@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,601 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import { basename } from "node:path";
3
+ import { letterQuotaOf, PERMISSION_MODES, permissionModeOf, RECEIVE_POLICIES, THINKING_LEVELS, } from "../shared/types.js";
4
+ import { dollars, isBudget } from "./budget.js";
5
+ import { invalid } from "./errors.js";
6
+ import { resolveWorkingFolder } from "./paths.js";
7
+ import { isInside } from "./permissions/paths.js";
8
+ import { readSettings, resolveLogFile } from "./settings.js";
9
+ import { validateSessionName } from "./store.js";
10
+ const TIER_RANK = { organize: 0, configure: 1, widen: 2 };
11
+ const MODE_RANK = { manual: 1, auto: 2, skip: 3 };
12
+ const RECEIVE_RANK = { refuse: 0, hold: 1, accept: 2 };
13
+ const THEMES = ["system", "light", "dark"];
14
+ const LANGUAGES = ["system", "zh", "en"];
15
+ const MAX_WORKSPACES = 50;
16
+ function text(fields, key, max) {
17
+ const value = fields[key];
18
+ if (value === undefined)
19
+ return undefined;
20
+ if (typeof value !== "string")
21
+ throw invalid(`${key} must be text.`, key);
22
+ if (value.length > max)
23
+ throw invalid(`${key} is longer than ${max} characters.`, key);
24
+ return value;
25
+ }
26
+ function oneOf(fields, key, values) {
27
+ const value = fields[key];
28
+ if (value === undefined)
29
+ return undefined;
30
+ if (!values.includes(value))
31
+ throw invalid(`${key} must be one of ${values.join(", ")}.`, key);
32
+ return value;
33
+ }
34
+ function flag(fields, key) {
35
+ const value = fields[key];
36
+ if (value === undefined)
37
+ return undefined;
38
+ if (typeof value !== "boolean")
39
+ throw invalid(`${key} must be true or false.`, key);
40
+ return value;
41
+ }
42
+ function list(value, key) {
43
+ if (value === undefined)
44
+ return [];
45
+ if (!Array.isArray(value))
46
+ throw invalid(`${key} must be a list.`, key);
47
+ return value.map((item, index) => {
48
+ if (!item || typeof item !== "object" || Array.isArray(item)) {
49
+ throw invalid(`${key}[${index}] must be an object.`, key);
50
+ }
51
+ return item;
52
+ });
53
+ }
54
+ const none = (value) => value === "none" || value === "";
55
+ /** A daily spending limit from a call: dollars, 0 for none; undefined when not given. */
56
+ function budgetOf(value) {
57
+ if (value === undefined)
58
+ return undefined;
59
+ if (value !== 0 && (value === null || !isBudget(value))) {
60
+ throw invalid("daily_budget must be an amount in US dollars, or 0 for no limit.", "daily_budget");
61
+ }
62
+ return value === 0 ? null : value;
63
+ }
64
+ /**
65
+ * manage_yew: everything the settings interface can do, for the main conversation and the sessions the
66
+ * person allowed. A call is planned first (checked, worded and ranked without changing anything), so
67
+ * the permission card shows exactly what will happen; running it plans again and applies all or none.
68
+ */
69
+ export class Manager {
70
+ #deps;
71
+ constructor(deps) {
72
+ this.#deps = deps;
73
+ }
74
+ plan(by, args) {
75
+ return this.#work(by, args, false);
76
+ }
77
+ /** Applies the call in one transaction and says what changed. */
78
+ apply(by, args) {
79
+ return this.#deps.store.tx(() => this.#work(by, args, true).summary);
80
+ }
81
+ #work(by, args, commit) {
82
+ const store = this.#deps.store;
83
+ const steps = [];
84
+ const summary = [];
85
+ const reasons = new Set();
86
+ let tier = "organize";
87
+ const reach = (level) => {
88
+ if (TIER_RANK[level] > TIER_RANK[tier])
89
+ tier = level;
90
+ };
91
+ const widen = (reason, target) => {
92
+ if (target?.id === by.id) {
93
+ throw invalid("Only the person can give you more room; ask them to change it in your session settings.", "sessions");
94
+ }
95
+ reach("widen");
96
+ reasons.add(reason);
97
+ };
98
+ const add = (step, line) => {
99
+ steps.push(step);
100
+ summary.push(line);
101
+ };
102
+ // Teams first, so sessions in the same call can move into a team created here.
103
+ const created = new Map();
104
+ const teamRef = (ref) => {
105
+ const groups = store.listGroups();
106
+ const byId = groups.find((group) => group.id === ref);
107
+ if (byId)
108
+ return byId;
109
+ const named = groups.filter((group) => group.name === ref);
110
+ if (named.length > 1)
111
+ throw invalid(`Several teams are called "${ref}"; give its id.`, "team");
112
+ if (named[0])
113
+ return named[0];
114
+ const fresh = created.get(ref);
115
+ if (fresh)
116
+ return fresh;
117
+ throw invalid(`There is no team "${ref}"; create it in teams first.`, "team");
118
+ };
119
+ for (const change of list(args.teams, "teams")) {
120
+ const name = text(change, "name", 60)?.trim();
121
+ if (!name)
122
+ throw invalid("A team needs a name.", "teams");
123
+ const ref = text(change, "team", 100);
124
+ if (ref === undefined) {
125
+ const group = commit
126
+ ? store.createGroup(name)
127
+ : { id: `new:${name}`, name };
128
+ created.set(name, group);
129
+ add({ action: "create_team", name }, `Created the team "${name}" (id ${group.id}).`);
130
+ }
131
+ else {
132
+ const group = teamRef(ref);
133
+ if (group.name === name)
134
+ continue;
135
+ if (commit)
136
+ store.updateGroup(group.id, { name });
137
+ add({ action: "rename_team", from: group.name, to: name }, `Renamed the team "${group.name}" to "${name}".`);
138
+ }
139
+ }
140
+ const sessionRef = (ref) => {
141
+ let found;
142
+ try {
143
+ found = store.getSession(ref);
144
+ }
145
+ catch {
146
+ found = store.findSessionByName(ref);
147
+ }
148
+ if (!found)
149
+ throw invalid(`There is no session "${ref}".`, "session");
150
+ return found;
151
+ };
152
+ const parseModel = (value, key) => {
153
+ const slash = value.indexOf("/");
154
+ if (slash <= 0 || slash === value.length - 1) {
155
+ throw invalid(`${key} must look like "provider/model".`, key);
156
+ }
157
+ const provider = value.slice(0, slash);
158
+ const model = value.slice(slash + 1);
159
+ if (!this.#deps.modelExists(provider, model)) {
160
+ throw invalid(`There is no model ${value}; pick one the person has set up.`, key);
161
+ }
162
+ return { provider, model };
163
+ };
164
+ const folderOf = (value, key) => {
165
+ try {
166
+ return resolveWorkingFolder(value);
167
+ }
168
+ catch (error) {
169
+ throw invalid(error.message, key);
170
+ }
171
+ };
172
+ const main = this.#deps.mainId();
173
+ for (const change of list(args.sessions, "sessions")) {
174
+ const ref = text(change, "session", 200);
175
+ const name = text(change, "name", 200);
176
+ const teamValue = text(change, "team", 100);
177
+ const role = text(change, "role", 20_000);
178
+ const modelValue = text(change, "model", 300);
179
+ const thinking = oneOf(change, "thinking", THINKING_LEVELS);
180
+ const folderValue = text(change, "folder", 4096);
181
+ if (ref === undefined) {
182
+ // A new standing session: never more room than the session that creates it.
183
+ if (!name)
184
+ throw invalid("A new session needs a name.", "name");
185
+ const valid = validateSessionName(name);
186
+ if (store.findSessionByName(valid))
187
+ throw invalid(`"${valid}" is taken.`, "name");
188
+ const settings = readSettings(store);
189
+ const defaults = settings.defaultModel;
190
+ const model = modelValue
191
+ ? parseModel(modelValue, "model")
192
+ : { provider: defaults.provider, model: defaults.model };
193
+ const cwd = folderValue === undefined || none(folderValue) ? null : folderOf(folderValue, "folder");
194
+ const team = teamValue === undefined || none(teamValue) ? null : teamRef(teamValue);
195
+ reach("configure");
196
+ if (cwd && !(by.cwd && isInside(cwd, by.cwd)))
197
+ widen("change-folder", null);
198
+ // The default for new sessions, but never more than the session that creates it has.
199
+ const mode = MODE_RANK[permissionModeOf(by.options)] < MODE_RANK[settings.defaultPermissionMode]
200
+ ? permissionModeOf(by.options)
201
+ : settings.defaultPermissionMode;
202
+ if (commit) {
203
+ store.createSession({
204
+ name: valid,
205
+ kind: "standing",
206
+ groupId: team?.id ?? null,
207
+ responsibility: role ?? "",
208
+ cwd,
209
+ provider: model.provider,
210
+ model: model.model,
211
+ thinking: thinking ?? defaults.thinking,
212
+ rules: [],
213
+ options: { permissionMode: mode },
214
+ });
215
+ }
216
+ add({
217
+ action: "create_session",
218
+ name: valid,
219
+ folder: cwd,
220
+ model: `${model.provider}/${model.model}`,
221
+ team: team?.name ?? null,
222
+ role: role ?? "",
223
+ }, `Created the standing session "${valid}".`);
224
+ continue;
225
+ }
226
+ const target = sessionRef(ref);
227
+ const archived = flag(change, "archived");
228
+ if (target.archivedAt !== null && archived !== false) {
229
+ throw invalid(`"${target.name}" is archived; set archived to false to restore it.`, "session");
230
+ }
231
+ const patch = {};
232
+ const changed = [];
233
+ const note = (step, line) => {
234
+ changed.push(step);
235
+ add(step, line);
236
+ };
237
+ if (name !== undefined) {
238
+ const valid = validateSessionName(name);
239
+ const other = store.findSessionByName(valid);
240
+ if (other && other.id !== target.id)
241
+ throw invalid(`"${valid}" is taken.`, "name");
242
+ if (valid !== target.name) {
243
+ patch.name = valid;
244
+ patch.options = { ...patch.options, autoName: false };
245
+ note({ action: "rename_session", from: target.name, to: valid }, `Renamed "${target.name}" to "${valid}".`);
246
+ }
247
+ }
248
+ if (teamValue !== undefined) {
249
+ const team = none(teamValue) ? null : teamRef(teamValue);
250
+ if ((team?.id ?? null) !== target.groupId) {
251
+ patch.groupId = team?.id ?? null;
252
+ note({ action: "move_to_team", sessions: [target.name], team: team?.name ?? null }, team
253
+ ? `Moved "${target.name}" into the team "${team.name}".`
254
+ : `Took "${target.name}" out of its team.`);
255
+ }
256
+ }
257
+ const pinned = flag(change, "pinned");
258
+ if (pinned !== undefined && pinned !== target.pinned) {
259
+ patch.pinned = pinned;
260
+ note({ action: "pin", session: target.name, on: pinned }, `${pinned ? "Pinned" : "Unpinned"} "${target.name}".`);
261
+ }
262
+ if (role !== undefined && role.trim() !== target.responsibility.trim()) {
263
+ reach("configure");
264
+ patch.responsibility = role;
265
+ note({ action: "set_role", session: target.name, role }, `Changed the role of "${target.name}".`);
266
+ }
267
+ if (modelValue !== undefined || thinking !== undefined) {
268
+ const model = modelValue ? parseModel(modelValue, "model") : target;
269
+ const from = `${target.provider}/${target.model}`;
270
+ const to = `${model.provider}/${model.model}`;
271
+ if (to !== from || (thinking && thinking !== target.thinking)) {
272
+ reach("configure");
273
+ patch.provider = model.provider;
274
+ patch.model = model.model;
275
+ if (thinking)
276
+ patch.thinking = thinking;
277
+ note({
278
+ action: "set_model",
279
+ session: target.name,
280
+ from,
281
+ to: thinking ? `${to} (${thinking})` : to,
282
+ }, `Set the model of "${target.name}" to ${to}${thinking ? ` with ${thinking} thinking` : ""}.`);
283
+ }
284
+ }
285
+ if (folderValue !== undefined) {
286
+ const cwd = none(folderValue) ? null : folderOf(folderValue, "folder");
287
+ if (cwd !== target.cwd) {
288
+ // No folder narrows what it can touch; any other folder is a new reach.
289
+ if (cwd === null)
290
+ reach("configure");
291
+ else
292
+ widen("change-folder", target);
293
+ patch.cwd = cwd;
294
+ note({ action: "set_folder", session: target.name, from: target.cwd, to: cwd }, `Set the working folder of "${target.name}" to ${cwd ?? "none (a chat)"}.`);
295
+ }
296
+ }
297
+ const options = { ...target.options };
298
+ const permission = oneOf(change, "permission", PERMISSION_MODES);
299
+ const currentMode = permissionModeOf(target.options);
300
+ if (permission !== undefined && permission !== currentMode) {
301
+ if (MODE_RANK[permission] > MODE_RANK[currentMode])
302
+ widen("widen-permission", target);
303
+ else
304
+ reach("configure");
305
+ options.permissionMode = permission;
306
+ note({ action: "set_permission", session: target.name, from: currentMode, to: permission }, `Set the permission mode of "${target.name}" to ${permission}.`);
307
+ }
308
+ const receive = oneOf(change, "receive", RECEIVE_POLICIES);
309
+ const currentReceive = target.options.receive ?? "accept";
310
+ if (receive !== undefined && receive !== currentReceive) {
311
+ if (RECEIVE_RANK[receive] > RECEIVE_RANK[currentReceive])
312
+ widen("open-letters", target);
313
+ else
314
+ reach("configure");
315
+ options.receive = receive;
316
+ note({ action: "set_receive", session: target.name, from: currentReceive, to: receive }, `Set how "${target.name}" receives letters to ${receive}.`);
317
+ }
318
+ const quotaValue = change.letter_quota;
319
+ if (quotaValue !== undefined) {
320
+ if (typeof quotaValue !== "number" ||
321
+ !Number.isInteger(quotaValue) ||
322
+ quotaValue < 0 ||
323
+ quotaValue > 100) {
324
+ throw invalid("letter_quota must be a whole number from 1 to 100, or 0 for the default.", "letter_quota");
325
+ }
326
+ const isMain = target.id === main;
327
+ const from = letterQuotaOf(target.options, isMain);
328
+ const next = quotaValue === 0 ? null : quotaValue;
329
+ const to = letterQuotaOf({ letterQuota: next }, isMain);
330
+ if (to !== from) {
331
+ if (to > from)
332
+ widen("raise-quota", target);
333
+ else
334
+ reach("configure");
335
+ options.letterQuota = next;
336
+ note({ action: "set_letter_quota", session: target.name, from, to }, `Set the letter quota of "${target.name}" to ${to}.`);
337
+ }
338
+ }
339
+ const budgetValue = budgetOf(change.daily_budget);
340
+ if (budgetValue !== undefined && budgetValue !== (target.options.dailyBudget ?? null)) {
341
+ const from = target.options.dailyBudget ?? null;
342
+ // More room to spend, or no limit at all, is for the person to allow.
343
+ if (budgetValue === null || (from !== null && budgetValue > from)) {
344
+ widen("raise-budget", target);
345
+ }
346
+ else
347
+ reach("configure");
348
+ options.dailyBudget = budgetValue;
349
+ options.budgetExtra = null;
350
+ note({ action: "set_daily_budget", session: target.name, from, to: budgetValue }, budgetValue === null
351
+ ? `Removed the daily spending limit of "${target.name}".`
352
+ : `Set the daily spending limit of "${target.name}" to ${dollars(budgetValue)}.`);
353
+ }
354
+ for (const [key, right] of [
355
+ ["can_post_cards", "cards"],
356
+ ["can_manage", "manage"],
357
+ ]) {
358
+ const on = flag(change, key);
359
+ const current = right === "cards"
360
+ ? target.options.canPostCards === true
361
+ : target.options.canOrganize === true;
362
+ if (on === undefined || on === current)
363
+ continue;
364
+ if (on)
365
+ widen("grant-rights", target);
366
+ else
367
+ reach("configure");
368
+ if (right === "cards")
369
+ options.canPostCards = on;
370
+ else
371
+ options.canOrganize = on;
372
+ note({ action: "set_right", session: target.name, right, on }, `${on ? "Gave" : "Took from"} "${target.name}" the right to ${right === "cards" ? "post cards" : "manage Yew"}.`);
373
+ }
374
+ if (archived !== undefined && archived !== (target.archivedAt !== null)) {
375
+ if (archived && target.id === by.id)
376
+ throw invalid("You cannot archive yourself.", "archived");
377
+ if (archived && target.id === main) {
378
+ throw invalid("The main conversation cannot be archived; choose another one first.", "archived");
379
+ }
380
+ reach("configure");
381
+ patch.archived = archived;
382
+ note({ action: "archive", session: target.name, on: archived }, `${archived ? "Archived" : "Restored"} "${target.name}".`);
383
+ }
384
+ if (JSON.stringify(options) !== JSON.stringify(target.options)) {
385
+ patch.options = { ...options, ...patch.options };
386
+ }
387
+ if (commit && changed.length > 0) {
388
+ store.updateSession(target.id, patch);
389
+ if (patch.archived === true)
390
+ this.#deps.onArchived(target.id);
391
+ store.appendEntry(target.id, {
392
+ role: "notice",
393
+ notice: {
394
+ kind: "changed",
395
+ text: `${by.name}: ${changed.length} change(s)`,
396
+ data: {
397
+ by: by.name,
398
+ steps: changed,
399
+ },
400
+ timestamp: Date.now(),
401
+ },
402
+ });
403
+ }
404
+ }
405
+ // Scheduled tasks: creating and changing one configures; a run mode wider than its session's widens.
406
+ const book = this.#deps.schedules;
407
+ for (const change of list(args.schedules, "schedules")) {
408
+ const ref = text(change, "schedule", 200);
409
+ const fields = {};
410
+ const name = text(change, "name", 200);
411
+ const cron = text(change, "cron", 200);
412
+ const session = text(change, "session", 200);
413
+ const message = text(change, "message", 20_000);
414
+ const permission = oneOf(change, "permission", ["session", ...PERMISSION_MODES]);
415
+ const catchUp = flag(change, "catch_up");
416
+ const enabled = flag(change, "enabled");
417
+ if (name !== undefined)
418
+ fields.name = name;
419
+ if (cron !== undefined)
420
+ fields.cron = cron;
421
+ if (session !== undefined)
422
+ fields.session = session;
423
+ if (message !== undefined)
424
+ fields.message = message;
425
+ if (permission !== undefined) {
426
+ fields.permissionMode = permission === "session" ? null : permission;
427
+ }
428
+ if (catchUp !== undefined)
429
+ fields.catchUp = catchUp;
430
+ if (enabled !== undefined)
431
+ fields.enabled = enabled;
432
+ const current = ref === undefined ? null : book.find(ref);
433
+ if (ref !== undefined && !current)
434
+ throw invalid(`There is no scheduled task "${ref}".`, "schedule");
435
+ const values = book.values(fields, current);
436
+ const target = store.getSession(values.sessionId);
437
+ reach("configure");
438
+ const mode = values.permissionMode;
439
+ if (mode && MODE_RANK[mode] > MODE_RANK[permissionModeOf(target.options)]) {
440
+ const before = current?.permissionMode ?? null;
441
+ // Wider than the session's own mode, and newly so: the person confirms it.
442
+ if (!current || before !== mode || current.sessionId !== values.sessionId) {
443
+ widen("widen-permission", target);
444
+ }
445
+ }
446
+ if (!current) {
447
+ if (commit)
448
+ book.create(fields);
449
+ add({
450
+ action: "create_schedule",
451
+ name: values.name,
452
+ cron: values.cron,
453
+ session: target.name,
454
+ message: values.message,
455
+ permission: mode,
456
+ }, `Created the scheduled task "${values.name}" (${values.cron}, to "${target.name}").`);
457
+ continue;
458
+ }
459
+ const step = {
460
+ action: "change_schedule",
461
+ schedule: current.name,
462
+ };
463
+ if (values.name !== current.name)
464
+ step.name = values.name;
465
+ if (values.cron !== current.cron)
466
+ step.cron = values.cron;
467
+ if (values.sessionId !== current.sessionId)
468
+ step.session = target.name;
469
+ if (values.message !== current.message)
470
+ step.message = values.message;
471
+ if (values.permissionMode !== current.permissionMode)
472
+ step.permission = values.permissionMode;
473
+ if (values.catchUp !== current.catchUp)
474
+ step.catchUp = values.catchUp;
475
+ if (values.enabled !== current.enabled)
476
+ step.enabled = values.enabled;
477
+ const changed = Object.keys(step).filter((key) => key !== "action" && key !== "schedule");
478
+ if (changed.length === 0)
479
+ continue;
480
+ if (commit)
481
+ book.update(current.id, fields);
482
+ add(step, changed.length === 1 && step.enabled !== undefined
483
+ ? `Turned ${step.enabled ? "on" : "off"} the scheduled task "${current.name}".`
484
+ : `Changed the scheduled task "${current.name}": ${changed.join(", ")}.`);
485
+ }
486
+ const settings = args.settings;
487
+ if (settings !== undefined) {
488
+ if (!settings || typeof settings !== "object" || Array.isArray(settings)) {
489
+ throw invalid("settings must be an object.", "settings");
490
+ }
491
+ const fields = settings;
492
+ const current = readSettings(store);
493
+ const mainValue = text(fields, "main", 200);
494
+ if (mainValue !== undefined) {
495
+ const target = none(mainValue) ? null : sessionRef(mainValue);
496
+ if (target?.archivedAt)
497
+ throw invalid("An archived session cannot be the main conversation.", "main");
498
+ if ((target?.id ?? null) !== current.mainSessionId) {
499
+ widen("change-main", target);
500
+ if (commit)
501
+ store.setSetting("mainSessionId", target?.id ?? null);
502
+ let from = null;
503
+ try {
504
+ from = current.mainSessionId ? store.getSession(current.mainSessionId).name : null;
505
+ }
506
+ catch {
507
+ from = null;
508
+ }
509
+ add({ action: "set_main", from, to: target?.name ?? null }, target
510
+ ? `Made "${target.name}" the main conversation.`
511
+ : "Left Yew without a main conversation.");
512
+ }
513
+ }
514
+ const defaultModel = text(fields, "default_model", 300);
515
+ const defaultThinking = oneOf(fields, "default_thinking", THINKING_LEVELS);
516
+ if (defaultModel !== undefined || defaultThinking !== undefined) {
517
+ const model = defaultModel
518
+ ? parseModel(defaultModel, "default_model")
519
+ : current.defaultModel;
520
+ const next = {
521
+ provider: model.provider,
522
+ model: model.model,
523
+ thinking: defaultThinking ?? current.defaultModel.thinking,
524
+ };
525
+ const from = `${current.defaultModel.provider}/${current.defaultModel.model} (${current.defaultModel.thinking})`;
526
+ const to = `${next.provider}/${next.model} (${next.thinking})`;
527
+ if (to !== from) {
528
+ reach("configure");
529
+ if (commit)
530
+ store.setSetting("defaultModel", next);
531
+ add({ action: "set_default_model", from, to }, `New sessions now use ${to}.`);
532
+ }
533
+ }
534
+ const budgetValue = budgetOf(fields.daily_budget);
535
+ if (budgetValue !== undefined && budgetValue !== current.dailyBudget) {
536
+ const from = current.dailyBudget;
537
+ if (budgetValue === null || (from !== null && budgetValue > from)) {
538
+ widen("raise-budget", null);
539
+ }
540
+ else
541
+ reach("configure");
542
+ if (commit) {
543
+ store.setSetting("dailyBudget", budgetValue);
544
+ store.setSetting("budgetExtra", null);
545
+ }
546
+ add({ action: "set_daily_budget", session: null, from, to: budgetValue }, budgetValue === null
547
+ ? "Removed the daily spending limit for all sessions together."
548
+ : `Set the daily spending limit for all sessions together to ${dollars(budgetValue)}.`);
549
+ }
550
+ const log = text(fields, "decision_log", 4096);
551
+ if (log !== undefined) {
552
+ const path = none(log) ? null : resolveLogFile(log);
553
+ if (path !== current.decisionLog) {
554
+ widen("decision-log", null);
555
+ if (commit)
556
+ store.setSetting("decisionLog", path);
557
+ add({ action: "set_decision_log", from: current.decisionLog, to: path }, path
558
+ ? `Decisions are now added to ${path}.`
559
+ : "Decisions are no longer written to a file.");
560
+ }
561
+ }
562
+ let workspaces = current.workspaces;
563
+ const addFolder = text(fields, "add_workspace", 4096);
564
+ if (addFolder !== undefined) {
565
+ const path = folderOf(addFolder, "add_workspace");
566
+ if (!workspaces.some((workspace) => workspace.path === path)) {
567
+ if (workspaces.length >= MAX_WORKSPACES) {
568
+ throw invalid(`Up to ${MAX_WORKSPACES} workspaces can be saved.`, "add_workspace");
569
+ }
570
+ workspaces = [...workspaces, { id: randomUUID(), name: basename(path) || path, path }];
571
+ add({ action: "add_workspace", folder: path }, `Added the workspace ${path}.`);
572
+ }
573
+ }
574
+ const removeFolder = text(fields, "remove_workspace", 4096);
575
+ if (removeFolder !== undefined) {
576
+ const found = workspaces.find((workspace) => workspace.path === removeFolder || workspace.name === removeFolder);
577
+ if (!found)
578
+ throw invalid(`There is no workspace "${removeFolder}".`, "remove_workspace");
579
+ workspaces = workspaces.filter((workspace) => workspace !== found);
580
+ add({ action: "remove_workspace", folder: found.path }, `Removed the workspace ${found.path}.`);
581
+ }
582
+ if (commit && workspaces !== current.workspaces)
583
+ store.setSetting("workspaces", workspaces);
584
+ const theme = oneOf(fields, "theme", THEMES);
585
+ if (theme !== undefined && theme !== current.theme) {
586
+ if (commit)
587
+ store.setSetting("theme", theme);
588
+ add({ action: "set_theme", to: theme }, `Set the theme to ${theme}.`);
589
+ }
590
+ const language = oneOf(fields, "language", LANGUAGES);
591
+ if (language !== undefined && language !== current.language) {
592
+ if (commit)
593
+ store.setSetting("language", language);
594
+ add({ action: "set_language", to: language }, `Set the interface language to ${language}.`);
595
+ }
596
+ }
597
+ if (steps.length === 0)
598
+ throw invalid("Nothing to change: everything is already as asked.", "sessions");
599
+ return { steps, tier, reasons: [...reasons], summary };
600
+ }
601
+ }