{"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../../src/harness/hooks.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,cAAc,EAAE,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAChG,OAAO,EAAE,KAAK,OAAO,EAAmB,MAAM,cAAc,CAAC;AAC7D,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,4BAA4B,CAAC;AAEvD,OAAO,KAAK,EAAE,yBAAyB,EAAE,8BAA8B,EAAE,MAAM,YAAY,CAAC;AAO5F,KAAK,iBAAiB,GAAG,CAAC,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;AAEhH,0DAA0D;AAC1D,qBAAa,YAAa,YAAW,KAAK;IACzC,OAAO,CAAC,QAAQ,CAAC,aAAa,CAA2C;IACzE,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAoB;IAChD,OAAO,CAAC,WAAW,CAAoB;IAEvC,YAAY,WAAW,EAAE,iBAAiB,EAEzC;IAED,EAAE,CAAC,KAAK,SAAS,QAAQ,EAAE,IAAI,EAAE,KAAK,EAAE,OAAO,EAAE,WAAW,CAAC,KAAK,CAAC,EAAE,OAAO,GAAE;QAAE,EAAE,CAAC,EAAE,MAAM,CAAA;KAAO,GAAG,MAAM,IAAI,CAa9G;IAED,GAAG,CAAC,IAAI,EAAE,QAAQ,GAAG,OAAO,CAE3B;IAED,2FAA2F;IAC3F,WAAW,CAAC,KAAK,SAAS,QAAQ,EACjC,IAAI,EAAE,KAAK,EACX,KAAK,EAAE,cAAc,CAAC,KAAK,CAAC,EAC5B,IAAI,EAAE,IAAI,EACV,OAAO,EAAE,OAAO,GACd,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,QAAQ,CAAC,CAAC,CAMnC;IAED,mFAAmF;IACnF,eAAe,CAAC,KAAK,SAAS,aAAa,GAAG,YAAY,EACzD,IAAI,EAAE,KAAK,EACX,KAAK,EAAE,cAAc,CAAC,KAAK,CAAC,EAC5B,IAAI,EAAE,IAAI,EACV,OAAO,EAAE,OAAO,GACd,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,QAAQ,CAAC,CAAC,CAUnC;IAED,KAAK,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI,CAExB;YAEa,WAAW;YAUX,SAAS;YAuCT,SAAS;YA4BT,UAAU;YAgCV,gBAAgB;YA8BhB,aAAa;YA4Bb,aAAa;YAwBb,aAAa;YAwBb,SAAS;YA2CT,eAAe;IAiC7B,OAAO,CAAC,sBAAsB;IAmC9B,OAAO,CAAC,gBAAgB;YAIV,mBAAmB;YAgBnB,SAAS;CAmBvB;AAED,wBAAgB,uBAAuB,CACtC,IAAI,EAAE,yBAAyB,EAC/B,KAAK,EAAE,8BAA8B,GACnC,yBAAyB,CAsC3B","sourcesContent":["import type { HookHandler, HookInvocation, HookMap, HookName, Hooks } from \"./agent-harness.ts\";\nimport { type Context, withAbortSignal } from \"./context.ts\";\nimport type { Gate } from \"./execution/effect-gate.ts\";\nimport { startHarnessSpan } from \"./telemetry.ts\";\nimport type { AgentHarnessStreamOptions, AgentHarnessStreamOptionsPatch } from \"./types.ts\";\n\ninterface HookRegistration {\n\tid?: string;\n\thandler: (event: unknown, context: Context) => unknown | Promise<unknown>;\n}\n\ntype HookErrorReporter = (error: Error, hook: HookName, lane: string, context: Context) => void | Promise<void>;\n\n/** Ordered harness hook registry and aggregate runner. */\nexport class HookRegistry implements Hooks {\n\tprivate readonly registrations = new Map<HookName, HookRegistration[]>();\n\tprivate readonly reportError: HookErrorReporter;\n\tprivate closedError: Error | undefined;\n\n\tconstructor(reportError: HookErrorReporter) {\n\t\tthis.reportError = reportError;\n\t}\n\n\ton<TName extends HookName>(name: TName, handler: HookHandler<TName>, options: { id?: string } = {}): () => void {\n\t\tif (this.closedError !== undefined) throw this.closedError;\n\t\tconst registrations = this.registrations.get(name) ?? [];\n\t\tconst registration: HookRegistration = {\n\t\t\t...(options.id === undefined ? {} : { id: options.id }),\n\t\t\thandler: (event, context) => handler(event as HookInvocation<TName>, context),\n\t\t};\n\t\tregistrations.push(registration);\n\t\tthis.registrations.set(name, registrations);\n\t\treturn () => {\n\t\t\tconst index = registrations.indexOf(registration);\n\t\t\tif (index !== -1) registrations.splice(index, 1);\n\t\t};\n\t}\n\n\thas(name: HookName): boolean {\n\t\treturn (this.registrations.get(name)?.length ?? 0) !== 0;\n\t}\n\n\t/** Invoke one accepted-operation aggregate after synchronously passing its effect gate. */\n\trunWithGate<TName extends HookName>(\n\t\tname: TName,\n\t\tevent: HookInvocation<TName>,\n\t\tgate: Gate,\n\t\tcontext: Context,\n\t): Promise<HookMap[TName][\"result\"]> {\n\t\treturn gate.admit(() => {\n\t\t\tconst admittedContext = withAbortSignal(gate.signal, context);\n\t\t\tadmittedContext.abortSignal?.throwIfAborted();\n\t\t\treturn this.runAdmitted(name, event, admittedContext);\n\t\t});\n\t}\n\n\t/** Invoke a tool-hook aggregate with one telemetry span per registered handler. */\n\trunToolWithGate<TName extends \"before_tool\" | \"after_tool\">(\n\t\tname: TName,\n\t\tevent: HookInvocation<TName>,\n\t\tgate: Gate,\n\t\tcontext: Context,\n\t): Promise<HookMap[TName][\"result\"]> {\n\t\treturn gate.admit(() => {\n\t\t\tconst admittedContext = withAbortSignal(gate.signal, context);\n\t\t\tadmittedContext.abortSignal?.throwIfAborted();\n\t\t\treturn (\n\t\t\t\tname === \"before_tool\"\n\t\t\t\t\t? this.beforeTool(event as HookInvocation<\"before_tool\">, admittedContext)\n\t\t\t\t\t: this.afterTool(event as HookInvocation<\"after_tool\">, admittedContext)\n\t\t\t) as Promise<HookMap[TName][\"result\"]>;\n\t\t});\n\t}\n\n\tclose(error: Error): void {\n\t\tthis.closedError ??= error;\n\t}\n\n\tprivate async runAdmitted<TName extends HookName>(\n\t\tname: TName,\n\t\tevent: HookInvocation<TName>,\n\t\tcontext: Context,\n\t): Promise<HookMap[TName][\"result\"]> {\n\t\tif (this.closedError !== undefined) throw this.closedError;\n\t\tconst result = await this.aggregate(name, event, context);\n\t\treturn result as HookMap[TName][\"result\"];\n\t}\n\n\tprivate async aggregate(name: HookName, event: HookInvocation<HookName>, context: Context): Promise<unknown> {\n\t\tswitch (name) {\n\t\t\tcase \"before_run\":\n\t\t\t\treturn this.beforeRun(event as HookInvocation<\"before_run\">, context);\n\t\t\tcase \"before_drive\":\n\t\t\t\tawait this.invokeAllFailClosed(name, event as HookInvocation<\"before_drive\">, context);\n\t\t\t\treturn undefined;\n\t\t\tcase \"before_run_end\": {\n\t\t\t\tlet followUp: string | undefined;\n\t\t\t\tawait this.invokeAll(\n\t\t\t\t\tname,\n\t\t\t\t\tevent,\n\t\t\t\t\t(value) => {\n\t\t\t\t\t\tconst result = value as HookMap[\"before_run_end\"][\"result\"];\n\t\t\t\t\t\tif (result?.followUp !== undefined) followUp = result.followUp;\n\t\t\t\t\t},\n\t\t\t\t\tcontext,\n\t\t\t\t);\n\t\t\t\treturn followUp === undefined ? undefined : { followUp };\n\t\t\t}\n\t\t\tcase \"transform_context\":\n\t\t\t\treturn this.transformContext(event as HookInvocation<\"transform_context\">, context);\n\t\t\tcase \"before_request\":\n\t\t\t\treturn this.beforeRequest(event as HookInvocation<\"before_request\">, context);\n\t\t\tcase \"before_payload\":\n\t\t\t\treturn this.beforePayload(event as HookInvocation<\"before_payload\">, context);\n\t\t\tcase \"after_response\":\n\t\t\t\treturn this.afterResponse(event as HookInvocation<\"after_response\">, context);\n\t\t\tcase \"before_tool\":\n\t\t\t\treturn this.beforeTool(event as HookInvocation<\"before_tool\">, context);\n\t\t\tcase \"after_tool\":\n\t\t\t\treturn this.afterTool(event as HookInvocation<\"after_tool\">, context);\n\t\t\tcase \"before_compaction\":\n\t\t\t\treturn this.firstStructural(name, event as HookInvocation<\"before_compaction\">, \"compaction\", context);\n\t\t\tcase \"before_navigation\":\n\t\t\t\treturn this.firstStructural(name, event as HookInvocation<\"before_navigation\">, \"summary\", context);\n\t\t}\n\t}\n\n\tprivate async beforeRun(\n\t\tevent: HookInvocation<\"before_run\">,\n\t\tcontext: Context,\n\t): Promise<HookMap[\"before_run\"][\"result\"]> {\n\t\tlet prompt = event.prompt;\n\t\tlet injected: HookMap[\"before_run\"][\"event\"][\"prompt\"] = [];\n\t\tfor (const registration of this.registrationsFor(\"before_run\")) {\n\t\t\ttry {\n\t\t\t\tconst result = (await registration.handler(\n\t\t\t\t\t{ ...event, prompt },\n\t\t\t\t\tcontext,\n\t\t\t\t)) as HookMap[\"before_run\"][\"result\"];\n\t\t\t\tif (result?.messages !== undefined) {\n\t\t\t\t\tinjected = [...injected, ...result.messages];\n\t\t\t\t\tprompt = [...prompt, ...result.messages];\n\t\t\t\t}\n\t\t\t} catch (error) {\n\t\t\t\tawait this.reportError(\n\t\t\t\t\terror instanceof Error ? error : new Error(String(error)),\n\t\t\t\t\t\"before_run\",\n\t\t\t\t\tevent.lane,\n\t\t\t\t\tcontext,\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t\treturn injected.length === 0 ? undefined : { messages: injected };\n\t}\n\n\tprivate async beforeTool(\n\t\tevent: HookInvocation<\"before_tool\">,\n\t\tcontext: Context,\n\t): Promise<HookMap[\"before_tool\"][\"result\"]> {\n\t\tlet args = event.args;\n\t\tlet block: { reason: string; terminate?: boolean } | undefined;\n\t\tfor (const registration of this.registrationsFor(\"before_tool\")) {\n\t\t\ttry {\n\t\t\t\tconst result = (await this.invokeToolRegistration(\n\t\t\t\t\t\"before_tool\",\n\t\t\t\t\tregistration,\n\t\t\t\t\t{ ...event, args },\n\t\t\t\t\tcontext,\n\t\t\t\t)) as HookMap[\"before_tool\"][\"result\"];\n\t\t\t\tif (result?.args !== undefined) args = result.args;\n\t\t\t\tif (result?.block !== undefined) {\n\t\t\t\t\tblock = result.block;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t} catch (error) {\n\t\t\t\tconst normalized = error instanceof Error ? error : new Error(String(error));\n\t\t\t\tawait this.reportError(normalized, \"before_tool\", event.lane, context);\n\t\t\t\tblock = { reason: normalized.message };\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\treturn {\n\t\t\t...(args === event.args ? {} : { args }),\n\t\t\t...(block === undefined ? {} : { block }),\n\t\t};\n\t}\n\n\tprivate async transformContext(\n\t\tevent: HookInvocation<\"transform_context\">,\n\t\tcontext: Context,\n\t): Promise<HookMap[\"transform_context\"][\"result\"]> {\n\t\tlet messages = event.messages;\n\t\tlet systemPrompt = event.systemPrompt;\n\t\tfor (const registration of this.registrationsFor(\"transform_context\")) {\n\t\t\ttry {\n\t\t\t\tconst result = (await registration.handler(\n\t\t\t\t\t{\n\t\t\t\t\t\t...event,\n\t\t\t\t\t\tmessages,\n\t\t\t\t\t\tsystemPrompt,\n\t\t\t\t\t},\n\t\t\t\t\tcontext,\n\t\t\t\t)) as HookMap[\"transform_context\"][\"result\"];\n\t\t\t\tif (result?.messages !== undefined) messages = result.messages;\n\t\t\t\tif (result?.systemPrompt !== undefined) systemPrompt = result.systemPrompt;\n\t\t\t} catch (error) {\n\t\t\t\tawait this.reportError(\n\t\t\t\t\terror instanceof Error ? error : new Error(String(error)),\n\t\t\t\t\t\"transform_context\",\n\t\t\t\t\tevent.lane,\n\t\t\t\t\tcontext,\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t\treturn { messages, systemPrompt };\n\t}\n\n\tprivate async beforeRequest(\n\t\tevent: HookInvocation<\"before_request\">,\n\t\tcontext: Context,\n\t): Promise<HookMap[\"before_request\"][\"result\"]> {\n\t\tlet streamOptions = event.streamOptions;\n\t\tlet changed = false;\n\t\tfor (const registration of this.registrationsFor(\"before_request\")) {\n\t\t\ttry {\n\t\t\t\tconst result = (await registration.handler(\n\t\t\t\t\t{ ...event, streamOptions },\n\t\t\t\t\tcontext,\n\t\t\t\t)) as HookMap[\"before_request\"][\"result\"];\n\t\t\t\tif (result?.streamOptions !== undefined) {\n\t\t\t\t\tstreamOptions = applyStreamOptionsPatch(streamOptions, result.streamOptions);\n\t\t\t\t\tchanged = true;\n\t\t\t\t}\n\t\t\t} catch (error) {\n\t\t\t\tawait this.reportError(\n\t\t\t\t\terror instanceof Error ? error : new Error(String(error)),\n\t\t\t\t\t\"before_request\",\n\t\t\t\t\tevent.lane,\n\t\t\t\t\tcontext,\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t\treturn changed ? { streamOptions: createStreamOptionsPatch(event.streamOptions, streamOptions) } : undefined;\n\t}\n\n\tprivate async beforePayload(\n\t\tevent: HookInvocation<\"before_payload\">,\n\t\tcontext: Context,\n\t): Promise<HookMap[\"before_payload\"][\"result\"]> {\n\t\tlet payload = event.payload;\n\t\tfor (const registration of this.registrationsFor(\"before_payload\")) {\n\t\t\ttry {\n\t\t\t\tconst result = (await registration.handler(\n\t\t\t\t\t{ ...event, payload },\n\t\t\t\t\tcontext,\n\t\t\t\t)) as HookMap[\"before_payload\"][\"result\"];\n\t\t\t\tif (result?.payload !== undefined) payload = result.payload;\n\t\t\t} catch (error) {\n\t\t\t\tawait this.reportError(\n\t\t\t\t\terror instanceof Error ? error : new Error(String(error)),\n\t\t\t\t\t\"before_payload\",\n\t\t\t\t\tevent.lane,\n\t\t\t\t\tcontext,\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t\treturn { payload };\n\t}\n\n\tprivate async afterResponse(\n\t\tevent: HookInvocation<\"after_response\">,\n\t\tcontext: Context,\n\t): Promise<HookMap[\"after_response\"][\"result\"]> {\n\t\tlet message = event.message;\n\t\tfor (const registration of this.registrationsFor(\"after_response\")) {\n\t\t\ttry {\n\t\t\t\tconst result = (await registration.handler(\n\t\t\t\t\t{ ...event, message },\n\t\t\t\t\tcontext,\n\t\t\t\t)) as HookMap[\"after_response\"][\"result\"];\n\t\t\t\tif (result?.message !== undefined) message = result.message;\n\t\t\t} catch (error) {\n\t\t\t\tawait this.reportError(\n\t\t\t\t\terror instanceof Error ? error : new Error(String(error)),\n\t\t\t\t\t\"after_response\",\n\t\t\t\t\tevent.lane,\n\t\t\t\t\tcontext,\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t\treturn { message };\n\t}\n\n\tprivate async afterTool(\n\t\tevent: HookInvocation<\"after_tool\">,\n\t\tcontext: Context,\n\t): Promise<HookMap[\"after_tool\"][\"result\"]> {\n\t\tlet current = {\n\t\t\tcontent: event.content,\n\t\t\tdetails: event.details,\n\t\t\tisError: event.isError,\n\t\t\tusage: event.usage,\n\t\t};\n\t\tconst aggregate: NonNullable<HookMap[\"after_tool\"][\"result\"]> = {};\n\t\tfor (const registration of this.registrationsFor(\"after_tool\")) {\n\t\t\ttry {\n\t\t\t\tconst result = (await this.invokeToolRegistration(\n\t\t\t\t\t\"after_tool\",\n\t\t\t\t\tregistration,\n\t\t\t\t\t{ ...event, ...current },\n\t\t\t\t\tcontext,\n\t\t\t\t)) as HookMap[\"after_tool\"][\"result\"];\n\t\t\t\tif (result === undefined) continue;\n\t\t\t\tif (result.content !== undefined) aggregate.content = result.content;\n\t\t\t\tif (result.details !== undefined) aggregate.details = result.details;\n\t\t\t\tif (result.isError !== undefined) aggregate.isError = result.isError;\n\t\t\t\tif (result.usage !== undefined) aggregate.usage = result.usage;\n\t\t\t\tif (result.terminate !== undefined) aggregate.terminate = result.terminate;\n\t\t\t\tcurrent = {\n\t\t\t\t\tcontent: result.content === undefined ? current.content : result.content,\n\t\t\t\t\tdetails: result.details === undefined ? current.details : result.details,\n\t\t\t\t\tisError: result.isError === undefined ? current.isError : result.isError,\n\t\t\t\t\tusage: result.usage === undefined ? current.usage : result.usage,\n\t\t\t\t};\n\t\t\t} catch (error) {\n\t\t\t\tawait this.reportError(\n\t\t\t\t\terror instanceof Error ? error : new Error(String(error)),\n\t\t\t\t\t\"after_tool\",\n\t\t\t\t\tevent.lane,\n\t\t\t\t\tcontext,\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t\treturn Object.keys(aggregate).length === 0 ? undefined : aggregate;\n\t}\n\n\tprivate async firstStructural(\n\t\tname: \"before_compaction\" | \"before_navigation\",\n\t\tevent: HookInvocation<\"before_compaction\"> | HookInvocation<\"before_navigation\">,\n\t\tresultField: \"compaction\" | \"summary\",\n\t\tcontext: Context,\n\t): Promise<unknown> {\n\t\tfor (const registration of this.registrationsFor(name)) {\n\t\t\ttry {\n\t\t\t\tconst value = await registration.handler(event, context);\n\t\t\t\tif (value === undefined || value === null || typeof value !== \"object\") continue;\n\t\t\t\tconst result = value as Record<string, unknown>;\n\t\t\t\tif (result.decline === true && result[resultField] !== undefined) {\n\t\t\t\t\tawait this.reportError(\n\t\t\t\t\t\tnew Error(`${name} hook cannot return both decline and ${resultField}`),\n\t\t\t\t\t\tname,\n\t\t\t\t\t\tevent.lane,\n\t\t\t\t\t\tcontext,\n\t\t\t\t\t);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tif (result.decline === true || result[resultField] !== undefined) return value;\n\t\t\t} catch (error) {\n\t\t\t\tawait this.reportError(\n\t\t\t\t\terror instanceof Error ? error : new Error(String(error)),\n\t\t\t\t\tname,\n\t\t\t\t\tevent.lane,\n\t\t\t\t\tcontext,\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t\treturn undefined;\n\t}\n\n\tprivate invokeToolRegistration(\n\t\tname: \"before_tool\" | \"after_tool\",\n\t\tregistration: HookRegistration,\n\t\tevent: HookInvocation<\"before_tool\"> | HookInvocation<\"after_tool\">,\n\t\tcontext: Context,\n\t): Promise<unknown> {\n\t\treturn startHarnessSpan(\n\t\t\t\"pi.harness.hook\",\n\t\t\t{\n\t\t\t\t\"pi.lane.name\": event.lane,\n\t\t\t\t\"pi.operation.id\": event.runId,\n\t\t\t\t\"pi.hook.name\": name,\n\t\t\t\t...(registration.id === undefined ? {} : { \"pi.hook.registration_id\": registration.id }),\n\t\t\t},\n\t\t\tasync (span, spanContext) => {\n\t\t\t\ttry {\n\t\t\t\t\tconst result = await registration.handler(event, spanContext);\n\t\t\t\t\tconst blocked =\n\t\t\t\t\t\tname === \"before_tool\" &&\n\t\t\t\t\t\tresult !== null &&\n\t\t\t\t\t\ttypeof result === \"object\" &&\n\t\t\t\t\t\t\"block\" in result &&\n\t\t\t\t\t\tresult.block !== undefined;\n\t\t\t\t\tspan.setAttributes({ \"pi.hook.outcome\": blocked ? \"blocked\" : \"completed\" });\n\t\t\t\t\treturn result;\n\t\t\t\t} catch (error) {\n\t\t\t\t\tspan.setAttributes({ \"pi.hook.outcome\": \"failed\" });\n\t\t\t\t\tspan.setStatus({ status: \"error\" });\n\t\t\t\t\tthrow error;\n\t\t\t\t}\n\t\t\t},\n\t\t\tcontext,\n\t\t);\n\t}\n\n\tprivate registrationsFor(name: HookName): HookRegistration[] {\n\t\treturn [...(this.registrations.get(name) ?? [])];\n\t}\n\n\tprivate async invokeAllFailClosed(\n\t\tname: \"before_drive\",\n\t\tevent: HookInvocation<\"before_drive\">,\n\t\tcontext: Context,\n\t): Promise<void> {\n\t\tfor (const registration of this.registrationsFor(name)) {\n\t\t\ttry {\n\t\t\t\tawait registration.handler(event, context);\n\t\t\t} catch (error) {\n\t\t\t\tconst normalized = error instanceof Error ? error : new Error(String(error));\n\t\t\t\tawait this.reportError(normalized, name, event.lane, context);\n\t\t\t\tthrow normalized;\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate async invokeAll(\n\t\tname: HookName,\n\t\tevent: HookInvocation<HookName>,\n\t\tapply: (value: unknown) => void,\n\t\tcontext: Context,\n\t): Promise<void> {\n\t\tfor (const registration of this.registrationsFor(name)) {\n\t\t\ttry {\n\t\t\t\tapply(await registration.handler(event, context));\n\t\t\t} catch (error) {\n\t\t\t\tawait this.reportError(\n\t\t\t\t\terror instanceof Error ? error : new Error(String(error)),\n\t\t\t\t\tname,\n\t\t\t\t\tevent.lane,\n\t\t\t\t\tcontext,\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t}\n}\n\nexport function applyStreamOptionsPatch(\n\tbase: AgentHarnessStreamOptions,\n\tpatch: AgentHarnessStreamOptionsPatch,\n): AgentHarnessStreamOptions {\n\tconst next: AgentHarnessStreamOptions = { ...base };\n\tfor (const key of [\n\t\t\"transport\",\n\t\t\"timeoutMs\",\n\t\t\"maxRetries\",\n\t\t\"maxRetryDelayMs\",\n\t\t\"cacheRetention\",\n\t\t\"deferred\",\n\t] as const) {\n\t\tif (!(key in patch)) continue;\n\t\tconst value = patch[key];\n\t\tif (value === undefined) delete next[key];\n\t\telse Object.assign(next, { [key]: value });\n\t}\n\tif (\"headers\" in patch) {\n\t\tif (patch.headers === undefined) delete next.headers;\n\t\telse {\n\t\t\tconst headers = { ...next.headers };\n\t\t\tfor (const [key, value] of Object.entries(patch.headers)) {\n\t\t\t\tif (value === undefined) delete headers[key];\n\t\t\t\telse headers[key] = value;\n\t\t\t}\n\t\t\tnext.headers = headers;\n\t\t}\n\t}\n\tif (\"metadata\" in patch) {\n\t\tif (patch.metadata === undefined) delete next.metadata;\n\t\telse {\n\t\t\tconst metadata = { ...next.metadata };\n\t\t\tfor (const [key, value] of Object.entries(patch.metadata)) {\n\t\t\t\tif (value === undefined) delete metadata[key];\n\t\t\t\telse metadata[key] = value;\n\t\t\t}\n\t\t\tnext.metadata = metadata;\n\t\t}\n\t}\n\treturn next;\n}\n\nfunction createStreamOptionsPatch(\n\tbase: AgentHarnessStreamOptions,\n\tvalue: AgentHarnessStreamOptions,\n): AgentHarnessStreamOptionsPatch {\n\tconst patch: AgentHarnessStreamOptionsPatch = {};\n\tfor (const key of [\n\t\t\"transport\",\n\t\t\"timeoutMs\",\n\t\t\"maxRetries\",\n\t\t\"maxRetryDelayMs\",\n\t\t\"cacheRetention\",\n\t\t\"deferred\",\n\t] as const) {\n\t\tif (base[key] !== value[key]) Object.assign(patch, { [key]: value[key] });\n\t}\n\tif (base.headers !== value.headers) {\n\t\tif (value.headers === undefined) patch.headers = undefined;\n\t\telse {\n\t\t\tconst headers: Record<string, string | undefined> = {};\n\t\t\tfor (const key of Object.keys(base.headers ?? {})) {\n\t\t\t\tif (!(key in value.headers)) headers[key] = undefined;\n\t\t\t}\n\t\t\tfor (const [key, header] of Object.entries(value.headers)) {\n\t\t\t\tif (base.headers?.[key] !== header) headers[key] = header;\n\t\t\t}\n\t\t\tif (base.headers === undefined && Object.keys(headers).length === 0) patch.headers = {};\n\t\t\telse if (Object.keys(headers).length !== 0) patch.headers = headers;\n\t\t}\n\t}\n\tif (base.metadata !== value.metadata) {\n\t\tif (value.metadata === undefined) patch.metadata = undefined;\n\t\telse {\n\t\t\tconst metadata: Record<string, unknown | undefined> = {};\n\t\t\tfor (const key of Object.keys(base.metadata ?? {})) {\n\t\t\t\tif (!(key in value.metadata)) metadata[key] = undefined;\n\t\t\t}\n\t\t\tfor (const [key, metadataValue] of Object.entries(value.metadata)) {\n\t\t\t\tif (base.metadata?.[key] !== metadataValue) metadata[key] = metadataValue;\n\t\t\t}\n\t\t\tif (base.metadata === undefined && Object.keys(metadata).length === 0) patch.metadata = {};\n\t\t\telse if (Object.keys(metadata).length !== 0) patch.metadata = metadata;\n\t\t}\n\t}\n\treturn patch;\n}\n"]}