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.
Files changed (78) hide show
  1. package/README.md +185 -13
  2. package/dist/ai/body.d.ts +20 -0
  3. package/dist/ai/body.d.ts.map +1 -0
  4. package/dist/ai/body.js +112 -0
  5. package/dist/ai/body.js.map +1 -0
  6. package/dist/ai/catalog-fetch.d.ts +51 -0
  7. package/dist/ai/catalog-fetch.d.ts.map +1 -0
  8. package/dist/ai/catalog-fetch.js +260 -0
  9. package/dist/ai/catalog-fetch.js.map +1 -0
  10. package/dist/ai/catalog-support.d.ts +13 -0
  11. package/dist/ai/catalog-support.d.ts.map +1 -0
  12. package/dist/ai/catalog-support.js +28 -0
  13. package/dist/ai/catalog-support.js.map +1 -0
  14. package/dist/ai/catalog.d.ts +148 -0
  15. package/dist/ai/catalog.d.ts.map +1 -0
  16. package/dist/ai/catalog.js +367 -0
  17. package/dist/ai/catalog.js.map +1 -0
  18. package/dist/ai/config.d.ts +83 -7
  19. package/dist/ai/config.d.ts.map +1 -1
  20. package/dist/ai/config.js +83 -11
  21. package/dist/ai/config.js.map +1 -1
  22. package/dist/ai/fetch-client.d.ts +20 -0
  23. package/dist/ai/fetch-client.d.ts.map +1 -1
  24. package/dist/ai/fetch-client.js +49 -24
  25. package/dist/ai/fetch-client.js.map +1 -1
  26. package/dist/ai/probe.d.ts +40 -0
  27. package/dist/ai/probe.d.ts.map +1 -0
  28. package/dist/ai/probe.js +110 -0
  29. package/dist/ai/probe.js.map +1 -0
  30. package/dist/ai/request.d.ts +58 -5
  31. package/dist/ai/request.d.ts.map +1 -1
  32. package/dist/ai/request.js +96 -7
  33. package/dist/ai/request.js.map +1 -1
  34. package/dist/ai/router.d.ts +12 -1
  35. package/dist/ai/router.d.ts.map +1 -1
  36. package/dist/ai/router.js +10 -2
  37. package/dist/ai/router.js.map +1 -1
  38. package/dist/ai/routing.d.ts +15 -4
  39. package/dist/ai/routing.d.ts.map +1 -1
  40. package/dist/ai/routing.js +25 -6
  41. package/dist/ai/routing.js.map +1 -1
  42. package/dist/ai/token-limit.d.ts +4 -4
  43. package/dist/ai/token-limit.d.ts.map +1 -1
  44. package/dist/ai/token-limit.js +5 -4
  45. package/dist/ai/token-limit.js.map +1 -1
  46. package/dist/ai/types.d.ts +12 -1
  47. package/dist/ai/types.d.ts.map +1 -1
  48. package/dist/ai/types.js +12 -0
  49. package/dist/ai/types.js.map +1 -1
  50. package/dist/autopilot/errors.d.ts +4 -3
  51. package/dist/autopilot/errors.d.ts.map +1 -1
  52. package/dist/autopilot/errors.js +21 -5
  53. package/dist/autopilot/errors.js.map +1 -1
  54. package/dist/autopilot/runner.d.ts.map +1 -1
  55. package/dist/autopilot/runner.js +21 -4
  56. package/dist/autopilot/runner.js.map +1 -1
  57. package/dist/autopilot/session.js +1 -1
  58. package/dist/autopilot/session.js.map +1 -1
  59. package/dist/autopilot/state.d.ts +4 -3
  60. package/dist/autopilot/state.d.ts.map +1 -1
  61. package/dist/autopilot/state.js +35 -9
  62. package/dist/autopilot/state.js.map +1 -1
  63. package/dist/autopilot/tasks.js +2 -2
  64. package/dist/autopilot/tasks.js.map +1 -1
  65. package/dist/autopilot/types.d.ts +27 -5
  66. package/dist/autopilot/types.d.ts.map +1 -1
  67. package/dist/autopilot/types.js +1 -1
  68. package/dist/autopilot/types.js.map +1 -1
  69. package/dist/index.d.ts +3 -0
  70. package/dist/index.d.ts.map +1 -1
  71. package/dist/index.js +3 -0
  72. package/dist/index.js.map +1 -1
  73. package/dist/server/protocol.d.ts +6 -1
  74. package/dist/server/protocol.d.ts.map +1 -1
  75. package/dist/server/protocol.js.map +1 -1
  76. package/dist/version.d.ts +1 -1
  77. package/dist/version.js +1 -1
  78. package/package.json +1 -1
