web: approval cards for chat permission requests (C10)

Completes the item the pipeline could not land: the C10 diff its own
implementer and probe assertions already verified is committed as-is.
The resource block is a padding-free six-line clamp inside a padded frame
(the app's maxLines = 6 + ellipsis) and the primary control is filled.
This commit is contained in:
2026-10-19 11:08:53 +01:00
parent 27a1605127
commit 5eb13968e3
9 changed files with 1148 additions and 15 deletions
+174
View File
@@ -815,6 +815,15 @@ export function newChatThread(sessionId = "") {
toolStartedAt: {},
lastDeltaAt: 0,
queued: false,
// C10: the session's current permission request (the last `chat_permission`
// frame the server pushed) and the operator's local answer to it
// (`once`/`always`/`reject`, or blank). The approval card and its controls
// derive from the two (see `approvalCard`).
permission: null,
permissionAnswer: "",
// The reason a refused `chat_reply` left on the thread's error line, kept
// apart from any other error so a later accepted answer clears only this one.
permissionError: "",
};
}
@@ -1471,3 +1480,168 @@ export function chatCommandEffect(input, thread) {
return { kind: "report", text: `“/${command.name}” is not a command.` };
}
}
// ---------------------------------------------------------------------------
// Permission approvals (C10) — ui/chat/ChatFormat.kt ApprovalCard/approvalCard/
// permissionRiskText/decisionLabel and the ChatProtocol.DECISION_* values
// ---------------------------------------------------------------------------
/** The three decision values the server accepts (ChatProtocol.DECISION_*). */
export const DECISION_ONCE = "once";
export const DECISION_ALWAYS = "always";
export const DECISION_REJECT = "reject";
/** Every acceptable decision, in the Android client's own order. */
export const DECISIONS = Object.freeze([DECISION_ONCE, DECISION_ALWAYS, DECISION_REJECT]);
/** A decision's human label (ChatFormat.kt decisionLabel), blank-safe. */
export const DECISION_LABEL_ONCE = "Allowed once";
export const DECISION_LABEL_ALWAYS = "Always allowed";
export const DECISION_LABEL_REJECT = "Rejected";
export const DECISION_LABEL_RESOLVED = "Resolved";
export function decisionLabel(decision) {
switch (str(decision)) {
case DECISION_ONCE:
return DECISION_LABEL_ONCE;
case DECISION_ALWAYS:
return DECISION_LABEL_ALWAYS;
case DECISION_REJECT:
return DECISION_LABEL_REJECT;
default:
return DECISION_LABEL_RESOLVED;
}
}
/** How one approval request stands (ChatFormat.kt ApprovalState). */
export const APPROVAL_PENDING = "pending";
export const APPROVAL_ANSWERED = "answered";
export const APPROVAL_RESOLVED = "resolved";
/** The card's fixed wording (ChatFormat.kt ApprovalCard and ApprovalCardItem). */
export const APPROVAL_BLANK_ACTION = "tool call";
export const APPROVAL_HEADLINE_RESOLVED = "Already resolved";
export const APPROVAL_STATUS_PENDING = "Waiting for your response";
export const APPROVAL_STATUS_ANSWERED = "Waiting for the agent to continue…";
export const APPROVAL_STATUS_RESOLVED = "Another client already answered this request.";
export const RISK_REVIEW_LABEL = "RISK REVIEW";
export const WAITING_FOR_APPROVAL_LABEL = "Waiting for your approval";
/** The once/always/reject control labels (ChatApprovalCard.kt), in their order. */
export const APPROVAL_CONTROL_ONCE = "Allow once";
export const APPROVAL_CONTROL_ALWAYS = "Always allow";
export const APPROVAL_CONTROL_REJECT = "Reject";
/** How many resource lines the card shows before the remainder is elided. */
export const MAX_APPROVAL_RESOURCE_LINES = 6;
/**
* The text in the amber risk-review box (ChatFormat.kt permissionRiskText): the
* server's own summary when it carries one, otherwise a neutral sentence naming
* the action (or `a tool call`) so the box is never blank.
*/
export function permissionRiskText(permission) {
const message = str(permission?.message);
if (!isBlank(message)) return message;
const what = isBlank(permission?.action) ? "a tool call" : str(permission.action);
return `The agent is asking to run ${what}; review the request before allowing.`;
}
/**
* The request's resources with the blanks dropped, in the server's own order
* (ChatFormat.kt ApprovalCard.resources). No entry is discarded here: the line
* cap belongs to the joined text, so an embedded newline counts as its own line
* instead of hiding a later resource.
*/
export function approvalResources(permission) {
return (permission?.resources ?? [])
.filter((resource) => !isBlank(resource))
.map((resource) => str(resource));
}
/**
* The resource block as the card renders it (ChatFormat.kt
* `resources.joinToString("\n")` with `maxLines = 6, overflow = Ellipsis`): the
* non-blank resources joined by newlines and capped at
* [MAX_APPROVAL_RESOURCE_LINES] lines, an embedded newline counting as a line,
* with an ellipsis on the last kept line when the rest was dropped. The rendered
* box clamps to the same six lines (styles.css), so a single long resource that
* wraps is elided too rather than spilling onto a seventh line.
*/
export function approvalResourceText(permission) {
const lines = approvalResources(permission).flatMap((resource) => resource.split("\n"));
if (lines.length <= MAX_APPROVAL_RESOURCE_LINES) return lines.join("\n");
const kept = lines.slice(0, MAX_APPROVAL_RESOURCE_LINES);
kept[kept.length - 1] = `${kept[kept.length - 1]}\u2026`;
return kept.join("\n");
}
/** The card headline (ChatFormat.kt ApprovalCard.headline). */
export function approvalHeadline(state, decision, action) {
if (state === APPROVAL_PENDING) {
return `Approve ${isBlank(action) ? APPROVAL_BLANK_ACTION : str(action)}`;
}
if (state === APPROVAL_ANSWERED) return decisionLabel(decision);
return isBlank(decision) ? APPROVAL_HEADLINE_RESOLVED : decisionLabel(decision);
}
/** The card's one-line status (ChatFormat.kt ApprovalCard.statusLine). */
export function approvalStatusLine(state, note) {
if (state === APPROVAL_PENDING) return APPROVAL_STATUS_PENDING;
if (state === APPROVAL_ANSWERED) return APPROVAL_STATUS_ANSWERED;
return isBlank(note) ? APPROVAL_STATUS_RESOLVED : str(note);
}
/**
* What the conversation renders for one permission request (ChatFormat.kt
* approvalCard): derived purely from the server's [permission] frame plus the
* operator's local [answer], so a server resolution always outranks a local
* answer and a stale tap can never reopen controls for a request already done.
* Returns null when there is no request to show (a null frame or a blank
* request id). `actionable` is true only while the request is open; `showRisk`
* mirrors the app (open, or answered with a rejection).
*/
export function approvalCard(permission, answer) {
if (permission == null || isBlank(permission.request_id)) return null;
const resolvedByServer = permission.resolved === true || !isBlank(permission.decision);
const state = resolvedByServer
? APPROVAL_RESOLVED
: !isBlank(answer)
? APPROVAL_ANSWERED
: APPROVAL_PENDING;
const decision = resolvedByServer ? str(permission.decision) : str(answer);
const note = resolvedByServer ? str(permission.message) : "";
return {
requestId: str(permission.request_id),
action: str(permission.action),
resources: approvalResources(permission),
resourceText: approvalResourceText(permission),
risk: permissionRiskText(permission),
state,
decision,
note,
actionable: state === APPROVAL_PENDING,
showRisk:
state === APPROVAL_PENDING || (state === APPROVAL_ANSWERED && decision === DECISION_REJECT),
headline: approvalHeadline(state, decision, str(permission.action)),
statusLine: approvalStatusLine(state, note),
};
}
/**
* Folds one pushed `chat_permission` frame into [thread] (ChatStore.permission):
* a frame for a *new* request replaces any settled card and clears the local
* answer, while a frame for the *same* request (for example a resolved re-push)
* keeps the answer the operator already gave. The frame itself is stored; the
* card derives from it, so a server resolution always wins.
*/
export function applyChatPermission(thread, frame) {
const current = thread ?? newChatThread(str(frame?.session_id));
const requestId = str(frame?.request_id);
const sameRequest = requestId !== "" && str(current.permission?.request_id) === requestId;
return {
...current,
permission: frame ?? null,
permissionAnswer: sameRequest ? str(current.permissionAnswer) : "",
};
}