@posthog/agent 2.0.2 → 2.0.3

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.
@@ -60,6 +60,9 @@ declare class SessionLogWriter {
60
60
  isRegistered(sessionId: string): boolean;
61
61
  appendRawLine(sessionId: string, line: string): void;
62
62
  flush(sessionId: string): Promise<void>;
63
+ private isAgentMessageChunk;
64
+ private extractChunkText;
65
+ private emitCoalescedMessage;
63
66
  private scheduleFlush;
64
67
  }
65
68
 
package/dist/agent.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { b as Agent } from './agent-DBQY1BfC.js';
1
+ export { b as Agent } from './agent-kRbaLUfe.js';
2
2
  import './types.js';
3
3
  import '@agentclientprotocol/sdk';
4
4
  import './logger-DDBiMOOD.js';
package/dist/agent.js CHANGED
@@ -276,7 +276,7 @@ import { v7 as uuidv7 } from "uuid";
276
276
  // package.json
277
277
  var package_default = {
278
278
  name: "@posthog/agent",
279
- version: "2.0.2",
279
+ version: "2.0.3",
280
280
  repository: "https://github.com/PostHog/twig",
281
281
  description: "TypeScript agent framework wrapping Claude Agent SDK with Git-based task execution for PostHog",
282
282
  exports: {
@@ -322,7 +322,7 @@ var package_default = {
322
322
  }
323
323
  },
324
324
  bin: {
325
- "agent-server": "./dist/server/bin.js"
325
+ "agent-server": "./dist/server/bin.cjs"
326
326
  },
327
327
  type: "module",
328
328
  keywords: [
@@ -3469,9 +3469,22 @@ var SessionLogWriter = class {
3469
3469
  }
3470
3470
  try {
3471
3471
  const message = JSON.parse(line);
3472
+ const timestamp = (/* @__PURE__ */ new Date()).toISOString();
3473
+ if (this.isAgentMessageChunk(message)) {
3474
+ const text2 = this.extractChunkText(message);
3475
+ if (text2) {
3476
+ if (!session.chunkBuffer) {
3477
+ session.chunkBuffer = { text: text2, firstTimestamp: timestamp };
3478
+ } else {
3479
+ session.chunkBuffer.text += text2;
3480
+ }
3481
+ }
3482
+ return;
3483
+ }
3484
+ this.emitCoalescedMessage(sessionId, session);
3472
3485
  const entry = {
3473
3486
  type: "notification",
3474
- timestamp: (/* @__PURE__ */ new Date()).toISOString(),
3487
+ timestamp,
3475
3488
  notification: message
3476
3489
  };
3477
3490
  if (session.otelWriter) {
@@ -3493,6 +3506,7 @@ var SessionLogWriter = class {
3493
3506
  async flush(sessionId) {
3494
3507
  const session = this.sessions.get(sessionId);
3495
3508
  if (!session) return;
3509
+ this.emitCoalescedMessage(sessionId, session);
3496
3510
  if (session.otelWriter) {
3497
3511
  await session.otelWriter.flush();
3498
3512
  }
@@ -3514,6 +3528,49 @@ var SessionLogWriter = class {
3514
3528
  this.logger.error("Failed to persist session logs:", error);
3515
3529
  }
3516
3530
  }
3531
+ isAgentMessageChunk(message) {
3532
+ if (message.method !== "session/update") return false;
3533
+ const params = message.params;
3534
+ const update = params?.update;
3535
+ return update?.sessionUpdate === "agent_message_chunk";
3536
+ }
3537
+ extractChunkText(message) {
3538
+ const params = message.params;
3539
+ const update = params?.update;
3540
+ const content = update?.content;
3541
+ if (content?.type === "text" && content.text) {
3542
+ return content.text;
3543
+ }
3544
+ return "";
3545
+ }
3546
+ emitCoalescedMessage(sessionId, session) {
3547
+ if (!session.chunkBuffer) return;
3548
+ const { text: text2, firstTimestamp } = session.chunkBuffer;
3549
+ session.chunkBuffer = void 0;
3550
+ const entry = {
3551
+ type: "notification",
3552
+ timestamp: firstTimestamp,
3553
+ notification: {
3554
+ jsonrpc: "2.0",
3555
+ method: "session/update",
3556
+ params: {
3557
+ update: {
3558
+ sessionUpdate: "agent_message",
3559
+ content: { type: "text", text: text2 }
3560
+ }
3561
+ }
3562
+ }
3563
+ };
3564
+ if (session.otelWriter) {
3565
+ session.otelWriter.emit({ notification: entry });
3566
+ }
3567
+ if (this.posthogAPI) {
3568
+ const pending = this.pendingEntries.get(sessionId) ?? [];
3569
+ pending.push(entry);
3570
+ this.pendingEntries.set(sessionId, pending);
3571
+ this.scheduleFlush(sessionId);
3572
+ }
3573
+ }
3517
3574
  scheduleFlush(sessionId) {
3518
3575
  const existing = this.flushTimeouts.get(sessionId);
3519
3576
  if (existing) clearTimeout(existing);