@opentiny/tiny-robot-kit 0.4.0 → 0.4.1-alpha.0

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.
@@ -0,0 +1,212 @@
1
+ /**
2
+ * 模型Provider基类
3
+ */
4
+ declare abstract class BaseModelProvider {
5
+ protected config: AIModelConfig;
6
+ /**
7
+ * @param config AI模型配置
8
+ */
9
+ constructor(config: AIModelConfig);
10
+ /**
11
+ * 发送聊天请求并获取响应
12
+ * @param request 聊天请求参数
13
+ * @returns 聊天响应
14
+ */
15
+ abstract chat(request: ChatCompletionRequest): Promise<ChatCompletionResponse>;
16
+ /**
17
+ * 发送流式聊天请求并通过处理器处理响应
18
+ * @param request 聊天请求参数
19
+ * @param handler 流式响应处理器
20
+ */
21
+ abstract chatStream(request: ChatCompletionRequest, handler: StreamHandler): Promise<void>;
22
+ /**
23
+ * 更新配置
24
+ * @param config 新的AI模型配置
25
+ */
26
+ updateConfig(config: AIModelConfig): void;
27
+ /**
28
+ * 获取当前配置
29
+ * @returns AI模型配置
30
+ */
31
+ getConfig(): AIModelConfig;
32
+ /**
33
+ * 验证请求参数
34
+ * @param request 聊天请求参数
35
+ */
36
+ protected validateRequest(request: ChatCompletionRequest): void;
37
+ }
38
+
39
+ type MaybePromise<T> = T | Promise<T>;
40
+ /**
41
+ * 消息角色类型
42
+ */
43
+ type MessageRole = 'system' | 'user' | 'assistant';
44
+ interface ToolCall {
45
+ index: number;
46
+ id: string;
47
+ type: 'function';
48
+ function: {
49
+ name: string;
50
+ arguments: string;
51
+ result?: string;
52
+ };
53
+ }
54
+ interface MessageMetadata {
55
+ createdAt?: number;
56
+ updatedAt?: number;
57
+ id?: string;
58
+ model?: string;
59
+ [key: string]: any;
60
+ }
61
+ /**
62
+ * 聊天消息接口
63
+ */
64
+ interface ChatMessage {
65
+ role: string;
66
+ content: string;
67
+ reasoning_content?: string;
68
+ metadata?: MessageMetadata;
69
+ tool_calls?: ToolCall[];
70
+ tool_call_id?: string;
71
+ [key: string]: any;
72
+ [key: symbol]: any;
73
+ }
74
+ /**
75
+ * 聊天历史记录
76
+ */
77
+ type ChatHistory = ChatMessage[];
78
+ /**
79
+ * 聊天完成请求选项
80
+ */
81
+ interface ChatCompletionOptions {
82
+ model?: string;
83
+ temperature?: number;
84
+ top_p?: number;
85
+ n?: number;
86
+ stream?: boolean;
87
+ max_tokens?: number;
88
+ signal?: AbortSignal;
89
+ }
90
+ /**
91
+ * 聊天完成请求参数
92
+ */
93
+ interface ChatCompletionRequest {
94
+ messages: ChatMessage[];
95
+ options?: ChatCompletionOptions;
96
+ }
97
+ /**
98
+ * 聊天完成响应消息
99
+ */
100
+ interface ChatCompletionResponseMessage {
101
+ role: MessageRole;
102
+ content: string;
103
+ [x: string]: unknown;
104
+ }
105
+ /**
106
+ * 聊天完成响应选择
107
+ */
108
+ interface ChatCompletionResponseChoice {
109
+ index: number;
110
+ message: ChatCompletionResponseMessage;
111
+ finish_reason: string;
112
+ }
113
+ /**
114
+ * 聊天完成响应使用情况
115
+ */
116
+ interface ChatCompletionResponseUsage {
117
+ prompt_tokens: number;
118
+ completion_tokens: number;
119
+ total_tokens: number;
120
+ }
121
+ /**
122
+ * 聊天完成响应
123
+ */
124
+ interface ChatCompletionResponse {
125
+ id: string;
126
+ object: string;
127
+ created: number;
128
+ model: string;
129
+ choices: ChatCompletionResponseChoice[];
130
+ usage: ChatCompletionResponseUsage;
131
+ }
132
+ /**
133
+ * 流式聊天完成响应增量
134
+ */
135
+ interface ChatCompletionStreamResponseDelta {
136
+ content?: string;
137
+ role?: MessageRole;
138
+ [x: string]: unknown;
139
+ }
140
+ /**
141
+ * 流式聊天完成响应选择
142
+ */
143
+ interface ChatCompletionStreamResponseChoice {
144
+ index: number;
145
+ delta: ChatCompletionStreamResponseDelta;
146
+ finish_reason: string | null;
147
+ }
148
+ /**
149
+ * 流式聊天完成响应
150
+ */
151
+ interface ChatCompletionStreamResponse {
152
+ id: string;
153
+ object: string;
154
+ created: number;
155
+ model: string;
156
+ choices: ChatCompletionStreamResponseChoice[];
157
+ }
158
+ /**
159
+ * AI模型提供商类型
160
+ */
161
+ type AIProvider = 'openai' | 'deepseek' | 'custom';
162
+ /**
163
+ * AI模型配置接口
164
+ */
165
+ interface AIModelConfig {
166
+ provider: AIProvider;
167
+ providerImplementation?: BaseModelProvider;
168
+ apiKey?: string;
169
+ apiUrl?: string;
170
+ apiVersion?: string;
171
+ defaultModel?: string;
172
+ defaultOptions?: ChatCompletionOptions;
173
+ }
174
+ /**
175
+ * 错误类型
176
+ */
177
+ declare enum ErrorType {
178
+ NETWORK_ERROR = "network_error",
179
+ AUTHENTICATION_ERROR = "authentication_error",
180
+ RATE_LIMIT_ERROR = "rate_limit_error",
181
+ SERVER_ERROR = "server_error",
182
+ MODEL_ERROR = "model_error",
183
+ TIMEOUT_ERROR = "timeout_error",
184
+ UNKNOWN_ERROR = "unknown_error"
185
+ }
186
+ /**
187
+ * AI适配器错误
188
+ */
189
+ interface AIAdapterError {
190
+ type: ErrorType;
191
+ message: string;
192
+ statusCode?: number;
193
+ originalError?: object;
194
+ }
195
+ /**
196
+ * 流式响应事件类型
197
+ */
198
+ declare enum StreamEventType {
199
+ DATA = "data",
200
+ ERROR = "error",
201
+ DONE = "done"
202
+ }
203
+ /**
204
+ * 流式响应处理器
205
+ */
206
+ interface StreamHandler {
207
+ onData: (data: ChatCompletionStreamResponse) => void;
208
+ onError: (error: AIAdapterError) => void;
209
+ onDone: (finishReason?: string) => void;
210
+ }
211
+
212
+ export { type AIAdapterError as A, BaseModelProvider as B, type ChatCompletionOptions as C, ErrorType as E, type MaybePromise as M, StreamEventType as S, type ToolCall as T, type AIModelConfig as a, type AIProvider as b, type ChatCompletionRequest as c, type ChatCompletionResponse as d, type ChatCompletionResponseChoice as e, type ChatCompletionResponseMessage as f, type ChatCompletionResponseUsage as g, type ChatCompletionStreamResponse as h, type ChatCompletionStreamResponseChoice as i, type ChatCompletionStreamResponseDelta as j, type ChatHistory as k, type ChatMessage as l, type MessageMetadata as m, type MessageRole as n, type StreamHandler as o };
@@ -0,0 +1,212 @@
1
+ /**
2
+ * 模型Provider基类
3
+ */
4
+ declare abstract class BaseModelProvider {
5
+ protected config: AIModelConfig;
6
+ /**
7
+ * @param config AI模型配置
8
+ */
9
+ constructor(config: AIModelConfig);
10
+ /**
11
+ * 发送聊天请求并获取响应
12
+ * @param request 聊天请求参数
13
+ * @returns 聊天响应
14
+ */
15
+ abstract chat(request: ChatCompletionRequest): Promise<ChatCompletionResponse>;
16
+ /**
17
+ * 发送流式聊天请求并通过处理器处理响应
18
+ * @param request 聊天请求参数
19
+ * @param handler 流式响应处理器
20
+ */
21
+ abstract chatStream(request: ChatCompletionRequest, handler: StreamHandler): Promise<void>;
22
+ /**
23
+ * 更新配置
24
+ * @param config 新的AI模型配置
25
+ */
26
+ updateConfig(config: AIModelConfig): void;
27
+ /**
28
+ * 获取当前配置
29
+ * @returns AI模型配置
30
+ */
31
+ getConfig(): AIModelConfig;
32
+ /**
33
+ * 验证请求参数
34
+ * @param request 聊天请求参数
35
+ */
36
+ protected validateRequest(request: ChatCompletionRequest): void;
37
+ }
38
+
39
+ type MaybePromise<T> = T | Promise<T>;
40
+ /**
41
+ * 消息角色类型
42
+ */
43
+ type MessageRole = 'system' | 'user' | 'assistant';
44
+ interface ToolCall {
45
+ index: number;
46
+ id: string;
47
+ type: 'function';
48
+ function: {
49
+ name: string;
50
+ arguments: string;
51
+ result?: string;
52
+ };
53
+ }
54
+ interface MessageMetadata {
55
+ createdAt?: number;
56
+ updatedAt?: number;
57
+ id?: string;
58
+ model?: string;
59
+ [key: string]: any;
60
+ }
61
+ /**
62
+ * 聊天消息接口
63
+ */
64
+ interface ChatMessage {
65
+ role: string;
66
+ content: string;
67
+ reasoning_content?: string;
68
+ metadata?: MessageMetadata;
69
+ tool_calls?: ToolCall[];
70
+ tool_call_id?: string;
71
+ [key: string]: any;
72
+ [key: symbol]: any;
73
+ }
74
+ /**
75
+ * 聊天历史记录
76
+ */
77
+ type ChatHistory = ChatMessage[];
78
+ /**
79
+ * 聊天完成请求选项
80
+ */
81
+ interface ChatCompletionOptions {
82
+ model?: string;
83
+ temperature?: number;
84
+ top_p?: number;
85
+ n?: number;
86
+ stream?: boolean;
87
+ max_tokens?: number;
88
+ signal?: AbortSignal;
89
+ }
90
+ /**
91
+ * 聊天完成请求参数
92
+ */
93
+ interface ChatCompletionRequest {
94
+ messages: ChatMessage[];
95
+ options?: ChatCompletionOptions;
96
+ }
97
+ /**
98
+ * 聊天完成响应消息
99
+ */
100
+ interface ChatCompletionResponseMessage {
101
+ role: MessageRole;
102
+ content: string;
103
+ [x: string]: unknown;
104
+ }
105
+ /**
106
+ * 聊天完成响应选择
107
+ */
108
+ interface ChatCompletionResponseChoice {
109
+ index: number;
110
+ message: ChatCompletionResponseMessage;
111
+ finish_reason: string;
112
+ }
113
+ /**
114
+ * 聊天完成响应使用情况
115
+ */
116
+ interface ChatCompletionResponseUsage {
117
+ prompt_tokens: number;
118
+ completion_tokens: number;
119
+ total_tokens: number;
120
+ }
121
+ /**
122
+ * 聊天完成响应
123
+ */
124
+ interface ChatCompletionResponse {
125
+ id: string;
126
+ object: string;
127
+ created: number;
128
+ model: string;
129
+ choices: ChatCompletionResponseChoice[];
130
+ usage: ChatCompletionResponseUsage;
131
+ }
132
+ /**
133
+ * 流式聊天完成响应增量
134
+ */
135
+ interface ChatCompletionStreamResponseDelta {
136
+ content?: string;
137
+ role?: MessageRole;
138
+ [x: string]: unknown;
139
+ }
140
+ /**
141
+ * 流式聊天完成响应选择
142
+ */
143
+ interface ChatCompletionStreamResponseChoice {
144
+ index: number;
145
+ delta: ChatCompletionStreamResponseDelta;
146
+ finish_reason: string | null;
147
+ }
148
+ /**
149
+ * 流式聊天完成响应
150
+ */
151
+ interface ChatCompletionStreamResponse {
152
+ id: string;
153
+ object: string;
154
+ created: number;
155
+ model: string;
156
+ choices: ChatCompletionStreamResponseChoice[];
157
+ }
158
+ /**
159
+ * AI模型提供商类型
160
+ */
161
+ type AIProvider = 'openai' | 'deepseek' | 'custom';
162
+ /**
163
+ * AI模型配置接口
164
+ */
165
+ interface AIModelConfig {
166
+ provider: AIProvider;
167
+ providerImplementation?: BaseModelProvider;
168
+ apiKey?: string;
169
+ apiUrl?: string;
170
+ apiVersion?: string;
171
+ defaultModel?: string;
172
+ defaultOptions?: ChatCompletionOptions;
173
+ }
174
+ /**
175
+ * 错误类型
176
+ */
177
+ declare enum ErrorType {
178
+ NETWORK_ERROR = "network_error",
179
+ AUTHENTICATION_ERROR = "authentication_error",
180
+ RATE_LIMIT_ERROR = "rate_limit_error",
181
+ SERVER_ERROR = "server_error",
182
+ MODEL_ERROR = "model_error",
183
+ TIMEOUT_ERROR = "timeout_error",
184
+ UNKNOWN_ERROR = "unknown_error"
185
+ }
186
+ /**
187
+ * AI适配器错误
188
+ */
189
+ interface AIAdapterError {
190
+ type: ErrorType;
191
+ message: string;
192
+ statusCode?: number;
193
+ originalError?: object;
194
+ }
195
+ /**
196
+ * 流式响应事件类型
197
+ */
198
+ declare enum StreamEventType {
199
+ DATA = "data",
200
+ ERROR = "error",
201
+ DONE = "done"
202
+ }
203
+ /**
204
+ * 流式响应处理器
205
+ */
206
+ interface StreamHandler {
207
+ onData: (data: ChatCompletionStreamResponse) => void;
208
+ onError: (error: AIAdapterError) => void;
209
+ onDone: (finishReason?: string) => void;
210
+ }
211
+
212
+ export { type AIAdapterError as A, BaseModelProvider as B, type ChatCompletionOptions as C, ErrorType as E, type MaybePromise as M, StreamEventType as S, type ToolCall as T, type AIModelConfig as a, type AIProvider as b, type ChatCompletionRequest as c, type ChatCompletionResponse as d, type ChatCompletionResponseChoice as e, type ChatCompletionResponseMessage as f, type ChatCompletionResponseUsage as g, type ChatCompletionStreamResponse as h, type ChatCompletionStreamResponseChoice as i, type ChatCompletionStreamResponseDelta as j, type ChatHistory as k, type ChatMessage as l, type MessageMetadata as m, type MessageRole as n, type StreamHandler as o };
package/package.json CHANGED
@@ -1,27 +1,69 @@
1
1
  {
2
2
  "name": "@opentiny/tiny-robot-kit",
3
- "version": "0.4.0",
3
+ "version": "0.4.1-alpha.0",
4
+ "homepage": "https://docs.opentiny.design/tiny-robot/",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/opentiny/tiny-robot.git"
8
+ },
9
+ "bugs": {
10
+ "url": "https://github.com/opentiny/tiny-robot/issues"
11
+ },
4
12
  "publishConfig": {
5
13
  "access": "public"
6
14
  },
