@c9up/polaris 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +1146 -2
- package/dist/Polaris.d.ts +80 -0
- package/dist/Polaris.d.ts.map +1 -0
- package/dist/Polaris.js +208 -0
- package/dist/Polaris.js.map +1 -0
- package/dist/PolarisClient.d.ts +184 -0
- package/dist/PolarisClient.d.ts.map +1 -0
- package/dist/PolarisClient.js +576 -0
- package/dist/PolarisClient.js.map +1 -0
- package/dist/PolarisProvider.d.ts +40 -0
- package/dist/PolarisProvider.d.ts.map +1 -0
- package/dist/PolarisProvider.js +79 -0
- package/dist/PolarisProvider.js.map +1 -0
- package/dist/augmentations.d.ts +18 -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 +77 -0
- package/dist/budget.d.ts.map +1 -0
- package/dist/budget.js +134 -0
- package/dist/budget.js.map +1 -0
- 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 +50 -0
- package/dist/cost.d.ts.map +1 -0
- package/dist/cost.js +131 -0
- package/dist/cost.js.map +1 -0
- package/dist/drivers/anthropic.d.ts +44 -0
- package/dist/drivers/anthropic.d.ts.map +1 -0
- package/dist/drivers/anthropic.js +539 -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 +699 -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 +79 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +86 -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 +21 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- 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 +47 -0
- package/dist/log.d.ts.map +1 -0
- package/dist/log.js +35 -0
- package/dist/log.js.map +1 -0
- 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 +63 -0
- package/dist/meter.d.ts.map +1 -0
- package/dist/meter.js +94 -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 +12 -0
- package/dist/redact.d.ts.map +1 -0
- package/dist/redact.js +77 -0
- package/dist/redact.js.map +1 -0
- 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 +39 -0
- package/dist/response.d.ts.map +1 -0
- package/dist/response.js +109 -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/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/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/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 +95 -0
- package/dist/sse.d.ts.map +1 -0
- package/dist/sse.js +117 -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 +214 -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/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 +132 -0
- package/dist/tools.d.ts.map +1 -0
- package/dist/tools.js +250 -0
- package/dist/tools.js.map +1 -0
- package/dist/transport.d.ts +23 -0
- package/dist/transport.d.ts.map +1 -0
- package/dist/transport.js +83 -0
- package/dist/transport.js.map +1 -0
- package/dist/types.d.ts +397 -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 +97 -3
- package/src/Polaris.ts +304 -0
- package/src/PolarisClient.ts +828 -0
- package/src/PolarisProvider.ts +111 -0
- package/src/augmentations.ts +20 -0
- package/src/budget.ts +201 -0
- 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 +176 -0
- package/src/drivers/anthropic.ts +703 -0
- package/src/drivers/openai-compatible.ts +907 -0
- package/src/drivers/status.ts +30 -0
- package/src/errors.ts +202 -0
- package/src/extract.ts +214 -0
- package/src/index.ts +85 -0
- package/src/json-schema.ts +597 -0
- package/src/log.ts +86 -0
- 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 +199 -0
- package/src/providers.ts +25 -0
- package/src/redact.ts +81 -0
- package/src/redis-budget.ts +85 -0
- package/src/response.ts +143 -0
- package/src/schema.ts +88 -0
- package/src/server-sent.ts +70 -0
- package/src/server-tools.ts +77 -0
- package/src/services/catalog.ts +43 -0
- package/src/services/main.ts +49 -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 +194 -0
- package/src/testing/FakeProvider.ts +322 -0
- package/src/testing/index.ts +12 -0
- package/src/tool-modules.ts +49 -0
- package/src/tools.ts +399 -0
- package/src/transport.ts +107 -0
- package/src/types.ts +434 -0
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A skill: instructions for one kind of task, the files they refer to, and
|
|
3
|
+
* the tools they need. Only its name and description reach the model at
|
|
4
|
+
* first; the rest when the model loads it.
|
|
5
|
+
*/
|
|
6
|
+
import { E_POLARIS_INVALID_REQUEST } from "../errors.js";
|
|
7
|
+
import { isTool } from "../tools.js";
|
|
8
|
+
/** A skill name: lower-case letters, digits and single hyphens, 64 at most. */
|
|
9
|
+
export const SKILL_NAME = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
10
|
+
export const MAX_SKILL_NAME = 64;
|
|
11
|
+
export const MAX_SKILL_DESCRIPTION = 1024;
|
|
12
|
+
/** Bytes of a skill's instructions, and of each file the model reads. */
|
|
13
|
+
export const MAX_SKILL_BYTES = 256 * 1024;
|
|
14
|
+
/** Why a skill's name, description or instructions are refused, if they are. */
|
|
15
|
+
export function skillProblem(name, description, instructions) {
|
|
16
|
+
if (typeof name !== "string" ||
|
|
17
|
+
name.length > MAX_SKILL_NAME ||
|
|
18
|
+
!SKILL_NAME.test(name)) {
|
|
19
|
+
return `a skill name is 1 to ${MAX_SKILL_NAME} lower-case letters, digits and single hyphens; '${String(name).slice(0, 80)}' is not`;
|
|
20
|
+
}
|
|
21
|
+
if (typeof description !== "string" || description.trim() === "") {
|
|
22
|
+
return `the skill '${name}' has no description`;
|
|
23
|
+
}
|
|
24
|
+
if (description.length > MAX_SKILL_DESCRIPTION) {
|
|
25
|
+
return `the description of the skill '${name}' is longer than ${MAX_SKILL_DESCRIPTION} characters`;
|
|
26
|
+
}
|
|
27
|
+
if (typeof instructions !== "string" || instructions.trim() === "") {
|
|
28
|
+
return `the skill '${name}' has no instructions`;
|
|
29
|
+
}
|
|
30
|
+
if (Buffer.byteLength(instructions, "utf8") > MAX_SKILL_BYTES) {
|
|
31
|
+
return `the instructions of the skill '${name}' are larger than ${MAX_SKILL_BYTES} bytes`;
|
|
32
|
+
}
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
/** Why a skill's tools are refused, if they are. */
|
|
36
|
+
export function toolsProblem(name, tools) {
|
|
37
|
+
const seen = new Set();
|
|
38
|
+
for (const tool of tools) {
|
|
39
|
+
if (!isTool(tool))
|
|
40
|
+
return `the tools of the skill '${name}' must be built by defineTool()`;
|
|
41
|
+
if (seen.has(tool.name))
|
|
42
|
+
return `the skill '${name}' has two tools named '${tool.name}'`;
|
|
43
|
+
seen.add(tool.name);
|
|
44
|
+
}
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Declare a skill in code:
|
|
49
|
+
*
|
|
50
|
+
* const payroll = defineSkill({
|
|
51
|
+
* name: 'swiss-payroll',
|
|
52
|
+
* description: 'Compute a Swiss payslip. Use it for any salary question.',
|
|
53
|
+
* instructions: '1. Ask for the canton...',
|
|
54
|
+
* tools: [socialRates, withholdingTax],
|
|
55
|
+
* files: { 'reference/avs.md': avsTable },
|
|
56
|
+
* })
|
|
57
|
+
*/
|
|
58
|
+
export function defineSkill(spec) {
|
|
59
|
+
const tools = spec.tools ?? [];
|
|
60
|
+
const problem = skillProblem(spec.name, spec.description, spec.instructions) ??
|
|
61
|
+
toolsProblem(spec.name, tools) ??
|
|
62
|
+
filesProblem(spec.name, spec.files ?? {});
|
|
63
|
+
if (problem !== undefined)
|
|
64
|
+
throw new E_POLARIS_INVALID_REQUEST([problem]);
|
|
65
|
+
const files = new Map(Object.entries(spec.files ?? {}));
|
|
66
|
+
return {
|
|
67
|
+
name: spec.name,
|
|
68
|
+
description: spec.description.trim(),
|
|
69
|
+
instructions: spec.instructions.trim(),
|
|
70
|
+
tools: [...tools],
|
|
71
|
+
files: {
|
|
72
|
+
list: async () => [...files.keys()].sort(),
|
|
73
|
+
read: async (path) => {
|
|
74
|
+
const text = files.get(path);
|
|
75
|
+
if (text === undefined) {
|
|
76
|
+
throw new SkillFileError(`The skill '${spec.name}' has no file '${path.slice(0, 200)}'.`);
|
|
77
|
+
}
|
|
78
|
+
return text;
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
function filesProblem(name, files) {
|
|
84
|
+
for (const [path, text] of Object.entries(files)) {
|
|
85
|
+
if (typeof text !== "string")
|
|
86
|
+
return `the file '${path}' of the skill '${name}' is not text`;
|
|
87
|
+
if (Buffer.byteLength(text, "utf8") > MAX_SKILL_BYTES) {
|
|
88
|
+
return `the file '${path}' of the skill '${name}' is larger than ${MAX_SKILL_BYTES} bytes`;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return undefined;
|
|
92
|
+
}
|
|
93
|
+
/** A file the model asked for cannot be read: the message is for the model. */
|
|
94
|
+
export class SkillFileError extends Error {
|
|
95
|
+
constructor(message) {
|
|
96
|
+
super(message);
|
|
97
|
+
this.name = "SkillFileError";
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/** Whether `value` is a skill built by {@link defineSkill} or `readSkill()`. */
|
|
101
|
+
export function isSkill(value) {
|
|
102
|
+
if (typeof value !== "object" || value === null)
|
|
103
|
+
return false;
|
|
104
|
+
const files = Reflect.get(value, "files");
|
|
105
|
+
return (typeof Reflect.get(value, "name") === "string" &&
|
|
106
|
+
typeof Reflect.get(value, "description") === "string" &&
|
|
107
|
+
typeof Reflect.get(value, "instructions") === "string" &&
|
|
108
|
+
Array.isArray(Reflect.get(value, "tools")) &&
|
|
109
|
+
typeof files === "object" &&
|
|
110
|
+
files !== null &&
|
|
111
|
+
typeof Reflect.get(files, "read") === "function" &&
|
|
112
|
+
typeof Reflect.get(files, "list") === "function");
|
|
113
|
+
}
|
|
114
|
+
//# sourceMappingURL=skill.js.map
|
|
@@ -0,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AAC;IAC9B,CAAC;CACD;AAED,gFAAgF;AAChF,MAAM,UAAU,OAAO,CAAC,KAAc;IACrC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI;QAAE,OAAO,KAAK,CAAC;IAC9D,MAAM,KAAK,GAAY,OAAO,CAAC,GAAG,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;IACnD,OAAO,CACN,OAAO,OAAO,CAAC,GAAG,CAAC,KAAK,EAAE,MAAM,CAAC,KAAK,QAAQ;QAC9C,OAAO,OAAO,CAAC,GAAG,CAAC,KAAK,EAAE,aAAa,CAAC,KAAK,QAAQ;QACrD,OAAO,OAAO,CAAC,GAAG,CAAC,KAAK,EAAE,cAAc,CAAC,KAAK,QAAQ;QACtD,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;QAC1C,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACd,OAAO,OAAO,CAAC,GAAG,CAAC,KAAK,EAAE,MAAM,CAAC,KAAK,UAAU;QAChD,OAAO,OAAO,CAAC,GAAG,CAAC,KAAK,EAAE,MAAM,CAAC,KAAK,UAAU,CAChD,CAAC;AACH,CAAC"}
|
package/dist/sse.d.ts
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A reply streamed to the browser as Server-Sent Events, over the response of
|
|
3
|
+
* the request that asked for it (ream's `ctx.response.sse()`).
|
|
4
|
+
*
|
|
5
|
+
* That stream, not a relay channel, is the transport here: it belongs to the
|
|
6
|
+
* one client waiting for this reply, and its close is a sure sign the client
|
|
7
|
+
* left, on every instance, which is what lets the provider request be
|
|
8
|
+
* cancelled. A relay channel reaches whoever subscribed, wherever they are,
|
|
9
|
+
* but cannot tell polaris that nobody listens any more.
|
|
10
|
+
*/
|
|
11
|
+
import type { RunEvent, RunResult } from "./PolarisClient.js";
|
|
12
|
+
import type { ChatResponse, StreamEvent, Usage } from "./types.js";
|
|
13
|
+
/**
|
|
14
|
+
* What polaris needs of an SSE stream: ream's `SseStream` fits as it is, and
|
|
15
|
+
* so does relay's.
|
|
16
|
+
*/
|
|
17
|
+
export interface SseSink {
|
|
18
|
+
/** Send one named event. Answers `false` once the stream is closed. */
|
|
19
|
+
send(event: string, data: unknown): Promise<boolean> | boolean;
|
|
20
|
+
/** Called once when the stream closes, whoever closed it. */
|
|
21
|
+
onClose(callback: () => void): void;
|
|
22
|
+
/** Close the stream from the server. */
|
|
23
|
+
end(): Promise<void> | void;
|
|
24
|
+
}
|
|
25
|
+
/** How a streamed reply ended. */
|
|
26
|
+
export type StreamToResult = {
|
|
27
|
+
readonly status: "done";
|
|
28
|
+
readonly response: ChatResponse;
|
|
29
|
+
} | SseFailure;
|
|
30
|
+
/** How a streamed tool run ended. */
|
|
31
|
+
export type StreamRunToResult = {
|
|
32
|
+
readonly status: "done";
|
|
33
|
+
readonly result: RunResult;
|
|
34
|
+
} | SseFailure;
|
|
35
|
+
/** The ways a stream to the browser ends without its last event. */
|
|
36
|
+
export type SseFailure =
|
|
37
|
+
/** The client went away; the provider request was cancelled. */
|
|
38
|
+
{
|
|
39
|
+
readonly status: "client_left";
|
|
40
|
+
}
|
|
41
|
+
/** The call failed; the client was sent an `error` event with its code. */
|
|
42
|
+
| {
|
|
43
|
+
readonly status: "failed";
|
|
44
|
+
readonly error: unknown;
|
|
45
|
+
};
|
|
46
|
+
/** The `done` event's data: what a page needs, none of the provider's raw blocks. */
|
|
47
|
+
export interface DoneEventData {
|
|
48
|
+
readonly id: string;
|
|
49
|
+
readonly provider: string;
|
|
50
|
+
readonly model: string;
|
|
51
|
+
readonly text: string;
|
|
52
|
+
readonly stopReason: ChatResponse["stopReason"];
|
|
53
|
+
readonly usage: Usage;
|
|
54
|
+
}
|
|
55
|
+
/** The `done` event's data at the end of a run: its last reply, with the run's steps and tokens. */
|
|
56
|
+
export interface RunDoneEventData extends DoneEventData {
|
|
57
|
+
readonly steps: number;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* The `tool_result` event's data. The result itself stays on the server: it
|
|
61
|
+
* is the application's data, read by the model, not meant for the page.
|
|
62
|
+
*/
|
|
63
|
+
export interface ToolResultEventData {
|
|
64
|
+
readonly id: string;
|
|
65
|
+
readonly name: string;
|
|
66
|
+
readonly isError: boolean;
|
|
67
|
+
}
|
|
68
|
+
/** Why polaris cancelled the provider request: the client closed the stream. */
|
|
69
|
+
export declare class ClientLeft extends Error {
|
|
70
|
+
constructor();
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Forward `events` to `sink` as events named by their type after `prefix`
|
|
74
|
+
* (`text`, `tool_call`, `done`…, and `error` on a failure), and end the stream
|
|
75
|
+
* after the last one. `leave` is aborted when the client closes the stream.
|
|
76
|
+
* `encode` gives an event's data, and the result when it is the last one.
|
|
77
|
+
* Never rejects: every outcome is in the result.
|
|
78
|
+
*/
|
|
79
|
+
export declare function pipeToSse<E extends {
|
|
80
|
+
readonly type: string;
|
|
81
|
+
}, R>(events: AsyncIterable<E>, sink: SseSink, leave: AbortController, prefix: string, encode: (event: E) => {
|
|
82
|
+
readonly data: unknown;
|
|
83
|
+
readonly last?: R;
|
|
84
|
+
}): Promise<R | SseFailure>;
|
|
85
|
+
/** A streamed reply's event, as `streamTo()` sends it. */
|
|
86
|
+
export declare function encodeStreamEvent(event: StreamEvent): {
|
|
87
|
+
readonly data: unknown;
|
|
88
|
+
readonly last?: StreamToResult;
|
|
89
|
+
};
|
|
90
|
+
/** A streamed run's event, as `streamRunTo()` sends it. */
|
|
91
|
+
export declare function encodeRunEvent(event: RunEvent): {
|
|
92
|
+
readonly data: unknown;
|
|
93
|
+
readonly last?: StreamRunToResult;
|
|
94
|
+
};
|
|
95
|
+
//# sourceMappingURL=sse.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sse.d.ts","sourceRoot":"","sources":["../src/sse.ts"],"names":[],"mappings":"A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"}
|
package/dist/sse.js
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A reply streamed to the browser as Server-Sent Events, over the response of
|
|
3
|
+
* the request that asked for it (ream's `ctx.response.sse()`).
|
|
4
|
+
*
|
|
5
|
+
* That stream, not a relay channel, is the transport here: it belongs to the
|
|
6
|
+
* one client waiting for this reply, and its close is a sure sign the client
|
|
7
|
+
* left, on every instance, which is what lets the provider request be
|
|
8
|
+
* cancelled. A relay channel reaches whoever subscribed, wherever they are,
|
|
9
|
+
* but cannot tell polaris that nobody listens any more.
|
|
10
|
+
*/
|
|
11
|
+
import { codeOf } from "./log.js";
|
|
12
|
+
/** Why polaris cancelled the provider request: the client closed the stream. */
|
|
13
|
+
export class ClientLeft extends Error {
|
|
14
|
+
constructor() {
|
|
15
|
+
super("the client closed the stream");
|
|
16
|
+
this.name = "ClientLeft";
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Forward `events` to `sink` as events named by their type after `prefix`
|
|
21
|
+
* (`text`, `tool_call`, `done`…, and `error` on a failure), and end the stream
|
|
22
|
+
* after the last one. `leave` is aborted when the client closes the stream.
|
|
23
|
+
* `encode` gives an event's data, and the result when it is the last one.
|
|
24
|
+
* Never rejects: every outcome is in the result.
|
|
25
|
+
*/
|
|
26
|
+
export async function pipeToSse(events, sink, leave, prefix, encode) {
|
|
27
|
+
try {
|
|
28
|
+
for await (const event of events) {
|
|
29
|
+
if (leave.signal.aborted)
|
|
30
|
+
break;
|
|
31
|
+
const { data, last } = encode(event);
|
|
32
|
+
const sent = await sink.send(`${prefix}${event.type}`, data);
|
|
33
|
+
if (!sent) {
|
|
34
|
+
leave.abort(new ClientLeft());
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
if (last !== undefined) {
|
|
38
|
+
await settle(() => sink.end());
|
|
39
|
+
return last;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
// Leaving the loop early cancels the provider request.
|
|
43
|
+
return { status: "client_left" };
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
if (leave.signal.aborted)
|
|
47
|
+
return { status: "client_left" };
|
|
48
|
+
// Only the code goes to the page: a message can carry the provider's
|
|
49
|
+
// details, or the application's.
|
|
50
|
+
await settle(() => sink.send(`${prefix}error`, { code: codeOf(error) }));
|
|
51
|
+
await settle(() => sink.end());
|
|
52
|
+
return { status: "failed", error };
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Run `step` and swallow its failure: the stream is already closing, and the
|
|
57
|
+
* caller of `streamTo()` usually does not await it (a ream route must return
|
|
58
|
+
* before its stream starts), so a rejection would go unhandled.
|
|
59
|
+
*/
|
|
60
|
+
async function settle(step) {
|
|
61
|
+
try {
|
|
62
|
+
await step();
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
// The stream is gone; there is nobody left to tell.
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/** A streamed reply's event, as `streamTo()` sends it. */
|
|
69
|
+
export function encodeStreamEvent(event) {
|
|
70
|
+
switch (event.type) {
|
|
71
|
+
case "text":
|
|
72
|
+
case "reasoning":
|
|
73
|
+
return { data: { text: event.text } };
|
|
74
|
+
case "tool_call":
|
|
75
|
+
return { data: toolCallData(event.call) };
|
|
76
|
+
case "done":
|
|
77
|
+
return {
|
|
78
|
+
data: doneData(event.response),
|
|
79
|
+
last: { status: "done", response: event.response },
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/** A streamed run's event, as `streamRunTo()` sends it. */
|
|
84
|
+
export function encodeRunEvent(event) {
|
|
85
|
+
switch (event.type) {
|
|
86
|
+
case "text":
|
|
87
|
+
case "reasoning":
|
|
88
|
+
return { data: { text: event.text } };
|
|
89
|
+
case "tool_call":
|
|
90
|
+
return { data: toolCallData(event.call) };
|
|
91
|
+
case "tool_result": {
|
|
92
|
+
const data = {
|
|
93
|
+
id: event.call.id,
|
|
94
|
+
name: event.call.name,
|
|
95
|
+
isError: event.result.isError === true,
|
|
96
|
+
};
|
|
97
|
+
return { data };
|
|
98
|
+
}
|
|
99
|
+
case "done": {
|
|
100
|
+
const { result } = event;
|
|
101
|
+
const data = {
|
|
102
|
+
...doneData(result.response),
|
|
103
|
+
usage: result.usage,
|
|
104
|
+
steps: result.steps,
|
|
105
|
+
};
|
|
106
|
+
return { data, last: { status: "done", result } };
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
function toolCallData(call) {
|
|
111
|
+
return { id: call.id, name: call.name, input: call.input };
|
|
112
|
+
}
|
|
113
|
+
function doneData(response) {
|
|
114
|
+
const { id, provider, model, text, stopReason, usage } = response;
|
|
115
|
+
return { id, provider, model, text, stopReason, usage };
|
|
116
|
+
}
|
|
117
|
+
//# sourceMappingURL=sse.js.map
|
package/dist/sse.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sse.js","sourceRoot":"","sources":["../src/sse.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAgElC,gFAAgF;AAChF,MAAM,OAAO,UAAW,SAAQ,KAAK;IACpC;QACC,KAAK,CAAC,8BAA8B,CAAC,CAAC;QACtC,IAAI,CAAC,IAAI,GAAG,YAAY,CAAC;IAC1B,CAAC;CACD;AAED;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,SAAS,CAC9B,MAAwB,EACxB,IAAa,EACb,KAAsB,EACtB,MAAc,EACd,MAAmE;IAEnE,IAAI,CAAC;QACJ,IAAI,KAAK,EAAE,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;YAClC,IAAI,KAAK,CAAC,MAAM,CAAC,OAAO;gBAAE,MAAM;YAChC,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;YACrC,MAAM,IAAI,G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B,CAAC;YACF,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,EAAE,CAAC;QACnD,CAAC;IACF,CAAC;AACF,CAAC;AAED,SAAS,YAAY,CAAC,IAAkB;IACvC,OAAO,EAAE,EAAE,EAAE,IAAI,CAAC,EAAE,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,CAAC;AAC5D,CAAC;AAED,SAAS,QAAQ,CAAC,QAAsB;IACvC,MAAM,EAAE,EAAE,EAAE,QAAQ,EAAE,KAAK,EAAE,IAAI,EAAE,UAAU,EAAE,KAAK,EAAE,GAAG,QAAQ,CAAC;IAClE,OAAO,EAAE,EAAE,EAAE,QAAQ,EAAE,KAAK,EAAE,IAAI,EAAE,UAAU,EAAE,KAAK,EAAE,CAAC;AACzD,CAAC"}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A provider that answers from a script, for tests: no network, no key.
|
|
3
|
+
*
|
|
4
|
+
* const fake = polaris.fake()
|
|
5
|
+
* fake.reply('Bonjour')
|
|
6
|
+
* await myAssistant.answer('hi')
|
|
7
|
+
* fake.assertSent((request) => request.system?.includes('newcomer'))
|
|
8
|
+
* polaris.restore()
|
|
9
|
+
*
|
|
10
|
+
* Every provider of the manager answers from the fake until `restore()`. It
|
|
11
|
+
* records each call, and reports usage, so cost and budget code runs as it
|
|
12
|
+
* would against a provider.
|
|
13
|
+
*/
|
|
14
|
+
import type { ChatRequest, DriverFeatures, EmbedRequest, PolarisDriver, StopReason, Usage } from "../types.js";
|
|
15
|
+
/** One scripted reply. */
|
|
16
|
+
export interface FakeReply {
|
|
17
|
+
readonly text?: string;
|
|
18
|
+
readonly reasoning?: string;
|
|
19
|
+
readonly toolCalls?: readonly {
|
|
20
|
+
readonly name: string;
|
|
21
|
+
readonly input: unknown;
|
|
22
|
+
readonly id?: string;
|
|
23
|
+
}[];
|
|
24
|
+
/** Default: `tool_use` when the reply calls tools, `end` otherwise. */
|
|
25
|
+
readonly stopReason?: StopReason;
|
|
26
|
+
readonly usage?: Partial<Usage>;
|
|
27
|
+
/** Default: the request's model, or the fake's. */
|
|
28
|
+
readonly model?: string;
|
|
29
|
+
/** How a stream sends the text. Default: the whole text in one event. */
|
|
30
|
+
readonly chunks?: readonly string[];
|
|
31
|
+
}
|
|
32
|
+
export interface FakeOptions {
|
|
33
|
+
/** What the fake provider supports. Default: everything. */
|
|
34
|
+
readonly features?: Partial<DriverFeatures>;
|
|
35
|
+
/** The model it answers with when a request names none. Default `fake-model`. */
|
|
36
|
+
readonly model?: string;
|
|
37
|
+
/** Its embedding model. Default `fake-embedding`. */
|
|
38
|
+
readonly embeddingModel?: string;
|
|
39
|
+
/** The length of the vectors it returns. Default 8. */
|
|
40
|
+
readonly dimensions?: number;
|
|
41
|
+
/** Its output limit for a request that sets none, for budget estimates. Default 1 000. */
|
|
42
|
+
readonly maxTokens?: number;
|
|
43
|
+
}
|
|
44
|
+
/** A call the fake received. */
|
|
45
|
+
export interface FakeCall {
|
|
46
|
+
readonly operation: "chat" | "stream" | "embed";
|
|
47
|
+
/** The provider's name in the config. */
|
|
48
|
+
readonly provider: string;
|
|
49
|
+
readonly request: ChatRequest | EmbedRequest;
|
|
50
|
+
/** The per-call key, if one was given. */
|
|
51
|
+
readonly apiKey: string | undefined;
|
|
52
|
+
}
|
|
53
|
+
export declare class FakeProvider {
|
|
54
|
+
#private;
|
|
55
|
+
constructor(options?: FakeOptions);
|
|
56
|
+
/** Answer the next request with `reply`: a text, or a scripted reply. */
|
|
57
|
+
reply(reply: string | FakeReply): this;
|
|
58
|
+
/**
|
|
59
|
+
* Answer the next request with `value` as structured output: as JSON text
|
|
60
|
+
* when the request asks for structured output, as a call to its tool when
|
|
61
|
+
* it asks through a tool.
|
|
62
|
+
*/
|
|
63
|
+
replyJson(value: unknown, extra?: Omit<FakeReply, "text" | "toolCalls">): this;
|
|
64
|
+
/** Answer the next request with a call to the tool `name`. */
|
|
65
|
+
callTool(name: string, input: unknown, id?: string): this;
|
|
66
|
+
/** Make the next request fail with `error`. */
|
|
67
|
+
fail(error: unknown): this;
|
|
68
|
+
/** Compute each input's vector with `embed` instead of the fake's own. */
|
|
69
|
+
embedWith(embed: (input: string) => readonly number[]): this;
|
|
70
|
+
/** Every call received, in order. */
|
|
71
|
+
get calls(): readonly FakeCall[];
|
|
72
|
+
/** The chat and stream requests received, in order. */
|
|
73
|
+
get requests(): readonly ChatRequest[];
|
|
74
|
+
/** Replies scripted and not used yet. */
|
|
75
|
+
get pending(): number;
|
|
76
|
+
/** Fail unless a chat or stream request matching `matches` was received. */
|
|
77
|
+
assertSent(matches?: (request: ChatRequest) => boolean): void;
|
|
78
|
+
/** Fail if a chat or stream request matching `matches` was received. */
|
|
79
|
+
assertNotSent(matches?: (request: ChatRequest) => boolean): void;
|
|
80
|
+
/** Fail unless exactly `count` chat and stream requests were received. */
|
|
81
|
+
assertSentCount(count: number): void;
|
|
82
|
+
/** The driver this fake stands in with, for the provider named `name`. */
|
|
83
|
+
driver(name: string): PolarisDriver;
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=FakeProvider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FakeProvider.d.ts","sourceRoot":"","sources":["../../src/testing/FakeProvider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAOH,OAAO,KAAK,EAEX,WAAW,EAGX,cAAc,EACd,YAAY,EAEZ,aAAa,EACb,UAAU,EAGV,KAAK,EACL,MAAM,aAAa,CAAC;AAErB,0BAA0B;AAC1B,MAAM,WAAW,SAAS;IACzB,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,SAAS,CAAC,EAAE,SAAS;QAC7B,QAAQ,CAAC,IAAI,EAAE,MA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,EAAE,OAAO,EACd,KAAK,GAAE,IAAI,CAAC,SAAS,EAAE,MAAM,GAAG,WAAW,CAAM,GAC/C,IAAI;IAcP,8DAA8D;IAC9D,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,EAAE,CAAC,EAAE,MAAM,GAAG,IAAI;IAMzD,+CAA+C;IAC/C,IAAI,CAAC,KAAK,EAAE,OAAO,GAAG,IAAI;IAK1B,0EAA0E;IAC1E,SAAS,CAAC,KAAK,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,SAAS,MAAM,EAAE,GAAG,IAAI;IAK5D,qCAAqC;IACrC,IAAI,KAAK,IAAI,SAAS,QAAQ,EAAE,CAE/B;IAED,uDAAuD;IACvD,IAAI,QAAQ,IAAI,SAAS,WAAW,EAAE,CAIrC;IAED,yCAAyC;IACzC,IAAI,OAAO,IAAI,MAAM,CAEpB;IAED,4EAA4E;IAC5E,UAAU,CAAC,OAAO,GAAE,CAAC,OAAO,EAAE,WAAW,KAAK,OAAoB,GAAG,IAAI;IASzE,wEAAwE;IACxE,aAAa,CAAC,OAAO,GAAE,CAAC,OAAO,EAAE,WAAW,KAAK,OAAoB,GAAG,IAAI;IAU5E,0EAA0E;IAC1E,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IASpC,0EAA0E;IAC1E,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,aAAa;CAwGnC"}
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A provider that answers from a script, for tests: no network, no key.
|
|
3
|
+
*
|
|
4
|
+
* const fake = polaris.fake()
|
|
5
|
+
* fake.reply('Bonjour')
|
|
6
|
+
* await myAssistant.answer('hi')
|
|
7
|
+
* fake.assertSent((request) => request.system?.includes('newcomer'))
|
|
8
|
+
* polaris.restore()
|
|
9
|
+
*
|
|
10
|
+
* Every provider of the manager answers from the fake until `restore()`. It
|
|
11
|
+
* records each call, and reports usage, so cost and budget code runs as it
|
|
12
|
+
* would against a provider.
|
|
13
|
+
*/
|
|
14
|
+
import { E_POLARIS_ASSERTION_FAILED, E_POLARIS_FAKE_NO_REPLY, } from "../errors.js";
|
|
15
|
+
import { createResponse } from "../response.js";
|
|
16
|
+
export class FakeProvider {
|
|
17
|
+
#options;
|
|
18
|
+
#steps = [];
|
|
19
|
+
#calls = [];
|
|
20
|
+
#embedding;
|
|
21
|
+
constructor(options = {}) {
|
|
22
|
+
this.#options = options;
|
|
23
|
+
const dimensions = options.dimensions ?? 8;
|
|
24
|
+
this.#embedding = (input) => deterministicVector(input, dimensions);
|
|
25
|
+
}
|
|
26
|
+
/** Answer the next request with `reply`: a text, or a scripted reply. */
|
|
27
|
+
reply(reply) {
|
|
28
|
+
const scripted = typeof reply === "string" ? { text: reply } : reply;
|
|
29
|
+
this.#steps.push((request) => this.#response(request, scripted));
|
|
30
|
+
return this;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Answer the next request with `value` as structured output: as JSON text
|
|
34
|
+
* when the request asks for structured output, as a call to its tool when
|
|
35
|
+
* it asks through a tool.
|
|
36
|
+
*/
|
|
37
|
+
replyJson(value, extra = {}) {
|
|
38
|
+
this.#steps.push((request) => {
|
|
39
|
+
const tool = request.output === undefined ? request.tools?.[0] : undefined;
|
|
40
|
+
return this.#response(request, tool
|
|
41
|
+
? { ...extra, toolCalls: [{ name: tool.name, input: value }] }
|
|
42
|
+
: { ...extra, text: JSON.stringify(value) });
|
|
43
|
+
});
|
|
44
|
+
return this;
|
|
45
|
+
}
|
|
46
|
+
/** Answer the next request with a call to the tool `name`. */
|
|
47
|
+
callTool(name, input, id) {
|
|
48
|
+
return this.reply({
|
|
49
|
+
toolCalls: [{ name, input, ...(id !== undefined ? { id } : {}) }],
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
/** Make the next request fail with `error`. */
|
|
53
|
+
fail(error) {
|
|
54
|
+
this.#steps.push(() => Promise.reject(error));
|
|
55
|
+
return this;
|
|
56
|
+
}
|
|
57
|
+
/** Compute each input's vector with `embed` instead of the fake's own. */
|
|
58
|
+
embedWith(embed) {
|
|
59
|
+
this.#embedding = embed;
|
|
60
|
+
return this;
|
|
61
|
+
}
|
|
62
|
+
/** Every call received, in order. */
|
|
63
|
+
get calls() {
|
|
64
|
+
return this.#calls;
|
|
65
|
+
}
|
|
66
|
+
/** The chat and stream requests received, in order. */
|
|
67
|
+
get requests() {
|
|
68
|
+
return this.#calls.flatMap((call) => call.operation === "embed" || !isChat(call.request) ? [] : [call.request]);
|
|
69
|
+
}
|
|
70
|
+
/** Replies scripted and not used yet. */
|
|
71
|
+
get pending() {
|
|
72
|
+
return this.#steps.length;
|
|
73
|
+
}
|
|
74
|
+
/** Fail unless a chat or stream request matching `matches` was received. */
|
|
75
|
+
assertSent(matches = () => true) {
|
|
76
|
+
if (!this.requests.some(matches)) {
|
|
77
|
+
throw new E_POLARIS_ASSERTION_FAILED([
|
|
78
|
+
"assertSent",
|
|
79
|
+
`no request out of ${this.requests.length} matched`,
|
|
80
|
+
]);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/** Fail if a chat or stream request matching `matches` was received. */
|
|
84
|
+
assertNotSent(matches = () => true) {
|
|
85
|
+
const count = this.requests.filter(matches).length;
|
|
86
|
+
if (count > 0) {
|
|
87
|
+
throw new E_POLARIS_ASSERTION_FAILED([
|
|
88
|
+
"assertNotSent",
|
|
89
|
+
`${count} requests matched`,
|
|
90
|
+
]);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
/** Fail unless exactly `count` chat and stream requests were received. */
|
|
94
|
+
assertSentCount(count) {
|
|
95
|
+
if (this.requests.length !== count) {
|
|
96
|
+
throw new E_POLARIS_ASSERTION_FAILED([
|
|
97
|
+
"assertSentCount",
|
|
98
|
+
`expected ${count} requests, received ${this.requests.length}`,
|
|
99
|
+
]);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/** The driver this fake stands in with, for the provider named `name`. */
|
|
103
|
+
driver(name) {
|
|
104
|
+
const options = this.#options;
|
|
105
|
+
const record = (operation, request, call) => this.#record(operation, name, request, call);
|
|
106
|
+
const next = (request) => this.#next(request);
|
|
107
|
+
const chunksOf = (response) => this.#chunks.get(response);
|
|
108
|
+
const embed = (request) => this.#embed(request);
|
|
109
|
+
return {
|
|
110
|
+
provider: "fake",
|
|
111
|
+
features: {
|
|
112
|
+
tools: true,
|
|
113
|
+
structuredOutput: true,
|
|
114
|
+
images: true,
|
|
115
|
+
serverTools: true,
|
|
116
|
+
...options.features,
|
|
117
|
+
},
|
|
118
|
+
defaults: {
|
|
119
|
+
model: options.model ?? "fake-model",
|
|
120
|
+
embeddingModel: options.embeddingModel ?? "fake-embedding",
|
|
121
|
+
maxTokens: options.maxTokens ?? 1_000,
|
|
122
|
+
streamMaxTokens: options.maxTokens ?? 1_000,
|
|
123
|
+
},
|
|
124
|
+
async chat(request, call) {
|
|
125
|
+
record("chat", request, call);
|
|
126
|
+
return next(request);
|
|
127
|
+
},
|
|
128
|
+
async *stream(request, call) {
|
|
129
|
+
record("stream", request, call);
|
|
130
|
+
const response = await next(request);
|
|
131
|
+
yield* streamOf(response, chunksOf(response));
|
|
132
|
+
},
|
|
133
|
+
async embed(request, call) {
|
|
134
|
+
record("embed", request, call);
|
|
135
|
+
return embed(request);
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
/** The chunks a reply streams its text in, when it was scripted with some. */
|
|
140
|
+
#chunks = new WeakMap();
|
|
141
|
+
#record(operation, provider, request, call) {
|
|
142
|
+
this.#calls.push({ operation, provider, request, apiKey: call.apiKey });
|
|
143
|
+
}
|
|
144
|
+
async #next(request) {
|
|
145
|
+
const step = this.#steps.shift();
|
|
146
|
+
if (step === undefined)
|
|
147
|
+
throw new E_POLARIS_FAKE_NO_REPLY();
|
|
148
|
+
return step(request);
|
|
149
|
+
}
|
|
150
|
+
#response(request, reply) {
|
|
151
|
+
const calls = (reply.toolCalls ?? []).map((call, index) => ({
|
|
152
|
+
type: "tool_call",
|
|
153
|
+
id: call.id ?? `call_${this.#calls.length}_${index}`,
|
|
154
|
+
name: call.name,
|
|
155
|
+
input: call.input,
|
|
156
|
+
}));
|
|
157
|
+
const content = [];
|
|
158
|
+
if (reply.reasoning !== undefined)
|
|
159
|
+
content.push({ type: "reasoning", text: reply.reasoning });
|
|
160
|
+
if (reply.text !== undefined && reply.text !== "")
|
|
161
|
+
content.push({ type: "text", text: reply.text });
|
|
162
|
+
content.push(...calls);
|
|
163
|
+
const response = createResponse({
|
|
164
|
+
id: `fake_${this.#calls.length}`,
|
|
165
|
+
provider: "fake",
|
|
166
|
+
model: reply.model ?? request.model ?? this.#options.model ?? "fake-model",
|
|
167
|
+
content,
|
|
168
|
+
stopReason: reply.stopReason ?? (calls.length > 0 ? "tool_use" : "end"),
|
|
169
|
+
usage: { inputTokens: 10, outputTokens: 10, ...reply.usage },
|
|
170
|
+
});
|
|
171
|
+
if (reply.chunks !== undefined)
|
|
172
|
+
this.#chunks.set(response, reply.chunks);
|
|
173
|
+
return response;
|
|
174
|
+
}
|
|
175
|
+
#embed(request) {
|
|
176
|
+
const vectors = request.inputs.map((input) => Float32Array.from(this.#embedding(input)));
|
|
177
|
+
return {
|
|
178
|
+
provider: "fake",
|
|
179
|
+
model: request.model ?? this.#options.embeddingModel ?? "fake-embedding",
|
|
180
|
+
dimensions: vectors[0]?.length ?? 0,
|
|
181
|
+
vectors,
|
|
182
|
+
usage: {
|
|
183
|
+
inputTokens: request.inputs.reduce((total, input) => total + input.length, 0),
|
|
184
|
+
},
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
/** The events a stream of `response` yields: reasoning, the text in chunks, the tool calls, done. */
|
|
189
|
+
function* streamOf(response, chunks) {
|
|
190
|
+
for (const part of response.message.content) {
|
|
191
|
+
if (part.type === "reasoning")
|
|
192
|
+
yield { type: "reasoning", text: part.text };
|
|
193
|
+
}
|
|
194
|
+
for (const text of chunks ?? (response.text === "" ? [] : [response.text])) {
|
|
195
|
+
yield { type: "text", text };
|
|
196
|
+
}
|
|
197
|
+
for (const call of response.toolCalls)
|
|
198
|
+
yield { type: "tool_call", call };
|
|
199
|
+
yield { type: "done", response };
|
|
200
|
+
}
|
|
201
|
+
/** A vector that depends only on the text: the same input always lands at the same place. */
|
|
202
|
+
function deterministicVector(input, dimensions) {
|
|
203
|
+
const vector = new Array(dimensions).fill(0);
|
|
204
|
+
for (let index = 0; index < input.length; index++) {
|
|
205
|
+
const slot = index % dimensions;
|
|
206
|
+
vector[slot] = ((vector[slot] ?? 0) + input.charCodeAt(index)) % 1_000;
|
|
207
|
+
}
|
|
208
|
+
const norm = Math.hypot(...vector) || 1;
|
|
209
|
+
return vector.map((value) => value / norm);
|
|
210
|
+
}
|
|
211
|
+
function isChat(request) {
|
|
212
|
+
return "messages" in request;
|
|
213
|
+
}
|
|
214
|
+
//# sourceMappingURL=FakeProvider.js.map
|