Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | 2x 2x 2x 1x 2x 1x 1x | /**
* Human in the loop — the times a run stopped for a person, and what they said.
*
* A run pauses three ways: an agent calls `ask_user` mid-task (`source:
* "tool"`), an agent that checks scope finds a request ambiguous before it
* starts (`source: "scope"`, the `__clarify__` node), or an approval gate
* holds work for a decision. The pause reaches the client three times over:
* as the `hitl_interrupt` frame that opens the form, as a `hitl_pause` trace
* frame (where it stopped and what it asked), and — once answered — a
* `hitl_resume` trace frame; a persisted turn also carries each exchange on
* `meta.hitl`. Mirrors svc-core's `app/services/hitl.py`.
*/
// ─── Questions ────────────────────────────────────────────────────────────────
export interface ClarifyOption {
value: string;
label: string;
description?: string | null;
/** The agent's suggested default. At most one on a radio question. */
recommended?: boolean;
}
/** `single`/`multi` choose from options; `text`/`number`/`date` are form fields. */
export type ClarifyQuestionKind = "single" | "multi" | "text" | "number" | "date";
export interface ClarifyQuestion {
id: string;
question: string;
kind: ClarifyQuestionKind;
options: ClarifyOption[];
allow_other: boolean;
required: boolean;
placeholder?: string | null;
/** For `text`: a paragraph rather than a phrase. */
multiline?: boolean;
}
export type ClarifySource = "tool" | "scope";
export interface ClarifyRequest {
thread_id: string;
title?: string | null;
intro?: string | null;
/** Why the agent is asking — set on a scope check. */
reason?: string | null;
source?: ClarifySource;
questions: ClarifyQuestion[];
}
/** Answers keyed by question id. Always a list, so single and multi match. */
export type ClarifyAnswers = Record<string, string[]>;
// ─── Pause, resume, exchange ──────────────────────────────────────────────────
export type HitlInterruptType = "clarification_request" | "approval_request";
export type HitlOutcome =
| "answered"
| "skipped"
| "approved"
| "rejected"
/** A policy rule refused the answer; the turn ended there. */
| "blocked"
/** Nobody answered — the chat closed. */
| "abandoned";
/** What the run showed the person when it stopped (the `hitl_pause` frame). */
export interface HitlPause {
interrupt_type: HitlInterruptType;
source?: ClarifySource;
title?: string | null;
intro?: string | null;
reason?: string | null;
questions?: ClarifyQuestion[];
/** approval_request */
task?: string;
output?: string;
paused_at?: string;
}
/** How the pause ended (the `hitl_resume` frame). */
export interface HitlResume {
outcome: HitlOutcome;
answers?: ClarifyAnswers;
approved?: boolean;
feedback?: string;
answered_at?: string | null;
answered_by?: string | null;
waited_ms?: number | null;
/** A policy rule filtered PII out of the answer before the agent saw it. */
redacted?: boolean;
}
/** One pause and its answer, as persisted on an assistant message's `meta.hitl`. */
export interface HitlExchange {
kind: "clarification" | "approval";
outcome: HitlOutcome;
node_id?: string | null;
paused_at?: string | null;
answered_at?: string | null;
answered_by?: string | null;
waited_ms?: number | null;
source?: ClarifySource;
title?: string | null;
intro?: string | null;
reason?: string | null;
questions?: ClarifyQuestion[];
answers?: ClarifyAnswers;
message?: string;
approved?: boolean;
feedback?: string;
redacted?: boolean;
blocked_by?: string;
reason_closed?: string;
}
/** A pause attached to the node execution that raised it. */
export interface NodeHitl {
pause: HitlPause;
resume?: HitlResume;
}
// ─── Helpers ──────────────────────────────────────────────────────────────────
/** An exchange as the pause/resume pair a trace view draws. */
export function exchangeToNodeHitl(exchange: HitlExchange): NodeHitl {
const clarification = exchange.kind === "clarification";
return {
pause: {
interrupt_type: clarification
? "clarification_request"
: "approval_request",
source: exchange.source,
title: exchange.title,
intro: exchange.intro,
reason: exchange.reason,
questions: exchange.questions,
task: exchange.message,
paused_at: exchange.paused_at ?? undefined,
},
resume: {
outcome: exchange.outcome,
answers: exchange.answers,
approved: exchange.approved,
feedback: exchange.feedback,
answered_at: exchange.answered_at,
answered_by: exchange.answered_by,
waited_ms: exchange.waited_ms,
redacted: exchange.redacted,
},
};
}
/** The label an answer value was chosen as, or the value itself (free text). */
export function answerLabel(question: ClarifyQuestion, value: string): string {
return question.options?.find((o) => o.value === value)?.label ?? value;
}
/** True when the person chose exactly the option(s) the agent recommended. */
export function tookRecommendation(
question: ClarifyQuestion,
values: string[] | undefined,
): boolean {
const recommended = (question.options ?? [])
.filter((o) => o.recommended)
.map((o) => o.value);
if (!values?.length || recommended.length === 0) return false;
return (
values.length === recommended.length &&
values.every((v) => recommended.includes(v))
);
}
|