|
| 1 | +import type OpenAI from "openai" |
| 2 | + |
| 3 | +/** |
| 4 | + * Native tool definition for write_stdin. |
| 5 | + * |
| 6 | + * This tool allows the LLM to write characters to an existing terminal session |
| 7 | + * and receive the resulting output. It enables interactive terminal workflows |
| 8 | + * where the LLM can respond to prompts, provide input to running processes, |
| 9 | + * and monitor long-running commands. |
| 10 | + */ |
| 11 | + |
| 12 | +const WRITE_STDIN_DESCRIPTION = `Writes characters to an existing exec session and returns recent output. |
| 13 | +
|
| 14 | +Use this tool when: |
| 15 | +1. A command started with execute_command is still running and waiting for input |
| 16 | +2. You need to respond to an interactive prompt (e.g., "Press y to continue", password prompts) |
| 17 | +3. You want to poll a long-running process for new output without sending input |
| 18 | +
|
| 19 | +The session_id is returned by execute_command when a process is still running. |
| 20 | +
|
| 21 | +Parameters: |
| 22 | +- session_id: (required) Identifier of the running exec session (returned by execute_command) |
| 23 | +- chars: (optional) Characters to write to stdin. Use empty string or omit to just poll for output. |
| 24 | +- yield_time_ms: (optional) Milliseconds to wait for output after writing (default: 250, min: 250, max: 30000) |
| 25 | +- max_output_tokens: (optional) Maximum tokens to return in the response |
| 26 | +
|
| 27 | +Common use cases: |
| 28 | +- Sending 'y' or 'n' to confirmation prompts |
| 29 | +- Providing input to interactive CLI tools |
| 30 | +- Sending Ctrl+C (\\x03) to terminate a process |
| 31 | +- Polling for output from a long-running process |
| 32 | +
|
| 33 | +Example: Responding to a confirmation prompt |
| 34 | +{ "session_id": 1234, "chars": "y\\n" } |
| 35 | +
|
| 36 | +Example: Sending Ctrl+C to stop a process |
| 37 | +{ "session_id": 1234, "chars": "\\x03" } |
| 38 | +
|
| 39 | +Example: Polling for new output (no input) |
| 40 | +{ "session_id": 1234, "chars": "", "yield_time_ms": 2000 } |
| 41 | +
|
| 42 | +Example: Providing password (note: prefer non-interactive approaches when possible) |
| 43 | +{ "session_id": 1234, "chars": "password\\n" }` |
| 44 | + |
| 45 | +const SESSION_ID_DESCRIPTION = `Identifier of the running exec session (returned by execute_command when a process is still running)` |
| 46 | + |
| 47 | +const CHARS_DESCRIPTION = `Characters to write to stdin. May be empty to just poll for output. Supports escape sequences like \\n (newline) and \\x03 (Ctrl+C).` |
| 48 | + |
| 49 | +const YIELD_TIME_MS_DESCRIPTION = `Milliseconds to wait for output after writing (default: 250, range: 250-30000). Use higher values when expecting delayed output.` |
| 50 | + |
| 51 | +const MAX_OUTPUT_TOKENS_DESCRIPTION = `Maximum tokens to return in the response. Excess output will be truncated with head/tail preservation.` |
| 52 | + |
| 53 | +export default { |
| 54 | + type: "function", |
| 55 | + function: { |
| 56 | + name: "write_stdin", |
| 57 | + description: WRITE_STDIN_DESCRIPTION, |
| 58 | + // Note: strict mode is intentionally disabled for this tool. |
| 59 | + // With strict: true, OpenAI requires ALL properties to be in the 'required' array, |
| 60 | + // which forces the LLM to always provide explicit values (even null) for optional params. |
| 61 | + // This creates verbose tool calls and poor UX. By disabling strict mode, the LLM can |
| 62 | + // omit optional parameters entirely, making the tool easier to use. |
| 63 | + parameters: { |
| 64 | + type: "object", |
| 65 | + properties: { |
| 66 | + session_id: { |
| 67 | + type: "number", |
| 68 | + description: SESSION_ID_DESCRIPTION, |
| 69 | + }, |
| 70 | + chars: { |
| 71 | + type: "string", |
| 72 | + description: CHARS_DESCRIPTION, |
| 73 | + }, |
| 74 | + yield_time_ms: { |
| 75 | + type: "number", |
| 76 | + description: YIELD_TIME_MS_DESCRIPTION, |
| 77 | + }, |
| 78 | + max_output_tokens: { |
| 79 | + type: "number", |
| 80 | + description: MAX_OUTPUT_TOKENS_DESCRIPTION, |
| 81 | + }, |
| 82 | + }, |
| 83 | + required: ["session_id"], |
| 84 | + additionalProperties: false, |
| 85 | + }, |
| 86 | + }, |
| 87 | +} satisfies OpenAI.Chat.ChatCompletionTool |
0 commit comments