evolcore 0.0.8 → 0.0.9

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 (213) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/README.md +37 -3
  3. package/dist/agents/baseagent.js +111 -19
  4. package/dist/agents/claude-runner.js +30 -2
  5. package/dist/agents/codex-app-server-client.js +31 -0
  6. package/dist/agents/codex-runner.js +3 -0
  7. package/dist/aun/msg/group.js +9 -9
  8. package/dist/aun/msg/p2p.js +11 -6
  9. package/dist/aun/msg/upload.js +43 -29
  10. package/dist/aun/outbox.js +48 -5
  11. package/dist/channels/aun.js +199 -50
  12. package/dist/cli/aun-commands.js +10 -7
  13. package/dist/cli/daemon-commands.js +19 -4
  14. package/dist/cli/index.js +11 -1
  15. package/dist/cli/init.js +103 -4
  16. package/dist/cli/model.js +5 -5
  17. package/dist/config/builtin-role-templates.js +4 -3
  18. package/dist/config/config-field-policy.js +6 -2
  19. package/dist/config/config-manager.js +48 -11
  20. package/dist/config/role-schema.js +18 -3
  21. package/dist/config/role-service.js +3 -3
  22. package/dist/config/role-store.js +4 -3
  23. package/dist/config-store.js +4 -0
  24. package/dist/core/auth/operation-authorizer.js +3 -1
  25. package/dist/core/bootstrap-service.js +0 -12
  26. package/dist/core/channel-loader.js +5 -1
  27. package/dist/core/command/command-handler.js +1 -1
  28. package/dist/core/command/evol-menu-version-gate.js +3 -2
  29. package/dist/core/command/menu-handler.js +33 -22
  30. package/dist/core/command/menu-protocol.js +7 -4
  31. package/dist/core/command/slash-handler.js +4 -4
  32. package/dist/core/inference/text-inference.js +18 -5
  33. package/dist/core/message/message-bridge.js +17 -6
  34. package/dist/core/message/response-engine.js +22 -3
  35. package/dist/core/model/config-scope.js +3 -3
  36. package/dist/core/permission/tool-policy.js +18 -0
  37. package/dist/index.js +43 -26
  38. package/dist/ipc.js +1 -1
  39. package/dist/product.js +1 -0
  40. package/dist/utils/autostart.js +27 -0
  41. package/dist/utils/ecweb-utils.js +15 -2
  42. package/dist/utils/macos-autostart.js +179 -0
  43. package/dist/utils/windows-autostart.js +130 -0
  44. package/ecagent/dist/agent-loop.d.ts +1 -1
  45. package/ecagent/dist/agent-loop.js +1 -1
  46. package/ecagent/dist/agent.d.ts +1 -1
  47. package/ecagent/dist/agent.js +1 -1
  48. package/ecagent/dist/harness/agent-harness.d.ts +1 -1
  49. package/ecagent/dist/harness/compaction/branch-summarization.d.ts +1 -1
  50. package/ecagent/dist/harness/compaction/compaction.d.ts +1 -1
  51. package/ecagent/dist/harness/compaction/utils.d.ts +1 -1
  52. package/ecagent/dist/harness/messages.d.ts +1 -1
  53. package/ecagent/dist/harness/session/session.d.ts +1 -1
  54. package/ecagent/dist/harness/types.d.ts +1 -1
  55. package/ecagent/dist/proxy.d.ts +1 -1
  56. package/ecagent/dist/proxy.js +1 -1
  57. package/ecagent/dist/types.d.ts +1 -1
  58. package/kits/migrations/migrate-role-config-v5.mjs +27 -2
  59. package/kits/schemas/agent-config.schema.10.json +19 -1
  60. package/kits/schemas/daemon.schema.4.json +4 -0
  61. package/kits/schemas/defaults.schema.4.json +15 -1
  62. package/kits/schemas/relation-config.schema.8.json +22 -1
  63. package/kits/schemas/role-config.schema.1.json +2 -2
  64. package/package.json +2 -4
  65. package/ecagent/dist/agent-loop.d.ts.map +0 -1
  66. package/ecagent/dist/agent-loop.js.map +0 -1
  67. package/ecagent/dist/agent.d.ts.map +0 -1
  68. package/ecagent/dist/agent.js.map +0 -1
  69. package/ecagent/dist/harness/agent-harness.d.ts.map +0 -1
  70. package/ecagent/dist/harness/agent-harness.js.map +0 -1
  71. package/ecagent/dist/harness/compaction/branch-summarization.d.ts.map +0 -1
  72. package/ecagent/dist/harness/compaction/branch-summarization.js.map +0 -1
  73. package/ecagent/dist/harness/compaction/compaction.d.ts.map +0 -1
  74. package/ecagent/dist/harness/compaction/compaction.js.map +0 -1
  75. package/ecagent/dist/harness/compaction/utils.d.ts.map +0 -1
  76. package/ecagent/dist/harness/compaction/utils.js.map +0 -1
  77. package/ecagent/dist/harness/env/nodejs.d.ts.map +0 -1
  78. package/ecagent/dist/harness/env/nodejs.js.map +0 -1
  79. package/ecagent/dist/harness/messages.d.ts.map +0 -1
  80. package/ecagent/dist/harness/messages.js.map +0 -1
  81. package/ecagent/dist/harness/prompt-templates.d.ts.map +0 -1
  82. package/ecagent/dist/harness/prompt-templates.js.map +0 -1
  83. package/ecagent/dist/harness/session/jsonl-repo.d.ts.map +0 -1
  84. package/ecagent/dist/harness/session/jsonl-repo.js.map +0 -1
  85. package/ecagent/dist/harness/session/jsonl-storage.d.ts.map +0 -1
  86. package/ecagent/dist/harness/session/jsonl-storage.js.map +0 -1
  87. package/ecagent/dist/harness/session/memory-repo.d.ts.map +0 -1
  88. package/ecagent/dist/harness/session/memory-repo.js.map +0 -1
  89. package/ecagent/dist/harness/session/memory-storage.d.ts.map +0 -1
  90. package/ecagent/dist/harness/session/memory-storage.js.map +0 -1
  91. package/ecagent/dist/harness/session/repo-utils.d.ts.map +0 -1
  92. package/ecagent/dist/harness/session/repo-utils.js.map +0 -1
  93. package/ecagent/dist/harness/session/session.d.ts.map +0 -1
  94. package/ecagent/dist/harness/session/session.js.map +0 -1
  95. package/ecagent/dist/harness/session/uuid.d.ts.map +0 -1
  96. package/ecagent/dist/harness/session/uuid.js.map +0 -1
  97. package/ecagent/dist/harness/skills.d.ts.map +0 -1
  98. package/ecagent/dist/harness/skills.js.map +0 -1
  99. package/ecagent/dist/harness/system-prompt.d.ts.map +0 -1
  100. package/ecagent/dist/harness/system-prompt.js.map +0 -1
  101. package/ecagent/dist/harness/types.d.ts.map +0 -1
  102. package/ecagent/dist/harness/types.js.map +0 -1
  103. package/ecagent/dist/harness/utils/shell-output.d.ts.map +0 -1
  104. package/ecagent/dist/harness/utils/shell-output.js.map +0 -1
  105. package/ecagent/dist/harness/utils/truncate.d.ts.map +0 -1
  106. package/ecagent/dist/harness/utils/truncate.js.map +0 -1
  107. package/ecagent/dist/index.d.ts.map +0 -1
  108. package/ecagent/dist/index.js.map +0 -1
  109. package/ecagent/dist/node.d.ts.map +0 -1
  110. package/ecagent/dist/node.js.map +0 -1
  111. package/ecagent/dist/proxy.d.ts.map +0 -1
  112. package/ecagent/dist/proxy.js.map +0 -1
  113. package/ecagent/dist/runtime/api/lazy.d.ts.map +0 -1
  114. package/ecagent/dist/runtime/api/lazy.js.map +0 -1
  115. package/ecagent/dist/runtime/api/pi-messages.d.ts.map +0 -1
  116. package/ecagent/dist/runtime/api/pi-messages.js.map +0 -1
  117. package/ecagent/dist/runtime/api/pi-messages.lazy.d.ts.map +0 -1
  118. package/ecagent/dist/runtime/api/pi-messages.lazy.js.map +0 -1
  119. package/ecagent/dist/runtime/auth/context.d.ts.map +0 -1
  120. package/ecagent/dist/runtime/auth/context.js.map +0 -1
  121. package/ecagent/dist/runtime/auth/credential-store.d.ts.map +0 -1
  122. package/ecagent/dist/runtime/auth/credential-store.js.map +0 -1
  123. package/ecagent/dist/runtime/auth/helpers.d.ts.map +0 -1
  124. package/ecagent/dist/runtime/auth/helpers.js.map +0 -1
  125. package/ecagent/dist/runtime/auth/resolve.d.ts.map +0 -1
  126. package/ecagent/dist/runtime/auth/resolve.js.map +0 -1
  127. package/ecagent/dist/runtime/auth/types.d.ts.map +0 -1
  128. package/ecagent/dist/runtime/auth/types.js.map +0 -1
  129. package/ecagent/dist/runtime/compat.d.ts.map +0 -1
  130. package/ecagent/dist/runtime/compat.js.map +0 -1
  131. package/ecagent/dist/runtime/index.d.ts.map +0 -1
  132. package/ecagent/dist/runtime/index.js.map +0 -1
  133. package/ecagent/dist/runtime/models.d.ts.map +0 -1
  134. package/ecagent/dist/runtime/models.js.map +0 -1
  135. package/ecagent/dist/runtime/oauth.d.ts.map +0 -1
  136. package/ecagent/dist/runtime/oauth.js.map +0 -1
  137. package/ecagent/dist/runtime/providers/faux.d.ts.map +0 -1
  138. package/ecagent/dist/runtime/providers/faux.js.map +0 -1
  139. package/ecagent/dist/runtime/types.d.ts.map +0 -1
  140. package/ecagent/dist/runtime/types.js.map +0 -1
  141. package/ecagent/dist/runtime/utils/diagnostics.d.ts.map +0 -1
  142. package/ecagent/dist/runtime/utils/diagnostics.js.map +0 -1
  143. package/ecagent/dist/runtime/utils/event-stream.d.ts.map +0 -1
  144. package/ecagent/dist/runtime/utils/event-stream.js.map +0 -1
  145. package/ecagent/dist/runtime/utils/headers.d.ts.map +0 -1
  146. package/ecagent/dist/runtime/utils/headers.js.map +0 -1
  147. package/ecagent/dist/runtime/utils/json-parse.d.ts.map +0 -1
  148. package/ecagent/dist/runtime/utils/json-parse.js.map +0 -1
  149. package/ecagent/dist/runtime/utils/oauth/types.d.ts.map +0 -1
  150. package/ecagent/dist/runtime/utils/oauth/types.js.map +0 -1
  151. package/ecagent/dist/runtime/utils/overflow.d.ts.map +0 -1
  152. package/ecagent/dist/runtime/utils/overflow.js.map +0 -1
  153. package/ecagent/dist/runtime/utils/provider-env.d.ts.map +0 -1
  154. package/ecagent/dist/runtime/utils/provider-env.js.map +0 -1
  155. package/ecagent/dist/runtime/utils/retry.d.ts.map +0 -1
  156. package/ecagent/dist/runtime/utils/retry.js.map +0 -1
  157. package/ecagent/dist/runtime/utils/typebox-helpers.d.ts.map +0 -1
  158. package/ecagent/dist/runtime/utils/typebox-helpers.js.map +0 -1
  159. package/ecagent/dist/runtime/utils/validation.d.ts.map +0 -1
  160. package/ecagent/dist/runtime/utils/validation.js.map +0 -1
  161. package/ecagent/dist/types.d.ts.map +0 -1
  162. package/ecagent/dist/types.js.map +0 -1
  163. package/ecagent/package.json +0 -93
  164. package/ecagent/src/agent-loop.ts +0 -792
  165. package/ecagent/src/agent.ts +0 -575
  166. package/ecagent/src/harness/agent-harness.ts +0 -1029
  167. package/ecagent/src/harness/compaction/branch-summarization.ts +0 -261
  168. package/ecagent/src/harness/compaction/compaction.ts +0 -753
  169. package/ecagent/src/harness/compaction/utils.ts +0 -144
  170. package/ecagent/src/harness/env/nodejs.ts +0 -569
  171. package/ecagent/src/harness/messages.ts +0 -164
  172. package/ecagent/src/harness/prompt-templates.ts +0 -267
  173. package/ecagent/src/harness/session/jsonl-repo.ts +0 -179
  174. package/ecagent/src/harness/session/jsonl-storage.ts +0 -314
  175. package/ecagent/src/harness/session/memory-repo.ts +0 -50
  176. package/ecagent/src/harness/session/memory-storage.ts +0 -133
  177. package/ecagent/src/harness/session/repo-utils.ts +0 -51
  178. package/ecagent/src/harness/session/session.ts +0 -338
  179. package/ecagent/src/harness/session/uuid.ts +0 -54
  180. package/ecagent/src/harness/skills.ts +0 -375
  181. package/ecagent/src/harness/system-prompt.ts +0 -34
  182. package/ecagent/src/harness/types.ts +0 -838
  183. package/ecagent/src/harness/utils/shell-output.ts +0 -135
  184. package/ecagent/src/harness/utils/truncate.ts +0 -344
  185. package/ecagent/src/index.ts +0 -46
  186. package/ecagent/src/node.ts +0 -2
  187. package/ecagent/src/proxy.ts +0 -367
  188. package/ecagent/src/runtime/api/lazy.ts +0 -70
  189. package/ecagent/src/runtime/api/pi-messages.lazy.ts +0 -4
  190. package/ecagent/src/runtime/api/pi-messages.ts +0 -436
  191. package/ecagent/src/runtime/auth/context.ts +0 -45
  192. package/ecagent/src/runtime/auth/credential-store.ts +0 -47
  193. package/ecagent/src/runtime/auth/helpers.ts +0 -46
  194. package/ecagent/src/runtime/auth/resolve.ts +0 -141
  195. package/ecagent/src/runtime/auth/types.ts +0 -182
  196. package/ecagent/src/runtime/compat.ts +0 -158
  197. package/ecagent/src/runtime/index.ts +0 -31
  198. package/ecagent/src/runtime/models.ts +0 -452
  199. package/ecagent/src/runtime/oauth.ts +0 -1
  200. package/ecagent/src/runtime/providers/faux.ts +0 -538
  201. package/ecagent/src/runtime/types.ts +0 -718
  202. package/ecagent/src/runtime/utils/diagnostics.ts +0 -45
  203. package/ecagent/src/runtime/utils/event-stream.ts +0 -88
  204. package/ecagent/src/runtime/utils/headers.ts +0 -18
  205. package/ecagent/src/runtime/utils/json-parse.ts +0 -124
  206. package/ecagent/src/runtime/utils/oauth/types.ts +0 -79
  207. package/ecagent/src/runtime/utils/overflow.ts +0 -165
  208. package/ecagent/src/runtime/utils/provider-env.ts +0 -52
  209. package/ecagent/src/runtime/utils/retry.ts +0 -101
  210. package/ecagent/src/runtime/utils/typebox-helpers.ts +0 -24
  211. package/ecagent/src/runtime/utils/validation.ts +0 -310
  212. package/ecagent/src/types.ts +0 -430
  213. package/ecagent/tsconfig.build.json +0 -32
