@livekit/agents 0.4.5 → 0.5.0

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.
Files changed (209) hide show
  1. package/README.md +17 -0
  2. package/dist/audio.cjs +77 -0
  3. package/dist/audio.cjs.map +1 -0
  4. package/dist/audio.js +48 -37
  5. package/dist/audio.js.map +1 -1
  6. package/dist/cli.cjs +131 -0
  7. package/dist/cli.cjs.map +1 -0
  8. package/dist/cli.js +96 -122
  9. package/dist/cli.js.map +1 -1
  10. package/dist/generator.cjs +36 -0
  11. package/dist/generator.cjs.map +1 -0
  12. package/dist/generator.js +8 -22
  13. package/dist/generator.js.map +1 -1
  14. package/dist/http_server.cjs +72 -0
  15. package/dist/http_server.cjs.map +1 -0
  16. package/dist/http_server.d.ts +1 -1
  17. package/dist/http_server.js +44 -47
  18. package/dist/http_server.js.map +1 -1
  19. package/dist/index.cjs +78 -0
  20. package/dist/index.cjs.map +1 -0
  21. package/dist/index.js +26 -28
  22. package/dist/index.js.map +1 -1
  23. package/dist/ipc/job_executor.cjs +33 -0
  24. package/dist/ipc/job_executor.cjs.map +1 -0
  25. package/dist/ipc/job_executor.js +7 -4
  26. package/dist/ipc/job_executor.js.map +1 -1
  27. package/dist/ipc/job_main.cjs +147 -0
  28. package/dist/ipc/job_main.cjs.map +1 -0
  29. package/dist/ipc/job_main.d.ts +1 -1
  30. package/dist/ipc/job_main.js +103 -103
  31. package/dist/ipc/job_main.js.map +1 -1
  32. package/dist/ipc/message.cjs +17 -0
  33. package/dist/ipc/message.cjs.map +1 -0
  34. package/dist/ipc/message.js +0 -1
  35. package/dist/ipc/message.js.map +1 -1
  36. package/dist/ipc/proc_job_executor.cjs +174 -0
  37. package/dist/ipc/proc_job_executor.cjs.map +1 -0
  38. package/dist/ipc/proc_job_executor.js +130 -126
  39. package/dist/ipc/proc_job_executor.js.map +1 -1
  40. package/dist/ipc/proc_pool.cjs +126 -0
  41. package/dist/ipc/proc_pool.cjs.map +1 -0
  42. package/dist/ipc/proc_pool.js +93 -96
  43. package/dist/ipc/proc_pool.js.map +1 -1
  44. package/dist/job.cjs +230 -0
  45. package/dist/job.cjs.map +1 -0
  46. package/dist/job.js +195 -198
  47. package/dist/job.js.map +1 -1
  48. package/dist/llm/chat_context.cjs +131 -0
  49. package/dist/llm/chat_context.cjs.map +1 -0
  50. package/dist/llm/chat_context.js +98 -86
  51. package/dist/llm/chat_context.js.map +1 -1
  52. package/dist/llm/function_context.cjs +103 -0
  53. package/dist/llm/function_context.cjs.map +1 -0
  54. package/dist/llm/function_context.js +72 -81
  55. package/dist/llm/function_context.js.map +1 -1
  56. package/dist/llm/function_context.test.cjs +218 -0
  57. package/dist/llm/function_context.test.cjs.map +1 -0
  58. package/dist/llm/function_context.test.js +209 -210
  59. package/dist/llm/function_context.test.js.map +1 -1
  60. package/dist/llm/index.cjs +43 -0
  61. package/dist/llm/index.cjs.map +1 -0
  62. package/dist/llm/index.js +22 -6
  63. package/dist/llm/index.js.map +1 -1
  64. package/dist/llm/llm.cjs +76 -0
  65. package/dist/llm/llm.cjs.map +1 -0
  66. package/dist/llm/llm.js +48 -42
  67. package/dist/llm/llm.js.map +1 -1
  68. package/dist/log.cjs +57 -0
  69. package/dist/log.cjs.map +1 -0
  70. package/dist/log.js +27 -26
  71. package/dist/log.js.map +1 -1
  72. package/dist/multimodal/agent_playout.cjs +228 -0
  73. package/dist/multimodal/agent_playout.cjs.map +1 -0
  74. package/dist/multimodal/agent_playout.d.ts +1 -1
  75. package/dist/multimodal/agent_playout.js +193 -180
  76. package/dist/multimodal/agent_playout.js.map +1 -1
  77. package/dist/multimodal/index.cjs +25 -0
  78. package/dist/multimodal/index.cjs.map +1 -0
  79. package/dist/multimodal/index.js +2 -5
  80. package/dist/multimodal/index.js.map +1 -1
  81. package/dist/multimodal/multimodal_agent.cjs +404 -0
  82. package/dist/multimodal/multimodal_agent.cjs.map +1 -0
  83. package/dist/multimodal/multimodal_agent.d.ts +2 -2
  84. package/dist/multimodal/multimodal_agent.d.ts.map +1 -1
  85. package/dist/multimodal/multimodal_agent.js +351 -303
  86. package/dist/multimodal/multimodal_agent.js.map +1 -1
  87. package/dist/pipeline/agent_output.cjs +172 -0
  88. package/dist/pipeline/agent_output.cjs.map +1 -0
  89. package/dist/pipeline/agent_output.js +136 -138
  90. package/dist/pipeline/agent_output.js.map +1 -1
  91. package/dist/pipeline/agent_playout.cjs +169 -0
  92. package/dist/pipeline/agent_playout.cjs.map +1 -0
  93. package/dist/pipeline/agent_playout.js +126 -136
  94. package/dist/pipeline/agent_playout.js.map +1 -1
  95. package/dist/pipeline/human_input.cjs +158 -0
  96. package/dist/pipeline/human_input.cjs.map +1 -0
  97. package/dist/pipeline/human_input.js +124 -125
  98. package/dist/pipeline/human_input.js.map +1 -1
  99. package/dist/pipeline/index.cjs +31 -0
  100. package/dist/pipeline/index.cjs.map +1 -0
  101. package/dist/pipeline/index.js +8 -4
  102. package/dist/pipeline/index.js.map +1 -1
  103. package/dist/pipeline/pipeline_agent.cjs +642 -0
  104. package/dist/pipeline/pipeline_agent.cjs.map +1 -0
  105. package/dist/pipeline/pipeline_agent.d.ts +1 -0
  106. package/dist/pipeline/pipeline_agent.d.ts.map +1 -1
  107. package/dist/pipeline/pipeline_agent.js +595 -650
  108. package/dist/pipeline/pipeline_agent.js.map +1 -1
  109. package/dist/pipeline/speech_handle.cjs +128 -0
  110. package/dist/pipeline/speech_handle.cjs.map +1 -0
  111. package/dist/pipeline/speech_handle.js +102 -100
  112. package/dist/pipeline/speech_handle.js.map +1 -1
  113. package/dist/plugin.cjs +46 -0
  114. package/dist/plugin.cjs.map +1 -0
  115. package/dist/plugin.js +20 -20
  116. package/dist/plugin.js.map +1 -1
  117. package/dist/stt/index.cjs +38 -0
  118. package/dist/stt/index.cjs.map +1 -0
  119. package/dist/stt/index.js +13 -5
  120. package/dist/stt/index.js.map +1 -1
  121. package/dist/stt/stream_adapter.cjs +87 -0
  122. package/dist/stt/stream_adapter.cjs.map +1 -0
  123. package/dist/stt/stream_adapter.js +58 -55
  124. package/dist/stt/stream_adapter.js.map +1 -1
  125. package/dist/stt/stt.cjs +98 -0
  126. package/dist/stt/stt.cjs.map +1 -0
  127. package/dist/stt/stt.js +63 -98
  128. package/dist/stt/stt.js.map +1 -1
  129. package/dist/tokenize/basic/basic.cjs +98 -0
  130. package/dist/tokenize/basic/basic.cjs.map +1 -0
  131. package/dist/tokenize/basic/basic.js +56 -45
  132. package/dist/tokenize/basic/basic.js.map +1 -1
  133. package/dist/tokenize/basic/hyphenator.cjs +425 -0
  134. package/dist/tokenize/basic/hyphenator.cjs.map +1 -0
  135. package/dist/tokenize/basic/hyphenator.js +66 -82
  136. package/dist/tokenize/basic/hyphenator.js.map +1 -1
  137. package/dist/tokenize/basic/index.cjs +35 -0
  138. package/dist/tokenize/basic/index.cjs.map +1 -0
  139. package/dist/tokenize/basic/index.js +7 -4
  140. package/dist/tokenize/basic/index.js.map +1 -1
  141. package/dist/tokenize/basic/paragraph.cjs +57 -0
  142. package/dist/tokenize/basic/paragraph.cjs.map +1 -0
  143. package/dist/tokenize/basic/paragraph.js +30 -35
  144. package/dist/tokenize/basic/paragraph.js.map +1 -1
  145. package/dist/tokenize/basic/sentence.cjs +83 -0
  146. package/dist/tokenize/basic/sentence.cjs.map +1 -0
  147. package/dist/tokenize/basic/sentence.js +56 -57
  148. package/dist/tokenize/basic/sentence.js.map +1 -1
  149. package/dist/tokenize/basic/word.cjs +44 -0
  150. package/dist/tokenize/basic/word.cjs.map +1 -0
  151. package/dist/tokenize/basic/word.js +17 -20
  152. package/dist/tokenize/basic/word.js.map +1 -1
  153. package/dist/tokenize/index.cjs +55 -0
  154. package/dist/tokenize/index.cjs.map +1 -0
  155. package/dist/tokenize/index.js +18 -7
  156. package/dist/tokenize/index.js.map +1 -1
  157. package/dist/tokenize/token_stream.cjs +164 -0
  158. package/dist/tokenize/token_stream.cjs.map +1 -0
  159. package/dist/tokenize/token_stream.js +133 -139
  160. package/dist/tokenize/token_stream.js.map +1 -1
  161. package/dist/tokenize/tokenizer.cjs +184 -0
  162. package/dist/tokenize/tokenizer.cjs.map +1 -0
  163. package/dist/tokenize/tokenizer.js +138 -99
  164. package/dist/tokenize/tokenizer.js.map +1 -1
  165. package/dist/transcription.cjs +131 -0
  166. package/dist/transcription.cjs.map +1 -0
  167. package/dist/transcription.d.ts +2 -0
  168. package/dist/transcription.d.ts.map +1 -1
  169. package/dist/transcription.js +99 -93
  170. package/dist/transcription.js.map +1 -1
  171. package/dist/tts/index.cjs +38 -0
  172. package/dist/tts/index.cjs.map +1 -0
  173. package/dist/tts/index.js +13 -5
  174. package/dist/tts/index.js.map +1 -1
  175. package/dist/tts/stream_adapter.cjs +78 -0
  176. package/dist/tts/stream_adapter.cjs.map +1 -0
  177. package/dist/tts/stream_adapter.js +50 -47
  178. package/dist/tts/stream_adapter.js.map +1 -1
  179. package/dist/tts/tts.cjs +127 -0
  180. package/dist/tts/tts.cjs.map +1 -0
  181. package/dist/tts/tts.js +90 -120
  182. package/dist/tts/tts.js.map +1 -1
  183. package/dist/utils.cjs +284 -0
  184. package/dist/utils.cjs.map +1 -0
  185. package/dist/utils.js +242 -247
  186. package/dist/utils.js.map +1 -1
  187. package/dist/vad.cjs +92 -0
  188. package/dist/vad.cjs.map +1 -0
  189. package/dist/vad.js +57 -52
  190. package/dist/vad.js.map +1 -1
  191. package/dist/version.cjs +29 -0
  192. package/dist/version.cjs.map +1 -0
  193. package/dist/version.js +4 -4
  194. package/dist/version.js.map +1 -1
  195. package/dist/worker.cjs +576 -0
  196. package/dist/worker.cjs.map +1 -0
  197. package/dist/worker.d.ts +1 -1
  198. package/dist/worker.js +511 -484
  199. package/dist/worker.js.map +1 -1
  200. package/package.json +23 -7
  201. package/src/ipc/job_main.ts +66 -64
  202. package/src/multimodal/multimodal_agent.ts +29 -2
  203. package/src/pipeline/pipeline_agent.ts +25 -24
  204. package/src/transcription.ts +5 -0
  205. package/.turbo/turbo-build.log +0 -4
  206. package/CHANGELOG.md +0 -165
  207. package/api-extractor.json +0 -20
  208. package/tsconfig.json +0 -16
  209. package/tsconfig.tsbuildinfo +0 -1
