@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.
- package/CLAUDE.md +3 -3
- package/README.md +3 -3
- package/dist/index.d.ts +11 -11
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -3
- package/dist/src/adapters/claude/claude-adapter.d.ts +3 -3
- package/dist/src/adapters/claude/claude-adapter.d.ts.map +1 -1
- package/dist/src/adapters/claude/claude-adapter.js +156 -111
- package/dist/src/adapters/claude/claude-adapter.js.map +1 -1
- package/dist/src/adapters/claude/tool-mapper.d.ts +1 -1
- package/dist/src/adapters/claude/tool-mapper.d.ts.map +1 -1
- package/dist/src/adapters/claude/tool-mapper.js.map +1 -1
- package/dist/src/adapters/types.d.ts +1 -1
- package/dist/src/adapters/types.d.ts.map +1 -1
- package/dist/src/agent.d.ts +7 -7
- package/dist/src/agent.d.ts.map +1 -1
- package/dist/src/agent.js +143 -85
- package/dist/src/agent.js.map +1 -1
- package/dist/src/agents/execution.js.map +1 -1
- package/dist/src/agents/planning.js.map +1 -1
- package/dist/src/agents/research.js.map +1 -1
- package/dist/src/file-manager.d.ts +4 -4
- package/dist/src/file-manager.d.ts.map +1 -1
- package/dist/src/file-manager.js +59 -58
- package/dist/src/file-manager.js.map +1 -1
- package/dist/src/git-manager.d.ts +1 -1
- package/dist/src/git-manager.d.ts.map +1 -1
- package/dist/src/git-manager.js +93 -69
- package/dist/src/git-manager.js.map +1 -1
- package/dist/src/posthog-api.d.ts +2 -3
- package/dist/src/posthog-api.d.ts.map +1 -1
- package/dist/src/posthog-api.js +22 -22
- package/dist/src/posthog-api.js.map +1 -1
- package/dist/src/prompt-builder.d.ts +3 -3
- package/dist/src/prompt-builder.d.ts.map +1 -1
- package/dist/src/prompt-builder.js +123 -93
- package/dist/src/prompt-builder.js.map +1 -1
- package/dist/src/task-manager.d.ts +4 -4
- package/dist/src/task-manager.d.ts.map +1 -1
- package/dist/src/task-manager.js +19 -18
- package/dist/src/task-manager.js.map +1 -1
- package/dist/src/task-progress-reporter.d.ts +3 -4
- package/dist/src/task-progress-reporter.d.ts.map +1 -1
- package/dist/src/task-progress-reporter.js +59 -54
- package/dist/src/task-progress-reporter.js.map +1 -1
- package/dist/src/template-manager.d.ts +1 -1
- package/dist/src/template-manager.d.ts.map +1 -1
- package/dist/src/template-manager.js +30 -28
- package/dist/src/template-manager.js.map +1 -1
- package/dist/src/todo-manager.d.ts +3 -3
- package/dist/src/todo-manager.d.ts.map +1 -1
- package/dist/src/todo-manager.js +29 -24
- package/dist/src/todo-manager.js.map +1 -1
- package/dist/src/tools/registry.d.ts +1 -1
- package/dist/src/tools/registry.js +60 -60
- package/dist/src/tools/registry.js.map +1 -1
- package/dist/src/tools/types.d.ts +31 -31
- package/dist/src/types.d.ts +33 -33
- package/dist/src/types.d.ts.map +1 -1
- package/dist/src/types.js.map +1 -1
- package/dist/src/utils/logger.d.ts +4 -4
- package/dist/src/utils/logger.d.ts.map +1 -1
- package/dist/src/utils/logger.js +8 -8
- package/dist/src/utils/logger.js.map +1 -1
- package/dist/src/workflow/config.d.ts +1 -1
- package/dist/src/workflow/config.d.ts.map +1 -1
- package/dist/src/workflow/config.js +18 -18
- package/dist/src/workflow/config.js.map +1 -1
- package/dist/src/workflow/steps/build.d.ts +1 -1
- package/dist/src/workflow/steps/build.d.ts.map +1 -1
- package/dist/src/workflow/steps/build.js +46 -38
- package/dist/src/workflow/steps/build.js.map +1 -1
- package/dist/src/workflow/steps/finalize.d.ts +1 -1
- package/dist/src/workflow/steps/finalize.d.ts.map +1 -1
- package/dist/src/workflow/steps/finalize.js +54 -48
- package/dist/src/workflow/steps/finalize.js.map +1 -1
- package/dist/src/workflow/steps/plan.d.ts +1 -1
- package/dist/src/workflow/steps/plan.d.ts.map +1 -1
- package/dist/src/workflow/steps/plan.js +58 -46
- package/dist/src/workflow/steps/plan.js.map +1 -1
- package/dist/src/workflow/steps/research.d.ts +1 -1
- package/dist/src/workflow/steps/research.d.ts.map +1 -1
- package/dist/src/workflow/steps/research.js +68 -56
- package/dist/src/workflow/steps/research.js.map +1 -1
- package/dist/src/workflow/types.d.ts +12 -12
- package/dist/src/workflow/types.d.ts.map +1 -1
- package/dist/src/workflow/utils.d.ts +1 -1
- package/dist/src/workflow/utils.d.ts.map +1 -1
- package/dist/src/workflow/utils.js +7 -4
- package/dist/src/workflow/utils.js.map +1 -1
- package/package.json +8 -8
- package/src/adapters/claude/claude-adapter.ts +220 -168
- package/src/adapters/claude/tool-mapper.ts +2 -2
- package/src/adapters/types.ts +1 -1
- package/src/agent.ts +579 -444
- package/src/agents/execution.ts +1 -1
- package/src/agents/planning.ts +1 -1
- package/src/agents/research.ts +0 -1
- package/src/file-manager.ts +64 -63
- package/src/git-manager.ts +152 -87
- package/src/posthog-api.ts +122 -82
- package/src/prompt-builder.ts +180 -135
- package/src/task-manager.ts +38 -30
- package/src/task-progress-reporter.ts +70 -59
- package/src/template-manager.ts +98 -45
- package/src/todo-manager.ts +35 -30
- package/src/tools/registry.ts +62 -62
- package/src/tools/types.ts +36 -36
- package/src/types.ts +93 -71
- package/src/utils/logger.ts +62 -56
- package/src/workflow/config.ts +48 -48
- package/src/workflow/steps/build.ts +122 -113
- package/src/workflow/steps/finalize.ts +214 -182
- package/src/workflow/steps/plan.ts +151 -131
- package/src/workflow/steps/research.ts +205 -186
- package/src/workflow/types.ts +38 -36
- package/src/workflow/utils.ts +37 -34
- 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(
|
|
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(
|
|
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(
|
|
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(
|
|
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
|
|
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.
|
|
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": "
|
|
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
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
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 {
|
|
2
|
-
import type {
|
|
3
|
-
import type { ProviderAdapter } from
|
|
4
|
-
import { ClaudeToolMapper } from
|
|
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 =
|
|
11
|
+
readonly name = "claude";
|
|
12
12
|
private toolMapper = new ClaudeToolMapper();
|
|
13
13
|
createRawSDKEvent(sdkMessage: any): AgentEvent {
|
|
14
14
|
return {
|
|
15
|
-
type:
|
|
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 ===
|
|
24
|
+
if (sdkMessage.type === "stream_event") {
|
|
25
25
|
const event = sdkMessage.event;
|
|
26
26
|
|
|
27
27
|
switch (event.type) {
|
|
28
|
-
case
|
|
29
|
-
return [
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
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
|
|
38
|
+
case "content_block_start": {
|
|
37
39
|
const contentBlock = event.content_block;
|
|
38
40
|
if (!contentBlock) return [];
|
|
39
41
|
|
|
40
|
-
return [
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
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 ===
|
|
54
|
-
return [
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
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
|
|
78
|
-
return [
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
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
|
|
128
|
+
case "ping":
|
|
102
129
|
// Ignore ping events
|
|
103
130
|
return [];
|
|
104
131
|
|
|
105
|
-
case
|
|
106
|
-
return [
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
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 ===
|
|
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 ===
|
|
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:
|
|
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
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
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 ===
|
|
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 ===
|
|
205
|
+
if (block.type === "tool_result") {
|
|
173
206
|
const toolResultEvent = {
|
|
174
207
|
...baseEvent,
|
|
175
|
-
type:
|
|
176
|
-
toolName: block.tool_name ||
|
|
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
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
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 ===
|
|
207
|
-
if (sdkMessage.subtype ===
|
|
208
|
-
return [
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
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
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
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
|
-
|
|
233
|
-
}];
|
|
272
|
+
];
|
|
234
273
|
}
|
|
235
274
|
}
|
|
236
275
|
|
|
237
276
|
// Handle system messages
|
|
238
|
-
if (sdkMessage.type ===
|
|
239
|
-
if (sdkMessage.subtype ===
|
|
240
|
-
return [
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
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:
|
|
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:
|
|
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 ===
|
|
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 ===
|
|
301
|
-
const candidate = this.extractFromObject(
|
|
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(
|
|
352
|
+
const text = parts.join("\n").trim();
|
|
308
353
|
return text.length > 0 ? text : null;
|
|
309
354
|
}
|
|
310
355
|
|
|
311
|
-
if (typeof content ===
|
|
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 = [
|
|
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] ===
|
|
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
|
|
2
|
-
import {
|
|
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.
|
package/src/adapters/types.ts
CHANGED