@posthog/agent 2.1.113 → 2.1.115

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@posthog/agent",
3
- "version": "2.1.113",
3
+ "version": "2.1.115",
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": {
@@ -71,8 +71,8 @@
71
71
  "tsx": "^4.20.6",
72
72
  "typescript": "^5.5.0",
73
73
  "vitest": "^2.1.8",
74
- "@posthog/shared": "1.0.0",
75
- "@twig/git": "1.0.0"
74
+ "@twig/git": "1.0.0",
75
+ "@posthog/shared": "1.0.0"
76
76
  },
77
77
  "dependencies": {
78
78
  "@agentclientprotocol/sdk": "^0.14.0",
@@ -609,32 +609,56 @@ export function toolUpdateFromToolResult(
609
609
  }
610
610
  }
611
611
 
612
+ function itemToText(item: unknown): string | null {
613
+ if (!item || typeof item !== "object") return null;
614
+ const obj = item as Record<string, unknown>;
615
+ // Standard text block
616
+ if (obj.type === "text" && typeof obj.text === "string") {
617
+ return obj.text;
618
+ }
619
+ // Any other structured object — serialize it
620
+ try {
621
+ return JSON.stringify(obj, null, 2);
622
+ } catch {
623
+ return null;
624
+ }
625
+ }
626
+
612
627
  function toAcpContentUpdate(
613
628
  content: unknown,
614
629
  isError: boolean = false,
615
630
  ): Pick<ToolCallUpdate, "content"> {
616
631
  if (Array.isArray(content) && content.length > 0) {
617
- return {
618
- content: content.map((item) => {
619
- const itemObj = item as { type?: string; text?: string };
620
- if (isError && itemObj.type === "text") {
621
- return {
622
- type: "content" as const,
623
- content: text(`\`\`\`\n${itemObj.text ?? ""}\n\`\`\``),
624
- };
625
- }
626
- return {
627
- type: "content" as const,
628
- content: item as { type: "text"; text: string },
629
- };
630
- }),
631
- };
632
+ const texts: string[] = [];
633
+ for (const item of content) {
634
+ const t = itemToText(item);
635
+ if (t) texts.push(t);
636
+ }
637
+ if (texts.length > 0) {
638
+ const combined = texts.join("\n");
639
+ return {
640
+ content: toolContent()
641
+ .text(isError ? `\`\`\`\n${combined}\n\`\`\`` : combined)
642
+ .build(),
643
+ };
644
+ }
632
645
  } else if (typeof content === "string" && content.length > 0) {
633
646
  return {
634
647
  content: toolContent()
635
648
  .text(isError ? `\`\`\`\n${content}\n\`\`\`` : content)
636
649
  .build(),
637
650
  };
651
+ } else if (content && typeof content === "object") {
652
+ try {
653
+ const json = JSON.stringify(content, null, 2);
654
+ if (json && json !== "{}") {
655
+ return {
656
+ content: toolContent().text(json).build(),
657
+ };
658
+ }
659
+ } catch {
660
+ // ignore serialization errors
661
+ }
638
662
  }
639
663
  return {};
640
664
  }
@@ -651,7 +651,10 @@ Important:
651
651
 
652
652
  private configureEnvironment(): void {
653
653
  const { apiKey, apiUrl, projectId } = this.config;
654
- const gatewayUrl = process.env.LLM_GATEWAY_URL || getLlmGatewayUrl(apiUrl);
654
+ const product =
655
+ this.config.mode === "background" ? "background_agents" : "twig";
656
+ const gatewayUrl =
657
+ process.env.LLM_GATEWAY_URL || getLlmGatewayUrl(apiUrl, product);
655
658
  const openaiBaseUrl = gatewayUrl.endsWith("/v1")
656
659
  ? gatewayUrl
657
660
  : `${gatewayUrl}/v1`;
@@ -1,18 +1,23 @@
1
- export function getLlmGatewayUrl(posthogHost: string): string {
1
+ export type GatewayProduct = "twig" | "background_agents";
2
+
3
+ export function getLlmGatewayUrl(
4
+ posthogHost: string,
5
+ product: GatewayProduct = "twig",
6
+ ): string {
2
7
  const url = new URL(posthogHost);
3
8
  const hostname = url.hostname;
4
9
 
5
10
  // Local development (normalize 127.0.0.1 to localhost)
6
11
  if (hostname === "localhost" || hostname === "127.0.0.1") {
7
- return `${url.protocol}//localhost:3308/twig`;
12
+ return `${url.protocol}//localhost:3308/${product}`;
8
13
  }
9
14
 
10
15
  // Docker containers accessing host
11
16
  if (hostname === "host.docker.internal") {
12
- return `${url.protocol}//host.docker.internal:3308/twig`;
17
+ return `${url.protocol}//host.docker.internal:3308/${product}`;
13
18
  }
14
19
 
15
20
  // Production - extract region from hostname, default to US
16
21
  const region = hostname.match(/^(us|eu)\.posthog\.com$/)?.[1] ?? "us";
17
- return `https://gateway.${region}.posthog.com/twig`;
22
+ return `https://gateway.${region}.posthog.com/${product}`;
18
23
  }