saeeol 1.2.0 → 1.2.1

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 (81) hide show
  1. package/package.json +14 -14
  2. package/src/session/compaction-helpers.ts +1 -169
  3. package/src/session/compaction.ts +1 -712
  4. package/src/session/core/compaction/compaction-helpers.ts +169 -0
  5. package/src/session/core/compaction/compaction.ts +712 -0
  6. package/src/session/core/compaction/overflow.ts +28 -0
  7. package/src/session/core/instruction.ts +234 -0
  8. package/src/session/core/llm.ts +504 -0
  9. package/src/session/core/network.ts +392 -0
  10. package/src/session/core/processor.ts +731 -0
  11. package/src/session/core/projectors.ts +139 -0
  12. package/src/session/core/resolve-tools.ts +241 -0
  13. package/src/session/core/retry.ts +149 -0
  14. package/src/session/core/revert.ts +173 -0
  15. package/src/session/core/run-state.ts +110 -0
  16. package/src/session/core/schema.ts +35 -0
  17. package/src/session/core/session-types.ts +160 -0
  18. package/src/session/core/session.sql.ts +124 -0
  19. package/src/session/core/session.ts +948 -0
  20. package/src/session/core/shell-exec.ts +205 -0
  21. package/src/session/core/status.ts +100 -0
  22. package/src/session/core/subtask.ts +268 -0
  23. package/src/session/core/summary.ts +173 -0
  24. package/src/session/core/system.ts +114 -0
  25. package/src/session/core/todo.ts +86 -0
  26. package/src/session/core/user-part.ts +293 -0
  27. package/src/session/instruction.ts +1 -234
  28. package/src/session/llm.ts +1 -504
  29. package/src/session/message/message-errors.ts +83 -0
  30. package/src/session/message/message-parts.ts +89 -0
  31. package/src/session/message/message-query.ts +107 -0
  32. package/src/session/message/message-transform.ts +156 -0
  33. package/src/session/message/message-types.ts +68 -0
  34. package/src/session/message/message-v2.ts +73 -0
  35. package/src/session/message/message.ts +192 -0
  36. package/src/session/message-errors.ts +1 -83
  37. package/src/session/message-parts.ts +1 -89
  38. package/src/session/message-query.ts +1 -107
  39. package/src/session/message-transform.ts +1 -156
  40. package/src/session/message-types.ts +1 -68
  41. package/src/session/message-v2.ts +1 -73
  42. package/src/session/message.ts +1 -192
  43. package/src/session/network.ts +1 -392
  44. package/src/session/overflow.ts +1 -28
  45. package/src/session/processor.ts +1 -731
  46. package/src/session/projectors.ts +2 -139
  47. package/src/session/prompt/prompt-command.ts +93 -0
  48. package/src/session/prompt/prompt-loop.ts +299 -0
  49. package/src/session/prompt/prompt-model.ts +44 -0
  50. package/src/session/prompt/prompt-reminders.ts +120 -0
  51. package/src/session/prompt/prompt-resolve.ts +42 -0
  52. package/src/session/prompt/prompt-schemas.ts +128 -0
  53. package/src/session/prompt/prompt-title.ts +55 -0
  54. package/src/session/prompt/prompt-types.ts +47 -0
  55. package/src/session/prompt/prompt-user-msg.ts +80 -0
  56. package/src/session/prompt/prompt.ts +211 -0
  57. package/src/session/prompt-command.ts +1 -93
  58. package/src/session/prompt-loop.ts +1 -299
  59. package/src/session/prompt-model.ts +1 -44
  60. package/src/session/prompt-reminders.ts +1 -120
  61. package/src/session/prompt-resolve.ts +1 -42
  62. package/src/session/prompt-schemas.ts +1 -128
  63. package/src/session/prompt-title.ts +1 -55
  64. package/src/session/prompt-types.ts +1 -47
  65. package/src/session/prompt-user-msg.ts +1 -80
  66. package/src/session/prompt.ts +1 -211
  67. package/src/session/resolve-tools.ts +1 -241
  68. package/src/session/retry.ts +1 -149
  69. package/src/session/revert.ts +1 -173
  70. package/src/session/run-state.ts +1 -110
  71. package/src/session/schema.ts +1 -35
  72. package/src/session/session-types.ts +1 -160
  73. package/src/session/session.sql.ts +1 -124
  74. package/src/session/session.ts +1 -948
  75. package/src/session/shell-exec.ts +1 -205
  76. package/src/session/status.ts +1 -100
  77. package/src/session/subtask.ts +1 -268
  78. package/src/session/summary.ts +1 -173
  79. package/src/session/system.ts +1 -114
  80. package/src/session/todo.ts +1 -86
  81. package/src/session/user-part.ts +1 -293
