@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.
- package/LICENSE.md +157 -0
- package/README.md +2 -2
- package/db.json +114 -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 +239 -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/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,34 @@
|
|
|
1
|
+
import * as ai from '../common.js';
|
|
2
|
+
import * as llama from './llama.js';
|
|
3
|
+
/** A model as it is saved, e.g. in the database on servers or the config file on devices */
|
|
4
|
+
export interface Registered extends ai.ModelInfo {
|
|
5
|
+
/** When the capabilities above were learned */
|
|
6
|
+
learned: Date | null;
|
|
7
|
+
}
|
|
8
|
+
export type Capabilities = Pick<Registered, 'reasoningLevels' | 'templateReasoning' | 'tools' | 'learned'>;
|
|
9
|
+
/** Where models are saved */
|
|
10
|
+
export interface Registry {
|
|
11
|
+
/** Sorted by name */
|
|
12
|
+
records(): Promise<Registered[]>;
|
|
13
|
+
record(id: string): Promise<Registered | undefined>;
|
|
14
|
+
saveCapabilities(id: string, capabilities: Capabilities): Promise<Registered>;
|
|
15
|
+
/** Called when a model's state changes */
|
|
16
|
+
changed?(model: ai.Model): void;
|
|
17
|
+
}
|
|
18
|
+
/** Sets where models are saved; required before models change state */
|
|
19
|
+
export declare function useRegistry(source: Registry): void;
|
|
20
|
+
export declare function refresh(): Promise<void>;
|
|
21
|
+
/** A saved model with its state from llama-server's last listing */
|
|
22
|
+
export declare function toModel(record: Registered): ai.Model;
|
|
23
|
+
/** Every saved model with its current state */
|
|
24
|
+
export declare function list(): Promise<ai.Model[]>;
|
|
25
|
+
export declare function get(id: string): Promise<ai.Model | undefined>;
|
|
26
|
+
/** Writes the presets file from the saved models and makes llama-server read it */
|
|
27
|
+
export declare function syncPreset(): Promise<void>;
|
|
28
|
+
export declare function getMemory(): ai.DRAM;
|
|
29
|
+
/** Memory model weights could use, in bytes */
|
|
30
|
+
export declare function getCapacity(): ai.MemoryUse;
|
|
31
|
+
/** Records what a model supports, unless already known. The model must be loaded for llama-server to report anything. */
|
|
32
|
+
export declare function learnCapabilities(id: string): Promise<void>;
|
|
33
|
+
/** Tracks downloads from a router event, returning whether models' states changed in a way worth reporting */
|
|
34
|
+
export declare function track({ model: name, event, data }: llama.RouterEvent): boolean;
|
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
import * as io from 'ioium';
|
|
2
|
+
import { existsSync, readdirSync, readFileSync } from 'node:fs';
|
|
3
|
+
import { freemem, totalmem } from 'node:os';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import * as ai from '../common.js';
|
|
6
|
+
import * as llama from './llama.js';
|
|
7
|
+
let registry;
|
|
8
|
+
/** Sets where models are saved; required before models change state */
|
|
9
|
+
export function useRegistry(source) {
|
|
10
|
+
registry = source;
|
|
11
|
+
}
|
|
12
|
+
function requireRegistry() {
|
|
13
|
+
if (!registry)
|
|
14
|
+
throw new Error('No model registry is in use');
|
|
15
|
+
return registry;
|
|
16
|
+
}
|
|
17
|
+
/** The router's models from its last listing, by router name */
|
|
18
|
+
let listed = new Map();
|
|
19
|
+
/** Downloads in progress, by source */
|
|
20
|
+
const downloads = new Map();
|
|
21
|
+
export async function refresh() {
|
|
22
|
+
if (!llama.isRunning()) {
|
|
23
|
+
listed = new Map();
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
try {
|
|
27
|
+
listed = new Map((await llama.listModels()).map(model => [model.id, model]));
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
io.debug('AI: could not list models:', io.errorText(error));
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
function sumUse(measurement) {
|
|
34
|
+
if (!measurement)
|
|
35
|
+
return;
|
|
36
|
+
const use = { device: 0, host: 0 };
|
|
37
|
+
for (const device of measurement.devices)
|
|
38
|
+
use[device.kind] += device.model + device.context + device.compute;
|
|
39
|
+
return use;
|
|
40
|
+
}
|
|
41
|
+
function measuredMemory(...models) {
|
|
42
|
+
return models.find(model => model?.memory?.status == 'measured')?.memory ?? models.find(model => model?.memory)?.memory;
|
|
43
|
+
}
|
|
44
|
+
function knownModalities(modalities) {
|
|
45
|
+
return modalities?.filter((modality) => {
|
|
46
|
+
if (ai.Modality.safeParse(modality).success)
|
|
47
|
+
return true;
|
|
48
|
+
io.warnOnce(`AI: ignoring unknown modality "${modality}" reported by llama-server`);
|
|
49
|
+
return false;
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
/** A saved model with its state from llama-server's last listing */
|
|
53
|
+
export function toModel(record) {
|
|
54
|
+
const preset = listed.get(record.id);
|
|
55
|
+
const cached = listed.get(record.source);
|
|
56
|
+
const memory = measuredMemory(preset, cached);
|
|
57
|
+
let status;
|
|
58
|
+
let progress = downloads.get(record.source);
|
|
59
|
+
const cachedStatus = cached?.status.value;
|
|
60
|
+
if (progress || cachedStatus == 'downloading' || cachedStatus == 'incomplete') {
|
|
61
|
+
status = cachedStatus == 'incomplete' ? 'incomplete' : 'downloading';
|
|
62
|
+
if (!progress && cached?.status.progress) {
|
|
63
|
+
progress = { done: 0, total: 0 };
|
|
64
|
+
for (const file of Object.values(cached.status.progress)) {
|
|
65
|
+
progress.done += file.done;
|
|
66
|
+
progress.total += file.total;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
else if (!preset?.context_length) {
|
|
71
|
+
status = 'unavailable';
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
status = preset.status.value;
|
|
75
|
+
}
|
|
76
|
+
return {
|
|
77
|
+
id: record.id,
|
|
78
|
+
name: record.name,
|
|
79
|
+
source: record.source,
|
|
80
|
+
size: record.size,
|
|
81
|
+
created: record.created,
|
|
82
|
+
status,
|
|
83
|
+
failed: preset?.status.failed || undefined,
|
|
84
|
+
progress,
|
|
85
|
+
contextLength: preset?.context_length ?? undefined,
|
|
86
|
+
inputs: knownModalities(preset?.architecture?.input_modalities),
|
|
87
|
+
outputs: knownModalities(preset?.architecture?.output_modalities),
|
|
88
|
+
memory: memory && {
|
|
89
|
+
minimum: sumUse(memory.min),
|
|
90
|
+
current: status == 'loaded' ? sumUse(memory.effective) : undefined,
|
|
91
|
+
context: status == 'loaded' ? memory.effective?.n_ctx : undefined,
|
|
92
|
+
},
|
|
93
|
+
reasoningLevels: record.reasoningLevels ?? undefined,
|
|
94
|
+
templateReasoning: record.templateReasoning ?? undefined,
|
|
95
|
+
tools: record.tools ?? undefined,
|
|
96
|
+
reasoning: record.reasoning,
|
|
97
|
+
systemPrompt: record.systemPrompt,
|
|
98
|
+
options: record.options,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
/** Every saved model with its current state */
|
|
102
|
+
export async function list() {
|
|
103
|
+
const [records] = await Promise.all([requireRegistry().records(), refresh()]);
|
|
104
|
+
return records.map(toModel);
|
|
105
|
+
}
|
|
106
|
+
export async function get(id) {
|
|
107
|
+
const [record] = await Promise.all([requireRegistry().record(id), refresh()]);
|
|
108
|
+
return record && toModel(record);
|
|
109
|
+
}
|
|
110
|
+
/** Writes the presets file from the saved models and makes llama-server read it */
|
|
111
|
+
export async function syncPreset() {
|
|
112
|
+
const records = await requireRegistry().records();
|
|
113
|
+
llama.writePreset(records.map(record => ({ id: record.id, source: record.source, options: record.options })));
|
|
114
|
+
await llama.reload();
|
|
115
|
+
}
|
|
116
|
+
/** AMD GPUs report memory use through sysfs, in card order */
|
|
117
|
+
function gpuUse() {
|
|
118
|
+
const gpus = [];
|
|
119
|
+
try {
|
|
120
|
+
for (const card of readdirSync('/sys/class/drm').filter(name => /^card\d+$/.test(name))) {
|
|
121
|
+
const device = join('/sys/class/drm', card, 'device');
|
|
122
|
+
if (!existsSync(join(device, 'mem_info_vram_total')))
|
|
123
|
+
continue;
|
|
124
|
+
gpus.push({
|
|
125
|
+
index: Number(card.slice(4)),
|
|
126
|
+
total: Number(readFileSync(join(device, 'mem_info_vram_total'), 'utf8')),
|
|
127
|
+
used: Number(readFileSync(join(device, 'mem_info_vram_used'), 'utf8')),
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
catch {
|
|
132
|
+
return [];
|
|
133
|
+
}
|
|
134
|
+
return gpus.sort((a, b) => a.index - b.index);
|
|
135
|
+
}
|
|
136
|
+
export function getMemory() {
|
|
137
|
+
const byIndex = new Map();
|
|
138
|
+
for (const model of listed.values()) {
|
|
139
|
+
for (const measurement of [model.memory?.min, model.memory?.effective]) {
|
|
140
|
+
for (const device of measurement?.devices ?? []) {
|
|
141
|
+
if (device.kind != 'device' || !device.total)
|
|
142
|
+
continue;
|
|
143
|
+
const index = Number(/\d+$/.exec(device.name)?.[0] ?? 0);
|
|
144
|
+
const entry = byIndex.get(index) ?? { names: new Set(), total: device.total };
|
|
145
|
+
entry.names.add(device.name);
|
|
146
|
+
entry.total = Math.max(entry.total, device.total);
|
|
147
|
+
entry.free = device.free ?? entry.free;
|
|
148
|
+
byIndex.set(index, entry);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
const gpus = gpuUse();
|
|
153
|
+
const devices = [...byIndex.entries()]
|
|
154
|
+
.sort(([a], [b]) => a - b)
|
|
155
|
+
.map(([index, { names, total, free }]) => ({
|
|
156
|
+
name: [...names].sort().join(' / '),
|
|
157
|
+
total: gpus[index]?.total ?? total,
|
|
158
|
+
used: gpus[index]?.used ?? (free === undefined ? undefined : total - free),
|
|
159
|
+
}));
|
|
160
|
+
if (!devices.length) {
|
|
161
|
+
for (const [index, gpu] of gpus.entries())
|
|
162
|
+
devices.push({ name: 'GPU ' + index, total: gpu.total, used: gpu.used });
|
|
163
|
+
}
|
|
164
|
+
return { devices, host: { name: 'RAM', total: totalmem(), used: totalmem() - freemem() } };
|
|
165
|
+
}
|
|
166
|
+
/** Memory model weights could use, in bytes */
|
|
167
|
+
export function getCapacity() {
|
|
168
|
+
const memory = getMemory();
|
|
169
|
+
return {
|
|
170
|
+
device: memory.devices.reduce((sum, device) => sum + device.total, 0),
|
|
171
|
+
host: memory.host.total - (memory.host.used ?? 0),
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
const learning = new Set();
|
|
175
|
+
/** Records what a model supports, unless already known. The model must be loaded for llama-server to report anything. */
|
|
176
|
+
export async function learnCapabilities(id) {
|
|
177
|
+
if (learning.has(id) || !registry)
|
|
178
|
+
return;
|
|
179
|
+
learning.add(id);
|
|
180
|
+
try {
|
|
181
|
+
const record = await registry.record(id);
|
|
182
|
+
if (!record || record.learned)
|
|
183
|
+
return;
|
|
184
|
+
const signal = AbortSignal.timeout(10_000);
|
|
185
|
+
const caps = await llama.getTemplateCaps(id, signal);
|
|
186
|
+
const reasoningLevels = caps?.reasoning_effort_levels;
|
|
187
|
+
if (!caps || !reasoningLevels)
|
|
188
|
+
return;
|
|
189
|
+
const templateReasoning = (await llama.getTemplateReasoning(id, ['none', ...reasoningLevels], signal)) ?? null;
|
|
190
|
+
const updated = await registry.saveCapabilities(id, {
|
|
191
|
+
reasoningLevels,
|
|
192
|
+
templateReasoning,
|
|
193
|
+
tools: caps.supports_tool_calls ?? null,
|
|
194
|
+
learned: new Date(),
|
|
195
|
+
});
|
|
196
|
+
registry.changed?.(toModel(updated));
|
|
197
|
+
}
|
|
198
|
+
catch (error) {
|
|
199
|
+
io.debug(`AI: could not get capabilities of ${id}: ${io.errorText(error)}`);
|
|
200
|
+
}
|
|
201
|
+
finally {
|
|
202
|
+
learning.delete(id);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
/** Reports the current state of the models a router name refers to, or of every model */
|
|
206
|
+
async function reportChanges(name) {
|
|
207
|
+
if (!registry?.changed)
|
|
208
|
+
return;
|
|
209
|
+
for (const record of await registry.records()) {
|
|
210
|
+
if (!name || record.id == name || record.source == name)
|
|
211
|
+
registry.changed(toModel(record));
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
const lastProgress = new Map();
|
|
215
|
+
/** Whether a progress update for `name` arrived too soon after the last one to be worth reporting */
|
|
216
|
+
function throttled(name) {
|
|
217
|
+
const now = Date.now();
|
|
218
|
+
if (now - (lastProgress.get(name) ?? 0) < 500)
|
|
219
|
+
return true;
|
|
220
|
+
lastProgress.set(name, now);
|
|
221
|
+
return false;
|
|
222
|
+
}
|
|
223
|
+
/** Tracks downloads from a router event, returning whether models' states changed in a way worth reporting */
|
|
224
|
+
export function track({ model: name, event, data }) {
|
|
225
|
+
switch (event) {
|
|
226
|
+
case 'download_progress': {
|
|
227
|
+
const progress = { done: 0, total: 0 };
|
|
228
|
+
for (const file of Object.values(data)) {
|
|
229
|
+
progress.done += file.done;
|
|
230
|
+
progress.total += file.total;
|
|
231
|
+
}
|
|
232
|
+
downloads.set(name, progress);
|
|
233
|
+
return !throttled(name);
|
|
234
|
+
}
|
|
235
|
+
case 'download_finished':
|
|
236
|
+
case 'download_failed':
|
|
237
|
+
downloads.delete(name);
|
|
238
|
+
lastProgress.delete(name);
|
|
239
|
+
return true;
|
|
240
|
+
case 'model_status':
|
|
241
|
+
case 'status_change':
|
|
242
|
+
if (data?.status == 'loading')
|
|
243
|
+
return !(data.progress && throttled(name));
|
|
244
|
+
lastProgress.delete(name);
|
|
245
|
+
return true;
|
|
246
|
+
default:
|
|
247
|
+
return true;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
async function handleEvent(event) {
|
|
251
|
+
if (!track(event))
|
|
252
|
+
return;
|
|
253
|
+
const { model: name, event: type, data } = event;
|
|
254
|
+
if (type == 'download_failed')
|
|
255
|
+
io.warn(`AI: download of ${name} failed: ${JSON.stringify(data ?? {})}`);
|
|
256
|
+
if (type == 'download_finished' || type == 'download_failed')
|
|
257
|
+
await llama.reload().catch(() => { });
|
|
258
|
+
if ((type == 'model_status' || type == 'status_change') && data?.status == 'loaded' && registry) {
|
|
259
|
+
for (const record of await registry.records()) {
|
|
260
|
+
if (record.id == name || record.source == name)
|
|
261
|
+
void learnCapabilities(record.id);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
await refresh();
|
|
265
|
+
await reportChanges(name == '*' ? undefined : name);
|
|
266
|
+
}
|
|
267
|
+
llama.events.on('event', event => {
|
|
268
|
+
void handleEvent(event).catch(error => io.debug(`AI: could not handle ${event.event}: ${io.errorText(error)}`));
|
|
269
|
+
});
|
|
270
|
+
async function handleReady() {
|
|
271
|
+
if (!registry)
|
|
272
|
+
return;
|
|
273
|
+
await refresh();
|
|
274
|
+
for (const record of await registry.records()) {
|
|
275
|
+
const model = toModel(record);
|
|
276
|
+
if (model.status == 'loaded')
|
|
277
|
+
void learnCapabilities(record.id);
|
|
278
|
+
registry.changed?.(model);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
llama.events.on('ready', () => {
|
|
282
|
+
void handleReady().catch(error => io.debug('AI: could not refresh models:', io.errorText(error)));
|
|
283
|
+
});
|
|
284
|
+
llama.events.on('down', () => {
|
|
285
|
+
listed = new Map();
|
|
286
|
+
void reportChanges().catch(() => { });
|
|
287
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/node/web.js
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import { getConfig } from '@axium/core';
|
|
2
|
+
import { lookup as dnsLookup } from 'node:dns';
|
|
3
|
+
import * as http from 'node:http';
|
|
4
|
+
import * as https from 'node:https';
|
|
5
|
+
import { BlockList, isIP } from 'node:net';
|
|
6
|
+
import * as z from 'zod';
|
|
7
|
+
import { htmlToMarkdown } from './markdown.js';
|
|
8
|
+
import * as tools from '../tools.js';
|
|
9
|
+
/** Addresses tools may not reach, so the model can't be used to probe the server's network */
|
|
10
|
+
const blocked = new BlockList();
|
|
11
|
+
for (const [network, prefix] of [
|
|
12
|
+
['0.0.0.0', 8],
|
|
13
|
+
['10.0.0.0', 8],
|
|
14
|
+
['100.64.0.0', 10],
|
|
15
|
+
['127.0.0.0', 8],
|
|
16
|
+
['169.254.0.0', 16],
|
|
17
|
+
['172.16.0.0', 12],
|
|
18
|
+
['192.0.0.0', 24],
|
|
19
|
+
['192.168.0.0', 16],
|
|
20
|
+
['198.18.0.0', 15],
|
|
21
|
+
['224.0.0.0', 4],
|
|
22
|
+
['240.0.0.0', 4],
|
|
23
|
+
])
|
|
24
|
+
blocked.addSubnet(network, prefix, 'ipv4');
|
|
25
|
+
for (const [network, prefix] of [
|
|
26
|
+
['::', 128],
|
|
27
|
+
['::1', 128],
|
|
28
|
+
['64:ff9b::', 96],
|
|
29
|
+
['2001::', 32],
|
|
30
|
+
['2002::', 16],
|
|
31
|
+
['fc00::', 7],
|
|
32
|
+
['fe80::', 10],
|
|
33
|
+
['ff00::', 8],
|
|
34
|
+
])
|
|
35
|
+
blocked.addSubnet(network, prefix, 'ipv6');
|
|
36
|
+
function isBlocked(address) {
|
|
37
|
+
const family = isIP(address);
|
|
38
|
+
return !family || blocked.check(address, family == 6 ? 'ipv6' : 'ipv4');
|
|
39
|
+
}
|
|
40
|
+
/** Resolves like `dns.lookup`, but fails for blocked addresses */
|
|
41
|
+
const publicLookup = ((hostname, options, callback) => {
|
|
42
|
+
dnsLookup(hostname, { ...options, all: true }, (error, addresses) => {
|
|
43
|
+
if (error)
|
|
44
|
+
return callback(error);
|
|
45
|
+
if (!addresses.length || addresses.some(entry => isBlocked(entry.address))) {
|
|
46
|
+
return callback(new Error(`${hostname} resolves to a private or reserved address`));
|
|
47
|
+
}
|
|
48
|
+
if (options.all)
|
|
49
|
+
callback(null, addresses);
|
|
50
|
+
else
|
|
51
|
+
callback(null, addresses[0].address, addresses[0].family);
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
/** Bytes read from a page */
|
|
55
|
+
const maxBytes = 2 * 1024 * 1024;
|
|
56
|
+
const userAgent = 'Mozilla/5.0 (compatible; AxiumAI/1.0)';
|
|
57
|
+
async function fetchPublic(url, signal, redirects = 5) {
|
|
58
|
+
if (url.protocol != 'http:' && url.protocol != 'https:')
|
|
59
|
+
throw new Error('Only http and https URLs can be fetched');
|
|
60
|
+
const literal = url.hostname.replace(/^\[|\]$/g, '');
|
|
61
|
+
if (isIP(literal) && isBlocked(literal))
|
|
62
|
+
throw new Error(`${literal} is a private or reserved address`);
|
|
63
|
+
const response = await new Promise((resolve, reject) => {
|
|
64
|
+
const request = (url.protocol == 'https:' ? https : http).get(url, {
|
|
65
|
+
lookup: publicLookup,
|
|
66
|
+
signal,
|
|
67
|
+
timeout: 15_000,
|
|
68
|
+
headers: { 'User-Agent': userAgent, Accept: 'text/html,text/plain,application/json;q=0.9,*/*;q=0.5' },
|
|
69
|
+
}, resolve);
|
|
70
|
+
request.on('error', reject);
|
|
71
|
+
request.on('timeout', () => request.destroy(new Error('Timed out')));
|
|
72
|
+
});
|
|
73
|
+
const status = response.statusCode ?? 0;
|
|
74
|
+
if (status >= 300 && status < 400 && response.headers.location) {
|
|
75
|
+
response.resume();
|
|
76
|
+
if (!redirects)
|
|
77
|
+
throw new Error('Too many redirects');
|
|
78
|
+
return await fetchPublic(new URL(response.headers.location, url), signal, redirects - 1);
|
|
79
|
+
}
|
|
80
|
+
if (status >= 400) {
|
|
81
|
+
response.resume();
|
|
82
|
+
throw new Error(`The server responded with ${status} ${response.statusMessage ?? ''}`.trim());
|
|
83
|
+
}
|
|
84
|
+
const chunks = [];
|
|
85
|
+
let size = 0;
|
|
86
|
+
for await (const chunk of response) {
|
|
87
|
+
chunks.push(chunk);
|
|
88
|
+
size += chunk.length;
|
|
89
|
+
if (size < maxBytes)
|
|
90
|
+
continue;
|
|
91
|
+
response.destroy();
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
const type = response.headers['content-type'] ?? '';
|
|
95
|
+
const charset = /charset=([\w-]+)/i.exec(type)?.[1] ?? 'utf-8';
|
|
96
|
+
let decoder;
|
|
97
|
+
try {
|
|
98
|
+
decoder = new TextDecoder(charset);
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
decoder = new TextDecoder();
|
|
102
|
+
}
|
|
103
|
+
return { url, type: type.split(';')[0].trim().toLowerCase(), body: decoder.decode(Buffer.concat(chunks).subarray(0, maxBytes)) };
|
|
104
|
+
}
|
|
105
|
+
/** Recently read pages by requested URL, so reading further into one doesn't fetch it again */
|
|
106
|
+
const recent = new Map();
|
|
107
|
+
/** Milliseconds a page is kept */
|
|
108
|
+
const keepTime = 15 * 60_000;
|
|
109
|
+
const keepCount = 32;
|
|
110
|
+
async function read(url, signal) {
|
|
111
|
+
const now = Date.now();
|
|
112
|
+
for (const [key, entry] of recent)
|
|
113
|
+
if (entry.expires < now)
|
|
114
|
+
recent.delete(key);
|
|
115
|
+
const cached = recent.get(url);
|
|
116
|
+
if (cached)
|
|
117
|
+
return cached.page;
|
|
118
|
+
const page = await fetchPublic(new URL(url), signal);
|
|
119
|
+
let readable;
|
|
120
|
+
if (page.type == 'text/html' || page.type == 'application/xhtml+xml') {
|
|
121
|
+
const { title, markdown } = htmlToMarkdown(page.body, page.url);
|
|
122
|
+
readable = { url: page.url.href, title, text: markdown };
|
|
123
|
+
}
|
|
124
|
+
else if (page.type.startsWith('text/') || page.type.includes('json') || page.type.includes('xml')) {
|
|
125
|
+
readable = { url: page.url.href, text: page.body };
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
throw new Error(`Can't read ${page.type || 'this kind of content'}`);
|
|
129
|
+
}
|
|
130
|
+
recent.set(url, { expires: now + keepTime, page: readable });
|
|
131
|
+
if (recent.size > keepCount)
|
|
132
|
+
recent.delete(recent.keys().next().value);
|
|
133
|
+
return readable;
|
|
134
|
+
}
|
|
135
|
+
/** Characters of a page given to the model at once */
|
|
136
|
+
const partSize = 20_000;
|
|
137
|
+
tools.add({
|
|
138
|
+
name: 'fetch_url',
|
|
139
|
+
description: "Reads a public web page or text file. HTML is converted to Markdown with only the page's main content. " +
|
|
140
|
+
'Long pages are returned in parts: when a result has `next`, call again with it as `offset` to read further.',
|
|
141
|
+
environment: 'web',
|
|
142
|
+
access: 'read',
|
|
143
|
+
input: z.object({
|
|
144
|
+
url: z.url({ protocol: /^https?$/ }).describe('An absolute http or https URL'),
|
|
145
|
+
offset: z.int().nonnegative().optional().describe('Where to continue reading, from `next` in an earlier result'),
|
|
146
|
+
}),
|
|
147
|
+
async run({ url, offset = 0 }, { signal }) {
|
|
148
|
+
const page = await read(url, signal);
|
|
149
|
+
let text = page.text.slice(offset, offset + partSize);
|
|
150
|
+
if (offset + text.length < page.text.length) {
|
|
151
|
+
const paragraph = text.lastIndexOf('\n\n');
|
|
152
|
+
const line = text.lastIndexOf('\n');
|
|
153
|
+
const breakAt = paragraph > partSize / 2 ? paragraph : line;
|
|
154
|
+
if (breakAt > partSize / 2)
|
|
155
|
+
text = text.slice(0, breakAt);
|
|
156
|
+
}
|
|
157
|
+
const end = offset + text.length;
|
|
158
|
+
return {
|
|
159
|
+
url: page.url,
|
|
160
|
+
title: page.title,
|
|
161
|
+
text,
|
|
162
|
+
...(end < page.text.length && { next: end, length: page.text.length }),
|
|
163
|
+
};
|
|
164
|
+
},
|
|
165
|
+
});
|
|
166
|
+
const SearchResponse = z.object({
|
|
167
|
+
results: z
|
|
168
|
+
.object({
|
|
169
|
+
url: z.string(),
|
|
170
|
+
title: z.string().optional(),
|
|
171
|
+
content: z.string().optional(),
|
|
172
|
+
publishedDate: z.string().nullish(),
|
|
173
|
+
})
|
|
174
|
+
.array(),
|
|
175
|
+
});
|
|
176
|
+
tools.add({
|
|
177
|
+
name: 'search_web',
|
|
178
|
+
description: 'Searches the web. Returns titles, URLs and snippets; use fetch_url to read a result.',
|
|
179
|
+
environment: 'web',
|
|
180
|
+
access: 'read',
|
|
181
|
+
available: () => !!getConfig('@axium/ai').search_url,
|
|
182
|
+
input: z.object({
|
|
183
|
+
query: z.string().min(1).max(400).describe('What to search for, as you would type it into a search engine'),
|
|
184
|
+
page: z.int().min(1).max(10).optional().describe('Result page, starting at 1'),
|
|
185
|
+
}),
|
|
186
|
+
async run({ query, page }, { signal }) {
|
|
187
|
+
const url = new URL('search', getConfig('@axium/ai').search_url.replace(/\/?$/, '/'));
|
|
188
|
+
url.searchParams.set('q', query);
|
|
189
|
+
url.searchParams.set('format', 'json');
|
|
190
|
+
if (page)
|
|
191
|
+
url.searchParams.set('pageno', String(page));
|
|
192
|
+
const response = await fetch(url, { signal, headers: { Accept: 'application/json' } });
|
|
193
|
+
if (!response.ok)
|
|
194
|
+
throw new Error(`The search service responded with ${response.status}`);
|
|
195
|
+
const { results } = SearchResponse.parse(await response.json());
|
|
196
|
+
return results.slice(0, 10).map(result => ({
|
|
197
|
+
title: result.title,
|
|
198
|
+
url: result.url,
|
|
199
|
+
snippet: result.content,
|
|
200
|
+
published: result.publishedDate ?? undefined,
|
|
201
|
+
}));
|
|
202
|
+
},
|
|
203
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|