@livekit/agents 0.4.6 → 0.5.1
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/README.md +17 -0
- package/dist/audio.cjs +77 -0
- package/dist/audio.cjs.map +1 -0
- package/dist/audio.js +48 -37
- package/dist/audio.js.map +1 -1
- package/dist/cli.cjs +131 -0
- package/dist/cli.cjs.map +1 -0
- package/dist/cli.js +96 -122
- package/dist/cli.js.map +1 -1
- package/dist/generator.cjs +36 -0
- package/dist/generator.cjs.map +1 -0
- package/dist/generator.js +8 -22
- package/dist/generator.js.map +1 -1
- package/dist/http_server.cjs +72 -0
- package/dist/http_server.cjs.map +1 -0
- package/dist/http_server.d.ts +1 -1
- package/dist/http_server.js +44 -47
- package/dist/http_server.js.map +1 -1
- package/dist/index.cjs +78 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.js +26 -28
- package/dist/index.js.map +1 -1
- package/dist/ipc/job_executor.cjs +33 -0
- package/dist/ipc/job_executor.cjs.map +1 -0
- package/dist/ipc/job_executor.js +7 -4
- package/dist/ipc/job_executor.js.map +1 -1
- package/dist/ipc/job_main.cjs +147 -0
- package/dist/ipc/job_main.cjs.map +1 -0
- package/dist/ipc/job_main.d.ts +1 -1
- package/dist/ipc/job_main.js +103 -103
- package/dist/ipc/job_main.js.map +1 -1
- package/dist/ipc/message.cjs +17 -0
- package/dist/ipc/message.cjs.map +1 -0
- package/dist/ipc/message.js +0 -1
- package/dist/ipc/message.js.map +1 -1
- package/dist/ipc/proc_job_executor.cjs +174 -0
- package/dist/ipc/proc_job_executor.cjs.map +1 -0
- package/dist/ipc/proc_job_executor.js +130 -126
- package/dist/ipc/proc_job_executor.js.map +1 -1
- package/dist/ipc/proc_pool.cjs +126 -0
- package/dist/ipc/proc_pool.cjs.map +1 -0
- package/dist/ipc/proc_pool.js +93 -96
- package/dist/ipc/proc_pool.js.map +1 -1
- package/dist/job.cjs +230 -0
- package/dist/job.cjs.map +1 -0
- package/dist/job.d.ts +6 -1
- package/dist/job.d.ts.map +1 -1
- package/dist/job.js +195 -198
- package/dist/job.js.map +1 -1
- package/dist/llm/chat_context.cjs +131 -0
- package/dist/llm/chat_context.cjs.map +1 -0
- package/dist/llm/chat_context.js +98 -86
- package/dist/llm/chat_context.js.map +1 -1
- package/dist/llm/function_context.cjs +103 -0
- package/dist/llm/function_context.cjs.map +1 -0
- package/dist/llm/function_context.js +72 -81
- package/dist/llm/function_context.js.map +1 -1
- package/dist/llm/function_context.test.cjs +218 -0
- package/dist/llm/function_context.test.cjs.map +1 -0
- package/dist/llm/function_context.test.js +209 -210
- package/dist/llm/function_context.test.js.map +1 -1
- package/dist/llm/index.cjs +43 -0
- package/dist/llm/index.cjs.map +1 -0
- package/dist/llm/index.js +22 -6
- package/dist/llm/index.js.map +1 -1
- package/dist/llm/llm.cjs +76 -0
- package/dist/llm/llm.cjs.map +1 -0
- package/dist/llm/llm.js +48 -42
- package/dist/llm/llm.js.map +1 -1
- package/dist/log.cjs +57 -0
- package/dist/log.cjs.map +1 -0
- package/dist/log.js +27 -26
- package/dist/log.js.map +1 -1
- package/dist/multimodal/agent_playout.cjs +228 -0
- package/dist/multimodal/agent_playout.cjs.map +1 -0
- package/dist/multimodal/agent_playout.d.ts +1 -1
- package/dist/multimodal/agent_playout.js +193 -180
- package/dist/multimodal/agent_playout.js.map +1 -1
- package/dist/multimodal/index.cjs +25 -0
- package/dist/multimodal/index.cjs.map +1 -0
- package/dist/multimodal/index.js +2 -5
- package/dist/multimodal/index.js.map +1 -1
- package/dist/multimodal/multimodal_agent.cjs +404 -0
- package/dist/multimodal/multimodal_agent.cjs.map +1 -0
- package/dist/multimodal/multimodal_agent.d.ts +1 -1
- package/dist/multimodal/multimodal_agent.js +351 -330
- package/dist/multimodal/multimodal_agent.js.map +1 -1
- package/dist/pipeline/agent_output.cjs +172 -0
- package/dist/pipeline/agent_output.cjs.map +1 -0
- package/dist/pipeline/agent_output.js +136 -138
- package/dist/pipeline/agent_output.js.map +1 -1
- package/dist/pipeline/agent_playout.cjs +169 -0
- package/dist/pipeline/agent_playout.cjs.map +1 -0
- package/dist/pipeline/agent_playout.js +126 -136
- package/dist/pipeline/agent_playout.js.map +1 -1
- package/dist/pipeline/human_input.cjs +158 -0
- package/dist/pipeline/human_input.cjs.map +1 -0
- package/dist/pipeline/human_input.js +124 -125
- package/dist/pipeline/human_input.js.map +1 -1
- package/dist/pipeline/index.cjs +31 -0
- package/dist/pipeline/index.cjs.map +1 -0
- package/dist/pipeline/index.js +8 -4
- package/dist/pipeline/index.js.map +1 -1
- package/dist/pipeline/pipeline_agent.cjs +642 -0
- package/dist/pipeline/pipeline_agent.cjs.map +1 -0
- package/dist/pipeline/pipeline_agent.js +595 -651
- package/dist/pipeline/pipeline_agent.js.map +1 -1
- package/dist/pipeline/speech_handle.cjs +128 -0
- package/dist/pipeline/speech_handle.cjs.map +1 -0
- package/dist/pipeline/speech_handle.js +102 -100
- package/dist/pipeline/speech_handle.js.map +1 -1
- package/dist/plugin.cjs +46 -0
- package/dist/plugin.cjs.map +1 -0
- package/dist/plugin.js +20 -20
- package/dist/plugin.js.map +1 -1
- package/dist/stt/index.cjs +38 -0
- package/dist/stt/index.cjs.map +1 -0
- package/dist/stt/index.js +13 -5
- package/dist/stt/index.js.map +1 -1
- package/dist/stt/stream_adapter.cjs +87 -0
- package/dist/stt/stream_adapter.cjs.map +1 -0
- package/dist/stt/stream_adapter.js +58 -55
- package/dist/stt/stream_adapter.js.map +1 -1
- package/dist/stt/stt.cjs +98 -0
- package/dist/stt/stt.cjs.map +1 -0
- package/dist/stt/stt.js +63 -98
- package/dist/stt/stt.js.map +1 -1
- package/dist/tokenize/basic/basic.cjs +98 -0
- package/dist/tokenize/basic/basic.cjs.map +1 -0
- package/dist/tokenize/basic/basic.d.ts +1 -1
- package/dist/tokenize/basic/basic.d.ts.map +1 -1
- package/dist/tokenize/basic/basic.js +56 -45
- package/dist/tokenize/basic/basic.js.map +1 -1
- package/dist/tokenize/basic/hyphenator.cjs +425 -0
- package/dist/tokenize/basic/hyphenator.cjs.map +1 -0
- package/dist/tokenize/basic/hyphenator.js +66 -82
- package/dist/tokenize/basic/hyphenator.js.map +1 -1
- package/dist/tokenize/basic/index.cjs +35 -0
- package/dist/tokenize/basic/index.cjs.map +1 -0
- package/dist/tokenize/basic/index.js +7 -4
- package/dist/tokenize/basic/index.js.map +1 -1
- package/dist/tokenize/basic/paragraph.cjs +57 -0
- package/dist/tokenize/basic/paragraph.cjs.map +1 -0
- package/dist/tokenize/basic/paragraph.js +30 -35
- package/dist/tokenize/basic/paragraph.js.map +1 -1
- package/dist/tokenize/basic/sentence.cjs +89 -0
- package/dist/tokenize/basic/sentence.cjs.map +1 -0
- package/dist/tokenize/basic/sentence.d.ts.map +1 -1
- package/dist/tokenize/basic/sentence.js +62 -57
- package/dist/tokenize/basic/sentence.js.map +1 -1
- package/dist/tokenize/basic/word.cjs +44 -0
- package/dist/tokenize/basic/word.cjs.map +1 -0
- package/dist/tokenize/basic/word.js +17 -20
- package/dist/tokenize/basic/word.js.map +1 -1
- package/dist/tokenize/index.cjs +55 -0
- package/dist/tokenize/index.cjs.map +1 -0
- package/dist/tokenize/index.js +18 -7
- package/dist/tokenize/index.js.map +1 -1
- package/dist/tokenize/token_stream.cjs +164 -0
- package/dist/tokenize/token_stream.cjs.map +1 -0
- package/dist/tokenize/token_stream.js +133 -139
- package/dist/tokenize/token_stream.js.map +1 -1
- package/dist/tokenize/tokenizer.cjs +184 -0
- package/dist/tokenize/tokenizer.cjs.map +1 -0
- package/dist/tokenize/tokenizer.js +138 -99
- package/dist/tokenize/tokenizer.js.map +1 -1
- package/dist/tokenize/tokenizer.test.cjs +220 -0
- package/dist/tokenize/tokenizer.test.cjs.map +1 -0
- package/dist/tokenize/tokenizer.test.d.ts +2 -0
- package/dist/tokenize/tokenizer.test.d.ts.map +1 -0
- package/dist/tokenize/tokenizer.test.js +219 -0
- package/dist/tokenize/tokenizer.test.js.map +1 -0
- package/dist/transcription.cjs +131 -0
- package/dist/transcription.cjs.map +1 -0
- package/dist/transcription.js +99 -96
- package/dist/transcription.js.map +1 -1
- package/dist/tts/index.cjs +38 -0
- package/dist/tts/index.cjs.map +1 -0
- package/dist/tts/index.js +13 -5
- package/dist/tts/index.js.map +1 -1
- package/dist/tts/stream_adapter.cjs +78 -0
- package/dist/tts/stream_adapter.cjs.map +1 -0
- package/dist/tts/stream_adapter.js +50 -47
- package/dist/tts/stream_adapter.js.map +1 -1
- package/dist/tts/tts.cjs +127 -0
- package/dist/tts/tts.cjs.map +1 -0
- package/dist/tts/tts.js +90 -120
- package/dist/tts/tts.js.map +1 -1
- package/dist/utils.cjs +284 -0
- package/dist/utils.cjs.map +1 -0
- package/dist/utils.js +242 -247
- package/dist/utils.js.map +1 -1
- package/dist/vad.cjs +92 -0
- package/dist/vad.cjs.map +1 -0
- package/dist/vad.js +57 -52
- package/dist/vad.js.map +1 -1
- package/dist/version.cjs +29 -0
- package/dist/version.cjs.map +1 -0
- package/dist/version.js +4 -4
- package/dist/version.js.map +1 -1
- package/dist/worker.cjs +577 -0
- package/dist/worker.cjs.map +1 -0
- package/dist/worker.d.ts +1 -1
- package/dist/worker.d.ts.map +1 -1
- package/dist/worker.js +512 -484
- package/dist/worker.js.map +1 -1
- package/package.json +18 -8
- package/src/ipc/job_main.ts +66 -64
- package/src/job.ts +3 -2
- package/src/pipeline/pipeline_agent.ts +23 -23
- package/src/tokenize/basic/basic.ts +1 -1
- package/src/tokenize/basic/sentence.ts +14 -8
- package/src/tokenize/tokenizer.test.ts +255 -0
- package/src/worker.ts +1 -0
package/dist/llm/chat_context.js
CHANGED
|
@@ -1,93 +1,105 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
})(ChatRole ||
|
|
1
|
+
var ChatRole = /* @__PURE__ */ ((ChatRole2) => {
|
|
2
|
+
ChatRole2[ChatRole2["SYSTEM"] = 0] = "SYSTEM";
|
|
3
|
+
ChatRole2[ChatRole2["USER"] = 1] = "USER";
|
|
4
|
+
ChatRole2[ChatRole2["ASSISTANT"] = 2] = "ASSISTANT";
|
|
5
|
+
ChatRole2[ChatRole2["TOOL"] = 3] = "TOOL";
|
|
6
|
+
return ChatRole2;
|
|
7
|
+
})(ChatRole || {});
|
|
8
8
|
const defaultCreateChatMessage = {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
text: "",
|
|
10
|
+
images: [],
|
|
11
|
+
role: 0 /* SYSTEM */
|
|
12
12
|
};
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
13
|
+
class ChatMessage {
|
|
14
|
+
role;
|
|
15
|
+
id;
|
|
16
|
+
name;
|
|
17
|
+
content;
|
|
18
|
+
toolCalls;
|
|
19
|
+
toolCallId;
|
|
20
|
+
toolException;
|
|
21
|
+
/** @internal */
|
|
22
|
+
constructor({
|
|
23
|
+
role,
|
|
24
|
+
id,
|
|
25
|
+
name,
|
|
26
|
+
content,
|
|
27
|
+
toolCalls,
|
|
28
|
+
toolCallId,
|
|
29
|
+
toolException
|
|
30
|
+
}) {
|
|
31
|
+
this.role = role;
|
|
32
|
+
this.id = id;
|
|
33
|
+
this.name = name;
|
|
34
|
+
this.content = content;
|
|
35
|
+
this.toolCalls = toolCalls;
|
|
36
|
+
this.toolCallId = toolCallId;
|
|
37
|
+
this.toolException = toolException;
|
|
38
|
+
}
|
|
39
|
+
static createToolFromFunctionResult(func) {
|
|
40
|
+
if (!func.result && !func.error) {
|
|
41
|
+
throw new TypeError("CallableFunctionResult must include result or error");
|
|
30
42
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
else {
|
|
59
|
-
return new ChatMessage({
|
|
60
|
-
role,
|
|
61
|
-
content: [...(text ? [text] : []), ...images],
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
/** Returns a structured clone of this message. */
|
|
66
|
-
copy() {
|
|
67
|
-
return new ChatMessage({
|
|
68
|
-
role: this.role,
|
|
69
|
-
id: this.id,
|
|
70
|
-
name: this.name,
|
|
71
|
-
content: this.content,
|
|
72
|
-
toolCalls: this.toolCalls,
|
|
73
|
-
toolCallId: this.toolCallId,
|
|
74
|
-
toolException: this.toolException,
|
|
75
|
-
});
|
|
43
|
+
return new ChatMessage({
|
|
44
|
+
role: 3 /* TOOL */,
|
|
45
|
+
name: func.name,
|
|
46
|
+
content: func.result || `Error: ${func.error}`,
|
|
47
|
+
toolCallId: func.toolCallId,
|
|
48
|
+
toolException: func.error
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
static createToolCalls(toolCalls, text = "") {
|
|
52
|
+
return new ChatMessage({
|
|
53
|
+
role: 2 /* ASSISTANT */,
|
|
54
|
+
toolCalls,
|
|
55
|
+
content: text
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
static create(options) {
|
|
59
|
+
const { text, images, role } = { ...defaultCreateChatMessage, ...options };
|
|
60
|
+
if (!images.length) {
|
|
61
|
+
return new ChatMessage({
|
|
62
|
+
role,
|
|
63
|
+
content: text
|
|
64
|
+
});
|
|
65
|
+
} else {
|
|
66
|
+
return new ChatMessage({
|
|
67
|
+
role,
|
|
68
|
+
content: [...text ? [text] : [], ...images]
|
|
69
|
+
});
|
|
76
70
|
}
|
|
71
|
+
}
|
|
72
|
+
/** Returns a structured clone of this message. */
|
|
73
|
+
copy() {
|
|
74
|
+
return new ChatMessage({
|
|
75
|
+
role: this.role,
|
|
76
|
+
id: this.id,
|
|
77
|
+
name: this.name,
|
|
78
|
+
content: this.content,
|
|
79
|
+
toolCalls: this.toolCalls,
|
|
80
|
+
toolCallId: this.toolCallId,
|
|
81
|
+
toolException: this.toolException
|
|
82
|
+
});
|
|
83
|
+
}
|
|
77
84
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
85
|
+
class ChatContext {
|
|
86
|
+
messages = [];
|
|
87
|
+
metadata = {};
|
|
88
|
+
append(msg) {
|
|
89
|
+
this.messages.push(ChatMessage.create(msg));
|
|
90
|
+
return this;
|
|
91
|
+
}
|
|
92
|
+
/** Returns a structured clone of this context. */
|
|
93
|
+
copy() {
|
|
94
|
+
const ctx = new ChatContext();
|
|
95
|
+
ctx.messages.push(...this.messages.map((msg) => msg.copy()));
|
|
96
|
+
ctx.metadata = structuredClone(this.metadata);
|
|
97
|
+
return ctx;
|
|
98
|
+
}
|
|
92
99
|
}
|
|
100
|
+
export {
|
|
101
|
+
ChatContext,
|
|
102
|
+
ChatMessage,
|
|
103
|
+
ChatRole
|
|
104
|
+
};
|
|
93
105
|
//# sourceMappingURL=chat_context.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"sources":["../../src/llm/chat_context.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { AudioFrame, VideoFrame } from '@livekit/rtc-node';\nimport type { CallableFunctionResult, FunctionCallInfo } from './function_context.js';\n\nexport enum ChatRole {\n SYSTEM,\n USER,\n ASSISTANT,\n TOOL,\n}\n\nexport interface ChatImage {\n image: string | VideoFrame;\n inferenceWidth?: number;\n inferenceHeight?: number;\n /**\n * @internal\n * Used by LLM implementations to store a processed version of the image for later use.\n */\n cache: { [id: string | number | symbol]: any };\n}\n\nexport interface ChatAudio {\n frame: AudioFrame | AudioFrame[];\n}\n\nexport type ChatContent = string | ChatImage | ChatAudio;\n\nconst defaultCreateChatMessage = {\n text: '',\n images: [],\n role: ChatRole.SYSTEM,\n};\n\nexport class ChatMessage {\n readonly role: ChatRole;\n readonly id?: string;\n readonly name?: string;\n readonly content?: ChatContent | ChatContent[];\n readonly toolCalls?: FunctionCallInfo[];\n readonly toolCallId?: string;\n readonly toolException?: Error;\n\n /** @internal */\n constructor({\n role,\n id,\n name,\n content,\n toolCalls,\n toolCallId,\n toolException,\n }: {\n role: ChatRole;\n id?: string;\n name?: string;\n content?: ChatContent | ChatContent[];\n toolCalls?: FunctionCallInfo[];\n toolCallId?: string;\n toolException?: Error;\n }) {\n this.role = role;\n this.id = id;\n this.name = name;\n this.content = content;\n this.toolCalls = toolCalls;\n this.toolCallId = toolCallId;\n this.toolException = toolException;\n }\n\n static createToolFromFunctionResult(func: CallableFunctionResult): ChatMessage {\n if (!func.result && !func.error) {\n throw new TypeError('CallableFunctionResult must include result or error');\n }\n\n return new ChatMessage({\n role: ChatRole.TOOL,\n name: func.name,\n content: func.result || `Error: ${func.error}`,\n toolCallId: func.toolCallId,\n toolException: func.error,\n });\n }\n\n static createToolCalls(toolCalls: FunctionCallInfo[], text = '') {\n return new ChatMessage({\n role: ChatRole.ASSISTANT,\n toolCalls,\n content: text,\n });\n }\n\n static create(\n options: Partial<{\n text?: string;\n images: ChatImage[];\n role: ChatRole;\n }>,\n ): ChatMessage {\n const { text, images, role } = { ...defaultCreateChatMessage, ...options };\n\n if (!images.length) {\n return new ChatMessage({\n role,\n content: text,\n });\n } else {\n return new ChatMessage({\n role,\n content: [...(text ? [text] : []), ...images],\n });\n }\n }\n\n /** Returns a structured clone of this message. */\n copy(): ChatMessage {\n return new ChatMessage({\n role: this.role,\n id: this.id,\n name: this.name,\n content: this.content,\n toolCalls: this.toolCalls,\n toolCallId: this.toolCallId,\n toolException: this.toolException,\n });\n }\n}\n\nexport class ChatContext {\n messages: ChatMessage[] = [];\n metadata: { [id: string]: any } = {};\n\n append(msg: { text?: string; images?: ChatImage[]; role: ChatRole }): ChatContext {\n this.messages.push(ChatMessage.create(msg));\n return this;\n }\n\n /** Returns a structured clone of this context. */\n copy(): ChatContext {\n const ctx = new ChatContext();\n ctx.messages.push(...this.messages.map((msg) => msg.copy()));\n ctx.metadata = structuredClone(this.metadata);\n return ctx;\n }\n}\n"],"mappings":"AAMO,IAAK,WAAL,kBAAKA,cAAL;AACL,EAAAA,oBAAA;AACA,EAAAA,oBAAA;AACA,EAAAA,oBAAA;AACA,EAAAA,oBAAA;AAJU,SAAAA;AAAA,GAAA;AAwBZ,MAAM,2BAA2B;AAAA,EAC/B,MAAM;AAAA,EACN,QAAQ,CAAC;AAAA,EACT,MAAM;AACR;AAEO,MAAM,YAAY;AAAA,EACd;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA;AAAA,EAGT,YAAY;AAAA,IACV;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,GAQG;AACD,SAAK,OAAO;AACZ,SAAK,KAAK;AACV,SAAK,OAAO;AACZ,SAAK,UAAU;AACf,SAAK,YAAY;AACjB,SAAK,aAAa;AAClB,SAAK,gBAAgB;AAAA,EACvB;AAAA,EAEA,OAAO,6BAA6B,MAA2C;AAC7E,QAAI,CAAC,KAAK,UAAU,CAAC,KAAK,OAAO;AAC/B,YAAM,IAAI,UAAU,qDAAqD;AAAA,IAC3E;AAEA,WAAO,IAAI,YAAY;AAAA,MACrB,MAAM;AAAA,MACN,MAAM,KAAK;AAAA,MACX,SAAS,KAAK,UAAU,UAAU,KAAK,KAAK;AAAA,MAC5C,YAAY,KAAK;AAAA,MACjB,eAAe,KAAK;AAAA,IACtB,CAAC;AAAA,EACH;AAAA,EAEA,OAAO,gBAAgB,WAA+B,OAAO,IAAI;AAC/D,WAAO,IAAI,YAAY;AAAA,MACrB,MAAM;AAAA,MACN;AAAA,MACA,SAAS;AAAA,IACX,CAAC;AAAA,EACH;AAAA,EAEA,OAAO,OACL,SAKa;AACb,UAAM,EAAE,MAAM,QAAQ,KAAK,IAAI,EAAE,GAAG,0BAA0B,GAAG,QAAQ;AAEzE,QAAI,CAAC,OAAO,QAAQ;AAClB,aAAO,IAAI,YAAY;AAAA,QACrB;AAAA,QACA,SAAS;AAAA,MACX,CAAC;AAAA,IACH,OAAO;AACL,aAAO,IAAI,YAAY;AAAA,QACrB;AAAA,QACA,SAAS,CAAC,GAAI,OAAO,CAAC,IAAI,IAAI,CAAC,GAAI,GAAG,MAAM;AAAA,MAC9C,CAAC;AAAA,IACH;AAAA,EACF;AAAA;AAAA,EAGA,OAAoB;AAClB,WAAO,IAAI,YAAY;AAAA,MACrB,MAAM,KAAK;AAAA,MACX,IAAI,KAAK;AAAA,MACT,MAAM,KAAK;AAAA,MACX,SAAS,KAAK;AAAA,MACd,WAAW,KAAK;AAAA,MAChB,YAAY,KAAK;AAAA,MACjB,eAAe,KAAK;AAAA,IACtB,CAAC;AAAA,EACH;AACF;AAEO,MAAM,YAAY;AAAA,EACvB,WAA0B,CAAC;AAAA,EAC3B,WAAkC,CAAC;AAAA,EAEnC,OAAO,KAA2E;AAChF,SAAK,SAAS,KAAK,YAAY,OAAO,GAAG,CAAC;AAC1C,WAAO;AAAA,EACT;AAAA;AAAA,EAGA,OAAoB;AAClB,UAAM,MAAM,IAAI,YAAY;AAC5B,QAAI,SAAS,KAAK,GAAG,KAAK,SAAS,IAAI,CAAC,QAAQ,IAAI,KAAK,CAAC,CAAC;AAC3D,QAAI,WAAW,gBAAgB,KAAK,QAAQ;AAC5C,WAAO;AAAA,EACT;AACF;","names":["ChatRole"]}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var function_context_exports = {};
|
|
20
|
+
__export(function_context_exports, {
|
|
21
|
+
oaiBuildFunctionInfo: () => oaiBuildFunctionInfo,
|
|
22
|
+
oaiParams: () => oaiParams
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(function_context_exports);
|
|
25
|
+
var import_zod = require("zod");
|
|
26
|
+
const looksLikeInstanceof = (value, target) => {
|
|
27
|
+
let current = value == null ? void 0 : value.constructor;
|
|
28
|
+
do {
|
|
29
|
+
if ((current == null ? void 0 : current.name) === target.name) return true;
|
|
30
|
+
current = Object.getPrototypeOf(current);
|
|
31
|
+
} while (current == null ? void 0 : current.name);
|
|
32
|
+
return false;
|
|
33
|
+
};
|
|
34
|
+
const oaiParams = (p) => {
|
|
35
|
+
const properties = {};
|
|
36
|
+
const requiredProperties = [];
|
|
37
|
+
const processZodType = (field) => {
|
|
38
|
+
const isOptional = field instanceof import_zod.z.ZodOptional;
|
|
39
|
+
const nestedField = isOptional ? field._def.innerType : field;
|
|
40
|
+
const description = field._def.description;
|
|
41
|
+
if (looksLikeInstanceof(nestedField, import_zod.z.ZodEnum)) {
|
|
42
|
+
return {
|
|
43
|
+
type: typeof nestedField._def.values[0],
|
|
44
|
+
...description && { description },
|
|
45
|
+
enum: nestedField._def.values
|
|
46
|
+
};
|
|
47
|
+
} else if (looksLikeInstanceof(nestedField, import_zod.z.ZodArray)) {
|
|
48
|
+
const elementType = nestedField._def.type;
|
|
49
|
+
return {
|
|
50
|
+
type: "array",
|
|
51
|
+
...description && { description },
|
|
52
|
+
items: processZodType(elementType)
|
|
53
|
+
};
|
|
54
|
+
} else if (looksLikeInstanceof(nestedField, import_zod.z.ZodObject)) {
|
|
55
|
+
const { properties: properties2, required } = oaiParams(nestedField);
|
|
56
|
+
return {
|
|
57
|
+
type: "object",
|
|
58
|
+
...description && { description },
|
|
59
|
+
properties: properties2,
|
|
60
|
+
required
|
|
61
|
+
};
|
|
62
|
+
} else {
|
|
63
|
+
let type2 = nestedField._def.typeName.toLowerCase();
|
|
64
|
+
type2 = type2.includes("zod") ? type2.substring(3) : type2;
|
|
65
|
+
return {
|
|
66
|
+
type: type2,
|
|
67
|
+
...description && { description }
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
for (const key in p.shape) {
|
|
72
|
+
const field = p.shape[key];
|
|
73
|
+
properties[key] = processZodType(field);
|
|
74
|
+
if (!(field instanceof import_zod.z.ZodOptional)) {
|
|
75
|
+
requiredProperties.push(key);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
const type = "object";
|
|
79
|
+
return {
|
|
80
|
+
type,
|
|
81
|
+
properties,
|
|
82
|
+
required: requiredProperties
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
const oaiBuildFunctionInfo = (fncCtx, toolCallId, fncName, rawArgs) => {
|
|
86
|
+
const func = fncCtx[fncName];
|
|
87
|
+
if (!func) {
|
|
88
|
+
throw new Error(`AI function ${fncName} not found`);
|
|
89
|
+
}
|
|
90
|
+
return {
|
|
91
|
+
name: fncName,
|
|
92
|
+
func,
|
|
93
|
+
toolCallId,
|
|
94
|
+
rawParams: rawArgs,
|
|
95
|
+
params: JSON.parse(rawArgs)
|
|
96
|
+
};
|
|
97
|
+
};
|
|
98
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
99
|
+
0 && (module.exports = {
|
|
100
|
+
oaiBuildFunctionInfo,
|
|
101
|
+
oaiParams
|
|
102
|
+
});
|
|
103
|
+
//# sourceMappingURL=function_context.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/llm/function_context.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { z } from 'zod';\n\n// heavily inspired by Vercel AI's `tool()`:\n// https://github.com/vercel/ai/blob/3b0983b/packages/ai/core/tool/tool.ts\n\n/* eslint-disable @typescript-eslint/no-explicit-any */\n\n/** Type reinforcement for the callable function's execute parameters. */\nexport type inferParameters<P extends z.ZodTypeAny> = z.infer<P>;\n\n/** Raw OpenAI-adherent function parameters. */\nexport type OpenAIFunctionParameters = {\n type: 'object';\n properties: { [id: string]: any };\n required: string[];\n additionalProperties: boolean;\n};\n\n/** A definition for a function callable by the LLM. */\nexport interface CallableFunction<P extends z.ZodTypeAny = any, R = any> {\n description: string;\n parameters: OpenAIFunctionParameters | P;\n execute: (args: inferParameters<P>) => PromiseLike<R>;\n}\n\n/** A function that has been called but is not yet running */\nexport interface FunctionCallInfo<P extends z.ZodTypeAny = any, R = any> {\n name: string;\n func: CallableFunction<P, R>;\n toolCallId: string;\n rawParams: string;\n params: inferParameters<P>;\n task?: PromiseLike<CallableFunctionResult>;\n}\n\n/** The result of a ran FunctionCallInfo. */\nexport interface CallableFunctionResult {\n name: string;\n toolCallId: string;\n result?: any;\n error?: any;\n}\n\n/** An object containing callable functions and their names */\nexport type FunctionContext = {\n [name: string]: CallableFunction;\n};\n\n// XXX: Zod is victim to the dual-package hazard. this is a hacky sorta-fix\n// until Zod v4.0.0 is released.\n// https://github.com/colinhacks/zod/issues/2241#issuecomment-2142688925\nconst looksLikeInstanceof = <T>(value: unknown, target: new (...args: any[]) => T): value is T => {\n let current = value?.constructor;\n do {\n if (current?.name === target.name) return true;\n // eslint-disable-next-line @typescript-eslint/ban-types\n current = Object.getPrototypeOf(current) as Function;\n } while (current?.name);\n return false;\n};\n\n/** @internal */\nexport const oaiParams = (p: z.AnyZodObject) => {\n const properties: Record<string, any> = {};\n const requiredProperties: string[] = [];\n\n const processZodType = (field: z.ZodTypeAny): any => {\n const isOptional = field instanceof z.ZodOptional;\n const nestedField = isOptional ? field._def.innerType : field;\n const description = field._def.description;\n\n if (looksLikeInstanceof(nestedField, z.ZodEnum)) {\n return {\n type: typeof nestedField._def.values[0],\n ...(description && { description }),\n enum: nestedField._def.values,\n };\n } else if (looksLikeInstanceof(nestedField, z.ZodArray)) {\n const elementType = nestedField._def.type;\n return {\n type: 'array',\n ...(description && { description }),\n items: processZodType(elementType),\n };\n } else if (looksLikeInstanceof(nestedField, z.ZodObject)) {\n const { properties, required } = oaiParams(nestedField);\n return {\n type: 'object',\n ...(description && { description }),\n properties,\n required,\n };\n } else {\n let type = nestedField._def.typeName.toLowerCase();\n type = type.includes('zod') ? type.substring(3) : type;\n return {\n type,\n ...(description && { description }),\n };\n }\n };\n\n for (const key in p.shape) {\n const field = p.shape[key];\n properties[key] = processZodType(field);\n\n if (!(field instanceof z.ZodOptional)) {\n requiredProperties.push(key);\n }\n }\n\n const type = 'object' as const;\n return {\n type,\n properties,\n required: requiredProperties,\n };\n};\n\n/** @internal */\nexport const oaiBuildFunctionInfo = (\n fncCtx: FunctionContext,\n toolCallId: string,\n fncName: string,\n rawArgs: string,\n): FunctionCallInfo => {\n const func = fncCtx[fncName];\n if (!func) {\n throw new Error(`AI function ${fncName} not found`);\n }\n\n return {\n name: fncName,\n func,\n toolCallId,\n rawParams: rawArgs,\n params: JSON.parse(rawArgs),\n };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,iBAAkB;AAmDlB,MAAM,sBAAsB,CAAI,OAAgB,WAAkD;AAChG,MAAI,UAAU,+BAAO;AACrB,KAAG;AACD,SAAI,mCAAS,UAAS,OAAO,KAAM,QAAO;AAE1C,cAAU,OAAO,eAAe,OAAO;AAAA,EACzC,SAAS,mCAAS;AAClB,SAAO;AACT;AAGO,MAAM,YAAY,CAAC,MAAsB;AAC9C,QAAM,aAAkC,CAAC;AACzC,QAAM,qBAA+B,CAAC;AAEtC,QAAM,iBAAiB,CAAC,UAA6B;AACnD,UAAM,aAAa,iBAAiB,aAAE;AACtC,UAAM,cAAc,aAAa,MAAM,KAAK,YAAY;AACxD,UAAM,cAAc,MAAM,KAAK;AAE/B,QAAI,oBAAoB,aAAa,aAAE,OAAO,GAAG;AAC/C,aAAO;AAAA,QACL,MAAM,OAAO,YAAY,KAAK,OAAO,CAAC;AAAA,QACtC,GAAI,eAAe,EAAE,YAAY;AAAA,QACjC,MAAM,YAAY,KAAK;AAAA,MACzB;AAAA,IACF,WAAW,oBAAoB,aAAa,aAAE,QAAQ,GAAG;AACvD,YAAM,cAAc,YAAY,KAAK;AACrC,aAAO;AAAA,QACL,MAAM;AAAA,QACN,GAAI,eAAe,EAAE,YAAY;AAAA,QACjC,OAAO,eAAe,WAAW;AAAA,MACnC;AAAA,IACF,WAAW,oBAAoB,aAAa,aAAE,SAAS,GAAG;AACxD,YAAM,EAAE,YAAAA,aAAY,SAAS,IAAI,UAAU,WAAW;AACtD,aAAO;AAAA,QACL,MAAM;AAAA,QACN,GAAI,eAAe,EAAE,YAAY;AAAA,QACjC,YAAAA;AAAA,QACA;AAAA,MACF;AAAA,IACF,OAAO;AACL,UAAIC,QAAO,YAAY,KAAK,SAAS,YAAY;AACjD,MAAAA,QAAOA,MAAK,SAAS,KAAK,IAAIA,MAAK,UAAU,CAAC,IAAIA;AAClD,aAAO;AAAA,QACL,MAAAA;AAAA,QACA,GAAI,eAAe,EAAE,YAAY;AAAA,MACnC;AAAA,IACF;AAAA,EACF;AAEA,aAAW,OAAO,EAAE,OAAO;AACzB,UAAM,QAAQ,EAAE,MAAM,GAAG;AACzB,eAAW,GAAG,IAAI,eAAe,KAAK;AAEtC,QAAI,EAAE,iBAAiB,aAAE,cAAc;AACrC,yBAAmB,KAAK,GAAG;AAAA,IAC7B;AAAA,EACF;AAEA,QAAM,OAAO;AACb,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA,UAAU;AAAA,EACZ;AACF;AAGO,MAAM,uBAAuB,CAClC,QACA,YACA,SACA,YACqB;AACrB,QAAM,OAAO,OAAO,OAAO;AAC3B,MAAI,CAAC,MAAM;AACT,UAAM,IAAI,MAAM,eAAe,OAAO,YAAY;AAAA,EACpD;AAEA,SAAO;AAAA,IACL,MAAM;AAAA,IACN;AAAA,IACA;AAAA,IACA,WAAW;AAAA,IACX,QAAQ,KAAK,MAAM,OAAO;AAAA,EAC5B;AACF;","names":["properties","type"]}
|
|
@@ -1,87 +1,78 @@
|
|
|
1
|
-
|
|
2
|
-
//
|
|
3
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
-
import { z } from 'zod';
|
|
5
|
-
// XXX: Zod is victim to the dual-package hazard. this is a hacky sorta-fix
|
|
6
|
-
// until Zod v4.0.0 is released.
|
|
7
|
-
// https://github.com/colinhacks/zod/issues/2241#issuecomment-2142688925
|
|
1
|
+
import { z } from "zod";
|
|
8
2
|
const looksLikeInstanceof = (value, target) => {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
} while (current?.name);
|
|
16
|
-
return false;
|
|
3
|
+
let current = value == null ? void 0 : value.constructor;
|
|
4
|
+
do {
|
|
5
|
+
if ((current == null ? void 0 : current.name) === target.name) return true;
|
|
6
|
+
current = Object.getPrototypeOf(current);
|
|
7
|
+
} while (current == null ? void 0 : current.name);
|
|
8
|
+
return false;
|
|
17
9
|
};
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
return {
|
|
54
|
-
type,
|
|
55
|
-
...(description && { description }),
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
};
|
|
59
|
-
for (const key in p.shape) {
|
|
60
|
-
const field = p.shape[key];
|
|
61
|
-
properties[key] = processZodType(field);
|
|
62
|
-
if (!(field instanceof z.ZodOptional)) {
|
|
63
|
-
requiredProperties.push(key);
|
|
64
|
-
}
|
|
10
|
+
const oaiParams = (p) => {
|
|
11
|
+
const properties = {};
|
|
12
|
+
const requiredProperties = [];
|
|
13
|
+
const processZodType = (field) => {
|
|
14
|
+
const isOptional = field instanceof z.ZodOptional;
|
|
15
|
+
const nestedField = isOptional ? field._def.innerType : field;
|
|
16
|
+
const description = field._def.description;
|
|
17
|
+
if (looksLikeInstanceof(nestedField, z.ZodEnum)) {
|
|
18
|
+
return {
|
|
19
|
+
type: typeof nestedField._def.values[0],
|
|
20
|
+
...description && { description },
|
|
21
|
+
enum: nestedField._def.values
|
|
22
|
+
};
|
|
23
|
+
} else if (looksLikeInstanceof(nestedField, z.ZodArray)) {
|
|
24
|
+
const elementType = nestedField._def.type;
|
|
25
|
+
return {
|
|
26
|
+
type: "array",
|
|
27
|
+
...description && { description },
|
|
28
|
+
items: processZodType(elementType)
|
|
29
|
+
};
|
|
30
|
+
} else if (looksLikeInstanceof(nestedField, z.ZodObject)) {
|
|
31
|
+
const { properties: properties2, required } = oaiParams(nestedField);
|
|
32
|
+
return {
|
|
33
|
+
type: "object",
|
|
34
|
+
...description && { description },
|
|
35
|
+
properties: properties2,
|
|
36
|
+
required
|
|
37
|
+
};
|
|
38
|
+
} else {
|
|
39
|
+
let type2 = nestedField._def.typeName.toLowerCase();
|
|
40
|
+
type2 = type2.includes("zod") ? type2.substring(3) : type2;
|
|
41
|
+
return {
|
|
42
|
+
type: type2,
|
|
43
|
+
...description && { description }
|
|
44
|
+
};
|
|
65
45
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
};
|
|
73
|
-
/** @internal */
|
|
74
|
-
export const oaiBuildFunctionInfo = (fncCtx, toolCallId, fncName, rawArgs) => {
|
|
75
|
-
const func = fncCtx[fncName];
|
|
76
|
-
if (!func) {
|
|
77
|
-
throw new Error(`AI function ${fncName} not found`);
|
|
46
|
+
};
|
|
47
|
+
for (const key in p.shape) {
|
|
48
|
+
const field = p.shape[key];
|
|
49
|
+
properties[key] = processZodType(field);
|
|
50
|
+
if (!(field instanceof z.ZodOptional)) {
|
|
51
|
+
requiredProperties.push(key);
|
|
78
52
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
53
|
+
}
|
|
54
|
+
const type = "object";
|
|
55
|
+
return {
|
|
56
|
+
type,
|
|
57
|
+
properties,
|
|
58
|
+
required: requiredProperties
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
const oaiBuildFunctionInfo = (fncCtx, toolCallId, fncName, rawArgs) => {
|
|
62
|
+
const func = fncCtx[fncName];
|
|
63
|
+
if (!func) {
|
|
64
|
+
throw new Error(`AI function ${fncName} not found`);
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
name: fncName,
|
|
68
|
+
func,
|
|
69
|
+
toolCallId,
|
|
70
|
+
rawParams: rawArgs,
|
|
71
|
+
params: JSON.parse(rawArgs)
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
|
+
export {
|
|
75
|
+
oaiBuildFunctionInfo,
|
|
76
|
+
oaiParams
|
|
86
77
|
};
|
|
87
78
|
//# sourceMappingURL=function_context.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"sources":["../../src/llm/function_context.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { z } from 'zod';\n\n// heavily inspired by Vercel AI's `tool()`:\n// https://github.com/vercel/ai/blob/3b0983b/packages/ai/core/tool/tool.ts\n\n/* eslint-disable @typescript-eslint/no-explicit-any */\n\n/** Type reinforcement for the callable function's execute parameters. */\nexport type inferParameters<P extends z.ZodTypeAny> = z.infer<P>;\n\n/** Raw OpenAI-adherent function parameters. */\nexport type OpenAIFunctionParameters = {\n type: 'object';\n properties: { [id: string]: any };\n required: string[];\n additionalProperties: boolean;\n};\n\n/** A definition for a function callable by the LLM. */\nexport interface CallableFunction<P extends z.ZodTypeAny = any, R = any> {\n description: string;\n parameters: OpenAIFunctionParameters | P;\n execute: (args: inferParameters<P>) => PromiseLike<R>;\n}\n\n/** A function that has been called but is not yet running */\nexport interface FunctionCallInfo<P extends z.ZodTypeAny = any, R = any> {\n name: string;\n func: CallableFunction<P, R>;\n toolCallId: string;\n rawParams: string;\n params: inferParameters<P>;\n task?: PromiseLike<CallableFunctionResult>;\n}\n\n/** The result of a ran FunctionCallInfo. */\nexport interface CallableFunctionResult {\n name: string;\n toolCallId: string;\n result?: any;\n error?: any;\n}\n\n/** An object containing callable functions and their names */\nexport type FunctionContext = {\n [name: string]: CallableFunction;\n};\n\n// XXX: Zod is victim to the dual-package hazard. this is a hacky sorta-fix\n// until Zod v4.0.0 is released.\n// https://github.com/colinhacks/zod/issues/2241#issuecomment-2142688925\nconst looksLikeInstanceof = <T>(value: unknown, target: new (...args: any[]) => T): value is T => {\n let current = value?.constructor;\n do {\n if (current?.name === target.name) return true;\n // eslint-disable-next-line @typescript-eslint/ban-types\n current = Object.getPrototypeOf(current) as Function;\n } while (current?.name);\n return false;\n};\n\n/** @internal */\nexport const oaiParams = (p: z.AnyZodObject) => {\n const properties: Record<string, any> = {};\n const requiredProperties: string[] = [];\n\n const processZodType = (field: z.ZodTypeAny): any => {\n const isOptional = field instanceof z.ZodOptional;\n const nestedField = isOptional ? field._def.innerType : field;\n const description = field._def.description;\n\n if (looksLikeInstanceof(nestedField, z.ZodEnum)) {\n return {\n type: typeof nestedField._def.values[0],\n ...(description && { description }),\n enum: nestedField._def.values,\n };\n } else if (looksLikeInstanceof(nestedField, z.ZodArray)) {\n const elementType = nestedField._def.type;\n return {\n type: 'array',\n ...(description && { description }),\n items: processZodType(elementType),\n };\n } else if (looksLikeInstanceof(nestedField, z.ZodObject)) {\n const { properties, required } = oaiParams(nestedField);\n return {\n type: 'object',\n ...(description && { description }),\n properties,\n required,\n };\n } else {\n let type = nestedField._def.typeName.toLowerCase();\n type = type.includes('zod') ? type.substring(3) : type;\n return {\n type,\n ...(description && { description }),\n };\n }\n };\n\n for (const key in p.shape) {\n const field = p.shape[key];\n properties[key] = processZodType(field);\n\n if (!(field instanceof z.ZodOptional)) {\n requiredProperties.push(key);\n }\n }\n\n const type = 'object' as const;\n return {\n type,\n properties,\n required: requiredProperties,\n };\n};\n\n/** @internal */\nexport const oaiBuildFunctionInfo = (\n fncCtx: FunctionContext,\n toolCallId: string,\n fncName: string,\n rawArgs: string,\n): FunctionCallInfo => {\n const func = fncCtx[fncName];\n if (!func) {\n throw new Error(`AI function ${fncName} not found`);\n }\n\n return {\n name: fncName,\n func,\n toolCallId,\n rawParams: rawArgs,\n params: JSON.parse(rawArgs),\n };\n};\n"],"mappings":"AAGA,SAAS,SAAS;AAmDlB,MAAM,sBAAsB,CAAI,OAAgB,WAAkD;AAChG,MAAI,UAAU,+BAAO;AACrB,KAAG;AACD,SAAI,mCAAS,UAAS,OAAO,KAAM,QAAO;AAE1C,cAAU,OAAO,eAAe,OAAO;AAAA,EACzC,SAAS,mCAAS;AAClB,SAAO;AACT;AAGO,MAAM,YAAY,CAAC,MAAsB;AAC9C,QAAM,aAAkC,CAAC;AACzC,QAAM,qBAA+B,CAAC;AAEtC,QAAM,iBAAiB,CAAC,UAA6B;AACnD,UAAM,aAAa,iBAAiB,EAAE;AACtC,UAAM,cAAc,aAAa,MAAM,KAAK,YAAY;AACxD,UAAM,cAAc,MAAM,KAAK;AAE/B,QAAI,oBAAoB,aAAa,EAAE,OAAO,GAAG;AAC/C,aAAO;AAAA,QACL,MAAM,OAAO,YAAY,KAAK,OAAO,CAAC;AAAA,QACtC,GAAI,eAAe,EAAE,YAAY;AAAA,QACjC,MAAM,YAAY,KAAK;AAAA,MACzB;AAAA,IACF,WAAW,oBAAoB,aAAa,EAAE,QAAQ,GAAG;AACvD,YAAM,cAAc,YAAY,KAAK;AACrC,aAAO;AAAA,QACL,MAAM;AAAA,QACN,GAAI,eAAe,EAAE,YAAY;AAAA,QACjC,OAAO,eAAe,WAAW;AAAA,MACnC;AAAA,IACF,WAAW,oBAAoB,aAAa,EAAE,SAAS,GAAG;AACxD,YAAM,EAAE,YAAAA,aAAY,SAAS,IAAI,UAAU,WAAW;AACtD,aAAO;AAAA,QACL,MAAM;AAAA,QACN,GAAI,eAAe,EAAE,YAAY;AAAA,QACjC,YAAAA;AAAA,QACA;AAAA,MACF;AAAA,IACF,OAAO;AACL,UAAIC,QAAO,YAAY,KAAK,SAAS,YAAY;AACjD,MAAAA,QAAOA,MAAK,SAAS,KAAK,IAAIA,MAAK,UAAU,CAAC,IAAIA;AAClD,aAAO;AAAA,QACL,MAAAA;AAAA,QACA,GAAI,eAAe,EAAE,YAAY;AAAA,MACnC;AAAA,IACF;AAAA,EACF;AAEA,aAAW,OAAO,EAAE,OAAO;AACzB,UAAM,QAAQ,EAAE,MAAM,GAAG;AACzB,eAAW,GAAG,IAAI,eAAe,KAAK;AAEtC,QAAI,EAAE,iBAAiB,EAAE,cAAc;AACrC,yBAAmB,KAAK,GAAG;AAAA,IAC7B;AAAA,EACF;AAEA,QAAM,OAAO;AACb,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA,UAAU;AAAA,EACZ;AACF;AAGO,MAAM,uBAAuB,CAClC,QACA,YACA,SACA,YACqB;AACrB,QAAM,OAAO,OAAO,OAAO;AAC3B,MAAI,CAAC,MAAM;AACT,UAAM,IAAI,MAAM,eAAe,OAAO,YAAY;AAAA,EACpD;AAEA,SAAO;AAAA,IACL,MAAM;AAAA,IACN;AAAA,IACA;AAAA,IACA,WAAW;AAAA,IACX,QAAQ,KAAK,MAAM,OAAO;AAAA,EAC5B;AACF;","names":["properties","type"]}
|