@opentiny/tiny-robot-kit 0.2.0-alpha.1 → 0.2.0-alpha.3
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.
- package/dist/index.js +2 -761
- package/dist/index.mjs +2 -722
- package/package.json +7 -3
- package/src/client.ts +0 -101
- package/src/error.ts +0 -100
- package/src/index.ts +0 -10
- package/src/providers/base.ts +0 -62
- package/src/providers/openai.ts +0 -139
- package/src/types.ts +0 -163
- package/src/utils.ts +0 -125
- package/src/vue/conversation/useConversation.ts +0 -365
- package/src/vue/index.ts +0 -2
- package/src/vue/message/useMessage.ts +0 -227
- package/tsconfig.json +0 -21
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opentiny/tiny-robot-kit",
|
|
3
|
-
"version": "0.2.0-alpha.
|
|
3
|
+
"version": "0.2.0-alpha.3",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -8,8 +8,12 @@
|
|
|
8
8
|
"main": "dist/index.js",
|
|
9
9
|
"module": "dist/index.mjs",
|
|
10
10
|
"types": "dist/index.d.ts",
|
|
11
|
+
"files": [
|
|
12
|
+
"dist"
|
|
13
|
+
],
|
|
14
|
+
"sideEffects": false,
|
|
11
15
|
"scripts": {
|
|
12
|
-
"build": "tsup src/index.ts --format cjs,esm --dts",
|
|
16
|
+
"build": "tsup src/index.ts --format cjs,esm --dts --minify",
|
|
13
17
|
"dev": "tsup src/index.ts --format cjs,esm --dts --watch"
|
|
14
18
|
},
|
|
15
19
|
"author": "",
|
|
@@ -22,5 +26,5 @@
|
|
|
22
26
|
"peerDependencies": {
|
|
23
27
|
"vue": ">=3.0.0"
|
|
24
28
|
},
|
|
25
|
-
"gitHead": "
|
|
29
|
+
"gitHead": "078c2aef51bc9cc9167bb96fcc70700999bfd6b9"
|
|
26
30
|
}
|
package/src/client.ts
DELETED
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* AI客户端类
|
|
3
|
-
* 负责根据配置选择合适的提供商并处理请求
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import type { AIModelConfig, ChatCompletionRequest, ChatCompletionResponse, StreamHandler } from './types'
|
|
7
|
-
import type { BaseModelProvider } from './providers/base'
|
|
8
|
-
import { OpenAIProvider } from './providers/openai'
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* AI客户端类
|
|
12
|
-
*/
|
|
13
|
-
export class AIClient {
|
|
14
|
-
private provider: BaseModelProvider
|
|
15
|
-
private config: AIModelConfig
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* 构造函数
|
|
19
|
-
* @param config AI模型配置
|
|
20
|
-
*/
|
|
21
|
-
constructor(config: AIModelConfig) {
|
|
22
|
-
this.config = config
|
|
23
|
-
this.provider = this.createProvider(config)
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* 创建提供商实例
|
|
28
|
-
* @param config AI模型配置
|
|
29
|
-
* @returns 提供商实例
|
|
30
|
-
*/
|
|
31
|
-
private createProvider(config: AIModelConfig): BaseModelProvider {
|
|
32
|
-
// 如果提供了自定义提供商实现,直接使用
|
|
33
|
-
if (config.provider === 'custom' && 'providerImplementation' in config) {
|
|
34
|
-
return (config as { providerImplementation: BaseModelProvider }).providerImplementation
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// 根据提供商类型创建对应的提供商实例
|
|
38
|
-
switch (config.provider) {
|
|
39
|
-
case 'deepseek':
|
|
40
|
-
const defaultConfig = {
|
|
41
|
-
defaultModel: 'deepseek-chat',
|
|
42
|
-
apiUrl: 'https://api.deepseek.com/v1',
|
|
43
|
-
}
|
|
44
|
-
return new OpenAIProvider({ ...defaultConfig, ...config })
|
|
45
|
-
case 'openai':
|
|
46
|
-
default:
|
|
47
|
-
return new OpenAIProvider(config)
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* 发送聊天请求并获取响应
|
|
53
|
-
* @param request 聊天请求参数
|
|
54
|
-
* @returns 聊天响应
|
|
55
|
-
*/
|
|
56
|
-
async chat(request: ChatCompletionRequest): Promise<ChatCompletionResponse> {
|
|
57
|
-
return this.provider.chat(request)
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* 发送流式聊天请求并通过处理器处理响应
|
|
62
|
-
* @param request 聊天请求参数
|
|
63
|
-
* @param handler 流式响应处理器
|
|
64
|
-
*/
|
|
65
|
-
async chatStream(request: ChatCompletionRequest, handler: StreamHandler): Promise<void> {
|
|
66
|
-
// 确保请求中启用了流式响应
|
|
67
|
-
const streamRequest = {
|
|
68
|
-
...request,
|
|
69
|
-
options: {
|
|
70
|
-
...request.options,
|
|
71
|
-
stream: true,
|
|
72
|
-
},
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
return this.provider.chatStream(streamRequest, handler)
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* 获取当前配置
|
|
80
|
-
* @returns AI模型配置
|
|
81
|
-
*/
|
|
82
|
-
getConfig(): AIModelConfig {
|
|
83
|
-
return { ...this.config }
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* 更新配置
|
|
88
|
-
* @param config 新的AI模型配置
|
|
89
|
-
*/
|
|
90
|
-
updateConfig(config: Partial<AIModelConfig>): void {
|
|
91
|
-
this.config = { ...this.config, ...config }
|
|
92
|
-
|
|
93
|
-
// 如果提供商类型发生变化,重新创建提供商实例
|
|
94
|
-
if (config.provider && config.provider !== this.config.provider) {
|
|
95
|
-
this.provider = this.createProvider(this.config)
|
|
96
|
-
} else {
|
|
97
|
-
// 否则只更新提供商配置
|
|
98
|
-
this.provider.updateConfig(this.config)
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
}
|
package/src/error.ts
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 错误处理模块
|
|
3
|
-
* 用于统一处理各种错误情况并提供标准化的错误格式
|
|
4
|
-
*/
|
|
5
|
-
import { AIAdapterError, ErrorType } from './types'
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* 创建标准化的AI适配器错误
|
|
9
|
-
* @param error 错误信息
|
|
10
|
-
* @returns 标准化的AI适配器错误
|
|
11
|
-
*/
|
|
12
|
-
export function createError(error: Partial<AIAdapterError>): AIAdapterError {
|
|
13
|
-
return {
|
|
14
|
-
type: error.type || ErrorType.UNKNOWN_ERROR,
|
|
15
|
-
message: error.message || '未知错误',
|
|
16
|
-
statusCode: error.statusCode,
|
|
17
|
-
originalError: error.originalError,
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
interface Error {
|
|
22
|
-
response?: object
|
|
23
|
-
code?: string
|
|
24
|
-
message?: string
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* 处理API请求错误
|
|
29
|
-
* @param error 原始错误
|
|
30
|
-
* @returns 标准化的AI适配器错误
|
|
31
|
-
*/
|
|
32
|
-
export function handleRequestError(error: Error): AIAdapterError {
|
|
33
|
-
// 网络错误
|
|
34
|
-
if (!error.response) {
|
|
35
|
-
return createError({
|
|
36
|
-
type: ErrorType.NETWORK_ERROR,
|
|
37
|
-
message: '网络连接错误,请检查您的网络连接',
|
|
38
|
-
originalError: error,
|
|
39
|
-
})
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// 服务器返回的错误
|
|
43
|
-
if (error.response) {
|
|
44
|
-
const { status, data } = error.response
|
|
45
|
-
|
|
46
|
-
// 身份验证错误
|
|
47
|
-
if (status === 401 || status === 403) {
|
|
48
|
-
return createError({
|
|
49
|
-
type: ErrorType.AUTHENTICATION_ERROR,
|
|
50
|
-
message: '身份验证失败,请检查您的API密钥',
|
|
51
|
-
statusCode: status,
|
|
52
|
-
originalError: error,
|
|
53
|
-
})
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
// 速率限制错误
|
|
57
|
-
if (status === 429) {
|
|
58
|
-
return createError({
|
|
59
|
-
type: ErrorType.RATE_LIMIT_ERROR,
|
|
60
|
-
message: '超出API调用限制,请稍后再试',
|
|
61
|
-
statusCode: status,
|
|
62
|
-
originalError: error,
|
|
63
|
-
})
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
// 服务器错误
|
|
67
|
-
if (status >= 500) {
|
|
68
|
-
return createError({
|
|
69
|
-
type: ErrorType.SERVER_ERROR,
|
|
70
|
-
message: '服务器错误,请稍后再试',
|
|
71
|
-
statusCode: status,
|
|
72
|
-
originalError: error,
|
|
73
|
-
})
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// 其他HTTP错误
|
|
77
|
-
return createError({
|
|
78
|
-
type: ErrorType.UNKNOWN_ERROR,
|
|
79
|
-
message: data?.error?.message || `请求失败,状态码: ${status}`,
|
|
80
|
-
statusCode: status,
|
|
81
|
-
originalError: error,
|
|
82
|
-
})
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// 超时错误
|
|
86
|
-
if (error.code === 'ECONNABORTED') {
|
|
87
|
-
return createError({
|
|
88
|
-
type: ErrorType.TIMEOUT_ERROR,
|
|
89
|
-
message: '请求超时,请稍后再试',
|
|
90
|
-
originalError: error,
|
|
91
|
-
})
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
// 默认错误
|
|
95
|
-
return createError({
|
|
96
|
-
type: ErrorType.UNKNOWN_ERROR,
|
|
97
|
-
message: error.message || '发生未知错误',
|
|
98
|
-
originalError: error,
|
|
99
|
-
})
|
|
100
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
export { AIClient } from './client'
|
|
2
|
-
|
|
3
|
-
export { BaseModelProvider } from './providers/base'
|
|
4
|
-
export { OpenAIProvider } from './providers/openai'
|
|
5
|
-
|
|
6
|
-
export { formatMessages, extractTextFromResponse } from './utils'
|
|
7
|
-
|
|
8
|
-
export * from './vue'
|
|
9
|
-
|
|
10
|
-
export * from './types'
|
package/src/providers/base.ts
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import { AIModelConfig, ChatCompletionRequest, ChatCompletionResponse, StreamHandler } from '../types'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* 模型Provider基类
|
|
5
|
-
*/
|
|
6
|
-
export abstract class BaseModelProvider {
|
|
7
|
-
protected config: AIModelConfig
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* @param config AI模型配置
|
|
11
|
-
*/
|
|
12
|
-
constructor(config: AIModelConfig) {
|
|
13
|
-
this.config = config
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* 发送聊天请求并获取响应
|
|
18
|
-
* @param request 聊天请求参数
|
|
19
|
-
* @returns 聊天响应
|
|
20
|
-
*/
|
|
21
|
-
abstract chat(request: ChatCompletionRequest): Promise<ChatCompletionResponse>
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* 发送流式聊天请求并通过处理器处理响应
|
|
25
|
-
* @param request 聊天请求参数
|
|
26
|
-
* @param handler 流式响应处理器
|
|
27
|
-
*/
|
|
28
|
-
abstract chatStream(request: ChatCompletionRequest, handler: StreamHandler): Promise<void>
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* 更新配置
|
|
32
|
-
* @param config 新的AI模型配置
|
|
33
|
-
*/
|
|
34
|
-
updateConfig(config: AIModelConfig): void {
|
|
35
|
-
this.config = { ...this.config, ...config }
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* 获取当前配置
|
|
40
|
-
* @returns AI模型配置
|
|
41
|
-
*/
|
|
42
|
-
getConfig(): AIModelConfig {
|
|
43
|
-
return { ...this.config }
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* 验证请求参数
|
|
48
|
-
* @param request 聊天请求参数
|
|
49
|
-
*/
|
|
50
|
-
protected validateRequest(request: ChatCompletionRequest): void {
|
|
51
|
-
if (!request.messages || !Array.isArray(request.messages) || request.messages.length === 0) {
|
|
52
|
-
throw new Error('请求必须包含至少一条消息')
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// 验证每条消息的格式
|
|
56
|
-
for (const message of request.messages) {
|
|
57
|
-
if (!message.role || !message.content) {
|
|
58
|
-
throw new Error('每条消息必须包含角色和内容')
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
}
|
package/src/providers/openai.ts
DELETED
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* OpenAI提供商
|
|
3
|
-
* 用于与OpenAI API进行交互
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { BaseModelProvider } from './base'
|
|
7
|
-
import type { AIModelConfig, ChatCompletionRequest, ChatCompletionResponse, StreamHandler } from '../types'
|
|
8
|
-
import { handleRequestError } from '../error'
|
|
9
|
-
import { handleSSEStream } from '../utils'
|
|
10
|
-
|
|
11
|
-
export class OpenAIProvider extends BaseModelProvider {
|
|
12
|
-
private baseURL: string
|
|
13
|
-
private apiKey: string
|
|
14
|
-
private defaultModel: string = 'gpt-3.5-turbo'
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* @param config AI模型配置
|
|
18
|
-
*/
|
|
19
|
-
constructor(config: AIModelConfig) {
|
|
20
|
-
super(config)
|
|
21
|
-
|
|
22
|
-
this.baseURL = config.apiUrl || 'https://api.openai.com/v1'
|
|
23
|
-
this.apiKey = config.apiKey || ''
|
|
24
|
-
|
|
25
|
-
// 设置默认模型
|
|
26
|
-
if (config.defaultModel) {
|
|
27
|
-
this.defaultModel = config.defaultModel
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
if (!this.apiKey) {
|
|
31
|
-
console.warn('API key is not provided. Authentication will likely fail.')
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* 发送聊天请求并获取响应
|
|
37
|
-
* @param request 聊天请求参数
|
|
38
|
-
* @returns 聊天响应
|
|
39
|
-
*/
|
|
40
|
-
async chat(request: ChatCompletionRequest): Promise<ChatCompletionResponse> {
|
|
41
|
-
try {
|
|
42
|
-
this.validateRequest(request)
|
|
43
|
-
|
|
44
|
-
const requestData = {
|
|
45
|
-
model: request.options?.model || this.config.defaultModel || this.defaultModel,
|
|
46
|
-
messages: request.messages,
|
|
47
|
-
...request.options,
|
|
48
|
-
stream: false,
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
const options = {
|
|
52
|
-
method: 'POST',
|
|
53
|
-
headers: { 'Content-Type': 'application/json' },
|
|
54
|
-
body: JSON.stringify(requestData),
|
|
55
|
-
}
|
|
56
|
-
if (this.apiKey) {
|
|
57
|
-
Object.assign(options.headers, { Authorization: `Bearer ${this.apiKey}` })
|
|
58
|
-
}
|
|
59
|
-
const response = await fetch(`${this.baseURL}/chat/completions`, options)
|
|
60
|
-
|
|
61
|
-
if (!response.ok) {
|
|
62
|
-
const errorText = await response.text()
|
|
63
|
-
throw new Error(`HTTP error! status: ${response.status}, details: ${errorText}`)
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
return await response.json()
|
|
67
|
-
} catch (error: unknown) {
|
|
68
|
-
// 处理错误
|
|
69
|
-
throw handleRequestError(error as { response: object })
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* 发送流式聊天请求并通过处理器处理响应
|
|
75
|
-
* @param request 聊天请求参数
|
|
76
|
-
* @param handler 流式响应处理器
|
|
77
|
-
*/
|
|
78
|
-
async chatStream(request: ChatCompletionRequest, handler: StreamHandler): Promise<void> {
|
|
79
|
-
const { signal, ...options } = request.options || {}
|
|
80
|
-
|
|
81
|
-
try {
|
|
82
|
-
// 验证请求参数
|
|
83
|
-
this.validateRequest(request)
|
|
84
|
-
|
|
85
|
-
const requestData = {
|
|
86
|
-
model: request.options?.model || this.config.defaultModel || this.defaultModel,
|
|
87
|
-
messages: request.messages,
|
|
88
|
-
...options,
|
|
89
|
-
stream: true,
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
const requestOptions = {
|
|
93
|
-
method: 'POST',
|
|
94
|
-
headers: {
|
|
95
|
-
'Content-Type': 'application/json',
|
|
96
|
-
Authorization: `Bearer ${this.apiKey}`,
|
|
97
|
-
Accept: 'text/event-stream',
|
|
98
|
-
},
|
|
99
|
-
body: JSON.stringify(requestData),
|
|
100
|
-
signal,
|
|
101
|
-
}
|
|
102
|
-
if (this.apiKey) {
|
|
103
|
-
Object.assign(requestOptions.headers, { Authorization: `Bearer ${this.apiKey}` })
|
|
104
|
-
}
|
|
105
|
-
const response = await fetch(`${this.baseURL}/chat/completions`, requestOptions)
|
|
106
|
-
|
|
107
|
-
if (!response.ok) {
|
|
108
|
-
const errorText = await response.text()
|
|
109
|
-
throw new Error(`HTTP error! status: ${response.status}, details: ${errorText}`)
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
await handleSSEStream(response, handler, signal)
|
|
113
|
-
} catch (error: unknown) {
|
|
114
|
-
if (signal?.aborted) return
|
|
115
|
-
handler.onError(handleRequestError(error as { response: object }))
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* 更新配置
|
|
121
|
-
* @param config 新的AI模型配置
|
|
122
|
-
*/
|
|
123
|
-
updateConfig(config: AIModelConfig): void {
|
|
124
|
-
super.updateConfig(config)
|
|
125
|
-
|
|
126
|
-
// 更新配置
|
|
127
|
-
if (config.apiUrl) {
|
|
128
|
-
this.baseURL = config.apiUrl
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
if (config.apiKey) {
|
|
132
|
-
this.apiKey = config.apiKey
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
if (config.defaultModel) {
|
|
136
|
-
this.defaultModel = config.defaultModel
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
}
|
package/src/types.ts
DELETED
|
@@ -1,163 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 消息角色类型
|
|
3
|
-
*/
|
|
4
|
-
export type MessageRole = 'system' | 'user' | 'assistant'
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* 聊天消息接口
|
|
8
|
-
*/
|
|
9
|
-
export interface ChatMessage {
|
|
10
|
-
role: MessageRole
|
|
11
|
-
content: string
|
|
12
|
-
name?: string
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* 聊天历史记录
|
|
17
|
-
*/
|
|
18
|
-
export type ChatHistory = ChatMessage[]
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* 聊天完成请求选项
|
|
22
|
-
*/
|
|
23
|
-
export interface ChatCompletionOptions {
|
|
24
|
-
model?: string
|
|
25
|
-
temperature?: number
|
|
26
|
-
top_p?: number
|
|
27
|
-
n?: number
|
|
28
|
-
stream?: boolean
|
|
29
|
-
max_tokens?: number
|
|
30
|
-
signal?: AbortSignal
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* 聊天完成请求参数
|
|
35
|
-
*/
|
|
36
|
-
export interface ChatCompletionRequest {
|
|
37
|
-
messages: ChatMessage[]
|
|
38
|
-
options?: ChatCompletionOptions
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* 聊天完成响应消息
|
|
43
|
-
*/
|
|
44
|
-
export interface ChatCompletionResponseMessage {
|
|
45
|
-
role: MessageRole
|
|
46
|
-
content: string
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* 聊天完成响应选择
|
|
51
|
-
*/
|
|
52
|
-
export interface ChatCompletionResponseChoice {
|
|
53
|
-
index: number
|
|
54
|
-
message: ChatCompletionResponseMessage
|
|
55
|
-
finish_reason: string
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* 聊天完成响应使用情况
|
|
60
|
-
*/
|
|
61
|
-
export interface ChatCompletionResponseUsage {
|
|
62
|
-
prompt_tokens: number
|
|
63
|
-
completion_tokens: number
|
|
64
|
-
total_tokens: number
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* 聊天完成响应
|
|
69
|
-
*/
|
|
70
|
-
export interface ChatCompletionResponse {
|
|
71
|
-
id: string
|
|
72
|
-
object: string
|
|
73
|
-
created: number
|
|
74
|
-
model: string
|
|
75
|
-
choices: ChatCompletionResponseChoice[]
|
|
76
|
-
usage: ChatCompletionResponseUsage
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* 流式聊天完成响应增量
|
|
81
|
-
*/
|
|
82
|
-
export interface ChatCompletionStreamResponseDelta {
|
|
83
|
-
content?: string
|
|
84
|
-
role?: MessageRole
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* 流式聊天完成响应选择
|
|
89
|
-
*/
|
|
90
|
-
export interface ChatCompletionStreamResponseChoice {
|
|
91
|
-
index: number
|
|
92
|
-
delta: ChatCompletionStreamResponseDelta
|
|
93
|
-
finish_reason: string | null
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* 流式聊天完成响应
|
|
98
|
-
*/
|
|
99
|
-
export interface ChatCompletionStreamResponse {
|
|
100
|
-
id: string
|
|
101
|
-
object: string
|
|
102
|
-
created: number
|
|
103
|
-
model: string
|
|
104
|
-
choices: ChatCompletionStreamResponseChoice[]
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* AI模型提供商类型
|
|
109
|
-
*/
|
|
110
|
-
export type AIProvider = 'openai' | 'deepseek' | 'custom'
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* AI模型配置接口
|
|
114
|
-
*/
|
|
115
|
-
export interface AIModelConfig {
|
|
116
|
-
provider: AIProvider
|
|
117
|
-
apiKey?: string
|
|
118
|
-
apiUrl?: string
|
|
119
|
-
apiVersion?: string
|
|
120
|
-
defaultModel?: string
|
|
121
|
-
defaultOptions?: ChatCompletionOptions
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* 错误类型
|
|
126
|
-
*/
|
|
127
|
-
export enum ErrorType {
|
|
128
|
-
NETWORK_ERROR = 'network_error',
|
|
129
|
-
AUTHENTICATION_ERROR = 'authentication_error',
|
|
130
|
-
RATE_LIMIT_ERROR = 'rate_limit_error',
|
|
131
|
-
SERVER_ERROR = 'server_error',
|
|
132
|
-
MODEL_ERROR = 'model_error',
|
|
133
|
-
TIMEOUT_ERROR = 'timeout_error',
|
|
134
|
-
UNKNOWN_ERROR = 'unknown_error',
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* AI适配器错误
|
|
139
|
-
*/
|
|
140
|
-
export interface AIAdapterError {
|
|
141
|
-
type: ErrorType
|
|
142
|
-
message: string
|
|
143
|
-
statusCode?: number
|
|
144
|
-
originalError?: object
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* 流式响应事件类型
|
|
149
|
-
*/
|
|
150
|
-
export enum StreamEventType {
|
|
151
|
-
DATA = 'data',
|
|
152
|
-
ERROR = 'error',
|
|
153
|
-
DONE = 'done',
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* 流式响应处理器
|
|
158
|
-
*/
|
|
159
|
-
export interface StreamHandler {
|
|
160
|
-
onData: (data: ChatCompletionStreamResponse) => void
|
|
161
|
-
onError: (error: AIAdapterError) => void
|
|
162
|
-
onDone: () => void
|
|
163
|
-
}
|