determinishtic 0.3.0

Blend deterministic Rust code with LLM-powered reasoning
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
//! ThinkBuilder for composing prompts with tools.

use std::fmt::{Debug, Display};
use std::marker::PhantomData;
use std::sync::Arc;

use agent_client_protocol::mcp_server::{McpConnectionTo, McpServer, McpServerBuilder};
use agent_client_protocol::role::{HasPeer, Role};
use agent_client_protocol::schema::{
    PermissionOptionKind, RequestPermissionOutcome, RequestPermissionRequest,
    RequestPermissionResponse, SelectedPermissionOutcome, SessionNotification, StopReason,
};
use agent_client_protocol::util::MatchDispatch;
use agent_client_protocol::{Agent, BoxFuture, ConnectionTo, NullRun, RunWithConnectionTo};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize, de::DeserializeOwned};
use tracing::{debug, info, trace, warn};

use crate::Error;

/// Observer for session updates during a think block.
///
/// Implement this to monitor what the LLM agent is doing in real time.
/// Default implementations are no-ops, so you only need to override
/// the callbacks you care about.
pub trait ThinkObserver: Send + Sync {
    /// Called with the assembled prompt text before it is sent to the agent.
    fn on_prompt(&self, _prompt: &str) {}

    /// Called for each session notification (agent text, tool calls, etc.).
    fn on_notification(&self, _notification: &SessionNotification) {}

    /// Called when the agent requests permission to use a tool.
    fn on_permission_request(&self, _request: &RequestPermissionRequest) {}

    /// Called when the session stops.
    fn on_stop(&self, _reason: &StopReason) {}
}

