@sixb/agent 0.2.4 → 0.2.5

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 (90) hide show
  1. package/README.md +2 -2
  2. package/dist/agent-prompt.d.ts.map +1 -1
  3. package/dist/artifacts/layout-check/layout-check.d.ts +21 -24
  4. package/dist/artifacts/layout-check/layout-check.d.ts.map +1 -1
  5. package/dist/artifacts/publish-tool.d.ts.map +1 -1
  6. package/dist/index.d.ts +0 -1
  7. package/dist/index.d.ts.map +1 -1
  8. package/dist/index.js +10590 -8888
  9. package/dist/index.js.map +30 -26
  10. package/dist/run-agent-turn.d.ts +3 -14
  11. package/dist/run-agent-turn.d.ts.map +1 -1
  12. package/dist/run-environment.d.ts.map +1 -1
  13. package/dist/run-subagent.d.ts.map +1 -1
  14. package/dist/sandbox-api-context.d.ts.map +1 -1
  15. package/dist/search-script.d.ts +7 -0
  16. package/dist/search-script.d.ts.map +1 -0
  17. package/dist/skills/built-in.d.ts.map +1 -1
  18. package/dist/tools/glob.d.ts +41 -0
  19. package/dist/tools/glob.d.ts.map +1 -0
  20. package/dist/tools/grep.d.ts +64 -0
  21. package/dist/tools/grep.d.ts.map +1 -0
  22. package/dist/tools/model-spec.d.ts.map +1 -1
  23. package/dist/tools/sandbox-search.d.ts +26 -0
  24. package/dist/tools/sandbox-search.d.ts.map +1 -0
  25. package/dist/tools/search/command.d.ts +15 -0
  26. package/dist/tools/search/command.d.ts.map +1 -0
  27. package/dist/tools/search/gitignore.d.ts +28 -0
  28. package/dist/tools/search/gitignore.d.ts.map +1 -0
  29. package/dist/tools/search/glob.d.ts +15 -0
  30. package/dist/tools/search/glob.d.ts.map +1 -0
  31. package/dist/tools/search/main.d.ts +2 -0
  32. package/dist/tools/search/main.d.ts.map +1 -0
  33. package/dist/tools/search/match.d.ts +30 -0
  34. package/dist/tools/search/match.d.ts.map +1 -0
  35. package/dist/tools/search/pattern.d.ts +19 -0
  36. package/dist/tools/search/pattern.d.ts.map +1 -0
  37. package/dist/tools/search/read.d.ts +62 -0
  38. package/dist/tools/search/read.d.ts.map +1 -0
  39. package/dist/tools/search/request.d.ts +80 -0
  40. package/dist/tools/search/request.d.ts.map +1 -0
  41. package/dist/tools/search/search.d.ts +4 -0
  42. package/dist/tools/search/search.d.ts.map +1 -0
  43. package/dist/tools/search/walk.d.ts +43 -0
  44. package/dist/tools/search/walk.d.ts.map +1 -0
  45. package/dist/tools/tool-outputs.d.ts.map +1 -1
  46. package/dist/turn-inbox.d.ts +75 -0
  47. package/dist/turn-inbox.d.ts.map +1 -0
  48. package/package.json +6 -4
  49. package/src/agent-prompt.ts +1 -0
  50. package/src/agent-runtime/preflight.ts +1 -0
  51. package/src/artifacts/layout-check/check.cjs +176 -95
  52. package/src/artifacts/layout-check/layout-check.ts +118 -76
  53. package/src/artifacts/publish-tool.ts +48 -99
  54. package/src/generated/search.mjs +985 -0
  55. package/src/index.ts +0 -1
  56. package/src/run-agent-turn.ts +85 -129
  57. package/src/run-conversation-turn.ts +4 -4
  58. package/src/run-environment.ts +4 -0
  59. package/src/run-subagent.ts +1 -1
  60. package/src/sandbox-api-context.ts +3 -0
  61. package/src/search-script-assets.d.ts +5 -0
  62. package/src/search-script.ts +9 -0
  63. package/src/skills/built-in.ts +16 -0
  64. package/src/skills/sixb-artifacts/SKILL.md +7 -7
  65. package/src/skills/sixb-artifacts/canvas.md +3 -2
  66. package/src/skills/sixb-artifacts/document.md +92 -5
  67. package/src/skills/sixb-artifacts/examples/document/service-proposal/design.css +114 -0
  68. package/src/skills/sixb-artifacts/examples/document/service-proposal/document.html +159 -0
  69. package/src/skills/sixb-artifacts/examples/document/service-proposal/images/fieldwise.svg +1 -0
  70. package/src/skills/sixb-artifacts/examples/document/service-proposal/images/harbor-pine.svg +1 -0
  71. package/src/skills/sixb-artifacts/queries.md +2 -1
  72. package/src/tools/glob.ts +124 -0
  73. package/src/tools/grep.ts +190 -0
  74. package/src/tools/model-spec.ts +4 -0
  75. package/src/tools/sandbox-search.ts +219 -0
  76. package/src/tools/search/command.ts +116 -0
  77. package/src/tools/search/gitignore.ts +101 -0
  78. package/src/tools/search/glob.ts +90 -0
  79. package/src/tools/search/main.ts +9 -0
  80. package/src/tools/search/match.ts +278 -0
  81. package/src/tools/search/pattern.ts +178 -0
  82. package/src/tools/search/read.ts +237 -0
  83. package/src/tools/search/request.ts +101 -0
  84. package/src/tools/search/search.ts +297 -0
  85. package/src/tools/search/walk.ts +145 -0
  86. package/src/tools/tool-outputs.ts +7 -2
  87. package/src/turn-inbox.ts +283 -0
  88. package/dist/artifacts/board-previews.d.ts +0 -41
  89. package/dist/artifacts/board-previews.d.ts.map +0 -1
  90. package/src/artifacts/board-previews.ts +0 -218
