@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,385 @@
|
|
|
1
|
+
import { getConfig } from '@axium/core';
|
|
2
|
+
import * as io from 'ioium';
|
|
3
|
+
import { spawn } from 'node:child_process';
|
|
4
|
+
import { createHash, randomBytes } from 'node:crypto';
|
|
5
|
+
import { EventEmitter } from 'node:events';
|
|
6
|
+
import { closeSync, existsSync, mkdirSync, openSync, readFileSync, writeFileSync } from 'node:fs';
|
|
7
|
+
import { basename, join } from 'node:path';
|
|
8
|
+
import { setTimeout as sleep } from 'node:timers/promises';
|
|
9
|
+
/** Emits `event` with each {@link RouterEvent}, `ready` once llama-server answers, and `down` when it stops answering */
|
|
10
|
+
export const events = new EventEmitter();
|
|
11
|
+
let settings = () => getConfig('@axium/ai');
|
|
12
|
+
/** Uses settings other than the plugin's config, e.g. a device's own */
|
|
13
|
+
export function useSettings(source) {
|
|
14
|
+
settings = source;
|
|
15
|
+
}
|
|
16
|
+
let child;
|
|
17
|
+
let state;
|
|
18
|
+
let failures = 0;
|
|
19
|
+
let restartTimer;
|
|
20
|
+
let stopping = false;
|
|
21
|
+
let running = false;
|
|
22
|
+
export function isRunning() {
|
|
23
|
+
return running;
|
|
24
|
+
}
|
|
25
|
+
function directory() {
|
|
26
|
+
return settings().data_dir;
|
|
27
|
+
}
|
|
28
|
+
const statePath = () => join(directory(), 'llama-server.json');
|
|
29
|
+
const presetPath = () => join(directory(), 'models.ini');
|
|
30
|
+
function baseURL() {
|
|
31
|
+
const { host, port } = settings().llama;
|
|
32
|
+
return `http://${host.includes(':') ? `[${host}]` : host}:${port}/`;
|
|
33
|
+
}
|
|
34
|
+
function presetValue(value) {
|
|
35
|
+
return String(value).replace(/[\r\n]/g, ' ');
|
|
36
|
+
}
|
|
37
|
+
/** Writes the presets file llama-server loads models from */
|
|
38
|
+
export function writePreset(models) {
|
|
39
|
+
const { llama } = settings();
|
|
40
|
+
const lines = ['version = 1', '', '[*]'];
|
|
41
|
+
for (const [key, value] of Object.entries(llama.defaults))
|
|
42
|
+
lines.push(`${key} = ${presetValue(value)}`);
|
|
43
|
+
lines.push('dedup-cache-models = true');
|
|
44
|
+
for (const model of models) {
|
|
45
|
+
lines.push('', `[${model.id}]`, `hf-repo = ${model.source}`);
|
|
46
|
+
const { device, context, reserve, parallel, mmproj } = model.options;
|
|
47
|
+
if (mmproj)
|
|
48
|
+
lines.push(`mmproj = ${presetValue(mmproj)}`);
|
|
49
|
+
if (device)
|
|
50
|
+
lines.push(`device = ${device}`);
|
|
51
|
+
if (context)
|
|
52
|
+
lines.push(`ctx-size = ${context}`);
|
|
53
|
+
if (reserve !== undefined)
|
|
54
|
+
lines.push(`fit-target = ${reserve}`);
|
|
55
|
+
if (parallel)
|
|
56
|
+
lines.push(`parallel = ${parallel}`);
|
|
57
|
+
}
|
|
58
|
+
mkdirSync(directory(), { recursive: true });
|
|
59
|
+
writeFileSync(presetPath(), lines.join('\n') + '\n');
|
|
60
|
+
}
|
|
61
|
+
function invocation() {
|
|
62
|
+
const { llama, data_dir } = settings();
|
|
63
|
+
const args = [
|
|
64
|
+
'--models-preset',
|
|
65
|
+
presetPath(),
|
|
66
|
+
'--host',
|
|
67
|
+
llama.host,
|
|
68
|
+
'--port',
|
|
69
|
+
String(llama.port),
|
|
70
|
+
'--models-max',
|
|
71
|
+
String(llama.models_max),
|
|
72
|
+
'--models-memory-margin',
|
|
73
|
+
String(llama.memory_margin),
|
|
74
|
+
];
|
|
75
|
+
const env = { LLAMA_CACHE: join(data_dir, 'models') };
|
|
76
|
+
if (llama.hf_token)
|
|
77
|
+
env.HF_TOKEN = llama.hf_token;
|
|
78
|
+
const fingerprint = createHash('sha256')
|
|
79
|
+
.update(JSON.stringify([llama.command, args, env]))
|
|
80
|
+
.digest('base64url');
|
|
81
|
+
return { command: llama.command, args, env, fingerprint };
|
|
82
|
+
}
|
|
83
|
+
function readState() {
|
|
84
|
+
if (!existsSync(statePath()))
|
|
85
|
+
return;
|
|
86
|
+
try {
|
|
87
|
+
return JSON.parse(readFileSync(statePath(), 'utf8'));
|
|
88
|
+
}
|
|
89
|
+
catch {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
function isAlive(pid) {
|
|
94
|
+
try {
|
|
95
|
+
process.kill(pid, 0);
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
catch {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/** Whether `pid` is a llama-server process */
|
|
103
|
+
function isLlamaServer(pid, command) {
|
|
104
|
+
try {
|
|
105
|
+
const [executable] = readFileSync(`/proc/${pid}/cmdline`, 'utf8').split('\0');
|
|
106
|
+
return basename(executable) == basename(command);
|
|
107
|
+
}
|
|
108
|
+
catch {
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
async function terminate(pid) {
|
|
113
|
+
try {
|
|
114
|
+
process.kill(pid, 'SIGTERM');
|
|
115
|
+
}
|
|
116
|
+
catch {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
for (let waited = 0; waited < 15_000; waited += 250) {
|
|
120
|
+
if (!isAlive(pid))
|
|
121
|
+
return;
|
|
122
|
+
await sleep(250);
|
|
123
|
+
}
|
|
124
|
+
try {
|
|
125
|
+
process.kill(pid, 'SIGKILL');
|
|
126
|
+
}
|
|
127
|
+
catch {
|
|
128
|
+
// Already gone
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
function scheduleRestart() {
|
|
132
|
+
if (stopping || restartTimer)
|
|
133
|
+
return;
|
|
134
|
+
const delay = Math.min(60_000, 1000 * 2 ** failures++);
|
|
135
|
+
io.warn(`AI: restarting llama-server in ${delay / 1000}s`);
|
|
136
|
+
restartTimer = setTimeout(() => {
|
|
137
|
+
restartTimer = undefined;
|
|
138
|
+
void start();
|
|
139
|
+
}, delay);
|
|
140
|
+
}
|
|
141
|
+
function launch(command, args, env, fingerprint) {
|
|
142
|
+
const key = randomBytes(24).toString('base64url');
|
|
143
|
+
const log = openSync(join(directory(), 'llama-server.log'), 'w');
|
|
144
|
+
const started = Date.now();
|
|
145
|
+
const server = spawn(command, args, {
|
|
146
|
+
env: { ...process.env, ...env, LLAMA_API_KEY: key },
|
|
147
|
+
stdio: ['ignore', log, log],
|
|
148
|
+
});
|
|
149
|
+
closeSync(log);
|
|
150
|
+
child = server;
|
|
151
|
+
server.on('error', error => {
|
|
152
|
+
io.error(`AI: could not run llama-server: ${io.errorText(error)}`);
|
|
153
|
+
if (error.code != 'ENOENT')
|
|
154
|
+
return;
|
|
155
|
+
io.warn(`AI: not restarting llama-server until "${command}" is installed and the settings change`);
|
|
156
|
+
stopping = true;
|
|
157
|
+
});
|
|
158
|
+
server.on('exit', (code, signal) => {
|
|
159
|
+
if (child == server)
|
|
160
|
+
child = undefined;
|
|
161
|
+
if (stopping)
|
|
162
|
+
return;
|
|
163
|
+
if (Date.now() - started > 60_000)
|
|
164
|
+
failures = 0;
|
|
165
|
+
io.warn(`AI: llama-server exited (${signal ?? code})`);
|
|
166
|
+
scheduleRestart();
|
|
167
|
+
});
|
|
168
|
+
if (!server.pid)
|
|
169
|
+
return;
|
|
170
|
+
state = { pid: server.pid, fingerprint, key };
|
|
171
|
+
writeFileSync(statePath(), JSON.stringify(state), { mode: 0o600 });
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Starts llama-server, or keeps using one started by a previous Axium process with the same settings.
|
|
175
|
+
* Models are listed from the presets file, so call {@link writePreset} first.
|
|
176
|
+
*/
|
|
177
|
+
export async function start() {
|
|
178
|
+
stopping = false;
|
|
179
|
+
const { command, args, env, fingerprint } = invocation();
|
|
180
|
+
mkdirSync(directory(), { recursive: true });
|
|
181
|
+
const previous = readState();
|
|
182
|
+
const reusable = previous && isAlive(previous.pid) && isLlamaServer(previous.pid, command);
|
|
183
|
+
if (reusable && previous.fingerprint == fingerprint) {
|
|
184
|
+
io.debug(`AI: using the running llama-server (${previous.pid})`);
|
|
185
|
+
state = previous;
|
|
186
|
+
}
|
|
187
|
+
else {
|
|
188
|
+
if (reusable)
|
|
189
|
+
await terminate(previous.pid);
|
|
190
|
+
io.debug('AI: starting llama-server');
|
|
191
|
+
launch(command, args, env, fingerprint);
|
|
192
|
+
}
|
|
193
|
+
void watch();
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Uses a llama-server that another process supervises, e.g. the device daemon, without supervising it.
|
|
197
|
+
* @returns whether one is running with the current settings
|
|
198
|
+
*/
|
|
199
|
+
export async function attach() {
|
|
200
|
+
const { command, fingerprint } = invocation();
|
|
201
|
+
const previous = readState();
|
|
202
|
+
if (!previous || previous.fingerprint != fingerprint || !isAlive(previous.pid) || !isLlamaServer(previous.pid, command))
|
|
203
|
+
return false;
|
|
204
|
+
state = previous;
|
|
205
|
+
try {
|
|
206
|
+
await waitUntilHealthy(10_000);
|
|
207
|
+
}
|
|
208
|
+
catch {
|
|
209
|
+
state = undefined;
|
|
210
|
+
return false;
|
|
211
|
+
}
|
|
212
|
+
running = true;
|
|
213
|
+
return true;
|
|
214
|
+
}
|
|
215
|
+
/** Stops llama-server, e.g. when its settings change */
|
|
216
|
+
export async function stop() {
|
|
217
|
+
stopping = true;
|
|
218
|
+
clearTimeout(restartTimer);
|
|
219
|
+
restartTimer = undefined;
|
|
220
|
+
running = false;
|
|
221
|
+
if (state)
|
|
222
|
+
await terminate(state.pid);
|
|
223
|
+
state = undefined;
|
|
224
|
+
child = undefined;
|
|
225
|
+
}
|
|
226
|
+
/** Whether llama-server needs restarting to apply the current config */
|
|
227
|
+
export function settingsChanged() {
|
|
228
|
+
return state?.fingerprint != invocation().fingerprint;
|
|
229
|
+
}
|
|
230
|
+
let watching = false;
|
|
231
|
+
/** Follows llama-server's events until it stops, restarting it if it was started elsewhere and died */
|
|
232
|
+
async function watch() {
|
|
233
|
+
if (watching)
|
|
234
|
+
return;
|
|
235
|
+
watching = true;
|
|
236
|
+
try {
|
|
237
|
+
while (!stopping && state) {
|
|
238
|
+
try {
|
|
239
|
+
await waitUntilHealthy();
|
|
240
|
+
running = true;
|
|
241
|
+
events.emit('ready');
|
|
242
|
+
const response = await request('models/sse');
|
|
243
|
+
for await (const event of serverSentEvents(response))
|
|
244
|
+
events.emit('event', event);
|
|
245
|
+
}
|
|
246
|
+
catch (error) {
|
|
247
|
+
if (!stopping)
|
|
248
|
+
io.debug(`AI: lost connection to llama-server: ${io.errorText(error)}`);
|
|
249
|
+
}
|
|
250
|
+
if (running) {
|
|
251
|
+
running = false;
|
|
252
|
+
events.emit('down');
|
|
253
|
+
}
|
|
254
|
+
if (stopping || !state)
|
|
255
|
+
break;
|
|
256
|
+
if (!child && !isAlive(state.pid)) {
|
|
257
|
+
state = undefined;
|
|
258
|
+
scheduleRestart();
|
|
259
|
+
break;
|
|
260
|
+
}
|
|
261
|
+
await sleep(1000);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
finally {
|
|
265
|
+
watching = false;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
/** @param timeout In milliseconds */
|
|
269
|
+
async function waitUntilHealthy(timeout = Infinity) {
|
|
270
|
+
const deadline = Date.now() + timeout;
|
|
271
|
+
for (;;) {
|
|
272
|
+
if (stopping || !state)
|
|
273
|
+
throw new Error('Stopped');
|
|
274
|
+
if (Date.now() > deadline)
|
|
275
|
+
throw new Error('llama-server did not become healthy in time');
|
|
276
|
+
const healthy = await fetch(new URL('health', baseURL()), { signal: AbortSignal.timeout(2000) })
|
|
277
|
+
.then(response => response.ok)
|
|
278
|
+
.catch(() => false);
|
|
279
|
+
if (healthy)
|
|
280
|
+
return;
|
|
281
|
+
if (!child && !isAlive(state.pid))
|
|
282
|
+
throw new Error('llama-server is not running');
|
|
283
|
+
await sleep(500);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
/** Subscribes to router events, which are yielded as they happen until `signal` aborts */
|
|
287
|
+
export async function follow(signal) {
|
|
288
|
+
const response = await request('models/sse', { signal });
|
|
289
|
+
return serverSentEvents(response);
|
|
290
|
+
}
|
|
291
|
+
export async function* serverSentEvents(response) {
|
|
292
|
+
let buffer = '';
|
|
293
|
+
for await (const chunk of response.body.pipeThrough(new TextDecoderStream())) {
|
|
294
|
+
buffer += chunk;
|
|
295
|
+
const lines = buffer.split('\n');
|
|
296
|
+
buffer = lines.pop();
|
|
297
|
+
for (const line of lines) {
|
|
298
|
+
if (!line.startsWith('data:'))
|
|
299
|
+
continue;
|
|
300
|
+
const data = line.slice(5).trim();
|
|
301
|
+
if (data == '[DONE]')
|
|
302
|
+
return;
|
|
303
|
+
yield JSON.parse(data);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
/** Sends a request to llama-server, returning its response whether or not it succeeded */
|
|
308
|
+
export async function send(path, init = {}) {
|
|
309
|
+
if (!state)
|
|
310
|
+
throw new Error('llama-server is not running');
|
|
311
|
+
const headers = new Headers(init.headers);
|
|
312
|
+
headers.set('Authorization', 'Bearer ' + state.key);
|
|
313
|
+
if (typeof init.body == 'string')
|
|
314
|
+
headers.set('Content-Type', 'application/json');
|
|
315
|
+
return await fetch(new URL(path, baseURL()), { ...init, headers });
|
|
316
|
+
}
|
|
317
|
+
/** Sends a request to llama-server, throwing its error message on failure */
|
|
318
|
+
export async function request(path, init = {}) {
|
|
319
|
+
const response = await send(path, init);
|
|
320
|
+
if (response.ok)
|
|
321
|
+
return response;
|
|
322
|
+
const body = await response.text();
|
|
323
|
+
let message = body;
|
|
324
|
+
try {
|
|
325
|
+
message = JSON.parse(body).error?.message ?? body;
|
|
326
|
+
}
|
|
327
|
+
catch {
|
|
328
|
+
// Not JSON
|
|
329
|
+
}
|
|
330
|
+
throw new Error(`llama-server: ${message || response.statusText}`);
|
|
331
|
+
}
|
|
332
|
+
async function post(path, body) {
|
|
333
|
+
await request(path, { method: 'POST', body: JSON.stringify(body) });
|
|
334
|
+
}
|
|
335
|
+
export async function listModels(reload = false) {
|
|
336
|
+
const response = await request('models' + (reload ? '?reload=1' : ''), { signal: AbortSignal.timeout(10_000) });
|
|
337
|
+
const { data } = (await response.json());
|
|
338
|
+
return data;
|
|
339
|
+
}
|
|
340
|
+
/** Makes llama-server read the presets file again */
|
|
341
|
+
export async function reload() {
|
|
342
|
+
if (running)
|
|
343
|
+
await listModels(true);
|
|
344
|
+
}
|
|
345
|
+
export const load = (model) => post('models/load', { model });
|
|
346
|
+
export const unload = (model) => post('models/unload', { model });
|
|
347
|
+
/** Starts downloading `<repository>:<quantization>` into the cache, or resumes a paused download */
|
|
348
|
+
export const download = (source) => post('models', { model: source });
|
|
349
|
+
/** Deletes a cached model's files, or a paused download's partial files */
|
|
350
|
+
export async function remove(source) {
|
|
351
|
+
await request('models?model=' + encodeURIComponent(source), { method: 'DELETE' });
|
|
352
|
+
}
|
|
353
|
+
/**
|
|
354
|
+
* What a loaded model's chat template supports.
|
|
355
|
+
* Only available while the model is loaded; this never loads it.
|
|
356
|
+
*/
|
|
357
|
+
export async function getTemplateCaps(model, signal) {
|
|
358
|
+
const response = await request(`props?model=${encodeURIComponent(model)}&autoload=false`, { signal }).catch(() => null);
|
|
359
|
+
if (!response)
|
|
360
|
+
return;
|
|
361
|
+
const props = (await response.json());
|
|
362
|
+
return props.chat_template_caps ?? undefined;
|
|
363
|
+
}
|
|
364
|
+
/** The prompt a loaded model would see, without running it */
|
|
365
|
+
async function renderPrompt(model, reasoning, signal) {
|
|
366
|
+
const response = await request('apply-template?autoload=false', {
|
|
367
|
+
method: 'POST',
|
|
368
|
+
signal,
|
|
369
|
+
body: JSON.stringify({ model, messages: [{ role: 'user', content: 'Hello' }], reasoning_effort: reasoning }),
|
|
370
|
+
}).catch(() => null);
|
|
371
|
+
if (!response)
|
|
372
|
+
return;
|
|
373
|
+
const { prompt } = (await response.json());
|
|
374
|
+
return prompt;
|
|
375
|
+
}
|
|
376
|
+
/** Which of `candidates` a loaded model's chat template uses when no reasoning effort is given, found by comparing rendered prompts */
|
|
377
|
+
export async function getTemplateReasoning(model, candidates, signal) {
|
|
378
|
+
const base = await renderPrompt(model, undefined, signal);
|
|
379
|
+
if (base === undefined)
|
|
380
|
+
return;
|
|
381
|
+
for (const candidate of candidates) {
|
|
382
|
+
if ((await renderPrompt(model, candidate, signal)) === base)
|
|
383
|
+
return candidate;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
import { isTag, isText } from 'domhandler';
|
|
2
|
+
import { DomUtils, parseDocument } from 'htmlparser2';
|
|
3
|
+
/** Elements whose content is never part of a page's text */
|
|
4
|
+
const ignored = new Set([
|
|
5
|
+
'script',
|
|
6
|
+
'style',
|
|
7
|
+
'noscript',
|
|
8
|
+
'template',
|
|
9
|
+
'svg',
|
|
10
|
+
'canvas',
|
|
11
|
+
'iframe',
|
|
12
|
+
'object',
|
|
13
|
+
'embed',
|
|
14
|
+
'head',
|
|
15
|
+
'nav',
|
|
16
|
+
'aside',
|
|
17
|
+
'footer',
|
|
18
|
+
'form',
|
|
19
|
+
'button',
|
|
20
|
+
'input',
|
|
21
|
+
'select',
|
|
22
|
+
'textarea',
|
|
23
|
+
'dialog',
|
|
24
|
+
]);
|
|
25
|
+
/** Roles of landmarks that surround a page's content */
|
|
26
|
+
const ignoredRoles = new Set(['navigation', 'banner', 'contentinfo', 'complementary', 'search']);
|
|
27
|
+
const blockTags = new Set([
|
|
28
|
+
'address',
|
|
29
|
+
'article',
|
|
30
|
+
'blockquote',
|
|
31
|
+
'caption',
|
|
32
|
+
'dd',
|
|
33
|
+
'details',
|
|
34
|
+
'div',
|
|
35
|
+
'dl',
|
|
36
|
+
'dt',
|
|
37
|
+
'fieldset',
|
|
38
|
+
'figcaption',
|
|
39
|
+
'figure',
|
|
40
|
+
'h1',
|
|
41
|
+
'h2',
|
|
42
|
+
'h3',
|
|
43
|
+
'h4',
|
|
44
|
+
'h5',
|
|
45
|
+
'h6',
|
|
46
|
+
'header',
|
|
47
|
+
'hr',
|
|
48
|
+
'li',
|
|
49
|
+
'main',
|
|
50
|
+
'ol',
|
|
51
|
+
'p',
|
|
52
|
+
'pre',
|
|
53
|
+
'section',
|
|
54
|
+
'summary',
|
|
55
|
+
'table',
|
|
56
|
+
'tbody',
|
|
57
|
+
'td',
|
|
58
|
+
'tfoot',
|
|
59
|
+
'th',
|
|
60
|
+
'thead',
|
|
61
|
+
'tr',
|
|
62
|
+
'ul',
|
|
63
|
+
]);
|
|
64
|
+
function isIgnored(node) {
|
|
65
|
+
if (isText(node))
|
|
66
|
+
return false;
|
|
67
|
+
if (!isTag(node))
|
|
68
|
+
return true;
|
|
69
|
+
const { attribs } = node;
|
|
70
|
+
return (ignored.has(node.name) ||
|
|
71
|
+
ignoredRoles.has(attribs.role) ||
|
|
72
|
+
'hidden' in attribs ||
|
|
73
|
+
attribs['aria-hidden'] == 'true' ||
|
|
74
|
+
/(^|\s)(sr-only|visually-hidden)(\s|$)/.test(attribs.class ?? ''));
|
|
75
|
+
}
|
|
76
|
+
class Converter {
|
|
77
|
+
base;
|
|
78
|
+
constructor(base) {
|
|
79
|
+
this.base = base;
|
|
80
|
+
}
|
|
81
|
+
link(element) {
|
|
82
|
+
const label = this.inline(element.children).trim();
|
|
83
|
+
const href = element.attribs.href;
|
|
84
|
+
if (href?.startsWith('#') && /^[#¶§🔗]?$/u.test(label))
|
|
85
|
+
return '';
|
|
86
|
+
if (!href || href.startsWith('#'))
|
|
87
|
+
return label;
|
|
88
|
+
try {
|
|
89
|
+
const target = new URL(href, this.base);
|
|
90
|
+
if (target.protocol != 'http:' && target.protocol != 'https:')
|
|
91
|
+
return label;
|
|
92
|
+
return label ? `[${label}](${target.href})` : '';
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
return label;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
/** Wraps the element's content in `mark`, keeping surrounding whitespace outside it */
|
|
99
|
+
wrap(element, mark) {
|
|
100
|
+
const content = this.inline(element.children);
|
|
101
|
+
const trimmed = content.trim();
|
|
102
|
+
if (!trimmed)
|
|
103
|
+
return content;
|
|
104
|
+
return (/^\s/.test(content) ? ' ' : '') + mark + trimmed + mark + (/\s$/.test(content) ? ' ' : '');
|
|
105
|
+
}
|
|
106
|
+
inline(nodes) {
|
|
107
|
+
let text = '';
|
|
108
|
+
for (const node of nodes) {
|
|
109
|
+
if (isIgnored(node))
|
|
110
|
+
continue;
|
|
111
|
+
if (isText(node)) {
|
|
112
|
+
text += node.data.replace(/\s+/g, ' ');
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
const element = node;
|
|
116
|
+
switch (element.name) {
|
|
117
|
+
case 'br':
|
|
118
|
+
text += '\n';
|
|
119
|
+
break;
|
|
120
|
+
case 'a': {
|
|
121
|
+
const link = this.link(element);
|
|
122
|
+
if (link.startsWith('[') && /\)$/.test(text))
|
|
123
|
+
text += ' ';
|
|
124
|
+
text += link;
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
case 'strong':
|
|
128
|
+
case 'b':
|
|
129
|
+
text += this.wrap(element, '**');
|
|
130
|
+
break;
|
|
131
|
+
case 'em':
|
|
132
|
+
case 'i':
|
|
133
|
+
text += this.wrap(element, '*');
|
|
134
|
+
break;
|
|
135
|
+
case 'del':
|
|
136
|
+
case 's':
|
|
137
|
+
text += this.wrap(element, '~~');
|
|
138
|
+
break;
|
|
139
|
+
case 'code':
|
|
140
|
+
case 'kbd':
|
|
141
|
+
case 'samp':
|
|
142
|
+
text += '`' + DomUtils.textContent(element).replace(/\s+/g, ' ') + '`';
|
|
143
|
+
break;
|
|
144
|
+
case 'img':
|
|
145
|
+
if (element.attribs.alt?.trim())
|
|
146
|
+
text += `[image: ${element.attribs.alt.trim()}]`;
|
|
147
|
+
break;
|
|
148
|
+
default:
|
|
149
|
+
text += blockTags.has(element.name) ? ' ' + this.blocks([element]).join(' ') + ' ' : this.inline(element.children);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return text;
|
|
153
|
+
}
|
|
154
|
+
/** Markdown blocks, to be separated by blank lines */
|
|
155
|
+
blocks(nodes) {
|
|
156
|
+
const blocks = [];
|
|
157
|
+
let run = [];
|
|
158
|
+
const flush = () => {
|
|
159
|
+
const text = this.inline(run)
|
|
160
|
+
.split('\n')
|
|
161
|
+
.map(line => line.trim())
|
|
162
|
+
.join('\n')
|
|
163
|
+
.trim();
|
|
164
|
+
if (text)
|
|
165
|
+
blocks.push(text);
|
|
166
|
+
run = [];
|
|
167
|
+
};
|
|
168
|
+
for (const node of nodes) {
|
|
169
|
+
if (isIgnored(node))
|
|
170
|
+
continue;
|
|
171
|
+
if (!isTag(node) || !blockTags.has(node.name)) {
|
|
172
|
+
run.push(node);
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
flush();
|
|
176
|
+
blocks.push(...this.block(node));
|
|
177
|
+
}
|
|
178
|
+
flush();
|
|
179
|
+
return blocks;
|
|
180
|
+
}
|
|
181
|
+
block(element) {
|
|
182
|
+
switch (element.name) {
|
|
183
|
+
case 'h1':
|
|
184
|
+
case 'h2':
|
|
185
|
+
case 'h3':
|
|
186
|
+
case 'h4':
|
|
187
|
+
case 'h5':
|
|
188
|
+
case 'h6': {
|
|
189
|
+
const text = this.inline(element.children).replace(/\s+/g, ' ').trim();
|
|
190
|
+
return text ? ['#'.repeat(Number(element.name[1])) + ' ' + text] : [];
|
|
191
|
+
}
|
|
192
|
+
case 'pre':
|
|
193
|
+
return [this.code(element)];
|
|
194
|
+
case 'hr':
|
|
195
|
+
return ['---'];
|
|
196
|
+
case 'blockquote':
|
|
197
|
+
return this.blocks(element.children).map(block => block.replace(/^/gm, '> '));
|
|
198
|
+
case 'ul':
|
|
199
|
+
case 'ol':
|
|
200
|
+
return [this.list(element)];
|
|
201
|
+
case 'table':
|
|
202
|
+
return [this.table(element)];
|
|
203
|
+
case 'dt': {
|
|
204
|
+
const text = this.inline(element.children).trim();
|
|
205
|
+
return text ? [`**${text}**`] : [];
|
|
206
|
+
}
|
|
207
|
+
default:
|
|
208
|
+
return this.blocks(element.children);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
/** Preformatted text, with block elements (e.g. one per line) on their own lines */
|
|
212
|
+
preformatted(nodes) {
|
|
213
|
+
let text = '';
|
|
214
|
+
for (const node of nodes) {
|
|
215
|
+
if (isIgnored(node))
|
|
216
|
+
continue;
|
|
217
|
+
if (isText(node)) {
|
|
218
|
+
text += node.data;
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
221
|
+
const element = node;
|
|
222
|
+
if (element.name == 'br') {
|
|
223
|
+
text += '\n';
|
|
224
|
+
continue;
|
|
225
|
+
}
|
|
226
|
+
if (blockTags.has(element.name) && text && !text.endsWith('\n'))
|
|
227
|
+
text += '\n';
|
|
228
|
+
text += this.preformatted(element.children);
|
|
229
|
+
}
|
|
230
|
+
return text;
|
|
231
|
+
}
|
|
232
|
+
code(element) {
|
|
233
|
+
const code = this.preformatted(element.children).replace(/\n+$/, '');
|
|
234
|
+
const codeElement = DomUtils.findOne(node => node.name == 'code', element.children);
|
|
235
|
+
const classes = `${element.attribs.class ?? ''} ${codeElement?.attribs.class ?? ''}`;
|
|
236
|
+
const language = /(?:^|\s)(?:language|lang)-([\w+-]+)/.exec(classes)?.[1] ?? element.attribs['data-language'] ?? '';
|
|
237
|
+
const longest = Math.max(0, ...Array.from(code.matchAll(/`+/g), match => match[0].length));
|
|
238
|
+
const fence = '`'.repeat(Math.max(3, longest + 1));
|
|
239
|
+
return `${fence}${language}\n${code}\n${fence}`;
|
|
240
|
+
}
|
|
241
|
+
list(element) {
|
|
242
|
+
const ordered = element.name == 'ol';
|
|
243
|
+
let number = Number(element.attribs.start) || 1;
|
|
244
|
+
const items = [];
|
|
245
|
+
for (const item of element.children) {
|
|
246
|
+
if (!isTag(item) || item.name != 'li' || isIgnored(item))
|
|
247
|
+
continue;
|
|
248
|
+
const marker = ordered ? `${number++}. ` : '- ';
|
|
249
|
+
const text = this.blocks(item.children).join('\n');
|
|
250
|
+
if (!text)
|
|
251
|
+
continue;
|
|
252
|
+
items.push(marker + text.replace(/\n/g, '\n' + ' '.repeat(marker.length)));
|
|
253
|
+
}
|
|
254
|
+
return items.join('\n');
|
|
255
|
+
}
|
|
256
|
+
table(element) {
|
|
257
|
+
const rows = DomUtils.findAll(node => node.name == 'tr', element.children).map(row => row.children
|
|
258
|
+
.filter((cell) => isTag(cell) && (cell.name == 'th' || cell.name == 'td'))
|
|
259
|
+
.map(cell => this.blocks(cell.children)
|
|
260
|
+
.join(' ')
|
|
261
|
+
.replace(/\s*\n\s*/g, ' ')
|
|
262
|
+
.replace(/\|/g, '\\|')));
|
|
263
|
+
const width = Math.max(0, ...rows.map(row => row.length));
|
|
264
|
+
if (!width)
|
|
265
|
+
return '';
|
|
266
|
+
const line = (cells) => '| ' + Array.from({ length: width }, (_, i) => cells[i] ?? '').join(' | ') + ' |';
|
|
267
|
+
const [head, ...body] = rows;
|
|
268
|
+
return [line(head), line(Array(width).fill('---')), ...body.map(line)].join('\n');
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
/** The main content of an HTML page as Markdown, without navigation and other surrounding parts */
|
|
272
|
+
export function htmlToMarkdown(html, base) {
|
|
273
|
+
const document = parseDocument(html);
|
|
274
|
+
const title = DomUtils.findOne(node => node.name == 'title', document.children);
|
|
275
|
+
const articles = DomUtils.findAll(node => node.name == 'article', document.children);
|
|
276
|
+
const root = DomUtils.findOne(node => node.name == 'main' || node.attribs.role == 'main', document.children) ??
|
|
277
|
+
(articles.length == 1 ? articles[0] : undefined) ??
|
|
278
|
+
DomUtils.findOne(node => node.name == 'body', document.children) ??
|
|
279
|
+
document;
|
|
280
|
+
const converter = new Converter(base);
|
|
281
|
+
return {
|
|
282
|
+
title: title ? DomUtils.textContent(title).replace(/\s+/g, ' ').trim() : undefined,
|
|
283
|
+
markdown: converter.blocks(root.children).join('\n\n'),
|
|
284
|
+
};
|
|
285
|
+
}
|