akanjs 3.3.0-rc.74 → 3.3.0-rc.75
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/agentLoop/agentLoop.ts +17 -11
- package/service/agentLoop/loopCompaction.ts +2 -1
- package/service/agentLoop/loopLent.ts +80 -0
- package/service/predefinedAdaptor/anthropicLlm.ts +29 -10
- package/service/predefinedAdaptor/llm.adaptor.ts +5 -0
- package/types/server/logging/rotatingLogWriter.d.ts +1 -1
- package/types/service/agentLoop/loopLent.d.ts +16 -0
- package/types/service/predefinedAdaptor/anthropicLlm.d.ts +13 -1
- package/types/service/predefinedAdaptor/llm.adaptor.d.ts +5 -0
package/package.json
CHANGED
|
@@ -27,6 +27,7 @@ import { LoopAnswer, type LoopPark } from "./loopAnswer";
|
|
|
27
27
|
import { LoopAsk } from "./loopAsk";
|
|
28
28
|
import { LoopCompaction } from "./loopCompaction";
|
|
29
29
|
import { LoopContext } from "./loopContext";
|
|
30
|
+
import { LoopLent } from "./loopLent";
|
|
30
31
|
import { LoopRemotes } from "./loopRemotes";
|
|
31
32
|
import { LoopRequest } from "./loopRequest";
|
|
32
33
|
import { LoopStored } from "./loopStored";
|
|
@@ -82,7 +83,7 @@ export class AgentLoop {
|
|
|
82
83
|
readonly #remotes: LoopRemotes;
|
|
83
84
|
readonly #toolkits: LoopToolkits;
|
|
84
85
|
readonly #askUser: LoopAsk;
|
|
85
|
-
#
|
|
86
|
+
readonly #lent = new LoopLent();
|
|
86
87
|
#account: unknown = null;
|
|
87
88
|
#round = 0;
|
|
88
89
|
#tokens = 0;
|
|
@@ -156,7 +157,8 @@ export class AgentLoop {
|
|
|
156
157
|
const { answer, tools } = yield* this.#ask(compacted ?? listed);
|
|
157
158
|
|
|
158
159
|
const mark = this.#tainted ? { tainted: true } : {};
|
|
159
|
-
const
|
|
160
|
+
const tracks = this.#toolkits.tracksListing || this.#lent.tracks(this.#messages);
|
|
161
|
+
const shown = tracks ? this.#lent.listingOf(tools, this.#messages) : {};
|
|
160
162
|
if (answer.stop === "length") {
|
|
161
163
|
|
|
162
164
|
if (answer.text) await this.#append({ role: "assistant", text: answer.text, ...shown, ...mark });
|
|
@@ -189,13 +191,14 @@ export class AgentLoop {
|
|
|
189
191
|
const listed = this.#toolkits.listed(visible);
|
|
190
192
|
const taken = new Set(visible.map((tool) => tool.name));
|
|
191
193
|
|
|
194
|
+
const free = (name: string) =>
|
|
195
|
+
!taken.has(name) && !this.#toolkits.isMeta(name) && !this.#askUser.isMeta(name) && !this.#remotes.ownerOf(name);
|
|
192
196
|
const client = (this.#request.clientTools ?? []).filter((tool) => {
|
|
193
|
-
if (
|
|
194
|
-
if (this.#remotes.ownerOf(tool.name)) return false;
|
|
197
|
+
if (!free(tool.name)) return false;
|
|
195
198
|
taken.add(tool.name);
|
|
196
199
|
return true;
|
|
197
200
|
});
|
|
198
|
-
this.#
|
|
201
|
+
this.#lent.lend(client, this.#messages, free);
|
|
199
202
|
this.#judgeStored(visible);
|
|
200
203
|
return [...listed, ...remote.tools, ...client.map(AgentLoop.#clientTool), ...this.#askUser.listed()];
|
|
201
204
|
}
|
|
@@ -262,9 +265,12 @@ export class AgentLoop {
|
|
|
262
265
|
]
|
|
263
266
|
.filter(Boolean)
|
|
264
267
|
.join("\n\n");
|
|
265
|
-
const deferred =
|
|
268
|
+
const deferred = [
|
|
269
|
+
...this.#toolkits.deferred(tools).map(({ readOnly: _readOnly, ...tool }) => tool),
|
|
270
|
+
...this.#lent.left,
|
|
271
|
+
];
|
|
266
272
|
return {
|
|
267
|
-
messages: LoopTranscript.wire(this.#messages),
|
|
273
|
+
messages: LoopLent.unlent(LoopTranscript.wire(this.#messages)),
|
|
268
274
|
tools: tools.map(({ readOnly: _readOnly, ...tool }) => tool),
|
|
269
275
|
context: [],
|
|
270
276
|
...(deferred.length ? { deferred } : {}),
|
|
@@ -386,7 +392,7 @@ export class AgentLoop {
|
|
|
386
392
|
stop = "awaiting";
|
|
387
393
|
break;
|
|
388
394
|
}
|
|
389
|
-
if (verdict === "allow" && this.#
|
|
395
|
+
if (verdict === "allow" && this.#lent.has(call.name)) {
|
|
390
396
|
this.#toolCalls += 1;
|
|
391
397
|
yield { type: "awaiting", call, client: true };
|
|
392
398
|
stop = "awaiting";
|
|
@@ -424,7 +430,7 @@ export class AgentLoop {
|
|
|
424
430
|
async *#accept(answer: AgentLoopCallAnswer): Events<void> {
|
|
425
431
|
const whole = LoopAnswer.resultOf(answer, this.#messages, (call) => this.#parks(call));
|
|
426
432
|
const result = LoopToolOutput.clipped(whole);
|
|
427
|
-
if (this.#
|
|
433
|
+
if (this.#lent.has(whole.name)) this.#tainted = true;
|
|
428
434
|
await this.#record([result]);
|
|
429
435
|
yield { type: "toolResult", result, round: this.#round };
|
|
430
436
|
}
|
|
@@ -433,7 +439,7 @@ export class AgentLoop {
|
|
|
433
439
|
if (call.argsError) return null;
|
|
434
440
|
const kit = this.#toolkits.externalOf(call);
|
|
435
441
|
if (kit) return { refusal: LoopRemotes.declined(kit), code: LoopRemotes.declinedCode };
|
|
436
|
-
const parked = this.#askUser.isMeta(call.name) ? !LoopAsk.refusalOf(call) : this.#
|
|
442
|
+
const parked = this.#askUser.isMeta(call.name) ? !LoopAsk.refusalOf(call) : this.#lent.has(call.name);
|
|
437
443
|
return parked ? "answer" : null;
|
|
438
444
|
}
|
|
439
445
|
|
|
@@ -472,7 +478,7 @@ export class AgentLoop {
|
|
|
472
478
|
const state = {
|
|
473
479
|
tainted: this.#tainted,
|
|
474
480
|
tool,
|
|
475
|
-
...(this.#
|
|
481
|
+
...(this.#lent.has(call.name) ? { client: true as const } : {}),
|
|
476
482
|
...(remote ? { remote } : {}),
|
|
477
483
|
};
|
|
478
484
|
if (approve) return AgentLoop.#judged(await this.#untilHalted(approve(call, state)));
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { AgentWireAttachment, AgentWireMessage, LlmTurnRequest } from "../predefinedAdaptor/llm.adaptor";
|
|
2
|
+
import { LoopLent } from "./loopLent";
|
|
2
3
|
import { LoopTranscript } from "./loopTranscript";
|
|
3
4
|
|
|
4
5
|
export class LoopCompaction {
|
|
@@ -16,7 +17,7 @@ export class LoopCompaction {
|
|
|
16
17
|
|
|
17
18
|
static tokensOf(messages: readonly AgentWireMessage[]): number {
|
|
18
19
|
let chars = 0;
|
|
19
|
-
for (const message of LoopTranscript.seen(messages)) chars += LoopCompaction.#charsOf(message);
|
|
20
|
+
for (const message of LoopLent.unlent(LoopTranscript.seen(messages))) chars += LoopCompaction.#charsOf(message);
|
|
20
21
|
return Math.ceil(chars / 4);
|
|
21
22
|
}
|
|
22
23
|
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type { AgentWireMessage, AgentWireTool } from "../predefinedAdaptor/llm.adaptor";
|
|
2
|
+
import { LoopRequest } from "./loopRequest";
|
|
3
|
+
|
|
4
|
+
export class LoopLent {
|
|
5
|
+
#names = new Set<string>();
|
|
6
|
+
#left: AgentWireTool[] = [];
|
|
7
|
+
|
|
8
|
+
lend(lent: readonly AgentWireTool[], messages: readonly AgentWireMessage[], free: (name: string) => boolean) {
|
|
9
|
+
this.#names = new Set(lent.map((tool) => tool.name));
|
|
10
|
+
this.#left = LoopLent.stored(messages).filter((tool) => !this.#names.has(tool.name) && free(tool.name));
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
has(name: string): boolean {
|
|
14
|
+
return this.#names.has(name);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
get left(): readonly AgentWireTool[] {
|
|
18
|
+
return this.#left;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
tracks(messages: readonly AgentWireMessage[]): boolean {
|
|
22
|
+
return this.#names.size > 0 || messages.some((message) => message.listed);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
listingOf(tools: readonly AgentWireTool[], messages: readonly AgentWireMessage[]) {
|
|
26
|
+
const lent = LoopLent.fresh(
|
|
27
|
+
tools.filter((tool) => this.#names.has(tool.name)),
|
|
28
|
+
messages,
|
|
29
|
+
);
|
|
30
|
+
return { listed: tools.map((tool) => tool.name), ...(lent.length ? { lent } : {}) };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
static definition({ name, description, parameters }: AgentWireTool): AgentWireTool {
|
|
34
|
+
return { name, ...(description ? { description } : {}), ...(parameters ? { parameters } : {}) };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
static stored(messages: readonly AgentWireMessage[]): AgentWireTool[] {
|
|
38
|
+
const latest = new Map<string, AgentWireTool>();
|
|
39
|
+
for (const message of messages)
|
|
40
|
+
for (const tool of Array.isArray(message.lent) ? message.lent : [])
|
|
41
|
+
if (LoopLent.#valid(tool)) {
|
|
42
|
+
latest.delete(tool.name);
|
|
43
|
+
latest.set(tool.name, LoopLent.definition(tool));
|
|
44
|
+
}
|
|
45
|
+
const kept: AgentWireTool[] = [];
|
|
46
|
+
let chars = 0;
|
|
47
|
+
for (const tool of [...latest.values()].reverse()) {
|
|
48
|
+
chars += JSON.stringify(tool).length;
|
|
49
|
+
if (kept.length >= LoopRequest.maxClientTools || chars > LoopRequest.maxClientToolChars) break;
|
|
50
|
+
kept.push(tool);
|
|
51
|
+
}
|
|
52
|
+
return kept.reverse();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
static unlent(messages: AgentWireMessage[]): AgentWireMessage[] {
|
|
56
|
+
return messages.map((message) => {
|
|
57
|
+
if (!message.lent) return message;
|
|
58
|
+
const { lent: _lent, ...rest } = message;
|
|
59
|
+
return rest;
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
static fresh(lent: readonly AgentWireTool[], messages: readonly AgentWireMessage[]): AgentWireTool[] {
|
|
64
|
+
const known = new Map(LoopLent.stored(messages).map((tool) => [tool.name, JSON.stringify(tool)]));
|
|
65
|
+
return lent
|
|
66
|
+
.map((tool) => LoopLent.definition(tool))
|
|
67
|
+
.filter((tool) => known.get(tool.name) !== JSON.stringify(tool));
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
static #valid(tool: unknown): tool is AgentWireTool {
|
|
71
|
+
const { name, description, parameters } = (tool ?? {}) as Record<string, unknown>;
|
|
72
|
+
return (
|
|
73
|
+
typeof name === "string" &&
|
|
74
|
+
!!name &&
|
|
75
|
+
(description === undefined || typeof description === "string") &&
|
|
76
|
+
(parameters === undefined ||
|
|
77
|
+
(typeof parameters === "object" && parameters !== null && !Array.isArray(parameters)))
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -87,10 +87,19 @@ interface AnthropicStreamEvent {
|
|
|
87
87
|
};
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
+
export type AnthropicRequestBody = ReturnType<typeof AnthropicLlm.requestBody>;
|
|
91
|
+
|
|
92
|
+
export interface AnthropicLlmOption extends LlmOption {
|
|
93
|
+
/** Has the last word on each body, after the retries chose its form: a `cache_control` breakpoint, an `effort`. */
|
|
94
|
+
body?: (body: AnthropicRequestBody, request: LlmTurnRequest) => AnthropicRequestBody & Record<string, unknown>;
|
|
95
|
+
/** `anthropic-beta` values what `body` adds needs, sent beside the ones a retry adds. */
|
|
96
|
+
betas?: string[];
|
|
97
|
+
}
|
|
98
|
+
|
|
90
99
|
/** Anthropic's Messages API, which reads images and PDFs. `model` is required and has no default. */
|
|
91
100
|
export class AnthropicLlm
|
|
92
101
|
extends adapt("akanAnthropicLlm" as const, ({ use }) => ({
|
|
93
|
-
llmOption: use<
|
|
102
|
+
llmOption: use<AnthropicLlmOption>(),
|
|
94
103
|
}))
|
|
95
104
|
implements LlmAdaptor
|
|
96
105
|
{
|
|
@@ -114,6 +123,14 @@ export class AnthropicLlm
|
|
|
114
123
|
readonly #dropped = new Set<string>();
|
|
115
124
|
readonly #plainTools = new Set<string>();
|
|
116
125
|
|
|
126
|
+
/**
|
|
127
|
+
* One with settings of its own, beside `option.setLlm`: an app's adaptor sending some turns here under another key
|
|
128
|
+
* keeps the retries, and what they learned about each model and conversation, by keeping the instance.
|
|
129
|
+
*/
|
|
130
|
+
static of(option: AnthropicLlmOption): AnthropicLlm {
|
|
131
|
+
return Object.assign(new AnthropicLlm(), { llmOption: option });
|
|
132
|
+
}
|
|
133
|
+
|
|
117
134
|
get #host() {
|
|
118
135
|
return this.llmOption.host ?? "https://api.anthropic.com/v1";
|
|
119
136
|
}
|
|
@@ -141,13 +158,15 @@ export class AnthropicLlm
|
|
|
141
158
|
}
|
|
142
159
|
try {
|
|
143
160
|
const { accepts } = this;
|
|
144
|
-
const maxTokens = this.llmOption
|
|
161
|
+
const { maxTokens, body } = this.llmOption;
|
|
145
162
|
const stream = !!onDelta;
|
|
146
163
|
const response = await this.#send(
|
|
147
164
|
model,
|
|
148
165
|
AnthropicLlm.#conversationOf(request.messages),
|
|
149
|
-
(thinking, toolChanges) =>
|
|
150
|
-
AnthropicLlm.requestBody(model, request, { accepts, stream, maxTokens, thinking, toolChanges })
|
|
166
|
+
(thinking, toolChanges) => {
|
|
167
|
+
const built = AnthropicLlm.requestBody(model, request, { accepts, stream, maxTokens, thinking, toolChanges });
|
|
168
|
+
return body ? body(built, request) : built;
|
|
169
|
+
},
|
|
151
170
|
signal,
|
|
152
171
|
);
|
|
153
172
|
if (!onDelta)
|
|
@@ -186,10 +205,7 @@ export class AnthropicLlm
|
|
|
186
205
|
async #send(
|
|
187
206
|
model: string,
|
|
188
207
|
conversation: string | null,
|
|
189
|
-
bodyOf: (
|
|
190
|
-
thinking: AnthropicThinkingReplay | undefined,
|
|
191
|
-
toolChanges: boolean,
|
|
192
|
-
) => ReturnType<typeof AnthropicLlm.requestBody>,
|
|
208
|
+
bodyOf: (thinking: AnthropicThinkingReplay | undefined, toolChanges: boolean) => AnthropicRequestBody,
|
|
193
209
|
signal?: AbortSignal,
|
|
194
210
|
): Promise<Response> {
|
|
195
211
|
let thinking: AnthropicThinkingReplay | undefined =
|
|
@@ -200,8 +216,11 @@ export class AnthropicLlm
|
|
|
200
216
|
const body = bodyOf(thinking, toolChanges);
|
|
201
217
|
const engaged = AnthropicTools.engaged(body);
|
|
202
218
|
const betas = [
|
|
203
|
-
...(
|
|
204
|
-
|
|
219
|
+
...new Set([
|
|
220
|
+
...(thinking === "drop" ? [AnthropicLlm.thinkingBindingBeta] : []),
|
|
221
|
+
...(engaged ? [AnthropicTools.beta] : []),
|
|
222
|
+
...(this.llmOption.betas ?? []),
|
|
223
|
+
]),
|
|
205
224
|
];
|
|
206
225
|
const response: Response = await fetch(`${this.#host}/messages`, {
|
|
207
226
|
method: "POST",
|
|
@@ -80,6 +80,11 @@ export interface AgentWireMessage {
|
|
|
80
80
|
* provider that declares tools ahead can replay where each one was added or withdrawn.
|
|
81
81
|
*/
|
|
82
82
|
listed?: string[];
|
|
83
|
+
/**
|
|
84
|
+
* On an assistant message `agentService.loop` wrote: the definitions of the page's tools that answer listed first,
|
|
85
|
+
* or listed changed. A later run declares them to a provider that keeps thinking, lent by the page or not.
|
|
86
|
+
*/
|
|
87
|
+
lent?: AgentWireTool[];
|
|
83
88
|
}
|
|
84
89
|
|
|
85
90
|
/**
|
|
@@ -19,5 +19,5 @@ export declare class RotatingLogWriter {
|
|
|
19
19
|
static sanitizeFilePart(value: string): string;
|
|
20
20
|
write(processKey: string, chunk: string): void;
|
|
21
21
|
close(): Promise<void>;
|
|
22
|
-
static fileMode(value?: string | undefined, warn?: typeof RotatingLogWriter.__#
|
|
22
|
+
static fileMode(value?: string | undefined, warn?: typeof RotatingLogWriter.__#180@#defaultWarn): number;
|
|
23
23
|
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { AgentWireMessage, AgentWireTool } from "../predefinedAdaptor/llm.adaptor";
|
|
2
|
+
export declare class LoopLent {
|
|
3
|
+
#private;
|
|
4
|
+
lend(lent: readonly AgentWireTool[], messages: readonly AgentWireMessage[], free: (name: string) => boolean): void;
|
|
5
|
+
has(name: string): boolean;
|
|
6
|
+
get left(): readonly AgentWireTool[];
|
|
7
|
+
tracks(messages: readonly AgentWireMessage[]): boolean;
|
|
8
|
+
listingOf(tools: readonly AgentWireTool[], messages: readonly AgentWireMessage[]): {
|
|
9
|
+
lent?: AgentWireTool[] | undefined;
|
|
10
|
+
listed: string[];
|
|
11
|
+
};
|
|
12
|
+
static definition({ name, description, parameters }: AgentWireTool): AgentWireTool;
|
|
13
|
+
static stored(messages: readonly AgentWireMessage[]): AgentWireTool[];
|
|
14
|
+
static unlent(messages: AgentWireMessage[]): AgentWireMessage[];
|
|
15
|
+
static fresh(lent: readonly AgentWireTool[], messages: readonly AgentWireMessage[]): AgentWireTool[];
|
|
16
|
+
}
|
|
@@ -68,8 +68,15 @@ interface AnthropicAnswer {
|
|
|
68
68
|
usage?: AnthropicUsage;
|
|
69
69
|
}
|
|
70
70
|
type AnthropicThinkingReplay = "drop" | "strip";
|
|
71
|
+
export type AnthropicRequestBody = ReturnType<typeof AnthropicLlm.requestBody>;
|
|
72
|
+
export interface AnthropicLlmOption extends LlmOption {
|
|
73
|
+
/** Has the last word on each body, after the retries chose its form: a `cache_control` breakpoint, an `effort`. */
|
|
74
|
+
body?: (body: AnthropicRequestBody, request: LlmTurnRequest) => AnthropicRequestBody & Record<string, unknown>;
|
|
75
|
+
/** `anthropic-beta` values what `body` adds needs, sent beside the ones a retry adds. */
|
|
76
|
+
betas?: string[];
|
|
77
|
+
}
|
|
71
78
|
declare const AnthropicLlm_base: import("..").AdaptorCls<{}, {
|
|
72
|
-
llmOption: import("..").InjectInfo<"use",
|
|
79
|
+
llmOption: import("..").InjectInfo<"use", AnthropicLlmOption, never, never>;
|
|
73
80
|
}>;
|
|
74
81
|
/** Anthropic's Messages API, which reads images and PDFs. `model` is required and has no default. */
|
|
75
82
|
export declare class AnthropicLlm extends AnthropicLlm_base implements LlmAdaptor {
|
|
@@ -86,6 +93,11 @@ export declare class AnthropicLlm extends AnthropicLlm_base implements LlmAdapto
|
|
|
86
93
|
static readonly dialect = "anthropic";
|
|
87
94
|
/** The API refuses `thinking.block_binding` without it. */
|
|
88
95
|
static readonly thinkingBindingBeta = "thinking-binding-controls-2026-08-01";
|
|
96
|
+
/**
|
|
97
|
+
* One with settings of its own, beside `option.setLlm`: an app's adaptor sending some turns here under another key
|
|
98
|
+
* keeps the retries, and what they learned about each model and conversation, by keeping the instance.
|
|
99
|
+
*/
|
|
100
|
+
static of(option: AnthropicLlmOption): AnthropicLlm;
|
|
89
101
|
get limits(): LlmLimits;
|
|
90
102
|
get accepts(): LlmAccepts;
|
|
91
103
|
chat(request: LlmTurnRequest, onDelta?: (delta: string) => void, signal?: AbortSignal): Promise<LlmTurnAnswer | null>;
|
|
@@ -76,6 +76,11 @@ export interface AgentWireMessage {
|
|
|
76
76
|
* provider that declares tools ahead can replay where each one was added or withdrawn.
|
|
77
77
|
*/
|
|
78
78
|
listed?: string[];
|
|
79
|
+
/**
|
|
80
|
+
* On an assistant message `agentService.loop` wrote: the definitions of the page's tools that answer listed first,
|
|
81
|
+
* or listed changed. A later run declares them to a provider that keeps thinking, lent by the page or not.
|
|
82
|
+
*/
|
|
83
|
+
lent?: AgentWireTool[];
|
|
79
84
|
}
|
|
80
85
|
/**
|
|
81
86
|
* Anthropic's thinking blocks, signatures included, in answer order: replayed verbatim, the model keeps its reasoning
|