@gadicc/loom 0.0.0-oidc-bootstrap.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 (342) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +6 -0
  3. package/README.md +448 -0
  4. package/THIRD_PARTY_NOTICES.md +25 -0
  5. package/db/drizzle-config.d.ts +20 -0
  6. package/db/drizzle-config.js +34 -0
  7. package/db/fixtures.d.ts +31 -0
  8. package/db/fixtures.js +52 -0
  9. package/db/neon.d.ts +3 -0
  10. package/db/neon.js +20 -0
  11. package/db/neonFull.d.ts +12 -0
  12. package/db/neonFull.js +33 -0
  13. package/db/package.json +3 -0
  14. package/db/testing.d.ts +26 -0
  15. package/db/testing.js +76 -0
  16. package/db/testing.test.ts +28 -0
  17. package/db/valibot.d.ts +23 -0
  18. package/db/valibot.js +8 -0
  19. package/esm/_dnt.polyfills.d.ts +149 -0
  20. package/esm/_dnt.polyfills.d.ts.map +1 -0
  21. package/esm/_dnt.polyfills.js +163 -0
  22. package/esm/_dnt.shims.d.ts +2 -0
  23. package/esm/_dnt.shims.d.ts.map +1 -0
  24. package/esm/_dnt.shims.js +57 -0
  25. package/esm/bin/loom.d.ts +2 -0
  26. package/esm/bin/loom.d.ts.map +1 -0
  27. package/esm/bin/loom.js +11 -0
  28. package/esm/mod.d.ts +9 -0
  29. package/esm/mod.d.ts.map +1 -0
  30. package/esm/mod.js +8 -0
  31. package/esm/package.json +3 -0
  32. package/esm/src/apiKeys/authorize.d.ts +4 -0
  33. package/esm/src/apiKeys/authorize.d.ts.map +1 -0
  34. package/esm/src/apiKeys/authorize.js +184 -0
  35. package/esm/src/apiKeys/country.d.ts +3 -0
  36. package/esm/src/apiKeys/country.d.ts.map +1 -0
  37. package/esm/src/apiKeys/country.js +7 -0
  38. package/esm/src/apiKeys/credentials.d.ts +4 -0
  39. package/esm/src/apiKeys/credentials.d.ts.map +1 -0
  40. package/esm/src/apiKeys/credentials.js +55 -0
  41. package/esm/src/apiKeys/fingerprint.d.ts +3 -0
  42. package/esm/src/apiKeys/fingerprint.d.ts.map +1 -0
  43. package/esm/src/apiKeys/fingerprint.js +5 -0
  44. package/esm/src/apiKeys/mod.d.ts +9 -0
  45. package/esm/src/apiKeys/mod.d.ts.map +1 -0
  46. package/esm/src/apiKeys/mod.js +8 -0
  47. package/esm/src/apiKeys/policy.d.ts +59 -0
  48. package/esm/src/apiKeys/policy.d.ts.map +1 -0
  49. package/esm/src/apiKeys/policy.js +88 -0
  50. package/esm/src/apiKeys/retention.d.ts +55 -0
  51. package/esm/src/apiKeys/retention.d.ts.map +1 -0
  52. package/esm/src/apiKeys/retention.js +65 -0
  53. package/esm/src/apiKeys/types.d.ts +175 -0
  54. package/esm/src/apiKeys/types.d.ts.map +1 -0
  55. package/esm/src/apiKeys/types.js +1 -0
  56. package/esm/src/cli/externalSkills.d.ts +39 -0
  57. package/esm/src/cli/externalSkills.d.ts.map +1 -0
  58. package/esm/src/cli/externalSkills.js +177 -0
  59. package/esm/src/cli/mod.d.ts +337 -0
  60. package/esm/src/cli/mod.d.ts.map +1 -0
  61. package/esm/src/cli/mod.js +9268 -0
  62. package/esm/src/cli/projectSkills.d.ts +102 -0
  63. package/esm/src/cli/projectSkills.d.ts.map +1 -0
  64. package/esm/src/cli/projectSkills.js +592 -0
  65. package/esm/src/files/client.d.ts +29 -0
  66. package/esm/src/files/client.d.ts.map +1 -0
  67. package/esm/src/files/client.js +41 -0
  68. package/esm/src/files/errors.d.ts +37 -0
  69. package/esm/src/files/errors.d.ts.map +1 -0
  70. package/esm/src/files/errors.js +66 -0
  71. package/esm/src/files/hash.d.ts +15 -0
  72. package/esm/src/files/hash.d.ts.map +1 -0
  73. package/esm/src/files/hash.js +82 -0
  74. package/esm/src/files/memory.d.ts +29 -0
  75. package/esm/src/files/memory.d.ts.map +1 -0
  76. package/esm/src/files/memory.js +109 -0
  77. package/esm/src/files/metadata.d.ts +10 -0
  78. package/esm/src/files/metadata.d.ts.map +1 -0
  79. package/esm/src/files/metadata.js +186 -0
  80. package/esm/src/files/mod.d.ts +10 -0
  81. package/esm/src/files/mod.d.ts.map +1 -0
  82. package/esm/src/files/mod.js +9 -0
  83. package/esm/src/files/s3.d.ts +29 -0
  84. package/esm/src/files/s3.d.ts.map +1 -0
  85. package/esm/src/files/s3.js +107 -0
  86. package/esm/src/files/service.d.ts +27 -0
  87. package/esm/src/files/service.d.ts.map +1 -0
  88. package/esm/src/files/service.js +136 -0
  89. package/esm/src/files/types.d.ts +134 -0
  90. package/esm/src/files/types.d.ts.map +1 -0
  91. package/esm/src/files/types.js +1 -0
  92. package/esm/src/jobs/catalog.d.ts +32 -0
  93. package/esm/src/jobs/catalog.d.ts.map +1 -0
  94. package/esm/src/jobs/catalog.js +208 -0
  95. package/esm/src/jobs/controller.d.ts +28 -0
  96. package/esm/src/jobs/controller.d.ts.map +1 -0
  97. package/esm/src/jobs/controller.js +95 -0
  98. package/esm/src/jobs/direct.d.ts +36 -0
  99. package/esm/src/jobs/direct.d.ts.map +1 -0
  100. package/esm/src/jobs/direct.js +122 -0
  101. package/esm/src/jobs/events.d.ts +15 -0
  102. package/esm/src/jobs/events.d.ts.map +1 -0
  103. package/esm/src/jobs/events.js +30 -0
  104. package/esm/src/jobs/format.d.ts +16 -0
  105. package/esm/src/jobs/format.d.ts.map +1 -0
  106. package/esm/src/jobs/format.js +183 -0
  107. package/esm/src/jobs/http.d.ts +22 -0
  108. package/esm/src/jobs/http.d.ts.map +1 -0
  109. package/esm/src/jobs/http.js +44 -0
  110. package/esm/src/jobs/log.d.ts +2 -0
  111. package/esm/src/jobs/log.d.ts.map +1 -0
  112. package/esm/src/jobs/log.js +10 -0
  113. package/esm/src/jobs/mod.d.ts +10 -0
  114. package/esm/src/jobs/mod.d.ts.map +1 -0
  115. package/esm/src/jobs/mod.js +9 -0
  116. package/esm/src/jobs/run.d.ts +20 -0
  117. package/esm/src/jobs/run.d.ts.map +1 -0
  118. package/esm/src/jobs/run.js +57 -0
  119. package/esm/src/jobs/types.d.ts +31 -0
  120. package/esm/src/jobs/types.d.ts.map +1 -0
  121. package/esm/src/jobs/types.js +1 -0
  122. package/esm/src/mui/apiKeys.d.ts +65 -0
  123. package/esm/src/mui/apiKeys.d.ts.map +1 -0
  124. package/esm/src/mui/apiKeys.js +301 -0
  125. package/esm/src/mui/apiKeysView.d.ts +48 -0
  126. package/esm/src/mui/apiKeysView.d.ts.map +1 -0
  127. package/esm/src/mui/apiKeysView.js +34 -0
  128. package/esm/src/mui/confirm.d.ts +12 -0
  129. package/esm/src/mui/confirm.d.ts.map +1 -0
  130. package/esm/src/mui/confirm.js +78 -0
  131. package/esm/src/mui/mod.d.ts +4 -0
  132. package/esm/src/mui/mod.d.ts.map +1 -0
  133. package/esm/src/mui/mod.js +2 -0
  134. package/esm/src/next/apiKeys/mod.d.ts +64 -0
  135. package/esm/src/next/apiKeys/mod.d.ts.map +1 -0
  136. package/esm/src/next/apiKeys/mod.js +217 -0
  137. package/esm/src/next/auth/config.d.ts +34 -0
  138. package/esm/src/next/auth/config.d.ts.map +1 -0
  139. package/esm/src/next/auth/config.js +103 -0
  140. package/esm/src/next/auth/mod.d.ts +3 -0
  141. package/esm/src/next/auth/mod.d.ts.map +1 -0
  142. package/esm/src/next/auth/mod.js +2 -0
  143. package/esm/src/next/auth/server.d.ts +28 -0
  144. package/esm/src/next/auth/server.d.ts.map +1 -0
  145. package/esm/src/next/auth/server.js +24 -0
  146. package/esm/src/next/files/mod.d.ts +3 -0
  147. package/esm/src/next/files/mod.d.ts.map +1 -0
  148. package/esm/src/next/files/mod.js +2 -0
  149. package/esm/src/next/files/route.d.ts +99 -0
  150. package/esm/src/next/files/route.d.ts.map +1 -0
  151. package/esm/src/next/files/route.js +349 -0
  152. package/esm/src/next/jobs/list.d.ts +25 -0
  153. package/esm/src/next/jobs/list.d.ts.map +1 -0
  154. package/esm/src/next/jobs/list.js +39 -0
  155. package/esm/src/next/jobs/mod.d.ts +6 -0
  156. package/esm/src/next/jobs/mod.d.ts.map +1 -0
  157. package/esm/src/next/jobs/mod.js +5 -0
  158. package/esm/src/next/jobs/paths.d.ts +14 -0
  159. package/esm/src/next/jobs/paths.d.ts.map +1 -0
  160. package/esm/src/next/jobs/paths.js +40 -0
  161. package/esm/src/next/jobs/route.d.ts +48 -0
  162. package/esm/src/next/jobs/route.d.ts.map +1 -0
  163. package/esm/src/next/jobs/route.js +101 -0
  164. package/esm/src/next/jobs/run.d.ts +51 -0
  165. package/esm/src/next/jobs/run.d.ts.map +1 -0
  166. package/esm/src/next/jobs/run.js +563 -0
  167. package/esm/src/next/jobs/stream.d.ts +3 -0
  168. package/esm/src/next/jobs/stream.d.ts.map +1 -0
  169. package/esm/src/next/jobs/stream.js +25 -0
  170. package/esm/src/next/pwa/mod.d.ts +104 -0
  171. package/esm/src/next/pwa/mod.d.ts.map +1 -0
  172. package/esm/src/next/pwa/mod.js +169 -0
  173. package/esm/src/next/pwa/worker.d.ts +27 -0
  174. package/esm/src/next/pwa/worker.d.ts.map +1 -0
  175. package/esm/src/next/pwa/worker.js +27 -0
  176. package/package.json +293 -0
  177. package/script/_dnt.polyfills.d.ts +149 -0
  178. package/script/_dnt.polyfills.d.ts.map +1 -0
  179. package/script/_dnt.polyfills.js +166 -0
  180. package/script/_dnt.shims.d.ts +2 -0
  181. package/script/_dnt.shims.d.ts.map +1 -0
  182. package/script/_dnt.shims.js +60 -0
  183. package/script/bin/loom.d.ts +2 -0
  184. package/script/bin/loom.d.ts.map +1 -0
  185. package/script/bin/loom.js +17 -0
  186. package/script/mod.d.ts +9 -0
  187. package/script/mod.d.ts.map +1 -0
  188. package/script/mod.js +24 -0
  189. package/script/package.json +3 -0
  190. package/script/src/apiKeys/authorize.d.ts +4 -0
  191. package/script/src/apiKeys/authorize.d.ts.map +1 -0
  192. package/script/src/apiKeys/authorize.js +187 -0
  193. package/script/src/apiKeys/country.d.ts +3 -0
  194. package/script/src/apiKeys/country.d.ts.map +1 -0
  195. package/script/src/apiKeys/country.js +10 -0
  196. package/script/src/apiKeys/credentials.d.ts +4 -0
  197. package/script/src/apiKeys/credentials.d.ts.map +1 -0
  198. package/script/src/apiKeys/credentials.js +58 -0
  199. package/script/src/apiKeys/fingerprint.d.ts +3 -0
  200. package/script/src/apiKeys/fingerprint.d.ts.map +1 -0
  201. package/script/src/apiKeys/fingerprint.js +8 -0
  202. package/script/src/apiKeys/mod.d.ts +9 -0
  203. package/script/src/apiKeys/mod.d.ts.map +1 -0
  204. package/script/src/apiKeys/mod.js +24 -0
  205. package/script/src/apiKeys/policy.d.ts +59 -0
  206. package/script/src/apiKeys/policy.d.ts.map +1 -0
  207. package/script/src/apiKeys/policy.js +92 -0
  208. package/script/src/apiKeys/retention.d.ts +55 -0
  209. package/script/src/apiKeys/retention.d.ts.map +1 -0
  210. package/script/src/apiKeys/retention.js +70 -0
  211. package/script/src/apiKeys/types.d.ts +175 -0
  212. package/script/src/apiKeys/types.d.ts.map +1 -0
  213. package/script/src/apiKeys/types.js +2 -0
  214. package/script/src/cli/externalSkills.d.ts +39 -0
  215. package/script/src/cli/externalSkills.d.ts.map +1 -0
  216. package/script/src/cli/externalSkills.js +181 -0
  217. package/script/src/cli/mod.d.ts +337 -0
  218. package/script/src/cli/mod.d.ts.map +1 -0
  219. package/script/src/cli/mod.js +9337 -0
  220. package/script/src/cli/projectSkills.d.ts +102 -0
  221. package/script/src/cli/projectSkills.d.ts.map +1 -0
  222. package/script/src/cli/projectSkills.js +606 -0
  223. package/script/src/files/client.d.ts +29 -0
  224. package/script/src/files/client.d.ts.map +1 -0
  225. package/script/src/files/client.js +45 -0
  226. package/script/src/files/errors.d.ts +37 -0
  227. package/script/src/files/errors.d.ts.map +1 -0
  228. package/script/src/files/errors.js +76 -0
  229. package/script/src/files/hash.d.ts +15 -0
  230. package/script/src/files/hash.d.ts.map +1 -0
  231. package/script/src/files/hash.js +89 -0
  232. package/script/src/files/memory.d.ts +29 -0
  233. package/script/src/files/memory.d.ts.map +1 -0
  234. package/script/src/files/memory.js +113 -0
  235. package/script/src/files/metadata.d.ts +10 -0
  236. package/script/src/files/metadata.d.ts.map +1 -0
  237. package/script/src/files/metadata.js +189 -0
  238. package/script/src/files/mod.d.ts +10 -0
  239. package/script/src/files/mod.d.ts.map +1 -0
  240. package/script/src/files/mod.js +25 -0
  241. package/script/src/files/s3.d.ts +29 -0
  242. package/script/src/files/s3.d.ts.map +1 -0
  243. package/script/src/files/s3.js +110 -0
  244. package/script/src/files/service.d.ts +27 -0
  245. package/script/src/files/service.d.ts.map +1 -0
  246. package/script/src/files/service.js +139 -0
  247. package/script/src/files/types.d.ts +134 -0
  248. package/script/src/files/types.d.ts.map +1 -0
  249. package/script/src/files/types.js +2 -0
  250. package/script/src/jobs/catalog.d.ts +32 -0
  251. package/script/src/jobs/catalog.d.ts.map +1 -0
  252. package/script/src/jobs/catalog.js +223 -0
  253. package/script/src/jobs/controller.d.ts +28 -0
  254. package/script/src/jobs/controller.d.ts.map +1 -0
  255. package/script/src/jobs/controller.js +100 -0
  256. package/script/src/jobs/direct.d.ts +36 -0
  257. package/script/src/jobs/direct.d.ts.map +1 -0
  258. package/script/src/jobs/direct.js +128 -0
  259. package/script/src/jobs/events.d.ts +15 -0
  260. package/script/src/jobs/events.d.ts.map +1 -0
  261. package/script/src/jobs/events.js +37 -0
  262. package/script/src/jobs/format.d.ts +16 -0
  263. package/script/src/jobs/format.d.ts.map +1 -0
  264. package/script/src/jobs/format.js +193 -0
  265. package/script/src/jobs/http.d.ts +22 -0
  266. package/script/src/jobs/http.d.ts.map +1 -0
  267. package/script/src/jobs/http.js +50 -0
  268. package/script/src/jobs/log.d.ts +2 -0
  269. package/script/src/jobs/log.d.ts.map +1 -0
  270. package/script/src/jobs/log.js +13 -0
  271. package/script/src/jobs/mod.d.ts +10 -0
  272. package/script/src/jobs/mod.d.ts.map +1 -0
  273. package/script/src/jobs/mod.js +25 -0
  274. package/script/src/jobs/run.d.ts +20 -0
  275. package/script/src/jobs/run.d.ts.map +1 -0
  276. package/script/src/jobs/run.js +61 -0
  277. package/script/src/jobs/types.d.ts +31 -0
  278. package/script/src/jobs/types.d.ts.map +1 -0
  279. package/script/src/jobs/types.js +2 -0
  280. package/script/src/mui/apiKeys.d.ts +65 -0
  281. package/script/src/mui/apiKeys.d.ts.map +1 -0
  282. package/script/src/mui/apiKeys.js +327 -0
  283. package/script/src/mui/apiKeysView.d.ts +48 -0
  284. package/script/src/mui/apiKeysView.d.ts.map +1 -0
  285. package/script/src/mui/apiKeysView.js +40 -0
  286. package/script/src/mui/confirm.d.ts +12 -0
  287. package/script/src/mui/confirm.d.ts.map +1 -0
  288. package/script/src/mui/confirm.js +85 -0
  289. package/script/src/mui/mod.d.ts +4 -0
  290. package/script/src/mui/mod.d.ts.map +1 -0
  291. package/script/src/mui/mod.js +19 -0
  292. package/script/src/next/apiKeys/mod.d.ts +64 -0
  293. package/script/src/next/apiKeys/mod.d.ts.map +1 -0
  294. package/script/src/next/apiKeys/mod.js +224 -0
  295. package/script/src/next/auth/config.d.ts +34 -0
  296. package/script/src/next/auth/config.d.ts.map +1 -0
  297. package/script/src/next/auth/config.js +142 -0
  298. package/script/src/next/auth/mod.d.ts +3 -0
  299. package/script/src/next/auth/mod.d.ts.map +1 -0
  300. package/script/src/next/auth/mod.js +18 -0
  301. package/script/src/next/auth/server.d.ts +28 -0
  302. package/script/src/next/auth/server.d.ts.map +1 -0
  303. package/script/src/next/auth/server.js +27 -0
  304. package/script/src/next/files/mod.d.ts +3 -0
  305. package/script/src/next/files/mod.d.ts.map +1 -0
  306. package/script/src/next/files/mod.js +18 -0
  307. package/script/src/next/files/route.d.ts +99 -0
  308. package/script/src/next/files/route.d.ts.map +1 -0
  309. package/script/src/next/files/route.js +353 -0
  310. package/script/src/next/jobs/list.d.ts +25 -0
  311. package/script/src/next/jobs/list.d.ts.map +1 -0
  312. package/script/src/next/jobs/list.js +48 -0
  313. package/script/src/next/jobs/mod.d.ts +6 -0
  314. package/script/src/next/jobs/mod.d.ts.map +1 -0
  315. package/script/src/next/jobs/mod.js +21 -0
  316. package/script/src/next/jobs/paths.d.ts +14 -0
  317. package/script/src/next/jobs/paths.d.ts.map +1 -0
  318. package/script/src/next/jobs/paths.js +47 -0
  319. package/script/src/next/jobs/route.d.ts +48 -0
  320. package/script/src/next/jobs/route.d.ts.map +1 -0
  321. package/script/src/next/jobs/route.js +105 -0
  322. package/script/src/next/jobs/run.d.ts +51 -0
  323. package/script/src/next/jobs/run.d.ts.map +1 -0
  324. package/script/src/next/jobs/run.js +570 -0
  325. package/script/src/next/jobs/stream.d.ts +3 -0
  326. package/script/src/next/jobs/stream.d.ts.map +1 -0
  327. package/script/src/next/jobs/stream.js +28 -0
  328. package/script/src/next/pwa/mod.d.ts +104 -0
  329. package/script/src/next/pwa/mod.d.ts.map +1 -0
  330. package/script/src/next/pwa/mod.js +192 -0
  331. package/script/src/next/pwa/worker.d.ts +27 -0
  332. package/script/src/next/pwa/worker.d.ts.map +1 -0
  333. package/script/src/next/pwa/worker.js +33 -0
  334. package/skills/loom/SKILL.md +200 -0
  335. package/skills/loom-api-keys/SKILL.md +123 -0
  336. package/skills/loom-auth/SKILL.md +70 -0
  337. package/skills/loom-db/SKILL.md +94 -0
  338. package/skills/loom-db/references/db-stack.md +39 -0
  339. package/skills/loom-files/SKILL.md +97 -0
  340. package/skills/loom-jobs/SKILL.md +148 -0
  341. package/skills/upgrade-deps/SKILL.md +165 -0
  342. package/skills/upgrade-deps/agents/openai.yaml +4 -0
