@sixb/agent 0.0.0-stage → 0.2.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 (240) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +31 -2
  3. package/dist/abort.d.ts +6 -0
  4. package/dist/abort.d.ts.map +1 -0
  5. package/dist/agent-prompt.d.ts +126 -0
  6. package/dist/agent-prompt.d.ts.map +1 -0
  7. package/dist/agent-runtime/errors.d.ts +13 -0
  8. package/dist/agent-runtime/errors.d.ts.map +1 -0
  9. package/dist/agent-runtime/preflight.d.ts +18 -0
  10. package/dist/agent-runtime/preflight.d.ts.map +1 -0
  11. package/dist/agent-runtime/profile.d.ts +14 -0
  12. package/dist/agent-runtime/profile.d.ts.map +1 -0
  13. package/dist/artifacts/layout-check/layout-check.d.ts +87 -0
  14. package/dist/artifacts/layout-check/layout-check.d.ts.map +1 -0
  15. package/dist/artifacts/materialize.d.ts +115 -0
  16. package/dist/artifacts/materialize.d.ts.map +1 -0
  17. package/dist/artifacts/publish-tool.d.ts +86 -0
  18. package/dist/artifacts/publish-tool.d.ts.map +1 -0
  19. package/dist/ask-parent-tool.d.ts +36 -0
  20. package/dist/ask-parent-tool.d.ts.map +1 -0
  21. package/dist/attachments.d.ts +109 -0
  22. package/dist/attachments.d.ts.map +1 -0
  23. package/dist/child-files.d.ts +91 -0
  24. package/dist/child-files.d.ts.map +1 -0
  25. package/dist/child-questions.d.ts +24 -0
  26. package/dist/child-questions.d.ts.map +1 -0
  27. package/dist/computer-runner.d.ts +74 -0
  28. package/dist/computer-runner.d.ts.map +1 -0
  29. package/dist/context-budget.d.ts +34 -0
  30. package/dist/context-budget.d.ts.map +1 -0
  31. package/dist/context-compaction.d.ts +148 -0
  32. package/dist/context-compaction.d.ts.map +1 -0
  33. package/dist/context-guard.d.ts +69 -0
  34. package/dist/context-guard.d.ts.map +1 -0
  35. package/dist/conversation-memory.d.ts +21 -0
  36. package/dist/conversation-memory.d.ts.map +1 -0
  37. package/dist/conversation-messages.d.ts +33 -0
  38. package/dist/conversation-messages.d.ts.map +1 -0
  39. package/dist/errors.d.ts +46 -0
  40. package/dist/errors.d.ts.map +1 -0
  41. package/dist/execution-plan.d.ts +38 -0
  42. package/dist/execution-plan.d.ts.map +1 -0
  43. package/dist/failure.d.ts +14 -0
  44. package/dist/failure.d.ts.map +1 -0
  45. package/dist/family-mailbox.d.ts +60 -0
  46. package/dist/family-mailbox.d.ts.map +1 -0
  47. package/dist/file-ref.d.ts +6 -0
  48. package/dist/file-ref.d.ts.map +1 -0
  49. package/dist/image-attachments.d.ts +34 -0
  50. package/dist/image-attachments.d.ts.map +1 -0
  51. package/dist/index.d.ts +35 -0
  52. package/dist/index.d.ts.map +1 -0
  53. package/dist/index.js +14387 -0
  54. package/dist/index.js.map +72 -0
  55. package/dist/inline-images.d.ts +28 -0
  56. package/dist/inline-images.d.ts.map +1 -0
  57. package/dist/message-authors.d.ts +34 -0
  58. package/dist/message-authors.d.ts.map +1 -0
  59. package/dist/message-times.d.ts +9 -0
  60. package/dist/message-times.d.ts.map +1 -0
  61. package/dist/model-adapters.d.ts +59 -0
  62. package/dist/model-adapters.d.ts.map +1 -0
  63. package/dist/model-lines.d.ts +16 -0
  64. package/dist/model-lines.d.ts.map +1 -0
  65. package/dist/prompt-cache.d.ts +28 -0
  66. package/dist/prompt-cache.d.ts.map +1 -0
  67. package/dist/requester.d.ts +15 -0
  68. package/dist/requester.d.ts.map +1 -0
  69. package/dist/run-agent-turn.d.ts +93 -0
  70. package/dist/run-agent-turn.d.ts.map +1 -0
  71. package/dist/run-child.d.ts +77 -0
  72. package/dist/run-child.d.ts.map +1 -0
  73. package/dist/run-conversation-turn.d.ts +42 -0
  74. package/dist/run-conversation-turn.d.ts.map +1 -0
  75. package/dist/run-credential.d.ts +22 -0
  76. package/dist/run-credential.d.ts.map +1 -0
  77. package/dist/run-environment.d.ts +145 -0
  78. package/dist/run-environment.d.ts.map +1 -0
  79. package/dist/run-fate.d.ts +15 -0
  80. package/dist/run-fate.d.ts.map +1 -0
  81. package/dist/run-files-lock.d.ts +16 -0
  82. package/dist/run-files-lock.d.ts.map +1 -0
  83. package/dist/run-sandbox.d.ts +60 -0
  84. package/dist/run-sandbox.d.ts.map +1 -0
  85. package/dist/run-subagent.d.ts +65 -0
  86. package/dist/run-subagent.d.ts.map +1 -0
  87. package/dist/run-workflow-agent-node.d.ts +46 -0
  88. package/dist/run-workflow-agent-node.d.ts.map +1 -0
  89. package/dist/sandbox-api-context.d.ts +34 -0
  90. package/dist/sandbox-api-context.d.ts.map +1 -0
  91. package/dist/sandbox-file-registry.d.ts +9 -0
  92. package/dist/sandbox-file-registry.d.ts.map +1 -0
  93. package/dist/sandbox-files.d.ts +71 -0
  94. package/dist/sandbox-files.d.ts.map +1 -0
  95. package/dist/sandbox-handle.d.ts +15 -0
  96. package/dist/sandbox-handle.d.ts.map +1 -0
  97. package/dist/sandbox-readiness.d.ts +8 -0
  98. package/dist/sandbox-readiness.d.ts.map +1 -0
  99. package/dist/skills/built-in.d.ts +15 -0
  100. package/dist/skills/built-in.d.ts.map +1 -0
  101. package/dist/subagent-context.d.ts +50 -0
  102. package/dist/subagent-context.d.ts.map +1 -0
  103. package/dist/subagent-continuation.d.ts +33 -0
  104. package/dist/subagent-continuation.d.ts.map +1 -0
  105. package/dist/subagent-inbox.d.ts +53 -0
  106. package/dist/subagent-inbox.d.ts.map +1 -0
  107. package/dist/subagent-tools.d.ts +293 -0
  108. package/dist/subagent-tools.d.ts.map +1 -0
  109. package/dist/thread-context.d.ts +17 -0
  110. package/dist/thread-context.d.ts.map +1 -0
  111. package/dist/tools/attachments.d.ts +11 -0
  112. package/dist/tools/attachments.d.ts.map +1 -0
  113. package/dist/tools/bash.d.ts +68 -0
  114. package/dist/tools/bash.d.ts.map +1 -0
  115. package/dist/tools/model-spec.d.ts +9 -0
  116. package/dist/tools/model-spec.d.ts.map +1 -0
  117. package/dist/tools/read.d.ts +43 -0
  118. package/dist/tools/read.d.ts.map +1 -0
  119. package/dist/tools/result-media.d.ts +65 -0
  120. package/dist/tools/result-media.d.ts.map +1 -0
  121. package/dist/tools/result-output.d.ts +18 -0
  122. package/dist/tools/result-output.d.ts.map +1 -0
  123. package/dist/tools/tool-files.d.ts +59 -0
  124. package/dist/tools/tool-files.d.ts.map +1 -0
  125. package/dist/tools/tool-outputs.d.ts +63 -0
  126. package/dist/tools/tool-outputs.d.ts.map +1 -0
  127. package/dist/tools/view-file.d.ts +40 -0
  128. package/dist/tools/view-file.d.ts.map +1 -0
  129. package/dist/tsconfig.json +5 -0
  130. package/dist/turn-runtime.d.ts +19 -0
  131. package/dist/turn-runtime.d.ts.map +1 -0
  132. package/dist/types.d.ts +83 -0
  133. package/dist/types.d.ts.map +1 -0
  134. package/dist/workflow-input-attachments.d.ts +18 -0
  135. package/dist/workflow-input-attachments.d.ts.map +1 -0
  136. package/package.json +59 -4
  137. package/src/abort.ts +40 -0
  138. package/src/agent-prompt.ts +860 -0
  139. package/src/agent-runtime/errors.ts +44 -0
  140. package/src/agent-runtime/preflight.ts +314 -0
  141. package/src/agent-runtime/profile.ts +23 -0
  142. package/src/artifacts/layout-check/check.cjs +478 -0
  143. package/src/artifacts/layout-check/layout-check.ts +460 -0
  144. package/src/artifacts/materialize.ts +380 -0
  145. package/src/artifacts/publish-tool.ts +700 -0
  146. package/src/ask-parent-tool.ts +100 -0
  147. package/src/attachments.ts +1041 -0
  148. package/src/child-files.ts +535 -0
  149. package/src/child-questions.ts +42 -0
  150. package/src/computer-runner.ts +336 -0
  151. package/src/context-budget.ts +177 -0
  152. package/src/context-compaction.ts +1349 -0
  153. package/src/context-guard.ts +826 -0
  154. package/src/conversation-memory.ts +45 -0
  155. package/src/conversation-messages.ts +86 -0
  156. package/src/errors.ts +85 -0
  157. package/src/execution-plan.ts +70 -0
  158. package/src/failure.ts +82 -0
  159. package/src/family-mailbox.ts +189 -0
  160. package/src/file-ref.ts +18 -0
  161. package/src/image-attachments.ts +322 -0
  162. package/src/index.ts +54 -0
  163. package/src/inline-images.ts +69 -0
  164. package/src/message-authors.ts +77 -0
  165. package/src/message-times.ts +24 -0
  166. package/src/model-adapters.ts +273 -0
  167. package/src/model-lines.ts +28 -0
  168. package/src/prompt-cache.ts +40 -0
  169. package/src/requester.ts +21 -0
  170. package/src/run-agent-turn.ts +509 -0
  171. package/src/run-child.ts +344 -0
  172. package/src/run-conversation-turn.ts +272 -0
  173. package/src/run-credential.ts +55 -0
  174. package/src/run-environment.ts +1216 -0
  175. package/src/run-fate.ts +51 -0
  176. package/src/run-files-lock.ts +118 -0
  177. package/src/run-sandbox.ts +63 -0
  178. package/src/run-subagent.ts +433 -0
  179. package/src/run-workflow-agent-node.ts +344 -0
  180. package/src/sandbox-api-context.ts +237 -0
  181. package/src/sandbox-file-registry.ts +26 -0
  182. package/src/sandbox-files.ts +326 -0
  183. package/src/sandbox-handle.ts +19 -0
  184. package/src/sandbox-readiness.ts +22 -0
  185. package/src/skills/built-in.ts +183 -0
  186. package/src/skills/sixb-artifacts/SKILL.md +95 -0
  187. package/src/skills/sixb-artifacts/canvas.md +104 -0
  188. package/src/skills/sixb-artifacts/document.md +63 -0
  189. package/src/skills/sixb-artifacts/examples/canvas/onboarding-flow/boards/first-job.html +42 -0
  190. package/src/skills/sixb-artifacts/examples/canvas/onboarding-flow/boards/region.html +29 -0
  191. package/src/skills/sixb-artifacts/examples/canvas/onboarding-flow/boards/shared.css +35 -0
  192. package/src/skills/sixb-artifacts/examples/canvas/onboarding-flow/boards/welcome.html +31 -0
  193. package/src/skills/sixb-artifacts/examples/canvas/onboarding-flow/canvas.json +107 -0
  194. package/src/skills/sixb-artifacts/examples/canvas/retro/canvas.json +158 -0
  195. package/src/skills/sixb-artifacts/examples/document/incident-report/document.md +35 -0
  196. package/src/skills/sixb-artifacts/examples/document/operations-review/document.md +41 -0
  197. package/src/skills/sixb-artifacts/examples/document/operations-review/figures/restorations.svg +1 -0
  198. package/src/skills/sixb-artifacts/examples/page/collections-desk/index.html +169 -0
  199. package/src/skills/sixb-artifacts/examples/page/collections-desk/queries/overdue.json +13 -0
  200. package/src/skills/sixb-artifacts/examples/page/dispatch-board/data/dispatch.json +234 -0
  201. package/src/skills/sixb-artifacts/examples/page/dispatch-board/index.html +245 -0
  202. package/src/skills/sixb-artifacts/examples/page/fleet-planner/data/plan.json +53 -0
  203. package/src/skills/sixb-artifacts/examples/page/fleet-planner/index.html +462 -0
  204. package/src/skills/sixb-artifacts/examples/page/service-explorer/data/service.json +276 -0
  205. package/src/skills/sixb-artifacts/examples/page/service-explorer/index.html +549 -0
  206. package/src/skills/sixb-artifacts/examples/sheet/overdue-invoices/queries/overdue.json +13 -0
  207. package/src/skills/sixb-artifacts/examples/sheet/overdue-invoices/workbook.json +30 -0
  208. package/src/skills/sixb-artifacts/examples/sheet/parts-inventory/sheets/Inventory.csv +13 -0
  209. package/src/skills/sixb-artifacts/examples/sheet/parts-inventory/sheets/Spend.csv +6 -0
  210. package/src/skills/sixb-artifacts/examples/sheet/parts-inventory/workbook.json +47 -0
  211. package/src/skills/sixb-artifacts/examples/sheet/service-metrics/sheets/Quarters.csv +5 -0
  212. package/src/skills/sixb-artifacts/examples/sheet/service-metrics/sheets/Regions.csv +5 -0
  213. package/src/skills/sixb-artifacts/examples/sheet/service-metrics/workbook.json +78 -0
  214. package/src/skills/sixb-artifacts/examples/slides/launch-plan/index.html +63 -0
  215. package/src/skills/sixb-artifacts/examples/slides/q3-review/index.html +133 -0
  216. package/src/skills/sixb-artifacts/file.md +17 -0
  217. package/src/skills/sixb-artifacts/page.md +215 -0
  218. package/src/skills/sixb-artifacts/queries.md +66 -0
  219. package/src/skills/sixb-artifacts/references.md +68 -0
  220. package/src/skills/sixb-artifacts/sheet.md +223 -0
  221. package/src/skills/sixb-artifacts/slides.md +137 -0
  222. package/src/skills/sixb-memory/SKILL.md +68 -0
  223. package/src/skills/text-assets.d.ts +25 -0
  224. package/src/subagent-context.ts +182 -0
  225. package/src/subagent-continuation.ts +56 -0
  226. package/src/subagent-inbox.ts +101 -0
  227. package/src/subagent-tools.ts +1671 -0
  228. package/src/thread-context.ts +31 -0
  229. package/src/tools/attachments.ts +28 -0
  230. package/src/tools/bash.ts +233 -0
  231. package/src/tools/model-spec.ts +27 -0
  232. package/src/tools/read.ts +262 -0
  233. package/src/tools/result-media.ts +351 -0
  234. package/src/tools/result-output.ts +46 -0
  235. package/src/tools/tool-files.ts +204 -0
  236. package/src/tools/tool-outputs.ts +464 -0
  237. package/src/tools/view-file.ts +155 -0
  238. package/src/turn-runtime.ts +33 -0
  239. package/src/types.ts +91 -0
  240. package/src/workflow-input-attachments.ts +140 -0
