@uncarved/jade 0.1.0 → 0.1.2
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/README.md +198 -188
- package/dist/distro.d.ts +12 -1
- package/dist/distro.js +37 -2
- package/dist/distro.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/main.js +159 -29
- package/dist/main.js.map +1 -1
- package/dist/mcp.js +1 -1
- package/dist/mcp.js.map +1 -1
- package/dist/plugins/policy.d.ts +41 -9
- package/dist/plugins/policy.js +367 -58
- package/dist/plugins/policy.js.map +1 -1
- package/dist/plugins/prompts.d.ts +10 -0
- package/dist/plugins/prompts.js +29 -2
- package/dist/plugins/prompts.js.map +1 -1
- package/dist/plugins/repeats.js +42 -7
- package/dist/plugins/repeats.js.map +1 -1
- package/dist/plugins/review.d.ts +16 -0
- package/dist/plugins/review.js +92 -0
- package/dist/plugins/review.js.map +1 -0
- package/dist/provider.d.ts +62 -3
- package/dist/provider.js +150 -7
- package/dist/provider.js.map +1 -1
- package/dist/sessions.d.ts +9 -0
- package/dist/sessions.js +14 -1
- package/dist/sessions.js.map +1 -1
- package/dist/sys.d.ts +5 -1
- package/dist/sys.js +3 -2
- package/dist/sys.js.map +1 -1
- package/dist/tools/screenshot.d.ts +15 -0
- package/dist/tools/screenshot.js +162 -0
- package/dist/tools/screenshot.js.map +1 -0
- package/dist/tools/shell.d.ts +6 -0
- package/dist/tools/shell.js +26 -7
- package/dist/tools/shell.js.map +1 -1
- package/dist/tools/task.d.ts +4 -0
- package/dist/tools/task.js +4 -1
- package/dist/tools/task.js.map +1 -1
- package/dist/ui/configure.js +121 -28
- package/dist/ui/configure.js.map +1 -1
- package/dist/ui/markdown.d.ts +17 -0
- package/dist/ui/markdown.js +204 -0
- package/dist/ui/markdown.js.map +1 -0
- package/dist/ui/render.d.ts +2 -0
- package/dist/ui/render.js +10 -0
- package/dist/ui/render.js.map +1 -1
- package/dist/ui/screen.d.ts +8 -0
- package/dist/ui/screen.js +3 -0
- package/dist/ui/screen.js.map +1 -1
- package/dist/ui/transcript.js +85 -13
- package/dist/ui/transcript.js.map +1 -1
- package/dist/ui/tui.d.ts +17 -0
- package/dist/ui/tui.js +367 -93
- package/dist/ui/tui.js.map +1 -1
- package/package.json +5 -5
- package/src/distro.ts +46 -4
- package/src/index.ts +2 -0
- package/src/main.ts +156 -31
- package/src/mcp.ts +1 -1
- package/src/plugins/policy.ts +366 -51
- package/src/plugins/prompts.ts +36 -2
- package/src/plugins/repeats.ts +42 -8
- package/src/plugins/review.ts +109 -0
- package/src/provider.ts +170 -11
- package/src/sessions.ts +14 -1
- package/src/sys.ts +5 -2
- package/src/tools/screenshot.ts +171 -0
- package/src/tools/shell.ts +25 -7
- package/src/tools/task.ts +5 -1
- package/src/ui/configure.ts +127 -29
- package/src/ui/markdown.ts +219 -0
- package/src/ui/render.ts +9 -0
- package/src/ui/screen.ts +5 -0
- package/src/ui/transcript.ts +84 -16
- package/src/ui/tui.tsx +480 -142
package/dist/index.js
CHANGED
|
@@ -7,11 +7,13 @@ export * from "./plugins/budget.js";
|
|
|
7
7
|
export * from "./plugins/compact.js";
|
|
8
8
|
export * from "./plugins/checks.js";
|
|
9
9
|
export * from "./plugins/policy.js";
|
|
10
|
+
export * from "./plugins/review.js";
|
|
10
11
|
export * from "./plugins/prompts.js";
|
|
11
12
|
export * from "./plugins/schema.js";
|
|
12
13
|
export * from "./tools/fs.js";
|
|
13
14
|
export * from "./tools/grep.js";
|
|
14
15
|
export * from "./tools/web.js";
|
|
16
|
+
export * from "./tools/screenshot.js";
|
|
15
17
|
export * from "./tools/shell.js";
|
|
16
18
|
export * from "./tools/task.js";
|
|
17
19
|
export * from "./tools/checkpoint.js";
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC;AAC9B,cAAc,eAAe,CAAC;AAC9B,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC;AAC9B,cAAc,qBAAqB,CAAC;AACpC,cAAc,sBAAsB,CAAC;AACrC,cAAc,qBAAqB,CAAC;AACpC,cAAc,qBAAqB,CAAC;AACpC,cAAc,sBAAsB,CAAC;AACrC,cAAc,qBAAqB,CAAC;AACpC,cAAc,eAAe,CAAC;AAC9B,cAAc,iBAAiB,CAAC;AAChC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kBAAkB,CAAC;AACjC,cAAc,iBAAiB,CAAC;AAChC,cAAc,uBAAuB,CAAC;AACtC,cAAc,iBAAiB,CAAC;AAChC,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,eAAe,CAAC;AAC9B,cAAc,qBAAqB,CAAC;AACpC,cAAc,sBAAsB,CAAC;AACrC,cAAc,uBAAuB,CAAC;AACtC,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC;AAC9B,cAAc,eAAe,CAAC;AAC9B,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC;AAC9B,cAAc,qBAAqB,CAAC;AACpC,cAAc,sBAAsB,CAAC;AACrC,cAAc,qBAAqB,CAAC;AACpC,cAAc,qBAAqB,CAAC;AACpC,cAAc,qBAAqB,CAAC;AACpC,cAAc,sBAAsB,CAAC;AACrC,cAAc,qBAAqB,CAAC;AACpC,cAAc,eAAe,CAAC;AAC9B,cAAc,iBAAiB,CAAC;AAChC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,uBAAuB,CAAC;AACtC,cAAc,kBAAkB,CAAC;AACjC,cAAc,iBAAiB,CAAC;AAChC,cAAc,uBAAuB,CAAC;AACtC,cAAc,iBAAiB,CAAC;AAChC,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,eAAe,CAAC;AAC9B,cAAc,qBAAqB,CAAC;AACpC,cAAc,sBAAsB,CAAC;AACrC,cAAc,uBAAuB,CAAC;AACtC,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC"}
|
package/dist/main.js
CHANGED
|
@@ -9,7 +9,7 @@ import os from "node:os";
|
|
|
9
9
|
import path from "node:path";
|
|
10
10
|
import { createHash } from "node:crypto";
|
|
11
11
|
import { JsonlLogStore, JsonlTrustStore } from "@uncarved/store-jsonl";
|
|
12
|
-
import { readAll, Runtime, UntrustedPlugin, } from "uncarved";
|
|
12
|
+
import { readAll, Runtime, RuntimeBusy, UntrustedPlugin, } from "uncarved";
|
|
13
13
|
import { realpathSync } from "node:fs";
|
|
14
14
|
import { expandCommand, loadCommands } from "./commands.js";
|
|
15
15
|
import { discoverPlugins, inspectPlugin, loadPlugin } from "./plugins/loader.js";
|
|
@@ -17,20 +17,21 @@ import { readPluginSettings } from "./plugins/install.js";
|
|
|
17
17
|
import { PLUGIN_HELP, pluginCli, UsageError } from "./plugin-cli.js";
|
|
18
18
|
import { describeServer, loadMcpConfig, McpClient, mcpTrustHash } from "./mcp.js";
|
|
19
19
|
import { loadSkills } from "./skills.js";
|
|
20
|
-
import { hasProvider, loadEnvFile, parseTokens } from "./config.js";
|
|
20
|
+
import { hasProvider, loadEnvFile, parseTokens, updateEnvFile } from "./config.js";
|
|
21
21
|
import { createJade } from "./distro.js";
|
|
22
22
|
import { describeRule, MODES, POLICY_KEY } from "./plugins/policy.js";
|
|
23
23
|
import { compactThreshold, contextWindowFromEnv, contextWindowInfo, isContextOverflow } from "./context.js";
|
|
24
24
|
import { InputHistory } from "./history.js";
|
|
25
25
|
import { attachMentions, mentionCandidates } from "./mentions.js";
|
|
26
26
|
import { repoRoot } from "./plugins/prompts.js";
|
|
27
|
-
import { providerFromEnv, providerInfo, switchable, withRetry } from "./provider.js";
|
|
28
|
-
import { SessionIndex } from "./sessions.js";
|
|
27
|
+
import { isRateLimit, providerFromEnv, providerInfo, RateGate, rpmFromEnv, switchable, withRetry } from "./provider.js";
|
|
28
|
+
import { discardIfEmpty, SessionIndex } from "./sessions.js";
|
|
29
29
|
import { lastTurnChanges, UNDO_KEY } from "./tools/checkpoint.js";
|
|
30
30
|
import { display } from "./tools/fs.js";
|
|
31
31
|
import { bold, dim, green, plainScreen, red, yellow } from "./ui/screen.js";
|
|
32
32
|
import { configurator } from "./ui/configure.js";
|
|
33
|
-
import {
|
|
33
|
+
import { reviewer } from "./plugins/review.js";
|
|
34
|
+
import { callDetails, clip, preview, shortPath, summarize } from "./ui/render.js";
|
|
34
35
|
import { transcript } from "./ui/transcript.js";
|
|
35
36
|
import { run, VERSION } from "./sys.js";
|
|
36
37
|
const HELP = `Usage: jade [options] [prompt]
|
|
@@ -39,7 +40,7 @@ const HELP = `Usage: jade [options] [prompt]
|
|
|
39
40
|
-p, --print <prompt> run one prompt and exit
|
|
40
41
|
-c, --continue continue the most recent session in this directory
|
|
41
42
|
-r, --resume [id] resume a session by id, or pick one from a list
|
|
42
|
-
-m, --mode <mode> permission mode: ${MODES.join(" | ")} (default ask)
|
|
43
|
+
-m, --mode <mode> permission mode for this run: ${MODES.join(" | ")} (default: the last mode you switched to, or ask)
|
|
43
44
|
--cwd <dir> working directory (default: current directory)
|
|
44
45
|
-v, --version
|
|
45
46
|
-h, --help
|
|
@@ -62,11 +63,11 @@ const COMMANDS = [
|
|
|
62
63
|
{ name: "hooks", about: "execution order of every extension point" },
|
|
63
64
|
{ name: "exit", about: "quit (the session stays resumable)" },
|
|
64
65
|
];
|
|
65
|
-
/**
|
|
66
|
-
const INSTANT = new Set(["help", "model", "usage", "rules", "hooks", "plugins", "exit", "quit"]);
|
|
66
|
+
/** 运行中也立即执行的命令(不碰运行时,或者只读;/mode 运行中先记在 pendingMode) */
|
|
67
|
+
const INSTANT = new Set(["help", "model", "mode", "usage", "rules", "hooks", "plugins", "exit", "quit"]);
|
|
67
68
|
/** 内置插件提供的命令(/memory 在 jade/memory 里,等等):名字保留给它们,自定义命令不能占用 */
|
|
68
69
|
const BUILTIN_PLUGIN_COMMANDS = ["memory", "checks", "mcp", "skills", "compact"];
|
|
69
|
-
const KEYS = `Keys: enter send · \\ or alt+enter new line · ↑↓ history · @ reference a file · esc interrupt · shift+tab cycle ask/auto
|
|
70
|
+
const KEYS = `Keys: enter send · \\ or alt+enter new line · ↑↓ history · @ reference a file · esc interrupt (twice to also clear queued messages) · shift+tab cycle ask/auto/plan · ctrl+c twice to exit
|
|
70
71
|
mouse wheel or PgUp/PgDn scroll the conversation · ctrl+home/ctrl+end jump to the top/bottom · shift+drag selects text
|
|
71
72
|
While jade is working you can keep typing: messages are queued and sent when the current turn ends.`;
|
|
72
73
|
/** 回复被输出上限截断后,自动让模型接着做的次数(每条用户消息) */
|
|
@@ -154,6 +155,11 @@ if (!(await stat(cwd).catch(() => undefined))?.isDirectory())
|
|
|
154
155
|
const home = process.env.JADE_HOME ?? path.join(os.homedir(), ".jade");
|
|
155
156
|
const envFile = path.join(home, ".env");
|
|
156
157
|
await loadEnvFile(envFile);
|
|
158
|
+
/** 新会话的权限模式:-m 只管这一次;没给时用上次在会话里切换到的模式(JADE_MODE,见 rememberMode),都没有时是 ask */
|
|
159
|
+
const savedMode = process.env.JADE_MODE?.trim();
|
|
160
|
+
const badSavedMode = savedMode && !MODES.includes(savedMode) ? savedMode : undefined;
|
|
161
|
+
if (savedMode && !badSavedMode)
|
|
162
|
+
opts.mode ??= savedMode;
|
|
157
163
|
const tui = Boolean(process.stdin.isTTY && process.stdout.isTTY) && opts.print === undefined;
|
|
158
164
|
if (!hasProvider() && !tui) {
|
|
159
165
|
console.error(`jade: no model is configured.\n\n` +
|
|
@@ -263,11 +269,39 @@ else {
|
|
|
263
269
|
fail(errorText(err));
|
|
264
270
|
}
|
|
265
271
|
}
|
|
272
|
+
/**
|
|
273
|
+
* 外面包一层重试和限速。限速对这个服务的所有请求(主 Agent、子 Agent、压缩)共用:
|
|
274
|
+
* JADE_RPM 设了上限时把请求平均分布在一分钟里;被限流时所有请求一起等,而不是各自去撞
|
|
275
|
+
*/
|
|
266
276
|
function retrying(inner) {
|
|
277
|
+
/** 状态行正显示着排队或等待重试:请求发出后换回来 */
|
|
278
|
+
let waiting = false;
|
|
279
|
+
const gate = new RateGate({
|
|
280
|
+
rpm: rpmFromEnv(),
|
|
281
|
+
onWait: (ms) => {
|
|
282
|
+
if (ms < 1000)
|
|
283
|
+
return;
|
|
284
|
+
waiting = true;
|
|
285
|
+
screen.busy(`Pacing requests (${gate.rpm ? `${+gate.rpm.toFixed(1)}/min` : "rate limited"}, ${seconds(ms)})`);
|
|
286
|
+
},
|
|
287
|
+
onLearn: (rpm) => screen.log(dim(`[rate limited: spacing requests to ${rpm} per minute from now on; set the exact limit with /model → requests per minute]`)),
|
|
288
|
+
});
|
|
267
289
|
return withRetry(inner, {
|
|
268
|
-
|
|
290
|
+
gate,
|
|
291
|
+
onRequest: () => {
|
|
292
|
+
if (waiting && running)
|
|
293
|
+
screen.busy("Waiting for the model");
|
|
294
|
+
waiting = false;
|
|
295
|
+
},
|
|
296
|
+
onRetry: ({ attempt, retries, delayMs, error }) => {
|
|
297
|
+
const why = isRateLimit(error) ? "rate limited" : clip(error.message.replace(/\s+/g, " "), 160);
|
|
298
|
+
screen.log(yellow(`[${why} · retry ${attempt}/${retries} in ${seconds(delayMs)}]`));
|
|
299
|
+
waiting = true;
|
|
300
|
+
screen.busy(`Waiting to retry (${attempt}/${retries}, ${seconds(delayMs)})`);
|
|
301
|
+
},
|
|
269
302
|
});
|
|
270
303
|
}
|
|
304
|
+
const seconds = (ms) => (ms >= 60_000 ? `${Math.floor(ms / 60_000)}m${Math.round((ms % 60_000) / 1000) ? `${Math.round((ms % 60_000) / 1000)}s` : ""}` : `${Math.max(1, Math.round(ms / 1000))}s`);
|
|
271
305
|
const store = new JsonlLogStore(path.join(home, "sessions"));
|
|
272
306
|
const trust = new JsonlTrustStore(path.join(home, "trust.jsonl"));
|
|
273
307
|
const index = new SessionIndex(path.join(home, "index.jsonl"));
|
|
@@ -279,6 +313,24 @@ for (const w of skillsLoaded.warnings)
|
|
|
279
313
|
screen.log(yellow(`[warning] ${w}`));
|
|
280
314
|
const skills = skillsLoaded.skills;
|
|
281
315
|
const mcpClients = await connectMcp();
|
|
316
|
+
/**
|
|
317
|
+
* 运行中切换的权限模式:这一轮正占着日志,写不进去。关卡先按它判断(下一个工具调用就生效),
|
|
318
|
+
* 这一轮结束后再记进日志(flushPendingMode)
|
|
319
|
+
*/
|
|
320
|
+
let pendingMode;
|
|
321
|
+
/** 当前配置的模型和服务,写进系统提示词,用户问起时模型照这个回答;/model 改了配置后下一次请求就是新的 */
|
|
322
|
+
function currentModel() {
|
|
323
|
+
if (!hasProvider())
|
|
324
|
+
return undefined;
|
|
325
|
+
const info = providerInfo();
|
|
326
|
+
let host;
|
|
327
|
+
try {
|
|
328
|
+
host = info.baseURL ? new URL(info.baseURL).host : undefined;
|
|
329
|
+
}
|
|
330
|
+
catch { }
|
|
331
|
+
const api = { anthropic: "the Anthropic API", openai: "the OpenAI Responses API", "openai-compat": "an OpenAI-compatible API" }[info.kind];
|
|
332
|
+
return { name: info.model, via: host ? `${api} at ${host}` : api };
|
|
333
|
+
}
|
|
282
334
|
/** 每个会话用一套新的插件实例:插件里有按会话记的状态(例如压缩、重复调用的计数) */
|
|
283
335
|
const makeJade = (mode) => createJade({
|
|
284
336
|
cwd,
|
|
@@ -286,6 +338,7 @@ const makeJade = (mode) => createJade({
|
|
|
286
338
|
store,
|
|
287
339
|
provider,
|
|
288
340
|
mode,
|
|
341
|
+
model: currentModel,
|
|
289
342
|
extensions: pluginSources,
|
|
290
343
|
disabled: disabledPlugins,
|
|
291
344
|
mcp: mcpClients,
|
|
@@ -293,6 +346,9 @@ const makeJade = (mode) => createJade({
|
|
|
293
346
|
compactTokens: compactAt,
|
|
294
347
|
approveSubagent,
|
|
295
348
|
sharedRules: () => [...settings().rules, ...pendingRules],
|
|
349
|
+
pendingMode: () => pendingMode,
|
|
350
|
+
// auto 模式的审查用当前的模型;rt 换会话时跟着换,所以每次取
|
|
351
|
+
review: reviewer({ provider, cwd, messages: () => rt.state.messages }),
|
|
296
352
|
approveCheck: process.env.JADE_CHECKS === "off" ? undefined : approveCheck,
|
|
297
353
|
checkConfirmed: (c) => trust.has(checkHash(c)),
|
|
298
354
|
checksUnavailable: process.env.JADE_CHECKS === "off" ? "Edit-time checks are turned off by JADE_CHECKS=off." : undefined,
|
|
@@ -300,6 +356,8 @@ const makeJade = (mode) => createJade({
|
|
|
300
356
|
let jade = await makeJade(opts.mode);
|
|
301
357
|
for (const w of jade.warnings)
|
|
302
358
|
screen.log(yellow(`[warning] ${w}`));
|
|
359
|
+
if (badSavedMode)
|
|
360
|
+
screen.log(yellow(`[warning] JADE_MODE=${badSavedMode} is not a mode (${MODES.join(", ")}); starting in ${opts.mode ?? "ask"}`));
|
|
303
361
|
for (const c of shadowed)
|
|
304
362
|
screen.log(yellow(`[warning] custom command ${display(cwd, c.file)} is ignored: /${c.name} is a built-in command`));
|
|
305
363
|
// 插件的命令加进菜单(名字在各个会话的实例之间不变)。内置命令和 .jade/commands 里的同名命令优先
|
|
@@ -434,11 +492,47 @@ let rt = resumeId
|
|
|
434
492
|
: await Runtime.create({ store, provider, plugins: jade.plugins });
|
|
435
493
|
let sessionId = rt.state.sessionId;
|
|
436
494
|
// 权限设置记在日志里(derived),关卡从那里读,恢复会话后仍然有效
|
|
495
|
+
// 主会话不会是 readonly(只给子 Agent 用;旧会话里记的 readonly 由关卡换成 plan)
|
|
437
496
|
const settings = () => jade.policy.settings(rt.state.derived);
|
|
438
497
|
const saveSettings = async (next) => {
|
|
439
498
|
await rt.record(POLICY_KEY, next);
|
|
499
|
+
pendingMode = undefined;
|
|
440
500
|
screen.status({ mode: next.mode });
|
|
441
501
|
};
|
|
502
|
+
/**
|
|
503
|
+
* 用户切换的模式记进 $JADE_HOME/.env 的 JADE_MODE,之后新开的会话默认用它(恢复的会话仍用它自己记下的模式)。
|
|
504
|
+
* yolo 不记:免得以后一打开就什么都不问;要默认 yolo 可以手动写 JADE_MODE=yolo 或用 -m yolo
|
|
505
|
+
*/
|
|
506
|
+
async function rememberMode(mode) {
|
|
507
|
+
if (mode === "yolo" || process.env.JADE_MODE === mode)
|
|
508
|
+
return;
|
|
509
|
+
await updateEnvFile(envFile, { JADE_MODE: mode }).catch((err) => screen.log(yellow(`[warning] could not remember the mode in ${envFile}: ${errorText(err)}`)));
|
|
510
|
+
}
|
|
511
|
+
/** 换权限模式;运行时正在执行这一轮时先记在 pendingMode,关卡立即按它判断,这一轮结束后再写进日志 */
|
|
512
|
+
async function setMode(mode) {
|
|
513
|
+
await rememberMode(mode);
|
|
514
|
+
try {
|
|
515
|
+
await saveSettings({ ...settings(), mode });
|
|
516
|
+
}
|
|
517
|
+
catch (err) {
|
|
518
|
+
if (!(err instanceof RuntimeBusy))
|
|
519
|
+
throw err;
|
|
520
|
+
pendingMode = mode;
|
|
521
|
+
screen.status({ mode });
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
/** 把运行中切换的模式写进日志 */
|
|
525
|
+
async function flushPendingMode() {
|
|
526
|
+
if (pendingMode === undefined)
|
|
527
|
+
return;
|
|
528
|
+
try {
|
|
529
|
+
await saveSettings(settings());
|
|
530
|
+
}
|
|
531
|
+
catch (err) {
|
|
532
|
+
if (!(err instanceof RuntimeBusy))
|
|
533
|
+
screen.log(red(`[error] ${errorText(err)}`));
|
|
534
|
+
}
|
|
535
|
+
}
|
|
442
536
|
// ---------- 渲染 ----------
|
|
443
537
|
const view = transcript({ screen, cwd, store, contextWindow: () => contextWindow, compactAt: () => compactAt(), canRetry: () => canRetry() });
|
|
444
538
|
rt.subscribe(view.onNotice);
|
|
@@ -456,7 +550,9 @@ async function ask(c, who, remember) {
|
|
|
456
550
|
{ label: "Yes", value: "yes" },
|
|
457
551
|
{ label: `Yes, and always allow ${always} in this session`, value: "always" },
|
|
458
552
|
{ label: "No, and tell jade what to do instead", value: "no" },
|
|
459
|
-
],
|
|
553
|
+
],
|
|
554
|
+
// 关卡给了理由的(例如 auto 模式下审查转给用户的),说明为什么要问
|
|
555
|
+
{ body: (c.reason ? `${yellow(c.reason)}\n\n` : "") + (await preview(c, cwd)) });
|
|
460
556
|
if (answer === "always")
|
|
461
557
|
await remember(rule);
|
|
462
558
|
const approved = answer === "yes" || answer === "always";
|
|
@@ -464,7 +560,10 @@ async function ask(c, who, remember) {
|
|
|
464
560
|
? undefined
|
|
465
561
|
: (answer === "no" ? await screen.input("What should jade do instead? (optional)") : undefined) || "The user rejected this action.";
|
|
466
562
|
// 主 Agent 的调用的结果之后接在这一行后面
|
|
467
|
-
screen.log(approved ? green(`✓ ${who}${head}`) : red(`✗ ${who}${head}`) + dim(reason ? ` — ${clip(reason, 200)}` : ""), {
|
|
563
|
+
screen.log(approved ? green(`✓ ${who}${head}`) : red(`✗ ${who}${head}`) + dim(reason ? ` — ${clip(reason, 200)}` : ""), {
|
|
564
|
+
id: c.toolUseId,
|
|
565
|
+
details: callDetails(c.name, c.input),
|
|
566
|
+
});
|
|
468
567
|
return { approved, reason };
|
|
469
568
|
}
|
|
470
569
|
async function approvePending() {
|
|
@@ -645,24 +744,52 @@ async function occupy(fn) {
|
|
|
645
744
|
finally {
|
|
646
745
|
running = false;
|
|
647
746
|
work = undefined;
|
|
747
|
+
interruptPending = false;
|
|
648
748
|
screen.busy(undefined);
|
|
749
|
+
await flushPendingMode();
|
|
649
750
|
}
|
|
650
751
|
}
|
|
752
|
+
/**
|
|
753
|
+
* 上一次打断的时间;正在收尾的这一轮结束、开始下一条排队的输入之前一直算"刚打断过"(interruptPending)。
|
|
754
|
+
* 刚打断过时再按一次 esc,连排队的消息一起清掉
|
|
755
|
+
*/
|
|
756
|
+
let interruptedAt = 0;
|
|
757
|
+
let interruptPending = false;
|
|
758
|
+
/** 第二次 esc 算作"再按一次"的时间窗口(从打断的那一轮结束、开始下一条排队的输入算起) */
|
|
759
|
+
const SECOND_ESC_MS = 3000;
|
|
651
760
|
function interrupt() {
|
|
652
761
|
if (!running)
|
|
653
762
|
return;
|
|
763
|
+
const again = interruptPending || Date.now() - interruptedAt < SECOND_ESC_MS;
|
|
764
|
+
interruptedAt = Date.now();
|
|
765
|
+
interruptPending = true;
|
|
654
766
|
commandAbort?.abort(new Error("interrupted by user"));
|
|
655
767
|
rt.abort("interrupted by user");
|
|
656
768
|
screen.busy("Interrupting");
|
|
769
|
+
if (queue.length === 0)
|
|
770
|
+
return;
|
|
771
|
+
if (again) {
|
|
772
|
+
const n = queue.splice(0).length;
|
|
773
|
+
screen.queued(queue);
|
|
774
|
+
screen.log(dim(`[cleared ${n} queued message${n > 1 ? "s" : ""}]`));
|
|
775
|
+
}
|
|
776
|
+
else {
|
|
777
|
+
screen.log(dim(`[${queue.length} queued message${queue.length > 1 ? "s" : ""} will still be sent; press esc again to clear ${queue.length > 1 ? "them" : "it"}]`));
|
|
778
|
+
}
|
|
657
779
|
}
|
|
658
|
-
/**
|
|
780
|
+
/**
|
|
781
|
+
* 离开会话但保留它:写入 session.detached,插件的收尾写入完成后再退出(4.2)。
|
|
782
|
+
* 没收到过输入的会话没什么可恢复的,删掉它的日志;返回会话是否保留了下来
|
|
783
|
+
*/
|
|
659
784
|
async function leave() {
|
|
660
785
|
jade.shutdown();
|
|
661
786
|
try {
|
|
662
787
|
await rt.detach();
|
|
788
|
+
return !(await discardIfEmpty(store, sessionId));
|
|
663
789
|
}
|
|
664
790
|
catch (err) {
|
|
665
791
|
screen.log(red(`[error] ${errorText(err)}`));
|
|
792
|
+
return true;
|
|
666
793
|
}
|
|
667
794
|
}
|
|
668
795
|
/** 关掉界面后再提示怎么恢复:终端界面退出备用屏幕后,里面的内容就看不到了 */
|
|
@@ -691,6 +818,7 @@ async function quit(code = 0) {
|
|
|
691
818
|
* 恢复失败时留在当前会话
|
|
692
819
|
*/
|
|
693
820
|
async function switchSession(id) {
|
|
821
|
+
await flushPendingMode();
|
|
694
822
|
const mode = settings().mode;
|
|
695
823
|
const next = await makeJade(mode);
|
|
696
824
|
let nextRt;
|
|
@@ -703,7 +831,7 @@ async function switchSession(id) {
|
|
|
703
831
|
next.shutdown();
|
|
704
832
|
return screen.log(red(`[error] could not resume ${id}: ${errorText(err)}`));
|
|
705
833
|
}
|
|
706
|
-
await leave();
|
|
834
|
+
const kept = await leave();
|
|
707
835
|
const old = sessionId;
|
|
708
836
|
jade = next;
|
|
709
837
|
rt = nextRt;
|
|
@@ -714,9 +842,9 @@ async function switchSession(id) {
|
|
|
714
842
|
announcedMode = undefined;
|
|
715
843
|
pendingRules.length = 0;
|
|
716
844
|
screen.clear();
|
|
717
|
-
screen.status({ session: sessionId, mode: settings().mode, context: undefined, cache: undefined, spent: undefined });
|
|
845
|
+
screen.status({ session: sessionId, mode: settings().mode, context: undefined, cache: undefined, spent: undefined, speed: undefined });
|
|
718
846
|
if (!id)
|
|
719
|
-
return screen.log(dim(`Started a new session. The previous one can be resumed with /resume or: jade --resume ${old}`));
|
|
847
|
+
return screen.log(dim(kept ? `Started a new session. The previous one can be resumed with /resume or: jade --resume ${old}` : "Started a new session."));
|
|
720
848
|
if (!(await resumeSession())) {
|
|
721
849
|
screen.log(dim("Starting a new session instead."));
|
|
722
850
|
await switchSession();
|
|
@@ -787,32 +915,31 @@ function announceMode() {
|
|
|
787
915
|
notes.push(PLAN_OFF);
|
|
788
916
|
announcedMode = mode;
|
|
789
917
|
}
|
|
790
|
-
/** Shift+Tab:ask → auto
|
|
791
|
-
const CYCLE = ["ask", "auto
|
|
918
|
+
/** Shift+Tab:ask → auto → plan → ask;yolo 也回到 ask。运行中也能切换,从下一个工具调用起生效 */
|
|
919
|
+
const CYCLE = ["ask", "auto", "plan"];
|
|
792
920
|
function cycleMode() {
|
|
793
|
-
if (running)
|
|
794
|
-
return screen.log(dim("[the mode can be changed when jade is idle, or queue /mode <mode>]"));
|
|
795
921
|
const i = CYCLE.indexOf(settings().mode);
|
|
796
|
-
|
|
797
|
-
saveSettings({ ...settings(), mode: next }).catch((err) => screen.log(red(`[error] ${errorText(err)}`)));
|
|
922
|
+
setMode(CYCLE[(i + 1) % CYCLE.length]).catch((err) => screen.log(red(`[error] ${errorText(err)}`)));
|
|
798
923
|
}
|
|
799
924
|
/**
|
|
800
925
|
* 计划模式下模型给出计划、这一轮正常结束后,问用户是否按计划执行。
|
|
801
926
|
* 用户已经排了别的输入、或者不能交互时不问
|
|
802
927
|
*/
|
|
803
928
|
async function offerPlan() {
|
|
804
|
-
|
|
929
|
+
// 这一轮中途才切到计划模式的,模型并不知道要给计划,不问
|
|
930
|
+
if (settings().mode !== "plan" || announcedMode !== "plan" || view.lastEnd !== "end" || !screen.interactive || queue.length > 0)
|
|
805
931
|
return;
|
|
806
932
|
const last = rt.state.messages.at(-1);
|
|
807
933
|
if (last?.role !== "assistant" || !last.content.some((b) => b.kind === "text" && b.text.trim()))
|
|
808
934
|
return;
|
|
809
935
|
const choice = await screen.select("Execute this plan?", [
|
|
810
|
-
{ label: "Yes,
|
|
936
|
+
{ label: "Yes, in auto mode", value: "auto", hint: "edits and safe commands run without asking" },
|
|
811
937
|
{ label: "Yes, and ask before each edit", value: "ask", hint: "mode ask" },
|
|
812
938
|
{ label: "No, keep planning", value: "plan", hint: "type what to change" },
|
|
813
939
|
]);
|
|
814
940
|
if (!choice || choice === "plan")
|
|
815
941
|
return;
|
|
942
|
+
await rememberMode(choice);
|
|
816
943
|
await saveSettings({ ...settings(), mode: choice });
|
|
817
944
|
screen.log(dim(`mode: ${choice}`));
|
|
818
945
|
await say("The plan is approved. Implement it now.");
|
|
@@ -956,8 +1083,8 @@ async function command(input) {
|
|
|
956
1083
|
if (!MODES.includes(m))
|
|
957
1084
|
screen.log(red(`unknown mode ${m}`));
|
|
958
1085
|
else
|
|
959
|
-
await
|
|
960
|
-
screen.log(`mode: ${settings().mode}`);
|
|
1086
|
+
await setMode(m);
|
|
1087
|
+
screen.log(`mode: ${settings().mode}${pendingMode ? dim(" (from the next tool call)") : ""}`);
|
|
961
1088
|
break;
|
|
962
1089
|
}
|
|
963
1090
|
case "undo":
|
|
@@ -1025,10 +1152,9 @@ async function command(input) {
|
|
|
1025
1152
|
}
|
|
1026
1153
|
const MODE_HINTS = {
|
|
1027
1154
|
ask: "ask before edits and commands",
|
|
1028
|
-
|
|
1029
|
-
yolo: "never ask",
|
|
1030
|
-
readonly: "read only, no edits or commands",
|
|
1155
|
+
auto: "edits in this directory run without asking; other commands are reviewed by the model, risky ones still ask",
|
|
1031
1156
|
plan: "research read-only and propose a plan; edits start after you approve it",
|
|
1157
|
+
yolo: "never ask",
|
|
1032
1158
|
};
|
|
1033
1159
|
// ---------- 输入 ----------
|
|
1034
1160
|
/** 等待执行的输入:运行中提交的消息和需要独占运行时的命令排在这里,按顺序执行 */
|
|
@@ -1064,6 +1190,10 @@ async function pump() {
|
|
|
1064
1190
|
let exit = false;
|
|
1065
1191
|
await occupy(async () => {
|
|
1066
1192
|
while (queue.length > 0 && !quitting) {
|
|
1193
|
+
// 打断的那一轮已经结束:从现在起再给 SECOND_ESC_MS 的时间按第二次 esc
|
|
1194
|
+
if (interruptPending)
|
|
1195
|
+
interruptedAt = Date.now();
|
|
1196
|
+
interruptPending = false;
|
|
1067
1197
|
const text = queue.shift();
|
|
1068
1198
|
screen.queued(queue);
|
|
1069
1199
|
echo(text);
|