Skip to content

Commit 904d036

Browse files
committed
Update backfillSampling.ts
1 parent a8affb3 commit 904d036

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

src/examples/backfill/backfillSampling.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import {
1212
Base64ImageSource,
1313
ContentBlock,
1414
ContentBlockParam,
15+
TextBlockParam,
16+
ImageBlockParam,
1517
Tool as ClaudeTool,
1618
ToolChoiceAuto,
1719
ToolChoiceAny,
@@ -41,6 +43,9 @@ import {
4143
JSONRPCNotification,
4244
AssistantMessageContent,
4345
UserMessageContent,
46+
ElicitResult,
47+
ElicitResultSchema,
48+
TextContent,
4449
} from "../../types.js";
4550
import { Transport } from "../../shared/transport.js";
4651

@@ -57,6 +62,9 @@ const isCallToolRequest: (value: unknown) => value is CallToolRequest =
5762
const isElicitRequest: (value: unknown) => value is ElicitRequest =
5863
((value: any) => ElicitRequestSchema.safeParse(value).success) as any;
5964

65+
const isElicitResult: (value: unknown) => value is ElicitResult =
66+
((value: any) => ElicitResultSchema.safeParse(value).success) as any;
67+
6068
const isCreateMessageRequest: (value: unknown) => value is CreateMessageRequest =
6169
((value: any) => CreateMessageRequestSchema.safeParse(value).success) as any;
6270

@@ -115,8 +123,10 @@ function stopReasonToMcp(reason: string | null): CreateMessageResult['stopReason
115123
return 'toolUse';
116124
case 'end_turn':
117125
return 'endTurn';
126+
case null:
127+
return undefined;
118128
default:
119-
return 'other';
129+
throw new Error(`[stopReasonToMcp] Unsupported stop reason: ${reason}`);
120130
}
121131
}
122132

@@ -138,7 +148,23 @@ function contentBlockFromMcp(content: AssistantMessageContent | UserMessageConte
138148
return {
139149
type: 'tool_result',
140150
tool_use_id: content.toolUseId,
141-
content: JSON.stringify(content.content), // TODO
151+
content: content.content.map(c => {
152+
if (c.type === 'text') {
153+
return {type: 'text', text: c.text};
154+
} else if (c.type === 'image') {
155+
return {
156+
type: 'image',
157+
source: {
158+
type: 'base64',
159+
data: c.data,
160+
media_type: c.mimeType as Base64ImageSource['media_type'],
161+
},
162+
};
163+
} else {
164+
throw new Error(`[contentBlockFromMcp] Unsupported content type in tool_result: ${c.type}`);
165+
}
166+
}),
167+
is_error: content.isError,
142168
};
143169
case 'tool_use':
144170
return {

0 commit comments

Comments
 (0)