@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.
@@ -1183,7 +1183,7 @@ import { v7 as uuidv7 } from "uuid";
1183
1183
  // package.json
1184
1184
  var package_default = {
1185
1185
  name: "@posthog/agent",
1186
- version: "2.0.2",
1186
+ version: "2.0.3",
1187
1187
  repository: "https://github.com/PostHog/twig",
1188
1188
  description: "TypeScript agent framework wrapping Claude Agent SDK with Git-based task execution for PostHog",
1189
1189
  exports: {
@@ -1229,7 +1229,7 @@ var package_default = {
1229
1229
  }
1230
1230
  },
1231
1231
  bin: {
1232
- "agent-server": "./dist/server/bin.js"
1232
+ "agent-server": "./dist/server/bin.cjs"
1233
1233
  },
1234
1234
  type: "module",
1235
1235
  keywords: [
@@ -4349,9 +4349,22 @@ var SessionLogWriter = class {
4349
4349
  }
4350
4350
  try {
4351
4351
  const message = JSON.parse(line);
4352
+ const timestamp = (/* @__PURE__ */ new Date()).toISOString();
4353
+ if (this.isAgentMessageChunk(message)) {
4354
+ const text2 = this.extractChunkText(message);
4355
+ if (text2) {
4356
+ if (!session.chunkBuffer) {
4357
+ session.chunkBuffer = { text: text2, firstTimestamp: timestamp };
4358
+ } else {
4359
+ session.chunkBuffer.text += text2;
4360
+ }
4361
+ }
4362
+ return;
4363
+ }
4364
+ this.emitCoalescedMessage(sessionId, session);
4352
4365
  const entry = {
4353
4366
  type: "notification",
4354
- timestamp: (/* @__PURE__ */ new Date()).toISOString(),
4367
+ timestamp,
4355
4368
  notification: message
4356
4369
  };
4357
4370
  if (session.otelWriter) {
@@ -4373,6 +4386,7 @@ var SessionLogWriter = class {
4373
4386
  async flush(sessionId) {
4374
4387
  const session = this.sessions.get(sessionId);
4375
4388
  if (!session) return;
4389
+ this.emitCoalescedMessage(sessionId, session);
4376
4390
  if (session.otelWriter) {
4377
4391
  await session.otelWriter.flush();
4378
4392
  }
@@ -4394,6 +4408,49 @@ var SessionLogWriter = class {
4394
4408
  this.logger.error("Failed to persist session logs:", error);
4395
4409
  }
4396
4410
  }
4411
+ isAgentMessageChunk(message) {
4412
+ if (message.method !== "session/update") return false;
4413
+ const params = message.params;
4414
+ const update = params?.update;
4415
+ return update?.sessionUpdate === "agent_message_chunk";
4416
+ }
4417
+ extractChunkText(message) {
4418
+ const params = message.params;
4419
+ const update = params?.update;
4420
+ const content = update?.content;
4421
+ if (content?.type === "text" && content.text) {
4422
+ return content.text;
4423
+ }
4424
+ return "";
4425
+ }
4426
+ emitCoalescedMessage(sessionId, session) {
4427
+ if (!session.chunkBuffer) return;
4428
+ const { text: text2, firstTimestamp } = session.chunkBuffer;
4429
+ session.chunkBuffer = void 0;
4430
+ const entry = {
4431
+ type: "notification",
4432
+ timestamp: firstTimestamp,
4433
+ notification: {
4434
+ jsonrpc: "2.0",
4435
+ method: "session/update",
4436
+ params: {
4437
+ update: {
4438
+ sessionUpdate: "agent_message",
4439
+ content: { type: "text", text: text2 }
4440
+ }
4441
+ }
4442
+ }
4443
+ };
4444
+ if (session.otelWriter) {
4445
+ session.otelWriter.emit({ notification: entry });
4446
+ }
4447
+ if (this.posthogAPI) {
4448
+ const pending = this.pendingEntries.get(sessionId) ?? [];
4449
+ pending.push(entry);
4450
+ this.pendingEntries.set(sessionId, pending);
4451
+ this.scheduleFlush(sessionId);
4452
+ }
4453
+ }
4397
4454
  scheduleFlush(sessionId) {
4398
4455
  const existing = this.flushTimeouts.get(sessionId);
4399
4456
  if (existing) clearTimeout(existing);