@posthog/agent 2.1.87 → 2.1.98

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/types.d.ts CHANGED
@@ -88,6 +88,7 @@ interface PostHogAPIConfig {
88
88
  apiUrl: string;
89
89
  getApiKey: () => string;
90
90
  projectId: number;
91
+ userAgent?: string;
91
92
  }
92
93
  interface OtelTransportConfig {
93
94
  /** PostHog ingest host, e.g., "https://us.i.posthog.com" */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@posthog/agent",
3
- "version": "2.1.87",
3
+ "version": "2.1.98",
4
4
  "repository": "https://github.com/PostHog/twig",
5
5
  "description": "TypeScript agent framework wrapping Claude Agent SDK with Git-based task execution for PostHog",
6
6
  "exports": {
@@ -1,3 +1,4 @@
1
+ import packageJson from "../package.json" with { type: "json" };
1
2
  import type {
2
3
  ArtifactType,
3
4
  PostHogAPIConfig,
@@ -10,6 +11,8 @@ import { getLlmGatewayUrl } from "./utils/gateway.js";
10
11
 
11
12
  export { getLlmGatewayUrl };
12
13
 
14
+ const DEFAULT_USER_AGENT = `posthog/agent.hog.dev; version: ${packageJson.version}`;
15
+
13
16
  export interface TaskArtifactUploadPayload {
14
17
  name: string;
15
18
  type: ArtifactType;
@@ -48,6 +51,7 @@ export class PostHogAPIClient {
48
51
  return {
49
52
  Authorization: `Bearer ${this.config.getApiKey()}`,
50
53
  "Content-Type": "application/json",
54
+ "User-Agent": this.config.userAgent ?? DEFAULT_USER_AGENT,
51
55
  };
52
56
  }
53
57
 
@@ -5,6 +5,7 @@ import {
5
5
  } from "@agentclientprotocol/sdk";
6
6
  import { type ServerType, serve } from "@hono/node-server";
7
7
  import { Hono } from "hono";
8
+ import packageJson from "../../package.json" with { type: "json" };
8
9
  import { POSTHOG_NOTIFICATIONS } from "../acp-extensions.js";
9
10
  import {
10
11
  createAcpConnection,
@@ -130,6 +131,7 @@ interface SseController {
130
131
 
131
132
  interface ActiveSession {
132
133
  payload: JwtPayload;
134
+ acpSessionId: string;
133
135
  acpConnection: InProcessAcpConnection;
134
136
  clientConnection: ClientSideConnection;
135
137
  treeTracker: TreeTracker;
@@ -153,6 +155,7 @@ export class AgentServer {
153
155
  apiUrl: config.apiUrl,
154
156
  projectId: config.projectId,
155
157
  getApiKey: () => config.apiKey,
158
+ userAgent: `posthog/cloud.hog.dev; version: ${config.version ?? packageJson.version}`,
156
159
  });
157
160
  this.app = this.createApp();
158
161
  }
@@ -409,7 +412,7 @@ export class AgentServer {
409
412
  );
410
413
 
411
414
  const result = await this.session.clientConnection.prompt({
412
- sessionId: this.session.payload.run_id,
415
+ sessionId: this.session.acpSessionId,
413
416
  prompt: [{ type: "text", text: content }],
414
417
  });
415
418
 
@@ -418,9 +421,11 @@ export class AgentServer {
418
421
 
419
422
  case POSTHOG_NOTIFICATIONS.CANCEL:
420
423
  case "cancel": {
421
- this.logger.info("Cancel requested");
424
+ this.logger.info("Cancel requested", {
425
+ acpSessionId: this.session.acpSessionId,
426
+ });
422
427
  await this.session.clientConnection.cancel({
423
- sessionId: this.session.payload.run_id,
428
+ sessionId: this.session.acpSessionId,
424
429
  });
425
430
  return { cancelled: true };
426
431
  }
@@ -468,6 +473,7 @@ export class AgentServer {
468
473
  apiUrl: this.config.apiUrl,
469
474
  projectId: this.config.projectId,
470
475
  getApiKey: () => this.config.apiKey,
476
+ userAgent: `posthog/cloud.hog.dev; version: ${this.config.version ?? packageJson.version}`,
471
477
  });
472
478
 
473
479
  const logWriter = new SessionLogWriter({
@@ -515,7 +521,7 @@ export class AgentServer {
515
521
  clientCapabilities: {},
516
522
  });
517
523
 
518
- await clientConnection.newSession({
524
+ const sessionResponse = await clientConnection.newSession({
519
525
  cwd: this.config.repositoryPath,
520
526
  mcpServers: [],
521
527
  _meta: {
@@ -525,8 +531,15 @@ export class AgentServer {
525
531
  },
526
532
  });
527
533
 
534
+ const acpSessionId = sessionResponse.sessionId;
535
+ this.logger.info("ACP session created", {
536
+ acpSessionId,
537
+ runId: payload.run_id,
538
+ });
539
+
528
540
  this.session = {
529
541
  payload,
542
+ acpSessionId,
530
543
  acpConnection,
531
544
  clientConnection,
532
545
  treeTracker,
@@ -567,33 +580,19 @@ export class AgentServer {
567
580
  });
568
581
 
569
582
  const result = await this.session.clientConnection.prompt({
570
- sessionId: payload.run_id,
583
+ sessionId: this.session.acpSessionId,
571
584
  prompt: [{ type: "text", text: task.description }],
572
585
  });
573
586
 
574
587
  this.logger.info("Initial task message completed", {
575
588
  stopReason: result.stopReason,
576
589
  });
577
-
578
- // Only auto-complete for background mode
579
- const mode = this.getEffectiveMode(payload);
580
- if (mode === "background") {
581
- // Flush all pending session logs before signaling completion,
582
- await this.session.logWriter.flushAll();
583
- await this.signalTaskComplete(payload, result.stopReason);
584
- } else {
585
- this.logger.info("Interactive mode - staying open for conversation");
586
- }
587
590
  } catch (error) {
588
591
  this.logger.error("Failed to send initial task message", error);
589
- // Signal failure for background mode
590
- const mode = this.getEffectiveMode(payload);
591
- if (mode === "background") {
592
- if (this.session) {
593
- await this.session.logWriter.flushAll();
594
- }
595
- await this.signalTaskComplete(payload, "error");
592
+ if (this.session) {
593
+ await this.session.logWriter.flushAll();
596
594
  }
595
+ await this.signalTaskComplete(payload, "error");
597
596
  }
598
597
  }
599
598
 
@@ -630,12 +629,14 @@ Important:
630
629
  }
631
630
  }
632
631
 
633
- const status =
634
- stopReason === "cancelled"
635
- ? "cancelled"
636
- : stopReason === "error"
637
- ? "failed"
638
- : "completed";
632
+ if (stopReason !== "error") {
633
+ this.logger.info("Skipping status update for non-error stop reason", {
634
+ stopReason,
635
+ });
636
+ return;
637
+ }
638
+
639
+ const status = "failed";
639
640
 
640
641
  try {
641
642
  await this.posthogAPI.updateTaskRun(payload.task_id, payload.run_id, {
@@ -10,4 +10,5 @@ export interface AgentServerConfig {
10
10
  mode: AgentMode;
11
11
  taskId: string;
12
12
  runId: string;
13
+ version?: string;
13
14
  }
package/src/types.ts CHANGED
@@ -126,6 +126,7 @@ export interface PostHogAPIConfig {
126
126
  apiUrl: string;
127
127
  getApiKey: () => string;
128
128
  projectId: number;
129
+ userAgent?: string;
129
130
  }
130
131
 
131
132
  export interface OtelTransportConfig {