File size: 6,675 Bytes
77895c9
 
043242c
59c9d94
 
 
 
 
 
5dfba10
59c9d94
 
 
 
0eac98b
59c9d94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0eac98b
59c9d94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad4aac8
77895c9
59c9d94
 
77895c9
59c9d94
77895c9
59c9d94
 
 
77895c9
 
59c9d94
 
77895c9
 
 
 
 
 
 
 
 
59c9d94
043242c
59c9d94
 
 
 
 
 
 
b3e1908
 
 
 
 
 
 
59c9d94
b3e1908
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b9ff581
b3e1908
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59c9d94
 
b3e1908
59c9d94
 
b3e1908
59c9d94
 
 
 
 
 
ad4aac8
 
59c9d94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad4aac8
 
59c9d94
 
 
ad4aac8
59c9d94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5dfba10
59c9d94
 
 
 
 
 
 
 
 
 
 
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
import type { ChatMessage, TokenUsage } from '$lib/helpers/types';
import { modelsState } from '$lib/state/models.svelte';
import { token } from '$lib/state/token.svelte';
import type { Edge, Node } from '@xyflow/svelte';

export interface TriggerAiCallContext {
	userId: string;
	newNodes: Node[];
	messages: ChatMessage[];
	selectedModels: string[];
	prompt: string;
	nodeData: Record<string, unknown> | undefined;
	authToken: string;
	billingOption: string;
	updateNodeData: (id: string, data: Record<string, unknown>, opts?: { replace?: boolean }) => void;
	updateNodes: (fn: (nodes: Node[]) => Node[]) => void;
	updateEdges: (fn: (edges: Edge[]) => Edge[]) => void;
	onLoadingChange: (loading: boolean) => void;
	onError: (message: string) => void;
}

function formatMessagesForModel(messages: ChatMessage[], modelId: string) {
	return messages.map((message) => {
		if (message.role === 'user') {
			return { role: 'user' as const, content: message.content };
		}
		const modelData = message[modelId];
		const content =
			typeof modelData === 'object' && modelData && 'content' in modelData
				? modelData.content
				: (Object.values(message).find(
						(m): m is { content: string } => typeof m === 'object' && m !== null && 'content' in m
					)?.content ?? '');
		return { role: 'assistant' as const, content };
	});
}

