P1 backend: chat persistence, tc-llm providers, runtime loop, gateway SSE
- tc-db: sessions/messages/steps/runs/run_events repos (atomic seq assignment, history with ordered step traces, journal replay-from-offset); migration 0003 - tc-llm: provider-neutral ChatRequest/LlmEvent; ScriptedProvider (scenario TOML, word-level deltas, multi-turn tool legs — ships in production for e2e/air-gap smoke), AnthropicProvider (Messages SSE), OpenAiCompatProvider (vLLM/Ollama/llama.cpp); opt-in live tests via TC_LIVE_LLM=1 - tc-runtime: run loop with persist-before-emit event journal, real built-in clock.now tool, step rows on the reply message, tool-error resilience, broadcast channels for live attach - tc-api: agent CRUD + settings/full (tenant-isolated, RBAC'd, audited), sessions create/list/history?tools=true, POST /api/gateway SSE with monotonic ids and exact resumeFrom journal replay (tested equal to live) - teamclaw-server: config-driven provider factory 83 Rust tests green, all against real Postgres / real TCP. Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
co-authored by
Claude Fable 5
parent
fc173f170d
commit
32008c9ef0
@@ -0,0 +1,89 @@
|
||||
use sqlx::PgPool;
|
||||
use tc_domain::{AgentRun, RunState, SessionId};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::DbError;
|
||||
|
||||
pub async fn create(pool: &PgPool, session_id: SessionId) -> Result<Uuid, DbError> {
|
||||
let id = Uuid::now_v7();
|
||||
sqlx::query!(
|
||||
"INSERT INTO agent_runs (id, session_id, state) VALUES ($1, $2, 'running')",
|
||||
id,
|
||||
session_id.as_uuid(),
|
||||
)
|
||||
.execute(pool)
|
||||
.await?;
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
pub async fn get(pool: &PgPool, id: Uuid) -> Result<AgentRun, DbError> {
|
||||
let row = sqlx::query!(
|
||||
"SELECT id, session_id, state, last_event_id, error FROM agent_runs WHERE id = $1",
|
||||
id,
|
||||
)
|
||||
.fetch_one(pool)
|
||||
.await?;
|
||||
Ok(AgentRun {
|
||||
id: row.id,
|
||||
session_id: SessionId::from(row.session_id),
|
||||
state: row.state.parse().expect("state CHECK constraint"),
|
||||
last_event_id: row.last_event_id,
|
||||
error: row.error,
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn set_state(
|
||||
pool: &PgPool,
|
||||
id: Uuid,
|
||||
state: RunState,
|
||||
error: Option<&str>,
|
||||
) -> Result<(), DbError> {
|
||||
let result = sqlx::query!(
|
||||
"UPDATE agent_runs
|
||||
SET state = $2, error = COALESCE($3, error), updated_at = now()
|
||||
WHERE id = $1",
|
||||
id,
|
||||
state.as_str(),
|
||||
error,
|
||||
)
|
||||
.execute(pool)
|
||||
.await?;
|
||||
if result.rows_affected() == 0 {
|
||||
return Err(DbError::NotFound);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Records the highest event seq persisted for this run (used by resume).
|
||||
pub async fn set_last_event(pool: &PgPool, id: Uuid, last_event_id: i64) -> Result<(), DbError> {
|
||||
sqlx::query!(
|
||||
"UPDATE agent_runs SET last_event_id = $2, updated_at = now() WHERE id = $1",
|
||||
id,
|
||||
last_event_id,
|
||||
)
|
||||
.execute(pool)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The most recent run for a session, if any (gateway re-attach).
|
||||
pub async fn latest_for_session(
|
||||
pool: &PgPool,
|
||||
session_id: SessionId,
|
||||
) -> Result<Option<AgentRun>, DbError> {
|
||||
let row = sqlx::query!(
|
||||
"SELECT id, session_id, state, last_event_id, error
|
||||
FROM agent_runs WHERE session_id = $1
|
||||
ORDER BY created_at DESC, id DESC LIMIT 1",
|
||||
session_id.as_uuid(),
|
||||
)
|
||||
.fetch_optional(pool)
|
||||
.await?;
|
||||
Ok(row.map(|row| AgentRun {
|
||||
id: row.id,
|
||||
session_id: SessionId::from(row.session_id),
|
||||
state: row.state.parse().expect("state CHECK constraint"),
|
||||
last_event_id: row.last_event_id,
|
||||
error: row.error,
|
||||
}))
|
||||
}
|
||||
Reference in New Issue
Block a user