Skip to content

Commit c369b75

Browse files
authored
Merge branch 'main' into fix/sidebar-shelf-chevron-direction
2 parents 33fd354 + 1e7c8e0 commit c369b75

8 files changed

Lines changed: 297 additions & 3 deletions

File tree

‎apps/server/scripts/record-claude-agent-sdk-replay-fixture.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ import {
7070
import { CLAUDE_BACKGROUND_MONITOR_WAKE_PROMPT } from "../src/orchestration-v2/testkit/fixtures/claude_background_monitor_wake/input.ts";
7171
import { CLAUDE_NESTED_BACKGROUND_SUBAGENT_WAKE_PROMPT } from "../src/orchestration-v2/testkit/fixtures/claude_nested_background_subagent_wake/input.ts";
7272
import { CLAUDE_NESTED_SUBAGENT_MODEL_PROMPT } from "../src/orchestration-v2/testkit/fixtures/claude_nested_subagent_model/input.ts";
73+
import { CLAUDE_MCP_TOOL_PRESENTATION_PROMPT } from "../src/orchestration-v2/testkit/fixtures/claude_mcp_tool_presentation/input.ts";
7374
import { CLAUDE_BACKGROUND_TASK_INTERRUPT_PROMPT } from "../src/orchestration-v2/testkit/fixtures/claude_background_task_interrupt/input.ts";
7475
import { CLAUDE_BACKGROUND_WAKE_BEFORE_QUEUED_PROMPT_LAUNCH_PROMPT } from "../src/orchestration-v2/testkit/fixtures/claude_background_wake_before_queued_prompt/input.ts";
7576
import {
@@ -253,6 +254,14 @@ const CLAUDE_RECORDINGS = {
253254
queryMode: "streaming",
254255
enableTools: true,
255256
},
257+
// Needs the claude.ai Firecrawl connector on the recording account. Claude
258+
// Code describes MCP tool uses in an undeclared `tool_use_meta` field.
259+
claude_mcp_tool_presentation: {
260+
prompts: [CLAUDE_MCP_TOOL_PRESENTATION_PROMPT],
261+
defaultTranscriptFile: "fixtures/claude_mcp_tool_presentation/claude_transcript.ndjson",
262+
queryMode: "streaming",
263+
enableTools: true,
264+
},
256265
subagent: {
257266
prompts: [SUBAGENT_PROMPT],
258267
defaultTranscriptFile: "fixtures/subagent/claude_transcript.ndjson",

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.testkit.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,11 @@ function sanitizeSdkMessageForReplay(input: {
11161116
session_id: message.session_id,
11171117
};
11181118
}
1119+
// Like init's slash_commands, the recording account's commands and skills
1120+
// are local configuration, not protocol.
1121+
if (message.type === "system" && message.subtype === "commands_changed") {
1122+
return { ...message, commands: [] };
1123+
}
11191124
if (message.type === "rate_limit_event") {
11201125
return {
11211126
...message,

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts‎

Lines changed: 105 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import {
6363
type ProviderUserInputAnswers,
6464
type ProviderThreadId,
6565
type ThreadId,
66+
type ToolActivitySource,
6667
} from "@t3tools/contracts";
6768

6869
import * as Cause from "effect/Cause";
@@ -2016,6 +2017,66 @@ function claudeToolUseBlocksFromAssistantMessage(
20162017
return message.message.content.filter(isClaudeToolUseContentBlock);
20172018
}
20182019

2020+
interface ClaudeToolPresentation {
2021+
readonly title: string;
2022+
readonly toolSource?: ToolActivitySource;
2023+
}
2024+
2025+
function boundedMetaText(value: unknown, maxLength: number): string | undefined {
2026+
if (typeof value !== "string") return undefined;
2027+
const text = value.trim().replace(/\s+/gu, " ");
2028+
return text.length > 0 && text.length <= maxLength ? text : undefined;
2029+
}
2030+
2031+
function metaHttpUrl(value: unknown): string | undefined {
2032+
if (typeof value !== "string" || value.length > 4096) return undefined;
2033+
try {
2034+
const url = new URL(value);
2035+
return (url.protocol === "https:" || url.protocol === "http:") && url.href.length <= 4096
2036+
? url.href
2037+
: undefined;
2038+
} catch {
2039+
return undefined;
2040+
}
2041+
}
2042+
2043+
/**
2044+
* Claude Code's own label for each tool use (the MCP tool's title, or its
2045+
* humanized name) plus the server's name and icon. The CLI sends these as
2046+
* `tool_use_meta` on assistant frames; the SDK types do not declare it yet.
2047+
*/
2048+
function claudeToolPresentationsFromAssistantMessage(
2049+
message: SDKMessage,
2050+
): ReadonlyMap<string, ClaudeToolPresentation> {
2051+
const presentations = new Map<string, ClaudeToolPresentation>();
2052+
const meta = message.type === "assistant" ? Reflect.get(message, "tool_use_meta") : undefined;
2053+
if (!Array.isArray(meta)) return presentations;
2054+
for (const entry of meta) {
2055+
if (typeof entry !== "object" || entry === null) continue;
2056+
const id = boundedMetaText(Reflect.get(entry, "id"), 512);
2057+
const title = boundedMetaText(Reflect.get(entry, "display_name"), 160);
2058+
if (id === undefined || title === undefined) continue;
2059+
const serverName = boundedMetaText(Reflect.get(entry, "server_display_name"), 160);
2060+
const iconUrl = metaHttpUrl(Reflect.get(entry, "icon_url"));
2061+
presentations.set(id, {
2062+
title,
2063+
...(serverName === undefined
2064+
? {}
2065+
: {
2066+
toolSource: {
2067+
key: `mcp:${serverName.toLowerCase()}`,
2068+
name: serverName,
2069+
kind: "integration",
2070+
...(iconUrl === undefined
2071+
? {}
2072+
: { icon: { _tag: "themed-logo", logoUrl: iconUrl } as const }),
2073+
},
2074+
}),
2075+
});
2076+
}
2077+
return presentations;
2078+
}
2079+
20192080
function claudeToolResultBlocksFromAssistantMessage(
20202081
message: SDKMessage,
20212082
): ReadonlyArray<ClaudeToolResultContentBlock> {
@@ -2619,6 +2680,7 @@ interface ActiveClaudeToolCall {
26192680
readonly parentNodeId: OrchestrationV2ExecutionNode["id"];
26202681
readonly ordinal: number;
26212682
readonly startedAt: DateTime.Utc;
2683+
readonly presentation?: ClaudeToolPresentation;
26222684
}
26232685

26242686
// What is known about a subagent before its task_started registers it,
@@ -3588,6 +3650,7 @@ export function makeClaudeAdapterV2(
35883650
>;
35893651
readonly startedAt: DateTime.Utc;
35903652
readonly updatedAt: DateTime.Utc;
3653+
readonly presentation: ClaudeToolPresentation | undefined;
35913654
}) => {
35923655
const completedAt = input.status === "running" ? null : input.updatedAt;
35933656
const nodeId = idAllocator.derive.nodeFromProviderItem({
@@ -3645,7 +3708,10 @@ export function makeClaudeAdapterV2(
36453708
parentItemId: null,
36463709
ordinal: input.ordinal,
36473710
status: input.status,
3648-
title: readPath !== undefined ? formatReadToolLabel(readPath) : (searchTitle ?? null),
3711+
title:
3712+
readPath !== undefined
3713+
? formatReadToolLabel(readPath)
3714+
: (searchTitle ?? input.presentation?.title ?? null),
36493715
startedAt: input.startedAt,
36503716
completedAt,
36513717
updatedAt: input.updatedAt,
@@ -3708,6 +3774,9 @@ export function makeClaudeAdapterV2(
37083774
: {
37093775
...itemBase,
37103776
type: "dynamic_tool",
3777+
...(input.presentation?.toolSource === undefined
3778+
? {}
3779+
: { toolSource: input.presentation.toolSource }),
37113780
toolName: input.toolName,
37123781
...(viewedImagePath === undefined ? {} : { viewedImagePath }),
37133782
input: claudeNativeToolInputValue(input.toolInput),
@@ -4378,10 +4447,38 @@ export function makeClaudeAdapterV2(
43784447
readonly toolName: string;
43794448
readonly toolInput: ClaudeNativeToolInput;
43804449
readonly parentToolUseId: string | null;
4450+
readonly presentation?: ClaudeToolPresentation | undefined;
43814451
}) {
43824452
const existing = input.context.toolCalls.get(input.nativeItemId);
43834453
if (existing !== undefined) {
4384-
return existing;
4454+
// The permission callback can start a call before its assistant
4455+
// frame arrives with the tool's display name and icon.
4456+
if (input.presentation === undefined || existing.presentation !== undefined) {
4457+
return existing;
4458+
}
4459+
const presented = { ...existing, presentation: input.presentation };
4460+
input.context.toolCalls.set(input.nativeItemId, presented);
4461+
const updatedAt = yield* DateTime.now;
4462+
yield* emitToolCallArtifacts(
4463+
buildToolCallArtifacts({
4464+
context: input.context,
4465+
nativeItemId: presented.nativeItemId,
4466+
toolName: presented.toolName,
4467+
classification: presented.classification,
4468+
toolInput: presented.input,
4469+
threadId: presented.threadId,
4470+
runId: presented.runId,
4471+
rootNodeId: presented.rootNodeId,
4472+
parentNodeId: presented.parentNodeId,
4473+
ordinal: presented.ordinal,
4474+
output: NO_CLAUDE_NATIVE_TOOL_OUTPUT,
4475+
status: "running",
4476+
startedAt: presented.startedAt,
4477+
updatedAt,
4478+
presentation: input.presentation,
4479+
}),
4480+
);
4481+
return presented;
43854482
}
43864483
const startedAt = yield* DateTime.now;
43874484
const classification = classifyClaudeNativeTool(input.toolName);
@@ -4408,6 +4505,7 @@ export function makeClaudeAdapterV2(
44084505
parentNodeId,
44094506
ordinal,
44104507
startedAt,
4508+
...(input.presentation === undefined ? {} : { presentation: input.presentation }),
44114509
};
44124510
input.context.toolCalls.set(input.nativeItemId, toolCall);
44134511
yield* emitToolCallArtifacts(
@@ -4426,6 +4524,7 @@ export function makeClaudeAdapterV2(
44264524
status: "running",
44274525
startedAt,
44284526
updatedAt: startedAt,
4527+
presentation: input.presentation,
44294528
}),
44304529
);
44314530
return toolCall;
@@ -4614,6 +4713,7 @@ export function makeClaudeAdapterV2(
46144713
status: "failed",
46154714
startedAt: toolCall.startedAt,
46164715
updatedAt: input.completedAt,
4716+
presentation: toolCall.presentation,
46174717
});
46184718
yield* emitToolCallArtifacts(artifacts);
46194719
}
@@ -5816,6 +5916,7 @@ export function makeClaudeAdapterV2(
58165916
}
58175917
}
58185918

5919+
const toolPresentations = claudeToolPresentationsFromAssistantMessage(message);
58195920
for (const toolUse of claudeToolUseBlocksFromAssistantMessage(message)) {
58205921
const nativeToolInput = claudeNativeToolInputFromUnknown(toolUse.input);
58215922
if (toolUse.name === "Agent") {
@@ -5841,6 +5942,7 @@ export function makeClaudeAdapterV2(
58415942
toolName: toolUse.name,
58425943
toolInput: nativeToolInput,
58435944
parentToolUseId: parentToolUseIdFromSdkMessage(message),
5945+
presentation: toolPresentations.get(toolUse.id),
58445946
});
58455947
const heldPlan = heldProposedPlansByToolUseId.get(toolUse.id);
58465948
if (heldPlan !== undefined) {
@@ -5904,6 +6006,7 @@ export function makeClaudeAdapterV2(
59046006
status: isClaudeToolResultError(toolResult) ? "failed" : "completed",
59056007
startedAt: toolCall.startedAt,
59066008
updatedAt: completedAt,
6009+
presentation: toolCall.presentation,
59076010
});
59086011
yield* emitToolCallArtifacts(artifacts);
59096012
context.toolCalls.delete(toolCall.nativeItemId);

‎apps/server/src/orchestration-v2/testkit/ClaudeReplayFixtures.integration.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,8 @@ describe("Claude Agent SDK replay fixtures", () => {
211211
for (const toolName of claudeToolUseNamesFromTranscript(transcript)) {
212212
seenToolNames.add(toolName);
213213
const classification = classifyClaudeNativeTool(toolName);
214-
if (!classification.known) {
214+
// MCP tools are open-ended and deliberately become dynamic tools.
215+
if (!classification.known && !toolName.startsWith("mcp__")) {
215216
unknownToolNames.add(`${fixture.name}:${toolName}`);
216217
}
217218
}

0 commit comments

Comments
 (0)