@@ -0,0 +1,283 @@
1
+ import type { AgentMessagePart, AgentStepRequestSize } from "@sixb/core"
2
+ import {
3
+ AGENT_MESSAGE_CONTENT_VERSION,
4
+ type AgentRunSession,
5
+ createAgentMessageId,
6
+ type PausableModelLoopResult,
7
+ toModelMessages,
8
+ } from "@sixb/core/internal/agents"
9
+ import type { ModelAssistantPart, ModelMessage, ModelStep } from "@sixb/core/models"
10
+ import type {
11
+ AgentInboxItemRecord,
12
+ AgentMessageRecord,
13
+ AppendAgentMessageInput,
14
+ ConversationAgentRunRecord,
15
+ Principal,
16
+ } from "@sixb/core/storage"
17
+ import { renderCurrentTime, type SentSettings } from "./agent-prompt"
18
+ import { type ConversationReaders, conversationAuthors } from "./message-authors"
19
+ import {
20
+ type AgentToolOutputFileOf,
21
+ agentTraceFromModelSteps,
22
+ agentTraceFromPartialModelLoop,
23
+ } from "./model-adapters"
24
+
25
+ /** A steer the turn read at one of its step boundaries: which step, and what it read. */
26
+ export interface TurnInboxDelivery {
27
+ /** The step the messages came after: its own count of the steps the turn completed. */
28
+ readonly afterSteps: number
29
+ readonly items: readonly AgentInboxItemRecord[]
30
+ }
31
+
32
+ /** What a steer's message is projected from: the thread as the turn started, and who reads it. */
33
+ interface TurnInboxContext {
34
+ readonly retainedMessages: readonly AgentMessageRecord[]
35
+ readonly conversation?: ConversationReaders
36
+ readonly project: SentSettings
37
+ }
38
+
39
+ /**
40
+ * A turn's mid-run steers, drained from its session's inbox at step boundaries.
41
+ *
42
+ * Every item a turn reads is claimed by its run and its step, so a step that reads twice reads
43
+ * once, and a turn that never writes one releases it (see AgentRunSession.finish). The messages
44
+ * here are what the model read: its history plus the steer, projected the way the next turn's
45
+ * replay of `agent_messages` projects it, so both send the same bytes.
46
+ */
47
+ export class TurnInbox {
48
+ readonly #deliveries: TurnInboxDelivery[] = []
49
+ #read: AgentInboxItemRecord[] = []
50
+ readonly #context: TurnInboxContext
51
+
52
+ constructor(
53
+ private readonly session: AgentRunSession,
54
+ context: TurnInboxContext
55
+ ) {
56
+ this.#context = context
57
+ }
58
+
59
+ /** What the turn read, in order. */
60
+ get deliveries(): readonly TurnInboxDelivery[] {
61
+ return this.#deliveries
62
+ }
63
+
64
+ /**
65
+ * The messages the model reads after `afterSteps` steps; none when nothing waits. `afterAnswer`
66
+ * marks a read that came as the model answered, which the next request follows with one more
67
+ * step.
68
+ */
69
+ async take(
70
+ afterSteps: number,
71
+ options: { readonly afterAnswer?: boolean } = {}
72
+ ): Promise<readonly ModelMessage[]> {
73
+ const items = await this.session.inbox.takeSteers({
74
+ stepIndex: afterSteps,
75
+ ...(options.afterAnswer ? { afterAnswer: true } : {}),
76
+ })
77
+ if (items.length === 0) return []
78
+
79
+ this.#deliveries.push({ afterSteps, items })
80
+ this.#read = [...this.#read, ...items]
81
+ return steerModelMessages(items, this.#context, this.#read)
82
+ }
83
+ }
84
+
85
+ /**
86
+ * A steer's parts as one user message, projected as a later turn replays it. A steer carries text
87
+ * and context only: storage refuses a file, whose text a later turn derives from the attachments it
88
+ * prepares, so the bytes could not be the same.
89
+ */
90
+ function steerModelMessages(
91
+ items: readonly AgentInboxItemRecord[],
92
+ context: TurnInboxContext,
93
+ read: readonly AgentInboxItemRecord[]
94
+ ): readonly ModelMessage[] {
95
+ // Who wrote them, as the thread reads once they are saved: the turn appends each steer to the
96
+ // conversation a later turn replays, so the same authors close the same messages.
97
+ const authors = context.conversation
98
+ ? conversationAuthors({
99
+ conversation: context.conversation,
100
+ messages: [...context.retainedMessages, ...read.map(savedRecord)],
101
+ }).byMessageId
102
+ : undefined
103
+
104
+ const closing = new Map<string, string>()
105
+ for (const item of items) {
106
+ const sent = renderCurrentTime(item.createdAt, {
107
+ timeZone: item.timeZone ?? context.project.timeZone,
108
+ locale: item.locale ?? context.project.locale,
109
+ })
110
+ closing.set(item.messageId, [sent, authors?.get(item.messageId)].filter(Boolean).join("\n"))
111
+ }
112
+
113
+ return toModelMessages(
114
+ items.map((item) => ({ id: item.messageId, role: "user" as const, parts: item.parts })),
115
+ { userMessageSuffix: (message) => (message.id ? closing.get(message.id) : undefined) }
116
+ )
117
+ }
118
+
119
+ /** A steer as the conversation holds it once the turn appends it: what its authors read from. */
120
+ function savedRecord(item: AgentInboxItemRecord): AgentMessageRecord {
121
+ return {
122
+ id: item.messageId,
123
+ projectId: item.projectId,
124
+ threadId: item.threadId,
125
+ runId: null,
126
+ role: "user",
127
+ authorPrincipal: item.authorPrincipal,
128
+ seq: 0,
129
+ parts: item.parts,
130
+ contentVersion: AGENT_MESSAGE_CONTENT_VERSION,
131
+ createdAt: item.createdAt,
132
+ }
133
+ }
134
+
135
+ /**
136
+ * Slice the steps a turn completed, or the part of one it did not, around the steers it read, as
137
+ * `agent_messages` rows: one assistant message up to each delivery, the steer as its own user
138
+ * message, then the rest. A steer no step followed — the turn stopped before answering it — is
139
+ * left out, so finishing without its message releases it back to the thread's inbox.
140
+ */
141
+ export function turnWorkMessages(input: {
142
+ readonly steps: readonly ModelStep[]
143
+ /** What an aborted step streamed: the one step `steps` does not hold whole. */
144
+ readonly partialContent?: readonly ModelAssistantPart[]
145
+ readonly aborted?: boolean
146
+ readonly run: ConversationAgentRunRecord
147
+ readonly deliveries: readonly TurnInboxDelivery[]
148
+ /** The first step whose request the context guard reduced: it and those after it are marked. */
149
+ readonly reducedFromStep?: number
150
+ readonly stepRequests: readonly (AgentStepRequestSize | undefined)[]
151
+ readonly outputFileOf?: AgentToolOutputFileOf
152
+ readonly authorPrincipal?: Principal
153
+ }): readonly AppendAgentMessageInput[] {
154
+ const { steps, run, deliveries, reducedFromStep, stepRequests, outputFileOf } = input
155
+ const details = { threadId: run.threadId, runId: run.id }
156
+ const messages: AppendAgentMessageInput[] = []
157
+
158
+ const pushSteps = (parts: readonly AgentMessagePart[], from: number, to: number) => {
159
+ const marked = recordStepRequests(
160
+ markReducedSteps(parts, reducedFromStep === undefined ? undefined : reducedFromStep - from),
161
+ stepRequests.slice(from, to)
162
+ )
163
+ if (!marked.some((part) => part.type !== "step-start")) return
164
+
165
+ messages.push({
166
+ id: createAgentMessageId(),
167
+ projectId: run.projectId,
168
+ threadId: run.threadId,
169
+ runId: run.id,
170
+ role: "assistant",
171
+ parts: marked,
172
+ ...(input.authorPrincipal === undefined ? {} : { authorPrincipal: input.authorPrincipal }),
173
+ })
174
+ }
175
+
176
+ // A delivery no step followed reads unread: the turn stopped before the model answered it.
177
+ const answered = (delivery: TurnInboxDelivery) => {
178
+ if (steps.length > delivery.afterSteps) return true
179
+ return input.aborted === true && (input.partialContent?.length ?? 0) > 0
180
+ }
181
+
182
+ let from = 0
183
+ for (const delivery of deliveries) {
184
+ pushSteps(
185
+ agentTraceFromModelSteps(steps.slice(from, delivery.afterSteps), details, outputFileOf),
186
+ from,
187
+ delivery.afterSteps
188
+ )
189
+ from = delivery.afterSteps
190
+ if (!answered(delivery)) break
191
+
192
+ for (const item of delivery.items) {
193
+ messages.push(savedSteer(item))
194
+ }
195
+ }
196
+
197
+ const rest = steps.slice(from)
198
+ pushSteps(
199
+ input.aborted === true
200
+ ? agentTraceFromPartialModelLoop(rest, input.partialContent ?? [], details, outputFileOf)
201
+ : agentTraceFromModelSteps(rest, details, outputFileOf),
202
+ from,
203
+ stepRequests.length
204
+ )
205
+ return messages
206
+ }
207
+
208
+ /** The rows a turn finishes with, for the model loop it just ran. */
209
+ export function turnResultMessages(
210
+ result: PausableModelLoopResult,
211
+ input: Omit<Parameters<typeof turnWorkMessages>[0], "steps" | "partialContent" | "aborted">
212
+ ): readonly AppendAgentMessageInput[] {
213
+ return result.status === "aborted"
214
+ ? turnWorkMessages({
215
+ ...input,
216
+ steps: result.steps,
217
+ partialContent: result.partialContent,
218
+ aborted: true,
219
+ })
220
+ : turnWorkMessages({ ...input, steps: result.steps })
221
+ }
222
+
223
+ /**
224
+ * A steer as the thread keeps it: a user message of no run, authored by whoever sent it, with the
225
+ * time zone and locale its `<sixb_current_time>` was rendered in.
226
+ */
227
+ function savedSteer(item: AgentInboxItemRecord): AppendAgentMessageInput {
228
+ const metadata =
229
+ item.timeZone === undefined && item.locale === undefined
230
+ ? undefined
231
+ : {
232
+ ...(item.timeZone === undefined ? {} : { timeZone: item.timeZone }),
233
+ ...(item.locale === undefined ? {} : { locale: item.locale }),
234
+ }
235
+ return {
236
+ id: item.messageId,
237
+ projectId: item.projectId,
238
+ threadId: item.threadId,
239
+ runId: null,
240
+ role: "user",
241
+ parts: item.parts,
242
+ authorPrincipal: item.authorPrincipal,
243
+ createdAt: item.createdAt,
244
+ ...(metadata === undefined ? {} : { metadata }),
245
+ }
246
+ }
247
+
248
+ /**
249
+ * Mark each step from `fromStep` on as answering a request the turn reduced, for the next turn's
250
+ * estimate not to go by what those calls read.
251
+ */
252
+ export function markReducedSteps(
253
+ parts: readonly AgentMessagePart[],
254
+ fromStep: number | undefined
255
+ ): readonly AgentMessagePart[] {
256
+ if (fromStep === undefined) return parts
257
+
258
+ let step = -1
259
+ return parts.map((part) => {
260
+ if (part.type !== "step-start") return part
261
+
262
+ step += 1
263
+ return step >= fromStep ? { type: "step-start", reducedRequest: true } : part
264
+ })
265
+ }
266
+
267
+ /**
268
+ * Record on each step-start what its request carried, by step: the next turn's context meter
269
+ * reads the system prompt and tools the latest one sent.
270
+ */
271
+ export function recordStepRequests(
272
+ parts: readonly AgentMessagePart[],
273
+ requests: readonly (AgentStepRequestSize | undefined)[]
274
+ ): readonly AgentMessagePart[] {
275
+ let step = -1
276
+ return parts.map((part) => {
277
+ if (part.type !== "step-start") return part
278
+
279
+ step += 1
280
+ const request = requests[step]
281
+ return request ? { ...part, request } : part
282
+ })
283
+ }
@@ -1,41 +0,0 @@
1
- import type { BlobStorage, Sandbox } from "@sixb/core";
2
- import type { AgentRunMissingPreview } from "@sixb/core/internal/agents";
3
- import { type BoardPreviewInput, type MissingBoardPreview, type RefusedBoardPreview } from "@sixb/core/internal/artifacts/publish";
4
- import type { ArtifactVersionRecord } from "@sixb/core/storage";
5
- import type { AgentSandboxHandle } from "../sandbox-handle";
6
- import { type CheckedPreview, type CheckedQuery } from "./layout-check/layout-check";
7
- /** The pictures read and stored, and those that were not, with why. */
8
- export interface CheckedPreviews {
9
- readonly previews: readonly BoardPreviewInput[];
10
- readonly dropped: readonly RefusedBoardPreview[];
11
- }
12
- /**
13
- * The pictures the check took that `missing` asks for, read out of the sandbox a batch at a time
14
- * and each stored in `blobStorage`. A picture the check did not take is left out: the worker
15
- * renders it later. Each stands alone: one too large or that cannot be read or stored is dropped,
16
- * and returned with why, and the others are kept.
17
- */
18
- export declare function storeCheckedPreviews(input: {
19
- readonly handle: AgentSandboxHandle;
20
- readonly checked: readonly CheckedPreview[];
21
- readonly missing: readonly AgentRunMissingPreview[];
22
- readonly blobStorage: Pick<BlobStorage, "put">;
23
- readonly signal: AbortSignal;
24
- }): Promise<CheckedPreviews>;
25
- /**
26
- * Renders the pictures a stored canvas version's boards miss, in `sandbox`, a blank one created
27
- * for it: the version's files are written there, each missing board is opened in every mode it
28
- * misses, and the pictures are stored in `blobStorage`. What the boards' queries return, `queries`
29
- * holds as the version's publisher read them. Without them a board that reads a query is not
30
- * pictured: it would show its queries rejecting, which no reader sees. A sandbox without the
31
- * agent image's browser renders nothing.
32
- */
33
- export declare function renderBoardPreviews(input: {
34
- readonly sandbox: Sandbox;
35
- readonly version: ArtifactVersionRecord;
36
- readonly missing: readonly MissingBoardPreview[];
37
- readonly blobStorage: Pick<BlobStorage, "put" | "open" | "stat">;
38
- readonly queries?: Readonly<Record<string, CheckedQuery>>;
39
- readonly signal: AbortSignal;
40
- }): Promise<CheckedPreviews>;
41
- //# sourceMappingURL=board-previews.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"board-previews.d.ts","sourceRoot":"","sources":["../../src/artifacts/board-previews.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;AAEtD,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,4BAA4B,CAAA;AACxE,OAAO,EAGL,KAAK,iBAAiB,EAEtB,KAAK,mBAAmB,EACxB,KAAK,mBAAmB,EACzB,MAAM,uCAAuC,CAAA;AAC9C,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAA;AAG/D,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAA;AAC3D,OAAO,EAAE,KAAK,cAAc,EAAE,KAAK,YAAY,EAAe,MAAM,6BAA6B,CAAA;AAiBjG,uEAAuE;AACvE,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,QAAQ,EAAE,SAAS,iBAAiB,EAAE,CAAA;IAC/C,QAAQ,CAAC,OAAO,EAAE,SAAS,mBAAmB,EAAE,CAAA;CACjD;AAED;;;;;GAKG;AACH,wBAAsB,oBAAoB,CAAC,KAAK,EAAE;IAChD,QAAQ,CAAC,MAAM,EAAE,kBAAkB,CAAA;IACnC,QAAQ,CAAC,OAAO,EAAE,SAAS,cAAc,EAAE,CAAA;IAC3C,QAAQ,CAAC,OAAO,EAAE,SAAS,sBAAsB,EAAE,CAAA;IACnD,QAAQ,CAAC,WAAW,EAAE,IAAI,CAAC,WAAW,EAAE,KAAK,CAAC,CAAA;IAC9C,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAA;CAC7B,GAAG,OAAO,CAAC,eAAe,CAAC,CAwD3B;AAqBD;;;;;;;GAOG;AACH,wBAAsB,mBAAmB,CAAC,KAAK,EAAE;IAC/C,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAA;IACzB,QAAQ,CAAC,OAAO,EAAE,qBAAqB,CAAA;IACvC,QAAQ,CAAC,OAAO,EAAE,SAAS,mBAAmB,EAAE,CAAA;IAChD,QAAQ,CAAC,WAAW,EAAE,IAAI,CAAC,WAAW,EAAE,KAAK,GAAG,MAAM,GAAG,MAAM,CAAC,CAAA;IAChE,QAAQ,CAAC,OAAO,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC,CAAA;IACzD,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAA;CAC7B,GAAG,OAAO,CAAC,eAAe,CAAC,CAmD3B"}
@@ -1,218 +0,0 @@
1
- import { posix } from "node:path"
2
- import type { BlobStorage, Sandbox } from "@sixb/core"
3
- import { ARTIFACT_THEME_FILE, CANVAS_FILE, parseArtifactTheme } from "@sixb/core/artifacts"
4
- import type { AgentRunMissingPreview } from "@sixb/core/internal/agents"
5
- import {
6
- artifactQueryName,
7
- BOARD_PREVIEW_MEDIA_TYPES,
8
- type BoardPreviewInput,
9
- MAX_BOARD_PREVIEW_BYTES,
10
- type MissingBoardPreview,
11
- type RefusedBoardPreview,
12
- } from "@sixb/core/internal/artifacts/publish"
13
- import type { ArtifactVersionRecord } from "@sixb/core/storage"
14
- import { SANDBOX_CONTEXT_DIRECTORY } from "../run-sandbox"
15
- import { readSandboxFiles } from "../sandbox-files"
16
- import type { AgentSandboxHandle } from "../sandbox-handle"
17
- import { type CheckedPreview, type CheckedQuery, checkLayout } from "./layout-check/layout-check"
18
- import { type PublishedArtifactFile, readArtifactVersionFiles } from "./materialize"
19
-
20
- // The pictures of a canvas's boards, which the app shows until a reader opens a board. The layout
21
- // check takes them as it checks each board; these read them out of the sandbox and store them as
22
- // blobs, for the version's previews. The agent worker renders the same way in a sandbox of its
23
- // own, for a version nobody rendered: one a person edited in the app.
24
-
25
- const READ_TIMEOUT_MS = 60_000
26
- /**
27
- * The most one command reads out of the sandbox, base64 on its stdout: a canvas of a hundred
28
- * boards' pictures takes a few commands, not one each.
29
- */
30
- const READ_BATCH_BYTES = 24 * 1024 * 1024
31
- /** Where the worker writes the version it renders, in its blank sandbox. */
32
- const WORK_DIRECTORY = "canvas"
33
-
34
- /** The pictures read and stored, and those that were not, with why. */
35
- export interface CheckedPreviews {
36
- readonly previews: readonly BoardPreviewInput[]
37
- readonly dropped: readonly RefusedBoardPreview[]
38
- }
39
-
40
- /**
41
- * The pictures the check took that `missing` asks for, read out of the sandbox a batch at a time
42
- * and each stored in `blobStorage`. A picture the check did not take is left out: the worker
43
- * renders it later. Each stands alone: one too large or that cannot be read or stored is dropped,
44
- * and returned with why, and the others are kept.
45
- */
46
- export async function storeCheckedPreviews(input: {
47
- readonly handle: AgentSandboxHandle
48
- readonly checked: readonly CheckedPreview[]
49
- readonly missing: readonly AgentRunMissingPreview[]
50
- readonly blobStorage: Pick<BlobStorage, "put">
51
- readonly signal: AbortSignal
52
- }): Promise<CheckedPreviews> {
53
- const wanted = new Set(
54
- input.missing.flatMap(({ board, themes }) => themes.map((theme) => `${board}\0${theme}`))
55
- )
56
- const pictures = input.checked.filter(({ board, theme }) => wanted.has(`${board}\0${theme}`))
57
-
58
- const previews: BoardPreviewInput[] = []
59
- const dropped: RefusedBoardPreview[] = []
60
- const drop = (picture: CheckedPreview, reason: string) => {
61
- const { board, theme, size } = picture
62
- dropped.push({ board, theme, size, reason })
63
- }
64
-
65
- for (const batch of readBatches(pictures)) {
66
- const read = await readSandboxFiles(input.handle, {
67
- paths: batch.map((picture) => picture.path),
68
- maxBytes: MAX_BOARD_PREVIEW_BYTES,
69
- signal: input.signal,
70
- timeoutMs: READ_TIMEOUT_MS,
71
- })
72
-
73
- for (const [index, picture] of batch.entries()) {
74
- const file = read[index]
75
- if (!file || "error" in file) {
76
- drop(
77
- picture,
78
- file?.error === "too_large"
79
- ? `it is past the ${MAX_BOARD_PREVIEW_BYTES} bytes kept`
80
- : "it could not be read"
81
- )
82
- continue
83
- }
84
-
85
- try {
86
- const stored = await input.blobStorage.put({
87
- body: file.bytes,
88
- expectedSizeBytes: file.bytes.byteLength,
89
- signal: input.signal,
90
- fileName: posix.basename(picture.path),
91
- mediaType: BOARD_PREVIEW_MEDIA_TYPES[picture.size],
92
- })
93
- previews.push({
94
- board: picture.board,
95
- theme: picture.theme,
96
- size: picture.size,
97
- file: stored,
98
- })
99
- } catch (error) {
100
- if (input.signal.aborted) throw error
101
- console.warn(`[SixbAgent] Could not store the picture '${picture.path}':`, error)
102
- drop(picture, "it could not be stored")
103
- }
104
- }
105
- }
106
-
107
- return { previews, dropped }
108
- }
109
-
110
- /** The pictures in batches of at most {@link READ_BATCH_BYTES}, as the check sized them. */
111
- function readBatches(pictures: readonly CheckedPreview[]): CheckedPreview[][] {
112
- const batches: CheckedPreview[][] = []
113
- let batch: CheckedPreview[] = []
114
- let bytes = 0
115
- for (const picture of pictures) {
116
- const size = picture.bytes ?? MAX_BOARD_PREVIEW_BYTES
117
- if (batch.length > 0 && bytes + size > READ_BATCH_BYTES) {
118
- batches.push(batch)
119
- batch = []
120
- bytes = 0
121
- }
122
- batch.push(picture)
123
- bytes += Math.min(size, MAX_BOARD_PREVIEW_BYTES)
124
- }
125
- if (batch.length > 0) batches.push(batch)
126
- return batches
127
- }
128
-
129
- /**
130
- * Renders the pictures a stored canvas version's boards miss, in `sandbox`, a blank one created
131
- * for it: the version's files are written there, each missing board is opened in every mode it
132
- * misses, and the pictures are stored in `blobStorage`. What the boards' queries return, `queries`
133
- * holds as the version's publisher read them. Without them a board that reads a query is not
134
- * pictured: it would show its queries rejecting, which no reader sees. A sandbox without the
135
- * agent image's browser renders nothing.
136
- */
137
- export async function renderBoardPreviews(input: {
138
- readonly sandbox: Sandbox
139
- readonly version: ArtifactVersionRecord
140
- readonly missing: readonly MissingBoardPreview[]
141
- readonly blobStorage: Pick<BlobStorage, "put" | "open" | "stat">
142
- readonly queries?: Readonly<Record<string, CheckedQuery>>
143
- readonly signal: AbortSignal
144
- }): Promise<CheckedPreviews> {
145
- const { sandbox, version, signal } = input
146
- if (input.missing.length === 0) return { previews: [], dropped: [] }
147
-
148
- const handle: AgentSandboxHandle = { sandbox, contextDirectory: SANDBOX_CONTEXT_DIRECTORY }
149
- const files = await readArtifactVersionFiles(input.blobStorage, version, signal)
150
- const bytesOf = (path: string) => files.find((file) => file.path === path)?.bytes
151
-
152
- const reading = input.queries ? undefined : boardsReadingQueries(files)
153
- const missing = input.missing.filter(({ board }) => !reading?.(board.file))
154
- if (missing.length === 0) return { previews: [], dropped: [] }
155
-
156
- const directory = posix.resolve(sandbox.workingDirectory, WORK_DIRECTORY)
157
- await sandbox.writeFiles(
158
- files
159
- .filter((file) => file.path !== CANVAS_FILE)
160
- .map((file) => ({ path: posix.join(directory, file.path), contents: file.bytes }))
161
- )
162
-
163
- const themeFile = bytesOf(ARTIFACT_THEME_FILE)
164
- const theme = themeFile ? parseArtifactTheme(new TextDecoder().decode(themeFile)) : null
165
- const check = await checkLayout({
166
- handle,
167
- layout: "canvas",
168
- boards: missing.flatMap(({ board }) => {
169
- const html = bytesOf(board.file)
170
- return html
171
- ? [{ id: board.id, file: board.file, w: board.w, h: board.h, html, pictured: true }]
172
- : []
173
- }),
174
- directory,
175
- files: files.map((file) => file.path),
176
- name: `${version.artifactId}-v${version.version}`,
177
- ...(theme && "theme" in theme ? { theme: theme.theme } : {}),
178
- ...(input.queries ? { queries: input.queries } : {}),
179
- signal,
180
- })
181
- if (check.status !== "checked") {
182
- console.warn(
183
- `[SixbAgent] The boards of version ${version.version} of '${version.artifactId}' were not pictured: ${check.reason}.`
184
- )
185
- return { previews: [], dropped: [] }
186
- }
187
-
188
- return storeCheckedPreviews({
189
- handle,
190
- checked: check.previews,
191
- missing: missing.map(({ board, themes }) => ({ board: board.id, themes })),
192
- blobStorage: input.blobStorage,
193
- signal,
194
- })
195
- }
196
-
197
- /**
198
- * Whether a board may read a query, for a version whose answers are not at hand: none when the
199
- * version holds no query; otherwise a board whose HTML names `query`, and every board once a
200
- * script beside them does, since which boards load it is not known. Erring on the side of reading
201
- * one only leaves a board its outline.
202
- */
203
- function boardsReadingQueries(
204
- files: readonly PublishedArtifactFile[]
205
- ): ((boardFile: string) => boolean) | undefined {
206
- if (!files.some((file) => artifactQueryName(file.path) !== undefined)) return undefined
207
-
208
- const names = (file: PublishedArtifactFile) =>
209
- /\bquery\b/.test(new TextDecoder().decode(file.bytes))
210
- if (files.some((file) => SCRIPT.test(file.path) && names(file))) return () => true
211
-
212
- return (boardFile) => {
213
- const html = files.find((file) => file.path === boardFile)
214
- return html === undefined || names(html)
215
- }
216
- }
217
-
218
- const SCRIPT = /\.m?js$/i