@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
@@ -0,0 +1,273 @@
1
+ import type { AgentMessagePart, JsonValue } from "@sixb/core"
2
+ import { AgentToolPublicError } from "@sixb/core"
3
+ import {
4
+ type AgentToolCall,
5
+ type AgentToolCallFile,
6
+ type AgentToolCallOptions,
7
+ type AgentToolCallResult,
8
+ AgentToolResultValidationError,
9
+ type AgentToolSpec,
10
+ } from "@sixb/core/internal/agents"
11
+ import { createSixbError } from "@sixb/core/internal/errors"
12
+ import type {
13
+ ModelAssistantPart,
14
+ ModelStep,
15
+ ModelTool,
16
+ ModelToolResultPart,
17
+ } from "@sixb/core/models"
18
+ import { AgentToolExecutionError, AgentToolOutputError } from "./errors"
19
+ import type { AgentToolModelOutput } from "./tools/result-output"
20
+
21
+ export type AgentErrorDetails =
22
+ | { readonly threadId: string; readonly runId: string }
23
+ | { readonly parentRunId: string; readonly runId: string }
24
+ | { readonly workflowId: string; readonly agentStepId: string; readonly runId: string }
25
+ | {
26
+ readonly agentStepId: string
27
+ readonly workflowId: string
28
+ readonly workflowRunId: string
29
+ readonly nodeRunId: string
30
+ }
31
+
32
+ /** How a run calls a project tool: through its session, or for a workflow step in the worker. */
33
+ export type AgentToolCaller = (
34
+ call: AgentToolCall,
35
+ options: AgentToolCallOptions
36
+ ) => Promise<AgentToolCallResult>
37
+
38
+ interface ProjectModelToolsInput {
39
+ readonly specs: readonly AgentToolSpec[]
40
+ readonly runId: string
41
+ readonly callTool: AgentToolCaller
42
+ /** Where the sandbox keeps the run's files, which the paths a tool's files get start with. */
43
+ readonly contextDirectory: string
44
+ /** The bytes the run's file budget has left, which a call's files may take. */
45
+ readonly fileBytesLeft: () => number
46
+ /** Copy the files a call stored into the sandbox, before the model reads its output. */
47
+ readonly installFiles: (input: {
48
+ readonly toolName: string
49
+ readonly files: readonly AgentToolCallFile[]
50
+ readonly signal: AbortSignal
51
+ }) => Promise<void>
52
+ readonly toolResultToModelOutput: (input: {
53
+ readonly output: JsonValue
54
+ readonly signal: AbortSignal
55
+ readonly toolCallId: string
56
+ }) => AgentToolModelOutput | Promise<AgentToolModelOutput>
57
+ readonly errorDetails?: AgentErrorDetails
58
+ }
59
+
60
+ /**
61
+ * The project's tools as the model is offered them. A call runs where the project is, through
62
+ * `callTool`, which checks its input against the tool's schema; the files it stored are copied into
63
+ * the sandbox before the model reads its output.
64
+ */
65
+ export function projectModelTools(input: ProjectModelToolsInput): readonly ModelTool[] {
66
+ const names = new Set<string>()
67
+ return input.specs.map((spec) => {
68
+ if (names.has(spec.name)) {
69
+ throw createSixbError(
70
+ "internal.unexpected",
71
+ `[SixbAgent] Agent run '${input.runId}' has duplicate selected tool name '${spec.name}'.`,
72
+ input.errorDetails === undefined ? undefined : { details: input.errorDetails }
73
+ )
74
+ }
75
+ names.add(spec.name)
76
+ return projectModelTool(spec, input)
77
+ })
78
+ }
79
+
80
+ function projectModelTool(
81
+ spec: AgentToolSpec,
82
+ context: Omit<ProjectModelToolsInput, "specs">
83
+ ): ModelTool<Readonly<Record<string, unknown>>> {
84
+ return {
85
+ ...spec,
86
+ parseInput(value) {
87
+ // The tool's schema is checked where it runs; the loop hands over a JSON object.
88
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
89
+ throw new TypeError(`[SixbAgent] Agent tool '${spec.name}' input must be a JSON object.`)
90
+ }
91
+ return value as Readonly<Record<string, unknown>>
92
+ },
93
+ async execute(toolInput, { signal, toolCallId }) {
94
+ let result: AgentToolCallResult
95
+ try {
96
+ result = await context.callTool(
97
+ { toolName: spec.name, toolCallId, input: toolInput },
98
+ {
99
+ signal,
100
+ maxFileBytes: context.fileBytesLeft(),
101
+ contextDirectory: context.contextDirectory,
102
+ }
103
+ )
104
+ } catch (error) {
105
+ if (error instanceof AgentToolResultValidationError) {
106
+ throw new AgentToolOutputError(spec.name, error.reason, { cause: error })
107
+ }
108
+ if (error instanceof AgentToolPublicError) throw error
109
+ throw new AgentToolExecutionError(spec.name, { cause: error })
110
+ }
111
+ await context.installFiles({ toolName: spec.name, files: result.files, signal })
112
+ return result.output
113
+ },
114
+ toModelOutput(output, { signal, toolCallId }) {
115
+ return context.toolResultToModelOutput({ output, signal, toolCallId })
116
+ },
117
+ errorText: agentToolErrorText,
118
+ }
119
+ }
120
+
121
+ type ToolOutcome =
122
+ | {
123
+ readonly state: "output-available"
124
+ readonly output: JsonValue
125
+ readonly modelOutput?: AgentToolModelOutput
126
+ readonly outputFile?: AgentToolCallFile
127
+ }
128
+ | { readonly state: "output-error"; readonly errorText: string }
129
+
130
+ /** The stored file of a call whose output was too large for the context (`outputFileOf`). */
131
+ export type AgentToolOutputFileOf = (toolCallId: string) => AgentToolCallFile | undefined
132
+
133
+ /** Project validated model-loop steps into Sixb's durable trace contract. */
134
+ export function agentTraceFromModelSteps(
135
+ steps: readonly ModelStep[],
136
+ errorDetails?: AgentErrorDetails,
137
+ outputFileOf?: AgentToolOutputFileOf
138
+ ): readonly AgentMessagePart[] {
139
+ return steps.flatMap((step) =>
140
+ tracePartsFromModelContent(step.content, { errorDetails, outputFileOf })
141
+ )
142
+ }
143
+
144
+ /** Convert an aborted model loop, retaining only coherent content from its in-flight step. */
145
+ export function agentTraceFromPartialModelLoop(
146
+ steps: readonly ModelStep[],
147
+ partialContent: readonly ModelAssistantPart[],
148
+ errorDetails?: AgentErrorDetails,
149
+ outputFileOf?: AgentToolOutputFileOf
150
+ ): readonly AgentMessagePart[] {
151
+ return [
152
+ ...agentTraceFromModelSteps(steps, errorDetails, outputFileOf),
153
+ ...(partialContent.length === 0
154
+ ? []
155
+ : tracePartsFromModelContent(partialContent, {
156
+ errorDetails,
157
+ outputFileOf,
158
+ missingToolResultText: "Tool execution was cancelled.",
159
+ })),
160
+ ]
161
+ }
162
+
163
+ function tracePartsFromModelContent(
164
+ content: readonly ModelAssistantPart[],
165
+ options: {
166
+ readonly errorDetails?: AgentErrorDetails
167
+ readonly outputFileOf?: AgentToolOutputFileOf
168
+ readonly missingToolResultText?: string
169
+ }
170
+ ): AgentMessagePart[] {
171
+ const { errorDetails, outputFileOf } = options
172
+ const missingToolResultText =
173
+ options.missingToolResultText ?? "Tool call did not produce a result."
174
+ const results = new Map(
175
+ content.filter((part) => part.type === "tool-result").map((part) => [part.toolCallId, part])
176
+ )
177
+ return [
178
+ { type: "step-start" },
179
+ ...content.flatMap((part): AgentMessagePart[] => {
180
+ switch (part.type) {
181
+ case "text":
182
+ case "reasoning": {
183
+ return [
184
+ {
185
+ type: part.type,
186
+ text: part.text,
187
+ ...(part.providerData === undefined ? {} : { providerMetadata: part.providerData }),
188
+ },
189
+ ]
190
+ }
191
+ case "provider-state":
192
+ return [
193
+ {
194
+ type: "provider-state",
195
+ providerId: part.providerId,
196
+ data: part.data,
197
+ },
198
+ ]
199
+ case "tool-call": {
200
+ return [
201
+ {
202
+ type: "tool-call",
203
+ ...(part.dynamic === true ? { dynamic: true } : {}),
204
+ toolCallId: part.toolCallId,
205
+ toolName: part.toolName,
206
+ input: part.input,
207
+ ...(part.providerExecuted === undefined
208
+ ? {}
209
+ : { providerExecuted: part.providerExecuted }),
210
+ ...(part.providerData === undefined ? {} : { providerMetadata: part.providerData }),
211
+ ...toolOutcome(
212
+ results.get(part.toolCallId),
213
+ missingToolResultText,
214
+ outputFileOf?.(part.toolCallId)
215
+ ),
216
+ },
217
+ ]
218
+ }
219
+ case "tool-result":
220
+ return []
221
+ default:
222
+ return assertUnreachableModelPart(part, errorDetails)
223
+ }
224
+ }),
225
+ ]
226
+ }
227
+
228
+ function toolOutcome(
229
+ result: ModelToolResultPart | undefined,
230
+ missingResultText: string,
231
+ outputFile: AgentToolCallFile | undefined
232
+ ): ToolOutcome {
233
+ if (!result) return { state: "output-error", errorText: missingResultText }
234
+ const { output } = result
235
+
236
+ // The tool projected its output for the model: the trace keeps both, and later turns replay
237
+ // what the model read, byte for byte. An output saved to a file keeps it as well.
238
+ if (result.originalOutput !== undefined) {
239
+ return {
240
+ state: "output-available",
241
+ output: result.originalOutput,
242
+ ...(output.type === "text"
243
+ ? { modelOutput: { type: "text", value: output.value } }
244
+ : output.type === "json"
245
+ ? { modelOutput: { type: "json", value: output.value } }
246
+ : {}),
247
+ ...(outputFile === undefined ? {} : { outputFile }),
248
+ }
249
+ }
250
+
251
+ switch (output.type) {
252
+ case "text":
253
+ case "json":
254
+ return { state: "output-available", output: output.value }
255
+ case "error-text":
256
+ return { state: "output-error", errorText: output.value }
257
+ case "error-json":
258
+ return { state: "output-error", errorText: JSON.stringify(output.value) }
259
+ }
260
+ }
261
+
262
+ function assertUnreachableModelPart(part: never, errorDetails?: AgentErrorDetails): never {
263
+ throw createSixbError(
264
+ "internal.unexpected",
265
+ `[SixbAgent] Model trace content '${(part as { type?: unknown }).type}' is not supported.`,
266
+ errorDetails === undefined ? undefined : { details: errorDetails }
267
+ )
268
+ }
269
+
270
+ /** Expose only failures that the tool author explicitly marked as safe for the model and storage. */
271
+ export function agentToolErrorText(error: unknown): string {
272
+ return error instanceof AgentToolPublicError ? error.message : "An error occurred."
273
+ }
@@ -0,0 +1,28 @@
1
+ import type { LanguageModelTier } from "@sixb/core"
2
+
3
+ /**
4
+ * One language model on one line, as the prompt lists the models the Agent can ask or delegate
5
+ * to: under its tier, else as a list item, then its name, what the caller adds about it, and its
6
+ * description.
7
+ */
8
+ export function renderModelLine(input: {
9
+ readonly tier?: LanguageModelTier
10
+ /** How the caller names the model, such as the reference a command takes. */
11
+ readonly reference: string
12
+ readonly name?: string
13
+ /** Written as given between the name and the description, such as `(default; tools)`. */
14
+ readonly details?: string
15
+ readonly description?: string
16
+ }): string {
17
+ const name = input.name === undefined ? "" : ` ${oneLine(input.name)}`
18
+ const details = input.details === undefined ? "" : ` ${input.details}`
19
+ const description = input.description === undefined ? "" : `: ${oneLine(input.description)}`
20
+ const line = `${input.reference}${name}${details}${description}`
21
+
22
+ return input.tier === undefined ? `- ${line}` : `${input.tier.padEnd(10)}${line}`
23
+ }
24
+
25
+ // A provider's catalog text may span lines; each model keeps to one.
26
+ function oneLine(text: string): string {
27
+ return text.replace(/\s+/g, " ").trim()
28
+ }
@@ -0,0 +1,40 @@
1
+ import { createHash } from "node:crypto"
2
+ import type { ModelCachingIntent } from "@sixb/core/models"
3
+
4
+ /**
5
+ * How a conversation's model calls are cached. Its prompt is read again by its next turn, which can
6
+ * come after a human pause, so a turn asks for a long lifetime; a child does too when it can wait on
7
+ * a human decision (see {@link childCacheLifetime}). Both name the conversation, so a provider
8
+ * keeps the turn and its children on the upstream that holds what they cached: a child shares its
9
+ * whole prefix with the runs that continue it, and, in a provider sandbox, its system prompt with
10
+ * siblings given the same tools. On a computer, each child's prompt names its own thread's files.
11
+ */
12
+ export function conversationPromptCaching(input: {
13
+ readonly projectId: string
14
+ /** The conversation's thread: a child's parent thread, never its own. */
15
+ readonly threadId: string
16
+ readonly lifetime: NonNullable<ModelCachingIntent["lifetime"]>
17
+ }): ModelCachingIntent {
18
+ return { lifetime: input.lifetime, key: conversationCacheKey(input.projectId, input.threadId) }
19
+ }
20
+
21
+ /**
22
+ * A child's next read can come after a human decision when its calls can be held for approval, or
23
+ * when it continues a run that paused, as it may pause again: long. Otherwise its next read is its
24
+ * own next step, within the turn: default. A question its parent's turn leaves to the user pauses a
25
+ * child too; that is rare, so a child that can only pause on one keeps the default.
26
+ */
27
+ export function childCacheLifetime(input: {
28
+ /** Whether its run's approval mode lets every call run without asking. */
29
+ readonly runsEveryCall: boolean
30
+ /** Whether it continues a run that paused. */
31
+ readonly continues: boolean
32
+ }): NonNullable<ModelCachingIntent["lifetime"]> {
33
+ return input.runsEveryCall && !input.continues ? "default" : "long"
34
+ }
35
+
36
+ /** Opaque and stable for a conversation: the provider learns neither the project nor the thread. */
37
+ function conversationCacheKey(projectId: string, threadId: string): string {
38
+ const digest = createHash("sha256").update(projectId).update("\0").update(threadId).digest("hex")
39
+ return `sixb_${digest.slice(0, 32)}`
40
+ }
@@ -0,0 +1,21 @@
1
+ import type { AuthorizablePrincipal } from "@sixb/core"
2
+
3
+ /** Who an Agent run works for, as its prompt names them. */
4
+ export type AgentRequester =
5
+ | { readonly type: "user"; readonly id: string; readonly displayName?: string }
6
+ | { readonly type: "serviceAccount" }
7
+
8
+ /**
9
+ * The requester of an Agent execution: the user, with their display name as the run read it when
10
+ * it started, or a service account. A renamed user is named correctly from the next run.
11
+ */
12
+ export function agentRequester(
13
+ requestedBy: AuthorizablePrincipal | undefined,
14
+ names: Readonly<Record<string, string>>
15
+ ): AgentRequester | undefined {
16
+ if (requestedBy === undefined) return undefined
17
+ if (requestedBy.type === "serviceAccount") return { type: "serviceAccount" }
18
+
19
+ const displayName = names[`user:${requestedBy.id}`]?.trim()
20
+ return { type: "user", id: requestedBy.id, ...(displayName ? { displayName } : {}) }
21
+ }