用途:为 BTST 插件页面注册页面级 AI 上下文,使 AI 聊天组件能理解当前页面并对其执行操作,例如填写表单、更新编辑器、总结内容。
主要能力:通过 useRegisterPageAIContext 在 .internal.tsx 页面组件内调用。只读内容页可传入 routeName、pageDescription、suggestions;数据加载期间传 null,非 null 时才注册上下文。表单或编辑器页可额外提供 clientTools,在客户端执行操作。
用法:从 @btst/stack/plugins/ai-chat/client/context 导入 useRegisterPageAIContext。clientTools 仅在客户端执行,需返回 { success: boolean, message?: string }。第一方工具需在 src/plugins/ai-chat/api/page-tools.ts 的 BUILT_IN_PAGE_TOOL_SCHEMAS 中添加服务端 schema,不写 execute,执行由客户端处理。PageAIContextProvider 必须包裹根布局,且位于所有 StackProvider 之上;monorepo 示例应用已接好,消费者应用集成 ai-chat 插件时在根布局添加一次。
参考示例:new-post-page.internal.tsx 使用 fillBlogForm(clientTools);post-page.internal.tsx 使用只读上下文;page-builder-page.internal.tsx 使用 updatePageLayers(clientTools)。
限制:clientTools 仅客户端执行;上下文在传入非 null 前不会注册;PageAIContextProvider 位置有严格要求。