@posthog/agent 2.0.2 → 2.1.2

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.1.2",
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: [
@@ -2415,7 +2415,7 @@ var ClaudeAcpAgent = class extends BaseAcpAgent {
2415
2415
  this.checkAuthStatus();
2416
2416
  const meta = params._meta;
2417
2417
  const internalSessionId = uuidv7();
2418
- const permissionMode = "default";
2418
+ const permissionMode = meta?.permissionMode && TWIG_EXECUTION_MODES.includes(meta.permissionMode) ? meta.permissionMode : "default";
2419
2419
  const mcpServers = parseMcpServers(params);
2420
2420
  await fetchMcpToolMetadata(mcpServers, this.logger);
2421
2421
  const options = buildSessionOptions({
@@ -2468,10 +2468,11 @@ var ClaudeAcpAgent = class extends BaseAcpAgent {
2468
2468
  const meta = params._meta;
2469
2469
  const mcpServers = parseMcpServers(params);
2470
2470
  await fetchMcpToolMetadata(mcpServers, this.logger);
2471
+ const permissionMode = meta?.permissionMode && TWIG_EXECUTION_MODES.includes(meta.permissionMode) ? meta.permissionMode : "default";
2471
2472
  const { query: q, session } = await this.initializeQuery({
2472
2473
  internalSessionId,
2473
2474
  cwd: params.cwd,
2474
- permissionMode: "default",
2475
+ permissionMode,
2475
2476
  mcpServers,
2476
2477
  systemPrompt: buildSystemPrompt(meta?.systemPrompt),
2477
2478
  userProvidedOptions: meta?.claudeCode?.options,
@@ -3469,9 +3470,22 @@ var SessionLogWriter = class {
3469
3470
  }
3470
3471
  try {
3471
3472
  const message = JSON.parse(line);
3473
+ const timestamp = (/* @__PURE__ */ new Date()).toISOString();
3474
+ if (this.isAgentMessageChunk(message)) {
3475
+ const text2 = this.extractChunkText(message);
3476
+ if (text2) {
3477
+ if (!session.chunkBuffer) {
3478
+ session.chunkBuffer = { text: text2, firstTimestamp: timestamp };
3479
+ } else {
3480
+ session.chunkBuffer.text += text2;
3481
+ }
3482
+ }
3483
+ return;
3484
+ }
3485
+ this.emitCoalescedMessage(sessionId, session);
3472
3486
  const entry = {
3473
3487
  type: "notification",
3474
- timestamp: (/* @__PURE__ */ new Date()).toISOString(),
3488
+ timestamp,
3475
3489
  notification: message
3476
3490
  };
3477
3491
  if (session.otelWriter) {
@@ -3493,6 +3507,7 @@ var SessionLogWriter = class {
3493
3507
  async flush(sessionId) {
3494
3508
  const session = this.sessions.get(sessionId);
3495
3509
  if (!session) return;
3510
+ this.emitCoalescedMessage(sessionId, session);
3496
3511
  if (session.otelWriter) {
3497
3512
  await session.otelWriter.flush();
3498
3513
  }
@@ -3514,6 +3529,49 @@ var SessionLogWriter = class {
3514
3529
  this.logger.error("Failed to persist session logs:", error);
3515
3530
  }
3516
3531
  }
3532
+ isAgentMessageChunk(message) {
3533
+ if (message.method !== "session/update") return false;
3534
+ const params = message.params;
3535
+ const update = params?.update;
3536
+ return update?.sessionUpdate === "agent_message_chunk";
3537
+ }
3538
+ extractChunkText(message) {
3539
+ const params = message.params;
3540
+ const update = params?.update;
3541
+ const content = update?.content;
3542
+ if (content?.type === "text" && content.text) {
3543
+ return content.text;
3544
+ }
3545
+ return "";
3546
+ }
3547
+ emitCoalescedMessage(sessionId, session) {
3548
+ if (!session.chunkBuffer) return;
3549
+ const { text: text2, firstTimestamp } = session.chunkBuffer;
3550
+ session.chunkBuffer = void 0;
3551
+ const entry = {
3552
+ type: "notification",
3553
+ timestamp: firstTimestamp,
3554
+ notification: {
3555
+ jsonrpc: "2.0",
3556
+ method: "session/update",
3557
+ params: {
3558
+ update: {
3559
+ sessionUpdate: "agent_message",
3560
+ content: { type: "text", text: text2 }
3561
+ }
3562
+ }
3563
+ }
3564
+ };
3565
+ if (session.otelWriter) {
3566
+ session.otelWriter.emit({ notification: entry });
3567
+ }
3568
+ if (this.posthogAPI) {
3569
+ const pending = this.pendingEntries.get(sessionId) ?? [];
3570
+ pending.push(entry);
3571
+ this.pendingEntries.set(sessionId, pending);
3572
+ this.scheduleFlush(sessionId);
3573
+ }
3574
+ }
3517
3575
  scheduleFlush(sessionId) {
3518
3576
  const existing = this.flushTimeouts.get(sessionId);
3519
3577
  if (existing) clearTimeout(existing);