@axium/ai 0.0.0-stage → 0.0.1
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/LICENSE.md +157 -0
- package/README.md +2 -2
- package/db.json +114 -0
- package/dist/.locale.d.ts +300 -0
- package/dist/.locale.js +1 -0
- package/dist/client/cli.d.ts +1 -0
- package/dist/client/cli.js +224 -0
- package/dist/client/config.d.ts +196 -0
- package/dist/client/config.js +55 -0
- package/dist/client/hooks.d.ts +3 -0
- package/dist/client/hooks.js +32 -0
- package/dist/client/local.d.ts +17 -0
- package/dist/client/local.js +112 -0
- package/dist/common.d.ts +1318 -0
- package/dist/common.js +569 -0
- package/dist/hooks.d.ts +13 -0
- package/dist/hooks.js +34 -0
- package/dist/hub.d.ts +4 -0
- package/dist/hub.js +75 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/integrations/notes.d.ts +1 -0
- package/dist/integrations/notes.js +104 -0
- package/dist/integrations/tasks.d.ts +1 -0
- package/dist/integrations/tasks.js +157 -0
- package/dist/loop.d.ts +75 -0
- package/dist/loop.js +238 -0
- package/dist/memory.d.ts +107 -0
- package/dist/memory.js +193 -0
- package/dist/node/chat.d.ts +50 -0
- package/dist/node/chat.js +60 -0
- package/dist/node/llama.d.ts +115 -0
- package/dist/node/llama.js +385 -0
- package/dist/node/markdown.d.ts +6 -0
- package/dist/node/markdown.js +285 -0
- package/dist/node/models.d.ts +34 -0
- package/dist/node/models.js +287 -0
- package/dist/node/web.d.ts +1 -0
- package/dist/node/web.js +203 -0
- package/dist/server/api/conversations.d.ts +1 -0
- package/dist/server/api/conversations.js +216 -0
- package/dist/server/api/gateway.d.ts +1 -0
- package/dist/server/api/gateway.js +113 -0
- package/dist/server/api/memories.d.ts +1 -0
- package/dist/server/api/memories.js +129 -0
- package/dist/server/api/models.d.ts +1 -0
- package/dist/server/api/models.js +161 -0
- package/dist/server/api/temporary.d.ts +1 -0
- package/dist/server/api/temporary.js +50 -0
- package/dist/server/api/timers.d.ts +1 -0
- package/dist/server/api/timers.js +27 -0
- package/dist/server/api/voice.d.ts +1 -0
- package/dist/server/api/voice.js +188 -0
- package/dist/server/api.d.ts +1 -0
- package/dist/server/api.js +157 -0
- package/dist/server/generation.d.ts +38 -0
- package/dist/server/generation.js +299 -0
- package/dist/server/memory.d.ts +7 -0
- package/dist/server/memory.js +218 -0
- package/dist/server/models.d.ts +13 -0
- package/dist/server/models.js +59 -0
- package/dist/server/preferences.d.ts +3 -0
- package/dist/server/preferences.js +12 -0
- package/dist/server/timers.d.ts +7 -0
- package/dist/server/timers.js +66 -0
- package/dist/speech.d.ts +17 -0
- package/dist/speech.js +69 -0
- package/dist/timers.d.ts +1 -0
- package/dist/timers.js +67 -0
- package/dist/tools.d.ts +70 -0
- package/dist/tools.js +84 -0
- package/lib/ChatMessage.svelte +331 -0
- package/lib/Composer.svelte +234 -0
- package/lib/Conversation.svelte +433 -0
- package/lib/ConversationMenu.svelte +18 -0
- package/lib/ModelActions.svelte +71 -0
- package/lib/ModelOptions.svelte +101 -0
- package/lib/ModelStatus.svelte +49 -0
- package/lib/ToolCall.svelte +171 -0
- package/lib/VoiceMode.svelte +113 -0
- package/lib/actions.ts +21 -0
- package/lib/index.ts +7 -0
- package/lib/live.svelte.ts +113 -0
- package/lib/markdown.ts +40 -0
- package/lib/preferences.ts +13 -0
- package/lib/transition.ts +24 -0
- package/lib/tsconfig.json +14 -0
- package/lib/voice.svelte.ts +476 -0
- package/locales/en.json +316 -0
- package/package.json +139 -5
- package/routes/ai/+layout.svelte +177 -0
- package/routes/ai/+layout.ts +26 -0
- package/routes/ai/+page.svelte +109 -0
- package/routes/ai/TimerAlerts.svelte +51 -0
- package/routes/ai/Timers.svelte +121 -0
- package/routes/ai/[id]/+page.svelte +18 -0
- package/routes/ai/[id]/+page.ts +14 -0
- package/routes/ai/memory/+page.svelte +313 -0
- package/routes/ai/memory/+page.ts +10 -0
- package/routes/ai/memory/MemoryForm.svelte +111 -0
- package/routes/ai/models/+page.svelte +199 -0
- package/routes/ai/models/+page.ts +5 -0
- package/routes/ai/models/AddModel.svelte +240 -0
- package/routes/ai/models/MemoryMeter.svelte +120 -0
- package/routes/ai/models/Roles.svelte +159 -0
- package/routes/ai/models/[id]/+page.svelte +331 -0
- package/routes/ai/models/[id]/+page.ts +6 -0
- package/routes/ai/timers.svelte.ts +94 -0
- package/routes/tsconfig.json +14 -0
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
import { getConfig } from '@axium/core';
|
|
2
|
+
import { getUser } from '@axium/server/auth';
|
|
3
|
+
import { callAPI } from '@axium/server/call';
|
|
4
|
+
import { database } from '@axium/server/database';
|
|
5
|
+
import { io as socketServer } from '@axium/server/socket';
|
|
6
|
+
import * as io from 'ioium';
|
|
7
|
+
import { randomUUID } from 'node:crypto';
|
|
8
|
+
import * as loop from '../loop.js';
|
|
9
|
+
import { chat, streamChat } from '../node/chat.js';
|
|
10
|
+
import features from '@axium/core/features';
|
|
11
|
+
import { getMemories } from './memory.js';
|
|
12
|
+
import { getPreferences } from './preferences.js';
|
|
13
|
+
import * as timers from './timers.js';
|
|
14
|
+
import { getRow, learnCapabilities, resolveRole } from './models.js';
|
|
15
|
+
import { pick } from 'utilium';
|
|
16
|
+
export function toMessage(row) {
|
|
17
|
+
return {
|
|
18
|
+
...row,
|
|
19
|
+
toolCalls: row.toolCalls,
|
|
20
|
+
usage: row.usage,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export function emitMessage(userId, message) {
|
|
24
|
+
socketServer?.to('user:' + userId).emit('ai.message', message);
|
|
25
|
+
}
|
|
26
|
+
export function emitRemoved(userId, removed) {
|
|
27
|
+
socketServer?.to('user:' + userId).emit('ai.message.remove', removed);
|
|
28
|
+
}
|
|
29
|
+
/** Running responses, by conversation ID */
|
|
30
|
+
const active = new Map();
|
|
31
|
+
export function isGenerating(conversationId) {
|
|
32
|
+
return active.has(conversationId);
|
|
33
|
+
}
|
|
34
|
+
/** Stops the response being generated in a conversation, if it belongs to `userId` */
|
|
35
|
+
export function cancel(conversationId, userId) {
|
|
36
|
+
const generation = active.get(conversationId);
|
|
37
|
+
if (generation?.userId != userId)
|
|
38
|
+
return false;
|
|
39
|
+
generation.controller.abort();
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
/** Fills in the text streamed so far, which is only saved once the response ends */
|
|
43
|
+
export function withProgress(message) {
|
|
44
|
+
const progress = active.get(message.conversationId)?.progress;
|
|
45
|
+
if (message.status != 'pending' || progress?.messageId != message.id)
|
|
46
|
+
return message;
|
|
47
|
+
return { ...message, content: progress.content, reasoning: progress.reasoning || null };
|
|
48
|
+
}
|
|
49
|
+
function toRow({ toolCalls, usage, ...changes }) {
|
|
50
|
+
return {
|
|
51
|
+
...changes,
|
|
52
|
+
...(toolCalls !== undefined && { toolCalls: toolCalls && JSON.stringify(toolCalls) }),
|
|
53
|
+
...(usage !== undefined && { usage: usage && JSON.stringify(usage) }),
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
function savedStore(conversation) {
|
|
57
|
+
return {
|
|
58
|
+
userId: conversation.userId,
|
|
59
|
+
conversationId: conversation.id,
|
|
60
|
+
async add(message) {
|
|
61
|
+
const row = await database
|
|
62
|
+
.insertInto('ai_messages')
|
|
63
|
+
.values({ ...toRow(message), role: message.role, conversationId: conversation.id })
|
|
64
|
+
.returningAll()
|
|
65
|
+
.executeTakeFirstOrThrow();
|
|
66
|
+
const added = toMessage(row);
|
|
67
|
+
emitMessage(conversation.userId, added);
|
|
68
|
+
return added;
|
|
69
|
+
},
|
|
70
|
+
async update(id, changes) {
|
|
71
|
+
const row = await database
|
|
72
|
+
.updateTable('ai_messages')
|
|
73
|
+
.set(toRow(changes))
|
|
74
|
+
.where('id', '=', id)
|
|
75
|
+
.returningAll()
|
|
76
|
+
.executeTakeFirstOrThrow();
|
|
77
|
+
const updated = toMessage(row);
|
|
78
|
+
emitMessage(conversation.userId, updated);
|
|
79
|
+
return updated;
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
/** Keeps messages only long enough to send them; the client holds temporary conversations */
|
|
84
|
+
function temporaryStore(userId, conversationId, messages) {
|
|
85
|
+
const known = new Map(messages.map(message => [message.id, message]));
|
|
86
|
+
return {
|
|
87
|
+
userId,
|
|
88
|
+
conversationId,
|
|
89
|
+
add(message) {
|
|
90
|
+
const added = {
|
|
91
|
+
id: randomUUID(),
|
|
92
|
+
conversationId,
|
|
93
|
+
created: new Date(),
|
|
94
|
+
content: '',
|
|
95
|
+
reasoning: null,
|
|
96
|
+
toolCalls: null,
|
|
97
|
+
toolCallId: null,
|
|
98
|
+
status: 'done',
|
|
99
|
+
error: null,
|
|
100
|
+
usage: null,
|
|
101
|
+
voice: false,
|
|
102
|
+
...message,
|
|
103
|
+
};
|
|
104
|
+
known.set(added.id, added);
|
|
105
|
+
emitMessage(userId, added);
|
|
106
|
+
return Promise.resolve(added);
|
|
107
|
+
},
|
|
108
|
+
update(id, changes) {
|
|
109
|
+
const existing = known.get(id);
|
|
110
|
+
if (!existing)
|
|
111
|
+
return Promise.reject(new ReferenceError(`Unknown message ${id}`));
|
|
112
|
+
const updated = { ...existing, ...changes };
|
|
113
|
+
known.set(id, updated);
|
|
114
|
+
emitMessage(userId, updated);
|
|
115
|
+
return Promise.resolve(updated);
|
|
116
|
+
},
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
/** Saves tools the user allowed in every conversation */
|
|
120
|
+
async function allowAlways(userId, preferences, names) {
|
|
121
|
+
for (const name of names)
|
|
122
|
+
preferences.tools.approvals[name] = 'allow';
|
|
123
|
+
await database
|
|
124
|
+
.insertInto('app_preferences')
|
|
125
|
+
.values({ userId, appId: 'ai', data: preferences })
|
|
126
|
+
.onConflict(conflict => conflict.columns(['userId', 'appId']).doUpdateSet({ data: preferences }))
|
|
127
|
+
.execute();
|
|
128
|
+
}
|
|
129
|
+
/** Responds with the server's model, streaming text to the user. Never throws. */
|
|
130
|
+
async function respond(init) {
|
|
131
|
+
const { store } = init;
|
|
132
|
+
const config = getConfig('@axium/ai');
|
|
133
|
+
const controller = new AbortController();
|
|
134
|
+
const signal = AbortSignal.any([controller.signal, AbortSignal.timeout(config.timeout * 1000)]);
|
|
135
|
+
const progress = { messageId: init.pending.id, content: '', reasoning: '' };
|
|
136
|
+
active.set(store.conversationId, { controller, userId: store.userId, progress });
|
|
137
|
+
try {
|
|
138
|
+
const id = init.model ?? (await resolveRole('chat'));
|
|
139
|
+
const model = id && (await getRow(id));
|
|
140
|
+
if (!model)
|
|
141
|
+
throw new Error('No model is available');
|
|
142
|
+
const [preferences, user] = await Promise.all([getPreferences(store.userId), getUser(store.userId)]);
|
|
143
|
+
const remembers = preferences.tools.memory && features('ai-memory') && !init.anonymous;
|
|
144
|
+
let streaming = false;
|
|
145
|
+
await loop.respond({
|
|
146
|
+
store,
|
|
147
|
+
model,
|
|
148
|
+
defaultSystemPrompt: config.system_prompt,
|
|
149
|
+
maxSteps: config.max_steps,
|
|
150
|
+
...pick(init, 'reasoning', 'pending', 'history', 'decisions', 'allowedTools', 'allowForChat', 'anonymous', 'temporary'),
|
|
151
|
+
complete: request => streamChat({ ...request, model: model.id }),
|
|
152
|
+
preferences,
|
|
153
|
+
timeZone: init.timeZone ?? user.preferences.time_zone,
|
|
154
|
+
homeTimeZone: user.preferences.time_zone,
|
|
155
|
+
memories: remembers ? await getMemories(store.userId) : undefined,
|
|
156
|
+
context: {
|
|
157
|
+
userId: user.id,
|
|
158
|
+
conversationId: init.temporary ? undefined : store.conversationId,
|
|
159
|
+
signal,
|
|
160
|
+
api: (method, endpoint, data, ...params) => callAPI(user, method, endpoint, data, ...params),
|
|
161
|
+
timers: timers.forUser(user.id),
|
|
162
|
+
},
|
|
163
|
+
allowAlways: names => allowAlways(store.userId, preferences, names),
|
|
164
|
+
progress,
|
|
165
|
+
onText(field, text, position) {
|
|
166
|
+
socketServer
|
|
167
|
+
?.to('user:' + store.userId)
|
|
168
|
+
.emit('ai.delta', { conversationId: store.conversationId, messageId: progress.messageId, field, text, position });
|
|
169
|
+
if (streaming)
|
|
170
|
+
return;
|
|
171
|
+
streaming = true;
|
|
172
|
+
void learnCapabilities(model.id);
|
|
173
|
+
},
|
|
174
|
+
onDone: content => init.onDone?.(model.id, content),
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
catch (error) {
|
|
178
|
+
io.warn(`AI: could not start a response in ${store.conversationId}: ${io.errorText(error)}`);
|
|
179
|
+
await store
|
|
180
|
+
.update(init.pending.id, { status: 'error', error: io.errorText(error) })
|
|
181
|
+
.catch(error => io.error(`AI: could not finish response in ${store.conversationId}: ${io.errorText(error)}`));
|
|
182
|
+
}
|
|
183
|
+
finally {
|
|
184
|
+
active.delete(store.conversationId);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
async function savedHistory(conversationId, exclude) {
|
|
188
|
+
const rows = await database
|
|
189
|
+
.selectFrom('ai_messages')
|
|
190
|
+
.selectAll()
|
|
191
|
+
.where('conversationId', '=', conversationId)
|
|
192
|
+
.where('id', '!=', exclude)
|
|
193
|
+
.orderBy('created')
|
|
194
|
+
.execute();
|
|
195
|
+
return rows.map(toMessage);
|
|
196
|
+
}
|
|
197
|
+
/** Responds into the pending `message` of a saved conversation. Never throws. */
|
|
198
|
+
export async function generate(conversation, message, { decisions, timeZone }) {
|
|
199
|
+
await respond({
|
|
200
|
+
timeZone,
|
|
201
|
+
store: savedStore(conversation),
|
|
202
|
+
model: conversation.model,
|
|
203
|
+
reasoning: conversation.reasoning,
|
|
204
|
+
pending: toMessage(message),
|
|
205
|
+
history: () => savedHistory(conversation.id, message.id),
|
|
206
|
+
decisions,
|
|
207
|
+
allowedTools: conversation.allowedTools,
|
|
208
|
+
async allowForChat(names) {
|
|
209
|
+
const updated = await database
|
|
210
|
+
.updateTable('ai_conversations')
|
|
211
|
+
.set({ allowedTools: names })
|
|
212
|
+
.where('id', '=', conversation.id)
|
|
213
|
+
.returningAll()
|
|
214
|
+
.executeTakeFirstOrThrow();
|
|
215
|
+
socketServer?.to('user:' + conversation.userId).emit('ai.conversation', updated);
|
|
216
|
+
},
|
|
217
|
+
onDone: (model, content) => void nameConversation(conversation, model, content),
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
/** Responds into `pending` for a temporary conversation, which is never saved. Never throws. */
|
|
221
|
+
export async function generateTemporary(userId, init, pending, timeZone) {
|
|
222
|
+
const history = init.messages.map((message) => ({
|
|
223
|
+
conversationId: pending.conversationId,
|
|
224
|
+
reasoning: null,
|
|
225
|
+
error: null,
|
|
226
|
+
usage: null,
|
|
227
|
+
...message,
|
|
228
|
+
toolCalls: message.toolCalls ?? null,
|
|
229
|
+
toolCallId: message.toolCallId ?? null,
|
|
230
|
+
status: message.status ?? 'done',
|
|
231
|
+
}));
|
|
232
|
+
await respond({
|
|
233
|
+
store: temporaryStore(userId, pending.conversationId, [...history, pending]),
|
|
234
|
+
model: init.model ?? null,
|
|
235
|
+
reasoning: init.reasoning,
|
|
236
|
+
pending,
|
|
237
|
+
history: () => Promise.resolve(history),
|
|
238
|
+
decisions: init.decisions,
|
|
239
|
+
allowedTools: init.allowedTools,
|
|
240
|
+
anonymous: init.anonymous,
|
|
241
|
+
temporary: true,
|
|
242
|
+
timeZone,
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
const titlePrompt = 'Write a title of 2 to 6 words for the conversation below. Reply with only the title.';
|
|
246
|
+
/** Titles a conversation from its first exchange, unless it already has a title from a model or the user */
|
|
247
|
+
async function nameConversation(conversation, model, reply) {
|
|
248
|
+
if (conversation.titleSource != 'none')
|
|
249
|
+
return;
|
|
250
|
+
try {
|
|
251
|
+
const first = await database
|
|
252
|
+
.selectFrom('ai_messages')
|
|
253
|
+
.select('content')
|
|
254
|
+
.where('conversationId', '=', conversation.id)
|
|
255
|
+
.where('role', '=', 'user')
|
|
256
|
+
.orderBy('created')
|
|
257
|
+
.limit(1)
|
|
258
|
+
.executeTakeFirst();
|
|
259
|
+
if (!first)
|
|
260
|
+
return;
|
|
261
|
+
const result = await chat({
|
|
262
|
+
model,
|
|
263
|
+
reasoning: 'none',
|
|
264
|
+
maxTokens: 24,
|
|
265
|
+
signal: AbortSignal.timeout(30_000),
|
|
266
|
+
messages: [
|
|
267
|
+
{ role: 'system', content: titlePrompt },
|
|
268
|
+
{ role: 'user', content: `User: ${first.content.slice(0, 2000)}\n\nAssistant: ${reply.slice(0, 2000)}` },
|
|
269
|
+
],
|
|
270
|
+
});
|
|
271
|
+
const title = result.content
|
|
272
|
+
.split('\n')
|
|
273
|
+
.find(line => line.trim())
|
|
274
|
+
?.replace(/^[\s"'*#]+|[\s"'*.]+$/g, '')
|
|
275
|
+
.slice(0, 100);
|
|
276
|
+
if (!title)
|
|
277
|
+
return;
|
|
278
|
+
const updated = await database
|
|
279
|
+
.updateTable('ai_conversations')
|
|
280
|
+
.set({ title, titleSource: 'auto' })
|
|
281
|
+
.where('id', '=', conversation.id)
|
|
282
|
+
.where('titleSource', '=', 'none')
|
|
283
|
+
.returningAll()
|
|
284
|
+
.executeTakeFirst();
|
|
285
|
+
if (updated)
|
|
286
|
+
socketServer?.to('user:' + conversation.userId).emit('ai.conversation', updated);
|
|
287
|
+
}
|
|
288
|
+
catch (error) {
|
|
289
|
+
io.debug(`AI: could not title ${conversation.id}: ${io.errorText(error)}`);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
/** Marks responses and tool calls left pending by a previous process as failed */
|
|
293
|
+
export async function failInterrupted() {
|
|
294
|
+
await database
|
|
295
|
+
.updateTable('ai_messages')
|
|
296
|
+
.set({ status: 'error', error: 'Interrupted by a server restart' })
|
|
297
|
+
.where('status', '=', 'pending')
|
|
298
|
+
.execute();
|
|
299
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import * as memory from '../memory.js';
|
|
2
|
+
export declare function getMemories(userId: string): Promise<memory.Node[]>;
|
|
3
|
+
/** Whether the server allows periodic updates */
|
|
4
|
+
export declare function updatesEnabled(): boolean;
|
|
5
|
+
export declare function getStatus(userId: string, running?: boolean): Promise<memory.Updates>;
|
|
6
|
+
/** Starts an update for the user now */
|
|
7
|
+
export declare function startUpdate(userId: string): Promise<void>;
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { getConfig } from '@axium/core';
|
|
2
|
+
import features from '@axium/core/features';
|
|
3
|
+
import { getUser } from '@axium/server/auth';
|
|
4
|
+
import { callAPI } from '@axium/server/call';
|
|
5
|
+
import { database } from '@axium/server/database';
|
|
6
|
+
import * as jobs from '@axium/server/jobs';
|
|
7
|
+
import { io as socketServer } from '@axium/server/socket';
|
|
8
|
+
import * as io from 'ioium';
|
|
9
|
+
import { sql } from 'kysely';
|
|
10
|
+
import * as z from 'zod';
|
|
11
|
+
import * as ai from '../common.js';
|
|
12
|
+
import * as memory from '../memory.js';
|
|
13
|
+
import { chat } from '../node/chat.js';
|
|
14
|
+
import { resolveRole } from './models.js';
|
|
15
|
+
import { getPreferences } from './preferences.js';
|
|
16
|
+
export async function getMemories(userId) {
|
|
17
|
+
return await database.selectFrom('ai_memories').selectAll().where('userId', '=', userId).orderBy('id').$castTo().execute();
|
|
18
|
+
}
|
|
19
|
+
/** Whether the server allows periodic updates */
|
|
20
|
+
export function updatesEnabled() {
|
|
21
|
+
return features('ai-memory') && features('ai-memory-updates');
|
|
22
|
+
}
|
|
23
|
+
/** The hour of the day updates run at, in the user's home time zone */
|
|
24
|
+
const updateHour = 4;
|
|
25
|
+
const frequencyDays = { daily: 1, weekly: 7 };
|
|
26
|
+
/** The next time at `updateHour` that is in the future, moving forward `days` when today's has passed */
|
|
27
|
+
function nextUpdate(days, timeZone = 'UTC') {
|
|
28
|
+
const now = Temporal.Now.zonedDateTimeISO(timeZone);
|
|
29
|
+
let next = now.withPlainTime({ hour: updateHour });
|
|
30
|
+
if (Temporal.ZonedDateTime.compare(next, now) <= 0)
|
|
31
|
+
next = next.add({ days });
|
|
32
|
+
return new Date(next.epochMilliseconds);
|
|
33
|
+
}
|
|
34
|
+
/** Characters of conversation text an update reads at most */
|
|
35
|
+
const maxTranscript = 40_000;
|
|
36
|
+
/** Characters of a single message an update reads at most */
|
|
37
|
+
const maxMessage = 4000;
|
|
38
|
+
/** How far back the first update looks, in milliseconds */
|
|
39
|
+
const firstLookback = 7 * 86_400_000;
|
|
40
|
+
function instructions(today, limit, maxLength) {
|
|
41
|
+
return [
|
|
42
|
+
'You maintain the long-term memory that an AI assistant keeps about its user. ' +
|
|
43
|
+
"You get the current memories and the user's recent conversations with the assistant, and return the changes to make.",
|
|
44
|
+
'- add: lasting things the conversations show about the user that no memory covers yet, such as preferences, facts about their life, ' +
|
|
45
|
+
'people they mention or ongoing projects. Skip trivia, one-off requests and anything that only mattered in one conversation. ' +
|
|
46
|
+
'Only add what the user said or clearly confirmed, not what the assistant suggested.',
|
|
47
|
+
'- update: memories the conversations show are wrong or out of date, and memories that should absorb a duplicate. ' +
|
|
48
|
+
'Give only the fields that change; content replaces the old content.',
|
|
49
|
+
'- delete: duplicates already merged into another memory with update, and memories that are clearly no longer true, ' +
|
|
50
|
+
'such as plans whose date has passed. Never delete memories written by the user.',
|
|
51
|
+
`Memories have a kebab-case ID, a one-line description and optional content, usually a short Markdown list of facts written in the third person, at most ${maxLength} characters. ` +
|
|
52
|
+
`There can be at most ${limit} memories. Change as little as possible; when nothing needs to change, return empty lists. Today is ${today}.`,
|
|
53
|
+
].join('\n\n');
|
|
54
|
+
}
|
|
55
|
+
/** Conversations as text, oldest first, keeping the newest messages that fit */
|
|
56
|
+
function transcript(messages) {
|
|
57
|
+
const kept = [];
|
|
58
|
+
let budget = maxTranscript;
|
|
59
|
+
for (const message of messages) {
|
|
60
|
+
const content = message.content.slice(0, maxMessage);
|
|
61
|
+
if (content.length > budget)
|
|
62
|
+
break;
|
|
63
|
+
budget -= content.length;
|
|
64
|
+
kept.push({ ...message, content });
|
|
65
|
+
}
|
|
66
|
+
const conversations = Map.groupBy(kept.reverse(), message => message.conversationId);
|
|
67
|
+
return Array.from(conversations.values(), group => {
|
|
68
|
+
const lines = group.map(({ role, content, created }) => `${role == 'user' ? 'User' : 'Assistant'} (${created.toISOString()}): ${content}`);
|
|
69
|
+
return `<conversation title=${JSON.stringify(group[0].title)}>\n${lines.join('\n')}\n</conversation>`;
|
|
70
|
+
}).join('\n\n');
|
|
71
|
+
}
|
|
72
|
+
/** Reviews the user's conversations since the last update and applies the changes the model proposes */
|
|
73
|
+
async function update(userId, signal) {
|
|
74
|
+
const started = new Date();
|
|
75
|
+
const user = await getUser(userId);
|
|
76
|
+
const previous = await database.selectFrom('ai_memory_updates').select('ran').where('userId', '=', userId).executeTakeFirst();
|
|
77
|
+
const since = previous?.ran ?? new Date(started.getTime() - firstLookback);
|
|
78
|
+
const messages = await database
|
|
79
|
+
.selectFrom('ai_messages')
|
|
80
|
+
.innerJoin('ai_conversations', 'ai_conversations.id', 'ai_messages.conversationId')
|
|
81
|
+
.select(['ai_messages.conversationId', 'ai_conversations.title', 'ai_messages.role', 'ai_messages.content', 'ai_messages.created'])
|
|
82
|
+
.where('ai_conversations.userId', '=', userId)
|
|
83
|
+
.where('ai_messages.role', 'in', ['user', 'assistant'])
|
|
84
|
+
.where('ai_messages.status', '=', 'done')
|
|
85
|
+
.where('ai_messages.content', '!=', '')
|
|
86
|
+
.where('ai_messages.created', '>', since)
|
|
87
|
+
.orderBy('ai_messages.created', 'desc')
|
|
88
|
+
.execute();
|
|
89
|
+
const applied = { added: [], updated: [], deleted: [] };
|
|
90
|
+
let summary = null;
|
|
91
|
+
if (messages.length) {
|
|
92
|
+
const model = await resolveRole('chat');
|
|
93
|
+
if (!model)
|
|
94
|
+
throw new Error('No model is available');
|
|
95
|
+
const { max_memories, max_memory_length } = getConfig('@axium/ai');
|
|
96
|
+
const memories = await getMemories(userId);
|
|
97
|
+
const today = Temporal.Now.zonedDateTimeISO(user.preferences.time_zone ?? 'UTC')
|
|
98
|
+
.toPlainDate()
|
|
99
|
+
.toString();
|
|
100
|
+
const current = memories.map(({ id, description, content, source }) => ({
|
|
101
|
+
id,
|
|
102
|
+
description,
|
|
103
|
+
content: content || undefined,
|
|
104
|
+
...(source == 'user' && { writtenByUser: true }),
|
|
105
|
+
}));
|
|
106
|
+
const result = await chat({
|
|
107
|
+
model,
|
|
108
|
+
reasoning: 'default',
|
|
109
|
+
schema: z.toJSONSchema(memory.Changes),
|
|
110
|
+
signal,
|
|
111
|
+
messages: [
|
|
112
|
+
{ role: 'system', content: instructions(today, max_memories, max_memory_length) },
|
|
113
|
+
{
|
|
114
|
+
role: 'user',
|
|
115
|
+
content: `<memories>\n${JSON.stringify(current, null, 1)}\n</memories>\n\n<conversations since="${since.toISOString()}">\n${transcript(messages)}\n</conversations>`,
|
|
116
|
+
},
|
|
117
|
+
],
|
|
118
|
+
});
|
|
119
|
+
const changes = memory.Changes.parse(JSON.parse(result.content));
|
|
120
|
+
summary = changes.summary;
|
|
121
|
+
const userWritten = new Set(memories.filter(existing => existing.source == 'user').map(existing => existing.id));
|
|
122
|
+
const attempt = async (action, id, run, list) => {
|
|
123
|
+
try {
|
|
124
|
+
await run();
|
|
125
|
+
list.push(id);
|
|
126
|
+
}
|
|
127
|
+
catch (error) {
|
|
128
|
+
io.debug(`AI: memory update could not ${action} "${id}":`, io.errorText(error));
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
for (const { id, ...changed } of changes.update) {
|
|
132
|
+
if (changed.description === undefined && changed.content === undefined)
|
|
133
|
+
continue;
|
|
134
|
+
await attempt('update', id, () => callAPI(user, 'PATCH', 'users/:id/ai/memories/:memory', changed, userId, id), applied.updated);
|
|
135
|
+
}
|
|
136
|
+
for (const id of changes.delete) {
|
|
137
|
+
if (userWritten.has(id))
|
|
138
|
+
continue;
|
|
139
|
+
await attempt('delete', id, () => callAPI(user, 'DELETE', 'users/:id/ai/memories/:memory', {}, userId, id), applied.deleted);
|
|
140
|
+
}
|
|
141
|
+
for (const init of changes.add) {
|
|
142
|
+
await attempt('add', init.id, () => callAPI(user, 'PUT', 'users/:id/ai/memories', { ...init, source: 'periodic' }, userId), applied.added);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
const row = { ran: started, summary, changes: JSON.stringify(applied) };
|
|
146
|
+
await database
|
|
147
|
+
.insertInto('ai_memory_updates')
|
|
148
|
+
.values({ userId, ...row })
|
|
149
|
+
.onConflict(conflict => conflict.column('userId').doUpdateSet(row))
|
|
150
|
+
.execute();
|
|
151
|
+
}
|
|
152
|
+
const updates = jobs.define('ai.memory', {
|
|
153
|
+
concurrency: 1,
|
|
154
|
+
attempts: 3,
|
|
155
|
+
backoff: 30 * 60_000,
|
|
156
|
+
async run(_, { userId, signal }) {
|
|
157
|
+
if (!userId)
|
|
158
|
+
return;
|
|
159
|
+
const preferences = await getPreferences(userId);
|
|
160
|
+
if (!updatesEnabled() || !preferences.tools.memory)
|
|
161
|
+
return;
|
|
162
|
+
await emitStatus(userId, true);
|
|
163
|
+
try {
|
|
164
|
+
await update(userId, signal);
|
|
165
|
+
}
|
|
166
|
+
finally {
|
|
167
|
+
await emitStatus(userId, false);
|
|
168
|
+
}
|
|
169
|
+
const { memory_updates } = preferences.tools;
|
|
170
|
+
if (memory_updates == 'off')
|
|
171
|
+
return;
|
|
172
|
+
const user = await getUser(userId);
|
|
173
|
+
return nextUpdate(frequencyDays[memory_updates], user.preferences.time_zone);
|
|
174
|
+
},
|
|
175
|
+
});
|
|
176
|
+
/** Makes sure every user with updates turned on has one scheduled, and retries ones that failed */
|
|
177
|
+
jobs.define('ai.memory.schedule', {
|
|
178
|
+
every: 3_600_000,
|
|
179
|
+
async run() {
|
|
180
|
+
if (!updatesEnabled())
|
|
181
|
+
return;
|
|
182
|
+
const rows = await database
|
|
183
|
+
.selectFrom('app_preferences')
|
|
184
|
+
.innerJoin('users', 'users.id', 'app_preferences.userId')
|
|
185
|
+
.select(['app_preferences.userId', 'app_preferences.data', 'users.preferences'])
|
|
186
|
+
.where('app_preferences.appId', '=', 'ai')
|
|
187
|
+
.where(sql `app_preferences.data->'tools'->>'memory_updates'`, 'in', ['daily', 'weekly'])
|
|
188
|
+
.execute();
|
|
189
|
+
for (const row of rows) {
|
|
190
|
+
if (!ai.Preferences.safeParse(row.data).data?.tools.memory)
|
|
191
|
+
continue;
|
|
192
|
+
await updates.enqueue({}, { key: row.userId, userId: row.userId, at: nextUpdate(1, row.preferences.time_zone) });
|
|
193
|
+
}
|
|
194
|
+
},
|
|
195
|
+
});
|
|
196
|
+
export async function getStatus(userId, running) {
|
|
197
|
+
const [preferences, row, job] = await Promise.all([
|
|
198
|
+
getPreferences(userId),
|
|
199
|
+
database.selectFrom('ai_memory_updates').selectAll().where('userId', '=', userId).executeTakeFirst(),
|
|
200
|
+
updates.get(userId),
|
|
201
|
+
]);
|
|
202
|
+
return {
|
|
203
|
+
enabled: updatesEnabled(),
|
|
204
|
+
frequency: preferences.tools.memory_updates,
|
|
205
|
+
ran: row?.ran ?? null,
|
|
206
|
+
summary: row?.summary ?? null,
|
|
207
|
+
changes: row ? memory.ChangeIds.parse(row.changes) : null,
|
|
208
|
+
next: job?.state == 'queued' ? job.runAt : null,
|
|
209
|
+
running: running ?? job?.state == 'running',
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
async function emitStatus(userId, running) {
|
|
213
|
+
socketServer?.to('user:' + userId).emit('ai.memory_updates', await getStatus(userId, running));
|
|
214
|
+
}
|
|
215
|
+
/** Starts an update for the user now */
|
|
216
|
+
export async function startUpdate(userId) {
|
|
217
|
+
await updates.enqueue({}, { key: userId, userId, replace: true });
|
|
218
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import * as ai from '../common.js';
|
|
2
|
+
import * as models from '../node/models.js';
|
|
3
|
+
export declare function getRows(): Promise<models.Registered[]>;
|
|
4
|
+
export declare function getRow(id: string): Promise<models.Registered | undefined>;
|
|
5
|
+
export declare function emitModel(model: ai.Model): void;
|
|
6
|
+
export declare function emitRemoved(id: string): void;
|
|
7
|
+
export { get as getModel, getCapacity, learnCapabilities, syncPreset } from '../node/models.js';
|
|
8
|
+
export declare function getRoles(): Promise<ai.Roles>;
|
|
9
|
+
/** Replaces the model lists of the given roles */
|
|
10
|
+
export declare function setRoles(update: ai.RolesUpdate): Promise<void>;
|
|
11
|
+
export declare function getModels(): Promise<ai.Models>;
|
|
12
|
+
/** The ID of the model that currently serves a role */
|
|
13
|
+
export declare function resolveRole(role: ai.Role): Promise<string | undefined>;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { database } from '@axium/server/database';
|
|
2
|
+
import { io as socketServer } from '@axium/server/socket';
|
|
3
|
+
import * as ai from '../common.js';
|
|
4
|
+
import * as llama from '../node/llama.js';
|
|
5
|
+
import * as models from '../node/models.js';
|
|
6
|
+
export async function getRows() {
|
|
7
|
+
return await database.selectFrom('ai_models').selectAll().orderBy('name').execute();
|
|
8
|
+
}
|
|
9
|
+
export async function getRow(id) {
|
|
10
|
+
return await database.selectFrom('ai_models').selectAll().where('id', '=', id).executeTakeFirst();
|
|
11
|
+
}
|
|
12
|
+
export function emitModel(model) {
|
|
13
|
+
socketServer?.to('web').emit('ai.model', model);
|
|
14
|
+
}
|
|
15
|
+
export function emitRemoved(id) {
|
|
16
|
+
socketServer?.to('web').emit('ai.model.remove', id);
|
|
17
|
+
}
|
|
18
|
+
models.useRegistry({
|
|
19
|
+
records: getRows,
|
|
20
|
+
record: getRow,
|
|
21
|
+
async saveCapabilities(id, capabilities) {
|
|
22
|
+
return await database.updateTable('ai_models').set(capabilities).where('id', '=', id).returningAll().executeTakeFirstOrThrow();
|
|
23
|
+
},
|
|
24
|
+
changed: emitModel,
|
|
25
|
+
});
|
|
26
|
+
export { get as getModel, getCapacity, learnCapabilities, syncPreset } from '../node/models.js';
|
|
27
|
+
export async function getRoles() {
|
|
28
|
+
const rows = await database.selectFrom('ai_roles').selectAll().orderBy('position').execute();
|
|
29
|
+
const roles = Object.fromEntries(ai.roles.map(role => [role, []]));
|
|
30
|
+
for (const row of rows)
|
|
31
|
+
roles[row.role].push(row.modelId);
|
|
32
|
+
return roles;
|
|
33
|
+
}
|
|
34
|
+
/** Replaces the model lists of the given roles */
|
|
35
|
+
export async function setRoles(update) {
|
|
36
|
+
await database.transaction().execute(async (tx) => {
|
|
37
|
+
for (const role of ai.roles) {
|
|
38
|
+
const list = update[role];
|
|
39
|
+
if (!list)
|
|
40
|
+
continue;
|
|
41
|
+
await tx.deleteFrom('ai_roles').where('role', '=', role).execute();
|
|
42
|
+
if (!list.length)
|
|
43
|
+
continue;
|
|
44
|
+
await tx
|
|
45
|
+
.insertInto('ai_roles')
|
|
46
|
+
.values(list.map((modelId, position) => ({ role, modelId, position })))
|
|
47
|
+
.execute();
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
export async function getModels() {
|
|
52
|
+
const [list, roles] = await Promise.all([models.list(), getRoles()]);
|
|
53
|
+
return { models: list, roles, memory: models.getMemory(), running: llama.isRunning() };
|
|
54
|
+
}
|
|
55
|
+
/** The ID of the model that currently serves a role */
|
|
56
|
+
export async function resolveRole(role) {
|
|
57
|
+
const { models: list, roles } = await getModels();
|
|
58
|
+
return ai.resolveRole(list, roles, role)?.id;
|
|
59
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { database } from '@axium/server/database';
|
|
2
|
+
import * as ai from '../common.js';
|
|
3
|
+
/** The user's AI preferences, with defaults for anything unset or invalid */
|
|
4
|
+
export async function getPreferences(userId) {
|
|
5
|
+
const row = await database
|
|
6
|
+
.selectFrom('app_preferences')
|
|
7
|
+
.select('data')
|
|
8
|
+
.where('userId', '=', userId)
|
|
9
|
+
.where('appId', '=', 'ai')
|
|
10
|
+
.executeTakeFirst();
|
|
11
|
+
return ai.Preferences.safeParse(row?.data ?? {}).data ?? ai.Preferences.parse({});
|
|
12
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import * as ai from '../common.js';
|
|
2
|
+
import type * as tools from '../tools.js';
|
|
3
|
+
export declare function list(userId: string): Promise<ai.Timer[]>;
|
|
4
|
+
export declare function start(userId: string, init: ai.TimerInit): Promise<ai.Timer>;
|
|
5
|
+
export declare function cancel(userId: string, id: string): Promise<ai.Timer>;
|
|
6
|
+
/** Timers for tools run by the server's loop */
|
|
7
|
+
export declare function forUser(userId: string): tools.Timers;
|