@xsai/shared-chat 0.0.22

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/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Moeru AI
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,107 @@
1
+ import { CommonRequestOptions } from '@xsai/shared';
2
+
3
+ type Part = AudioPart | ImagePart | RefusalPart | TextPart;
4
+ interface CommonPart<T extends string> {
5
+ type: T;
6
+ }
7
+ interface ImagePart extends CommonPart<'image_url'> {
8
+ image_url: ImageURLorBase64;
9
+ }
10
+ interface ImageURLorBase64 {
11
+ /**
12
+ * Specifies the detail level of the image.
13
+ */
14
+ detail?: 'auto' | 'high' | 'low';
15
+ /**
16
+ * Either a URL of the image or the base64 encoded image data.
17
+ */
18
+ url: string;
19
+ }
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
+ }
30
+ interface RefusalPart extends CommonPart<'refusal'> {
31
+ refusal: string;
32
+ }
33
+ interface TextPart extends CommonPart<'text'> {
34
+ text: string;
35
+ }
36
+
37
+ type Optional<T, K extends keyof T> = Omit<T, K> & Pick<Partial<T>, K>;
38
+ type Message = AssistantMessage | SystemMessage | ToolMessage | UserMessage;
39
+ interface CommonMessage<T extends string, P extends Part> {
40
+ content: Array<P> | string;
41
+ name?: string;
42
+ role: T;
43
+ }
44
+ type SystemMessagePart = TextPart;
45
+ interface SystemMessage extends CommonMessage<'system', SystemMessagePart> {
46
+ }
47
+ type UserMessagePart = AudioPart | ImagePart | TextPart;
48
+ interface UserMessage extends CommonMessage<'user', UserMessagePart> {
49
+ }
50
+ type AssistantMessagePart = RefusalPart | TextPart;
51
+ interface ToolCall {
52
+ function: {
53
+ arguments: string;
54
+ name: string;
55
+ };
56
+ id: string;
57
+ type: 'function';
58
+ }
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
+ interface ToolMessage extends Omit<CommonMessage<'tool', ToolMessagePart>, 'name'> {
68
+ tool_call_id: string;
69
+ }
70
+
71
+ type ToolChoice = 'auto' | 'none' | 'required' | {
72
+ function: {
73
+ name: string;
74
+ };
75
+ type: 'function';
76
+ };
77
+
78
+ interface ChatCompletionOptions extends CommonRequestOptions {
79
+ [key: string]: unknown;
80
+ messages: Message[];
81
+ toolChoice?: ToolChoice;
82
+ }
83
+
84
+ type FinishReason = 'content_filter' | 'error' | 'length' | 'other' | 'stop' | 'tool-calls' | ({} & string);
85
+
86
+ interface Tool {
87
+ execute: (input: unknown) => Promise<string> | string;
88
+ function: {
89
+ description?: string;
90
+ name: string;
91
+ parameters: Record<string, unknown>;
92
+ strict?: boolean;
93
+ };
94
+ type: 'function';
95
+ }
96
+
97
+ declare const chatCompletion: <T extends ChatCompletionOptions>(options: T) => Promise<Response>;
98
+
99
+ declare function messages(...messages: Message[]): Message[];
100
+ declare function system<C extends string | SystemMessagePart[]>(content: C): SystemMessage;
101
+ declare function user<C extends Array<UserMessagePart> | string>(content: C): UserMessage;
102
+ declare function textPart(text: string): TextPart;
103
+ declare function imagePart(url: string): ImagePart;
104
+ declare function assistant<C extends AssistantMessagePart[] | string | ToolCall>(content: C): AssistantMessage;
105
+ declare function tool<C extends string | ToolMessagePart[]>(content: C, toolCall: ToolCall): ToolMessage;
106
+
107
+ export { type AssistantMessage, type AssistantMessagePart, type AssistantMessageResponse, type AudioBase64, type AudioPart, type ChatCompletionOptions, 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 UserMessage, type UserMessagePart, assistant, chatCompletion, imagePart, messages, system, textPart, tool, user };
package/dist/index.js ADDED
@@ -0,0 +1,49 @@
1
+ import { requestBody, requestHeaders } from '@xsai/shared';
2
+
3
+ const chatCompletion = async (options) => await (options.fetch ?? globalThis.fetch)(new URL("chat/completions", options.baseURL), {
4
+ body: requestBody({
5
+ ...options,
6
+ tools: options.tools?.map((tool) => ({
7
+ function: tool.function,
8
+ type: "function"
9
+ }))
10
+ }),
11
+ headers: requestHeaders({
12
+ "Content-Type": "application/json",
13
+ ...options.headers
14
+ }, options.apiKey),
15
+ method: "POST",
16
+ signal: options.abortSignal
17
+ });
18
+
19
+ function messages(...messages2) {
20
+ return messages2;
21
+ }
22
+ function system(content) {
23
+ return { content, role: "system" };
24
+ }
25
+ function user(content) {
26
+ return { content, role: "user" };
27
+ }
28
+ function textPart(text) {
29
+ return { text, type: "text" };
30
+ }
31
+ function imagePart(url) {
32
+ return { image_url: { url }, type: "image_url" };
33
+ }
34
+ function assistant(content) {
35
+ if (typeof content === "string")
36
+ return { content, role: "assistant" };
37
+ if (Array.isArray(content))
38
+ return { content, role: "assistant" };
39
+ return { role: "assistant", tool_calls: [content] };
40
+ }
41
+ function tool(content, toolCall) {
42
+ return {
43
+ content,
44
+ role: "tool",
45
+ tool_call_id: toolCall.id
46
+ };
47
+ }
48
+
49
+ export { assistant, chatCompletion, imagePart, messages, system, textPart, tool, user };
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@xsai/shared-chat",
3
+ "version": "0.0.22",
4
+ "type": "module",
5
+ "author": "Moeru AI",
6
+ "license": "MIT",
7
+ "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
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/moeru-ai/xsai.git",
17
+ "directory": "packages/shared-chat"
18
+ },
19
+ "bugs": "https://github.com/moeru-ai/xsai/issues",
20
+ "sideEffects": false,
21
+ "main": "./dist/index.js",
22
+ "types": "./dist/index.d.ts",
23
+ "exports": {
24
+ ".": {
25
+ "types": "./dist/index.d.ts",
26
+ "default": "./dist/index.js"
27
+ }
28
+ },
29
+ "files": [
30
+ "dist"
31
+ ],
32
+ "dependencies": {
33
+ "@xsai/shared": ""
34
+ },
35
+ "scripts": {
36
+ "build": "pkgroll",
37
+ "build:watch": "pkgroll --watch"
38
+ }
39
+ }