@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,703 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Anthropic driver: Claude through the official SDK (`@anthropic-ai/sdk`,
|
|
3
|
+
* an optional peer, loaded on the first request).
|
|
4
|
+
*
|
|
5
|
+
* Requests go through the SDK's beta namespace, which is the general one plus
|
|
6
|
+
* the beta features (server-side refusal fallbacks among them): one request
|
|
7
|
+
* shape and one response shape whether or not a beta is turned on.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type Anthropic from "@anthropic-ai/sdk";
|
|
11
|
+
import type { BetaMessageStreamParams } from "@anthropic-ai/sdk/resources/beta/messages/messages";
|
|
12
|
+
import {
|
|
13
|
+
E_POLARIS_INVALID_CONFIG,
|
|
14
|
+
E_POLARIS_INVALID_REQUEST,
|
|
15
|
+
E_POLARIS_PROVIDER_REJECTED,
|
|
16
|
+
E_POLARIS_PROVIDER_UNREACHABLE,
|
|
17
|
+
E_POLARIS_RESPONSE_TOO_LARGE,
|
|
18
|
+
} from "../errors.js";
|
|
19
|
+
import { MAX_TIMEOUT } from "../PolarisClient.js";
|
|
20
|
+
|
|
21
|
+
import { createResponse } from "../response.js";
|
|
22
|
+
import { serverToolsProblem } from "../server-tools.js";
|
|
23
|
+
import type {
|
|
24
|
+
AssistantPart,
|
|
25
|
+
ChatRequest,
|
|
26
|
+
ChatResponse,
|
|
27
|
+
DriverCall,
|
|
28
|
+
DriverDefaults,
|
|
29
|
+
DriverFeatures,
|
|
30
|
+
JsonSchema,
|
|
31
|
+
Message,
|
|
32
|
+
Origin,
|
|
33
|
+
PolarisDriver,
|
|
34
|
+
ProviderFactory,
|
|
35
|
+
ServerTools,
|
|
36
|
+
StopReason,
|
|
37
|
+
StreamEvent,
|
|
38
|
+
ToolCallPart,
|
|
39
|
+
} from "../types.js";
|
|
40
|
+
import { errorForStatus } from "./status.js";
|
|
41
|
+
|
|
42
|
+
type Sdk = typeof Anthropic;
|
|
43
|
+
/** The request fields shared by a streamed and a one-shot request. */
|
|
44
|
+
type Params = BetaMessageStreamParams;
|
|
45
|
+
type BlockParam = Anthropic.Beta.BetaContentBlockParam;
|
|
46
|
+
type Block = Anthropic.Beta.BetaContentBlock;
|
|
47
|
+
type MessageParam = Anthropic.Beta.BetaMessageParam;
|
|
48
|
+
|
|
49
|
+
const PROVIDER = "anthropic";
|
|
50
|
+
/** What the server-side refusal fallback needs turned on. */
|
|
51
|
+
const FALLBACK_BETA = "server-side-fallback-2026-07-01";
|
|
52
|
+
/** What the MCP connector (`mcp_servers`) needs turned on. */
|
|
53
|
+
const MCP_BETA = "mcp-client-2025-11-20";
|
|
54
|
+
/** The code execution tool, which runs skills too. */
|
|
55
|
+
const CODE_EXECUTION = {
|
|
56
|
+
type: "code_execution_20260521",
|
|
57
|
+
name: "code_execution",
|
|
58
|
+
} as const;
|
|
59
|
+
/** The most skills one request may load. */
|
|
60
|
+
const MAX_SKILLS = 20;
|
|
61
|
+
/** Output limits when the request sets none: the SDK's own guidance. */
|
|
62
|
+
const DEFAULT_MAX_TOKENS = 16_000;
|
|
63
|
+
const DEFAULT_STREAM_MAX_TOKENS = 64_000;
|
|
64
|
+
|
|
65
|
+
export interface AnthropicConfig {
|
|
66
|
+
/** The model every request uses unless it names another. */
|
|
67
|
+
readonly model: string;
|
|
68
|
+
/**
|
|
69
|
+
* The API key. Without one, the SDK reads `ANTHROPIC_API_KEY` (or another
|
|
70
|
+
* credential it knows) from the environment. A per-call key overrides it.
|
|
71
|
+
*/
|
|
72
|
+
readonly apiKey?: string;
|
|
73
|
+
/** Another endpoint speaking the Anthropic API. */
|
|
74
|
+
readonly baseURL?: string;
|
|
75
|
+
/** Output limit when a request sets none. Default 16 000, 64 000 for a stream. */
|
|
76
|
+
readonly maxTokens?: number;
|
|
77
|
+
/**
|
|
78
|
+
* Retries of a rate-limited, overloaded or failed request, honouring the
|
|
79
|
+
* provider's `retry-after`, all within the call's deadline. Default 2.
|
|
80
|
+
*/
|
|
81
|
+
readonly maxRetries?: number;
|
|
82
|
+
/**
|
|
83
|
+
* Server-side fallback when the model declines a request: `'default'` lets
|
|
84
|
+
* the API pick the fallback model, or name them. Turns on its beta.
|
|
85
|
+
*/
|
|
86
|
+
readonly fallbacks?: "default" | readonly { readonly model: string }[];
|
|
87
|
+
/** Further `anthropic-beta` flags sent with every request. */
|
|
88
|
+
readonly betas?: readonly string[];
|
|
89
|
+
/**
|
|
90
|
+
* Server-side tools every request gets: code execution, skills, MCP
|
|
91
|
+
* servers. Each one a request's `server` sets replaces this one's.
|
|
92
|
+
*/
|
|
93
|
+
readonly server?: Omit<ServerTools, "container">;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const FEATURES: DriverFeatures = {
|
|
97
|
+
tools: true,
|
|
98
|
+
structuredOutput: true,
|
|
99
|
+
images: true,
|
|
100
|
+
serverTools: true,
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
/** A provider factory for Claude: `providers: { claude: anthropic({ model }) }`. */
|
|
104
|
+
export function anthropic(config: AnthropicConfig): ProviderFactory {
|
|
105
|
+
return () => new AnthropicDriver(config);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** What the driver loads from the SDK on its first request. */
|
|
109
|
+
interface Loaded {
|
|
110
|
+
readonly Anthropic: Sdk;
|
|
111
|
+
/**
|
|
112
|
+
* The SDK's own rewrite of a JSON Schema into what Claude's structured
|
|
113
|
+
* output accepts: `additionalProperties: false` on every object, and the
|
|
114
|
+
* constraints it does not enforce (`minLength`, `minimum`…) moved into the
|
|
115
|
+
* description. polaris checks the answer against the full schema after.
|
|
116
|
+
*/
|
|
117
|
+
readonly strictSchema: (
|
|
118
|
+
schema: Record<string, unknown>,
|
|
119
|
+
) => Record<string, unknown>;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
let loading: Promise<Loaded> | undefined;
|
|
123
|
+
|
|
124
|
+
function loadSdk(): Promise<Loaded> {
|
|
125
|
+
loading ??= Promise.all([
|
|
126
|
+
import("@anthropic-ai/sdk"),
|
|
127
|
+
import("@anthropic-ai/sdk/lib/transform-json-schema"),
|
|
128
|
+
]).then(
|
|
129
|
+
([sdk, transform]) => ({
|
|
130
|
+
Anthropic: sdk.default,
|
|
131
|
+
strictSchema: (schema) => transform.transformJSONSchema(schema),
|
|
132
|
+
}),
|
|
133
|
+
(cause: unknown) => {
|
|
134
|
+
loading = undefined;
|
|
135
|
+
throw new E_POLARIS_INVALID_CONFIG(
|
|
136
|
+
[
|
|
137
|
+
"the Anthropic driver needs @anthropic-ai/sdk; install it next to @c9up/polaris",
|
|
138
|
+
],
|
|
139
|
+
{ cause },
|
|
140
|
+
);
|
|
141
|
+
},
|
|
142
|
+
);
|
|
143
|
+
return loading;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
class AnthropicDriver implements PolarisDriver {
|
|
147
|
+
readonly provider = PROVIDER;
|
|
148
|
+
readonly features = FEATURES;
|
|
149
|
+
readonly defaults: DriverDefaults;
|
|
150
|
+
readonly #config: AnthropicConfig;
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* The configured key, and the credentials the SDK reads from the
|
|
154
|
+
* environment when there is none, read when an error is scrubbed.
|
|
155
|
+
*/
|
|
156
|
+
get secrets(): readonly string[] {
|
|
157
|
+
const env = typeof process === "undefined" ? undefined : process.env;
|
|
158
|
+
return [
|
|
159
|
+
this.#config.apiKey,
|
|
160
|
+
env?.ANTHROPIC_API_KEY,
|
|
161
|
+
env?.ANTHROPIC_AUTH_TOKEN,
|
|
162
|
+
...(this.#config.server?.mcpServers ?? []).map(
|
|
163
|
+
(mcp) => mcp.authorizationToken,
|
|
164
|
+
),
|
|
165
|
+
].filter((secret): secret is string => typeof secret === "string");
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
constructor(config: AnthropicConfig) {
|
|
169
|
+
if (typeof config.model !== "string" || config.model.trim() === "") {
|
|
170
|
+
throw new E_POLARIS_INVALID_CONFIG([
|
|
171
|
+
"the Anthropic driver needs a `model`",
|
|
172
|
+
]);
|
|
173
|
+
}
|
|
174
|
+
if (config.maxTokens !== undefined && !isCount(config.maxTokens)) {
|
|
175
|
+
throw new E_POLARIS_INVALID_CONFIG([
|
|
176
|
+
"the Anthropic driver's `maxTokens` must be a positive integer",
|
|
177
|
+
]);
|
|
178
|
+
}
|
|
179
|
+
if (
|
|
180
|
+
config.maxRetries !== undefined &&
|
|
181
|
+
!(Number.isInteger(config.maxRetries) && config.maxRetries >= 0)
|
|
182
|
+
) {
|
|
183
|
+
throw new E_POLARIS_INVALID_CONFIG([
|
|
184
|
+
"the Anthropic driver's `maxRetries` must be an integer, 0 or more",
|
|
185
|
+
]);
|
|
186
|
+
}
|
|
187
|
+
const problem =
|
|
188
|
+
config.server !== undefined
|
|
189
|
+
? serverToolsProblem(config.server)
|
|
190
|
+
: undefined;
|
|
191
|
+
if (problem !== undefined) {
|
|
192
|
+
throw new E_POLARIS_INVALID_CONFIG([`the Anthropic driver's ${problem}`]);
|
|
193
|
+
}
|
|
194
|
+
this.#config = config;
|
|
195
|
+
this.defaults = {
|
|
196
|
+
model: config.model,
|
|
197
|
+
maxTokens: config.maxTokens ?? DEFAULT_MAX_TOKENS,
|
|
198
|
+
streamMaxTokens: config.maxTokens ?? DEFAULT_STREAM_MAX_TOKENS,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
async chat(request: ChatRequest, call: DriverCall): Promise<ChatResponse> {
|
|
203
|
+
const { Anthropic: sdk, strictSchema } = await loadSdk();
|
|
204
|
+
const client = this.#client(sdk, call);
|
|
205
|
+
try {
|
|
206
|
+
const message = await client.beta.messages.create(
|
|
207
|
+
{ ...this.#params(request, false, strictSchema), stream: false },
|
|
208
|
+
{ signal: call.signal },
|
|
209
|
+
);
|
|
210
|
+
return toResponse(message);
|
|
211
|
+
} catch (error) {
|
|
212
|
+
throw toPolarisError(sdk, error);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
async *stream(
|
|
217
|
+
request: ChatRequest,
|
|
218
|
+
call: DriverCall,
|
|
219
|
+
): AsyncGenerator<StreamEvent, void, undefined> {
|
|
220
|
+
const { Anthropic: sdk, strictSchema } = await loadSdk();
|
|
221
|
+
const client = this.#client(sdk, call);
|
|
222
|
+
const stream = client.beta.messages.stream(
|
|
223
|
+
this.#params(request, true, strictSchema),
|
|
224
|
+
{
|
|
225
|
+
signal: call.signal,
|
|
226
|
+
},
|
|
227
|
+
);
|
|
228
|
+
let ended = false;
|
|
229
|
+
try {
|
|
230
|
+
for await (const event of stream) {
|
|
231
|
+
if (event.type === "content_block_delta") {
|
|
232
|
+
if (event.delta.type === "text_delta") {
|
|
233
|
+
yield { type: "text", text: event.delta.text };
|
|
234
|
+
} else if (event.delta.type === "thinking_delta") {
|
|
235
|
+
yield { type: "reasoning", text: event.delta.thinking };
|
|
236
|
+
}
|
|
237
|
+
} else if (event.type === "content_block_stop") {
|
|
238
|
+
// The SDK has parsed the tool's arguments by the time its block
|
|
239
|
+
// closes; before that they are a partial JSON string.
|
|
240
|
+
const block = stream.currentMessage?.content[event.index];
|
|
241
|
+
if (block?.type === "tool_use") {
|
|
242
|
+
yield { type: "tool_call", call: toToolCall(block) };
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
const response = toResponse(await stream.finalMessage());
|
|
247
|
+
ended = true;
|
|
248
|
+
yield { type: "done", response };
|
|
249
|
+
} catch (error) {
|
|
250
|
+
throw toPolarisError(sdk, error);
|
|
251
|
+
} finally {
|
|
252
|
+
if (!ended) stream.abort();
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/** One client per call: the call's own key and its bounded `fetch`. */
|
|
257
|
+
#client(sdk: Sdk, call: DriverCall): Anthropic {
|
|
258
|
+
return new sdk({
|
|
259
|
+
apiKey: call.apiKey ?? this.#config.apiKey,
|
|
260
|
+
baseURL: this.#config.baseURL,
|
|
261
|
+
fetch: call.fetch,
|
|
262
|
+
maxRetries: this.#config.maxRetries ?? 2,
|
|
263
|
+
// polaris enforces the call's deadline through its signal. Set
|
|
264
|
+
// explicitly so the SDK does not refuse a long non-streamed request
|
|
265
|
+
// on a timeout of its own.
|
|
266
|
+
timeout: MAX_TIMEOUT,
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
#params(
|
|
271
|
+
request: ChatRequest,
|
|
272
|
+
streaming: boolean,
|
|
273
|
+
strictSchema: Loaded["strictSchema"],
|
|
274
|
+
): Params {
|
|
275
|
+
const config = this.#config;
|
|
276
|
+
const extra = providerOptions(request);
|
|
277
|
+
const server = serverParams(config.server, request);
|
|
278
|
+
const betas = [
|
|
279
|
+
...new Set([
|
|
280
|
+
...(config.betas ?? []),
|
|
281
|
+
...(config.fallbacks !== undefined ? [FALLBACK_BETA] : []),
|
|
282
|
+
...server.betas,
|
|
283
|
+
]),
|
|
284
|
+
];
|
|
285
|
+
const outputConfig: Anthropic.Beta.BetaOutputConfig = {
|
|
286
|
+
...(request.effort !== undefined ? { effort: request.effort } : {}),
|
|
287
|
+
...(request.output !== undefined
|
|
288
|
+
? {
|
|
289
|
+
format: {
|
|
290
|
+
type: "json_schema",
|
|
291
|
+
schema: outputSchema(strictSchema, request.output.schema),
|
|
292
|
+
},
|
|
293
|
+
}
|
|
294
|
+
: {}),
|
|
295
|
+
};
|
|
296
|
+
const tools: Anthropic.Beta.BetaToolUnion[] = [
|
|
297
|
+
...(request.tools ?? []).map((tool) => ({
|
|
298
|
+
name: tool.name,
|
|
299
|
+
description: tool.description,
|
|
300
|
+
input_schema: inputSchema(tool.name, tool.inputSchema),
|
|
301
|
+
})),
|
|
302
|
+
...server.tools,
|
|
303
|
+
];
|
|
304
|
+
|
|
305
|
+
return {
|
|
306
|
+
// Before the raw fields, so `providerOptions.anthropic` can refine it
|
|
307
|
+
// (a 1-hour `ttl`).
|
|
308
|
+
...(request.cache === true
|
|
309
|
+
? { cache_control: { type: "ephemeral" as const } }
|
|
310
|
+
: {}),
|
|
311
|
+
...extra,
|
|
312
|
+
model: request.model ?? config.model,
|
|
313
|
+
max_tokens:
|
|
314
|
+
request.maxTokens ??
|
|
315
|
+
config.maxTokens ??
|
|
316
|
+
(streaming ? DEFAULT_STREAM_MAX_TOKENS : DEFAULT_MAX_TOKENS),
|
|
317
|
+
messages: toMessageParams(request.messages),
|
|
318
|
+
...(request.system !== undefined ? { system: request.system } : {}),
|
|
319
|
+
...(tools.length > 0 ? { tools } : {}),
|
|
320
|
+
...server.fields,
|
|
321
|
+
...(request.toolChoice !== undefined
|
|
322
|
+
? { tool_choice: { type: request.toolChoice } }
|
|
323
|
+
: {}),
|
|
324
|
+
...(request.stopSequences !== undefined
|
|
325
|
+
? { stop_sequences: [...request.stopSequences] }
|
|
326
|
+
: {}),
|
|
327
|
+
...(Object.keys(outputConfig).length > 0
|
|
328
|
+
? { output_config: outputConfig }
|
|
329
|
+
: {}),
|
|
330
|
+
...(config.fallbacks !== undefined
|
|
331
|
+
? {
|
|
332
|
+
fallbacks:
|
|
333
|
+
config.fallbacks === "default"
|
|
334
|
+
? "default"
|
|
335
|
+
: config.fallbacks.map((fallback) => ({
|
|
336
|
+
model: fallback.model,
|
|
337
|
+
})),
|
|
338
|
+
}
|
|
339
|
+
: {}),
|
|
340
|
+
...(betas.length > 0 ? { betas } : {}),
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/** What a request's server-side tools add to it. */
|
|
346
|
+
interface ServerParams {
|
|
347
|
+
readonly tools: Anthropic.Beta.BetaToolUnion[];
|
|
348
|
+
readonly fields: Pick<Params, "container" | "mcp_servers">;
|
|
349
|
+
readonly betas: readonly string[];
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* The request fields for the server-side tools: the config's, each one the
|
|
354
|
+
* request sets replacing it. Skills run in the code execution container, so
|
|
355
|
+
* they bring its tool; each MCP server is named by one `mcp_toolset`.
|
|
356
|
+
*/
|
|
357
|
+
function serverParams(
|
|
358
|
+
defaults: AnthropicConfig["server"],
|
|
359
|
+
request: ChatRequest,
|
|
360
|
+
): ServerParams {
|
|
361
|
+
const asked = request.server;
|
|
362
|
+
const skills = asked?.skills ?? defaults?.skills ?? [];
|
|
363
|
+
const mcpServers = asked?.mcpServers ?? defaults?.mcpServers ?? [];
|
|
364
|
+
const codeExecution =
|
|
365
|
+
skills.length > 0 ||
|
|
366
|
+
(asked?.codeExecution ?? defaults?.codeExecution) === true;
|
|
367
|
+
const container = asked?.container;
|
|
368
|
+
|
|
369
|
+
if (skills.length > MAX_SKILLS) {
|
|
370
|
+
throw new E_POLARIS_INVALID_REQUEST([
|
|
371
|
+
`Claude loads at most ${MAX_SKILLS} skills per request, not ${skills.length}`,
|
|
372
|
+
]);
|
|
373
|
+
}
|
|
374
|
+
if (
|
|
375
|
+
codeExecution &&
|
|
376
|
+
(request.tools ?? []).some((tool) => tool.name === CODE_EXECUTION.name)
|
|
377
|
+
) {
|
|
378
|
+
throw new E_POLARIS_INVALID_REQUEST([
|
|
379
|
+
`a tool is named '${CODE_EXECUTION.name}', the name of Claude's code execution tool`,
|
|
380
|
+
]);
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
const tools: Anthropic.Beta.BetaToolUnion[] = [
|
|
384
|
+
...(codeExecution ? [CODE_EXECUTION] : []),
|
|
385
|
+
...mcpServers.map(
|
|
386
|
+
(mcp): Anthropic.Beta.BetaMCPToolset => ({
|
|
387
|
+
type: "mcp_toolset",
|
|
388
|
+
mcp_server_name: mcp.name,
|
|
389
|
+
// Only the tools named: the others are turned off.
|
|
390
|
+
...(mcp.tools !== undefined
|
|
391
|
+
? {
|
|
392
|
+
default_config: { enabled: false },
|
|
393
|
+
configs: Object.fromEntries(
|
|
394
|
+
mcp.tools.map((tool) => [tool, { enabled: true }]),
|
|
395
|
+
),
|
|
396
|
+
}
|
|
397
|
+
: {}),
|
|
398
|
+
}),
|
|
399
|
+
),
|
|
400
|
+
];
|
|
401
|
+
const fields: ServerParams["fields"] = {
|
|
402
|
+
...(skills.length > 0
|
|
403
|
+
? {
|
|
404
|
+
container: {
|
|
405
|
+
...(container !== undefined ? { id: container } : {}),
|
|
406
|
+
skills: skills.map((skill) => ({
|
|
407
|
+
type: skill.source,
|
|
408
|
+
skill_id: skill.id,
|
|
409
|
+
version: skill.version ?? "latest",
|
|
410
|
+
})),
|
|
411
|
+
},
|
|
412
|
+
}
|
|
413
|
+
: container !== undefined
|
|
414
|
+
? { container }
|
|
415
|
+
: {}),
|
|
416
|
+
...(mcpServers.length > 0
|
|
417
|
+
? {
|
|
418
|
+
mcp_servers: mcpServers.map((mcp) => ({
|
|
419
|
+
type: "url" as const,
|
|
420
|
+
name: mcp.name,
|
|
421
|
+
url: mcp.url,
|
|
422
|
+
...(mcp.authorizationToken !== undefined
|
|
423
|
+
? { authorization_token: mcp.authorizationToken }
|
|
424
|
+
: {}),
|
|
425
|
+
})),
|
|
426
|
+
}
|
|
427
|
+
: {}),
|
|
428
|
+
};
|
|
429
|
+
return {
|
|
430
|
+
tools,
|
|
431
|
+
fields,
|
|
432
|
+
betas: mcpServers.length > 0 ? [MCP_BETA] : [],
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/** A structured output's schema as Claude takes it; a schema it cannot take is the request's fault. */
|
|
437
|
+
function outputSchema(
|
|
438
|
+
strictSchema: Loaded["strictSchema"],
|
|
439
|
+
schema: JsonSchema,
|
|
440
|
+
): Record<string, unknown> {
|
|
441
|
+
try {
|
|
442
|
+
return strictSchema({ ...schema });
|
|
443
|
+
} catch (cause) {
|
|
444
|
+
throw new E_POLARIS_INVALID_REQUEST(
|
|
445
|
+
[
|
|
446
|
+
`the output schema cannot be sent to Claude: ${cause instanceof Error ? cause.message : String(cause)}`,
|
|
447
|
+
],
|
|
448
|
+
{ cause },
|
|
449
|
+
);
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/** `providerOptions.anthropic`: raw request fields, sent as they are. */
|
|
454
|
+
function providerOptions(request: ChatRequest): Record<string, unknown> {
|
|
455
|
+
const value: unknown = request.providerOptions?.[PROVIDER];
|
|
456
|
+
if (value === undefined) return {};
|
|
457
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
458
|
+
throw new E_POLARIS_INVALID_REQUEST([
|
|
459
|
+
"providerOptions.anthropic must be an object of request fields",
|
|
460
|
+
]);
|
|
461
|
+
}
|
|
462
|
+
return { ...value };
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
function inputSchema(
|
|
466
|
+
name: string,
|
|
467
|
+
schema: JsonSchema,
|
|
468
|
+
): Anthropic.Beta.BetaTool.InputSchema {
|
|
469
|
+
if (schema.type !== undefined && schema.type !== "object") {
|
|
470
|
+
throw new E_POLARIS_INVALID_REQUEST([
|
|
471
|
+
`the input schema of tool '${name}' must describe an object`,
|
|
472
|
+
]);
|
|
473
|
+
}
|
|
474
|
+
return { ...schema, type: "object" };
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
function toMessageParams(messages: readonly Message[]): MessageParam[] {
|
|
478
|
+
const params: MessageParam[] = [];
|
|
479
|
+
for (const message of messages) {
|
|
480
|
+
if (message.role === "user") {
|
|
481
|
+
params.push({
|
|
482
|
+
role: "user",
|
|
483
|
+
content:
|
|
484
|
+
typeof message.content === "string"
|
|
485
|
+
? message.content
|
|
486
|
+
: message.content.map((part): BlockParam => {
|
|
487
|
+
if (part.type === "text")
|
|
488
|
+
return { type: "text", text: part.text };
|
|
489
|
+
if (part.type === "image") {
|
|
490
|
+
return {
|
|
491
|
+
type: "image",
|
|
492
|
+
source: {
|
|
493
|
+
type: "base64",
|
|
494
|
+
media_type: part.mediaType,
|
|
495
|
+
data: part.data,
|
|
496
|
+
},
|
|
497
|
+
};
|
|
498
|
+
}
|
|
499
|
+
return {
|
|
500
|
+
type: "tool_result",
|
|
501
|
+
tool_use_id: part.toolCallId,
|
|
502
|
+
content: part.content,
|
|
503
|
+
...(part.isError ? { is_error: true } : {}),
|
|
504
|
+
};
|
|
505
|
+
}),
|
|
506
|
+
});
|
|
507
|
+
continue;
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
if (typeof message.content === "string") {
|
|
511
|
+
params.push({ role: "assistant", content: message.content });
|
|
512
|
+
continue;
|
|
513
|
+
}
|
|
514
|
+
const content = message.content.flatMap(toAssistantBlock);
|
|
515
|
+
// A turn left empty (only another provider's blocks) is dropped rather
|
|
516
|
+
// than sent empty, which the API refuses.
|
|
517
|
+
if (content.length > 0) params.push({ role: "assistant", content });
|
|
518
|
+
}
|
|
519
|
+
return params;
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
/** An assistant part as Claude takes it back: its own block when it has one. */
|
|
523
|
+
function toAssistantBlock(part: AssistantPart): BlockParam[] {
|
|
524
|
+
const own = ownBlock(part.origin);
|
|
525
|
+
if (own !== undefined) return [own];
|
|
526
|
+
switch (part.type) {
|
|
527
|
+
case "text":
|
|
528
|
+
return [{ type: "text", text: part.text }];
|
|
529
|
+
case "tool_call":
|
|
530
|
+
return [
|
|
531
|
+
{ type: "tool_use", id: part.id, name: part.name, input: part.input },
|
|
532
|
+
];
|
|
533
|
+
// Reasoning without Claude's signature, and another provider's blocks,
|
|
534
|
+
// cannot be sent: Claude would reject them.
|
|
535
|
+
case "reasoning":
|
|
536
|
+
case "provider":
|
|
537
|
+
return [];
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
/**
|
|
542
|
+
* The block an origin holds, when it is one this driver produced. It came
|
|
543
|
+
* from a Claude response, which the API takes back as a request block as it
|
|
544
|
+
* is; only its shape can be checked here.
|
|
545
|
+
*/
|
|
546
|
+
function ownBlock(origin: Origin | undefined): BlockParam | undefined {
|
|
547
|
+
if (origin === undefined || origin.provider !== PROVIDER) return undefined;
|
|
548
|
+
return isBlockParam(origin.block) ? origin.block : undefined;
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
function isBlockParam(value: unknown): value is BlockParam {
|
|
552
|
+
return (
|
|
553
|
+
typeof value === "object" &&
|
|
554
|
+
value !== null &&
|
|
555
|
+
typeof Reflect.get(value, "type") === "string"
|
|
556
|
+
);
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
function toResponse(message: Anthropic.Beta.BetaMessage): ChatResponse {
|
|
560
|
+
return createResponse({
|
|
561
|
+
id: message.id,
|
|
562
|
+
provider: PROVIDER,
|
|
563
|
+
model: message.model,
|
|
564
|
+
content: message.content.map(toPart),
|
|
565
|
+
stopReason: toStopReason(message.stop_reason),
|
|
566
|
+
// As reported: `createResponse` checks the counts. A gateway speaking the
|
|
567
|
+
// API may leave usage out, whatever the SDK's types say.
|
|
568
|
+
usage:
|
|
569
|
+
typeof message.usage === "object" && message.usage !== null
|
|
570
|
+
? {
|
|
571
|
+
inputTokens: message.usage.input_tokens,
|
|
572
|
+
outputTokens: message.usage.output_tokens,
|
|
573
|
+
cacheReadTokens: message.usage.cache_read_input_tokens ?? 0,
|
|
574
|
+
cacheWriteTokens: message.usage.cache_creation_input_tokens ?? 0,
|
|
575
|
+
serverToolUse: serverToolUse(message.usage.server_tool_use),
|
|
576
|
+
}
|
|
577
|
+
: undefined,
|
|
578
|
+
...readContainer(message.container),
|
|
579
|
+
});
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
/**
|
|
583
|
+
* The server tool counters, renamed: `code_execution_requests` becomes
|
|
584
|
+
* `codeExecutionRequests`. Read whole, the SDK types only some of them.
|
|
585
|
+
*/
|
|
586
|
+
function serverToolUse(reported: unknown): Record<string, unknown> | undefined {
|
|
587
|
+
if (typeof reported !== "object" || reported === null) return undefined;
|
|
588
|
+
return Object.fromEntries(
|
|
589
|
+
Object.entries(reported).map(([counter, count]) => [
|
|
590
|
+
counter.replace(/_([a-z0-9])/g, (_, letter: string) =>
|
|
591
|
+
letter.toUpperCase(),
|
|
592
|
+
),
|
|
593
|
+
count,
|
|
594
|
+
]),
|
|
595
|
+
);
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
/** The reply's container, when it ran code in one. */
|
|
599
|
+
function readContainer(
|
|
600
|
+
container: unknown,
|
|
601
|
+
): { container: { id: string; expiresAt?: string } } | Record<string, never> {
|
|
602
|
+
if (typeof container !== "object" || container === null) return {};
|
|
603
|
+
const id: unknown = Reflect.get(container, "id");
|
|
604
|
+
if (typeof id !== "string" || id === "") return {};
|
|
605
|
+
const expiresAt: unknown = Reflect.get(container, "expires_at");
|
|
606
|
+
return {
|
|
607
|
+
container: {
|
|
608
|
+
id,
|
|
609
|
+
...(typeof expiresAt === "string" ? { expiresAt } : {}),
|
|
610
|
+
},
|
|
611
|
+
};
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
function toPart(block: Block): AssistantPart {
|
|
615
|
+
const origin: Origin = { provider: PROVIDER, block };
|
|
616
|
+
switch (block.type) {
|
|
617
|
+
case "text":
|
|
618
|
+
return { type: "text", text: block.text, origin };
|
|
619
|
+
case "thinking":
|
|
620
|
+
return { type: "reasoning", text: block.thinking, origin };
|
|
621
|
+
case "redacted_thinking":
|
|
622
|
+
return { type: "reasoning", text: "", origin };
|
|
623
|
+
case "tool_use":
|
|
624
|
+
return toToolCall(block);
|
|
625
|
+
default:
|
|
626
|
+
return { type: "provider", origin };
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
function toToolCall(block: Anthropic.Beta.BetaToolUseBlock): ToolCallPart {
|
|
631
|
+
return {
|
|
632
|
+
type: "tool_call",
|
|
633
|
+
id: block.id,
|
|
634
|
+
name: block.name,
|
|
635
|
+
input: block.input,
|
|
636
|
+
origin: { provider: PROVIDER, block },
|
|
637
|
+
};
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
function toStopReason(
|
|
641
|
+
reason: Anthropic.Beta.BetaStopReason | null,
|
|
642
|
+
): StopReason {
|
|
643
|
+
switch (reason) {
|
|
644
|
+
case "end_turn":
|
|
645
|
+
return "end";
|
|
646
|
+
case "max_tokens":
|
|
647
|
+
case "model_context_window_exceeded":
|
|
648
|
+
return "max_tokens";
|
|
649
|
+
case "stop_sequence":
|
|
650
|
+
return "stop_sequence";
|
|
651
|
+
case "tool_use":
|
|
652
|
+
return "tool_use";
|
|
653
|
+
case "pause_turn":
|
|
654
|
+
case "compaction":
|
|
655
|
+
return "pause";
|
|
656
|
+
case "refusal":
|
|
657
|
+
return "refusal";
|
|
658
|
+
default:
|
|
659
|
+
return "other";
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
/** The polaris error for what the SDK raised. */
|
|
664
|
+
function toPolarisError(sdk: Sdk, error: unknown): unknown {
|
|
665
|
+
// polaris's own bounds, which the SDK wraps when they fire inside `fetch`:
|
|
666
|
+
// the response size, and a redirect refused.
|
|
667
|
+
const bound =
|
|
668
|
+
findCause(error, E_POLARIS_RESPONSE_TOO_LARGE) ??
|
|
669
|
+
findCause(error, E_POLARIS_PROVIDER_REJECTED);
|
|
670
|
+
if (bound !== undefined) return bound;
|
|
671
|
+
// A cancelled call: polaris reports it from the signal.
|
|
672
|
+
if (error instanceof sdk.APIUserAbortError) return error;
|
|
673
|
+
|
|
674
|
+
if (error instanceof sdk.APIConnectionError) {
|
|
675
|
+
return new E_POLARIS_PROVIDER_UNREACHABLE([PROVIDER, error.message], {
|
|
676
|
+
cause: error,
|
|
677
|
+
});
|
|
678
|
+
}
|
|
679
|
+
if (error instanceof sdk.APIError) {
|
|
680
|
+
const detail =
|
|
681
|
+
error.requestID != null
|
|
682
|
+
? `${error.message} (request ${error.requestID})`
|
|
683
|
+
: error.message;
|
|
684
|
+
return errorForStatus(PROVIDER, error.status ?? 0, detail, error);
|
|
685
|
+
}
|
|
686
|
+
return error;
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
function findCause<T>(
|
|
690
|
+
error: unknown,
|
|
691
|
+
type: abstract new (...args: never[]) => T,
|
|
692
|
+
): T | undefined {
|
|
693
|
+
let current: unknown = error;
|
|
694
|
+
for (let depth = 0; depth < 5 && current !== undefined; depth++) {
|
|
695
|
+
if (current instanceof type) return current;
|
|
696
|
+
current = current instanceof Error ? current.cause : undefined;
|
|
697
|
+
}
|
|
698
|
+
return undefined;
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
function isCount(value: number): boolean {
|
|
702
|
+
return Number.isInteger(value) && value > 0;
|
|
703
|
+
}
|