@h-ai/ai 0.1.0-alpha9 → 1.0.0-beta.2

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.
@@ -1,628 +0,0 @@
1
- import { z } from 'zod';
2
-
3
- /**
4
- * @h-ai/ai/api — AI API 端点契约定义
5
- *
6
- * 所有 ai 模块的 API 端点(path + method + schema),
7
- * 客户端和服务端都从此处引用,编译时保证一致性。
8
- *
9
- * 注意:`chatStream` 使用 SSE 流式协议,客户端应使用
10
- * `createAIClient().chatStream()` 而非 `api.call()`。
11
- * 此处定义仅用于服务端路由注册和文档生成。
12
- *
13
- * @module ai-api-contract
14
- */
15
-
16
- interface EndpointDef<TInput = unknown, TOutput = unknown> {
17
- method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
18
- path: string;
19
- input: z.ZodType<TInput>;
20
- output: z.ZodType<TOutput>;
21
- requireAuth?: boolean;
22
- meta?: {
23
- summary?: string;
24
- tags?: string[];
25
- streaming?: boolean;
26
- };
27
- }
28
- /**
29
- * ai 所有 API 端点
30
- *
31
- * @example
32
- * ```ts
33
- * // 客户端(非流式)
34
- * import { aiEndpoints } from '@h-ai/ai/api'
35
- * const result = await api.call(aiEndpoints.chat, { messages: [...] })
36
- *
37
- * // 客户端(流式)— 使用专用 AIClient
38
- * import { api } from '@h-ai/api-client'
39
- * import { createAIClient } from '@h-ai/ai/client'
40
- * await api.init({ baseUrl: '/api' })
41
- * const client = createAIClient({ api })
42
- * for await (const chunk of client.chatStream({ messages })) { ... }
43
- *
44
- * // 服务端
45
- * export const POST = kit.fromContract(aiEndpoints.chat, async (input) => {
46
- * const result = await ai.llm.chat(input)
47
- * return result.success ? result.data : kit.response.internalError(result.error.message)
48
- * })
49
- * ```
50
- */
51
- declare const aiEndpoints: {
52
- /** 聊天完成(非流式) */
53
- readonly chat: EndpointDef<{
54
- messages: {
55
- role: "system" | "user" | "assistant" | "tool";
56
- content?: string | ({
57
- type: "text";
58
- text: string;
59
- } | {
60
- type: "image_url";
61
- image_url: {
62
- url: string;
63
- detail?: "auto" | "low" | "high" | undefined;
64
- };
65
- })[] | undefined;
66
- name?: string | undefined;
67
- tool_calls?: {
68
- id: string;
69
- type: "function";
70
- function: {
71
- name: string;
72
- arguments: string;
73
- };
74
- }[] | undefined;
75
- tool_call_id?: string | undefined;
76
- }[];
77
- model?: string | undefined;
78
- temperature?: number | undefined;
79
- top_p?: number | undefined;
80
- max_tokens?: number | undefined;
81
- stream?: boolean | undefined;
82
- tools?: {
83
- type: "function";
84
- function: {
85
- name: string;
86
- description?: string | undefined;
87
- parameters?: {
88
- type: "object";
89
- properties?: Record<string, {
90
- type: string;
91
- description?: string | undefined;
92
- enum?: string[] | undefined;
93
- }> | undefined;
94
- required?: string[] | undefined;
95
- } | undefined;
96
- };
97
- }[] | undefined;
98
- tool_choice?: "auto" | "none" | {
99
- type: "function";
100
- function: {
101
- name: string;
102
- };
103
- } | undefined;
104
- }, {
105
- id: string;
106
- object: "chat.completion";
107
- created: number;
108
- model: string;
109
- choices: {
110
- index: number;
111
- message: {
112
- role: "assistant";
113
- content?: string | null | undefined;
114
- tool_calls?: {
115
- id: string;
116
- type: "function";
117
- function: {
118
- name: string;
119
- arguments: string;
120
- };
121
- }[] | undefined;
122
- };
123
- finish_reason: "tool_calls" | "stop" | "length" | "content_filter";
124
- }[];
125
- usage: {
126
- prompt_tokens: number;
127
- completion_tokens: number;
128
- total_tokens: number;
129
- };
130
- }>;
131
- /**
132
- * 聊天完成(流式 SSE)
133
- *
134
- * 此端点使用 Server-Sent Events 协议返回流式 chunk。
135
- * 客户端应使用 `createAIClient().chatStream()` 而非 `api.call()`。
136
- * 定义此契约用于服务端路由注册和 API 文档。
137
- */
138
- readonly chatStream: EndpointDef<{
139
- messages: {
140
- role: "system" | "user" | "assistant" | "tool";
141
- content?: string | ({
142
- type: "text";
143
- text: string;
144
- } | {
145
- type: "image_url";
146
- image_url: {
147
- url: string;
148
- detail?: "auto" | "low" | "high" | undefined;
149
- };
150
- })[] | undefined;
151
- name?: string | undefined;
152
- tool_calls?: {
153
- id: string;
154
- type: "function";
155
- function: {
156
- name: string;
157
- arguments: string;
158
- };
159
- }[] | undefined;
160
- tool_call_id?: string | undefined;
161
- }[];
162
- model?: string | undefined;
163
- temperature?: number | undefined;
164
- top_p?: number | undefined;
165
- max_tokens?: number | undefined;
166
- stream?: boolean | undefined;
167
- tools?: {
168
- type: "function";
169
- function: {
170
- name: string;
171
- description?: string | undefined;
172
- parameters?: {
173
- type: "object";
174
- properties?: Record<string, {
175
- type: string;
176
- description?: string | undefined;
177
- enum?: string[] | undefined;
178
- }> | undefined;
179
- required?: string[] | undefined;
180
- } | undefined;
181
- };
182
- }[] | undefined;
183
- tool_choice?: "auto" | "none" | {
184
- type: "function";
185
- function: {
186
- name: string;
187
- };
188
- } | undefined;
189
- }, unknown>;
190
- /** 简单消息(便捷接口,封装单轮对话) */
191
- readonly sendMessage: EndpointDef<{
192
- message: string;
193
- systemPrompt?: string | undefined;
194
- model?: string | undefined;
195
- temperature?: number | undefined;
196
- }, {
197
- content: string;
198
- model: string;
199
- usage?: {
200
- prompt_tokens: number;
201
- completion_tokens: number;
202
- total_tokens: number;
203
- } | undefined;
204
- }>;
205
- /** 记忆检索 */
206
- readonly memoryRecall: EndpointDef<{
207
- query: string;
208
- topK?: number | undefined;
209
- types?: ("custom" | "fact" | "preference" | "event" | "summary")[] | undefined;
210
- minImportance?: number | undefined;
211
- objectId?: string | undefined;
212
- }, {
213
- items: {
214
- id: string;
215
- content: string;
216
- type: "custom" | "fact" | "preference" | "event" | "summary";
217
- importance: number;
218
- createdAt: number;
219
- lastAccessedAt: number;
220
- accessCount: number;
221
- objectId?: string | undefined;
222
- metadata?: Record<string, unknown> | undefined;
223
- }[];
224
- }>;
225
- /** 记忆列表(分页) */
226
- readonly memoryList: EndpointDef<{
227
- types?: ("custom" | "fact" | "preference" | "event" | "summary")[] | undefined;
228
- objectId?: string | undefined;
229
- offset?: number | undefined;
230
- limit?: number | undefined;
231
- }, {
232
- items: {
233
- id: string;
234
- content: string;
235
- type: "custom" | "fact" | "preference" | "event" | "summary";
236
- importance: number;
237
- createdAt: number;
238
- lastAccessedAt: number;
239
- accessCount: number;
240
- objectId?: string | undefined;
241
- metadata?: Record<string, unknown> | undefined;
242
- }[];
243
- total: number;
244
- }>;
245
- /** 会话列表 */
246
- readonly sessionList: EndpointDef<{
247
- objectId: string;
248
- }, {
249
- items: {
250
- objectId: string;
251
- sessionId: string;
252
- createdAt: number;
253
- updatedAt: number;
254
- metadata?: Record<string, unknown> | undefined;
255
- }[];
256
- }>;
257
- /** 对话历史 */
258
- readonly chatHistory: EndpointDef<{
259
- objectId: string;
260
- sessionId: string;
261
- limit?: number | undefined;
262
- order?: "asc" | "desc" | undefined;
263
- }, {
264
- items: {
265
- id: string;
266
- objectId: string;
267
- sessionId: string;
268
- request: {
269
- model: string;
270
- messages: {
271
- role: "system" | "user" | "assistant" | "tool";
272
- content?: string | ({
273
- type: "text";
274
- text: string;
275
- } | {
276
- type: "image_url";
277
- image_url: {
278
- url: string;
279
- detail?: "auto" | "low" | "high" | undefined;
280
- };
281
- })[] | undefined;
282
- name?: string | undefined;
283
- tool_calls?: {
284
- id: string;
285
- type: "function";
286
- function: {
287
- name: string;
288
- arguments: string;
289
- };
290
- }[] | undefined;
291
- tool_call_id?: string | undefined;
292
- }[];
293
- };
294
- response: {
295
- content: string;
296
- finishReason: string;
297
- usage: {
298
- prompt_tokens: number;
299
- completion_tokens: number;
300
- total_tokens: number;
301
- };
302
- toolCalls?: {
303
- id: string;
304
- type: "function";
305
- function: {
306
- name: string;
307
- arguments: string;
308
- };
309
- }[] | undefined;
310
- };
311
- createdAt: number;
312
- duration: number;
313
- }[];
314
- }>;
315
- };
316
-
317
- /**
318
- * @h-ai/ai/api — AI API 契约 Schema
319
- *
320
- * 入参/出参 Schema,客户端和服务端共享的唯一真相源。
321
- * @module ai-api-schemas
322
- */
323
-
324
- /** 聊天消息 */
325
- declare const ChatMessageSchema: z.ZodObject<{
326
- role: z.ZodEnum<{
327
- system: "system";
328
- user: "user";
329
- assistant: "assistant";
330
- tool: "tool";
331
- }>;
332
- content: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodArray<z.ZodUnion<readonly [z.ZodObject<{
333
- type: z.ZodLiteral<"text">;
334
- text: z.ZodString;
335
- }, z.core.$strip>, z.ZodObject<{
336
- type: z.ZodLiteral<"image_url">;
337
- image_url: z.ZodObject<{
338
- url: z.ZodString;
339
- detail: z.ZodOptional<z.ZodEnum<{
340
- auto: "auto";
341
- low: "low";
342
- high: "high";
343
- }>>;
344
- }, z.core.$strip>;
345
- }, z.core.$strip>]>>]>>;
346
- name: z.ZodOptional<z.ZodString>;
347
- tool_calls: z.ZodOptional<z.ZodArray<z.ZodObject<{
348
- id: z.ZodString;
349
- type: z.ZodLiteral<"function">;
350
- function: z.ZodObject<{
351
- name: z.ZodString;
352
- arguments: z.ZodString;
353
- }, z.core.$strip>;
354
- }, z.core.$strip>>>;
355
- tool_call_id: z.ZodOptional<z.ZodString>;
356
- }, z.core.$strip>;
357
- /** 聊天完成请求入参 */
358
- declare const ChatCompletionInputSchema: z.ZodObject<{
359
- model: z.ZodOptional<z.ZodString>;
360
- messages: z.ZodArray<z.ZodObject<{
361
- role: z.ZodEnum<{
362
- system: "system";
363
- user: "user";
364
- assistant: "assistant";
365
- tool: "tool";
366
- }>;
367
- content: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodArray<z.ZodUnion<readonly [z.ZodObject<{
368
- type: z.ZodLiteral<"text">;
369
- text: z.ZodString;
370
- }, z.core.$strip>, z.ZodObject<{
371
- type: z.ZodLiteral<"image_url">;
372
- image_url: z.ZodObject<{
373
- url: z.ZodString;
374
- detail: z.ZodOptional<z.ZodEnum<{
375
- auto: "auto";
376
- low: "low";
377
- high: "high";
378
- }>>;
379
- }, z.core.$strip>;
380
- }, z.core.$strip>]>>]>>;
381
- name: z.ZodOptional<z.ZodString>;
382
- tool_calls: z.ZodOptional<z.ZodArray<z.ZodObject<{
383
- id: z.ZodString;
384
- type: z.ZodLiteral<"function">;
385
- function: z.ZodObject<{
386
- name: z.ZodString;
387
- arguments: z.ZodString;
388
- }, z.core.$strip>;
389
- }, z.core.$strip>>>;
390
- tool_call_id: z.ZodOptional<z.ZodString>;
391
- }, z.core.$strip>>;
392
- temperature: z.ZodOptional<z.ZodNumber>;
393
- top_p: z.ZodOptional<z.ZodNumber>;
394
- max_tokens: z.ZodOptional<z.ZodNumber>;
395
- stream: z.ZodOptional<z.ZodBoolean>;
396
- tools: z.ZodOptional<z.ZodArray<z.ZodObject<{
397
- type: z.ZodLiteral<"function">;
398
- function: z.ZodObject<{
399
- name: z.ZodString;
400
- description: z.ZodOptional<z.ZodString>;
401
- parameters: z.ZodOptional<z.ZodObject<{
402
- type: z.ZodLiteral<"object">;
403
- properties: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{
404
- type: z.ZodString;
405
- description: z.ZodOptional<z.ZodString>;
406
- enum: z.ZodOptional<z.ZodArray<z.ZodString>>;
407
- }, z.core.$strip>>>;
408
- required: z.ZodOptional<z.ZodArray<z.ZodString>>;
409
- }, z.core.$strip>>;
410
- }, z.core.$strip>;
411
- }, z.core.$strip>>>;
412
- tool_choice: z.ZodOptional<z.ZodUnion<readonly [z.ZodLiteral<"auto">, z.ZodLiteral<"none">, z.ZodObject<{
413
- type: z.ZodLiteral<"function">;
414
- function: z.ZodObject<{
415
- name: z.ZodString;
416
- }, z.core.$strip>;
417
- }, z.core.$strip>]>>;
418
- }, z.core.$strip>;
419
- /** 聊天完成响应出参 */
420
- declare const ChatCompletionOutputSchema: z.ZodObject<{
421
- id: z.ZodString;
422
- object: z.ZodLiteral<"chat.completion">;
423
- created: z.ZodNumber;
424
- model: z.ZodString;
425
- choices: z.ZodArray<z.ZodObject<{
426
- index: z.ZodNumber;
427
- message: z.ZodObject<{
428
- role: z.ZodLiteral<"assistant">;
429
- content: z.ZodOptional<z.ZodNullable<z.ZodString>>;
430
- tool_calls: z.ZodOptional<z.ZodArray<z.ZodObject<{
431
- id: z.ZodString;
432
- type: z.ZodLiteral<"function">;
433
- function: z.ZodObject<{
434
- name: z.ZodString;
435
- arguments: z.ZodString;
436
- }, z.core.$strip>;
437
- }, z.core.$strip>>>;
438
- }, z.core.$strip>;
439
- finish_reason: z.ZodEnum<{
440
- tool_calls: "tool_calls";
441
- stop: "stop";
442
- length: "length";
443
- content_filter: "content_filter";
444
- }>;
445
- }, z.core.$strip>>;
446
- usage: z.ZodObject<{
447
- prompt_tokens: z.ZodNumber;
448
- completion_tokens: z.ZodNumber;
449
- total_tokens: z.ZodNumber;
450
- }, z.core.$strip>;
451
- }, z.core.$strip>;
452
- /** 简单消息请求入参(便捷接口) */
453
- declare const SendMessageInputSchema: z.ZodObject<{
454
- message: z.ZodString;
455
- systemPrompt: z.ZodOptional<z.ZodString>;
456
- model: z.ZodOptional<z.ZodString>;
457
- temperature: z.ZodOptional<z.ZodNumber>;
458
- }, z.core.$strip>;
459
- /** 简单消息响应出参 */
460
- declare const SendMessageOutputSchema: z.ZodObject<{
461
- content: z.ZodString;
462
- model: z.ZodString;
463
- usage: z.ZodOptional<z.ZodObject<{
464
- prompt_tokens: z.ZodNumber;
465
- completion_tokens: z.ZodNumber;
466
- total_tokens: z.ZodNumber;
467
- }, z.core.$strip>>;
468
- }, z.core.$strip>;
469
- /** 记忆检索请求入参 */
470
- declare const MemoryRecallInputSchema: z.ZodObject<{
471
- query: z.ZodString;
472
- topK: z.ZodOptional<z.ZodNumber>;
473
- types: z.ZodOptional<z.ZodArray<z.ZodEnum<{
474
- custom: "custom";
475
- fact: "fact";
476
- preference: "preference";
477
- event: "event";
478
- summary: "summary";
479
- }>>>;
480
- minImportance: z.ZodOptional<z.ZodNumber>;
481
- objectId: z.ZodOptional<z.ZodString>;
482
- }, z.core.$strip>;
483
- /** 记忆检索响应出参 */
484
- declare const MemoryRecallOutputSchema: z.ZodObject<{
485
- items: z.ZodArray<z.ZodObject<{
486
- id: z.ZodString;
487
- content: z.ZodString;
488
- type: z.ZodEnum<{
489
- custom: "custom";
490
- fact: "fact";
491
- preference: "preference";
492
- event: "event";
493
- summary: "summary";
494
- }>;
495
- importance: z.ZodNumber;
496
- objectId: z.ZodOptional<z.ZodString>;
497
- metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
498
- createdAt: z.ZodNumber;
499
- lastAccessedAt: z.ZodNumber;
500
- accessCount: z.ZodNumber;
501
- }, z.core.$strip>>;
502
- }, z.core.$strip>;
503
- /** 记忆列表请求入参 */
504
- declare const MemoryListInputSchema: z.ZodObject<{
505
- types: z.ZodOptional<z.ZodArray<z.ZodEnum<{
506
- custom: "custom";
507
- fact: "fact";
508
- preference: "preference";
509
- event: "event";
510
- summary: "summary";
511
- }>>>;
512
- objectId: z.ZodOptional<z.ZodString>;
513
- offset: z.ZodOptional<z.ZodNumber>;
514
- limit: z.ZodOptional<z.ZodNumber>;
515
- }, z.core.$strip>;
516
- /** 记忆列表响应出参 */
517
- declare const MemoryListOutputSchema: z.ZodObject<{
518
- items: z.ZodArray<z.ZodObject<{
519
- id: z.ZodString;
520
- content: z.ZodString;
521
- type: z.ZodEnum<{
522
- custom: "custom";
523
- fact: "fact";
524
- preference: "preference";
525
- event: "event";
526
- summary: "summary";
527
- }>;
528
- importance: z.ZodNumber;
529
- objectId: z.ZodOptional<z.ZodString>;
530
- metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
531
- createdAt: z.ZodNumber;
532
- lastAccessedAt: z.ZodNumber;
533
- accessCount: z.ZodNumber;
534
- }, z.core.$strip>>;
535
- total: z.ZodNumber;
536
- }, z.core.$strip>;
537
- /** 会话列表请求入参 */
538
- declare const SessionListInputSchema: z.ZodObject<{
539
- objectId: z.ZodString;
540
- }, z.core.$strip>;
541
- /** 会话列表响应出参 */
542
- declare const SessionListOutputSchema: z.ZodObject<{
543
- items: z.ZodArray<z.ZodObject<{
544
- objectId: z.ZodString;
545
- sessionId: z.ZodString;
546
- createdAt: z.ZodNumber;
547
- updatedAt: z.ZodNumber;
548
- metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
549
- }, z.core.$strip>>;
550
- }, z.core.$strip>;
551
- /** 对话历史请求入参 */
552
- declare const ChatHistoryInputSchema: z.ZodObject<{
553
- objectId: z.ZodString;
554
- sessionId: z.ZodString;
555
- limit: z.ZodOptional<z.ZodNumber>;
556
- order: z.ZodOptional<z.ZodEnum<{
557
- asc: "asc";
558
- desc: "desc";
559
- }>>;
560
- }, z.core.$strip>;
561
- /** 对话历史响应出参 */
562
- declare const ChatHistoryOutputSchema: z.ZodObject<{
563
- items: z.ZodArray<z.ZodObject<{
564
- id: z.ZodString;
565
- objectId: z.ZodString;
566
- sessionId: z.ZodString;
567
- request: z.ZodObject<{
568
- model: z.ZodString;
569
- messages: z.ZodArray<z.ZodObject<{
570
- role: z.ZodEnum<{
571
- system: "system";
572
- user: "user";
573
- assistant: "assistant";
574
- tool: "tool";
575
- }>;
576
- content: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodArray<z.ZodUnion<readonly [z.ZodObject<{
577
- type: z.ZodLiteral<"text">;
578
- text: z.ZodString;
579
- }, z.core.$strip>, z.ZodObject<{
580
- type: z.ZodLiteral<"image_url">;
581
- image_url: z.ZodObject<{
582
- url: z.ZodString;
583
- detail: z.ZodOptional<z.ZodEnum<{
584
- auto: "auto";
585
- low: "low";
586
- high: "high";
587
- }>>;
588
- }, z.core.$strip>;
589
- }, z.core.$strip>]>>]>>;
590
- name: z.ZodOptional<z.ZodString>;
591
- tool_calls: z.ZodOptional<z.ZodArray<z.ZodObject<{
592
- id: z.ZodString;
593
- type: z.ZodLiteral<"function">;
594
- function: z.ZodObject<{
595
- name: z.ZodString;
596
- arguments: z.ZodString;
597
- }, z.core.$strip>;
598
- }, z.core.$strip>>>;
599
- tool_call_id: z.ZodOptional<z.ZodString>;
600
- }, z.core.$strip>>;
601
- }, z.core.$strip>;
602
- response: z.ZodObject<{
603
- content: z.ZodString;
604
- toolCalls: z.ZodOptional<z.ZodArray<z.ZodObject<{
605
- id: z.ZodString;
606
- type: z.ZodLiteral<"function">;
607
- function: z.ZodObject<{
608
- name: z.ZodString;
609
- arguments: z.ZodString;
610
- }, z.core.$strip>;
611
- }, z.core.$strip>>>;
612
- finishReason: z.ZodString;
613
- usage: z.ZodObject<{
614
- prompt_tokens: z.ZodNumber;
615
- completion_tokens: z.ZodNumber;
616
- total_tokens: z.ZodNumber;
617
- }, z.core.$strip>;
618
- }, z.core.$strip>;
619
- createdAt: z.ZodNumber;
620
- duration: z.ZodNumber;
621
- }, z.core.$strip>>;
622
- }, z.core.$strip>;
623
- type ChatCompletionInput = z.infer<typeof ChatCompletionInputSchema>;
624
- type ChatCompletionOutput = z.infer<typeof ChatCompletionOutputSchema>;
625
- type SendMessageInput = z.infer<typeof SendMessageInputSchema>;
626
- type SendMessageOutput = z.infer<typeof SendMessageOutputSchema>;
627
-
628
- export { type ChatCompletionInput, ChatCompletionInputSchema, type ChatCompletionOutput, ChatCompletionOutputSchema, ChatHistoryInputSchema, ChatHistoryOutputSchema, ChatMessageSchema, MemoryListInputSchema, MemoryListOutputSchema, MemoryRecallInputSchema, MemoryRecallOutputSchema, type SendMessageInput, SendMessageInputSchema, type SendMessageOutput, SendMessageOutputSchema, SessionListInputSchema, SessionListOutputSchema, aiEndpoints };