@posthog/agent 1.22.0 → 1.24.0

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.
Files changed (118) hide show
  1. package/CLAUDE.md +3 -3
  2. package/README.md +3 -3
  3. package/dist/index.d.ts +11 -11
  4. package/dist/index.d.ts.map +1 -1
  5. package/dist/index.js +3 -3
  6. package/dist/src/adapters/claude/claude-adapter.d.ts +3 -3
  7. package/dist/src/adapters/claude/claude-adapter.d.ts.map +1 -1
  8. package/dist/src/adapters/claude/claude-adapter.js +156 -111
  9. package/dist/src/adapters/claude/claude-adapter.js.map +1 -1
  10. package/dist/src/adapters/claude/tool-mapper.d.ts +1 -1
  11. package/dist/src/adapters/claude/tool-mapper.d.ts.map +1 -1
  12. package/dist/src/adapters/claude/tool-mapper.js.map +1 -1
  13. package/dist/src/adapters/types.d.ts +1 -1
  14. package/dist/src/adapters/types.d.ts.map +1 -1
  15. package/dist/src/agent.d.ts +7 -7
  16. package/dist/src/agent.d.ts.map +1 -1
  17. package/dist/src/agent.js +143 -85
  18. package/dist/src/agent.js.map +1 -1
  19. package/dist/src/agents/execution.js.map +1 -1
  20. package/dist/src/agents/planning.js.map +1 -1
  21. package/dist/src/agents/research.js.map +1 -1
  22. package/dist/src/file-manager.d.ts +4 -4
  23. package/dist/src/file-manager.d.ts.map +1 -1
  24. package/dist/src/file-manager.js +59 -58
  25. package/dist/src/file-manager.js.map +1 -1
  26. package/dist/src/git-manager.d.ts +1 -1
  27. package/dist/src/git-manager.d.ts.map +1 -1
  28. package/dist/src/git-manager.js +93 -69
  29. package/dist/src/git-manager.js.map +1 -1
  30. package/dist/src/posthog-api.d.ts +2 -3
  31. package/dist/src/posthog-api.d.ts.map +1 -1
  32. package/dist/src/posthog-api.js +22 -22
  33. package/dist/src/posthog-api.js.map +1 -1
  34. package/dist/src/prompt-builder.d.ts +3 -3
  35. package/dist/src/prompt-builder.d.ts.map +1 -1
  36. package/dist/src/prompt-builder.js +123 -93
  37. package/dist/src/prompt-builder.js.map +1 -1
  38. package/dist/src/task-manager.d.ts +4 -4
  39. package/dist/src/task-manager.d.ts.map +1 -1
  40. package/dist/src/task-manager.js +19 -18
  41. package/dist/src/task-manager.js.map +1 -1
  42. package/dist/src/task-progress-reporter.d.ts +3 -4
  43. package/dist/src/task-progress-reporter.d.ts.map +1 -1
  44. package/dist/src/task-progress-reporter.js +59 -54
  45. package/dist/src/task-progress-reporter.js.map +1 -1
  46. package/dist/src/template-manager.d.ts +1 -1
  47. package/dist/src/template-manager.d.ts.map +1 -1
  48. package/dist/src/template-manager.js +30 -28
  49. package/dist/src/template-manager.js.map +1 -1
  50. package/dist/src/todo-manager.d.ts +3 -3
  51. package/dist/src/todo-manager.d.ts.map +1 -1
  52. package/dist/src/todo-manager.js +29 -24
  53. package/dist/src/todo-manager.js.map +1 -1
  54. package/dist/src/tools/registry.d.ts +1 -1
  55. package/dist/src/tools/registry.js +60 -60
  56. package/dist/src/tools/registry.js.map +1 -1
  57. package/dist/src/tools/types.d.ts +31 -31
  58. package/dist/src/types.d.ts +33 -33
  59. package/dist/src/types.d.ts.map +1 -1
  60. package/dist/src/types.js.map +1 -1
  61. package/dist/src/utils/logger.d.ts +4 -4
  62. package/dist/src/utils/logger.d.ts.map +1 -1
  63. package/dist/src/utils/logger.js +8 -8
  64. package/dist/src/utils/logger.js.map +1 -1
  65. package/dist/src/workflow/config.d.ts +1 -1
  66. package/dist/src/workflow/config.d.ts.map +1 -1
  67. package/dist/src/workflow/config.js +18 -18
  68. package/dist/src/workflow/config.js.map +1 -1
  69. package/dist/src/workflow/steps/build.d.ts +1 -1
  70. package/dist/src/workflow/steps/build.d.ts.map +1 -1
  71. package/dist/src/workflow/steps/build.js +46 -38
  72. package/dist/src/workflow/steps/build.js.map +1 -1
  73. package/dist/src/workflow/steps/finalize.d.ts +1 -1
  74. package/dist/src/workflow/steps/finalize.d.ts.map +1 -1
  75. package/dist/src/workflow/steps/finalize.js +54 -48
  76. package/dist/src/workflow/steps/finalize.js.map +1 -1
  77. package/dist/src/workflow/steps/plan.d.ts +1 -1
  78. package/dist/src/workflow/steps/plan.d.ts.map +1 -1
  79. package/dist/src/workflow/steps/plan.js +58 -46
  80. package/dist/src/workflow/steps/plan.js.map +1 -1
  81. package/dist/src/workflow/steps/research.d.ts +1 -1
  82. package/dist/src/workflow/steps/research.d.ts.map +1 -1
  83. package/dist/src/workflow/steps/research.js +68 -56
  84. package/dist/src/workflow/steps/research.js.map +1 -1
  85. package/dist/src/workflow/types.d.ts +12 -12
  86. package/dist/src/workflow/types.d.ts.map +1 -1
  87. package/dist/src/workflow/utils.d.ts +1 -1
  88. package/dist/src/workflow/utils.d.ts.map +1 -1
  89. package/dist/src/workflow/utils.js +7 -4
  90. package/dist/src/workflow/utils.js.map +1 -1
  91. package/package.json +8 -8
  92. package/src/adapters/claude/claude-adapter.ts +220 -168
  93. package/src/adapters/claude/tool-mapper.ts +2 -2
  94. package/src/adapters/types.ts +1 -1
  95. package/src/agent.ts +579 -444
  96. package/src/agents/execution.ts +1 -1
  97. package/src/agents/planning.ts +1 -1
  98. package/src/agents/research.ts +0 -1
  99. package/src/file-manager.ts +64 -63
  100. package/src/git-manager.ts +152 -87
  101. package/src/posthog-api.ts +122 -82
  102. package/src/prompt-builder.ts +180 -135
  103. package/src/task-manager.ts +38 -30
  104. package/src/task-progress-reporter.ts +70 -59
  105. package/src/template-manager.ts +98 -45
  106. package/src/todo-manager.ts +35 -30
  107. package/src/tools/registry.ts +62 -62
  108. package/src/tools/types.ts +36 -36
  109. package/src/types.ts +93 -71
  110. package/src/utils/logger.ts +62 -56
  111. package/src/workflow/config.ts +48 -48
  112. package/src/workflow/steps/build.ts +122 -113
  113. package/src/workflow/steps/finalize.ts +214 -182
  114. package/src/workflow/steps/plan.ts +151 -131
  115. package/src/workflow/steps/research.ts +205 -186
  116. package/src/workflow/types.ts +38 -36
  117. package/src/workflow/utils.ts +37 -34
  118. package/LICENSE +0 -33
