@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,242 @@
1
+ /**
2
+ * Keeps what the model is sent in bounds while a session works: a system prompt that stays the same
3
+ * between turns, and a transcript that is summarized when it nears the model's context window.
4
+ */
5
+ import { messageText } from "../../shared/messages.js";
6
+ import { permissionModeOf } from "../../shared/types.js";
7
+ import { estimateTokens, isOwnNote, transcriptForSummary, usageTotals } from "./context.js";
8
+ import { loadInstructions, MAIN_ROLE, PERMISSION_MODE, renderInstructions, SUMMARY_PROMPT, takeSnapshot, today, } from "./prompt.js";
9
+ const KEEP_RECENT_TOKENS = 20_000;
10
+ const MAX_COMPACT_AT = 200_000;
11
+ const MAX_REQUEST_CHARS = 20_000;
12
+ /** The context size at which a session's history is summarized. */
13
+ function compactAt(session, model) {
14
+ return (session.options.compactAt ?? Math.min(Math.floor(model.contextWindow * 0.8), MAX_COMPACT_AT));
15
+ }
16
+ /** A message from the person or a letter, not one of Yew's own notes to the model. */
17
+ function isRequest(entry) {
18
+ return entry.role === "user" && !isOwnNote(messageText(entry.message));
19
+ }
20
+ function clipRequest(text) {
21
+ return text.length > MAX_REQUEST_CHARS
22
+ ? `${text.slice(0, MAX_REQUEST_CHARS)}\n[cut by Yew: the message was longer]`
23
+ : text;
24
+ }
25
+ /** What a session is told when the person's main conversation changes. */
26
+ function mainUpdate(session, before, now) {
27
+ if (now?.id === session.id)
28
+ return `The person made you their main conversation.\n${MAIN_ROLE}`;
29
+ if (before?.id === session.id) {
30
+ return `You are no longer the person's main conversation.${now ? ` Their main conversation is now "${now.name}" (id ${now.id}).` : ""}`;
31
+ }
32
+ if (now) {
33
+ return `The person's main conversation is now "${now.name}" (id ${now.id}). When something needs the person's decision, send it a request; it puts the question in the approval center.`;
34
+ }
35
+ return "The person has no main conversation now. When something needs the person's decision, ask them with ask_user.";
36
+ }
37
+ /** A line in the record for the person, not for the model. */
38
+ export function notice(store, sessionId, kind, text, data) {
39
+ store.appendEntry(sessionId, {
40
+ role: "notice",
41
+ notice: data
42
+ ? { kind, text, data, timestamp: Date.now() }
43
+ : { kind, text, timestamp: Date.now() },
44
+ });
45
+ }
46
+ export class ContextKeeper {
47
+ #store;
48
+ #gateway;
49
+ #home;
50
+ #placement;
51
+ #extras;
52
+ constructor(options) {
53
+ this.#store = options.store;
54
+ this.#gateway = options.gateway;
55
+ this.#home = options.home;
56
+ this.#placement = options.placement;
57
+ this.#extras = options.extras;
58
+ }
59
+ /**
60
+ * Keeps the system prompt the same between turns. The environment and instruction files are saved at the
61
+ * first turn; when the files, the date, the approval mode or the main conversation change later, an
62
+ * update is appended to the transcript instead, and the prompt keeps its saved inputs until the next
63
+ * compaction, so the provider's prompt cache stays valid (prior-art 4.5).
64
+ */
65
+ refresh(session) {
66
+ const placement = this.#placement(session);
67
+ const saved = this.#store.getContextSnapshot(session.id);
68
+ if (!saved) {
69
+ const snapshot = takeSnapshot(session, this.#home, placement, this.#extras());
70
+ this.#store.setContextSnapshot(session.id, snapshot);
71
+ return snapshot;
72
+ }
73
+ // A session switched between chat and code gets a fresh environment: folder, git status and files.
74
+ if (saved.cwd !== session.cwd) {
75
+ const snapshot = takeSnapshot(session, this.#home, placement, this.#extras());
76
+ this.#store.setContextSnapshot(session.id, snapshot);
77
+ this.#store.appendEntry(session.id, {
78
+ role: "system",
79
+ message: {
80
+ role: "system",
81
+ content: session.cwd
82
+ ? `The working folder is now ${session.cwd}. File and shell tools work there.`
83
+ : "This session no longer has a working folder. It is a chat now, without file or shell tools.",
84
+ timestamp: Date.now(),
85
+ },
86
+ });
87
+ return snapshot;
88
+ }
89
+ const mode = permissionModeOf(session.options);
90
+ // Snapshots from before a field existed learn it quietly; the prompt reads the same either way.
91
+ const team = placement.team ?? null;
92
+ const { told: _told, ...prompt } = {
93
+ ...saved,
94
+ permissionMode: saved.permissionMode ?? mode,
95
+ main: saved.main === undefined ? null : saved.main,
96
+ parent: saved.parent === undefined ? placement.parent : saved.parent,
97
+ name: saved.name ?? session.name,
98
+ team: saved.team === undefined ? team : saved.team,
99
+ };
100
+ const told = saved.told ?? {
101
+ date: prompt.date,
102
+ instructions: prompt.instructions,
103
+ permissionMode: prompt.permissionMode,
104
+ main: prompt.main,
105
+ };
106
+ const toldName = told.name ?? prompt.name;
107
+ const toldTeam = told.team === undefined ? prompt.team : told.team;
108
+ const updates = [];
109
+ const date = today();
110
+ if (date !== told.date)
111
+ updates.push(`Today's date is now ${date}.`);
112
+ const current = loadInstructions(session.cwd, this.#home, this.#extras());
113
+ if (JSON.stringify(current) !== JSON.stringify(told.instructions)) {
114
+ updates.push(current.length > 0
115
+ ? `The instruction files changed. These are the current versions; they replace the earlier ones.\n\n${renderInstructions(current, session.cwd)}`
116
+ : "The instruction files were removed.");
117
+ }
118
+ if (told.permissionMode !== mode) {
119
+ updates.push(`The person changed the permission mode. ${PERMISSION_MODE[mode]}`);
120
+ }
121
+ const sameMain = (told.main?.id ?? null) === (placement.main?.id ?? null);
122
+ if (!sameMain)
123
+ updates.push(mainUpdate(session, told.main, placement.main));
124
+ if (session.name !== toldName)
125
+ updates.push(`Your name is now "${session.name}".`);
126
+ if (team !== toldTeam) {
127
+ updates.push(team ? `You now belong to the team "${team}".` : "You are no longer in a team.");
128
+ }
129
+ if (updates.length > 0) {
130
+ this.#store.appendEntry(session.id, {
131
+ role: "system",
132
+ message: { role: "system", content: updates.join("\n\n"), timestamp: Date.now() },
133
+ });
134
+ }
135
+ const next = {
136
+ ...prompt,
137
+ told: {
138
+ date,
139
+ instructions: current,
140
+ permissionMode: mode,
141
+ main: sameMain ? told.main : placement.main,
142
+ name: session.name,
143
+ team,
144
+ },
145
+ };
146
+ if (JSON.stringify(next) !== JSON.stringify(saved)) {
147
+ this.#store.setContextSnapshot(session.id, next);
148
+ }
149
+ return prompt;
150
+ }
151
+ async compactIfNeeded(session, model, signal) {
152
+ if (session.contextTokens < compactAt(session, model))
153
+ return "nothing";
154
+ return this.compact(session, model, signal, false);
155
+ }
156
+ /**
157
+ * Summarizes older history with the session's own model and keeps roughly the last 20,000 tokens
158
+ * (less for a model with a small context window, so that what is kept fits well under the limit).
159
+ * The kept part starts at a message or at a step of the model, never at a tool result, so a tool
160
+ * call is never separated from its result (prior-art 4.4). A turn of hundreds of steps after one
161
+ * message is cut inside the turn; the message being worked on is then carried over word for word,
162
+ * because a summary only paraphrases it and loses its constraints. The next prompt is built fresh,
163
+ * with a new environment snapshot.
164
+ */
165
+ async compact(session, model, signal, force) {
166
+ const entries = this.#store.contextEntries(session.id);
167
+ const keep = Math.min(KEEP_RECENT_TOKENS, Math.floor(compactAt(session, model) / 4));
168
+ let kept = 0;
169
+ let firstKept = null;
170
+ for (let i = entries.length - 1; i >= 0; i--) {
171
+ const entry = entries[i];
172
+ kept += estimateTokens(entry);
173
+ if (entry.role === "user" || entry.role === "system" || entry.role === "assistant") {
174
+ if (kept > keep && firstKept)
175
+ break;
176
+ firstKept = entry;
177
+ }
178
+ }
179
+ if (!firstKept)
180
+ return "nothing";
181
+ const older = entries.filter((entry) => entry.seq < firstKept.seq);
182
+ if (older.length === 0 || (!force && older.every((entry) => entry.role === "compaction")))
183
+ return "nothing";
184
+ const remaining = entries.filter((entry) => entry.seq >= firstKept.seq);
185
+ let request;
186
+ if (!remaining.some(isRequest)) {
187
+ const asked = older.findLast(isRequest);
188
+ const before = older.find((entry) => entry.role === "compaction");
189
+ request =
190
+ asked?.role === "user"
191
+ ? clipRequest(messageText(asked.message))
192
+ : before?.role === "compaction"
193
+ ? before.compaction.request
194
+ : undefined;
195
+ }
196
+ this.#store.live(session.id, { type: "draftCleared" });
197
+ const summary = await this.#gateway.stream({
198
+ provider: session.provider,
199
+ model: model.id,
200
+ thinking: "off",
201
+ systemPrompt: SUMMARY_PROMPT,
202
+ messages: [
203
+ {
204
+ role: "user",
205
+ content: transcriptForSummary(older),
206
+ timestamp: Date.now(),
207
+ },
208
+ ],
209
+ tools: [],
210
+ signal,
211
+ });
212
+ this.#store.addUsage(session.id, usageTotals(summary), session.contextTokens);
213
+ const text = summary.content
214
+ .filter((block) => block.type === "text")
215
+ .map((block) => block.text)
216
+ .join("\n")
217
+ .trim();
218
+ if (summary.stopReason !== "stop" || !text) {
219
+ if (!signal.aborted)
220
+ notice(this.#store, session.id, "error", summary.errorMessage ?? "The summary came back empty.", {
221
+ code: "compact-failed",
222
+ });
223
+ return "failed";
224
+ }
225
+ this.#store.tx(() => {
226
+ this.#store.appendEntry(session.id, {
227
+ role: "compaction",
228
+ compaction: {
229
+ summary: text,
230
+ ...(request ? { request } : {}),
231
+ firstKeptSeq: firstKept.seq,
232
+ tokensBefore: session.contextTokens,
233
+ timestamp: Date.now(),
234
+ },
235
+ });
236
+ notice(this.#store, session.id, "compacted", `Earlier history was summarized to save context (${session.contextTokens} tokens before).`);
237
+ this.#store.setContextSnapshot(session.id, takeSnapshot(session, this.#home, this.#placement(session), this.#extras()));
238
+ this.#store.setContextTokens(session.id, remaining.reduce((sum, entry) => sum + estimateTokens(entry), Math.ceil((text.length + (request?.length ?? 0)) / 4)));
239
+ });
240
+ return "done";
241
+ }
242
+ }
@@ -0,0 +1,119 @@
1
+ import { messageText } from "../../shared/messages.js";
2
+ export const SUMMARY_HEADING = "[Summary of the earlier conversation, written by Yew when it shortened the context]";
3
+ export const REQUEST_HEADING = "[The latest message to you before that summary, word for word; it is still what you are working on]";
4
+ /** What Yew puts in front of its own messages to the model, which are stored like the person's. */
5
+ export const YEW_NOTE = "[From Yew, not from the person]";
6
+ /** Whether a stored message is one of Yew's own notes. */
7
+ export const isOwnNote = (text) => text.startsWith(YEW_NOTE);
8
+ function placeholder(call, timestamp) {
9
+ return {
10
+ role: "toolResult",
11
+ toolCallId: call.id,
12
+ toolName: call.name,
13
+ content: [{ type: "text", text: "This call did not run." }],
14
+ isError: true,
15
+ timestamp,
16
+ };
17
+ }
18
+ /**
19
+ * Turns stored entries into what the model sees. Providers require each tool call to be answered right
20
+ * after the message that made it, so results are placed there whatever order they were stored in, and a
21
+ * call without a result (after a crash, say) gets a placeholder. Notices are for the person only.
22
+ */
23
+ export function toModelMessages(entries) {
24
+ const results = new Map();
25
+ for (const entry of entries) {
26
+ if (entry.role === "toolResult" && !results.has(entry.message.toolCallId)) {
27
+ results.set(entry.message.toolCallId, entry.message);
28
+ }
29
+ }
30
+ const out = [];
31
+ for (const entry of entries) {
32
+ switch (entry.role) {
33
+ case "compaction":
34
+ out.push({
35
+ role: "user",
36
+ content: entry.compaction.request
37
+ ? `${SUMMARY_HEADING}\n\n${entry.compaction.summary}\n\n${REQUEST_HEADING}\n\n${entry.compaction.request}`
38
+ : `${SUMMARY_HEADING}\n\n${entry.compaction.summary}`,
39
+ timestamp: entry.compaction.timestamp,
40
+ });
41
+ break;
42
+ case "system":
43
+ case "user":
44
+ out.push(entry.message);
45
+ break;
46
+ case "assistant": {
47
+ const message = entry.message;
48
+ if (message.content.length === 0)
49
+ break;
50
+ out.push(message);
51
+ for (const block of message.content) {
52
+ if (block.type !== "toolCall")
53
+ continue;
54
+ out.push(results.get(block.id) ?? placeholder(block, message.timestamp));
55
+ }
56
+ break;
57
+ }
58
+ default:
59
+ break;
60
+ }
61
+ }
62
+ return out;
63
+ }
64
+ /** An interrupted response may end in a half-written tool call; keep the text and drop the call. */
65
+ export function sanitizeInterrupted(message) {
66
+ if (message.stopReason !== "aborted" && message.stopReason !== "error")
67
+ return message;
68
+ return { ...message, content: message.content.filter((block) => block.type !== "toolCall") };
69
+ }
70
+ const TOOL_RESULT_CHARS = 2000;
71
+ /** The transcript as plain text for the summary request; tool results are cut short (prior-art 4.4). */
72
+ export function transcriptForSummary(entries) {
73
+ const lines = [];
74
+ for (const entry of entries) {
75
+ if (entry.role === "compaction") {
76
+ lines.push(`## Earlier summary\n${entry.compaction.summary}`);
77
+ if (entry.compaction.request)
78
+ lines.push(`## Person\n${entry.compaction.request}`);
79
+ }
80
+ else if (entry.role === "user")
81
+ lines.push(`## Person\n${messageText(entry.message)}`);
82
+ else if (entry.role === "system")
83
+ lines.push(`## Update from Yew\n${entry.message.content}`);
84
+ else if (entry.role === "assistant") {
85
+ const text = entry.message.content
86
+ .filter((block) => block.type === "text")
87
+ .map((block) => block.text)
88
+ .join("\n");
89
+ if (text)
90
+ lines.push(`## Agent\n${text}`);
91
+ for (const block of entry.message.content) {
92
+ if (block.type === "toolCall")
93
+ lines.push(`## Tool call: ${block.name}\n${JSON.stringify(block.arguments)}`);
94
+ }
95
+ }
96
+ else if (entry.role === "toolResult") {
97
+ let text = messageText(entry.message);
98
+ if (text.length > TOOL_RESULT_CHARS)
99
+ text = `${text.slice(0, TOOL_RESULT_CHARS)}\n[cut]`;
100
+ lines.push(`## Tool result: ${entry.message.toolName}${entry.message.isError ? " (error)" : ""}\n${text}`);
101
+ }
102
+ }
103
+ return lines.join("\n\n");
104
+ }
105
+ /** Rough token count (four characters per token), used to choose what compaction keeps. */
106
+ export function estimateTokens(entry) {
107
+ return Math.ceil(JSON.stringify(entry).length / 4);
108
+ }
109
+ /** What a response used, as Yew adds it up. */
110
+ export function usageTotals(message) {
111
+ const usage = message.usage;
112
+ return {
113
+ input: usage.input,
114
+ output: usage.output,
115
+ cacheRead: usage.cacheRead,
116
+ cacheWrite: usage.cacheWrite,
117
+ cost: usage.cost.total,
118
+ };
119
+ }
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Sessions that are held back: ones the person stopped, and ones that stopped at a daily spending
3
+ * limit. Kept in the database, so Yew behaves the same after a restart: a session the person stopped
4
+ * is not started by a letter, and one at a limit goes on when the limit is raised that day.
5
+ */
6
+ export class Held {
7
+ #store;
8
+ #paused;
9
+ /** The day (local) each session stopped at a limit. */
10
+ #capped;
11
+ constructor(store) {
12
+ this.#store = store;
13
+ this.#paused = new Set(store.getSetting("pausedSessions", []));
14
+ this.#capped = new Map(Object.entries(store.getSetting("cappedSessions", {})));
15
+ }
16
+ isPaused(sessionId) {
17
+ return this.#paused.has(sessionId);
18
+ }
19
+ pause(sessionId) {
20
+ if (this.#paused.has(sessionId))
21
+ return;
22
+ this.#paused.add(sessionId);
23
+ this.#store.setSetting("pausedSessions", [...this.#paused]);
24
+ }
25
+ resume(sessionId) {
26
+ if (this.#paused.delete(sessionId))
27
+ this.#store.setSetting("pausedSessions", [...this.#paused]);
28
+ }
29
+ capped() {
30
+ return [...this.#capped];
31
+ }
32
+ cap(sessionId, day) {
33
+ if (this.#capped.get(sessionId) === day)
34
+ return;
35
+ this.#capped.set(sessionId, day);
36
+ this.#store.setSetting("cappedSessions", Object.fromEntries(this.#capped));
37
+ }
38
+ uncap(sessionId) {
39
+ if (this.#capped.delete(sessionId)) {
40
+ this.#store.setSetting("cappedSessions", Object.fromEntries(this.#capped));
41
+ }
42
+ }
43
+ /** The session is gone for good. */
44
+ forget(sessionId) {
45
+ this.resume(sessionId);
46
+ this.uncap(sessionId);
47
+ }
48
+ }
@@ -0,0 +1,122 @@
1
+ import { plainError } from "../model/gateway.js";
2
+ import { specs } from "../tools/registry.js";
3
+ import { ToolInputError, } from "../tools/types.js";
4
+ import { usageTotals } from "./context.js";
5
+ /** The tools a helper may have, when its session has them. */
6
+ export const HELPER_TOOLS = ["read", "glob", "grep", "shell", "web_fetch", "web_search"];
7
+ /** Model requests one helper makes at most. */
8
+ const MAX_STEPS = 30;
9
+ /** The most of its answer that goes back. */
10
+ const MAX_ANSWER_CHARS = 20_000;
11
+ const textOf = (message) => message.content
12
+ .flatMap((block) => (block.type === "text" && block.text.trim() ? [block.text] : []))
13
+ .join("\n\n");
14
+ const refusal = (text) => ({
15
+ content: [{ type: "text", text }],
16
+ isError: true,
17
+ });
18
+ /** A few words on what a call looks at: its path, pattern, command or address. */
19
+ export function stepLine(call) {
20
+ const args = call.arguments;
21
+ const what = [args.path, args.pattern, args.command, args.url, args.query].find((value) => typeof value === "string" && value);
22
+ const shown = typeof what === "string" ? what.replace(/\s+/g, " ").slice(0, 160) : "";
23
+ return shown ? `${call.name} ${shown}` : call.name;
24
+ }
25
+ export async function runHelper(options) {
26
+ const { session, signal } = options;
27
+ const tools = options.tools.filter((tool) => HELPER_TOOLS.includes(tool.name));
28
+ const byName = new Map(tools.map((tool) => [tool.name, tool]));
29
+ // The same session in the mode that asks before anything but looking: a command it lets run
30
+ // unasked only looks.
31
+ const asReader = {
32
+ ...session,
33
+ options: { ...session.options, permissionMode: "manual" },
34
+ };
35
+ const messages = [{ role: "user", content: options.task, timestamp: Date.now() }];
36
+ let calls = 0;
37
+ let said = "";
38
+ const runCall = async (call) => {
39
+ const started = Date.now();
40
+ const tool = byName.get(call.name);
41
+ let result;
42
+ if (!tool) {
43
+ result = refusal(`There is no tool named ${call.name}. A helper can only look.`);
44
+ }
45
+ else if (call.argumentsError) {
46
+ result = refusal(`The arguments were not valid JSON: ${call.argumentsError}`);
47
+ }
48
+ else if (call.arguments.background === true) {
49
+ result = refusal("Not run: a helper cannot leave commands running in the background.");
50
+ }
51
+ else {
52
+ try {
53
+ const subject = tool.subject(call.arguments, session, options.lookup);
54
+ // Allowed for the session as it is; a command also has to be one that only looks.
55
+ const allowed = options.permissions.decide(subject, session).effect === "allow" &&
56
+ (tool.name !== "shell" ||
57
+ options.permissions.decide(subject, asReader).effect === "allow");
58
+ if (!allowed) {
59
+ result = refusal("Not run: a helper only looks and cannot ask the person, and this call would change something or needs their approval. Say in your answer what you could not check.");
60
+ }
61
+ else {
62
+ options.onStep?.(stepLine(call));
63
+ result = await tool.run(call.arguments, options.context);
64
+ }
65
+ }
66
+ catch (error) {
67
+ if (signal.aborted)
68
+ throw error;
69
+ result = refusal(error instanceof ToolInputError
70
+ ? error.message
71
+ : `The tool failed: ${error.message}`);
72
+ }
73
+ }
74
+ return {
75
+ role: "toolResult",
76
+ toolCallId: call.id,
77
+ toolName: call.name,
78
+ content: result.content,
79
+ isError: result.isError ?? false,
80
+ timestamp: Date.now(),
81
+ durationMs: Date.now() - started,
82
+ };
83
+ };
84
+ const stop = (stopped) => ({
85
+ text: said.slice(0, MAX_ANSWER_CHARS),
86
+ calls,
87
+ stopped,
88
+ });
89
+ for (let step = 0; step < (options.maxSteps ?? MAX_STEPS); step++) {
90
+ if (signal.aborted)
91
+ return stop("stopped");
92
+ const reply = await options.gateway.stream({
93
+ provider: session.provider,
94
+ model: session.model,
95
+ thinking: session.thinking,
96
+ systemPrompt: options.systemPrompt,
97
+ messages,
98
+ tools: specs(tools),
99
+ signal,
100
+ });
101
+ options.onUsage?.(usageTotals(reply));
102
+ if (reply.stopReason === "aborted" || signal.aborted)
103
+ return stop("stopped");
104
+ if (reply.stopReason === "error") {
105
+ return stop(plainError(reply.errorMessage ?? "The model request failed."));
106
+ }
107
+ const text = textOf(reply);
108
+ if (text)
109
+ said = text;
110
+ const asked = reply.content.filter((block) => block.type === "toolCall");
111
+ if (asked.length === 0) {
112
+ return reply.stopReason === "length"
113
+ ? stop("its answer was cut off at the output limit")
114
+ : { text: said.slice(0, MAX_ANSWER_CHARS), calls };
115
+ }
116
+ messages.push(reply);
117
+ calls += asked.length;
118
+ // Everything a helper may do only looks, so its calls of one step run at the same time.
119
+ messages.push(...(await Promise.all(asked.map(runCall))));
120
+ }
121
+ return stop(`it used all ${options.maxSteps ?? MAX_STEPS} of its steps`);
122
+ }