talon-agent 1.46.1 → 1.47.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "talon-agent",
3
- "version": "1.46.1",
3
+ "version": "1.47.1",
4
4
  "description": "Multi-frontend AI agent with full tool access, streaming, cron jobs, and plugin system",
5
5
  "author": "Dylan Neve",
6
6
  "license": "MIT",
@@ -110,6 +110,7 @@
110
110
  "croner": "^10.0.1",
111
111
  "cross-spawn": "^7.0.6",
112
112
  "discord.js": "^14.16.3",
113
+ "file-type": "^22.0.1",
113
114
  "grammy": "^1.42.0",
114
115
  "liquidjs": "^10.27.0",
115
116
  "marked": "^18.0.0",
@@ -3,12 +3,35 @@
3
3
  * binary content (validated by magic bytes) into the uploads workspace.
4
4
  */
5
5
 
6
+ import { randomUUID } from "node:crypto";
6
7
  import { existsSync, mkdirSync, writeFileSync } from "node:fs";
7
8
  import { resolve } from "node:path";
9
+ import {
10
+ readBodyLimited,
11
+ ResponseTooLargeError,
12
+ } from "../../../util/http-body.js";
13
+ import {
14
+ advertisedBinaryKind,
15
+ decodeText,
16
+ detectBinaryType,
17
+ extractText,
18
+ isHtmlContent,
19
+ isTextContent,
20
+ matchesBinaryKind,
21
+ } from "../../../util/web-content.js";
8
22
  import { dirs } from "../../../util/paths.js";
9
- import { extractText } from "./shared.js";
10
23
  import type { SharedActionHandlers } from "./types.js";
11
24
 