package/src/types.ts ADDED
@@ -0,0 +1,91 @@
1
+ import type { AuthorizablePrincipal } from "@sixb/core"
2
+ import type {
3
+ AgentRunFileStore,
4
+ AgentSystemPromptSize,
5
+ AgentToolCallFile,
6
+ RunModelLoopInput,
7
+ } from "@sixb/core/internal/agents"
8
+ import type { ModelTool } from "@sixb/core/models"
9
+ import type { PreparedAgentAttachmentContext } from "./attachments"
10
+ import type { AgentRequester } from "./requester"
11
+ import type { BeforeFinalizeOptions } from "./run-sandbox"
12
+ import type { AgentSandboxHandle } from "./sandbox-handle"
13
+ import type { AgentFileBudget } from "./tools/tool-files"
14
+
15
+ /**
16
+ * What every run of a project shares, whichever runner runs it: its settings. A run reaches the
17
+ * project through its session, never through storage, the project's SDK or its definitions.
18
+ */
19
+ export interface AgentRunSettings {
20
+ /** The project id. */
21
+ readonly id: string
22
+ /** The Sixb API origin the run's commands call, through their environment's credentialed address. */
23
+ readonly apiBaseUrl: string
24
+ /** How many children one conversation turn may have queued or running at once. */
25
+ readonly subagentConcurrencyPerRun: number
26
+ /** Whether a child keeps its workspace between runs: its environment can be resumed. */
27
+ readonly keepsChildWorkspaces: boolean
28
+ /** Project locale and time zone, for runs that do not carry the requester's own. */
29
+ readonly locale: string
30
+ readonly timeZone: string
31
+ }
32
+
33
+ /** A run's settings, with who it works for and where its files are stored. */
34
+ export interface AgentExecutionContext extends AgentRunSettings {
35
+ readonly authorPrincipal?: AuthorizablePrincipal
36
+ /**
37
+ * Who the run works for; absent when no principal started its chain of executions, or in a
38
+ * shared thread, where each user message names its author instead.
39
+ */
40
+ readonly requester?: AgentRequester
41
+ readonly blobStorage: AgentRunFileStore
42
+ }
43
+
44
+ export interface AgentTurnContext {
45
+ /** Environment-owned failure (for example lost source credentials), not user cancellation. */
46
+ readonly environmentFailureSignal?: AbortSignal
47
+ /** Persistent environments must confirm cleanup/save before any terminal write. */
48
+ readonly beforeFinalize?: (options?: BeforeFinalizeOptions) => Promise<void>
49
+ readonly id: string
50
+ readonly authorPrincipal?: AuthorizablePrincipal
51
+ readonly blobStorage: AgentRunFileStore
52
+ /** The Sixb API the run's commands call, when this turn was created through a run environment. */
53
+ readonly apiBaseUrl?: string
54
+ readonly tools: readonly ModelTool[]
55
+ /** The stored file of a call whose output was too large for the context, for its message. */
56
+ readonly outputFileOf?: (toolCallId: string) => AgentToolCallFile | undefined
57
+ readonly prepareStep?: RunModelLoopInput["prepareStep"]
58
+ /** Complete worker-owned system prompt, derived from the mode and provisioned runtime. */
59
+ readonly systemPrompt: string
60
+ /** The same prompt with each section's bytes, for each step to record what its request sent. */
61
+ readonly systemPromptSize?: AgentSystemPromptSize
62
+ /** Where the run's context files are, as the agent names them: its reads of them are named. */
63
+ readonly contextDirectory?: string
64
+ /** Time zone and locale of runs that captured none, used to date their user messages. */
65
+ readonly projectTimeZone: string
66
+ readonly projectLocale: string
67
+ /** Prepared by the run's environment, which materializes the files in its context directory. */
68
+ readonly attachmentContext?: PreparedAgentAttachmentContext
69
+ /**
70
+ * The concurrently provisioning sandbox, exposed so the turn can fail if it rejects. Resolved
71
+ * value is irrelevant here (the sandbox tools consume the handle); only its rejection matters.
72
+ */
73
+ readonly sandboxReady?: Promise<AgentSandboxHandle>
74
+ }
75
+
76
+ /**
77
+ * A run's sandbox and its stored files, as its framework tools use them: a parent hands files to
78
+ * its children and gets theirs back, a child returns what it wrote.
79
+ */
80
+ export interface AgentRunFiles {
81
+ sandbox(): Promise<AgentSandboxHandle>
82
+ readonly blobStorage: AgentRunFileStore
83
+ /** Shared with every other file the run stores. */
84
+ readonly budget: AgentFileBudget
85
+ /**
86
+ * Install the latest version of each artifact these runs published in the thread in the run's
87
+ * `$SIXB_ARTIFACTS`; nothing for a run that installs no artifacts. Throws, naming them, when
88
+ * some could not be installed.
89
+ */
90
+ installArtifactsFrom(runIds: readonly string[], signal: AbortSignal): Promise<void>
91
+ }
@@ -0,0 +1,140 @@
1
+ import { posix } from "node:path"
2
+ import type { FileRef } from "@sixb/core"
3
+ import { isFileRef } from "@sixb/core"
4
+ import type { AgentRunFileStore } from "@sixb/core/internal/agents"
5
+ import type { WorkflowIOSnapshot } from "@sixb/core/internal/workflows"
6
+ import { waitForAbort } from "./abort"
7
+ import type { PreparedAgentAttachmentContext } from "./attachments"
8
+
9
+ const MAX_WORKFLOW_ATTACHMENT_BYTES = 25 * 1024 * 1024
10
+ const MAX_WORKFLOW_ATTACHMENTS_TOTAL_BYTES = 100 * 1024 * 1024
11
+
12
+ /** Materialize nested FileRefs from a workflow input into the headless task sandbox. */
13
+ export async function prepareWorkflowInputAttachments(input: {
14
+ readonly input: WorkflowIOSnapshot
15
+ readonly blobStorage: AgentRunFileStore
16
+ /** Where the run's files go: the input's files are materialized in it. */
17
+ readonly contextDirectory: string
18
+ readonly signal?: AbortSignal
19
+ }): Promise<PreparedAgentAttachmentContext> {
20
+ const refs = workflowInputFileRefs(input.input)
21
+ const sandboxFiles: PreparedAgentAttachmentContext["sandboxFiles"] extends readonly (infer T)[]
22
+ ? T[]
23
+ : never = []
24
+ const manifest: Array<Record<string, unknown>> = []
25
+ let totalBytes = 0
26
+
27
+ for (const [index, item] of refs.entries()) {
28
+ input.signal?.throwIfAborted()
29
+ const stat = await waitForAbort(input.blobStorage.stat(item.fileRef.blobId), input.signal)
30
+ if (
31
+ !stat ||
32
+ stat.digest !== item.fileRef.digest ||
33
+ stat.sizeBytes !== item.fileRef.sizeBytes ||
34
+ stat.sizeBytes > MAX_WORKFLOW_ATTACHMENT_BYTES ||
35
+ totalBytes + stat.sizeBytes > MAX_WORKFLOW_ATTACHMENTS_TOTAL_BYTES
36
+ ) {
37
+ manifest.push({ key: item.path, path: item.path, fileRef: item.fileRef, materialized: false })
38
+ continue
39
+ }
40
+
41
+ const bytes = await readAllBytes(
42
+ await waitForAbort(input.blobStorage.open(item.fileRef.blobId), input.signal),
43
+ stat.sizeBytes,
44
+ input.signal
45
+ )
46
+ const fileName = safeFileName(item.fileRef.fileName ?? `${item.fileRef.blobId}.bin`)
47
+ const sandboxPath = posix.join(
48
+ input.contextDirectory,
49
+ "attachments/workflow-input",
50
+ `${index}-${fileName}`
51
+ )
52
+ sandboxFiles.push({ key: item.path, path: sandboxPath, bytes, fileRef: item.fileRef })
53
+ totalBytes += bytes.byteLength
54
+ manifest.push({
55
+ key: item.path,
56
+ path: item.path,
57
+ fileRef: item.fileRef,
58
+ materialized: true,
59
+ sandboxPath,
60
+ })
61
+ }
62
+
63
+ return {
64
+ entries: [],
65
+ promptTextByPartKey: new Map(),
66
+ modelFileDataByPartKey: new Map(),
67
+ sandboxFiles,
68
+ manifestJson: JSON.stringify({ attachments: manifest }, null, 2),
69
+ }
70
+ }
71
+
72
+ /** Every FileRef in a workflow input, keyed by its path such as `input.documents[0]`. */
73
+ export function workflowInputFileRefs(
74
+ input: WorkflowIOSnapshot
75
+ ): readonly { readonly path: string; readonly fileRef: FileRef }[] {
76
+ const refs: Array<{ readonly path: string; readonly fileRef: FileRef }> = []
77
+ collectFileRefs(input, "input", refs)
78
+ return refs
79
+ }
80
+
81
+ function collectFileRefs(
82
+ value: unknown,
83
+ path: string,
84
+ output: Array<{ readonly path: string; readonly fileRef: FileRef }>
85
+ ): void {
86
+ if (isFileRef(value)) {
87
+ output.push({ path, fileRef: value })
88
+ return
89
+ }
90
+ if (Array.isArray(value)) {
91
+ value.forEach((entry, index) => {
92
+ collectFileRefs(entry, `${path}[${index}]`, output)
93
+ })
94
+ return
95
+ }
96
+ if (typeof value === "object" && value !== null) {
97
+ for (const [key, entry] of Object.entries(value)) {
98
+ collectFileRefs(entry, `${path}.${key}`, output)
99
+ }
100
+ }
101
+ }
102
+
103
+ async function readAllBytes(
104
+ stream: ReadableStream<Uint8Array>,
105
+ expectedSize: number,
106
+ signal?: AbortSignal
107
+ ): Promise<Uint8Array> {
108
+ const bytes = new Uint8Array(expectedSize)
109
+ const reader = stream.getReader()
110
+ let offset = 0
111
+ const cancelOnAbort = () => {
112
+ void reader.cancel(signal?.reason)
113
+ }
114
+ signal?.addEventListener("abort", cancelOnAbort, { once: true })
115
+ try {
116
+ for (;;) {
117
+ const { done, value } = await reader.read()
118
+ signal?.throwIfAborted()
119
+ if (done) break
120
+ if (offset + value.byteLength > expectedSize) {
121
+ throw new Error("Blob size changed while reading.")
122
+ }
123
+ bytes.set(value, offset)
124
+ offset += value.byteLength
125
+ }
126
+ } finally {
127
+ signal?.removeEventListener("abort", cancelOnAbort)
128
+ reader.releaseLock()
129
+ }
130
+ if (offset !== expectedSize) throw new Error("Blob size changed while reading.")
131
+ return bytes
132
+ }
133
+
134
+ function safeFileName(value: string): string {
135
+ const name = value
136
+ .split(/[\\/]/)
137
+ .at(-1)
138
+ ?.replace(/[^A-Za-z0-9._-]/g, "_")
139
+ return name || "attachment.bin"
140
+ }