agent-accelerator 0.2.0 → 0.4.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/README.md +94 -14
- package/package.json +1 -1
- package/src/agent/agent.ts +1010 -104
- package/src/agent/context.ts +35 -0
- package/src/agent/delegation.ts +758 -40
- package/src/agent/loop.ts +811 -102
- package/src/agent/subagent.ts +59 -0
- package/src/index.ts +186 -59
- package/src/models/catalog-cache.ts +68 -25
- package/src/models/catalog.ts +101 -21
- package/src/providers/google.ts +99 -39
- package/src/providers/openai-compat.ts +140 -80
- package/src/providers/openai.ts +149 -69
- package/src/providers/openrouter.ts +106 -71
- package/src/providers/registry.ts +16 -12
- package/src/providers/shared.ts +132 -0
- package/src/providers.ts +14 -13
- package/src/session/store.ts +1285 -0
- package/src/streaming/event-stream.ts +156 -41
- package/src/tools/executor.ts +252 -16
- package/src/tools/tool.ts +147 -3
- package/src/types/agent.ts +253 -2
- package/src/types/core.ts +2 -1
- package/src/types/errors.ts +133 -0
- package/src/types/model.ts +1 -1
- package/src/types/response.ts +150 -3
- package/src/types/tool.ts +71 -0
- package/src/update-models.ts +8 -2
- package/src/utils/cache.ts +68 -1
- package/src/utils/env.ts +2 -6
- package/src/utils/headers.ts +153 -19
- package/src/utils/retry.ts +27 -13
- package/src/utils/session.ts +320 -0
- package/src/providers/openrouter-responses.ts +0 -949
- package/src/types/provider-payloads.ts +0 -352
package/src/utils/retry.ts
CHANGED
|
@@ -1,23 +1,19 @@
|
|
|
1
1
|
import { toConciseProviderError } from "./errors.ts";
|
|
2
|
+
import { isAbortError } from "../types/errors.ts";
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
* Checks whether a provider error is a cancellation (never retried).
|
|
5
|
-
*/
|
|
6
|
-
export function isAbortError(error: unknown): boolean {
|
|
7
|
-
const err = error as { name?: unknown; message?: unknown } | null;
|
|
8
|
-
if (!err || typeof err !== "object") return false;
|
|
9
|
-
if ((err as { name?: unknown }).name === "AbortError") return true;
|
|
10
|
-
return /abort|cancell?ed/i.test(String((err as { message?: unknown }).message ?? error));
|
|
11
|
-
}
|
|
4
|
+
export { isAbortError };
|
|
12
5
|
|
|
13
6
|
/**
|
|
14
7
|
* Checks whether a provider error is transient and safe to retry.
|
|
8
|
+
* Structured-data only: status / code / transient flag. Never message text,
|
|
9
|
+
* so a 400 mentioning "512 tokens" is not retried (Q-24).
|
|
15
10
|
*/
|
|
16
11
|
export function isTransientError(error: unknown): boolean {
|
|
17
12
|
if (!error) return false;
|
|
18
13
|
if (isAbortError(error)) return false;
|
|
19
14
|
const err = error as Record<string, unknown>;
|
|
20
15
|
if (err["transient"] === true) return true;
|
|
16
|
+
if ((err as { retryable?: unknown }).retryable === true) return true;
|
|
21
17
|
const status = Number(
|
|
22
18
|
(err["status"] as number | undefined) ??
|
|
23
19
|
(err["statusCode"] as number | undefined) ??
|
|
@@ -30,9 +26,19 @@ export function isTransientError(error: unknown): boolean {
|
|
|
30
26
|
if (["ECONNRESET", "ECONNREFUSED", "EAI_AGAIN", "UND_ERR_CONNECT_TIMEOUT", "EPIPE", "ETIMEDOUT"].includes(code)) {
|
|
31
27
|
return true;
|
|
32
28
|
}
|
|
33
|
-
return
|
|
34
|
-
|
|
35
|
-
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function readRetryAfterMs(error: unknown): number | undefined {
|
|
33
|
+
const err = error as Record<string, unknown>;
|
|
34
|
+
const raw =
|
|
35
|
+
(err["retryAfterMs"] as unknown) ??
|
|
36
|
+
((err["response"] as Record<string, unknown> | undefined)?.["headers"] as Record<string, unknown> | undefined)?.["retry-after"] ??
|
|
37
|
+
((err["headers"] as Record<string, unknown> | undefined)?.["retry-after"]);
|
|
38
|
+
if (raw === undefined) return undefined;
|
|
39
|
+
const secs = Number(Array.isArray(raw) ? raw[0] : raw);
|
|
40
|
+
if (!Number.isFinite(secs) || secs < 0) return undefined;
|
|
41
|
+
return Math.min(60_000, secs * 1000);
|
|
36
42
|
}
|
|
37
43
|
|
|
38
44
|
function sleep(ms: number, signal?: AbortSignal): Promise<void> {
|
|
@@ -56,6 +62,8 @@ function sleep(ms: number, signal?: AbortSignal): Promise<void> {
|
|
|
56
62
|
|
|
57
63
|
/**
|
|
58
64
|
* Runs a provider call with bounded retries for transient failures.
|
|
65
|
+
* Honors Retry-After up to 60s cap. Streams retry only before first byte
|
|
66
|
+
* (callers must not retry after partial output).
|
|
59
67
|
*/
|
|
60
68
|
export async function withRetries<T>(
|
|
61
69
|
fn: () => Promise<T> | PromiseLike<T>,
|
|
@@ -64,10 +72,13 @@ export async function withRetries<T>(
|
|
|
64
72
|
maxRetryDelayMs?: number;
|
|
65
73
|
signal?: AbortSignal;
|
|
66
74
|
label?: { providerId?: string; modelId?: string };
|
|
75
|
+
retryBudgetMs?: number;
|
|
67
76
|
}
|
|
68
77
|
): Promise<T> {
|
|
69
78
|
const maxRetries = options?.maxRetries ?? 2;
|
|
70
79
|
const maxDelay = options?.maxRetryDelayMs ?? 5000;
|
|
80
|
+
const budgetMs = options?.retryBudgetMs ?? 30_000;
|
|
81
|
+
const started = Date.now();
|
|
71
82
|
const fail = (error: unknown): never => {
|
|
72
83
|
if (options?.label?.providerId && options?.label?.modelId) {
|
|
73
84
|
throw toConciseProviderError(error, options.label.providerId, options.label.modelId);
|
|
@@ -81,7 +92,10 @@ export async function withRetries<T>(
|
|
|
81
92
|
} catch (error) {
|
|
82
93
|
if (options?.signal?.aborted || isAbortError(error)) throw error;
|
|
83
94
|
if (attempt >= maxRetries || !isTransientError(error)) fail(error);
|
|
84
|
-
|
|
95
|
+
if (Date.now() - started >= budgetMs) fail(error);
|
|
96
|
+
const retryAfter = readRetryAfterMs(error);
|
|
97
|
+
const backoff = Math.min(maxDelay, 250 * 2 ** attempt + Math.random() * 100);
|
|
98
|
+
const delay = retryAfter !== undefined ? Math.min(maxDelay, Math.max(backoff, retryAfter)) : backoff;
|
|
85
99
|
attempt += 1;
|
|
86
100
|
await sleep(delay, options?.signal);
|
|
87
101
|
}
|
package/src/utils/session.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
import { clampCacheKey } from "./cache.ts";
|
|
2
|
+
import type { ThinkingLevel } from "../types/core.ts";
|
|
3
|
+
import type { MidSessionMode } from "../types/agent.ts";
|
|
4
|
+
import type { AgentConfig } from "../types/agent.ts";
|
|
5
|
+
import type { ModelSpec } from "../types/model.ts";
|
|
6
|
+
import type { ModelProviderInstance } from "../providers/registry.ts";
|
|
7
|
+
import type { ContentPart } from "../types/message.ts";
|
|
8
|
+
import { ConfigError } from "../types/errors.ts";
|
|
2
9
|
|
|
3
10
|
/**
|
|
4
11
|
* Creates or formats a unique session ID for prompt caching affinity (clamped to 64 chars like agent-accel)
|
|
@@ -24,3 +31,316 @@ export function createSessionId(prefix = "accel"): string {
|
|
|
24
31
|
}
|
|
25
32
|
return clampCacheKey(id) || id;
|
|
26
33
|
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Short stable hash (FNV-1a, 8 hex chars) for embedding parent lineage
|
|
37
|
+
* into truncated child session IDs without extra dependencies.
|
|
38
|
+
*/
|
|
39
|
+
export function hashSessionPart(value: string): string {
|
|
40
|
+
let h = 0x811c9dc5;
|
|
41
|
+
for (let i = 0; i < value.length; i++) {
|
|
42
|
+
h ^= value.charCodeAt(i);
|
|
43
|
+
h = Math.imul(h, 0x01000193);
|
|
44
|
+
}
|
|
45
|
+
return (h >>> 0).toString(16).padStart(8, "0");
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* System-generated 32-char TrackingID for one sub-agent (lowercase hex, no
|
|
50
|
+
* dashes). Surfaced as the suffix in `SUBAGENT-NAME-{TrackingID}` display ids
|
|
51
|
+
* so `agent.track(id)` can address one worker among many concurrent ones.
|
|
52
|
+
*/
|
|
53
|
+
export function createTrackingId(): string {
|
|
54
|
+
try {
|
|
55
|
+
const c = (globalThis as any)?.crypto;
|
|
56
|
+
if (c && typeof c.randomUUID === "function") {
|
|
57
|
+
return String(c.randomUUID()).replace(/-/g, "").slice(0, 32);
|
|
58
|
+
}
|
|
59
|
+
} catch {}
|
|
60
|
+
try {
|
|
61
|
+
const nodeCrypto: any = (globalThis as any)?.process?.getBuiltinModule?.("node:crypto") ?? null;
|
|
62
|
+
if (nodeCrypto?.randomUUID) {
|
|
63
|
+
return String(nodeCrypto.randomUUID()).replace(/-/g, "").slice(0, 32);
|
|
64
|
+
}
|
|
65
|
+
} catch {}
|
|
66
|
+
let out = "";
|
|
67
|
+
while (out.length < 32) out += Math.random().toString(16).slice(2);
|
|
68
|
+
return out.slice(0, 32);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Monotonic clock for measuring durations (Q-42). Uses `performance.now()`
|
|
73
|
+
* when available, falling back to `Date.now()`. Differences on the same
|
|
74
|
+
* clock are valid durations; never treat the value as wall-clock epoch.
|
|
75
|
+
*
|
|
76
|
+
* @example `const start = nowMs(); ...; const durationMs = nowMs() - start;`
|
|
77
|
+
*/
|
|
78
|
+
export function nowMs(): number {
|
|
79
|
+
try {
|
|
80
|
+
if (typeof performance !== "undefined" && typeof performance.now === "function") {
|
|
81
|
+
return performance.now();
|
|
82
|
+
}
|
|
83
|
+
} catch {}
|
|
84
|
+
return Date.now();
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function random32Hex(): string {
|
|
88
|
+
try {
|
|
89
|
+
const c = (globalThis as any)?.crypto;
|
|
90
|
+
if (c && typeof c.randomUUID === "function") {
|
|
91
|
+
return String(c.randomUUID()).replace(/-/g, "").slice(0, 32);
|
|
92
|
+
}
|
|
93
|
+
} catch {}
|
|
94
|
+
let out = "";
|
|
95
|
+
while (out.length < 32) out += Math.random().toString(16).slice(2);
|
|
96
|
+
return out.slice(0, 32);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* New stable run id: 32 lowercase hex chars (crypto.randomUUID, no dashes) (Q-42).
|
|
101
|
+
* Opaque lineage token attached via `(metadata as any).runId`.
|
|
102
|
+
*/
|
|
103
|
+
export function newRunId(): string {
|
|
104
|
+
return random32Hex();
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* New stable turn id: 32 lowercase hex chars (crypto.randomUUID, no dashes) (Q-42).
|
|
109
|
+
* Opaque lineage token attached via `(metadata as any).turnId`.
|
|
110
|
+
*/
|
|
111
|
+
export function newTurnId(): string {
|
|
112
|
+
return random32Hex();
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Single canonical prompt preview (Q-49): merges the former
|
|
117
|
+
* `previewPrompt` (agent.ts) / `steerPreview` (loop.ts) duplicates.
|
|
118
|
+
* Bounded to 500 chars, display-only. Other modules own their copies;
|
|
119
|
+
* new code should import this one.
|
|
120
|
+
*
|
|
121
|
+
* @example `previewPrompt("hello")`
|
|
122
|
+
*/
|
|
123
|
+
export function previewPrompt(prompt: string | ContentPart[]): string {
|
|
124
|
+
if (typeof prompt === "string") return prompt.slice(0, 500);
|
|
125
|
+
try {
|
|
126
|
+
const texts = (prompt as ContentPart[])
|
|
127
|
+
.filter((p) => (p as { type?: unknown }).type === "text")
|
|
128
|
+
.map((p) => (p as { text?: string }).text ?? "")
|
|
129
|
+
.join("\n");
|
|
130
|
+
return (texts || "[multipart]").slice(0, 500);
|
|
131
|
+
} catch {
|
|
132
|
+
return "[multipart]";
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Parsed model reference returned by {@link parseModelRef} (Q-48). */
|
|
137
|
+
export interface ParsedModelRef {
|
|
138
|
+
provider: string;
|
|
139
|
+
modelId: string;
|
|
140
|
+
apiKey?: string;
|
|
141
|
+
baseUrl?: string;
|
|
142
|
+
thinking?: ThinkingLevel;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function splitModelString(raw: string): { provider: string; modelId: string } {
|
|
146
|
+
const trimmed = raw.trim();
|
|
147
|
+
const slash = trimmed.indexOf("/");
|
|
148
|
+
if (slash <= 0 || slash >= trimmed.length - 1) {
|
|
149
|
+
return { provider: "openrouter", modelId: trimmed };
|
|
150
|
+
}
|
|
151
|
+
return {
|
|
152
|
+
provider: trimmed.slice(0, slash).toLowerCase(),
|
|
153
|
+
modelId: trimmed.slice(slash + 1),
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function isModelProviderInstance(v: unknown): v is ModelProviderInstance {
|
|
158
|
+
if (!v || typeof v !== "object" || Array.isArray(v)) return false;
|
|
159
|
+
const r = v as Record<string, unknown>;
|
|
160
|
+
return typeof r["model"] === "string" && (r["model"] as string).length > 0;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function isModelSpec(v: unknown): v is ModelSpec {
|
|
164
|
+
if (!v || typeof v !== "object" || Array.isArray(v)) return false;
|
|
165
|
+
const r = v as Record<string, unknown>;
|
|
166
|
+
return typeof r["provider"] === "string" && typeof r["id"] === "string";
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Single model-ref parser (Q-48). Table-driven, no env sniffing:
|
|
171
|
+
* - string `"provider/model"` → split on first `/`; bare ids default to `openrouter`.
|
|
172
|
+
* - `ModelProviderInstance` (`{ model, apiKey?, baseUrl?, thinkingLevel? }`) → parse `model`, passthrough creds.
|
|
173
|
+
* - `ModelSpec` (`{ provider, id }`) → direct mapping.
|
|
174
|
+
*
|
|
175
|
+
* @example `parseModelRef("google/gemini-3.5-flash-lite")`
|
|
176
|
+
*/
|
|
177
|
+
export function parseModelRef(input: string | ModelSpec | ModelProviderInstance): ParsedModelRef {
|
|
178
|
+
if (typeof input === "string") {
|
|
179
|
+
if (!input.trim()) throw new ConfigError("parseModelRef: model string must be non-empty.");
|
|
180
|
+
const { provider, modelId } = splitModelString(input);
|
|
181
|
+
return { provider, modelId };
|
|
182
|
+
}
|
|
183
|
+
if (isModelProviderInstance(input)) {
|
|
184
|
+
const { provider, modelId } = splitModelString(input.model);
|
|
185
|
+
const out: ParsedModelRef = { provider, modelId };
|
|
186
|
+
if (input.apiKey !== undefined) out.apiKey = input.apiKey;
|
|
187
|
+
if (input.baseUrl !== undefined) out.baseUrl = input.baseUrl;
|
|
188
|
+
if (input.thinkingLevel !== undefined) out.thinking = input.thinkingLevel;
|
|
189
|
+
return out;
|
|
190
|
+
}
|
|
191
|
+
if (isModelSpec(input)) {
|
|
192
|
+
return { provider: String(input.provider), modelId: String(input.id) };
|
|
193
|
+
}
|
|
194
|
+
throw new ConfigError("parseModelRef: expected string | ModelSpec | ModelProviderInstance.");
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** Validated, readonly agent configuration returned by {@link resolveAgentConfig} (Q-32). */
|
|
198
|
+
export interface ResolvedAgentConfig {
|
|
199
|
+
readonly model: NonNullable<AgentConfig["model"]>;
|
|
200
|
+
readonly thinkingLevel?: ThinkingLevel;
|
|
201
|
+
readonly maxTurns: number;
|
|
202
|
+
readonly midSession: { readonly mode: MidSessionMode; readonly maxQueued: number };
|
|
203
|
+
readonly toolNames: readonly string[];
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const VALID_THINKING_LEVELS: readonly ThinkingLevel[] = [
|
|
207
|
+
"none",
|
|
208
|
+
"dynamic",
|
|
209
|
+
"minimal",
|
|
210
|
+
"low",
|
|
211
|
+
"medium",
|
|
212
|
+
"high",
|
|
213
|
+
"xhigh",
|
|
214
|
+
"max",
|
|
215
|
+
];
|
|
216
|
+
|
|
217
|
+
const VALID_MIDSESSION_MODES: readonly MidSessionMode[] = ["steer", "queue", "auto"];
|
|
218
|
+
|
|
219
|
+
/** Local name normalization mirroring `normalizeToolName` (no import cycle, Q-32). */
|
|
220
|
+
function normalizeConfigToolName(name: string): string {
|
|
221
|
+
return name
|
|
222
|
+
.trim()
|
|
223
|
+
.replace(/^(?:functions?|tools?)\./i, "")
|
|
224
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1_$2")
|
|
225
|
+
.replace(/[\s-]+/g, "_")
|
|
226
|
+
.toLowerCase();
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Single config resolution (Q-32). Validates `model`, `thinkingLevel`,
|
|
231
|
+
* `maxTurns`, `midSession`, and tool-name collisions, collecting EVERY issue
|
|
232
|
+
* and throwing one `ConfigError` listing all of them. Not wired into `Agent`
|
|
233
|
+
* (other owner) — call it from setup paths that want fail-fast validation.
|
|
234
|
+
*
|
|
235
|
+
* @example `const cfg = resolveAgentConfig({ model, maxTurns: 10 });`
|
|
236
|
+
*/
|
|
237
|
+
export function resolveAgentConfig(config: AgentConfig): Readonly<ResolvedAgentConfig> {
|
|
238
|
+
const issues: string[] = [];
|
|
239
|
+
const cfg = (config ?? {}) as AgentConfig;
|
|
240
|
+
|
|
241
|
+
let model: NonNullable<AgentConfig["model"]> | undefined;
|
|
242
|
+
const rawModel = cfg.model;
|
|
243
|
+
if (typeof rawModel === "string") {
|
|
244
|
+
if (rawModel.trim()) model = rawModel;
|
|
245
|
+
else issues.push("model: must be a non-empty string | ModelSpec | ModelProviderInstance.");
|
|
246
|
+
} else if (rawModel && typeof rawModel === "object") {
|
|
247
|
+
model = rawModel as NonNullable<AgentConfig["model"]>;
|
|
248
|
+
} else {
|
|
249
|
+
issues.push("model: is required (string | ModelSpec | ModelProviderInstance).");
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
let thinkingLevel: ThinkingLevel | undefined;
|
|
253
|
+
if (cfg.thinkingLevel !== undefined) {
|
|
254
|
+
if ((VALID_THINKING_LEVELS as readonly string[]).includes(cfg.thinkingLevel)) {
|
|
255
|
+
thinkingLevel = cfg.thinkingLevel;
|
|
256
|
+
} else {
|
|
257
|
+
issues.push(
|
|
258
|
+
`thinkingLevel: invalid '${cfg.thinkingLevel}'. Expected one of: ${VALID_THINKING_LEVELS.join(", ")}.`
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
let maxTurns = Infinity;
|
|
264
|
+
if (cfg.maxTurns !== undefined) {
|
|
265
|
+
const m = cfg.maxTurns;
|
|
266
|
+
if (m === 0) {
|
|
267
|
+
maxTurns = Infinity;
|
|
268
|
+
} else if (m === Infinity) {
|
|
269
|
+
maxTurns = Infinity;
|
|
270
|
+
} else if (typeof m === "number" && Number.isFinite(m) && m >= 1) {
|
|
271
|
+
maxTurns = Math.floor(m);
|
|
272
|
+
} else {
|
|
273
|
+
issues.push(`maxTurns: must be finite >= 1 or Infinity (0/omitted means unlimited). Received: ${String(m)}.`);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
let mode: MidSessionMode = "auto";
|
|
278
|
+
if (cfg.midSession?.mode !== undefined) {
|
|
279
|
+
if ((VALID_MIDSESSION_MODES as readonly string[]).includes(cfg.midSession.mode as string)) {
|
|
280
|
+
mode = cfg.midSession.mode as MidSessionMode;
|
|
281
|
+
} else {
|
|
282
|
+
issues.push(
|
|
283
|
+
`midSession.mode: invalid '${String(cfg.midSession.mode)}'. Expected one of: ${VALID_MIDSESSION_MODES.join(", ")}.`
|
|
284
|
+
);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
let maxQueued = 20;
|
|
288
|
+
if (cfg.midSession?.maxQueued !== undefined) {
|
|
289
|
+
const q = cfg.midSession.maxQueued;
|
|
290
|
+
if (typeof q === "number" && Number.isFinite(q) && Math.floor(q) === q && q >= 0) {
|
|
291
|
+
maxQueued = q;
|
|
292
|
+
} else {
|
|
293
|
+
issues.push(`midSession.maxQueued: must be an integer >= 0. Received: ${String(q)}.`);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
const toolNames: string[] = [];
|
|
298
|
+
const tools = cfg.tools;
|
|
299
|
+
if (tools !== undefined) {
|
|
300
|
+
const entries: Array<[string, string]> = [];
|
|
301
|
+
if (Array.isArray(tools)) {
|
|
302
|
+
for (const def of tools) {
|
|
303
|
+
const declared = (def as { name?: unknown })?.name;
|
|
304
|
+
if (typeof declared === "string" && declared.trim()) {
|
|
305
|
+
toolNames.push(declared);
|
|
306
|
+
entries.push([declared, declared]);
|
|
307
|
+
} else {
|
|
308
|
+
issues.push("tools: array entries must declare a non-empty `name`.");
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
} else if (tools && typeof tools === "object") {
|
|
312
|
+
for (const [key, def] of Object.entries(tools)) {
|
|
313
|
+
toolNames.push(key);
|
|
314
|
+
const declared = (def as { name?: unknown })?.name;
|
|
315
|
+
entries.push([key, typeof declared === "string" && declared ? declared : key]);
|
|
316
|
+
}
|
|
317
|
+
} else {
|
|
318
|
+
issues.push("tools: must be a record or array of ToolDefinitions.");
|
|
319
|
+
}
|
|
320
|
+
const seen = new Map<string, string>();
|
|
321
|
+
for (const [key, declared] of entries) {
|
|
322
|
+
for (const candidate of new Set([key, declared])) {
|
|
323
|
+
const normalized = normalizeConfigToolName(String(candidate));
|
|
324
|
+
const prev = seen.get(normalized);
|
|
325
|
+
if (prev !== undefined && prev !== key) {
|
|
326
|
+
issues.push(
|
|
327
|
+
`tools: name collision: '${prev}' and '${key}' both normalize to '${normalized}'. Rename one tool.`
|
|
328
|
+
);
|
|
329
|
+
} else if (prev === undefined) {
|
|
330
|
+
seen.set(normalized, key);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
if (issues.length > 0) {
|
|
337
|
+
throw new ConfigError(`Invalid Agent config (${issues.length} issue${issues.length === 1 ? "" : "s"}): ${issues.join(" ")}`);
|
|
338
|
+
}
|
|
339
|
+
return {
|
|
340
|
+
model: model as NonNullable<AgentConfig["model"]>,
|
|
341
|
+
...(thinkingLevel !== undefined ? { thinkingLevel } : {}),
|
|
342
|
+
maxTurns,
|
|
343
|
+
midSession: { mode, maxQueued },
|
|
344
|
+
toolNames,
|
|
345
|
+
};
|
|
346
|
+
}
|