@@ -1,24 +0,0 @@
1
- import { type TUnsafe, Type } from "typebox";
2
-
3
- /**
4
- * Creates a string enum schema compatible with Google's API and other providers
5
- * that don't support anyOf/const patterns.
6
- *
7
- * @example
8
- * const OperationSchema = StringEnum(["add", "subtract", "multiply", "divide"], {
9
- * description: "The operation to perform"
10
- * });
11
- *
12
- * type Operation = Static<typeof OperationSchema>; // "add" | "subtract" | "multiply" | "divide"
13
- */
14
- export function StringEnum<T extends readonly string[]>(
15
- values: T,
16
- options?: { description?: string; default?: T[number] },
17
- ): TUnsafe<T[number]> {
18
- return Type.Unsafe<T[number]>({
19
- type: "string",
20
- enum: values as any,
21
- ...(options?.description && { description: options.description }),
22
- ...(options?.default && { default: options.default }),
23
- });
24
- }
@@ -1,310 +0,0 @@
1
- import { Compile } from "typebox/compile";
2
- import type { TLocalizedValidationError } from "typebox/error";
3
- import { Value } from "typebox/value";
4
- import type { Tool, ToolCall } from "../types.ts";
5
-
6
- const validatorCache = new WeakMap<object, ReturnType<typeof Compile>>();
7
- const TYPEBOX_KIND = Symbol.for("TypeBox.Kind");
8
-
9
- interface JsonSchemaObject {
10
- type?: string | string[];
11
- properties?: Record<string, JsonSchemaObject>;
12
- items?: JsonSchemaObject | JsonSchemaObject[];
13
- additionalProperties?: boolean | JsonSchemaObject;
14
- allOf?: JsonSchemaObject[];
15
- anyOf?: JsonSchemaObject[];
16
- oneOf?: JsonSchemaObject[];
17
- }
18
-
19
- function getSchemaTypes(schema: JsonSchemaObject): string[] {
20
- if (typeof schema.type === "string") {
21
- return [schema.type];
22
- }
23
- if (Array.isArray(schema.type)) {
24
- return schema.type.filter((type): type is string => typeof type === "string");
25
- }
26
- return [];
27
- }
28
-
29
- function matchesJsonType(value: unknown, type: string): boolean {
30
- switch (type) {
31
- case "number":
32
- return typeof value === "number";
33
- case "integer":
34
- return typeof value === "number" && Number.isInteger(value);
35
- case "boolean":
36
- return typeof value === "boolean";
37
- case "string":
38
- return typeof value === "string";
39
- case "null":
40
- return value === null;
41
- case "array":
42
- return Array.isArray(value);
43
- case "object":
44
- return typeof value === "object" && value !== null && !Array.isArray(value);
45
- default:
46
- return false;
47
- }
48
- }
49
-
50
- function getSubSchemaValidator(schema: JsonSchemaObject): ReturnType<typeof Compile> | undefined {
51
- try {
52
- return getValidator(schema as Tool["parameters"]);
53
- } catch {
54
- return undefined;
55
- }
56
- }
57
-
58
- function coercePrimitiveByType(value: unknown, type: string): unknown {
59
- switch (type) {
60
- case "number": {
61
- if (value === null) {
62
- return 0;
63
- }
64
- if (typeof value === "string" && value.trim() !== "") {
65
- const parsed = Number(value);
66
- if (Number.isFinite(parsed)) {
67
- return parsed;
68
- }
69
- }
70
- if (typeof value === "boolean") {
71
- return value ? 1 : 0;
72
- }
73
- return value;
74
- }
75
- case "integer": {
76
- if (value === null) {
77
- return 0;
78
- }
79
- if (typeof value === "string" && value.trim() !== "") {
80
- const parsed = Number(value);
81
- if (Number.isInteger(parsed)) {
82
- return parsed;
83
- }
84
- }
85
- if (typeof value === "boolean") {
86
- return value ? 1 : 0;
87
- }
88
- return value;
89
- }
90
- case "boolean": {
91
- if (value === null) {
92
- return false;
93
- }
94
- if (typeof value === "string") {
95
- if (value === "true") {
96
- return true;
97
- }
98
- if (value === "false") {
99
- return false;
100
- }
101
- }
102
- if (typeof value === "number") {
103
- if (value === 1) {
104
- return true;
105
- }
106
- if (value === 0) {
107
- return false;
108
- }
109
- }
110
- return value;
111
- }
112
- case "string": {
113
- if (value === null) {
114
- return "";
115
- }
116
- if (typeof value === "number" || typeof value === "boolean") {
117
- return String(value);
118
- }
119
- return value;
120
- }
121
- case "null": {
122
- if (value === "" || value === 0 || value === false) {
123
- return null;
124
- }
125
- return value;
126
- }
127
- default:
128
- return value;
129
- }
130
- }
131
-
132
- function applySchemaObjectCoercion(value: Record<string, unknown>, schema: JsonSchemaObject): void {
133
- const properties = schema.properties;
134
- const definedKeys = new Set<string>(properties ? Object.keys(properties) : []);
135
-
136
- if (properties) {
137
- for (const [key, propertySchema] of Object.entries(properties)) {
138
- if (!(key in value)) {
139
- continue;
140
- }
141
- value[key] = coerceWithJsonSchema(value[key], propertySchema);
142
- }
143
- }
144
-
145
- if (schema.additionalProperties && typeof schema.additionalProperties === "object") {
146
- for (const [key, propertyValue] of Object.entries(value)) {
147
- if (definedKeys.has(key)) {
148
- continue;
149
- }
150
- value[key] = coerceWithJsonSchema(propertyValue, schema.additionalProperties);
151
- }
152
- }
153
- }
154
-
155
- function applySchemaArrayCoercion(value: unknown[], schema: JsonSchemaObject): void {
156
- if (Array.isArray(schema.items)) {
157
- for (let index = 0; index < value.length; index++) {
158
- const itemSchema = schema.items[index];
159
- if (!itemSchema) {
160
- continue;
161
- }
162
- value[index] = coerceWithJsonSchema(value[index], itemSchema);
163
- }
164
- return;
165
- }
166
-
167
- if (schema.items && typeof schema.items === "object") {
168
- for (let index = 0; index < value.length; index++) {
169
- value[index] = coerceWithJsonSchema(value[index], schema.items);
170
- }
171
- }
172
- }
173
-
174
- function coerceWithUnionSchema(value: unknown, schemas: JsonSchemaObject[]): unknown {
175
- for (const schema of schemas) {
176
- const candidate = structuredClone(value);
177
- const coerced = coerceWithJsonSchema(candidate, schema);
178
- const validator = getSubSchemaValidator(schema);
179
- if (validator?.Check(coerced)) {
180
- return coerced;
181
- }
182
- }
183
- return value;
184
- }
185
-
186
- function coerceWithJsonSchema(value: unknown, schema: JsonSchemaObject): unknown {
187
- let nextValue = value;
188
-
189
- if (Array.isArray(schema.allOf)) {
190
- for (const nested of schema.allOf) {
191
- nextValue = coerceWithJsonSchema(nextValue, nested);
192
- }
193
- }
194
-
195
- if (Array.isArray(schema.anyOf)) {
196
- nextValue = coerceWithUnionSchema(nextValue, schema.anyOf);
197
- }
198
-
199
- if (Array.isArray(schema.oneOf)) {
200
- nextValue = coerceWithUnionSchema(nextValue, schema.oneOf);
201
- }
202
-
203
- const schemaTypes = getSchemaTypes(schema);
204
- const matchesUnionMember =
205
- schemaTypes.length > 1 && schemaTypes.some((schemaType) => matchesJsonType(nextValue, schemaType));
206
- if (schemaTypes.length > 0 && !matchesUnionMember) {
207
- for (const schemaType of schemaTypes) {
208
- const candidate = coercePrimitiveByType(nextValue, schemaType);
209
- if (candidate !== nextValue) {
210
- nextValue = candidate;
211
- break;
212
- }
213
- }
214
- }
215
-
216
- if (
217
- schemaTypes.includes("object") &&
218
- typeof nextValue === "object" &&
219
- nextValue !== null &&
220
- !Array.isArray(nextValue)
221
- ) {
222
- applySchemaObjectCoercion(nextValue as Record<string, unknown>, schema);
223
- }
224
-
225
- if (schemaTypes.includes("array") && Array.isArray(nextValue)) {
226
- applySchemaArrayCoercion(nextValue, schema);
227
- }
228
-
229
- return nextValue;
230
- }
231
-
232
- function getValidator(schema: Tool["parameters"]): ReturnType<typeof Compile> {
233
- const key = schema as object;
234
- const cached = validatorCache.get(key);
235
- if (cached) {
236
- return cached;
237
- }
238
- const validator = Compile(schema);
239
- validatorCache.set(key, validator);
240
- return validator;
241
- }
242
-
243
- function formatValidationPath(error: TLocalizedValidationError): string {
244
- if (error.keyword === "required") {
245
- const requiredProperties = (error.params as { requiredProperties?: string[] }).requiredProperties;
246
- const requiredProperty = requiredProperties?.[0];
247
- if (requiredProperty) {
248
- const basePath = error.instancePath.replace(/^\//, "").replace(/\//g, ".");
249
- return basePath ? `${basePath}.${requiredProperty}` : requiredProperty;
250
- }
251
- }
252
- const path = error.instancePath.replace(/^\//, "").replace(/\//g, ".");
253
- return path || "root";
254
- }
255
-
256
- /**
257
- * Finds a tool by name and validates the tool call arguments against its TypeBox schema
258
- * @param tools Array of tool definitions
259
- * @param toolCall The tool call from the LLM
260
- * @returns The validated arguments
261
- * @throws Error if tool is not found or validation fails
262
- */
263
- export function validateToolCall(tools: Tool[], toolCall: ToolCall): any {
264
- const tool = tools.find((t) => t.name === toolCall.name);
265
- if (!tool) {
266
- throw new Error(`Tool "${toolCall.name}" not found`);
267
- }
268
- return validateToolArguments(tool, toolCall);
269
- }
270
-
271
- /**
272
- * Validates tool call arguments against the tool's TypeBox schema
273
- * @param tool The tool definition with TypeBox schema
274
- * @param toolCall The tool call from the LLM
275
- * @returns The validated (and potentially coerced) arguments
276
- * @throws Error with formatted message if validation fails
277
- */
278
- export function validateToolArguments(tool: Tool, toolCall: ToolCall): any {
279
- const args = structuredClone(toolCall.arguments);
280
- Value.Convert(tool.parameters, args);
281
-
282
- const validator = getValidator(tool.parameters);
283
- if (!Object.getOwnPropertySymbols(tool.parameters).includes(TYPEBOX_KIND)) {
284
- const coerced = coerceWithJsonSchema(args, tool.parameters as JsonSchemaObject);
285
- if (coerced !== args) {
286
- if (typeof args === "object" && args !== null && typeof coerced === "object" && coerced !== null) {
287
- for (const key of Object.keys(args)) {
288
- delete args[key];
289
- }
290
- Object.assign(args, coerced);
291
- } else {
292
- return validator.Check(coerced) ? coerced : args;
293
- }
294
- }
295
- }
296
-
297
- if (validator.Check(args)) {
298
- return args;
299
- }
300
-
301
- const errors =
302
- validator
303
- .Errors(args)
304
- .map((error) => ` - ${formatValidationPath(error)}: ${error.message}`)
305
- .join("\n") || "Unknown validation error";
306
-
307
- const errorMessage = `Validation failed for tool "${toolCall.name}":\n${errors}\n\nReceived arguments:\n${JSON.stringify(toolCall.arguments, null, 2)}`;
308
-
309
- throw new Error(errorMessage);
310
- }