mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-11-18 07:58:27 +00:00
fix(sse): preserve tool identity throughout Responses streaming
Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>
This commit is contained in:
@@ -1 +1 @@
|
||||
- **fix(sse):** accumulate streamed tool-call name deltas instead of keeping only one fragment (a provider may split `functions__exec` into `functions__` + `exec`), and resolve a bare/dotted tool name against the request's declared Responses tools — the custom/freeform classification and the `{namespace, name}` identity restore now accept the qualified (`functions__exec`), dotted (`functions.exec`) and unique-bare (`exec`) spellings, so a Codex freeform call can no longer arrive as a plain `function_call` with no `namespace` (which its dispatcher rejects). Ambiguous bare leaves and explicit flat declarations of the same name keep their own identity.
|
||||
- **fix(sse):** accumulate fragmented tool-call names and resolve qualified, dotted and unique bare declarations in both Responses streaming paths. Buffer arguments until a declared identity is ready, preserve that identity and call type through completion, and reset pending state when a call ID is replaced. Explicit flat functions keep precedence over namespaced custom tools; ambiguous or unknown names wait for closure and retain their wire spelling, with the existing MCP continuation fallback preserved. Declaration context retains repeated-letter fragments; complete-name and progressive resends remain idempotent. Before publication, a fragment that extends another declared name takes precedence over an ambiguous suffix resend. Without declarations, suffix resends retain the deterministic deduplication policy. Request-ledger aliases also retain their original namespace and custom type when long wire names were hash-truncated; literal flat declarations and ambiguous aliases remain flat.
|
||||
|
||||
@@ -9,6 +9,7 @@ import { convertResponsesApiFormat } from "../translator/helpers/responsesApiHel
|
||||
import { collectResponsesCustomToolNames } from "../translator/request/openai-responses/additionalTools.ts";
|
||||
import { createResponsesApiTransformStream } from "../transformer/responsesTransformer.ts";
|
||||
import { createSseHeartbeatTransform, HEARTBEAT_SHAPES } from "../utils/sseHeartbeat.ts";
|
||||
import { extractToolSchemaMap } from "../translator/response/openai-responses/toolSchemas.ts";
|
||||
import { SSE_HEARTBEAT_INTERVAL_MS } from "../config/constants.ts";
|
||||
|
||||
/**
|
||||
@@ -55,6 +56,8 @@ export async function handleResponsesCore({
|
||||
(convertedBody as { _namespaceToolIdentityMap?: Map<string, unknown> })
|
||||
._namespaceToolIdentityMap ?? null;
|
||||
|
||||
const toolSchemas = extractToolSchemaMap(convertedBody);
|
||||
|
||||
// Ensure stream is enabled
|
||||
convertedBody.stream = true;
|
||||
|
||||
@@ -97,6 +100,7 @@ export async function handleResponsesCore({
|
||||
const transformStream = createResponsesApiTransformStream(null, undefined, {
|
||||
customToolNames,
|
||||
requestToolIdentityMap,
|
||||
toolSchemas,
|
||||
});
|
||||
const transformedBody = response.body.pipeThrough(transformStream).pipeThrough(
|
||||
createSseHeartbeatTransform({
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
import { appendToolCallArgumentDelta } from "../utils/toolCallArguments.ts";
|
||||
import {
|
||||
appendToolCallNameDelta,
|
||||
matchesCustomToolDeclaration,
|
||||
} from "../utils/toolCallName.ts";
|
||||
import { ToolCallLifecycle } from "../translator/response/openai-responses/toolCallLifecycle.ts";
|
||||
import { shouldParseTextualReasoningTags } from "../handlers/responseSanitizer.ts";
|
||||
import { getReadableReasoningValue } from "../utils/reasoningFields.ts";
|
||||
import {
|
||||
@@ -11,7 +8,6 @@ import {
|
||||
} from "../utils/reasoningPlaceholder.ts";
|
||||
import * as fs from "fs";
|
||||
import * as path from "path";
|
||||
import { resolveRequestToolIdentity } from "../translator/response/openai-responses/requestToolIdentity.ts";
|
||||
import { plaintextCollaborationFields } from "../translator/response/openai-responses/collaborationPlaintextMarker.ts";
|
||||
import { finalizeResponsesTerminalStatus } from "../translator/helpers/responsesTerminalStatus.ts";
|
||||
|
||||
@@ -217,16 +213,11 @@ export function createResponsesApiTransformStream(
|
||||
keepaliveIntervalMs = 3000,
|
||||
options: {
|
||||
customToolNames?: Iterable<string>;
|
||||
toolSchemas?: ReadonlyMap<string, unknown> | null;
|
||||
requestToolIdentityMap?: ReadonlyMap<string, unknown> | null;
|
||||
} = {}
|
||||
) {
|
||||
const customToolNames = new Set(options.customToolNames || []);
|
||||
// #14154 — #7936-style {namespace, name} identity restoration was missing
|
||||
// entirely on this emitter (unlike the streaming translator / non-streaming
|
||||
// client translator). Carried through so function_call/custom_tool_call
|
||||
// items round-trip their namespace, and so the collaboration plaintext
|
||||
// marker below can be gated on the restored namespace.
|
||||
const requestToolIdentityMap = options.requestToolIdentityMap ?? null;
|
||||
const lifecycle = new ToolCallLifecycle(options);
|
||||
const state = {
|
||||
seq: 0,
|
||||
responseId: `resp_${Date.now()}`,
|
||||
@@ -247,7 +238,6 @@ export function createResponsesApiTransformStream(
|
||||
funcNames: {},
|
||||
funcCallIds: {},
|
||||
funcItemAdded: {},
|
||||
funcItemTypes: {},
|
||||
funcArgsDone: {},
|
||||
funcItemDone: {},
|
||||
// Cached at first computation (see toolCallOutputIndexBase) so every
|
||||
@@ -475,38 +465,32 @@ export function createResponsesApiTransformStream(
|
||||
return state.funcOutputIndex[tcIdx];
|
||||
};
|
||||
|
||||
const emitToolCallAdded = (controller, idx) => {
|
||||
if (state.funcItemAdded[idx] || !state.funcCallIds[idx]) return false;
|
||||
|
||||
// Declared-name match accepts the qualified (`functions__exec`), dotted
|
||||
// (`functions.exec`) and unique-bare (`exec`) spellings: providers that drop
|
||||
// the namespace prefix must not turn a declared freeform tool into a plain
|
||||
// function_call. No request-side schema map is threaded to this emitter, so
|
||||
// the explicit-flat-declaration guard is the identity resolver's job here.
|
||||
const customTool = matchesCustomToolDeclaration({
|
||||
customToolNames,
|
||||
toolName: state.funcNames[idx] || "",
|
||||
});
|
||||
const itemType = customTool ? "custom_tool_call" : "function_call";
|
||||
state.funcItemTypes[idx] = itemType;
|
||||
const emitToolCallAdded = (controller, idx, terminal = false) => {
|
||||
if (state.funcItemAdded[idx] || !state.funcCallIds[idx]) return;
|
||||
const selection = lifecycle.select(idx, state.funcNames[idx] || "", terminal);
|
||||
if (!selection) return;
|
||||
state.funcItemAdded[idx] = true;
|
||||
const name = state.funcNames[idx] || "";
|
||||
const identity = resolveRequestToolIdentity(requestToolIdentityMap, name);
|
||||
|
||||
emit(controller, "response.output_item.added", {
|
||||
type: "response.output_item.added",
|
||||
output_index: state.funcOutputIndex[idx],
|
||||
item: {
|
||||
id: `fc_${state.funcCallIds[idx]}`,
|
||||
type: itemType,
|
||||
...(customTool ? { input: "" } : { arguments: "" }),
|
||||
type: selection.custom ? "custom_tool_call" : "function_call",
|
||||
...(selection.custom ? { input: "" } : { arguments: "" }),
|
||||
call_id: state.funcCallIds[idx],
|
||||
name: identity?.name ?? name,
|
||||
...(identity ? { namespace: identity.namespace } : {}),
|
||||
name: selection.name,
|
||||
...(selection.namespace ? { namespace: selection.namespace } : {}),
|
||||
status: "in_progress",
|
||||
},
|
||||
});
|
||||
return true;
|
||||
if (state.funcArgsBuf[idx] && !selection.custom) {
|
||||
emit(controller, "response.function_call_arguments.delta", {
|
||||
type: "response.function_call_arguments.delta",
|
||||
item_id: `fc_${state.funcCallIds[idx]}`,
|
||||
output_index: state.funcOutputIndex[idx],
|
||||
delta: state.funcArgsBuf[idx],
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const closeToolCall = (controller, idx, recordAsCompleted = true) => {
|
||||
@@ -514,9 +498,9 @@ export function createResponsesApiTransformStream(
|
||||
if (callId && !state.funcItemDone[idx]) {
|
||||
const normalizedIndex = state.funcOutputIndex[idx];
|
||||
let args = state.funcArgsBuf[idx] || "{}";
|
||||
const toolName = state.funcNames[idx] || "";
|
||||
emitToolCallAdded(controller, idx);
|
||||
const isCustomTool = state.funcItemTypes[idx] === "custom_tool_call";
|
||||
emitToolCallAdded(controller, idx, true);
|
||||
const selection = lifecycle.get(idx)!;
|
||||
const isCustomTool = selection.custom;
|
||||
|
||||
// Fix #1674 & #1852: Final cleanup of empty string and empty array placeholders.
|
||||
// Custom-tool input is intentionally allowed to be an empty string.
|
||||
@@ -568,7 +552,8 @@ export function createResponsesApiTransformStream(
|
||||
type: "custom_tool_call",
|
||||
input: rawInput,
|
||||
call_id: callId,
|
||||
name: toolName,
|
||||
name: selection.name,
|
||||
...(selection.namespace ? { namespace: selection.namespace } : {}),
|
||||
status: "completed",
|
||||
};
|
||||
} else {
|
||||
@@ -583,20 +568,13 @@ export function createResponsesApiTransformStream(
|
||||
type: "function_call",
|
||||
arguments: args,
|
||||
call_id: callId,
|
||||
name: toolName,
|
||||
name: selection.name,
|
||||
...(selection.namespace ? { namespace: selection.namespace } : {}),
|
||||
status: "completed",
|
||||
};
|
||||
}
|
||||
|
||||
// #14154 — restore the request-declared {namespace, name} identity (matching
|
||||
// the streaming translator / non-streaming client translator, #7936) and, when
|
||||
// the restored identity is a Codex collaboration call, stamp the
|
||||
// encrypted_function_args:[] plaintext-delivery marker Codex requires.
|
||||
const identity = resolveRequestToolIdentity(requestToolIdentityMap, toolName);
|
||||
if (identity) {
|
||||
funcItem.namespace = identity.namespace;
|
||||
funcItem.name = identity.name;
|
||||
}
|
||||
// Preserve the published identity when adding the collaboration plaintext marker.
|
||||
Object.assign(funcItem, plaintextCollaborationFields(funcItem.namespace, funcItem.name));
|
||||
|
||||
emit(controller, "response.output_item.done", {
|
||||
@@ -916,7 +894,7 @@ export function createResponsesApiTransformStream(
|
||||
delete state.funcNames[tcIdx];
|
||||
delete state.funcArgsBuf[tcIdx];
|
||||
delete state.funcItemAdded[tcIdx];
|
||||
delete state.funcItemTypes[tcIdx];
|
||||
lifecycle.reset(tcIdx);
|
||||
delete state.funcArgsDone[tcIdx];
|
||||
delete state.funcItemDone[tcIdx];
|
||||
// Deliberately keep funcOutputIndex[tcIdx]: the replacement call
|
||||
@@ -925,36 +903,15 @@ export function createResponsesApiTransformStream(
|
||||
// msgItemAdded state shifted mid-turn).
|
||||
}
|
||||
|
||||
// Accumulate name deltas (a split `functions__` + `exec` must not be
|
||||
// truncated to its last fragment) — mirrors the translator path.
|
||||
if (funcName)
|
||||
state.funcNames[tcIdx] = appendToolCallNameDelta(
|
||||
if (funcName) {
|
||||
state.funcNames[tcIdx] = lifecycle.appendName(
|
||||
tcIdx,
|
||||
state.funcNames[tcIdx],
|
||||
funcName
|
||||
);
|
||||
|
||||
if (!state.funcCallIds[tcIdx] && newCallId) {
|
||||
state.funcCallIds[tcIdx] = newCallId;
|
||||
}
|
||||
|
||||
// The provider may send the call id before the function name. Defer the
|
||||
// lifecycle item until the name is available so custom calls are not first
|
||||
// announced as function calls.
|
||||
if (state.funcCallIds[tcIdx] && state.funcNames[tcIdx]) {
|
||||
const itemAdded = emitToolCallAdded(controller, tcIdx);
|
||||
if (
|
||||
itemAdded &&
|
||||
state.funcItemTypes[tcIdx] !== "custom_tool_call" &&
|
||||
state.funcArgsBuf[tcIdx]
|
||||
) {
|
||||
emit(controller, "response.function_call_arguments.delta", {
|
||||
type: "response.function_call_arguments.delta",
|
||||
item_id: `fc_${state.funcCallIds[tcIdx]}`,
|
||||
output_index: outputIndex,
|
||||
delta: state.funcArgsBuf[tcIdx],
|
||||
});
|
||||
}
|
||||
}
|
||||
if (!state.funcCallIds[tcIdx] && newCallId) state.funcCallIds[tcIdx] = newCallId;
|
||||
emitToolCallAdded(controller, tcIdx);
|
||||
|
||||
if (!state.funcArgsBuf[tcIdx]) state.funcArgsBuf[tcIdx] = "";
|
||||
|
||||
@@ -984,7 +941,7 @@ export function createResponsesApiTransformStream(
|
||||
refCallId &&
|
||||
emittedDelta &&
|
||||
state.funcItemAdded[tcIdx] &&
|
||||
state.funcItemTypes[tcIdx] !== "custom_tool_call"
|
||||
lifecycle.get(tcIdx)?.custom !== true
|
||||
) {
|
||||
emit(controller, "response.function_call_arguments.delta", {
|
||||
type: "response.function_call_arguments.delta",
|
||||
|
||||
@@ -5,10 +5,7 @@
|
||||
import { register } from "../registry.ts";
|
||||
import { FORMATS } from "../formats.ts";
|
||||
import { appendToolCallArgumentDelta } from "../../utils/toolCallArguments.ts";
|
||||
import {
|
||||
appendToolCallNameDelta,
|
||||
matchesCustomToolDeclaration,
|
||||
} from "../../utils/toolCallName.ts";
|
||||
import { translatorToolCallLifecycle } from "./openai-responses/toolCallLifecycle.ts";
|
||||
import { projectCompletedStreamError } from "../../utils/streamErrorFormat.ts";
|
||||
import { fallbackToolCallId } from "../helpers/toolCallHelper.ts";
|
||||
import { finalizeResponsesTerminalStatus } from "../helpers/responsesTerminalStatus.ts";
|
||||
@@ -37,7 +34,7 @@ import {
|
||||
toolNameSideChannelNames,
|
||||
} from "./openai-responses/declaredToolName.ts";
|
||||
import { logger } from "../../utils/logger.ts";
|
||||
import { applyFunctionCallIdentity } from "./openai-responses/functionCallIdentity.ts";
|
||||
import { plaintextCollaborationFields } from "./openai-responses/collaborationPlaintextMarker.ts";
|
||||
import { resolveLocalToolCallIndex } from "./openai-responses/toolCallLocalIndex.ts";
|
||||
import {
|
||||
synthesizeCompletedToolItem,
|
||||
@@ -641,8 +638,40 @@ function canonicalizeEmittedToolName(state, name) {
|
||||
return result.name;
|
||||
}
|
||||
|
||||
function emitToolCallAdded(state, emit, idx, outputIndex, terminal = false) {
|
||||
if (state.funcItemAdded[idx] || !state.funcCallIds[idx]) return;
|
||||
// Publish the canonical spelling (#16645) without overwriting the raw
|
||||
// accumulated name. Later deltas, including empty ones, still append
|
||||
// against that raw spelling inside the lifecycle.
|
||||
const nameForSelection = canonicalizeEmittedToolName(state, state.funcNames[idx] || "");
|
||||
const selection = translatorToolCallLifecycle(state).select(idx, nameForSelection, terminal);
|
||||
if (!selection) return;
|
||||
const callId = state.funcCallIds[idx];
|
||||
emit("response.output_item.added", {
|
||||
type: "response.output_item.added",
|
||||
output_index: outputIndex,
|
||||
item: buildResponsesToolCallItem({
|
||||
callId,
|
||||
toolName: selection.name,
|
||||
custom: selection.custom,
|
||||
namespace: selection.namespace,
|
||||
}),
|
||||
});
|
||||
state.funcItemAdded[idx] = true;
|
||||
const bufferedArgs = state.funcArgsBuf[idx] || "";
|
||||
if (bufferedArgs && !selection.custom) {
|
||||
emit("response.function_call_arguments.delta", {
|
||||
type: "response.function_call_arguments.delta",
|
||||
item_id: `fc_${callId}`,
|
||||
output_index: outputIndex,
|
||||
delta: bufferedArgs,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function emitToolCall(state, emit, tc) {
|
||||
const tcIdx = tc.index ?? 0;
|
||||
const lifecycle = translatorToolCallLifecycle(state);
|
||||
const outputIndex = toolCallOutputIndexBase(state) + resolveLocalToolCallIndex(state, tcIdx);
|
||||
// Record every allocated tool-call output_index so a post-close text
|
||||
// relocation (nextFreeMessageIndex) can never collide with it.
|
||||
@@ -664,77 +693,17 @@ function emitToolCall(state, emit, tc) {
|
||||
delete state.funcItemAdded[tcIdx];
|
||||
delete state.funcItemDone[tcIdx];
|
||||
delete state.funcArgsEscapeState?.[tcIdx];
|
||||
lifecycle.reset(tcIdx);
|
||||
}
|
||||
|
||||
// Accumulate name deltas: a provider may split the declared wire name
|
||||
// (`functions__` + `exec`) or re-send it. Keeping only one delta turned a
|
||||
// namespaced custom tool into a bare fragment, which then missed both the
|
||||
// custom/freeform classification and the {namespace, name} restore below.
|
||||
// Canonicalize the accumulated spelling so a provider-injected dotted prefix
|
||||
// (`functions.exec` for a declared flat `exec`) still maps back (#16645).
|
||||
// An empty delta is not a name: appendToolCallNameDelta keeps the previous
|
||||
// spelling, and a missing name does not enter this branch.
|
||||
if (funcName) {
|
||||
const accumulated = appendToolCallNameDelta(state.funcNames[tcIdx], funcName);
|
||||
state.funcNames[tcIdx] = canonicalizeEmittedToolName(state, accumulated);
|
||||
}
|
||||
|
||||
// Custom tools are surfaced as custom_tool_call items and stream raw input instead of the
|
||||
// function_call_arguments.* events used for regular function tools. (#1007)
|
||||
//
|
||||
// apply_patch defaults to custom (native Codex CLI convention: the model emits it
|
||||
// without the client ever declaring it as a tool) UNLESS the client's own request
|
||||
// explicitly declared it with a `parameters` JSON schema — i.e. as a plain
|
||||
// `type:"function"` tool (state.toolSchemas, populated from body.tools by
|
||||
// extractToolSchemaMap()). Live incident: a client that registers apply_patch as a
|
||||
// function tool and only implements function_call dispatch never recognized the
|
||||
// custom_tool_call item this produced, so the tool call was silently never executed
|
||||
// and no follow-up request ever carried a result back. PR #7905 already intended this
|
||||
// precedence ("...while preserving explicit function-tool precedence") but its
|
||||
// unconditional `toolName === "apply_patch"` OR never actually implemented the carve-out.
|
||||
const toolName = state.funcNames[tcIdx] || funcName || "";
|
||||
const lowerName = toolName.toLowerCase();
|
||||
const isCustomTool =
|
||||
((lowerName === "apply_patch" || lowerName === "applypatch") &&
|
||||
!state.toolSchemas?.has?.(toolName)) ||
|
||||
matchesCustomToolDeclaration({
|
||||
customToolNames: state.customToolNames,
|
||||
declaredFunctionSchemas: state.toolSchemas,
|
||||
toolName,
|
||||
});
|
||||
|
||||
// Empty or missing name deltas must not wipe a name already accumulated.
|
||||
// appendName keeps the previous spelling when the incoming delta is blank,
|
||||
// and a falsy name does not enter this branch at all.
|
||||
if (funcName)
|
||||
state.funcNames[tcIdx] = lifecycle.appendName(tcIdx, state.funcNames[tcIdx], funcName);
|
||||
if (!state.funcCallIds[tcIdx] && newCallId) state.funcCallIds[tcIdx] = newCallId;
|
||||
const callId = state.funcCallIds[tcIdx];
|
||||
|
||||
if (callId && toolName && !state.funcItemAdded[tcIdx]) {
|
||||
// #7936 — restore the codex-side `{namespace, name}` pair when the bare
|
||||
// leaf on the Chat wire was flattened from a Responses namespace sub-tool.
|
||||
// Codex dispatches from `namespace` independently of `name` (no `__` split).
|
||||
const identity = resolveRequestToolIdentity(state.requestToolIdentityMap, toolName, {
|
||||
explicitNames: state.toolSchemas?.keys?.() ?? null,
|
||||
});
|
||||
emit("response.output_item.added", {
|
||||
type: "response.output_item.added",
|
||||
output_index: outputIndex,
|
||||
item: buildResponsesToolCallItem({
|
||||
callId,
|
||||
toolName: identity ? identity.name : toolName,
|
||||
custom: isCustomTool,
|
||||
namespace: identity ? identity.namespace : null,
|
||||
}),
|
||||
});
|
||||
state.funcItemAdded[tcIdx] = true;
|
||||
|
||||
const bufferedArgs = state.funcArgsBuf[tcIdx] || "";
|
||||
if (bufferedArgs && !isCustomTool) {
|
||||
emit("response.function_call_arguments.delta", {
|
||||
type: "response.function_call_arguments.delta",
|
||||
item_id: `fc_${callId}`,
|
||||
output_index: outputIndex,
|
||||
delta: bufferedArgs,
|
||||
});
|
||||
}
|
||||
}
|
||||
emitToolCallAdded(state, emit, tcIdx, outputIndex);
|
||||
const isCustomTool = lifecycle.get(tcIdx)?.custom === true;
|
||||
|
||||
if (!state.funcArgsBuf[tcIdx]) state.funcArgsBuf[tcIdx] = "";
|
||||
|
||||
@@ -769,18 +738,9 @@ function closeToolCall(state, emit, idx, recordAsCompleted = true) {
|
||||
if (callId && !state.funcItemDone[idx]) {
|
||||
const normalizedIndex = toolCallOutputIndexBase(state) + resolveLocalToolCallIndex(state, idx);
|
||||
const args = state.funcArgsBuf[idx] || "{}";
|
||||
const toolName = state.funcNames[idx] || "";
|
||||
// See emitToolCall()'s isCustomTool comment — must stay in sync (both compute the
|
||||
// same classification independently for their respective add/close call sites).
|
||||
const lowerName = toolName.toLowerCase();
|
||||
const isCustomTool =
|
||||
((lowerName === "apply_patch" || lowerName === "applypatch") &&
|
||||
!state.toolSchemas?.has?.(toolName)) ||
|
||||
matchesCustomToolDeclaration({
|
||||
customToolNames: state.customToolNames,
|
||||
declaredFunctionSchemas: state.toolSchemas,
|
||||
toolName,
|
||||
});
|
||||
emitToolCallAdded(state, emit, idx, normalizedIndex, true);
|
||||
const selection = translatorToolCallLifecycle(state).get(idx)!;
|
||||
const isCustomTool = selection.custom;
|
||||
|
||||
let funcItem;
|
||||
if (isCustomTool) {
|
||||
@@ -813,22 +773,11 @@ function closeToolCall(state, emit, idx, recordAsCompleted = true) {
|
||||
type: "custom_tool_call",
|
||||
input: rawInput,
|
||||
call_id: callId,
|
||||
name: state.funcNames[idx] || "",
|
||||
name: selection.name,
|
||||
...(selection.namespace ? { namespace: selection.namespace } : {}),
|
||||
status: "completed",
|
||||
};
|
||||
|
||||
// #7936 identity closure for custom_tool_call items (apply_patch stays
|
||||
// bare; namespace sub-tools get back their `namespace` + `name`).
|
||||
const customIdentity = resolveRequestToolIdentity(
|
||||
state.requestToolIdentityMap,
|
||||
state.funcNames[idx] || "",
|
||||
{ explicitNames: state.toolSchemas?.keys?.() ?? null }
|
||||
);
|
||||
if (customIdentity) {
|
||||
funcItem.namespace = customIdentity.namespace;
|
||||
funcItem.name = customIdentity.name;
|
||||
}
|
||||
|
||||
emit("response.output_item.done", {
|
||||
type: "response.output_item.done",
|
||||
output_index: normalizedIndex,
|
||||
@@ -847,14 +796,12 @@ function closeToolCall(state, emit, idx, recordAsCompleted = true) {
|
||||
type: "function_call",
|
||||
arguments: args,
|
||||
call_id: callId,
|
||||
name: state.funcNames[idx] || "",
|
||||
name: selection.name,
|
||||
...(selection.namespace ? { namespace: selection.namespace } : {}),
|
||||
status: "completed",
|
||||
};
|
||||
|
||||
// #7936/#14154 identity closure + collaboration plaintext marker.
|
||||
applyFunctionCallIdentity(funcItem, state.requestToolIdentityMap, state.funcNames[idx] || "", {
|
||||
explicitNames: state.toolSchemas?.keys?.() ?? null,
|
||||
});
|
||||
Object.assign(funcItem, plaintextCollaborationFields(funcItem.namespace, funcItem.name));
|
||||
|
||||
emit("response.output_item.done", {
|
||||
type: "response.output_item.done",
|
||||
|
||||
@@ -0,0 +1,145 @@
|
||||
import { appendToolCallNameDelta, resolveDeclaredToolName } from "../../../utils/toolCallName.ts";
|
||||
import { resolveRequestToolIdentity } from "./requestToolIdentity.ts";
|
||||
|
||||
type ToolMetadata = {
|
||||
customToolNames?: Iterable<string> | null;
|
||||
toolSchemas?: ReadonlyMap<string, unknown> | null;
|
||||
requestToolIdentityMap?: unknown;
|
||||
};
|
||||
type SelectedTool = { name: string; namespace?: string; custom: boolean };
|
||||
|
||||
/** Per-response decisions: defer partial/unknown names, then freeze the published
|
||||
* identity and type. The caller buffers arguments and resets a replaced call ID. */
|
||||
export class ToolCallLifecycle {
|
||||
private readonly metadata: ToolMetadata & { customToolNames: Set<string> };
|
||||
private readonly declaredNames: Set<string>;
|
||||
private readonly wireSpellings: Set<string>;
|
||||
private readonly selected = new Map<string, SelectedTool>();
|
||||
private readonly identities = new Map<string, { namespace: string; name: string }>();
|
||||
private readonly aliases = new Map<string, Set<string>>();
|
||||
|
||||
constructor(
|
||||
metadata: ToolMetadata,
|
||||
private readonly nativeApplyPatch = false
|
||||
) {
|
||||
this.metadata = { ...metadata, customToolNames: new Set(metadata.customToolNames || []) };
|
||||
const ledger = this.metadata.requestToolIdentityMap;
|
||||
const identities =
|
||||
ledger instanceof Map
|
||||
? [...ledger.keys()]
|
||||
: ledger && typeof ledger === "object"
|
||||
? Object.keys(ledger)
|
||||
: [];
|
||||
this.declaredNames = new Set([
|
||||
...(this.metadata.customToolNames || []),
|
||||
...(metadata.toolSchemas?.keys() || []),
|
||||
...identities,
|
||||
]);
|
||||
const entries = ledger instanceof Map ? [...ledger] : Object.entries(ledger || {});
|
||||
for (const [wireName, value] of entries) this.addIdentity(wireName, value);
|
||||
this.wireSpellings = new Set([...this.declaredNames, ...this.aliases.keys()]);
|
||||
for (const name of this.declaredNames) {
|
||||
if (this.identities.has(name)) continue;
|
||||
const separator = name.lastIndexOf("__");
|
||||
if (separator > 0) {
|
||||
this.wireSpellings.add(name.slice(separator + 2));
|
||||
this.wireSpellings.add(`${name.slice(0, separator)}.${name.slice(separator + 2)}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private addIdentity(wireName: unknown, value: unknown): void {
|
||||
if (
|
||||
typeof wireName !== "string" ||
|
||||
!wireName ||
|
||||
!value ||
|
||||
typeof value !== "object" ||
|
||||
Array.isArray(value)
|
||||
)
|
||||
return;
|
||||
const { namespace, name } = value as Record<string, unknown>;
|
||||
if (typeof namespace !== "string" || !namespace || typeof name !== "string" || !name) return;
|
||||
this.identities.set(wireName, { namespace, name });
|
||||
for (const alias of [name, `${namespace}.${name}`]) {
|
||||
const names = this.aliases.get(alias) ?? new Set<string>();
|
||||
names.add(wireName);
|
||||
this.aliases.set(alias, names);
|
||||
}
|
||||
}
|
||||
|
||||
private declaredName(name: string): string | null {
|
||||
// Literal declarations (including flat dotted names) win over ledger aliases.
|
||||
if (this.declaredNames.has(name)) return name;
|
||||
const aliases = this.aliases.get(name);
|
||||
if (aliases) return aliases.size === 1 ? [...aliases][0] : null;
|
||||
return resolveDeclaredToolName(this.declaredNames, name, this.metadata.toolSchemas?.keys());
|
||||
}
|
||||
|
||||
appendName(index: string | number, previous: string, incoming: unknown): string {
|
||||
if (this.selected.has(String(index))) return previous;
|
||||
return appendToolCallNameDelta(previous, incoming, this.wireSpellings);
|
||||
}
|
||||
|
||||
reset(index: string | number): void {
|
||||
this.selected.delete(String(index));
|
||||
}
|
||||
|
||||
get(index: string | number): SelectedTool | undefined {
|
||||
return this.selected.get(String(index));
|
||||
}
|
||||
|
||||
select(index: string | number, name: string, terminal = false): SelectedTool | null {
|
||||
const published = this.get(index);
|
||||
if (published) return published;
|
||||
const declared = this.declaredName(name);
|
||||
const nativePatch =
|
||||
this.nativeApplyPatch &&
|
||||
/^(apply_patch|applypatch)$/i.test(name) &&
|
||||
!this.metadata.toolSchemas?.has(name);
|
||||
// Even an exact name may still be a prefix of a longer declaration. Wait for
|
||||
// the terminal boundary rather than guess which declared call was intended.
|
||||
const isPrefix = [...this.wireSpellings].some(
|
||||
(candidate) => candidate !== name && candidate.startsWith(name)
|
||||
);
|
||||
if (!terminal && ((!declared && !nativePatch) || isPrefix)) return null;
|
||||
const selection = this.resolveSelection(name, declared, nativePatch);
|
||||
|
||||
this.selected.set(String(index), selection);
|
||||
return selection;
|
||||
}
|
||||
private resolveSelection(
|
||||
name: string,
|
||||
declared: string | null,
|
||||
nativePatch: boolean
|
||||
): SelectedTool {
|
||||
const custom = nativePatch || Boolean(declared && this.metadata.customToolNames.has(declared));
|
||||
// Read original identities from ledger values: a >64-character wire key is
|
||||
// hash-truncated and cannot be split to recover its namespace or leaf.
|
||||
const literalFlat = this.metadata.toolSchemas?.has(name) && !this.identities.has(name);
|
||||
const identity =
|
||||
!literalFlat && (declared || this.declaredNames.size === 0)
|
||||
? (this.identities.get(declared) ??
|
||||
resolveRequestToolIdentity(this.metadata.requestToolIdentityMap, name, {
|
||||
explicitNames: this.metadata.toolSchemas?.keys(),
|
||||
}))
|
||||
: null;
|
||||
return {
|
||||
name: identity?.name ?? name,
|
||||
...(identity ? { namespace: identity.namespace } : {}),
|
||||
custom,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const lifecycles = new WeakMap<object, ToolCallLifecycle>();
|
||||
|
||||
/** Translator state arrives from initState and acquires request metadata before
|
||||
* its first tool delta. Keep lifecycle bookkeeping private to that response. */
|
||||
export function translatorToolCallLifecycle(state: ToolMetadata): ToolCallLifecycle {
|
||||
let lifecycle = lifecycles.get(state);
|
||||
if (!lifecycle) {
|
||||
lifecycle = new ToolCallLifecycle(state, true);
|
||||
lifecycles.set(state, lifecycle);
|
||||
}
|
||||
return lifecycle;
|
||||
}
|
||||
@@ -53,7 +53,11 @@ function toNameSet(values: Iterable<string> | null | undefined): Set<string> {
|
||||
* fragment (`functions__exec` then `exec`), and a genuine split
|
||||
* (`functions__` + `exec`).
|
||||
*/
|
||||
export function appendToolCallNameDelta(current: unknown, incoming: unknown): string {
|
||||
export function appendToolCallNameDelta(
|
||||
current: unknown,
|
||||
incoming: unknown,
|
||||
declaredNames?: Iterable<string> | null
|
||||
): string {
|
||||
const previous = typeof current === "string" ? current.trim() : "";
|
||||
const delta = typeof incoming === "string" ? incoming.trim() : "";
|
||||
if (!delta) return previous;
|
||||
@@ -61,6 +65,14 @@ export function appendToolCallNameDelta(current: unknown, incoming: unknown): st
|
||||
if (delta === previous) return previous;
|
||||
// Progressive accumulation: a provider re-sending the longer spelling.
|
||||
if (delta.startsWith(previous)) return delta;
|
||||
// Before publication, prefer a continuation that still fits a declaration,
|
||||
// even if the current name also names a shorter declared tool. A suffix resend
|
||||
// and a genuine repeated character are indistinguishable in that case; this
|
||||
// is a deterministic continuation policy, not evidence of provider intent.
|
||||
// Without declarations, preserve the suffix-resend fallback below.
|
||||
const declared = toNameSet(declaredNames);
|
||||
const combined = previous + delta;
|
||||
if ([...declared].some((name) => name.startsWith(combined))) return combined;
|
||||
// A provider re-sending a fragment we already hold.
|
||||
if (previous.endsWith(delta)) return previous;
|
||||
return previous + delta;
|
||||
|
||||
366
tests/unit/responses-tool-call-lifecycle-16106.test.ts
Normal file
366
tests/unit/responses-tool-call-lifecycle-16106.test.ts
Normal file
@@ -0,0 +1,366 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
|
||||
const { openaiToOpenAIResponsesResponse } =
|
||||
await import("../../open-sse/translator/response/openai-responses.ts");
|
||||
const { createResponsesApiTransformStream } =
|
||||
await import("../../open-sse/transformer/responsesTransformer.ts");
|
||||
const { initState } = await import("../../open-sse/translator/index.ts");
|
||||
const { FORMATS } = await import("../../open-sse/translator/formats.ts");
|
||||
type Item = { type: string; name?: string; namespace?: string; input?: string; arguments?: string };
|
||||
type Event = { event: string; data: { item?: Item; response?: { output: Item[] } } };
|
||||
type Context = {
|
||||
customToolNames: Set<string>;
|
||||
toolSchemas: Map<string, unknown>;
|
||||
identityMap: Map<string, { namespace: string; name: string }>;
|
||||
};
|
||||
const schema = { type: "object", properties: { input: { type: "string" } } };
|
||||
const context = (leaf = "exec", flat = false): Context => ({
|
||||
customToolNames: new Set([`functions__${leaf}`]),
|
||||
toolSchemas: new Map([
|
||||
[`functions__${leaf}`, schema],
|
||||
...(flat ? [[leaf, schema] as [string, typeof schema]] : []),
|
||||
]),
|
||||
identityMap: new Map([
|
||||
[`functions__${leaf}`, { namespace: "functions", name: leaf }],
|
||||
...(flat
|
||||
? [[leaf, { namespace: "", name: leaf }] as [string, { namespace: string; name: string }]]
|
||||
: []),
|
||||
]),
|
||||
});
|
||||
const chunks = (names: string[]) =>
|
||||
names.map((name, i) => ({
|
||||
id: "chatcmpl-probe",
|
||||
model: "offline-probe",
|
||||
choices: [
|
||||
{
|
||||
index: 0,
|
||||
delta: {
|
||||
tool_calls: [
|
||||
{
|
||||
index: 0,
|
||||
id: "call_probe",
|
||||
type: "function",
|
||||
function: {
|
||||
name,
|
||||
...(i === names.length - 1 ? { arguments: '{"input":"echo safe"}' } : {}),
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
finish_reason: null,
|
||||
},
|
||||
],
|
||||
}));
|
||||
const finish = {
|
||||
id: "chatcmpl-probe",
|
||||
model: "offline-probe",
|
||||
choices: [{ index: 0, delta: {}, finish_reason: "tool_calls" }],
|
||||
};
|
||||
async function collect(lane: string, names: string[], ctx: Context): Promise<Event[]> {
|
||||
const inputs = [...chunks(names), finish];
|
||||
if (lane === "translator") {
|
||||
const state = initState(FORMATS.OPENAI_RESPONSES);
|
||||
Object.assign(state, {
|
||||
customToolNames: ctx.customToolNames,
|
||||
toolSchemas: ctx.toolSchemas,
|
||||
requestToolIdentityMap: ctx.identityMap,
|
||||
});
|
||||
return [...inputs, null].flatMap(
|
||||
(chunk) => openaiToOpenAIResponsesResponse(chunk, state) || []
|
||||
);
|
||||
}
|
||||
const encoder = new TextEncoder();
|
||||
const sourceStream = new ReadableStream({
|
||||
start(controller) {
|
||||
for (const chunk of inputs)
|
||||
controller.enqueue(encoder.encode(`data: ${JSON.stringify(chunk)}\n\n`));
|
||||
controller.enqueue(encoder.encode("data: [DONE]\n\n"));
|
||||
controller.close();
|
||||
},
|
||||
});
|
||||
const body = await new Response(
|
||||
sourceStream.pipeThrough(
|
||||
createResponsesApiTransformStream(null, 0, {
|
||||
customToolNames: ctx.customToolNames,
|
||||
requestToolIdentityMap: ctx.identityMap,
|
||||
toolSchemas: ctx.toolSchemas,
|
||||
})
|
||||
)
|
||||
).text();
|
||||
return body
|
||||
.split("\n\n")
|
||||
.filter((frame) => frame.startsWith("event:"))
|
||||
.map((frame) => ({
|
||||
event: frame.split("\n")[0].slice(7),
|
||||
data: JSON.parse(
|
||||
frame
|
||||
.split("\n")
|
||||
.find((line) => line.startsWith("data: "))!
|
||||
.slice(6)
|
||||
),
|
||||
}));
|
||||
}
|
||||
async function record(_label: string, lane: string, names: string[], ctx: Context) {
|
||||
const events = await collect(lane, names, ctx);
|
||||
const added = events.find(
|
||||
(e) => e.event === "response.output_item.added" && e.data.item?.type.endsWith("call")
|
||||
)?.data.item;
|
||||
const done = events.find(
|
||||
(e) => e.event === "response.output_item.done" && e.data.item?.type.endsWith("call")
|
||||
)?.data.item;
|
||||
assert.ok(added, "positive setup: tool item added");
|
||||
assert.ok(done, "positive setup: tool item done");
|
||||
return { added, done, events };
|
||||
}
|
||||
for (const lane of ["translator", "transformer"]) {
|
||||
test(`${lane}: complete qualified custom control`, async () => {
|
||||
const { added, done } = await record(`${lane}-complete`, lane, ["functions__exec"], context());
|
||||
for (const item of [added, done])
|
||||
assert.deepEqual(
|
||||
[item.type, item.namespace, item.name],
|
||||
["custom_tool_call", "functions", "exec"]
|
||||
);
|
||||
assert.equal(done.input, "echo safe");
|
||||
});
|
||||
test(`${lane}: split custom identity is correct from added`, async () => {
|
||||
const { added } = await record(`${lane}-split-added`, lane, ["functions__", "exec"], context());
|
||||
assert.deepEqual(
|
||||
[added.type, added.namespace, added.name],
|
||||
["custom_tool_call", "functions", "exec"]
|
||||
);
|
||||
});
|
||||
test(`${lane}: split custom completion retains custom type`, async () => {
|
||||
const { done } = await record(`${lane}-split-done`, lane, ["functions__", "exec"], context());
|
||||
assert.deepEqual(
|
||||
[done.type, done.namespace, done.name, done.input],
|
||||
["custom_tool_call", "functions", "exec", "echo safe"]
|
||||
);
|
||||
});
|
||||
test(`${lane}: explicit flat exec is not namespaced custom exec`, async () => {
|
||||
const { added, done, events } = await record(
|
||||
`${lane}-flat`,
|
||||
lane,
|
||||
["exec"],
|
||||
context("exec", true)
|
||||
);
|
||||
for (const item of [added, done])
|
||||
assert.deepEqual(
|
||||
[item.type, item.namespace, item.name],
|
||||
["function_call", undefined, "exec"]
|
||||
);
|
||||
assert.equal(done.arguments, '{"input":"echo safe"}');
|
||||
assert.equal(
|
||||
events.some((e) => e.event.startsWith("response.custom_tool_call_input")),
|
||||
false
|
||||
);
|
||||
});
|
||||
test(`${lane}: repeated-letter fragment is retained in final name`, async () => {
|
||||
const { done } = await record(
|
||||
`${lane}-letter`,
|
||||
lane,
|
||||
["functions__shel", "l"],
|
||||
context("shell")
|
||||
);
|
||||
assert.deepEqual([done.namespace, done.name], ["functions", "shell"]);
|
||||
});
|
||||
test(`${lane}: whole-name repeat remains idempotent control`, async () => {
|
||||
const { added, done } = await record(
|
||||
`${lane}-repeat`,
|
||||
lane,
|
||||
["functions__shell", "functions__shell"],
|
||||
context("shell")
|
||||
);
|
||||
for (const item of [added, done])
|
||||
assert.deepEqual(
|
||||
[item.type, item.namespace, item.name],
|
||||
["custom_tool_call", "functions", "shell"]
|
||||
);
|
||||
assert.equal(done.input, "echo safe");
|
||||
});
|
||||
}
|
||||
|
||||
for (const lane of ["translator", "transformer"]) {
|
||||
test(`${lane}: real request metadata preserves the explicit flat exec`, async () => {
|
||||
const { openaiResponsesToOpenAIRequest } =
|
||||
await import("../../open-sse/translator/request/openai-responses.ts");
|
||||
const { extractRequestToolIdentityMap } =
|
||||
await import("../../open-sse/handlers/chatCore/requestToolIdentity.ts");
|
||||
const { extractToolSchemaMap } =
|
||||
await import("../../open-sse/translator/response/openai-responses/toolSchemas.ts");
|
||||
const { collectResponsesCustomToolNames } =
|
||||
await import("../../open-sse/translator/request/openai-responses/additionalTools.ts");
|
||||
const request = {
|
||||
input: "run the flat tool",
|
||||
tools: [
|
||||
{ type: "function", name: "exec", parameters: schema },
|
||||
{ type: "custom", name: "functions__exec", format: { type: "text" } },
|
||||
],
|
||||
};
|
||||
const translated = openaiResponsesToOpenAIRequest("offline-probe", request, true, {
|
||||
provider: "kimi",
|
||||
});
|
||||
const customToolNames = collectResponsesCustomToolNames(request.tools, []);
|
||||
assert.deepEqual(
|
||||
[...customToolNames],
|
||||
["functions__exec"],
|
||||
"real custom classification metadata control"
|
||||
);
|
||||
const identityMap = extractRequestToolIdentityMap(translated);
|
||||
const toolSchemas = extractToolSchemaMap(translated);
|
||||
assert.ok(
|
||||
translated.tools.some((tool) => tool.function?.name === "exec"),
|
||||
"real flat declaration survives request translation"
|
||||
);
|
||||
assert.ok(
|
||||
translated.tools.some((tool) => tool.function?.name === "functions__exec"),
|
||||
"distinct custom declaration survives translation"
|
||||
);
|
||||
assert.ok(toolSchemas?.has("exec"));
|
||||
assert.ok(toolSchemas?.has("functions__exec"));
|
||||
const { added, done } = await record(`${lane}-real-request-flat`, lane, ["exec"], {
|
||||
customToolNames,
|
||||
identityMap: identityMap ?? new Map(),
|
||||
toolSchemas: toolSchemas!,
|
||||
});
|
||||
for (const item of [added, done])
|
||||
assert.deepEqual(
|
||||
[item.type, item.namespace, item.name],
|
||||
["function_call", undefined, "exec"]
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
test("request normalization control: a flat exec excludes the namespace member exec", async () => {
|
||||
const { collectResponsesCustomToolNames, collectResponsesTools } =
|
||||
await import("../../open-sse/translator/request/openai-responses/additionalTools.ts");
|
||||
const tools = [
|
||||
{ type: "function", name: "exec", parameters: schema },
|
||||
{ type: "namespace", name: "functions", tools: [{ type: "custom", name: "exec" }] },
|
||||
];
|
||||
assert.equal(collectResponsesCustomToolNames(tools, []).size, 0);
|
||||
assert.deepEqual(collectResponsesTools(tools, []), [tools[0], { ...tools[1], tools: [] }]);
|
||||
});
|
||||
|
||||
const { openaiResponsesToOpenAIRequest } =
|
||||
await import("../../open-sse/translator/request/openai-responses.ts");
|
||||
const { extractRequestToolIdentityMap } =
|
||||
await import("../../open-sse/handlers/chatCore/requestToolIdentity.ts");
|
||||
const { extractToolSchemaMap } =
|
||||
await import("../../open-sse/translator/response/openai-responses/toolSchemas.ts");
|
||||
const { collectResponsesCustomToolNames } =
|
||||
await import("../../open-sse/translator/request/openai-responses/additionalTools.ts");
|
||||
const longNamespace = "mcp__" + "long_server_".repeat(6);
|
||||
function realMetadata(
|
||||
namespace: string,
|
||||
leaf: string,
|
||||
options: { custom?: boolean; flat?: string; ambiguous?: boolean } = {}
|
||||
) {
|
||||
const tools = [
|
||||
{
|
||||
type: "namespace",
|
||||
name: namespace,
|
||||
tools: [{ type: options.custom ? "custom" : "function", name: leaf, parameters: schema }],
|
||||
},
|
||||
...(options.ambiguous
|
||||
? [
|
||||
{
|
||||
type: "namespace",
|
||||
name: namespace + "other",
|
||||
tools: [{ type: "function", name: leaf, parameters: schema }],
|
||||
},
|
||||
]
|
||||
: []),
|
||||
...(options.flat ? [{ type: "function", name: options.flat, parameters: schema }] : []),
|
||||
];
|
||||
const request = { input: "offline ledger test", tools };
|
||||
const translated = openaiResponsesToOpenAIRequest("offline-probe", request, true, {
|
||||
provider: "kimi",
|
||||
});
|
||||
const toolSchemas = extractToolSchemaMap(translated);
|
||||
const identityMap = extractRequestToolIdentityMap(translated) ?? new Map();
|
||||
const customToolNames = collectResponsesCustomToolNames(tools, []);
|
||||
assert.ok(toolSchemas);
|
||||
const wireName = [...identityMap.entries()].find(
|
||||
([, identity]) => identity.namespace === namespace && identity.name === leaf
|
||||
)?.[0];
|
||||
if (!options.flat || options.flat !== leaf) {
|
||||
assert.ok(wireName, "real conversion produced namespace ledger");
|
||||
assert.equal(wireName.length, 64, "real normalization crosses the 64-character boundary");
|
||||
assert.notEqual(wireName, namespace + "__" + leaf);
|
||||
assert.match(wireName, /_[a-f0-9]{7}$/);
|
||||
assert.ok(toolSchemas.has(wireName));
|
||||
}
|
||||
return { ctx: { customToolNames, toolSchemas, identityMap }, wireName };
|
||||
}
|
||||
for (const lane of ["translator", "transformer"]) {
|
||||
for (const [size, namespace, leaf] of [
|
||||
["long namespace", longNamespace, "shell"],
|
||||
["long leaf", "mcp__short", "tool_".repeat(16)],
|
||||
] as const) {
|
||||
for (const spelling of ["wire", "bare", "dotted"] as const) {
|
||||
test(`${lane}: ledger hash ${size} ${spelling}`, async () => {
|
||||
const { ctx, wireName } = realMetadata(namespace, leaf);
|
||||
const incoming =
|
||||
spelling === "wire" ? wireName : spelling === "bare" ? leaf : namespace + "." + leaf;
|
||||
const { added, done } = await record("ledger", lane, [incoming], ctx);
|
||||
for (const item of [added, done])
|
||||
assert.deepEqual(
|
||||
[item.type, item.namespace, item.name],
|
||||
["function_call", namespace, leaf]
|
||||
);
|
||||
assert.equal(done.arguments, '{"input":"echo safe"}');
|
||||
});
|
||||
}
|
||||
}
|
||||
for (const spelling of ["bare", "dotted"] as const) {
|
||||
test(`${lane}: ledger hash custom ${spelling}`, async () => {
|
||||
const { ctx } = realMetadata(longNamespace, "shell", { custom: true });
|
||||
const incoming = spelling === "bare" ? "shell" : longNamespace + ".shell";
|
||||
const { added, done } = await record("custom", lane, [incoming], ctx);
|
||||
for (const item of [added, done])
|
||||
assert.deepEqual(
|
||||
[item.type, item.namespace, item.name],
|
||||
["custom_tool_call", longNamespace, "shell"]
|
||||
);
|
||||
assert.equal(done.input, "echo safe");
|
||||
});
|
||||
}
|
||||
for (const flat of ["shell", longNamespace + ".shell"]) {
|
||||
test(`${lane}: ledger hash explicit flat ${flat === "shell" ? "bare" : "dotted"}`, async () => {
|
||||
const { ctx } = realMetadata(longNamespace, "shell", { flat });
|
||||
assert.ok(ctx.toolSchemas.has(flat));
|
||||
const { added, done } = await record("flat", lane, [flat], ctx);
|
||||
for (const item of [added, done])
|
||||
assert.deepEqual(
|
||||
[item.type, item.namespace, item.name],
|
||||
["function_call", undefined, flat]
|
||||
);
|
||||
});
|
||||
}
|
||||
test(`${lane}: ledger hash ambiguous bare stays flat`, async () => {
|
||||
const { ctx } = realMetadata(longNamespace, "shell", { ambiguous: true });
|
||||
assert.equal(ctx.identityMap.size, 2);
|
||||
const { added, done } = await record("ambiguous", lane, ["shell"], ctx);
|
||||
for (const item of [added, done])
|
||||
assert.deepEqual(
|
||||
[item.type, item.namespace, item.name],
|
||||
["function_call", undefined, "shell"]
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
for (const lane of ["translator", "transformer"]) {
|
||||
for (const spelling of ["bare", "dotted"] as const) {
|
||||
test(`${lane}: ledger hash fragments ${spelling}`, async () => {
|
||||
const { ctx } = realMetadata(longNamespace, "shell");
|
||||
const prefix = spelling === "bare" ? "shel" : longNamespace + ".shel";
|
||||
const { added, done } = await record("fragment", lane, [prefix, "l"], ctx);
|
||||
for (const item of [added, done])
|
||||
assert.deepEqual(
|
||||
[item.type, item.namespace, item.name],
|
||||
["function_call", longNamespace, "shell"]
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
403
tests/unit/responses-tool-call-lifecycle-edges-16106.test.ts
Normal file
403
tests/unit/responses-tool-call-lifecycle-edges-16106.test.ts
Normal file
@@ -0,0 +1,403 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { openaiToOpenAIResponsesResponse } from "../../open-sse/translator/response/openai-responses.ts";
|
||||
import { createResponsesApiTransformStream } from "../../open-sse/transformer/responsesTransformer.ts";
|
||||
import { initState } from "../../open-sse/translator/index.ts";
|
||||
import { FORMATS } from "../../open-sse/translator/formats.ts";
|
||||
import { appendToolCallNameDelta } from "../../open-sse/utils/toolCallName.ts";
|
||||
|
||||
type Event = {
|
||||
event: string;
|
||||
data: {
|
||||
item?: Record<string, unknown>;
|
||||
output_index?: number;
|
||||
item_id?: string;
|
||||
delta?: string;
|
||||
response?: { output: Record<string, unknown>[] };
|
||||
};
|
||||
};
|
||||
const schema = { type: "object", properties: { input: { type: "string" } } };
|
||||
const customToolNames = new Set(["functions__shell"]);
|
||||
const toolSchemas = new Map([
|
||||
["functions__shell", schema],
|
||||
["inspect", schema],
|
||||
]);
|
||||
const requestToolIdentityMap = new Map([
|
||||
["functions__shell", { namespace: "functions", name: "shell" }],
|
||||
]);
|
||||
const metadata = { customToolNames, toolSchemas, requestToolIdentityMap };
|
||||
const call = (name: string, args = "", id = "call_a", index = 0) => ({
|
||||
index,
|
||||
id,
|
||||
type: "function",
|
||||
function: { name, arguments: args },
|
||||
});
|
||||
const chunk = (calls: ReturnType<typeof call>[], finish_reason: string | null = null) => ({
|
||||
id: "chatcmpl-lifecycle",
|
||||
choices: [{ index: 0, delta: { tool_calls: calls }, finish_reason }],
|
||||
});
|
||||
const toolItems = (events: Event[], suffix: string) =>
|
||||
events
|
||||
.filter((e) => e.event === `response.output_item.${suffix}`)
|
||||
.map((e) => e.data.item!)
|
||||
.filter((item) => String(item.type).endsWith("call"));
|
||||
const identity = (item: Record<string, unknown>) => [item.type, item.name, item.namespace];
|
||||
|
||||
async function driver(
|
||||
lane: string,
|
||||
context: NonNullable<Parameters<typeof createResponsesApiTransformStream>[2]> = metadata
|
||||
) {
|
||||
const events: Event[] = [];
|
||||
if (lane === "translator") {
|
||||
const state = initState(FORMATS.OPENAI_RESPONSES);
|
||||
Object.assign(state, context);
|
||||
return {
|
||||
events,
|
||||
write: async (value: ReturnType<typeof chunk>) => {
|
||||
events.push(...(openaiToOpenAIResponsesResponse(value, state) || []));
|
||||
},
|
||||
end: async () => {
|
||||
events.push(...(openaiToOpenAIResponsesResponse(null, state) || []));
|
||||
},
|
||||
abort: async () => {},
|
||||
};
|
||||
}
|
||||
const stream = createResponsesApiTransformStream(null, 0, context);
|
||||
const writer = stream.writable.getWriter();
|
||||
const reader = stream.readable.getReader();
|
||||
const decoder = new TextDecoder();
|
||||
const reading = (async () => {
|
||||
try {
|
||||
for (;;) {
|
||||
const { value, done } = await reader.read();
|
||||
if (done) break;
|
||||
for (const frame of decoder.decode(value).split("\n\n")) {
|
||||
if (!frame.startsWith("event:")) continue;
|
||||
const [event, data] = frame.split("\n");
|
||||
events.push({ event: event.slice(7), data: JSON.parse(data.slice(6)) });
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
if (!(error instanceof Error) || error.message !== "fixture cancellation") throw error;
|
||||
}
|
||||
})();
|
||||
return {
|
||||
events,
|
||||
write: async (value: ReturnType<typeof chunk>) => {
|
||||
await writer.write(new TextEncoder().encode(`data: ${JSON.stringify(value)}\n\n`));
|
||||
},
|
||||
end: async () => {
|
||||
await writer.close();
|
||||
await reading;
|
||||
},
|
||||
abort: async () => {
|
||||
await writer.abort(new Error("fixture cancellation"));
|
||||
await reading;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
for (const lane of ["translator", "transformer"]) {
|
||||
for (const [label, declared, names, expected] of [
|
||||
["single long declaration", ["shell"], ["functions__shel", "l"], "shell"],
|
||||
[
|
||||
"both declarations, genuine final letter",
|
||||
["shel", "shell"],
|
||||
["functions__shel", "l"],
|
||||
"shell",
|
||||
],
|
||||
["short name without another fragment", ["shel", "shell"], ["functions__shel"], "shel"],
|
||||
[
|
||||
"short name repeated in full",
|
||||
["shel", "shell"],
|
||||
["functions__shel", "functions__shel"],
|
||||
"shel",
|
||||
],
|
||||
["long name suffix resend", ["shel", "shell"], ["functions__shell", "l"], "shell"],
|
||||
] as const) {
|
||||
test(`${lane}: prefix policy: ${label}`, async () => {
|
||||
const d = await driver(lane, {
|
||||
customToolNames: new Set(declared.map((name) => `functions__${name}`)),
|
||||
toolSchemas: new Map(declared.map((name) => [`functions__${name}`, schema])),
|
||||
requestToolIdentityMap: new Map(
|
||||
declared.map((name) => [`functions__${name}`, { namespace: "functions", name }])
|
||||
),
|
||||
});
|
||||
for (const name of names) await d.write(chunk([call(name)]));
|
||||
await d.write(chunk([], "tool_calls"));
|
||||
await d.end();
|
||||
const done = toolItems(d.events, "done");
|
||||
assert.equal(done.length, 1);
|
||||
assert.deepEqual(identity(done[0]), ["custom_tool_call", expected, "functions"]);
|
||||
});
|
||||
}
|
||||
|
||||
test(`${lane}: undeclared tool after reasoning keeps its final output index`, async () => {
|
||||
const d = await driver(lane);
|
||||
const reasoning = chunk([]);
|
||||
Object.assign(reasoning.choices[0].delta, { reasoning_content: "Think before lookup." });
|
||||
await d.write(reasoning);
|
||||
await d.write(chunk([call("lookup", '{"query":"status"}')]));
|
||||
const pending = [...d.events];
|
||||
await d.write(chunk([], "tool_calls"));
|
||||
await d.end();
|
||||
assert.equal(toolItems(pending, "added").length, 0);
|
||||
const reasoningItem = d.events.find(
|
||||
(e) => e.event === "response.output_item.added" && e.data.item?.type === "reasoning"
|
||||
);
|
||||
const added = d.events.find(
|
||||
(e) => e.event === "response.output_item.added" && e.data.item?.type === "function_call"
|
||||
);
|
||||
const done = d.events.find(
|
||||
(e) => e.event === "response.output_item.done" && e.data.item?.type === "function_call"
|
||||
);
|
||||
assert.equal(reasoningItem?.data.output_index, 0);
|
||||
assert.equal(added?.data.output_index, 1);
|
||||
assert.equal(done?.data.output_index, 1);
|
||||
assert.deepEqual(identity(done!.data.item!), ["function_call", "lookup", undefined]);
|
||||
assert.equal(done!.data.item!.arguments, '{"query":"status"}');
|
||||
});
|
||||
|
||||
test(`${lane}: consumes iterable custom declarations once for classification`, async () => {
|
||||
const d = await driver(lane, {
|
||||
...metadata,
|
||||
customToolNames: new Set(["functions__shell"]).values(),
|
||||
});
|
||||
await d.write(chunk([call("functions__shell", '{"input":"safe"}')]));
|
||||
await d.write(chunk([], "tool_calls"));
|
||||
await d.end();
|
||||
assert.deepEqual(identity(toolItems(d.events, "added")[0]), [
|
||||
"custom_tool_call",
|
||||
"shell",
|
||||
"functions",
|
||||
]);
|
||||
assert.deepEqual(identity(toolItems(d.events, "done")[0]), [
|
||||
"custom_tool_call",
|
||||
"shell",
|
||||
"functions",
|
||||
]);
|
||||
});
|
||||
|
||||
test(`${lane}: withholds a partial identity and buffers arguments until ready`, async () => {
|
||||
const d = await driver(lane);
|
||||
await d.write(chunk([call("functions__shel", '{"input":"')]));
|
||||
const pending = [...d.events];
|
||||
await d.write(chunk([call("l", 'safe"}')]));
|
||||
await d.end();
|
||||
assert.equal(toolItems(pending, "added").length, 0);
|
||||
assert.equal(
|
||||
pending.some((e) => e.event.includes("arguments") || e.event.includes("tool_call_input")),
|
||||
false
|
||||
);
|
||||
const added = toolItems(d.events, "added");
|
||||
const done = toolItems(d.events, "done");
|
||||
assert.equal(added.length, 1);
|
||||
assert.deepEqual(identity(added[0]), ["custom_tool_call", "shell", "functions"]);
|
||||
assert.deepEqual(identity(done[0]), identity(added[0]));
|
||||
assert.equal(done[0].input, "safe");
|
||||
});
|
||||
|
||||
for (const alias of ["shel", "functions.shel"]) {
|
||||
test(`${lane}: a repeated-letter fragment completes declared alias ${alias}`, async () => {
|
||||
const d = await driver(lane);
|
||||
await d.write(chunk([call(alias)]));
|
||||
await d.write(chunk([call("l", '{"input":"safe"}')]));
|
||||
await d.write(chunk([], "tool_calls"));
|
||||
await d.end();
|
||||
assert.deepEqual(identity(toolItems(d.events, "done")[0]), [
|
||||
"custom_tool_call",
|
||||
"shell",
|
||||
"functions",
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
test(`${lane}: replays buffered function arguments after added without loss`, async () => {
|
||||
const d = await driver(lane);
|
||||
await d.write(chunk([call("ins", '{"input":')]));
|
||||
await d.write(chunk([call("pect", '"safe"}')]));
|
||||
await d.end();
|
||||
const addedIndex = d.events.findIndex((e) => e.event === "response.output_item.added");
|
||||
const deltas = d.events.filter((e) => e.event === "response.function_call_arguments.delta");
|
||||
assert.ok(deltas.length > 0);
|
||||
assert.ok(d.events.findIndex((e) => e === deltas[0]) > addedIndex);
|
||||
assert.equal(deltas.map((e) => e.data.delta).join(""), '{"input":"safe"}');
|
||||
assert.equal(toolItems(d.events, "done")[0].arguments, '{"input":"safe"}');
|
||||
assert.equal(toolItems(d.events, "added")[0].name, "inspect");
|
||||
});
|
||||
|
||||
test(`${lane}: published identity and type stay immutable on later name noise`, async () => {
|
||||
const d = await driver(lane);
|
||||
await d.write(chunk([call("functions__shell", '{"input":"safe"}')]));
|
||||
const first = toolItems(d.events, "added");
|
||||
await d.write(chunk([call("_unrelated")]));
|
||||
await d.end();
|
||||
assert.equal(first.length, 1, "known declaration can be announced before EOF");
|
||||
assert.deepEqual(identity(toolItems(d.events, "done")[0]), identity(first[0]));
|
||||
});
|
||||
|
||||
test(`${lane}: unknown names stay pending and close flat at EOF without changing terminal policy`, async () => {
|
||||
const d = await driver(lane);
|
||||
await d.write(chunk([call("other__", '{"x":1}')]));
|
||||
await d.write(chunk([call("tool")]));
|
||||
const pending = [...d.events];
|
||||
await d.end();
|
||||
assert.equal(toolItems(pending, "added").length, 0);
|
||||
assert.deepEqual(identity(toolItems(d.events, "added")[0]), [
|
||||
"function_call",
|
||||
"other__tool",
|
||||
undefined,
|
||||
]);
|
||||
assert.deepEqual(
|
||||
identity(toolItems(d.events, "done")[0]),
|
||||
identity(toolItems(d.events, "added")[0])
|
||||
);
|
||||
assert.equal(
|
||||
d.events.filter(
|
||||
(e) => e.event === (lane === "translator" ? "response.failed" : "response.completed")
|
||||
).length,
|
||||
1
|
||||
);
|
||||
});
|
||||
|
||||
test(`${lane}: ambiguous bare names never borrow a namespace`, async () => {
|
||||
const context = {
|
||||
customToolNames: new Set(["a__exec", "b__exec"]),
|
||||
toolSchemas: new Map([
|
||||
["a__exec", schema],
|
||||
["b__exec", schema],
|
||||
]),
|
||||
requestToolIdentityMap: new Map([
|
||||
["a__exec", { namespace: "a", name: "exec" }],
|
||||
["b__exec", { namespace: "b", name: "exec" }],
|
||||
]),
|
||||
};
|
||||
const d = await driver(lane, context);
|
||||
await d.write(chunk([call("exec")]));
|
||||
const pending = [...d.events];
|
||||
await d.write(chunk([], "tool_calls"));
|
||||
await d.end();
|
||||
assert.equal(toolItems(pending, "added").length, 0);
|
||||
for (const item of [...toolItems(d.events, "added"), ...toolItems(d.events, "done")])
|
||||
assert.deepEqual(identity(item), ["function_call", "exec", undefined]);
|
||||
});
|
||||
|
||||
test(`${lane}: replacement call ID resets pending name, type and arguments`, async () => {
|
||||
const d = await driver(lane);
|
||||
await d.write(chunk([call("unknown__", '{"old":1}')]));
|
||||
await d.write(chunk([call("functions__shel", '{"input":"', "call_b")]));
|
||||
await d.write(chunk([call("l", 'new"}', "call_b")]));
|
||||
await d.write(chunk([], "tool_calls"));
|
||||
await d.end();
|
||||
const added = toolItems(d.events, "added");
|
||||
const done = toolItems(d.events, "done");
|
||||
assert.deepEqual(
|
||||
added.map((item) => item.call_id),
|
||||
["call_a", "call_b"]
|
||||
);
|
||||
assert.deepEqual(
|
||||
done.map((item) => item.call_id),
|
||||
["call_a", "call_b"]
|
||||
);
|
||||
assert.equal(done[0].arguments, '{"old":1}');
|
||||
assert.deepEqual(identity(done[1]), ["custom_tool_call", "shell", "functions"]);
|
||||
assert.equal(done[1].input, "new");
|
||||
const output = d.events.find((e) => e.event === "response.completed")!.data.response!.output;
|
||||
assert.deepEqual(
|
||||
output.map((item) => item.call_id),
|
||||
["call_b"]
|
||||
);
|
||||
});
|
||||
|
||||
test(`${lane}: simultaneous calls and no-argument finish retain distinct identities`, async () => {
|
||||
const d = await driver(lane);
|
||||
await d.write(chunk([call("functions__", "", "call_a", 0), call("inspect", "", "call_b", 1)]));
|
||||
await d.write(chunk([call("shell", "", "call_a", 0)]));
|
||||
await d.write(chunk([], "tool_calls"));
|
||||
await d.end();
|
||||
const done = toolItems(d.events, "done");
|
||||
assert.equal(done.length, 2);
|
||||
assert.deepEqual(identity(done.find((item) => item.call_id === "call_a")!), [
|
||||
"custom_tool_call",
|
||||
"shell",
|
||||
"functions",
|
||||
]);
|
||||
assert.deepEqual(identity(done.find((item) => item.call_id === "call_b")!), [
|
||||
"function_call",
|
||||
"inspect",
|
||||
undefined,
|
||||
]);
|
||||
assert.equal(done.find((item) => item.call_id === "call_b")!.arguments, "{}");
|
||||
});
|
||||
}
|
||||
|
||||
test("transformer: cancelling an unresolved call does not invent terminal success", async () => {
|
||||
const d = await driver("transformer");
|
||||
await d.write(chunk([call("functions__", '{"input":"')]));
|
||||
await d.abort();
|
||||
assert.equal(toolItems(d.events, "added").length, 0);
|
||||
assert.equal(toolItems(d.events, "done").length, 0);
|
||||
assert.equal(
|
||||
d.events.some((e) => e.event === "response.completed"),
|
||||
false
|
||||
);
|
||||
});
|
||||
|
||||
test("without declarations, suffix resend remains the deterministic accumulation policy", () => {
|
||||
assert.equal(appendToolCallNameDelta("functions__shel", "l"), "functions__shel");
|
||||
assert.equal(appendToolCallNameDelta("functions__shell", "shell"), "functions__shell");
|
||||
});
|
||||
|
||||
test("real Responses handler threads explicit flat declarations through dispatch", async (t) => {
|
||||
const { handleResponsesCore } = await import("../../open-sse/handlers/responsesHandler.ts");
|
||||
const sent: Record<string, unknown>[] = [];
|
||||
t.mock.method(globalThis, "fetch", async (_url: unknown, init: RequestInit) => {
|
||||
sent.push(JSON.parse(String(init.body)));
|
||||
const pieces = [chunk([call("exec", '{"input":"safe"}')]), chunk([], "tool_calls")];
|
||||
return new Response(
|
||||
pieces.map((value) => `data: ${JSON.stringify(value)}\n\n`).join("") + "data: [DONE]\n\n",
|
||||
{ headers: { "Content-Type": "text/event-stream" } }
|
||||
);
|
||||
});
|
||||
const result = await handleResponsesCore({
|
||||
body: {
|
||||
input: "call the flat function",
|
||||
model: "gpt-4o-mini",
|
||||
tools: [
|
||||
{ type: "function", name: "exec", parameters: schema },
|
||||
{ type: "custom", name: "functions__exec", format: { type: "text" } },
|
||||
],
|
||||
},
|
||||
modelInfo: { provider: "openai", model: "gpt-4o-mini" },
|
||||
credentials: { apiKey: "synthetic-only", providerSpecificData: {} },
|
||||
log: { debug() {}, info() {}, warn() {}, error() {} },
|
||||
onCredentialsRefreshed: null,
|
||||
onRequestSuccess: null,
|
||||
onDisconnect: null,
|
||||
connectionId: null,
|
||||
signal: undefined,
|
||||
});
|
||||
assert.ok(!(result instanceof Response));
|
||||
assert.equal(result.success, true);
|
||||
const text = await result.response!.text();
|
||||
assert.equal(sent.length, 1);
|
||||
const tools = sent[0].tools as Array<{ function: { name: string } }>;
|
||||
assert.deepEqual(
|
||||
tools.map((tool) => tool.function.name),
|
||||
["exec", "functions__exec"]
|
||||
);
|
||||
const events: Event[] = text
|
||||
.split("\n\n")
|
||||
.filter((frame) => frame.startsWith("event:"))
|
||||
.map((frame) => ({
|
||||
event: frame.split("\n")[0].slice(7),
|
||||
data: JSON.parse(frame.split("\n")[1].slice(6)),
|
||||
}));
|
||||
const added = toolItems(events, "added");
|
||||
const done = toolItems(events, "done");
|
||||
assert.equal(added.length, 1);
|
||||
assert.equal(done.length, 1);
|
||||
for (const item of [...added, ...done])
|
||||
assert.deepEqual(identity(item), ["function_call", "exec", undefined]);
|
||||
assert.equal(done[0].arguments, '{"input":"safe"}');
|
||||
});
|
||||
@@ -40,7 +40,8 @@ const chunk = (input, finish = null) => ({
|
||||
choices: [{ index: 0, delta: input, finish_reason: finish }],
|
||||
});
|
||||
|
||||
const NAME_ONLY = (name) => chunk({ tool_calls: [{ index: 0, id: "call_1", type: "function", function: { name } }] });
|
||||
const NAME_ONLY = (name) =>
|
||||
chunk({ tool_calls: [{ index: 0, id: "call_1", type: "function", function: { name } }] });
|
||||
const ARGS_ONLY = (name, args) =>
|
||||
chunk({
|
||||
tool_calls: [{ index: 0, id: "call_1", type: "function", function: { name, arguments: args } }],
|
||||
@@ -63,11 +64,14 @@ const doneItems = (events) =>
|
||||
events.filter((e) => e.event === "response.output_item.done").map((e) => e.data.item);
|
||||
|
||||
test("OpenAI -> Responses: a name split across deltas is accumulated before classification", () => {
|
||||
const events = collectEvents([NAME_ONLY("functions__"), ARGS_ONLY("exec", EXEC_ARGS), FINISH, null], {
|
||||
customToolNames: functionsExecCustom,
|
||||
toolSchemas: new Map([["functions__exec", functionsExecSchema]]),
|
||||
identityMap: functionsExecIdentity,
|
||||
});
|
||||
const events = collectEvents(
|
||||
[NAME_ONLY("functions__"), ARGS_ONLY("exec", EXEC_ARGS), FINISH, null],
|
||||
{
|
||||
customToolNames: functionsExecCustom,
|
||||
toolSchemas: new Map([["functions__exec", functionsExecSchema]]),
|
||||
identityMap: functionsExecIdentity,
|
||||
}
|
||||
);
|
||||
|
||||
const items = doneItems(events);
|
||||
const custom = items.find((item) => item.type === "custom_tool_call");
|
||||
@@ -111,7 +115,10 @@ test("OpenAI -> Responses: an ambiguous bare leaf stays a plain function_call",
|
||||
assert.ok(call, "expected a function_call item");
|
||||
assert.equal(call.name, "exec");
|
||||
assert.equal(call.namespace, undefined);
|
||||
assert.equal(items.some((item) => item.type === "custom_tool_call"), false);
|
||||
assert.equal(
|
||||
items.some((item) => item.type === "custom_tool_call"),
|
||||
false
|
||||
);
|
||||
});
|
||||
|
||||
test("OpenAI -> Responses: an explicit flat declaration of the bare name keeps its identity", () => {
|
||||
@@ -131,5 +138,8 @@ test("OpenAI -> Responses: an explicit flat declaration of the bare name keeps i
|
||||
assert.ok(call, "expected a function_call item for the flat declaration");
|
||||
assert.equal(call.name, "exec");
|
||||
assert.equal(call.namespace, undefined);
|
||||
assert.equal(items.some((item) => item.type === "custom_tool_call"), false);
|
||||
assert.equal(
|
||||
items.some((item) => item.type === "custom_tool_call"),
|
||||
false
|
||||
);
|
||||
});
|
||||
|
||||
@@ -412,11 +412,17 @@ test("Responses->Chat: string tool_choice passes through when tools present, str
|
||||
tools: [{ type: "function", name: "f", parameters: {} }],
|
||||
tool_choice: "auto",
|
||||
};
|
||||
const resWith = openaiResponsesToOpenAIRequest(null, withTools, null, null) as Record<string, unknown>;
|
||||
const resWith = openaiResponsesToOpenAIRequest(null, withTools, null, null) as Record<
|
||||
string,
|
||||
unknown
|
||||
>;
|
||||
assert.equal(resWith.tool_choice, "auto");
|
||||
|
||||
const noTools = { model: "gpt-4", input: "hello", tool_choice: "auto" };
|
||||
const resWithout = openaiResponsesToOpenAIRequest(null, noTools, null, null) as Record<string, unknown>;
|
||||
const resWithout = openaiResponsesToOpenAIRequest(null, noTools, null, null) as Record<
|
||||
string,
|
||||
unknown
|
||||
>;
|
||||
assert.equal(resWithout.tool_choice, undefined);
|
||||
});
|
||||
|
||||
@@ -828,6 +834,10 @@ test("Responses→Chat streaming: flush finalizes stop when no tool call was emi
|
||||
|
||||
test("Chat→Responses streaming: reasoning and a following tool call use distinct output indexes", () => {
|
||||
const state = initState(FORMATS.OPENAI_RESPONSES);
|
||||
// A declared tool is ready before the terminal marker; unknown names stay buffered.
|
||||
state.toolSchemas = new Map([
|
||||
["lookup", { type: "object", properties: { query: { type: "string" } } }],
|
||||
]);
|
||||
|
||||
const reasoningEvents = openaiToOpenAIResponsesResponse(
|
||||
{
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
|
||||
const {
|
||||
appendToolCallNameDelta,
|
||||
resolveDeclaredToolName,
|
||||
matchesCustomToolDeclaration,
|
||||
} = await import("../../open-sse/utils/toolCallName.ts");
|
||||
const { appendToolCallNameDelta, resolveDeclaredToolName, matchesCustomToolDeclaration } =
|
||||
await import("../../open-sse/utils/toolCallName.ts");
|
||||
|
||||
// ── Streamed name accumulation ──────────────────────────────────────────────
|
||||
// A provider may split `function.name` across deltas, re-send the whole name, or
|
||||
|
||||
Reference in New Issue
Block a user