@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,279 @@
1
+ import { json } from "../db/database.js";
2
+ import { notFound } from "../errors.js";
3
+ import { newId } from "../ids.js";
4
+ function toLetter(row) {
5
+ return {
6
+ id: row.id,
7
+ threadId: row.thread_id,
8
+ replyTo: row.reply_to,
9
+ fromKind: row.from_kind,
10
+ fromSession: row.from_session,
11
+ fromName: row.from_name,
12
+ toSession: row.to_session,
13
+ kind: row.kind,
14
+ summary: row.summary,
15
+ body: row.body,
16
+ status: row.status,
17
+ statusReason: row.status_reason,
18
+ path: json(row.path, []),
19
+ auto: row.auto === 1,
20
+ decision: row.decision,
21
+ scheduleId: row.schedule_id,
22
+ createdAt: row.created_at,
23
+ deliveredAt: row.delivered_at,
24
+ };
25
+ }
26
+ function toCard(row) {
27
+ return {
28
+ id: row.id,
29
+ number: row.number,
30
+ createdBy: row.created_by,
31
+ requestedBy: row.requested_by,
32
+ title: row.title,
33
+ context: row.context,
34
+ options: json(row.options, []),
35
+ recommended: row.recommended,
36
+ deadline: row.deadline,
37
+ sessions: json(row.sessions, []),
38
+ undoable: row.undoable === null ? null : row.undoable === 1,
39
+ reversible: row.reversible,
40
+ after: row.after,
41
+ status: row.status,
42
+ choice: row.choice,
43
+ note: row.note,
44
+ decidedAt: row.decided_at,
45
+ withdrawnReason: row.withdrawn_reason,
46
+ createdAt: row.created_at,
47
+ updatedAt: row.updated_at,
48
+ };
49
+ }
50
+ /**
51
+ * Letters and decision cards in the database. Every change is announced on the global event stream after
52
+ * its transaction commits, so the approval center and the letters list update in every tab.
53
+ */
54
+ export class MailStore {
55
+ #store;
56
+ #statements = new Map();
57
+ constructor(store) {
58
+ this.#store = store;
59
+ }
60
+ get db() {
61
+ return this.#store.db;
62
+ }
63
+ #q(sql) {
64
+ let statement = this.#statements.get(sql);
65
+ if (!statement) {
66
+ statement = this.#store.db.prepare(sql);
67
+ this.#statements.set(sql, statement);
68
+ }
69
+ return statement;
70
+ }
71
+ // --- letters ---
72
+ addLetter(input) {
73
+ return this.#store.tx(() => {
74
+ const id = newId();
75
+ const now = Date.now();
76
+ this.#q(`INSERT INTO letters (id, thread_id, reply_to, from_kind, from_session, from_name, to_session, kind,
77
+ summary, body, status, status_reason, path, auto, decision, schedule_id, created_at, updated_at)
78
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`).run(id, input.threadId ?? id, input.replyTo ?? null, input.fromKind, input.fromSession, input.fromName, input.toSession, input.kind, input.summary, input.body, input.status, input.statusReason ?? null, JSON.stringify(input.path ?? []), input.auto ? 1 : 0, input.decision ?? null, input.scheduleId ?? null, now, now);
79
+ return this.#emitLetter(id);
80
+ });
81
+ }
82
+ getLetter(id) {
83
+ return toLetter(this.#letterRow(id));
84
+ }
85
+ #letterRow(id) {
86
+ const row = this.#q("SELECT * FROM letters WHERE id = ?").get(id);
87
+ if (!row)
88
+ throw notFound("Letter", id);
89
+ return row;
90
+ }
91
+ /** The transcript entry a delivered letter became; null when it went to a waiting tool call. */
92
+ letterEntry(id) {
93
+ return this.#letterRow(id).entry_id;
94
+ }
95
+ setLetterStatus(id, status, extra = {}) {
96
+ return this.#store.tx(() => {
97
+ const current = this.#letterRow(id);
98
+ this.#q(`UPDATE letters SET status = ?, status_reason = ?, entry_id = ?, delivered_at = ?, updated_at = ?
99
+ WHERE id = ?`).run(status, extra.reason === undefined ? current.status_reason : extra.reason, extra.entryId === undefined ? current.entry_id : extra.entryId, extra.deliveredAt ?? current.delivered_at, Date.now(), id);
100
+ return this.#emitLetter(id);
101
+ });
102
+ }
103
+ /** Letters waiting for a session, oldest first. */
104
+ queuedFor(sessionId) {
105
+ const rows = this.#q("SELECT * FROM letters WHERE to_session = ? AND status = 'queued' ORDER BY created_at, id").all(sessionId);
106
+ return rows.map(toLetter);
107
+ }
108
+ /** Sessions with letters waiting to be delivered. */
109
+ sessionsWithQueued() {
110
+ const rows = this.#q("SELECT DISTINCT to_session AS id FROM letters WHERE status = 'queued'").all();
111
+ return rows.map((row) => row.id);
112
+ }
113
+ /** Letters held for the person to release or refuse, oldest first. */
114
+ heldLetters() {
115
+ const rows = this.#q("SELECT * FROM letters WHERE status = 'held' ORDER BY created_at, id").all();
116
+ return rows.map(toLetter);
117
+ }
118
+ /**
119
+ * Requests from other sessions that a session has read and not answered yet. A request stays open
120
+ * until the session answers it or finishes a turn normally, so a turn that ended with an error, was
121
+ * stopped, or is waiting for answers of its own still owes the requester a reply. Kept in the
122
+ * database, so what was owed when Yew stopped is still owed after it starts again.
123
+ */
124
+ openRequests(sessionId) {
125
+ const rows = this.#q(`SELECT * FROM letters WHERE to_session = ? AND status = 'delivered' AND kind = 'request'
126
+ AND from_kind = 'session' ORDER BY created_at, id`).all(sessionId);
127
+ return rows.map(toLetter);
128
+ }
129
+ /** Letters from scheduled tasks the session has read in the turn that is ending. */
130
+ scheduledRead(sessionId) {
131
+ const rows = this.#q(`SELECT * FROM letters WHERE to_session = ? AND status = 'delivered' AND from_kind = 'yew'
132
+ AND schedule_id IS NOT NULL ORDER BY created_at, id`).all(sessionId);
133
+ return rows.map(toLetter);
134
+ }
135
+ /** The request was answered: it is done. */
136
+ closeRequest(id) {
137
+ if (this.#letterRow(id).status === "delivered")
138
+ this.setLetterStatus(id, "done");
139
+ }
140
+ /** At the end of a turn, what the session read is done, except requests it still owes a reply. */
141
+ finishRead(sessionId) {
142
+ const rows = this.#q(`SELECT id FROM letters WHERE to_session = ? AND status = 'delivered'
143
+ AND NOT (kind = 'request' AND from_kind = 'session')`).all(sessionId);
144
+ this.#store.tx(() => {
145
+ for (const row of rows)
146
+ this.setLetterStatus(row.id, "done");
147
+ });
148
+ }
149
+ /** A letter handed to a waiting tool call reached the transcript with that call's result. */
150
+ setLetterEntry(id, entryId) {
151
+ this.#q("UPDATE letters SET entry_id = ?, updated_at = ? WHERE id = ?").run(entryId, Date.now(), id);
152
+ }
153
+ /**
154
+ * After a restart: letters handed to a waiting tool call whose result was never stored are delivered
155
+ * again as ordinary letters, so the answer is not lost.
156
+ */
157
+ requeueHandedOff() {
158
+ const rows = this.#q("SELECT id FROM letters WHERE status = 'delivered' AND entry_id IS NULL").all();
159
+ this.#store.tx(() => {
160
+ for (const row of rows)
161
+ this.setLetterStatus(row.id, "queued");
162
+ });
163
+ return rows.length;
164
+ }
165
+ /** Letters waiting for a session that was archived: they will not be delivered. */
166
+ dropWaiting(sessionId, reason) {
167
+ const rows = this.#q("SELECT id FROM letters WHERE to_session = ? AND status IN ('queued', 'held')").all(sessionId);
168
+ return this.#store.tx(() => rows.map((row) => this.setLetterStatus(row.id, "dropped", { reason })));
169
+ }
170
+ /** Letters from one session to another since a time, refused ones left out. */
171
+ recentBetween(from, to, since) {
172
+ const rows = this.#q(`SELECT * FROM letters WHERE from_session = ? AND to_session = ? AND created_at >= ?
173
+ AND status != 'refused' ORDER BY created_at, id`).all(from, to, since);
174
+ return rows.map(toLetter);
175
+ }
176
+ /**
177
+ * For the sessions map: how many letters went from one session to another since a time, the busiest
178
+ * pair first. Letters that were refused or dropped never arrived and are left out, as are letters to
179
+ * or from an archived session, which the map does not show.
180
+ */
181
+ letterFlows(since) {
182
+ const rows = this.#q(`SELECT l.from_session AS sender, l.to_session AS receiver, COUNT(*) AS n, MAX(l.created_at) AS last
183
+ FROM letters l
184
+ JOIN sessions a ON a.id = l.from_session AND a.archived_at IS NULL
185
+ JOIN sessions b ON b.id = l.to_session AND b.archived_at IS NULL
186
+ WHERE l.created_at >= ? AND l.from_session != l.to_session
187
+ AND l.status NOT IN ('refused', 'dropped')
188
+ GROUP BY l.from_session, l.to_session
189
+ ORDER BY n DESC, last DESC, sender, receiver`).all(since);
190
+ return rows.map((row) => ({
191
+ from: row.sender,
192
+ to: row.receiver,
193
+ count: row.n,
194
+ lastAt: row.last,
195
+ }));
196
+ }
197
+ /** All letters, newest first; with a session, the ones it sent or received. */
198
+ listLetters(options) {
199
+ const cursor = options.cursor ?? "￿";
200
+ const rows = (options.sessionId
201
+ ? this.#q(`SELECT * FROM letters WHERE (from_session = ? OR to_session = ?) AND id < ?
202
+ ORDER BY id DESC LIMIT ?`).all(options.sessionId, options.sessionId, cursor, options.limit + 1)
203
+ : this.#q("SELECT * FROM letters WHERE id < ? ORDER BY id DESC LIMIT ?").all(cursor, options.limit + 1));
204
+ const more = rows.length > options.limit;
205
+ const items = rows.slice(0, options.limit).map(toLetter);
206
+ return { items, nextCursor: more ? (items.at(-1)?.id ?? null) : null };
207
+ }
208
+ #emitLetter(id) {
209
+ const letter = this.getLetter(id);
210
+ this.#store.publish({ type: "letter", letter });
211
+ return letter;
212
+ }
213
+ // --- decision cards ---
214
+ addCard(createdBy, requestedBy, fields) {
215
+ return this.#store.tx(() => {
216
+ const id = newId();
217
+ const now = Date.now();
218
+ const max = this.#q("SELECT MAX(number) AS n FROM cards").get();
219
+ this.#q(`INSERT INTO cards (id, number, created_by, requested_by, title, context, options, recommended,
220
+ deadline, sessions, undoable, reversible, after, created_at, updated_at)
221
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`).run(id, (max.n ?? 0) + 1, createdBy, requestedBy, fields.title, fields.context, JSON.stringify(fields.options), fields.recommended, fields.deadline, JSON.stringify(fields.sessions), fields.undoable ? 1 : 0, fields.reversible, fields.after, now, now);
222
+ return this.#emitCard(id);
223
+ });
224
+ }
225
+ getCard(id) {
226
+ const row = this.#q("SELECT * FROM cards WHERE id = ?").get(id);
227
+ if (!row)
228
+ throw notFound("Card", id);
229
+ return toCard(row);
230
+ }
231
+ cardByNumber(number) {
232
+ const row = this.#q("SELECT * FROM cards WHERE number = ?").get(number);
233
+ if (!row)
234
+ throw notFound("Decision", `#${number}`);
235
+ return toCard(row);
236
+ }
237
+ /** Replaces a pending card's six points. */
238
+ updateCardFields(id, fields) {
239
+ return this.#store.tx(() => {
240
+ this.#q(`UPDATE cards SET title = ?, context = ?, options = ?, recommended = ?, deadline = ?, sessions = ?,
241
+ undoable = ?, reversible = ?, after = ?, updated_at = ? WHERE id = ?`).run(fields.title, fields.context, JSON.stringify(fields.options), fields.recommended, fields.deadline, JSON.stringify(fields.sessions), fields.undoable ? 1 : 0, fields.reversible, fields.after, Date.now(), id);
242
+ return this.#emitCard(id);
243
+ });
244
+ }
245
+ decideCard(id, choice, note) {
246
+ return this.#store.tx(() => {
247
+ const now = Date.now();
248
+ this.#q(`UPDATE cards SET status = 'decided', choice = ?, note = ?, decided_at = ?, updated_at = ?
249
+ WHERE id = ?`).run(choice, note, now, now, id);
250
+ return this.#emitCard(id);
251
+ });
252
+ }
253
+ withdrawCard(id, reason) {
254
+ return this.#store.tx(() => {
255
+ this.#q("UPDATE cards SET status = 'withdrawn', withdrawn_reason = ?, updated_at = ? WHERE id = ?").run(reason, Date.now(), id);
256
+ return this.#emitCard(id);
257
+ });
258
+ }
259
+ /** Cards waiting for the person, oldest first. */
260
+ pendingCards() {
261
+ const rows = this.#q("SELECT * FROM cards WHERE status = 'pending' ORDER BY number").all();
262
+ return rows.map(toCard);
263
+ }
264
+ /** Cards by status, newest number first; `cursor` is the last number of the previous page. */
265
+ listCards(options) {
266
+ const cursor = options.cursor ?? Number.MAX_SAFE_INTEGER;
267
+ const rows = (options.status
268
+ ? this.#q("SELECT * FROM cards WHERE status = ? AND number < ? ORDER BY number DESC LIMIT ?").all(options.status, cursor, options.limit + 1)
269
+ : this.#q("SELECT * FROM cards WHERE number < ? ORDER BY number DESC LIMIT ?").all(cursor, options.limit + 1));
270
+ const more = rows.length > options.limit;
271
+ const items = rows.slice(0, options.limit).map(toCard);
272
+ return { items, nextCursor: more ? String(items.at(-1)?.number ?? "") : null };
273
+ }
274
+ #emitCard(id) {
275
+ const card = this.getCard(id);
276
+ this.#store.publish({ type: "card", card });
277
+ return card;
278
+ }
279
+ }