@@ -0,0 +1,260 @@
1
+ import { BodyError, discardBody, readBoundedJson } from "./body.js";
2
+ import { AiCatalogError, CATALOG_MAX_MODELS, parseLmStudioModels, parseModalities, parseModelDetails, 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
+ /** The most bytes of an answer that are read. A longer one is refused as it passes the limit, whatever it said it weighed. */
8
+ export const CATALOG_MAX_BYTES = 600_000;
9
+ /** How long an endpoint may be silent, for each request: before it answers, and between two pieces of its answer. */
10
+ export const CATALOG_TIMEOUT_MS = 8_000;
11
+ /** The most a request may take in all, however the endpoint goes on sending: a safety, far above what a catalog needs. */
12
+ export const CATALOG_TOTAL_CAP_MS = 60_000;
13
+ // ─── How a request fails ────────────────────────────────────────────────────
14
+ /** What went wrong with a request, before it is told in words. Internal: never leaves this module. */
15
+ class Fault extends Error {
16
+ reason;
17
+ about;
18
+ constructor(reason, about = {}) {
19
+ super(reason);
20
+ this.reason = reason;
21
+ this.about = about;
22
+ this.name = "Fault";
23
+ }
24
+ }
25
+ /** What went wrong, in words: why, and what to do. In English, and without the key, the address, or a word of what the endpoint sent. */
26
+ function describe(fault, how) {
27
+ const { name } = how;
28
+ const { status, phase, ms, bytes } = fault.about;
29
+ const message = (() => {
30
+ switch (fault.reason) {
31
+ case "unreachable":
32
+ return `Could not reach ${name} to read its models. Check that the server is running and that the address is right (a redirect is refused: use the final address). In a browser, a server that does not allow this site (CORS) looks exactly like a server that is off.`;
33
+ case "timeout":
34
+ return phase === "start"
35
+ ? `${name} did not answer within ${formatDuration(ms ?? how.timeoutMs)}. The server may be busy or off: try again, or check the address.`
36
+ : phase === "idle"
37
+ ? `${name} stopped sending its models for ${formatDuration(ms ?? how.timeoutMs)}. Try again in a moment.`
38
+ : `Reading the models of ${name} took longer than ${formatDuration(ms ?? CATALOG_TOTAL_CAP_MS)} and was stopped. Try again, or enter the model names by hand.`;
39
+ case "unauthorized":
40
+ return status === 403
41
+ ? `${name} denied access to its models (HTTP 403). The key may be wrong or not allowed to list models: check the key of the integration, or enter the model names by hand.`
42
+ : `${name} refused the API key (HTTP ${status ?? 401}). Check the key of the integration.`;
43
+ case "not-found":
44
+ return `${name} has no list of models at its address (HTTP 404). Check that the address ends with the version path (for example /v1), or enter the model names by hand.`;
45
+ case "rate-limited":
46
+ return `${name} is limiting requests (HTTP 429). Wait a moment and try again.`;
47
+ case "server-error":
48
+ return `${name} failed while listing its models (HTTP ${status ?? 500}). It may be busy or restarting: try again in a moment.`;
49
+ case "http-error":
50
+ return `${name} answered HTTP ${status ?? 0} to the request for its models. Check the address and the key, or enter the model names by hand.`;
51
+ case "empty":
52
+ return `${name} lists no models. If it is a gateway, start a model there (a model that is stopped is not listed), or enter the model names by hand.`;
53
+ case "too-large":
54
+ return bytes
55
+ ? `The answer of ${name} is larger than ${CATALOG_MAX_BYTES / 1000} KB and was not read. Enter the model names by hand.`
56
+ : `The list of models of ${name} is too large to be read: it has more than ${CATALOG_MAX_MODELS} models, or it comes in pages. Enter the model names by hand.`;
57
+ case "unsupported":
58
+ return `${name} has no list of models to read.`;
59
+ default:
60
+ return `${name} answered, but what it sent is not a list of models in the OpenAI format. Check that the address is the API base (for example …/v1), or enter the model names by hand.`;
61
+ }
62
+ })();
63
+ return new AiCatalogError(redactSecrets(message, how.secrets), fault.reason, status);
64
+ }
65
+ function statusFault(status) {
66
+ if (status === 401 || status === 403)
67
+ return new Fault("unauthorized", { status });
68
+ if (status === 404)
69
+ return new Fault("not-found", { status });
70
+ if (status === 429)
71
+ return new Fault("rate-limited", { status });
72
+ return new Fault(status >= 500 ? "server-error" : "http-error", { status });
73
+ }
74
+ /** What a failure of the request is: the author's cancel, a deadline, a status, a body that cannot be used, or no answer at all. */
75
+ function interpret(error, clock, how) {
76
+ if (how.signal?.aborted)
77
+ return abortReason(how.signal);
78
+ const expired = clock.expired();
79
+ if (expired)
80
+ return new Fault("timeout", { phase: expired.phase, ms: expired.ms });
81
+ if (error instanceof Fault)
82
+ return error;
83
+ if (error instanceof BodyError)
84
+ return error.fault === "too-large" ? new Fault("too-large", { bytes: true }) : new Fault(error.fault === "lost" ? "unreachable" : "invalid");
85
+ return new Fault("unreachable");
86
+ }
87
+ /** A parser's refusal of an answer is a fault of the answer; whatever else a parser throws is the same. */
88
+ function parsed(read) {
89
+ try {
90
+ return read();
91
+ }
92
+ catch (error) {
93
+ throw new Fault(error instanceof AiCatalogError ? error.reason : "invalid");
94
+ }
95
+ }
96
+ // ─── One request ────────────────────────────────────────────────────────────
97
+ /**
98
+ * One GET: no cookie, no redirect, no cache, no referrer, the key in its header and nowhere else, a start deadline and then a silence between two pieces
99
+ * (and a cap on the whole), an answer that is not OK let go unread, and a body that is read as JSON within `CATALOG_MAX_BYTES`. It throws a `Fault`,
100
+ * or the author's cancel as it is.
101
+ */
102
+ async function readJson(target, how) {
103
+ const clock = startClock(how.signal, { startMs: how.timeoutMs, idleMs: how.timeoutMs, capMs: Math.max(CATALOG_TOTAL_CAP_MS, how.timeoutMs) });
104
+ try {
105
+ const sent = Promise.resolve(how.fetchImpl(target.url, {
106
+ method: "GET",
107
+ headers: { accept: "application/json", ...target.headers },
108
+ credentials: "omit",
109
+ redirect: "error",
110
+ cache: "no-store",
111
+ referrerPolicy: "no-referrer",
112
+ signal: clock.signal,
113
+ }));
114
+ // An answer that comes after the request was given up is dropped, and its body let go.
115
+ sent.then((late) => {
116
+ if (clock.interrupted())
117
+ discardBody(late);
118
+ }, () => undefined);
119
+ const response = await clock.guard(sent);
120
+ if (!response.ok) {
121
+ discardBody(response);
122
+ throw statusFault(response.status);
123
+ }
124
+ // The headers are here: the start deadline is over, and from now on it is the silence between two pieces that is watched.
125
+ clock.streaming();
126
+ return await readBoundedJson(response, clock, CATALOG_MAX_BYTES);
127
+ }
128
+ catch (error) {
129
+ throw interpret(error, clock, how);
130
+ }
131
+ finally {
132
+ clock.dispose();
133
+ }
134
+ }
135
+ function readingOf(integration, options) {
136
+ return {
137
+ name: nameOf(integration),
138
+ secrets: [integration.apiKey],
139
+ fetchImpl: options.fetch ?? fetch,
140
+ signal: options.signal,
141
+ timeoutMs: timeoutOf(options.timeoutMs, CATALOG_TIMEOUT_MS),
142
+ };
143
+ }
144
+ // ─── The catalog of an endpoint ─────────────────────────────────────────────
145
+ /** Fills in what the companions say of a model: field by field, what `over` declares wins; what it leaves out stays as `base` has it. */
146
+ function combine(base, over) {
147
+ const contextWindow = over.contextWindow ?? base.contextWindow;
148
+ const maxOutputTokens = over.maxOutputTokens ?? base.maxOutputTokens;
149
+ const inputModalities = over.inputModalities ?? base.inputModalities;
150
+ const outputModalities = over.outputModalities ?? base.outputModalities;
151
+ return {
152
+ id: base.id,
153
+ type: over.type !== "unknown" ? over.type : base.type,
154
+ ...(contextWindow !== undefined ? { contextWindow } : {}),
155
+ ...(maxOutputTokens !== undefined ? { maxOutputTokens } : {}),
156
+ ...(inputModalities ? { inputModalities: [...inputModalities] } : {}),
157
+ ...(outputModalities ? { outputModalities: [...outputModalities] } : {}),
158
+ capabilities: { ...base.capabilities, ...over.capabilities },
159
+ };
160
+ }
161
+ /** The catalog: every id of the list, then what `/modalities` knows of it, then what `/models/details` says (which wins); an id only they know is added, up to the limit. */
162
+ function merge(ids, details, groups) {
163
+ const entries = new Map();
164
+ for (const id of ids)
165
+ entries.set(id, { id, type: "unknown", capabilities: {} });
166
+ const add = (entry) => {
167
+ const have = entries.get(entry.id);
168
+ if (have)
169
+ entries.set(entry.id, combine(have, entry));
170
+ else if (entries.size < CATALOG_MAX_MODELS)
171
+ entries.set(entry.id, combine({ id: entry.id, type: "unknown", capabilities: {} }, entry));
172
+ };
173
+ for (const group of groups ?? [])
174
+ for (const model of group.models)
175
+ add(model);
176
+ for (const model of details ?? [])
177
+ add(model);
178
+ return [...entries.values()].sort((left, right) => (left.id < right.id ? -1 : left.id > right.id ? 1 : 0));
179
+ }
180
+ /**
181
+ * Reads the models an integration's endpoint offers, as BookWriter does and as LlmProxy exposes them: `GET <base>/models` (the OpenAI list: without
182
+ * it there is no catalog, and a failure is an `AiCatalogError` that says why and what to do), then, together, `GET <base>/models/details` (the window,
183
+ * the largest answer and what each model can do) and `GET <base>/modalities` (what the gateway offers, by kind of model, and what runs for each), which
184
+ * an endpoint may not have and whose every failure is silent: the catalog is then what the list says. The base is the one the chat requests use.
185
+ * The models of every kind are returned, with their `type`, so that a screen can fill the tables of the tools that are not chat. Only OpenAI-compatible
186
+ * integrations have such a list: any other is an `AiCatalogError` of reason `unsupported`, and nothing is sent.
187
+ *
188
+ * What an endpoint sends is untrusted: a request is a plain GET with no cookie and no redirect, with the key in its header only; an endpoint may be
189
+ * silent for 8 s (before it answers, and between two pieces), a minute in all; a body is read in pieces up to 600 KB, and let go the moment it is too
190
+ * much, or not JSON; and nothing it says is quoted in an error. The key is in no message and no result. A cancelled reading is an `AbortError`.
191
+ */
192
+ export async function fetchModelCatalog(integration, options = {}) {
193
+ const target = catalogTarget(integration, "/models");
194
+ if (!target)
195
+ throw unsupportedCatalog(integration);
196
+ const how = readingOf(integration, options);
197
+ if (how.signal?.aborted)
198
+ throw abortReason(how.signal);
199
+ let ids;
200
+ try {
201
+ const payload = await readJson(target, how);
202
+ ids = parsed(() => parseModelList(payload));
203
+ }
204
+ catch (error) {
205
+ throw error instanceof Fault ? describe(error, how) : error;
206
+ }
207
+ // The companions are optional: an endpoint that does not have them, or whose answer is not what it should be, leaves the catalog as the list says it.
208
+ const optional = async (path, read) => {
209
+ const asked = catalogTarget(integration, path);
210
+ if (!asked)
211
+ return undefined;
212
+ try {
213
+ return read(await readJson(asked, how));
214
+ }
215
+ catch {
216
+ return undefined;
217
+ }
218
+ };
219
+ const [details, groups] = await Promise.all([optional("/models/details", parseModelDetails), optional("/modalities", parseModalities)]);
220
+ if (how.signal?.aborted)
221
+ throw abortReason(how.signal);
222
+ return {
223
+ models: merge(ids, details, groups),
224
+ source: { models: true, details: details !== undefined, modalities: groups !== undefined },
225
+ ...(groups !== undefined ? { modalities: groups } : {}),
226
+ };
227
+ }
228
+ /**
229
+ * The native list of an LM Studio on this computer (`GET /api/v1/models` at the root of its server): the thinking levels each language model declares
230
+ * and the context it loaded and supports, for `parseLmStudioModels`. Only a server that runs on this machine is asked (`localhost`, `127.0.0.1`, `::1`,
231
+ * with no credentials in its address), and only an OpenAI-compatible integration is read: for any other, and for any server that is not LM Studio,
232
+ * cannot be reached, or answers what is not its list, the answer is `undefined`, and the caller keeps what it knew. A key that is configured is sent, to this
233
+ * computer only. A cancelled reading is an `AbortError`.
234
+ */
235
+ export async function fetchLmStudioModels(integration, options = {}) {
236
+ const headers = catalogTarget(integration, "/models")?.headers;
237
+ if (!headers)
238
+ return undefined;
239
+ let url;
240
+ try {
241
+ url = new URL(integration.endpoint ?? "");
242
+ }
243
+ catch {
244
+ return undefined;
245
+ }
246
+ if ((url.protocol !== "http:" && url.protocol !== "https:") || url.username || url.password || !["localhost", "127.0.0.1", "[::1]"].includes(url.hostname))
247
+ return undefined;
248
+ const how = readingOf(integration, options);
249
+ if (how.signal?.aborted)
250
+ throw abortReason(how.signal);
251
+ try {
252
+ return parseLmStudioModels(await readJson({ url: `${url.origin}/api/v1/models`, headers }, how));
253
+ }
254
+ catch {
255
+ if (how.signal?.aborted)
256
+ throw abortReason(how.signal);
257
+ return undefined;
258
+ }
259
+ }
260
+ //# sourceMappingURL=catalog-fetch.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"catalog-fetch.js","sourceRoot":"","sources":["../../src/ai/catalog-fetch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AACpE,OAAO,EACL,cAAc,EACd,kBAAkB,EAClB,mBAAmB,EACnB,eAAe,EACf,iBAAiB,EACjB,cAAc,GAKf,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,SAAS,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAE1F,OAAO,EAAE,cAAc,EAAE,UAAU,EAAsB,MAAM,gBAAgB,C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OAAO,GAAG,aAAa,CAAC,WAAW,EAAE,SAAS,CAAC,EAAE,OAAO,CAAC;IAC/D,IAAI,CAAC,OAAO;QAAE,OAAO,SAAS,CAAC;IAC/B,IAAI,GAAQ,CAAC;IACb,IAAI,CAAC;QACH,GAAG,GAAG,IAAI,GAAG,CAAC,WAAW,CAAC,QAAQ,IAAI,EAAE,CAAC,CAAC;IAC5C,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,SAAS,CAAC;IACnB,CAAC;IACD,IAAI,CAAC,GAAG,CAAC,QAAQ,KAAK,OAAO,IAAI,GAAG,CAAC,QAAQ,KAAK,QAAQ,CAAC,IAAI,GAAG,CAAC,QAAQ,IAAI,GAAG,CAAC,QAAQ,IAAI,CAAC,CAAC,WAAW,EAAE,WAAW,EAAE,OAAO,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,QAAQ,CAAC;QAAE,OAAO,SAAS,CAAC;IAC7K,MAAM,GAAG,GAAG,SAAS,CAAC,WAAW,EAAE,OAAO,CAAC,CAAC;IAC5C,IAAI,GAAG,CAAC,MAAM,EAAE,OAAO;QAAE,MAAM,WAAW,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IACvD,IAAI,CAAC;QACH,OAAO,mBAAmB,CAAC,MAAM,QAAQ,CAAC,EAAE,GAAG,EAAE,GAAG,GAAG,CAAC,MAAM,gBAAgB,EAAE,OAAO,EAAE,EAAE,GAAG,CAAC,CAAC,CAAC;IACnG,CAAC;IAAC,MAAM,CAAC;QACP,IAAI,GAAG,CAAC,MAAM,EAAE,OAAO;YAAE,MAAM,WAAW,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QACvD,OAAO,SAAS,CAAC;IACnB,CAAC;AACH,CAAC"}
@@ -0,0 +1,13 @@
1
+ import { AiCatalogError } from "./catalog.js";
2
+ import type { AiIntegrationConfig } from "./config.js";
3
+ /** The integration as a message names it, in quotes, with its key (which a careless name may hold) taken out. */
4
+ export declare function nameOf(integration: Pick<AiIntegrationConfig, "name" | "provider" | "apiKey">): string;
5
+ /** What a caller that cancelled is told: its own reason when it gave one, else an `AbortError`. */
6
+ export declare function abortReason(signal: AbortSignal): Error;
7
+ /** The longest a host may ask a request to be silent: ten minutes, far under what a timer can count (past about 24 days it would fire at once). */
8
+ export declare const MAX_REQUEST_TIMEOUT_MS = 600000;
9
+ /** The positive number a host gave as a time, kept within `MAX_REQUEST_TIMEOUT_MS`, or the default. */
10
+ export declare function timeoutOf(value: unknown, fallback: number): number;
11
+ /** An integration that has no list of models to read: it says why, and that its models are entered by hand or picked from the catalog that has them. */
12
+ export declare function unsupportedCatalog(integration: Pick<AiIntegrationConfig, "provider">): AiCatalogError;
13
+ //# sourceMappingURL=catalog-support.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"catalog-support.d.ts","sourceRoot":"","sources":["../../src/ai/catalog-support.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAKvD,iHAAiH;AACjH,wBAAgB,MAAM,CAAC,WAAW,EAAE,IAAI,CAAC,mBAAmB,EAAE,MAAM,GAAG,UAAU,GAAG,QAAQ,CAAC,GAAG,MAAM,CAErG;AAED,mGAAmG;AACnG,wBAAgB,WAAW,CAAC,MAAM,EAAE,WAAW,GAAG,KAAK,CAEtD;AAED,mJAAmJ;AACnJ,eAAO,MAAM,sBAAsB,SAAU,CAAC;AAE9C,uGAAuG;AACvG,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAElE;AAED,wJAAwJ;AACxJ,wBAAgB,kBAAkB,CAAC,WAAW,EAAE,IAAI,CAAC,mBAAmB,EAAE,UAAU,CAAC,GAAG,cAAc,CAOrG"}
@@ -0,0 +1,28 @@
1
+ import { abortError } from "../util/abort.js";
2
+ import { AiCatalogError } from "./catalog.js";
3
+ import { redactSecrets } from "./redact.js";
4
+ // What reading the models of an endpoint and probing it have in common. Internal: not exported by the package.
5
+ /** The integration as a message names it, in quotes, with its key (which a careless name may hold) taken out. */
6
+ export function nameOf(integration) {
7
+ return redactSecrets(`"${integration.name || integration.provider}"`, [integration.apiKey]);
8
+ }
9
+ /** What a caller that cancelled is told: its own reason when it gave one, else an `AbortError`. */
10
+ export function abortReason(signal) {
11
+ return signal.reason instanceof Error ? signal.reason : abortError();
12
+ }
13
+ /** The longest a host may ask a request to be silent: ten minutes, far under what a timer can count (past about 24 days it would fire at once). */
14
+ export const MAX_REQUEST_TIMEOUT_MS = 600_000;
15
+ /** The positive number a host gave as a time, kept within `MAX_REQUEST_TIMEOUT_MS`, or the default. */
16
+ export function timeoutOf(value, fallback) {
17
+ return typeof value === "number" && Number.isFinite(value) && value > 0 ? Math.min(value, MAX_REQUEST_TIMEOUT_MS) : fallback;
18
+ }
19
+ /** An integration that has no list of models to read: it says why, and that its models are entered by hand or picked from the catalog that has them. */
20
+ export function unsupportedCatalog(integration) {
21
+ const message = integration.provider === "azure_openai"
22
+ ? "The models of an Azure OpenAI integration cannot be listed: a deployment is created by hand in Azure, and nobody lists them. Enter the deployment names."
23
+ : integration.provider === "github_models"
24
+ ? "GitHub Models does not list its models at this address: its catalog is read from GitHub's own catalog, not from the endpoint. Pick the models from that catalog."
25
+ : "A Microsoft 365 Copilot integration has no catalog of models to read: it runs only inside Microsoft 365.";
26
+ return new AiCatalogError(message, "unsupported");
27
+ }
28
+ //# sourceMappingURL=catalog-support.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"catalog-support.js","sourceRoot":"","sources":["../../src/ai/catalog-support.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAE9C,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE5C,+GAA+G;AAE/G,iHAAiH;AACjH,MAAM,UAAU,MAAM,CAAC,WAAsE;IAC3F,OAAO,aAAa,CAAC,IAAI,WAAW,CAAC,IAAI,IAAI,WAAW,CAAC,QAAQ,GAAG,EAAE,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC;AAC9F,CAAC;AAED,mGAAmG;AACnG,MAAM,UAAU,WAAW,CAAC,MAAmB;IAC7C,OAAO,MAAM,CAAC,MAAM,YAAY,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,UAAU,EAAE,CAAC;AACvE,CAAC;AAED,mJAAmJ;AACnJ,MAAM,CAAC,MAAM,sBAAsB,GAAG,OAAO,CAAC;AAE9C,uGAAuG;AACvG,MAAM,UAAU,SAAS,CAAC,KAAc,EAAE,QAAgB;IACxD,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,sBAAsB,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC;AAC/H,CAAC;AAED,wJAAwJ;AACxJ,MAAM,UAAU,kBAAkB,CAAC,WAAkD;IACnF,MAAM,OAAO,GAAG,WAAW,CAAC,QAAQ,KAAK,cAAc;QACrD,CAAC,CAAC,0JAA0J;QAC5J,CAAC,CAAC,WAAW,CAAC,QAAQ,KAAK,eAAe;YACxC,CAAC,CAAC,kKAAkK;YACpK,CAAC,CAAC,0GAA0G,CAAC;IACjH,OAAO,IAAI,cAAc,CAAC,OAAO,EAAE,aAAa,CAAC,CAAC;AACpD,CAAC"}
@@ -0,0 +1,148 @@
1
+ import type { AiChatModelConfig, AiThinking } from "./config.js";
2
+ /** The most models a list may hold: a longer one is refused (`GET /models`) or cut here (`/models/details`, `/modalities`). */
3
+ export declare const CATALOG_MAX_MODELS = 2000;
4
+ /** The longest id of a model. */
5
+ export declare const CATALOG_MAX_ID_LENGTH = 256;
6
+ /** A window or an answer that is declared is a whole number of tokens within these limits; anything else is not declared. */
7
+ export declare const CATALOG_MIN_TOKENS = 256;
8
+ export declare const CATALOG_MAX_TOKENS = 10000000;
9
+ /** LM Studio's list: how many models and how long a key are read. */
10
+ export declare const LMSTUDIO_MAX_MODELS = 500;
11
+ export declare const LMSTUDIO_MAX_KEY_LENGTH = 200;
12
+ /**
13
+ * Why the models of an endpoint could not be read. `unreachable`: no answer (the server is off, the address is wrong, a redirect was refused, or, in a
14
+ * browser, CORS blocked the request: the browser cannot tell them apart). `timeout`: it did not answer, or stopped answering, in time. `unauthorized`:
15
+ * 401 or 403, the key. `not-found`: 404, no list at this address. `rate-limited`: 429. `server-error`: 5xx. `http-error`: any other status. `invalid`:
16
+ * the answer is not a list of models. `empty`: it is one, and has no model in it. `too-large`: more models, or more bytes, than may be read.
17
+ * `unsupported`: the integration has no list to read (Azure OpenAI, GitHub Models, Microsoft 365).
18
+ */
19
+ export type AiCatalogErrorReason = "unreachable" | "timeout" | "unauthorized" | "not-found" | "rate-limited" | "server-error" | "http-error" | "invalid" | "empty" | "too-large" | "unsupported";
20
+ /**
21
+ * The models of an endpoint could not be read, or what it sent is not a catalog. The message is in English, says why and what to do, and never quotes
22
+ * what the endpoint sent or the key that was used. `status` is the HTTP status, for the reasons that come from one.
23
+ */
24
+ export declare class AiCatalogError extends Error {
25
+ readonly reason: AiCatalogErrorReason;
26
+ readonly status?: number | undefined;
27
+ constructor(message: string, reason: AiCatalogErrorReason, status?: number | undefined);
28
+ }
29
+ /** The kinds of model a gateway tells apart (LlmProxy: `GET /v1/models/details`, `GET /v1/modalities`). */
30
+ export declare const AI_MODEL_TYPES: readonly ["chat", "image", "speech", "transcription", "embedding", "realtime", "classifier"];
31
+ /** A kind of model, or `unknown` for one that is not said or is not one of these. */
32
+ export type AiModelType = (typeof AI_MODEL_TYPES)[number] | "unknown";
33
+ /** What a gateway declares a model can do. A capability that is not said is absent; `false` is what LlmProxy sends for everything its administrator has not declared. */
34
+ export interface AiModelCapabilities {
35
+ streaming?: boolean;
36
+ tools?: boolean;
37
+ vision?: boolean;
38
+ reasoning?: boolean;
39
+ }
40
+ /** One model of an endpoint, as its catalog says it. Every field but `id`, `type` and `capabilities` is absent when it is not declared: never a 0, never a guess. */
41
+ export interface AiModelCatalogEntry {
42
+ id: string;
43
+ type: AiModelType;
44
+ /** The longest prompt plus answer one request may use (`context_window`), in tokens. */
45
+ contextWindow?: number;
46
+ /** The largest answer to ask for (`max_output_tokens`), in tokens. */
47
+ maxOutputTokens?: number;
48
+ /** What the model accepts and returns: short words such as `text`, `image`, `audio`, `embedding`. */
49
+ inputModalities?: string[];
50
+ outputModalities?: string[];
51
+ capabilities: AiModelCapabilities;
52
+ }
53
+ /** The models of one way of use (a type), with what they offer together: one entry of `GET /v1/modalities`. */
54
+ export interface AiModalityGroup {
55
+ type: AiModelType;
56
+ /** The paths this kind of model is called at (`/v1/chat/completions`, `/v1/images/generations`…). */
57
+ endpoints: string[];
58
+ /** What the running models of the type accept and return together. */
59
+ inputModalities?: string[];
60
+ outputModalities?: string[];
61
+ /** How many models run for it, as the gateway counts them; a type that is offered and has nothing running has 0 and no models. */
62
+ modelCount: number;
63
+ models: AiModelCatalogEntry[];
64
+ }
65
+ /**
66
+ * The ids of an OpenAI `GET /models` list, sorted and without repeats. It is all or nothing, as BookWriter reads it: a payload that is not a list, an
67
+ * entry that is not an object with a good id, a list with nothing in it or with more than `CATALOG_MAX_MODELS` models is an `AiCatalogError`, so that
68
+ * an answer that is wrong never replaces a catalog that was right. So is a list that says it goes on (`has_more: true`, the pagination of OpenAI-style
69
+ * lists, which Anthropic's `/v1/models` uses: 20 to a page): its first page is not the catalog, and nothing here follows the pages. The error never
70
+ * quotes the payload.
71
+ */
72
+ export declare function parseModelList(payload: unknown): string[];
73
+ /**
74
+ * The models of LlmProxy's `GET /models/details`, in the order received: for each, its kind, the window and the answer it declares, what it accepts
75
+ * and returns, and what it can do. What is `null`, out of scale, or not what it should be is absent; an entry that cannot be one is left out; at most
76
+ * `CATALOG_MAX_MODELS` entries are read. A payload that is not a list is an `AiCatalogError`; a list with nothing in it is a list with nothing in it.
77
+ */
78
+ export declare function parseModelDetails(payload: unknown): AiModelCatalogEntry[];
79
+ /**
80
+ * What LlmProxy's `GET /modalities` says: for each way of use, in the order the gateway lists them, the paths it is called at, what its models
81
+ * accept and return together, and the models that run for it. A model with no kind of its own is of the kind of its group. At most 32 groups, and
82
+ * `CATALOG_MAX_MODELS` models in each, are read. A payload that is not a list is an `AiCatalogError`.
83
+ */
84
+ export declare function parseModalities(payload: unknown): AiModalityGroup[];
85
+ /** The limits of a model as BookWriter keeps them: `inputTokens` is the window minus the answer, and `outputTokens` the answer. */
86
+ export interface AiTokenLimits {
87
+ inputTokens?: number;
88
+ outputTokens?: number;
89
+ }
90
+ /**
91
+ * BookWriter's formula (`tokenLimitsFromDetails`): the input is the window minus the answer the model declares, or, when it declares none, minus a
92
+ * quarter of the window up to 4096; an answer with no window is only the answer; an answer that is not smaller than its window is not believed; and an
93
+ * input that is not a usable number of tokens (it would be 0 or negative, or under 256) is not said.
94
+ *
95
+ * Mind what it is: BookWriter's `inputTokens` is what is left for the prompt once the answer is taken off. The engine's `maxInputTokens` is the window
96
+ * itself (the engine takes the answer and a safety margin off it when it judges a prompt), so it is the declared `contextWindow` that goes there,
97
+ * never this number: use `modelFieldsFromCatalogEntry`, which does.
98
+ */
99
+ export declare function tokenLimitsFromDetails(entry: Pick<AiModelCatalogEntry, "contextWindow" | "maxOutputTokens"> | null | undefined): AiTokenLimits;
100
+ /** What a catalog entry can fill in a model of the configuration. */
101
+ export type AiCatalogModelFields = Partial<Pick<AiChatModelConfig, "maxInputTokens" | "maxOutputTokens" | "supportsToolCalls" | "supportsStreaming" | "supportsReasoning" | "supportsVision">>;
102
+ /**
103
+ * What a gateway declares of a model, as fields of the model in the configuration, for the models of kind `chat`. Only what is declared is said:
104
+ * the context window is `maxInputTokens` (the window, not BookWriter's input: see `tokenLimitsFromDetails`), the answer is `maxOutputTokens` (when it fits the
105
+ * window), and a capability is said only when it is `true`. A capability that is `false` is what LlmProxy sends for everything its administrator has not
106
+ * declared, so it is never turned into a restriction (a model that streams, with the box not ticked, must go on streaming); and an id that only
107
+ * `/models` listed says nothing at all. The author's own values are kept by the caller: `{ ...model, ...fields }` lets the gateway's declarations win for
108
+ * what it declares, and leaves the rest as it is.
109
+ */
110
+ export declare function modelFieldsFromCatalogEntry(entry: AiModelCatalogEntry): AiCatalogModelFields;
111
+ type ThinkingLevel = Exclude<AiThinking, "automatic">;
112
+ /** The levels of thinking a model allows (in the engine's own order), and the one it uses when none is chosen. */
113
+ export interface AiLmStudioReasoning {
114
+ options: ThinkingLevel[];
115
+ default?: ThinkingLevel;
116
+ }
117
+ /** One language model of LM Studio, as its own list says it. */
118
+ export interface AiLmStudioModel {
119
+ key: string;
120
+ /**
121
+ * The levels of thinking the model declares. `null`: the server says the model has no level to choose (what it allows is not low, medium, high or
122
+ * xhigh). Absent: the server said nothing of reasoning for this model.
123
+ */
124
+ reasoning?: AiLmStudioReasoning | null;
125
+ /** The longest context the model supports. */
126
+ maxContext?: number;
127
+ /** The instances that are loaded now, with the context each was loaded with. */
128
+ loaded: Array<{
129
+ id: string;
130
+ contextLength?: number;
131
+ }>;
132
+ }
133
+ /**
134
+ * The language models (`type: "llm"`) of LM Studio's `GET /api/v1/models`, as BookWriter reads them: the thinking levels each declares
135
+ * (`capabilities.reasoning.allowed_options`, of which only low, medium, high and xhigh are levels), its default, the context it supports and the one
136
+ * it loaded. At most `LMSTUDIO_MAX_MODELS` entries are read. A payload that is not a list of models is an `AiCatalogError`.
137
+ */
138
+ export declare function parseLmStudioModels(payload: unknown): AiLmStudioModel[];
139
+ /**
140
+ * The context of one LM Studio model, found as BookWriter finds it: by its key or by the id of one of its loaded instances; `loaded` is the context
141
+ * of that instance (else of the first one) and `max` the longest the model supports. `undefined` for a model the list does not have.
142
+ */
143
+ export declare function lmStudioContext(models: readonly AiLmStudioModel[], modelId: string): {
144
+ loaded?: number;
145
+ max?: number;
146
+ } | undefined;
147
+ export {};
148
+ //# sourceMappingURL=catalog.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"catalog.d.ts","sourceRoot":"","sources":["../../src/ai/catalog.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAOjE,+HAA+H;AAC/H,eAAO,MAAM,kBAAkB,OAAQ,CAAC;AACxC,iCAAiC;AACjC,eAAO,MAAM,qBAAqB,MAAM,CAAC;AACzC,6HAA6H;AAC7H,eAAO,MAAM,kBAAkB,MAAM,CAAC;AACtC,eAAO,MAAM,kBAAkB,WAAa,CAAC;AAC7C,qEAAqE;AACrE,eAAO,MAAM,mBAAmB,MAAM,CAAC;AACvC,eAAO,MAAM,uBAAuB,MAAM,CAAC;AAkB3C;;;;;;GAMG;AACH,MAAM,MAAM,oBAAoB,GAAG,aAAa,GAAG,SAAS,GAAG,cAAc,GAAG,WAAW,GAAG,cAAc,GAAG,cAAc,GAAG,YAAY,GAAG,SAAS,GAAG,OAAO,GAAG,WAAW,GAAG,aAAa,CAAC;AAEjM;;;GAGG;AACH,qBAAa,cAAe,SAAQ,KAAK;IACV,QAAQ,CAAC,MAAM,EAAE,oBAAoB;IAAE,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM;gBAAhF,OAAO,EAAE,MAAM,EAAW,MAAM,EAAE,oBAAoB,EAAW,MAAM,CAAC,EAAE,MAAM,YAAA;CAI7F;AA2CD,2GAA2G;AAC3G,eAAO,MAAM,cAAc,8FAA+F,CAAC;AAC3H,qFAAqF;AACrF,MAAM,MAAM,WAAW,GAAG,CAAC,OAAO,cAAc,CAAC,CAAC,MAAM,CAAC,GAAG,SAAS,CAAC;AAEtE,yKAAyK;AACzK,MAAM,WAAW,mBAAmB;IAClC,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,MAAM,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