@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.
Files changed (214) 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.d.ts +6 -1
  47. package/dist/job.d.ts.map +1 -1
  48. package/dist/job.js +195 -198
  49. package/dist/job.js.map +1 -1
  50. package/dist/llm/chat_context.cjs +131 -0
  51. package/dist/llm/chat_context.cjs.map +1 -0
  52. package/dist/llm/chat_context.js +98 -86
  53. package/dist/llm/chat_context.js.map +1 -1
  54. package/dist/llm/function_context.cjs +103 -0
  55. package/dist/llm/function_context.cjs.map +1 -0
  56. package/dist/llm/function_context.js +72 -81
  57. package/dist/llm/function_context.js.map +1 -1
  58. package/dist/llm/function_context.test.cjs +218 -0
  59. package/dist/llm/function_context.test.cjs.map +1 -0
  60. package/dist/llm/function_context.test.js +209 -210
  61. package/dist/llm/function_context.test.js.map +1 -1
  62. package/dist/llm/index.cjs +43 -0
  63. package/dist/llm/index.cjs.map +1 -0
  64. package/dist/llm/index.js +22 -6
  65. package/dist/llm/index.js.map +1 -1
  66. package/dist/llm/llm.cjs +76 -0
  67. package/dist/llm/llm.cjs.map +1 -0
  68. package/dist/llm/llm.js +48 -42
  69. package/dist/llm/llm.js.map +1 -1
  70. package/dist/log.cjs +57 -0
  71. package/dist/log.cjs.map +1 -0
  72. package/dist/log.js +27 -26
  73. package/dist/log.js.map +1 -1
  74. package/dist/multimodal/agent_playout.cjs +228 -0
  75. package/dist/multimodal/agent_playout.cjs.map +1 -0
  76. package/dist/multimodal/agent_playout.d.ts +1 -1
  77. package/dist/multimodal/agent_playout.js +193 -180
  78. package/dist/multimodal/agent_playout.js.map +1 -1
  79. package/dist/multimodal/index.cjs +25 -0
  80. package/dist/multimodal/index.cjs.map +1 -0
  81. package/dist/multimodal/index.js +2 -5
  82. package/dist/multimodal/index.js.map +1 -1
  83. package/dist/multimodal/multimodal_agent.cjs +404 -0
  84. package/dist/multimodal/multimodal_agent.cjs.map +1 -0
  85. package/dist/multimodal/multimodal_agent.d.ts +1 -1
  86. package/dist/multimodal/multimodal_agent.js +351 -330
  87. package/dist/multimodal/multimodal_agent.js.map +1 -1
  88. package/dist/pipeline/agent_output.cjs +172 -0
  89. package/dist/pipeline/agent_output.cjs.map +1 -0
  90. package/dist/pipeline/agent_output.js +136 -138
  91. package/dist/pipeline/agent_output.js.map +1 -1
  92. package/dist/pipeline/agent_playout.cjs +169 -0
  93. package/dist/pipeline/agent_playout.cjs.map +1 -0
  94. package/dist/pipeline/agent_playout.js +126 -136
  95. package/dist/pipeline/agent_playout.js.map +1 -1
  96. package/dist/pipeline/human_input.cjs +158 -0
  97. package/dist/pipeline/human_input.cjs.map +1 -0
  98. package/dist/pipeline/human_input.js +124 -125
  99. package/dist/pipeline/human_input.js.map +1 -1
  100. package/dist/pipeline/index.cjs +31 -0
  101. package/dist/pipeline/index.cjs.map +1 -0
  102. package/dist/pipeline/index.js +8 -4
  103. package/dist/pipeline/index.js.map +1 -1
  104. package/dist/pipeline/pipeline_agent.cjs +642 -0
  105. package/dist/pipeline/pipeline_agent.cjs.map +1 -0
  106. package/dist/pipeline/pipeline_agent.js +595 -651
  107. package/dist/pipeline/pipeline_agent.js.map +1 -1
  108. package/dist/pipeline/speech_handle.cjs +128 -0
  109. package/dist/pipeline/speech_handle.cjs.map +1 -0
  110. package/dist/pipeline/speech_handle.js +102 -100
  111. package/dist/pipeline/speech_handle.js.map +1 -1
  112. package/dist/plugin.cjs +46 -0
  113. package/dist/plugin.cjs.map +1 -0
  114. package/dist/plugin.js +20 -20
  115. package/dist/plugin.js.map +1 -1
  116. package/dist/stt/index.cjs +38 -0
  117. package/dist/stt/index.cjs.map +1 -0
  118. package/dist/stt/index.js +13 -5
  119. package/dist/stt/index.js.map +1 -1
  120. package/dist/stt/stream_adapter.cjs +87 -0
  121. package/dist/stt/stream_adapter.cjs.map +1 -0
  122. package/dist/stt/stream_adapter.js +58 -55
  123. package/dist/stt/stream_adapter.js.map +1 -1
  124. package/dist/stt/stt.cjs +98 -0
  125. package/dist/stt/stt.cjs.map +1 -0
  126. package/dist/stt/stt.js +63 -98
  127. package/dist/stt/stt.js.map +1 -1
  128. package/dist/tokenize/basic/basic.cjs +98 -0
  129. package/dist/tokenize/basic/basic.cjs.map +1 -0
  130. package/dist/tokenize/basic/basic.d.ts +1 -1
  131. package/dist/tokenize/basic/basic.d.ts.map +1 -1
  132. package/dist/tokenize/basic/basic.js +56 -45
  133. package/dist/tokenize/basic/basic.js.map +1 -1
  134. package/dist/tokenize/basic/hyphenator.cjs +425 -0
  135. package/dist/tokenize/basic/hyphenator.cjs.map +1 -0
  136. package/dist/tokenize/basic/hyphenator.js +66 -82
  137. package/dist/tokenize/basic/hyphenator.js.map +1 -1
  138. package/dist/tokenize/basic/index.cjs +35 -0
  139. package/dist/tokenize/basic/index.cjs.map +1 -0
  140. package/dist/tokenize/basic/index.js +7 -4
  141. package/dist/tokenize/basic/index.js.map +1 -1
  142. package/dist/tokenize/basic/paragraph.cjs +57 -0
  143. package/dist/tokenize/basic/paragraph.cjs.map +1 -0
  144. package/dist/tokenize/basic/paragraph.js +30 -35
  145. package/dist/tokenize/basic/paragraph.js.map +1 -1
  146. package/dist/tokenize/basic/sentence.cjs +89 -0
  147. package/dist/tokenize/basic/sentence.cjs.map +1 -0
  148. package/dist/tokenize/basic/sentence.d.ts.map +1 -1
  149. package/dist/tokenize/basic/sentence.js +62 -57
  150. package/dist/tokenize/basic/sentence.js.map +1 -1
  151. package/dist/tokenize/basic/word.cjs +44 -0
  152. package/dist/tokenize/basic/word.cjs.map +1 -0
  153. package/dist/tokenize/basic/word.js +17 -20
  154. package/dist/tokenize/basic/word.js.map +1 -1
  155. package/dist/tokenize/index.cjs +55 -0
  156. package/dist/tokenize/index.cjs.map +1 -0
  157. package/dist/tokenize/index.js +18 -7
  158. package/dist/tokenize/index.js.map +1 -1
  159. package/dist/tokenize/token_stream.cjs +164 -0
  160. package/dist/tokenize/token_stream.cjs.map +1 -0
  161. package/dist/tokenize/token_stream.js +133 -139
  162. package/dist/tokenize/token_stream.js.map +1 -1
  163. package/dist/tokenize/tokenizer.cjs +184 -0
  164. package/dist/tokenize/tokenizer.cjs.map +1 -0
  165. package/dist/tokenize/tokenizer.js +138 -99
  166. package/dist/tokenize/tokenizer.js.map +1 -1
  167. package/dist/tokenize/tokenizer.test.cjs +220 -0
  168. package/dist/tokenize/tokenizer.test.cjs.map +1 -0
  169. package/dist/tokenize/tokenizer.test.d.ts +2 -0
  170. package/dist/tokenize/tokenizer.test.d.ts.map +1 -0
  171. package/dist/tokenize/tokenizer.test.js +219 -0
  172. package/dist/tokenize/tokenizer.test.js.map +1 -0
  173. package/dist/transcription.cjs +131 -0
  174. package/dist/transcription.cjs.map +1 -0
  175. package/dist/transcription.js +99 -96
  176. package/dist/transcription.js.map +1 -1
  177. package/dist/tts/index.cjs +38 -0
  178. package/dist/tts/index.cjs.map +1 -0
  179. package/dist/tts/index.js +13 -5
  180. package/dist/tts/index.js.map +1 -1
  181. package/dist/tts/stream_adapter.cjs +78 -0
  182. package/dist/tts/stream_adapter.cjs.map +1 -0
  183. package/dist/tts/stream_adapter.js +50 -47
  184. package/dist/tts/stream_adapter.js.map +1 -1
  185. package/dist/tts/tts.cjs +127 -0
  186. package/dist/tts/tts.cjs.map +1 -0
  187. package/dist/tts/tts.js +90 -120
  188. package/dist/tts/tts.js.map +1 -1
  189. package/dist/utils.cjs +284 -0
  190. package/dist/utils.cjs.map +1 -0
  191. package/dist/utils.js +242 -247
  192. package/dist/utils.js.map +1 -1
  193. package/dist/vad.cjs +92 -0
  194. package/dist/vad.cjs.map +1 -0
  195. package/dist/vad.js +57 -52
  196. package/dist/vad.js.map +1 -1
  197. package/dist/version.cjs +29 -0
  198. package/dist/version.cjs.map +1 -0
  199. package/dist/version.js +4 -4
  200. package/dist/version.js.map +1 -1
  201. package/dist/worker.cjs +577 -0
  202. package/dist/worker.cjs.map +1 -0
  203. package/dist/worker.d.ts +1 -1
  204. package/dist/worker.d.ts.map +1 -1
  205. package/dist/worker.js +512 -484
  206. package/dist/worker.js.map +1 -1
  207. package/package.json +18 -8
  208. package/src/ipc/job_main.ts +66 -64
  209. package/src/job.ts +3 -2
  210. package/src/pipeline/pipeline_agent.ts +23 -23
  211. package/src/tokenize/basic/basic.ts +1 -1
  212. package/src/tokenize/basic/sentence.ts +14 -8
  213. package/src/tokenize/tokenizer.test.ts +255 -0
  214. package/src/worker.ts +1 -0
