Repository navigation
Expand file tree
/
Copy pathcontent.ts
More file actions
269 lines (248 loc) · 6.93 KB
/
Copy pathcontent.ts
File metadata and controls
269 lines (248 loc) · 6.93 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import type { ContentBlock, ContentChunk, ResourceLink, Role } from "@agentclientprotocol/sdk"
import path from "node:path"
import { fileURLToPath, pathToFileURL } from "node:url"
import { SessionV1 } from "@opencode-ai/core/v1/session"
export type PromptPart = SessionV1.TextPartInput | SessionV1.FilePartInput
export type ReplayPart =
| {
type: "text"
text: string
synthetic?: boolean
ignored?: boolean
}
| {
type: "file"
url: string
mime: string
filename?: string
}
| {
type: "reasoning"
text: string
}
export function promptContentToParts(content: readonly ContentBlock[]): PromptPart[] {
return content.flatMap(contentBlockToParts)
}
export function contentBlockToParts(block: ContentBlock): PromptPart[] {
switch (block.type) {
case "text":
return [
{
type: "text",
text: block.text,
...audienceFlags(block.annotations?.audience ?? undefined),
},
]
case "image":
if (block.data) {
return [
{
type: "file",
url: `data:${block.mimeType};base64,${block.data}`,
filename: filenameFromUri(block.uri ?? undefined) ?? "image",
mime: block.mimeType,
},
]
}
if (block.uri?.startsWith("data:")) {
return [
{
type: "file",
url: block.uri,
filename: filenameFromUri(block.uri) ?? "image",
mime: block.mimeType,
},
]
}
if (block.uri?.startsWith("http://") || block.uri?.startsWith("https://")) {
return [
{
type: "file",
url: block.uri,
filename: filenameFromUri(block.uri) ?? "image",
mime: block.mimeType,
},
]
}
return []
case "resource_link":
return [resourceLinkToPart(block)]
case "resource":
if ("text" in block.resource) {
try {
const parsed = new URL(block.resource.uri)
if (parsed.protocol === "file:") {
const line = parsed.hash.match(/^#L(\d+)/)?.[1]
let filepath: string
try {
filepath = fileURLToPath(parsed)
} catch {
filepath = decodeURIComponent(parsed.pathname)
}
if (path.sep === "\\") filepath = filepath.replace(/\\/g, "/")
return [
{
type: "text",
text: `[${filepath}${line ? `:${line}` : ""}]\n${block.resource.text}`,
},
]
}
} catch {}
return [{ type: "text", text: `[${block.resource.uri}]\n${block.resource.text}` }]
}
if (block.resource.mimeType) {
return [
{
type: "file",
url: block.resource.uri.startsWith("data:")
? block.resource.uri
: `data:${block.resource.mimeType};base64,${block.resource.blob}`,
filename: filenameFromUri(block.resource.uri) ?? "file",
mime: block.resource.mimeType,
},
]
}
return []
default:
return []
}
}
export function partsToContentChunks(parts: readonly ReplayPart[]): ContentChunk[] {
return parts.flatMap(partToContentChunks)
}
export function partToContentChunks(part: ReplayPart): ContentChunk[] {
switch (part.type) {
case "text":
if (!part.text) return []
return [
{
content: {
type: "text",
text: part.text,
...partAudience(part),
},
},
]
case "file":
return filePartToContentChunks(part)
case "reasoning":
if (!part.text) return []
return [
{
content: {
type: "text",
text: part.text,
},
},
]
}
}
function resourceLinkToPart(link: ResourceLink): PromptPart {
const parsed = uriToFilePart(link.uri, link.mimeType ?? "text/plain", link.name)
if (parsed.type === "file") return parsed
return { type: "text", text: parsed.text }
}
function uriToFilePart(
uri: string,
mime: string,
filename?: string,
): SessionV1.FilePartInput | SessionV1.TextPartInput {
try {
if (uri.startsWith("file://")) {
return {
type: "file",
url: uri,
filename: filename ?? filenameFromUri(uri) ?? "file",
mime,
}
}
if (uri.startsWith("zed://")) {
const pathname = new URL(uri).searchParams.get("path")
if (pathname) {
return {
type: "file",
url: pathToFileURL(pathname).href,
filename: filename ?? (path.basename(pathname) || "file"),
mime,
}
}
}
return { type: "text", text: uri }
} catch {
return { type: "text", text: uri }
}
}
function filePartToContentChunks(part: Extract<ReplayPart, { type: "file" }>): ContentChunk[] {
if (part.url.startsWith("file://")) {
return [
{
content: {
type: "resource_link",
uri: part.url,
name: part.filename ?? "file",
mimeType: part.mime,
},
},
]
}
if (!part.url.startsWith("data:")) return []
const data = decodeDataUrl(part.url)
if (!data) return []
if (data.mime.startsWith("image/")) {
return [
{
content: {
type: "image",
mimeType: data.mime,
data: data.base64,
uri: pathToFileURL(part.filename ?? "image").href,
},
},
]
}
return [
{
content: {
type: "resource",
resource:
data.mime.startsWith("text/") || data.mime === "application/json"
? {
uri: pathToFileURL(part.filename ?? "file").href,
mimeType: data.mime,
text: Buffer.from(data.base64, "base64").toString("utf8"),
}
: {
uri: pathToFileURL(part.filename ?? "file").href,
mimeType: data.mime,
blob: data.base64,
},
},
},
]
}
function decodeDataUrl(url: string) {
const match = /^data:([^;]+);base64,(.*)$/.exec(url)
if (!match) return
return { mime: match[1], base64: match[2] }
}
function audienceFlags(audience: readonly Role[] | null | undefined) {
if (audience?.length === 1 && audience[0] === "assistant") return { synthetic: true }
if (audience?.length === 1 && audience[0] === "user") return { ignored: true }
return {}
}
function partAudience(part: Extract<ReplayPart, { type: "text" }>) {
const audience: Role[] | undefined = part.synthetic ? ["assistant"] : part.ignored ? ["user"] : undefined
if (!audience) return {}
return { annotations: { audience } }
}
function filenameFromUri(uri: string | undefined) {
if (!uri) return
if (uri.startsWith("data:")) return
try {
const parsed = new URL(uri)
const name = path.basename(parsed.pathname)
return name || undefined
} catch {
return path.basename(uri) || undefined
}
}