export async function triggerAiCall(ctx: TriggerAiCallContext): Promise<void> {
	const {
		userId,
		newNodes,
		messages,
		selectedModels,
		prompt,
		nodeData,
		billingOption,
		updateNodeData,
		updateNodes,
		updateEdges,
		onLoadingChange,
		onError
	} = ctx;

	onLoadingChange(true);
	const failedNodeIds = new Set<string>();
	const failedModelIds = new Set<string>();
	const errorMessages: string[] = [];

	const results = await Promise.all(
		newNodes.map(async (node) => {
			const model = node?.data?.selectedModel as string;
			if (!model) return null;
			try {
				const modelSettings = modelsState.models.find((m) => m.id === model);
				const start = Date.now();
				const formattedMessages = formatMessagesForModel(messages, model);
				const response = await fetch('/api', {
					method: 'POST',
					body: JSON.stringify({
						model: model,
						provider: modelSettings?.provider ?? 'auto',
						messages: formattedMessages,
						billingTo: billingOption,
						...(modelSettings
							? {
									options: {
										temperature: modelSettings.temperature,
										max_tokens: modelSettings.max_tokens,
										top_p: modelSettings.top_p
									}
								}
							: {})
					}),
					headers: { Authorization: `Bearer ${token.value}` }
				});
				if (!response.ok) {
					const errorBody = await response.text().catch(() => response.statusText);
					throw new Error(errorBody || response.statusText);
				}
				if (!response.body) throw new Error('No response body');

				let content = '';
				let reasoning = '';
				let usage: TokenUsage | null = null;
				let inThink = false;
				let buffer = '';
				const reader = response.body.getReader();
				const decoder = new TextDecoder();

				while (true) {
					const { done, value } = await reader.read();
					if (done) {
						if (content.includes('__ERROR__')) {
							const errMsg = content.split('__ERROR__').pop() ?? 'Unknown error';
							throw new Error(errMsg);
						}
						if (content.includes('__USAGE__')) {
							const usageParts = content.split('__USAGE__');
							const usageJson = usageParts.pop() ?? '';
							content = usageParts.join('').trimEnd();
							try {
								usage = JSON.parse(usageJson) as TokenUsage;
							} catch {
								// ignore malformed usage JSON
							}
						}
						const end = Date.now();
						updateNodeData(
							node.id,
							{
								...node.data,
								content,
								reasoning,
								timestamp: end - start,
								loading: false,
								messages,
								usage
							} as Record<string, unknown>,
							{ replace: true }
						);
						return { [model]: { content, timestamp: String(end - start) } };
					}
					buffer += decoder.decode(value, { stream: true });

					// Process buffer chunk by chunk, splitting on think tags
					while (true) {
						if (inThink) {
							const closeIdx = buffer.indexOf('</think>');
							if (closeIdx === -1) {
								reasoning += buffer;
								buffer = '';
								break;
							}
							reasoning += buffer.slice(0, closeIdx);
							buffer = buffer.slice(closeIdx + '</think>'.length);
							inThink = false;
						} else {
							const openIdx = buffer.indexOf('<think>');
							if (openIdx === -1) {
								content += buffer;
								buffer = '';
								break;
							}
							content += buffer.slice(0, openIdx);
							buffer = buffer.slice(openIdx + '<think>'.length);
							inThink = true;
						}
					}

					updateNodeData(
						node.id,
						{ ...node.data, content, reasoning, loading: false } as Record<string, unknown>,
						{ replace: true }
					);
				}
			} catch (error) {
				const msg = error instanceof Error ? error.message : 'An unknown error occurred';
				failedNodeIds.add(node.id);
				// failedModelIds.add(model);
				errorMessages.push(msg);
				return null;
			} finally {
				onLoadingChange(false);
			}
		})
	);

	if (failedNodeIds.size > 0) {
		updateNodes((currentNodes) => currentNodes.filter((n) => !failedNodeIds.has(n.id)));
		updateEdges((currentEdges) =>
			currentEdges.filter((e) => !failedNodeIds.has(e.target as string))
		);
		updateNodeData(
			userId,
			{
				...nodeData,
				messages: newNodes.length === failedNodeIds.size ? messages.slice(0, -1) : messages,
				prompt: newNodes.length === failedNodeIds.size ? prompt : ''
				// selectedModels: selectedModels.filter((m) => !failedModelIds.has(m))
			} as Record<string, unknown>,
			{ replace: true }
		);
		errorMessages.forEach((msg) => onError(msg));
	}

	const validResults = results.filter(
		(r): r is Record<string, { content: string; timestamp: string }> => r != null
	);
	if (validResults.length === 0) return;

	const assistantMessage = validResults.reduce<ChatMessage>(
		(acc, result) => (result ? { ...acc, ...result } : acc),
		{ role: 'assistant' }
	);

	const newNodeId = `user-${crypto.randomUUID()}`;
	const newNode: Node = {
		id: newNodeId,
		type: 'user',
		position: { x: 0, y: 0 },
		data: {
			role: 'user',
			selectedModels: selectedModels.filter((m) => !failedModelIds.has(m)),
			messages: [...messages, assistantMessage]
		}
	};
	const newEdges: Edge[] = newNodes.map((node) => ({
		id: `edge-${crypto.randomUUID()}`,
		source: node.id,
		target: newNodeId
	}));
	updateNodes((currentNodes) => [...currentNodes, newNode]);
	updateEdges((currentEdges) => [...currentEdges, ...newEdges]);
}