@agent-native/core 0.44.0 → 0.44.3

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.
Files changed (75) hide show
  1. package/dist/agent/production-agent.d.ts.map +1 -1
  2. package/dist/agent/production-agent.js +28 -13
  3. package/dist/agent/production-agent.js.map +1 -1
  4. package/dist/cli/pr-visual-recap-workflow.d.ts +1 -1
  5. package/dist/cli/pr-visual-recap-workflow.d.ts.map +1 -1
  6. package/dist/cli/pr-visual-recap-workflow.js +1 -1
  7. package/dist/cli/pr-visual-recap-workflow.js.map +1 -1
  8. package/dist/cli/recap.d.ts +23 -0
  9. package/dist/cli/recap.d.ts.map +1 -1
  10. package/dist/cli/recap.js +175 -0
  11. package/dist/cli/recap.js.map +1 -1
  12. package/dist/cli/skills.d.ts +3 -3
  13. package/dist/cli/skills.d.ts.map +1 -1
  14. package/dist/cli/skills.js +54 -7
  15. package/dist/cli/skills.js.map +1 -1
  16. package/dist/client/blocks/library/AnnotatedCodeBlock.d.ts.map +1 -1
  17. package/dist/client/blocks/library/AnnotatedCodeBlock.js +21 -8
  18. package/dist/client/blocks/library/AnnotatedCodeBlock.js.map +1 -1
  19. package/dist/client/blocks/library/ApiEndpointBlock.d.ts.map +1 -1
  20. package/dist/client/blocks/library/ApiEndpointBlock.js +112 -12
  21. package/dist/client/blocks/library/ApiEndpointBlock.js.map +1 -1
  22. package/dist/client/blocks/library/DiffBlock.d.ts.map +1 -1
  23. package/dist/client/blocks/library/DiffBlock.js +59 -75
  24. package/dist/client/blocks/library/DiffBlock.js.map +1 -1
  25. package/dist/client/blocks/library/JsonExplorerBlock.d.ts.map +1 -1
  26. package/dist/client/blocks/library/JsonExplorerBlock.js +1 -1
  27. package/dist/client/blocks/library/JsonExplorerBlock.js.map +1 -1
  28. package/dist/client/blocks/library/MermaidBlock.d.ts.map +1 -1
  29. package/dist/client/blocks/library/MermaidBlock.js +22 -3
  30. package/dist/client/blocks/library/MermaidBlock.js.map +1 -1
  31. package/dist/client/blocks/library/annotation-rail.d.ts +85 -0
  32. package/dist/client/blocks/library/annotation-rail.d.ts.map +1 -1
  33. package/dist/client/blocks/library/annotation-rail.js +149 -8
  34. package/dist/client/blocks/library/annotation-rail.js.map +1 -1
  35. package/dist/client/blocks/library/diagram.d.ts +17 -0
  36. package/dist/client/blocks/library/diagram.d.ts.map +1 -1
  37. package/dist/client/blocks/library/diagram.js +47 -2
  38. package/dist/client/blocks/library/diagram.js.map +1 -1
  39. package/dist/client/composer/TiptapComposer.d.ts.map +1 -1
  40. package/dist/client/composer/TiptapComposer.js +13 -8
  41. package/dist/client/composer/TiptapComposer.js.map +1 -1
  42. package/dist/client/composer/pasted-text.d.ts +25 -0
  43. package/dist/client/composer/pasted-text.d.ts.map +1 -1
  44. package/dist/client/composer/pasted-text.js +86 -4
  45. package/dist/client/composer/pasted-text.js.map +1 -1
  46. package/dist/client/context-xray/ContextMeter.d.ts.map +1 -1
  47. package/dist/client/context-xray/ContextMeter.js +5 -3
  48. package/dist/client/context-xray/ContextMeter.js.map +1 -1
  49. package/dist/db/migrations.d.ts +10 -0
  50. package/dist/db/migrations.d.ts.map +1 -1
  51. package/dist/db/migrations.js +32 -0
  52. package/dist/db/migrations.js.map +1 -1
  53. package/dist/file-upload/builder.d.ts.map +1 -1
  54. package/dist/file-upload/builder.js +23 -8
  55. package/dist/file-upload/builder.js.map +1 -1
  56. package/dist/server/og-fonts-data.d.ts +3 -0
  57. package/dist/server/og-fonts-data.d.ts.map +1 -0
  58. package/dist/server/og-fonts-data.js +9 -0
  59. package/dist/server/og-fonts-data.js.map +1 -0
  60. package/dist/server/og-fonts.d.ts +10 -0
  61. package/dist/server/og-fonts.d.ts.map +1 -0
  62. package/dist/server/og-fonts.js +58 -0
  63. package/dist/server/og-fonts.js.map +1 -0
  64. package/dist/server/social-og-image.d.ts.map +1 -1
  65. package/dist/server/social-og-image.js +16 -5
  66. package/dist/server/social-og-image.js.map +1 -1
  67. package/dist/styles/blocks.css +111 -0
  68. package/dist/usage/store.d.ts +12 -0
  69. package/dist/usage/store.d.ts.map +1 -1
  70. package/dist/usage/store.js +35 -5
  71. package/dist/usage/store.js.map +1 -1
  72. package/dist/vite/client.d.ts.map +1 -1
  73. package/dist/vite/client.js +26 -3
  74. package/dist/vite/client.js.map +1 -1
  75. package/package.json +1 -1
@@ -1,4 +1,29 @@
1
+ /** The clipboard flavors we care about for a paste. */
2
+ export interface ClipboardPaste {
3
+ /** `text/plain` flavor — used for size heuristics and as the default body. */
4
+ text: string;
5
+ /** `text/html` flavor when the source provided one. */
6
+ html?: string;
7
+ }
8
+ /** Read the relevant clipboard flavors from a paste/drop DataTransfer. */
9
+ export declare function readClipboardPaste(data: {
10
+ getData(type: string): string;
11
+ } | null | undefined): ClipboardPaste;
1
12
  export declare function shouldConvertPasteToAttachment(text: string): boolean;
13
+ /**
14
+ * Whether a clipboard paste is large enough to become a `Pasted text`
15
+ * attachment chip. Mirrors `shouldConvertPasteToAttachment` but evaluates the
16
+ * representation we'd actually store, so an HTML-only paste (empty text/plain)
17
+ * still converts on the strength of its markup.
18
+ */
19
+ export declare function shouldConvertClipboardToAttachment(paste: ClipboardPaste): boolean;
20
+ /**
21
+ * Build the attachment File for a page-sized paste, preserving HTML markup when
22
+ * the pasted content is an HTML document so it travels the same rail as an
23
+ * uploaded .html file.
24
+ */
25
+ export declare function createPastedAttachmentFile(paste: ClipboardPaste): File;
26
+ /** Back-compat helper for callers that only have plain text. */
2
27
  export declare function createPastedTextFile(text: string): File;
3
28
  export declare function isPastedTextAttachmentName(name: string | undefined): boolean;
4
29
  export declare function unwrapAttachmentEnvelope(text: string): string;
@@ -1 +1 @@
1
- {"version":3,"file":"pasted-text.d.ts","sourceRoot":"","sources":["../../../src/client/composer/pasted-text.ts"],"names":[],"mappings":"AAQA,wBAAgB,8BAA8B,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAWpE;AAED,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAKvD;AAED,wBAAgB,0BAA0B,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,GAAG,OAAO,CAE5E;AAKD,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAG7D;AAED,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAO/C"}
1
+ {"version":3,"file":"pasted-text.d.ts","sourceRoot":"","sources":["../../../src/client/composer/pasted-text.ts"],"names":[],"mappings":"AA4CA,uDAAuD;AACvD,MAAM,WAAW,cAAc;IAC7B,8EAA8E;IAC9E,IAAI,EAAE,MAAM,CAAC;IACb,uDAAuD;IACvD,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,0EAA0E;AAC1E,wBAAgB,kBAAkB,CAChC,IAAI,EAAE;IAAE,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAA;CAAE,GAAG,IAAI,GAAG,SAAS,GACzD,cAAc,CAIhB;AAmCD,wBAAgB,8BAA8B,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAWpE;AAED;;;;;GAKG;AACH,wBAAgB,kCAAkC,CAChD,KAAK,EAAE,cAAc,GACpB,OAAO,CAET;AAQD;;;;GAIG;AACH,wBAAgB,0BAA0B,CAAC,KAAK,EAAE,cAAc,GAAG,IAAI,CAGtE;AAED,gEAAgE;AAChE,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAEvD;AAED,wBAAgB,0BAA0B,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,GAAG,OAAO,CAE5E;AAKD,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAG7D;AAED,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAO/C"}
@@ -4,6 +4,67 @@
4
4
  const PASTED_TEXT_MIN_CHARS = 3200;
5
5
  const PASTED_TEXT_MIN_LINES = 24;
6
6
  const PASTED_TEXT_FILENAME_PREFIX = "pasted-text-";
