@wraith-ai/core 0.1.0 → 0.1.1
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 +1 -1
- package/dist/{attempt-BeB7EltG.d.mts → attempt-HgararU3.d.mts} +2 -1
- package/dist/attempt-HgararU3.d.mts.map +1 -0
- package/dist/{capabilities-BPb5HSH6.d.mts → capabilities-CNsPrkB7.d.mts} +2 -1
- package/dist/capabilities-CNsPrkB7.d.mts.map +1 -0
- package/dist/index.d.mts +6 -5
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +11 -9
- package/dist/index.mjs.map +1 -0
- package/dist/{methods-CZp6XGn8.mjs → methods-7Ryz6zbd.mjs} +4 -2
- package/dist/methods-7Ryz6zbd.mjs.map +1 -0
- package/dist/{model-B6tJ9kX_.mjs → model-C16NUwXW.mjs} +2 -0
- package/dist/model-C16NUwXW.mjs.map +1 -0
- package/dist/{model-D6LQSveS.d.mts → model-_wqePNTL.d.mts} +4 -3
- package/dist/model-_wqePNTL.d.mts.map +1 -0
- package/dist/{models-dev-Y0ZikMBQ.d.mts → models-dev-DNgymZy3.d.mts} +3 -2
- package/dist/models-dev-DNgymZy3.d.mts.map +1 -0
- package/dist/{openai-codex-BhtZvNpm.mjs → openai-codex-Bz1EFPEi.mjs} +3 -1
- package/dist/openai-codex-Bz1EFPEi.mjs.map +1 -0
- package/dist/{openai-codex-Bk-iG7MZ.mjs → openai-codex-uvjhVoVy.mjs} +6 -4
- package/dist/openai-codex-uvjhVoVy.mjs.map +1 -0
- package/dist/{openai-compatible-hkXv-P74.mjs → openai-compatible-DBppnQ_N.mjs} +4 -2
- package/dist/openai-compatible-DBppnQ_N.mjs.map +1 -0
- package/dist/{openrouter-BflQBug2.mjs → openrouter-D8nTOAQo.mjs} +5 -3
- package/dist/openrouter-D8nTOAQo.mjs.map +1 -0
- package/dist/providers/openai-codex.d.mts +4 -3
- package/dist/providers/openai-codex.d.mts.map +1 -0
- package/dist/providers/openai-codex.mjs +1 -1
- package/dist/providers/openai-compatible.d.mts +3 -2
- package/dist/providers/openai-compatible.d.mts.map +1 -0
- package/dist/providers/openai-compatible.mjs +1 -1
- package/dist/providers/openrouter.d.mts +3 -2
- package/dist/providers/openrouter.d.mts.map +1 -0
- package/dist/providers/openrouter.mjs +1 -1
- package/dist/{shared-8d7RsyPH.mjs → shared-BIJaPhMm.mjs} +3 -1
- package/dist/shared-BIJaPhMm.mjs.map +1 -0
- package/dist/snapshot.d.mts +3 -2
- package/dist/snapshot.d.mts.map +1 -0
- package/dist/snapshot.mjs +4999 -4977
- package/dist/snapshot.mjs.map +1 -0
- package/dist/{turn-effort-DquDDwdE.mjs → turn-effort-C61L4JIt.mjs} +2 -0
- package/dist/turn-effort-C61L4JIt.mjs.map +1 -0
- package/package.json +3 -2
- package/src/agent/agent.ts +90 -0
- package/src/agent/attachments.ts +195 -0
- package/src/agent/classifier.ts +525 -0
- package/src/agent/compact.ts +384 -0
- package/src/agent/conversation.ts +300 -0
- package/src/agent/cost.ts +164 -0
- package/src/agent/inbox.ts +80 -0
- package/src/agent/instructions.ts +62 -0
- package/src/agent/loop.ts +675 -0
- package/src/agent/messages.ts +252 -0
- package/src/agent/on-demand.ts +58 -0
- package/src/agent/prompts.ts +35 -0
- package/src/agent/request-images.ts +94 -0
- package/src/agent/stop.ts +37 -0
- package/src/agent/title.ts +122 -0
- package/src/agent/tool-groups.ts +186 -0
- package/src/agent/tools.ts +106 -0
- package/src/app.ts +67 -0
- package/src/config/credential-file-store.ts +66 -0
- package/src/config/environment.ts +105 -0
- package/src/config/instructions.ts +157 -0
- package/src/config/load.ts +667 -0
- package/src/config/locked-file.ts +85 -0
- package/src/config/paths.ts +368 -0
- package/src/config/schema.ts +363 -0
- package/src/config/subagents.ts +156 -0
- package/src/config/trust.ts +53 -0
- package/src/config/write.ts +241 -0
- package/src/errors.ts +85 -0
- package/src/events/types.ts +258 -0
- package/src/glob.ts +161 -0
- package/src/index.ts +681 -0
- package/src/logger.ts +47 -0
- package/src/paths.ts +143 -0
- package/src/permissions/chain.ts +42 -0
- package/src/permissions/command.ts +233 -0
- package/src/permissions/layered.ts +77 -0
- package/src/permissions/match.ts +80 -0
- package/src/permissions/modes.ts +48 -0
- package/src/permissions/policy.ts +160 -0
- package/src/permissions/types.ts +159 -0
- package/src/providers/cache.ts +107 -0
- package/src/providers/capabilities.ts +77 -0
- package/src/providers/catalog-view.ts +91 -0
- package/src/providers/catalog.ts +65 -0
- package/src/providers/errors.ts +108 -0
- package/src/providers/factories/index.ts +15 -0
- package/src/providers/factories/openai-codex.ts +382 -0
- package/src/providers/factories/openai-compatible.ts +81 -0
- package/src/providers/factories/openrouter.ts +165 -0
- package/src/providers/factories/shared.ts +22 -0
- package/src/providers/model.ts +197 -0
- package/src/providers/models-dev.ts +355 -0
- package/src/providers/oauth/methods.ts +10 -0
- package/src/providers/oauth/openai-codex.ts +228 -0
- package/src/providers/oauth.ts +177 -0
- package/src/providers/openai-codex.ts +7 -0
- package/src/providers/openai-compatible.ts +2 -0
- package/src/providers/openrouter.ts +2 -0
- package/src/providers/registry.ts +464 -0
- package/src/providers/turn-effort.ts +156 -0
- package/src/providers/types.ts +83 -0
- package/src/session/backend.ts +44 -0
- package/src/session/file-store.ts +278 -0
- package/src/session/format.ts +1037 -0
- package/src/session/memory-store.ts +73 -0
- package/src/session/session.ts +153 -0
- package/src/session/store.ts +219 -0
- package/src/sign-in/attempt.ts +166 -0
- package/src/sign-in/loopback.ts +74 -0
- package/src/snapshot/data.json +8936 -0
- package/src/snapshot/index.ts +37 -0
- package/src/tools/bin.ts +364 -0
- package/src/tools/builtin.ts +48 -0
- package/src/tools/change.ts +88 -0
- package/src/tools/define.ts +267 -0
- package/src/tools/describe.ts +96 -0
- package/src/tools/fs/edit-file.ts +129 -0
- package/src/tools/fs/glob.ts +75 -0
- package/src/tools/fs/grep.ts +212 -0
- package/src/tools/fs/line-endings.ts +17 -0
- package/src/tools/fs/list-dir.ts +85 -0
- package/src/tools/fs/read-file.ts +230 -0
- package/src/tools/fs/ripgrep.ts +211 -0
- package/src/tools/fs/write-file.ts +97 -0
- package/src/tools/job.ts +252 -0
- package/src/tools/mcp/auth-store.ts +127 -0
- package/src/tools/mcp/command.ts +151 -0
- package/src/tools/mcp/config.ts +168 -0
- package/src/tools/mcp/servers.ts +463 -0
- package/src/tools/mcp/sign-in.ts +226 -0
- package/src/tools/mcp/stdio.ts +204 -0
- package/src/tools/mcp/tools.ts +173 -0
- package/src/tools/on-demand.ts +184 -0
- package/src/tools/registry.ts +935 -0
- package/src/tools/shell/bash.ts +217 -0
- package/src/tools/shell/find-bash.ts +162 -0
- package/src/tools/source.ts +35 -0
- package/src/tools/spill.ts +127 -0
- package/src/tools/subagents.ts +597 -0
- package/src/tools/tasks.ts +603 -0
- package/src/tools/to-model.ts +33 -0
- package/src/tools/web/address.ts +95 -0
- package/src/tools/web/exa.ts +163 -0
- package/src/tools/web/fetch.ts +373 -0
- package/src/tools/web/html.ts +173 -0
- package/src/tools/web/search.ts +111 -0
- package/src/tools/web/source.ts +51 -0
- package/src/units.ts +18 -0
- package/src/version.ts +23 -0
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { AgentError } from "../errors";
|
|
3
|
+
import { DANGER_LEVELS, type DangerLevel } from "../tools/define";
|
|
4
|
+
import { mcpServerSchema } from "../tools/mcp/config";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The single source of truth for everything configurable. Defaults live here
|
|
8
|
+
* and nowhere else, so `loadConfig` only has to merge partial inputs.
|
|
9
|
+
*
|
|
10
|
+
* Model identifiers are deliberately absent: slugs baked into the code would go
|
|
11
|
+
* stale, as models change weekly. Providers are declared in config, and models
|
|
12
|
+
* are discovered with `wraith models`.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The value of a model reference meaning "whatever the run is using".
|
|
17
|
+
* Spelled as a real string, not left `undefined`, so `wraith config show`
|
|
18
|
+
* renders it. Reserved: a profile cannot be named this.
|
|
19
|
+
*/
|
|
20
|
+
export const INHERIT_MODEL = "inherit";
|
|
21
|
+
|
|
22
|
+
/** What `instructions.files` is when nobody wrote it. */
|
|
23
|
+
export const DEFAULT_INSTRUCTION_FILES: readonly string[] = ["AGENTS.md"];
|
|
24
|
+
|
|
25
|
+
export const logLevelSchema = z.enum(["silent", "error", "warn", "info", "debug"]);
|
|
26
|
+
|
|
27
|
+
export const permissionModeSchema = z.enum(["ask", "yolo", "deny"]);
|
|
28
|
+
|
|
29
|
+
export const permissionDecisionSchema = z.enum(["allow", "ask", "deny"]);
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* A host for a rule: one name, `*.name` for its subdomains at any depth but not
|
|
33
|
+
* the name itself, or `*` for every host. A star anywhere else is refused:
|
|
34
|
+
* `example.*` would match a domain anybody can register.
|
|
35
|
+
*/
|
|
36
|
+
const domainPatternSchema = z
|
|
37
|
+
.string()
|
|
38
|
+
.regex(
|
|
39
|
+
/^(?:\*|(?:\*\.)?[a-z0-9-]+(?:\.[a-z0-9-]+)*\.?)$/i,
|
|
40
|
+
'must be a host name, "*.name" for its subdomains, or "*"',
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
export const permissionRuleSchema = z.object({
|
|
44
|
+
tool: z.string().optional(),
|
|
45
|
+
path: z.string().optional(),
|
|
46
|
+
command: z.string().optional(),
|
|
47
|
+
/** The host of the address a call claims. Never matched against the address as text. */
|
|
48
|
+
domain: domainPatternSchema.optional(),
|
|
49
|
+
decision: permissionDecisionSchema,
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
export const dangerLevelSchema = z.enum(DANGER_LEVELS);
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* What the classifier settles unless told otherwise; anything else goes to a
|
|
56
|
+
* person. One list for the config default and a hand-built classifier.
|
|
57
|
+
*/
|
|
58
|
+
export const DEFAULT_CLASSIFIER_DECIDES: readonly DangerLevel[] = ["write", "exec", "network"];
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Whether writing inside the workspace needs approval. Writing outside the
|
|
62
|
+
* workspace is a deny that this setting cannot override.
|
|
63
|
+
*/
|
|
64
|
+
export const workspaceWritesSchema = z.enum(["ask", "allow"]);
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The classifier that settles approvals. Every field has a default, so the
|
|
68
|
+
* block is always present and `config show` lists it.
|
|
69
|
+
*/
|
|
70
|
+
export const classifierSchema = z.object({
|
|
71
|
+
/** Off by default: it costs money and puts a model in the decision loop. */
|
|
72
|
+
enabled: z.boolean().default(false),
|
|
73
|
+
/** Profile name, "provider:model", or `INHERIT_MODEL` for the run's own. */
|
|
74
|
+
model: z.string().min(1).default(INHERIT_MODEL),
|
|
75
|
+
/** Which danger levels it may settle. The rest go to a human regardless. */
|
|
76
|
+
decides: z.array(dangerLevelSchema).default([...DEFAULT_CLASSIFIER_DECIDES]),
|
|
77
|
+
/** What a timeout, a provider error or an unparseable answer means. */
|
|
78
|
+
onUnsure: z.enum(["ask", "deny"]).default("ask"),
|
|
79
|
+
escalateAfter: z
|
|
80
|
+
.object({
|
|
81
|
+
consecutive: z.number().int().positive().default(3),
|
|
82
|
+
total: z.number().int().positive().default(20),
|
|
83
|
+
})
|
|
84
|
+
.prefault({}),
|
|
85
|
+
/**
|
|
86
|
+
* The trust boundary text in the classifier prompt. Empty means "use the
|
|
87
|
+
* built-in default", never "there is none". An empty value is the one input
|
|
88
|
+
* that would quietly halve the classifier's accuracy.
|
|
89
|
+
*/
|
|
90
|
+
environment: z.string().default(""),
|
|
91
|
+
blockRules: z.array(z.string()).default([]),
|
|
92
|
+
allowExceptions: z.array(z.string()).default([]),
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
/** Portable reasoning effort: AI SDK 7 maps these onto each vendor's native knob. */
|
|
96
|
+
export const reasoningEffortSchema = z.enum([
|
|
97
|
+
"provider-default",
|
|
98
|
+
"none",
|
|
99
|
+
"minimal",
|
|
100
|
+
"low",
|
|
101
|
+
"medium",
|
|
102
|
+
"high",
|
|
103
|
+
"xhigh",
|
|
104
|
+
"max",
|
|
105
|
+
]);
|
|
106
|
+
|
|
107
|
+
export const cacheTtlSchema = z.enum(["5m", "1h", "24h"]);
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Whether this endpoint understands OpenAI's prompt-cache fields. Opt-in,
|
|
111
|
+
* because vLLM answers an unknown body field with a 400.
|
|
112
|
+
*/
|
|
113
|
+
export const cacheDialectSchema = z.enum(["openai", "none"]);
|
|
114
|
+
|
|
115
|
+
export const cacheSchema = z.object({
|
|
116
|
+
enabled: z.boolean().default(true),
|
|
117
|
+
ttl: cacheTtlSchema.default("1h"),
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
/** Per-provider override: only the keys someone actually wrote. */
|
|
121
|
+
export const providerCacheSchema = z.object({
|
|
122
|
+
enabled: z.boolean().optional(),
|
|
123
|
+
ttl: cacheTtlSchema.optional(),
|
|
124
|
+
dialect: cacheDialectSchema.optional(),
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* `type` selects the factory that builds the provider. Built-ins cover
|
|
129
|
+
* OpenRouter and anything speaking the OpenAI wire format; a host can register
|
|
130
|
+
* more without changing the core.
|
|
131
|
+
*/
|
|
132
|
+
export const providerSchema = z.object({
|
|
133
|
+
type: z.string().min(1),
|
|
134
|
+
/** Inline key. Prefer `apiKeyEnv` so secrets stay out of the config file. */
|
|
135
|
+
apiKey: z.string().optional(),
|
|
136
|
+
/** Environment variable holding the key. Defaults to `<NAME>_API_KEY`. */
|
|
137
|
+
apiKeyEnv: z.string().optional(),
|
|
138
|
+
baseURL: z.string().optional(),
|
|
139
|
+
headers: z.record(z.string(), z.string()).default({}),
|
|
140
|
+
/** Passed verbatim to the provider factory. */
|
|
141
|
+
options: z.record(z.string(), z.unknown()).default({}),
|
|
142
|
+
/** Overrides the global cache settings for this provider only. */
|
|
143
|
+
cache: providerCacheSchema.optional(),
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* `~/.wraith/credentials.json`: provider name to key, and nothing else.
|
|
148
|
+
*
|
|
149
|
+
* Deliberately not part of `configSchema`: this file holds secrets, is never
|
|
150
|
+
* printed, and is written by `wraith key set` rather than edited by hand.
|
|
151
|
+
*/
|
|
152
|
+
export const credentialsSchema = z.record(
|
|
153
|
+
z.string(),
|
|
154
|
+
z.object({ apiKey: z.string().min(1) }),
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
export type Credentials = z.infer<typeof credentialsSchema>;
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Keys for services that are not model providers: the name `key set` takes,
|
|
161
|
+
* and the variable that wins over the stored key.
|
|
162
|
+
*/
|
|
163
|
+
export const SERVICE_KEY_ENV = { exa: "EXA_API_KEY" } as const;
|
|
164
|
+
|
|
165
|
+
export type ServiceName = keyof typeof SERVICE_KEY_ENV;
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* What this model costs, in USD per million tokens.
|
|
169
|
+
*
|
|
170
|
+
* A model with no price gets no cost, not zero, unless one is written here.
|
|
171
|
+
* A price written here wins over the catalog: it states the bill that actually
|
|
172
|
+
* arrives, not a public list price.
|
|
173
|
+
*/
|
|
174
|
+
export const modelPricingSchema = z.object({
|
|
175
|
+
input: z.number().nonnegative(),
|
|
176
|
+
output: z.number().nonnegative(),
|
|
177
|
+
/** Optional; missing rates fall back to multipliers of `input`. */
|
|
178
|
+
cacheRead: z.number().nonnegative().optional(),
|
|
179
|
+
cacheWrite: z.number().nonnegative().optional(),
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
export const modelProfileSchema = z.object({
|
|
183
|
+
/** "provider:model", or a bare model id when only one provider is configured. */
|
|
184
|
+
model: z.string().min(1),
|
|
185
|
+
pricing: modelPricingSchema.optional(),
|
|
186
|
+
reasoning: reasoningEffortSchema.optional(),
|
|
187
|
+
temperature: z.number().min(0).max(2).optional(),
|
|
188
|
+
topP: z.number().min(0).max(1).optional(),
|
|
189
|
+
maxOutputTokens: z.number().int().positive().optional(),
|
|
190
|
+
/** Escape hatch for vendor-specific settings, passed straight through. */
|
|
191
|
+
providerOptions: z.record(z.string(), z.record(z.string(), z.unknown())).optional(),
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
/** A profile may be written as a bare string when it needs no settings. */
|
|
195
|
+
export const modelProfileInputSchema = z
|
|
196
|
+
.union([z.string().min(1), modelProfileSchema])
|
|
197
|
+
.transform((value) => (typeof value === "string" ? { model: value } : value));
|
|
198
|
+
|
|
199
|
+
export const configSchema = z.object({
|
|
200
|
+
providers: z
|
|
201
|
+
.record(z.string(), providerSchema)
|
|
202
|
+
.prefault({ openrouter: { type: "openrouter", headers: {}, options: {} } }),
|
|
203
|
+
/** Named profiles the application refers to. Empty until the user picks one. */
|
|
204
|
+
models: z.record(z.string(), modelProfileInputSchema).prefault({}),
|
|
205
|
+
/** Which profile (or "provider:model") to use when none is named. */
|
|
206
|
+
defaultModel: z.string().optional(),
|
|
207
|
+
limits: z
|
|
208
|
+
.object({
|
|
209
|
+
// No default: each host applies its own, and a written number beats it.
|
|
210
|
+
maxSteps: z.number().int().positive().max(1000).optional(),
|
|
211
|
+
})
|
|
212
|
+
.prefault({}),
|
|
213
|
+
tools: z
|
|
214
|
+
.object({
|
|
215
|
+
allow: z.array(z.string()).optional(),
|
|
216
|
+
deny: z.array(z.string()).default([]),
|
|
217
|
+
/** Where bash is, when neither PATH nor git can say. */
|
|
218
|
+
bashPath: z.string().min(1).optional(),
|
|
219
|
+
/** Where ripgrep is, when neither the bundled copy nor PATH will do. */
|
|
220
|
+
ripgrepPath: z.string().min(1).optional(),
|
|
221
|
+
})
|
|
222
|
+
.prefault({}),
|
|
223
|
+
/**
|
|
224
|
+
* The network tools. Search has its own `enabled` switch, because every query
|
|
225
|
+
* goes to a third party. `web_fetch` is turned off like any other tool:
|
|
226
|
+
* through `tools.deny`.
|
|
227
|
+
*/
|
|
228
|
+
web: z
|
|
229
|
+
.object({
|
|
230
|
+
search: z.object({ enabled: z.boolean().default(true) }).prefault({}),
|
|
231
|
+
})
|
|
232
|
+
.prefault({}),
|
|
233
|
+
permissions: z
|
|
234
|
+
.object({
|
|
235
|
+
mode: permissionModeSchema.default("ask"),
|
|
236
|
+
workspaceWrites: workspaceWritesSchema.default("ask"),
|
|
237
|
+
rules: z.array(permissionRuleSchema).default([]),
|
|
238
|
+
classifier: classifierSchema.prefault({}),
|
|
239
|
+
})
|
|
240
|
+
.prefault({}),
|
|
241
|
+
session: z
|
|
242
|
+
.object({
|
|
243
|
+
store: z.enum(["memory", "file"]).default("file"),
|
|
244
|
+
dir: z.string().optional(),
|
|
245
|
+
/**
|
|
246
|
+
* Let a model name the session after its first turn. The derived name,
|
|
247
|
+
* the first line of the first prompt, stays either way.
|
|
248
|
+
*/
|
|
249
|
+
autoTitle: z.boolean().default(true),
|
|
250
|
+
/** Profile that writes it, or `INHERIT_MODEL` for the conversation's own. */
|
|
251
|
+
titleModel: z.string().min(1).default(INHERIT_MODEL),
|
|
252
|
+
})
|
|
253
|
+
.prefault({}),
|
|
254
|
+
/** The files whose text the agent is given as instructions: AGENTS.md and the like. */
|
|
255
|
+
instructions: z
|
|
256
|
+
.object({
|
|
257
|
+
/** Names looked for in each directory, first found wins; `[]` reads none. */
|
|
258
|
+
files: z
|
|
259
|
+
.array(
|
|
260
|
+
z
|
|
261
|
+
.string()
|
|
262
|
+
.min(1)
|
|
263
|
+
.refine((name) => !/[\\/:\0]/.test(name) && name !== "." && name !== "..", "must be a file name"),
|
|
264
|
+
)
|
|
265
|
+
.default([...DEFAULT_INSTRUCTION_FILES]),
|
|
266
|
+
/** All files together; a longer one is cut there, and says so. `0` reads none. */
|
|
267
|
+
maxBytes: z.number().int().min(0).default(32_768),
|
|
268
|
+
})
|
|
269
|
+
.prefault({}),
|
|
270
|
+
compaction: z
|
|
271
|
+
.object({
|
|
272
|
+
enabled: z.boolean().default(true),
|
|
273
|
+
/** Profile used to write the summary, or `INHERIT_MODEL` for the run's own. */
|
|
274
|
+
model: z.string().min(1).default(INHERIT_MODEL),
|
|
275
|
+
keepLastTurns: z.number().int().min(0).max(50).default(2),
|
|
276
|
+
bufferTokens: z.number().int().min(0).default(20_000),
|
|
277
|
+
summaryMaxTokens: z.number().int().positive().default(4_096),
|
|
278
|
+
})
|
|
279
|
+
.prefault({}),
|
|
280
|
+
capabilities: z
|
|
281
|
+
.object({
|
|
282
|
+
/** "off" for networks that do not let a CLI call out. */
|
|
283
|
+
source: z.enum(["models.dev", "off"]).default("models.dev"),
|
|
284
|
+
ttlHours: z.number().positive().default(24),
|
|
285
|
+
})
|
|
286
|
+
.prefault({}),
|
|
287
|
+
/** MCP servers by name. A project file can turn one off with `{ "enabled": false }`. */
|
|
288
|
+
mcpServers: z.record(z.string(), mcpServerSchema).default({}),
|
|
289
|
+
/** On by default: a provider that cannot cache ignores it. */
|
|
290
|
+
cache: cacheSchema.prefault({}),
|
|
291
|
+
logLevel: logLevelSchema.default("info"),
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* The core's schema plus a host's own settings. A host adds keys and never
|
|
296
|
+
* redefines one: its `permissions` would be a second policy. Unknown keys are
|
|
297
|
+
* stripped, so a setting nobody declared can be written to the file and never
|
|
298
|
+
* read back.
|
|
299
|
+
*/
|
|
300
|
+
export function withHostSettings(extend: z.ZodObject | undefined): z.ZodObject {
|
|
301
|
+
if (extend === undefined) return configSchema;
|
|
302
|
+
|
|
303
|
+
const taken = Object.keys(extend.shape).filter((key) => key in configSchema.shape);
|
|
304
|
+
if (taken.length > 0) {
|
|
305
|
+
throw new AgentError(
|
|
306
|
+
"config/invalid",
|
|
307
|
+
`A host's settings cannot redefine the core's: ${taken.join(", ")}.`,
|
|
308
|
+
{ details: { keys: taken } },
|
|
309
|
+
);
|
|
310
|
+
}
|
|
311
|
+
return configSchema.extend(extend.shape);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* A config from an object, with every default filled in: what `loadConfig`
|
|
316
|
+
* does once its layers are merged, for a host that reads no files.
|
|
317
|
+
*/
|
|
318
|
+
export function parseConfig(input: unknown, extend?: z.ZodObject): Config {
|
|
319
|
+
const parsed = withHostSettings(extend).safeParse(input);
|
|
320
|
+
if (!parsed.success) {
|
|
321
|
+
const issues = parsed.error.issues.map((issue) => ({
|
|
322
|
+
path: issue.path.join("."),
|
|
323
|
+
message: issue.message,
|
|
324
|
+
}));
|
|
325
|
+
const summary = issues.map((issue) => `${issue.path || "(root)"}: ${issue.message}`).join("; ");
|
|
326
|
+
throw new AgentError("config/invalid", `Invalid configuration — ${summary}`, {
|
|
327
|
+
details: { issues },
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
// The schema is assembled at run time, so its output type is stated here once.
|
|
331
|
+
return parsed.data as Config;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
export type Config = z.infer<typeof configSchema>;
|
|
335
|
+
export type ConfigInput = z.input<typeof configSchema>;
|
|
336
|
+
export type LogLevelName = z.infer<typeof logLevelSchema>;
|
|
337
|
+
export type PermissionMode = z.infer<typeof permissionModeSchema>;
|
|
338
|
+
export type PermissionDecision = z.infer<typeof permissionDecisionSchema>;
|
|
339
|
+
export type PermissionRule = z.infer<typeof permissionRuleSchema>;
|
|
340
|
+
export type PermissionPolicyConfig = z.infer<typeof configSchema>["permissions"];
|
|
341
|
+
export type WorkspaceWrites = z.infer<typeof workspaceWritesSchema>;
|
|
342
|
+
export type ClassifierConfig = z.infer<typeof classifierSchema>;
|
|
343
|
+
export type ProviderConfig = z.infer<typeof providerSchema>;
|
|
344
|
+
export type CacheConfig = z.infer<typeof cacheSchema>;
|
|
345
|
+
export type ProviderCacheConfig = z.infer<typeof providerCacheSchema>;
|
|
346
|
+
export type ModelProfile = z.infer<typeof modelProfileSchema>;
|
|
347
|
+
export type ModelPricingConfig = z.infer<typeof modelPricingSchema>;
|
|
348
|
+
export type ReasoningEffort = z.infer<typeof reasoningEffortSchema>;
|
|
349
|
+
|
|
350
|
+
/** Conventional environment variable for a provider that declares none. */
|
|
351
|
+
export function defaultApiKeyEnv(providerName: string): string {
|
|
352
|
+
return `${providerName.replace(/[^a-zA-Z0-9]+/g, "_").toUpperCase()}_API_KEY`;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* Renders a secret for display. Never returns enough to reconstruct the value,
|
|
357
|
+
* but enough to tell two keys apart.
|
|
358
|
+
*/
|
|
359
|
+
export function maskSecret(value: string | undefined): string {
|
|
360
|
+
if (value === undefined || value.length === 0) return "(not set)";
|
|
361
|
+
if (value.length <= 8) return "*".repeat(value.length);
|
|
362
|
+
return `${value.slice(0, 4)}${"*".repeat(6)}${value.slice(-4)}`;
|
|
363
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { readFile, readdir } from "node:fs/promises";
|
|
2
|
+
import { basename, join } from "node:path";
|
|
3
|
+
import type { SubagentType } from "../tools/subagents";
|
|
4
|
+
import { AGENT_FILE_EXTENSION as EXTENSION } from "./paths";
|
|
5
|
+
import { reasoningEffortSchema } from "./schema";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Kinds of agent a person wrote as files: one `<name>.md` each, a few
|
|
9
|
+
* `key: value` lines between two `---`, and under them the agent's role.
|
|
10
|
+
*
|
|
11
|
+
* A host calls this and decides which directories to pass; nothing reads them
|
|
12
|
+
* on its own. A project's directory is the host's to leave out when it does
|
|
13
|
+
* not trust the project: a file here is a prompt its author controls.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
export interface SkippedSubagentFile {
|
|
17
|
+
path: string;
|
|
18
|
+
/** Why it gave no agent, with the line when a line is to blame. */
|
|
19
|
+
reason: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface LoadedSubagents {
|
|
23
|
+
agents: Record<string, SubagentType>;
|
|
24
|
+
/** Files that gave no agent. Said, never dropped: a typo is otherwise an agent that never appears. */
|
|
25
|
+
skipped: SkippedSubagentFile[];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const FENCE = "---";
|
|
29
|
+
|
|
30
|
+
/** The name goes into the tool's schema and its description, so it is kept plain. */
|
|
31
|
+
const NAME = /^[a-z][a-z0-9_-]*$/;
|
|
32
|
+
|
|
33
|
+
const KEYS = ["description", "tools", "model", "reasoning", "maxSteps"] as const;
|
|
34
|
+
type Key = (typeof KEYS)[number];
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Reads the directories in order; a later one's agent replaces an earlier
|
|
38
|
+
* one's of the same name, so a project's can stand in for the user's. A
|
|
39
|
+
* directory that is not there gives nothing.
|
|
40
|
+
*/
|
|
41
|
+
export async function loadSubagents(dirs: readonly string[]): Promise<LoadedSubagents> {
|
|
42
|
+
const agents: Record<string, SubagentType> = {};
|
|
43
|
+
const skipped: SkippedSubagentFile[] = [];
|
|
44
|
+
|
|
45
|
+
for (const dir of dirs) {
|
|
46
|
+
let names: string[];
|
|
47
|
+
try {
|
|
48
|
+
names = (await readdir(dir, { withFileTypes: true }))
|
|
49
|
+
.filter((entry) => entry.isFile() && entry.name.endsWith(EXTENSION))
|
|
50
|
+
.map((entry) => entry.name)
|
|
51
|
+
.sort();
|
|
52
|
+
} catch (error) {
|
|
53
|
+
if ((error as NodeJS.ErrnoException).code !== "ENOENT") {
|
|
54
|
+
skipped.push({ path: dir, reason: `cannot be read: ${reasonOf(error)}` });
|
|
55
|
+
}
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
for (const file of names) {
|
|
60
|
+
const path = join(dir, file);
|
|
61
|
+
const name = basename(file, EXTENSION);
|
|
62
|
+
if (!NAME.test(name)) {
|
|
63
|
+
skipped.push({
|
|
64
|
+
path,
|
|
65
|
+
reason: "the file's name is the agent's: lowercase letters, digits, `-` and `_`, starting with a letter",
|
|
66
|
+
});
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
try {
|
|
70
|
+
agents[name] = parseSubagent(await readFile(path, "utf8"));
|
|
71
|
+
} catch (error) {
|
|
72
|
+
skipped.push({ path, reason: reasonOf(error) });
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return { agents, skipped };
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function reasonOf(error: unknown): string {
|
|
81
|
+
return error instanceof Error ? error.message : String(error);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* One file. The header is not YAML: one `key: value` to a line, so a list or
|
|
86
|
+
* a value over several lines is refused by name, never read wrong.
|
|
87
|
+
*/
|
|
88
|
+
export function parseSubagent(text: string): SubagentType {
|
|
89
|
+
const lines = text.replace(/^/, "").split(/\r?\n/);
|
|
90
|
+
if (lines[0]?.trim() !== FENCE) throw new Error("line 1: expected `---` to open the header");
|
|
91
|
+
const close = lines.findIndex((line, index) => index > 0 && line.trim() === FENCE);
|
|
92
|
+
if (close === -1) throw new Error("the header is never closed with `---`");
|
|
93
|
+
|
|
94
|
+
const header = new Map<Key, string>();
|
|
95
|
+
for (let index = 1; index < close; index += 1) {
|
|
96
|
+
const line = lines[index] ?? "";
|
|
97
|
+
const at = `line ${String(index + 1)}`;
|
|
98
|
+
if (line.trim() === "" || line.trim().startsWith("#")) continue;
|
|
99
|
+
|
|
100
|
+
const match = /^([A-Za-z][A-Za-z0-9_]*)\s*:(.*)$/.exec(line);
|
|
101
|
+
if (match === null) {
|
|
102
|
+
throw new Error(`${at}: expected \`key: value\` on one line; lists and values over several lines are not read`);
|
|
103
|
+
}
|
|
104
|
+
const key = match[1] as string;
|
|
105
|
+
if (!(KEYS as readonly string[]).includes(key)) {
|
|
106
|
+
throw new Error(`${at}: unknown key \`${key}\`; known: ${KEYS.join(", ")}`);
|
|
107
|
+
}
|
|
108
|
+
if (header.has(key as Key)) throw new Error(`${at}: \`${key}\` is given twice`);
|
|
109
|
+
|
|
110
|
+
const value = unquoted((match[2] as string).trim());
|
|
111
|
+
if (value === "" || value === ">" || value === "|") {
|
|
112
|
+
throw new Error(`${at}: \`${key}\` needs its value on the same line`);
|
|
113
|
+
}
|
|
114
|
+
header.set(key as Key, value);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const description = header.get("description");
|
|
118
|
+
if (description === undefined) throw new Error("the header has no `description`");
|
|
119
|
+
const system = lines.slice(close + 1).join("\n").trim();
|
|
120
|
+
if (system === "") throw new Error("there is no prompt under the header");
|
|
121
|
+
|
|
122
|
+
const agent: SubagentType = { description, system };
|
|
123
|
+
|
|
124
|
+
const tools = header.get("tools");
|
|
125
|
+
if (tools !== undefined) {
|
|
126
|
+
const listed = tools.replace(/^\[(.*)\]$/, "$1").split(",").map((tool) => unquoted(tool.trim()));
|
|
127
|
+
if (listed.some((tool) => tool === "")) throw new Error("`tools` has an empty name in it");
|
|
128
|
+
agent.tools = listed;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const model = header.get("model");
|
|
132
|
+
if (model !== undefined) agent.model = model;
|
|
133
|
+
|
|
134
|
+
const reasoning = header.get("reasoning");
|
|
135
|
+
if (reasoning !== undefined) {
|
|
136
|
+
const parsed = reasoningEffortSchema.safeParse(reasoning);
|
|
137
|
+
if (!parsed.success) {
|
|
138
|
+
throw new Error(`\`reasoning\` is "${reasoning}"; known: ${reasoningEffortSchema.options.join(", ")}`);
|
|
139
|
+
}
|
|
140
|
+
agent.reasoning = parsed.data;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const maxSteps = header.get("maxSteps");
|
|
144
|
+
if (maxSteps !== undefined) {
|
|
145
|
+
if (!/^[1-9][0-9]*$/.test(maxSteps)) throw new Error(`\`maxSteps\` is "${maxSteps}"; it has to be a whole number above zero`);
|
|
146
|
+
agent.maxSteps = Number(maxSteps);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return agent;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** A value somebody quoted out of habit means what is between the quotes. */
|
|
153
|
+
function unquoted(value: string): string {
|
|
154
|
+
const quoted = /^"(.*)"$/.exec(value) ?? /^'(.*)'$/.exec(value);
|
|
155
|
+
return quoted === null ? value : (quoted[1] as string);
|
|
156
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { dirname, join, resolve } from "node:path";
|
|
3
|
+
import type { ProjectTrust } from "./load";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The answers somebody gave to "trust this project?", kept per repository.
|
|
7
|
+
*
|
|
8
|
+
* In core so every host reads the same answers: a project trusted with the CLI
|
|
9
|
+
* is trusted in a program that asks for the remembered decision. Asking is the
|
|
10
|
+
* host's; this only keeps what was answered.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
const TRUST_FILE = "trusted.json";
|
|
14
|
+
|
|
15
|
+
interface TrustFile {
|
|
16
|
+
version: 1;
|
|
17
|
+
projects: Record<string, ProjectTrust>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function trustFilePath(userDir: string): string {
|
|
21
|
+
return join(userDir, TRUST_FILE);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Exact paths only: a trusted parent never overrules a child marked untrusted.
|
|
25
|
+
function keyFor(root: string): string {
|
|
26
|
+
const full = resolve(root);
|
|
27
|
+
return process.platform === "win32" ? full.toLowerCase() : full;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function readTrustFile(file: string): TrustFile {
|
|
31
|
+
try {
|
|
32
|
+
const parsed = JSON.parse(readFileSync(file, "utf8").replace(/^/, "")) as Partial<TrustFile>;
|
|
33
|
+
return { version: 1, projects: { ...parsed.projects } };
|
|
34
|
+
} catch {
|
|
35
|
+
return { version: 1, projects: {} };
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** The stored answer for this repository, or undefined when nobody answered. */
|
|
40
|
+
export function readTrust(userDir: string, root: string): ProjectTrust | undefined {
|
|
41
|
+
const decision = readTrustFile(trustFilePath(userDir)).projects[keyFor(root)];
|
|
42
|
+
return decision === "trusted" || decision === "untrusted" ? decision : undefined;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function writeTrust(userDir: string, root: string, decision: ProjectTrust): void {
|
|
46
|
+
const file = trustFilePath(userDir);
|
|
47
|
+
const current = readTrustFile(file);
|
|
48
|
+
current.projects[keyFor(root)] = decision;
|
|
49
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
50
|
+
const temporary = `${file}.${String(process.pid)}.tmp`;
|
|
51
|
+
writeFileSync(temporary, `${JSON.stringify(current, null, 2)}\n`, "utf8");
|
|
52
|
+
renameSync(temporary, file);
|
|
53
|
+
}
|