OpenProgram Docs

函数元数据

每项信息只有一个归属。签名能表达的不在 input 里重复;input 能表达的不在 docstring 里重复。下文 input.x 指 method_options[method]["input"]["x"]。

字段位置读取
函数名def name(...)fn.__name__
参数名、类型、默认值签名inspect.signature(fn).parameters
一句话摘要:做什么、何时选docstring 第一段inspect.getdoc(fn)
单次调用的指令传给那次 llm() 的提示词调用时传入
参数描述input.x.descriptionfn.input_meta["x"]["description"]
枚举input.x.optionsfn.input_meta["x"]["options"]
对 LLM 隐藏input.x.hiddenfn.input_meta["x"]["hidden"]
WebUI 占位符、多行input.x.placeholder, input.x.multilinefn.input_meta["x"]
动态选项input.x.options_from: "functions"fn.input_meta["x"]["options_from"]
覆盖环境系统提示词systemfn.system
调用者在 DAG 渲染中看到什么:"io"、"llm"、"full"、"hidden"exposefn.expose
此函数的 llm() 读取多少历史render_rangefn.render_range
Skill 触发词、Agent 发现同级 SKILL.md frontmatterSkill 加载器
from openprogram import Agent

class ExampleAgent(Agent):
    method_options = {
        'review_essay': {'tool': True, 'input': {
            'essay': {'description': 'Essay to review.', 'placeholder': 'Paste the essay text here...', 'multiline': True},
            'rubric_id': {'description': 'Which rubric to apply.', 'options': ['ielts_writing', 'toefl_writing', 'gre_argument']},
            'max_score': {'description': 'Upper bound for the numeric score.'},
            'show_rubric_internals': {'description': 'Include rubric breakdown in the output.'},
            'session_id': {'hidden': True},   # filled by Python via context, not the LLM
        }},
    }

    def review_essay(self, essay: str, rubric_id: str, max_score: int,
                     show_rubric_internals: bool, session_id: str) -> dict:
        """Score an essay against a named rubric and return a structured report."""
        ...

_example_agent = ExampleAgent()
review_essay = _example_agent.review_essay

docstring 只保留摘要,不写 Args: 或 Returns:。下游需要的字段含义用 TypedDict 或 dataclass 返回类型表达。

默认值

方面默认
expose普通方法 "full";显式工具 "io"(名称、输入、输出,内部 llm() 调用隐藏)
render_range.callersNone:本帧之前的节点不限。0 隔绝全部先前上下文。N:最近 N 个。
render_range.subcalls-1:本帧看到自己的全部进展。子调用内部由子调用的 expose 隐藏。N 用于限制长循环的提示词大小;0 隔绝本帧节点。
顶层聊天回合frame_entry_seq=-1:所有节点都在帧内,与其他帧同一路径
工具裸 runtime.exec:完整注册表。tools=[...]:指定菜单。toolset="none" 或 tools=[]:无工具。嵌套 exec 继承(_current_tools)。
systemNone:使用运行时现有系统提示词

render_range 按 seq 统计节点。

docstring 与提示词

docstringllm(prompt)
范围整个函数一次模型调用;一个函数可有多次
读者人、决策菜单、工具 spec、元工具。作为 metadata.doc 携带,并渲染为描述性上下文。模型,作为权威指令
必须包含直接说明的一句话摘要,可写更多细节。不写“You are a helpful assistant”之类的套话,不角色扮演,不用比喻。指令、数据和确切的输出格式,即使 docstring 已写过
llm([{"type": "text", "text": (
    f"Classify the sentiment of the following text. Reply with exactly one "
    f"word: positive, negative, or neutral.\n\nText:\n{text}"
)}])

每项是字典,如 {"type": "text", "text": ...} 或 {"type": "image", "path": ...}。默认系统提示词来自环境运行时和 skills 块,不来自 docstring。

谁读取什么

使用方需要
runtime.exec(tools=[fn])名称、描述、参数 JSON schema
render_options(options)名称、何时选、参数名、类型、描述、枚举、是否系统填充
parse_args(reply, options, ...)参数名、类型、枚举、隐藏标记、自动注入列表
WebUI 表单description, placeholder, multiline, options, hidden
render_contextexpose、render_range
追踪与持久化身份、参数、返回值、时间

工具 spec 的描述回退顺序:description,然后 placeholder(写成“e.g. …”),最后只有名称和类型。render_options 只读 description 和 options。两者都不读 docstring 的 Args:,旧式 Args: 能运行,但在这里不起作用。

普通可调用对象

render_options、decision.make 和 choices= 接受。runtime.exec(tools=[...]) 只接受 Agent 方法、{"spec", "execute"} 字典或带 .spec 和 .execute 的对象,否则抛 TypeError。

Agent 方法普通可调用对象
参数描述input空
枚举input.x.options无
隐藏input.x.hidden仅自动注入的名称
会话 DAG启动执行时记录不记录

适合参数少、没有 WebUI 界面的简单分支。

自动注入的参数

名称值
runtime当前 Runtime
exec_runtime多 Runtime 场景的执行 Runtime
review_runtime多 Runtime 场景的评审 Runtime

对 LLM 隐藏。新方法不声明它们:方法运行在其 Agent 或调用者的 Runtime 上;注入保留给现有函数。新增名称要同时改 openprogram/runtime/agent/call_state.py 的 _RUNTIME_PARAMS 和 decision.py 的 _AUTO_PARAMS,不要逐处用 hidden。

WebUI 表单控件

参数控件
bool是、否按钮
str,未设 multiline多行文本框
str, multiline: False单行输入
其他类型,未设 multiline单行输入
options可点选项加自由输入
options_from: "functions"已注册的非内置、非元函数下拉框
hidden: True不显示
有 Python 默认值、无 placeholder默认值作为占位文字;在空字段按 Tab 填入。None 和以 _ 开头的值不显示。

description 是名称旁的小标签,同一行显示类型,非必填带“optional”标记。已注册函数使用实时 input_meta;否则 function_catalog.py 解析源码,只有字面量 input 字典才显示。

工具注册和门控选项

name、description、toolset、unsafe_in、requires_approval、check_fn、available_if、defer:见函数调用设计。cache 和 cache_ttl 按名称和参数缓存。timeout 在 N 秒后停止函数体并返回错误结果。

尚不存在的字段

effects、permissions、idempotent、latency_hint 和 cost_hint 未实现:还没有使用方。门控已有上面的选项。

新方法检查清单

项目规则
函数名清晰:LLM 把它当作动作名
docstring以一句话摘要开头,不写 Args: 或 Returns:
LLM 可见参数每个都有 description
枚举用 options,不写在描述里
系统填充的参数hidden: True
runtime 参数没有

Agent 调用与方法 · 工具调用 · 下一步决策

Last updated · 2026-11-25