@avadisabelle/ava-pi-agent-core 0.61.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"proxy.d.ts","sourceRoot":"","sources":["../src/proxy.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,EACN,KAAK,gBAAgB,EACrB,KAAK,qBAAqB,EAC1B,KAAK,OAAO,EACZ,WAAW,EACX,KAAK,KAAK,EAEV,KAAK,mBAAmB,EACxB,KAAK,UAAU,EAEf,MAAM,yBAAyB,CAAC;AAGjC,cAAM,uBAAwB,SAAQ,WAAW,CAAC,qBAAqB,EAAE,gBAAgB,CAAC;IACzF,cASC;CACD;AAED;;GAEG;AACH,MAAM,MAAM,0BAA0B,GACnC;IAAE,IAAI,EAAE,OAAO,CAAA;CAAE,GACjB;IAAE,IAAI,EAAE,YAAY,CAAC;IAAC,YAAY,EAAE,MAAM,CAAA;CAAE,GAC5C;IAAE,IAAI,EAAE,YAAY,CAAC;IAAC,YAAY,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GAC3D;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,YAAY,EAAE,MAAM,CAAC;IAAC,gBAAgB,CAAC,EAAE,MAAM,CAAA;CAAE,GACrE;IAAE,IAAI,EAAE,gBAAgB,CAAC;IAAC,YAAY,EAAE,MAAM,CAAA;CAAE,GAChD;IAAE,IAAI,EAAE,gBAAgB,CAAC;IAAC,YAAY,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GAC/D;IAAE,IAAI,EAAE,cAAc,CAAC;IAAC,YAAY,EAAE,MAAM,CAAC;IAAC,gBAAgB,CAAC,EAAE,MAAM,CAAA;CAAE,GACzE;IAAE,IAAI,EAAE,gBAAgB,CAAC;IAAC,YAAY,EAAE,MAAM,CAAC;IAAC,EAAE,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,GAC9E;IAAE,IAAI,EAAE,gBAAgB,CAAC;IAAC,YAAY,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GAC/D;IAAE,IAAI,EAAE,cAAc,CAAC;IAAC,YAAY,EAAE,MAAM,CAAA;CAAE,GAC9C;IACA,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,OAAO,CAAC,UAAU,EAAE,MAAM,GAAG,QAAQ,GAAG,SAAS,CAAC,CAAC;IAC3D,KAAK,EAAE,gBAAgB,CAAC,OAAO,CAAC,CAAC;CAChC,GACD;IACA,IAAI,EAAE,OAAO,CAAC;IACd,MAAM,EAAE,OAAO,CAAC,UAAU,EAAE,SAAS,GAAG,OAAO,CAAC,CAAC;IACjD,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,KAAK,EAAE,gBAAgB,CAAC,OAAO,CAAC,CAAC;CAChC,CAAC;AAEL,MAAM,WAAW,kBAAmB,SAAQ,mBAAmB;IAC9D,sCAAsC;IACtC,SAAS,EAAE,MAAM,CAAC;IAClB,2DAA2D;IAC3D,QAAQ,EAAE,MAAM,CAAC;CACjB;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,kBAAkB,GAAG,uBAAuB,CAyHrH","sourcesContent":["/**\n * Proxy stream function for apps that route LLM calls through a server.\n * The server manages auth and proxies requests to LLM providers.\n */\n\n// Internal import for JSON parsing utility\nimport {\n\ttype AssistantMessage,\n\ttype AssistantMessageEvent,\n\ttype Context,\n\tEventStream,\n\ttype Model,\n\tparseStreamingJson,\n\ttype SimpleStreamOptions,\n\ttype StopReason,\n\ttype ToolCall,\n} from \"@avadisabelle/ava-pi-ai\";\n\n// Create stream class matching ProxyMessageEventStream\nclass ProxyMessageEventStream extends EventStream<AssistantMessageEvent, AssistantMessage> {\n\tconstructor() {\n\t\tsuper(\n\t\t\t(event) => event.type === \"done\" || event.type === \"error\",\n\t\t\t(event) => {\n\t\t\t\tif (event.type === \"done\") return event.message;\n\t\t\t\tif (event.type === \"error\") return event.error;\n\t\t\t\tthrow new Error(\"Unexpected event type\");\n\t\t\t},\n\t\t);\n\t}\n}\n\n/**\n * Proxy event types - server sends these with partial field stripped to reduce bandwidth.\n */\nexport type ProxyAssistantMessageEvent =\n\t| { type: \"start\" }\n\t| { type: \"text_start\"; contentIndex: number }\n\t| { type: \"text_delta\"; contentIndex: number; delta: string }\n\t| { type: \"text_end\"; contentIndex: number; contentSignature?: string }\n\t| { type: \"thinking_start\"; contentIndex: number }\n\t| { type: \"thinking_delta\"; contentIndex: number; delta: string }\n\t| { type: \"thinking_end\"; contentIndex: number; contentSignature?: string }\n\t| { type: \"toolcall_start\"; contentIndex: number; id: string; toolName: string }\n\t| { type: \"toolcall_delta\"; contentIndex: number; delta: string }\n\t| { type: \"toolcall_end\"; contentIndex: number }\n\t| {\n\t\t\ttype: \"done\";\n\t\t\treason: Extract<StopReason, \"stop\" | \"length\" | \"toolUse\">;\n\t\t\tusage: AssistantMessage[\"usage\"];\n\t }\n\t| {\n\t\t\ttype: \"error\";\n\t\t\treason: Extract<StopReason, \"aborted\" | \"error\">;\n\t\t\terrorMessage?: string;\n\t\t\tusage: AssistantMessage[\"usage\"];\n\t };\n\nexport interface ProxyStreamOptions extends SimpleStreamOptions {\n\t/** Auth token for the proxy server */\n\tauthToken: string;\n\t/** Proxy server URL (e.g., \"https://genai.example.com\") */\n\tproxyUrl: string;\n}\n\n/**\n * Stream function that proxies through a server instead of calling LLM providers directly.\n * The server strips the partial field from delta events to reduce bandwidth.\n * We reconstruct the partial message client-side.\n *\n * Use this as the `streamFn` option when creating an Agent that needs to go through a proxy.\n *\n * @example\n * ```typescript\n * const agent = new Agent({\n * streamFn: (model, context, options) =>\n * streamProxy(model, context, {\n * ...options,\n * authToken: await getAuthToken(),\n * proxyUrl: \"https://genai.example.com\",\n * }),\n * });\n * ```\n */\nexport function streamProxy(model: Model<any>, context: Context, options: ProxyStreamOptions): ProxyMessageEventStream {\n\tconst stream = new ProxyMessageEventStream();\n\n\t(async () => {\n\t\t// Initialize the partial message that we'll build up from events\n\t\tconst partial: AssistantMessage = {\n\t\t\trole: \"assistant\",\n\t\t\tstopReason: \"stop\",\n\t\t\tcontent: [],\n\t\t\tapi: model.api,\n\t\t\tprovider: model.provider,\n\t\t\tmodel: model.id,\n\t\t\tusage: {\n\t\t\t\tinput: 0,\n\t\t\t\toutput: 0,\n\t\t\t\tcacheRead: 0,\n\t\t\t\tcacheWrite: 0,\n\t\t\t\ttotalTokens: 0,\n\t\t\t\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n\t\t\t},\n\t\t\ttimestamp: Date.now(),\n\t\t};\n\n\t\tlet reader: ReadableStreamDefaultReader<Uint8Array> | undefined;\n\n\t\tconst abortHandler = () => {\n\t\t\tif (reader) {\n\t\t\t\treader.cancel(\"Request aborted by user\").catch(() => {});\n\t\t\t}\n\t\t};\n\n\t\tif (options.signal) {\n\t\t\toptions.signal.addEventListener(\"abort\", abortHandler);\n\t\t}\n\n\t\ttry {\n\t\t\tconst response = await fetch(`${options.proxyUrl}/api/stream`, {\n\t\t\t\tmethod: \"POST\",\n\t\t\t\theaders: {\n\t\t\t\t\tAuthorization: `Bearer ${options.authToken}`,\n\t\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t\t},\n\t\t\t\tbody: JSON.stringify({\n\t\t\t\t\tmodel,\n\t\t\t\t\tcontext,\n\t\t\t\t\toptions: {\n\t\t\t\t\t\ttemperature: options.temperature,\n\t\t\t\t\t\tmaxTokens: options.maxTokens,\n\t\t\t\t\t\treasoning: options.reasoning,\n\t\t\t\t\t},\n\t\t\t\t}),\n\t\t\t\tsignal: options.signal,\n\t\t\t});\n\n\t\t\tif (!response.ok) {\n\t\t\t\tlet errorMessage = `Proxy error: ${response.status} ${response.statusText}`;\n\t\t\t\ttry {\n\t\t\t\t\tconst errorData = (await response.json()) as { error?: string };\n\t\t\t\t\tif (errorData.error) {\n\t\t\t\t\t\terrorMessage = `Proxy error: ${errorData.error}`;\n\t\t\t\t\t}\n\t\t\t\t} catch {\n\t\t\t\t\t// Couldn't parse error response\n\t\t\t\t}\n\t\t\t\tthrow new Error(errorMessage);\n\t\t\t}\n\n\t\t\treader = response.body!.getReader();\n\t\t\tconst decoder = new TextDecoder();\n\t\t\tlet buffer = \"\";\n\n\t\t\twhile (true) {\n\t\t\t\tconst { done, value } = await reader.read();\n\t\t\t\tif (done) break;\n\n\t\t\t\tif (options.signal?.aborted) {\n\t\t\t\t\tthrow new Error(\"Request aborted by user\");\n\t\t\t\t}\n\n\t\t\t\tbuffer += decoder.decode(value, { stream: true });\n\t\t\t\tconst lines = buffer.split(\"\\n\");\n\t\t\t\tbuffer = lines.pop() || \"\";\n\n\t\t\t\tfor (const line of lines) {\n\t\t\t\t\tif (line.startsWith(\"data: \")) {\n\t\t\t\t\t\tconst data = line.slice(6).trim();\n\t\t\t\t\t\tif (data) {\n\t\t\t\t\t\t\tconst proxyEvent = JSON.parse(data) as ProxyAssistantMessageEvent;\n\t\t\t\t\t\t\tconst event = processProxyEvent(proxyEvent, partial);\n\t\t\t\t\t\t\tif (event) {\n\t\t\t\t\t\t\t\tstream.push(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (options.signal?.aborted) {\n\t\t\t\tthrow new Error(\"Request aborted by user\");\n\t\t\t}\n\n\t\t\tstream.end();\n\t\t} catch (error) {\n\t\t\tconst errorMessage = error instanceof Error ? error.message : String(error);\n\t\t\tconst reason = options.signal?.aborted ? \"aborted\" : \"error\";\n\t\t\tpartial.stopReason = reason;\n\t\t\tpartial.errorMessage = errorMessage;\n\t\t\tstream.push({\n\t\t\t\ttype: \"error\",\n\t\t\t\treason,\n\t\t\t\terror: partial,\n\t\t\t});\n\t\t\tstream.end();\n\t\t} finally {\n\t\t\tif (options.signal) {\n\t\t\t\toptions.signal.removeEventListener(\"abort\", abortHandler);\n\t\t\t}\n\t\t}\n\t})();\n\n\treturn stream;\n}\n\n/**\n * Process a proxy event and update the partial message.\n */\nfunction processProxyEvent(\n\tproxyEvent: ProxyAssistantMessageEvent,\n\tpartial: AssistantMessage,\n): AssistantMessageEvent | undefined {\n\tswitch (proxyEvent.type) {\n\t\tcase \"start\":\n\t\t\treturn { type: \"start\", partial };\n\n\t\tcase \"text_start\":\n\t\t\tpartial.content[proxyEvent.contentIndex] = { type: \"text\", text: \"\" };\n\t\t\treturn { type: \"text_start\", contentIndex: proxyEvent.contentIndex, partial };\n\n\t\tcase \"text_delta\": {\n\t\t\tconst content = partial.content[proxyEvent.contentIndex];\n\t\t\tif (content?.type === \"text\") {\n\t\t\t\tcontent.text += proxyEvent.delta;\n\t\t\t\treturn {\n\t\t\t\t\ttype: \"text_delta\",\n\t\t\t\t\tcontentIndex: proxyEvent.contentIndex,\n\t\t\t\t\tdelta: proxyEvent.delta,\n\t\t\t\t\tpartial,\n\t\t\t\t};\n\t\t\t}\n\t\t\tthrow new Error(\"Received text_delta for non-text content\");\n\t\t}\n\n\t\tcase \"text_end\": {\n\t\t\tconst content = partial.content[proxyEvent.contentIndex];\n\t\t\tif (content?.type === \"text\") {\n\t\t\t\tcontent.textSignature = proxyEvent.contentSignature;\n\t\t\t\treturn {\n\t\t\t\t\ttype: \"text_end\",\n\t\t\t\t\tcontentIndex: proxyEvent.contentIndex,\n\t\t\t\t\tcontent: content.text,\n\t\t\t\t\tpartial,\n\t\t\t\t};\n\t\t\t}\n\t\t\tthrow new Error(\"Received text_end for non-text content\");\n\t\t}\n\n\t\tcase \"thinking_start\":\n\t\t\tpartial.content[proxyEvent.contentIndex] = { type: \"thinking\", thinking: \"\" };\n\t\t\treturn { type: \"thinking_start\", contentIndex: proxyEvent.contentIndex, partial };\n\n\t\tcase \"thinking_delta\": {\n\t\t\tconst content = partial.content[proxyEvent.contentIndex];\n\t\t\tif (content?.type === \"thinking\") {\n\t\t\t\tcontent.thinking += proxyEvent.delta;\n\t\t\t\treturn {\n\t\t\t\t\ttype: \"thinking_delta\",\n\t\t\t\t\tcontentIndex: proxyEvent.contentIndex,\n\t\t\t\t\tdelta: proxyEvent.delta,\n\t\t\t\t\tpartial,\n\t\t\t\t};\n\t\t\t}\n\t\t\tthrow new Error(\"Received thinking_delta for non-thinking content\");\n\t\t}\n\n\t\tcase \"thinking_end\": {\n\t\t\tconst content = partial.content[proxyEvent.contentIndex];\n\t\t\tif (content?.type === \"thinking\") {\n\t\t\t\tcontent.thinkingSignature = proxyEvent.contentSignature;\n\t\t\t\treturn {\n\t\t\t\t\ttype: \"thinking_end\",\n\t\t\t\t\tcontentIndex: proxyEvent.contentIndex,\n\t\t\t\t\tcontent: content.thinking,\n\t\t\t\t\tpartial,\n\t\t\t\t};\n\t\t\t}\n\t\t\tthrow new Error(\"Received thinking_end for non-thinking content\");\n\t\t}\n\n\t\tcase \"toolcall_start\":\n\t\t\tpartial.content[proxyEvent.contentIndex] = {\n\t\t\t\ttype: \"toolCall\",\n\t\t\t\tid: proxyEvent.id,\n\t\t\t\tname: proxyEvent.toolName,\n\t\t\t\targuments: {},\n\t\t\t\tpartialJson: \"\",\n\t\t\t} satisfies ToolCall & { partialJson: string } as ToolCall;\n\t\t\treturn { type: \"toolcall_start\", contentIndex: proxyEvent.contentIndex, partial };\n\n\t\tcase \"toolcall_delta\": {\n\t\t\tconst content = partial.content[proxyEvent.contentIndex];\n\t\t\tif (content?.type === \"toolCall\") {\n\t\t\t\t(content as any).partialJson += proxyEvent.delta;\n\t\t\t\tcontent.arguments = parseStreamingJson((content as any).partialJson) || {};\n\t\t\t\tpartial.content[proxyEvent.contentIndex] = { ...content }; // Trigger reactivity\n\t\t\t\treturn {\n\t\t\t\t\ttype: \"toolcall_delta\",\n\t\t\t\t\tcontentIndex: proxyEvent.contentIndex,\n\t\t\t\t\tdelta: proxyEvent.delta,\n\t\t\t\t\tpartial,\n\t\t\t\t};\n\t\t\t}\n\t\t\tthrow new Error(\"Received toolcall_delta for non-toolCall content\");\n\t\t}\n\n\t\tcase \"toolcall_end\": {\n\t\t\tconst content = partial.content[proxyEvent.contentIndex];\n\t\t\tif (content?.type === \"toolCall\") {\n\t\t\t\tdelete (content as any).partialJson;\n\t\t\t\treturn {\n\t\t\t\t\ttype: \"toolcall_end\",\n\t\t\t\t\tcontentIndex: proxyEvent.contentIndex,\n\t\t\t\t\ttoolCall: content,\n\t\t\t\t\tpartial,\n\t\t\t\t};\n\t\t\t}\n\t\t\treturn undefined;\n\t\t}\n\n\t\tcase \"done\":\n\t\t\tpartial.stopReason = proxyEvent.reason;\n\t\t\tpartial.usage = proxyEvent.usage;\n\t\t\treturn { type: \"done\", reason: proxyEvent.reason, message: partial };\n\n\t\tcase \"error\":\n\t\t\tpartial.stopReason = proxyEvent.reason;\n\t\t\tpartial.errorMessage = proxyEvent.errorMessage;\n\t\t\tpartial.usage = proxyEvent.usage;\n\t\t\treturn { type: \"error\", reason: proxyEvent.reason, error: partial };\n\n\t\tdefault: {\n\t\t\tconst _exhaustiveCheck: never = proxyEvent;\n\t\t\tconsole.warn(`Unhandled proxy event type: ${(proxyEvent as any).type}`);\n\t\t\treturn undefined;\n\t\t}\n\t}\n}\n"]}
package/dist/proxy.js ADDED
@@ -0,0 +1,268 @@
1
+ /**
2
+ * Proxy stream function for apps that route LLM calls through a server.
3
+ * The server manages auth and proxies requests to LLM providers.
4
+ */
5
+ // Internal import for JSON parsing utility
6
+ import { EventStream, parseStreamingJson, } from "@avadisabelle/ava-pi-ai";
7
+ // Create stream class matching ProxyMessageEventStream
8
+ class ProxyMessageEventStream extends EventStream {
9
+ constructor() {
10
+ super((event) => event.type === "done" || event.type === "error", (event) => {
11
+ if (event.type === "done")
12
+ return event.message;
13
+ if (event.type === "error")
14
+ return event.error;
15
+ throw new Error("Unexpected event type");
16
+ });
17
+ }
18
+ }
19
+ /**
20
+ * Stream function that proxies through a server instead of calling LLM providers directly.
21
+ * The server strips the partial field from delta events to reduce bandwidth.
22
+ * We reconstruct the partial message client-side.
23
+ *
24
+ * Use this as the `streamFn` option when creating an Agent that needs to go through a proxy.
25
+ *
26
+ * @example
27
+ * ```typescript
28
+ * const agent = new Agent({
29
+ * streamFn: (model, context, options) =>
30
+ * streamProxy(model, context, {
31
+ * ...options,
32
+ * authToken: await getAuthToken(),
33
+ * proxyUrl: "https://genai.example.com",
34
+ * }),
35
+ * });
36
+ * ```
37
+ */
38
+ export function streamProxy(model, context, options) {
39
+ const stream = new ProxyMessageEventStream();
40
+ (async () => {
41
+ // Initialize the partial message that we'll build up from events
42
+ const partial = {
43
+ role: "assistant",
44
+ stopReason: "stop",
45
+ content: [],
46
+ api: model.api,
47
+ provider: model.provider,
48
+ model: model.id,
49
+ usage: {
50
+ input: 0,
51
+ output: 0,
52
+ cacheRead: 0,
53
+ cacheWrite: 0,
54
+ totalTokens: 0,
55
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
56
+ },
57
+ timestamp: Date.now(),
58
+ };
59
+ let reader;
60
+ const abortHandler = () => {
61
+ if (reader) {
62
+ reader.cancel("Request aborted by user").catch(() => { });
63
+ }
64
+ };
65
+ if (options.signal) {
66
+ options.signal.addEventListener("abort", abortHandler);
67
+ }
68
+ try {
69
+ const response = await fetch(`${options.proxyUrl}/api/stream`, {
70
+ method: "POST",
71
+ headers: {
72
+ Authorization: `Bearer ${options.authToken}`,
73
+ "Content-Type": "application/json",
74
+ },
75
+ body: JSON.stringify({
76
+ model,
77
+ context,
78
+ options: {
79
+ temperature: options.temperature,
80
+ maxTokens: options.maxTokens,
81
+ reasoning: options.reasoning,
82
+ },
83
+ }),
84
+ signal: options.signal,
85
+ });
86
+ if (!response.ok) {
87
+ let errorMessage = `Proxy error: ${response.status} ${response.statusText}`;
88
+ try {
89
+ const errorData = (await response.json());
90
+ if (errorData.error) {
91
+ errorMessage = `Proxy error: ${errorData.error}`;
92
+ }
93
+ }
94
+ catch {
95
+ // Couldn't parse error response
96
+ }
97
+ throw new Error(errorMessage);
98
+ }
99
+ reader = response.body.getReader();
100
+ const decoder = new TextDecoder();
101
+ let buffer = "";
102
+ while (true) {
103
+ const { done, value } = await reader.read();
104
+ if (done)
105
+ break;
106
+ if (options.signal?.aborted) {
107
+ throw new Error("Request aborted by user");
108
+ }
109
+ buffer += decoder.decode(value, { stream: true });
110
+ const lines = buffer.split("\n");
111
+ buffer = lines.pop() || "";
112
+ for (const line of lines) {
113
+ if (line.startsWith("data: ")) {
114
+ const data = line.slice(6).trim();
115
+ if (data) {
116
+ const proxyEvent = JSON.parse(data);
117
+ const event = processProxyEvent(proxyEvent, partial);
118
+ if (event) {
119
+ stream.push(event);
120
+ }
121
+ }
122
+ }
123
+ }
124
+ }
125
+ if (options.signal?.aborted) {
126
+ throw new Error("Request aborted by user");
127
+ }
128
+ stream.end();
129
+ }
130
+ catch (error) {
131
+ const errorMessage = error instanceof Error ? error.message : String(error);
132
+ const reason = options.signal?.aborted ? "aborted" : "error";
133
+ partial.stopReason = reason;
134
+ partial.errorMessage = errorMessage;
135
+ stream.push({
136
+ type: "error",
137
+ reason,
138
+ error: partial,
139
+ });
140
+ stream.end();
141
+ }
142
+ finally {
143
+ if (options.signal) {
144
+ options.signal.removeEventListener("abort", abortHandler);
145
+ }
146
+ }
147
+ })();
148
+ return stream;
149
+ }
150
+ /**
151
+ * Process a proxy event and update the partial message.
152
+ */
153
+ function processProxyEvent(proxyEvent, partial) {
154
+ switch (proxyEvent.type) {
155
+ case "start":
156
+ return { type: "start", partial };
157
+ case "text_start":
158
+ partial.content[proxyEvent.contentIndex] = { type: "text", text: "" };
159
+ return { type: "text_start", contentIndex: proxyEvent.contentIndex, partial };
160
+ case "text_delta": {
161
+ const content = partial.content[proxyEvent.contentIndex];
162
+ if (content?.type === "text") {
163
+ content.text += proxyEvent.delta;
164
+ return {
165
+ type: "text_delta",
166
+ contentIndex: proxyEvent.contentIndex,
167
+ delta: proxyEvent.delta,
168
+ partial,
169
+ };
170
+ }
171
+ throw new Error("Received text_delta for non-text content");
172
+ }
173
+ case "text_end": {
174
+ const content = partial.content[proxyEvent.contentIndex];
175
+ if (content?.type === "text") {
176
+ content.textSignature = proxyEvent.contentSignature;
177
+ return {
178
+ type: "text_end",
179
+ contentIndex: proxyEvent.contentIndex,
180
+ content: content.text,
181
+ partial,
182
+ };
183
+ }
184
+ throw new Error("Received text_end for non-text content");
185
+ }
186
+ case "thinking_start":
187
+ partial.content[proxyEvent.contentIndex] = { type: "thinking", thinking: "" };
188
+ return { type: "thinking_start", contentIndex: proxyEvent.contentIndex, partial };
189
+ case "thinking_delta": {
190
+ const content = partial.content[proxyEvent.contentIndex];
191
+ if (content?.type === "thinking") {
192
+ content.thinking += proxyEvent.delta;
193
+ return {
194
+ type: "thinking_delta",
195
+ contentIndex: proxyEvent.contentIndex,
196
+ delta: proxyEvent.delta,
197
+ partial,
198
+ };
199
+ }
200
+ throw new Error("Received thinking_delta for non-thinking content");
201
+ }
202
+ case "thinking_end": {
203
+ const content = partial.content[proxyEvent.contentIndex];
204
+ if (content?.type === "thinking") {
205
+ content.thinkingSignature = proxyEvent.contentSignature;
206
+ return {
207
+ type: "thinking_end",
208
+ contentIndex: proxyEvent.contentIndex,
209
+ content: content.thinking,
210
+ partial,
211
+ };
212
+ }
213
+ throw new Error("Received thinking_end for non-thinking content");
214
+ }
215
+ case "toolcall_start":
216
+ partial.content[proxyEvent.contentIndex] = {
217
+ type: "toolCall",
218
+ id: proxyEvent.id,
219
+ name: proxyEvent.toolName,
220
+ arguments: {},
221
+ partialJson: "",
222
+ };
223
+ return { type: "toolcall_start", contentIndex: proxyEvent.contentIndex, partial };
224
+ case "toolcall_delta": {
225
+ const content = partial.content[proxyEvent.contentIndex];
226
+ if (content?.type === "toolCall") {
227
+ content.partialJson += proxyEvent.delta;
228
+ content.arguments = parseStreamingJson(content.partialJson) || {};
229
+ partial.content[proxyEvent.contentIndex] = { ...content }; // Trigger reactivity
230
+ return {
231
+ type: "toolcall_delta",
232
+ contentIndex: proxyEvent.contentIndex,
233
+ delta: proxyEvent.delta,
234
+ partial,
235
+ };
236
+ }
237
+ throw new Error("Received toolcall_delta for non-toolCall content");
238
+ }
239
+ case "toolcall_end": {
240
+ const content = partial.content[proxyEvent.contentIndex];
241
+ if (content?.type === "toolCall") {
242
+ delete content.partialJson;
243
+ return {
244
+ type: "toolcall_end",
245
+ contentIndex: proxyEvent.contentIndex,
246
+ toolCall: content,
247
+ partial,
248
+ };
249
+ }
250
+ return undefined;
251
+ }
252
+ case "done":
253
+ partial.stopReason = proxyEvent.reason;
254
+ partial.usage = proxyEvent.usage;
255
+ return { type: "done", reason: proxyEvent.reason, message: partial };
256
+ case "error":
257
+ partial.stopReason = proxyEvent.reason;
258
+ partial.errorMessage = proxyEvent.errorMessage;
259
+ partial.usage = proxyEvent.usage;
260
+ return { type: "error", reason: proxyEvent.reason, error: partial };
261
+ default: {
262
+ const _exhaustiveCheck = proxyEvent;
263
+ console.warn(`Unhandled proxy event type: ${proxyEvent.type}`);
264
+ return undefined;
265
+ }
266
+ }
267
+ }
268
+ //# sourceMappingURL=proxy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"proxy.js","sourceRoot":"","sources":["../src/proxy.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,2CAA2C;AAC3C,OAAO,EAIN,WAAW,EAEX,kBAAkB,GAIlB,MAAM,yBAAyB,CAAC;AAEjC,uDAAuD;AACvD,MAAM,uBAAwB,SAAQ,WAAoD;IACzF,cAAc;QACb,KAAK,CACJ,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,KAAK,MAAM,IAAI,KAAK,CAAC,IAAI,KAAK,OAAO,EAC1D,CAAC,KAAK,EAAE,EAAE,CAAC;YACV,IAAI,KAAK,CAAC,IAAI,KAAK,MAAM;gBAAE,OAAO,KAAK,CA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3C,OAAO,CAAC,IAAI,CAAC,+BAAgC,UAAkB,CAAC,IAAI,EAAE,CAAC,CAAC;YACxE,OAAO,SAAS,CAAC;QAClB,CAAC;IACF,CAAC;AAAA,CACD","sourcesContent":["/**\n * Proxy stream function for apps that route LLM calls through a server.\n * The server manages auth and proxies requests to LLM providers.\n */\n\n// Internal import for JSON parsing utility\nimport {\n\ttype AssistantMessage,\n\ttype AssistantMessageEvent,\n\ttype Context,\n\tEventStream,\n\ttype Model,\n\tparseStreamingJson,\n\ttype SimpleStreamOptions,\n\ttype StopReason,\n\ttype ToolCall,\n} from \"@avadisabelle/ava-pi-ai\";\n\n// Create stream class matching ProxyMessageEventStream\nclass ProxyMessageEventStream extends EventStream<AssistantMessageEvent, AssistantMessage> {\n\tconstructor() {\n\t\tsuper(\n\t\t\t(event) => event.type === \"done\" || event.type === \"error\",\n\t\t\t(event) => {\n\t\t\t\tif (event.type === \"done\") return event.message;\n\t\t\t\tif (event.type === \"error\") return event.error;\n\t\t\t\tthrow new Error(\"Unexpected event type\");\n\t\t\t},\n\t\t);\n\t}\n}\n\n/**\n * Proxy event types - server sends these with partial field stripped to reduce bandwidth.\n */\nexport type ProxyAssistantMessageEvent =\n\t| { type: \"start\" }\n\t| { type: \"text_start\"; contentIndex: number }\n\t| { type: \"text_delta\"; contentIndex: number; delta: string }\n\t| { type: \"text_end\"; contentIndex: number; contentSignature?: string }\n\t| { type: \"thinking_start\"; contentIndex: number }\n\t| { type: \"thinking_delta\"; contentIndex: number; delta: string }\n\t| { type: \"thinking_end\"; contentIndex: number; contentSignature?: string }\n\t| { type: \"toolcall_start\"; contentIndex: number; id: string; toolName: string }\n\t| { type: \"toolcall_delta\"; contentIndex: number; delta: string }\n\t| { type: \"toolcall_end\"; contentIndex: number }\n\t| {\n\t\t\ttype: \"done\";\n\t\t\treason: Extract<StopReason, \"stop\" | \"length\" | \"toolUse\">;\n\t\t\tusage: AssistantMessage[\"usage\"];\n\t }\n\t| {\n\t\t\ttype: \"error\";\n\t\t\treason: Extract<StopReason, \"aborted\" | \"error\">;\n\t\t\terrorMessage?: string;\n\t\t\tusage: AssistantMessage[\"usage\"];\n\t };\n\nexport interface ProxyStreamOptions extends SimpleStreamOptions {\n\t/** Auth token for the proxy server */\n\tauthToken: string;\n\t/** Proxy server URL (e.g., \"https://genai.example.com\") */\n\tproxyUrl: string;\n}\n\n/**\n * Stream function that proxies through a server instead of calling LLM providers directly.\n * The server strips the partial field from delta events to reduce bandwidth.\n * We reconstruct the partial message client-side.\n *\n * Use this as the `streamFn` option when creating an Agent that needs to go through a proxy.\n *\n * @example\n * ```typescript\n * const agent = new Agent({\n * streamFn: (model, context, options) =>\n * streamProxy(model, context, {\n * ...options,\n * authToken: await getAuthToken(),\n * proxyUrl: \"https://genai.example.com\",\n * }),\n * });\n * ```\n */\nexport function streamProxy(model: Model<any>, context: Context, options: ProxyStreamOptions): ProxyMessageEventStream {\n\tconst stream = new ProxyMessageEventStream();\n\n\t(async () => {\n\t\t// Initialize the partial message that we'll build up from events\n\t\tconst partial: AssistantMessage = {\n\t\t\trole: \"assistant\",\n\t\t\tstopReason: \"stop\",\n\t\t\tcontent: [],\n\t\t\tapi: model.api,\n\t\t\tprovider: model.provider,\n\t\t\tmodel: model.id,\n\t\t\tusage: {\n\t\t\t\tinput: 0,\n\t\t\t\toutput: 0,\n\t\t\t\tcacheRead: 0,\n\t\t\t\tcacheWrite: 0,\n\t\t\t\ttotalTokens: 0,\n\t\t\t\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n\t\t\t},\n\t\t\ttimestamp: Date.now(),\n\t\t};\n\n\t\tlet reader: ReadableStreamDefaultReader<Uint8Array> | undefined;\n\n\t\tconst abortHandler = () => {\n\t\t\tif (reader) {\n\t\t\t\treader.cancel(\"Request aborted by user\").catch(() => {});\n\t\t\t}\n\t\t};\n\n\t\tif (options.signal) {\n\t\t\toptions.signal.addEventListener(\"abort\", abortHandler);\n\t\t}\n\n\t\ttry {\n\t\t\tconst response = await fetch(`${options.proxyUrl}/api/stream`, {\n\t\t\t\tmethod: \"POST\",\n\t\t\t\theaders: {\n\t\t\t\t\tAuthorization: `Bearer ${options.authToken}`,\n\t\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t\t},\n\t\t\t\tbody: JSON.stringify({\n\t\t\t\t\tmodel,\n\t\t\t\t\tcontext,\n\t\t\t\t\toptions: {\n\t\t\t\t\t\ttemperature: options.temperature,\n\t\t\t\t\t\tmaxTokens: options.maxTokens,\n\t\t\t\t\t\treasoning: options.reasoning,\n\t\t\t\t\t},\n\t\t\t\t}),\n\t\t\t\tsignal: options.signal,\n\t\t\t});\n\n\t\t\tif (!response.ok) {\n\t\t\t\tlet errorMessage = `Proxy error: ${response.status} ${response.statusText}`;\n\t\t\t\ttry {\n\t\t\t\t\tconst errorData = (await response.json()) as { error?: string };\n\t\t\t\t\tif (errorData.error) {\n\t\t\t\t\t\terrorMessage = `Proxy error: ${errorData.error}`;\n\t\t\t\t\t}\n\t\t\t\t} catch {\n\t\t\t\t\t// Couldn't parse error response\n\t\t\t\t}\n\t\t\t\tthrow new Error(errorMessage);\n\t\t\t}\n\n\t\t\treader = response.body!.getReader();\n\t\t\tconst decoder = new TextDecoder();\n\t\t\tlet buffer = \"\";\n\n\t\t\twhile (true) {\n\t\t\t\tconst { done, value } = await reader.read();\n\t\t\t\tif (done) break;\n\n\t\t\t\tif (options.signal?.aborted) {\n\t\t\t\t\tthrow new Error(\"Request aborted by user\");\n\t\t\t\t}\n\n\t\t\t\tbuffer += decoder.decode(value, { stream: true });\n\t\t\t\tconst lines = buffer.split(\"\\n\");\n\t\t\t\tbuffer = lines.pop() || \"\";\n\n\t\t\t\tfor (const line of lines) {\n\t\t\t\t\tif (line.startsWith(\"data: \")) {\n\t\t\t\t\t\tconst data = line.slice(6).trim();\n\t\t\t\t\t\tif (data) {\n\t\t\t\t\t\t\tconst proxyEvent = JSON.parse(data) as ProxyAssistantMessageEvent;\n\t\t\t\t\t\t\tconst event = processProxyEvent(proxyEvent, partial);\n\t\t\t\t\t\t\tif (event) {\n\t\t\t\t\t\t\t\tstream.push(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (options.signal?.aborted) {\n\t\t\t\tthrow new Error(\"Request aborted by user\");\n\t\t\t}\n\n\t\t\tstream.end();\n\t\t} catch (error) {\n\t\t\tconst errorMessage = error instanceof Error ? error.message : String(error);\n\t\t\tconst reason = options.signal?.aborted ? \"aborted\" : \"error\";\n\t\t\tpartial.stopReason = reason;\n\t\t\tpartial.errorMessage = errorMessage;\n\t\t\tstream.push({\n\t\t\t\ttype: \"error\",\n\t\t\t\treason,\n\t\t\t\terror: partial,\n\t\t\t});\n\t\t\tstream.end();\n\t\t} finally {\n\t\t\tif (options.signal) {\n\t\t\t\toptions.signal.removeEventListener(\"abort\", abortHandler);\n\t\t\t}\n\t\t}\n\t})();\n\n\treturn stream;\n}\n\n/**\n * Process a proxy event and update the partial message.\n */\nfunction processProxyEvent(\n\tproxyEvent: ProxyAssistantMessageEvent,\n\tpartial: AssistantMessage,\n): AssistantMessageEvent | undefined {\n\tswitch (proxyEvent.type) {\n\t\tcase \"start\":\n\t\t\treturn { type: \"start\", partial };\n\n\t\tcase \"text_start\":\n\t\t\tpartial.content[proxyEvent.contentIndex] = { type: \"text\", text: \"\" };\n\t\t\treturn { type: \"text_start\", contentIndex: proxyEvent.contentIndex, partial };\n\n\t\tcase \"text_delta\": {\n\t\t\tconst content = partial.content[proxyEvent.contentIndex];\n\t\t\tif (content?.type === \"text\") {\n\t\t\t\tcontent.text += proxyEvent.delta;\n\t\t\t\treturn {\n\t\t\t\t\ttype: \"text_delta\",\n\t\t\t\t\tcontentIndex: proxyEvent.contentIndex,\n\t\t\t\t\tdelta: proxyEvent.delta,\n\t\t\t\t\tpartial,\n\t\t\t\t};\n\t\t\t}\n\t\t\tthrow new Error(\"Received text_delta for non-text content\");\n\t\t}\n\n\t\tcase \"text_end\": {\n\t\t\tconst content = partial.content[proxyEvent.contentIndex];\n\t\t\tif (content?.type === \"text\") {\n\t\t\t\tcontent.textSignature = proxyEvent.contentSignature;\n\t\t\t\treturn {\n\t\t\t\t\ttype: \"text_end\",\n\t\t\t\t\tcontentIndex: proxyEvent.contentIndex,\n\t\t\t\t\tcontent: content.text,\n\t\t\t\t\tpartial,\n\t\t\t\t};\n\t\t\t}\n\t\t\tthrow new Error(\"Received text_end for non-text content\");\n\t\t}\n\n\t\tcase \"thinking_start\":\n\t\t\tpartial.content[proxyEvent.contentIndex] = { type: \"thinking\", thinking: \"\" };\n\t\t\treturn { type: \"thinking_start\", contentIndex: proxyEvent.contentIndex, partial };\n\n\t\tcase \"thinking_delta\": {\n\t\t\tconst content = partial.content[proxyEvent.contentIndex];\n\t\t\tif (content?.type === \"thinking\") {\n\t\t\t\tcontent.thinking += proxyEvent.delta;\n\t\t\t\treturn {\n\t\t\t\t\ttype: \"thinking_delta\",\n\t\t\t\t\tcontentIndex: proxyEvent.contentIndex,\n\t\t\t\t\tdelta: proxyEvent.delta,\n\t\t\t\t\tpartial,\n\t\t\t\t};\n\t\t\t}\n\t\t\tthrow new Error(\"Received thinking_delta for non-thinking content\");\n\t\t}\n\n\t\tcase \"thinking_end\": {\n\t\t\tconst content = partial.content[proxyEvent.contentIndex];\n\t\t\tif (content?.type === \"thinking\") {\n\t\t\t\tcontent.thinkingSignature = proxyEvent.contentSignature;\n\t\t\t\treturn {\n\t\t\t\t\ttype: \"thinking_end\",\n\t\t\t\t\tcontentIndex: proxyEvent.contentIndex,\n\t\t\t\t\tcontent: content.thinking,\n\t\t\t\t\tpartial,\n\t\t\t\t};\n\t\t\t}\n\t\t\tthrow new Error(\"Received thinking_end for non-thinking content\");\n\t\t}\n\n\t\tcase \"toolcall_start\":\n\t\t\tpartial.content[proxyEvent.contentIndex] = {\n\t\t\t\ttype: \"toolCall\",\n\t\t\t\tid: proxyEvent.id,\n\t\t\t\tname: proxyEvent.toolName,\n\t\t\t\targuments: {},\n\t\t\t\tpartialJson: \"\",\n\t\t\t} satisfies ToolCall & { partialJson: string } as ToolCall;\n\t\t\treturn { type: \"toolcall_start\", contentIndex: proxyEvent.contentIndex, partial };\n\n\t\tcase \"toolcall_delta\": {\n\t\t\tconst content = partial.content[proxyEvent.contentIndex];\n\t\t\tif (content?.type === \"toolCall\") {\n\t\t\t\t(content as any).partialJson += proxyEvent.delta;\n\t\t\t\tcontent.arguments = parseStreamingJson((content as any).partialJson) || {};\n\t\t\t\tpartial.content[proxyEvent.contentIndex] = { ...content }; // Trigger reactivity\n\t\t\t\treturn {\n\t\t\t\t\ttype: \"toolcall_delta\",\n\t\t\t\t\tcontentIndex: proxyEvent.contentIndex,\n\t\t\t\t\tdelta: proxyEvent.delta,\n\t\t\t\t\tpartial,\n\t\t\t\t};\n\t\t\t}\n\t\t\tthrow new Error(\"Received toolcall_delta for non-toolCall content\");\n\t\t}\n\n\t\tcase \"toolcall_end\": {\n\t\t\tconst content = partial.content[proxyEvent.contentIndex];\n\t\t\tif (content?.type === \"toolCall\") {\n\t\t\t\tdelete (content as any).partialJson;\n\t\t\t\treturn {\n\t\t\t\t\ttype: \"toolcall_end\",\n\t\t\t\t\tcontentIndex: proxyEvent.contentIndex,\n\t\t\t\t\ttoolCall: content,\n\t\t\t\t\tpartial,\n\t\t\t\t};\n\t\t\t}\n\t\t\treturn undefined;\n\t\t}\n\n\t\tcase \"done\":\n\t\t\tpartial.stopReason = proxyEvent.reason;\n\t\t\tpartial.usage = proxyEvent.usage;\n\t\t\treturn { type: \"done\", reason: proxyEvent.reason, message: partial };\n\n\t\tcase \"error\":\n\t\t\tpartial.stopReason = proxyEvent.reason;\n\t\t\tpartial.errorMessage = proxyEvent.errorMessage;\n\t\t\tpartial.usage = proxyEvent.usage;\n\t\t\treturn { type: \"error\", reason: proxyEvent.reason, error: partial };\n\n\t\tdefault: {\n\t\t\tconst _exhaustiveCheck: never = proxyEvent;\n\t\t\tconsole.warn(`Unhandled proxy event type: ${(proxyEvent as any).type}`);\n\t\t\treturn undefined;\n\t\t}\n\t}\n}\n"]}
@@ -0,0 +1,286 @@
1
+ import type { AssistantMessage, AssistantMessageEvent, ImageContent, Message, Model, SimpleStreamOptions, streamSimple, TextContent, Tool, ToolResultMessage } from "@avadisabelle/ava-pi-ai";
2
+ import type { Static, TSchema } from "@sinclair/typebox";
3
+ /**
4
+ * Stream function used by the agent loop.
5
+ *
6
+ * Contract:
7
+ * - Must not throw or return a rejected promise for request/model/runtime failures.
8
+ * - Must return an AssistantMessageEventStream.
9
+ * - Failures must be encoded in the returned stream via protocol events and a
10
+ * final AssistantMessage with stopReason "error" or "aborted" and errorMessage.
11
+ */
12
+ export type StreamFn = (...args: Parameters<typeof streamSimple>) => ReturnType<typeof streamSimple> | Promise<ReturnType<typeof streamSimple>>;
13
+ /**
14
+ * Configuration for how tool calls from a single assistant message are executed.
15
+ *
16
+ * - "sequential": each tool call is prepared, executed, and finalized before the next one starts.
17
+ * - "parallel": tool calls are prepared sequentially, then allowed tools execute concurrently.
18
+ * Final tool results are still emitted in assistant source order.
19
+ */
20
+ export type ToolExecutionMode = "sequential" | "parallel";
21
+ /** A single tool call content block emitted by an assistant message. */
22
+ export type AgentToolCall = Extract<AssistantMessage["content"][number], {
23
+ type: "toolCall";
24
+ }>;
25
+ /**
26
+ * Result returned from `beforeToolCall`.
27
+ *
28
+ * Returning `{ block: true }` prevents the tool from executing. The loop emits an error tool result instead.
29
+ * `reason` becomes the text shown in that error result. If omitted, a default blocked message is used.
30
+ */
31
+ export interface BeforeToolCallResult {
32
+ block?: boolean;
33
+ reason?: string;
34
+ }
35
+ /**
36
+ * Partial override returned from `afterToolCall`.
37
+ *
38
+ * Merge semantics are field-by-field:
39
+ * - `content`: if provided, replaces the tool result content array in full
40
+ * - `details`: if provided, replaces the tool result details value in full
41
+ * - `isError`: if provided, replaces the tool result error flag
42
+ *
43
+ * Omitted fields keep the original executed tool result values.
44
+ * There is no deep merge for `content` or `details`.
45
+ */
46
+ export interface AfterToolCallResult {
47
+ content?: (TextContent | ImageContent)[];
48
+ details?: unknown;
49
+ isError?: boolean;
50
+ }
51
+ /** Context passed to `beforeToolCall`. */
52
+ export interface BeforeToolCallContext {
53
+ /** The assistant message that requested the tool call. */
54
+ assistantMessage: AssistantMessage;
55
+ /** The raw tool call block from `assistantMessage.content`. */
56
+ toolCall: AgentToolCall;
57
+ /** Validated tool arguments for the target tool schema. */
58
+ args: unknown;
59
+ /** Current agent context at the time the tool call is prepared. */
60
+ context: AgentContext;
61
+ }
62
+ /** Context passed to `afterToolCall`. */
63
+ export interface AfterToolCallContext {
64
+ /** The assistant message that requested the tool call. */
65
+ assistantMessage: AssistantMessage;
66
+ /** The raw tool call block from `assistantMessage.content`. */
67
+ toolCall: AgentToolCall;
68
+ /** Validated tool arguments for the target tool schema. */
69
+ args: unknown;
70
+ /** The executed tool result before any `afterToolCall` overrides are applied. */
71
+ result: AgentToolResult<any>;
72
+ /** Whether the executed tool result is currently treated as an error. */
73
+ isError: boolean;
74
+ /** Current agent context at the time the tool call is finalized. */
75
+ context: AgentContext;
76
+ }
77
+ export interface AgentLoopConfig extends SimpleStreamOptions {
78
+ model: Model<any>;
79
+ /**
80
+ * Converts AgentMessage[] to LLM-compatible Message[] before each LLM call.
81
+ *
82
+ * Each AgentMessage must be converted to a UserMessage, AssistantMessage, or ToolResultMessage
83
+ * that the LLM can understand. AgentMessages that cannot be converted (e.g., UI-only notifications,
84
+ * status messages) should be filtered out.
85
+ *
86
+ * Contract: must not throw or reject. Return a safe fallback value instead.
87
+ * Throwing interrupts the low-level agent loop without producing a normal event sequence.
88
+ *
89
+ * @example
90
+ * ```typescript
91
+ * convertToLlm: (messages) => messages.flatMap(m => {
92
+ * if (m.role === "custom") {
93
+ * // Convert custom message to user message
94
+ * return [{ role: "user", content: m.content, timestamp: m.timestamp }];
95
+ * }
96
+ * if (m.role === "notification") {
97
+ * // Filter out UI-only messages
98
+ * return [];
99
+ * }
100
+ * // Pass through standard LLM messages
101
+ * return [m];
102
+ * })
103
+ * ```
104
+ */
105
+ convertToLlm: (messages: AgentMessage[]) => Message[] | Promise<Message[]>;
106
+ /**
107
+ * Optional transform applied to the context before `convertToLlm`.
108
+ *
109
+ * Use this for operations that work at the AgentMessage level:
110
+ * - Context window management (pruning old messages)
111
+ * - Injecting context from external sources
112
+ *
113
+ * Contract: must not throw or reject. Return the original messages or another
114
+ * safe fallback value instead.
115
+ *
116
+ * @example
117
+ * ```typescript
118
+ * transformContext: async (messages) => {
119
+ * if (estimateTokens(messages) > MAX_TOKENS) {
120
+ * return pruneOldMessages(messages);
121
+ * }
122
+ * return messages;
123
+ * }
124
+ * ```
125
+ */
126
+ transformContext?: (messages: AgentMessage[], signal?: AbortSignal) => Promise<AgentMessage[]>;
127
+ /**
128
+ * Resolves an API key dynamically for each LLM call.
129
+ *
130
+ * Useful for short-lived OAuth tokens (e.g., GitHub Copilot) that may expire
131
+ * during long-running tool execution phases.
132
+ *
133
+ * Contract: must not throw or reject. Return undefined when no key is available.
134
+ */
135
+ getApiKey?: (provider: string) => Promise<string | undefined> | string | undefined;
136
+ /**
137
+ * Returns steering messages to inject into the conversation mid-run.
138
+ *
139
+ * Called after the current assistant turn finishes executing its tool calls.
140
+ * If messages are returned, they are added to the context before the next LLM call.
141
+ * Tool calls from the current assistant message are not skipped.
142
+ *
143
+ * Use this for "steering" the agent while it's working.
144
+ *
145
+ * Contract: must not throw or reject. Return [] when no steering messages are available.
146
+ */
147
+ getSteeringMessages?: () => Promise<AgentMessage[]>;
148
+ /**
149
+ * Returns follow-up messages to process after the agent would otherwise stop.
150
+ *
151
+ * Called when the agent has no more tool calls and no steering messages.
152
+ * If messages are returned, they're added to the context and the agent
153
+ * continues with another turn.
154
+ *
155
+ * Use this for follow-up messages that should wait until the agent finishes.
156
+ *
157
+ * Contract: must not throw or reject. Return [] when no follow-up messages are available.
158
+ */
159
+ getFollowUpMessages?: () => Promise<AgentMessage[]>;
160
+ /**
161
+ * Tool execution mode.
162
+ * - "sequential": execute tool calls one by one
163
+ * - "parallel": preflight tool calls sequentially, then execute allowed tools concurrently
164
+ *
165
+ * Default: "parallel"
166
+ */
167
+ toolExecution?: ToolExecutionMode;
168
+ /**
169
+ * Called before a tool is executed, after arguments have been validated.
170
+ *
171
+ * Return `{ block: true }` to prevent execution. The loop emits an error tool result instead.
172
+ * The hook receives the agent abort signal and is responsible for honoring it.
173
+ */
174
+ beforeToolCall?: (context: BeforeToolCallContext, signal?: AbortSignal) => Promise<BeforeToolCallResult | undefined>;
175
+ /**
176
+ * Called after a tool finishes executing, before final tool events are emitted.
177
+ *
178
+ * Return an `AfterToolCallResult` to override parts of the executed tool result:
179
+ * - `content` replaces the full content array
180
+ * - `details` replaces the full details payload
181
+ * - `isError` replaces the error flag
182
+ *
183
+ * Any omitted fields keep their original values. No deep merge is performed.
184
+ * The hook receives the agent abort signal and is responsible for honoring it.
185
+ */
186
+ afterToolCall?: (context: AfterToolCallContext, signal?: AbortSignal) => Promise<AfterToolCallResult | undefined>;
187
+ }
188
+ /**
189
+ * Thinking/reasoning level for models that support it.
190
+ * Note: "xhigh" is only supported by OpenAI gpt-5.1-codex-max, gpt-5.2, gpt-5.2-codex, gpt-5.3, and gpt-5.3-codex models.
191
+ */
192
+ export type ThinkingLevel = "off" | "minimal" | "low" | "medium" | "high" | "xhigh";
193
+ /**
194
+ * Extensible interface for custom app messages.
195
+ * Apps can extend via declaration merging:
196
+ *
197
+ * @example
198
+ * ```typescript
199
+ * declare module "@avadisabelle/ava-pi-agent-core" {
200
+ * interface CustomAgentMessages {
201
+ * artifact: ArtifactMessage;
202
+ * notification: NotificationMessage;
203
+ * }
204
+ * }
205
+ * ```
206
+ */
207
+ export interface CustomAgentMessages {
208
+ }
209
+ /**
210
+ * AgentMessage: Union of LLM messages + custom messages.
211
+ * This abstraction allows apps to add custom message types while maintaining
212
+ * type safety and compatibility with the base LLM messages.
213
+ */
214
+ export type AgentMessage = Message | CustomAgentMessages[keyof CustomAgentMessages];
215
+ /**
216
+ * Agent state containing all configuration and conversation data.
217
+ */
218
+ export interface AgentState {
219
+ systemPrompt: string;
220
+ model: Model<any>;
221
+ thinkingLevel: ThinkingLevel;
222
+ tools: AgentTool<any>[];
223
+ messages: AgentMessage[];
224
+ isStreaming: boolean;
225
+ streamMessage: AgentMessage | null;
226
+ pendingToolCalls: Set<string>;
227
+ error?: string;
228
+ }
229
+ export interface AgentToolResult<T> {
230
+ content: (TextContent | ImageContent)[];
231
+ details: T;
232
+ }
233
+ export type AgentToolUpdateCallback<T = any> = (partialResult: AgentToolResult<T>) => void;
234
+ export interface AgentTool<TParameters extends TSchema = TSchema, TDetails = any> extends Tool<TParameters> {
235
+ label: string;
236
+ execute: (toolCallId: string, params: Static<TParameters>, signal?: AbortSignal, onUpdate?: AgentToolUpdateCallback<TDetails>) => Promise<AgentToolResult<TDetails>>;
237
+ }
238
+ export interface AgentContext {
239
+ systemPrompt: string;
240
+ messages: AgentMessage[];
241
+ tools?: AgentTool<any>[];
242
+ }
243
+ /**
244
+ * Events emitted by the Agent for UI updates.
245
+ * These events provide fine-grained lifecycle information for messages, turns, and tool executions.
246
+ */
247
+ export type AgentEvent = {
248
+ type: "agent_start";
249
+ } | {
250
+ type: "agent_end";
251
+ messages: AgentMessage[];
252
+ } | {
253
+ type: "turn_start";
254
+ } | {
255
+ type: "turn_end";
256
+ message: AgentMessage;
257
+ toolResults: ToolResultMessage[];
258
+ } | {
259
+ type: "message_start";
260
+ message: AgentMessage;
261
+ } | {
262
+ type: "message_update";
263
+ message: AgentMessage;
264
+ assistantMessageEvent: AssistantMessageEvent;
265
+ } | {
266
+ type: "message_end";
267
+ message: AgentMessage;
268
+ } | {
269
+ type: "tool_execution_start";
270
+ toolCallId: string;
271
+ toolName: string;
272
+ args: any;
273
+ } | {
274
+ type: "tool_execution_update";
275
+ toolCallId: string;
276
+ toolName: string;
277
+ args: any;
278
+ partialResult: any;
279
+ } | {
280
+ type: "tool_execution_end";
281
+ toolCallId: string;
282
+ toolName: string;
283
+ result: any;
284
+ isError: boolean;
285
+ };
286
+ //# sourceMappingURL=types.d.ts.map