7
15
  "description": "AI大模型请求与数据处理工具包",
16
+ "keywords": [
17
+ "vue",
18
+ "vue3",
19
+ "ai",
20
+ "ai-client",
21
+ "ai-sdk",
22
+ "chat",
23
+ "chatbot",
24
+ "llm",
25
+ "openai",
26
+ "assistant",
27
+ "streaming",
28
+ "composables",
29
+ "conversation",
30
+ "model-provider",
31
+ "tiny-robot",
32
+ "opentiny"
33
+ ],
8
34
  "main": "dist/index.js",
9
35
  "module": "dist/index.mjs",
10
36
  "types": "dist/index.d.ts",
37
+ "exports": {
38
+ ".": {
39
+ "types": "./dist/index.d.ts",
40
+ "import": "./dist/index.mjs",
41
+ "require": "./dist/index.js"
42
+ },
43
+ "./core": {
44
+ "types": "./dist/core.d.ts",
45
+ "import": "./dist/core.mjs",
46
+ "require": "./dist/core.js"
47
+ }
48
+ },
11
49
  "files": [
12
50
  "dist"
13
51
  ],
14
52
  "sideEffects": false,
15
53
  "scripts": {
16
- "build": "tsup src/index.ts --format cjs,esm --dts --minify",
17
- "dev": "tsup src/index.ts --format cjs,esm --dts --watch"
54
+ "build": "tsup src/index.ts src/core.ts --format cjs,esm --dts --minify",
55
+ "dev": "tsup src/index.ts src/core.ts --format cjs,esm --dts --watch",
56
+ "test": "vitest run",
57
+ "test:watch": "vitest"
18
58
  },
19
59
  "author": "",
20
60
  "license": "MIT",
21
61
  "devDependencies": {
22
62
  "@types/node": "^22.13.17",
63
+ "openai": "^6.34.0",
23
64
  "tsup": "^8.0.1",
24
- "typescript": "^5.8.2"
65
+ "typescript": "^5.8.2",
66
+ "vitest": "^4.1.4"
25
67
  },
26
68
  "peerDependencies": {
27
69
  "vue": ">=3.0.0"
@@ -29,5 +71,5 @@
29
71
  "dependencies": {
30
72
  "idb": "^8.0.3"
31
73
  },
32
- "gitHead": "558095a424930d99e079e6e1cf4fe8929d48c7b2"
74
+ "gitHead": "22cb4503b47f2e1d2134d4a90d02b5643672ef6c"
33
75
  }