@alpaca-editor/core 1.0.4037 → 1.0.4039

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.
@@ -149,11 +149,15 @@ export async function connectToAgentStream(
149
149
  onMessage: (message: AgentStreamMessage) => void,
150
150
  signal?: AbortSignal,
151
151
  ): Promise<void> {
152
+ console.log("RECONNECT: Starting connectToAgentStream for agent:", agentId);
153
+
152
154
  // Add cache-busting timestamp to prevent browser connection reuse
153
155
  const timestamp = Date.now();
154
156
  const streamUrl =
155
157
  AGENT_BASE_URL + "/stream?agentId=" + agentId + "&t=" + timestamp;
156
158
 
159
+ console.log("RECONNECT: Stream URL:", streamUrl);
160
+
157
161
  // Retry logic to handle potential race conditions
158
162
  let retryCount = 0;
159
163
  const maxRetries = 3;
@@ -161,6 +165,10 @@ export async function connectToAgentStream(
161
165
 
162
166
  while (retryCount <= maxRetries) {
163
167
  try {
168
+ console.log(
169
+ `RECONNECT: Attempting connection ${retryCount + 1}/${maxRetries + 1} to agent ${agentId}`,
170
+ );
171
+
164
172
  const response = await fetch(streamUrl, {
165
173
  headers: {
166
174
  Accept: "text/event-stream",
@@ -171,9 +179,22 @@ export async function connectToAgentStream(
171
179
  signal,
172
180
  });
173
181
 
182
+ console.log(`RECONNECT: Fetch response for agent ${agentId}:`, {
183
+ status: response.status,
184
+ statusText: response.statusText,
185
+ ok: response.ok,
186
+ headers: Object.fromEntries(response.headers.entries()),
187
+ });
188
+
174
189
  if (response.ok) {
175
190
  // Success - proceed with stream processing
191
+ console.log(
192
+ `RECONNECT: Successfully connected to stream for agent ${agentId}, starting to process events`,
193
+ );
176
194
  await processEventStream(response, onMessage);
195
+ console.log(
196
+ `RECONNECT: Event stream processing completed for agent ${agentId}`,
197
+ );
177
198
  return;
178
199
  }
179
200
 
@@ -181,28 +202,34 @@ export async function connectToAgentStream(
181
202
  if (response.status === 404 && retryCount < maxRetries) {
182
203
  retryCount++;
183
204
  console.warn(
184
- `Agent stream not ready (404), retrying in ${retryDelay}ms... (${retryCount}/${maxRetries})`,
205
+ `RECONNECT: Agent stream not ready (404), retrying in ${retryDelay}ms... (${retryCount}/${maxRetries}) for agent ${agentId}`,
185
206
  );
186
207
  await new Promise((resolve) => setTimeout(resolve, retryDelay));
187
208
  continue;
188
209
  }
189
210
 
190
211
  // For other errors or max retries reached, throw
191
- throw new Error(
192
- `Failed to connect to agent stream: ${response.status} ${response.statusText}`,
193
- );
212
+ const errorMessage = `Failed to connect to agent stream: ${response.status} ${response.statusText}`;
213
+ console.error(`RECONNECT: ${errorMessage} for agent ${agentId}`);
214
+ throw new Error(errorMessage);
194
215
  } catch (error) {
216
+ console.log(`RECONNECT: Caught error for agent ${agentId}:`, error);
217
+
195
218
  if (signal?.aborted) {
219
+ console.log(`RECONNECT: Request aborted for agent ${agentId}`);
196
220
  throw error; // Don't retry if cancelled
197
221
  }
198
222
 
199
223
  if (retryCount >= maxRetries) {
224
+ console.error(
225
+ `RECONNECT: Max retries reached for agent ${agentId}, giving up`,
226
+ );
200
227
  throw error;
201
228
  }
202
229
 
203
230
  retryCount++;
204
231
  console.warn(
205
- `Error connecting to agent stream, retrying... (${retryCount}/${maxRetries})`,
232
+ `RECONNECT: Error connecting to agent stream, retrying... (${retryCount}/${maxRetries}) for agent ${agentId}`,
206
233
  error,
207
234
  );
208
235
  await new Promise((resolve) => setTimeout(resolve, retryDelay));
@@ -217,19 +244,27 @@ async function processEventStream(
217
244
  response: Response,
218
245
  onMessage: (message: AgentStreamMessage) => void,
219
246
  ): Promise<void> {
247
+ console.log("RECONNECT: Starting to process event stream");
248
+
220
249
  const reader = response.body?.getReader();
221
250
  if (!reader) {
222
- throw new Error("Failed to get stream reader");
251
+ const error = "Failed to get stream reader";
252
+ console.error("RECONNECT:", error);
253
+ throw new Error(error);
223
254
  }
224
255
 
225
256
  const decoder = new TextDecoder();
226
257
  let buffer = "";
258
+ let messageCount = 0;
227
259
 
228
260
  try {
229
261
  while (true) {
230
262
  const { done, value } = await reader.read();
231
263
 
232
264
  if (done) {
265
+ console.log(
266
+ `RECONNECT: Stream reader finished. Total messages processed: ${messageCount}`,
267
+ );
233
268
  break;
234
269
  }
235
270
 
@@ -253,10 +288,21 @@ async function processEventStream(
253
288
  try {
254
289
  const data = line.slice(6); // Remove 'data: ' prefix
255
290
  if (data.trim()) {
291
+ messageCount++;
256
292
  const message: AgentStreamMessage = JSON.parse(data);
257
293
 
294
+ console.log(
295
+ `RECONNECT: Processing SSE message #${messageCount}:`,
296
+ {
297
+ type: message.type,
298
+ hasData: !!message.data,
299
+ error: message.error,
300
+ },
301
+ );
302
+
258
303
  // Normalize message type (handle both numeric and string enum values)
259
304
  if (typeof message.type === "number") {
305
+ const originalType = message.type;
260
306
  switch (message.type) {
261
307
  case 0:
262
308
  message.type = AgentStreamMessageType.StatusUpdate;
@@ -282,6 +328,9 @@ async function processEventStream(
282
328
  default:
283
329
  message.type = `Unknown_${message.type}`;
284
330
  }
331
+ console.log(
332
+ `RECONNECT: Normalized message type from ${originalType} to ${message.type}`,
333
+ );
285
334
  }
286
335
 
287
336
  onMessage(message);
@@ -291,12 +340,18 @@ async function processEventStream(
291
340
  message.type === AgentStreamMessageType.Completed ||
292
341
  message.type === AgentStreamMessageType.Error
293
342
  ) {
294
- // console.log("Stream ending due to message type:", message.type);
343
+ console.log(
344
+ `RECONNECT: Stream ending due to message type: ${message.type}. Total messages: ${messageCount}`,
345
+ );
295
346
  return;
296
347
  }
297
348
  }
298
349
  } catch (error) {
299
- console.error("Failed to parse SSE message:", error, line);
350
+ console.error(
351
+ "RECONNECT: Failed to parse SSE message:",
352
+ error,
353
+ line,
354
+ );
300
355
  // Send error message to the client to notify user
301
356
  onMessage({
302
357
  type: AgentStreamMessageType.Error,
@@ -305,11 +360,16 @@ async function processEventStream(
305
360
  timestamp: new Date().toISOString(),
306
361
  });
307
362
  }
363
+ } else if (line.trim() && !line.startsWith(":")) {
364
+ // Log non-data, non-comment lines for debugging
365
+ console.log("RECONNECT: Unexpected SSE line format:", line);
308
366
  }
309
367
  }
310
368
  }
311
369
  } finally {
312
- // console.log("Stream ended, releasing reader lock");
370
+ console.log(
371
+ `RECONNECT: Stream ended, releasing reader lock. Final message count: ${messageCount}`,
372
+ );
313
373
  reader.releaseLock();
314
374
  }
315
375
  }
@@ -457,7 +517,7 @@ export async function cancelAgent(
457
517
  agentId: string,
458
518
  context: AiContext,
459
519
  ): Promise<any> {
460
- const response = await fetch(AGENT_BASE_URL + "/cancelAgent", {
520
+ const response = await fetch(AGENT_BASE_URL + "/cancel", {
461
521
  method: "POST",
462
522
  headers: {
463
523
  "Content-Type": "application/json",
@@ -529,14 +589,27 @@ export async function checkAgentState(
529
589
  agentId: string,
530
590
  context: AiContext,
531
591
  ): Promise<{ exists: boolean; agent?: AgentChat; isRunning: boolean }> {
592
+ console.log("RECONNECT: Checking agent state for:", agentId);
593
+
532
594
  try {
533
595
  const agent = await getAgent(agentId, context);
596
+ console.log("RECONNECT: Agent found:", {
597
+ id: agent.id,
598
+ status: agent.status,
599
+ messageCount: agent.messages?.length || 0,
600
+ lastMessageDate: agent.lastMessageDate,
601
+ });
602
+
603
+ const isRunning = agent.status === 0;
604
+ console.log("RECONNECT: Agent running status:", isRunning);
605
+
534
606
  return {
535
607
  exists: true,
536
608
  agent,
537
- isRunning: agent.status === "Active",
609
+ isRunning,
538
610
  };
539
611
  } catch (error) {
612
+ console.log("RECONNECT: Agent not found or error fetching:", error);
540
613
  // If agent doesn't exist, return false
541
614
  return {
542
615
  exists: false,
@@ -572,6 +645,8 @@ export function convertAgentMessagesToTerminalFormat(
572
645
  function: {
573
646
  name: tc.functionName,
574
647
  arguments: tc.functionArguments,
648
+ result: tc.functionResult,
649
+ error: tc.functionError,
575
650
  },
576
651
  }));
577
652
  }
package/src/revision.ts CHANGED
@@ -1,2 +1,2 @@
1
- export const version = "1.0.4037";
2
- export const buildDate = "2025-08-10 09:51:50";
1
+ export const version = "1.0.4039";
2
+ export const buildDate = "2025-08-11 14:38:58";