@tenonai/yew 0.0.0-stage → 1.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.
Files changed (133) hide show
  1. package/CHANGELOG.md +296 -0
  2. package/LICENSE +21 -0
  3. package/README.md +55 -2
  4. package/THIRD-PARTY-NOTICES.md +122 -0
  5. package/bin/yew.js +9 -0
  6. package/dist/cli/app.js +198 -0
  7. package/dist/cli/export.js +57 -0
  8. package/dist/cli/io.js +65 -0
  9. package/dist/cli/key.js +113 -0
  10. package/dist/cli/main.js +44 -0
  11. package/dist/cli/mcp.js +76 -0
  12. package/dist/cli/remote.js +116 -0
  13. package/dist/cli/run.js +72 -0
  14. package/dist/cli/schedule.js +123 -0
  15. package/dist/cli/send.js +126 -0
  16. package/dist/cli/serve.js +301 -0
  17. package/dist/cli/service.js +241 -0
  18. package/dist/cli/system.js +44 -0
  19. package/dist/remote/access.js +107 -0
  20. package/dist/remote/connector.js +278 -0
  21. package/dist/remote/identity.js +71 -0
  22. package/dist/remote/noise.js +287 -0
  23. package/dist/remote/protocol.js +51 -0
  24. package/dist/remote/push.js +37 -0
  25. package/dist/server/agent/compaction.js +242 -0
  26. package/dist/server/agent/context.js +119 -0
  27. package/dist/server/agent/held.js +48 -0
  28. package/dist/server/agent/helper.js +122 -0
  29. package/dist/server/agent/mailroom.js +510 -0
  30. package/dist/server/agent/prompt.js +268 -0
  31. package/dist/server/agent/runner.js +1159 -0
  32. package/dist/server/artifacts.js +216 -0
  33. package/dist/server/budget.js +88 -0
  34. package/dist/server/data-export.js +105 -0
  35. package/dist/server/data-usage.js +143 -0
  36. package/dist/server/db/database.js +49 -0
  37. package/dist/server/db/migrations.js +347 -0
  38. package/dist/server/errors.js +16 -0
  39. package/dist/server/events.js +17 -0
  40. package/dist/server/export.js +378 -0
  41. package/dist/server/folder-picker.js +32 -0
  42. package/dist/server/from-jig.js +98 -0
  43. package/dist/server/http/agents.js +78 -0
  44. package/dist/server/http/api.js +468 -0
  45. package/dist/server/http/app.js +111 -0
  46. package/dist/server/http/artifacts.js +53 -0
  47. package/dist/server/http/data.js +54 -0
  48. package/dist/server/http/devices.js +47 -0
  49. package/dist/server/http/events.js +134 -0
  50. package/dist/server/http/idempotency.js +53 -0
  51. package/dist/server/http/input.js +106 -0
  52. package/dist/server/http/mail.js +52 -0
  53. package/dist/server/http/mcp.js +134 -0
  54. package/dist/server/http/notes.js +91 -0
  55. package/dist/server/http/problem.js +55 -0
  56. package/dist/server/http/providers.js +131 -0
  57. package/dist/server/http/schedules.js +58 -0
  58. package/dist/server/http/search.js +42 -0
  59. package/dist/server/http/security.js +168 -0
  60. package/dist/server/http/skills.js +53 -0
  61. package/dist/server/http/system.js +26 -0
  62. package/dist/server/ids.js +30 -0
  63. package/dist/server/keys.js +51 -0
  64. package/dist/server/mail/envelope.js +152 -0
  65. package/dist/server/mail/post.js +603 -0
  66. package/dist/server/mail/store.js +279 -0
  67. package/dist/server/manage.js +601 -0
  68. package/dist/server/mcp-builtin.js +179 -0
  69. package/dist/server/mcp-config.js +305 -0
  70. package/dist/server/mcp-oauth.js +448 -0
  71. package/dist/server/mcp.js +668 -0
  72. package/dist/server/model/gateway.js +410 -0
  73. package/dist/server/paths.js +34 -0
  74. package/dist/server/permissions/engine.js +249 -0
  75. package/dist/server/permissions/paths.js +93 -0
  76. package/dist/server/permissions/shell.js +635 -0
  77. package/dist/server/push.js +345 -0
  78. package/dist/server/reveal.js +36 -0
  79. package/dist/server/runtime.js +17 -0
  80. package/dist/server/schedules.js +356 -0
  81. package/dist/server/settings.js +80 -0
  82. package/dist/server/skills.js +262 -0
  83. package/dist/server/store.js +1077 -0
  84. package/dist/server/tools/files.js +318 -0
  85. package/dist/server/tools/html.js +221 -0
  86. package/dist/server/tools/jobs.js +38 -0
  87. package/dist/server/tools/mail.js +680 -0
  88. package/dist/server/tools/match.js +81 -0
  89. package/dist/server/tools/misc.js +253 -0
  90. package/dist/server/tools/process.js +128 -0
  91. package/dist/server/tools/read-state.js +40 -0
  92. package/dist/server/tools/registry.js +43 -0
  93. package/dist/server/tools/search.js +175 -0
  94. package/dist/server/tools/shell.js +124 -0
  95. package/dist/server/tools/types.js +43 -0
  96. package/dist/server/tools/web-search.js +198 -0
  97. package/dist/server/tools/web.js +240 -0
  98. package/dist/shared/cron.js +105 -0
  99. package/dist/shared/mcp-presets.js +170 -0
  100. package/dist/shared/messages.js +45 -0
  101. package/dist/shared/policy.js +42 -0
  102. package/dist/shared/types.js +34 -0
  103. package/dist/shared/version.js +8 -0
  104. package/mcp-builtin/package-lock.json +3398 -0
  105. package/mcp-builtin/package.json +14 -0
  106. package/mcp-builtin/presets/gitee/package-lock.json +105 -0
  107. package/mcp-builtin/presets/gitee/package.json +10 -0
  108. package/mcp-builtin/presets/notion/package-lock.json +2243 -0
  109. package/mcp-builtin/presets/notion/package.json +10 -0
  110. package/mcp-builtin/presets/sentry/package-lock.json +28 -0
  111. package/mcp-builtin/presets/sentry/package.json +10 -0
  112. package/package.json +75 -5
  113. package/web/dist/THIRD-PARTY-NOTICES.txt +1944 -0
  114. package/web/dist/assets/index-B9oWPSod.css +1 -0
  115. package/web/dist/assets/index-C5qBMzFX.js +68 -0
  116. package/web/dist/icons/app-icon.png +0 -0
  117. package/web/dist/icons/apple-touch-icon.png +0 -0
  118. package/web/dist/icons/favicon-16.png +0 -0
  119. package/web/dist/icons/favicon-32.png +0 -0
  120. package/web/dist/icons/favicon-64.png +0 -0
  121. package/web/dist/icons/tenon-mark.svg +1 -0
  122. package/web/dist/icons/yew-mark.svg +1 -0
  123. package/web/dist/index.html +18 -0
  124. package/web/dist/mascot/gift.webp +0 -0
  125. package/web/dist/mascot/greeting.webp +0 -0
  126. package/web/dist/mascot/happy.webp +0 -0
  127. package/web/dist/mascot/lost.webp +0 -0
  128. package/web/dist/mascot/shush.webp +0 -0
  129. package/web/dist/mascot/sleepy.webp +0 -0
  130. package/web/dist/mascot/sorry.webp +0 -0
  131. package/web/dist/mascot/thinking.webp +0 -0
  132. package/web/dist/mascot/waking.webp +0 -0
  133. package/web/dist/mascot/writing.webp +0 -0
