@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,378 @@
|
|
|
1
|
+
import { askedQuestion } from "../shared/messages.js";
|
|
2
|
+
import { isOwnNote } from "./agent/context.js";
|
|
3
|
+
import { dollars } from "./budget.js";
|
|
4
|
+
const ZH = {
|
|
5
|
+
separator: ":",
|
|
6
|
+
sessionId: "会话 ID",
|
|
7
|
+
kind: "类型",
|
|
8
|
+
kindStanding: "常设会话",
|
|
9
|
+
kindTask: "任务会话",
|
|
10
|
+
group: "团队",
|
|
11
|
+
noGroup: "不属于团队",
|
|
12
|
+
workspace: "工作区",
|
|
13
|
+
chat: "聊天",
|
|
14
|
+
model: "模型",
|
|
15
|
+
created: "创建",
|
|
16
|
+
exported: "导出",
|
|
17
|
+
usage: "用量",
|
|
18
|
+
token: "token",
|
|
19
|
+
role: "职责说明",
|
|
20
|
+
you: "你",
|
|
21
|
+
thinking: "思考过程",
|
|
22
|
+
status: "状态",
|
|
23
|
+
toolCall: "工具调用",
|
|
24
|
+
arguments: "参数",
|
|
25
|
+
result: "结果",
|
|
26
|
+
changes: "改动",
|
|
27
|
+
summary: "较早记录的摘要",
|
|
28
|
+
instructionsUpdated: "说明文件有更新",
|
|
29
|
+
image: "图片",
|
|
30
|
+
stopped: "已停止",
|
|
31
|
+
modelChanged: "换了模型",
|
|
32
|
+
restarted: "Yew 重启时这个会话在干活,已标为待核对。检查过后点「继续」接着运行。",
|
|
33
|
+
changed: "{by} 改了这个会话的设置",
|
|
34
|
+
compacted: "较早的记录已压成摘要",
|
|
35
|
+
modelError: "模型请求失败",
|
|
36
|
+
compactFailed: "压缩上下文失败",
|
|
37
|
+
budgetReached: "今天的费用到了{scope}每日上限({spent} / {limit}),已停下",
|
|
38
|
+
budgetWarning: "今天的费用超过了{scope}每日上限的 80%({spent} / {limit})",
|
|
39
|
+
budgetSession: "这个会话的",
|
|
40
|
+
budgetAll: "全部会话的",
|
|
41
|
+
missingKey: "没有 {provider} 的 API Key",
|
|
42
|
+
error: "出错",
|
|
43
|
+
jobFinished: "后台任务 {job}({command})结束了{exit}。",
|
|
44
|
+
jobExit: ",退出码 {code}",
|
|
45
|
+
toolStatus: {
|
|
46
|
+
pending: "等待",
|
|
47
|
+
waiting: "等你授权",
|
|
48
|
+
running: "执行中",
|
|
49
|
+
done: "完成",
|
|
50
|
+
error: "出错",
|
|
51
|
+
denied: "被拒",
|
|
52
|
+
needs_review: "待核对",
|
|
53
|
+
},
|
|
54
|
+
tools: {
|
|
55
|
+
read: { done: "读取", running: "正在读取" },
|
|
56
|
+
write: { done: "写入", running: "正在写入" },
|
|
57
|
+
edit: { done: "编辑", running: "正在编辑" },
|
|
58
|
+
glob: { done: "查找文件", running: "正在查找文件" },
|
|
59
|
+
grep: { done: "搜索", running: "正在搜索" },
|
|
60
|
+
shell: { done: "运行", running: "正在运行" },
|
|
61
|
+
shell_job: { done: "查看后台任务", running: "正在查看后台任务" },
|
|
62
|
+
todo: { done: "更新任务清单", running: "正在更新任务清单" },
|
|
63
|
+
ask_user: { done: "提问", running: "正在提问" },
|
|
64
|
+
web_fetch: { done: "抓取网页", running: "正在抓取网页" },
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
const EN = {
|
|
68
|
+
separator: ": ",
|
|
69
|
+
sessionId: "Session ID",
|
|
70
|
+
kind: "Type",
|
|
71
|
+
kindStanding: "Standing session",
|
|
72
|
+
kindTask: "Task session",
|
|
73
|
+
group: "Team",
|
|
74
|
+
noGroup: "No team",
|
|
75
|
+
workspace: "Workspace",
|
|
76
|
+
chat: "Chat",
|
|
77
|
+
model: "Model",
|
|
78
|
+
created: "Created",
|
|
79
|
+
exported: "Exported",
|
|
80
|
+
usage: "Usage",
|
|
81
|
+
token: "tokens",
|
|
82
|
+
role: "Role",
|
|
83
|
+
you: "You",
|
|
84
|
+
thinking: "Thinking",
|
|
85
|
+
status: "Status",
|
|
86
|
+
toolCall: "Tool call",
|
|
87
|
+
arguments: "Arguments",
|
|
88
|
+
result: "Result",
|
|
89
|
+
changes: "Changes",
|
|
90
|
+
summary: "Summary of earlier messages",
|
|
91
|
+
instructionsUpdated: "Instruction files updated",
|
|
92
|
+
image: "Image",
|
|
93
|
+
stopped: "Stopped",
|
|
94
|
+
modelChanged: "Model changed",
|
|
95
|
+
restarted: "Yew restarted while this session was working, so it is marked for review. Check it, then choose Continue.",
|
|
96
|
+
changed: "{by} changed this session's settings",
|
|
97
|
+
compacted: "Earlier messages were summarized",
|
|
98
|
+
modelError: "The model request failed",
|
|
99
|
+
compactFailed: "Could not compact the context",
|
|
100
|
+
budgetReached: "Stopped: today's spending reached {scope} daily limit ({spent} of {limit})",
|
|
101
|
+
budgetWarning: "Today's spending passed 80% of {scope} daily limit ({spent} of {limit})",
|
|
102
|
+
budgetSession: "this session's",
|
|
103
|
+
budgetAll: "the shared",
|
|
104
|
+
missingKey: "There is no API key for {provider}",
|
|
105
|
+
error: "Error",
|
|
106
|
+
jobFinished: "Background job {job} ({command}) ended{exit}.",
|
|
107
|
+
jobExit: " with exit code {code}",
|
|
108
|
+
toolStatus: {
|
|
109
|
+
pending: "Waiting",
|
|
110
|
+
waiting: "Needs your permission",
|
|
111
|
+
running: "Running",
|
|
112
|
+
done: "Done",
|
|
113
|
+
error: "Error",
|
|
114
|
+
denied: "Declined",
|
|
115
|
+
needs_review: "Needs review",
|
|
116
|
+
},
|
|
117
|
+
tools: {
|
|
118
|
+
read: { done: "Read", running: "Reading" },
|
|
119
|
+
write: { done: "Wrote", running: "Writing" },
|
|
120
|
+
edit: { done: "Edited", running: "Editing" },
|
|
121
|
+
glob: { done: "Listed files", running: "Listing files" },
|
|
122
|
+
grep: { done: "Searched", running: "Searching" },
|
|
123
|
+
shell: { done: "Ran", running: "Running" },
|
|
124
|
+
shell_job: { done: "Checked job", running: "Checking job" },
|
|
125
|
+
todo: { done: "Updated tasks", running: "Updating tasks" },
|
|
126
|
+
ask_user: { done: "Asked", running: "Asking" },
|
|
127
|
+
web_fetch: { done: "Fetched", running: "Fetching" },
|
|
128
|
+
},
|
|
129
|
+
};
|
|
130
|
+
const STRINGS = { zh: ZH, en: EN };
|
|
131
|
+
/**
|
|
132
|
+
* The whole session as one Markdown file: who the session is and what was said, in order.
|
|
133
|
+
* Tool results are folded into the call they answer, the same way the interface shows them.
|
|
134
|
+
*/
|
|
135
|
+
export function sessionMarkdown(input) {
|
|
136
|
+
const strings = STRINGS[input.language];
|
|
137
|
+
const now = input.now ?? Date.now();
|
|
138
|
+
const sections = [`# ${input.session.name}`, metadata(input, strings, now)];
|
|
139
|
+
const responsibility = input.session.responsibility.trim();
|
|
140
|
+
if (responsibility)
|
|
141
|
+
sections.push(`## ${strings.role}\n\n${responsibility}`);
|
|
142
|
+
const toolCalls = new Map(input.toolCalls.map((call) => [call.callId, call]));
|
|
143
|
+
const toolResults = new Map();
|
|
144
|
+
for (const entry of input.entries) {
|
|
145
|
+
if (entry.role === "toolResult")
|
|
146
|
+
toolResults.set(entry.message.toolCallId, entry.message);
|
|
147
|
+
}
|
|
148
|
+
for (const entry of input.entries) {
|
|
149
|
+
const rendered = entryMarkdown(entry, input.session, strings, toolCalls, toolResults);
|
|
150
|
+
if (rendered)
|
|
151
|
+
sections.push(rendered);
|
|
152
|
+
}
|
|
153
|
+
return `${sections.join("\n\n")}\n`;
|
|
154
|
+
}
|
|
155
|
+
function metadata(input, strings, now) {
|
|
156
|
+
const { session } = input;
|
|
157
|
+
const separator = strings.separator;
|
|
158
|
+
const tokens = session.usage.input + session.usage.output + session.usage.cacheRead + session.usage.cacheWrite;
|
|
159
|
+
const lines = [
|
|
160
|
+
`- ${strings.sessionId}${separator}${session.id}`,
|
|
161
|
+
`- ${strings.kind}${separator}${session.kind === "standing" ? strings.kindStanding : strings.kindTask}`,
|
|
162
|
+
`- ${strings.group}${separator}${input.groupName ?? strings.noGroup}`,
|
|
163
|
+
`- ${strings.workspace}${separator}${session.cwd ?? strings.chat}`,
|
|
164
|
+
`- ${strings.model}${separator}${session.provider}/${session.model}`,
|
|
165
|
+
`- ${strings.created}${separator}${formatDateTime(session.createdAt)}`,
|
|
166
|
+
`- ${strings.exported}${separator}${formatDateTime(now)}`,
|
|
167
|
+
`- ${strings.usage}${separator}${formatNumber(tokens, input.language)} ${strings.token} · USD ${formatCost(session.usage.cost)}`,
|
|
168
|
+
];
|
|
169
|
+
return lines.join("\n");
|
|
170
|
+
}
|
|
171
|
+
function entryMarkdown(entry, session, strings, toolCalls, toolResults) {
|
|
172
|
+
switch (entry.role) {
|
|
173
|
+
case "user": {
|
|
174
|
+
const text = userText(entry.message, strings);
|
|
175
|
+
// Yew's own notes to the model; the interface hides them and shows the matching notice.
|
|
176
|
+
if (!text || isOwnNote(text))
|
|
177
|
+
return "";
|
|
178
|
+
return `## ${strings.you}\n\n${quote(text)}`;
|
|
179
|
+
}
|
|
180
|
+
case "assistant":
|
|
181
|
+
return assistantMarkdown(entry.message, session.name, strings, toolCalls, toolResults);
|
|
182
|
+
case "toolResult":
|
|
183
|
+
// Shown inside the tool call it answers.
|
|
184
|
+
return "";
|
|
185
|
+
case "system":
|
|
186
|
+
return `## ${strings.instructionsUpdated}\n\n${entry.message.content}`;
|
|
187
|
+
case "compaction":
|
|
188
|
+
return `## ${strings.summary}\n\n${entry.compaction.summary}`;
|
|
189
|
+
case "notice":
|
|
190
|
+
return `> ${noticeLine(entry.notice, strings)}`;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
function assistantMarkdown(message, sessionName, strings, toolCalls, toolResults) {
|
|
194
|
+
const blocks = [];
|
|
195
|
+
for (const block of message.content) {
|
|
196
|
+
if (block.type === "text") {
|
|
197
|
+
if (block.text.trim())
|
|
198
|
+
blocks.push(block.text);
|
|
199
|
+
}
|
|
200
|
+
else if (block.type === "thinking") {
|
|
201
|
+
if (block.thinking.trim())
|
|
202
|
+
blocks.push(quoteWithLabel(strings.thinking, block.thinking));
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
const result = toolResults.get(block.id);
|
|
206
|
+
blocks.push(toolCallMarkdown(block, toolCalls.get(block.id), result, strings));
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
if (blocks.length === 0)
|
|
210
|
+
return "";
|
|
211
|
+
return `## ${sessionName}\n\n${blocks.join("\n\n")}`;
|
|
212
|
+
}
|
|
213
|
+
function toolCallMarkdown(call, view, result, strings) {
|
|
214
|
+
const status = view?.status ?? (result ? (result.isError ? "error" : "done") : "pending");
|
|
215
|
+
const running = status === "running";
|
|
216
|
+
const verb = strings.tools[call.name]?.[running ? "running" : "done"] ?? call.name;
|
|
217
|
+
const target = callTarget(call.arguments);
|
|
218
|
+
const title = target ? `${verb} ${target}` : verb;
|
|
219
|
+
const parts = [`### ${strings.toolCall}${strings.separator}${title}`];
|
|
220
|
+
if (status !== "done") {
|
|
221
|
+
parts.push(`**${strings.status}**${strings.separator}${strings.toolStatus[status]}`);
|
|
222
|
+
}
|
|
223
|
+
parts.push(`**${strings.arguments}**\n\n${fenced(JSON.stringify(call.arguments, null, 2), "json")}`);
|
|
224
|
+
const diff = view?.detail?.diff;
|
|
225
|
+
if (typeof diff === "string" && diff.trim()) {
|
|
226
|
+
parts.push(`**${strings.changes}**\n\n${fenced(diff, "diff")}`);
|
|
227
|
+
}
|
|
228
|
+
const output = result ? toolResultText(result, strings) : "";
|
|
229
|
+
if (output)
|
|
230
|
+
parts.push(`**${strings.result}**\n\n${output}`);
|
|
231
|
+
return parts.join("\n\n");
|
|
232
|
+
}
|
|
233
|
+
function toolResultText(result, strings) {
|
|
234
|
+
const parts = [];
|
|
235
|
+
for (const block of result.content) {
|
|
236
|
+
if (block.type === "text") {
|
|
237
|
+
if (block.text.trim())
|
|
238
|
+
parts.push(fenced(block.text, "text"));
|
|
239
|
+
}
|
|
240
|
+
else {
|
|
241
|
+
parts.push(imageMarkdown(block, strings));
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
return parts.join("\n\n");
|
|
245
|
+
}
|
|
246
|
+
function userText(message, strings) {
|
|
247
|
+
if (typeof message.content === "string")
|
|
248
|
+
return message.content;
|
|
249
|
+
const parts = [];
|
|
250
|
+
for (const block of message.content) {
|
|
251
|
+
if (block.type === "text") {
|
|
252
|
+
if (block.text)
|
|
253
|
+
parts.push(block.text);
|
|
254
|
+
}
|
|
255
|
+
else {
|
|
256
|
+
parts.push(imageMarkdown(block, strings));
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return parts.join("\n\n");
|
|
260
|
+
}
|
|
261
|
+
function imageMarkdown(block, strings) {
|
|
262
|
+
return ``;
|
|
263
|
+
}
|
|
264
|
+
function noticeLine(notice, strings) {
|
|
265
|
+
switch (notice.kind) {
|
|
266
|
+
case "interrupted":
|
|
267
|
+
return strings.stopped;
|
|
268
|
+
case "model_changed":
|
|
269
|
+
return `${strings.modelChanged}${strings.separator}${notice.text ?? ""}`;
|
|
270
|
+
case "restarted":
|
|
271
|
+
return strings.restarted;
|
|
272
|
+
case "compacted":
|
|
273
|
+
return strings.compacted;
|
|
274
|
+
case "changed": {
|
|
275
|
+
const by = typeof notice.data?.by === "string" ? notice.data.by : "";
|
|
276
|
+
return strings.changed.replace("{by}", () => by);
|
|
277
|
+
}
|
|
278
|
+
case "budget": {
|
|
279
|
+
const data = notice.data ?? {};
|
|
280
|
+
const amount = (value) => (typeof value === "number" ? dollars(value) : "");
|
|
281
|
+
return (data.code === "budget-reached" ? strings.budgetReached : strings.budgetWarning)
|
|
282
|
+
.replace("{scope}", data.scope === "session" ? strings.budgetSession : strings.budgetAll)
|
|
283
|
+
.replace("{spent}", () => amount(data.spent))
|
|
284
|
+
.replace("{limit}", () => amount(data.limit));
|
|
285
|
+
}
|
|
286
|
+
case "error": {
|
|
287
|
+
const code = typeof notice.data?.code === "string" ? notice.data.code : "";
|
|
288
|
+
if (code === "model-error") {
|
|
289
|
+
return `${strings.modelError}${strings.separator}${notice.text ?? ""}`;
|
|
290
|
+
}
|
|
291
|
+
if (code === "compact-failed") {
|
|
292
|
+
return `${strings.compactFailed}${strings.separator}${notice.text ?? ""}`;
|
|
293
|
+
}
|
|
294
|
+
if (code === "missing-api-key") {
|
|
295
|
+
const provider = typeof notice.data?.provider === "string" ? notice.data.provider : "";
|
|
296
|
+
return strings.missingKey.replace("{provider}", () => provider);
|
|
297
|
+
}
|
|
298
|
+
return `${strings.error}${strings.separator}${notice.text ?? ""}`;
|
|
299
|
+
}
|
|
300
|
+
case "job_finished": {
|
|
301
|
+
const data = notice.data ?? {};
|
|
302
|
+
const job = data.job;
|
|
303
|
+
const command = data.command;
|
|
304
|
+
if (typeof job !== "string" || typeof command !== "string") {
|
|
305
|
+
return notice.text ?? strings.jobFinished;
|
|
306
|
+
}
|
|
307
|
+
const exit = typeof data.exitCode === "number"
|
|
308
|
+
? strings.jobExit.replace("{code}", String(data.exitCode))
|
|
309
|
+
: "";
|
|
310
|
+
return strings.jobFinished
|
|
311
|
+
.replace("{job}", () => job)
|
|
312
|
+
.replace("{command}", () => command)
|
|
313
|
+
.replace("{exit}", () => exit);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
function callTarget(args) {
|
|
318
|
+
const asked = askedQuestion(args);
|
|
319
|
+
if (asked)
|
|
320
|
+
return oneLine(asked.question);
|
|
321
|
+
const value = args.command ?? args.path ?? args.pattern ?? args.url ?? args.job;
|
|
322
|
+
return typeof value === "string" ? oneLine(value) : "";
|
|
323
|
+
}
|
|
324
|
+
/** One line, at most `max` characters, for a heading. */
|
|
325
|
+
function oneLine(text, max = 140) {
|
|
326
|
+
const flat = text.replace(/\s+/g, " ").trim();
|
|
327
|
+
const characters = [...flat];
|
|
328
|
+
return characters.length > max ? `${characters.slice(0, max).join("")}…` : flat;
|
|
329
|
+
}
|
|
330
|
+
/** Every line quoted, so what the person wrote is not read as the assistant's Markdown. */
|
|
331
|
+
function quote(text) {
|
|
332
|
+
return text
|
|
333
|
+
.split("\n")
|
|
334
|
+
.map((line) => (line.length > 0 ? `> ${line}` : ">"))
|
|
335
|
+
.join("\n");
|
|
336
|
+
}
|
|
337
|
+
function quoteWithLabel(label, text) {
|
|
338
|
+
const lines = [`**${label}**`, "", ...text.split("\n")];
|
|
339
|
+
return lines.map((line) => (line.length > 0 ? `> ${line}` : ">")).join("\n");
|
|
340
|
+
}
|
|
341
|
+
/** A fenced block whose fence is longer than any run of backticks inside, so the content survives. */
|
|
342
|
+
function fenced(text, language) {
|
|
343
|
+
let longest = 0;
|
|
344
|
+
for (const match of text.matchAll(/`+/g))
|
|
345
|
+
longest = Math.max(longest, match[0].length);
|
|
346
|
+
const fence = "`".repeat(Math.max(3, longest + 1));
|
|
347
|
+
return `${fence}${language}\n${text.replace(/\n$/, "")}\n${fence}`;
|
|
348
|
+
}
|
|
349
|
+
function formatNumber(value, language) {
|
|
350
|
+
return new Intl.NumberFormat(language === "zh" ? "zh-CN" : "en-US").format(value);
|
|
351
|
+
}
|
|
352
|
+
function formatCost(value) {
|
|
353
|
+
return value > 0 && value < 0.01 ? "<0.01" : value.toFixed(2);
|
|
354
|
+
}
|
|
355
|
+
/** `2026-11-09 14:30 UTC+8`, with the machine's own offset so the time is unambiguous. */
|
|
356
|
+
function formatDateTime(ms) {
|
|
357
|
+
const date = new Date(ms);
|
|
358
|
+
const pad = (value) => String(value).padStart(2, "0");
|
|
359
|
+
const offset = -date.getTimezoneOffset();
|
|
360
|
+
const sign = offset >= 0 ? "+" : "-";
|
|
361
|
+
const hours = Math.floor(Math.abs(offset) / 60);
|
|
362
|
+
const minutes = Math.abs(offset) % 60;
|
|
363
|
+
const zone = `UTC${sign}${hours}${minutes > 0 ? `:${pad(minutes)}` : ""}`;
|
|
364
|
+
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())} ${zone}`;
|
|
365
|
+
}
|
|
366
|
+
/** A readable file name: the session's name, today's date and `.md`, safe on every platform. */
|
|
367
|
+
export function exportFileName(name, at = new Date()) {
|
|
368
|
+
const clean = name
|
|
369
|
+
.replace(/[\\/:*?"<>|]/g, "-")
|
|
370
|
+
.replace(/\p{Cc}/gu, "-")
|
|
371
|
+
.replace(/\s+/g, " ")
|
|
372
|
+
.trim()
|
|
373
|
+
.replace(/[. ]+$/, "");
|
|
374
|
+
const base = ([...clean].slice(0, 80).join("") || "session").trim();
|
|
375
|
+
const pad = (value) => String(value).padStart(2, "0");
|
|
376
|
+
const date = `${at.getFullYear()}-${pad(at.getMonth() + 1)}-${pad(at.getDate())}`;
|
|
377
|
+
return `${base}-${date}.md`;
|
|
378
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { YewError } from "./errors.js";
|
|
3
|
+
const runOsascript = (args) => new Promise((resolve, reject) => {
|
|
4
|
+
// Waits for as long as the person keeps the picker open, within reason.
|
|
5
|
+
execFile("osascript", args, { timeout: 10 * 60_000 }, (error, stdout, stderr) => {
|
|
6
|
+
if (error)
|
|
7
|
+
reject(Object.assign(error, { stderr }));
|
|
8
|
+
else
|
|
9
|
+
resolve(stdout);
|
|
10
|
+
});
|
|
11
|
+
});
|
|
12
|
+
/**
|
|
13
|
+
* Opens the system folder picker on the computer that runs Yew and returns the chosen folder, or null
|
|
14
|
+
* when the person cancels. The script is fixed text with nothing from the request in it. Only macOS has
|
|
15
|
+
* a picker Yew can open this way; elsewhere the interface asks for a typed path.
|
|
16
|
+
*/
|
|
17
|
+
export async function pickFolder(platform = process.platform, run = runOsascript) {
|
|
18
|
+
if (platform !== "darwin") {
|
|
19
|
+
throw new YewError("not-supported", 501, "Yew cannot open a folder picker on this computer. Type the folder's path instead.");
|
|
20
|
+
}
|
|
21
|
+
try {
|
|
22
|
+
const out = (await run(["-e", "activate", "-e", "POSIX path of (choose folder)"])).trim();
|
|
23
|
+
return out.length > 1 ? out.replace(/\/+$/, "") : out;
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
const text = `${error.message} ${error.stderr ?? ""}`;
|
|
27
|
+
// osascript reports a cancelled dialog as error -128.
|
|
28
|
+
if (text.includes("-128"))
|
|
29
|
+
return null;
|
|
30
|
+
throw new YewError("picker-failed", 500, "The folder picker could not be opened.");
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What is left of Jig, Yew's name until 2026-11-18 (blueprint, section 12): only what carries a
|
|
3
|
+
* person's data and settings over from Jig, so upgrading loses nothing. Everything else uses Yew's
|
|
4
|
+
* names. Remove this file, and the few places that use it, once nobody upgrades from Jig any more.
|
|
5
|
+
*/
|
|
6
|
+
import { existsSync, lstatSync, readFileSync, renameSync, symlinkSync } from "node:fs";
|
|
7
|
+
import { homedir } from "node:os";
|
|
8
|
+
import { join, resolve } from "node:path";
|
|
9
|
+
/** Jig's data folder, in the home folder. */
|
|
10
|
+
const JIG_FOLDER = ".jig";
|
|
11
|
+
/** Jig's database file. */
|
|
12
|
+
export const JIG_DATABASE = "jig.db";
|
|
13
|
+
/** Jig's database files, for counting what a data folder holds before they are renamed. */
|
|
14
|
+
export const JIG_DATABASE_FILES = ["jig.db", "jig.db-wal", "jig.db-shm"];
|
|
15
|
+
/** Where Jig kept keys and tokens in the system keychain. */
|
|
16
|
+
export const JIG_KEYCHAIN_SERVICE = "asia.tenon.jig";
|
|
17
|
+
/** Jig's login service: the LaunchAgent's label on macOS, the systemd unit on Linux. */
|
|
18
|
+
export const JIG_SERVICE_LABEL = "cn.tenonai.apps.jig";
|
|
19
|
+
export const JIG_SERVICE_UNIT = "jig.service";
|
|
20
|
+
/** Jig's launcher app, and the program inside that shows it is the one Jig made. */
|
|
21
|
+
export const JIG_APP = "Jig.app";
|
|
22
|
+
export const JIG_LAUNCHER = "jig-launcher";
|
|
23
|
+
/** A project folder Jig read skills from. */
|
|
24
|
+
export const JIG_PROJECT_FOLDER = ".jig";
|
|
25
|
+
/** How Jig marked its own notes in a conversation. */
|
|
26
|
+
const JIG_NOTE = "[From Jig, not from the person]";
|
|
27
|
+
/** Whether a stored message is a note Jig wrote, not something the person wrote. */
|
|
28
|
+
export const isJigNote = (text) => text.startsWith(JIG_NOTE);
|
|
29
|
+
/** An environment variable by Yew's name, or by Jig's: `YEW_HOME`, else `JIG_HOME`. */
|
|
30
|
+
export function fromJigEnv(env, name) {
|
|
31
|
+
return env[`YEW_${name}`] || env[`JIG_${name}`] || undefined;
|
|
32
|
+
}
|
|
33
|
+
/** A real folder, not a link to one. */
|
|
34
|
+
function isFolder(path) {
|
|
35
|
+
try {
|
|
36
|
+
return lstatSync(path).isDirectory();
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/** Jig's data folder while it has not been moved to Yew's: there, real, and Yew's not made yet. */
|
|
43
|
+
export function unmovedJigHome(home, user = homedir()) {
|
|
44
|
+
const old = join(user, JIG_FOLDER);
|
|
45
|
+
return !existsSync(home) && isFolder(old) ? old : null;
|
|
46
|
+
}
|
|
47
|
+
/** The process a server running from this folder has, by its serve file; null when none runs. */
|
|
48
|
+
function runningIn(home) {
|
|
49
|
+
try {
|
|
50
|
+
const { pid } = JSON.parse(readFileSync(join(home, "serve.json"), "utf8"));
|
|
51
|
+
if (typeof pid !== "number")
|
|
52
|
+
return null;
|
|
53
|
+
process.kill(pid, 0);
|
|
54
|
+
return pid;
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/** Jig's database file becomes Yew's, with its write-ahead log and shared memory. */
|
|
61
|
+
function renameDatabase(home) {
|
|
62
|
+
if (existsSync(join(home, "yew.db")))
|
|
63
|
+
return;
|
|
64
|
+
for (const suffix of ["", "-wal", "-shm"]) {
|
|
65
|
+
const from = join(home, `jig.db${suffix}`);
|
|
66
|
+
if (existsSync(from))
|
|
67
|
+
renameSync(from, join(home, `yew.db${suffix}`));
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* The first time Yew runs, Jig's data folder `~/.jig` moves to `~/.yew` and a link to it is left at
|
|
72
|
+
* `~/.jig`, so paths written down before (in conversations, scripts, the person's notes) still work;
|
|
73
|
+
* the database file becomes `yew.db`. In a folder set by `YEW_HOME` or `JIG_HOME` only the database
|
|
74
|
+
* file is renamed. Nothing moves while a server runs from the folder: then the result names it, and
|
|
75
|
+
* Yew goes on using the folder where it is.
|
|
76
|
+
*/
|
|
77
|
+
export function moveJigHome(env = process.env, user = homedir()) {
|
|
78
|
+
const custom = fromJigEnv(env, "HOME");
|
|
79
|
+
const home = custom ? resolve(custom) : join(user, ".yew");
|
|
80
|
+
const old = unmovedJigHome(home, user);
|
|
81
|
+
if (!custom && old) {
|
|
82
|
+
const busy = runningIn(old);
|
|
83
|
+
if (busy !== null)
|
|
84
|
+
return { busy, folder: old };
|
|
85
|
+
renameSync(old, home);
|
|
86
|
+
symlinkSync(home, old);
|
|
87
|
+
renameDatabase(home);
|
|
88
|
+
return { moved: old, to: home };
|
|
89
|
+
}
|
|
90
|
+
if (existsSync(join(home, JIG_DATABASE)) && !existsSync(join(home, "yew.db"))) {
|
|
91
|
+
const busy = runningIn(home);
|
|
92
|
+
if (busy !== null)
|
|
93
|
+
return { busy, folder: home };
|
|
94
|
+
renameDatabase(home);
|
|
95
|
+
return { renamed: home };
|
|
96
|
+
}
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { Hono } from "hono";
|
|
2
|
+
import { invalid } from "../errors.js";
|
|
3
|
+
import { idempotent } from "./idempotency.js";
|
|
4
|
+
import { optArray, optEnum, optString, readJson, reqString } from "./input.js";
|
|
5
|
+
/** One answer of a structured question card: option indexes and the person's own words. */
|
|
6
|
+
function questionAnswer(value) {
|
|
7
|
+
const fields = (value && typeof value === "object" ? value : null);
|
|
8
|
+
const choices = fields?.choices;
|
|
9
|
+
if (!fields || !Array.isArray(choices) || choices.some((choice) => !Number.isInteger(choice))) {
|
|
10
|
+
throw invalid("Each answer needs a list of option numbers in choices.", "answers");
|
|
11
|
+
}
|
|
12
|
+
const text = fields.text;
|
|
13
|
+
if (text !== undefined && (typeof text !== "string" || text.length > 10_000)) {
|
|
14
|
+
throw invalid("An answer's text must be text of at most 10000 characters.", "answers");
|
|
15
|
+
}
|
|
16
|
+
return { choices: choices, ...(typeof text === "string" ? { text } : {}) };
|
|
17
|
+
}
|
|
18
|
+
/** Sending messages, stopping, continuing, and answering approvals and questions. */
|
|
19
|
+
/**
|
|
20
|
+
* A decision made on a paired phone. "Always allow" for a tool that acts with the person's sign-ins
|
|
21
|
+
* is only given where its scope is said (spec 21.2), and the phone's card does not show that yet:
|
|
22
|
+
* from a phone it allows this one call. Security review, 2026-11-16.
|
|
23
|
+
*/
|
|
24
|
+
export function decisionFromPhone(decision, request) {
|
|
25
|
+
const warned = request?.warning !== undefined;
|
|
26
|
+
return decision === "allow_always" && warned ? "allow_once" : decision;
|
|
27
|
+
}
|
|
28
|
+
export function agentRoutes(store, agents, log) {
|
|
29
|
+
const app = new Hono();
|
|
30
|
+
const once = idempotent(store.db);
|
|
31
|
+
// The person deletes all archived sessions for good (settings, data); the interface asks first.
|
|
32
|
+
app.delete("/sessions/archived", (c) => c.json({ deleted: agents.deleteArchived() }));
|
|
33
|
+
app.post("/sessions/:id/messages", once, async (c) => {
|
|
34
|
+
const fields = await readJson(c);
|
|
35
|
+
const result = agents.send(c.req.param("id"), reqString(fields, "text", 100_000));
|
|
36
|
+
return c.json(result, "entry" in result ? 201 : 202);
|
|
37
|
+
});
|
|
38
|
+
app.post("/sessions/:id/stop", (c) => {
|
|
39
|
+
const id = c.req.param("id");
|
|
40
|
+
store.getSession(id);
|
|
41
|
+
agents.stop(id);
|
|
42
|
+
return c.body(null, 204);
|
|
43
|
+
});
|
|
44
|
+
app.post("/sessions/:id/continue", (c) => {
|
|
45
|
+
const id = c.req.param("id");
|
|
46
|
+
agents.continue(id);
|
|
47
|
+
return c.json(store.getSession(id), 202);
|
|
48
|
+
});
|
|
49
|
+
// The conversation goes back to a reply, or to just before one of the person's messages.
|
|
50
|
+
app.post("/sessions/:id/rewind", async (c) => {
|
|
51
|
+
const id = c.req.param("id");
|
|
52
|
+
const withdrawn = agents.rewind(id, reqString(await readJson(c), "entryId", 100));
|
|
53
|
+
return c.json({ withdrawn, session: store.getSession(id) });
|
|
54
|
+
});
|
|
55
|
+
app.post("/sessions/:id/compact", (c) => {
|
|
56
|
+
const id = c.req.param("id");
|
|
57
|
+
store.getSession(id);
|
|
58
|
+
void agents.compactNow(id).catch((error) => log(`compact ${id}: ${error.message}`));
|
|
59
|
+
return c.body(null, 202);
|
|
60
|
+
});
|
|
61
|
+
app.post("/approvals/:id", async (c) => {
|
|
62
|
+
const fields = await readJson(c);
|
|
63
|
+
const asked = optEnum(fields, "decision", ["allow_once", "allow_always", "deny"]);
|
|
64
|
+
const decision = asked && c.env?.device
|
|
65
|
+
? decisionFromPhone(asked, store.getApproval(c.req.param("id")).request)
|
|
66
|
+
: asked;
|
|
67
|
+
const reason = optString(fields, "reason", 2000);
|
|
68
|
+
const answer = optString(fields, "answer", 10_000);
|
|
69
|
+
const answers = optArray(fields, "answers")?.map(questionAnswer);
|
|
70
|
+
return c.json(agents.answer(c.req.param("id"), {
|
|
71
|
+
...(decision ? { decision } : {}),
|
|
72
|
+
...(reason ? { reason } : {}),
|
|
73
|
+
...(answer !== undefined ? { answer } : {}),
|
|
74
|
+
...(answers ? { answers } : {}),
|
|
75
|
+
}));
|
|
76
|
+
});
|
|
77
|
+
return app;
|
|
78
|
+
}
|