@jsonstudio/llms 0.6.802 → 0.6.938
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/bridge/routecodex-adapter.d.ts +74 -0
- package/dist/config-unified/enhanced-path-resolver.d.ts +5 -0
- package/dist/config-unified/unified-config.d.ts +26 -0
- package/dist/conversion/codec-registry.d.ts +10 -0
- package/dist/conversion/codecs/gemini-openai-codec.d.ts +16 -0
- package/dist/conversion/codecs/openai-openai-codec.d.ts +12 -0
- package/dist/conversion/codecs/responses-openai-codec.d.ts +12 -0
- package/dist/conversion/compat/profiles/chat-gemini.json +12 -0
- package/dist/conversion/config/config-manager.d.ts +212 -0
- package/dist/conversion/hub/config/types.d.ts +26 -0
- package/dist/conversion/hub/core/detour-registry.d.ts +9 -0
- package/dist/conversion/hub/core/hub-context.d.ts +21 -0
- package/dist/conversion/hub/core/index.d.ts +3 -0
- package/dist/conversion/hub/core/stage-driver.d.ts +30 -0
- package/dist/conversion/hub/format-adapters/anthropic-format-adapter.d.ts +16 -0
- package/dist/conversion/hub/format-adapters/chat-format-adapter.d.ts +17 -0
- package/dist/conversion/hub/format-adapters/gemini-format-adapter.d.ts +16 -0
- package/dist/conversion/hub/format-adapters/index.d.ts +21 -0
- package/dist/conversion/hub/hub-feature.d.ts +1 -0
- package/dist/conversion/hub/node-support.d.ts +19 -0
- package/dist/conversion/hub/pipeline/compat/compat-pipeline-executor.js +11 -0
- package/dist/conversion/hub/pipeline/compat/compat-types.d.ts +3 -0
- package/dist/conversion/hub/pipeline/hub-pipeline.d.ts +7 -0
- package/dist/conversion/hub/pipeline/hub-pipeline.js +71 -14
- package/dist/conversion/hub/pipeline/stages/req_process/req_process_stage1_tool_governance/index.js +4 -0
- package/dist/conversion/hub/pipeline/stages/resp_outbound/resp_outbound_stage1_client_remap/index.js +23 -1
- package/dist/conversion/hub/pipelines/inbound.d.ts +22 -0
- package/dist/conversion/hub/pipelines/outbound.d.ts +22 -0
- package/dist/conversion/hub/policy/policy-engine.d.ts +46 -0
- package/dist/conversion/hub/policy/policy-engine.js +176 -0
- package/dist/conversion/hub/policy/protocol-spec.d.ts +50 -0
- package/dist/conversion/hub/policy/protocol-spec.js +105 -0
- package/dist/conversion/hub/process/chat-process.d.ts +32 -0
- package/dist/conversion/hub/registry.d.ts +28 -0
- package/dist/conversion/hub/response/chat-response-utils.d.ts +6 -0
- package/dist/conversion/hub/response/provider-response.js +31 -0
- package/dist/conversion/hub/semantic-mappers/gemini-mapper.d.ts +7 -0
- package/dist/conversion/hub/semantic-mappers/gemini-mapper.js +87 -1
- package/dist/conversion/hub/semantic-mappers/index.d.ts +4 -0
- package/dist/conversion/hub/semantic-mappers/responses-mapper.d.ts +21 -0
- package/dist/conversion/hub/standardized-bridge.d.ts +12 -0
- package/dist/conversion/hub/types/chat-schema.d.ts +112 -0
- package/dist/conversion/hub/types/errors.d.ts +5 -0
- package/dist/conversion/hub/types/format-envelope.d.ts +7 -0
- package/dist/conversion/hub/types/index.d.ts +6 -0
- package/dist/conversion/hub/types/json.d.ts +9 -0
- package/dist/conversion/hub/types/node.d.ts +31 -0
- package/dist/conversion/responses/responses-openai-bridge.js +263 -10
- package/dist/conversion/schema-validator.d.ts +7 -0
- package/dist/conversion/shared/args-mapping.d.ts +18 -0
- package/dist/conversion/shared/chat-request-filters.d.ts +9 -0
- package/dist/conversion/shared/errors.d.ts +1 -1
- package/dist/conversion/shared/gemini-tool-utils.js +61 -0
- package/dist/conversion/shared/jsonish.d.ts +3 -0
- package/dist/conversion/shared/mcp-injection.d.ts +2 -0
- package/dist/conversion/shared/media.d.ts +1 -0
- package/dist/conversion/shared/openai-message-normalize.d.ts +1 -0
- package/dist/conversion/shared/payload-budget.d.ts +13 -0
- package/dist/conversion/shared/reasoning-mapping.d.ts +5 -0
- package/dist/conversion/shared/responses-request-adapter.d.ts +1 -28
- package/dist/conversion/shared/responses-request-adapter.js +1 -430
- package/dist/conversion/shared/snapshot-hooks.js +58 -3
- package/dist/conversion/shared/tool-governor.js +8 -2
- package/dist/conversion/shared/tool-harvester.d.ts +31 -0
- package/dist/conversion/shared/tool-mapping.js +10 -29
- package/dist/conversion/types.d.ts +33 -0
- package/dist/filters/builtin/add-fields-filter.d.ts +8 -0
- package/dist/filters/builtin/blacklist-filter.d.ts +8 -0
- package/dist/filters/builtin/whitelist-filter.d.ts +8 -0
- package/dist/filters/engine.d.ts +16 -0
- package/dist/filters/special/request-tool-choice-policy.d.ts +11 -0
- package/dist/filters/special/response-finish-invariants.d.ts +11 -0
- package/dist/filters/special/response-openai-to-responses-bridge.d.ts +13 -0
- package/dist/filters/special/response-tool-arguments-blacklist.d.ts +12 -0
- package/dist/filters/special/response-tool-arguments-schema-converge.d.ts +13 -0
- package/dist/filters/special/response-tool-arguments-stringify.d.ts +9 -0
- package/dist/filters/special/response-tool-arguments-whitelist.d.ts +11 -0
- package/dist/filters/special/tool-filter-hooks.d.ts +19 -0
- package/dist/filters/special/tool-post-constraints.d.ts +31 -0
- package/dist/filters/types.d.ts +68 -0
- package/dist/filters/utils/fieldmap-loader.d.ts +2 -0
- package/dist/filters/utils/snapshot-writer.d.ts +10 -0
- package/dist/guidance/index.d.ts +3 -0
- package/dist/guidance/index.js +78 -83
- package/dist/http/sse-response.d.ts +22 -0
- package/dist/router/virtual-router/bootstrap.d.ts +6 -0
- package/dist/router/virtual-router/bootstrap.js +49 -5
- package/dist/router/virtual-router/classifier.d.ts +10 -0
- package/dist/router/virtual-router/engine-selection.js +98 -11
- package/dist/router/virtual-router/engine.js +177 -31
- package/dist/router/virtual-router/error-center.d.ts +10 -0
- package/dist/router/virtual-router/features.d.ts +3 -0
- package/dist/router/virtual-router/routing-instructions.d.ts +23 -1
- package/dist/router/virtual-router/routing-instructions.js +120 -30
- package/dist/router/virtual-router/types.d.ts +11 -0
- package/dist/servertool/engine.js +189 -16
- package/dist/servertool/handlers/apply-patch-guard.js +269 -0
- package/dist/servertool/handlers/exec-command-guard.js +558 -0
- package/dist/servertool/handlers/followup-message-trimmer.d.ts +16 -0
- package/dist/servertool/handlers/followup-message-trimmer.js +198 -0
- package/dist/servertool/handlers/followup-request-builder.d.ts +17 -0
- package/dist/servertool/handlers/followup-request-builder.js +122 -0
- package/dist/servertool/handlers/gemini-empty-reply-continue.js +252 -51
- package/dist/servertool/handlers/iflow-model-error-retry.js +12 -22
- package/dist/servertool/handlers/stop-message-auto.js +237 -75
- package/dist/servertool/handlers/vision.js +15 -27
- package/dist/servertool/handlers/web-search.js +17 -43
- package/dist/servertool/server-side-tools.d.ts +3 -0
- package/dist/servertool/server-side-tools.js +3 -0
- package/dist/sse/json-to-sse/anthropic-json-to-sse-converter.d.ts +2 -1
- package/dist/sse/json-to-sse/chat-json-to-sse-converter.d.ts +80 -0
- package/dist/sse/json-to-sse/event-generators/chat.d.ts +55 -0
- package/dist/sse/json-to-sse/event-generators/responses.d.ts +99 -0
- package/dist/sse/json-to-sse/gemini-json-to-sse-converter.d.ts +2 -1
- package/dist/sse/json-to-sse/responses-json-to-sse-converter.d.ts +80 -0
- package/dist/sse/json-to-sse/sequencers/anthropic-sequencer.d.ts +1 -1
- package/dist/sse/json-to-sse/sequencers/chat-sequencer.d.ts +2 -2
- package/dist/sse/json-to-sse/sequencers/gemini-sequencer.d.ts +1 -1
- package/dist/sse/json-to-sse/sequencers/responses-sequencer.d.ts +40 -0
- package/dist/sse/shared/chat-serializer.d.ts +4 -0
- package/dist/sse/shared/constants.d.ts +272 -0
- package/dist/sse/shared/serializers/anthropic-event-serializer.d.ts +1 -1
- package/dist/sse/shared/serializers/base-serializer.d.ts +158 -0
- package/dist/sse/shared/serializers/chat-event-serializer.d.ts +82 -0
- package/dist/sse/shared/serializers/gemini-event-serializer.d.ts +1 -1
- package/dist/sse/shared/serializers/index.d.ts +2 -1
- package/dist/sse/shared/serializers/responses-event-serializer.d.ts +123 -0
- package/dist/sse/shared/serializers/types.d.ts +51 -0
- package/dist/sse/shared/utils.d.ts +254 -0
- package/dist/sse/shared/writer.d.ts +2 -2
- package/dist/sse/sse-to-json/anthropic-sse-to-json-converter.d.ts +1 -1
- package/dist/sse/sse-to-json/builders/anthropic-response-builder.d.ts +1 -1
- package/dist/sse/sse-to-json/builders/response-builder.d.ts +1 -1
- package/dist/sse/sse-to-json/chat-sse-to-json-converter.d.ts +2 -1
- package/dist/sse/sse-to-json/gemini-sse-to-json-converter.d.ts +2 -1
- package/dist/sse/sse-to-json/parsers/sse-parser.d.ts +73 -0
- package/dist/sse/sse-to-json/responses-sse-to-json-converter.d.ts +1 -1
- package/dist/sse/types/chat-types.d.ts +1 -1
- package/dist/sse/types/responses-types.d.ts +1 -1
- package/dist/tools/apply-patch/execution-capturer.d.ts +13 -0
- package/dist/tools/apply-patch/execution-capturer.js +158 -0
- package/dist/tools/apply-patch/regression-capturer.d.ts +1 -0
- package/dist/tools/apply-patch/regression-capturer.js +5 -4
- package/dist/tools/apply-patch/structured.js +109 -13
- package/dist/tools/apply-patch/validator.js +112 -18
- package/dist/tools/tool-registry.d.ts +8 -0
- package/dist/tools/tool-registry.js +2 -1
- package/package.json +4 -4
- package/dist/conversion/compat/actions/apply-patch-format-fixer.js +0 -233
- package/dist/conversion/config/compat-profiles.json +0 -38
- package/dist/conversion/hub/pipeline/context-limit.d.ts +0 -13
- package/dist/conversion/hub/pipeline/context-limit.js +0 -55
- package/dist/conversion/hub/response/server-side-tools.d.ts +0 -26
- package/dist/conversion/hub/response/server-side-tools.js +0 -383
- package/dist/conversion/shared/bridge-conversation-store.d.ts +0 -41
- package/dist/conversion/shared/bridge-conversation-store.js +0 -279
- package/dist/conversion/shared/bridge-request-adapter.d.ts +0 -28
- package/dist/conversion/shared/bridge-request-adapter.js +0 -430
- package/dist/conversion/shared/responses-id-utils.js +0 -42
- package/dist/conversion/shared/responses-instructions.js +0 -113
- package/dist/conversion/shared/responses-message-utils.d.ts +0 -15
- package/dist/conversion/shared/responses-message-utils.js +0 -206
- package/dist/conversion/shared/responses-metadata.js +0 -1
- package/dist/conversion/shared/responses-output-utils.d.ts +0 -7
- package/dist/conversion/shared/responses-output-utils.js +0 -108
- package/dist/conversion/shared/responses-types.d.ts +0 -33
- package/dist/conversion/shared/tool-normalizers.d.ts +0 -4
- package/dist/conversion/shared/tool-normalizers.js +0 -84
- package/dist/filters/special/request-streaming-to-nonstreaming.d.ts +0 -13
- package/dist/filters/special/request-streaming-to-nonstreaming.js +0 -39
- package/dist/filters/special/response-apply-patch-toon-decode.d.ts +0 -23
- package/dist/filters/special/response-apply-patch-toon-decode.js +0 -460
- package/dist/filters/special/response-tool-arguments-toon-decode.d.ts +0 -10
- package/dist/filters/special/response-tool-arguments-toon-decode.js +0 -154
- package/dist/servertool/flow-types.d.ts +0 -40
- package/dist/servertool/flow-types.js +0 -1
- package/dist/servertool/orchestration-types.d.ts +0 -33
- package/dist/servertool/orchestration-types.js +0 -1
- package/dist/servertool/vision-tool.d.ts +0 -2
- package/dist/servertool/vision-tool.js +0 -185
- package/dist/tools/patch-args-normalizer.d.ts +0 -15
- package/dist/tools/patch-args-normalizer.js +0 -472
- package/dist/utils/toon.d.ts +0 -4
- package/dist/utils/toon.js +0 -75
- /package/dist/{conversion/compat/actions/apply-patch-format-fixer.d.ts → servertool/handlers/apply-patch-guard.d.ts} +0 -0
- /package/dist/{conversion/shared/responses-types.js → servertool/handlers/exec-command-guard.d.ts} +0 -0
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import type { BridgeInputItem, BridgeToolDefinition } from './bridge-message-types.js';
|
|
2
|
-
export interface ResponsesRequestContext {
|
|
3
|
-
requestId?: string;
|
|
4
|
-
instructions?: string;
|
|
5
|
-
originalSystemMessages?: string[];
|
|
6
|
-
input?: BridgeInputItem[];
|
|
7
|
-
include?: unknown;
|
|
8
|
-
store?: unknown;
|
|
9
|
-
toolChoice?: unknown;
|
|
10
|
-
parallelToolCalls?: boolean;
|
|
11
|
-
metadata?: Record<string, unknown>;
|
|
12
|
-
responseFormat?: unknown;
|
|
13
|
-
toolsRaw?: BridgeToolDefinition[];
|
|
14
|
-
stream?: boolean;
|
|
15
|
-
instructionsIsRaw?: boolean;
|
|
16
|
-
isChatPayload?: boolean;
|
|
17
|
-
isResponsesPayload?: boolean;
|
|
18
|
-
}
|
|
19
|
-
export interface BuildChatRequestResult {
|
|
20
|
-
request: Record<string, unknown>;
|
|
21
|
-
toolsNormalized?: Array<Record<string, unknown>>;
|
|
22
|
-
}
|
|
23
|
-
export declare function captureResponsesContext(payload: Record<string, unknown>, dto?: {
|
|
24
|
-
route?: {
|
|
25
|
-
requestId?: string;
|
|
26
|
-
};
|
|
27
|
-
}): ResponsesRequestContext;
|
|
28
|
-
export declare function buildChatRequestFromResponses(payload: Record<string, unknown>, context: ResponsesRequestContext): BuildChatRequestResult;
|
|
@@ -1,430 +0,0 @@
|
|
|
1
|
-
import { normalizeFunctionCallId, normalizeFunctionCallOutputId } from './bridge-id-utils.js';
|
|
2
|
-
import { ensureBridgeInstructions } from './bridge-instructions.js';
|
|
3
|
-
import { serializeToolArguments, coerceBridgeRole, serializeToolOutput } from './bridge-message-utils.js';
|
|
4
|
-
export function captureResponsesContext(payload, dto) {
|
|
5
|
-
ensureBridgeInstructions(payload);
|
|
6
|
-
const context = {
|
|
7
|
-
requestId: dto?.route?.requestId,
|
|
8
|
-
instructions: typeof payload.instructions === 'string' ? payload.instructions : undefined,
|
|
9
|
-
input: Array.isArray(payload.input) ? payload.input : undefined,
|
|
10
|
-
include: payload.include,
|
|
11
|
-
store: payload.store,
|
|
12
|
-
toolChoice: payload.tool_choice,
|
|
13
|
-
parallelToolCalls: typeof payload.parallel_tool_calls === 'boolean' ? payload.parallel_tool_calls : undefined,
|
|
14
|
-
metadata: (payload.metadata && typeof payload.metadata === 'object') ? { ...payload.metadata } : undefined,
|
|
15
|
-
responseFormat: payload.response_format,
|
|
16
|
-
stream: typeof payload.stream === 'boolean' ? payload.stream : undefined,
|
|
17
|
-
instructionsIsRaw: payload.instructions_is_raw === true,
|
|
18
|
-
isChatPayload: Array.isArray(payload.messages)
|
|
19
|
-
};
|
|
20
|
-
if (Array.isArray(payload.tools)) {
|
|
21
|
-
context.toolsRaw = payload.tools;
|
|
22
|
-
}
|
|
23
|
-
context.isResponsesPayload = !context.isChatPayload && Array.isArray(context.input);
|
|
24
|
-
return context;
|
|
25
|
-
}
|
|
26
|
-
export function buildChatRequestFromResponses(payload, context) {
|
|
27
|
-
const toolOutputs = Array.isArray(payload.tool_outputs)
|
|
28
|
-
? payload.tool_outputs
|
|
29
|
-
: undefined;
|
|
30
|
-
const responseId = typeof payload.response_id === 'string' ? payload.response_id : undefined;
|
|
31
|
-
const hasInput = Array.isArray(payload.input) && payload.input.length > 0;
|
|
32
|
-
const submitOnlyMode = !!toolOutputs && !!responseId && !hasInput;
|
|
33
|
-
if (submitOnlyMode) {
|
|
34
|
-
return {
|
|
35
|
-
request: {
|
|
36
|
-
type: 'submit_tool_outputs',
|
|
37
|
-
model: payload.model,
|
|
38
|
-
response_id: responseId,
|
|
39
|
-
tool_outputs: toolOutputs,
|
|
40
|
-
stream: payload.stream === true
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
let toolsNormalized = Array.isArray(payload.tools)
|
|
45
|
-
? normalizeTools(payload.tools)
|
|
46
|
-
: undefined;
|
|
47
|
-
// Inject MCP tools (CCR style) if enabled
|
|
48
|
-
try {
|
|
49
|
-
const enableMcp = String(process.env?.ROUTECODEX_MCP_ENABLE ?? '1') !== '0';
|
|
50
|
-
if (enableMcp) {
|
|
51
|
-
// Discover servers from prior inputs if present (best-effort; depends on client payload)
|
|
52
|
-
const discovered = new Set();
|
|
53
|
-
try {
|
|
54
|
-
const input = Array.isArray(payload.input) ? payload.input : [];
|
|
55
|
-
for (const it of input) {
|
|
56
|
-
const t = (it && typeof it === 'object') ? it : {};
|
|
57
|
-
const isToolCall = (t.type === 'function_call' || t.type === 'tool_call');
|
|
58
|
-
const isToolMsg = (t.type === 'tool_message' || t.type === 'tool_result');
|
|
59
|
-
if (isToolCall) {
|
|
60
|
-
const args = typeof t.arguments === 'string' ? (() => { try {
|
|
61
|
-
return JSON.parse(t.arguments);
|
|
62
|
-
}
|
|
63
|
-
catch {
|
|
64
|
-
return {};
|
|
65
|
-
} })() : (t.arguments || {});
|
|
66
|
-
const sv = (args && typeof args === 'object') ? args.server : undefined;
|
|
67
|
-
if (typeof sv === 'string' && sv.trim())
|
|
68
|
-
discovered.add(sv.trim());
|
|
69
|
-
}
|
|
70
|
-
else if (isToolMsg) {
|
|
71
|
-
const output = t?.output;
|
|
72
|
-
const val = (typeof output === 'string') ? (() => { try {
|
|
73
|
-
return JSON.parse(output);
|
|
74
|
-
}
|
|
75
|
-
catch {
|
|
76
|
-
return null;
|
|
77
|
-
} })() : (output || null);
|
|
78
|
-
const args = (val && typeof val === 'object') ? val.arguments : undefined;
|
|
79
|
-
const sv = (args && typeof args === 'object') ? args.server : undefined;
|
|
80
|
-
if (typeof sv === 'string' && sv.trim())
|
|
81
|
-
discovered.add(sv.trim());
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
catch { /* ignore */ }
|
|
86
|
-
const list = toolsNormalized && Array.isArray(toolsNormalized) ? toolsNormalized : [];
|
|
87
|
-
const have = new Set(list.map((t) => (t?.function?.name || '').toString()));
|
|
88
|
-
const addTool = (def) => { if (!have.has(def.function.name)) {
|
|
89
|
-
list.push(def);
|
|
90
|
-
have.add(def.function.name);
|
|
91
|
-
} };
|
|
92
|
-
const obj = (props, req) => ({ type: 'object', properties: props, required: req, additionalProperties: false });
|
|
93
|
-
const serversRaw = String(process.env?.ROUTECODEX_MCP_SERVERS || '').trim();
|
|
94
|
-
const envServers = serversRaw ? serversRaw.split(',').map((s) => s.trim()).filter(Boolean) : [];
|
|
95
|
-
const merged = Array.from(new Set([...envServers, ...Array.from(discovered)]));
|
|
96
|
-
const serverProp = merged.length ? { type: 'string', enum: merged } : { type: 'string' };
|
|
97
|
-
// 初始仅注入 list_mcp_resources;有已知 server 后再注入其余 MCP 工具
|
|
98
|
-
// 与 Chat 路径保持一致:server 可选(不强制要求)
|
|
99
|
-
addTool({ type: 'function', function: { name: 'list_mcp_resources', strict: true, description: 'List resources from a given MCP server (arguments.server = server label).', parameters: obj({ server: serverProp, filter: { type: 'string' }, root: { type: 'string' } }, [] /* server optional */) } });
|
|
100
|
-
if (merged.length > 0) {
|
|
101
|
-
addTool({ type: 'function', function: { name: 'read_mcp_resource', strict: true, description: 'Read a resource via MCP server.', parameters: obj({ server: serverProp, uri: { type: 'string' } }, ['server', 'uri']) } });
|
|
102
|
-
addTool({ type: 'function', function: { name: 'list_mcp_resource_templates', strict: true, description: 'List resource templates via MCP server.', parameters: obj({ server: serverProp }, ['server']) } });
|
|
103
|
-
}
|
|
104
|
-
toolsNormalized = list;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
catch { /* ignore MCP injection */ }
|
|
108
|
-
const mergedInput = mergeToolOutputsIntoInput(context.input, payload.tool_outputs);
|
|
109
|
-
let messages = mapResponsesInputToChat({
|
|
110
|
-
instructions: context.instructions,
|
|
111
|
-
input: mergedInput,
|
|
112
|
-
toolsNormalized
|
|
113
|
-
});
|
|
114
|
-
const preservedSystemMessages = Array.isArray(context.originalSystemMessages)
|
|
115
|
-
? context.originalSystemMessages
|
|
116
|
-
.map((text) => (typeof text === 'string' ? text.trim() : ''))
|
|
117
|
-
.filter((text) => text.length > 0)
|
|
118
|
-
.map((text) => ({ role: 'system', content: text }))
|
|
119
|
-
: undefined;
|
|
120
|
-
if (preservedSystemMessages && preservedSystemMessages.length) {
|
|
121
|
-
const nonSystemMessages = messages.filter((msg) => String(msg?.role || '').toLowerCase() !== 'system');
|
|
122
|
-
messages = [...preservedSystemMessages, ...nonSystemMessages];
|
|
123
|
-
}
|
|
124
|
-
const request = {
|
|
125
|
-
model: payload.model || 'gpt-4.1-mini',
|
|
126
|
-
messages
|
|
127
|
-
};
|
|
128
|
-
for (const key of ['temperature', 'top_p', 'max_tokens', 'tool_choice', 'parallel_tool_calls', 'user', 'logit_bias', 'seed', 'response_format']) {
|
|
129
|
-
if (payload[key] !== undefined) {
|
|
130
|
-
request[key] = payload[key];
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
if (Array.isArray(toolsNormalized) && toolsNormalized.length) {
|
|
134
|
-
request.tools = toolsNormalized;
|
|
135
|
-
}
|
|
136
|
-
if (typeof payload.stream === 'boolean') {
|
|
137
|
-
request.stream = payload.stream;
|
|
138
|
-
}
|
|
139
|
-
return {
|
|
140
|
-
request,
|
|
141
|
-
toolsNormalized
|
|
142
|
-
};
|
|
143
|
-
}
|
|
144
|
-
function mergeToolOutputsIntoInput(input, toolOutputsRaw) {
|
|
145
|
-
const result = Array.isArray(input) ? [...input] : [];
|
|
146
|
-
if (!Array.isArray(toolOutputsRaw)) {
|
|
147
|
-
return result;
|
|
148
|
-
}
|
|
149
|
-
for (const entry of toolOutputsRaw) {
|
|
150
|
-
if (!entry || typeof entry !== 'object')
|
|
151
|
-
continue;
|
|
152
|
-
const data = entry;
|
|
153
|
-
const callIdRaw = data.tool_call_id ?? data.call_id ?? data.id;
|
|
154
|
-
if (typeof callIdRaw !== 'string' || !callIdRaw.trim())
|
|
155
|
-
continue;
|
|
156
|
-
const outputRaw = data.output;
|
|
157
|
-
const outputText = normalizeToolOutputPayload(outputRaw);
|
|
158
|
-
const normalizedCallId = normalizeFunctionCallId({ callId: callIdRaw, fallback: callIdRaw });
|
|
159
|
-
const outputId = normalizeFunctionCallOutputId({ callId: normalizedCallId, fallback: normalizedCallId });
|
|
160
|
-
result.push({
|
|
161
|
-
type: 'function_call_output',
|
|
162
|
-
id: outputId,
|
|
163
|
-
call_id: normalizedCallId,
|
|
164
|
-
output: outputText
|
|
165
|
-
});
|
|
166
|
-
}
|
|
167
|
-
return result;
|
|
168
|
-
}
|
|
169
|
-
function normalizeToolOutputPayload(payload) {
|
|
170
|
-
if (typeof payload === 'string') {
|
|
171
|
-
return payload;
|
|
172
|
-
}
|
|
173
|
-
if (payload == null) {
|
|
174
|
-
return '';
|
|
175
|
-
}
|
|
176
|
-
try {
|
|
177
|
-
return JSON.stringify(payload);
|
|
178
|
-
}
|
|
179
|
-
catch {
|
|
180
|
-
return String(payload);
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
function mapResponsesInputToChat(options) {
|
|
184
|
-
const { instructions, input, toolsNormalized } = options;
|
|
185
|
-
const messages = [];
|
|
186
|
-
if (typeof instructions === 'string' && instructions.length) {
|
|
187
|
-
messages.push({ role: 'system', content: instructions });
|
|
188
|
-
}
|
|
189
|
-
if (!Array.isArray(input))
|
|
190
|
-
return messages;
|
|
191
|
-
const toolNameById = new Map();
|
|
192
|
-
const toolCallIdAliases = new Map();
|
|
193
|
-
let lastToolCallId = null;
|
|
194
|
-
for (const entry of input) {
|
|
195
|
-
if (!entry || typeof entry !== 'object')
|
|
196
|
-
continue;
|
|
197
|
-
const entryType = typeof entry.type === 'string' ? entry.type.toLowerCase() : 'message';
|
|
198
|
-
if (typeof entry.content === 'string') {
|
|
199
|
-
const directText = (entry.content || '').toString().trim();
|
|
200
|
-
if (directText.length) {
|
|
201
|
-
const normalizedRole = coerceBridgeRole(entry.role || 'user');
|
|
202
|
-
messages.push({ role: normalizedRole, content: directText });
|
|
203
|
-
continue;
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
if (entryType === 'function_call' || entryType === 'tool_call') {
|
|
207
|
-
const rawName = typeof entry.name === 'string' ? entry.name : (typeof entry?.function?.name === 'string' ? entry.function.name : undefined);
|
|
208
|
-
const name = (typeof rawName === 'string' && rawName.includes('.')) ? rawName.slice(rawName.indexOf('.') + 1).trim() : rawName;
|
|
209
|
-
if (typeof name !== 'string' || !name.trim() || name.toLowerCase() === 'tool') {
|
|
210
|
-
continue;
|
|
211
|
-
}
|
|
212
|
-
const args = entry.arguments ?? entry?.function?.arguments ?? {};
|
|
213
|
-
const parsedArgs = args;
|
|
214
|
-
const callIdRaw = typeof entry.id === 'string' ? entry.id : (typeof entry.call_id === 'string' ? entry.call_id : `call_${Math.random().toString(36).slice(2, 8)}`);
|
|
215
|
-
const callId = normalizeFunctionCallId({ callId: callIdRaw, fallback: `fc_call_${messages.length + 1}` });
|
|
216
|
-
if (callIdRaw && callIdRaw !== callId) {
|
|
217
|
-
toolCallIdAliases.set(callIdRaw, callId);
|
|
218
|
-
}
|
|
219
|
-
const serialized = serializeToolArguments(parsedArgs, name, toolsNormalized).trim();
|
|
220
|
-
toolNameById.set(callId, name);
|
|
221
|
-
messages.push({ role: 'assistant', tool_calls: [{ id: callId, type: 'function', function: { name, arguments: serialized } }] });
|
|
222
|
-
lastToolCallId = callId;
|
|
223
|
-
continue;
|
|
224
|
-
}
|
|
225
|
-
if (entryType === 'function_call_output' || entryType === 'tool_result' || entryType === 'tool_message') {
|
|
226
|
-
let toolCallId = entry.tool_call_id || entry.call_id || entry.tool_use_id || entry.id || lastToolCallId;
|
|
227
|
-
if (toolCallId && toolCallIdAliases.has(String(toolCallId))) {
|
|
228
|
-
toolCallId = toolCallIdAliases.get(String(toolCallId)) || toolCallId;
|
|
229
|
-
}
|
|
230
|
-
else if (typeof toolCallId === 'string' && toolCallId.trim().length) {
|
|
231
|
-
const normalized = normalizeFunctionCallId({ callId: toolCallId, fallback: toolCallId });
|
|
232
|
-
if (normalized !== toolCallId) {
|
|
233
|
-
toolCallIdAliases.set(toolCallId, normalized);
|
|
234
|
-
}
|
|
235
|
-
toolCallId = normalized;
|
|
236
|
-
}
|
|
237
|
-
const output = serializeToolOutput(entry);
|
|
238
|
-
if (toolCallId) {
|
|
239
|
-
try {
|
|
240
|
-
let contentStr = output != null ? String(output) : '';
|
|
241
|
-
if (!contentStr || contentStr.trim().length === 0) {
|
|
242
|
-
contentStr = 'Command succeeded (no output).';
|
|
243
|
-
}
|
|
244
|
-
const nm = toolNameById.get(String(toolCallId));
|
|
245
|
-
const toolMsg = { role: 'tool', tool_call_id: String(toolCallId), content: contentStr };
|
|
246
|
-
if (typeof nm === 'string' && nm.trim().length)
|
|
247
|
-
toolMsg.name = nm;
|
|
248
|
-
messages.push(toolMsg);
|
|
249
|
-
}
|
|
250
|
-
catch {
|
|
251
|
-
const fallback = (output ?? '');
|
|
252
|
-
const nm = toolNameById.get(String(toolCallId));
|
|
253
|
-
const toolMsg = { role: 'tool', tool_call_id: String(toolCallId), content: String(fallback) };
|
|
254
|
-
if (typeof nm === 'string' && nm.trim().length)
|
|
255
|
-
toolMsg.name = nm;
|
|
256
|
-
messages.push(toolMsg);
|
|
257
|
-
}
|
|
258
|
-
lastToolCallId = null;
|
|
259
|
-
}
|
|
260
|
-
continue;
|
|
261
|
-
}
|
|
262
|
-
let handledViaExplicitMessage = false;
|
|
263
|
-
if (entry && typeof entry.message === 'object' && Array.isArray(entry.message?.content)) {
|
|
264
|
-
const explicit = entry.message;
|
|
265
|
-
const { text, toolCalls, toolMessages, lastCallId } = processMessageBlocks(Array.isArray(explicit.content) ? explicit.content : [], toolsNormalized, toolNameById, toolCallIdAliases, lastToolCallId);
|
|
266
|
-
if (toolCalls.length)
|
|
267
|
-
messages.push({ role: 'assistant', tool_calls: toolCalls });
|
|
268
|
-
for (const msg of toolMessages)
|
|
269
|
-
messages.push(msg);
|
|
270
|
-
const normalizedRole = coerceBridgeRole((explicit.role ?? entry.role) || 'user');
|
|
271
|
-
if (typeof text === 'string' && text.trim().length) {
|
|
272
|
-
messages.push({ role: normalizedRole, content: text.trim() });
|
|
273
|
-
}
|
|
274
|
-
lastToolCallId = lastCallId;
|
|
275
|
-
handledViaExplicitMessage = true;
|
|
276
|
-
}
|
|
277
|
-
if (!handledViaExplicitMessage) {
|
|
278
|
-
const { text, toolCalls, toolMessages, lastCallId } = processMessageBlocks(Array.isArray(entry.content) ? entry.content : [], toolsNormalized, toolNameById, toolCallIdAliases, lastToolCallId);
|
|
279
|
-
if (toolCalls.length)
|
|
280
|
-
messages.push({ role: 'assistant', tool_calls: toolCalls });
|
|
281
|
-
for (const msg of toolMessages)
|
|
282
|
-
messages.push(msg);
|
|
283
|
-
const normalizedRole = coerceBridgeRole(entry.role || 'user');
|
|
284
|
-
if (typeof text === 'string' && text.trim().length) {
|
|
285
|
-
messages.push({ role: normalizedRole, content: text.trim() });
|
|
286
|
-
}
|
|
287
|
-
lastToolCallId = lastCallId;
|
|
288
|
-
}
|
|
289
|
-
try {
|
|
290
|
-
const t = String(entry.type || '').toLowerCase();
|
|
291
|
-
if ((t === 'input_text' || t === 'text' || t === 'output_text' || t === 'commentary') && typeof entry.text === 'string') {
|
|
292
|
-
const normalizedRole = coerceBridgeRole(entry.role || 'user');
|
|
293
|
-
const s = entry.text;
|
|
294
|
-
if (s && s.length)
|
|
295
|
-
messages.push({ role: normalizedRole, content: s });
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
catch { /* ignore */ }
|
|
299
|
-
}
|
|
300
|
-
return messages;
|
|
301
|
-
}
|
|
302
|
-
function processMessageBlocks(blocks, toolsNormalized, toolNameById, toolCallIdAliases, lastToolCallId) {
|
|
303
|
-
const textParts = [];
|
|
304
|
-
const toolCalls = [];
|
|
305
|
-
const toolMessages = [];
|
|
306
|
-
let currentLastCall = lastToolCallId;
|
|
307
|
-
for (const block of blocks) {
|
|
308
|
-
if (!block || typeof block !== 'object')
|
|
309
|
-
continue;
|
|
310
|
-
const type = typeof block.type === 'string' ? block.type.toLowerCase() : '';
|
|
311
|
-
if (type === 'input_text' || type === 'output_text' || type === 'text' || type === 'commentary') {
|
|
312
|
-
if (typeof block.text === 'string')
|
|
313
|
-
textParts.push(block.text);
|
|
314
|
-
else if (typeof block.content === 'string')
|
|
315
|
-
textParts.push(block.content);
|
|
316
|
-
continue;
|
|
317
|
-
}
|
|
318
|
-
if (type === 'message' && Array.isArray(block.content)) {
|
|
319
|
-
const nested = processMessageBlocks(block.content, toolsNormalized, toolNameById, toolCallIdAliases, currentLastCall);
|
|
320
|
-
if (nested.text)
|
|
321
|
-
textParts.push(nested.text);
|
|
322
|
-
for (const tc of nested.toolCalls)
|
|
323
|
-
toolCalls.push(tc);
|
|
324
|
-
for (const tm of nested.toolMessages)
|
|
325
|
-
toolMessages.push(tm);
|
|
326
|
-
currentLastCall = nested.lastCallId;
|
|
327
|
-
continue;
|
|
328
|
-
}
|
|
329
|
-
if (type === 'function_call') {
|
|
330
|
-
const rawName = typeof block.name === 'string' ? block.name : (typeof block?.function?.name === 'string' ? block.function.name : undefined);
|
|
331
|
-
const name = (typeof rawName === 'string' && rawName.includes('.')) ? rawName.slice(rawName.indexOf('.') + 1).trim() : rawName;
|
|
332
|
-
const args = block.arguments ?? block?.function?.arguments ?? {};
|
|
333
|
-
const parsedArgs = args;
|
|
334
|
-
const callIdRaw = typeof block.id === 'string' ? block.id : (typeof block.call_id === 'string' ? block.call_id : `call_${Math.random().toString(36).slice(2, 8)}`);
|
|
335
|
-
const callId = normalizeFunctionCallId({ callId: callIdRaw, fallback: `fc_call_${toolCalls.length + 1}` });
|
|
336
|
-
if (callIdRaw && callIdRaw !== callId) {
|
|
337
|
-
toolCallIdAliases.set(callIdRaw, callId);
|
|
338
|
-
}
|
|
339
|
-
if (typeof name !== 'string' || !name.trim()) {
|
|
340
|
-
currentLastCall = null;
|
|
341
|
-
continue;
|
|
342
|
-
}
|
|
343
|
-
const serialized = serializeToolArguments(parsedArgs, name, toolsNormalized).trim();
|
|
344
|
-
toolNameById.set(callId, name);
|
|
345
|
-
toolCalls.push({ id: callId, type: 'function', function: { name, arguments: serialized } });
|
|
346
|
-
currentLastCall = callId;
|
|
347
|
-
continue;
|
|
348
|
-
}
|
|
349
|
-
if (type === 'function_call_output' || type === 'tool_result' || type === 'tool_message') {
|
|
350
|
-
let toolCallId = block.tool_call_id || block.call_id || block.tool_use_id || block.id || currentLastCall;
|
|
351
|
-
if (toolCallId && toolCallIdAliases.has(String(toolCallId))) {
|
|
352
|
-
toolCallId = toolCallIdAliases.get(String(toolCallId)) || toolCallId;
|
|
353
|
-
}
|
|
354
|
-
else if (typeof toolCallId === 'string' && toolCallId.trim().length) {
|
|
355
|
-
const normalized = normalizeFunctionCallId({ callId: toolCallId, fallback: toolCallId });
|
|
356
|
-
if (normalized !== toolCallId) {
|
|
357
|
-
toolCallIdAliases.set(toolCallId, normalized);
|
|
358
|
-
}
|
|
359
|
-
toolCallId = normalized;
|
|
360
|
-
}
|
|
361
|
-
const output = serializeToolOutput(block);
|
|
362
|
-
if (toolCallId) {
|
|
363
|
-
try {
|
|
364
|
-
let contentStr = output != null ? String(output) : '';
|
|
365
|
-
if (!contentStr || contentStr.trim().length === 0) {
|
|
366
|
-
contentStr = 'Command succeeded (no output).';
|
|
367
|
-
}
|
|
368
|
-
const nm = toolNameById.get(String(toolCallId));
|
|
369
|
-
const toolMsg = { role: 'tool', tool_call_id: String(toolCallId), content: contentStr };
|
|
370
|
-
if (typeof nm === 'string' && nm.trim().length)
|
|
371
|
-
toolMsg.name = nm;
|
|
372
|
-
toolMessages.push(toolMsg);
|
|
373
|
-
}
|
|
374
|
-
catch {
|
|
375
|
-
const fallback = (output ?? '');
|
|
376
|
-
const nm = toolNameById.get(String(toolCallId));
|
|
377
|
-
const toolMsg = { role: 'tool', tool_call_id: String(toolCallId), content: String(fallback) };
|
|
378
|
-
if (typeof nm === 'string' && nm.trim().length)
|
|
379
|
-
toolMsg.name = nm;
|
|
380
|
-
toolMessages.push(toolMsg);
|
|
381
|
-
}
|
|
382
|
-
currentLastCall = null;
|
|
383
|
-
}
|
|
384
|
-
continue;
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
return {
|
|
388
|
-
text: textParts.length ? textParts.join('\n') : null,
|
|
389
|
-
toolCalls,
|
|
390
|
-
toolMessages,
|
|
391
|
-
lastCallId: currentLastCall
|
|
392
|
-
};
|
|
393
|
-
}
|
|
394
|
-
function normalizeTools(tools) {
|
|
395
|
-
const normalized = [];
|
|
396
|
-
for (const tool of tools) {
|
|
397
|
-
if (!tool || typeof tool !== 'object')
|
|
398
|
-
continue;
|
|
399
|
-
const originalType = typeof tool.type === 'string' ? tool.type : 'function';
|
|
400
|
-
const mappedType = ['function', 'custom'].includes(originalType.toLowerCase())
|
|
401
|
-
? 'function'
|
|
402
|
-
: originalType;
|
|
403
|
-
const fn = (tool.function && typeof tool.function === 'object')
|
|
404
|
-
? tool.function
|
|
405
|
-
: {};
|
|
406
|
-
const topName = typeof tool.name === 'string' ? tool.name : undefined;
|
|
407
|
-
const fnName = typeof fn.name === 'string' ? fn.name : undefined;
|
|
408
|
-
const name = (fnName || topName || '').trim();
|
|
409
|
-
if (!name)
|
|
410
|
-
continue;
|
|
411
|
-
const topDesc = typeof tool.description === 'string' ? tool.description : undefined;
|
|
412
|
-
const fnDesc = typeof fn.description === 'string' ? fn.description : undefined;
|
|
413
|
-
const description = fnDesc || topDesc;
|
|
414
|
-
const fnParams = Object.prototype.hasOwnProperty.call(fn, 'parameters') ? fn.parameters : undefined;
|
|
415
|
-
const topParams = Object.prototype.hasOwnProperty.call(tool, 'parameters') ? tool.parameters : undefined;
|
|
416
|
-
const parameters = fnParams !== undefined ? fnParams : topParams;
|
|
417
|
-
const strict = typeof fn.strict === 'boolean' ? fn.strict :
|
|
418
|
-
typeof tool.strict === 'boolean' ? tool.strict :
|
|
419
|
-
undefined;
|
|
420
|
-
const fnOut = { name };
|
|
421
|
-
if (description)
|
|
422
|
-
fnOut.description = description;
|
|
423
|
-
if (parameters !== undefined)
|
|
424
|
-
fnOut.parameters = parameters;
|
|
425
|
-
if (strict !== undefined)
|
|
426
|
-
fnOut.strict = strict;
|
|
427
|
-
normalized.push({ type: mappedType, function: fnOut });
|
|
428
|
-
}
|
|
429
|
-
return normalized;
|
|
430
|
-
}
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
function sanitizeCore(value) {
|
|
2
|
-
return value
|
|
3
|
-
.replace(/[^A-Za-z0-9_-]/g, '_')
|
|
4
|
-
.replace(/_{2,}/g, '_')
|
|
5
|
-
.replace(/^_+/, '')
|
|
6
|
-
.replace(/_+$/, '');
|
|
7
|
-
}
|
|
8
|
-
function stripPrefix(value) {
|
|
9
|
-
if (typeof value !== 'string')
|
|
10
|
-
return null;
|
|
11
|
-
const trimmed = value.trim();
|
|
12
|
-
if (!trimmed)
|
|
13
|
-
return null;
|
|
14
|
-
let sanitized = sanitizeCore(trimmed);
|
|
15
|
-
if (!sanitized)
|
|
16
|
-
return null;
|
|
17
|
-
if (/^fc[_-]/i.test(sanitized)) {
|
|
18
|
-
sanitized = sanitized.replace(/^fc[_-]?/i, '');
|
|
19
|
-
}
|
|
20
|
-
else if (/^call[_-]/i.test(sanitized)) {
|
|
21
|
-
sanitized = sanitized.replace(/^call[_-]?/i, '');
|
|
22
|
-
}
|
|
23
|
-
if (!sanitized) {
|
|
24
|
-
sanitized = Math.random().toString(36).slice(2, 10);
|
|
25
|
-
}
|
|
26
|
-
return `fc_${sanitized}`;
|
|
27
|
-
}
|
|
28
|
-
function normalizeWithFallback(options) {
|
|
29
|
-
const normalized = stripPrefix(options.callId);
|
|
30
|
-
if (normalized)
|
|
31
|
-
return normalized;
|
|
32
|
-
const fallbackNormalized = stripPrefix(options.fallback);
|
|
33
|
-
if (fallbackNormalized)
|
|
34
|
-
return fallbackNormalized;
|
|
35
|
-
return `fc_${Math.random().toString(36).slice(2, 10)}`;
|
|
36
|
-
}
|
|
37
|
-
export function normalizeFunctionCallId(options) {
|
|
38
|
-
return normalizeWithFallback(options);
|
|
39
|
-
}
|
|
40
|
-
export function normalizeFunctionCallOutputId(options) {
|
|
41
|
-
return normalizeWithFallback(options);
|
|
42
|
-
}
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
function collectTextFromBlocks(blocks) {
|
|
2
|
-
const parts = [];
|
|
3
|
-
for (const block of blocks) {
|
|
4
|
-
if (!block || typeof block !== 'object')
|
|
5
|
-
continue;
|
|
6
|
-
const type = typeof block.type === 'string' ? block.type.toLowerCase() : '';
|
|
7
|
-
if ((type === 'input_text' || type === 'output_text' || type === 'text' || type === 'commentary') && typeof block.text === 'string') {
|
|
8
|
-
const text = block.text.trim();
|
|
9
|
-
if (text)
|
|
10
|
-
parts.push(text);
|
|
11
|
-
continue;
|
|
12
|
-
}
|
|
13
|
-
if (type === 'message' && Array.isArray(block.content)) {
|
|
14
|
-
const nested = collectTextFromBlocks(block.content);
|
|
15
|
-
if (nested)
|
|
16
|
-
parts.push(nested);
|
|
17
|
-
continue;
|
|
18
|
-
}
|
|
19
|
-
if (typeof block.content === 'string') {
|
|
20
|
-
const text = block.content.trim();
|
|
21
|
-
if (text)
|
|
22
|
-
parts.push(text);
|
|
23
|
-
continue;
|
|
24
|
-
}
|
|
25
|
-
if (Array.isArray(block.content)) {
|
|
26
|
-
const nested = collectTextFromBlocks(block.content);
|
|
27
|
-
if (nested)
|
|
28
|
-
parts.push(nested);
|
|
29
|
-
continue;
|
|
30
|
-
}
|
|
31
|
-
if (typeof block.text === 'string') {
|
|
32
|
-
const text = block.text.trim();
|
|
33
|
-
if (text)
|
|
34
|
-
parts.push(text);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
return parts.join('\n');
|
|
38
|
-
}
|
|
39
|
-
function extractSystemInstruction(entry) {
|
|
40
|
-
if (!entry || typeof entry !== 'object')
|
|
41
|
-
return null;
|
|
42
|
-
const roleSource = entry.role ?? entry.message?.role;
|
|
43
|
-
const role = typeof roleSource === 'string' ? roleSource.toLowerCase() : '';
|
|
44
|
-
if (role !== 'system')
|
|
45
|
-
return null;
|
|
46
|
-
const collected = [];
|
|
47
|
-
if (Array.isArray(entry.content)) {
|
|
48
|
-
const text = collectTextFromBlocks(entry.content);
|
|
49
|
-
if (text.trim())
|
|
50
|
-
collected.push(text.trim());
|
|
51
|
-
}
|
|
52
|
-
else if (typeof entry.content === 'string') {
|
|
53
|
-
const text = entry.content.trim();
|
|
54
|
-
if (text)
|
|
55
|
-
collected.push(text);
|
|
56
|
-
}
|
|
57
|
-
if (typeof entry.text === 'string') {
|
|
58
|
-
const text = entry.text.trim();
|
|
59
|
-
if (text)
|
|
60
|
-
collected.push(text);
|
|
61
|
-
}
|
|
62
|
-
const message = entry.message;
|
|
63
|
-
if (message && typeof message === 'object') {
|
|
64
|
-
if (Array.isArray(message.content)) {
|
|
65
|
-
const text = collectTextFromBlocks(message.content);
|
|
66
|
-
if (text.trim())
|
|
67
|
-
collected.push(text.trim());
|
|
68
|
-
}
|
|
69
|
-
else if (typeof message.content === 'string') {
|
|
70
|
-
const text = message.content.trim();
|
|
71
|
-
if (text)
|
|
72
|
-
collected.push(text);
|
|
73
|
-
}
|
|
74
|
-
if (typeof message.text === 'string') {
|
|
75
|
-
const text = String(message.text).trim();
|
|
76
|
-
if (text)
|
|
77
|
-
collected.push(text);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
const merged = collected.join('\n').trim();
|
|
81
|
-
return merged ? merged : null;
|
|
82
|
-
}
|
|
83
|
-
export function ensureResponsesInstructions(payload) {
|
|
84
|
-
let instructions = typeof payload.instructions === 'string' ? payload.instructions : '';
|
|
85
|
-
const hasClientInstruction = typeof payload.instructions === 'string' && payload.instructions.length > 0;
|
|
86
|
-
const input = Array.isArray(payload.input) ? payload.input : undefined;
|
|
87
|
-
if (input && input.length) {
|
|
88
|
-
for (let i = 0; i < input.length; i += 1) {
|
|
89
|
-
const entry = input[i];
|
|
90
|
-
const roleSource = entry?.role ?? entry?.message?.role;
|
|
91
|
-
const role = typeof roleSource === 'string' ? roleSource.toLowerCase() : '';
|
|
92
|
-
if (role === 'system') {
|
|
93
|
-
const text = extractSystemInstruction(entry);
|
|
94
|
-
input.splice(i, 1);
|
|
95
|
-
i -= 1;
|
|
96
|
-
if (text && text.trim()) {
|
|
97
|
-
if (!hasClientInstruction && !instructions) {
|
|
98
|
-
instructions = text.trim();
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
continue;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
if (instructions) {
|
|
106
|
-
payload.instructions = instructions;
|
|
107
|
-
return instructions;
|
|
108
|
-
}
|
|
109
|
-
if (typeof payload.instructions !== 'undefined') {
|
|
110
|
-
delete payload.instructions;
|
|
111
|
-
}
|
|
112
|
-
return undefined;
|
|
113
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type { ResponsesInputItem } from './responses-types.js';
|
|
2
|
-
export declare function normalizeResponseRole(role: unknown): string;
|
|
3
|
-
export declare function normalizeArgumentsBySchema(argsStringOrObj: unknown, _functionName: string | undefined, _tools: unknown): string;
|
|
4
|
-
export declare function normalizeToolOutput(entry: ResponsesInputItem): string | null;
|
|
5
|
-
export interface BuildResponsesInputOptions {
|
|
6
|
-
messages: Array<Record<string, unknown>>;
|
|
7
|
-
tools?: Array<Record<string, unknown>> | undefined;
|
|
8
|
-
}
|
|
9
|
-
export interface BuildResponsesInputResult {
|
|
10
|
-
input: ResponsesInputItem[];
|
|
11
|
-
combinedSystemInstruction?: string;
|
|
12
|
-
latestUserInstruction?: string;
|
|
13
|
-
originalSystemMessages: string[];
|
|
14
|
-
}
|
|
15
|
-
export declare function buildResponsesInputFromChatMessages(options: BuildResponsesInputOptions): BuildResponsesInputResult;
|