/// Builder for composing LLM prompts with embedded tools.
///
/// Created via [`Determinishtic::think`](crate::Determinishtic::think).
///
/// The type parameter `R` is the role whose connection we use. It defaults to `Agent`
/// for backwards compatibility, but can be any role that has `Agent` as a peer
/// (e.g., `Conductor` when used inside a proxy).
///
/// The type parameter `Run` tracks the responder chain for registered tools,
/// allowing tools to capture references from the stack frame.
pub struct ThinkBuilder<'bound, Output, R: Role = Agent, Run: RunWithConnectionTo<R> = NullRun>
where
    R: HasPeer<Agent>,
{
    cx: ConnectionTo<R>,
    segments: Vec<Segment>,
    server: McpServerBuilder<R, Run>,
    explicit_spacing: bool,
    observer: Option<Arc<dyn ThinkObserver>>,
    phantom: PhantomData<fn(&'bound Run) -> Output>,
}

/// A segment of the prompt being built.
enum Segment {
    Text(String),
    ToolReference(String),
}

impl<'bound, Output, R: Role> ThinkBuilder<'bound, Output, R, NullRun>
where
    R: HasPeer<Agent>,
    Output: Send + JsonSchema + DeserializeOwned + 'static,
{
    pub(crate) fn new(cx: ConnectionTo<R>, observer: Option<Arc<dyn ThinkObserver>>) -> Self {
        Self {
            cx,
            segments: Vec::new(),
            server: McpServer::builder("patchwork".to_string())
                .instructions("You have access to tools. Call return_result when done."),
            explicit_spacing: false,
            observer,
            phantom: PhantomData,
        }
        .textln("Please complete the following task to the best of your ability,")
        .textln("No further instructions will be given,")
        .textln(
            "so do your best to interpret the instructions without further feedback from the user,",
        )
        .textln("making use of the tools you have available.")
        .textln("")
        .textln(
            "IMPORTANT: When complete, invoke the `return_result` tool with the requested result.",
        )
        .textln("")
    }
}

impl<'bound, Output, R: Role, Run: RunWithConnectionTo<R>> ThinkBuilder<'bound, Output, R, Run>
where
    R: HasPeer<Agent>,
    Output: Send + JsonSchema + DeserializeOwned + 'static,
{
    /// Add literal text to the prompt.
    pub fn text(mut self, text: &str) -> Self {
        self.segments.push(Segment::Text(text.to_string()));
        self
    }

    /// Add literal text to the prompt followed by a newline.
    pub fn textln(mut self, text: &str) -> Self {
        self.segments.push(Segment::Text(format!("{text}\n")));
        self
    }

    /// Interpolate a value using its [`Display`] implementation.
    pub fn display(mut self, value: &impl Display) -> Self {
        self.segments.push(Segment::Text(value.to_string()));
        self
    }

    /// Interpolate a value using its [`Debug`] implementation.
    ///
    /// Useful for paths, complex types, or when you want to see the
    /// debug representation.
    pub fn debug(mut self, value: &impl Debug) -> Self {
        self.segments.push(Segment::Text(format!("{:?}", value)));
        self
    }

    /// Disable automatic spacing between segments.
    ///
    /// By default, the builder inserts spaces between segments unless
    /// the next segment starts with punctuation. Call this to require
    /// explicit spacing.
    pub fn explicit_spacing(mut self) -> Self {
        self.explicit_spacing = true;
        self
    }

    /// Build the final prompt string with smart spacing.
    fn build_prompt(&self) -> String {
        let mut result = String::new();

        for (i, segment) in self.segments.iter().enumerate() {
            let text = match segment {
                Segment::Text(t) => t.as_str(),
                Segment::ToolReference(name) => {
                    // Tool references are embedded inline
                    result.push_str(&format!("<mcp_tool>{}</mcp_tool>", name));
                    continue;
                }
            };

            // Smart spacing: insert space before this segment if needed
            if !self.explicit_spacing && i > 0 && !result.is_empty() {
                let needs_space = !result.ends_with([' ', '\t', '\n', '(', '[', '{'])
                    && !text.starts_with(['.', ',', ':', ';', '!', '?']);
                if needs_space {
                    result.push(' ');
                }
            }

            result.push_str(text);
        }

        result
    }

    /// Register a tool and embed a reference to it in the prompt.
    ///
    /// The tool closure receives the input and an [`McpConnectionTo`], and returns
    /// the output. Both input and output must implement [`JsonSchema`] for
    /// the LLM to understand the expected types.
    ///
    /// Tools can capture references from the stack frame, enabling them to
    /// access and mutate local data during the session. Tool invocations are
    /// serialized (one at a time) because the closure is `AsyncFnMut`.
    ///
    /// Due to Rust compiler limitations, you must pass `agent_client_protocol::tool_fn_mut!()`
    /// as the final argument.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let results = Mutex::new(Vec::new());
    /// patchwork.think()
    ///     .text("Process the data using")
    ///     .tool(
    ///         "transform",
    ///         "Transform the input data",
    ///         async |input: TransformInput, _cx| {
    ///             let output = transform_data(&input);
    ///             results.lock().unwrap().push(output.clone());
    ///             Ok(output)
    ///         },
    ///         agent_client_protocol::tool_fn_mut!(),
    ///     )
    ///     .run()
    ///     .await?
    /// ```
    pub fn tool<I, O, F, H>(
        mut self,
        name: &str,
        description: &str,
        func: F,
        tool_future_hack: H,
    ) -> ThinkBuilder<'bound, Output, R, impl RunWithConnectionTo<R>>
    where
        I: JsonSchema + DeserializeOwned + Send + 'static,
        O: JsonSchema + Serialize + Send + 'static,
        F: AsyncFnMut(I, McpConnectionTo<R>) -> Result<O, agent_client_protocol::Error> + Send,
        H: for<'a> Fn(
                &'a mut F,
                I,
                McpConnectionTo<R>,
            ) -> BoxFuture<'a, Result<O, agent_client_protocol::Error>>
            + Send
            + 'static,
    {
        debug!(tool_name = name, "registering tool");
        self.segments.push(Segment::ToolReference(name.to_string()));
        ThinkBuilder {
            cx: self.cx,
            segments: self.segments,
            server: self
                .server
                .tool_fn_mut(name, description, func, tool_future_hack),
            explicit_spacing: self.explicit_spacing,
            observer: self.observer,
            phantom: PhantomData,
        }
    }

    /// Register a tool without embedding a reference in the prompt.
    ///
    /// Use this when you want the tool to be available but don't want to
    /// explicitly mention it at this point in the prompt.
    ///
    /// Due to Rust compiler limitations, you must pass `agent_client_protocol::tool_fn_mut!()`
    /// as the final argument.
    pub fn define_tool<I, O, F, H>(
        self,
        name: &str,
        description: &str,
        func: F,
        tool_future_hack: H,
    ) -> ThinkBuilder<'bound, Output, R, impl RunWithConnectionTo<R>>
    where
        I: JsonSchema + DeserializeOwned + Send + 'static,
        O: JsonSchema + Serialize + Send + 'static,
        F: AsyncFnMut(I, McpConnectionTo<R>) -> Result<O, agent_client_protocol::Error> + Send,
        H: for<'a> Fn(
                &'a mut F,
                I,
                McpConnectionTo<R>,
            ) -> BoxFuture<'a, Result<O, agent_client_protocol::Error>>
            + Send
            + 'static,
    {
        debug!(tool_name = name, "defining tool (hidden from prompt)");
        ThinkBuilder {
            cx: self.cx,
            segments: self.segments,
            server: self
                .server
                .tool_fn_mut(name, description, func, tool_future_hack),
            explicit_spacing: self.explicit_spacing,
            observer: self.observer,
            phantom: PhantomData,
        }
    }

}

