rpi_cli/host/
binding_resources.rs1use super::{
3 ordinary::{BackendError, BackendResult},
4 AgentSpec, NativeExtensionCustody, NativeInstructionResources,
5};
6use crate::resource_selection::ResourceSelection;
7use rpi_harness::{
8 session::{Session, SessionValues},
9 types::{AgentHarnessOptions, AgentHarnessResources, HarnessTool},
10};
11use serde::Deserialize;
12#[cfg(unix)]
13use std::path::PathBuf;
14use std::sync::Arc;
15
16#[cfg(unix)]
17#[path = "native_hooks.rs"]
18mod native_hooks;
19
20#[derive(Default)]
23pub(super) struct NativeHookCallbacks {
24 pub after_tool_call: Option<rpi_agent::AfterToolCall>,
25 pub provider_hooks: Option<Arc<dyn rpi_ai::ProviderHooks>>,
26}
27
28#[derive(Deserialize)]
31#[serde(deny_unknown_fields, rename_all = "camelCase")]
32pub struct NativeHookConfig {
33 #[cfg(unix)]
36 staging_root: PathBuf,
37 #[cfg(unix)]
38 registrations: Vec<HookRegistration>,
39}
40#[cfg(unix)]
41#[derive(Deserialize)]
42#[serde(deny_unknown_fields, rename_all = "camelCase")]
43struct HookRegistration {
44 #[serde(default)]
45 tool_result: usize,
46 #[serde(default)]
47 provider_events: Vec<ProviderEventApproval>,
48}
49
50#[cfg(unix)]
51#[derive(Deserialize)]
52#[serde(deny_unknown_fields, rename_all = "camelCase")]
53struct ProviderEventApproval {
54 event: ProviderEvent,
55 count: usize,
56}
57
58#[cfg(unix)]
60#[derive(Deserialize)]
61#[serde(rename_all = "camelCase")]
62enum ProviderEvent {
63 BeforeRequest,
64 BeforeHeaders,
65 AfterResponse,
66}
67#[cfg(unix)]
68impl ProviderEvent {
69 fn tag(&self) -> rpi_plugin_sdk::EventTag {
70 use rpi_plugin_sdk::EventTag;
71 match self {
72 Self::BeforeRequest => EventTag::BeforeProviderRequest,
73 Self::BeforeHeaders => EventTag::BeforeProviderHeaders,
74 Self::AfterResponse => EventTag::AfterProviderResponse,
75 }
76 }
77}
78
79pub enum NativeBindingResources {
82 Instructions(Arc<NativeInstructionResources>),
83 #[cfg(unix)]
84 Hooks(Box<native_hooks::NativeHookResources>),
85}
86impl NativeBindingResources {
87 pub fn freeze(
88 selection: ResourceSelection,
89 hooks: Option<NativeHookConfig>,
90 ) -> BackendResult<Self> {
91 match hooks {
92 None => Ok(Self::Instructions(Arc::new(
93 NativeInstructionResources::freeze(selection)?,
94 ))),
95 Some(config) => {
96 #[cfg(unix)]
97 {
98 Ok(Self::Hooks(Box::new(
99 native_hooks::NativeHookResources::freeze(selection, config)?,
100 )))
101 }
102 #[cfg(not(unix))]
103 {
104 let _ = (selection, config);
105 Err(BackendError::Binding("native Host hooks require Unix"))
106 }
107 }
108 }
109 }
110 pub fn identity(&self) -> &str {
111 match self {
112 Self::Instructions(resources) => resources.identity(),
113 #[cfg(unix)]
114 Self::Hooks(resources) => resources.identity(),
115 }
116 }
117 pub(super) fn has_hooks(&self) -> bool {
118 match self {
119 Self::Instructions(_) => false,
120 #[cfg(unix)]
121 Self::Hooks(_) => true,
122 }
123 }
124 pub(super) fn validate_tools(&self, tools: &[String]) -> BackendResult<()> {
125 match self {
126 Self::Instructions(resources) => resources.validate_tools(tools),
127 #[cfg(unix)]
128 Self::Hooks(resources) => resources.validate_tools(tools),
129 }
130 }
131 pub(super) fn configure_parts(
134 &self,
135 prompt: &mut Option<String>,
136 provenance: &mut AgentHarnessResources,
137 ) {
138 match self {
139 Self::Instructions(resources) => resources.configure_parts(prompt, provenance),
140 #[cfg(unix)]
141 Self::Hooks(resources) => resources.configure_parts(prompt, provenance),
142 }
143 }
144 pub(super) fn validate(
145 &self,
146 values: &SessionValues,
147 tools: &[HarnessTool],
148 ) -> BackendResult<()> {
149 match self {
150 Self::Instructions(resources) => resources.validate(values, tools),
151 #[cfg(unix)]
152 Self::Hooks(resources) => resources.validate(values, tools),
153 }
154 }
155 pub(super) async fn persist(
156 &self,
157 session: &Session,
158 tools: &[HarnessTool],
159 ) -> BackendResult<()> {
160 match self {
161 Self::Instructions(resources) => resources.persist(session, tools).await,
162 #[cfg(unix)]
163 Self::Hooks(resources) => resources.persist(session, tools).await,
164 }
165 }
166 pub(super) async fn assemble(
167 &self,
168 spec: &AgentSpec,
169 owner: Arc<NativeExtensionCustody>,
170 mut options: AgentHarnessOptions,
171 ) -> BackendResult<AgentHarnessOptions> {
172 let callbacks = self.assemble_callbacks(spec, owner).await?;
173 if let Some(after_tool_call) = callbacks.after_tool_call {
174 options.after_tool_call = Some(after_tool_call);
175 }
176 if let Some(provider_hooks) = callbacks.provider_hooks {
177 options.provider_hooks = Some(provider_hooks);
178 }
179 Ok(options)
180 }
181 pub(super) async fn assemble_callbacks(
184 &self,
185 spec: &AgentSpec,
186 owner: Arc<NativeExtensionCustody>,
187 ) -> BackendResult<NativeHookCallbacks> {
188 match self {
189 Self::Instructions(_) => {
190 let _ = (spec, owner);
191 Ok(NativeHookCallbacks::default())
192 }
193 #[cfg(unix)]
194 Self::Hooks(resources) => resources.assemble_callbacks(spec, owner).await,
195 }
196 }
197}