@dabuliu_wsy/dsh-im 0.0.0-stage → 0.2.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.
Files changed (137) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +197 -3
  3. package/cordis.patch.yml +4 -0
  4. package/dist/core/agent-state.d.ts +38 -0
  5. package/dist/core/agent-state.js +59 -0
  6. package/dist/core/arbiter-bridge.d.ts +25 -0
  7. package/dist/core/arbiter-bridge.js +20 -0
  8. package/dist/core/arbiter.d.ts +29 -0
  9. package/dist/core/arbiter.js +59 -0
  10. package/dist/core/avatar-brands.d.ts +23 -0
  11. package/dist/core/avatar-brands.js +75 -0
  12. package/dist/core/avatar-presets.d.ts +21 -0
  13. package/dist/core/avatar-presets.js +38 -0
  14. package/dist/core/avatar.d.ts +1 -0
  15. package/dist/core/avatar.js +18 -0
  16. package/dist/core/blocked.d.ts +12 -0
  17. package/dist/core/blocked.js +23 -0
  18. package/dist/core/budget-plan.d.ts +41 -0
  19. package/dist/core/budget-plan.js +69 -0
  20. package/dist/core/calibration-samples.d.ts +77 -0
  21. package/dist/core/calibration-samples.js +168 -0
  22. package/dist/core/calibrator.d.ts +57 -0
  23. package/dist/core/calibrator.js +125 -0
  24. package/dist/core/chapter-tree.d.ts +128 -0
  25. package/dist/core/chapter-tree.js +400 -0
  26. package/dist/core/consensus.d.ts +135 -0
  27. package/dist/core/consensus.js +394 -0
  28. package/dist/core/context-budget.d.ts +153 -0
  29. package/dist/core/context-budget.js +183 -0
  30. package/dist/core/curator.d.ts +28 -0
  31. package/dist/core/curator.js +190 -0
  32. package/dist/core/dual-window.d.ts +86 -0
  33. package/dist/core/dual-window.js +175 -0
  34. package/dist/core/events.d.ts +110 -0
  35. package/dist/core/events.js +117 -0
  36. package/dist/core/facts.d.ts +114 -0
  37. package/dist/core/facts.js +400 -0
  38. package/dist/core/fence.d.ts +34 -0
  39. package/dist/core/fence.js +129 -0
  40. package/dist/core/floor.d.ts +18 -0
  41. package/dist/core/floor.js +80 -0
  42. package/dist/core/ids.d.ts +11 -0
  43. package/dist/core/ids.js +29 -0
  44. package/dist/core/im.d.ts +167 -0
  45. package/dist/core/im.js +1307 -0
  46. package/dist/core/index.d.ts +33 -0
  47. package/dist/core/index.js +33 -0
  48. package/dist/core/log.d.ts +21 -0
  49. package/dist/core/log.js +63 -0
  50. package/dist/core/memory-claims.d.ts +67 -0
  51. package/dist/core/memory-claims.js +164 -0
  52. package/dist/core/memory-context.d.ts +121 -0
  53. package/dist/core/memory-context.js +303 -0
  54. package/dist/core/memory-visibility.d.ts +58 -0
  55. package/dist/core/memory-visibility.js +36 -0
  56. package/dist/core/memory.d.ts +12 -0
  57. package/dist/core/memory.js +73 -0
  58. package/dist/core/metrics.d.ts +49 -0
  59. package/dist/core/metrics.js +89 -0
  60. package/dist/core/multi-hop.d.ts +116 -0
  61. package/dist/core/multi-hop.js +278 -0
  62. package/dist/core/persist.d.ts +21 -0
  63. package/dist/core/persist.js +94 -0
  64. package/dist/core/proactive.d.ts +54 -0
  65. package/dist/core/proactive.js +108 -0
  66. package/dist/core/prompt.d.ts +82 -0
  67. package/dist/core/prompt.js +164 -0
  68. package/dist/core/receipt.d.ts +107 -0
  69. package/dist/core/receipt.js +86 -0
  70. package/dist/core/retrieval.d.ts +69 -0
  71. package/dist/core/retrieval.js +260 -0
  72. package/dist/core/role-state.d.ts +16 -0
  73. package/dist/core/role-state.js +65 -0
  74. package/dist/core/runtime.d.ts +137 -0
  75. package/dist/core/runtime.js +574 -0
  76. package/dist/core/summary.d.ts +19 -0
  77. package/dist/core/summary.js +96 -0
  78. package/dist/core/text.d.ts +10 -0
  79. package/dist/core/text.js +45 -0
  80. package/dist/core/time.d.ts +22 -0
  81. package/dist/core/time.js +84 -0
  82. package/dist/core/tool-schema.d.ts +12 -0
  83. package/dist/core/tool-schema.js +92 -0
  84. package/dist/core/tools.d.ts +137 -0
  85. package/dist/core/tools.js +348 -0
  86. package/dist/core/types.d.ts +400 -0
  87. package/dist/core/types.js +1 -0
  88. package/dist/core/window.d.ts +6 -0
  89. package/dist/core/window.js +7 -0
  90. package/dist/core/workspace-node.d.ts +28 -0
  91. package/dist/core/workspace-node.js +232 -0
  92. package/dist/core/workspace.d.ts +9 -0
  93. package/dist/core/workspace.js +45 -0
  94. package/dist/host/agent-session.d.ts +279 -0
  95. package/dist/host/agent-session.js +935 -0
  96. package/dist/host/attachment-gc.d.ts +37 -0
  97. package/dist/host/attachment-gc.js +97 -0
  98. package/dist/host/calibration-samples-store.d.ts +5 -0
  99. package/dist/host/calibration-samples-store.js +67 -0
  100. package/dist/host/capabilities.d.ts +45 -0
  101. package/dist/host/capabilities.js +67 -0
  102. package/dist/host/derived-store.d.ts +36 -0
  103. package/dist/host/derived-store.js +199 -0
  104. package/dist/host/dsh-llm.d.ts +143 -0
  105. package/dist/host/dsh-llm.js +395 -0
  106. package/dist/host/event-store.d.ts +47 -0
  107. package/dist/host/event-store.js +154 -0
  108. package/dist/host/fs-atomic.d.ts +1 -0
  109. package/dist/host/fs-atomic.js +26 -0
  110. package/dist/host/im-api.d.ts +352 -0
  111. package/dist/host/im-api.js +1137 -0
  112. package/dist/host/index.d.ts +103 -0
  113. package/dist/host/index.js +453 -0
  114. package/dist/host/memory-index.d.ts +65 -0
  115. package/dist/host/memory-index.js +181 -0
  116. package/dist/host/metrics-store.d.ts +7 -0
  117. package/dist/host/metrics-store.js +104 -0
  118. package/dist/host/models.d.ts +39 -0
  119. package/dist/host/models.js +67 -0
  120. package/dist/host/persona-tools.d.ts +57 -0
  121. package/dist/host/persona-tools.js +226 -0
  122. package/dist/host/preflight.d.ts +31 -0
  123. package/dist/host/preflight.js +29 -0
  124. package/dist/host/receipt-store.d.ts +26 -0
  125. package/dist/host/receipt-store.js +68 -0
  126. package/dist/host/run-check.d.ts +51 -0
  127. package/dist/host/run-check.js +190 -0
  128. package/dist/host/state-roots.d.ts +50 -0
  129. package/dist/host/state-roots.js +163 -0
  130. package/dist/host/tool-cache.d.ts +25 -0
  131. package/dist/host/tool-cache.js +90 -0
  132. package/dist/host/tool-call-budget.d.ts +29 -0
  133. package/dist/host/tool-call-budget.js +38 -0
  134. package/dsh-plugin.json +25 -0
  135. package/lib/client.inner.cjs +3346 -0
  136. package/lib/client.js +3354 -0
  137. package/package.json +86 -6