@@ -9,18 +9,18 @@ async function finalizeStepGitActions(context, step, options) {
9
9
  const { gitManager, logger } = context;
10
10
  const hasStagedChanges = await gitManager.hasStagedChanges();
11
11
  if (!hasStagedChanges && !options.allowEmptyCommit) {
12
- logger.debug('No staged changes to commit for step', { stepId: step.id });
12
+ logger.debug("No staged changes to commit for step", { stepId: step.id });
13
13
  return false;
14
14
  }
15
15
  try {
16
16
  await gitManager.commitChanges(options.commitMessage);
17
- logger.info('Committed changes for step', {
17
+ logger.info("Committed changes for step", {
18
18
  stepId: step.id,
19
19
  message: options.commitMessage,
20
20
  });
21
21
  }
22
22
  catch (error) {
23
- logger.error('Failed to commit changes for step', {
23
+ logger.error("Failed to commit changes for step", {
24
24
  stepId: step.id,
25
25
  error: error instanceof Error ? error.message : String(error),
26
26
  });
@@ -29,7 +29,10 @@ async function finalizeStepGitActions(context, step, options) {
29
29
  if (step.push) {
30
30
  const branchName = await gitManager.getCurrentBranch();
31
31
  await gitManager.pushBranch(branchName);
32
- logger.info('Pushed branch after step', { stepId: step.id, branch: branchName });
32
+ logger.info("Pushed branch after step", {
33
+ stepId: step.id,
34
+ branch: branchName,
35
+ });
33
36
  }
34
37
  return true;
35
38
  }
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","sources":["../../../src/workflow/utils.ts"],"sourcesContent":["import type { WorkflowRuntime, WorkflowStepDefinition } from './types.js';\n\ninterface FinalizeGitOptions {\n commitMessage: string;\n allowEmptyCommit?: boolean;\n}\n\n/**\n * Commits (and optionally pushes) any staged changes according to the step configuration.\n * Returns true if a commit was created.\n */\nexport async function finalizeStepGitActions(\n context: WorkflowRuntime,\n step: WorkflowStepDefinition,\n options: FinalizeGitOptions\n): Promise<boolean> {\n if (!step.commit) {\n return false;\n }\n\n const { gitManager, logger } = context;\n const hasStagedChanges = await gitManager.hasStagedChanges();\n\n if (!hasStagedChanges && !options.allowEmptyCommit) {\n logger.debug('No staged changes to commit for step', { stepId: step.id });\n return false;\n }\n\n try {\n await gitManager.commitChanges(options.commitMessage);\n logger.info('Committed changes for step', {\n stepId: step.id,\n message: options.commitMessage,\n });\n } catch (error) {\n logger.error('Failed to commit changes for step', {\n stepId: step.id,\n error: error instanceof Error ? error.message : String(error),\n });\n throw error;\n }\n\n if (step.push) {\n const branchName = await gitManager.getCurrentBranch();\n await gitManager.pushBranch(branchName);\n logger.info('Pushed branch after step', { stepId: step.id, branch: branchName });\n }\n\n return true;\n}\n"],"names":[],"mappings":"AAOA;;;AAGG;AACI,eAAe,sBAAsB,CACxC,OAAwB,EACxB,IAA4B,EAC5B,OAA2B,EAAA;AAE3B,IAAA,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE;AACd,QAAA,OAAO,KAAK;IAChB;AAEA,IAAA,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,GAAG,OAAO;AACtC,IAAA,MAAM,gBAAgB,GAAG,MAAM,UAAU,CAAC,gBAAgB,EAAE;IAE5D,IAAI,CAAC,gBAAgB,IAAI,CAAC,OAAO,CAAC,gBAAgB,EAAE;AAChD,QAAA,MAAM,CAAC,KAAK,CAAC,sCAAsC,EAAE,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC;AACzE,QAAA,OAAO,KAAK;IAChB;AAEA,IAAA,IAAI;QACA,MAAM,UAAU,CAAC,aAAa,CAAC,OAAO,CAAC,aAAa,CAAC;AACrD,QAAA,MAAM,CAAC,IAAI,CAAC,4BAA4B,EAAE;YACtC,MAAM,EAAE,IAAI,CAAC,EAAE;YACf,OAAO,EAAE,OAAO,CAAC,aAAa;AACjC,SAAA,CAAC;IACN;IAAE,OAAO,KAAK,EAAE;AACZ,QAAA,MAAM,CAAC,KAAK,CAAC,mCAAmC,EAAE;YAC9C,MAAM,EAAE,IAAI,CAAC,EAAE;AACf,YAAA,KAAK,EAAE,KAAK,YAAY,KAAK,GAAG,KAAK,CAAC,OAAO,GAAG,MAAM,CAAC,KAAK,CAAC;AAChE,SAAA,CAAC;AACF,QAAA,MAAM,KAAK;IACf;AAEA,IAAA,IAAI,IAAI,CAAC,IAAI,EAAE;AACX,QAAA,MAAM,UAAU,GAAG,MAAM,UAAU,CAAC,gBAAgB,EAAE;AACtD,QAAA,MAAM,UAAU,CAAC,UAAU,CAAC,UAAU,CAAC;AACvC,QAAA,MAAM,CAAC,IAAI,CAAC,0BAA0B,EAAE,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,EAAE,MAAM,EAAE,UAAU,EAAE,CAAC;IACpF;AAEA,IAAA,OAAO,IAAI;AACf;;;;"}
1
+ {"version":3,"file":"utils.js","sources":["../../../src/workflow/utils.ts"],"sourcesContent":["import type { WorkflowRuntime, WorkflowStepDefinition } from \"./types.js\";\n\ninterface FinalizeGitOptions {\n commitMessage: string;\n allowEmptyCommit?: boolean;\n}\n\n/**\n * Commits (and optionally pushes) any staged changes according to the step configuration.\n * Returns true if a commit was created.\n */\nexport async function finalizeStepGitActions(\n context: WorkflowRuntime,\n step: WorkflowStepDefinition,\n options: FinalizeGitOptions,\n): Promise<boolean> {\n if (!step.commit) {\n return false;\n }\n\n const { gitManager, logger } = context;\n const hasStagedChanges = await gitManager.hasStagedChanges();\n\n if (!hasStagedChanges && !options.allowEmptyCommit) {\n logger.debug(\"No staged changes to commit for step\", { stepId: step.id });\n return false;\n }\n\n try {\n await gitManager.commitChanges(options.commitMessage);\n logger.info(\"Committed changes for step\", {\n stepId: step.id,\n message: options.commitMessage,\n });\n } catch (error) {\n logger.error(\"Failed to commit changes for step\", {\n stepId: step.id,\n error: error instanceof Error ? error.message : String(error),\n });\n throw error;\n }\n\n if (step.push) {\n const branchName = await gitManager.getCurrentBranch();\n await gitManager.pushBranch(branchName);\n logger.info(\"Pushed branch after step\", {\n stepId: step.id,\n branch: branchName,\n });\n }\n\n return true;\n}\n"],"names":[],"mappings":"AAOA;;;AAGG;AACI,eAAe,sBAAsB,CAC1C,OAAwB,EACxB,IAA4B,EAC5B,OAA2B,EAAA;AAE3B,IAAA,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE;AAChB,QAAA,OAAO,KAAK;IACd;AAEA,IAAA,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,GAAG,OAAO;AACtC,IAAA,MAAM,gBAAgB,GAAG,MAAM,UAAU,CAAC,gBAAgB,EAAE;IAE5D,IAAI,CAAC,gBAAgB,IAAI,CAAC,OAAO,CAAC,gBAAgB,EAAE;AAClD,QAAA,MAAM,CAAC,KAAK,CAAC,sCAAsC,EAAE,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC;AACzE,QAAA,OAAO,KAAK;IACd;AAEA,IAAA,IAAI;QACF,MAAM,UAAU,CAAC,aAAa,CAAC,OAAO,CAAC,aAAa,CAAC;AACrD,QAAA,MAAM,CAAC,IAAI,CAAC,4BAA4B,EAAE;YACxC,MAAM,EAAE,IAAI,CAAC,EAAE;YACf,OAAO,EAAE,OAAO,CAAC,aAAa;AAC/B,SAAA,CAAC;IACJ;IAAE,OAAO,KAAK,EAAE;AACd,QAAA,MAAM,CAAC,KAAK,CAAC,mCAAmC,EAAE;YAChD,MAAM,EAAE,IAAI,CAAC,EAAE;AACf,YAAA,KAAK,EAAE,KAAK,YAAY,KAAK,GAAG,KAAK,CAAC,OAAO,GAAG,MAAM,CAAC,KAAK,CAAC;AAC9D,SAAA,CAAC;AACF,QAAA,MAAM,KAAK;IACb;AAEA,IAAA,IAAI,IAAI,CAAC,IAAI,EAAE;AACb,QAAA,MAAM,UAAU,GAAG,MAAM,UAAU,CAAC,gBAAgB,EAAE;AACtD,QAAA,MAAM,UAAU,CAAC,UAAU,CAAC,UAAU,CAAC;AACvC,QAAA,MAAM,CAAC,IAAI,CAAC,0BAA0B,EAAE;YACtC,MAAM,EAAE,IAAI,CAAC,EAAE;AACf,YAAA,MAAM,EAAE,UAAU;AACnB,SAAA,CAAC;IACJ;AAEA,IAAA,OAAO,IAAI;AACb;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@posthog/agent",
3
- "version": "1.22.0",
3
+ "version": "1.24.0",
4
4
  "description": "TypeScript agent framework wrapping Claude Agent SDK with Git-based task execution for PostHog",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",
@@ -22,16 +22,17 @@
22
22
  "typescript"
23
23
  ],
24
24
  "author": "PostHog",
25
- "license": "MIT",
25
+ "license": "SEE LICENSE IN LICENSE",
26
26
  "repository": {
27
27
  "type": "git",
28
28
  "url": "https://github.com/PostHog/posthog-agent"
29
29
  },
30
30
  "scripts": {
31
- "build": "rimraf dist && rollup -c && tsc --project tsconfig.build.json --emitDeclarationOnly",
32
- "example": "bun run example-usage.ts",
33
- "dev": "bun run example-usage.ts",
34
- "prepublishOnly": "bun run build"
31
+ "build": "pnpm exec rimraf dist && pnpm exec rollup -c && pnpm exec tsc --project tsconfig.build.json --emitDeclarationOnly",
32
+ "dev": "pnpm exec rollup -c --watch",
33
+ "typecheck": "pnpm exec tsc --noEmit",
34
+ "example": "pnpm run example-usage.ts",
35
+ "prepublishOnly": "pnpm run build"
35
36
  },
36
37
  "engines": {
37
38
  "node": ">=20.0.0"
@@ -61,6 +62,5 @@
61
62
  ],
62
63
  "publishConfig": {
63
64
  "access": "public"
64
- },
65
- "packageManager": "pnpm@10.14.0+sha512.ad27a79641b49c3e481a16a805baa71817a04bbe06a38d17e60e2eaee83f6a146c6a688125f5792e48dd5ba30e7da52a5cda4c3992b9ccf333f9ce223af84748"
65
+ }
66
66
  }
@@ -1,120 +1,151 @@
1
- import type { AgentEvent, ArtifactEvent, StatusEvent } from '../../types.js';
2
- import type { SDKMessage } from '@anthropic-ai/claude-agent-sdk';
3
- import type { ProviderAdapter } from '../types.js';
4
- import { ClaudeToolMapper } from './tool-mapper.js';
1
+ import type { SDKMessage } from "@anthropic-ai/claude-agent-sdk";
2
+ import type { AgentEvent, ArtifactEvent, StatusEvent } from "../../types.js";
3
+ import type { ProviderAdapter } from "../types.js";
4
+ import { ClaudeToolMapper } from "./tool-mapper.js";
5
5
 
6
6
  /**
7
7
  * Claude provider adapter.
8
8
  * Transforms Claude SDK messages into our standardized AgentEvent format.
9
9
  */
10
10
  export class ClaudeAdapter implements ProviderAdapter {
11
- readonly name = 'claude';
11
+ readonly name = "claude";
12
12
  private toolMapper = new ClaudeToolMapper();
13
13
  createRawSDKEvent(sdkMessage: any): AgentEvent {
14
14
  return {
15
- type: 'raw_sdk_event',
15
+ type: "raw_sdk_event",
16
16
  ts: Date.now(),
17
- sdkMessage
17
+ sdkMessage,
18
18
  };
19
19
  }
20
20
 
21
21
  transform(sdkMessage: SDKMessage): AgentEvent[] {
22
22
  const baseEvent = { ts: Date.now() };
23
23
 
24
- if (sdkMessage.type === 'stream_event') {
24
+ if (sdkMessage.type === "stream_event") {
25
25
  const event = sdkMessage.event;
26
26
 
27
27
  switch (event.type) {
28
- case 'message_start':
29
- return [{
30
- ...baseEvent,
31
- type: 'message_start',
32
- messageId: event.message?.id,
33
- model: event.message?.model
34
- }];
28
+ case "message_start":
29
+ return [
30
+ {
31
+ ...baseEvent,
32
+ type: "message_start",
33
+ messageId: event.message?.id,
34
+ model: event.message?.model,
35
+ },
36
+ ];
35
37
 
36
- case 'content_block_start':
38
+ case "content_block_start": {
37
39
  const contentBlock = event.content_block;
38
40
  if (!contentBlock) return [];
39
41
 
40
- return [{
41
- ...baseEvent,
42
- type: 'content_block_start',
43
- index: event.index,
44
- contentType: contentBlock.type as 'text' | 'tool_use' | 'thinking',
45
- toolName: contentBlock.type === 'tool_use' ? contentBlock.name : undefined,
46
- toolId: contentBlock.type === 'tool_use' ? contentBlock.id : undefined
47
- }];
48
-
49
- case 'content_block_delta':
42
+ return [
43
+ {
44
+ ...baseEvent,
45
+ type: "content_block_start",
46
+ index: event.index,
47
+ contentType: contentBlock.type as
48
+ | "text"
49
+ | "tool_use"
50
+ | "thinking",
51
+ toolName:
52
+ contentBlock.type === "tool_use"
53
+ ? contentBlock.name
54
+ : undefined,
55
+ toolId:
56
+ contentBlock.type === "tool_use" ? contentBlock.id : undefined,
57
+ },
58
+ ];
59
+ }
60
+
61
+ case "content_block_delta": {
50
62
  const delta = event.delta;
51
63
  if (!delta) return [];
52
64
 
53
- if (delta.type === 'text_delta') {
54
- return [{
55
- ...baseEvent,
56
- type: 'token',
57
- content: delta.text,
58
- contentType: 'text'
59
- }];
60
- } else if (delta.type === 'input_json_delta') {
61
- return [{
62
- ...baseEvent,
63
- type: 'token',
64
- content: delta.partial_json,
65
- contentType: 'tool_input'
66
- }];
67
- } else if (delta.type === 'thinking_delta') {
68
- return [{
69
- ...baseEvent,
70
- type: 'token',
71
- content: delta.thinking,
72
- contentType: 'thinking'
73
- }];
65
+ if (delta.type === "text_delta") {
66
+ return [
67
+ {
68
+ ...baseEvent,
69
+ type: "token",
70
+ content: delta.text,
71
+ contentType: "text",
72
+ },
73
+ ];
74
+ } else if (delta.type === "input_json_delta") {
75
+ return [
76
+ {
77
+ ...baseEvent,
78
+ type: "token",
79
+ content: delta.partial_json,
80
+ contentType: "tool_input",
81
+ },
82
+ ];
83
+ } else if (delta.type === "thinking_delta") {
84
+ return [
85
+ {
86
+ ...baseEvent,
87
+ type: "token",
88
+ content: delta.thinking,
89
+ contentType: "thinking",
90
+ },
91
+ ];
74
92
  }
75
93
  return [];
94
+ }
76
95
 
77
- case 'content_block_stop':
78
- return [{
79
- ...baseEvent,
80
- type: 'content_block_stop',
81
- index: event.index
82
- }];
83
-
84
- case 'message_delta':
85
- return [{
86
- ...baseEvent,
87
- type: 'message_delta',
88
- stopReason: event.delta?.stop_reason,
89
- stopSequence: event.delta?.stop_sequence,
90
- usage: event.usage ? {
91
- outputTokens: event.usage.output_tokens
92
- } : undefined
93
- }];
94
-
95
- case 'message_stop':
96
- return [{
97
- ...baseEvent,
98
- type: 'message_stop'
99
- }];
96
+ case "content_block_stop":
97
+ return [
98
+ {
99
+ ...baseEvent,
100
+ type: "content_block_stop",
101
+ index: event.index,
102
+ },
103
+ ];
104
+
105
+ case "message_delta":
106
+ return [
107
+ {
108
+ ...baseEvent,
109
+ type: "message_delta",
110
+ stopReason: event.delta?.stop_reason,
111
+ stopSequence: event.delta?.stop_sequence,
112
+ usage: event.usage
113
+ ? {
114
+ outputTokens: event.usage.output_tokens,
115
+ }
116
+ : undefined,
117
+ },
118
+ ];
119
+
120
+ case "message_stop":
121
+ return [
122
+ {
123
+ ...baseEvent,
124
+ type: "message_stop",
125
+ },
126
+ ];
100
127
 
101
- case 'ping':
128
+ case "ping":
102
129
  // Ignore ping events
103
130
  return [];
104
131
 
105
- case 'error':
106
- return [{
107
- ...baseEvent,
108
- type: 'error',
109
- message: event.error?.message || 'Unknown error',
110
- error: event.error,
111
- errorType: event.error?.type || 'stream_error',
112
- context: event.error ? {
113
- type: event.error.type,
114
- code: event.error.code,
115
- } : undefined,
116
- sdkError: event.error
117
- }];
132
+ case "error":
133
+ return [
134
+ {
135
+ ...baseEvent,
136
+ type: "error",
137
+ message: event.error?.message || "Unknown error",
138
+ error: event.error,
139
+ errorType: event.error?.type || "stream_error",
140
+ context: event.error
141
+ ? {
142
+ type: event.error.type,
143
+ code: event.error.code,
144
+ }
145
+ : undefined,
146
+ sdkError: event.error,
147
+ },
148
+ ];
118
149
 
119
150
  default:
120
151
  return [];
@@ -122,7 +153,7 @@ export class ClaudeAdapter implements ProviderAdapter {
122
153
  }
123
154
 
124
155
  // Handle assistant messages (full message, not streaming)
125
- if (sdkMessage.type === 'assistant') {
156
+ if (sdkMessage.type === "assistant") {
126
157
  const message = sdkMessage.message;
127
158
  const events: AgentEvent[] = [];
128
159
 
@@ -130,15 +161,15 @@ export class ClaudeAdapter implements ProviderAdapter {
130
161
  // A single assistant message can contain multiple tool_use blocks
131
162
  if (message.content && Array.isArray(message.content)) {
132
163
  for (const block of message.content) {
133
- if (block.type === 'tool_use') {
164
+ if (block.type === "tool_use") {
134
165
  // Create tool_call event and enrich with metadata
135
166
  const toolCallEvent = {
136
167
  ...baseEvent,
137
- type: 'tool_call' as const,
168
+ type: "tool_call" as const,
138
169
  toolName: block.name,
139
170
  callId: block.id,
140
171
  args: block.input || {},
141
- parentToolUseId: sdkMessage.parent_tool_use_id
172
+ parentToolUseId: sdkMessage.parent_tool_use_id,
142
173
  };
143
174
  // Enrich with tool metadata and add to events array
144
175
  events.push(this.toolMapper.enrichToolCall(toolCallEvent));
@@ -152,32 +183,34 @@ export class ClaudeAdapter implements ProviderAdapter {
152
183
  }
153
184
 
154
185
  // If no tool calls, emit status event
155
- return [{
156
- ...baseEvent,
157
- type: 'status',
158
- phase: 'assistant_message',
159
- messageId: message.id,
160
- model: message.model
161
- }];
186
+ return [
187
+ {
188
+ ...baseEvent,
189
+ type: "status",
190
+ phase: "assistant_message",
191
+ messageId: message.id,
192
+ model: message.model,
193
+ },
194
+ ];
162
195
  }
163
196
 
164
197
  // Handle user messages
165
- if (sdkMessage.type === 'user') {
198
+ if (sdkMessage.type === "user") {
166
199
  const message = sdkMessage.message;
167
200
  const events: AgentEvent[] = [];
168
201
 
169
202
  // Check for tool results in content blocks, A single user message can contain multiple tool_result blocks
170
- if (message?.content && Array.isArray(message.content)) {
203
+ if (message?.content && Array.isArray(message.content)) {
171
204
  for (const block of message.content) {
172
- if (block.type === 'tool_result') {
205
+ if (block.type === "tool_result") {
173
206
  const toolResultEvent = {
174
207
  ...baseEvent,
175
- type: 'tool_result' as const,
176
- toolName: block.tool_name || 'unknown',
177
- callId: block.tool_use_id || '',
208
+ type: "tool_result" as const,
209
+ toolName: block.tool_name || "unknown",
210
+ callId: block.tool_use_id || "",
178
211
  result: block.content,
179
212
  isError: block.is_error,
180
- parentToolUseId: sdkMessage.parent_tool_use_id
213
+ parentToolUseId: sdkMessage.parent_tool_use_id,
181
214
  };
182
215
  events.push(this.toolMapper.enrichToolResult(toolResultEvent));
183
216
  }
@@ -194,90 +227,100 @@ export class ClaudeAdapter implements ProviderAdapter {
194
227
  if (!textContent) {
195
228
  return [];
196
229
  }
197
- return [{
198
- ...baseEvent,
199
- type: 'user_message',
200
- content: textContent,
201
- isSynthetic: sdkMessage.isSynthetic
202
- }];
230
+ return [
231
+ {
232
+ ...baseEvent,
233
+ type: "user_message",
234
+ content: textContent,
235
+ isSynthetic: sdkMessage.isSynthetic,
236
+ },
237
+ ];
203
238
  }
204
239
 
205
240
  // Handle result messages
206
- if (sdkMessage.type === 'result') {
207
- if (sdkMessage.subtype === 'success') {
208
- return [{
209
- ...baseEvent,
210
- type: 'done',
211
- result: sdkMessage.result,
212
- durationMs: sdkMessage.duration_ms,
213
- durationApiMs: sdkMessage.duration_api_ms,
214
- numTurns: sdkMessage.num_turns,
215
- totalCostUsd: sdkMessage.total_cost_usd,
216
- usage: sdkMessage.usage,
217
- modelUsage: sdkMessage.modelUsage,
218
- permissionDenials: sdkMessage.permission_denials
219
- }];
241
+ if (sdkMessage.type === "result") {
242
+ if (sdkMessage.subtype === "success") {
243
+ return [
244
+ {
245
+ ...baseEvent,
246
+ type: "done",
247
+ result: sdkMessage.result,
248
+ durationMs: sdkMessage.duration_ms,
249
+ durationApiMs: sdkMessage.duration_api_ms,
250
+ numTurns: sdkMessage.num_turns,
251
+ totalCostUsd: sdkMessage.total_cost_usd,
252
+ usage: sdkMessage.usage,
253
+ modelUsage: sdkMessage.modelUsage,
254
+ permissionDenials: sdkMessage.permission_denials,
255
+ },
256
+ ];
220
257
  } else {
221
- return [{
222
- ...baseEvent,
223
- type: 'error',
224
- message: `Execution failed: ${sdkMessage.subtype}`,
225
- error: { subtype: sdkMessage.subtype },
226
- errorType: sdkMessage.subtype || 'result_error',
227
- context: {
228
- subtype: sdkMessage.subtype,
229
- duration_ms: sdkMessage.duration_ms,
230
- num_turns: sdkMessage.num_turns
258
+ return [
259
+ {
260
+ ...baseEvent,
261
+ type: "error",
262
+ message: `Execution failed: ${sdkMessage.subtype}`,
263
+ error: { subtype: sdkMessage.subtype },
264
+ errorType: sdkMessage.subtype || "result_error",
265
+ context: {
266
+ subtype: sdkMessage.subtype,
267
+ duration_ms: sdkMessage.duration_ms,
268
+ num_turns: sdkMessage.num_turns,
269
+ },
270
+ sdkError: sdkMessage,
231
271
  },
232
- sdkError: sdkMessage
233
- }];
272
+ ];
234
273
  }
235
274
  }
236
275
 
237
276
  // Handle system messages
238
- if (sdkMessage.type === 'system') {
239
- if (sdkMessage.subtype === 'init') {
240
- return [{
241
- ...baseEvent,
242
- type: 'init',
243
- model: sdkMessage.model,
244
- tools: sdkMessage.tools,
245
- permissionMode: sdkMessage.permissionMode,
246
- cwd: sdkMessage.cwd,
247
- apiKeySource: sdkMessage.apiKeySource,
248
- agents: sdkMessage.agents,
249
- slashCommands: sdkMessage.slash_commands,
250
- outputStyle: sdkMessage.output_style,
251
- mcpServers: sdkMessage.mcp_servers
252
- }];
253
- } else if (sdkMessage.subtype === 'compact_boundary') {
254
- return [{
255
- ...baseEvent,
256
- type: 'compact_boundary',
257
- trigger: sdkMessage.compact_metadata.trigger,
258
- preTokens: sdkMessage.compact_metadata.pre_tokens
259
- }];
277
+ if (sdkMessage.type === "system") {
278
+ if (sdkMessage.subtype === "init") {
279
+ return [
280
+ {
281
+ ...baseEvent,
282
+ type: "init",
283
+ model: sdkMessage.model,
284
+ tools: sdkMessage.tools,
285
+ permissionMode: sdkMessage.permissionMode,
286
+ cwd: sdkMessage.cwd,
287
+ apiKeySource: sdkMessage.apiKeySource,
288
+ agents: sdkMessage.agents,
289
+ slashCommands: sdkMessage.slash_commands,
290
+ outputStyle: sdkMessage.output_style,
291
+ mcpServers: sdkMessage.mcp_servers,
292
+ },
293
+ ];
294
+ } else if (sdkMessage.subtype === "compact_boundary") {
295
+ return [
296
+ {
297
+ ...baseEvent,
298
+ type: "compact_boundary",
299
+ trigger: sdkMessage.compact_metadata.trigger,
300
+ preTokens: sdkMessage.compact_metadata.pre_tokens,
301
+ },
302
+ ];
260
303
  }
261
304
  }
262
305
 
263
306
  return [];
264
307
  }
265
-
308
+
266
309
  createStatusEvent(phase: string, additionalData?: any): StatusEvent {
267
310
  return {
268
- type: 'status',
311
+ type: "status",
269
312
  ts: Date.now(),
270
313
  phase,
271
- ...additionalData
314
+ ...additionalData,
272
315
  };
273
316
  }
274
317
 
275
318
  createArtifactEvent(kind: string, content: any): ArtifactEvent {
276
319
  return {
277
- type: 'artifact',
320
+ type: "artifact",
278
321
  ts: Date.now(),
279
322
  kind,
280
- content
323
+ content,
281
324
  };
282
325
  }
283
326
 
@@ -286,7 +329,7 @@ export class ClaudeAdapter implements ProviderAdapter {
286
329
  return null;
287
330
  }
288
331
 
289
- if (typeof content === 'string') {
332
+ if (typeof content === "string") {
290
333
  const trimmed = content.trim();
291
334
  return trimmed.length > 0 ? trimmed : null;
292
335
  }
@@ -297,18 +340,20 @@ export class ClaudeAdapter implements ProviderAdapter {
297
340
  const extracted = this.extractUserContent(block);
298
341
  if (extracted) {
299
342
  parts.push(extracted);
300
- } else if (block && typeof block === 'object') {
301
- const candidate = this.extractFromObject(block as Record<string, unknown>);
343
+ } else if (block && typeof block === "object") {
344
+ const candidate = this.extractFromObject(
345
+ block as Record<string, unknown>,
346
+ );
302
347
  if (candidate) {
303
348
  parts.push(candidate);
304
349
  }
305
350
  }
306
351
  }
307
- const text = parts.join('\n').trim();
352
+ const text = parts.join("\n").trim();
308
353
  return text.length > 0 ? text : null;
309
354
  }
310
355
 
311
- if (typeof content === 'object') {
356
+ if (typeof content === "object") {
312
357
  return this.extractFromObject(content as Record<string, unknown>);
313
358
  }
314
359
 
@@ -316,9 +361,16 @@ export class ClaudeAdapter implements ProviderAdapter {
316
361
  }
317
362
 
318
363
  private extractFromObject(value: Record<string, unknown>): string | null {
319
- const preferredKeys = ['text', 'input_text', 'input', 'markdown', 'content', 'message'];
364
+ const preferredKeys = [
365
+ "text",
366
+ "input_text",
367
+ "input",
368
+ "markdown",
369
+ "content",
370
+ "message",
371
+ ];
320
372
  for (const key of preferredKeys) {
321
- if (typeof value[key] === 'string') {
373
+ if (typeof value[key] === "string") {
322
374
  const trimmed = (value[key] as string).trim();
323
375
  if (trimmed.length > 0) {
324
376
  return trimmed;
@@ -1,5 +1,5 @@
1
- import type { ToolCallEvent, ToolResultEvent } from '../../types.js';
2
- import { ToolRegistry } from '../../tools/registry.js';
1
+ import { ToolRegistry } from "../../tools/registry.js";
2
+ import type { ToolCallEvent, ToolResultEvent } from "../../types.js";
3
3
 
4
4
  /**
5
5
  * Maps Claude tool names to our tool type system.
@@ -1,4 +1,4 @@
1
- import type { AgentEvent, StatusEvent, ArtifactEvent } from '../types.js';
1
+ import type { AgentEvent, ArtifactEvent, StatusEvent } from "../types.js";
2
2
 
3
3
  /**
4
4
  * Provider adapter interface for transforming provider-specific messages