(block: ContentBlock)
| 28 | } |
| 29 | |
| 30 | export function contentBlockToParts(block: ContentBlock): PromptPart[] { |
| 31 | switch (block.type) { |
| 32 | case "text": |
| 33 | return [ |
| 34 | { |
| 35 | type: "text", |
| 36 | text: block.text, |
| 37 | ...audienceFlags(block.annotations?.audience ?? undefined), |
| 38 | }, |
| 39 | ] |
| 40 | |
| 41 | case "image": |
| 42 | if (block.data) { |
| 43 | return [ |
| 44 | { |
| 45 | type: "file", |
| 46 | url: `data:${block.mimeType};base64,${block.data}`, |
| 47 | filename: filenameFromUri(block.uri ?? undefined) ?? "image", |
| 48 | mime: block.mimeType, |
| 49 | }, |
| 50 | ] |
| 51 | } |
| 52 | if (block.uri?.startsWith("data:")) { |
| 53 | return [ |
| 54 | { |
| 55 | type: "file", |
| 56 | url: block.uri, |
| 57 | filename: filenameFromUri(block.uri) ?? "image", |
| 58 | mime: block.mimeType, |
| 59 | }, |
| 60 | ] |
| 61 | } |
| 62 | if (block.uri?.startsWith("http://") || block.uri?.startsWith("https://")) { |
| 63 | return [ |
| 64 | { |
| 65 | type: "file", |
| 66 | url: block.uri, |
| 67 | filename: filenameFromUri(block.uri) ?? "image", |
| 68 | mime: block.mimeType, |
| 69 | }, |
| 70 | ] |
| 71 | } |
| 72 | return [] |
| 73 | |
| 74 | case "resource_link": |
| 75 | return [resourceLinkToPart(block)] |
| 76 | |
| 77 | case "resource": |
| 78 | if ("text" in block.resource) { |
| 79 | try { |
| 80 | const parsed = new URL(block.resource.uri) |
| 81 | if (parsed.protocol === "file:") { |
| 82 | const line = parsed.hash.match(/^#L(\d+)/)?.[1] |
| 83 | let filepath: string |
| 84 | try { |
| 85 | filepath = fileURLToPath(parsed) |
| 86 | } catch { |
| 87 | filepath = decodeURIComponent(parsed.pathname) |
no test coverage detected