@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,224 @@
|
|
|
1
|
+
import { config } from '@axium/client/cli/config';
|
|
2
|
+
import { fetchAPI } from '@axium/client/requests';
|
|
3
|
+
import { configCommand } from '@james-pre/config/cli';
|
|
4
|
+
import { program } from 'commander';
|
|
5
|
+
import * as io from 'ioium/node';
|
|
6
|
+
import { spawn } from 'node:child_process';
|
|
7
|
+
import { styleText } from 'node:util';
|
|
8
|
+
import * as format from 'utilium/format';
|
|
9
|
+
import * as ai from '../common.js';
|
|
10
|
+
import * as llama from '../node/llama.js';
|
|
11
|
+
import * as models from '../node/models.js';
|
|
12
|
+
import { configManager } from './config.js';
|
|
13
|
+
import * as local from './local.js';
|
|
14
|
+
const cli = program.command('ai').helpGroup('Plugins:').description('CLI integration for @axium/ai');
|
|
15
|
+
configCommand(cli, configManager, { defaultType: 'user', sensitive: ['hf_token'] }).description('Manage this device’s AI settings');
|
|
16
|
+
/** The gateway URL and this client's session token */
|
|
17
|
+
function gateway() {
|
|
18
|
+
if (!config.server || !config.token)
|
|
19
|
+
io.exit('Not logged in. Run `axc login` first.', 4);
|
|
20
|
+
return { url: new URL('/api/ai/gateway', config.server).href, token: config.token };
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Checks that a model or role can be used. The gateway resolves roles on each request,
|
|
24
|
+
* so a role keeps working when its first model becomes unavailable.
|
|
25
|
+
*/
|
|
26
|
+
async function checkModel(requested) {
|
|
27
|
+
const { models, roles } = await fetchAPI('GET', 'ai/models');
|
|
28
|
+
const usable = models.filter(ai.isUsable);
|
|
29
|
+
const role = ai.Role.safeParse(requested);
|
|
30
|
+
if (usable.some(model => model.id == requested))
|
|
31
|
+
return;
|
|
32
|
+
if (!role.success) {
|
|
33
|
+
io.exit(`Unknown or unavailable model "${requested}". Available: ${[...usable.map(model => model.id), ...ai.roles].join(', ')}`, 2);
|
|
34
|
+
}
|
|
35
|
+
if (!ai.resolveRole(models, roles, role.data))
|
|
36
|
+
io.exit(`No model is available for the ${role.data} role`, 3);
|
|
37
|
+
}
|
|
38
|
+
/** Environment variables that point Anthropic and OpenAI clients at the gateway */
|
|
39
|
+
async function environment(model = 'coding') {
|
|
40
|
+
const { url, token } = gateway();
|
|
41
|
+
await checkModel(model);
|
|
42
|
+
return {
|
|
43
|
+
ANTHROPIC_BASE_URL: url,
|
|
44
|
+
ANTHROPIC_AUTH_TOKEN: token,
|
|
45
|
+
ANTHROPIC_MODEL: model,
|
|
46
|
+
ANTHROPIC_DEFAULT_OPUS_MODEL: model,
|
|
47
|
+
ANTHROPIC_DEFAULT_SONNET_MODEL: model,
|
|
48
|
+
ANTHROPIC_DEFAULT_HAIKU_MODEL: model,
|
|
49
|
+
CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC: '1',
|
|
50
|
+
OPENAI_BASE_URL: url + '/v1',
|
|
51
|
+
OPENAI_API_KEY: token,
|
|
52
|
+
OPENAI_MODEL: model,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
const shellQuote = (value) => `'${value.replaceAll("'", `'\\''`)}'`;
|
|
56
|
+
/** The device's models, connecting to its llama-server when it runs */
|
|
57
|
+
async function localModels() {
|
|
58
|
+
await local.connect().catch(error => io.warn(io.errorText(error)));
|
|
59
|
+
return (await local.getModels()).models;
|
|
60
|
+
}
|
|
61
|
+
async function serverModels() {
|
|
62
|
+
if (!config.token)
|
|
63
|
+
return;
|
|
64
|
+
return await fetchAPI('GET', 'ai/models').catch(error => {
|
|
65
|
+
io.warn('Could not get the server’s models: ' + io.errorText(error));
|
|
66
|
+
return undefined;
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
function printModel(model, serves) {
|
|
70
|
+
const status = ai.isUsable(model) ? styleText('green', model.status) : styleText('yellow', model.status);
|
|
71
|
+
const progress = model.progress?.total ? ` ${Math.floor((model.progress.done / model.progress.total) * 100)}%` : '';
|
|
72
|
+
console.log(' ' + styleText('bold', model.name), styleText('dim', model.id), status + progress, styleText('blueBright', format.bytes(model.size)), serves.length ? styleText('cyan', `(${serves.join(', ')})`) : '');
|
|
73
|
+
}
|
|
74
|
+
const cli_models = cli
|
|
75
|
+
.command('models')
|
|
76
|
+
.description('List the models on this device and the server')
|
|
77
|
+
.action(async () => {
|
|
78
|
+
const [device, server] = await Promise.all([localModels(), serverModels()]);
|
|
79
|
+
const deviceRoles = local.getRoles();
|
|
80
|
+
console.log(styleText('whiteBright', 'This device:'));
|
|
81
|
+
for (const model of device)
|
|
82
|
+
printModel(model, ai.roles.filter(role => ai.firstServable(device, deviceRoles[role], role)?.id == model.id));
|
|
83
|
+
if (!device.length)
|
|
84
|
+
console.log(styleText('dim', ' No models. Add one with `axc ai models add <repository>`.'));
|
|
85
|
+
if (!server)
|
|
86
|
+
return;
|
|
87
|
+
console.log(styleText('whiteBright', 'Server:'));
|
|
88
|
+
for (const model of server.models) {
|
|
89
|
+
printModel(model, ai.roles.filter(role => ai.resolveRole(server.models, server.roles, role)?.id == model.id));
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
/** Shows a download's progress until it finishes */
|
|
93
|
+
async function followDownload(id, events) {
|
|
94
|
+
const model = await models.get(id);
|
|
95
|
+
if (model?.status != 'downloading')
|
|
96
|
+
return;
|
|
97
|
+
io.start('Downloading ' + model.name);
|
|
98
|
+
for await (const event of events) {
|
|
99
|
+
if (event.model != model.source)
|
|
100
|
+
continue;
|
|
101
|
+
if (event.event == 'download_failed')
|
|
102
|
+
throw new Error('The download failed: ' + JSON.stringify(event.data ?? {}));
|
|
103
|
+
if (event.event == 'download_finished')
|
|
104
|
+
break;
|
|
105
|
+
if (event.event != 'download_progress')
|
|
106
|
+
continue;
|
|
107
|
+
let done = 0, total = 0;
|
|
108
|
+
for (const file of Object.values(event.data)) {
|
|
109
|
+
done += file.done;
|
|
110
|
+
total += file.total;
|
|
111
|
+
}
|
|
112
|
+
if (done < total)
|
|
113
|
+
io.progress(done, total, undefined, `${format.bytes(done)} of ${format.bytes(total)}`);
|
|
114
|
+
}
|
|
115
|
+
io.done();
|
|
116
|
+
}
|
|
117
|
+
cli_models
|
|
118
|
+
.command('add')
|
|
119
|
+
.description('Download a model from Hugging Face to this device')
|
|
120
|
+
.argument('<repository>', 'the Hugging Face repository, e.g. unsloth/gemma-4-E4B-it-GGUF')
|
|
121
|
+
.argument('[quantization]', 'e.g. Q4_K_M; asked for when not given')
|
|
122
|
+
.option('-n, --name <name>', 'the display name')
|
|
123
|
+
.option('-i, --id <id>', 'the model ID, from the name by default')
|
|
124
|
+
.option('--no-wait', 'return once the download starts')
|
|
125
|
+
.action(async (repository, quantization, options) => {
|
|
126
|
+
await local.connect().catch(io.exit);
|
|
127
|
+
io.start('Getting ' + repository);
|
|
128
|
+
const details = await local.details(repository).catch(io.exit);
|
|
129
|
+
io.done();
|
|
130
|
+
const fit = (quant) => ({ device: 'fits in VRAM', host: 'needs RAM', none: styleText('red', 'too large') })[quant.fit];
|
|
131
|
+
quantization ??= await io.select('Quantization', details.quants.map(quant => ({ name: `${quant.tag} (${format.bytes(quant.size)}, ${fit(quant)})`, value: quant.tag })));
|
|
132
|
+
const name = options.name ?? ai.nameFromRepo(details.base ?? details.repo);
|
|
133
|
+
const id = ai.ModelId.parse(options.id ?? ai.modelIdFrom(name));
|
|
134
|
+
const controller = new AbortController();
|
|
135
|
+
const events = await llama.follow(controller.signal).catch(io.exit);
|
|
136
|
+
const model = await local.add({ id, name, repo: details.repo, quant: quantization }).catch(io.exit);
|
|
137
|
+
console.log(`Added ${styleText('bold', model.name)} ${styleText('dim', `(${model.id})`)}`);
|
|
138
|
+
if (options.wait)
|
|
139
|
+
await followDownload(id, events).catch(io.exit);
|
|
140
|
+
controller.abort();
|
|
141
|
+
});
|
|
142
|
+
cli_models
|
|
143
|
+
.command('remove')
|
|
144
|
+
.alias('rm')
|
|
145
|
+
.description('Delete a model from this device')
|
|
146
|
+
.argument('<id>')
|
|
147
|
+
.option('-y, --yes', 'do not ask for confirmation')
|
|
148
|
+
.action(async (id, options) => {
|
|
149
|
+
await local.connect().catch(io.exit);
|
|
150
|
+
if (!options.yes)
|
|
151
|
+
await io.assertYes(`Delete ${id} and its files?`);
|
|
152
|
+
await local.remove(id).catch(io.exit);
|
|
153
|
+
console.log('Deleted', id);
|
|
154
|
+
});
|
|
155
|
+
for (const action of ['load', 'unload']) {
|
|
156
|
+
cli_models
|
|
157
|
+
.command(action)
|
|
158
|
+
.description(`${action == 'load' ? 'Load' : 'Unload'} a model on this device`)
|
|
159
|
+
.argument('<id>')
|
|
160
|
+
.action(async (id) => {
|
|
161
|
+
await local.connect().catch(io.exit);
|
|
162
|
+
if (!(await models.get(id)))
|
|
163
|
+
io.exit(`There is no local model "${id}"`, 2);
|
|
164
|
+
await llama[action](id).catch(io.exit);
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
cli_models
|
|
168
|
+
.command('search')
|
|
169
|
+
.description('Search Hugging Face for models to add')
|
|
170
|
+
.argument('<query...>')
|
|
171
|
+
.action(async (query) => {
|
|
172
|
+
const results = await local.search(query.join(' ')).catch(io.exit);
|
|
173
|
+
for (const repo of results) {
|
|
174
|
+
console.log(styleText('bold', repo.id), styleText('dim', `${repo.downloads.toLocaleString()} downloads, ${repo.likes} likes`));
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
const cli_roles = cli
|
|
178
|
+
.command('roles')
|
|
179
|
+
.description('Show which model each role uses: the first usable model in this device’s list, then the server’s')
|
|
180
|
+
.action(async () => {
|
|
181
|
+
const [device, server] = await Promise.all([localModels(), serverModels()]);
|
|
182
|
+
const deviceRoles = local.getRoles();
|
|
183
|
+
for (const role of ai.roles) {
|
|
184
|
+
const onDevice = ai.firstServable(device, deviceRoles[role], role);
|
|
185
|
+
const onServer = server && ai.resolveRole(server.models, server.roles, role);
|
|
186
|
+
const using = onDevice
|
|
187
|
+
? `${onDevice.name} ${styleText('dim', '(this device)')}`
|
|
188
|
+
: onServer
|
|
189
|
+
? `${onServer.name} ${styleText('dim', '(server)')}`
|
|
190
|
+
: styleText('yellow', 'none');
|
|
191
|
+
const list = deviceRoles[role].length ? styleText('dim', ` [${deviceRoles[role].join(', ')}]`) : '';
|
|
192
|
+
console.log(styleText('bold', role.padEnd(10)), using + list);
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
cli_roles
|
|
196
|
+
.command('set')
|
|
197
|
+
.description('Set the local models a role uses, most preferred first; none clears the list')
|
|
198
|
+
.argument('<role>', ai.roles.join(', '))
|
|
199
|
+
.argument('[models...]')
|
|
200
|
+
.action((role, ids) => {
|
|
201
|
+
const parsed = ai.Role.safeParse(role);
|
|
202
|
+
if (!parsed.success)
|
|
203
|
+
io.exit(`Unknown role "${role}". Roles: ${ai.roles.join(', ')}`, 2);
|
|
204
|
+
local.setRoles({ [parsed.data]: ids });
|
|
205
|
+
});
|
|
206
|
+
cli.command('create-env')
|
|
207
|
+
.description('Print shell exports that point Anthropic and OpenAI clients at the AI gateway, using this client’s session')
|
|
208
|
+
.option('-m, --model <id>', 'a model or role to use instead of the coding role')
|
|
209
|
+
.action(async (options) => {
|
|
210
|
+
for (const [name, value] of Object.entries(await environment(options.model))) {
|
|
211
|
+
console.log(`export ${name}=${shellQuote(value)}`);
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
cli.command('claude')
|
|
215
|
+
.description('Run Claude Code against the AI gateway, using this client’s session. Other arguments are passed to Claude Code.')
|
|
216
|
+
.option('-m, --model <id>', 'a model or role to use instead of the coding role')
|
|
217
|
+
.argument('[args...]')
|
|
218
|
+
.allowUnknownOption()
|
|
219
|
+
.action(async (args, options) => {
|
|
220
|
+
const env = { ...process.env, ...(await environment(options.model)) };
|
|
221
|
+
const child = spawn('claude', args, { stdio: 'inherit', env });
|
|
222
|
+
child.on('error', error => io.exit('Could not run Claude Code: ' + io.errorText(error), 5));
|
|
223
|
+
child.on('exit', (code, signal) => process.exit(code ?? (signal ? 1 : 0)));
|
|
224
|
+
});
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { Manager } from '@james-pre/config';
|
|
2
|
+
import type { PartialRecursive } from 'utilium';
|
|
3
|
+
import * as z from 'zod';
|
|
4
|
+
import type * as llama from '../node/llama.js';
|
|
5
|
+
import type { Registered } from '../node/models.js';
|
|
6
|
+
declare const LocalModel: z.ZodObject<{
|
|
7
|
+
id: z.ZodString;
|
|
8
|
+
source: z.ZodString;
|
|
9
|
+
created: z.ZodCoercedDate<unknown>;
|
|
10
|
+
name: z.ZodString;
|
|
11
|
+
reasoning: z.ZodNullable<z.ZodString>;
|
|
12
|
+
systemPrompt: z.ZodNullable<z.ZodString>;
|
|
13
|
+
size: z.ZodNumber;
|
|
14
|
+
options: z.ZodDefault<z.ZodObject<{
|
|
15
|
+
device: z.ZodOptional<z.ZodString>;
|
|
16
|
+
context: z.ZodOptional<z.ZodInt>;
|
|
17
|
+
reserve: z.ZodOptional<z.ZodInt>;
|
|
18
|
+
parallel: z.ZodOptional<z.ZodInt>;
|
|
19
|
+
mmproj: z.ZodOptional<z.ZodString>;
|
|
20
|
+
}, z.core.$strip>>;
|
|
21
|
+
reasoningLevels: z.ZodOptional<z.ZodNullable<z.ZodArray<z.ZodString>>>;
|
|
22
|
+
templateReasoning: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
23
|
+
tools: z.ZodOptional<z.ZodNullable<z.ZodBoolean>>;
|
|
24
|
+
learned: z.ZodDefault<z.ZodOptional<z.ZodNullable<z.ZodCoercedDate<unknown>>>>;
|
|
25
|
+
}, z.core.$strip>;
|
|
26
|
+
type LocalModel = z.infer<typeof LocalModel>;
|
|
27
|
+
declare const LocalConfig: z.ZodObject<{
|
|
28
|
+
data_dir: z.ZodDefault<z.ZodString>;
|
|
29
|
+
llama: z.ZodDefault<z.ZodObject<{
|
|
30
|
+
command: z.ZodOptional<z.ZodString>;
|
|
31
|
+
host: z.ZodOptional<z.ZodString>;
|
|
32
|
+
port: z.ZodOptional<z.ZodInt>;
|
|
33
|
+
models_max: z.ZodOptional<z.ZodInt>;
|
|
34
|
+
memory_margin: z.ZodOptional<z.ZodInt>;
|
|
35
|
+
defaults: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnion<readonly [z.ZodString, z.ZodNumber, z.ZodBoolean]>>>;
|
|
36
|
+
hf_token: z.ZodOptional<z.ZodOptional<z.ZodString>>;
|
|
37
|
+
}, z.core.$strip>>;
|
|
38
|
+
models: z.ZodDefault<z.ZodArray<z.ZodObject<{
|
|
39
|
+
id: z.ZodString;
|
|
40
|
+
source: z.ZodString;
|
|
41
|
+
created: z.ZodCoercedDate<unknown>;
|
|
42
|
+
name: z.ZodString;
|
|
43
|
+
reasoning: z.ZodNullable<z.ZodString>;
|
|
44
|
+
systemPrompt: z.ZodNullable<z.ZodString>;
|
|
45
|
+
size: z.ZodNumber;
|
|
46
|
+
options: z.ZodDefault<z.ZodObject<{
|
|
47
|
+
device: z.ZodOptional<z.ZodString>;
|
|
48
|
+
context: z.ZodOptional<z.ZodInt>;
|
|
49
|
+
reserve: z.ZodOptional<z.ZodInt>;
|
|
50
|
+
parallel: z.ZodOptional<z.ZodInt>;
|
|
51
|
+
mmproj: z.ZodOptional<z.ZodString>;
|
|
52
|
+
}, z.core.$strip>>;
|
|
53
|
+
reasoningLevels: z.ZodOptional<z.ZodNullable<z.ZodArray<z.ZodString>>>;
|
|
54
|
+
templateReasoning: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
55
|
+
tools: z.ZodOptional<z.ZodNullable<z.ZodBoolean>>;
|
|
56
|
+
learned: z.ZodDefault<z.ZodOptional<z.ZodNullable<z.ZodCoercedDate<unknown>>>>;
|
|
57
|
+
}, z.core.$strip>>>;
|
|
58
|
+
roles: z.ZodDefault<z.ZodRecord<z.ZodLiteral<"chat" | "assistant" | "coding" | "stt" | "tts"> & z.core.$partial, z.ZodArray<z.ZodString>>>;
|
|
59
|
+
}, z.core.$strip>;
|
|
60
|
+
export declare const configManager: Manager<z.ZodObject<{
|
|
61
|
+
data_dir: z.ZodDefault<z.ZodString>;
|
|
62
|
+
llama: z.ZodDefault<z.ZodObject<{
|
|
63
|
+
command: z.ZodOptional<z.ZodString>;
|
|
64
|
+
host: z.ZodOptional<z.ZodString>;
|
|
65
|
+
port: z.ZodOptional<z.ZodInt>;
|
|
66
|
+
models_max: z.ZodOptional<z.ZodInt>;
|
|
67
|
+
memory_margin: z.ZodOptional<z.ZodInt>;
|
|
68
|
+
defaults: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnion<readonly [z.ZodString, z.ZodNumber, z.ZodBoolean]>>>;
|
|
69
|
+
hf_token: z.ZodOptional<z.ZodOptional<z.ZodString>>;
|
|
70
|
+
}, z.core.$strip>>;
|
|
71
|
+
models: z.ZodDefault<z.ZodArray<z.ZodObject<{
|
|
72
|
+
id: z.ZodString;
|
|
73
|
+
source: z.ZodString;
|
|
74
|
+
created: z.ZodCoercedDate<unknown>;
|
|
75
|
+
name: z.ZodString;
|
|
76
|
+
reasoning: z.ZodNullable<z.ZodString>;
|
|
77
|
+
systemPrompt: z.ZodNullable<z.ZodString>;
|
|
78
|
+
size: z.ZodNumber;
|
|
79
|
+
options: z.ZodDefault<z.ZodObject<{
|
|
80
|
+
device: z.ZodOptional<z.ZodString>;
|
|
81
|
+
context: z.ZodOptional<z.ZodInt>;
|
|
82
|
+
reserve: z.ZodOptional<z.ZodInt>;
|
|
83
|
+
parallel: z.ZodOptional<z.ZodInt>;
|
|
84
|
+
mmproj: z.ZodOptional<z.ZodString>;
|
|
85
|
+
}, z.core.$strip>>;
|
|
86
|
+
reasoningLevels: z.ZodOptional<z.ZodNullable<z.ZodArray<z.ZodString>>>;
|
|
87
|
+
templateReasoning: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
88
|
+
tools: z.ZodOptional<z.ZodNullable<z.ZodBoolean>>;
|
|
89
|
+
learned: z.ZodDefault<z.ZodOptional<z.ZodNullable<z.ZodCoercedDate<unknown>>>>;
|
|
90
|
+
}, z.core.$strip>>>;
|
|
91
|
+
roles: z.ZodDefault<z.ZodRecord<z.ZodLiteral<"chat" | "assistant" | "coding" | "stt" | "tts"> & z.core.$partial, z.ZodArray<z.ZodString>>>;
|
|
92
|
+
}, z.core.$strip>, import("@axium/ai").LoadOptions, {
|
|
93
|
+
data_dir?: string | undefined;
|
|
94
|
+
llama?: {
|
|
95
|
+
command?: string | undefined;
|
|
96
|
+
host?: string | undefined;
|
|
97
|
+
port?: number | undefined;
|
|
98
|
+
models_max?: number | undefined;
|
|
99
|
+
memory_margin?: number | undefined;
|
|
100
|
+
defaults?: Record<string, string | number | boolean> | undefined;
|
|
101
|
+
hf_token?: string | undefined;
|
|
102
|
+
} | undefined;
|
|
103
|
+
models?: {
|
|
104
|
+
id: string;
|
|
105
|
+
source: string;
|
|
106
|
+
created: unknown;
|
|
107
|
+
name: string;
|
|
108
|
+
reasoning: string | null;
|
|
109
|
+
systemPrompt: string | null;
|
|
110
|
+
size: number;
|
|
111
|
+
options?: {
|
|
112
|
+
device?: string | undefined;
|
|
113
|
+
context?: number | undefined;
|
|
114
|
+
reserve?: number | undefined;
|
|
115
|
+
parallel?: number | undefined;
|
|
116
|
+
mmproj?: string | undefined;
|
|
117
|
+
} | undefined;
|
|
118
|
+
reasoningLevels?: string[] | null | undefined;
|
|
119
|
+
templateReasoning?: string | null | undefined;
|
|
120
|
+
tools?: boolean | null | undefined;
|
|
121
|
+
learned?: unknown;
|
|
122
|
+
}[] | undefined;
|
|
123
|
+
roles?: Partial<Record<"chat" | "assistant" | "coding" | "stt" | "tts", string[]>> | undefined;
|
|
124
|
+
}, {
|
|
125
|
+
data_dir?: string | undefined;
|
|
126
|
+
llama?: {
|
|
127
|
+
command?: string | undefined;
|
|
128
|
+
host?: string | undefined;
|
|
129
|
+
port?: number | undefined;
|
|
130
|
+
models_max?: number | undefined;
|
|
131
|
+
memory_margin?: number | undefined;
|
|
132
|
+
defaults?: Record<string, string | number | boolean> | undefined;
|
|
133
|
+
hf_token?: string | undefined;
|
|
134
|
+
} | undefined;
|
|
135
|
+
models?: {
|
|
136
|
+
id?: string | undefined;
|
|
137
|
+
source?: string | undefined;
|
|
138
|
+
created?: Date | undefined;
|
|
139
|
+
name?: string | undefined;
|
|
140
|
+
reasoning?: string | null | undefined;
|
|
141
|
+
systemPrompt?: string | null | undefined;
|
|
142
|
+
size?: number | undefined;
|
|
143
|
+
options?: {
|
|
144
|
+
device?: string | undefined;
|
|
145
|
+
context?: number | undefined;
|
|
146
|
+
reserve?: number | undefined;
|
|
147
|
+
parallel?: number | undefined;
|
|
148
|
+
mmproj?: string | undefined;
|
|
149
|
+
} | undefined;
|
|
150
|
+
reasoningLevels?: string[] | null | undefined;
|
|
151
|
+
templateReasoning?: string | null | undefined;
|
|
152
|
+
tools?: boolean | null | undefined;
|
|
153
|
+
learned?: Date | null | undefined;
|
|
154
|
+
}[] | undefined;
|
|
155
|
+
roles?: Partial<Record<"chat" | "assistant" | "coding" | "stt" | "tts", string[]>> | undefined;
|
|
156
|
+
}>;
|
|
157
|
+
/** The device's AI config; other processes may change it, see {@link updateConfig} */
|
|
158
|
+
export declare const config: {
|
|
159
|
+
data_dir: string;
|
|
160
|
+
llama: {
|
|
161
|
+
command?: string | undefined;
|
|
162
|
+
host?: string | undefined;
|
|
163
|
+
port?: number | undefined;
|
|
164
|
+
models_max?: number | undefined;
|
|
165
|
+
memory_margin?: number | undefined;
|
|
166
|
+
defaults?: Record<string, string | number | boolean> | undefined;
|
|
167
|
+
hf_token?: string | undefined;
|
|
168
|
+
};
|
|
169
|
+
models: {
|
|
170
|
+
id: string;
|
|
171
|
+
source: string;
|
|
172
|
+
created: Date;
|
|
173
|
+
name: string;
|
|
174
|
+
reasoning: string | null;
|
|
175
|
+
systemPrompt: string | null;
|
|
176
|
+
size: number;
|
|
177
|
+
options: {
|
|
178
|
+
device?: string | undefined;
|
|
179
|
+
context?: number | undefined;
|
|
180
|
+
reserve?: number | undefined;
|
|
181
|
+
parallel?: number | undefined;
|
|
182
|
+
mmproj?: string | undefined;
|
|
183
|
+
};
|
|
184
|
+
learned: Date | null;
|
|
185
|
+
reasoningLevels?: string[] | null | undefined;
|
|
186
|
+
templateReasoning?: string | null | undefined;
|
|
187
|
+
tools?: boolean | null | undefined;
|
|
188
|
+
}[];
|
|
189
|
+
roles: Partial<Record<"chat" | "assistant" | "coding" | "stt" | "tts", string[]>>;
|
|
190
|
+
};
|
|
191
|
+
/** Saves changes to the device's AI config, keeping changes other processes made */
|
|
192
|
+
export declare function updateConfig(change: (current: typeof config) => PartialRecursive<z.input<typeof LocalConfig>>): void;
|
|
193
|
+
export declare function settings(): llama.Settings;
|
|
194
|
+
export declare function toRecord(model: LocalModel): Registered;
|
|
195
|
+
export declare function fromRecord(record: Registered): LocalModel;
|
|
196
|
+
export {};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { getConfig } from '@axium/core';
|
|
2
|
+
import { Manager } from '@james-pre/config';
|
|
3
|
+
import * as io from 'ioium/node';
|
|
4
|
+
import { homedir } from 'node:os';
|
|
5
|
+
import { join } from 'node:path';
|
|
6
|
+
import * as z from 'zod';
|
|
7
|
+
import * as ai from '../common.js';
|
|
8
|
+
const LocalModel = ai.Model.pick({ id: true, name: true, source: true, created: true, reasoning: true, systemPrompt: true }).extend({
|
|
9
|
+
/** Bytes; JSON has no big integers */
|
|
10
|
+
size: z.number().nonnegative(),
|
|
11
|
+
options: ai.ModelOptions.default({}),
|
|
12
|
+
reasoningLevels: z.string().array().nullish(),
|
|
13
|
+
templateReasoning: z.string().nullish(),
|
|
14
|
+
tools: z.boolean().nullish(),
|
|
15
|
+
learned: z.coerce.date().nullish().default(null),
|
|
16
|
+
});
|
|
17
|
+
const LocalConfig = z.object({
|
|
18
|
+
/** Where models and llama-server's files are kept */
|
|
19
|
+
data_dir: z.string().default(join(process.env.XDG_DATA_HOME || join(homedir(), '.local/share'), 'axium/ai')),
|
|
20
|
+
/** Overrides of the server's default llama-server settings */
|
|
21
|
+
llama: ai.LlamaConfig.partial().default({}),
|
|
22
|
+
models: LocalModel.array().default([]),
|
|
23
|
+
/** Local models for each role, most preferred first; the server's roles are used after these */
|
|
24
|
+
roles: ai.RolesUpdate.default({}),
|
|
25
|
+
});
|
|
26
|
+
export const configManager = new Manager(LocalConfig, { xdg: 'axium/ai' })
|
|
27
|
+
.on('load_error', (path, stage, error) => {
|
|
28
|
+
if (error.code == 'ENOENT')
|
|
29
|
+
return;
|
|
30
|
+
io.warn(`AI: could not load ${path}:`, io.errorText(error));
|
|
31
|
+
})
|
|
32
|
+
.on('write', path => io.debug('AI: saved config to', path));
|
|
33
|
+
configManager.loadDefaults();
|
|
34
|
+
/** The device's AI config; other processes may change it, see {@link updateConfig} */
|
|
35
|
+
export const config = configManager.data;
|
|
36
|
+
/** Saves changes to the device's AI config, keeping changes other processes made */
|
|
37
|
+
export function updateConfig(change) {
|
|
38
|
+
configManager.reloadFiles();
|
|
39
|
+
configManager.update(change(config));
|
|
40
|
+
}
|
|
41
|
+
/** The port llama-server listens on for the device, so it doesn't clash with a server on the same machine */
|
|
42
|
+
const devicePort = 6688;
|
|
43
|
+
export function settings() {
|
|
44
|
+
const base = getConfig('@axium/ai').llama;
|
|
45
|
+
return {
|
|
46
|
+
data_dir: config.data_dir,
|
|
47
|
+
llama: { ...base, port: devicePort, ...config.llama, defaults: { ...base.defaults, ...config.llama.defaults } },
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
export function toRecord(model) {
|
|
51
|
+
return { ...model, size: BigInt(model.size) };
|
|
52
|
+
}
|
|
53
|
+
export function fromRecord(record) {
|
|
54
|
+
return { ...record, size: Number(record.size) };
|
|
55
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import * as io from 'ioium/node';
|
|
2
|
+
import { mkdirSync, watch } from 'node:fs';
|
|
3
|
+
import { basename, dirname } from 'node:path';
|
|
4
|
+
import * as llama from '../node/llama.js';
|
|
5
|
+
import * as models from '../node/models.js';
|
|
6
|
+
import { configManager } from './config.js';
|
|
7
|
+
import './local.js';
|
|
8
|
+
async function applyConfig() {
|
|
9
|
+
if (!llama.settingsChanged())
|
|
10
|
+
return await models.syncPreset();
|
|
11
|
+
io.info('AI: restarting llama-server with the new settings');
|
|
12
|
+
await llama.stop();
|
|
13
|
+
await models.syncPreset();
|
|
14
|
+
await llama.start();
|
|
15
|
+
}
|
|
16
|
+
/** Runs the device's llama-server while the daemon runs, following changes to the device's AI config */
|
|
17
|
+
export async function daemon() {
|
|
18
|
+
await models.syncPreset();
|
|
19
|
+
await llama.start();
|
|
20
|
+
const path = configManager.findPath('user');
|
|
21
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
22
|
+
let timer;
|
|
23
|
+
watch(dirname(path), (_, filename) => {
|
|
24
|
+
if (filename != basename(path))
|
|
25
|
+
return;
|
|
26
|
+
clearTimeout(timer);
|
|
27
|
+
timer = setTimeout(() => {
|
|
28
|
+
configManager.reloadFiles();
|
|
29
|
+
void applyConfig().catch(error => io.error('AI: could not apply the config:', io.errorText(error)));
|
|
30
|
+
}, 500);
|
|
31
|
+
});
|
|
32
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import * as ai from '../common.js';
|
|
2
|
+
/**
|
|
3
|
+
* Uses the llama-server the device daemon runs.
|
|
4
|
+
* @throws when it isn't running
|
|
5
|
+
*/
|
|
6
|
+
export declare function connect(): Promise<void>;
|
|
7
|
+
/** The device's models and roles, with their current state */
|
|
8
|
+
export declare function getModels(): Promise<Pick<ai.Models, 'models' | 'roles' | 'memory' | 'running'>>;
|
|
9
|
+
export declare function getRoles(): ai.Roles;
|
|
10
|
+
/** Replaces the local model lists of the given roles */
|
|
11
|
+
export declare function setRoles(update: ai.RolesUpdate): void;
|
|
12
|
+
export declare function details(repo: string): Promise<ai.HubDetails>;
|
|
13
|
+
export declare function search(query: string): Promise<ai.HubRepo[]>;
|
|
14
|
+
/** Saves a model and starts downloading it */
|
|
15
|
+
export declare function add(init: ai.ModelInit): Promise<ai.Model>;
|
|
16
|
+
/** Deletes a model, and its files unless another model uses them */
|
|
17
|
+
export declare function remove(id: string): Promise<void>;
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import * as ai from '../common.js';
|
|
2
|
+
import * as hub from '../hub.js';
|
|
3
|
+
import * as llama from '../node/llama.js';
|
|
4
|
+
import * as models from '../node/models.js';
|
|
5
|
+
import { config, fromRecord, settings, toRecord, updateConfig } from './config.js';
|
|
6
|
+
llama.useSettings(settings);
|
|
7
|
+
function records() {
|
|
8
|
+
return config.models.map(toRecord).sort((a, b) => a.name.localeCompare(b.name));
|
|
9
|
+
}
|
|
10
|
+
models.useRegistry({
|
|
11
|
+
records: () => Promise.resolve(records()),
|
|
12
|
+
record: id => Promise.resolve(records().find(record => record.id == id)),
|
|
13
|
+
saveCapabilities(id, capabilities) {
|
|
14
|
+
updateConfig(current => ({
|
|
15
|
+
models: current.models.map(model => (model.id == id ? { ...model, ...capabilities } : model)),
|
|
16
|
+
}));
|
|
17
|
+
const saved = config.models.find(model => model.id == id);
|
|
18
|
+
return saved ? Promise.resolve(toRecord(saved)) : Promise.reject(new ReferenceError(`Model "${id}" not found`));
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
/**
|
|
22
|
+
* Uses the llama-server the device daemon runs.
|
|
23
|
+
* @throws when it isn't running
|
|
24
|
+
*/
|
|
25
|
+
export async function connect() {
|
|
26
|
+
if (await llama.attach())
|
|
27
|
+
return;
|
|
28
|
+
throw new Error("llama-server isn't running on this device. Start the Axium client daemon with `axc daemon`.");
|
|
29
|
+
}
|
|
30
|
+
/** The device's models and roles, with their current state */
|
|
31
|
+
export async function getModels() {
|
|
32
|
+
return { models: await models.list(), roles: getRoles(), memory: models.getMemory(), running: llama.isRunning() };
|
|
33
|
+
}
|
|
34
|
+
export function getRoles() {
|
|
35
|
+
const { roles } = config;
|
|
36
|
+
return Object.fromEntries(ai.roles.map(role => [role, roles[role] ?? []]));
|
|
37
|
+
}
|
|
38
|
+
/** Replaces the local model lists of the given roles */
|
|
39
|
+
export function setRoles(update) {
|
|
40
|
+
const known = new Set(records().map(record => record.id));
|
|
41
|
+
for (const role of ai.roles) {
|
|
42
|
+
const list = update[role];
|
|
43
|
+
if (!list)
|
|
44
|
+
continue;
|
|
45
|
+
const unknown = list.find(id => !known.has(id));
|
|
46
|
+
if (unknown)
|
|
47
|
+
throw new ReferenceError(`There is no local model "${unknown}"`);
|
|
48
|
+
if (new Set(list).size != list.length)
|
|
49
|
+
throw new Error('A model is listed twice for the same role');
|
|
50
|
+
}
|
|
51
|
+
updateConfig(current => ({ roles: { ...current.roles, ...update } }));
|
|
52
|
+
}
|
|
53
|
+
export function details(repo) {
|
|
54
|
+
return hub.details(repo, models.getCapacity(), settings().llama.hf_token);
|
|
55
|
+
}
|
|
56
|
+
export function search(query) {
|
|
57
|
+
return hub.search(query, settings().llama.hf_token);
|
|
58
|
+
}
|
|
59
|
+
/** Saves a model and starts downloading it */
|
|
60
|
+
export async function add(init) {
|
|
61
|
+
if (records().some(record => record.id == init.id))
|
|
62
|
+
throw new Error(`A model with the ID "${init.id}" already exists`);
|
|
63
|
+
const repo = await details(init.repo);
|
|
64
|
+
const quant = repo.quants.find(quant => quant.tag == init.quant);
|
|
65
|
+
if (!quant)
|
|
66
|
+
throw new Error(`${repo.repo} has no ${init.quant} quantization`);
|
|
67
|
+
const record = {
|
|
68
|
+
id: init.id,
|
|
69
|
+
name: init.name,
|
|
70
|
+
source: `${repo.repo}:${quant.tag}`,
|
|
71
|
+
size: quant.size,
|
|
72
|
+
created: new Date(),
|
|
73
|
+
reasoning: null,
|
|
74
|
+
systemPrompt: null,
|
|
75
|
+
options: init.options ?? {},
|
|
76
|
+
reasoningLevels: null,
|
|
77
|
+
templateReasoning: null,
|
|
78
|
+
tools: null,
|
|
79
|
+
learned: null,
|
|
80
|
+
};
|
|
81
|
+
updateConfig(current => ({ models: [...current.models, fromRecord(record)] }));
|
|
82
|
+
try {
|
|
83
|
+
await models.syncPreset();
|
|
84
|
+
await llama.download(record.source);
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
updateConfig(current => ({ models: current.models.filter(model => model.id != init.id) }));
|
|
88
|
+
await models.syncPreset().catch(() => { });
|
|
89
|
+
throw error;
|
|
90
|
+
}
|
|
91
|
+
return models.toModel(record);
|
|
92
|
+
}
|
|
93
|
+
/** Deletes a model, and its files unless another model uses them */
|
|
94
|
+
export async function remove(id) {
|
|
95
|
+
const model = await models.get(id);
|
|
96
|
+
if (!model)
|
|
97
|
+
throw new ReferenceError(`There is no local model "${id}"`);
|
|
98
|
+
if (model.status == 'loaded' || model.status == 'loading' || model.status == 'sleeping')
|
|
99
|
+
await llama.unload(id);
|
|
100
|
+
const shared = records().some(record => record.id != id && record.source == model.source);
|
|
101
|
+
if (!shared) {
|
|
102
|
+
await llama.remove(model.source).catch(error => {
|
|
103
|
+
if (model.status != 'unavailable')
|
|
104
|
+
throw error;
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
updateConfig(current => ({
|
|
108
|
+
models: current.models.filter(model => model.id != id),
|
|
109
|
+
roles: Object.fromEntries(Object.entries(current.roles).map(([role, ids]) => [role, ids.filter(model => model != id)])),
|
|
110
|
+
}));
|
|
111
|
+
await models.syncPreset();
|
|
112
|
+
}
|