@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
|
@@ -1,104 +1,107 @@
|
|
|
1
|
-
// jade/compact:历史接近上下文窗口时压缩投影(见 compact.ts);/compact 让下一次请求前总结一次;
|
|
2
|
-
// 请求因为超出上下文窗口失败时,压缩后用 { kind: "retry" } 重试一次(session.afterTurn,设计 4.3)。
|
|
3
1
|
import { definePlugin } from "@uncarved/jade-api";
|
|
4
2
|
import { compactor, compactThreshold, isContextOverflow, parseTokens } from "./compact.js";
|
|
5
3
|
import { dim } from "./style.js";
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
const ANTHROPIC_REPLY_TOKENS = 64_000;
|
|
10
|
-
/** 总结要调用模型,比 hook 默认的 30 秒长 */
|
|
11
|
-
const SUMMARY_TIMEOUT_MS = 300_000;
|
|
12
|
-
/** 自动压缩的阈值:配置的 thresholdTokens,否则按当前模型的上下文窗口算 */
|
|
4
|
+
const FALLBACK_THRESHOLD = 120000;
|
|
5
|
+
const ANTHROPIC_REPLY_TOKENS = 64000;
|
|
6
|
+
const SUMMARY_TIMEOUT_MS = 300000;
|
|
13
7
|
function thresholdFor(config, models) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
};
|
|
8
|
+
const configured = parseTokens(config.thresholdTokens);
|
|
9
|
+
return () => {
|
|
10
|
+
if (configured)
|
|
11
|
+
return configured;
|
|
12
|
+
const m = models.current();
|
|
13
|
+
if (!m || !(typeof m.contextWindow === "number" && m.contextWindow > 0))
|
|
14
|
+
return FALLBACK_THRESHOLD;
|
|
15
|
+
return compactThreshold(m.contextWindow, m.kind === "anthropic" ? ANTHROPIC_REPLY_TOKENS : 0);
|
|
16
|
+
};
|
|
24
17
|
}
|
|
25
|
-
|
|
26
|
-
const requested = new Map();
|
|
18
|
+
const requested = new Map;
|
|
27
19
|
export default definePlugin({
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
},
|
|
51
|
-
});
|
|
52
|
-
renderers.event({ event: "trimmed", render: (data) => ({ line: dim(`[cleared old tool output, ~${data.saved} tokens]`) }) });
|
|
20
|
+
process(ctx) {
|
|
21
|
+
ctx.provide("compact@1", { threshold: thresholdFor(ctx.config, ctx.require("models@1")) });
|
|
22
|
+
const commands = ctx.service("commands@1");
|
|
23
|
+
commands?.register({
|
|
24
|
+
name: "compact",
|
|
25
|
+
args: "[what to keep]",
|
|
26
|
+
description: "compact the history before the next request; text after the command tells the summary what to pay attention to",
|
|
27
|
+
run(args, c) {
|
|
28
|
+
const s = c.sessions.current;
|
|
29
|
+
if (!s)
|
|
30
|
+
return "No session is open.";
|
|
31
|
+
requested.set(s.id, args.trim());
|
|
32
|
+
return dim("History will be compacted before the next request.");
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
const renderers = ctx.service("renderers@1");
|
|
36
|
+
if (renderers) {
|
|
37
|
+
renderers.event({
|
|
38
|
+
event: "compacted",
|
|
39
|
+
render: (data) => {
|
|
40
|
+
const d = data;
|
|
41
|
+
return { line: dim(`[compacted ${d.upTo - d.from} messages, ~${d.before} tokens before]`) };
|
|
53
42
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
43
|
+
});
|
|
44
|
+
renderers.event({ event: "trimmed", render: (data) => ({ line: dim(`[cleared old tool output, ~${data.saved} tokens]`) }) });
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
session(ctx) {
|
|
48
|
+
if (ctx.session.parent)
|
|
49
|
+
return;
|
|
50
|
+
const models = ctx.require("models@1");
|
|
51
|
+
const configured = parseTokens(ctx.config.thresholdTokens);
|
|
52
|
+
const keepTokens = parseTokens(ctx.config.keepTokens);
|
|
53
|
+
const threshold = thresholdFor(ctx.config, models);
|
|
54
|
+
const c = compactor({ complete: (req, o) => models.complete(req, o), threshold, keepTokens, id: ctx.plugin, writes: (t) => ctx.session.tool(t)?.meta?.writes });
|
|
55
|
+
const id = ctx.session.id;
|
|
56
|
+
const show = (projected) => {
|
|
57
|
+
const at = threshold();
|
|
58
|
+
if (!at)
|
|
59
|
+
return ctx.status?.(undefined);
|
|
60
|
+
ctx.status?.({ text: `compacts at ${tokens(at)}`, ...projected !== undefined && projected >= at * 0.85 ? { tone: "warn" } : {} });
|
|
61
|
+
};
|
|
62
|
+
show();
|
|
63
|
+
const offModel = models.subscribe?.(() => show());
|
|
64
|
+
if (offModel)
|
|
65
|
+
ctx.effect(() => offModel);
|
|
66
|
+
ctx.kernel.hook({
|
|
67
|
+
kind: "transform",
|
|
68
|
+
point: "context.project",
|
|
69
|
+
order: 100,
|
|
70
|
+
timeoutMs: SUMMARY_TIMEOUT_MS,
|
|
71
|
+
fn: async (messages, hook) => {
|
|
72
|
+
const focus = requested.get(id);
|
|
73
|
+
if (focus !== undefined) {
|
|
74
|
+
requested.delete(id);
|
|
75
|
+
c.request(focus || undefined);
|
|
76
|
+
}
|
|
77
|
+
const out = await c.project(messages, hook);
|
|
78
|
+
show(c.projected());
|
|
79
|
+
return out;
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
let retried = false;
|
|
83
|
+
ctx.kernel.hook({
|
|
84
|
+
kind: "observe",
|
|
85
|
+
point: "event",
|
|
86
|
+
fn: (e) => {
|
|
87
|
+
c.observe(e);
|
|
88
|
+
if (e.type === "input.received" && e.data.kind === "message")
|
|
89
|
+
retried = false;
|
|
90
|
+
if (e.type !== "turn.ended")
|
|
91
|
+
return;
|
|
92
|
+
const d = e.data;
|
|
93
|
+
if (d.reason !== "error" || retried || !isContextOverflow(d.error))
|
|
94
|
+
return;
|
|
95
|
+
retried = true;
|
|
96
|
+
c.request();
|
|
97
|
+
const ui = ctx.service("ui@1");
|
|
98
|
+
const window = models.current()?.contextWindow;
|
|
99
|
+
ui?.notify("the request exceeded the model's context window; compacting the history and retrying" + (configured || !window ? "" : ` (jade assumed a ${window}-token window; set the real one with JADE_CONTEXT_WINDOW)`), "warning");
|
|
100
|
+
ctx.session.afterTurn(() => {
|
|
101
|
+
ctx.session.send({ kind: "retry" }).catch((err) => ctx.signal.aborted || ui?.notify(`retry after compaction failed: ${err instanceof Error ? err.message : String(err)}`, "error"));
|
|
102
102
|
});
|
|
103
|
-
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
}
|
|
104
106
|
});
|
|
107
|
+
const tokens = (n) => n >= 1e6 ? `${(n / 1e6).toFixed(n >= 1e7 ? 0 : 1)}M` : n >= 1000 ? `${(n / 1000).toFixed(n >= 1e4 ? 0 : 1)}k` : String(n);
|
|
@@ -1,14 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
export const dim = paint("2", "22");
|
|
11
|
-
export const red = paint("31", "39");
|
|
12
|
-
export const green = paint("32", "39");
|
|
13
|
-
export const yellow = paint("33", "39");
|
|
14
|
-
export const bold = paint("1", "22");
|
|
1
|
+
export const clip = (s, n) => s.length > n ? s.slice(0, n) + "…" : s;
|
|
2
|
+
const styled = (style) => (text) => ({ text, style });
|
|
3
|
+
export const dim = styled("dim");
|
|
4
|
+
export const bold = styled("bold");
|
|
5
|
+
export const ok = styled("ok");
|
|
6
|
+
export const warn = styled("warn");
|
|
7
|
+
export const error = styled("error");
|
|
8
|
+
export const lines = (rows) => rows.flatMap((r, i) => i ? [`
|
|
9
|
+
`, r] : [r]);
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { definePlugin } from "@uncarved/jade-api";
|
|
2
|
+
const registry = new Map;
|
|
3
|
+
export default definePlugin({
|
|
4
|
+
process(ctx) {
|
|
5
|
+
ctx.provide("completions@1", (caller) => ({
|
|
6
|
+
register(completion) {
|
|
7
|
+
const trigger = completion?.trigger;
|
|
8
|
+
if (typeof trigger !== "string" || [...trigger].length !== 1 || /[\p{L}\p{N}\s/]/u.test(trigger)) {
|
|
9
|
+
throw new Error(`invalid completion trigger ${JSON.stringify(trigger)}: use one character that is not a letter, digit, space or "/"`);
|
|
10
|
+
}
|
|
11
|
+
if (typeof completion.candidates !== "function")
|
|
12
|
+
throw new Error(`completion ${trigger} has no candidates function`);
|
|
13
|
+
const taken = registry.get(trigger);
|
|
14
|
+
if (taken) {
|
|
15
|
+
ctx.report(`completion ${trigger} from ${caller.plugin} was skipped: ${taken.plugin} already registered it`);
|
|
16
|
+
return () => {};
|
|
17
|
+
}
|
|
18
|
+
const entry = { completion, plugin: caller.plugin };
|
|
19
|
+
registry.set(trigger, entry);
|
|
20
|
+
const revoke = () => {
|
|
21
|
+
if (registry.get(trigger) === entry)
|
|
22
|
+
registry.delete(trigger);
|
|
23
|
+
};
|
|
24
|
+
caller.onDispose(revoke);
|
|
25
|
+
return revoke;
|
|
26
|
+
},
|
|
27
|
+
list: () => [...registry.values()]
|
|
28
|
+
}));
|
|
29
|
+
}
|
|
30
|
+
});
|
|
@@ -1,57 +1,45 @@
|
|
|
1
|
-
// jade/continue:模型的回复被输出上限截断(这一轮以 max_tokens 结束)时,让模型接着做,而不是把半截任务丢给用户。
|
|
2
|
-
//
|
|
3
|
-
// 从 turn.ended 事件知道这一轮怎么结束的(observe event),在 session.afterTurn 里用 session.send 发一条 [Note from jade]。
|
|
4
|
-
// 每次用户输入之后最多续写 maxContinues 次。从旧 jade 的 plugins/turns.ts 的 continueTurns 移植。
|
|
5
|
-
//
|
|
6
|
-
// 旧 jade 的"上下文溢出后压缩重试"不在这里:不压缩就重试只会再溢出一次,整件事归 jade/compact。
|
|
7
1
|
import { definePlugin } from "@uncarved/jade-api";
|
|
8
2
|
export const DEFAULT_CONTINUES = 3;
|
|
9
|
-
export const CONTINUE_NOTE = "Your previous reply was cut off because it reached the output token limit. Continue from where you stopped. " +
|
|
10
|
-
"If you were writing a large file or edit, split it into several smaller write_file/edit_file calls.";
|
|
3
|
+
export const CONTINUE_NOTE = "Your previous reply was cut off because it reached the output token limit. Continue from where you stopped. " + "If you were writing a large file or edit, split it into several smaller write_file/edit_file calls.";
|
|
11
4
|
export default definePlugin({
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
ctx.session.afterTurn(() => {
|
|
49
|
-
ui?.notify(`[asking the model to continue (${n}/${max})]`);
|
|
50
|
-
pending = note();
|
|
51
|
-
// 不能 await:afterTurn 跑完之后会话才处理排队的输入。出错时前端从 turn.ended 显示原因
|
|
52
|
-
ctx.session.send({ kind: "message", content: [pending] }).catch(() => { });
|
|
53
|
-
});
|
|
54
|
-
},
|
|
5
|
+
session(ctx) {
|
|
6
|
+
const ui = ctx.service("ui@1");
|
|
7
|
+
const raw = ctx.config.maxContinues;
|
|
8
|
+
let max = DEFAULT_CONTINUES;
|
|
9
|
+
if (typeof raw === "number" && Number.isInteger(raw) && raw >= 0)
|
|
10
|
+
max = raw;
|
|
11
|
+
else if (raw !== undefined)
|
|
12
|
+
ui?.notify(`jade/continue: maxContinues must be a non-negative integer, got ${JSON.stringify(raw)}; using ${DEFAULT_CONTINUES}`, "warning");
|
|
13
|
+
let count = 0;
|
|
14
|
+
let pending;
|
|
15
|
+
const note = () => ({ kind: "text", text: `[Note from jade] ${CONTINUE_NOTE}`, origin: { by: "plugin", id: ctx.plugin }, trust: "trusted" });
|
|
16
|
+
ctx.kernel.hook({
|
|
17
|
+
kind: "observe",
|
|
18
|
+
point: "event",
|
|
19
|
+
fn: (e) => {
|
|
20
|
+
if (e.type === "input.received") {
|
|
21
|
+
const input = e.data;
|
|
22
|
+
if (input.kind !== "message")
|
|
23
|
+
return;
|
|
24
|
+
const ours = pending !== undefined && input.content.length === 1 && input.content[0].kind === "text" && input.content[0].origin.by === "plugin" && input.content[0].origin.id === ctx.plugin;
|
|
25
|
+
if (ours)
|
|
26
|
+
pending = undefined;
|
|
27
|
+
else
|
|
28
|
+
count = 0;
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
if (e.type !== "turn.ended")
|
|
32
|
+
return;
|
|
33
|
+
if (e.data.reason !== "max_tokens" || count >= max)
|
|
34
|
+
return;
|
|
35
|
+
count++;
|
|
36
|
+
const n = count;
|
|
37
|
+
ctx.session.afterTurn(() => {
|
|
38
|
+
ui?.notify(`[asking the model to continue (${n}/${max})]`);
|
|
39
|
+
pending = note();
|
|
40
|
+
ctx.session.send({ kind: "message", content: [pending] }).catch(() => {});
|
|
55
41
|
});
|
|
56
|
-
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
}
|
|
57
45
|
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { definePlugin } from "@uncarved/jade-api";
|
|
2
|
+
const styled = (style) => (text) => ({ text, style });
|
|
3
|
+
const dim = styled("dim");
|
|
4
|
+
const error = styled("error");
|
|
5
|
+
export function lastReply(messages) {
|
|
6
|
+
for (let i = messages.length - 1;i >= 0; i--) {
|
|
7
|
+
const m = messages[i];
|
|
8
|
+
if (m.role !== "assistant")
|
|
9
|
+
continue;
|
|
10
|
+
const text = m.content.flatMap((b) => b.kind === "text" ? [b.text] : []).join(`
|
|
11
|
+
`).trim();
|
|
12
|
+
if (text)
|
|
13
|
+
return text;
|
|
14
|
+
}
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
export function lastCodeBlock(markdown) {
|
|
18
|
+
const blocks = [...markdown.matchAll(/^ {0,3}(`{3,}|~{3,})[^\n]*\n([\s\S]*?)^ {0,3}\1[`~]*[ \t]*$/gm)];
|
|
19
|
+
return blocks.at(-1)?.[2]?.replace(/\n$/, "");
|
|
20
|
+
}
|
|
21
|
+
export default definePlugin({
|
|
22
|
+
process(ctx) {
|
|
23
|
+
ctx.require("commands@1").register({
|
|
24
|
+
name: "copy",
|
|
25
|
+
args: "[code]",
|
|
26
|
+
description: "copy the last reply to the clipboard as it was written (code: only its last code block)",
|
|
27
|
+
instant: true,
|
|
28
|
+
run(args, c) {
|
|
29
|
+
const code = args.trim() === "code";
|
|
30
|
+
const reply = lastReply(c.sessions.current?.state.messages ?? []);
|
|
31
|
+
const text = code ? reply && lastCodeBlock(reply) : reply;
|
|
32
|
+
if (!text)
|
|
33
|
+
return dim(code ? "The last reply has no code block." : "There is no reply to copy yet.");
|
|
34
|
+
if (!c.ui.copy?.(text))
|
|
35
|
+
return error("[error] this front end cannot copy to the clipboard");
|
|
36
|
+
return dim(`[copied ${[...text].length} characters]`);
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
});
|