@c9up/polaris 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +614 -10
- package/dist/Polaris.d.ts +6 -2
- package/dist/Polaris.d.ts.map +1 -1
- package/dist/Polaris.js +14 -0
- package/dist/Polaris.js.map +1 -1
- package/dist/PolarisClient.d.ts +51 -3
- package/dist/PolarisClient.d.ts.map +1 -1
- package/dist/PolarisClient.js +213 -45
- package/dist/PolarisClient.js.map +1 -1
- package/dist/PolarisProvider.d.ts +7 -2
- package/dist/PolarisProvider.d.ts.map +1 -1
- package/dist/PolarisProvider.js +34 -2
- package/dist/PolarisProvider.js.map +1 -1
- package/dist/augmentations.d.ts +4 -1
- package/dist/augmentations.d.ts.map +1 -1
- package/dist/augmentations.js +1 -1
- package/dist/budget.d.ts +39 -6
- package/dist/budget.d.ts.map +1 -1
- package/dist/budget.js +79 -12
- package/dist/budget.js.map +1 -1
- package/dist/catalog/catalog.d.ts +37 -0
- package/dist/catalog/catalog.d.ts.map +1 -0
- package/dist/catalog/catalog.js +145 -0
- package/dist/catalog/catalog.js.map +1 -0
- package/dist/catalog/discover.d.ts +64 -0
- package/dist/catalog/discover.d.ts.map +1 -0
- package/dist/catalog/discover.js +148 -0
- package/dist/catalog/discover.js.map +1 -0
- package/dist/catalog/index.d.ts +8 -0
- package/dist/catalog/index.d.ts.map +1 -0
- package/dist/catalog/index.js +8 -0
- package/dist/catalog/index.js.map +1 -0
- package/dist/commands/index.d.ts +82 -0
- package/dist/commands/index.d.ts.map +1 -0
- package/dist/commands/index.js +278 -0
- package/dist/commands/index.js.map +1 -0
- package/dist/cost.d.ts +3 -1
- package/dist/cost.d.ts.map +1 -1
- package/dist/cost.js +8 -3
- package/dist/cost.js.map +1 -1
- package/dist/drivers/anthropic.d.ts +6 -1
- package/dist/drivers/anthropic.d.ts.map +1 -1
- package/dist/drivers/anthropic.js +166 -21
- package/dist/drivers/anthropic.js.map +1 -1
- package/dist/drivers/openai-compatible.d.ts +1 -1
- package/dist/drivers/openai-compatible.d.ts.map +1 -1
- package/dist/drivers/openai-compatible.js +106 -70
- package/dist/drivers/openai-compatible.js.map +1 -1
- package/dist/errors.d.ts +24 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +24 -0
- package/dist/errors.js.map +1 -1
- package/dist/index.d.ts +6 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/json-schema.d.ts +33 -0
- package/dist/json-schema.d.ts.map +1 -0
- package/dist/json-schema.js +508 -0
- package/dist/json-schema.js.map +1 -0
- package/dist/log.d.ts.map +1 -1
- package/dist/log.js +3 -1
- package/dist/log.js.map +1 -1
- package/dist/mcp/client.d.ts +81 -0
- package/dist/mcp/client.d.ts.map +1 -0
- package/dist/mcp/client.js +401 -0
- package/dist/mcp/client.js.map +1 -0
- package/dist/mcp/endpoint.d.ts +54 -0
- package/dist/mcp/endpoint.d.ts.map +1 -0
- package/dist/mcp/endpoint.js +158 -0
- package/dist/mcp/endpoint.js.map +1 -0
- package/dist/mcp/host.d.ts +42 -0
- package/dist/mcp/host.d.ts.map +1 -0
- package/dist/mcp/host.js +111 -0
- package/dist/mcp/host.js.map +1 -0
- package/dist/mcp/http.d.ts +32 -0
- package/dist/mcp/http.d.ts.map +1 -0
- package/dist/mcp/http.js +190 -0
- package/dist/mcp/http.js.map +1 -0
- package/dist/mcp/index.d.ts +23 -0
- package/dist/mcp/index.d.ts.map +1 -0
- package/dist/mcp/index.js +21 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/mcp/manifest.d.ts +36 -0
- package/dist/mcp/manifest.d.ts.map +1 -0
- package/dist/mcp/manifest.js +40 -0
- package/dist/mcp/manifest.js.map +1 -0
- package/dist/mcp/rpc.d.ts +39 -0
- package/dist/mcp/rpc.d.ts.map +1 -0
- package/dist/mcp/rpc.js +149 -0
- package/dist/mcp/rpc.js.map +1 -0
- package/dist/mcp/stdio.d.ts +24 -0
- package/dist/mcp/stdio.d.ts.map +1 -0
- package/dist/mcp/stdio.js +157 -0
- package/dist/mcp/stdio.js.map +1 -0
- package/dist/mcp/worker-main.d.ts +6 -0
- package/dist/mcp/worker-main.d.ts.map +1 -0
- package/dist/mcp/worker-main.js +17 -0
- package/dist/mcp/worker-main.js.map +1 -0
- package/dist/mcp/worker.d.ts +54 -0
- package/dist/mcp/worker.d.ts.map +1 -0
- package/dist/mcp/worker.js +166 -0
- package/dist/mcp/worker.js.map +1 -0
- package/dist/meter.d.ts +6 -0
- package/dist/meter.d.ts.map +1 -1
- package/dist/meter.js +6 -3
- package/dist/meter.js.map +1 -1
- package/dist/redact.d.ts +4 -3
- package/dist/redact.d.ts.map +1 -1
- package/dist/redact.js +5 -4
- package/dist/redact.js.map +1 -1
- package/dist/redis-budget.d.ts +45 -0
- package/dist/redis-budget.d.ts.map +1 -0
- package/dist/redis-budget.js +56 -0
- package/dist/redis-budget.js.map +1 -0
- package/dist/response.d.ts +21 -3
- package/dist/response.d.ts.map +1 -1
- package/dist/response.js +71 -7
- package/dist/response.js.map +1 -1
- package/dist/server-sent.d.ts +9 -0
- package/dist/server-sent.d.ts.map +1 -0
- package/dist/server-sent.js +74 -0
- package/dist/server-sent.js.map +1 -0
- package/dist/server-tools.d.ts +12 -0
- package/dist/server-tools.d.ts.map +1 -0
- package/dist/server-tools.js +71 -0
- package/dist/server-tools.js.map +1 -0
- package/dist/services/catalog.d.ts +19 -0
- package/dist/services/catalog.d.ts.map +1 -0
- package/dist/services/catalog.js +37 -0
- package/dist/services/catalog.js.map +1 -0
- package/dist/skills/folder.d.ts +32 -0
- package/dist/skills/folder.d.ts.map +1 -0
- package/dist/skills/folder.js +170 -0
- package/dist/skills/folder.js.map +1 -0
- package/dist/skills/frontmatter.d.ts +15 -0
- package/dist/skills/frontmatter.d.ts.map +1 -0
- package/dist/skills/frontmatter.js +92 -0
- package/dist/skills/frontmatter.js.map +1 -0
- package/dist/skills/index.d.ts +18 -0
- package/dist/skills/index.d.ts.map +1 -0
- package/dist/skills/index.js +18 -0
- package/dist/skills/index.js.map +1 -0
- package/dist/skills/kit.d.ts +39 -0
- package/dist/skills/kit.d.ts.map +1 -0
- package/dist/skills/kit.js +124 -0
- package/dist/skills/kit.js.map +1 -0
- package/dist/skills/skill.d.ts +67 -0
- package/dist/skills/skill.d.ts.map +1 -0
- package/dist/skills/skill.js +114 -0
- package/dist/skills/skill.js.map +1 -0
- package/dist/sse.d.ts +43 -6
- package/dist/sse.d.ts.map +1 -1
- package/dist/sse.js +44 -18
- package/dist/sse.js.map +1 -1
- package/dist/testing/FakeProvider.d.ts.map +1 -1
- package/dist/testing/FakeProvider.js +1 -0
- package/dist/testing/FakeProvider.js.map +1 -1
- package/dist/tool-modules.d.ts +17 -0
- package/dist/tool-modules.d.ts.map +1 -0
- package/dist/tool-modules.js +43 -0
- package/dist/tool-modules.js.map +1 -0
- package/dist/tools.d.ts +69 -5
- package/dist/tools.d.ts.map +1 -1
- package/dist/tools.js +174 -31
- package/dist/tools.js.map +1 -1
- package/dist/transport.d.ts +8 -3
- package/dist/transport.d.ts.map +1 -1
- package/dist/transport.js +19 -4
- package/dist/transport.js.map +1 -1
- package/dist/types.d.ts +106 -2
- package/dist/types.d.ts.map +1 -1
- package/package.json +22 -1
- package/src/Polaris.ts +25 -1
- package/src/PolarisClient.ts +288 -51
- package/src/PolarisProvider.ts +40 -2
- package/src/augmentations.ts +4 -1
- package/src/budget.ts +115 -15
- package/src/catalog/catalog.ts +180 -0
- package/src/catalog/discover.ts +234 -0
- package/src/catalog/index.ts +19 -0
- package/src/commands/index.ts +412 -0
- package/src/cost.ts +9 -3
- package/src/drivers/anthropic.ts +203 -20
- package/src/drivers/openai-compatible.ts +143 -76
- package/src/errors.ts +41 -0
- package/src/index.ts +16 -0
- package/src/json-schema.ts +597 -0
- package/src/log.ts +3 -1
- package/src/mcp/client.ts +532 -0
- package/src/mcp/endpoint.ts +229 -0
- package/src/mcp/host.ts +140 -0
- package/src/mcp/http.ts +276 -0
- package/src/mcp/index.ts +51 -0
- package/src/mcp/manifest.ts +73 -0
- package/src/mcp/rpc.ts +197 -0
- package/src/mcp/stdio.ts +190 -0
- package/src/mcp/worker-main.ts +18 -0
- package/src/mcp/worker.ts +212 -0
- package/src/meter.ts +13 -2
- package/src/redact.ts +6 -3
- package/src/redis-budget.ts +85 -0
- package/src/response.ts +86 -8
- package/src/server-sent.ts +70 -0
- package/src/server-tools.ts +77 -0
- package/src/services/catalog.ts +43 -0
- package/src/skills/folder.ts +231 -0
- package/src/skills/frontmatter.ts +92 -0
- package/src/skills/index.ts +38 -0
- package/src/skills/kit.ts +163 -0
- package/src/skills/skill.ts +171 -0
- package/src/sse.ts +88 -22
- package/src/testing/FakeProvider.ts +1 -0
- package/src/tool-modules.ts +49 -0
- package/src/tools.ts +260 -27
- package/src/transport.ts +25 -3
- package/src/types.ts +108 -2
|
@@ -15,7 +15,12 @@ import {
|
|
|
15
15
|
E_POLARIS_PROVIDER_UNREACHABLE,
|
|
16
16
|
E_POLARIS_STREAM_INCOMPLETE,
|
|
17
17
|
} from "../errors.js";
|
|
18
|
-
import {
|
|
18
|
+
import {
|
|
19
|
+
createResponse,
|
|
20
|
+
isTokenCount,
|
|
21
|
+
type ResponseInput,
|
|
22
|
+
} from "../response.js";
|
|
23
|
+
import { serverSentData } from "../server-sent.js";
|
|
19
24
|
import type {
|
|
20
25
|
AssistantPart,
|
|
21
26
|
ChatRequest,
|
|
@@ -31,7 +36,6 @@ import type {
|
|
|
31
36
|
StopReason,
|
|
32
37
|
StreamEvent,
|
|
33
38
|
ToolCallPart,
|
|
34
|
-
Usage,
|
|
35
39
|
UserPart,
|
|
36
40
|
} from "../types.js";
|
|
37
41
|
import { errorForStatus } from "./status.js";
|
|
@@ -44,6 +48,14 @@ const MAX_RETRY_DELAY = 60_000;
|
|
|
44
48
|
const BASE_RETRY_DELAY = 500;
|
|
45
49
|
/** Inputs per embedding request: well under OpenAI's 2048, for services that take fewer. */
|
|
46
50
|
const DEFAULT_EMBEDDING_BATCH = 128;
|
|
51
|
+
/**
|
|
52
|
+
* The most tool calls one reply is read with. Far above what a run lets
|
|
53
|
+
* through (`maxToolCalls`): this only stops a broken or hostile reply from
|
|
54
|
+
* growing without end while it is read.
|
|
55
|
+
*/
|
|
56
|
+
const MAX_REPLY_TOOL_CALLS = 256;
|
|
57
|
+
/** The longest server-sent event read, in characters: a chunk is far shorter. */
|
|
58
|
+
const MAX_EVENT_LENGTH = 4 * 1024 * 1024;
|
|
47
59
|
|
|
48
60
|
export interface OpenAICompatibleConfig {
|
|
49
61
|
/** The API's base URL, up to and including `/v1`: `http://localhost:11434/v1`. */
|
|
@@ -69,7 +81,7 @@ export interface OpenAICompatibleConfig {
|
|
|
69
81
|
* images off. Declare what your model does, and polaris refuses the rest
|
|
70
82
|
* before sending.
|
|
71
83
|
*/
|
|
72
|
-
readonly features?: Partial<DriverFeatures
|
|
84
|
+
readonly features?: Partial<Omit<DriverFeatures, "serverTools">>;
|
|
73
85
|
/** Output limit when a request sets none. Default 16 000. */
|
|
74
86
|
readonly maxTokens?: number;
|
|
75
87
|
/**
|
|
@@ -99,6 +111,25 @@ interface PartialCall {
|
|
|
99
111
|
id: string;
|
|
100
112
|
name: string;
|
|
101
113
|
arguments: string;
|
|
114
|
+
/**
|
|
115
|
+
* The call's fields polaris does not model, such as Gemini's
|
|
116
|
+
* `extra_content.google.thought_signature`, which the service requires
|
|
117
|
+
* back on the next turn.
|
|
118
|
+
*/
|
|
119
|
+
extra: Record<string, unknown>;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** A tool call's fields polaris models; every other one is kept as it came. */
|
|
123
|
+
const MODELLED_CALL_FIELDS = new Set(["index", "id", "type", "function"]);
|
|
124
|
+
|
|
125
|
+
/** Copy the fields of `raw` polaris does not model into `extra`. */
|
|
126
|
+
function keepExtraFields(
|
|
127
|
+
raw: Record<string, unknown>,
|
|
128
|
+
extra: Record<string, unknown>,
|
|
129
|
+
): void {
|
|
130
|
+
for (const [key, value] of Object.entries(raw)) {
|
|
131
|
+
if (!MODELLED_CALL_FIELDS.has(key)) extra[key] = value;
|
|
132
|
+
}
|
|
102
133
|
}
|
|
103
134
|
|
|
104
135
|
interface StreamState {
|
|
@@ -108,7 +139,7 @@ interface StreamState {
|
|
|
108
139
|
reasoning: string;
|
|
109
140
|
calls: Map<number, PartialCall>;
|
|
110
141
|
finish: string | undefined;
|
|
111
|
-
usage:
|
|
142
|
+
usage: ResponseInput["usage"];
|
|
112
143
|
done: boolean;
|
|
113
144
|
}
|
|
114
145
|
|
|
@@ -116,6 +147,8 @@ class OpenAICompatibleDriver implements PolarisDriver {
|
|
|
116
147
|
readonly provider: string;
|
|
117
148
|
readonly features: DriverFeatures;
|
|
118
149
|
readonly defaults: DriverDefaults;
|
|
150
|
+
/** The configured key and every header value: an `api-key`, a gateway token... */
|
|
151
|
+
readonly secrets: readonly string[];
|
|
119
152
|
readonly embed?: (
|
|
120
153
|
request: EmbedRequest,
|
|
121
154
|
call: DriverCall,
|
|
@@ -164,6 +197,8 @@ class OpenAICompatibleDriver implements PolarisDriver {
|
|
|
164
197
|
structuredOutput: true,
|
|
165
198
|
images: false,
|
|
166
199
|
...config.features,
|
|
200
|
+
// The Chat Completions API has no server-side tools to send them to.
|
|
201
|
+
serverTools: false,
|
|
167
202
|
};
|
|
168
203
|
this.defaults = {
|
|
169
204
|
model: config.model,
|
|
@@ -171,6 +206,10 @@ class OpenAICompatibleDriver implements PolarisDriver {
|
|
|
171
206
|
maxTokens: config.maxTokens ?? DEFAULT_MAX_TOKENS,
|
|
172
207
|
streamMaxTokens: config.maxTokens ?? DEFAULT_MAX_TOKENS,
|
|
173
208
|
};
|
|
209
|
+
this.secrets = [
|
|
210
|
+
...(config.apiKey !== undefined ? [config.apiKey] : []),
|
|
211
|
+
...Object.values(config.headers ?? {}),
|
|
212
|
+
];
|
|
174
213
|
const embeddingModel = config.embeddingModel;
|
|
175
214
|
if (embeddingModel !== undefined) {
|
|
176
215
|
this.embed = (request, call) =>
|
|
@@ -211,7 +250,15 @@ class OpenAICompatibleDriver implements PolarisDriver {
|
|
|
211
250
|
usage: undefined,
|
|
212
251
|
done: false,
|
|
213
252
|
};
|
|
214
|
-
for await (const data of serverSentData(
|
|
253
|
+
for await (const data of serverSentData(
|
|
254
|
+
response.body,
|
|
255
|
+
MAX_EVENT_LENGTH,
|
|
256
|
+
() =>
|
|
257
|
+
new E_POLARIS_INVALID_RESPONSE([
|
|
258
|
+
this.provider,
|
|
259
|
+
`a server-sent event is longer than ${MAX_EVENT_LENGTH} characters`,
|
|
260
|
+
]),
|
|
261
|
+
)) {
|
|
215
262
|
if (data === "[DONE]") {
|
|
216
263
|
state.done = true;
|
|
217
264
|
break;
|
|
@@ -224,7 +271,7 @@ class OpenAICompatibleDriver implements PolarisDriver {
|
|
|
224
271
|
|
|
225
272
|
const calls = [...state.calls.entries()]
|
|
226
273
|
.sort(([a], [b]) => a - b)
|
|
227
|
-
.map(([, partial]) => toToolCall(partial));
|
|
274
|
+
.map(([, partial]) => toToolCall(partial, this.provider));
|
|
228
275
|
for (const toolCall of calls) yield { type: "tool_call", call: toolCall };
|
|
229
276
|
|
|
230
277
|
yield {
|
|
@@ -274,12 +321,16 @@ class OpenAICompatibleDriver implements PolarisDriver {
|
|
|
274
321
|
for (const fragment of list(delta.tool_calls)) {
|
|
275
322
|
if (!isRecord(fragment)) continue;
|
|
276
323
|
const index = typeof fragment.index === "number" ? fragment.index : 0;
|
|
324
|
+
if (!state.calls.has(index))
|
|
325
|
+
checkCallCount(state.calls.size + 1, this.provider);
|
|
277
326
|
const partial = state.calls.get(index) ?? {
|
|
278
327
|
id: "",
|
|
279
328
|
name: "",
|
|
280
329
|
arguments: "",
|
|
330
|
+
extra: {},
|
|
281
331
|
};
|
|
282
332
|
if (typeof fragment.id === "string") partial.id = fragment.id;
|
|
333
|
+
keepExtraFields(fragment, partial.extra);
|
|
283
334
|
const fn = isRecord(fragment.function) ? fragment.function : {};
|
|
284
335
|
if (typeof fn.name === "string") partial.name += fn.name;
|
|
285
336
|
if (typeof fn.arguments === "string") partial.arguments += fn.arguments;
|
|
@@ -299,6 +350,7 @@ class OpenAICompatibleDriver implements PolarisDriver {
|
|
|
299
350
|
const size = this.#config.embeddingBatchSize ?? DEFAULT_EMBEDDING_BATCH;
|
|
300
351
|
const vectors: Float32Array[] = [];
|
|
301
352
|
let inputTokens = 0;
|
|
353
|
+
let usageUnknown = false;
|
|
302
354
|
let model = request.model ?? defaultModel;
|
|
303
355
|
for (let start = 0; start < request.inputs.length; start += size) {
|
|
304
356
|
const batch = request.inputs.slice(start, start + size);
|
|
@@ -315,7 +367,8 @@ class OpenAICompatibleDriver implements PolarisDriver {
|
|
|
315
367
|
]);
|
|
316
368
|
}
|
|
317
369
|
vectors.push(...answer.vectors);
|
|
318
|
-
inputTokens += answer.inputTokens;
|
|
370
|
+
if (isTokenCount(answer.inputTokens)) inputTokens += answer.inputTokens;
|
|
371
|
+
else usageUnknown = true;
|
|
319
372
|
model = answer.model;
|
|
320
373
|
}
|
|
321
374
|
// polaris then checks that every vector has this length, across batches.
|
|
@@ -325,6 +378,7 @@ class OpenAICompatibleDriver implements PolarisDriver {
|
|
|
325
378
|
dimensions: vectors[0]?.length ?? 0,
|
|
326
379
|
vectors,
|
|
327
380
|
usage: { inputTokens },
|
|
381
|
+
...(usageUnknown ? { usageUnknown } : {}),
|
|
328
382
|
};
|
|
329
383
|
}
|
|
330
384
|
|
|
@@ -333,7 +387,7 @@ class OpenAICompatibleDriver implements PolarisDriver {
|
|
|
333
387
|
inputs: readonly string[],
|
|
334
388
|
dimensions: number | undefined,
|
|
335
389
|
call: DriverCall,
|
|
336
|
-
): Promise<{ model: string; vectors: Float32Array[]; inputTokens:
|
|
390
|
+
): Promise<{ model: string; vectors: Float32Array[]; inputTokens: unknown }> {
|
|
337
391
|
const response = await this.#post(
|
|
338
392
|
"/embeddings",
|
|
339
393
|
{
|
|
@@ -364,11 +418,11 @@ class OpenAICompatibleDriver implements PolarisDriver {
|
|
|
364
418
|
}
|
|
365
419
|
return Float32Array.from(numbers);
|
|
366
420
|
});
|
|
367
|
-
const usage = isRecord(body.usage) ? body.usage : {};
|
|
368
421
|
return {
|
|
369
422
|
model: typeof body.model === "string" ? body.model : model,
|
|
370
423
|
vectors,
|
|
371
|
-
|
|
424
|
+
// As reported: `#embed` adds it up only when it can be a count.
|
|
425
|
+
inputTokens: isRecord(body.usage) ? body.usage.prompt_tokens : undefined,
|
|
372
426
|
};
|
|
373
427
|
}
|
|
374
428
|
|
|
@@ -379,7 +433,7 @@ class OpenAICompatibleDriver implements PolarisDriver {
|
|
|
379
433
|
return {
|
|
380
434
|
...providerOptions(request, this.provider),
|
|
381
435
|
model: request.model ?? config.model,
|
|
382
|
-
messages: toMessages(request),
|
|
436
|
+
messages: toMessages(request, this.provider),
|
|
383
437
|
[config.maxTokensField ?? "max_tokens"]:
|
|
384
438
|
request.maxTokens ?? config.maxTokens ?? DEFAULT_MAX_TOKENS,
|
|
385
439
|
// Sent either way: Infomaniak streams unless told not to.
|
|
@@ -513,16 +567,22 @@ class OpenAICompatibleDriver implements PolarisDriver {
|
|
|
513
567
|
"the reply has no message",
|
|
514
568
|
]);
|
|
515
569
|
}
|
|
516
|
-
const
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
570
|
+
const rawCalls = list(message.tool_calls);
|
|
571
|
+
checkCallCount(rawCalls.length, this.provider);
|
|
572
|
+
const calls = rawCalls.filter(isRecord).map((raw) => {
|
|
573
|
+
const fn = isRecord(raw.function) ? raw.function : {};
|
|
574
|
+
const extra: Record<string, unknown> = {};
|
|
575
|
+
keepExtraFields(raw, extra);
|
|
576
|
+
return toToolCall(
|
|
577
|
+
{
|
|
521
578
|
id: typeof raw.id === "string" ? raw.id : "",
|
|
522
579
|
name: typeof fn.name === "string" ? fn.name : "",
|
|
523
580
|
arguments: typeof fn.arguments === "string" ? fn.arguments : "",
|
|
524
|
-
|
|
525
|
-
|
|
581
|
+
extra,
|
|
582
|
+
},
|
|
583
|
+
this.provider,
|
|
584
|
+
);
|
|
585
|
+
});
|
|
526
586
|
const text = typeof message.content === "string" ? message.content : "";
|
|
527
587
|
return createResponse({
|
|
528
588
|
id: typeof body.id === "string" ? body.id : "",
|
|
@@ -538,7 +598,7 @@ class OpenAICompatibleDriver implements PolarisDriver {
|
|
|
538
598
|
: undefined,
|
|
539
599
|
calls.length > 0,
|
|
540
600
|
),
|
|
541
|
-
usage:
|
|
601
|
+
usage: toUsage(body.usage),
|
|
542
602
|
});
|
|
543
603
|
}
|
|
544
604
|
}
|
|
@@ -557,16 +617,39 @@ function assistantContent(
|
|
|
557
617
|
}
|
|
558
618
|
|
|
559
619
|
/** The chat messages, the system prompt first. */
|
|
560
|
-
function toMessages(
|
|
620
|
+
function toMessages(
|
|
621
|
+
request: ChatRequest,
|
|
622
|
+
provider: string,
|
|
623
|
+
): Record<string, unknown>[] {
|
|
561
624
|
const out: Record<string, unknown>[] = [];
|
|
562
625
|
if (request.system !== undefined) {
|
|
563
626
|
out.push({ role: "system", content: request.system });
|
|
564
627
|
}
|
|
565
|
-
for (const message of request.messages)
|
|
628
|
+
for (const message of request.messages) {
|
|
629
|
+
out.push(...toMessage(message, provider));
|
|
630
|
+
}
|
|
566
631
|
return out;
|
|
567
632
|
}
|
|
568
633
|
|
|
569
|
-
|
|
634
|
+
/**
|
|
635
|
+
* The fields a tool call came with from `provider` and polaris does not
|
|
636
|
+
* model, to send back as they were; nothing for another provider's call.
|
|
637
|
+
*/
|
|
638
|
+
function extraFieldsOf(
|
|
639
|
+
part: ToolCallPart,
|
|
640
|
+
provider: string,
|
|
641
|
+
): Record<string, unknown> {
|
|
642
|
+
const origin = part.origin;
|
|
643
|
+
if (origin?.provider !== provider || !isRecord(origin.block)) return {};
|
|
644
|
+
const extra: Record<string, unknown> = {};
|
|
645
|
+
keepExtraFields(origin.block, extra);
|
|
646
|
+
return extra;
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
function toMessage(
|
|
650
|
+
message: Message,
|
|
651
|
+
provider: string,
|
|
652
|
+
): Record<string, unknown>[] {
|
|
570
653
|
if (message.role === "assistant") {
|
|
571
654
|
if (typeof message.content === "string") {
|
|
572
655
|
return [{ role: "assistant", content: message.content }];
|
|
@@ -579,6 +662,8 @@ function toMessage(message: Message): Record<string, unknown>[] {
|
|
|
579
662
|
const calls = message.content
|
|
580
663
|
.filter((part): part is ToolCallPart => part.type === "tool_call")
|
|
581
664
|
.map((part) => ({
|
|
665
|
+
// First, so a kept field can never replace the call's own.
|
|
666
|
+
...extraFieldsOf(part, provider),
|
|
582
667
|
id: part.id,
|
|
583
668
|
type: "function",
|
|
584
669
|
function: {
|
|
@@ -626,7 +711,7 @@ function toMessage(message: Message): Record<string, unknown>[] {
|
|
|
626
711
|
return [...results, { role: "user", content }];
|
|
627
712
|
}
|
|
628
713
|
|
|
629
|
-
function toToolCall(partial: PartialCall): ToolCallPart {
|
|
714
|
+
function toToolCall(partial: PartialCall, provider: string): ToolCallPart {
|
|
630
715
|
let input: unknown = {};
|
|
631
716
|
if (partial.arguments.trim() !== "") {
|
|
632
717
|
try {
|
|
@@ -637,7 +722,17 @@ function toToolCall(partial: PartialCall): ToolCallPart {
|
|
|
637
722
|
input = partial.arguments;
|
|
638
723
|
}
|
|
639
724
|
}
|
|
640
|
-
|
|
725
|
+
const call: ToolCallPart = {
|
|
726
|
+
type: "tool_call",
|
|
727
|
+
id: partial.id,
|
|
728
|
+
name: partial.name,
|
|
729
|
+
input,
|
|
730
|
+
};
|
|
731
|
+
// Only a call that carries such fields gets an origin: the others stay as
|
|
732
|
+
// plain as every other provider's.
|
|
733
|
+
return Object.keys(partial.extra).length === 0
|
|
734
|
+
? call
|
|
735
|
+
: { ...call, origin: { provider, block: partial.extra } };
|
|
641
736
|
}
|
|
642
737
|
|
|
643
738
|
/** Reasoning text where each service puts it: Groq and Ollama `reasoning`, Workers AI `reasoning_content`. */
|
|
@@ -670,15 +765,25 @@ function toStopReason(
|
|
|
670
765
|
}
|
|
671
766
|
}
|
|
672
767
|
|
|
673
|
-
|
|
768
|
+
/**
|
|
769
|
+
* The usage of a reply, as reported: `createResponse` checks the counts, and
|
|
770
|
+
* marks the response's usage unknown when one cannot be right. A body with
|
|
771
|
+
* no `prompt_tokens` reported none.
|
|
772
|
+
*/
|
|
773
|
+
function toUsage(usage: unknown): ResponseInput["usage"] {
|
|
774
|
+
if (!isRecord(usage) || usage.prompt_tokens === undefined) return undefined;
|
|
674
775
|
const details = isRecord(usage.prompt_tokens_details)
|
|
675
776
|
? usage.prompt_tokens_details
|
|
676
777
|
: {};
|
|
677
|
-
// `prompt_tokens` counts the cached tokens too; polaris's input count does
|
|
678
|
-
|
|
778
|
+
// `prompt_tokens` counts the cached tokens too; polaris's input count does
|
|
779
|
+
// not. More cached tokens than prompt tokens leaves a negative count.
|
|
780
|
+
const cached = details.cached_tokens ?? 0;
|
|
679
781
|
return {
|
|
680
|
-
inputTokens:
|
|
681
|
-
|
|
782
|
+
inputTokens:
|
|
783
|
+
typeof usage.prompt_tokens === "number" && typeof cached === "number"
|
|
784
|
+
? usage.prompt_tokens - cached
|
|
785
|
+
: usage.prompt_tokens,
|
|
786
|
+
outputTokens: usage.completion_tokens,
|
|
682
787
|
cacheReadTokens: cached,
|
|
683
788
|
cacheWriteTokens: 0,
|
|
684
789
|
};
|
|
@@ -730,47 +835,6 @@ function parseJson(text: string, provider: string): Record<string, unknown> {
|
|
|
730
835
|
return value;
|
|
731
836
|
}
|
|
732
837
|
|
|
733
|
-
/** The `data:` payloads of a server-sent event stream, lines rejoined across chunks. */
|
|
734
|
-
async function* serverSentData(
|
|
735
|
-
body: ReadableStream<Uint8Array>,
|
|
736
|
-
): AsyncGenerator<string> {
|
|
737
|
-
const reader = body.getReader();
|
|
738
|
-
const decoder = new TextDecoder();
|
|
739
|
-
let buffer = "";
|
|
740
|
-
let data: string[] = [];
|
|
741
|
-
const lines = function* (final: boolean): Generator<string> {
|
|
742
|
-
let newline = buffer.indexOf("\n");
|
|
743
|
-
while (newline !== -1) {
|
|
744
|
-
const line = buffer.slice(0, newline).replace(/\r$/, "");
|
|
745
|
-
buffer = buffer.slice(newline + 1);
|
|
746
|
-
if (line === "") {
|
|
747
|
-
if (data.length > 0) yield data.join("\n");
|
|
748
|
-
data = [];
|
|
749
|
-
} else if (line.startsWith("data:")) {
|
|
750
|
-
data.push(line.slice(5).replace(/^ /, ""));
|
|
751
|
-
}
|
|
752
|
-
newline = buffer.indexOf("\n");
|
|
753
|
-
}
|
|
754
|
-
if (final) {
|
|
755
|
-
if (buffer.startsWith("data:"))
|
|
756
|
-
data.push(buffer.slice(5).replace(/^ /, ""));
|
|
757
|
-
if (data.length > 0) yield data.join("\n");
|
|
758
|
-
}
|
|
759
|
-
};
|
|
760
|
-
try {
|
|
761
|
-
while (true) {
|
|
762
|
-
const { value, done } = await reader.read();
|
|
763
|
-
if (done) break;
|
|
764
|
-
buffer += decoder.decode(value, { stream: true });
|
|
765
|
-
yield* lines(false);
|
|
766
|
-
}
|
|
767
|
-
buffer += decoder.decode();
|
|
768
|
-
yield* lines(true);
|
|
769
|
-
} finally {
|
|
770
|
-
await reader.cancel().catch(() => undefined);
|
|
771
|
-
}
|
|
772
|
-
}
|
|
773
|
-
|
|
774
838
|
/**
|
|
775
839
|
* How long to wait before attempt `attempt + 1`: what the provider asked for
|
|
776
840
|
* (`retry-after-ms`, then `retry-after` in seconds or as a date), at most a
|
|
@@ -820,6 +884,15 @@ function isRecord(value: unknown): value is Record<string, unknown> {
|
|
|
820
884
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
821
885
|
}
|
|
822
886
|
|
|
887
|
+
function checkCallCount(count: number, provider: string): void {
|
|
888
|
+
if (count > MAX_REPLY_TOOL_CALLS) {
|
|
889
|
+
throw new E_POLARIS_INVALID_RESPONSE([
|
|
890
|
+
provider,
|
|
891
|
+
`the reply calls more than ${MAX_REPLY_TOOL_CALLS} tools`,
|
|
892
|
+
]);
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
|
|
823
896
|
function list(value: unknown): unknown[] {
|
|
824
897
|
return Array.isArray(value) ? value : [];
|
|
825
898
|
}
|
|
@@ -829,12 +902,6 @@ function first(value: unknown): Record<string, unknown> | undefined {
|
|
|
829
902
|
return isRecord(head) ? head : undefined;
|
|
830
903
|
}
|
|
831
904
|
|
|
832
|
-
function count(value: unknown): number {
|
|
833
|
-
return typeof value === "number" && Number.isFinite(value) && value >= 0
|
|
834
|
-
? value
|
|
835
|
-
: 0;
|
|
836
|
-
}
|
|
837
|
-
|
|
838
905
|
function isCount(value: number): boolean {
|
|
839
906
|
return Number.isInteger(value) && value > 0;
|
|
840
907
|
}
|
package/src/errors.ts
CHANGED
|
@@ -84,6 +84,16 @@ export const E_POLARIS_TOO_MANY_STEPS = createError<[number]>(
|
|
|
84
84
|
"E_POLARIS_TOO_MANY_STEPS",
|
|
85
85
|
);
|
|
86
86
|
|
|
87
|
+
/**
|
|
88
|
+
* One reply called more tools than the run allows: none of its calls ran.
|
|
89
|
+
* A model steered by what it read could otherwise fire a side-effecting tool
|
|
90
|
+
* thousands of times at once.
|
|
91
|
+
*/
|
|
92
|
+
export const E_POLARIS_TOO_MANY_TOOL_CALLS = createError<[number, number]>(
|
|
93
|
+
"[polaris] the model called %d tools in one reply, more than the %d allowed; none ran.",
|
|
94
|
+
"E_POLARIS_TOO_MANY_TOOL_CALLS",
|
|
95
|
+
);
|
|
96
|
+
|
|
87
97
|
/** A stream ended without its final `done` event. */
|
|
88
98
|
export const E_POLARIS_STREAM_INCOMPLETE = createError<[string]>(
|
|
89
99
|
"[polaris] the stream from '%s' ended before its final response.",
|
|
@@ -159,3 +169,34 @@ export const E_POLARIS_PROVIDER_FAILED = createError<[string, string]>(
|
|
|
159
169
|
"[polaris] the call to '%s' failed: %s",
|
|
160
170
|
"E_POLARIS_PROVIDER_FAILED",
|
|
161
171
|
);
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* An MCP server could not be started or reached, broke the protocol, or
|
|
175
|
+
* answered with an error. Its connection's secrets (headers, token, the
|
|
176
|
+
* environment given to a stdio server) are taken out of the message.
|
|
177
|
+
*/
|
|
178
|
+
export const E_POLARIS_MCP = createError<[string, string]>(
|
|
179
|
+
"[polaris] the MCP server '%s' failed: %s",
|
|
180
|
+
"E_POLARIS_MCP",
|
|
181
|
+
502,
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* A skill folder cannot be read: no `SKILL.md`, a frontmatter that misses
|
|
186
|
+
* `name` or `description` or breaks their limits, a file over its size, a
|
|
187
|
+
* tool module that exports no tools.
|
|
188
|
+
*/
|
|
189
|
+
export const E_POLARIS_INVALID_SKILL = createError<[string, string]>(
|
|
190
|
+
"[polaris] the skill at '%s' cannot be used: %s",
|
|
191
|
+
"E_POLARIS_INVALID_SKILL",
|
|
192
|
+
);
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* The application's tool and skill catalog cannot be built or used: a tool
|
|
196
|
+
* module that exports no tools, one name in two files, a manifest that is
|
|
197
|
+
* missing, unreadable or out of date.
|
|
198
|
+
*/
|
|
199
|
+
export const E_POLARIS_INVALID_CATALOG = createError<[string]>(
|
|
200
|
+
"[polaris] the tool catalog cannot be used: %s",
|
|
201
|
+
"E_POLARIS_INVALID_CATALOG",
|
|
202
|
+
);
|
package/src/index.ts
CHANGED
|
@@ -6,12 +6,14 @@ import "./augmentations.js";
|
|
|
6
6
|
|
|
7
7
|
export {
|
|
8
8
|
type BudgetConfig,
|
|
9
|
+
type BudgetPeriod,
|
|
9
10
|
type BudgetStore,
|
|
10
11
|
memoryBudgetStore,
|
|
11
12
|
} from "./budget.js";
|
|
12
13
|
export type { Cost, ModelPrice, Pricing } from "./cost.js";
|
|
13
14
|
export * as errors from "./errors.js";
|
|
14
15
|
export type { ExtractRequest, ExtractResult } from "./extract.js";
|
|
16
|
+
export { JSON_SCHEMA_KEYWORDS, jsonSchema } from "./json-schema.js";
|
|
15
17
|
export type { ExchangeEntry, ExchangeLog } from "./log.js";
|
|
16
18
|
export {
|
|
17
19
|
type DeclaredProviders,
|
|
@@ -24,6 +26,7 @@ export {
|
|
|
24
26
|
type ClientOptions,
|
|
25
27
|
type ExtractOptions,
|
|
26
28
|
PolarisClient,
|
|
29
|
+
type RunEvent,
|
|
27
30
|
type RunOptions,
|
|
28
31
|
type RunRequest,
|
|
29
32
|
type RunResult,
|
|
@@ -34,6 +37,12 @@ export {
|
|
|
34
37
|
type OpenAICompatibleConfig,
|
|
35
38
|
providers,
|
|
36
39
|
} from "./providers.js";
|
|
40
|
+
export {
|
|
41
|
+
type RedisBudgetClient,
|
|
42
|
+
type RedisBudgetStoreOptions,
|
|
43
|
+
type RedisBudgetTransaction,
|
|
44
|
+
redisBudgetStore,
|
|
45
|
+
} from "./redis-budget.js";
|
|
37
46
|
export {
|
|
38
47
|
addUsage,
|
|
39
48
|
createResponse,
|
|
@@ -49,8 +58,12 @@ export type {
|
|
|
49
58
|
export {
|
|
50
59
|
ClientLeft,
|
|
51
60
|
type DoneEventData,
|
|
61
|
+
type RunDoneEventData,
|
|
62
|
+
type SseFailure,
|
|
52
63
|
type SseSink,
|
|
64
|
+
type StreamRunToResult,
|
|
53
65
|
type StreamToResult,
|
|
66
|
+
type ToolResultEventData,
|
|
54
67
|
} from "./sse.js";
|
|
55
68
|
export type {
|
|
56
69
|
FakeCall,
|
|
@@ -60,6 +73,9 @@ export type {
|
|
|
60
73
|
} from "./testing/FakeProvider.js";
|
|
61
74
|
export {
|
|
62
75
|
defineTool,
|
|
76
|
+
type ExecuteContext,
|
|
77
|
+
type JsonSchemaToolSpec,
|
|
78
|
+
MAX_RUN_TOOLS,
|
|
63
79
|
type PolarisTool,
|
|
64
80
|
type ToolContext,
|
|
65
81
|
ToolError,
|