Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(chat): handle tool call errors gracefully

Steven Enamakeld7e951a92a52
diff --git a/app/src/components/assistant-ui/elements/agent-status.tsx b/app/src/components/assistant-ui/elements/agent-status.tsx
new file mode 100644
index 0000000000..a477e5ddf0
--- /dev/null
+++ b/app/src/components/assistant-ui/elements/agent-status.tsx
@@ -0,0 +1,84 @@
+'use client';
+
+/**
+ * A compact status pill for a long-running agent activity: state dot/icon,
+ * label, optional elapsed time, and a trailing slot (defaults to a
+ * pause/retry icon).
+ *
+ * Vendored from the assistant-ui `elements-agent-status` registry item
+ * (https://r.assistant-ui.com/styles/base-nova/elements-agent-status.json).
+ * Changes from upstream:
+ * - `cn` import path (`@/components/assistant-ui/lib/utils`).
+ * No literal user-facing strings here — every visible value already comes
+ * from props/children, so there is nothing to route through `useT()`.
+ */
+import { cn } from '@/components/assistant-ui/lib/utils';
+import { CheckIcon, PauseIcon, RotateCcwIcon, XIcon } from 'lucide-react';
+import type { ComponentProps, ReactNode } from 'react';
+
+import { mono, paper } from './surfaces';
+
+export type AgentState = 'working' | 'waiting' | 'done' | 'failed';
+
+export interface StatusStep {
+ state: AgentState;
+ label: string;
+}
+
+export function AgentStatus({
+ state,
+ label,
+ elapsed,
+ trailing,
+ className,
+ ...props
+}: Omit<ComponentProps<'span'>, 'children' | 'state' | 'label' | 'elapsed'> & {
+ state: AgentState;
+ label: string;
+ elapsed?: string | undefined;
+ trailing?: ReactNode | undefined;
+}) {
+ return (
+ <span
+ data-slot="agent-status"
+ className={cn(paper, 'flex items-center gap-2.5 rounded-full py-1.5 ps-3.5 pe-1.5', className)}
+ {...props}>
+ {state === 'done' ? (
+ <CheckIcon aria-hidden className="size-3 shrink-0 text-emerald-500" />
+ ) : state === 'failed' ? (
+ <XIcon aria-hidden className="text-destructive size-3 shrink-0" />
+ ) : (
+ <span
+ aria-hidden
+ className={cn(
+ 'size-1.5 shrink-0 rounded-full motion-reduce:animate-none',
+ state === 'working'
+ ? 'animate-pulse bg-blue-500 dark:bg-blue-400'
+ : 'border-foreground/35 border'
+ )}
+ />
+ )}
+ <span className="sr-only">{state}</span>
+ <span
+ key={label}
+ className="fade-in blur-in-[2px] animate-in max-w-44 truncate text-xs duration-300 motion-reduce:animate-none">
+ {label}
+ </span>
+ {elapsed !== undefined && state !== 'done' && state !== 'failed' && (
+ <span className={cn(mono, 'text-foreground/30 tabular-nums')}>{elapsed}</span>
+ )}
+ <span
+ aria-hidden
+ data-slot="agent-status-trailing"
+ className="text-foreground/45 flex size-6 items-center justify-center rounded-full">
+ {trailing !== undefined ? (
+ trailing
+ ) : state === 'done' || state === 'failed' ? (
+ <RotateCcwIcon className="size-3" />
+ ) : (
+ <PauseIcon className="size-3" />
+ )}
+ </span>
+ </span>
+ );
+}
diff --git a/app/src/components/assistant-ui/elements/memory-chips.tsx b/app/src/components/assistant-ui/elements/memory-chips.tsx
new file mode 100644
index 0000000000..631fbc9496
--- /dev/null
+++ b/app/src/components/assistant-ui/elements/memory-chips.tsx
@@ -0,0 +1,82 @@
+'use client';
+
+/**
+ * Vendored from the assistant-ui `elements-memory-chips` registry item
+ * (https://r.assistant-ui.com/styles/base-nova/elements-memory-chips.json).
+ * Changes from upstream:
+ * - `cn` import path (`@/components/assistant-ui/lib/utils`).
+ * - The "remembered {n}" / "memory" heading and the `Forget "{text}"`
+ * aria-label are `headingRememberedLabel` / `headingIdleLabel` /
+ * `forgetAriaLabel` props with English defaults, for `useT()` — see
+ * `features/conversations/aui/ChatMemoryChips.tsx`, the caller.
+ */
+import type { ComponentProps } from 'react';
+import { BrainIcon, XIcon } from 'lucide-react';
+
+import { cn } from '@/components/assistant-ui/lib/utils';
+
+import { field, ghostButton, mono } from './surfaces';
+
+export type MemoryChange = 'added' | 'updated' | 'existing';
+
+export interface MemoryChip {
+ id: string;
+ text: string;
+ change: MemoryChange;
+}
+
+export function MemoryChips({
+ chips,
+ onForget,
+ headingRememberedLabel = (n: number) => `remembered ${n}`,
+ headingIdleLabel = 'memory',
+ forgetAriaLabel = (text: string) => `Forget "${text}"`,
+ className,
+ ...props
+}: Omit<
+ ComponentProps<'div'>,
+ 'children' | 'chips' | 'onForget'
+> & {
+ chips: readonly MemoryChip[];
+ onForget?: (id: string) => void;
+ headingRememberedLabel?: (n: number) => string;
+ headingIdleLabel?: string;
+ forgetAriaLabel?: (text: string) => string;
+}) {
+ const fresh = chips.filter(chip => chip.change !== 'existing').length;
+
+ return (
+ <div data-slot="memory-chips" className={cn('flex w-full max-w-sm flex-col gap-2', className)} {...props}>
+ <div className="flex items-center gap-1.5">
+ <BrainIcon className="text-foreground/30 size-3.5" />
+ <span className={cn(mono, 'text-foreground/35')}>
+ {fresh > 0 ? headingRememberedLabel(fresh) : headingIdleLabel}
+ </span>
+ </div>
+
+ <div className="flex flex-wrap gap-1.5">
+ {chips.map(chip => (
+ <span
+ key={chip.id}
+ className={cn(
+ 'fade-in zoom-in-95 animate-in fill-mode-both group flex items-center gap-1 rounded-full py-1 pr-1 pl-2.5 text-xs duration-300',
+ chip.change === 'existing'
+ ? cn(field, 'text-foreground/55')
+ : 'bg-blue-500/12 text-blue-700 dark:bg-blue-400/15 dark:text-blue-300'
+ )}>
+ {chip.text}
+ {onForget && (
+ <button
+ type="button"
+ aria-label={forgetAriaLabel(chip.text)}
+ onClick={() => onForget(chip.id)}
+ className={cn(ghostButton, 'size-4')}>
+ <XIcon className="size-2.5" />
+ </button>
+ )}
+ </span>
+ ))}
+ </div>
+ </div>
+ );
+}
diff --git a/app/src/services/chatService.ts b/app/src/services/chatService.ts
index 0d07756823..80c54de1a7 100644
--- a/app/src/services/chatService.ts
+++ b/app/src/services/chatService.ts
@@ -1062,6 +1062,22 @@ export function subscribeChatEvents(listeners: ChatEventListeners): () => void {
handlers.push([EVENTS.approvalRequest, cb]);
}
+ if (listeners.onApprovalDecided) {
+ const cb = (payload: unknown) => {
+ const e = payload as ChatApprovalDecidedEvent;
+ chatLog(
+ '%s thread_id=%s request_id=%s resolution=%s',
+ EVENTS.approvalDecided,
+ e.thread_id,
+ e.request_id,
+ e.resolution
+ );
+ listeners.onApprovalDecided?.(e);
+ };
+ socket.on(EVENTS.approvalDecided, cb);
+ handlers.push([EVENTS.approvalDecided, cb]);
+ }
+
if (listeners.onPlanReviewRequest) {
const cb = (payload: unknown) => {
const e = payload as ChatPlanReviewRequestEvent;
diff --git a/crates/openhuman-core/src/agent/tools/todo.rs b/crates/openhuman-core/src/agent/tools/todo.rs
index c8f453c8c3..6bfa6ed6b6 100644
--- a/crates/openhuman-core/src/agent/tools/todo.rs
+++ b/crates/openhuman-core/src/agent/tools/todo.rs
@@ -130,31 +130,94 @@ impl TodoTool {
tool_context: Option<&dyn ToolRunContext>,
) -> anyhow::Result<ToolResult> {
let scope = current_scope(parent.as_ref(), tool_context);
+ // One-time fallback: a list written under the pre-rekey
+ // `session_id` key (the web channel's `{client_id,thread_id}` JSON
+ // blob) is otherwise invisible once `current_scope` starts keying by
+ // thread id. If the new key has no list yet and the legacy key does,
+ // migrate it forward so an in-flight list isn't dropped by the rekey.
+ if let Some(legacy_key) = legacy_session_key(parent.as_ref(), &scope) {
+ self.migrate_legacy_list_if_absent(&scope, &legacy_key).await;
+ }
tracing::debug!(session_id = ?scope.session_id(), "[tool][todo] dispatch");
let key = ScopedKey(scope.key());
self.inner
.execute_with_context(args, ToolCallOptions::default(), Some(&key))
.await
}
+
+ /// If `scope`'s list is empty and `legacy_key` has a non-empty one,
+ /// copy it forward under `scope`'s key so the rekey is transparent to an
+ /// in-flight session. Best-effort: any store error is logged and
+ /// swallowed — a failed migration just means the tool starts from an
+ /// empty list, same as any other first `todo` call.
+ async fn migrate_legacy_list_if_absent(&self, scope: &TodoScope, legacy_key: &str) {
+ let current = match ops::list(&self.workspace_dir, scope).await {
+ Ok(snapshot) => snapshot,
+ Err(e) => {
+ tracing::debug!(error = %e, "[tool][todo] legacy-migration: current list read failed");
+ return;
+ }
+ };
+ if !current.items.is_empty() {
+ return;
+ }
+ let legacy_scope = TodoScope::Session {
+ id: legacy_key.to_string(),
+ };
+ match ops::list(&self.workspace_dir, &legacy_scope).await {
+ Ok(legacy) if !legacy.items.is_empty() => {
+ tracing::info!(
+ legacy_key,
+ thread_key = scope.key(),
+ items = legacy.items.len(),
+ "[tool][todo] migrating legacy session-keyed list to thread-keyed list"
+ );
+ if let Err(e) = ops::replace(&self.workspace_dir, scope, legacy.items).await {
+ tracing::debug!(error = %e, "[tool][todo] legacy-migration: write failed");
+ }
+ }
+ Ok(_) => {}
+ Err(e) => {
+ tracing::debug!(error = %e, "[tool][todo] legacy-migration: legacy list read failed");
+ }
+ }
+ }
}
+/// The scope this call resolves to: the chat thread id when available,
+/// falling back to the legacy `ParentExecutionContext::session_id` (for
+/// non-web-chat callers that never carry a `thread_id`), and finally the
+/// scratch scope for a bare `Tool::execute` with neither.
fn current_scope(
parent: Option<&ParentExecutionContext>,
tool_context: Option<&dyn ToolRunContext>,
) -> TodoScope {
- if let Some(parent) = parent {
+ if let Some(thread_id) = tool_context.and_then(ToolRunContext::thread_id) {
return TodoScope::Session {
- id: parent.session_id.clone(),
+ id: thread_id.to_owned(),
};
}
- match tool_context.and_then(ToolRunContext::thread_id) {
- Some(thread_id) => TodoScope::Session {
- id: thread_id.to_owned(),
+ match parent {
+ Some(parent) => TodoScope::Session {
+ id: parent.session_id.clone(),
},
None => TodoScope::Scratch,
}
}
+/// The pre-rekey `session_id` key to check as a one-time fallback, when it
+/// differs from the scope's own (now thread-id-first) key.
+fn legacy_session_key(
+ parent: Option<&ParentExecutionContext>,
+ scope: &TodoScope,
+) -> Option<String> {
+ let parent = parent?;
+ if parent.session_id == scope.key() {
+ return None;
+ }
+ Some(parent.session_id.clone())
+}
+
#[cfg(test)]
#[path = "todo_tests.rs"]
mod tests;