rpi-extensions 0.21.0

Rust-native (cdylib) plugin loader + AgentTool adapter for rpi — libloading + spawn_blocking ABI bridge
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
//! `tool_result` transform — a plugin's chance to change what a finished tool
//! call hands the model.
//!
//! pi lets a `tool_result` handler return a changed `content`/`details`
//! (`ToolResultEventResult`), and the agent records that instead of what the
//! tool produced. rpi broadcasts [`EventTag::ToolResult`] to observers only;
//! this module carries the return channel, the way
//! [`prompt_transform`](crate::prompt_transform) does for `before_agent_start`.
//!
//! - **Chained in registration order.** Each handler sees the result the
//!   previous one returned.
//! - **Partial replacement.** A handler returns only the fields it changes,
//!   `content`, `details` or both; anything else (`terminate`, usage, added
//!   tools) stays the tool's.
//! - **A failing handler is skipped, not fatal.** Nonzero `rc`, a panic or
//!   unreadable output leave the result as the previous handler left it.
//! - **Only when registered.** Tools are wrapped only when at least one handler
//!   exists, so a session without such a plugin runs the tools untouched.
//!
//! [`EventTag::ToolResult`]: rpi_plugin_sdk::EventTag::ToolResult

use std::panic::{catch_unwind, AssertUnwindSafe};
use std::sync::Arc;

use async_trait::async_trait;
use rpi_agent::types::{AgentToolResult, TextContentOrImage, ToolExecutionMode, ToolResultPartial};
use rpi_agent::{AgentError, AgentTool};
use rpi_ai::types::{ImageContent, ImageContentType, Tool};
use rpi_plugin_sdk::{StbString, StbStringRef};
use serde_json::{json, Value};
use tokio_util::sync::CancellationToken;

use crate::loader::PluginKeepalive;
use crate::registry::{assert_active, RegistrySnapshot, ToolResultHandler};

/// Wrap `tool` so its successful results pass through the snapshot's
/// `tool_result` transforms. Returns `tool` itself when there are none.
pub fn with_tool_result_transform(
    tool: Arc<dyn AgentTool>,
    snapshot: Arc<RegistrySnapshot>,
    keepalive: Arc<PluginKeepalive>,
) -> Arc<dyn AgentTool> {
    if snapshot.tool_result().is_empty() {
        return tool;
    }
    Arc::new(TransformedTool {
        tool,
        snapshot,
        keepalive,
    })
}

/// Run every `tool_result` transform over one result. `None` means no handler
/// changed anything (or the snapshot is stale), so the caller keeps `result`.
pub fn emit_tool_result(
    tool_call_id: &str,
    tool_name: &str,
    input: &Value,
    result: &AgentToolResult,
    snapshot: &RegistrySnapshot,
) -> Option<AgentToolResult> {
    if !assert_active(snapshot.active_flag()) {
        return None;
    }
    let handlers = snapshot.tool_result();
    if handlers.is_empty() {
        return None;
    }
    let mut content = content_to_json(&result.content);
    let mut details = result.details.clone();
    let mut changed = false;
    let mut content_replaced = false;
    for h in handlers {
        let envelope = json!({
            "toolCallId": tool_call_id,
            "toolName": tool_name,
            "input": input,
            "content": content,
            "details": details,
        })
        .to_string();
        let outcome = catch_unwind(AssertUnwindSafe(|| {
            call_one_handler(*h, StbStringRef::from_str(&envelope))
        }));
        match outcome {
            Ok(Ok(Some(patch))) => {
                if let Some(next) = patch.get("content").filter(|v| v.is_array()) {
                    content = next.clone();
                    content_replaced = true;
                    changed = true;
                }
                if let Some(next) = patch.get("details") {
                    details = next.clone();
                    changed = true;
                }
            }
            Ok(Ok(None)) => {}
            Ok(Err(rc)) => tracing::warn!(
                rc,
                tool_name,
                "tool_result handler returned nonzero — result left as-is (fan-out continues)"
            ),
            Err(_) => tracing::error!(
                tool_name,
                "tool_result handler panicked — result left as-is (fan-out continues)"
            ),
        }
    }
    changed.then(|| AgentToolResult {
        content: if content_replaced {
            content_from_json(&content)
        } else {
            result.content.clone()
        },
        details,
        ..result.clone()
    })
}