@@ -0,0 +1,218 @@
1
+ "use strict";
2
+ var import_vitest = require("vitest");
3
+ var import_zod = require("zod");
4
+ var import_function_context = require("./function_context.cjs");
5
+ (0, import_vitest.describe)("function_context", () => {
6
+ (0, import_vitest.describe)("oaiParams", () => {
7
+ (0, import_vitest.it)("should handle basic object schema", () => {
8
+ const schema = import_zod.z.object({
9
+ name: import_zod.z.string().describe("The user name"),
10
+ age: import_zod.z.number().describe("The user age")
11
+ });
12
+ const result = (0, import_function_context.oaiParams)(schema);
13
+ (0, import_vitest.expect)(result).toEqual({
14
+ type: "object",
15
+ properties: {
16
+ name: {
17
+ type: "string",
18
+ description: "The user name"
19
+ },
20
+ age: {
21
+ type: "number",
22
+ description: "The user age"
23
+ }
24
+ },
25
+ required: ["name", "age"]
26
+ });
27
+ });
28
+ (0, import_vitest.it)("should handle enum fields", () => {
29
+ const schema = import_zod.z.object({
30
+ color: import_zod.z.enum(["red", "blue", "green"]).describe("Choose a color")
31
+ });
32
+ const result = (0, import_function_context.oaiParams)(schema);
33
+ (0, import_vitest.expect)(result).toEqual({
34
+ type: "object",
35
+ properties: {
36
+ color: {
37
+ type: "string",
38
+ description: "Choose a color",
39
+ enum: ["red", "blue", "green"]
40
+ }
41
+ },
42
+ required: ["color"]
43
+ });
44
+ });
45
+ (0, import_vitest.it)("should handle array fields", () => {
46
+ const schema = import_zod.z.object({
47
+ tags: import_zod.z.array(import_zod.z.string()).describe("List of tags")
48
+ });
49
+ const result = (0, import_function_context.oaiParams)(schema);
50
+ (0, import_vitest.expect)(result).toEqual({
51
+ type: "object",
52
+ properties: {
53
+ tags: {
54
+ type: "array",
55
+ description: "List of tags",
56
+ items: {
57
+ type: "string"
58
+ }
59
+ }
60
+ },
61
+ required: ["tags"]
62
+ });
63
+ });
64
+ (0, import_vitest.it)("should handle array of enums", () => {
65
+ const schema = import_zod.z.object({
66
+ colors: import_zod.z.array(import_zod.z.enum(["red", "blue", "green"])).describe("List of colors")
67
+ });
68
+ const result = (0, import_function_context.oaiParams)(schema);
69
+ (0, import_vitest.expect)(result).toEqual({
70
+ type: "object",
71
+ properties: {
72
+ colors: {
73
+ type: "array",
74
+ description: "List of colors",
75
+ items: {
76
+ type: "string",
77
+ enum: ["red", "blue", "green"]
78
+ }
79
+ }
80
+ },
81
+ required: ["colors"]
82
+ });
83
+ });
84
+ (0, import_vitest.it)("should handle optional fields", () => {
85
+ const schema = import_zod.z.object({
86
+ name: import_zod.z.string().describe("The user name"),
87
+ age: import_zod.z.number().optional().describe("The user age")
88
+ });
89
+ const result = (0, import_function_context.oaiParams)(schema);
90
+ (0, import_vitest.expect)(result).toEqual({
91
+ type: "object",
92
+ properties: {
93
+ name: {
94
+ type: "string",
95
+ description: "The user name"
96
+ },
97
+ age: {
98
+ type: "number",
99
+ description: "The user age"
100
+ }
101
+ },
102
+ required: ["name"]
103
+ // age should not be required
104
+ });
105
+ });
106
+ (0, import_vitest.it)("should handle fields without descriptions", () => {
107
+ const schema = import_zod.z.object({
108
+ name: import_zod.z.string(),
109
+ age: import_zod.z.number()
110
+ });
111
+ const result = (0, import_function_context.oaiParams)(schema);
112
+ (0, import_vitest.expect)(result).toEqual({
113
+ type: "object",
114
+ properties: {
115
+ name: {
116
+ type: "string",
117
+ description: void 0
118
+ },
119
+ age: {
120
+ type: "number",
121
+ description: void 0
122
+ }
123
+ },
124
+ required: ["name", "age"]
125
+ });
126
+ });
127
+ });
128
+ (0, import_vitest.describe)("CallableFunction type", () => {
129
+ (0, import_vitest.it)("should properly type a callable function", async () => {
130
+ const schema = import_zod.z.object({
131
+ name: import_zod.z.string().describe("The user name"),
132
+ age: import_zod.z.number().describe("The user age")
133
+ });
134
+ const testFunction = {
135
+ description: "Test function",
136
+ parameters: schema,
137
+ execute: async (args) => {
138
+ return `${args.name} is ${args.age} years old`;
139
+ }
140
+ };
141
+ const result = await testFunction.execute({ name: "John", age: 30 });
142
+ (0, import_vitest.expect)(result).toBe("John is 30 years old");
143
+ });
144
+ (0, import_vitest.it)("should handle async execution", async () => {
145
+ const schema = import_zod.z.object({
146
+ delay: import_zod.z.number().describe("Delay in milliseconds")
147
+ });
148
+ const testFunction = {
149
+ description: "Async test function",
150
+ parameters: schema,
151
+ execute: async (args) => {
152
+ await new Promise((resolve) => setTimeout(resolve, args.delay));
153
+ return args.delay;
154
+ }
155
+ };
156
+ const start = Date.now();
157
+ const result = await testFunction.execute({ delay: 100 });
158
+ const duration = Date.now() - start;
159
+ (0, import_vitest.expect)(result).toBe(100);
160
+ (0, import_vitest.expect)(duration).toBeGreaterThanOrEqual(95);
161
+ });
162
+ (0, import_vitest.describe)("nested array support", () => {
163
+ (0, import_vitest.it)("should handle nested array fields", () => {
164
+ const schema = import_zod.z.object({
165
+ items: import_zod.z.array(
166
+ import_zod.z.object({
167
+ name: import_zod.z.string().describe("the item name"),
168
+ modifiers: import_zod.z.array(
169
+ import_zod.z.object({
170
+ modifier_name: import_zod.z.string(),
171
+ modifier_value: import_zod.z.string()
172
+ })
173
+ ).describe("list of the modifiers applied on this item, such as size")
174
+ })
175
+ )
176
+ });
177
+ const result = (0, import_function_context.oaiParams)(schema);
178
+ (0, import_vitest.expect)(result).toEqual({
179
+ type: "object",
180
+ properties: {
181
+ items: {
182
+ type: "array",
183
+ description: void 0,
184
+ items: {
185
+ type: "object",
186
+ properties: {
187
+ name: {
188
+ type: "string",
189
+ description: "the item name"
190
+ },
191
+ modifiers: {
192
+ type: "array",
193
+ description: "list of the modifiers applied on this item, such as size",
194
+ items: {
195
+ type: "object",
196
+ properties: {
197
+ modifier_name: {
198
+ type: "string"
199
+ },
200
+ modifier_value: {
201
+ type: "string"
202
+ }
203
+ },
204
+ required: ["modifier_name", "modifier_value"]
205
+ }
206
+ }
207
+ },
208
+ required: ["name", "modifiers"]
209
+ }
210
+ }
211
+ },
212
+ required: ["items"]
213
+ });
214
+ });
215
+ });
216
+ });
217
+ });
218
+ //# sourceMappingURL=function_context.test.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/llm/function_context.test.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { describe, expect, it } from 'vitest';\nimport { z } from 'zod';\nimport type { CallableFunction } from './function_context.js';\nimport { oaiParams } from './function_context.js';\n\ndescribe('function_context', () => {\n describe('oaiParams', () => {\n it('should handle basic object schema', () => {\n const schema = z.object({\n name: z.string().describe('The user name'),\n age: z.number().describe('The user age'),\n });\n\n const result = oaiParams(schema);\n\n expect(result).toEqual({\n type: 'object',\n properties: {\n name: {\n type: 'string',\n description: 'The user name',\n },\n age: {\n type: 'number',\n description: 'The user age',\n },\n },\n required: ['name', 'age'],\n });\n });\n\n it('should handle enum fields', () => {\n const schema = z.object({\n color: z.enum(['red', 'blue', 'green']).describe('Choose a color'),\n });\n\n const result = oaiParams(schema);\n\n expect(result).toEqual({\n type: 'object',\n properties: {\n color: {\n type: 'string',\n description: 'Choose a color',\n enum: ['red', 'blue', 'green'],\n },\n },\n required: ['color'],\n });\n });\n\n it('should handle array fields', () => {\n const schema = z.object({\n tags: z.array(z.string()).describe('List of tags'),\n });\n\n const result = oaiParams(schema);\n\n expect(result).toEqual({\n type: 'object',\n properties: {\n tags: {\n type: 'array',\n description: 'List of tags',\n items: {\n type: 'string',\n },\n },\n },\n required: ['tags'],\n });\n });\n\n it('should handle array of enums', () => {\n const schema = z.object({\n colors: z.array(z.enum(['red', 'blue', 'green'])).describe('List of colors'),\n });\n\n const result = oaiParams(schema);\n\n expect(result).toEqual({\n type: 'object',\n properties: {\n colors: {\n type: 'array',\n description: 'List of colors',\n items: {\n type: 'string',\n enum: ['red', 'blue', 'green'],\n },\n },\n },\n required: ['colors'],\n });\n });\n\n it('should handle optional fields', () => {\n const schema = z.object({\n name: z.string().describe('The user name'),\n age: z.number().optional().describe('The user age'),\n });\n\n const result = oaiParams(schema);\n\n expect(result).toEqual({\n type: 'object',\n properties: {\n name: {\n type: 'string',\n description: 'The user name',\n },\n age: {\n type: 'number',\n description: 'The user age',\n },\n },\n required: ['name'], // age should not be required\n });\n });\n\n it('should handle fields without descriptions', () => {\n const schema = z.object({\n name: z.string(),\n age: z.number(),\n });\n\n const result = oaiParams(schema);\n\n expect(result).toEqual({\n type: 'object',\n properties: {\n name: {\n type: 'string',\n description: undefined,\n },\n age: {\n type: 'number',\n description: undefined,\n },\n },\n required: ['name', 'age'],\n });\n });\n });\n\n describe('CallableFunction type', () => {\n it('should properly type a callable function', async () => {\n const schema = z.object({\n name: z.string().describe('The user name'),\n age: z.number().describe('The user age'),\n });\n\n const testFunction: CallableFunction<typeof schema, string> = {\n description: 'Test function',\n parameters: schema,\n execute: async (args: z.infer<typeof schema>) => {\n // TypeScript should recognize args.name and args.age\n return `${args.name} is ${args.age} years old`;\n },\n };\n\n const result = await testFunction.execute({ name: 'John', age: 30 });\n expect(result).toBe('John is 30 years old');\n });\n\n it('should handle async execution', async () => {\n const schema = z.object({\n delay: z.number().describe('Delay in milliseconds'),\n });\n\n const testFunction: CallableFunction<typeof schema, number> = {\n description: 'Async test function',\n parameters: schema,\n execute: async (args: z.infer<typeof schema>) => {\n await new Promise((resolve) => setTimeout(resolve, args.delay));\n return args.delay;\n },\n };\n\n const start = Date.now();\n const result = await testFunction.execute({ delay: 100 });\n const duration = Date.now() - start;\n\n expect(result).toBe(100);\n expect(duration).toBeGreaterThanOrEqual(95); // Allow for small timing variations\n });\n\n describe('nested array support', () => {\n it('should handle nested array fields', () => {\n const schema = z.object({\n items: z.array(\n z.object({\n name: z.string().describe('the item name'),\n modifiers: z\n .array(\n z.object({\n modifier_name: z.string(),\n modifier_value: z.string(),\n }),\n )\n .describe('list of the modifiers applied on this item, such as size'),\n }),\n ),\n });\n const result = oaiParams(schema);\n expect(result).toEqual({\n type: 'object',\n properties: {\n items: {\n type: 'array',\n description: undefined,\n items: {\n type: 'object',\n properties: {\n name: {\n type: 'string',\n description: 'the item name',\n },\n modifiers: {\n type: 'array',\n description: 'list of the modifiers applied on this item, such as size',\n items: {\n type: 'object',\n properties: {\n modifier_name: {\n type: 'string',\n },\n modifier_value: {\n type: 'string',\n },\n },\n required: ['modifier_name', 'modifier_value'],\n },\n },\n },\n required: ['name', 'modifiers'],\n },\n },\n },\n required: ['items'],\n });\n });\n });\n });\n});\n"],"mappings":";AAGA,oBAAqC;AACrC,iBAAkB;AAElB,8BAA0B;AAAA,IAE1B,wBAAS,oBAAoB,MAAM;AACjC,8BAAS,aAAa,MAAM;AAC1B,0BAAG,qCAAqC,MAAM;AAC5C,YAAM,SAAS,aAAE,OAAO;AAAA,QACtB,MAAM,aAAE,OAAO,EAAE,SAAS,eAAe;AAAA,QACzC,KAAK,aAAE,OAAO,EAAE,SAAS,cAAc;AAAA,MACzC,CAAC;AAED,YAAM,aAAS,mCAAU,MAAM;AAE/B,gCAAO,MAAM,EAAE,QAAQ;AAAA,QACrB,MAAM;AAAA,QACN,YAAY;AAAA,UACV,MAAM;AAAA,YACJ,MAAM;AAAA,YACN,aAAa;AAAA,UACf;AAAA,UACA,KAAK;AAAA,YACH,MAAM;AAAA,YACN,aAAa;AAAA,UACf;AAAA,QACF;AAAA,QACA,UAAU,CAAC,QAAQ,KAAK;AAAA,MAC1B,CAAC;AAAA,IACH,CAAC;AAED,0BAAG,6BAA6B,MAAM;AACpC,YAAM,SAAS,aAAE,OAAO;AAAA,QACtB,OAAO,aAAE,KAAK,CAAC,OAAO,QAAQ,OAAO,CAAC,EAAE,SAAS,gBAAgB;AAAA,MACnE,CAAC;AAED,YAAM,aAAS,mCAAU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Aa;AAAA,cACb,OAAO;AAAA,gBACL,MAAM;AAAA,gBACN,YAAY;AAAA,kBACV,MAAM;AAAA,oBACJ,MAAM;AAAA,oBACN,aAAa;AAAA,kBACf;AAAA,kBACA,WAAW;AAAA,oBACT,MAAM;AAAA,oBACN,aAAa;AAAA,oBACb,OAAO;AAAA,sBACL,MAAM;AAAA,sBACN,YAAY;AAAA,wBACV,eAAe;AAAA,0BACb,MAAM;AAAA,wBACR;AAAA,wBACA,gBAAgB;AAAA,0BACd,MAAM;AAAA,wBACR;AAAA,sBACF;AAAA,sBACA,UAAU,CAAC,iBAAiB,gBAAgB;AAAA,oBAC9C;AAAA,kBACF;AAAA,gBACF;AAAA,gBACA,UAAU,CAAC,QAAQ,WAAW;AAAA,cAChC;AAAA,YACF;AAAA,UACF;AAAA,UACA,UAAU,CAAC,OAAO;AAAA,QACpB,CAAC;AAAA,MACH,CAAC;AAAA,IACH,CAAC;AAAA,EACH,CAAC;AACH,CAAC;","names":[]}
@@ -1,218 +1,217 @@
1
- // SPDX-FileCopyrightText: 2024 LiveKit, Inc.
2
- //
3
- // SPDX-License-Identifier: Apache-2.0
4
- import { describe, expect, it } from 'vitest';
5
- import { z } from 'zod';
6
- import { oaiParams } from './function_context.js';
7
- describe('function_context', () => {
8
- describe('oaiParams', () => {
9
- it('should handle basic object schema', () => {
10
- const schema = z.object({
11
- name: z.string().describe('The user name'),
12
- age: z.number().describe('The user age'),
13
- });
14
- const result = oaiParams(schema);
15
- expect(result).toEqual({
16
- type: 'object',
17
- properties: {
18
- name: {
19
- type: 'string',
20
- description: 'The user name',
21
- },
22
- age: {
23
- type: 'number',
24
- description: 'The user age',
25
- },
26
- },
27
- required: ['name', 'age'],
28
- });
29
- });
30
- it('should handle enum fields', () => {
31
- const schema = z.object({
32
- color: z.enum(['red', 'blue', 'green']).describe('Choose a color'),
33
- });
34
- const result = oaiParams(schema);
35
- expect(result).toEqual({
36
- type: 'object',
37
- properties: {
38
- color: {
39
- type: 'string',
40
- description: 'Choose a color',
41
- enum: ['red', 'blue', 'green'],
42
- },
43
- },
44
- required: ['color'],
45
- });
46
- });
47
- it('should handle array fields', () => {
48
- const schema = z.object({
49
- tags: z.array(z.string()).describe('List of tags'),
50
- });
51
- const result = oaiParams(schema);
52
- expect(result).toEqual({
53
- type: 'object',
54
- properties: {
55
- tags: {
56
- type: 'array',
57
- description: 'List of tags',
58
- items: {
59
- type: 'string',
60
- },
61
- },
62
- },
63
- required: ['tags'],
64
- });
1
+ import { describe, expect, it } from "vitest";
2
+ import { z } from "zod";
3
+ import { oaiParams } from "./function_context.js";
4
+ describe("function_context", () => {
5
+ describe("oaiParams", () => {
6
+ it("should handle basic object schema", () => {
7
+ const schema = z.object({
8
+ name: z.string().describe("The user name"),
9
+ age: z.number().describe("The user age")
10
+ });
11
+ const result = oaiParams(schema);
12
+ expect(result).toEqual({
13
+ type: "object",
14
+ properties: {
15
+ name: {
16
+ type: "string",
17
+ description: "The user name"
18
+ },
19
+ age: {
20
+ type: "number",
21
+ description: "The user age"
22
+ }
23
+ },
24
+ required: ["name", "age"]
25
+ });
26
+ });
27
+ it("should handle enum fields", () => {
28
+ const schema = z.object({
29
+ color: z.enum(["red", "blue", "green"]).describe("Choose a color")
30
+ });
31
+ const result = oaiParams(schema);
32
+ expect(result).toEqual({
33
+ type: "object",
34
+ properties: {
35
+ color: {
36
+ type: "string",
37
+ description: "Choose a color",
38
+ enum: ["red", "blue", "green"]
39
+ }
40
+ },
41
+ required: ["color"]
42
+ });
43
+ });
44
+ it("should handle array fields", () => {
45
+ const schema = z.object({
46
+ tags: z.array(z.string()).describe("List of tags")
47
+ });
48
+ const result = oaiParams(schema);
49
+ expect(result).toEqual({
50
+ type: "object",
51
+ properties: {
52
+ tags: {
53
+ type: "array",
54
+ description: "List of tags",
55
+ items: {
56
+ type: "string"
57
+ }
58
+ }
59
+ },
60
+ required: ["tags"]
61
+ });
62
+ });
63
+ it("should handle array of enums", () => {
64
+ const schema = z.object({
65
+ colors: z.array(z.enum(["red", "blue", "green"])).describe("List of colors")
66
+ });
67
+ const result = oaiParams(schema);
68
+ expect(result).toEqual({
69
+ type: "object",
70
+ properties: {
71
+ colors: {
72
+ type: "array",
73
+ description: "List of colors",
74
+ items: {
75
+ type: "string",
76
+ enum: ["red", "blue", "green"]
77
+ }
78
+ }
79
+ },
80
+ required: ["colors"]
81
+ });
82
+ });
83
+ it("should handle optional fields", () => {
84
+ const schema = z.object({
85
+ name: z.string().describe("The user name"),
86
+ age: z.number().optional().describe("The user age")
87
+ });
88
+ const result = oaiParams(schema);
89
+ expect(result).toEqual({
90
+ type: "object",
91
+ properties: {
92
+ name: {
93
+ type: "string",
94
+ description: "The user name"
95
+ },
96
+ age: {
97
+ type: "number",
98
+ description: "The user age"
99
+ }
100
+ },
101
+ required: ["name"]
102
+ // age should not be required
103
+ });
104
+ });
105
+ it("should handle fields without descriptions", () => {
106
+ const schema = z.object({
107
+ name: z.string(),
108
+ age: z.number()
109
+ });
110
+ const result = oaiParams(schema);
111
+ expect(result).toEqual({
112
+ type: "object",
113
+ properties: {
114
+ name: {
115
+ type: "string",
116
+ description: void 0
117
+ },
118
+ age: {
119
+ type: "number",
120
+ description: void 0
121
+ }
122
+ },
123
+ required: ["name", "age"]
124
+ });
125
+ });
126
+ });
127
+ describe("CallableFunction type", () => {
128
+ it("should properly type a callable function", async () => {
129
+ const schema = z.object({
130
+ name: z.string().describe("The user name"),
131
+ age: z.number().describe("The user age")
132
+ });
133
+ const testFunction = {
134
+ description: "Test function",
135
+ parameters: schema,
136
+ execute: async (args) => {
137
+ return `${args.name} is ${args.age} years old`;
138
+ }
139
+ };
140
+ const result = await testFunction.execute({ name: "John", age: 30 });
141
+ expect(result).toBe("John is 30 years old");
142
+ });
143
+ it("should handle async execution", async () => {
144
+ const schema = z.object({
145
+ delay: z.number().describe("Delay in milliseconds")
146
+ });
147
+ const testFunction = {
148
+ description: "Async test function",
149
+ parameters: schema,
150
+ execute: async (args) => {
151
+ await new Promise((resolve) => setTimeout(resolve, args.delay));
152
+ return args.delay;
153
+ }
154
+ };
155
+ const start = Date.now();
156
+ const result = await testFunction.execute({ delay: 100 });
157
+ const duration = Date.now() - start;
158
+ expect(result).toBe(100);
159
+ expect(duration).toBeGreaterThanOrEqual(95);
160
+ });
161
+ describe("nested array support", () => {
162
+ it("should handle nested array fields", () => {
163
+ const schema = z.object({
164
+ items: z.array(
165
+ z.object({
166
+ name: z.string().describe("the item name"),
167
+ modifiers: z.array(
168
+ z.object({
169
+ modifier_name: z.string(),
170
+ modifier_value: z.string()
171
+ })
172
+ ).describe("list of the modifiers applied on this item, such as size")
173
+ })
174
+ )
65
175
  });
66
- it('should handle array of enums', () => {
67
- const schema = z.object({
68
- colors: z.array(z.enum(['red', 'blue', 'green'])).describe('List of colors'),
69
- });
70
- const result = oaiParams(schema);
71
- expect(result).toEqual({
72
- type: 'object',
176
+ const result = oaiParams(schema);
177
+ expect(result).toEqual({
178
+ type: "object",
179
+ properties: {
180
+ items: {
181
+ type: "array",
182
+ description: void 0,
183
+ items: {
184
+ type: "object",
73
185
  properties: {
74
- colors: {
75
- type: 'array',
76
- description: 'List of colors',
77
- items: {
78
- type: 'string',
79
- enum: ['red', 'blue', 'green'],
186
+ name: {
187
+ type: "string",
188
+ description: "the item name"
189
+ },
190
+ modifiers: {
191
+ type: "array",
192
+ description: "list of the modifiers applied on this item, such as size",
193
+ items: {
194
+ type: "object",
195
+ properties: {
196
+ modifier_name: {
197
+ type: "string"
80
198
  },
81
- },
82
- },
83
- required: ['colors'],
84
- });
85
- });
86
- it('should handle optional fields', () => {
87
- const schema = z.object({
88
- name: z.string().describe('The user name'),
89
- age: z.number().optional().describe('The user age'),
90
- });
91
- const result = oaiParams(schema);
92
- expect(result).toEqual({
93
- type: 'object',
94
- properties: {
95
- name: {
96
- type: 'string',
97
- description: 'The user name',
98
- },
99
- age: {
100
- type: 'number',
101
- description: 'The user age',
102
- },
103
- },
104
- required: ['name'], // age should not be required
105
- });
106
- });
107
- it('should handle fields without descriptions', () => {
108
- const schema = z.object({
109
- name: z.string(),
110
- age: z.number(),
111
- });
112
- const result = oaiParams(schema);
113
- expect(result).toEqual({
114
- type: 'object',
115
- properties: {
116
- name: {
117
- type: 'string',
118
- description: undefined,
119
- },
120
- age: {
121
- type: 'number',
122
- description: undefined,
123
- },
199
+ modifier_value: {
200
+ type: "string"
201
+ }
202
+ },
203
+ required: ["modifier_name", "modifier_value"]
204
+ }
205
+ }
124
206
  },
125
- required: ['name', 'age'],
126
- });
127
- });
128
- });
129
- describe('CallableFunction type', () => {
130
- it('should properly type a callable function', async () => {
131
- const schema = z.object({
132
- name: z.string().describe('The user name'),
133
- age: z.number().describe('The user age'),
134
- });
135
- const testFunction = {
136
- description: 'Test function',
137
- parameters: schema,
138
- execute: async (args) => {
139
- // TypeScript should recognize args.name and args.age
140
- return `${args.name} is ${args.age} years old`;
141
- },
142
- };
143
- const result = await testFunction.execute({ name: 'John', age: 30 });
144
- expect(result).toBe('John is 30 years old');
145
- });
146
- it('should handle async execution', async () => {
147
- const schema = z.object({
148
- delay: z.number().describe('Delay in milliseconds'),
149
- });
150
- const testFunction = {
151
- description: 'Async test function',
152
- parameters: schema,
153
- execute: async (args) => {
154
- await new Promise((resolve) => setTimeout(resolve, args.delay));
155
- return args.delay;
156
- },
157
- };
158
- const start = Date.now();
159
- const result = await testFunction.execute({ delay: 100 });
160
- const duration = Date.now() - start;
161
- expect(result).toBe(100);
162
- expect(duration).toBeGreaterThanOrEqual(95); // Allow for small timing variations
163
- });
164
- describe('nested array support', () => {
165
- it('should handle nested array fields', () => {
166
- const schema = z.object({
167
- items: z.array(z.object({
168
- name: z.string().describe('the item name'),
169
- modifiers: z
170
- .array(z.object({
171
- modifier_name: z.string(),
172
- modifier_value: z.string(),
173
- }))
174
- .describe('list of the modifiers applied on this item, such as size'),
175
- })),
176
- });
177
- const result = oaiParams(schema);
178
- expect(result).toEqual({
179
- type: 'object',
180
- properties: {
181
- items: {
182
- type: 'array',
183
- description: undefined,
184
- items: {
185
- type: 'object',
186
- properties: {
187
- name: {
188
- type: 'string',
189
- description: 'the item name',
190
- },
191
- modifiers: {
192
- type: 'array',
193
- description: 'list of the modifiers applied on this item, such as size',
194
- items: {
195
- type: 'object',
196
- properties: {
197
- modifier_name: {
198
- type: 'string',
199
- },
200
- modifier_value: {
201
- type: 'string',
202
- },
203
- },
204
- required: ['modifier_name', 'modifier_value'],
205
- },
206
- },
207
- },
208
- required: ['name', 'modifiers'],
209
- },
210
- },
211
- },
212
- required: ['items'],
213
- });
214
- });
207
+ required: ["name", "modifiers"]
208
+ }
209
+ }
210
+ },
211
+ required: ["items"]
215
212
  });
213
+ });
216
214
  });
215
+ });
217
216
  });
218
217
  //# sourceMappingURL=function_context.test.js.map