@absolutejs/absolute 0.19.0-beta.225 → 0.19.0-beta.227

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.
Files changed (75) hide show
  1. package/.absolutejs/eslint-cache +1 -0
  2. package/.absolutejs/prettier.cache.json +42 -19
  3. package/.absolutejs/vue-tsc.tsbuildinfo +1 -1
  4. package/.claude/settings.local.json +3 -1
  5. package/ROADMAP.md +0 -25
  6. package/dist/{Image-y5bdvj57.vue → Image-b3r6hxqk.vue} +6 -1
  7. package/dist/ai/index.js +529 -0
  8. package/dist/ai/index.js.map +13 -0
  9. package/dist/ai/providers/anthropic.js +396 -0
  10. package/dist/ai/providers/anthropic.js.map +10 -0
  11. package/dist/ai/providers/ollama.js +233 -0
  12. package/dist/ai/providers/ollama.js.map +10 -0
  13. package/dist/ai/providers/openai.js +355 -0
  14. package/dist/ai/providers/openai.js.map +10 -0
  15. package/dist/ai-client/angular/ai/index.js +532 -0
  16. package/dist/ai-client/react/ai/index.js +499 -0
  17. package/dist/ai-client/vue/ai/index.js +460 -0
  18. package/dist/angular/ai/index.js +668 -0
  19. package/dist/angular/ai/index.js.map +15 -0
  20. package/dist/angular/components/image.component.js +1 -1
  21. package/dist/angular/index.js +23 -3
  22. package/dist/angular/index.js.map +3 -3
  23. package/dist/build.js +64 -4
  24. package/dist/build.js.map +5 -5
  25. package/dist/index.js +66 -4
  26. package/dist/index.js.map +7 -7
  27. package/dist/react/ai/index.js +635 -0
  28. package/dist/react/ai/index.js.map +16 -0
  29. package/dist/react/components/index.js +11 -1
  30. package/dist/react/components/index.js.map +2 -2
  31. package/dist/react/hooks/index.js +11 -1
  32. package/dist/react/hooks/index.js.map +2 -2
  33. package/dist/react/index.js +23 -3
  34. package/dist/react/index.js.map +3 -3
  35. package/dist/src/ai/client/actions.d.ts +46 -0
  36. package/dist/src/ai/client/connection.d.ts +9 -0
  37. package/dist/src/ai/client/messageStore.d.ts +12 -0
  38. package/dist/src/ai/conversationManager.d.ts +11 -0
  39. package/dist/src/ai/index.d.ts +3 -0
  40. package/dist/src/ai/protocol.d.ts +4 -0
  41. package/dist/src/ai/providers/anthropic.d.ts +3 -0
  42. package/dist/src/ai/providers/ollama.d.ts +6 -0
  43. package/dist/src/ai/providers/openai.d.ts +7 -0
  44. package/dist/src/ai/streamAI.d.ts +2 -0
  45. package/dist/src/angular/ai/ai-stream.service.d.ts +15 -0
  46. package/dist/src/angular/ai/index.d.ts +1 -0
  47. package/dist/src/react/ai/AIStreamProvider.d.ts +13 -0
  48. package/dist/src/react/ai/index.d.ts +2 -0
  49. package/dist/src/react/ai/useAIStream.d.ts +8 -0
  50. package/dist/src/svelte/ai/createAIStream.d.ts +10 -0
  51. package/dist/src/svelte/ai/index.d.ts +1 -0
  52. package/dist/src/vue/ai/index.d.ts +1 -0
  53. package/dist/src/vue/ai/useAIStream.d.ts +22 -0
  54. package/dist/svelte/ai/index.js +590 -0
  55. package/dist/svelte/ai/index.js.map +15 -0
  56. package/dist/svelte/components/Image.svelte +6 -1
  57. package/dist/svelte/index.js +23 -3
  58. package/dist/svelte/index.js.map +3 -3
  59. package/dist/types/ai.d.ts +188 -0
  60. package/dist/types/anthropic.d.ts +18 -0
  61. package/dist/types/index.d.ts +1 -0
  62. package/dist/types/typeGuards.d.ts +3 -0
  63. package/dist/vue/ai/index.js +596 -0
  64. package/dist/vue/ai/index.js.map +15 -0
  65. package/dist/vue/components/Image.vue +6 -1
  66. package/dist/vue/components/index.js +12 -2
  67. package/dist/vue/components/index.js.map +1 -1
  68. package/dist/vue/index.js +23 -3
  69. package/dist/vue/index.js.map +3 -3
  70. package/package.json +36 -1
  71. package/scripts/build.ts +33 -0
  72. package/types/ai.ts +235 -0
  73. package/types/anthropic.ts +17 -0
  74. package/types/index.ts +1 -0
  75. package/types/typeGuards.ts +72 -1
