codemini-cli 0.2.9 → 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/src/core/tools.js CHANGED
@@ -1,6 +1,5 @@
1
1
  import fs from 'node:fs/promises';
2
2
  import path from 'node:path';
3
- import crypto from 'node:crypto';
4
3
  import { spawn } from 'node:child_process';
5
4
  import net from 'node:net';
6
5
  import {
@@ -14,70 +13,10 @@ import {
14
13
  } from './shell.js';
15
14
  import { evaluateCommandPolicy } from './command-policy.js';
16
15
  import { queryAst, readAstNode, resolveAstTarget } from './ast.js';
17
- import { initializeProjectIndex, refreshIndexedFile } from './project-index.js';
16
+ import { initializeProjectIndex, queryProjectIndex, refreshIndexedFile } from './project-index.js';
18
17
  import { checkReadDedup } from './agent-loop.js';
19
-
20
- const SKIP_DIRS = new Set(['.git', 'node_modules', '.codemini', '.codemini-global', 'dist', 'coverage']);
21
- const TEXT_EXTENSIONS = new Set([
22
- '.js',
23
- '.jsx',
24
- '.ts',
25
- '.tsx',
26
- '.json',
27
- '.md',
28
- '.mjs',
29
- '.cjs',
30
- '.py',
31
- '.rb',
32
- '.go',
33
- '.rs',
34
- '.java',
35
- '.cs',
36
- '.css',
37
- '.scss',
38
- '.html',
39
- '.yml',
40
- '.yaml',
41
- '.sh',
42
- '.ps1'
43
- ]);
44
- const CODE_WRITE_GUARD_EXTENSIONS = new Set([
45
- '.js',
46
- '.jsx',
47
- '.ts',
48
- '.tsx',
49
- '.mjs',
50
- '.cjs',
51
- '.py',
52
- '.rb',
53
- '.go',
54
- '.rs',
55
- '.java',
56
- '.cs',
57
- '.css',
58
- '.scss',
59
- '.html',
60
- '.sh',
61
- '.ps1'
62
- ]);
63
- const LANGUAGE_FILE_TYPES = {
64
- js: ['js', 'jsx', 'mjs', 'cjs'],
65
- ts: ['ts', 'tsx'],
66
- py: ['py'],
67
- python: ['py'],
68
- md: ['md'],
69
- json: ['json'],
70
- css: ['css', 'scss'],
71
- html: ['html'],
72
- java: ['java'],
73
- csharp: ['cs'],
74
- cs: ['cs'],
75
- go: ['go'],
76
- rust: ['rs'],
77
- ruby: ['rb'],
78
- shell: ['sh', 'ps1'],
79
- yaml: ['yml', 'yaml']
80
- };
18
+ import { TOOL_SKIP_DIRS as SKIP_DIRS, TEXT_EXTENSIONS, CODE_WRITE_GUARD_EXTENSIONS, LANGUAGE_FILE_TYPES } from './constants.js';
19
+ import { sha256Prefixed as sha256, sha1 } from './crypto-utils.js';
81
20
  const SERVICE_RECENT_LOG_LIMIT = 80;
82
21
  const SERVICE_STARTUP_POLL_MS = 150;
83
22
  const serviceRegistry = new Map();
@@ -97,14 +36,6 @@ function toWorkspaceRelative(root, absPath) {
97
36
  return path.relative(path.resolve(root), absPath).replace(/\\/g, '/');
98
37
  }
99
38
 
100
- function sha256(input) {
101
- return `sha256:${crypto.createHash('sha256').update(String(input || '')).digest('hex')}`;
102
- }
103
-
104
- function sha1(input) {
105
- return crypto.createHash('sha1').update(String(input || '')).digest('hex');
106
- }
107
-
108
39
  function trimLinePreview(line, maxLen = 180) {
109
40
  const text = String(line || '').replace(/\t/g, ' ').trim();
110
41
  if (text.length <= maxLen) return text;
@@ -1928,6 +1859,24 @@ export function getBuiltinTools({ workspaceRoot = process.cwd(), config, onSyste
1928
1859
  }
1929
1860
  }
1930
1861
  },
1862
+ {
1863
+ type: 'function',
1864
+ function: {
1865
+ name: 'query_project_index',
1866
+ description:
1867
+ 'Query the lightweight project index before broad file reads. Uses both project-map metadata and file-index symbols to suggest the most relevant files for the current task.',
1868
+ parameters: {
1869
+ type: 'object',
1870
+ properties: {
1871
+ query: { type: 'string', description: 'Task or code search phrase such as "login auth" or "tui presenters"' },
1872
+ path: { type: 'string', description: 'Optional path prefix like src or src/auth to narrow results' },
1873
+ path_prefix: { type: 'string', description: 'Alias for path' },
1874
+ language: { type: 'string', description: 'Optional language filter such as ts, js, python, or go' },
1875
+ max_results: { type: 'number', description: 'Max result files to return' }
1876
+ }
1877
+ }
1878
+ }
1879
+ },
1931
1880
  {
1932
1881
  type: 'function',
1933
1882
  function: {
@@ -2182,6 +2131,10 @@ export function getBuiltinTools({ workspaceRoot = process.cwd(), config, onSyste
2182
2131
  if (readPath) lastReadPath = readPath;
2183
2132
  return result;
2184
2133
  }),
2134
+ query_project_index: async (args) => {
2135
+ await ensureProjectIndex();
2136
+ return queryProjectIndex(workspaceRoot, args);
2137
+ },
2185
2138
  grep: (args) => grep(workspaceRoot, args),
2186
2139
  glob: (args) => glob(workspaceRoot, args),
2187
2140
  list: (args) => list(workspaceRoot, args),
@@ -2311,6 +2264,33 @@ export function getBuiltinTools({ workspaceRoot = process.cwd(), config, onSyste
2311
2264
  return `${header}\n${dirs.join('\n')}${dirs.length && files.length ? '\n' : ''}${files.join('\n')}`;
2312
2265
  },
2313
2266
 
2267
+ query_project_index(result) {
2268
+ if (!result || typeof result !== 'object') return String(result);
2269
+ const lines = [];
2270
+ if (result.query) lines.push(`[project_index: "${result.query}"]`);
2271
+ if (result.project_root) lines.push(`project_root: ${result.project_root}`);
2272
+ const projectMap = result.project_map;
2273
+ if (projectMap) {
2274
+ lines.push(`languages: ${(projectMap.languages || []).join(', ') || 'unknown'}`);
2275
+ lines.push(`source_roots: ${(projectMap.source_roots || []).join(', ') || 'none'}`);
2276
+ lines.push(`test_roots: ${(projectMap.test_roots || []).join(', ') || 'none'}`);
2277
+ lines.push(`entry_candidates: ${(projectMap.entry_candidates || []).join(', ') || 'none'}`);
2278
+ lines.push(`framework_hints: ${(projectMap.framework_hints || []).join(', ') || 'none'}`);
2279
+ }
2280
+ const matches = Array.isArray(result.matches) ? result.matches : [];
2281
+ if (matches.length === 0) {
2282
+ lines.push('No indexed file matches found.');
2283
+ return lines.join('\n');
2284
+ }
2285
+ lines.push('matches:');
2286
+ for (const item of matches) {
2287
+ lines.push(
2288
+ `- ${item.file} [score=${item.score}] exports=[${(item.exports || []).join(', ')}] functions=[${(item.functions || []).join(', ')}] classes=[${(item.classes || []).join(', ')}]`
2289
+ );
2290
+ }
2291
+ return lines.join('\n');
2292
+ },
2293
+
2314
2294
  edit(result) {
2315
2295
  if (!result || typeof result !== 'object') return String(result);
2316
2296
  const p = result.path || '';
@@ -128,8 +128,12 @@ const TUI_COPY = {
128
128
  doingWrite: '正在写入文件',
129
129
  donePatch: '已应用补丁',
130
130
  doingPatch: '正在应用补丁',
131
- doneList: '已查看目录',
132
- doingList: '正在查看目录',
131
+ doneList: '已列出目录',
132
+ doingList: '正在列出目录',
133
+ doneGlob: '已按模式查找文件',
134
+ doingGlob: '正在按模式查找文件',
135
+ doneGrep: '已搜索关键词',
136
+ doingGrep: '正在搜索关键词',
133
137
  doneCommand: '已执行命令',
134
138
  doingCommand: '正在执行命令',
135
139
  doneCreateTask: '已创建任务',
@@ -152,6 +156,14 @@ const TUI_COPY = {
152
156
  doingDatabase: '正在启动数据库服务',
153
157
  doneDocker: '已完成 Docker 命令',
154
158
  doingDocker: '正在执行 Docker 命令',
159
+ doneListServices: '已列出服务',
160
+ doingListServices: '正在列出服务',
161
+ doneServiceStatus: '已查看服务状态',
162
+ doingServiceStatus: '正在查看服务状态',
163
+ doneServiceLogs: '已查看服务日志',
164
+ doingServiceLogs: '正在查看服务日志',
165
+ doneStopService: '已停止服务',
166
+ doingStopService: '正在停止服务',
155
167
  doneCodeGeneration: '已生成代码',
156
168
  doingCodeGeneration: '正在生成代码',
157
169
  doneSkill: '已完成技能',
@@ -177,6 +189,7 @@ const TUI_COPY = {
177
189
  runtime: {
178
190
  sendingToGateway: '正在发送到网关',
179
191
  preparingRequest: '准备本轮请求',
192
+ submittedWaiting: '已提交,等待开始处理',
180
193
  modelThinking: '模型正在思考',
181
194
  requestDelivered: '请求已送达,等待首个 token',
182
195
  generatingReply: '正在生成回复',
@@ -262,6 +275,10 @@ const TUI_COPY = {
262
275
  doingPatch: 'Applying patch',
263
276
  doneList: 'Listed directory',
264
277
  doingList: 'Listing directory',
278
+ doneGlob: 'Matched files by pattern',
279
+ doingGlob: 'Matching files by pattern',
280
+ doneGrep: 'Searched keywords',
281
+ doingGrep: 'Searching keywords',
265
282
  doneCommand: 'Ran command',
266
283
  doingCommand: 'Running command',
267
284
  doneCreateTask: 'Created task',
@@ -284,6 +301,14 @@ const TUI_COPY = {
284
301
  doingDatabase: 'Starting database service',
285
302
  doneDocker: 'Docker command completed',
286
303
  doingDocker: 'Running Docker command',
304
+ doneListServices: 'Listed services',
305
+ doingListServices: 'Listing services',
306
+ doneServiceStatus: 'Checked service status',
307
+ doingServiceStatus: 'Checking service status',
308
+ doneServiceLogs: 'Viewed service logs',
309
+ doingServiceLogs: 'Viewing service logs',
310
+ doneStopService: 'Stopped service',
311
+ doingStopService: 'Stopping service',
287
312
  doneCodeGeneration: 'Code generated',
288
313
  doingCodeGeneration: 'Generating code',
289
314
  doneSkill: 'Completed skill',
@@ -309,6 +334,7 @@ const TUI_COPY = {
309
334
  runtime: {
310
335
  sendingToGateway: 'sending to gateway',
311
336
  preparingRequest: 'preparing this turn',
337
+ submittedWaiting: 'submitted, waiting to start',
312
338
  modelThinking: 'model is thinking',
313
339
  requestDelivered: 'request sent, waiting for first token',
314
340
  generatingReply: 'generating reply',
@@ -773,6 +799,27 @@ export function formatActivityDurationText(row, nowMs = Date.now()) {
773
799
  return '';
774
800
  }
775
801
 
802
+ export function getPendingUserMessageMeta(copy, { immediateLocal = false, inFlight = false } = {}) {
803
+ if (immediateLocal) {
804
+ return {
805
+ phase: 'sending',
806
+ liveStatus: copy.runtime.localCommandRunning
807
+ };
808
+ }
809
+
810
+ if (inFlight) {
811
+ return {
812
+ phase: 'queued',
813
+ liveStatus: copy.runtime.queuedWaiting
814
+ };
815
+ }
816
+
817
+ return {
818
+ phase: 'sending',
819
+ liveStatus: copy.runtime.submittedWaiting || copy.runtime.sendingToGateway
820
+ };
821
+ }
822
+
776
823
  function getActivityDisplayParts(activity) {
777
824
  if (isCodeGenerationActivityName(activity?.name)) {
778
825
  return {
@@ -795,6 +842,12 @@ function getActivityDisplayParts(activity) {
795
842
  };
796
843
  }
797
844
  if ((activity?.type || 'tool') === 'system_tool') {
845
+ if (parsed.base === 'project_index') {
846
+ return { primary: 'Project Index', secondary: '' };
847
+ }
848
+ if (parsed.base === 'file_index') {
849
+ return { primary: 'File Index', secondary: parsed.target ? `(${parsed.target})` : '' };
850
+ }
798
851
  return {
799
852
  primary: 'Index',
800
853
  secondary: parsed.target ? `(${parsed.target})` : parsed.base ? `(${parsed.base})` : ''
@@ -806,14 +859,14 @@ function getActivityDisplayParts(activity) {
806
859
  write: 'Write',
807
860
  patch: 'Patch',
808
861
  run: 'Run',
809
- grep: 'Grep',
862
+ grep: 'Search',
810
863
  glob: 'Glob',
811
864
  list: 'List',
812
865
  start_service: 'Service',
813
- list_services: 'Service',
814
- get_service_status: 'Service',
815
- get_service_logs: 'Service',
816
- stop_service: 'Service',
866
+ list_services: 'Services',
867
+ get_service_status: 'Status',
868
+ get_service_logs: 'Logs',
869
+ stop_service: 'Stop',
817
870
  list_files: 'Glob',
818
871
  create_task: 'Task',
819
872
  update_task: 'Task'
@@ -3257,7 +3310,7 @@ export function ChatApp({ runtime, sessionId, model, language = 'zh', shellName
3257
3310
  updateMessageMeta(next.messageId, {
3258
3311
  loading: true,
3259
3312
  phase: 'sending',
3260
- liveStatus: copy.runtime.sendingToGateway
3313
+ liveStatus: copy.runtime.submittedWaiting || copy.runtime.sendingToGateway
3261
3314
  });
3262
3315
  runSubmission(next.line, next.messageId);
3263
3316
  }
@@ -3449,6 +3502,10 @@ export function ChatApp({ runtime, sessionId, model, language = 'zh', shellName
3449
3502
  const immediateLocal =
3450
3503
  typeof runtime.isImmediateLocalInput === 'function' &&
3451
3504
  runtime.isImmediateLocalInput(line);
3505
+ const pendingUserMeta = getPendingUserMessageMeta(copy, {
3506
+ immediateLocal,
3507
+ inFlight: inFlightRef.current
3508
+ });
3452
3509
  setMessages((prev) => [
3453
3510
  ...prev,
3454
3511
  {
@@ -3457,12 +3514,8 @@ export function ChatApp({ runtime, sessionId, model, language = 'zh', shellName
3457
3514
  text: line,
3458
3515
  color: 'white',
3459
3516
  loading: true,
3460
- phase: immediateLocal ? 'sending' : inFlightRef.current ? 'queued' : 'sending',
3461
- liveStatus: immediateLocal
3462
- ? copy.runtime.localCommandRunning
3463
- : inFlightRef.current
3464
- ? copy.runtime.queuedWaiting
3465
- : copy.runtime.sendingToGateway
3517
+ phase: pendingUserMeta.phase,
3518
+ liveStatus: pendingUserMeta.liveStatus
3466
3519
  }
3467
3520
  ]);
3468
3521
  if (immediateLocal) {
@@ -21,7 +21,20 @@ export function describeCommandToolActivity(copy, parsed, { done = false, blocke
21
21
  if (parsed.base === 'run') return phaseText(copy, blocked, done, trimText(target, 72) || parsed.base, copy.toolActivity.doingCommand, copy.toolActivity.doneCommand);
22
22
  }
23
23
 
24
- if (parsed.base === 'start_service' || parsed.base === 'list_services' || parsed.base === 'get_service_status' || parsed.base === 'get_service_logs' || parsed.base === 'stop_service') {
24
+ if (parsed.base === 'list_services') {
25
+ return phaseText(copy, blocked, done, trimText(parsed.target, 72) || parsed.base, copy.toolActivity.doingListServices, copy.toolActivity.doneListServices);
26
+ }
27
+ if (parsed.base === 'get_service_status') {
28
+ return phaseText(copy, blocked, done, trimText(parsed.target, 72) || parsed.base, copy.toolActivity.doingServiceStatus, copy.toolActivity.doneServiceStatus);
29
+ }
30
+ if (parsed.base === 'get_service_logs') {
31
+ return phaseText(copy, blocked, done, trimText(parsed.target, 72) || parsed.base, copy.toolActivity.doingServiceLogs, copy.toolActivity.doneServiceLogs);
32
+ }
33
+ if (parsed.base === 'stop_service') {
34
+ return phaseText(copy, blocked, done, trimText(parsed.target, 72) || parsed.base, copy.toolActivity.doingStopService, copy.toolActivity.doneStopService);
35
+ }
36
+
37
+ if (parsed.base === 'start_service') {
25
38
  return phaseText(copy, blocked, done, trimText(parsed.target, 72) || parsed.base, copy.toolActivity.doingGeneric, copy.toolActivity.doneGeneric);
26
39
  }
27
40
 
@@ -19,8 +19,30 @@ export function describeFileToolActivity(copy, parsed, options = {}) {
19
19
  if (parsed.base === 'patch') {
20
20
  return describePathTool(copy, parsed, { done: copy.toolActivity.donePatch, doing: copy.toolActivity.doingPatch }, options);
21
21
  }
22
- if (parsed.base === 'list' || parsed.base === 'glob' || parsed.base === 'grep') {
22
+ if (parsed.base === 'list') {
23
23
  return describePathTool(copy, parsed, { done: copy.toolActivity.doneList, doing: copy.toolActivity.doingList }, options);
24
24
  }
25
+ if (parsed.base === 'glob') {
26
+ return describePathTool(
27
+ copy,
28
+ parsed,
29
+ {
30
+ done: copy.toolActivity.doneGlob || copy.toolActivity.doneList,
31
+ doing: copy.toolActivity.doingGlob || copy.toolActivity.doingList
32
+ },
33
+ options
34
+ );
35
+ }
36
+ if (parsed.base === 'grep') {
37
+ return describePathTool(
38
+ copy,
39
+ parsed,
40
+ {
41
+ done: copy.toolActivity.doneGrep || copy.toolActivity.doneList,
42
+ doing: copy.toolActivity.doingGrep || copy.toolActivity.doingList
43
+ },
44
+ options
45
+ );
46
+ }
25
47
  return '';
26
48
  }
@@ -2,7 +2,7 @@ import { makeBlocked, trimText } from '../common.js';
2
2
 
3
3
  export function describeSystemToolActivity(copy, parsed, { done = false, blocked = false } = {}) {
4
4
  if (parsed.base === 'project_index') {
5
- if (blocked) return `${copy.toolActivity.blocked}: project index`;
5
+ if (blocked) return `${copy.toolActivity.blocked}: ${copy.toolActivity.doingProjectIndex}`;
6
6
  return done ? copy.toolActivity.doneProjectIndex : copy.toolActivity.doingProjectIndex;
7
7
  }
8
8
  if (parsed.base === 'file_index') {
@@ -1,7 +1,7 @@
1
1
  export const globPresenter = {
2
2
  prelude: {
3
- en: ({ target }) => (target ? `I'll inspect the ${target} directory first.` : 'I\'ll inspect the relevant directory first.'),
4
- zh: ({ target }) => (target ? `我先查看 ${target} 目录里的内容。` : '我先查看相关目录内容。')
3
+ en: ({ target }) => (target ? `I'll find files matching ${target} first.` : 'I\'ll find the relevant files by pattern first.'),
4
+ zh: ({ target }) => (target ? `我先按模式查找匹配 ${target} 的文件。` : '我先按模式查找相关文件。')
5
5
  },
6
6
  completion: {
7
7
  en: () => 'I have the relevant context now. Do you want me to make the change next, or summarize the findings first?',
@@ -1,7 +1,7 @@
1
1
  export const grepPresenter = {
2
2
  prelude: {
3
- en: () => `I'll search the relevant code first.`,
4
- zh: () => '我先搜索相关代码位置。'
3
+ en: ({ target }) => (target ? `I'll search the codebase for the keyword ${target} first.` : `I'll search the codebase by keyword first.`),
4
+ zh: ({ target }) => (target ? `我先按关键词搜索 ${target} 相关的代码位置。` : '我先按关键词搜索相关代码位置。')
5
5
  },
6
6
  completion: {
7
7
  en: () => 'I found the relevant spots. Do you want me to make the change next, or summarize the findings first?',
@@ -1,7 +1,7 @@
1
1
  export const listPresenter = {
2
2
  prelude: {
3
- en: ({ target }) => (target ? `I'll inspect the ${target} directory first.` : 'I\'ll inspect the relevant directory first.'),
4
- zh: ({ target }) => (target ? `我先查看 ${target} 目录里的内容。` : '我先查看相关目录内容。')
3
+ en: ({ target }) => (target ? `I'll list the contents of ${target} first.` : 'I\'ll list the relevant directory contents first.'),
4
+ zh: ({ target }) => (target ? `我先列出 ${target} 目录内容。` : '我先列出相关目录内容。')
5
5
  },
6
6
  completion: {
7
7
  en: () => 'I have the relevant context now. Do you want me to make the change next, or summarize the findings first?',
@@ -1,7 +1,7 @@
1
1
  export const runPresenter = {
2
2
  prelude: {
3
- en: () => `I'll verify the current project state first.`,
4
- zh: () => '我先检查当前项目状态。'
3
+ en: ({ target }) => (target ? `I'll run ${target} first and check the result.` : `I'll run the relevant command first and check the result.`),
4
+ zh: ({ target }) => (target ? `我先执行 ${target},再看一下结果。` : '我先执行相关命令,再看一下结果。')
5
5
  },
6
6
  completion: {
7
7
  en: () => 'That step is finished. Do you want me to act on the result next, or summarize what it means first?',