@sixb/agent 0.0.0-stage → 0.1.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 +120 -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 +68 -0
- package/dist/attachments.d.ts.map +1 -0
- package/dist/child-files.d.ts +96 -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 +65 -0
- package/dist/computer-runner.d.ts.map +1 -0
- package/dist/context-budget.d.ts +28 -0
- package/dist/context-budget.d.ts.map +1 -0
- package/dist/context-compaction.d.ts +48 -0
- package/dist/context-compaction.d.ts.map +1 -0
- package/dist/conversation-memory.d.ts +21 -0
- package/dist/conversation-memory.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 +34 -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 +12168 -0
- package/dist/index.js.map +65 -0
- package/dist/message-authors.d.ts +34 -0
- package/dist/message-authors.d.ts.map +1 -0
- package/dist/message-times.d.ts +8 -0
- package/dist/message-times.d.ts.map +1 -0
- package/dist/model-adapters.d.ts +55 -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/requester.d.ts +15 -0
- package/dist/requester.d.ts.map +1 -0
- package/dist/run-agent-turn.d.ts +63 -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 +115 -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-sandbox.d.ts +60 -0
- package/dist/run-sandbox.d.ts.map +1 -0
- package/dist/run-subagent.d.ts +51 -0
- package/dist/run-subagent.d.ts.map +1 -0
- package/dist/run-workflow-agent-node.d.ts +41 -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-continuation.d.ts +27 -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 +282 -0
- package/dist/subagent-tools.d.ts.map +1 -0
- package/dist/thread-context.d.ts +12 -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 +60 -0
- package/dist/tools/bash.d.ts.map +1 -0
- package/dist/tools/model-spec.d.ts +4 -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 +22 -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/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 +76 -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 +758 -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 +850 -0
- package/src/child-files.ts +540 -0
- package/src/child-questions.ts +42 -0
- package/src/computer-runner.ts +284 -0
- package/src/context-budget.ts +161 -0
- package/src/context-compaction.ts +529 -0
- package/src/conversation-memory.ts +45 -0
- package/src/errors.ts +85 -0
- package/src/execution-plan.ts +50 -0
- package/src/failure.ts +71 -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/message-authors.ts +77 -0
- package/src/message-times.ts +21 -0
- package/src/model-adapters.ts +227 -0
- package/src/model-lines.ts +28 -0
- package/src/requester.ts +21 -0
- package/src/run-agent-turn.ts +347 -0
- package/src/run-child.ts +319 -0
- package/src/run-conversation-turn.ts +257 -0
- package/src/run-credential.ts +55 -0
- package/src/run-environment.ts +769 -0
- package/src/run-fate.ts +51 -0
- package/src/run-sandbox.ts +63 -0
- package/src/run-subagent.ts +347 -0
- package/src/run-workflow-agent-node.ts +270 -0
- package/src/sandbox-api-context.ts +234 -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-continuation.ts +51 -0
- package/src/subagent-inbox.ts +101 -0
- package/src/subagent-tools.ts +1609 -0
- package/src/thread-context.ts +25 -0
- package/src/tools/attachments.ts +28 -0
- package/src/tools/bash.ts +217 -0
- package/src/tools/model-spec.ts +22 -0
- package/src/tools/read.ts +262 -0
- package/src/tools/result-media.ts +147 -0
- package/src/tools/result-output.ts +44 -0
- package/src/tools/tool-files.ts +204 -0
- package/src/tools/view-file.ts +155 -0
- package/src/turn-runtime.ts +33 -0
- package/src/types.ts +79 -0
- package/src/workflow-input-attachments.ts +140 -0
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { AgentReasoning, AgentSkillDefinition } from "@sixb/core"
|
|
2
|
+
import type { AgentRunPlan, AgentToolSpec, SandboxModel } from "@sixb/core/internal/agents"
|
|
3
|
+
import type { RenderedOntologyDocs } from "@sixb/core/internal/ontology"
|
|
4
|
+
import type { LanguageModel } from "@sixb/core/models"
|
|
5
|
+
import { withBuiltInSkills } from "./skills/built-in"
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Fully resolved inputs shared by the agent execution engines.
|
|
9
|
+
*
|
|
10
|
+
* This internal runtime value is neither a durable run nor a public API contract. Source-specific
|
|
11
|
+
* adapters resolve it before entering the shared execution path.
|
|
12
|
+
*/
|
|
13
|
+
export interface ResolvedAgentExecutionPlan {
|
|
14
|
+
readonly model: LanguageModel
|
|
15
|
+
readonly reasoning?: AgentReasoning
|
|
16
|
+
readonly instructions?: string
|
|
17
|
+
/** The project tools it offers, as the model is offered them; they run where the project is. */
|
|
18
|
+
readonly tools: readonly AgentToolSpec[]
|
|
19
|
+
/** Agent Skills installed in the sandbox and listed in the prompt. */
|
|
20
|
+
readonly skills: readonly AgentSkillDefinition[]
|
|
21
|
+
/** The requester's time zone and locale, when the run captured them. */
|
|
22
|
+
readonly timeZone?: string
|
|
23
|
+
readonly locale?: string
|
|
24
|
+
/** Models the sandbox CLI can call beside the agent's own, listed in the prompt. */
|
|
25
|
+
readonly sandboxModels?: readonly SandboxModel[]
|
|
26
|
+
/** Reference docs for the object types the run's requester can see, from their own view. */
|
|
27
|
+
readonly ontologyDocs: RenderedOntologyDocs
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* A turn's or a child's plan, with the session's model, which records each call itself, and the
|
|
32
|
+
* harness's skills added. Its project tools run through the session.
|
|
33
|
+
*/
|
|
34
|
+
export function resolveRunPlan(
|
|
35
|
+
plan: AgentRunPlan,
|
|
36
|
+
resolved: { readonly model: LanguageModel }
|
|
37
|
+
): ResolvedAgentExecutionPlan {
|
|
38
|
+
const { reasoning, instructions, timeZone, locale, sandboxModels, ontologyDocs, tools } = plan
|
|
39
|
+
return Object.freeze({
|
|
40
|
+
model: resolved.model,
|
|
41
|
+
ontologyDocs,
|
|
42
|
+
...(instructions === undefined ? {} : { instructions }),
|
|
43
|
+
tools,
|
|
44
|
+
skills: withBuiltInSkills(plan.skills),
|
|
45
|
+
...(reasoning === undefined ? {} : { reasoning }),
|
|
46
|
+
...(timeZone === undefined ? {} : { timeZone }),
|
|
47
|
+
...(locale === undefined ? {} : { locale }),
|
|
48
|
+
...(sandboxModels.length ? { sandboxModels } : {}),
|
|
49
|
+
})
|
|
50
|
+
}
|
package/src/failure.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { SixbFailure } from "@sixb/core"
|
|
2
|
+
import {
|
|
3
|
+
captureSixbFailure,
|
|
4
|
+
createSixbError,
|
|
5
|
+
isSixbError,
|
|
6
|
+
summarizeErrorMessage,
|
|
7
|
+
} from "@sixb/core/internal/errors"
|
|
8
|
+
import { AGENT_RUN_FAILURE_CODES, type AgentRunFailureCode } from "@sixb/core/storage"
|
|
9
|
+
import { AgentRuntimeProfileError } from "./agent-runtime/errors"
|
|
10
|
+
|
|
11
|
+
export type AgentRunFailure = SixbFailure<AgentRunFailureCode>
|
|
12
|
+
|
|
13
|
+
interface AgentFailureInput {
|
|
14
|
+
readonly status: "failed" | "cancelled"
|
|
15
|
+
readonly at: Date
|
|
16
|
+
readonly details: Readonly<Record<string, string>>
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Normalize a failure without changing its classification. Used outside active execution. */
|
|
20
|
+
export function toAgentRunFailure(error: unknown, input: AgentFailureInput): AgentRunFailure {
|
|
21
|
+
const failureCode = input.status === "cancelled" ? "runtime.cancelled" : "internal.unexpected"
|
|
22
|
+
return captureSixbFailure(error, {
|
|
23
|
+
allowedCodes: AGENT_RUN_FAILURE_CODES,
|
|
24
|
+
defaultCode: failureCode,
|
|
25
|
+
details: input.details,
|
|
26
|
+
at: input.at,
|
|
27
|
+
})
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Classify only work performed by an active Agent execution. */
|
|
31
|
+
export function toAgentExecutionFailure(error: unknown, input: AgentFailureInput): AgentRunFailure {
|
|
32
|
+
const failureError =
|
|
33
|
+
input.status === "failed" ? translateAgentExecutionError(error, input.details) : error
|
|
34
|
+
return toAgentRunFailure(failureError, input)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function translateAgentExecutionError(
|
|
38
|
+
error: unknown,
|
|
39
|
+
details: Readonly<Record<string, string>>
|
|
40
|
+
): unknown {
|
|
41
|
+
if (error instanceof AgentRuntimeProfileError) {
|
|
42
|
+
return createSixbError("agent.execution_failed", error.message, {
|
|
43
|
+
cause: error,
|
|
44
|
+
details: {
|
|
45
|
+
...details,
|
|
46
|
+
provider: error.provider,
|
|
47
|
+
runtimeProfile: error.profile,
|
|
48
|
+
runtimeCheck: error.check,
|
|
49
|
+
runtimeFailure: error.reason,
|
|
50
|
+
...(error.exitCode === undefined ? {} : { runtimeExitCode: String(error.exitCode) }),
|
|
51
|
+
remediation: error.remediation,
|
|
52
|
+
},
|
|
53
|
+
})
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (
|
|
57
|
+
isSixbError(error) &&
|
|
58
|
+
(error.code === "internal.unexpected" ||
|
|
59
|
+
error.code === "agent.execution_failed" ||
|
|
60
|
+
error.code === "ai.usage_limit_exceeded" ||
|
|
61
|
+
error.code === "ai.usage_limit_unavailable")
|
|
62
|
+
) {
|
|
63
|
+
return error
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return createSixbError(
|
|
67
|
+
"agent.execution_failed",
|
|
68
|
+
summarizeErrorMessage(error, "Agent execution failed."),
|
|
69
|
+
{ cause: error, details }
|
|
70
|
+
)
|
|
71
|
+
}
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { waitForAbort } from "./abort"
|
|
2
|
+
import type { ChildQuestion, QuestionRef } from "./child-questions"
|
|
3
|
+
|
|
4
|
+
// A turn and the children it runs share a runner, wherever it runs: the worker, or a computer. So
|
|
5
|
+
// what they say to each other never leaves it. A child's answer and its files reach the turn
|
|
6
|
+
// through the child's run; what it never read when it paused, through its run's result.
|
|
7
|
+
|
|
8
|
+
/** What a turn sends one of its children: a message, or a reply to its question. */
|
|
9
|
+
export interface ChildMail {
|
|
10
|
+
/** Names the turn's tool call, so a replay of the call sends nothing twice. */
|
|
11
|
+
readonly id: string
|
|
12
|
+
readonly kind: "message" | "answer" | "deferred"
|
|
13
|
+
readonly text: string
|
|
14
|
+
/** The question a reply answers. */
|
|
15
|
+
readonly inReplyTo?: string
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** A message a child had not read when it stopped. */
|
|
19
|
+
export type UnreadChildMail = Pick<ChildMail, "id" | "text">
|
|
20
|
+
|
|
21
|
+
/** The child stopped reading: it paused or ended, so mail sent to it now would never be read. */
|
|
22
|
+
export class ChildMailClosedError extends Error {
|
|
23
|
+
readonly name = "ChildMailClosedError"
|
|
24
|
+
constructor(readonly childRunId: string) {
|
|
25
|
+
super(`[SixbAgent] Subagent run '${childRunId}' reads no more mail.`)
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** One child's side of its turn's mailbox. */
|
|
30
|
+
export interface ChildMailbox {
|
|
31
|
+
/** The messages sent since the last take, in the order they were sent. */
|
|
32
|
+
take(): readonly UnreadChildMail[]
|
|
33
|
+
/** Ask the turn, and wait for its reply: an answer, or `deferred` when the turn ends first. */
|
|
34
|
+
ask(question: QuestionRef, signal: AbortSignal): Promise<ChildMail>
|
|
35
|
+
/** Stop reading: what came and was never taken is returned, and later mail is refused. */
|
|
36
|
+
close(): readonly UnreadChildMail[]
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The mail between a turn and the children it runs, held in their runner, one per turn. The turn
|
|
41
|
+
* sends its children messages and replies, and hears their questions and their ends; each child
|
|
42
|
+
* takes its messages at its step boundaries and asks its questions.
|
|
43
|
+
*/
|
|
44
|
+
export class FamilyMailbox {
|
|
45
|
+
readonly #children = new Map<string, ChildBox>()
|
|
46
|
+
/** The questions no reply settled yet, by the child that asked: one at a time each. */
|
|
47
|
+
readonly #questions = new Map<string, ChildQuestion>()
|
|
48
|
+
#wake: { readonly promise: Promise<void>; readonly resolve: () => void } | undefined
|
|
49
|
+
#closed = false
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Send a child mail. Sent twice under one id, it arrives once. Throws
|
|
53
|
+
* {@link ChildMailClosedError} once the child stopped reading, or the turn ended.
|
|
54
|
+
*/
|
|
55
|
+
send(childRunId: string, mail: ChildMail): void {
|
|
56
|
+
if (this.#closed) throw new ChildMailClosedError(childRunId)
|
|
57
|
+
this.#box(childRunId).deliver(mail)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** The child's side, for its runner: mail sent before it started is there already. */
|
|
61
|
+
child(childRunId: string): ChildMailbox {
|
|
62
|
+
return this.#box(childRunId)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** The questions the turn's children wait on, in the order they asked. */
|
|
66
|
+
questions(): readonly ChildQuestion[] {
|
|
67
|
+
return [...this.#questions.values()]
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** The question `childRunId` waits on, if any. */
|
|
71
|
+
question(childRunId: string): ChildQuestion | undefined {
|
|
72
|
+
return this.#questions.get(childRunId)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** The turn took the question to answer it: no other answer is sent to it. */
|
|
76
|
+
answered(question: ChildQuestion): void {
|
|
77
|
+
if (this.#questions.get(question.runId)?.id === question.id) {
|
|
78
|
+
this.#questions.delete(question.runId)
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Open again a question its answer could not be sent to. */
|
|
83
|
+
reopen(question: ChildQuestion): void {
|
|
84
|
+
if (!this.#questions.has(question.runId)) this.#questions.set(question.runId, question)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** A child's run ended, or paused: its runner says so, and the turn's waits wake. */
|
|
88
|
+
finished(childRunId: string): void {
|
|
89
|
+
this.#children.get(childRunId)?.close()
|
|
90
|
+
this.#questions.delete(childRunId)
|
|
91
|
+
this.#changed()
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Settles when a child asks or finishes. Take it before reading what it wakes for, so what
|
|
96
|
+
* comes during the read wakes the wait.
|
|
97
|
+
*/
|
|
98
|
+
changed(): Promise<void> {
|
|
99
|
+
if (!this.#wake) {
|
|
100
|
+
let resolve!: () => void
|
|
101
|
+
const promise = new Promise<void>((settle) => {
|
|
102
|
+
resolve = settle
|
|
103
|
+
})
|
|
104
|
+
this.#wake = { promise, resolve }
|
|
105
|
+
}
|
|
106
|
+
return this.#wake.promise
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** The turn ended: it sends nothing more. */
|
|
110
|
+
close(): void {
|
|
111
|
+
this.#closed = true
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
#box(childRunId: string): ChildBox {
|
|
115
|
+
let box = this.#children.get(childRunId)
|
|
116
|
+
if (!box) {
|
|
117
|
+
box = new ChildBox(childRunId, {
|
|
118
|
+
asked: (question) => {
|
|
119
|
+
this.#questions.set(childRunId, { ...question, runId: childRunId })
|
|
120
|
+
this.#changed()
|
|
121
|
+
},
|
|
122
|
+
withdrawn: (question) => {
|
|
123
|
+
if (this.#questions.get(childRunId)?.id === question.id) {
|
|
124
|
+
this.#questions.delete(childRunId)
|
|
125
|
+
}
|
|
126
|
+
},
|
|
127
|
+
})
|
|
128
|
+
this.#children.set(childRunId, box)
|
|
129
|
+
}
|
|
130
|
+
return box
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
#changed(): void {
|
|
134
|
+
this.#wake?.resolve()
|
|
135
|
+
this.#wake = undefined
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
class ChildBox implements ChildMailbox {
|
|
140
|
+
readonly #seen = new Set<string>()
|
|
141
|
+
#unread: UnreadChildMail[] = []
|
|
142
|
+
#asking: { readonly id: string; readonly reply: (mail: ChildMail) => void } | undefined
|
|
143
|
+
#closed = false
|
|
144
|
+
|
|
145
|
+
constructor(
|
|
146
|
+
private readonly runId: string,
|
|
147
|
+
private readonly family: {
|
|
148
|
+
readonly asked: (question: QuestionRef) => void
|
|
149
|
+
readonly withdrawn: (question: QuestionRef) => void
|
|
150
|
+
}
|
|
151
|
+
) {}
|
|
152
|
+
|
|
153
|
+
deliver(mail: ChildMail): void {
|
|
154
|
+
if (this.#closed) throw new ChildMailClosedError(this.runId)
|
|
155
|
+
if (this.#seen.has(mail.id)) return
|
|
156
|
+
this.#seen.add(mail.id)
|
|
157
|
+
|
|
158
|
+
if (mail.kind === "message") {
|
|
159
|
+
this.#unread.push({ id: mail.id, text: mail.text })
|
|
160
|
+
} else if (this.#asking && mail.inReplyTo === this.#asking.id) {
|
|
161
|
+
this.#asking.reply(mail)
|
|
162
|
+
}
|
|
163
|
+
// A reply to a question the child no longer asks has no one to read it.
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
take(): readonly UnreadChildMail[] {
|
|
167
|
+
const taken = this.#unread
|
|
168
|
+
this.#unread = []
|
|
169
|
+
return taken
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
async ask(question: QuestionRef, signal: AbortSignal): Promise<ChildMail> {
|
|
173
|
+
signal.throwIfAborted()
|
|
174
|
+
const reply = Promise.withResolvers<ChildMail>()
|
|
175
|
+
this.#asking = { id: question.id, reply: reply.resolve }
|
|
176
|
+
this.family.asked(question)
|
|
177
|
+
try {
|
|
178
|
+
return await waitForAbort(reply.promise, signal)
|
|
179
|
+
} finally {
|
|
180
|
+
this.#asking = undefined
|
|
181
|
+
this.family.withdrawn(question)
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
close(): readonly UnreadChildMail[] {
|
|
186
|
+
this.#closed = true
|
|
187
|
+
return this.take()
|
|
188
|
+
}
|
|
189
|
+
}
|
package/src/file-ref.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { FileRef } from "@sixb/core"
|
|
2
|
+
|
|
3
|
+
/** Content identity used only where byte-for-byte duplicates should collapse. */
|
|
4
|
+
export function fileContentKey(fileRef: FileRef): string {
|
|
5
|
+
return `${fileRef.digest}:${fileRef.sizeBytes}`
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/** Exact reference identity for metadata-sensitive lookups such as sandbox paths. */
|
|
9
|
+
export function fileRefKey(fileRef: FileRef): string {
|
|
10
|
+
return JSON.stringify([
|
|
11
|
+
fileRef.blobId,
|
|
12
|
+
fileRef.digest,
|
|
13
|
+
fileRef.sizeBytes,
|
|
14
|
+
fileRef.fileName ?? null,
|
|
15
|
+
fileRef.mediaType ?? null,
|
|
16
|
+
fileRef.logicalPath ?? null,
|
|
17
|
+
])
|
|
18
|
+
}
|
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
export interface AgentAttachmentLimits {
|
|
2
|
+
readonly textInlineMaxBytes: number
|
|
3
|
+
readonly textInlineMaxLines: number
|
|
4
|
+
readonly imageMaxWidth: number
|
|
5
|
+
readonly imageMaxHeight: number
|
|
6
|
+
readonly imageMaxBase64Bytes: number
|
|
7
|
+
readonly imageJpegQualities: readonly number[]
|
|
8
|
+
readonly imageMaxPixels: number
|
|
9
|
+
readonly sandboxFileMaxBytes: number
|
|
10
|
+
readonly sandboxTotalMaxBytes: number
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type ProcessedAgentImage =
|
|
14
|
+
| {
|
|
15
|
+
readonly ok: true
|
|
16
|
+
readonly dataUrl: URL
|
|
17
|
+
readonly mediaType: "image/png" | "image/jpeg" | "image/webp" | "image/gif"
|
|
18
|
+
readonly width: number
|
|
19
|
+
readonly height: number
|
|
20
|
+
readonly originalWidth: number
|
|
21
|
+
readonly originalHeight: number
|
|
22
|
+
readonly wasResized: boolean
|
|
23
|
+
readonly notes: readonly string[]
|
|
24
|
+
}
|
|
25
|
+
| {
|
|
26
|
+
readonly ok: false
|
|
27
|
+
readonly reason: string
|
|
28
|
+
readonly notes: readonly string[]
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
interface ImageMetadata {
|
|
32
|
+
readonly width: number
|
|
33
|
+
readonly height: number
|
|
34
|
+
readonly format: string
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
interface ImageCandidate {
|
|
38
|
+
readonly base64: string
|
|
39
|
+
readonly mediaType: "image/png" | "image/jpeg" | "image/webp" | "image/gif"
|
|
40
|
+
readonly encodedSize: number
|
|
41
|
+
readonly width: number
|
|
42
|
+
readonly height: number
|
|
43
|
+
readonly convertedFrom?: string
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const BUN_FORMAT_TO_MEDIA_TYPE: Readonly<Record<string, ImageCandidate["mediaType"]>> = {
|
|
47
|
+
jpeg: "image/jpeg",
|
|
48
|
+
jpg: "image/jpeg",
|
|
49
|
+
png: "image/png",
|
|
50
|
+
webp: "image/webp",
|
|
51
|
+
gif: "image/gif",
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export async function processAgentImageAttachment(input: {
|
|
55
|
+
readonly bytes: Uint8Array
|
|
56
|
+
readonly declaredMediaType?: string
|
|
57
|
+
readonly fileName?: string
|
|
58
|
+
readonly limits: AgentAttachmentLimits
|
|
59
|
+
readonly signal?: AbortSignal
|
|
60
|
+
}): Promise<ProcessedAgentImage> {
|
|
61
|
+
const { bytes, limits } = input
|
|
62
|
+
input.signal?.throwIfAborted()
|
|
63
|
+
if (typeof Bun.Image !== "function") {
|
|
64
|
+
return {
|
|
65
|
+
ok: false,
|
|
66
|
+
reason: "Bun.Image is unavailable in this runtime",
|
|
67
|
+
notes: ["[Image not inlined: this Bun runtime does not provide image processing.]"],
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
let metadata: ImageMetadata
|
|
72
|
+
try {
|
|
73
|
+
metadata = await readImageMetadata(bytes, limits)
|
|
74
|
+
input.signal?.throwIfAborted()
|
|
75
|
+
} catch (error) {
|
|
76
|
+
input.signal?.throwIfAborted()
|
|
77
|
+
return {
|
|
78
|
+
ok: false,
|
|
79
|
+
reason: imageErrorMessage(error),
|
|
80
|
+
notes: ["[Image omitted: could not be decoded as a supported image format.]"],
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const originalMediaType =
|
|
85
|
+
mediaTypeForFormat(metadata.format) ?? normalizedMediaType(input.declaredMediaType)
|
|
86
|
+
if (!originalMediaType) {
|
|
87
|
+
return {
|
|
88
|
+
ok: false,
|
|
89
|
+
reason: `unsupported image format '${metadata.format}'`,
|
|
90
|
+
notes: ["[Image omitted: unsupported image format.]"],
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const candidates: ImageCandidate[] = []
|
|
95
|
+
const originalBase64Size = base64EncodedSize(bytes.byteLength)
|
|
96
|
+
const withinDimensions =
|
|
97
|
+
metadata.width <= limits.imageMaxWidth && metadata.height <= limits.imageMaxHeight
|
|
98
|
+
if (
|
|
99
|
+
withinDimensions &&
|
|
100
|
+
originalBase64Size < limits.imageMaxBase64Bytes &&
|
|
101
|
+
originalMediaType !== "image/bmp"
|
|
102
|
+
) {
|
|
103
|
+
const originalCandidate = originalCandidateFor(bytes, metadata, originalMediaType)
|
|
104
|
+
if (originalCandidate) {
|
|
105
|
+
return processedImageFromCandidate(originalCandidate, metadata)
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
candidates.push(
|
|
110
|
+
...(await encodedCandidates(bytes, metadata, originalMediaType, limits, input.signal))
|
|
111
|
+
)
|
|
112
|
+
input.signal?.throwIfAborted()
|
|
113
|
+
const winner = candidates
|
|
114
|
+
.filter((candidate) => candidate.encodedSize < limits.imageMaxBase64Bytes)
|
|
115
|
+
.sort((left, right) => left.encodedSize - right.encodedSize)[0]
|
|
116
|
+
|
|
117
|
+
if (!winner) {
|
|
118
|
+
return {
|
|
119
|
+
ok: false,
|
|
120
|
+
reason: "inline image size limit exceeded",
|
|
121
|
+
notes: ["[Image omitted: could not be resized below the inline image size limit.]"],
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return processedImageFromCandidate(winner, metadata)
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
async function readImageMetadata(
|
|
129
|
+
bytes: Uint8Array,
|
|
130
|
+
limits: AgentAttachmentLimits
|
|
131
|
+
): Promise<ImageMetadata> {
|
|
132
|
+
const metadata = await new Bun.Image(bytes, {
|
|
133
|
+
maxPixels: limits.imageMaxPixels,
|
|
134
|
+
autoOrient: true,
|
|
135
|
+
}).metadata()
|
|
136
|
+
return {
|
|
137
|
+
width: metadata.width,
|
|
138
|
+
height: metadata.height,
|
|
139
|
+
format: metadata.format,
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function originalCandidateFor(
|
|
144
|
+
bytes: Uint8Array,
|
|
145
|
+
metadata: ImageMetadata,
|
|
146
|
+
mediaType: string
|
|
147
|
+
): ImageCandidate | undefined {
|
|
148
|
+
if (!isSupportedInlineImageMediaType(mediaType)) {
|
|
149
|
+
return undefined
|
|
150
|
+
}
|
|
151
|
+
return {
|
|
152
|
+
base64: Buffer.from(bytes).toString("base64"),
|
|
153
|
+
mediaType,
|
|
154
|
+
encodedSize: base64EncodedSize(bytes.byteLength),
|
|
155
|
+
width: metadata.width,
|
|
156
|
+
height: metadata.height,
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
async function encodedCandidates(
|
|
161
|
+
bytes: Uint8Array,
|
|
162
|
+
metadata: ImageMetadata,
|
|
163
|
+
originalMediaType: string,
|
|
164
|
+
limits: AgentAttachmentLimits,
|
|
165
|
+
signal?: AbortSignal
|
|
166
|
+
): Promise<ImageCandidate[]> {
|
|
167
|
+
const result: ImageCandidate[] = []
|
|
168
|
+
const resized = () =>
|
|
169
|
+
new Bun.Image(bytes, { maxPixels: limits.imageMaxPixels, autoOrient: true }).resize(
|
|
170
|
+
limits.imageMaxWidth,
|
|
171
|
+
limits.imageMaxHeight,
|
|
172
|
+
{ fit: "inside", withoutEnlargement: true, filter: "lanczos3" }
|
|
173
|
+
)
|
|
174
|
+
|
|
175
|
+
try {
|
|
176
|
+
signal?.throwIfAborted()
|
|
177
|
+
const pngBytes = await resized().png().bytes()
|
|
178
|
+
signal?.throwIfAborted()
|
|
179
|
+
result.push(
|
|
180
|
+
await candidateFromEncodedBytes({
|
|
181
|
+
bytes: pngBytes,
|
|
182
|
+
mediaType: "image/png",
|
|
183
|
+
originalMediaType,
|
|
184
|
+
fallbackWidth: Math.min(metadata.width, limits.imageMaxWidth),
|
|
185
|
+
fallbackHeight: Math.min(metadata.height, limits.imageMaxHeight),
|
|
186
|
+
limits,
|
|
187
|
+
})
|
|
188
|
+
)
|
|
189
|
+
} catch {
|
|
190
|
+
signal?.throwIfAborted()
|
|
191
|
+
// Keep trying JPEG candidates below. The caller reports omission if none fit.
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
for (const quality of limits.imageJpegQualities) {
|
|
195
|
+
try {
|
|
196
|
+
signal?.throwIfAborted()
|
|
197
|
+
const jpegBytes = await resized().jpeg({ quality }).bytes()
|
|
198
|
+
signal?.throwIfAborted()
|
|
199
|
+
result.push(
|
|
200
|
+
await candidateFromEncodedBytes({
|
|
201
|
+
bytes: jpegBytes,
|
|
202
|
+
mediaType: "image/jpeg",
|
|
203
|
+
originalMediaType,
|
|
204
|
+
fallbackWidth: Math.min(metadata.width, limits.imageMaxWidth),
|
|
205
|
+
fallbackHeight: Math.min(metadata.height, limits.imageMaxHeight),
|
|
206
|
+
limits,
|
|
207
|
+
})
|
|
208
|
+
)
|
|
209
|
+
} catch {
|
|
210
|
+
signal?.throwIfAborted()
|
|
211
|
+
// Try the next quality; some source formats/platforms may reject a candidate.
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
return result
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
async function candidateFromEncodedBytes(input: {
|
|
219
|
+
readonly bytes: Uint8Array
|
|
220
|
+
readonly mediaType: "image/png" | "image/jpeg"
|
|
221
|
+
readonly originalMediaType: string
|
|
222
|
+
readonly fallbackWidth: number
|
|
223
|
+
readonly fallbackHeight: number
|
|
224
|
+
readonly limits: AgentAttachmentLimits
|
|
225
|
+
}): Promise<ImageCandidate> {
|
|
226
|
+
const metadata = await readImageMetadata(input.bytes, input.limits).catch(() => ({
|
|
227
|
+
width: input.fallbackWidth,
|
|
228
|
+
height: input.fallbackHeight,
|
|
229
|
+
format: input.mediaType.split("/")[1] ?? "",
|
|
230
|
+
}))
|
|
231
|
+
return {
|
|
232
|
+
base64: Buffer.from(input.bytes).toString("base64"),
|
|
233
|
+
mediaType: input.mediaType,
|
|
234
|
+
encodedSize: base64EncodedSize(input.bytes.byteLength),
|
|
235
|
+
width: metadata.width,
|
|
236
|
+
height: metadata.height,
|
|
237
|
+
...(input.originalMediaType === input.mediaType
|
|
238
|
+
? {}
|
|
239
|
+
: { convertedFrom: input.originalMediaType }),
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function processedImageFromCandidate(
|
|
244
|
+
candidate: ImageCandidate,
|
|
245
|
+
original: ImageMetadata
|
|
246
|
+
): Extract<ProcessedAgentImage, { readonly ok: true }> {
|
|
247
|
+
const notes = imageNotes({
|
|
248
|
+
from: candidate.convertedFrom,
|
|
249
|
+
to: candidate.mediaType,
|
|
250
|
+
originalWidth: original.width,
|
|
251
|
+
originalHeight: original.height,
|
|
252
|
+
width: candidate.width,
|
|
253
|
+
height: candidate.height,
|
|
254
|
+
})
|
|
255
|
+
|
|
256
|
+
return {
|
|
257
|
+
ok: true,
|
|
258
|
+
dataUrl: new URL(`data:${candidate.mediaType};base64,${candidate.base64}`),
|
|
259
|
+
mediaType: candidate.mediaType,
|
|
260
|
+
width: candidate.width,
|
|
261
|
+
height: candidate.height,
|
|
262
|
+
originalWidth: original.width,
|
|
263
|
+
originalHeight: original.height,
|
|
264
|
+
wasResized: candidate.width !== original.width || candidate.height !== original.height,
|
|
265
|
+
notes,
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function base64EncodedSize(byteLength: number): number {
|
|
270
|
+
return Math.ceil(byteLength / 3) * 4
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
function mediaTypeForFormat(format: string): ImageCandidate["mediaType"] | "image/bmp" | undefined {
|
|
274
|
+
const normalized = format.trim().toLowerCase()
|
|
275
|
+
if (normalized === "bmp") {
|
|
276
|
+
return "image/bmp"
|
|
277
|
+
}
|
|
278
|
+
return BUN_FORMAT_TO_MEDIA_TYPE[normalized]
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
function normalizedMediaType(value: string | undefined): string | undefined {
|
|
282
|
+
const normalized = value?.split(";")[0]?.trim().toLowerCase()
|
|
283
|
+
return normalized || undefined
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
function isSupportedInlineImageMediaType(
|
|
287
|
+
mediaType: string
|
|
288
|
+
): mediaType is ImageCandidate["mediaType"] {
|
|
289
|
+
return (
|
|
290
|
+
mediaType === "image/png" ||
|
|
291
|
+
mediaType === "image/jpeg" ||
|
|
292
|
+
mediaType === "image/webp" ||
|
|
293
|
+
mediaType === "image/gif"
|
|
294
|
+
)
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
function imageNotes(input: {
|
|
298
|
+
readonly from: string | undefined
|
|
299
|
+
readonly to: string
|
|
300
|
+
readonly originalWidth: number
|
|
301
|
+
readonly originalHeight: number
|
|
302
|
+
readonly width: number
|
|
303
|
+
readonly height: number
|
|
304
|
+
}): string[] {
|
|
305
|
+
const notes: string[] = []
|
|
306
|
+
if (input.from && input.from !== input.to) {
|
|
307
|
+
notes.push(`[Image converted from ${input.from} to ${input.to}.]`)
|
|
308
|
+
}
|
|
309
|
+
if (input.width !== input.originalWidth || input.height !== input.originalHeight) {
|
|
310
|
+
notes.push(
|
|
311
|
+
`[Image resized from ${input.originalWidth}x${input.originalHeight} to ${input.width}x${input.height}.]`
|
|
312
|
+
)
|
|
313
|
+
}
|
|
314
|
+
return notes
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function imageErrorMessage(error: unknown): string {
|
|
318
|
+
if (error instanceof Error) {
|
|
319
|
+
return error.message
|
|
320
|
+
}
|
|
321
|
+
return String(error)
|
|
322
|
+
}
|