Fix 5 bugs: project deletion in browser mode, hardcoded model string, dead code, server state overwrite, stale query caches
- Fix project deletion in browser mode by removing isElectron guard around api.projects.remove() call (Sidebar.tsx) - Replace hardcoded 'gpt-5-codex' with DEFAULT_MODEL / 'gpt-5.3-codex' in wsServer.ts and core reducer.ts - Remove unused bindTerminalEvents method and its imports from coreRuntime.ts - Preserve client-local terminal UI state (terminalOpen, terminalHeight, activeTerminalId, terminalGroups, activeTerminalGroupId) during SET_SERVER_STATE merges in store.ts - Invalidate git and checkpoint diff query caches when turn completions are detected in server state updates (__root.tsx)
This commit is contained in:
1 parent
ac44561caf
commit
8816ba5f81
6 files changed
+195
-108
No files matched your search
@@ -1,11 +1,15 @@
|
||||
import path from "node:path";
|
||||
import crypto from "node:crypto";
|
||||
import type { ProviderEvent, ProviderSession, TerminalEvent } from "@t3tools/contracts";
|
||||
import { EffectFanout, OrchestrationEngine, QueueProjector, type AppViewState } from "@t3tools/core";
|
||||
import type { ProviderEvent, ProviderSession } from "@t3tools/contracts";
|
||||
import {
|
||||
EffectFanout,
|
||||
OrchestrationEngine,
|
||||
QueueProjector,
|
||||
type AppViewState,
|
||||
} from "@t3tools/core";
|
||||
import { createSqliteStores } from "@t3tools/infra-sqlite";
|
||||
|
||||
import type { ProviderManager } from "./providerManager";
|
||||
import type { TerminalManager } from "./terminalManager";
|
||||
|
||||
function nowIso(): string {
|
||||
return new Date().toISOString();
|
||||
@@ -21,7 +25,11 @@ export class CoreRuntime {
|
||||
const dbPath = path.join(dbDir, "event-store.sqlite");
|
||||
this.stores = createSqliteStores(dbPath);
|
||||
this.projector = new QueueProjector(this.stores.projectionStore, this.fanout);
|
||||
this.engine = new OrchestrationEngine(this.stores.eventStore, this.stores.projectionStore, this.projector);
|
||||
this.engine = new OrchestrationEngine(
|
||||
this.stores.eventStore,
|
||||
this.stores.projectionStore,
|
||||
this.projector,
|
||||
);
|
||||
}
|
||||
|
||||
async start(cwd: string, projectName: string): Promise<void> {
|
||||
@@ -57,22 +65,6 @@ export class CoreRuntime {
|
||||
});
|
||||
}
|
||||
|
||||
bindTerminalEvents(terminalManager: TerminalManager): void {
|
||||
terminalManager.on("event", (event: TerminalEvent) => {
|
||||
if (event.type !== "activity" && event.type !== "error" && event.type !== "exited") return;
|
||||
void this.dispatch({
|
||||
id: crypto.randomUUID(),
|
||||
type: "thread.setTerminalActivity",
|
||||
issuedAt: event.createdAt,
|
||||
payload: {
|
||||
threadId: event.threadId,
|
||||
terminalId: event.terminalId,
|
||||
running: event.type === "activity" ? event.hasRunningSubprocess : false,
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async bindProviderSession(threadId: string, session: ProviderSession): Promise<void> {
|
||||
await this.dispatch({
|
||||
id: crypto.randomUUID(),
|
||||
|
||||
+41
-39
@@ -7,6 +7,7 @@ import crypto from "node:crypto";
|
||||
import type { Duplex } from "node:stream";
|
||||
|
||||
import {
|
||||
DEFAULT_MODEL,
|
||||
EDITORS,
|
||||
WS_CHANNELS,
|
||||
WS_METHODS,
|
||||
@@ -304,7 +305,8 @@ export function createServer(options: ServerOptions) {
|
||||
switch (request.method) {
|
||||
case WS_METHODS.providersStartSession: {
|
||||
const session = await providerManager.startSession(request.params as never);
|
||||
const uiThreadId = typeof paramsObj.uiThreadId === "string" ? paramsObj.uiThreadId : undefined;
|
||||
const uiThreadId =
|
||||
typeof paramsObj.uiThreadId === "string" ? paramsObj.uiThreadId : undefined;
|
||||
if (uiThreadId) {
|
||||
await coreRuntime.bindProviderSession(uiThreadId, session);
|
||||
}
|
||||
@@ -313,8 +315,11 @@ export function createServer(options: ServerOptions) {
|
||||
|
||||
case WS_METHODS.providersSendTurn: {
|
||||
const sessionId = typeof paramsObj.sessionId === "string" ? paramsObj.sessionId : undefined;
|
||||
const uiThreadId = typeof paramsObj.uiThreadId === "string" ? paramsObj.uiThreadId : undefined;
|
||||
const targetThread = uiThreadId ? state.threads.find((thread) => thread.id === uiThreadId) : findThreadBySessionId(sessionId);
|
||||
const uiThreadId =
|
||||
typeof paramsObj.uiThreadId === "string" ? paramsObj.uiThreadId : undefined;
|
||||
const targetThread = uiThreadId
|
||||
? state.threads.find((thread) => thread.id === uiThreadId)
|
||||
: findThreadBySessionId(sessionId);
|
||||
const inputText = typeof paramsObj.input === "string" ? paramsObj.input : undefined;
|
||||
if (targetThread && inputText && inputText.trim().length > 0) {
|
||||
await coreRuntime.dispatch({
|
||||
@@ -363,52 +368,49 @@ export function createServer(options: ServerOptions) {
|
||||
case WS_METHODS.projectsList:
|
||||
return projectRegistry.list();
|
||||
|
||||
case WS_METHODS.projectsAdd:
|
||||
{
|
||||
const result = projectRegistry.add(request.params as never);
|
||||
case WS_METHODS.projectsAdd: {
|
||||
const result = projectRegistry.add(request.params as never);
|
||||
await coreRuntime.dispatch({
|
||||
id: crypto.randomUUID(),
|
||||
type: "project.add",
|
||||
issuedAt: requestNow,
|
||||
payload: {
|
||||
id: result.project.id,
|
||||
name: result.project.name,
|
||||
cwd: result.project.cwd,
|
||||
model: DEFAULT_MODEL,
|
||||
scripts: result.project.scripts,
|
||||
},
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
case WS_METHODS.projectsRemove: {
|
||||
const projectId = typeof paramsObj.id === "string" ? paramsObj.id : undefined;
|
||||
if (projectId) {
|
||||
await coreRuntime.dispatch({
|
||||
id: crypto.randomUUID(),
|
||||
type: "project.add",
|
||||
type: "project.remove",
|
||||
issuedAt: requestNow,
|
||||
payload: {
|
||||
id: result.project.id,
|
||||
name: result.project.name,
|
||||
cwd: result.project.cwd,
|
||||
model: "gpt-5-codex",
|
||||
scripts: result.project.scripts,
|
||||
},
|
||||
payload: { id: projectId },
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
case WS_METHODS.projectsRemove:
|
||||
{
|
||||
const projectId = typeof paramsObj.id === "string" ? paramsObj.id : undefined;
|
||||
if (projectId) {
|
||||
await coreRuntime.dispatch({
|
||||
id: crypto.randomUUID(),
|
||||
type: "project.remove",
|
||||
issuedAt: requestNow,
|
||||
payload: { id: projectId },
|
||||
});
|
||||
}
|
||||
projectRegistry.remove(request.params as never);
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
case WS_METHODS.projectsSearchEntries:
|
||||
return searchWorkspaceEntries(request.params as never);
|
||||
case WS_METHODS.projectsUpdateScripts:
|
||||
{
|
||||
const result = projectRegistry.updateScripts(request.params as never);
|
||||
await coreRuntime.dispatch({
|
||||
id: crypto.randomUUID(),
|
||||
type: "project.updateScripts",
|
||||
issuedAt: requestNow,
|
||||
payload: { id: result.project.id, scripts: result.project.scripts },
|
||||
});
|
||||
return result;
|
||||
}
|
||||
case WS_METHODS.projectsUpdateScripts: {
|
||||
const result = projectRegistry.updateScripts(request.params as never);
|
||||
await coreRuntime.dispatch({
|
||||
id: crypto.randomUUID(),
|
||||
type: "project.updateScripts",
|
||||
issuedAt: requestNow,
|
||||
payload: { id: result.project.id, scripts: result.project.scripts },
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
case WS_METHODS.shellOpenInEditor: {
|
||||
const params = request.params as {
|
||||
|
||||
@@ -379,22 +379,18 @@ export default function Sidebar() {
|
||||
);
|
||||
if (!confirmed) return;
|
||||
|
||||
if (isElectron) {
|
||||
try {
|
||||
await api.projects.remove({ id: projectId });
|
||||
} catch (error) {
|
||||
const message =
|
||||
error instanceof Error ? error.message : "Unknown error deleting project.";
|
||||
console.error("Failed to remove project", { projectId, error });
|
||||
toastManager.add({
|
||||
type: "error",
|
||||
title: `Failed to delete "${project.name}"`,
|
||||
description: message,
|
||||
});
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await api.projects.remove({ id: projectId });
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Unknown error deleting project.";
|
||||
console.error("Failed to remove project", { projectId, error });
|
||||
toastManager.add({
|
||||
type: "error",
|
||||
title: `Failed to delete "${project.name}"`,
|
||||
description: message,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
},
|
||||
[api, dispatch, state.projects, state.threads],
|
||||
);
|
||||
|
||||
@@ -4,14 +4,16 @@ import {
|
||||
type ErrorComponentProps,
|
||||
useParams,
|
||||
} from "@tanstack/react-router";
|
||||
import { useEffect } from "react";
|
||||
import { QueryClient } from "@tanstack/react-query";
|
||||
import { useEffect, useRef } from "react";
|
||||
import { QueryClient, useQueryClient } from "@tanstack/react-query";
|
||||
|
||||
import { APP_DISPLAY_NAME } from "../branding";
|
||||
import { Button } from "../components/ui/button";
|
||||
import { AnchoredToastProvider, ToastProvider } from "../components/ui/toast";
|
||||
import { isElectron } from "../env";
|
||||
import { useNativeApi } from "../hooks/useNativeApi";
|
||||
import { invalidateGitQueries } from "../lib/gitReactQuery";
|
||||
import { providerQueryKeys } from "../lib/providerReactQuery";
|
||||
import { type AppState, useStore } from "../store";
|
||||
import { onServerStateUpdate, onServerWelcome } from "../wsNativeApi";
|
||||
|
||||
@@ -123,10 +125,12 @@ function errorDetails(error: unknown): string {
|
||||
function EventRouter() {
|
||||
const api = useNativeApi();
|
||||
const { dispatch } = useStore();
|
||||
const queryClient = useQueryClient();
|
||||
const activeThreadId = useParams({
|
||||
strict: false,
|
||||
select: (params) => params.threadId,
|
||||
});
|
||||
const prevTurnCompletionsRef = useRef<Map<string, string | undefined>>(new Map());
|
||||
|
||||
useEffect(() => {
|
||||
if (!api) return;
|
||||
@@ -142,9 +146,28 @@ function EventRouter() {
|
||||
|
||||
useEffect(() => {
|
||||
return onServerStateUpdate((snapshot) => {
|
||||
dispatch({ type: "SET_SERVER_STATE", state: snapshot as AppState });
|
||||
const serverState = snapshot as AppState;
|
||||
dispatch({ type: "SET_SERVER_STATE", state: serverState });
|
||||
|
||||
let hasNewTurnCompletion = false;
|
||||
for (const thread of serverState.threads) {
|
||||
const prev = prevTurnCompletionsRef.current.get(thread.id);
|
||||
if (thread.latestTurnCompletedAt && thread.latestTurnCompletedAt !== prev) {
|
||||
hasNewTurnCompletion = true;
|
||||
}
|
||||
}
|
||||
const nextMap = new Map<string, string | undefined>();
|
||||
for (const thread of serverState.threads) {
|
||||
nextMap.set(thread.id, thread.latestTurnCompletedAt);
|
||||
}
|
||||
prevTurnCompletionsRef.current = nextMap;
|
||||
|
||||
if (hasNewTurnCompletion) {
|
||||
void invalidateGitQueries(queryClient);
|
||||
void queryClient.invalidateQueries({ queryKey: providerQueryKeys.all });
|
||||
}
|
||||
});
|
||||
}, [dispatch]);
|
||||
}, [dispatch, queryClient]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!activeThreadId) return;
|
||||
|
||||
+19
-11
@@ -127,7 +127,6 @@ function normalizeRunningTerminalIds(
|
||||
.filter((id) => id.length > 0 && validTerminalIdSet.has(id));
|
||||
}
|
||||
|
||||
|
||||
function normalizeTerminalGroupIds(terminalIds: string[]): string[] {
|
||||
return [...new Set(terminalIds.map((id) => id.trim()).filter((id) => id.length > 0))];
|
||||
}
|
||||
@@ -297,11 +296,26 @@ function closeThreadTerminal(thread: Thread, terminalId: string): Thread {
|
||||
|
||||
export function reducer(state: AppState, action: Action): AppState {
|
||||
switch (action.type) {
|
||||
case "SET_SERVER_STATE":
|
||||
case "SET_SERVER_STATE": {
|
||||
const previousThreadById = new Map(
|
||||
state.threads.map((thread) => [thread.id, thread] as const),
|
||||
);
|
||||
return {
|
||||
...action.state,
|
||||
threads: action.state.threads.map((thread) => normalizeThreadTerminals(thread)),
|
||||
threads: action.state.threads.map((thread) => {
|
||||
const prev = previousThreadById.get(thread.id);
|
||||
if (!prev) return normalizeThreadTerminals(thread);
|
||||
return normalizeThreadTerminals({
|
||||
...thread,
|
||||
terminalOpen: prev.terminalOpen,
|
||||
terminalHeight: prev.terminalHeight,
|
||||
activeTerminalId: prev.activeTerminalId,
|
||||
terminalGroups: prev.terminalGroups,
|
||||
activeTerminalGroupId: prev.activeTerminalGroupId,
|
||||
});
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
case "ADD_PROJECT":
|
||||
if (state.projects.some((project) => project.cwd === action.project.cwd)) {
|
||||
@@ -447,10 +461,7 @@ export function reducer(state: AppState, action: Action): AppState {
|
||||
threads: updateThread(state.threads, action.threadId, (thread) => {
|
||||
const normalizedThread = normalizeThreadTerminals(thread);
|
||||
const isNewTerminal = !normalizedThread.terminalIds.includes(action.terminalId);
|
||||
if (
|
||||
isNewTerminal &&
|
||||
normalizedThread.terminalIds.length >= MAX_THREAD_TERMINAL_COUNT
|
||||
) {
|
||||
if (isNewTerminal && normalizedThread.terminalIds.length >= MAX_THREAD_TERMINAL_COUNT) {
|
||||
return normalizedThread;
|
||||
}
|
||||
const terminalIds = normalizedThread.terminalIds.includes(action.terminalId)
|
||||
@@ -520,10 +531,7 @@ export function reducer(state: AppState, action: Action): AppState {
|
||||
threads: updateThread(state.threads, action.threadId, (thread) => {
|
||||
const normalizedThread = normalizeThreadTerminals(thread);
|
||||
const isNewTerminal = !normalizedThread.terminalIds.includes(action.terminalId);
|
||||
if (
|
||||
isNewTerminal &&
|
||||
normalizedThread.terminalIds.length >= MAX_THREAD_TERMINAL_COUNT
|
||||
) {
|
||||
if (isNewTerminal && normalizedThread.terminalIds.length >= MAX_THREAD_TERMINAL_COUNT) {
|
||||
return normalizedThread;
|
||||
}
|
||||
const terminalIds = normalizedThread.terminalIds.includes(action.terminalId)
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
import type { DomainEventEnvelope } from "../domain/events";
|
||||
import type { AppViewState, ChatMessage, ProviderEvent, ProviderSessionView, ThreadView } from "../domain/models";
|
||||
import type {
|
||||
AppViewState,
|
||||
ChatMessage,
|
||||
ProviderEvent,
|
||||
ProviderSessionView,
|
||||
ThreadView,
|
||||
} from "../domain/models";
|
||||
import { emptyAppViewState } from "../domain/models";
|
||||
|
||||
function asObject(value: unknown): Record<string, unknown> | undefined {
|
||||
@@ -27,7 +33,11 @@ function evolveSession(previous: ProviderSessionView, event: ProviderEvent): Pro
|
||||
const payload = asObject(event.payload);
|
||||
if (event.method === "thread/started") {
|
||||
const thread = asObject(payload?.thread);
|
||||
return { ...previous, threadId: asString(thread?.id) ?? event.threadId ?? previous.threadId, updatedAt: event.createdAt };
|
||||
return {
|
||||
...previous,
|
||||
threadId: asString(thread?.id) ?? event.threadId ?? previous.threadId,
|
||||
updatedAt: event.createdAt,
|
||||
};
|
||||
}
|
||||
if (event.method === "turn/started") {
|
||||
const turn = asObject(payload?.turn);
|
||||
@@ -70,7 +80,10 @@ function evolveSession(previous: ProviderSessionView, event: ProviderEvent): Pro
|
||||
return { ...previous, updatedAt: event.createdAt };
|
||||
}
|
||||
|
||||
function applyEventToMessages(previous: ReadonlyArray<ChatMessage>, event: ProviderEvent): ChatMessage[] {
|
||||
function applyEventToMessages(
|
||||
previous: ReadonlyArray<ChatMessage>,
|
||||
event: ProviderEvent,
|
||||
): ChatMessage[] {
|
||||
const payload = asObject(event.payload);
|
||||
if (event.method === "item/started") {
|
||||
const item = asObject(payload?.item);
|
||||
@@ -80,7 +93,13 @@ function applyEventToMessages(previous: ReadonlyArray<ChatMessage>, event: Provi
|
||||
const seedText = asString(item?.text) ?? "";
|
||||
return [
|
||||
...previous.filter((entry) => entry.id !== itemId),
|
||||
{ id: itemId, role: "assistant", text: seedText, createdAt: event.createdAt, streaming: true },
|
||||
{
|
||||
id: itemId,
|
||||
role: "assistant",
|
||||
text: seedText,
|
||||
createdAt: event.createdAt,
|
||||
streaming: true,
|
||||
},
|
||||
];
|
||||
}
|
||||
if (event.method === "item/agentMessage/delta") {
|
||||
@@ -89,7 +108,10 @@ function applyEventToMessages(previous: ReadonlyArray<ChatMessage>, event: Provi
|
||||
if (!itemId || !delta) return [...previous];
|
||||
const idx = previous.findIndex((entry) => entry.id === itemId);
|
||||
if (idx < 0) {
|
||||
return [...previous, { id: itemId, role: "assistant", text: delta, createdAt: event.createdAt, streaming: true }];
|
||||
return [
|
||||
...previous,
|
||||
{ id: itemId, role: "assistant", text: delta, createdAt: event.createdAt, streaming: true },
|
||||
];
|
||||
}
|
||||
const next = [...previous];
|
||||
const current = next[idx];
|
||||
@@ -105,7 +127,16 @@ function applyEventToMessages(previous: ReadonlyArray<ChatMessage>, event: Provi
|
||||
const fullText = asString(item?.text);
|
||||
const idx = previous.findIndex((entry) => entry.id === itemId);
|
||||
if (idx < 0) {
|
||||
return [...previous, { id: itemId, role: "assistant", text: fullText ?? "", createdAt: event.createdAt, streaming: false }];
|
||||
return [
|
||||
...previous,
|
||||
{
|
||||
id: itemId,
|
||||
role: "assistant",
|
||||
text: fullText ?? "",
|
||||
createdAt: event.createdAt,
|
||||
streaming: false,
|
||||
},
|
||||
];
|
||||
}
|
||||
const next = [...previous];
|
||||
const current = next[idx];
|
||||
@@ -156,7 +187,8 @@ export function applyDomainEvent(state: AppViewState, event: DomainEventEnvelope
|
||||
switch (event.type) {
|
||||
case "app.bootstrapped": {
|
||||
const payload = event.payload as { cwd: string; projectName: string };
|
||||
if (state.projects.length > 0) return { ...state, threadsHydrated: true, lastPosition: event.position };
|
||||
if (state.projects.length > 0)
|
||||
return { ...state, threadsHydrated: true, lastPosition: event.position };
|
||||
return {
|
||||
...state,
|
||||
projects: [
|
||||
@@ -164,7 +196,7 @@ export function applyDomainEvent(state: AppViewState, event: DomainEventEnvelope
|
||||
id: "bootstrap-project",
|
||||
name: payload.projectName,
|
||||
cwd: payload.cwd,
|
||||
model: "gpt-5-codex",
|
||||
model: "gpt-5.3-codex",
|
||||
expanded: true,
|
||||
scripts: [],
|
||||
},
|
||||
@@ -181,7 +213,9 @@ export function applyDomainEvent(state: AppViewState, event: DomainEventEnvelope
|
||||
model: string;
|
||||
scripts: Array<{ id: string; name: string; command: string; keybinding?: string }>;
|
||||
};
|
||||
if (state.projects.some((project) => project.id === payload.id || project.cwd === payload.cwd)) {
|
||||
if (
|
||||
state.projects.some((project) => project.id === payload.id || project.cwd === payload.cwd)
|
||||
) {
|
||||
return { ...state, lastPosition: event.position };
|
||||
}
|
||||
return {
|
||||
@@ -225,11 +259,19 @@ export function applyDomainEvent(state: AppViewState, event: DomainEventEnvelope
|
||||
if (state.threads.some((thread) => thread.id === payload.id)) {
|
||||
return { ...state, lastPosition: event.position };
|
||||
}
|
||||
return { ...state, threads: [...state.threads, defaultThread(payload)], lastPosition: event.position };
|
||||
return {
|
||||
...state,
|
||||
threads: [...state.threads, defaultThread(payload)],
|
||||
lastPosition: event.position,
|
||||
};
|
||||
}
|
||||
case "thread.deleted": {
|
||||
const payload = event.payload as { id: string };
|
||||
return { ...state, threads: state.threads.filter((thread) => thread.id !== payload.id), lastPosition: event.position };
|
||||
return {
|
||||
...state,
|
||||
threads: state.threads.filter((thread) => thread.id !== payload.id),
|
||||
lastPosition: event.position,
|
||||
};
|
||||
}
|
||||
case "thread.userMessageAdded": {
|
||||
const payload = event.payload as {
|
||||
@@ -293,11 +335,22 @@ export function applyDomainEvent(state: AppViewState, event: DomainEventEnvelope
|
||||
threads: state.threads.map((thread) => {
|
||||
if (thread.id !== payload.threadId) return thread;
|
||||
const nextEvents = [payload.event, ...thread.events];
|
||||
const nextSession = thread.session ? evolveSession(thread.session, payload.event) : thread.session;
|
||||
const nextSession = thread.session
|
||||
? evolveSession(thread.session, payload.event)
|
||||
: thread.session;
|
||||
const nextMessages = applyEventToMessages(thread.messages, payload.event);
|
||||
const nextTurnId = payload.event.method === "turn/started" ? eventTurnId(payload.event) ?? thread.latestTurnId : thread.latestTurnId;
|
||||
const nextTurnStartedAt = payload.event.method === "turn/started" ? payload.event.createdAt : thread.latestTurnStartedAt;
|
||||
const nextTurnCompletedAt = payload.event.method === "turn/completed" ? payload.event.createdAt : thread.latestTurnCompletedAt;
|
||||
const nextTurnId =
|
||||
payload.event.method === "turn/started"
|
||||
? (eventTurnId(payload.event) ?? thread.latestTurnId)
|
||||
: thread.latestTurnId;
|
||||
const nextTurnStartedAt =
|
||||
payload.event.method === "turn/started"
|
||||
? payload.event.createdAt
|
||||
: thread.latestTurnStartedAt;
|
||||
const nextTurnCompletedAt =
|
||||
payload.event.method === "turn/completed"
|
||||
? payload.event.createdAt
|
||||
: thread.latestTurnCompletedAt;
|
||||
const nextTurnDuration =
|
||||
payload.event.method === "turn/completed" && thread.latestTurnStartedAt
|
||||
? durationMs(thread.latestTurnStartedAt, payload.event.createdAt)
|
||||
@@ -307,7 +360,10 @@ export function applyDomainEvent(state: AppViewState, event: DomainEventEnvelope
|
||||
events: nextEvents,
|
||||
messages: nextMessages,
|
||||
session: nextSession,
|
||||
error: payload.event.kind === "error" && payload.event.message ? payload.event.message : thread.error,
|
||||
error:
|
||||
payload.event.kind === "error" && payload.event.message
|
||||
? payload.event.message
|
||||
: thread.error,
|
||||
latestTurnId: nextTurnId,
|
||||
latestTurnStartedAt: nextTurnStartedAt,
|
||||
latestTurnCompletedAt: nextTurnCompletedAt,
|
||||
@@ -318,7 +374,11 @@ export function applyDomainEvent(state: AppViewState, event: DomainEventEnvelope
|
||||
};
|
||||
}
|
||||
case "thread.branchSet": {
|
||||
const payload = event.payload as { threadId: string; branch: string | null; worktreePath: string | null };
|
||||
const payload = event.payload as {
|
||||
threadId: string;
|
||||
branch: string | null;
|
||||
worktreePath: string | null;
|
||||
};
|
||||
return {
|
||||
...state,
|
||||
threads: state.threads.map((thread) =>
|
||||
@@ -365,6 +425,12 @@ export function applyDomainEvent(state: AppViewState, event: DomainEventEnvelope
|
||||
}
|
||||
}
|
||||
|
||||
export function reduceEvents(events: ReadonlyArray<DomainEventEnvelope>, seed?: AppViewState): AppViewState {
|
||||
return events.reduce((state, event) => applyDomainEvent(state, event), seed ?? emptyAppViewState());
|
||||
export function reduceEvents(
|
||||
events: ReadonlyArray<DomainEventEnvelope>,
|
||||
seed?: AppViewState,
|
||||
): AppViewState {
|
||||
return events.reduce(
|
||||
(state, event) => applyDomainEvent(state, event),
|
||||
seed ?? emptyAppViewState(),
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user