@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
|
@@ -2,22 +2,62 @@
|
|
|
2
2
|
* llm.complete procedure
|
|
3
3
|
*
|
|
4
4
|
* Send a prompt to a language model and give the text and the usage. The text is the text
|
|
5
|
-
* blocks of the answer, joined: the thinking blocks are not in it.
|
|
5
|
+
* blocks of the last answer, joined: the thinking blocks are not in it.
|
|
6
|
+
*
|
|
7
|
+
* With `tools`, the output gives each `tool_use` block of the answer, and the caller starts them.
|
|
8
|
+
* With `procedures`, the procedure starts the tools. It calls each procedure that the model asks
|
|
9
|
+
* for through `ctx.client` and sends the results back. Then it asks again, until the model stops
|
|
10
|
+
* or `maxRounds` requests went out.
|
|
6
11
|
*/
|
|
7
|
-
import { DEFAULT_LLM_SETTINGS,
|
|
8
|
-
import { baseRequest, timeoutOf } from "../../request.js";
|
|
12
|
+
import { addUsage, DEFAULT_LLM_SETTINGS, NO_USAGE, sendWithRetries, textOf } from "../../anthropic.js";
|
|
13
|
+
import { addTools, baseRequest, retriesOf, timeoutOf, toolParam } from "../../request.js";
|
|
14
|
+
import { DEFAULT_MAX_ROUNDS, procedureTools, runToolUse, toolUsesOf } from "../../tools.js";
|
|
9
15
|
const PATH = ["llm", "complete"];
|
|
10
16
|
/**
|
|
11
17
|
* Send a prompt to a language model and give the text and the usage
|
|
12
18
|
*/
|
|
13
19
|
export async function llmComplete(input, ctx, settings = DEFAULT_LLM_SETTINGS) {
|
|
14
20
|
const request = baseRequest(input, settings, input.system);
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
21
|
+
const procedures = procedureTools(input.procedures ?? [], ctx, PATH);
|
|
22
|
+
addTools(request, [...(input.tools ?? []).map(toolParam), ...procedures.map((tool) => tool.param)], input.toolChoice);
|
|
23
|
+
const byName = new Map(procedures.map((tool) => [tool.param.name, tool]));
|
|
24
|
+
const limits = { path: PATH, ctx, timeoutMs: timeoutOf(input) };
|
|
25
|
+
const retries = retriesOf(input.retries, settings);
|
|
26
|
+
const maxRounds = input.maxRounds ?? DEFAULT_MAX_ROUNDS;
|
|
27
|
+
let usage = NO_USAGE;
|
|
28
|
+
const toolCalls = [];
|
|
29
|
+
for (let round = 1;; round++) {
|
|
30
|
+
const answer = await sendWithRetries(request, settings, limits, retries);
|
|
31
|
+
usage = addUsage(usage, answer.usage);
|
|
32
|
+
const uses = toolUsesOf(answer.content);
|
|
33
|
+
// The procedure runs the tools only when each tool of the answer is a procedure tool
|
|
34
|
+
const runnable = answer.stopReason === "tool_use" && uses.length > 0 && uses.every((use) => byName.has(use.name));
|
|
35
|
+
if (!runnable || round >= maxRounds) {
|
|
36
|
+
return {
|
|
37
|
+
text: textOf(answer.content),
|
|
38
|
+
model: answer.model || request.model,
|
|
39
|
+
stopReason: answer.stopReason,
|
|
40
|
+
usage,
|
|
41
|
+
toolUses: uses,
|
|
42
|
+
toolCalls,
|
|
43
|
+
rounds: round,
|
|
44
|
+
roundLimit: runnable,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
// One at a time, in the sequence of the answer: a procedure can change data that the next one reads
|
|
48
|
+
const results = [];
|
|
49
|
+
for (const use of uses) {
|
|
50
|
+
const { call, result } = await runToolUse(use, byName.get(use.name), ctx);
|
|
51
|
+
toolCalls.push(call);
|
|
52
|
+
results.push(result);
|
|
53
|
+
}
|
|
54
|
+
// The next request continues the conversation: the answer as it came, then the results
|
|
55
|
+
request.messages = [...request.messages, { role: "assistant", content: answer.content }, { role: "user", content: results }];
|
|
56
|
+
// A forced tool_choice applies to the first request only: then the model can stop
|
|
57
|
+
if (request.tool_choice !== undefined && (request.tool_choice.type === "any" || request.tool_choice.type === "tool")) {
|
|
58
|
+
const { disable_parallel_tool_use } = request.tool_choice;
|
|
59
|
+
request.tool_choice = { type: "auto", ...(disable_parallel_tool_use !== undefined ? { disable_parallel_tool_use } : {}) };
|
|
60
|
+
}
|
|
61
|
+
}
|
|
22
62
|
}
|
|
23
63
|
//# sourceMappingURL=complete.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"complete.js","sourceRoot":"","sources":["../../../src/procedures/llm/complete.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"complete.js","sourceRoot":"","sources":["../../../src/procedures/llm/complete.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EAAE,QAAQ,EAAE,oBAAoB,EAAE,QAAQ,EAAE,eAAe,EAAE,MAAM,EAAwD,MAAM,oBAAoB,CAAC;AAC7J,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,SAAS,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC1F,OAAO,EAAE,kBAAkB,EAAE,cAAc,EAAE,UAAU,EAAE,UAAU,EAAoB,MAAM,gBAAgB,CAAC;AAG9G,MAAM,IAAI,GAAG,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC;AAEjC;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAAC,KAAuB,EAAE,GAAiB,EAAE,QAAQ,GAAgB,oBAAoB;IACxH,MAAM,OAAO,GAAG,WAAW,CAAC,KAAK,EAAE,QAAQ,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;IAC3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,MAAM,CAAC,EAAE,CAAC;YACrH,MAAM,EAAE,yBAAyB,EAAE,GAAG,OAAO,CAAC,WAAW,CAAC;YAC1D,OAAO,CAAC,WAAW,GAAG,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,CAAC,yBAAyB,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,yBAAyB,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC;QAC5H,CAAC;IACH,CAAC;AACH,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/procedures/llm/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/procedures/llm/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAC7D,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/procedures/llm/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/procedures/llm/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAC7D,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* llm.stream procedure
|
|
3
|
+
*
|
|
4
|
+
* Send a prompt to a language model and stream the events of the answer. The request has
|
|
5
|
+
* `"stream": true`. The items are the start of the message, the text deltas and each complete
|
|
6
|
+
* `tool_use` block. The last item has the stop reason, the usage and the full text.
|
|
7
|
+
*/
|
|
8
|
+
import { type CallLimits, type LlmSettings } from "../../anthropic.js";
|
|
9
|
+
import type { LlmStreamInput, LlmStreamOutput } from "../../types.js";
|
|
10
|
+
/**
|
|
11
|
+
* Send a prompt to a language model and stream the events of the answer
|
|
12
|
+
*/
|
|
13
|
+
export declare function llmStream(input: LlmStreamInput, ctx?: CallLimits["ctx"], settings?: LlmSettings): AsyncGenerator<LlmStreamOutput, void, undefined>;
|
|
14
|
+
//# sourceMappingURL=stream.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../../../src/procedures/llm/stream.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAwB,KAAK,UAAU,EAAE,KAAK,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAG7F,OAAO,KAAK,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAItE;;GAEG;AACH,wBAAuB,SAAS,CAAC,KAAK,EAAE,cAAc,EAAE,GAAG,CAAC,EAAE,UAAU,CAAC,KAAK,CAAC,EAAE,QAAQ,GAAE,WAAkC,GAAG,cAAc,CAAC,eAAe,EAAE,IAAI,EAAE,SAAS,CAAC,CAI/K"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* llm.stream procedure
|
|
3
|
+
*
|
|
4
|
+
* Send a prompt to a language model and stream the events of the answer. The request has
|
|
5
|
+
* `"stream": true`. The items are the start of the message, the text deltas and each complete
|
|
6
|
+
* `tool_use` block. The last item has the stop reason, the usage and the full text.
|
|
7
|
+
*/
|
|
8
|
+
import { DEFAULT_LLM_SETTINGS } from "../../anthropic.js";
|
|
9
|
+
import { streamMessages } from "../../events.js";
|
|
10
|
+
import { addTools, baseRequest, retriesOf, timeoutOf, toolParam } from "../../request.js";
|
|
11
|
+
const PATH = ["llm", "stream"];
|
|
12
|
+
/**
|
|
13
|
+
* Send a prompt to a language model and stream the events of the answer
|
|
14
|
+
*/
|
|
15
|
+
export async function* llmStream(input, ctx, settings = DEFAULT_LLM_SETTINGS) {
|
|
16
|
+
const request = baseRequest(input, settings, input.system);
|
|
17
|
+
addTools(request, (input.tools ?? []).map(toolParam), input.toolChoice);
|
|
18
|
+
yield* streamMessages(request, settings, { path: PATH, ctx, timeoutMs: timeoutOf(input) }, retriesOf(input.retries, settings));
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=stream.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream.js","sourceRoot":"","sources":["../../../src/procedures/llm/stream.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,oBAAoB,EAAqC,MAAM,oBAAoB,CAAC;AAC7F,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,SAAS,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAG1F,MAAM,IAAI,GAAG,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;AAE/B;;GAEG;AACH,MAAM,CAAC,KAAK,SAAS,CAAC,CAAC,SAAS,CAAC,KAAqB,EAAE,GAAuB,EAAE,QAAQ,GAAgB,oBAAoB;IAC3H,MAAM,OAAO,GAAG,WAAW,CAAC,KAAK,EAAE,QAAQ,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;IAC3D,QAAQ,CAAC,OAAO,EAAE,CAAC,KAAK,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,SAAS,CAAC,EAAE,KAAK,CAAC,UAAU,CAAC,CAAC;IACxE,KAAK,CAAC,CAAC,cAAc,CAAC,OAAO,EAAE,QAAQ,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,SAAS,EAAE,SAAS,CAAC,KAAK,CAAC,EAAE,EAAE,SAAS,CAAC,KAAK,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC,CAAC;AACjI,CAAC"}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* llm.structured procedure
|
|
3
3
|
*
|
|
4
4
|
* Send a prompt and a JSON Schema to a language model and give a value that the schema accepts.
|
|
5
|
-
* An answer that is not JSON, or that the schema refuses, gets a new try
|
|
5
|
+
* An answer that is not JSON, or that the schema refuses, gets a new try. The next request has
|
|
6
6
|
* the answer (its content, unchanged) and a user turn with the problems. At most 2 new tries.
|
|
7
7
|
*/
|
|
8
8
|
import { type CallLimits, type LlmSettings } from "../../anthropic.js";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"structured.d.ts","sourceRoot":"","sources":["../../../src/procedures/llm/structured.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAIH,OAAO,
|
|
1
|
+
{"version":3,"file":"structured.d.ts","sourceRoot":"","sources":["../../../src/procedures/llm/structured.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAIH,OAAO,EAAqE,KAAK,UAAU,EAAE,KAAK,WAAW,EAAwB,MAAM,oBAAoB,CAAC;AAGhK,OAAO,KAAK,EAAE,kBAAkB,EAAE,mBAAmB,EAAY,MAAM,gBAAgB,CAAC;AAIxF,4DAA4D;AAC5D,eAAO,MAAM,WAAW,IAAI,CAAC;AAoB7B;;GAEG;AACH,wBAAsB,aAAa,CAAC,KAAK,EAAE,kBAAkB,EAAE,GAAG,CAAC,EAAE,UAAU,CAAC,KAAK,CAAC,EAAE,QAAQ,GAAE,WAAkC,GAAG,OAAO,CAAC,mBAAmB,CAAC,CAuClK"}
|
|
@@ -2,13 +2,13 @@
|
|
|
2
2
|
* llm.structured procedure
|
|
3
3
|
*
|
|
4
4
|
* Send a prompt and a JSON Schema to a language model and give a value that the schema accepts.
|
|
5
|
-
* An answer that is not JSON, or that the schema refuses, gets a new try
|
|
5
|
+
* An answer that is not JSON, or that the schema refuses, gets a new try. The next request has
|
|
6
6
|
* the answer (its content, unchanged) and a user turn with the problems. At most 2 new tries.
|
|
7
7
|
*/
|
|
8
8
|
import { validateJson } from "@procedures/json";
|
|
9
9
|
import { LlmError } from "../../errors.js";
|
|
10
|
-
import { addUsage, DEFAULT_LLM_SETTINGS, NO_USAGE,
|
|
11
|
-
import { baseRequest, timeoutOf } from "../../request.js";
|
|
10
|
+
import { addUsage, DEFAULT_LLM_SETTINGS, NO_USAGE, sendWithRetries, textOf } from "../../anthropic.js";
|
|
11
|
+
import { baseRequest, retriesOf, timeoutOf } from "../../request.js";
|
|
12
12
|
import { constrainedSchema, parseAnswer } from "../../schema.js";
|
|
13
13
|
const PATH = ["llm", "structured"];
|
|
14
14
|
/** The most new tries after an answer that is not valid. */
|
|
@@ -37,6 +37,7 @@ function problemsOf(value, schema) {
|
|
|
37
37
|
export async function llmStructured(input, ctx, settings = DEFAULT_LLM_SETTINGS) {
|
|
38
38
|
const constrained = input.constrained ?? true;
|
|
39
39
|
const retries = Math.min(Math.max(input.retries ?? MAX_RETRIES, 0), MAX_RETRIES);
|
|
40
|
+
const requestRetries = retriesOf(input.requestRetries, settings);
|
|
40
41
|
const system = [input.system, instruction(input, constrained)].filter((part) => part !== undefined && part !== "").join("\n\n");
|
|
41
42
|
const request = baseRequest(input, settings, system);
|
|
42
43
|
if (constrained) {
|
|
@@ -45,7 +46,8 @@ export async function llmStructured(input, ctx, settings = DEFAULT_LLM_SETTINGS)
|
|
|
45
46
|
let usage = NO_USAGE;
|
|
46
47
|
let lastProblem = "";
|
|
47
48
|
for (let attempt = 1; attempt <= retries + 1; attempt++) {
|
|
48
|
-
|
|
49
|
+
// A retryable error of the API gets new tries here: they do not count as attempts
|
|
50
|
+
const answer = await sendWithRetries(request, settings, { path: PATH, ctx, timeoutMs: timeoutOf(input) }, requestRetries);
|
|
49
51
|
usage = addUsage(usage, answer.usage);
|
|
50
52
|
if (answer.stopReason === "max_tokens") {
|
|
51
53
|
throw new LlmError("MAX_TOKENS", `llm.structured: the answer stopped at maxTokens (${request.max_tokens}), so the JSON is not complete`);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"structured.js","sourceRoot":"","sources":["../../../src/procedures/llm/structured.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,YAAY,EAAkB,MAAM,kBAAkB,CAAC;AAChE,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EAAE,QAAQ,EAAE,oBAAoB,EAAE,QAAQ,EAAE,
|
|
1
|
+
{"version":3,"file":"structured.js","sourceRoot":"","sources":["../../../src/procedures/llm/structured.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,YAAY,EAAkB,MAAM,kBAAkB,CAAC;AAChE,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EAAE,QAAQ,EAAE,oBAAoB,EAAE,QAAQ,EAAE,eAAe,EAAE,MAAM,EAA2D,MAAM,oBAAoB,CAAC;AAChK,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AACrE,OAAO,EAAE,iBAAiB,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAGjE,MAAM,IAAI,GAAG,CAAC,KAAK,EAAE,YAAY,CAAC,CAAC;AAEnC,4DAA4D;AAC5D,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,CAAC;AAE7B,2EAA2E;AAC3E,SAAS,WAAW,CAAC,KAAyB,EAAE,WAAoB;IAClE,MAAM,KAAK,GAAG,CAAC,gIAAgI,CAAC,CAAC;IACjJ,IAAI,CAAC,WAAW;QAAE,KAAK,CAAC,IA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M,CAAC,EAAE,EAAE,CAAC;YACd,MAAM,EAAE,QAAQ,EAAE,SAAS,EAAE,GAAG,UAAU,CAAC,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;YACvE,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;gBAC3B,OAAO,EAAE,KAAK,EAAE,MAAM,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,CAAC,KAAK,IAAI,OAAO,CAAC,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC;YAC5G,CAAC;YACD,OAAO,GAAG,iCAAiC,QAAQ,EAAE,CAAC;QACxD,CAAC;aAAM,CAAC;YACN,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC;QAC3B,CAAC;QACD,WAAW,GAAG,OAAO,CAAC;QACtB,wFAAwF;QACxF,OAAO,CAAC,QAAQ,GAAG;YACjB,GAAG,OAAO,CAAC,QAAQ;YACnB,EAAE,IAAI,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,CAAC,OAAO,EAAE;YAC9C,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,6BAA6B,OAAO,0CAA0C,EAAE;SAC1G,CAAC;IACJ,CAAC;IACD,MAAM,IAAI,QAAQ,CAAC,gBAAgB,EAAE,gCAAgC,OAAO,GAAG,CAAC,0CAA0C,WAAW,EAAE,CAAC,CAAC;AAC3I,CAAC"}
|
package/dist/request.d.ts
CHANGED
|
@@ -1,10 +1,21 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The request of the fields that
|
|
2
|
+
* The request of the fields that the procedures take (`LlmRequestInput`), and the tool fields.
|
|
3
3
|
*/
|
|
4
|
-
import { type LlmSettings, type MessagesRequest } from "./anthropic.js";
|
|
5
|
-
import type { LlmRequestInput } from "./types.js";
|
|
4
|
+
import { type LlmSettings, type MessagesRequest, type ToolChoiceParam, type ToolParam } from "./anthropic.js";
|
|
5
|
+
import type { LlmRequestInput, LlmTool, LlmToolChoice } from "./types.js";
|
|
6
6
|
/** This function gives the body of a request: the model, the limits, the system text and the user text. */
|
|
7
7
|
export declare function baseRequest(input: LlmRequestInput, settings: LlmSettings, system: string | undefined): MessagesRequest;
|
|
8
|
-
/** The time limit of a
|
|
8
|
+
/** The time limit of a request. */
|
|
9
9
|
export declare function timeoutOf(input: LlmRequestInput): number;
|
|
10
|
+
/** The number of new tries after a retryable error: the input, or the default of the module. */
|
|
11
|
+
export declare function retriesOf(given: number | undefined, settings: LlmSettings): number;
|
|
12
|
+
/** The definition of a tool of the input, as the API gets it. */
|
|
13
|
+
export declare function toolParam(tool: LlmTool): ToolParam;
|
|
14
|
+
/** The `tool_choice` of the input, as the API gets it. */
|
|
15
|
+
export declare function toolChoiceParam(choice: LlmToolChoice): ToolChoiceParam;
|
|
16
|
+
/**
|
|
17
|
+
* This function puts the tools and the `tool_choice` into a request. It throws `BAD_TOOL` for two
|
|
18
|
+
* tools with the same name, and for a `toolChoice` without tools.
|
|
19
|
+
*/
|
|
20
|
+
export declare function addTools(request: MessagesRequest, tools: readonly ToolParam[], choice: LlmToolChoice | undefined): void;
|
|
10
21
|
//# sourceMappingURL=request.d.ts.map
|
package/dist/request.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request.d.ts","sourceRoot":"","sources":["../src/request.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAA0C,KAAK,WAAW,EAAE,KAAK,eAAe,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"request.d.ts","sourceRoot":"","sources":["../src/request.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAA0C,KAAK,WAAW,EAAE,KAAK,eAAe,EAAE,KAAK,eAAe,EAAE,KAAK,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAGtJ,OAAO,KAAK,EAAE,eAAe,EAAE,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAE1E,2GAA2G;AAC3G,wBAAgB,WAAW,CAAC,KAAK,EAAE,eAAe,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,GAAG,SAAS,GAAG,eAAe,CAWtH;AAED,mCAAmC;AACnC,wBAAgB,SAAS,CAAC,KAAK,EAAE,eAAe,GAAG,MAAM,CAExD;AAED,gGAAgG;AAChG,wBAAgB,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,EAAE,QAAQ,EAAE,WAAW,GAAG,MAAM,CAElF;AAED,iEAAiE;AACjE,wBAAgB,SAAS,CAAC,IAAI,EAAE,OAAO,GAAG,SAAS,CAOlD;AAED,0DAA0D;AAC1D,wBAAgB,eAAe,CAAC,MAAM,EAAE,aAAa,GAAG,eAAe,CAOtE;AAED;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,OAAO,EAAE,eAAe,EAAE,KAAK,EAAE,SAAS,SAAS,EAAE,EAAE,MAAM,EAAE,aAAa,GAAG,SAAS,GAAG,IAAI,CAUvH"}
|
package/dist/request.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The request of the fields that
|
|
2
|
+
* The request of the fields that the procedures take (`LlmRequestInput`), and the tool fields.
|
|
3
3
|
*/
|
|
4
4
|
import { DEFAULT_MAX_TOKENS, DEFAULT_TIMEOUT_MS } from "./anthropic.js";
|
|
5
|
+
import { LlmError } from "./errors.js";
|
|
6
|
+
import { MAX_REQUEST_RETRIES } from "./retry.js";
|
|
5
7
|
/** This function gives the body of a request: the model, the limits, the system text and the user text. */
|
|
6
8
|
export function baseRequest(input, settings, system) {
|
|
7
9
|
const request = {
|
|
@@ -19,8 +21,51 @@ export function baseRequest(input, settings, system) {
|
|
|
19
21
|
request.fallbacks = "default";
|
|
20
22
|
return request;
|
|
21
23
|
}
|
|
22
|
-
/** The time limit of a
|
|
24
|
+
/** The time limit of a request. */
|
|
23
25
|
export function timeoutOf(input) {
|
|
24
26
|
return input.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
25
27
|
}
|
|
28
|
+
/** The number of new tries after a retryable error: the input, or the default of the module. */
|
|
29
|
+
export function retriesOf(given, settings) {
|
|
30
|
+
return Math.min(Math.max(given ?? settings.retries, 0), MAX_REQUEST_RETRIES);
|
|
31
|
+
}
|
|
32
|
+
/** The definition of a tool of the input, as the API gets it. */
|
|
33
|
+
export function toolParam(tool) {
|
|
34
|
+
return {
|
|
35
|
+
name: tool.name,
|
|
36
|
+
...(tool.description !== undefined ? { description: tool.description } : {}),
|
|
37
|
+
input_schema: tool.inputSchema,
|
|
38
|
+
...(tool.strict !== undefined ? { strict: tool.strict } : {}),
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/** The `tool_choice` of the input, as the API gets it. */
|
|
42
|
+
export function toolChoiceParam(choice) {
|
|
43
|
+
if (choice.type === "none")
|
|
44
|
+
return { type: "none" };
|
|
45
|
+
return {
|
|
46
|
+
type: choice.type,
|
|
47
|
+
...(choice.type === "tool" ? { name: choice.name } : {}),
|
|
48
|
+
...(choice.disableParallelToolUse !== undefined ? { disable_parallel_tool_use: choice.disableParallelToolUse } : {}),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* This function puts the tools and the `tool_choice` into a request. It throws `BAD_TOOL` for two
|
|
53
|
+
* tools with the same name, and for a `toolChoice` without tools.
|
|
54
|
+
*/
|
|
55
|
+
export function addTools(request, tools, choice) {
|
|
56
|
+
const names = new Set();
|
|
57
|
+
for (const tool of tools) {
|
|
58
|
+
if (names.has(tool.name))
|
|
59
|
+
throw new LlmError("BAD_TOOL", `llm: two tools have the name ${tool.name}`);
|
|
60
|
+
names.add(tool.name);
|
|
61
|
+
}
|
|
62
|
+
if (choice !== undefined && tools.length === 0)
|
|
63
|
+
throw new LlmError("BAD_TOOL", "llm: toolChoice needs tools");
|
|
64
|
+
if (choice?.type === "tool" && !names.has(choice.name))
|
|
65
|
+
throw new LlmError("BAD_TOOL", `llm: toolChoice names the tool ${choice.name}, and no tool has that name`);
|
|
66
|
+
if (tools.length > 0)
|
|
67
|
+
request.tools = [...tools];
|
|
68
|
+
if (choice !== undefined)
|
|
69
|
+
request.tool_choice = toolChoiceParam(choice);
|
|
70
|
+
}
|
|
26
71
|
//# sourceMappingURL=request.js.map
|
package/dist/request.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request.js","sourceRoot":"","sources":["../src/request.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,
|
|
1
|
+
{"version":3,"file":"request.js","sourceRoot":"","sources":["../src/request.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAgF,MAAM,gBAAgB,CAAC;AACtJ,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAC;AAGjD,2GAA2G;AAC3G,MAAM,UAAU,WAAW,CAAC,KAAsB,EAAE,QAAqB,EAAE,MAA0B;IACnG,MAAM,OAAO,GAAoB;QAC/B,KAAK,EAAE,KAAK,CAAC,KAAK,IAAI,QAAQ,CAAC,YAAY;QAC3C,UAAU,EAAE,KAAK,CAAC,SAAS,IAAI,kBAAkB;QACjD,QAAQ,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,CAAC,MAAM,EAAE,CAAC;KACpD,CAAC;IACF,IAAI,MAAM,KAAK,SAAS,IAAI,MAAM,KAAK,EAAE;QAAE,OAAO,CAAC,MAAM,GAAG,MAAM,CAAC;IACnE,IAAI,KAAK,CAAC,WAAW,KAAK,SAAS;QAAE,OAAO,CAAC,WAAW,GAAG,KAAK,CAAC,WAAW,CAAC;IAC7E,IAAI,KAAK,CAAC,MAAM,KAAK,SAAS;QAAE,OAAO,CAAC,aAAa,GAAG,EAAE,MAAM,EAAE,KAAK,CAAC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
* A request that fails with an `LlmError` with `retryable: true` (429, 529 and the other 5xx
|
|
5
|
+
* statuses, 408, 409, a connection error, an error event at the start of a stream) gets a new
|
|
6
|
+
* try. The wait before a try is the `retry-after-ms` or `retry-after` header of the API, or the
|
|
7
|
+
* backoff of `@mark1russell7/service/backoff`. A wait that ends after the deadline of the call
|
|
8
|
+
* gives the error at once. The abort of the caller stops a wait with `ABORTED`.
|
|
9
|
+
*/
|
|
10
|
+
import { type Backoff } from "@mark1russell7/service/backoff";
|
|
11
|
+
import type { CallLimits } from "./anthropic.js";
|
|
12
|
+
/** The default number of new tries after a retryable error, as in the Anthropic SDKs. */
|
|
13
|
+
export declare const DEFAULT_REQUEST_RETRIES = 2;
|
|
14
|
+
/** The most new tries that an input or the module can ask for. */
|
|
15
|
+
export declare const MAX_REQUEST_RETRIES = 10;
|
|
16
|
+
/** The longest wait that a `retry-after` header can ask for: 60 seconds. A longer wait gives the error at once. */
|
|
17
|
+
export declare const MAX_RETRY_AFTER_MS = 60000;
|
|
18
|
+
/**
|
|
19
|
+
* The default backoff: 500 ms before the first new try, then two times more each time, at most
|
|
20
|
+
* 8 seconds. A jitter removes up to a quarter of each wait.
|
|
21
|
+
*/
|
|
22
|
+
export declare const DEFAULT_BACKOFF: Backoff;
|
|
23
|
+
/** What a retry uses: the backoff policy and the number of new tries. */
|
|
24
|
+
export interface RetryPolicy {
|
|
25
|
+
readonly backoff: Backoff;
|
|
26
|
+
readonly retries: number;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* This function gives the wait that the headers of an answer ask for, in milliseconds, or
|
|
30
|
+
* undefined. It reads `retry-after-ms`, then `retry-after` as seconds or as an HTTP date.
|
|
31
|
+
*/
|
|
32
|
+
export declare function retryAfterMs(headers: Pick<Headers, "get">, now?: number): number | undefined;
|
|
33
|
+
/** The time that is left before the deadline of the capability, or undefined. */
|
|
34
|
+
export declare function deadlineLeft(ctx: CallLimits["ctx"]): number | undefined;
|
|
35
|
+
/**
|
|
36
|
+
* This function decides about a new try after an error. It waits and gives true, or it gives
|
|
37
|
+
* false at once. `retry` is the number of the new try: the first new try is 1.
|
|
38
|
+
*/
|
|
39
|
+
export declare function waitToRetry(error: unknown, retry: number, policy: RetryPolicy, limits: CallLimits): Promise<boolean>;
|
|
40
|
+
/**
|
|
41
|
+
* This function runs `attempt` and gives its result. After a retryable error, it waits and
|
|
42
|
+
* runs `attempt` again, at most `policy.retries` more times. The last error goes to the caller.
|
|
43
|
+
*/
|
|
44
|
+
export declare function withRetries<T>(attempt: () => Promise<T>, policy: RetryPolicy, limits: CallLimits): Promise<T>;
|
|
45
|
+
//# sourceMappingURL=retry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"retry.d.ts","sourceRoot":"","sources":["../src/retry.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAgB,KAAK,OAAO,EAAE,MAAM,gCAAgC,CAAC;AAG5E,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAEjD,yFAAyF;AACzF,eAAO,MAAM,uBAAuB,IAAI,CAAC;AACzC,kEAAkE;AAClE,eAAO,MAAM,mBAAmB,KAAK,CAAC;AACtC,mHAAmH;AACnH,eAAO,MAAM,kBAAkB,QAAS,CAAC;AAEzC;;;GAGG;AACH,eAAO,MAAM,eAAe,EAAE,OAK5B,CAAC;AAEH,yEAAyE;AACzE,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CAC1B;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE,KAAK,CAAC,EAAE,GAAG,GAAE,MAAmB,GAAG,MAAM,GAAG,SAAS,CAYxG;AAED,iFAAiF;AACjF,wBAAgB,YAAY,CAAC,GAAG,EAAE,UAAU,CAAC,KAAK,CAAC,GAAG,MAAM,GAAG,SAAS,CAGvE;AAoBD;;;GAGG;AACH,wBAAsB,WAAW,CAAC,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,UAAU,GAAG,OAAO,CAAC,OAAO,CAAC,CAU1H;AAED;;;GAGG;AACH,wBAAsB,WAAW,CAAC,CAAC,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,UAAU,GAAG,OAAO,CAAC,CAAC,CAAC,CAQnH"}
|
package/dist/retry.js
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The automatic retry of a request to the Messages API.
|
|
3
|
+
*
|
|
4
|
+
* A request that fails with an `LlmError` with `retryable: true` (429, 529 and the other 5xx
|
|
5
|
+
* statuses, 408, 409, a connection error, an error event at the start of a stream) gets a new
|
|
6
|
+
* try. The wait before a try is the `retry-after-ms` or `retry-after` header of the API, or the
|
|
7
|
+
* backoff of `@mark1russell7/service/backoff`. A wait that ends after the deadline of the call
|
|
8
|
+
* gives the error at once. The abort of the caller stops a wait with `ABORTED`.
|
|
9
|
+
*/
|
|
10
|
+
import { backoffDelay } from "@mark1russell7/service/backoff";
|
|
11
|
+
import { InvocationError } from "@procedures/client";
|
|
12
|
+
import { LlmError } from "./errors.js";
|
|
13
|
+
/** The default number of new tries after a retryable error, as in the Anthropic SDKs. */
|
|
14
|
+
export const DEFAULT_REQUEST_RETRIES = 2;
|
|
15
|
+
/** The most new tries that an input or the module can ask for. */
|
|
16
|
+
export const MAX_REQUEST_RETRIES = 10;
|
|
17
|
+
/** The longest wait that a `retry-after` header can ask for: 60 seconds. A longer wait gives the error at once. */
|
|
18
|
+
export const MAX_RETRY_AFTER_MS = 60_000;
|
|
19
|
+
/**
|
|
20
|
+
* The default backoff: 500 ms before the first new try, then two times more each time, at most
|
|
21
|
+
* 8 seconds. A jitter removes up to a quarter of each wait.
|
|
22
|
+
*/
|
|
23
|
+
export const DEFAULT_BACKOFF = Object.freeze({
|
|
24
|
+
base: 500,
|
|
25
|
+
factor: 2,
|
|
26
|
+
cap: 8_000,
|
|
27
|
+
jitter: Object.freeze({ kind: "subtract", fraction: 0.25 }),
|
|
28
|
+
});
|
|
29
|
+
/**
|
|
30
|
+
* This function gives the wait that the headers of an answer ask for, in milliseconds, or
|
|
31
|
+
* undefined. It reads `retry-after-ms`, then `retry-after` as seconds or as an HTTP date.
|
|
32
|
+
*/
|
|
33
|
+
export function retryAfterMs(headers, now = Date.now()) {
|
|
34
|
+
const milliseconds = headers.get("retry-after-ms");
|
|
35
|
+
if (milliseconds !== null && milliseconds.trim() !== "") {
|
|
36
|
+
const value = Number(milliseconds);
|
|
37
|
+
if (Number.isFinite(value) && value >= 0)
|
|
38
|
+
return value;
|
|
39
|
+
}
|
|
40
|
+
const after = headers.get("retry-after");
|
|
41
|
+
if (after === null || after.trim() === "")
|
|
42
|
+
return undefined;
|
|
43
|
+
const seconds = Number(after);
|
|
44
|
+
if (Number.isFinite(seconds))
|
|
45
|
+
return seconds >= 0 ? seconds * 1000 : undefined;
|
|
46
|
+
const date = Date.parse(after);
|
|
47
|
+
return Number.isNaN(date) ? undefined : Math.max(0, date - now);
|
|
48
|
+
}
|
|
49
|
+
/** The time that is left before the deadline of the capability, or undefined. */
|
|
50
|
+
export function deadlineLeft(ctx) {
|
|
51
|
+
const deadline = ctx?.capability?.deadline;
|
|
52
|
+
return typeof deadline === "number" ? deadline - Date.now() : undefined;
|
|
53
|
+
}
|
|
54
|
+
/** This function waits `ms` milliseconds. The abort of the signal stops the wait with `ABORTED`. */
|
|
55
|
+
function sleep(ms, limits) {
|
|
56
|
+
const signal = limits.ctx?.signal;
|
|
57
|
+
const aborted = () => new InvocationError("ABORTED", `Procedure was aborted: ${limits.path.join(".")}`, limits.path);
|
|
58
|
+
if (signal?.aborted)
|
|
59
|
+
return Promise.reject(aborted());
|
|
60
|
+
return new Promise((resolve, reject) => {
|
|
61
|
+
const onAbort = () => {
|
|
62
|
+
clearTimeout(timer);
|
|
63
|
+
reject(aborted());
|
|
64
|
+
};
|
|
65
|
+
const timer = setTimeout(() => {
|
|
66
|
+
signal?.removeEventListener("abort", onAbort);
|
|
67
|
+
resolve();
|
|
68
|
+
}, ms);
|
|
69
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* This function decides about a new try after an error. It waits and gives true, or it gives
|
|
74
|
+
* false at once. `retry` is the number of the new try: the first new try is 1.
|
|
75
|
+
*/
|
|
76
|
+
export async function waitToRetry(error, retry, policy, limits) {
|
|
77
|
+
if (retry > policy.retries)
|
|
78
|
+
return false;
|
|
79
|
+
if (!(error instanceof LlmError) || !error.retryable)
|
|
80
|
+
return false;
|
|
81
|
+
const after = error.retryAfterMs;
|
|
82
|
+
if (after !== undefined && after > MAX_RETRY_AFTER_MS)
|
|
83
|
+
return false;
|
|
84
|
+
const wait = after ?? backoffDelay(policy.backoff, retry);
|
|
85
|
+
const left = deadlineLeft(limits.ctx);
|
|
86
|
+
if (left !== undefined && wait >= left)
|
|
87
|
+
return false;
|
|
88
|
+
await sleep(wait, limits);
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* This function runs `attempt` and gives its result. After a retryable error, it waits and
|
|
93
|
+
* runs `attempt` again, at most `policy.retries` more times. The last error goes to the caller.
|
|
94
|
+
*/
|
|
95
|
+
export async function withRetries(attempt, policy, limits) {
|
|
96
|
+
for (let retry = 1;; retry++) {
|
|
97
|
+
try {
|
|
98
|
+
return await attempt();
|
|
99
|
+
}
|
|
100
|
+
catch (error) {
|
|
101
|
+
if (!(await waitToRetry(error, retry, policy, limits)))
|
|
102
|
+
throw error;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
//# sourceMappingURL=retry.js.map
|
|
@@ -0,0 +1 @@
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AAC,GAAI,KAAK,EAAE,EAAE,CAAC;QAC9B,IAAI,CAAC;YACH,OAAO,MAAM,OAAO,EAAE,CAAC;QACzB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,CAAC,CAAC,MAAM,WAAW,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;gBAAE,MAAM,KAAK,CAAC;QACtE,CAAC;IACH,CAAC;AACH,CAAC"}
|
package/dist/tools.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Procedures as tools of the model (`llm.complete` with `procedures`).
|
|
3
|
+
*
|
|
4
|
+
* The caller names the procedures. `llm.complete` gives the model a tool for each one. The name
|
|
5
|
+
* is the key with `_` in place of each `.`. The description and the input schema come from the
|
|
6
|
+
* procedure. When the model asks for a tool, `llm.complete` calls the procedure through
|
|
7
|
+
* `ctx.client` with the input of the model. The output goes back as a `tool_result`.
|
|
8
|
+
*
|
|
9
|
+
* The procedure `llm.complete` is data-driven (`markDataDriven`): its input chooses its callees.
|
|
10
|
+
* Thus the admission of the host examines each nested call: its capability and its expose rule.
|
|
11
|
+
* Before the first request, the same rules examine each named procedure, so a refused tool costs
|
|
12
|
+
* no request.
|
|
13
|
+
*/
|
|
14
|
+
import { type ProcedureContext, type ProcedurePath } from "@procedures/client";
|
|
15
|
+
import type { ContentBlock, ToolParam } from "./anthropic.js";
|
|
16
|
+
import type { LlmToolCall, LlmToolUse } from "./types.js";
|
|
17
|
+
/** The context that procedure tools need: the nested client, the registry and the rules of the host. */
|
|
18
|
+
export type ToolContext = Pick<ProcedureContext, "signal" | "capability"> & Partial<Pick<ProcedureContext, "client" | "registry" | "expose">>;
|
|
19
|
+
/** A procedure that the model can use as a tool. */
|
|
20
|
+
export interface ProcedureTool {
|
|
21
|
+
/** The key of the procedure, for example `weather.current`. */
|
|
22
|
+
readonly key: string;
|
|
23
|
+
readonly path: ProcedurePath;
|
|
24
|
+
/** The definition of the tool, as the API gets it. */
|
|
25
|
+
readonly param: ToolParam;
|
|
26
|
+
}
|
|
27
|
+
/** The default of `maxRounds`: the most requests to the model in one call of `llm.complete`. */
|
|
28
|
+
export declare const DEFAULT_MAX_ROUNDS = 8;
|
|
29
|
+
/** The most characters of the output of a tool that the model gets. */
|
|
30
|
+
export declare const MAX_TOOL_RESULT_CHARS = 200000;
|
|
31
|
+
/** The name of the tool of a procedure: the key with `_` in place of each `.`. */
|
|
32
|
+
export declare function toolNameOf(key: string): string;
|
|
33
|
+
/**
|
|
34
|
+
* This function gives the tools of the procedures that the input names. It throws before a
|
|
35
|
+
* request goes out. It throws for a refusal of the expose rule or of the capability. It also
|
|
36
|
+
* throws for a procedure that is not in the registry, and for a stream without an end.
|
|
37
|
+
*/
|
|
38
|
+
export declare function procedureTools(keys: readonly string[], ctx: ToolContext | undefined, caller: ProcedurePath): ProcedureTool[];
|
|
39
|
+
/** The `tool_use` blocks of the content of an answer. */
|
|
40
|
+
export declare function toolUsesOf(content: readonly ContentBlock[]): LlmToolUse[];
|
|
41
|
+
/**
|
|
42
|
+
* This function runs one `tool_use` block with the procedure of its tool, through `ctx.client`.
|
|
43
|
+
* It gives the record of the call and the `tool_result` block. An error of the procedure goes to
|
|
44
|
+
* the model with `is_error: true`. A refusal of the host, the abort and the deadline stop the call.
|
|
45
|
+
*/
|
|
46
|
+
export declare function runToolUse(use: LlmToolUse, tool: ProcedureTool, ctx: ToolContext): Promise<{
|
|
47
|
+
call: LlmToolCall;
|
|
48
|
+
result: ContentBlock;
|
|
49
|
+
}>;
|
|
50
|
+
//# sourceMappingURL=tools.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tools.d.ts","sourceRoot":"","sources":["../src/tools.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAML,KAAK,gBAAgB,EACrB,KAAK,aAAa,EACnB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,KAAK,E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