@push.rocks/smartagent 1.0.2
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/dist_ts/00_commitinfo_data.d.ts +8 -0
- package/dist_ts/00_commitinfo_data.js +9 -0
- package/dist_ts/index.d.ts +10 -0
- package/dist_ts/index.js +18 -0
- package/dist_ts/plugins.d.ts +6 -0
- package/dist_ts/plugins.js +8 -0
- package/dist_ts/smartagent.classes.driveragent.d.ts +70 -0
- package/dist_ts/smartagent.classes.driveragent.js +274 -0
- package/dist_ts/smartagent.classes.dualagent.d.ts +62 -0
- package/dist_ts/smartagent.classes.dualagent.js +298 -0
- package/dist_ts/smartagent.classes.guardianagent.d.ts +46 -0
- package/dist_ts/smartagent.classes.guardianagent.js +201 -0
- package/dist_ts/smartagent.classes.smartagent.d.ts +123 -0
- package/dist_ts/smartagent.classes.smartagent.js +274 -0
- package/dist_ts/smartagent.interfaces.d.ts +165 -0
- package/dist_ts/smartagent.interfaces.js +8 -0
- package/dist_ts/smartagent.tools.base.d.ts +46 -0
- package/dist_ts/smartagent.tools.base.js +45 -0
- package/dist_ts/smartagent.tools.browser.d.ts +16 -0
- package/dist_ts/smartagent.tools.browser.js +177 -0
- package/dist_ts/smartagent.tools.filesystem.d.ts +16 -0
- package/dist_ts/smartagent.tools.filesystem.js +352 -0
- package/dist_ts/smartagent.tools.http.d.ts +15 -0
- package/dist_ts/smartagent.tools.http.js +187 -0
- package/dist_ts/smartagent.tools.shell.d.ts +16 -0
- package/dist_ts/smartagent.tools.shell.js +155 -0
- package/npmextra.json +18 -0
- package/package.json +50 -0
- package/readme.hints.md +16 -0
- package/readme.md +299 -0
- package/ts/00_commitinfo_data.ts +8 -0
- package/ts/index.ts +29 -0
- package/ts/plugins.ts +14 -0
- package/ts/smartagent.classes.driveragent.ts +321 -0
- package/ts/smartagent.classes.dualagent.ts +350 -0
- package/ts/smartagent.classes.guardianagent.ts +241 -0
- package/ts/smartagent.interfaces.ts +210 -0
- package/ts/smartagent.tools.base.ts +80 -0
- package/ts/smartagent.tools.browser.ts +200 -0
- package/ts/smartagent.tools.filesystem.ts +379 -0
- package/ts/smartagent.tools.http.ts +205 -0
- package/ts/smartagent.tools.shell.ts +182 -0
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
import * as plugins from './plugins.js';
|
|
2
|
+
import * as interfaces from './smartagent.interfaces.js';
|
|
3
|
+
import { BaseToolWrapper } from './smartagent.tools.base.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Filesystem tool for file and directory operations
|
|
7
|
+
* Wraps @push.rocks/smartfs
|
|
8
|
+
*/
|
|
9
|
+
export class FilesystemTool extends BaseToolWrapper {
|
|
10
|
+
public name = 'filesystem';
|
|
11
|
+
public description = 'Read, write, list, and delete files and directories';
|
|
12
|
+
|
|
13
|
+
public actions: interfaces.IToolAction[] = [
|
|
14
|
+
{
|
|
15
|
+
name: 'read',
|
|
16
|
+
description: 'Read the contents of a file',
|
|
17
|
+
parameters: {
|
|
18
|
+
type: 'object',
|
|
19
|
+
properties: {
|
|
20
|
+
path: { type: 'string', description: 'Absolute path to the file' },
|
|
21
|
+
encoding: {
|
|
22
|
+
type: 'string',
|
|
23
|
+
enum: ['utf8', 'binary', 'base64'],
|
|
24
|
+
default: 'utf8',
|
|
25
|
+
description: 'File encoding',
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
required: ['path'],
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
name: 'write',
|
|
33
|
+
description: 'Write content to a file (creates or overwrites)',
|
|
34
|
+
parameters: {
|
|
35
|
+
type: 'object',
|
|
36
|
+
properties: {
|
|
37
|
+
path: { type: 'string', description: 'Absolute path to the file' },
|
|
38
|
+
content: { type: 'string', description: 'Content to write' },
|
|
39
|
+
encoding: {
|
|
40
|
+
type: 'string',
|
|
41
|
+
enum: ['utf8', 'binary', 'base64'],
|
|
42
|
+
default: 'utf8',
|
|
43
|
+
description: 'File encoding',
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
required: ['path', 'content'],
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
name: 'append',
|
|
51
|
+
description: 'Append content to a file',
|
|
52
|
+
parameters: {
|
|
53
|
+
type: 'object',
|
|
54
|
+
properties: {
|
|
55
|
+
path: { type: 'string', description: 'Absolute path to the file' },
|
|
56
|
+
content: { type: 'string', description: 'Content to append' },
|
|
57
|
+
},
|
|
58
|
+
required: ['path', 'content'],
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
name: 'list',
|
|
63
|
+
description: 'List files and directories in a path',
|
|
64
|
+
parameters: {
|
|
65
|
+
type: 'object',
|
|
66
|
+
properties: {
|
|
67
|
+
path: { type: 'string', description: 'Directory path to list' },
|
|
68
|
+
recursive: { type: 'boolean', default: false, description: 'List recursively' },
|
|
69
|
+
filter: { type: 'string', description: 'Glob pattern to filter results (e.g., "*.ts")' },
|
|
70
|
+
},
|
|
71
|
+
required: ['path'],
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
name: 'delete',
|
|
76
|
+
description: 'Delete a file or directory',
|
|
77
|
+
parameters: {
|
|
78
|
+
type: 'object',
|
|
79
|
+
properties: {
|
|
80
|
+
path: { type: 'string', description: 'Path to delete' },
|
|
81
|
+
recursive: {
|
|
82
|
+
type: 'boolean',
|
|
83
|
+
default: false,
|
|
84
|
+
description: 'For directories, delete recursively',
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
required: ['path'],
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
name: 'exists',
|
|
92
|
+
description: 'Check if a file or directory exists',
|
|
93
|
+
parameters: {
|
|
94
|
+
type: 'object',
|
|
95
|
+
properties: {
|
|
96
|
+
path: { type: 'string', description: 'Path to check' },
|
|
97
|
+
},
|
|
98
|
+
required: ['path'],
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
name: 'stat',
|
|
103
|
+
description: 'Get file or directory statistics (size, dates, etc.)',
|
|
104
|
+
parameters: {
|
|
105
|
+
type: 'object',
|
|
106
|
+
properties: {
|
|
107
|
+
path: { type: 'string', description: 'Path to get stats for' },
|
|
108
|
+
},
|
|
109
|
+
required: ['path'],
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
name: 'copy',
|
|
114
|
+
description: 'Copy a file to a new location',
|
|
115
|
+
parameters: {
|
|
116
|
+
type: 'object',
|
|
117
|
+
properties: {
|
|
118
|
+
source: { type: 'string', description: 'Source file path' },
|
|
119
|
+
destination: { type: 'string', description: 'Destination file path' },
|
|
120
|
+
},
|
|
121
|
+
required: ['source', 'destination'],
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
name: 'move',
|
|
126
|
+
description: 'Move a file to a new location',
|
|
127
|
+
parameters: {
|
|
128
|
+
type: 'object',
|
|
129
|
+
properties: {
|
|
130
|
+
source: { type: 'string', description: 'Source file path' },
|
|
131
|
+
destination: { type: 'string', description: 'Destination file path' },
|
|
132
|
+
},
|
|
133
|
+
required: ['source', 'destination'],
|
|
134
|
+
},
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
name: 'mkdir',
|
|
138
|
+
description: 'Create a directory',
|
|
139
|
+
parameters: {
|
|
140
|
+
type: 'object',
|
|
141
|
+
properties: {
|
|
142
|
+
path: { type: 'string', description: 'Directory path to create' },
|
|
143
|
+
recursive: {
|
|
144
|
+
type: 'boolean',
|
|
145
|
+
default: true,
|
|
146
|
+
description: 'Create parent directories if needed',
|
|
147
|
+
},
|
|
148
|
+
},
|
|
149
|
+
required: ['path'],
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
];
|
|
153
|
+
|
|
154
|
+
private smartfs!: plugins.smartfs.SmartFs;
|
|
155
|
+
|
|
156
|
+
public async initialize(): Promise<void> {
|
|
157
|
+
this.smartfs = new plugins.smartfs.SmartFs(new plugins.smartfs.SmartFsProviderNode());
|
|
158
|
+
this.isInitialized = true;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
public async cleanup(): Promise<void> {
|
|
162
|
+
this.isInitialized = false;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
public async execute(
|
|
166
|
+
action: string,
|
|
167
|
+
params: Record<string, unknown>
|
|
168
|
+
): Promise<interfaces.IToolExecutionResult> {
|
|
169
|
+
this.validateAction(action);
|
|
170
|
+
this.ensureInitialized();
|
|
171
|
+
|
|
172
|
+
try {
|
|
173
|
+
switch (action) {
|
|
174
|
+
case 'read': {
|
|
175
|
+
const encoding = (params.encoding as string) || 'utf8';
|
|
176
|
+
const content = await this.smartfs
|
|
177
|
+
.file(params.path as string)
|
|
178
|
+
.encoding(encoding as 'utf8' | 'binary' | 'base64')
|
|
179
|
+
.read();
|
|
180
|
+
return {
|
|
181
|
+
success: true,
|
|
182
|
+
result: {
|
|
183
|
+
path: params.path,
|
|
184
|
+
content: content.toString(),
|
|
185
|
+
encoding,
|
|
186
|
+
},
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
case 'write': {
|
|
191
|
+
const encoding = (params.encoding as string) || 'utf8';
|
|
192
|
+
await this.smartfs
|
|
193
|
+
.file(params.path as string)
|
|
194
|
+
.encoding(encoding as 'utf8' | 'binary' | 'base64')
|
|
195
|
+
.write(params.content as string);
|
|
196
|
+
return {
|
|
197
|
+
success: true,
|
|
198
|
+
result: {
|
|
199
|
+
path: params.path,
|
|
200
|
+
written: true,
|
|
201
|
+
bytesWritten: (params.content as string).length,
|
|
202
|
+
},
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
case 'append': {
|
|
207
|
+
await this.smartfs.file(params.path as string).append(params.content as string);
|
|
208
|
+
return {
|
|
209
|
+
success: true,
|
|
210
|
+
result: {
|
|
211
|
+
path: params.path,
|
|
212
|
+
appended: true,
|
|
213
|
+
},
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
case 'list': {
|
|
218
|
+
let dir = this.smartfs.directory(params.path as string);
|
|
219
|
+
if (params.recursive) {
|
|
220
|
+
dir = dir.recursive();
|
|
221
|
+
}
|
|
222
|
+
if (params.filter) {
|
|
223
|
+
dir = dir.filter(params.filter as string);
|
|
224
|
+
}
|
|
225
|
+
const entries = await dir.list();
|
|
226
|
+
return {
|
|
227
|
+
success: true,
|
|
228
|
+
result: {
|
|
229
|
+
path: params.path,
|
|
230
|
+
entries,
|
|
231
|
+
count: entries.length,
|
|
232
|
+
},
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
case 'delete': {
|
|
237
|
+
const path = params.path as string;
|
|
238
|
+
// Check if it's a directory or file
|
|
239
|
+
const exists = await this.smartfs.file(path).exists();
|
|
240
|
+
if (exists) {
|
|
241
|
+
// Try to get stats to check if it's a directory
|
|
242
|
+
try {
|
|
243
|
+
const stats = await this.smartfs.file(path).stat();
|
|
244
|
+
if (stats.isDirectory && params.recursive) {
|
|
245
|
+
await this.smartfs.directory(path).recursive().delete();
|
|
246
|
+
} else {
|
|
247
|
+
await this.smartfs.file(path).delete();
|
|
248
|
+
}
|
|
249
|
+
} catch {
|
|
250
|
+
await this.smartfs.file(path).delete();
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
return {
|
|
254
|
+
success: true,
|
|
255
|
+
result: {
|
|
256
|
+
path,
|
|
257
|
+
deleted: true,
|
|
258
|
+
},
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
case 'exists': {
|
|
263
|
+
const exists = await this.smartfs.file(params.path as string).exists();
|
|
264
|
+
return {
|
|
265
|
+
success: true,
|
|
266
|
+
result: {
|
|
267
|
+
path: params.path,
|
|
268
|
+
exists,
|
|
269
|
+
},
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
case 'stat': {
|
|
274
|
+
const stats = await this.smartfs.file(params.path as string).stat();
|
|
275
|
+
return {
|
|
276
|
+
success: true,
|
|
277
|
+
result: {
|
|
278
|
+
path: params.path,
|
|
279
|
+
stats,
|
|
280
|
+
},
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
case 'copy': {
|
|
285
|
+
await this.smartfs.file(params.source as string).copy(params.destination as string);
|
|
286
|
+
return {
|
|
287
|
+
success: true,
|
|
288
|
+
result: {
|
|
289
|
+
source: params.source,
|
|
290
|
+
destination: params.destination,
|
|
291
|
+
copied: true,
|
|
292
|
+
},
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
case 'move': {
|
|
297
|
+
await this.smartfs.file(params.source as string).move(params.destination as string);
|
|
298
|
+
return {
|
|
299
|
+
success: true,
|
|
300
|
+
result: {
|
|
301
|
+
source: params.source,
|
|
302
|
+
destination: params.destination,
|
|
303
|
+
moved: true,
|
|
304
|
+
},
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
case 'mkdir': {
|
|
309
|
+
let dir = this.smartfs.directory(params.path as string);
|
|
310
|
+
if (params.recursive !== false) {
|
|
311
|
+
dir = dir.recursive();
|
|
312
|
+
}
|
|
313
|
+
await dir.create();
|
|
314
|
+
return {
|
|
315
|
+
success: true,
|
|
316
|
+
result: {
|
|
317
|
+
path: params.path,
|
|
318
|
+
created: true,
|
|
319
|
+
},
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
default:
|
|
324
|
+
return {
|
|
325
|
+
success: false,
|
|
326
|
+
error: `Unknown action: ${action}`,
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
} catch (error) {
|
|
330
|
+
return {
|
|
331
|
+
success: false,
|
|
332
|
+
error: error instanceof Error ? error.message : String(error),
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
public getCallSummary(action: string, params: Record<string, unknown>): string {
|
|
338
|
+
switch (action) {
|
|
339
|
+
case 'read':
|
|
340
|
+
return `Read file "${params.path}" with encoding ${params.encoding || 'utf8'}`;
|
|
341
|
+
|
|
342
|
+
case 'write': {
|
|
343
|
+
const content = params.content as string;
|
|
344
|
+
const preview = content.length > 100 ? content.substring(0, 100) + '...' : content;
|
|
345
|
+
return `Write ${content.length} bytes to "${params.path}". Content preview: "${preview}"`;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
case 'append': {
|
|
349
|
+
const content = params.content as string;
|
|
350
|
+
const preview = content.length > 100 ? content.substring(0, 100) + '...' : content;
|
|
351
|
+
return `Append ${content.length} bytes to "${params.path}". Content preview: "${preview}"`;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
case 'list':
|
|
355
|
+
return `List directory "${params.path}"${params.recursive ? ' recursively' : ''}${params.filter ? ` with filter "${params.filter}"` : ''}`;
|
|
356
|
+
|
|
357
|
+
case 'delete':
|
|
358
|
+
return `Delete "${params.path}"${params.recursive ? ' recursively' : ''}`;
|
|
359
|
+
|
|
360
|
+
case 'exists':
|
|
361
|
+
return `Check if "${params.path}" exists`;
|
|
362
|
+
|
|
363
|
+
case 'stat':
|
|
364
|
+
return `Get statistics for "${params.path}"`;
|
|
365
|
+
|
|
366
|
+
case 'copy':
|
|
367
|
+
return `Copy "${params.source}" to "${params.destination}"`;
|
|
368
|
+
|
|
369
|
+
case 'move':
|
|
370
|
+
return `Move "${params.source}" to "${params.destination}"`;
|
|
371
|
+
|
|
372
|
+
case 'mkdir':
|
|
373
|
+
return `Create directory "${params.path}"${params.recursive !== false ? ' (with parents)' : ''}`;
|
|
374
|
+
|
|
375
|
+
default:
|
|
376
|
+
return `Unknown action: ${action}`;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
}
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import * as plugins from './plugins.js';
|
|
2
|
+
import * as interfaces from './smartagent.interfaces.js';
|
|
3
|
+
import { BaseToolWrapper } from './smartagent.tools.base.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* HTTP tool for making web requests
|
|
7
|
+
* Wraps @push.rocks/smartrequest
|
|
8
|
+
*/
|
|
9
|
+
export class HttpTool extends BaseToolWrapper {
|
|
10
|
+
public name = 'http';
|
|
11
|
+
public description = 'Make HTTP requests to web APIs and services';
|
|
12
|
+
|
|
13
|
+
public actions: interfaces.IToolAction[] = [
|
|
14
|
+
{
|
|
15
|
+
name: 'get',
|
|
16
|
+
description: 'Make a GET request',
|
|
17
|
+
parameters: {
|
|
18
|
+
type: 'object',
|
|
19
|
+
properties: {
|
|
20
|
+
url: { type: 'string', description: 'URL to request' },
|
|
21
|
+
headers: { type: 'object', description: 'Request headers (key-value pairs)' },
|
|
22
|
+
query: { type: 'object', description: 'Query parameters (key-value pairs)' },
|
|
23
|
+
timeout: { type: 'number', description: 'Timeout in milliseconds' },
|
|
24
|
+
},
|
|
25
|
+
required: ['url'],
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
name: 'post',
|
|
30
|
+
description: 'Make a POST request with JSON body',
|
|
31
|
+
parameters: {
|
|
32
|
+
type: 'object',
|
|
33
|
+
properties: {
|
|
34
|
+
url: { type: 'string', description: 'URL to request' },
|
|
35
|
+
body: { type: 'object', description: 'JSON body to send' },
|
|
36
|
+
headers: { type: 'object', description: 'Request headers (key-value pairs)' },
|
|
37
|
+
query: { type: 'object', description: 'Query parameters (key-value pairs)' },
|
|
38
|
+
timeout: { type: 'number', description: 'Timeout in milliseconds' },
|
|
39
|
+
},
|
|
40
|
+
required: ['url'],
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
name: 'put',
|
|
45
|
+
description: 'Make a PUT request with JSON body',
|
|
46
|
+
parameters: {
|
|
47
|
+
type: 'object',
|
|
48
|
+
properties: {
|
|
49
|
+
url: { type: 'string', description: 'URL to request' },
|
|
50
|
+
body: { type: 'object', description: 'JSON body to send' },
|
|
51
|
+
headers: { type: 'object', description: 'Request headers (key-value pairs)' },
|
|
52
|
+
timeout: { type: 'number', description: 'Timeout in milliseconds' },
|
|
53
|
+
},
|
|
54
|
+
required: ['url', 'body'],
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
name: 'patch',
|
|
59
|
+
description: 'Make a PATCH request with JSON body',
|
|
60
|
+
parameters: {
|
|
61
|
+
type: 'object',
|
|
62
|
+
properties: {
|
|
63
|
+
url: { type: 'string', description: 'URL to request' },
|
|
64
|
+
body: { type: 'object', description: 'JSON body to send' },
|
|
65
|
+
headers: { type: 'object', description: 'Request headers (key-value pairs)' },
|
|
66
|
+
timeout: { type: 'number', description: 'Timeout in milliseconds' },
|
|
67
|
+
},
|
|
68
|
+
required: ['url', 'body'],
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
name: 'delete',
|
|
73
|
+
description: 'Make a DELETE request',
|
|
74
|
+
parameters: {
|
|
75
|
+
type: 'object',
|
|
76
|
+
properties: {
|
|
77
|
+
url: { type: 'string', description: 'URL to request' },
|
|
78
|
+
headers: { type: 'object', description: 'Request headers (key-value pairs)' },
|
|
79
|
+
timeout: { type: 'number', description: 'Timeout in milliseconds' },
|
|
80
|
+
},
|
|
81
|
+
required: ['url'],
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
];
|
|
85
|
+
|
|
86
|
+
public async initialize(): Promise<void> {
|
|
87
|
+
// SmartRequest is stateless, no initialization needed
|
|
88
|
+
this.isInitialized = true;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
public async cleanup(): Promise<void> {
|
|
92
|
+
this.isInitialized = false;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
public async execute(
|
|
96
|
+
action: string,
|
|
97
|
+
params: Record<string, unknown>
|
|
98
|
+
): Promise<interfaces.IToolExecutionResult> {
|
|
99
|
+
this.validateAction(action);
|
|
100
|
+
this.ensureInitialized();
|
|
101
|
+
|
|
102
|
+
try {
|
|
103
|
+
let request = plugins.smartrequest.SmartRequest.create().url(params.url as string);
|
|
104
|
+
|
|
105
|
+
// Add headers
|
|
106
|
+
if (params.headers && typeof params.headers === 'object') {
|
|
107
|
+
for (const [key, value] of Object.entries(params.headers as Record<string, string>)) {
|
|
108
|
+
request = request.header(key, value);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Add query parameters
|
|
113
|
+
if (params.query && typeof params.query === 'object') {
|
|
114
|
+
request = request.query(params.query as Record<string, string>);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Add timeout
|
|
118
|
+
if (params.timeout) {
|
|
119
|
+
request = request.timeout(params.timeout as number);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Add JSON body for POST, PUT, PATCH
|
|
123
|
+
if (params.body && ['post', 'put', 'patch'].includes(action)) {
|
|
124
|
+
request = request.json(params.body);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Execute the request
|
|
128
|
+
let response;
|
|
129
|
+
switch (action) {
|
|
130
|
+
case 'get':
|
|
131
|
+
response = await request.get();
|
|
132
|
+
break;
|
|
133
|
+
case 'post':
|
|
134
|
+
response = await request.post();
|
|
135
|
+
break;
|
|
136
|
+
case 'put':
|
|
137
|
+
response = await request.put();
|
|
138
|
+
break;
|
|
139
|
+
case 'patch':
|
|
140
|
+
response = await request.patch();
|
|
141
|
+
break;
|
|
142
|
+
case 'delete':
|
|
143
|
+
response = await request.delete();
|
|
144
|
+
break;
|
|
145
|
+
default:
|
|
146
|
+
return { success: false, error: `Unknown action: ${action}` };
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Parse response body
|
|
150
|
+
let body: unknown;
|
|
151
|
+
const contentType = response.headers?.['content-type'] || '';
|
|
152
|
+
|
|
153
|
+
try {
|
|
154
|
+
if (contentType.includes('application/json')) {
|
|
155
|
+
body = await response.json();
|
|
156
|
+
} else {
|
|
157
|
+
body = await response.text();
|
|
158
|
+
}
|
|
159
|
+
} catch {
|
|
160
|
+
body = null;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return {
|
|
164
|
+
success: response.ok,
|
|
165
|
+
result: {
|
|
166
|
+
url: params.url,
|
|
167
|
+
method: action.toUpperCase(),
|
|
168
|
+
status: response.status,
|
|
169
|
+
statusText: response.statusText,
|
|
170
|
+
ok: response.ok,
|
|
171
|
+
headers: response.headers,
|
|
172
|
+
body,
|
|
173
|
+
},
|
|
174
|
+
};
|
|
175
|
+
} catch (error) {
|
|
176
|
+
return {
|
|
177
|
+
success: false,
|
|
178
|
+
error: error instanceof Error ? error.message : String(error),
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
public getCallSummary(action: string, params: Record<string, unknown>): string {
|
|
184
|
+
const method = action.toUpperCase();
|
|
185
|
+
let summary = `${method} request to "${params.url}"`;
|
|
186
|
+
|
|
187
|
+
if (params.query && Object.keys(params.query as object).length > 0) {
|
|
188
|
+
const queryStr = JSON.stringify(params.query);
|
|
189
|
+
summary += ` with query: ${queryStr.length > 50 ? queryStr.substring(0, 50) + '...' : queryStr}`;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
if (params.body) {
|
|
193
|
+
const bodyStr = JSON.stringify(params.body);
|
|
194
|
+
const preview = bodyStr.length > 100 ? bodyStr.substring(0, 100) + '...' : bodyStr;
|
|
195
|
+
summary += ` with body: ${preview}`;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
if (params.headers && Object.keys(params.headers as object).length > 0) {
|
|
199
|
+
const headerKeys = Object.keys(params.headers as object).join(', ');
|
|
200
|
+
summary += ` with headers: [${headerKeys}]`;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return summary;
|
|
204
|
+
}
|
|
205
|
+
}
|