@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
package/dist/common.js
ADDED
|
@@ -0,0 +1,569 @@
|
|
|
1
|
+
import { $API, serverConfigs } from '@axium/core';
|
|
2
|
+
import { appPreferences } from '@axium/core/apps';
|
|
3
|
+
import { addServerToClient } from '@axium/core/socket';
|
|
4
|
+
import * as locales from '@james-pre/locales';
|
|
5
|
+
import * as z from 'zod';
|
|
6
|
+
import * as memory from './memory.js';
|
|
7
|
+
export const ModelId = z
|
|
8
|
+
.string()
|
|
9
|
+
.max(64)
|
|
10
|
+
.regex(/^[a-z0-9]+(?:-[a-z0-9]+)*$/, 'Model IDs may only contain lowercase letters, numbers, and single dashes between them');
|
|
11
|
+
/** Turns a display name into a model ID */
|
|
12
|
+
export function modelIdFrom(name) {
|
|
13
|
+
return name
|
|
14
|
+
.toLowerCase()
|
|
15
|
+
.replace(/[^a-z0-9]+/g, '-')
|
|
16
|
+
.replace(/^-+|-+$/g, '')
|
|
17
|
+
.slice(0, 64)
|
|
18
|
+
.replace(/-+$/, '');
|
|
19
|
+
}
|
|
20
|
+
/** A display name for a model from its repository, e.g. "Gemma 4 E4B" from "org/Gemma-4-E4B-GGUF" */
|
|
21
|
+
export function nameFromRepo(repo) {
|
|
22
|
+
return repo
|
|
23
|
+
.split('/')
|
|
24
|
+
.at(-1)
|
|
25
|
+
.replace(/([-_.](i1|imatrix|gguf))+$/gi, '')
|
|
26
|
+
.replace(/[-_]+/g, ' ');
|
|
27
|
+
}
|
|
28
|
+
/** A Hugging Face repository */
|
|
29
|
+
export const HubRepoId = z
|
|
30
|
+
.string()
|
|
31
|
+
.max(200)
|
|
32
|
+
.regex(/^[\w.-]+\/[\w.-]+$/, 'Repositories have the form <owner>/<name>');
|
|
33
|
+
/** A quantization tag, as llama.cpp matches it against file names */
|
|
34
|
+
export const QuantTag = z
|
|
35
|
+
.string()
|
|
36
|
+
.max(32)
|
|
37
|
+
.regex(/^[A-Z0-9_]+$/);
|
|
38
|
+
export const LlamaConfig = z.object({
|
|
39
|
+
/** The `llama-server` executable, either a path or a name looked up in `PATH` */
|
|
40
|
+
command: z.string().min(1),
|
|
41
|
+
host: z.string().min(1),
|
|
42
|
+
port: z.int().min(1).max(65535),
|
|
43
|
+
/** How many models may be loaded at once, 0 for no limit */
|
|
44
|
+
models_max: z.int().nonnegative(),
|
|
45
|
+
/** Device memory to keep free when loading models, in MiB */
|
|
46
|
+
memory_margin: z.int().nonnegative(),
|
|
47
|
+
/** Options applied to every model, using llama-server's argument names without dashes */
|
|
48
|
+
defaults: z.record(z.string(), z.union([z.string(), z.number(), z.boolean()])),
|
|
49
|
+
/** Used for Hugging Face downloads */
|
|
50
|
+
hf_token: z.string().optional(),
|
|
51
|
+
});
|
|
52
|
+
export const Config = z
|
|
53
|
+
.object({
|
|
54
|
+
/** Where downloaded models and llama-server's state are kept */
|
|
55
|
+
data_dir: z.string().min(1),
|
|
56
|
+
llama: LlamaConfig,
|
|
57
|
+
/** Used for models without their own system prompt; the current date is appended */
|
|
58
|
+
system_prompt: z.string().register(locales.zod, { multiline: true }),
|
|
59
|
+
/** Seconds a single response may take before it is stopped */
|
|
60
|
+
timeout: z.number().positive(),
|
|
61
|
+
/** The most responses the model may give in one turn while using tools */
|
|
62
|
+
max_steps: z.int().positive(),
|
|
63
|
+
/** A SearXNG instance used by the web search tool, which is hidden while this is unset */
|
|
64
|
+
search_url: z.url().nullish(),
|
|
65
|
+
/** The most memories each user may have */
|
|
66
|
+
max_memories: z.int().positive(),
|
|
67
|
+
/** The most characters in a memory's content */
|
|
68
|
+
max_memory_length: z.int().positive().max(10_000),
|
|
69
|
+
/** A recording of the voice that spoken replies imitate, for models that clone voices */
|
|
70
|
+
speaker_reference: z.string().nullish(),
|
|
71
|
+
/** What is said in `speaker_reference`; models that support it then also imitate its accent and delivery */
|
|
72
|
+
speaker_reference_text: z.string().trim().min(1).nullish().register(locales.zod, { multiline: true }),
|
|
73
|
+
})
|
|
74
|
+
.register(locales.zod, { prefix: 'ai.config' });
|
|
75
|
+
serverConfigs.set('@axium/ai', Config);
|
|
76
|
+
/** `default` uses the model's default, `none` disables reasoning, anything else is a level */
|
|
77
|
+
export const Reasoning = z
|
|
78
|
+
.string()
|
|
79
|
+
.regex(/^[a-z]+$/)
|
|
80
|
+
.max(16);
|
|
81
|
+
/** Reasoning levels with names, lowest to highest */
|
|
82
|
+
export const reasoningLevels = ['minimal', 'low', 'medium', 'high', 'xhigh', 'max'];
|
|
83
|
+
export const ModelStatus = z.literal(['unavailable', 'downloading', 'incomplete', 'unloaded', 'loading', 'loaded', 'sleeping']);
|
|
84
|
+
/** Sizes in bytes */
|
|
85
|
+
export const MemoryUse = z.object({
|
|
86
|
+
device: z.number().nonnegative(),
|
|
87
|
+
host: z.number().nonnegative(),
|
|
88
|
+
});
|
|
89
|
+
export const ModelMemory = z.object({
|
|
90
|
+
/** At the smallest context the model may be loaded with */
|
|
91
|
+
minimum: MemoryUse.optional(),
|
|
92
|
+
/** While loaded */
|
|
93
|
+
current: MemoryUse.optional(),
|
|
94
|
+
/** The context size while loaded, in tokens */
|
|
95
|
+
context: z.int().optional(),
|
|
96
|
+
});
|
|
97
|
+
/** How llama-server loads a model */
|
|
98
|
+
export const ModelOptions = z.object({
|
|
99
|
+
/** Comma-separated llama.cpp device names */
|
|
100
|
+
device: z
|
|
101
|
+
.string()
|
|
102
|
+
.max(64)
|
|
103
|
+
.regex(/^[\w,]+$/)
|
|
104
|
+
.optional(),
|
|
105
|
+
/** Context size in tokens */
|
|
106
|
+
context: z.int().positive().optional(),
|
|
107
|
+
/** Device memory to leave free when fitting the model, in MiB */
|
|
108
|
+
reserve: z.int().nonnegative().optional(),
|
|
109
|
+
/** Number of requests the model serves at once */
|
|
110
|
+
parallel: z.int().positive().max(64).optional(),
|
|
111
|
+
/** Path of a local multimodal projector file, used instead of the repository's */
|
|
112
|
+
mmproj: z
|
|
113
|
+
.string()
|
|
114
|
+
.regex(/^\/[^\r\n]*$/, 'Must be an absolute path')
|
|
115
|
+
.max(4096)
|
|
116
|
+
.optional(),
|
|
117
|
+
});
|
|
118
|
+
export const DownloadProgress = z.object({
|
|
119
|
+
/** Bytes */
|
|
120
|
+
done: z.number().nonnegative(),
|
|
121
|
+
/** Bytes */
|
|
122
|
+
total: z.number().nonnegative(),
|
|
123
|
+
});
|
|
124
|
+
export const Modality = z.literal(['text', 'image', 'audio', 'video']);
|
|
125
|
+
export const ModelInfo = z.object({
|
|
126
|
+
id: ModelId,
|
|
127
|
+
name: z.string().min(1).max(100),
|
|
128
|
+
/** `<repository>:<quantization>` */
|
|
129
|
+
source: z.string(),
|
|
130
|
+
/** Bytes on disk once downloaded */
|
|
131
|
+
size: z.coerce.bigint().nonnegative(),
|
|
132
|
+
created: z.coerce.date(),
|
|
133
|
+
/** Reasoning levels the model distinguishes, lowest to highest. Empty when it only has on and off; missing when not known yet. */
|
|
134
|
+
reasoningLevels: Reasoning.array().nullish(),
|
|
135
|
+
/** What the chat template does without a reasoning level: `none` or one of the levels. Missing when not known. */
|
|
136
|
+
templateReasoning: Reasoning.nullish(),
|
|
137
|
+
/** Whether the chat template supports tool calls. Missing when not known. */
|
|
138
|
+
tools: z.boolean().nullish(),
|
|
139
|
+
/** What `default` amounts to, set by an administrator */
|
|
140
|
+
reasoning: Reasoning.nullable(),
|
|
141
|
+
systemPrompt: z.string().max(20_000).nullable(),
|
|
142
|
+
options: ModelOptions,
|
|
143
|
+
});
|
|
144
|
+
export const Model = z.object({
|
|
145
|
+
...ModelInfo.shape,
|
|
146
|
+
status: ModelStatus,
|
|
147
|
+
/** The last attempt to load the model failed */
|
|
148
|
+
failed: z.boolean().optional(),
|
|
149
|
+
progress: DownloadProgress.optional(),
|
|
150
|
+
/** The context size the model was trained with, in tokens */
|
|
151
|
+
contextLength: z.int().optional(),
|
|
152
|
+
inputs: Modality.array().optional(),
|
|
153
|
+
outputs: Modality.array().optional(),
|
|
154
|
+
memory: ModelMemory.optional(),
|
|
155
|
+
});
|
|
156
|
+
/** Whether a model can be chatted with */
|
|
157
|
+
export function isUsable(model) {
|
|
158
|
+
return model.status != 'unavailable' && model.status != 'downloading' && model.status != 'incomplete';
|
|
159
|
+
}
|
|
160
|
+
/** What `default` amounts to for a model, when known */
|
|
161
|
+
export function defaultReasoning(model) {
|
|
162
|
+
return model.reasoning ?? model.templateReasoning;
|
|
163
|
+
}
|
|
164
|
+
export const Device = z.object({
|
|
165
|
+
name: z.string(),
|
|
166
|
+
/** Bytes */
|
|
167
|
+
total: z.number().nonnegative(),
|
|
168
|
+
/** Bytes in use by anything, when known */
|
|
169
|
+
used: z.number().nonnegative().optional(),
|
|
170
|
+
});
|
|
171
|
+
/** RAM and VRAM */
|
|
172
|
+
export const DRAM = z.object({
|
|
173
|
+
devices: Device.array(),
|
|
174
|
+
host: Device,
|
|
175
|
+
});
|
|
176
|
+
/** What a model is used for */
|
|
177
|
+
export const roles = ['chat', 'assistant', 'coding', 'stt', 'tts'];
|
|
178
|
+
export const Role = z.literal(roles);
|
|
179
|
+
/** Models for each role, most preferred first */
|
|
180
|
+
export const Roles = z.record(Role, ModelId.array().max(50));
|
|
181
|
+
export const RolesUpdate = z.partialRecord(Role, ModelId.array().max(50));
|
|
182
|
+
/** Roles that use another role's models when none of their own are usable */
|
|
183
|
+
const roleFallbacks = { assistant: 'chat', coding: 'chat' };
|
|
184
|
+
/** Whether a model has the modalities a role needs */
|
|
185
|
+
export function canServe(model, role) {
|
|
186
|
+
switch (role) {
|
|
187
|
+
case 'stt':
|
|
188
|
+
return !!model.inputs?.includes('audio');
|
|
189
|
+
case 'tts':
|
|
190
|
+
return !!model.outputs?.includes('audio');
|
|
191
|
+
default:
|
|
192
|
+
return !model.outputs || model.outputs.includes('text');
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
function servable(model, role) {
|
|
196
|
+
return !!model && isUsable(model) && canServe(model, role);
|
|
197
|
+
}
|
|
198
|
+
/** The first model of `ids` that is usable, can serve the role and didn't fail to load */
|
|
199
|
+
export function firstServable(models, ids, role) {
|
|
200
|
+
for (const id of ids) {
|
|
201
|
+
const model = models.find(model => model.id == id);
|
|
202
|
+
if (servable(model, role) && !model.failed)
|
|
203
|
+
return model;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* The model that serves a role: the first usable one in its list that didn't fail to load,
|
|
208
|
+
* then the fallback role's, then any model that can serve it.
|
|
209
|
+
*/
|
|
210
|
+
export function resolveRole(models, roleModels, role) {
|
|
211
|
+
const listed = firstServable(models, roleModels[role], role);
|
|
212
|
+
if (listed)
|
|
213
|
+
return listed;
|
|
214
|
+
const fallback = roleFallbacks[role];
|
|
215
|
+
if (fallback)
|
|
216
|
+
return resolveRole(models, roleModels, fallback);
|
|
217
|
+
return models.find(model => servable(model, role) && !model.failed) ?? models.find(model => servable(model, role));
|
|
218
|
+
}
|
|
219
|
+
export const Models = z.object({
|
|
220
|
+
models: Model.array(),
|
|
221
|
+
roles: Roles,
|
|
222
|
+
memory: DRAM,
|
|
223
|
+
/** Whether llama-server is reachable */
|
|
224
|
+
running: z.boolean(),
|
|
225
|
+
});
|
|
226
|
+
export const ModelInit = z.object({
|
|
227
|
+
id: ModelId,
|
|
228
|
+
name: Model.shape.name,
|
|
229
|
+
repo: HubRepoId,
|
|
230
|
+
quant: QuantTag,
|
|
231
|
+
options: ModelOptions.optional(),
|
|
232
|
+
});
|
|
233
|
+
export const ModelUpdate = z.object({
|
|
234
|
+
name: Model.shape.name.optional(),
|
|
235
|
+
reasoning: Reasoning.nullable().optional(),
|
|
236
|
+
systemPrompt: Model.shape.systemPrompt.optional(),
|
|
237
|
+
options: ModelOptions.optional(),
|
|
238
|
+
});
|
|
239
|
+
/** `pause` and `resume` apply to downloads */
|
|
240
|
+
export const ModelAction = z.object({
|
|
241
|
+
action: z.literal(['load', 'unload', 'pause', 'resume']),
|
|
242
|
+
});
|
|
243
|
+
export const HubSearch = z.object({
|
|
244
|
+
search: z.string().trim().min(1).max(200),
|
|
245
|
+
});
|
|
246
|
+
export const HubRepo = z.object({
|
|
247
|
+
id: HubRepoId,
|
|
248
|
+
downloads: z.int().nonnegative(),
|
|
249
|
+
likes: z.int().nonnegative(),
|
|
250
|
+
modified: z.coerce.date().optional(),
|
|
251
|
+
});
|
|
252
|
+
/** Where a model's weights would go: all on a device, partly in host memory, or nowhere */
|
|
253
|
+
export const Fit = z.literal(['device', 'host', 'none']);
|
|
254
|
+
export const HubQuant = z.object({
|
|
255
|
+
tag: QuantTag,
|
|
256
|
+
files: z.string().array(),
|
|
257
|
+
/** Bytes, including sidecar files */
|
|
258
|
+
size: z.coerce.bigint().nonnegative(),
|
|
259
|
+
fit: Fit,
|
|
260
|
+
});
|
|
261
|
+
export const HubDetails = z.object({
|
|
262
|
+
repo: HubRepoId,
|
|
263
|
+
/** The repository the weights were converted from */
|
|
264
|
+
base: HubRepoId.optional(),
|
|
265
|
+
quants: HubQuant.array(),
|
|
266
|
+
});
|
|
267
|
+
export const ConversationInit = z.object({
|
|
268
|
+
title: z.string().max(200).optional(),
|
|
269
|
+
/** `null` uses the default model */
|
|
270
|
+
model: ModelId.nullish(),
|
|
271
|
+
reasoning: Reasoning.optional(),
|
|
272
|
+
});
|
|
273
|
+
export const Conversation = z.object({
|
|
274
|
+
id: z.uuid(),
|
|
275
|
+
userId: z.uuid(),
|
|
276
|
+
title: z.string(),
|
|
277
|
+
/** `null` when the model was removed or the default model is used */
|
|
278
|
+
model: ModelId.nullable(),
|
|
279
|
+
reasoning: Reasoning,
|
|
280
|
+
created: z.coerce.date(),
|
|
281
|
+
modified: z.coerce.date(),
|
|
282
|
+
/** Names of tools the user allowed for the rest of the conversation */
|
|
283
|
+
allowedTools: z.string().array(),
|
|
284
|
+
});
|
|
285
|
+
export const MessageRole = z.literal(['user', 'assistant', 'tool']);
|
|
286
|
+
/** `awaiting` is a tool call waiting for the user to approve it */
|
|
287
|
+
export const MessageStatus = z.literal(['pending', 'done', 'error', 'cancelled', 'awaiting']);
|
|
288
|
+
/** Where a tool acts */
|
|
289
|
+
export const Environment = z.literal(['account', 'web', 'memory']).nullable();
|
|
290
|
+
/** `write` tools change data, so they may need the user's approval */
|
|
291
|
+
export const ToolAccess = z.literal(['read', 'write']);
|
|
292
|
+
/** Whether a tool runs without asking */
|
|
293
|
+
export const Approval = z.literal(['ask', 'allow']).register(locales.zod, { prefix: 'ai.approval' });
|
|
294
|
+
/** `chat` allows the tool for the rest of the conversation, `always` for every conversation */
|
|
295
|
+
export const Decision = z.literal(['deny', 'once', 'chat', 'always']);
|
|
296
|
+
/** Tool call IDs mapped to the user's choice */
|
|
297
|
+
export const Decisions = z.record(z.string(), Decision);
|
|
298
|
+
export const ToolCall = z.object({
|
|
299
|
+
id: z.string(),
|
|
300
|
+
name: z.string(),
|
|
301
|
+
/** JSON, as produced by the model */
|
|
302
|
+
arguments: z.string(),
|
|
303
|
+
});
|
|
304
|
+
export const Usage = z.object({
|
|
305
|
+
promptTokens: z.int().nonnegative(),
|
|
306
|
+
completionTokens: z.int().nonnegative(),
|
|
307
|
+
/** Tokens per second */
|
|
308
|
+
promptSpeed: z.number().optional(),
|
|
309
|
+
/** Tokens per second */
|
|
310
|
+
generationSpeed: z.number().optional(),
|
|
311
|
+
});
|
|
312
|
+
export const Message = z.object({
|
|
313
|
+
id: z.uuid(),
|
|
314
|
+
conversationId: z.uuid(),
|
|
315
|
+
created: z.coerce.date(),
|
|
316
|
+
role: MessageRole,
|
|
317
|
+
content: z.string(),
|
|
318
|
+
reasoning: z.string().nullable(),
|
|
319
|
+
toolCalls: ToolCall.array().nullable(),
|
|
320
|
+
toolCallId: z.string().nullable(),
|
|
321
|
+
status: MessageStatus,
|
|
322
|
+
error: z.string().nullable(),
|
|
323
|
+
usage: Usage.nullable(),
|
|
324
|
+
voice: z.boolean(),
|
|
325
|
+
});
|
|
326
|
+
export const MessageInit = z.object({
|
|
327
|
+
content: z.string().trim().min(1).max(100_000),
|
|
328
|
+
voice: z.boolean().optional(),
|
|
329
|
+
});
|
|
330
|
+
export const ConversationAction = z.discriminatedUnion('action', [
|
|
331
|
+
/** Cancel the current response */
|
|
332
|
+
z.object({ action: z.literal('cancel') }),
|
|
333
|
+
/** Retry the last response */
|
|
334
|
+
z.object({ action: z.literal('retry') }),
|
|
335
|
+
/** Replaces the content of one of the user's messages, removes the messages after it, and responds again. */
|
|
336
|
+
MessageInit.pick({ content: true }).extend({ action: z.literal('edit'), message: z.uuid() }),
|
|
337
|
+
/** Approves or denies every tool call awaiting the user, then continues the response */
|
|
338
|
+
z.object({ action: z.literal('decide'), decisions: Decisions }),
|
|
339
|
+
]);
|
|
340
|
+
/** A message in a temporary conversation, which only the client keeps */
|
|
341
|
+
export const TemporaryMessage = Message.pick({ id: true, role: true, toolCalls: true, toolCallId: true, status: true }).extend({
|
|
342
|
+
content: z.string().max(100_000),
|
|
343
|
+
voice: z.boolean().default(false),
|
|
344
|
+
created: z.coerce.date(),
|
|
345
|
+
});
|
|
346
|
+
export const TemporaryInit = z.object({
|
|
347
|
+
/** `null` uses the default model */
|
|
348
|
+
model: ModelId.nullish(),
|
|
349
|
+
reasoning: Reasoning,
|
|
350
|
+
/** Leaves out the user's memories and tools that use their data */
|
|
351
|
+
anonymous: z.boolean().default(false),
|
|
352
|
+
/** The conversation so far, ending with the message to respond to or the tool calls being decided */
|
|
353
|
+
messages: TemporaryMessage.array().min(1).max(1000),
|
|
354
|
+
/** For the tool calls at the end of `messages` that await the user */
|
|
355
|
+
decisions: Decisions.optional(),
|
|
356
|
+
/** Names of tools the user allowed for the rest of the conversation */
|
|
357
|
+
allowedTools: z.string().array().max(100).default([]),
|
|
358
|
+
});
|
|
359
|
+
export const TimerLabel = z.string().trim().min(1).max(100);
|
|
360
|
+
export const TimerMessage = z.string().trim().min(1).max(200);
|
|
361
|
+
/**
|
|
362
|
+
* The longest timer, in seconds
|
|
363
|
+
* @todo Temporal
|
|
364
|
+
*/
|
|
365
|
+
export const maxTimerDuration = 7 * 86_400;
|
|
366
|
+
export const Timer = z.object({
|
|
367
|
+
id: z.uuid(),
|
|
368
|
+
label: TimerLabel.nullable(),
|
|
369
|
+
/** Shown when the timer ends */
|
|
370
|
+
message: TimerMessage.nullable(),
|
|
371
|
+
/** In seconds @todo Temporal */
|
|
372
|
+
duration: z.int().positive(),
|
|
373
|
+
created: z.coerce.date(),
|
|
374
|
+
ends: z.coerce.date(),
|
|
375
|
+
});
|
|
376
|
+
const TimerDetails = z.object({
|
|
377
|
+
label: TimerLabel.optional(),
|
|
378
|
+
message: TimerMessage.optional(),
|
|
379
|
+
});
|
|
380
|
+
/** Whether `value` is an ISO 8601 date and time with a UTC offset */
|
|
381
|
+
function isInstant(value) {
|
|
382
|
+
try {
|
|
383
|
+
Temporal.Instant.from(value);
|
|
384
|
+
return true;
|
|
385
|
+
}
|
|
386
|
+
catch {
|
|
387
|
+
return false;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
/**
|
|
391
|
+
* An ISO 8601 date and time with a UTC offset, with or without seconds, e.g. `2026-11-16T15:30-06:00`
|
|
392
|
+
* @see https://github.com/colinhacks/zod/pull/5961
|
|
393
|
+
* @todo Remove once Zod has proper Temporal support built-in
|
|
394
|
+
*/
|
|
395
|
+
export const Instant = z.string().max(100).refine(isInstant, 'Expected a date and time with a UTC offset');
|
|
396
|
+
/** A timer that runs for a length of time, or one that ends at a time */
|
|
397
|
+
export const TimerInit = z.union([
|
|
398
|
+
TimerDetails.extend({
|
|
399
|
+
/** In seconds @todo Temporal */
|
|
400
|
+
duration: z.int().positive().max(maxTimerDuration),
|
|
401
|
+
}),
|
|
402
|
+
TimerDetails.extend({
|
|
403
|
+
/** An ISO 8601 date and time with its UTC offset, e.g. `2026-11-16T15:30-06:00` */
|
|
404
|
+
at: Instant,
|
|
405
|
+
}),
|
|
406
|
+
]);
|
|
407
|
+
export const TimerRemoved = z.object({
|
|
408
|
+
id: z.uuid(),
|
|
409
|
+
/** Whether the timer finished, rather than being cancelled */
|
|
410
|
+
done: z.boolean(),
|
|
411
|
+
});
|
|
412
|
+
export const Preferences = z
|
|
413
|
+
.object({
|
|
414
|
+
/** Used for new conversations instead of the default model */
|
|
415
|
+
model: ModelId.optional(),
|
|
416
|
+
reasoning: z
|
|
417
|
+
.literal(['default', 'none', 'low', 'medium', 'high', 'max'])
|
|
418
|
+
.default('default')
|
|
419
|
+
.register(locales.zod, { prefix: 'ai.reasoning_level' }),
|
|
420
|
+
ctrl_enter: z.boolean().default(false),
|
|
421
|
+
/** Added to the system prompt of every conversation */
|
|
422
|
+
instructions: z.string().max(10_000).optional().register(locales.zod, { multiline: true }),
|
|
423
|
+
tools: z
|
|
424
|
+
.object({
|
|
425
|
+
/** What tools may do with the user's Axium data */
|
|
426
|
+
account_access: z
|
|
427
|
+
.literal(['none', 'read', 'write'])
|
|
428
|
+
.default('none')
|
|
429
|
+
.register(locales.zod, { prefix: 'ai.data_access', feature: 'ai-account-access' }),
|
|
430
|
+
/** Whether tools that change the user's data wait for approval */
|
|
431
|
+
confirm_writes: z
|
|
432
|
+
.boolean()
|
|
433
|
+
.default(true)
|
|
434
|
+
.register(locales.zod, { feature: 'ai-account-access', requires: { account_access: ['write'] } }),
|
|
435
|
+
/** Whether tools may search and read the web */
|
|
436
|
+
web: z.boolean().default(false).register(locales.zod, { feature: 'ai-web-tools' }),
|
|
437
|
+
/** Per-tool overrides of whether write calls ask first, by tool name */
|
|
438
|
+
approvals: z
|
|
439
|
+
.record(z.string(), Approval)
|
|
440
|
+
.default({})
|
|
441
|
+
.register(locales.zod, { feature: 'ai-account-access', requires: { account_access: ['write'] } }),
|
|
442
|
+
/** Whether the assistant uses and saves memories */
|
|
443
|
+
memory: z.boolean().default(true).register(locales.zod, { feature: 'ai-memory' }),
|
|
444
|
+
/** How often the assistant reviews conversations to update its memories */
|
|
445
|
+
memory_updates: memory.UpdateFrequency.default('off').register(locales.zod, {
|
|
446
|
+
prefix: 'ai.memory_updates.frequency',
|
|
447
|
+
feature: 'ai-memory-updates',
|
|
448
|
+
requires: { memory: [true] },
|
|
449
|
+
}),
|
|
450
|
+
})
|
|
451
|
+
.default({ account_access: 'none', confirm_writes: true, web: false, approvals: {}, memory: true, memory_updates: 'off' }),
|
|
452
|
+
})
|
|
453
|
+
.register(locales.zod, { prefix: 'ai.preferences' });
|
|
454
|
+
appPreferences.set('ai', Preferences);
|
|
455
|
+
/** The closest reasoning setting a model supports to a general preference */
|
|
456
|
+
export function preferredReasoning(preference, model) {
|
|
457
|
+
if (preference == 'default' || preference == 'none' || !model?.reasoningLevels)
|
|
458
|
+
return preference;
|
|
459
|
+
const levels = model.reasoningLevels;
|
|
460
|
+
if (!levels.length)
|
|
461
|
+
return defaultReasoning(model) == 'none' ? 'high' : 'default';
|
|
462
|
+
if (levels.includes(preference))
|
|
463
|
+
return preference;
|
|
464
|
+
const ranks = reasoningLevels;
|
|
465
|
+
const target = ranks.indexOf(preference);
|
|
466
|
+
const known = levels.filter(level => ranks.includes(level));
|
|
467
|
+
if (target == -1 || !known.length)
|
|
468
|
+
return 'default';
|
|
469
|
+
return known.reduce((best, level) => (Math.abs(ranks.indexOf(level) - target) < Math.abs(ranks.indexOf(best) - target) ? level : best));
|
|
470
|
+
}
|
|
471
|
+
/** Text to speak; long text is spoken in parts by the client */
|
|
472
|
+
export const SpeechInit = z.object({
|
|
473
|
+
text: z.string().trim().min(1).max(1000),
|
|
474
|
+
});
|
|
475
|
+
export const Transcription = z.object({
|
|
476
|
+
text: z.string(),
|
|
477
|
+
});
|
|
478
|
+
const AIAPI = {
|
|
479
|
+
'ai/models': {
|
|
480
|
+
GET: Models,
|
|
481
|
+
/** Starts downloading a model */
|
|
482
|
+
PUT: [ModelInit, Model],
|
|
483
|
+
},
|
|
484
|
+
'ai/models/:id': {
|
|
485
|
+
GET: Model,
|
|
486
|
+
PATCH: [ModelUpdate, Model],
|
|
487
|
+
POST: [ModelAction, Model],
|
|
488
|
+
DELETE: Model,
|
|
489
|
+
},
|
|
490
|
+
'ai/roles': {
|
|
491
|
+
GET: Roles,
|
|
492
|
+
PATCH: [RolesUpdate, Roles],
|
|
493
|
+
},
|
|
494
|
+
'ai/voice/prepare': {
|
|
495
|
+
/** Loads the speech-to-text and text-to-speech models, so voice is ready when used */
|
|
496
|
+
POST: [z.object({}), z.object({})],
|
|
497
|
+
},
|
|
498
|
+
'ai/hub': {
|
|
499
|
+
GET: [HubSearch, HubRepo.array()],
|
|
500
|
+
},
|
|
501
|
+
'ai/hub/details': {
|
|
502
|
+
GET: [z.object({ repo: HubRepoId }), HubDetails],
|
|
503
|
+
},
|
|
504
|
+
'users/:id/ai/conversations': {
|
|
505
|
+
GET: Conversation.array(),
|
|
506
|
+
PUT: [ConversationInit, Conversation],
|
|
507
|
+
},
|
|
508
|
+
'ai/conversations/:id': {
|
|
509
|
+
GET: Conversation.extend({ messages: Message.array() }),
|
|
510
|
+
PATCH: [ConversationInit, Conversation],
|
|
511
|
+
/** Sends a message and starts the response; returns the user's message and the pending response */
|
|
512
|
+
PUT: [MessageInit, Message.array()],
|
|
513
|
+
/** `message` is the new pending response, for `retry` and `edit` */
|
|
514
|
+
POST: [ConversationAction, z.object({ message: Message.optional() })],
|
|
515
|
+
DELETE: Conversation,
|
|
516
|
+
},
|
|
517
|
+
'ai/temporary/:id': {
|
|
518
|
+
/** Starts a response without saving anything; returns the pending response */
|
|
519
|
+
PUT: [TemporaryInit, Message],
|
|
520
|
+
POST: [z.object({ action: z.literal('cancel') }), z.object({})],
|
|
521
|
+
},
|
|
522
|
+
'users/:id/ai/memories': {
|
|
523
|
+
GET: memory.List,
|
|
524
|
+
PUT: [memory.Init, memory.Node],
|
|
525
|
+
},
|
|
526
|
+
'users/:id/ai/memories/:memory': {
|
|
527
|
+
GET: memory.Node,
|
|
528
|
+
PATCH: [memory.Update, memory.Node],
|
|
529
|
+
DELETE: memory.Node,
|
|
530
|
+
},
|
|
531
|
+
'users/:id/ai/memory_updates': {
|
|
532
|
+
GET: memory.Updates,
|
|
533
|
+
/** Starts an update now */
|
|
534
|
+
POST: [z.object({}), memory.Updates],
|
|
535
|
+
},
|
|
536
|
+
'users/:id/ai/timers': {
|
|
537
|
+
GET: Timer.array(),
|
|
538
|
+
PUT: [TimerInit, Timer],
|
|
539
|
+
},
|
|
540
|
+
'users/:id/ai/timers/:timer': {
|
|
541
|
+
DELETE: Timer,
|
|
542
|
+
},
|
|
543
|
+
};
|
|
544
|
+
Object.assign($API, AIAPI);
|
|
545
|
+
export const MessagesRemoved = z.object({
|
|
546
|
+
conversationId: z.uuid(),
|
|
547
|
+
ids: z.uuid().array(),
|
|
548
|
+
});
|
|
549
|
+
/** Text appended to a field of a pending message */
|
|
550
|
+
export const Delta = z.object({
|
|
551
|
+
conversationId: z.uuid(),
|
|
552
|
+
messageId: z.uuid(),
|
|
553
|
+
field: z.literal(['content', 'reasoning']),
|
|
554
|
+
text: z.string(),
|
|
555
|
+
/** The length of the field before `text`, in UTF-16 code units */
|
|
556
|
+
position: z.int().nonnegative(),
|
|
557
|
+
});
|
|
558
|
+
const AIServerToClient = {
|
|
559
|
+
'ai.delta': [Delta],
|
|
560
|
+
'ai.message': [Message],
|
|
561
|
+
'ai.model': [Model],
|
|
562
|
+
'ai.message.remove': [MessagesRemoved],
|
|
563
|
+
'ai.model.remove': [ModelId],
|
|
564
|
+
'ai.conversation': [Conversation],
|
|
565
|
+
'ai.timer': [Timer],
|
|
566
|
+
'ai.timer.remove': [TimerRemoved],
|
|
567
|
+
'ai.memory_updates': [memory.Updates],
|
|
568
|
+
};
|
|
569
|
+
addServerToClient(AIServerToClient);
|
package/dist/hooks.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import './common.js';
|
|
2
|
+
import './server/api/conversations.js';
|
|
3
|
+
import './server/api/gateway.js';
|
|
4
|
+
import './server/api/memories.js';
|
|
5
|
+
import './server/api/temporary.js';
|
|
6
|
+
import './server/api/timers.js';
|
|
7
|
+
import './server/api/voice.js';
|
|
8
|
+
import './memory.js';
|
|
9
|
+
import './node/web.js';
|
|
10
|
+
import './server/api/models.js';
|
|
11
|
+
export declare function load(): Promise<void>;
|
|
12
|
+
export declare function configChanged(): Promise<void>;
|
|
13
|
+
export declare function statusText(): Promise<string>;
|
package/dist/hooks.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { count } from '@axium/server/database';
|
|
2
|
+
import * as io from 'ioium';
|
|
3
|
+
import './common.js';
|
|
4
|
+
import './server/api/conversations.js';
|
|
5
|
+
import './server/api/gateway.js';
|
|
6
|
+
import './server/api/memories.js';
|
|
7
|
+
import './server/api/temporary.js';
|
|
8
|
+
import './server/api/timers.js';
|
|
9
|
+
import './server/api/voice.js';
|
|
10
|
+
import './memory.js';
|
|
11
|
+
import './node/web.js';
|
|
12
|
+
import { failInterrupted } from './server/generation.js';
|
|
13
|
+
import * as llama from './node/llama.js';
|
|
14
|
+
import './server/api/models.js';
|
|
15
|
+
import { syncPreset } from './server/models.js';
|
|
16
|
+
export async function load() {
|
|
17
|
+
await failInterrupted();
|
|
18
|
+
await syncPreset();
|
|
19
|
+
await llama.start();
|
|
20
|
+
}
|
|
21
|
+
export async function configChanged() {
|
|
22
|
+
if (llama.settingsChanged()) {
|
|
23
|
+
io.info('AI: restarting llama-server with the new settings');
|
|
24
|
+
await llama.stop();
|
|
25
|
+
await syncPreset();
|
|
26
|
+
await llama.start();
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
await syncPreset();
|
|
30
|
+
}
|
|
31
|
+
export async function statusText() {
|
|
32
|
+
const { ai_models, ai_conversations, ai_messages, ai_memories } = await count('ai_models', 'ai_conversations', 'ai_messages', 'ai_memories');
|
|
33
|
+
return `${ai_models} models, ${ai_conversations} conversations, ${ai_messages} messages, ${ai_memories} memories`;
|
|
34
|
+
}
|
package/dist/hub.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type * as ai from './common.js';
|
|
2
|
+
export declare function search(query: string, token?: string): Promise<ai.HubRepo[]>;
|
|
3
|
+
/** @param capacity Memory available for models on the host they would run on, used to say whether each quantization fits */
|
|
4
|
+
export declare function details(repo: string, capacity: ai.MemoryUse, token?: string): Promise<ai.HubDetails>;
|