feat: agents ask the user for a secret through a private card (#15907)
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
1 parent
25d5c7cacb
commit
9f61ba6741
49 files changed
+2250
-27
No files matched your search
@@ -740,6 +740,8 @@ function eventPathContainsSelector(event: Event, selector: string): boolean {
|
||||
return path.some((target) => target instanceof Element && target.closest(selector));
|
||||
}
|
||||
|
||||
const SECRET_REQUEST_SELECTOR = '[data-v2-item-type="secret_request"]';
|
||||
|
||||
/**
|
||||
* Whether input that landed outside any editable or interactive element
|
||||
* should be redirected into the composer. Shared by type-to-focus and
|
||||
@@ -747,6 +749,9 @@ function eventPathContainsSelector(event: Event, selector: string): boolean {
|
||||
*/
|
||||
function shouldRedirectInputToComposer(event: Event): boolean {
|
||||
if (event.defaultPrevented) return false;
|
||||
// Near a pending secret request, input is meant for its private field: it
|
||||
// must never land in the composer draft, which is persisted and sent.
|
||||
if (eventPathContainsSelector(event, SECRET_REQUEST_SELECTOR)) return false;
|
||||
if (eventPathContainsSelector(event, TYPE_TO_FOCUS_EDITABLE_SELECTOR)) return false;
|
||||
if (eventPathContainsSelector(event, TYPE_TO_FOCUS_INTERACTIVE_SELECTOR)) return false;
|
||||
if (document.querySelector(TYPE_TO_FOCUS_FLOATING_LAYER_SELECTOR)) return false;
|
||||
|
||||
@@ -276,6 +276,7 @@ import {
|
||||
V2LifecycleRow,
|
||||
type HandoffTimelineRun,
|
||||
} from "./V2LifecycleRow";
|
||||
import { SecretRequestCard } from "./SecretRequestCard";
|
||||
import { TimelineSystemDivider } from "./TimelineSystemDivider";
|
||||
|
||||
import { SkillChipIcon, SkillInlineText } from "./SkillInlineText";
|
||||
@@ -2808,6 +2809,15 @@ function V2EventTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "event"
|
||||
if (item.type === "subagent" && (row.subagents?.length ?? 1) > 1) {
|
||||
return <V2SubagentGroup key={row.id} row={row} />;
|
||||
}
|
||||
if (item.type === "secret_request") {
|
||||
return (
|
||||
<SecretRequestCard
|
||||
environmentId={ctx.activeThreadEnvironmentId}
|
||||
item={item}
|
||||
visibility={visibility}
|
||||
/>
|
||||
);
|
||||
}
|
||||
if (isV2LifecycleItem(item)) {
|
||||
return (
|
||||
<V2LifecycleRow
|
||||
|
||||
@@ -0,0 +1,179 @@
|
||||
import {
|
||||
SECRET_REQUEST_DEFAULT_PLACEHOLDER,
|
||||
SECRET_REQUEST_PRIVACY_NOTE,
|
||||
secretRequestAnswerInput,
|
||||
secretRequestDisplay,
|
||||
secretRequestFailureMessage,
|
||||
type SecretRequestItem,
|
||||
} from "@t3tools/client-runtime/secret-request";
|
||||
import {
|
||||
isAtomCommandInterrupted,
|
||||
squashAtomCommandFailure,
|
||||
} from "@t3tools/client-runtime/state/runtime";
|
||||
import type { EnvironmentId, OrchestrationV2ProjectedTurnItem } from "@t3tools/contracts";
|
||||
import { CheckIcon, LockIcon, MinusIcon, ShieldCheckIcon } from "lucide-react";
|
||||
import { useId, useRef, useState, type FormEvent } from "react";
|
||||
|
||||
import { serverEnvironment } from "../../state/server";
|
||||
import { useAtomCommand } from "../../state/use-atom-command";
|
||||
import { Button } from "../ui/button";
|
||||
import { Input } from "../ui/input";
|
||||
import { WorkLogRow } from "./WorkLog";
|
||||
|
||||
/**
|
||||
* Inline card for a secret an agent asked the user for. The typed value lives
|
||||
* only in this component's state and the RPC payload: it is never logged,
|
||||
* toasted, or persisted, and the field clears once the answer is sent.
|
||||
*/
|
||||
export function SecretRequestCard(props: {
|
||||
readonly environmentId: EnvironmentId;
|
||||
readonly item: SecretRequestItem;
|
||||
readonly visibility: OrchestrationV2ProjectedTurnItem["visibility"];
|
||||
}) {
|
||||
const { item } = props;
|
||||
const display = secretRequestDisplay(item, props.visibility);
|
||||
if (display.kind === "answered" || display.kind === "pending-elsewhere") {
|
||||
const Icon =
|
||||
display.kind === "pending-elsewhere"
|
||||
? LockIcon
|
||||
: display.outcome === "saved"
|
||||
? CheckIcon
|
||||
: MinusIcon;
|
||||
return (
|
||||
<WorkLogRow
|
||||
data-v2-item-type={item.type}
|
||||
icon={<Icon className="size-3.5 text-icon-muted" aria-hidden />}
|
||||
label={
|
||||
<>
|
||||
{item.label} · {display.label}
|
||||
</>
|
||||
}
|
||||
/>
|
||||
);
|
||||
}
|
||||
return <PendingSecretRequestForm environmentId={props.environmentId} item={item} />;
|
||||
}
|
||||
|
||||
function PendingSecretRequestForm(props: {
|
||||
readonly environmentId: EnvironmentId;
|
||||
readonly item: SecretRequestItem;
|
||||
}) {
|
||||
const { item } = props;
|
||||
const inputId = useId();
|
||||
const errorId = useId();
|
||||
const privacyId = useId();
|
||||
const answer = useAtomCommand(serverEnvironment.answerSecretRequest, {
|
||||
label: "answer secret request",
|
||||
// The failure cause holds the request; keep it out of the console.
|
||||
reportFailure: false,
|
||||
reportDefect: false,
|
||||
});
|
||||
const [secret, setSecret] = useState("");
|
||||
const [submitting, setSubmitting] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
// Enter then a click can both run before a re-render; this guard is synchronous.
|
||||
const inFlight = useRef(false);
|
||||
|
||||
const send = async (
|
||||
reply: { readonly type: "save"; readonly secret: string } | { readonly type: "decline" },
|
||||
) => {
|
||||
const input = secretRequestAnswerInput(item, reply);
|
||||
if (input === null || inFlight.current) return;
|
||||
inFlight.current = true;
|
||||
setSubmitting(true);
|
||||
setError(null);
|
||||
const result = await answer({ environmentId: props.environmentId, input }).finally(() => {
|
||||
inFlight.current = false;
|
||||
setSubmitting(false);
|
||||
});
|
||||
if (result._tag === "Success") {
|
||||
// The card switches to its answered row once the item updates.
|
||||
setSecret("");
|
||||
return;
|
||||
}
|
||||
if (!isAtomCommandInterrupted(result)) {
|
||||
setError(secretRequestFailureMessage(squashAtomCommandFailure(result)));
|
||||
}
|
||||
};
|
||||
|
||||
const onSubmit = (event: FormEvent<HTMLFormElement>) => {
|
||||
event.preventDefault();
|
||||
void send({ type: "save", secret });
|
||||
};
|
||||
|
||||
// Same hierarchy as a chat card: what is asked, why, the field, then the
|
||||
// promise about where the value goes.
|
||||
return (
|
||||
<form
|
||||
data-v2-item-type={item.type}
|
||||
className="flex min-w-0 flex-col gap-3 rounded-xl border border-border/60 bg-card p-4"
|
||||
onSubmit={onSubmit}
|
||||
autoComplete="off"
|
||||
// A click anywhere on the card targets its field, so a paste that
|
||||
// follows lands here rather than with the composer.
|
||||
onPointerDown={(event) => {
|
||||
if (!(event.target instanceof HTMLElement)) return;
|
||||
if (event.target.closest("input, button, a")) return;
|
||||
document.getElementById(inputId)?.focus();
|
||||
}}
|
||||
>
|
||||
<div className="flex min-w-0 flex-col gap-1">
|
||||
<label htmlFor={inputId} className="text-sm font-medium text-foreground">
|
||||
{item.label}
|
||||
</label>
|
||||
{item.reason.trim() ? <p className="text-sm text-muted-foreground">{item.reason}</p> : null}
|
||||
</div>
|
||||
<div className="flex min-w-0 items-center gap-2">
|
||||
<div className="min-w-0 flex-1">
|
||||
<Input
|
||||
id={inputId}
|
||||
// Masked text rather than a password field: browsers offer to
|
||||
// save any submitted password, and this is not a login.
|
||||
type="text"
|
||||
className="[-webkit-text-security:disc]"
|
||||
autoComplete="off"
|
||||
autoCorrect="off"
|
||||
autoCapitalize="off"
|
||||
spellCheck={false}
|
||||
// Password managers otherwise offer to save or fill this field.
|
||||
data-1p-ignore
|
||||
data-lpignore="true"
|
||||
data-bwignore
|
||||
placeholder={item.placeholder ?? SECRET_REQUEST_DEFAULT_PLACEHOLDER}
|
||||
value={secret}
|
||||
disabled={submitting}
|
||||
aria-invalid={error !== null || undefined}
|
||||
aria-describedby={error !== null ? `${privacyId} ${errorId}` : privacyId}
|
||||
onChange={(event) => setSecret(event.currentTarget.value)}
|
||||
/>
|
||||
</div>
|
||||
<Button type="submit" disabled={submitting || secret.trim().length === 0}>
|
||||
Save securely
|
||||
</Button>
|
||||
</div>
|
||||
{error !== null ? (
|
||||
<p id={errorId} role="alert" className="text-xs text-destructive">
|
||||
{error}
|
||||
</p>
|
||||
) : null}
|
||||
<div className="flex min-w-0 items-center justify-between gap-2">
|
||||
<p
|
||||
id={privacyId}
|
||||
className="flex min-w-0 items-center gap-1.5 text-xs text-muted-foreground"
|
||||
>
|
||||
<ShieldCheckIcon className="size-3.5 shrink-0" aria-hidden />
|
||||
{SECRET_REQUEST_PRIVACY_NOTE}
|
||||
</p>
|
||||
<Button
|
||||
type="button"
|
||||
size="xs"
|
||||
variant="ghost-muted"
|
||||
disabled={submitting}
|
||||
onClick={() => void send({ type: "decline" })}
|
||||
>
|
||||
Decline
|
||||
</Button>
|
||||
</div>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
@@ -315,12 +315,15 @@ const STANDALONE_V2_ITEM_TYPES = new Set<OrchestrationV2ProjectedTurnItem["item"
|
||||
"handoff",
|
||||
"run_interrupt_request",
|
||||
"run_interrupt_result",
|
||||
"secret_request",
|
||||
"subagent",
|
||||
]);
|
||||
|
||||
const PERSISTENT_RESOURCE_V2_ITEM_TYPES = new Set<OrchestrationV2TurnItem["type"]>([
|
||||
"fork",
|
||||
"thread_created",
|
||||
// Still answerable after a steer supersedes the attempt that asked.
|
||||
"secret_request",
|
||||
]);
|
||||
|
||||
export function timelineEntryIsPersistentResourceCard(entry: TimelineEntry): boolean {
|
||||
|
||||
Reference in new issue
Block a user