@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.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { A as AcpConnection, a as AcpConnectionConfig, b as Agent, c as AgentAdapter, C as CodexProcessOptions, I as InProcessAcpConnection, O as OtelLogConfig, d as OtelLogWriter, S as SessionContext, e as SessionLogWriter, f as SessionLogWriterOptions, g as createAcpConnection } from './agent-DBQY1BfC.js';
1
+ export { A as AcpConnection, a as AcpConnectionConfig, b as Agent, c as AgentAdapter, C as CodexProcessOptions, I as InProcessAcpConnection, O as OtelLogConfig, d as OtelLogWriter, S as SessionContext, e as SessionLogWriter, f as SessionLogWriterOptions, g as createAcpConnection } from './agent-kRbaLUfe.js';
2
2
  import { McpServerConfig } from '@anthropic-ai/claude-agent-sdk';
3
3
  import { L as Logger } from './logger-DDBiMOOD.js';
4
4
  export { a as LoggerConfig } from './logger-DDBiMOOD.js';
package/dist/index.js CHANGED
@@ -1174,7 +1174,7 @@ import { v7 as uuidv7 } from "uuid";
1174
1174
  // package.json
1175
1175
  var package_default = {
1176
1176
  name: "@posthog/agent",
1177
- version: "2.0.2",
1177
+ version: "2.0.3",
1178
1178
  repository: "https://github.com/PostHog/twig",
1179
1179
  description: "TypeScript agent framework wrapping Claude Agent SDK with Git-based task execution for PostHog",
1180
1180
  exports: {
@@ -1220,7 +1220,7 @@ var package_default = {
1220
1220
  }
1221
1221
  },
1222
1222
  bin: {
1223
- "agent-server": "./dist/server/bin.js"
1223
+ "agent-server": "./dist/server/bin.cjs"
1224
1224
  },
1225
1225
  type: "module",
1226
1226
  keywords: [
@@ -4375,9 +4375,22 @@ var SessionLogWriter = class {
4375
4375
  }
4376
4376
  try {
4377
4377
  const message = JSON.parse(line);
4378
+ const timestamp = (/* @__PURE__ */ new Date()).toISOString();
4379
+ if (this.isAgentMessageChunk(message)) {
4380
+ const text2 = this.extractChunkText(message);
4381
+ if (text2) {
4382
+ if (!session.chunkBuffer) {
4383
+ session.chunkBuffer = { text: text2, firstTimestamp: timestamp };
4384
+ } else {
4385
+ session.chunkBuffer.text += text2;
4386
+ }
4387
+ }
4388
+ return;
4389
+ }
4390
+ this.emitCoalescedMessage(sessionId, session);
4378
4391
  const entry = {
4379
4392
  type: "notification",
4380
- timestamp: (/* @__PURE__ */ new Date()).toISOString(),
4393
+ timestamp,
4381
4394
  notification: message
4382
4395
  };
4383
4396
  if (session.otelWriter) {
@@ -4399,6 +4412,7 @@ var SessionLogWriter = class {
4399
4412
  async flush(sessionId) {
4400
4413
  const session = this.sessions.get(sessionId);
4401
4414
  if (!session) return;
4415
+ this.emitCoalescedMessage(sessionId, session);
4402
4416
  if (session.otelWriter) {
4403
4417
  await session.otelWriter.flush();
4404
4418
  }
@@ -4420,6 +4434,49 @@ var SessionLogWriter = class {
4420
4434
  this.logger.error("Failed to persist session logs:", error);
4421
4435
  }
4422
4436
  }
4437
+ isAgentMessageChunk(message) {
4438
+ if (message.method !== "session/update") return false;
4439
+ const params = message.params;
4440
+ const update = params?.update;
4441
+ return update?.sessionUpdate === "agent_message_chunk";
4442
+ }
4443
+ extractChunkText(message) {
4444
+ const params = message.params;
4445
+ const update = params?.update;
4446
+ const content = update?.content;
4447
+ if (content?.type === "text" && content.text) {
4448
+ return content.text;
4449
+ }
4450
+ return "";
4451
+ }
4452
+ emitCoalescedMessage(sessionId, session) {
4453
+ if (!session.chunkBuffer) return;
4454
+ const { text: text2, firstTimestamp } = session.chunkBuffer;
4455
+ session.chunkBuffer = void 0;
4456
+ const entry = {
4457
+ type: "notification",
4458
+ timestamp: firstTimestamp,
4459
+ notification: {
4460
+ jsonrpc: "2.0",
4461
+ method: "session/update",
4462
+ params: {
4463
+ update: {
4464
+ sessionUpdate: "agent_message",
4465
+ content: { type: "text", text: text2 }
4466
+ }
4467
+ }
4468
+ }
4469
+ };
4470
+ if (session.otelWriter) {
4471
+ session.otelWriter.emit({ notification: entry });
4472
+ }
4473
+ if (this.posthogAPI) {
4474
+ const pending = this.pendingEntries.get(sessionId) ?? [];
4475
+ pending.push(entry);
4476
+ this.pendingEntries.set(sessionId, pending);
4477
+ this.scheduleFlush(sessionId);
4478
+ }
4479
+ }
4423
4480
  scheduleFlush(sessionId) {
4424
4481
  const existing = this.flushTimeouts.get(sessionId);
4425
4482
  if (existing) clearTimeout(existing);
@@ -10160,6 +10217,18 @@ var ResumeSaga = class extends Saga {
10160
10217
  });
10161
10218
  break;
10162
10219
  }
10220
+ case "agent_message": {
10221
+ const content = update.content;
10222
+ if (content) {
10223
+ if (content.type === "text" && currentAssistantContent.length > 0 && currentAssistantContent[currentAssistantContent.length - 1].type === "text") {
10224
+ const lastBlock = currentAssistantContent[currentAssistantContent.length - 1];
10225
+ lastBlock.text += content.text;
10226
+ } else {
10227
+ currentAssistantContent.push(content);
10228
+ }
10229
+ }
10230
+ break;
10231
+ }
10163
10232
  case "agent_message_chunk": {
10164
10233
  const content = update.content;
10165
10234
  if (content) {