25
+ const MAX_RESPONSE_MB = 50;
26
+ const MAX_RESPONSE_BYTES = MAX_RESPONSE_MB * 1024 * 1024;
27
+ const MAX_TEXT_CHARS = 50_000;
28
+
29
+ /** Cap returned text, marking the cut so truncation is never silent. */
30
+ function capText(text: string): string {
31
+ if (text.length <= MAX_TEXT_CHARS) return text;
32
+ return `${text.slice(0, MAX_TEXT_CHARS)}\n\n[Content truncated at ${MAX_TEXT_CHARS} characters]`;
33
+ }
34
+
12
35
  export const fetchUrlHandlers: SharedActionHandlers = {
13
36
  fetch_url: async (body) => {
14
37
  const url = String(body.url ?? "");
@@ -32,84 +55,75 @@ export const fetchUrlHandlers: SharedActionHandlers = {
32
55
 
33
56
  // Reject oversized responses before downloading the body.
34
57
  // The Content-Length header is advisory but saves bandwidth when present.
35
- const MAX_BYTES = 20 * 1024 * 1024; // 20 MB
36
58
  const contentLength = resp.headers.get("content-length");
37
- if (contentLength && Number(contentLength) > MAX_BYTES) {
59
+ if (contentLength && Number(contentLength) > MAX_RESPONSE_BYTES) {
38
60
  return {
39
61
  ok: false,
40
- error: `File too large (${(Number(contentLength) / 1024 / 1024).toFixed(0)}MB, max 20MB)`,
62
+ error: `File too large (${(Number(contentLength) / 1024 / 1024).toFixed(0)}MB, max ${MAX_RESPONSE_MB}MB)`,
41
63
  };
42
64
  }
43
65
 
44
- // Binary content: download and save to workspace
45
66
  const mimeType = ct.split(";")[0].trim().toLowerCase();
46
- const isText =
47
- mimeType.startsWith("text/") || mimeType === "application/json";
48
- if (!isText) {
49
- const buffer = Buffer.from(await resp.arrayBuffer());
50
- if (buffer.length > MAX_BYTES)
51
- return { ok: false, error: "File too large (max 20MB)" };
52
- if (buffer.length === 0)
53
- return { ok: false, error: "Empty response (0 bytes)" };
54
-
55
- // Validate magic bytes — prevent saving HTML error pages as images
56
- // (servers can return error pages with image content-type headers)
57
- const magic = buffer.subarray(0, 16);
58
- const isRealImage =
59
- (magic[0] === 0xff && magic[1] === 0xd8) || // JPEG
60
- (magic[0] === 0x89 &&
61
- magic[1] === 0x50 &&
62
- magic[2] === 0x4e &&
63
- magic[3] === 0x47) || // PNG
64
- (magic[0] === 0x47 && magic[1] === 0x49 && magic[2] === 0x46) || // GIF
65
- (magic[0] === 0x52 &&
66
- magic[1] === 0x49 &&
67
- magic[2] === 0x46 &&
68
- magic[3] === 0x46 &&
69
- magic[8] === 0x57 &&
70
- magic[9] === 0x45 &&
71
- magic[10] === 0x42 &&
72
- magic[11] === 0x50); // WebP
73
-
74
- // If content-type says image but bytes say otherwise, treat as text
75
- if (ct.startsWith("image/") && !isRealImage) {
76
- const text = extractText(buffer.toString("utf-8"), 500);
67
+ let buffer: Buffer;
68
+ try {
69
+ buffer = await readBodyLimited(resp, MAX_RESPONSE_BYTES);
70
+ } catch (err) {
71
+ if (err instanceof ResponseTooLargeError) {
77
72
  return {
78
73
  ok: false,
79
- error: `Server returned an error page instead of an image. Content: ${text}`,
74
+ error: `Response too large (max ${MAX_RESPONSE_MB}MB)`,
80
75
  };
81
76
  }
77
+ throw err;
78
+ }
79
+
80
+ if (isTextContent(mimeType, buffer)) {
81
+ const trimmed = decodeText(buffer, ct).trim();
82
+ if (!trimmed)
83
+ return { ok: true, text: "(Page has no readable content)" };
82
84
 
83
- const ext = isRealImage
84
- ? magic[0] === 0xff
85
- ? "jpg"
86
- : magic[0] === 0x89
87
- ? "png"
88
- : magic[0] === 0x47
89
- ? "gif"
90
- : "webp"
91
- : ct.includes("pdf")
92
- ? "pdf"
93
- : ct.includes("zip")
94
- ? "zip"
95
- : "bin";
96
- const uploadsDir = dirs.uploads;
97
- if (!existsSync(uploadsDir)) mkdirSync(uploadsDir, { recursive: true });
98
- const filePath = resolve(uploadsDir, `${Date.now()}-fetched.${ext}`);
99
- writeFileSync(filePath, buffer);
100
- const typeLabel = isRealImage
101
- ? "image"
102
- : (ct.split("/")[1]?.split(";")[0] ?? "file");
85
+ // extractText is a DOM extractor — running it on JSON/XML/JavaScript/
86
+ // plain text strips small payloads like {"status":"ok"} to nothing,
87
+ // so only HTML (declared or sniffed) goes through it.
88
+ if (!isHtmlContent(mimeType, trimmed)) {
89
+ return { ok: true, text: capText(trimmed) };
90
+ }
91
+ const text = extractText(trimmed, Number.POSITIVE_INFINITY);
92
+ if (text.length < 20)
93
+ return { ok: true, text: "(Page has no readable content)" };
94
+ return { ok: true, text: capText(text) };
95
+ }
96
+
97
+ if (buffer.length === 0)
98
+ return { ok: false, error: "Empty response (0 bytes)" };
99
+
100
+ const detected = await detectBinaryType(buffer);
101
+ const advertised = advertisedBinaryKind(mimeType);
102
+
103
+ // Do not save an error page or arbitrary bytes under a trusted-looking
104
+ // image/PDF/ZIP extension merely because the server advertised one.
105
+ if (advertised && !matchesBinaryKind(advertised, detected, buffer)) {
106
+ const text = extractText(decodeText(buffer, ct), 500);
103
107
  return {
104
- ok: true,
105
- text: `Downloaded ${typeLabel} (${(buffer.length / 1024).toFixed(0)}KB) to: ${filePath}\nRead it with the Read tool or send it with send(type="file", file_path="${filePath}").`,
108
+ ok: false,
109
+ error: `Server returned invalid ${advertised} content.${text ? ` Content: ${text}` : ""}`,
106
110
  };
107
111
  }
108
- const raw = await resp.text();
109
- const text = extractText(raw);
110
- if (text.length < 20)
111
- return { ok: true, text: "(Page has no readable content)" };
112
- return { ok: true, text };
112
+
113
+ const uploadsDir = dirs.uploads;
114
+ if (!existsSync(uploadsDir)) mkdirSync(uploadsDir, { recursive: true });
115
+ const filePath = resolve(
116
+ uploadsDir,
117
+ `${Date.now()}-${randomUUID().slice(0, 8)}-fetched.${detected?.ext ?? "bin"}`,
118
+ );
119
+ writeFileSync(filePath, buffer);
120
+ const typeLabel = detected?.mime.startsWith("image/")
121
+ ? "image"
122
+ : (detected?.ext ?? ct.split("/")[1]?.split(";")[0] ?? "file");
123
+ return {
124
+ ok: true,
125
+ text: `Downloaded ${typeLabel} (${(buffer.length / 1024).toFixed(0)}KB) to: ${filePath}\nRead it with the Read tool or send it with send(type="file", file_path="${filePath}").`,
126
+ };
113
127
  } catch (err) {
114
128
  return {
115
129
  ok: false,
@@ -1,9 +1,8 @@
1
1
  /**
2
- * Helpers shared across shared-action domains: due-date parsing, HTML text
3
- * extraction, and per-job model-override validation.
2
+ * Helpers shared across shared-action domains: due-date parsing and per-job
3
+ * model-override validation.
4
4
  */
5
5
 
6
- import * as cheerio from "cheerio";
7
6
  import { resolveExplicitModelRef } from "../../models/active-model.js";
8
7
  import {
9
8
  getBackendForChat,
@@ -20,16 +19,6 @@ export function parseDueDate(value: unknown): number | undefined {
20
19
  return Number.isFinite(ms) ? ms : undefined;
21
20
  }
22
21
 
23
- /** Extract readable text from HTML using cheerio (proper DOM parser). */
24
- export function extractText(html: string, maxLength = 8000): string {
25
- const $ = cheerio.load(html);
26
- // Remove non-content elements
27
- $("script, style, noscript, iframe, svg, nav, footer, header").remove();
28
- // Get text content, normalize whitespace
29
- const text = $("body").text().replace(/\s+/g, " ").trim();
30
- return text.slice(0, maxLength);
31
- }
32
-
33
22
  /**
34
23
  * Validate a per-job model override so create_cron_job / trigger_create can
35
24
  * reject a bad override up front — the agent retries or tells the user —
@@ -0,0 +1,51 @@
1
+ /**
2
+ * Bounded reading of fetch response bodies. Content-Length is optional and
3
+ * advisory, so the only trustworthy size limit is one enforced while the
4
+ * body streams in.
5
+ */
6
+
7
+ export class ResponseTooLargeError extends Error {}
8
+
9
+ /**
10
+ * Read a response incrementally so a missing or dishonest Content-Length
11
+ * header cannot buffer an unbounded body in memory.
12
+ */
13
+ export async function readBodyLimited(
14
+ response: Response,
15
+ maxBytes: number,
16
+ ): Promise<Buffer> {
17
+ if (!response.body) {
18
+ const buffer = Buffer.from(await response.arrayBuffer());
19
+ if (buffer.length > maxBytes) throw new ResponseTooLargeError();
20
+ return buffer;
21
+ }
22
+
23
+ const reader = response.body.getReader();
24
+ const chunks: Buffer[] = [];
25
+ let totalBytes = 0;
26
+
27
+ try {
28
+ while (true) {
29
+ const { done, value } = await reader.read();
30
+ if (done) break;
31
+ if (!value?.byteLength) continue;
32
+
33
+ totalBytes += value.byteLength;
34
+ if (totalBytes > maxBytes) {
35
+ try {
36
+ await reader.cancel("response exceeds download limit");
37
+ } catch {
38
+ // Cancellation is cleanup only; preserve the useful size error.
39
+ }
40
+ throw new ResponseTooLargeError();
41
+ }
42
+ chunks.push(
43
+ Buffer.from(value.buffer, value.byteOffset, value.byteLength),
44
+ );
45
+ }
46
+ } finally {
47
+ reader.releaseLock();
48
+ }
49
+
50
+ return Buffer.concat(chunks, totalBytes);
51
+ }
@@ -0,0 +1,115 @@
1
+ /**
2
+ * Interpretation of fetched web content: charset decoding, text/binary
3
+ * classification, HTML text extraction, and magic-byte validation of binary
4
+ * payloads (via file-type) so server-declared Content-Type headers are never
5
+ * trusted on their own.
6
+ */
7
+
8
+ import * as cheerio from "cheerio";
9
+ import { fileTypeFromBuffer } from "file-type";
10
+
11
+ /** Binary categories whose content Talon validates against magic bytes. */
12
+ export type BinaryKind = "image" | "pdf" | "zip";
13
+
14
+ export interface DetectedBinary {
15
+ ext: string;
16
+ mime: string;
17
+ }
18
+
19
+ function isTextMimeType(mimeType: string): boolean {
20
+ return (
21
+ mimeType.startsWith("text/") ||
22
+ mimeType === "application/json" ||
23
+ mimeType.endsWith("+json") ||
24
+ mimeType === "application/xml" ||
25
+ mimeType.endsWith("+xml") ||
26
+ mimeType === "application/javascript" ||
27
+ mimeType === "application/x-javascript"
28
+ );
29
+ }
30
+
31
+ function looksLikeStructuredText(buffer: Buffer): boolean {
32
+ if (buffer.subarray(0, 512).includes(0)) return false;
33
+ const sample = buffer.subarray(0, 512).toString("utf-8").trimStart();
34
+ return (
35
+ sample.startsWith("{") ||
36
+ sample.startsWith("[") ||
37
+ sample.startsWith("<?xml") ||
38
+ /^<!doctype\s+html/i.test(sample) ||
39
+ /^<[a-z][\w:-]*(?:\s|>)/i.test(sample)
40
+ );
41
+ }
42
+
43
+ /**
44
+ * Whether a response should be treated as readable text. Servers frequently
45
+ * omit Content-Type or fall back to application/octet-stream, so structured
46
+ * text is also sniffed from the body when the header says nothing useful.
47
+ */
48
+ export function isTextContent(mimeType: string, buffer: Buffer): boolean {
49
+ if (isTextMimeType(mimeType)) return true;
50
+ const isGenericMime = !mimeType || mimeType === "application/octet-stream";
51
+ return isGenericMime && looksLikeStructuredText(buffer);
52
+ }
53
+
54
+ /**
55
+ * Whether textual content is an HTML document (declared or sniffed) and thus
56
+ * benefits from DOM text extraction rather than being returned verbatim.
57
+ */
58
+ export function isHtmlContent(mimeType: string, text: string): boolean {
59
+ return (
60
+ mimeType.includes("html") || /^<!doctype\s+html|^<html[\s>]/i.test(text)
61
+ );
62
+ }
63
+
64
+ /** Decode a body honoring the declared response charset, defaulting to UTF-8. */
65
+ export function decodeText(buffer: Buffer, contentType: string): string {
66
+ const charset = /charset\s*=\s*["']?([^;\s"']+)/i.exec(contentType)?.[1];
67
+ try {
68
+ return new TextDecoder(charset || "utf-8").decode(buffer);
69
+ } catch {
70
+ return buffer.toString("utf-8");
71
+ }
72
+ }
73
+
74
+ /** Extract readable text from HTML using cheerio (proper DOM parser). */
75
+ export function extractText(html: string, maxLength = 8000): string {
76
+ const $ = cheerio.load(html);
77
+ // Remove non-content elements
78
+ $("script, style, noscript, iframe, svg, nav, footer, header").remove();
79
+ // Get text content, normalize whitespace
80
+ const text = $("body").text().replace(/\s+/g, " ").trim();
81
+ return text.slice(0, maxLength);
82
+ }
83
+
84
+ /** Identify a binary payload from its magic bytes. */
85
+ export async function detectBinaryType(
86
+ buffer: Buffer,
87
+ ): Promise<DetectedBinary | null> {
88
+ const detected = await fileTypeFromBuffer(buffer);
89
+ return detected ? { ext: detected.ext, mime: detected.mime } : null;
90
+ }
91
+
92
+ /** The binary kind a Content-Type header promises, if it promises one. */
93
+ export function advertisedBinaryKind(mimeType: string): BinaryKind | null {
94
+ if (mimeType.startsWith("image/")) return "image";
95
+ if (mimeType.includes("pdf")) return "pdf";
96
+ if (mimeType.includes("zip")) return "zip";
97
+ return null;
98
+ }
99
+
100
+ /** Whether the actual bytes satisfy what the Content-Type header promised. */
101
+ export function matchesBinaryKind(
102
+ kind: BinaryKind,
103
+ detected: DetectedBinary | null,
104
+ buffer: Buffer,
105
+ ): boolean {
106
+ switch (kind) {
107
+ case "image":
108
+ return detected?.mime.startsWith("image/") ?? false;
109
+ case "pdf":
110
+ return detected?.ext === "pdf";
111
+ case "zip":
112
+ // The whole zip family (docx, jar, epub, …) shares the PK signature.
113
+ return buffer[0] === 0x50 && buffer[1] === 0x4b;
114
+ }
115
+ }