@@ -0,0 +1,107 @@
1
+ import { LoomFileDuplicateError } from "./errors.js";
2
+ function joinS3Key(prefix, key) {
3
+ const cleanPrefix = prefix?.replaceAll(/^\/+|\/+$/g, "");
4
+ const cleanKey = key.replaceAll(/^\/+/g, "");
5
+ return cleanPrefix ? `${cleanPrefix}/${cleanKey}` : cleanKey;
6
+ }
7
+ async function s3BodyToBodyInit(body) {
8
+ if (body instanceof Blob || body instanceof ReadableStream) {
9
+ return body;
10
+ }
11
+ if (body instanceof ArrayBuffer) {
12
+ return body;
13
+ }
14
+ if (ArrayBuffer.isView(body)) {
15
+ return new Blob([
16
+ new Uint8Array(body.buffer, body.byteOffset, body.byteLength).slice(),
17
+ ]);
18
+ }
19
+ if (typeof body === "string") {
20
+ return body;
21
+ }
22
+ const transformable = body;
23
+ if (transformable?.transformToWebStream) {
24
+ return transformable.transformToWebStream();
25
+ }
26
+ if (transformable?.transformToByteArray) {
27
+ return new Blob([(await transformable.transformToByteArray()).slice()]);
28
+ }
29
+ throw new TypeError("Unsupported S3 object body.");
30
+ }
31
+ function optionalString(value) {
32
+ return typeof value === "string" && value ? value : null;
33
+ }
34
+ function optionalNumber(value) {
35
+ return typeof value === "number" ? value : undefined;
36
+ }
37
+ function isObjectAlreadyExistsError(error) {
38
+ if (typeof error !== "object" || error === null) {
39
+ return false;
40
+ }
41
+ const candidate = error;
42
+ return (candidate.$metadata?.httpStatusCode === 409 ||
43
+ candidate.$metadata?.httpStatusCode === 412 ||
44
+ candidate.name === "ConditionalRequestConflict" ||
45
+ candidate.name === "PreconditionFailed");
46
+ }
47
+ /**
48
+ * Creates an S3-compatible storage adapter without making AWS SDK v3 a Loom
49
+ * dependency. Consumers pass their own client and command constructors.
50
+ */
51
+ export function createS3FileStorage(options) {
52
+ const provider = options.provider ?? "s3";
53
+ return {
54
+ bucket: options.bucket,
55
+ provider,
56
+ deleteObject(input) {
57
+ if (!options.commands.DeleteObjectCommand) {
58
+ return Promise.resolve();
59
+ }
60
+ return options.client.send(new options.commands.DeleteObjectCommand({
61
+ Bucket: input.bucket ?? options.bucket,
62
+ Key: input.objectKey,
63
+ })).then(() => undefined);
64
+ },
65
+ async getObject(input) {
66
+ const result = await options.client.send(new options.commands.GetObjectCommand({
67
+ Bucket: input.bucket ?? options.bucket,
68
+ Key: input.objectKey,
69
+ }));
70
+ if (!("Body" in result) || result.Body === undefined) {
71
+ return null;
72
+ }
73
+ return {
74
+ body: await s3BodyToBodyInit(result.Body),
75
+ byteSize: optionalNumber(result.ContentLength),
76
+ contentType: optionalString(result.ContentType),
77
+ etag: optionalString(result.ETag) ?? undefined,
78
+ };
79
+ },
80
+ async putObject(input) {
81
+ const objectKey = joinS3Key(options.keyPrefix, input.objectKey);
82
+ try {
83
+ await options.client.send(new options.commands.PutObjectCommand({
84
+ Body: input.body,
85
+ Bucket: options.bucket,
86
+ ContentLength: input.byteSize,
87
+ ContentType: input.detectedContentType ?? input.contentType ??
88
+ undefined,
89
+ IfNoneMatch: options.preventOverwrite ? "*" : undefined,
90
+ Key: objectKey,
91
+ Metadata: input.metadata,
92
+ }));
93
+ }
94
+ catch (error) {
95
+ if (options.preventOverwrite && isObjectAlreadyExistsError(error)) {
96
+ throw new LoomFileDuplicateError("Stored file object already exists.");
97
+ }
98
+ throw error;
99
+ }
100
+ return {
101
+ bucket: options.bucket,
102
+ objectKey,
103
+ storageProvider: provider,
104
+ };
105
+ },
106
+ };
107
+ }
@@ -0,0 +1,27 @@
1
+ import type { LoomFileRecord, LoomFileRepository, LoomFileService, LoomFileStorageAdapter } from "./types.js";
2
+ export interface CreateLoomFileServiceOptions<TRecord extends LoomFileRecord = LoomFileRecord> {
3
+ /**
4
+ * Controls what happens when the same SHA-256 digest already has an active
5
+ * record. `reuse` preserves content-addressed dedupe, `reject` returns a
6
+ * 409-style LoomFileDuplicateError, and `store` preserves the legacy
7
+ * `reuseExisting: false` behavior of attempting another storage/DB write.
8
+ */
9
+ duplicatePolicy?: "reject" | "reuse" | "store";
10
+ objectKey?: (input: {
11
+ byteSize: number;
12
+ sha256: string;
13
+ }) => string;
14
+ repository: LoomFileRepository<TRecord>;
15
+ /**
16
+ * @deprecated Use duplicatePolicy instead. `false` maps to `store` unless
17
+ * duplicatePolicy is explicitly provided.
18
+ */
19
+ reuseExisting?: boolean;
20
+ storage: LoomFileStorageAdapter;
21
+ }
22
+ /**
23
+ * Creates the core files service used by route handlers and app-local adapters.
24
+ * It dedupes by SHA-256 before writing to object storage by default.
25
+ */
26
+ export declare function createLoomFileService<TRecord extends LoomFileRecord = LoomFileRecord>(options: CreateLoomFileServiceOptions<TRecord>): LoomFileService<TRecord>;
27
+ //# sourceMappingURL=service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../../../src/src/files/service.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EACV,cAAc,EACd,kBAAkB,EAClB,eAAe,EACf,sBAAsB,EAGvB,MAAM,YAAY,CAAC;AAEpB,MAAM,WAAW,4BAA4B,CAC3C,OAAO,SAAS,cAAc,GAAG,cAAc;IAE/C;;;;;OAKG;IACH,eAAe,CAAC,EAAE,QAAQ,GAAG,OAAO,GAAG,OAAO,CAAC;IAC/C,SAAS,CAAC,EAAE,CAAC,KAAK,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,KAAK,MAAM,CAAC;IACpE,UAAU,EAAE,kBAAkB,CAAC,OAAO,CAAC,CAAC;IACxC;;;OAGG;IACH,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,OAAO,EAAE,sBAAsB,CAAC;CACjC;AAkDD;;;GAGG;AACH,wBAAgB,qBAAqB,CACnC,OAAO,SAAS,cAAc,GAAG,cAAc,EAE/C,OAAO,EAAE,4BAA4B,CAAC,OAAO,CAAC,GAC7C,eAAe,CAAC,OAAO,CAAC,CAgH1B"}
@@ -0,0 +1,136 @@
1
+ import { LoomFileDuplicateError, LoomFileNotFoundError, LoomFileStorageReadUnsupportedError, } from "./errors.js";
2
+ import { assertExpectedSha256, readLoomFileBodyBytes, sha256Hex, } from "./hash.js";
3
+ import { detectLoomFileMetadata } from "./metadata.js";
4
+ function normalizeNullableText(value) {
5
+ const normalized = value?.trim();
6
+ return normalized ? normalized : null;
7
+ }
8
+ function isActiveRecord(record) {
9
+ return !record.deletedAt;
10
+ }
11
+ const visibilityRank = {
12
+ private: 0,
13
+ authenticated: 1,
14
+ public: 2,
15
+ };
16
+ function normalizeVisibility(visibility) {
17
+ return visibility ?? "private";
18
+ }
19
+ function mostPermissiveVisibility(a, b) {
20
+ const normalizedA = normalizeVisibility(a);
21
+ const normalizedB = normalizeVisibility(b);
22
+ return visibilityRank[normalizedB] > visibilityRank[normalizedA]
23
+ ? normalizedB
24
+ : normalizedA;
25
+ }
26
+ async function maybeUpgradeExistingVisibility(repository, record, requestedVisibility) {
27
+ const nextVisibility = mostPermissiveVisibility(record.visibility, requestedVisibility);
28
+ if (nextVisibility === record.visibility)
29
+ return record;
30
+ return await repository.updateVisibility?.(record.id, nextVisibility) ??
31
+ record;
32
+ }
33
+ /**
34
+ * Creates the core files service used by route handlers and app-local adapters.
35
+ * It dedupes by SHA-256 before writing to object storage by default.
36
+ */
37
+ export function createLoomFileService(options) {
38
+ const duplicatePolicy = options.duplicatePolicy ??
39
+ (options.reuseExisting === false ? "store" : "reuse");
40
+ return {
41
+ async getDownloadObject(record) {
42
+ if (!options.storage.getObject) {
43
+ throw new LoomFileStorageReadUnsupportedError();
44
+ }
45
+ const object = await options.storage.getObject({
46
+ bucket: record.bucket,
47
+ objectKey: record.objectKey,
48
+ record,
49
+ });
50
+ if (!object) {
51
+ throw new LoomFileNotFoundError("Stored file object not found.");
52
+ }
53
+ return object;
54
+ },
55
+ async getFileById(id) {
56
+ if (!options.repository.findById) {
57
+ throw new LoomFileNotFoundError("This files repository does not support id lookups.");
58
+ }
59
+ const record = await options.repository.findById(id);
60
+ return record && isActiveRecord(record) ? record : null;
61
+ },
62
+ async getFileBySha256(sha256) {
63
+ const record = await options.repository.findBySha256(sha256);
64
+ return record && isActiveRecord(record) ? record : null;
65
+ },
66
+ async saveFile(input) {
67
+ const bytes = await readLoomFileBodyBytes(input.body, {
68
+ maxBytes: input.maxBytes,
69
+ });
70
+ const sha256 = await sha256Hex(bytes);
71
+ assertExpectedSha256(sha256, input.expectedSha256 ?? null);
72
+ const existing = await options.repository.findBySha256(sha256);
73
+ if (existing && isActiveRecord(existing)) {
74
+ if (duplicatePolicy === "reject") {
75
+ throw new LoomFileDuplicateError();
76
+ }
77
+ if (duplicatePolicy === "reuse") {
78
+ const record = await maybeUpgradeExistingVisibility(options.repository, existing, input.visibility);
79
+ return {
80
+ byteSize: record.byteSize,
81
+ created: false,
82
+ detectedContentType: record.detectedContentType,
83
+ kind: record.kind,
84
+ record,
85
+ reusedExisting: true,
86
+ sha256,
87
+ stored: false,
88
+ };
89
+ }
90
+ }
91
+ const contentType = normalizeNullableText(input.contentType);
92
+ const detected = detectLoomFileMetadata(bytes, { contentType });
93
+ const objectKey = input.objectKey ??
94
+ options.objectKey?.({ byteSize: bytes.byteLength, sha256 }) ??
95
+ sha256;
96
+ const storageReference = await options.storage.putObject({
97
+ body: bytes,
98
+ byteSize: bytes.byteLength,
99
+ contentType,
100
+ detectedContentType: detected.detectedContentType,
101
+ metadata: {
102
+ sha256,
103
+ },
104
+ objectKey,
105
+ sha256,
106
+ });
107
+ const record = await options.repository.insert({
108
+ bucket: storageReference.bucket ?? options.storage.bucket ?? null,
109
+ byteSize: bytes.byteLength,
110
+ contentType,
111
+ detectedContentType: detected.detectedContentType,
112
+ imageMeta: detected.imageMeta,
113
+ kind: detected.kind,
114
+ meta: input.meta ?? {},
115
+ objectKey: storageReference.objectKey,
116
+ originalFilename: normalizeNullableText(input.originalFilename),
117
+ ownerId: normalizeNullableText(input.ownerId),
118
+ ownerType: normalizeNullableText(input.ownerType),
119
+ sha256,
120
+ storageProvider: storageReference.storageProvider ??
121
+ options.storage.provider,
122
+ visibility: normalizeVisibility(input.visibility),
123
+ });
124
+ return {
125
+ byteSize: bytes.byteLength,
126
+ created: true,
127
+ detectedContentType: detected.detectedContentType,
128
+ kind: detected.kind,
129
+ record,
130
+ reusedExisting: false,
131
+ sha256,
132
+ stored: true,
133
+ };
134
+ },
135
+ };
136
+ }
@@ -0,0 +1,134 @@
1
+ export type LoomFileKind = "archive" | "audio" | "image" | "other" | "pdf" | "text" | "video";
2
+ export type LoomFileBody = ArrayBuffer | ArrayBufferView | Blob | ReadableStream<Uint8Array>;
3
+ export interface LoomFileOwner {
4
+ ownerId: string;
5
+ ownerType: string;
6
+ }
7
+ export type LoomFileVisibility = "authenticated" | "private" | "public";
8
+ export interface LoomImageMetadata extends Record<string, unknown> {
9
+ format: string;
10
+ height: number;
11
+ width: number;
12
+ }
13
+ export interface LoomFileDetectedMetadata {
14
+ detectedContentType: string | null;
15
+ imageMeta: LoomImageMetadata | null;
16
+ kind: LoomFileKind;
17
+ }
18
+ /**
19
+ * Canonical file metadata shape mirrored by the managed `files` schema
20
+ * blueprint. Apps may add domain associations in separate local tables.
21
+ */
22
+ export interface LoomFileRecord {
23
+ audioMeta: Record<string, unknown> | null;
24
+ bucket: string | null;
25
+ byteSize: number;
26
+ contentType: string | null;
27
+ createdAt?: Date;
28
+ deletedAt?: Date | null;
29
+ detectedContentType: string | null;
30
+ id: string;
31
+ imageMeta: Record<string, unknown> | null;
32
+ kind: LoomFileKind;
33
+ meta: Record<string, unknown>;
34
+ objectKey: string;
35
+ originalFilename: string | null;
36
+ ownerId: string | null;
37
+ ownerType: string | null;
38
+ sha256: string;
39
+ storageProvider: string;
40
+ updatedAt?: Date;
41
+ visibility: LoomFileVisibility;
42
+ }
43
+ export interface LoomFileCreateInput {
44
+ audioMeta?: Record<string, unknown> | null;
45
+ bucket?: string | null;
46
+ byteSize: number;
47
+ contentType?: string | null;
48
+ detectedContentType?: string | null;
49
+ imageMeta?: Record<string, unknown> | null;
50
+ kind: LoomFileKind;
51
+ meta?: Record<string, unknown>;
52
+ objectKey: string;
53
+ originalFilename?: string | null;
54
+ ownerId?: string | null;
55
+ ownerType?: string | null;
56
+ sha256: string;
57
+ storageProvider: string;
58
+ visibility?: LoomFileVisibility;
59
+ }
60
+ /**
61
+ * Persistence adapter for the app-local Drizzle table materialized by
62
+ * `loom add files`.
63
+ */
64
+ export interface LoomFileRepository<TRecord extends LoomFileRecord = LoomFileRecord> {
65
+ findById?(id: string): Promise<TRecord | null>;
66
+ findBySha256(sha256: string): Promise<TRecord | null>;
67
+ insert(input: LoomFileCreateInput): Promise<TRecord>;
68
+ updateVisibility?(id: string, visibility: LoomFileVisibility): Promise<TRecord>;
69
+ }
70
+ export interface LoomFileStorageReference {
71
+ bucket?: string | null;
72
+ objectKey: string;
73
+ storageProvider?: string;
74
+ }
75
+ export interface LoomFileStoragePutInput {
76
+ body: Uint8Array;
77
+ byteSize: number;
78
+ contentType?: string | null;
79
+ detectedContentType?: string | null;
80
+ metadata?: Record<string, string>;
81
+ objectKey: string;
82
+ sha256: string;
83
+ }
84
+ export interface LoomFileStorageGetInput {
85
+ bucket?: string | null;
86
+ objectKey: string;
87
+ record?: LoomFileRecord;
88
+ }
89
+ export interface LoomFileStorageGetResult {
90
+ body: BodyInit;
91
+ byteSize?: number;
92
+ contentType?: string | null;
93
+ etag?: string;
94
+ }
95
+ /**
96
+ * Object-storage adapter boundary. Provider-specific packages such as S3 should
97
+ * implement this instead of leaking SDK types through the core runtime.
98
+ */
99
+ export interface LoomFileStorageAdapter {
100
+ readonly bucket?: string | null;
101
+ readonly provider: string;
102
+ deleteObject?(input: LoomFileStorageGetInput): Promise<void>;
103
+ getObject?(input: LoomFileStorageGetInput): Promise<LoomFileStorageGetResult | null>;
104
+ putObject(input: LoomFileStoragePutInput): Promise<LoomFileStorageReference>;
105
+ }
106
+ export interface SaveLoomFileInput {
107
+ body: LoomFileBody;
108
+ contentType?: string | null;
109
+ expectedSha256?: string | null;
110
+ maxBytes?: number;
111
+ meta?: Record<string, unknown>;
112
+ objectKey?: string;
113
+ originalFilename?: string | null;
114
+ ownerId?: string | null;
115
+ ownerType?: string | null;
116
+ visibility?: LoomFileVisibility;
117
+ }
118
+ export interface SaveLoomFileResult<TRecord extends LoomFileRecord = LoomFileRecord> {
119
+ byteSize: number;
120
+ created: boolean;
121
+ detectedContentType: string | null;
122
+ kind: LoomFileKind;
123
+ record: TRecord;
124
+ reusedExisting: boolean;
125
+ sha256: string;
126
+ stored: boolean;
127
+ }
128
+ export interface LoomFileService<TRecord extends LoomFileRecord = LoomFileRecord> {
129
+ getDownloadObject(record: TRecord): Promise<LoomFileStorageGetResult>;
130
+ getFileById(id: string): Promise<TRecord | null>;
131
+ getFileBySha256(sha256: string): Promise<TRecord | null>;
132
+ saveFile(input: SaveLoomFileInput): Promise<SaveLoomFileResult<TRecord>>;
133
+ }
134
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/src/files/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,YAAY,GACpB,SAAS,GACT,OAAO,GACP,OAAO,GACP,OAAO,GACP,KAAK,GACL,MAAM,GACN,OAAO,CAAC;AAEZ,MAAM,MAAM,YAAY,GACpB,WAAW,GACX,eAAe,GACf,IAAI,GACJ,cAAc,CAAC,UAAU,CAAC,CAAC;AAE/B,MAAM,WAAW,aAAa;IAC5B,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,MAAM,kBAAkB,GAAG,eAAe,GAAG,SAAS,GAAG,QAAQ,CAAC;AAExE,MAAM,WAAW,iBAAkB,SAAQ,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAChE,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,wBAAwB;IACvC,mBAAmB,EAAE,MAAM,GAAG,IAAI,CAAC;IACnC,SAAS,EAAE,iBAAiB,GAAG,IAAI,CAAC;IACpC,IAAI,EAAE,YAAY,CAAC;CACpB;AAED;;;GAGG;AACH,MAAM,WAAW,cAAc;IAC7B,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAAC;IAC1C,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,EAAE,MAAM,GAAG,IAA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ACnB,MAAM,EAAE,OAAO,CAAC;IAChB,cAAc,EAAE,OAAO,CAAC;IACxB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,OAAO,CAAC;CACjB;AAED,MAAM,WAAW,eAAe,CAC9B,OAAO,SAAS,cAAc,GAAG,cAAc;IAE/C,iBAAiB,CAAC,MAAM,EAAE,OAAO,GAAG,OAAO,CAAC,wBAAwB,CAAC,CAAC;IACtE,WAAW,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC,CAAC;IACjD,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC,CAAC;IACzD,QAAQ,CAAC,KAAK,EAAE,iBAAiB,GAAG,OAAO,CAAC,kBAAkB,CAAC,OAAO,CAAC,CAAC,CAAC;CAC1E"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,32 @@
1
+ import type { JobCatalog, JobDefinition, JobMeta, JobParams, JobParamsDefinition } from "./types.js";
2
+ type JobModule<TParams extends Record<string, any>> = Pick<JobDefinition<TParams>, "handler" | "meta">;
3
+ /**
4
+ * Create a catalog entry that exposes metadata immediately and loads its job
5
+ * implementation only when selected for execution.
6
+ *
7
+ * This prevents HTTP routes and direct runners from evaluating every job's
8
+ * transitive dependencies while constructing a catalog.
9
+ */
10
+ export declare function createLazyJob<TParams extends Record<string, any> = Record<string, any>>(meta: JobDefinition<TParams>["meta"], load: () => Promise<JobModule<TParams>>): JobDefinition<TParams>;
11
+ export declare function createJobCatalog<const TJobs extends readonly JobDefinition[]>(jobs: TJobs): JobCatalog<TJobs[number]>;
12
+ export declare function getJob<TJob extends JobDefinition>(catalog: JobCatalog<TJob>, id: string): TJob | undefined;
13
+ export declare function isJobMeta(value: unknown): value is JobMeta;
14
+ export declare function isJobDefinition(value: unknown): value is JobDefinition;
15
+ export declare function isJobCatalog<TJob extends JobDefinition>(value: unknown): value is JobCatalog<TJob>;
16
+ export declare function collectJobDefinitions(source: Record<string, unknown> | Iterable<unknown>): JobDefinition[];
17
+ export declare function collectJobMetas(source: Record<string, unknown> | Iterable<unknown>): JobMeta[];
18
+ export declare function createJobCatalogFromSource<TJob extends JobDefinition>(source: JobCatalog<TJob> | Record<string, unknown> | Iterable<unknown>): JobCatalog<TJob>;
19
+ export interface ResolvedJobSelection<TJob extends JobDefinition> {
20
+ id: string;
21
+ job: TJob;
22
+ index: number | null;
23
+ }
24
+ export declare function resolveJobSelection<TJob extends JobDefinition>(catalog: JobCatalog<TJob>, selection: string): ResolvedJobSelection<TJob>;
25
+ export declare function createJobUsageText<TJob extends JobDefinition>(catalog: JobCatalog<TJob>, command?: string): string;
26
+ export declare function validateAndParseParams(rawParams: Record<string, string>, paramsDef?: JobParamsDefinition): {
27
+ parsed: JobParams;
28
+ errors: string[];
29
+ };
30
+ export declare function createJobHelpText(meta: JobMeta, command?: string): string;
31
+ export {};
32
+ //# sourceMappingURL=catalog.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"catalog.d.ts","sourceRoot":"","sources":["../../../src/src/jobs/catalog.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,UAAU,EACV,aAAa,EACb,OAAO,EACP,SAAS,EACT,mBAAmB,EACpB,MAAM,YAAY,CAAC;AAEpB,KAAK,SAAS,CAAC,OAAO,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,IAAI,IAAI,CACxD,aAAa,CAAC,OAAO,CAAC,EACtB,SAAS,GAAG,MAAM,CACnB,CAAC;AAEF;;;;;;GAMG;AACH,wBAAgB,aAAa,CAC3B,OAAO,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAEzD,IAAI,EAAE,aAAa,CAAC,OAAO,CAAC,CAAC,MAAM,CAAC,EACpC,IAAI,EAAE,MAAM,OAAO,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,GACtC,aAAa,CAA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S,aAAa,EAC5D,OAAO,EAAE,UAAU,CAAC,IAAI,CAAC,EACzB,SAAS,EAAE,MAAM,GAChB,oBAAoB,CAAC,IAAI,CAAC,CA6B5B;AAED,wBAAgB,kBAAkB,CAAC,IAAI,SAAS,aAAa,EAC3D,OAAO,EAAE,UAAU,CAAC,IAAI,CAAC,EACzB,OAAO,SAAa,GACnB,MAAM,CAgBR;AAED,wBAAgB,sBAAsB,CACpC,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EACjC,SAAS,CAAC,EAAE,mBAAmB,GAC9B;IAAE,MAAM,EAAE,SAAS,CAAC;IAAC,MAAM,EAAE,MAAM,EAAE,CAAA;CAAE,CAsDzC;AAED,wBAAgB,iBAAiB,CAC/B,IAAI,EAAE,OAAO,EACb,OAAO,SAAa,GACnB,MAAM,CAmCR"}
@@ -0,0 +1,208 @@
1
+ /**
2
+ * Create a catalog entry that exposes metadata immediately and loads its job
3
+ * implementation only when selected for execution.
4
+ *
5
+ * This prevents HTTP routes and direct runners from evaluating every job's
6
+ * transitive dependencies while constructing a catalog.
7
+ */
8
+ export function createLazyJob(meta, load) {
9
+ let loadedJob;
10
+ return {
11
+ meta,
12
+ async handler(controller, params) {
13
+ loadedJob ??= load().then((job) => {
14
+ if (job.meta.id !== meta.id) {
15
+ throw new Error(`Lazy job metadata mismatch: expected "${meta.id}", loaded "${job.meta.id}"`);
16
+ }
17
+ return job;
18
+ });
19
+ const job = await loadedJob;
20
+ await job.handler(controller, params);
21
+ },
22
+ };
23
+ }
24
+ export function createJobCatalog(jobs) {
25
+ const registry = {};
26
+ const metas = jobs.map((job) => job.meta);
27
+ for (const job of jobs) {
28
+ const id = job.meta.id.trim();
29
+ if (!id) {
30
+ throw new Error("Job meta.id must be non-empty.");
31
+ }
32
+ if (id in registry) {
33
+ throw new Error(`Duplicate job id "${id}".`);
34
+ }
35
+ registry[id] = job;
36
+ }
37
+ return {
38
+ jobs: Object.freeze(registry),
39
+ metas: Object.freeze([...metas]),
40
+ ids: Object.freeze(metas.map((meta) => meta.id)),
41
+ };
42
+ }
43
+ export function getJob(catalog, id) {
44
+ return catalog.jobs[id];
45
+ }
46
+ export function isJobMeta(value) {
47
+ if (!value || typeof value !== "object") {
48
+ return false;
49
+ }
50
+ const meta = value;
51
+ return typeof meta.id === "string" && typeof meta.desc === "string";
52
+ }
53
+ export function isJobDefinition(value) {
54
+ if (!value || typeof value !== "object") {
55
+ return false;
56
+ }
57
+ const job = value;
58
+ return isJobMeta(job.meta) && typeof job.handler === "function";
59
+ }
60
+ export function isJobCatalog(value) {
61
+ if (!value || typeof value !== "object") {
62
+ return false;
63
+ }
64
+ const catalog = value;
65
+ return (!!catalog.jobs &&
66
+ typeof catalog.jobs === "object" &&
67
+ Array.isArray(catalog.metas) &&
68
+ Array.isArray(catalog.ids));
69
+ }
70
+ function getSourceValues(source) {
71
+ if (Symbol.iterator in Object(source)) {
72
+ return Array.from(source);
73
+ }
74
+ return Object.values(source);
75
+ }
76
+ export function collectJobDefinitions(source) {
77
+ return getSourceValues(source).filter(isJobDefinition);
78
+ }
79
+ export function collectJobMetas(source) {
80
+ return getSourceValues(source).filter(isJobMeta);
81
+ }
82
+ export function createJobCatalogFromSource(source) {
83
+ if (isJobCatalog(source)) {
84
+ return source;
85
+ }
86
+ return createJobCatalog(collectJobDefinitions(source));
87
+ }
88
+ export function resolveJobSelection(catalog, selection) {
89
+ if (!selection) {
90
+ throw new Error("Please provide a job ID as the first argument.");
91
+ }
92
+ const jobIndex = Number.parseInt(selection, 10);
93
+ if (jobIndex.toString() === selection) {
94
+ const meta = catalog.metas[jobIndex];
95
+ if (!meta) {
96
+ throw new Error(`Invalid job number "${jobIndex}".`);
97
+ }
98
+ return {
99
+ id: meta.id,
100
+ job: catalog.jobs[meta.id],
101
+ index: jobIndex,
102
+ };
103
+ }
104
+ const job = catalog.jobs[selection];
105
+ if (!job) {
106
+ throw new Error(`No job found with ID "${selection}".`);
107
+ }
108
+ return {
109
+ id: selection,
110
+ job,
111
+ index: null,
112
+ };
113
+ }
114
+ export function createJobUsageText(catalog, command = "pnpm job") {
115
+ const lines = [
116
+ `Usage: ${command} <jobId>[?key=value&key2=value2]`,
117
+ "",
118
+ "Available jobs:",
119
+ ];
120
+ catalog.metas.forEach((meta, index) => {
121
+ lines.push(` - [${index.toString().padStart(2, " ")}] ${meta.id}: ${meta.desc}`);
122
+ });
123
+ lines.push("");
124
+ return lines.join("\n");
125
+ }
126
+ export function validateAndParseParams(rawParams, paramsDef) {
127
+ const parsed = {};
128
+ const errors = [];
129
+ if (!paramsDef) {
130
+ return { parsed: rawParams, errors: [] };
131
+ }
132
+ for (const [key, def] of Object.entries(paramsDef)) {
133
+ const rawVal = rawParams[key];
134
+ if (rawVal === undefined) {
135
+ if (def.required) {
136
+ errors.push(`Missing required option: --${key}`);
137
+ }
138
+ else if (def.default !== undefined) {
139
+ parsed[key] = def.default;
140
+ }
141
+ continue;
142
+ }
143
+ if (def.type === "boolean") {
144
+ const lower = rawVal.toLowerCase();
145
+ if (lower === "true" || lower === "" || lower === "1" || lower === "yes") {
146
+ parsed[key] = true;
147
+ }
148
+ else if (lower === "false" || lower === "0" || lower === "no") {
149
+ parsed[key] = false;
150
+ }
151
+ else {
152
+ errors.push(`Invalid value for boolean option --${key}: "${rawVal}". Expected true or false.`);
153
+ }
154
+ }
155
+ else if (def.type === "number") {
156
+ const num = Number(rawVal);
157
+ if (Number.isNaN(num)) {
158
+ errors.push(`Invalid value for number option --${key}: "${rawVal}". Expected a number.`);
159
+ }
160
+ else {
161
+ parsed[key] = num;
162
+ }
163
+ }
164
+ else {
165
+ parsed[key] = rawVal;
166
+ }
167
+ }
168
+ for (const key of Object.keys(rawParams)) {
169
+ if (!(key in paramsDef)) {
170
+ errors.push(`Unknown option: --${key}`);
171
+ }
172
+ }
173
+ return { parsed, errors };
174
+ }
175
+ export function createJobHelpText(meta, command = "pnpm job") {
176
+ const lines = [
177
+ `Job: ${meta.id}`,
178
+ `Description: ${meta.desc}`,
179
+ "",
180
+ `Usage: ${command} ${meta.id} [options]`,
181
+ ];
182
+ if (meta.params && Object.keys(meta.params).length > 0) {
183
+ lines.push("", "Options:");
184
+ const keys = Object.keys(meta.params);
185
+ const maxKeyLength = Math.max(...keys.map((k) => k.length));
186
+ for (const [key, def] of Object.entries(meta.params)) {
187
+ const typeStr = `<${def.type}>`;
188
+ const optPart = `--${key} ${typeStr}`.padEnd(maxKeyLength + 12, " ");
189
+ let descPart = def.desc;
190
+ const details = [];
191
+ if (def.required) {
192
+ details.push("required");
193
+ }
194
+ if (def.default !== undefined) {
195
+ details.push(`default: ${JSON.stringify(def.default)}`);
196
+ }
197
+ if (details.length > 0) {
198
+ descPart += ` (${details.join(", ")})`;
199
+ }
200
+ lines.push(` ${optPart}${descPart}`);
201
+ }
202
+ }
203
+ else {
204
+ lines.push("", "Options: None");
205
+ }
206
+ lines.push("");
207
+ return lines.join("\n");
208
+ }