@procedures/llm 0.4.0 → 0.6.0
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/CHANGELOG.md +36 -0
- package/README.md +114 -9
- package/dist/.tsbuildinfo +1 -1
- package/dist/anthropic.d.ts +64 -5
- package/dist/anthropic.d.ts.map +1 -1
- package/dist/anthropic.js +109 -50
- package/dist/anthropic.js.map +1 -1
- package/dist/errors.d.ts +15 -8
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +4 -1
- package/dist/errors.js.map +1 -1
- package/dist/events.d.ts +21 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/events.js +218 -0
- package/dist/events.js.map +1 -0
- package/dist/index.d.ts +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -1
- package/dist/index.js.map +1 -1
- package/dist/module.d.ts +8 -2
- package/dist/module.d.ts.map +1 -1
- package/dist/module.js +127 -12
- package/dist/module.js.map +1 -1
- package/dist/procedures/llm/complete.d.ts +9 -3
- package/dist/procedures/llm/complete.d.ts.map +1 -1
- package/dist/procedures/llm/complete.js +50 -10
- package/dist/procedures/llm/complete.js.map +1 -1
- package/dist/procedures/llm/index.d.ts +1 -0
- package/dist/procedures/llm/index.d.ts.map +1 -1
- package/dist/procedures/llm/index.js +1 -0
- package/dist/procedures/llm/index.js.map +1 -1
- package/dist/procedures/llm/stream.d.ts +14 -0
- package/dist/procedures/llm/stream.d.ts.map +1 -0
- package/dist/procedures/llm/stream.js +20 -0
- package/dist/procedures/llm/stream.js.map +1 -0
- package/dist/procedures/llm/structured.d.ts +1 -1
- package/dist/procedures/llm/structured.d.ts.map +1 -1
- package/dist/procedures/llm/structured.js +6 -4
- package/dist/procedures/llm/structured.js.map +1 -1
- package/dist/request.d.ts +15 -4
- package/dist/request.d.ts.map +1 -1
- package/dist/request.js +47 -2
- package/dist/request.js.map +1 -1
- package/dist/retry.d.ts +45 -0
- package/dist/retry.d.ts.map +1 -0
- package/dist/retry.js +106 -0
- package/dist/retry.js.map +1 -0
- package/dist/tools.d.ts +50 -0
- package/dist/tools.d.ts.map +1 -0
- package/dist/tools.js +162 -0
- package/dist/tools.js.map +1 -0
- package/dist/types.d.ts +138 -4
- package/dist/types.d.ts.map +1 -1
- package/package.json +9 -5
- package/src/anthropic.ts +149 -49
- package/src/errors.ts +17 -5
- package/src/events.ts +218 -0
- package/src/index.ts +5 -2
- package/src/module.ts +143 -12
- package/src/procedures/llm/complete.ts +53 -12
- package/src/procedures/llm/index.ts +1 -0
- package/src/procedures/llm/stream.ts +23 -0
- package/src/procedures/llm/structured.ts +6 -4
- package/src/request.ts +47 -4
- package/src/retry.ts +110 -0
- package/src/tools.ts +178 -0
- package/src/types.ts +148 -4
package/dist/anthropic.d.ts
CHANGED
|
@@ -2,13 +2,15 @@
|
|
|
2
2
|
* The Anthropic Messages API over `fetch` (roadmap R46).
|
|
3
3
|
*
|
|
4
4
|
* The procedures send each request with the `fetch` of the module settings, not through
|
|
5
|
-
* `http.request
|
|
5
|
+
* `http.request`. The API key goes in a header. The input of a nested call is visible to the
|
|
6
6
|
* interceptors and to the traces of the host. The key comes from an environment variable that the
|
|
7
7
|
* module names. An input cannot give a key.
|
|
8
8
|
*
|
|
9
|
-
* A
|
|
10
|
-
* the caller. A stop is an `InvocationError` with the code `TIMEOUT` or `ABORTED`.
|
|
9
|
+
* A request stops at the earliest of its time limit, the deadline of its capability and the abort
|
|
10
|
+
* of the caller. A stop is an `InvocationError` with the code `TIMEOUT` or `ABORTED`. A request
|
|
11
|
+
* that fails with a retryable error gets new tries (`retry.ts`).
|
|
11
12
|
*/
|
|
13
|
+
import type { Backoff } from "@mark1russell7/service/backoff";
|
|
12
14
|
import { type ProcedureContext, type ProcedurePath } from "@procedures/client";
|
|
13
15
|
import type { LlmEffort, LlmUsage } from "./types.js";
|
|
14
16
|
/** The variables that the module reads: a name and a value, or undefined for a variable that is not set. */
|
|
@@ -23,7 +25,7 @@ export declare const DEFAULT_API_KEY_ENV = "ANTHROPIC_API_KEY";
|
|
|
23
25
|
export declare const DEFAULT_BASE_URL = "https://api.anthropic.com";
|
|
24
26
|
/** The default of `maxTokens`. */
|
|
25
27
|
export declare const DEFAULT_MAX_TOKENS = 16000;
|
|
26
|
-
/** The default time limit of a
|
|
28
|
+
/** The default time limit of a request: 10 minutes. */
|
|
27
29
|
export declare const DEFAULT_TIMEOUT_MS = 600000;
|
|
28
30
|
/** The version header of the Messages API. */
|
|
29
31
|
export declare const ANTHROPIC_VERSION = "2023-06-01";
|
|
@@ -39,10 +41,14 @@ export interface LlmSettings {
|
|
|
39
41
|
readonly defaultModel: string;
|
|
40
42
|
/** The default of the `fallback` input. */
|
|
41
43
|
readonly fallback: boolean;
|
|
44
|
+
/** The default number of new tries after a retryable error. */
|
|
45
|
+
readonly retries: number;
|
|
46
|
+
/** The backoff before each new try, when the API does not give `retry-after`. */
|
|
47
|
+
readonly backoff: Backoff;
|
|
42
48
|
}
|
|
43
49
|
/** The settings of the default module. */
|
|
44
50
|
export declare const DEFAULT_LLM_SETTINGS: LlmSettings;
|
|
45
|
-
/** One content block of a message. The procedures read
|
|
51
|
+
/** One content block of a message. The procedures read the text blocks and the `tool_use` blocks. */
|
|
46
52
|
export interface ContentBlock {
|
|
47
53
|
readonly type: string;
|
|
48
54
|
readonly text?: string;
|
|
@@ -53,6 +59,19 @@ export interface MessageParam {
|
|
|
53
59
|
readonly role: "user" | "assistant";
|
|
54
60
|
readonly content: string | readonly ContentBlock[];
|
|
55
61
|
}
|
|
62
|
+
/** A tool definition as the API gets it. */
|
|
63
|
+
export interface ToolParam {
|
|
64
|
+
readonly name: string;
|
|
65
|
+
readonly description?: string;
|
|
66
|
+
readonly input_schema: Record<string, unknown>;
|
|
67
|
+
readonly strict?: boolean;
|
|
68
|
+
}
|
|
69
|
+
/** The `tool_choice` field as the API gets it. */
|
|
70
|
+
export interface ToolChoiceParam {
|
|
71
|
+
readonly type: "auto" | "any" | "tool" | "none";
|
|
72
|
+
readonly name?: string;
|
|
73
|
+
readonly disable_parallel_tool_use?: boolean;
|
|
74
|
+
}
|
|
56
75
|
/** The request body of the Messages API, as the procedures send it. */
|
|
57
76
|
export interface MessagesRequest {
|
|
58
77
|
model: string;
|
|
@@ -68,6 +87,9 @@ export interface MessagesRequest {
|
|
|
68
87
|
};
|
|
69
88
|
};
|
|
70
89
|
fallbacks?: "default";
|
|
90
|
+
tools?: ToolParam[];
|
|
91
|
+
tool_choice?: ToolChoiceParam;
|
|
92
|
+
stream?: true;
|
|
71
93
|
}
|
|
72
94
|
/** The parts of an answer that the procedures use. */
|
|
73
95
|
export interface MessagesResponse {
|
|
@@ -94,9 +116,46 @@ export declare function textOf(content: readonly ContentBlock[]): string;
|
|
|
94
116
|
export declare function addUsage(a: LlmUsage, b: LlmUsage): LlmUsage;
|
|
95
117
|
/** A usage of zero tokens. */
|
|
96
118
|
export declare const NO_USAGE: LlmUsage;
|
|
119
|
+
/**
|
|
120
|
+
* This function gives the usage of a `usage` object of the API. A field that the object does not
|
|
121
|
+
* have keeps its value of `base`: the usage of a `message_delta` event changes only some fields.
|
|
122
|
+
*/
|
|
123
|
+
export declare function usageOf(raw: unknown, base?: LlmUsage): LlmUsage;
|
|
124
|
+
/**
|
|
125
|
+
* The scope of one request: the key, the signal of the `fetch` and the stop. The signal aborts at
|
|
126
|
+
* the time limit, at the deadline and at the abort of the caller.
|
|
127
|
+
*/
|
|
128
|
+
export interface RequestScope {
|
|
129
|
+
readonly key: string;
|
|
130
|
+
readonly signal: AbortSignal;
|
|
131
|
+
/** The reason of a stop, or undefined while the request runs. */
|
|
132
|
+
readonly stopped: () => "timeout" | "caller" | undefined;
|
|
133
|
+
/** This function gives the error of a stop for an error of the request, or the error unchanged. */
|
|
134
|
+
readonly translate: (error: unknown) => unknown;
|
|
135
|
+
/** This function stops the timer and removes the listener of the abort. */
|
|
136
|
+
readonly close: () => void;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* This function opens the scope of one request. It throws `NO_API_KEY` without a key, `TIMEOUT`
|
|
140
|
+
* when the deadline passed and `ABORTED` when the caller aborted, before a request goes out.
|
|
141
|
+
*/
|
|
142
|
+
export declare function openScope(settings: LlmSettings, limits: CallLimits): RequestScope;
|
|
143
|
+
/** The message of an error, without the key. */
|
|
144
|
+
export declare function safeMessage(error: unknown, key: string): string;
|
|
145
|
+
/**
|
|
146
|
+
* This function sends a request to the Messages API in a scope, and gives the answer with a
|
|
147
|
+
* status from 200 to 299. It throws `CONNECTION` for a failed connection and `API_ERROR` for an
|
|
148
|
+
* error status.
|
|
149
|
+
*/
|
|
150
|
+
export declare function postMessages(request: MessagesRequest, settings: LlmSettings, scope: RequestScope): Promise<Response>;
|
|
97
151
|
/**
|
|
98
152
|
* This function sends one request to the Messages API and gives the checked answer. It throws a
|
|
99
153
|
* `REFUSAL` error when a safety classifier declined the request.
|
|
100
154
|
*/
|
|
101
155
|
export declare function sendMessages(request: MessagesRequest, settings: LlmSettings, limits: CallLimits): Promise<MessagesResponse>;
|
|
156
|
+
/**
|
|
157
|
+
* This function sends a request with `sendMessages`, and sends it again after a retryable
|
|
158
|
+
* error, at most `retries` more times.
|
|
159
|
+
*/
|
|
160
|
+
export declare function sendWithRetries(request: MessagesRequest, settings: LlmSettings, limits: CallLimits, retries: number): Promise<MessagesResponse>;
|
|
102
161
|
//# sourceMappingURL=anthropic.d.ts.map
|
package/dist/anthropic.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"anthropic.d.ts","sourceRoot":"","sources":["../src/anthropic.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"anthropic.d.ts","sourceRoot":"","sources":["../src/anthropic.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,gCAAgC,CAAC;AAC9D,OAAO,EAAmB,KAAK,gBAAgB,EAAE,KAAK,aAAa,EAAE,MAAM,oBAAoB,CA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AAQ,CAOzE;AA0DD;;;GAGG;AACH,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC;IAC7B,iEAAiE;IACjE,QAAQ,CAAC,OAAO,EAAE,MAAM,SAAS,GAAG,QAAQ,GAAG,SAAS,CAAC;IACzD,mGAAmG;IACnG,QAAQ,CAAC,SAAS,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,OAAO,CAAC;IAChD,2EAA2E;IAC3E,QAAQ,CAAC,KAAK,EAAE,MAAM,IAAI,CAAC;CAC5B;AAED;;;GAGG;AACH,wBAAgB,SAAS,CAAC,QAAQ,EAAE,WAAW,EAAE,MAAM,EAAE,UAAU,GAAG,YAAY,CAiCjF;AAED,gDAAgD;AAChD,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAG/D;AAED;;;;GAIG;AACH,wBAAsB,YAAY,CAAC,OAAO,EAAE,eAAe,EAAE,QAAQ,EAAE,WAAW,EAAE,KAAK,EAAE,YAAY,GAAG,OAAO,CAAC,QAAQ,CAAC,CAuB1H;AAED;;;GAGG;AACH,wBAAsB,YAAY,CAAC,OAAO,EAAE,eAAe,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,EAAE,UAAU,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAejI;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAAC,OAAO,EAAE,eAAe,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAE/I"}
|
package/dist/anthropic.js
CHANGED
|
@@ -2,15 +2,17 @@
|
|
|
2
2
|
* The Anthropic Messages API over `fetch` (roadmap R46).
|
|
3
3
|
*
|
|
4
4
|
* The procedures send each request with the `fetch` of the module settings, not through
|
|
5
|
-
* `http.request
|
|
5
|
+
* `http.request`. The API key goes in a header. The input of a nested call is visible to the
|
|
6
6
|
* interceptors and to the traces of the host. The key comes from an environment variable that the
|
|
7
7
|
* module names. An input cannot give a key.
|
|
8
8
|
*
|
|
9
|
-
* A
|
|
10
|
-
* the caller. A stop is an `InvocationError` with the code `TIMEOUT` or `ABORTED`.
|
|
9
|
+
* A request stops at the earliest of its time limit, the deadline of its capability and the abort
|
|
10
|
+
* of the caller. A stop is an `InvocationError` with the code `TIMEOUT` or `ABORTED`. A request
|
|
11
|
+
* that fails with a retryable error gets new tries (`retry.ts`).
|
|
11
12
|
*/
|
|
12
13
|
import { InvocationError } from "@procedures/client";
|
|
13
14
|
import { LlmError } from "./errors.js";
|
|
15
|
+
import { DEFAULT_BACKOFF, DEFAULT_REQUEST_RETRIES, deadlineLeft, retryAfterMs, withRetries } from "./retry.js";
|
|
14
16
|
/** The default model of the module. */
|
|
15
17
|
export const DEFAULT_MODEL = "claude-sonnet-5-5";
|
|
16
18
|
/** The default environment variable of the API key. */
|
|
@@ -19,7 +21,7 @@ export const DEFAULT_API_KEY_ENV = "ANTHROPIC_API_KEY";
|
|
|
19
21
|
export const DEFAULT_BASE_URL = "https://api.anthropic.com";
|
|
20
22
|
/** The default of `maxTokens`. */
|
|
21
23
|
export const DEFAULT_MAX_TOKENS = 16_000;
|
|
22
|
-
/** The default time limit of a
|
|
24
|
+
/** The default time limit of a request: 10 minutes. */
|
|
23
25
|
export const DEFAULT_TIMEOUT_MS = 600_000;
|
|
24
26
|
/** The version header of the Messages API. */
|
|
25
27
|
export const ANTHROPIC_VERSION = "2023-06-01";
|
|
@@ -35,6 +37,8 @@ export const DEFAULT_LLM_SETTINGS = Object.freeze({
|
|
|
35
37
|
baseUrl: DEFAULT_BASE_URL,
|
|
36
38
|
defaultModel: DEFAULT_MODEL,
|
|
37
39
|
fallback: true,
|
|
40
|
+
retries: DEFAULT_REQUEST_RETRIES,
|
|
41
|
+
backoff: DEFAULT_BACKOFF,
|
|
38
42
|
});
|
|
39
43
|
/** The API key, or a `NO_API_KEY` error that names the variable. */
|
|
40
44
|
export function apiKey(settings) {
|
|
@@ -63,6 +67,26 @@ export function addUsage(a, b) {
|
|
|
63
67
|
/** A usage of zero tokens. */
|
|
64
68
|
export const NO_USAGE = Object.freeze({ inputTokens: 0, outputTokens: 0, cacheReadInputTokens: 0, cacheCreationInputTokens: 0 });
|
|
65
69
|
const count = (value) => (typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : 0);
|
|
70
|
+
/** The four fields of the usage of the API and their names in `LlmUsage`. */
|
|
71
|
+
const USAGE_FIELDS = [
|
|
72
|
+
["input_tokens", "inputTokens"],
|
|
73
|
+
["output_tokens", "outputTokens"],
|
|
74
|
+
["cache_read_input_tokens", "cacheReadInputTokens"],
|
|
75
|
+
["cache_creation_input_tokens", "cacheCreationInputTokens"],
|
|
76
|
+
];
|
|
77
|
+
/**
|
|
78
|
+
* This function gives the usage of a `usage` object of the API. A field that the object does not
|
|
79
|
+
* have keeps its value of `base`: the usage of a `message_delta` event changes only some fields.
|
|
80
|
+
*/
|
|
81
|
+
export function usageOf(raw, base = NO_USAGE) {
|
|
82
|
+
const record = raw !== null && typeof raw === "object" ? raw : {};
|
|
83
|
+
const usage = { ...base };
|
|
84
|
+
for (const [from, to] of USAGE_FIELDS) {
|
|
85
|
+
if (typeof record[from] === "number")
|
|
86
|
+
usage[to] = count(record[from]);
|
|
87
|
+
}
|
|
88
|
+
return usage;
|
|
89
|
+
}
|
|
66
90
|
/** The answer of the API, checked: an answer of another form is a `BAD_RESPONSE`. */
|
|
67
91
|
function parseResponse(text) {
|
|
68
92
|
let body;
|
|
@@ -76,23 +100,25 @@ function parseResponse(text) {
|
|
|
76
100
|
if (record === null || typeof record !== "object" || !Array.isArray(record["content"]) || typeof record["stop_reason"] !== "string") {
|
|
77
101
|
throw new LlmError("BAD_RESPONSE", "llm: the answer of the API is not a message with content and stop_reason");
|
|
78
102
|
}
|
|
79
|
-
const usage = (record["usage"] ?? {});
|
|
80
103
|
const details = record["stop_details"];
|
|
81
104
|
return {
|
|
82
105
|
model: typeof record["model"] === "string" ? record["model"] : "",
|
|
83
106
|
content: record["content"].filter((block) => block !== null && typeof block === "object" && typeof block.type === "string"),
|
|
84
107
|
stopReason: record["stop_reason"],
|
|
85
108
|
stopDetails: details !== null && typeof details === "object" ? details : undefined,
|
|
86
|
-
usage:
|
|
87
|
-
inputTokens: count(usage["input_tokens"]),
|
|
88
|
-
outputTokens: count(usage["output_tokens"]),
|
|
89
|
-
cacheReadInputTokens: count(usage["cache_read_input_tokens"]),
|
|
90
|
-
cacheCreationInputTokens: count(usage["cache_creation_input_tokens"]),
|
|
91
|
-
},
|
|
109
|
+
usage: usageOf(record["usage"]),
|
|
92
110
|
};
|
|
93
111
|
}
|
|
94
|
-
/**
|
|
95
|
-
function
|
|
112
|
+
/** True for a status that the same request can pass later: 408, 409, 429 and 5xx, as in the Anthropic SDKs. */
|
|
113
|
+
function retryableStatus(status) {
|
|
114
|
+
return status === 408 || status === 409 || status === 429 || status >= 500;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* The error of an answer with an error status. The message has the type and the message of the
|
|
118
|
+
* API. The header `x-should-retry` of the API overrides the rule of the status.
|
|
119
|
+
*/
|
|
120
|
+
function apiError(response, text, key) {
|
|
121
|
+
const { status } = response;
|
|
96
122
|
let detail = text.slice(0, 500);
|
|
97
123
|
try {
|
|
98
124
|
const error = JSON.parse(text).error;
|
|
@@ -104,13 +130,9 @@ function apiError(status, text, key) {
|
|
|
104
130
|
}
|
|
105
131
|
// The API does not repeat the key, but the message must not carry it in any case
|
|
106
132
|
const safe = detail.split(key).join("[key]");
|
|
107
|
-
const
|
|
108
|
-
|
|
109
|
-
}
|
|
110
|
-
/** The time that is left before the deadline of the capability, or undefined. */
|
|
111
|
-
function deadlineLeft(ctx) {
|
|
112
|
-
const deadline = ctx?.capability?.deadline;
|
|
113
|
-
return typeof deadline === "number" ? deadline - Date.now() : undefined;
|
|
133
|
+
const should = response.headers.get("x-should-retry");
|
|
134
|
+
const retryable = should === "true" ? true : should === "false" ? false : retryableStatus(status);
|
|
135
|
+
return new LlmError("API_ERROR", `llm: the API answered ${status}: ${safe}`, { status, retryable, retryAfterMs: retryAfterMs(response.headers) });
|
|
114
136
|
}
|
|
115
137
|
/** The body of an answer as text, with a size limit. */
|
|
116
138
|
async function readText(response) {
|
|
@@ -123,10 +145,10 @@ async function readText(response) {
|
|
|
123
145
|
return text;
|
|
124
146
|
}
|
|
125
147
|
/**
|
|
126
|
-
* This function
|
|
127
|
-
* `
|
|
148
|
+
* This function opens the scope of one request. It throws `NO_API_KEY` without a key, `TIMEOUT`
|
|
149
|
+
* when the deadline passed and `ABORTED` when the caller aborted, before a request goes out.
|
|
128
150
|
*/
|
|
129
|
-
export
|
|
151
|
+
export function openScope(settings, limits) {
|
|
130
152
|
const key = apiKey(settings);
|
|
131
153
|
const { path, ctx } = limits;
|
|
132
154
|
const left = deadlineLeft(ctx);
|
|
@@ -146,34 +168,69 @@ export async function sendMessages(request, settings, limits) {
|
|
|
146
168
|
controller.abort();
|
|
147
169
|
};
|
|
148
170
|
ctx?.signal?.addEventListener("abort", onAbort, { once: true });
|
|
171
|
+
return {
|
|
172
|
+
key,
|
|
173
|
+
signal: controller.signal,
|
|
174
|
+
stopped: () => stop,
|
|
175
|
+
translate: (error) => {
|
|
176
|
+
if (stop === "timeout")
|
|
177
|
+
return new InvocationError("TIMEOUT", `${path.join(".")} passed its time limit of ${timeoutMs} ms`, path);
|
|
178
|
+
if (stop === "caller")
|
|
179
|
+
return new InvocationError("ABORTED", `Procedure was aborted: ${path.join(".")}`, path);
|
|
180
|
+
return error;
|
|
181
|
+
},
|
|
182
|
+
close: () => {
|
|
183
|
+
clearTimeout(timer);
|
|
184
|
+
ctx?.signal?.removeEventListener("abort", onAbort);
|
|
185
|
+
},
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
/** The message of an error, without the key. */
|
|
189
|
+
export function safeMessage(error, key) {
|
|
190
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
191
|
+
return message.split(key).join("[key]");
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* This function sends a request to the Messages API in a scope, and gives the answer with a
|
|
195
|
+
* status from 200 to 299. It throws `CONNECTION` for a failed connection and `API_ERROR` for an
|
|
196
|
+
* error status.
|
|
197
|
+
*/
|
|
198
|
+
export async function postMessages(request, settings, scope) {
|
|
149
199
|
const headers = {
|
|
150
200
|
"content-type": "application/json",
|
|
151
|
-
"x-api-key": key,
|
|
201
|
+
"x-api-key": scope.key,
|
|
152
202
|
"anthropic-version": ANTHROPIC_VERSION,
|
|
153
203
|
};
|
|
154
204
|
if (request.fallbacks !== undefined)
|
|
155
205
|
headers["anthropic-beta"] = FALLBACK_BETA;
|
|
206
|
+
let response;
|
|
156
207
|
try {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
if (stop === undefined) {
|
|
168
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
169
|
-
throw new LlmError("CONNECTION", `llm: the connection to the API failed: ${message.split(key).join("[key]")}`, { retryable: true });
|
|
170
|
-
}
|
|
171
|
-
throw error;
|
|
208
|
+
response = await settings.fetch(`${settings.baseUrl.replace(/\/+$/, "")}/v1/messages`, {
|
|
209
|
+
method: "POST",
|
|
210
|
+
headers,
|
|
211
|
+
body: JSON.stringify(request),
|
|
212
|
+
signal: scope.signal,
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
catch (error) {
|
|
216
|
+
if (scope.stopped() === undefined) {
|
|
217
|
+
throw new LlmError("CONNECTION", `llm: the connection to the API failed: ${safeMessage(error, scope.key)}`, { retryable: true });
|
|
172
218
|
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
219
|
+
throw error;
|
|
220
|
+
}
|
|
221
|
+
if (!response.ok)
|
|
222
|
+
throw apiError(response, await readText(response), scope.key);
|
|
223
|
+
return response;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* This function sends one request to the Messages API and gives the checked answer. It throws a
|
|
227
|
+
* `REFUSAL` error when a safety classifier declined the request.
|
|
228
|
+
*/
|
|
229
|
+
export async function sendMessages(request, settings, limits) {
|
|
230
|
+
const scope = openScope(settings, limits);
|
|
231
|
+
try {
|
|
232
|
+
const response = await postMessages(request, settings, scope);
|
|
233
|
+
const answer = parseResponse(await readText(response));
|
|
177
234
|
if (answer.stopReason === "refusal") {
|
|
178
235
|
const category = answer.stopDetails?.category ?? "not given";
|
|
179
236
|
throw new LlmError("REFUSAL", `llm: a safety classifier declined the request (category: ${category})`);
|
|
@@ -181,15 +238,17 @@ export async function sendMessages(request, settings, limits) {
|
|
|
181
238
|
return answer;
|
|
182
239
|
}
|
|
183
240
|
catch (error) {
|
|
184
|
-
|
|
185
|
-
throw new InvocationError("TIMEOUT", `${path.join(".")} passed its time limit of ${timeoutMs} ms`, path);
|
|
186
|
-
if (stop === "caller")
|
|
187
|
-
throw new InvocationError("ABORTED", `Procedure was aborted: ${path.join(".")}`, path);
|
|
188
|
-
throw error;
|
|
241
|
+
throw scope.translate(error);
|
|
189
242
|
}
|
|
190
243
|
finally {
|
|
191
|
-
|
|
192
|
-
ctx?.signal?.removeEventListener("abort", onAbort);
|
|
244
|
+
scope.close();
|
|
193
245
|
}
|
|
194
246
|
}
|
|
247
|
+
/**
|
|
248
|
+
* This function sends a request with `sendMessages`, and sends it again after a retryable
|
|
249
|
+
* error, at most `retries` more times.
|
|
250
|
+
*/
|
|
251
|
+
export function sendWithRetries(request, settings, limits, retries) {
|
|
252
|
+
return withRetries(() => sendMessages(request, settings, limits), { backoff: settings.backoff, retries }, limits);
|
|
253
|
+
}
|
|
195
254
|
//# sourceMappingURL=anthropic.js.map
|
package/dist/anthropic.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"anthropic.js","sourceRoot":"","sources":["../src/anthropic.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"anthropic.js","sourceRoot":"","sources":["../src/anthropic.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAGH,OAAO,EAAE,eAAe,EAA6C,MAAM,oBAAoB,CAAC;AAChG,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EAAE,eAAe,EAAE,uBAAuB,EAAE,YAAY,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAS/G,uCAAuC;AACvC,MAAM,CAAC,MAAM,aAAa,GAAG,mBAAmB,CAAC;AACjD,uDAAuD;AACvD,MAAM,CAAC,MAAM,mBAAmB,GAAG,mBAAmB,CAAC;AACvD,uCAAuC;AACvC,MAAM,CAAC,MAAM,gBAAgB,GAAG,2BAA2B,CAAC;AAC5D,kCAAkC;AAClC,MAAM,CAAC,MAAM,kBAAkB,GAAG,MAAM,CAAC;AACzC,uDAAuD;AACvD,MAAM,CAAC,MAAM,kBAAkB,GAAG,OAAO,CAAC;AAC1C,8CAA8C;AAC9C,MAAM,CAAC,MAAM,iBAAiB,GAAG,YAAY,CAAC;AAC9C,+EAA+E;AAC/E,MAAM,CAAC,MAAM,aAAa,GAAG,iCAAiC,CAAC;AAC/D,6DAA6D;AAC7D,MAAM,kBAAkB,GAAG,EAAE,GAAG,IAAI,GAAG,IAAI,CAAC;AAkB5C,0CAA0C;AAC1C,MAAM,CAAC,MAAM,oBAAoB,GAAgB,MAAM,CAAC,MAAM,CAAC;IAC7D,SAAS,EAAE,mBAAmB;IAC9B,GAAG,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,GAAG;IACtB,KAAK,EAAE,CAAC,GAAW,EAAE,IAAiB,EAAE,EAAE,CAAC,UAAU,CAAC,KAAK,CAAC,GAAG,EAAE,IAAI,CAAC;IACtE,OAAO,EAAE,gBAAgB;IACzB,YAAY,E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,UAAU,YAAY,CAAC,OAAwB,EAAE,QAAqB,EAAE,MAAkB;IACpG,MAAM,KAAK,GAAG,SAAS,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;IAC1C,IAAI,CAAC;QACH,MAAM,QAAQ,GAAG,MAAM,YAAY,CAAC,OAAO,EAAE,QAAQ,EAAE,KAAK,CAAC,CAAC;QAC9D,MAAM,MAAM,GAAG,aAAa,CAAC,MAAM,QAAQ,CAAC,QAAQ,CAAC,CAAC,CAAC;QACvD,IAAI,MAAM,CAAC,UAAU,KAAK,SAAS,EAAE,CAAC;YACpC,MAAM,QAAQ,GAAG,MAAM,CAAC,WAAW,EAAE,QAAQ,IAAI,WAAW,CAAC;YAC7D,MAAM,IAAI,QAAQ,CAAC,SAAS,EAAE,4DAA4D,QAAQ,GAAG,CAAC,CAAC;QACzG,CAAC;QACD,OAAO,MAAM,CAAC;IAChB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,KAAK,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;IAC/B,CAAC;YAAS,CAAC;QACT,KAAK,CAAC,KAAK,EAAE,CAAC;IAChB,CAAC;AACH,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,eAAe,CAAC,OAAwB,EAAE,QAAqB,EAAE,MAAkB,EAAE,OAAe;IAClH,OAAO,WAAW,CAAC,GAAG,EAAE,CAAC,YAAY,CAAC,OAAO,EAAE,QAAQ,EAAE,MAAM,CAAC,EAAE,EAAE,OAAO,EAAE,QAAQ,CAAC,OAAO,EAAE,OAAO,EAAE,EAAE,MAAM,CAAC,CAAC;AACpH,CAAC"}
|
package/dist/errors.d.ts
CHANGED
|
@@ -6,29 +6,36 @@
|
|
|
6
6
|
export type LlmErrorCode =
|
|
7
7
|
/** The environment variable of the API key has no value. */
|
|
8
8
|
"NO_API_KEY"
|
|
9
|
-
/** The API answered with an error status
|
|
9
|
+
/** The API answered with an error status, or sent an error event in a stream. */
|
|
10
10
|
| "API_ERROR"
|
|
11
|
-
/** The connection to the API failed. */
|
|
11
|
+
/** The connection to the API failed, or a stream ended before its end event. */
|
|
12
12
|
| "CONNECTION"
|
|
13
|
-
/** The answer of the API does not have the form of a message. */
|
|
13
|
+
/** The answer of the API does not have the form of a message or of a stream of events. */
|
|
14
14
|
| "BAD_RESPONSE"
|
|
15
15
|
/** A safety classifier declined the request (`stop_reason: "refusal"`). */
|
|
16
16
|
| "REFUSAL"
|
|
17
|
+
/** A tool definition is not valid: a name that two tools have, or a procedure without an object input. */
|
|
18
|
+
| "BAD_TOOL"
|
|
17
19
|
/** `llm.structured`: the output stopped at `maxTokens`, so the JSON is not complete. */
|
|
18
20
|
| "MAX_TOKENS"
|
|
19
21
|
/** `llm.structured`: no answer gave a value that the schema accepts. */
|
|
20
22
|
| "INVALID_OUTPUT";
|
|
23
|
+
/** The options of an `LlmError`. */
|
|
24
|
+
export interface LlmErrorOptions {
|
|
25
|
+
readonly status?: number | undefined;
|
|
26
|
+
readonly retryable?: boolean | undefined;
|
|
27
|
+
readonly retryAfterMs?: number | undefined;
|
|
28
|
+
}
|
|
21
29
|
/** An error of a call to the model. */
|
|
22
30
|
export declare class LlmError extends Error {
|
|
23
31
|
readonly name = "LlmError";
|
|
24
32
|
readonly code: LlmErrorCode;
|
|
25
|
-
/** The HTTP status of an `API_ERROR`. */
|
|
33
|
+
/** The HTTP status of an `API_ERROR`. An error event of a stream has no status. */
|
|
26
34
|
readonly status: number | undefined;
|
|
27
35
|
/** True when the same call can succeed later: a rate limit, an overload, a server error or a connection error. */
|
|
28
36
|
readonly retryable: boolean;
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
});
|
|
37
|
+
/** The wait that the API asked for with `retry-after-ms` or `retry-after`, in milliseconds. */
|
|
38
|
+
readonly retryAfterMs: number | undefined;
|
|
39
|
+
constructor(code: LlmErrorCode, message: string, options?: LlmErrorOptions);
|
|
33
40
|
}
|
|
34
41
|
//# sourceMappingURL=errors.d.ts.map
|
package/dist/errors.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,mCAAmC;AACnC,MAAM,MAAM,YAAY;AACtB,4DAA4D;AAC1D,YAAY;AACd,
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,mCAAmC;AACnC,MAAM,MAAM,YAAY;AACtB,4DAA4D;AAC1D,YAAY;AACd,iFAAiF;GAC/E,WAAW;AACb,gFAAgF;GAC9E,YAAY;AACd,0FAA0F;GACxF,cAAc;AAChB,2EAA2E;GACzE,SAAS;AACX,0GAA0G;GACxG,UAAU;AACZ,wFAAwF;GACtF,YAAY;AACd,wEAAwE;GACtE,gBAAgB,CAAC;AAErB,oCAAoC;AACpC,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACrC,QAAQ,CAAC,SAAS,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IACzC,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CAC5C;AAED,uCAAuC;AACvC,qBAAa,QAAS,SAAQ,KAAK;IACjC,SAAkB,IAAI,cAAc;IACpC,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAC;IAC5B,mFAAmF;IACnF,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC;IACpC,kHAAkH;IAClH,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAC;IAC5B,+FAA+F;IAC/F,QAAQ,CAAC,YAAY,EAAE,MAAM,GAAG,SAAS,CAAC;IAE1C,YAAY,IAAI,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,GAAE,eAAoB,EAM7E;CACF"}
|
package/dist/errors.js
CHANGED
|
@@ -6,15 +6,18 @@
|
|
|
6
6
|
export class LlmError extends Error {
|
|
7
7
|
name = "LlmError";
|
|
8
8
|
code;
|
|
9
|
-
/** The HTTP status of an `API_ERROR`. */
|
|
9
|
+
/** The HTTP status of an `API_ERROR`. An error event of a stream has no status. */
|
|
10
10
|
status;
|
|
11
11
|
/** True when the same call can succeed later: a rate limit, an overload, a server error or a connection error. */
|
|
12
12
|
retryable;
|
|
13
|
+
/** The wait that the API asked for with `retry-after-ms` or `retry-after`, in milliseconds. */
|
|
14
|
+
retryAfterMs;
|
|
13
15
|
constructor(code, message, options = {}) {
|
|
14
16
|
super(message);
|
|
15
17
|
this.code = code;
|
|
16
18
|
this.status = options.status;
|
|
17
19
|
this.retryable = options.retryable ?? false;
|
|
20
|
+
this.retryAfterMs = options.retryAfterMs;
|
|
18
21
|
}
|
|
19
22
|
}
|
|
20
23
|
//# sourceMappingURL=errors.js.map
|
package/dist/errors.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;GAGG;
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;GAGG;AA4BH,uCAAuC;AACvC,MAAM,OAAO,QAAS,SAAQ,KAAK;IACf,IAAI,GAAG,UAAU,CAAC;IAC3B,IAAI,CAAe;IAC5B,mFAAmF;IAC1E,MAAM,CAAqB;IACpC,kHAAkH;IACzG,SAAS,CAAU;IAC5B,+FAA+F;IACtF,YAAY,CAAqB;IAE1C,YAAY,IAAkB,EAAE,OAAe,EAAE,OAAO,GAAoB,EAAE;QAC5E,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;QAC7B,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC,SAAS,IAAI,KAAK,CAAC;QAC5C,IAAI,CAAC,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC;IAC3C,CAAC;CACF"}
|
package/dist/events.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The streaming Messages API (`"stream": true`): the server-sent events of an answer as stream items.
|
|
3
|
+
*
|
|
4
|
+
* `sseEvents` of `@procedures/http` parses the event stream. The first event of the API is
|
|
5
|
+
* `message_start`. Then each content block comes (`content_block_start`, its
|
|
6
|
+
* `content_block_delta` events, `content_block_stop`), then one or more `message_delta` events, and
|
|
7
|
+
* at the end `message_stop`. A `ping` event can come at each time, and an `error` event stops the
|
|
8
|
+
* stream. The usage of a `message_delta` event is cumulative. Other event types are ignored, as
|
|
9
|
+
* the versioning policy of the API asks.
|
|
10
|
+
*
|
|
11
|
+
* A stream gets a new try only before its first item. After the first item, an error goes to the
|
|
12
|
+
* caller.
|
|
13
|
+
*/
|
|
14
|
+
import { type CallLimits, type LlmSettings, type MessagesRequest } from "./anthropic.js";
|
|
15
|
+
import type { LlmStreamOutput } from "./types.js";
|
|
16
|
+
/**
|
|
17
|
+
* This function sends a request with `"stream": true` and gives each item of the answer. Before
|
|
18
|
+
* the first item, a retryable error gets a new try, at most `retries` times.
|
|
19
|
+
*/
|
|
20
|
+
export declare function streamMessages(request: MessagesRequest, settings: LlmSettings, limits: CallLimits, retries: number): AsyncGenerator<LlmStreamOutput, void, undefined>;
|
|
21
|
+
//# sourceMappingURL=events.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"events.d.ts","sourceRoot":"","sources":["../src/events.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAIH,OAAO,EAA2D,KAAK,UAAU,EAAE,KAAK,WAAW,EAAE,KAAK,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAGlJ,OAAO,KAAK,EAAE,eAAe,EAAwB,MAAM,YAAY,CAAC;AAoLxE;;;GAGG;AACH,wBAAuB,cAAc,CAAC,OAAO,EAAE,eAAe,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,GAAG,cAAc,CAAC,eAAe,EAAE,IAAI,EAAE,SAAS,CAAC,CAc5K"}
|