@axium/ai 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. package/LICENSE.md +157 -0
  2. package/README.md +2 -2
  3. package/db.json +114 -0
  4. package/dist/client/cli.d.ts +1 -0
  5. package/dist/client/cli.js +224 -0
  6. package/dist/client/config.d.ts +196 -0
  7. package/dist/client/config.js +55 -0
  8. package/dist/client/hooks.d.ts +3 -0
  9. package/dist/client/hooks.js +32 -0
  10. package/dist/client/local.d.ts +17 -0
  11. package/dist/client/local.js +112 -0
  12. package/dist/common.d.ts +1318 -0
  13. package/dist/common.js +569 -0
  14. package/dist/hooks.d.ts +13 -0
  15. package/dist/hooks.js +34 -0
  16. package/dist/hub.d.ts +4 -0
  17. package/dist/hub.js +75 -0
  18. package/dist/index.d.ts +1 -0
  19. package/dist/index.js +1 -0
  20. package/dist/integrations/notes.d.ts +1 -0
  21. package/dist/integrations/notes.js +104 -0
  22. package/dist/integrations/tasks.d.ts +1 -0
  23. package/dist/integrations/tasks.js +157 -0
  24. package/dist/loop.d.ts +75 -0
  25. package/dist/loop.js +239 -0
  26. package/dist/memory.d.ts +107 -0
  27. package/dist/memory.js +193 -0
  28. package/dist/node/chat.d.ts +50 -0
  29. package/dist/node/chat.js +60 -0
  30. package/dist/node/llama.d.ts +115 -0
  31. package/dist/node/llama.js +385 -0
  32. package/dist/node/markdown.d.ts +6 -0
  33. package/dist/node/markdown.js +285 -0
  34. package/dist/node/models.d.ts +34 -0
  35. package/dist/node/models.js +287 -0
  36. package/dist/node/web.d.ts +1 -0
  37. package/dist/node/web.js +203 -0
  38. package/dist/server/api/conversations.d.ts +1 -0
  39. package/dist/server/api/conversations.js +216 -0
  40. package/dist/server/api/gateway.d.ts +1 -0
  41. package/dist/server/api/gateway.js +113 -0
  42. package/dist/server/api/memories.d.ts +1 -0
  43. package/dist/server/api/memories.js +129 -0
  44. package/dist/server/api/models.d.ts +1 -0
  45. package/dist/server/api/models.js +161 -0
  46. package/dist/server/api/temporary.d.ts +1 -0
  47. package/dist/server/api/temporary.js +50 -0
  48. package/dist/server/api/timers.d.ts +1 -0
  49. package/dist/server/api/timers.js +27 -0
  50. package/dist/server/api/voice.d.ts +1 -0
  51. package/dist/server/api/voice.js +188 -0
  52. package/dist/server/generation.d.ts +38 -0
  53. package/dist/server/generation.js +299 -0
  54. package/dist/server/memory.d.ts +7 -0
  55. package/dist/server/memory.js +218 -0
  56. package/dist/server/models.d.ts +13 -0
  57. package/dist/server/models.js +59 -0
  58. package/dist/server/preferences.d.ts +3 -0
  59. package/dist/server/preferences.js +12 -0
  60. package/dist/server/timers.d.ts +7 -0
  61. package/dist/server/timers.js +66 -0
  62. package/dist/speech.d.ts +17 -0
  63. package/dist/speech.js +69 -0
  64. package/dist/timers.d.ts +1 -0
  65. package/dist/timers.js +67 -0
  66. package/dist/tools.d.ts +70 -0
  67. package/dist/tools.js +84 -0
  68. package/lib/ChatMessage.svelte +331 -0
  69. package/lib/Composer.svelte +234 -0
  70. package/lib/Conversation.svelte +433 -0
  71. package/lib/ConversationMenu.svelte +18 -0
  72. package/lib/ModelActions.svelte +71 -0
  73. package/lib/ModelOptions.svelte +101 -0
  74. package/lib/ModelStatus.svelte +49 -0
  75. package/lib/ToolCall.svelte +171 -0
  76. package/lib/VoiceMode.svelte +113 -0
  77. package/lib/actions.ts +21 -0
  78. package/lib/index.ts +7 -0
  79. package/lib/live.svelte.ts +113 -0
  80. package/lib/markdown.ts +40 -0
  81. package/lib/preferences.ts +13 -0
  82. package/lib/transition.ts +24 -0
  83. package/lib/tsconfig.json +14 -0
  84. package/lib/voice.svelte.ts +476 -0
  85. package/locales/en.json +316 -0
  86. package/package.json +139 -5
  87. package/routes/ai/+layout.svelte +177 -0
  88. package/routes/ai/+layout.ts +26 -0
  89. package/routes/ai/+page.svelte +109 -0
  90. package/routes/ai/TimerAlerts.svelte +51 -0
  91. package/routes/ai/Timers.svelte +121 -0
  92. package/routes/ai/[id]/+page.svelte +18 -0
  93. package/routes/ai/[id]/+page.ts +14 -0
  94. package/routes/ai/memory/+page.svelte +313 -0
  95. package/routes/ai/memory/+page.ts +10 -0
  96. package/routes/ai/memory/MemoryForm.svelte +111 -0
  97. package/routes/ai/models/+page.svelte +199 -0
  98. package/routes/ai/models/+page.ts +5 -0
  99. package/routes/ai/models/AddModel.svelte +240 -0
  100. package/routes/ai/models/MemoryMeter.svelte +120 -0
  101. package/routes/ai/models/Roles.svelte +159 -0
  102. package/routes/ai/models/[id]/+page.svelte +331 -0
  103. package/routes/ai/models/[id]/+page.ts +6 -0
  104. package/routes/ai/timers.svelte.ts +94 -0
  105. package/routes/tsconfig.json +14 -0
@@ -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
+ name: z.ZodString;
9
+ source: z.ZodString;
10
+ created: z.ZodCoercedDate<unknown>;
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
+ name: z.ZodString;
41
+ source: z.ZodString;
42
+ created: z.ZodCoercedDate<unknown>;
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
+ name: z.ZodString;
74
+ source: z.ZodString;
75
+ created: z.ZodCoercedDate<unknown>;
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
+ name: string;
106
+ source: string;
107
+ created: unknown;
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
+ name?: string | undefined;
138
+ source?: string | undefined;
139
+ created?: Date | 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
+ name: string;
172
+ source: string;
173
+ created: Date;
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,3 @@
1
+ import './local.js';
2
+ /** Runs the device's llama-server while the daemon runs, following changes to the device's AI config */
3
+ export declare function daemon(): Promise<void>;
@@ -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
+ }