@gsc-basic/ai
v0.1.0
Published
AI interaction primitives and workspace components for GSC Basic
Readme
@gsc-basic/ai
@gsc-basic/ai 是 GSC Basic 的 AI 能力域包。当前首个模块是基于 @antdv-next/x 的 Vue AI 工作区,提供四种交互模式;后续 Agent、Harness 等能力可以继续归入同一能力域,Node CLI 则应保持独立运行时入口或拆分为独立包。
四种模式
| 组件 | 适用场景 | 默认交互 |
| --- | --- | --- |
| AiChatUltramodern | 现代感单任务工作区 | 居中的 Agent 名称、深度思考开关、可配置 Sender.slotConfig |
| AiChatIndependent | 独立多会话工作台 | 会话侧栏、欢迎引导、提示词分组、消息操作 |
| AiChatCopilot | 贴边协作场景 | 内容插槽、可收起助手面板、会话浮层、快捷操作 |
| AiChatTbox | 工具型智能体场景 | 会话侧栏、任务提示词、执行状态和思考链 |
四个组件共享同一个 useXChat / useXConversations 状态层,Provider、历史消息、请求参数、消息渲染和输入区均可由宿主配置或替换。
安装
pnpm add @gsc-basic/ai @antdv-next/x @antdv-next/x-card @antdv-next/x-markdown @antdv-next/x-sdk @antdv-next/icons antdv-next vueimport { AiChatIndependent } from '@gsc-basic/ai';
import '@gsc-basic/ai/style.css';最小使用
<script setup>
import { AiChatIndependent } from '@gsc-basic/ai';
const provider = /* 创建 @antdv-next/x-sdk Provider */ null;
</script>
<template>
<AiChatIndependent
:provider="provider"
:use-mock-provider="!provider"
height="720px"
/>
</template>未传 provider 时默认使用本地 mock 流式 Provider,适合 Storybook、联调和布局开发。生产环境应传入自定义 Provider,并由服务端代理模型请求,不在前端配置 API Key。
配置面
四个组件支持同一组 props。常用配置包括:
- Provider:
provider、providerFactory、requestParams、requestFallback、useMockProvider、mockReplies - 会话:
defaultConversations、defaultActiveConversationKey、initialMessages、historyMessages、initialMessage - 文案:
locale、localeCode、title、agentName、welcomeTitle、welcomeDescription、placeholder - 引导:
quickActions、promptGroups、promptItems、senderPrompts、suggestions - 功能开关:
showSidebar、showConversations、showConversationMenu、showWelcome、showPrompts、showAttachments、showSpeech、showDeepThink、showThoughtChain、showActions、showFeedback - 组件透传:
xProviderProps、bubbleListProps、markdownProps、senderProps、attachmentProps、conversationsProps、promptProps、attachments、slotConfig - 布局主题:
height、sidebarWidth、copilotWidth、contentMaxWidth、open、theme、dark、deepThink
Events 与 slots
组件会发出 update:open、update:deep-think、submit、message-sent、messages-change、request-error、conversation-change、conversation-action、feedback-change、attachment-change 和 notify。
可用 slots 包括:
default:Copilot 左侧内容阅读区context:非 Copilot 模式顶部上下文区header、panel-header、logo、sidebar-footerempty:无消息时的欢迎区sender-footer:替换 Sender 底部操作区
包名决策
包名使用 @gsc-basic/ai,表达的是能力域而不是某一种 UI。这样未来可以在同一 monorepo 下增加:
@gsc-basic/ai-agent:Agent 编排和工具调用@gsc-basic/ai-harness:评测、回放和运行时测试@gsc-basic/ai-cli:Node CLI
当前包只发布浏览器端 Vue UI 和浏览器端 Provider 适配能力,不把 CLI 的 Node 依赖打入浏览器产物。若未来这些能力需要独立版本节奏,优先拆为上述子包,并由 @gsc-basic/ai 负责共享类型或聚合导出。
动态 Card
AiCard 封装了 @antdv-next/x-card 的 A2UI 运行时,支持 Catalog、组件映射、多个 Surface 和 Action 事件:
<script setup>
import { defineComponent, h } from 'vue';
import { AiCard } from '@gsc-basic/ai';
const Text = defineComponent({
props: { text: { type: String, default: '' } },
setup(props) {
return () => h('p', props.text);
},
});
const commands = [
{ version: 'v0.9', createSurface: { surfaceId: 'demo' } },
{
version: 'v0.9',
updateComponents: {
surfaceId: 'demo',
components: [{ id: 'root', component: 'Text', text: 'Hello GSC AI' }],
},
},
];
</script>
<template>
<AiCard
:commands="commands"
:components="{ Text }"
@action="handleCardAction"
/>
</template>生产环境应固定 A2UI Catalog 白名单,只暴露经过审核的组件和 Action。新项目优先使用 A2UI v0.9;组件同时兼容 x-card 支持的 v0.8 命令流。
独立请求工具
无需使用 Chat UI 时,可以直接从 @gsc-basic/ai 使用官方请求和流解析工具:
import { XRequest, XStream } from '@gsc-basic/ai';
const request = XRequest('/api/ai/chat', {
manual: true,
params: { stream: true },
});
request.run({ messages: [{ role: 'user', content: 'hello' }] });
const response = await fetch('/api/ai/stream');
for await (const chunk of XStream({ readableStream: response.body })) {
console.log(chunk);
}浏览器端不要配置模型 API Key。认证、模型选择和请求代理应由服务端或网关负责。
