agent-sdk-toolkit 0.1.0-alpha.3

Optional tool-pack helpers layered over agent-sdk-core primitive contracts.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//! Scripted ACP protocol harnesses for SDK consumers. Use these fakes in tests to
//! prove lifecycle and message exchange without a live editor or product host.
//! Harness methods mutate in-memory endpoint transcripts only.
//!
use std::collections::{BTreeMap, BTreeSet};

use agent_sdk_core::AgentError;
use serde_json::{Value, json};

use crate::protocol::{
    JsonRpcFrame, JsonRpcId, JsonRpcLineEndpoint, JsonRpcNotification, JsonRpcRequest,
    JsonRpcResponse, expect_notification, expect_response, protocol_violation,
};

enum ReceiveNext {
    Empty,
    Handled,
    Frame(JsonRpcFrame),
}

#[derive(Clone, Debug)]
/// In-memory scripted acp client fixture for SDK conformance tests.
/// Use it to script deterministic behavior in memory; any transcript or endpoint mutation is documented on the method that performs it.
pub struct ScriptedAcpClient {
    endpoint: JsonRpcLineEndpoint,
    next_id: i64,
}

impl ScriptedAcpClient {
    /// Creates a new testing::protocol::acp value with explicit
    /// caller-provided inputs. This constructor is data-only and
    /// performs no I/O or external side effects.
    pub fn new(endpoint: JsonRpcLineEndpoint) -> Self {
        Self {
            endpoint,
            next_id: 1,
        }
    }

    /// Returns the endpoint currently held by this value.
    /// This reads scripted protocol state or a queued response without contacting an external
    /// process.
    pub fn endpoint(&self) -> &JsonRpcLineEndpoint {
        &self.endpoint
    }

    /// Initialize.
    /// This appends the corresponding JSON-RPC frame to the scripted ACP mock transcript and
    /// returns the request id when applicable.
    pub fn initialize(&mut self) -> Result<JsonRpcId, AgentError> {
        self.request(
            "initialize",
            json!({
                "protocolVersion": 1,
                "clientInfo": {
                    "name": "agent-sdk-toolkit-acp-fake",
                    "version": "0.0.0"
                },
                "clientCapabilities": {
                    "fs": {"readTextFile": false, "writeTextFile": false},
                    "terminal": false
                }
            }),
        )
    }

    /// Queues an ACP `session/new` request in the scripted client transcript.
    /// This mutates only the in-memory JSON-RPC endpoint and returns the request id; no live ACP
    /// server, journal, event bus, or process is contacted.
    pub fn new_session(&mut self, cwd: impl Into<String>) -> Result<JsonRpcId, AgentError> {
        self.request(
            "session/new",
            json!({
                "cwd": cwd.into(),
                "mcpServers": []
            }),
        )
    }

    /// Prompt.
    /// This appends the corresponding JSON-RPC frame to the in-memory test endpoint transcript.
    pub fn prompt(
        &mut self,
        session_id: impl Into<String>,
        prompt: impl Into<String>,
    ) -> Result<JsonRpcId, AgentError> {
        self.request(
            "session/prompt",
            json!({
                "sessionId": session_id.into(),
                "prompt": [{"type": "text", "text": prompt.into()}]
            }),
        )
    }

    /// Cancel.
    /// This appends the corresponding JSON-RPC frame to the scripted ACP mock transcript and
    /// returns the request id when applicable.
    pub fn cancel(&self, session_id: impl Into<String>) -> Result<(), AgentError> {
        self.endpoint
            .send_notification("session/cancel", json!({"sessionId": session_id.into()}))
            .map(|_| ())
    }

    /// Handle next.
    /// This consumes one queued JSON-RPC frame from the in-memory endpoint and mutates only
    /// scripted mock state.
    pub fn handle_next(&mut self, endpoint: &JsonRpcLineEndpoint) -> Result<bool, AgentError> {
        let frame = match receive_frame_or_parse_error(endpoint)? {
            ReceiveNext::Empty => return Ok(false),
            ReceiveNext::Handled => return Ok(true),
            ReceiveNext::Frame(frame) => frame,
        };
        let JsonRpcFrame::Request(request) = frame else {
            return Ok(true);
        };
        match request.method.as_str() {
            "fs/read_text_file" => {
                endpoint.send_error(
                    Some(request.id),
                    -32003,
                    "ACP fs/read_text_file denied by client capability policy",
                )?;
            }
            "terminal/create" => {
                endpoint.send_error(
                    Some(request.id),
                    -32004,
                    "ACP terminal/create denied by client capability policy",
                )?;
            }
            "session/request_permission" => {
                endpoint.send_result(request.id, json!({"outcome": {"outcome": "cancelled"}}))?;
            }
            _ => {
                endpoint.send_error(Some(request.id), -32601, "ACP client method not found")?;
            }
        }
        Ok(true)
    }

