@link-assistant/agent 0.0.8

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 (133) hide show
  1. package/EXAMPLES.md +383 -0
  2. package/LICENSE +24 -0
  3. package/MODELS.md +95 -0
  4. package/README.md +388 -0
  5. package/TOOLS.md +134 -0
  6. package/package.json +89 -0
  7. package/src/agent/agent.ts +150 -0
  8. package/src/agent/generate.txt +75 -0
  9. package/src/auth/index.ts +64 -0
  10. package/src/bun/index.ts +96 -0
  11. package/src/bus/global.ts +10 -0
  12. package/src/bus/index.ts +119 -0
  13. package/src/cli/bootstrap.js +41 -0
  14. package/src/cli/bootstrap.ts +17 -0
  15. package/src/cli/cmd/agent.ts +165 -0
  16. package/src/cli/cmd/cmd.ts +5 -0
  17. package/src/cli/cmd/export.ts +88 -0
  18. package/src/cli/cmd/mcp.ts +80 -0
  19. package/src/cli/cmd/models.ts +58 -0
  20. package/src/cli/cmd/run.ts +359 -0
  21. package/src/cli/cmd/stats.ts +276 -0
  22. package/src/cli/error.ts +27 -0
  23. package/src/command/index.ts +73 -0
  24. package/src/command/template/initialize.txt +10 -0
  25. package/src/config/config.ts +705 -0
  26. package/src/config/markdown.ts +41 -0
  27. package/src/file/ripgrep.ts +391 -0
  28. package/src/file/time.ts +38 -0
  29. package/src/file/watcher.ts +75 -0
  30. package/src/file.ts +6 -0
  31. package/src/flag/flag.ts +19 -0
  32. package/src/format/formatter.ts +248 -0
  33. package/src/format/index.ts +137 -0
  34. package/src/global/index.ts +52 -0
  35. package/src/id/id.ts +72 -0
  36. package/src/index.js +371 -0
  37. package/src/mcp/index.ts +289 -0
  38. package/src/patch/index.ts +622 -0
  39. package/src/project/bootstrap.ts +22 -0
  40. package/src/project/instance.ts +67 -0
  41. package/src/project/project.ts +105 -0
  42. package/src/project/state.ts +65 -0
  43. package/src/provider/models-macro.ts +11 -0
  44. package/src/provider/models.ts +98 -0
  45. package/src/provider/opencode.js +47 -0
  46. package/src/provider/provider.ts +636 -0
  47. package/src/provider/transform.ts +241 -0
  48. package/src/server/project.ts +48 -0
  49. package/src/server/server.ts +249 -0
  50. package/src/session/agent.js +204 -0
  51. package/src/session/compaction.ts +249 -0
  52. package/src/session/index.ts +380 -0
  53. package/src/session/message-v2.ts +758 -0
  54. package/src/session/message.ts +189 -0
  55. package/src/session/processor.ts +356 -0
  56. package/src/session/prompt/anthropic-20250930.txt +166 -0
  57. package/src/session/prompt/anthropic.txt +105 -0
  58. package/src/session/prompt/anthropic_spoof.txt +1 -0
  59. package/src/session/prompt/beast.txt +147 -0
  60. package/src/session/prompt/build-switch.txt +5 -0
  61. package/src/session/prompt/codex.txt +318 -0
  62. package/src/session/prompt/copilot-gpt-5.txt +143 -0
  63. package/src/session/prompt/gemini.txt +155 -0
  64. package/src/session/prompt/grok-code.txt +1 -0
  65. package/src/session/prompt/plan.txt +8 -0
  66. package/src/session/prompt/polaris.txt +107 -0
  67. package/src/session/prompt/qwen.txt +109 -0
  68. package/src/session/prompt/summarize-turn.txt +5 -0
  69. package/src/session/prompt/summarize.txt +10 -0
  70. package/src/session/prompt/title.txt +25 -0
  71. package/src/session/prompt.ts +1390 -0
  72. package/src/session/retry.ts +53 -0
  73. package/src/session/revert.ts +108 -0
  74. package/src/session/status.ts +75 -0
  75. package/src/session/summary.ts +179 -0
  76. package/src/session/system.ts +138 -0
  77. package/src/session/todo.ts +36 -0
  78. package/src/snapshot/index.ts +197 -0
  79. package/src/storage/storage.ts +226 -0
  80. package/src/tool/bash.ts +193 -0
  81. package/src/tool/bash.txt +121 -0
  82. package/src/tool/batch.ts +173 -0
  83. package/src/tool/batch.txt +28 -0
  84. package/src/tool/codesearch.ts +123 -0
  85. package/src/tool/codesearch.txt +12 -0
  86. package/src/tool/edit.ts +604 -0
  87. package/src/tool/edit.txt +10 -0
  88. package/src/tool/glob.ts +65 -0
  89. package/src/tool/glob.txt +6 -0
  90. package/src/tool/grep.ts +116 -0
  91. package/src/tool/grep.txt +8 -0
  92. package/src/tool/invalid.ts +17 -0
  93. package/src/tool/ls.ts +110 -0
  94. package/src/tool/ls.txt +1 -0
  95. package/src/tool/multiedit.ts +46 -0
  96. package/src/tool/multiedit.txt +41 -0
  97. package/src/tool/patch.ts +188 -0
  98. package/src/tool/patch.txt +1 -0
  99. package/src/tool/read.ts +201 -0
  100. package/src/tool/read.txt +12 -0
  101. package/src/tool/registry.ts +87 -0
  102. package/src/tool/task.ts +126 -0
  103. package/src/tool/task.txt +60 -0
  104. package/src/tool/todo.ts +39 -0
  105. package/src/tool/todoread.txt +14 -0
  106. package/src/tool/todowrite.txt +167 -0
  107. package/src/tool/tool.ts +66 -0
  108. package/src/tool/webfetch.ts +171 -0
  109. package/src/tool/webfetch.txt +14 -0
  110. package/src/tool/websearch.ts +133 -0
  111. package/src/tool/websearch.txt +11 -0
  112. package/src/tool/write.ts +33 -0
  113. package/src/tool/write.txt +8 -0
  114. package/src/util/binary.ts +41 -0
  115. package/src/util/context.ts +25 -0
  116. package/src/util/defer.ts +12 -0
  117. package/src/util/error.ts +54 -0
  118. package/src/util/eventloop.ts +20 -0
  119. package/src/util/filesystem.ts +69 -0
  120. package/src/util/fn.ts +11 -0
  121. package/src/util/iife.ts +3 -0
  122. package/src/util/keybind.ts +79 -0
  123. package/src/util/lazy.ts +11 -0
  124. package/src/util/locale.ts +39 -0
  125. package/src/util/lock.ts +98 -0
  126. package/src/util/log.ts +177 -0
  127. package/src/util/queue.ts +19 -0
  128. package/src/util/rpc.ts +42 -0
  129. package/src/util/scrap.ts +10 -0
  130. package/src/util/signal.ts +12 -0
  131. package/src/util/timeout.ts +14 -0
  132. package/src/util/token.ts +7 -0
  133. package/src/util/wildcard.ts +54 -0
