@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,216 @@
|
|
|
1
|
+
import { checkAuthForUser } from '@axium/server/auth';
|
|
2
|
+
import { database } from '@axium/server/database';
|
|
3
|
+
import { error, parseBody, withError } from '@axium/server/requests';
|
|
4
|
+
import { addRoute } from '@axium/server/routes';
|
|
5
|
+
import * as z from 'zod';
|
|
6
|
+
import * as ai from '../../common.js';
|
|
7
|
+
import { cancel, emitMessage, emitRemoved, generate, isGenerating, toMessage, withProgress } from '../generation.js';
|
|
8
|
+
import { resolveRole } from '../models.js';
|
|
9
|
+
async function authConversation(request, id) {
|
|
10
|
+
const conversation = await database
|
|
11
|
+
.selectFrom('ai_conversations')
|
|
12
|
+
.selectAll()
|
|
13
|
+
.where('id', '=', id)
|
|
14
|
+
.executeTakeFirstOrThrow()
|
|
15
|
+
.catch(withError('Conversation not found', 404));
|
|
16
|
+
const { timezone } = await checkAuthForUser(request, conversation.userId);
|
|
17
|
+
return { conversation, timeZone: timezone };
|
|
18
|
+
}
|
|
19
|
+
/** Removes the messages after `from`, one of the user's, and responds to it again, with new content if given */
|
|
20
|
+
async function restart(conversation, from, timeZone, content) {
|
|
21
|
+
const { edited, removed, pending } = await database
|
|
22
|
+
.transaction()
|
|
23
|
+
.execute(async (tx) => ({
|
|
24
|
+
edited: content === undefined
|
|
25
|
+
? from
|
|
26
|
+
: await tx
|
|
27
|
+
.updateTable('ai_messages')
|
|
28
|
+
.set({ content })
|
|
29
|
+
.where('id', '=', from.id)
|
|
30
|
+
.returningAll()
|
|
31
|
+
.executeTakeFirstOrThrow(),
|
|
32
|
+
removed: await tx
|
|
33
|
+
.deleteFrom('ai_messages')
|
|
34
|
+
.where('conversationId', '=', conversation.id)
|
|
35
|
+
.where('created', '>', eb => eb.selectFrom('ai_messages').select('created').where('id', '=', from.id))
|
|
36
|
+
.returning('id')
|
|
37
|
+
.execute(),
|
|
38
|
+
pending: await tx
|
|
39
|
+
.insertInto('ai_messages')
|
|
40
|
+
.values({ conversationId: conversation.id, role: 'assistant', status: 'pending' })
|
|
41
|
+
.returningAll()
|
|
42
|
+
.executeTakeFirstOrThrow(),
|
|
43
|
+
}))
|
|
44
|
+
.catch(withError('Could not restart the response'));
|
|
45
|
+
await database
|
|
46
|
+
.updateTable('ai_conversations')
|
|
47
|
+
.set({ modified: new Date() })
|
|
48
|
+
.where('id', '=', conversation.id)
|
|
49
|
+
.execute()
|
|
50
|
+
.catch(withError('Could not update conversation'));
|
|
51
|
+
const message = toMessage(pending);
|
|
52
|
+
if (content !== undefined)
|
|
53
|
+
emitMessage(conversation.userId, toMessage(edited));
|
|
54
|
+
emitRemoved(conversation.userId, { conversationId: conversation.id, ids: removed.map(row => row.id) });
|
|
55
|
+
emitMessage(conversation.userId, message);
|
|
56
|
+
void generate(conversation, pending, { timeZone });
|
|
57
|
+
return message;
|
|
58
|
+
}
|
|
59
|
+
addRoute({
|
|
60
|
+
path: '/api/users/:id/ai/conversations',
|
|
61
|
+
params: { id: z.uuid() },
|
|
62
|
+
async GET(request, { id: userId }) {
|
|
63
|
+
await checkAuthForUser(request, userId);
|
|
64
|
+
return await database
|
|
65
|
+
.selectFrom('ai_conversations')
|
|
66
|
+
.selectAll()
|
|
67
|
+
.where('userId', '=', userId)
|
|
68
|
+
.orderBy('modified', 'desc')
|
|
69
|
+
.execute()
|
|
70
|
+
.catch(withError('Could not get conversations'));
|
|
71
|
+
},
|
|
72
|
+
async PUT(request, { id: userId }) {
|
|
73
|
+
const init = await parseBody(request, ai.ConversationInit);
|
|
74
|
+
await checkAuthForUser(request, userId);
|
|
75
|
+
const model = init.model ?? (await resolveRole('chat'));
|
|
76
|
+
if (!model)
|
|
77
|
+
error(503, 'No model is available');
|
|
78
|
+
return await database
|
|
79
|
+
.insertInto('ai_conversations')
|
|
80
|
+
.values({ title: init.title, titleSource: init.title ? 'user' : 'none', model, reasoning: init.reasoning, userId })
|
|
81
|
+
.returningAll()
|
|
82
|
+
.executeTakeFirstOrThrow()
|
|
83
|
+
.catch(withError('Could not create conversation'));
|
|
84
|
+
},
|
|
85
|
+
});
|
|
86
|
+
addRoute({
|
|
87
|
+
path: '/api/ai/conversations/:id',
|
|
88
|
+
params: { id: z.uuid() },
|
|
89
|
+
async GET(request, { id }) {
|
|
90
|
+
const { conversation } = await authConversation(request, id);
|
|
91
|
+
const messages = await database
|
|
92
|
+
.selectFrom('ai_messages')
|
|
93
|
+
.selectAll()
|
|
94
|
+
.where('conversationId', '=', id)
|
|
95
|
+
.orderBy('created')
|
|
96
|
+
.execute()
|
|
97
|
+
.catch(withError('Could not get messages'));
|
|
98
|
+
return { ...conversation, messages: messages.map(row => withProgress(toMessage(row))) };
|
|
99
|
+
},
|
|
100
|
+
async PATCH(request, { id }) {
|
|
101
|
+
const init = await parseBody(request, ai.ConversationInit);
|
|
102
|
+
await authConversation(request, id);
|
|
103
|
+
return await database
|
|
104
|
+
.updateTable('ai_conversations')
|
|
105
|
+
.set({ ...init, ...(init.title !== undefined && { titleSource: 'user' }) })
|
|
106
|
+
.where('id', '=', id)
|
|
107
|
+
.returningAll()
|
|
108
|
+
.executeTakeFirstOrThrow()
|
|
109
|
+
.catch(withError('Could not update conversation'));
|
|
110
|
+
},
|
|
111
|
+
async PUT(request, { id }) {
|
|
112
|
+
const { content, voice } = await parseBody(request, ai.MessageInit);
|
|
113
|
+
const auth = await authConversation(request, id);
|
|
114
|
+
if (isGenerating(id))
|
|
115
|
+
error(409, 'A response is already being generated');
|
|
116
|
+
const conversation = await database
|
|
117
|
+
.updateTable('ai_conversations')
|
|
118
|
+
.set({ modified: new Date(), title: auth.conversation.title || content.slice(0, 60) })
|
|
119
|
+
.where('id', '=', id)
|
|
120
|
+
.returningAll()
|
|
121
|
+
.executeTakeFirstOrThrow()
|
|
122
|
+
.catch(withError('Could not update conversation'));
|
|
123
|
+
const user = await database
|
|
124
|
+
.insertInto('ai_messages')
|
|
125
|
+
.values({ conversationId: id, role: 'user', content, voice })
|
|
126
|
+
.returningAll()
|
|
127
|
+
.executeTakeFirstOrThrow()
|
|
128
|
+
.catch(withError('Could not save message'));
|
|
129
|
+
const assistant = await database
|
|
130
|
+
.insertInto('ai_messages')
|
|
131
|
+
.values({ conversationId: id, role: 'assistant', status: 'pending' })
|
|
132
|
+
.returningAll()
|
|
133
|
+
.executeTakeFirstOrThrow()
|
|
134
|
+
.catch(withError('Could not save message'));
|
|
135
|
+
const messages = [toMessage(user), toMessage(assistant)];
|
|
136
|
+
for (const message of messages)
|
|
137
|
+
emitMessage(conversation.userId, message);
|
|
138
|
+
void generate(conversation, assistant, { timeZone: auth.timeZone });
|
|
139
|
+
return messages;
|
|
140
|
+
},
|
|
141
|
+
async POST(request, { id }) {
|
|
142
|
+
const body = await parseBody(request, ai.ConversationAction);
|
|
143
|
+
const { conversation, timeZone } = await authConversation(request, id);
|
|
144
|
+
switch (body.action) {
|
|
145
|
+
case 'cancel':
|
|
146
|
+
if (!cancel(id, conversation.userId))
|
|
147
|
+
error(409, 'No response is being generated');
|
|
148
|
+
return {};
|
|
149
|
+
case 'retry': {
|
|
150
|
+
if (isGenerating(id))
|
|
151
|
+
error(409, 'A response is already being generated');
|
|
152
|
+
const last = await database
|
|
153
|
+
.selectFrom('ai_messages')
|
|
154
|
+
.selectAll()
|
|
155
|
+
.where('conversationId', '=', id)
|
|
156
|
+
.where('role', '=', 'user')
|
|
157
|
+
.orderBy('created', 'desc')
|
|
158
|
+
.limit(1)
|
|
159
|
+
.executeTakeFirst()
|
|
160
|
+
.catch(withError('Could not get messages'));
|
|
161
|
+
if (!last)
|
|
162
|
+
error(409, 'There is nothing to retry');
|
|
163
|
+
return { message: await restart(conversation, last, timeZone) };
|
|
164
|
+
}
|
|
165
|
+
case 'edit': {
|
|
166
|
+
if (isGenerating(id))
|
|
167
|
+
error(409, 'A response is already being generated');
|
|
168
|
+
const target = await database
|
|
169
|
+
.selectFrom('ai_messages')
|
|
170
|
+
.selectAll()
|
|
171
|
+
.where('id', '=', body.message)
|
|
172
|
+
.where('conversationId', '=', id)
|
|
173
|
+
.executeTakeFirst()
|
|
174
|
+
.catch(withError('Could not get message'));
|
|
175
|
+
if (!target)
|
|
176
|
+
error(404, 'Message not found');
|
|
177
|
+
if (target.role != 'user')
|
|
178
|
+
error(409, 'Only your own messages can be edited');
|
|
179
|
+
return { message: await restart(conversation, target, timeZone, body.content) };
|
|
180
|
+
}
|
|
181
|
+
case 'decide': {
|
|
182
|
+
if (isGenerating(id))
|
|
183
|
+
error(409, 'A response is already being generated');
|
|
184
|
+
const awaiting = await database
|
|
185
|
+
.selectFrom('ai_messages')
|
|
186
|
+
.select('id')
|
|
187
|
+
.where('conversationId', '=', id)
|
|
188
|
+
.where('status', '=', 'awaiting')
|
|
189
|
+
.execute()
|
|
190
|
+
.catch(withError('Could not get messages'));
|
|
191
|
+
if (!awaiting.length)
|
|
192
|
+
error(409, 'No tool calls are awaiting approval');
|
|
193
|
+
const pending = await database
|
|
194
|
+
.insertInto('ai_messages')
|
|
195
|
+
.values({ conversationId: id, role: 'assistant', status: 'pending' })
|
|
196
|
+
.returningAll()
|
|
197
|
+
.executeTakeFirstOrThrow()
|
|
198
|
+
.catch(withError('Could not save message'));
|
|
199
|
+
const message = toMessage(pending);
|
|
200
|
+
emitMessage(conversation.userId, message);
|
|
201
|
+
void generate(conversation, pending, { decisions: body.decisions, timeZone });
|
|
202
|
+
return { message };
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
},
|
|
206
|
+
async DELETE(request, { id }) {
|
|
207
|
+
const { conversation } = await authConversation(request, id);
|
|
208
|
+
cancel(id, conversation.userId);
|
|
209
|
+
return await database
|
|
210
|
+
.deleteFrom('ai_conversations')
|
|
211
|
+
.where('id', '=', id)
|
|
212
|
+
.returningAll()
|
|
213
|
+
.executeTakeFirstOrThrow()
|
|
214
|
+
.catch(withError('Could not delete conversation'));
|
|
215
|
+
},
|
|
216
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { getSessionAndUser } from '@axium/server/auth';
|
|
2
|
+
import { error, isResponseError, withError } from '@axium/server/requests';
|
|
3
|
+
import { addRoute } from '@axium/server/routes';
|
|
4
|
+
import features from '@axium/core/features';
|
|
5
|
+
import * as io from 'ioium';
|
|
6
|
+
import * as ai from '../../common.js';
|
|
7
|
+
import * as llama from '../../node/llama.js';
|
|
8
|
+
import { getRows, resolveRole } from '../models.js';
|
|
9
|
+
const anthropicErrorTypes = {
|
|
10
|
+
400: 'invalid_request_error',
|
|
11
|
+
401: 'authentication_error',
|
|
12
|
+
403: 'permission_error',
|
|
13
|
+
404: 'not_found_error',
|
|
14
|
+
503: 'overloaded_error',
|
|
15
|
+
};
|
|
16
|
+
/** An error in the format the client's API uses */
|
|
17
|
+
function errorResponse(format, status, message) {
|
|
18
|
+
const body = format == 'anthropic'
|
|
19
|
+
? { type: 'error', error: { type: anthropicErrorTypes[status] ?? 'api_error', message } }
|
|
20
|
+
: { error: { message, type: status < 500 ? 'invalid_request_error' : 'server_error', code: status } };
|
|
21
|
+
return Response.json(body, { status });
|
|
22
|
+
}
|
|
23
|
+
/** Authenticates with a session token, as OpenAI clients (`Authorization`) and Anthropic clients (`x-api-key`) send it */
|
|
24
|
+
async function authenticate(request) {
|
|
25
|
+
if (!features('ai-gateway'))
|
|
26
|
+
error(403, 'The AI gateway is disabled');
|
|
27
|
+
const token = request.headers.get('Authorization')?.replace(/^Bearer /, '') || request.headers.get('x-api-key');
|
|
28
|
+
if (!token)
|
|
29
|
+
error(401, 'Missing API key');
|
|
30
|
+
const session = await getSessionAndUser(token).catch(withError('Invalid or expired API key', 401));
|
|
31
|
+
if (session.user.isSuspended)
|
|
32
|
+
error(403, 'User is suspended');
|
|
33
|
+
return session;
|
|
34
|
+
}
|
|
35
|
+
async function handle(format, run) {
|
|
36
|
+
try {
|
|
37
|
+
return await run();
|
|
38
|
+
}
|
|
39
|
+
catch (cause) {
|
|
40
|
+
if (isResponseError(cause))
|
|
41
|
+
return errorResponse(format, cause.status, cause.message);
|
|
42
|
+
io.warn('AI: gateway request failed:', io.errorText(cause));
|
|
43
|
+
return errorResponse(format, 502, io.errorText(cause));
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/** Paths under `/v1/` that are forwarded to llama-server, by the API format they use */
|
|
47
|
+
const forwarded = {
|
|
48
|
+
'chat/completions': 'openai',
|
|
49
|
+
'chat/completions/input_tokens': 'openai',
|
|
50
|
+
completions: 'openai',
|
|
51
|
+
responses: 'openai',
|
|
52
|
+
'responses/input_tokens': 'openai',
|
|
53
|
+
embeddings: 'openai',
|
|
54
|
+
messages: 'anthropic',
|
|
55
|
+
'messages/count_tokens': 'anthropic',
|
|
56
|
+
};
|
|
57
|
+
for (const [path, format] of Object.entries(forwarded)) {
|
|
58
|
+
addRoute({
|
|
59
|
+
path: '/api/ai/gateway/v1/' + path,
|
|
60
|
+
async POST(request) {
|
|
61
|
+
return await handle(format, async () => {
|
|
62
|
+
await authenticate(request);
|
|
63
|
+
if (!llama.isRunning())
|
|
64
|
+
error(503, 'llama-server is not running');
|
|
65
|
+
const body = await request.json().catch(withError('The request body must be JSON', 400));
|
|
66
|
+
const models = await getRows();
|
|
67
|
+
if (!models.some(model => model.id == body?.model)) {
|
|
68
|
+
const role = ai.Role.safeParse(body?.model);
|
|
69
|
+
if (!role.success) {
|
|
70
|
+
const available = [...models.map(model => model.id), ...ai.roles].join(', ');
|
|
71
|
+
error(404, `Unknown model "${body?.model}". Available: ${available}`);
|
|
72
|
+
}
|
|
73
|
+
body.model = (await resolveRole(role.data)) ?? error(503, `No model is available for the ${role.data} role`);
|
|
74
|
+
}
|
|
75
|
+
const upstream = await llama.send('v1/' + path, {
|
|
76
|
+
method: 'POST',
|
|
77
|
+
body: JSON.stringify(body),
|
|
78
|
+
signal: request.signal,
|
|
79
|
+
});
|
|
80
|
+
return new Response(upstream.body, {
|
|
81
|
+
status: upstream.status,
|
|
82
|
+
headers: { 'Content-Type': upstream.headers.get('Content-Type') ?? 'application/json' },
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
addRoute({
|
|
89
|
+
path: '/api/ai/gateway/v1/models',
|
|
90
|
+
async GET(request) {
|
|
91
|
+
return await handle('openai', async () => {
|
|
92
|
+
await authenticate(request);
|
|
93
|
+
const models = await getRows();
|
|
94
|
+
/** Fields from both the OpenAI and Anthropic model list formats */
|
|
95
|
+
const data = models.map(model => ({
|
|
96
|
+
id: model.id,
|
|
97
|
+
object: 'model',
|
|
98
|
+
type: 'model',
|
|
99
|
+
display_name: model.name,
|
|
100
|
+
created: Math.floor(model.created.getTime() / 1000),
|
|
101
|
+
created_at: model.created.toISOString(),
|
|
102
|
+
owned_by: 'axium',
|
|
103
|
+
}));
|
|
104
|
+
return Response.json({
|
|
105
|
+
object: 'list',
|
|
106
|
+
data,
|
|
107
|
+
has_more: false,
|
|
108
|
+
first_id: data[0]?.id ?? null,
|
|
109
|
+
last_id: data.at(-1)?.id ?? null,
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
},
|
|
113
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { getConfig } from '@axium/core';
|
|
2
|
+
import features from '@axium/core/features';
|
|
3
|
+
import { checkAuthForUser } from '@axium/server/auth';
|
|
4
|
+
import { database } from '@axium/server/database';
|
|
5
|
+
import { error, parseBody, withError } from '@axium/server/requests';
|
|
6
|
+
import { addRoute } from '@axium/server/routes';
|
|
7
|
+
import * as z from 'zod';
|
|
8
|
+
import * as memory from '../../memory.js';
|
|
9
|
+
import { getMemories, getStatus, startUpdate, updatesEnabled } from '../memory.js';
|
|
10
|
+
import { getPreferences } from '../preferences.js';
|
|
11
|
+
function checkWrite(content) {
|
|
12
|
+
if (!features('ai-memory'))
|
|
13
|
+
error(403, 'Memory is disabled on this server');
|
|
14
|
+
const limit = getConfig('@axium/ai').max_memory_length;
|
|
15
|
+
if (content && content.length > limit)
|
|
16
|
+
error(400, `A memory's content can have at most ${limit} characters; this has ${content.length}`);
|
|
17
|
+
}
|
|
18
|
+
async function exists(userId, id) {
|
|
19
|
+
const row = await database
|
|
20
|
+
.selectFrom('ai_memories')
|
|
21
|
+
.select('id')
|
|
22
|
+
.where('userId', '=', userId)
|
|
23
|
+
.where('id', '=', id)
|
|
24
|
+
.executeTakeFirst()
|
|
25
|
+
.catch(withError('Could not get memory'));
|
|
26
|
+
return !!row;
|
|
27
|
+
}
|
|
28
|
+
addRoute({
|
|
29
|
+
path: '/api/users/:id/ai/memories',
|
|
30
|
+
params: { id: z.uuid() },
|
|
31
|
+
async GET(request, { id: userId }) {
|
|
32
|
+
await checkAuthForUser(request, userId);
|
|
33
|
+
const config = getConfig('@axium/ai');
|
|
34
|
+
return { memories: await getMemories(userId), limit: config.max_memories, maxLength: config.max_memory_length };
|
|
35
|
+
},
|
|
36
|
+
async PUT(request, { id: userId }) {
|
|
37
|
+
const init = await parseBody(request, memory.Init);
|
|
38
|
+
await checkAuthForUser(request, userId);
|
|
39
|
+
checkWrite(init.content);
|
|
40
|
+
if (await exists(userId, init.id))
|
|
41
|
+
error(409, `A memory with the ID "${init.id}" already exists; update it instead`);
|
|
42
|
+
const limit = getConfig('@axium/ai').max_memories;
|
|
43
|
+
const { count } = await database
|
|
44
|
+
.selectFrom('ai_memories')
|
|
45
|
+
.select(eb => eb.fn.countAll().as('count'))
|
|
46
|
+
.where('userId', '=', userId)
|
|
47
|
+
.executeTakeFirstOrThrow()
|
|
48
|
+
.catch(withError('Could not count memories'));
|
|
49
|
+
if (Number(count) >= limit)
|
|
50
|
+
error(409, `Memory is full (${limit} memories); delete one before adding another`);
|
|
51
|
+
if (init.conversationId) {
|
|
52
|
+
await database
|
|
53
|
+
.selectFrom('ai_conversations')
|
|
54
|
+
.select('id')
|
|
55
|
+
.where('id', '=', init.conversationId)
|
|
56
|
+
.where('userId', '=', userId)
|
|
57
|
+
.executeTakeFirstOrThrow()
|
|
58
|
+
.catch(withError('Conversation not found', 404));
|
|
59
|
+
}
|
|
60
|
+
return await database
|
|
61
|
+
.insertInto('ai_memories')
|
|
62
|
+
.values({ ...init, userId })
|
|
63
|
+
.returningAll()
|
|
64
|
+
.$castTo()
|
|
65
|
+
.executeTakeFirstOrThrow()
|
|
66
|
+
.catch(withError('Could not save memory'));
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
addRoute({
|
|
70
|
+
path: '/api/users/:id/ai/memories/:memory',
|
|
71
|
+
params: { id: z.uuid(), memory: memory.Id },
|
|
72
|
+
async GET(request, { id: userId, memory: id }) {
|
|
73
|
+
await checkAuthForUser(request, userId);
|
|
74
|
+
return await database
|
|
75
|
+
.selectFrom('ai_memories')
|
|
76
|
+
.selectAll()
|
|
77
|
+
.where('userId', '=', userId)
|
|
78
|
+
.where('id', '=', id)
|
|
79
|
+
.$castTo()
|
|
80
|
+
.executeTakeFirstOrThrow()
|
|
81
|
+
.catch(withError(`No memory has the ID "${id}"`));
|
|
82
|
+
},
|
|
83
|
+
async PATCH(request, { id: userId, memory: id }) {
|
|
84
|
+
const update = await parseBody(request, memory.Update);
|
|
85
|
+
await checkAuthForUser(request, userId);
|
|
86
|
+
checkWrite(update.content);
|
|
87
|
+
if (update.id && update.id != id && (await exists(userId, update.id))) {
|
|
88
|
+
error(409, `A memory with the ID "${update.id}" already exists`);
|
|
89
|
+
}
|
|
90
|
+
return await database
|
|
91
|
+
.updateTable('ai_memories')
|
|
92
|
+
.set({ ...update, modified: new Date() })
|
|
93
|
+
.where('userId', '=', userId)
|
|
94
|
+
.where('id', '=', id)
|
|
95
|
+
.returningAll()
|
|
96
|
+
.$castTo()
|
|
97
|
+
.executeTakeFirstOrThrow()
|
|
98
|
+
.catch(withError(`No memory has the ID "${id}"`));
|
|
99
|
+
},
|
|
100
|
+
async DELETE(request, { id: userId, memory: id }) {
|
|
101
|
+
await checkAuthForUser(request, userId);
|
|
102
|
+
return await database
|
|
103
|
+
.deleteFrom('ai_memories')
|
|
104
|
+
.where('userId', '=', userId)
|
|
105
|
+
.where('id', '=', id)
|
|
106
|
+
.returningAll()
|
|
107
|
+
.$castTo()
|
|
108
|
+
.executeTakeFirstOrThrow()
|
|
109
|
+
.catch(withError(`No memory has the ID "${id}"`));
|
|
110
|
+
},
|
|
111
|
+
});
|
|
112
|
+
addRoute({
|
|
113
|
+
path: '/api/users/:id/ai/memory_updates',
|
|
114
|
+
params: { id: z.uuid() },
|
|
115
|
+
async GET(request, { id: userId }) {
|
|
116
|
+
await checkAuthForUser(request, userId);
|
|
117
|
+
return await getStatus(userId).catch(withError('Could not get memory updates'));
|
|
118
|
+
},
|
|
119
|
+
async POST(request, { id: userId }) {
|
|
120
|
+
await parseBody(request, z.object({}));
|
|
121
|
+
await checkAuthForUser(request, userId);
|
|
122
|
+
if (!updatesEnabled())
|
|
123
|
+
error(403, 'Memory updates are disabled on this server');
|
|
124
|
+
if (!(await getPreferences(userId)).tools.memory)
|
|
125
|
+
error(409, 'Memory is turned off in your AI preferences');
|
|
126
|
+
await startUpdate(userId).catch(withError('Could not start a memory update'));
|
|
127
|
+
return await getStatus(userId).catch(withError('Could not get memory updates'));
|
|
128
|
+
},
|
|
129
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { getConfig } from '@axium/core';
|
|
2
|
+
import { requireSession } from '@axium/server/auth';
|
|
3
|
+
import { database } from '@axium/server/database';
|
|
4
|
+
import { error, parseBody, parseSearch, withError } from '@axium/server/requests';
|
|
5
|
+
import { addRoute } from '@axium/server/routes';
|
|
6
|
+
import * as io from 'ioium';
|
|
7
|
+
import * as z from 'zod';
|
|
8
|
+
import * as ai from '../../common.js';
|
|
9
|
+
import * as hub from '../../hub.js';
|
|
10
|
+
import * as llama from '../../node/llama.js';
|
|
11
|
+
import { emitModel, emitRemoved, getCapacity, getModel, getModels, getRoles, getRow, getRows, setRoles, syncPreset } from '../models.js';
|
|
12
|
+
async function requireAdmin(request) {
|
|
13
|
+
const session = await requireSession(request);
|
|
14
|
+
if (!session.user.isAdmin)
|
|
15
|
+
error(403, 'Only administrators can manage models');
|
|
16
|
+
}
|
|
17
|
+
const hfToken = () => getConfig('@axium/ai').llama.hf_token;
|
|
18
|
+
function requireRunning() {
|
|
19
|
+
if (!llama.isRunning())
|
|
20
|
+
error(503, 'llama-server is not running');
|
|
21
|
+
}
|
|
22
|
+
async function existingModel(id) {
|
|
23
|
+
const model = await getModel(id);
|
|
24
|
+
if (!model)
|
|
25
|
+
error(404, 'Model not found');
|
|
26
|
+
return model;
|
|
27
|
+
}
|
|
28
|
+
addRoute({
|
|
29
|
+
path: '/api/ai/models',
|
|
30
|
+
async GET(request) {
|
|
31
|
+
await requireSession(request);
|
|
32
|
+
return await getModels();
|
|
33
|
+
},
|
|
34
|
+
async PUT(request) {
|
|
35
|
+
const init = await parseBody(request, ai.ModelInit);
|
|
36
|
+
await requireAdmin(request);
|
|
37
|
+
requireRunning();
|
|
38
|
+
if (await getRow(init.id))
|
|
39
|
+
error(409, 'A model with this ID already exists');
|
|
40
|
+
const details = await hub
|
|
41
|
+
.details(init.repo, getCapacity(), hfToken())
|
|
42
|
+
.catch(withError('Could not get the repository from Hugging Face', 502));
|
|
43
|
+
const quant = details.quants.find(quant => quant.tag == init.quant);
|
|
44
|
+
if (!quant)
|
|
45
|
+
error(400, 'The repository has no such quantization');
|
|
46
|
+
const source = `${details.repo}:${quant.tag}`;
|
|
47
|
+
await database
|
|
48
|
+
.insertInto('ai_models')
|
|
49
|
+
.values({ id: init.id, name: init.name, source, size: quant.size, options: JSON.stringify(init.options ?? {}) })
|
|
50
|
+
.execute()
|
|
51
|
+
.catch(withError('Could not save the model'));
|
|
52
|
+
try {
|
|
53
|
+
await syncPreset();
|
|
54
|
+
await llama.download(source);
|
|
55
|
+
}
|
|
56
|
+
catch (cause) {
|
|
57
|
+
await database.deleteFrom('ai_models').where('id', '=', init.id).execute();
|
|
58
|
+
await syncPreset().catch(() => { });
|
|
59
|
+
error(502, io.errorText(cause));
|
|
60
|
+
}
|
|
61
|
+
const model = await existingModel(init.id);
|
|
62
|
+
emitModel(model);
|
|
63
|
+
return model;
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
addRoute({
|
|
67
|
+
path: '/api/ai/models/:id',
|
|
68
|
+
params: { id: ai.ModelId },
|
|
69
|
+
async GET(request, { id }) {
|
|
70
|
+
await requireSession(request);
|
|
71
|
+
return await existingModel(id);
|
|
72
|
+
},
|
|
73
|
+
async PATCH(request, { id }) {
|
|
74
|
+
const { options, ...update } = await parseBody(request, ai.ModelUpdate);
|
|
75
|
+
await requireAdmin(request);
|
|
76
|
+
await existingModel(id);
|
|
77
|
+
await database
|
|
78
|
+
.updateTable('ai_models')
|
|
79
|
+
.set({ ...update, ...(options && { options: JSON.stringify(options) }) })
|
|
80
|
+
.where('id', '=', id)
|
|
81
|
+
.execute()
|
|
82
|
+
.catch(withError('Could not update the model'));
|
|
83
|
+
if (options)
|
|
84
|
+
await syncPreset().catch(withError('Could not apply the new options', 502));
|
|
85
|
+
const model = await existingModel(id);
|
|
86
|
+
emitModel(model);
|
|
87
|
+
return model;
|
|
88
|
+
},
|
|
89
|
+
async POST(request, { id }) {
|
|
90
|
+
const { action } = await parseBody(request, ai.ModelAction);
|
|
91
|
+
await requireAdmin(request);
|
|
92
|
+
const { source } = await existingModel(id);
|
|
93
|
+
requireRunning();
|
|
94
|
+
const actions = {
|
|
95
|
+
load: () => llama.load(id),
|
|
96
|
+
unload: () => llama.unload(id),
|
|
97
|
+
pause: () => llama.unload(source),
|
|
98
|
+
resume: () => llama.download(source),
|
|
99
|
+
};
|
|
100
|
+
await actions[action]().catch(withError(`Could not ${action} the model`, 502));
|
|
101
|
+
const model = await existingModel(id);
|
|
102
|
+
emitModel(model);
|
|
103
|
+
return model;
|
|
104
|
+
},
|
|
105
|
+
async DELETE(request, { id }) {
|
|
106
|
+
await requireAdmin(request);
|
|
107
|
+
const model = await existingModel(id);
|
|
108
|
+
requireRunning();
|
|
109
|
+
if (model.status == 'loaded' || model.status == 'loading' || model.status == 'sleeping') {
|
|
110
|
+
await llama.unload(id).catch(withError('Could not unload the model', 502));
|
|
111
|
+
}
|
|
112
|
+
const shared = (await getRows()).some(row => row.id != id && row.source == model.source);
|
|
113
|
+
if (!shared) {
|
|
114
|
+
await llama.remove(model.source).catch(cause => {
|
|
115
|
+
if (model.status != 'unavailable')
|
|
116
|
+
error(502, 'Could not delete the model files: ' + io.errorText(cause));
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
await database.deleteFrom('ai_models').where('id', '=', id).execute().catch(withError('Could not delete the model'));
|
|
120
|
+
await syncPreset().catch(cause => io.warn(`AI: could not update llama-server's models: ${io.errorText(cause)}`));
|
|
121
|
+
emitRemoved(id);
|
|
122
|
+
return model;
|
|
123
|
+
},
|
|
124
|
+
});
|
|
125
|
+
addRoute({
|
|
126
|
+
path: '/api/ai/roles',
|
|
127
|
+
async GET(request) {
|
|
128
|
+
await requireSession(request);
|
|
129
|
+
return await getRoles();
|
|
130
|
+
},
|
|
131
|
+
async PATCH(request) {
|
|
132
|
+
const update = await parseBody(request, ai.RolesUpdate);
|
|
133
|
+
await requireAdmin(request);
|
|
134
|
+
const known = new Set((await getRows()).map(row => row.id));
|
|
135
|
+
for (const models of Object.values(update)) {
|
|
136
|
+
if (new Set(models).size != models.length)
|
|
137
|
+
error(400, 'A model is listed twice for the same role');
|
|
138
|
+
const unknown = models.find(id => !known.has(id));
|
|
139
|
+
if (unknown)
|
|
140
|
+
error(404, `Model "${unknown}" not found`);
|
|
141
|
+
}
|
|
142
|
+
await setRoles(update).catch(withError('Could not update the roles'));
|
|
143
|
+
return await getRoles();
|
|
144
|
+
},
|
|
145
|
+
});
|
|
146
|
+
addRoute({
|
|
147
|
+
path: '/api/ai/hub',
|
|
148
|
+
async GET(request) {
|
|
149
|
+
const { search } = parseSearch(request, ai.HubSearch);
|
|
150
|
+
await requireAdmin(request);
|
|
151
|
+
return await hub.search(search, hfToken()).catch(withError('Could not search Hugging Face', 502));
|
|
152
|
+
},
|
|
153
|
+
});
|
|
154
|
+
addRoute({
|
|
155
|
+
path: '/api/ai/hub/details',
|
|
156
|
+
async GET(request) {
|
|
157
|
+
const { repo } = parseSearch(request, z.object({ repo: ai.HubRepoId }));
|
|
158
|
+
await requireAdmin(request);
|
|
159
|
+
return await hub.details(repo, getCapacity(), hfToken()).catch(withError('Could not get the repository from Hugging Face', 502));
|
|
160
|
+
},
|
|
161
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|