@rayvision/ai-chat 0.0.0-stage → 0.0.2-beta

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/README.md +166 -2
  2. package/dist/index.d.ts +7 -0
  3. package/dist/index.js +54 -0
  4. package/dist/index.mjs +13638 -0
  5. package/dist/src/ai-chat.d.ts +4 -0
  6. package/dist/src/components/attachment-image-modal.d.ts +9 -0
  7. package/dist/src/components/chat-composer.d.ts +16 -0
  8. package/dist/src/components/chat-header.d.ts +10 -0
  9. package/dist/src/components/chat-launcher.d.ts +9 -0
  10. package/dist/src/components/chat-messages.d.ts +14 -0
  11. package/dist/src/components/chat-toast.d.ts +9 -0
  12. package/dist/src/components/message-bubble.d.ts +10 -0
  13. package/dist/src/components/modal.d.ts +11 -0
  14. package/dist/src/components/panel.d.ts +3 -0
  15. package/dist/src/components/popover.d.ts +11 -0
  16. package/dist/src/components/portal.d.ts +10 -0
  17. package/dist/src/components/tooltip.d.ts +11 -0
  18. package/dist/src/context/ai-chat-ui-context.d.ts +20 -0
  19. package/dist/src/controller/ai-chat-controller.d.ts +2 -0
  20. package/dist/src/hooks/use-ai-chat-layout.d.ts +10 -0
  21. package/dist/src/i18n/attachment-error-messages.d.ts +2 -0
  22. package/dist/src/i18n/index.d.ts +5 -0
  23. package/dist/src/i18n/messages.en.d.ts +61 -0
  24. package/dist/src/i18n/messages.zh.d.ts +61 -0
  25. package/dist/src/i18n/phase-labels.d.ts +2 -0
  26. package/dist/src/i18n/types.d.ts +1 -0
  27. package/dist/src/markdown/render.d.ts +3 -0
  28. package/dist/src/services/customer-v1/attachments/client.d.ts +9 -0
  29. package/dist/src/services/customer-v1/client.d.ts +9 -0
  30. package/dist/src/services/customer-v1/error.d.ts +3 -0
  31. package/dist/src/services/customer-v1/protocol.d.ts +93 -0
  32. package/dist/src/services/customer-v1/sse-parser.d.ts +2 -0
  33. package/dist/src/store/ai-chat-store.d.ts +41 -0
  34. package/dist/src/theme/global-style.d.ts +24 -0
  35. package/dist/src/theme/panel-style.d.ts +6 -0
  36. package/dist/src/types.d.ts +294 -0
  37. package/dist/src/utils/build-chat-content.d.ts +6 -0
  38. package/dist/src/utils/copy-text.d.ts +1 -0
  39. package/dist/src/utils/format-chat-file-error.d.ts +3 -0
  40. package/package.json +31 -4
