Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/chat-store-slices.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@schlessera/brain-ui-react": patch
---

The chat store is now built from separate slices for sessions, session status, messages, streaming, approvals, ask-user questions and transcript replay. It behaves as before: the store's state, actions, hooks and selectors are unchanged.
2 changes: 1 addition & 1 deletion docs/decisions/answer-delivery.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ manufacture an identity, and it was not needed.
## One card per request

The store updates a request it already holds and never appends it again
(`addExchange`, `packages/ui-react/src/stores/chat-state.ts:270-301`). A
(`addExchange`, `packages/ui-react/src/stores/chat-slices/ask-user.ts:32-63`). A
settled card stays settled, so a late or replayed frame cannot revive a
dismissed or answered question. A pending card keeps its payload and the
exchange object it is keyed by, so a draft in progress survives a re-send.
Expand Down
2 changes: 1 addition & 1 deletion docs/investigations/live-conversation-investigation.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ is unknown, not queued against a replacement request.
Terminal state invalidates every pending exchange for that turn without waiting
for `tool_result`. Current client terminal handling finishes the assistant
message and resyncs; its message-state helper only marks streaming false
(`finishAssistantMessage`, `packages/ui-react/src/stores/chat-state.ts:1835-1840`).
(`finishAssistantMessage`, `packages/ui-react/src/stores/chat-slices/streaming.ts:27-32`).
This observation is a required integration check, not proof that all cards
currently clear. A host terminal event plus local audio drain/discard defines
conversation completion; provider turn/audio end does not.
Expand Down
6 changes: 3 additions & 3 deletions docs/plans/navigation-recovery.md
Original file line number Diff line number Diff line change
Expand Up @@ -365,11 +365,11 @@ implementation. Any PR moving these lines owns their citation repairs.

- New chat: (`{hasMessages && !wide && (`, `packages/ui-react/src/components/chat/chat-page.tsx:702-709`).

- Buffer eviction: (`function evictStale(`, `packages/ui-react/src/stores/chat-state.ts:1086-1102`).
- Buffer eviction: (`function evictStale(`, `packages/ui-react/src/stores/chat-slices/buffers.ts:61-76`).

- Root namespace: (`const prefix = options.storagePrefix ??`, `packages/ui-react/src/root.ts:182-237`).

- Clear transcript selection: (`clearMessages: () => {`, `packages/ui-react/src/stores/chat-state.ts:2139-2148`).
- Clear transcript selection: (`clearMessages: () => {`, `packages/ui-react/src/stores/chat-slices/session.ts:116-125`).

