@push.rocks/smartagent 1.2.7 → 1.4.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 +1 -1
- package/dist_ts/index.d.ts +1 -0
- package/dist_ts/index.js +2 -1
- package/dist_ts/smartagent.classes.driveragent.d.ts +11 -1
- package/dist_ts/smartagent.classes.driveragent.js +57 -16
- package/dist_ts/smartagent.classes.dualagent.d.ts +3 -1
- package/dist_ts/smartagent.classes.dualagent.js +13 -4
- package/dist_ts/smartagent.interfaces.d.ts +9 -0
- package/dist_ts/smartagent.interfaces.js +1 -1
- package/dist_ts/smartagent.tools.base.d.ts +7 -1
- package/dist_ts/smartagent.tools.base.js +3 -6
- package/dist_ts/smartagent.tools.browser.d.ts +1 -0
- package/dist_ts/smartagent.tools.browser.js +53 -1
- package/dist_ts/smartagent.tools.deno.d.ts +1 -0
- package/dist_ts/smartagent.tools.deno.js +39 -1
- package/dist_ts/smartagent.tools.filesystem.d.ts +1 -0
- package/dist_ts/smartagent.tools.filesystem.js +164 -1
- package/dist_ts/smartagent.tools.http.d.ts +1 -0
- package/dist_ts/smartagent.tools.http.js +78 -1
- package/dist_ts/smartagent.tools.json.d.ts +24 -0
- package/dist_ts/smartagent.tools.json.js +202 -0
- package/dist_ts/smartagent.tools.shell.d.ts +1 -0
- package/dist_ts/smartagent.tools.shell.js +48 -1
- package/package.json +2 -2
- package/readme.hints.md +29 -5
- package/readme.md +145 -9
- package/ts/00_commitinfo_data.ts +1 -1
- package/ts/index.ts +1 -0
- package/ts/smartagent.classes.driveragent.ts +59 -15
- package/ts/smartagent.classes.dualagent.ts +14 -3
- package/ts/smartagent.interfaces.ts +14 -0
- package/ts/smartagent.tools.base.ts +9 -6
- package/ts/smartagent.tools.browser.ts +53 -0
- package/ts/smartagent.tools.deno.ts +39 -0
- package/ts/smartagent.tools.filesystem.ts +164 -0
- package/ts/smartagent.tools.http.ts +78 -0
- package/ts/smartagent.tools.json.ts +224 -0
- package/ts/smartagent.tools.shell.ts +48 -0
|
@@ -0,0 +1,224 @@
|
|
|
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
|
+
}
|
|
@@ -148,6 +148,54 @@ export class ShellTool extends BaseToolWrapper {
|
|
|
148
148
|
}
|
|
149
149
|
}
|
|
150
150
|
|
|
151
|
+
public getToolExplanation(): string {
|
|
152
|
+
return `## Tool: shell
|
|
153
|
+
Execute shell commands securely. Uses execSpawn (shell:false) to prevent command injection.
|
|
154
|
+
|
|
155
|
+
### Actions:
|
|
156
|
+
|
|
157
|
+
**execute** - Execute a command with arguments (secure, no shell injection possible)
|
|
158
|
+
Parameters:
|
|
159
|
+
- command (required): The command to execute (e.g., "ls", "cat", "grep", "node")
|
|
160
|
+
- args (optional): Array of arguments (each argument is properly escaped)
|
|
161
|
+
- cwd (optional): Working directory for the command
|
|
162
|
+
- timeout (optional): Timeout in milliseconds
|
|
163
|
+
- env (optional): Additional environment variables (key-value object)
|
|
164
|
+
|
|
165
|
+
Example - List files:
|
|
166
|
+
<tool_call>
|
|
167
|
+
<tool>shell</tool>
|
|
168
|
+
<action>execute</action>
|
|
169
|
+
<params>{"command": "ls", "args": ["-la", "/path/to/dir"]}</params>
|
|
170
|
+
</tool_call>
|
|
171
|
+
|
|
172
|
+
Example - Run Node script:
|
|
173
|
+
<tool_call>
|
|
174
|
+
<tool>shell</tool>
|
|
175
|
+
<action>execute</action>
|
|
176
|
+
<params>{"command": "node", "args": ["script.js"], "cwd": "/path/to/project"}</params>
|
|
177
|
+
</tool_call>
|
|
178
|
+
|
|
179
|
+
Example - Search in files:
|
|
180
|
+
<tool_call>
|
|
181
|
+
<tool>shell</tool>
|
|
182
|
+
<action>execute</action>
|
|
183
|
+
<params>{"command": "grep", "args": ["-r", "pattern", "src/"]}</params>
|
|
184
|
+
</tool_call>
|
|
185
|
+
|
|
186
|
+
**which** - Check if a command exists and get its path
|
|
187
|
+
Parameters:
|
|
188
|
+
- command (required): Command name to look up (e.g., "node", "git")
|
|
189
|
+
|
|
190
|
+
Example:
|
|
191
|
+
<tool_call>
|
|
192
|
+
<tool>shell</tool>
|
|
193
|
+
<action>which</action>
|
|
194
|
+
<params>{"command": "node"}</params>
|
|
195
|
+
</tool_call>
|
|
196
|
+
`;
|
|
197
|
+
}
|
|
198
|
+
|
|
151
199
|
public getCallSummary(action: string, params: Record<string, unknown>): string {
|
|
152
200
|
switch (action) {
|
|
153
201
|
case 'execute': {
|