    /// Returns the response currently held by this value.
    /// This reads scripted protocol state or a queued response without contacting an external
    /// process.
    pub fn response(&self) -> Result<JsonRpcResponse, AgentError> {
        expect_response(self.endpoint.receive_frame()?)
    }

    /// Returns notification for this testing::protocol::acp value without
    /// performing external I/O.
    pub fn notification(&self) -> Result<JsonRpcNotification, AgentError> {
        expect_notification(self.endpoint.receive_frame()?)
    }

    fn request(&mut self, method: &str, params: Value) -> Result<JsonRpcId, AgentError> {
        let id = JsonRpcId::Number(self.next_id);
        self.next_id += 1;
        self.endpoint
            .send_request(id.clone(), method, params)
            .map(|_| id)
    }
}

#[derive(Clone, Debug)]
/// In-memory scripted acp agent fixture for SDK conformance tests.
/// Use it to script deterministic behavior in memory; any transcript or endpoint mutation is documented on the method that performs it.
pub struct ScriptedAcpAgent {
    session_id: String,
    prompt_outputs: BTreeMap<String, String>,
    cancelled_sessions: BTreeSet<String>,
    next_client_request: i64,
}

impl ScriptedAcpAgent {
    /// Creates a new testing::protocol::acp value with explicit
    /// caller-provided inputs. This constructor is data-only and
    /// performs no I/O or external side effects.
    pub fn new(session_id: impl Into<String>) -> Self {
        Self {
            session_id: session_id.into(),
            prompt_outputs: BTreeMap::new(),
            cancelled_sessions: BTreeSet::new(),
            next_client_request: 1,
        }
    }

    /// Returns this value with its prompt output setting replaced. The
    /// method follows builder-style data construction and does not
    /// execute external work.
    pub fn with_prompt_output(
        mut self,
        prompt: impl Into<String>,
        output: impl Into<String>,
    ) -> Self {
        self.prompt_outputs.insert(prompt.into(), output.into());
        self
    }

    /// Handle next.
    /// This consumes one queued JSON-RPC frame from the in-memory endpoint and mutates only
    /// scripted mock state.
    pub fn handle_next(&mut self, endpoint: &JsonRpcLineEndpoint) -> Result<bool, AgentError> {
        let frame = match receive_frame_or_parse_error(endpoint)? {
            ReceiveNext::Empty => return Ok(false),
            ReceiveNext::Handled => return Ok(true),
            ReceiveNext::Frame(frame) => frame,
        };
        let request = match frame {
            JsonRpcFrame::Request(request) => request,
            JsonRpcFrame::Notification(notification) => {
                if notification.method == "session/cancel" {
                    if let Some(session_id) =
                        notification.params.get("sessionId").and_then(Value::as_str)
                    {
                        self.cancelled_sessions.insert(session_id.to_string());
                    }
                    return Ok(true);
                }
                endpoint.send_error(
                    None,
                    -32600,
                    "ACP agent expected a request or known notification",
                )?;
                return Ok(true);
            }
            JsonRpcFrame::Response(_) => {
                endpoint.send_error(None, -32600, "ACP agent expected a request frame")?;
                return Ok(true);
            }
        };
        match request.method.as_str() {
            "initialize" => {
                endpoint.send_result(
                    request.id,
                    json!({
                        "protocolVersion": request.params
                            .get("protocolVersion")
                            .cloned()
                            .unwrap_or_else(|| json!(1)),
                        "agentCapabilities": {
                            "auth": {},
                            "loadSession": false,
                            "mcpCapabilities": {"http": false, "sse": false},
                            "promptCapabilities": {
                                "audio": false,
                                "embeddedContext": false,
                                "image": false
                            },
                            "sessionCapabilities": {}
                        },
                        "agentInfo": {
                            "name": "agent-sdk-toolkit-acp-fake",
                            "version": "0.0.0"
                        },
                        "authMethods": []
                    }),
                )?;
            }
            "session/new" => {
                endpoint.send_result(
                    request.id,
                    json!({
                        "sessionId": self.session_id,
                        "modes": null,
                        "configOptions": null
                    }),
                )?;
            }
            "session/prompt" => self.handle_prompt(endpoint, request)?,
            _ => {
                endpoint.send_error(Some(request.id), -32601, "ACP method not found")?;
            }
        };
        Ok(true)
    }

