@uncarved/jade 0.2.1 → 0.3.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 +65 -2
- package/assets/logo.svg +1 -0
- package/bin/jade.js +4 -0
- package/dist/api.d.ts +5 -250
- package/dist/api.js +5 -7
- package/dist/api.js.map +1 -1
- package/dist/fetch.d.ts +8 -0
- package/dist/fetch.js +19 -17
- package/dist/fetch.js.map +1 -1
- package/dist/host.d.ts +12 -1
- package/dist/host.js +49 -1
- package/dist/host.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/main.js +26 -4
- package/dist/main.js.map +1 -1
- package/dist/remote.js +4 -0
- package/dist/remote.js.map +1 -1
- package/dist/session.d.ts +2 -0
- package/dist/session.js +133 -13
- package/dist/session.js.map +1 -1
- package/dist/source.d.ts +2 -10
- package/dist/source.js.map +1 -1
- package/dist/update.d.ts +8 -20
- package/dist/update.js +12 -1
- package/dist/update.js.map +1 -1
- package/package.json +4 -2
- package/plugins/approval/index.ts +236 -85
- package/plugins/approval/plugin.json +1 -1
- package/plugins/approval/policy.ts +36 -17
- package/plugins/approval/review.ts +1 -1
- package/plugins/ask/index.ts +2 -2
- package/plugins/budget/index.ts +1 -1
- package/plugins/checks/checks.ts +3 -2
- package/plugins/checks/index.ts +2 -5
- package/plugins/commands/index.ts +2 -44
- package/plugins/compact/compact.ts +8 -3
- package/plugins/compact/index.ts +13 -16
- package/plugins/continue/index.ts +1 -1
- package/plugins/cost/index.ts +143 -0
- package/plugins/cost/plugin.json +13 -0
- package/plugins/custom-commands/index.ts +85 -0
- package/plugins/custom-commands/plugin.json +8 -0
- package/plugins/editor/index.ts +88 -0
- package/plugins/editor/plugin.json +17 -0
- package/plugins/fs/fs.ts +33 -6
- package/plugins/fs/grep.ts +19 -7
- package/plugins/fs/index.ts +6 -7
- package/plugins/fs/plugin.json +1 -1
- package/plugins/fs/renderers.ts +1 -1
- package/plugins/hooks/index.ts +152 -0
- package/plugins/hooks/plugin.json +14 -0
- package/plugins/keys/index.ts +2 -18
- package/plugins/mcp/index.ts +43 -15
- package/plugins/mcp/install.ts +139 -0
- package/plugins/mcp/mcp.ts +2 -1
- package/plugins/mcp/plugin.json +1 -1
- package/plugins/mcp/project.ts +37 -1
- package/plugins/memory/index.ts +4 -9
- package/plugins/memory/plugin.json +1 -1
- package/plugins/mentions/index.ts +2 -2
- package/plugins/models/configure.ts +258 -150
- package/plugins/models/context.ts +2 -2
- package/plugins/models/index.ts +39 -45
- package/plugins/models/plugin.json +1 -1
- package/plugins/models/provider.ts +34 -5
- package/plugins/models/saved.ts +8 -4
- package/plugins/modes/index.ts +2 -17
- package/plugins/plugins/index.ts +2 -2
- package/plugins/print/index.ts +36 -6
- package/plugins/print/plugin.json +1 -0
- package/plugins/prompts/index.ts +22 -14
- package/plugins/{memory → prompts}/notes.ts +21 -9
- package/plugins/prompts/plugin.json +8 -2
- package/plugins/prompts/prompts.ts +5 -4
- package/plugins/renderers/index.ts +2 -43
- package/plugins/repeats/index.ts +32 -2
- package/plugins/schedule/index.ts +2 -2
- package/plugins/scratch/index.ts +2 -3
- package/plugins/screenshot/browser.ts +221 -0
- package/plugins/screenshot/index.ts +29 -15
- package/plugins/screenshot/plugin.json +1 -1
- package/plugins/screenshot/tool.ts +27 -51
- package/plugins/sessions/index.ts +2 -17
- package/plugins/shell/index.ts +115 -7
- package/plugins/shell/plugin.json +3 -1
- package/plugins/shell/renderers.ts +1 -1
- package/plugins/shell/service.ts +2 -13
- package/plugins/shell/shell.ts +119 -47
- package/plugins/skills/index.ts +172 -37
- package/plugins/skills/install.ts +212 -0
- package/plugins/skills/plugin.json +3 -2
- package/plugins/skills/project.ts +38 -1
- package/plugins/skills/skills.ts +28 -9
- package/plugins/steer/index.ts +142 -0
- package/plugins/steer/plugin.json +8 -0
- package/plugins/suggest/index.ts +16 -11
- package/plugins/suggest/plugin.json +1 -1
- package/plugins/task/index.ts +18 -57
- package/plugins/task/plugin.json +9 -3
- package/plugins/task/subagent.ts +25 -7
- package/plugins/todo/index.ts +2 -2
- package/plugins/tools/index.ts +50 -0
- package/plugins/tools/plugin.json +7 -0
- package/plugins/tui/editor.ts +10 -2
- package/plugins/tui/frontend.ts +129 -23
- package/plugins/tui/index.ts +3 -8
- package/plugins/tui/markdown.ts +51 -6
- package/plugins/tui/mascot.ts +506 -0
- package/plugins/tui/plugin.json +2 -2
- package/plugins/tui/screen.ts +6 -0
- package/plugins/tui/transcript.ts +17 -2
- package/plugins/tui/tui.ts +344 -97
- package/plugins/undo/checkpoint.ts +2 -2
- package/plugins/undo/index.ts +84 -26
- package/plugins/undo/plugin.json +8 -2
- package/plugins/undo/snapshot.ts +129 -0
- package/plugins/update/index.ts +11 -15
- package/plugins/update/plugin.json +2 -1
- package/plugins/web/index.ts +8 -11
- package/plugins/web/plugin.json +1 -1
- package/plugins/web/renderers.ts +1 -1
- package/plugins-dist/approval/index.js +221 -51
- package/plugins-dist/approval/plugin.json +1 -1
- package/plugins-dist/approval/policy.js +30 -15
- package/plugins-dist/ask/index.js +1 -1
- package/plugins-dist/budget/index.js +1 -1
- package/plugins-dist/checks/checks.js +2 -1
- package/plugins-dist/checks/index.js +1 -1
- package/plugins-dist/commands/index.js +1 -1
- package/plugins-dist/compact/compact.js +6 -1
- package/plugins-dist/compact/index.js +12 -8
- package/plugins-dist/continue/index.js +1 -1
- package/plugins-dist/cost/index.js +135 -0
- package/plugins-dist/cost/plugin.json +16 -0
- package/plugins-dist/custom-commands/index.js +86 -0
- package/plugins-dist/custom-commands/plugin.json +12 -0
- package/plugins-dist/editor/index.js +89 -0
- package/plugins-dist/editor/plugin.json +21 -0
- package/plugins-dist/fs/fs.js +34 -6
- package/plugins-dist/fs/grep.js +17 -6
- package/plugins-dist/fs/index.js +6 -6
- package/plugins-dist/fs/plugin.json +1 -1
- package/plugins-dist/hooks/index.js +145 -0
- package/plugins-dist/hooks/plugin.json +19 -0
- package/plugins-dist/keys/index.js +1 -1
- package/plugins-dist/mcp/index.js +45 -13
- package/plugins-dist/mcp/install.js +160 -0
- package/plugins-dist/mcp/mcp.js +2 -1
- package/plugins-dist/mcp/plugin.json +2 -1
- package/plugins-dist/mcp/project.js +42 -1
- package/plugins-dist/memory/index.js +3 -5
- package/plugins-dist/memory/plugin.json +1 -1
- package/plugins-dist/mentions/index.js +1 -1
- package/plugins-dist/models/configure.js +252 -151
- package/plugins-dist/models/context.js +2 -2
- package/plugins-dist/models/index.js +41 -17
- package/plugins-dist/models/plugin.json +1 -1
- package/plugins-dist/models/provider.js +17 -3
- package/plugins-dist/models/saved.js +9 -4
- package/plugins-dist/modes/index.js +1 -1
- package/plugins-dist/plugins/index.js +1 -1
- package/plugins-dist/print/index.js +37 -3
- package/plugins-dist/print/plugin.json +3 -0
- package/plugins-dist/prompts/index.js +22 -3
- package/plugins-dist/{memory → prompts}/notes.js +10 -7
- package/plugins-dist/prompts/plugin.json +8 -2
- package/plugins-dist/prompts/prompts.js +5 -4
- package/plugins-dist/renderers/index.js +1 -1
- package/plugins-dist/repeats/index.js +28 -2
- package/plugins-dist/schedule/index.js +1 -1
- package/plugins-dist/scratch/index.js +1 -1
- package/plugins-dist/screenshot/browser.js +187 -0
- package/plugins-dist/screenshot/index.js +28 -13
- package/plugins-dist/screenshot/plugin.json +1 -1
- package/plugins-dist/screenshot/tool.js +27 -49
- package/plugins-dist/sessions/index.js +1 -1
- package/plugins-dist/shell/index.js +119 -4
- package/plugins-dist/shell/plugin.json +3 -1
- package/plugins-dist/shell/shell.js +106 -36
- package/plugins-dist/skills/index.js +184 -35
- package/plugins-dist/skills/install.js +178 -0
- package/plugins-dist/skills/plugin.json +6 -2
- package/plugins-dist/skills/project.js +43 -1
- package/plugins-dist/skills/skills.js +27 -10
- package/plugins-dist/steer/index.js +135 -0
- package/plugins-dist/steer/plugin.json +12 -0
- package/plugins-dist/suggest/index.js +15 -9
- package/plugins-dist/suggest/plugin.json +1 -1
- package/plugins-dist/task/index.js +17 -46
- package/plugins-dist/task/plugin.json +11 -5
- package/plugins-dist/task/subagent.js +21 -6
- package/plugins-dist/todo/index.js +1 -1
- package/plugins-dist/tools/index.js +49 -0
- package/plugins-dist/tools/plugin.json +9 -0
- package/plugins-dist/tui/editor.js +7 -2
- package/plugins-dist/tui/frontend.js +134 -15
- package/plugins-dist/tui/index.js +2 -1
- package/plugins-dist/tui/markdown.js +48 -6
- package/plugins-dist/tui/mascot.js +441 -0
- package/plugins-dist/tui/plugin.json +3 -2
- package/plugins-dist/tui/screen.js +2 -0
- package/plugins-dist/tui/transcript.js +16 -1
- package/plugins-dist/tui/tui.js +320 -88
- package/plugins-dist/tui/vendor.js +1388 -1088
- package/plugins-dist/undo/checkpoint.js +1 -1
- package/plugins-dist/undo/index.js +91 -24
- package/plugins-dist/undo/plugin.json +10 -3
- package/plugins-dist/undo/snapshot.js +111 -0
- package/plugins-dist/update/index.js +9 -10
- package/plugins-dist/update/plugin.json +4 -1
- package/plugins-dist/web/index.js +7 -7
- package/plugins-dist/web/plugin.json +1 -1
- package/sdk/python/__pycache__/jade_plugin.cpython-312.pyc +0 -0
- package/sdk/python/jade_plugin.py +8 -0
- package/src/api.ts +7 -262
- package/src/fetch.ts +18 -16
- package/src/host.ts +49 -1
- package/src/index.ts +1 -1
- package/src/main.ts +23 -5
- package/src/remote.ts +2 -0
- package/src/session.ts +134 -15
- package/src/source.ts +2 -11
- package/src/update.ts +16 -10
- package/plugins/undo/notes.ts +0 -43
- package/plugins-dist/undo/notes.js +0 -36
package/plugins/models/index.ts
CHANGED
|
@@ -6,40 +6,13 @@
|
|
|
6
6
|
|
|
7
7
|
import path from "node:path";
|
|
8
8
|
import type { ModelRequest, ModelResponse, Provider } from "uncarved";
|
|
9
|
-
import { definePlugin, type UI } from "
|
|
10
|
-
import type { Commands } from "
|
|
9
|
+
import { definePlugin, type UI } from "@uncarved/jade-api";
|
|
10
|
+
import type { Commands, Models } from "@uncarved/jade-api/services";
|
|
11
11
|
import { contextWindow } from "./context.ts";
|
|
12
12
|
import { envWithFile, hasProvider, parseTokens } from "./config.ts";
|
|
13
13
|
import { configurator } from "./configure.ts";
|
|
14
|
-
import { isBuiltinKind, listModels, MODEL_KEY, providerFromEnv, providerInfo, providerKind, RateGate, rpmFromEnv, visionFromEnv, withRetry } from "./provider.ts";
|
|
15
|
-
import { readSavedServices, type Env } from "./saved.ts";
|
|
16
|
-
|
|
17
|
-
/** models@1 */
|
|
18
|
-
export interface Models {
|
|
19
|
-
/** 当前配置的服务和模型;没有配置时 undefined。via 是给人看的服务说明,例如 "the Anthropic API" */
|
|
20
|
-
current(): {
|
|
21
|
-
kind: string;
|
|
22
|
-
model: string;
|
|
23
|
-
via: string;
|
|
24
|
-
/** 上下文窗口(token) */
|
|
25
|
-
contextWindow: number;
|
|
26
|
-
/** 模型能不能看图片(JADE_VISION;不设时按服务推断) */
|
|
27
|
-
vision: boolean;
|
|
28
|
-
/** 每次回复的输出上限(JADE_MAX_TOKENS,含 .env 里的);不设或写错时没有,用服务的默认值 */
|
|
29
|
-
maxTokens?: number;
|
|
30
|
-
} | undefined;
|
|
31
|
-
/**
|
|
32
|
-
* 完成一次请求,带重试和限速。默认用当前配置的模型(限速和会话共用);
|
|
33
|
-
* model 是 /model 保存过的模型名($JADE_HOME/models.json),用它和它自己的设置,找不到时报错
|
|
34
|
-
*/
|
|
35
|
-
complete(request: ModelRequest, options: { signal: AbortSignal; onDelta?: (delta: import("uncarved").Delta) => void; model?: string }): Promise<ModelResponse>;
|
|
36
|
-
/** 服务提供的模型列表;不支持或出错时为空 */
|
|
37
|
-
list(): Promise<string[]>;
|
|
38
|
-
/** /model 保存过的模型名(complete 的 model 能用的那些),最近用过的在前 */
|
|
39
|
-
saved?(): Promise<string[]>;
|
|
40
|
-
/** 配置换了(/model)时调用,返回取消订阅。前端用它更新状态栏 */
|
|
41
|
-
subscribe?(fn: () => void): () => void;
|
|
42
|
-
}
|
|
14
|
+
import { isBuiltinKind, listModels, MODEL_KEY, providerFromEnv, providerInfo, providerKind, RateGate, rpmFromEnv, visionFromEnv, withRetry, type RetriedResponse } from "./provider.ts";
|
|
15
|
+
import { MODEL_SETTINGS, readSavedServices, type Env } from "./saved.ts";
|
|
43
16
|
|
|
44
17
|
export default definePlugin({
|
|
45
18
|
async process(ctx) {
|
|
@@ -47,7 +20,8 @@ export default definePlugin({
|
|
|
47
20
|
const modelsFile = path.join(ctx.home, "models.json");
|
|
48
21
|
// 当前的模型配置。改配置时整份换掉(updateEnvFile 改的是这一份,apply 再从文件读一遍)
|
|
49
22
|
let env = await envWithFile(envFile);
|
|
50
|
-
|
|
23
|
+
// 用的时候再取:jade/tui 也依赖这个插件(models@1),它在这之后才激活,这里取到的是 undefined,重试的提示就没人显示
|
|
24
|
+
const ui = () => ctx.service<UI>("ui@1");
|
|
51
25
|
const watchers = new Set<() => void>();
|
|
52
26
|
const notify = () => {
|
|
53
27
|
for (const fn of watchers) {
|
|
@@ -71,11 +45,11 @@ export default definePlugin({
|
|
|
71
45
|
// 用创建时的这份配置:之后换了配置,新的一份自己建 provider
|
|
72
46
|
const build = (snapshot: Env): Promise<Provider> => {
|
|
73
47
|
return (async () => {
|
|
74
|
-
const gate = new RateGate({ rpm: rpmFromEnv(snapshot), onLearn: (rpm) => ui?.notify(`rate limited: spacing requests to ${rpm} per minute from now on`, "warning") });
|
|
48
|
+
const gate = new RateGate({ rpm: rpmFromEnv(snapshot), onLearn: (rpm) => ui()?.notify(`rate limited: spacing requests to ${rpm} per minute from now on`, "warning") });
|
|
75
49
|
return withRetry(await providerFromEnv(snapshot), {
|
|
76
50
|
gate,
|
|
77
51
|
onRetry: ({ attempt, retries, delayMs, error }) =>
|
|
78
|
-
ui?.notify(`model request failed (${error.message}); retry ${attempt} of ${retries} in ${Math.round(delayMs / 1000)}s`, "warning"),
|
|
52
|
+
ui()?.notify(`model request failed (${error.message}); retry ${attempt} of ${retries} in ${Math.round(delayMs / 1000)}s`, "warning"),
|
|
79
53
|
});
|
|
80
54
|
})();
|
|
81
55
|
};
|
|
@@ -95,24 +69,32 @@ export default definePlugin({
|
|
|
95
69
|
|
|
96
70
|
/** 保存过的模型的 provider,按配置缓存:models.json 里的密钥或设置改了就另建一份 */
|
|
97
71
|
const savedProviders = new Map<string, Promise<Provider>>();
|
|
72
|
+
const cachedBuild = (snapshot: Env): Promise<Provider> => {
|
|
73
|
+
const key = JSON.stringify(snapshot);
|
|
74
|
+
const cached = savedProviders.get(key);
|
|
75
|
+
if (cached) return cached;
|
|
76
|
+
const p = build(snapshot);
|
|
77
|
+
savedProviders.set(key, p);
|
|
78
|
+
// 创建失败时下次重试
|
|
79
|
+
p.catch(() => savedProviders.delete(key));
|
|
80
|
+
return p;
|
|
81
|
+
};
|
|
98
82
|
const saved = async (name: string): Promise<Provider> => {
|
|
99
83
|
for (const s of await readSavedServices(modelsFile)) {
|
|
100
84
|
const m = s.models.find((x) => x.name === name);
|
|
101
85
|
const kind = providerKind(s.env);
|
|
102
86
|
if (!m || !isBuiltinKind(kind)) continue;
|
|
103
|
-
|
|
104
|
-
const key = JSON.stringify(snapshot);
|
|
105
|
-
const cached = savedProviders.get(key);
|
|
106
|
-
if (cached) return cached;
|
|
107
|
-
const p = build(snapshot);
|
|
108
|
-
savedProviders.set(key, p);
|
|
109
|
-
// 创建失败时下次重试
|
|
110
|
-
p.catch(() => savedProviders.delete(key));
|
|
111
|
-
return p;
|
|
87
|
+
return cachedBuild({ ...s.env, ...m.settings, JADE_PROVIDER: kind, [MODEL_KEY[kind]]: m.name });
|
|
112
88
|
}
|
|
89
|
+
if (!hasProvider(env)) throw notConfigured();
|
|
113
90
|
// 只写在 .env 里、没保存过的当前模型
|
|
114
|
-
if (
|
|
115
|
-
|
|
91
|
+
if (providerInfo(env).model === name) return provider();
|
|
92
|
+
// 当前服务上的另一个模型(没保存过):用这个服务的地址和密钥,不带当前模型自己的设置,用服务的默认值
|
|
93
|
+
const kind = providerKind(env);
|
|
94
|
+
if (!isBuiltinKind(kind)) throw new Error(`model ${JSON.stringify(name)} is not one of the models saved by /model (${modelsFile})`);
|
|
95
|
+
const snapshot: Env = { ...env, JADE_PROVIDER: kind, [MODEL_KEY[kind]]: name };
|
|
96
|
+
for (const k of MODEL_SETTINGS) delete snapshot[k];
|
|
97
|
+
return cachedBuild(snapshot);
|
|
116
98
|
};
|
|
117
99
|
|
|
118
100
|
const current: Provider["complete"] = (request, options) => provider().then((p) => p.complete(request, options));
|
|
@@ -195,6 +177,18 @@ export default definePlugin({
|
|
|
195
177
|
},
|
|
196
178
|
});
|
|
197
179
|
},
|
|
180
|
+
|
|
181
|
+
session(ctx) {
|
|
182
|
+
// 重试过的请求:把每次失败写进会话日志,事后能查到为什么慢、为什么重新输出(插件事件,不进上下文)
|
|
183
|
+
ctx.kernel.hook({
|
|
184
|
+
kind: "observe",
|
|
185
|
+
point: "model.after",
|
|
186
|
+
fn: async (res, hook) => {
|
|
187
|
+
const retries = (res as RetriedResponse).retries;
|
|
188
|
+
if (retries?.length) await hook.emit("retried", { retries });
|
|
189
|
+
},
|
|
190
|
+
});
|
|
191
|
+
},
|
|
198
192
|
});
|
|
199
193
|
|
|
200
194
|
function via(kind: string, baseURL: string | undefined): string {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Provider:按环境变量选择,外面包一层重试(4.7:重试是包装 Provider 的事)。
|
|
2
2
|
|
|
3
|
-
import { ProviderError, type Provider } from "uncarved";
|
|
3
|
+
import { ProviderError, type ModelResponse, type Provider } from "uncarved";
|
|
4
4
|
import { parseTokens } from "./config.ts";
|
|
5
5
|
|
|
6
6
|
/** 默认的重试等待:一次比一次久,最长 3 分钟 */
|
|
@@ -33,6 +33,22 @@ export const isRateLimit = (err: ProviderError) => err.status === 429 || /\b429\
|
|
|
33
33
|
*/
|
|
34
34
|
export const isConcurrencyLimit = (err: ProviderError) => /concurren/i.test(err.message);
|
|
35
35
|
|
|
36
|
+
/** 重试过的请求里每次失败的记录 */
|
|
37
|
+
export interface RetryRecord {
|
|
38
|
+
attempt: number;
|
|
39
|
+
delayMs: number;
|
|
40
|
+
error: string;
|
|
41
|
+
status?: number;
|
|
42
|
+
/** 失败前已经输出了一部分(界面上标了作废) */
|
|
43
|
+
discarded?: true;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* withRetry 的回复:重试过时带上失败的记录。核心写日志时只取回复的 content、stopReason、usage,
|
|
48
|
+
* 这一项进不了上下文;jade/models 在 model.after 里把它写成插件事件
|
|
49
|
+
*/
|
|
50
|
+
export type RetriedResponse = ModelResponse & { retries?: RetryRecord[] };
|
|
51
|
+
|
|
36
52
|
export function withRetry(inner: Provider, opts: RetryOptions = {}): Provider {
|
|
37
53
|
const delays = opts.delaysMs ?? RETRY_DELAYS_MS;
|
|
38
54
|
const retries = opts.retries ?? delays.length;
|
|
@@ -40,7 +56,8 @@ export function withRetry(inner: Provider, opts: RetryOptions = {}): Provider {
|
|
|
40
56
|
const random = opts.random ?? Math.random;
|
|
41
57
|
return {
|
|
42
58
|
id: `retry(${inner.id})`,
|
|
43
|
-
async complete(request, options) {
|
|
59
|
+
async complete(request, options): Promise<RetriedResponse> {
|
|
60
|
+
const failures: RetryRecord[] = [];
|
|
44
61
|
for (let attempt = 1; ; attempt++) {
|
|
45
62
|
let streamed = false;
|
|
46
63
|
await opts.gate?.acquire(options.signal);
|
|
@@ -54,9 +71,20 @@ export function withRetry(inner: Provider, opts: RetryOptions = {}): Provider {
|
|
|
54
71
|
},
|
|
55
72
|
});
|
|
56
73
|
// 实际回复的是被包装者(4.7 第 3 款)
|
|
57
|
-
return { ...res, provider: res.provider ?? inner.id };
|
|
74
|
+
return { ...res, provider: res.provider ?? inner.id, ...(failures.length > 0 ? { retries: failures } : {}) };
|
|
58
75
|
} catch (err) {
|
|
59
|
-
if (!(err instanceof ProviderError) ||
|
|
76
|
+
if (!(err instanceof ProviderError) || options.signal.aborted) throw err;
|
|
77
|
+
if (!err.retryable || attempt > retries) {
|
|
78
|
+
if (failures.length === 0) throw err;
|
|
79
|
+
// 失败时没有回复可以带上重试记录:写进错误信息里,会话日志(turn.ended)和界面上都看得到重试过、等了多久
|
|
80
|
+
const waited = Math.round(failures.reduce((ms, f) => ms + f.delayMs, 0) / 1000);
|
|
81
|
+
throw new ProviderError(`${err.message} (after ${failures.length} ${failures.length === 1 ? "retry" : "retries"}, ${waited}s of waiting)`, {
|
|
82
|
+
retryable: err.retryable,
|
|
83
|
+
status: err.status,
|
|
84
|
+
retryAfterMs: err.retryAfterMs,
|
|
85
|
+
cause: err,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
60
88
|
// 在计划的时间上随机缩短最多 20%,并行的子 Agent 同时遇到 429 时不会同时重试。
|
|
61
89
|
// 上游给的 retryAfterMs 只当下限:有的服务(尤其是并发数限流)每次都回 Retry-After: 1,
|
|
62
90
|
// 拿它代替退避计划会每秒撞一次,十几秒就把重试次数用完
|
|
@@ -66,6 +94,7 @@ export function withRetry(inner: Provider, opts: RetryOptions = {}): Provider {
|
|
|
66
94
|
if (isRateLimit(err)) opts.gate?.limited(delayMs, { learn: !isConcurrencyLimit(err) });
|
|
67
95
|
// 失败的这次已经推送过增量的,先让订阅者作废它们(4.7)
|
|
68
96
|
if (streamed) options.onDelta?.({ kind: "reset" });
|
|
97
|
+
failures.push({ attempt, delayMs, error: err.message, ...(err.status !== undefined ? { status: err.status } : {}), ...(streamed ? { discarded: true as const } : {}) });
|
|
69
98
|
opts.onRetry?.({ attempt, retries, delayMs, error: err });
|
|
70
99
|
await sleep(delayMs, options.signal);
|
|
71
100
|
}
|
|
@@ -351,7 +380,7 @@ export async function listModels(env: Env = process.env, fetchImpl: typeof fetch
|
|
|
351
380
|
}
|
|
352
381
|
}
|
|
353
382
|
|
|
354
|
-
function jsonObject(name: string, text: string): Record<string, unknown> {
|
|
383
|
+
export function jsonObject(name: string, text: string): Record<string, unknown> {
|
|
355
384
|
let value: unknown;
|
|
356
385
|
try {
|
|
357
386
|
value = JSON.parse(text);
|
package/plugins/models/saved.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// 保存过的模型:$JADE_HOME/models.json。同一个服务(类型、地址、密钥都相同)只存一份,下面是在它上面用过的模型,
|
|
2
|
-
//
|
|
2
|
+
// 每个模型带自己的设置(输出上限、思考力度、上下文窗口、openai-compat 的额外请求参数等)。.env 里始终是当前用的那一个,这里记着用过的全部,
|
|
3
3
|
// /model 可以切回去。
|
|
4
4
|
|
|
5
5
|
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
@@ -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"];
|
|
11
|
+
export const MODEL_SETTINGS = ["JADE_MAX_TOKENS", "JADE_EFFORT", "JADE_CONTEXT_WINDOW", "JADE_RPM", "JADE_VISION", "OPENAI_COMPAT_EXTRA_BODY"];
|
|
12
12
|
|
|
13
13
|
export interface SavedModel {
|
|
14
14
|
name: string;
|
|
@@ -34,11 +34,15 @@ export async function readSavedServices(file: string): Promise<SavedService[]> {
|
|
|
34
34
|
return data.services.flatMap((raw: unknown) => {
|
|
35
35
|
const s = raw as { env?: unknown; models?: unknown } | null;
|
|
36
36
|
if (!s || !Array.isArray(s.models)) return [];
|
|
37
|
+
// 以前额外请求参数存在服务上:分给它下面没有自己那份的模型
|
|
38
|
+
const { OPENAI_COMPAT_EXTRA_BODY: extraBody, ...env } = strings(s.env);
|
|
37
39
|
const models = s.models.flatMap((m: unknown) => {
|
|
38
40
|
const name = (m as { name?: unknown } | null)?.name;
|
|
39
|
-
|
|
41
|
+
const settings = strings((m as { settings?: unknown }).settings);
|
|
42
|
+
if (extraBody && settings.OPENAI_COMPAT_EXTRA_BODY === undefined) settings.OPENAI_COMPAT_EXTRA_BODY = extraBody;
|
|
43
|
+
return typeof name === "string" && name ? [{ name, settings }] : [];
|
|
40
44
|
});
|
|
41
|
-
return models.length ? [{ env
|
|
45
|
+
return models.length ? [{ env, models }] : [];
|
|
42
46
|
});
|
|
43
47
|
} catch {
|
|
44
48
|
return [];
|
package/plugins/modes/index.ts
CHANGED
|
@@ -10,23 +10,8 @@
|
|
|
10
10
|
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
11
11
|
import path from "node:path";
|
|
12
12
|
import type { Tool } from "uncarved";
|
|
13
|
-
import { definePlugin, type UI } from "
|
|
14
|
-
import type { Approval } from "
|
|
15
|
-
import type { Commands } from "../commands/index.ts";
|
|
16
|
-
import type { Keys } from "../keys/index.ts";
|
|
17
|
-
import type { Prompts } from "../prompts/index.ts";
|
|
18
|
-
|
|
19
|
-
/** modes@1 */
|
|
20
|
-
export interface Modes {
|
|
21
|
-
current(): string;
|
|
22
|
-
list(): { name: string; description: string }[];
|
|
23
|
-
/** 换模式,从下一个工具调用起生效 */
|
|
24
|
-
set(mode: string): void;
|
|
25
|
-
/** 换到轮换顺序里的下一个 */
|
|
26
|
-
cycle(): string;
|
|
27
|
-
/** 模式变化时调用,返回取消订阅的函数;调用方关闭时自动取消 */
|
|
28
|
-
subscribe(fn: (mode: string) => void): () => void;
|
|
29
|
-
}
|
|
13
|
+
import { definePlugin, type UI } from "@uncarved/jade-api";
|
|
14
|
+
import type { Approval, Commands, Keys, Modes, Prompts } from "@uncarved/jade-api/services";
|
|
30
15
|
|
|
31
16
|
export const DESCRIPTIONS: Record<string, string> = {
|
|
32
17
|
ask: "ask before edits and commands",
|
package/plugins/plugins/index.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
// jade/plugins:/plugins 命令。列出这次启动里每个插件的状态和原因(failed、skipped、conflicted……),
|
|
2
2
|
// 以及每个服务由谁提供。数据来自核心自己的 jade@1 服务,和救援 CLI `jade plugins list` 是同一份状态。
|
|
3
3
|
|
|
4
|
-
import { definePlugin, type Jade } from "
|
|
5
|
-
import type { Commands } from "
|
|
4
|
+
import { definePlugin, type Jade } from "@uncarved/jade-api";
|
|
5
|
+
import type { Commands } from "@uncarved/jade-api/services";
|
|
6
6
|
|
|
7
7
|
export default definePlugin({
|
|
8
8
|
process(ctx) {
|
package/plugins/print/index.ts
CHANGED
|
@@ -1,16 +1,20 @@
|
|
|
1
1
|
// jade/print:jade -p "<prompt>" 跑一轮就退出。模型的回复写到 stdout,工具调用和提示写到 stderr。
|
|
2
2
|
// 不能向用户提问:ui@1 的 select 总是返回 undefined,要审批的调用由审批插件拒绝。
|
|
3
3
|
|
|
4
|
-
import type { Block, LogEvent, RuntimeNotice } from "uncarved";
|
|
5
|
-
import { definePlugin, type Frontend, type UI } from "
|
|
4
|
+
import type { Block, LogEvent, RuntimeNotice, Usage } from "uncarved";
|
|
5
|
+
import { definePlugin, type Frontend, type UI } from "@uncarved/jade-api";
|
|
6
|
+
import type { SessionIndex } from "@uncarved/jade-api/services";
|
|
6
7
|
|
|
7
|
-
const USAGE = `usage: jade -p <prompt> [--resume <session id>]
|
|
8
|
+
const USAGE = `usage: jade -p <prompt> [--resume <session id> | --continue] [--json]
|
|
8
9
|
echo <prompt> | jade -p
|
|
9
10
|
cat <file> | jade -p - <prompt>
|
|
10
11
|
|
|
11
12
|
With "-", what comes in on stdin is added after the prompt (without a prompt, stdin is the prompt).
|
|
12
13
|
|
|
13
14
|
Runs one prompt and exits. The reply goes to stdout; tool calls and notices go to stderr.
|
|
15
|
+
-c, --continue continues the most recent session in this directory.
|
|
16
|
+
--json prints one JSON object on stdout instead of the reply as it is written:
|
|
17
|
+
{"session", "result" (the final reply), "reason" (why the turn ended), "error"?, "usage"}
|
|
14
18
|
Exit code: 0 when the turn ends normally, 1 on an error, 130 when interrupted.
|
|
15
19
|
jade's own commands (plugins, update, --safe): jade help`;
|
|
16
20
|
|
|
@@ -23,6 +27,9 @@ const ui: UI = {
|
|
|
23
27
|
input: async () => undefined,
|
|
24
28
|
};
|
|
25
29
|
|
|
30
|
+
/** --continue 用:process() 里记下的工作目录和会话索引(jade/sessions 的 sessions@1,用到的时候取) */
|
|
31
|
+
let here: { cwd: string; index(): SessionIndex | undefined } | undefined;
|
|
32
|
+
|
|
26
33
|
const frontend: Frontend = {
|
|
27
34
|
async run({ argv, sessions }) {
|
|
28
35
|
const args = parse(argv);
|
|
@@ -42,6 +49,14 @@ const frontend: Frontend = {
|
|
|
42
49
|
return 2;
|
|
43
50
|
}
|
|
44
51
|
|
|
52
|
+
if (args.cont) {
|
|
53
|
+
const latest = (await here?.index()?.list(here.cwd))?.[0]?.id;
|
|
54
|
+
if (!latest) {
|
|
55
|
+
process.stderr.write("jade: there is no earlier session in this directory to continue\n");
|
|
56
|
+
return 2;
|
|
57
|
+
}
|
|
58
|
+
args.resume = latest;
|
|
59
|
+
}
|
|
45
60
|
let session: Awaited<ReturnType<typeof sessions.open>>;
|
|
46
61
|
try {
|
|
47
62
|
session = await sessions.open(args.resume ? { resume: args.resume } : {});
|
|
@@ -52,7 +67,11 @@ const frontend: Frontend = {
|
|
|
52
67
|
}
|
|
53
68
|
let ended: { reason: string; error?: string } | undefined;
|
|
54
69
|
let midLine = false;
|
|
70
|
+
/** --json:回复不边写边打印,最后一次回复的正文和用量合计在结束时一起输出 */
|
|
71
|
+
let result = "";
|
|
72
|
+
const used: Required<Usage> = { inputTokens: 0, outputTokens: 0, cacheReadTokens: 0, cacheWriteTokens: 0 };
|
|
55
73
|
const out = (text: string) => {
|
|
74
|
+
if (args.json) return;
|
|
56
75
|
process.stdout.write(text);
|
|
57
76
|
midLine = !text.endsWith("\n");
|
|
58
77
|
};
|
|
@@ -66,7 +85,10 @@ const frontend: Frontend = {
|
|
|
66
85
|
if (n.type !== "event") return;
|
|
67
86
|
const e = n.event as LogEvent;
|
|
68
87
|
if (e.type === "model.responded") {
|
|
69
|
-
const content =
|
|
88
|
+
const { content, usage } = e.data as { content: Block[]; usage?: Usage };
|
|
89
|
+
const text = content.flatMap((b) => (b.kind === "text" ? [b.text] : [])).join("\n");
|
|
90
|
+
if (text) result = text;
|
|
91
|
+
for (const k of Object.keys(used) as (keyof Usage)[]) used[k] += usage?.[k] ?? 0;
|
|
70
92
|
if (!streamed) for (const b of content) if (b.kind === "text") out(b.text);
|
|
71
93
|
streamed = false;
|
|
72
94
|
if (midLine) out("\n");
|
|
@@ -99,6 +121,10 @@ const frontend: Frontend = {
|
|
|
99
121
|
if (midLine) out("\n");
|
|
100
122
|
process.stderr.write(`session ${session.id}\n`);
|
|
101
123
|
const end = ended as { reason: string; error?: string } | undefined;
|
|
124
|
+
if (args.json) {
|
|
125
|
+
const error = failure !== undefined ? errorText(failure) : end?.error;
|
|
126
|
+
process.stdout.write(`${JSON.stringify({ session: session.id, result, reason: end?.reason ?? "error", ...(error ? { error } : {}), usage: used })}\n`);
|
|
127
|
+
}
|
|
102
128
|
if (end?.reason === "aborted") return 130;
|
|
103
129
|
if (failure !== undefined || end?.reason === "error") {
|
|
104
130
|
process.stderr.write(`error: ${failure !== undefined ? errorText(failure) : (end?.error ?? "the turn failed")}\n`);
|
|
@@ -110,19 +136,22 @@ const frontend: Frontend = {
|
|
|
110
136
|
|
|
111
137
|
export default definePlugin({
|
|
112
138
|
process(ctx) {
|
|
139
|
+
here = { cwd: ctx.cwd, index: () => ctx.service<SessionIndex>("sessions@1") };
|
|
113
140
|
ctx.provide("ui@1", ui);
|
|
114
141
|
ctx.provide("frontend@1", frontend);
|
|
115
142
|
},
|
|
116
143
|
});
|
|
117
144
|
|
|
118
|
-
function parse(argv: readonly string[]): { prompt?: string; resume?: string; help?: boolean; stdin?: boolean } | string {
|
|
119
|
-
const out: { prompt?: string; resume?: string; help?: boolean; stdin?: boolean } = {};
|
|
145
|
+
function parse(argv: readonly string[]): { prompt?: string; resume?: string; cont?: boolean; json?: boolean; help?: boolean; stdin?: boolean } | string {
|
|
146
|
+
const out: { prompt?: string; resume?: string; cont?: boolean; json?: boolean; help?: boolean; stdin?: boolean } = {};
|
|
120
147
|
const words: string[] = [];
|
|
121
148
|
for (let i = 0; i < argv.length; i++) {
|
|
122
149
|
const a = argv[i]!;
|
|
123
150
|
if (a === "-p" || a === "--print") continue;
|
|
124
151
|
if (a === "-h" || a === "--help") out.help = true;
|
|
125
152
|
else if (a === "-") out.stdin = true;
|
|
153
|
+
else if (a === "-c" || a === "--continue") out.cont = true;
|
|
154
|
+
else if (a === "--json") out.json = true;
|
|
126
155
|
else if (a === "--resume") {
|
|
127
156
|
const id = argv[++i];
|
|
128
157
|
if (!id) return "--resume needs a session id";
|
|
@@ -130,6 +159,7 @@ function parse(argv: readonly string[]): { prompt?: string; resume?: string; hel
|
|
|
130
159
|
} else if (a.startsWith("-")) return `unknown option ${a}`;
|
|
131
160
|
else words.push(a);
|
|
132
161
|
}
|
|
162
|
+
if (out.cont && out.resume) return "--continue and --resume both pick the session; use one of them";
|
|
133
163
|
if (words.length > 0) out.prompt = words.join(" ");
|
|
134
164
|
return out;
|
|
135
165
|
}
|
package/plugins/prompts/index.ts
CHANGED
|
@@ -1,23 +1,16 @@
|
|
|
1
1
|
// jade/prompts:在 context.build 里生成系统提示词。其他插件经 prompts@1 往里加段落。
|
|
2
|
-
// 也负责内核不写的、给模型看的说明:内容为空的错误工具结果在投影里补一句原因(results.ts
|
|
2
|
+
// 也负责内核不写的、给模型看的说明:内容为空的错误工具结果在投影里补一句原因(results.ts);
|
|
3
|
+
// 其他插件经 prompts@1 的 note 给模型留一句说明(notes.ts)。
|
|
3
4
|
|
|
5
|
+
import os from "node:os";
|
|
4
6
|
import path from "node:path";
|
|
5
7
|
import type { ContentSource, LogEvent, SystemSegment } from "uncarved";
|
|
6
|
-
import { definePlugin, type UI } from "
|
|
7
|
-
import type { Models } from "
|
|
8
|
+
import { definePlugin, type UI } from "@uncarved/jade-api";
|
|
9
|
+
import type { Models, PromptInstructions, Prompts } from "@uncarved/jade-api/services";
|
|
10
|
+
import { Notes } from "./notes.ts";
|
|
8
11
|
import { allInstructions, environment, mainPrompt, realPath } from "./prompts.ts";
|
|
9
12
|
import { explainEmptyResults, whyEmpty, type Reasons, type Why } from "./results.ts";
|
|
10
13
|
|
|
11
|
-
/** prompts@1 */
|
|
12
|
-
export interface Prompts {
|
|
13
|
-
/**
|
|
14
|
-
* 往系统提示词里加一段,排在主体、环境信息和指令文件之后,返回撤销函数;调用方的作用域关闭时自动撤销。
|
|
15
|
-
* stable(默认 true)的段落排在 volatile 的前面:会变的段落放在后面,前面的才能命中提示缓存。
|
|
16
|
-
* 项目层插件加的段落标为不可信;untrusted 为 true 时也标为不可信(内容来自外部,例如 MCP 服务的说明、仓库里的 skill)
|
|
17
|
-
*/
|
|
18
|
-
add(section: { text: string; stable?: boolean; untrusted?: boolean }): () => void;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
14
|
interface Section {
|
|
22
15
|
plugin: string;
|
|
23
16
|
text: string;
|
|
@@ -29,6 +22,10 @@ const ID = "jade/prompts";
|
|
|
29
22
|
|
|
30
23
|
/** 其他插件加的段落。模块每个进程只加载一次,进程级和会话级共用 */
|
|
31
24
|
const sections: Section[] = [];
|
|
25
|
+
/** 打开着的会话 → 它读到的指令文件(prompts@1.instructions) */
|
|
26
|
+
const loaded = new Map<string, PromptInstructions[]>();
|
|
27
|
+
/** 其他插件留给模型的说明 */
|
|
28
|
+
const notes = new Notes();
|
|
32
29
|
|
|
33
30
|
function segment(text: string, trust: SystemSegment["trust"] = "trusted", stability: SystemSegment["stability"] = "stable", by = ID, source?: ContentSource): SystemSegment {
|
|
34
31
|
return { text, stability, origin: { by: "plugin", id: by, ...(source ? { source } : {}) }, trust };
|
|
@@ -55,12 +52,20 @@ export default definePlugin({
|
|
|
55
52
|
caller.onDispose(remove);
|
|
56
53
|
return remove;
|
|
57
54
|
},
|
|
55
|
+
note(session, text) {
|
|
56
|
+
if (typeof session !== "string" || !session) throw new Error("prompts.note: session must be a session id");
|
|
57
|
+
if (typeof text !== "string" || !text) throw new Error("prompts.note: text must be a non-empty string");
|
|
58
|
+
notes.add(session, { plugin: caller.plugin, text, trusted: caller.layer !== "project" });
|
|
59
|
+
},
|
|
60
|
+
instructions: (session) => (loaded.get(session) ?? []).map((i) => ({ ...i, source: { ...i.source } })),
|
|
58
61
|
}));
|
|
59
62
|
},
|
|
60
63
|
|
|
61
64
|
async session(ctx) {
|
|
62
65
|
const models = ctx.service<Models>("models@1");
|
|
63
|
-
const [env, instructions] = await Promise.all([environment(ctx.cwd), allInstructions(ctx.home, ctx.cwd)]);
|
|
66
|
+
const [env, instructions] = await Promise.all([environment(ctx.cwd), allInstructions(ctx.home, ctx.cwd, ctx.config.claudeInstructions === false ? null : os.homedir())]);
|
|
67
|
+
loaded.set(ctx.session.id, instructions.map(({ text, trust, source }) => ({ text, trust, source })));
|
|
68
|
+
await ctx.effect(() => () => void loaded.delete(ctx.session.id));
|
|
64
69
|
const fixed = [segment(env), ...instructions.map((i) => segment(i.text, i.trust, "stable", ID, i.source))];
|
|
65
70
|
// 告诉用户读了哪些指令文件,否则看不出它们生效了
|
|
66
71
|
if (instructions.length > 0) ctx.service<UI>("ui@1")?.notify(`loaded instructions: ${instructions.map((i) => display(realPath(ctx.cwd), i.file)).join(", ")}`);
|
|
@@ -91,6 +96,9 @@ export default definePlugin({
|
|
|
91
96
|
fn: (messages, hook) => explainEmptyResults(messages, { ...(hook.derived[reasonsKey] as Reasons | undefined), ...reasons }, ID),
|
|
92
97
|
});
|
|
93
98
|
|
|
99
|
+
ctx.kernel.hook(notes.hook(ctx.session.id, `${ID}/notes`));
|
|
100
|
+
await ctx.effect(() => () => notes.drop(ctx.session.id));
|
|
101
|
+
|
|
94
102
|
ctx.kernel.hook({
|
|
95
103
|
kind: "transform",
|
|
96
104
|
point: "context.build",
|
|
@@ -1,23 +1,35 @@
|
|
|
1
|
-
// 给模型的说明([Note from jade]
|
|
2
|
-
//
|
|
1
|
+
// 给模型的说明([Note from jade],prompts@1 的 note):命令里做的事(例如用户删掉了一条记忆、撤销了模型改的文件)
|
|
2
|
+
// 要让模型知道,但命令拿不到会话日志,也不该替用户发一条消息。所以先记下来,下一次请求模型时在 context.project 里
|
|
3
3
|
// 附在最后一条消息后面,并用 ctx.record 写进日志(derived):之后的投影、恢复的会话里它都在同一个位置。
|
|
4
|
-
// 插件之间不共享代码,jade/undo 里也有一份。
|
|
5
4
|
|
|
6
5
|
import type { Block, LocalHookSpec, Message } from "uncarved";
|
|
7
6
|
|
|
7
|
+
export interface Note {
|
|
8
|
+
/** 留下这条说明的插件 */
|
|
9
|
+
plugin: string;
|
|
10
|
+
text: string;
|
|
11
|
+
/** 项目层插件留的说明不可信 */
|
|
12
|
+
trusted: boolean;
|
|
13
|
+
}
|
|
14
|
+
|
|
8
15
|
/** derived 里:消息的下标 → 附在它后面的说明 */
|
|
9
|
-
type NotesState = Record<string,
|
|
16
|
+
type NotesState = Record<string, Note[]>;
|
|
10
17
|
|
|
11
18
|
export class Notes {
|
|
12
19
|
/** 会话 → 还没交给模型的说明 */
|
|
13
|
-
readonly #pending = new Map<string,
|
|
20
|
+
readonly #pending = new Map<string, Note[]>();
|
|
21
|
+
|
|
22
|
+
add(sessionId: string, note: Note): void {
|
|
23
|
+
this.#pending.set(sessionId, [...(this.#pending.get(sessionId) ?? []), note]);
|
|
24
|
+
}
|
|
14
25
|
|
|
15
|
-
|
|
16
|
-
|
|
26
|
+
/** 会话关闭:还没交给模型的说明不再留着 */
|
|
27
|
+
drop(sessionId: string): void {
|
|
28
|
+
this.#pending.delete(sessionId);
|
|
17
29
|
}
|
|
18
30
|
|
|
19
31
|
/** 会话里的 context.project hook;key 是 derived 里的键 */
|
|
20
|
-
hook(sessionId: string,
|
|
32
|
+
hook(sessionId: string, key: string): LocalHookSpec {
|
|
21
33
|
return {
|
|
22
34
|
kind: "transform",
|
|
23
35
|
point: "context.project",
|
|
@@ -34,7 +46,7 @@ export class Notes {
|
|
|
34
46
|
return messages.map((m, i): Message => {
|
|
35
47
|
const notes = state[i];
|
|
36
48
|
if (!notes?.length || m.role !== "user") return m;
|
|
37
|
-
const blocks: Block[] = notes.map((
|
|
49
|
+
const blocks: Block[] = notes.map((n) => ({ kind: "text", text: `[Note from jade] ${n.text}`, origin: { by: "plugin", id: n.plugin }, trust: n.trusted ? "trusted" : "untrusted" }));
|
|
38
50
|
return { ...m, content: [...m.content, ...blocks] };
|
|
39
51
|
});
|
|
40
52
|
},
|
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"id": "jade/prompts",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"api": "^1",
|
|
5
5
|
"entry": "./index.ts",
|
|
6
6
|
"provides": ["prompts@1"],
|
|
7
|
-
"optional": ["models@1", "ui@1"]
|
|
7
|
+
"optional": ["models@1", "ui@1"],
|
|
8
|
+
"config": {
|
|
9
|
+
"claudeInstructions": {
|
|
10
|
+
"default": true,
|
|
11
|
+
"description": "when $JADE_HOME has no AGENTS.md or CLAUDE.md, use ~/.claude/CLAUDE.md as the user instructions; false turns this off"
|
|
12
|
+
}
|
|
13
|
+
}
|
|
8
14
|
}
|
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
// 主体和环境信息都是 stable:环境信息在会话开始时取一次,不在每次请求时更新,
|
|
4
4
|
// 否则它一变,排在后面的整段历史都无法命中提示缓存。
|
|
5
5
|
// 指令文件兼容 AGENTS.md 和 CLAUDE.md,同一目录两者都有时只读 AGENTS.md。
|
|
6
|
-
//
|
|
6
|
+
// 用户层的先找 $JADE_HOME,没有时用 ~/.claude 里的(和 jade/skills 读 ~/.claude/skills 一样,已有的全局指令不用再抄一份)。
|
|
7
|
+
// 用户层的是 trusted;项目层(随仓库分发)的是 untrusted,并记下出处(宪法 7.2)。
|
|
7
8
|
// 项目层从仓库根目录到工作目录逐级查找,上级的在前,离工作目录近的在后。
|
|
8
9
|
|
|
9
10
|
import { createHash } from "node:crypto";
|
|
@@ -110,10 +111,10 @@ export interface Instructions {
|
|
|
110
111
|
file: string;
|
|
111
112
|
}
|
|
112
113
|
|
|
113
|
-
/** 用户层(home
|
|
114
|
-
export async function allInstructions(home: string, cwd: string): Promise<Instructions[]> {
|
|
114
|
+
/** 用户层(home 里,没有时 userHome/.claude 里;userHome 给 null 时不找)和项目层(仓库根目录到 cwd 逐级)的指令文件,按顺序 */
|
|
115
|
+
export async function allInstructions(home: string, cwd: string, userHome: string | null = os.homedir()): Promise<Instructions[]> {
|
|
115
116
|
const out: Instructions[] = [];
|
|
116
|
-
const user = await instructions(home, home, "user");
|
|
117
|
+
const user = (await instructions(home, home, "user")) ?? (userHome === null ? undefined : await instructions(userHome, path.join(userHome, ".claude"), "user"));
|
|
117
118
|
if (user) out.push({ text: `# User instructions (${user.source.path})\n\n${user.text}`, trust: "trusted", source: user.source, file: user.file });
|
|
118
119
|
const root = realPath(await repoRoot(cwd));
|
|
119
120
|
for (const dir of ancestry(root, realPath(cwd))) {
|
|
@@ -1,49 +1,8 @@
|
|
|
1
1
|
// jade/renderers:工具调用和插件事件在对话里怎么显示的注册表。前端查这里;没有登记的用前端的默认显示。
|
|
2
2
|
// 显示的文字可以带 ANSI 颜色,前端负责过滤掉颜色之外的控制序列。
|
|
3
3
|
|
|
4
|
-
import { definePlugin } from "
|
|
5
|
-
|
|
6
|
-
export interface ToolRenderer {
|
|
7
|
-
tool: string;
|
|
8
|
-
/** 调用行 "● 工具名(…)" 括号里的摘要,只显示第一行;默认是 path 参数或参数的 JSON */
|
|
9
|
-
summary?(input: unknown): string | undefined;
|
|
10
|
-
/** 摘要省略了的参数(例如很长的命令),展开调用行后显示在最前面,可以多行 */
|
|
11
|
-
details?(input: unknown): string | undefined;
|
|
12
|
-
/** 执行时显示的内容,代替默认的 "工具名(摘要)":第一行跟在 "● " 后面,其余的行原样显示在下面,不折叠 */
|
|
13
|
-
call?(input: unknown): string | undefined;
|
|
14
|
-
/**
|
|
15
|
-
* 运行中的进度:工具用 ToolContext.progress 推送的一项换成一行,显示在对话的最后,工具结束时去掉;返回 undefined 不显示。
|
|
16
|
-
* 没有这个函数时,{ text } 形式的进度按输出逐行显示,其他的不显示
|
|
17
|
-
*/
|
|
18
|
-
progress?(update: unknown, call: { toolUseId: string; summary(tool: string, input: unknown): string }): string | undefined;
|
|
19
|
-
/** 请用户批准时显示的预览,可以多行;默认是参数的 JSON */
|
|
20
|
-
preview?(input: unknown): string | undefined | Promise<string | undefined>;
|
|
21
|
-
/** 工具执行之前同步调用(例如记下修改前的文件,好在结果里显示 diff),返回值交给 result 的 before */
|
|
22
|
-
before?(input: unknown): unknown;
|
|
23
|
-
/**
|
|
24
|
-
* 结果:line 接在调用行的 "⎿ " 后面(一行),details 是展开后的内容(可以多行);
|
|
25
|
-
* 返回 null 时不显示结果;默认显示结果文字的第一行,多出的行折叠起来
|
|
26
|
-
*/
|
|
27
|
-
result?(r: { toolUseId: string; input: unknown; text: string; isError: boolean; before?: unknown }): { line: string; details?: string } | null | undefined;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/** 插件自己的事件(hook 的 ctx.emit 写入的)怎么显示。只能为自己的事件注册:event 不带 "<插件 id>/" 前缀 */
|
|
31
|
-
export interface EventRenderer {
|
|
32
|
-
event: string;
|
|
33
|
-
/** line 显示成一行,details 是展开后的内容;返回 undefined 时不显示 */
|
|
34
|
-
render(data: unknown): { line: string; details?: string } | undefined;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export interface Renderers {
|
|
38
|
-
/** 为一个工具登记显示方式,返回撤销函数。同一个工具已有登记时抛错 */
|
|
39
|
-
tool(renderer: ToolRenderer): () => void;
|
|
40
|
-
/** 为自己的一种事件登记显示方式,返回撤销函数 */
|
|
41
|
-
event(renderer: EventRenderer): () => void;
|
|
42
|
-
/** 前端查找 */
|
|
43
|
-
forTool(tool: string): { renderer: ToolRenderer; plugin: string } | undefined;
|
|
44
|
-
/** 前端按完整的事件类型("<插件 id>/<event>")查找 */
|
|
45
|
-
forEvent(type: string): { renderer: EventRenderer; plugin: string } | undefined;
|
|
46
|
-
}
|
|
4
|
+
import { definePlugin } from "@uncarved/jade-api";
|
|
5
|
+
import type { EventRenderer, Renderers, ToolRenderer } from "@uncarved/jade-api/services";
|
|
47
6
|
|
|
48
7
|
const tools = new Map<string, { renderer: ToolRenderer; plugin: string }>();
|
|
49
8
|
const events = new Map<string, { renderer: EventRenderer; plugin: string }>();
|