akanjs 3.3.0-rc.75 → 3.3.0-rc.77
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/package.json +1 -1
- package/service/predefinedAdaptor/anthropicLlm.ts +20 -8
- package/service/predefinedAdaptor/anthropicTools.ts +4 -1
- package/service/predefinedAdaptor/llmOverflow.ts +7 -0
- package/service/predefinedAdaptor/openaiLlm.ts +1 -2
- package/types/service/predefinedAdaptor/anthropicLlm.d.ts +18 -2
- package/types/service/predefinedAdaptor/anthropicTools.d.ts +4 -2
- package/types/service/predefinedAdaptor/llmOverflow.d.ts +2 -0
package/package.json
CHANGED
|
@@ -89,9 +89,19 @@ interface AnthropicStreamEvent {
|
|
|
89
89
|
|
|
90
90
|
export type AnthropicRequestBody = ReturnType<typeof AnthropicLlm.requestBody>;
|
|
91
91
|
|
|
92
|
+
/** A body as `body` hands it on: the retries read `messages` and `tools`, and the API judges the rest. */
|
|
93
|
+
export interface AnthropicSentBody {
|
|
94
|
+
messages: readonly { role: string; content: readonly { type: string }[] }[];
|
|
95
|
+
tools?: readonly { defer_loading?: true }[];
|
|
96
|
+
[field: string]: unknown;
|
|
97
|
+
}
|
|
98
|
+
|
|
92
99
|
export interface AnthropicLlmOption extends LlmOption {
|
|
93
|
-
/**
|
|
94
|
-
|
|
100
|
+
/**
|
|
101
|
+
* Has the last word on each body, after the retries chose its form: a `cache_control` breakpoint, `system` as
|
|
102
|
+
* blocks, an `effort`.
|
|
103
|
+
*/
|
|
104
|
+
body?: (body: AnthropicRequestBody, request: LlmTurnRequest) => AnthropicSentBody;
|
|
95
105
|
/** `anthropic-beta` values what `body` adds needs, sent beside the ones a retry adds. */
|
|
96
106
|
betas?: string[];
|
|
97
107
|
}
|
|
@@ -122,13 +132,16 @@ export class AnthropicLlm
|
|
|
122
132
|
|
|
123
133
|
readonly #dropped = new Set<string>();
|
|
124
134
|
readonly #plainTools = new Set<string>();
|
|
135
|
+
#setting = "option.setLlm";
|
|
125
136
|
|
|
126
137
|
/**
|
|
127
138
|
* One with settings of its own, beside `option.setLlm`: an app's adaptor sending some turns here under another key
|
|
128
139
|
* keeps the retries, and what they learned about each model and conversation, by keeping the instance.
|
|
129
140
|
*/
|
|
130
141
|
static of(option: AnthropicLlmOption): AnthropicLlm {
|
|
131
|
-
|
|
142
|
+
const llm = Object.assign(new AnthropicLlm(), { llmOption: option });
|
|
143
|
+
llm.#setting = "AnthropicLlm.of";
|
|
144
|
+
return llm;
|
|
132
145
|
}
|
|
133
146
|
|
|
134
147
|
get #host() {
|
|
@@ -152,7 +165,7 @@ export class AnthropicLlm
|
|
|
152
165
|
const model = request.model || this.llmOption.model;
|
|
153
166
|
if (!this.llmOption.apiKey || !model) {
|
|
154
167
|
this.logger.warn(
|
|
155
|
-
|
|
168
|
+
`AnthropicLlm needs both apiKey and model — set them with ${this.#setting}(). Agent turns are unavailable.`,
|
|
156
169
|
);
|
|
157
170
|
return null;
|
|
158
171
|
}
|
|
@@ -180,8 +193,7 @@ export class AnthropicLlm
|
|
|
180
193
|
model,
|
|
181
194
|
};
|
|
182
195
|
} catch (error) {
|
|
183
|
-
if (!signal?.aborted)
|
|
184
|
-
this.logger.error(`Anthropic turn failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
196
|
+
if (!signal?.aborted) LlmOverflow.failed(this.logger, "Anthropic", error);
|
|
185
197
|
throw LlmSpend.attach(error, { model });
|
|
186
198
|
}
|
|
187
199
|
}
|
|
@@ -189,7 +201,7 @@ export class AnthropicLlm
|
|
|
189
201
|
#reported(answer: LlmTurnAnswer): LlmTurnAnswer {
|
|
190
202
|
if (!answer.text && !answer.toolCalls?.length && answer.stop !== "length")
|
|
191
203
|
this.logger.warn(
|
|
192
|
-
`Anthropic answered with no text and no tool call. If this repeats, raise
|
|
204
|
+
`Anthropic answered with no text and no tool call. If this repeats, raise ${this.#setting}({ maxTokens }) — currently ${this.llmOption.maxTokens ?? AnthropicLlm.defaultMaxTokens}.`,
|
|
193
205
|
);
|
|
194
206
|
return answer;
|
|
195
207
|
}
|
|
@@ -205,7 +217,7 @@ export class AnthropicLlm
|
|
|
205
217
|
async #send(
|
|
206
218
|
model: string,
|
|
207
219
|
conversation: string | null,
|
|
208
|
-
bodyOf: (thinking: AnthropicThinkingReplay | undefined, toolChanges: boolean) =>
|
|
220
|
+
bodyOf: (thinking: AnthropicThinkingReplay | undefined, toolChanges: boolean) => AnthropicSentBody,
|
|
209
221
|
signal?: AbortSignal,
|
|
210
222
|
): Promise<Response> {
|
|
211
223
|
let thinking: AnthropicThinkingReplay | undefined =
|
|
@@ -48,7 +48,10 @@ export class AnthropicTools {
|
|
|
48
48
|
];
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
-
static engaged(body: {
|
|
51
|
+
static engaged(body: {
|
|
52
|
+
tools?: readonly { defer_loading?: true }[];
|
|
53
|
+
messages: readonly { role: string }[];
|
|
54
|
+
}): boolean {
|
|
52
55
|
return (
|
|
53
56
|
!!body.tools?.some((tool) => tool.defer_loading) || body.messages.some((message) => message.role === "system")
|
|
54
57
|
);
|
|
@@ -72,6 +72,13 @@ export class LlmOverflow {
|
|
|
72
72
|
return new Err("agent.error.llmDeclined", { provider, ...(typeof category === "string" ? { category } : {}) });
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
+
static failed(logger: Pick<Logger, "warn" | "error">, provider: string, error: unknown) {
|
|
76
|
+
if (Err.is(error, "agent.error.llmRequestFailed", "agent.error.llmDeclined")) return;
|
|
77
|
+
const line = `${provider} turn failed: ${error instanceof Error ? error.message : String(error)}`;
|
|
78
|
+
if (Err.is(error, "agent.error.contextOverflow")) logger.warn(line);
|
|
79
|
+
else logger.error(line);
|
|
80
|
+
}
|
|
81
|
+
|
|
75
82
|
static #count(text: string | undefined): number | undefined {
|
|
76
83
|
const count = text ? Number(text.replace(/,/g, "")) : Number.NaN;
|
|
77
84
|
return Number.isFinite(count) && count > 0 ? count : undefined;
|
|
@@ -78,8 +78,7 @@ export class OpenaiLlm
|
|
|
78
78
|
return { ...(await OpenaiDialect.consumeStream(body, onDelta, this.#host, request.tools)), model };
|
|
79
79
|
} catch (error) {
|
|
80
80
|
|
|
81
|
-
if (!signal?.aborted)
|
|
82
|
-
this.logger.error(`LLM turn failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
81
|
+
if (!signal?.aborted) LlmOverflow.failed(this.logger, "LLM", error);
|
|
83
82
|
throw LlmSpend.attach(error, { model });
|
|
84
83
|
}
|
|
85
84
|
}
|
|
@@ -69,9 +69,25 @@ interface AnthropicAnswer {
|
|
|
69
69
|
}
|
|
70
70
|
type AnthropicThinkingReplay = "drop" | "strip";
|
|
71
71
|
export type AnthropicRequestBody = ReturnType<typeof AnthropicLlm.requestBody>;
|
|
72
|
+
/** A body as `body` hands it on: the retries read `messages` and `tools`, and the API judges the rest. */
|
|
73
|
+
export interface AnthropicSentBody {
|
|
74
|
+
messages: readonly {
|
|
75
|
+
role: string;
|
|
76
|
+
content: readonly {
|
|
77
|
+
type: string;
|
|
78
|
+
}[];
|
|
79
|
+
}[];
|
|
80
|
+
tools?: readonly {
|
|
81
|
+
defer_loading?: true;
|
|
82
|
+
}[];
|
|
83
|
+
[field: string]: unknown;
|
|
84
|
+
}
|
|
72
85
|
export interface AnthropicLlmOption extends LlmOption {
|
|
73
|
-
/**
|
|
74
|
-
|
|
86
|
+
/**
|
|
87
|
+
* Has the last word on each body, after the retries chose its form: a `cache_control` breakpoint, `system` as
|
|
88
|
+
* blocks, an `effort`.
|
|
89
|
+
*/
|
|
90
|
+
body?: (body: AnthropicRequestBody, request: LlmTurnRequest) => AnthropicSentBody;
|
|
75
91
|
/** `anthropic-beta` values what `body` adds needs, sent beside the ones a retry adds. */
|
|
76
92
|
betas?: string[];
|
|
77
93
|
}
|
|
@@ -22,8 +22,10 @@ export declare class AnthropicTools {
|
|
|
22
22
|
static declared({ tools, deferred }: Pick<LlmTurnRequest, "tools" | "deferred">): AgentWireTool[];
|
|
23
23
|
static changes(from: ReadonlySet<string>, to: ReadonlySet<string>, declared: ReadonlySet<string>): AnthropicToolChange[];
|
|
24
24
|
static engaged(body: {
|
|
25
|
-
tools?:
|
|
26
|
-
|
|
25
|
+
tools?: readonly {
|
|
26
|
+
defer_loading?: true;
|
|
27
|
+
}[];
|
|
28
|
+
messages: readonly {
|
|
27
29
|
role: string;
|
|
28
30
|
}[];
|
|
29
31
|
}): boolean;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Logger } from "akanjs/common";
|
|
1
2
|
/** Each field is present only when the refusal named it. */
|
|
2
3
|
export interface LlmOverflowFacts {
|
|
3
4
|
limit?: number;
|
|
@@ -18,4 +19,5 @@ export declare class LlmOverflow {
|
|
|
18
19
|
* discarded. `category` is the provider's policy class, kept for an app to branch on; `explanation` stays here.
|
|
19
20
|
*/
|
|
20
21
|
static declined(host: string, category?: unknown, explanation?: unknown): Error;
|
|
22
|
+
static failed(logger: Pick<Logger, "warn" | "error">, provider: string, error: unknown): void;
|
|
21
23
|
}
|