pub mod cell;
pub mod child_summary;
pub mod children;
pub mod compaction;
pub mod context;
pub mod docs;
pub mod entries;
#[cfg(feature = "test-support")]
pub mod faux;
pub mod gate;
pub mod generation;
pub mod host_ops;
pub mod inbox;
pub mod live;
pub mod models;
pub mod prompt;
pub mod submissions;
pub mod swarm;
mod throttle;
pub mod usage;
mod util;
use std::sync::Arc;
use ai::{AssistantMessage, Message};
use durable::{
ConversationId, ConversationOwnership, ConversationQuery, Cursor, Doc, EntryDraft, EntryId, EntryQuery,
EntryRecord, Owner, ROOT_CONVERSATION_ID, ScanOrder, SessionEnd, Storage, SubmissionId, SubmissionRecord, TaskId,
TaskRecord, Tx, TxScope,
};
use indexmap::IndexMap;
use serde_json::{Value, json};
pub use cell::{CellCall, CellExecutor, CellProgress, CellResult, Replay};
pub use context::ContextView;
pub use generation::Control;
pub use inbox::{QueueMode, QueueModes};
pub use models::{CompactionPolicy, ModelRef, Models, Settings, SettingsSource, StreamRequest, ToolExecution};
use submissions::admit_run_with_steering;
pub use submissions::{SubmissionAbort, SubmissionDraft, WhenBusy, admit_submission};
use crate::scheduler::{
AbortResult, Clock, Ctx, Extension, FallibleClock, Harness, HarnessError, HarnessOptions, Inspection, Registry,
Report, Result, safe_now,
};
use submissions::Submissions;
pub type SectionSource =
Arc<dyn Fn(ConversationId, &crate::scheduler::Cancel) -> Result<IndexMap<String, String>> + Send + Sync>;
pub(crate) type OnYield = Arc<dyn Fn(&AssistantMessage) -> Option<Value> + Send + Sync>;
pub(crate) type BeforeRequest = Arc<dyn Fn(Vec<Message>) -> Vec<Message> + Send + Sync>;
pub(crate) type AfterResponse = Arc<dyn Fn(&AssistantMessage) + Send + Sync>;
pub(crate) type CompactionFor = Arc<dyn Fn(ConversationId) -> Option<CompactionPolicy> + Send + Sync>;
pub(crate) type BeforeCompaction = Arc<dyn Fn(ConversationId, u64, u64) -> Vec<Value> + Send + Sync>;
#[derive(Clone, Default)]
pub struct Hooks {
pub compaction: Option<CompactionFor>,
pub before_compaction: Option<BeforeCompaction>,
pub on_yield: Option<OnYield>,
pub before_request: Option<BeforeRequest>,
pub after_response: Option<AfterResponse>,
pub gate_request: Option<gate::GateRequest>,
pub gate_tool: Option<gate::GateTool>,
}
pub(crate) struct Services {
pub(crate) models: Arc<dyn Models>,
pub(crate) cells: Arc<dyn CellExecutor>,
pub(crate) settings: SettingsSource,
pub(crate) sections: Option<SectionSource>,
pub(crate) hooks: Hooks,
pub(crate) children: children::ChildServices,
}
impl Services {
pub(crate) fn compaction_policy(&self, conversation: ConversationId, settings: &Settings) -> CompactionPolicy {
self.hooks
.compaction
.as_ref()
.and_then(|policy| policy(conversation))
.unwrap_or(settings.compaction)
}
}
#[derive(Clone)]
pub struct ChatOptions {
pub registry: Registry,
pub models: Arc<dyn Models>,
pub cells: Arc<dyn CellExecutor>,
pub settings: Option<SettingsSource>,
pub sections: Option<SectionSource>,
pub hooks: Hooks,
pub now: Option<FallibleClock>,
pub on_report: Option<Report>,
}
impl ChatOptions {
pub fn new(registry: Registry, models: Arc<dyn Models>, cells: Arc<dyn CellExecutor>) -> ChatOptions {
ChatOptions {
registry,
models,
cells,
settings: None,
sections: None,
hooks: Hooks::default(),
now: None,
on_report: None,
}
}
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct AgentChange {
pub model: Option<Option<ModelRef>>,
pub thinking_level: Option<Option<String>>,
}
impl AgentChange {
pub fn model(model: ModelRef) -> AgentChange {
AgentChange {
model: Some(Some(model)),
thinking_level: None,
}
}
}
pub fn configure(tx: &mut Tx<'_>, conversation_id: ConversationId, change: &AgentChange) -> Result<()> {
let state = tx.doc(&docs::AGENT, Owner::Conversation(conversation_id))?;
match &change.model {
None => {}
Some(None) => util::delete(&state, &["model"])?,
Some(Some(model)) => util::set(&state, &["model"], util::to_value(model))?,
}
match &change.thinking_level {
None => {}
Some(None) => util::delete(&state, &["thinkingLevel"])?,
Some(Some(level)) => util::set(&state, &["thinkingLevel"], json!(level))?,
}
Ok(())
}
pub(crate) fn create_documents(tx: &mut Tx<'_>, conversation_id: ConversationId, fork: bool) -> Result<()> {
let owner = Owner::Conversation(conversation_id);
for doc in [&docs::LIVE, &docs::INBOX, &docs::USAGE, &docs::PROVIDER] {
tx.doc(doc, owner)?;
}
if !fork {
tx.doc(&docs::AGENT, owner)?;
}
Ok(())
}
#[derive(Clone)]
pub struct ChatHarness {
harness: Harness,
submissions: Arc<Submissions>,
now: Clock,
settings: SettingsSource,
}
impl ChatHarness {
pub fn open(storage: impl Storage + Send + 'static, options: ChatOptions) -> Result<ChatHarness> {
let report: Report = {
let on_report = options.on_report.clone();
Arc::new(move |error| {
if let Some(on_report) = &on_report {
let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| on_report(error)));
}
})
};
let now = safe_now(options.now.clone(), report.clone());
let settings: SettingsSource = options.settings.clone().unwrap_or_else(|| Arc::new(Settings::default));
let services = Arc::new(Services {
models: options.models.clone(),
cells: options.cells.clone(),
settings: settings.clone(),
sections: options.sections.clone(),
hooks: options.hooks.clone(),
children: children::ChildServices::default(),
});
options.registry.install(Extension::tasks(
"pi",
vec![
generation::generation_task(services.clone()),
cell::cell_task(services.clone()),
compaction::compaction_task(services.clone()),
children::child_task(services.clone()),
],
))?;
let clock = now.clone();
let mut harness_options = HarnessOptions::new(Arc::new(options.registry.clone()));
harness_options.now = Some(Arc::new(move || Ok(clock())));
harness_options.on_report = Some(report);
harness_options.settle_outcome = Some(Arc::new(live::settle_scheduler_outcome));
harness_options.withdraw_inputs = Some(Arc::new(inbox::withdraw_queued_inputs));
let harness = Harness::open(storage, harness_options)?;
let submissions = match Submissions::new(harness.session().clone()) {
Ok(submissions) => submissions,
Err(error) => {
let _ = harness.close();
return Err(error);
}
};
let _ = services.children.submissions.set(submissions.clone());
Ok(ChatHarness {
harness,
submissions,
now,
settings,
})
}
pub fn harness(&self) -> &Harness {
&self.harness
}
pub fn now(&self) -> u64 {
(self.now)()
}
pub fn resume(&self) -> Result<()> {
self.harness.resume()
}
pub fn commit<T>(&self, change: impl FnOnce(&mut Tx<'_>) -> Result<T>) -> Result<T> {
self.harness.commit(change)
}
fn handle(&self, id: ConversationId) -> Conversation {
Conversation { id, chat: self.clone() }
}
pub fn root(&self, agent: Option<&AgentChange>) -> Result<Conversation> {
let id = self.harness.commit(|tx| {
if tx.conversation(ROOT_CONVERSATION_ID)?.is_some() {
return Ok(ROOT_CONVERSATION_ID);
}
let record = tx.create_root_conversation()?;
create_documents(tx, record.id, false)?;
if let Some(agent) = agent {
configure(tx, record.id, agent)?;
}
Ok(record.id)
})?;
Ok(self.handle(id))
}
pub fn create_conversation(
&self,
ownership: ConversationOwnership,
agent: Option<&AgentChange>,
) -> Result<Conversation> {
let id = self.harness.commit(|tx| {
let record = tx.create_conversation(ownership)?;
create_documents(tx, record.id, false)?;
if let Some(agent) = agent {
configure(tx, record.id, agent)?;
}
Ok(record.id)
})?;
Ok(self.handle(id))
}
pub fn conversation(&self, id: ConversationId) -> Result<Option<Conversation>> {
let record = self.harness.commit(|tx| Ok(tx.conversation(id)?))?;
Ok(record.map(|record| self.handle(record.id)))
}
pub fn submission(&self, id: SubmissionId) -> Result<Option<Submission>> {
Ok(self
.submissions
.exists(id)?
.then(|| Submission { id, chat: self.clone() }))
}
pub fn abort_submission(
&self,
id: SubmissionId,
conversation_id: Option<ConversationId>,
) -> Result<SubmissionAbort> {
self.submissions.abort(id, conversation_id)
}
pub fn get_task(&self, id: TaskId) -> Result<Option<TaskRecord>> {
self.harness.get_task(id)
}
pub fn abort_task(&self, id: TaskId, ctx: &Ctx) -> Result<AbortResult> {
self.harness.abort_task(id, ctx)
}
pub fn wait_for_task(&self, id: TaskId, ctx: &Ctx) -> Result<TaskRecord> {
self.harness.wait_for_task(id, ctx)
}
pub fn wait_for_idle(&self, ctx: &Ctx) -> Result<()> {
self.harness.wait_for_idle(ctx)
}
pub fn inspect(&self) -> Result<Inspection> {
self.harness.inspect()
}
pub fn snapshot(&self, doc: &Doc, owner: Owner) -> Result<Option<Arc<Value>>> {
self.harness.snapshot(doc, owner)
}
pub fn live(&self, conversation_id: ConversationId) -> Result<Value> {
Ok(self
.snapshot(&docs::LIVE, Owner::Conversation(conversation_id))?
.map(|value| (*value).clone())
.unwrap_or(json!({})))
}
pub fn inbox(&self, conversation_id: ConversationId) -> Result<Vec<(SubmissionId, String)>> {
let state = self.snapshot(&docs::INBOX, Owner::Conversation(conversation_id))?;
Ok(state
.and_then(|state| state["items"].as_array().cloned())
.unwrap_or_default()
.iter()
.map(|item| {
(
SubmissionId(item["id"].as_u64().unwrap_or_default()),
item["mode"].as_str().unwrap_or_default().to_owned(),
)
})
.collect())
}
pub fn provider_session_ids(&self) -> Result<Vec<String>> {
let mut ids = Vec::new();
for id in self.conversation_ids()? {
if let Some(state) = self.snapshot(&docs::PROVIDER, Owner::Conversation(id))?
&& let Some(session) = state["sessionId"].as_str()
{
ids.push(session.to_owned());
}
}
Ok(ids)
}
fn conversation_ids(&self) -> Result<Vec<ConversationId>> {
self.harness.commit(|tx| {
let mut ids = Vec::new();
let mut cursor: Option<Cursor> = None;
loop {
let page = tx.committed(|storage| {
storage.scan_conversations(&ConversationQuery::default(), 256, cursor.as_ref())
})?;
ids.extend(page.items.into_iter().map(|record| record.id));
match page.next {
Some(next) => cursor = Some(next),
None => return Ok(ids),
}
}
})
}
pub fn usage(&self) -> Result<Value> {
let conversations = self.conversation_ids()?;
let mut total = json!({ "models": {}, "tools": {} });
for id in conversations {
if let Some(state) = self.snapshot(&docs::USAGE, Owner::Conversation(id))? {
usage::add_usage_state(&mut total, &state);
}
}
Ok(total)
}
pub fn close(&self) -> Result<()> {
self.harness.close()
}
pub fn closed(&self) -> SessionEnd {
self.harness.closed()
}
fn submit(&self, conversation_id: ConversationId, draft: &SubmissionDraft) -> Result<Submission> {
let _ = self.harness.resume();
let id = self.harness.commit(|tx| {
admit_submission(
tx,
conversation_id,
draft,
(self.now)(),
(self.settings)().queue_modes(),
)
})?;
Ok(Submission { id, chat: self.clone() })
}
fn submit_run(
&self,
conversation_id: ConversationId,
first: &SubmissionDraft,
steering: &[SubmissionDraft],
) -> Result<Vec<Submission>> {
let _ = self.harness.resume();
let placed = self.harness.commit(|tx| {
admit_run_with_steering(
tx,
conversation_id,
first,
steering,
(self.now)(),
(self.settings)().queue_modes(),
)
})?;
match placed {
Some(ids) => Ok(ids
.into_iter()
.map(|id| Submission { id, chat: self.clone() })
.collect()),
None => std::iter::once(first)
.chain(steering)
.map(|draft| self.submit(conversation_id, draft))
.collect(),
}
}
}
#[derive(Clone)]
pub struct Submission {
pub id: SubmissionId,
chat: ChatHarness,
}
impl Submission {
pub fn status(&self) -> Result<SubmissionRecord> {
self.chat.submissions.status(self.id)
}
pub fn wait(&self, ctx: &Ctx) -> Result<SubmissionRecord> {
let _ = self.chat.harness.resume();
self.chat.submissions.wait(self.id, ctx)
}
pub fn abort(&self) -> Result<SubmissionAbort> {
match self.chat.submissions.abort(self.id, None)? {
SubmissionAbort::NotFound => Err(HarnessError::Error(format!("Submission {} does not exist", self.id.0))),
result => Ok(result),
}
}
}
#[derive(Clone)]
pub struct Conversation {
pub id: ConversationId,
chat: ChatHarness,
}
impl Conversation {
pub fn chat(&self) -> &ChatHarness {
&self.chat
}
pub fn submit(&self, draft: SubmissionDraft) -> Result<Submission> {
self.chat.submit(self.id, &draft)
}
pub fn submit_run(&self, first: SubmissionDraft, steering: Vec<SubmissionDraft>) -> Result<Vec<Submission>> {
self.chat.submit_run(self.id, &first, &steering)
}
pub fn reset(&self, handoff: Option<&str>) -> Result<()> {
let mut entry = json!({ "kind": entries::RESET, "head": "self" });
if let Some(text) = handoff {
entry["model"] = json!([{ "role": "user", "content": text, "timestamp": self.chat.now() }]);
}
self.submit(SubmissionDraft::write(entry))?;
Ok(())
}
pub fn compact(&self, instructions: Option<&str>) -> Result<TaskId> {
let _ = self.chat.harness.resume();
let id = self.id;
self.commit(|tx| compaction::create_compaction(tx, id, "manual", instructions, None))
}
pub fn commit<T>(&self, change: impl FnOnce(&mut Tx<'_>) -> Result<T>) -> Result<T> {
let scope = TxScope {
conversation_id: Some(self.id),
task_id: None,
};
crate::scheduler::commit_on(self.chat.harness.session(), scope, change)
}
pub fn configure(&self, change: &AgentChange) -> Result<()> {
let id = self.id;
self.commit(|tx| configure(tx, id, change))
}
pub fn context(&self, at: Option<EntryId>) -> Result<ContextView> {
let id = self.id;
self.commit(|tx| context::read_context(tx, id, at))
}
pub fn entries(&self) -> Result<Vec<EntryRecord>> {
let id = self.id;
self.commit(|tx| {
let query = EntryQuery {
conversation_id: id,
min_entry_id: None,
max_entry_id: None,
order: Some(ScanOrder::Ascending),
};
let mut entries = Vec::new();
let mut cursor: Option<Cursor> = None;
loop {
let page = tx.committed(|storage| storage.scan_entries(&query, 256, cursor.as_ref()))?;
entries.extend(page.items);
match page.next {
Some(next) => cursor = Some(next),
None => return Ok(entries),
}
}
})
}
pub fn append(&self, draft: EntryDraft) -> Result<EntryRecord> {
let id = self.id;
self.commit(|tx| Ok(tx.append_entry(id, draft)?))
}
pub fn fork(&self, at: EntryId, ownership: ConversationOwnership) -> Result<Conversation> {
let parent = self.id;
let id = self.chat.harness.commit(|tx| {
let record = tx.fork_conversation(parent, at, ownership)?;
create_documents(tx, record.id, true)?;
Ok(record.id)
})?;
Ok(self.chat.handle(id))
}
pub fn abort(&self, background: bool, ctx: &Ctx) -> Result<()> {
self.chat
.harness
.conversation(self.id)?
.map_or(Ok(()), |handle| handle.abort(background, ctx))
}
pub fn wait_for_idle(&self, ctx: &Ctx) -> Result<()> {
self.chat
.harness
.conversation(self.id)?
.map_or(Ok(()), |handle| handle.wait_for_idle(ctx))
}
}