@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.
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.1.2",
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: [
@@ -3321,7 +3321,7 @@ var ClaudeAcpAgent = class extends BaseAcpAgent {
3321
3321
  this.checkAuthStatus();
3322
3322
  const meta = params._meta;
3323
3323
  const internalSessionId = uuidv7();
3324
- const permissionMode = "default";
3324
+ const permissionMode = meta?.permissionMode && TWIG_EXECUTION_MODES.includes(meta.permissionMode) ? meta.permissionMode : "default";
3325
3325
  const mcpServers = parseMcpServers(params);
3326
3326
  await fetchMcpToolMetadata(mcpServers, this.logger);
3327
3327
  const options = buildSessionOptions({
@@ -3374,10 +3374,11 @@ var ClaudeAcpAgent = class extends BaseAcpAgent {
3374
3374
  const meta = params._meta;
3375
3375
  const mcpServers = parseMcpServers(params);
3376
3376
  await fetchMcpToolMetadata(mcpServers, this.logger);
3377
+ const permissionMode = meta?.permissionMode && TWIG_EXECUTION_MODES.includes(meta.permissionMode) ? meta.permissionMode : "default";
3377
3378
  const { query: q, session } = await this.initializeQuery({
3378
3379
  internalSessionId,
3379
3380
  cwd: params.cwd,
3380
- permissionMode: "default",
3381
+ permissionMode,
3381
3382
  mcpServers,
3382
3383
  systemPrompt: buildSystemPrompt(meta?.systemPrompt),
3383
3384
  userProvidedOptions: meta?.claudeCode?.options,
@@ -4375,9 +4376,22 @@ var SessionLogWriter = class {
4375
4376
  }
4376
4377
  try {
4377
4378
  const message = JSON.parse(line);
4379
+ const timestamp = (/* @__PURE__ */ new Date()).toISOString();
4380
+ if (this.isAgentMessageChunk(message)) {
4381
+ const text2 = this.extractChunkText(message);
4382
+ if (text2) {
4383
+ if (!session.chunkBuffer) {
4384
+ session.chunkBuffer = { text: text2, firstTimestamp: timestamp };
4385
+ } else {
4386
+ session.chunkBuffer.text += text2;
4387
+ }
4388
+ }
4389
+ return;
4390
+ }
4391
+ this.emitCoalescedMessage(sessionId, session);
4378
4392
  const entry = {
4379
4393
  type: "notification",
4380
- timestamp: (/* @__PURE__ */ new Date()).toISOString(),
4394
+ timestamp,
4381
4395
  notification: message
4382
4396
  };
4383
4397
  if (session.otelWriter) {
@@ -4399,6 +4413,7 @@ var SessionLogWriter = class {
4399
4413
  async flush(sessionId) {
4400
4414
  const session = this.sessions.get(sessionId);
4401
4415
  if (!session) return;
4416
+ this.emitCoalescedMessage(sessionId, session);
4402
4417
  if (session.otelWriter) {
4403
4418
  await session.otelWriter.flush();
4404
4419
  }
@@ -4420,6 +4435,49 @@ var SessionLogWriter = class {
4420
4435
  this.logger.error("Failed to persist session logs:", error);
4421
4436
  }
4422
4437
  }
4438
+ isAgentMessageChunk(message) {
4439
+ if (message.method !== "session/update") return false;
4440
+ const params = message.params;
4441
+ const update = params?.update;
4442
+ return update?.sessionUpdate === "agent_message_chunk";
4443
+ }
4444
+ extractChunkText(message) {
4445
+ const params = message.params;
4446
+ const update = params?.update;
4447
+ const content = update?.content;
4448
+ if (content?.type === "text" && content.text) {
4449
+ return content.text;
4450
+ }
4451
+ return "";
4452
+ }
4453
+ emitCoalescedMessage(sessionId, session) {
4454
+ if (!session.chunkBuffer) return;
4455
+ const { text: text2, firstTimestamp } = session.chunkBuffer;
4456
+ session.chunkBuffer = void 0;
4457
+ const entry = {
4458
+ type: "notification",
4459
+ timestamp: firstTimestamp,
4460
+ notification: {
4461
+ jsonrpc: "2.0",
4462
+ method: "session/update",
4463
+ params: {
4464
+ update: {
4465
+ sessionUpdate: "agent_message",
4466
+ content: { type: "text", text: text2 }
4467
+ }
4468
+ }
4469
+ }
4470
+ };
4471
+ if (session.otelWriter) {
4472
+ session.otelWriter.emit({ notification: entry });
4473
+ }
4474
+ if (this.posthogAPI) {
4475
+ const pending = this.pendingEntries.get(sessionId) ?? [];
4476
+ pending.push(entry);
4477
+ this.pendingEntries.set(sessionId, pending);
4478
+ this.scheduleFlush(sessionId);
4479
+ }
4480
+ }
4423
4481
  scheduleFlush(sessionId) {
4424
4482
  const existing = this.flushTimeouts.get(sessionId);
4425
4483
  if (existing) clearTimeout(existing);
@@ -10160,6 +10218,18 @@ var ResumeSaga = class extends Saga {
10160
10218
  });
10161
10219
  break;
10162
10220
  }
10221
+ case "agent_message": {
10222
+ const content = update.content;
10223
+ if (content) {
10224
+ if (content.type === "text" && currentAssistantContent.length > 0 && currentAssistantContent[currentAssistantContent.length - 1].type === "text") {
10225
+ const lastBlock = currentAssistantContent[currentAssistantContent.length - 1];
10226
+ lastBlock.text += content.text;
10227
+ } else {
10228
+ currentAssistantContent.push(content);
10229
+ }
10230
+ }
10231
+ break;
10232
+ }
10163
10233
  case "agent_message_chunk": {
10164
10234
  const content = update.content;
10165
10235
  if (content) {