|
| 1 | +import { bridgeToResponsesSSE, buildResponseJSON } from "../../src/bridge"; |
| 2 | +import type { AdapterEvent } from "../../src/types"; |
| 3 | + |
| 4 | +/** |
| 5 | + * Shared harness for Responses tool round-trip conformance |
| 6 | + * (devlog/_plan/260813_routed_tool_discovery_profiles/030-034). |
| 7 | + * |
| 8 | + * Every existing tool test re-implements `replay`/`collectSse` locally, which is why the |
| 9 | + * streaming and non-streaming paths had never been compared: each test only looked at one. |
| 10 | + * |
| 11 | + * The streamed side is read from BOTH surfaces on purpose. `response.completed` is what a |
| 12 | + * client that reconnects or ignores deltas sees; `response.output_item.done` is what a client |
| 13 | + * consuming normal incremental frames sees. The bridge builds them separately, so reading only |
| 14 | + * the snapshot hides a whole divergence class — an item can be correct in the final snapshot |
| 15 | + * and wrong in the incremental frame. devlog 034 requires the incremental assertions. |
| 16 | + */ |
| 17 | + |
| 18 | +export async function* replay(events: readonly AdapterEvent[]): AsyncGenerator<AdapterEvent> { |
| 19 | + for (const event of events) yield event; |
| 20 | +} |
| 21 | + |
| 22 | +export interface SseFrame { |
| 23 | + event?: string; |
| 24 | + data: Record<string, unknown>; |
| 25 | +} |
| 26 | + |
| 27 | +export async function collectSse(stream: ReadableStream<Uint8Array>): Promise<SseFrame[]> { |
| 28 | + const reader = stream.getReader(); |
| 29 | + const decoder = new TextDecoder(); |
| 30 | + let text = ""; |
| 31 | + for (;;) { |
| 32 | + const { done, value } = await reader.read(); |
| 33 | + if (done) break; |
| 34 | + text += decoder.decode(value, { stream: true }); |
| 35 | + } |
| 36 | + return text.split("\n\n") |
| 37 | + .map(frame => frame.trim()) |
| 38 | + .filter(frame => frame.length > 0 && frame !== "data: [DONE]") |
| 39 | + .map(frame => { |
| 40 | + const lines = frame.split("\n"); |
| 41 | + const event = lines.find(line => line.startsWith("event: "))?.slice(7); |
| 42 | + const dataLine = lines.find(line => line.startsWith("data: ")); |
| 43 | + return { event, data: JSON.parse(dataLine?.slice(6) ?? "{}") as Record<string, unknown> }; |
| 44 | + }); |
| 45 | +} |
| 46 | + |
| 47 | +/** The tool-bearing fields every transport must agree on, in output order. */ |
| 48 | +export interface NormalizedToolItem { |
| 49 | + type: string; |
| 50 | + name?: string; |
| 51 | + call_id?: string; |
| 52 | + /** `arguments` for function/tool_search, `input` for custom. Objects are preserved. */ |
| 53 | + payload?: unknown; |
| 54 | + status?: string; |
| 55 | + /** Namespace identity, when the restored item carries one. */ |
| 56 | + namespace?: string; |
| 57 | +} |
| 58 | + |
| 59 | +function normalizeItem(item: Record<string, unknown>): NormalizedToolItem { |
| 60 | + const payload = item.arguments !== undefined ? item.arguments : item.input; |
| 61 | + return { |
| 62 | + type: String(item.type ?? ""), |
| 63 | + ...(typeof item.name === "string" ? { name: item.name } : {}), |
| 64 | + ...(typeof item.call_id === "string" ? { call_id: item.call_id } : {}), |
| 65 | + ...(payload !== undefined ? { payload } : {}), |
| 66 | + ...(typeof item.status === "string" ? { status: item.status } : {}), |
| 67 | + ...(typeof item.namespace === "string" ? { namespace: item.namespace } : {}), |
| 68 | + }; |
| 69 | +} |
| 70 | + |
| 71 | +const isToolItem = (item: Record<string, unknown>): boolean => |
| 72 | + String(item.type ?? "").includes("call"); |
| 73 | + |
| 74 | +type BridgeMaps = [ |
| 75 | + toolNsMap?: Map<string, { namespace: string; name: string }>, |
| 76 | + freeformToolNames?: Set<string>, |
| 77 | + toolSearchToolNames?: Set<string>, |
| 78 | +]; |
| 79 | + |
| 80 | +export interface StreamedView { |
| 81 | + /** Tool items from the terminal `response.completed` snapshot. */ |
| 82 | + snapshot: NormalizedToolItem[]; |
| 83 | + /** Tool items from the incremental `response.output_item.done` frames. */ |
| 84 | + incremental: NormalizedToolItem[]; |
| 85 | + /** Every frame's event name, in order. */ |
| 86 | + eventNames: string[]; |
| 87 | + /** Ordered payloads of every argument/input delta frame. */ |
| 88 | + deltas: string[]; |
| 89 | + /** Every non-call output item type from the snapshot, e.g. "message". */ |
| 90 | + snapshotItemTypes: string[]; |
| 91 | +} |
| 92 | + |
| 93 | +export async function streamedView( |
| 94 | + events: readonly AdapterEvent[], |
| 95 | + modelId: string, |
| 96 | + ...maps: BridgeMaps |
| 97 | +): Promise<StreamedView> { |
| 98 | + const frames = await collectSse(bridgeToResponsesSSE(replay(events), modelId, ...maps)); |
| 99 | + const completed = frames.find(frame => frame.event === "response.completed"); |
| 100 | + const response = completed?.data.response as Record<string, unknown> | undefined; |
| 101 | + const output = Array.isArray(response?.output) ? response.output as Record<string, unknown>[] : []; |
| 102 | + |
| 103 | + const doneItems = frames |
| 104 | + .filter(frame => frame.event === "response.output_item.done") |
| 105 | + .map(frame => frame.data.item) |
| 106 | + .filter((item): item is Record<string, unknown> => !!item && typeof item === "object"); |
| 107 | + |
| 108 | + return { |
| 109 | + snapshot: output.filter(isToolItem).map(normalizeItem), |
| 110 | + incremental: doneItems.filter(isToolItem).map(normalizeItem), |
| 111 | + eventNames: frames.map(frame => frame.event ?? ""), |
| 112 | + deltas: frames |
| 113 | + .filter(frame => frame.event?.endsWith(".delta") && typeof frame.data.delta === "string") |
| 114 | + .map(frame => String(frame.data.delta)), |
| 115 | + snapshotItemTypes: output.map(item => String(item.type ?? "")), |
| 116 | + }; |
| 117 | +} |
| 118 | + |
| 119 | +/** Tool items from the non-streaming transport. */ |
| 120 | +export function jsonToolItems( |
| 121 | + events: readonly AdapterEvent[], |
| 122 | + modelId: string, |
| 123 | + options?: Parameters<typeof buildResponseJSON>[2], |
| 124 | +): NormalizedToolItem[] { |
| 125 | + const body = buildResponseJSON([...events], modelId, options); |
| 126 | + const output = Array.isArray(body.output) ? body.output as Record<string, unknown>[] : []; |
| 127 | + return output.filter(isToolItem).map(normalizeItem); |
| 128 | +} |
| 129 | + |
| 130 | +/** Every output item type from the non-streaming transport, including non-call items. */ |
| 131 | +export function jsonItemTypes( |
| 132 | + events: readonly AdapterEvent[], |
| 133 | + modelId: string, |
| 134 | + options?: Parameters<typeof buildResponseJSON>[2], |
| 135 | +): string[] { |
| 136 | + const body = buildResponseJSON([...events], modelId, options); |
| 137 | + const output = Array.isArray(body.output) ? body.output as Record<string, unknown>[] : []; |
| 138 | + return output.map(item => String(item.type ?? "")); |
| 139 | +} |
0 commit comments