struct TransformedTool {
    tool: Arc<dyn AgentTool>,
    snapshot: Arc<RegistrySnapshot>,
    keepalive: Arc<PluginKeepalive>,
}

#[async_trait]
impl AgentTool for TransformedTool {
    fn schema(&self) -> &Tool {
        self.tool.schema()
    }
    fn label(&self) -> &str {
        self.tool.label()
    }
    fn execution_mode(&self) -> ToolExecutionMode {
        self.tool.execution_mode()
    }
    fn prepare_arguments(&self, args: Value) -> Result<Value, AgentError> {
        self.tool.prepare_arguments(args)
    }
    async fn execute(
        &self,
        id: &str,
        params: Value,
        signal: CancellationToken,
        on_update: Arc<dyn Fn(ToolResultPartial) + Send + Sync>,
    ) -> Result<AgentToolResult, AgentError> {
        let result = self
            .tool
            .execute(id, params.clone(), signal, on_update)
            .await?;
        let snapshot = Arc::clone(&self.snapshot);
        let keepalive = Arc::clone(&self.keepalive);
        let id = id.to_string();
        let name = self.tool.schema().name.clone();
        // Handlers may be slow (re-encoding an image), so keep them off the
        // async workers. A join error keeps the tool's own result.
        tokio::task::spawn_blocking(move || {
            // Dropping the awaiter cannot stop a started blocking callback.
            // Keep its library loaded through handler execution and the
            // producer-side reclamation of every returned string.
            let _keepalive = keepalive;
            match emit_tool_result(&id, &name, &params, &result, &snapshot) {
                Some(changed) => changed,
                None => result,
            }
        })
        .await
        .map_err(|error| AgentError::Tool(format!("tool_result transform failed: {error}")))
    }
}

/// Call one handler; `Ok(None)` is "no change" (empty or non-object output).
fn call_one_handler(h: ToolResultHandler, event: StbStringRef) -> Result<Option<Value>, i32> {
    let mut out = StbString::empty();
    let rc = (h.handler)(event, &mut out, h.user_data);
    // SAFETY: the callback transfers its live output to its registered producer.
    let out = unsafe {
        rpi_plugin_sdk::callbacks::OwnedString::from_raw(out, Some(h.plugin_free_string))
    };
    if rc != 0 {
        return Err(rc);
    }
    let text = out.to_string_lossy();
    let text = text.trim();
    if text.is_empty() {
        return Ok(None);
    }
    Ok(serde_json::from_str::<Value>(text)
        .ok()
        .filter(Value::is_object))
}

/// Result content in the shape a plugin tool returns it.
fn content_to_json(content: &[TextContentOrImage]) -> Value {
    serde_json::to_value(content).expect("tool result content serializes to JSON")
}

