-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Expand file tree
/
Copy pathtypes.ts
More file actions
558 lines (478 loc) · 13.5 KB
/
types.ts
File metadata and controls
558 lines (478 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
import type { LanguageModelUsage } from 'ai'
import { v4 as uuidv4 } from 'uuid'
import type { MCPServerConfig } from '@/packages/mcp/types'
export interface SearchResultItem {
title: string
link: string
snippet: string
}
export interface SearchResult {
items: SearchResultItem[]
}
export interface MessageFile {
id: string
name: string
fileType: string
url?: string
storageKey?: string
chatboxAIFileUUID?: string
}
export interface MessageLink {
id: string
url: string
title: string
storageKey?: string
chatboxAILinkUUID?: string
}
export interface MessageCitation {
id: string
url: string
title: string
number: number // Citation number like [1], [2], etc.
}
export interface MessagePicture {
url?: string
storageKey?: string
loading?: boolean
}
export const MessageRoleEnum = {
System: 'system',
User: 'user',
Assistant: 'assistant',
Tool: 'tool',
} as const
export type MessageRole = (typeof MessageRoleEnum)[keyof typeof MessageRoleEnum]
export type MessageTextPart = { type: 'text'; text: string }
export type MessageImagePart = { type: 'image'; storageKey: string }
export type MessageToolCallPart<Args = unknown, Result = unknown> = {
type: 'tool-call'
state: 'call' | 'result' | 'error'
toolCallId: string
toolName: string
args: Args
result?: Result
}
export type MessageContentParts = (MessageTextPart | MessageImagePart | MessageToolCallPart)[]
export type StreamTextResult = {
contentParts: MessageContentParts
reasoningContent?: string
usage?: LanguageModelUsage
citations?: MessageCitation[]
}
// Chatbox 应用的消息类型
export interface Message {
id: string // 当role为tool时,id为toolCallId
role: MessageRole
// 把这个字段注释是为了避免新的引用,兼容老数据的时候还是可以读取
// content?: string // contentParts 有值的时候用contentParts
name?: string // 之前不知道是干什么的,现在用于role=tool时存储tool name
cancel?: () => void
generating?: boolean
aiProvider?: ModelProvider | string
model?: string
style?: string // image style
// pictures?: MessagePicture[] // 迁移到 contentParts 中
files?: MessageFile[] // chatboxai 专用
links?: MessageLink[] // chatboxai 专用
citations?: MessageCitation[] // Perplexity AI citations
// webBrowsing?: MessageWebBrowsing // chatboxai 专用, (已废弃)
// toolCalls?: MessageToolCalls // 已废弃,使用contentParts代替
reasoningContent?: string
contentParts: MessageContentParts
errorCode?: number
error?: string
errorExtra?: {
[key: string]: any
}
status?: (
| {
type: 'sending_file'
mode?: 'local' | 'advanced'
}
| {
type: 'loading_webpage'
mode?: 'local' | 'advanced'
}
)[]
wordCount?: number // 当前消息的字数
tokenCount?: number // 当前消息的 token 数量
tokensUsed?: number // 生成当前消息的 token 使用量
timestamp?: number // 当前消息的时间戳
firstTokenLatency?: number // AI 回答首字耗时(毫秒) - 从发送请求到接收到第一个字的时间间隔
}
export type SettingWindowTab = 'ai' | 'display' | 'chat' | 'advanced' | 'extension' | 'mcp'
export type ExportChatScope = 'all_threads' | 'current_thread'
export type ExportChatFormat = 'Markdown' | 'TXT' | 'HTML'
export type SessionType = 'chat' | 'picture'
export function isChatSession(session: Session) {
return session.type === 'chat' || !session.type
}
export function isPictureSession(session: Session) {
return session.type === 'picture'
}
type ClaudeParams = {
thinking: {
type: 'enabled' | 'disabled'
budgetTokens: number
}
}
type OpenAIParams = {
reasoningEffort: 'low' | 'medium' | 'high' | null
}
type GoogleParams = {
thinkingConfig: {
thinkingBudget: number
includeThoughts: boolean
}
}
export type ProviderOptions = {
claude?: Partial<ClaudeParams>
openai?: Partial<OpenAIParams>
google?: Partial<GoogleParams>
}
export type SessionSettings = Partial<{
provider: ModelProvider
modelId: string
maxContextMessageCount: number
temperature: number
topP: number
dalleStyle: 'vivid' | 'natural'
imageGenerateNum: number // 生成图片的数量
providerOptions?: ProviderOptions
}>
export interface Session {
id: string
type?: SessionType // undefined 为了兼容老版本 chat
name: string
picUrl?: string
messages: Message[]
starred?: boolean
copilotId?: string
assistantAvatarKey?: string // 助手头像的 key
settings?: SessionSettings // 会话设置
threads?: SessionThread[] // 历史话题列表
threadName?: string // 当前话题名称
messageForksHash?: Record<
string,
{
position: number // 当前分叉列表的游标
lists: {
id: string // fork list id
messages: Message[]
}[]
createdAt: number
}
> // 消息 ID 对应的分叉数据
}
export type SessionMeta = Pick<Session, 'id' | 'name' | 'starred' | 'assistantAvatarKey' | 'picUrl' | 'type'>
// 话题
export interface SessionThread {
id: string
name: string
messages: Message[]
createdAt: number
}
export interface SessionThreadBrief {
id: string
name: string
createdAt?: number
createdAtLabel?: string
firstMessageId: string
messageCount: number
}
export function createMessage(role: MessageRole = MessageRoleEnum.User, content: string = ''): Message {
return {
id: uuidv4(),
contentParts: content ? [{ type: 'text', text: content }] : [], // 防止为 undefined 或 null
role: role,
timestamp: new Date().getTime(),
}
}
export enum ModelProviderEnum {
ChatboxAI = 'chatbox-ai',
OpenAI = 'openai',
Azure = 'azure',
ChatGLM6B = 'chatglm-6b',
Claude = 'claude',
Gemini = 'gemini',
Ollama = 'ollama',
Groq = 'groq',
DeepSeek = 'deepseek',
SiliconFlow = 'siliconflow',
VolcEngine = 'volcengine',
LMStudio = 'lm-studio',
Perplexity = 'perplexity',
XAI = 'xAI',
Custom = 'custom',
}
export type ModelProvider = ModelProviderEnum | string
export type ProviderModelInfo = {
modelId: string
nickname?: string
labels?: string[]
capabilities?: ('vision' | 'reasoning' | 'tool_use')[]
contextWindow?: number
maxOutput?: number
}
export type BuiltinProviderBaseInfo = {
id: ModelProvider
name: string
type: ModelProviderType
isCustom?: false
urls?: {
website?: string
apiKey?: string
docs?: string
models?: string
}
defaultSettings?: ProviderSettings
}
export type CustomProviderBaseInfo = Omit<BuiltinProviderBaseInfo, 'id' | 'isCustom'> & {
id: string
isCustom: true
}
export type ProviderBaseInfo = BuiltinProviderBaseInfo | CustomProviderBaseInfo
export type ProviderSettings = Partial<{
apiHost: string
apiPath: string
apiKey: string
models: ProviderModelInfo[]
baseDefaultModels: ProviderModelInfo[]
excludedModels: string[] // chatbox ai记录被移除的模型id
// azure
endpoint: string
deploymentName: string
dalleDeploymentName: string // dall-e-3 的部署名称
apiVersion: string
useProxy: boolean // 目前只有custom provider会使用这个字段
}>
export type ProviderInfo = (ProviderBaseInfo | CustomProviderBaseInfo) & ProviderSettings
export enum ModelProviderType {
ChatboxAI = 'chatbox-ai',
OpenAI = 'openai',
Gemini = 'gemini',
Claude = 'claude',
}
export type ModelMeta = {
[key: string]: {
contextWindow: number
maxOutput?: number
functionCalling?: boolean
vision?: boolean
reasoning?: boolean
}
}
export interface CustomProvider {
id: string
name: string
api: 'openai'
host: string
path: string
key: string
model: string
modelOptions?: string[]
useProxy?: boolean
}
export interface ExtensionSettings {
webSearch: {
provider: 'build-in' | 'bing' | 'tavily' // 搜索提供方
tavilyApiKey?: string // Tavily API 密钥
}
}
export interface MCPSettings {
servers: MCPServerConfig[]
enabledBuiltinServers: string[]
}
export interface Settings extends SessionSettings {
providers?: {
[key: string]: ProviderSettings
}
customProviders?: CustomProviderBaseInfo[]
favoritedModels?: {
provider: ModelProvider | string
model: string
}[]
// default models
defaultChatModel?: {
provider: ModelProvider | string
model: string
}
threadNamingModel?: {
provider: ModelProvider | string
model: string
}
searchTermConstructionModel?: {
provider: ModelProvider | string
model: string
}
// chatboxai
licenseKey?: string
licenseInstances?: {
[key: string]: string
}
licenseDetail?: ChatboxAILicenseDetail
showWordCount?: boolean
showTokenCount?: boolean
showTokenUsed?: boolean
showModelName?: boolean
showMessageTimestamp?: boolean
showFirstTokenLatency?: boolean
theme: Theme
language: Language
languageInited?: boolean
fontSize: number
spellCheck: boolean
startupPage: 'home' | 'session' // 启动页
// disableQuickToggleShortcut?: boolean // 是否关闭快捷键切换窗口显隐(弃用,为了兼容历史数据,这个字段永远不要使用)
defaultPrompt?: string // 新会话的默认 prompt
proxy?: string // 代理地址
allowReportingAndTracking: boolean // 是否允许错误报告和事件追踪
userAvatarKey?: string // 用户头像的 key
defaultAssistantAvatarKey?: string // 默认助手头像的 key
enableMarkdownRendering: boolean
enableMermaidRendering: boolean
enableLaTeXRendering: boolean
injectDefaultMetadata: boolean // 是否注入默认附加元数据(如模型名称、当前日期)
autoPreviewArtifacts: boolean // 是否自动展开预览 artifacts
autoCollapseCodeBlock: boolean // 是否自动折叠代码块
pasteLongTextAsAFile: boolean // 是否将长文本粘贴为文件
autoGenerateTitle: boolean
autoLaunch: boolean
autoUpdate: boolean // 是否自动检查更新
betaUpdate: boolean // 是否自动检查 beta 更新
shortcuts: ShortcutSetting
extension: ExtensionSettings
mcp: MCPSettings
}
export interface ShortcutSetting {
// windowQuickToggle: string // 快速切换窗口显隐的快捷键
quickToggle: ShortcutToggleWindowValue
inputBoxFocus: string // 聚焦输入框的快捷键
inputBoxWebBrowsingMode: string // 切换输入框的 web 浏览模式的快捷键
newChat: string // 新建聊天的快捷键
newPictureChat: string // 新建图片会话的快捷键
sessionListNavNext: string // 切换到下一个会话的快捷键
sessionListNavPrev: string // 切换到上一个会话的快捷键
sessionListNavTargetIndex: string // 切换到指定会话的快捷键
messageListRefreshContext: string // 刷新上下文的快捷键
dialogOpenSearch: string // 打开搜索对话框的快捷键
// inputBoxSend: string // 发送消息的快捷键
// inputBoxInsertNewLine: string // 输入框换行的快捷键
// inputBoxSendWithoutResponse: string // 发送但不生成回复的快捷键
optionNavUp: string // 选项导航的快捷键
optionNavDown: string // 选项导航的快捷键
optionSelect: string // 选项导航的快捷键
inpubBoxSendMessage: ShortcutSendValue
inpubBoxSendMessageWithoutResponse: ShortcutSendValue
}
export const shortcutSendValues = ['', 'Enter', 'Ctrl+Enter', 'Command+Enter', 'Shift+Enter', 'Ctrl+Shift+Enter']
export type ShortcutSendValue = (typeof shortcutSendValues)[number]
export const shortcutToggleWindowValues = [
'',
'Alt+`',
'Alt+Space',
'Ctrl+Alt+Space',
// 'Command+Space', // 系统快捷键冲突
'Ctrl+Space', // 系统快捷键冲突
// 'Command+Alt+Space', 系统快捷键冲突
]
export type ShortcutToggleWindowValue = (typeof shortcutToggleWindowValues)[number]
export type ShortcutName = keyof ShortcutSetting
export type Language =
| 'en'
| 'zh-Hans'
| 'zh-Hant'
| 'ja'
| 'ko'
| 'ru'
| 'de'
| 'fr'
| 'pt-PT'
| 'es'
| 'ar'
| 'it-IT'
| 'sv'
| 'nb-NO'
export interface Config {
uuid: string
}
export interface SponsorAd {
text: string
url: string
}
export interface SponsorAboutBanner {
type: 'picture' | 'picture-text'
name: string
pictureUrl: string
link: string
title: string
description: string
}
export interface CopilotDetail {
id: string
name: string
picUrl?: string
prompt: string
demoQuestion?: string
demoAnswer?: string
starred?: boolean
usedCount: number
shared?: boolean
}
export interface Toast {
id: string
content: string
}
export enum Theme {
Dark,
Light,
System,
}
export interface RemoteConfig {
setting_chatboxai_first: boolean
product_ids: number[]
}
export interface ChatboxAILicenseDetail {
type: ChatboxAIModel // 弃用,存在于老版本中
name: string
defaultModel: ChatboxAIModel
remaining_quota_35: number
remaining_quota_4: number
remaining_quota_image: number
image_used_count: number
image_total_quota: number
token_refreshed_time: string
token_expire_time: string | null | undefined
remaining_quota_unified: number
expansion_pack_limit: number
expansion_pack_usage: number
}
export type ChatboxAIModel = 'chatboxai-3.5' | 'chatboxai-4' | string
export interface ModelOptionGroup {
group_name?: string
options: {
label: string
value: string
recommended?: boolean
}[]
// hidden?: boolean
collapsable?: boolean
}
export function copyMessage(source: Message): Message {
return {
...source,
cancel: undefined,
id: uuidv4(),
}
}
export function copyThreads(source?: SessionThread[]): SessionThread[] | undefined {
if (!source) {
return undefined
}
return source.map((thread) => ({
...thread,
messages: thread.messages.map(copyMessage),
createdAt: Date.now(),
id: uuidv4(),
}))
}