@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/src/main.ts
CHANGED
|
@@ -13,6 +13,7 @@ import { JsonlLogStore, JsonlTrustStore } from "@uncarved/store-jsonl";
|
|
|
13
13
|
import {
|
|
14
14
|
readAll,
|
|
15
15
|
Runtime,
|
|
16
|
+
RuntimeBusy,
|
|
16
17
|
UntrustedPlugin,
|
|
17
18
|
type Block,
|
|
18
19
|
type Plugin,
|
|
@@ -26,22 +27,23 @@ import { readPluginSettings } from "./plugins/install.ts";
|
|
|
26
27
|
import { PLUGIN_HELP, pluginCli, UsageError } from "./plugin-cli.ts";
|
|
27
28
|
import { describeServer, loadMcpConfig, McpClient, mcpTrustHash } from "./mcp.ts";
|
|
28
29
|
import { loadSkills } from "./skills.ts";
|
|
29
|
-
import { hasProvider, loadEnvFile, parseTokens } from "./config.ts";
|
|
30
|
+
import { hasProvider, loadEnvFile, parseTokens, updateEnvFile } from "./config.ts";
|
|
30
31
|
import { createJade, type Jade } from "./distro.ts";
|
|
31
32
|
import { describeRule, MODES, POLICY_KEY, type AllowRule, type Mode, type PolicySettings } from "./plugins/policy.ts";
|
|
32
33
|
import { compactThreshold, contextWindowFromEnv, contextWindowInfo, isContextOverflow } from "./context.ts";
|
|
33
34
|
import { InputHistory } from "./history.ts";
|
|
34
35
|
import { attachMentions, mentionCandidates } from "./mentions.ts";
|
|
35
36
|
import { repoRoot } from "./plugins/prompts.ts";
|
|
36
|
-
import { providerFromEnv, providerInfo, switchable, withRetry } from "./provider.ts";
|
|
37
|
-
import { SessionIndex } from "./sessions.ts";
|
|
37
|
+
import { isRateLimit, providerFromEnv, providerInfo, RateGate, rpmFromEnv, switchable, withRetry } from "./provider.ts";
|
|
38
|
+
import { discardIfEmpty, SessionIndex } from "./sessions.ts";
|
|
38
39
|
import { lastTurnChanges, UNDO_KEY, type Checkpoint, type UndoState } from "./tools/checkpoint.ts";
|
|
39
40
|
import { display } from "./tools/fs.ts";
|
|
40
41
|
import type { ApproveSubagent, SubagentApproval } from "./tools/task.ts";
|
|
41
42
|
import type { Check } from "./plugins/checks.ts";
|
|
42
43
|
import { bold, dim, green, plainScreen, red, yellow, type CommandInfo, type Screen } from "./ui/screen.ts";
|
|
43
44
|
import { configurator } from "./ui/configure.ts";
|
|
44
|
-
import {
|
|
45
|
+
import { reviewer } from "./plugins/review.ts";
|
|
46
|
+
import { callDetails, clip, preview, shortPath, summarize } from "./ui/render.ts";
|
|
45
47
|
import { transcript } from "./ui/transcript.ts";
|
|
46
48
|
import { run, VERSION } from "./sys.ts";
|
|
47
49
|
|
|
@@ -51,7 +53,7 @@ const HELP = `Usage: jade [options] [prompt]
|
|
|
51
53
|
-p, --print <prompt> run one prompt and exit
|
|
52
54
|
-c, --continue continue the most recent session in this directory
|
|
53
55
|
-r, --resume [id] resume a session by id, or pick one from a list
|
|
54
|
-
-m, --mode <mode> permission mode: ${MODES.join(" | ")} (default ask)
|
|
56
|
+
-m, --mode <mode> permission mode for this run: ${MODES.join(" | ")} (default: the last mode you switched to, or ask)
|
|
55
57
|
--cwd <dir> working directory (default: current directory)
|
|
56
58
|
-v, --version
|
|
57
59
|
-h, --help
|
|
@@ -75,12 +77,12 @@ const COMMANDS: CommandInfo[] = [
|
|
|
75
77
|
{ name: "hooks", about: "execution order of every extension point" },
|
|
76
78
|
{ name: "exit", about: "quit (the session stays resumable)" },
|
|
77
79
|
];
|
|
78
|
-
/**
|
|
79
|
-
const INSTANT = new Set(["help", "model", "usage", "rules", "hooks", "plugins", "exit", "quit"]);
|
|
80
|
+
/** 运行中也立即执行的命令(不碰运行时,或者只读;/mode 运行中先记在 pendingMode) */
|
|
81
|
+
const INSTANT = new Set(["help", "model", "mode", "usage", "rules", "hooks", "plugins", "exit", "quit"]);
|
|
80
82
|
/** 内置插件提供的命令(/memory 在 jade/memory 里,等等):名字保留给它们,自定义命令不能占用 */
|
|
81
83
|
const BUILTIN_PLUGIN_COMMANDS = ["memory", "checks", "mcp", "skills", "compact"];
|
|
82
84
|
|
|
83
|
-
const KEYS = `Keys: enter send · \\ or alt+enter new line · ↑↓ history · @ reference a file · esc interrupt · shift+tab cycle ask/auto
|
|
85
|
+
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
|
|
84
86
|
mouse wheel or PgUp/PgDn scroll the conversation · ctrl+home/ctrl+end jump to the top/bottom · shift+drag selects text
|
|
85
87
|
While jade is working you can keep typing: messages are queued and sent when the current turn ends.`;
|
|
86
88
|
|
|
@@ -164,6 +166,11 @@ const home = process.env.JADE_HOME ?? path.join(os.homedir(), ".jade");
|
|
|
164
166
|
const envFile = path.join(home, ".env");
|
|
165
167
|
await loadEnvFile(envFile);
|
|
166
168
|
|
|
169
|
+
/** 新会话的权限模式:-m 只管这一次;没给时用上次在会话里切换到的模式(JADE_MODE,见 rememberMode),都没有时是 ask */
|
|
170
|
+
const savedMode = process.env.JADE_MODE?.trim();
|
|
171
|
+
const badSavedMode = savedMode && !(MODES as readonly string[]).includes(savedMode) ? savedMode : undefined;
|
|
172
|
+
if (savedMode && !badSavedMode) opts.mode ??= savedMode as Mode;
|
|
173
|
+
|
|
167
174
|
const tui = Boolean(process.stdin.isTTY && process.stdout.isTTY) && opts.print === undefined;
|
|
168
175
|
if (!hasProvider() && !tui) {
|
|
169
176
|
console.error(
|
|
@@ -280,12 +287,40 @@ if (!hasProvider()) {
|
|
|
280
287
|
}
|
|
281
288
|
}
|
|
282
289
|
|
|
290
|
+
/**
|
|
291
|
+
* 外面包一层重试和限速。限速对这个服务的所有请求(主 Agent、子 Agent、压缩)共用:
|
|
292
|
+
* JADE_RPM 设了上限时把请求平均分布在一分钟里;被限流时所有请求一起等,而不是各自去撞
|
|
293
|
+
*/
|
|
283
294
|
function retrying(inner: Provider) {
|
|
295
|
+
/** 状态行正显示着排队或等待重试:请求发出后换回来 */
|
|
296
|
+
let waiting = false;
|
|
297
|
+
const gate = new RateGate({
|
|
298
|
+
rpm: rpmFromEnv(),
|
|
299
|
+
onWait: (ms) => {
|
|
300
|
+
if (ms < 1000) return;
|
|
301
|
+
waiting = true;
|
|
302
|
+
screen.busy(`Pacing requests (${gate.rpm ? `${+gate.rpm.toFixed(1)}/min` : "rate limited"}, ${seconds(ms)})`);
|
|
303
|
+
},
|
|
304
|
+
onLearn: (rpm) =>
|
|
305
|
+
screen.log(dim(`[rate limited: spacing requests to ${rpm} per minute from now on; set the exact limit with /model → requests per minute]`)),
|
|
306
|
+
});
|
|
284
307
|
return withRetry(inner, {
|
|
285
|
-
|
|
308
|
+
gate,
|
|
309
|
+
onRequest: () => {
|
|
310
|
+
if (waiting && running) screen.busy("Waiting for the model");
|
|
311
|
+
waiting = false;
|
|
312
|
+
},
|
|
313
|
+
onRetry: ({ attempt, retries, delayMs, error }) => {
|
|
314
|
+
const why = isRateLimit(error) ? "rate limited" : clip(error.message.replace(/\s+/g, " "), 160);
|
|
315
|
+
screen.log(yellow(`[${why} · retry ${attempt}/${retries} in ${seconds(delayMs)}]`));
|
|
316
|
+
waiting = true;
|
|
317
|
+
screen.busy(`Waiting to retry (${attempt}/${retries}, ${seconds(delayMs)})`);
|
|
318
|
+
},
|
|
286
319
|
});
|
|
287
320
|
}
|
|
288
321
|
|
|
322
|
+
const seconds = (ms: number) => (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`);
|
|
323
|
+
|
|
289
324
|
const store = new JsonlLogStore(path.join(home, "sessions"));
|
|
290
325
|
const trust = new JsonlTrustStore(path.join(home, "trust.jsonl"));
|
|
291
326
|
const index = new SessionIndex(path.join(home, "index.jsonl"));
|
|
@@ -297,6 +332,22 @@ const skillsLoaded = await loadSkills({ home, root: repo, cwd: realPath(cwd), pl
|
|
|
297
332
|
for (const w of skillsLoaded.warnings) screen.log(yellow(`[warning] ${w}`));
|
|
298
333
|
const skills = skillsLoaded.skills;
|
|
299
334
|
const mcpClients = await connectMcp();
|
|
335
|
+
/**
|
|
336
|
+
* 运行中切换的权限模式:这一轮正占着日志,写不进去。关卡先按它判断(下一个工具调用就生效),
|
|
337
|
+
* 这一轮结束后再记进日志(flushPendingMode)
|
|
338
|
+
*/
|
|
339
|
+
let pendingMode: Mode | undefined;
|
|
340
|
+
/** 当前配置的模型和服务,写进系统提示词,用户问起时模型照这个回答;/model 改了配置后下一次请求就是新的 */
|
|
341
|
+
function currentModel(): { name: string; via: string } | undefined {
|
|
342
|
+
if (!hasProvider()) return undefined;
|
|
343
|
+
const info = providerInfo();
|
|
344
|
+
let host: string | undefined;
|
|
345
|
+
try {
|
|
346
|
+
host = info.baseURL ? new URL(info.baseURL).host : undefined;
|
|
347
|
+
} catch {}
|
|
348
|
+
const api = { anthropic: "the Anthropic API", openai: "the OpenAI Responses API", "openai-compat": "an OpenAI-compatible API" }[info.kind];
|
|
349
|
+
return { name: info.model, via: host ? `${api} at ${host}` : api };
|
|
350
|
+
}
|
|
300
351
|
/** 每个会话用一套新的插件实例:插件里有按会话记的状态(例如压缩、重复调用的计数) */
|
|
301
352
|
const makeJade = (mode?: Mode) =>
|
|
302
353
|
createJade({
|
|
@@ -305,6 +356,7 @@ const makeJade = (mode?: Mode) =>
|
|
|
305
356
|
store,
|
|
306
357
|
provider,
|
|
307
358
|
mode,
|
|
359
|
+
model: currentModel,
|
|
308
360
|
extensions: pluginSources,
|
|
309
361
|
disabled: disabledPlugins,
|
|
310
362
|
mcp: mcpClients,
|
|
@@ -312,6 +364,9 @@ const makeJade = (mode?: Mode) =>
|
|
|
312
364
|
compactTokens: compactAt,
|
|
313
365
|
approveSubagent,
|
|
314
366
|
sharedRules: (): AllowRule[] => [...settings().rules, ...pendingRules],
|
|
367
|
+
pendingMode: () => pendingMode,
|
|
368
|
+
// auto 模式的审查用当前的模型;rt 换会话时跟着换,所以每次取
|
|
369
|
+
review: reviewer({ provider, cwd, messages: () => rt.state.messages }),
|
|
315
370
|
approveCheck: process.env.JADE_CHECKS === "off" ? undefined : approveCheck,
|
|
316
371
|
checkConfirmed: (c) => trust.has(checkHash(c)),
|
|
317
372
|
checksUnavailable: process.env.JADE_CHECKS === "off" ? "Edit-time checks are turned off by JADE_CHECKS=off." : undefined,
|
|
@@ -319,6 +374,7 @@ const makeJade = (mode?: Mode) =>
|
|
|
319
374
|
|
|
320
375
|
let jade: Jade = await makeJade(opts.mode);
|
|
321
376
|
for (const w of jade.warnings) screen.log(yellow(`[warning] ${w}`));
|
|
377
|
+
if (badSavedMode) screen.log(yellow(`[warning] JADE_MODE=${badSavedMode} is not a mode (${MODES.join(", ")}); starting in ${opts.mode ?? "ask"}`));
|
|
322
378
|
for (const c of shadowed) screen.log(yellow(`[warning] custom command ${display(cwd, c.file)} is ignored: /${c.name} is a built-in command`));
|
|
323
379
|
// 插件的命令加进菜单(名字在各个会话的实例之间不变)。内置命令和 .jade/commands 里的同名命令优先
|
|
324
380
|
// skill 也可以当命令用:/<skill 名> [要做的事]。内置命令和自定义命令优先
|
|
@@ -465,12 +521,47 @@ let rt = resumeId
|
|
|
465
521
|
let sessionId = rt.state.sessionId;
|
|
466
522
|
|
|
467
523
|
// 权限设置记在日志里(derived),关卡从那里读,恢复会话后仍然有效
|
|
468
|
-
|
|
524
|
+
// 主会话不会是 readonly(只给子 Agent 用;旧会话里记的 readonly 由关卡换成 plan)
|
|
525
|
+
const settings = () => jade.policy.settings(rt.state.derived) as PolicySettings & { mode: Mode };
|
|
469
526
|
const saveSettings = async (next: PolicySettings) => {
|
|
470
527
|
await rt.record(POLICY_KEY, next);
|
|
528
|
+
pendingMode = undefined;
|
|
471
529
|
screen.status({ mode: next.mode });
|
|
472
530
|
};
|
|
473
531
|
|
|
532
|
+
/**
|
|
533
|
+
* 用户切换的模式记进 $JADE_HOME/.env 的 JADE_MODE,之后新开的会话默认用它(恢复的会话仍用它自己记下的模式)。
|
|
534
|
+
* yolo 不记:免得以后一打开就什么都不问;要默认 yolo 可以手动写 JADE_MODE=yolo 或用 -m yolo
|
|
535
|
+
*/
|
|
536
|
+
async function rememberMode(mode: Mode) {
|
|
537
|
+
if (mode === "yolo" || process.env.JADE_MODE === mode) return;
|
|
538
|
+
await updateEnvFile(envFile, { JADE_MODE: mode }).catch((err) =>
|
|
539
|
+
screen.log(yellow(`[warning] could not remember the mode in ${envFile}: ${errorText(err)}`)),
|
|
540
|
+
);
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
/** 换权限模式;运行时正在执行这一轮时先记在 pendingMode,关卡立即按它判断,这一轮结束后再写进日志 */
|
|
544
|
+
async function setMode(mode: Mode) {
|
|
545
|
+
await rememberMode(mode);
|
|
546
|
+
try {
|
|
547
|
+
await saveSettings({ ...settings(), mode });
|
|
548
|
+
} catch (err) {
|
|
549
|
+
if (!(err instanceof RuntimeBusy)) throw err;
|
|
550
|
+
pendingMode = mode;
|
|
551
|
+
screen.status({ mode });
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
/** 把运行中切换的模式写进日志 */
|
|
556
|
+
async function flushPendingMode() {
|
|
557
|
+
if (pendingMode === undefined) return;
|
|
558
|
+
try {
|
|
559
|
+
await saveSettings(settings());
|
|
560
|
+
} catch (err) {
|
|
561
|
+
if (!(err instanceof RuntimeBusy)) screen.log(red(`[error] ${errorText(err)}`));
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
|
|
474
565
|
// ---------- 渲染 ----------
|
|
475
566
|
|
|
476
567
|
const view = transcript({ screen, cwd, store, contextWindow: () => contextWindow, compactAt: () => compactAt(), canRetry: () => canRetry() });
|
|
@@ -480,7 +571,7 @@ const percent = (part: number, whole: number) => (whole > 0 ? `${Math.round((par
|
|
|
480
571
|
|
|
481
572
|
// ---------- 审批 ----------
|
|
482
573
|
|
|
483
|
-
type Call = { toolUseId: string; name: string; input: unknown };
|
|
574
|
+
type Call = { toolUseId: string; name: string; input: unknown; reason?: string };
|
|
484
575
|
|
|
485
576
|
/** 问用户是否允许一个调用;选了"总是允许"时交给 remember 记下规则 */
|
|
486
577
|
async function ask(c: Call, who: string, remember: (rule: AllowRule) => Promise<void> | void): Promise<SubagentApproval> {
|
|
@@ -498,7 +589,8 @@ async function ask(c: Call, who: string, remember: (rule: AllowRule) => Promise<
|
|
|
498
589
|
{ label: `Yes, and always allow ${always} in this session`, value: "always" as const },
|
|
499
590
|
{ label: "No, and tell jade what to do instead", value: "no" as const },
|
|
500
591
|
],
|
|
501
|
-
|
|
592
|
+
// 关卡给了理由的(例如 auto 模式下审查转给用户的),说明为什么要问
|
|
593
|
+
{ body: (c.reason ? `${yellow(c.reason)}\n\n` : "") + (await preview(c, cwd)) },
|
|
502
594
|
);
|
|
503
595
|
if (answer === "always") await remember(rule);
|
|
504
596
|
const approved = answer === "yes" || answer === "always";
|
|
@@ -506,7 +598,10 @@ async function ask(c: Call, who: string, remember: (rule: AllowRule) => Promise<
|
|
|
506
598
|
? undefined
|
|
507
599
|
: (answer === "no" ? await screen.input("What should jade do instead? (optional)") : undefined) || "The user rejected this action.";
|
|
508
600
|
// 主 Agent 的调用的结果之后接在这一行后面
|
|
509
|
-
screen.log(approved ? green(`✓ ${who}${head}`) : red(`✗ ${who}${head}`) + dim(reason ? ` — ${clip(reason, 200)}` : ""), {
|
|
601
|
+
screen.log(approved ? green(`✓ ${who}${head}`) : red(`✗ ${who}${head}`) + dim(reason ? ` — ${clip(reason, 200)}` : ""), {
|
|
602
|
+
id: c.toolUseId,
|
|
603
|
+
details: callDetails(c.name, c.input),
|
|
604
|
+
});
|
|
510
605
|
return { approved, reason };
|
|
511
606
|
}
|
|
512
607
|
|
|
@@ -697,24 +792,51 @@ async function occupy(fn: () => Promise<void>) {
|
|
|
697
792
|
} finally {
|
|
698
793
|
running = false;
|
|
699
794
|
work = undefined;
|
|
795
|
+
interruptPending = false;
|
|
700
796
|
screen.busy(undefined);
|
|
797
|
+
await flushPendingMode();
|
|
701
798
|
}
|
|
702
799
|
}
|
|
703
800
|
|
|
801
|
+
/**
|
|
802
|
+
* 上一次打断的时间;正在收尾的这一轮结束、开始下一条排队的输入之前一直算"刚打断过"(interruptPending)。
|
|
803
|
+
* 刚打断过时再按一次 esc,连排队的消息一起清掉
|
|
804
|
+
*/
|
|
805
|
+
let interruptedAt = 0;
|
|
806
|
+
let interruptPending = false;
|
|
807
|
+
/** 第二次 esc 算作"再按一次"的时间窗口(从打断的那一轮结束、开始下一条排队的输入算起) */
|
|
808
|
+
const SECOND_ESC_MS = 3000;
|
|
809
|
+
|
|
704
810
|
function interrupt() {
|
|
705
811
|
if (!running) return;
|
|
812
|
+
const again = interruptPending || Date.now() - interruptedAt < SECOND_ESC_MS;
|
|
813
|
+
interruptedAt = Date.now();
|
|
814
|
+
interruptPending = true;
|
|
706
815
|
commandAbort?.abort(new Error("interrupted by user"));
|
|
707
816
|
rt.abort("interrupted by user");
|
|
708
817
|
screen.busy("Interrupting");
|
|
818
|
+
if (queue.length === 0) return;
|
|
819
|
+
if (again) {
|
|
820
|
+
const n = queue.splice(0).length;
|
|
821
|
+
screen.queued(queue);
|
|
822
|
+
screen.log(dim(`[cleared ${n} queued message${n > 1 ? "s" : ""}]`));
|
|
823
|
+
} else {
|
|
824
|
+
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"}]`));
|
|
825
|
+
}
|
|
709
826
|
}
|
|
710
827
|
|
|
711
|
-
/**
|
|
712
|
-
|
|
828
|
+
/**
|
|
829
|
+
* 离开会话但保留它:写入 session.detached,插件的收尾写入完成后再退出(4.2)。
|
|
830
|
+
* 没收到过输入的会话没什么可恢复的,删掉它的日志;返回会话是否保留了下来
|
|
831
|
+
*/
|
|
832
|
+
async function leave(): Promise<boolean> {
|
|
713
833
|
jade.shutdown();
|
|
714
834
|
try {
|
|
715
835
|
await rt.detach();
|
|
836
|
+
return !(await discardIfEmpty(store, sessionId));
|
|
716
837
|
} catch (err) {
|
|
717
838
|
screen.log(red(`[error] ${errorText(err)}`));
|
|
839
|
+
return true;
|
|
718
840
|
}
|
|
719
841
|
}
|
|
720
842
|
|
|
@@ -743,6 +865,7 @@ async function quit(code = 0) {
|
|
|
743
865
|
* 恢复失败时留在当前会话
|
|
744
866
|
*/
|
|
745
867
|
async function switchSession(id?: string) {
|
|
868
|
+
await flushPendingMode();
|
|
746
869
|
const mode = settings().mode;
|
|
747
870
|
const next = await makeJade(mode);
|
|
748
871
|
let nextRt: Runtime;
|
|
@@ -754,7 +877,7 @@ async function switchSession(id?: string) {
|
|
|
754
877
|
next.shutdown();
|
|
755
878
|
return screen.log(red(`[error] could not resume ${id}: ${errorText(err)}`));
|
|
756
879
|
}
|
|
757
|
-
await leave();
|
|
880
|
+
const kept = await leave();
|
|
758
881
|
const old = sessionId;
|
|
759
882
|
jade = next;
|
|
760
883
|
rt = nextRt;
|
|
@@ -765,8 +888,8 @@ async function switchSession(id?: string) {
|
|
|
765
888
|
announcedMode = undefined;
|
|
766
889
|
pendingRules.length = 0;
|
|
767
890
|
screen.clear();
|
|
768
|
-
screen.status({ session: sessionId, mode: settings().mode, context: undefined, cache: undefined, spent: undefined });
|
|
769
|
-
if (!id) return screen.log(dim(`Started a new session. The previous one can be resumed with /resume or: jade --resume ${old}`));
|
|
891
|
+
screen.status({ session: sessionId, mode: settings().mode, context: undefined, cache: undefined, spent: undefined, speed: undefined });
|
|
892
|
+
if (!id) 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."));
|
|
770
893
|
if (!(await resumeSession())) {
|
|
771
894
|
screen.log(dim("Starting a new session instead."));
|
|
772
895
|
await switchSession();
|
|
@@ -841,13 +964,11 @@ function announceMode() {
|
|
|
841
964
|
announcedMode = mode;
|
|
842
965
|
}
|
|
843
966
|
|
|
844
|
-
/** Shift+Tab:ask → auto
|
|
845
|
-
const CYCLE: Mode[] = ["ask", "auto
|
|
967
|
+
/** Shift+Tab:ask → auto → plan → ask;yolo 也回到 ask。运行中也能切换,从下一个工具调用起生效 */
|
|
968
|
+
const CYCLE: Mode[] = ["ask", "auto", "plan"];
|
|
846
969
|
function cycleMode() {
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
const next = CYCLE[(i + 1) % CYCLE.length]!;
|
|
850
|
-
saveSettings({ ...settings(), mode: next }).catch((err) => screen.log(red(`[error] ${errorText(err)}`)));
|
|
970
|
+
const i = CYCLE.indexOf(settings().mode as Mode);
|
|
971
|
+
setMode(CYCLE[(i + 1) % CYCLE.length]!).catch((err) => screen.log(red(`[error] ${errorText(err)}`)));
|
|
851
972
|
}
|
|
852
973
|
|
|
853
974
|
/**
|
|
@@ -855,15 +976,17 @@ function cycleMode() {
|
|
|
855
976
|
* 用户已经排了别的输入、或者不能交互时不问
|
|
856
977
|
*/
|
|
857
978
|
async function offerPlan() {
|
|
858
|
-
|
|
979
|
+
// 这一轮中途才切到计划模式的,模型并不知道要给计划,不问
|
|
980
|
+
if (settings().mode !== "plan" || announcedMode !== "plan" || view.lastEnd !== "end" || !screen.interactive || queue.length > 0) return;
|
|
859
981
|
const last = rt.state.messages.at(-1);
|
|
860
982
|
if (last?.role !== "assistant" || !last.content.some((b) => b.kind === "text" && b.text.trim())) return;
|
|
861
983
|
const choice = await screen.select("Execute this plan?", [
|
|
862
|
-
{ label: "Yes,
|
|
984
|
+
{ label: "Yes, in auto mode", value: "auto" as const, hint: "edits and safe commands run without asking" },
|
|
863
985
|
{ label: "Yes, and ask before each edit", value: "ask" as const, hint: "mode ask" },
|
|
864
986
|
{ label: "No, keep planning", value: "plan" as const, hint: "type what to change" },
|
|
865
987
|
]);
|
|
866
988
|
if (!choice || choice === "plan") return;
|
|
989
|
+
await rememberMode(choice);
|
|
867
990
|
await saveSettings({ ...settings(), mode: choice });
|
|
868
991
|
screen.log(dim(`mode: ${choice}`));
|
|
869
992
|
await say("The plan is approved. Implement it now.");
|
|
@@ -1015,8 +1138,8 @@ async function command(input: string): Promise<boolean> {
|
|
|
1015
1138
|
if (!m) break;
|
|
1016
1139
|
}
|
|
1017
1140
|
if (!(MODES as readonly string[]).includes(m)) screen.log(red(`unknown mode ${m}`));
|
|
1018
|
-
else await
|
|
1019
|
-
screen.log(`mode: ${settings().mode}`);
|
|
1141
|
+
else await setMode(m as Mode);
|
|
1142
|
+
screen.log(`mode: ${settings().mode}${pendingMode ? dim(" (from the next tool call)") : ""}`);
|
|
1020
1143
|
break;
|
|
1021
1144
|
}
|
|
1022
1145
|
case "undo":
|
|
@@ -1087,10 +1210,9 @@ async function command(input: string): Promise<boolean> {
|
|
|
1087
1210
|
|
|
1088
1211
|
const MODE_HINTS: Record<string, string> = {
|
|
1089
1212
|
ask: "ask before edits and commands",
|
|
1090
|
-
|
|
1091
|
-
yolo: "never ask",
|
|
1092
|
-
readonly: "read only, no edits or commands",
|
|
1213
|
+
auto: "edits in this directory run without asking; other commands are reviewed by the model, risky ones still ask",
|
|
1093
1214
|
plan: "research read-only and propose a plan; edits start after you approve it",
|
|
1215
|
+
yolo: "never ask",
|
|
1094
1216
|
};
|
|
1095
1217
|
|
|
1096
1218
|
// ---------- 输入 ----------
|
|
@@ -1127,6 +1249,9 @@ async function pump() {
|
|
|
1127
1249
|
let exit = false;
|
|
1128
1250
|
await occupy(async () => {
|
|
1129
1251
|
while (queue.length > 0 && !quitting) {
|
|
1252
|
+
// 打断的那一轮已经结束:从现在起再给 SECOND_ESC_MS 的时间按第二次 esc
|
|
1253
|
+
if (interruptPending) interruptedAt = Date.now();
|
|
1254
|
+
interruptPending = false;
|
|
1130
1255
|
const text = queue.shift()!;
|
|
1131
1256
|
screen.queued(queue);
|
|
1132
1257
|
echo(text);
|
package/src/mcp.ts
CHANGED
|
@@ -627,7 +627,7 @@ export function mcpTools(clients: McpClient[], opts: { readOnly?: boolean } = {}
|
|
|
627
627
|
name,
|
|
628
628
|
description: `${(info.description ?? info.title ?? info.annotations?.title ?? info.name).trim()}\n\n(Tool "${info.name}" from the MCP server "${client.name}".)`,
|
|
629
629
|
inputSchema: { type: "object", ...schema },
|
|
630
|
-
// 服务没有声明只读的,都按有外部副作用处理:ask
|
|
630
|
+
// 服务没有声明只读的,都按有外部副作用处理:ask 模式下先问,auto 模式下先交给审查
|
|
631
631
|
effect: readOnly ? "read" : "external",
|
|
632
632
|
concurrency: readOnly ? "safe" : "exclusive",
|
|
633
633
|
idempotent: readOnly || info.annotations?.idempotentHint === true,
|