fix: align attachment storage model

This commit is contained in:
kris
2026-03-29 15:18:27 +08:00
parent 4262c8fb5c
commit c3900a11ec
2 changed files with 105 additions and 14 deletions

View File

@@ -173,6 +173,7 @@ export interface Message {
body: string;
sentAt: string;
kind?: MessageKind;
attachments?: MessageAttachment[];
forwardSource?: ForwardSource;
forwardBundle?: ForwardBundlePayload;
}
@@ -1065,7 +1066,7 @@ const initialState: BossState = {
{
account: PRIMARY_ADMIN_ACCOUNT,
mode: "server_file",
updatedAt: "2026-03-29T00:00:00+08:00",
updatedAt: nowIso(),
},
],
masterAgentTasks: [],
@@ -1894,11 +1895,32 @@ function normalizeMessage(raw: Partial<Message>): Message {
body: raw.body ?? "",
sentAt: raw.sentAt ?? nowIso(),
kind: raw.kind ?? "text",
attachments: Array.isArray(raw.attachments)
? raw.attachments.map((attachment) => normalizeMessageAttachment(attachment))
: undefined,
forwardSource: raw.forwardSource,
forwardBundle: raw.forwardBundle,
};
}
function normalizeMessageAttachment(raw: Partial<MessageAttachment>): MessageAttachment {
return {
attachmentId: raw.attachmentId ?? randomToken("att"),
fileName: raw.fileName ?? "",
mimeType: raw.mimeType ?? "application/octet-stream",
fileSizeBytes: raw.fileSizeBytes ?? 0,
attachmentKind: raw.attachmentKind ?? "binary",
storageBackend: raw.storageBackend ?? "server_file",
storagePath: raw.storagePath ?? "",
previewAvailable: raw.previewAvailable ?? false,
uploadedAt: raw.uploadedAt ?? nowIso(),
uploadedBy: raw.uploadedBy ?? "system",
analysisState: raw.analysisState ?? "not_applicable",
analysisSummary: raw.analysisSummary,
analysisCardId: raw.analysisCardId,
};
}
function normalizeAttachmentStorageConfig(
raw: Partial<UserAttachmentStorageConfig>,
fallback: UserAttachmentStorageConfig,
@@ -2661,26 +2683,15 @@ export async function readState(): Promise<BossState> {
try {
const state = normalizeState(JSON.parse(raw) as Partial<BossState>);
const normalizedText = JSON.stringify(state, null, 2);
lastPersistedStateText = normalizedText;
if (normalizedText !== raw) {
await fs.writeFile(dataFile, normalizedText, "utf8");
await fs.writeFile(backupFile, normalizedText, "utf8");
}
lastPersistedStateText = JSON.stringify(state, null, 2);
return state;
} catch {
const fallbackText =
(await fs.readFile(backupFile, "utf8").catch(() => null)) ??
lastPersistedStateText ??
JSON.stringify(syncDerivedState(cloneInitialState()), null, 2);
await fs.writeFile(dataFile, fallbackText, "utf8");
const state = normalizeState(JSON.parse(fallbackText) as Partial<BossState>);
const normalizedText = JSON.stringify(state, null, 2);
lastPersistedStateText = normalizedText;
if (normalizedText !== fallbackText) {
await fs.writeFile(dataFile, normalizedText, "utf8");
await fs.writeFile(backupFile, normalizedText, "utf8");
}
lastPersistedStateText = JSON.stringify(state, null, 2);
return state;
}
}