@xsai/shared-chat 0.3.5 → 0.4.0-beta.10

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.d.ts CHANGED
@@ -1,75 +1,88 @@
1
- import { CommonRequestOptions } from '@xsai/shared';
1
+ import { WithUnknown, CommonRequestOptions } from '@xsai/shared';
2
2
 
3
3
  type FinishReason = 'content_filter' | 'error' | 'length' | 'other' | 'stop' | 'tool-calls' | (string & {});
4
4
 
5
- interface AudioBase64 {
6
- /**
7
- * Base64 encoded audio data.
8
- */
9
- data: string;
10
- format: 'mp3' | 'wav';
11
- }
12
- interface AudioPart extends CommonPart<'input_audio'> {
13
- input_audio: AudioBase64;
14
- }
15
- interface CommonPart<T extends string> {
16
- type: T;
5
+ interface AudioContentPart {
6
+ input_audio: {
7
+ data: string;
8
+ format: 'mp3' | 'wav';
9
+ };
10
+ type: 'input_audio';
17
11
  }
18
- interface ImagePart extends CommonPart<'image_url'> {
19
- image_url: ImageURLorBase64;
12
+ /** @internal */
13
+ type CommonContentPart = AudioContentPart | FileContentPart | ImageContentPart | TextContentPart;
14
+ interface FileContentPart {
15
+ file: {
16
+ file_data?: string;
17
+ file_id?: string;
18
+ filename?: string;
19
+ };
20
+ type: 'file';
20
21
  }
21
- interface ImageURLorBase64 {
22
- /**
23
- * Specifies the detail level of the image.
24
- */
25
- detail?: 'auto' | 'high' | 'low';
26
- /**
27
- * Either a URL of the image or the base64 encoded image data.
28
- */
29
- url: string;
22
+ interface ImageContentPart {
23
+ image_url: {
24
+ detail?: 'auto' | 'high' | 'low';
25
+ url: string;
26
+ };
27
+ type: 'image_url';
30
28
  }
31
- type Part = AudioPart | ImagePart | RefusalPart | TextPart;
32
- interface RefusalPart extends CommonPart<'refusal'> {
29
+ interface RefusalContentPart {
33
30
  refusal: string;
31
+ type: 'refusal';
34
32
  }
35
- interface TextPart extends CommonPart<'text'> {
33
+ interface TextContentPart {
36
34
  text: string;
35
+ type: 'text';
37
36
  }
38
37
 
39
- interface AssistantMessage extends Optional<CommonMessage<'assistant', AssistantMessagePart>, 'content'> {
40
- refusal?: null | string;
41
- tool_calls?: ToolCall[];
38
+ interface ToolCall {
39
+ function: ToolCallFunction | ToolCallFunctionWithoutArguments | ToolCallFunctionWithoutName;
40
+ id: string;
41
+ type: 'function';
42
42
  }
43
- type AssistantMessagePart = RefusalPart | TextPart;
44
- interface AssistantMessageResponse extends Omit<AssistantMessage, 'content'> {
45
- content?: string;
43
+ interface ToolCallFunction {
44
+ arguments: string;
45
+ name: string;
46
46
  }
47
- interface CommonMessage<T extends string, P extends Part> {
48
- content: Array<P> | string;
47
+ interface ToolCallFunctionWithoutArguments {
48
+ arguments?: never;
49
+ name: string;
50
+ }
51
+ interface ToolCallFunctionWithoutName {
52
+ arguments: string;
53
+ name?: never;
54
+ }
55
+
56
+ interface AssistantMessage {
57
+ content?: (RefusalContentPart | TextContentPart)[] | string;
49
58
  name?: string;
50
- role: T;
59
+ refusal?: string;
60
+ role: 'assistant';
61
+ tool_calls?: ToolCall[];
51
62
  }
52
- type Message = AssistantMessage | SystemMessage | ToolMessage | UserMessage;
53
- interface SystemMessage extends CommonMessage<'system', SystemMessagePart> {
63
+ interface DeveloperMessage {
64
+ content: string | TextContentPart[];
65
+ name?: string;
66
+ /** @remarks Before using, confirm that your model supports this. */
67
+ role: 'developer';
54
68
  }
55
- type SystemMessagePart = TextPart;
56
- interface ToolCall {
57
- function: {
58
- arguments: string;
59
- name: string;
60
- };
61
- id: string;
62
- index: number;
63
- type: 'function';
69
+ type Message = AssistantMessage | DeveloperMessage | SystemMessage | ToolMessage | UserMessage;
70
+ interface SystemMessage {
71
+ content: string | TextContentPart[];
72
+ name?: string;
73
+ role: 'system';
64
74
  }
65
- interface ToolMessage extends Omit<CommonMessage<'tool', ToolMessagePart>, 'name'> {
75
+ interface ToolMessage {
76
+ /** @remarks considering the support of ecosystems (such as MCP), we have relaxed this type. */
77
+ content: CommonContentPart[] | string;
78
+ role: 'tool';
66
79
  tool_call_id: string;
67
80
  }
68
- type ToolMessagePart = AudioPart | ImagePart | TextPart;
69
- interface UserMessage extends CommonMessage<'user', UserMessagePart> {
81
+ interface UserMessage {
82
+ content: CommonContentPart[] | string;
83
+ name?: string;
84
+ role: 'user';
70
85
  }
71
- type UserMessagePart = AudioPart | ImagePart | TextPart;
72
- type Optional<T, K extends keyof T> = Omit<T, K> & Pick<Partial<T>, K>;
73
86
 
74
87
  interface CompletionToolCall {
75
88
  args: string;
@@ -79,7 +92,7 @@ interface CompletionToolCall {
79
92
  }
80
93
  interface CompletionToolResult {
81
94
  args: Record<string, unknown>;
82
- result: string | ToolMessagePart[];
95
+ result: ToolMessage['content'];
83
96
  toolCallId: string;
84
97
  toolName: string;
85
98
  }
@@ -119,16 +132,22 @@ type CompletionStep<T extends boolean = false> = (T extends true ? {
119
132
  };
120
133
  type CompletionStepType = 'continue' | 'done' | 'initial' | 'tool-result';
121
134
 
122
- type ToolChoice = 'auto' | 'none' | 'required' | {
135
+ type ToolChoice = AllowedTools | ToolChoiceMode | ToolChoiceTool;
136
+ interface AllowedTools {
137
+ mode: 'auto' | 'required';
138
+ tools: ToolChoiceTool[];
139
+ type: 'allowed_tools';
140
+ }
141
+ type ToolChoiceMode = 'auto' | 'none' | 'required';
142
+ interface ToolChoiceTool {
123
143
  function: {
124
144
  name: string;
125
145
  };
126
146
  type: 'function';
127
- };
147
+ }
128
148
 
129
149
  /** @see {@link https://platform.openai.com/docs/api-reference/chat/create} */
130
150
  interface ChatOptions extends CommonRequestOptions {
131
- [key: string]: unknown;
132
151
  /**
133
152
  * number between -2.0 and 2.0.
134
153
  * @default 0
@@ -153,7 +172,7 @@ interface ChatOptions extends CommonRequestOptions {
153
172
  /** @default 1 */
154
173
  topP?: number;
155
174
  }
156
- declare const chat: <T extends ChatOptions>(options: T) => Promise<Response>;
175
+ declare const chat: <T extends WithUnknown<ChatOptions>>(options: T) => Promise<Response>;
157
176
 
158
177
  interface DetermineStepTypeOptions {
159
178
  finishReason: FinishReason;
@@ -177,4 +196,5 @@ interface ExecuteToolResult {
177
196
  }
178
197
  declare const executeTool: ({ abortSignal, messages, toolCall, tools }: ExecuteToolOptions) => Promise<ExecuteToolResult>;
179
198
 
180
- export { type AssistantMessage, type AssistantMessagePart, type AssistantMessageResponse, type AudioBase64, type AudioPart, type ChatOptions, type CommonMessage, type CommonPart, type CompletionStep, type CompletionStepType, type CompletionToolCall, type CompletionToolResult, type DetermineStepTypeOptions, type ExecuteToolOptions, type ExecuteToolResult, type FinishReason, type ImagePart, type ImageURLorBase64, type Message, type Part, type RefusalPart, type SystemMessage, type SystemMessagePart, type TextPart, type Tool, type ToolCall, type ToolChoice, type ToolExecuteOptions, type ToolExecuteResult, type ToolMessage, type ToolMessagePart, type Usage, type UserMessage, type UserMessagePart, chat, determineStepType, executeTool };
199
+ export { chat, determineStepType, executeTool };
200
+ export type { AssistantMessage, AudioContentPart, ChatOptions, CommonContentPart, CompletionStep, CompletionStepType, CompletionToolCall, CompletionToolResult, DetermineStepTypeOptions, DeveloperMessage, ExecuteToolOptions, ExecuteToolResult, FileContentPart, FinishReason, ImageContentPart, Message, RefusalContentPart, SystemMessage, TextContentPart, Tool, ToolCall, ToolChoice, ToolExecuteOptions, ToolExecuteResult, ToolMessage, Usage, UserMessage };
package/dist/index.js CHANGED
@@ -35,7 +35,7 @@ const wrapToolResult = (result) => {
35
35
  if (typeof result === "string")
36
36
  return result;
37
37
  if (Array.isArray(result)) {
38
- if (result.every((item) => !!(typeof item === "object" && "type" in item && ["audio", "image", "text"].includes(item.type)))) {
38
+ if (result.every((item) => !!(typeof item === "object" && "type" in item && ["file", "image_url", "input_audio", "text"].includes(item.type)))) {
39
39
  return result;
40
40
  }
41
41
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@xsai/shared-chat",
3
3
  "type": "module",
4
- "version": "0.3.5",
4
+ "version": "0.4.0-beta.10",
5
5
  "description": "extra-small AI SDK.",
6
6
  "author": "Moeru AI",
7
7
  "license": "MIT",
@@ -29,7 +29,7 @@
29
29
  "dist"
30
30
  ],
31
31
  "dependencies": {
32
- "@xsai/shared": "~0.3.5"
32
+ "@xsai/shared": "~0.4.0-beta.10"
33
33
  },
34
34
  "scripts": {
35
35
  "build": "pkgroll"