@xsai/shared-chat 0.0.28 → 0.0.30

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,6 +1,15 @@
1
1
  import { CommonRequestOptions } from '@xsai/shared';
2
2
 
3
- type Part = AudioPart | ImagePart | RefusalPart | TextPart;
3
+ interface AudioBase64 {
4
+ /**
5
+ * Base64 encoded audio data.
6
+ */
7
+ data: string;
8
+ format: 'mp3' | 'wav';
9
+ }
10
+ interface AudioPart extends CommonPart<'input_audio'> {
11
+ input_audio: AudioBase64;
12
+ }
4
13
  interface CommonPart<T extends string> {
5
14
  type: T;
6
15
  }
@@ -17,16 +26,7 @@ interface ImageURLorBase64 {
17
26
  */
18
27
  url: string;
19
28
  }
20
- interface AudioPart extends CommonPart<'input_audio'> {
21
- input_audio: AudioBase64;
22
- }
23
- interface AudioBase64 {
24
- /**
25
- * Base64 encoded audio data.
26
- */
27
- data: string;
28
- format: 'mp3' | 'wav';
29
- }
29
+ type Part = AudioPart | ImagePart | RefusalPart | TextPart;
30
30
  interface RefusalPart extends CommonPart<'refusal'> {
31
31
  refusal: string;
32
32
  }
@@ -34,20 +34,23 @@ interface TextPart extends CommonPart<'text'> {
34
34
  text: string;
35
35
  }
36
36
 
37
- type Optional<T, K extends keyof T> = Omit<T, K> & Pick<Partial<T>, K>;
38
- type Message = AssistantMessage | SystemMessage | ToolMessage | UserMessage;
37
+ interface AssistantMessage extends Optional<CommonMessage<'assistant', AssistantMessagePart>, 'content'> {
38
+ refusal?: null | string;
39
+ tool_calls?: ToolCall[];
40
+ }
41
+ type AssistantMessagePart = RefusalPart | TextPart;
42
+ interface AssistantMessageResponse extends Omit<AssistantMessage, 'content'> {
43
+ content?: string;
44
+ }
39
45
  interface CommonMessage<T extends string, P extends Part> {
40
46
  content: Array<P> | string;
41
47
  name?: string;
42
48
  role: T;
43
49
  }
44
- type SystemMessagePart = TextPart;
50
+ type Message = AssistantMessage | SystemMessage | ToolMessage | UserMessage;
45
51
  interface SystemMessage extends CommonMessage<'system', SystemMessagePart> {
46
52
  }
