@atomic-chat/core 0.19.0 → 0.20.5
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/dist/app-daemon.js +5 -0
- package/dist/app-daemon.js.map +1 -1
- package/dist/cli/bin.js +0 -0
- package/dist/cli/launch.js +2 -0
- package/dist/cli/launch.js.map +1 -1
- package/dist/cli/main.d.ts +1 -1
- package/dist/cli/main.js +5 -0
- package/dist/cli/main.js.map +1 -1
- package/dist/cloud/registry.js +1 -0
- package/dist/cloud/registry.js.map +1 -1
- package/dist/config/paths.d.ts +1 -1
- package/dist/config/paths.js +12 -0
- package/dist/config/paths.js.map +1 -1
- package/dist/contracts/engine-builds.d.ts +6 -1
- package/dist/contracts/engine-builds.js +1 -1
- package/dist/contracts/engine-builds.js.map +1 -1
- package/dist/contracts/engines.d.ts +1 -1
- package/dist/contracts/engines.js +2 -0
- package/dist/contracts/engines.js.map +1 -1
- package/dist/contracts/errors.d.ts +7 -1
- package/dist/contracts/errors.js.map +1 -1
- package/dist/contracts/events.d.ts +3 -1
- package/dist/contracts/geniex.d.ts +98 -0
- package/dist/contracts/geniex.js +15 -0
- package/dist/contracts/geniex.js.map +1 -0
- package/dist/contracts/index.d.ts +1 -0
- package/dist/contracts/index.js +1 -0
- package/dist/contracts/index.js.map +1 -1
- package/dist/contracts/model-yml.d.ts +40 -0
- package/dist/contracts/model-yml.js +7 -0
- package/dist/contracts/model-yml.js.map +1 -1
- package/dist/contracts/session.d.ts +6 -1
- package/dist/contracts/session.js.map +1 -1
- package/dist/contracts/settings.d.ts +12 -1
- package/dist/contracts/settings.js.map +1 -1
- package/dist/core/app-lease.d.ts +56 -0
- package/dist/core/app-lease.js +50 -0
- package/dist/core/app-lease.js.map +1 -0
- package/dist/core/atomic-core.d.ts +1 -1
- package/dist/core/create.js +109 -5
- package/dist/core/create.js.map +1 -1
- package/dist/engine-builds/engines.d.ts +28 -7
- package/dist/engine-builds/engines.js +49 -5
- package/dist/engine-builds/engines.js.map +1 -1
- package/dist/engine-builds/geniex.d.ts +35 -0
- package/dist/engine-builds/geniex.js +111 -0
- package/dist/engine-builds/geniex.js.map +1 -0
- package/dist/engine-builds/manifest.d.ts +43 -0
- package/dist/engine-builds/manifest.js +92 -0
- package/dist/engine-builds/manifest.js.map +1 -1
- package/dist/engine-builds/order.d.ts +2 -1
- package/dist/engine-builds/order.js +2 -0
- package/dist/engine-builds/order.js.map +1 -1
- package/dist/engine-builds/select.d.ts +15 -0
- package/dist/engine-builds/select.js +14 -0
- package/dist/engine-builds/select.js.map +1 -1
- package/dist/engine-builds/service.d.ts +5 -3
- package/dist/engine-builds/service.js +21 -2
- package/dist/engine-builds/service.js.map +1 -1
- package/dist/engines/service.d.ts +2 -1
- package/dist/engines/service.js +3 -1
- package/dist/engines/service.js.map +1 -1
- package/dist/engines/versions.js +3 -0
- package/dist/engines/versions.js.map +1 -1
- package/dist/integrations/catalog.d.ts +3 -1
- package/dist/integrations/catalog.js +8 -2
- package/dist/integrations/catalog.js.map +1 -1
- package/dist/integrations/config-io.d.ts +45 -4
- package/dist/integrations/config-io.js +91 -6
- package/dist/integrations/config-io.js.map +1 -1
- package/dist/integrations/configure/codex-repair.d.ts +75 -0
- package/dist/integrations/configure/codex-repair.js +254 -0
- package/dist/integrations/configure/codex-repair.js.map +1 -0
- package/dist/integrations/configure/codex.d.ts +48 -10
- package/dist/integrations/configure/codex.js +126 -32
- package/dist/integrations/configure/codex.js.map +1 -1
- package/dist/integrations/configure/registry.d.ts +4 -1
- package/dist/integrations/configure/registry.js +1 -0
- package/dist/integrations/configure/registry.js.map +1 -1
- package/dist/models/gguf/kv-cache.d.ts +5 -0
- package/dist/models/gguf/kv-cache.js +35 -2
- package/dist/models/gguf/kv-cache.js.map +1 -1
- package/dist/models/gguf/reader.d.ts +5 -0
- package/dist/models/gguf/reader.js +15 -3
- package/dist/models/gguf/reader.js.map +1 -1
- package/dist/router/resolve.d.ts +3 -2
- package/dist/router/resolve.js +3 -1
- package/dist/router/resolve.js.map +1 -1
- package/dist/runtime/geniex/accelerators.d.ts +20 -0
- package/dist/runtime/geniex/accelerators.js +53 -0
- package/dist/runtime/geniex/accelerators.js.map +1 -0
- package/dist/runtime/geniex/check.d.ts +34 -0
- package/dist/runtime/geniex/check.js +117 -0
- package/dist/runtime/geniex/check.js.map +1 -0
- package/dist/runtime/geniex/chip.d.ts +29 -0
- package/dist/runtime/geniex/chip.js +35 -0
- package/dist/runtime/geniex/chip.js.map +1 -0
- package/dist/runtime/geniex/devices.d.ts +13 -0
- package/dist/runtime/geniex/devices.js +32 -0
- package/dist/runtime/geniex/devices.js.map +1 -0
- package/dist/runtime/geniex/geniex-host.d.ts +17 -0
- package/dist/runtime/geniex/geniex-host.js +26 -0
- package/dist/runtime/geniex/geniex-host.js.map +1 -0
- package/dist/runtime/geniex/host/args.d.ts +56 -0
- package/dist/runtime/geniex/host/args.js +115 -0
- package/dist/runtime/geniex/host/args.js.map +1 -0
- package/dist/runtime/geniex/host/engine.d.ts +76 -0
- package/dist/runtime/geniex/host/engine.js +122 -0
- package/dist/runtime/geniex/host/engine.js.map +1 -0
- package/dist/runtime/geniex/host/ffi-bun.d.ts +15 -0
- package/dist/runtime/geniex/host/ffi-bun.js +415 -0
- package/dist/runtime/geniex/host/ffi-bun.js.map +1 -0
- package/dist/runtime/geniex/host/grammar.d.ts +11 -0
- package/dist/runtime/geniex/host/grammar.js +223 -0
- package/dist/runtime/geniex/host/grammar.js.map +1 -0
- package/dist/runtime/geniex/host/inspect.d.ts +25 -0
- package/dist/runtime/geniex/host/inspect.js +177 -0
- package/dist/runtime/geniex/host/inspect.js.map +1 -0
- package/dist/runtime/geniex/host/library.d.ts +117 -0
- package/dist/runtime/geniex/host/library.js +38 -0
- package/dist/runtime/geniex/host/library.js.map +1 -0
- package/dist/runtime/geniex/host/openai.d.ts +62 -0
- package/dist/runtime/geniex/host/openai.js +458 -0
- package/dist/runtime/geniex/host/openai.js.map +1 -0
- package/dist/runtime/geniex/host/run.d.ts +48 -0
- package/dist/runtime/geniex/host/run.js +208 -0
- package/dist/runtime/geniex/host/run.js.map +1 -0
- package/dist/runtime/geniex/host/server.d.ts +39 -0
- package/dist/runtime/geniex/host/server.js +232 -0
- package/dist/runtime/geniex/host/server.js.map +1 -0
- package/dist/runtime/geniex/host/worker-protocol.d.ts +76 -0
- package/dist/runtime/geniex/host/worker-protocol.js +116 -0
- package/dist/runtime/geniex/host/worker-protocol.js.map +1 -0
- package/dist/runtime/geniex/host/worker-source.generated.d.ts +1 -0
- package/dist/runtime/geniex/host/worker-source.generated.js +4 -0
- package/dist/runtime/geniex/host/worker-source.generated.js.map +1 -0
- package/dist/runtime/geniex/host/worker.d.ts +7 -0
- package/dist/runtime/geniex/host/worker.js +19 -0
- package/dist/runtime/geniex/host/worker.js.map +1 -0
- package/dist/runtime/geniex/host-process.d.ts +48 -0
- package/dist/runtime/geniex/host-process.js +104 -0
- package/dist/runtime/geniex/host-process.js.map +1 -0
- package/dist/runtime/geniex/index.d.ts +11 -0
- package/dist/runtime/geniex/index.js +12 -0
- package/dist/runtime/geniex/index.js.map +1 -0
- package/dist/runtime/geniex/model-yml.d.ts +8 -0
- package/dist/runtime/geniex/model-yml.js +98 -0
- package/dist/runtime/geniex/model-yml.js.map +1 -0
- package/dist/runtime/geniex/runtime.d.ts +94 -0
- package/dist/runtime/geniex/runtime.js +366 -0
- package/dist/runtime/geniex/runtime.js.map +1 -0
- package/dist/runtime/litert-lm/host/openai.d.ts +4 -37
- package/dist/runtime/litert-lm/host/openai.js +11 -96
- package/dist/runtime/litert-lm/host/openai.js.map +1 -1
- package/dist/runtime/litert-lm/host/pool.d.ts +1 -7
- package/dist/runtime/litert-lm/host/pool.js +1 -17
- package/dist/runtime/litert-lm/host/pool.js.map +1 -1
- package/dist/runtime/litert-lm/host/server.js +11 -53
- package/dist/runtime/litert-lm/host/server.js.map +1 -1
- package/dist/runtime/llamacpp/args.d.ts +5 -0
- package/dist/runtime/llamacpp/args.js +2 -0
- package/dist/runtime/llamacpp/args.js.map +1 -1
- package/dist/runtime/llamacpp/fit-margin.d.ts +44 -0
- package/dist/runtime/llamacpp/fit-margin.js +50 -0
- package/dist/runtime/llamacpp/fit-margin.js.map +1 -1
- package/dist/runtime/llamacpp/load-plan.d.ts +5 -0
- package/dist/runtime/llamacpp/load-plan.js +57 -22
- package/dist/runtime/llamacpp/load-plan.js.map +1 -1
- package/dist/runtime/llamacpp/runtime.d.ts +5 -1
- package/dist/runtime/llamacpp/runtime.js +6 -1
- package/dist/runtime/llamacpp/runtime.js.map +1 -1
- package/dist/runtime/shared/external-sessions.js +1 -0
- package/dist/runtime/shared/external-sessions.js.map +1 -1
- package/dist/runtime/shared/fifo-lock.d.ts +10 -0
- package/dist/runtime/shared/fifo-lock.js +21 -0
- package/dist/runtime/shared/fifo-lock.js.map +1 -0
- package/dist/runtime/shared/index.d.ts +2 -0
- package/dist/runtime/shared/index.js +2 -0
- package/dist/runtime/shared/index.js.map +1 -1
- package/dist/runtime/shared/openai-sse.d.ts +112 -0
- package/dist/runtime/shared/openai-sse.js +178 -0
- package/dist/runtime/shared/openai-sse.js.map +1 -0
- package/dist/server/clients.d.ts +6 -0
- package/dist/server/clients.js +11 -0
- package/dist/server/clients.js.map +1 -1
- package/dist/server/control/router.js +2 -0
- package/dist/server/control/router.js.map +1 -1
- package/dist/server/control/routes/geniex.d.ts +7 -0
- package/dist/server/control/routes/geniex.js +22 -0
- package/dist/server/control/routes/geniex.js.map +1 -0
- package/dist/server/control/types.d.ts +11 -1
- package/dist/server/control/types.js.map +1 -1
- package/dist/server/http.js +2 -0
- package/dist/server/http.js.map +1 -1
- package/dist/server/public/embedding.d.ts +8 -7
- package/dist/server/public/embedding.js +15 -7
- package/dist/server/public/embedding.js.map +1 -1
- package/dist/server/public/forward.js +23 -6
- package/dist/server/public/forward.js.map +1 -1
- package/dist/server/public/listing.js +1 -0
- package/dist/server/public/listing.js.map +1 -1
- package/dist/server/public/responses.js +9 -0
- package/dist/server/public/responses.js.map +1 -1
- package/dist/server/public/telemetry.d.ts +7 -1
- package/dist/server/public/telemetry.js +11 -3
- package/dist/server/public/telemetry.js.map +1 -1
- package/dist/server/public/trace.d.ts +8 -4
- package/dist/server/public/trace.js +15 -7
- package/dist/server/public/trace.js.map +1 -1
- package/dist/server/public/validate.d.ts +32 -0
- package/dist/server/public/validate.js +146 -0
- package/dist/server/public/validate.js.map +1 -0
- package/dist/settings/schema/geniex.json +63 -0
- package/dist/settings/schema/mlx.json +1 -1
- package/dist/settings/schema.js +11 -0
- package/dist/settings/schema.js.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +7 -2
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `geniex` provider's sessions (openspec change `add-geniex-runtime`, task 6.3, design D4–D7):
|
|
3
|
+
* one `geniex-host serve` process per model — this very binary in its hidden mode — on the sidecar
|
|
4
|
+
* table Foundry Local and LiteRT-LM use, behind the session gateway the managed engines use.
|
|
5
|
+
*
|
|
6
|
+
* load = model.yml → a bundle is for this chip → the device (settings, else model.yml; a bundle on
|
|
7
|
+
* the NPU only) → the active build → the device is one the build's probe found → the model's
|
|
8
|
+
* facts (`inspect`, cached by path and mtime): a bundle for a newer GenieX? context cut to
|
|
9
|
+
* the model → [GPU claim, for gpu] → host on a random loopback port → readiness line →
|
|
10
|
+
* gateway (key + Host) → session on the gateway's port
|
|
11
|
+
*
|
|
12
|
+
* The host's server has no authentication; the gateway holds the key and lets through only
|
|
13
|
+
* `/v1/chat/completions` and `/v1/models`. The host serves the model under core's id (`--id`).
|
|
14
|
+
*/
|
|
15
|
+
import { stat } from 'node:fs/promises';
|
|
16
|
+
import { dirname, join } from 'node:path';
|
|
17
|
+
import { AtomicCoreError, GENIEX_DEVICES } from '../../contracts/index.js';
|
|
18
|
+
import { generateGatewayKey, startManagedGateway } from '../managed-text/index.js';
|
|
19
|
+
import { backendOutputReporter, exitedOnTakenPort, randomFreePort, retryOnTakenPort, SidecarTable, spawnAndAwaitReady, throwIfLoadCancelled, } from '../shared/index.js';
|
|
20
|
+
import { compareVersions } from '../../backend/index.js';
|
|
21
|
+
import { deviceFound, deviceMissingReason } from './devices.js';
|
|
22
|
+
import { GENIEX_POWER_MODES } from './host/library.js';
|
|
23
|
+
import { runGeniexHostInspect } from './host-process.js';
|
|
24
|
+
import { readGeniexBlock } from './model-yml.js';
|
|
25
|
+
/** The settings' `timeout` default, in seconds: opening the file, compiling for the NPU, the server. */
|
|
26
|
+
export const GENIEX_DEFAULT_TIMEOUT_SECS = 600;
|
|
27
|
+
/** What a GenieX session serves (design D6); anything else is refused before it reaches the host. */
|
|
28
|
+
export const GENIEX_ROUTES = [
|
|
29
|
+
{ method: 'POST', path: '/v1/chat/completions' },
|
|
30
|
+
{ method: 'GET', path: '/v1/models' },
|
|
31
|
+
];
|
|
32
|
+
/** `device` of the settings when it names one, else `model.yml`'s default. */
|
|
33
|
+
export function launchDevice(settingDevice, block) {
|
|
34
|
+
return GENIEX_DEVICES.some((known) => known === settingDevice)
|
|
35
|
+
? settingDevice
|
|
36
|
+
: block.backend;
|
|
37
|
+
}
|
|
38
|
+
/** `ctx_len` of the settings (0: the model's own), else `model.yml`'s, cut to the model's maximum. */
|
|
39
|
+
export function engineContext(settingCtx, block, modelMax) {
|
|
40
|
+
const asked = Number(settingCtx) > 0 ? Math.round(Number(settingCtx)) : block.context_length;
|
|
41
|
+
return modelMax !== null ? Math.min(asked, modelMax) : asked;
|
|
42
|
+
}
|
|
43
|
+
const powerMode = (value) => GENIEX_POWER_MODES.some((known) => known === value) ? value : 'burst';
|
|
44
|
+
/** The host's exit codes (`host/run.ts` `EXIT`) as the core's errors. */
|
|
45
|
+
function hostError(exitCode, stderr) {
|
|
46
|
+
const lines = stderr.trim().split('\n');
|
|
47
|
+
// A bind failure sits above Node's stack, outside the last five lines: kept first, the retry reads it.
|
|
48
|
+
const taken = lines.find((line) => exitedOnTakenPort(line));
|
|
49
|
+
const said = [...(taken ? [taken] : []), ...lines.slice(-5).filter((line) => line !== taken)].join('\n');
|
|
50
|
+
const last = stderr.trim().split('\n').at(-1) ?? '';
|
|
51
|
+
let line = {};
|
|
52
|
+
try {
|
|
53
|
+
line = JSON.parse(last);
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
// Not one of the host's JSON error lines.
|
|
57
|
+
}
|
|
58
|
+
if (exitCode === 6)
|
|
59
|
+
return new AtomicCoreError('GENIEX_MODEL_REQUIRES_NEWER_ENGINE', `This model needs GenieX ${String(line['required'] ?? 'newer')}; update GenieX first.`, said);
|
|
60
|
+
if (exitCode === 7)
|
|
61
|
+
return new AtomicCoreError('GENIEX_VARIANT_UNSUPPORTED', typeof line['message'] === 'string' ? line['message'] : 'This model does not run on its device here.', said);
|
|
62
|
+
return new AtomicCoreError('GENIEX_HOST_ERROR', exitCode === 3
|
|
63
|
+
? 'The GenieX library does not load on this computer.'
|
|
64
|
+
: 'The GenieX host stopped before the model was ready.', said || `exit code ${String(exitCode)}`);
|
|
65
|
+
}
|
|
66
|
+
export class GeniexRuntime {
|
|
67
|
+
options;
|
|
68
|
+
table;
|
|
69
|
+
emit;
|
|
70
|
+
/** Loads past their GPU claim and not yet a session. */
|
|
71
|
+
claimed = new Map();
|
|
72
|
+
/** `inspect` answers by `<path>|<mtime>`: a model that changed is read again. */
|
|
73
|
+
facts = new Map();
|
|
74
|
+
get registry() {
|
|
75
|
+
return this.options.registry;
|
|
76
|
+
}
|
|
77
|
+
constructor(options) {
|
|
78
|
+
this.options = options;
|
|
79
|
+
this.emit = options.emit ?? (() => { });
|
|
80
|
+
this.table = new SidecarTable({
|
|
81
|
+
provider: 'geniex',
|
|
82
|
+
instanceId: options.instanceId,
|
|
83
|
+
journal: options.journal,
|
|
84
|
+
emit: this.emit,
|
|
85
|
+
engine: 'GenieX',
|
|
86
|
+
unloadGraceMs: 15_000,
|
|
87
|
+
shutdownGraceMs: 5_000,
|
|
88
|
+
describeExit: (exit, stderr) => `The GenieX host exited (${exit.code !== null ? `code ${exit.code}` : `signal ${String(exit.signal)}`})${stderr.trim() ? `: ${stderr.trim().split('\n').slice(-3).join(' ')}` : ''}`,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
list() {
|
|
92
|
+
return this.table.list();
|
|
93
|
+
}
|
|
94
|
+
findSession(modelId) {
|
|
95
|
+
return this.table.findSession(modelId);
|
|
96
|
+
}
|
|
97
|
+
getLoadedModels() {
|
|
98
|
+
return this.table.getLoadedModels();
|
|
99
|
+
}
|
|
100
|
+
isLoading(modelId) {
|
|
101
|
+
return this.table.isLoading(modelId);
|
|
102
|
+
}
|
|
103
|
+
/** A `gpu` session holds every card (the library picks one we cannot name); `npu`, `cpu` and `hybrid` none (design D7). */
|
|
104
|
+
gpuOccupancy() {
|
|
105
|
+
const ready = this.table.getLoadedModels().flatMap((modelId) => {
|
|
106
|
+
const session = this.table.get(modelId);
|
|
107
|
+
return session
|
|
108
|
+
? [
|
|
109
|
+
{
|
|
110
|
+
model_id: modelId,
|
|
111
|
+
cards: session.extra.device === 'gpu' ? 'all' : [],
|
|
112
|
+
auxiliary: false,
|
|
113
|
+
state: 'ready',
|
|
114
|
+
},
|
|
115
|
+
]
|
|
116
|
+
: [];
|
|
117
|
+
});
|
|
118
|
+
const loading = [...this.claimed.values()].filter((claim) => !this.table.findSession(claim.model_id));
|
|
119
|
+
return [...ready, ...loading];
|
|
120
|
+
}
|
|
121
|
+
/** What the engine-builds module needs: the load queue, and the builds sessions run from (design D2). */
|
|
122
|
+
engineHost() {
|
|
123
|
+
return {
|
|
124
|
+
exclusive: (fn) => this.table.exclusive(fn),
|
|
125
|
+
inUse: async () => [
|
|
126
|
+
...new Set(this.table.getLoadedModels().flatMap((modelId) => {
|
|
127
|
+
const build = this.table.get(modelId)?.extra.build;
|
|
128
|
+
return build ? [build] : [];
|
|
129
|
+
})),
|
|
130
|
+
],
|
|
131
|
+
activate: async (dir) => {
|
|
132
|
+
for (const modelId of this.table.getLoadedModels())
|
|
133
|
+
if (this.table.get(modelId)?.extra.build !== dir)
|
|
134
|
+
await this.unload(modelId);
|
|
135
|
+
},
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
routePolicy(modelId) {
|
|
139
|
+
const session = this.table.get(modelId);
|
|
140
|
+
if (!session)
|
|
141
|
+
return undefined;
|
|
142
|
+
return {
|
|
143
|
+
routes: GENIEX_ROUTES,
|
|
144
|
+
tools: session.extra.block.tools,
|
|
145
|
+
structuredOutput: true,
|
|
146
|
+
mapError: () => null,
|
|
147
|
+
contextLength: session.extra.context,
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
/** The model's facts, read by this binary's host once per path and modification time. */
|
|
151
|
+
async inspect(path) {
|
|
152
|
+
const mtime = await stat(path).then((s) => s.mtimeMs, () => {
|
|
153
|
+
throw new AtomicCoreError('MODEL_NOT_FOUND', `The model ${path} is missing.`);
|
|
154
|
+
});
|
|
155
|
+
const key = `${path}|${mtime}`;
|
|
156
|
+
const known = this.facts.get(key);
|
|
157
|
+
if (known)
|
|
158
|
+
return known;
|
|
159
|
+
const read = this.options.inspectModel
|
|
160
|
+
? await this.options.inspectModel(path)
|
|
161
|
+
: await runGeniexHostInspect(this.options.hostCommand, path, {
|
|
162
|
+
env: this.options.baseEnv ?? process.env,
|
|
163
|
+
});
|
|
164
|
+
this.facts.set(key, read);
|
|
165
|
+
return read;
|
|
166
|
+
}
|
|
167
|
+
async load(modelId, opts = {}) {
|
|
168
|
+
this.table.assertRunning();
|
|
169
|
+
return this.table.load(modelId, () => this.start(modelId, opts), opts.signal);
|
|
170
|
+
}
|
|
171
|
+
async start(modelId, opts) {
|
|
172
|
+
throwIfLoadCancelled(opts.signal);
|
|
173
|
+
const registry = this.options.registry;
|
|
174
|
+
const yml = await registry.read(modelId);
|
|
175
|
+
const block = readGeniexBlock(yml);
|
|
176
|
+
if (block.plugin === 'qairt' && block.chip !== this.options.chip)
|
|
177
|
+
throw new AtomicCoreError('GENIEX_VARIANT_UNSUPPORTED', `This AI Hub bundle is built for the Snapdragon ${block.chip ?? 'unknown'}; this computer has the ${this.options.chip}.`);
|
|
178
|
+
const settings = await this.options.readSettings();
|
|
179
|
+
const device = launchDevice(settings['device'], block);
|
|
180
|
+
if (block.plugin === 'qairt' && device !== 'npu')
|
|
181
|
+
throw new AtomicCoreError('GENIEX_VARIANT_UNSUPPORTED', `AI Hub bundles run on the NPU only, not on the ${device}; set the device to NPU in the GenieX settings.`);
|
|
182
|
+
const build = await this.options.resolveBuild();
|
|
183
|
+
if (!build)
|
|
184
|
+
throw new AtomicCoreError('BINARY_NOT_FOUND', 'GenieX is not installed; install it before loading a model.');
|
|
185
|
+
if (!deviceFound(device, build.devices))
|
|
186
|
+
throw new AtomicCoreError('GENIEX_VARIANT_UNSUPPORTED', deviceMissingReason(device), build.devices.join(', ') || 'no devices');
|
|
187
|
+
const { modelPath, mmprojPath: declared } = registry.resolvePaths(yml);
|
|
188
|
+
// The projector: `mmproj_path` of model.yml, else the block's file name beside the model.
|
|
189
|
+
const mmprojPath = declared ?? (block.mmproj ? join(dirname(modelPath), block.mmproj) : undefined);
|
|
190
|
+
const info = await this.inspect(modelPath);
|
|
191
|
+
if (info.kind === 'qairt' &&
|
|
192
|
+
info.min_runtime_version &&
|
|
193
|
+
compareVersions(info.min_runtime_version, build.version) > 0)
|
|
194
|
+
throw new AtomicCoreError('GENIEX_MODEL_REQUIRES_NEWER_ENGINE', `This bundle needs GenieX ${info.min_runtime_version}; the active build is ${build.version}. Update GenieX first.`, info.min_runtime_version);
|
|
195
|
+
const context = engineContext(settings['ctx_len'], block, info.max_context_tokens);
|
|
196
|
+
const timeoutSecs = opts.timeoutSecs ??
|
|
197
|
+
(Number(settings['timeout']) > 0 ? Number(settings['timeout']) : GENIEX_DEFAULT_TIMEOUT_SECS);
|
|
198
|
+
const mode = powerMode(settings['power_mode']);
|
|
199
|
+
if (!(opts.bypassAutoUnload ?? false))
|
|
200
|
+
for (const loaded of this.table.getLoadedModels()) {
|
|
201
|
+
throwIfLoadCancelled(opts.signal);
|
|
202
|
+
await this.unload(loaded);
|
|
203
|
+
}
|
|
204
|
+
this.table.assertRunning();
|
|
205
|
+
throwIfLoadCancelled(opts.signal);
|
|
206
|
+
// GPU residency (design D7): a gpu session holds every card; the NPU, the CPU and hybrid claim nothing.
|
|
207
|
+
const holdsGpu = device === 'gpu';
|
|
208
|
+
const occupancy = {
|
|
209
|
+
model_id: modelId,
|
|
210
|
+
cards: holdsGpu ? 'all' : [],
|
|
211
|
+
auxiliary: false,
|
|
212
|
+
state: 'loading',
|
|
213
|
+
};
|
|
214
|
+
const hold = () => void this.claimed.set(modelId, occupancy);
|
|
215
|
+
try {
|
|
216
|
+
if (holdsGpu && this.options.claimGpu) {
|
|
217
|
+
await this.options.claimGpu({ model_id: modelId, cards: 'all', auxiliary: false }, opts.signal, hold);
|
|
218
|
+
this.table.assertRunning();
|
|
219
|
+
throwIfLoadCancelled(opts.signal);
|
|
220
|
+
}
|
|
221
|
+
hold();
|
|
222
|
+
return await this.startHost(modelId, opts, {
|
|
223
|
+
block,
|
|
224
|
+
device,
|
|
225
|
+
build,
|
|
226
|
+
modelPath,
|
|
227
|
+
mmprojPath,
|
|
228
|
+
context,
|
|
229
|
+
timeoutSecs,
|
|
230
|
+
mode,
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
finally {
|
|
234
|
+
if (this.claimed.get(modelId) === occupancy)
|
|
235
|
+
this.claimed.delete(modelId);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
async startHost(modelId, opts, launch) {
|
|
239
|
+
const { block, device, build, modelPath, mmprojPath, context, timeoutSecs, mode } = launch;
|
|
240
|
+
const paths = this.options.layout.provider('geniex');
|
|
241
|
+
const env = {};
|
|
242
|
+
for (const [key, value] of Object.entries(this.options.baseEnv ?? process.env))
|
|
243
|
+
if (value !== undefined)
|
|
244
|
+
env[key] = value;
|
|
245
|
+
const exe = this.options.hostCommand.exe;
|
|
246
|
+
const reportOutput = backendOutputReporter(this.options.backendOutput, this.options.log);
|
|
247
|
+
this.options.log?.('info', `starting the GenieX host for ${modelId} (${block.plugin} on ${device}) from ${build.dir}`);
|
|
248
|
+
// The free-port probe is racy: another process may bind the port before the host does. The host
|
|
249
|
+
// then exits on EADDRINUSE, and is started again on a port picked anew (as llama-server is).
|
|
250
|
+
const { started, hostPort } = await retryOnTakenPort(async () => {
|
|
251
|
+
const hostPort = await randomFreePort(this.table.usedPorts());
|
|
252
|
+
const tmp = join(paths.tmpDir, `${modelId.replace(/[^A-Za-z0-9._-]+/g, '_')}-${hostPort}`);
|
|
253
|
+
const args = [
|
|
254
|
+
...this.options.hostCommand.args,
|
|
255
|
+
'serve',
|
|
256
|
+
'--lib',
|
|
257
|
+
build.dir,
|
|
258
|
+
'--engine-version',
|
|
259
|
+
build.version,
|
|
260
|
+
'--model',
|
|
261
|
+
modelPath,
|
|
262
|
+
'--plugin',
|
|
263
|
+
block.plugin,
|
|
264
|
+
'--device',
|
|
265
|
+
device,
|
|
266
|
+
...(mmprojPath ? ['--mmproj', mmprojPath] : []),
|
|
267
|
+
'--nctx',
|
|
268
|
+
String(context),
|
|
269
|
+
'--max-tokens',
|
|
270
|
+
String(context),
|
|
271
|
+
'--power-mode',
|
|
272
|
+
mode,
|
|
273
|
+
'--port',
|
|
274
|
+
String(hostPort),
|
|
275
|
+
'--tmp',
|
|
276
|
+
tmp,
|
|
277
|
+
'--id',
|
|
278
|
+
modelId,
|
|
279
|
+
...(block.reasoning ? ['--reasoning'] : []),
|
|
280
|
+
];
|
|
281
|
+
const started = await (this.options.spawn ?? spawnAndAwaitReady)({ exe, args, env, stdin: 'pipe', platform: process.platform }, {
|
|
282
|
+
timeoutMs: timeoutSecs * 1000,
|
|
283
|
+
signal: this.table.signal,
|
|
284
|
+
...(opts.signal ? { cancelSignal: opts.signal } : {}),
|
|
285
|
+
streamReadyMarkers: { stdout: ['"ready":true'], stderr: [] },
|
|
286
|
+
timeoutError: (stderr) => new AtomicCoreError('MODEL_LOAD_TIMED_OUT', `The GenieX model did not load within ${timeoutSecs} seconds.`, stderr.trim().split('\n').slice(-5).join('\n')),
|
|
287
|
+
classifyExit: (exit, stderr) => hostError(exit.code, stderr),
|
|
288
|
+
onLine: (stream, line) => {
|
|
289
|
+
if (opts.verbose)
|
|
290
|
+
this.emit('core:log', { level: 'debug', msg: `[geniex/${modelId}][${stream}] ${line}` });
|
|
291
|
+
reportOutput({ provider: 'geniex', model: modelId, stream, line });
|
|
292
|
+
},
|
|
293
|
+
});
|
|
294
|
+
return { started, hostPort };
|
|
295
|
+
}, (error) => error instanceof AtomicCoreError && exitedOnTakenPort(error.details), (attempt) => this.options.log?.('warn', `the GenieX host for ${modelId} found its port taken (attempt ${attempt}); starting it on another`));
|
|
296
|
+
let gateway;
|
|
297
|
+
const apiKey = generateGatewayKey();
|
|
298
|
+
try {
|
|
299
|
+
gateway = await startManagedGateway({
|
|
300
|
+
upstream: { host: '127.0.0.1', port: hostPort },
|
|
301
|
+
apiKey,
|
|
302
|
+
allowedHosts: this.options.trustedHosts,
|
|
303
|
+
routes: GENIEX_ROUTES,
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
catch (error) {
|
|
307
|
+
await started.process.terminate(this.table.isClosing ? 0 : 5_000).catch(() => { });
|
|
308
|
+
throw error;
|
|
309
|
+
}
|
|
310
|
+
void started.process.exited.then(() => gateway.close().catch(() => { }));
|
|
311
|
+
try {
|
|
312
|
+
return await this.table.adopt({
|
|
313
|
+
info: {
|
|
314
|
+
pid: started.process.pid,
|
|
315
|
+
port: gateway.port,
|
|
316
|
+
model_id: modelId,
|
|
317
|
+
model_path: modelPath,
|
|
318
|
+
is_embedding: false,
|
|
319
|
+
api_key: apiKey,
|
|
320
|
+
},
|
|
321
|
+
process: started.process,
|
|
322
|
+
exe,
|
|
323
|
+
extra: { build: build.dir, block, device, gateway, context },
|
|
324
|
+
}, opts.signal);
|
|
325
|
+
}
|
|
326
|
+
catch (error) {
|
|
327
|
+
await gateway.close().catch(() => { });
|
|
328
|
+
throw error;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
/** Waits for the host to exit; the gateway goes with it. */
|
|
332
|
+
async unload(modelId) {
|
|
333
|
+
this.table.assertRunning();
|
|
334
|
+
const gateway = this.table.get(modelId)?.extra.gateway;
|
|
335
|
+
const result = await this.table.unload(modelId);
|
|
336
|
+
if (result.success)
|
|
337
|
+
await gateway?.close().catch(() => { });
|
|
338
|
+
return result;
|
|
339
|
+
}
|
|
340
|
+
/** The context is fixed when the model is created: nothing to grow. */
|
|
341
|
+
async autoIncreaseCtx(modelId) {
|
|
342
|
+
return this.table.get(modelId)
|
|
343
|
+
? { ok: false, reason: 'unsupported' }
|
|
344
|
+
: { ok: false, reason: 'not-loaded' };
|
|
345
|
+
}
|
|
346
|
+
async recreateSession(modelId) {
|
|
347
|
+
this.table.assertRunning();
|
|
348
|
+
if (!this.table.get(modelId))
|
|
349
|
+
return { ok: false, reason: 'not-loaded' };
|
|
350
|
+
await this.unload(modelId);
|
|
351
|
+
return { ok: true, session: await this.load(modelId, { bypassAutoUnload: true }) };
|
|
352
|
+
}
|
|
353
|
+
async shutdown() {
|
|
354
|
+
const gateways = this.table.getLoadedModels().flatMap((id) => {
|
|
355
|
+
const gateway = this.table.get(id)?.extra.gateway;
|
|
356
|
+
return gateway ? [gateway] : [];
|
|
357
|
+
});
|
|
358
|
+
try {
|
|
359
|
+
await this.table.shutdown();
|
|
360
|
+
}
|
|
361
|
+
finally {
|
|
362
|
+
await Promise.all(gateways.map((gateway) => gateway.close().catch(() => { })));
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
//# sourceMappingURL=runtime.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runtime.js","sourceRoot":"","sources":["../../../src/runtime/geniex/runtime.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAE,I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,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,YAAY,EAAE,CAAA;QACxE,MAAM,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAA;QAC1B,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,EAAE,gBAAgB,EAAE,IAAI,EAAE,CAAC,EAAE,CAAA;IACpF,CAAC;IAED,KAAK,CAAC,QAAQ;QACZ,MAAM,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,eAAe,EAAE,CAAC,OAAO,CAAC,CAAC,EAAE,EAAE,EAAE;YAC3D,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,KAAK,CAAC,OAAO,CAAA;YACjD,OAAO,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,EAAE,CAAA;QACjC,CAAC,CAAC,CAAA;QACF,IAAI,CAAC;YACH,MAAM,IAAI,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAA;QAC7B,CAAC;gBAAS,CAAC;YACT,MAAM,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC,CAAC,CAAA;QAC/E,CAAC;IACH,CAAC;CACF"}
|
|
@@ -8,26 +8,10 @@
|
|
|
8
8
|
* of and the host adds: `stop` strings, OpenAI's string `arguments`, call ids, `finish_reason`.
|
|
9
9
|
*/
|
|
10
10
|
import type { LiteRtMessage, LiteRtSampler } from './library.js';
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
param: string | null;
|
|
16
|
-
code: string | null;
|
|
17
|
-
};
|
|
18
|
-
}
|
|
19
|
-
/** A request this server answers with an OpenAI-shaped error instead of a completion. */
|
|
20
|
-
export declare class OpenAiRequestError extends Error {
|
|
21
|
-
readonly status: number;
|
|
22
|
-
readonly body: OpenAiErrorBody;
|
|
23
|
-
constructor(status: number, message: string, param?: string | null, type?: string, code?: string | null);
|
|
24
|
-
}
|
|
25
|
-
export interface ModelCaps {
|
|
26
|
-
/** The file declares image input. */
|
|
27
|
-
vision: boolean;
|
|
28
|
-
/** Think unless the request says not to (`model.yml` `reasoning`). */
|
|
29
|
-
reasoning: boolean;
|
|
30
|
-
}
|
|
11
|
+
import { OpenAiRequestError } from '../../shared/index.js';
|
|
12
|
+
import type { Delta, FinishReason, ModelCaps } from '../../shared/index.js';
|
|
13
|
+
export { OpenAiRequestError };
|
|
14
|
+
export type { Delta, FinishReason, ModelCaps, OpenAiErrorBody } from '../../shared/index.js';
|
|
31
15
|
export interface ParsedChat {
|
|
32
16
|
/** Every message but the trailing user message or run of tool results. */
|
|
33
17
|
history: LiteRtMessage[];
|
|
@@ -48,23 +32,6 @@ export interface ParsedChat {
|
|
|
48
32
|
}
|
|
49
33
|
/** A chat completions body, or `OpenAiRequestError` (400) naming the field it cannot serve. */
|
|
50
34
|
export declare function parseChatRequest(raw: unknown, caps: ModelCaps): ParsedChat;
|
|
51
|
-
/** One OpenAI delta (`choices[0].delta`) without the role. */
|
|
52
|
-
export type Delta = {
|
|
53
|
-
content: string;
|
|
54
|
-
} | {
|
|
55
|
-
reasoning_content: string;
|
|
56
|
-
} | {
|
|
57
|
-
tool_calls: Array<{
|
|
58
|
-
index: number;
|
|
59
|
-
id: string;
|
|
60
|
-
type: 'function';
|
|
61
|
-
function: {
|
|
62
|
-
name: string;
|
|
63
|
-
arguments: string;
|
|
64
|
-
};
|
|
65
|
-
}>;
|
|
66
|
-
};
|
|
67
|
-
export type FinishReason = 'stop' | 'length' | 'tool_calls';
|
|
68
35
|
/** Builds one reply from the library's chunks: deltas as they come, the whole message at the end. */
|
|
69
36
|
export declare class ReplyBuilder {
|
|
70
37
|
private readonly stops;
|
|
@@ -7,19 +7,8 @@
|
|
|
7
7
|
* an `arguments` object (one chunk, no deltas), thinking as `channels.thought`. What it has no notion
|
|
8
8
|
* of and the host adds: `stop` strings, OpenAI's string `arguments`, call ids, `finish_reason`.
|
|
9
9
|
*/
|
|
10
|
-
|
|
11
|
-
export
|
|
12
|
-
status;
|
|
13
|
-
body;
|
|
14
|
-
constructor(status, message, param = null, type = status >= 500 ? 'server_error' : 'invalid_request_error', code = null) {
|
|
15
|
-
super(message);
|
|
16
|
-
this.status = status;
|
|
17
|
-
this.name = 'OpenAiRequestError';
|
|
18
|
-
this.body = { error: { message, type, param, code } };
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
const invalid = (message, param = null) => new OpenAiRequestError(400, message, param);
|
|
22
|
-
const isRecord = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
10
|
+
import { DATA_URL_RE, invalidRequest as invalid, isRecord, OpenAiRequestError, parseCommonChatRequest, textOf, } from '../../shared/index.js';
|
|
11
|
+
export { OpenAiRequestError };
|
|
23
12
|
const text = (value) => ({ type: 'text', text: value });
|
|
24
13
|
function parseArguments(raw) {
|
|
25
14
|
if (typeof raw !== 'string')
|
|
@@ -41,22 +30,6 @@ function parseToolResponse(content) {
|
|
|
41
30
|
return raw;
|
|
42
31
|
}
|
|
43
32
|
}
|
|
44
|
-
function textOf(content, param) {
|
|
45
|
-
if (content == null)
|
|
46
|
-
return '';
|
|
47
|
-
if (typeof content === 'string')
|
|
48
|
-
return content;
|
|
49
|
-
if (!Array.isArray(content))
|
|
50
|
-
throw invalid(`${param} must be a string or an array of parts`, param);
|
|
51
|
-
return content
|
|
52
|
-
.map((part) => {
|
|
53
|
-
if (isRecord(part) && part['type'] === 'text' && typeof part['text'] === 'string')
|
|
54
|
-
return part['text'];
|
|
55
|
-
throw invalid(`${param} may hold text parts only`, param);
|
|
56
|
-
})
|
|
57
|
-
.join('');
|
|
58
|
-
}
|
|
59
|
-
const DATA_URL_RE = /^data:image\/[a-z0-9.+-]+;base64,([A-Za-z0-9+/=]+)$/i;
|
|
60
33
|
function userContent(content, caps, param) {
|
|
61
34
|
if (typeof content === 'string')
|
|
62
35
|
return [text(content)];
|
|
@@ -117,49 +90,11 @@ function convertMessages(raw, caps) {
|
|
|
117
90
|
throw invalid(`${param}.role ${JSON.stringify(role)} is not system, developer, user, assistant or tool`, `${param}.role`);
|
|
118
91
|
});
|
|
119
92
|
}
|
|
120
|
-
function optionalNumber(body, key) {
|
|
121
|
-
const value = body[key];
|
|
122
|
-
if (value === undefined || value === null)
|
|
123
|
-
return undefined;
|
|
124
|
-
if (typeof value !== 'number' || !Number.isFinite(value))
|
|
125
|
-
throw invalid(`${key} must be a number`, key);
|
|
126
|
-
return value;
|
|
127
|
-
}
|
|
128
|
-
function parseResponseFormat(raw) {
|
|
129
|
-
if (raw === undefined || raw === null)
|
|
130
|
-
return null;
|
|
131
|
-
if (!isRecord(raw))
|
|
132
|
-
throw invalid('response_format must be an object', 'response_format');
|
|
133
|
-
if (raw['type'] === 'text')
|
|
134
|
-
return null;
|
|
135
|
-
if (raw['type'] === 'json_object')
|
|
136
|
-
return { kind: 'json_schema', value: '{"type":"object"}' };
|
|
137
|
-
if (raw['type'] === 'json_schema') {
|
|
138
|
-
const schema = isRecord(raw['json_schema']) ? raw['json_schema']['schema'] : undefined;
|
|
139
|
-
if (!isRecord(schema))
|
|
140
|
-
throw invalid('response_format.json_schema.schema must be a JSON schema', 'response_format');
|
|
141
|
-
return { kind: 'json_schema', value: JSON.stringify(schema) };
|
|
142
|
-
}
|
|
143
|
-
throw invalid(`response_format type ${JSON.stringify(raw['type'])} is not supported`, 'response_format');
|
|
144
|
-
}
|
|
145
|
-
function parseThinking(body, caps) {
|
|
146
|
-
const kwargs = body['chat_template_kwargs'];
|
|
147
|
-
if (isRecord(kwargs) && typeof kwargs['enable_thinking'] === 'boolean')
|
|
148
|
-
return kwargs['enable_thinking'];
|
|
149
|
-
const effort = body['reasoning_effort'];
|
|
150
|
-
if (typeof effort === 'string')
|
|
151
|
-
return effort !== 'none';
|
|
152
|
-
return caps.reasoning;
|
|
153
|
-
}
|
|
154
93
|
/** A chat completions body, or `OpenAiRequestError` (400) naming the field it cannot serve. */
|
|
155
94
|
export function parseChatRequest(raw, caps) {
|
|
156
95
|
if (!isRecord(raw))
|
|
157
96
|
throw invalid('The request body must be a JSON object.');
|
|
158
|
-
const
|
|
159
|
-
if (n !== undefined && n !== null && n !== 1)
|
|
160
|
-
throw invalid('Only n: 1 is supported.', 'n');
|
|
161
|
-
if (!Array.isArray(raw['messages']) || raw['messages'].length === 0)
|
|
162
|
-
throw invalid('messages must be a non-empty array', 'messages');
|
|
97
|
+
const common = parseCommonChatRequest(raw, caps);
|
|
163
98
|
const messages = convertMessages(raw['messages'], caps);
|
|
164
99
|
let split = messages.length;
|
|
165
100
|
while (split > 0 && messages[split - 1]?.['role'] === 'tool')
|
|
@@ -169,37 +104,17 @@ export function parseChatRequest(raw, caps) {
|
|
|
169
104
|
throw invalid('The last message must be from the user or a tool.', 'messages');
|
|
170
105
|
split--;
|
|
171
106
|
}
|
|
172
|
-
const toolChoice = raw['tool_choice'];
|
|
173
|
-
if (toolChoice !== undefined && toolChoice !== null && toolChoice !== 'auto' && toolChoice !== 'none')
|
|
174
|
-
throw invalid('Only tool_choice "auto" and "none" are supported.', 'tool_choice');
|
|
175
|
-
const tools = Array.isArray(raw['tools']) && raw['tools'].length > 0 && toolChoice !== 'none' ? raw['tools'] : null;
|
|
176
|
-
const streamOptions = raw['stream_options'];
|
|
177
|
-
const stop = raw['stop'];
|
|
178
|
-
const maxTokens = optionalNumber(raw, 'max_completion_tokens') ?? optionalNumber(raw, 'max_tokens');
|
|
179
|
-
const temperature = optionalNumber(raw, 'temperature');
|
|
180
|
-
const topP = optionalNumber(raw, 'top_p');
|
|
181
|
-
const topK = optionalNumber(raw, 'top_k');
|
|
182
|
-
const seed = optionalNumber(raw, 'seed');
|
|
183
107
|
return {
|
|
184
108
|
history: messages.slice(0, split),
|
|
185
109
|
next: messages.slice(split),
|
|
186
|
-
tools,
|
|
187
|
-
stream:
|
|
188
|
-
includeUsage:
|
|
189
|
-
sampler:
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
},
|
|
195
|
-
...(maxTokens !== undefined ? { maxTokens: Math.max(1, Math.round(maxTokens)) } : {}),
|
|
196
|
-
stop: typeof stop === 'string'
|
|
197
|
-
? [stop]
|
|
198
|
-
: Array.isArray(stop)
|
|
199
|
-
? stop.filter((s) => typeof s === 'string' && s)
|
|
200
|
-
: [],
|
|
201
|
-
responseFormat: parseResponseFormat(raw['response_format']),
|
|
202
|
-
thinking: parseThinking(raw, caps),
|
|
110
|
+
tools: common.tools,
|
|
111
|
+
stream: common.stream,
|
|
112
|
+
includeUsage: common.includeUsage,
|
|
113
|
+
sampler: common.sampler,
|
|
114
|
+
...(common.maxTokens !== undefined ? { maxTokens: common.maxTokens } : {}),
|
|
115
|
+
stop: common.stop,
|
|
116
|
+
responseFormat: common.responseFormat,
|
|
117
|
+
thinking: common.thinking,
|
|
203
118
|
};
|
|
204
119
|
}
|
|
205
120
|
/** The library's words for a generation it ended at the token limit; not an error to the client. */
|