@tenonai/yew 0.0.0-stage → 1.3.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 +273 -0
  2. package/LICENSE +21 -0
  3. package/README.md +64 -2
  4. package/THIRD-PARTY-NOTICES.md +122 -0
  5. package/bin/yew.js +9 -0
  6. package/dist/cli/app.js +211 -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 +58 -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 +127 -0
  16. package/dist/cli/serve.js +301 -0
  17. package/dist/cli/service.js +268 -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 +120 -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 +144 -0
  36. package/dist/server/db/database.js +49 -0
  37. package/dist/server/db/migrations.js +343 -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 +465 -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 +70 -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 +180 -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 +43 -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 +77 -0
  82. package/dist/server/skills.js +263 -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-BtK_SzpF.css +1 -0
  115. package/web/dist/assets/index-C0rnUFI-.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,345 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import { plainLine } from "../shared/messages.js";
3
+ import { dollars } from "./budget.js";
4
+ import { SETTING_DEFAULTS } from "./settings.js";
5
+ /** The setting each kind of item belongs to. */
6
+ const NOTIFY_KIND = {
7
+ permission: "waiting",
8
+ question: "waiting",
9
+ card: "waiting",
10
+ letter: "waiting",
11
+ error: "error",
12
+ review: "error",
13
+ task: "task",
14
+ schedule: "task",
15
+ budget: "budget",
16
+ };
17
+ const TITLE_MAX = 80;
18
+ const BODY_MAX = 240;
19
+ const clip = (text, max) => text.length > max ? `${text.slice(0, max - 1)}…` : text;
20
+ export class Notifier {
21
+ #options;
22
+ /** Items in the approval center that have not gone out yet. */
23
+ #waiting = new Map();
24
+ /** One-off news that has not gone out yet. */
25
+ #news = [];
26
+ /** Every item in the approval center seen so far, sent or not. */
27
+ #known = new Set();
28
+ /** For items that went out: the notification that carried each. */
29
+ #sent = new Map();
30
+ /** For each notification still on the phone: the items it carried that are still open. */
31
+ #notes = new Map();
32
+ #sessions = new Map();
33
+ #schedules = new Map();
34
+ #seenAt = 0;
35
+ #lastSentAt = 0;
36
+ #timer = null;
37
+ #interval = null;
38
+ #unsubscribe = null;
39
+ constructor(options) {
40
+ this.#options = {
41
+ now: Date.now,
42
+ settleMs: 1500,
43
+ windowMs: 10_000,
44
+ presenceMs: 2 * 60 * 1000,
45
+ keepMs: 10 * 60 * 1000,
46
+ ...options,
47
+ };
48
+ }
49
+ /** Starts from what is there now: only what comes after this is news. */
50
+ start() {
51
+ const { store } = this.#options;
52
+ for (const item of this.#pending())
53
+ this.#known.add(item.key);
54
+ for (const session of store.listSessions({ limit: 1000 }).items)
55
+ this.#remember(session);
56
+ for (const schedule of this.#options.schedules?.() ?? []) {
57
+ this.#schedules.set(schedule.id, runOf(schedule));
58
+ }
59
+ this.#unsubscribe = this.#options.bus.subscribe((event) => this.#heard(event));
60
+ // Presence runs out and windows end without any event.
61
+ this.#interval = setInterval(() => this.tick(), 5000);
62
+ this.#interval.unref();
63
+ }
64
+ stop() {
65
+ this.#unsubscribe?.();
66
+ this.#unsubscribe = null;
67
+ if (this.#timer)
68
+ clearTimeout(this.#timer);
69
+ if (this.#interval)
70
+ clearInterval(this.#interval);
71
+ this.#timer = null;
72
+ this.#interval = null;
73
+ }
74
+ /** Yew's page on the Mac is in front and in use. */
75
+ seen() {
76
+ this.#seenAt = this.#options.now();
77
+ }
78
+ /** Whether the person wants to hear about this kind of item. */
79
+ #wanted(kind) {
80
+ const chosen = this.#options.store.getSetting("notify", SETTING_DEFAULTS.notify);
81
+ return chosen[NOTIFY_KIND[kind]] ?? true;
82
+ }
83
+ #remember(session) {
84
+ this.#sessions.set(session.id, { status: session.status, outcome: session.outcome });
85
+ }
86
+ #heard(event) {
87
+ const now = this.#options.now();
88
+ if (event.scope === "session") {
89
+ const inner = event.event;
90
+ if (inner.type !== "entry" || inner.entry.role !== "notice")
91
+ return;
92
+ const notice = inner.entry.notice;
93
+ if (notice.kind !== "budget")
94
+ return;
95
+ if (!this.#wanted("budget"))
96
+ return;
97
+ const data = notice.data ?? {};
98
+ const amounts = typeof data.spent === "number" && typeof data.limit === "number"
99
+ ? `${dollars(data.spent)} / ${dollars(data.limit)}`
100
+ : "";
101
+ this.#news.push({
102
+ key: `budget:${inner.entry.id}`,
103
+ kind: "budget",
104
+ title: this.#name(event.sessionId),
105
+ body: amounts,
106
+ session: event.sessionId,
107
+ code: String(data.code ?? ""),
108
+ at: now,
109
+ });
110
+ this.#soon();
111
+ return;
112
+ }
113
+ if (event.scope !== "global")
114
+ return;
115
+ const global = event.event;
116
+ if (global.type === "approval" || global.type === "card" || global.type === "letter") {
117
+ this.#soon();
118
+ }
119
+ else if (global.type === "sessionSummary") {
120
+ const session = global.session;
121
+ const before = this.#sessions.get(session.id);
122
+ this.#remember(session);
123
+ if (!before || session.archivedAt !== null)
124
+ return;
125
+ if (session.status !== before.status && session.status === "error") {
126
+ this.#tell(session, "error", session.statusDetail ?? "");
127
+ }
128
+ else if (session.status !== before.status && session.status === "needs_review") {
129
+ this.#tell(session, "review", "");
130
+ }
131
+ else if (session.kind === "task" && session.outcome && !before.outcome) {
132
+ this.#tell(session, "task", "", session.outcome);
133
+ }
134
+ }
135
+ else if (global.type === "schedules") {
136
+ for (const schedule of global.schedules) {
137
+ const before = this.#schedules.get(schedule.id);
138
+ const run = runOf(schedule);
139
+ this.#schedules.set(schedule.id, run);
140
+ if (before === undefined || before === run)
141
+ continue;
142
+ if (schedule.lastStatus !== "done" && schedule.lastStatus !== "error")
143
+ continue;
144
+ if (!this.#wanted("schedule"))
145
+ continue;
146
+ this.#news.push({
147
+ key: `schedule:${schedule.id}:${schedule.lastRunAt}`,
148
+ kind: "schedule",
149
+ title: schedule.name,
150
+ body: plainLine(schedule.lastDetail ?? ""),
151
+ session: schedule.sessionId,
152
+ code: schedule.lastStatus,
153
+ at: now,
154
+ });
155
+ this.#soon();
156
+ }
157
+ }
158
+ else if (global.type === "sessionsDeleted") {
159
+ for (const id of global.ids)
160
+ this.#sessions.delete(id);
161
+ }
162
+ }
163
+ #tell(session, kind, body, code) {
164
+ if (!this.#wanted(kind))
165
+ return;
166
+ this.#news.push({
167
+ key: `${kind}:${session.id}:${this.#options.now()}`,
168
+ kind,
169
+ title: session.name,
170
+ body: plainLine(body),
171
+ session: session.id,
172
+ ...(code ? { code } : {}),
173
+ at: this.#options.now(),
174
+ });
175
+ this.#soon();
176
+ }
177
+ #name(sessionId) {
178
+ try {
179
+ return this.#options.store.getSession(sessionId).name;
180
+ }
181
+ catch {
182
+ return "";
183
+ }
184
+ }
185
+ #soon(ms = this.#options.settleMs) {
186
+ if (this.#timer)
187
+ return;
188
+ this.#timer = setTimeout(() => {
189
+ this.#timer = null;
190
+ this.tick();
191
+ }, ms);
192
+ this.#timer.unref();
193
+ }
194
+ /** What waits for the person in the approval center, as items. */
195
+ #pending() {
196
+ const { store, mail } = this.#options;
197
+ const now = this.#options.now();
198
+ const items = [];
199
+ for (const approval of store.allPendingApprovals()) {
200
+ items.push({
201
+ key: `approval:${approval.id}`,
202
+ kind: approval.kind,
203
+ title: this.#name(approval.sessionId),
204
+ body: askedIn(approval),
205
+ // A question asked in a reply is answered in its session; the rest in the approval center.
206
+ ...(approval.kind === "question" ? { session: approval.sessionId } : {}),
207
+ at: now,
208
+ });
209
+ }
210
+ for (const card of mail.pendingCards()) {
211
+ items.push({
212
+ key: `card:${card.id}`,
213
+ kind: "card",
214
+ title: `#${card.number} ${card.title}`,
215
+ body: plainLine(card.context),
216
+ at: now,
217
+ });
218
+ }
219
+ for (const letter of mail.heldLetters()) {
220
+ items.push({
221
+ key: `letter:${letter.id}`,
222
+ kind: "letter",
223
+ title: `${letter.fromName} → ${this.#name(letter.toSession)}`,
224
+ body: letter.summary,
225
+ at: now,
226
+ });
227
+ }
228
+ return items;
229
+ }
230
+ /** Looks at what changed and sends what is due. Runs after events and every few seconds. */
231
+ tick() {
232
+ const now = this.#options.now();
233
+ const pending = this.#pending();
234
+ const open = new Set(pending.map((item) => item.key));
235
+ for (const item of pending) {
236
+ if (this.#known.has(item.key))
237
+ continue;
238
+ this.#known.add(item.key);
239
+ // Not wanted now: it stays known, so turning the kind on later does not send old items.
240
+ if (this.#wanted(item.kind))
241
+ this.#waiting.set(item.key, item);
242
+ }
243
+ // Handled items: not sent any more, and their notifications come off the phone.
244
+ const cleared = [];
245
+ for (const key of [...this.#known]) {
246
+ if (open.has(key))
247
+ continue;
248
+ this.#known.delete(key);
249
+ this.#waiting.delete(key);
250
+ const note = this.#sent.get(key);
251
+ if (!note)
252
+ continue;
253
+ this.#sent.delete(key);
254
+ const left = this.#notes.get(note);
255
+ left?.delete(key);
256
+ if (left && left.size === 0) {
257
+ this.#notes.delete(note);
258
+ cleared.push(note);
259
+ }
260
+ }
261
+ if (cleared.length > 0) {
262
+ this.#options.send({ v: 1, clear: cleared, badge: open.size }, "background");
263
+ }
264
+ const present = now - this.#seenAt < this.#options.presenceMs;
265
+ // At the Mac you see the news there; what needs you waits until you have left.
266
+ if (present)
267
+ this.#news = [];
268
+ this.#news = this.#news.filter((item) => now - item.at < this.#options.keepMs);
269
+ if (present)
270
+ return;
271
+ // A kind turned off while its items were waiting to go out is dropped here.
272
+ for (const [key, item] of this.#waiting) {
273
+ if (!this.#wanted(item.kind))
274
+ this.#waiting.delete(key);
275
+ }
276
+ this.#news = this.#news.filter((item) => this.#wanted(item.kind));
277
+ const due = [...this.#waiting.values(), ...this.#news].filter((item) => now - item.at >= this.#options.settleMs);
278
+ if (due.length === 0) {
279
+ if (this.#waiting.size + this.#news.length > 0)
280
+ this.#soon();
281
+ return;
282
+ }
283
+ const wait = this.#lastSentAt + this.#options.windowMs - now;
284
+ if (wait > 0) {
285
+ this.#soon(wait);
286
+ return;
287
+ }
288
+ const id = randomUUID();
289
+ const first = due[0];
290
+ const note = due.length === 1
291
+ ? {
292
+ v: 1,
293
+ id,
294
+ kind: first.kind,
295
+ title: clip(first.title, TITLE_MAX),
296
+ body: clip(first.body, BODY_MAX),
297
+ ...(first.session ? { session: first.session } : {}),
298
+ ...(first.code ? { code: first.code } : {}),
299
+ badge: open.size,
300
+ }
301
+ : {
302
+ v: 1,
303
+ id,
304
+ kind: "many",
305
+ title: "",
306
+ body: clip(due
307
+ .map((item) => item.title)
308
+ .filter(Boolean)
309
+ .join(" · "), BODY_MAX),
310
+ n: due.length,
311
+ badge: open.size,
312
+ };
313
+ if (this.#options.send(note, "alert", id) === 0) {
314
+ // No relay or no phone right now: items that need you are tried again while they are open.
315
+ for (const [key, item] of this.#waiting) {
316
+ if (now - item.at >= this.#options.keepMs)
317
+ this.#waiting.delete(key);
318
+ }
319
+ return;
320
+ }
321
+ this.#lastSentAt = now;
322
+ const carried = new Set();
323
+ for (const item of due) {
324
+ if (!this.#waiting.delete(item.key))
325
+ continue;
326
+ this.#sent.set(item.key, id);
327
+ carried.add(item.key);
328
+ }
329
+ if (carried.size > 0)
330
+ this.#notes.set(id, carried);
331
+ const gone = new Set(due.map((item) => item.key));
332
+ this.#news = this.#news.filter((item) => !gone.has(item.key));
333
+ if (this.#waiting.size + this.#news.length > 0)
334
+ this.#soon();
335
+ }
336
+ }
337
+ /** A scheduled task's latest run, as one value that changes when a run ends. */
338
+ const runOf = (schedule) => `${schedule.lastRunAt ?? 0}:${schedule.lastStatus ?? ""}`;
339
+ /** One line of what an approval asks. */
340
+ function askedIn(approval) {
341
+ if (approval.kind === "permission")
342
+ return plainLine(approval.request.summary);
343
+ const request = approval.request;
344
+ return plainLine(request.questions?.[0]?.question ?? request.question ?? "");
345
+ }
@@ -0,0 +1,36 @@
1
+ import { execFile } from "node:child_process";
2
+ import { conflict, YewError } from "./errors.js";
3
+ export const runProgram = (command, args) => new Promise((resolve, reject) => {
4
+ execFile(command, args, { timeout: 15_000 }, (error) => (error ? reject(error) : resolve()));
5
+ });
6
+ export function revealKind(platform = process.platform) {
7
+ return platform === "darwin" ? "finder" : platform === "linux" ? "files" : null;
8
+ }
9
+ /**
10
+ * The program that shows a folder in the system's file manager, or null where Yew knows none.
11
+ * `inside` opens the folder itself; otherwise Finder opens the folder that holds it, with it selected.
12
+ */
13
+ export function revealCommand(dir, platform = process.platform, inside = false) {
14
+ if (platform === "darwin")
15
+ return { command: "open", args: inside ? [dir] : ["-R", dir] };
16
+ if (platform === "linux")
17
+ return { command: "xdg-open", args: [dir] };
18
+ return null;
19
+ }
20
+ /**
21
+ * Shows a folder in the file manager. The caller decides which folder: it is never a path taken from
22
+ * a request. `field` names the request field in the problem when this system has no file manager
23
+ * Yew knows.
24
+ */
25
+ export async function showInFileManager(dir, options) {
26
+ const program = revealCommand(dir, options.platform, options.inside);
27
+ if (!program) {
28
+ throw conflict("Showing a folder in the file manager works on macOS and Linux.", { field: options.field }, "not-on-this-system");
29
+ }
30
+ try {
31
+ await (options.run ?? runProgram)(program.command, program.args);
32
+ }
33
+ catch {
34
+ throw new YewError("reveal-failed", 500, "The folder could not be shown.");
35
+ }
36
+ }
@@ -0,0 +1,17 @@
1
+ import { VERSION } from "../shared/version.js";
2
+ import { openDatabase } from "./db/database.js";
3
+ import { EventBus } from "./events.js";
4
+ import { systemKeys } from "./keys.js";
5
+ import { ModelGateway } from "./model/gateway.js";
6
+ import { dbPath, ensureDir, yewHome } from "./paths.js";
7
+ import { Store } from "./store.js";
8
+ /** Opens Yew's data directory, database, keychain and model catalog. Used by `yew serve` and the CLI. */
9
+ export function openRuntime(options = {}) {
10
+ const home = ensureDir(yewHome(options.env));
11
+ const bus = new EventBus();
12
+ const db = openDatabase(dbPath(home));
13
+ const store = new Store(db, bus);
14
+ const keys = options.keys ?? systemKeys();
15
+ const gateway = new ModelGateway({ keys, version: VERSION, custom: store.listCustomProviders() });
16
+ return { home, store, bus, keys, gateway, close: () => db.close() };
17
+ }