@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.
@@ -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.1.2",
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: [
@@ -3295,7 +3295,7 @@ var ClaudeAcpAgent = class extends BaseAcpAgent {
3295
3295
  this.checkAuthStatus();
3296
3296
  const meta = params._meta;
3297
3297
  const internalSessionId = uuidv7();
3298
- const permissionMode = "default";
3298
+ const permissionMode = meta?.permissionMode && TWIG_EXECUTION_MODES.includes(meta.permissionMode) ? meta.permissionMode : "default";
3299
3299
  const mcpServers = parseMcpServers(params);
3300
3300
  await fetchMcpToolMetadata(mcpServers, this.logger);
3301
3301
  const options = buildSessionOptions({
@@ -3348,10 +3348,11 @@ var ClaudeAcpAgent = class extends BaseAcpAgent {
3348
3348
  const meta = params._meta;
3349
3349
  const mcpServers = parseMcpServers(params);
3350
3350
  await fetchMcpToolMetadata(mcpServers, this.logger);
3351
+ const permissionMode = meta?.permissionMode && TWIG_EXECUTION_MODES.includes(meta.permissionMode) ? meta.permissionMode : "default";
3351
3352
  const { query: q, session } = await this.initializeQuery({
3352
3353
  internalSessionId,
3353
3354
  cwd: params.cwd,
3354
- permissionMode: "default",
3355
+ permissionMode,
3355
3356
  mcpServers,
3356
3357
  systemPrompt: buildSystemPrompt(meta?.systemPrompt),
3357
3358
  userProvidedOptions: meta?.claudeCode?.options,
@@ -4349,9 +4350,22 @@ var SessionLogWriter = class {
4349
4350
  }
4350
4351
  try {
4351
4352
  const message = JSON.parse(line);
4353
+ const timestamp = (/* @__PURE__ */ new Date()).toISOString();
4354
+ if (this.isAgentMessageChunk(message)) {
4355
+ const text2 = this.extractChunkText(message);
4356
+ if (text2) {
4357
+ if (!session.chunkBuffer) {
4358
+ session.chunkBuffer = { text: text2, firstTimestamp: timestamp };
4359
+ } else {
4360
+ session.chunkBuffer.text += text2;
4361
+ }
4362
+ }
4363
+ return;
4364
+ }
4365
+ this.emitCoalescedMessage(sessionId, session);
4352
4366
  const entry = {
4353
4367
  type: "notification",
4354
- timestamp: (/* @__PURE__ */ new Date()).toISOString(),
4368
+ timestamp,
4355
4369
  notification: message
4356
4370
  };
4357
4371
  if (session.otelWriter) {
@@ -4373,6 +4387,7 @@ var SessionLogWriter = class {
4373
4387
  async flush(sessionId) {
4374
4388
  const session = this.sessions.get(sessionId);
4375
4389
  if (!session) return;
4390
+ this.emitCoalescedMessage(sessionId, session);
4376
4391
  if (session.otelWriter) {
4377
4392
  await session.otelWriter.flush();
4378
4393
  }
@@ -4394,6 +4409,49 @@ var SessionLogWriter = class {
4394
4409
  this.logger.error("Failed to persist session logs:", error);
4395
4410
  }
4396
4411
  }
4412
+ isAgentMessageChunk(message) {
4413
+ if (message.method !== "session/update") return false;
4414
+ const params = message.params;
4415
+ const update = params?.update;
4416
+ return update?.sessionUpdate === "agent_message_chunk";
4417
+ }
4418
+ extractChunkText(message) {
4419
+ const params = message.params;
4420
+ const update = params?.update;
4421
+ const content = update?.content;
4422
+ if (content?.type === "text" && content.text) {
4423
+ return content.text;
4424
+ }
4425
+ return "";
4426
+ }
4427
+ emitCoalescedMessage(sessionId, session) {
4428
+ if (!session.chunkBuffer) return;
4429
+ const { text: text2, firstTimestamp } = session.chunkBuffer;
4430
+ session.chunkBuffer = void 0;
4431
+ const entry = {
4432
+ type: "notification",
4433
+ timestamp: firstTimestamp,
4434
+ notification: {
4435
+ jsonrpc: "2.0",
4436
+ method: "session/update",
4437
+ params: {
4438
+ update: {
4439
+ sessionUpdate: "agent_message",
4440
+ content: { type: "text", text: text2 }
4441
+ }
4442
+ }
4443
+ }
4444
+ };
4445
+ if (session.otelWriter) {
4446
+ session.otelWriter.emit({ notification: entry });
4447
+ }
4448
+ if (this.posthogAPI) {
4449
+ const pending = this.pendingEntries.get(sessionId) ?? [];
4450
+ pending.push(entry);
4451
+ this.pendingEntries.set(sessionId, pending);
4452
+ this.scheduleFlush(sessionId);
4453
+ }
4454
+ }
4397
4455
  scheduleFlush(sessionId) {
4398
4456
  const existing = this.flushTimeouts.get(sessionId);
4399
4457
  if (existing) clearTimeout(existing);