47
- type UserMessagePart = AudioPart | ImagePart | TextPart;
48
- interface UserMessage extends CommonMessage<'user', UserMessagePart> {
49
- }
50
- type AssistantMessagePart = RefusalPart | TextPart;
53
+ type SystemMessagePart = TextPart;
51
54
  interface ToolCall {
52
55
  function: {
53
56
  arguments: string;
@@ -56,17 +59,14 @@ interface ToolCall {
56
59
  id: string;
57
60
  type: 'function';
58
61
  }
59
- interface AssistantMessage extends Optional<CommonMessage<'assistant', AssistantMessagePart>, 'content'> {
60
- refusal?: null | string;
61
- tool_calls?: ToolCall[];
62
- }
63
- interface AssistantMessageResponse extends Omit<AssistantMessage, 'content'> {
64
- content?: string;
65
- }
66
- type ToolMessagePart = TextPart;
67
62
  interface ToolMessage extends Omit<CommonMessage<'tool', ToolMessagePart>, 'name'> {
68
63
  tool_call_id: string;
69
64
  }
65
+ type ToolMessagePart = TextPart;
66
+ interface UserMessage extends CommonMessage<'user', UserMessagePart> {
67
+ }
68
+ type UserMessagePart = AudioPart | ImagePart | TextPart;
69
+ type Optional<T, K extends keyof T> = Omit<T, K> & Pick<Partial<T>, K>;
70
70
 
71
71
  type ToolChoice = 'auto' | 'none' | 'required' | {
72
72
  function: {
@@ -81,10 +81,10 @@ interface ChatOptions extends CommonRequestOptions {
81
81
  toolChoice?: ToolChoice;
82
82
  }
83
83
 
84
- type FinishReason = 'content_filter' | 'error' | 'length' | 'other' | 'stop' | 'tool-calls' | ({} & string);
84
+ type FinishReason = 'content_filter' | 'error' | 'length' | 'other' | 'stop' | 'tool-calls' | (string & {});
85
85
 
86
86
  interface Tool {
87
- execute: (input: unknown) => Promise<string> | string;
87
+ execute: (input: unknown, options: ToolExecuteOptions) => Promise<string> | string;
88
88
  function: {
89
89
  description?: string;
90
90
  name: string;
@@ -93,6 +93,11 @@ interface Tool {
93
93
  };
94
94
  type: 'function';
95
95
  }
96
+ interface ToolExecuteOptions {
97
+ abortSignal?: AbortSignal;
98
+ messages: Message[];
99
+ toolCallId: string;
100
+ }
96
101
 
97
102
  interface Usage {
98
103
  completion_tokens: number;
@@ -102,23 +107,4 @@ interface Usage {
102
107
 
103
108
  declare const chat: <T extends ChatOptions>(options: T) => Promise<Response>;
104
109
 
105
- declare const messages: (...messages: Message[]) => Message[];
106
- declare const system: <C extends string | SystemMessagePart[]>(content: C) => SystemMessage;
107
- declare const user: <C extends Array<UserMessagePart> | string>(content: C) => UserMessage;
108
- declare const textPart: (text: string) => TextPart;
109
- declare const imagePart: (url: string) => ImagePart;
110
- declare const assistant: <C extends AssistantMessagePart[] | string | ToolCall>(content: C) => AssistantMessage;
111
- declare const tool: <C extends string | ToolMessagePart[]>(content: C, toolCall: ToolCall) => ToolMessage;
112
-
113
- declare const message_assistant: typeof assistant;
114
- declare const message_imagePart: typeof imagePart;
115
- declare const message_messages: typeof messages;
116
- declare const message_system: typeof system;
117
- declare const message_textPart: typeof textPart;
118
- declare const message_tool: typeof tool;
119
- declare const message_user: typeof user;
120
- declare namespace message {
121
- export { message_assistant as assistant, message_imagePart as imagePart, message_messages as messages, message_system as system, message_textPart as textPart, message_tool as tool, message_user as user };
122
- }
123
-
124
- export { type AssistantMessage, type AssistantMessagePart, type AssistantMessageResponse, type AudioBase64, type AudioPart, type ChatOptions, type CommonMessage, type CommonPart, 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 ToolMessage, type ToolMessagePart, type Usage, type UserMessage, type UserMessagePart, chat, message };
110
+ export { type AssistantMessage, type AssistantMessagePart, type AssistantMessageResponse, type AudioBase64, type AudioPart, type ChatOptions, type CommonMessage, type CommonPart, 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 ToolMessage, type ToolMessagePart, type Usage, type UserMessage, type UserMessagePart, chat };
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { requestURL, requestBody, requestHeaders, responseCatch } from '@xsai/shared';
2
2
 
3
- const chat = async (options) => await (options.fetch ?? globalThis.fetch)(requestURL("chat/completions", options.baseURL), {
3
+ const chat = async (options) => (options.fetch ?? globalThis.fetch)(requestURL("chat/completions", options.baseURL), {
4
4
  body: requestBody({
5
5
  ...options,
6
6
  tools: options.tools?.map((tool) => ({
@@ -16,27 +16,4 @@ const chat = async (options) => await (options.fetch ?? globalThis.fetch)(reques
16
16
  signal: options.abortSignal
17
17
  }).then(responseCatch);
18
18
 
19
- const messages = (...messages2) => messages2;
20
- const system = (content) => ({ content, role: "system" });
21
- const user = (content) => ({ content, role: "user" });
22
- const textPart = (text) => ({ text, type: "text" });
23
- const imagePart = (url) => ({ image_url: { url }, type: "image_url" });
24
- const assistant = (content) => typeof content === "string" || Array.isArray(content) ? { content, role: "assistant" } : { role: "assistant", tool_calls: [content] };
25
- const tool = (content, toolCall) => ({
26
- content,
27
- role: "tool",
28
- tool_call_id: toolCall.id
29
- });
30
-
31
- var message = /*#__PURE__*/Object.freeze({
32
- __proto__: null,
33
- assistant: assistant,
34
- imagePart: imagePart,
35
- messages: messages,
36
- system: system,
37
- textPart: textPart,
38
- tool: tool,
39
- user: user
40
- });
41
-
42
- export { chat, message };
19
+ export { chat };
package/package.json CHANGED
@@ -1,31 +1,31 @@
1
1
  {
2
2
  "name": "@xsai/shared-chat",
3
- "version": "0.0.28",
4
3
  "type": "module",
4
+ "version": "0.0.30",
5
+ "description": "extra-small AI SDK for Browser, Node.js, Deno, Bun or Edge Runtime.",
5
6
  "author": "Moeru AI",
6
7
  "license": "MIT",
7
8
  "homepage": "https://xsai.js.org",
8
- "description": "extra-small AI SDK for Browser, Node.js, Deno, Bun or Edge Runtime.",
9
- "keywords": [
10
- "xsai",
11
- "openai",
12
- "ai"
13
- ],
14
9
  "repository": {
15
10
  "type": "git",
16
11
  "url": "git+https://github.com/moeru-ai/xsai.git",
17
12
  "directory": "packages/shared-chat"
18
13
  },
19
14
  "bugs": "https://github.com/moeru-ai/xsai/issues",
15
+ "keywords": [
16
+ "xsai",
17
+ "openai",
18
+ "ai"
19
+ ],
20
20
  "sideEffects": false,
21
- "main": "./dist/index.js",
22
- "types": "./dist/index.d.ts",
23
21
  "exports": {
24
22
  ".": {
25
23
  "types": "./dist/index.d.ts",
26
24
  "default": "./dist/index.js"
27
25
  }
28
26
  },
27
+ "main": "./dist/index.js",
28
+ "types": "./dist/index.d.ts",
29
29
  "files": [
30
30
  "dist"
31
31
  ],