mirror of
https://github.com/windmill-labs/windmill.git
synced 2026-12-04 08:09:16 +00:00
* feat(ai-agent): built-in approval tool that parks the agent An AI agent step can now be given a built-in "Request approval" tool. The model calls `request_approval` with a message; the agent stores its conversation in the new `ai_agent_checkpoint` table, releases its worker by parking its own queue row like a workflow-as-code parent, and resumes with the answer (approved, approver, comment) or a timeout once someone answers from the run page. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(ai-agent): tie approval answers to their request and hold the turn - A turn that asks for approval runs none of its other calls; they are refused so the model repeats them after the answer. - The run page sends the request's call id. An answer naming another request is ignored and the agent parks again for the time left; `suspend` is zeroed when the answer is read, so answers sent while the agent runs are refused. - The checkpoint is read whatever the current roster holds, the answer's outcome is written by position, and a cancel that beats the park completes the job as cancelled. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(ai-agent): say why calls beside an invalid approval request were refused Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(ai-agent): one approval form per request, none on a finished run Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * feat(ai-agent): answer approval requests from a flow's chat Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(ai-agent): show chat approvals for agents that do not stream Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * feat(ai-agent): approval as a per-tool setting, built-in tool asks a question Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(ai-agent): say when a nested agent leaves out tools that require approval Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * feat(ai-agent): show chat approvals and questions on the tool's own card Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * feat(ai-agent): question choices, and approval buttons shared with approval steps Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * feat(ai-agent): show a waiting agent as suspended, and refused calls as not run Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(ai-agent): each added chat card shows its own call, answered marker keyed by job Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test(ai-agent): approve and reject a held call through resume_suspended Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * feat(ai-agent): require approval in a tool settings tab, plain descriptions on built-in tool panes Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(ai-agent): sentence case for the web search tool label Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * feat(ai-agent): built-in tool drawers explain themselves in the header subtitle Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(ai-agent): clearer question tool subtitle Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(ai-agent): badge the tool settings tab like run settings Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * chore(ai-agent): regenerate system prompts and MCP session files Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
144 lines
4.9 KiB
TypeScript
144 lines
4.9 KiB
TypeScript
import type { WebSearchSource } from './copilot/chat/shared'
|
|
import type { AgentMessage } from './aiAgentResult'
|
|
|
|
/**
|
|
* One entry in the trace of an agent run, built from the envelope alone so it
|
|
* renders without waiting on any request; a tool's child job is enrichment. The
|
|
* prompt and the question are deliberately absent: they are the step's inputs and
|
|
* are shown as inputs, while the trace is what the agent did with them.
|
|
*/
|
|
export type AgentTraceEntry =
|
|
| { kind: 'assistant'; content: string }
|
|
/** The worker records only that a search happened. Its citations ride on the assistant
|
|
* turn that follows, and are moved here so the search shows what it found. */
|
|
| { kind: 'search'; sources?: WebSearchSource[] }
|
|
| {
|
|
kind: 'tool'
|
|
name: string
|
|
args?: string
|
|
result: string
|
|
/** Present for a flow-module tool, which runs as its own job. */
|
|
jobId?: string
|
|
/** An MCP tool runs in the worker, so it names its server instead. */
|
|
resourcePath?: string
|
|
}
|
|
|
|
/** `content` is a string for text messages and a part list once images are involved. */
|
|
function contentText(content: unknown): string {
|
|
if (typeof content === 'string') {
|
|
return content
|
|
}
|
|
if (Array.isArray(content)) {
|
|
return content
|
|
.map((part) =>
|
|
part && typeof part === 'object' && typeof (part as { text?: unknown }).text === 'string'
|
|
? (part as { text: string }).text
|
|
: ''
|
|
)
|
|
.join('')
|
|
}
|
|
return ''
|
|
}
|
|
|
|
function sourcesOf(message: AgentMessage): WebSearchSource[] | undefined {
|
|
const annotations = message.annotations
|
|
if (!annotations?.length) {
|
|
return undefined
|
|
}
|
|
return annotations.map((a) => ({ url: a.url, title: a.title }))
|
|
}
|
|
|
|
export function buildAgentTrace(messages: AgentMessage[]): AgentTraceEntry[] {
|
|
// The arguments live on the assistant message that requested the call, while
|
|
// the action tag and the result live on the `tool` message answering it, so
|
|
// the two are joined by `tool_call_id`.
|
|
const argsByCallId = new Map<string, string>()
|
|
for (const message of messages) {
|
|
for (const call of message.tool_calls ?? []) {
|
|
if (call.id && typeof call.function?.arguments === 'string') {
|
|
argsByCallId.set(call.id, call.function.arguments)
|
|
}
|
|
}
|
|
}
|
|
|
|
const entries: AgentTraceEntry[] = []
|
|
for (const message of messages) {
|
|
const action = message.agent_action
|
|
if (action?.type === 'tool_call') {
|
|
entries.push({
|
|
kind: 'tool',
|
|
name: action.function_name,
|
|
args: message.tool_call_id ? argsByCallId.get(message.tool_call_id) : undefined,
|
|
result: contentText(message.content),
|
|
jobId: action.job_id
|
|
})
|
|
continue
|
|
}
|
|
if (action?.type === 'mcp_tool_call') {
|
|
entries.push({
|
|
kind: 'tool',
|
|
name: action.function_name,
|
|
// An MCP call records its arguments on the action itself: it never
|
|
// became a job, so there is nowhere else for them to live.
|
|
args: action.arguments ? JSON.stringify(action.arguments, null, 2) : undefined,
|
|
result: contentText(message.content),
|
|
resourcePath: action.resource_path
|
|
})
|
|
continue
|
|
}
|
|
if (action?.type === 'web_search') {
|
|
entries.push({ kind: 'search' })
|
|
continue
|
|
}
|
|
if (action?.type === 'question') {
|
|
entries.push({
|
|
kind: 'tool',
|
|
name: 'Ask a question',
|
|
args: message.tool_call_id ? argsByCallId.get(message.tool_call_id) : undefined,
|
|
result: contentText(message.content)
|
|
})
|
|
continue
|
|
}
|
|
// Every message this run produced is tagged, including the agent narrating
|
|
// its next move and its final answer. Untagged ones are the prompt, the
|
|
// question, or a previous turn replayed out of memory — history loses its
|
|
// tags on the way back, and attributing it to this run would credit it with
|
|
// answers it never gave.
|
|
if (action?.type !== 'message') {
|
|
continue
|
|
}
|
|
const content = contentText(message.content)
|
|
// The worker writes a provider call's search right before that call's answer, and
|
|
// only a call that searched carries annotations.
|
|
const previous = entries[entries.length - 1]
|
|
if (message.role === 'assistant' && previous?.kind === 'search' && !previous.sources) {
|
|
previous.sources = sourcesOf(message)
|
|
}
|
|
if (message.role === 'assistant' && content !== '') {
|
|
entries.push({ kind: 'assistant', content })
|
|
}
|
|
}
|
|
return entries
|
|
}
|
|
|
|
/**
|
|
* Separates the turn that produced the output from the rest of the trace, so the
|
|
* answer is rendered once. Found by content and searched from the end: a run whose
|
|
* last turn returned a tool call leaves its answer mid-trace, where inspecting only
|
|
* the final entry prints it twice.
|
|
*/
|
|
export function splitFinalAnswer(entries: AgentTraceEntry[], output: unknown): AgentTraceEntry[] {
|
|
if (typeof output !== 'string') {
|
|
// A schema-shaped output is rendered by the result viewer itself and matches
|
|
// no turn, so the whole trace stands.
|
|
return entries
|
|
}
|
|
for (let i = entries.length - 1; i >= 0; i--) {
|
|
const entry = entries[i]
|
|
if (entry.kind === 'assistant' && entry.content === output) {
|
|
return [...entries.slice(0, i), ...entries.slice(i + 1)]
|
|
}
|
|
}
|
|
return entries
|
|
}
|