@@ -0,0 +1,89 @@
1
+ /** Part 스키마 + ToolState + Input 스키마 — message-v2.ts에서 분리 */
2
+
3
+ import { SessionID, MessageID, PartID } from "../core/schema"
4
+ import { LSP } from "@/lsp/lsp"
5
+ import { Snapshot } from "@/snapshot"
6
+ import { Effect, Schema, Types } from "effect"
7
+ import { zod } from "@/util/effect-zod"
8
+ import { NonNegativeInt, withStatics } from "@/util/schema"
9
+ import { APIError } from "./message-errors"
10
+ import { ModelID, ProviderID } from "@/provider/schema"
11
+ import z from "zod"
12
+
13
+ const partBase = { id: PartID, sessionID: SessionID, messageID: MessageID }
14
+
15
+ export const SnapshotPart = Schema.Struct({ ...partBase, type: Schema.Literal("snapshot"), snapshot: Schema.String }).annotate({ identifier: "SnapshotPart" }).pipe(withStatics((s) => ({ zod: zod(s) })))
16
+ export type SnapshotPart = Types.DeepMutable<Schema.Schema.Type<typeof SnapshotPart>>
17
+ export const PatchPart = Schema.Struct({ ...partBase, type: Schema.Literal("patch"), hash: Schema.String, files: Schema.Array(Schema.String) }).annotate({ identifier: "PatchPart" }).pipe(withStatics((s) => ({ zod: zod(s) })))
18
+ export type PatchPart = Types.DeepMutable<Schema.Schema.Type<typeof PatchPart>>
19
+ export const TextPart = Schema.Struct({
20
+ ...partBase, type: Schema.Literal("text"), text: Schema.String, synthetic: Schema.optional(Schema.Boolean), ignored: Schema.optional(Schema.Boolean),
21
+ time: Schema.optional(Schema.Struct({ start: NonNegativeInt, end: Schema.optional(NonNegativeInt) })), metadata: Schema.optional(Schema.Record(Schema.String, Schema.Any)),
22
+ }).annotate({ identifier: "TextPart" }).pipe(withStatics((s) => ({ zod: zod(s) })))
23
+ export type TextPart = Types.DeepMutable<Schema.Schema.Type<typeof TextPart>>
24
+ export const ReasoningPart = Schema.Struct({
25
+ ...partBase, type: Schema.Literal("reasoning"), text: Schema.String, metadata: Schema.optional(Schema.Record(Schema.String, Schema.Any)),
26
+ time: Schema.Struct({ start: NonNegativeInt, end: Schema.optional(NonNegativeInt) }),
27
+ }).annotate({ identifier: "ReasoningPart" }).pipe(withStatics((s) => ({ zod: zod(s) })))
28
+ export type ReasoningPart = Types.DeepMutable<Schema.Schema.Type<typeof ReasoningPart>>
29
+
30
+ const filePartSourceBase = { text: Schema.Struct({ value: Schema.String, start: NonNegativeInt, end: NonNegativeInt }).annotate({ identifier: "FilePartSourceText" }) }
31
+ export const FileSource = Schema.Struct({ ...filePartSourceBase, type: Schema.Literal("file"), path: Schema.String }).annotate({ identifier: "FileSource" }).pipe(withStatics((s) => ({ zod: zod(s) })))
32
+ export const SymbolSource = Schema.Struct({ ...filePartSourceBase, type: Schema.Literal("symbol"), path: Schema.String, range: LSP.Range, name: Schema.String, kind: NonNegativeInt }).annotate({ identifier: "SymbolSource" }).pipe(withStatics((s) => ({ zod: zod(s) })))
33
+ export const ResourceSource = Schema.Struct({ ...filePartSourceBase, type: Schema.Literal("resource"), clientName: Schema.String, uri: Schema.String }).annotate({ identifier: "ResourceSource" }).pipe(withStatics((s) => ({ zod: zod(s) })))
34
+
35
+ const _FilePartSource = Schema.Union([FileSource, SymbolSource, ResourceSource]).annotate({ discriminator: "type", identifier: "FilePartSource" })
36
+ export const FilePartSource = Object.assign(_FilePartSource, { zod: zod(_FilePartSource) })
37
+
38
+ export const FilePart = Schema.Struct({ ...partBase, type: Schema.Literal("file"), mime: Schema.String, filename: Schema.optional(Schema.String), url: Schema.String, source: Schema.optional(_FilePartSource) }).annotate({ identifier: "FilePart" }).pipe(withStatics((s) => ({ zod: zod(s) })))
39
+ export type FilePart = Types.DeepMutable<Schema.Schema.Type<typeof FilePart>>
40
+ export const AgentPart = Schema.Struct({ ...partBase, type: Schema.Literal("agent"), name: Schema.String, source: Schema.optional(Schema.Struct({ value: Schema.String, start: NonNegativeInt, end: NonNegativeInt })) }).annotate({ identifier: "AgentPart" }).pipe(withStatics((s) => ({ zod: zod(s) })))
41
+ export type AgentPart = Types.DeepMutable<Schema.Schema.Type<typeof AgentPart>>
42
+ export const CompactionPart = Schema.Struct({ ...partBase, type: Schema.Literal("compaction"), auto: Schema.Boolean, overflow: Schema.optional(Schema.Boolean), tail_start_id: Schema.optional(MessageID) }).annotate({ identifier: "CompactionPart" }).pipe(withStatics((s) => ({ zod: zod(s) })))
43
+ export type CompactionPart = Types.DeepMutable<Schema.Schema.Type<typeof CompactionPart>>
44
+ export const SubtaskPart = Schema.Struct({ ...partBase, type: Schema.Literal("subtask"), prompt: Schema.String, description: Schema.String, agent: Schema.String, model: Schema.optional(Schema.Struct({ providerID: ProviderID, modelID: ModelID })), command: Schema.optional(Schema.String) }).annotate({ identifier: "SubtaskPart" }).pipe(withStatics((s) => ({ zod: zod(s) })))
45
+ export type SubtaskPart = Types.DeepMutable<Schema.Schema.Type<typeof SubtaskPart>>
46
+ export const RetryPart = Schema.Struct({ ...partBase, type: Schema.Literal("retry"), attempt: NonNegativeInt, error: APIError.EffectSchema, time: Schema.Struct({ created: NonNegativeInt }) }).annotate({ identifier: "RetryPart" }).pipe(withStatics((s) => ({ zod: zod(s) })))
47
+ export type RetryPart = Omit<Types.DeepMutable<Schema.Schema.Type<typeof RetryPart>>, "error"> & { error: APIError }
48
+ export const StepStartPart = Schema.Struct({ ...partBase, type: Schema.Literal("step-start"), snapshot: Schema.optional(Schema.String) }).annotate({ identifier: "StepStartPart" }).pipe(withStatics((s) => ({ zod: zod(s) })))
49
+ export type StepStartPart = Types.DeepMutable<Schema.Schema.Type<typeof StepStartPart>>
50
+ export const StepFinishPart = Schema.Struct({
51
+ ...partBase, type: Schema.Literal("step-finish"), reason: Schema.String, snapshot: Schema.optional(Schema.String), cost: Schema.Finite,
52
+ tokens: Schema.Struct({ total: Schema.optional(NonNegativeInt), input: NonNegativeInt, output: NonNegativeInt, reasoning: NonNegativeInt, cache: Schema.Struct({ read: NonNegativeInt, write: NonNegativeInt }) }),
53
+ }).annotate({ identifier: "StepFinishPart" }).pipe(withStatics((s) => ({ zod: zod(s) })))
54
+ export type StepFinishPart = Types.DeepMutable<Schema.Schema.Type<typeof StepFinishPart>>
55
+
56
+ export const ToolStatePending = Schema.Struct({ status: Schema.Literal("pending"), input: Schema.Record(Schema.String, Schema.Any), raw: Schema.String }).annotate({ identifier: "ToolStatePending" }).pipe(withStatics((s) => ({ zod: zod(s) })))
57
+ export type ToolStatePending = Types.DeepMutable<Schema.Schema.Type<typeof ToolStatePending>>
58
+ export const ToolStateRunning = Schema.Struct({ status: Schema.Literal("running"), input: Schema.Record(Schema.String, Schema.Any), title: Schema.optional(Schema.String), metadata: Schema.optional(Schema.Record(Schema.String, Schema.Any)), time: Schema.Struct({ start: NonNegativeInt }) }).annotate({ identifier: "ToolStateRunning" }).pipe(withStatics((s) => ({ zod: zod(s) })))
59
+ export type ToolStateRunning = Types.DeepMutable<Schema.Schema.Type<typeof ToolStateRunning>>
60
+ export const ToolStateCompleted = Schema.Struct({
61
+ status: Schema.Literal("completed"), input: Schema.Record(Schema.String, Schema.Any), output: Schema.String, title: Schema.String, metadata: Schema.Record(Schema.String, Schema.Any),
62
+ time: Schema.Struct({ start: NonNegativeInt, end: NonNegativeInt, compacted: Schema.optional(NonNegativeInt) }), attachments: Schema.optional(Schema.Array(FilePart)),
63
+ }).annotate({ identifier: "ToolStateCompleted" }).pipe(withStatics((s) => ({ zod: zod(s) })))
64
+ export type ToolStateCompleted = Types.DeepMutable<Schema.Schema.Type<typeof ToolStateCompleted>>
65
+ export function truncateToolOutput(text: string, maxChars?: number) { if (!maxChars || text.length <= maxChars) return text; return `${text.slice(0, maxChars)}\n[Tool output truncated for compaction: omitted ${text.length - maxChars} chars]` }
66
+ export const ToolStateError = Schema.Struct({ status: Schema.Literal("error"), input: Schema.Record(Schema.String, Schema.Any), error: Schema.String, metadata: Schema.optional(Schema.Record(Schema.String, Schema.Any)), time: Schema.Struct({ start: NonNegativeInt, end: NonNegativeInt }) }).annotate({ identifier: "ToolStateError" }).pipe(withStatics((s) => ({ zod: zod(s) })))
67
+ export type ToolStateError = Types.DeepMutable<Schema.Schema.Type<typeof ToolStateError>>
68
+
69
+ const _ToolState = Schema.Union([ToolStatePending, ToolStateRunning, ToolStateCompleted, ToolStateError]).annotate({ discriminator: "status", identifier: "ToolState" })
70
+ export const ToolState = Object.assign(_ToolState, { zod: zod(_ToolState) as unknown as z.ZodType<ToolStatePending | ToolStateRunning | ToolStateCompleted | ToolStateError> })
71
+ export type ToolState = ToolStatePending | ToolStateRunning | ToolStateCompleted | ToolStateError
72
+ export const ToolPart = Schema.Struct({ ...partBase, type: Schema.Literal("tool"), callID: Schema.String, tool: Schema.String, state: _ToolState, metadata: Schema.optional(Schema.Record(Schema.String, Schema.Any)) }).annotate({ identifier: "ToolPart" }).pipe(withStatics((s) => ({ zod: zod(s) })))
73
+ export type ToolPart = Omit<Types.DeepMutable<Schema.Schema.Type<typeof ToolPart>>, "state"> & { state: ToolState }
74
+
75
+ // Input variants (without ambient IDs)
76
+ export const TextPartInput = Schema.Struct({ id: Schema.optional(PartID), type: Schema.Literal("text"), text: Schema.String, synthetic: Schema.optional(Schema.Boolean), ignored: Schema.optional(Schema.Boolean), time: Schema.optional(Schema.Struct({ start: NonNegativeInt, end: Schema.optional(NonNegativeInt) })), metadata: Schema.optional(Schema.Record(Schema.String, Schema.Any)) }).annotate({ identifier: "TextPartInput" }).pipe(withStatics((s) => ({ zod: zod(s) })))
77
+ export type TextPartInput = Types.DeepMutable<Schema.Schema.Type<typeof TextPartInput>>
78
+ export const FilePartInput = Schema.Struct({ id: Schema.optional(PartID), type: Schema.Literal("file"), mime: Schema.String, filename: Schema.optional(Schema.String), url: Schema.String, source: Schema.optional(_FilePartSource) }).annotate({ identifier: "FilePartInput" }).pipe(withStatics((s) => ({ zod: zod(s) })))
79
+ export type FilePartInput = Types.DeepMutable<Schema.Schema.Type<typeof FilePartInput>>
80
+ export const AgentPartInput = Schema.Struct({ id: Schema.optional(PartID), type: Schema.Literal("agent"), name: Schema.String, source: Schema.optional(Schema.Struct({ value: Schema.String, start: NonNegativeInt, end: NonNegativeInt })) }).annotate({ identifier: "AgentPartInput" }).pipe(withStatics((s) => ({ zod: zod(s) })))
81
+ export type AgentPartInput = Types.DeepMutable<Schema.Schema.Type<typeof AgentPartInput>>
82
+ export const SubtaskPartInput = Schema.Struct({ id: Schema.optional(PartID), type: Schema.Literal("subtask"), prompt: Schema.String, description: Schema.String, agent: Schema.String, model: Schema.optional(Schema.Struct({ providerID: ProviderID, modelID: ModelID })), command: Schema.optional(Schema.String) }).annotate({ identifier: "SubtaskPartInput" }).pipe(withStatics((s) => ({ zod: zod(s) })))
83
+ export type SubtaskPartInput = Types.DeepMutable<Schema.Schema.Type<typeof SubtaskPartInput>>
84
+
85
+ // Part union
86
+ const _Part = Schema.Union([TextPart, SubtaskPart, ReasoningPart, FilePart, ToolPart, StepStartPart, StepFinishPart, SnapshotPart, PatchPart, AgentPart, RetryPart, CompactionPart]).annotate({ discriminator: "type", identifier: "Part" })
87
+ export const Part = Object.assign(_Part, { zod: zod(_Part) as unknown as z.ZodType<TextPart | SubtaskPart | ReasoningPart | FilePart | ToolPart | StepStartPart | StepFinishPart | SnapshotPart | PatchPart | AgentPart | RetryPart | CompactionPart> })
88
+ export type Part = TextPart | SubtaskPart | ReasoningPart | FilePart | ToolPart | StepStartPart | StepFinishPart | SnapshotPart | PatchPart | AgentPart | RetryPart | CompactionPart
89
+ export { _Part }
@@ -0,0 +1,107 @@
1
+ import { Effect } from "effect"
2
+ import { and, desc, eq } from "drizzle-orm"
3
+ import { Database } from "@/storage/db"
4
+ import { NotFoundError } from "@/storage/storage"
5
+ import { MessageTable, PartTable, SessionTable } from "../core/session.sql"
6
+ import { SessionID, MessageID } from "../core/schema"
7
+ import type { WithParts } from "./message-types"
8
+ import { cursor } from "./message-types"
9
+ import type { Part, CompactionPart } from "./message-parts"
10
+ import { stripPartMetadata, hydrate, older, toInfo } from "./message-transform"
11
+
12
+ export function page(input: { sessionID: SessionID; limit: number; before?: string }) {
13
+ const before = input.before ? cursor.decode(input.before) : undefined
14
+ const where = before
15
+ ? and(eq(MessageTable.session_id, input.sessionID), older(before))
16
+ : eq(MessageTable.session_id, input.sessionID)
17
+ const rows = Database.use((db) =>
18
+ db
19
+ .select()
20
+ .from(MessageTable)
21
+ .where(where)
22
+ .orderBy(desc(MessageTable.time_created), desc(MessageTable.id))
23
+ .limit(input.limit + 1)
24
+ .all(),
25
+ )
26
+ if (rows.length === 0) {
27
+ const row = Database.use((db) =>
28
+ db.select({ id: SessionTable.id }).from(SessionTable).where(eq(SessionTable.id, input.sessionID)).get(),
29
+ )
30
+ if (!row) throw new NotFoundError({ message: `Session not found: ${input.sessionID}` })
31
+ return { items: [] as WithParts[], more: false }
32
+ }
33
+ const more = rows.length > input.limit
34
+ const slice = more ? rows.slice(0, input.limit) : rows
35
+ const items = hydrate(slice)
36
+ items.reverse()
37
+ const tail = slice.at(-1)
38
+ return {
39
+ items,
40
+ more,
41
+ cursor: more && tail ? cursor.encode({ id: tail.id, time: tail.time_created }) : undefined,
42
+ }
43
+ }
44
+
45
+ export function* stream(sessionID: SessionID) {
46
+ const size = 50
47
+ let before: string | undefined
48
+ while (true) {
49
+ const next = page({ sessionID, limit: size, before })
50
+ if (next.items.length === 0) break
51
+ for (let i = next.items.length - 1; i >= 0; i--) yield next.items[i]
52
+ if (!next.more || !next.cursor) break
53
+ before = next.cursor
54
+ }
55
+ }
56
+
57
+ export function parts(message_id: MessageID) {
58
+ const rows = Database.use((db) =>
59
+ db.select().from(PartTable).where(eq(PartTable.message_id, message_id)).orderBy(PartTable.id).all(),
60
+ )
61
+ return rows.map((row) =>
62
+ stripPartMetadata({ ...row.data, id: row.id, sessionID: row.session_id, messageID: row.message_id } as Part),
63
+ )
64
+ }
65
+
66
+ export function get(input: { sessionID: SessionID; messageID: MessageID }): WithParts {
67
+ const row = Database.use((db) =>
68
+ db
69
+ .select()
70
+ .from(MessageTable)
71
+ .where(and(eq(MessageTable.id, input.messageID), eq(MessageTable.session_id, input.sessionID)))
72
+ .get(),
73
+ )
74
+ if (!row) throw new NotFoundError({ message: `Message not found: ${input.messageID}` })
75
+ return { info: toInfo(row), parts: parts(input.messageID) }
76
+ }
77
+
78
+ export function filterCompacted(msgs: Iterable<WithParts>) {
79
+ const result = [] as WithParts[]
80
+ const completed = new Set<string>()
81
+ let retain: MessageID | undefined
82
+ for (const msg of msgs) {
83
+ result.push(msg)
84
+ if (retain) {
85
+ if (msg.info.id === retain) break
86
+ continue
87
+ }
88
+ if (msg.info.role === "user" && completed.has(msg.info.id)) {
89
+ const part = msg.parts.find((item): item is CompactionPart => item.type === "compaction")
90
+ if (!part) continue
91
+ if (!part.tail_start_id) break
92
+ retain = part.tail_start_id
93
+ if (msg.info.id === retain) break
94
+ continue
95
+ }
96
+ if (msg.info.role === "user" && completed.has(msg.info.id) && msg.parts.some((part) => part.type === "compaction"))
97
+ break
98
+ if (msg.info.role === "assistant" && msg.info.summary && msg.info.finish && !msg.info.error)
99
+ completed.add(msg.info.parentID)
100
+ }
101
+ result.reverse()
102
+ return result
103
+ }
104
+
105
+ export const filterCompactedEffect = Effect.fnUntraced(function* (sessionID: SessionID) {
106
+ return filterCompacted(stream(sessionID))
107
+ })
@@ -0,0 +1,156 @@
1
+ /** toModelMessages + 메타데이터 스트리핑 + DB 하이드레이션 — message-v2.ts에서 분리 */
2
+
3
+ import { convertToModelMessages, type UIMessage } from "ai"
4
+ import { Effect } from "effect"
5
+ import { and, desc, eq, inArray, lt, or } from "drizzle-orm"
6
+ import { Database } from "@/storage/db"
7
+ import { MessageTable, PartTable } from "../core/session.sql"
8
+ import { Snapshot } from "@/snapshot"
9
+ import { iife } from "@/util/iife"
10
+ import { isMedia } from "@/util/media"
11
+ import { MessageID, SessionID } from "../core/schema"
12
+ import * as EffectLogger from "@saeeol/core/effect/logger"
13
+ import type { Provider } from "@/provider/provider"
14
+ export const SYNTHETIC_ATTACHMENT_PROMPT = "Attached image(s) from tool result:"
15
+ import type { Info, User, WithParts } from "./message-types"
16
+ import type { Part, CompactionPart, ToolState, ToolStateCompleted } from "./message-parts"
17
+ import { truncateToolOutput } from "./message-parts"
18
+ import { AbortedError } from "./message-errors"
19
+
20
+ export { isMedia }
21
+
22
+ function stripPatch(value: unknown) {
23
+ if (typeof value !== "string") return undefined
24
+ if (Buffer.byteLength(value) > Snapshot.MAX_DIFF_SIZE) return undefined
25
+ return value
26
+ }
27
+ function withPatch(value: unknown) { const kept = stripPatch(value); return kept ? { patch: kept } : {} }
28
+
29
+ export function stripPartMetadata(part: Part): Part {
30
+ if (part.type !== "tool") return part
31
+ const { state } = part
32
+ if (state.status !== "completed" && state.status !== "running") return part
33
+ const meta = state.metadata
34
+ if (!meta) return part
35
+ let changed = false
36
+ let next = meta
37
+ if (meta.diff !== undefined) { const { diff, ...rest } = next; next = rest; changed = true }
38
+ if (meta.filediff) { const { before, after, patch, ...rest } = meta.filediff; next = { ...next, filediff: { ...rest, ...withPatch(patch) } }; changed = true }
39
+ if (Array.isArray(meta.files) && meta.files.length > 0) { next = { ...next, files: meta.files.map((f: Record<string, unknown>) => { const { before, after, patch, diff, ...rest } = f; const kept = stripPatch(patch) ?? stripPatch(diff); return { ...rest, ...(kept ? { patch: kept } : {}) } }) }; changed = true }
40
+ if (Array.isArray(meta.results) && meta.results.length > 0) { next = { ...next, results: meta.results.map((r: Record<string, unknown>) => { const { diff, ...rest } = r; if (!r.filediff || typeof r.filediff !== "object") return rest; const fd = r.filediff as Record<string, unknown>; const { before, after, patch, ...file } = fd; return { ...rest, filediff: { ...file, ...withPatch(patch) } } }) }; changed = true }
41
+ if (!changed) return part
42
+ return { ...part, state: { ...state, metadata: next } } as Part
43
+ }
44
+
45
+ export function stripMessageMetadata(info: Info): Info {
46
+ if (info.role !== "user") return info
47
+ const user = info as User
48
+ if (!user.summary?.diffs?.length) return info
49
+ const oversized = (d: Snapshot.FileDiff) => d.patch && Buffer.byteLength(d.patch) > Snapshot.MAX_DIFF_SIZE
50
+ if (!user.summary.diffs.some(oversized)) return info
51
+ return { ...user, summary: { ...user.summary, diffs: user.summary.diffs.map((d: Snapshot.FileDiff) => (oversized(d) ? { ...d, patch: "" } : d)) } } as Info
52
+ }
53
+
54
+ export const toInfo = (row: typeof MessageTable.$inferSelect) => stripMessageMetadata({ ...row.data, id: row.id, sessionID: row.session_id } as Info)
55
+ const toPart = (row: typeof PartTable.$inferSelect) => stripPartMetadata({ ...row.data, id: row.id, sessionID: row.session_id, messageID: row.message_id } as Part)
56
+
57
+ export const older = (row: { id: MessageID; time: number }) =>
58
+ or(lt(MessageTable.time_created, row.time), and(eq(MessageTable.time_created, row.time), lt(MessageTable.id, row.id)))
59
+
60
+ export function hydrate(rows: (typeof MessageTable.$inferSelect)[]) {
61
+ const ids = rows.map((row) => row.id)
62
+ const partByMessage = new Map<string, Part[]>()
63
+ if (ids.length > 0) {
64
+ const partRows = Database.use((db) => db.select().from(PartTable).where(inArray(PartTable.message_id, ids)).orderBy(PartTable.message_id, PartTable.id).all())
65
+ for (const row of partRows) { const next = toPart(row); const list = partByMessage.get(row.message_id); if (list) list.push(next); else partByMessage.set(row.message_id, [next]) }
66
+ }
67
+ return rows.map((row) => ({ info: toInfo(row), parts: partByMessage.get(row.id) ?? [] }))
68
+ }
69
+
70
+ function providerMeta(metadata: Record<string, any> | undefined) {
71
+ if (!metadata) return undefined
72
+ const { providerExecuted: _, ...rest } = metadata
73
+ return Object.keys(rest).length > 0 ? rest : undefined
74
+ }
75
+
76
+ export const toModelMessagesEffect = Effect.fnUntraced(function* (input: WithParts[], model: Provider.Model, options?: { stripMedia?: boolean; toolOutputMaxChars?: number }) {
77
+ const result: UIMessage[] = []
78
+ const toolNames = new Set<string>()
79
+ const supportsMediaInToolResults = (() => {
80
+ if (model.api.npm === "@ai-sdk/anthropic") return true
81
+ if (model.api.npm === "@ai-sdk/openai") return true
82
+ if (model.api.npm === "@ai-sdk/amazon-bedrock") return true
83
+ if (model.api.npm === "@ai-sdk/google-vertex/anthropic") return true
84
+ if (model.api.npm === "@ai-sdk/google") { const id = model.api.id.toLowerCase(); return id.includes("gemini-3") && !id.includes("gemini-2") }
85
+ return false
86
+ })()
87
+ const toModelOutput = (opts: { toolCallId: string; input: unknown; output: unknown }) => {
88
+ const output = opts.output
89
+ if (typeof output === "string") return { type: "text", value: output }
90
+ if (typeof output === "object") {
91
+ const o = output as { text: string; attachments?: Array<{ mime: string; url: string }> }
92
+ const attachments = (o.attachments ?? []).filter((a) => a.url.startsWith("data:") && a.url.includes(","))
93
+ return { type: "content", value: [...(o.text ? [{ type: "text", text: o.text }] : []), ...attachments.map((a) => ({ type: "media", mediaType: a.mime, data: iife(() => { const ci = a.url.indexOf(","); return ci === -1 ? a.url : a.url.slice(ci + 1) }) }))] }
94
+ }
95
+ return { type: "json", value: output as never }
96
+ }
97
+ for (const msg of input) {
98
+ if (msg.parts.length === 0) continue
99
+ if (msg.info.role === "user") {
100
+ const userMessage: UIMessage = { id: msg.info.id, role: "user", parts: [] }
101
+ result.push(userMessage)
102
+ for (const part of msg.parts) {
103
+ if (part.type === "text" && !part.ignored) userMessage.parts.push({ type: "text", text: part.text })
104
+ if (part.type === "file" && part.mime !== "text/plain" && part.mime !== "application/x-directory") {
105
+ if (options?.stripMedia && isMedia(part.mime)) userMessage.parts.push({ type: "text", text: `[Attached ${part.mime}: ${part.filename ?? "file"}]` })
106
+ else userMessage.parts.push({ type: "file", url: part.url, mediaType: part.mime, filename: part.filename })
107
+ }
108
+ if (part.type === "compaction") userMessage.parts.push({ type: "text", text: "What did we do so far?" })
109
+ if (part.type === "subtask") userMessage.parts.push({ type: "text", text: "The following tool was executed by the user" })
110
+ }
111
+ }
112
+ if (msg.info.role === "assistant") {
113
+ const differentModel = `${model.providerID}/${model.id}` !== `${msg.info.providerID}/${msg.info.modelID}`
114
+ const media: Array<{ mime: string; url: string }> = []
115
+ if (msg.info.error && !(AbortedError.isInstance(msg.info.error) && msg.parts.some((p) => p.type !== "step-start" && p.type !== "reasoning"))) continue
116
+ const assistantMessage: UIMessage = { id: msg.info.id, role: "assistant", parts: [] }
117
+ for (const part of msg.parts) {
118
+ if (part.type === "text" && !part.ignored) assistantMessage.parts.push({ type: "text", text: part.text, ...(differentModel ? {} : { providerMetadata: part.metadata }) })
119
+ if (part.type === "step-start") assistantMessage.parts.push({ type: "step-start" })
120
+ if (part.type === "tool") {
121
+ toolNames.add(part.tool)
122
+ if (part.state.status === "completed") {
123
+ const outputText = part.state.time.compacted ? "[Old tool result content cleared]" : truncateToolOutput(part.state.output, options?.toolOutputMaxChars)
124
+ const attachments = part.state.time.compacted || options?.stripMedia ? [] : (part.state.attachments ?? [])
125
+ const mediaAttachments = attachments.filter((a) => isMedia(a.mime))
126
+ const nonMediaAttachments = attachments.filter((a) => !isMedia(a.mime))
127
+ if (!supportsMediaInToolResults && mediaAttachments.length > 0) media.push(...mediaAttachments)
128
+ const finalAttachments = supportsMediaInToolResults ? attachments : nonMediaAttachments
129
+ const output = finalAttachments.length > 0 ? { text: outputText, attachments: finalAttachments } : outputText
130
+ assistantMessage.parts.push({ type: ("tool-" + part.tool) as `tool-${string}`, state: "output-available", toolCallId: part.callID, input: part.state.input, output, ...(part.metadata?.providerExecuted ? { providerExecuted: true } : {}), ...(differentModel ? {} : { callProviderMetadata: providerMeta(part.metadata) }) })
131
+ }
132
+ if (part.state.status === "error") {
133
+ const output = part.state.metadata?.interrupted === true ? part.state.metadata.output : undefined
134
+ if (typeof output === "string") assistantMessage.parts.push({ type: ("tool-" + part.tool) as `tool-${string}`, state: "output-available", toolCallId: part.callID, input: part.state.input, output, ...(part.metadata?.providerExecuted ? { providerExecuted: true } : {}), ...(differentModel ? {} : { callProviderMetadata: providerMeta(part.metadata) }) })
135
+ else assistantMessage.parts.push({ type: ("tool-" + part.tool) as `tool-${string}`, state: "output-error", toolCallId: part.callID, input: part.state.input, errorText: part.state.error, ...(part.metadata?.providerExecuted ? { providerExecuted: true } : {}), ...(differentModel ? {} : { callProviderMetadata: providerMeta(part.metadata) }) })
136
+ }
137
+ if (part.state.status === "pending" || part.state.status === "running") assistantMessage.parts.push({ type: ("tool-" + part.tool) as `tool-${string}`, state: "output-error", toolCallId: part.callID, input: part.state.input, errorText: "[Tool execution was interrupted]", ...(part.metadata?.providerExecuted ? { providerExecuted: true } : {}), ...(differentModel ? {} : { callProviderMetadata: providerMeta(part.metadata) }) })
138
+ }
139
+ if (part.type === "reasoning") {
140
+ if (differentModel) { if (part.text.trim().length > 0) assistantMessage.parts.push({ type: "text", text: part.text }); continue }
141
+ assistantMessage.parts.push({ type: "reasoning", text: part.text, providerMetadata: part.metadata })
142
+ }
143
+ }
144
+ if (assistantMessage.parts.length > 0) {
145
+ result.push(assistantMessage)
146
+ if (media.length > 0) result.push({ id: MessageID.ascending(), role: "user", parts: [{ type: "text" as const, text: SYNTHETIC_ATTACHMENT_PROMPT }, ...media.map((a) => ({ type: "file" as const, url: a.url, mediaType: a.mime }))] })
147
+ }
148
+ }
149
+ }
150
+ const tools = Object.fromEntries(Array.from(toolNames).map((n) => [n, { toModelOutput }]))
151
+ return yield* Effect.promise(() => convertToModelMessages(result.filter((m) => m.parts.some((p) => p.type !== "step-start")), { tools } as any))
152
+ })
153
+
154
+ export function toModelMessages(input: WithParts[], model: Provider.Model, options?: { stripMedia?: boolean; toolOutputMaxChars?: number }): Promise<import("ai").ModelMessage[]> {
155
+ return Effect.runPromise(toModelMessagesEffect(input, model, options).pipe(Effect.provide(EffectLogger.layer)))
156
+ }
@@ -0,0 +1,68 @@
1
+ /** User/Assistant 스키마 + Events + Cursor — message-v2.ts에서 분리 */
2
+
3
+ import { SessionID, MessageID, PartID } from "../core/schema"
4
+ import { Snapshot } from "@/snapshot"
5
+ import { SyncEvent } from "../../sync"
6
+ import { BusEvent } from "@/bus/bus-event"
7
+ import { Effect, Schema, Types } from "effect"
8
+ import { zod } from "@/util/effect-zod"
9
+ import { NonNegativeInt, withStatics } from "@/util/schema"
10
+ import { ModelID, ProviderID } from "@/provider/schema"
11
+ import { _Part } from "./message-parts"
12
+ import { AssistantErrorSchema, AssistantErrorZod, _Format } from "./message-errors"
13
+ export { AssistantErrorSchema, AssistantErrorZod, _Format, _Part as Part }
14
+ export type { AssistantError } from "./message-errors"
15
+ import z from "zod"
16
+
17
+ const messageBase = { id: MessageID, sessionID: SessionID }
18
+
19
+ export const EditorContext = Schema.Struct({
20
+ visibleFiles: Schema.optional(Schema.Array(Schema.String)), openTabs: Schema.optional(Schema.Array(Schema.String)),
21
+ activeFile: Schema.optional(Schema.String), shell: Schema.optional(Schema.String),
22
+ })
23
+ export type EditorContext = Types.DeepMutable<Schema.Schema.Type<typeof EditorContext>>
24
+
25
+ export const User = Schema.Struct({
26
+ ...messageBase, role: Schema.Literal("user"), time: Schema.Struct({ created: NonNegativeInt }), format: Schema.optional(_Format),
27
+ summary: Schema.optional(Schema.Struct({ title: Schema.optional(Schema.String), body: Schema.optional(Schema.String), diffs: Schema.Array(Snapshot.FileDiff) })),
28
+ agent: Schema.String, model: Schema.Struct({ providerID: ProviderID, modelID: ModelID, variant: Schema.optional(Schema.String) }),
29
+ system: Schema.optional(Schema.String), tools: Schema.optional(Schema.Record(Schema.String, Schema.Boolean)), editorContext: Schema.optional(EditorContext),
30
+ }).annotate({ identifier: "UserMessage" }).pipe(withStatics((s) => ({ zod: zod(s) })))
31
+ export type User = Types.DeepMutable<Schema.Schema.Type<typeof User>>
32
+
33
+ export const Assistant = Schema.Struct({
34
+ ...messageBase, role: Schema.Literal("assistant"), time: Schema.Struct({ created: NonNegativeInt, completed: Schema.optional(NonNegativeInt) }),
35
+ error: Schema.optional(AssistantErrorSchema), parentID: MessageID, modelID: ModelID, providerID: ProviderID, mode: Schema.String, agent: Schema.String,
36
+ path: Schema.Struct({ cwd: Schema.String, root: Schema.String }), summary: Schema.optional(Schema.Boolean), cost: Schema.Finite,
37
+ tokens: Schema.Struct({ total: Schema.optional(NonNegativeInt), input: NonNegativeInt, output: NonNegativeInt, reasoning: NonNegativeInt, cache: Schema.Struct({ read: NonNegativeInt, write: NonNegativeInt }) }),
38
+ structured: Schema.optional(Schema.Any), variant: Schema.optional(Schema.String), finish: Schema.optional(Schema.String),
39
+ }).annotate({ identifier: "AssistantMessage" }).pipe(withStatics((s) => ({ zod: zod(s) })))
40
+ export type Assistant = Omit<Types.DeepMutable<Schema.Schema.Type<typeof Assistant>>, "error"> & { error?: import("./message-errors").AssistantError }
41
+
42
+ const _Info = Schema.Union([User, Assistant]).annotate({ discriminator: "role", identifier: "Message" })
43
+ export const Info = Object.assign(_Info, { zod: zod(_Info) as unknown as z.ZodType<User | Assistant> })
44
+ export type Info = User | Assistant
45
+
46
+ export const WithParts = Schema.Struct({ info: _Info, parts: Schema.Array(_Part) }).pipe(withStatics((s) => ({ zod: zod(s) })))
47
+ export type WithParts = { info: Info; parts: import("./message-parts").Part[] }
48
+
49
+ const UpdatedEventSchema = Schema.Struct({ sessionID: SessionID, info: _Info })
50
+ const RemovedEventSchema = Schema.Struct({ sessionID: SessionID, messageID: MessageID })
51
+ const PartUpdatedEventSchema = Schema.Struct({ sessionID: SessionID, part: _Part, time: NonNegativeInt })
52
+ const PartRemovedEventSchema = Schema.Struct({ sessionID: SessionID, messageID: MessageID, partID: PartID })
53
+
54
+ export const Event = {
55
+ Updated: SyncEvent.define({ type: "message.updated", version: 1, aggregate: "sessionID", schema: UpdatedEventSchema }),
56
+ Removed: SyncEvent.define({ type: "message.removed", version: 1, aggregate: "sessionID", schema: RemovedEventSchema }),
57
+ PartUpdated: SyncEvent.define({ type: "message.part.updated", version: 1, aggregate: "sessionID", schema: PartUpdatedEventSchema }),
58
+ PartDelta: BusEvent.define("message.part.delta", Schema.Struct({ sessionID: SessionID, messageID: MessageID, partID: PartID, field: Schema.String, delta: Schema.String })),
59
+ PartRemoved: SyncEvent.define({ type: "message.part.removed", version: 1, aggregate: "sessionID", schema: PartRemovedEventSchema }),
60
+ }
61
+
62
+ const Cursor = Schema.Struct({ id: MessageID, time: Schema.Finite.check(Schema.isGreaterThanOrEqualTo(0)) })
63
+ type Cursor = typeof Cursor.Type
64
+ const decodeCursor = Schema.decodeUnknownSync(Cursor)
65
+ export const cursor = {
66
+ encode(input: Cursor) { return Buffer.from(JSON.stringify(input)).toString("base64url") },
67
+ decode(input: string) { return decodeCursor(JSON.parse(Buffer.from(input, "base64url").toString("utf8"))) },
68
+ }
@@ -0,0 +1,73 @@
1
+ export {
2
+ OutputLengthError,
3
+ AbortedError,
4
+ StructuredOutputError,
5
+ AuthError,
6
+ APIError,
7
+ ContextOverflowError,
8
+ OutputFormatText,
9
+ OutputFormatJsonSchema,
10
+ Format,
11
+ fromError,
12
+ } from "./message-errors"
13
+ export type { OutputFormat } from "./message-errors"
14
+
15
+ export {
16
+ SnapshotPart,
17
+ PatchPart,
18
+ TextPart,
19
+ ReasoningPart,
20
+ FileSource,
21
+ SymbolSource,
22
+ ResourceSource,
23
+ FilePartSource,
24
+ FilePart,
25
+ AgentPart,
26
+ CompactionPart,
27
+ SubtaskPart,
28
+ RetryPart,
29
+ StepStartPart,
30
+ StepFinishPart,
31
+ ToolStatePending,
32
+ ToolStateRunning,
33
+ ToolStateCompleted,
34
+ ToolStateError,
35
+ ToolState,
36
+ ToolPart,
37
+ truncateToolOutput,
38
+ TextPartInput,
39
+ FilePartInput,
40
+ AgentPartInput,
41
+ SubtaskPartInput,
42
+ Part,
43
+ } from "./message-parts"
44
+
45
+ export {
46
+ EditorContext,
47
+ User,
48
+ Assistant,
49
+ Info,
50
+ WithParts,
51
+ Event,
52
+ cursor,
53
+ } from "./message-types"
54
+
55
+ export {
56
+ SYNTHETIC_ATTACHMENT_PROMPT,
57
+ isMedia,
58
+ stripPartMetadata,
59
+ stripMessageMetadata,
60
+ toModelMessagesEffect,
61
+ toModelMessages,
62
+ } from "./message-transform"
63
+
64
+ export {
65
+ page,
66
+ stream,
67
+ parts,
68
+ get,
69
+ filterCompacted,
70
+ filterCompactedEffect,
71
+ } from "./message-query"
72
+
73
+ export * as MessageV2 from "./message-v2"