forked from RooCodeInc/Roo-Code
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathsimple-format.ts
More file actions
58 lines (55 loc) · 1.37 KB
/
simple-format.ts
File metadata and controls
58 lines (55 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { Anthropic } from "@anthropic-ai/sdk"
/**
* Convert complex content blocks to simple string content
*/
export function convertToSimpleContent(content: Anthropic.Messages.MessageParam["content"]): string {
if (typeof content === "string") {
return content
}
// Extract text from content blocks
return content
.map((block) => {
if (block.type === "text") {
return block.text
}
if (block.type === "image") {
return `[Image: ${block.source.media_type}]`
}
if (block.type === "tool_use") {
return `[Tool Use: ${block.name}]`
}
if (block.type === "tool_result") {
if (typeof block.content === "string") {
return block.content
}
if (Array.isArray(block.content)) {
return block.content
.map((part) => {
if (part.type === "text") {
return part.text
}
if (part.type === "image") {
return `[Image: ${part.source.media_type}]`
}
return ""
})
.join("\n")
}
return ""
}
return ""
})
.filter(Boolean)
.join("\n")
}
/**
* Convert Anthropic messages to simple format with string content
*/
export function convertToSimpleMessages(
messages: Anthropic.Messages.MessageParam[],
): Array<{ role: "user" | "assistant"; content: string }> {
return messages.map((message) => ({
role: message.role,
content: convertToSimpleContent(message.content),
}))
}