@alexma03/utcp-cli 1.1.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/README.md +519 -0
- package/dist/index.cjs +4432 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +319 -0
- package/dist/index.d.ts +319 -0
- package/dist/index.js +4409 -0
- package/dist/index.js.map +1 -0
- package/package.json +53 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { CallTemplate, Serializer, CommunicationProtocol, IUtcpClient, RegisterManualResult } from '@alexma03/utcp-sdk';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* REQUIRED
|
|
6
|
+
* Configuration for a single command step in a CLI execution flow.
|
|
7
|
+
*
|
|
8
|
+
* Attributes:
|
|
9
|
+
* command: The command string to execute. Can contain UTCP_ARG_argname_UTCP_END
|
|
10
|
+
* placeholders that will be replaced with values from tool_args. Can also
|
|
11
|
+
* reference previous command outputs using $CMD_0_OUTPUT, $CMD_1_OUTPUT, etc.
|
|
12
|
+
* append_to_final_output: Whether this command's output should be included
|
|
13
|
+
* in the final result. If not specified, defaults to False for all
|
|
14
|
+
* commands except the last one.
|
|
15
|
+
*
|
|
16
|
+
* Examples:
|
|
17
|
+
* Basic command step:
|
|
18
|
+
* ```json
|
|
19
|
+
* {
|
|
20
|
+
* "command": "git status",
|
|
21
|
+
* "append_to_final_output": true
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* Command with argument placeholders and output reference:
|
|
26
|
+
* ```json
|
|
27
|
+
* {
|
|
28
|
+
* "command": "echo \"Cloning to: UTCP_ARG_target_dir_UTCP_END, previous status: $CMD_0_OUTPUT\"",
|
|
29
|
+
* "append_to_final_output": true
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
interface CommandStep {
|
|
34
|
+
/**
|
|
35
|
+
* Command string to execute, may contain UTCP_ARG_argname_UTCP_END placeholders
|
|
36
|
+
*/
|
|
37
|
+
command: string;
|
|
38
|
+
/**
|
|
39
|
+
* Whether to include this command's output in final result. Defaults to False for all except last command
|
|
40
|
+
*/
|
|
41
|
+
append_to_final_output?: boolean | null;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Zod schema for CommandStep.
|
|
45
|
+
*/
|
|
46
|
+
declare const CommandStepSchema: z.ZodType<CommandStep>;
|
|
47
|
+
/**
|
|
48
|
+
* REQUIRED
|
|
49
|
+
* Call template configuration for Command Line Interface (CLI) tools.
|
|
50
|
+
*
|
|
51
|
+
* This class defines the configuration for executing command-line tools and
|
|
52
|
+
* programs as UTCP tool providers. Commands are executed in a single subprocess
|
|
53
|
+
* to maintain state (like directory changes) between commands.
|
|
54
|
+
*
|
|
55
|
+
* **Cross-Platform Script Generation:**
|
|
56
|
+
* - **Windows**: Commands are converted to a PowerShell script
|
|
57
|
+
* - **Unix/Linux/macOS**: Commands are converted to a Bash script
|
|
58
|
+
*
|
|
59
|
+
* **Command Syntax Requirements:**
|
|
60
|
+
* - Windows: Use PowerShell syntax (e.g., `Get-ChildItem`, `Set-Location`)
|
|
61
|
+
* - Unix: Use Bash/shell syntax (e.g., `ls`, `cd`)
|
|
62
|
+
*
|
|
63
|
+
* **Referencing Previous Command Output:**
|
|
64
|
+
* You can reference the output of previous commands using variables:
|
|
65
|
+
* - **PowerShell**: `$CMD_0_OUTPUT`, `$CMD_1_OUTPUT`, etc.
|
|
66
|
+
* - **Bash**: `$CMD_0_OUTPUT`, `$CMD_1_OUTPUT`, etc.
|
|
67
|
+
*
|
|
68
|
+
* Example: `echo "Previous result: $CMD_0_OUTPUT"`
|
|
69
|
+
*
|
|
70
|
+
* Attributes:
|
|
71
|
+
* call_template_type: The type of the call template. Must be "cli".
|
|
72
|
+
* commands: A list of CommandStep objects defining the commands to execute
|
|
73
|
+
* in order. Each command can contain UTCP_ARG_argname_UTCP_END placeholders
|
|
74
|
+
* that will be replaced with values from tool_args during execution.
|
|
75
|
+
* env_vars: A dictionary of environment variables to set for the command's
|
|
76
|
+
* execution context. Values can be static strings or placeholders for
|
|
77
|
+
* variables from the UTCP client's variable substitutor.
|
|
78
|
+
* working_dir: The working directory from which to run the commands. If not
|
|
79
|
+
* provided, it defaults to the current process's working directory.
|
|
80
|
+
* auth: Authentication details. Not applicable to the CLI protocol, so it
|
|
81
|
+
* is always None.
|
|
82
|
+
*
|
|
83
|
+
* Examples:
|
|
84
|
+
* Cross-platform directory operations:
|
|
85
|
+
* ```json
|
|
86
|
+
* {
|
|
87
|
+
* "name": "cross_platform_dir_tool",
|
|
88
|
+
* "call_template_type": "cli",
|
|
89
|
+
* "commands": [
|
|
90
|
+
* {
|
|
91
|
+
* "command": "cd UTCP_ARG_target_dir_UTCP_END",
|
|
92
|
+
* "append_to_final_output": false
|
|
93
|
+
* },
|
|
94
|
+
* {
|
|
95
|
+
* "command": "ls -la",
|
|
96
|
+
* "append_to_final_output": true
|
|
97
|
+
* }
|
|
98
|
+
* ]
|
|
99
|
+
* }
|
|
100
|
+
* ```
|
|
101
|
+
*
|
|
102
|
+
* Referencing previous command output:
|
|
103
|
+
* ```json
|
|
104
|
+
* {
|
|
105
|
+
* "name": "reference_previous_output_tool",
|
|
106
|
+
* "call_template_type": "cli",
|
|
107
|
+
* "commands": [
|
|
108
|
+
* {
|
|
109
|
+
* "command": "git status --porcelain",
|
|
110
|
+
* "append_to_final_output": false
|
|
111
|
+
* },
|
|
112
|
+
* {
|
|
113
|
+
* "command": "echo \"Found changes: $CMD_0_OUTPUT\"",
|
|
114
|
+
* "append_to_final_output": true
|
|
115
|
+
* }
|
|
116
|
+
* ]
|
|
117
|
+
* }
|
|
118
|
+
* ```
|
|
119
|
+
*
|
|
120
|
+
* Command with environment variables and placeholders:
|
|
121
|
+
* ```json
|
|
122
|
+
* {
|
|
123
|
+
* "name": "python_multi_step_tool",
|
|
124
|
+
* "call_template_type": "cli",
|
|
125
|
+
* "commands": [
|
|
126
|
+
* {
|
|
127
|
+
* "command": "python setup.py install",
|
|
128
|
+
* "append_to_final_output": false
|
|
129
|
+
* },
|
|
130
|
+
* {
|
|
131
|
+
* "command": "python script.py --input UTCP_ARG_input_file_UTCP_END --result \"$CMD_0_OUTPUT\""
|
|
132
|
+
* }
|
|
133
|
+
* ],
|
|
134
|
+
* "env_vars": {
|
|
135
|
+
* "PYTHONPATH": "/custom/path",
|
|
136
|
+
* "API_KEY": "${API_KEY_VAR}"
|
|
137
|
+
* }
|
|
138
|
+
* }
|
|
139
|
+
* ```
|
|
140
|
+
*
|
|
141
|
+
* Security Considerations:
|
|
142
|
+
* - Commands are executed in a subprocess. Ensure that the commands
|
|
143
|
+
* specified are from a trusted source.
|
|
144
|
+
* - Avoid passing unsanitized user input directly into the command string.
|
|
145
|
+
* Use tool argument validation where possible.
|
|
146
|
+
* - All placeholders are replaced with string values from tool_args.
|
|
147
|
+
* - Commands should use the appropriate syntax for the target platform
|
|
148
|
+
* (PowerShell on Windows, Bash on Unix).
|
|
149
|
+
* - Previous command outputs are available as variables but should be
|
|
150
|
+
* used carefully to avoid command injection.
|
|
151
|
+
*/
|
|
152
|
+
interface CliCallTemplate extends CallTemplate {
|
|
153
|
+
call_template_type: 'cli';
|
|
154
|
+
commands: CommandStep[];
|
|
155
|
+
env_vars?: Record<string, string> | null;
|
|
156
|
+
working_dir?: string | null;
|
|
157
|
+
auth?: undefined;
|
|
158
|
+
allowed_communication_protocols?: string[];
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Zod schema for CLI Call Template.
|
|
162
|
+
*/
|
|
163
|
+
declare const CliCallTemplateSchema: z.ZodType<CliCallTemplate>;
|
|
164
|
+
/**
|
|
165
|
+
* REQUIRED
|
|
166
|
+
* Serializer for converting between `CliCallTemplate` and dictionary representations.
|
|
167
|
+
*
|
|
168
|
+
* This class handles the serialization and deserialization of `CliCallTemplate`
|
|
169
|
+
* objects, ensuring that they can be correctly represented as dictionaries and
|
|
170
|
+
* reconstructed from them, with validation.
|
|
171
|
+
*/
|
|
172
|
+
declare class CliCallTemplateSerializer extends Serializer<CliCallTemplate> {
|
|
173
|
+
/**
|
|
174
|
+
* REQUIRED
|
|
175
|
+
* Converts a `CliCallTemplate` instance to its dictionary representation.
|
|
176
|
+
*
|
|
177
|
+
* @param obj The `CliCallTemplate` instance to serialize.
|
|
178
|
+
* @returns A dictionary representing the `CliCallTemplate`.
|
|
179
|
+
*/
|
|
180
|
+
toDict(obj: CliCallTemplate): Record<string, unknown>;
|
|
181
|
+
/**
|
|
182
|
+
* REQUIRED
|
|
183
|
+
* Validates a dictionary and constructs a `CliCallTemplate` instance.
|
|
184
|
+
*
|
|
185
|
+
* @param obj The dictionary to validate and deserialize.
|
|
186
|
+
* @returns A `CliCallTemplate` instance.
|
|
187
|
+
* @throws Error if the dictionary is not a valid representation of a `CliCallTemplate`.
|
|
188
|
+
*/
|
|
189
|
+
validateDict(obj: Record<string, unknown>): CliCallTemplate;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Command Line Interface (CLI) communication protocol for the UTCP client.
|
|
194
|
+
*
|
|
195
|
+
* This module provides an implementation of the `CommunicationProtocol` interface
|
|
196
|
+
* that enables the UTCP client to interact with command-line tools. It supports
|
|
197
|
+
* discovering tools by executing a command and parsing its output for a UTCP
|
|
198
|
+
* manual, as well as calling those tools with arguments.
|
|
199
|
+
*
|
|
200
|
+
* Key Features:
|
|
201
|
+
* - Asynchronous execution of shell commands.
|
|
202
|
+
* - Tool discovery by running a command that outputs a UTCP manual.
|
|
203
|
+
* - Flexible argument formatting for different CLI conventions.
|
|
204
|
+
* - Support for environment variables and custom working directories.
|
|
205
|
+
* - Cross-platform command parsing for Windows and Unix-like systems.
|
|
206
|
+
*
|
|
207
|
+
* Security Considerations:
|
|
208
|
+
* Executing arbitrary command-line tools can be dangerous. This protocol
|
|
209
|
+
* should only be used with trusted tools.
|
|
210
|
+
*/
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* REQUIRED
|
|
214
|
+
* Communication protocol for interacting with CLI-based tool providers.
|
|
215
|
+
*
|
|
216
|
+
* This class implements the `CommunicationProtocol` interface to handle
|
|
217
|
+
* communication with command-line tools. It discovers tools by executing a
|
|
218
|
+
* command specified in a `CliCallTemplate` and parsing the output for a UTCP
|
|
219
|
+
* manual. It also executes tool calls by running the corresponding command
|
|
220
|
+
* with the provided arguments.
|
|
221
|
+
*/
|
|
222
|
+
declare class CliCommunicationProtocol implements CommunicationProtocol {
|
|
223
|
+
/**
|
|
224
|
+
* Log informational messages.
|
|
225
|
+
*/
|
|
226
|
+
private _log_info;
|
|
227
|
+
/**
|
|
228
|
+
* Log error messages.
|
|
229
|
+
*/
|
|
230
|
+
private _log_error;
|
|
231
|
+
/**
|
|
232
|
+
* Prepare environment variables for command execution.
|
|
233
|
+
*
|
|
234
|
+
* @param provider The CLI provider
|
|
235
|
+
* @returns Environment variables dictionary
|
|
236
|
+
*/
|
|
237
|
+
private _prepare_environment;
|
|
238
|
+
private _executeShellScript;
|
|
239
|
+
/**
|
|
240
|
+
* Substitute UTCP_ARG placeholders in command string with tool arguments.
|
|
241
|
+
*
|
|
242
|
+
* @param command Command string containing UTCP_ARG_argname_UTCP_END placeholders
|
|
243
|
+
* @param toolArgs Dictionary of argument names and values
|
|
244
|
+
* @returns Command string with placeholders replaced by actual values
|
|
245
|
+
*/
|
|
246
|
+
private _substitute_utcp_args;
|
|
247
|
+
/**
|
|
248
|
+
* Build a combined shell script from multiple commands.
|
|
249
|
+
*
|
|
250
|
+
* @param commands List of CommandStep objects to combine
|
|
251
|
+
* @param toolArgs Tool arguments for placeholder substitution
|
|
252
|
+
* @returns Shell script string that executes all commands in sequence
|
|
253
|
+
*/
|
|
254
|
+
private _build_combined_shell_script;
|
|
255
|
+
/**
|
|
256
|
+
* REQUIRED
|
|
257
|
+
* Registers a CLI-based manual and discovers its tools.
|
|
258
|
+
*
|
|
259
|
+
* This method executes the command specified in the `CliCallTemplate`'s
|
|
260
|
+
* commands. It then attempts to parse the command's output (stdout) as a
|
|
261
|
+
* UTCP manual in JSON format.
|
|
262
|
+
*
|
|
263
|
+
* @param caller The UTCP client instance that is calling this method.
|
|
264
|
+
* @param manualCallTemplate The `CliCallTemplate` containing the details for
|
|
265
|
+
* tool discovery, such as the command to run.
|
|
266
|
+
* @returns A `RegisterManualResult` object indicating whether the registration
|
|
267
|
+
* was successful and containing the discovered tools.
|
|
268
|
+
* @throws Error if the `manualCallTemplate` is not an instance of
|
|
269
|
+
* `CliCallTemplate` or if commands are not set.
|
|
270
|
+
*/
|
|
271
|
+
registerManual(caller: IUtcpClient, manualCallTemplate: CallTemplate): Promise<RegisterManualResult>;
|
|
272
|
+
/**
|
|
273
|
+
* REQUIRED
|
|
274
|
+
* Deregisters a CLI manual.
|
|
275
|
+
*
|
|
276
|
+
* For the CLI protocol, this is a no-op as there are no persistent
|
|
277
|
+
* connections to terminate.
|
|
278
|
+
*
|
|
279
|
+
* @param caller The UTCP client instance that is calling this method.
|
|
280
|
+
* @param manualCallTemplate The call template of the manual to deregister.
|
|
281
|
+
*/
|
|
282
|
+
deregisterManual(caller: IUtcpClient, manualCallTemplate: CallTemplate): Promise<void>;
|
|
283
|
+
/**
|
|
284
|
+
* REQUIRED
|
|
285
|
+
* Calls a CLI tool by executing its command.
|
|
286
|
+
*
|
|
287
|
+
* This method constructs and executes the command specified in the
|
|
288
|
+
* `CliCallTemplate`. It formats the provided `tool_args` as command-line
|
|
289
|
+
* arguments and runs the command in a subprocess.
|
|
290
|
+
*
|
|
291
|
+
* @param caller The UTCP client instance that is calling this method.
|
|
292
|
+
* @param toolName The name of the tool to call.
|
|
293
|
+
* @param toolArgs A dictionary of arguments for the tool call.
|
|
294
|
+
* @param toolCallTemplate The `CliCallTemplate` for the tool.
|
|
295
|
+
* @returns The result of the command execution. If the command exits with a code
|
|
296
|
+
* of 0, it returns the content of stdout. If the exit code is non-zero,
|
|
297
|
+
* it returns the content of stderr.
|
|
298
|
+
* @throws Error if `toolCallTemplate` is not an instance of
|
|
299
|
+
* `CliCallTemplate` or if commands are not set.
|
|
300
|
+
*/
|
|
301
|
+
callTool(caller: IUtcpClient, toolName: string, toolArgs: Record<string, any>, toolCallTemplate: CallTemplate): Promise<any>;
|
|
302
|
+
/**
|
|
303
|
+
* REQUIRED
|
|
304
|
+
* Streaming calls are not supported for the CLI protocol.
|
|
305
|
+
*
|
|
306
|
+
* @throws Error Always, as this functionality is not supported.
|
|
307
|
+
*/
|
|
308
|
+
callToolStreaming(caller: IUtcpClient, toolName: string, toolArgs: Record<string, any>, toolCallTemplate: CallTemplate): AsyncGenerator<any, void, unknown>;
|
|
309
|
+
close(): Promise<void>;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Registers the CLI protocol's CallTemplate serializer
|
|
314
|
+
* and its CommunicationProtocol implementation.
|
|
315
|
+
* This function should be called once when the CLI plugin is loaded.
|
|
316
|
+
*/
|
|
317
|
+
declare function register(override?: boolean): void;
|
|
318
|
+
|
|
319
|
+
export { type CliCallTemplate, CliCallTemplateSchema, CliCallTemplateSerializer, CliCommunicationProtocol, type CommandStep, CommandStepSchema, register };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { CallTemplate, Serializer, CommunicationProtocol, IUtcpClient, RegisterManualResult } from '@alexma03/utcp-sdk';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* REQUIRED
|
|
6
|
+
* Configuration for a single command step in a CLI execution flow.
|
|
7
|
+
*
|
|
8
|
+
* Attributes:
|
|
9
|
+
* command: The command string to execute. Can contain UTCP_ARG_argname_UTCP_END
|
|
10
|
+
* placeholders that will be replaced with values from tool_args. Can also
|
|
11
|
+
* reference previous command outputs using $CMD_0_OUTPUT, $CMD_1_OUTPUT, etc.
|
|
12
|
+
* append_to_final_output: Whether this command's output should be included
|
|
13
|
+
* in the final result. If not specified, defaults to False for all
|
|
14
|
+
* commands except the last one.
|
|
15
|
+
*
|
|
16
|
+
* Examples:
|
|
17
|
+
* Basic command step:
|
|
18
|
+
* ```json
|
|
19
|
+
* {
|
|
20
|
+
* "command": "git status",
|
|
21
|
+
* "append_to_final_output": true
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* Command with argument placeholders and output reference:
|
|
26
|
+
* ```json
|
|
27
|
+
* {
|
|
28
|
+
* "command": "echo \"Cloning to: UTCP_ARG_target_dir_UTCP_END, previous status: $CMD_0_OUTPUT\"",
|
|
29
|
+
* "append_to_final_output": true
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
interface CommandStep {
|
|
34
|
+
/**
|
|
35
|
+
* Command string to execute, may contain UTCP_ARG_argname_UTCP_END placeholders
|
|
36
|
+
*/
|
|
37
|
+
command: string;
|
|
38
|
+
/**
|
|
39
|
+
* Whether to include this command's output in final result. Defaults to False for all except last command
|
|
40
|
+
*/
|
|
41
|
+
append_to_final_output?: boolean | null;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Zod schema for CommandStep.
|
|
45
|
+
*/
|
|
46
|
+
declare const CommandStepSchema: z.ZodType<CommandStep>;
|
|
47
|
+
/**
|
|
48
|
+
* REQUIRED
|
|
49
|
+
* Call template configuration for Command Line Interface (CLI) tools.
|
|
50
|
+
*
|
|
51
|
+
* This class defines the configuration for executing command-line tools and
|
|
52
|
+
* programs as UTCP tool providers. Commands are executed in a single subprocess
|
|
53
|
+
* to maintain state (like directory changes) between commands.
|
|
54
|
+
*
|
|
55
|
+
* **Cross-Platform Script Generation:**
|
|
56
|
+
* - **Windows**: Commands are converted to a PowerShell script
|
|
57
|
+
* - **Unix/Linux/macOS**: Commands are converted to a Bash script
|
|
58
|
+
*
|
|
59
|
+
* **Command Syntax Requirements:**
|
|
60
|
+
* - Windows: Use PowerShell syntax (e.g., `Get-ChildItem`, `Set-Location`)
|
|
61
|
+
* - Unix: Use Bash/shell syntax (e.g., `ls`, `cd`)
|
|
62
|
+
*
|
|
63
|
+
* **Referencing Previous Command Output:**
|
|
64
|
+
* You can reference the output of previous commands using variables:
|
|
65
|
+
* - **PowerShell**: `$CMD_0_OUTPUT`, `$CMD_1_OUTPUT`, etc.
|
|
66
|
+
* - **Bash**: `$CMD_0_OUTPUT`, `$CMD_1_OUTPUT`, etc.
|
|
67
|
+
*
|
|
68
|
+
* Example: `echo "Previous result: $CMD_0_OUTPUT"`
|
|
69
|
+
*
|
|
70
|
+
* Attributes:
|
|
71
|
+
* call_template_type: The type of the call template. Must be "cli".
|
|
72
|
+
* commands: A list of CommandStep objects defining the commands to execute
|
|
73
|
+
* in order. Each command can contain UTCP_ARG_argname_UTCP_END placeholders
|
|
74
|
+
* that will be replaced with values from tool_args during execution.
|
|
75
|
+
* env_vars: A dictionary of environment variables to set for the command's
|
|
76
|
+
* execution context. Values can be static strings or placeholders for
|
|
77
|
+
* variables from the UTCP client's variable substitutor.
|
|
78
|
+
* working_dir: The working directory from which to run the commands. If not
|
|
79
|
+
* provided, it defaults to the current process's working directory.
|
|
80
|
+
* auth: Authentication details. Not applicable to the CLI protocol, so it
|
|
81
|
+
* is always None.
|
|
82
|
+
*
|
|
83
|
+
* Examples:
|
|
84
|
+
* Cross-platform directory operations:
|
|
85
|
+
* ```json
|
|
86
|
+
* {
|
|
87
|
+
* "name": "cross_platform_dir_tool",
|
|
88
|
+
* "call_template_type": "cli",
|
|
89
|
+
* "commands": [
|
|
90
|
+
* {
|
|
91
|
+
* "command": "cd UTCP_ARG_target_dir_UTCP_END",
|
|
92
|
+
* "append_to_final_output": false
|
|
93
|
+
* },
|
|
94
|
+
* {
|
|
95
|
+
* "command": "ls -la",
|
|
96
|
+
* "append_to_final_output": true
|
|
97
|
+
* }
|
|
98
|
+
* ]
|
|
99
|
+
* }
|
|
100
|
+
* ```
|
|
101
|
+
*
|
|
102
|
+
* Referencing previous command output:
|
|
103
|
+
* ```json
|
|
104
|
+
* {
|
|
105
|
+
* "name": "reference_previous_output_tool",
|
|
106
|
+
* "call_template_type": "cli",
|
|
107
|
+
* "commands": [
|
|
108
|
+
* {
|
|
109
|
+
* "command": "git status --porcelain",
|
|
110
|
+
* "append_to_final_output": false
|
|
111
|
+
* },
|
|
112
|
+
* {
|
|
113
|
+
* "command": "echo \"Found changes: $CMD_0_OUTPUT\"",
|
|
114
|
+
* "append_to_final_output": true
|
|
115
|
+
* }
|
|
116
|
+
* ]
|
|
117
|
+
* }
|
|
118
|
+
* ```
|
|
119
|
+
*
|
|
120
|
+
* Command with environment variables and placeholders:
|
|
121
|
+
* ```json
|
|
122
|
+
* {
|
|
123
|
+
* "name": "python_multi_step_tool",
|
|
124
|
+
* "call_template_type": "cli",
|
|
125
|
+
* "commands": [
|
|
126
|
+
* {
|
|
127
|
+
* "command": "python setup.py install",
|
|
128
|
+
* "append_to_final_output": false
|
|
129
|
+
* },
|
|
130
|
+
* {
|
|
131
|
+
* "command": "python script.py --input UTCP_ARG_input_file_UTCP_END --result \"$CMD_0_OUTPUT\""
|
|
132
|
+
* }
|
|
133
|
+
* ],
|
|
134
|
+
* "env_vars": {
|
|
135
|
+
* "PYTHONPATH": "/custom/path",
|
|
136
|
+
* "API_KEY": "${API_KEY_VAR}"
|
|
137
|
+
* }
|
|
138
|
+
* }
|
|
139
|
+
* ```
|
|
140
|
+
*
|
|
141
|
+
* Security Considerations:
|
|
142
|
+
* - Commands are executed in a subprocess. Ensure that the commands
|
|
143
|
+
* specified are from a trusted source.
|
|
144
|
+
* - Avoid passing unsanitized user input directly into the command string.
|
|
145
|
+
* Use tool argument validation where possible.
|
|
146
|
+
* - All placeholders are replaced with string values from tool_args.
|
|
147
|
+
* - Commands should use the appropriate syntax for the target platform
|
|
148
|
+
* (PowerShell on Windows, Bash on Unix).
|
|
149
|
+
* - Previous command outputs are available as variables but should be
|
|
150
|
+
* used carefully to avoid command injection.
|
|
151
|
+
*/
|
|
152
|
+
interface CliCallTemplate extends CallTemplate {
|
|
153
|
+
call_template_type: 'cli';
|
|
154
|
+
commands: CommandStep[];
|
|
155
|
+
env_vars?: Record<string, string> | null;
|
|
156
|
+
working_dir?: string | null;
|
|
157
|
+
auth?: undefined;
|
|
158
|
+
allowed_communication_protocols?: string[];
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Zod schema for CLI Call Template.
|
|
162
|
+
*/
|
|
163
|
+
declare const CliCallTemplateSchema: z.ZodType<CliCallTemplate>;
|
|
164
|
+
/**
|
|
165
|
+
* REQUIRED
|
|
166
|
+
* Serializer for converting between `CliCallTemplate` and dictionary representations.
|
|
167
|
+
*
|
|
168
|
+
* This class handles the serialization and deserialization of `CliCallTemplate`
|
|
169
|
+
* objects, ensuring that they can be correctly represented as dictionaries and
|
|
170
|
+
* reconstructed from them, with validation.
|
|
171
|
+
*/
|
|
172
|
+
declare class CliCallTemplateSerializer extends Serializer<CliCallTemplate> {
|
|
173
|
+
/**
|
|
174
|
+
* REQUIRED
|
|
175
|
+
* Converts a `CliCallTemplate` instance to its dictionary representation.
|
|
176
|
+
*
|
|
177
|
+
* @param obj The `CliCallTemplate` instance to serialize.
|
|
178
|
+
* @returns A dictionary representing the `CliCallTemplate`.
|
|
179
|
+
*/
|
|
180
|
+
toDict(obj: CliCallTemplate): Record<string, unknown>;
|
|
181
|
+
/**
|
|
182
|
+
* REQUIRED
|
|
183
|
+
* Validates a dictionary and constructs a `CliCallTemplate` instance.
|
|
184
|
+
*
|
|
185
|
+
* @param obj The dictionary to validate and deserialize.
|
|
186
|
+
* @returns A `CliCallTemplate` instance.
|
|
187
|
+
* @throws Error if the dictionary is not a valid representation of a `CliCallTemplate`.
|
|
188
|
+
*/
|
|
189
|
+
validateDict(obj: Record<string, unknown>): CliCallTemplate;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Command Line Interface (CLI) communication protocol for the UTCP client.
|
|
194
|
+
*
|
|
195
|
+
* This module provides an implementation of the `CommunicationProtocol` interface
|
|
196
|
+
* that enables the UTCP client to interact with command-line tools. It supports
|
|
197
|
+
* discovering tools by executing a command and parsing its output for a UTCP
|
|
198
|
+
* manual, as well as calling those tools with arguments.
|
|
199
|
+
*
|
|
200
|
+
* Key Features:
|
|
201
|
+
* - Asynchronous execution of shell commands.
|
|
202
|
+
* - Tool discovery by running a command that outputs a UTCP manual.
|
|
203
|
+
* - Flexible argument formatting for different CLI conventions.
|
|
204
|
+
* - Support for environment variables and custom working directories.
|
|
205
|
+
* - Cross-platform command parsing for Windows and Unix-like systems.
|
|
206
|
+
*
|
|
207
|
+
* Security Considerations:
|
|
208
|
+
* Executing arbitrary command-line tools can be dangerous. This protocol
|
|
209
|
+
* should only be used with trusted tools.
|
|
210
|
+
*/
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* REQUIRED
|
|
214
|
+
* Communication protocol for interacting with CLI-based tool providers.
|
|
215
|
+
*
|
|
216
|
+
* This class implements the `CommunicationProtocol` interface to handle
|
|
217
|
+
* communication with command-line tools. It discovers tools by executing a
|
|
218
|
+
* command specified in a `CliCallTemplate` and parsing the output for a UTCP
|
|
219
|
+
* manual. It also executes tool calls by running the corresponding command
|
|
220
|
+
* with the provided arguments.
|
|
221
|
+
*/
|
|
222
|
+
declare class CliCommunicationProtocol implements CommunicationProtocol {
|
|
223
|
+
/**
|
|
224
|
+
* Log informational messages.
|
|
225
|
+
*/
|
|
226
|
+
private _log_info;
|
|
227
|
+
/**
|
|
228
|
+
* Log error messages.
|
|
229
|
+
*/
|
|
230
|
+
private _log_error;
|
|
231
|
+
/**
|
|
232
|
+
* Prepare environment variables for command execution.
|
|
233
|
+
*
|
|
234
|
+
* @param provider The CLI provider
|
|
235
|
+
* @returns Environment variables dictionary
|
|
236
|
+
*/
|
|
237
|
+
private _prepare_environment;
|
|
238
|
+
private _executeShellScript;
|
|
239
|
+
/**
|
|
240
|
+
* Substitute UTCP_ARG placeholders in command string with tool arguments.
|
|
241
|
+
*
|
|
242
|
+
* @param command Command string containing UTCP_ARG_argname_UTCP_END placeholders
|
|
243
|
+
* @param toolArgs Dictionary of argument names and values
|
|
244
|
+
* @returns Command string with placeholders replaced by actual values
|
|
245
|
+
*/
|
|
246
|
+
private _substitute_utcp_args;
|
|
247
|
+
/**
|
|
248
|
+
* Build a combined shell script from multiple commands.
|
|
249
|
+
*
|
|
250
|
+
* @param commands List of CommandStep objects to combine
|
|
251
|
+
* @param toolArgs Tool arguments for placeholder substitution
|
|
252
|
+
* @returns Shell script string that executes all commands in sequence
|
|
253
|
+
*/
|
|
254
|
+
private _build_combined_shell_script;
|
|
255
|
+
/**
|
|
256
|
+
* REQUIRED
|
|
257
|
+
* Registers a CLI-based manual and discovers its tools.
|
|
258
|
+
*
|
|
259
|
+
* This method executes the command specified in the `CliCallTemplate`'s
|
|
260
|
+
* commands. It then attempts to parse the command's output (stdout) as a
|
|
261
|
+
* UTCP manual in JSON format.
|
|
262
|
+
*
|
|
263
|
+
* @param caller The UTCP client instance that is calling this method.
|
|
264
|
+
* @param manualCallTemplate The `CliCallTemplate` containing the details for
|
|
265
|
+
* tool discovery, such as the command to run.
|
|
266
|
+
* @returns A `RegisterManualResult` object indicating whether the registration
|
|
267
|
+
* was successful and containing the discovered tools.
|
|
268
|
+
* @throws Error if the `manualCallTemplate` is not an instance of
|
|
269
|
+
* `CliCallTemplate` or if commands are not set.
|
|
270
|
+
*/
|
|
271
|
+
registerManual(caller: IUtcpClient, manualCallTemplate: CallTemplate): Promise<RegisterManualResult>;
|
|
272
|
+
/**
|
|
273
|
+
* REQUIRED
|
|
274
|
+
* Deregisters a CLI manual.
|
|
275
|
+
*
|
|
276
|
+
* For the CLI protocol, this is a no-op as there are no persistent
|
|
277
|
+
* connections to terminate.
|
|
278
|
+
*
|
|
279
|
+
* @param caller The UTCP client instance that is calling this method.
|
|
280
|
+
* @param manualCallTemplate The call template of the manual to deregister.
|
|
281
|
+
*/
|
|
282
|
+
deregisterManual(caller: IUtcpClient, manualCallTemplate: CallTemplate): Promise<void>;
|
|
283
|
+
/**
|
|
284
|
+
* REQUIRED
|
|
285
|
+
* Calls a CLI tool by executing its command.
|
|
286
|
+
*
|
|
287
|
+
* This method constructs and executes the command specified in the
|
|
288
|
+
* `CliCallTemplate`. It formats the provided `tool_args` as command-line
|
|
289
|
+
* arguments and runs the command in a subprocess.
|
|
290
|
+
*
|
|
291
|
+
* @param caller The UTCP client instance that is calling this method.
|
|
292
|
+
* @param toolName The name of the tool to call.
|
|
293
|
+
* @param toolArgs A dictionary of arguments for the tool call.
|
|
294
|
+
* @param toolCallTemplate The `CliCallTemplate` for the tool.
|
|
295
|
+
* @returns The result of the command execution. If the command exits with a code
|
|
296
|
+
* of 0, it returns the content of stdout. If the exit code is non-zero,
|
|
297
|
+
* it returns the content of stderr.
|
|
298
|
+
* @throws Error if `toolCallTemplate` is not an instance of
|
|
299
|
+
* `CliCallTemplate` or if commands are not set.
|
|
300
|
+
*/
|
|
301
|
+
callTool(caller: IUtcpClient, toolName: string, toolArgs: Record<string, any>, toolCallTemplate: CallTemplate): Promise<any>;
|
|
302
|
+
/**
|
|
303
|
+
* REQUIRED
|
|
304
|
+
* Streaming calls are not supported for the CLI protocol.
|
|
305
|
+
*
|
|
306
|
+
* @throws Error Always, as this functionality is not supported.
|
|
307
|
+
*/
|
|
308
|
+
callToolStreaming(caller: IUtcpClient, toolName: string, toolArgs: Record<string, any>, toolCallTemplate: CallTemplate): AsyncGenerator<any, void, unknown>;
|
|
309
|
+
close(): Promise<void>;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Registers the CLI protocol's CallTemplate serializer
|
|
314
|
+
* and its CommunicationProtocol implementation.
|
|
315
|
+
* This function should be called once when the CLI plugin is loaded.
|
|
316
|
+
*/
|
|
317
|
+
declare function register(override?: boolean): void;
|
|
318
|
+
|
|
319
|
+
export { type CliCallTemplate, CliCallTemplateSchema, CliCallTemplateSerializer, CliCommunicationProtocol, type CommandStep, CommandStepSchema, register };
|