UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

122 lines • 4.53 kB
const SYNTHETIC_OUTPUT = "Tool output unavailable (interrupted before result)"; function isObject(value) { return typeof value === "object" && value !== null; } function hasResponsesInput(value) { return isObject(value) && Array.isArray(value.input); } function getType(item) { return isObject(item) && typeof item.type === "string" ? item.type : undefined; } function getCallId(item) { if (!isObject(item) || typeof item.call_id !== "string" || item.call_id.length === 0) return undefined; return item.call_id; } function isFunctionCallItem(item) { const type = getType(item); return type === "function_call" || type === "local_shell_call"; } function isFunctionCallOutputItem(item) { return getType(item) === "function_call_output"; } function isCustomToolCallItem(item) { return getType(item) === "custom_tool_call"; } function isCustomToolCallOutputItem(item) { return getType(item) === "custom_tool_call_output"; } function getCustomToolName(item) { return typeof item.name === "string" && item.name.length > 0 ? item.name : undefined; } function collectMatchedOutputIds(input) { const seenFunctionCallIds = new Set(); const seenCustomCallIds = new Set(); const functionOutputIds = new Set(); const customOutputIds = new Set(); for (const item of input) { const callId = getCallId(item); if (!callId) continue; if (isFunctionCallItem(item)) { seenFunctionCallIds.add(callId); continue; } if (isCustomToolCallItem(item)) { seenCustomCallIds.add(callId); continue; } if (isFunctionCallOutputItem(item) && seenFunctionCallIds.has(callId) && !functionOutputIds.has(callId)) { functionOutputIds.add(callId); continue; } if (isCustomToolCallOutputItem(item) && seenCustomCallIds.has(callId) && !customOutputIds.has(callId)) { customOutputIds.add(callId); } } return { functionOutputIds, customOutputIds }; } function createSyntheticFunctionOutput(callId) { return { type: "function_call_output", call_id: callId, output: SYNTHETIC_OUTPUT, }; } function createSyntheticCustomToolOutput(callId, callItem) { const name = getCustomToolName(callItem); return { type: "custom_tool_call_output", call_id: callId, ...(name ? { name } : {}), output: SYNTHETIC_OUTPUT, }; } /** Repairs OpenAI Responses request input by keeping tool call/output pairs balanced. */ export function sanitizeOpenAIResponsesPayload(payload) { if (!hasResponsesInput(payload)) return payload; // Output-only deltas are valid when server-side continuation is explicitly referenced. if (typeof payload.previous_response_id === "string" && payload.previous_response_id.length > 0) return payload; const { functionOutputIds, customOutputIds } = collectMatchedOutputIds(payload.input); const sanitizedInput = []; const emittedFunctionOutputIds = new Set(); const emittedCustomOutputIds = new Set(); let changed = false; for (const item of payload.input) { const callId = getCallId(item); if (isFunctionCallOutputItem(item)) { if (!callId || !functionOutputIds.has(callId) || emittedFunctionOutputIds.has(callId)) { changed = true; continue; } emittedFunctionOutputIds.add(callId); sanitizedInput.push(item); continue; } if (isCustomToolCallOutputItem(item)) { if (!callId || !customOutputIds.has(callId) || emittedCustomOutputIds.has(callId)) { changed = true; continue; } emittedCustomOutputIds.add(callId); sanitizedInput.push(item); continue; } sanitizedInput.push(item); if (callId && isFunctionCallItem(item) && !functionOutputIds.has(callId)) { sanitizedInput.push(createSyntheticFunctionOutput(callId)); changed = true; continue; } if (callId && isCustomToolCallItem(item) && !customOutputIds.has(callId)) { sanitizedInput.push(createSyntheticCustomToolOutput(callId, item)); changed = true; } } if (!changed) return payload; return { ...payload, input: sanitizedInput }; } //# sourceMappingURL=sanitize-openai-responses-payload.js.map