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:
+199
-6
@@ -233,7 +233,17 @@ import {
|
||||
LOAD_OLDER_ACTION,
|
||||
REOPEN_ACTION,
|
||||
SELECT_CHAT_HINT,
|
||||
APPROVAL_CONTROL_ALWAYS,
|
||||
APPROVAL_CONTROL_ONCE,
|
||||
APPROVAL_CONTROL_REJECT,
|
||||
DECISION_ALWAYS,
|
||||
DECISION_ONCE,
|
||||
DECISION_REJECT,
|
||||
RISK_REVIEW_LABEL,
|
||||
WAITING_FOR_APPROVAL_LABEL,
|
||||
applyChatEvent,
|
||||
applyChatPermission,
|
||||
approvalCard,
|
||||
attachmentDisplayName,
|
||||
attachmentError,
|
||||
attachmentFiles,
|
||||
@@ -3659,6 +3669,64 @@ function onChatEvent(frame) {
|
||||
renderContent();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fold one pushed `chat_permission` frame into its conversation (C10). Only a
|
||||
* conversation this client has open is folded — the server only pushes a
|
||||
* permission frame for sessions this connection is subscribed to, but the guard
|
||||
* keeps a frame for a session nobody opened from creating a thread. The card is
|
||||
* derived from the stored frame plus the local answer, so a resolved re-push
|
||||
* always clears the controls.
|
||||
*/
|
||||
function onChatPermission(frame) {
|
||||
const sessionId = String(frame?.session_id ?? "").trim();
|
||||
if (!sessionId || !state.chatThreads.has(sessionId)) return;
|
||||
const thread = state.chatThreads.get(sessionId);
|
||||
const next = applyChatPermission(thread, frame);
|
||||
// A different request replaces the card, so the previous refusal's reason (and
|
||||
// the error line it owns) no longer describes what is on screen.
|
||||
const replaced =
|
||||
String(thread?.permission?.request_id ?? "") !== String(next.permission?.request_id ?? "");
|
||||
if (replaced && thread?.permissionError) {
|
||||
next.error = thread.error === thread.permissionError ? "" : next.error;
|
||||
next.permissionError = "";
|
||||
}
|
||||
state.chatThreads.set(sessionId, next);
|
||||
if (sessionId === state.chatActiveId) renderContent();
|
||||
}
|
||||
|
||||
/**
|
||||
* Answer the pending permission request (C10): settle the matching card
|
||||
* optimistically so the buttons are never left offering a decision already made,
|
||||
* then send `chat_reply`. A refused reply reopens the same request (the local
|
||||
* answer is cleared) and surfaces the server's reason through the thread's own
|
||||
* error line, so an unaccepted decision is never shown as accepted. A refusal
|
||||
* that arrives after a different request replaced the card is ignored.
|
||||
*/
|
||||
async function answerPermission(sessionId, requestId, decision) {
|
||||
const thread = state.chatThreads.get(sessionId);
|
||||
const permission = thread?.permission;
|
||||
if (!thread || String(permission?.request_id ?? "") !== requestId) return;
|
||||
setThread(sessionId, { permissionAnswer: decision });
|
||||
renderContent();
|
||||
try {
|
||||
await repo.chatReply(sessionId, requestId, decision);
|
||||
// A retried answer the server accepted clears the reason a previous refusal
|
||||
// left on the thread's error line — and only that reason, never an unrelated
|
||||
// error the conversation is carrying.
|
||||
const settled = state.chatThreads.get(sessionId);
|
||||
if (settled?.permissionError && settled.error === settled.permissionError) {
|
||||
setThread(sessionId, { error: "", permissionError: "" });
|
||||
renderContent();
|
||||
}
|
||||
} catch (error) {
|
||||
const current = state.chatThreads.get(sessionId);
|
||||
if (!current || String(current.permission?.request_id ?? "") !== requestId) return;
|
||||
const reason = describeError(error);
|
||||
setThread(sessionId, { permissionAnswer: "", error: reason, permissionError: reason });
|
||||
renderContent();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The reconnect repair (C5): re-attach the open chat's stream and replace its
|
||||
* conversation with the newest history page wholesale. Everything the gap could
|
||||
@@ -4362,6 +4430,11 @@ function renderChatConversation() {
|
||||
const session = state.chatSessions.find((candidate) => candidate.id === state.chatActiveId) ?? null;
|
||||
const thread = state.chatThreads.get(state.chatActiveId) ?? null;
|
||||
const title = session ? sessionTitle(session) : state.chatActiveId.slice(0, 18);
|
||||
// The pending approval card (C10), derived from the stored frame plus the local
|
||||
// answer. While it is actionable the top bar offers a jump control and the
|
||||
// running indicator stands down (a turn blocked on a decision is not generating).
|
||||
const approval = approvalCard(thread?.permission, thread?.permissionAnswer);
|
||||
const awaiting = approval?.actionable === true;
|
||||
|
||||
const bar = h("div", { class: "chat-bar" });
|
||||
const titles = h("div", { class: "chat-bar-titles" });
|
||||
@@ -4384,6 +4457,10 @@ function renderChatConversation() {
|
||||
}),
|
||||
);
|
||||
}
|
||||
// The approval jump control appears only while a request is actionable, and
|
||||
// brings its card (the transcript's last item) into view — the one scroll path,
|
||||
// so a pending request is reached the same way new content is.
|
||||
if (awaiting) bar.append(approvalJumpButton());
|
||||
pane.append(bar);
|
||||
|
||||
// The context line: the kind chip, the activity word and directory · model.
|
||||
@@ -4427,11 +4504,16 @@ function renderChatConversation() {
|
||||
pane.append(empty);
|
||||
} else {
|
||||
const rows = transcriptRows(thread?.messages ?? []);
|
||||
const running = thread?.running === true;
|
||||
if (rows.length === 0 && notices.length === 0 && !running) {
|
||||
// The running indicator stands down while an approval is pending (C10): a turn
|
||||
// blocked on a decision is not generating, so neither the spinner nor its clock
|
||||
// shows. The composer's Stop control stays governed by thread.running.
|
||||
const running = thread?.running === true && !awaiting;
|
||||
if (rows.length === 0 && notices.length === 0 && !running && !approval) {
|
||||
pane.append(h("div", { class: "chat-transcript-empty muted", text: CONVERSATION_EMPTY }));
|
||||
} else {
|
||||
pane.append(renderChatTranscript(state.chatActiveId, rows, { notices, running, now: Date.now() }));
|
||||
pane.append(
|
||||
renderChatTranscript(state.chatActiveId, rows, { notices, running, now: Date.now(), approval }),
|
||||
);
|
||||
}
|
||||
if (thread?.error && thread?.loaded) {
|
||||
pane.append(h("div", { class: "chat-transcript-error", text: thread.error }));
|
||||
@@ -4463,7 +4545,11 @@ function appendChatDialogs(pane) {
|
||||
* in order as the reasoning block, then the tool-call chip, then the answer
|
||||
* markdown. No timestamps, model or agent are shown inside the transcript.
|
||||
*/
|
||||
function renderChatTranscript(sessionId, rows, { notices = [], running = false, now = Date.now() } = {}) {
|
||||
function renderChatTranscript(
|
||||
sessionId,
|
||||
rows,
|
||||
{ notices = [], running = false, now = Date.now(), approval = null } = {},
|
||||
) {
|
||||
const wrap = h("div", { class: "chat-transcript-wrap" });
|
||||
const transcript = h("div", {
|
||||
class: "chat-transcript",
|
||||
@@ -4490,6 +4576,10 @@ function renderChatTranscript(sessionId, rows, { notices = [], running = false,
|
||||
// The conversation's local command lines sit after the turns, full width and
|
||||
// deliberately unlike a message bubble (see ChatCommandNoticeItem).
|
||||
for (const notice of notices) transcript.append(renderChatCommandNotice(sessionId, notice));
|
||||
// The approval card is the transcript's last item (C10) — after the notices and
|
||||
// after the running indicator when it is shown — so the top bar's jump control
|
||||
// and a fresh card both land at the end.
|
||||
if (approval) transcript.append(renderChatApproval(sessionId, approval));
|
||||
wrap.append(transcript);
|
||||
wrap.append(
|
||||
h("button", {
|
||||
@@ -4509,6 +4599,102 @@ function renderChatTranscript(sessionId, rows, { notices = [], running = false,
|
||||
return wrap;
|
||||
}
|
||||
|
||||
/** The top bar's approval jump control (C10), naming the pending request. */
|
||||
function approvalJumpButton() {
|
||||
return h("button", {
|
||||
class: "ghost chat-approval-jump",
|
||||
type: "button",
|
||||
text: WAITING_FOR_APPROVAL_LABEL,
|
||||
onclick: () => {
|
||||
// The card is the transcript's last item, so the transcript's own end is the
|
||||
// card; the one scroll path shared with jump-to-latest.
|
||||
const el = document.getElementById("chatTranscript");
|
||||
if (!el) return;
|
||||
state.chatFollowing = true;
|
||||
el.scrollTop = el.scrollHeight;
|
||||
updateChatJump(el);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* One approval request as a card (C10), the browser's port of
|
||||
* `ui/chat/ChatApprovalCard.kt`: a shield and the headline, the status line, the
|
||||
* request's resources newline-joined in monospace (blank entries dropped, at most
|
||||
* six lines), the amber `RISK REVIEW` panel only while the app shows it, and the
|
||||
* once/always/reject controls only while the request is actionable. Every server
|
||||
* string is set with textContent and every node is built by hand.
|
||||
*/
|
||||
function renderChatApproval(sessionId, card) {
|
||||
const node = h("div", {
|
||||
class: `chat-approval${card.actionable ? " chat-approval-pending" : ""}`,
|
||||
id: "chatApproval",
|
||||
"data-request-id": card.requestId,
|
||||
});
|
||||
const head = h("div", { class: "chat-approval-head" });
|
||||
head.append(shieldIcon("chat-approval-shield"));
|
||||
const titles = h("div", { class: "chat-approval-titles" });
|
||||
titles.append(h("div", { class: "chat-approval-headline", text: card.headline }));
|
||||
titles.append(h("div", { class: "chat-approval-status muted", text: card.statusLine }));
|
||||
head.append(titles);
|
||||
node.append(head);
|
||||
if (card.resourceText !== "") {
|
||||
// The padded frame around a padding-free clamp block: clamping the padded
|
||||
// element itself lets the seventh line paint into its bottom padding.
|
||||
const box = h("div", { class: "chat-approval-resources" });
|
||||
box.append(h("pre", { class: "chat-approval-resources-text", text: card.resourceText }));
|
||||
node.append(box);
|
||||
}
|
||||
if (card.showRisk && card.risk.trim() !== "") {
|
||||
const risk = h("div", { class: "chat-approval-risk" });
|
||||
const riskHead = h("div", { class: "chat-approval-risk-head" });
|
||||
riskHead.append(shieldIcon("chat-approval-risk-shield"));
|
||||
riskHead.append(h("span", { class: "chat-approval-risk-label", text: RISK_REVIEW_LABEL }));
|
||||
risk.append(riskHead);
|
||||
risk.append(h("div", { class: "chat-approval-risk-text", text: card.risk }));
|
||||
node.append(risk);
|
||||
}
|
||||
if (card.actionable) {
|
||||
const controls = h("div", { class: "chat-approval-controls" });
|
||||
controls.append(approvalControl(APPROVAL_CONTROL_ONCE, DECISION_ONCE, sessionId, card.requestId, "primary"));
|
||||
controls.append(approvalControl(APPROVAL_CONTROL_ALWAYS, DECISION_ALWAYS, sessionId, card.requestId));
|
||||
controls.append(approvalControl(APPROVAL_CONTROL_REJECT, DECISION_REJECT, sessionId, card.requestId));
|
||||
node.append(controls);
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
/**
|
||||
* The app's amber outlined Shield (`Icons.Outlined.Shield`): an inline SVG stroked
|
||||
* with `currentColor` so the palette's warn hue actually tints it, unlike the
|
||||
* colour emoji it replaces. Decorative — the headline carries the meaning.
|
||||
*/
|
||||
function shieldIcon(className) {
|
||||
const NS = "http://www.w3.org/2000/svg";
|
||||
const svg = document.createElementNS(NS, "svg");
|
||||
svg.setAttribute("class", className);
|
||||
svg.setAttribute("viewBox", "0 0 24 24");
|
||||
svg.setAttribute("aria-hidden", "true");
|
||||
svg.setAttribute("focusable", "false");
|
||||
const path = document.createElementNS(NS, "path");
|
||||
path.setAttribute("d", "M12 3 5 6v5.5c0 4.2 2.8 8.1 7 9.5 4.2-1.4 7-5.3 7-9.5V6l-7-3z");
|
||||
path.setAttribute("fill", "none");
|
||||
path.setAttribute("stroke", "currentColor");
|
||||
path.setAttribute("stroke-width", "1.8");
|
||||
path.setAttribute("stroke-linejoin", "round");
|
||||
svg.append(path);
|
||||
return svg;
|
||||
}
|
||||
|
||||
function approvalControl(label, decision, sessionId, requestId, extraClass = "") {
|
||||
return h("button", {
|
||||
class: `chat-approval-control${extraClass ? ` ${extraClass}` : ""}`,
|
||||
type: "button",
|
||||
text: label,
|
||||
onclick: () => answerPermission(sessionId, requestId, decision),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* The in-flight turn's indicator: a spinner, the phase headline (`Thinking…` /
|
||||
* `Running a tool…` / `Waiting…`), `· {elapsed}`, then one line per running tool
|
||||
@@ -6009,7 +6195,8 @@ function refreshChatClock() {
|
||||
stopChatClock();
|
||||
chatNow = Date.now();
|
||||
const thread = state.chatActiveId ? state.chatThreads.get(state.chatActiveId) : null;
|
||||
if (state.view === "chat" && thread?.running) {
|
||||
const awaiting = approvalCard(thread?.permission, thread?.permissionAnswer)?.actionable === true;
|
||||
if (state.view === "chat" && thread?.running && !awaiting) {
|
||||
chatClock = setInterval(() => {
|
||||
chatNow = Date.now();
|
||||
updateChatIndicator(chatNow);
|
||||
@@ -6086,7 +6273,13 @@ client.addEventListener("state", (event) => {
|
||||
// type is ignored.
|
||||
client.addEventListener("frame", (event) => {
|
||||
const message = event.detail;
|
||||
if (!message || message.type !== Protocol.CHAT_EVENT) return;
|
||||
if (!message) return;
|
||||
if (message.type === Protocol.CHAT_PERMISSION) {
|
||||
const permission = message[ChatKeys.PERMISSION];
|
||||
if (permission) onChatPermission(permission);
|
||||
return;
|
||||
}
|
||||
if (message.type !== Protocol.CHAT_EVENT) return;
|
||||
const frame = message[ChatKeys.EVENT];
|
||||
if (frame) onChatEvent(frame);
|
||||
onChatFrame(message);
|
||||
|
||||
+174
@@ -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) : "",
|
||||
};
|
||||
}
|
||||
|
||||
@@ -64,6 +64,11 @@ export const Protocol = Object.freeze({
|
||||
CHAT_PROMPT: "chat_prompt",
|
||||
CHAT_INTERRUPT: "chat_interrupt",
|
||||
CHAT_CREATE: "chat_create",
|
||||
// The approval card (C10): the server pushes `chat_permission` frames (one
|
||||
// pending request, later re-pushed with the resolution) and the phone answers
|
||||
// with `chat_reply` (internal/protocol/chat.go ChatPermission/ChatReply).
|
||||
CHAT_PERMISSION: "chat_permission",
|
||||
CHAT_REPLY: "chat_reply",
|
||||
|
||||
SUCCESS_END: "#success",
|
||||
FAILURE_END: "#failure",
|
||||
@@ -92,6 +97,10 @@ export const ChatKeys = Object.freeze({
|
||||
PROMPT: "chat_prompt",
|
||||
CREATE: "chat_create",
|
||||
QUEUED: "chat_queued",
|
||||
// C10: the pushed approval frame (`chat_permission`) and the answer body
|
||||
// (`chat_reply`), both distinct Message fields in internal/protocol/message.go.
|
||||
PERMISSION: "chat_permission",
|
||||
REPLY: "chat_reply",
|
||||
});
|
||||
|
||||
/**
|
||||
|
||||
@@ -3,10 +3,18 @@
|
||||
// divergent field names. Mirrors android/.../data/PipelinesRepository.kt.
|
||||
import { ChatKeys, Protocol } from "./protocol.js";
|
||||
import { ProtocolError } from "./client.js";
|
||||
import { DECISIONS } from "./chat.js";
|
||||
|
||||
/** The server default history page the app asks for (CHAT_HISTORY_LIMIT, 200). */
|
||||
export const CHAT_HISTORY_LIMIT = 200;
|
||||
|
||||
/**
|
||||
* The label this browser client records with a permission answer, so every other
|
||||
* watching client can see which device resolved the request (the Android analogue
|
||||
* is the device name). It is always sent; the server records it with the outcome.
|
||||
*/
|
||||
export const WEB_CHAT_CLIENT = "Web";
|
||||
|
||||
export class PipelinesRepository {
|
||||
#client;
|
||||
|
||||
@@ -334,6 +342,37 @@ export class PipelinesRepository {
|
||||
}
|
||||
return session;
|
||||
}
|
||||
|
||||
/**
|
||||
* Answers one pending permission request (C10). Sends exactly
|
||||
* `{type:'chat_reply', chat_reply:{session_id,request_id,decision,client}}` —
|
||||
* `message` rides only when non-blank (mirrors ChatReplyRequest's omitempty).
|
||||
* A blank session/request/decision, or a decision outside `once`/`always`/
|
||||
* `reject`, is refused here before anything is sent, so an unaccepted decision
|
||||
* can never reach the server. The server pushes the shared resolution back to
|
||||
* every watching client once the first answer is recorded.
|
||||
*/
|
||||
async chatReply(sessionId, requestId, decision, { message = "", client = WEB_CHAT_CLIENT } = {}) {
|
||||
const session = String(sessionId ?? "").trim();
|
||||
const request = String(requestId ?? "").trim();
|
||||
const chosen = String(decision ?? "").trim();
|
||||
if (session === "") throw new ProtocolError("Select a chat session first.");
|
||||
if (request === "") {
|
||||
throw new ProtocolError("This approval request is no longer available.");
|
||||
}
|
||||
if (!DECISIONS.includes(chosen)) {
|
||||
throw new ProtocolError("Choose once, always or reject.");
|
||||
}
|
||||
const body = {
|
||||
session_id: session,
|
||||
request_id: request,
|
||||
decision: chosen,
|
||||
client: String(client ?? "").trim() || WEB_CHAT_CLIENT,
|
||||
};
|
||||
const trimmed = String(message ?? "").trim();
|
||||
if (trimmed !== "") body.message = trimmed;
|
||||
await this.#client.call({ type: Protocol.CHAT_REPLY, [ChatKeys.REPLY]: body });
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user