genai-lite 0.13.1 → 0.14.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.
- package/dist/llm/clients/AnthropicClientAdapter.d.ts +0 -8
- package/dist/llm/clients/AnthropicClientAdapter.js +18 -49
- package/dist/llm/clients/OpenAIClientAdapter.d.ts +0 -8
- package/dist/llm/clients/OpenAIClientAdapter.js +4 -33
- package/dist/shared/adapters/schemaUtils.d.ts +32 -0
- package/dist/shared/adapters/schemaUtils.js +87 -0
- package/package.json +1 -1
|
@@ -53,14 +53,6 @@ export declare class AnthropicClientAdapter implements ILLMClientAdapter {
|
|
|
53
53
|
private mapAnthropicUsage;
|
|
54
54
|
private mergeAnthropicUsage;
|
|
55
55
|
private createSyntheticMessage;
|
|
56
|
-
/**
|
|
57
|
-
* Recursively adds additionalProperties: false to all object schemas
|
|
58
|
-
* Required by Anthropic's strict mode for structured outputs
|
|
59
|
-
*
|
|
60
|
-
* @param schema - The JSON schema to process
|
|
61
|
-
* @returns A new schema with additionalProperties: false on all objects
|
|
62
|
-
*/
|
|
63
|
-
private addAdditionalPropertiesFalse;
|
|
64
56
|
/**
|
|
65
57
|
* Formats messages for Anthropic API with proper system message handling
|
|
66
58
|
*
|
|
@@ -9,6 +9,7 @@ exports.AnthropicClientAdapter = void 0;
|
|
|
9
9
|
const sdk_1 = __importDefault(require("@anthropic-ai/sdk"));
|
|
10
10
|
const types_1 = require("./types");
|
|
11
11
|
const errorUtils_1 = require("../../shared/adapters/errorUtils");
|
|
12
|
+
const schemaUtils_1 = require("../../shared/adapters/schemaUtils");
|
|
12
13
|
const systemMessageUtils_1 = require("../../shared/adapters/systemMessageUtils");
|
|
13
14
|
const defaultLogger_1 = require("../../logging/defaultLogger");
|
|
14
15
|
/**
|
|
@@ -247,7 +248,7 @@ class AnthropicClientAdapter {
|
|
|
247
248
|
const so = request.settings.structuredOutput;
|
|
248
249
|
// Anthropic requires additionalProperties: false on all object schemas
|
|
249
250
|
const processedSchema = so.strict !== false
|
|
250
|
-
?
|
|
251
|
+
? (0, schemaUtils_1.applyStrictSchemaConstraints)({ ...so.schema })
|
|
251
252
|
: { ...so.schema };
|
|
252
253
|
messageParams.output_config = {
|
|
253
254
|
format: {
|
|
@@ -344,35 +345,6 @@ class AnthropicClientAdapter {
|
|
|
344
345
|
usage: accumulator.usage,
|
|
345
346
|
};
|
|
346
347
|
}
|
|
347
|
-
/**
|
|
348
|
-
* Recursively adds additionalProperties: false to all object schemas
|
|
349
|
-
* Required by Anthropic's strict mode for structured outputs
|
|
350
|
-
*
|
|
351
|
-
* @param schema - The JSON schema to process
|
|
352
|
-
* @returns A new schema with additionalProperties: false on all objects
|
|
353
|
-
*/
|
|
354
|
-
addAdditionalPropertiesFalse(schema) {
|
|
355
|
-
if (!schema || typeof schema !== 'object') {
|
|
356
|
-
return schema;
|
|
357
|
-
}
|
|
358
|
-
const processed = { ...schema };
|
|
359
|
-
// If this is an object type, add additionalProperties: false
|
|
360
|
-
if (processed.type === 'object') {
|
|
361
|
-
processed.additionalProperties = false;
|
|
362
|
-
// Process nested properties
|
|
363
|
-
if (processed.properties) {
|
|
364
|
-
processed.properties = {};
|
|
365
|
-
for (const [key, value] of Object.entries(schema.properties)) {
|
|
366
|
-
processed.properties[key] = this.addAdditionalPropertiesFalse(value);
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
// Process array items
|
|
371
|
-
if (processed.items) {
|
|
372
|
-
processed.items = this.addAdditionalPropertiesFalse(schema.items);
|
|
373
|
-
}
|
|
374
|
-
return processed;
|
|
375
|
-
}
|
|
376
348
|
/**
|
|
377
349
|
* Formats messages for Anthropic API with proper system message handling
|
|
378
350
|
*
|
|
@@ -490,24 +462,15 @@ class AnthropicClientAdapter {
|
|
|
490
462
|
if (textBlocks.length === 0) {
|
|
491
463
|
throw new Error("Invalid completion structure from Anthropic API");
|
|
492
464
|
}
|
|
493
|
-
// Extract
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
//
|
|
497
|
-
if (completion.thinking_content) {
|
|
498
|
-
reasoning = completion.thinking_content;
|
|
499
|
-
}
|
|
465
|
+
// Extract reasoning from thinking content blocks. This is the only place
|
|
466
|
+
// Anthropic exposes it: `Message` has no `thinking_content` or
|
|
467
|
+
// `reasoning_details` field (the latter is an OpenRouter concept, handled in
|
|
468
|
+
// OpenRouterClientAdapter), so branches reading those could never fire.
|
|
500
469
|
const thinkingContent = completion.content
|
|
501
470
|
.filter((block) => block.type === "thinking" && typeof block.thinking === "string")
|
|
502
471
|
.map((block) => block.thinking)
|
|
503
472
|
.join("");
|
|
504
|
-
|
|
505
|
-
reasoning = thinkingContent;
|
|
506
|
-
}
|
|
507
|
-
// Check for reasoning details that need to be preserved
|
|
508
|
-
if (completion.reasoning_details) {
|
|
509
|
-
reasoning_details = completion.reasoning_details;
|
|
510
|
-
}
|
|
473
|
+
const reasoning = thinkingContent || undefined;
|
|
511
474
|
// Map Anthropic's stop reason to our standard format
|
|
512
475
|
const finishReason = this.mapAnthropicStopReason(completion.stop_reason);
|
|
513
476
|
const choice = {
|
|
@@ -522,10 +485,6 @@ class AnthropicClientAdapter {
|
|
|
522
485
|
if (reasoning && request.settings.reasoning && !request.settings.reasoning.exclude) {
|
|
523
486
|
choice.reasoning = reasoning;
|
|
524
487
|
}
|
|
525
|
-
// Always include reasoning_details if present (for tool use continuation)
|
|
526
|
-
if (reasoning_details) {
|
|
527
|
-
choice.reasoning_details = reasoning_details;
|
|
528
|
-
}
|
|
529
488
|
return {
|
|
530
489
|
id: completion.id,
|
|
531
490
|
provider: request.providerId,
|
|
@@ -551,12 +510,22 @@ class AnthropicClientAdapter {
|
|
|
551
510
|
mapAnthropicStopReason(anthropicReason) {
|
|
552
511
|
if (!anthropicReason)
|
|
553
512
|
return null;
|
|
513
|
+
// Anthropic's StopReason union is exactly:
|
|
514
|
+
// end_turn | max_tokens | stop_sequence | tool_use | pause_turn | refusal
|
|
515
|
+
// Note there is no `content_filter` - that is OpenAI's vocabulary. A model
|
|
516
|
+
// that declines on policy grounds reports `refusal`, which we normalize to
|
|
517
|
+
// `content_filter` so callers can detect a blocked completion with the same
|
|
518
|
+
// check they already use for OpenAI and Gemini.
|
|
554
519
|
const reasonMap = {
|
|
555
520
|
end_turn: "stop",
|
|
556
521
|
max_tokens: "length",
|
|
557
522
|
stop_sequence: "stop",
|
|
558
|
-
content_filter: "content_filter",
|
|
559
523
|
tool_use: "tool_calls",
|
|
524
|
+
refusal: "content_filter",
|
|
525
|
+
// Anthropic-specific: a long-running turn was paused and the caller is
|
|
526
|
+
// expected to continue it. No equivalent in the normalized vocabulary, so
|
|
527
|
+
// it maps to "other" deliberately rather than by falling through.
|
|
528
|
+
pause_turn: "other",
|
|
560
529
|
};
|
|
561
530
|
return reasonMap[anthropicReason] || "other";
|
|
562
531
|
}
|
|
@@ -52,14 +52,6 @@ export declare class OpenAIClientAdapter implements ILLMClientAdapter {
|
|
|
52
52
|
private createTransportOptions;
|
|
53
53
|
private getStreamChoiceState;
|
|
54
54
|
private createSyntheticCompletion;
|
|
55
|
-
/**
|
|
56
|
-
* Recursively adds additionalProperties: false to all object schemas
|
|
57
|
-
* Required by OpenAI's strict mode for structured outputs
|
|
58
|
-
*
|
|
59
|
-
* @param schema - The JSON schema to process
|
|
60
|
-
* @returns A new schema with additionalProperties: false on all objects
|
|
61
|
-
*/
|
|
62
|
-
private addAdditionalPropertiesFalse;
|
|
63
55
|
/**
|
|
64
56
|
* Formats messages for OpenAI API
|
|
65
57
|
*
|
|
@@ -10,6 +10,7 @@ const openai_1 = __importDefault(require("openai"));
|
|
|
10
10
|
const types_1 = require("./types");
|
|
11
11
|
const errorUtils_1 = require("../../shared/adapters/errorUtils");
|
|
12
12
|
const logprobsUtils_1 = require("../../shared/adapters/logprobsUtils");
|
|
13
|
+
const schemaUtils_1 = require("../../shared/adapters/schemaUtils");
|
|
13
14
|
const systemMessageUtils_1 = require("../../shared/adapters/systemMessageUtils");
|
|
14
15
|
const defaultLogger_1 = require("../../logging/defaultLogger");
|
|
15
16
|
/**
|
|
@@ -231,8 +232,10 @@ class OpenAIClientAdapter {
|
|
|
231
232
|
}
|
|
232
233
|
if (request.settings.structuredOutput?.schema && request.settings.structuredOutput.enabled !== false) {
|
|
233
234
|
const so = request.settings.structuredOutput;
|
|
235
|
+
// OpenAI strict mode additionally requires `required` to list every
|
|
236
|
+
// property of each object schema.
|
|
234
237
|
const processedSchema = so.strict !== false
|
|
235
|
-
?
|
|
238
|
+
? (0, schemaUtils_1.applyStrictSchemaConstraints)({ ...so.schema }, { requireAllProperties: true })
|
|
236
239
|
: so.schema;
|
|
237
240
|
completionParams.response_format = {
|
|
238
241
|
type: 'json_schema',
|
|
@@ -288,38 +291,6 @@ class OpenAIClientAdapter {
|
|
|
288
291
|
...(usage && { usage }),
|
|
289
292
|
};
|
|
290
293
|
}
|
|
291
|
-
/**
|
|
292
|
-
* Recursively adds additionalProperties: false to all object schemas
|
|
293
|
-
* Required by OpenAI's strict mode for structured outputs
|
|
294
|
-
*
|
|
295
|
-
* @param schema - The JSON schema to process
|
|
296
|
-
* @returns A new schema with additionalProperties: false on all objects
|
|
297
|
-
*/
|
|
298
|
-
addAdditionalPropertiesFalse(schema) {
|
|
299
|
-
if (!schema || typeof schema !== 'object') {
|
|
300
|
-
return schema;
|
|
301
|
-
}
|
|
302
|
-
const processed = { ...schema };
|
|
303
|
-
// If this is an object type, add additionalProperties: false and ensure required includes all properties
|
|
304
|
-
if (processed.type === 'object') {
|
|
305
|
-
processed.additionalProperties = false;
|
|
306
|
-
// Process nested properties
|
|
307
|
-
if (processed.properties) {
|
|
308
|
-
const propertyKeys = Object.keys(schema.properties);
|
|
309
|
-
processed.properties = {};
|
|
310
|
-
for (const [key, value] of Object.entries(schema.properties)) {
|
|
311
|
-
processed.properties[key] = this.addAdditionalPropertiesFalse(value);
|
|
312
|
-
}
|
|
313
|
-
// OpenAI strict mode requires 'required' to include ALL properties
|
|
314
|
-
processed.required = propertyKeys;
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
// Process array items
|
|
318
|
-
if (processed.items) {
|
|
319
|
-
processed.items = this.addAdditionalPropertiesFalse(schema.items);
|
|
320
|
-
}
|
|
321
|
-
return processed;
|
|
322
|
-
}
|
|
323
294
|
/**
|
|
324
295
|
* Formats messages for OpenAI API
|
|
325
296
|
*
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export interface StrictSchemaOptions {
|
|
2
|
+
/**
|
|
3
|
+
* When true, every object schema's `required` is rewritten to list all of its
|
|
4
|
+
* properties. OpenAI's strict mode demands this; Anthropic's does not.
|
|
5
|
+
*
|
|
6
|
+
* @default false
|
|
7
|
+
*/
|
|
8
|
+
requireAllProperties?: boolean;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Returns a deep copy of `schema` with `additionalProperties: false` set on every
|
|
12
|
+
* object schema, as required by OpenAI's and Anthropic's strict structured-output
|
|
13
|
+
* modes.
|
|
14
|
+
*
|
|
15
|
+
* The input is never mutated.
|
|
16
|
+
*
|
|
17
|
+
* Traversal covers `properties`, `items` (single or tuple form), `prefixItems`,
|
|
18
|
+
* `$defs`, `definitions`, `anyOf`, `oneOf`, `allOf`, and `not`. Note that `$ref`
|
|
19
|
+
* pointers are **not** resolved — they are left as-is, and the schemas they point
|
|
20
|
+
* at are constrained where they are *defined* (typically under `$defs`), which is
|
|
21
|
+
* what the providers validate. Both providers reject genuinely recursive schemas
|
|
22
|
+
* anyway, so resolving refs would buy nothing.
|
|
23
|
+
*
|
|
24
|
+
* Cyclic *JavaScript* object graphs (a schema object reachable from itself) are
|
|
25
|
+
* handled: each input node is copied at most once and revisits reuse that copy,
|
|
26
|
+
* so the walk terminates and shared subschemas stay shared.
|
|
27
|
+
*
|
|
28
|
+
* @param schema - The JSON schema to process
|
|
29
|
+
* @param options - Provider-specific strictness tweaks
|
|
30
|
+
* @returns A new schema with strict-mode constraints applied
|
|
31
|
+
*/
|
|
32
|
+
export declare function applyStrictSchemaConstraints<T>(schema: T, options?: StrictSchemaOptions): T;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// AI Summary: Shared JSON Schema preparation for providers whose strict
|
|
3
|
+
// structured-output mode requires additionalProperties: false on every object
|
|
4
|
+
// schema. Traverses properties, items (incl. tuple form), $defs/definitions,
|
|
5
|
+
// anyOf/oneOf/allOf, prefixItems and not, and is safe against cyclic input.
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.applyStrictSchemaConstraints = applyStrictSchemaConstraints;
|
|
8
|
+
/** Keywords whose value is a map of name -> subschema. */
|
|
9
|
+
const SUBSCHEMA_MAPS = ["properties", "$defs", "definitions"];
|
|
10
|
+
/** Keywords whose value is an array of subschemas. */
|
|
11
|
+
const SUBSCHEMA_ARRAYS = ["anyOf", "oneOf", "allOf", "prefixItems"];
|
|
12
|
+
/**
|
|
13
|
+
* Returns a deep copy of `schema` with `additionalProperties: false` set on every
|
|
14
|
+
* object schema, as required by OpenAI's and Anthropic's strict structured-output
|
|
15
|
+
* modes.
|
|
16
|
+
*
|
|
17
|
+
* The input is never mutated.
|
|
18
|
+
*
|
|
19
|
+
* Traversal covers `properties`, `items` (single or tuple form), `prefixItems`,
|
|
20
|
+
* `$defs`, `definitions`, `anyOf`, `oneOf`, `allOf`, and `not`. Note that `$ref`
|
|
21
|
+
* pointers are **not** resolved — they are left as-is, and the schemas they point
|
|
22
|
+
* at are constrained where they are *defined* (typically under `$defs`), which is
|
|
23
|
+
* what the providers validate. Both providers reject genuinely recursive schemas
|
|
24
|
+
* anyway, so resolving refs would buy nothing.
|
|
25
|
+
*
|
|
26
|
+
* Cyclic *JavaScript* object graphs (a schema object reachable from itself) are
|
|
27
|
+
* handled: each input node is copied at most once and revisits reuse that copy,
|
|
28
|
+
* so the walk terminates and shared subschemas stay shared.
|
|
29
|
+
*
|
|
30
|
+
* @param schema - The JSON schema to process
|
|
31
|
+
* @param options - Provider-specific strictness tweaks
|
|
32
|
+
* @returns A new schema with strict-mode constraints applied
|
|
33
|
+
*/
|
|
34
|
+
function applyStrictSchemaConstraints(schema, options = {}) {
|
|
35
|
+
return walk(schema, options, new Map());
|
|
36
|
+
}
|
|
37
|
+
function walk(node, options, seen) {
|
|
38
|
+
if (!node || typeof node !== "object") {
|
|
39
|
+
return node;
|
|
40
|
+
}
|
|
41
|
+
const cached = seen.get(node);
|
|
42
|
+
if (cached !== undefined) {
|
|
43
|
+
return cached;
|
|
44
|
+
}
|
|
45
|
+
if (Array.isArray(node)) {
|
|
46
|
+
const copy = [];
|
|
47
|
+
seen.set(node, copy);
|
|
48
|
+
for (const entry of node) {
|
|
49
|
+
copy.push(walk(entry, options, seen));
|
|
50
|
+
}
|
|
51
|
+
return copy;
|
|
52
|
+
}
|
|
53
|
+
const source = node;
|
|
54
|
+
const processed = { ...source };
|
|
55
|
+
// Registered before recursing so a cycle resolves to this same object.
|
|
56
|
+
seen.set(node, processed);
|
|
57
|
+
if (source.type === "object") {
|
|
58
|
+
processed.additionalProperties = false;
|
|
59
|
+
const properties = source.properties;
|
|
60
|
+
if (options.requireAllProperties && properties && typeof properties === "object") {
|
|
61
|
+
processed.required = Object.keys(properties);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
for (const key of SUBSCHEMA_MAPS) {
|
|
65
|
+
const value = source[key];
|
|
66
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
67
|
+
const out = {};
|
|
68
|
+
for (const [name, subschema] of Object.entries(value)) {
|
|
69
|
+
out[name] = walk(subschema, options, seen);
|
|
70
|
+
}
|
|
71
|
+
processed[key] = out;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
for (const key of SUBSCHEMA_ARRAYS) {
|
|
75
|
+
const value = source[key];
|
|
76
|
+
if (Array.isArray(value)) {
|
|
77
|
+
processed[key] = value.map((subschema) => walk(subschema, options, seen));
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (source.items !== undefined) {
|
|
81
|
+
processed.items = walk(source.items, options, seen);
|
|
82
|
+
}
|
|
83
|
+
if (source.not !== undefined) {
|
|
84
|
+
processed.not = walk(source.not, options, seen);
|
|
85
|
+
}
|
|
86
|
+
return processed;
|
|
87
|
+
}
|