@threadwell/ai 0.0.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 (178) hide show
  1. package/README.md +1313 -0
  2. package/dist/api-registry.d.ts +20 -0
  3. package/dist/api-registry.d.ts.map +1 -0
  4. package/dist/api-registry.js +44 -0
  5. package/dist/api-registry.js.map +1 -0
  6. package/dist/bedrock-provider.d.ts +5 -0
  7. package/dist/bedrock-provider.d.ts.map +1 -0
  8. package/dist/bedrock-provider.js +6 -0
  9. package/dist/bedrock-provider.js.map +1 -0
  10. package/dist/cli.d.ts +3 -0
  11. package/dist/cli.d.ts.map +1 -0
  12. package/dist/cli.js +116 -0
  13. package/dist/cli.js.map +1 -0
  14. package/dist/env-api-keys.d.ts +18 -0
  15. package/dist/env-api-keys.d.ts.map +1 -0
  16. package/dist/env-api-keys.js +169 -0
  17. package/dist/env-api-keys.js.map +1 -0
  18. package/dist/index.d.ts +28 -0
  19. package/dist/index.d.ts.map +1 -0
  20. package/dist/index.js +16 -0
  21. package/dist/index.js.map +1 -0
  22. package/dist/models.d.ts +18 -0
  23. package/dist/models.d.ts.map +1 -0
  24. package/dist/models.generated.d.ts +16993 -0
  25. package/dist/models.generated.d.ts.map +1 -0
  26. package/dist/models.generated.js +16228 -0
  27. package/dist/models.generated.js.map +1 -0
  28. package/dist/models.js +71 -0
  29. package/dist/models.js.map +1 -0
  30. package/dist/oauth.d.ts +2 -0
  31. package/dist/oauth.d.ts.map +1 -0
  32. package/dist/oauth.js +2 -0
  33. package/dist/oauth.js.map +1 -0
  34. package/dist/providers/amazon-bedrock.d.ts +38 -0
  35. package/dist/providers/amazon-bedrock.d.ts.map +1 -0
  36. package/dist/providers/amazon-bedrock.js +759 -0
  37. package/dist/providers/amazon-bedrock.js.map +1 -0
  38. package/dist/providers/anthropic.d.ts +54 -0
  39. package/dist/providers/anthropic.d.ts.map +1 -0
  40. package/dist/providers/anthropic.js +951 -0
  41. package/dist/providers/anthropic.js.map +1 -0
  42. package/dist/providers/azure-openai-responses.d.ts +15 -0
  43. package/dist/providers/azure-openai-responses.d.ts.map +1 -0
  44. package/dist/providers/azure-openai-responses.js +208 -0
  45. package/dist/providers/azure-openai-responses.js.map +1 -0
  46. package/dist/providers/cloudflare.d.ts +13 -0
  47. package/dist/providers/cloudflare.d.ts.map +1 -0
  48. package/dist/providers/cloudflare.js +26 -0
  49. package/dist/providers/cloudflare.js.map +1 -0
  50. package/dist/providers/faux.d.ts +56 -0
  51. package/dist/providers/faux.d.ts.map +1 -0
  52. package/dist/providers/faux.js +368 -0
  53. package/dist/providers/faux.js.map +1 -0
  54. package/dist/providers/github-copilot-headers.d.ts +8 -0
  55. package/dist/providers/github-copilot-headers.d.ts.map +1 -0
  56. package/dist/providers/github-copilot-headers.js +29 -0
  57. package/dist/providers/github-copilot-headers.js.map +1 -0
  58. package/dist/providers/google-shared.d.ts +70 -0
  59. package/dist/providers/google-shared.d.ts.map +1 -0
  60. package/dist/providers/google-shared.js +329 -0
  61. package/dist/providers/google-shared.js.map +1 -0
  62. package/dist/providers/google-vertex.d.ts +15 -0
  63. package/dist/providers/google-vertex.d.ts.map +1 -0
  64. package/dist/providers/google-vertex.js +442 -0
  65. package/dist/providers/google-vertex.js.map +1 -0
  66. package/dist/providers/google.d.ts +13 -0
  67. package/dist/providers/google.d.ts.map +1 -0
  68. package/dist/providers/google.js +400 -0
  69. package/dist/providers/google.js.map +1 -0
  70. package/dist/providers/mistral.d.ts +25 -0
  71. package/dist/providers/mistral.d.ts.map +1 -0
  72. package/dist/providers/mistral.js +535 -0
  73. package/dist/providers/mistral.js.map +1 -0
  74. package/dist/providers/openai-codex-responses.d.ts +30 -0
  75. package/dist/providers/openai-codex-responses.d.ts.map +1 -0
  76. package/dist/providers/openai-codex-responses.js +1034 -0
  77. package/dist/providers/openai-codex-responses.js.map +1 -0
  78. package/dist/providers/openai-completions.d.ts +19 -0
  79. package/dist/providers/openai-completions.d.ts.map +1 -0
  80. package/dist/providers/openai-completions.js +925 -0
  81. package/dist/providers/openai-completions.js.map +1 -0
  82. package/dist/providers/openai-responses-shared.d.ts +18 -0
  83. package/dist/providers/openai-responses-shared.d.ts.map +1 -0
  84. package/dist/providers/openai-responses-shared.js +492 -0
  85. package/dist/providers/openai-responses-shared.js.map +1 -0
  86. package/dist/providers/openai-responses.d.ts +13 -0
  87. package/dist/providers/openai-responses.d.ts.map +1 -0
  88. package/dist/providers/openai-responses.js +220 -0
  89. package/dist/providers/openai-responses.js.map +1 -0
  90. package/dist/providers/register-builtins.d.ts +35 -0
  91. package/dist/providers/register-builtins.d.ts.map +1 -0
  92. package/dist/providers/register-builtins.js +243 -0
  93. package/dist/providers/register-builtins.js.map +1 -0
  94. package/dist/providers/simple-options.d.ts +8 -0
  95. package/dist/providers/simple-options.d.ts.map +1 -0
  96. package/dist/providers/simple-options.js +39 -0
  97. package/dist/providers/simple-options.js.map +1 -0
  98. package/dist/providers/transform-messages.d.ts +8 -0
  99. package/dist/providers/transform-messages.d.ts.map +1 -0
  100. package/dist/providers/transform-messages.js +184 -0
  101. package/dist/providers/transform-messages.js.map +1 -0
  102. package/dist/session-resources.d.ts +4 -0
  103. package/dist/session-resources.d.ts.map +1 -0
  104. package/dist/session-resources.js +22 -0
  105. package/dist/session-resources.js.map +1 -0
  106. package/dist/stream.d.ts +8 -0
  107. package/dist/stream.d.ts.map +1 -0
  108. package/dist/stream.js +27 -0
  109. package/dist/stream.js.map +1 -0
  110. package/dist/types.d.ts +405 -0
  111. package/dist/types.d.ts.map +1 -0
  112. package/dist/types.js +2 -0
  113. package/dist/types.js.map +1 -0
  114. package/dist/utils/diagnostics.d.ts +19 -0
  115. package/dist/utils/diagnostics.d.ts.map +1 -0
  116. package/dist/utils/diagnostics.js +25 -0
  117. package/dist/utils/diagnostics.js.map +1 -0
  118. package/dist/utils/event-stream.d.ts +21 -0
  119. package/dist/utils/event-stream.d.ts.map +1 -0
  120. package/dist/utils/event-stream.js +81 -0
  121. package/dist/utils/event-stream.js.map +1 -0
  122. package/dist/utils/hash.d.ts +3 -0
  123. package/dist/utils/hash.d.ts.map +1 -0
  124. package/dist/utils/hash.js +14 -0
  125. package/dist/utils/hash.js.map +1 -0
  126. package/dist/utils/headers.d.ts +2 -0
  127. package/dist/utils/headers.d.ts.map +1 -0
  128. package/dist/utils/headers.js +8 -0
  129. package/dist/utils/headers.js.map +1 -0
  130. package/dist/utils/json-parse.d.ts +16 -0
  131. package/dist/utils/json-parse.d.ts.map +1 -0
  132. package/dist/utils/json-parse.js +113 -0
  133. package/dist/utils/json-parse.js.map +1 -0
  134. package/dist/utils/oauth/anthropic.d.ts +25 -0
  135. package/dist/utils/oauth/anthropic.d.ts.map +1 -0
  136. package/dist/utils/oauth/anthropic.js +335 -0
  137. package/dist/utils/oauth/anthropic.js.map +1 -0
  138. package/dist/utils/oauth/github-copilot.d.ts +30 -0
  139. package/dist/utils/oauth/github-copilot.d.ts.map +1 -0
  140. package/dist/utils/oauth/github-copilot.js +292 -0
  141. package/dist/utils/oauth/github-copilot.js.map +1 -0
  142. package/dist/utils/oauth/index.d.ts +57 -0
  143. package/dist/utils/oauth/index.d.ts.map +1 -0
  144. package/dist/utils/oauth/index.js +121 -0
  145. package/dist/utils/oauth/index.js.map +1 -0
  146. package/dist/utils/oauth/oauth-page.d.ts +3 -0
  147. package/dist/utils/oauth/oauth-page.d.ts.map +1 -0
  148. package/dist/utils/oauth/oauth-page.js +105 -0
  149. package/dist/utils/oauth/oauth-page.js.map +1 -0
  150. package/dist/utils/oauth/openai-codex.d.ts +34 -0
  151. package/dist/utils/oauth/openai-codex.d.ts.map +1 -0
  152. package/dist/utils/oauth/openai-codex.js +385 -0
  153. package/dist/utils/oauth/openai-codex.js.map +1 -0
  154. package/dist/utils/oauth/pkce.d.ts +13 -0
  155. package/dist/utils/oauth/pkce.d.ts.map +1 -0
  156. package/dist/utils/oauth/pkce.js +31 -0
  157. package/dist/utils/oauth/pkce.js.map +1 -0
  158. package/dist/utils/oauth/types.d.ts +57 -0
  159. package/dist/utils/oauth/types.d.ts.map +1 -0
  160. package/dist/utils/oauth/types.js +2 -0
  161. package/dist/utils/oauth/types.js.map +1 -0
  162. package/dist/utils/overflow.d.ts +55 -0
  163. package/dist/utils/overflow.d.ts.map +1 -0
  164. package/dist/utils/overflow.js +146 -0
  165. package/dist/utils/overflow.js.map +1 -0
  166. package/dist/utils/sanitize-unicode.d.ts +22 -0
  167. package/dist/utils/sanitize-unicode.d.ts.map +1 -0
  168. package/dist/utils/sanitize-unicode.js +26 -0
  169. package/dist/utils/sanitize-unicode.js.map +1 -0
  170. package/dist/utils/typebox-helpers.d.ts +17 -0
  171. package/dist/utils/typebox-helpers.d.ts.map +1 -0
  172. package/dist/utils/typebox-helpers.js +21 -0
  173. package/dist/utils/typebox-helpers.js.map +1 -0
  174. package/dist/utils/validation.d.ts +18 -0
  175. package/dist/utils/validation.d.ts.map +1 -0
  176. package/dist/utils/validation.js +281 -0
  177. package/dist/utils/validation.js.map +1 -0
  178. package/package.json +108 -0
