akanjs 3.3.0-rc.72 → 3.3.0-rc.74
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/constant/index.ts +1 -0
- package/constant/types.ts +10 -9
- package/dictionary/agent.dictionary.ts +4 -0
- package/fetch/client/fetchClient.ts +1 -1
- package/fetch/client/signalCatalog.ts +9 -4
- package/package.json +1 -1
- package/server/routing/apiRouter.ts +12 -1
- package/service/agent.service.ts +1 -0
- package/service/agentLoop/agentLoop.ts +23 -4
- package/service/agentLoop/loopCompaction.ts +5 -2
- package/service/agentLoop/loopContext.ts +5 -2
- package/service/agentLoop/loopToolkits.ts +24 -6
- package/service/agentLoop/loopTranscript.ts +1 -1
- package/service/predefinedAdaptor/anthropicLlm.ts +299 -50
- package/service/predefinedAdaptor/anthropicTools.ts +56 -0
- package/service/predefinedAdaptor/index.ts +1 -0
- package/service/predefinedAdaptor/llm.adaptor.ts +27 -0
- package/service/predefinedAdaptor/llmOverflow.ts +12 -0
- package/service/predefinedAdaptor/openaiDialect.ts +27 -13
- package/service/toolNames.ts +7 -1
- package/signal/endpointInfo.ts +2 -1
- package/signal/internalInfo.ts +2 -2
- package/types/constant/index.d.ts +1 -1
- package/types/constant/types.d.ts +6 -5
- package/types/dictionary/agent.dictionary.d.ts +1 -1
- package/types/dictionary/dictionary.d.ts +4 -4
- package/types/fetch/client/signalCatalog.d.ts +2 -1
- package/types/service/agent.service.d.ts +1 -0
- package/types/service/agentLoop/agentLoop.d.ts +2 -1
- package/types/service/agentLoop/loopCompaction.d.ts +1 -1
- package/types/service/agentLoop/loopToolkits.d.ts +2 -0
- package/types/service/predefinedAdaptor/anthropicLlm.d.ts +56 -17
- package/types/service/predefinedAdaptor/anthropicTools.d.ts +30 -0
- package/types/service/predefinedAdaptor/index.d.ts +1 -0
- package/types/service/predefinedAdaptor/llm.adaptor.d.ts +26 -0
- package/types/service/predefinedAdaptor/llmOverflow.d.ts +5 -0
- package/types/signal/agent.signal.d.ts +1 -0
- package/types/signal/endpointInfo.d.ts +2 -2
- package/types/signal/internalInfo.d.ts +2 -2
- package/types/ui/recipe/badgeRecipe.d.ts +2 -2
- package/types/ui/recipe/buttonRecipe.d.ts +2 -2
- package/types/ui/recipe/inputRecipe.d.ts +2 -2
- package/types/webkit/csrSlide.d.ts +1 -1
- package/types/webkit/useCsrRefs.d.ts +1 -1
package/constant/index.ts
CHANGED
package/constant/types.ts
CHANGED
|
@@ -4,30 +4,31 @@ export type QueryOf<_T = any> = any;
|
|
|
4
4
|
|
|
5
5
|
export type ConstantType = "input" | "object" | "full" | "light" | "insight" | "scalar";
|
|
6
6
|
|
|
7
|
-
type ObjectToId<O> = O extends BaseObject
|
|
7
|
+
type ObjectToId<O, Unset> = O extends BaseObject
|
|
8
8
|
? string
|
|
9
9
|
: O extends BaseObject[]
|
|
10
10
|
? string[]
|
|
11
11
|
: O extends Dayjs
|
|
12
12
|
? Dayjs
|
|
13
13
|
: O extends object
|
|
14
|
-
? DocumentModel<O>
|
|
14
|
+
? DocumentModel<O, Unset>
|
|
15
15
|
: O;
|
|
16
16
|
|
|
17
|
-
type Docify<T, _StateKeys extends keyof GetStateObject<T> = keyof GetStateObject<T>> = unknown extends T
|
|
17
|
+
type Docify<T, Unset, _StateKeys extends keyof GetStateObject<T> = keyof GetStateObject<T>> = unknown extends T
|
|
18
18
|
? T
|
|
19
|
-
: { [K in _StateKeys as null extends T[K] ? never : K]-?: ObjectToId<NonNullable<T[K]
|
|
20
|
-
[K in _StateKeys as null extends T[K] ? K : never]?: ObjectToId<NonNullable<T[K]
|
|
19
|
+
: { [K in _StateKeys as null extends T[K] ? never : K]-?: ObjectToId<NonNullable<T[K]>, Unset> } & {
|
|
20
|
+
[K in _StateKeys as null extends T[K] ? K : never]?: ObjectToId<NonNullable<T[K]>, Unset> | Unset | undefined;
|
|
21
21
|
};
|
|
22
|
-
export type DocumentModel<T> = unknown extends T
|
|
22
|
+
export type DocumentModel<T, Unset = undefined> = unknown extends T
|
|
23
23
|
? T
|
|
24
24
|
: T extends (infer S)[]
|
|
25
|
-
? DocumentModel<S>[]
|
|
25
|
+
? DocumentModel<S, Unset>[]
|
|
26
26
|
: T extends string | number | boolean | Dayjs | File | Uint8Array
|
|
27
27
|
? T
|
|
28
28
|
: T extends Map<infer K, infer V>
|
|
29
|
-
? Map<K, DocumentModel<V>>
|
|
30
|
-
: Docify<T>;
|
|
29
|
+
? Map<K, DocumentModel<V, Unset>>
|
|
30
|
+
: Docify<T, Unset>;
|
|
31
|
+
export type ServerReturnModel<T> = DocumentModel<T, null>;
|
|
31
32
|
|
|
32
33
|
export type ServerArgModel<T> = unknown extends T
|
|
33
34
|
? T
|
|
@@ -29,6 +29,10 @@ export const agentDictionary = serviceDictionary(["en", "ko"])
|
|
|
29
29
|
"{provider} refused this turn with status {status}",
|
|
30
30
|
"{provider}가 이번 턴을 거절했습니다 (status {status})",
|
|
31
31
|
],
|
|
32
|
+
llmDeclined: [
|
|
33
|
+
"The model at {provider} declined to answer this turn",
|
|
34
|
+
"{provider}의 모델이 이번 턴에 답하지 않겠다고 했습니다",
|
|
35
|
+
],
|
|
32
36
|
|
|
33
37
|
contextOverflow: [
|
|
34
38
|
"This conversation no longer fits the model's context window at {provider}. Use /compact to summarize it, or /new to start over",
|
|
@@ -368,7 +368,7 @@ export class FetchClient {
|
|
|
368
368
|
}
|
|
369
369
|
#makeHttpFn(key: string, endpoint: SerializedEndpoint, prefix?: string) {
|
|
370
370
|
const argLength = endpoint.args.length;
|
|
371
|
-
const serializerMap = makeArgSerializer(endpoint.args);
|
|
371
|
+
const serializerMap = makeArgSerializer(endpoint.args, this.ErrorCls);
|
|
372
372
|
const parseReturn = refusingUndeclaredNull(key, endpoint, makeReturnParser(endpoint.returns));
|
|
373
373
|
const { bodyArgs, uploadArgs } = FetchClient.classifyHttpArgs(endpoint.args);
|
|
374
374
|
const requestOf = (argData: unknown[]) => {
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { PrimitiveRegistry } from "akanjs/base";
|
|
2
2
|
import { ConstantRegistry, deserialize, serialize } from "akanjs/constant";
|
|
3
3
|
import type { SerializedArg, SerializedEndpoint, SerializedReturns, SerializedSignal } from "akanjs/signal";
|
|
4
|
+
import { type ErrorConstructor, restoreRemoteError } from "./remoteError";
|
|
4
5
|
|
|
5
6
|
interface SharedSignalRegistry {
|
|
6
7
|
signal: { [key: string]: SerializedSignal };
|
|
@@ -83,7 +84,7 @@ export const helpersOf = (signal: SerializedSignal) => {
|
|
|
83
84
|
const write = update || !!mountedGuards(signal, "create") || !!mountedGuards(signal, "remove");
|
|
84
85
|
return { view: get, edit: get && write, merge: update };
|
|
85
86
|
};
|
|
86
|
-
export const makeArgSerializer = (args: SerializedArg[]) => {
|
|
87
|
+
export const makeArgSerializer = (args: SerializedArg[], ErrorCls?: ErrorConstructor) => {
|
|
87
88
|
const serializerMap = new Map(
|
|
88
89
|
args.map((arg) => {
|
|
89
90
|
const argRef = arg.modelType
|
|
@@ -91,10 +92,14 @@ export const makeArgSerializer = (args: SerializedArg[]) => {
|
|
|
91
92
|
: PrimitiveRegistry.get(arg.refName);
|
|
92
93
|
return [
|
|
93
94
|
arg.name,
|
|
94
|
-
(value: unknown) =>
|
|
95
|
-
|
|
95
|
+
(value: unknown) => {
|
|
96
|
+
|
|
97
|
+
if (arg.type === "param" && (value ?? "") === "")
|
|
98
|
+
throw restoreRemoteError({ error: "base.error.requiredArgument", data: { name: arg.name } }, 400, ErrorCls);
|
|
99
|
+
return serialize(argRef, arg.arrDepth ?? 0, value, "input", {
|
|
96
100
|
nullable: arg.nullable ?? arg.type === "search",
|
|
97
|
-
})
|
|
101
|
+
});
|
|
102
|
+
},
|
|
98
103
|
] as const;
|
|
99
104
|
}),
|
|
100
105
|
);
|
package/package.json
CHANGED
|
@@ -117,7 +117,7 @@ export class ApiRouter {
|
|
|
117
117
|
},
|
|
118
118
|
...Object.fromEntries(endpointEntries),
|
|
119
119
|
...Object.fromEntries(builtinEntries),
|
|
120
|
-
...(renderEnvRoutes ?? {}),
|
|
120
|
+
...ApiRouter.#refuseUnknownApiPaths(renderEnvRoutes ?? {}, prefix),
|
|
121
121
|
} as NonNullHttpRoutes;
|
|
122
122
|
const proxied = webProxyRunner
|
|
123
123
|
? ApiRouter.#wrapRoutesWithWebProxy(routeTable, webProxyRunner, prefix, endpointPaths)
|
|
@@ -436,6 +436,17 @@ export class ApiRouter {
|
|
|
436
436
|
return path === normalized || path.startsWith(`${normalized}/`);
|
|
437
437
|
}
|
|
438
438
|
|
|
439
|
+
static #refuseUnknownApiPaths(routes: NonNullHttpRoutes, apiPrefix: string): NonNullHttpRoutes {
|
|
440
|
+
const catchAll = routes["/*"];
|
|
441
|
+
if (!catchAll || !apiPrefix.replace(/\/$/, "")) return routes;
|
|
442
|
+
const refused = ApiRouter.#mapRoute(catchAll, (handler) => (req) => {
|
|
443
|
+
const { pathname } = new URL(req.url);
|
|
444
|
+
if (!ApiRouter.#isApiRoute(pathname, apiPrefix)) return handler(req);
|
|
445
|
+
return SignalFailure.response(new AkanError("routeNotFound", `No endpoint at ${pathname}`, { statusCode: 404 }));
|
|
446
|
+
});
|
|
447
|
+
return { ...routes, "/*": refused } as NonNullHttpRoutes;
|
|
448
|
+
}
|
|
449
|
+
|
|
439
450
|
static #isInternalRenderRoute(path: string): boolean {
|
|
440
451
|
return path === "/__csr" || path === "/__rsc" || path.startsWith("/__rsc/") || path.startsWith("/_akan/");
|
|
441
452
|
}
|
package/service/agent.service.ts
CHANGED
|
@@ -51,6 +51,7 @@ export class AgentService extends serve("agent" as const, ({ plug }) => ({
|
|
|
51
51
|
stop: answer.stop,
|
|
52
52
|
...(answer.usage ? { usage: answer.usage } : {}),
|
|
53
53
|
...(answer.model ? { model: answer.model } : {}),
|
|
54
|
+
...(answer.providerContent ? { providerContent: answer.providerContent } : {}),
|
|
54
55
|
...(this.llm.limits ? { limits: this.llm.limits } : {}),
|
|
55
56
|
};
|
|
56
57
|
}
|
|
@@ -7,11 +7,14 @@ import { AgentRelayPolicy, type AgentTurnContext } from "../agentRelayPolicy";
|
|
|
7
7
|
import type { AgentTool, AgentToolsAdaptor } from "../predefinedAdaptor/agentTools.adaptor";
|
|
8
8
|
import type {
|
|
9
9
|
AgentWireMessage,
|
|
10
|
+
AgentWireProviderContent,
|
|
10
11
|
AgentWireToolCall,
|
|
11
12
|
AgentWireToolResult,
|
|
12
13
|
LlmTurnRequest,
|
|
13
14
|
LlmUsage,
|
|
14
15
|
} from "../predefinedAdaptor/llm.adaptor";
|
|
16
|
+
import { LlmContext } from "../predefinedAdaptor/llmContext";
|
|
17
|
+
import { LlmFence } from "../predefinedAdaptor/llmFence";
|
|
15
18
|
import type {
|
|
16
19
|
AgentLoopApproval,
|
|
17
20
|
AgentLoopCallAnswer,
|
|
@@ -37,6 +40,7 @@ export interface AgentLoopAnswer {
|
|
|
37
40
|
stop: "end" | "toolUse" | "length";
|
|
38
41
|
usage?: LlmUsage;
|
|
39
42
|
model?: string;
|
|
43
|
+
providerContent?: AgentWireProviderContent;
|
|
40
44
|
}
|
|
41
45
|
|
|
42
46
|
export interface AgentLoopPort {
|
|
@@ -147,19 +151,23 @@ export class AgentLoop {
|
|
|
147
151
|
|
|
148
152
|
async *#runRound(): Events<AgentLoopStop | null> {
|
|
149
153
|
const listed = await this.#listTools();
|
|
154
|
+
await this.#pinContext();
|
|
150
155
|
const compacted = yield* this.#compact(listed);
|
|
151
156
|
const { answer, tools } = yield* this.#ask(compacted ?? listed);
|
|
152
|
-
|
|
157
|
+
|
|
153
158
|
const mark = this.#tainted ? { tainted: true } : {};
|
|
159
|
+
const shown = this.#toolkits.tracksListing ? { listed: tools.map((tool) => tool.name) } : {};
|
|
154
160
|
if (answer.stop === "length") {
|
|
155
161
|
|
|
156
|
-
if (answer.text) await this.#append({ role: "assistant", text: answer.text, ...mark });
|
|
162
|
+
if (answer.text) await this.#append({ role: "assistant", text: answer.text, ...shown, ...mark });
|
|
157
163
|
return "length";
|
|
158
164
|
}
|
|
159
165
|
const message: AgentWireMessage = {
|
|
160
166
|
role: "assistant",
|
|
161
167
|
...(answer.text ? { text: answer.text } : {}),
|
|
162
168
|
...(answer.toolCalls.length ? { toolCalls: answer.toolCalls } : {}),
|
|
169
|
+
...(answer.providerContent ? { providerContent: answer.providerContent } : {}),
|
|
170
|
+
...shown,
|
|
163
171
|
...mark,
|
|
164
172
|
};
|
|
165
173
|
if (LoopTranscript.carries(message)) await this.#append(message);
|
|
@@ -220,8 +228,16 @@ export class AgentLoop {
|
|
|
220
228
|
};
|
|
221
229
|
}
|
|
222
230
|
|
|
231
|
+
async #pinContext() {
|
|
232
|
+
const { context = [] } = this.#request;
|
|
233
|
+
const text = LlmContext.of(context);
|
|
234
|
+
if (!text || this.#messages.findLast((message) => message.context)?.text === text) return;
|
|
235
|
+
await this.#append({ role: "user", text, context: true, ...(LlmFence.marks(context) ? { tainted: true } : {}) });
|
|
236
|
+
}
|
|
237
|
+
|
|
223
238
|
async *#ask(listed: AgentTool[]): Events<{ answer: AgentLoopAnswer; tools: AgentTool[] }> {
|
|
224
239
|
const preamble = this.#request.preamble ?? false;
|
|
240
|
+
await this.#pinContext();
|
|
225
241
|
const request = this.#turnOf(listed);
|
|
226
242
|
let tools = listed;
|
|
227
243
|
try {
|
|
@@ -234,21 +250,24 @@ export class AgentLoop {
|
|
|
234
250
|
if (!compacted) throw error;
|
|
235
251
|
tools = compacted;
|
|
236
252
|
}
|
|
253
|
+
await this.#pinContext();
|
|
237
254
|
return { answer: yield* this.#metered(this.#turnOf(tools), preamble, true), tools };
|
|
238
255
|
}
|
|
239
256
|
|
|
240
257
|
#turnOf(tools: AgentTool[]): LlmTurnRequest {
|
|
241
|
-
const {
|
|
258
|
+
const { model, preamble } = this.#request;
|
|
242
259
|
const instructions = [
|
|
243
260
|
this.#request.instructions || (preamble ? "" : AgentLoop.noInstructions),
|
|
244
261
|
...this.#toolkits.pinnedInstructions(),
|
|
245
262
|
]
|
|
246
263
|
.filter(Boolean)
|
|
247
264
|
.join("\n\n");
|
|
265
|
+
const deferred = this.#toolkits.deferred(tools).map(({ readOnly: _readOnly, ...tool }) => tool);
|
|
248
266
|
return {
|
|
249
267
|
messages: LoopTranscript.wire(this.#messages),
|
|
250
268
|
tools: tools.map(({ readOnly: _readOnly, ...tool }) => tool),
|
|
251
|
-
context,
|
|
269
|
+
context: [],
|
|
270
|
+
...(deferred.length ? { deferred } : {}),
|
|
252
271
|
...(instructions ? { instructions } : {}),
|
|
253
272
|
...(model ? { model } : {}),
|
|
254
273
|
};
|
|
@@ -20,7 +20,7 @@ export class LoopCompaction {
|
|
|
20
20
|
return Math.ceil(chars / 4);
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
static frameTokensOf({ messages: _messages, ...frame }: LlmTurnRequest): number {
|
|
23
|
+
static frameTokensOf({ messages: _messages, deferred: _deferred, ...frame }: LlmTurnRequest): number {
|
|
24
24
|
return Math.ceil(JSON.stringify(frame).length / 4);
|
|
25
25
|
}
|
|
26
26
|
|
|
@@ -80,7 +80,10 @@ export class LoopCompaction {
|
|
|
80
80
|
const previous = messages
|
|
81
81
|
.filter((message) => message.summary)
|
|
82
82
|
.map((message) => `previous summary:\n${message.text ?? ""}`);
|
|
83
|
-
|
|
83
|
+
|
|
84
|
+
const lines = messages
|
|
85
|
+
.filter((message) => !message.summary && !message.context)
|
|
86
|
+
.map((message) => LoopCompaction.#line(message));
|
|
84
87
|
return [...previous, LoopCompaction.#fit(lines, budget)].filter(Boolean).join("\n");
|
|
85
88
|
}
|
|
86
89
|
|
|
@@ -16,8 +16,11 @@ export class LoopContext {
|
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
static carried(request: AgentLoopRequest, messages: readonly AgentWireMessage[]): boolean {
|
|
19
|
-
if (LoopContext.marked(request)
|
|
20
|
-
|
|
19
|
+
if (LoopContext.marked(request)) return true;
|
|
20
|
+
if (messages.some((message) => !!message.references?.length || (message.context && message.tainted))) return true;
|
|
21
|
+
const first = messages.find(
|
|
22
|
+
({ role, summary, tainted, context }) => !context && (role === "tool" || summary || tainted),
|
|
23
|
+
);
|
|
21
24
|
return first?.role === "assistant" && !first.summary;
|
|
22
25
|
}
|
|
23
26
|
|
|
@@ -65,9 +65,19 @@ export class LoopToolkits {
|
|
|
65
65
|
this.#unlist(over, loaded);
|
|
66
66
|
({ shown } = this.#arrange());
|
|
67
67
|
}
|
|
68
|
-
const
|
|
69
|
-
const searchable = this.#search
|
|
70
|
-
return [...shown, ...(
|
|
68
|
+
const offered = this.#offered();
|
|
69
|
+
const searchable = this.#search ? [this.#search.tool()] : [];
|
|
70
|
+
return [...shown, ...(offered.length ? [this.#metaTool(offered)] : []), ...searchable];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
deferred(listed: readonly AgentTool[]): AgentTool[] {
|
|
74
|
+
if (!this.#any) return [];
|
|
75
|
+
const names = new Set(listed.map((tool) => tool.name));
|
|
76
|
+
return this.#visible.filter((tool) => this.#inKit(tool.name) && !names.has(tool.name));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
get tracksListing(): boolean {
|
|
80
|
+
return this.#any;
|
|
71
81
|
}
|
|
72
82
|
|
|
73
83
|
#arrange(): Arranged {
|
|
@@ -246,6 +256,14 @@ export class LoopToolkits {
|
|
|
246
256
|
return this.#visible.filter((tool) => tools.includes(tool.name)).map((tool) => tool.name);
|
|
247
257
|
}
|
|
248
258
|
|
|
259
|
+
#offered(): string[] {
|
|
260
|
+
const pinned = (kit: string) => this.#pinned.some((entry) => entry.kind === "kit" && entry.name === kit);
|
|
261
|
+
return [
|
|
262
|
+
...[...this.#kits.keys()].filter((kit) => !pinned(kit) && this.#toolsOf(kit).length),
|
|
263
|
+
...this.#external.keys(),
|
|
264
|
+
];
|
|
265
|
+
}
|
|
266
|
+
|
|
249
267
|
#closed(): string[] {
|
|
250
268
|
const partial = (kit: string) => this.#toolsOf(kit).some((tool) => this.#evicted.has(tool));
|
|
251
269
|
return [
|
|
@@ -256,8 +274,8 @@ export class LoopToolkits {
|
|
|
256
274
|
];
|
|
257
275
|
}
|
|
258
276
|
|
|
259
|
-
#metaTool(
|
|
260
|
-
const lines =
|
|
277
|
+
#metaTool(offered: string[]): AgentTool {
|
|
278
|
+
const lines = offered.map((kit) =>
|
|
261
279
|
`- ${kit}: ${this.#kits.get(kit)?.description ?? this.#external.get(kit) ?? ""}`.trimEnd(),
|
|
262
280
|
);
|
|
263
281
|
return {
|
|
@@ -269,7 +287,7 @@ export class LoopToolkits {
|
|
|
269
287
|
].join("\n"),
|
|
270
288
|
parameters: {
|
|
271
289
|
type: "object",
|
|
272
|
-
properties: { name: { type: "string", enum:
|
|
290
|
+
properties: { name: { type: "string", enum: offered } },
|
|
273
291
|
required: ["name"],
|
|
274
292
|
additionalProperties: false,
|
|
275
293
|
},
|
|
@@ -13,7 +13,7 @@ export class LoopTranscript {
|
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
static seen(messages: readonly AgentWireMessage[]): AgentWireMessage[] {
|
|
16
|
-
const latest = messages.findLastIndex((message) => message.role === "user" && !message.summary);
|
|
16
|
+
const latest = messages.findLastIndex((message) => message.role === "user" && !message.summary && !message.context);
|
|
17
17
|
if (latest <= 0) return [...messages];
|
|
18
18
|
return messages.map((message, at) =>
|
|
19
19
|
at < latest && message.toolResults?.some(LoopTranscript.#carriesFile)
|