Skip to content

Commit 3896f1a

Browse files
committed
refactor(import): use .parse() at boundaries instead of manual .make() calls
Parse external SDK data through branded Zod schemas (Session.Info, MessageV2.Info, MessageV2.Part) at the import boundary. This replaces manual SessionID.make/MessageID.make/PartID.make wrapping with a single .parse() call per entity, which both validates and brands in one step.
1 parent 2a4dedc commit 3896f1a

1 file changed

Lines changed: 11 additions & 19 deletions

File tree

packages/opencode/src/cli/cmd/import.ts

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import type { Argv } from "yargs"
22
import type { Session as SDKSession, Message, Part } from "@opencode-ai/sdk/v2"
33
import { Session } from "../../session"
4-
import { SessionID, MessageID, PartID } from "../../session/schema"
5-
import { WorkspaceID } from "../../control-plane/schema"
4+
import { MessageV2 } from "../../session/message-v2"
65
import { cmd } from "./cmd"
76
import { bootstrap } from "../bootstrap"
87
import { Database } from "../../storage/db"
@@ -154,20 +153,11 @@ export const ImportCommand = cmd({
154153
return
155154
}
156155

157-
const row = Session.toRow({
156+
const info = Session.Info.parse({
158157
...exportData.info,
159-
id: SessionID.make(exportData.info.id),
160-
parentID: exportData.info.parentID ? SessionID.make(exportData.info.parentID) : undefined,
161-
workspaceID: exportData.info.workspaceID ? WorkspaceID.make(exportData.info.workspaceID) : undefined,
162158
projectID: Instance.project.id,
163-
revert: exportData.info.revert
164-
? {
165-
...exportData.info.revert,
166-
messageID: MessageID.make(exportData.info.revert.messageID),
167-
partID: exportData.info.revert.partID ? PartID.make(exportData.info.revert.partID) : undefined,
168-
}
169-
: undefined,
170159
})
160+
const row = Session.toRow(info)
171161
Database.use((db) =>
172162
db
173163
.insert(SessionTable)
@@ -177,28 +167,30 @@ export const ImportCommand = cmd({
177167
)
178168

179169
for (const msg of exportData.messages) {
180-
const { id: _mid, sessionID: _msid, ...msgData } = msg.info
170+
const msgInfo = MessageV2.Info.parse(msg.info)
171+
const { id, sessionID: _, ...msgData } = msgInfo
181172
Database.use((db) =>
182173
db
183174
.insert(MessageTable)
184175
.values({
185-
id: MessageID.make(msg.info.id),
176+
id,
186177
session_id: row.id,
187-
time_created: msg.info.time?.created ?? Date.now(),
178+
time_created: msgInfo.time?.created ?? Date.now(),
188179
data: msgData,
189180
})
190181
.onConflictDoNothing()
191182
.run(),
192183
)
193184

194185
for (const part of msg.parts) {
195-
const { id: _pid, sessionID: _psid, messageID: _pmid, ...partData } = part
186+
const partInfo = MessageV2.Part.parse(part)
187+
const { id: partId, sessionID: _s, messageID, ...partData } = partInfo
196188
Database.use((db) =>
197189
db
198190
.insert(PartTable)
199191
.values({
200-
id: PartID.make(part.id),
201-
message_id: MessageID.make(msg.info.id),
192+
id: partId,
193+
message_id: messageID,
202194
session_id: row.id,
203195
data: partData,
204196
})

0 commit comments

Comments
 (0)