narrarium-engine 0.1.7 → 0.1.8
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/README.md +185 -13
- package/dist/ai/body.d.ts +20 -0
- package/dist/ai/body.d.ts.map +1 -0
- package/dist/ai/body.js +112 -0
- package/dist/ai/body.js.map +1 -0
- package/dist/ai/catalog-fetch.d.ts +51 -0
- package/dist/ai/catalog-fetch.d.ts.map +1 -0
- package/dist/ai/catalog-fetch.js +260 -0
- package/dist/ai/catalog-fetch.js.map +1 -0
- package/dist/ai/catalog-support.d.ts +13 -0
- package/dist/ai/catalog-support.d.ts.map +1 -0
- package/dist/ai/catalog-support.js +28 -0
- package/dist/ai/catalog-support.js.map +1 -0
- package/dist/ai/catalog.d.ts +148 -0
- package/dist/ai/catalog.d.ts.map +1 -0
- package/dist/ai/catalog.js +367 -0
- package/dist/ai/catalog.js.map +1 -0
- package/dist/ai/config.d.ts +83 -7
- package/dist/ai/config.d.ts.map +1 -1
- package/dist/ai/config.js +83 -11
- package/dist/ai/config.js.map +1 -1
- package/dist/ai/fetch-client.d.ts +20 -0
- package/dist/ai/fetch-client.d.ts.map +1 -1
- package/dist/ai/fetch-client.js +49 -24
- package/dist/ai/fetch-client.js.map +1 -1
- package/dist/ai/probe.d.ts +40 -0
- package/dist/ai/probe.d.ts.map +1 -0
- package/dist/ai/probe.js +110 -0
- package/dist/ai/probe.js.map +1 -0
- package/dist/ai/request.d.ts +58 -5
- package/dist/ai/request.d.ts.map +1 -1
- package/dist/ai/request.js +96 -7
- package/dist/ai/request.js.map +1 -1
- package/dist/ai/router.d.ts +12 -1
- package/dist/ai/router.d.ts.map +1 -1
- package/dist/ai/router.js +10 -2
- package/dist/ai/router.js.map +1 -1
- package/dist/ai/routing.d.ts +15 -4
- package/dist/ai/routing.d.ts.map +1 -1
- package/dist/ai/routing.js +25 -6
- package/dist/ai/routing.js.map +1 -1
- package/dist/ai/token-limit.d.ts +4 -4
- package/dist/ai/token-limit.d.ts.map +1 -1
- package/dist/ai/token-limit.js +5 -4
- package/dist/ai/token-limit.js.map +1 -1
- package/dist/ai/types.d.ts +12 -1
- package/dist/ai/types.d.ts.map +1 -1
- package/dist/ai/types.js +12 -0
- package/dist/ai/types.js.map +1 -1
- package/dist/autopilot/errors.d.ts +4 -3
- package/dist/autopilot/errors.d.ts.map +1 -1
- package/dist/autopilot/errors.js +21 -5
- package/dist/autopilot/errors.js.map +1 -1
- package/dist/autopilot/runner.d.ts.map +1 -1
- package/dist/autopilot/runner.js +21 -4
- package/dist/autopilot/runner.js.map +1 -1
- package/dist/autopilot/session.js +1 -1
- package/dist/autopilot/session.js.map +1 -1
- package/dist/autopilot/state.d.ts +4 -3
- package/dist/autopilot/state.d.ts.map +1 -1
- package/dist/autopilot/state.js +35 -9
- package/dist/autopilot/state.js.map +1 -1
- package/dist/autopilot/tasks.js +2 -2
- package/dist/autopilot/tasks.js.map +1 -1
- package/dist/autopilot/types.d.ts +27 -5
- package/dist/autopilot/types.d.ts.map +1 -1
- package/dist/autopilot/types.js +1 -1
- package/dist/autopilot/types.js.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/server/protocol.d.ts +6 -1
- package/dist/server/protocol.d.ts.map +1 -1
- package/dist/server/protocol.js.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
package/dist/ai/probe.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { BodyError, discardBody, readBoundedJson } from "./body.js";
|
|
2
|
+
import { AiCatalogError, parseModelList } from "./catalog.js";
|
|
3
|
+
import { abortReason, nameOf, timeoutOf, unsupportedCatalog } from "./catalog-support.js";
|
|
4
|
+
import { formatDuration, startClock } from "./deadlines.js";
|
|
5
|
+
import { redactSecrets } from "./redact.js";
|
|
6
|
+
import { catalogTarget } from "./request.js";
|
|
7
|
+
/** How long the probe waits for the endpoint to answer: a short GET, as BookWriter's `localEndpointTimeoutHint` makes. */
|
|
8
|
+
export const PROBE_TIMEOUT_MS = 4_000;
|
|
9
|
+
/** The most of the answer the probe reads, to count the models: a bigger one is not read, and the endpoint has answered all the same. */
|
|
10
|
+
const PROBE_MAX_BYTES = 256_000;
|
|
11
|
+
const CORS_NOTE = "In a browser, a server that does not allow this site (CORS) looks exactly like a server that is off: if it runs, allow this site in its CORS settings.";
|
|
12
|
+
function reachedMessage(name, status) {
|
|
13
|
+
if (status === 401)
|
|
14
|
+
return `${name} is reachable but refused the API key (HTTP 401). Check the key of the integration.`;
|
|
15
|
+
if (status === 403)
|
|
16
|
+
return `${name} is reachable but denied access (HTTP 403). The key may be wrong or not allowed to list models: check the key of the integration.`;
|
|
17
|
+
if (status === 404)
|
|
18
|
+
return `${name} is reachable but has no list of models at its address (HTTP 404). Check that the address ends with the version path (for example /v1).`;
|
|
19
|
+
if (status === 429)
|
|
20
|
+
return `${name} is reachable but is limiting requests (HTTP 429). Wait a moment and try again.`;
|
|
21
|
+
if (status >= 500)
|
|
22
|
+
return `${name} is reachable but failing (HTTP ${status}): it may be busy or restarting. Try again in a moment.`;
|
|
23
|
+
return `${name} is reachable but answered HTTP ${status} to the request for its models. Check the address and the key.`;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Asks an OpenAI-compatible endpoint one short question, `GET <base>/models` (the base chat uses), and says whether it answers: how fast, with which
|
|
27
|
+
* status, how many models it lists, and, when it does not answer, that a browser cannot tell why (the server is off, the address is wrong, CORS blocked
|
|
28
|
+
* the request): the foundation of a "Check the connection" in an error panel. A request that is refused with a status is still an answer: the endpoint is
|
|
29
|
+
* reachable, and the status says what to fix (the key, the address). The key is sent, so that a gateway that refuses it is told from one that is off.
|
|
30
|
+
* Never throws for what the endpoint does: only for a cancel (`AbortError`) and for an integration that has no such list to ask (`AiCatalogError`,
|
|
31
|
+
* reason `unsupported`: Azure OpenAI, GitHub Models, Microsoft 365).
|
|
32
|
+
*/
|
|
33
|
+
export async function probeEndpoint(integration, options = {}) {
|
|
34
|
+
const target = catalogTarget(integration, "/models");
|
|
35
|
+
if (!target)
|
|
36
|
+
throw unsupportedCatalog(integration);
|
|
37
|
+
const { signal } = options;
|
|
38
|
+
if (signal?.aborted)
|
|
39
|
+
throw abortReason(signal);
|
|
40
|
+
const name = nameOf(integration);
|
|
41
|
+
const secrets = [integration.apiKey];
|
|
42
|
+
const timeoutMs = timeoutOf(options.timeoutMs, PROBE_TIMEOUT_MS);
|
|
43
|
+
const fetchImpl = options.fetch ?? fetch;
|
|
44
|
+
const say = (message) => redactSecrets(message, secrets);
|
|
45
|
+
const startedAt = Date.now();
|
|
46
|
+
const clock = startClock(signal, { startMs: timeoutMs, idleMs: timeoutMs, capMs: timeoutMs * 2 });
|
|
47
|
+
try {
|
|
48
|
+
let response;
|
|
49
|
+
try {
|
|
50
|
+
const sent = Promise.resolve(fetchImpl(target.url, {
|
|
51
|
+
method: "GET",
|
|
52
|
+
headers: { accept: "application/json", ...target.headers },
|
|
53
|
+
credentials: "omit",
|
|
54
|
+
redirect: "error",
|
|
55
|
+
cache: "no-store",
|
|
56
|
+
referrerPolicy: "no-referrer",
|
|
57
|
+
signal: clock.signal,
|
|
58
|
+
}));
|
|
59
|
+
sent.then((late) => {
|
|
60
|
+
if (clock.interrupted())
|
|
61
|
+
discardBody(late);
|
|
62
|
+
}, () => undefined);
|
|
63
|
+
response = await clock.guard(sent);
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
if (signal?.aborted)
|
|
67
|
+
throw abortReason(signal);
|
|
68
|
+
const latencyMs = Date.now() - startedAt;
|
|
69
|
+
const expired = clock.expired();
|
|
70
|
+
const what = expired
|
|
71
|
+
? `${name} did not answer within ${formatDuration(expired.ms)}. The server may be off, busy or unreachable: check that it is running and that the address is right.`
|
|
72
|
+
: `Could not reach ${name}: the connection failed. Check that the server is running and that the address is right (a redirect is refused: use the final address).`;
|
|
73
|
+
return { outcome: "unreachable", latencyMs, cors: "blocked-or-unreachable", message: say(`${what} ${CORS_NOTE}`) };
|
|
74
|
+
}
|
|
75
|
+
const latencyMs = Date.now() - startedAt;
|
|
76
|
+
const httpStatus = response.status;
|
|
77
|
+
if (!response.ok) {
|
|
78
|
+
discardBody(response);
|
|
79
|
+
return { outcome: "http-error", httpStatus, latencyMs, cors: "ok", message: say(reachedMessage(name, httpStatus)) };
|
|
80
|
+
}
|
|
81
|
+
// It answered. Its models are counted when its body can be read in the time that is left: whatever goes wrong with the body, the endpoint has answered.
|
|
82
|
+
clock.streaming();
|
|
83
|
+
const answered = `${name} answered in ${latencyMs} ms (HTTP ${httpStatus})`;
|
|
84
|
+
try {
|
|
85
|
+
const models = parseModelList(await readBoundedJson(response, clock, PROBE_MAX_BYTES)).length;
|
|
86
|
+
return { outcome: "ok", httpStatus, latencyMs, models, cors: "ok", message: say(`${answered} and lists ${models} ${models === 1 ? "model" : "models"}.`) };
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
if (signal?.aborted)
|
|
90
|
+
throw abortReason(signal);
|
|
91
|
+
if (error instanceof AiCatalogError && error.reason === "empty") {
|
|
92
|
+
return { outcome: "ok", httpStatus, latencyMs, models: 0, cors: "ok", message: say(`${answered} but lists no models. If it is a gateway, start a model there (a model that is stopped is not listed).`) };
|
|
93
|
+
}
|
|
94
|
+
const notAList = (error instanceof AiCatalogError && error.reason === "invalid") || (error instanceof BodyError && error.fault === "invalid");
|
|
95
|
+
return {
|
|
96
|
+
outcome: "ok",
|
|
97
|
+
httpStatus,
|
|
98
|
+
latencyMs,
|
|
99
|
+
cors: "ok",
|
|
100
|
+
message: say(notAList
|
|
101
|
+
? `${answered}, but not with a list of models: check that the address is the API base (for example …/v1) and not a web page.`
|
|
102
|
+
: `${answered}; its list of models was not read.`),
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
finally {
|
|
107
|
+
clock.dispose();
|
|
108
|
+
}
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oC,CAAC;aACrD,CAAC;QACJ,CAAC;IACH,CAAC;YAAS,CAAC;QACT,KAAK,CAAC,OAAO,EAAE,CAAC;IAClB,CAAC;AACH,CAAC"}
|
package/dist/ai/request.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type AiIntegrationConfig } from "./config.js";
|
|
1
|
+
import { type AiIntegrationConfig, type AiReasoningParameter, type AiThinking } from "./config.js";
|
|
2
2
|
import type { AiCandidate } from "./router.js";
|
|
3
3
|
import { type AiRequest } from "./types.js";
|
|
4
4
|
export declare const GITHUB_MODELS_BASE_URL = "https://models.github.ai";
|
|
@@ -14,17 +14,69 @@ export declare function effectiveAiBase(integration: Pick<AiIntegrationConfig, "
|
|
|
14
14
|
* it, which is what may be shown and saved about where a request went. `undefined` for anything that is not an http or https address.
|
|
15
15
|
*/
|
|
16
16
|
export declare function endpointOrigin(value: unknown): string | undefined;
|
|
17
|
+
/**
|
|
18
|
+
* Where a request for the models of an OpenAI-compatible integration goes (`path` is `/models`, `/models/details`, `/modalities`), and how it
|
|
19
|
+
* authenticates: the very base that its chat requests use (`effectiveAiBase`: the endpoint it was given, or OpenAI's own) and the same key header.
|
|
20
|
+
* `null` for an integration that has no such list to read: Azure OpenAI (a deployment is created by hand, nobody lists them), GitHub Models (its
|
|
21
|
+
* catalog is not at this address) and Microsoft 365.
|
|
22
|
+
*/
|
|
23
|
+
export declare function catalogTarget(integration: Pick<AiIntegrationConfig, "provider" | "endpoint" | "apiKey">, path: string): {
|
|
24
|
+
url: string;
|
|
25
|
+
headers: Record<string, string>;
|
|
26
|
+
} | null;
|
|
27
|
+
/** The thinking levels that travel to the provider (as `reasoning_effort`, or as the chat-template argument the model's `reasoningParameter` says): `automatic` and no level at all send nothing. */
|
|
28
|
+
export type AiReasoningEffort = Exclude<AiThinking, "automatic">;
|
|
29
|
+
/**
|
|
30
|
+
* Puts a thinking level into the body of a chat request the way the model's thinking parameter says; it is the one place where a level lands in a body.
|
|
31
|
+
* `reasoning_effort` (and absent, and any word the engine does not know) is the OpenAI field. `chat_template_kwargs.reasoning_strength` is the argument of
|
|
32
|
+
* the chat template, `chat_template_kwargs: { reasoning_strength: <level> }`, merged with the arguments the body already has (the object that was there is
|
|
33
|
+
* not changed, and a value that is not a set of arguments is replaced); `reasoning_effort` is then not sent. `none` puts nothing in the body.
|
|
34
|
+
*/
|
|
35
|
+
export declare function applyReasoningLevel(body: Record<string, unknown>, parameter: AiReasoningParameter | undefined, level: AiReasoningEffort): void;
|
|
36
|
+
/**
|
|
37
|
+
* A hint for a screen, never an order: the thinking parameter that a documented source gives the model with this name, or `undefined` for every other
|
|
38
|
+
* name. Today that is Muse Glimmer, and nothing else: no other family is guessed (Muse Spark included: no source says it). For Muse Glimmer it is
|
|
39
|
+
* `chat_template_kwargs.reasoning_strength`, because
|
|
40
|
+
* - Meta, "Run inference" (https://dev.meta.ai/docs/muse-glimmer/deploy), for llama.cpp: «`reasoning_effort`, the OpenAI spelling, is not implemented by
|
|
41
|
+
* llama.cpp. Use `chat_template_kwargs.reasoning_strength`», per request or for the whole server; and, for ExecuTorch, `reasoning_effort` is rejected with a 400;
|
|
42
|
+
* - Meta, "Prompting guide" (https://dev.meta.ai/docs/muse-glimmer/prompting#reasoning): the level is the `reasoning_strength` template argument, `xhigh`,
|
|
43
|
+
* `high`, `medium` or `low`;
|
|
44
|
+
* - LlmProxy, `docs/model-profiles.md` ("Reasoning controls differ per model") and `src/LlmProxy.NodeAgent/ModelFamilyProfiles.cs` (the Muse Glimmer
|
|
45
|
+
* profile, checked on 2026-11-13).
|
|
46
|
+
* The names are those that these sources use, with or without the part before the last `/` (`meta-models/`, `unsloth/`, `RadixArk/`) and a tag after the
|
|
47
|
+
* name: `muse-glimmer`, `muse-glimmer-30B`, `Muse-Glimmer-30B-GGUF:Q4_K_M`, `muse-glimmer:30b-mlx` and, as LlmProxy's logs name one, `meta-muse-glimmer`.
|
|
48
|
+
*
|
|
49
|
+
* The engine does not apply the hint: a model is called with `reasoning_effort` until its author sets `reasoningParameter`. `underlyingModel` is the model
|
|
50
|
+
* that a deployment really is, when it is declared: it is what is read then (as `isReasoningModel` does for an Azure deployment), else the name. Pure, and
|
|
51
|
+
* cheap for any text.
|
|
52
|
+
*/
|
|
53
|
+
export declare function suggestedReasoningParameter(modelName: string, underlyingModel?: string): AiReasoningParameter | undefined;
|
|
54
|
+
/**
|
|
55
|
+
* The level a request carries: the one that is asked for, unless the model declares that it does not reason (`supportsReasoning: false`), which
|
|
56
|
+
* is a refusal waiting to happen, or its author says that no level is sent to it (`reasoningParameter: "none"`). `automatic`, a level the engine does
|
|
57
|
+
* not know and no level at all send nothing: `automatic` is a choice, so it is not overridden by the integration's level.
|
|
58
|
+
*/
|
|
59
|
+
export declare function reasoningEffortOf(candidate: Pick<AiCandidate, "integration"> & Partial<Pick<AiCandidate, "thinking" | "supportsReasoning" | "reasoningParameter">>): AiReasoningEffort | undefined;
|
|
17
60
|
/** One chat completion request exactly as it goes on the wire. */
|
|
18
61
|
export interface ChatRequest {
|
|
19
62
|
url: string;
|
|
20
63
|
headers: Record<string, string>;
|
|
21
64
|
body: Record<string, unknown>;
|
|
22
|
-
/** The reply is asked for as server-sent events: every request, the forced function included, unless the integration's `streamMode` is `off
|
|
65
|
+
/** The reply is asked for as server-sent events: every request, the forced function included, unless the integration's `streamMode` is `off` or the model declares it does not stream. */
|
|
23
66
|
stream: boolean;
|
|
24
67
|
/** The structured reply is requested as a forced function call; otherwise the JSON schema travels in the system prompt. */
|
|
25
68
|
useTools: boolean;
|
|
26
69
|
/** The output token limit the body asks for (`undefined`: none). */
|
|
27
70
|
outputLimit?: number;
|
|
71
|
+
/** The thinking level the body carries, as `reasoning_effort` or, when `reasoningParameter` says so, as the chat-template argument; absent when none is sent. */
|
|
72
|
+
thinking?: AiReasoningEffort;
|
|
73
|
+
/** Where the level went, when it went in the chat-template argument (`chat_template_kwargs.reasoning_strength`). Absent: it went as `reasoning_effort`, or no level was sent. */
|
|
74
|
+
reasoningParameter?: "chat_template_kwargs.reasoning_strength";
|
|
75
|
+
/**
|
|
76
|
+
* What the request does not do that the configuration asked, because of what a model declares or says: a level of thinking that is not sent to a model that
|
|
77
|
+
* does not reason or whose thinking parameter is `none`, a stream that is not asked of a model that does not stream. Absent when there is nothing to say.
|
|
78
|
+
*/
|
|
79
|
+
notes?: string[];
|
|
28
80
|
}
|
|
29
81
|
export interface ChatRequestOptions {
|
|
30
82
|
/** Origin of GitHub Models (or of a proxy forwarding `/inference`). */
|
|
@@ -33,9 +85,10 @@ export interface ChatRequestOptions {
|
|
|
33
85
|
/**
|
|
34
86
|
* The only place a chat request is built: URL, headers, body, streaming and tool use for every provider and model
|
|
35
87
|
* family, so that every host (browser and server) sends the same request for the same book and configuration.
|
|
36
|
-
* Streaming is the integration's choice (`streamMode`, `auto` unless it says `off`) and not the request's
|
|
37
|
-
* streams too, as the arguments of its forced function, so that a slow
|
|
88
|
+
* Streaming is the integration's choice (`streamMode`, `auto` unless it says `off`) and not the request's, and a model that declares it does not
|
|
89
|
+
* stream (`supportsStreaming: false`) is not asked to: a structured reply streams too, as the arguments of its forced function, so that a slow
|
|
90
|
+
* model is never a silent connection.
|
|
38
91
|
* Pure: it calls nothing and changes nothing. Throws only when the integration cannot be called at all.
|
|
39
92
|
*/
|
|
40
|
-
export declare function buildChatRequest(candidate: Pick<AiCandidate, "integration" | "model"> & Partial<Pick<AiCandidate, "underlyingModel" | "supportsToolCalls" | "maxOutputTokens">>, request: Pick<AiRequest, "system" | "user" | "json" | "maxOutputTokens" | "outputBoost" | "temperature" | "onText">, options?: ChatRequestOptions): ChatRequest;
|
|
93
|
+
export declare function buildChatRequest(candidate: Pick<AiCandidate, "integration" | "model"> & Partial<Pick<AiCandidate, "underlyingModel" | "supportsToolCalls" | "supportsStreaming" | "supportsReasoning" | "reasoningParameter" | "maxOutputTokens" | "thinking">>, request: Pick<AiRequest, "system" | "user" | "json" | "maxOutputTokens" | "outputBoost" | "temperature" | "onText">, options?: ChatRequestOptions): ChatRequest;
|
|
41
94
|
//# sourceMappingURL=request.d.ts.map
|
package/dist/ai/request.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request.d.ts","sourceRoot":"","sources":["../../src/ai/request.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"request.d.ts","sourceRoot":"","sources":["../../src/ai/request.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgD,KAAK,mBAAmB,EAAE,KAAK,oBAAoB,EAAE,KAAK,UAAU,EAAE,MAAM,aAAa,CAAC;AACjJ,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAG/C,OAAO,EAAsC,KAAK,SAAS,EAAE,MAAM,YAAY,CAAC;AAEhF,eAAO,MAAM,sBAAsB,6BAA6B,CAAC;AAOjE;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,WAAW,EAAE,IAAI,CAAC,mBAAmB,EAAE,UAAU,GAAG,UAAU,CAAC,EAAE,mBAAmB,GAAE,MAA+B,GAAG,MAAM,GAAG,IAAI,CAKpK;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,GAAG,SAAS,CAQjE;AAiCD;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,WAAW,EAAE,IAAI,CAAC,mBAAmB,EAAE,UAAU,GAAG,UAAU,GAAG,QAAQ,CAAC,EAAE,IAAI,EAAE,MAAM,GAAG;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;CAAE,GAAG,IAAI,CAI/K;AAMD,qMAAqM;AACrM,MAAM,MAAM,iBAAiB,GAAG,OAAO,CAAC,UAAU,EAAE,WAAW,CAAC,CAAC;AAIjE;;;;;GAKG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,SAAS,EA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GAAG,iBAAiB,GAAG,UAAU,CAAC,CAAC,EAC/N,OAAO,EAAE,IAAI,CAAC,SAAS,EAAE,QAAQ,GAAG,MAAM,GAAG,MAAM,GAAG,iBAAiB,GAAG,aAAa,GAAG,aAAa,GAAG,QAAQ,CAAC,EACnH,OAAO,GAAE,kBAAuB,GAC/B,WAAW,CA8Cb"}
|
package/dist/ai/request.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { DEFAULT_AZURE_API_VERSION } from "./config.js";
|
|
1
|
+
import { DEFAULT_AZURE_API_VERSION, normalizeThinking } from "./config.js";
|
|
2
2
|
import { forcedToolChoice } from "./structured.js";
|
|
3
3
|
import { completionTokenLimit, isReasoningModel, requestOutputLimit } from "./token-limit.js";
|
|
4
4
|
import { AiProviderError } from "./types.js";
|
|
@@ -40,8 +40,14 @@ export function endpointOrigin(value) {
|
|
|
40
40
|
function azureV1(integration) {
|
|
41
41
|
return integration.provider === "azure_openai" && /\/openai\/v1$/i.test(trimBase(integration.endpoint));
|
|
42
42
|
}
|
|
43
|
-
|
|
43
|
+
/** How an integration authenticates: its key, in the header its provider expects (none for an endpoint that needs no key). */
|
|
44
|
+
function authHeaders(integration) {
|
|
44
45
|
const key = (integration.apiKey ?? "").trim();
|
|
46
|
+
if (!key)
|
|
47
|
+
return {};
|
|
48
|
+
return integration.provider === "azure_openai" ? { "api-key": key } : { authorization: `Bearer ${key}` };
|
|
49
|
+
}
|
|
50
|
+
function requestTarget(integration, model, githubModelsBaseUrl) {
|
|
45
51
|
const base = effectiveAiBase(integration, githubModelsBaseUrl);
|
|
46
52
|
if (integration.provider === "azure_openai") {
|
|
47
53
|
if (!base)
|
|
@@ -49,28 +55,99 @@ function requestTarget(integration, model, githubModelsBaseUrl) {
|
|
|
49
55
|
const url = azureV1(integration)
|
|
50
56
|
? `${base}/chat/completions`
|
|
51
57
|
: `${base}/openai/deployments/${encodeURIComponent(model)}/chat/completions?api-version=${encodeURIComponent(integration.apiVersion || DEFAULT_AZURE_API_VERSION)}`;
|
|
52
|
-
return { url, headers:
|
|
58
|
+
return { url, headers: authHeaders(integration) };
|
|
53
59
|
}
|
|
54
60
|
if (base && (integration.provider === "github_models" || integration.provider === "openai")) {
|
|
55
|
-
return { url: `${base}/chat/completions`, headers:
|
|
61
|
+
return { url: `${base}/chat/completions`, headers: authHeaders(integration) };
|
|
56
62
|
}
|
|
57
63
|
throw new AiProviderError(`Provider ${integration.provider} cannot run chat tasks outside Microsoft 365.`);
|
|
58
64
|
}
|
|
65
|
+
/**
|
|
66
|
+
* Where a request for the models of an OpenAI-compatible integration goes (`path` is `/models`, `/models/details`, `/modalities`), and how it
|
|
67
|
+
* authenticates: the very base that its chat requests use (`effectiveAiBase`: the endpoint it was given, or OpenAI's own) and the same key header.
|
|
68
|
+
* `null` for an integration that has no such list to read: Azure OpenAI (a deployment is created by hand, nobody lists them), GitHub Models (its
|
|
69
|
+
* catalog is not at this address) and Microsoft 365.
|
|
70
|
+
*/
|
|
71
|
+
export function catalogTarget(integration, path) {
|
|
72
|
+
if (integration.provider !== "openai")
|
|
73
|
+
return null;
|
|
74
|
+
const base = effectiveAiBase(integration);
|
|
75
|
+
return base ? { url: `${base}${path}`, headers: authHeaders(integration) } : null;
|
|
76
|
+
}
|
|
59
77
|
function jsonInstruction(schema) {
|
|
60
78
|
return `Reply with ONLY one JSON object, without code fences or commentary, matching this JSON schema (${schema.name}: ${schema.description}):\n${JSON.stringify(schema.parameters)}`;
|
|
61
79
|
}
|
|
80
|
+
const CHAT_TEMPLATE_PARAMETER = "chat_template_kwargs.reasoning_strength";
|
|
81
|
+
/**
|
|
82
|
+
* Puts a thinking level into the body of a chat request the way the model's thinking parameter says; it is the one place where a level lands in a body.
|
|
83
|
+
* `reasoning_effort` (and absent, and any word the engine does not know) is the OpenAI field. `chat_template_kwargs.reasoning_strength` is the argument of
|
|
84
|
+
* the chat template, `chat_template_kwargs: { reasoning_strength: <level> }`, merged with the arguments the body already has (the object that was there is
|
|
85
|
+
* not changed, and a value that is not a set of arguments is replaced); `reasoning_effort` is then not sent. `none` puts nothing in the body.
|
|
86
|
+
*/
|
|
87
|
+
export function applyReasoningLevel(body, parameter, level) {
|
|
88
|
+
if (parameter === "none")
|
|
89
|
+
return;
|
|
90
|
+
if (parameter === CHAT_TEMPLATE_PARAMETER) {
|
|
91
|
+
const existing = body.chat_template_kwargs;
|
|
92
|
+
body.chat_template_kwargs = { ...(typeof existing === "object" && existing !== null && !Array.isArray(existing) ? existing : {}), reasoning_strength: level };
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
body.reasoning_effort = level;
|
|
96
|
+
}
|
|
97
|
+
/** Narrow on purpose: Muse Glimmer by the names that its sources use, with an optional `meta-` before, and nothing that merely contains the words. */
|
|
98
|
+
const MUSE_GLIMMER = /^(?:meta[-_ ]?)?muse[-_ ]?glimmer(?![a-z])/i;
|
|
99
|
+
/**
|
|
100
|
+
* A hint for a screen, never an order: the thinking parameter that a documented source gives the model with this name, or `undefined` for every other
|
|
101
|
+
* name. Today that is Muse Glimmer, and nothing else: no other family is guessed (Muse Spark included: no source says it). For Muse Glimmer it is
|
|
102
|
+
* `chat_template_kwargs.reasoning_strength`, because
|
|
103
|
+
* - Meta, "Run inference" (https://dev.meta.ai/docs/muse-glimmer/deploy), for llama.cpp: «`reasoning_effort`, the OpenAI spelling, is not implemented by
|
|
104
|
+
* llama.cpp. Use `chat_template_kwargs.reasoning_strength`», per request or for the whole server; and, for ExecuTorch, `reasoning_effort` is rejected with a 400;
|
|
105
|
+
* - Meta, "Prompting guide" (https://dev.meta.ai/docs/muse-glimmer/prompting#reasoning): the level is the `reasoning_strength` template argument, `xhigh`,
|
|
106
|
+
* `high`, `medium` or `low`;
|
|
107
|
+
* - LlmProxy, `docs/model-profiles.md` ("Reasoning controls differ per model") and `src/LlmProxy.NodeAgent/ModelFamilyProfiles.cs` (the Muse Glimmer
|
|
108
|
+
* profile, checked on 2026-11-13).
|
|
109
|
+
* The names are those that these sources use, with or without the part before the last `/` (`meta-models/`, `unsloth/`, `RadixArk/`) and a tag after the
|
|
110
|
+
* name: `muse-glimmer`, `muse-glimmer-30B`, `Muse-Glimmer-30B-GGUF:Q4_K_M`, `muse-glimmer:30b-mlx` and, as LlmProxy's logs name one, `meta-muse-glimmer`.
|
|
111
|
+
*
|
|
112
|
+
* The engine does not apply the hint: a model is called with `reasoning_effort` until its author sets `reasoningParameter`. `underlyingModel` is the model
|
|
113
|
+
* that a deployment really is, when it is declared: it is what is read then (as `isReasoningModel` does for an Azure deployment), else the name. Pure, and
|
|
114
|
+
* cheap for any text.
|
|
115
|
+
*/
|
|
116
|
+
export function suggestedReasoningParameter(modelName, underlyingModel) {
|
|
117
|
+
const declared = typeof underlyingModel === "string" ? underlyingModel.trim() : "";
|
|
118
|
+
const identity = declared || (typeof modelName === "string" ? modelName.trim() : "");
|
|
119
|
+
return MUSE_GLIMMER.test(identity.slice(identity.lastIndexOf("/") + 1)) ? CHAT_TEMPLATE_PARAMETER : undefined;
|
|
120
|
+
}
|
|
121
|
+
/** The level that is asked for: the candidate's own (resolved from its route target, else its integration), else its integration's. */
|
|
122
|
+
function requestedEffort(candidate) {
|
|
123
|
+
const level = normalizeThinking(candidate.thinking) ?? normalizeThinking(candidate.integration.thinking);
|
|
124
|
+
return level && level !== "automatic" ? level : undefined;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* The level a request carries: the one that is asked for, unless the model declares that it does not reason (`supportsReasoning: false`), which
|
|
128
|
+
* is a refusal waiting to happen, or its author says that no level is sent to it (`reasoningParameter: "none"`). `automatic`, a level the engine does
|
|
129
|
+
* not know and no level at all send nothing: `automatic` is a choice, so it is not overridden by the integration's level.
|
|
130
|
+
*/
|
|
131
|
+
export function reasoningEffortOf(candidate) {
|
|
132
|
+
return candidate.supportsReasoning === false || candidate.reasoningParameter === "none" ? undefined : requestedEffort(candidate);
|
|
133
|
+
}
|
|
62
134
|
/**
|
|
63
135
|
* The only place a chat request is built: URL, headers, body, streaming and tool use for every provider and model
|
|
64
136
|
* family, so that every host (browser and server) sends the same request for the same book and configuration.
|
|
65
|
-
* Streaming is the integration's choice (`streamMode`, `auto` unless it says `off`) and not the request's
|
|
66
|
-
* streams too, as the arguments of its forced function, so that a slow
|
|
137
|
+
* Streaming is the integration's choice (`streamMode`, `auto` unless it says `off`) and not the request's, and a model that declares it does not
|
|
138
|
+
* stream (`supportsStreaming: false`) is not asked to: a structured reply streams too, as the arguments of its forced function, so that a slow
|
|
139
|
+
* model is never a silent connection.
|
|
67
140
|
* Pure: it calls nothing and changes nothing. Throws only when the integration cannot be called at all.
|
|
68
141
|
*/
|
|
69
142
|
export function buildChatRequest(candidate, request, options = {}) {
|
|
70
143
|
const { integration, model } = candidate;
|
|
71
144
|
const target = requestTarget(integration, model, options.githubModelsBaseUrl ?? GITHUB_MODELS_BASE_URL);
|
|
72
145
|
const useTools = Boolean(request.json) && candidate.supportsToolCalls !== false;
|
|
73
|
-
|
|
146
|
+
// What the integration turns off a model does not turn on; what a model declares it cannot do is not asked of it.
|
|
147
|
+
const stream = integration.streamMode !== "off" && candidate.supportsStreaming !== false;
|
|
148
|
+
const notes = [];
|
|
149
|
+
if (integration.streamMode !== "off" && !stream)
|
|
150
|
+
notes.push(`Streaming was not used: "${model}" is declared as not streaming (supportsStreaming is off).`);
|
|
74
151
|
const system = request.json && !useTools ? `${request.system}\n\n${jsonInstruction(request.json)}` : request.system;
|
|
75
152
|
const outputLimit = requestOutputLimit(request, candidate);
|
|
76
153
|
const body = {
|
|
@@ -83,6 +160,15 @@ export function buildChatRequest(candidate, request, options = {}) {
|
|
|
83
160
|
};
|
|
84
161
|
if (request.temperature !== undefined && !isReasoningModel(integration.provider, model, candidate.underlyingModel))
|
|
85
162
|
body.temperature = request.temperature;
|
|
163
|
+
const thinking = reasoningEffortOf(candidate);
|
|
164
|
+
if (thinking)
|
|
165
|
+
applyReasoningLevel(body, candidate.reasoningParameter, thinking);
|
|
166
|
+
const asked = requestedEffort(candidate);
|
|
167
|
+
if (asked && !thinking) {
|
|
168
|
+
notes.push(candidate.reasoningParameter === "none"
|
|
169
|
+
? `The thinking level "${asked}" was not sent: "${model}" has its thinking parameter set to "none".`
|
|
170
|
+
: `The thinking level "${asked}" was not sent: "${model}" is declared as not reasoning (supportsReasoning is off).`);
|
|
171
|
+
}
|
|
86
172
|
if (useTools && request.json) {
|
|
87
173
|
body.tools = [{ type: "function", function: request.json }];
|
|
88
174
|
body.tool_choice = forcedToolChoice(integration.provider, integration.apiVersion, request.json.name);
|
|
@@ -98,6 +184,9 @@ export function buildChatRequest(candidate, request, options = {}) {
|
|
|
98
184
|
stream,
|
|
99
185
|
useTools,
|
|
100
186
|
...(outputLimit ? { outputLimit } : {}),
|
|
187
|
+
...(thinking ? { thinking } : {}),
|
|
188
|
+
...(thinking && candidate.reasoningParameter === CHAT_TEMPLATE_PARAMETER ? { reasoningParameter: CHAT_TEMPLATE_PARAMETER } : {}),
|
|
189
|
+
...(notes.length ? { notes } : {}),
|
|
101
190
|
};
|
|
102
191
|
}
|
|
103
192
|
//# sourceMappingURL=request.js.map
|
package/dist/ai/request.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request.js","sourceRoot":"","sources":["../../src/ai/request.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,yBAAyB,
|
|
1
|
+
{"version":3,"file":"request.js","sourceRoot":"","sources":["../../src/ai/request.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,yBAAyB,EAAE,iBAAiB,EAAwE,MAAM,aAAa,CAAC;AAEjJ,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AACnD,OAAO,EAAE,oBAAoB,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAC9F,OAAO,EAAE,eAAe,EAAqC,MAAM,YAAY,CAAC;AAEhF,MAAM,CAAC,MAAM,sBAAsB,GAAG,0BAA0B,CAAC;AACjE,MAAM,eAAe,GAA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M;QACN,QAAQ;QACR,GAAG,CAAC,WAAW,CAAC,CAAC,CAAC,EAAE,WAAW,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QACvC,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QACjC,GAAG,CAAC,QAAQ,IAAI,SAAS,CAAC,kBAAkB,KAAK,uBAAuB,CAAC,CAAC,CAAC,EAAE,kBAAkB,EAAE,uBAAuB,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QAChI,GAAG,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KACnC,CAAC;AACJ,CAAC"}
|
package/dist/ai/router.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type AiChatModelConfig, type AiIntegrationConfig, type AiPricingConfig, type AiReasoningParameter, type AiRuntimeConfig, type AiThinking } from "./config.js";
|
|
2
2
|
export interface AiCandidate {
|
|
3
3
|
integration: AiIntegrationConfig;
|
|
4
4
|
model: string;
|
|
@@ -7,6 +7,17 @@ export interface AiCandidate {
|
|
|
7
7
|
maxOutputTokens?: number;
|
|
8
8
|
underlyingModel?: string;
|
|
9
9
|
supportsToolCalls?: boolean;
|
|
10
|
+
/** What the model's configuration declares (see `AiChatModelConfig`); absent when it does not say. */
|
|
11
|
+
supportsStreaming?: boolean;
|
|
12
|
+
supportsReasoning?: boolean;
|
|
13
|
+
supportsVision?: boolean;
|
|
14
|
+
/** How the model's thinking level is sent (see `AiChatModelConfig.reasoningParameter`); absent when the model does not say: `reasoning_effort`. */
|
|
15
|
+
reasoningParameter?: AiReasoningParameter;
|
|
16
|
+
/**
|
|
17
|
+
* The thinking level for this candidate: its route target's, else its integration's (`automatic` is a choice that sends nothing). Absent when
|
|
18
|
+
* neither says: the request carries no level.
|
|
19
|
+
*/
|
|
20
|
+
thinking?: AiThinking;
|
|
10
21
|
}
|
|
11
22
|
/** Chat models of an integration, tolerant of the legacy single-model fields. */
|
|
12
23
|
export declare function integrationChatModels(integration: AiIntegrationConfig): AiChatModelConfig[];
|
package/dist/ai/router.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"router.d.ts","sourceRoot":"","sources":["../../src/ai/router.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"router.d.ts","sourceRoot":"","sources":["../../src/ai/router.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkD,KAAK,iBAAiB,EAAE,KAAK,mBAAmB,EAAE,KAAK,eAAe,EAAE,KAAK,oBAAoB,EAAsB,KAAK,eAAe,EAAE,KAAK,UAAU,EAAE,MAAM,aAAa,CAAC;AAE3O,MAAM,WAAW,WAAW;IAC1B,WAAW,EAAE,mBAAmB,CAAC;IACjC,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,CAAC,EAAE,eAAe,CAAC;IAC1B,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAC5B,sGAAsG;IACtG,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAC5B,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAC5B,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,mJAAmJ;IACnJ,kBAAkB,CAAC,EAAE,oBAAoB,CAAC;IAC1C;;;OAGG;IACH,QAAQ,CAAC,EAAE,UAAU,CAAC;CACvB;AAED,iFAAiF;AACjF,wBAAgB,qBAAqB,CAAC,WAAW,EAAE,mBAAmB,GAAG,iBAAiB,EAAE,CAM3F;AA8CD;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,GAAG,WAAW,EAAE,CAiC9F;AAED,wBAAgB,eAAe,CAAC,MAAM,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CAEpF"}
|
package/dist/ai/router.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { normalizeReasoningParameter, normalizeThinking } from "./config.js";
|
|
1
2
|
/** Chat models of an integration, tolerant of the legacy single-model fields. */
|
|
2
3
|
export function integrationChatModels(integration) {
|
|
3
4
|
if (integration.chatModels?.length)
|
|
@@ -12,7 +13,9 @@ export function integrationChatModels(integration) {
|
|
|
12
13
|
function supports(model, capability) {
|
|
13
14
|
return model.capabilities.includes(capability) || model.capabilities.includes("default");
|
|
14
15
|
}
|
|
15
|
-
function candidate(integration, model) {
|
|
16
|
+
function candidate(integration, model, target) {
|
|
17
|
+
const thinking = normalizeThinking(target?.thinking) ?? normalizeThinking(integration.thinking);
|
|
18
|
+
const reasoningParameter = normalizeReasoningParameter(model.reasoningParameter);
|
|
16
19
|
return {
|
|
17
20
|
integration,
|
|
18
21
|
model: model.name,
|
|
@@ -21,6 +24,11 @@ function candidate(integration, model) {
|
|
|
21
24
|
maxOutputTokens: model.maxOutputTokens,
|
|
22
25
|
underlyingModel: model.underlyingModel,
|
|
23
26
|
supportsToolCalls: model.supportsToolCalls,
|
|
27
|
+
...(typeof model.supportsStreaming === "boolean" ? { supportsStreaming: model.supportsStreaming } : {}),
|
|
28
|
+
...(typeof model.supportsReasoning === "boolean" ? { supportsReasoning: model.supportsReasoning } : {}),
|
|
29
|
+
...(typeof model.supportsVision === "boolean" ? { supportsVision: model.supportsVision } : {}),
|
|
30
|
+
...(reasoningParameter ? { reasoningParameter } : {}),
|
|
31
|
+
...(thinking ? { thinking } : {}),
|
|
24
32
|
};
|
|
25
33
|
}
|
|
26
34
|
function fromTarget(config, target, capability) {
|
|
@@ -30,7 +38,7 @@ function fromTarget(config, target, capability) {
|
|
|
30
38
|
const model = integrationChatModels(integration).find((entry) => entry.name === target.model.trim());
|
|
31
39
|
if (!model || !supports(model, capability))
|
|
32
40
|
return null;
|
|
33
|
-
return candidate(integration, model);
|
|
41
|
+
return candidate(integration, model, target);
|
|
34
42
|
}
|
|
35
43
|
function writingIntegration(config) {
|
|
36
44
|
const integrations = config.aiIntegrations;
|
package/dist/ai/router.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"router.js","sourceRoot":"","sources":["../../src/ai/router.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"router.js","sourceRoot":"","sources":["../../src/ai/router.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,2BAA2B,EAAE,iBAAiB,EAAgK,MAAM,aAAa,CAAC;AAuB3O,iFAAiF;AACjF,MAAM,UAAU,qBAAqB,CAAC,WAAgC;IACpE,IAAI,WAAW,CAAC,UAAU,EAAE,MAAM;QAAE,OAAO,WAAW,CAAC,UAAU,CAAC;IAClE,MAAM,MAAM,GAAwB,EAAE,CAAC;IACvC,IAAI,WAAW,CAAC,YAAY;QAAE,MAAM,CAAC,IAAI,CAAC,EAAE,EAAE,EAAE,gBAAgB,EAAE,IAAI,EAAE,WAAW,CAAC,YAAY,EAAE,YAAY,EAAE,CAAC,SAAS,EAAE,SAAS,CAAC,EAAE,CAAC,CAAC;IAC1I,IAAI,WAAW,CAAC,WAAW,IAAI,WAAW,CAAC,WAAW,KAAK,WAAW,CAAC,YAAY;QAAE,MAAM,CAAC,IAAI,CAAC,EAAE,EAAE,EAAE,eAAe,EAAE,IAAI,EAAE,WAAW,CAAC,WAAW,EAAE,YAAY,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;IACnL,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,SAAS,QAAQ,CAAC,KAAwB,EAAE,UAAkB;IAC5D,OAAO,KAAK,CAAC,YAAY,CAAC,QAAQ,CAAC,UAAU,CAAC,IAAI,KAAK,CAAC,YAAY,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC;AAC3F,CAAC;AAED,SAAS,SAAS,CAAC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KAAK,GAAG,IAAI,CAAC,WAAW,EAAE,UAAU,CAAC,CAAC;QAC5C,IAAI,KAAK;YAAE,IAAI,CAAC,SAAS,CAAC,WAAW,EAAE,KAAK,CAAC,CAAC,CAAC;IACjD,CAAC;IACD,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC;AAChC,CAAC;AAED,MAAM,UAAU,eAAe,CAAC,MAAuB,EAAE,UAAkB;IACzE,OAAO,mBAAmB,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC;AAC5D,CAAC"}
|
package/dist/ai/routing.d.ts
CHANGED
|
@@ -28,20 +28,31 @@ export interface PlannedAttempt {
|
|
|
28
28
|
inputTokens: number;
|
|
29
29
|
estimate: AttemptEstimate;
|
|
30
30
|
}
|
|
31
|
+
/** A candidate that will not be asked, and why: with "Tools only" on, a structured request skips a model marked as without tool calls. */
|
|
32
|
+
export interface SkippedAttempt {
|
|
33
|
+
candidate: AiCandidate;
|
|
34
|
+
/** Position in the route's order: 0 is the primary. */
|
|
35
|
+
index: number;
|
|
36
|
+
reason: "no-tool-calls";
|
|
37
|
+
}
|
|
31
38
|
export interface AttemptPlan {
|
|
32
39
|
attempts: PlannedAttempt[];
|
|
33
|
-
/** The first candidate of the route: the boundary content is meant to stay in. */
|
|
40
|
+
/** The first candidate of the route: the boundary content is meant to stay in (also when "Tools only" skips it). */
|
|
34
41
|
primary: AiCandidate;
|
|
35
42
|
limits: RoutingLimits;
|
|
36
43
|
/** Candidates left out because `sameBoundaryOnly` keeps content inside the primary's account. */
|
|
37
44
|
excludedByBoundary: number;
|
|
45
|
+
/** Candidates left out because "Tools only" is on and they are marked as without tool calls; empty for a request without a function. */
|
|
46
|
+
skipped: SkippedAttempt[];
|
|
38
47
|
}
|
|
39
48
|
export declare function routingLimits(config: AiRuntimeConfig): RoutingLimits;
|
|
40
49
|
/**
|
|
41
50
|
* The candidates a call will try, in order, with the request each would get: the route's order, minus the fallbacks the
|
|
42
|
-
* privacy policy excludes, minus
|
|
43
|
-
*
|
|
44
|
-
*
|
|
51
|
+
* privacy policy excludes, minus (for a structured request, with "Tools only" on, which is the default) the models marked as without
|
|
52
|
+
* tool calls, minus the candidates whose context window cannot hold the prompt (a prompt is never cut),
|
|
53
|
+
* kept while the estimated attempts stay within the token and cost limits. A prompt no window holds, a first attempt
|
|
54
|
+
* that alone exceeds the limits, or a route where no model can call a function, is refused before anything is sent.
|
|
55
|
+
* The privacy policy is applied to the whole route first, so a primary that is skipped is still the account content is meant to stay in.
|
|
45
56
|
*/
|
|
46
57
|
export declare function planAttempts(config: AiRuntimeConfig, request: AiRequest, githubModelsBaseUrl?: string): AttemptPlan;
|
|
47
58
|
//# sourceMappingURL=routing.d.ts.map
|
package/dist/ai/routing.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"routing.d.ts","sourceRoot":"","sources":["../../src/ai/routing.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"routing.d.ts","sourceRoot":"","sources":["../../src/ai/routing.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAEnD,OAAO,EAAoB,KAAK,WAAW,EAAE,MAAM,cAAc,CAAC;AAClE,OAAO,EAAuB,KAAK,WAAW,EAAE,MAAM,aAAa,CAAC;AACpE,OAAO,EAAyB,eAAe,EAAkE,KAAK,SAAS,EAAE,MAAM,YAAY,CAAC;AAEpJ,gIAAgI;AAChI,wBAAgB,iBAAiB,CAAC,SAAS,EAAE,IAAI,CAAC,WAAW,EAAE,aAAa,CAAC,GAAG,MAAM,CAErF;AAED,qFAAqF;AACrF,wBAAgB,uBAAuB,CAAC,CAAC,SAAS,IAAI,CAAC,WAAW,EAAE,aAAa,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,eAAe,EAAE,oBAAoB,CA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-1,7 +1,8 @@
|
|
|
1
1
|
import { likelyMessageTokens, likelyTextTokens, resolveModelTokenBudgets } from "./budget.js";
|
|
2
|
+
import { redactSecrets } from "./redact.js";
|
|
2
3
|
import { buildChatRequest } from "./request.js";
|
|
3
4
|
import { resolveAiCandidates } from "./router.js";
|
|
4
|
-
import { AiPromptTooLargeError, AiProviderError, AiRoutingBudgetError, AiUnavailableError } from "./types.js";
|
|
5
|
+
import { AiPromptTooLargeError, AiProviderError, AiRoutingBudgetError, AiToolsRequiredError, AiUnavailableError } from "./types.js";
|
|
5
6
|
/** What a fallback's privacy boundary is: the integration (one provider account). Same as the web app's `candidateBoundary`. */
|
|
6
7
|
export function candidateBoundary(candidate) {
|
|
7
8
|
return `integration:${candidate.integration.id}`;
|
|
@@ -26,16 +27,30 @@ function estimateAttempt(candidate, inputTokens, outputLimit) {
|
|
|
26
27
|
}
|
|
27
28
|
/**
|
|
28
29
|
* The candidates a call will try, in order, with the request each would get: the route's order, minus the fallbacks the
|
|
29
|
-
* privacy policy excludes, minus
|
|
30
|
-
*
|
|
31
|
-
*
|
|
30
|
+
* privacy policy excludes, minus (for a structured request, with "Tools only" on, which is the default) the models marked as without
|
|
31
|
+
* tool calls, minus the candidates whose context window cannot hold the prompt (a prompt is never cut),
|
|
32
|
+
* kept while the estimated attempts stay within the token and cost limits. A prompt no window holds, a first attempt
|
|
33
|
+
* that alone exceeds the limits, or a route where no model can call a function, is refused before anything is sent.
|
|
34
|
+
* The privacy policy is applied to the whole route first, so a primary that is skipped is still the account content is meant to stay in.
|
|
32
35
|
*/
|
|
33
36
|
export function planAttempts(config, request, githubModelsBaseUrl) {
|
|
34
37
|
const resolved = resolveAiCandidates(config, request.capability);
|
|
35
38
|
if (!resolved.length)
|
|
36
39
|
throw new AiUnavailableError(request.capability);
|
|
37
40
|
const limits = routingLimits(config);
|
|
38
|
-
const
|
|
41
|
+
const inBoundary = applySameBoundaryPolicy(config, resolved);
|
|
42
|
+
const toolsOnly = config.toolsOnly !== false && Boolean(request.json);
|
|
43
|
+
const skipped = [];
|
|
44
|
+
const allowed = inBoundary.filter((candidate) => {
|
|
45
|
+
if (!toolsOnly || candidate.supportsToolCalls !== false)
|
|
46
|
+
return true;
|
|
47
|
+
skipped.push({ candidate, index: resolved.indexOf(candidate), reason: "no-tool-calls" });
|
|
48
|
+
return false;
|
|
49
|
+
});
|
|
50
|
+
if (!allowed.length) {
|
|
51
|
+
const secrets = config.aiIntegrations.map((integration) => integration.apiKey);
|
|
52
|
+
throw new AiToolsRequiredError(redactSecrets(toolsRequiredMessage(skipped), secrets), skipped.map((entry) => redactSecrets(entry.candidate.model, secrets)));
|
|
53
|
+
}
|
|
39
54
|
const planned = allowed.map((candidate) => {
|
|
40
55
|
const index = resolved.indexOf(candidate);
|
|
41
56
|
try {
|
|
@@ -78,6 +93,10 @@ export function planAttempts(config, request, githubModelsBaseUrl) {
|
|
|
78
93
|
tokens += attempt.estimate.tokens;
|
|
79
94
|
cost += attempt.estimate.cost;
|
|
80
95
|
}
|
|
81
|
-
return { attempts, primary: resolved[0], limits, excludedByBoundary: resolved.length -
|
|
96
|
+
return { attempts, primary: resolved[0], limits, excludedByBoundary: resolved.length - inBoundary.length, skipped };
|
|
97
|
+
}
|
|
98
|
+
function toolsRequiredMessage(skipped) {
|
|
99
|
+
const models = skipped.map(({ candidate }) => `"${candidate.model}" in "${candidate.integration.name}"`).join(", ");
|
|
100
|
+
return `"Tools only" is on, and every model this task can use is marked as without tool calls (${models}), so a structured answer has nowhere to go. Turn on "Forced tool calls" for the model in AI Integrations, or turn off "Tools only" in the AI Router. Nothing was sent.`;
|
|
82
101
|
}
|
|
83
102
|
//# sourceMappingURL=routing.js.map
|