@@ -1,93 +1,105 @@
1
- export var ChatRole;
2
- (function (ChatRole) {
3
- ChatRole[ChatRole["SYSTEM"] = 0] = "SYSTEM";
4
- ChatRole[ChatRole["USER"] = 1] = "USER";
5
- ChatRole[ChatRole["ASSISTANT"] = 2] = "ASSISTANT";
6
- ChatRole[ChatRole["TOOL"] = 3] = "TOOL";
7
- })(ChatRole || (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
- text: '',
10
- images: [],
11
- role: ChatRole.SYSTEM,
9
+ text: "",
10
+ images: [],
11
+ role: 0 /* SYSTEM */
12
12
  };
13
- export class ChatMessage {
14
- role;
15
- id;
16
- name;
17
- content;
18
- toolCalls;
19
- toolCallId;
20
- toolException;
21
- /** @internal */
22
- constructor({ role, id, name, content, toolCalls, toolCallId, toolException, }) {
23
- this.role = role;
24
- this.id = id;
25
- this.name = name;
26
- this.content = content;
27
- this.toolCalls = toolCalls;
28
- this.toolCallId = toolCallId;
29
- this.toolException = toolException;
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
- static createToolFromFunctionResult(func) {
32
- if (!func.result && !func.error) {
33
- throw new TypeError('CallableFunctionResult must include result or error');
34
- }
35
- return new ChatMessage({
36
- role: ChatRole.TOOL,
37
- name: func.name,
38
- content: func.result || `Error: ${func.error}`,
39
- toolCallId: func.toolCallId,
40
- toolException: func.error,
41
- });
42
- }
43
- static createToolCalls(toolCalls, text = '') {
44
- return new ChatMessage({
45
- role: ChatRole.ASSISTANT,
46
- toolCalls,
47
- content: text,
48
- });
49
- }
50
- static create(options) {
51
- const { text, images, role } = { ...defaultCreateChatMessage, ...options };
52
- if (!images.length) {
53
- return new ChatMessage({
54
- role,
55
- content: text,
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
- export class ChatContext {
79
- messages = [];
80
- metadata = {};
81
- append(msg) {
82
- this.messages.push(ChatMessage.create(msg));
83
- return this;
84
- }
85
- /** Returns a structured clone of this context. */
86
- copy() {
87
- const ctx = new ChatContext();
88
- ctx.messages.push(...this.messages.map((msg) => msg.copy()));
89
- ctx.metadata = structuredClone(this.metadata);
90
- return ctx;
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,"file":"chat_context.js","sourceRoot":"","sources":["../../src/llm/chat_context.ts"],"names":[],"mappings":"AAMA,MAAM,CAAN,IAAY,QAKX;AALD,WAAY,QAAQ;IAClB,2CAAM,CAAA;IACN,uCAAI,CAAA;IACJ,iDAAS,CAAA;IACT,uCAAI,CAAA;AACN,CAAC,EALW,QAAQ,KAAR,QAAQ,QAKnB;AAmBD,MAAM,wBAAwB,GAAG;IAC/B,IAAI,EAAE,EAAE;IACR,MAAM,EAAE,EAAE;IACV,IAAI,EAAE,QAAQ,CAAC,MAAM;CACtB,CAAC;AAEF,MAAM,OAAO,WAAW;IACb,IAAI,CAAW;IACf,EAAE,CAAU;IACZ,IAAI,CAAU;IACd,OAAO,CAA+B;IACtC,SAAS,CAAsB;IAC/B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AAe,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;QAC9C,OAAO,GAAG,CAAC;IACb,CAAC;CACF"}
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;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AkC,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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,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
- // SPDX-FileCopyrightText: 2024 LiveKit, Inc.
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
- let current = value?.constructor;
10
- do {
11
- if (current?.name === target.name)
12
- return true;
13
- // eslint-disable-next-line @typescript-eslint/ban-types
14
- current = Object.getPrototypeOf(current);
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
- /** @internal */
19
- export const oaiParams = (p) => {
20
- const properties = {};
21
- const requiredProperties = [];
22
- const processZodType = (field) => {
23
- const isOptional = field instanceof z.ZodOptional;
24
- const nestedField = isOptional ? field._def.innerType : field;
25
- const description = field._def.description;
26
- if (looksLikeInstanceof(nestedField, z.ZodEnum)) {
27
- return {
28
- type: typeof nestedField._def.values[0],
29
- ...(description && { description }),
30
- enum: nestedField._def.values,
31
- };
32
- }
33
- else if (looksLikeInstanceof(nestedField, z.ZodArray)) {
34
- const elementType = nestedField._def.type;
35
- return {
36
- type: 'array',
37
- ...(description && { description }),
38
- items: processZodType(elementType),
39
- };
40
- }
41
- else if (looksLikeInstanceof(nestedField, z.ZodObject)) {
42
- const { properties, required } = oaiParams(nestedField);
43
- return {
44
- type: 'object',
45
- ...(description && { description }),
46
- properties,
47
- required,
48
- };
49
- }
50
- else {
51
- let type = nestedField._def.typeName.toLowerCase();
52
- type = type.includes('zod') ? type.substring(3) : type;
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
- const type = 'object';
67
- return {
68
- type,
69
- properties,
70
- required: requiredProperties,
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
- return {
80
- name: fncName,
81
- func,
82
- toolCallId,
83
- rawParams: rawArgs,
84
- params: JSON.parse(rawArgs),
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,"file":"function_context.js","sourceRoot":"","sources":["../../src/llm/function_context.ts"],"names":[],"mappings":"AAAA,6CAA6C;AAC7C,EAAE;AACF,sCAAsC;AACtC,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAgDxB,2EAA2E;AAC3E,gCAAgC;AAChC,wEAAwE;AACxE,MAAM,mBAAmB,GAAG,CAAI,KAAc,EAAE,MAAiC,EAAc,EAAE;IAC/F,IAAI,OAAO,GAAG,KAAK,EAAE,WAAW,CAAC;IACjC,GAAG,CAAC;QACF,IAAI,OAAO,EAAE,IAAI,KAAK,MAAM,CAAC,IAAI;YAAE,OAAO,IAAI,CAAC;QAC/C,wDAAwD;QACxD,OAAO,GAAG,MAAM,CAAC,cAAc,CAAC,OAAO,CAAa,CAAC;IACvD,CAAC,QAAQ,OAAO,EAAE,IAAI,EAAE;IACxB,OAAO,KAAK,CAAC;AACf,CAAC,CAAC;AAEF,gBAAgB;AAChB,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,CAAiB,EAAE,EAAE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AAC;KAC5B,CAAC;AACJ,CAAC,CAAC"}
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+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