@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.
- package/LICENSE +21 -0
- package/README.md +31 -2
- package/dist/abort.d.ts +6 -0
- package/dist/abort.d.ts.map +1 -0
- package/dist/agent-prompt.d.ts +126 -0
- package/dist/agent-prompt.d.ts.map +1 -0
- package/dist/agent-runtime/errors.d.ts +13 -0
- package/dist/agent-runtime/errors.d.ts.map +1 -0
- package/dist/agent-runtime/preflight.d.ts +18 -0
- package/dist/agent-runtime/preflight.d.ts.map +1 -0
- package/dist/agent-runtime/profile.d.ts +14 -0
- package/dist/agent-runtime/profile.d.ts.map +1 -0
- package/dist/artifacts/layout-check/layout-check.d.ts +87 -0
- package/dist/artifacts/layout-check/layout-check.d.ts.map +1 -0
- package/dist/artifacts/materialize.d.ts +115 -0
- package/dist/artifacts/materialize.d.ts.map +1 -0
- package/dist/artifacts/publish-tool.d.ts +86 -0
- package/dist/artifacts/publish-tool.d.ts.map +1 -0
- package/dist/ask-parent-tool.d.ts +36 -0
- package/dist/ask-parent-tool.d.ts.map +1 -0
- package/dist/attachments.d.ts +109 -0
- package/dist/attachments.d.ts.map +1 -0
- package/dist/child-files.d.ts +91 -0
- package/dist/child-files.d.ts.map +1 -0
- package/dist/child-questions.d.ts +24 -0
- package/dist/child-questions.d.ts.map +1 -0
- package/dist/computer-runner.d.ts +74 -0
- package/dist/computer-runner.d.ts.map +1 -0
- package/dist/context-budget.d.ts +34 -0
- package/dist/context-budget.d.ts.map +1 -0
- package/dist/context-compaction.d.ts +148 -0
- package/dist/context-compaction.d.ts.map +1 -0
- package/dist/context-guard.d.ts +69 -0
- package/dist/context-guard.d.ts.map +1 -0
- package/dist/conversation-memory.d.ts +21 -0
- package/dist/conversation-memory.d.ts.map +1 -0
- package/dist/conversation-messages.d.ts +33 -0
- package/dist/conversation-messages.d.ts.map +1 -0
- package/dist/errors.d.ts +46 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/execution-plan.d.ts +38 -0
- package/dist/execution-plan.d.ts.map +1 -0
- package/dist/failure.d.ts +14 -0
- package/dist/failure.d.ts.map +1 -0
- package/dist/family-mailbox.d.ts +60 -0
- package/dist/family-mailbox.d.ts.map +1 -0
- package/dist/file-ref.d.ts +6 -0
- package/dist/file-ref.d.ts.map +1 -0
- package/dist/image-attachments.d.ts +34 -0
- package/dist/image-attachments.d.ts.map +1 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +14387 -0
- package/dist/index.js.map +72 -0
- package/dist/inline-images.d.ts +28 -0
- package/dist/inline-images.d.ts.map +1 -0
- package/dist/message-authors.d.ts +34 -0
- package/dist/message-authors.d.ts.map +1 -0
- package/dist/message-times.d.ts +9 -0
- package/dist/message-times.d.ts.map +1 -0
- package/dist/model-adapters.d.ts +59 -0
- package/dist/model-adapters.d.ts.map +1 -0
- package/dist/model-lines.d.ts +16 -0
- package/dist/model-lines.d.ts.map +1 -0
- package/dist/prompt-cache.d.ts +28 -0
- package/dist/prompt-cache.d.ts.map +1 -0
- package/dist/requester.d.ts +15 -0
- package/dist/requester.d.ts.map +1 -0
- package/dist/run-agent-turn.d.ts +93 -0
- package/dist/run-agent-turn.d.ts.map +1 -0
- package/dist/run-child.d.ts +77 -0
- package/dist/run-child.d.ts.map +1 -0
- package/dist/run-conversation-turn.d.ts +42 -0
- package/dist/run-conversation-turn.d.ts.map +1 -0
- package/dist/run-credential.d.ts +22 -0
- package/dist/run-credential.d.ts.map +1 -0
- package/dist/run-environment.d.ts +145 -0
- package/dist/run-environment.d.ts.map +1 -0
- package/dist/run-fate.d.ts +15 -0
- package/dist/run-fate.d.ts.map +1 -0
- package/dist/run-files-lock.d.ts +16 -0
- package/dist/run-files-lock.d.ts.map +1 -0
- package/dist/run-sandbox.d.ts +60 -0
- package/dist/run-sandbox.d.ts.map +1 -0
- package/dist/run-subagent.d.ts +65 -0
- package/dist/run-subagent.d.ts.map +1 -0
- package/dist/run-workflow-agent-node.d.ts +46 -0
- package/dist/run-workflow-agent-node.d.ts.map +1 -0
- package/dist/sandbox-api-context.d.ts +34 -0
- package/dist/sandbox-api-context.d.ts.map +1 -0
- package/dist/sandbox-file-registry.d.ts +9 -0
- package/dist/sandbox-file-registry.d.ts.map +1 -0
- package/dist/sandbox-files.d.ts +71 -0
- package/dist/sandbox-files.d.ts.map +1 -0
- package/dist/sandbox-handle.d.ts +15 -0
- package/dist/sandbox-handle.d.ts.map +1 -0
- package/dist/sandbox-readiness.d.ts +8 -0
- package/dist/sandbox-readiness.d.ts.map +1 -0
- package/dist/skills/built-in.d.ts +15 -0
- package/dist/skills/built-in.d.ts.map +1 -0
- package/dist/subagent-context.d.ts +50 -0
- package/dist/subagent-context.d.ts.map +1 -0
- package/dist/subagent-continuation.d.ts +33 -0
- package/dist/subagent-continuation.d.ts.map +1 -0
- package/dist/subagent-inbox.d.ts +53 -0
- package/dist/subagent-inbox.d.ts.map +1 -0
- package/dist/subagent-tools.d.ts +293 -0
- package/dist/subagent-tools.d.ts.map +1 -0
- package/dist/thread-context.d.ts +17 -0
- package/dist/thread-context.d.ts.map +1 -0
- package/dist/tools/attachments.d.ts +11 -0
- package/dist/tools/attachments.d.ts.map +1 -0
- package/dist/tools/bash.d.ts +68 -0
- package/dist/tools/bash.d.ts.map +1 -0
- package/dist/tools/model-spec.d.ts +9 -0
- package/dist/tools/model-spec.d.ts.map +1 -0
- package/dist/tools/read.d.ts +43 -0
- package/dist/tools/read.d.ts.map +1 -0
- package/dist/tools/result-media.d.ts +65 -0
- package/dist/tools/result-media.d.ts.map +1 -0
- package/dist/tools/result-output.d.ts +18 -0
- package/dist/tools/result-output.d.ts.map +1 -0
- package/dist/tools/tool-files.d.ts +59 -0
- package/dist/tools/tool-files.d.ts.map +1 -0
- package/dist/tools/tool-outputs.d.ts +63 -0
- package/dist/tools/tool-outputs.d.ts.map +1 -0
- package/dist/tools/view-file.d.ts +40 -0
- package/dist/tools/view-file.d.ts.map +1 -0
- package/dist/tsconfig.json +5 -0
- package/dist/turn-runtime.d.ts +19 -0
- package/dist/turn-runtime.d.ts.map +1 -0
- package/dist/types.d.ts +83 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/workflow-input-attachments.d.ts +18 -0
- package/dist/workflow-input-attachments.d.ts.map +1 -0
- package/package.json +59 -4
- package/src/abort.ts +40 -0
- package/src/agent-prompt.ts +860 -0
- package/src/agent-runtime/errors.ts +44 -0
- package/src/agent-runtime/preflight.ts +314 -0
- package/src/agent-runtime/profile.ts +23 -0
- package/src/artifacts/layout-check/check.cjs +478 -0
- package/src/artifacts/layout-check/layout-check.ts +460 -0
- package/src/artifacts/materialize.ts +380 -0
- package/src/artifacts/publish-tool.ts +700 -0
- package/src/ask-parent-tool.ts +100 -0
- package/src/attachments.ts +1041 -0
- package/src/child-files.ts +535 -0
- package/src/child-questions.ts +42 -0
- package/src/computer-runner.ts +336 -0
- package/src/context-budget.ts +177 -0
- package/src/context-compaction.ts +1349 -0
- package/src/context-guard.ts +826 -0
- package/src/conversation-memory.ts +45 -0
- package/src/conversation-messages.ts +86 -0
- package/src/errors.ts +85 -0
- package/src/execution-plan.ts +70 -0
- package/src/failure.ts +82 -0
- package/src/family-mailbox.ts +189 -0
- package/src/file-ref.ts +18 -0
- package/src/image-attachments.ts +322 -0
- package/src/index.ts +54 -0
- package/src/inline-images.ts +69 -0
- package/src/message-authors.ts +77 -0
- package/src/message-times.ts +24 -0
- package/src/model-adapters.ts +273 -0
- package/src/model-lines.ts +28 -0
- package/src/prompt-cache.ts +40 -0
- package/src/requester.ts +21 -0
- package/src/run-agent-turn.ts +509 -0
- package/src/run-child.ts +344 -0
- package/src/run-conversation-turn.ts +272 -0
- package/src/run-credential.ts +55 -0
- package/src/run-environment.ts +1216 -0
- package/src/run-fate.ts +51 -0
- package/src/run-files-lock.ts +118 -0
- package/src/run-sandbox.ts +63 -0
- package/src/run-subagent.ts +433 -0
- package/src/run-workflow-agent-node.ts +344 -0
- package/src/sandbox-api-context.ts +237 -0
- package/src/sandbox-file-registry.ts +26 -0
- package/src/sandbox-files.ts +326 -0
- package/src/sandbox-handle.ts +19 -0
- package/src/sandbox-readiness.ts +22 -0
- package/src/skills/built-in.ts +183 -0
- package/src/skills/sixb-artifacts/SKILL.md +95 -0
- package/src/skills/sixb-artifacts/canvas.md +104 -0
- package/src/skills/sixb-artifacts/document.md +63 -0
- package/src/skills/sixb-artifacts/examples/canvas/onboarding-flow/boards/first-job.html +42 -0
- package/src/skills/sixb-artifacts/examples/canvas/onboarding-flow/boards/region.html +29 -0
- package/src/skills/sixb-artifacts/examples/canvas/onboarding-flow/boards/shared.css +35 -0
- package/src/skills/sixb-artifacts/examples/canvas/onboarding-flow/boards/welcome.html +31 -0
- package/src/skills/sixb-artifacts/examples/canvas/onboarding-flow/canvas.json +107 -0
- package/src/skills/sixb-artifacts/examples/canvas/retro/canvas.json +158 -0
- package/src/skills/sixb-artifacts/examples/document/incident-report/document.md +35 -0
- package/src/skills/sixb-artifacts/examples/document/operations-review/document.md +41 -0
- package/src/skills/sixb-artifacts/examples/document/operations-review/figures/restorations.svg +1 -0
- package/src/skills/sixb-artifacts/examples/page/collections-desk/index.html +169 -0
- package/src/skills/sixb-artifacts/examples/page/collections-desk/queries/overdue.json +13 -0
- package/src/skills/sixb-artifacts/examples/page/dispatch-board/data/dispatch.json +234 -0
- package/src/skills/sixb-artifacts/examples/page/dispatch-board/index.html +245 -0
- package/src/skills/sixb-artifacts/examples/page/fleet-planner/data/plan.json +53 -0
- package/src/skills/sixb-artifacts/examples/page/fleet-planner/index.html +462 -0
- package/src/skills/sixb-artifacts/examples/page/service-explorer/data/service.json +276 -0
- package/src/skills/sixb-artifacts/examples/page/service-explorer/index.html +549 -0
- package/src/skills/sixb-artifacts/examples/sheet/overdue-invoices/queries/overdue.json +13 -0
- package/src/skills/sixb-artifacts/examples/sheet/overdue-invoices/workbook.json +30 -0
- package/src/skills/sixb-artifacts/examples/sheet/parts-inventory/sheets/Inventory.csv +13 -0
- package/src/skills/sixb-artifacts/examples/sheet/parts-inventory/sheets/Spend.csv +6 -0
- package/src/skills/sixb-artifacts/examples/sheet/parts-inventory/workbook.json +47 -0
- package/src/skills/sixb-artifacts/examples/sheet/service-metrics/sheets/Quarters.csv +5 -0
- package/src/skills/sixb-artifacts/examples/sheet/service-metrics/sheets/Regions.csv +5 -0
- package/src/skills/sixb-artifacts/examples/sheet/service-metrics/workbook.json +78 -0
- package/src/skills/sixb-artifacts/examples/slides/launch-plan/index.html +63 -0
- package/src/skills/sixb-artifacts/examples/slides/q3-review/index.html +133 -0
- package/src/skills/sixb-artifacts/file.md +17 -0
- package/src/skills/sixb-artifacts/page.md +215 -0
- package/src/skills/sixb-artifacts/queries.md +66 -0
- package/src/skills/sixb-artifacts/references.md +68 -0
- package/src/skills/sixb-artifacts/sheet.md +223 -0
- package/src/skills/sixb-artifacts/slides.md +137 -0
- package/src/skills/sixb-memory/SKILL.md +68 -0
- package/src/skills/text-assets.d.ts +25 -0
- package/src/subagent-context.ts +182 -0
- package/src/subagent-continuation.ts +56 -0
- package/src/subagent-inbox.ts +101 -0
- package/src/subagent-tools.ts +1671 -0
- package/src/thread-context.ts +31 -0
- package/src/tools/attachments.ts +28 -0
- package/src/tools/bash.ts +233 -0
- package/src/tools/model-spec.ts +27 -0
- package/src/tools/read.ts +262 -0
- package/src/tools/result-media.ts +351 -0
- package/src/tools/result-output.ts +46 -0
- package/src/tools/tool-files.ts +204 -0
- package/src/tools/tool-outputs.ts +464 -0
- package/src/tools/view-file.ts +155 -0
- package/src/turn-runtime.ts +33 -0
- package/src/types.ts +91 -0
- 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
|
+
}
|