File size: 10,620 Bytes
682b227
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/**
 * @deprecated Legacy migration utility — remove at some point in the future once all users have migrated to the new structured agentic message format.
 *
 * Converts old marker-based agentic messages to the new structured format
 * with separate messages per turn.
 *
 * Old format: Single assistant message with markers in content:
 *   <<<reasoning_content_start>>>...<<<reasoning_content_end>>>
 *   <<<AGENTIC_TOOL_CALL_START>>>...<<<AGENTIC_TOOL_CALL_END>>>
 *
 * New format: Separate messages per turn:
 *   - assistant (content + reasoningContent + toolCalls)
 *   - tool (toolCallId + content)
 *   - assistant (next turn)
 *   - ...
 */

import { LEGACY_AGENTIC_REGEX, LEGACY_REASONING_TAGS } from '$lib/constants';
import { DatabaseService } from '$lib/services/database.service';
import { MessageRole, MessageType } from '$lib/enums';
import type { DatabaseMessage } from '$lib/types/database';

const MIGRATION_DONE_KEY = 'llama-webui-migration-v2-done';

/**
 * @deprecated Part of legacy migration — remove with the migration module.
 * Check if migration has been performed.
 */
export function isMigrationNeeded(): boolean {
	try {
		return !localStorage.getItem(MIGRATION_DONE_KEY);
	} catch {
		return false;
	}
}

/**
 * Mark migration as done.
 */
function markMigrationDone(): void {
	try {
		localStorage.setItem(MIGRATION_DONE_KEY, String(Date.now()));
	} catch {
		// Ignore localStorage errors
	}
}

/**
 * Check if a message has legacy markers in its content.
 */
function hasLegacyMarkers(message: DatabaseMessage): boolean {
	if (!message.content) return false;
	return LEGACY_AGENTIC_REGEX.HAS_LEGACY_MARKERS.test(message.content);
}

/**
 * Extract reasoning content from legacy marker format.
 */
function extractLegacyReasoning(content: string): { reasoning: string; cleanContent: string } {
	let reasoning = '';
	let cleanContent = content;

	// Extract all reasoning blocks
	const re = new RegExp(LEGACY_AGENTIC_REGEX.REASONING_EXTRACT.source, 'g');
	let match;
	while ((match = re.exec(content)) !== null) {
		reasoning += match[1];
	}

	// Remove reasoning tags from content
	cleanContent = cleanContent
		.replace(new RegExp(LEGACY_AGENTIC_REGEX.REASONING_BLOCK.source, 'g'), '')
		.replace(LEGACY_AGENTIC_REGEX.REASONING_OPEN, '');

	return { reasoning, cleanContent };
}

/**
 * Parse legacy content with tool call markers into structured turns.
 */
interface ParsedTurn {
	textBefore: string;
	toolCalls: Array<{
		name: string;
		args: string;
		result: string;
	}>;
}

function parseLegacyToolCalls(content: string): ParsedTurn[] {
	const turns: ParsedTurn[] = [];
	const regex = new RegExp(LEGACY_AGENTIC_REGEX.COMPLETED_TOOL_CALL.source, 'g');

	let lastIndex = 0;
	let currentTurn: ParsedTurn = { textBefore: '', toolCalls: [] };
	let match;

	while ((match = regex.exec(content)) !== null) {
		const textBefore = content.slice(lastIndex, match.index).trim();

		// If there's text between tool calls and we already have tool calls,
		// that means a new turn started (text after tool results = new LLM turn)
		if (textBefore && currentTurn.toolCalls.length > 0) {
			turns.push(currentTurn);
			currentTurn = { textBefore, toolCalls: [] };
		} else if (textBefore && currentTurn.toolCalls.length === 0) {
			currentTurn.textBefore = textBefore;
		}

		currentTurn.toolCalls.push({
			name: match[1],
			args: match[2],
			result: match[3].replace(/^\n+|\n+$/g, '')
		});

		lastIndex = match.index + match[0].length;
	}

	// Any remaining text after the last tool call
	const remainingText = content.slice(lastIndex).trim();

	if (currentTurn.toolCalls.length > 0) {
		turns.push(currentTurn);
	}

	// If there's text after all tool calls, it's the final assistant response
	if (remainingText) {
		// Remove any partial/open markers
		const cleanRemaining = remainingText
			.replace(LEGACY_AGENTIC_REGEX.AGENTIC_TOOL_CALL_OPEN, '')
			.trim();
		if (cleanRemaining) {
			turns.push({ textBefore: cleanRemaining, toolCalls: [] });
		}
	}

	// If no tool calls found at all, return the original content as a single turn
	if (turns.length === 0) {
		turns.push({ textBefore: content.trim(), toolCalls: [] });
	}

	return turns;
}

/**
 * Migrate a single conversation's messages from legacy format to new format.
 */
