@uncarved/jade 0.3.1 → 0.4.1
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 +1 -0
- package/dist/api.d.ts +1 -1
- package/dist/api.js +1 -1
- package/dist/host.d.ts +2 -0
- package/dist/host.js +39 -5
- package/dist/host.js.map +1 -1
- package/dist/remote.js +5 -0
- package/dist/remote.js.map +1 -1
- package/dist/session.d.ts +7 -0
- package/dist/session.js +490 -29
- package/dist/session.js.map +1 -1
- package/package.json +4 -4
- package/plugins/approval/index.ts +36 -14
- package/plugins/approval/policy.ts +15 -10
- package/plugins/approval/review.ts +9 -5
- package/plugins/checks/checks.ts +10 -7
- package/plugins/checks/index.ts +13 -13
- package/plugins/checks/plugin.json +1 -1
- package/plugins/checks/project.ts +37 -38
- package/plugins/commands/index.ts +27 -3
- package/plugins/compact/compact.ts +13 -5
- package/plugins/compact/index.ts +20 -3
- package/plugins/compact/style.ts +13 -14
- package/plugins/completions/index.ts +35 -0
- package/plugins/completions/plugin.json +7 -0
- package/plugins/copy/index.ts +46 -0
- package/plugins/copy/plugin.json +7 -0
- package/plugins/editor/index.ts +10 -4
- package/plugins/fs/diff.ts +7 -6
- package/plugins/fs/fs.ts +8 -4
- package/plugins/fs/grep.ts +42 -9
- package/plugins/fs/index.ts +2 -7
- package/plugins/fs/plugin.json +1 -1
- package/plugins/fs/renderers.ts +16 -15
- package/plugins/fs/style.ts +16 -15
- package/plugins/hooks/index.ts +3 -2
- package/plugins/keys/index.ts +5 -1
- package/plugins/mcp/index.ts +197 -78
- package/plugins/mcp/install.ts +5 -3
- package/plugins/mcp/mcp.ts +163 -11
- package/plugins/mcp/plugin.json +2 -1
- package/plugins/mcp/project.ts +47 -50
- package/plugins/memory/index.ts +4 -4
- package/plugins/memory/style.ts +13 -14
- package/plugins/mentions/candidates.ts +67 -0
- package/plugins/mentions/index.ts +12 -5
- package/plugins/mentions/plugin.json +1 -1
- package/plugins/mentions/style.ts +13 -14
- package/plugins/models/configure.ts +47 -12
- package/plugins/models/index.ts +27 -1
- package/plugins/models/provider.ts +24 -0
- package/plugins/models/saved.ts +1 -1
- package/plugins/modes/index.ts +13 -2
- package/plugins/print/index.ts +7 -4
- package/plugins/prompts/index.ts +0 -0
- package/plugins/renderers/index.ts +10 -5
- package/plugins/repeats/index.ts +8 -8
- package/plugins/schedule/index.ts +5 -1
- package/plugins/screenshot/browser.ts +10 -0
- package/plugins/sessions/index.ts +48 -2
- package/plugins/shell/index.ts +27 -28
- package/plugins/shell/plugin.json +1 -1
- package/plugins/shell/renderers.ts +9 -7
- package/plugins/shell/shell.ts +3 -1
- package/plugins/skills/index.ts +28 -19
- package/plugins/skills/project.ts +12 -7
- package/plugins/steer/index.ts +5 -4
- package/plugins/task/index.ts +80 -50
- package/plugins/task/plugin.json +6 -2
- package/plugins/task/style.ts +13 -14
- package/plugins/task/subagent.ts +67 -184
- package/plugins/todo/index.ts +39 -9
- package/plugins/tools/index.ts +9 -2
- package/plugins/tui/frontend.ts +139 -89
- package/plugins/tui/index.ts +2 -3
- package/plugins/tui/mentions.ts +11 -80
- package/plugins/tui/plugin.json +1 -1
- package/plugins/tui/screen.ts +55 -5
- package/plugins/tui/transcript.ts +17 -7
- package/plugins/tui/tui.ts +337 -101
- package/plugins/tui/vendor.ts +1 -1
- package/plugins/undo/checkpoint.ts +8 -4
- package/plugins/undo/index.ts +170 -31
- package/plugins/undo/plugin.json +1 -0
- package/plugins/undo/snapshot.ts +87 -14
- package/plugins/undo/style.ts +13 -14
- package/plugins/web/index.ts +7 -18
- package/plugins/web/plugin.json +1 -1
- package/plugins-dist/approval/index.js +363 -418
- package/plugins-dist/approval/policy.js +391 -508
- package/plugins-dist/approval/review.js +132 -158
- package/plugins-dist/approval/schema.js +50 -55
- package/plugins-dist/ask/index.js +115 -129
- package/plugins-dist/budget/index.js +27 -31
- package/plugins-dist/checks/checks.js +293 -329
- package/plugins-dist/checks/index.js +119 -139
- package/plugins-dist/checks/plugin.json +2 -1
- package/plugins-dist/checks/project.js +57 -65
- package/plugins-dist/commands/index.js +47 -29
- package/plugins-dist/compact/compact.js +233 -249
- package/plugins-dist/compact/index.js +98 -95
- package/plugins-dist/compact/style.js +9 -14
- package/plugins-dist/completions/index.js +30 -0
- package/plugins-dist/completions/plugin.json +9 -0
- package/plugins-dist/continue/index.js +40 -52
- package/plugins-dist/copy/index.js +40 -0
- package/plugins-dist/copy/plugin.json +9 -0
- package/plugins-dist/cost/index.js +98 -115
- package/plugins-dist/custom-commands/index.js +55 -72
- package/plugins-dist/editor/index.js +64 -72
- package/plugins-dist/fs/diff.js +111 -124
- package/plugins-dist/fs/fs.js +426 -469
- package/plugins-dist/fs/grep.js +262 -253
- package/plugins-dist/fs/index.js +20 -34
- package/plugins-dist/fs/plugin.json +1 -2
- package/plugins-dist/fs/renderers.js +100 -112
- package/plugins-dist/fs/style.js +11 -15
- package/plugins-dist/fs/sys.js +31 -43
- package/plugins-dist/hooks/index.js +107 -127
- package/plugins-dist/keys/index.js +27 -31
- package/plugins-dist/mcp/index.js +271 -199
- package/plugins-dist/mcp/install.js +123 -137
- package/plugins-dist/mcp/mcp.js +638 -577
- package/plugins-dist/mcp/plugin.json +3 -1
- package/plugins-dist/mcp/project.js +98 -115
- package/plugins-dist/memory/index.js +83 -88
- package/plugins-dist/memory/memory.js +111 -128
- package/plugins-dist/memory/style.js +9 -14
- package/plugins-dist/mentions/attach.js +91 -99
- package/plugins-dist/mentions/candidates.js +49 -0
- package/plugins-dist/mentions/index.js +71 -81
- package/plugins-dist/mentions/plugin.json +2 -1
- package/plugins-dist/mentions/style.js +9 -14
- package/plugins-dist/models/config.js +29 -43
- package/plugins-dist/models/configure.js +421 -474
- package/plugins-dist/models/context.js +14 -22
- package/plugins-dist/models/index.js +182 -189
- package/plugins-dist/models/provider.js +263 -307
- package/plugins-dist/models/saved.js +60 -72
- package/plugins-dist/modes/index.js +138 -165
- package/plugins-dist/plugins/index.js +34 -34
- package/plugins-dist/print/index.js +192 -175
- package/plugins-dist/prompts/index.js +86 -89
- package/plugins-dist/prompts/notes.js +31 -37
- package/plugins-dist/prompts/prompts.js +155 -193
- package/plugins-dist/prompts/results.js +40 -53
- package/plugins-dist/prompts/sys.js +39 -50
- package/plugins-dist/renderers/index.js +32 -32
- package/plugins-dist/repeats/index.js +131 -174
- package/plugins-dist/schedule/cron.js +49 -56
- package/plugins-dist/schedule/index.js +230 -247
- package/plugins-dist/schedule/store.js +93 -109
- package/plugins-dist/scratch/index.js +12 -18
- package/plugins-dist/screenshot/browser.js +136 -154
- package/plugins-dist/screenshot/index.js +37 -46
- package/plugins-dist/screenshot/sys.js +53 -65
- package/plugins-dist/screenshot/tool.js +98 -126
- package/plugins-dist/sessions/index.js +149 -117
- package/plugins-dist/shell/index.js +117 -135
- package/plugins-dist/shell/plugin.json +1 -1
- package/plugins-dist/shell/renderers.js +28 -33
- package/plugins-dist/shell/service.js +17 -23
- package/plugins-dist/shell/shell.js +307 -362
- package/plugins-dist/shell/sys.js +64 -79
- package/plugins-dist/skills/index.js +193 -211
- package/plugins-dist/skills/install.js +122 -149
- package/plugins-dist/skills/project.js +65 -73
- package/plugins-dist/skills/skills.js +135 -171
- package/plugins-dist/steer/index.js +108 -125
- package/plugins-dist/suggest/index.js +235 -259
- package/plugins-dist/task/index.js +130 -117
- package/plugins-dist/task/plugin.json +5 -2
- package/plugins-dist/task/style.js +9 -14
- package/plugins-dist/task/subagent.js +156 -279
- package/plugins-dist/todo/index.js +79 -63
- package/plugins-dist/tools/index.js +36 -42
- package/plugins-dist/tui/editor.js +145 -160
- package/plugins-dist/tui/frontend.js +639 -678
- package/plugins-dist/tui/history.js +34 -37
- package/plugins-dist/tui/index.js +16 -18
- package/plugins-dist/tui/markdown.js +203 -247
- package/plugins-dist/tui/mascot.js +266 -355
- package/plugins-dist/tui/mentions.js +67 -144
- package/plugins-dist/tui/plugin.json +1 -2
- package/plugins-dist/tui/render.js +35 -45
- package/plugins-dist/tui/screen.js +197 -180
- package/plugins-dist/tui/transcript.js +436 -468
- package/plugins-dist/tui/tui.js +1554 -1656
- package/plugins-dist/tui/vendor.js +1088 -1388
- package/plugins-dist/undo/checkpoint.js +97 -118
- package/plugins-dist/undo/index.js +281 -192
- package/plugins-dist/undo/plugin.json +3 -0
- package/plugins-dist/undo/snapshot.js +134 -99
- package/plugins-dist/undo/style.js +9 -14
- package/plugins-dist/update/index.js +26 -28
- package/plugins-dist/web/env.js +55 -68
- package/plugins-dist/web/index.js +80 -110
- package/plugins-dist/web/plugin.json +1 -2
- package/plugins-dist/web/renderers.js +4 -5
- package/plugins-dist/web/web.js +514 -593
- package/sdk/python/__pycache__/jade_plugin.cpython-312.pyc +0 -0
- package/sdk/python/jade_plugin.py +9 -2
- package/src/api.ts +1 -1
- package/src/host.ts +39 -4
- package/src/remote.ts +6 -1
- package/src/session.ts +496 -36
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
import { readFile, rm, writeFile } from "node:fs/promises";
|
|
8
8
|
import { hasProvider, parseTokens } from "./config.ts";
|
|
9
9
|
import { contextWindow } from "./context.ts";
|
|
10
|
-
import { EFFORTS, isBuiltinKind, jsonObject, KEY_KEY, listModels, MODEL_KEY, providerInfo, providerKind, rpmFromEnv, URL_KEY, visionFromEnv, type ProviderKind } from "./provider.ts";
|
|
10
|
+
import { EFFORTS, fallbacksFromEnv, isBuiltinKind, isOfficialAnthropic, jsonObject, KEY_KEY, listModels, MODEL_KEY, providerInfo, providerKind, rpmFromEnv, URL_KEY, visionFromEnv, type ProviderKind } from "./provider.ts";
|
|
11
11
|
import { MODEL_SETTINGS, readSavedServices, updateEnvFile, writeSavedServices, type Env, type SavedModel, type SavedService } from "./saved.ts";
|
|
12
12
|
import type { UI } from "@uncarved/jade-api";
|
|
13
13
|
|
|
@@ -55,6 +55,21 @@ function visionHint(env: Env, kind: string): string {
|
|
|
55
55
|
return v === undefined ? `${isBuiltinKind(kind) && kind !== "openai-compat" ? "yes" : "no"} (default)` : v ? "yes" : "no";
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
+
/** JADE_FALLBACKS 设成了什么;没设或写错时 undefined */
|
|
59
|
+
function fallbacksSetting(env: Env): boolean | undefined {
|
|
60
|
+
if (!env.JADE_FALLBACKS?.trim()) return undefined;
|
|
61
|
+
try {
|
|
62
|
+
return fallbacksFromEnv({ JADE_FALLBACKS: env.JADE_FALLBACKS });
|
|
63
|
+
} catch {
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function fallbacksHint(env: Env, baseURL: string | undefined): string {
|
|
69
|
+
const v = fallbacksSetting(env);
|
|
70
|
+
return v === undefined ? `${isOfficialAnthropic(baseURL) ? "on" : "off"} (default)` : v ? "on" : "off";
|
|
71
|
+
}
|
|
72
|
+
|
|
58
73
|
/** 一个服务包含的变量(不含模型名) */
|
|
59
74
|
const serviceKeys = (kind: ProviderKind) => ["JADE_PROVIDER", URL_KEY[kind], KEY_KEY[kind]];
|
|
60
75
|
|
|
@@ -252,16 +267,17 @@ export function configurator(opts: ConfigureOptions): Configure {
|
|
|
252
267
|
const same = kind === current?.kind;
|
|
253
268
|
|
|
254
269
|
let baseURL: string | undefined = env[URL_KEY[kind]];
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
270
|
+
// Anthropic 也能改地址(代理、兼容服务):留空用官方地址。不预填官方地址:预填了回车就会把它写进
|
|
271
|
+
// ANTHROPIC_BASE_URL,和以前没写地址时保存的同一个服务对不上(找不到保存过的密钥,models.json 里多出一份)
|
|
272
|
+
const fallback = kind === "openai" ? "https://api.openai.com/v1" : "";
|
|
273
|
+
const placeholder = kind === "anthropic" ? "empty for https://api.anthropic.com (SDK adds /v1)" : "https://example.com/v1";
|
|
274
|
+
const url = await ui.input({ title: "Base URL", initial: baseURL ?? fallback, placeholder }, opts.signal);
|
|
275
|
+
if (url === undefined) return false;
|
|
276
|
+
if (kind === "openai-compat" && !url) {
|
|
277
|
+
report("A base URL is required.");
|
|
278
|
+
return false;
|
|
264
279
|
}
|
|
280
|
+
baseURL = url || undefined;
|
|
265
281
|
|
|
266
282
|
// 已知的密钥:当前服务的,或者保存过的同一个地址的服务的(最近用过的那个)
|
|
267
283
|
const sameAddress = (e: Env) => {
|
|
@@ -312,16 +328,18 @@ export function configurator(opts: ConfigureOptions): Configure {
|
|
|
312
328
|
title: string;
|
|
313
329
|
kind: string;
|
|
314
330
|
model: string;
|
|
331
|
+
/** 服务地址:Claude 的服务端回退默认开不开看它 */
|
|
332
|
+
baseURL: string | undefined;
|
|
315
333
|
read(): Promise<Env>;
|
|
316
334
|
update(settings: Env): Promise<boolean>;
|
|
317
335
|
}
|
|
318
336
|
|
|
319
337
|
/**
|
|
320
|
-
* 某个模型的设置(输出上限、思考力度、上下文窗口、限速、能不能看图,openai-compat 还有额外请求参数)。
|
|
338
|
+
* 某个模型的设置(输出上限、思考力度、上下文窗口、限速、能不能看图,Claude 还有服务端回退,openai-compat 还有额外请求参数)。
|
|
321
339
|
* 改完一项回到这个菜单接着改(重新读一遍,显示的是改过的值);Back 或 Esc 才离开
|
|
322
340
|
*/
|
|
323
341
|
async function configureSettings(target: SettingsTarget) {
|
|
324
|
-
const { title, kind, model } = target;
|
|
342
|
+
const { title, kind, model, baseURL } = target;
|
|
325
343
|
const efforts = isBuiltinKind(kind) ? EFFORTS[kind] : undefined;
|
|
326
344
|
while (true) {
|
|
327
345
|
const env = await target.read();
|
|
@@ -334,6 +352,7 @@ export function configurator(opts: ConfigureOptions): Configure {
|
|
|
334
352
|
{ label: "Context window", value: "window", hint: `${contextWindow(model, env)} tokens${env.JADE_CONTEXT_WINDOW ? "" : " (default)"}` },
|
|
335
353
|
{ label: "Requests per minute", value: "rpm", hint: env.JADE_RPM ? `${env.JADE_RPM}/min` : "no limit (slows down after being rate limited)" },
|
|
336
354
|
{ label: "Can see images", value: "vision", hint: visionHint(env, kind) },
|
|
355
|
+
...(kind === "anthropic" ? [{ label: "Server-side fallback", value: "fallbacks", hint: fallbacksHint(env, baseURL) }] : []),
|
|
337
356
|
...(kind === "openai-compat" ? [{ label: "Extra request body", value: "extra", hint: env.OPENAI_COMPAT_EXTRA_BODY ? shorten(env.OPENAI_COMPAT_EXTRA_BODY) : "none" }] : []),
|
|
338
357
|
{ label: "Back", value: "back" },
|
|
339
358
|
],
|
|
@@ -384,6 +403,21 @@ export function configurator(opts: ConfigureOptions): Configure {
|
|
|
384
403
|
const on = choice === DEFAULT ? isBuiltinKind(kind) && kind !== "openai-compat" : choice === "on";
|
|
385
404
|
report(on ? "Images will be sent to the model." : "Images will be replaced with a note.");
|
|
386
405
|
}
|
|
406
|
+
} else if (action === "fallbacks" && kind === "anthropic") {
|
|
407
|
+
const DEFAULT = "\0default";
|
|
408
|
+
const current = fallbacksSetting(env);
|
|
409
|
+
const choice = await ui.select(
|
|
410
|
+
{
|
|
411
|
+
title: "Retry with another model on the service side when Claude refuses for safety reasons?",
|
|
412
|
+
options: [
|
|
413
|
+
{ label: "On", value: "on", hint: current === true ? "current" : "official API only; most relays reject it" },
|
|
414
|
+
{ label: "Off", value: "off", hint: current === false ? "current" : "the field is not sent" },
|
|
415
|
+
{ label: "Default", value: DEFAULT, hint: isOfficialAnthropic(baseURL) ? "on (official API)" : "off (not the official API)" },
|
|
416
|
+
],
|
|
417
|
+
},
|
|
418
|
+
opts.signal,
|
|
419
|
+
);
|
|
420
|
+
if (choice !== undefined) await target.update({ JADE_FALLBACKS: choice === DEFAULT ? undefined : choice });
|
|
387
421
|
} else if (action === "window") {
|
|
388
422
|
const n = await ui.input({ title: "Context window in tokens, e.g. 128000, 512K, 1M (empty to infer it from the model name, or 256K if unknown)", initial: env.JADE_CONTEXT_WINDOW ?? "" }, opts.signal);
|
|
389
423
|
if (n !== undefined && tokensInput("Context window", n)) await target.update({ JADE_CONTEXT_WINDOW: n || undefined });
|
|
@@ -468,6 +502,7 @@ export function configurator(opts: ConfigureOptions): Configure {
|
|
|
468
502
|
title: o.model.name,
|
|
469
503
|
kind,
|
|
470
504
|
model: o.model.name,
|
|
505
|
+
baseURL: providerInfo(o.service.env).baseURL,
|
|
471
506
|
read,
|
|
472
507
|
update: (updates) => (isCurrent ? applyConfig(updates) : writeSavedModel(o.service, o.model, updates)),
|
|
473
508
|
});
|
package/plugins/models/index.ts
CHANGED
|
@@ -14,12 +14,19 @@ import { configurator } from "./configure.ts";
|
|
|
14
14
|
import { isBuiltinKind, listModels, MODEL_KEY, providerFromEnv, providerInfo, providerKind, RateGate, rpmFromEnv, visionFromEnv, withRetry, type RetriedResponse } from "./provider.ts";
|
|
15
15
|
import { MODEL_SETTINGS, readSavedServices, type Env } from "./saved.ts";
|
|
16
16
|
|
|
17
|
+
/** 进程级的配置交给会话里的提示用:有没有配好模型、有没有 /model、.env 在哪 */
|
|
18
|
+
let setup: { configured(): boolean; command: boolean; envFile: string } | undefined;
|
|
19
|
+
/** 没配模型的提示一个进程只说一次 */
|
|
20
|
+
let hinted = false;
|
|
21
|
+
|
|
17
22
|
export default definePlugin({
|
|
18
23
|
async process(ctx) {
|
|
19
24
|
const envFile = path.join(ctx.home, ".env");
|
|
20
25
|
const modelsFile = path.join(ctx.home, "models.json");
|
|
21
26
|
// 当前的模型配置。改配置时整份换掉(updateEnvFile 改的是这一份,apply 再从文件读一遍)
|
|
22
27
|
let env = await envWithFile(envFile);
|
|
28
|
+
hinted = false;
|
|
29
|
+
setup = { configured: () => hasProvider(env), command: false, envFile };
|
|
23
30
|
// 用的时候再取:jade/tui 也依赖这个插件(models@1),它在这之后才激活,这里取到的是 undefined,重试的提示就没人显示
|
|
24
31
|
const ui = () => ctx.service<UI>("ui@1");
|
|
25
32
|
const watchers = new Set<() => void>();
|
|
@@ -157,7 +164,9 @@ export default definePlugin({
|
|
|
157
164
|
notify();
|
|
158
165
|
};
|
|
159
166
|
|
|
160
|
-
ctx.service<Commands>("commands@1")
|
|
167
|
+
const commands = ctx.service<Commands>("commands@1");
|
|
168
|
+
setup.command = commands !== undefined;
|
|
169
|
+
commands?.register({
|
|
161
170
|
name: "model",
|
|
162
171
|
args: "[name]",
|
|
163
172
|
description: "switch models, or add one with its provider, API key and limits",
|
|
@@ -179,6 +188,23 @@ export default definePlugin({
|
|
|
179
188
|
},
|
|
180
189
|
|
|
181
190
|
session(ctx) {
|
|
191
|
+
const ui = ctx.service<UI>("ui@1");
|
|
192
|
+
// 还没配模型:告诉用户怎么配。/model 当场就能配好,不用重启;没有这条命令时只能改文件
|
|
193
|
+
if (setup && !setup.configured() && !hinted && ui?.interactive) {
|
|
194
|
+
hinted = true;
|
|
195
|
+
const file = `put ANTHROPIC_API_KEY=... (or OPENAI_API_KEY=...) in ${setup.envFile}, then restart jade`;
|
|
196
|
+
ui.notify(setup.command ? `No model is configured yet. Type /model to set one up, or ${file}.` : `No model is configured yet. To set one up, ${file}.`, "warning");
|
|
197
|
+
}
|
|
198
|
+
// 回复被输出上限截断:上限是这里的配置(JADE_MAX_TOKENS),只有这里知道怎么改
|
|
199
|
+
ctx.kernel.hook({
|
|
200
|
+
kind: "observe",
|
|
201
|
+
point: "event",
|
|
202
|
+
fn: (e) => {
|
|
203
|
+
if (e.type === "turn.ended" && (e.data as { reason?: string }).reason === "max_tokens" && setup?.command) {
|
|
204
|
+
ui?.notify("if replies keep getting cut off, raise the output limit with /model", "info");
|
|
205
|
+
}
|
|
206
|
+
},
|
|
207
|
+
});
|
|
182
208
|
// 重试过的请求:把每次失败写进会话日志,事后能查到为什么慢、为什么重新输出(插件事件,不进上下文)
|
|
183
209
|
ctx.kernel.hook({
|
|
184
210
|
kind: "observe",
|
|
@@ -258,6 +258,28 @@ export function visionFromEnv(env: Env = process.env): boolean {
|
|
|
258
258
|
throw new Error(`JADE_VISION must be on or off, got ${JSON.stringify(env.JADE_VISION)}`);
|
|
259
259
|
}
|
|
260
260
|
|
|
261
|
+
/** 地址是不是 Anthropic 官方的(没设地址时用的也是官方的) */
|
|
262
|
+
export function isOfficialAnthropic(baseURL: string | undefined): boolean {
|
|
263
|
+
if (!baseURL?.trim()) return true;
|
|
264
|
+
try {
|
|
265
|
+
return new URL(baseURL).host === "api.anthropic.com";
|
|
266
|
+
} catch {
|
|
267
|
+
return false;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* JADE_FALLBACKS:Claude 因安全策略拒绝时,由服务端换模型重试。不设时只对官方地址开启:
|
|
273
|
+
* 中转站多半不认这个字段,整个请求返回 400
|
|
274
|
+
*/
|
|
275
|
+
export function fallbacksFromEnv(env: Env = process.env): boolean {
|
|
276
|
+
const text = env.JADE_FALLBACKS?.trim().toLowerCase();
|
|
277
|
+
if (!text) return isOfficialAnthropic(env.ANTHROPIC_BASE_URL);
|
|
278
|
+
if (["1", "true", "on", "yes"].includes(text)) return true;
|
|
279
|
+
if (["0", "false", "off", "no"].includes(text)) return false;
|
|
280
|
+
throw new Error(`JADE_FALLBACKS must be on or off, got ${JSON.stringify(env.JADE_FALLBACKS)}`);
|
|
281
|
+
}
|
|
282
|
+
|
|
261
283
|
/** JADE_EFFORT;不设时 Claude 用 high,OpenAI 用服务端默认值 */
|
|
262
284
|
export function effortFromEnv(env: Env = process.env): string | undefined {
|
|
263
285
|
const effort = env.JADE_EFFORT;
|
|
@@ -304,6 +326,7 @@ export function providerInfo(env: Env = process.env): ProviderInfo {
|
|
|
304
326
|
* JADE_RPM 是每分钟最多发几个请求,见 rpmFromEnv(限速在 withRetry 的 gate 里做)
|
|
305
327
|
* JADE_IDLE_TIMEOUT 是等模型服务回数据的时限,见 idleTimeoutFromEnv
|
|
306
328
|
* JADE_VISION 是模型能不能看图片,见 visionFromEnv
|
|
329
|
+
* JADE_FALLBACKS 是 Claude 被安全策略拒绝时要不要服务端换模型重试,见 fallbacksFromEnv
|
|
307
330
|
* 只导入用到的那个服务的包:Anthropic 的 SDK 加载要一百多毫秒,拖慢启动
|
|
308
331
|
*/
|
|
309
332
|
export async function providerFromEnv(env: Env = process.env): Promise<Provider> {
|
|
@@ -352,6 +375,7 @@ export async function providerFromEnv(env: Env = process.env): Promise<Provider>
|
|
|
352
375
|
model: env.ANTHROPIC_MODEL,
|
|
353
376
|
effort: (effort ?? "high") as "low" | "medium" | "high" | "xhigh" | "max",
|
|
354
377
|
maxTokens,
|
|
378
|
+
fallbacks: fallbacksFromEnv(env),
|
|
355
379
|
});
|
|
356
380
|
}
|
|
357
381
|
|
package/plugins/models/saved.ts
CHANGED
|
@@ -8,7 +8,7 @@ import path from "node:path";
|
|
|
8
8
|
export type Env = Record<string, string | undefined>;
|
|
9
9
|
|
|
10
10
|
/** 每个模型自己的设置:切换模型时一起换掉 */
|
|
11
|
-
export const MODEL_SETTINGS = ["JADE_MAX_TOKENS", "JADE_EFFORT", "JADE_CONTEXT_WINDOW", "JADE_RPM", "JADE_VISION", "OPENAI_COMPAT_EXTRA_BODY"];
|
|
11
|
+
export const MODEL_SETTINGS = ["JADE_MAX_TOKENS", "JADE_EFFORT", "JADE_CONTEXT_WINDOW", "JADE_RPM", "JADE_VISION", "JADE_FALLBACKS", "OPENAI_COMPAT_EXTRA_BODY"];
|
|
12
12
|
|
|
13
13
|
export interface SavedModel {
|
|
14
14
|
name: string;
|
package/plugins/modes/index.ts
CHANGED
|
@@ -181,7 +181,8 @@ export default definePlugin({
|
|
|
181
181
|
}
|
|
182
182
|
|
|
183
183
|
// 能问用户时才有 propose_plan;它只在 plan 模式下发给模型
|
|
184
|
-
|
|
184
|
+
// 子会话(子 Agent)不提交计划:计划由用户的会话提交
|
|
185
|
+
const canPropose = !!ui?.interactive && ctx.session.parent === undefined;
|
|
185
186
|
if (canPropose) ctx.kernel.tool(proposePlanTool(approval, ui!));
|
|
186
187
|
ctx.kernel.hook({
|
|
187
188
|
kind: "transform",
|
|
@@ -199,8 +200,18 @@ export default definePlugin({
|
|
|
199
200
|
note = undefined;
|
|
200
201
|
}
|
|
201
202
|
};
|
|
203
|
+
// 状态栏上的当前模式:窄的时候也留着(去掉 shift+tab 提示),颜色说明它放得多宽
|
|
204
|
+
const show = () => {
|
|
205
|
+
const mode = approval.mode();
|
|
206
|
+
const tone = mode === "yolo" ? "error" : mode === "auto" ? "warn" : mode === "plan" ? "accent" : "ok";
|
|
207
|
+
ctx.status?.({ text: `${mode} (shift+tab)`, short: mode, tone, keep: true });
|
|
208
|
+
};
|
|
202
209
|
sync();
|
|
203
|
-
|
|
210
|
+
show();
|
|
211
|
+
const onChange = () => {
|
|
212
|
+
sync();
|
|
213
|
+
show();
|
|
214
|
+
};
|
|
204
215
|
listeners.add(onChange);
|
|
205
216
|
ctx.effect(() => () => void listeners.delete(onChange));
|
|
206
217
|
},
|
package/plugins/print/index.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// jade/print:jade -p "<prompt>" 跑一轮就退出。模型的回复写到 stdout,工具调用和提示写到 stderr。
|
|
2
2
|
// 不能向用户提问:ui@1 的 select 总是返回 undefined,要审批的调用由审批插件拒绝。
|
|
3
3
|
|
|
4
|
+
import { stripVTControlCharacters } from "node:util";
|
|
4
5
|
import type { Block, LogEvent, RuntimeNotice, Usage } from "uncarved";
|
|
5
6
|
import { definePlugin, type Frontend, type UI } from "@uncarved/jade-api";
|
|
6
7
|
import type { SessionIndex } from "@uncarved/jade-api/services";
|
|
@@ -18,10 +19,12 @@ Runs one prompt and exits. The reply goes to stdout; tool calls and notices go t
|
|
|
18
19
|
Exit code: 0 when the turn ends normally, 1 on an error, 130 when interrupted.
|
|
19
20
|
jade's own commands (plugins, update, --safe): jade help`;
|
|
20
21
|
|
|
21
|
-
|
|
22
|
+
/** -p 的 ui@1(导出给测试) */
|
|
23
|
+
export const ui: UI = {
|
|
22
24
|
interactive: false,
|
|
23
25
|
notify(message, level = "info") {
|
|
24
|
-
|
|
26
|
+
// 输出常常被重定向、交给别的程序:插件和工具带来的颜色、控制序列都去掉
|
|
27
|
+
process.stderr.write(`${level === "info" ? "" : `[${level}] `}${stripVTControlCharacters(String(message))}\n`);
|
|
25
28
|
},
|
|
26
29
|
select: async () => undefined,
|
|
27
30
|
input: async () => undefined,
|
|
@@ -96,11 +99,11 @@ const frontend: Frontend = {
|
|
|
96
99
|
}
|
|
97
100
|
if (e.type === "tool.decided") {
|
|
98
101
|
const d = e.data as { decision: string; reason?: string };
|
|
99
|
-
if (d.decision === "deny") process.stderr.write(` denied${d.reason ? `: ${d.reason}` : ""}\n`);
|
|
102
|
+
if (d.decision === "deny") process.stderr.write(` denied${d.reason ? `: ${stripVTControlCharacters(d.reason)}` : ""}\n`);
|
|
100
103
|
}
|
|
101
104
|
if (e.type === "tool.completed") {
|
|
102
105
|
const d = e.data as { isError: boolean; content: Block[] };
|
|
103
|
-
if (d.isError) process.stderr.write(` failed: ${firstLine(d.content)}\n`);
|
|
106
|
+
if (d.isError) process.stderr.write(` failed: ${stripVTControlCharacters(firstLine(d.content))}\n`);
|
|
104
107
|
}
|
|
105
108
|
if (e.type === "turn.ended") ended = e.data as { reason: string; error?: string };
|
|
106
109
|
});
|
package/plugins/prompts/index.ts
CHANGED
|
Binary file
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// jade/renderers:工具调用和插件事件在对话里怎么显示的注册表。前端查这里;没有登记的用前端的默认显示。
|
|
2
|
-
//
|
|
2
|
+
// 显示的文字是 jade-api 的 Text(带语义样式的片段,也可以是普通字符串),怎么画由前端决定;
|
|
3
|
+
// 前端负责过滤掉字符串里颜色之外的控制序列。
|
|
3
4
|
|
|
4
5
|
import { definePlugin } from "@uncarved/jade-api";
|
|
5
6
|
import type { EventRenderer, Renderers, ToolRenderer } from "@uncarved/jade-api/services";
|
|
@@ -7,9 +8,13 @@ import type { EventRenderer, Renderers, ToolRenderer } from "@uncarved/jade-api/
|
|
|
7
8
|
const tools = new Map<string, { renderer: ToolRenderer; plugin: string }>();
|
|
8
9
|
const events = new Map<string, { renderer: EventRenderer; plugin: string }>();
|
|
9
10
|
|
|
10
|
-
function add<T>(map: Map<string, { renderer: T; plugin: string }>, key: string, renderer: T, plugin: string, what: string, onDispose: (d: () => void) => void): () => void {
|
|
11
|
+
function add<T>(map: Map<string, { renderer: T; plugin: string }>, key: string, renderer: T, plugin: string, what: string, onDispose: (d: () => void) => void, report: (m: string) => void): () => void {
|
|
12
|
+
// 重名时先登记的留下,后来的跳过并报告(同 commands@1)
|
|
11
13
|
const taken = map.get(key);
|
|
12
|
-
if (taken)
|
|
14
|
+
if (taken) {
|
|
15
|
+
report(`the renderer for ${what} ${key} from ${plugin} was skipped: ${taken.plugin} already registered one`);
|
|
16
|
+
return () => {};
|
|
17
|
+
}
|
|
13
18
|
const entry = { renderer, plugin };
|
|
14
19
|
map.set(key, entry);
|
|
15
20
|
const revoke = () => {
|
|
@@ -24,11 +29,11 @@ export default definePlugin({
|
|
|
24
29
|
ctx.provide("renderers@1", (caller): Renderers => ({
|
|
25
30
|
tool(renderer) {
|
|
26
31
|
if (typeof renderer?.tool !== "string" || !renderer.tool) throw new Error("a tool renderer needs the tool name");
|
|
27
|
-
return add(tools, renderer.tool, renderer, caller.plugin, "tool", caller.onDispose);
|
|
32
|
+
return add(tools, renderer.tool, renderer, caller.plugin, "tool", caller.onDispose, ctx.report);
|
|
28
33
|
},
|
|
29
34
|
event(renderer) {
|
|
30
35
|
if (typeof renderer?.event !== "string" || !renderer.event || typeof renderer.render !== "function") throw new Error("an event renderer needs the event type and render");
|
|
31
|
-
return add(events, `${caller.plugin}/${renderer.event}`, renderer, caller.plugin, "event", caller.onDispose);
|
|
36
|
+
return add(events, `${caller.plugin}/${renderer.event}`, renderer, caller.plugin, "event", caller.onDispose, ctx.report);
|
|
32
37
|
},
|
|
33
38
|
forTool: (tool) => tools.get(tool),
|
|
34
39
|
forEvent: (type) => events.get(type),
|
package/plugins/repeats/index.ts
CHANGED
|
@@ -25,10 +25,10 @@ export const COMPACT_KEY = "jade/compact";
|
|
|
25
25
|
/** 只打开一个文件或网址的命令:start "" x、start x、xdg-open x、open x、explorer x */
|
|
26
26
|
const OPEN = /^\s*(?:start\s+""|start|xdg-open|open|explorer(?:\.exe)?)\s+(?:"([^"]+)"|'([^']+)'|([^\s"'&|;<>]+))\s*$/i;
|
|
27
27
|
|
|
28
|
-
/**
|
|
29
|
-
export function openTarget(call: { name: string; input: unknown }, cwd: string): string | undefined {
|
|
30
|
-
if (
|
|
31
|
-
const command = (call.input as
|
|
28
|
+
/** 执行命令的调用(shell 是命令所在的字段,见工具的 meta.shell)打开的目标(规范化后),不是打开命令时是 undefined */
|
|
29
|
+
export function openTarget(call: { name: string; input: unknown }, cwd: string, shell: string | undefined): string | undefined {
|
|
30
|
+
if (shell === undefined) return undefined;
|
|
31
|
+
const command = (call.input as Record<string, unknown> | null)?.[shell];
|
|
32
32
|
if (typeof command !== "string") return undefined;
|
|
33
33
|
const m = OPEN.exec(command);
|
|
34
34
|
const target = m && (m[1] ?? m[2] ?? m[3]);
|
|
@@ -56,7 +56,7 @@ function replacements(call: { name: string; input: unknown }): { old_string: str
|
|
|
56
56
|
const hash = (content: Block[]) => createHash("sha1").update(JSON.stringify(content.map((b) => (b.kind === "text" ? b.text : b)))).digest("hex");
|
|
57
57
|
|
|
58
58
|
/** 会话里的 hook:model.before 数回复次数,tool.gate 拒绝重复,tool.after 加提醒,event 在用户发消息、压缩后重新计 */
|
|
59
|
-
export function repeatsHooks(opts: { cwd: string; id?: string }): LocalHookSpec[] {
|
|
59
|
+
export function repeatsHooks(opts: { cwd: string; id?: string; /** 执行命令的工具把命令放在哪个字段(ctx.session.tool 的 meta.shell) */ shell: (tool: string) => string | undefined }): LocalHookSpec[] {
|
|
60
60
|
const id = opts.id ?? ID;
|
|
61
61
|
const cwd = opts.cwd;
|
|
62
62
|
/** 模型调用的次数,每次调用模型前加一 */
|
|
@@ -99,7 +99,7 @@ export function repeatsHooks(opts: { cwd: string; id?: string }): LocalHookSpec[
|
|
|
99
99
|
return { decision: "deny", reason: "This call is identical to an earlier call in the same response and was not run; use that call's result." };
|
|
100
100
|
}
|
|
101
101
|
current.keys.add(k);
|
|
102
|
-
const target = openTarget(call, cwd);
|
|
102
|
+
const target = openTarget(call, cwd, opts.shell(call.name));
|
|
103
103
|
// 同一次回复里排在前面的调用可能改了它,还没执行,不拒绝
|
|
104
104
|
if (target !== undefined && opened.get(target) === changes && !current.mutates) {
|
|
105
105
|
return {
|
|
@@ -123,7 +123,7 @@ export function repeatsHooks(opts: { cwd: string; id?: string }): LocalHookSpec[
|
|
|
123
123
|
readOnly.delete(outcome.call.toolUseId);
|
|
124
124
|
if (!outcome.isError) {
|
|
125
125
|
failures.delete(k);
|
|
126
|
-
const target = openTarget(outcome.call, cwd);
|
|
126
|
+
const target = openTarget(outcome.call, cwd, opts.shell(outcome.call.name));
|
|
127
127
|
if (target !== undefined) opened.set(target, changes);
|
|
128
128
|
else if (!read) changes++;
|
|
129
129
|
const file = (outcome.call.input as { path?: unknown } | null)?.path;
|
|
@@ -190,6 +190,6 @@ export function repeatsHooks(opts: { cwd: string; id?: string }): LocalHookSpec[
|
|
|
190
190
|
|
|
191
191
|
export default definePlugin({
|
|
192
192
|
session(ctx) {
|
|
193
|
-
for (const h of repeatsHooks({ cwd: ctx.cwd, id: ctx.plugin })) ctx.kernel.hook(h);
|
|
193
|
+
for (const h of repeatsHooks({ cwd: ctx.cwd, id: ctx.plugin, shell: (t) => ctx.session.tool(t)?.meta?.shell })) ctx.kernel.hook(h);
|
|
194
194
|
},
|
|
195
195
|
});
|
|
@@ -249,6 +249,8 @@ export default definePlugin({
|
|
|
249
249
|
},
|
|
250
250
|
|
|
251
251
|
async session(ctx) {
|
|
252
|
+
// 子会话(子 Agent)不建定时任务,也不接收:到点的任务投到用户的会话里
|
|
253
|
+
if (ctx.session.parent) return;
|
|
252
254
|
const store = new ScheduleStore(ctx.home);
|
|
253
255
|
for (const t of tools(store, ctx.cwd)) ctx.kernel.tool(t);
|
|
254
256
|
|
|
@@ -283,7 +285,9 @@ export default definePlugin({
|
|
|
283
285
|
.finally(() => running.delete(t.id));
|
|
284
286
|
}
|
|
285
287
|
};
|
|
286
|
-
//
|
|
288
|
+
// 会话建好就先检查一次:jade 没开着的时候到期的任务不用等第一个间隔。
|
|
289
|
+
// 不直接在这里检查:会话没能打开时排着的 send 被拒绝,认领了的任务就丢了;afterTurn 等会话建好才执行
|
|
290
|
+
ctx.session.afterTurn(() => void check());
|
|
287
291
|
await ctx.effect(() => {
|
|
288
292
|
const timer = setInterval(() => void check(), every);
|
|
289
293
|
return () => clearInterval(timer);
|
|
@@ -18,6 +18,8 @@ const READY_MS = 10_000;
|
|
|
18
18
|
const LOAD_MS = 15_000;
|
|
19
19
|
/** load 之后等网络安静下来(脚本、图片、字体)的最长时间 */
|
|
20
20
|
const SETTLE_MS = 3000;
|
|
21
|
+
/** 让浏览器自己关的最长时间,过了就直接结束进程 */
|
|
22
|
+
const CLOSE_MS = 5000;
|
|
21
23
|
/** 滚动之后给懒加载的图片、滚动触发的动画的时间 */
|
|
22
24
|
const SCROLL_SETTLE_MS = 400;
|
|
23
25
|
|
|
@@ -214,6 +216,14 @@ export const capture: Capture = async (browser, req, signal) => {
|
|
|
214
216
|
return await Promise.race([work(), aborted]);
|
|
215
217
|
} finally {
|
|
216
218
|
signal.removeEventListener("abort", abort);
|
|
219
|
+
// 先让浏览器自己关:直接结束主进程时,Windows 上它的子进程还会占着 profile 好几秒,临时目录删不掉。
|
|
220
|
+
// 中止了(用户打断、超时)就不等它:马上结束,删不掉的临时目录留给系统清理
|
|
221
|
+
if (cdp && !signal.aborted) {
|
|
222
|
+
void cdp.send("Browser.close").catch(() => {});
|
|
223
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
224
|
+
await Promise.race([proc.exited, new Promise((r) => (timer = setTimeout(r, CLOSE_MS)))]);
|
|
225
|
+
clearTimeout(timer);
|
|
226
|
+
}
|
|
217
227
|
cdp?.close();
|
|
218
228
|
proc.kill();
|
|
219
229
|
await proc.exited;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
// jade/sessions:/clear、/resume、/retry,以及 /resume 用的会话索引。
|
|
1
|
+
// jade/sessions:/clear、/resume、/rewind、/retry,以及 /resume 用的会话索引。
|
|
2
2
|
// 索引是 $JADE_HOME/state/sessions.jsonl,每行 { id, cwd, title, at }:会话在本进程里第一次收到消息时追加一行。
|
|
3
3
|
// 读的时候按 id 合并:标题取最早的一行,时间取最晚的一行;目录按 cwdKey 比较。
|
|
4
4
|
|
|
5
5
|
import { appendFile, mkdir, readFile } from "node:fs/promises";
|
|
6
6
|
import path from "node:path";
|
|
7
7
|
import type { UserInput } from "uncarved";
|
|
8
|
-
import { definePlugin } from "@uncarved/jade-api";
|
|
8
|
+
import { definePlugin, type Draft } from "@uncarved/jade-api";
|
|
9
9
|
import type { Commands, SessionIndex, SessionSummary } from "@uncarved/jade-api/services";
|
|
10
10
|
|
|
11
11
|
const TITLE_CHARS = 80;
|
|
@@ -69,6 +69,20 @@ function titleOf(input: UserInput): string | undefined {
|
|
|
69
69
|
return text.length > TITLE_CHARS ? `${text.slice(0, TITLE_CHARS)}…` : text;
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
+
/** 用户自己写的那部分文字(插件附上的内容不算);不是消息、或者没有时为空 */
|
|
73
|
+
function userText(input: UserInput): string {
|
|
74
|
+
if (input.kind !== "message") return "";
|
|
75
|
+
return input.content.flatMap((b) => (b.kind === "text" && b.origin.by === "user" ? [b.text] : [])).join("\n").trim();
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const oneLine = (text: string) => {
|
|
79
|
+
const one = text.replace(/\s+/g, " ");
|
|
80
|
+
return one.length > TITLE_CHARS ? `${one.slice(0, TITLE_CHARS)}…` : one;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
/** 最近打开的会话的输入框(/rewind 把消息放回去);前端没有输入框时为 undefined */
|
|
84
|
+
let input: Draft | undefined;
|
|
85
|
+
|
|
72
86
|
export default definePlugin({
|
|
73
87
|
process(ctx) {
|
|
74
88
|
const file = path.join(ctx.home, "state", "sessions.jsonl");
|
|
@@ -102,6 +116,35 @@ export default definePlugin({
|
|
|
102
116
|
},
|
|
103
117
|
});
|
|
104
118
|
|
|
119
|
+
commands.register({
|
|
120
|
+
name: "rewind",
|
|
121
|
+
description: "go back to before one of your messages, in a copy of this session (files stay as they are; to restore them, /undo in the session before rewinding)",
|
|
122
|
+
async run(_args, c) {
|
|
123
|
+
const s = c.sessions.current;
|
|
124
|
+
if (!s) return "There is no open session.";
|
|
125
|
+
const sent = (await s.history()).flatMap((e) => {
|
|
126
|
+
const text = e.type === "input.received" ? userText(e.data as UserInput) : "";
|
|
127
|
+
return text ? [{ seq: e.seq, text }] : [];
|
|
128
|
+
});
|
|
129
|
+
if (sent.length === 0) return "Nothing to rewind to: you have not sent a message in this session.";
|
|
130
|
+
const recent = sent.slice(-50).reverse();
|
|
131
|
+
const picked = await c.ui.select(
|
|
132
|
+
{
|
|
133
|
+
title: "Rewind to before which message?",
|
|
134
|
+
options: recent.map((m, i) => ({ label: oneLine(m.text), value: String(m.seq), hint: i === 0 ? "your last message" : `${i + 1} messages ago` })),
|
|
135
|
+
},
|
|
136
|
+
c.signal,
|
|
137
|
+
);
|
|
138
|
+
const to = recent.find((m) => String(m.seq) === picked);
|
|
139
|
+
if (!to) return;
|
|
140
|
+
// 原会话不动:复制出它在这条消息之前的部分,作为新会话打开。前端在会话换掉之后重新显示它的对话
|
|
141
|
+
await c.sessions.open({ fork: { session: s.id, before: to.seq } });
|
|
142
|
+
// 那条消息放回输入框,改一改再发
|
|
143
|
+
input?.set(to.text);
|
|
144
|
+
return `Rewound to before that message${input ? "; it is back in the input" : ""}. Files were not changed; to restore them, run /undo in the session before rewinding: jade --resume ${s.id}`;
|
|
145
|
+
},
|
|
146
|
+
});
|
|
147
|
+
|
|
105
148
|
commands.register({
|
|
106
149
|
name: "retry",
|
|
107
150
|
description: "ask the model again after a failed request",
|
|
@@ -114,6 +157,9 @@ export default definePlugin({
|
|
|
114
157
|
},
|
|
115
158
|
|
|
116
159
|
session(ctx) {
|
|
160
|
+
// 子会话(子 Agent)不进 /resume 的列表,也没有输入框
|
|
161
|
+
if (ctx.session.parent) return;
|
|
162
|
+
input = ctx.input;
|
|
117
163
|
const file = path.join(ctx.home, "state", "sessions.jsonl");
|
|
118
164
|
let indexed = false;
|
|
119
165
|
ctx.kernel.hook({
|
package/plugins/shell/index.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
// jade/shell:bash 工具,以及后台命令的 bash_output、kill_shell;shell@1 给其他插件执行命令。
|
|
2
2
|
// 后台命令自己结束时告诉用户,并给模型留一句说明(不用它一直轮询);/shells 给用户看和停掉后台命令。
|
|
3
3
|
|
|
4
|
-
import { definePlugin, type UI } from "@uncarved/jade-api";
|
|
5
|
-
import type { Commands, Prompts, Renderers,
|
|
4
|
+
import { definePlugin, type Span, type UI } from "@uncarved/jade-api";
|
|
5
|
+
import type { Commands, Prompts, Renderers, Undo } from "@uncarved/jade-api/services";
|
|
6
6
|
import { shellRenderers } from "./renderers.ts";
|
|
7
7
|
import { shellService } from "./service.ts";
|
|
8
8
|
import { shellTools, type ShellTools } from "./shell.ts";
|
|
@@ -20,10 +20,8 @@ const SH_TIMEOUT_MS = 600_000;
|
|
|
20
20
|
const SH_LIVE_CHARS = 4000;
|
|
21
21
|
/** /sh 的结果收起时那一行最多这么多个字符 */
|
|
22
22
|
const SH_SUMMARY = 160;
|
|
23
|
-
|
|
24
|
-
const
|
|
25
|
-
/** 失败的红色,其余的灰色 */
|
|
26
|
-
const shade = (failed: boolean, s: string) => (colors ? (failed ? `\x1b[31m${s}\x1b[39m` : `\x1b[2m${s}\x1b[22m`) : s);
|
|
23
|
+
/** 结果的一行:失败的是错误,成功的是次要的(带语义样式的文字,怎么画由前端决定) */
|
|
24
|
+
const shade = (failed: boolean, text: string): Span => ({ text, style: failed ? "error" : "dim" });
|
|
27
25
|
|
|
28
26
|
export default definePlugin({
|
|
29
27
|
process(ctx) {
|
|
@@ -41,6 +39,8 @@ export default definePlugin({
|
|
|
41
39
|
name: "sh",
|
|
42
40
|
args: "<command>",
|
|
43
41
|
description: "run a shell command yourself (short form: !<command>); then the agent continues with its output",
|
|
42
|
+
prefix: "!",
|
|
43
|
+
prefixHint: "shell command · you run it, not the agent",
|
|
44
44
|
// 不是 instant:和消息一样排队,前端才把它当作正在做的事(esc 能停掉它)
|
|
45
45
|
async run(args, { sessions: manager, signal, print, progress }) {
|
|
46
46
|
const command = args.trim();
|
|
@@ -50,8 +50,9 @@ export default definePlugin({
|
|
|
50
50
|
let live = "";
|
|
51
51
|
let r: { code: number | null; output: string; timedOut: boolean };
|
|
52
52
|
let stopped = false;
|
|
53
|
-
|
|
54
|
-
|
|
53
|
+
const session = manager.current;
|
|
54
|
+
const exec = () =>
|
|
55
|
+
shell.run(command, {
|
|
55
56
|
cwd: ctx.cwd,
|
|
56
57
|
timeoutMs: SH_TIMEOUT_MS,
|
|
57
58
|
signal,
|
|
@@ -60,6 +61,10 @@ export default definePlugin({
|
|
|
60
61
|
progress?.(live);
|
|
61
62
|
},
|
|
62
63
|
});
|
|
64
|
+
// 有 undo@1 时这条命令改的文件能用 /undo 撤销(单独算一步)
|
|
65
|
+
const undo = ctx.service<Undo>("undo@1");
|
|
66
|
+
try {
|
|
67
|
+
r = session && undo ? await undo.track(session.id, `$ ${firstLine(command)}`, exec) : await exec();
|
|
63
68
|
} catch (e) {
|
|
64
69
|
if (!signal.aborted) throw e;
|
|
65
70
|
stopped = true;
|
|
@@ -73,7 +78,6 @@ export default definePlugin({
|
|
|
73
78
|
const more = lines.length > 1 ? ` (+${lines.length - 1} lines)` : "";
|
|
74
79
|
const folded = lines.length > 1 || first.length > SH_SUMMARY;
|
|
75
80
|
print(shade(failed, `⎿ ${first.length > SH_SUMMARY ? `${first.slice(0, SH_SUMMARY)}…` : first}${more}`), folded ? { details: r.output.replace(/^/gm, " ") } : undefined);
|
|
76
|
-
const session = manager.current;
|
|
77
81
|
if (!stopped && session && session.state.messages.length > 0) return { prompt: `I ran this shell command myself (${status}):\n$ ${command}\n${r.output}` };
|
|
78
82
|
if (session) ctx.service<Prompts>("prompts@1")?.note(session.id, `The user ran this shell command themselves (${status}):\n$ ${command}\n${r.output}`);
|
|
79
83
|
},
|
|
@@ -107,25 +111,20 @@ export default definePlugin({
|
|
|
107
111
|
},
|
|
108
112
|
});
|
|
109
113
|
sessions.set(id, { list, kill });
|
|
110
|
-
//
|
|
111
|
-
|
|
112
|
-
const
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
} catch {
|
|
125
|
-
// jade/tools 被隔离时它的服务已失效:嵌套 Agent 没有 bash,主会话的照常
|
|
126
|
-
}
|
|
127
|
-
if (unavailable) ctx.service<UI>("ui@1")?.notify(unavailable, "warning");
|
|
128
|
-
for (const tool of tools) ctx.kernel.tool(tool);
|
|
114
|
+
// 子会话(子 Agent)里的 bash 只收命令和时限,不能在后台跑:子 Agent 交了报告会话就结束,后台命令随之被杀掉
|
|
115
|
+
const child = ctx.session.parent !== undefined;
|
|
116
|
+
const foreground = (t: (typeof tools)[number]): (typeof tools)[number] =>
|
|
117
|
+
t.name !== "bash"
|
|
118
|
+
? t
|
|
119
|
+
: {
|
|
120
|
+
...t,
|
|
121
|
+
inputSchema: { type: "object", properties: { command: { type: "string" }, timeout_ms: { type: "integer" } }, required: ["command"], additionalProperties: false },
|
|
122
|
+
execute: (input, c) => t.execute({ command: (input as { command: string }).command, timeout_ms: (input as { timeout_ms?: number }).timeout_ms }, c),
|
|
123
|
+
};
|
|
124
|
+
// 主会话已经说过一次
|
|
125
|
+
if (unavailable && !child) ctx.service<UI>("ui@1")?.notify(unavailable, "warning");
|
|
126
|
+
// 管后台命令的工具(bash_output、kill_shell)子会话里用不上
|
|
127
|
+
for (const tool of tools) if (!(child && (tool.name === "bash_output" || tool.name === "kill_shell"))) ctx.kernel.tool(child ? foreground(tool) : tool);
|
|
129
128
|
if (executable) {
|
|
130
129
|
const lines = [`- Shell for the bash tool: ${executable} (POSIX syntax, also on Windows)`];
|
|
131
130
|
// 模型常用 xdg-open、open 打开文件,Git Bash 里没有;start 是 Git 自带的 /usr/bin/start
|