@oh-my-pi/pi-agent-core 6.7.670 → 6.8.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.
Files changed (2) hide show
  1. package/package.json +5 -3
  2. package/src/proxy.ts +14 -30
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oh-my-pi/pi-agent-core",
3
- "version": "6.7.670",
3
+ "version": "6.8.1",
4
4
  "description": "General-purpose agent with transport abstraction, state management, and attachment support",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
@@ -13,8 +13,9 @@
13
13
  "test": "vitest --run"
14
14
  },
15
15
  "dependencies": {
16
- "@oh-my-pi/pi-ai": "6.7.670",
17
- "@oh-my-pi/pi-tui": "6.7.670"
16
+ "@oh-my-pi/pi-ai": "6.8.1",
17
+ "@oh-my-pi/pi-tui": "6.8.1",
18
+ "@oh-my-pi/pi-utils": "6.8.1"
18
19
  },
19
20
  "keywords": [
20
21
  "ai",
@@ -34,6 +35,7 @@
34
35
  "bun": ">=1.0.0"
35
36
  },
36
37
  "devDependencies": {
38
+ "@sinclair/typebox": "^0.34.46",
37
39
  "@types/node": "^24.3.0",
38
40
  "vitest": "^3.2.4"
39
41
  }
package/src/proxy.ts CHANGED
@@ -14,6 +14,7 @@ import {
14
14
  type ToolCall,
15
15
  } from "@oh-my-pi/pi-ai";
16
16
  import { parseStreamingJson } from "@oh-my-pi/pi-ai/src/utils/json-parse";
17
+ import { readSseEvents } from "@oh-my-pi/pi-utils";
17
18
 
18
19
  // Create stream class matching ProxyMessageEventStream
19
20
  class ProxyMessageEventStream extends EventStream<AssistantMessageEvent, AssistantMessage> {
@@ -104,20 +105,19 @@ export function streamProxy(model: Model<any>, context: Context, options: ProxyS
104
105
  timestamp: Date.now(),
105
106
  };
106
107
 
107
- let reader: ReadableStreamDefaultReader<Uint8Array> | undefined;
108
-
108
+ let response: Response | null = null;
109
109
  const abortHandler = () => {
110
- if (reader) {
111
- reader.cancel("Request aborted by user").catch(() => {});
110
+ const body = response?.body;
111
+ if (body) {
112
+ body.cancel("Request aborted by user").catch(() => {});
112
113
  }
113
114
  };
114
-
115
115
  if (options.signal) {
116
- options.signal.addEventListener("abort", abortHandler);
116
+ options.signal.addEventListener("abort", abortHandler, { once: true });
117
117
  }
118
118
 
119
119
  try {
120
- const response = await fetch(`${options.proxyUrl}/api/stream`, {
120
+ response = await fetch(`${options.proxyUrl}/api/stream`, {
121
121
  method: "POST",
122
122
  headers: {
123
123
  Authorization: `Bearer ${options.authToken}`,
@@ -148,33 +148,17 @@ export function streamProxy(model: Model<any>, context: Context, options: ProxyS
148
148
  throw new Error(errorMessage);
149
149
  }
150
150
 
151
- reader = response.body!.getReader() as ReadableStreamDefaultReader<Uint8Array>;
152
- const decoder = new TextDecoder();
153
- let buffer = "";
154
-
155
- while (true) {
156
- const { done, value } = await reader!.read();
157
- if (done) break;
158
-
151
+ for await (const event of readSseEvents(response.body!)) {
159
152
  if (options.signal?.aborted) {
160
153
  throw new Error("Request aborted by user");
161
154
  }
162
155
 
163
- buffer += decoder.decode(value, { stream: true });
164
- const lines = buffer.split("\n");
165
- buffer = lines.pop() || "";
166
-
167
- for (const line of lines) {
168
- if (line.startsWith("data: ")) {
169
- const data = line.slice(6).trim();
170
- if (data) {
171
- const proxyEvent = JSON.parse(data) as ProxyAssistantMessageEvent;
172
- const event = processProxyEvent(proxyEvent, partial);
173
- if (event) {
174
- stream.push(event);
175
- }
176
- }
177
- }
156
+ const data = event.data?.trim();
157
+ if (!data || data === "[DONE]") continue;
158
+ const proxyEvent = JSON.parse(data) as ProxyAssistantMessageEvent;
159
+ const parsedEvent = processProxyEvent(proxyEvent, partial);
160
+ if (parsedEvent) {
161
+ stream.push(parsedEvent);
178
162
  }
179
163
  }
180
164