@@ -0,0 +1,758 @@
1
+ import z from "zod"
2
+ import { Bus } from "../bus"
3
+ import { NamedError } from "../util/error"
4
+ import { Message } from "./message"
5
+ import { APICallError, convertToModelMessages, LoadAPIKeyError, type ModelMessage, type UIMessage } from "ai"
6
+ import { Identifier } from "../id/id"
7
+ import { Snapshot } from "../snapshot"
8
+ import { fn } from "../util/fn"
9
+ import { Storage } from "../storage/storage"
10
+
11
+ export namespace MessageV2 {
12
+ export const OutputLengthError = NamedError.create("MessageOutputLengthError", z.object({}))
13
+ export const AbortedError = NamedError.create("MessageAbortedError", z.object({ message: z.string() }))
14
+ export const AuthError = NamedError.create(
15
+ "ProviderAuthError",
16
+ z.object({
17
+ providerID: z.string(),
18
+ message: z.string(),
19
+ }),
20
+ )
21
+ export const APIError = NamedError.create(
22
+ "APIError",
23
+ z.object({
24
+ message: z.string(),
25
+ statusCode: z.number().optional(),
26
+ isRetryable: z.boolean(),
27
+ responseHeaders: z.record(z.string(), z.string()).optional(),
28
+ responseBody: z.string().optional(),
29
+ }),
30
+ )
31
+ export type APIError = z.infer<typeof APIError.Schema>
32
+
33
+ const PartBase = z.object({
34
+ id: z.string(),
35
+ sessionID: z.string(),
36
+ messageID: z.string(),
37
+ })
38
+
39
+ export const SnapshotPart = PartBase.extend({
40
+ type: z.literal("snapshot"),
41
+ snapshot: z.string(),
42
+ }).meta({
43
+ ref: "SnapshotPart",
44
+ })
45
+ export type SnapshotPart = z.infer<typeof SnapshotPart>
46
+
47
+ export const PatchPart = PartBase.extend({
48
+ type: z.literal("patch"),
49
+ hash: z.string(),
50
+ files: z.string().array(),
51
+ }).meta({
52
+ ref: "PatchPart",
53
+ })
54
+ export type PatchPart = z.infer<typeof PatchPart>
55
+
56
+ export const TextPart = PartBase.extend({
57
+ type: z.literal("text"),
58
+ text: z.string(),
59
+ synthetic: z.boolean().optional(),
60
+ time: z
61
+ .object({
62
+ start: z.number(),
63
+ end: z.number().optional(),
64
+ })
65
+ .optional(),
66
+ metadata: z.record(z.string(), z.any()).optional(),
67
+ }).meta({
68
+ ref: "TextPart",
69
+ })
70
+ export type TextPart = z.infer<typeof TextPart>
71
+
72
+ export const ReasoningPart = PartBase.extend({
73
+ type: z.literal("reasoning"),
74
+ text: z.string(),
75
+ metadata: z.record(z.string(), z.any()).optional(),
76
+ time: z.object({
77
+ start: z.number(),
78
+ end: z.number().optional(),
79
+ }),
80
+ }).meta({
81
+ ref: "ReasoningPart",
82
+ })
83
+ export type ReasoningPart = z.infer<typeof ReasoningPart>
84
+
85
+ const FilePartSourceBase = z.object({
86
+ text: z
87
+ .object({
88
+ value: z.string(),
89
+ start: z.number().int(),
90
+ end: z.number().int(),
91
+ })
92
+ .meta({
93
+ ref: "FilePartSourceText",
94
+ }),
95
+ })
96
+
97
+ export const FileSource = FilePartSourceBase.extend({
98
+ type: z.literal("file"),
99
+ path: z.string(),
100
+ }).meta({
101
+ ref: "FileSource",
102
+ })
103
+
104
+ export const SymbolSource = FilePartSourceBase.extend({
105
+ type: z.literal("symbol"),
106
+ path: z.string(),
107
+ range: z.object({
108
+ start: z.object({ line: z.number(), character: z.number() }),
109
+ end: z.object({ line: z.number(), character: z.number() }),
110
+ }),
111
+ name: z.string(),
112
+ kind: z.number().int(),
113
+ }).meta({
114
+ ref: "SymbolSource",
115
+ })
116
+
117
+ export const FilePartSource = z.discriminatedUnion("type", [FileSource, SymbolSource]).meta({
118
+ ref: "FilePartSource",
119
+ })
120
+
121
+ export const FilePart = PartBase.extend({
122
+ type: z.literal("file"),
123
+ mime: z.string(),
124
+ filename: z.string().optional(),
125
+ url: z.string(),
126
+ source: FilePartSource.optional(),
127
+ }).meta({
128
+ ref: "FilePart",
129
+ })
130
+ export type FilePart = z.infer<typeof FilePart>
131
+
132
+ export const AgentPart = PartBase.extend({
133
+ type: z.literal("agent"),
134
+ name: z.string(),
135
+ source: z
136
+ .object({
137
+ value: z.string(),
138
+ start: z.number().int(),
139
+ end: z.number().int(),
140
+ })
141
+ .optional(),
142
+ }).meta({
143
+ ref: "AgentPart",
144
+ })
145
+ export type AgentPart = z.infer<typeof AgentPart>
146
+
147
+ export const CompactionPart = PartBase.extend({
148
+ type: z.literal("compaction"),
149
+ }).meta({
150
+ ref: "CompactionPart",
151
+ })
152
+ export type CompactionPart = z.infer<typeof CompactionPart>
153
+
154
+ export const SubtaskPart = PartBase.extend({
155
+ type: z.literal("subtask"),
156
+ prompt: z.string(),
157
+ description: z.string(),
158
+ agent: z.string(),
159
+ })
160
+ export type SubtaskPart = z.infer<typeof SubtaskPart>
161
+
162
+ export const RetryPart = PartBase.extend({
163
+ type: z.literal("retry"),
164
+ attempt: z.number(),
165
+ error: APIError.Schema,
166
+ time: z.object({
167
+ created: z.number(),
168
+ }),
169
+ }).meta({
170
+ ref: "RetryPart",
171
+ })
172
+ export type RetryPart = z.infer<typeof RetryPart>
173
+
174
+ export const StepStartPart = PartBase.extend({
175
+ type: z.literal("step-start"),
176
+ snapshot: z.string().optional(),
177
+ }).meta({
178
+ ref: "StepStartPart",
179
+ })
180
+ export type StepStartPart = z.infer<typeof StepStartPart>
181
+
182
+ export const StepFinishPart = PartBase.extend({
183
+ type: z.literal("step-finish"),
184
+ reason: z.string(),
185
+ snapshot: z.string().optional(),
186
+ cost: z.number(),
187
+ tokens: z.object({
188
+ input: z.number(),
189
+ output: z.number(),
190
+ reasoning: z.number(),
191
+ cache: z.object({
192
+ read: z.number(),
193
+ write: z.number(),
194
+ }),
195
+ }),
196
+ }).meta({
197
+ ref: "StepFinishPart",
198
+ })
199
+ export type StepFinishPart = z.infer<typeof StepFinishPart>
200
+
201
+ export const ToolStatePending = z
202
+ .object({
203
+ status: z.literal("pending"),
204
+ input: z.record(z.string(), z.any()),
205
+ raw: z.string(),
206
+ })
207
+ .meta({
208
+ ref: "ToolStatePending",
209
+ })
210
+
211
+ export type ToolStatePending = z.infer<typeof ToolStatePending>
212
+
213
+ export const ToolStateRunning = z
214
+ .object({
215
+ status: z.literal("running"),
216
+ input: z.record(z.string(), z.any()),
217
+ title: z.string().optional(),
218
+ metadata: z.record(z.string(), z.any()).optional(),
219
+ time: z.object({
220
+ start: z.number(),
221
+ }),
222
+ })
223
+ .meta({
224
+ ref: "ToolStateRunning",
225
+ })
226
+ export type ToolStateRunning = z.infer<typeof ToolStateRunning>
227
+
228
+ export const ToolStateCompleted = z
229
+ .object({
230
+ status: z.literal("completed"),
231
+ input: z.record(z.string(), z.any()),
232
+ output: z.string(),
233
+ title: z.string(),
234
+ metadata: z.record(z.string(), z.any()),
235
+ time: z.object({
236
+ start: z.number(),
237
+ end: z.number(),
238
+ compacted: z.number().optional(),
239
+ }),
240
+ attachments: FilePart.array().optional(),
241
+ })
242
+ .meta({
243
+ ref: "ToolStateCompleted",
244
+ })
245
+ export type ToolStateCompleted = z.infer<typeof ToolStateCompleted>
246
+
247
+ export const ToolStateError = z
248
+ .object({
249
+ status: z.literal("error"),
250
+ input: z.record(z.string(), z.any()),
251
+ error: z.string(),
252
+ metadata: z.record(z.string(), z.any()).optional(),
253
+ time: z.object({
254
+ start: z.number(),
255
+ end: z.number(),
256
+ }),
257
+ })
258
+ .meta({
259
+ ref: "ToolStateError",
260
+ })
261
+ export type ToolStateError = z.infer<typeof ToolStateError>
262
+
263
+ export const ToolState = z
264
+ .discriminatedUnion("status", [ToolStatePending, ToolStateRunning, ToolStateCompleted, ToolStateError])
265
+ .meta({
266
+ ref: "ToolState",
267
+ })
268
+
269
+ export const ToolPart = PartBase.extend({
270
+ type: z.literal("tool"),
271
+ callID: z.string(),
272
+ tool: z.string(),
273
+ state: ToolState,
274
+ metadata: z.record(z.string(), z.any()).optional(),
275
+ }).meta({
276
+ ref: "ToolPart",
277
+ })
278
+ export type ToolPart = z.infer<typeof ToolPart>
279
+
280
+ const Base = z.object({
281
+ id: z.string(),
282
+ sessionID: z.string(),
283
+ })
284
+
285
+ export const User = Base.extend({
286
+ role: z.literal("user"),
287
+ time: z.object({
288
+ created: z.number(),
289
+ }),
290
+ summary: z
291
+ .object({
292
+ title: z.string().optional(),
293
+ body: z.string().optional(),
294
+ diffs: Snapshot.FileDiff.array(),
295
+ })
296
+ .optional(),
297
+ agent: z.string(),
298
+ model: z.object({
299
+ providerID: z.string(),
300
+ modelID: z.string(),
301
+ }),
302
+ system: z.string().optional(),
303
+ appendSystem: z.string().optional(),
304
+ tools: z.record(z.string(), z.boolean()).optional(),
305
+ }).meta({
306
+ ref: "UserMessage",
307
+ })
308
+ export type User = z.infer<typeof User>
309
+
310
+ export const Part = z
311
+ .discriminatedUnion("type", [
312
+ TextPart,
313
+ SubtaskPart,
314
+ ReasoningPart,
315
+ FilePart,
316
+ ToolPart,
317
+ StepStartPart,
318
+ StepFinishPart,
319
+ SnapshotPart,
320
+ PatchPart,
321
+ AgentPart,
322
+ RetryPart,
323
+ CompactionPart,
324
+ ])
325
+ .meta({
326
+ ref: "Part",
327
+ })
328
+ export type Part = z.infer<typeof Part>
329
+
330
+ export const Assistant = Base.extend({
331
+ role: z.literal("assistant"),
332
+ time: z.object({
333
+ created: z.number(),
334
+ completed: z.number().optional(),
335
+ }),
336
+ error: z
337
+ .discriminatedUnion("name", [
338
+ AuthError.Schema,
339
+ NamedError.Unknown.Schema,
340
+ OutputLengthError.Schema,
341
+ AbortedError.Schema,
342
+ APIError.Schema,
343
+ ])
344
+ .optional(),
345
+ parentID: z.string(),
346
+ modelID: z.string(),
347
+ providerID: z.string(),
348
+ mode: z.string(),
349
+ path: z.object({
350
+ cwd: z.string(),
351
+ root: z.string(),
352
+ }),
353
+ summary: z.boolean().optional(),
354
+ cost: z.number(),
355
+ tokens: z.object({
356
+ input: z.number(),
357
+ output: z.number(),
358
+ reasoning: z.number(),
359
+ cache: z.object({
360
+ read: z.number(),
361
+ write: z.number(),
362
+ }),
363
+ }),
364
+ finish: z.string().optional(),
365
+ }).meta({
366
+ ref: "AssistantMessage",
367
+ })
368
+ export type Assistant = z.infer<typeof Assistant>
369
+
370
+ export const Info = z.discriminatedUnion("role", [User, Assistant]).meta({
371
+ ref: "Message",
372
+ })
373
+ export type Info = z.infer<typeof Info>
374
+
375
+ export const Event = {
376
+ Updated: Bus.event(
377
+ "message.updated",
378
+ z.object({
379
+ info: Info,
380
+ }),
381
+ ),
382
+ Removed: Bus.event(
383
+ "message.removed",
384
+ z.object({
385
+ sessionID: z.string(),
386
+ messageID: z.string(),
387
+ }),
388
+ ),
389
+ PartUpdated: Bus.event(
390
+ "message.part.updated",
391
+ z.object({
392
+ part: Part,
393
+ delta: z.string().optional(),
394
+ }),
395
+ ),
396
+ PartRemoved: Bus.event(
397
+ "message.part.removed",
398
+ z.object({
399
+ sessionID: z.string(),
400
+ messageID: z.string(),
401
+ partID: z.string(),
402
+ }),
403
+ ),
404
+ }
405
+
406
+ export const WithParts = z.object({
407
+ info: Info,
408
+ parts: z.array(Part),
409
+ })
410
+ export type WithParts = z.infer<typeof WithParts>
411
+
412
+ export function fromV1(v1: Message.Info) {
413
+ if (v1.role === "assistant") {
414
+ const info: Assistant = {
415
+ id: v1.id,
416
+ parentID: "",
417
+ sessionID: v1.metadata.sessionID,
418
+ role: "assistant",
419
+ time: {
420
+ created: v1.metadata.time.created,
421
+ completed: v1.metadata.time.completed,
422
+ },
423
+ cost: v1.metadata.assistant!.cost,
424
+ path: v1.metadata.assistant!.path,
425
+ summary: v1.metadata.assistant!.summary,
426
+ tokens: v1.metadata.assistant!.tokens,
427
+ modelID: v1.metadata.assistant!.modelID,
428
+ providerID: v1.metadata.assistant!.providerID,
429
+ mode: "build",
430
+ error: v1.metadata.error,
431
+ }
432
+ const parts = v1.parts.flatMap((part): Part[] => {
433
+ const base = {
434
+ id: Identifier.ascending("part"),
435
+ messageID: v1.id,
436
+ sessionID: v1.metadata.sessionID,
437
+ }
438
+ if (part.type === "text") {
439
+ return [
440
+ {
441
+ ...base,
442
+ type: "text",
443
+ text: part.text,
444
+ },
445
+ ]
446
+ }
447
+ if (part.type === "step-start") {
448
+ return [
449
+ {
450
+ ...base,
451
+ type: "step-start",
452
+ },
453
+ ]
454
+ }
455
+ if (part.type === "tool-invocation") {
456
+ return [
457
+ {
458
+ ...base,
459
+ type: "tool",
460
+ callID: part.toolInvocation.toolCallId,
461
+ tool: part.toolInvocation.toolName,
462
+ state: (() => {
463
+ if (part.toolInvocation.state === "partial-call") {
464
+ return {
465
+ status: "pending",
466
+ input: {},
467
+ raw: "",
468
+ }
469
+ }
470
+
471
+ const { title, time, ...metadata } = v1.metadata.tool[part.toolInvocation.toolCallId] ?? {}
472
+ if (part.toolInvocation.state === "call") {
473
+ return {
474
+ status: "running",
475
+ input: part.toolInvocation.args,
476
+ time: {
477
+ start: time?.start,
478
+ },
479
+ }
480
+ }
481
+
482
+ if (part.toolInvocation.state === "result") {
483
+ return {
484
+ status: "completed",
485
+ input: part.toolInvocation.args,
486
+ output: part.toolInvocation.result,
487
+ title,
488
+ time,
489
+ metadata,
490
+ }
491
+ }
492
+ throw new Error("unknown tool invocation state")
493
+ })(),
494
+ },
495
+ ]
496
+ }
497
+ return []
498
+ })
499
+ return {
500
+ info,
501
+ parts,
502
+ }
503
+ }
504
+
505
+ if (v1.role === "user") {
506
+ const info: User = {
507
+ id: v1.id,
508
+ sessionID: v1.metadata.sessionID,
509
+ role: "user",
510
+ time: {
511
+ created: v1.metadata.time.created,
512
+ },
513
+ agent: "build",
514
+ model: {
515
+ providerID: "opencode",
516
+ modelID: "opencode",
517
+ },
518
+ }
519
+ const parts = v1.parts.flatMap((part): Part[] => {
520
+ const base = {
521
+ id: Identifier.ascending("part"),
522
+ messageID: v1.id,
523
+ sessionID: v1.metadata.sessionID,
524
+ }
525
+ if (part.type === "text") {
526
+ return [
527
+ {
528
+ ...base,
529
+ type: "text",
530
+ text: part.text,
531
+ },
532
+ ]
533
+ }
534
+ if (part.type === "file") {
535
+ return [
536
+ {
537
+ ...base,
538
+ type: "file",
539
+ mime: part.mediaType,
540
+ filename: part.filename,
541
+ url: part.url,
542
+ },
543
+ ]
544
+ }
545
+ return []
546
+ })
547
+ return { info, parts }
548
+ }
549
+
550
+ throw new Error("unknown message type")
551
+ }
552
+
553
+ export function toModelMessage(
554
+ input: {
555
+ info: Info
556
+ parts: Part[]
557
+ }[],
558
+ ): ModelMessage[] {
559
+ const result: UIMessage[] = []
560
+
561
+ for (const msg of input) {
562
+ if (msg.parts.length === 0) continue
563
+
564
+ if (msg.info.role === "user") {
565
+ const userMessage: UIMessage = {
566
+ id: msg.info.id,
567
+ role: "user",
568
+ parts: [],
569
+ }
570
+ result.push(userMessage)
571
+ for (const part of msg.parts) {
572
+ if (part.type === "text")
573
+ userMessage.parts.push({
574
+ type: "text",
575
+ text: part.text,
576
+ })
577
+ // text/plain and directory files are converted into text parts, ignore them
578
+ if (part.type === "file" && part.mime !== "text/plain" && part.mime !== "application/x-directory")
579
+ userMessage.parts.push({
580
+ type: "file",
581
+ url: part.url,
582
+ mediaType: part.mime,
583
+ filename: part.filename,
584
+ })
585
+
586
+ if (part.type === "compaction") {
587
+ userMessage.parts.push({
588
+ type: "text",
589
+ text: "What did we do so far?",
590
+ })
591
+ }
592
+ if (part.type === "subtask") {
593
+ userMessage.parts.push({
594
+ type: "text",
595
+ text: "The following tool was executed by the user",
596
+ })
597
+ }
598
+ }
599
+ }
600
+
601
+ if (msg.info.role === "assistant") {
602
+ const assistantMessage: UIMessage = {
603
+ id: msg.info.id,
604
+ role: "assistant",
605
+ parts: [],
606
+ }
607
+ result.push(assistantMessage)
608
+ for (const part of msg.parts) {
609
+ if (part.type === "text")
610
+ assistantMessage.parts.push({
611
+ type: "text",
612
+ text: part.text,
613
+ providerMetadata: part.metadata,
614
+ })
615
+ if (part.type === "step-start")
616
+ assistantMessage.parts.push({
617
+ type: "step-start",
618
+ })
619
+ if (part.type === "tool") {
620
+ if (part.state.status === "completed") {
621
+ if (part.state.attachments?.length) {
622
+ result.push({
623
+ id: Identifier.ascending("message"),
624
+ role: "user",
625
+ parts: [
626
+ {
627
+ type: "text",
628
+ text: `Tool ${part.tool} returned an attachment:`,
629
+ },
630
+ ...part.state.attachments.map((attachment) => ({
631
+ type: "file" as const,
632
+ url: attachment.url,
633
+ mediaType: attachment.mime,
634
+ filename: attachment.filename,
635
+ })),
636
+ ],
637
+ })
638
+ }
639
+ assistantMessage.parts.push({
640
+ type: ("tool-" + part.tool) as `tool-${string}`,
641
+ state: "output-available",
642
+ toolCallId: part.callID,
643
+ input: part.state.input,
644
+ output: part.state.time.compacted ? "[Old tool result content cleared]" : part.state.output,
645
+ callProviderMetadata: part.metadata,
646
+ })
647
+ }
648
+ if (part.state.status === "error")
649
+ assistantMessage.parts.push({
650
+ type: ("tool-" + part.tool) as `tool-${string}`,
651
+ state: "output-error",
652
+ toolCallId: part.callID,
653
+ input: part.state.input,
654
+ errorText: part.state.error,
655
+ callProviderMetadata: part.metadata,
656
+ })
657
+ }
658
+ if (part.type === "reasoning") {
659
+ assistantMessage.parts.push({
660
+ type: "reasoning",
661
+ text: part.text,
662
+ providerMetadata: part.metadata,
663
+ })
664
+ }
665
+ }
666
+ }
667
+ }
668
+
669
+ return convertToModelMessages(result)
670
+ }
671
+
672
+ export const stream = fn(Identifier.schema("session"), async function* (sessionID) {
673
+ const list = await Array.fromAsync(await Storage.list(["message", sessionID]))
674
+ for (let i = list.length - 1; i >= 0; i--) {
675
+ yield await get({
676
+ sessionID,
677
+ messageID: list[i][2],
678
+ })
679
+ }
680
+ })
681
+
682
+ export const parts = fn(Identifier.schema("message"), async (messageID) => {
683
+ const result = [] as MessageV2.Part[]
684
+ for (const item of await Storage.list(["part", messageID])) {
685
+ const read = await Storage.read<MessageV2.Part>(item)
686
+ result.push(read)
687
+ }
688
+ result.sort((a, b) => (a.id > b.id ? 1 : -1))
689
+ return result
690
+ })
691
+
692
+ export const get = fn(
693
+ z.object({
694
+ sessionID: Identifier.schema("session"),
695
+ messageID: Identifier.schema("message"),
696
+ }),
697
+ async (input) => {
698
+ return {
699
+ info: await Storage.read<MessageV2.Info>(["message", input.sessionID, input.messageID]),
700
+ parts: await parts(input.messageID),
701
+ }
702
+ },
703
+ )
704
+
705
+ export async function filterCompacted(stream: AsyncIterable<MessageV2.WithParts>) {
706
+ const result = [] as MessageV2.WithParts[]
707
+ const completed = new Set<string>()
708
+ for await (const msg of stream) {
709
+ result.push(msg)
710
+ if (
711
+ msg.info.role === "user" &&
712
+ completed.has(msg.info.id) &&
713
+ msg.parts.some((part) => part.type === "compaction")
714
+ )
715
+ break
716
+ if (msg.info.role === "assistant" && msg.info.summary && msg.info.finish) completed.add(msg.info.parentID)
717
+ }
718
+ result.reverse()
719
+ return result
720
+ }
721
+
722
+ export function fromError(e: unknown, ctx: { providerID: string }) {
723
+ switch (true) {
724
+ case e instanceof DOMException && e.name === "AbortError":
725
+ return new MessageV2.AbortedError(
726
+ { message: e.message },
727
+ {
728
+ cause: e,
729
+ },
730
+ ).toObject()
731
+ case MessageV2.OutputLengthError.isInstance(e):
732
+ return e
733
+ case LoadAPIKeyError.isInstance(e):
734
+ return new MessageV2.AuthError(
735
+ {
736
+ providerID: ctx.providerID,
737
+ message: e.message,
738
+ },
739
+ { cause: e },
740
+ ).toObject()
741
+ case APICallError.isInstance(e):
742
+ return new MessageV2.APIError(
743
+ {
744
+ message: e.message,
745
+ statusCode: e.statusCode,
746
+ isRetryable: e.isRetryable,
747
+ responseHeaders: e.responseHeaders,
748
+ responseBody: e.responseBody,
749
+ },
750
+ { cause: e },
751
+ ).toObject()
752
+ case e instanceof Error:
753
+ return new NamedError.Unknown({ message: e.toString() }, { cause: e }).toObject()
754
+ default:
755
+ return new NamedError.Unknown({ message: JSON.stringify(e) }, { cause: e })
756
+ }
757
+ }
758
+ }