-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfetch-tools-debug.ts
More file actions
297 lines (257 loc) · 7.29 KB
/
fetch-tools-debug.ts
File metadata and controls
297 lines (257 loc) · 7.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/**
* Interactive CLI Demo
*
* This example demonstrates how to build an interactive CLI tool using
* @clack/prompts to dynamically discover and execute StackOne tools.
*
* Features:
* - Interactive credential input with environment variable fallback
* - Dynamic tool discovery and selection
* - Spinner feedback during async operations
*
* Run with:
* ```bash
* node --env-files=.env examples/interactive-cli.ts
* ```
*/
import process from 'node:process';
import * as clack from '@clack/prompts';
import { StackOneToolSet } from '@stackone/ai';
/**
* Mask a sensitive value, showing only the first few and last few characters
*/
function maskValue(value: string, visibleStart = 4, visibleEnd = 4): string {
if (value.length <= visibleStart + visibleEnd) {
return '*'.repeat(value.length);
}
const start = value.slice(0, visibleStart);
const end = value.slice(-visibleEnd);
const masked = '*'.repeat(Math.min(value.length - visibleStart - visibleEnd, 8));
return `${start}${masked}${end}`;
}
clack.intro('Welcome to StackOne AI Tool Tester');
// Get API key
let apiKey: string;
const envApiKey = process.env.STACKONE_API_KEY;
if (envApiKey) {
const apiKeyChoice = await clack.select({
message: 'StackOne API Key:',
options: [
{ value: 'env', label: 'Use environment variable', hint: maskValue(envApiKey) },
{ value: 'input', label: 'Enter manually' },
],
});
if (clack.isCancel(apiKeyChoice)) {
clack.cancel('Operation cancelled');
process.exit(0);
}
if (apiKeyChoice === 'env') {
apiKey = envApiKey;
} else {
const apiKeyInput = await clack.text({
message: 'Enter your StackOne API key:',
placeholder: 'v1.us1.xxx...',
validate: (value) => {
if (!value) return 'API key is required';
},
});
if (clack.isCancel(apiKeyInput)) {
clack.cancel('Operation cancelled');
process.exit(0);
}
apiKey = apiKeyInput;
}
} else {
const apiKeyInput = await clack.text({
message: 'Enter your StackOne API key:',
placeholder: 'v1.us1.xxx...',
validate: (value) => {
if (!value) return 'API key is required';
},
});
if (clack.isCancel(apiKeyInput)) {
clack.cancel('Operation cancelled');
process.exit(0);
}
apiKey = apiKeyInput;
}
// Get base URL
let baseUrl: string;
const envBaseUrl = process.env.STACKONE_BASE_URL;
if (envBaseUrl) {
const baseUrlChoice = await clack.select({
message: 'StackOne Base URL:',
options: [
{ value: 'env', label: 'Use environment variable', hint: maskValue(envBaseUrl, 8, 8) },
{ value: 'input', label: 'Enter manually' },
],
});
if (clack.isCancel(baseUrlChoice)) {
clack.cancel('Operation cancelled');
process.exit(0);
}
if (baseUrlChoice === 'env') {
baseUrl = envBaseUrl;
} else {
const baseUrlInput = await clack.text({
message: 'Enter StackOne Base URL:',
placeholder: 'https://api.stackone.com',
defaultValue: 'https://api.stackone.com',
});
if (clack.isCancel(baseUrlInput)) {
clack.cancel('Operation cancelled');
process.exit(0);
}
baseUrl = baseUrlInput;
}
} else {
const baseUrlInput = await clack.text({
message: 'Enter StackOne Base URL (optional):',
placeholder: 'https://api.stackone.com',
defaultValue: 'https://api.stackone.com',
});
if (clack.isCancel(baseUrlInput)) {
clack.cancel('Operation cancelled');
process.exit(0);
}
baseUrl = baseUrlInput;
}
// Get account ID
let accountId: string;
const envAccountId = process.env.STACKONE_ACCOUNT_ID;
if (envAccountId) {
const accountIdChoice = await clack.select({
message: 'StackOne Account ID:',
options: [
{ value: 'env', label: 'Use environment variable', hint: maskValue(envAccountId) },
{ value: 'input', label: 'Enter manually' },
],
});
if (clack.isCancel(accountIdChoice)) {
clack.cancel('Operation cancelled');
process.exit(0);
}
if (accountIdChoice === 'env') {
accountId = envAccountId;
} else {
const accountIdInput = await clack.text({
message: 'Enter your StackOne Account ID:',
placeholder: 'acc_xxx...',
validate: (value) => {
if (!value) return 'Account ID is required';
},
});
if (clack.isCancel(accountIdInput)) {
clack.cancel('Operation cancelled');
process.exit(0);
}
accountId = accountIdInput as string;
}
} else {
const accountIdInput = await clack.text({
message: 'Enter your StackOne Account ID:',
placeholder: 'acc_xxx...',
validate: (value) => {
if (!value) return 'Account ID is required';
},
});
if (clack.isCancel(accountIdInput)) {
clack.cancel('Operation cancelled');
process.exit(0);
}
accountId = accountIdInput as string;
}
// @ts-expect-error Bun global is not in Node.js types
if ((typeof globalThis.Bun as any) !== 'undefined') {
const detailedLog = await clack.confirm({
message: 'Enable detailed logging? (recommended for Bun.js users)',
});
if (clack.isCancel(detailedLog)) {
clack.cancel('Operation cancelled');
process.exit(0);
}
if (detailedLog) {
process.env.BUN_CONFIG_VERBOSE_FETCH = 'curl';
}
}
const spinner = clack.spinner();
spinner.start('Initializing StackOne client...');
const toolset = new StackOneToolSet({
apiKey,
baseUrl,
accountId,
});
spinner.message('Fetching available tools...');
const tools = await toolset.fetchTools();
const allTools = tools.toArray();
spinner.stop(`Found ${allTools.length} tools`);
// Select a tool interactively
const selectedToolName = await clack.select({
message: 'Select a tool to execute:',
options: allTools.map((tool) => ({
label: tool.description,
value: tool.name,
hint: tool.name,
})),
});
if (clack.isCancel(selectedToolName)) {
clack.cancel('Operation cancelled');
process.exit(0);
}
const selectedTool = tools.getTool(selectedToolName as string);
if (!selectedTool) {
clack.log.error(`Tool '${selectedToolName}' not found!`);
process.exit(1);
}
spinner.start(`Executing: ${selectedTool.description}`);
try {
const result = await selectedTool.execute({
query: { limit: 5 },
});
spinner.stop('Execution complete');
clack.log.success('Result:');
// Display result based on its structure
if (Array.isArray(result)) {
// For array results, use console.table for better readability
if (result.length > 0 && typeof result[0] === 'object') {
console.table(result);
} else {
console.log(result);
}
} else if (result && typeof result === 'object') {
// Check if result has a data array property (common API response pattern)
const data = (result as Record<string, unknown>).data;
if (Array.isArray(data) && data.length > 0 && typeof data[0] === 'object') {
console.log('\nData:');
console.table(data);
// Show other properties
const otherProps = Object.fromEntries(
Object.entries(result as Record<string, unknown>).filter(([key]) => key !== 'data'),
);
if (Object.keys(otherProps).length > 0) {
console.log('\nMetadata:');
console.log(JSON.stringify(otherProps, null, 2));
}
} else {
console.log(JSON.stringify(result, null, 2));
}
} else {
console.log(result);
}
clack.outro('Done!');
} catch (error) {
spinner.stop('Execution failed');
if (error instanceof Error) {
clack.log.error(`Error: ${error.message}`);
if (error.cause) {
clack.log.info(`Cause: ${JSON.stringify(error.cause, null, 2)}`);
}
if (error.stack) {
clack.log.info(`Stack trace:\n${error.stack}`);
}
} else {
clack.log.error(`Error: ${JSON.stringify(error, null, 2)}`);
}
clack.outro('Failed');
process.exit(1);
}