Repository navigation
Expand file tree
/
Copy pathdocuments.mjs
More file actions
84 lines (77 loc) · 3.67 KB
/
Copy pathdocuments.mjs
File metadata and controls
84 lines (77 loc) · 3.67 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
// An Anthropic document block, as text for agy's ACP prompt. agy takes text
// and images: an embedded resource reaches the model as its inline text
// only, so a PDF or other binary file can't be handed over as it is.
//
// - a text source, or a content list of text parts: the text;
// - a base64 source of a text type (text/*, JSON, XML): the decoded text;
// - a URL source: the URL, said to be unread, so the model can fetch it
// with a tool instead of guessing what it says (daoche_wuzui on X: a
// client's URL document turned the whole request away);
// - a binary file in a tool result (a client's file read of a PDF): a note
// that it couldn't be attached, so the model can read it another way
// instead of the whole turn failing.
//
// documentText returns null for what can't be carried: a binary file the
// user attached, and a file id from the Files API. The caller refuses it
// with documentRefusal's words.
import { Buffer } from "node:buffer"
import { TextDecoder } from "node:util"
const TEXT_TYPES = /^(?:text\/[\w.+-]+|application\/(?:[\w.+-]+\+)?(?:json|xml))$/
function decodeBase64Text(data) {
if (typeof data !== "string" || !/^[A-Za-z0-9+/\s]*={0,2}\s*$/.test(data)) return null
try {
return new TextDecoder("utf-8", { fatal: true }).decode(Buffer.from(data, "base64"))
} catch {
return null
}
}
function mediaTypeOf(src) {
return String(src?.media_type || "").toLowerCase().split(";")[0].trim()
}
function titleOf(block) {
return typeof block.title === "string" && block.title.trim() ? block.title.trim() : ""
}
export function documentText(block, { inToolResult = false } = {}) {
if (block?.type !== "document") return ""
const title = titleOf(block)
const src = block.source
let text = null
if (src?.type === "text" && typeof src.data === "string") {
text = src.data
} else if (src?.type === "content") {
if (typeof src.content === "string") {
text = src.content
} else if (Array.isArray(src.content) && src.content.every((part) => part?.type === "text" && typeof part.text === "string")) {
text = src.content.map((part) => part.text).join("\n")
}
} else if (src?.type === "base64" && TEXT_TYPES.test(mediaTypeOf(src))) {
text = decodeBase64Text(src.data)
} else if (src?.type === "url" && typeof src.url === "string" && /^https?:\/\//i.test(src.url.trim())) {
const url = src.url.trim()
return `[document${title ? ": " + title : ""} at ${url}; its content is not attached here. Fetch the URL with a tool to read it.]`
}
if (text !== null) return title ? `[document: ${title}]\n${text}\n[end of document]` : text
if (inToolResult && src?.type === "base64") {
const kind = mediaTypeOf(src) === "application/pdf" ? "a PDF" : `a ${mediaTypeOf(src) || "binary"} file`
return `[document${title ? ": " + title : ""}: ${kind} that could not be attached here. Extract its text with a tool (pdftotext, say) to read it.]`
}
return null
}
// documentRefusal says what was turned away and what the user can do.
export function documentRefusal(block) {
const src = block?.source
if (src?.type === "file") {
return {
what: "a document sent as a Files API file_id",
hint: "agy can't fetch it; send the document inline, as text or base64",
}
}
if (src?.type === "url") {
return { what: "a document URL other than http(s)", hint: "send an http(s) URL, or the document's text" }
}
const type = mediaTypeOf(src)
return {
what: `a ${type === "application/pdf" ? "PDF" : (type || "binary") + " file"} attached as a document`,
hint: "agy reads text and images, not PDFs or other files. Paste the document's text, attach its pages as images, or let the agent read the file with a tool",
}
}