forked from vhaudiquet/rhema-claw
The framework 0.4.0 split the toolkit: rhema-agent-tools becomes rhema-bash plus rhema-prompt, and the facade crate is deleted from the workspace this time, so the 0.3 facades never publish. The driver changes name with its types: rhema-coding-agent and its CodingAgent, CodingAgentConfig, and CodingAgentError become rhema-host-agent with HostAgent, HostAgentConfig, and HostAgentError. The claw imports the split crates directly, which drops its own vision once-lock: rhema-bash's wire already caches the process-wide answer. The paste expansion moved off the provider seam. Chips travel as the message's spans note now, and ExpandPastes is deleted: the dispatch input migrates its embedded form into plain text plus the note at run_turn, and a restashed session migrates the same way on restore, one rewrite on the way in exactly as the driver's from_session does. The transcript events keep the raw chip form, the record the web UI owns, and a migration test pins the restore path. max_turn_steps is NonZeroU32, the engine's own budget shape: a config naming zero is refused at load instead of cutting every round dead. The engine's Event and the sandbox error enum are non-exhaustive now, so the hub sink and the protocol error map carry wildcard arms that drop and fail in-band the way their neighbors do. The claw's own version travels with the framework, as the 0.3.0 move carried 0.2.3 to 0.3.0: the workspace and the chart base become 0.4.0, so the edge channel's pre-releases order above the 0.3.x line. fmt, clippy with -D warnings over all features and targets, the workspace tests, and the doc build pass.
75 lines
2.5 KiB
Rust
75 lines
2.5 KiB
Rust
//! The debug provider (`mock` feature): an endless scripted stand-in so
|
|
//! the daemon and a UI can run with zero credentials.
|
|
//!
|
|
//! [`MockProvider`](rhema_core::MockProvider) replays a fixed script and
|
|
//! errors once it runs dry — right for tests, wrong for a daemon a
|
|
//! developer chats with all afternoon. This one never dries up: every
|
|
//! turn answers with a numbered canned reply, streamed as deltas so a UI
|
|
//! exercises the same rendering path a real turn takes.
|
|
|
|
use std::sync::Arc;
|
|
use std::sync::atomic::{AtomicU64, Ordering};
|
|
|
|
use rhema_core::{
|
|
Content, Event, EventSink, Message, ModelOutput, ModelProvider, ModelRequest, ProviderError,
|
|
Role, StopReason, Usage, async_trait,
|
|
};
|
|
|
|
/// Answers every request with a canned, streamed reply.
|
|
#[derive(Default)]
|
|
pub struct MockLoop {
|
|
turns: AtomicU64,
|
|
}
|
|
|
|
impl MockLoop {
|
|
/// The provider, shared as the daemon's backend seam. (Named for what
|
|
/// it returns: a `new` returning `Arc<dyn ModelProvider>` would lie
|
|
/// about constructing `Self`.)
|
|
pub fn provider() -> Arc<dyn ModelProvider> {
|
|
Arc::new(Self::default())
|
|
}
|
|
}
|
|
|
|
#[async_trait]
|
|
impl ModelProvider for MockLoop {
|
|
async fn complete(
|
|
&self,
|
|
_req: ModelRequest,
|
|
sink: &mut dyn EventSink,
|
|
) -> Result<ModelOutput, ProviderError> {
|
|
let turn = self.turns.fetch_add(1, Ordering::Relaxed) + 1;
|
|
let text = format!(
|
|
"Mock claw, turn {turn}. No model is attached - every reply \
|
|
is this canned text, streamed in deltas like the real thing."
|
|
);
|
|
// Three char-safe deltas: the UI's streaming path gets exercised,
|
|
// not just the single-frame one.
|
|
let chars: Vec<&str> = {
|
|
let total = text.chars().count();
|
|
let boundary = |n: usize| -> usize {
|
|
text.char_indices()
|
|
.nth(n)
|
|
.map(|(i, _)| i)
|
|
.unwrap_or(text.len())
|
|
};
|
|
vec![
|
|
&text[..boundary(total / 3)],
|
|
&text[boundary(total / 3)..boundary(2 * total / 3)],
|
|
&text[boundary(2 * total / 3)..],
|
|
]
|
|
};
|
|
for piece in chars {
|
|
sink.emit(Event::TextDelta(piece));
|
|
}
|
|
Ok(ModelOutput {
|
|
message: Message {
|
|
role: Role::Assistant,
|
|
content: vec![Content::Text(text)],
|
|
annotations: Default::default(),
|
|
},
|
|
stop_reason: StopReason::EndTurn,
|
|
usage: Usage::default(),
|
|
})
|
|
}
|
|
}
|