@@ -0,0 +1,410 @@
1
+ /**
2
+ * The model layer: the only place that imports the model library (AGENTS.md). Everything else talks to
3
+ * models through `ModelGateway` and the message types in `src/shared/messages.ts`.
4
+ */
5
+ import { clampThinkingLevel, createModels, createProvider, getSupportedThinkingLevels, isContextOverflow, isRetryableAssistantError, retryDelayMs, } from "@earendil-works/pi-ai";
6
+ import { openAICompletionsApi } from "@earendil-works/pi-ai/api/openai-completions.lazy";
7
+ import { builtinProviders } from "@earendil-works/pi-ai/providers/all";
8
+ import { invalid, YewError } from "../errors.js";
9
+ const DEFAULT_RETRY = { maxRetries: 8, baseDelayMs: 2000 };
10
+ const MAX_RETRY_DELAY_MS = 60_000;
11
+ /** A response that sends nothing for this long is given up and tried again. */
12
+ const DEFAULT_IDLE_MS = 5 * 60 * 1000;
13
+ /** What a request that never answered comes back as. */
14
+ function emptyMessage(model) {
15
+ return {
16
+ role: "assistant",
17
+ content: [],
18
+ api: model.api,
19
+ provider: model.provider,
20
+ model: model.id,
21
+ usage: {
22
+ input: 0,
23
+ output: 0,
24
+ cacheRead: 0,
25
+ cacheWrite: 0,
26
+ totalTokens: 0,
27
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
28
+ },
29
+ stopReason: "error",
30
+ timestamp: Date.now(),
31
+ };
32
+ }
33
+ /** Resolves true after `ms`, or false as soon as the signal aborts. */
34
+ function pause(ms, signal) {
35
+ return new Promise((resolve) => {
36
+ if (signal?.aborted)
37
+ return resolve(false);
38
+ const stop = () => {
39
+ clearTimeout(timer);
40
+ resolve(false);
41
+ };
42
+ const timer = setTimeout(() => {
43
+ signal?.removeEventListener("abort", stop);
44
+ resolve(true);
45
+ }, ms);
46
+ signal?.addEventListener("abort", stop, { once: true });
47
+ });
48
+ }
49
+ /** Providers registered in mainland China; the default comes from this list (spec 21.6). */
50
+ const CN_PROVIDERS = new Set([
51
+ "deepseek",
52
+ "moonshotai-cn",
53
+ "zai-coding-cn",
54
+ "minimax-cn",
55
+ "qwen-token-plan-cn",
56
+ "xiaomi",
57
+ "xiaomi-token-plan-cn",
58
+ "ant-ling",
59
+ ]);
60
+ /**
61
+ * Built-in providers Yew does not offer: they sign in through an account or a cloud SDK instead of an
62
+ * API key, or need extra account configuration. Yew only uses API keys (docs/prior-art.md, section 3).
63
+ */
64
+ const HIDDEN_PROVIDERS = new Set([
65
+ "amazon-bedrock",
66
+ "google-vertex",
67
+ "azure",
68
+ "openai-codex",
69
+ "github-copilot",
70
+ "radius",
71
+ "typesafe",
72
+ "cloudflare-workers-ai",
73
+ "cloudflare-ai-gateway",
74
+ ]);
75
+ /** Sent to keyless local servers, which ignore it; the OpenAI client needs some value. */
76
+ const KEYLESS = "yew-no-key";
77
+ /** Keys come only from the keychain: never from environment variables or files (AGENTS.md). */
78
+ const NO_AMBIENT_AUTH = {
79
+ env: async () => undefined,
80
+ fileExists: async () => false,
81
+ };
82
+ class KeychainCredentials {
83
+ #keys;
84
+ constructor(keys) {
85
+ this.#keys = keys;
86
+ }
87
+ async read(providerId) {
88
+ const key = await this.#keys.get(providerId);
89
+ return key ? { type: "api_key", key } : undefined;
90
+ }
91
+ async list() {
92
+ return [];
93
+ }
94
+ async modify(providerId, fn) {
95
+ const current = await this.read(providerId);
96
+ const next = await fn(current);
97
+ if (next?.type === "api_key" && next.key)
98
+ await this.#keys.set(providerId, next.key);
99
+ return next ?? current;
100
+ }
101
+ async delete(providerId) {
102
+ await this.#keys.delete(providerId);
103
+ }
104
+ }
105
+ function customProvider(config) {
106
+ return createProvider({
107
+ id: config.id,
108
+ name: config.name,
109
+ baseUrl: config.baseUrl,
110
+ auth: {
111
+ apiKey: {
112
+ name: `${config.name} API key`,
113
+ resolve: async ({ credential }) => {
114
+ if (credential?.key)
115
+ return { auth: { apiKey: credential.key }, source: "keychain" };
116
+ return config.keyRequired ? undefined : { auth: { apiKey: KEYLESS }, source: "none" };
117
+ },
118
+ },
119
+ },
120
+ models: config.models.map((model) => ({
121
+ id: model.id,
122
+ name: model.name ?? model.id,
123
+ api: "openai-completions",
124
+ provider: config.id,
125
+ baseUrl: config.baseUrl,
126
+ reasoning: model.reasoning ?? false,
127
+ input: model.input ?? ["text"],
128
+ cost: {
129
+ input: model.cost?.input ?? 0,
130
+ output: model.cost?.output ?? 0,
131
+ cacheRead: model.cost?.cacheRead ?? 0,
132
+ cacheWrite: model.cost?.cacheWrite ?? 0,
133
+ },
134
+ contextWindow: model.contextWindow,
135
+ maxTokens: model.maxTokens,
136
+ })),
137
+ api: openAICompletionsApi(),
138
+ });
139
+ }
140
+ function toInfo(model) {
141
+ const levels = model.reasoning ? getSupportedThinkingLevels(model) : ["off"];
142
+ return {
143
+ provider: model.provider,
144
+ id: model.id,
145
+ name: model.name,
146
+ contextWindow: model.contextWindow,
147
+ maxTokens: model.maxTokens,
148
+ reasoning: model.reasoning,
149
+ thinkingLevels: levels,
150
+ input: model.input.filter((kind) => kind === "text" || kind === "image"),
151
+ cost: {
152
+ input: model.cost.input,
153
+ output: model.cost.output,
154
+ cacheRead: model.cost.cacheRead,
155
+ cacheWrite: model.cost.cacheWrite,
156
+ },
157
+ };
158
+ }
159
+ /**
160
+ * A provider's error as one readable line: "401 {json}" becomes "401: <the message in the json>".
161
+ * Anything that does not look like that is returned as it is, cut to a sensible length.
162
+ */
163
+ export function plainError(text) {
164
+ const match = /^(\d{3}):?\s*(\{[\s\S]*\})\s*$/.exec(text.trim());
165
+ if (match) {
166
+ try {
167
+ const body = JSON.parse(match[2]);
168
+ const inner = typeof body.error === "string" ? body.error : body.error?.message;
169
+ const message = typeof inner === "string" ? inner : body.message;
170
+ if (typeof message === "string" && message.trim())
171
+ return `${match[1]}: ${message.trim()}`;
172
+ }
173
+ catch {
174
+ // Not JSON after all.
175
+ }
176
+ }
177
+ return text.length > 400 ? `${text.slice(0, 400)}…` : text;
178
+ }
179
+ /** How long a connection test may take. */
180
+ const PING_MS = 30_000;
181
+ export class ModelGateway {
182
+ #models;
183
+ #keys;
184
+ #custom = new Map();
185
+ #retry;
186
+ #idleMs;
187
+ constructor(options) {
188
+ this.#keys = options.keys;
189
+ this.#retry = options.retry ?? DEFAULT_RETRY;
190
+ this.#idleMs = options.idleMs ?? DEFAULT_IDLE_MS;
191
+ this.#models = createModels({
192
+ credentials: new KeychainCredentials(options.keys),
193
+ authContext: NO_AMBIENT_AUTH,
194
+ // No attribution headers, and our own User-Agent (AGENTS.md "数据在本机").
195
+ identity: { userAgent: { custom: `yew/${options.version}` }, attribution: null },
196
+ });
197
+ const providers = options.providers ?? builtinProviders();
198
+ for (const provider of providers) {
199
+ if (!HIDDEN_PROVIDERS.has(provider.id))
200
+ this.#models.setProvider(provider);
201
+ }
202
+ this.setCustomProviders(options.custom ?? []);
203
+ }
204
+ setCustomProviders(configs) {
205
+ for (const id of this.#custom.keys())
206
+ this.#models.deleteProvider(id);
207
+ this.#custom.clear();
208
+ for (const config of configs) {
209
+ if (this.#models.getProvider(config.id)) {
210
+ throw invalid(`${config.id} is already a built-in provider.`, "id");
211
+ }
212
+ this.#custom.set(config.id, config);
213
+ this.#models.setProvider(customProvider(config));
214
+ }
215
+ }
216
+ providers() {
217
+ return this.#models.getProviders().map((provider) => {
218
+ const custom = this.#custom.get(provider.id);
219
+ return {
220
+ id: provider.id,
221
+ name: provider.name,
222
+ region: custom ? "custom" : CN_PROVIDERS.has(provider.id) ? "cn" : "global",
223
+ baseUrl: provider.baseUrl ?? custom?.baseUrl ?? "",
224
+ keyRequired: custom ? custom.keyRequired : true,
225
+ custom: custom !== undefined,
226
+ };
227
+ });
228
+ }
229
+ provider(id) {
230
+ return this.providers().find((provider) => provider.id === id);
231
+ }
232
+ models(provider) {
233
+ return this.#models.getModels(provider).map(toInfo);
234
+ }
235
+ model(provider, id) {
236
+ const model = this.#models.getModel(provider, id);
237
+ return model ? toInfo(model) : undefined;
238
+ }
239
+ /** Throws a 400 error when the provider or model is unknown. */
240
+ check(provider, id) {
241
+ if (!this.#models.getProvider(provider)) {
242
+ throw invalid(`Unknown provider: ${provider}.`, "provider");
243
+ }
244
+ if (!this.#models.getModel(provider, id)) {
245
+ throw invalid(`${provider} has no model ${id}.`, "model");
246
+ }
247
+ }
248
+ saveKey(provider, key) {
249
+ return this.#keys.set(provider, key);
250
+ }
251
+ deleteKey(provider) {
252
+ return this.#keys.delete(provider);
253
+ }
254
+ async hasKey(provider) {
255
+ const custom = this.#custom.get(provider);
256
+ if (custom && !custom.keyRequired)
257
+ return true;
258
+ return (await this.#keys.get(provider)) !== null;
259
+ }
260
+ /**
261
+ * Streams one model response. `onDraft` sees the response so far after each update; it is the same
262
+ * object every time and keeps changing, so copy it if you keep it. Resolves with the final message,
263
+ * whose stopReason is "error" or "aborted" when the request failed or was stopped.
264
+ *
265
+ * A failure that may pass (the provider is overloaded or limits the rate, the connection dropped,
266
+ * the response stalled) is tried again here, so callers only see a failure that stayed. A context
267
+ * that does not fit is not tried again: the caller shortens it.
268
+ */
269
+ async stream(request, onDraft) {
270
+ const model = this.#models.getModel(request.provider, request.model);
271
+ if (!model)
272
+ throw invalid(`${request.provider} has no model ${request.model}.`, "model");
273
+ if (!(await this.hasKey(request.provider))) {
274
+ throw new YewError("missing-api-key", 400, `There is no API key for ${request.provider}. Add one in Settings, or run: yew key set ${request.provider}`, { provider: request.provider });
275
+ }
276
+ const context = {
277
+ systemPrompt: request.systemPrompt,
278
+ messages: request.messages,
279
+ tools: request.tools,
280
+ };
281
+ // One layer of retries, the one below that the person can see: the provider SDKs' own are off.
282
+ const options = { cacheRetention: "short", maxRetries: 0 };
283
+ if (request.sessionId)
284
+ options.sessionId = request.sessionId;
285
+ if (model.reasoning && request.thinking !== "off") {
286
+ const level = clampThinkingLevel(model, request.thinking);
287
+ if (level !== "off")
288
+ options.reasoning = level;
289
+ }
290
+ for (let attempt = 1;; attempt++) {
291
+ const message = await this.#once(model, context, options, request.signal, onDraft);
292
+ if (message.stopReason !== "error" ||
293
+ attempt > this.#retry.maxRetries ||
294
+ isContextOverflow(message, model.contextWindow) ||
295
+ !isRetryableAssistantError(message)) {
296
+ return message;
297
+ }
298
+ const delayMs = retryDelayMs({ baseDelayMs: this.#retry.baseDelayMs, maxAgentDelayMs: MAX_RETRY_DELAY_MS }, attempt);
299
+ request.onRetry?.({
300
+ attempt,
301
+ of: this.#retry.maxRetries,
302
+ delayMs,
303
+ error: message.errorMessage ?? "",
304
+ });
305
+ if (!(await pause(delayMs, request.signal))) {
306
+ const { errorMessage: _errorMessage, ...rest } = message;
307
+ return { ...rest, stopReason: "aborted" };
308
+ }
309
+ }
310
+ }
311
+ /** One request. A response that goes quiet for too long ends as a failure that can be tried again. */
312
+ async #once(model, context, options, signal, onDraft) {
313
+ const quiet = new AbortController();
314
+ let stalled = false;
315
+ let timer = null;
316
+ const watch = () => {
317
+ if (timer)
318
+ clearTimeout(timer);
319
+ timer = setTimeout(() => {
320
+ stalled = true;
321
+ quiet.abort();
322
+ }, this.#idleMs);
323
+ };
324
+ watch();
325
+ try {
326
+ const stream = this.#models.streamSimple(model, context, {
327
+ ...options,
328
+ signal: signal ? AbortSignal.any([signal, quiet.signal]) : quiet.signal,
329
+ });
330
+ const seen = { draft: null };
331
+ const read = async () => {
332
+ for await (const event of stream) {
333
+ watch();
334
+ if ("partial" in event)
335
+ seen.draft = event.partial;
336
+ if (onDraft && "partial" in event)
337
+ onDraft(event.partial);
338
+ }
339
+ return await stream.result();
340
+ };
341
+ // Not every transport ends its stream when it is aborted, so the wait ends here either way.
342
+ const gaveUp = new Promise((resolve) => quiet.signal.addEventListener("abort", () => resolve(null), { once: true }));
343
+ const reading = read();
344
+ reading.catch(() => { });
345
+ const message = await Promise.race([reading, gaveUp]);
346
+ if (message && (!stalled || signal?.aborted))
347
+ return message;
348
+ if (message && message.stopReason !== "aborted" && message.stopReason !== "error")
349
+ return message;
350
+ return {
351
+ ...(message ?? seen.draft ?? emptyMessage(model)),
352
+ stopReason: "error",
353
+ errorMessage: `The provider sent nothing for ${Math.round(this.#idleMs / 1000)} seconds (timed out).`,
354
+ };
355
+ }
356
+ finally {
357
+ if (timer)
358
+ clearTimeout(timer);
359
+ }
360
+ }
361
+ /**
362
+ * Sends one very short request with the stored key and times it: it shows that the address, the key
363
+ * and the model work from this computer, and how long the provider takes to start answering. It
364
+ * uses a few tokens. Without `modelId`, the provider's cheapest model is asked. Not tried again on
365
+ * failure: the failure is the result.
366
+ */
367
+ async ping(provider, modelId) {
368
+ const listed = this.#models.getModels(provider);
369
+ const model = modelId
370
+ ? this.#models.getModel(provider, modelId)
371
+ : [...listed].sort((a, b) => a.cost.input - b.cost.input || a.id.localeCompare(b.id))[0];
372
+ if (!model)
373
+ throw invalid(`${provider} has no model ${modelId ?? ""}.`.replace(" .", "."), "model");
374
+ if (!(await this.hasKey(provider))) {
375
+ throw new YewError("missing-api-key", 400, `There is no API key for ${provider}.`, {
376
+ provider,
377
+ });
378
+ }
379
+ const started = Date.now();
380
+ let firstMs = null;
381
+ const limit = new AbortController();
382
+ const timer = setTimeout(() => limit.abort(), PING_MS);
383
+ try {
384
+ const message = await this.#once(model, { systemPrompt: "", messages: [{ role: "user", content: "ping", timestamp: started }] }, { cacheRetention: "none", maxRetries: 0, maxTokens: 16 }, limit.signal, () => {
385
+ firstMs ??= Date.now() - started;
386
+ });
387
+ const totalMs = Date.now() - started;
388
+ if (message.stopReason === "error" || message.stopReason === "aborted") {
389
+ return {
390
+ ok: false,
391
+ model: model.id,
392
+ firstMs: null,
393
+ totalMs,
394
+ error: limit.signal.aborted
395
+ ? `No answer within ${PING_MS / 1000} seconds.`
396
+ : plainError(message.errorMessage ?? "The request failed."),
397
+ };
398
+ }
399
+ return { ok: true, model: model.id, firstMs: firstMs ?? totalMs, totalMs };
400
+ }
401
+ finally {
402
+ clearTimeout(timer);
403
+ }
404
+ }
405
+ /** True when a failed response means the conversation no longer fits the model's context window. */
406
+ isContextOverflow(message, provider, model) {
407
+ const info = this.#models.getModel(provider, model);
408
+ return isContextOverflow(message, info?.contextWindow);
409
+ }
410
+ }
@@ -0,0 +1,34 @@
1
+ import { mkdirSync, realpathSync, statSync } from "node:fs";
2
+ import { homedir } from "node:os";
3
+ import { isAbsolute, join, resolve } from "node:path";
4
+ import { invalid } from "./errors.js";
5
+ /** Yew's data directory: `YEW_HOME` when set, otherwise `~/.yew`. */
6
+ export function yewHome(env = process.env, user = homedir()) {
7
+ const custom = env.YEW_HOME;
8
+ return custom ? resolve(custom) : join(user, ".yew");
9
+ }
10
+ /** Creates the data directory readable only by the current user. */
11
+ export function ensureDir(path) {
12
+ mkdirSync(path, { recursive: true, mode: 0o700 });
13
+ return path;
14
+ }
15
+ /** The database file. */
16
+ export function dbPath(home) {
17
+ return join(home, "yew.db");
18
+ }
19
+ /** Resolves `~`, requires an existing folder and stores its real path, so permission checks compare like with like. */
20
+ export function resolveWorkingFolder(path) {
21
+ const expanded = path === "~" ? homedir() : path.startsWith("~/") ? join(homedir(), path.slice(2)) : path;
22
+ if (!isAbsolute(expanded))
23
+ throw invalid("The working folder must be an absolute path.", "cwd");
24
+ let real;
25
+ try {
26
+ real = realpathSync(expanded);
27
+ }
28
+ catch {
29
+ throw invalid(`The folder ${path} does not exist.`, "cwd");
30
+ }
31
+ if (!statSync(real).isDirectory())
32
+ throw invalid(`${path} is not a folder.`, "cwd");
33
+ return real;
34
+ }
@@ -0,0 +1,249 @@
1
+ import { dirname } from "node:path";
2
+ import { permissionModeOf, } from "../../shared/types.js";
3
+ import { isInside, isSecretPath, pathMatches, resolvePath, secretLocations } from "./paths.js";
4
+ import { isReadOnly, matchesPrefix, mustConfirmReason, parseShell, pathArguments, rememberPrefix, unwrap, } from "./shell.js";
5
+ const SAFE_REDIRECT_TARGETS = new Set(["/dev/null", "/dev/stdout", "/dev/stderr", "&1", "&2", "-"]);
6
+ /**
7
+ * The rules a new session takes over from the one it was opened by or copied from. "Always allow"
8
+ * for the tools of Yew's own browser and computer control was given for one session, with its scope
9
+ * said at the time (spec 21.2): it does not travel.
10
+ */
11
+ export function inheritableRules(rules) {
12
+ return rules.filter((rule) => !(rule.effect === "allow" && /^mcp__(browser|computer)__/.test(rule.tool)));
13
+ }
14
+ function strongest(effects) {
15
+ if (effects.includes("deny"))
16
+ return "deny";
17
+ if (effects.includes("ask"))
18
+ return "ask";
19
+ if (effects.includes("allow"))
20
+ return "allow";
21
+ return null;
22
+ }
23
+ /**
24
+ * Decides whether a tool call may run, must be asked about, or is refused (M1 plan, section 8).
25
+ * Order: credential locations are refused; must-confirm operations always ask and cannot be remembered
26
+ * (spec 21.2); then the session's rules, where deny beats ask beats allow; then the built-in defaults.
27
+ * Text in the transcript never counts as approval: only the person's answer in Yew does.
28
+ */
29
+ export class PermissionEngine {
30
+ #secrets;
31
+ /** Folders outside the working folder that may be read without asking: memory and skill folders. */
32
+ readRoots = () => [];
33
+ constructor(yewHome) {
34
+ this.#secrets = secretLocations(yewHome);
35
+ }
36
+ /**
37
+ * `flagged` is the model's own judgment, through the `confirm` argument: in the automatic mode a
38
+ * flagged call is asked about, others run. Must-confirm operations, explicit "ask" rules and commands
39
+ * Yew cannot read are asked about in every mode.
40
+ */
41
+ decide(subject, session, flagged = false) {
42
+ switch (subject.kind) {
43
+ case "none":
44
+ return { effect: "allow" };
45
+ case "read":
46
+ case "write":
47
+ return this.#files(subject, session, flagged);
48
+ case "command":
49
+ return this.#command(subject, session, flagged);
50
+ case "url":
51
+ return this.#url(subject, session, flagged);
52
+ case "act":
53
+ return this.#act(subject, session, flagged);
54
+ }
55
+ }
56
+ /** An approval Yew asks for only by default; the session's mode settles it. */
57
+ #byDefault(session, flagged, tool, summary, remember) {
58
+ const mode = permissionModeOf(session.options);
59
+ if (mode === "skip" || (mode === "auto" && !flagged))
60
+ return { effect: "allow" };
61
+ return this.#ask(tool, summary, undefined, remember);
62
+ }
63
+ #rulesFor(session, tool, matches) {
64
+ const effects = session.rules
65
+ .filter((rule) => (rule.tool === tool || rule.tool === "*") && (!rule.pattern || matches(rule)))
66
+ .map((rule) => rule.effect);
67
+ return strongest(effects);
68
+ }
69
+ #files(subject, session, flagged) {
70
+ const cwd = session.cwd;
71
+ for (const path of subject.paths) {
72
+ if (isSecretPath(path, this.#secrets)) {
73
+ const verb = subject.kind === "read" ? "read" : "change";
74
+ return {
75
+ effect: "deny",
76
+ reason: `${path} holds credentials or Yew's own data; sessions cannot ${verb} it.`,
77
+ };
78
+ }
79
+ }
80
+ const effects = subject.paths.map((path) => this.#rulesFor(session, subject.tool, (rule) => pathMatches(path, rule.pattern ?? "", cwd)));
81
+ if (effects.includes("deny")) {
82
+ return { effect: "deny", reason: "A rule in this session refuses this." };
83
+ }
84
+ const outside = subject.paths.filter((path) => !cwd || !isInside(path, cwd));
85
+ if (subject.kind === "write") {
86
+ if (outside.length > 0) {
87
+ return this.#ask(subject.tool, subject.summary, ["write-outside"]);
88
+ }
89
+ if (effects.includes("ask"))
90
+ return this.#ask(subject.tool, subject.summary);
91
+ if (effects.every((effect) => effect === "allow"))
92
+ return { effect: "allow" };
93
+ return this.#byDefault(session, flagged, subject.tool, subject.summary, [
94
+ { effect: "allow", tool: subject.tool, pattern: "**" },
95
+ ]);
96
+ }
97
+ if (effects.includes("ask"))
98
+ return this.#ask(subject.tool, subject.summary);
99
+ const roots = this.readRoots();
100
+ const needed = subject.paths.filter((path, index) => effects[index] !== "allow" &&
101
+ (!cwd || !isInside(path, cwd)) &&
102
+ !roots.some((root) => isInside(path, root)));
103
+ if (needed.length === 0)
104
+ return { effect: "allow" };
105
+ // Remember the folder: allowing ~/memory/a.md once usually means the person trusts ~/memory.
106
+ const folders = [
107
+ ...new Set(needed.map((path) => (subject.tool === "read" ? dirname(path) : path))),
108
+ ];
109
+ return this.#byDefault(session, flagged, subject.tool, subject.summary, folders.map((pattern) => ({ effect: "allow", tool: subject.tool, pattern })));
110
+ }
111
+ #command(subject, session, flagged) {
112
+ const cwd = session.cwd;
113
+ const parsed = parseShell(subject.command);
114
+ const commands = [];
115
+ let privileged = false;
116
+ let understood = parsed.understood;
117
+ let dynamic = false;
118
+ const redirectTargets = [];
119
+ for (const segment of parsed.segments) {
120
+ dynamic ||= segment.dynamic;
121
+ for (const redirect of segment.redirects) {
122
+ const duplicatesDescriptor = redirect.op.endsWith("&") && /^(\d+|-)$/.test(redirect.target);
123
+ if (redirect.op.includes(">") &&
124
+ !duplicatesDescriptor &&
125
+ !SAFE_REDIRECT_TARGETS.has(redirect.target)) {
126
+ redirectTargets.push(redirect.target);
127
+ }
128
+ }
129
+ const inner = unwrap(segment.argv);
130
+ commands.push(...inner.commands);
131
+ privileged ||= inner.privileged;
132
+ understood &&= inner.understood;
133
+ }
134
+ const reasons = new Set();
135
+ for (const argv of commands) {
136
+ const reason = mustConfirmReason(argv);
137
+ if (reason)
138
+ reasons.add(reason);
139
+ // File tools cannot touch these places at all; a command that names one asks every time.
140
+ const secret = pathArguments(argv).some((arg) => {
141
+ const path = resolvePath(arg, cwd);
142
+ return path !== null && isSecretPath(path, this.#secrets);
143
+ });
144
+ if (secret)
145
+ reasons.add("credentials");
146
+ }
147
+ if (privileged)
148
+ reasons.add("admin");
149
+ for (const target of redirectTargets) {
150
+ const path = resolvePath(target, cwd);
151
+ if (!path || !cwd || !isInside(path, cwd))
152
+ reasons.add("write-outside");
153
+ else if (isSecretPath(path, this.#secrets))
154
+ reasons.add("write-credentials");
155
+ }
156
+ const ruleFor = (argv) => this.#rulesFor(session, subject.tool, (r) => matchesPrefix(argv, r.pattern ?? ""));
157
+ if (commands.some((argv) => ruleFor(argv) === "deny")) {
158
+ return { effect: "deny", reason: "A rule in this session refuses this command." };
159
+ }
160
+ if (reasons.size > 0)
161
+ return this.#ask(subject.tool, subject.command, [...reasons]);
162
+ if (!understood || dynamic)
163
+ return this.#ask(subject.tool, subject.command);
164
+ const pending = [];
165
+ let ruleAsks = false;
166
+ for (const argv of commands) {
167
+ const rule = ruleFor(argv);
168
+ if (rule === "ask")
169
+ ruleAsks = true;
170
+ if (rule === "allow")
171
+ continue;
172
+ if (rule === null && this.#readOnlyHere(argv, cwd))
173
+ continue;
174
+ pending.push(argv);
175
+ }
176
+ if (ruleAsks)
177
+ return this.#ask(subject.tool, subject.command);
178
+ if (redirectTargets.length === 0 && pending.length === 0)
179
+ return { effect: "allow" };
180
+ const remember = [...new Set(pending.map(rememberPrefix))].map((pattern) => ({
181
+ effect: "allow",
182
+ tool: subject.tool,
183
+ pattern,
184
+ }));
185
+ return this.#byDefault(session, flagged, subject.tool, subject.command, pending.length === 0 ? undefined : remember);
186
+ }
187
+ #readOnlyHere(argv, cwd) {
188
+ if (!cwd || !isReadOnly(argv))
189
+ return false;
190
+ return pathArguments(argv).every((arg) => {
191
+ const path = resolvePath(arg, cwd);
192
+ return path !== null && isInside(path, cwd) && !isSecretPath(path, this.#secrets);
193
+ });
194
+ }
195
+ #url(subject, session, flagged) {
196
+ let host;
197
+ try {
198
+ host = new URL(subject.url).hostname.toLowerCase();
199
+ }
200
+ catch {
201
+ return { effect: "deny", reason: "This is not a valid URL." };
202
+ }
203
+ const rule = this.#rulesFor(session, subject.tool, (r) => {
204
+ const pattern = (r.pattern ?? "").toLowerCase();
205
+ return pattern.startsWith("*.")
206
+ ? host.endsWith(pattern.slice(1)) || host === pattern.slice(2)
207
+ : host === pattern;
208
+ });
209
+ if (rule === "deny")
210
+ return { effect: "deny", reason: `A rule in this session refuses ${host}.` };
211
+ if (rule === "allow")
212
+ return { effect: "allow" };
213
+ if (rule === "ask")
214
+ return this.#ask(subject.tool, subject.url);
215
+ return this.#byDefault(session, flagged, subject.tool, subject.url, [
216
+ { effect: "allow", tool: subject.tool, pattern: host },
217
+ ]);
218
+ }
219
+ #act(subject, session, flagged) {
220
+ const rule = this.#rulesFor(session, subject.tool, (r) => !r.pattern || r.pattern === subject.scope);
221
+ if (rule === "deny")
222
+ return { effect: "deny", reason: "A rule in this session refuses this." };
223
+ if (subject.reasons && subject.reasons.length > 0) {
224
+ return this.#ask(subject.tool, subject.summary, subject.reasons);
225
+ }
226
+ if (rule === "allow")
227
+ return { effect: "allow" };
228
+ const remember = [
229
+ subject.scope
230
+ ? { effect: "allow", tool: subject.tool, pattern: subject.scope }
231
+ : { effect: "allow", tool: subject.tool },
232
+ ];
233
+ if (rule === "ask" || subject.always) {
234
+ const decision = this.#ask(subject.tool, subject.summary, undefined, remember);
235
+ if (subject.warning && decision.effect === "ask")
236
+ decision.request.warning = subject.warning;
237
+ return decision;
238
+ }
239
+ return this.#byDefault(session, flagged, subject.tool, subject.summary, remember);
240
+ }
241
+ #ask(tool, summary, reasons, remember) {
242
+ const request = { tool, summary, mustConfirm: reasons !== undefined };
243
+ if (reasons)
244
+ request.reasons = reasons;
245
+ else if (remember && remember.length > 0)
246
+ request.rememberRules = remember;
247
+ return { effect: "ask", request };
248
+ }
249
+ }