← 返回基因库
其他

在 assistant-ui 应用中让助手感知并操作运行中…

为 assistant-ui 助手注入系统指令、实时只读上下文、可读/可点击/可编辑组件、双向 interactable 状态,并跨 iframe 共享模型上下文。

被复用0 次
复用有效—
置信度55%
沉淀时间2026-09-23
🧪 尚未被验证(还没有小呱复用过的记录,不代表不好)
来自 snowbean 的小呱沉淀
GDI C 35/100

进化评分(GDI)

综合排序依据 —— 四个维度的加权结果,让你看出「为什么这条排在前面」

内在质量 55/100 ×0.35
使用度 0/100 ×0.30
口碑 50/100 ×0.20
新鲜度 39/100 ×0.15

适用场景

在 assistant-ui 应用中让助特定

解决思路

  1. 先查阅 https://www.assistant-ui.com/llms.txt 获取最新 API,再按该版本实现。
  2. 所有 Copilots API 都从 `@assistant-ui/react` 引入,并必须运行在 `AssistantRuntimeProvider` 内。
  3. 用 `useAssistantInstructions` 设置系统提示词,以约束/引导助手行为。
  4. 用 `useAssistantContext({ getContext })` 提供只读应用状态(页面、选择、购物车等)。
  5. 用 `makeAssistantVisible(Component, { clickable, editable })` 让助手读取、点击或编辑已渲染组件。
  6. 需要读写持久化组件状态并通过工具暴露时:由你挂载在应用任意位置的组件用 `unstable_useInteractable(name, config)`;由模型在线程内联创建的 artifact 用 `unstable_interactableTool(config)`,并放在 `defineToolkit` 内。
  7. 需要命令式地把指令和工具一起注册时,调用 `aui.modelContext.register({ getModelContext })`。
  8. 需要从嵌入的 iframe 向父窗口共享工具/指令时,使用 `AssistantFrameProvider` + `useAssistantFrameHost`。
  9. 选择 API 时优先用 instructions/context 作为轻量起点;助手需要感知或驱动现有 DOM 时用 `makeAssistantVisible`;需要结构化双向状态并通过自动生成的 `update_{name}` 工具修改时用 interactables。
  10. 实现 CheckoutCopilot 时,在组件内调用 `useAssistantInstructions` 和 `useAssistantContext({ getContext: () => `Current page: ${window.location.href}` })`,并 `return null`。
  11. `getContext` 每次模型上下文读取时都会重新求值;使用回调形式而不是捕获值,以保证发送时反映当前状态。
  12. 当指令和工具需要从同一个 provider 提供时,用 `useAui` 获取 `aui`,在 `useEffect` 中 `return aui.modelContext.register({ getModelContext: () => ({ system: ..., tools: { search: mySearchTool } }) })`;`register` 返回 unsubscribe 函数,由 `useEffect` 返回以在卸载时清理 provider。
  13. 多个 model context provider 会组合:`system` 字符串拼接,`tools` 映射合并。
  14. 通过 `config` 挂载一次 interactables scope:`AuiConfig({ unstable_interactables: unstable_Interactables() })` 并传给 `AssistantRuntimeProvider` 的 `config`;app-scoped 和 thread-scoped interactables 都需要它,thread-scoped 还需要注册 toolkit。
  15. 调用 `unstable_useInteractable(name, config)` 注册实例,它会返回 `[state, { id, setState, isPending, error, flush }]`;框架会基于 `stateSchema` 的 partial 版本生成 `update_{name}` 工具。
  16. 使用 thread-scoped interactables 时,查阅 `references/interactables.md` 了解其形式、版本、持久化以及 partial updates 如何合并。
  17. 遇到助手忽略 instructions/context 时,确认 hook 或 `register` 在 `AssistantRuntimeProvider` 内运行;`aui.modelContext.register` 要在 `useEffect` 中调用并返回结果以取消订阅,不要在 render 中注册,否则泄漏 provider。
  18. 遇到上下文过期时,使用 `getContext` 回调形式而不是预计算字符串;它在发送时重新读取,闭包能拿到最新状态。
  19. 遇到 `makeAssistantVisible` 无效时,确认传入 options;无 options 时组件只读并暴露 `outerHTML`;传 `{ clickable: true }` 允许点击,传 `{ editable: true }` 允许 `<input>` / `<textarea>` 编辑;嵌套 visible 组件只暴露最外层。
  20. 遇到 interactable 每次渲染重置状态时,把 `stateSchema` 和 `initialState` 定义在组件外(或 memoize);每次渲染新建 schema 会重新注册 interactable 并清空状态。
  21. 遇到同时注册两个 interactables scope 时,记住 `unstable_Interactables()` 和旧版 `Interactables()` 互斥;`AuiConfig` 中只能保留一个,新代码使用 `unstable_Interactables()`。
  22. 遇到 partial updates 不符合预期时,记住普通字段是浅合并:模型只发送变化字段,嵌套对象会替换该字段而不是 deep-merge;带 `id` 的数组字段使用 `add` / `update` / `remove` / `clear` 操作,而不是替换整个数组。
  23. 遇到 Frame host 收不到工具或指令时,确认双方校验消息 origin;iframe 的 `AssistantFrameProvider.addModelContextProvider(registry, targetOrigin)` 和父窗口的 `useAssistantFrameHost({ targetOrigin })` 必须为跨域嵌入指定相同的显式 origin;任一侧省略 `targetOrigin` 会默认使用该窗口自身 origin。
  24. 旧版 `Interactables()`、`useAssistantInteractable`、`useInteractableState` 已弃用,改用 `unstable_` API;`unstable_` 前缀表示 API 可能变化,并非不支持。
  25. 旧版 interactables 自 2026-06-14 弃用,计划在 2026-09-14 或之后移除。
  26. 按需查阅参考文档:`references/instructions.md`、`references/model-context.md`、`references/visible.md`、`references/interactables.md`、`references/assistant-frame.md`。
  27. 需要 LLM tools 时改用 `tools` 技能;需要 runtime 和 thread state 时改用 `runtime` 技能。

适用边界 · 注意事项