explorbot 0.4.5 → 0.4.6
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/boat/api-tester/src/apibot.ts +18 -2
- package/boat/api-tester/src/cli.ts +85 -274
- package/boat/api-tester/src/commands/api-command.ts +10 -0
- package/boat/api-tester/src/commands/explore-command.ts +52 -0
- package/boat/api-tester/src/commands/init-command.ts +119 -0
- package/boat/api-tester/src/commands/know-command.ts +44 -0
- package/boat/api-tester/src/commands/plan-command.ts +42 -0
- package/boat/api-tester/src/commands/test-command.ts +54 -0
- package/dist/boat/api-tester/src/apibot.js +14 -1
- package/dist/boat/api-tester/src/cli.js +87 -243
- package/dist/boat/api-tester/src/commands/api-command.js +7 -0
- package/dist/boat/api-tester/src/commands/explore-command.js +41 -0
- package/dist/boat/api-tester/src/commands/init-command.js +88 -0
- package/dist/boat/api-tester/src/commands/know-command.js +39 -0
- package/dist/boat/api-tester/src/commands/plan-command.js +37 -0
- package/dist/boat/api-tester/src/commands/test-command.js +45 -0
- package/dist/package.json +4 -4
- package/dist/src/ai/researcher/deep-analysis.d.ts +1 -1
- package/dist/src/ai/researcher/deep-analysis.js +14 -6
- package/dist/src/ai/tools.d.ts +1 -1
- package/dist/src/ai/tools.js +15 -10
- package/dist/src/api/spec-reader.d.ts +1 -0
- package/dist/src/api/spec-reader.js +93 -1
- package/dist/src/commands/base-command.d.ts +3 -3
- package/dist/src/commands/init-command.d.ts +3 -0
- package/dist/src/commands/init-command.js +6 -3
- package/dist/src/explorer.d.ts +1 -1
- package/dist/src/explorer.js +1 -1
- package/dist/src/utils/html-diff.js +4 -1
- package/docs/api-testing/basics.md +26 -2
- package/docs/superpowers/specs/2026-09-09-pagination-rule-design.md +317 -0
- package/package.json +4 -4
- package/src/ai/researcher/deep-analysis.ts +13 -6
- package/src/ai/tools.ts +15 -11
- package/src/api/spec-reader.ts +106 -1
- package/src/commands/base-command.ts +3 -3
- package/src/commands/init-command.ts +6 -3
- package/src/explorer.ts +1 -1
- package/src/utils/html-diff.ts +3 -1
|
@@ -2,10 +2,10 @@ import { existsSync, mkdirSync } from 'node:fs';
|
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
import { AIProvider } from '../../../src/ai/provider.ts';
|
|
4
4
|
import { RequestStore } from '../../../src/api/request-store.ts';
|
|
5
|
-
import { extractEndpointDefinition, loadSpec, searchEndpoints, validateSpecs } from '../../../src/api/spec-reader.ts';
|
|
5
|
+
import { extractEndpointDefinition, loadSpec, resolveEndpoints, searchEndpoints, validateSpecs } from '../../../src/api/spec-reader.ts';
|
|
6
6
|
import { KnowledgeTracker } from '../../../src/knowledge-tracker.ts';
|
|
7
7
|
import { Reporter } from '../../../src/reporter.ts';
|
|
8
|
-
import { Plan } from '../../../src/test-plan.ts';
|
|
8
|
+
import { Plan, type Test } from '../../../src/test-plan.ts';
|
|
9
9
|
import { setVerboseMode, tag } from '../../../src/utils/logger.ts';
|
|
10
10
|
import { Chief } from './ai/chief.ts';
|
|
11
11
|
import { Curler } from './ai/curler.ts';
|
|
@@ -170,6 +170,18 @@ export class ApiBot {
|
|
|
170
170
|
return this.configParser;
|
|
171
171
|
}
|
|
172
172
|
|
|
173
|
+
getOptions(): ApibotOptions {
|
|
174
|
+
return this.options;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
async runTest(test: Test): Promise<{ success: boolean }> {
|
|
178
|
+
return this.agentCurler().test(test, {
|
|
179
|
+
specDefinition: this.tryGetEndpointDefinition(test.startUrl!),
|
|
180
|
+
baseEndpoint: this.config.api.baseEndpoint,
|
|
181
|
+
searchSpec: (query: string) => this.searchSpec(query),
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
|
|
173
185
|
getRequestState(): RequestStore {
|
|
174
186
|
return this.requestState;
|
|
175
187
|
}
|
|
@@ -178,6 +190,10 @@ export class ApiBot {
|
|
|
178
190
|
return extractEndpointDefinition(this.apiSpec, endpoint, this.config.api.baseEndpoint);
|
|
179
191
|
}
|
|
180
192
|
|
|
193
|
+
expandEndpoints(pattern: string): string[] {
|
|
194
|
+
return resolveEndpoints(this.apiSpec, this.configParser.resolveEndpointPath(pattern), this.config.api.baseEndpoint);
|
|
195
|
+
}
|
|
196
|
+
|
|
181
197
|
searchSpec(query: string): string {
|
|
182
198
|
return searchEndpoints(this.apiSpec, query, this.config.api.baseEndpoint);
|
|
183
199
|
}
|
|
@@ -1,100 +1,33 @@
|
|
|
1
|
-
import fs from 'node:fs';
|
|
2
|
-
import path from 'node:path';
|
|
3
1
|
import { Command } from 'commander';
|
|
4
2
|
import { ConfigCommand } from '../../../src/commands/config-command.ts';
|
|
5
3
|
import { RecommendedModelsCommand } from '../../../src/commands/recommended-models-command.ts';
|
|
6
4
|
import { listSites } from '../../../src/global-config.ts';
|
|
7
5
|
import { setPreserveConsoleLogs } from '../../../src/utils/logger.ts';
|
|
8
|
-
import { getStyles } from './ai/chief/styles.ts';
|
|
9
6
|
import { ApiBot, type ApibotOptions } from './apibot.ts';
|
|
7
|
+
import { ExploreCommand } from './commands/explore-command.ts';
|
|
8
|
+
import { runInit } from './commands/init-command.ts';
|
|
9
|
+
import { KnowCommand } from './commands/know-command.ts';
|
|
10
|
+
import { PlanCommand } from './commands/plan-command.ts';
|
|
11
|
+
import { TestCommand } from './commands/test-command.ts';
|
|
10
12
|
import { ApibotConfigParser } from './config.ts';
|
|
11
13
|
|
|
12
|
-
function buildOptions(options: any): ApibotOptions {
|
|
13
|
-
return {
|
|
14
|
-
verbose: options.verbose || options.debug,
|
|
15
|
-
config: options.config,
|
|
16
|
-
path: options.path,
|
|
17
|
-
baseEndpoint: options.endpoint,
|
|
18
|
-
spec: options.spec,
|
|
19
|
-
header: options.header,
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function addCommonOptions(cmd: Command): Command {
|
|
24
|
-
return cmd
|
|
25
|
-
.option('-v, --verbose', 'Enable verbose logging')
|
|
26
|
-
.option('--debug', 'Enable debug logging')
|
|
27
|
-
.option('-c, --config <path>', 'Path to configuration file')
|
|
28
|
-
.option('-p, --path <path>', 'Working directory path')
|
|
29
|
-
.option('--endpoint <url>', 'Base API endpoint to test (env: EXPLORBOT_URL)')
|
|
30
|
-
.option('--spec <path>', 'OpenAPI spec file or URL (env: EXPLORBOT_API_SPEC)')
|
|
31
|
-
.option('-H, --header <header>', 'Header sent with every request, as "Name: value". Repeatable (env: EXPLORBOT_API_HEADERS)', (value: string, previous: string[] = []) => [...previous, value]);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function selectTests(tests: any[], index?: string): any[] {
|
|
35
|
-
if (!index || index === '*' || index === 'all') {
|
|
36
|
-
return tests.filter((t) => t.status === 'pending');
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
const rangeMatch = index.match(/^(\d+)-(\d+)$/);
|
|
40
|
-
if (rangeMatch) {
|
|
41
|
-
const start = Number.parseInt(rangeMatch[1]) - 1;
|
|
42
|
-
const end = Number.parseInt(rangeMatch[2]);
|
|
43
|
-
return tests.slice(start, end);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
if (index.includes(',')) {
|
|
47
|
-
const indices = index.split(',').map((i) => Number.parseInt(i.trim()) - 1);
|
|
48
|
-
return indices.map((i) => tests[i]).filter(Boolean);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
const num = Number.parseInt(index);
|
|
52
|
-
if (!Number.isNaN(num) && tests[num - 1]) {
|
|
53
|
-
return [tests[num - 1]];
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
return tests.filter((t) => t.status === 'pending');
|
|
57
|
-
}
|
|
58
|
-
|
|
59
14
|
export function createApiCommands(name = 'api'): Command {
|
|
60
15
|
const cmd = new Command(name);
|
|
61
16
|
cmd.description('AI-powered API testing tool');
|
|
62
17
|
|
|
63
|
-
addCommonOptions(cmd.command('plan <endpoint>').description('Generate test plan for an API endpoint')
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
await bot
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
process.exit(1);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
console.log(`\nPlan: ${plan.title} (${plan.tests.length} tests)\n`);
|
|
78
|
-
plan.tests.forEach((test, i) => {
|
|
79
|
-
console.log(` ${i + 1}. [${test.priority}] ${test.scenario}`);
|
|
18
|
+
addCommonOptions(cmd.command('plan <endpoint>').description('Generate test plan for an API endpoint'))
|
|
19
|
+
.option('--style <style>', 'Planning style: basename of a file in rules/chief/styles/')
|
|
20
|
+
.option('--fresh', 'Start planning from scratch')
|
|
21
|
+
.action(async (endpoint, options) => {
|
|
22
|
+
await run(name, options, endpoint, async (bot) => {
|
|
23
|
+
const command = new PlanCommand(bot);
|
|
24
|
+
command.prefix = name;
|
|
25
|
+
command.style = options.style;
|
|
26
|
+
command.fresh = !!options.fresh;
|
|
27
|
+
await command.execute(endpoint);
|
|
28
|
+
return 0;
|
|
80
29
|
});
|
|
81
|
-
|
|
82
|
-
const savedPath = bot.savePlan();
|
|
83
|
-
if (savedPath) {
|
|
84
|
-
console.log(`\nSaved to: ${savedPath}`);
|
|
85
|
-
console.log('\nRun tests:');
|
|
86
|
-
console.log(` ${name} test ${savedPath} 1 # run first test`);
|
|
87
|
-
console.log(` ${name} test ${savedPath} 1-3 # run tests 1 to 3`);
|
|
88
|
-
console.log(` ${name} test ${savedPath} * # run all tests`);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
await bot.stop();
|
|
92
|
-
process.exit(0);
|
|
93
|
-
} catch (error) {
|
|
94
|
-
console.error('Failed:', error instanceof Error ? error.message : 'Unknown error');
|
|
95
|
-
process.exit(1);
|
|
96
|
-
}
|
|
97
|
-
});
|
|
30
|
+
});
|
|
98
31
|
|
|
99
32
|
addCommonOptions(cmd.command('config [endpoint]').description('Show models, config file and paths used by this run'))
|
|
100
33
|
.option('--json', 'Print the resolved config as JSON')
|
|
@@ -116,87 +49,22 @@ export function createApiCommands(name = 'api'): Command {
|
|
|
116
49
|
RecommendedModelsCommand.register(cmd);
|
|
117
50
|
|
|
118
51
|
addCommonOptions(cmd.command('test <planfile> [index]').description('Execute tests from a plan file. Index: 1, 1-3, *')).action(async (planfile, index, options) => {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
await
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
const tests = selectTests(plan.tests, index);
|
|
128
|
-
console.log(`Running ${tests.length} test(s)\n`);
|
|
129
|
-
|
|
130
|
-
let passed = 0;
|
|
131
|
-
let failed = 0;
|
|
132
|
-
|
|
133
|
-
for (const test of tests) {
|
|
134
|
-
const specDefinition = bot.tryGetEndpointDefinition(test.startUrl);
|
|
135
|
-
const result = await bot.agentCurler().test(test, {
|
|
136
|
-
specDefinition,
|
|
137
|
-
baseEndpoint: bot.getConfig().api.baseEndpoint,
|
|
138
|
-
searchSpec: (query) => bot.searchSpec(query),
|
|
139
|
-
});
|
|
140
|
-
if (result.success) passed++;
|
|
141
|
-
else failed++;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
bot.savePlan();
|
|
145
|
-
|
|
146
|
-
console.log(`\nResults: ${passed} passed, ${failed} failed out of ${tests.length}`);
|
|
147
|
-
await bot.stop();
|
|
148
|
-
process.exit(failed > 0 ? 1 : 0);
|
|
149
|
-
} catch (error) {
|
|
150
|
-
console.error('Failed:', error instanceof Error ? error.message : 'Unknown error');
|
|
151
|
-
process.exit(1);
|
|
152
|
-
}
|
|
52
|
+
await run(name, options, undefined, async (bot) => {
|
|
53
|
+
const command = new TestCommand(bot);
|
|
54
|
+
command.index = index;
|
|
55
|
+
await command.execute(planfile);
|
|
56
|
+
if (command.failed) return 1;
|
|
57
|
+
return 0;
|
|
58
|
+
});
|
|
153
59
|
});
|
|
154
60
|
|
|
155
|
-
addCommonOptions(cmd.command('explore <endpoint>').description('Full cycle: plan
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
const styles = Object.keys(getStyles());
|
|
163
|
-
let totalPassed = 0;
|
|
164
|
-
let totalFailed = 0;
|
|
165
|
-
let totalTests = 0;
|
|
166
|
-
|
|
167
|
-
for (const style of styles) {
|
|
168
|
-
console.log(`\n=== Style: ${style} ===\n`);
|
|
169
|
-
|
|
170
|
-
const plan = await bot.plan(endpoint, { style, fresh: true });
|
|
171
|
-
if (!plan?.tests.length) {
|
|
172
|
-
console.log(`No tests generated for style: ${style}`);
|
|
173
|
-
continue;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
const pending = plan.getPendingTests();
|
|
177
|
-
for (const test of pending) {
|
|
178
|
-
const specDefinition = bot.tryGetEndpointDefinition(test.startUrl);
|
|
179
|
-
const result = await bot.agentCurler().test(test, {
|
|
180
|
-
specDefinition,
|
|
181
|
-
baseEndpoint: bot.getConfig().api.baseEndpoint,
|
|
182
|
-
searchSpec: (query) => bot.searchSpec(query),
|
|
183
|
-
});
|
|
184
|
-
totalTests++;
|
|
185
|
-
if (result.success) totalPassed++;
|
|
186
|
-
else totalFailed++;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
bot.savePlan(style);
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
console.log('\n=== Final Results ===');
|
|
193
|
-
console.log(`Total: ${totalTests} tests, ${totalPassed} passed, ${totalFailed} failed`);
|
|
194
|
-
await bot.stop();
|
|
195
|
-
process.exit(totalFailed > 0 ? 1 : 0);
|
|
196
|
-
} catch (error) {
|
|
197
|
-
console.error('Failed:', error instanceof Error ? error.message : 'Unknown error');
|
|
198
|
-
process.exit(1);
|
|
199
|
-
}
|
|
61
|
+
addCommonOptions(cmd.command('explore <endpoint>').description('Full cycle: plan, execute tests, re-plan. Use * to cover many endpoints, or the base endpoint for all of them')).action(async (endpoint, options) => {
|
|
62
|
+
await run(name, options, endpoint, async (bot) => {
|
|
63
|
+
const command = new ExploreCommand(bot);
|
|
64
|
+
await command.execute(endpoint);
|
|
65
|
+
if (command.result.failed) return 1;
|
|
66
|
+
return 0;
|
|
67
|
+
});
|
|
200
68
|
});
|
|
201
69
|
|
|
202
70
|
cmd
|
|
@@ -204,87 +72,11 @@ export function createApiCommands(name = 'api'): Command {
|
|
|
204
72
|
.description('Initialize a new apibot project with configuration')
|
|
205
73
|
.option('-f, --force', 'Overwrite existing config file')
|
|
206
74
|
.option('-p, --path <path>', 'Working directory for initialization')
|
|
75
|
+
.option('--provider <name>', 'AI provider written into the config')
|
|
76
|
+
.option('--endpoint <url>', 'Base API endpoint, with --spec skips the questions')
|
|
77
|
+
.option('--spec <path>', 'OpenAPI spec file or URL')
|
|
207
78
|
.action(async (options) => {
|
|
208
|
-
|
|
209
|
-
if (options.path) {
|
|
210
|
-
const resolvedPath = path.resolve(options.path);
|
|
211
|
-
fs.mkdirSync(resolvedPath, { recursive: true });
|
|
212
|
-
process.chdir(resolvedPath);
|
|
213
|
-
console.log(`Working in: ${resolvedPath}`);
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
const configPath = path.resolve('apibot.config.ts');
|
|
217
|
-
|
|
218
|
-
if (fs.existsSync(configPath) && !options.force) {
|
|
219
|
-
console.log(`Config file already exists: ${configPath}`);
|
|
220
|
-
console.log('Use --force to overwrite.');
|
|
221
|
-
process.exit(1);
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
const rl = await import('node:readline');
|
|
225
|
-
const iface = rl.createInterface({ input: process.stdin, output: process.stdout });
|
|
226
|
-
const ask = (q: string, fallback = ''): Promise<string> => new Promise((resolve) => iface.question(q, (a: string) => resolve(a.trim() || fallback)));
|
|
227
|
-
|
|
228
|
-
console.log('Apibot — API Testing Tool Setup\n');
|
|
229
|
-
|
|
230
|
-
const baseEndpoint = await ask('Base API endpoint (e.g., https://api.example.com/v1): ');
|
|
231
|
-
if (!baseEndpoint) {
|
|
232
|
-
console.error('Base endpoint is required.');
|
|
233
|
-
iface.close();
|
|
234
|
-
process.exit(1);
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
const spec = await ask('OpenAPI spec file or URL (e.g., openapi.yaml or https://.../ — or press Enter to skip): ');
|
|
238
|
-
const knowledge = await ask('Describe your API (auth method, data formats, special rules — or press Enter to skip): ');
|
|
239
|
-
|
|
240
|
-
iface.close();
|
|
241
|
-
|
|
242
|
-
const specLine = spec ? `\n spec: ['${spec}'],` : '';
|
|
243
|
-
|
|
244
|
-
const configContent = `import { openai } from '@ai-sdk/openai';
|
|
245
|
-
|
|
246
|
-
export default {
|
|
247
|
-
ai: {
|
|
248
|
-
model: openai('gpt-4o'),
|
|
249
|
-
},
|
|
250
|
-
api: {
|
|
251
|
-
baseEndpoint: '${baseEndpoint}',${specLine}
|
|
252
|
-
headers: {
|
|
253
|
-
// 'Authorization': 'Bearer <token>',
|
|
254
|
-
},
|
|
255
|
-
// bootstrap: async ({ headers, baseEndpoint }) => {
|
|
256
|
-
// // Run before tests — e.g. obtain auth token
|
|
257
|
-
// // Return headers to merge: { Authorization: 'Bearer ...' }
|
|
258
|
-
// },
|
|
259
|
-
// teardown: async ({ headers, baseEndpoint }) => {
|
|
260
|
-
// // Run after tests — e.g. cleanup test data
|
|
261
|
-
// },
|
|
262
|
-
},
|
|
263
|
-
dirs: {
|
|
264
|
-
output: 'output',
|
|
265
|
-
knowledge: 'knowledge',
|
|
266
|
-
},
|
|
267
|
-
};
|
|
268
|
-
`;
|
|
269
|
-
|
|
270
|
-
fs.writeFileSync(configPath, configContent, 'utf8');
|
|
271
|
-
console.log(`\nCreated: ${configPath}`);
|
|
272
|
-
|
|
273
|
-
fs.mkdirSync('output', { recursive: true });
|
|
274
|
-
fs.mkdirSync('knowledge', { recursive: true });
|
|
275
|
-
|
|
276
|
-
if (knowledge) {
|
|
277
|
-
const knowledgePath = path.resolve('knowledge', 'general.md');
|
|
278
|
-
fs.writeFileSync(knowledgePath, `---\nendpoint: "*"\n---\n${knowledge}\n`, 'utf8');
|
|
279
|
-
console.log(`Created: ${knowledgePath}`);
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
console.log('\nNext steps:');
|
|
283
|
-
console.log('1. Edit apibot.config.ts — set your AI provider and API headers');
|
|
284
|
-
console.log(`2. Add API knowledge: ${name} know /users "CRUD endpoint for user management"`);
|
|
285
|
-
console.log(`3. Plan tests: ${name} plan /users`);
|
|
286
|
-
|
|
287
|
-
if (process.cwd() !== originalCwd) process.chdir(originalCwd);
|
|
79
|
+
await runInit({ ...options, baseEndpoint: options.endpoint, prefix: name });
|
|
288
80
|
});
|
|
289
81
|
|
|
290
82
|
cmd
|
|
@@ -294,42 +86,61 @@ export default {
|
|
|
294
86
|
.option('-c, --config <path>', 'Path to configuration file')
|
|
295
87
|
.option('-p, --path <path>', 'Working directory path')
|
|
296
88
|
.action(async (endpoint, description, options) => {
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
description = await new Promise<string>((resolve) => iface.question(`Describe ${endpoint}: `, (a: string) => resolve(a.trim())));
|
|
301
|
-
iface.close();
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
if (!description) {
|
|
305
|
-
console.error('Description is required.');
|
|
306
|
-
process.exit(1);
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
let knowledgeDir = 'knowledge';
|
|
89
|
+
const command = new KnowCommand(new ApiBot(buildOptions(options)));
|
|
90
|
+
command.prefix = name;
|
|
91
|
+
command.knowledge = description || (await askDescription(endpoint));
|
|
310
92
|
try {
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
if (options.path) knowledgeDir = path.join(path.resolve(options.path), 'knowledge');
|
|
93
|
+
await command.execute(endpoint);
|
|
94
|
+
} catch (error) {
|
|
95
|
+
console.error(error instanceof Error ? error.message : 'Unknown error');
|
|
96
|
+
process.exit(1);
|
|
316
97
|
}
|
|
98
|
+
});
|
|
317
99
|
|
|
318
|
-
|
|
100
|
+
return cmd;
|
|
101
|
+
}
|
|
319
102
|
|
|
320
|
-
|
|
321
|
-
|
|
103
|
+
async function run(name: string, options: any, endpoint: string | undefined, body: (bot: ApiBot) => Promise<number>): Promise<void> {
|
|
104
|
+
setPreserveConsoleLogs(true);
|
|
105
|
+
try {
|
|
106
|
+
if (endpoint && URL.canParse(endpoint)) options.endpoint ||= endpoint;
|
|
107
|
+
const bot = new ApiBot({ ...buildOptions(options), endpoint });
|
|
108
|
+
await bot.start();
|
|
109
|
+
const code = await body(bot);
|
|
110
|
+
await bot.stop();
|
|
111
|
+
process.exit(code);
|
|
112
|
+
} catch (error) {
|
|
113
|
+
console.error('Failed:', error instanceof Error ? error.message : 'Unknown error');
|
|
114
|
+
process.exit(1);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
322
117
|
|
|
323
|
-
|
|
118
|
+
async function askDescription(endpoint: string): Promise<string> {
|
|
119
|
+
const rl = await import('node:readline');
|
|
120
|
+
const iface = rl.createInterface({ input: process.stdin, output: process.stdout });
|
|
121
|
+
const answer = await new Promise<string>((resolve) => iface.question(`Describe ${endpoint}: `, (text: string) => resolve(text.trim())));
|
|
122
|
+
iface.close();
|
|
123
|
+
return answer;
|
|
124
|
+
}
|
|
324
125
|
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
126
|
+
function buildOptions(options: any): ApibotOptions {
|
|
127
|
+
return {
|
|
128
|
+
verbose: options.verbose || options.debug,
|
|
129
|
+
config: options.config,
|
|
130
|
+
path: options.path,
|
|
131
|
+
baseEndpoint: options.endpoint,
|
|
132
|
+
spec: options.spec,
|
|
133
|
+
header: options.header,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
333
136
|
|
|
334
|
-
|
|
137
|
+
function addCommonOptions(cmd: Command): Command {
|
|
138
|
+
return cmd
|
|
139
|
+
.option('-v, --verbose', 'Enable verbose logging')
|
|
140
|
+
.option('--debug', 'Enable debug logging')
|
|
141
|
+
.option('-c, --config <path>', 'Path to configuration file')
|
|
142
|
+
.option('-p, --path <path>', 'Working directory path')
|
|
143
|
+
.option('--endpoint <url>', 'Base API endpoint to test (env: EXPLORBOT_URL)')
|
|
144
|
+
.option('--spec <path>', 'OpenAPI spec file or URL (env: EXPLORBOT_API_SPEC)')
|
|
145
|
+
.option('-H, --header <header>', 'Header sent with every request, as "Name: value". Repeatable (env: EXPLORBOT_API_HEADERS)', (value: string, previous: string[] = []) => [...previous, value]);
|
|
335
146
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { BaseCommand } from '../../../../src/commands/base-command.ts';
|
|
2
|
+
import type { ApiBot } from '../apibot.ts';
|
|
3
|
+
|
|
4
|
+
export abstract class ApiCommand extends BaseCommand<ApiBot> {
|
|
5
|
+
prefix = 'apibot';
|
|
6
|
+
|
|
7
|
+
protected get bot(): ApiBot {
|
|
8
|
+
return this.explorBot;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import figureSet from 'figures';
|
|
2
|
+
import { tag } from '../../../../src/utils/logger.ts';
|
|
3
|
+
import { getStyles } from '../ai/chief/styles.ts';
|
|
4
|
+
import { ApiCommand } from './api-command.ts';
|
|
5
|
+
|
|
6
|
+
export class ExploreCommand extends ApiCommand {
|
|
7
|
+
name = 'explore';
|
|
8
|
+
description = 'Full cycle: plan, execute tests, re-plan. Use * to cover many endpoints, or the base endpoint for all of them';
|
|
9
|
+
result: ExploreResult = { tests: 0, passed: 0, failed: 0 };
|
|
10
|
+
|
|
11
|
+
async execute(endpoint: string): Promise<void> {
|
|
12
|
+
const styles = Object.keys(getStyles());
|
|
13
|
+
const endpoints = this.bot.expandEndpoints(endpoint);
|
|
14
|
+
|
|
15
|
+
for (const [index, target] of endpoints.entries()) {
|
|
16
|
+
let runStyles = [styles[index % styles.length]];
|
|
17
|
+
if (endpoints.length === 1) runStyles = styles;
|
|
18
|
+
if (endpoints.length > 1) tag('info').log(`Endpoint ${index + 1}/${endpoints.length}: ${target}`);
|
|
19
|
+
|
|
20
|
+
for (const style of runStyles) {
|
|
21
|
+
await this.runStyle(target, style);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
tag('info').log(`${figureSet.tick} ${this.result.tests} tests completed: ${this.result.passed} passed, ${this.result.failed} failed`);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
private async runStyle(endpoint: string, style: string): Promise<void> {
|
|
29
|
+
tag('info').log(`Planning style: ${style}`);
|
|
30
|
+
|
|
31
|
+
const plan = await this.bot.plan(endpoint, { style, fresh: true });
|
|
32
|
+
if (!plan?.tests.length) {
|
|
33
|
+
tag('warning').log(`No tests generated for style: ${style}`);
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
for (const test of plan.getPendingTests()) {
|
|
38
|
+
const outcome = await this.bot.runTest(test);
|
|
39
|
+
this.result.tests++;
|
|
40
|
+
if (outcome.success) this.result.passed++;
|
|
41
|
+
else this.result.failed++;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
this.bot.savePlan(style);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface ExploreResult {
|
|
49
|
+
tests: number;
|
|
50
|
+
passed: number;
|
|
51
|
+
failed: number;
|
|
52
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import chalk from 'chalk';
|
|
4
|
+
import { envTemplate, modelLines } from '../../../../src/commands/init-command.ts';
|
|
5
|
+
import { missingModelRoles } from '../../../../src/config.ts';
|
|
6
|
+
import { log, tag } from '../../../../src/utils/logger.ts';
|
|
7
|
+
|
|
8
|
+
export async function runInit(options: InitOptions): Promise<void> {
|
|
9
|
+
const provider = options.provider || 'openrouter';
|
|
10
|
+
const originalCwd = process.cwd();
|
|
11
|
+
|
|
12
|
+
if (options.path) {
|
|
13
|
+
const dir = path.resolve(options.path);
|
|
14
|
+
mkdirSync(dir, { recursive: true });
|
|
15
|
+
process.chdir(dir);
|
|
16
|
+
log(`Working in directory: ${dir}`);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const configPath = path.resolve('apibot.config.js');
|
|
20
|
+
if (existsSync(configPath) && !options.force) {
|
|
21
|
+
log(`Config file already exists: ${configPath}`);
|
|
22
|
+
log('Use --force to overwrite existing file');
|
|
23
|
+
process.exit(1);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const answers = await ask(options);
|
|
27
|
+
if (!answers.baseEndpoint) {
|
|
28
|
+
tag('error').log('Base endpoint is required.');
|
|
29
|
+
process.exit(1);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (!answers.spec) {
|
|
33
|
+
tag('error').log('OpenAPI spec is required. Chief plans from it and Curler looks up schemas in it.');
|
|
34
|
+
process.exit(1);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
writeFileSync(configPath, configTemplate(provider, answers.baseEndpoint, answers.spec), 'utf8');
|
|
38
|
+
log(`Created config file: ${configPath}`);
|
|
39
|
+
|
|
40
|
+
const envPath = path.resolve('.env');
|
|
41
|
+
if (!existsSync(envPath)) {
|
|
42
|
+
writeFileSync(envPath, `${envTemplate(provider)}\n`, 'utf8');
|
|
43
|
+
log(`Created env file: ${envPath}`);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
mkdirSync('output', { recursive: true });
|
|
47
|
+
mkdirSync('knowledge', { recursive: true });
|
|
48
|
+
|
|
49
|
+
if (answers.knowledge) {
|
|
50
|
+
const knowledgePath = path.resolve('knowledge', 'general.md');
|
|
51
|
+
writeFileSync(knowledgePath, `---\nendpoint: "*"\n---\n${answers.knowledge}\n`, 'utf8');
|
|
52
|
+
log(`Created knowledge file: ${knowledgePath}`);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const missing = missingModelRoles(provider);
|
|
56
|
+
if (missing.length) {
|
|
57
|
+
tag('warning').log(`No recommended ${missing.join(' and ')} for ${provider} — set the model ids in ${configPath}`);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
log('');
|
|
61
|
+
log('Next steps:');
|
|
62
|
+
log('1. Add your provider API key to .env');
|
|
63
|
+
log('2. Describe the API so the plans match it');
|
|
64
|
+
tag('substep').log(chalk.yellow(`${options.prefix} know /users "CRUD endpoint for user management"`));
|
|
65
|
+
log('3. Plan and run tests for one endpoint');
|
|
66
|
+
tag('substep').log(chalk.yellow(`${options.prefix} explore /users`));
|
|
67
|
+
|
|
68
|
+
if (process.cwd() !== originalCwd) process.chdir(originalCwd);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function configTemplate(provider: string, baseEndpoint: string, spec: string): string {
|
|
72
|
+
return `// Models are written as 'provider/model-id' so they resolve without a local node_modules.
|
|
73
|
+
// https://github.com/testomatio/explorbot/blob/main/docs/basics/providers.md
|
|
74
|
+
|
|
75
|
+
export default {
|
|
76
|
+
ai: {
|
|
77
|
+
${modelLines(provider, ['model', 'agenticModel'])}
|
|
78
|
+
},
|
|
79
|
+
|
|
80
|
+
api: {
|
|
81
|
+
baseEndpoint: '${baseEndpoint}',
|
|
82
|
+
spec: ['${spec}'],
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
`;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async function ask(options: InitOptions): Promise<Answers> {
|
|
89
|
+
if (options.baseEndpoint) {
|
|
90
|
+
return { baseEndpoint: options.baseEndpoint, spec: options.spec || '', knowledge: '' };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const rl = await import('node:readline');
|
|
94
|
+
const iface = rl.createInterface({ input: process.stdin, output: process.stdout });
|
|
95
|
+
const question = (text: string): Promise<string> => new Promise((resolve) => iface.question(text, (answer: string) => resolve(answer.trim())));
|
|
96
|
+
|
|
97
|
+
log('Apibot — API Testing Tool Setup\n');
|
|
98
|
+
const baseEndpoint = await question('Base API endpoint (e.g. https://api.example.com/v1): ');
|
|
99
|
+
const spec = await question('OpenAPI spec file or URL: ');
|
|
100
|
+
const knowledge = await question('Describe your API, its auth and its rules (Enter to skip): ');
|
|
101
|
+
iface.close();
|
|
102
|
+
|
|
103
|
+
return { baseEndpoint, spec, knowledge };
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
interface InitOptions {
|
|
107
|
+
force?: boolean;
|
|
108
|
+
path?: string;
|
|
109
|
+
provider?: string;
|
|
110
|
+
baseEndpoint?: string;
|
|
111
|
+
spec?: string;
|
|
112
|
+
prefix: string;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
interface Answers {
|
|
116
|
+
baseEndpoint: string;
|
|
117
|
+
spec: string;
|
|
118
|
+
knowledge: string;
|
|
119
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { tag } from '../../../../src/utils/logger.ts';
|
|
4
|
+
import { ApiCommand } from './api-command.ts';
|
|
5
|
+
|
|
6
|
+
export class KnowCommand extends ApiCommand {
|
|
7
|
+
name = 'know';
|
|
8
|
+
aliases = ['add-knowledge'];
|
|
9
|
+
description = 'Add API knowledge for an endpoint';
|
|
10
|
+
knowledge = '';
|
|
11
|
+
|
|
12
|
+
async execute(endpoint: string): Promise<void> {
|
|
13
|
+
if (!this.knowledge) {
|
|
14
|
+
throw new Error('Description is required.');
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const knowledgeDir = await this.resolveKnowledgeDir();
|
|
18
|
+
fs.mkdirSync(knowledgeDir, { recursive: true });
|
|
19
|
+
|
|
20
|
+
const filename = endpoint.replace(/^\//, '').replace(/[^a-zA-Z0-9]/g, '_') || 'general';
|
|
21
|
+
const filePath = path.join(knowledgeDir, `${filename}.md`);
|
|
22
|
+
|
|
23
|
+
if (fs.existsSync(filePath)) {
|
|
24
|
+
fs.appendFileSync(filePath, `\n---\n${this.knowledge}\n`, 'utf8');
|
|
25
|
+
tag('success').log(`Updated: ${filePath}`);
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
fs.writeFileSync(filePath, `---\nendpoint: "${endpoint}"\n---\n${this.knowledge}\n`, 'utf8');
|
|
30
|
+
tag('success').log(`Created: ${filePath}`);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
private async resolveKnowledgeDir(): Promise<string> {
|
|
34
|
+
const parser = this.bot.getConfigParser();
|
|
35
|
+
const options = this.bot.getOptions();
|
|
36
|
+
try {
|
|
37
|
+
await parser.loadConfig(options);
|
|
38
|
+
return parser.getKnowledgeDir();
|
|
39
|
+
} catch {
|
|
40
|
+
if (options.path) return path.join(path.resolve(options.path), 'knowledge');
|
|
41
|
+
return 'knowledge';
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|