mirror of
https://gitea.toothfairyai.com/ToothFairyAI/tf_code.git
synced 2026-04-27 11:04:39 +00:00
refactor(import): use .parse() at boundaries instead of manual .make() (#17106)
This commit is contained in:
@@ -1,8 +1,7 @@
|
|||||||
import type { Argv } from "yargs"
|
import type { Argv } from "yargs"
|
||||||
import type { Session as SDKSession, Message, Part } from "@opencode-ai/sdk/v2"
|
import type { Session as SDKSession, Message, Part } from "@opencode-ai/sdk/v2"
|
||||||
import { Session } from "../../session"
|
import { Session } from "../../session"
|
||||||
import { SessionID, MessageID, PartID } from "../../session/schema"
|
import { MessageV2 } from "../../session/message-v2"
|
||||||
import { WorkspaceID } from "../../control-plane/schema"
|
|
||||||
import { cmd } from "./cmd"
|
import { cmd } from "./cmd"
|
||||||
import { bootstrap } from "../bootstrap"
|
import { bootstrap } from "../bootstrap"
|
||||||
import { Database } from "../../storage/db"
|
import { Database } from "../../storage/db"
|
||||||
@@ -154,20 +153,11 @@ export const ImportCommand = cmd({
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const row = Session.toRow({
|
const info = Session.Info.parse({
|
||||||
...exportData.info,
|
...exportData.info,
|
||||||
id: SessionID.make(exportData.info.id),
|
|
||||||
parentID: exportData.info.parentID ? SessionID.make(exportData.info.parentID) : undefined,
|
|
||||||
workspaceID: exportData.info.workspaceID ? WorkspaceID.make(exportData.info.workspaceID) : undefined,
|
|
||||||
projectID: Instance.project.id,
|
projectID: Instance.project.id,
|
||||||
revert: exportData.info.revert
|
|
||||||
? {
|
|
||||||
...exportData.info.revert,
|
|
||||||
messageID: MessageID.make(exportData.info.revert.messageID),
|
|
||||||
partID: exportData.info.revert.partID ? PartID.make(exportData.info.revert.partID) : undefined,
|
|
||||||
}
|
|
||||||
: undefined,
|
|
||||||
})
|
})
|
||||||
|
const row = Session.toRow(info)
|
||||||
Database.use((db) =>
|
Database.use((db) =>
|
||||||
db
|
db
|
||||||
.insert(SessionTable)
|
.insert(SessionTable)
|
||||||
@@ -177,14 +167,15 @@ export const ImportCommand = cmd({
|
|||||||
)
|
)
|
||||||
|
|
||||||
for (const msg of exportData.messages) {
|
for (const msg of exportData.messages) {
|
||||||
const { id: _mid, sessionID: _msid, ...msgData } = msg.info
|
const msgInfo = MessageV2.Info.parse(msg.info)
|
||||||
|
const { id, sessionID: _, ...msgData } = msgInfo
|
||||||
Database.use((db) =>
|
Database.use((db) =>
|
||||||
db
|
db
|
||||||
.insert(MessageTable)
|
.insert(MessageTable)
|
||||||
.values({
|
.values({
|
||||||
id: MessageID.make(msg.info.id),
|
id,
|
||||||
session_id: row.id,
|
session_id: row.id,
|
||||||
time_created: msg.info.time?.created ?? Date.now(),
|
time_created: msgInfo.time?.created ?? Date.now(),
|
||||||
data: msgData,
|
data: msgData,
|
||||||
})
|
})
|
||||||
.onConflictDoNothing()
|
.onConflictDoNothing()
|
||||||
@@ -192,13 +183,14 @@ export const ImportCommand = cmd({
|
|||||||
)
|
)
|
||||||
|
|
||||||
for (const part of msg.parts) {
|
for (const part of msg.parts) {
|
||||||
const { id: _pid, sessionID: _psid, messageID: _pmid, ...partData } = part
|
const partInfo = MessageV2.Part.parse(part)
|
||||||
|
const { id: partId, sessionID: _s, messageID, ...partData } = partInfo
|
||||||
Database.use((db) =>
|
Database.use((db) =>
|
||||||
db
|
db
|
||||||
.insert(PartTable)
|
.insert(PartTable)
|
||||||
.values({
|
.values({
|
||||||
id: PartID.make(part.id),
|
id: partId,
|
||||||
message_id: MessageID.make(msg.info.id),
|
message_id: messageID,
|
||||||
session_id: row.id,
|
session_id: row.id,
|
||||||
data: partData,
|
data: partData,
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user