ai-worktool 1.0.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/LICENSE.txt +1 -0
- package/README.md +67 -0
- package/dist/agents/chanjet.js +382 -0
- package/dist/agents/index.js +73 -0
- package/dist/agents/jianguoke.js +202 -0
- package/dist/agents/prompt.js +41 -0
- package/dist/agents/toolCall.js +140 -0
- package/dist/cli.js +9 -0
- package/dist/loging.js +110 -0
- package/dist/program.js +114 -0
- package/dist/testAgent.js +141 -0
- package/dist/tools/file.js +543 -0
- package/dist/tools/git.js +204 -0
- package/dist/tools/index.js +71 -0
- package/dist/tools/interface.js +3 -0
- package/dist/tools/javascript.js +183 -0
- package/dist/tools/jest.js +484 -0
- package/dist/tools/mocha.js +107 -0
- package/dist/tools/package.js +381 -0
- package/dist/tools/project.js +561 -0
- package/dist/tools/typescript.js +242 -0
- package/dist/tools/vitest.js +115 -0
- package/dist/user.js +505 -0
- package/dist/view.js +225 -0
- package/package.json +302 -0
@@ -0,0 +1,204 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.clone = clone;
|
7
|
+
exports.add = add;
|
8
|
+
exports.commit = commit;
|
9
|
+
exports.pull = pull;
|
10
|
+
exports.push = push;
|
11
|
+
exports.createBranch = createBranch;
|
12
|
+
exports.switchBranch = switchBranch;
|
13
|
+
exports.getStatus = getStatus;
|
14
|
+
exports.addRemote = addRemote;
|
15
|
+
exports.setUserInfo = setUserInfo;
|
16
|
+
const simple_git_1 = __importDefault(require("simple-git"));
|
17
|
+
const path_1 = require("path");
|
18
|
+
/**
|
19
|
+
* 创建 Git 实例
|
20
|
+
* @param repoPath 仓库路径,默认为当前工作目录
|
21
|
+
*/
|
22
|
+
function createGitInstance(repoPath) {
|
23
|
+
const options = {
|
24
|
+
baseDir: repoPath,
|
25
|
+
binary: 'git',
|
26
|
+
maxConcurrentProcesses: 6
|
27
|
+
};
|
28
|
+
return (0, simple_git_1.default)(options);
|
29
|
+
}
|
30
|
+
/**
|
31
|
+
* 克隆远程仓库到本地
|
32
|
+
* @param remoteUrl 远程仓库URL
|
33
|
+
* @param localPath 本地存储路径
|
34
|
+
* @param branch 要克隆的分支
|
35
|
+
* @param repoPath 仓库根路径
|
36
|
+
*/
|
37
|
+
async function clone(repoPath, remoteUrl, localPath, branch) {
|
38
|
+
const git = createGitInstance(repoPath);
|
39
|
+
try {
|
40
|
+
const clonePath = localPath ? (0, path_1.join)(repoPath, localPath) : repoPath;
|
41
|
+
const options = branch ? ['-b', branch] : [];
|
42
|
+
await git.clone(remoteUrl, clonePath, options);
|
43
|
+
console.log(`成功克隆仓库: ${remoteUrl} 到 ${clonePath}`);
|
44
|
+
}
|
45
|
+
catch (error) {
|
46
|
+
console.log(`克隆仓库失败: ${error}`);
|
47
|
+
throw error;
|
48
|
+
}
|
49
|
+
}
|
50
|
+
/**
|
51
|
+
* 添加文件到暂存区
|
52
|
+
* @param files 文件路径,可以是单个文件或文件数组
|
53
|
+
* @param repoPath 仓库路径
|
54
|
+
*/
|
55
|
+
async function add(files = '.', repoPath) {
|
56
|
+
const git = createGitInstance(repoPath);
|
57
|
+
try {
|
58
|
+
await git.add(files);
|
59
|
+
console.log(`成功添加文件到暂存区: ${Array.isArray(files) ? files.join(', ') : files}`);
|
60
|
+
}
|
61
|
+
catch (error) {
|
62
|
+
console.log(`添加文件失败: ${error}`);
|
63
|
+
throw error;
|
64
|
+
}
|
65
|
+
}
|
66
|
+
/**
|
67
|
+
* 提交代码
|
68
|
+
* @param message 提交信息
|
69
|
+
* @param files 可选的文件路径,指定要提交的文件
|
70
|
+
* @param repoPath 仓库路径
|
71
|
+
*/
|
72
|
+
async function commit(repoPath, message, files) {
|
73
|
+
const git = createGitInstance(repoPath);
|
74
|
+
try {
|
75
|
+
const result = files ? await git.commit(message, files) : await git.commit(message);
|
76
|
+
console.log(`提交成功: ${result?.summary}`);
|
77
|
+
}
|
78
|
+
catch (error) {
|
79
|
+
console.log(`提交失败: ${error}`);
|
80
|
+
throw error;
|
81
|
+
}
|
82
|
+
}
|
83
|
+
/**
|
84
|
+
* 拉取远程分支
|
85
|
+
* @param remote 远程仓库名称,默认为 'origin'
|
86
|
+
* @param branch 分支名称,默认为当前分支
|
87
|
+
* @param repoPath 仓库路径
|
88
|
+
*/
|
89
|
+
async function pull(repoPath, remote = 'origin', branch) {
|
90
|
+
const git = createGitInstance(repoPath);
|
91
|
+
try {
|
92
|
+
const currentBranch = branch || (await git.branchLocal()).current;
|
93
|
+
const result = await git.pull(remote, currentBranch);
|
94
|
+
console.log(`拉取成功: ${result?.summary}`);
|
95
|
+
}
|
96
|
+
catch (error) {
|
97
|
+
console.log(`拉取失败: ${error}`);
|
98
|
+
throw error;
|
99
|
+
}
|
100
|
+
}
|
101
|
+
/**
|
102
|
+
* 推送代码到远程仓库
|
103
|
+
* @param remote 远程仓库名称,默认为 'origin'
|
104
|
+
* @param branch 分支名称,默认为当前分支
|
105
|
+
* @param repoPath 仓库路径
|
106
|
+
*/
|
107
|
+
async function push(repoPath, remote = 'origin', branch) {
|
108
|
+
const git = createGitInstance(repoPath);
|
109
|
+
try {
|
110
|
+
const currentBranch = branch || (await git.branchLocal()).current;
|
111
|
+
await git.push(remote, currentBranch);
|
112
|
+
console.log(`推送成功: ${remote}/${currentBranch}`);
|
113
|
+
}
|
114
|
+
catch (error) {
|
115
|
+
console.log(`推送失败: ${error}`);
|
116
|
+
// 处理常见的推送失败情况(如远程分支有新提交)
|
117
|
+
if (error.message.includes('non-fast-forward')) {
|
118
|
+
console.log('提示: 远程分支有新提交,建议先拉取再推送');
|
119
|
+
}
|
120
|
+
throw error;
|
121
|
+
}
|
122
|
+
}
|
123
|
+
/**
|
124
|
+
* 创建并切换到新分支
|
125
|
+
* @param branchName 新分支名称
|
126
|
+
* @param repoPath 仓库路径
|
127
|
+
*/
|
128
|
+
async function createBranch(branchName, repoPath) {
|
129
|
+
const git = createGitInstance(repoPath);
|
130
|
+
try {
|
131
|
+
await git.checkoutLocalBranch(branchName);
|
132
|
+
console.log(`成功创建并切换到分支: ${branchName}`);
|
133
|
+
}
|
134
|
+
catch (error) {
|
135
|
+
console.log(`创建分支失败: ${error}`);
|
136
|
+
throw error;
|
137
|
+
}
|
138
|
+
}
|
139
|
+
/**
|
140
|
+
* 切换分支
|
141
|
+
* @param branchName 目标分支名称
|
142
|
+
* @param repoPath 仓库路径
|
143
|
+
*/
|
144
|
+
async function switchBranch(branchName, repoPath) {
|
145
|
+
const git = createGitInstance(repoPath);
|
146
|
+
try {
|
147
|
+
await git.checkout(branchName);
|
148
|
+
console.log(`成功切换到分支: ${branchName}`);
|
149
|
+
}
|
150
|
+
catch (error) {
|
151
|
+
console.log(`切换分支失败: ${error}`);
|
152
|
+
throw error;
|
153
|
+
}
|
154
|
+
}
|
155
|
+
/**
|
156
|
+
* 获取当前仓库状态
|
157
|
+
* @param repoPath 仓库路径
|
158
|
+
*/
|
159
|
+
async function getStatus(repoPath) {
|
160
|
+
const git = createGitInstance(repoPath);
|
161
|
+
try {
|
162
|
+
return await git.status();
|
163
|
+
}
|
164
|
+
catch (error) {
|
165
|
+
console.log(`获取仓库状态失败: ${error}`);
|
166
|
+
throw error;
|
167
|
+
}
|
168
|
+
}
|
169
|
+
/**
|
170
|
+
* 添加远程仓库
|
171
|
+
* @param remoteName 远程仓库名称
|
172
|
+
* @param remoteUrl 远程仓库URL
|
173
|
+
* @param repoPath 仓库路径
|
174
|
+
*/
|
175
|
+
async function addRemote(remoteName, remoteUrl, repoPath) {
|
176
|
+
const git = createGitInstance(repoPath);
|
177
|
+
try {
|
178
|
+
await git.addRemote(remoteName, remoteUrl);
|
179
|
+
console.log(`成功添加远程仓库: ${remoteName} -> ${remoteUrl}`);
|
180
|
+
}
|
181
|
+
catch (error) {
|
182
|
+
console.log(`添加远程仓库失败: ${error}`);
|
183
|
+
throw error;
|
184
|
+
}
|
185
|
+
}
|
186
|
+
/**
|
187
|
+
* 设置提交用户信息
|
188
|
+
* @param username 用户名
|
189
|
+
* @param email 邮箱
|
190
|
+
* @param repoPath 仓库路径
|
191
|
+
*/
|
192
|
+
async function setUserInfo(username, email, repoPath) {
|
193
|
+
const git = createGitInstance(repoPath);
|
194
|
+
try {
|
195
|
+
await git.addConfig('user.name', username);
|
196
|
+
await git.addConfig('user.email', email);
|
197
|
+
console.log(`已设置用户信息: ${username} <${email}>`);
|
198
|
+
}
|
199
|
+
catch (error) {
|
200
|
+
console.log(`设置用户信息失败: ${error}`);
|
201
|
+
throw error;
|
202
|
+
}
|
203
|
+
}
|
204
|
+
//# sourceMappingURL=git.js.map
|
@@ -0,0 +1,71 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
|
+
};
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
+
exports.modifyTools = void 0;
|
18
|
+
exports.wait = wait;
|
19
|
+
exports.runTests = runTests;
|
20
|
+
const jest_1 = require("./jest");
|
21
|
+
const vitest_1 = require("./vitest");
|
22
|
+
const mocha_1 = require("./mocha");
|
23
|
+
__exportStar(require("./interface"), exports);
|
24
|
+
__exportStar(require("./file"), exports);
|
25
|
+
// export * from './javascript';
|
26
|
+
// export * from './typescript';
|
27
|
+
__exportStar(require("./jest"), exports);
|
28
|
+
__exportStar(require("./vitest"), exports);
|
29
|
+
__exportStar(require("./mocha"), exports);
|
30
|
+
__exportStar(require("./git"), exports);
|
31
|
+
__exportStar(require("./package"), exports);
|
32
|
+
__exportStar(require("./project"), exports);
|
33
|
+
exports.modifyTools = [
|
34
|
+
'writeFile',
|
35
|
+
'batchModifyLines',
|
36
|
+
'insertLine',
|
37
|
+
'deleteLine',
|
38
|
+
'modifyLine',
|
39
|
+
'renameFile',
|
40
|
+
'initProject',
|
41
|
+
'installDependencies',
|
42
|
+
'createTsConfigFile',
|
43
|
+
'createGitIgnore',
|
44
|
+
'createPackageJson',
|
45
|
+
'createTsConfigJson',
|
46
|
+
'createJestConfig',
|
47
|
+
'createVitestConfig',
|
48
|
+
'createMochaConfig'
|
49
|
+
];
|
50
|
+
async function wait(timerout = 1000) {
|
51
|
+
return new Promise((resolve) => {
|
52
|
+
setTimeout(() => {
|
53
|
+
resolve(true);
|
54
|
+
}, timerout);
|
55
|
+
});
|
56
|
+
}
|
57
|
+
async function runTests(projectRoot, testFramework, coverageDir, configPath) {
|
58
|
+
if (testFramework === 'jest') {
|
59
|
+
return (0, jest_1.runJestTests)(projectRoot, coverageDir, configPath);
|
60
|
+
}
|
61
|
+
else if (testFramework === 'vitest') {
|
62
|
+
return (0, vitest_1.runVitestTests)(projectRoot, coverageDir, configPath);
|
63
|
+
}
|
64
|
+
else if (testFramework === 'mocha') {
|
65
|
+
return (0, mocha_1.runMochaTests)(projectRoot, coverageDir, configPath);
|
66
|
+
}
|
67
|
+
else {
|
68
|
+
throw new Error(`尚未支持 ${testFramework} 测试框架`);
|
69
|
+
}
|
70
|
+
}
|
71
|
+
//# sourceMappingURL=index.js.map
|
@@ -0,0 +1,183 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
15
|
+
}) : function(o, v) {
|
16
|
+
o["default"] = v;
|
17
|
+
});
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
19
|
+
var ownKeys = function(o) {
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
21
|
+
var ar = [];
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
23
|
+
return ar;
|
24
|
+
};
|
25
|
+
return ownKeys(o);
|
26
|
+
};
|
27
|
+
return function (mod) {
|
28
|
+
if (mod && mod.__esModule) return mod;
|
29
|
+
var result = {};
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
31
|
+
__setModuleDefault(result, mod);
|
32
|
+
return result;
|
33
|
+
};
|
34
|
+
})();
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
36
|
+
exports.parseJavaScriptFile = parseJavaScriptFile;
|
37
|
+
exports.extractExportedFunctions = extractExportedFunctions;
|
38
|
+
exports.extractExportedClasses = extractExportedClasses;
|
39
|
+
exports.extractImportedModules = extractImportedModules;
|
40
|
+
exports.loadCodeLine = loadCodeLine;
|
41
|
+
exports.loadCodeLines = loadCodeLines;
|
42
|
+
const fs = __importStar(require("fs"));
|
43
|
+
const acorn = __importStar(require("acorn"));
|
44
|
+
const walk = __importStar(require("acorn-walk"));
|
45
|
+
/**
|
46
|
+
* 解析 JavaScript 文件为 AST
|
47
|
+
* @param filePath 文件路径
|
48
|
+
* @returns AST 对象
|
49
|
+
*/
|
50
|
+
function parseJavaScriptFile(filePath) {
|
51
|
+
if (!fs.existsSync(filePath)) {
|
52
|
+
throw new Error(`文件不存在: ${filePath}`);
|
53
|
+
}
|
54
|
+
const sourceCode = fs.readFileSync(filePath, 'utf-8');
|
55
|
+
try {
|
56
|
+
return acorn.parse(sourceCode, {
|
57
|
+
ecmaVersion: 2020,
|
58
|
+
sourceType: 'module'
|
59
|
+
});
|
60
|
+
}
|
61
|
+
catch (error) {
|
62
|
+
throw new Error(`解析文件失败: ${error.message}`);
|
63
|
+
}
|
64
|
+
}
|
65
|
+
/**
|
66
|
+
* 从 AST 中提取导出的函数
|
67
|
+
* @param ast AST 对象
|
68
|
+
* @returns 导出函数名称数组
|
69
|
+
*/
|
70
|
+
function extractExportedFunctions(filePath) {
|
71
|
+
const ast = typeof filePath === 'string' ? parseJavaScriptFile(filePath) : filePath;
|
72
|
+
const exportedFunctions = [];
|
73
|
+
walk.simple(ast, {
|
74
|
+
// 处理命名函数导出
|
75
|
+
ExportNamedDeclaration(node) {
|
76
|
+
if (node.declaration && node.declaration.type === 'FunctionDeclaration') {
|
77
|
+
exportedFunctions.push(node.declaration.id.name);
|
78
|
+
}
|
79
|
+
// 处理变量导出 (可能是函数)
|
80
|
+
if (node.specifiers) {
|
81
|
+
node.specifiers.forEach((specifier) => {
|
82
|
+
if (specifier.exported.type === 'Identifier') {
|
83
|
+
exportedFunctions.push(specifier.exported.name);
|
84
|
+
}
|
85
|
+
});
|
86
|
+
}
|
87
|
+
},
|
88
|
+
// 处理默认导出的函数
|
89
|
+
ExportDefaultDeclaration(node) {
|
90
|
+
if (node.declaration.type === 'FunctionDeclaration') {
|
91
|
+
exportedFunctions.push(node.declaration.id?.name || 'default');
|
92
|
+
}
|
93
|
+
else if (node.declaration.type === 'Identifier') {
|
94
|
+
exportedFunctions.push(`default (${node.declaration.name})`);
|
95
|
+
}
|
96
|
+
}
|
97
|
+
});
|
98
|
+
return exportedFunctions;
|
99
|
+
}
|
100
|
+
/**
|
101
|
+
* 从 AST 中提取导出的类
|
102
|
+
* @param ast AST 对象
|
103
|
+
* @returns 导出类名称数组
|
104
|
+
*/
|
105
|
+
function extractExportedClasses(filePath) {
|
106
|
+
const ast = typeof filePath === 'string' ? parseJavaScriptFile(filePath) : filePath;
|
107
|
+
const exportedClasses = [];
|
108
|
+
walk.simple(ast, {
|
109
|
+
// 处理命名类导出
|
110
|
+
ExportNamedDeclaration(node) {
|
111
|
+
if (node.declaration && node.declaration.type === 'ClassDeclaration') {
|
112
|
+
exportedClasses.push(node.declaration.id.name);
|
113
|
+
}
|
114
|
+
// 处理变量导出 (可能是类)
|
115
|
+
if (node.specifiers) {
|
116
|
+
node.specifiers.forEach((specifier) => {
|
117
|
+
if (specifier.exported.type === 'Identifier') {
|
118
|
+
exportedClasses.push(specifier.exported.name);
|
119
|
+
}
|
120
|
+
});
|
121
|
+
}
|
122
|
+
},
|
123
|
+
// 处理默认导出的类
|
124
|
+
ExportDefaultDeclaration(node) {
|
125
|
+
if (node.declaration.type === 'ClassDeclaration') {
|
126
|
+
exportedClasses.push(node.declaration.id?.name || 'default');
|
127
|
+
}
|
128
|
+
else if (node.declaration.type === 'Identifier') {
|
129
|
+
exportedClasses.push(`default (${node.declaration.name})`);
|
130
|
+
}
|
131
|
+
}
|
132
|
+
});
|
133
|
+
return exportedClasses;
|
134
|
+
}
|
135
|
+
/**
|
136
|
+
* 从 AST 中提取导入的模块
|
137
|
+
* @param ast AST 对象
|
138
|
+
* @returns 导入模块路径数组
|
139
|
+
*/
|
140
|
+
function extractImportedModules(filePath) {
|
141
|
+
const ast = typeof filePath === 'string' ? parseJavaScriptFile(filePath) : filePath;
|
142
|
+
const importedModules = [];
|
143
|
+
walk.simple(ast, {
|
144
|
+
ImportDeclaration(node) {
|
145
|
+
importedModules.push(node.source.value);
|
146
|
+
}
|
147
|
+
});
|
148
|
+
return importedModules;
|
149
|
+
}
|
150
|
+
/**
|
151
|
+
* 从文件中加载指定行的代码
|
152
|
+
* @param filePath 文件路径
|
153
|
+
* @param lineNumber 行号 (从1开始)
|
154
|
+
* @returns 指定行的代码
|
155
|
+
*/
|
156
|
+
function loadCodeLine(filePath, lineNumber) {
|
157
|
+
if (!fs.existsSync(filePath)) {
|
158
|
+
throw new Error(`文件不存在: ${filePath}`);
|
159
|
+
}
|
160
|
+
const lines = fs.readFileSync(filePath, 'utf-8').split('\n');
|
161
|
+
if (lineNumber < 1 || lineNumber > lines.length) {
|
162
|
+
throw new Error(`行号超出范围 (1-${lines.length})`);
|
163
|
+
}
|
164
|
+
return lines[lineNumber - 1];
|
165
|
+
}
|
166
|
+
/**
|
167
|
+
* 从文件中加载指定范围的代码行
|
168
|
+
* @param filePath 文件路径
|
169
|
+
* @param startLine 起始行号 (从1开始)
|
170
|
+
* @param endLine 结束行号 (从1开始)
|
171
|
+
* @returns 指定范围的代码
|
172
|
+
*/
|
173
|
+
function loadCodeLines(filePath, startLine, endLine) {
|
174
|
+
if (!fs.existsSync(filePath)) {
|
175
|
+
throw new Error(`文件不存在: ${filePath}`);
|
176
|
+
}
|
177
|
+
const lines = fs.readFileSync(filePath, 'utf-8').split('\n');
|
178
|
+
if (startLine < 1 || endLine > lines.length || startLine > endLine) {
|
179
|
+
throw new Error(`行号范围无效 (1-${lines.length})`);
|
180
|
+
}
|
181
|
+
return lines.slice(startLine - 1, endLine).join('\n');
|
182
|
+
}
|
183
|
+
//# sourceMappingURL=javascript.js.map
|