package/README.md CHANGED
@@ -1,3 +1,167 @@
1
- # Temporary Holding Version
1
+ # @rayvision/ai-chat
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Renderbus AI Chat React 组件,提供流式问答、Markdown、引用、建议问题、附件上传、会话恢复和中英文界面。
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ npm install @rayvision/ai-chat
9
+ ```
10
+
11
+ 需要 React 和 React DOM 18 或更高版本。
12
+
13
+ ## 快速开始
14
+
15
+ ```tsx
16
+ import { useRef, useState } from 'react'
17
+ import { AIChat, createAIChatController, createCustomerV1ChatAdapter } from '@rayvision/ai-chat'
18
+
19
+ export function App(): JSX.Element {
20
+ const [container, setContainer] = useState<HTMLDivElement | null>(null)
21
+ const controller = useRef(
22
+ createAIChatController({
23
+ adapter: createCustomerV1ChatAdapter(),
24
+ }),
25
+ ).current
26
+
27
+ return (
28
+ <>
29
+ <div ref={setContainer} />
30
+ <AIChat container={container} controller={controller} language='zh' theme='light' />
31
+ </>
32
+ )
33
+ }
34
+ ```
35
+
36
+ `AIChat` 会把界面挂载到 `container` 的 Shadow DOM 中,并自动注入组件样式。`container` 为 `null` 时组件不会渲染。
37
+
38
+ 对于 Vue 等非 React 宿主,可以直接使用 `mountAIChat`:
39
+
40
+ ```ts
41
+ import { mountAIChat } from '@rayvision/ai-chat'
42
+
43
+ const chat = mountAIChat(document.querySelector('#ai-chat')!, {
44
+ language: 'zh',
45
+ launcher: false,
46
+ })
47
+
48
+ chat.controller.toggle(true)
49
+ // 页面卸载时调用
50
+ chat.destroy()
51
+ ```
52
+
53
+ 该 API 会在包内部创建 React root、controller 和 Shadow DOM,宿主只需要提供一个 DOM 容器。
54
+
55
+ ## Gateway 适配器
56
+
57
+ 默认适配器请求同源地址 `/api/ai-assistant/v1`,使用浏览器 Cookie 携带会话信息:
58
+
59
+ ```tsx
60
+ const adapter = createCustomerV1ChatAdapter({
61
+ baseUrl: '/api/ai-assistant/v1',
62
+ module: 'tasks',
63
+ knowledgeScope: 'public',
64
+ })
65
+
66
+ const controller = createAIChatController({ adapter })
67
+ ```
68
+
69
+ 选项:
70
+
71
+ | 选项 | 类型 | 说明 |
72
+ | ---------------- | ---------- | ------------------------------------------------ |
73
+ | `baseUrl` | `string` | Gateway v1 基础地址,默认 `/api/ai-assistant/v1` |
74
+ | `module` | `string` | 建议问题和请求使用的业务模块 |
75
+ | `knowledgeScope` | `'public'` | 知识库范围 |
76
+
77
+ 宿主应用需要保证 `baseUrl` 可访问,并按服务端要求配置 Cookie、跨域和代理。
78
+
79
+ ## 组件配置
80
+
81
+ ```tsx
82
+ <AIChat container={container} controller={controller} language='en' theme='dark' launcher={false} />
83
+ ```
84
+
85
+ | 属性 | 类型 | 默认值 | 说明 |
86
+ | ------------ | -------------------------------- | ------------------- | --------------------- |
87
+ | `container` | `HTMLElement \| null` | 无 | Shadow DOM 的宿主节点 |
88
+ | `controller` | `AIChatController` | 必填 | 管理会话、消息和请求 |
89
+ | `language` | `'zh' \| 'en'` | `'zh'` | 界面语言 |
90
+ | `theme` | `'light' \| 'dark'` | `'light'` | 初始主题 |
91
+ | `launcher` | `boolean \| AIChatLauncherProps` | `true` | 是否显示悬浮打开按钮 |
92
+ | `adapter` | `ChatAdapter` | controller 的适配器 | 替换请求实现 |
93
+
94
+ 通过 `launcher` 对象可以设置按钮属性:
95
+
96
+ ```tsx
97
+ <AIChat
98
+ container={container}
99
+ controller={controller}
100
+ launcher={{
101
+ disabled: false,
102
+ className: 'my-ai-chat-launcher',
103
+ }}
104
+ />
105
+ ```
106
+
107
+ ## 控制器
108
+
109
+ ```tsx
110
+ const controller = createAIChatController({
111
+ defaultOpen: false,
112
+ })
113
+
114
+ await controller.initialize()
115
+ controller.toggle(true)
116
+ controller.setLanguage('zh')
117
+ controller.setDraft('如何提交渲染任务?')
118
+ controller.send()
119
+ controller.cancel()
120
+ controller.clear()
121
+ ```
122
+
123
+ 常用方法:
124
+
125
+ - `initialize()`:加载建议问题和附件配置。
126
+ - `toggle(show?)`:打开或关闭聊天面板。
127
+ - `setDraft(draft)`、`send()`:设置并发送文本问题。
128
+ - `addFiles(files)`、`removeAttachment(id)`:管理附件。
129
+ - `restoreConversation(conversationId, limit?)`:恢复历史会话。
130
+ - `cancel()`:取消当前流式请求。
131
+ - `destroy()`:释放控制器资源。
132
+
133
+ ## 自定义适配器
134
+
135
+ 如果后端不是 Gateway v1,可以实现 `ChatAdapter` 并注入控制器:
136
+
137
+ ```tsx
138
+ import type { ChatAdapter } from '@rayvision/ai-chat'
139
+
140
+ const adapter: ChatAdapter = {
141
+ send(input, handlers) {
142
+ // 调用业务接口,并通过 handlers.onDelta / onComplete / onError
143
+ // 把流式结果通知给组件。
144
+ return {
145
+ cancel() {
146
+ // 取消请求
147
+ },
148
+ }
149
+ },
150
+ }
151
+
152
+ const controller = createAIChatController({ adapter })
153
+ ```
154
+
155
+ `send` 至少需要实现 `onDelta`、`onComplete` 和 `onError` 对应的调用逻辑。附件、建议问题、历史会话和引用详情为可选能力。
156
+
157
+ ## 导出内容
158
+
159
+ 主要导出包括:
160
+
161
+ - 组件:`AIChat`
162
+ - 工具:`mountAIChat`、`createAIChatController`、`createCustomerV1ChatAdapter`
163
+ - 类型:`AIChatProps`、`AIChatController`、`ChatAdapter`、`ChatMessage`、`Citation`、`Attachment`
164
+
165
+ ## 浏览器要求
166
+
167
+ 组件使用 `fetch`、`AbortController`、`Shadow DOM` 和 `File` API。请在支持这些 Web API 的现代浏览器中使用。
@@ -0,0 +1,7 @@
1
+ export { AIChat, mountAIChat } from './src/ai-chat';
2
+ export type { AIChatModalProps } from './src/components/modal';
3
+ export { createAIChatController } from './src/controller/ai-chat-controller';
4
+ export { createCustomerV1ChatAdapter } from './src/services/customer-v1/client';
5
+ export type { CustomerV1ChatAdapterOptions } from './src/services/customer-v1/client';
6
+ export type { AIChatMessages } from './src/i18n';
7
+ export type { AIChatController, AIChatControllerOptions, Attachment, AttachmentOptions, Citation, ConversationItem, GatewayError, PendingAttachment, SuggestedQuestion, AIChatLanguage, AIChatTheme, AIChatLauncherProps, AIChatProps, AIChatMountOptions, AIChatMountResult, ChatAdapter, ChatContentPart, ChatImage, ChatMessage, ChatSendInput, ChatTextFile, ImageContentPart, TextContentPart, TextFileContentPart, } from './src/types';