@supa-media/tex-contract 0.0.0-stage → 0.3.0

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 (227) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +33 -2
  3. package/package.json +16 -4
  4. package/texContract/aboutLines.cjs +117 -0
  5. package/texContract/aboutLines.d.cts +14 -0
  6. package/texContract/aboutNoteLimits.cjs +41 -0
  7. package/texContract/aboutNoteLimits.d.cts +5 -0
  8. package/texContract/actionManual.cjs +104 -0
  9. package/texContract/activityHint.cjs +40 -0
  10. package/texContract/activityHint.d.cts +4 -0
  11. package/texContract/asks.cjs +16 -0
  12. package/texContract/asks.d.cts +6 -0
  13. package/texContract/asksToFile.cjs +20 -0
  14. package/texContract/asksToFile.d.cts +2 -0
  15. package/texContract/attention.cjs +138 -0
  16. package/texContract/attention.d.cts +20 -0
  17. package/texContract/browserHandback.cjs +13 -0
  18. package/texContract/browserHandback.d.cts +3 -0
  19. package/texContract/browserOwner.cjs +31 -0
  20. package/texContract/browserOwner.d.cts +2 -0
  21. package/texContract/bytes.cjs +12 -0
  22. package/texContract/capabilities.cjs +47 -0
  23. package/texContract/capabilities.d.cts +27 -0
  24. package/texContract/capabilities.generated.json +77 -0
  25. package/texContract/chatHistoryPath.cjs +38 -0
  26. package/texContract/chatHistoryPath.d.cts +5 -0
  27. package/texContract/chatLedger.cjs +91 -0
  28. package/texContract/chatLedger.d.cts +21 -0
  29. package/texContract/computerTool.cjs +102 -0
  30. package/texContract/computerTool.d.cts +16 -0
  31. package/texContract/consentYes.cjs +22 -0
  32. package/texContract/consentYes.d.cts +5 -0
  33. package/texContract/contextGateway.cjs +52 -0
  34. package/texContract/contextGateway.d.cts +19 -0
  35. package/texContract/deskEligible.cjs +20 -0
  36. package/texContract/deskEligible.d.cts +3 -0
  37. package/texContract/downloads.cjs +129 -0
  38. package/texContract/downloads.d.cts +15 -0
  39. package/texContract/egressLedger.cjs +114 -0
  40. package/texContract/egressLedger.d.cts +14 -0
  41. package/texContract/emailRead.cjs +38 -0
  42. package/texContract/emailRead.d.cts +5 -0
  43. package/texContract/eventWords.cjs +35 -0
  44. package/texContract/eventWords.d.cts +4 -0
  45. package/texContract/fileParts.cjs +43 -0
  46. package/texContract/fileParts.d.cts +6 -0
  47. package/texContract/fileTables.cjs +243 -0
  48. package/texContract/fileWords.cjs +169 -0
  49. package/texContract/folded.cjs +16 -0
  50. package/texContract/folded.d.cts +3 -0
  51. package/texContract/googleAiAccess.cjs +12 -0
  52. package/texContract/googleAiAccess.d.cts +2 -0
  53. package/texContract/graphWires.cjs +18 -0
  54. package/texContract/graphWires.d.cts +5 -0
  55. package/texContract/groupSession.cjs +28 -0
  56. package/texContract/groupSession.d.cts +7 -0
  57. package/texContract/guestNames.cjs +136 -0
  58. package/texContract/guestNames.d.cts +35 -0
  59. package/texContract/heard.cjs +19 -0
  60. package/texContract/heard.d.cts +4 -0
  61. package/texContract/heldOffer.cjs +57 -0
  62. package/texContract/heldOffer.d.cts +8 -0
  63. package/texContract/helperCallers.cjs +29 -0
  64. package/texContract/helperCallers.d.cts +4 -0
  65. package/texContract/identity.cjs +179 -0
  66. package/texContract/identity.d.cts +8 -0
  67. package/texContract/jobLogEntries.cjs +93 -0
  68. package/texContract/jobLogEntries.d.cts +12 -0
  69. package/texContract/judgedActions.cjs +20 -0
  70. package/texContract/judgedActions.d.cts +4 -0
  71. package/texContract/labSandboxGate.cjs +63 -0
  72. package/texContract/labSandboxGate.d.cts +9 -0
  73. package/texContract/ledgerReadChars.cjs +4 -0
  74. package/texContract/ledgerReadChars.d.cts +3 -0
  75. package/texContract/mailCheck.cjs +90 -0
  76. package/texContract/mailCheck.d.cts +12 -0
  77. package/texContract/mailEntries.cjs +64 -0
  78. package/texContract/mailEntries.d.cts +13 -0
  79. package/texContract/mailHealth.cjs +60 -0
  80. package/texContract/mailHealth.d.cts +10 -0
  81. package/texContract/notePath.cjs +99 -0
  82. package/texContract/notePath.d.cts +4 -0
  83. package/texContract/noteRunFacts.cjs +136 -0
  84. package/texContract/noteRunFacts.d.cts +32 -0
  85. package/texContract/objectEtag.cjs +84 -0
  86. package/texContract/objectEtag.d.cts +4 -0
  87. package/texContract/objectPlace.cjs +32 -0
  88. package/texContract/objectPlace.d.cts +5 -0
  89. package/texContract/pageJobs.cjs +226 -0
  90. package/texContract/pageJobs.d.cts +13 -0
  91. package/texContract/pdfLinks.cjs +135 -0
  92. package/texContract/pdfLinks.d.cts +4 -0
  93. package/texContract/personHub.cjs +38 -0
  94. package/texContract/personHub.d.cts +5 -0
  95. package/texContract/personNameText.cjs +18 -0
  96. package/texContract/personNameText.d.cts +3 -0
  97. package/texContract/pinnedContext.cjs +12 -0
  98. package/texContract/pinnedContext.d.cts +3 -0
  99. package/texContract/quietHoursWindow.cjs +36 -0
  100. package/texContract/quietHoursWindow.d.cts +3 -0
  101. package/texContract/reactEmoji.cjs +79 -0
  102. package/texContract/reactEmoji.d.cts +2 -0
  103. package/texContract/scopeClamp.cjs +117 -0
  104. package/texContract/scopeClamp.d.cts +5 -0
  105. package/texContract/scopes.cjs +29 -0
  106. package/texContract/scopes.d.cts +7 -0
  107. package/texContract/searchSettings.cjs +54 -0
  108. package/texContract/searchSettings.d.cts +6 -0
  109. package/texContract/settingHabits.cjs +87 -0
  110. package/texContract/settingHabits.d.cts +20 -0
  111. package/texContract/settingsNotes.cjs +217 -0
  112. package/texContract/settingsNotes.d.cts +26 -0
  113. package/texContract/shownEmail.cjs +181 -0
  114. package/texContract/texClients.cjs +53 -0
  115. package/texContract/texClients.d.cts +12 -0
  116. package/texContract/texGraph.cjs +593 -0
  117. package/texContract/texGraph.d.cts +55 -0
  118. package/texContract/texKeys.cjs +100 -0
  119. package/texContract/texKeys.d.cts +13 -0
  120. package/texContract/texNumber.cjs +20 -0
  121. package/texContract/texNumber.d.cts +3 -0
  122. package/texContract/texOwnSources.cjs +9 -0
  123. package/texContract/texOwnSources.d.cts +3 -0
  124. package/texContract/texSettings.cjs +22 -0
  125. package/texContract/texSettings.d.cts +20 -0
  126. package/texContract/texSettings.generated.json +25 -0
  127. package/texContract/texStateFiles.cjs +22 -0
  128. package/texContract/texStateFiles.d.cts +9 -0
  129. package/texContract/texSteps.cjs +27 -0
  130. package/texContract/texSteps.d.cts +14 -0
  131. package/texContract/texSteps.generated.json +41 -0
  132. package/texContract/texStoreNames.cjs +26 -0
  133. package/texContract/texStoreNames.d.cts +12 -0
  134. package/texContract/texStoreWire.cjs +306 -0
  135. package/texContract/texStoreWire.d.cts +32 -0
  136. package/texContract/threadsFold.cjs +75 -0
  137. package/texContract/threadsFold.d.cts +22 -0
  138. package/texContract/toolActions.cjs +253 -0
  139. package/texContract/toolCatalog.cjs +49 -0
  140. package/texContract/toolCatalog.generated.json +90 -0
  141. package/texContract/toolContract.cjs +52 -0
  142. package/texContract/toolContract.d.cts +12 -0
  143. package/texContract/toolShapes.cjs +14 -0
  144. package/texContract/transcription.cjs +101 -0
  145. package/texContract/transcription.d.cts +3 -0
  146. package/texContract/treeJobs.cjs +31 -0
  147. package/texContract/treeJobs.d.cts +3 -0
  148. package/texContract/turnSession.cjs +90 -0
  149. package/texContract/turnSession.d.cts +19 -0
  150. package/texContract/turnWords.cjs +90 -0
  151. package/texContract/turnWords.d.cts +9 -0
  152. package/texContract/uploadImages.cjs +131 -0
  153. package/texContract/uploadImages.d.cts +13 -0
  154. package/texContract/v2Names.cjs +41 -0
  155. package/texContract/v2SearchFetch.cjs +316 -0
  156. package/texContract/v2Translate.cjs +123 -0
  157. package/texContract/vaultTools.cjs +112 -0
  158. package/texContract/vaultTools.d.cts +15 -0
  159. package/texContract/yesWords.cjs +91 -0
  160. package/texShared/argumentCoercion.cjs +93 -0
  161. package/texShared/cellComments.cjs +122 -0
  162. package/texShared/cellComments.d.cts +21 -0
  163. package/texShared/comments.cjs +586 -0
  164. package/texShared/comments.d.cts +115 -0
  165. package/texShared/contentTypes.cjs +85 -0
  166. package/texShared/fileKinds.cjs +123 -0
  167. package/texShared/flags.cjs +159 -0
  168. package/texShared/flags.d.cts +23 -0
  169. package/texShared/folderJobInfo.cjs +301 -0
  170. package/texShared/folderJobInfo.d.cts +40 -0
  171. package/texShared/folderRoles.cjs +512 -0
  172. package/texShared/folderRoles.d.cts +73 -0
  173. package/texShared/githubOidc.ts +98 -0
  174. package/texShared/groupRunQuestions.cjs +98 -0
  175. package/texShared/groupRunQuestions.d.cts +4 -0
  176. package/texShared/inboxFolders.cjs +49 -0
  177. package/texShared/inboxFolders.d.cts +5 -0
  178. package/texShared/ink/drawing.cjs +474 -0
  179. package/texShared/ink/texFigure.cjs +252 -0
  180. package/texShared/ink/texFigure.d.cts +36 -0
  181. package/texShared/ink/texPoses.cjs +180 -0
  182. package/texShared/ink/texPoses.d.cts +20 -0
  183. package/texShared/jobPartEnd.cjs +133 -0
  184. package/texShared/jobPartEnd.d.cts +13 -0
  185. package/texShared/jobResume.cjs +31 -0
  186. package/texShared/jobResume.d.cts +5 -0
  187. package/texShared/jobSplit.cjs +90 -0
  188. package/texShared/jobSplit.d.cts +6 -0
  189. package/texShared/jobStart.cjs +56 -0
  190. package/texShared/jobStart.d.cts +9 -0
  191. package/texShared/mail.cjs +400 -0
  192. package/texShared/mail.d.cts +56 -0
  193. package/texShared/messageAnchors.cjs +38 -0
  194. package/texShared/models/aiKeys.cjs +137 -0
  195. package/texShared/models/aiKeys.d.cts +42 -0
  196. package/texShared/models/aiKeys.json +56 -0
  197. package/texShared/models/creditRest.cjs +269 -0
  198. package/texShared/models/promptBudget.cjs +295 -0
  199. package/texShared/models/promptSections.cjs +29 -0
  200. package/texShared/noteAppLink.cjs +23 -0
  201. package/texShared/notePreview.cjs +219 -0
  202. package/texShared/notePreview.d.cts +21 -0
  203. package/texShared/pages/blocks.cjs +215 -0
  204. package/texShared/pages/cover.cjs +164 -0
  205. package/texShared/pages/css.cjs +181 -0
  206. package/texShared/pages/cssBlocks.cjs +264 -0
  207. package/texShared/pages/draw.cjs +212 -0
  208. package/texShared/pages/drawRich.cjs +232 -0
  209. package/texShared/pages/html.cjs +168 -0
  210. package/texShared/pages/inline.cjs +251 -0
  211. package/texShared/pages/layout.cjs +76 -0
  212. package/texShared/pages/parse.cjs +418 -0
  213. package/texShared/pages/render.cjs +200 -0
  214. package/texShared/pages/skill.cjs +147 -0
  215. package/texShared/pages/skill.d.cts +5 -0
  216. package/texShared/pages/templates.cjs +336 -0
  217. package/texShared/r2Store.cjs +163 -0
  218. package/texShared/remoteImage.cjs +177 -0
  219. package/texShared/routines.cjs +656 -0
  220. package/texShared/routines.d.cts +86 -0
  221. package/texShared/servedListing.cjs +250 -0
  222. package/texShared/servedListing.d.cts +17 -0
  223. package/texShared/sheetCsv.cjs +566 -0
  224. package/texShared/sheetCsv.d.cts +91 -0
  225. package/texShared/storageLayout.cjs +88 -0
  226. package/texShared/storeKeys.cjs +144 -0
  227. package/texShared/toolArguments.cjs +323 -0
