@c9up/polaris 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +542 -2
- package/dist/Polaris.d.ts +76 -0
- package/dist/Polaris.d.ts.map +1 -0
- package/dist/Polaris.js +194 -0
- package/dist/Polaris.js.map +1 -0
- package/dist/PolarisClient.d.ts +136 -0
- package/dist/PolarisClient.d.ts.map +1 -0
- package/dist/PolarisClient.js +408 -0
- package/dist/PolarisClient.js.map +1 -0
- package/dist/PolarisProvider.d.ts +35 -0
- package/dist/PolarisProvider.d.ts.map +1 -0
- package/dist/PolarisProvider.js +47 -0
- package/dist/PolarisProvider.js.map +1 -0
- package/dist/augmentations.d.ts +15 -0
- package/dist/augmentations.d.ts.map +1 -0
- package/dist/augmentations.js +9 -0
- package/dist/augmentations.js.map +1 -0
- package/dist/budget.d.ts +44 -0
- package/dist/budget.d.ts.map +1 -0
- package/dist/budget.js +67 -0
- package/dist/budget.js.map +1 -0
- package/dist/cost.d.ts +48 -0
- package/dist/cost.d.ts.map +1 -0
- package/dist/cost.js +126 -0
- package/dist/cost.js.map +1 -0
- package/dist/drivers/anthropic.d.ts +39 -0
- package/dist/drivers/anthropic.d.ts.map +1 -0
- package/dist/drivers/anthropic.js +394 -0
- package/dist/drivers/anthropic.js.map +1 -0
- package/dist/drivers/openai-compatible.d.ts +54 -0
- package/dist/drivers/openai-compatible.d.ts.map +1 -0
- package/dist/drivers/openai-compatible.js +663 -0
- package/dist/drivers/openai-compatible.js.map +1 -0
- package/dist/drivers/status.d.ts +8 -0
- package/dist/drivers/status.d.ts.map +1 -0
- package/dist/drivers/status.js +21 -0
- package/dist/drivers/status.js.map +1 -0
- package/dist/errors.d.ts +55 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +62 -0
- package/dist/errors.js.map +1 -0
- package/dist/extract.d.ts +38 -0
- package/dist/extract.d.ts.map +1 -0
- package/dist/extract.js +144 -0
- package/dist/extract.js.map +1 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +13 -0
- package/dist/index.js.map +1 -0
- package/dist/log.d.ts +47 -0
- package/dist/log.d.ts.map +1 -0
- package/dist/log.js +33 -0
- package/dist/log.js.map +1 -0
- package/dist/meter.d.ts +57 -0
- package/dist/meter.d.ts.map +1 -0
- package/dist/meter.js +91 -0
- package/dist/meter.js.map +1 -0
- package/dist/providers.d.ts +23 -0
- package/dist/providers.d.ts.map +1 -0
- package/dist/providers.js +21 -0
- package/dist/providers.js.map +1 -0
- package/dist/redact.d.ts +11 -0
- package/dist/redact.d.ts.map +1 -0
- package/dist/redact.js +76 -0
- package/dist/redact.js.map +1 -0
- package/dist/response.d.ts +21 -0
- package/dist/response.d.ts.map +1 -0
- package/dist/response.js +45 -0
- package/dist/response.js.map +1 -0
- package/dist/schema.d.ts +45 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +43 -0
- package/dist/schema.js.map +1 -0
- package/dist/services/main.d.ts +23 -0
- package/dist/services/main.d.ts.map +1 -0
- package/dist/services/main.js +43 -0
- package/dist/services/main.js.map +1 -0
- package/dist/sse.d.ts +58 -0
- package/dist/sse.d.ts.map +1 -0
- package/dist/sse.js +91 -0
- package/dist/sse.js.map +1 -0
- package/dist/testing/FakeProvider.d.ts +85 -0
- package/dist/testing/FakeProvider.d.ts.map +1 -0
- package/dist/testing/FakeProvider.js +213 -0
- package/dist/testing/FakeProvider.js.map +1 -0
- package/dist/testing/index.d.ts +7 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +7 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/tools.d.ts +68 -0
- package/dist/tools.d.ts.map +1 -0
- package/dist/tools.js +107 -0
- package/dist/tools.js.map +1 -0
- package/dist/transport.d.ts +18 -0
- package/dist/transport.d.ts.map +1 -0
- package/dist/transport.js +68 -0
- package/dist/transport.js.map +1 -0
- package/dist/types.d.ts +293 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +12 -0
- package/dist/types.js.map +1 -0
- package/package.json +76 -3
- package/src/Polaris.ts +280 -0
- package/src/PolarisClient.ts +591 -0
- package/src/PolarisProvider.ts +73 -0
- package/src/augmentations.ts +17 -0
- package/src/budget.ts +101 -0
- package/src/cost.ts +170 -0
- package/src/drivers/anthropic.ts +520 -0
- package/src/drivers/openai-compatible.ts +840 -0
- package/src/drivers/status.ts +30 -0
- package/src/errors.ts +161 -0
- package/src/extract.ts +214 -0
- package/src/index.ts +69 -0
- package/src/log.ts +84 -0
- package/src/meter.ts +188 -0
- package/src/providers.ts +25 -0
- package/src/redact.ts +78 -0
- package/src/response.ts +65 -0
- package/src/schema.ts +88 -0
- package/src/services/main.ts +49 -0
- package/src/sse.ts +128 -0
- package/src/testing/FakeProvider.ts +321 -0
- package/src/testing/index.ts +12 -0
- package/src/tools.ts +166 -0
- package/src/transport.ts +85 -0
- package/src/types.ts +328 -0
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
import { estimateChat, estimateEmbed } from "./cost.js";
|
|
2
|
+
import { E_POLARIS_INVALID_CONFIG, E_POLARIS_INVALID_REQUEST, E_POLARIS_INVALID_RESPONSE, E_POLARIS_STREAM_INCOMPLETE, E_POLARIS_TIMEOUT, E_POLARIS_UNSUPPORTED, } from "./errors.js";
|
|
3
|
+
import { extract } from "./extract.js";
|
|
4
|
+
import { addCost, openTicket } from "./meter.js";
|
|
5
|
+
import { scrub } from "./redact.js";
|
|
6
|
+
import { addUsage, NO_USAGE } from "./response.js";
|
|
7
|
+
import { ClientLeft, pipeToSse, } from "./sse.js";
|
|
8
|
+
import { runToolCalls, tooManySteps } from "./tools.js";
|
|
9
|
+
import { boundFetch, untilAborted } from "./transport.js";
|
|
10
|
+
const DEFAULT_MAX_STEPS = 8;
|
|
11
|
+
/** `setTimeout`'s ceiling: a longer delay fires at once. */
|
|
12
|
+
export const MAX_TIMEOUT = 2_147_483_647;
|
|
13
|
+
/** Shorter than this, a string is not a key, and redacting it would garble every message. */
|
|
14
|
+
const MIN_KEY_LENGTH = 8;
|
|
15
|
+
/** Whether `value` is a usable deadline in ms. */
|
|
16
|
+
export function isTimeout(value) {
|
|
17
|
+
return (typeof value === "number" &&
|
|
18
|
+
Number.isInteger(value) &&
|
|
19
|
+
value > 0 &&
|
|
20
|
+
value <= MAX_TIMEOUT);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* One configured provider: what `polaris.use(name)` returns.
|
|
24
|
+
*
|
|
25
|
+
* Everything a request asks for is checked against what the driver declares
|
|
26
|
+
* before anything is sent, so an unsupported feature fails the same way on
|
|
27
|
+
* every provider. Every call runs under a deadline and a response size bound,
|
|
28
|
+
* and an error that mentions the call's own key is replaced before it leaves.
|
|
29
|
+
*/
|
|
30
|
+
export class PolarisClient {
|
|
31
|
+
#name;
|
|
32
|
+
#driver;
|
|
33
|
+
#options;
|
|
34
|
+
constructor(name, driver, options) {
|
|
35
|
+
this.#name = name;
|
|
36
|
+
this.#driver = driver;
|
|
37
|
+
this.#options = options;
|
|
38
|
+
}
|
|
39
|
+
/** The name this provider has in the config. */
|
|
40
|
+
get name() {
|
|
41
|
+
return this.#name;
|
|
42
|
+
}
|
|
43
|
+
/** The provider behind it, as its driver names itself. */
|
|
44
|
+
get provider() {
|
|
45
|
+
return this.#driver.provider;
|
|
46
|
+
}
|
|
47
|
+
get features() {
|
|
48
|
+
return {
|
|
49
|
+
...this.#driver.features,
|
|
50
|
+
streaming: this.#driver.stream !== undefined,
|
|
51
|
+
embeddings: this.#driver.embed !== undefined,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/** Send a request and wait for the whole reply. */
|
|
55
|
+
async chat(request, options = {}) {
|
|
56
|
+
this.#checkChat(request);
|
|
57
|
+
const prepared = this.#prepare(options, undefined);
|
|
58
|
+
const ticket = await this.#openChat("chat", request, options, false);
|
|
59
|
+
let response;
|
|
60
|
+
try {
|
|
61
|
+
response = await untilAborted(this.#driver.chat(request, prepared.call), prepared.call.signal);
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
const translated = this.#translate(error, prepared, options);
|
|
65
|
+
await ticket.fail(translated, {
|
|
66
|
+
cancelled: options.signal?.aborted === true,
|
|
67
|
+
spent: false,
|
|
68
|
+
});
|
|
69
|
+
throw translated;
|
|
70
|
+
}
|
|
71
|
+
return this.#priced(response, ticket);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Send a request and read the reply as it is written. The stream ends with
|
|
75
|
+
* one `done` event holding the whole response. Leaving the loop early
|
|
76
|
+
* cancels the provider request.
|
|
77
|
+
*/
|
|
78
|
+
async *stream(request, options = {}) {
|
|
79
|
+
this.#checkChat(request);
|
|
80
|
+
const driver = this.#driver;
|
|
81
|
+
const open = driver.stream;
|
|
82
|
+
if (open === undefined) {
|
|
83
|
+
throw new E_POLARIS_UNSUPPORTED([driver.provider, "streaming"]);
|
|
84
|
+
}
|
|
85
|
+
const stop = new AbortController();
|
|
86
|
+
const prepared = this.#prepare(options, stop.signal);
|
|
87
|
+
const ticket = await this.#openChat("stream", request, options, true);
|
|
88
|
+
let finished = false;
|
|
89
|
+
let started = false;
|
|
90
|
+
let settled = false;
|
|
91
|
+
let iterator;
|
|
92
|
+
try {
|
|
93
|
+
iterator = open
|
|
94
|
+
.call(driver, request, prepared.call)[Symbol.asyncIterator]();
|
|
95
|
+
while (true) {
|
|
96
|
+
const next = await untilAborted(iterator.next(), prepared.call.signal);
|
|
97
|
+
if (next.done)
|
|
98
|
+
break;
|
|
99
|
+
if (finished) {
|
|
100
|
+
throw new E_POLARIS_INVALID_RESPONSE([
|
|
101
|
+
driver.provider,
|
|
102
|
+
"an event came after the final `done`",
|
|
103
|
+
]);
|
|
104
|
+
}
|
|
105
|
+
started = true;
|
|
106
|
+
if (next.value.type === "done") {
|
|
107
|
+
finished = true;
|
|
108
|
+
settled = true;
|
|
109
|
+
yield {
|
|
110
|
+
type: "done",
|
|
111
|
+
response: await this.#priced(next.value.response, ticket),
|
|
112
|
+
};
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
yield next.value;
|
|
116
|
+
}
|
|
117
|
+
if (!finished)
|
|
118
|
+
throw new E_POLARIS_STREAM_INCOMPLETE([driver.provider]);
|
|
119
|
+
}
|
|
120
|
+
catch (error) {
|
|
121
|
+
const translated = this.#translate(error, prepared, options);
|
|
122
|
+
settled = true;
|
|
123
|
+
await ticket.fail(translated, {
|
|
124
|
+
cancelled: options.signal?.aborted === true,
|
|
125
|
+
spent: started,
|
|
126
|
+
});
|
|
127
|
+
throw translated;
|
|
128
|
+
}
|
|
129
|
+
finally {
|
|
130
|
+
if (!finished)
|
|
131
|
+
stop.abort();
|
|
132
|
+
// A consumer that left early: the provider was asked, and may bill it.
|
|
133
|
+
if (!settled) {
|
|
134
|
+
await ticket.fail(new ClientLeft(), {
|
|
135
|
+
cancelled: true,
|
|
136
|
+
spent: started,
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
await iterator?.return?.().catch(() => undefined);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Send a request and run the tools the model calls, until it answers
|
|
144
|
+
* without calling one. Each turn's calls run at once and are answered in
|
|
145
|
+
* one message. Input that fails a tool's schema goes back to the model as
|
|
146
|
+
* an error it can correct. A reply cut at the output limit or refused is
|
|
147
|
+
* returned as it is: its tool calls may be incomplete.
|
|
148
|
+
*/
|
|
149
|
+
async run(request, options = {}) {
|
|
150
|
+
const maxSteps = options.maxSteps ?? DEFAULT_MAX_STEPS;
|
|
151
|
+
if (!isCount(maxSteps)) {
|
|
152
|
+
throw new E_POLARIS_INVALID_REQUEST([
|
|
153
|
+
"`maxSteps` must be a positive integer",
|
|
154
|
+
]);
|
|
155
|
+
}
|
|
156
|
+
const tools = new Map(request.tools.map((tool) => [tool.name, tool]));
|
|
157
|
+
const definitions = request.tools.map((tool) => tool.definition);
|
|
158
|
+
const signal = options.signal ?? new AbortController().signal;
|
|
159
|
+
const messages = [...request.messages];
|
|
160
|
+
let usage = NO_USAGE;
|
|
161
|
+
let cost;
|
|
162
|
+
let priced = true;
|
|
163
|
+
for (let step = 1; step <= maxSteps; step++) {
|
|
164
|
+
// A copy per request: the history grows after it is sent, and what a
|
|
165
|
+
// request carried must stay what it was (a log, a driver keeping it).
|
|
166
|
+
const response = await this.chat({ ...request, messages: [...messages], tools: definitions }, options);
|
|
167
|
+
usage = addUsage(usage, response.usage);
|
|
168
|
+
cost = addCost(cost, response.cost);
|
|
169
|
+
priced = priced && response.cost !== undefined;
|
|
170
|
+
messages.push(response.message);
|
|
171
|
+
// A paused turn goes back as it is, and the provider carries on.
|
|
172
|
+
if (response.stopReason === "pause")
|
|
173
|
+
continue;
|
|
174
|
+
if (response.stopReason !== "tool_use" ||
|
|
175
|
+
response.toolCalls.length === 0) {
|
|
176
|
+
return {
|
|
177
|
+
response,
|
|
178
|
+
messages,
|
|
179
|
+
steps: step,
|
|
180
|
+
usage,
|
|
181
|
+
...(priced && cost !== undefined ? { cost } : {}),
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
messages.push({
|
|
185
|
+
role: "user",
|
|
186
|
+
content: await runToolCalls(response.toolCalls, tools, signal, options.onToolError),
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
throw tooManySteps(maxSteps);
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Ask for one value matching `schema`, and get it typed and validated.
|
|
193
|
+
* The provider's structured-output mode is used when it has one, a tool
|
|
194
|
+
* otherwise. An answer that fails the schema goes back to the model with
|
|
195
|
+
* the schema's issues, `retries` times at most, then raises
|
|
196
|
+
* `E_POLARIS_INVALID_OUTPUT`.
|
|
197
|
+
*/
|
|
198
|
+
async extract(request, options = {}) {
|
|
199
|
+
const retries = options.retries ?? 1;
|
|
200
|
+
if (!(Number.isInteger(retries) && retries >= 0)) {
|
|
201
|
+
throw new E_POLARIS_INVALID_REQUEST([
|
|
202
|
+
"`retries` must be an integer, 0 or more",
|
|
203
|
+
]);
|
|
204
|
+
}
|
|
205
|
+
return extract(request, retries, { ...this.#driver.features, provider: this.#driver.provider }, (chat) => this.chat(chat, options));
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Stream a reply to the browser over an SSE stream, `ctx.response.sse()`
|
|
209
|
+
* in a ream route. Each event goes out named `text`, `reasoning`,
|
|
210
|
+
* `tool_call`, then `done`, and the stream is ended. When the client
|
|
211
|
+
* leaves, the provider request is cancelled. A failure sends an `error`
|
|
212
|
+
* event carrying only its polaris code. It never rejects: the outcome is
|
|
213
|
+
* in the result.
|
|
214
|
+
*
|
|
215
|
+
* In a ream route, do not await it: ream starts sending the response once
|
|
216
|
+
* the handler has returned, so an awaited stream would reach the client
|
|
217
|
+
* all at once at the end.
|
|
218
|
+
*
|
|
219
|
+
* router.post('/chat', async ({ response }) => {
|
|
220
|
+
* const sse = await response.sse()
|
|
221
|
+
* void polaris.use('claude').streamTo(sse, { messages }).then(logOutcome)
|
|
222
|
+
* })
|
|
223
|
+
*/
|
|
224
|
+
async streamTo(sink, request, options = {}) {
|
|
225
|
+
const leave = new AbortController();
|
|
226
|
+
sink.onClose(() => {
|
|
227
|
+
if (!leave.signal.aborted)
|
|
228
|
+
leave.abort(new ClientLeft());
|
|
229
|
+
});
|
|
230
|
+
const signal = options.signal
|
|
231
|
+
? AbortSignal.any([options.signal, leave.signal])
|
|
232
|
+
: leave.signal;
|
|
233
|
+
return pipeToSse(this.stream(request, { ...options, signal }), sink, leave, options.prefix ?? "");
|
|
234
|
+
}
|
|
235
|
+
/** Turn texts into vectors, one per input, in input order. */
|
|
236
|
+
async embed(request, options = {}) {
|
|
237
|
+
const driver = this.#driver;
|
|
238
|
+
const embed = driver.embed;
|
|
239
|
+
if (embed === undefined) {
|
|
240
|
+
throw new E_POLARIS_UNSUPPORTED([driver.provider, "embeddings"]);
|
|
241
|
+
}
|
|
242
|
+
if (request.inputs.length === 0) {
|
|
243
|
+
throw new E_POLARIS_INVALID_REQUEST(["`inputs` is empty"]);
|
|
244
|
+
}
|
|
245
|
+
if (request.dimensions !== undefined && !isCount(request.dimensions)) {
|
|
246
|
+
throw new E_POLARIS_INVALID_REQUEST([
|
|
247
|
+
"`dimensions` must be a positive integer",
|
|
248
|
+
]);
|
|
249
|
+
}
|
|
250
|
+
const prepared = this.#prepare(options, undefined);
|
|
251
|
+
const ticket = await openTicket(this.#options.meter, {
|
|
252
|
+
operation: "embed",
|
|
253
|
+
provider: driver.provider,
|
|
254
|
+
request,
|
|
255
|
+
model: request.model ?? driver.defaults?.embeddingModel,
|
|
256
|
+
estimate: (price) => estimateEmbed(price, request),
|
|
257
|
+
...(options.scope !== undefined ? { scope: options.scope } : {}),
|
|
258
|
+
...(options.maxCost !== undefined ? { maxCost: options.maxCost } : {}),
|
|
259
|
+
});
|
|
260
|
+
let response;
|
|
261
|
+
try {
|
|
262
|
+
response = await untilAborted(embed.call(driver, request, prepared.call), prepared.call.signal);
|
|
263
|
+
}
|
|
264
|
+
catch (error) {
|
|
265
|
+
const translated = this.#translate(error, prepared, options);
|
|
266
|
+
await ticket.fail(translated, {
|
|
267
|
+
cancelled: options.signal?.aborted === true,
|
|
268
|
+
spent: false,
|
|
269
|
+
});
|
|
270
|
+
throw translated;
|
|
271
|
+
}
|
|
272
|
+
if (response.vectors.length !== request.inputs.length) {
|
|
273
|
+
throw new E_POLARIS_INVALID_RESPONSE([
|
|
274
|
+
driver.provider,
|
|
275
|
+
`${response.vectors.length} vectors for ${request.inputs.length} inputs`,
|
|
276
|
+
]);
|
|
277
|
+
}
|
|
278
|
+
if (!isCount(response.dimensions) ||
|
|
279
|
+
response.vectors.some((vector) => vector.length !== response.dimensions)) {
|
|
280
|
+
throw new E_POLARIS_INVALID_RESPONSE([
|
|
281
|
+
driver.provider,
|
|
282
|
+
`vectors do not all have the announced ${response.dimensions} dimensions`,
|
|
283
|
+
]);
|
|
284
|
+
}
|
|
285
|
+
const cost = await ticket.finish({
|
|
286
|
+
model: response.model,
|
|
287
|
+
usage: { ...NO_USAGE, inputTokens: response.usage.inputTokens },
|
|
288
|
+
response,
|
|
289
|
+
});
|
|
290
|
+
return cost !== undefined ? { ...response, cost } : response;
|
|
291
|
+
}
|
|
292
|
+
/** Open the meter of a chat request: price it, check its budget, reserve. */
|
|
293
|
+
#openChat(operation, request, options, streaming) {
|
|
294
|
+
const defaults = this.#driver.defaults;
|
|
295
|
+
const maxTokens = request.maxTokens ??
|
|
296
|
+
(streaming ? defaults?.streamMaxTokens : defaults?.maxTokens);
|
|
297
|
+
return openTicket(this.#options.meter, {
|
|
298
|
+
operation,
|
|
299
|
+
provider: this.#driver.provider,
|
|
300
|
+
request,
|
|
301
|
+
model: request.model ?? defaults?.model,
|
|
302
|
+
estimate: (price) => {
|
|
303
|
+
if (maxTokens === undefined) {
|
|
304
|
+
throw new E_POLARIS_INVALID_REQUEST([
|
|
305
|
+
"a budget applies: set `maxTokens`, the provider declares no default",
|
|
306
|
+
]);
|
|
307
|
+
}
|
|
308
|
+
return estimateChat(price, request, maxTokens);
|
|
309
|
+
},
|
|
310
|
+
...(options.scope !== undefined ? { scope: options.scope } : {}),
|
|
311
|
+
...(options.maxCost !== undefined ? { maxCost: options.maxCost } : {}),
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
/** Close the meter of an answered chat request, and put its cost on it. */
|
|
315
|
+
async #priced(response, ticket) {
|
|
316
|
+
const cost = await ticket.finish({
|
|
317
|
+
model: response.model,
|
|
318
|
+
usage: response.usage,
|
|
319
|
+
responseId: response.id,
|
|
320
|
+
stopReason: response.stopReason,
|
|
321
|
+
response,
|
|
322
|
+
});
|
|
323
|
+
return cost !== undefined ? { ...response, cost } : response;
|
|
324
|
+
}
|
|
325
|
+
#checkChat(request) {
|
|
326
|
+
const provider = this.#driver.provider;
|
|
327
|
+
const features = this.#driver.features;
|
|
328
|
+
if (request.messages.length === 0) {
|
|
329
|
+
throw new E_POLARIS_INVALID_REQUEST(["`messages` is empty"]);
|
|
330
|
+
}
|
|
331
|
+
if (request.maxTokens !== undefined && !isCount(request.maxTokens)) {
|
|
332
|
+
throw new E_POLARIS_INVALID_REQUEST([
|
|
333
|
+
"`maxTokens` must be a positive integer",
|
|
334
|
+
]);
|
|
335
|
+
}
|
|
336
|
+
const tools = request.tools ?? [];
|
|
337
|
+
if (tools.length > 0) {
|
|
338
|
+
if (!features.tools) {
|
|
339
|
+
throw new E_POLARIS_UNSUPPORTED([provider, "tool calls"]);
|
|
340
|
+
}
|
|
341
|
+
const names = new Set();
|
|
342
|
+
for (const tool of tools) {
|
|
343
|
+
if (names.has(tool.name)) {
|
|
344
|
+
throw new E_POLARIS_INVALID_REQUEST([
|
|
345
|
+
`two tools are named '${tool.name}'`,
|
|
346
|
+
]);
|
|
347
|
+
}
|
|
348
|
+
names.add(tool.name);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
if (request.output !== undefined && !features.structuredOutput) {
|
|
352
|
+
throw new E_POLARIS_UNSUPPORTED([provider, "structured output"]);
|
|
353
|
+
}
|
|
354
|
+
if (!features.images && sendsImages(request)) {
|
|
355
|
+
throw new E_POLARIS_UNSUPPORTED([provider, "images"]);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
#prepare(options, stop) {
|
|
359
|
+
const timeout = options.timeout ?? this.#options.timeout;
|
|
360
|
+
if (!isTimeout(timeout)) {
|
|
361
|
+
throw new E_POLARIS_INVALID_CONFIG([
|
|
362
|
+
`timeout must be a positive integer of ms, at most ${MAX_TIMEOUT}`,
|
|
363
|
+
]);
|
|
364
|
+
}
|
|
365
|
+
const apiKey = options.apiKey;
|
|
366
|
+
if (apiKey !== undefined &&
|
|
367
|
+
(apiKey.length < MIN_KEY_LENGTH || apiKey.trim() !== apiKey)) {
|
|
368
|
+
throw new E_POLARIS_INVALID_REQUEST([
|
|
369
|
+
`\`apiKey\` must be at least ${MIN_KEY_LENGTH} characters, without surrounding spaces`,
|
|
370
|
+
]);
|
|
371
|
+
}
|
|
372
|
+
if (options.signal?.aborted)
|
|
373
|
+
throw options.signal.reason;
|
|
374
|
+
const deadline = AbortSignal.timeout(timeout);
|
|
375
|
+
const signals = [deadline];
|
|
376
|
+
if (options.signal)
|
|
377
|
+
signals.push(options.signal);
|
|
378
|
+
if (stop)
|
|
379
|
+
signals.push(stop);
|
|
380
|
+
const signal = AbortSignal.any(signals);
|
|
381
|
+
return {
|
|
382
|
+
deadline,
|
|
383
|
+
timeout,
|
|
384
|
+
call: {
|
|
385
|
+
signal,
|
|
386
|
+
apiKey,
|
|
387
|
+
fetch: boundFetch(this.#options.fetch, signal, this.#options.maxResponseBytes),
|
|
388
|
+
},
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
/** The error a failed call raises. */
|
|
392
|
+
#translate(error, prepared, options) {
|
|
393
|
+
if (options.signal?.aborted)
|
|
394
|
+
return options.signal.reason;
|
|
395
|
+
if (prepared.deadline.aborted)
|
|
396
|
+
return new E_POLARIS_TIMEOUT([prepared.timeout]);
|
|
397
|
+
return scrub(error, this.#driver.provider, [prepared.call.apiKey]);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
function isCount(value) {
|
|
401
|
+
return Number.isInteger(value) && value > 0;
|
|
402
|
+
}
|
|
403
|
+
function sendsImages(request) {
|
|
404
|
+
return request.messages.some((message) => message.role === "user" &&
|
|
405
|
+
typeof message.content !== "string" &&
|
|
406
|
+
message.content.some((part) => part.type === "image"));
|
|
407
|
+
}
|
|
408
|
+
//# sourceMappingURL=PolarisClient.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PolarisClient.js","sourceRoot":"","sources":["../src/PolarisClient.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AACxD,OAAO,EACN,wBAAwB,EACxB,yBAAyB,EACzB,0BAA0B,EAC1B,2BAA2B,EAC3B,iBAAiB,EACj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C,CAAC;QACxC,OAAO;YACN,QAAQ;YACR,OAAO;YACP,IAAI,EAAE;gBACL,MAAM;gBACN,MAAM;gBACN,KAAK,EAAE,UAAU,CAChB,IAAI,CAAC,QAAQ,CAAC,KAAK,EACnB,MAAM,EACN,IAAI,CAAC,QAAQ,CAAC,gBAAgB,CAC9B;aACD;SACD,CAAC;IACH,CAAC;IAED,sCAAsC;IACtC,UAAU,CACT,KAAc,EACd,QAAsB,EACtB,OAAoB;QAEpB,IAAI,OAAO,CAAC,MAAM,EAAE,OAAO;YAAE,OAAO,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC;QAC1D,IAAI,QAAQ,CAAC,QAAQ,CAAC,OAAO;YAC5B,OAAO,IAAI,iBAAiB,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC;QAClD,OAAO,KAAK,CAAC,KAAK,EAAE,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;IACpE,CAAC;CACD;AAED,SAAS,OAAO,CAAC,KAAa;IAC7B,OAAO,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC,CAAC;AAC7C,CAAC;AAED,SAAS,WAAW,CAAC,OAAoB;IACxC,OAAO,OAAO,CAAC,QAAQ,CAAC,IAAI,CAC3B,CAAC,OAAO,EAAE,EAAE,CACX,OAAO,CAAC,IAAI,KAAK,MAAM;QACvB,OAAO,OAAO,CAAC,OAAO,KAAK,QAAQ;QACnC,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,KAAK,OAAO,CAAC,CACtD,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PolarisProvider: binds the provider manager built from `config/polaris.ts`.
|
|
3
|
+
*
|
|
4
|
+
* register() binds `polaris.manager`, built on first resolve, unless the
|
|
5
|
+
* application bound its own first.
|
|
6
|
+
* boot() builds it and publishes it to `@c9up/polaris/services/main`;
|
|
7
|
+
* a missing or invalid config fails the boot here.
|
|
8
|
+
* shutdown() releases the published manager, if it is still this one.
|
|
9
|
+
*
|
|
10
|
+
* Typed against the slice of ream's `AppContext` it uses rather than importing
|
|
11
|
+
* it, so ream stays an optional peer.
|
|
12
|
+
*/
|
|
13
|
+
import "./augmentations.js";
|
|
14
|
+
interface ContainerLike {
|
|
15
|
+
singleton(token: string | symbol, factory: () => unknown): void;
|
|
16
|
+
/** Async, as in ream: a factory may be async. */
|
|
17
|
+
resolve<T = unknown>(token: string | symbol): Promise<T>;
|
|
18
|
+
has?(token: string | symbol): boolean;
|
|
19
|
+
}
|
|
20
|
+
interface ConfigStoreLike {
|
|
21
|
+
get<T = unknown>(key: string): T | undefined;
|
|
22
|
+
}
|
|
23
|
+
export interface PolarisAppContext {
|
|
24
|
+
container: ContainerLike;
|
|
25
|
+
config: ConfigStoreLike;
|
|
26
|
+
}
|
|
27
|
+
export default class PolarisProvider {
|
|
28
|
+
#private;
|
|
29
|
+
constructor(app: PolarisAppContext);
|
|
30
|
+
register(): void;
|
|
31
|
+
boot(): Promise<void>;
|
|
32
|
+
shutdown(): Promise<void>;
|
|
33
|
+
}
|
|
34
|
+
export {};
|
|
35
|
+
//# sourceMappingURL=PolarisProvider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PolarisProvider.d.ts","sourceRoot":"","sources":["../src/PolarisProvider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,oBAAoB,CAAC;AAY5B,UAAU,aAAa;IACtB,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,OAAO,EAAE,MAAM,OAAO,GAAG,IAAI,CAAC;IAChE,iDAAiD;IACjD,OAAO,CAAC,CAAC,GAAG,OAAO,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;IACzD,GAAG,CAAC,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC;CACtC;AAED,UAAU,eAAe;IACxB,GAAG,CAAC,CAAC,GAAG,OAAO,EAAE,GAAG,EAAE,MAAM,GAAG,CAAC,GAAG,SAAS,CAAC;CAC7C;AAED,MAAM,WAAW,iBAAiB;IACjC,SAAS,EAAE,aAAa,CAAC;IACzB,MAAM,EAAE,eAAe,CAAC;CACxB;AAED,MAAM,CAAC,OAAO,OAAO,eAAe;;gBAIvB,GAAG,EAAE,iBAAiB;IAIlC,QAAQ,IAAI,IAAI;IAWV,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAMrB,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;CAM/B"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PolarisProvider: binds the provider manager built from `config/polaris.ts`.
|
|
3
|
+
*
|
|
4
|
+
* register() binds `polaris.manager`, built on first resolve, unless the
|
|
5
|
+
* application bound its own first.
|
|
6
|
+
* boot() builds it and publishes it to `@c9up/polaris/services/main`;
|
|
7
|
+
* a missing or invalid config fails the boot here.
|
|
8
|
+
* shutdown() releases the published manager, if it is still this one.
|
|
9
|
+
*
|
|
10
|
+
* Typed against the slice of ream's `AppContext` it uses rather than importing
|
|
11
|
+
* it, so ream stays an optional peer.
|
|
12
|
+
*/
|
|
13
|
+
import "./augmentations.js";
|
|
14
|
+
import { E_POLARIS_MISSING_CONFIG } from "./errors.js";
|
|
15
|
+
import { Polaris, } from "./Polaris.js";
|
|
16
|
+
import { clearPolaris, getPolaris, setPolaris } from "./services/main.js";
|
|
17
|
+
const TOKEN = "polaris.manager";
|
|
18
|
+
export default class PolarisProvider {
|
|
19
|
+
#app;
|
|
20
|
+
#owned;
|
|
21
|
+
constructor(app) {
|
|
22
|
+
this.#app = app;
|
|
23
|
+
}
|
|
24
|
+
register() {
|
|
25
|
+
const container = this.#app.container;
|
|
26
|
+
if (container.has?.(TOKEN))
|
|
27
|
+
return;
|
|
28
|
+
container.singleton(TOKEN, () => {
|
|
29
|
+
const config = this.#app.config.get("polaris");
|
|
30
|
+
if (config === undefined)
|
|
31
|
+
throw new E_POLARIS_MISSING_CONFIG();
|
|
32
|
+
return new Polaris(config);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
async boot() {
|
|
36
|
+
const manager = await this.#app.container.resolve(TOKEN);
|
|
37
|
+
this.#owned = manager;
|
|
38
|
+
setPolaris(manager);
|
|
39
|
+
}
|
|
40
|
+
async shutdown() {
|
|
41
|
+
if (this.#owned !== undefined && getPolaris() === this.#owned) {
|
|
42
|
+
clearPolaris();
|
|
43
|
+
}
|
|
44
|
+
this.#owned = undefined;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=PolarisProvider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PolarisProvider.js","sourceRoot":"","sources":["../src/PolarisProvider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,oBAAoB,CAAC;AAC5B,OAAO,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAC;AACvD,OAAO,EAEN,OAAO,GAEP,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAG1E,MAAM,KAAK,GAAG,iBAAiB,CAAC;AAkBhC,MAAM,CAAC,OAAO,OAAO,eAAe;IAC1B,IAAI,CAAoB;IACjC,MAAM,CAA6B;IAEnC,YAAY,GAAsB;QACjC,IAAI,CAAC,IAAI,GAAG,GAAG,CAAC;IACjB,CAAC;IAED,QAAQ;QACP,MAAM,SAAS,GAAG,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC;QACtC,IAAI,SAAS,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC;YAAE,OAAO;QACnC,SAAS,CAAC,SAAS,CAAC,KAAK,EAAE,GAAG,EAAE;YAC/B,MAAM,MAAM,GACX,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAmC,SAAS,CAAC,CAAC;YACnE,IAAI,MAAM,KAAK,SAAS;gBAAE,MAAM,IAAI,wBAAwB,EAAE,CAAC;YAC/D,OAAO,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;QAC5B,CAAC,CAAC,CAAC;IACJ,CAAC;IAED,KAAK,CAAC,IAAI;QACT,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAiB,KAAK,CAAC,CAAC;QACzE,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC;QACtB,UAAU,CAAC,OAAO,CAAC,CAAC;IACrB,CAAC;IAED,KAAK,CAAC,QAAQ;QACb,IAAI,IAAI,CAAC,MAAM,KAAK,SAAS,IAAI,UAAU,EAAE,KAAK,IAAI,CAAC,MAAM,EAAE,CAAC;YAC/D,YAAY,EAAE,CAAC;QAChB,CAAC;QACD,IAAI,CAAC,MAAM,GAAG,SAAS,CAAC;IACzB,CAAC;CACD"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tell ream's `ContainerBindings` what `container.resolve('polaris.manager')`
|
|
3
|
+
* returns. Loaded from the package barrel and from the provider, so an
|
|
4
|
+
* application writes no `declare module` of its own for it.
|
|
5
|
+
*
|
|
6
|
+
* Type-only: ream stays an optional peer.
|
|
7
|
+
*/
|
|
8
|
+
import type { PolarisService } from "./Polaris.js";
|
|
9
|
+
declare module "@c9up/ream/types" {
|
|
10
|
+
interface ContainerBindings {
|
|
11
|
+
/** The provider manager, bound by `PolarisProvider`. */
|
|
12
|
+
"polaris.manager": PolarisService;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=augmentations.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"augmentations.d.ts","sourceRoot":"","sources":["../src/augmentations.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAEnD,OAAO,QAAQ,kBAAkB,CAAC;IACjC,UAAU,iBAAiB;QAC1B,wDAAwD;QACxD,iBAAiB,EAAE,cAAc,CAAC;KAClC;CACD"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tell ream's `ContainerBindings` what `container.resolve('polaris.manager')`
|
|
3
|
+
* returns. Loaded from the package barrel and from the provider, so an
|
|
4
|
+
* application writes no `declare module` of its own for it.
|
|
5
|
+
*
|
|
6
|
+
* Type-only: ream stays an optional peer.
|
|
7
|
+
*/
|
|
8
|
+
export {};
|
|
9
|
+
//# sourceMappingURL=augmentations.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"augmentations.js","sourceRoot":"","sources":["../src/augmentations.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG"}
|
package/dist/budget.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Spending limits per scope (a user, a tenant), enforced before a request is
|
|
3
|
+
* sent.
|
|
4
|
+
*
|
|
5
|
+
* A request reserves its estimated cost first: the reservation is added to
|
|
6
|
+
* the scope's spend, and if the total goes past the limit the reservation is
|
|
7
|
+
* taken back and the request refused, before anything leaves the server.
|
|
8
|
+
* Once the reply is in, the reservation is replaced by the real cost.
|
|
9
|
+
* Reserving rather than reading the spend is what keeps two requests sent at
|
|
10
|
+
* the same moment from passing the limit together.
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Where spend is kept. `add` must be atomic (a Redis `INCRBYFLOAT`, an SQL
|
|
14
|
+
* `UPDATE … SET spent = spent + ?`): it is what reservations rely on.
|
|
15
|
+
*/
|
|
16
|
+
export interface BudgetStore {
|
|
17
|
+
/** Add `amount` (a decimal string, possibly negative) to `scope`, and answer the new total. */
|
|
18
|
+
add(scope: string, amount: string): Promise<string>;
|
|
19
|
+
}
|
|
20
|
+
export interface BudgetConfig {
|
|
21
|
+
/** The limit of a scope, as a decimal string in the pricing currency; `undefined` for none. */
|
|
22
|
+
readonly limit: {
|
|
23
|
+
readonly [scope: string]: string;
|
|
24
|
+
} | ((scope: string) => string | undefined | Promise<string | undefined>);
|
|
25
|
+
/** Where spend is kept. Default: in memory, for one process and its lifetime. */
|
|
26
|
+
readonly store?: BudgetStore;
|
|
27
|
+
}
|
|
28
|
+
/** A spend store in memory: one process, lost on restart. For tests and a single instance. */
|
|
29
|
+
export declare function memoryBudgetStore(): BudgetStore;
|
|
30
|
+
/** What a reservation hands back to settle it. */
|
|
31
|
+
export interface Reservation {
|
|
32
|
+
/** Replace the reservation by what the request really cost. */
|
|
33
|
+
settle(cost: string): Promise<void>;
|
|
34
|
+
/** Take the whole reservation back (the request was never answered). */
|
|
35
|
+
release(): Promise<void>;
|
|
36
|
+
}
|
|
37
|
+
/** Resolve the limit of `scope`. */
|
|
38
|
+
export declare function limitOf(config: BudgetConfig, scope: string): Promise<string | undefined>;
|
|
39
|
+
/**
|
|
40
|
+
* Reserve `estimate` on `scope`, or refuse with `E_POLARIS_BUDGET_EXCEEDED`
|
|
41
|
+
* when that would go past `limit`. With no limit, nothing is reserved.
|
|
42
|
+
*/
|
|
43
|
+
export declare function reserve(store: BudgetStore, scope: string, limit: string | undefined, estimate: string): Promise<Reservation>;
|
|
44
|
+
//# sourceMappingURL=budget.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"budget.d.ts","sourceRoot":"","sources":["../src/budget.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAKH;;;GAGG;AACH,MAAM,WAAW,WAAW;IAC3B,+FAA+F;IAC/F,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;CACpD;AAED,MAAM,WAAW,YAAY;IAC5B,+FAA+F;IAC/F,QAAQ,CAAC,KAAK,EACX;QAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,GACpC,CAAC,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM,GAAG,SAAS,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC,CAAC;IACzE,iFAAiF;IACjF,QAAQ,CAAC,KAAK,CAAC,EAAE,WAAW,CAAC;CAC7B;AAED,8FAA8F;AAC9F,wBAAgB,iBAAiB,IAAI,WAAW,CAS/C;AAED,kDAAkD;AAClD,MAAM,WAAW,WAAW;IAC3B,+DAA+D;IAC/D,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACpC,wEAAwE;IACxE,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACzB;AAOD,oCAAoC;AACpC,wBAAsB,OAAO,CAC5B,MAAM,EAAE,YAAY,EACpB,KAAK,EAAE,MAAM,GACX,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAM7B;AAED;;;GAGG;AACH,wBAAsB,OAAO,CAC5B,KAAK,EAAE,WAAW,EAClB,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,GAAG,SAAS,EACzB,QAAQ,EAAE,MAAM,GACd,OAAO,CAAC,WAAW,CAAC,CAqBtB"}
|