|
| 1 | +import { Agent, tool, InterruptResponseContent, SessionManager, FileStorage } from '@strands-agents/sdk' |
| 2 | +import { HumanInTheLoop } from '@strands-agents/sdk/vended-interventions/hitl' |
| 3 | +import { z } from 'zod' |
| 4 | + |
| 5 | +// ===================== |
| 6 | +// Basic (Interrupt/Resume Mode) |
| 7 | +// ===================== |
| 8 | + |
| 9 | +async function basicInterruptExample() { |
| 10 | + // --8<-- [start:basic_interrupt] |
| 11 | + const deleteFiles = tool({ |
| 12 | + name: 'delete_files', |
| 13 | + description: 'Delete files at the given paths', |
| 14 | + inputSchema: z.object({ paths: z.array(z.string()) }), |
| 15 | + callback: (input) => `Deleted ${input.paths.length} files`, |
| 16 | + }) |
| 17 | + |
| 18 | + const agent = new Agent({ |
| 19 | + tools: [deleteFiles], |
| 20 | + interventions: [new HumanInTheLoop()], |
| 21 | + }) |
| 22 | + |
| 23 | + // Agent pauses with stopReason 'interrupt' when a tool requires approval |
| 24 | + let result = await agent.invoke('Delete the temp files') |
| 25 | + |
| 26 | + if (result.stopReason === 'interrupt') { |
| 27 | + // Present the interrupt to your user (web UI, Slack, push notification, etc.) |
| 28 | + console.log(result.interrupts![0].reason) |
| 29 | + |
| 30 | + // Resume with the human's response |
| 31 | + result = await agent.invoke([ |
| 32 | + new InterruptResponseContent({ |
| 33 | + interruptId: result.interrupts![0].id, |
| 34 | + response: 'yes', // 'y', 'yes', or true → approved. Anything else → denied. |
| 35 | + }), |
| 36 | + ]) |
| 37 | + } |
| 38 | + |
| 39 | + console.log('Result:', result.lastMessage) |
| 40 | + // --8<-- [end:basic_interrupt] |
| 41 | +} |
| 42 | + |
| 43 | +// ===================== |
| 44 | +// Stdio Mode |
| 45 | +// ===================== |
| 46 | + |
| 47 | +async function stdioModeExample() { |
| 48 | + // --8<-- [start:stdio_mode] |
| 49 | + const agent = new Agent({ |
| 50 | + tools: [deleteFiles], |
| 51 | + interventions: [new HumanInTheLoop({ ask: 'stdio' })], |
| 52 | + }) |
| 53 | + |
| 54 | + await agent.invoke('Delete the temp files') |
| 55 | + // Terminal: Tool "delete_files" requires human approval. Input: {"paths":[...]} (y/n): |
| 56 | + // --8<-- [end:stdio_mode] |
| 57 | +} |
| 58 | + |
| 59 | +// ===================== |
| 60 | +// Custom Ask Callback |
| 61 | +// ===================== |
| 62 | + |
| 63 | +async function customAskExample() { |
| 64 | + // --8<-- [start:custom_ask] |
| 65 | + const agent = new Agent({ |
| 66 | + tools: [deleteFiles], |
| 67 | + interventions: [ |
| 68 | + new HumanInTheLoop({ |
| 69 | + ask: async (prompt) => { |
| 70 | + // Your UI: Slack DM, web modal, push notification, etc. |
| 71 | + return await askUserViaSlack(prompt) |
| 72 | + }, |
| 73 | + }), |
| 74 | + ], |
| 75 | + }) |
| 76 | + |
| 77 | + await agent.invoke('Delete the temp files') |
| 78 | + // --8<-- [end:custom_ask] |
| 79 | +} |
| 80 | + |
| 81 | +// ===================== |
| 82 | +// Allowed Tools |
| 83 | +// ===================== |
| 84 | + |
| 85 | +async function allowedToolsExample() { |
| 86 | + // --8<-- [start:allowed_tools] |
| 87 | + const agent = new Agent({ |
| 88 | + tools: [readFile, deleteFiles], |
| 89 | + interventions: [ |
| 90 | + new HumanInTheLoop({ |
| 91 | + ask: 'stdio', |
| 92 | + // Pattern syntax: |
| 93 | + // 'read_file' → this tool runs without approval |
| 94 | + // '*' → all tools run without approval (disables handler) |
| 95 | + // ['*', '!delete_files'] → all tools run freely except delete_files |
| 96 | + allowedTools: ['read_file'], |
| 97 | + }), |
| 98 | + ], |
| 99 | + }) |
| 100 | + |
| 101 | + await agent.invoke('Read config.json then delete /tmp/old-logs') |
| 102 | + // Only delete_files prompts for approval; read_file executes immediately |
| 103 | + // --8<-- [end:allowed_tools] |
| 104 | +} |
| 105 | + |
| 106 | +// ===================== |
| 107 | +// Trust Mode |
| 108 | +// ===================== |
| 109 | + |
| 110 | +async function trustModeExample() { |
| 111 | + // --8<-- [start:trust_mode] |
| 112 | + const agent = new Agent({ |
| 113 | + tools: [deleteFiles], |
| 114 | + interventions: [ |
| 115 | + new HumanInTheLoop({ |
| 116 | + ask: 'stdio', |
| 117 | + enableTrust: true, |
| 118 | + }), |
| 119 | + ], |
| 120 | + }) |
| 121 | + |
| 122 | + await agent.invoke('Delete all log files in /tmp') |
| 123 | + // First delete_files call: user responds 't' → approved AND remembered |
| 124 | + // Subsequent delete_files calls: no prompt needed for the rest of the session |
| 125 | + // --8<-- [end:trust_mode] |
| 126 | +} |
| 127 | + |
| 128 | +// ===================== |
| 129 | +// Cloud / API Server (Interrupt/Resume with Session) |
| 130 | +// ===================== |
| 131 | + |
| 132 | +async function cloudApiExample() { |
| 133 | + // --8<-- [start:cloud_api] |
| 134 | + const deleteFiles = tool({ |
| 135 | + name: 'delete_files', |
| 136 | + description: 'Delete files at the given paths', |
| 137 | + inputSchema: z.object({ paths: z.array(z.string()) }), |
| 138 | + callback: (input) => `Deleted ${input.paths.length} files`, |
| 139 | + }) |
| 140 | + |
| 141 | + const readFile = tool({ |
| 142 | + name: 'read_file', |
| 143 | + description: 'Read a file', |
| 144 | + inputSchema: z.object({ path: z.string() }), |
| 145 | + callback: (input) => `Contents of ${input.path}`, |
| 146 | + }) |
| 147 | + |
| 148 | + function createAgent() { |
| 149 | + return new Agent({ |
| 150 | + tools: [deleteFiles, readFile], |
| 151 | + interventions: [new HumanInTheLoop({ allowedTools: ['read_file'] })], |
| 152 | + sessionManager: new SessionManager({ |
| 153 | + sessionId: 'my-session', |
| 154 | + storage: { snapshot: new FileStorage('/path/to/storage') }, |
| 155 | + }), |
| 156 | + }) |
| 157 | + } |
| 158 | + |
| 159 | + // Request 1: Agent tries to delete → pauses with interrupt |
| 160 | + const agent = createAgent() |
| 161 | + const result = await agent.invoke('Delete the temp files') |
| 162 | + |
| 163 | + if (result.stopReason === 'interrupt') { |
| 164 | + // Send interrupt details to client for approval |
| 165 | + const pending = { |
| 166 | + interruptId: result.interrupts![0].id, |
| 167 | + reason: result.interrupts![0].reason, |
| 168 | + } |
| 169 | + // ... return pending to client via HTTP response |
| 170 | + } |
| 171 | + |
| 172 | + // Request 2: Client sends back approval |
| 173 | + const resumedAgent = createAgent() |
| 174 | + const finalResult = await resumedAgent.invoke([ |
| 175 | + new InterruptResponseContent({ |
| 176 | + interruptId: 'the-interrupt-id', |
| 177 | + response: 'yes', |
| 178 | + }), |
| 179 | + ]) |
| 180 | + // --8<-- [end:cloud_api] |
| 181 | +} |
| 182 | + |
| 183 | +// ===================== |
| 184 | +// Custom Evaluate (OTP example) |
| 185 | +// ===================== |
| 186 | + |
| 187 | +async function customEvaluateExample() { |
| 188 | + // --8<-- [start:custom_evaluate] |
| 189 | + const expectedOtp = '483291' |
| 190 | + |
| 191 | + const agent = new Agent({ |
| 192 | + tools: [transferFunds], |
| 193 | + interventions: [ |
| 194 | + new HumanInTheLoop({ |
| 195 | + ask: async (prompt) => { |
| 196 | + await sendOtpToUser(expectedOtp) |
| 197 | + return await collectUserInput(prompt + ' Enter OTP to confirm:') |
| 198 | + }, |
| 199 | + // Only approve if the user enters the correct OTP |
| 200 | + evaluate: (response) => response === expectedOtp, |
| 201 | + }), |
| 202 | + ], |
| 203 | + }) |
| 204 | + |
| 205 | + await agent.invoke('Transfer $500 from checking to savings') |
| 206 | + // --8<-- [end:custom_evaluate] |
| 207 | +} |
| 208 | + |
| 209 | +// Suppress unused function warnings |
| 210 | +void basicInterruptExample |
| 211 | +void stdioModeExample |
| 212 | +void customAskExample |
| 213 | +void allowedToolsExample |
| 214 | +void trustModeExample |
| 215 | +void cloudApiExample |
| 216 | +void customEvaluateExample |
| 217 | + |
| 218 | +declare function askUserViaSlack(prompt: string): Promise<string> |
| 219 | +declare function sendOtpToUser(otp: string): Promise<void> |
| 220 | +declare function collectUserInput(prompt: string): Promise<string> |
0 commit comments