@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
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The worker protocol: how `ream mcp:serve` has the application's tools run
|
|
3
|
+
* by a long-lived Node process. One JSON object per line, both ways.
|
|
4
|
+
*
|
|
5
|
+
* Started as `node <worker> <tools.json>`, the worker imports the module the
|
|
6
|
+
* manifest names, then writes `{"type":"ready","protocol":1}`; when it cannot,
|
|
7
|
+
* `{"type":"failed","message":"..."}`, and it exits with code 1.
|
|
8
|
+
*
|
|
9
|
+
* Then, from the parent:
|
|
10
|
+
* {"type":"call","id":1,"name":"canton_rate","input":{...}}
|
|
11
|
+
* {"type":"cancel","id":1}
|
|
12
|
+
* and from the worker:
|
|
13
|
+
* {"type":"result","id":1,"ok":true,"content":"..."}
|
|
14
|
+
* {"type":"error","id":null,"message":"..."} a line it could not read
|
|
15
|
+
*
|
|
16
|
+
* Calls run side by side; each is answered once. `ok: false` is a failure
|
|
17
|
+
* meant for the agent (unknown tool, refused input, the tool's error), never
|
|
18
|
+
* a stack trace. The worker leaves when its stdin ends. stdout carries only
|
|
19
|
+
* the protocol: what the application writes there goes to stderr.
|
|
20
|
+
*/
|
|
21
|
+
import { resolve } from "node:path";
|
|
22
|
+
import { pathToFileURL } from "node:url";
|
|
23
|
+
import { loadTools, ToolHost } from "./host.js";
|
|
24
|
+
import { isObject } from "./rpc.js";
|
|
25
|
+
/** The version of the protocol above. */
|
|
26
|
+
export const WORKER_PROTOCOL = 1;
|
|
27
|
+
/** The longest line the worker reads, in bytes. */
|
|
28
|
+
export const MAX_WORKER_LINE = 4 * 1024 * 1024;
|
|
29
|
+
/**
|
|
30
|
+
* Serve the protocol until `input` ends. Writes `ready` first. Resolves once
|
|
31
|
+
* the input ended and every call was answered.
|
|
32
|
+
*/
|
|
33
|
+
export async function runWorker(options) {
|
|
34
|
+
const host = new ToolHost(options.tools, options.onToolError);
|
|
35
|
+
const max = options.maxLineBytes ?? MAX_WORKER_LINE;
|
|
36
|
+
const send = (message) => options.write(JSON.stringify(message));
|
|
37
|
+
const running = new Map();
|
|
38
|
+
const settled = [];
|
|
39
|
+
const handle = (line) => {
|
|
40
|
+
let message;
|
|
41
|
+
try {
|
|
42
|
+
message = JSON.parse(line);
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
send({ type: "error", id: null, message: "The line is not JSON." });
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
const id = isObject(message) ? message.id : undefined;
|
|
49
|
+
if (!isObject(message) ||
|
|
50
|
+
(typeof id !== "string" && typeof id !== "number")) {
|
|
51
|
+
send({ type: "error", id: null, message: "The message has no id." });
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
if (message.type === "cancel") {
|
|
55
|
+
running.get(id)?.abort();
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (message.type !== "call" || typeof message.name !== "string") {
|
|
59
|
+
send({
|
|
60
|
+
type: "error",
|
|
61
|
+
id,
|
|
62
|
+
message: "The message is neither a call nor a cancel.",
|
|
63
|
+
});
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (running.has(id)) {
|
|
67
|
+
send({ type: "error", id, message: "A call with this id is running." });
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
const controller = new AbortController();
|
|
71
|
+
running.set(id, controller);
|
|
72
|
+
settled.push(host
|
|
73
|
+
.call(message.name, message.input, { signal: controller.signal })
|
|
74
|
+
.then((result) => {
|
|
75
|
+
running.delete(id);
|
|
76
|
+
send({ type: "result", id, ...result });
|
|
77
|
+
}));
|
|
78
|
+
};
|
|
79
|
+
send({ type: "ready", protocol: WORKER_PROTOCOL });
|
|
80
|
+
const tooLong = () => send({
|
|
81
|
+
type: "error",
|
|
82
|
+
id: null,
|
|
83
|
+
message: `The line is longer than ${max} bytes.`,
|
|
84
|
+
});
|
|
85
|
+
let pending = "";
|
|
86
|
+
let skipping = false;
|
|
87
|
+
options.input.setEncoding("utf8");
|
|
88
|
+
for await (const chunk of options.input) {
|
|
89
|
+
let text = pending + String(chunk);
|
|
90
|
+
pending = "";
|
|
91
|
+
let start = 0;
|
|
92
|
+
let newline = text.indexOf("\n");
|
|
93
|
+
while (newline !== -1) {
|
|
94
|
+
const line = text.slice(start, newline).trim();
|
|
95
|
+
if (skipping)
|
|
96
|
+
skipping = false;
|
|
97
|
+
else if (Buffer.byteLength(line, "utf8") > max)
|
|
98
|
+
tooLong();
|
|
99
|
+
else if (line !== "")
|
|
100
|
+
handle(line);
|
|
101
|
+
start = newline + 1;
|
|
102
|
+
newline = text.indexOf("\n", start);
|
|
103
|
+
}
|
|
104
|
+
text = text.slice(start);
|
|
105
|
+
if (skipping)
|
|
106
|
+
continue;
|
|
107
|
+
if (Buffer.byteLength(text, "utf8") > max) {
|
|
108
|
+
tooLong();
|
|
109
|
+
skipping = true;
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
pending = text;
|
|
113
|
+
}
|
|
114
|
+
if (!skipping && pending.trim() !== "")
|
|
115
|
+
handle(pending.trim());
|
|
116
|
+
await Promise.all(settled);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Read the manifest, import its tools, and serve the protocol on the
|
|
120
|
+
* process's stdin and stdout. What the application writes to stdout goes to
|
|
121
|
+
* stderr. A failure to start is written as `failed`, with exit code 1.
|
|
122
|
+
*/
|
|
123
|
+
export async function startWorker(manifestPath, { readFile, process: proc }) {
|
|
124
|
+
const out = proc.stdout.write.bind(proc.stdout);
|
|
125
|
+
const write = (line) => {
|
|
126
|
+
out(`${line}\n`);
|
|
127
|
+
};
|
|
128
|
+
// Only the protocol on stdout: console.log and the like go to stderr.
|
|
129
|
+
proc.stdout.write = proc.stderr.write.bind(proc.stderr);
|
|
130
|
+
const log = (text) => {
|
|
131
|
+
proc.stderr.write(`[polaris worker] ${text}\n`);
|
|
132
|
+
};
|
|
133
|
+
proc.on("unhandledRejection", (reason) => {
|
|
134
|
+
log(`unhandled rejection: ${reason instanceof Error ? reason.message : String(reason)}`);
|
|
135
|
+
});
|
|
136
|
+
let tools;
|
|
137
|
+
try {
|
|
138
|
+
if (manifestPath === undefined) {
|
|
139
|
+
throw new Error("usage: node worker-main.js <tools.json>");
|
|
140
|
+
}
|
|
141
|
+
const manifest = JSON.parse(await readFile(manifestPath));
|
|
142
|
+
if (!isObject(manifest) || typeof manifest.module !== "string") {
|
|
143
|
+
throw new Error(`'${manifestPath}' is not a tools manifest`);
|
|
144
|
+
}
|
|
145
|
+
if (manifest.protocol !== WORKER_PROTOCOL) {
|
|
146
|
+
throw new Error(`'${manifestPath}' asks for worker protocol ${String(manifest.protocol)}; this polaris speaks ${WORKER_PROTOCOL}`);
|
|
147
|
+
}
|
|
148
|
+
const base = pathToFileURL(resolve(manifestPath));
|
|
149
|
+
tools = await loadTools(new URL(manifest.module, base));
|
|
150
|
+
}
|
|
151
|
+
catch (error) {
|
|
152
|
+
write(JSON.stringify({
|
|
153
|
+
type: "failed",
|
|
154
|
+
message: error instanceof Error ? error.message : String(error),
|
|
155
|
+
}));
|
|
156
|
+
proc.exitCode = 1;
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
await runWorker({
|
|
160
|
+
tools,
|
|
161
|
+
input: proc.stdin,
|
|
162
|
+
write,
|
|
163
|
+
onToolError: (error, call) => log(`'${call.name}' failed: ${error instanceof Error ? (error.stack ?? error.message) : String(error)}`),
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
//# sourceMappingURL=worker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"worker.js","sourceRoot":"","sources":["../../src/mcp/worker.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEpC,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,SAAS,EAA0B,QAAQ,EAAE,MAAM,WAAW,CAAC;AACxE,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAEpC,yCAAyC;AACzC,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,CAAC;AAEjC,mDAAmD;AACnD,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,GAAG,IAAI,GAAG,IAAI,CAAC;AAc/C;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,SAAS,CAAC,OAAsB;IACrD,MAAM,IAAI,GAAG,IAAI,QAAQ,CAAC,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,WAAW,CAAC,CAAC;IAC9D,MAAM,GAAG,GAAG,OAAO,CAAC,YAAY,IAAI,eAAe,CAAC;IACpD,MAAM,IAAI,GAAG,CAAC,OAAgC,EAAQ,EAAE,CACvD,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,CAAC;IACxC,MAAM,OAAO,GAAG,IAAI,GAAG,EAAuB,CAAC;IAC/C,MAAM,OAAO,GAAoB,EAAE,CAAC;IAEpC,MAAM,MAAM,GAAG,CAAC,IAAY,EAAQ,EAA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,OAAO,KAAK,EAAE,CAAC;QAChB,KAAK,CACJ,IAAI,CAAC,SAAS,CAAC;YACd,IAAI,EAAE,QAAQ;YACd,OAAO,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC;SAC/D,CAAC,CACF,CAAC;QACF,IAAI,CAAC,QAAQ,GAAG,CAAC,CAAC;QAClB,OAAO;IACR,CAAC;IACD,MAAM,SAAS,CAAC;QACf,KAAK;QACL,KAAK,EAAE,IAAI,CAAC,KAAK;QACjB,KAAK;QACL,WAAW,EAAE,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE,CAC5B,GAAG,CACF,IAAI,IAAI,CAAC,IAAI,aAAa,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CACnG;KACF,CAAC,CAAC;AACJ,CAAC"}
|
package/dist/meter.d.ts
CHANGED
|
@@ -33,6 +33,12 @@ export interface MeterInput {
|
|
|
33
33
|
export interface MeterOutcome {
|
|
34
34
|
readonly model: string;
|
|
35
35
|
readonly usage: Usage;
|
|
36
|
+
/**
|
|
37
|
+
* Whether the provider reported usage that can be priced. Without it the
|
|
38
|
+
* call has no cost, and its estimate stays on the budget: a missing count
|
|
39
|
+
* is not a free call.
|
|
40
|
+
*/
|
|
41
|
+
readonly usageKnown: boolean;
|
|
36
42
|
readonly responseId?: string;
|
|
37
43
|
readonly stopReason?: StopReason;
|
|
38
44
|
readonly response: ChatResponse | EmbedResponse;
|
package/dist/meter.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"meter.d.ts","sourceRoot":"","sources":["../src/meter.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EACN,KAAK,YAAY,EACjB,KAAK,WAAW,EAIh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,GAAG,SAAS,GACjB,IAAI,GAAG,SAAS,CAIlB"}
|
package/dist/meter.js
CHANGED
|
@@ -34,7 +34,7 @@ export async function openTicket(config, input) {
|
|
|
34
34
|
throw new E_POLARIS_BUDGET_EXCEEDED(["this request", input.maxCost]);
|
|
35
35
|
}
|
|
36
36
|
if (input.scope !== undefined && config.budget !== undefined) {
|
|
37
|
-
reservation = await reserve(config.budget.store, input.scope, limit, estimate);
|
|
37
|
+
reservation = await reserve(config.budget.store, input.scope, limit, estimate, config.budget.period);
|
|
38
38
|
}
|
|
39
39
|
}
|
|
40
40
|
const entry = (status) => ({
|
|
@@ -48,11 +48,14 @@ export async function openTicket(config, input) {
|
|
|
48
48
|
return {
|
|
49
49
|
async finish(outcome) {
|
|
50
50
|
const answered = priceFor(config.pricing, outcome.model) ?? price;
|
|
51
|
-
const amount = answered
|
|
51
|
+
const amount = answered !== undefined && outcome.usageKnown
|
|
52
|
+
? costOf(answered, outcome.usage)
|
|
53
|
+
: undefined;
|
|
52
54
|
const cost = amount !== undefined && config.pricing !== undefined
|
|
53
55
|
? { amount, currency: config.pricing.currency }
|
|
54
56
|
: undefined;
|
|
55
|
-
// Without a price for the model that answered,
|
|
57
|
+
// Without a price for the model that answered, or usage to price, the
|
|
58
|
+
// estimate stays.
|
|
56
59
|
if (reservation !== undefined && amount !== undefined) {
|
|
57
60
|
await reservation.settle(amount);
|
|
58
61
|
}
|
package/dist/meter.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"meter.js","sourceRoot":"","sources":["../src/meter.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAGN,OAAO,EAEP,OAAO,GACP,MAAM,aAAa,CAAC;AACrB,OAAO,EACN,GAAG,EAEH,MAAM,EACN,OAAO,EAGP,QAAQ,GACR,MAAM,WAAW,CAAC;AACnB,OAAO,EACN,yBAAyB,EACzB,yBAAyB,EACzB,uBAAuB,GACvB,MAAM,aAAa,CAAC;AACrB,O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AAK;gBAC9C,MAAM,WAAW,CAAC,OAAO,EAAE,CAAC;YAC7B,MAAM,QAAQ,CACb,MAAM,CAAC,GAAG,EACV;gBACC,GAAG,KAAK,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,OAAO,CAAC;gBACnD,SAAS,EAAE,MAAM,CAAC,KAAK,CAAC;aACxB,EACD,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,CAC1B,CAAC;QACH,CAAC;KACD,CAAC;AACH,CAAC;AAED,4EAA4E;AAC5E,MAAM,UAAU,OAAO,CACtB,CAAmB,EACnB,CAAmB;IAEnB,IAAI,CAAC,KAAK,SAAS;QAAE,OAAO,CAAC,CAAC;IAC9B,IAAI,CAAC,KAAK,SAAS,IAAI,CAAC,CAAC,QAAQ,KAAK,CAAC,CAAC,QAAQ;QAAE,OAAO,CAAC,CAAC;IAC3D,OAAO,EAAE,MAAM,EAAE,GAAG,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,MAAM,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAC,QAAQ,EAAE,CAAC;AAClE,CAAC"}
|
package/dist/redact.d.ts
CHANGED
|
@@ -4,8 +4,9 @@ export declare function redact(text: string, secrets: readonly string[]): string
|
|
|
4
4
|
* The error to raise for a failed call that was given `secrets`.
|
|
5
5
|
*
|
|
6
6
|
* An error that holds none of them is returned as it is. One that holds any is
|
|
7
|
-
* replaced by `E_POLARIS_PROVIDER_FAILED
|
|
8
|
-
* with the secrets taken out, and which keeps no
|
|
7
|
+
* replaced by `E_POLARIS_PROVIDER_FAILED` (or what `replace` builds), whose
|
|
8
|
+
* message is the original one with the secrets taken out, and which keeps no
|
|
9
|
+
* reference to the original.
|
|
9
10
|
*/
|
|
10
|
-
export declare function scrub(error: unknown, provider: string, secrets: readonly (string | undefined)[]): unknown;
|
|
11
|
+
export declare function scrub(error: unknown, provider: string, secrets: readonly (string | undefined)[], replace?: (provider: string, message: string) => Error): unknown;
|
|
11
12
|
//# sourceMappingURL=redact.d.ts.map
|
package/dist/redact.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"redact.d.ts","sourceRoot":"","sources":["../src/redact.ts"],"names":[],"mappings":"AAOA,yDAAyD;AACzD,wBAAgB,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAIvE;AA+CD
|
|
1
|
+
{"version":3,"file":"redact.d.ts","sourceRoot":"","sources":["../src/redact.ts"],"names":[],"mappings":"AAOA,yDAAyD;AACzD,wBAAgB,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAIvE;AA+CD;;;;;;;GAOG;AACH,wBAAgB,KAAK,CACpB,KAAK,EAAE,OAAO,EACd,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,SAAS,CAAC,MAAM,GAAG,SAAS,CAAC,EAAE,EACxC,OAAO,GAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,KAAK,KACD,GAC7C,OAAO,CAOT"}
|
package/dist/redact.js
CHANGED
|
@@ -63,14 +63,15 @@ function holdsSecret(value, secrets) {
|
|
|
63
63
|
* The error to raise for a failed call that was given `secrets`.
|
|
64
64
|
*
|
|
65
65
|
* An error that holds none of them is returned as it is. One that holds any is
|
|
66
|
-
* replaced by `E_POLARIS_PROVIDER_FAILED
|
|
67
|
-
* with the secrets taken out, and which keeps no
|
|
66
|
+
* replaced by `E_POLARIS_PROVIDER_FAILED` (or what `replace` builds), whose
|
|
67
|
+
* message is the original one with the secrets taken out, and which keeps no
|
|
68
|
+
* reference to the original.
|
|
68
69
|
*/
|
|
69
|
-
export function scrub(error, provider, secrets) {
|
|
70
|
+
export function scrub(error, provider, secrets, replace = (name, message) => new E_POLARIS_PROVIDER_FAILED([name, message])) {
|
|
70
71
|
const live = secrets.filter((secret) => typeof secret === "string" && secret !== "");
|
|
71
72
|
if (live.length === 0 || !holdsSecret(error, live))
|
|
72
73
|
return error;
|
|
73
74
|
const message = error instanceof Error ? error.message : String(error);
|
|
74
|
-
return
|
|
75
|
+
return replace(provider, redact(message, live));
|
|
75
76
|
}
|
|
76
77
|
//# sourceMappingURL=redact.js.map
|
package/dist/redact.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"redact.js","sourceRoot":"","sources":["../src/redact.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,yBAAyB,EAAE,MAAM,aAAa,CAAC;AAExD,MAAM,QAAQ,GAAG,YAAY,CAAC;AAC9B,mEAAmE;AACnE,MAAM,UAAU,GAAG,KAAK,CAAC;AACzB,MAAM,SAAS,GAAG,CAAC,CAAC;AAEpB,yDAAyD;AACzD,MAAM,UAAU,MAAM,CAAC,IAAY,EAAE,OAA0B;IAC9D,IAAI,GAAG,GAAG,IAAI,CAAC;IACf,KAAK,MAAM,MAAM,IAAI,OAAO;QAAE,GAAG,GAAG,GAAG,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IACrE,OAAO,GAAG,CAAC;AACZ,CAAC;AAED;;;;;;;GAOG;AACH,SAAS,WAAW,CAAC,KAAc,EAAE,OAA0B;IAC9D,MAAM,IAAI,GAAG,IAAI,GAAG,EAAU,CAAC;IAC/B,IAAI,MAAM,GAAG,CAAC,CAAC;IAEf,MAAM,KAAK,GAAG,CAAC,OAAgB,EAAE,KAAa,EAAW,EAAE;QAC1D,MAAM,IAAI,CAAC,CAAC;QACZ,IAAI,MAAM,GAAG,UAAU,IAAI,KAAK,GAAG,SAAS;YAAE,OAAO,IAAI,CAAC;QAC1D,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE,CAAC;YACjC,OAAO,OAAO,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC,C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UAAU,KAAK,CACpB,KAAc,EACd,QAAgB,EAChB,OAAwC,EACxC,UAAwD,CAAC,IAAI,EAAE,OAAO,EAAE,EAAE,CACzE,IAAI,yBAAyB,CAAC,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;IAE/C,MAAM,IAAI,GAAG,OAAO,CAAC,MAAM,CAC1B,CAAC,MAAM,EAAoB,EAAE,CAAC,OAAO,MAAM,KAAK,QAAQ,IAAI,MAAM,KAAK,EAAE,CACzE,CAAC;IACF,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,IAAI,CAAC,WAAW,CAAC,KAAK,EAAE,IAAI,CAAC;QAAE,OAAO,KAAK,CAAC;IACjE,MAAM,OAAO,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IACvE,OAAO,OAAO,CAAC,QAAQ,EAAE,MAAM,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,CAAC;AACjD,CAAC"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A budget store in Redis, shared by every instance that talks to the same
|
|
3
|
+
* server: what one instance reserves, the others see at once.
|
|
4
|
+
*
|
|
5
|
+
* Spend is kept as an integer count of billionths of the pricing currency, so
|
|
6
|
+
* that Redis adds it with `INCRBY`, exactly and atomically, where
|
|
7
|
+
* `INCRBYFLOAT` would add binary floats. A key's expiry is set in the same
|
|
8
|
+
* `MULTI` as the addition: a key is never left without one by a process that
|
|
9
|
+
* died between the two.
|
|
10
|
+
*/
|
|
11
|
+
import type { BudgetStore } from "./budget.js";
|
|
12
|
+
/** The commands of a `MULTI` the store queues. */
|
|
13
|
+
export interface RedisBudgetTransaction {
|
|
14
|
+
incrby(key: string, increment: string): RedisBudgetTransaction;
|
|
15
|
+
pexpireat(key: string, unixTimeMilliseconds: number): RedisBudgetTransaction;
|
|
16
|
+
exec(): Promise<[error: Error | null, result: unknown][] | null>;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* What the store needs of a Redis client: an ioredis client and a
|
|
20
|
+
* `@c9up/quasar` connection both fit as they are.
|
|
21
|
+
*/
|
|
22
|
+
export interface RedisBudgetClient {
|
|
23
|
+
multi(): RedisBudgetTransaction;
|
|
24
|
+
}
|
|
25
|
+
export interface RedisBudgetStoreOptions {
|
|
26
|
+
/** Put before every key, to share one database with others. Default: `polaris:budget:`. */
|
|
27
|
+
readonly prefix?: string;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* A budget store in Redis, for budgets shared by several instances:
|
|
31
|
+
*
|
|
32
|
+
* budget: { limit, period: 'month', store: redisBudgetStore(() => redis.connection()) }
|
|
33
|
+
*
|
|
34
|
+
* `client` is the client, or a function that answers it, called on each
|
|
35
|
+
* addition: a config file read before the Redis connections are booted can
|
|
36
|
+
* then name one.
|
|
37
|
+
*
|
|
38
|
+
* Amounts are rounded to nine decimals (half away from zero, so a release
|
|
39
|
+
* cancels its reservation exactly), and a key counts exactly up to about nine
|
|
40
|
+
* million units of the currency. The store declares those nine decimals: a
|
|
41
|
+
* reservation rounds its estimate and the final cost to them once each, so a
|
|
42
|
+
* request leaves exactly its rounded cost in the key.
|
|
43
|
+
*/
|
|
44
|
+
export declare function redisBudgetStore(client: RedisBudgetClient | (() => RedisBudgetClient), options?: RedisBudgetStoreOptions): BudgetStore;
|
|
45
|
+
//# sourceMappingURL=redis-budget.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"redis-budget.d.ts","sourceRoot":"","sources":["../src/redis-budget.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAGH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE/C,kDAAkD;AAClD,MAAM,WAAW,sBAAsB;IACtC,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,sBAAsB,CAAC;IAC/D,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,oBAAoB,EAAE,MAAM,GAAG,sBAAsB,CAAC;IAC7E,IAAI,IAAI,OAAO,CAAC,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,GAAG,IAAI,CAAC,CAAC;CACjE;AAED;;;GAGG;AACH,MAAM,WAAW,iBAAiB;IACjC,KAAK,IAAI,sBAAsB,CAAC;CAChC;AAED,MAAM,WAAW,uBAAuB;IACvC,2FAA2F;IAC3F,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;CACzB;AAOD;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,gBAAgB,CAC/B,MAAM,EAAE,iBAAiB,GAAG,CAAC,MAAM,iBAAiB,CAAC,EACrD,OAAO,GAAE,uBAA4B,GACnC,WAAW,CA2Bb"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A budget store in Redis, shared by every instance that talks to the same
|
|
3
|
+
* server: what one instance reserves, the others see at once.
|
|
4
|
+
*
|
|
5
|
+
* Spend is kept as an integer count of billionths of the pricing currency, so
|
|
6
|
+
* that Redis adds it with `INCRBY`, exactly and atomically, where
|
|
7
|
+
* `INCRBYFLOAT` would add binary floats. A key's expiry is set in the same
|
|
8
|
+
* `MULTI` as the addition: a key is never left without one by a process that
|
|
9
|
+
* died between the two.
|
|
10
|
+
*/
|
|
11
|
+
import { decimal } from "@c9up/atom";
|
|
12
|
+
/** Spend is counted in billionths of the currency. */
|
|
13
|
+
const DECIMALS = 9;
|
|
14
|
+
const UNIT = "1000000000";
|
|
15
|
+
const STEP = "0.000000001";
|
|
16
|
+
/**
|
|
17
|
+
* A budget store in Redis, for budgets shared by several instances:
|
|
18
|
+
*
|
|
19
|
+
* budget: { limit, period: 'month', store: redisBudgetStore(() => redis.connection()) }
|
|
20
|
+
*
|
|
21
|
+
* `client` is the client, or a function that answers it, called on each
|
|
22
|
+
* addition: a config file read before the Redis connections are booted can
|
|
23
|
+
* then name one.
|
|
24
|
+
*
|
|
25
|
+
* Amounts are rounded to nine decimals (half away from zero, so a release
|
|
26
|
+
* cancels its reservation exactly), and a key counts exactly up to about nine
|
|
27
|
+
* million units of the currency. The store declares those nine decimals: a
|
|
28
|
+
* reservation rounds its estimate and the final cost to them once each, so a
|
|
29
|
+
* request leaves exactly its rounded cost in the key.
|
|
30
|
+
*/
|
|
31
|
+
export function redisBudgetStore(client, options = {}) {
|
|
32
|
+
const prefix = options.prefix ?? "polaris:budget:";
|
|
33
|
+
return {
|
|
34
|
+
decimals: DECIMALS,
|
|
35
|
+
async add(key, amount, expiresAt) {
|
|
36
|
+
const name = prefix + key;
|
|
37
|
+
const increment = decimal(amount).times(UNIT).toFixed(0, "half-up");
|
|
38
|
+
const redis = typeof client === "function" ? client() : client;
|
|
39
|
+
let transaction = redis.multi().incrby(name, increment);
|
|
40
|
+
if (expiresAt !== undefined)
|
|
41
|
+
transaction = transaction.pexpireat(name, expiresAt);
|
|
42
|
+
const replies = await transaction.exec();
|
|
43
|
+
if (replies === null) {
|
|
44
|
+
throw new Error(`[polaris] the budget transaction on '${name}' was aborted`);
|
|
45
|
+
}
|
|
46
|
+
const [error, total] = replies[0] ?? [null, undefined];
|
|
47
|
+
if (error !== null)
|
|
48
|
+
throw error;
|
|
49
|
+
if (typeof total !== "number" || !Number.isSafeInteger(total)) {
|
|
50
|
+
throw new Error(`[polaris] the spend under '${name}' is past what a budget counts exactly`);
|
|
51
|
+
}
|
|
52
|
+
return decimal(total).times(STEP).toString();
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=redis-budget.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"redis-budget.js","sourceRoot":"","sources":["../src/redis-budget.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAuBrC,sDAAsD;AAC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CAAC,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC,EAAE,CAAC;gBAC/D,MAAM,IAAI,KAAK,CACd,8BAA8B,IAAI,wCAAwC,CAC1E,CAAC;YACH,CAAC;YACD,OAAO,OAAO,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,QAAQ,EAAE,CAAC;QAC9C,CAAC;KACD,CAAC;AACH,CAAC"}
|
package/dist/response.d.ts
CHANGED
|
@@ -1,21 +1,39 @@
|
|
|
1
|
-
import type { AssistantPart, ChatResponse, StopReason, Usage } from "./types.js";
|
|
1
|
+
import type { AssistantPart, ChatResponse, ServerContainer, StopReason, Usage } from "./types.js";
|
|
2
2
|
export interface ResponseInput {
|
|
3
3
|
readonly id: string;
|
|
4
4
|
readonly provider: string;
|
|
5
5
|
readonly model: string;
|
|
6
6
|
readonly content: readonly AssistantPart[];
|
|
7
7
|
readonly stopReason: StopReason;
|
|
8
|
-
|
|
8
|
+
/**
|
|
9
|
+
* The token counts as the provider reported them, unchecked: leave it out
|
|
10
|
+
* when it reported none.
|
|
11
|
+
*/
|
|
12
|
+
readonly usage?: {
|
|
13
|
+
readonly [K in keyof Usage]?: unknown;
|
|
14
|
+
};
|
|
15
|
+
/** The provider's container the reply ran code in. */
|
|
16
|
+
readonly container?: ServerContainer;
|
|
9
17
|
}
|
|
10
18
|
/** Usage of nothing yet: what a sum of usages starts from. */
|
|
11
19
|
export declare const NO_USAGE: Usage;
|
|
12
20
|
/** Two usages added up: the cost of a run or of several attempts. */
|
|
13
21
|
export declare function addUsage(a: Usage, b: Usage): Usage;
|
|
22
|
+
/** Whether `value` can be a count of tokens: a whole number, 0 or more. */
|
|
23
|
+
export declare function isTokenCount(value: unknown): value is number;
|
|
24
|
+
/** Whether every count of `usage` can be one. */
|
|
25
|
+
export declare function isUsage(usage: Usage): boolean;
|
|
14
26
|
/**
|
|
15
27
|
* Build a {@link ChatResponse} from what a provider returned: `text` and
|
|
16
|
-
* `toolCalls` are derived from `content
|
|
28
|
+
* `toolCalls` are derived from `content`.
|
|
17
29
|
* Drivers build their responses with it, so the derived fields cannot drift
|
|
18
30
|
* from the content they summarize.
|
|
31
|
+
*
|
|
32
|
+
* Usage the provider did not report, reported without its input or output
|
|
33
|
+
* count, or with a count that cannot be one (negative, fractional, not a
|
|
34
|
+
* number), is marked `usageUnknown`: the wrong count is 0, and the call is
|
|
35
|
+
* not priced from it. Cache counts left out are 0. A count of 0 would
|
|
36
|
+
* otherwise settle the call's budget as free.
|
|
19
37
|
*/
|
|
20
38
|
export declare function createResponse(input: ResponseInput): ChatResponse;
|
|
21
39
|
//# sourceMappingURL=response.d.ts.map
|
package/dist/response.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"response.d.ts","sourceRoot":"","sources":["../src/response.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACX,aAAa,EACb,YAAY,EACZ,UAAU,EAEV,KAAK,EACL,MAAM,YAAY,CAAC;AAEpB,MAAM,WAAW,aAAa;IAC7B,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,OAAO,EAAE,SAAS,aAAa,EAAE,CAAC;IAC3C,QAAQ,CAAC,UAAU,EAAE,UAAU,CAAC;IAChC,QAAQ,CAAC,KAAK,CAAC,EAAE,
|
|
1
|
+
{"version":3,"file":"response.d.ts","sourceRoot":"","sources":["../src/response.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACX,aAAa,EACb,YAAY,EACZ,eAAe,EACf,UAAU,EAEV,KAAK,EACL,MAAM,YAAY,CAAC;AAEpB,MAAM,WAAW,aAAa;IAC7B,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,OAAO,EAAE,SAAS,aAAa,EAAE,CAAC;IAC3C,QAAQ,CAAC,UAAU,EAAE,UAAU,CAAC;IAChC;;;OAGG;IACH,QAAQ,CAAC,KAAK,CAAC,EAAE;QAAE,QAAQ,EAAE,CAAC,IAAI,MAAM,KAAK,CAAC,CAAC,EAAE,OAAO;KAAE,CAAC;IAC3D,sDAAsD;IACtD,QAAQ,CAAC,SAAS,CAAC,EAAE,eAAe,CAAC;CACrC;AAED,8DAA8D;AAC9D,eAAO,MAAM,QAAQ,EAAE,KAKtB,CAAC;AAEF,qEAAqE;AACrE,wBAAgB,QAAQ,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,KAAK,GAAG,KAAK,CASlD;AAiBD,2EAA2E;AAC3E,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAE5D;AAED,iDAAiD;AACjD,wBAAgB,OAAO,CAAC,KAAK,EAAE,KAAK,GAAG,OAAO,CAO7C;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,aAAa,GAAG,YAAY,CAmBjE"}
|
package/dist/response.js
CHANGED
|
@@ -7,18 +7,48 @@ export const NO_USAGE = {
|
|
|
7
7
|
};
|
|
8
8
|
/** Two usages added up: the cost of a run or of several attempts. */
|
|
9
9
|
export function addUsage(a, b) {
|
|
10
|
+
const serverToolUse = addCounters(a.serverToolUse, b.serverToolUse);
|
|
10
11
|
return {
|
|
11
12
|
inputTokens: a.inputTokens + b.inputTokens,
|
|
12
13
|
outputTokens: a.outputTokens + b.outputTokens,
|
|
13
14
|
cacheReadTokens: a.cacheReadTokens + b.cacheReadTokens,
|
|
14
15
|
cacheWriteTokens: a.cacheWriteTokens + b.cacheWriteTokens,
|
|
16
|
+
...(serverToolUse !== undefined ? { serverToolUse } : {}),
|
|
15
17
|
};
|
|
16
18
|
}
|
|
19
|
+
function addCounters(a, b) {
|
|
20
|
+
if (a === undefined)
|
|
21
|
+
return b;
|
|
22
|
+
if (b === undefined)
|
|
23
|
+
return a;
|
|
24
|
+
const sum = { ...a };
|
|
25
|
+
for (const [counter, count] of Object.entries(b)) {
|
|
26
|
+
sum[counter] = (sum[counter] ?? 0) + count;
|
|
27
|
+
}
|
|
28
|
+
return sum;
|
|
29
|
+
}
|
|
30
|
+
/** Whether `value` can be a count of tokens: a whole number, 0 or more. */
|
|
31
|
+
export function isTokenCount(value) {
|
|
32
|
+
return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
|
|
33
|
+
}
|
|
34
|
+
/** Whether every count of `usage` can be one. */
|
|
35
|
+
export function isUsage(usage) {
|
|
36
|
+
return (isTokenCount(usage.inputTokens) &&
|
|
37
|
+
isTokenCount(usage.outputTokens) &&
|
|
38
|
+
isTokenCount(usage.cacheReadTokens) &&
|
|
39
|
+
isTokenCount(usage.cacheWriteTokens));
|
|
40
|
+
}
|
|
17
41
|
/**
|
|
18
42
|
* Build a {@link ChatResponse} from what a provider returned: `text` and
|
|
19
|
-
* `toolCalls` are derived from `content
|
|
43
|
+
* `toolCalls` are derived from `content`.
|
|
20
44
|
* Drivers build their responses with it, so the derived fields cannot drift
|
|
21
45
|
* from the content they summarize.
|
|
46
|
+
*
|
|
47
|
+
* Usage the provider did not report, reported without its input or output
|
|
48
|
+
* count, or with a count that cannot be one (negative, fractional, not a
|
|
49
|
+
* number), is marked `usageUnknown`: the wrong count is 0, and the call is
|
|
50
|
+
* not priced from it. Cache counts left out are 0. A count of 0 would
|
|
51
|
+
* otherwise settle the call's budget as free.
|
|
22
52
|
*/
|
|
23
53
|
export function createResponse(input) {
|
|
24
54
|
const text = input.content
|
|
@@ -34,12 +64,46 @@ export function createResponse(input) {
|
|
|
34
64
|
text,
|
|
35
65
|
toolCalls,
|
|
36
66
|
stopReason: input.stopReason,
|
|
37
|
-
usage
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
67
|
+
...readUsage(input.usage),
|
|
68
|
+
...(input.container !== undefined ? { container: input.container } : {}),
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
function readUsage(reported) {
|
|
72
|
+
let unknown = reported === undefined;
|
|
73
|
+
// Input and output are always reported by a provider that reports usage:
|
|
74
|
+
// one left out (a gateway's `usage: {}`) would price the call as free. The
|
|
75
|
+
// cache counts are left out by providers without a cache.
|
|
76
|
+
const count = (value, required) => {
|
|
77
|
+
if (value === undefined && !required)
|
|
78
|
+
return 0;
|
|
79
|
+
if (isTokenCount(value))
|
|
80
|
+
return value;
|
|
81
|
+
unknown = true;
|
|
82
|
+
return 0;
|
|
43
83
|
};
|
|
84
|
+
let usage = {
|
|
85
|
+
inputTokens: count(reported?.inputTokens, true),
|
|
86
|
+
outputTokens: count(reported?.outputTokens, true),
|
|
87
|
+
cacheReadTokens: count(reported?.cacheReadTokens, false),
|
|
88
|
+
cacheWriteTokens: count(reported?.cacheWriteTokens, false),
|
|
89
|
+
};
|
|
90
|
+
const serverToolUse = readCounters(reported?.serverToolUse);
|
|
91
|
+
if (serverToolUse !== undefined)
|
|
92
|
+
usage = { ...usage, serverToolUse };
|
|
93
|
+
return unknown ? { usage, usageUnknown: true } : { usage };
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* The server tool counters that can be counts. They price nothing, so one
|
|
97
|
+
* that cannot be a count is left out rather than marking the usage unknown.
|
|
98
|
+
*/
|
|
99
|
+
function readCounters(reported) {
|
|
100
|
+
if (typeof reported !== "object" || reported === null)
|
|
101
|
+
return undefined;
|
|
102
|
+
const counters = {};
|
|
103
|
+
for (const [counter, count] of Object.entries(reported)) {
|
|
104
|
+
if (isTokenCount(count))
|
|
105
|
+
counters[counter] = count;
|
|
106
|
+
}
|
|
107
|
+
return Object.keys(counters).length > 0 ? counters : undefined;
|
|
44
108
|
}
|
|
45
109
|
//# sourceMappingURL=response.js.map
|
package/dist/response.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"response.js","sourceRoot":"","sources":["../src/response.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"response.js","sourceRoot":"","sources":["../src/response.ts"],"names":[],"mappings":"AAwBA,8DAA8D;AAC9D,MAAM,CAAC,MAAM,QAAQ,GAAU;IAC9B,WAAW,EAAE,CAAC;IACd,YAAY,EAAE,CAAC;IACf,eAAe,EAAE,CAAC;IAClB,gBAAgB,EAAE,CAAC;CACnB,CAAC;AAEF,qEAAqE;AACrE,MAAM,UAAU,QAAQ,CAAC,CAAQ,EAAE,CAAQ;IAC1C,MAAM,aAAa,GAAG,WAAW,CAAC,CAAC,CAAC,aAAa,EAAE,CAAC,CAAC,aAAa,CAAC,CAAC;IACpE,OAAO;QACN,WAAW,EAAE,CAAC,CAAC,WAAW,GAAG,CAAC,CAAC,WAAW;QAC1C,YAAY,EAAE,CAAC,CAAC,YAAY,GAAG,CAAC,CAAC,YAAY;QAC7C,eAAe,EAAE,CAAC,CAAC,eAAe,GAAG,CAAC,CAAC,eAAe;QACtD,gBAAgB,EAAE,CAAC,CAAC,gBAAgB,GAAG,CAAC,CAAC,gBAAgB;QACzD,GAAG,CAAC,aAAa,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,aAAa,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KACzD,CAAC;AACH,CAAC;AAID,SAAS,WAAW,CACnB,CAAuB,EACvB,CAAuB;IAEvB,IAAI,CAAC,KAAK,SAAS;QAAE,OAAO,CAAC,CAAC;IAC9B,IAAI,CAAC,KAAK,SAAS;QAAE,OAAO,CAAC,CAAC;IAC9B,MAAM,GAAG,GAA2B,EAAE,GAAG,CAAC,EAAE,CAAC;IAC7C,KAAK,MAAM,CAAC,OAAO,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC;QAClD,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC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AK,QAAQ,IAAI,QAAQ,KAAK,IAAI;QAAE,OAAO,SAAS,CAAC;IACxE,MAAM,QAAQ,GAA2B,EAAE,CAAC;IAC5C,KAAK,MAAM,CAAC,OAAO,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC;QACzD,IAAI,YAAY,CAAC,KAAK,CAAC;YAAE,QAAQ,CAAC,OAAO,CAAC,GAAG,KAAK,CAAC;IACpD,CAAC;IACD,OAAO,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAC;AAChE,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `data:` payloads of a server-sent event stream, lines rejoined across
|
|
3
|
+
* chunks. Linear in the stream's size: a line still open is kept in pieces,
|
|
4
|
+
* joined once its end arrives, and only the text just received is searched
|
|
5
|
+
* for a line end. An event longer than `maxLength` characters raises
|
|
6
|
+
* `tooLong()`: holding it would only feed a runaway or hostile stream.
|
|
7
|
+
*/
|
|
8
|
+
export declare function serverSentData(body: ReadableStream<Uint8Array>, maxLength: number, tooLong: () => Error): AsyncGenerator<string>;
|
|
9
|
+
//# sourceMappingURL=server-sent.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server-sent.d.ts","sourceRoot":"","sources":["../src/server-sent.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,wBAAuB,cAAc,CACpC,IAAI,EAAE,cAAc,CAAC,UAAU,CAAC,EAChC,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,MAAM,KAAK,GAClB,cAAc,CAAC,MAAM,CAAC,CA0DxB"}
|