# 嵌入你自己的技术栈

库调用不会启动 Web UI 或终端 UI，也不能代替完整安装的产品。

## 宿主 Runtime

```
from openprogram import Agent, Runtime


def model_call(content, model="host-model", response_format=None):
    # Replace this offline response with the host's model client.
    return "Host model response"

runtime = Runtime(call=model_call, model="host-model")

class ReviewAgent(Agent):
    tools = []

    def classify(self, review: str) -> str:
        """Classify review sentiment."""
        return self(f"Classify review sentiment:\n{review}")

    def summarize(self, review: str) -> str:
        """Summarize a review."""
        sentiment = self.classify(review)
        return self(f"Summarize this {sentiment} review:\n{review}")

reviewer = ReviewAgent(runtime=runtime)
try:
    summary = reviewer.summarize("The service was fast.")
finally:
    runtime.close()
```

回调收到内容块、模型名和可选的响应格式；这个示例返回固定响应，无需联网。构造 Runtime 不发模型请求。宿主负责关闭自己提供的 Runtime。Agent 方法自动继承它；Context 和函数作用域是自动的。

## 显式会话

```
from openprogram import Agent, Context
from openprogram.state.store import SessionStore, SessionNodeWriter

store = SessionStore(root_path="/var/lib/myapp/sessions")
store.create_session("research", agent_id="main")
writer = SessionNodeWriter(store, "research")
researcher = Agent(context=Context(store=writer), tools=[])
try:
    first = researcher("List the project requirements.")
    second = researcher("Review the requirements from the previous call.")
finally:
    store.close()
```

默认的 Agent 调用彼此独立；`Context(store=writer)` 延续这个会话。`head_id` 可选择已有分支末端。store 和 Runtime 归宿主所有。

## 查看调用

```
from openprogram.state.store import SessionNodeWriter

for node in SessionNodeWriter(store, "review-42").load():
    print(node.name, node.caller, node.output)
```

要把方法暴露为模型工具，在 `method_options` 里设 `tool=True`，它提供 `.spec` 和 `.execute`。仅被记录不授予工具权限。

## 宿主钩子

| 钩子 | 位置 |
| --- | --- |
| `set_cancellation_check`、`CancelledError`、`check_cancelled` | `openprogram.host.cancellation` |
| `set_session_id_provider`（问题路由） | `openprogram.runtime.agent.call_state` |

导入检查：[`test_standalone_embed.py`](https://github.com/Fzkuji/OpenProgram/blob/main/tests/component/runtime/test_standalone_embed.py)。

[Runtime](https://fzkuji.github.io/OpenProgram/docs/reference/api/runtime.zh.html) · [Agent 与 Context API](https://fzkuji.github.io/OpenProgram/docs/reference/api/agent.zh.html) · [Agent 调用与方法](https://fzkuji.github.io/OpenProgram/docs/capabilities/agentic-programming/writing-functions/agent.zh.html)
