DConversation: converters update

This commit is contained in:
Enrico Ros
2024-12-02 08:47:26 -08:00
parent fa8501b73d
commit 0a27544db3
+106 -106
View File
@@ -127,112 +127,6 @@ export namespace V4ToHeadConverters {
}
export namespace DataAtRestV1 {
/// Rest V1 -> Head ///
/** Used by: AppLinkChat.fetchStoredChatV1, trade_client.loadSingleChatFromAtRestV1 */
export function recreateConversation(chatJV1: RestChatJsonV1 & { tokenCount?: number }): DConversation | null {
// sanity check
if (!chatJV1 || !chatJV1.id || !chatJV1.messages) {
console.warn('createDConversationFromJsonV1: invalid conversation json', chatJV1);
return null;
}
// RestChatJsonV1 could be EITHER a V3 or V4/Head message - uncomment below to see little difference
// const sentinel1: RestChatJsonV1 = {} as DConversation;
// const sentinel1: DConversation = {} as RestChatJsonV1;
// TODO: probably better to fork off the saving already, so imports are only V3's
// heuristic to find out if we're dealing with a V3 or V4 message
// const v3Count = chatJV1.messages.filter((m) => 'text' in m).length;
// const v4Count = chatJV1.messages.filter((m) => 'fragments' in m).length;
// if (v3Count > v4Count) {
// we are dealing with a V3 message
// ...
// }
return V3StoreDataToHead.recreateConversation(chatJV1);
}
/** Used by: trade_client.loadConversationsFromAtRestV1 */
export function recreateFolders(part: RestFolderJsonV1[]): DFolder[] {
return (part || []).map(_recreateFolder).filter((f) => f) as DFolder[];
}
function _recreateFolder(part: RestFolderJsonV1): DFolder | null {
// sanity check
if (!part || !part.id || !part.title || !part.conversationIds) {
console.warn('createFolderFromJsonV1: invalid folder json', part);
return null;
}
return {
id: part.id,
title: part.title,
conversationIds: part.conversationIds,
color: part.color,
};
}
/// Head -> Rest V1 ///
/** Used by: downloadAllJsonV1B */
export function formatAllToJsonV1B(conversations: DConversation[], modelSources: DModelsService[], folders: DFolder[], enableFolders: boolean): RestAllJsonV1B {
return {
conversations: (conversations || []).map(formatChatToJsonV1),
models: { sources: modelSources },
folders: { folders, enableFolders },
};
}
/** Used by: ^, downloadSingleChat, ChatLinkExport.handleCreate */
export function formatChatToJsonV1(ec: DConversation): RestChatJsonV1 {
return {
id: ec.id,
messages: ec.messages,
systemPurposeId: ec.systemPurposeId,
userTitle: ec.userTitle,
autoTitle: ec.autoTitle,
created: ec.created,
updated: ec.updated,
};
}
/// STORED TYPES definitions ///
/// do not change(!) these - consider people's backups and stored data
export type RestAllJsonV1B = {
conversations: RestChatJsonV1[];
models: { sources: DModelsService[] };
folders?: { folders: RestFolderJsonV1[]; enableFolders: boolean };
}
export type RestChatJsonV1 = {
id: string;
messages: (DMessage | V3StoreDataToHead.ImportMessageV3)[];
systemPurposeId: string;
userTitle?: string;
autoTitle?: string;
created: number;
updated: number | null;
}
type RestFolderJsonV1 = {
id: string;
title: string;
conversationIds: DConversationId[];
color?: string; // Optional color property
}
}
export namespace V3StoreDataToHead {
/** Used by: chat-store.migrate() for direct data conversion to V4 from V3 */
@@ -342,3 +236,109 @@ export namespace V3StoreDataToHead {
}
}
export namespace DataAtRestV1 {
/// Rest V1 -> Head ///
/** Used by: AppLinkChat.fetchStoredChatV1, trade_client.loadSingleChatFromAtRestV1 */
export function recreateConversation(chatJV1: RestChatJsonV1 & { tokenCount?: number }): DConversation | null {
// sanity check
if (!chatJV1 || !chatJV1.id || !chatJV1.messages) {
console.warn('createDConversationFromJsonV1: invalid conversation json', chatJV1);
return null;
}
// RestChatJsonV1 could be EITHER a V3 or V4/Head message - uncomment below to see little difference
// const sentinel1: RestChatJsonV1 = {} as DConversation;
// const sentinel1: DConversation = {} as RestChatJsonV1;
// TODO: probably better to fork off the saving already, so imports are only V3's
// heuristic to find out if we're dealing with a V3 or V4 message
// const v3Count = chatJV1.messages.filter((m) => 'text' in m).length;
// const v4Count = chatJV1.messages.filter((m) => 'fragments' in m).length;
// if (v3Count > v4Count) {
// we are dealing with a V3 message
// ...
// }
return V3StoreDataToHead.recreateConversation(chatJV1);
}
/** Used by: trade_client.loadConversationsFromAtRestV1 */
export function recreateFolders(part: RestFolderJsonV1[]): DFolder[] {
return (part || []).map(_recreateFolder).filter((f) => f) as DFolder[];
}
function _recreateFolder(part: RestFolderJsonV1): DFolder | null {
// sanity check
if (!part || !part.id || !part.title || !part.conversationIds) {
console.warn('createFolderFromJsonV1: invalid folder json', part);
return null;
}
return {
id: part.id,
title: part.title,
conversationIds: part.conversationIds,
color: part.color,
};
}
/// Head -> Rest V1 ///
/** Used by: downloadAllJsonV1B */
export function formatAllToJsonV1B(conversations: DConversation[], modelSources: DModelsService[], folders: DFolder[], enableFolders: boolean): RestAllJsonV1B {
return {
conversations: (conversations || []).map(formatChatToJsonV1),
models: { sources: modelSources },
folders: { folders, enableFolders },
};
}
/** Used by: ^, downloadSingleChat, ChatLinkExport.handleCreate */
export function formatChatToJsonV1(ec: DConversation): RestChatJsonV1 {
return {
id: ec.id,
messages: ec.messages,
systemPurposeId: ec.systemPurposeId,
userTitle: ec.userTitle,
autoTitle: ec.autoTitle,
created: ec.created,
updated: ec.updated,
};
}
/// STORED TYPES definitions ///
/// do not change(!) these - consider people's backups and stored data
export type RestAllJsonV1B = {
conversations: RestChatJsonV1[];
models: { sources: DModelsService[] };
folders?: { folders: RestFolderJsonV1[]; enableFolders: boolean };
}
export type RestChatJsonV1 = {
id: string;
messages: (DMessage | V3StoreDataToHead.ImportMessageV3)[];
systemPurposeId: string;
userTitle?: string;
autoTitle?: string;
created: number;
updated: number | null;
}
type RestFolderJsonV1 = {
id: string;
title: string;
conversationIds: DConversationId[];
color?: string; // Optional color property
}
}