impl<'bound, Output, R: Role, Run: RunWithConnectionTo<R>> IntoFuture for ThinkBuilder<'bound, Output, R, Run>
where
    R: HasPeer<Agent>,
    Output: Send + JsonSchema + DeserializeOwned + 'static,
    Run: Send,
{
    type Output = Result<Output, Error>;

    type IntoFuture = BoxFuture<'bound, Result<Output, Error>>;

    fn into_future(self) -> Self::IntoFuture {
        Box::pin(async move {
            // Build prompt before consuming server
            let prompt = self.build_prompt();
            let cx = self.cx;
            let observer = self.observer;

            // Use a cell to store the result from the return_result tool
            let mut output: Option<Output> = None;

            // Add the return_result tool
            let server = self.server.tool_fn_mut(
                "return_result",
                "Return the final result. Call this when you have completed the task.",
                async |input: ReturnResultInput<Output>, _cx| {
                    debug!("return_result tool invoked");
                    output = Some(input.result);
                    Ok(ReturnResultOutput { success: true })
                },
                agent_client_protocol::tool_fn_mut!(),
            );

            if let Some(observer) = &observer {
                observer.on_prompt(&prompt);
            }

            info!(prompt_len = prompt.len(), "executing think block");
            trace!(prompt = %prompt, "full prompt");

            // Create a session with the MCP server and run it
            let cwd = std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from("/"));

            cx.build_session(&cwd)
                .with_mcp_server(server.build())?
                .block_task()
                .run_until(async |mut session| {
                    session.send_prompt(&prompt)?;
                    tracing::info!(?prompt, "sending prompt");

                    // Wait for updates until we get a stop reason
                    loop {
                        let update = session.read_update().await?;
                        trace!(?update, "received session update");
                        match update {
                            agent_client_protocol::SessionMessage::StopReason(reason) => {
                                debug!(?reason, "session stopped");
                                if let Some(observer) = &observer {
                                    observer.on_stop(&reason);
                                }
                                break;
                            }
                            agent_client_protocol::SessionMessage::SessionMessage(dispatch) => {
                                MatchDispatch::new(dispatch)
                                    .if_notification(async |notification: SessionNotification| {
                                        tracing::debug!(?notification, "received session notification");
                                        if let Some(observer) = &observer {
                                            observer.on_notification(&notification);
                                        }
                                        Ok(())
                                    })
                                    .await
                                    .if_request(
                                        async |request: RequestPermissionRequest, responder| {
                                            tracing::debug!(
                                                ?request,
                                                "received tool use permission request"
                                            );
                                            if let Some(observer) = &observer {
                                                observer.on_permission_request(&request);
                                            }
                                            // approve all tool usage
                                            let option =
                                                request.options.iter().find(|o| match o.kind {
                                                    PermissionOptionKind::AllowOnce
                                                    | PermissionOptionKind::AllowAlways => true,
                                                    PermissionOptionKind::RejectOnce
                                                    | PermissionOptionKind::RejectAlways => false,
                                                    _ => false,
                                                });
                                            let outcome = option
                                                .map(|o| {
                                                    RequestPermissionOutcome::Selected(
                                                        SelectedPermissionOutcome::new(
                                                            o.option_id.clone(),
                                                        ),
                                                    )
                                                })
                                                .unwrap_or(RequestPermissionOutcome::Cancelled);
                                            responder.respond(RequestPermissionResponse::new(outcome))
                                        },
                                    )
                                    .await
                                    .otherwise_ignore()?
                            }
                            _ => continue,
                        }
                    }
                    Ok(())
                })
                .await?;

            if output.is_some() {
                info!("think block completed successfully");
            } else {
                warn!("think block completed but no result was returned");
            }

            output.ok_or(Error::NoResult)
        })
    }
}

/// Input schema for the return_result tool.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
struct ReturnResultInput<T> {
    /// The result value to return.
    result: T,
}

/// Output schema for the return_result tool.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
struct ReturnResultOutput {
    /// Whether the result was successfully recorded.
    success: bool,
}