@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.
- package/CHANGELOG.md +273 -0
- package/LICENSE +21 -0
- package/README.md +64 -2
- package/THIRD-PARTY-NOTICES.md +122 -0
- package/bin/yew.js +9 -0
- package/dist/cli/app.js +211 -0
- package/dist/cli/export.js +57 -0
- package/dist/cli/io.js +65 -0
- package/dist/cli/key.js +113 -0
- package/dist/cli/main.js +58 -0
- package/dist/cli/mcp.js +76 -0
- package/dist/cli/remote.js +116 -0
- package/dist/cli/run.js +72 -0
- package/dist/cli/schedule.js +123 -0
- package/dist/cli/send.js +127 -0
- package/dist/cli/serve.js +301 -0
- package/dist/cli/service.js +268 -0
- package/dist/cli/system.js +44 -0
- package/dist/remote/access.js +107 -0
- package/dist/remote/connector.js +278 -0
- package/dist/remote/identity.js +71 -0
- package/dist/remote/noise.js +287 -0
- package/dist/remote/protocol.js +51 -0
- package/dist/remote/push.js +37 -0
- package/dist/server/agent/compaction.js +242 -0
- package/dist/server/agent/context.js +120 -0
- package/dist/server/agent/held.js +48 -0
- package/dist/server/agent/helper.js +122 -0
- package/dist/server/agent/mailroom.js +510 -0
- package/dist/server/agent/prompt.js +268 -0
- package/dist/server/agent/runner.js +1159 -0
- package/dist/server/artifacts.js +216 -0
- package/dist/server/budget.js +88 -0
- package/dist/server/data-export.js +105 -0
- package/dist/server/data-usage.js +144 -0
- package/dist/server/db/database.js +49 -0
- package/dist/server/db/migrations.js +343 -0
- package/dist/server/errors.js +16 -0
- package/dist/server/events.js +17 -0
- package/dist/server/export.js +378 -0
- package/dist/server/folder-picker.js +32 -0
- package/dist/server/from-jig.js +98 -0
- package/dist/server/http/agents.js +78 -0
- package/dist/server/http/api.js +465 -0
- package/dist/server/http/app.js +111 -0
- package/dist/server/http/artifacts.js +53 -0
- package/dist/server/http/data.js +54 -0
- package/dist/server/http/devices.js +47 -0
- package/dist/server/http/events.js +134 -0
- package/dist/server/http/idempotency.js +53 -0
- package/dist/server/http/input.js +106 -0
- package/dist/server/http/mail.js +52 -0
- package/dist/server/http/mcp.js +134 -0
- package/dist/server/http/notes.js +91 -0
- package/dist/server/http/problem.js +55 -0
- package/dist/server/http/providers.js +131 -0
- package/dist/server/http/schedules.js +58 -0
- package/dist/server/http/search.js +42 -0
- package/dist/server/http/security.js +168 -0
- package/dist/server/http/skills.js +53 -0
- package/dist/server/http/system.js +26 -0
- package/dist/server/ids.js +30 -0
- package/dist/server/keys.js +70 -0
- package/dist/server/mail/envelope.js +152 -0
- package/dist/server/mail/post.js +603 -0
- package/dist/server/mail/store.js +279 -0
- package/dist/server/manage.js +601 -0
- package/dist/server/mcp-builtin.js +180 -0
- package/dist/server/mcp-config.js +305 -0
- package/dist/server/mcp-oauth.js +448 -0
- package/dist/server/mcp.js +668 -0
- package/dist/server/model/gateway.js +410 -0
- package/dist/server/paths.js +43 -0
- package/dist/server/permissions/engine.js +249 -0
- package/dist/server/permissions/paths.js +93 -0
- package/dist/server/permissions/shell.js +635 -0
- package/dist/server/push.js +345 -0
- package/dist/server/reveal.js +36 -0
- package/dist/server/runtime.js +17 -0
- package/dist/server/schedules.js +356 -0
- package/dist/server/settings.js +77 -0
- package/dist/server/skills.js +263 -0
- package/dist/server/store.js +1077 -0
- package/dist/server/tools/files.js +318 -0
- package/dist/server/tools/html.js +221 -0
- package/dist/server/tools/jobs.js +38 -0
- package/dist/server/tools/mail.js +680 -0
- package/dist/server/tools/match.js +81 -0
- package/dist/server/tools/misc.js +253 -0
- package/dist/server/tools/process.js +128 -0
- package/dist/server/tools/read-state.js +40 -0
- package/dist/server/tools/registry.js +43 -0
- package/dist/server/tools/search.js +175 -0
- package/dist/server/tools/shell.js +124 -0
- package/dist/server/tools/types.js +43 -0
- package/dist/server/tools/web-search.js +198 -0
- package/dist/server/tools/web.js +240 -0
- package/dist/shared/cron.js +105 -0
- package/dist/shared/mcp-presets.js +170 -0
- package/dist/shared/messages.js +45 -0
- package/dist/shared/policy.js +42 -0
- package/dist/shared/types.js +34 -0
- package/dist/shared/version.js +8 -0
- package/mcp-builtin/package-lock.json +3398 -0
- package/mcp-builtin/package.json +14 -0
- package/mcp-builtin/presets/gitee/package-lock.json +105 -0
- package/mcp-builtin/presets/gitee/package.json +10 -0
- package/mcp-builtin/presets/notion/package-lock.json +2243 -0
- package/mcp-builtin/presets/notion/package.json +10 -0
- package/mcp-builtin/presets/sentry/package-lock.json +28 -0
- package/mcp-builtin/presets/sentry/package.json +10 -0
- package/package.json +75 -5
- package/web/dist/THIRD-PARTY-NOTICES.txt +1944 -0
- package/web/dist/assets/index-BtK_SzpF.css +1 -0
- package/web/dist/assets/index-C0rnUFI-.js +68 -0
- package/web/dist/icons/app-icon.png +0 -0
- package/web/dist/icons/apple-touch-icon.png +0 -0
- package/web/dist/icons/favicon-16.png +0 -0
- package/web/dist/icons/favicon-32.png +0 -0
- package/web/dist/icons/favicon-64.png +0 -0
- package/web/dist/icons/tenon-mark.svg +1 -0
- package/web/dist/icons/yew-mark.svg +1 -0
- package/web/dist/index.html +18 -0
- package/web/dist/mascot/gift.webp +0 -0
- package/web/dist/mascot/greeting.webp +0 -0
- package/web/dist/mascot/happy.webp +0 -0
- package/web/dist/mascot/lost.webp +0 -0
- package/web/dist/mascot/shush.webp +0 -0
- package/web/dist/mascot/sleepy.webp +0 -0
- package/web/dist/mascot/sorry.webp +0 -0
- package/web/dist/mascot/thinking.webp +0 -0
- package/web/dist/mascot/waking.webp +0 -0
- package/web/dist/mascot/writing.webp +0 -0
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { messageText } from "../../shared/messages.js";
|
|
2
|
+
import { isJigNote } from "../from-jig.js";
|
|
3
|
+
export const SUMMARY_HEADING = "[Summary of the earlier conversation, written by Yew when it shortened the context]";
|
|
4
|
+
export const REQUEST_HEADING = "[The latest message to you before that summary, word for word; it is still what you are working on]";
|
|
5
|
+
/** What Yew puts in front of its own messages to the model, which are stored like the person's. */
|
|
6
|
+
export const YEW_NOTE = "[From Yew, not from the person]";
|
|
7
|
+
/** Whether a stored message is one of Yew's own notes (or Jig's, from before the rename). */
|
|
8
|
+
export const isOwnNote = (text) => text.startsWith(YEW_NOTE) || isJigNote(text);
|
|
9
|
+
function placeholder(call, timestamp) {
|
|
10
|
+
return {
|
|
11
|
+
role: "toolResult",
|
|
12
|
+
toolCallId: call.id,
|
|
13
|
+
toolName: call.name,
|
|
14
|
+
content: [{ type: "text", text: "This call did not run." }],
|
|
15
|
+
isError: true,
|
|
16
|
+
timestamp,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Turns stored entries into what the model sees. Providers require each tool call to be answered right
|
|
21
|
+
* after the message that made it, so results are placed there whatever order they were stored in, and a
|
|
22
|
+
* call without a result (after a crash, say) gets a placeholder. Notices are for the person only.
|
|
23
|
+
*/
|
|
24
|
+
export function toModelMessages(entries) {
|
|
25
|
+
const results = new Map();
|
|
26
|
+
for (const entry of entries) {
|
|
27
|
+
if (entry.role === "toolResult" && !results.has(entry.message.toolCallId)) {
|
|
28
|
+
results.set(entry.message.toolCallId, entry.message);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
const out = [];
|
|
32
|
+
for (const entry of entries) {
|
|
33
|
+
switch (entry.role) {
|
|
34
|
+
case "compaction":
|
|
35
|
+
out.push({
|
|
36
|
+
role: "user",
|
|
37
|
+
content: entry.compaction.request
|
|
38
|
+
? `${SUMMARY_HEADING}\n\n${entry.compaction.summary}\n\n${REQUEST_HEADING}\n\n${entry.compaction.request}`
|
|
39
|
+
: `${SUMMARY_HEADING}\n\n${entry.compaction.summary}`,
|
|
40
|
+
timestamp: entry.compaction.timestamp,
|
|
41
|
+
});
|
|
42
|
+
break;
|
|
43
|
+
case "system":
|
|
44
|
+
case "user":
|
|
45
|
+
out.push(entry.message);
|
|
46
|
+
break;
|
|
47
|
+
case "assistant": {
|
|
48
|
+
const message = entry.message;
|
|
49
|
+
if (message.content.length === 0)
|
|
50
|
+
break;
|
|
51
|
+
out.push(message);
|
|
52
|
+
for (const block of message.content) {
|
|
53
|
+
if (block.type !== "toolCall")
|
|
54
|
+
continue;
|
|
55
|
+
out.push(results.get(block.id) ?? placeholder(block, message.timestamp));
|
|
56
|
+
}
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
default:
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return out;
|
|
64
|
+
}
|
|
65
|
+
/** An interrupted response may end in a half-written tool call; keep the text and drop the call. */
|
|
66
|
+
export function sanitizeInterrupted(message) {
|
|
67
|
+
if (message.stopReason !== "aborted" && message.stopReason !== "error")
|
|
68
|
+
return message;
|
|
69
|
+
return { ...message, content: message.content.filter((block) => block.type !== "toolCall") };
|
|
70
|
+
}
|
|
71
|
+
const TOOL_RESULT_CHARS = 2000;
|
|
72
|
+
/** The transcript as plain text for the summary request; tool results are cut short (prior-art 4.4). */
|
|
73
|
+
export function transcriptForSummary(entries) {
|
|
74
|
+
const lines = [];
|
|
75
|
+
for (const entry of entries) {
|
|
76
|
+
if (entry.role === "compaction") {
|
|
77
|
+
lines.push(`## Earlier summary\n${entry.compaction.summary}`);
|
|
78
|
+
if (entry.compaction.request)
|
|
79
|
+
lines.push(`## Person\n${entry.compaction.request}`);
|
|
80
|
+
}
|
|
81
|
+
else if (entry.role === "user")
|
|
82
|
+
lines.push(`## Person\n${messageText(entry.message)}`);
|
|
83
|
+
else if (entry.role === "system")
|
|
84
|
+
lines.push(`## Update from Yew\n${entry.message.content}`);
|
|
85
|
+
else if (entry.role === "assistant") {
|
|
86
|
+
const text = entry.message.content
|
|
87
|
+
.filter((block) => block.type === "text")
|
|
88
|
+
.map((block) => block.text)
|
|
89
|
+
.join("\n");
|
|
90
|
+
if (text)
|
|
91
|
+
lines.push(`## Agent\n${text}`);
|
|
92
|
+
for (const block of entry.message.content) {
|
|
93
|
+
if (block.type === "toolCall")
|
|
94
|
+
lines.push(`## Tool call: ${block.name}\n${JSON.stringify(block.arguments)}`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
else if (entry.role === "toolResult") {
|
|
98
|
+
let text = messageText(entry.message);
|
|
99
|
+
if (text.length > TOOL_RESULT_CHARS)
|
|
100
|
+
text = `${text.slice(0, TOOL_RESULT_CHARS)}\n[cut]`;
|
|
101
|
+
lines.push(`## Tool result: ${entry.message.toolName}${entry.message.isError ? " (error)" : ""}\n${text}`);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return lines.join("\n\n");
|
|
105
|
+
}
|
|
106
|
+
/** Rough token count (four characters per token), used to choose what compaction keeps. */
|
|
107
|
+
export function estimateTokens(entry) {
|
|
108
|
+
return Math.ceil(JSON.stringify(entry).length / 4);
|
|
109
|
+
}
|
|
110
|
+
/** What a response used, as Yew adds it up. */
|
|
111
|
+
export function usageTotals(message) {
|
|
112
|
+
const usage = message.usage;
|
|
113
|
+
return {
|
|
114
|
+
input: usage.input,
|
|
115
|
+
output: usage.output,
|
|
116
|
+
cacheRead: usage.cacheRead,
|
|
117
|
+
cacheWrite: usage.cacheWrite,
|
|
118
|
+
cost: usage.cost.total,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
@@ -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
|
+
}
|