@uncarved/jade 0.3.0 → 0.4.0
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 +482 -27
- package/dist/session.js.map +1 -1
- package/package.json +4 -4
- package/plugins/approval/index.ts +49 -24
- package/plugins/approval/policy.ts +48 -25
- 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 +55 -12
- 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 +10 -9
- package/plugins/models/index.ts +31 -4
- package/plugins/models/provider.ts +12 -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 +39 -9
- package/plugins/schedule/index.ts +2 -0
- package/plugins/screenshot/browser.ts +231 -0
- package/plugins/screenshot/index.ts +3 -2
- package/plugins/screenshot/tool.ts +27 -51
- package/plugins/sessions/index.ts +48 -2
- package/plugins/shell/index.ts +75 -22
- package/plugins/shell/plugin.json +1 -2
- package/plugins/shell/renderers.ts +9 -7
- package/plugins/shell/shell.ts +93 -34
- 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 +81 -81
- package/plugins/task/plugin.json +5 -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 +57 -0
- package/plugins/tools/plugin.json +7 -0
- package/plugins/tui/frontend.ts +161 -87
- package/plugins/tui/index.ts +2 -3
- package/plugins/tui/mascot.ts +8 -7
- 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 +356 -104
- package/plugins/tui/vendor.ts +1 -1
- package/plugins/undo/checkpoint.ts +5 -3
- package/plugins/undo/index.ts +43 -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 -414
- package/plugins-dist/approval/policy.js +395 -497
- 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 -244
- 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 +392 -473
- package/plugins-dist/models/context.js +14 -22
- package/plugins-dist/models/index.js +182 -188
- package/plugins-dist/models/provider.js +243 -295
- package/plugins-dist/models/saved.js +59 -71
- 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 +81 -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 +134 -151
- package/plugins-dist/schedule/cron.js +49 -56
- package/plugins-dist/schedule/index.js +229 -247
- package/plugins-dist/schedule/store.js +93 -109
- package/plugins-dist/scratch/index.js +12 -18
- package/plugins-dist/screenshot/browser.js +169 -0
- package/plugins-dist/screenshot/index.js +37 -45
- package/plugins-dist/screenshot/sys.js +53 -65
- package/plugins-dist/screenshot/tool.js +101 -151
- package/plugins-dist/sessions/index.js +149 -117
- package/plugins-dist/shell/index.js +117 -74
- package/plugins-dist/shell/plugin.json +1 -2
- package/plugins-dist/shell/renderers.js +28 -33
- package/plugins-dist/shell/service.js +17 -23
- package/plugins-dist/shell/shell.js +317 -320
- 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 -148
- package/plugins-dist/task/plugin.json +5 -4
- 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 +43 -0
- package/plugins-dist/tools/plugin.json +9 -0
- package/plugins-dist/tui/editor.js +145 -160
- package/plugins-dist/tui/frontend.js +641 -655
- 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 -353
- 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 -1645
- package/plugins-dist/tui/vendor.js +1388 -1088
- package/plugins-dist/undo/checkpoint.js +95 -118
- package/plugins-dist/undo/index.js +185 -192
- package/plugins-dist/undo/snapshot.js +83 -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 +489 -34
|
@@ -1,87 +1,77 @@
|
|
|
1
|
-
// jade/mentions:用户消息里 @ 引用的文件和目录,内容随消息交给模型,模型不用再读一遍。
|
|
2
|
-
//
|
|
3
|
-
// 内核没有改写用户输入的扩展点:input.received 原样进日志。所以在 context.project 里把附件加到引用它的那条用户消息后面。
|
|
4
|
-
// 附件在消息之后第一次投影时读取,先用 ctx.record 写进日志(derived)再使用(I-5):之后每次投影都从 derived 里取,
|
|
5
|
-
// 文件之后变了也不影响已经发出的消息(提示缓存不失效),恢复会话后也一样。
|
|
6
|
-
// 每条消息的附件单独记一个 key(attachedKey):derived.recorded 是整值替换,记成一张表的话,
|
|
7
|
-
// 每次有新的 @ 都要把之前附过的全部内容再写一遍,日志按平方增长。
|
|
8
|
-
// 输入框里 @ 的补全不在这里,在 jade/tui。
|
|
9
1
|
import { definePlugin } from "@uncarved/jade-api";
|
|
10
2
|
import { attachMentions } from "./attach.js";
|
|
11
|
-
import {
|
|
12
|
-
|
|
3
|
+
import { mentionCandidates } from "./candidates.js";
|
|
4
|
+
import { clip, dim, lines as joinLines, warn } from "./style.js";
|
|
13
5
|
export const attachedKey = (plugin, i) => `${plugin}/${i}`;
|
|
14
|
-
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
const userText = (m) => (m.role === "user" ? m.content.flatMap((b) => (b.kind === "text" && b.origin.by === "user" ? [b.text] : [])).join("\n") : "");
|
|
6
|
+
const READ_TIMEOUT_MS = 30000;
|
|
7
|
+
const userText = (m) => m.role === "user" ? m.content.flatMap((b) => b.kind === "text" && b.origin.by === "user" ? [b.text] : []).join(`
|
|
8
|
+
`) : "";
|
|
18
9
|
export default definePlugin({
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
10
|
+
process(ctx) {
|
|
11
|
+
ctx.service("completions@1")?.register({
|
|
12
|
+
trigger: "@",
|
|
13
|
+
label: "files",
|
|
14
|
+
hint: "the file's content is sent with the message",
|
|
15
|
+
candidates: () => mentionCandidates(ctx.cwd)
|
|
16
|
+
});
|
|
17
|
+
const renderers = ctx.service("renderers@1");
|
|
18
|
+
renderers?.event({
|
|
19
|
+
event: "attached",
|
|
20
|
+
render: (data) => {
|
|
21
|
+
const d = data;
|
|
22
|
+
const lines = [
|
|
23
|
+
...d.failed.map((f) => warn(`[@${f.path} not attached: ${clip(f.reason, 200)}]`)),
|
|
24
|
+
...d.attached.length ? [dim(`[attached ${d.attached.map((p) => `@${p}`).join(", ")}]`)] : []
|
|
25
|
+
];
|
|
26
|
+
return lines.length ? { line: joinLines(lines) } : undefined;
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
},
|
|
30
|
+
session(ctx) {
|
|
31
|
+
const key = ctx.plugin;
|
|
32
|
+
let scanned = 0;
|
|
33
|
+
ctx.kernel.hook({
|
|
34
|
+
kind: "transform",
|
|
35
|
+
point: "context.project",
|
|
36
|
+
order: -50,
|
|
37
|
+
optional: true,
|
|
38
|
+
timeoutMs: READ_TIMEOUT_MS + 5000,
|
|
39
|
+
fn: async (messages, hook) => {
|
|
40
|
+
const state = hook.derived[key];
|
|
41
|
+
const start = Math.max(state?.upTo ?? 0, scanned);
|
|
42
|
+
const fresh = new Map;
|
|
43
|
+
if (state?.attached) {
|
|
44
|
+
for (const [i, blocks] of Object.entries(state.attached)) {
|
|
45
|
+
await hook.record(attachedKey(key, Number(i)), blocks);
|
|
46
|
+
fresh.set(Number(i), blocks);
|
|
47
|
+
}
|
|
48
|
+
await hook.record(key, { upTo: state.upTo });
|
|
49
|
+
}
|
|
50
|
+
if (messages.length > start) {
|
|
51
|
+
let changed = false;
|
|
52
|
+
const signal = AbortSignal.any([hook.signal, AbortSignal.timeout(READ_TIMEOUT_MS)]);
|
|
53
|
+
for (let i = start;i < messages.length; i++) {
|
|
54
|
+
const text = userText(messages[i]);
|
|
55
|
+
if (!text.includes("@"))
|
|
56
|
+
continue;
|
|
57
|
+
changed = true;
|
|
58
|
+
const refs = await attachMentions(text, { cwd: ctx.cwd, plugin: ctx.plugin, signal });
|
|
59
|
+
if (refs.blocks.length) {
|
|
60
|
+
await hook.record(attachedKey(key, i), refs.blocks);
|
|
61
|
+
fresh.set(i, refs.blocks);
|
|
62
|
+
}
|
|
63
|
+
if (refs.attached.length || refs.failed.length)
|
|
64
|
+
await hook.emit("attached", { attached: refs.attached, failed: refs.failed });
|
|
65
|
+
}
|
|
66
|
+
if (changed)
|
|
67
|
+
await hook.record(key, { upTo: messages.length });
|
|
68
|
+
scanned = messages.length;
|
|
69
|
+
}
|
|
70
|
+
return messages.map((m, i) => {
|
|
71
|
+
const extra = fresh.get(i) ?? hook.derived[attachedKey(key, i)];
|
|
72
|
+
return extra ? { ...m, content: [...m.content, ...extra] } : m;
|
|
31
73
|
});
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
/** 本进程里已经扫过的消息条数:没有 @ 的消息不用记进日志 */
|
|
36
|
-
let scanned = 0;
|
|
37
|
-
ctx.kernel.hook({
|
|
38
|
-
kind: "transform",
|
|
39
|
-
point: "context.project",
|
|
40
|
-
// 排在压缩之前:压缩估算的是带着附件的投影
|
|
41
|
-
order: -50,
|
|
42
|
-
// 出错时跳过(不附上),不让这一轮失败
|
|
43
|
-
optional: true,
|
|
44
|
-
timeoutMs: READ_TIMEOUT_MS + 5_000,
|
|
45
|
-
fn: async (messages, hook) => {
|
|
46
|
-
const state = hook.derived[key];
|
|
47
|
-
const start = Math.max(state?.upTo ?? 0, scanned);
|
|
48
|
-
/** 这次记下的;hook.derived 是调用时的快照,刚记下的不在里面 */
|
|
49
|
-
const fresh = new Map();
|
|
50
|
-
if (state?.attached) {
|
|
51
|
-
// 旧格式的会话:先拆成每条消息一个 key,进度里去掉整表,之后不再随新的 @ 重写
|
|
52
|
-
for (const [i, blocks] of Object.entries(state.attached)) {
|
|
53
|
-
await hook.record(attachedKey(key, Number(i)), blocks);
|
|
54
|
-
fresh.set(Number(i), blocks);
|
|
55
|
-
}
|
|
56
|
-
await hook.record(key, { upTo: state.upTo });
|
|
57
|
-
}
|
|
58
|
-
if (messages.length > start) {
|
|
59
|
-
let changed = false;
|
|
60
|
-
const signal = AbortSignal.any([hook.signal, AbortSignal.timeout(READ_TIMEOUT_MS)]);
|
|
61
|
-
for (let i = start; i < messages.length; i++) {
|
|
62
|
-
const text = userText(messages[i]);
|
|
63
|
-
if (!text.includes("@"))
|
|
64
|
-
continue;
|
|
65
|
-
changed = true;
|
|
66
|
-
const refs = await attachMentions(text, { cwd: ctx.cwd, plugin: ctx.plugin, signal });
|
|
67
|
-
if (refs.blocks.length) {
|
|
68
|
-
await hook.record(attachedKey(key, i), refs.blocks);
|
|
69
|
-
fresh.set(i, refs.blocks);
|
|
70
|
-
}
|
|
71
|
-
if (refs.attached.length || refs.failed.length)
|
|
72
|
-
await hook.emit("attached", { attached: refs.attached, failed: refs.failed });
|
|
73
|
-
}
|
|
74
|
-
// 处理过带 @ 的消息才记:恢复的会话不再重新读这些消息引用的文件(文件可能已经变了)
|
|
75
|
-
// 附件先于进度记下:两次记录之间崩溃时请求还没发出,恢复后重新处理这条消息即可
|
|
76
|
-
if (changed)
|
|
77
|
-
await hook.record(key, { upTo: messages.length });
|
|
78
|
-
scanned = messages.length;
|
|
79
|
-
}
|
|
80
|
-
return messages.map((m, i) => {
|
|
81
|
-
const extra = fresh.get(i) ?? hook.derived[attachedKey(key, i)];
|
|
82
|
-
return extra ? { ...m, content: [...m.content, ...extra] } : m;
|
|
83
|
-
});
|
|
84
|
-
},
|
|
85
|
-
});
|
|
86
|
-
},
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
}
|
|
87
77
|
});
|
|
@@ -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]);
|
|
@@ -1,53 +1,39 @@
|
|
|
1
|
-
// 模型配置:$JADE_HOME/.env(默认 ~/.jade/.env),和旧 jade 相同。shell 里已经设置的变量优先。
|
|
2
|
-
//
|
|
3
|
-
// 不读当前目录的 .env:那是被操作项目自己的配置,里面的 OPENAI_API_KEY 等会改掉模型选择。
|
|
4
1
|
import { readFile } from "node:fs/promises";
|
|
5
|
-
/** 解析 KEY=VALUE 格式:忽略空行和 # 注释,允许 export 前缀,去掉成对的引号(双引号里的 \n 换成换行) */
|
|
6
2
|
export function parseEnv(text) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
value = value.replace(/\s+#.*$/, "");
|
|
22
|
-
}
|
|
23
|
-
out[m[1]] = value;
|
|
3
|
+
const out = {};
|
|
4
|
+
for (const raw of text.split(/\r?\n/)) {
|
|
5
|
+
const m = /^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*?)\s*$/.exec(raw);
|
|
6
|
+
if (!m)
|
|
7
|
+
continue;
|
|
8
|
+
let value = m[2];
|
|
9
|
+
const quote = value[0];
|
|
10
|
+
if ((quote === '"' || quote === "'") && value.length >= 2 && value.endsWith(quote)) {
|
|
11
|
+
value = value.slice(1, -1);
|
|
12
|
+
if (quote === '"')
|
|
13
|
+
value = value.replaceAll("\\n", `
|
|
14
|
+
`);
|
|
15
|
+
} else {
|
|
16
|
+
value = value.replace(/\s+#.*$/, "");
|
|
24
17
|
}
|
|
25
|
-
|
|
18
|
+
out[m[1]] = value;
|
|
19
|
+
}
|
|
20
|
+
return out;
|
|
26
21
|
}
|
|
27
|
-
/** 配置文件里的变量,env 里已有的优先;文件不存在时就是 env */
|
|
28
22
|
export async function envWithFile(file, env = process.env) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
// 没有配置文件
|
|
35
|
-
}
|
|
36
|
-
return { ...parseEnv(text), ...Object.fromEntries(Object.entries(env).filter(([, v]) => v !== undefined)) };
|
|
23
|
+
let text = "";
|
|
24
|
+
try {
|
|
25
|
+
text = await readFile(file, "utf8");
|
|
26
|
+
} catch {}
|
|
27
|
+
return { ...parseEnv(text), ...Object.fromEntries(Object.entries(env).filter(([, v]) => v !== undefined)) };
|
|
37
28
|
}
|
|
38
|
-
/**
|
|
39
|
-
* token 数:整数,或者带 K(千)、M(百万)的写法,例如 32000、512K、1M、1.5M,不区分大小写,可以用 _ 分隔数字。
|
|
40
|
-
* 不是正整数时抛出,错误信息里带变量名 name
|
|
41
|
-
*/
|
|
42
29
|
export function parseTokens(name, text) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
30
|
+
const m = /^\s*(\d+(?:\.\d+)?)\s*([km])?\s*$/i.exec(text.replaceAll("_", ""));
|
|
31
|
+
const unit = m?.[2] ? m[2].toLowerCase() === "k" ? 1000 : 1e6 : 1;
|
|
32
|
+
const n = m ? unit === 1 ? Number(m[1]) : Math.round(Number(m[1]) * unit) : NaN;
|
|
33
|
+
if (!Number.isInteger(n) || n <= 0)
|
|
34
|
+
throw new Error(`${name} must be a number of tokens such as 32000, 512K or 1M, got ${JSON.stringify(text)}`);
|
|
35
|
+
return n;
|
|
49
36
|
}
|
|
50
|
-
/** 至少配置了一种模型 */
|
|
51
37
|
export function hasProvider(env = process.env) {
|
|
52
|
-
|
|
38
|
+
return Boolean(env.JADE_PROVIDER || env.OPENAI_API_KEY || env.OPENAI_COMPAT_BASE_URL || env.ANTHROPIC_API_KEY);
|
|
53
39
|
}
|