> For the complete documentation index, see [llms.txt](https://tinyhumans.gitbook.io/openhuman/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tinyhumans.gitbook.io/openhuman/zh/kai-fa/embed/architecture.md). Agents can also search and read this documentation through its [MCP server](https://tinyhumans.gitbook.io/openhuman/~gitbook/mcp).

# 嵌入式架构

宿主围绕一个核心组合基础设施；代理基于其运行时派生带作用域的上下文。

Embed 是上层的库门面 `openhuman-core`。TinyHumans 层安装后端 SDK 传输，并负责产品登录/会话行为；RPC 层增加 JSON-RPC、服务器监听以及打包的主机启动。Rust embedder 在自己负责这些关注点时，可以停留在门面层。

## 所有权边界

| 组件                     | 拥有                                |
| ---------------------- | --------------------------------- |
| 你的应用                   | UI/HTTP 传输、用户认证、提供的凭据、应用工具和资源生命周期 |
| OpenHuman 核心和 Embed 门面 | 运行时组合、按代理的上下文、策略、审批、沙箱选择和类型化领域调用  |
| TinyAgents             | 模型/工具循环、解析、方言、持久会话和记录回放           |
| TinyInference          | 原生模型/提供方契约和通用有序回退                 |
| TinyTools              | 共享工具 trait 和契约类型                  |
| 存储和模块库                 | 它们的后端/模块实现；OpenHuman 负责适配和编排它们    |

库链为 core → Embed → TinyHumans → RPC → app/CLI/TUI。随附的宿主依赖其正下方的一层，并使用其精选门面。Embed 用户不应在文档隐藏的 `__host` 内部之上构建新的集成。

## 运行时组合与代理作用域

Cargo 特性决定构件中存在哪些实现。运行时域、服务、工具组和模块选择会缩小这些已编译的选项。代理规范进一步缩小运行时范围，并增加提供方、访问、提示词、技能、MCP 以及操作目录选择。

一次轮次通过代理的原生上下文分发，而不是通过 JSON-RPC 或操作员回退。递归子上下文会继承父宿主覆盖，包括自定义提供方和钩子组合，而独立的兄弟代理仍保持各自的作用域。

```rust
    let analyst_dir = tempfile::tempdir()?;
    let writer_dir = tempfile::tempdir()?;
    let analyst = runtime.agent(
        AgentSpec::new("analyst")
            .system_prompt("ANALYST_PROMPT: 总结文档")
            .action_dir(analyst_dir.path())
            .access(openhuman_embed::Access::readonly()),
    )?;
    let writer = runtime.agent(
        AgentSpec::new("writer")
            .system_prompt("WRITER_PROMPT: 编写说明")
            .action_dir(writer_dir.path())
            .access(openhuman_embed::Access::full()),
    )?;
    assert_ne!(analyst.action_dir(), writer.action_dir());
    assert_ne!(analyst.home_dir(), writer.home_dir());
    assert_ne!(analyst.workspace_dir(), analyst.action_dir());
    assert!(!analyst.run("Analyze").await?.reply.is_empty());
    assert!(!writer.run("Explain").await?.reply.is_empty());
    if support::offline() {
        let requests = support::chat_requests(&provider).await;
        assert_eq!(requests.len(), 2);
        assert!(String::from_utf8_lossy(&requests[0].body).contains("ANALYST_PROMPT"));
        assert!(!String::from_utf8_lossy(&requests[0].body).contains("WRITER_PROMPT"));
        assert!(String::from_utf8_lossy(&requests[1].body).contains("WRITER_PROMPT"));
    }
    println!("已验证两个不同的提示词和操作工作区");
```

[完整可运行示例](https://github.com/tinyhumansai/openhuman/blob/main/crates/openhuman-embed/examples/two_agents.rs)，包括运行时构建、导入和测试夹具设置。

## 生命周期与隔离

即使运行时句柄被释放，代理句柄也会让核心保持存活。显式移除会将该实例标记为已移除，取消轮次和回调，拒绝挂起的审批，并清除其状态/MCP 注册。即使是自然的最后句柄销毁以及已启动的移除被取消，ID 在清理期间也会保持保留。经过指针检查的释放可防止旧的保留实例移除更新的注册项。

代理隔离不等于凭据隔离：所有普通运行时代理共享操作员的配置路径和凭据根。使用 [配置文件](https://tinyhumans.gitbook.io/openhuman/zh/kai-fa/embed/concepts/profiles-saas.md) 用于独立认证的用户。参见 [存储架构](https://tinyhumans.gitbook.io/openhuman/zh/kai-fa/architecture/storage.md) 关于作用域/驱动所有权，以及 [代理测试桩架构](https://tinyhumans.gitbook.io/openhuman/zh/kai-fa/architecture/agent-harness.md) 关于会话执行。
