@node-agent/provider 0.0.0-stage → 0.1.5
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 +167 -2
- package/dist/i18n/en.json +105 -0
- package/dist/i18n/zh-CN.json +105 -0
- package/dist/icon.svg +6 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +364 -0
- package/dist/index.js.map +1 -0
- package/dist/mode.d.ts +26 -0
- package/dist/mode.js +39 -0
- package/dist/mode.js.map +1 -0
- package/dist/persistency/history.d.ts +11 -0
- package/dist/persistency/history.js +50 -0
- package/dist/persistency/history.js.map +1 -0
- package/dist/persistency/index.d.ts +23 -0
- package/dist/persistency/index.js +33 -0
- package/dist/persistency/index.js.map +1 -0
- package/dist/persistency/local.d.ts +20 -0
- package/dist/persistency/local.js +50 -0
- package/dist/persistency/local.js.map +1 -0
- package/dist/providers/anthropic.d.ts +28 -0
- package/dist/providers/anthropic.js +538 -0
- package/dist/providers/anthropic.js.map +1 -0
- package/dist/providers/base.d.ts +262 -0
- package/dist/providers/base.js +871 -0
- package/dist/providers/base.js.map +1 -0
- package/dist/providers/deepseek.d.ts +18 -0
- package/dist/providers/deepseek.js +73 -0
- package/dist/providers/deepseek.js.map +1 -0
- package/dist/providers/index.d.ts +13 -0
- package/dist/providers/index.js +30 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/providers/responses.d.ts +28 -0
- package/dist/providers/responses.js +310 -0
- package/dist/providers/responses.js.map +1 -0
- package/dist/serve.d.ts +2 -0
- package/dist/serve.js +28 -0
- package/dist/serve.js.map +1 -0
- package/dist/ui.html +324 -0
- package/package.json +190 -4
- package/src/icon.svg +6 -0
- package/src/ui.html +324 -0
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import OpenAI from 'openai';
|
|
2
|
+
export type JsonPrimitive = null | boolean | number | string;
|
|
3
|
+
export type JsonValue = JsonPrimitive | JsonValue[] | {
|
|
4
|
+
[key: string]: JsonValue;
|
|
5
|
+
};
|
|
6
|
+
export type JsonObject = {
|
|
7
|
+
[key: string]: unknown;
|
|
8
|
+
};
|
|
9
|
+
export type Fetcher = typeof fetch;
|
|
10
|
+
/** How many attempts one request gets, the first one included; `NODE_AGENT_MODEL_ATTEMPTS` (8).
|
|
11
|
+
* 1 means "never retry". A failed request is a pause, not the end. */
|
|
12
|
+
export declare const RETRY_ATTEMPTS: number;
|
|
13
|
+
/** The wait before the second attempt, in seconds; `NODE_AGENT_MODEL_BACKOFF` (1). */
|
|
14
|
+
export declare const RETRY_BACKOFF: number;
|
|
15
|
+
/** The longest one wait may grow to, in seconds; `NODE_AGENT_MODEL_MAX_DELAY` (60). */
|
|
16
|
+
export declare const RETRY_MAX_DELAY: number;
|
|
17
|
+
/** Statuses this deployment asked *not* to retry: `NODE_AGENT_MODEL_NO_RETRY_STATUSES=401,403`,
|
|
18
|
+
* `4xx`, or `400-499`.
|
|
19
|
+
*
|
|
20
|
+
* The policy is "an HTTP failure is a retry", 400 and 404 included, because a gateway in front of a
|
|
21
|
+
* model answers all of those for reasons a second attempt outlives. A status is skipped only when
|
|
22
|
+
* somebody writes it here, in the open — never because the base class quietly decided a retry is
|
|
23
|
+
* pointless. A 429 or an upstream limit body is retried even so: that is a wait, not a verdict. */
|
|
24
|
+
export declare const RETRY_EXCLUDED_STATUSES: Set<number>;
|
|
25
|
+
/** Everything the retry policy reads, in one object: a test replaces a field instead of sleeping. */
|
|
26
|
+
export type RetryPolicy = {
|
|
27
|
+
/** Total attempts, the first one included. */
|
|
28
|
+
attempts: number;
|
|
29
|
+
/** The wait before the second attempt, in seconds; every later one doubles it. */
|
|
30
|
+
backoffSeconds: number;
|
|
31
|
+
/** A ceiling on one wait, in seconds. A `Retry-After` from the endpoint overrides it. */
|
|
32
|
+
maxDelaySeconds: number;
|
|
33
|
+
/** Statuses this deployment asked not to retry (see `RETRY_EXCLUDED_STATUSES`). */
|
|
34
|
+
excludedStatuses: Set<number>;
|
|
35
|
+
/** The jitter source: 0 gives the plain doubled wait, 1 the longest one. */
|
|
36
|
+
random: () => number;
|
|
37
|
+
};
|
|
38
|
+
/** The one policy the three protocols run under; the base class owns it, a provider does not. */
|
|
39
|
+
export declare const retryPolicy: RetryPolicy;
|
|
40
|
+
/** The wait before attempt `attempt + 1`, in seconds: the base doubled per attempt, jittered, capped.
|
|
41
|
+
*
|
|
42
|
+
* Half of the wait is fixed and half random, so callers that failed together do not all come back
|
|
43
|
+
* at the same instant, while the wait still grows with every attempt. */
|
|
44
|
+
export declare function retryDelaySeconds(attempt: number, policy?: RetryPolicy): number;
|
|
45
|
+
/** What an OpenAI row posts to when the page leaves `base_url` blank. */
|
|
46
|
+
export declare const OPENAI_BASE_URL = "https://api.openai.com/v1";
|
|
47
|
+
/** The pause between two attempts. A test replaces it, because the wait is not what it checks; a
|
|
48
|
+
* cancel closes it early, because a caller that stopped waiting deserves its error back at once. */
|
|
49
|
+
export declare const retryPause: {
|
|
50
|
+
for(seconds: number, signal?: AbortSignal): Promise<void>;
|
|
51
|
+
};
|
|
52
|
+
/** One model request's HTTP failure, carrying the status that decides whether a retry can help. */
|
|
53
|
+
export declare class HttpError extends Error {
|
|
54
|
+
readonly status: number;
|
|
55
|
+
readonly retryAfterMs: number | null;
|
|
56
|
+
constructor(status: number, message: string, retryAfterMs?: number | null);
|
|
57
|
+
}
|
|
58
|
+
/** What kind of failure a caller is looking at after the retries are spent. */
|
|
59
|
+
export type FailureKind = 'connection' | 'rate_limit' | 'server' | 'http' | 'aborted' | 'unknown';
|
|
60
|
+
/** Did somebody cancel this request? Whichever layer wrapped it, the abort is in the chain. */
|
|
61
|
+
export declare function isAbortFailure(error: unknown): boolean;
|
|
62
|
+
/** The HTTP status an SDK or a fetch error carries, if it has one. */
|
|
63
|
+
export declare function statusOf(error: unknown): number | null;
|
|
64
|
+
/** The first machine-readable code in the chain (`ECONNRESET`, `rate_limit_exceeded`, …). */
|
|
65
|
+
export declare function codeOf(error: unknown): string;
|
|
66
|
+
/** Parse `Retry-After` (seconds or HTTP date) into milliseconds. */
|
|
67
|
+
export declare function retryAfterMsFromHeader(raw: string | null): number | null;
|
|
68
|
+
/** Parse `Retry-After` off an error or anything in its cause chain. */
|
|
69
|
+
export declare function retryAfterMsOf(error: unknown): number | null;
|
|
70
|
+
/** Did the endpoint answer "you have no slot right now"?
|
|
71
|
+
*
|
|
72
|
+
* A spent concurrency slot, a rate limiter, an overloaded gateway: the status is the polite part and
|
|
73
|
+
* the body is the truth. A 429 says it outright, and a 400, 403 or 503 carrying the words means the
|
|
74
|
+
* same thing — the same request works once a slot frees up. */
|
|
75
|
+
export declare function isUpstreamLimitFailure(error: unknown): boolean;
|
|
76
|
+
/** The name the callers, the logs and the kinds already use: a concurrency answer is a rate limit. */
|
|
77
|
+
export declare function isRateLimitFailure(error: unknown): boolean;
|
|
78
|
+
/** Did the endpoint answer, but with a server-side failure? */
|
|
79
|
+
export declare function isServerFailure(error: unknown): boolean;
|
|
80
|
+
/** Did the request fail to reach the endpoint at all? */
|
|
81
|
+
export declare function isConnectionFailure(error: unknown): boolean;
|
|
82
|
+
/** The broad class of the final failure, so callers and logs can tell a dead socket from a 429. */
|
|
83
|
+
export declare function failureKind(error: unknown): FailureKind;
|
|
84
|
+
/** Is this failure one a second attempt can fix?
|
|
85
|
+
*
|
|
86
|
+
* Every HTTP failure is: a status that is not 2xx reached this function because an attempt failed,
|
|
87
|
+
* and a gateway, a proxy or a queue in front of the model is free to answer 400, 401, 403, 404 or
|
|
88
|
+
* 409 for a reason that goes away by itself. So the answer is yes for all of them, and a caller
|
|
89
|
+
* that knows its endpoint better says otherwise through `excludedStatuses`
|
|
90
|
+
* (`NODE_AGENT_MODEL_NO_RETRY_STATUSES`) — in the open, never by the base class quietly deciding
|
|
91
|
+
* that a retry is pointless. Two things are never retried: a request somebody cancelled, and a
|
|
92
|
+
* failure with no status and no dead socket behind it (a malformed answer, which a retry would
|
|
93
|
+
* reproduce). A wait the endpoint asked for — a 429 or an upstream concurrency body — is always
|
|
94
|
+
* retried, whatever status carried it and whatever was excluded. */
|
|
95
|
+
export declare function retryable(error: unknown, policy?: RetryPolicy): boolean;
|
|
96
|
+
/** The SDK says `Connection error.` and hides the reason in `cause`; show the whole chain, with every
|
|
97
|
+
* type and machine-readable field, instead of one bare sentence. */
|
|
98
|
+
export declare function failureReason(error: unknown): string;
|
|
99
|
+
/** One endpoint as a failure message names it: the row, and the host that did not answer. */
|
|
100
|
+
export type Endpoint = {
|
|
101
|
+
name: string;
|
|
102
|
+
host: string;
|
|
103
|
+
};
|
|
104
|
+
/** The host inside a base URL, or the wire's own when the row names none. */
|
|
105
|
+
export declare function endpointHost(baseUrl: string | null, fallback: string): string;
|
|
106
|
+
/** What a caller reads when the attempts are spent: provider, host, attempts, final kind, and the
|
|
107
|
+
* complete cause chain (type, message, code, status, Retry-After). */
|
|
108
|
+
export declare function failureMessage(endpoint: Endpoint, attempts: number, cause: unknown, streamStarted?: boolean): string;
|
|
109
|
+
/** A model request that failed after the provider's retry policy was spent. It keeps the original
|
|
110
|
+
* error as `cause` and exposes the fields a caller needs without parsing the sentence. */
|
|
111
|
+
export declare class ProviderCallError extends Error {
|
|
112
|
+
readonly provider: string;
|
|
113
|
+
readonly host: string;
|
|
114
|
+
readonly attempts: number;
|
|
115
|
+
readonly kind: FailureKind;
|
|
116
|
+
readonly status: number | null;
|
|
117
|
+
readonly code: string;
|
|
118
|
+
readonly causeType: string;
|
|
119
|
+
readonly streamStarted: boolean;
|
|
120
|
+
constructor(endpoint: Endpoint, attempts: number, cause: unknown, streamStarted?: boolean);
|
|
121
|
+
}
|
|
122
|
+
/** Kept as the connection-specific name so callers that already distinguish an unreachable host
|
|
123
|
+
* keep working; it now carries the same full detail as every other ProviderCallError. */
|
|
124
|
+
export declare class UnreachableError extends ProviderCallError {
|
|
125
|
+
constructor(endpoint: Endpoint, attempts: number, cause: unknown, streamStarted?: boolean);
|
|
126
|
+
}
|
|
127
|
+
/** The error to raise once the attempts are spent. Every failure is wrapped: a 429, a 500 and a
|
|
128
|
+
* malformed answer all deserve the provider, host, attempt count and original type. */
|
|
129
|
+
export declare function providerFailure(error: unknown, endpoint: Endpoint, attempts: number, streamStarted?: boolean): ProviderCallError;
|
|
130
|
+
/** Wait out the pause before the next attempt: the endpoint's `Retry-After` when it named one, the
|
|
131
|
+
* policy's doubled and jittered backoff otherwise. A cancel ends the wait instead of sleeping on. */
|
|
132
|
+
export declare function waitBeforeRetrying(attempt: number, error: unknown, policy?: RetryPolicy, signal?: AbortSignal): Promise<void>;
|
|
133
|
+
export type Usage = {
|
|
134
|
+
prompt_tokens: number;
|
|
135
|
+
completion_tokens: number;
|
|
136
|
+
total_tokens: number;
|
|
137
|
+
};
|
|
138
|
+
export type ToolCall = {
|
|
139
|
+
id?: string;
|
|
140
|
+
type?: 'function';
|
|
141
|
+
function?: {
|
|
142
|
+
name?: string;
|
|
143
|
+
arguments?: string;
|
|
144
|
+
};
|
|
145
|
+
index?: number;
|
|
146
|
+
};
|
|
147
|
+
export type Message = {
|
|
148
|
+
role?: 'system' | 'developer' | 'user' | 'assistant' | 'tool';
|
|
149
|
+
content?: unknown;
|
|
150
|
+
name?: string;
|
|
151
|
+
tool_call_id?: string;
|
|
152
|
+
tool_calls?: ToolCall[];
|
|
153
|
+
reasoning_content?: string;
|
|
154
|
+
provider_state?: JsonObject;
|
|
155
|
+
[key: string]: unknown;
|
|
156
|
+
};
|
|
157
|
+
export type ToolSpec = {
|
|
158
|
+
type: 'function';
|
|
159
|
+
function?: {
|
|
160
|
+
name?: string;
|
|
161
|
+
description?: string;
|
|
162
|
+
parameters?: JsonObject;
|
|
163
|
+
strict?: boolean;
|
|
164
|
+
};
|
|
165
|
+
};
|
|
166
|
+
export type ChatOutput = {
|
|
167
|
+
message: Message;
|
|
168
|
+
usage: Usage | null;
|
|
169
|
+
previous_response_id: string | null;
|
|
170
|
+
};
|
|
171
|
+
export type StreamOutput = {
|
|
172
|
+
delta: Message;
|
|
173
|
+
usage?: Usage | null;
|
|
174
|
+
previous_response_id?: string | null;
|
|
175
|
+
provider_state?: JsonObject;
|
|
176
|
+
};
|
|
177
|
+
/** One optional string field of a provider row. */
|
|
178
|
+
export declare function optionalString(data: JsonObject, key: string): string | null;
|
|
179
|
+
/** The row's optional thinking effort: an empty value means the endpoint's default. */
|
|
180
|
+
export declare function thinkingEffort(data: JsonObject): string | null;
|
|
181
|
+
export declare function timeoutOf(data: JsonObject, label: string): number | null;
|
|
182
|
+
export declare function missingProviderMessage(name: string, known: string[]): string;
|
|
183
|
+
export declare function blankProviderMessage(): string;
|
|
184
|
+
/** Resolve with the work, or reject after `ms` rather than waiting forever on a request. */
|
|
185
|
+
export declare function within<T>(work: Promise<T>, ms: number, what: string): Promise<T>;
|
|
186
|
+
/** JSON-compatible plain data, with null object fields dropped like model_dump(exclude_none). */
|
|
187
|
+
export declare function plain(value: unknown): unknown;
|
|
188
|
+
export declare function messageOf(value: unknown): Message;
|
|
189
|
+
export declare function usageFromOpenAI(value: unknown): Usage | null;
|
|
190
|
+
/** POST a JSON body and return its JSON answer, raising an HttpError for a non-2xx status. */
|
|
191
|
+
export declare function postJson(fetcher: Fetcher, url: string, headers: Record<string, string>, body: JsonObject, signal?: AbortSignal): Promise<JsonObject>;
|
|
192
|
+
/** Read a server-sent-event body and yield each JSON `data:` payload. */
|
|
193
|
+
export declare function sseEvents(response: Response): AsyncGenerator<JsonObject, void, unknown>;
|
|
194
|
+
/** What a retried request may be told: the policy to run under, and the caller's cancel. */
|
|
195
|
+
export type RetryOptions = {
|
|
196
|
+
policy?: RetryPolicy;
|
|
197
|
+
/** A request the caller aborted is never retried, and neither is the wait before the next try. */
|
|
198
|
+
signal?: AbortSignal;
|
|
199
|
+
};
|
|
200
|
+
/** Retry one request under the policy: the one loop every protocol goes through.
|
|
201
|
+
*
|
|
202
|
+
* This and `retryStream` are the whole retry story of the package. A provider says how to make one
|
|
203
|
+
* attempt and nothing else — which statuses are retried, how long to wait, when to give up and how
|
|
204
|
+
* the failure is wrapped are decided here, once. */
|
|
205
|
+
export declare function retryRequests<T>(work: () => Promise<T>, endpoint: Endpoint, options?: RetryOptions): Promise<T>;
|
|
206
|
+
/** Retry a stream only until its first chunk; a retry after that would repeat what was read. */
|
|
207
|
+
export declare function retryStream<T>(open: () => AsyncGenerator<T, void, unknown>, endpoint: Endpoint, options?: RetryOptions): AsyncGenerator<T, void, unknown>;
|
|
208
|
+
/** The base every protocol extends — the `BaseProvider` the retry policy is written for.
|
|
209
|
+
*
|
|
210
|
+
* A protocol implements `chat` and `chatStream` as one attempt each and hands the attempt over
|
|
211
|
+
* through `attempt` / `attemptStream`. The retry itself — which failures are retried, the wait (backoff,
|
|
212
|
+
* jitter, `Retry-After`), the attempt ceiling, the abort, the wrapping into `ProviderCallError` —
|
|
213
|
+
* lives here and only here, so the three wires cannot drift apart. */
|
|
214
|
+
export declare abstract class Provider {
|
|
215
|
+
/** The row this provider serves, set by whoever built it: a failure names the row, not the wire. */
|
|
216
|
+
name: string;
|
|
217
|
+
/** The retry policy this row runs under; the module's own unless a caller replaced it. */
|
|
218
|
+
policy: RetryPolicy;
|
|
219
|
+
/** The host this provider posts to. */
|
|
220
|
+
abstract readonly host: string;
|
|
221
|
+
protected endpoint(): Endpoint;
|
|
222
|
+
/** Run one attempt's request under the policy, and wrap the final failure once it is spent. */
|
|
223
|
+
protected attempt<T>(work: () => Promise<T>, signal?: AbortSignal): Promise<T>;
|
|
224
|
+
/** Run one attempt's stream under the policy: retried until the first chunk, never after it. */
|
|
225
|
+
protected attemptStream<T>(open: () => AsyncGenerator<T, void, unknown>, signal?: AbortSignal): AsyncGenerator<T, void, unknown>;
|
|
226
|
+
abstract dump(): JsonObject;
|
|
227
|
+
abstract chat(messages: Message[], tools?: ToolSpec[] | null, maxCompletionTokens?: number | null, previousResponseId?: string | null): Promise<ChatOutput>;
|
|
228
|
+
abstract chatStream(messages: Message[], tools?: ToolSpec[] | null, maxCompletionTokens?: number | null, previousResponseId?: string | null, signal?: AbortSignal): AsyncGenerator<StreamOutput, void, unknown>;
|
|
229
|
+
close(): Promise<void>;
|
|
230
|
+
}
|
|
231
|
+
/** The OpenAI chat-completions provider. */
|
|
232
|
+
export declare class OpenAIProvider extends Provider {
|
|
233
|
+
readonly apiKey: string | null;
|
|
234
|
+
readonly organization: string | null;
|
|
235
|
+
readonly modelName: string;
|
|
236
|
+
readonly baseUrl: string | null;
|
|
237
|
+
readonly timeout: number | null;
|
|
238
|
+
readonly effort: string | null;
|
|
239
|
+
readonly fetcher: Fetcher;
|
|
240
|
+
readonly client: OpenAI | null;
|
|
241
|
+
readonly host: string;
|
|
242
|
+
static build(data: JsonObject, fetcher: Fetcher): OpenAIProvider;
|
|
243
|
+
constructor(options: {
|
|
244
|
+
apiKey?: string | null;
|
|
245
|
+
organization?: string | null;
|
|
246
|
+
modelName?: string;
|
|
247
|
+
baseUrl?: string | null;
|
|
248
|
+
timeout?: number | null;
|
|
249
|
+
effort?: string | null;
|
|
250
|
+
fetcher: Fetcher;
|
|
251
|
+
});
|
|
252
|
+
dump(): JsonObject;
|
|
253
|
+
/** One transcript line as the endpoint reads it. Split out so a subclass can speak a wire whose
|
|
254
|
+
* house rules differ from the generic one (DeepSeek, for instance, insists on a field a plain
|
|
255
|
+
* chat-completions endpoint never heard of). */
|
|
256
|
+
protected wireMessage(message: Message): JsonObject;
|
|
257
|
+
protected body(messages: Message[], tools?: ToolSpec[] | null, limit?: number | null): JsonObject;
|
|
258
|
+
chat(messages: Message[], tools?: ToolSpec[] | null, maxCompletionTokens?: number | null, _previousResponseId?: string | null): Promise<ChatOutput>;
|
|
259
|
+
chatStream(messages: Message[], tools?: ToolSpec[] | null, maxCompletionTokens?: number | null, _previousResponseId?: string | null, signal?: AbortSignal): AsyncGenerator<StreamOutput, void, unknown>;
|
|
260
|
+
}
|
|
261
|
+
/** The same class under the name the retry policy talks about. */
|
|
262
|
+
export { Provider as BaseProvider };
|