    /// Request file read.
    /// This appends the corresponding JSON-RPC frame to the in-memory test endpoint transcript.
    pub fn request_file_read(
        &mut self,
        endpoint: &JsonRpcLineEndpoint,
        session_id: impl Into<String>,
        path: impl Into<String>,
    ) -> Result<JsonRpcId, AgentError> {
        let id = self.next_client_request_id();
        endpoint
            .send_request(
                id.clone(),
                "fs/read_text_file",
                json!({"sessionId": session_id.into(), "path": path.into()}),
            )
            .map(|_| id)
    }

    /// Request terminal create.
    /// This appends the corresponding JSON-RPC frame to the in-memory test endpoint transcript.
    pub fn request_terminal_create(
        &mut self,
        endpoint: &JsonRpcLineEndpoint,
        session_id: impl Into<String>,
        command: impl Into<String>,
    ) -> Result<JsonRpcId, AgentError> {
        let id = self.next_client_request_id();
        endpoint
            .send_request(
                id.clone(),
                "terminal/create",
                json!({
                    "sessionId": session_id.into(),
                    "command": command.into(),
                    "args": [],
                    "cwd": null
                }),
            )
            .map(|_| id)
    }

    /// Request permission.
    /// This appends the corresponding JSON-RPC frame to the in-memory test endpoint transcript.
    pub fn request_permission(
        &mut self,
        endpoint: &JsonRpcLineEndpoint,
        session_id: impl Into<String>,
        tool_call_id: impl Into<String>,
    ) -> Result<JsonRpcId, AgentError> {
        let id = self.next_client_request_id();
        endpoint
            .send_request(
                id.clone(),
                "session/request_permission",
                json!({
                    "sessionId": session_id.into(),
                    "toolCall": {"toolCallId": tool_call_id.into()},
                    "options": [
                        {"optionId": "allow-once", "name": "Allow once", "kind": "allow_once"},
                        {"optionId": "reject-once", "name": "Reject", "kind": "reject_once"}
                    ]
                }),
            )
            .map(|_| id)
    }

    /// Returns the response currently held by this value.
    /// This reads scripted protocol state or a queued response without contacting an external
    /// process.
    pub fn response(&self, endpoint: &JsonRpcLineEndpoint) -> Result<JsonRpcResponse, AgentError> {
        expect_response(endpoint.receive_frame()?)
    }

    /// Cancelled sessions.
    /// This reads the scripted ACP mock cancellation set without sending a frame.
    pub fn cancelled_sessions(&self) -> BTreeSet<String> {
        self.cancelled_sessions.clone()
    }

    fn next_client_request_id(&mut self) -> JsonRpcId {
        let id = JsonRpcId::Number(self.next_client_request);
        self.next_client_request += 1;
        id
    }

    fn handle_prompt(
        &mut self,
        endpoint: &JsonRpcLineEndpoint,
        request: JsonRpcRequest,
    ) -> Result<(), AgentError> {
        let prompt = request
            .params
            .get("prompt")
            .and_then(Value::as_array)
            .and_then(|blocks| blocks.first())
            .and_then(|block| block.get("text"))
            .and_then(Value::as_str)
            .ok_or_else(|| protocol_violation("ACP prompt request requires prompt string"))?;
        let session_id = request
            .params
            .get("sessionId")
            .and_then(Value::as_str)
            .ok_or_else(|| protocol_violation("ACP prompt request requires sessionId"))?;
        if session_id != self.session_id {
            endpoint.send_error(Some(request.id), -32602, "ACP session is not active")?;
            return Ok(());
        }
        let output = self
            .prompt_outputs
            .get(prompt)
            .cloned()
            .unwrap_or_else(|| "scripted ACP response".to_string());
        endpoint.send_notification(
            "session/update",
            json!({
                "sessionId": session_id,
                "update": {
                    "sessionUpdate": "agent_message_chunk",
                    "content": {
                        "type": "text",
                        "text": output
                    }
                }
            }),
        )?;
        endpoint.send_result(request.id, json!({"stopReason": "end_turn"}))?;
        Ok(())
    }
}

fn receive_frame_or_parse_error(endpoint: &JsonRpcLineEndpoint) -> Result<ReceiveNext, AgentError> {
    let Some(line) = endpoint.try_receive_raw_line()? else {
        return Ok(ReceiveNext::Empty);
    };
    match JsonRpcFrame::from_line(&line) {
        Ok(frame) => Ok(ReceiveNext::Frame(frame)),
        Err(error) => {
            endpoint.send_error(None, -32700, error.context().message)?;
            Ok(ReceiveNext::Handled)
        }
    }
}