feat: let a background task ask the chat that started it, not just the Inbox
Nightly Build / build (push) Successful in 7m42s
Nightly Build / build (push) Successful in 7m42s
An async sub-agent runs in a session of its own, so the rich per-session events
that draw the inline approval card never reach the chat's socket — only the
id-only inbox lifecycle ones do. A task blocked on an approval was therefore
invisible in the conversation that started it, and the only way to unblock it
was to notice the sidebar badge and go to the Inbox.
The chat already shows what it handed off. This asks the same question of the
pending items: `GET /{source}/inbox` joins them against the sessions of this
conversation's running async jobs, so "whose is this" has one answer, in the
same place `/{source}/tasks` answers it for a task. The client is left with a
list to render, not a correlation to guess. The live path adds no event — the
existing `approval_requested` / `clarification_*` broadcasts already reach every
socket of the user, and re-reading the endpoint turns a nudge into something
renderable and survives a reload for free.
The card sits above the task strip rather than in the transcript: the task that
is asking may have been started twenty messages ago, and a card that scrolls
away is a card that gets missed. One at a time, with a count of what is behind
it — a blocked task stays blocked whether or not its card is on screen, so
stacking them would trade a readable chat for a queue nobody asked to see. And
it closes: the ✕ hides the card without resolving anything, leaving the item in
the Inbox, because a panel that cannot be moved takes the chat hostage.
`InboxCardsMixin` is the cards and their resolve calls, split out of
`InboxMixin` so the chat and the Inbox render the same approval rather than two
drifting copies of it; `_afterInboxResolve` is the only thing they disagree on.
Elicitations are left out: `PendingElicitationInfo` carries no `session_id`, so
there is nothing to attribute one to a task with.
Also: an async task's context label said "CronJob:", which sends whoever reads
the approval looking on the wrong page — and now says so next to the task's
real name.
This commit is contained in:
@@ -29,6 +29,92 @@ pub async fn list(
|
||||
})))
|
||||
}
|
||||
|
||||
// ── GET /api/{source}/inbox ───────────────────────────────────────────────────
|
||||
//
|
||||
// The pending items raised by the background tasks *this* conversation started.
|
||||
//
|
||||
// An async sub-agent runs in a session of its own (`source = "cron"`), so its
|
||||
// `ApprovalRequired` / `AgentQuestion` events — the rich, per-session ones that
|
||||
// draw the inline card — never reach the chat's WebSocket, and until now the
|
||||
// only place they surfaced was the Inbox. The chat already shows what it handed
|
||||
// off (the background-task strip); this is the same question asked of the
|
||||
// pending items, so the strip can carry the card too.
|
||||
//
|
||||
// The join is server-side on purpose: "whose is this pending item" is the same
|
||||
// question `/{source}/tasks` answers for a task, and it should have one answer.
|
||||
// The client is left with a list to render, not a correlation to guess.
|
||||
//
|
||||
// Live updates ride the `approval_requested` / `approval_resolved` /
|
||||
// `clarification_*` events, which are already forwarded to every one of this
|
||||
// user's sockets regardless of source — they carry ids only, so this endpoint
|
||||
// is what turns a nudge into something renderable, and what a page reload reads.
|
||||
//
|
||||
// Elicitations are absent: `PendingElicitationInfo` carries no `session_id`, so
|
||||
// there is nothing to attribute one to a task with.
|
||||
|
||||
#[derive(serde::Serialize)]
|
||||
pub struct TaskInboxApproval {
|
||||
/// The task that is asking — the strip labels the card with it.
|
||||
pub job_id: i64,
|
||||
pub job_title: String,
|
||||
#[serde(flatten)]
|
||||
pub item: skald_core::approval::PendingApprovalInfo,
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize)]
|
||||
pub struct TaskInboxClarification {
|
||||
pub job_id: i64,
|
||||
pub job_title: String,
|
||||
#[serde(flatten)]
|
||||
pub item: skald_core::clarification::PendingClarificationInfo,
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize)]
|
||||
pub struct TaskInbox {
|
||||
pub approvals: Vec<TaskInboxApproval>,
|
||||
pub clarifications: Vec<TaskInboxClarification>,
|
||||
}
|
||||
|
||||
pub async fn session_task_inbox(
|
||||
State(skald): State<Arc<Skald>>,
|
||||
Extension(auth): Extension<AuthUser>,
|
||||
Path(p): Path<super::cron::SourcePath>,
|
||||
) -> Result<Json<TaskInbox>, ApiError> {
|
||||
let ctx = require_context(&skald, &auth.user_id).await?;
|
||||
let empty = TaskInbox { approvals: vec![], clarifications: vec![] };
|
||||
|
||||
// A chat that has never run has no session, and therefore no tasks. Not an
|
||||
// error: the strip asks on every load, including the first one.
|
||||
let Some(session_id) = skald_core::db::sources::active_session_id(&ctx.pool, &p.source).await? else {
|
||||
return Ok(Json(empty));
|
||||
};
|
||||
let children =
|
||||
skald_core::db::scheduled_jobs::running_child_sessions(&ctx.pool, session_id).await?;
|
||||
if children.is_empty() {
|
||||
return Ok(Json(empty));
|
||||
}
|
||||
let by_session: std::collections::HashMap<i64, &skald_core::db::scheduled_jobs::RunningChildSession> =
|
||||
children.iter().map(|c| (c.session_id, c)).collect();
|
||||
|
||||
let items = ctx.inbox.list_pending().await;
|
||||
let approvals = items.approvals.into_iter()
|
||||
.filter_map(|item| by_session.get(&item.session_id).map(|job| TaskInboxApproval {
|
||||
job_id: job.job_id,
|
||||
job_title: job.title.clone(),
|
||||
item,
|
||||
}))
|
||||
.collect();
|
||||
let clarifications = items.clarifications.into_iter()
|
||||
.filter_map(|item| by_session.get(&item.session_id).map(|job| TaskInboxClarification {
|
||||
job_id: job.job_id,
|
||||
job_title: job.title.clone(),
|
||||
item,
|
||||
}))
|
||||
.collect();
|
||||
|
||||
Ok(Json(TaskInbox { approvals, clarifications }))
|
||||
}
|
||||
|
||||
// ── POST /api/inbox/approvals/:request_id/resolve ─────────────────────────────
|
||||
|
||||
#[derive(Deserialize)]
|
||||
|
||||
@@ -74,6 +74,9 @@ pub fn router() -> Router<Arc<Skald>> {
|
||||
.route("/{source}/messages", get(sessions::source_messages))
|
||||
// Background (`execute_task mode=async`) tasks of this source's session.
|
||||
.route("/{source}/tasks", get(cron::session_tasks))
|
||||
// Pending approvals/questions raised by those tasks — the chat renders
|
||||
// them itself instead of leaving them to the Inbox alone.
|
||||
.route("/{source}/inbox", get(inbox::session_task_inbox))
|
||||
// File attachments: streamed to disk, so the default body-size limit is
|
||||
// disabled on this route only.
|
||||
.route("/{source}/uploads", post(uploads::upload).layer(DefaultBodyLimit::disable()))
|
||||
|
||||
Reference in New Issue
Block a user