# 函数元数据

每项信息只有一个归属。签名能表达的不在 `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.description` | `fn.input_meta["x"]["description"]` |
| 枚举 | `input.x.options` | `fn.input_meta["x"]["options"]` |
| 对 LLM 隐藏 | `input.x.hidden` | `fn.input_meta["x"]["hidden"]` |
| WebUI 占位符、多行 | `input.x.placeholder`, `input.x.multiline` | `fn.input_meta["x"]` |
| 动态选项 | `input.x.options_from: "functions"` | `fn.input_meta["x"]["options_from"]` |
| 覆盖环境系统提示词 | `system` | `fn.system` |
| 调用者在 DAG 渲染中看到什么：`"io"`、`"llm"`、`"full"`、`"hidden"` | `expose` | `fn.expose` |
| 此函数的 `llm()` 读取多少历史 | `render_range` | `fn.render_range` |
| Skill 触发词、Agent 发现 | 同级 `SKILL.md` frontmatter | Skill 加载器 |

```
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.callers` | `None`：本帧之前的节点不限。`0` 隔绝全部先前上下文。`N`：最近 N 个。 |
| `render_range.subcalls` | `-1`：本帧看到自己的全部进展。子调用内部由子调用的 `expose` 隐藏。`N` 用于限制长循环的提示词大小；`0` 隔绝本帧节点。 |
| 顶层聊天回合 | `frame_entry_seq=-1`：所有节点都在帧内，与其他帧同一路径 |
| 工具 | 裸 `runtime.exec`：完整注册表。`tools=[...]`：指定菜单。`toolset="none"` 或 `tools=[]`：无工具。嵌套 `exec` 继承（`_current_tools`）。 |
| `system` | `None`：使用运行时现有系统提示词 |

`render_range` 按 `seq` 统计节点。

## docstring 与提示词

|  | docstring | `llm(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_context` | `expose`、`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`：见[函数调用设计](https://fzkuji.github.io/OpenProgram/docs/reference/design/function/calling-unification.zh.html)。`cache` 和 `cache_ttl` 按名称和参数缓存。`timeout` 在 N 秒后停止函数体并返回错误结果。

**尚不存在的字段**

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

## 新方法检查清单

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

[Agent 调用与方法](https://fzkuji.github.io/OpenProgram/docs/capabilities/agentic-programming/writing-functions/agent.zh.html) · [工具调用](https://fzkuji.github.io/OpenProgram/docs/capabilities/agentic-programming/choosing-the-next-step/tool-calling.zh.html) · [下一步决策](https://fzkuji.github.io/OpenProgram/docs/capabilities/agentic-programming/choosing-the-next-step/next-step-decision.zh.html)