- Composer draft/acknowledgement: [`const [input, setInput] = useState("");`](https://github.com/schlessera/brain-kit/blob/7bfdc71074f4ff6243e0d7133e83dbb36eb2a971/packages/ui-react/src/components/chat/composer.tsx#L62-L85), before #951 moved the draft into the root's draft store (`stores/draft-state.ts`).

Expand Down Expand Up @@ -401,7 +401,7 @@ implementation. Any PR moving these lines owns their citation repairs.

- Queued acceptance: (`// Queue it as the session's next turn; report queued immediately.`, `packages/ui-server/src/ws/run-session.ts:1089-1134`).

- Approval target: (`function mutateLastAssistant(`, `packages/ui-react/src/stores/chat-state.ts:1233-1263`).
- Approval target: (`function mutateLastAssistant(`, `packages/ui-react/src/stores/chat-slices/buffers.ts:142-172`).

## Bounded follow-up ownership

Expand Down
199 changes: 199 additions & 0 deletions packages/ui-react/src/stores/chat-slices/approvals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
/**
* Approvals (#1361): tool-permission cards — the request, its decision,
* the voice-permission receipt, and the closure, reopening and revocation of
* cards restored from history.
*/
import { POLICY_CHANGE_REVIEW_SCHEMA } from "@schlessera/brain-ui-sdk/tool-contracts";
import type { ChatMessage, ChatState, SessionChat, ToolCall } from "../chat-state.js";
import type { ChatSliceContext } from "./buffers.js";

export type ApprovalsSliceSlice = Pick<ChatState,
| "requestToolApproval"
| "applyVoicePermission"
| "resolveToolApproval"
| "closeRestoredApprovals"
| "reopenRestoredApprovals"
| "revokeRestoredApprovals"
>;

export function createApprovalsSlice(ctx: ChatSliceContext): ApprovalsSliceSlice {
const { set, get, nextId, mutateBuffer, mutateToolHolder } = ctx;
return {
requestToolApproval: (key, toolUseId, toolName, input, description, kind, rememberable, turnId) =>
mutateBuffer(key, (chat) => {
const msgs = [...chat.messages];
const request = (previous: ToolCall | undefined, restored: boolean): ToolCall => ({
id: toolUseId,
name: toolName,
input,
inputJson: JSON.stringify(input, null, 2),
status: "pending_approval",
...(kind ? { approvalKind: kind } : {}),
...(kind === "policy" && POLICY_CHANGE_REVIEW_SCHEMA.safeParse(input.policyReview).success ? { policyChange: POLICY_CHANGE_REVIEW_SCHEMA.parse(input.policyReview) } : {}),
...(rememberable === false ? { approvalRememberable: false } : {}),
...(description ? { approvalDescription: description } : {}),
// A card already pending keeps what it was; anything else is
// restored when it lands on a message no live turn is streaming.
...((previous?.status === "pending_approval" ? previous.restored : restored)
? { restored: true as const, ...(turnId ? { approvalTurnId: turnId } : {}) }
: {}),
});
// One card per request: a re-delivery updates the card wherever it
// is, and never draws a second one (#964).
const holder = msgs.findLastIndex((m) => m.role === "assistant" && m.toolCalls.some((t) => t.id === toolUseId));
if (holder !== -1) {
const target = msgs[holder]!;
msgs[holder] = {
...target,
// A card the host already closed with a reason (#1072) stays
// closed: a late duplicate of its request revives nothing.
toolCalls: target.toolCalls.map((t) => t.id !== toolUseId || (t.readOnly && t.readOnly !== "unlisted") ? t : request(t, !target.isStreaming)),
};
return { messages: msgs };
}
// The turn's own answer, when a message carries its id; otherwise
// the latest answer, if it is still streaming or comes after the
// user's latest message, and no other turn owns it.
let index = turnId ? msgs.findLastIndex((m) => m.role === "assistant" && m.turnId === turnId) : -1;
if (index === -1) {
const last = msgs.findLastIndex((m) => m.role === "assistant");
const lastUser = msgs.findLastIndex((m) => m.role === "user");
const candidate = last === -1 ? undefined : msgs[last]!;
if (candidate && (candidate.isStreaming || last > lastUser) && (!candidate.turnId || !turnId || candidate.turnId === turnId)) {
index = last;
}
}
if (index === -1) {
// No answer belongs to this turn: the replay ended on the user's
// message. Draw the turn's shell, holding only the card, with no
// invented text (D52 §4, approval recovery).
msgs.push({
id: nextId(),
role: "assistant",
content: "",
toolCalls: [request(undefined, true)],
parts: [{ kind: "tool", toolIndex: 0 }],
isStreaming: false,
timestamp: Date.now(),
turnShell: true,
...(turnId ? { turnId } : {}),
});
return { messages: msgs };
}
const last = msgs[index]!;
// Check if tool call already exists (from streaming)
const existingIdx = last.toolCalls.findIndex(
(t) => t.name === toolName && t.status === "streaming"
);
const tools = [...last.toolCalls];
const toolCall = request(undefined, !last.isStreaming);
let parts = last.parts;
if (existingIdx >= 0) {
tools[existingIdx] = toolCall;
} else {
// Approval request for a tool that never streamed a start event —
// give it a chronological slot too.
parts = [...parts, { kind: "tool", toolIndex: tools.length }];
tools.push(toolCall);
}
msgs[index] = { ...last, toolCalls: tools, parts };
return { messages: msgs };
}),

applyVoicePermission: (receipt) => {
const key = receipt.sessionId;
const chat = get().buffers[key];
const owns = (m: ChatMessage, t: ToolCall) => t.id === receipt.toolUseId && (t.approvalTurnId ?? m.turnId ?? m.streamTurnId) === receipt.turnId;
if (!chat?.messages.some(m => m.toolCalls.some(t => owns(m, t) && t.voicePermission !== receipt))) return;
mutateBuffer(key, current => ({ messages: current.messages.map(m => ({ ...m, toolCalls: m.toolCalls.map(t => {
if (!owns(m, t)) return t;
// Receipt state locks decisions through awaitsDecision. Preserve the
// transcript status so ordinary result/recovery routing can still
// recognize an unanswered restored card and record its closure.
return { ...t, voicePermission: receipt };
}) })) }));
},

resolveToolApproval: (key, toolUseId, approved) =>
mutateToolHolder(key, toolUseId, (last) => ({
...last,
toolCalls: last.toolCalls.map((t) =>
t.id === toolUseId
? {
...t,
status: approved ? ("approved" as const) : ("denied" as const),
// Don't let approval wait time inflate the duration badge
...(approved ? { startedAt: Date.now() } : {}),
}
: t
),
})),

closeRestoredApprovals: (key, closures) => {
if (closures.length === 0) return;
const byId = new Map(closures.map((c) => [c.toolUseId, c.closure]));
mutateBuffer(key, (chat) => {
let changed = false;
const messages = chat.messages.map((m) => {
if (m.role !== "assistant" || !m.toolCalls.some((t) => byId.has(t.id))) return m;
let touched = false;
const toolCalls = m.toolCalls.map((t) => {
const closure = byId.get(t.id);
if (!closure || !t.restored || t.readOnly === closure) return t;
// Decided on this page: the card already says what happened.
if (t.status === "approved" || t.status === "denied") return t;
if (t.readOnly && t.readOnly !== "unlisted") return t;
touched = true;
return { ...t, readOnly: closure };
});
if (!touched) return m;
changed = true;
return { ...m, toolCalls };
});
return changed ? { messages } : {};
});
},

reopenRestoredApprovals: (key, toolUseIds) => {
const reopen = (t: ToolCall) => toolUseIds.includes(t.id) && t.readOnly === "unlisted" && t.status === "pending_approval";
const chat = key === null ? get().draft : get().buffers[key];
if (!chat?.messages.some((m) => m.toolCalls.some(reopen))) return;
mutateBuffer(key, (current) => ({
messages: current.messages.map((m) => {
if (!m.toolCalls.some(reopen)) return m;
return { ...m, toolCalls: m.toolCalls.map((t) => {
if (!reopen(t)) return t;
const { readOnly: _closed, ...open } = t;
return open;
}) };
}),
}));
},

revokeRestoredApprovals: () =>
set((state) => {
const revoke = (chat: SessionChat): SessionChat => {
if (!chat.messages.some((m) => m.toolCalls.some((t) => t.restored && t.readOnly !== "revoked"))) return chat;
const messages = chat.messages.map((m) => {
if (!m.toolCalls.some((t) => t.restored && t.readOnly !== "revoked")) return m;
const toolCalls = m.toolCalls.map((t) => {
if (!t.restored || t.readOnly === "revoked") return t;
// The request's identity goes with the authority to answer
// it: the card keeps only what it showed.
const { approvalTurnId: _turn, ...rest } = t;
return { ...rest, id: `revoked-${nextId()}`, readOnly: "revoked" as const };
});
if (!m.turnShell) return { ...m, toolCalls };
const { turnId: _shellTurn, ...shell } = m;
return { ...shell, toolCalls };
});
return { ...chat, messages };
};
const buffers = Object.fromEntries(Object.entries(state.buffers).map(([id, chat]) => [id, revoke(chat)]));
const changed = Object.keys(buffers).some((id) => buffers[id] !== state.buffers[id]);
const draft = state.draft ? revoke(state.draft) : state.draft;
if (!changed && draft === state.draft) return state;
return { buffers, draft };
}),
};
}
Loading
Loading