async function migrateConversation(convId: string): Promise<number> {
	const allMessages = await DatabaseService.getConversationMessages(convId);
	let migratedCount = 0;

	for (const message of allMessages) {
		if (message.role !== MessageRole.ASSISTANT) continue;
		if (!hasLegacyMarkers(message)) {
			// Still check for reasoning-only markers (no tool calls)
			if (message.content?.includes(LEGACY_REASONING_TAGS.START)) {
				const { reasoning, cleanContent } = extractLegacyReasoning(message.content);
				await DatabaseService.updateMessage(message.id, {
					content: cleanContent.trim(),
					reasoningContent: reasoning || undefined
				});
				migratedCount++;
			}
			continue;
		}

		// Has agentic markers - full migration needed
		const { reasoning, cleanContent } = extractLegacyReasoning(message.content);
		const turns = parseLegacyToolCalls(cleanContent);

		// Parse existing toolCalls JSON to try to match IDs
		let existingToolCalls: Array<{ id: string; function?: { name: string; arguments: string } }> =
			[];
		if (message.toolCalls) {
			try {
				existingToolCalls = JSON.parse(message.toolCalls);
			} catch {
				// Ignore
			}
		}

		// First turn uses the existing message
		const firstTurn = turns[0];
		if (!firstTurn) continue;

		// Match tool calls from the first turn to existing IDs
		const firstTurnToolCalls = firstTurn.toolCalls.map((tc, i) => {
			const existing =
				existingToolCalls.find((e) => e.function?.name === tc.name) || existingToolCalls[i];
			return {
				id: existing?.id || `legacy_tool_${i}`,
				type: 'function' as const,
				function: { name: tc.name, arguments: tc.args }
			};
		});

		// Update the existing message for the first turn
		await DatabaseService.updateMessage(message.id, {
			content: firstTurn.textBefore,
			reasoningContent: reasoning || undefined,
			toolCalls: firstTurnToolCalls.length > 0 ? JSON.stringify(firstTurnToolCalls) : ''
		});

		let currentParentId = message.id;
		let toolCallIdCounter = existingToolCalls.length;

		// Create tool result messages for the first turn
		for (let i = 0; i < firstTurn.toolCalls.length; i++) {
			const tc = firstTurn.toolCalls[i];
			const toolCallId = firstTurnToolCalls[i]?.id || `legacy_tool_${i}`;

			const toolMsg = await DatabaseService.createMessageBranch(
				{
					convId,
					type: MessageType.TEXT,
					role: MessageRole.TOOL,
					content: tc.result,
					toolCallId,
					timestamp: message.timestamp + i + 1,
					toolCalls: '',
					children: []
				},
				currentParentId
			);
			currentParentId = toolMsg.id;
		}

		// Create messages for subsequent turns
		for (let turnIdx = 1; turnIdx < turns.length; turnIdx++) {
			const turn = turns[turnIdx];

			const turnToolCalls = turn.toolCalls.map((tc, i) => {
				const idx = toolCallIdCounter + i;
				const existing = existingToolCalls[idx];
				return {
					id: existing?.id || `legacy_tool_${idx}`,
					type: 'function' as const,
					function: { name: tc.name, arguments: tc.args }
				};
			});
			toolCallIdCounter += turn.toolCalls.length;

			// Create assistant message for this turn
			const assistantMsg = await DatabaseService.createMessageBranch(
				{
					convId,
					type: MessageType.TEXT,
					role: MessageRole.ASSISTANT,
					content: turn.textBefore,
					timestamp: message.timestamp + turnIdx * 100,
					toolCalls: turnToolCalls.length > 0 ? JSON.stringify(turnToolCalls) : '',
					children: [],
					model: message.model
				},
				currentParentId
			);
			currentParentId = assistantMsg.id;

			// Create tool result messages for this turn
			for (let i = 0; i < turn.toolCalls.length; i++) {
				const tc = turn.toolCalls[i];
				const toolCallId = turnToolCalls[i]?.id || `legacy_tool_${toolCallIdCounter + i}`;

				const toolMsg = await DatabaseService.createMessageBranch(
					{
						convId,
						type: MessageType.TEXT,
						role: MessageRole.TOOL,
						content: tc.result,
						toolCallId,
						timestamp: message.timestamp + turnIdx * 100 + i + 1,
						toolCalls: '',
						children: []
					},
					currentParentId
				);
				currentParentId = toolMsg.id;
			}
		}

		// Re-parent any children of the original message to the last created message
		// (the original message's children list was the next user message or similar)
		if (message.children.length > 0 && currentParentId !== message.id) {
			for (const childId of message.children) {
				// Skip children we just created (they were already properly parented)
				const child = allMessages.find((m) => m.id === childId);
				if (!child) continue;
				// Only re-parent non-tool messages that were original children
				if (child.role !== MessageRole.TOOL) {
					await DatabaseService.updateMessage(childId, { parent: currentParentId });
					// Add to new parent's children
					const newParent = await DatabaseService.getConversationMessages(convId).then((msgs) =>
						msgs.find((m) => m.id === currentParentId)
					);
					if (newParent && !newParent.children.includes(childId)) {
						await DatabaseService.updateMessage(currentParentId, {
							children: [...newParent.children, childId]
						});
					}
				}
			}
			// Clear re-parented children from the original message
			await DatabaseService.updateMessage(message.id, { children: [] });
		}

		migratedCount++;
	}

	return migratedCount;
}

/**
 * @deprecated Part of legacy migration — remove with the migration module.
 * Run the full migration across all conversations.
 * This should be called once at app startup if migration is needed.
 */
export async function runLegacyMigration(): Promise<void> {
	if (!isMigrationNeeded()) return;

	console.log('[Migration] Starting legacy message format migration...');

	try {
		const conversations = await DatabaseService.getAllConversations();
		let totalMigrated = 0;

		for (const conv of conversations) {
			const count = await migrateConversation(conv.id);
			totalMigrated += count;
		}

		if (totalMigrated > 0) {
			console.log(
				`[Migration] Migrated ${totalMigrated} messages across ${conversations.length} conversations`
			);
		} else {
			console.log('[Migration] No legacy messages found, marking as done');
		}

		markMigrationDone();
	} catch (error) {
		console.error('[Migration] Failed to migrate legacy messages:', error);
		// Still mark as done to avoid infinite retry loops
		markMigrationDone();
	}
}