@absolutejs/absolute 0.19.0-beta.226 → 0.19.0-beta.228
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.absolutejs/eslint-cache +1 -0
- package/.absolutejs/prettier.cache.json +42 -19
- package/.absolutejs/vue-tsc.tsbuildinfo +1 -1
- package/.claude/settings.local.json +2 -1
- package/ROADMAP.md +0 -25
- package/dist/{Image-y5bdvj57.vue → Image-b3r6hxqk.vue} +6 -1
- package/dist/ai/index.js +529 -0
- package/dist/ai/index.js.map +13 -0
- package/dist/ai/providers/anthropic.js +396 -0
- package/dist/ai/providers/anthropic.js.map +10 -0
- package/dist/ai/providers/ollama.js +233 -0
- package/dist/ai/providers/ollama.js.map +10 -0
- package/dist/ai/providers/openai.js +355 -0
- package/dist/ai/providers/openai.js.map +10 -0
- package/dist/ai-client/angular/ai/index.js +532 -0
- package/dist/ai-client/react/ai/index.js +499 -0
- package/dist/ai-client/vue/ai/index.js +460 -0
- package/dist/angular/ai/index.js +668 -0
- package/dist/angular/ai/index.js.map +15 -0
- package/dist/angular/components/image.component.js +1 -1
- package/dist/angular/index.js +11 -1
- package/dist/angular/index.js.map +3 -3
- package/dist/build.js +48 -2
- package/dist/build.js.map +4 -4
- package/dist/index.js +50 -2
- package/dist/index.js.map +6 -6
- package/dist/react/ai/index.js +635 -0
- package/dist/react/ai/index.js.map +16 -0
- package/dist/react/components/index.js +11 -1
- package/dist/react/components/index.js.map +2 -2
- package/dist/react/hooks/index.js +11 -1
- package/dist/react/hooks/index.js.map +2 -2
- package/dist/react/index.js +11 -1
- package/dist/react/index.js.map +3 -3
- package/dist/src/ai/client/actions.d.ts +46 -0
- package/dist/src/ai/client/connection.d.ts +9 -0
- package/dist/src/ai/client/messageStore.d.ts +12 -0
- package/dist/src/ai/conversationManager.d.ts +11 -0
- package/dist/src/ai/index.d.ts +3 -0
- package/dist/src/ai/protocol.d.ts +4 -0
- package/dist/src/ai/providers/anthropic.d.ts +3 -0
- package/dist/src/ai/providers/ollama.d.ts +6 -0
- package/dist/src/ai/providers/openai.d.ts +7 -0
- package/dist/src/ai/streamAI.d.ts +2 -0
- package/dist/src/angular/ai/ai-stream.service.d.ts +15 -0
- package/dist/src/angular/ai/index.d.ts +1 -0
- package/dist/src/react/ai/AIStreamProvider.d.ts +13 -0
- package/dist/src/react/ai/index.d.ts +2 -0
- package/dist/src/react/ai/useAIStream.d.ts +8 -0
- package/dist/src/svelte/ai/createAIStream.d.ts +10 -0
- package/dist/src/svelte/ai/index.d.ts +1 -0
- package/dist/src/vue/ai/index.d.ts +1 -0
- package/dist/src/vue/ai/useAIStream.d.ts +22 -0
- package/dist/svelte/ai/index.js +590 -0
- package/dist/svelte/ai/index.js.map +15 -0
- package/dist/svelte/components/Image.svelte +6 -1
- package/dist/svelte/index.js +11 -1
- package/dist/svelte/index.js.map +3 -3
- package/dist/types/ai.d.ts +188 -0
- package/dist/types/anthropic.d.ts +18 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/typeGuards.d.ts +3 -0
- package/dist/vue/ai/index.js +596 -0
- package/dist/vue/ai/index.js.map +15 -0
- package/dist/vue/components/Image.vue +6 -1
- package/dist/vue/components/index.js +12 -2
- package/dist/vue/components/index.js.map +1 -1
- package/dist/vue/index.js +11 -1
- package/dist/vue/index.js.map +3 -3
- package/package.json +36 -1
- package/scripts/build.ts +33 -0
- package/types/ai.ts +235 -0
- package/types/anthropic.ts +17 -0
- package/types/index.ts +1 -0
- package/types/typeGuards.ts +72 -1
|
@@ -0,0 +1,635 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
function __accessProp(key) {
|
|
9
|
+
return this[key];
|
|
10
|
+
}
|
|
11
|
+
var __toESMCache_node;
|
|
12
|
+
var __toESMCache_esm;
|
|
13
|
+
var __toESM = (mod, isNodeMode, target) => {
|
|
14
|
+
var canCache = mod != null && typeof mod === "object";
|
|
15
|
+
if (canCache) {
|
|
16
|
+
var cache = isNodeMode ? __toESMCache_node ??= new WeakMap : __toESMCache_esm ??= new WeakMap;
|
|
17
|
+
var cached = cache.get(mod);
|
|
18
|
+
if (cached)
|
|
19
|
+
return cached;
|
|
20
|
+
}
|
|
21
|
+
target = mod != null ? __create(__getProtoOf(mod)) : {};
|
|
22
|
+
const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
|
|
23
|
+
for (let key of __getOwnPropNames(mod))
|
|
24
|
+
if (!__hasOwnProp.call(to, key))
|
|
25
|
+
__defProp(to, key, {
|
|
26
|
+
get: __accessProp.bind(mod, key),
|
|
27
|
+
enumerable: true
|
|
28
|
+
});
|
|
29
|
+
if (canCache)
|
|
30
|
+
cache.set(mod, to);
|
|
31
|
+
return to;
|
|
32
|
+
};
|
|
33
|
+
var __toCommonJS = (from) => {
|
|
34
|
+
var entry = (__moduleCache ??= new WeakMap).get(from), desc;
|
|
35
|
+
if (entry)
|
|
36
|
+
return entry;
|
|
37
|
+
entry = __defProp({}, "__esModule", { value: true });
|
|
38
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
39
|
+
for (var key of __getOwnPropNames(from))
|
|
40
|
+
if (!__hasOwnProp.call(entry, key))
|
|
41
|
+
__defProp(entry, key, {
|
|
42
|
+
get: __accessProp.bind(from, key),
|
|
43
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
__moduleCache.set(from, entry);
|
|
47
|
+
return entry;
|
|
48
|
+
};
|
|
49
|
+
var __moduleCache;
|
|
50
|
+
var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
|
|
51
|
+
var __returnValue = (v) => v;
|
|
52
|
+
function __exportSetter(name, newValue) {
|
|
53
|
+
this[name] = __returnValue.bind(null, newValue);
|
|
54
|
+
}
|
|
55
|
+
var __export = (target, all) => {
|
|
56
|
+
for (var name in all)
|
|
57
|
+
__defProp(target, name, {
|
|
58
|
+
get: all[name],
|
|
59
|
+
enumerable: true,
|
|
60
|
+
configurable: true,
|
|
61
|
+
set: __exportSetter.bind(all, name)
|
|
62
|
+
});
|
|
63
|
+
};
|
|
64
|
+
var __legacyDecorateClassTS = function(decorators, target, key, desc) {
|
|
65
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
66
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
67
|
+
r = Reflect.decorate(decorators, target, key, desc);
|
|
68
|
+
else
|
|
69
|
+
for (var i = decorators.length - 1;i >= 0; i--)
|
|
70
|
+
if (d = decorators[i])
|
|
71
|
+
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
72
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
73
|
+
};
|
|
74
|
+
var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
|
|
75
|
+
var __require = import.meta.require;
|
|
76
|
+
|
|
77
|
+
// types/typeGuards.ts
|
|
78
|
+
var isValidAIClientMessage = (data) => {
|
|
79
|
+
if (!data || typeof data !== "object") {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
if (!("type" in data) || typeof data.type !== "string") {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
switch (data.type) {
|
|
86
|
+
case "message":
|
|
87
|
+
return "content" in data && typeof data.content === "string";
|
|
88
|
+
case "cancel":
|
|
89
|
+
return "conversationId" in data && typeof data.conversationId === "string";
|
|
90
|
+
case "branch":
|
|
91
|
+
return "messageId" in data && typeof data.messageId === "string" && "content" in data && typeof data.content === "string" && "conversationId" in data && typeof data.conversationId === "string";
|
|
92
|
+
default:
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
}, isValidAIServerMessage = (data) => {
|
|
96
|
+
if (!data || typeof data !== "object") {
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
if (!("type" in data) || typeof data.type !== "string") {
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
switch (data.type) {
|
|
103
|
+
case "chunk":
|
|
104
|
+
return "content" in data && typeof data.content === "string" && "messageId" in data && "conversationId" in data;
|
|
105
|
+
case "tool_status":
|
|
106
|
+
return "name" in data && "status" in data && "messageId" in data && "conversationId" in data;
|
|
107
|
+
case "complete":
|
|
108
|
+
return "messageId" in data && "conversationId" in data;
|
|
109
|
+
case "error":
|
|
110
|
+
return "message" in data && typeof data.message === "string";
|
|
111
|
+
default:
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
}, isValidHMRClientMessage = (data) => {
|
|
115
|
+
if (!data || typeof data !== "object") {
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
if (!("type" in data) || typeof data.type !== "string") {
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
switch (data.type) {
|
|
122
|
+
case "ping":
|
|
123
|
+
return true;
|
|
124
|
+
case "ready":
|
|
125
|
+
return true;
|
|
126
|
+
case "request-rebuild":
|
|
127
|
+
return true;
|
|
128
|
+
case "hydration-error":
|
|
129
|
+
return true;
|
|
130
|
+
case "hmr-timing":
|
|
131
|
+
return true;
|
|
132
|
+
default:
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
// src/react/ai/AIStreamProvider.tsx
|
|
138
|
+
import {
|
|
139
|
+
createContext,
|
|
140
|
+
useContext,
|
|
141
|
+
useEffect,
|
|
142
|
+
useRef
|
|
143
|
+
} from "react";
|
|
144
|
+
|
|
145
|
+
// src/ai/client/connection.ts
|
|
146
|
+
var WS_OPEN = 1;
|
|
147
|
+
var WS_NORMAL_CLOSURE = 1000;
|
|
148
|
+
var WS_CLOSED = 3;
|
|
149
|
+
var DEFAULT_PING_INTERVAL = 30000;
|
|
150
|
+
var RECONNECT_INITIAL_DELAY = 500;
|
|
151
|
+
var RECONNECT_POLL_INTERVAL = 300;
|
|
152
|
+
var DEFAULT_MAX_RECONNECT_ATTEMPTS = 60;
|
|
153
|
+
var noop = () => {};
|
|
154
|
+
var noopUnsubscribe = () => noop;
|
|
155
|
+
var NOOP_CONNECTION = {
|
|
156
|
+
close: noop,
|
|
157
|
+
send: noop,
|
|
158
|
+
subscribe: noopUnsubscribe,
|
|
159
|
+
getReadyState: () => WS_CLOSED
|
|
160
|
+
};
|
|
161
|
+
var buildWsUrl = (path) => {
|
|
162
|
+
const { hostname, port, protocol } = window.location;
|
|
163
|
+
const wsProtocol = protocol === "https:" ? "wss:" : "ws:";
|
|
164
|
+
const portSuffix = port ? `:${port}` : "";
|
|
165
|
+
return `${wsProtocol}//${hostname}${portSuffix}${path}`;
|
|
166
|
+
};
|
|
167
|
+
var parseServerMessage = (event) => {
|
|
168
|
+
let data;
|
|
169
|
+
try {
|
|
170
|
+
data = JSON.parse(String(event.data));
|
|
171
|
+
} catch {
|
|
172
|
+
return null;
|
|
173
|
+
}
|
|
174
|
+
if (data && typeof data === "object" && "type" in data && data.type === "pong") {
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
if (!isValidAIServerMessage(data)) {
|
|
178
|
+
return null;
|
|
179
|
+
}
|
|
180
|
+
return data;
|
|
181
|
+
};
|
|
182
|
+
var createAIConnection = (path, options = {}) => {
|
|
183
|
+
if (typeof window === "undefined") {
|
|
184
|
+
return NOOP_CONNECTION;
|
|
185
|
+
}
|
|
186
|
+
const shouldReconnect = options.reconnect !== false;
|
|
187
|
+
const pingInterval = options.pingInterval ?? DEFAULT_PING_INTERVAL;
|
|
188
|
+
const maxReconnectAttempts = options.maxReconnectAttempts ?? DEFAULT_MAX_RECONNECT_ATTEMPTS;
|
|
189
|
+
const listeners = new Set;
|
|
190
|
+
const connState = {
|
|
191
|
+
isConnected: false,
|
|
192
|
+
pingInterval: null,
|
|
193
|
+
reconnectAttempts: 0,
|
|
194
|
+
reconnectTimeout: null,
|
|
195
|
+
ws: null
|
|
196
|
+
};
|
|
197
|
+
const clearTimers = () => {
|
|
198
|
+
if (connState.pingInterval) {
|
|
199
|
+
clearInterval(connState.pingInterval);
|
|
200
|
+
connState.pingInterval = null;
|
|
201
|
+
}
|
|
202
|
+
if (connState.reconnectTimeout) {
|
|
203
|
+
clearTimeout(connState.reconnectTimeout);
|
|
204
|
+
connState.reconnectTimeout = null;
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
const scheduleReconnect = () => {
|
|
208
|
+
connState.reconnectAttempts++;
|
|
209
|
+
const delay = connState.reconnectAttempts === 1 ? RECONNECT_INITIAL_DELAY : RECONNECT_POLL_INTERVAL;
|
|
210
|
+
connState.reconnectTimeout = setTimeout(() => {
|
|
211
|
+
if (connState.reconnectAttempts > maxReconnectAttempts) {
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
connect();
|
|
215
|
+
}, delay);
|
|
216
|
+
};
|
|
217
|
+
const connect = () => {
|
|
218
|
+
const url = buildWsUrl(path);
|
|
219
|
+
const wsInstance = new WebSocket(url, options.protocols);
|
|
220
|
+
wsInstance.onopen = () => {
|
|
221
|
+
connState.isConnected = true;
|
|
222
|
+
connState.reconnectAttempts = 0;
|
|
223
|
+
connState.pingInterval = setInterval(() => {
|
|
224
|
+
if (wsInstance.readyState === WS_OPEN && connState.isConnected) {
|
|
225
|
+
wsInstance.send(JSON.stringify({ type: "ping" }));
|
|
226
|
+
}
|
|
227
|
+
}, pingInterval);
|
|
228
|
+
};
|
|
229
|
+
wsInstance.onmessage = (event) => {
|
|
230
|
+
const message = parseServerMessage(event);
|
|
231
|
+
if (!message) {
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
listeners.forEach((listener) => listener(message));
|
|
235
|
+
};
|
|
236
|
+
wsInstance.onclose = (event) => {
|
|
237
|
+
connState.isConnected = false;
|
|
238
|
+
clearTimers();
|
|
239
|
+
const shouldAttemptReconnect = shouldReconnect && event.code !== WS_NORMAL_CLOSURE && connState.reconnectAttempts < maxReconnectAttempts;
|
|
240
|
+
if (shouldAttemptReconnect) {
|
|
241
|
+
scheduleReconnect();
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
wsInstance.onerror = () => {};
|
|
245
|
+
connState.ws = wsInstance;
|
|
246
|
+
};
|
|
247
|
+
const send = (msg) => {
|
|
248
|
+
if (connState.ws?.readyState === WS_OPEN) {
|
|
249
|
+
connState.ws.send(JSON.stringify(msg));
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
const subscribe = (callback) => {
|
|
253
|
+
listeners.add(callback);
|
|
254
|
+
return () => {
|
|
255
|
+
listeners.delete(callback);
|
|
256
|
+
};
|
|
257
|
+
};
|
|
258
|
+
const close = () => {
|
|
259
|
+
clearTimers();
|
|
260
|
+
if (connState.ws) {
|
|
261
|
+
connState.ws.close(WS_NORMAL_CLOSURE);
|
|
262
|
+
connState.ws = null;
|
|
263
|
+
}
|
|
264
|
+
connState.isConnected = false;
|
|
265
|
+
listeners.clear();
|
|
266
|
+
};
|
|
267
|
+
const getReadyState = () => connState.ws?.readyState ?? WS_CLOSED;
|
|
268
|
+
connect();
|
|
269
|
+
return { close, getReadyState, send, subscribe };
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
// src/ai/client/messageStore.ts
|
|
273
|
+
var EMPTY_STATE = {
|
|
274
|
+
activeConversationId: null,
|
|
275
|
+
conversations: new Map,
|
|
276
|
+
error: null,
|
|
277
|
+
isStreaming: false
|
|
278
|
+
};
|
|
279
|
+
var initialActiveConversationId = null;
|
|
280
|
+
var initialError = null;
|
|
281
|
+
var freshState = () => ({
|
|
282
|
+
activeConversationId: initialActiveConversationId,
|
|
283
|
+
conversations: new Map,
|
|
284
|
+
error: initialError,
|
|
285
|
+
isStreaming: false
|
|
286
|
+
});
|
|
287
|
+
var findAssistantMessage = (conversation, messageId) => conversation.messages.find((msg) => msg.id === messageId && msg.role === "assistant");
|
|
288
|
+
var getOrCreate = (state, conversationId) => {
|
|
289
|
+
let conversation = state.conversations.get(conversationId);
|
|
290
|
+
if (!conversation) {
|
|
291
|
+
conversation = { id: conversationId, messages: [] };
|
|
292
|
+
state.conversations.set(conversationId, conversation);
|
|
293
|
+
}
|
|
294
|
+
return conversation;
|
|
295
|
+
};
|
|
296
|
+
var handleSend = (state, action) => {
|
|
297
|
+
const conversation = getOrCreate(state, action.conversationId);
|
|
298
|
+
const message = {
|
|
299
|
+
content: action.content,
|
|
300
|
+
conversationId: action.conversationId,
|
|
301
|
+
id: action.messageId,
|
|
302
|
+
role: "user",
|
|
303
|
+
timestamp: Date.now()
|
|
304
|
+
};
|
|
305
|
+
conversation.messages = [...conversation.messages, message];
|
|
306
|
+
state.activeConversationId = action.conversationId;
|
|
307
|
+
state.error = null;
|
|
308
|
+
state.isStreaming = true;
|
|
309
|
+
};
|
|
310
|
+
var handleChunk = (state, action) => {
|
|
311
|
+
const conversation = getOrCreate(state, action.conversationId);
|
|
312
|
+
const existing = findAssistantMessage(conversation, action.messageId);
|
|
313
|
+
if (existing) {
|
|
314
|
+
existing.content += action.content;
|
|
315
|
+
conversation.messages = [...conversation.messages];
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
const message = {
|
|
319
|
+
content: action.content,
|
|
320
|
+
conversationId: action.conversationId,
|
|
321
|
+
id: action.messageId,
|
|
322
|
+
isStreaming: true,
|
|
323
|
+
role: "assistant",
|
|
324
|
+
timestamp: Date.now()
|
|
325
|
+
};
|
|
326
|
+
conversation.messages = [...conversation.messages, message];
|
|
327
|
+
};
|
|
328
|
+
var upsertToolCall = (message, toolCall) => {
|
|
329
|
+
if (!message.toolCalls) {
|
|
330
|
+
message.toolCalls = [toolCall];
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
const existingIdx = message.toolCalls.findIndex((existing) => existing.name === toolCall.name);
|
|
334
|
+
if (existingIdx >= 0) {
|
|
335
|
+
message.toolCalls[existingIdx] = toolCall;
|
|
336
|
+
} else {
|
|
337
|
+
message.toolCalls = [...message.toolCalls, toolCall];
|
|
338
|
+
}
|
|
339
|
+
};
|
|
340
|
+
var handleToolStatus = (state, action) => {
|
|
341
|
+
const conversation = getOrCreate(state, action.conversationId);
|
|
342
|
+
const message = findAssistantMessage(conversation, action.messageId);
|
|
343
|
+
if (!message) {
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
const toolCall = {
|
|
347
|
+
id: action.messageId,
|
|
348
|
+
input: action.input,
|
|
349
|
+
name: action.name,
|
|
350
|
+
result: action.status === "complete" ? action.result ?? "" : undefined
|
|
351
|
+
};
|
|
352
|
+
upsertToolCall(message, toolCall);
|
|
353
|
+
conversation.messages = [...conversation.messages];
|
|
354
|
+
};
|
|
355
|
+
var markMessageComplete = (conversation, messageId) => {
|
|
356
|
+
const message = findAssistantMessage(conversation, messageId);
|
|
357
|
+
if (message) {
|
|
358
|
+
message.isStreaming = false;
|
|
359
|
+
}
|
|
360
|
+
conversation.messages = [...conversation.messages];
|
|
361
|
+
};
|
|
362
|
+
var handleComplete = (state, action) => {
|
|
363
|
+
const conversation = state.conversations.get(action.conversationId);
|
|
364
|
+
if (conversation) {
|
|
365
|
+
markMessageComplete(conversation, action.messageId);
|
|
366
|
+
}
|
|
367
|
+
state.isStreaming = false;
|
|
368
|
+
};
|
|
369
|
+
var markConversationStreamsComplete = (conversation) => {
|
|
370
|
+
const streamingMessages = conversation.messages.filter((msg) => msg.isStreaming);
|
|
371
|
+
if (streamingMessages.length === 0) {
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
for (const msg of streamingMessages) {
|
|
375
|
+
msg.isStreaming = false;
|
|
376
|
+
}
|
|
377
|
+
conversation.messages = [...conversation.messages];
|
|
378
|
+
};
|
|
379
|
+
var markAllStreamsComplete = (state) => {
|
|
380
|
+
for (const [, conversation] of state.conversations) {
|
|
381
|
+
markConversationStreamsComplete(conversation);
|
|
382
|
+
}
|
|
383
|
+
};
|
|
384
|
+
var handleBranch = (state, action) => {
|
|
385
|
+
const source = state.conversations.get(action.oldConversationId);
|
|
386
|
+
if (!source) {
|
|
387
|
+
return;
|
|
388
|
+
}
|
|
389
|
+
const cutoffIndex = source.messages.findIndex((msg) => msg.id === action.fromMessageId);
|
|
390
|
+
if (cutoffIndex < 0) {
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
const branchedMessages = source.messages.slice(0, cutoffIndex + 1).map((msg) => ({ ...msg, conversationId: action.newConversationId }));
|
|
394
|
+
const newConversation = {
|
|
395
|
+
id: action.newConversationId,
|
|
396
|
+
messages: branchedMessages
|
|
397
|
+
};
|
|
398
|
+
state.conversations.set(action.newConversationId, newConversation);
|
|
399
|
+
state.activeConversationId = action.newConversationId;
|
|
400
|
+
};
|
|
401
|
+
var dispatch = (state, subscribers, action) => {
|
|
402
|
+
switch (action.type) {
|
|
403
|
+
case "send":
|
|
404
|
+
handleSend(state, action);
|
|
405
|
+
break;
|
|
406
|
+
case "chunk":
|
|
407
|
+
handleChunk(state, action);
|
|
408
|
+
break;
|
|
409
|
+
case "tool_status":
|
|
410
|
+
handleToolStatus(state, action);
|
|
411
|
+
break;
|
|
412
|
+
case "complete":
|
|
413
|
+
handleComplete(state, action);
|
|
414
|
+
break;
|
|
415
|
+
case "error":
|
|
416
|
+
state.error = action.message;
|
|
417
|
+
state.isStreaming = false;
|
|
418
|
+
break;
|
|
419
|
+
case "cancel":
|
|
420
|
+
state.isStreaming = false;
|
|
421
|
+
markAllStreamsComplete(state);
|
|
422
|
+
break;
|
|
423
|
+
case "branch":
|
|
424
|
+
handleBranch(state, action);
|
|
425
|
+
break;
|
|
426
|
+
case "set_conversation":
|
|
427
|
+
state.activeConversationId = action.conversationId;
|
|
428
|
+
break;
|
|
429
|
+
}
|
|
430
|
+
Object.assign(state, { conversations: new Map(state.conversations) });
|
|
431
|
+
subscribers.forEach((callback) => callback());
|
|
432
|
+
};
|
|
433
|
+
var createAIMessageStore = () => {
|
|
434
|
+
const state = freshState();
|
|
435
|
+
const subscribers = new Set;
|
|
436
|
+
return {
|
|
437
|
+
dispatch: (action) => dispatch(state, subscribers, action),
|
|
438
|
+
getServerSnapshot: () => EMPTY_STATE,
|
|
439
|
+
getSnapshot: () => state,
|
|
440
|
+
subscribe: (callback) => {
|
|
441
|
+
subscribers.add(callback);
|
|
442
|
+
return () => {
|
|
443
|
+
subscribers.delete(callback);
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
};
|
|
447
|
+
};
|
|
448
|
+
|
|
449
|
+
// src/ai/client/actions.ts
|
|
450
|
+
var serverMessageToAction = (msg) => {
|
|
451
|
+
switch (msg.type) {
|
|
452
|
+
case "chunk":
|
|
453
|
+
return {
|
|
454
|
+
content: msg.content,
|
|
455
|
+
conversationId: msg.conversationId,
|
|
456
|
+
messageId: msg.messageId,
|
|
457
|
+
type: "chunk"
|
|
458
|
+
};
|
|
459
|
+
case "tool_status":
|
|
460
|
+
return {
|
|
461
|
+
conversationId: msg.conversationId,
|
|
462
|
+
input: msg.input,
|
|
463
|
+
messageId: msg.messageId,
|
|
464
|
+
name: msg.name,
|
|
465
|
+
result: msg.result,
|
|
466
|
+
status: msg.status,
|
|
467
|
+
type: "tool_status"
|
|
468
|
+
};
|
|
469
|
+
case "complete":
|
|
470
|
+
return {
|
|
471
|
+
conversationId: msg.conversationId,
|
|
472
|
+
messageId: msg.messageId,
|
|
473
|
+
type: "complete",
|
|
474
|
+
usage: msg.usage
|
|
475
|
+
};
|
|
476
|
+
case "error":
|
|
477
|
+
return { message: msg.message, type: "error" };
|
|
478
|
+
default:
|
|
479
|
+
return null;
|
|
480
|
+
}
|
|
481
|
+
};
|
|
482
|
+
|
|
483
|
+
// src/react/ai/AIStreamProvider.tsx
|
|
484
|
+
import { jsxDEV } from "react/jsx-dev-runtime";
|
|
485
|
+
var AIStreamContext = createContext(null);
|
|
486
|
+
var AIStreamProvider = ({
|
|
487
|
+
children,
|
|
488
|
+
path
|
|
489
|
+
}) => {
|
|
490
|
+
const ref = useRef(null);
|
|
491
|
+
if (!ref.current) {
|
|
492
|
+
const connection = createAIConnection(path);
|
|
493
|
+
const store = createAIMessageStore();
|
|
494
|
+
ref.current = { connection, store };
|
|
495
|
+
}
|
|
496
|
+
useEffect(() => {
|
|
497
|
+
const { current } = ref;
|
|
498
|
+
if (!current) {
|
|
499
|
+
return;
|
|
500
|
+
}
|
|
501
|
+
const { connection, store } = current;
|
|
502
|
+
const unsubscribe = connection.subscribe((msg) => {
|
|
503
|
+
const action = serverMessageToAction(msg);
|
|
504
|
+
if (action) {
|
|
505
|
+
store.dispatch(action);
|
|
506
|
+
}
|
|
507
|
+
});
|
|
508
|
+
return () => {
|
|
509
|
+
unsubscribe();
|
|
510
|
+
connection.close();
|
|
511
|
+
};
|
|
512
|
+
}, []);
|
|
513
|
+
return /* @__PURE__ */ jsxDEV(AIStreamContext.Provider, {
|
|
514
|
+
value: ref.current,
|
|
515
|
+
children
|
|
516
|
+
}, undefined, false, undefined, this);
|
|
517
|
+
};
|
|
518
|
+
var useAIStreamContext = () => useContext(AIStreamContext);
|
|
519
|
+
// src/react/ai/useAIStream.ts
|
|
520
|
+
import { useCallback, useEffect as useEffect2, useRef as useRef2, useSyncExternalStore } from "react";
|
|
521
|
+
|
|
522
|
+
// src/ai/protocol.ts
|
|
523
|
+
var generateId = () => crypto.randomUUID();
|
|
524
|
+
var parseAIMessage = (raw) => {
|
|
525
|
+
if (raw === null || raw === undefined) {
|
|
526
|
+
return null;
|
|
527
|
+
}
|
|
528
|
+
let text;
|
|
529
|
+
if (typeof raw === "string") {
|
|
530
|
+
text = raw;
|
|
531
|
+
} else if (raw instanceof ArrayBuffer) {
|
|
532
|
+
text = new TextDecoder().decode(raw);
|
|
533
|
+
} else if (ArrayBuffer.isView(raw)) {
|
|
534
|
+
text = new TextDecoder().decode(raw);
|
|
535
|
+
} else if (typeof raw === "object") {
|
|
536
|
+
if (isValidAIClientMessage(raw)) {
|
|
537
|
+
return raw;
|
|
538
|
+
}
|
|
539
|
+
return null;
|
|
540
|
+
} else {
|
|
541
|
+
return null;
|
|
542
|
+
}
|
|
543
|
+
try {
|
|
544
|
+
const parsed = JSON.parse(text);
|
|
545
|
+
if (isValidAIClientMessage(parsed)) {
|
|
546
|
+
return parsed;
|
|
547
|
+
}
|
|
548
|
+
return null;
|
|
549
|
+
} catch {
|
|
550
|
+
return null;
|
|
551
|
+
}
|
|
552
|
+
};
|
|
553
|
+
var serializeAIMessage = (msg) => JSON.stringify(msg);
|
|
554
|
+
|
|
555
|
+
// src/react/ai/useAIStream.ts
|
|
556
|
+
var useAIStream = (path, conversationId) => {
|
|
557
|
+
const context = useAIStreamContext();
|
|
558
|
+
const standaloneRef = useRef2(null);
|
|
559
|
+
const isStandalone = !context;
|
|
560
|
+
if (isStandalone && !standaloneRef.current && path) {
|
|
561
|
+
const connection2 = createAIConnection(path);
|
|
562
|
+
const store2 = createAIMessageStore();
|
|
563
|
+
standaloneRef.current = { connection: connection2, store: store2 };
|
|
564
|
+
}
|
|
565
|
+
const resolved = context ?? standaloneRef.current;
|
|
566
|
+
if (!resolved) {
|
|
567
|
+
throw new Error("useAIStream requires either an AIStreamProvider or a path argument");
|
|
568
|
+
}
|
|
569
|
+
const { connection, store } = resolved;
|
|
570
|
+
useEffect2(() => {
|
|
571
|
+
if (!isStandalone) {
|
|
572
|
+
return;
|
|
573
|
+
}
|
|
574
|
+
const unsubscribe = connection.subscribe((msg) => {
|
|
575
|
+
const action = serverMessageToAction(msg);
|
|
576
|
+
if (action) {
|
|
577
|
+
store.dispatch(action);
|
|
578
|
+
}
|
|
579
|
+
});
|
|
580
|
+
return () => {
|
|
581
|
+
unsubscribe();
|
|
582
|
+
connection.close();
|
|
583
|
+
};
|
|
584
|
+
}, [connection, isStandalone, store]);
|
|
585
|
+
const state = useSyncExternalStore(store.subscribe, store.getSnapshot, store.getServerSnapshot);
|
|
586
|
+
const activeConvId = conversationId ?? state.activeConversationId;
|
|
587
|
+
const conversation = activeConvId ? state.conversations.get(activeConvId) : undefined;
|
|
588
|
+
const messages = conversation?.messages ?? [];
|
|
589
|
+
const send = useCallback((content) => {
|
|
590
|
+
const convId = activeConvId ?? generateId();
|
|
591
|
+
const msgId = generateId();
|
|
592
|
+
store.dispatch({
|
|
593
|
+
content,
|
|
594
|
+
conversationId: convId,
|
|
595
|
+
messageId: msgId,
|
|
596
|
+
type: "send"
|
|
597
|
+
});
|
|
598
|
+
connection.send({
|
|
599
|
+
content,
|
|
600
|
+
conversationId: convId,
|
|
601
|
+
type: "message"
|
|
602
|
+
});
|
|
603
|
+
}, [activeConvId, connection, store]);
|
|
604
|
+
const cancel = useCallback(() => {
|
|
605
|
+
if (activeConvId) {
|
|
606
|
+
store.dispatch({ type: "cancel" });
|
|
607
|
+
connection.send({ conversationId: activeConvId, type: "cancel" });
|
|
608
|
+
}
|
|
609
|
+
}, [activeConvId, connection, store]);
|
|
610
|
+
const branch = useCallback((messageId, content) => {
|
|
611
|
+
if (activeConvId) {
|
|
612
|
+
connection.send({
|
|
613
|
+
content,
|
|
614
|
+
conversationId: activeConvId,
|
|
615
|
+
messageId,
|
|
616
|
+
type: "branch"
|
|
617
|
+
});
|
|
618
|
+
}
|
|
619
|
+
}, [activeConvId, connection]);
|
|
620
|
+
return {
|
|
621
|
+
branch,
|
|
622
|
+
cancel,
|
|
623
|
+
error: state.error,
|
|
624
|
+
isStreaming: state.isStreaming,
|
|
625
|
+
messages,
|
|
626
|
+
send
|
|
627
|
+
};
|
|
628
|
+
};
|
|
629
|
+
export {
|
|
630
|
+
useAIStream,
|
|
631
|
+
AIStreamProvider
|
|
632
|
+
};
|
|
633
|
+
|
|
634
|
+
//# debugId=64EEC7ABDA71D1BE64756E2164756E21
|
|
635
|
+
//# sourceMappingURL=index.js.map
|