@@ -0,0 +1,336 @@
1
+ /**
2
+ * The built-in page templates: a page whose shape is already decided (which
3
+ * parts, in which order, where a drawing goes), with gaps for the facts.
4
+ *
5
+ * Owner, 2026-11-10: pages "were supposed to have templates". An agent picks
6
+ * one from the ask, reads it (`orient about: "template <name>"`), and fills it,
7
+ * so it decides the facts and not the layout. A person's own templates come
8
+ * first: `tex/templates/` in their personal workspace and `templates/` in a
9
+ * shared one, written there when they ask Tex to "keep this as a template".
10
+ *
11
+ * Every gap is `[in brackets]` and every unknown number `?`, except where a
12
+ * block needs a number to draw (a `prices` group size, a `progress` share, a
13
+ * `chart` value), which holds a 0 to replace. So a template is a page that
14
+ * draws (each one is checked to: `pageTemplates.test.mjs`), and a filled page
15
+ * with a bracket left in it is plainly unfinished.
16
+ * Decision: `docs/decisions/app-and-console/pages.md`, "Pages start from a template".
17
+ */
18
+
19
+ "use strict";
20
+
21
+ /** The stamp's place in a template's front matter; the gateway puts the current stamp in when it hands one out. */
22
+ const STAMP = "{stamp}";
23
+
24
+ const TEMPLATES = [
25
+ {
26
+ name: "decision",
27
+ title: "Decision",
28
+ use: "a choice someone has to make: your pick first, then the options, prices and a vote",
29
+ source: `---
30
+ skill: ${STAMP}
31
+ kind: research
32
+ for: [who decides]
33
+ status: [one line: where the decision stands]
34
+ ---
35
+
36
+ # [The pick, as the outcome: "The Stand, for $2,850"]
37
+
38
+ [One sentence: what this decides, the pick and the numbers that settle it.]
39
+
40
+ \`\`\`vote [Your pick in a few words]
41
+ action: Go with {pick}
42
+ - [Option one] | [one line of why]
43
+ - [Option two] | [one line of why]
44
+ - [Option three] | [one line of why]
45
+ \`\`\`
46
+
47
+ \`\`\`stats
48
+ - [Budget]: ? | [note]
49
+ - [People]: ? | [note]
50
+ - [Deadline]: ? | [note]
51
+ \`\`\`
52
+
53
+ ## [What each option costs, as an answer: "From $420 to $2,850"]
54
+
55
+ [One sentence.]
56
+
57
+ \`\`\`prices What each one costs
58
+ per: [guest]
59
+ for: 0
60
+ - [Option one]: ? | [what you get]
61
+ source: [the page you read it on]
62
+ - [Option two]: ? | [what you get]
63
+ source: [the page you read it on]
64
+ \`\`\`
65
+
66
+ ## [How it fits, as an answer]
67
+
68
+ [One sentence on how the pick works out in practice.]
69
+
70
+ \`\`\`drawing [How the pick fits together]
71
+ <rect x="140" y="320" width="320" height="200" fill="paper"/>
72
+ <text x="300" y="430">[part]</text>
73
+ <line x1="460" y1="420" x2="740" y2="420"/>
74
+ <rect x="740" y="320" width="320" height="200" fill="hatch"/>
75
+ <text x="900" y="430">[part]</text>
76
+ \`\`\`
77
+
78
+ ## Next
79
+
80
+ - [ ] [The first thing to do once it's decided]
81
+ - [ ] [The second]
82
+ `,
83
+ },
84
+ {
85
+ name: "research",
86
+ title: "Research brief",
87
+ use: "a question answered: the answer, the evidence and its limits, and the sources",
88
+ source: `---
89
+ skill: ${STAMP}
90
+ kind: research
91
+ for: [who asked]
92
+ status: [one line: how sure the answer is]
93
+ ---
94
+
95
+ # [The answer in a few words]
96
+
97
+ [One sentence: the question and its answer, with the number that matters most.]
98
+
99
+ \`\`\`stats
100
+ - [The key number]: ? | [what it measures]
101
+ - [The second]: ? | [what it measures]
102
+ \`\`\`
103
+
104
+ ## [The first finding, as an answer]
105
+
106
+ [Two or three sentences of evidence, each fact with where it came from.]
107
+
108
+ ## [The second finding, as an answer]
109
+
110
+ [Two or three sentences.]
111
+
112
+ \`\`\`versus [Two things the evidence compares]
113
+ a: [First]
114
+ b: [Second]
115
+ - [Measure]: ? | ?
116
+ - [Measure]: ? | ?
117
+ \`\`\`
118
+
119
+ ## What this doesn't settle
120
+
121
+ > [!NOTE]
122
+ > [What the evidence can't tell you yet, and what would.]
123
+
124
+ ## Sources
125
+
126
+ - [Title](https://[the page you read]): [what it showed]
127
+ `,
128
+ },
129
+ {
130
+ name: "plan",
131
+ title: "Plan",
132
+ use: "how something gets done: the steps or a timeline, who owns each, and by when",
133
+ source: `---
134
+ skill: ${STAMP}
135
+ kind: plan
136
+ for: [who it is for]
137
+ status: [one line: "Starts Monday", "On track"]
138
+ ---
139
+
140
+ # [The outcome, done: "Sunday services, sorted"]
141
+
142
+ [One sentence: what this plan delivers, by when, for how many.]
143
+
144
+ \`\`\`stats
145
+ - [Budget]: ? | [note]
146
+ - [People]: ? | [note]
147
+ - [Date]: ? | [note]
148
+ \`\`\`
149
+
150
+ ## [When it happens, as an answer]
151
+
152
+ \`\`\`timeline
153
+ - [Date]: [milestone] | [detail]
154
+ state: now
155
+ - [Date]: [milestone] | [detail]
156
+ - [Date]: [milestone] | [detail]
157
+ \`\`\`
158
+
159
+ ## [Who does what, as an answer]
160
+
161
+ \`\`\`board
162
+ - To do: [task] | [who]
163
+ - Doing: [task] | [who]
164
+ - Done: [task] | [who]
165
+ \`\`\`
166
+
167
+ ## [How it flows, as an answer]
168
+
169
+ \`\`\`drawing [How the pieces hand off]
170
+ <circle cx="260" cy="420" r="90" fill="paper"/>
171
+ <text x="260" y="430">[step]</text>
172
+ <path d="M350 420 L 520 420"/>
173
+ <circle cx="610" cy="420" r="90" fill="paper"/>
174
+ <text x="610" y="430">[step]</text>
175
+ <path d="M700 420 L 870 420"/>
176
+ <circle cx="960" cy="420" r="90" fill="hatch"/>
177
+ <text x="960" y="430">[done]</text>
178
+ \`\`\`
179
+
180
+ ## Next
181
+
182
+ - [ ] [The first step, with who]
183
+ - [ ] [The second]
184
+
185
+ \`\`\`contacts Who to call
186
+ - [Name]: [role] | [note]
187
+ phone: ?
188
+ \`\`\`
189
+ `,
190
+ },
191
+ {
192
+ name: "status",
193
+ title: "Status",
194
+ use: "where something stands: one big number against its goal, what moved, what's stuck",
195
+ source: `---
196
+ skill: ${STAMP}
197
+ kind: status
198
+ for: [who follows it]
199
+ status: [one line: "On track", "Behind by a week"]
200
+ number: ? of ?
201
+ updated: [date]
202
+ ---
203
+
204
+ # [Where it stands, in a few words]
205
+
206
+ [One sentence: the number, the goal, and what changed since last time.]
207
+
208
+ \`\`\`progress
209
+ - [Part one]: 0/1 | [note]
210
+ - [Part two]: 0/1 | [note]
211
+ \`\`\`
212
+
213
+ ## [What moved, as an answer]
214
+
215
+ \`\`\`chart [The number over time]
216
+ type: line
217
+ - [Week 1]: 0
218
+ - [Week 2]: 0
219
+ - [Week 3]: 0
220
+ \`\`\`
221
+
222
+ ## [What's stuck, as an answer]
223
+
224
+ - [What is stuck, and what would unstick it]
225
+
226
+ ## Next
227
+
228
+ - [ ] [The next thing, with who]
229
+ `,
230
+ },
231
+ {
232
+ name: "review",
233
+ title: "Review",
234
+ use: "a close look at something: numbered findings, each with what shows it, and what to fix first",
235
+ source: `---
236
+ skill: ${STAMP}
237
+ kind: research
238
+ kicker: Review
239
+ for: [who it is for]
240
+ status: [one line: the verdict]
241
+ ---
242
+
243
+ # [The verdict in a few words]
244
+
245
+ [One sentence: what was looked at, how, and the finding that matters most.]
246
+
247
+ \`\`\`stats
248
+ - Findings: ? | [how many are serious]
249
+ - [Looked at]: ? | [what and how much]
250
+ \`\`\`
251
+
252
+ ## 1. [The first finding, as a claim]
253
+
254
+ [Two sentences: what you saw and why it matters.]
255
+
256
+ ![What shows it](exhibit-1.png)
257
+
258
+ ## 2. [The second finding, as a claim]
259
+
260
+ [Two sentences.]
261
+
262
+ ![What shows it](exhibit-2.png)
263
+
264
+ ## 3. [The third finding, as a claim]
265
+
266
+ [Two sentences.]
267
+
268
+ ## Fix first
269
+
270
+ - [ ] [The fix that matters most]
271
+ - [ ] [The next]
272
+ `,
273
+ },
274
+ {
275
+ name: "trip",
276
+ title: "Trip or event",
277
+ use: "a trip, a night out or an event: the places with photos, the day by day, and the map",
278
+ source: `---
279
+ skill: ${STAMP}
280
+ kind: plan
281
+ for: [who is going]
282
+ status: [one line: "Nothing booked yet"]
283
+ ---
284
+
285
+ # [The trip or event, as the outcome: "Two days in Philly, $310 each"]
286
+
287
+ [One sentence: when, where, how many, and the total.]
288
+
289
+ \`\`\`stats
290
+ - People: ? | [note]
291
+ - Each: ? | [what it covers]
292
+ - Dates: ? | [note]
293
+ \`\`\`
294
+
295
+ ## [Where to stay or go, as an answer]
296
+
297
+ \`\`\`places [Places that fit]
298
+ near: [the town]
299
+ need: [? seats, or a room for ?]
300
+ - [Place]: [street address] | [why it fits]
301
+ price: ?
302
+ source: [the page you read it on]
303
+ - [Place]: [street address] | [why it fits]
304
+ price: ?
305
+ source: [the page you read it on]
306
+ \`\`\`
307
+
308
+ ## [The day, as an answer]
309
+
310
+ \`\`\`itinerary [Day one]
311
+ - [7:00]: [stop] | [detail]
312
+ walk: [? min]
313
+ - [9:00]: [stop] | [detail]
314
+ \`\`\`
315
+
316
+ ## Next
317
+
318
+ - [ ] [Book the first thing]
319
+ `,
320
+ },
321
+ ];
322
+
323
+ const BY_NAME = new Map(TEMPLATES.map((template) => [template.name, template]));
324
+
325
+ /** One template's source with the stamp in it, or null for a name that isn't one. */
326
+ function templateSource(name, stamp) {
327
+ const template = BY_NAME.get(String(name ?? "").trim().toLowerCase());
328
+ return template ? template.source.replace(STAMP, stamp) : null;
329
+ }
330
+
331
+ /** The list the page skill shows: one line per template. */
332
+ function templateLines() {
333
+ return TEMPLATES.map((template) => `- \`${template.name}\` (${template.title}): ${template.use}`);
334
+ }
335
+
336
+ module.exports = { TEMPLATES, templateSource, templateLines };
@@ -0,0 +1,163 @@
1
+ "use strict";
2
+ /**
3
+ * R2Store — a ContextStore over a Cloudflare R2 bucket binding, or anything shaped like one (Tex's store over its
4
+ * object, `texContract/texStoreWire.cjs`). Moved from `apps/mcp/src/store/r2.js`, which re-exports it, into the
5
+ * shared code Tex's tree takes with it.
6
+ *
7
+ * A thin pass-through by design: R2's binding already matches the ContextStore
8
+ * contract (null on a failed `onlyIf`, unquoted etags, `delimitedPrefixes` on
9
+ * list), so nothing is reshaped and today's behavior is preserved exactly.
10
+ *
11
+ * Keys are the customer's own keys. An optional `rootPrefix` is applied here
12
+ * and is invisible to every caller above this file.
13
+ */
14
+
15
+ const {
16
+ applyRootPrefix,
17
+ assertSafeEtag,
18
+ assertSafeKey,
19
+ assertSafePrefix,
20
+ normalizeEtag,
21
+ normalizeRootPrefix,
22
+ stripListResult,
23
+ } = require("./storeKeys.cjs");
24
+ const { assertWritableContentType } = require("./contentTypes.cjs");
25
+
26
+ class R2Store {
27
+ /**
28
+ * @param {R2Bucket} bucket a Cloudflare R2 binding
29
+ * @param {{ rootPrefix?: string }} [options]
30
+ */
31
+ constructor(bucket, options = {}) {
32
+ if (!bucket) throw new Error("R2Store requires an R2 bucket binding");
33
+ this.bucket = bucket;
34
+ this.rootPrefix = normalizeRootPrefix(options.rootPrefix);
35
+ // R2 implements conditional writes natively. Its Worker delete API does
36
+ // not expose an atomic If-Match parameter, so this adapter must never claim
37
+ // physical conditional deletion; the factory may add the logical-delete
38
+ // wrapper when conditional writes and creates are verified.
39
+ this.capabilities = {
40
+ conditionalWrite: true,
41
+ conditionalCreate: true,
42
+ conditionalDelete: false,
43
+ serverSideCopy: false,
44
+ };
45
+ }
46
+
47
+ // Keys are validated here for the same reasons as in S3Store, and with the
48
+ // same shared helper: the two adapters must accept and reject exactly the
49
+ // same keys, or the same input lands in two different places depending on
50
+ // which backend a workspace is bound to.
51
+
52
+ // async so a rejected key always rejects the promise, never throws
53
+ // synchronously — rollback paths use `store.delete(key).catch(...)`.
54
+ async get(key) {
55
+ return this.bucket.get(applyRootPrefix(this.rootPrefix, assertSafeKey(key)));
56
+ }
57
+
58
+ /** Read object metadata without downloading its body. */
59
+ async head(key) {
60
+ // `undefined` means this binding cannot answer metadata-only. Keep `null`
61
+ // reserved for a provider-confirmed missing object so callers can safely
62
+ // fall back to their existing existence path.
63
+ if (typeof this.bucket.head !== "function") return undefined;
64
+ const object = await this.bucket.head(applyRootPrefix(this.rootPrefix, assertSafeKey(key)));
65
+ if (!object) return null;
66
+ return {
67
+ etag: normalizeEtag(object.etag || object.httpEtag || ""),
68
+ size: object.size,
69
+ contentType: object.contentType || object.httpMetadata?.contentType,
70
+ };
71
+ }
72
+
73
+ /**
74
+ * Is there an object at exactly this key, without fetching it.
75
+ *
76
+ * `head` rather than a prefix `list`: a list is not an existence check on
77
+ * every backend — some list a FOLDER — and it is the wrong shape even
78
+ * here, answering "what begins with this" for a question about one key.
79
+ */
80
+ async exists(key) {
81
+ const full = applyRootPrefix(this.rootPrefix, assertSafeKey(key));
82
+ // `head` is on the real binding; a prefix listing is the fallback and is a
83
+ // correct existence check HERE, because R2 keys really are a flat
84
+ // namespace with prefix semantics. It is not correct on a backend with real
85
+ // folders, where a listing of a key lists a directory.
86
+ if (typeof this.bucket.head === "function") {
87
+ return (await this.bucket.head(full)) !== null;
88
+ }
89
+ const page = await this.bucket.list({ prefix: full, limit: 4 });
90
+ return (page?.objects || []).some((object) => object.key === full);
91
+ }
92
+
93
+ async put(key, value, options) {
94
+ // An `onlyIf` carrying a missing or empty etag is rejected, exactly as
95
+ // S3Store does. R2's R2Conditional with no etagMatches carries no
96
+ // condition at all — a caller that asked for a conditional write would
97
+ // silently get last-writer-wins, which is the failure this adapter exists
98
+ // to make impossible. Unreachable today (every call site passes a real
99
+ // object etag), but the two adapters must agree, and the in-memory test
100
+ // stub shares R2's blind spot so nothing else would catch a drift.
101
+ if (options && "onlyIf" in options) {
102
+ const expected = options.onlyIf?.etagMatches;
103
+ if (options.onlyIf?.absent === true) {
104
+ options = { ...options, onlyIf: { etagDoesNotMatch: "*" } };
105
+ } else if (typeof expected !== "string" || !expected.trim()) {
106
+ throw new Error(
107
+ "onlyIf requires a non-empty etagMatches; refusing to downgrade a conditional write to an unconditional one",
108
+ );
109
+ }
110
+ if (expected) assertSafeEtag(normalizeEtag(expected));
111
+ }
112
+ // R2 carries the content type in `httpMetadata` rather than a header, and
113
+ // the same allow-list applies — the two adapters must agree about what a
114
+ // bucket may be made to hold. `options` is forwarded whole so `onlyIf`
115
+ // keeps working; only the metadata is added.
116
+ const contentType = assertWritableContentType(options?.contentType);
117
+ return this.bucket.put(applyRootPrefix(this.rootPrefix, assertSafeKey(key)), value, {
118
+ ...options,
119
+ httpMetadata: { ...options?.httpMetadata, contentType },
120
+ });
121
+ }
122
+
123
+ async copy(sourceKey, destinationKey, options = {}) {
124
+ if (options?.onlyIf?.absent === true && (await this.get(destinationKey)) !== null) return null;
125
+ const source = await this.bucket.get(applyRootPrefix(this.rootPrefix, assertSafeKey(sourceKey)));
126
+ if (!source) return null;
127
+ const body =
128
+ source.body && typeof source.body.getReader === "function"
129
+ ? source.body
130
+ : await source.arrayBuffer();
131
+ return this.bucket.put(applyRootPrefix(this.rootPrefix, assertSafeKey(destinationKey)), body, {
132
+ httpMetadata: source.httpMetadata,
133
+ customMetadata: source.customMetadata,
134
+ });
135
+ }
136
+
137
+ async delete(key, options = {}) {
138
+ const safeKey = assertSafeKey(key);
139
+ const scoped = applyRootPrefix(this.rootPrefix, safeKey);
140
+ if (options?.onlyIf?.etagMatches) {
141
+ const expected = assertSafeEtag(normalizeEtag(options.onlyIf.etagMatches));
142
+ const current = await this.bucket.get(scoped);
143
+ if (!current || normalizeEtag(current.etag || current.httpEtag || "") !== expected) return null;
144
+ }
145
+ return this.bucket.delete(scoped);
146
+ }
147
+
148
+ async list(options = {}) {
149
+ const { prefix, delimiter, cursor, limit, startAfter } = options;
150
+ const scoped = applyRootPrefix(this.rootPrefix, assertSafePrefix(prefix));
151
+ const page = await this.bucket.list({
152
+ prefix: scoped || undefined,
153
+ delimiter,
154
+ cursor,
155
+ limit,
156
+ // A key to resume after, as `s3.js` takes it; a cursor wins when both are given.
157
+ ...(startAfter !== undefined && cursor === undefined ? { startAfter: applyRootPrefix(this.rootPrefix, assertSafeKey(startAfter)) } : {}),
158
+ });
159
+ return stripListResult(this.rootPrefix, page);
160
+ }
161
+ }
162
+
163
+ module.exports = { R2Store };
@@ -0,0 +1,177 @@
1
+ /**
2
+ * Fetching an image from somebody else's server, safely, and telling what it is.
3
+ *
4
+ * Two callers, one rule, which is why this is shared rather than written twice:
5
+ *
6
+ * - the gateway, when an agent attaches an image by `url` to `write_note`
7
+ * (`apps/mcp/src/notes/uploadedImages.js`), fetches it once and stores it;
8
+ * - the control plane's image proxy (`readRemoteImage` in
9
+ * `apps/convex/functions/lib/filesFns/images.ts`), which fetches a remote
10
+ * image a note names so the reader's own browser never contacts its host.
11
+ *
12
+ * Plain CommonJS with no dependencies, like `storageLayout.cjs`, so the
13
+ * dependency-free gateway and Convex both import it. `fetch` is passed in so a
14
+ * test can serve the network.
15
+ */
16
+
17
+ /** The most one fetched image may be: the app's paste ceiling and `read_image`'s inline one. */
18
+ const MAX_REMOTE_IMAGE_BYTES = 5_000_000;
19
+
20
+ /** How many redirects a URL may take before it is refused. */
21
+ const MAX_REMOTE_IMAGE_REDIRECTS = 3;
22
+
23
+ /** How long one fetch may take, so a slow host cannot hold a request open. */
24
+ const REMOTE_IMAGE_TIMEOUT_MS = 15_000;
25
+
26
+ const ACCEPT = "image/png,image/jpeg,image/gif,image/webp,image/heic,image/heif";
27
+
28
+ /**
29
+ * What the bytes are, from their first few, or null.
30
+ *
31
+ * The type is always decided here and never by a caller's `data:` header or a
32
+ * remote server's `content-type`, because what is returned is drawn by a client
33
+ * that trusts it. SVG is not here and cannot be: it has no magic number, it is
34
+ * text, and it is a script container — the reason the store and `read_image`
35
+ * refuse it too.
36
+ */
37
+ function sniffImageType(bytes) {
38
+ const at = (i) => bytes[i];
39
+ const ascii = (from, length) => String.fromCharCode(...bytes.subarray(from, from + length));
40
+ if (bytes.length >= 8 && at(0) === 0x89 && ascii(1, 3) === "PNG" && at(4) === 0x0d && at(5) === 0x0a) {
41
+ return { extension: "png", contentType: "image/png" };
42
+ }
43
+ if (bytes.length >= 3 && at(0) === 0xff && at(1) === 0xd8 && at(2) === 0xff) {
44
+ return { extension: "jpg", contentType: "image/jpeg" };
45
+ }
46
+ if (bytes.length >= 6 && (ascii(0, 6) === "GIF87a" || ascii(0, 6) === "GIF89a")) {
47
+ return { extension: "gif", contentType: "image/gif" };
48
+ }
49
+ if (bytes.length >= 12 && ascii(0, 4) === "RIFF" && ascii(8, 4) === "WEBP") {
50
+ return { extension: "webp", contentType: "image/webp" };
51
+ }
52
+ if (bytes.length >= 12 && ascii(4, 4) === "ftyp") {
53
+ const brand = ascii(8, 4);
54
+ if (["heic", "heix", "heim", "heis", "hevc", "hevx"].includes(brand)) {
55
+ return { extension: "heic", contentType: "image/heic" };
56
+ }
57
+ if (["mif1", "msf1"].includes(brand)) {
58
+ return { extension: "heif", contentType: "image/heif" };
59
+ }
60
+ }
61
+ return null;
62
+ }
63
+
64
+ /**
65
+ * The one shape of URL either caller will fetch, as a `URL`, or null: https,
66
+ * the default port, no userinfo, a name rather than an address.
67
+ *
68
+ * Plain http is refused because the bytes would cross the internet unsigned;
69
+ * userinfo because a credential in a URL is one we would then have handled; a
70
+ * port, an IP literal, `localhost` and `.internal` because what this is for is
71
+ * an image on the web, which has a name, and those are how a caller aims at a
72
+ * host beside us. Every redirect hop is put through this again.
73
+ */
74
+ function fetchableImageUrl(value) {
75
+ if (typeof value !== "string" || value.length > 2048) return null;
76
+ let url;
77
+ try {
78
+ url = new URL(value);
79
+ } catch {
80
+ return null;
81
+ }
82
+ if (url.protocol !== "https:" || url.username || url.password || url.port) return null;
83
+ const host = url.hostname.toLowerCase();
84
+ if (/^\d+(\.\d+){3}$/.test(host) || host.startsWith("[")) return null;
85
+ if (!host.includes(".") || host === "localhost" || host.endsWith(".localhost") || host.endsWith(".internal")) {
86
+ return null;
87
+ }
88
+ return url;
89
+ }
90
+
91
+ async function readCapped(response) {
92
+ const declared = Number(response.headers.get("content-length"));
93
+ if (Number.isFinite(declared) && declared > MAX_REMOTE_IMAGE_BYTES) return { tooLarge: true };
94
+ if (!response.body) {
95
+ const buffer = new Uint8Array(await response.arrayBuffer());
96
+ return buffer.byteLength > MAX_REMOTE_IMAGE_BYTES ? { tooLarge: true } : { bytes: buffer };
97
+ }
98
+ const reader = response.body.getReader();
99
+ const chunks = [];
100
+ let total = 0;
101
+ for (;;) {
102
+ const { done, value } = await reader.read();
103
+ if (done) break;
104
+ total += value.byteLength;
105
+ if (total > MAX_REMOTE_IMAGE_BYTES) {
106
+ await reader.cancel().catch(() => {});
107
+ return { tooLarge: true };
108
+ }
109
+ chunks.push(value);
110
+ }
111
+ const bytes = new Uint8Array(total);
112
+ let offset = 0;
113
+ for (const chunk of chunks) {
114
+ bytes.set(chunk, offset);
115
+ offset += chunk.byteLength;
116
+ }
117
+ return { bytes };
118
+ }
119
+
120
+ /**
121
+ * Fetch one image. Resolves to `{ bytes, extension, contentType }`, or to
122
+ * `{ error }` with a sentence naming what was wrong — never throws.
123
+ *
124
+ * No cookies, no credentials, no referrer: the request carries nothing about
125
+ * who asked for it.
126
+ */
127
+ async function fetchRemoteImage(value, fetchImpl) {
128
+ let url = fetchableImageUrl(value);
129
+ if (!url) {
130
+ return { error: "url must be an https address on the default port, with no username or password" };
131
+ }
132
+ for (let hop = 0; hop <= MAX_REMOTE_IMAGE_REDIRECTS; hop += 1) {
133
+ let response;
134
+ try {
135
+ response = await fetchImpl(url.toString(), {
136
+ method: "GET",
137
+ redirect: "manual",
138
+ credentials: "omit",
139
+ headers: { accept: ACCEPT },
140
+ signal:
141
+ typeof AbortSignal !== "undefined" && typeof AbortSignal.timeout === "function"
142
+ ? AbortSignal.timeout(REMOTE_IMAGE_TIMEOUT_MS)
143
+ : undefined,
144
+ });
145
+ } catch {
146
+ return { error: `could not fetch ${url.hostname}` };
147
+ }
148
+ if (response.status >= 300 && response.status < 400) {
149
+ const next = response.headers.get("location");
150
+ let resolved = null;
151
+ try {
152
+ resolved = next ? new URL(next, url).toString() : null;
153
+ } catch {
154
+ resolved = null;
155
+ }
156
+ url = resolved ? fetchableImageUrl(resolved) : null;
157
+ if (!url) return { error: "the url redirected somewhere that is not a plain https address" };
158
+ continue;
159
+ }
160
+ if (!response.ok) return { error: `fetching the url answered ${response.status}` };
161
+ const read = await readCapped(response);
162
+ if (read.tooLarge) return { error: `image is larger than ${MAX_REMOTE_IMAGE_BYTES} bytes` };
163
+ if (read.bytes.byteLength === 0) return { error: "the image is empty" };
164
+ const type = sniffImageType(read.bytes);
165
+ if (!type) return { error: "not a PNG, JPEG, GIF, WebP or HEIC image (SVG is never served)" };
166
+ return { bytes: read.bytes, ...type };
167
+ }
168
+ return { error: `the url redirected more than ${MAX_REMOTE_IMAGE_REDIRECTS} times` };
169
+ }
170
+
171
+ module.exports = {
172
+ MAX_REMOTE_IMAGE_BYTES,
173
+ MAX_REMOTE_IMAGE_REDIRECTS,
174
+ sniffImageType,
175
+ fetchableImageUrl,
176
+ fetchRemoteImage,
177
+ };