@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
package/src/Polaris.ts
ADDED
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
import { memoryBudgetStore } from "./budget.js";
|
|
2
|
+
import { checkPricing } from "./cost.js";
|
|
3
|
+
import {
|
|
4
|
+
E_POLARIS_INVALID_CONFIG,
|
|
5
|
+
E_POLARIS_NO_DEFAULT_PROVIDER,
|
|
6
|
+
E_POLARIS_UNKNOWN_PROVIDER,
|
|
7
|
+
} from "./errors.js";
|
|
8
|
+
import type { ExtractRequest, ExtractResult } from "./extract.js";
|
|
9
|
+
import {
|
|
10
|
+
type CallOptions,
|
|
11
|
+
type ClientOptions,
|
|
12
|
+
type ExtractOptions,
|
|
13
|
+
isTimeout,
|
|
14
|
+
MAX_TIMEOUT,
|
|
15
|
+
PolarisClient,
|
|
16
|
+
type RunEvent,
|
|
17
|
+
type RunOptions,
|
|
18
|
+
type RunRequest,
|
|
19
|
+
type RunResult,
|
|
20
|
+
type StreamToOptions,
|
|
21
|
+
} from "./PolarisClient.js";
|
|
22
|
+
import type { SchemaOutput, StandardSchema } from "./schema.js";
|
|
23
|
+
import type { SseSink, StreamRunToResult, StreamToResult } from "./sse.js";
|
|
24
|
+
import { type FakeOptions, FakeProvider } from "./testing/FakeProvider.js";
|
|
25
|
+
import type {
|
|
26
|
+
ChatRequest,
|
|
27
|
+
ChatResponse,
|
|
28
|
+
EmbedRequest,
|
|
29
|
+
EmbedResponse,
|
|
30
|
+
PolarisConfig,
|
|
31
|
+
PolarisDriver,
|
|
32
|
+
ProviderFactory,
|
|
33
|
+
ProvidersList,
|
|
34
|
+
StreamEvent,
|
|
35
|
+
} from "./types.js";
|
|
36
|
+
|
|
37
|
+
const DEFAULT_TIMEOUT = 600_000;
|
|
38
|
+
const DEFAULT_MAX_RESPONSE_BYTES = 32 * 1024 * 1024;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Declare the providers an application talks to.
|
|
42
|
+
*
|
|
43
|
+
* const polarisConfig = defineConfig({
|
|
44
|
+
* default: 'claude',
|
|
45
|
+
* providers: {
|
|
46
|
+
* claude: providers.anthropic({ apiKey: env.get('ANTHROPIC_API_KEY'), model: 'claude-opus-5-5' }),
|
|
47
|
+
* },
|
|
48
|
+
* })
|
|
49
|
+
* export default polarisConfig
|
|
50
|
+
*
|
|
51
|
+
* declare module '@c9up/polaris/types' {
|
|
52
|
+
* interface ProvidersList extends InferProviders<typeof polarisConfig> {}
|
|
53
|
+
* }
|
|
54
|
+
*
|
|
55
|
+
* The shape is @adonisjs/mail's (`default`, named entries built by helpers,
|
|
56
|
+
* a list interface the application augments). Adonis has no LLM package.
|
|
57
|
+
*
|
|
58
|
+
* NAMED DEVIATION: it returns the config itself, where @adonisjs/mail wraps it
|
|
59
|
+
* in a `ConfigProvider` resolved at boot. A provider factory needs nothing
|
|
60
|
+
* from the application, and the other packages of this cohort (rover, nova)
|
|
61
|
+
* return plain config the same way.
|
|
62
|
+
*/
|
|
63
|
+
export function defineConfig<
|
|
64
|
+
KnownProviders extends Record<string, ProviderFactory>,
|
|
65
|
+
>(config: PolarisConfig<KnownProviders>): PolarisConfig<KnownProviders> {
|
|
66
|
+
return config;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* The providers the application declared in {@link ProvidersList}, as an
|
|
71
|
+
* object type `Polaris` can take. A mapped type, because an interface carries
|
|
72
|
+
* no index signature and is never assignable to a `Record`.
|
|
73
|
+
*/
|
|
74
|
+
export type DeclaredProviders = {
|
|
75
|
+
[K in keyof ProvidersList & string]: Extract<
|
|
76
|
+
ProvidersList[K],
|
|
77
|
+
ProviderFactory
|
|
78
|
+
>;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
/** The manager `@c9up/polaris/services/main` and the container hand out. */
|
|
82
|
+
export type PolarisService = Polaris<DeclaredProviders>;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The provider manager: builds each declared provider the first time it is
|
|
86
|
+
* used and keeps it for the life of the process.
|
|
87
|
+
*/
|
|
88
|
+
export class Polaris<KnownProviders extends Record<string, ProviderFactory>> {
|
|
89
|
+
readonly #providers: Record<string, ProviderFactory>;
|
|
90
|
+
readonly #default: string | undefined;
|
|
91
|
+
readonly #options: ClientOptions;
|
|
92
|
+
readonly #clients = new Map<string, PolarisClient>();
|
|
93
|
+
#fake:
|
|
94
|
+
| {
|
|
95
|
+
readonly provider: FakeProvider;
|
|
96
|
+
readonly clients: Map<string, PolarisClient>;
|
|
97
|
+
}
|
|
98
|
+
| undefined;
|
|
99
|
+
|
|
100
|
+
constructor(config: PolarisConfig<KnownProviders>) {
|
|
101
|
+
const providers: Record<string, ProviderFactory> = config.providers;
|
|
102
|
+
const declared = Object.keys(providers);
|
|
103
|
+
if (declared.length === 0) {
|
|
104
|
+
throw new E_POLARIS_INVALID_CONFIG(["`providers` declares none"]);
|
|
105
|
+
}
|
|
106
|
+
for (const name of declared) {
|
|
107
|
+
if (typeof providers[name] !== "function") {
|
|
108
|
+
throw new E_POLARIS_INVALID_CONFIG([
|
|
109
|
+
`providers.${name} is not a provider factory`,
|
|
110
|
+
]);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (
|
|
114
|
+
config.default !== undefined &&
|
|
115
|
+
!Object.hasOwn(providers, config.default)
|
|
116
|
+
) {
|
|
117
|
+
throw new E_POLARIS_INVALID_CONFIG([
|
|
118
|
+
`default names '${config.default}', which is not in providers (${declared.join(", ")})`,
|
|
119
|
+
]);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const timeout = config.timeout ?? DEFAULT_TIMEOUT;
|
|
123
|
+
if (!isTimeout(timeout)) {
|
|
124
|
+
throw new E_POLARIS_INVALID_CONFIG([
|
|
125
|
+
`timeout must be a positive integer of ms, at most ${MAX_TIMEOUT}`,
|
|
126
|
+
]);
|
|
127
|
+
}
|
|
128
|
+
const maxResponseBytes =
|
|
129
|
+
config.maxResponseBytes ?? DEFAULT_MAX_RESPONSE_BYTES;
|
|
130
|
+
if (!Number.isSafeInteger(maxResponseBytes) || maxResponseBytes <= 0) {
|
|
131
|
+
throw new E_POLARIS_INVALID_CONFIG([
|
|
132
|
+
"maxResponseBytes must be a positive integer",
|
|
133
|
+
]);
|
|
134
|
+
}
|
|
135
|
+
if (config.fetch !== undefined && typeof config.fetch !== "function") {
|
|
136
|
+
throw new E_POLARIS_INVALID_CONFIG(["fetch must be a function"]);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (config.pricing !== undefined) checkPricing(config.pricing);
|
|
140
|
+
if (config.budget !== undefined && config.pricing === undefined) {
|
|
141
|
+
throw new E_POLARIS_INVALID_CONFIG([
|
|
142
|
+
"a `budget` needs a `pricing` table to know what requests cost",
|
|
143
|
+
]);
|
|
144
|
+
}
|
|
145
|
+
const period: unknown = config.budget?.period;
|
|
146
|
+
if (period !== undefined && period !== "day" && period !== "month") {
|
|
147
|
+
throw new E_POLARIS_INVALID_CONFIG([
|
|
148
|
+
"budget.period must be 'day' or 'month'",
|
|
149
|
+
]);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
this.#providers = providers;
|
|
153
|
+
this.#default = config.default;
|
|
154
|
+
this.#options = {
|
|
155
|
+
timeout,
|
|
156
|
+
maxResponseBytes,
|
|
157
|
+
fetch: config.fetch ?? ((input, init) => fetch(input, init)),
|
|
158
|
+
meter: {
|
|
159
|
+
...(config.pricing !== undefined ? { pricing: config.pricing } : {}),
|
|
160
|
+
...(config.budget !== undefined
|
|
161
|
+
? {
|
|
162
|
+
// One store for every provider of this manager: a scope's spend
|
|
163
|
+
// is its spend, whichever provider it went to.
|
|
164
|
+
budget: {
|
|
165
|
+
...config.budget,
|
|
166
|
+
store: config.budget.store ?? memoryBudgetStore(),
|
|
167
|
+
},
|
|
168
|
+
}
|
|
169
|
+
: {}),
|
|
170
|
+
...(config.log !== undefined ? { log: config.log } : {}),
|
|
171
|
+
},
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* The provider declared under `name`, or the `default` one. Built on first
|
|
177
|
+
* use, then the same instance every time.
|
|
178
|
+
*/
|
|
179
|
+
use<K extends keyof KnownProviders & string>(name?: K): PolarisClient {
|
|
180
|
+
const selected: string | undefined = name ?? this.#default;
|
|
181
|
+
if (selected === undefined) throw new E_POLARIS_NO_DEFAULT_PROVIDER();
|
|
182
|
+
|
|
183
|
+
const factory = Object.hasOwn(this.#providers, selected)
|
|
184
|
+
? this.#providers[selected]
|
|
185
|
+
: undefined;
|
|
186
|
+
if (factory === undefined) {
|
|
187
|
+
throw new E_POLARIS_UNKNOWN_PROVIDER([
|
|
188
|
+
selected,
|
|
189
|
+
`Declared: ${Object.keys(this.#providers).join(", ")}.`,
|
|
190
|
+
]);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const fake = this.#fake;
|
|
194
|
+
if (fake !== undefined) {
|
|
195
|
+
const faked =
|
|
196
|
+
fake.clients.get(selected) ??
|
|
197
|
+
new PolarisClient(
|
|
198
|
+
selected,
|
|
199
|
+
fake.provider.driver(selected),
|
|
200
|
+
this.#options,
|
|
201
|
+
);
|
|
202
|
+
fake.clients.set(selected, faked);
|
|
203
|
+
return faked;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const cached = this.#clients.get(selected);
|
|
207
|
+
if (cached) return cached;
|
|
208
|
+
const driver: unknown = factory();
|
|
209
|
+
if (!isDriver(driver)) {
|
|
210
|
+
throw new E_POLARIS_INVALID_CONFIG([
|
|
211
|
+
`providers.${selected} did not build a driver (provider, features and chat() are required)`,
|
|
212
|
+
]);
|
|
213
|
+
}
|
|
214
|
+
const client = new PolarisClient(selected, driver, this.#options);
|
|
215
|
+
this.#clients.set(selected, client);
|
|
216
|
+
return client;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Answer every request from a script instead of the providers, until
|
|
221
|
+
* `restore()`: for tests. Prices, budgets and the log apply as usual.
|
|
222
|
+
*
|
|
223
|
+
* const fake = polaris.fake()
|
|
224
|
+
* fake.reply('Bonjour')
|
|
225
|
+
*/
|
|
226
|
+
fake(options?: FakeOptions): FakeProvider {
|
|
227
|
+
const provider = new FakeProvider(options);
|
|
228
|
+
this.#fake = { provider, clients: new Map() };
|
|
229
|
+
return provider;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** Back to the real providers after `fake()`. */
|
|
233
|
+
restore(): void {
|
|
234
|
+
this.#fake = undefined;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** `use().chat()`: the default provider. */
|
|
238
|
+
chat(request: ChatRequest, options?: CallOptions): Promise<ChatResponse> {
|
|
239
|
+
return this.use().chat(request, options);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** `use().stream()`: the default provider. */
|
|
243
|
+
stream(
|
|
244
|
+
request: ChatRequest,
|
|
245
|
+
options?: CallOptions,
|
|
246
|
+
): AsyncGenerator<StreamEvent, void, undefined> {
|
|
247
|
+
return this.use().stream(request, options);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** `use().run()`: the default provider. */
|
|
251
|
+
run(request: RunRequest, options?: RunOptions): Promise<RunResult> {
|
|
252
|
+
return this.use().run(request, options);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/** `use().streamRun()`: the default provider. */
|
|
256
|
+
streamRun(
|
|
257
|
+
request: RunRequest,
|
|
258
|
+
options?: RunOptions,
|
|
259
|
+
): AsyncGenerator<RunEvent, void, undefined> {
|
|
260
|
+
return this.use().streamRun(request, options);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/** `use().streamRunTo()`: the default provider. */
|
|
264
|
+
streamRunTo(
|
|
265
|
+
sink: SseSink,
|
|
266
|
+
request: RunRequest,
|
|
267
|
+
options?: RunOptions & StreamToOptions,
|
|
268
|
+
): Promise<StreamRunToResult> {
|
|
269
|
+
return this.use().streamRunTo(sink, request, options);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** `use().streamTo()`: the default provider. */
|
|
273
|
+
streamTo(
|
|
274
|
+
sink: SseSink,
|
|
275
|
+
request: ChatRequest,
|
|
276
|
+
options?: StreamToOptions,
|
|
277
|
+
): Promise<StreamToResult> {
|
|
278
|
+
return this.use().streamTo(sink, request, options);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/** `use().extract()`: the default provider. */
|
|
282
|
+
extract<S extends StandardSchema>(
|
|
283
|
+
request: ExtractRequest<S>,
|
|
284
|
+
options?: ExtractOptions,
|
|
285
|
+
): Promise<ExtractResult<SchemaOutput<S>>> {
|
|
286
|
+
return this.use().extract(request, options);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/** `use().embed()`: the default provider. */
|
|
290
|
+
embed(request: EmbedRequest, options?: CallOptions): Promise<EmbedResponse> {
|
|
291
|
+
return this.use().embed(request, options);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
function isDriver(value: unknown): value is PolarisDriver {
|
|
296
|
+
if (typeof value !== "object" || value === null) return false;
|
|
297
|
+
const features: unknown = Reflect.get(value, "features");
|
|
298
|
+
return (
|
|
299
|
+
typeof Reflect.get(value, "provider") === "string" &&
|
|
300
|
+
typeof Reflect.get(value, "chat") === "function" &&
|
|
301
|
+
typeof features === "object" &&
|
|
302
|
+
features !== null
|
|
303
|
+
);
|
|
304
|
+
}
|