@@ -0,0 +1,281 @@
1
+ import { Compile } from "typebox/compile";
2
+ import { Value } from "typebox/value";
3
+ const validatorCache = new WeakMap();
4
+ const TYPEBOX_KIND = Symbol.for("TypeBox.Kind");
5
+ function isRecord(value) {
6
+ return typeof value === "object" && value !== null;
7
+ }
8
+ function isJsonSchemaObject(value) {
9
+ return isRecord(value);
10
+ }
11
+ function hasTypeBoxMetadata(schema) {
12
+ return isRecord(schema) && Object.getOwnPropertySymbols(schema).includes(TYPEBOX_KIND);
13
+ }
14
+ function getSchemaTypes(schema) {
15
+ if (typeof schema.type === "string") {
16
+ return [schema.type];
17
+ }
18
+ if (Array.isArray(schema.type)) {
19
+ return schema.type.filter((type) => typeof type === "string");
20
+ }
21
+ return [];
22
+ }
23
+ function matchesJsonType(value, type) {
24
+ switch (type) {
25
+ case "number":
26
+ return typeof value === "number";
27
+ case "integer":
28
+ return typeof value === "number" && Number.isInteger(value);
29
+ case "boolean":
30
+ return typeof value === "boolean";
31
+ case "string":
32
+ return typeof value === "string";
33
+ case "null":
34
+ return value === null;
35
+ case "array":
36
+ return Array.isArray(value);
37
+ case "object":
38
+ return isRecord(value) && !Array.isArray(value);
39
+ default:
40
+ return false;
41
+ }
42
+ }
43
+ function isValidatorSchema(value) {
44
+ return isRecord(value);
45
+ }
46
+ function getSubSchemaValidator(schema) {
47
+ if (!isValidatorSchema(schema)) {
48
+ return undefined;
49
+ }
50
+ try {
51
+ return getValidator(schema);
52
+ }
53
+ catch {
54
+ return undefined;
55
+ }
56
+ }
57
+ function coercePrimitiveByType(value, type) {
58
+ switch (type) {
59
+ case "number": {
60
+ if (value === null) {
61
+ return 0;
62
+ }
63
+ if (typeof value === "string" && value.trim() !== "") {
64
+ const parsed = Number(value);
65
+ if (Number.isFinite(parsed)) {
66
+ return parsed;
67
+ }
68
+ }
69
+ if (typeof value === "boolean") {
70
+ return value ? 1 : 0;
71
+ }
72
+ return value;
73
+ }
74
+ case "integer": {
75
+ if (value === null) {
76
+ return 0;
77
+ }
78
+ if (typeof value === "string" && value.trim() !== "") {
79
+ const parsed = Number(value);
80
+ if (Number.isInteger(parsed)) {
81
+ return parsed;
82
+ }
83
+ }
84
+ if (typeof value === "boolean") {
85
+ return value ? 1 : 0;
86
+ }
87
+ return value;
88
+ }
89
+ case "boolean": {
90
+ if (value === null) {
91
+ return false;
92
+ }
93
+ if (typeof value === "string") {
94
+ if (value === "true") {
95
+ return true;
96
+ }
97
+ if (value === "false") {
98
+ return false;
99
+ }
100
+ }
101
+ if (typeof value === "number") {
102
+ if (value === 1) {
103
+ return true;
104
+ }
105
+ if (value === 0) {
106
+ return false;
107
+ }
108
+ }
109
+ return value;
110
+ }
111
+ case "string": {
112
+ if (value === null) {
113
+ return "";
114
+ }
115
+ if (typeof value === "number" || typeof value === "boolean") {
116
+ return String(value);
117
+ }
118
+ return value;
119
+ }
120
+ case "null": {
121
+ if (value === "" || value === 0 || value === false) {
122
+ return null;
123
+ }
124
+ return value;
125
+ }
126
+ default:
127
+ return value;
128
+ }
129
+ }
130
+ function applySchemaObjectCoercion(value, schema) {
131
+ const properties = schema.properties;
132
+ const definedKeys = new Set(properties ? Object.keys(properties) : []);
133
+ if (properties) {
134
+ for (const [key, propertySchema] of Object.entries(properties)) {
135
+ if (!(key in value)) {
136
+ continue;
137
+ }
138
+ value[key] = coerceWithJsonSchema(value[key], propertySchema);
139
+ }
140
+ }
141
+ if (schema.additionalProperties && isJsonSchemaObject(schema.additionalProperties)) {
142
+ for (const [key, propertyValue] of Object.entries(value)) {
143
+ if (definedKeys.has(key)) {
144
+ continue;
145
+ }
146
+ value[key] = coerceWithJsonSchema(propertyValue, schema.additionalProperties);
147
+ }
148
+ }
149
+ }
150
+ function applySchemaArrayCoercion(value, schema) {
151
+ if (Array.isArray(schema.items)) {
152
+ for (let index = 0; index < value.length; index++) {
153
+ const itemSchema = schema.items[index];
154
+ if (!itemSchema) {
155
+ continue;
156
+ }
157
+ value[index] = coerceWithJsonSchema(value[index], itemSchema);
158
+ }
159
+ return;
160
+ }
161
+ if (isJsonSchemaObject(schema.items)) {
162
+ for (let index = 0; index < value.length; index++) {
163
+ value[index] = coerceWithJsonSchema(value[index], schema.items);
164
+ }
165
+ }
166
+ }
167
+ function coerceWithUnionSchema(value, schemas) {
168
+ for (const schema of schemas) {
169
+ const candidate = structuredClone(value);
170
+ const coerced = coerceWithJsonSchema(candidate, schema);
171
+ const validator = getSubSchemaValidator(schema);
172
+ if (validator?.Check(coerced)) {
173
+ return coerced;
174
+ }
175
+ }
176
+ return value;
177
+ }
178
+ function coerceWithJsonSchema(value, schema) {
179
+ let nextValue = value;
180
+ if (Array.isArray(schema.allOf)) {
181
+ for (const nested of schema.allOf) {
182
+ nextValue = coerceWithJsonSchema(nextValue, nested);
183
+ }
184
+ }
185
+ if (Array.isArray(schema.anyOf)) {
186
+ nextValue = coerceWithUnionSchema(nextValue, schema.anyOf);
187
+ }
188
+ if (Array.isArray(schema.oneOf)) {
189
+ nextValue = coerceWithUnionSchema(nextValue, schema.oneOf);
190
+ }
191
+ const schemaTypes = getSchemaTypes(schema);
192
+ const matchesUnionMember = schemaTypes.length > 1 && schemaTypes.some((schemaType) => matchesJsonType(nextValue, schemaType));
193
+ if (schemaTypes.length > 0 && !matchesUnionMember) {
194
+ for (const schemaType of schemaTypes) {
195
+ const candidate = coercePrimitiveByType(nextValue, schemaType);
196
+ if (candidate !== nextValue) {
197
+ nextValue = candidate;
198
+ break;
199
+ }
200
+ }
201
+ }
202
+ if (schemaTypes.includes("object") && isRecord(nextValue) && !Array.isArray(nextValue)) {
203
+ applySchemaObjectCoercion(nextValue, schema);
204
+ }
205
+ if (schemaTypes.includes("array") && Array.isArray(nextValue)) {
206
+ applySchemaArrayCoercion(nextValue, schema);
207
+ }
208
+ return nextValue;
209
+ }
210
+ function getValidator(schema) {
211
+ const key = schema;
212
+ const cached = validatorCache.get(key);
213
+ if (cached) {
214
+ return cached;
215
+ }
216
+ const validator = Compile(schema);
217
+ validatorCache.set(key, validator);
218
+ return validator;
219
+ }
220
+ function formatValidationPath(error) {
221
+ if (error.keyword === "required") {
222
+ const requiredProperties = error.params.requiredProperties;
223
+ const requiredProperty = requiredProperties?.[0];
224
+ if (requiredProperty) {
225
+ const basePath = error.instancePath.replace(/^\//, "").replace(/\//g, ".");
226
+ return basePath ? `${basePath}.${requiredProperty}` : requiredProperty;
227
+ }
228
+ }
229
+ const path = error.instancePath.replace(/^\//, "").replace(/\//g, ".");
230
+ return path || "root";
231
+ }
232
+ /**
233
+ * Finds a tool by name and validates the tool call arguments against its TypeBox schema
234
+ * @param tools Array of tool definitions
235
+ * @param toolCall The tool call from the LLM
236
+ * @returns The validated arguments
237
+ * @throws Error if tool is not found or validation fails
238
+ */
239
+ export function validateToolCall(tools, toolCall) {
240
+ const tool = tools.find((t) => t.name === toolCall.name);
241
+ if (!tool) {
242
+ throw new Error(`Tool "${toolCall.name}" not found`);
243
+ }
244
+ return validateToolArguments(tool, toolCall);
245
+ }
246
+ /**
247
+ * Validates tool call arguments against the tool's TypeBox schema
248
+ * @param tool The tool definition with TypeBox schema
249
+ * @param toolCall The tool call from the LLM
250
+ * @returns The validated (and potentially coerced) arguments
251
+ * @throws Error with formatted message if validation fails
252
+ */
253
+ export function validateToolArguments(tool, toolCall) {
254
+ const args = structuredClone(toolCall.arguments);
255
+ Value.Convert(tool.parameters, args);
256
+ const validator = getValidator(tool.parameters);
257
+ if (!hasTypeBoxMetadata(tool.parameters) && isJsonSchemaObject(tool.parameters)) {
258
+ const coerced = coerceWithJsonSchema(args, tool.parameters);
259
+ if (coerced !== args) {
260
+ if (isRecord(args) && isRecord(coerced)) {
261
+ for (const key of Object.keys(args)) {
262
+ delete args[key];
263
+ }
264
+ Object.assign(args, coerced);
265
+ }
266
+ else {
267
+ return validator.Check(coerced) ? coerced : args;
268
+ }
269
+ }
270
+ }
271
+ if (validator.Check(args)) {
272
+ return args;
273
+ }
274
+ const errors = validator
275
+ .Errors(args)
276
+ .map((error) => ` - ${formatValidationPath(error)}: ${error.message}`)
277
+ .join("\n") || "Unknown validation error";
278
+ const errorMessage = `Validation failed for tool "${toolCall.name}":\n${errors}\n\nReceived arguments:\n${JSON.stringify(toolCall.arguments, null, 2)}`;
279
+ throw new Error(errorMessage);
280
+ }
281
+ //# sourceMappingURL=validation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validation.js","sourceRoot":"","sources":["../../src/utils/validation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAE1C,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAGtC,MAAM,cAAc,GAAG,IAAI,OAAO,EAAsC,CAAC;AACzE,MAAM,YAAY,GAAG,MAAM,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC;AAYhD,SAAS,QAAQ,CAAC,KAAc,EAAoC;IACnE,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,CAAC;AAAA,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AAC;gBACD,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;YAC9B,CAAC;iBAAM,CAAC;gBACP,OAAO,SAAS,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC;YAClD,CAAC;QACF,CAAC;IACF,CAAC;IAED,IAAI,SAAS,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC;QAC3B,OAAO,IAAI,CAAC;IACb,CAAC;IAED,MAAM,MAAM,GACX,SAAS;SACP,MAAM,CAAC,IAAI,CAAC;SACZ,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,OAAO,oBAAoB,CAAC,KAAK,CAAC,KAAK,KAAK,CAAC,OAAO,EAAE,CAAC;SACtE,IAAI,CAAC,IAAI,CAAC,IAAI,0BAA0B,CAAC;IAE5C,MAAM,YAAY,GAAG,+BAA+B,QAAQ,CAAC,IAAI,OAAO,MAAM,4BAA4B,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC;IAExJ,MAAM,IAAI,KAAK,CAAC,YAAY,CAAC,CAAC;AAAA,CAC9B","sourcesContent":["import { Compile } from \"typebox/compile\";\nimport type { TLocalizedValidationError } from \"typebox/error\";\nimport { Value } from \"typebox/value\";\nimport type { Tool, ToolCall } from \"../types.js\";\n\nconst validatorCache = new WeakMap<object, ReturnType<typeof Compile>>();\nconst TYPEBOX_KIND = Symbol.for(\"TypeBox.Kind\");\n\ninterface JsonSchemaObject {\n\ttype?: string | string[];\n\tproperties?: Record<string, JsonSchemaObject>;\n\titems?: JsonSchemaObject | JsonSchemaObject[];\n\tadditionalProperties?: boolean | JsonSchemaObject;\n\tallOf?: JsonSchemaObject[];\n\tanyOf?: JsonSchemaObject[];\n\toneOf?: JsonSchemaObject[];\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n\treturn typeof value === \"object\" && value !== null;\n}\n\nfunction isJsonSchemaObject(value: unknown): value is JsonSchemaObject {\n\treturn isRecord(value);\n}\n\nfunction hasTypeBoxMetadata(schema: unknown): boolean {\n\treturn isRecord(schema) && Object.getOwnPropertySymbols(schema).includes(TYPEBOX_KIND);\n}\n\nfunction getSchemaTypes(schema: JsonSchemaObject): string[] {\n\tif (typeof schema.type === \"string\") {\n\t\treturn [schema.type];\n\t}\n\tif (Array.isArray(schema.type)) {\n\t\treturn schema.type.filter((type): type is string => typeof type === \"string\");\n\t}\n\treturn [];\n}\n\nfunction matchesJsonType(value: unknown, type: string): boolean {\n\tswitch (type) {\n\t\tcase \"number\":\n\t\t\treturn typeof value === \"number\";\n\t\tcase \"integer\":\n\t\t\treturn typeof value === \"number\" && Number.isInteger(value);\n\t\tcase \"boolean\":\n\t\t\treturn typeof value === \"boolean\";\n\t\tcase \"string\":\n\t\t\treturn typeof value === \"string\";\n\t\tcase \"null\":\n\t\t\treturn value === null;\n\t\tcase \"array\":\n\t\t\treturn Array.isArray(value);\n\t\tcase \"object\":\n\t\t\treturn isRecord(value) && !Array.isArray(value);\n\t\tdefault:\n\t\t\treturn false;\n\t}\n}\n\nfunction isValidatorSchema(value: unknown): value is Tool[\"parameters\"] {\n\treturn isRecord(value);\n}\n\nfunction getSubSchemaValidator(schema: JsonSchemaObject): ReturnType<typeof Compile> | undefined {\n\tif (!isValidatorSchema(schema)) {\n\t\treturn undefined;\n\t}\n\ttry {\n\t\treturn getValidator(schema);\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n\nfunction coercePrimitiveByType(value: unknown, type: string): unknown {\n\tswitch (type) {\n\t\tcase \"number\": {\n\t\t\tif (value === null) {\n\t\t\t\treturn 0;\n\t\t\t}\n\t\t\tif (typeof value === \"string\" && value.trim() !== \"\") {\n\t\t\t\tconst parsed = Number(value);\n\t\t\t\tif (Number.isFinite(parsed)) {\n\t\t\t\t\treturn parsed;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (typeof value === \"boolean\") {\n\t\t\t\treturn value ? 1 : 0;\n\t\t\t}\n\t\t\treturn value;\n\t\t}\n\t\tcase \"integer\": {\n\t\t\tif (value === null) {\n\t\t\t\treturn 0;\n\t\t\t}\n\t\t\tif (typeof value === \"string\" && value.trim() !== \"\") {\n\t\t\t\tconst parsed = Number(value);\n\t\t\t\tif (Number.isInteger(parsed)) {\n\t\t\t\t\treturn parsed;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (typeof value === \"boolean\") {\n\t\t\t\treturn value ? 1 : 0;\n\t\t\t}\n\t\t\treturn value;\n\t\t}\n\t\tcase \"boolean\": {\n\t\t\tif (value === null) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\tif (typeof value === \"string\") {\n\t\t\t\tif (value === \"true\") {\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t\tif (value === \"false\") {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (typeof value === \"number\") {\n\t\t\t\tif (value === 1) {\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t\tif (value === 0) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn value;\n\t\t}\n\t\tcase \"string\": {\n\t\t\tif (value === null) {\n\t\t\t\treturn \"\";\n\t\t\t}\n\t\t\tif (typeof value === \"number\" || typeof value === \"boolean\") {\n\t\t\t\treturn String(value);\n\t\t\t}\n\t\t\treturn value;\n\t\t}\n\t\tcase \"null\": {\n\t\t\tif (value === \"\" || value === 0 || value === false) {\n\t\t\t\treturn null;\n\t\t\t}\n\t\t\treturn value;\n\t\t}\n\t\tdefault:\n\t\t\treturn value;\n\t}\n}\n\nfunction applySchemaObjectCoercion(value: Record<string, unknown>, schema: JsonSchemaObject): void {\n\tconst properties = schema.properties;\n\tconst definedKeys = new Set<string>(properties ? Object.keys(properties) : []);\n\n\tif (properties) {\n\t\tfor (const [key, propertySchema] of Object.entries(properties)) {\n\t\t\tif (!(key in value)) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tvalue[key] = coerceWithJsonSchema(value[key], propertySchema);\n\t\t}\n\t}\n\n\tif (schema.additionalProperties && isJsonSchemaObject(schema.additionalProperties)) {\n\t\tfor (const [key, propertyValue] of Object.entries(value)) {\n\t\t\tif (definedKeys.has(key)) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tvalue[key] = coerceWithJsonSchema(propertyValue, schema.additionalProperties);\n\t\t}\n\t}\n}\n\nfunction applySchemaArrayCoercion(value: unknown[], schema: JsonSchemaObject): void {\n\tif (Array.isArray(schema.items)) {\n\t\tfor (let index = 0; index < value.length; index++) {\n\t\t\tconst itemSchema = schema.items[index];\n\t\t\tif (!itemSchema) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tvalue[index] = coerceWithJsonSchema(value[index], itemSchema);\n\t\t}\n\t\treturn;\n\t}\n\n\tif (isJsonSchemaObject(schema.items)) {\n\t\tfor (let index = 0; index < value.length; index++) {\n\t\t\tvalue[index] = coerceWithJsonSchema(value[index], schema.items);\n\t\t}\n\t}\n}\n\nfunction coerceWithUnionSchema(value: unknown, schemas: JsonSchemaObject[]): unknown {\n\tfor (const schema of schemas) {\n\t\tconst candidate = structuredClone(value);\n\t\tconst coerced = coerceWithJsonSchema(candidate, schema);\n\t\tconst validator = getSubSchemaValidator(schema);\n\t\tif (validator?.Check(coerced)) {\n\t\t\treturn coerced;\n\t\t}\n\t}\n\treturn value;\n}\n\nfunction coerceWithJsonSchema(value: unknown, schema: JsonSchemaObject): unknown {\n\tlet nextValue = value;\n\n\tif (Array.isArray(schema.allOf)) {\n\t\tfor (const nested of schema.allOf) {\n\t\t\tnextValue = coerceWithJsonSchema(nextValue, nested);\n\t\t}\n\t}\n\n\tif (Array.isArray(schema.anyOf)) {\n\t\tnextValue = coerceWithUnionSchema(nextValue, schema.anyOf);\n\t}\n\n\tif (Array.isArray(schema.oneOf)) {\n\t\tnextValue = coerceWithUnionSchema(nextValue, schema.oneOf);\n\t}\n\n\tconst schemaTypes = getSchemaTypes(schema);\n\tconst matchesUnionMember =\n\t\tschemaTypes.length > 1 && schemaTypes.some((schemaType) => matchesJsonType(nextValue, schemaType));\n\tif (schemaTypes.length > 0 && !matchesUnionMember) {\n\t\tfor (const schemaType of schemaTypes) {\n\t\t\tconst candidate = coercePrimitiveByType(nextValue, schemaType);\n\t\t\tif (candidate !== nextValue) {\n\t\t\t\tnextValue = candidate;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\n\tif (schemaTypes.includes(\"object\") && isRecord(nextValue) && !Array.isArray(nextValue)) {\n\t\tapplySchemaObjectCoercion(nextValue, schema);\n\t}\n\n\tif (schemaTypes.includes(\"array\") && Array.isArray(nextValue)) {\n\t\tapplySchemaArrayCoercion(nextValue, schema);\n\t}\n\n\treturn nextValue;\n}\n\nfunction getValidator(schema: Tool[\"parameters\"]): ReturnType<typeof Compile> {\n\tconst key = schema as object;\n\tconst cached = validatorCache.get(key);\n\tif (cached) {\n\t\treturn cached;\n\t}\n\tconst validator = Compile(schema);\n\tvalidatorCache.set(key, validator);\n\treturn validator;\n}\n\nfunction formatValidationPath(error: TLocalizedValidationError): string {\n\tif (error.keyword === \"required\") {\n\t\tconst requiredProperties = (error.params as { requiredProperties?: string[] }).requiredProperties;\n\t\tconst requiredProperty = requiredProperties?.[0];\n\t\tif (requiredProperty) {\n\t\t\tconst basePath = error.instancePath.replace(/^\\//, \"\").replace(/\\//g, \".\");\n\t\t\treturn basePath ? `${basePath}.${requiredProperty}` : requiredProperty;\n\t\t}\n\t}\n\tconst path = error.instancePath.replace(/^\\//, \"\").replace(/\\//g, \".\");\n\treturn path || \"root\";\n}\n\n/**\n * Finds a tool by name and validates the tool call arguments against its TypeBox schema\n * @param tools Array of tool definitions\n * @param toolCall The tool call from the LLM\n * @returns The validated arguments\n * @throws Error if tool is not found or validation fails\n */\nexport function validateToolCall(tools: Tool[], toolCall: ToolCall): any {\n\tconst tool = tools.find((t) => t.name === toolCall.name);\n\tif (!tool) {\n\t\tthrow new Error(`Tool \"${toolCall.name}\" not found`);\n\t}\n\treturn validateToolArguments(tool, toolCall);\n}\n\n/**\n * Validates tool call arguments against the tool's TypeBox schema\n * @param tool The tool definition with TypeBox schema\n * @param toolCall The tool call from the LLM\n * @returns The validated (and potentially coerced) arguments\n * @throws Error with formatted message if validation fails\n */\nexport function validateToolArguments(tool: Tool, toolCall: ToolCall): any {\n\tconst args = structuredClone(toolCall.arguments);\n\tValue.Convert(tool.parameters, args);\n\n\tconst validator = getValidator(tool.parameters);\n\tif (!hasTypeBoxMetadata(tool.parameters) && isJsonSchemaObject(tool.parameters)) {\n\t\tconst coerced = coerceWithJsonSchema(args, tool.parameters);\n\t\tif (coerced !== args) {\n\t\t\tif (isRecord(args) && isRecord(coerced)) {\n\t\t\t\tfor (const key of Object.keys(args)) {\n\t\t\t\t\tdelete args[key];\n\t\t\t\t}\n\t\t\t\tObject.assign(args, coerced);\n\t\t\t} else {\n\t\t\t\treturn validator.Check(coerced) ? coerced : args;\n\t\t\t}\n\t\t}\n\t}\n\n\tif (validator.Check(args)) {\n\t\treturn args;\n\t}\n\n\tconst errors =\n\t\tvalidator\n\t\t\t.Errors(args)\n\t\t\t.map((error) => ` - ${formatValidationPath(error)}: ${error.message}`)\n\t\t\t.join(\"\\n\") || \"Unknown validation error\";\n\n\tconst errorMessage = `Validation failed for tool \"${toolCall.name}\":\\n${errors}\\n\\nReceived arguments:\\n${JSON.stringify(toolCall.arguments, null, 2)}`;\n\n\tthrow new Error(errorMessage);\n}\n"]}
package/package.json ADDED
@@ -0,0 +1,108 @@
1
+ {
2
+ "name": "@threadwell/ai",
3
+ "version": "0.0.1",
4
+ "description": "Unified LLM API with automatic model discovery and provider configuration",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js"
12
+ },
13
+ "./anthropic": {
14
+ "types": "./dist/providers/anthropic.d.ts",
15
+ "import": "./dist/providers/anthropic.js"
16
+ },
17
+ "./azure-openai-responses": {
18
+ "types": "./dist/providers/azure-openai-responses.d.ts",
19
+ "import": "./dist/providers/azure-openai-responses.js"
20
+ },
21
+ "./google": {
22
+ "types": "./dist/providers/google.d.ts",
23
+ "import": "./dist/providers/google.js"
24
+ },
25
+ "./google-vertex": {
26
+ "types": "./dist/providers/google-vertex.d.ts",
27
+ "import": "./dist/providers/google-vertex.js"
28
+ },
29
+ "./mistral": {
30
+ "types": "./dist/providers/mistral.d.ts",
31
+ "import": "./dist/providers/mistral.js"
32
+ },
33
+ "./openai-codex-responses": {
34
+ "types": "./dist/providers/openai-codex-responses.d.ts",
35
+ "import": "./dist/providers/openai-codex-responses.js"
36
+ },
37
+ "./openai-completions": {
38
+ "types": "./dist/providers/openai-completions.d.ts",
39
+ "import": "./dist/providers/openai-completions.js"
40
+ },
41
+ "./openai-responses": {
42
+ "types": "./dist/providers/openai-responses.d.ts",
43
+ "import": "./dist/providers/openai-responses.js"
44
+ },
45
+ "./oauth": {
46
+ "types": "./dist/oauth.d.ts",
47
+ "import": "./dist/oauth.js"
48
+ },
49
+ "./bedrock-provider": {
50
+ "types": "./dist/bedrock-provider.d.ts",
51
+ "import": "./dist/bedrock-provider.js"
52
+ }
53
+ },
54
+ "bin": {
55
+ "threadwell-ai": "./dist/cli.js"
56
+ },
57
+ "files": [
58
+ "dist",
59
+ "README.md"
60
+ ],
61
+ "scripts": {
62
+ "clean": "shx rm -rf dist",
63
+ "generate-models": "npx tsx scripts/generate-models.ts",
64
+ "build": "npm run generate-models && tsgo -p tsconfig.build.json",
65
+ "dev": "tsgo -p tsconfig.build.json --watch --preserveWatchOutput",
66
+ "dev:tsc": "tsgo -p tsconfig.build.json --watch --preserveWatchOutput",
67
+ "test": "vitest --run",
68
+ "prepublishOnly": "npm run clean && npm run build"
69
+ },
70
+ "dependencies": {
71
+ "@anthropic-ai/sdk": "^0.91.1",
72
+ "@aws-sdk/client-bedrock-runtime": "^3.1030.0",
73
+ "@google/genai": "^1.40.0",
74
+ "@mistralai/mistralai": "^2.2.0",
75
+ "typebox": "^1.1.24",
76
+ "chalk": "^5.6.2",
77
+ "openai": "6.26.0",
78
+ "partial-json": "^0.1.7",
79
+ "proxy-agent": "^6.5.0",
80
+ "undici": "^7.19.1",
81
+ "zod-to-json-schema": "^3.24.6"
82
+ },
83
+ "keywords": [
84
+ "ai",
85
+ "llm",
86
+ "openai",
87
+ "anthropic",
88
+ "gemini",
89
+ "bedrock",
90
+ "unified",
91
+ "api"
92
+ ],
93
+ "author": "Mario Zechner",
94
+ "license": "MIT",
95
+ "repository": {
96
+ "type": "git",
97
+ "url": "git+https://github.com/EngramResearch/threadwell.git",
98
+ "directory": "packages/ai"
99
+ },
100
+ "engines": {
101
+ "node": ">=20.0.0"
102
+ },
103
+ "devDependencies": {
104
+ "@types/node": "^24.3.0",
105
+ "canvas": "^3.2.0",
106
+ "vitest": "^3.2.4"
107
+ }
108
+ }