@@ -0,0 +1,163 @@
1
+ /**
2
+ * 数据根发现与体检(只读)。
3
+ *
4
+ * 背景:`state.json` 是按「DSH 工作区根」放在 `<root>/.dsh-im/state.json` 的。
5
+ * 一旦面板的数据根指向了一个 DSH 没注册的目录,宿主会 400,而旧客户端把 400
6
+ * 吞成「空数据」——用户看到的就是「专家团队数据没了」。这个模块让面板能:
7
+ * 1) 列出当前根 / 已注册工作区 / 以前用过的根,各自有没有数据、多少角色/会话;
8
+ * 2) 在加载失败时给出「哪个根里还有数据」,而不是一片空白。
9
+ *
10
+ * 这里只做 stat + JSON 计数,不触发任何共识/摘要副作用。
11
+ */
12
+ import { readFile, stat } from 'node:fs/promises';
13
+ import { homedir } from 'node:os';
14
+ import { join } from 'node:path';
15
+ import { writeFileAtomic } from './fs-atomic.js';
16
+ /** 根去重键:Windows 大小写不敏感,其它平台保留大小写。 */
17
+ export function rootKey(path) {
18
+ const normalized = path.replaceAll('\\', '/').replace(/\/+$/, '');
19
+ return process.platform === 'win32' ? normalized.toLowerCase() : normalized;
20
+ }
21
+ export function stateFileOf(root) {
22
+ return join(root, '.dsh-im', 'state.json');
23
+ }
24
+ /** 每个数据根体检一次(只读、不写入、不触发共识副作用)。 */
25
+ export async function describeStateRoot(root, options = {}) {
26
+ const io = options.io ?? {};
27
+ const file = stateFileOf(root);
28
+ const base = {
29
+ path: root,
30
+ registered: Boolean(options.registered),
31
+ hasState: false,
32
+ personas: 0,
33
+ threads: 0,
34
+ messages: 0,
35
+ ...(options.title ? { title: options.title } : {}),
36
+ };
37
+ const readFileOf = io.readFile ?? ((target) => readFile(target, 'utf8'));
38
+ const mtimeOf = io.mtime ?? (async (target) => (await stat(target)).mtimeMs);
39
+ try {
40
+ const text = await readFileOf(file);
41
+ const parsed = JSON.parse(text);
42
+ const threads = Array.isArray(parsed.threads) ? parsed.threads : [];
43
+ const updatedAt = threads.reduce((max, thread) => Math.max(max, Number(thread?.updatedAt) || 0), 0);
44
+ let mtime = 0;
45
+ try {
46
+ mtime = await mtimeOf(file);
47
+ }
48
+ catch {
49
+ mtime = 0;
50
+ }
51
+ return {
52
+ ...base,
53
+ hasState: true,
54
+ personas: Array.isArray(parsed.personas) ? parsed.personas.length : 0,
55
+ threads: threads.length,
56
+ messages: threads.reduce((sum, thread) => sum + (Array.isArray(thread?.messages) ? thread.messages.length : 0), 0),
57
+ updatedAt: updatedAt || mtime || undefined,
58
+ };
59
+ }
60
+ catch (error) {
61
+ const code = error.code;
62
+ if (code === 'ENOENT')
63
+ return base;
64
+ return { ...base, error: error instanceof Error ? error.message : String(error) };
65
+ }
66
+ }
67
+ /**
68
+ * 候选数据根 = 已注册工作区 + 以前用过且被记住的根 + 当前根(即使未注册)。
69
+ * 当前根排第一;其余按「有没有数据 → 最近更新 → 是否注册」排序。
70
+ */
71
+ export async function listStateRoots(options) {
72
+ const current = options.current?.trim();
73
+ const entries = new Map();
74
+ const add = (path, title, registered) => {
75
+ if (!path || !path.trim())
76
+ return;
77
+ const key = rootKey(path);
78
+ const existing = entries.get(key);
79
+ // 注册信息优先(同一个根既是注册工作区又是历史根时,保留标题)。
80
+ if (!existing) {
81
+ entries.set(key, { path, registered, ...(title ? { title } : {}) });
82
+ return;
83
+ }
84
+ if (registered && !existing.registered)
85
+ existing.registered = true;
86
+ if (!existing.title && title)
87
+ existing.title = title;
88
+ };
89
+ for (const workspace of options.workspaces ?? []) {
90
+ if (workspace?.path)
91
+ add(workspace.path, workspace.title?.trim() || undefined, true);
92
+ }
93
+ for (const path of options.knownRoots ?? [])
94
+ add(path, undefined, false);
95
+ if (current)
96
+ add(current, undefined, false);
97
+ const infos = await Promise.all([...entries.values()].map((entry) => describeStateRoot(entry.path, {
98
+ title: entry.title,
99
+ registered: entry.registered,
100
+ io: options.io,
101
+ })));
102
+ const currentKey = current ? rootKey(current) : '';
103
+ return infos.sort((a, b) => {
104
+ const aCurrent = rootKey(a.path) === currentKey ? 1 : 0;
105
+ const bCurrent = rootKey(b.path) === currentKey ? 1 : 0;
106
+ if (aCurrent !== bCurrent)
107
+ return bCurrent - aCurrent;
108
+ if (a.hasState !== b.hasState)
109
+ return a.hasState ? -1 : 1;
110
+ if ((a.updatedAt ?? 0) !== (b.updatedAt ?? 0))
111
+ return (b.updatedAt ?? 0) - (a.updatedAt ?? 0);
112
+ if (a.registered !== b.registered)
113
+ return a.registered ? -1 : 1;
114
+ return a.path.localeCompare(b.path);
115
+ });
116
+ }
117
+ /**
118
+ * 记住用过的数据根。必须放在不随工作区变化的位置:
119
+ * 否则切工作区后连「以前用过哪个根」都查不到,只能继续空着。
120
+ */
121
+ export function defaultRootsFile() {
122
+ const override = process.env.DSH_IM_ROOTS_FILE?.trim();
123
+ if (override)
124
+ return override;
125
+ return join(homedir(), '.dsh-im', 'known-roots.json');
126
+ }
127
+ const MAX_KNOWN_ROOTS = 50;
128
+ export async function readKnownRoots(file = defaultRootsFile()) {
129
+ try {
130
+ const parsed = JSON.parse(await readFile(file, 'utf8'));
131
+ if (!Array.isArray(parsed.roots))
132
+ return [];
133
+ return parsed.roots.filter((entry) => typeof entry === 'string' && entry.trim().length > 0);
134
+ }
135
+ catch {
136
+ return [];
137
+ }
138
+ }
139
+ /** 记住用过的根是 read-modify-write 的小文件:并发写会互相覆盖(丢掉刚记的根),必须串行 + 原子写。 */
140
+ const rootFileChains = new Map();
141
+ function enqueueRootFile(key, task) {
142
+ const previous = rootFileChains.get(key) ?? Promise.resolve();
143
+ const next = previous.then(task, task);
144
+ rootFileChains.set(key, next.then(() => undefined, () => undefined));
145
+ return next;
146
+ }
147
+ /** 记录一次成功读写的根;失败绝不打断主流程(只是让「找回数据」变方便)。 */
148
+ export function recordKnownRoot(root, file = defaultRootsFile()) {
149
+ const target = root?.trim();
150
+ if (!target)
151
+ return Promise.resolve();
152
+ return enqueueRootFile(file, async () => {
153
+ try {
154
+ const roots = await readKnownRoots(file);
155
+ const key = rootKey(target);
156
+ const next = [target, ...roots.filter((entry) => rootKey(entry) !== key)].slice(0, MAX_KNOWN_ROOTS);
157
+ await writeFileAtomic(file, JSON.stringify({ roots: next }, null, 2));
158
+ }
159
+ catch {
160
+ // 忘记记录不是错误:主流程(读写 state)已经成功。
161
+ }
162
+ });
163
+ }
@@ -0,0 +1,25 @@
1
+ export interface TurnToolCacheOptions {
2
+ /** 最多记多少条(防极端情况无界增长)。 */
3
+ maxEntries?: number;
4
+ /** Windows 路径不区分大小写;测试里可显式指定。 */
5
+ caseInsensitive?: boolean;
6
+ }
7
+ export interface ToolCallLookup {
8
+ repeated: true;
9
+ hint: string;
10
+ key: string;
11
+ }
12
+ export interface TurnToolCache {
13
+ /** 第一次返回 null;重复调用返回提示(调用方直接把它当 tool 结果喂回去)。 */
14
+ lookup(name: string, args: Record<string, unknown>): ToolCallLookup | null;
15
+ /** 成功的工具结果记入缓存;失败的结果不记(允许模型重试)。 */
16
+ record(name: string, args: Record<string, unknown>, result: string): void;
17
+ /** 写文件后调用:结果可能已变化,全部作废。 */
18
+ invalidate(): void;
19
+ /** 本轮命中的重复调用次数(给指标用)。 */
20
+ hits(): number;
21
+ size(): number;
22
+ }
23
+ /** 工具 + 参数的规范化 key:路径统一成正斜杠/去 ./..,Windows 下忽略大小写。 */
24
+ export declare function toolCallKey(name: string, args: Record<string, unknown>, options?: TurnToolCacheOptions): string;
25
+ export declare function createTurnToolCache(options?: TurnToolCacheOptions): TurnToolCache;
@@ -0,0 +1,90 @@
1
+ /**
2
+ * 单轮工具调用去重:模型经常反复读同一个文件 / 列同一个目录 / 搜同一个关键词。
3
+ *
4
+ * 做法是“工具结果缓存 + 重复提示”:本轮内同一工具 + 同一(规范化后的)参数再次调用时,
5
+ * 不再执行真实 IO,而是返回一句提示,让模型基于前面已经拿到的结果继续。
6
+ *
7
+ * 只作用于一次 `/complete` 之内;`write_file`/`run_check` 会让结果过期,所以写文件后清空缓存。
8
+ */
9
+ import { normalizePath } from '../core/workspace.js';
10
+ const PATH_TOOLS = new Set(['read_file', 'list_dir']);
11
+ const TEXT_TOOLS = new Set(['search_files', 'run_check', 'memory_search']);
12
+ const SEQS_TOOLS = new Set(['memory_expand']);
13
+ function asText(value) {
14
+ return typeof value === 'string' ? value : value === undefined ? '' : String(value);
15
+ }
16
+ /** 工具 + 参数的规范化 key:路径统一成正斜杠/去 ./..,Windows 下忽略大小写。 */
17
+ export function toolCallKey(name, args, options = {}) {
18
+ const caseInsensitive = options.caseInsensitive ?? process.platform === 'win32';
19
+ if (PATH_TOOLS.has(name)) {
20
+ const raw = asText(args.path ?? '.');
21
+ const normalized = normalizePath(raw.replace(/\\/g, '/')) || '.';
22
+ return `${name}|${caseInsensitive ? normalized.toLowerCase() : normalized}`;
23
+ }
24
+ if (TEXT_TOOLS.has(name)) {
25
+ const raw = name === 'run_check' ? asText(args.target) : asText(args.query);
26
+ const normalized = raw.replace(/\s+/g, ' ').trim();
27
+ return `${name}|${caseInsensitive ? normalized.toLowerCase() : normalized}`;
28
+ }
29
+ if (SEQS_TOOLS.has(name)) {
30
+ // 记忆展开:seqs 去空白、排序后比较("12, 30" 与 "30,12" 是同一请求)。
31
+ const seqs = (asText(args.seqs ?? args.seq).match(/\d+/g) ?? []).map(Number).filter((seq) => seq > 0);
32
+ const normalized = [...new Set(seqs)].sort((left, right) => left - right).join(',');
33
+ return `${name}|${normalized}`;
34
+ }
35
+ // 其它工具:参数按 key 排序做稳定序列化。
36
+ const ordered = Object.keys(args).sort().map((key) => `${key}=${asText(args[key])}`).join('&');
37
+ return `${name}|${caseInsensitive ? ordered.toLowerCase() : ordered}`;
38
+ }
39
+ function hintFor(name, args) {
40
+ const path = asText(args.path ?? '.');
41
+ if (name === 'read_file') {
42
+ return `本轮已经读过文件「${path}」,内容就在前面的工具结果里,本次不再重复读取。不要整份重读:需要定位某段就用 search_files 搜关键词,或直接基于已有内容作答。`;
43
+ }
44
+ if (name === 'list_dir') {
45
+ return `本轮已经列过目录「${path}」,内容就在前面的工具结果里,本次不再重复列目录。请换一个目录,或直接 read_file 读具体文件。`;
46
+ }
47
+ if (name === 'search_files') {
48
+ return `本轮已经用同样的关键词搜过,结果就在前面的工具结果里,本次不再重复搜索。请换关键词,或基于已有结果作答。`;
49
+ }
50
+ if (name === 'memory_search') {
51
+ return '本轮已经查过同一个记忆检索词,句柄结果就在前面的工具结果里,本次不再重复检索(已查过,换关键词,或直接用 memory_expand / 直接作答)。';
52
+ }
53
+ if (name === 'memory_expand') {
54
+ return '本轮已经展开过同一批 #seq,原文就在前面的工具结果里,本次不再重复展开。请基于已有原文作答。';
55
+ }
56
+ if (name === 'run_check') {
57
+ return '本轮已经用同样的参数跑过检查,结果就在前面的工具结果里,本次不再重复执行。';
58
+ }
59
+ return `本轮已经用相同参数调用过 ${name},结果就在前面的工具结果里,本次不再重复执行。`;
60
+ }
61
+ export function createTurnToolCache(options = {}) {
62
+ const maxEntries = Math.max(1, options.maxEntries ?? 200);
63
+ const entries = new Map();
64
+ let hitCount = 0;
65
+ return {
66
+ lookup(name, args) {
67
+ const key = toolCallKey(name, args, options);
68
+ if (!entries.has(key))
69
+ return null;
70
+ hitCount += 1;
71
+ return { repeated: true, hint: hintFor(name, args), key };
72
+ },
73
+ record(name, args, result) {
74
+ const key = toolCallKey(name, args, options);
75
+ entries.delete(key);
76
+ entries.set(key, result);
77
+ while (entries.size > maxEntries) {
78
+ const oldest = entries.keys().next().value;
79
+ if (oldest === undefined)
80
+ break;
81
+ entries.delete(oldest);
82
+ }
83
+ },
84
+ invalidate() {
85
+ entries.clear();
86
+ },
87
+ hits: () => hitCount,
88
+ size: () => entries.size,
89
+ };
90
+ }
@@ -0,0 +1,29 @@
1
+ /**
2
+ * 单轮工具调用预算(B 修复:真机一轮约 25 次工具把耗时拖到 240s 超时)。
3
+ *
4
+ * 三条纪律:
5
+ * 1. 单轮工具调用有**硬上限**(默认 12,`DSH_IM_MAX_TOOL_CALLS` 可调);
6
+ * 2. 到上限后不再执行工具,而是把"已到上限,直接作答"作为 tool 结果喂回并强制收尾;
7
+ * 3. 计数/是否触顶要能被 trace 与 metrics 记录(`toolCalls` / `toolBudgetExhausted`)。
8
+ *
9
+ * 纯逻辑、可单测;ctx.llm 工具循环与原生会话工具注册共用这一份预算实现。
10
+ */
11
+ /** 默认单轮工具调用上限(含被重复抑制的调用;模型请求一次算一次)。 */
12
+ export declare const DEFAULT_TOOL_CALL_LIMIT = 12;
13
+ /** 从环境变量读上限;非法/非正数回退默认。 */
14
+ export declare function toolCallLimitFromEnv(env?: Record<string, string | undefined>): number;
15
+ export interface ToolCallBudget {
16
+ readonly limit: number;
17
+ /** 已经"请求"过多少次工具调用(被抑制的也算,防止模型靠换参数绕过预算)。 */
18
+ used(): number;
19
+ /** 触顶后不再执行:调用方拿 refused 的 message 当 tool 结果喂回,并强制作答。 */
20
+ tryReserve(): {
21
+ ok: true;
22
+ } | {
23
+ ok: false;
24
+ message: string;
25
+ };
26
+ /** 上限原因文本(trace/metrics 用)。 */
27
+ exhaustedMessage(): string;
28
+ }
29
+ export declare function createToolCallBudget(limit?: number): ToolCallBudget;
@@ -0,0 +1,38 @@
1
+ /**
2
+ * 单轮工具调用预算(B 修复:真机一轮约 25 次工具把耗时拖到 240s 超时)。
3
+ *
4
+ * 三条纪律:
5
+ * 1. 单轮工具调用有**硬上限**(默认 12,`DSH_IM_MAX_TOOL_CALLS` 可调);
6
+ * 2. 到上限后不再执行工具,而是把"已到上限,直接作答"作为 tool 结果喂回并强制收尾;
7
+ * 3. 计数/是否触顶要能被 trace 与 metrics 记录(`toolCalls` / `toolBudgetExhausted`)。
8
+ *
9
+ * 纯逻辑、可单测;ctx.llm 工具循环与原生会话工具注册共用这一份预算实现。
10
+ */
11
+ /** 默认单轮工具调用上限(含被重复抑制的调用;模型请求一次算一次)。 */
12
+ export const DEFAULT_TOOL_CALL_LIMIT = 12;
13
+ /** 从环境变量读上限;非法/非正数回退默认。 */
14
+ export function toolCallLimitFromEnv(env = process.env) {
15
+ const raw = Number(env.DSH_IM_MAX_TOOL_CALLS);
16
+ return Number.isFinite(raw) && raw > 0 ? Math.floor(raw) : DEFAULT_TOOL_CALL_LIMIT;
17
+ }
18
+ export function createToolCallBudget(limit = DEFAULT_TOOL_CALL_LIMIT) {
19
+ const max = Number.isFinite(limit) && limit > 0 ? Math.floor(limit) : DEFAULT_TOOL_CALL_LIMIT;
20
+ let used = 0;
21
+ return {
22
+ limit: max,
23
+ used: () => used,
24
+ tryReserve() {
25
+ if (used >= max) {
26
+ return {
27
+ ok: false,
28
+ message: `本轮工具调用已达上限(${max} 次)。不要再调用任何工具,请直接基于目前已经看到的信息作答。`,
29
+ };
30
+ }
31
+ used += 1;
32
+ return { ok: true };
33
+ },
34
+ exhaustedMessage() {
35
+ return `本轮工具调用已达上限(${max} 次),已强制停止调用工具并进入作答。`;
36
+ },
37
+ };
38
+ }
@@ -0,0 +1,25 @@
1
+ {
2
+ "$schema": "https://dsh.community/schemas/dsh-plugin-0.15.json",
3
+ "id": "dsh-im",
4
+ "name": "dsh-im",
5
+ "version": "0.2.0",
6
+ "manifestVersion": "0.15",
7
+ "facets": {
8
+ "host": {
9
+ "entry": "dist/host/index.js",
10
+ "apiVersion": "v1alpha1"
11
+ }
12
+ },
13
+ "requires": {
14
+ "contracts": []
15
+ },
16
+ "permissions": [],
17
+ "contributes": {
18
+ "commands": []
19
+ },
20
+ "subscriptions": [],
21
+ "license": "MIT",
22
+ "source": {
23
+ "repository": "https://github.com/wangshouyong/dsh-roundtable"
24
+ }
25
+ }