@push.rocks/smartagent 1.7.0 → 3.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/dist_ts/00_commitinfo_data.js +3 -3
- package/dist_ts/index.d.ts +9 -12
- package/dist_ts/index.js +9 -20
- package/dist_ts/plugins.d.ts +8 -9
- package/dist_ts/plugins.js +10 -12
- package/dist_ts/smartagent.classes.agent.d.ts +2 -0
- package/dist_ts/smartagent.classes.agent.js +173 -0
- package/dist_ts/smartagent.classes.toolregistry.d.ts +12 -0
- package/dist_ts/smartagent.classes.toolregistry.js +17 -0
- package/dist_ts/smartagent.interfaces.d.ts +47 -231
- package/dist_ts/smartagent.interfaces.js +6 -7
- package/dist_ts/smartagent.utils.truncation.d.ts +10 -0
- package/dist_ts/smartagent.utils.truncation.js +26 -0
- package/dist_ts_compaction/index.d.ts +1 -0
- package/dist_ts_compaction/index.js +2 -0
- package/dist_ts_compaction/plugins.d.ts +4 -0
- package/dist_ts_compaction/plugins.js +3 -0
- package/dist_ts_compaction/smartagent.compaction.d.ts +10 -0
- package/dist_ts_compaction/smartagent.compaction.js +46 -0
- package/dist_ts_tools/index.d.ts +8 -0
- package/dist_ts_tools/index.js +6 -0
- package/dist_ts_tools/plugins.d.ts +15 -0
- package/dist_ts_tools/plugins.js +19 -0
- package/dist_ts_tools/tool.filesystem.d.ts +6 -0
- package/dist_ts_tools/tool.filesystem.js +102 -0
- package/dist_ts_tools/tool.http.d.ts +2 -0
- package/dist_ts_tools/tool.http.js +65 -0
- package/dist_ts_tools/tool.json.d.ts +2 -0
- package/dist_ts_tools/tool.json.js +47 -0
- package/dist_ts_tools/tool.shell.d.ts +8 -0
- package/dist_ts_tools/tool.shell.js +40 -0
- package/npmextra.json +1 -1
- package/package.json +30 -18
- package/readme.hints.md +43 -42
- package/readme.md +257 -526
- package/ts/00_commitinfo_data.ts +2 -2
- package/ts/index.ts +11 -31
- package/ts/plugins.ts +22 -21
- package/ts/smartagent.classes.agent.ts +198 -0
- package/ts/smartagent.classes.toolregistry.ts +20 -0
- package/ts/smartagent.interfaces.ts +51 -303
- package/ts/smartagent.utils.truncation.ts +39 -0
- package/ts_compaction/index.ts +1 -0
- package/ts_compaction/plugins.ts +6 -0
- package/ts_compaction/smartagent.compaction.ts +51 -0
- package/ts_tools/index.ts +8 -0
- package/ts_tools/plugins.ts +30 -0
- package/ts_tools/tool.filesystem.ts +131 -0
- package/ts_tools/tool.http.ts +78 -0
- package/ts_tools/tool.json.ts +53 -0
- package/ts_tools/tool.shell.ts +62 -0
- package/dist_ts/smartagent.classes.driveragent.d.ts +0 -134
- package/dist_ts/smartagent.classes.driveragent.js +0 -671
- package/dist_ts/smartagent.classes.dualagent.d.ts +0 -79
- package/dist_ts/smartagent.classes.dualagent.js +0 -583
- package/dist_ts/smartagent.classes.guardianagent.d.ts +0 -46
- package/dist_ts/smartagent.classes.guardianagent.js +0 -201
- package/dist_ts/smartagent.tools.base.d.ts +0 -52
- package/dist_ts/smartagent.tools.base.js +0 -42
- package/dist_ts/smartagent.tools.browser.d.ts +0 -17
- package/dist_ts/smartagent.tools.browser.js +0 -229
- package/dist_ts/smartagent.tools.deno.d.ts +0 -21
- package/dist_ts/smartagent.tools.deno.js +0 -191
- package/dist_ts/smartagent.tools.filesystem.d.ts +0 -40
- package/dist_ts/smartagent.tools.filesystem.js +0 -801
- package/dist_ts/smartagent.tools.http.d.ts +0 -16
- package/dist_ts/smartagent.tools.http.js +0 -264
- package/dist_ts/smartagent.tools.json.d.ts +0 -24
- package/dist_ts/smartagent.tools.json.js +0 -202
- package/dist_ts/smartagent.tools.shell.d.ts +0 -17
- package/dist_ts/smartagent.tools.shell.js +0 -202
- package/ts/smartagent.classes.driveragent.ts +0 -775
- package/ts/smartagent.classes.dualagent.ts +0 -657
- package/ts/smartagent.classes.guardianagent.ts +0 -241
- package/ts/smartagent.tools.base.ts +0 -83
- package/ts/smartagent.tools.browser.ts +0 -253
- package/ts/smartagent.tools.deno.ts +0 -230
- package/ts/smartagent.tools.filesystem.ts +0 -885
- package/ts/smartagent.tools.http.ts +0 -283
- package/ts/smartagent.tools.json.ts +0 -224
- package/ts/smartagent.tools.shell.ts +0 -230
|
@@ -1,283 +0,0 @@
|
|
|
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 getToolExplanation(): string {
|
|
184
|
-
return `## Tool: http
|
|
185
|
-
Make HTTP requests to web APIs and services.
|
|
186
|
-
|
|
187
|
-
### Actions:
|
|
188
|
-
|
|
189
|
-
**get** - Make a GET request
|
|
190
|
-
Parameters:
|
|
191
|
-
- url (required): URL to request
|
|
192
|
-
- headers (optional): Request headers (key-value object)
|
|
193
|
-
- query (optional): Query parameters (key-value object)
|
|
194
|
-
- timeout (optional): Timeout in milliseconds
|
|
195
|
-
|
|
196
|
-
Example:
|
|
197
|
-
<tool_call>
|
|
198
|
-
<tool>http</tool>
|
|
199
|
-
<action>get</action>
|
|
200
|
-
<params>{"url": "https://api.example.com/data", "headers": {"Authorization": "Bearer token123"}}</params>
|
|
201
|
-
</tool_call>
|
|
202
|
-
|
|
203
|
-
**post** - Make a POST request with JSON body
|
|
204
|
-
Parameters:
|
|
205
|
-
- url (required): URL to request
|
|
206
|
-
- body (optional): JSON body to send
|
|
207
|
-
- headers (optional): Request headers (key-value object)
|
|
208
|
-
- query (optional): Query parameters (key-value object)
|
|
209
|
-
- timeout (optional): Timeout in milliseconds
|
|
210
|
-
|
|
211
|
-
Example:
|
|
212
|
-
<tool_call>
|
|
213
|
-
<tool>http</tool>
|
|
214
|
-
<action>post</action>
|
|
215
|
-
<params>{"url": "https://api.example.com/submit", "body": {"name": "test", "value": 123}}</params>
|
|
216
|
-
</tool_call>
|
|
217
|
-
|
|
218
|
-
**put** - Make a PUT request with JSON body
|
|
219
|
-
Parameters:
|
|
220
|
-
- url (required): URL to request
|
|
221
|
-
- body (required): JSON body to send
|
|
222
|
-
- headers (optional): Request headers (key-value object)
|
|
223
|
-
- timeout (optional): Timeout in milliseconds
|
|
224
|
-
|
|
225
|
-
Example:
|
|
226
|
-
<tool_call>
|
|
227
|
-
<tool>http</tool>
|
|
228
|
-
<action>put</action>
|
|
229
|
-
<params>{"url": "https://api.example.com/resource/1", "body": {"name": "updated"}}</params>
|
|
230
|
-
</tool_call>
|
|
231
|
-
|
|
232
|
-
**patch** - Make a PATCH request with JSON body
|
|
233
|
-
Parameters:
|
|
234
|
-
- url (required): URL to request
|
|
235
|
-
- body (required): JSON body to send
|
|
236
|
-
- headers (optional): Request headers (key-value object)
|
|
237
|
-
- timeout (optional): Timeout in milliseconds
|
|
238
|
-
|
|
239
|
-
Example:
|
|
240
|
-
<tool_call>
|
|
241
|
-
<tool>http</tool>
|
|
242
|
-
<action>patch</action>
|
|
243
|
-
<params>{"url": "https://api.example.com/resource/1", "body": {"status": "active"}}</params>
|
|
244
|
-
</tool_call>
|
|
245
|
-
|
|
246
|
-
**delete** - Make a DELETE request
|
|
247
|
-
Parameters:
|
|
248
|
-
- url (required): URL to request
|
|
249
|
-
- headers (optional): Request headers (key-value object)
|
|
250
|
-
- timeout (optional): Timeout in milliseconds
|
|
251
|
-
|
|
252
|
-
Example:
|
|
253
|
-
<tool_call>
|
|
254
|
-
<tool>http</tool>
|
|
255
|
-
<action>delete</action>
|
|
256
|
-
<params>{"url": "https://api.example.com/resource/1"}</params>
|
|
257
|
-
</tool_call>
|
|
258
|
-
`;
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
public getCallSummary(action: string, params: Record<string, unknown>): string {
|
|
262
|
-
const method = action.toUpperCase();
|
|
263
|
-
let summary = `${method} request to "${params.url}"`;
|
|
264
|
-
|
|
265
|
-
if (params.query && Object.keys(params.query as object).length > 0) {
|
|
266
|
-
const queryStr = JSON.stringify(params.query);
|
|
267
|
-
summary += ` with query: ${queryStr.length > 50 ? queryStr.substring(0, 50) + '...' : queryStr}`;
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
if (params.body) {
|
|
271
|
-
const bodyStr = JSON.stringify(params.body);
|
|
272
|
-
const preview = bodyStr.length > 100 ? bodyStr.substring(0, 100) + '...' : bodyStr;
|
|
273
|
-
summary += ` with body: ${preview}`;
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
if (params.headers && Object.keys(params.headers as object).length > 0) {
|
|
277
|
-
const headerKeys = Object.keys(params.headers as object).join(', ');
|
|
278
|
-
summary += ` with headers: [${headerKeys}]`;
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
return summary;
|
|
282
|
-
}
|
|
283
|
-
}
|
|
@@ -1,224 +0,0 @@
|
|
|
1
|
-
import * as interfaces from './smartagent.interfaces.js';
|
|
2
|
-
import { BaseToolWrapper } from './smartagent.tools.base.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* JsonValidatorTool - Validates and formats JSON data
|
|
6
|
-
* Useful for agents to self-validate their JSON output before completing a task
|
|
7
|
-
*/
|
|
8
|
-
export class JsonValidatorTool extends BaseToolWrapper {
|
|
9
|
-
public name = 'json';
|
|
10
|
-
public description = 'Validate and format JSON data. Use this to verify your JSON output is valid before completing a task.';
|
|
11
|
-
|
|
12
|
-
public actions: interfaces.IToolAction[] = [
|
|
13
|
-
{
|
|
14
|
-
name: 'validate',
|
|
15
|
-
description: 'Validate that a string is valid JSON and optionally check required fields',
|
|
16
|
-
parameters: {
|
|
17
|
-
type: 'object',
|
|
18
|
-
properties: {
|
|
19
|
-
jsonString: {
|
|
20
|
-
type: 'string',
|
|
21
|
-
description: 'The JSON string to validate',
|
|
22
|
-
},
|
|
23
|
-
requiredFields: {
|
|
24
|
-
type: 'array',
|
|
25
|
-
items: { type: 'string' },
|
|
26
|
-
description: 'Optional list of field names that must be present at the root level',
|
|
27
|
-
},
|
|
28
|
-
},
|
|
29
|
-
required: ['jsonString'],
|
|
30
|
-
},
|
|
31
|
-
},
|
|
32
|
-
{
|
|
33
|
-
name: 'format',
|
|
34
|
-
description: 'Parse and pretty-print JSON string',
|
|
35
|
-
parameters: {
|
|
36
|
-
type: 'object',
|
|
37
|
-
properties: {
|
|
38
|
-
jsonString: {
|
|
39
|
-
type: 'string',
|
|
40
|
-
description: 'The JSON string to format',
|
|
41
|
-
},
|
|
42
|
-
},
|
|
43
|
-
required: ['jsonString'],
|
|
44
|
-
},
|
|
45
|
-
},
|
|
46
|
-
];
|
|
47
|
-
|
|
48
|
-
async initialize(): Promise<void> {
|
|
49
|
-
this.isInitialized = true;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
async cleanup(): Promise<void> {
|
|
53
|
-
this.isInitialized = false;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
async execute(
|
|
57
|
-
action: string,
|
|
58
|
-
params: Record<string, unknown>
|
|
59
|
-
): Promise<interfaces.IToolExecutionResult> {
|
|
60
|
-
this.validateAction(action);
|
|
61
|
-
|
|
62
|
-
switch (action) {
|
|
63
|
-
case 'validate':
|
|
64
|
-
return this.validateJson(params);
|
|
65
|
-
case 'format':
|
|
66
|
-
return this.formatJson(params);
|
|
67
|
-
default:
|
|
68
|
-
return { success: false, error: `Unknown action: ${action}` };
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Validate JSON string and optionally check for required fields
|
|
74
|
-
*/
|
|
75
|
-
private validateJson(params: Record<string, unknown>): interfaces.IToolExecutionResult {
|
|
76
|
-
const jsonString = params.jsonString as string;
|
|
77
|
-
const requiredFields = params.requiredFields as string[] | undefined;
|
|
78
|
-
|
|
79
|
-
if (!jsonString || typeof jsonString !== 'string') {
|
|
80
|
-
return {
|
|
81
|
-
success: false,
|
|
82
|
-
error: 'jsonString parameter is required and must be a string',
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
try {
|
|
87
|
-
const parsed = JSON.parse(jsonString);
|
|
88
|
-
|
|
89
|
-
// Check required fields if specified
|
|
90
|
-
if (requiredFields && Array.isArray(requiredFields)) {
|
|
91
|
-
const missingFields = requiredFields.filter((field) => {
|
|
92
|
-
if (typeof parsed !== 'object' || parsed === null) {
|
|
93
|
-
return true;
|
|
94
|
-
}
|
|
95
|
-
return !(field in parsed);
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
if (missingFields.length > 0) {
|
|
99
|
-
return {
|
|
100
|
-
success: false,
|
|
101
|
-
error: `Missing required fields: ${missingFields.join(', ')}`,
|
|
102
|
-
result: {
|
|
103
|
-
valid: false,
|
|
104
|
-
missingFields,
|
|
105
|
-
presentFields: Object.keys(parsed || {}),
|
|
106
|
-
},
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
return {
|
|
112
|
-
success: true,
|
|
113
|
-
result: {
|
|
114
|
-
valid: true,
|
|
115
|
-
parsed,
|
|
116
|
-
type: Array.isArray(parsed) ? 'array' : typeof parsed,
|
|
117
|
-
fieldCount: typeof parsed === 'object' && parsed !== null ? Object.keys(parsed).length : undefined,
|
|
118
|
-
},
|
|
119
|
-
summary: `JSON is valid (${Array.isArray(parsed) ? 'array' : typeof parsed})`,
|
|
120
|
-
};
|
|
121
|
-
} catch (error) {
|
|
122
|
-
const errorMessage = (error as Error).message;
|
|
123
|
-
|
|
124
|
-
// Extract position from error message if available
|
|
125
|
-
const posMatch = errorMessage.match(/position\s*(\d+)/i);
|
|
126
|
-
const position = posMatch ? parseInt(posMatch[1]) : undefined;
|
|
127
|
-
|
|
128
|
-
// Provide context around the error position
|
|
129
|
-
let context: string | undefined;
|
|
130
|
-
if (position !== undefined) {
|
|
131
|
-
const start = Math.max(0, position - 20);
|
|
132
|
-
const end = Math.min(jsonString.length, position + 20);
|
|
133
|
-
context = jsonString.substring(start, end);
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
return {
|
|
137
|
-
success: false,
|
|
138
|
-
error: `Invalid JSON: ${errorMessage}`,
|
|
139
|
-
result: {
|
|
140
|
-
valid: false,
|
|
141
|
-
errorPosition: position,
|
|
142
|
-
errorContext: context,
|
|
143
|
-
},
|
|
144
|
-
};
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* Format/pretty-print JSON string
|
|
150
|
-
*/
|
|
151
|
-
private formatJson(params: Record<string, unknown>): interfaces.IToolExecutionResult {
|
|
152
|
-
const jsonString = params.jsonString as string;
|
|
153
|
-
|
|
154
|
-
if (!jsonString || typeof jsonString !== 'string') {
|
|
155
|
-
return {
|
|
156
|
-
success: false,
|
|
157
|
-
error: 'jsonString parameter is required and must be a string',
|
|
158
|
-
};
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
try {
|
|
162
|
-
const parsed = JSON.parse(jsonString);
|
|
163
|
-
const formatted = JSON.stringify(parsed, null, 2);
|
|
164
|
-
|
|
165
|
-
return {
|
|
166
|
-
success: true,
|
|
167
|
-
result: formatted,
|
|
168
|
-
summary: `Formatted JSON (${formatted.length} chars)`,
|
|
169
|
-
};
|
|
170
|
-
} catch (error) {
|
|
171
|
-
return {
|
|
172
|
-
success: false,
|
|
173
|
-
error: `Cannot format invalid JSON: ${(error as Error).message}`,
|
|
174
|
-
};
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
public getToolExplanation(): string {
|
|
179
|
-
return `## Tool: json
|
|
180
|
-
Validate and format JSON data. Use this to verify your JSON output is valid before completing a task.
|
|
181
|
-
|
|
182
|
-
### Actions:
|
|
183
|
-
|
|
184
|
-
**validate** - Validate that a string is valid JSON and optionally check required fields
|
|
185
|
-
Parameters:
|
|
186
|
-
- jsonString (required): The JSON string to validate
|
|
187
|
-
- requiredFields (optional): Array of field names that must be present
|
|
188
|
-
|
|
189
|
-
Example:
|
|
190
|
-
<tool_call>
|
|
191
|
-
<tool>json</tool>
|
|
192
|
-
<action>validate</action>
|
|
193
|
-
<params>{"jsonString": "{\\"invoice_number\\":\\"INV-001\\",\\"total\\":99.99}", "requiredFields": ["invoice_number", "total"]}</params>
|
|
194
|
-
</tool_call>
|
|
195
|
-
|
|
196
|
-
**format** - Parse and pretty-print JSON string
|
|
197
|
-
Parameters:
|
|
198
|
-
- jsonString (required): The JSON string to format
|
|
199
|
-
|
|
200
|
-
Example:
|
|
201
|
-
<tool_call>
|
|
202
|
-
<tool>json</tool>
|
|
203
|
-
<action>format</action>
|
|
204
|
-
<params>{"jsonString": "{\\"name\\":\\"test\\",\\"value\\":123}"}</params>
|
|
205
|
-
</tool_call>
|
|
206
|
-
`;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
getCallSummary(action: string, params: Record<string, unknown>): string {
|
|
210
|
-
const jsonStr = (params.jsonString as string) || '';
|
|
211
|
-
const preview = jsonStr.length > 50 ? jsonStr.substring(0, 50) + '...' : jsonStr;
|
|
212
|
-
|
|
213
|
-
switch (action) {
|
|
214
|
-
case 'validate':
|
|
215
|
-
const fields = params.requiredFields as string[] | undefined;
|
|
216
|
-
const fieldInfo = fields ? ` (checking fields: ${fields.join(', ')})` : '';
|
|
217
|
-
return `Validate JSON: ${preview}${fieldInfo}`;
|
|
218
|
-
case 'format':
|
|
219
|
-
return `Format JSON: ${preview}`;
|
|
220
|
-
default:
|
|
221
|
-
return `JSON ${action}: ${preview}`;
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
}
|