7
+ // A copied HTML document/source is recognizable from its markup: a closing tag
8
+ // (`</div>`), a doctype, or a common structural/element tag. We key off the
9
+ // *content* the user actually pasted, not the clipboard's `text/html` flavor —
10
+ // editors (VS Code) and rich-text apps (Google Docs) populate `text/html` with
11
+ // syntax-highlight spans or formatting wrappers even when the real content is
12
+ // plain code/prose, so trusting `text/html` blindly would mangle those pastes.
13
+ const HTML_SOURCE_SIGNAL = /<!doctype\s+html|<html[\s>]|<\/[a-z][a-z0-9-]*\s*>|<(?:body|head|div|span|section|main|header|footer|nav|article|aside|ul|ol|li|table|thead|tbody|tr|td|th|h[1-6]|p|a|img|button|input|textarea|select|form|label|script|style|link|meta|svg|canvas|template)\b/i;
14
+ // A real HTML *document* announces itself with a doctype / html / head / body.
15
+ // When one of these is present we keep the HTML classification even if an inline
16
+ // <script> contains JS keywords — it's a genuine page.
17
+ const HTML_DOCUMENT_SIGNAL = /<!doctype\s+html|<html[\s>]|<head[\s>]|<body[\s>]/i;
18
+ // JSX/TSX source contains the same `</div>`/`<span>` tags as an HTML document,
19
+ // so the HTML tag signal alone misfiles a pasted React/TS component (even a bare
20
+ // function component) as an `.html` artifact — the agent then mishandles it as a
21
+ // hostable document instead of source. These markers appear in JS/TS/JSX source
22
+ // but not in a plain HTML *fragment*: `className=` (JSX uses it; HTML uses
23
+ // `class=`), ES module import/export, arrow functions, TS type/React annotations,
24
+ // React hooks, and the basic JS declaration/return keywords that make up a
25
+ // component body.
26
+ const CODE_SOURCE_SIGNAL = /\bclassName=|\bimport\b|\bexport\b|=>|:\s*React\.|\buse[A-Z]\w*\(|\b(?:function|const|let|var|return|class|interface|type|enum)\b/;
27
+ function looksLikeHtml(value) {
28
+ if (!value || !HTML_SOURCE_SIGNAL.test(value))
29
+ return false;
30
+ // A self-announcing HTML document stays HTML even if it embeds a <script>.
31
+ if (HTML_DOCUMENT_SIGNAL.test(value))
32
+ return true;
33
+ // Otherwise it's a bare fragment — if it carries JS/TS/JSX code signals,
34
+ // treat it as source code, not an HTML attachment.
35
+ if (CODE_SOURCE_SIGNAL.test(value))
36
+ return false;
37
+ return true;
38
+ }
39
+ /** Read the relevant clipboard flavors from a paste/drop DataTransfer. */
40
+ export function readClipboardPaste(data) {
41
+ const text = data?.getData("text/plain") ?? "";
42
+ const html = data?.getData("text/html") ?? "";
43
+ return { text, html: html.trim() ? html : undefined };
44
+ }
45
+ // Decide what to actually store for a paste. Preserving HTML markup means a
46
+ // pasted HTML document behaves exactly like uploading that .html file: the agent
47
+ // recognizes it as a hostable artifact and reads it verbatim via
48
+ // `contentFromAttachment` instead of retyping it inline (which cuts off
49
+ // mid-stream on large files and triggers a continuation loop / "spin").
50
+ function selectPasteBody(paste) {
51
+ const plain = paste.text ?? "";
52
+ const html = paste.html ?? "";
53
+ // 1) The pasted text is itself HTML source (copied from an editor, a file, or
54
+ // view-source). The plain text *is* the markup, so keep it as .html.
55
+ if (plain.trim() && looksLikeHtml(plain)) {
56
+ return { body: plain, ext: "html", type: "text/html" };
57
+ }
58
+ // 2) No usable plain text, but a real HTML flavor exists (some apps only
59
+ // expose text/html). Fall back to the markup so nothing is dropped.
60
+ if (!plain.trim() && looksLikeHtml(html)) {
61
+ return { body: html, ext: "html", type: "text/html" };
62
+ }
63
+ // 3) Default: keep the clean plain text. Avoids the syntax-highlight /
64
+ // rich-text noise that lives in text/html when the plain text is already
65
+ // the real content (code from an editor, prose from a doc).
66
+ return { body: plain, ext: "txt", type: "text/plain" };
67
+ }
7
68
  export function shouldConvertPasteToAttachment(text) {
8
69
  if (!text)
9
70
  return false;
@@ -19,11 +80,32 @@ export function shouldConvertPasteToAttachment(text) {
19
80
  }
20
81
  return false;
21
82
  }
22
- export function createPastedTextFile(text) {
23
- const name = `${PASTED_TEXT_FILENAME_PREFIX}${Date.now()}-${Math.random()
83
+ /**
84
+ * Whether a clipboard paste is large enough to become a `Pasted text`
85
+ * attachment chip. Mirrors `shouldConvertPasteToAttachment` but evaluates the
86
+ * representation we'd actually store, so an HTML-only paste (empty text/plain)
87
+ * still converts on the strength of its markup.
88
+ */
89
+ export function shouldConvertClipboardToAttachment(paste) {
90
+ return shouldConvertPasteToAttachment(selectPasteBody(paste).body);
91
+ }
92
+ function pastedAttachmentName(ext) {
93
+ return `${PASTED_TEXT_FILENAME_PREFIX}${Date.now()}-${Math.random()
24
94
  .toString(36)
25
- .slice(2, 8)}.txt`;
26
- return new File([text], name, { type: "text/plain" });
95
+ .slice(2, 8)}.${ext}`;
96
+ }
97
+ /**
98
+ * Build the attachment File for a page-sized paste, preserving HTML markup when
99
+ * the pasted content is an HTML document so it travels the same rail as an
100
+ * uploaded .html file.
101
+ */
102
+ export function createPastedAttachmentFile(paste) {
103
+ const { body, ext, type } = selectPasteBody(paste);
104
+ return new File([body], pastedAttachmentName(ext), { type });
105
+ }
106
+ /** Back-compat helper for callers that only have plain text. */
107
+ export function createPastedTextFile(text) {
108
+ return createPastedAttachmentFile({ text });
27
109
  }
28
110
  export function isPastedTextAttachmentName(name) {
29
111
  return !!name && name.startsWith(PASTED_TEXT_FILENAME_PREFIX);
@@ -1 +1 @@
1
- {"version":3,"file":"pasted-text.js","sourceRoot":"","sources":["../../../src/client/composer/pasted-text.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,0EAA0E;AAC1E,+DAA+D;AAC/D,MAAM,qBAAqB,GAAG,IAAI,CAAC;AACnC,MAAM,qBAAqB,GAAG,EAAE,CAAC;AAEjC,MAAM,2BAA2B,GAAG,cAAc,CAAC;AAEnD,MAAM,UAAU,8BAA8B,CAAC,IAAY;IACzD,IAAI,CAAC,IAAI;QAAE,OAAO,KAAK,CAAC;IACxB,IAAI,IAAI,CAAC,MAAM,IAAI,qBAAqB;QAAE,OAAO,IAAI,CAAC;IACtD,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACr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E,CAAC;IACzC,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["// Page-sized pastes turn into a `Pasted text` attachment chip instead of being\n// dumped into the editor. Short paragraphs and everyday lists should stay\n// inline so the composer still feels like a normal text field.\nconst PASTED_TEXT_MIN_CHARS = 3200;\nconst PASTED_TEXT_MIN_LINES = 24;\n\nconst PASTED_TEXT_FILENAME_PREFIX = \"pasted-text-\";\n\nexport function shouldConvertPasteToAttachment(text: string): boolean {\n if (!text) return false;\n if (text.length >= PASTED_TEXT_MIN_CHARS) return true;\n let lines = 1;\n for (let i = 0; i < text.length; i++) {\n if (text.charCodeAt(i) === 10) {\n lines++;\n if (lines >= PASTED_TEXT_MIN_LINES) return true;\n }\n }\n return false;\n}\n\nexport function createPastedTextFile(text: string): File {\n const name = `${PASTED_TEXT_FILENAME_PREFIX}${Date.now()}-${Math.random()\n .toString(36)\n .slice(2, 8)}.txt`;\n return new File([text], name, { type: \"text/plain\" });\n}\n\nexport function isPastedTextAttachmentName(name: string | undefined): boolean {\n return !!name && name.startsWith(PASTED_TEXT_FILENAME_PREFIX);\n}\n\n// Strips the `<attachment name=...>\\n` / `\\n</attachment>` envelope that\n// SimpleTextAttachmentAdapter wraps the file body in when sending. Returns the\n// raw body for previewing.\nexport function unwrapAttachmentEnvelope(text: string): string {\n const match = text.match(/^<attachment\\b[^>]*>\\n([\\s\\S]*)\\n<\\/attachment>$/);\n return match ? match[1] : text;\n}\n\nexport function countLines(text: string): number {\n if (!text) return 0;\n let lines = 1;\n for (let i = 0; i < text.length; i++) {\n if (text.charCodeAt(i) === 10) lines++;\n }\n return lines;\n}\n"]}
1
+ {"version":3,"file":"pasted-text.js","sourceRoot":"","sources":["../../../src/client/composer/pasted-text.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,0EAA0E;AAC1E,+DAA+D;AAC/D,MAAM,qBAAqB,GAAG,IAAI,CAAC;AACnC,MAAM,qBAAqB,GAAG,EAAE,CAAC;AAEjC,MAAM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KAAK,GAAG,CAAC,CAAC;IACd,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACrC,IAAI,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,KAAK,EAAE;YAAE,KAAK,EAAE,CAAC;IACzC,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["// Page-sized pastes turn into a `Pasted text` attachment chip instead of being\n// dumped into the editor. Short paragraphs and everyday lists should stay\n// inline so the composer still feels like a normal text field.\nconst PASTED_TEXT_MIN_CHARS = 3200;\nconst PASTED_TEXT_MIN_LINES = 24;\n\nconst PASTED_TEXT_FILENAME_PREFIX = \"pasted-text-\";\n\n// A copied HTML document/source is recognizable from its markup: a closing tag\n// (`</div>`), a doctype, or a common structural/element tag. We key off the\n// *content* the user actually pasted, not the clipboard's `text/html` flavor —\n// editors (VS Code) and rich-text apps (Google Docs) populate `text/html` with\n// syntax-highlight spans or formatting wrappers even when the real content is\n// plain code/prose, so trusting `text/html` blindly would mangle those pastes.\nconst HTML_SOURCE_SIGNAL =\n /<!doctype\\s+html|<html[\\s>]|<\\/[a-z][a-z0-9-]*\\s*>|<(?:body|head|div|span|section|main|header|footer|nav|article|aside|ul|ol|li|table|thead|tbody|tr|td|th|h[1-6]|p|a|img|button|input|textarea|select|form|label|script|style|link|meta|svg|canvas|template)\\b/i;\n\n// A real HTML *document* announces itself with a doctype / html / head / body.\n// When one of these is present we keep the HTML classification even if an inline\n// <script> contains JS keywords — it's a genuine page.\nconst HTML_DOCUMENT_SIGNAL =\n /<!doctype\\s+html|<html[\\s>]|<head[\\s>]|<body[\\s>]/i;\n\n// JSX/TSX source contains the same `</div>`/`<span>` tags as an HTML document,\n// so the HTML tag signal alone misfiles a pasted React/TS component (even a bare\n// function component) as an `.html` artifact — the agent then mishandles it as a\n// hostable document instead of source. These markers appear in JS/TS/JSX source\n// but not in a plain HTML *fragment*: `className=` (JSX uses it; HTML uses\n// `class=`), ES module import/export, arrow functions, TS type/React annotations,\n// React hooks, and the basic JS declaration/return keywords that make up a\n// component body.\nconst CODE_SOURCE_SIGNAL =\n /\\bclassName=|\\bimport\\b|\\bexport\\b|=>|:\\s*React\\.|\\buse[A-Z]\\w*\\(|\\b(?:function|const|let|var|return|class|interface|type|enum)\\b/;\n\nfunction looksLikeHtml(value: string): boolean {\n if (!value || !HTML_SOURCE_SIGNAL.test(value)) return false;\n // A self-announcing HTML document stays HTML even if it embeds a <script>.\n if (HTML_DOCUMENT_SIGNAL.test(value)) return true;\n // Otherwise it's a bare fragment — if it carries JS/TS/JSX code signals,\n // treat it as source code, not an HTML attachment.\n if (CODE_SOURCE_SIGNAL.test(value)) return false;\n return true;\n}\n\n/** The clipboard flavors we care about for a paste. */\nexport interface ClipboardPaste {\n /** `text/plain` flavor — used for size heuristics and as the default body. */\n text: string;\n /** `text/html` flavor when the source provided one. */\n html?: string;\n}\n\n/** Read the relevant clipboard flavors from a paste/drop DataTransfer. */\nexport function readClipboardPaste(\n data: { getData(type: string): string } | null | undefined,\n): ClipboardPaste {\n const text = data?.getData(\"text/plain\") ?? \"\";\n const html = data?.getData(\"text/html\") ?? \"\";\n return { text, html: html.trim() ? html : undefined };\n}\n\ninterface SelectedPasteBody {\n body: string;\n ext: \"html\" | \"txt\";\n type: \"text/html\" | \"text/plain\";\n}\n\n// Decide what to actually store for a paste. Preserving HTML markup means a\n// pasted HTML document behaves exactly like uploading that .html file: the agent\n// recognizes it as a hostable artifact and reads it verbatim via\n// `contentFromAttachment` instead of retyping it inline (which cuts off\n// mid-stream on large files and triggers a continuation loop / \"spin\").\nfunction selectPasteBody(paste: ClipboardPaste): SelectedPasteBody {\n const plain = paste.text ?? \"\";\n const html = paste.html ?? \"\";\n\n // 1) The pasted text is itself HTML source (copied from an editor, a file, or\n // view-source). The plain text *is* the markup, so keep it as .html.\n if (plain.trim() && looksLikeHtml(plain)) {\n return { body: plain, ext: \"html\", type: \"text/html\" };\n }\n\n // 2) No usable plain text, but a real HTML flavor exists (some apps only\n // expose text/html). Fall back to the markup so nothing is dropped.\n if (!plain.trim() && looksLikeHtml(html)) {\n return { body: html, ext: \"html\", type: \"text/html\" };\n }\n\n // 3) Default: keep the clean plain text. Avoids the syntax-highlight /\n // rich-text noise that lives in text/html when the plain text is already\n // the real content (code from an editor, prose from a doc).\n return { body: plain, ext: \"txt\", type: \"text/plain\" };\n}\n\nexport function shouldConvertPasteToAttachment(text: string): boolean {\n if (!text) return false;\n if (text.length >= PASTED_TEXT_MIN_CHARS) return true;\n let lines = 1;\n for (let i = 0; i < text.length; i++) {\n if (text.charCodeAt(i) === 10) {\n lines++;\n if (lines >= PASTED_TEXT_MIN_LINES) return true;\n }\n }\n return false;\n}\n\n/**\n * Whether a clipboard paste is large enough to become a `Pasted text`\n * attachment chip. Mirrors `shouldConvertPasteToAttachment` but evaluates the\n * representation we'd actually store, so an HTML-only paste (empty text/plain)\n * still converts on the strength of its markup.\n */\nexport function shouldConvertClipboardToAttachment(\n paste: ClipboardPaste,\n): boolean {\n return shouldConvertPasteToAttachment(selectPasteBody(paste).body);\n}\n\nfunction pastedAttachmentName(ext: \"html\" | \"txt\"): string {\n return `${PASTED_TEXT_FILENAME_PREFIX}${Date.now()}-${Math.random()\n .toString(36)\n .slice(2, 8)}.${ext}`;\n}\n\n/**\n * Build the attachment File for a page-sized paste, preserving HTML markup when\n * the pasted content is an HTML document so it travels the same rail as an\n * uploaded .html file.\n */\nexport function createPastedAttachmentFile(paste: ClipboardPaste): File {\n const { body, ext, type } = selectPasteBody(paste);\n return new File([body], pastedAttachmentName(ext), { type });\n}\n\n/** Back-compat helper for callers that only have plain text. */\nexport function createPastedTextFile(text: string): File {\n return createPastedAttachmentFile({ text });\n}\n\nexport function isPastedTextAttachmentName(name: string | undefined): boolean {\n return !!name && name.startsWith(PASTED_TEXT_FILENAME_PREFIX);\n}\n\n// Strips the `<attachment name=...>\\n` / `\\n</attachment>` envelope that\n// SimpleTextAttachmentAdapter wraps the file body in when sending. Returns the\n// raw body for previewing.\nexport function unwrapAttachmentEnvelope(text: string): string {\n const match = text.match(/^<attachment\\b[^>]*>\\n([\\s\\S]*)\\n<\\/attachment>$/);\n return match ? match[1] : text;\n}\n\nexport function countLines(text: string): number {\n if (!text) return 0;\n let lines = 1;\n for (let i = 0; i < text.length; i++) {\n if (text.charCodeAt(i) === 10) lines++;\n }\n return lines;\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"ContextMeter.d.ts","sourceRoot":"","sources":["../../../src/client/context-xray/ContextMeter.tsx"],"names":[],"mappings":"AAuDA,wBAAgB,YAAY,CAAC,EAC3B,QAAQ,EACR,OAAc,GACf,EAAE;IACD,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB,2CA8GA"}
1
+ {"version":3,"file":"ContextMeter.d.ts","sourceRoot":"","sources":["../../../src/client/context-xray/ContextMeter.tsx"],"names":[],"mappings":"AA4DA,wBAAgB,YAAY,CAAC,EAC3B,QAAQ,EACR,OAAc,GACf,EAAE;IACD,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB,2CA0HA"}
@@ -1,11 +1,13 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
- import { useEffect, useRef, useState } from "react";
2
+ import { lazy, Suspense, useEffect, useRef, useState } from "react";
3
3
  import { Popover, PopoverContent, PopoverTrigger, } from "../components/ui/popover.js";
4
4
  import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger, } from "../components/ui/tooltip.js";
5
5
  import { useActionMutation, useActionQuery } from "../use-action.js";
6
6
  import { cn } from "../utils.js";
7
- import { ContextXRayPanel } from "./ContextXRayPanel.js";
8
7
  import { CONTEXT_XRAY_MODEL_LIMIT, formatTokens } from "./format.js";
8
+ const ContextXRayPanel = lazy(() => import("./ContextXRayPanel.js").then((m) => ({
9
+ default: m.ContextXRayPanel,
10
+ })));
9
11
  function ContextDonut({ pct, advisory }) {
10
12
  const radius = 7.5;
11
13
  const circumference = 2 * Math.PI * radius;
@@ -65,6 +67,6 @@ export function ContextMeter({ threadId, enabled = true, }) {
65
67
  if (action === "restore")
66
68
  restore.mutate(params, options);
67
69
  };
68
- return (_jsx(TooltipProvider, { delayDuration: 200, children: _jsxs(Popover, { open: open, onOpenChange: setOpen, children: [_jsxs(Tooltip, { children: [_jsx(TooltipTrigger, { asChild: true, children: _jsx(PopoverTrigger, { asChild: true, children: _jsx("button", { type: "button", "aria-label": `Context ${pct}%, ${formatTokens(manifest.totalTokens)}. Open Context X-Ray.`, className: cn("flex size-7 shrink-0 items-center justify-center rounded-md text-muted-foreground transition-colors hover:bg-accent/50 hover:text-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring", open && "bg-accent/60 text-foreground"), children: _jsx(ContextDonut, { pct: pct, advisory: !manifest.enforceable }) }) }) }), _jsxs(TooltipContent, { children: ["Context ", pct, "% \u00B7 ", formatTokens(manifest.totalTokens)] })] }), _jsx(PopoverContent, { align: "end", side: "top", sideOffset: 8, className: "w-[min(92vw,380px)] overflow-hidden border-border/70 p-0", children: _jsx(ContextXRayPanel, { manifest: manifest, optimistic: optimistic, onPin: (segmentId) => mutateStatus(segmentId, "pinned", "pin"), onEvict: (segmentId) => mutateStatus(segmentId, "evicted", "evict"), onRestore: (segmentId) => mutateStatus(segmentId, "active", "restore") }) })] }) }));
70
+ return (_jsx(TooltipProvider, { delayDuration: 200, children: _jsxs(Popover, { open: open, onOpenChange: setOpen, children: [_jsxs(Tooltip, { children: [_jsx(TooltipTrigger, { asChild: true, children: _jsx(PopoverTrigger, { asChild: true, children: _jsx("button", { type: "button", "aria-label": `Context ${pct}%, ${formatTokens(manifest.totalTokens)}. Open Context X-Ray.`, className: cn("flex size-7 shrink-0 items-center justify-center rounded-md text-muted-foreground transition-colors hover:bg-accent/50 hover:text-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring", open && "bg-accent/60 text-foreground"), children: _jsx(ContextDonut, { pct: pct, advisory: !manifest.enforceable }) }) }) }), _jsxs(TooltipContent, { children: ["Context ", pct, "% \u00B7 ", formatTokens(manifest.totalTokens)] })] }), _jsx(PopoverContent, { align: "end", side: "top", sideOffset: 8, className: "w-[min(92vw,380px)] overflow-hidden border-border/70 p-0", children: open ? (_jsx(Suspense, { fallback: _jsx("div", { className: "flex h-52 items-center justify-center text-xs text-muted-foreground", children: "Loading context view\u2026" }), children: _jsx(ContextXRayPanel, { manifest: manifest, optimistic: optimistic, onPin: (segmentId) => mutateStatus(segmentId, "pinned", "pin"), onEvict: (segmentId) => mutateStatus(segmentId, "evicted", "evict"), onRestore: (segmentId) => mutateStatus(segmentId, "active", "restore") }) })) : null })] }) }));
69
71
  }
70
72
  //# sourceMappingURL=ContextMeter.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"ContextMeter.js","sourceRoot":"","sources":["../../../src/client/context-xray/ContextMeter.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAKpD,OAAO,EACL,OAAO,EACP,cAAc,EACd,cAAc,GACf,MAAM,6BAA6B,CAAC;AACrC,OAAO,EACL,OAAO,EACP,cAAc,EACd,eAAe,EACf,cAAc,GACf,MAAM,6BAA6B,CAAC;AACrC,OAAO,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AACrE,OAAO,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AACjC,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACzD,OAAO,EAAE,wBAAwB,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAErE,SAAS,YAAY,CAAC,EAAE,GAAG,EAAE,QAAQ,EAAsC;IACzE,MAAM,MAAM,GAAG,GAAG,CAAC;IACnB,MAAM,aAAa,GA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C,IAAI,EAAE,IAAI,EAAE,YAAY,EAAE,OAAO,aACxC,MAAC,OAAO,eACN,KAAC,cAAc,IAAC,OAAO,kBACrB,KAAC,cAAc,IAAC,OAAO,kBACrB,iBACE,IAAI,EAAC,QAAQ,gBACD,WAAW,GAAG,MAAM,YAAY,CAC1C,QAAQ,CAAC,WAAW,CACrB,uBAAuB,EACxB,SAAS,EAAE,EAAE,CACX,sNAAsN,EACtN,IAAI,IAAI,8BAA8B,CACvC,YAED,KAAC,YAAY,IAAC,GAAG,EAAE,GAAG,EAAE,QAAQ,EAAE,CAAC,QAAQ,CAAC,WAAW,GAAI,GACpD,GACM,GACF,EACjB,MAAC,cAAc,2BACJ,GAAG,eAAM,YAAY,CAAC,QAAQ,CAAC,WAAW,CAAC,IACrC,IACT,EACV,KAAC,cAAc,IACb,KAAK,EAAC,KAAK,EACX,IAAI,EAAC,KAAK,EACV,UAAU,EAAE,CAAC,EACb,SAAS,EAAC,0DAA0D,YAEpE,KAAC,gBAAgB,IACf,QAAQ,EAAE,QAAQ,EAClB,UAAU,EAAE,UAAU,EACtB,KAAK,EAAE,CAAC,SAAS,EAAE,EAAE,CAAC,YAAY,CAAC,SAAS,EAAE,QAAQ,EAAE,KAAK,CAAC,EAC9D,OAAO,EAAE,CAAC,SAAS,EAAE,EAAE,CAAC,YAAY,CAAC,SAAS,EAAE,SAAS,EAAE,OAAO,CAAC,EACnE,SAAS,EAAE,CAAC,SAAS,EAAE,EAAE,CACvB,YAAY,CAAC,SAAS,EAAE,QAAQ,EAAE,SAAS,CAAC,GAE9C,GACa,IACT,GACM,CACnB,CAAC;AACJ,CAAC","sourcesContent":["import { useEffect, useRef, useState } from \"react\";\nimport type {\n ContextManifest,\n ContextSegmentStatus,\n} from \"../../shared/context-xray.js\";\nimport {\n Popover,\n PopoverContent,\n PopoverTrigger,\n} from \"../components/ui/popover.js\";\nimport {\n Tooltip,\n TooltipContent,\n TooltipProvider,\n TooltipTrigger,\n} from \"../components/ui/tooltip.js\";\nimport { useActionMutation, useActionQuery } from \"../use-action.js\";\nimport { cn } from \"../utils.js\";\nimport { ContextXRayPanel } from \"./ContextXRayPanel.js\";\nimport { CONTEXT_XRAY_MODEL_LIMIT, formatTokens } from \"./format.js\";\n\nfunction ContextDonut({ pct, advisory }: { pct: number; advisory: boolean }) {\n const radius = 7.5;\n const circumference = 2 * Math.PI * radius;\n const displayPct = Math.max(3, Math.min(100, pct));\n const dashOffset = circumference - (displayPct / 100) * circumference;\n\n return (\n <span className=\"relative flex size-5 items-center justify-center\">\n <svg aria-hidden=\"true\" viewBox=\"0 0 20 20\" className=\"-rotate-90 size-5\">\n <circle\n cx=\"10\"\n cy=\"10\"\n r={radius}\n className=\"stroke-muted\"\n fill=\"none\"\n strokeWidth=\"3\"\n />\n <circle\n cx=\"10\"\n cy=\"10\"\n r={radius}\n className={cn(advisory ? \"stroke-amber-500\" : \"stroke-[#00B5FF]\")}\n fill=\"none\"\n strokeLinecap=\"round\"\n strokeWidth=\"3\"\n strokeDasharray={circumference}\n strokeDashoffset={dashOffset}\n />\n </svg>\n <span className=\"absolute size-2 rounded-full bg-background\" />\n </span>\n );\n}\n\nexport function ContextMeter({\n threadId,\n enabled = true,\n}: {\n threadId?: string | null;\n enabled?: boolean;\n}) {\n const [open, setOpen] = useState(false);\n const [optimistic, setOptimistic] = useState<\n Map<string, ContextSegmentStatus>\n >(new Map());\n const currentThreadId = useRef(threadId);\n const shouldQuery = Boolean(threadId && enabled);\n const query = useActionQuery(\n \"context-manifest-get\",\n shouldQuery && threadId ? { threadId } : undefined,\n {\n enabled: shouldQuery,\n staleTime: 1000,\n },\n ) as { data?: ContextManifest };\n const pin = useActionMutation(\"context-pin\");\n const evict = useActionMutation(\"context-evict\");\n const restore = useActionMutation(\"context-restore\");\n\n useEffect(() => {\n currentThreadId.current = threadId;\n setOptimistic(new Map());\n }, [threadId]);\n\n useEffect(() => {\n if (!threadId || !enabled || typeof window === \"undefined\") return;\n const params = new URLSearchParams(window.location.search);\n const wantsXray = params.get(\"contextXray\") === \"1\";\n const targetThread = params.get(\"threadId\");\n if (wantsXray && (!targetThread || targetThread === threadId)) {\n setOpen(true);\n }\n }, [enabled, threadId]);\n\n const manifest = query.data;\n const segments = manifest?.segments ?? [];\n const pct = manifest\n ? Math.min(\n 100,\n Math.round((manifest.totalTokens / CONTEXT_XRAY_MODEL_LIMIT) * 100),\n )\n : 0;\n\n if (!shouldQuery || !threadId || !manifest || manifest.rawTokens <= 0) {\n return null;\n }\n\n const mutateStatus = (\n segmentId: string,\n status: ContextSegmentStatus,\n action: \"pin\" | \"evict\" | \"restore\",\n ) => {\n const previous = new Map(optimistic);\n setOptimistic((prev) => new Map(prev).set(segmentId, status));\n const params = { threadId, segmentId };\n const options = {\n onError: () => {\n if (currentThreadId.current === threadId) {\n setOptimistic(previous);\n }\n },\n };\n if (action === \"pin\") pin.mutate(params, options);\n if (action === \"evict\") evict.mutate(params, options);\n if (action === \"restore\") restore.mutate(params, options);\n };\n\n return (\n <TooltipProvider delayDuration={200}>\n <Popover open={open} onOpenChange={setOpen}>\n <Tooltip>\n <TooltipTrigger asChild>\n <PopoverTrigger asChild>\n <button\n type=\"button\"\n aria-label={`Context ${pct}%, ${formatTokens(\n manifest.totalTokens,\n )}. Open Context X-Ray.`}\n className={cn(\n \"flex size-7 shrink-0 items-center justify-center rounded-md text-muted-foreground transition-colors hover:bg-accent/50 hover:text-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring\",\n open && \"bg-accent/60 text-foreground\",\n )}\n >\n <ContextDonut pct={pct} advisory={!manifest.enforceable} />\n </button>\n </PopoverTrigger>\n </TooltipTrigger>\n <TooltipContent>\n Context {pct}% · {formatTokens(manifest.totalTokens)}\n </TooltipContent>\n </Tooltip>\n <PopoverContent\n align=\"end\"\n side=\"top\"\n sideOffset={8}\n className=\"w-[min(92vw,380px)] overflow-hidden border-border/70 p-0\"\n >\n <ContextXRayPanel\n manifest={manifest}\n optimistic={optimistic}\n onPin={(segmentId) => mutateStatus(segmentId, \"pinned\", \"pin\")}\n onEvict={(segmentId) => mutateStatus(segmentId, \"evicted\", \"evict\")}\n onRestore={(segmentId) =>\n mutateStatus(segmentId, \"active\", \"restore\")\n }\n />\n </PopoverContent>\n </Popover>\n </TooltipProvider>\n );\n}\n"]}
1
+ {"version":3,"file":"ContextMeter.js","sourceRoot":"","sources":["../../../src/client/context-xray/ContextMeter.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAKpE,OAAO,EACL,OAAO,EACP,cAAc,EACd,cAAc,GACf,MAAM,6BAA6B,CAAC;AACrC,OAAO,EACL,OAAO,EACP,cAAc,EACd,eAAe,EACf,cAAc,GACf,MAAM,6BAA6B,CAAC;AACrC,OAAO,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AACrE,OAAO,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AACjC,OAAO,EAAE,wBAAwB,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAErE,MAAM,gBAAgB,GAAG,IAAI,CAAC,GAAG,EAAE,CACjC,MAAM,CAAC,uBAAuB,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;IAC3C,OAAO,EAAE,CAAC,CAAC,gBAAgB;CAC5B,CAAC,CAAC,CACJ,CAAC;AAEF,SAAS,YAAY,CAAC,EAAE,GAAG,EAAE,QAAQ,EAAsC;IACzE,MAAM,MAAM,GAAG,GAAG,CAAC;IACnB,MAAM,aAAa,GAAG,CAAC,GAAG,IAAI,CAAC,EAAE,GAAG,MAAM,CAAC;IAC3C,MAAM,UAAU,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;IACnD,MAAM,UAAU,GAAG,aAAa,GAAG,CAAC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rB,uBAAuB,EACxB,SAAS,EAAE,EAAE,CACX,sNAAsN,EACtN,IAAI,IAAI,8BAA8B,CACvC,YAED,KAAC,YAAY,IAAC,GAAG,EAAE,GAAG,EAAE,QAAQ,EAAE,CAAC,QAAQ,CAAC,WAAW,GAAI,GACpD,GACM,GACF,EACjB,MAAC,cAAc,2BACJ,GAAG,eAAM,YAAY,CAAC,QAAQ,CAAC,WAAW,CAAC,IACrC,IACT,EACV,KAAC,cAAc,IACb,KAAK,EAAC,KAAK,EACX,IAAI,EAAC,KAAK,EACV,UAAU,EAAE,CAAC,EACb,SAAS,EAAC,0DAA0D,YAEnE,IAAI,CAAC,CAAC,CAAC,CACN,KAAC,QAAQ,IACP,QAAQ,EACN,cAAK,SAAS,EAAC,qEAAqE,2CAE9E,YAGR,KAAC,gBAAgB,IACf,QAAQ,EAAE,QAAQ,EAClB,UAAU,EAAE,UAAU,EACtB,KAAK,EAAE,CAAC,SAAS,EAAE,EAAE,CAAC,YAAY,CAAC,SAAS,EAAE,QAAQ,EAAE,KAAK,CAAC,EAC9D,OAAO,EAAE,CAAC,SAAS,EAAE,EAAE,CACrB,YAAY,CAAC,SAAS,EAAE,SAAS,EAAE,OAAO,CAAC,EAE7C,SAAS,EAAE,CAAC,SAAS,EAAE,EAAE,CACvB,YAAY,CAAC,SAAS,EAAE,QAAQ,EAAE,SAAS,CAAC,GAE9C,GACO,CACZ,CAAC,CAAC,CAAC,IAAI,GACO,IACT,GACM,CACnB,CAAC;AACJ,CAAC","sourcesContent":["import { lazy, Suspense, useEffect, useRef, useState } from \"react\";\nimport type {\n ContextManifest,\n ContextSegmentStatus,\n} from \"../../shared/context-xray.js\";\nimport {\n Popover,\n PopoverContent,\n PopoverTrigger,\n} from \"../components/ui/popover.js\";\nimport {\n Tooltip,\n TooltipContent,\n TooltipProvider,\n TooltipTrigger,\n} from \"../components/ui/tooltip.js\";\nimport { useActionMutation, useActionQuery } from \"../use-action.js\";\nimport { cn } from \"../utils.js\";\nimport { CONTEXT_XRAY_MODEL_LIMIT, formatTokens } from \"./format.js\";\n\nconst ContextXRayPanel = lazy(() =>\n import(\"./ContextXRayPanel.js\").then((m) => ({\n default: m.ContextXRayPanel,\n })),\n);\n\nfunction ContextDonut({ pct, advisory }: { pct: number; advisory: boolean }) {\n const radius = 7.5;\n const circumference = 2 * Math.PI * radius;\n const displayPct = Math.max(3, Math.min(100, pct));\n const dashOffset = circumference - (displayPct / 100) * circumference;\n\n return (\n <span className=\"relative flex size-5 items-center justify-center\">\n <svg aria-hidden=\"true\" viewBox=\"0 0 20 20\" className=\"-rotate-90 size-5\">\n <circle\n cx=\"10\"\n cy=\"10\"\n r={radius}\n className=\"stroke-muted\"\n fill=\"none\"\n strokeWidth=\"3\"\n />\n <circle\n cx=\"10\"\n cy=\"10\"\n r={radius}\n className={cn(advisory ? \"stroke-amber-500\" : \"stroke-[#00B5FF]\")}\n fill=\"none\"\n strokeLinecap=\"round\"\n strokeWidth=\"3\"\n strokeDasharray={circumference}\n strokeDashoffset={dashOffset}\n />\n </svg>\n <span className=\"absolute size-2 rounded-full bg-background\" />\n </span>\n );\n}\n\nexport function ContextMeter({\n threadId,\n enabled = true,\n}: {\n threadId?: string | null;\n enabled?: boolean;\n}) {\n const [open, setOpen] = useState(false);\n const [optimistic, setOptimistic] = useState<\n Map<string, ContextSegmentStatus>\n >(new Map());\n const currentThreadId = useRef(threadId);\n const shouldQuery = Boolean(threadId && enabled);\n const query = useActionQuery(\n \"context-manifest-get\",\n shouldQuery && threadId ? { threadId } : undefined,\n {\n enabled: shouldQuery,\n staleTime: 1000,\n },\n ) as { data?: ContextManifest };\n const pin = useActionMutation(\"context-pin\");\n const evict = useActionMutation(\"context-evict\");\n const restore = useActionMutation(\"context-restore\");\n\n useEffect(() => {\n currentThreadId.current = threadId;\n setOptimistic(new Map());\n }, [threadId]);\n\n useEffect(() => {\n if (!threadId || !enabled || typeof window === \"undefined\") return;\n const params = new URLSearchParams(window.location.search);\n const wantsXray = params.get(\"contextXray\") === \"1\";\n const targetThread = params.get(\"threadId\");\n if (wantsXray && (!targetThread || targetThread === threadId)) {\n setOpen(true);\n }\n }, [enabled, threadId]);\n\n const manifest = query.data;\n const segments = manifest?.segments ?? [];\n const pct = manifest\n ? Math.min(\n 100,\n Math.round((manifest.totalTokens / CONTEXT_XRAY_MODEL_LIMIT) * 100),\n )\n : 0;\n\n if (!shouldQuery || !threadId || !manifest || manifest.rawTokens <= 0) {\n return null;\n }\n\n const mutateStatus = (\n segmentId: string,\n status: ContextSegmentStatus,\n action: \"pin\" | \"evict\" | \"restore\",\n ) => {\n const previous = new Map(optimistic);\n setOptimistic((prev) => new Map(prev).set(segmentId, status));\n const params = { threadId, segmentId };\n const options = {\n onError: () => {\n if (currentThreadId.current === threadId) {\n setOptimistic(previous);\n }\n },\n };\n if (action === \"pin\") pin.mutate(params, options);\n if (action === \"evict\") evict.mutate(params, options);\n if (action === \"restore\") restore.mutate(params, options);\n };\n\n return (\n <TooltipProvider delayDuration={200}>\n <Popover open={open} onOpenChange={setOpen}>\n <Tooltip>\n <TooltipTrigger asChild>\n <PopoverTrigger asChild>\n <button\n type=\"button\"\n aria-label={`Context ${pct}%, ${formatTokens(\n manifest.totalTokens,\n )}. Open Context X-Ray.`}\n className={cn(\n \"flex size-7 shrink-0 items-center justify-center rounded-md text-muted-foreground transition-colors hover:bg-accent/50 hover:text-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring\",\n open && \"bg-accent/60 text-foreground\",\n )}\n >\n <ContextDonut pct={pct} advisory={!manifest.enforceable} />\n </button>\n </PopoverTrigger>\n </TooltipTrigger>\n <TooltipContent>\n Context {pct}% · {formatTokens(manifest.totalTokens)}\n </TooltipContent>\n </Tooltip>\n <PopoverContent\n align=\"end\"\n side=\"top\"\n sideOffset={8}\n className=\"w-[min(92vw,380px)] overflow-hidden border-border/70 p-0\"\n >\n {open ? (\n <Suspense\n fallback={\n <div className=\"flex h-52 items-center justify-center text-xs text-muted-foreground\">\n Loading context view…\n </div>\n }\n >\n <ContextXRayPanel\n manifest={manifest}\n optimistic={optimistic}\n onPin={(segmentId) => mutateStatus(segmentId, \"pinned\", \"pin\")}\n onEvict={(segmentId) =>\n mutateStatus(segmentId, \"evicted\", \"evict\")\n }\n onRestore={(segmentId) =>\n mutateStatus(segmentId, \"active\", \"restore\")\n }\n />\n </Suspense>\n ) : null}\n </PopoverContent>\n </Popover>\n </TooltipProvider>\n );\n}\n"]}
@@ -8,6 +8,16 @@ type NitroPluginDef = (nitroApp: any) => void | Promise<void>;
8
8
  * regex with subtly different shapes.
9
9
  */
10
10
  export declare function isDuplicateColumnError(err: unknown): boolean;
11
+ /**
12
+ * True when a migration statement failed because the connected DB ROLE lacks
13
+ * privilege — e.g. a permission-limited dev/replica role that doesn't own the
14
+ * table. Postgres raises SQLSTATE 42501 ("insufficient_privilege", routine
15
+ * aclcheck_error, message "must be owner of table …"). We treat these as
16
+ * NON-FATAL so a perms-limited database can't crash-loop the whole server: the
17
+ * migration is skipped (left unrecorded) and a properly-privileged role applies
18
+ * it later. Production, where the role owns its tables, never hits this path.
19
+ */
20
+ export declare function isPermissionError(err: unknown): boolean;
11
21
  export interface RunMigrationsOptions {
12
22
  /**
13
23
  * Name of the migrations bookkeeping table. REQUIRED — there is intentionally
@@ -1 +1 @@
1
- {"version":3,"file":"migrations.d.ts","sourceRoot":"","sources":["../../src/db/migrations.ts"],"names":[],"mappings":"AAOA,KAAK,cAAc,GAAG,CAAC,QAAQ,EAAE,GAAG,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;AA4B9D;;;;;;;GAOG;AACH,wBAAgB,sBAAsB,CAAC,GAAG,EAAE,OAAO,GAAG,OAAO,CAK5D;AAmDD,MAAM,WAAW,oBAAoB;IACnC;;;;;;;;;;;;OAYG;IACH,KAAK,EAAE,MAAM,CAAC;CACf;AAED;;;;;;;;;GASG;AACH,MAAM,MAAM,YAAY,GAAG,MAAM,GAAG;IAAE,QAAQ,CAAC,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAE3E,MAAM,WAAW,cAAc;IAC7B,OAAO,EAAE,MAAM,CAAC;IAChB,GAAG,EAAE,YAAY,CAAC;CACnB;AAQD,wBAAgB,aAAa,CAC3B,UAAU,EAAE,KAAK,CAAC,cAAc,CAAC,EACjC,OAAO,EAAE,oBAAoB,GAC5B,cAAc,CAuLhB"}
1
+ {"version":3,"file":"migrations.d.ts","sourceRoot":"","sources":["../../src/db/migrations.ts"],"names":[],"mappings":"AAOA,KAAK,cAAc,GAAG,CAAC,QAAQ,EAAE,GAAG,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;AA4B9D;;;;;;;GAOG;AACH,wBAAgB,sBAAsB,CAAC,GAAG,EAAE,OAAO,GAAG,OAAO,CAK5D;AAED;;;;;;;;GAQG;AACH,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,OAAO,GAAG,OAAO,CASvD;AAmDD,MAAM,WAAW,oBAAoB;IACnC;;;;;;;;;;;;OAYG;IACH,KAAK,EAAE,MAAM,CAAC;CACf;AAED;;;;;;;;;GASG;AACH,MAAM,MAAM,YAAY,GAAG,MAAM,GAAG;IAAE,QAAQ,CAAC,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAE3E,MAAM,WAAW,cAAc;IAC7B,OAAO,EAAE,MAAM,CAAC;IAChB,GAAG,EAAE,YAAY,CAAC;CACnB;AAQD,wBAAgB,aAAa,CAC3B,UAAU,EAAE,KAAK,CAAC,cAAc,CAAC,EACjC,OAAO,EAAE,oBAAoB,GAC5B,cAAc,CAyMhB"}
@@ -34,6 +34,24 @@ export function isDuplicateColumnError(err) {
34
34
  const msg = err?.message ?? "";
35
35
  return (/duplicate column name/i.test(msg) || /column .* already exists/i.test(msg));
36
36
  }
37
+ /**
38
+ * True when a migration statement failed because the connected DB ROLE lacks
39
+ * privilege — e.g. a permission-limited dev/replica role that doesn't own the
40
+ * table. Postgres raises SQLSTATE 42501 ("insufficient_privilege", routine
41
+ * aclcheck_error, message "must be owner of table …"). We treat these as
42
+ * NON-FATAL so a perms-limited database can't crash-loop the whole server: the
43
+ * migration is skipped (left unrecorded) and a properly-privileged role applies
44
+ * it later. Production, where the role owns its tables, never hits this path.
45
+ */
46
+ export function isPermissionError(err) {
47
+ const e = err;
48
+ if (e?.code === "42501")
49
+ return true;
50
+ const msg = e?.message ?? "";
51
+ return (/must be owner of/i.test(msg) ||
52
+ /permission denied/i.test(msg) ||
53
+ /insufficient privilege/i.test(msg));
54
+ }
37
55
  /**
38
56
  * Split a multi-statement SQL blob into individual statements.
39
57
  *
@@ -221,6 +239,20 @@ export function runMigrations(migrations, options) {
221
239
  console.log(`[db] Applied migration v${m.version} (${statements.length} statement${statements.length === 1 ? "" : "s"})`);
222
240
  }
223
241
  catch (err) {
242
+ if (pg && isPermissionError(err)) {
243
+ // The connected role lacks privilege for this migration (e.g. a
244
+ // permission-limited dev/replica role that doesn't own the table).
245
+ // Don't crash-loop the whole server over it — warn and STOP here.
246
+ // We must NOT continue to later migrations: pending work is computed
247
+ // as `version > MAX(recorded version)`, so applying a later migration
248
+ // would advance MAX past this unrecorded one and orphan it forever.
249
+ // Stopping leaves MAX at the last recorded version, so a properly-
250
+ // privileged role resumes from this exact migration, in order.
251
+ console.warn(`[db] Migration v${m.version} skipped — insufficient privilege: ${err.message}. ` +
252
+ `Apply it with a DB role that owns the table. ` +
253
+ `Halting further migrations so this one isn't orphaned.`, "\nStatement:", currentStmt);
254
+ break;
255
+ }
224
256
  console.error(`[db] Migration v${m.version} FAILED:`, err.message, "\nStatement:", currentStmt);
225
257
  throw err;
226
258
  }
@@ -1 +1 @@
1
- {"version":3,"file":"migrations.js","sourceRoot":"","sources":["../../src/db/migrations.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,EACT,UAAU,EACV,UAAU,EACV,eAAe,GAChB,MAAM,aAAa,CAAC;AA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ourcesContent":["import {\n getDbExec,\n isPostgres,\n getDialect,\n retrySqliteBusy,\n} from \"./client.js\";\n\ntype NitroPluginDef = (nitroApp: any) => void | Promise<void>;\n\n/**\n * Rewrite SQLite-specific SQL to Postgres-compatible equivalents.\n * Handles: datetime('now') → CURRENT_TIMESTAMP, AUTOINCREMENT → GENERATED, etc.\n */\nfunction adaptSqlForPostgres(sql: string): string {\n return sql\n .replace(/datetime\\s*\\(\\s*'now'\\s*\\)/gi, \"CURRENT_TIMESTAMP\")\n .replace(/\\bAUTOINCREMENT\\b/gi, \"\")\n .replace(/\\bINTEGER\\b/gi, \"BIGINT\");\n}\n\nconst IF_NOT_EXISTS_ADD_COLUMN_RE = /ADD\\s+COLUMN\\s+IF\\s+NOT\\s+EXISTS/i;\n\n/**\n * Strip Postgres-only syntax that SQLite doesn't support.\n * Handles: ALTER TABLE ... ADD COLUMN IF NOT EXISTS → ADD COLUMN\n *\n * Note: SQLite does not have a native equivalent, so the idempotent\n * semantic is emulated at the executor level by swallowing the\n * \"duplicate column name\" error for statements that originally carried\n * the clause. See `hadIfNotExists` tracking in the run loop.\n */\nfunction adaptSqlForSqlite(sql: string): string {\n return sql.replace(/ADD\\s+COLUMN\\s+IF\\s+NOT\\s+EXISTS/gi, \"ADD COLUMN\");\n}\n\n/**\n * True when an error from `ALTER TABLE ... ADD COLUMN` indicates the\n * column already existed. Recognizes both SQLite (\"duplicate column\n * name\") and Postgres (\"column ... already exists\" — exact text varies\n * by error code 42701, but the substring is stable). Exported so other\n * idempotent column-upgrade loops in the codebase don't reinvent this\n * regex with subtly different shapes.\n */\nexport function isDuplicateColumnError(err: unknown): boolean {\n const msg = (err as Error | undefined)?.message ?? \"\";\n return (\n /duplicate column name/i.test(msg) || /column .* already exists/i.test(msg)\n );\n}\n\n/**\n * Split a multi-statement SQL blob into individual statements.\n *\n * libsql's `execute(sql)` only runs the first statement in a multi-statement\n * string. This splitter is intentionally simple: it respects single-quoted\n * string literals (with `''` escaping) and `--` line comments, and splits on\n * top-level `;`. It does NOT attempt to parse `$$`-quoted Postgres function\n * bodies — migrations that define functions/triggers with `;` inside bodies\n * should pass a single-statement migration per entry instead.\n */\nfunction splitSqlStatements(sql: string): string[] {\n const out: string[] = [];\n let buf = \"\";\n let i = 0;\n let inSingle = false;\n while (i < sql.length) {\n const ch = sql[i];\n const next = sql[i + 1];\n if (!inSingle && ch === \"-\" && next === \"-\") {\n // Skip to end of line\n while (i < sql.length && sql[i] !== \"\\n\") i++;\n continue;\n }\n if (ch === \"'\") {\n buf += ch;\n if (inSingle && next === \"'\") {\n buf += next;\n i += 2;\n continue;\n }\n inSingle = !inSingle;\n i++;\n continue;\n }\n if (ch === \";\" && !inSingle) {\n const trimmed = buf.trim();\n if (trimmed) out.push(trimmed);\n buf = \"\";\n i++;\n continue;\n }\n buf += ch;\n i++;\n }\n const tail = buf.trim();\n if (tail) out.push(tail);\n return out;\n}\n\nexport interface RunMigrationsOptions {\n /**\n * Name of the migrations bookkeeping table. REQUIRED — there is intentionally\n * no default. Two templates that share a database (e.g. via the same Neon URL)\n * each have their own version space starting at v1, and a single shared\n * `_migrations` table will silently skip the second template's migrations if\n * the first has already advanced past those version numbers. This caused the\n * design template's migrations to be skipped entirely on a Neon DB that\n * slides had already populated up to v15 (PR #320 era).\n *\n * Use one bookkeeping table per template, e.g. `slides_migrations`. Core\n * feature plugins (e.g. the org module) follow the same convention with\n * their own prefix, e.g. `_org_migrations`.\n */\n table: string;\n}\n\n/**\n * A single migration entry.\n *\n * `sql` can be a string (runs on every dialect) or an object with dialect\n * keys for dialect-gated SQL. Useful when Postgres needs an ALTER that\n * SQLite can't parse.\n *\n * { version: 14, sql: { postgres: \"ALTER TABLE …\" } } // no-op on sqlite\n * { version: 15, sql: { sqlite: \"…\", postgres: \"…\" } } // both dialects\n */\nexport type MigrationSql = string | { postgres?: string; sqlite?: string };\n\nexport interface MigrationEntry {\n version: number;\n sql: MigrationSql;\n}\n\nfunction resolveMigrationSql(sql: MigrationSql, pg: boolean): string | null {\n if (typeof sql === \"string\") return sql;\n const raw = pg ? sql.postgres : sql.sqlite;\n return raw ?? null;\n}\n\nexport function runMigrations(\n migrations: Array<MigrationEntry>,\n options: RunMigrationsOptions,\n): NitroPluginDef {\n const table = options?.table;\n if (\n !table ||\n typeof table !== \"string\" ||\n !/^[A-Za-z_][A-Za-z0-9_]*$/.test(table)\n ) {\n throw new Error(\n \"runMigrations: `table` option is required and must be a valid SQL identifier \" +\n '(e.g. `{ table: \"slides_migrations\" }`). See packages/core/src/db/migrations.ts ' +\n \"for why this is required (shared-DB version-collision bug).\",\n );\n }\n return async () => {\n try {\n // Check for Cloudflare D1 binding (only if DATABASE_URL not set)\n const d1 = getDialect() === \"d1\" ? globalThis.__cf_env?.DB : null;\n if (d1) {\n await d1\n .prepare(\n `CREATE TABLE IF NOT EXISTS ${table} (version INTEGER PRIMARY KEY)`,\n )\n .run();\n const firstRow = await d1\n .prepare(`SELECT MAX(version) as v FROM ${table}`)\n .first<{ v?: number }>();\n const current = (firstRow?.v as number) ?? 0;\n\n for (const m of migrations.filter((m) => m.version > current)) {\n try {\n // D1 is SQLite-compatible\n const raw = resolveMigrationSql(m.sql, false);\n if (raw == null) {\n await d1\n .prepare(`INSERT OR IGNORE INTO ${table} VALUES (?)`)\n .bind(m.version)\n .run();\n continue;\n }\n const originalStatements = splitSqlStatements(raw);\n const statements = originalStatements.map((orig) => ({\n sql: adaptSqlForSqlite(orig),\n hadIfNotExists: IF_NOT_EXISTS_ADD_COLUMN_RE.test(orig),\n }));\n const hasIfNotExists = statements.some((s) => s.hadIfNotExists);\n if (hasIfNotExists) {\n // Per-statement path: we need to swallow \"duplicate column\"\n // errors for statements that originally carried\n // `ADD COLUMN IF NOT EXISTS`, which a batch() can't express.\n // Loses atomicity, but the idempotent-ADD-COLUMN semantic\n // means a partial re-run resolves cleanly on retry.\n for (const { sql: stmt, hadIfNotExists } of statements) {\n try {\n await d1.prepare(stmt).run();\n } catch (err) {\n if (hadIfNotExists && isDuplicateColumnError(err)) continue;\n throw err;\n }\n }\n await d1\n .prepare(`INSERT OR IGNORE INTO ${table} VALUES (?)`)\n .bind(m.version)\n .run();\n } else {\n // Atomic batch: all statements + version-row insert land in\n // the same transaction. A failing statement rolls the whole\n // migration back, so we never record a half-applied version.\n await d1.batch([\n ...statements.map((s) => d1.prepare(s.sql)),\n d1\n .prepare(`INSERT OR IGNORE INTO ${table} VALUES (?)`)\n .bind(m.version),\n ]);\n }\n console.log(\n `[db] Applied migration v${m.version} (${statements.length} statement${statements.length === 1 ? \"\" : \"s\"})`,\n );\n } catch (err) {\n console.error(\n `[db] Migration v${m.version} FAILED:`,\n (err as Error).message,\n \"\\nSQL:\",\n JSON.stringify(m.sql),\n );\n throw err;\n }\n }\n return;\n }\n\n // Generic path — works for libsql and Postgres\n const exec = getDbExec();\n const pg = isPostgres();\n\n // Retry initial table creation — SQLITE_BUSY_RECOVERY can occur on HMR\n // restarts when WAL files from the previous process haven't been released yet.\n await retrySqliteBusy(\n () =>\n exec.execute(\n `CREATE TABLE IF NOT EXISTS ${table} (version INTEGER PRIMARY KEY)`,\n ),\n { maxAttempts: 6, baseDelayMs: 1000, rethrow: true },\n );\n\n const { rows } = await exec.execute(\n `SELECT MAX(version) as v FROM ${table}`,\n );\n const current = (rows[0]?.v as number) ?? 0;\n\n const insertSql = pg\n ? `INSERT INTO ${table} VALUES (?) ON CONFLICT DO NOTHING`\n : `INSERT OR IGNORE INTO ${table} VALUES (?)`;\n\n const pending = migrations.filter((m) => m.version > current);\n if (pending.length > 0) {\n console.log(\n `[db] Applying ${pending.length} migration(s) on ${pg ? \"Postgres\" : \"SQLite/libsql\"}…`,\n );\n }\n\n for (const m of pending) {\n const raw = resolveMigrationSql(m.sql, pg);\n if (raw == null) {\n // Dialect-gated migration with no SQL for this dialect; still mark\n // as applied so we don't retry forever.\n await exec.execute({ sql: insertSql, args: [m.version] });\n continue;\n }\n // Split BEFORE adapting so we can remember which original statements\n // carried `ADD COLUMN IF NOT EXISTS` — SQLite drops the clause, so we\n // emulate the idempotent semantic by swallowing duplicate-column\n // errors only for those statements.\n const originalStatements = splitSqlStatements(raw);\n const statements = originalStatements.map((orig) => ({\n sql: pg ? adaptSqlForPostgres(orig) : adaptSqlForSqlite(orig),\n hadIfNotExists: IF_NOT_EXISTS_ADD_COLUMN_RE.test(orig),\n }));\n let currentStmt = \"\";\n try {\n for (const { sql: stmt, hadIfNotExists } of statements) {\n currentStmt = stmt;\n try {\n await exec.execute(stmt);\n } catch (err) {\n if (!pg && hadIfNotExists && isDuplicateColumnError(err)) {\n // IF NOT EXISTS semantic: column already present, skip.\n continue;\n }\n throw err;\n }\n }\n await exec.execute({ sql: insertSql, args: [m.version] });\n console.log(\n `[db] Applied migration v${m.version} (${statements.length} statement${statements.length === 1 ? \"\" : \"s\"})`,\n );\n } catch (err) {\n console.error(\n `[db] Migration v${m.version} FAILED:`,\n (err as Error).message,\n \"\\nStatement:\",\n currentStmt,\n );\n throw err;\n }\n }\n } catch (err) {\n console.error(\"[db] Migration failed:\", (err as Error).message);\n // In local dev, hard-fail so the developer catches errors immediately.\n // On serverless runtimes (Netlify Functions, Vercel, CF Workers) we\n // keep the process alive — the app will return 500s for routes that\n // depend on the missing tables, but at least other routes still work.\n // Note: Node.js 21+ defines globalThis.navigator, so we check for\n // serverless env vars instead of navigator presence.\n const isServerless =\n !!globalThis.process?.env?.NETLIFY ||\n !!globalThis.process?.env?.AWS_LAMBDA_FUNCTION_NAME ||\n !!globalThis.process?.env?.VERCEL ||\n \"__cf_env\" in globalThis;\n if (typeof globalThis.process?.exit === \"function\" && !isServerless) {\n process.exit(1);\n }\n }\n };\n}\n"]}
1
+ {"version":3,"file":"migrations.js","sourceRoot":"","sources":["../../src/db/migrations.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,EACT,UAAU,EACV,UAAU,EACV,eAAe,GAChB,MAAM,aAAa,CAAC;AAIrB;;;GAGG;AACH,SAAS,mBAAmB,CAAC,GAAW;IACtC,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O,EACtB,cAAc,EACd,WAAW,CACZ,CAAC;oBACF,MAAM,GAAG,CAAC;gBACZ,CAAC;YACH,CAAC;QACH,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,OAAO,CAAC,KAAK,CAAC,wBAAwB,EAAG,GAAa,CAAC,OAAO,CAAC,CAAC;YAChE,uEAAuE;YACvE,oEAAoE;YACpE,oEAAoE;YACpE,sEAAsE;YACtE,kEAAkE;YAClE,qDAAqD;YACrD,MAAM,YAAY,GAChB,CAAC,CAAC,UAAU,CAAC,OAAO,EAAE,GAAG,EAAE,OAAO;gBAClC,CAAC,CAAC,UAAU,CAAC,OAAO,EAAE,GAAG,EAAE,wBAAwB;gBACnD,CAAC,CAAC,UAAU,CAAC,OAAO,EAAE,GAAG,EAAE,MAAM;gBACjC,UAAU,IAAI,UAAU,CAAC;YAC3B,IAAI,OAAO,UAAU,CAAC,OAAO,EAAE,IAAI,KAAK,UAAU,IAAI,CAAC,YAAY,EAAE,CAAC;gBACpE,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;YAClB,CAAC;QACH,CAAC;IACH,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import {\n getDbExec,\n isPostgres,\n getDialect,\n retrySqliteBusy,\n} from \"./client.js\";\n\ntype NitroPluginDef = (nitroApp: any) => void | Promise<void>;\n\n/**\n * Rewrite SQLite-specific SQL to Postgres-compatible equivalents.\n * Handles: datetime('now') → CURRENT_TIMESTAMP, AUTOINCREMENT → GENERATED, etc.\n */\nfunction adaptSqlForPostgres(sql: string): string {\n return sql\n .replace(/datetime\\s*\\(\\s*'now'\\s*\\)/gi, \"CURRENT_TIMESTAMP\")\n .replace(/\\bAUTOINCREMENT\\b/gi, \"\")\n .replace(/\\bINTEGER\\b/gi, \"BIGINT\");\n}\n\nconst IF_NOT_EXISTS_ADD_COLUMN_RE = /ADD\\s+COLUMN\\s+IF\\s+NOT\\s+EXISTS/i;\n\n/**\n * Strip Postgres-only syntax that SQLite doesn't support.\n * Handles: ALTER TABLE ... ADD COLUMN IF NOT EXISTS → ADD COLUMN\n *\n * Note: SQLite does not have a native equivalent, so the idempotent\n * semantic is emulated at the executor level by swallowing the\n * \"duplicate column name\" error for statements that originally carried\n * the clause. See `hadIfNotExists` tracking in the run loop.\n */\nfunction adaptSqlForSqlite(sql: string): string {\n return sql.replace(/ADD\\s+COLUMN\\s+IF\\s+NOT\\s+EXISTS/gi, \"ADD COLUMN\");\n}\n\n/**\n * True when an error from `ALTER TABLE ... ADD COLUMN` indicates the\n * column already existed. Recognizes both SQLite (\"duplicate column\n * name\") and Postgres (\"column ... already exists\" — exact text varies\n * by error code 42701, but the substring is stable). Exported so other\n * idempotent column-upgrade loops in the codebase don't reinvent this\n * regex with subtly different shapes.\n */\nexport function isDuplicateColumnError(err: unknown): boolean {\n const msg = (err as Error | undefined)?.message ?? \"\";\n return (\n /duplicate column name/i.test(msg) || /column .* already exists/i.test(msg)\n );\n}\n\n/**\n * True when a migration statement failed because the connected DB ROLE lacks\n * privilege — e.g. a permission-limited dev/replica role that doesn't own the\n * table. Postgres raises SQLSTATE 42501 (\"insufficient_privilege\", routine\n * aclcheck_error, message \"must be owner of table …\"). We treat these as\n * NON-FATAL so a perms-limited database can't crash-loop the whole server: the\n * migration is skipped (left unrecorded) and a properly-privileged role applies\n * it later. Production, where the role owns its tables, never hits this path.\n */\nexport function isPermissionError(err: unknown): boolean {\n const e = err as { code?: string; message?: string } | undefined;\n if (e?.code === \"42501\") return true;\n const msg = e?.message ?? \"\";\n return (\n /must be owner of/i.test(msg) ||\n /permission denied/i.test(msg) ||\n /insufficient privilege/i.test(msg)\n );\n}\n\n/**\n * Split a multi-statement SQL blob into individual statements.\n *\n * libsql's `execute(sql)` only runs the first statement in a multi-statement\n * string. This splitter is intentionally simple: it respects single-quoted\n * string literals (with `''` escaping) and `--` line comments, and splits on\n * top-level `;`. It does NOT attempt to parse `$$`-quoted Postgres function\n * bodies — migrations that define functions/triggers with `;` inside bodies\n * should pass a single-statement migration per entry instead.\n */\nfunction splitSqlStatements(sql: string): string[] {\n const out: string[] = [];\n let buf = \"\";\n let i = 0;\n let inSingle = false;\n while (i < sql.length) {\n const ch = sql[i];\n const next = sql[i + 1];\n if (!inSingle && ch === \"-\" && next === \"-\") {\n // Skip to end of line\n while (i < sql.length && sql[i] !== \"\\n\") i++;\n continue;\n }\n if (ch === \"'\") {\n buf += ch;\n if (inSingle && next === \"'\") {\n buf += next;\n i += 2;\n continue;\n }\n inSingle = !inSingle;\n i++;\n continue;\n }\n if (ch === \";\" && !inSingle) {\n const trimmed = buf.trim();\n if (trimmed) out.push(trimmed);\n buf = \"\";\n i++;\n continue;\n }\n buf += ch;\n i++;\n }\n const tail = buf.trim();\n if (tail) out.push(tail);\n return out;\n}\n\nexport interface RunMigrationsOptions {\n /**\n * Name of the migrations bookkeeping table. REQUIRED — there is intentionally\n * no default. Two templates that share a database (e.g. via the same Neon URL)\n * each have their own version space starting at v1, and a single shared\n * `_migrations` table will silently skip the second template's migrations if\n * the first has already advanced past those version numbers. This caused the\n * design template's migrations to be skipped entirely on a Neon DB that\n * slides had already populated up to v15 (PR #320 era).\n *\n * Use one bookkeeping table per template, e.g. `slides_migrations`. Core\n * feature plugins (e.g. the org module) follow the same convention with\n * their own prefix, e.g. `_org_migrations`.\n */\n table: string;\n}\n\n/**\n * A single migration entry.\n *\n * `sql` can be a string (runs on every dialect) or an object with dialect\n * keys for dialect-gated SQL. Useful when Postgres needs an ALTER that\n * SQLite can't parse.\n *\n * { version: 14, sql: { postgres: \"ALTER TABLE …\" } } // no-op on sqlite\n * { version: 15, sql: { sqlite: \"…\", postgres: \"…\" } } // both dialects\n */\nexport type MigrationSql = string | { postgres?: string; sqlite?: string };\n\nexport interface MigrationEntry {\n version: number;\n sql: MigrationSql;\n}\n\nfunction resolveMigrationSql(sql: MigrationSql, pg: boolean): string | null {\n if (typeof sql === \"string\") return sql;\n const raw = pg ? sql.postgres : sql.sqlite;\n return raw ?? null;\n}\n\nexport function runMigrations(\n migrations: Array<MigrationEntry>,\n options: RunMigrationsOptions,\n): NitroPluginDef {\n const table = options?.table;\n if (\n !table ||\n typeof table !== \"string\" ||\n !/^[A-Za-z_][A-Za-z0-9_]*$/.test(table)\n ) {\n throw new Error(\n \"runMigrations: `table` option is required and must be a valid SQL identifier \" +\n '(e.g. `{ table: \"slides_migrations\" }`). See packages/core/src/db/migrations.ts ' +\n \"for why this is required (shared-DB version-collision bug).\",\n );\n }\n return async () => {\n try {\n // Check for Cloudflare D1 binding (only if DATABASE_URL not set)\n const d1 = getDialect() === \"d1\" ? globalThis.__cf_env?.DB : null;\n if (d1) {\n await d1\n .prepare(\n `CREATE TABLE IF NOT EXISTS ${table} (version INTEGER PRIMARY KEY)`,\n )\n .run();\n const firstRow = await d1\n .prepare(`SELECT MAX(version) as v FROM ${table}`)\n .first<{ v?: number }>();\n const current = (firstRow?.v as number) ?? 0;\n\n for (const m of migrations.filter((m) => m.version > current)) {\n try {\n // D1 is SQLite-compatible\n const raw = resolveMigrationSql(m.sql, false);\n if (raw == null) {\n await d1\n .prepare(`INSERT OR IGNORE INTO ${table} VALUES (?)`)\n .bind(m.version)\n .run();\n continue;\n }\n const originalStatements = splitSqlStatements(raw);\n const statements = originalStatements.map((orig) => ({\n sql: adaptSqlForSqlite(orig),\n hadIfNotExists: IF_NOT_EXISTS_ADD_COLUMN_RE.test(orig),\n }));\n const hasIfNotExists = statements.some((s) => s.hadIfNotExists);\n if (hasIfNotExists) {\n // Per-statement path: we need to swallow \"duplicate column\"\n // errors for statements that originally carried\n // `ADD COLUMN IF NOT EXISTS`, which a batch() can't express.\n // Loses atomicity, but the idempotent-ADD-COLUMN semantic\n // means a partial re-run resolves cleanly on retry.\n for (const { sql: stmt, hadIfNotExists } of statements) {\n try {\n await d1.prepare(stmt).run();\n } catch (err) {\n if (hadIfNotExists && isDuplicateColumnError(err)) continue;\n throw err;\n }\n }\n await d1\n .prepare(`INSERT OR IGNORE INTO ${table} VALUES (?)`)\n .bind(m.version)\n .run();\n } else {\n // Atomic batch: all statements + version-row insert land in\n // the same transaction. A failing statement rolls the whole\n // migration back, so we never record a half-applied version.\n await d1.batch([\n ...statements.map((s) => d1.prepare(s.sql)),\n d1\n .prepare(`INSERT OR IGNORE INTO ${table} VALUES (?)`)\n .bind(m.version),\n ]);\n }\n console.log(\n `[db] Applied migration v${m.version} (${statements.length} statement${statements.length === 1 ? \"\" : \"s\"})`,\n );\n } catch (err) {\n console.error(\n `[db] Migration v${m.version} FAILED:`,\n (err as Error).message,\n \"\\nSQL:\",\n JSON.stringify(m.sql),\n );\n throw err;\n }\n }\n return;\n }\n\n // Generic path — works for libsql and Postgres\n const exec = getDbExec();\n const pg = isPostgres();\n\n // Retry initial table creation — SQLITE_BUSY_RECOVERY can occur on HMR\n // restarts when WAL files from the previous process haven't been released yet.\n await retrySqliteBusy(\n () =>\n exec.execute(\n `CREATE TABLE IF NOT EXISTS ${table} (version INTEGER PRIMARY KEY)`,\n ),\n { maxAttempts: 6, baseDelayMs: 1000, rethrow: true },\n );\n\n const { rows } = await exec.execute(\n `SELECT MAX(version) as v FROM ${table}`,\n );\n const current = (rows[0]?.v as number) ?? 0;\n\n const insertSql = pg\n ? `INSERT INTO ${table} VALUES (?) ON CONFLICT DO NOTHING`\n : `INSERT OR IGNORE INTO ${table} VALUES (?)`;\n\n const pending = migrations.filter((m) => m.version > current);\n if (pending.length > 0) {\n console.log(\n `[db] Applying ${pending.length} migration(s) on ${pg ? \"Postgres\" : \"SQLite/libsql\"}…`,\n );\n }\n\n for (const m of pending) {\n const raw = resolveMigrationSql(m.sql, pg);\n if (raw == null) {\n // Dialect-gated migration with no SQL for this dialect; still mark\n // as applied so we don't retry forever.\n await exec.execute({ sql: insertSql, args: [m.version] });\n continue;\n }\n // Split BEFORE adapting so we can remember which original statements\n // carried `ADD COLUMN IF NOT EXISTS` — SQLite drops the clause, so we\n // emulate the idempotent semantic by swallowing duplicate-column\n // errors only for those statements.\n const originalStatements = splitSqlStatements(raw);\n const statements = originalStatements.map((orig) => ({\n sql: pg ? adaptSqlForPostgres(orig) : adaptSqlForSqlite(orig),\n hadIfNotExists: IF_NOT_EXISTS_ADD_COLUMN_RE.test(orig),\n }));\n let currentStmt = \"\";\n try {\n for (const { sql: stmt, hadIfNotExists } of statements) {\n currentStmt = stmt;\n try {\n await exec.execute(stmt);\n } catch (err) {\n if (!pg && hadIfNotExists && isDuplicateColumnError(err)) {\n // IF NOT EXISTS semantic: column already present, skip.\n continue;\n }\n throw err;\n }\n }\n await exec.execute({ sql: insertSql, args: [m.version] });\n console.log(\n `[db] Applied migration v${m.version} (${statements.length} statement${statements.length === 1 ? \"\" : \"s\"})`,\n );\n } catch (err) {\n if (pg && isPermissionError(err)) {\n // The connected role lacks privilege for this migration (e.g. a\n // permission-limited dev/replica role that doesn't own the table).\n // Don't crash-loop the whole server over it — warn and STOP here.\n // We must NOT continue to later migrations: pending work is computed\n // as `version > MAX(recorded version)`, so applying a later migration\n // would advance MAX past this unrecorded one and orphan it forever.\n // Stopping leaves MAX at the last recorded version, so a properly-\n // privileged role resumes from this exact migration, in order.\n console.warn(\n `[db] Migration v${m.version} skipped — insufficient privilege: ${(err as Error).message}. ` +\n `Apply it with a DB role that owns the table. ` +\n `Halting further migrations so this one isn't orphaned.`,\n \"\\nStatement:\",\n currentStmt,\n );\n break;\n }\n console.error(\n `[db] Migration v${m.version} FAILED:`,\n (err as Error).message,\n \"\\nStatement:\",\n currentStmt,\n );\n throw err;\n }\n }\n } catch (err) {\n console.error(\"[db] Migration failed:\", (err as Error).message);\n // In local dev, hard-fail so the developer catches errors immediately.\n // On serverless runtimes (Netlify Functions, Vercel, CF Workers) we\n // keep the process alive — the app will return 500s for routes that\n // depend on the missing tables, but at least other routes still work.\n // Note: Node.js 21+ defines globalThis.navigator, so we check for\n // serverless env vars instead of navigator presence.\n const isServerless =\n !!globalThis.process?.env?.NETLIFY ||\n !!globalThis.process?.env?.AWS_LAMBDA_FUNCTION_NAME ||\n !!globalThis.process?.env?.VERCEL ||\n \"__cf_env\" in globalThis;\n if (typeof globalThis.process?.exit === \"function\" && !isServerless) {\n process.exit(1);\n }\n }\n };\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"builder.d.ts","sourceRoot":"","sources":["../../src/file-upload/builder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAYrD;;;;;;;GAOG;AACH,eAAO,MAAM,yBAAyB,EAAE,kBAkFvC,CAAC"}
1
+ {"version":3,"file":"builder.d.ts","sourceRoot":"","sources":["../../src/file-upload/builder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAYrD;;;;;;;GAOG;AACH,eAAO,MAAM,yBAAyB,EAAE,kBA+FvC,CAAC"}
@@ -44,17 +44,32 @@ export const builderFileUploadProvider = {
44
44
  // attempt — usually GCS write hiccups that succeed on retry. We bound
45
45
  // it tight so a deterministic 500 surfaces quickly to the caller.
46
46
  const RETRY_DELAYS_MS = [600, 1800];
47
+ const UPLOAD_TIMEOUT_MS = 120_000; // 2 minutes per attempt
47
48
  let response = null;
48
49
  let lastErrorBody = "";
49
50
  for (let attempt = 0; attempt <= RETRY_DELAYS_MS.length; attempt++) {
50
- response = await fetch(url, {
51
- method: "POST",
52
- headers: {
53
- Authorization: `Bearer ${privateKey}`,
54
- "Content-Type": bareMimeType,
55
- },
56
- body,
57
- });
51
+ const controller = new AbortController();
52
+ const timer = setTimeout(() => controller.abort(), UPLOAD_TIMEOUT_MS);
53
+ try {
54
+ response = await fetch(url, {
55
+ method: "POST",
56
+ headers: {
57
+ Authorization: `Bearer ${privateKey}`,
58
+ "Content-Type": bareMimeType,
59
+ },
60
+ body,
61
+ signal: controller.signal,
62
+ });
63
+ }
64
+ catch (err) {
65
+ clearTimeout(timer);
66
+ const isLastAttempt = attempt === RETRY_DELAYS_MS.length;
67
+ if (isLastAttempt)
68
+ throw err;
69
+ await new Promise((r) => setTimeout(r, RETRY_DELAYS_MS[attempt]));
70
+ continue;
71
+ }
72
+ clearTimeout(timer);
58
73
  if (response.ok)
59
74
  break;
60
75
  const isTransient = response.status >= 500 && response.status !== 501;
@@ -1 +1 @@
1
- {"version":3,"file":"builder.js","sourceRoot":"","sources":["../../src/file-upload/builder.ts"],"names":[],"mappings":"AAEA,MAAM,wBAAwB,GAAG,oBAAoB,CAAC;AAEtD,SAAS,iBAAiB;IACxB,OAAO,CACL,OAAO,CAAC,GAAG,CAAC,gBAAgB;QAC5B,OAAO,CAAC,GAAG,CAAC,uBAAuB;QACnC,wBAAwB,CACzB,CAAC;AACJ,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,yBAAyB,GAAuB;IAC3D,EAAE,EAAE,SAAS;IACb,IAAI,EAAE,YAAY;IAClB,YAAY,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,mBAAmB;IACrD,MAAM,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,QAAQ,EAAE,EAAE,EAAE;QAC7C,MAAM,EAAE,wBAAwB,EAAE,GAChC,MAAM,MAAM,CAAC,kCAAkC,CAAC,CAAC;QACnD,MAAM,UAAU,GAAG,MAAM,wBAAwB,EAAE,CAAC;QACpD,IAAI,CAAC,UAAU,EAAE,CAAC;YAChB,MAAM,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAC;QACpD,CAAC;QAED,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,gBAAgB,EAAE,iBAAiB,EAAE,CAAC,CAAC;QAC3D,IAAI,QAAQ;YAAE,GAAG,CAAC,YAAY,CAAC,GAAG,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;QAErD,iEAAiE;QACjE,qEAAqE;QACrE,qEAAqE;QACrE,sEAAsE;QACtE,iEAAiE;QACjE,oBAAoB;QACpB,MAAM,YAAY,GAAG,CAAC,QAAQ,IAAI,0BAA0B,CAA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,aAAa,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,EAAE,CAAC,CAAC;gBACtD,MAAM;YACR,CAAC;YACD,aAAa,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,EAAE,CAAC,CAAC;YACtD,MAAM,IAAI,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,UAAU,CAAC,CAAC,EAAE,eAAe,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;QACpE,CAAC;QAED,IAAI,CAAC,QAAQ,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;YAC9B,MAAM,MAAM,GAAG,QAAQ,EAAE,MAAM,IAAI,CAAC,CAAC;YACrC,MAAM,UAAU,GAAG,QAAQ,EAAE,UAAU,IAAI,aAAa,CAAC;YACzD,MAAM,IAAI,KAAK,CACb,6BAA6B,MAAM,MAAM,aAAa,IAAI,UAAU,EAAE,CACvE,CAAC;QACJ,CAAC;QAED,MAAM,IAAI,GAAG,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,CAGpD,CAAC;QACF,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC;YACd,MAAM,IAAI,KAAK,CAAC,mCAAmC,CAAC,CAAC;QACvD,CAAC;QAED,OAAO,EAAE,GAAG,EAAE,IAAI,CAAC,GAAG,EAAE,EAAE,EAAE,IAAI,CAAC,EAAE,EAAE,QAAQ,EAAE,SAAS,EAAE,CAAC;IAC7D,CAAC;CACF,CAAC","sourcesContent":["import type { FileUploadProvider } from \"./types.js\";\n\nconst DEFAULT_BUILDER_APP_HOST = \"https://builder.io\";\n\nfunction builderUploadHost(): string {\n return (\n process.env.BUILDER_APP_HOST ||\n process.env.BUILDER_PUBLIC_APP_HOST ||\n DEFAULT_BUILDER_APP_HOST\n );\n}\n\n/**\n * Built-in Builder.io file upload provider.\n * Uses the same BUILDER_PRIVATE_KEY as the browser/background-agent flows,\n * so connecting Builder once (via the sidebar \"Connect Builder\" action)\n * automatically enables file uploads.\n *\n * Upload API: https://www.builder.io/c/docs/upload-api\n */\nexport const builderFileUploadProvider: FileUploadProvider = {\n id: \"builder\",\n name: \"Builder.io\",\n isConfigured: () => !!process.env.BUILDER_PRIVATE_KEY,\n upload: async ({ data, filename, mimeType }) => {\n const { resolveBuilderPrivateKey } =\n await import(\"../server/credential-provider.js\");\n const privateKey = await resolveBuilderPrivateKey();\n if (!privateKey) {\n throw new Error(\"BUILDER_PRIVATE_KEY is not set\");\n }\n\n const url = new URL(\"/api/v1/upload\", builderUploadHost());\n if (filename) url.searchParams.set(\"name\", filename);\n\n // Strip any media-type parameters (e.g. `;codecs=avc1,opus` from\n // MediaRecorder blobs) — Builder's upload API parses the body as raw\n // binary only when Content-Type is a bare MIME type. A parameterized\n // Content-Type falls through to the multipart/base64 paths which look\n // for an `image` field, and returns \"No image specified\" when it\n // doesn't find one.\n const bareMimeType = (mimeType || \"application/octet-stream\")\n .split(\";\")[0]\n .trim();\n\n const buffer =\n data instanceof Uint8Array ? data : new Uint8Array(data as any);\n const bytes = new Uint8Array(\n buffer.buffer,\n buffer.byteOffset,\n buffer.byteLength,\n );\n const body =\n typeof Blob !== \"undefined\"\n ? new Blob([bytes], { type: bareMimeType })\n : (bytes as unknown as BodyInit);\n\n // Retry transient 5xx once with backoff. Builder.io's upload service\n // occasionally returns a bodyless 500 (\"Internal Error\") on the first\n // attempt — usually GCS write hiccups that succeed on retry. We bound\n // it tight so a deterministic 500 surfaces quickly to the caller.\n const RETRY_DELAYS_MS = [600, 1800];\n let response: Response | null = null;\n let lastErrorBody = \"\";\n for (let attempt = 0; attempt <= RETRY_DELAYS_MS.length; attempt++) {\n response = await fetch(url, {\n method: \"POST\",\n headers: {\n Authorization: `Bearer ${privateKey}`,\n \"Content-Type\": bareMimeType,\n },\n body,\n });\n if (response.ok) break;\n const isTransient = response.status >= 500 && response.status !== 501;\n const isLastAttempt = attempt === RETRY_DELAYS_MS.length;\n if (!isTransient || isLastAttempt) {\n lastErrorBody = await response.text().catch(() => \"\");\n break;\n }\n lastErrorBody = await response.text().catch(() => \"\");\n await new Promise((r) => setTimeout(r, RETRY_DELAYS_MS[attempt]));\n }\n\n if (!response || !response.ok) {\n const status = response?.status ?? 0;\n const statusText = response?.statusText ?? \"no response\";\n throw new Error(\n `Builder.io upload failed (${status}): ${lastErrorBody || statusText}`,\n );\n }\n\n const json = (await response.json().catch(() => ({}))) as {\n url?: string;\n id?: string;\n };\n if (!json.url) {\n throw new Error(\"Builder.io upload returned no URL\");\n }\n\n return { url: json.url, id: json.id, provider: \"builder\" };\n },\n};\n"]}
1
+ {"version":3,"file":"builder.js","sourceRoot":"","sources":["../../src/file-upload/builder.ts"],"names":[],"mappings":"AAEA,MAAM,wBAAwB,GAAG,oBAAoB,CAAC;AAEtD,SAAS,iBAAiB;IACxB,OAAO,CACL,OAAO,CAAC,GAAG,CAAC,gBAAgB;QAC5B,OAAO,CAAC,GAAG,CAAC,uBAAuB;QACnC,wBAAwB,CACzB,CAAC;AACJ,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,yBAAyB,GAAuB;IAC3D,EAAE,EAAE,SAAS;IACb,IAAI,EAAE,YAAY;IAClB,YAAY,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,mBAAmB;IACrD,MAAM,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,QAAQ,EAAE,EAAE,EAAE;QAC7C,MAAM,EAAE,wBAAwB,EAAE,GAChC,MAAM,MAAM,CAAC,kCAAkC,CAAC,CAAC;QACnD,MAAM,UAAU,GAAG,MAAM,wBAAwB,EAAE,CAAC;QACpD,IAAI,CAAC,UAAU,EAAE,CAAC;YAChB,MAAM,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAC;Q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,EAAE,EAAE,CAAC,UAAU,CAAC,CAAC,EAAE,eAAe,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;QACpE,CAAC;QAED,IAAI,CAAC,QAAQ,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;YAC9B,MAAM,MAAM,GAAG,QAAQ,EAAE,MAAM,IAAI,CAAC,CAAC;YACrC,MAAM,UAAU,GAAG,QAAQ,EAAE,UAAU,IAAI,aAAa,CAAC;YACzD,MAAM,IAAI,KAAK,CACb,6BAA6B,MAAM,MAAM,aAAa,IAAI,UAAU,EAAE,CACvE,CAAC;QACJ,CAAC;QAED,MAAM,IAAI,GAAG,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,CAGpD,CAAC;QACF,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC;YACd,MAAM,IAAI,KAAK,CAAC,mCAAmC,CAAC,CAAC;QACvD,CAAC;QAED,OAAO,EAAE,GAAG,EAAE,IAAI,CAAC,GAAG,EAAE,EAAE,EAAE,IAAI,CAAC,EAAE,EAAE,QAAQ,EAAE,SAAS,EAAE,CAAC;IAC7D,CAAC;CACF,CAAC","sourcesContent":["import type { FileUploadProvider } from \"./types.js\";\n\nconst DEFAULT_BUILDER_APP_HOST = \"https://builder.io\";\n\nfunction builderUploadHost(): string {\n return (\n process.env.BUILDER_APP_HOST ||\n process.env.BUILDER_PUBLIC_APP_HOST ||\n DEFAULT_BUILDER_APP_HOST\n );\n}\n\n/**\n * Built-in Builder.io file upload provider.\n * Uses the same BUILDER_PRIVATE_KEY as the browser/background-agent flows,\n * so connecting Builder once (via the sidebar \"Connect Builder\" action)\n * automatically enables file uploads.\n *\n * Upload API: https://www.builder.io/c/docs/upload-api\n */\nexport const builderFileUploadProvider: FileUploadProvider = {\n id: \"builder\",\n name: \"Builder.io\",\n isConfigured: () => !!process.env.BUILDER_PRIVATE_KEY,\n upload: async ({ data, filename, mimeType }) => {\n const { resolveBuilderPrivateKey } =\n await import(\"../server/credential-provider.js\");\n const privateKey = await resolveBuilderPrivateKey();\n if (!privateKey) {\n throw new Error(\"BUILDER_PRIVATE_KEY is not set\");\n }\n\n const url = new URL(\"/api/v1/upload\", builderUploadHost());\n if (filename) url.searchParams.set(\"name\", filename);\n\n // Strip any media-type parameters (e.g. `;codecs=avc1,opus` from\n // MediaRecorder blobs) — Builder's upload API parses the body as raw\n // binary only when Content-Type is a bare MIME type. A parameterized\n // Content-Type falls through to the multipart/base64 paths which look\n // for an `image` field, and returns \"No image specified\" when it\n // doesn't find one.\n const bareMimeType = (mimeType || \"application/octet-stream\")\n .split(\";\")[0]\n .trim();\n\n const buffer =\n data instanceof Uint8Array ? data : new Uint8Array(data as any);\n const bytes = new Uint8Array(\n buffer.buffer,\n buffer.byteOffset,\n buffer.byteLength,\n );\n const body =\n typeof Blob !== \"undefined\"\n ? new Blob([bytes], { type: bareMimeType })\n : (bytes as unknown as BodyInit);\n\n // Retry transient 5xx once with backoff. Builder.io's upload service\n // occasionally returns a bodyless 500 (\"Internal Error\") on the first\n // attempt — usually GCS write hiccups that succeed on retry. We bound\n // it tight so a deterministic 500 surfaces quickly to the caller.\n const RETRY_DELAYS_MS = [600, 1800];\n const UPLOAD_TIMEOUT_MS = 120_000; // 2 minutes per attempt\n let response: Response | null = null;\n let lastErrorBody = \"\";\n for (let attempt = 0; attempt <= RETRY_DELAYS_MS.length; attempt++) {\n const controller = new AbortController();\n const timer = setTimeout(() => controller.abort(), UPLOAD_TIMEOUT_MS);\n try {\n response = await fetch(url, {\n method: \"POST\",\n headers: {\n Authorization: `Bearer ${privateKey}`,\n \"Content-Type\": bareMimeType,\n },\n body,\n signal: controller.signal,\n });\n } catch (err) {\n clearTimeout(timer);\n const isLastAttempt = attempt === RETRY_DELAYS_MS.length;\n if (isLastAttempt) throw err;\n await new Promise((r) => setTimeout(r, RETRY_DELAYS_MS[attempt]));\n continue;\n }\n clearTimeout(timer);\n if (response.ok) break;\n const isTransient = response.status >= 500 && response.status !== 501;\n const isLastAttempt = attempt === RETRY_DELAYS_MS.length;\n if (!isTransient || isLastAttempt) {\n lastErrorBody = await response.text().catch(() => \"\");\n break;\n }\n lastErrorBody = await response.text().catch(() => \"\");\n await new Promise((r) => setTimeout(r, RETRY_DELAYS_MS[attempt]));\n }\n\n if (!response || !response.ok) {\n const status = response?.status ?? 0;\n const statusText = response?.statusText ?? \"no response\";\n throw new Error(\n `Builder.io upload failed (${status}): ${lastErrorBody || statusText}`,\n );\n }\n\n const json = (await response.json().catch(() => ({}))) as {\n url?: string;\n id?: string;\n };\n if (!json.url) {\n throw new Error(\"Builder.io upload returned no URL\");\n }\n\n return { url: json.url, id: json.id, provider: \"builder\" };\n },\n};\n"]}