@mcpmesh/sdk 0.8.0 → 0.9.0-beta.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.
- package/dist/__tests__/claude-handler.test.js +17 -71
- package/dist/__tests__/claude-handler.test.js.map +1 -1
- package/dist/__tests__/llm-provider.test.js +3 -3
- package/dist/__tests__/llm-provider.test.js.map +1 -1
- package/dist/__tests__/provider-handler-registry.test.js +6 -6
- package/dist/__tests__/provider-handler-registry.test.js.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -2
- package/dist/index.js.map +1 -1
- package/dist/llm-agent.d.ts +37 -2
- package/dist/llm-agent.d.ts.map +1 -1
- package/dist/llm-agent.js +206 -14
- package/dist/llm-agent.js.map +1 -1
- package/dist/llm-provider.d.ts +7 -0
- package/dist/llm-provider.d.ts.map +1 -1
- package/dist/llm-provider.js +42 -15
- package/dist/llm-provider.js.map +1 -1
- package/dist/llm.d.ts.map +1 -1
- package/dist/llm.js +21 -6
- package/dist/llm.js.map +1 -1
- package/dist/provider-handlers/claude-handler.d.ts +16 -31
- package/dist/provider-handlers/claude-handler.d.ts.map +1 -1
- package/dist/provider-handlers/claude-handler.js +34 -115
- package/dist/provider-handlers/claude-handler.js.map +1 -1
- package/dist/provider-handlers/gemini-handler.d.ts +12 -9
- package/dist/provider-handlers/gemini-handler.d.ts.map +1 -1
- package/dist/provider-handlers/gemini-handler.js +68 -17
- package/dist/provider-handlers/gemini-handler.js.map +1 -1
- package/dist/provider-handlers/index.d.ts +1 -1
- package/dist/provider-handlers/index.d.ts.map +1 -1
- package/dist/provider-handlers/index.js +1 -1
- package/dist/provider-handlers/index.js.map +1 -1
- package/dist/provider-handlers/openai-handler.d.ts +7 -10
- package/dist/provider-handlers/openai-handler.d.ts.map +1 -1
- package/dist/provider-handlers/openai-handler.js +61 -19
- package/dist/provider-handlers/openai-handler.js.map +1 -1
- package/dist/provider-handlers/provider-handler.d.ts +11 -0
- package/dist/provider-handlers/provider-handler.d.ts.map +1 -1
- package/dist/provider-handlers/provider-handler.js +106 -2
- package/dist/provider-handlers/provider-handler.js.map +1 -1
- package/dist/types.d.ts +2 -2
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
package/dist/llm.js
CHANGED
|
@@ -230,18 +230,33 @@ export function buildLlmAgentSpecs() {
|
|
|
230
230
|
const registry = LlmToolRegistry.getInstance();
|
|
231
231
|
const specs = [];
|
|
232
232
|
for (const [, config] of registry.getAllConfigs()) {
|
|
233
|
-
//
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
233
|
+
// Apply MESH_LLM_* env var overrides (matches Python SDK behavior)
|
|
234
|
+
// ENV > config > defaults
|
|
235
|
+
// MESH_LLM_PROVIDER: Override provider for direct mode (not mesh delegation)
|
|
236
|
+
let resolvedProvider;
|
|
237
|
+
if (typeof config.provider === "string") {
|
|
238
|
+
resolvedProvider = process.env.MESH_LLM_PROVIDER || config.provider;
|
|
239
|
+
}
|
|
240
|
+
else {
|
|
241
|
+
resolvedProvider = config.provider; // mesh delegation - no override
|
|
242
|
+
}
|
|
243
|
+
const providerJson = typeof resolvedProvider === "string"
|
|
244
|
+
? JSON.stringify({ direct: resolvedProvider })
|
|
245
|
+
: JSON.stringify(resolvedProvider);
|
|
237
246
|
// Serialize filter to JSON if present
|
|
238
247
|
const filterJson = config.filter ? JSON.stringify(config.filter) : undefined;
|
|
248
|
+
// MESH_LLM_FILTER_MODE: Override filter mode
|
|
249
|
+
const resolvedFilterMode = process.env.MESH_LLM_FILTER_MODE || config.filterMode;
|
|
250
|
+
// MESH_LLM_MAX_ITERATIONS: Override max iterations
|
|
251
|
+
const resolvedMaxIterations = process.env.MESH_LLM_MAX_ITERATIONS
|
|
252
|
+
? parseInt(process.env.MESH_LLM_MAX_ITERATIONS, 10)
|
|
253
|
+
: config.maxIterations;
|
|
239
254
|
specs.push({
|
|
240
255
|
functionId: config.functionId,
|
|
241
256
|
provider: providerJson,
|
|
242
257
|
filter: filterJson,
|
|
243
|
-
filterMode:
|
|
244
|
-
maxIterations:
|
|
258
|
+
filterMode: resolvedFilterMode,
|
|
259
|
+
maxIterations: resolvedMaxIterations,
|
|
245
260
|
});
|
|
246
261
|
}
|
|
247
262
|
return specs;
|
package/dist/llm.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"llm.js","sourceRoot":"","sources":["../src/llm.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AAGH,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAUrD,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AAClE,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEnC;;GAEG;AACH,MAAM,OAAO,eAAe;IAClB,MAAM,CAAC,QAAQ,GAA2B,IAAI,CAAC;IAEvD,6CAA6C;IACrC,OAAO,GAA+B,IAAI,GAAG,EAAE,CAAC;IAExD,2EAA2E;IACnE,aAAa,GAAgC,IAAI,GAAG,EAAE,CAAC;IAE/D,oFAAoF;IAC5E,iBAAiB,GAAkC,IAAI,GAAG,EAAE,CAAC;IAErE,gBAAuB,CAAC;IAExB,MAAM,CAAC,WAAW;QAChB,IAAI,CAAC,eAAe,CAAC,QAAQ,EAAE,CAAC;YAC9B,eAAe,CAAC,QAAQ,GAAG,IAAI,eAAe,EAAE,CAAC;QACnD,CAAC;QACD,OAAO,eAAe,CAAC,QAAQ,CAAC;IAClC,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,KAAK;QACV,eAAe,CAAC,QAAQ,GAAG,IAAI,CAAC;IAClC,CAAC;IAED;;OAEG;IACH,QAAQ,CAAC,UAAkB,EAAE,MAAqB;QAChD,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC;IACvC,CAAC;IAED;;OAEG;IACH,SAA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S,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,SAAS;QACpE,WAAW,EACT,OAAO,MAAM,CAAC,QAAQ,KAAK,QAAQ;YACjC,CAAC,CAAC,MAAM,CAAC,QAAQ;YACjB,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,QAAQ,CAAC;KACtC,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,SAAS,CAAC,OAAgB;IACxC,OAAO,CACL,OAAO,OAAO,KAAK,QAAQ;QAC3B,OAAO,KAAK,IAAI;QAChB,gBAAgB,IAAI,OAAO,CAC5B,CAAC;AACJ,CAAC"}
|
|
@@ -4,20 +4,19 @@
|
|
|
4
4
|
* Optimized for Claude API (Claude 3.x, Sonnet, Opus, Haiku)
|
|
5
5
|
* using Anthropic's best practices for tool calling and JSON responses.
|
|
6
6
|
*
|
|
7
|
-
* Supports
|
|
8
|
-
* -
|
|
9
|
-
* - hint: Use prompt-based JSON instructions (medium speed, ~95% reliable)
|
|
7
|
+
* Supports two output modes (TEXT + HINT only):
|
|
8
|
+
* - hint: Use prompt-based JSON instructions with DECISION GUIDE (~95% reliable)
|
|
10
9
|
* - text: Plain text output for str return types (fastest)
|
|
11
10
|
*
|
|
11
|
+
* Native response_format (strict mode) is NOT used due to cross-runtime
|
|
12
|
+
* incompatibilities when tools are present, and grammar compilation overhead.
|
|
13
|
+
*
|
|
12
14
|
* Features:
|
|
13
15
|
* - Anti-XML tool calling instructions
|
|
14
|
-
* -
|
|
16
|
+
* - DECISION GUIDE for tool vs. direct JSON response decisions
|
|
15
17
|
*
|
|
16
18
|
* Note: Prompt caching is not yet implemented for AI SDK v6.
|
|
17
19
|
* TODO: Re-enable via experimental_providerOptions when AI SDK v6 supports it.
|
|
18
|
-
*
|
|
19
|
-
* Based on Python's ClaudeHandler:
|
|
20
|
-
* src/runtime/python/_mcp_mesh/engine/provider_handlers/claude_handler.py
|
|
21
20
|
*/
|
|
22
21
|
import type { LlmMessage } from "../types.js";
|
|
23
22
|
import { type ProviderHandler, type VendorCapabilities, type ToolSchema, type OutputSchema, type PreparedRequest, type OutputMode } from "./provider-handler.js";
|
|
@@ -26,18 +25,18 @@ import { type ProviderHandler, type VendorCapabilities, type ToolSchema, type Ou
|
|
|
26
25
|
*
|
|
27
26
|
* Claude Characteristics:
|
|
28
27
|
* - Excellent at following detailed instructions
|
|
29
|
-
* - Native structured output via response_format (requires strict schema)
|
|
30
28
|
* - Native tool calling (via Anthropic messages API)
|
|
31
29
|
* - Performs best with anti-XML tool calling instructions
|
|
32
30
|
*
|
|
33
|
-
* Output Modes:
|
|
34
|
-
* -
|
|
35
|
-
* - hint: JSON schema in prompt (medium speed, usually valid JSON)
|
|
31
|
+
* Output Modes (TEXT + HINT only):
|
|
32
|
+
* - hint: JSON schema in prompt with DECISION GUIDE (~95% reliable)
|
|
36
33
|
* - text: Plain text output for str return types (fastest)
|
|
37
34
|
*
|
|
35
|
+
* Native response_format (strict mode) is not used. HINT mode with
|
|
36
|
+
* detailed prompt instructions provides sufficient reliability (~95%)
|
|
37
|
+
* without cross-runtime incompatibilities and grammar compilation overhead.
|
|
38
|
+
*
|
|
38
39
|
* Best Practices (from Anthropic docs):
|
|
39
|
-
* - Use response_format for guaranteed JSON schema compliance
|
|
40
|
-
* - Schema must have additionalProperties: false on all objects
|
|
41
40
|
* - Add anti-XML instructions to prevent <invoke> style tool calls
|
|
42
41
|
* - Use one tool call at a time for better reliability
|
|
43
42
|
*/
|
|
@@ -46,9 +45,8 @@ export declare class ClaudeHandler implements ProviderHandler {
|
|
|
46
45
|
/**
|
|
47
46
|
* Prepare request parameters for Claude API with output mode support.
|
|
48
47
|
*
|
|
49
|
-
* Output Mode Strategy:
|
|
50
|
-
* -
|
|
51
|
-
* - hint: No response_format, rely on prompt instructions (medium speed)
|
|
48
|
+
* Output Mode Strategy (TEXT + HINT only):
|
|
49
|
+
* - hint: No response_format, rely on prompt instructions (~95% reliable)
|
|
52
50
|
* - text: No response_format, plain text output (fastest)
|
|
53
51
|
*
|
|
54
52
|
* Message Format (Anthropic-specific):
|
|
@@ -66,9 +64,8 @@ export declare class ClaudeHandler implements ProviderHandler {
|
|
|
66
64
|
/**
|
|
67
65
|
* Format system prompt for Claude with output mode support.
|
|
68
66
|
*
|
|
69
|
-
* Output Mode Strategy:
|
|
70
|
-
* -
|
|
71
|
-
* - hint: Add detailed JSON schema instructions in prompt
|
|
67
|
+
* Output Mode Strategy (TEXT + HINT only):
|
|
68
|
+
* - hint: Add detailed JSON schema instructions with DECISION GUIDE in prompt
|
|
72
69
|
* - text: No JSON instructions (plain text output)
|
|
73
70
|
*/
|
|
74
71
|
formatSystemPrompt(basePrompt: string, toolSchemas: ToolSchema[] | null, outputSchema: OutputSchema | null, outputMode?: OutputMode): string;
|
|
@@ -91,17 +88,5 @@ export declare class ClaudeHandler implements ProviderHandler {
|
|
|
91
88
|
* - If schema exists, always use "hint" mode (prompt-based)
|
|
92
89
|
*/
|
|
93
90
|
determineOutputMode(outputSchema: OutputSchema | null, overrideMode?: OutputMode): OutputMode;
|
|
94
|
-
/**
|
|
95
|
-
* Append JSON schema instructions to the system prompt for "hint" mode.
|
|
96
|
-
*
|
|
97
|
-
* This enables fast, prompt-based structured output instead of slow generateObject().
|
|
98
|
-
* Claude is excellent at following detailed instructions, so this approach is
|
|
99
|
-
* ~95% reliable while being much faster than native structured output.
|
|
100
|
-
*
|
|
101
|
-
* @param messages - Messages with system prompt
|
|
102
|
-
* @param outputSchema - Schema to include in instructions
|
|
103
|
-
* @returns Messages with updated system prompt
|
|
104
|
-
*/
|
|
105
|
-
private appendJsonInstructionsToSystemPrompt;
|
|
106
91
|
}
|
|
107
92
|
//# sourceMappingURL=claude-handler.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"claude-handler.d.ts","sourceRoot":"","sources":["../../src/provider-handlers/claude-handler.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"claude-handler.d.ts","sourceRoot":"","sources":["../../src/provider-handlers/claude-handler.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAGH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,EAIL,KAAK,eAAe,EACpB,KAAK,kBAAkB,EACvB,KAAK,UAAU,EACf,KAAK,YAAY,EACjB,KAAK,eAAe,EACpB,KAAK,UAAU,EAChB,MAAM,uBAAuB,CAAC;AAS/B;;;;;;;;;;;;;;;;;;;GAmBG;AACH,qBAAa,aAAc,YAAW,eAAe;IACnD,QAAQ,CAAC,MAAM,eAAe;IAE9B;;;;;;;;;;;OAWG;IACH,cAAc,CACZ,QAAQ,EAAE,UAAU,EAAE,EACtB,KAAK,EAAE,UAAU,EAAE,GAAG,IAAI,EAC1B,YAAY,EAAE,YAAY,GAAG,IAAI,EACjC,OAAO,CAAC,EAAE;QACR,UAAU,CAAC,EAAE,UAAU,CAAC;QACxB,WAAW,CAAC,EAAE,MAAM,CAAC;QACrB,eAAe,CAAC,EAAE,MAAM,CAAC;QACzB,IAAI,CAAC,EAAE,MAAM,CAAC;QACd,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;KACxB,GACA,eAAe;IAuClB;;;;;;OAMG;IACH,kBAAkB,CAChB,UAAU,EAAE,MAAM,EAClB,WAAW,EAAE,UAAU,EAAE,GAAG,IAAI,EAChC,YAAY,EAAE,YAAY,GAAG,IAAI,EACjC,UAAU,CAAC,EAAE,UAAU,GACtB,MAAM;IAyET;;OAEG;IACH,eAAe,IAAI,kBAAkB;IAWrC;;;;;;;;;;;;;OAaG;IACH,mBAAmB,CACjB,YAAY,EAAE,YAAY,GAAG,IAAI,EACjC,YAAY,CAAC,EAAE,UAAU,GACxB,UAAU;CAgBd"}
|
|
@@ -4,23 +4,22 @@
|
|
|
4
4
|
* Optimized for Claude API (Claude 3.x, Sonnet, Opus, Haiku)
|
|
5
5
|
* using Anthropic's best practices for tool calling and JSON responses.
|
|
6
6
|
*
|
|
7
|
-
* Supports
|
|
8
|
-
* -
|
|
9
|
-
* - hint: Use prompt-based JSON instructions (medium speed, ~95% reliable)
|
|
7
|
+
* Supports two output modes (TEXT + HINT only):
|
|
8
|
+
* - hint: Use prompt-based JSON instructions with DECISION GUIDE (~95% reliable)
|
|
10
9
|
* - text: Plain text output for str return types (fastest)
|
|
11
10
|
*
|
|
11
|
+
* Native response_format (strict mode) is NOT used due to cross-runtime
|
|
12
|
+
* incompatibilities when tools are present, and grammar compilation overhead.
|
|
13
|
+
*
|
|
12
14
|
* Features:
|
|
13
15
|
* - Anti-XML tool calling instructions
|
|
14
|
-
* -
|
|
16
|
+
* - DECISION GUIDE for tool vs. direct JSON response decisions
|
|
15
17
|
*
|
|
16
18
|
* Note: Prompt caching is not yet implemented for AI SDK v6.
|
|
17
19
|
* TODO: Re-enable via experimental_providerOptions when AI SDK v6 supports it.
|
|
18
|
-
*
|
|
19
|
-
* Based on Python's ClaudeHandler:
|
|
20
|
-
* src/runtime/python/_mcp_mesh/engine/provider_handlers/claude_handler.py
|
|
21
20
|
*/
|
|
22
21
|
import { createDebug } from "../debug.js";
|
|
23
|
-
import { convertMessagesToVercelFormat,
|
|
22
|
+
import { convertMessagesToVercelFormat, BASE_TOOL_INSTRUCTIONS, CLAUDE_ANTI_XML_INSTRUCTION, } from "./provider-handler.js";
|
|
24
23
|
import { ProviderHandlerRegistry } from "./provider-handler-registry.js";
|
|
25
24
|
const debug = createDebug("claude-handler");
|
|
26
25
|
// ============================================================================
|
|
@@ -31,18 +30,18 @@ const debug = createDebug("claude-handler");
|
|
|
31
30
|
*
|
|
32
31
|
* Claude Characteristics:
|
|
33
32
|
* - Excellent at following detailed instructions
|
|
34
|
-
* - Native structured output via response_format (requires strict schema)
|
|
35
33
|
* - Native tool calling (via Anthropic messages API)
|
|
36
34
|
* - Performs best with anti-XML tool calling instructions
|
|
37
35
|
*
|
|
38
|
-
* Output Modes:
|
|
39
|
-
* -
|
|
40
|
-
* - hint: JSON schema in prompt (medium speed, usually valid JSON)
|
|
36
|
+
* Output Modes (TEXT + HINT only):
|
|
37
|
+
* - hint: JSON schema in prompt with DECISION GUIDE (~95% reliable)
|
|
41
38
|
* - text: Plain text output for str return types (fastest)
|
|
42
39
|
*
|
|
40
|
+
* Native response_format (strict mode) is not used. HINT mode with
|
|
41
|
+
* detailed prompt instructions provides sufficient reliability (~95%)
|
|
42
|
+
* without cross-runtime incompatibilities and grammar compilation overhead.
|
|
43
|
+
*
|
|
43
44
|
* Best Practices (from Anthropic docs):
|
|
44
|
-
* - Use response_format for guaranteed JSON schema compliance
|
|
45
|
-
* - Schema must have additionalProperties: false on all objects
|
|
46
45
|
* - Add anti-XML instructions to prevent <invoke> style tool calls
|
|
47
46
|
* - Use one tool call at a time for better reliability
|
|
48
47
|
*/
|
|
@@ -51,9 +50,8 @@ export class ClaudeHandler {
|
|
|
51
50
|
/**
|
|
52
51
|
* Prepare request parameters for Claude API with output mode support.
|
|
53
52
|
*
|
|
54
|
-
* Output Mode Strategy:
|
|
55
|
-
* -
|
|
56
|
-
* - hint: No response_format, rely on prompt instructions (medium speed)
|
|
53
|
+
* Output Mode Strategy (TEXT + HINT only):
|
|
54
|
+
* - hint: No response_format, rely on prompt instructions (~95% reliable)
|
|
57
55
|
* - text: No response_format, plain text output (fastest)
|
|
58
56
|
*
|
|
59
57
|
* Message Format (Anthropic-specific):
|
|
@@ -66,11 +64,11 @@ export class ClaudeHandler {
|
|
|
66
64
|
const determinedMode = this.determineOutputMode(outputSchema, outputMode);
|
|
67
65
|
// Convert messages to Vercel AI SDK format (shared utility)
|
|
68
66
|
let convertedMessages = convertMessagesToVercelFormat(messages);
|
|
69
|
-
//
|
|
70
|
-
//
|
|
67
|
+
// Note: In hint mode, JSON instructions are added by formatSystemPrompt() which
|
|
68
|
+
// is called by llm-provider.ts before prepareRequest(). We don't duplicate here.
|
|
69
|
+
// The formatSystemPrompt() method handles the DECISION GUIDE when tools are present.
|
|
71
70
|
if (determinedMode === "hint" && outputSchema) {
|
|
72
|
-
|
|
73
|
-
debug(`Using hint mode with JSON instructions in prompt for schema: ${outputSchema.name}`);
|
|
71
|
+
debug(`Using hint mode (JSON instructions added by formatSystemPrompt)`);
|
|
74
72
|
}
|
|
75
73
|
const request = {
|
|
76
74
|
messages: convertedMessages,
|
|
@@ -91,29 +89,13 @@ export class ClaudeHandler {
|
|
|
91
89
|
if (topP !== undefined) {
|
|
92
90
|
request.topP = topP;
|
|
93
91
|
}
|
|
94
|
-
// Only add response_format in "strict" mode (not used for Claude anymore)
|
|
95
|
-
if (determinedMode === "strict" && outputSchema) {
|
|
96
|
-
// Claude requires additionalProperties: false on all object types
|
|
97
|
-
// Unlike OpenAI/Gemini, Claude doesn't require all properties in 'required'
|
|
98
|
-
const strictSchema = makeSchemaStrict(outputSchema.schema, { addAllRequired: false });
|
|
99
|
-
request.responseFormat = {
|
|
100
|
-
type: "json_schema",
|
|
101
|
-
jsonSchema: {
|
|
102
|
-
name: outputSchema.name,
|
|
103
|
-
schema: strictSchema,
|
|
104
|
-
strict: false, // Allow optional fields with defaults
|
|
105
|
-
},
|
|
106
|
-
};
|
|
107
|
-
debug(`Using strict mode with response_format for schema: ${outputSchema.name}`);
|
|
108
|
-
}
|
|
109
92
|
return request;
|
|
110
93
|
}
|
|
111
94
|
/**
|
|
112
95
|
* Format system prompt for Claude with output mode support.
|
|
113
96
|
*
|
|
114
|
-
* Output Mode Strategy:
|
|
115
|
-
* -
|
|
116
|
-
* - hint: Add detailed JSON schema instructions in prompt
|
|
97
|
+
* Output Mode Strategy (TEXT + HINT only):
|
|
98
|
+
* - hint: Add detailed JSON schema instructions with DECISION GUIDE in prompt
|
|
117
99
|
* - text: No JSON instructions (plain text output)
|
|
118
100
|
*/
|
|
119
101
|
formatSystemPrompt(basePrompt, toolSchemas, outputSchema, outputMode) {
|
|
@@ -131,15 +113,6 @@ export class ClaudeHandler {
|
|
|
131
113
|
// Text mode: No JSON instructions
|
|
132
114
|
return systemContent;
|
|
133
115
|
}
|
|
134
|
-
if (determinedMode === "strict") {
|
|
135
|
-
// Strict mode: Minimal instructions (response_format handles schema)
|
|
136
|
-
if (outputSchema) {
|
|
137
|
-
systemContent += `
|
|
138
|
-
|
|
139
|
-
Your final response will be structured as JSON matching the ${outputSchema.name} format.`;
|
|
140
|
-
}
|
|
141
|
-
return systemContent;
|
|
142
|
-
}
|
|
143
116
|
// Hint mode: Add detailed JSON schema instructions
|
|
144
117
|
if (determinedMode === "hint" && outputSchema) {
|
|
145
118
|
const properties = (outputSchema.schema.properties ?? {});
|
|
@@ -156,8 +129,18 @@ Your final response will be structured as JSON matching the ${outputSchema.name}
|
|
|
156
129
|
}
|
|
157
130
|
const fieldsText = fieldDescriptions.join("\n");
|
|
158
131
|
const exampleFormat = Object.fromEntries(Object.entries(properties).map(([k, v]) => [k, `<${v.type ?? "value"}>`]));
|
|
132
|
+
// Add DECISION GUIDE when tools are present to help Claude know when NOT to use tools
|
|
133
|
+
let decisionGuide = "";
|
|
134
|
+
if (toolSchemas && toolSchemas.length > 0) {
|
|
135
|
+
decisionGuide = `
|
|
136
|
+
DECISION GUIDE:
|
|
137
|
+
- If your answer requires real-time data (weather, calculations, etc.), call the appropriate tool FIRST, then format your response as JSON.
|
|
138
|
+
- If your answer is general knowledge (like facts, explanations, definitions), directly return your response as JSON WITHOUT calling tools.
|
|
139
|
+
- After calling a tool and receiving results, STOP calling tools and return your final JSON response.
|
|
140
|
+
`;
|
|
141
|
+
}
|
|
159
142
|
systemContent += `
|
|
160
|
-
|
|
143
|
+
${decisionGuide}
|
|
161
144
|
RESPONSE FORMAT:
|
|
162
145
|
You MUST respond with valid JSON matching this schema:
|
|
163
146
|
{
|
|
@@ -180,10 +163,10 @@ CRITICAL: Your response must be ONLY the raw JSON object.
|
|
|
180
163
|
getCapabilities() {
|
|
181
164
|
return {
|
|
182
165
|
nativeToolCalling: true, // Claude has native function calling
|
|
183
|
-
structuredOutput:
|
|
166
|
+
structuredOutput: false, // Uses HINT mode (prompt-based), not native response_format
|
|
184
167
|
streaming: true, // Supports streaming
|
|
185
168
|
vision: true, // Claude 3+ supports vision
|
|
186
|
-
jsonMode:
|
|
169
|
+
jsonMode: false, // No native JSON mode used
|
|
187
170
|
promptCaching: false, // TODO: Re-enable via experimental_providerOptions
|
|
188
171
|
};
|
|
189
172
|
}
|
|
@@ -214,70 +197,6 @@ CRITICAL: Your response must be ONLY the raw JSON object.
|
|
|
214
197
|
// and native structured output is slow. Prompt-based JSON is fast and reliable.
|
|
215
198
|
return "hint";
|
|
216
199
|
}
|
|
217
|
-
// ==========================================================================
|
|
218
|
-
// Private Helper Methods
|
|
219
|
-
// ==========================================================================
|
|
220
|
-
/**
|
|
221
|
-
* Append JSON schema instructions to the system prompt for "hint" mode.
|
|
222
|
-
*
|
|
223
|
-
* This enables fast, prompt-based structured output instead of slow generateObject().
|
|
224
|
-
* Claude is excellent at following detailed instructions, so this approach is
|
|
225
|
-
* ~95% reliable while being much faster than native structured output.
|
|
226
|
-
*
|
|
227
|
-
* @param messages - Messages with system prompt
|
|
228
|
-
* @param outputSchema - Schema to include in instructions
|
|
229
|
-
* @returns Messages with updated system prompt
|
|
230
|
-
*/
|
|
231
|
-
appendJsonInstructionsToSystemPrompt(messages, outputSchema) {
|
|
232
|
-
const properties = (outputSchema.schema.properties ?? {});
|
|
233
|
-
const required = (outputSchema.schema.required ?? []);
|
|
234
|
-
// Build human-readable schema description
|
|
235
|
-
const fieldDescriptions = [];
|
|
236
|
-
for (const [fieldName, fieldSchema] of Object.entries(properties)) {
|
|
237
|
-
const fieldType = fieldSchema.type ?? "any";
|
|
238
|
-
const isRequired = required.includes(fieldName);
|
|
239
|
-
const reqMarker = isRequired ? " (required)" : " (optional)";
|
|
240
|
-
const desc = fieldSchema.description ?? "";
|
|
241
|
-
const descText = desc ? ` - ${desc}` : "";
|
|
242
|
-
fieldDescriptions.push(` - ${fieldName}: ${fieldType}${reqMarker}${descText}`);
|
|
243
|
-
}
|
|
244
|
-
const fieldsText = fieldDescriptions.join("\n");
|
|
245
|
-
const exampleFormat = Object.fromEntries(Object.entries(properties).map(([k, v]) => [k, `<${v.type ?? "value"}>`]));
|
|
246
|
-
const jsonInstructions = `
|
|
247
|
-
|
|
248
|
-
RESPONSE FORMAT:
|
|
249
|
-
You MUST respond with valid JSON matching the ${outputSchema.name} schema:
|
|
250
|
-
{
|
|
251
|
-
${fieldsText}
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
Example format:
|
|
255
|
-
${JSON.stringify(exampleFormat, null, 2)}
|
|
256
|
-
|
|
257
|
-
CRITICAL: Your response must be ONLY the raw JSON object.
|
|
258
|
-
- DO NOT wrap in markdown code fences (\`\`\`json or \`\`\`)
|
|
259
|
-
- DO NOT include any text before or after the JSON
|
|
260
|
-
- Start directly with { and end with }`;
|
|
261
|
-
// Find and update system message, or prepend one if none exists
|
|
262
|
-
const updatedMessages = [...messages];
|
|
263
|
-
const systemIndex = updatedMessages.findIndex((m) => m.role === "system");
|
|
264
|
-
if (systemIndex >= 0) {
|
|
265
|
-
// Append to existing system message
|
|
266
|
-
const systemMsg = updatedMessages[systemIndex];
|
|
267
|
-
updatedMessages[systemIndex] = {
|
|
268
|
-
...systemMsg,
|
|
269
|
-
content: (systemMsg.content ?? "") + jsonInstructions,
|
|
270
|
-
};
|
|
271
|
-
}
|
|
272
|
-
else {
|
|
273
|
-
// Prepend new system message with JSON instructions
|
|
274
|
-
updatedMessages.unshift({
|
|
275
|
-
role: "system",
|
|
276
|
-
content: jsonInstructions.trim(),
|
|
277
|
-
});
|
|
278
|
-
}
|
|
279
|
-
return updatedMessages;
|
|
280
|
-
}
|
|
281
200
|
}
|
|
282
201
|
// Register with the registry
|
|
283
202
|
ProviderHandlerRegistry.register("anthropic", ClaudeHandler);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"claude-handler.js","sourceRoot":"","sources":["../../src/provider-handlers/claude-handler.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"claude-handler.js","sourceRoot":"","sources":["../../src/provider-handlers/claude-handler.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE1C,OAAO,EACL,6BAA6B,EAC7B,sBAAsB,EACtB,2BAA2B,GAO5B,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,uBAAuB,EAAE,MAAM,gCAAgC,CAAC;AAEzE,MAAM,KAAK,GAAG,WAAW,CAAC,gBAAgB,CAAC,CAAC;AAE5C,+EAA+E;AAC/E,gCAAgC;AAChC,+EAA+E;AAE/E;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,OAAO,aAAa;IACf,MAAM,GAAG,WAAW,CAAC;IAE9B;;;;;;;;;;;OAWG;IACH,cAAc,CACZ,QAAsB,EACtB,KAA0B,EAC1B,YAAiC,EACjC,OAMC;QAED,MAAM,EAAE,UAAU,EAAE,WAAW,EAAE,eAAe,EAAE,SAAS,EAAE,IAAI,EAAE,GAAG,IAAI,EAAE,GAAG,OAAO,IAAI,EAAE,CAAC;QAC7F,MAAM,cAAc,GAAG,IAAI,CAAC,mBAAmB,CAAC,YAAY,EAAE,UAAU,CAAC,CAAC;QAE1E,4DAA4D;QAC5D,IAAI,iBAAiB,GAAG,6BAA6B,CAAC,QAAQ,CAAC,CAAC;QAEhE,gFAAgF;QAChF,iFAAiF;QACjF,qFAAqF;QACrF,IAAI,cAAc,KAAK,MAAM,IAAI,YAAY,EAAE,CAAC;YAC9C,KAAK,CAAC,iEAAiE,CAAC,CAAC;QAC3E,CAAC;QAED,MAAM,OAAO,GAAoB;YAC/B,QAAQ,EAAE,iBAAiB;YAC3B,GAAG,IAAI;SACR,CAAC;QAEF,wBAAwB;QACxB,sEAAsE;QACtE,IAAI,KAAK,IAAI,KAAK,CAAC,MAA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;;;;;uCAKD,CAAC;QACpC,CAAC;QAED,OAAO,aAAa,CAAC;IACvB,CAAC;IAED;;OAEG;IACH,eAAe;QACb,OAAO;YACL,iBAAiB,EAAE,IAAI,EAAE,qCAAqC;YAC9D,gBAAgB,EAAE,KAAK,EAAE,4DAA4D;YACrF,SAAS,EAAE,IAAI,EAAE,qBAAqB;YACtC,MAAM,EAAE,IAAI,EAAE,4BAA4B;YAC1C,QAAQ,EAAE,KAAK,EAAE,2BAA2B;YAC5C,aAAa,EAAE,KAAK,EAAE,mDAAmD;SAC1E,CAAC;IACJ,CAAC;IAED;;;;;;;;;;;;;OAaG;IACH,mBAAmB,CACjB,YAAiC,EACjC,YAAyB;QAEzB,0BAA0B;QAC1B,IAAI,YAAY,EAAE,CAAC;YACjB,OAAO,YAAY,CAAC;QACtB,CAAC;QAED,iDAAiD;QACjD,IAAI,CAAC,YAAY,EAAE,CAAC;YAClB,OAAO,MAAM,CAAC;QAChB,CAAC;QAED,+EAA+E;QAC/E,gFAAgF;QAChF,OAAO,MAAM,CAAC;IAChB,CAAC;CAEF;AAED,6BAA6B;AAC7B,uBAAuB,CAAC,QAAQ,CAAC,WAAW,EAAE,aAAa,CAAC,CAAC"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Gemini/Google provider handler.
|
|
2
|
+
* Gemini/Google provider handler for Gemini 3.x models.
|
|
3
3
|
*
|
|
4
|
-
* Optimized for Gemini models (Gemini
|
|
4
|
+
* Optimized for Gemini models (Gemini 3 Flash Preview, Gemini 2.0 Flash, etc.)
|
|
5
5
|
* using Google's best practices for tool calling and structured output.
|
|
6
6
|
*
|
|
7
7
|
* Features:
|
|
@@ -34,12 +34,12 @@ import { type ProviderHandler, type VendorCapabilities, type ToolSchema, type Ou
|
|
|
34
34
|
* - Similar schema enforcement requirements
|
|
35
35
|
*
|
|
36
36
|
* Supported Models (via Vercel AI SDK):
|
|
37
|
+
* - gemini-3-flash-preview (reasoning support)
|
|
38
|
+
* - gemini-3-pro-preview (advanced reasoning)
|
|
37
39
|
* - gemini-2.0-flash (fast, efficient)
|
|
38
40
|
* - gemini-2.0-flash-lite (fastest, most efficient)
|
|
39
41
|
* - gemini-1.5-pro (high capability)
|
|
40
42
|
* - gemini-1.5-flash (balanced)
|
|
41
|
-
* - gemini-3-flash-preview (reasoning support)
|
|
42
|
-
* - gemini-3-pro-preview (advanced reasoning)
|
|
43
43
|
*/
|
|
44
44
|
export declare class GeminiHandler implements ProviderHandler {
|
|
45
45
|
readonly vendor = "google";
|
|
@@ -59,13 +59,16 @@ export declare class GeminiHandler implements ProviderHandler {
|
|
|
59
59
|
[key: string]: unknown;
|
|
60
60
|
}): PreparedRequest;
|
|
61
61
|
/**
|
|
62
|
-
* Format system prompt for Gemini
|
|
62
|
+
* Format system prompt for Gemini with output mode support.
|
|
63
63
|
*
|
|
64
64
|
* Gemini Strategy:
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
65
|
+
* - strict mode: Brief JSON note (response_format handles schema)
|
|
66
|
+
* - hint mode: Detailed JSON schema instructions in prompt
|
|
67
|
+
* - text mode: No JSON instructions
|
|
68
|
+
*
|
|
69
|
+
* When tools are present, llm-provider forces "hint" mode because
|
|
70
|
+
* generateObject() doesn't support tools, so we need prompt-based
|
|
71
|
+
* JSON instructions to ensure structured output.
|
|
69
72
|
*/
|
|
70
73
|
formatSystemPrompt(basePrompt: string, toolSchemas: ToolSchema[] | null, outputSchema: OutputSchema | null, outputMode?: OutputMode): string;
|
|
71
74
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"gemini-handler.d.ts","sourceRoot":"","sources":["../../src/provider-handlers/gemini-handler.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAGH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,
|
|
1
|
+
{"version":3,"file":"gemini-handler.d.ts","sourceRoot":"","sources":["../../src/provider-handlers/gemini-handler.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAGH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,EAML,KAAK,eAAe,EACpB,KAAK,kBAAkB,EACvB,KAAK,UAAU,EACf,KAAK,YAAY,EACjB,KAAK,eAAe,EACpB,KAAK,UAAU,EAChB,MAAM,uBAAuB,CAAC;AAK/B;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,qBAAa,aAAc,YAAW,eAAe;IACnD,QAAQ,CAAC,MAAM,YAAY;IAE3B;;;;;;;OAOG;IACH,cAAc,CACZ,QAAQ,EAAE,UAAU,EAAE,EACtB,KAAK,EAAE,UAAU,EAAE,GAAG,IAAI,EAC1B,YAAY,EAAE,YAAY,GAAG,IAAI,EACjC,OAAO,CAAC,EAAE;QACR,UAAU,CAAC,EAAE,UAAU,CAAC;QACxB,WAAW,CAAC,EAAE,MAAM,CAAC;QACrB,eAAe,CAAC,EAAE,MAAM,CAAC;QACzB,IAAI,CAAC,EAAE,MAAM,CAAC;QACd,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;KACxB,GACA,eAAe;IAwDlB;;;;;;;;;;;OAWG;IACH,kBAAkB,CAChB,UAAU,EAAE,MAAM,EAClB,WAAW,EAAE,UAAU,EAAE,GAAG,IAAI,EAChC,YAAY,EAAE,YAAY,GAAG,IAAI,EACjC,UAAU,CAAC,EAAE,UAAU,GACtB,MAAM;IA0ET;;OAEG;IACH,eAAe,IAAI,kBAAkB;IAUrC;;;;OAIG;IACH,mBAAmB,CACjB,YAAY,EAAE,YAAY,GAAG,IAAI,EACjC,YAAY,CAAC,EAAE,UAAU,GACxB,UAAU;CAGd"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Gemini/Google provider handler.
|
|
2
|
+
* Gemini/Google provider handler for Gemini 3.x models.
|
|
3
3
|
*
|
|
4
|
-
* Optimized for Gemini models (Gemini
|
|
4
|
+
* Optimized for Gemini models (Gemini 3 Flash Preview, Gemini 2.0 Flash, etc.)
|
|
5
5
|
* using Google's best practices for tool calling and structured output.
|
|
6
6
|
*
|
|
7
7
|
* Features:
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
* - https://ai.google.dev/gemini-api/docs
|
|
18
18
|
*/
|
|
19
19
|
import { createDebug } from "../debug.js";
|
|
20
|
-
import { convertMessagesToVercelFormat, makeSchemaStrict, defaultDetermineOutputMode, BASE_TOOL_INSTRUCTIONS, } from "./provider-handler.js";
|
|
20
|
+
import { convertMessagesToVercelFormat, makeSchemaStrict, sanitizeSchemaForStructuredOutput, defaultDetermineOutputMode, BASE_TOOL_INSTRUCTIONS, } from "./provider-handler.js";
|
|
21
21
|
import { ProviderHandlerRegistry } from "./provider-handler-registry.js";
|
|
22
22
|
const debug = createDebug("gemini-handler");
|
|
23
23
|
/**
|
|
@@ -36,12 +36,12 @@ const debug = createDebug("gemini-handler");
|
|
|
36
36
|
* - Similar schema enforcement requirements
|
|
37
37
|
*
|
|
38
38
|
* Supported Models (via Vercel AI SDK):
|
|
39
|
+
* - gemini-3-flash-preview (reasoning support)
|
|
40
|
+
* - gemini-3-pro-preview (advanced reasoning)
|
|
39
41
|
* - gemini-2.0-flash (fast, efficient)
|
|
40
42
|
* - gemini-2.0-flash-lite (fastest, most efficient)
|
|
41
43
|
* - gemini-1.5-pro (high capability)
|
|
42
44
|
* - gemini-1.5-flash (balanced)
|
|
43
|
-
* - gemini-3-flash-preview (reasoning support)
|
|
44
|
-
* - gemini-3-pro-preview (advanced reasoning)
|
|
45
45
|
*/
|
|
46
46
|
export class GeminiHandler {
|
|
47
47
|
vendor = "google";
|
|
@@ -85,7 +85,8 @@ export class GeminiHandler {
|
|
|
85
85
|
// Hint mode relies on prompt instructions instead
|
|
86
86
|
if (determinedMode === "strict") {
|
|
87
87
|
// Vercel AI SDK translates this to Gemini's native format
|
|
88
|
-
const
|
|
88
|
+
const sanitizedSchema = sanitizeSchemaForStructuredOutput(outputSchema.schema);
|
|
89
|
+
const strictSchema = makeSchemaStrict(sanitizedSchema, { addAllRequired: true });
|
|
89
90
|
request.responseFormat = {
|
|
90
91
|
type: "json_schema",
|
|
91
92
|
jsonSchema: {
|
|
@@ -99,13 +100,16 @@ export class GeminiHandler {
|
|
|
99
100
|
return request;
|
|
100
101
|
}
|
|
101
102
|
/**
|
|
102
|
-
* Format system prompt for Gemini
|
|
103
|
+
* Format system prompt for Gemini with output mode support.
|
|
103
104
|
*
|
|
104
105
|
* Gemini Strategy:
|
|
105
|
-
*
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
*
|
|
106
|
+
* - strict mode: Brief JSON note (response_format handles schema)
|
|
107
|
+
* - hint mode: Detailed JSON schema instructions in prompt
|
|
108
|
+
* - text mode: No JSON instructions
|
|
109
|
+
*
|
|
110
|
+
* When tools are present, llm-provider forces "hint" mode because
|
|
111
|
+
* generateObject() doesn't support tools, so we need prompt-based
|
|
112
|
+
* JSON instructions to ensure structured output.
|
|
109
113
|
*/
|
|
110
114
|
formatSystemPrompt(basePrompt, toolSchemas, outputSchema, outputMode) {
|
|
111
115
|
let systemContent = basePrompt;
|
|
@@ -114,14 +118,61 @@ export class GeminiHandler {
|
|
|
114
118
|
if (toolSchemas && toolSchemas.length > 0) {
|
|
115
119
|
systemContent += BASE_TOOL_INSTRUCTIONS;
|
|
116
120
|
}
|
|
117
|
-
// Skip JSON
|
|
121
|
+
// Skip JSON instructions for text mode or no schema
|
|
118
122
|
if (determinedMode === "text" || !outputSchema) {
|
|
119
123
|
return systemContent;
|
|
120
124
|
}
|
|
121
|
-
//
|
|
122
|
-
|
|
125
|
+
// Strict mode: Brief note (response_format handles enforcement)
|
|
126
|
+
if (determinedMode === "strict") {
|
|
127
|
+
systemContent += `
|
|
123
128
|
|
|
124
129
|
Your final response will be structured as JSON matching the ${outputSchema.name} format.`;
|
|
130
|
+
return systemContent;
|
|
131
|
+
}
|
|
132
|
+
// Hint mode: Add detailed JSON schema instructions
|
|
133
|
+
// This is used when tools are present (can't use generateObject)
|
|
134
|
+
const schema = outputSchema.schema;
|
|
135
|
+
const properties = (schema.properties ?? {});
|
|
136
|
+
const required = (schema.required ?? []);
|
|
137
|
+
// Build human-readable schema description
|
|
138
|
+
const fieldDescriptions = [];
|
|
139
|
+
for (const [fieldName, fieldSchema] of Object.entries(properties)) {
|
|
140
|
+
const fieldType = fieldSchema.type ?? "any";
|
|
141
|
+
const isRequired = required.includes(fieldName);
|
|
142
|
+
const reqMarker = isRequired ? " (required)" : " (optional)";
|
|
143
|
+
const desc = fieldSchema.description;
|
|
144
|
+
const descText = desc ? ` - ${desc}` : "";
|
|
145
|
+
fieldDescriptions.push(` - ${fieldName}: ${fieldType}${reqMarker}${descText}`);
|
|
146
|
+
}
|
|
147
|
+
const fieldsText = fieldDescriptions.join("\n");
|
|
148
|
+
const exampleObj = {};
|
|
149
|
+
for (const [k, v] of Object.entries(properties)) {
|
|
150
|
+
exampleObj[k] = `<${v.type ?? "value"}>`;
|
|
151
|
+
}
|
|
152
|
+
// Add DECISION GUIDE when tools are present to help Gemini know when NOT to use tools
|
|
153
|
+
let decisionGuide = "";
|
|
154
|
+
if (toolSchemas && toolSchemas.length > 0) {
|
|
155
|
+
decisionGuide = `
|
|
156
|
+
DECISION GUIDE:
|
|
157
|
+
- If your answer requires real-time data (weather, calculations, etc.), call the appropriate tool FIRST, then format your response as JSON.
|
|
158
|
+
- If your answer is general knowledge (like facts, explanations, definitions), directly return your response as JSON WITHOUT calling tools.
|
|
159
|
+
`;
|
|
160
|
+
}
|
|
161
|
+
systemContent += `
|
|
162
|
+
${decisionGuide}
|
|
163
|
+
FINAL RESPONSE FORMAT:
|
|
164
|
+
After gathering all needed information using tools, your FINAL response MUST be valid JSON matching this schema:
|
|
165
|
+
{
|
|
166
|
+
${fieldsText}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
Example format:
|
|
170
|
+
${JSON.stringify(exampleObj, null, 2)}
|
|
171
|
+
|
|
172
|
+
IMPORTANT:
|
|
173
|
+
- First, use the available tools to gather information if needed
|
|
174
|
+
- Only after you have all the data, provide your final JSON response
|
|
175
|
+
- The final response must be ONLY valid JSON - no markdown code fences, no preamble text`;
|
|
125
176
|
return systemContent;
|
|
126
177
|
}
|
|
127
178
|
/**
|
|
@@ -146,7 +197,7 @@ Your final response will be structured as JSON matching the ${outputSchema.name}
|
|
|
146
197
|
}
|
|
147
198
|
}
|
|
148
199
|
// Register with the registry
|
|
149
|
-
// Use "
|
|
150
|
-
//
|
|
151
|
-
ProviderHandlerRegistry.register("
|
|
200
|
+
// Use "gemini" as vendor name to match model prefix (e.g., "gemini/gemini-3-flash-preview")
|
|
201
|
+
// This is consistent with Python SDK's registration
|
|
202
|
+
ProviderHandlerRegistry.register("gemini", GeminiHandler);
|
|
152
203
|
//# sourceMappingURL=gemini-handler.js.map
|