@@ -0,0 +1,460 @@
1
+ var __legacyDecorateClassTS = function(decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
4
+ r = Reflect.decorate(decorators, target, key, desc);
5
+ else
6
+ for (var i = decorators.length - 1;i >= 0; i--)
7
+ if (d = decorators[i])
8
+ r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
9
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
10
+ };
11
+
12
+ // src/vue/ai/useAIStream.ts
13
+ import { onUnmounted, ref, shallowRef } from "vue";
14
+
15
+ // src/ai/client/actions.ts
16
+ var serverMessageToAction = (msg) => {
17
+ switch (msg.type) {
18
+ case "chunk":
19
+ return {
20
+ content: msg.content,
21
+ conversationId: msg.conversationId,
22
+ messageId: msg.messageId,
23
+ type: "chunk"
24
+ };
25
+ case "tool_status":
26
+ return {
27
+ conversationId: msg.conversationId,
28
+ input: msg.input,
29
+ messageId: msg.messageId,
30
+ name: msg.name,
31
+ result: msg.result,
32
+ status: msg.status,
33
+ type: "tool_status"
34
+ };
35
+ case "complete":
36
+ return {
37
+ conversationId: msg.conversationId,
38
+ messageId: msg.messageId,
39
+ type: "complete",
40
+ usage: msg.usage
41
+ };
42
+ case "error":
43
+ return { message: msg.message, type: "error" };
44
+ default:
45
+ return null;
46
+ }
47
+ };
48
+
49
+ // types/typeGuards.ts
50
+ var isValidAIServerMessage = (data) => {
51
+ if (!data || typeof data !== "object") {
52
+ return false;
53
+ }
54
+ if (!("type" in data) || typeof data.type !== "string") {
55
+ return false;
56
+ }
57
+ switch (data.type) {
58
+ case "chunk":
59
+ return "content" in data && typeof data.content === "string" && "messageId" in data && "conversationId" in data;
60
+ case "tool_status":
61
+ return "name" in data && "status" in data && "messageId" in data && "conversationId" in data;
62
+ case "complete":
63
+ return "messageId" in data && "conversationId" in data;
64
+ case "error":
65
+ return "message" in data && typeof data.message === "string";
66
+ default:
67
+ return false;
68
+ }
69
+ };
70
+
71
+ // src/ai/client/connection.ts
72
+ var WS_OPEN = 1;
73
+ var WS_NORMAL_CLOSURE = 1000;
74
+ var WS_CLOSED = 3;
75
+ var DEFAULT_PING_INTERVAL = 30000;
76
+ var RECONNECT_INITIAL_DELAY = 500;
77
+ var RECONNECT_POLL_INTERVAL = 300;
78
+ var DEFAULT_MAX_RECONNECT_ATTEMPTS = 60;
79
+ var noop = () => {};
80
+ var noopUnsubscribe = () => noop;
81
+ var NOOP_CONNECTION = {
82
+ close: noop,
83
+ send: noop,
84
+ subscribe: noopUnsubscribe,
85
+ getReadyState: () => WS_CLOSED
86
+ };
87
+ var buildWsUrl = (path) => {
88
+ const { hostname, port, protocol } = window.location;
89
+ const wsProtocol = protocol === "https:" ? "wss:" : "ws:";
90
+ const portSuffix = port ? `:${port}` : "";
91
+ return `${wsProtocol}//${hostname}${portSuffix}${path}`;
92
+ };
93
+ var parseServerMessage = (event) => {
94
+ let data;
95
+ try {
96
+ data = JSON.parse(String(event.data));
97
+ } catch {
98
+ return null;
99
+ }
100
+ if (data && typeof data === "object" && "type" in data && data.type === "pong") {
101
+ return null;
102
+ }
103
+ if (!isValidAIServerMessage(data)) {
104
+ return null;
105
+ }
106
+ return data;
107
+ };
108
+ var createAIConnection = (path, options = {}) => {
109
+ if (typeof window === "undefined") {
110
+ return NOOP_CONNECTION;
111
+ }
112
+ const shouldReconnect = options.reconnect !== false;
113
+ const pingInterval = options.pingInterval ?? DEFAULT_PING_INTERVAL;
114
+ const maxReconnectAttempts = options.maxReconnectAttempts ?? DEFAULT_MAX_RECONNECT_ATTEMPTS;
115
+ const listeners = new Set;
116
+ const connState = {
117
+ isConnected: false,
118
+ pingInterval: null,
119
+ reconnectAttempts: 0,
120
+ reconnectTimeout: null,
121
+ ws: null
122
+ };
123
+ const clearTimers = () => {
124
+ if (connState.pingInterval) {
125
+ clearInterval(connState.pingInterval);
126
+ connState.pingInterval = null;
127
+ }
128
+ if (connState.reconnectTimeout) {
129
+ clearTimeout(connState.reconnectTimeout);
130
+ connState.reconnectTimeout = null;
131
+ }
132
+ };
133
+ const scheduleReconnect = () => {
134
+ connState.reconnectAttempts++;
135
+ const delay = connState.reconnectAttempts === 1 ? RECONNECT_INITIAL_DELAY : RECONNECT_POLL_INTERVAL;
136
+ connState.reconnectTimeout = setTimeout(() => {
137
+ if (connState.reconnectAttempts > maxReconnectAttempts) {
138
+ return;
139
+ }
140
+ connect();
141
+ }, delay);
142
+ };
143
+ const connect = () => {
144
+ const url = buildWsUrl(path);
145
+ const wsInstance = new WebSocket(url, options.protocols);
146
+ wsInstance.onopen = () => {
147
+ connState.isConnected = true;
148
+ connState.reconnectAttempts = 0;
149
+ connState.pingInterval = setInterval(() => {
150
+ if (wsInstance.readyState === WS_OPEN && connState.isConnected) {
151
+ wsInstance.send(JSON.stringify({ type: "ping" }));
152
+ }
153
+ }, pingInterval);
154
+ };
155
+ wsInstance.onmessage = (event) => {
156
+ const message = parseServerMessage(event);
157
+ if (!message) {
158
+ return;
159
+ }
160
+ listeners.forEach((listener) => listener(message));
161
+ };
162
+ wsInstance.onclose = (event) => {
163
+ connState.isConnected = false;
164
+ clearTimers();
165
+ const shouldAttemptReconnect = shouldReconnect && event.code !== WS_NORMAL_CLOSURE && connState.reconnectAttempts < maxReconnectAttempts;
166
+ if (shouldAttemptReconnect) {
167
+ scheduleReconnect();
168
+ }
169
+ };
170
+ wsInstance.onerror = () => {};
171
+ connState.ws = wsInstance;
172
+ };
173
+ const send = (msg) => {
174
+ if (connState.ws?.readyState === WS_OPEN) {
175
+ connState.ws.send(JSON.stringify(msg));
176
+ }
177
+ };
178
+ const subscribe = (callback) => {
179
+ listeners.add(callback);
180
+ return () => {
181
+ listeners.delete(callback);
182
+ };
183
+ };
184
+ const close = () => {
185
+ clearTimers();
186
+ if (connState.ws) {
187
+ connState.ws.close(WS_NORMAL_CLOSURE);
188
+ connState.ws = null;
189
+ }
190
+ connState.isConnected = false;
191
+ listeners.clear();
192
+ };
193
+ const getReadyState = () => connState.ws?.readyState ?? WS_CLOSED;
194
+ connect();
195
+ return { close, getReadyState, send, subscribe };
196
+ };
197
+
198
+ // src/ai/client/messageStore.ts
199
+ var EMPTY_STATE = {
200
+ activeConversationId: null,
201
+ conversations: new Map,
202
+ error: null,
203
+ isStreaming: false
204
+ };
205
+ var initialActiveConversationId = null;
206
+ var initialError = null;
207
+ var freshState = () => ({
208
+ activeConversationId: initialActiveConversationId,
209
+ conversations: new Map,
210
+ error: initialError,
211
+ isStreaming: false
212
+ });
213
+ var findAssistantMessage = (conversation, messageId) => conversation.messages.find((msg) => msg.id === messageId && msg.role === "assistant");
214
+ var getOrCreate = (state, conversationId) => {
215
+ let conversation = state.conversations.get(conversationId);
216
+ if (!conversation) {
217
+ conversation = { id: conversationId, messages: [] };
218
+ state.conversations.set(conversationId, conversation);
219
+ }
220
+ return conversation;
221
+ };
222
+ var handleSend = (state, action) => {
223
+ const conversation = getOrCreate(state, action.conversationId);
224
+ const message = {
225
+ content: action.content,
226
+ conversationId: action.conversationId,
227
+ id: action.messageId,
228
+ role: "user",
229
+ timestamp: Date.now()
230
+ };
231
+ conversation.messages = [...conversation.messages, message];
232
+ state.activeConversationId = action.conversationId;
233
+ state.error = null;
234
+ state.isStreaming = true;
235
+ };
236
+ var handleChunk = (state, action) => {
237
+ const conversation = getOrCreate(state, action.conversationId);
238
+ const existing = findAssistantMessage(conversation, action.messageId);
239
+ if (existing) {
240
+ existing.content += action.content;
241
+ conversation.messages = [...conversation.messages];
242
+ return;
243
+ }
244
+ const message = {
245
+ content: action.content,
246
+ conversationId: action.conversationId,
247
+ id: action.messageId,
248
+ isStreaming: true,
249
+ role: "assistant",
250
+ timestamp: Date.now()
251
+ };
252
+ conversation.messages = [...conversation.messages, message];
253
+ };
254
+ var upsertToolCall = (message, toolCall) => {
255
+ if (!message.toolCalls) {
256
+ message.toolCalls = [toolCall];
257
+ return;
258
+ }
259
+ const existingIdx = message.toolCalls.findIndex((existing) => existing.name === toolCall.name);
260
+ if (existingIdx >= 0) {
261
+ message.toolCalls[existingIdx] = toolCall;
262
+ } else {
263
+ message.toolCalls = [...message.toolCalls, toolCall];
264
+ }
265
+ };
266
+ var handleToolStatus = (state, action) => {
267
+ const conversation = getOrCreate(state, action.conversationId);
268
+ const message = findAssistantMessage(conversation, action.messageId);
269
+ if (!message) {
270
+ return;
271
+ }
272
+ const toolCall = {
273
+ id: action.messageId,
274
+ input: action.input,
275
+ name: action.name,
276
+ result: action.status === "complete" ? action.result ?? "" : undefined
277
+ };
278
+ upsertToolCall(message, toolCall);
279
+ conversation.messages = [...conversation.messages];
280
+ };
281
+ var markMessageComplete = (conversation, messageId) => {
282
+ const message = findAssistantMessage(conversation, messageId);
283
+ if (message) {
284
+ message.isStreaming = false;
285
+ }
286
+ conversation.messages = [...conversation.messages];
287
+ };
288
+ var handleComplete = (state, action) => {
289
+ const conversation = state.conversations.get(action.conversationId);
290
+ if (conversation) {
291
+ markMessageComplete(conversation, action.messageId);
292
+ }
293
+ state.isStreaming = false;
294
+ };
295
+ var markConversationStreamsComplete = (conversation) => {
296
+ const streamingMessages = conversation.messages.filter((msg) => msg.isStreaming);
297
+ if (streamingMessages.length === 0) {
298
+ return;
299
+ }
300
+ for (const msg of streamingMessages) {
301
+ msg.isStreaming = false;
302
+ }
303
+ conversation.messages = [...conversation.messages];
304
+ };
305
+ var markAllStreamsComplete = (state) => {
306
+ for (const [, conversation] of state.conversations) {
307
+ markConversationStreamsComplete(conversation);
308
+ }
309
+ };
310
+ var handleBranch = (state, action) => {
311
+ const source = state.conversations.get(action.oldConversationId);
312
+ if (!source) {
313
+ return;
314
+ }
315
+ const cutoffIndex = source.messages.findIndex((msg) => msg.id === action.fromMessageId);
316
+ if (cutoffIndex < 0) {
317
+ return;
318
+ }
319
+ const branchedMessages = source.messages.slice(0, cutoffIndex + 1).map((msg) => ({ ...msg, conversationId: action.newConversationId }));
320
+ const newConversation = {
321
+ id: action.newConversationId,
322
+ messages: branchedMessages
323
+ };
324
+ state.conversations.set(action.newConversationId, newConversation);
325
+ state.activeConversationId = action.newConversationId;
326
+ };
327
+ var dispatch = (state, subscribers, action) => {
328
+ switch (action.type) {
329
+ case "send":
330
+ handleSend(state, action);
331
+ break;
332
+ case "chunk":
333
+ handleChunk(state, action);
334
+ break;
335
+ case "tool_status":
336
+ handleToolStatus(state, action);
337
+ break;
338
+ case "complete":
339
+ handleComplete(state, action);
340
+ break;
341
+ case "error":
342
+ state.error = action.message;
343
+ state.isStreaming = false;
344
+ break;
345
+ case "cancel":
346
+ state.isStreaming = false;
347
+ markAllStreamsComplete(state);
348
+ break;
349
+ case "branch":
350
+ handleBranch(state, action);
351
+ break;
352
+ case "set_conversation":
353
+ state.activeConversationId = action.conversationId;
354
+ break;
355
+ }
356
+ Object.assign(state, { conversations: new Map(state.conversations) });
357
+ subscribers.forEach((callback) => callback());
358
+ };
359
+ var createAIMessageStore = () => {
360
+ const state = freshState();
361
+ const subscribers = new Set;
362
+ return {
363
+ dispatch: (action) => dispatch(state, subscribers, action),
364
+ getServerSnapshot: () => EMPTY_STATE,
365
+ getSnapshot: () => state,
366
+ subscribe: (callback) => {
367
+ subscribers.add(callback);
368
+ return () => {
369
+ subscribers.delete(callback);
370
+ };
371
+ }
372
+ };
373
+ };
374
+
375
+ // src/ai/protocol.ts
376
+ var generateId = () => crypto.randomUUID();
377
+
378
+ // src/vue/ai/useAIStream.ts
379
+ var AIStreamKey = Symbol("ai-stream");
380
+ var useAIStream = (path, conversationId) => {
381
+ const connection = createAIConnection(path);
382
+ const store = createAIMessageStore();
383
+ const messages = shallowRef([]);
384
+ const isStreaming = ref(false);
385
+ const error = ref(null);
386
+ const activeConversationId = ref(conversationId ?? null);
387
+ let unsubscribeConnection = null;
388
+ let unsubscribeStore = null;
389
+ const syncState = () => {
390
+ const snapshot = store.getSnapshot();
391
+ const convId = activeConversationId.value ?? snapshot.activeConversationId;
392
+ const conversation = convId ? snapshot.conversations.get(convId) : undefined;
393
+ messages.value = conversation?.messages ?? [];
394
+ isStreaming.value = snapshot.isStreaming;
395
+ error.value = snapshot.error;
396
+ if (convId) {
397
+ activeConversationId.value = convId;
398
+ }
399
+ };
400
+ unsubscribeStore = store.subscribe(syncState);
401
+ unsubscribeConnection = connection.subscribe((msg) => {
402
+ const action = serverMessageToAction(msg);
403
+ if (action) {
404
+ store.dispatch(action);
405
+ }
406
+ });
407
+ const send = (content) => {
408
+ const convId = activeConversationId.value ?? generateId();
409
+ const msgId = generateId();
410
+ store.dispatch({
411
+ content,
412
+ conversationId: convId,
413
+ messageId: msgId,
414
+ type: "send"
415
+ });
416
+ connection.send({
417
+ content,
418
+ conversationId: convId,
419
+ type: "message"
420
+ });
421
+ };
422
+ const cancel = () => {
423
+ if (activeConversationId.value) {
424
+ store.dispatch({ type: "cancel" });
425
+ connection.send({
426
+ conversationId: activeConversationId.value,
427
+ type: "cancel"
428
+ });
429
+ }
430
+ };
431
+ const branch = (messageId, content) => {
432
+ if (activeConversationId.value) {
433
+ connection.send({
434
+ content,
435
+ conversationId: activeConversationId.value,
436
+ messageId,
437
+ type: "branch"
438
+ });
439
+ }
440
+ };
441
+ const destroy = () => {
442
+ unsubscribeConnection?.();
443
+ unsubscribeStore?.();
444
+ connection.close();
445
+ };
446
+ onUnmounted(destroy);
447
+ return {
448
+ branch,
449
+ cancel,
450
+ destroy,
451
+ error,
452
+ isStreaming,
453
+ messages,
454
+ send
455
+ };
456
+ };
457
+ export {
458
+ useAIStream,
459
+ AIStreamKey
460
+ };