Skip to content

Commit 9a28dc1

Browse files
committed
webui : add ExportedConversations type for chat import/export
1 parent 9daa72d commit 9a28dc1

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

tools/server/webui/src/lib/stores/chat.svelte.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { browser } from '$app/environment';
77
import { goto } from '$app/navigation';
88
import { extractPartialThinking } from '$lib/utils/thinking';
99
import { toast } from 'svelte-sonner';
10+
import type { ExportedConversations } from '$lib/types/database';
1011

1112
/**
1213
* ChatStore - Central state management for chat conversations and AI interactions
@@ -971,8 +972,8 @@ class ChatStore {
971972
this.triggerDownload(conversationData);
972973
} else {
973974
// Use current active conversation data
974-
const conversationData = {
975-
conv: this.activeConversation,
975+
const conversationData: ExportedConversations = {
976+
conv: this.activeConversation!,
976977
messages: this.activeMessages
977978
};
978979

@@ -985,8 +986,12 @@ class ChatStore {
985986
* @param data - Data to download (expected: { conv: DatabaseConversation, messages: DatabaseMessage[] })
986987
* @param filename - Optional filename
987988
*/
988-
private triggerDownload(data: any, filename?: string): void {
989-
const conversation = data.conv || data;
989+
private triggerDownload(data: ExportedConversations, filename?: string): void {
990+
const conversation = 'conv' in data ? data.conv : (Array.isArray(data) ? data[0]?.conv : undefined);
991+
if (!conversation) {
992+
console.error('Invalid data: missing conversation');
993+
return;
994+
}
990995
const conversationName = conversation.name ? conversation.name.trim() : '';
991996
const convId = conversation.id || 'unknown';
992997
const truncatedSuffix = conversationName.toLowerCase()
@@ -1017,7 +1022,7 @@ class ChatStore {
10171022
throw new Error('No conversations to export');
10181023
}
10191024

1020-
const allData = await Promise.all(
1025+
const allData: ExportedConversations = await Promise.all(
10211026
allConversations.map(async (conv) => {
10221027
const messages = await DatabaseStore.getConversationMessages(conv.id);
10231028
return { conv, messages };
@@ -1064,9 +1069,7 @@ class ChatStore {
10641069
try {
10651070
const text = await file.text();
10661071
const parsedData = JSON.parse(text);
1067-
1068-
// Handle both single conversation object and array of conversations
1069-
let importedData: { conv: DatabaseConversation; messages: DatabaseMessage[] }[];
1072+
let importedData: ExportedConversations;
10701073

10711074
if (Array.isArray(parsedData)) {
10721075
importedData = parsedData;

tools/server/webui/src/lib/types/database.d.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,18 @@ export interface DatabaseMessage {
5454
timings?: ChatMessageTimings;
5555
model?: string;
5656
}
57+
58+
/**
59+
* Represents a single conversation with its associated messages,
60+
* typically used for import/export operations.
61+
*/
62+
export type ExportedConversation = {
63+
conv: DatabaseConversation;
64+
messages: DatabaseMessage[];
65+
};
66+
67+
/**
68+
* Type representing one or more exported conversations.
69+
* Can be a single conversation object or an array of them.
70+
*/
71+
export type ExportedConversations = ExportedConversation | ExportedConversation[];

0 commit comments

Comments
 (0)