/// The inverse of [`content_to_json`], as lenient as plugin tool results: an
/// unknown block type is read as text, a missing `mimeType` as PNG.
fn content_from_json(content: &Value) -> Vec<TextContentOrImage> {
    let field = |block: &Value, key: &str| {
        block
            .get(key)
            .and_then(Value::as_str)
            .unwrap_or_default()
            .to_string()
    };
    content
        .as_array()
        .map(|blocks| {
            blocks
                .iter()
                .map(|block| match block.get("type").and_then(Value::as_str) {
                    Some("image") => TextContentOrImage::Image(
                        serde_json::from_value(block.clone()).unwrap_or_else(|_| ImageContent {
                            kind: ImageContentType,
                            data: field(block, "data").into(),
                            mime_type: block
                                .get("mimeType")
                                .and_then(Value::as_str)
                                .unwrap_or("image/png")
                                .to_string(),
                        }),
                    ),
                    _ => serde_json::from_value(block.clone())
                        .map(TextContentOrImage::Text)
                        .unwrap_or_else(|_| TextContentOrImage::text(field(block, "text"))),
                })
                .collect()
        })
        .unwrap_or_default()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::registry::ExtensionRegistry;
    use rpi_plugin_sdk::callbacks::free_string;

    /// Swaps every PNG image for a "webp" one and tags `details`.
    extern "C" fn to_webp(
        event: StbStringRef,
        out: *mut StbString,
        _user_data: *mut std::ffi::c_void,
    ) -> i32 {
        // SAFETY: the host keeps the envelope alive for the call.
        let event: Value = serde_json::from_str(unsafe { event.as_str() }).unwrap();
        let mut content = event["content"].clone();
        for block in content.as_array_mut().unwrap() {
            if block["mimeType"] == "image/png" {
                block["mimeType"] = json!("image/webp");
                block["data"] = json!("V0VCUA==");
            }
        }
        let patch = json!({"content": content, "details": {"seen": event["toolName"]}});
        // SAFETY: `out` is the host's slot for this call.
        unsafe { *out = StbString::from_string(patch.to_string()) };
        0
    }

    /// Sees the previous handler's output and appends a text block.
    extern "C" fn append_note(
        event: StbStringRef,
        out: *mut StbString,
        _user_data: *mut std::ffi::c_void,
    ) -> i32 {
        // SAFETY: the host keeps the envelope alive for the call.
        let event: Value = serde_json::from_str(unsafe { event.as_str() }).unwrap();
        let mut content = event["content"].clone();
        let mime = content[1]["mimeType"]
            .as_str()
            .unwrap_or_default()
            .to_string();
        content
            .as_array_mut()
            .unwrap()
            .push(json!({"type": "text", "text": format!("after {mime}")}));
        // SAFETY: as above.
        unsafe { *out = StbString::from_string(json!({"content": content}).to_string()) };
        0
    }

    extern "C" fn fails(_: StbStringRef, _: *mut StbString, _: *mut std::ffi::c_void) -> i32 {
        3
    }

    fn snapshot(handlers: &[rpi_plugin_sdk::ToolResultFn]) -> Arc<RegistrySnapshot> {
        let mut registry = ExtensionRegistry::new();
        for h in handlers {
            registry.register_tool_result(*h, free_string, std::ptr::null_mut());
        }
        Arc::new(registry.snapshot())
    }

    fn result() -> AgentToolResult {
        AgentToolResult {
            content: vec![
                TextContentOrImage::text("Read image file [image/png]"),
                TextContentOrImage::Image(ImageContent {
                    kind: ImageContentType,
                    data: "UE5H".into(),
                    mime_type: "image/png".into(),
                }),
            ],
            details: json!({"path": "a.png"}),
            terminate: true,
            ..Default::default()
        }
    }

    #[test]
    fn handlers_chain_and_replace_only_what_they_return() {
        let snap = snapshot(&[fails, to_webp, append_note]);
        let changed = emit_tool_result("c1", "read", &json!({"path": "a.png"}), &result(), &snap)
            .expect("changed");
        let TextContentOrImage::Image(image) = &changed.content[1] else {
            panic!("image kept its place")
        };
        assert_eq!(
            (image.mime_type.as_str(), &*image.data),
            ("image/webp", "V0VCUA==")
        );
        assert_eq!(
            changed.content[2],
            TextContentOrImage::text("after image/webp")
        );
        assert_eq!(changed.details, json!({"seen": "read"}));
        assert!(
            changed.terminate,
            "fields a handler cannot return stay the tool's"
        );
    }

    #[test]
    fn patches_preserve_content_metadata() {
        extern "C" fn patch_details(
            event: StbStringRef,
            out: *mut StbString,
            _: *mut std::ffi::c_void,
        ) -> i32 {
            // SAFETY: the envelope and output slot are live for the callback.
            let event: Value = serde_json::from_str(unsafe { event.as_str() }).unwrap();
            let mut patch = json!({"details": {"updated": true}});
            if event["input"]["replaceContent"] == true {
                patch["content"] = event["content"].clone();
            }
            unsafe { *out = StbString::from_string(patch.to_string()) };
            0
        }

        let mut original = result();
        let TextContentOrImage::Text(text) = &mut original.content[0] else {
            unreachable!()
        };
        text.text_signature = Some("stable-signature".into());
        let snap = snapshot(&[patch_details]);
        // Both a details-only patch and an explicit identity content replacement
        // must preserve all supported content metadata.
        for replace_content in [false, true] {
            let changed = emit_tool_result(
                "c1",
                "read",
                &json!({"replaceContent": replace_content}),
                &original,
                &snap,
            )
            .expect("details changed");
            assert_eq!(changed.details, json!({"updated": true}));
            assert_eq!(changed.content, original.content);
        }
    }

    #[tokio::test]
    async fn aborted_awaiter_keeps_blocking_handler_library_alive() {
        use std::sync::{mpsc, Mutex};
        use std::time::Duration;

        struct Paused {
            entered: tokio::sync::Notify,
            release: Mutex<mpsc::Receiver<()>>,
        }
        extern "C" fn paused(
            _: StbStringRef,
            out: *mut StbString,
            user_data: *mut std::ffi::c_void,
        ) -> i32 {
            // SAFETY: the test retains the state until the blocking task exits.
            let state = unsafe { &*user_data.cast::<Paused>() };
            state.entered.notify_one();
            if state
                .release
                .lock()
                .unwrap()
                .recv_timeout(Duration::from_secs(10))
                .is_err()
            {
                return 1;
            }
            // Exercise producer-side output reclamation as well as the callback.
            unsafe { *out = StbString::from_string(r#"{"details":{"updated":true}}"#.into()) };
            0
        }

        // No plugin adapter: only the transform can retain the library here.
        struct BuiltinTool(Tool);
        #[async_trait]
        impl AgentTool for BuiltinTool {
            fn schema(&self) -> &Tool {
                &self.0
            }
            fn label(&self) -> &str {
                "read"
            }
            async fn execute(
                &self,
                _: &str,
                _: Value,
                _: CancellationToken,
                _: Arc<dyn Fn(ToolResultPartial) + Send + Sync>,
            ) -> Result<AgentToolResult, AgentError> {
                Ok(result())
            }
        }

        let (release, receiver) = mpsc::channel();
        let state = Arc::new(Paused {
            entered: tokio::sync::Notify::new(),
            release: Mutex::new(receiver),
        });
        let mut registry = ExtensionRegistry::new();
        registry.register_tool_result(
            paused,
            free_string,
            Arc::as_ptr(&state) as *mut std::ffi::c_void,
        );
        let session = crate::ExtensionSession::from_parts(
            Arc::new(registry.snapshot()),
            PluginKeepalive::empty(),
            Vec::new(),
            None,
        );
        let weak = Arc::downgrade(&session.keepalive());
        let tool = with_tool_result_transform(
            Arc::new(BuiltinTool(Tool {
                name: "read".into(),
                description: "test built-in".into(),
                parameters: json!({"type": "object"}).into(),
                constrained_sampling: None,
            })),
            session.snapshot_arc().unwrap(),
            session.keepalive(),
        );
        let task = tokio::spawn(async move {
            tool.execute(
                "c1",
                Value::Null,
                CancellationToken::new(),
                Arc::new(|_| {}),
            )
            .await
        });
        tokio::time::timeout(Duration::from_secs(2), state.entered.notified())
            .await
            .unwrap();
        task.abort();
        assert!(task.await.unwrap_err().is_cancelled());
        registry.invalidate();
        drop(session);
        let retained = weak.upgrade().is_some();
        release.send(()).unwrap();
        tokio::time::timeout(Duration::from_secs(2), async {
            while weak.upgrade().is_some() {
                tokio::time::sleep(Duration::from_millis(1)).await;
            }
        })
        .await
        .unwrap();
        assert!(
            retained,
            "aborting the awaiter must not unload its running handler"
        );
    }

    #[test]
    fn no_handler_or_no_change_leaves_the_tool_alone() {
        assert!(emit_tool_result("c1", "read", &Value::Null, &result(), &snapshot(&[])).is_none());
        assert!(
            emit_tool_result("c1", "read", &Value::Null, &result(), &snapshot(&[fails])).is_none()
        );
    }
}