mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-04 14:49:46 +08:00
1252 lines
40 KiB
TypeScript
1252 lines
40 KiB
TypeScript
"use client";
|
|
|
|
import React, {
|
|
createContext,
|
|
useCallback,
|
|
useContext,
|
|
useEffect,
|
|
useLayoutEffect,
|
|
useMemo,
|
|
useReducer,
|
|
useRef,
|
|
} from "react";
|
|
import {
|
|
LANGUAGE_EVENT,
|
|
LANGUAGE_STORAGE_KEY,
|
|
normalizeLanguage,
|
|
readStoredLanguage,
|
|
writeStoredActiveSessionId,
|
|
} from "@/context/app-shell-storage";
|
|
import type { StreamEvent, ChatMessage, LLMSelection } from "@/lib/unified-ws";
|
|
import { UnifiedWSClient } from "@/lib/unified-ws";
|
|
import { getSession, type SessionMessage } from "@/lib/session-api";
|
|
import { normalizeMarkdownForDisplay } from "@/lib/markdown-display";
|
|
import { normalizeMessageContent } from "@/lib/message-content";
|
|
import { shouldAppendEventContent } from "@/lib/stream";
|
|
import {
|
|
normalizeBookReferences,
|
|
type BookReferencePayload,
|
|
} from "@/lib/book-references";
|
|
|
|
type SessionRuntimeStatus =
|
|
| "idle"
|
|
| "running"
|
|
| "completed"
|
|
| "failed"
|
|
| "cancelled"
|
|
| "rejected";
|
|
|
|
interface OutgoingAttachment {
|
|
type: string;
|
|
url?: string;
|
|
base64?: string;
|
|
filename?: string;
|
|
mime_type?: string;
|
|
}
|
|
|
|
interface NotebookReferencePayload {
|
|
notebook_id: string;
|
|
record_ids: string[];
|
|
}
|
|
|
|
type HistoryReferencePayload = string[];
|
|
|
|
type QuestionNotebookReferencePayload = number[];
|
|
|
|
type MemoryReferencePayload = Array<"summary" | "profile">;
|
|
|
|
export interface SendMessageOptions {
|
|
displayUserMessage?: boolean;
|
|
persistUserMessage?: boolean;
|
|
requestSnapshotOverride?: MessageRequestSnapshot;
|
|
bookReferences?: BookReferencePayload[];
|
|
}
|
|
|
|
export interface ChatState {
|
|
sessionId: string | null;
|
|
enabledTools: string[];
|
|
activeCapability: string | null;
|
|
knowledgeBases: string[];
|
|
llmSelection: LLMSelection | null;
|
|
messages: MessageItem[];
|
|
isStreaming: boolean;
|
|
currentStage: string;
|
|
language: string;
|
|
}
|
|
|
|
interface SessionStatusSnapshot {
|
|
sessionId: string;
|
|
status: SessionRuntimeStatus;
|
|
activeTurnId: string | null;
|
|
updatedAt: number;
|
|
}
|
|
|
|
export interface MessageAttachment {
|
|
type: string;
|
|
filename?: string;
|
|
base64?: string;
|
|
url?: string;
|
|
mime_type?: string;
|
|
/** Stable per-attachment id; matches the URL segment served by /api/attachments. */
|
|
id?: string;
|
|
/** Plain-text rendering of office docs, populated by the backend extractor.
|
|
* Used by the preview drawer to show "what the LLM saw" for binary docs. */
|
|
extracted_text?: string;
|
|
}
|
|
|
|
export interface MessageRequestSnapshot {
|
|
content: string;
|
|
capability?: string | null;
|
|
enabledTools: string[];
|
|
knowledgeBases: string[];
|
|
language: string;
|
|
attachments?: MessageAttachment[];
|
|
config?: Record<string, unknown>;
|
|
notebookReferences?: NotebookReferencePayload[];
|
|
historyReferences?: HistoryReferencePayload;
|
|
questionNotebookReferences?: QuestionNotebookReferencePayload;
|
|
bookReferences?: BookReferencePayload[];
|
|
skills?: string[];
|
|
memoryReferences?: MemoryReferencePayload;
|
|
llmSelection?: LLMSelection | null;
|
|
}
|
|
|
|
export interface MessageItem {
|
|
role: "user" | "assistant" | "system";
|
|
content: string;
|
|
capability?: string;
|
|
events?: StreamEvent[];
|
|
attachments?: MessageAttachment[];
|
|
requestSnapshot?: MessageRequestSnapshot;
|
|
}
|
|
|
|
interface SessionEntry extends ChatState {
|
|
key: string;
|
|
status: SessionRuntimeStatus;
|
|
activeTurnId: string | null;
|
|
lastSeq: number;
|
|
updatedAt: number;
|
|
}
|
|
|
|
interface ProviderState {
|
|
selectedKey: string | null;
|
|
sessions: Record<string, SessionEntry>;
|
|
sidebarRefreshToken: number;
|
|
}
|
|
|
|
type Action =
|
|
| { type: "SET_TOOLS"; tools: string[] }
|
|
| { type: "SET_CAPABILITY"; cap: string | null }
|
|
| { type: "SET_KB"; kbs: string[] }
|
|
| { type: "SET_LLM_SELECTION"; selection: LLMSelection | null }
|
|
| { type: "SET_LANGUAGE"; lang: string }
|
|
| {
|
|
type: "ADD_USER_MSG";
|
|
key: string;
|
|
content: string;
|
|
capability?: string | null;
|
|
attachments?: MessageAttachment[];
|
|
requestSnapshot?: MessageRequestSnapshot;
|
|
}
|
|
| { type: "POP_LAST_ASSISTANT"; key: string }
|
|
| { type: "RESTORE_ASSISTANT"; key: string; message: MessageItem }
|
|
| { type: "STREAM_START"; key: string }
|
|
| { type: "STREAM_EVENT"; key: string; event: StreamEvent }
|
|
| {
|
|
type: "STREAM_END";
|
|
key: string;
|
|
status?: SessionRuntimeStatus;
|
|
turnId?: string | null;
|
|
}
|
|
| {
|
|
type: "BIND_SERVER_SESSION";
|
|
key: string;
|
|
sessionId: string;
|
|
turnId?: string | null;
|
|
}
|
|
| {
|
|
type: "LOAD_SESSION";
|
|
key: string;
|
|
sessionId: string;
|
|
messages: MessageItem[];
|
|
activeTurnId?: string | null;
|
|
status?: SessionRuntimeStatus;
|
|
tools?: string[];
|
|
capability?: string | null;
|
|
knowledgeBases?: string[];
|
|
llmSelection?: LLMSelection | null;
|
|
language?: string;
|
|
}
|
|
| { type: "NEW_SESSION"; key: string };
|
|
|
|
function createSessionEntry(
|
|
key: string,
|
|
sessionId: string | null = null,
|
|
): SessionEntry {
|
|
return {
|
|
key,
|
|
sessionId,
|
|
enabledTools: [],
|
|
activeCapability: null,
|
|
knowledgeBases: [],
|
|
llmSelection: null,
|
|
messages: [],
|
|
isStreaming: false,
|
|
currentStage: "",
|
|
language: typeof window === "undefined" ? "en" : readStoredLanguage(),
|
|
status: "idle",
|
|
activeTurnId: null,
|
|
lastSeq: 0,
|
|
updatedAt: Date.now(),
|
|
};
|
|
}
|
|
|
|
function ensureSelectedSession(state: ProviderState): SessionEntry {
|
|
if (state.selectedKey && state.sessions[state.selectedKey]) {
|
|
return state.sessions[state.selectedKey];
|
|
}
|
|
return createSessionEntry("draft");
|
|
}
|
|
|
|
function updateSelectedSession(
|
|
state: ProviderState,
|
|
updater: (session: SessionEntry) => SessionEntry,
|
|
): ProviderState {
|
|
const current = ensureSelectedSession(state);
|
|
const key = state.selectedKey || current.key;
|
|
const nextSession = updater(current);
|
|
return {
|
|
...state,
|
|
selectedKey: key,
|
|
sessions: {
|
|
...state.sessions,
|
|
[key]: nextSession,
|
|
},
|
|
};
|
|
}
|
|
|
|
function reducer(state: ProviderState, action: Action): ProviderState {
|
|
switch (action.type) {
|
|
case "SET_TOOLS":
|
|
return updateSelectedSession(state, (session) => ({
|
|
...session,
|
|
enabledTools: action.tools,
|
|
}));
|
|
case "SET_CAPABILITY":
|
|
return updateSelectedSession(state, (session) => ({
|
|
...session,
|
|
activeCapability: action.cap,
|
|
}));
|
|
case "SET_KB":
|
|
return updateSelectedSession(state, (session) => ({
|
|
...session,
|
|
knowledgeBases: action.kbs,
|
|
}));
|
|
case "SET_LLM_SELECTION":
|
|
return updateSelectedSession(state, (session) => ({
|
|
...session,
|
|
llmSelection: action.selection,
|
|
}));
|
|
case "SET_LANGUAGE":
|
|
return updateSelectedSession(state, (session) => ({
|
|
...session,
|
|
language: action.lang,
|
|
}));
|
|
case "ADD_USER_MSG": {
|
|
const session =
|
|
state.sessions[action.key] ?? createSessionEntry(action.key);
|
|
return {
|
|
...state,
|
|
sessions: {
|
|
...state.sessions,
|
|
[action.key]: {
|
|
...session,
|
|
messages: [
|
|
...session.messages,
|
|
{
|
|
role: "user",
|
|
content: action.content,
|
|
capability: action.capability || "",
|
|
...(action.attachments?.length
|
|
? { attachments: action.attachments }
|
|
: {}),
|
|
...(action.requestSnapshot
|
|
? { requestSnapshot: action.requestSnapshot }
|
|
: {}),
|
|
},
|
|
],
|
|
updatedAt: Date.now(),
|
|
},
|
|
},
|
|
};
|
|
}
|
|
case "POP_LAST_ASSISTANT": {
|
|
const session = state.sessions[action.key];
|
|
if (!session || session.messages.length === 0) return state;
|
|
const last = session.messages[session.messages.length - 1];
|
|
if (last.role !== "assistant") return state;
|
|
return {
|
|
...state,
|
|
sessions: {
|
|
...state.sessions,
|
|
[action.key]: {
|
|
...session,
|
|
messages: session.messages.slice(0, -1),
|
|
updatedAt: Date.now(),
|
|
},
|
|
},
|
|
};
|
|
}
|
|
case "RESTORE_ASSISTANT": {
|
|
// Revert an optimistic POP_LAST_ASSISTANT when the server rejects a
|
|
// regenerate request (e.g. ``regenerate_busy``), so the user doesn't
|
|
// silently lose their last reply.
|
|
const session = state.sessions[action.key];
|
|
if (!session) return state;
|
|
const messages = [...session.messages];
|
|
// Drop any placeholder STREAM_START assistant bubble before restoring.
|
|
while (
|
|
messages.length > 0 &&
|
|
messages[messages.length - 1].role === "assistant" &&
|
|
(messages[messages.length - 1].content ?? "") === "" &&
|
|
(messages[messages.length - 1].events?.length ?? 0) === 0
|
|
) {
|
|
messages.pop();
|
|
}
|
|
messages.push(action.message);
|
|
return {
|
|
...state,
|
|
sessions: {
|
|
...state.sessions,
|
|
[action.key]: {
|
|
...session,
|
|
messages,
|
|
updatedAt: Date.now(),
|
|
},
|
|
},
|
|
};
|
|
}
|
|
case "STREAM_START":
|
|
return {
|
|
...state,
|
|
sessions: {
|
|
...state.sessions,
|
|
[action.key]: {
|
|
...(state.sessions[action.key] ?? createSessionEntry(action.key)),
|
|
isStreaming: true,
|
|
status: "running",
|
|
messages: [
|
|
...(state.sessions[action.key]?.messages ?? []),
|
|
{
|
|
role: "assistant",
|
|
content: "",
|
|
events: [],
|
|
capability:
|
|
(state.sessions[action.key] ?? createSessionEntry(action.key))
|
|
.activeCapability || "",
|
|
},
|
|
],
|
|
updatedAt: Date.now(),
|
|
},
|
|
},
|
|
};
|
|
case "STREAM_EVENT": {
|
|
const session =
|
|
state.sessions[action.key] ?? createSessionEntry(action.key);
|
|
const msgs = [...session.messages];
|
|
let last = msgs[msgs.length - 1];
|
|
if (last?.role !== "assistant") {
|
|
msgs.push({
|
|
role: "assistant",
|
|
content: "",
|
|
events: [],
|
|
capability: session.activeCapability || "",
|
|
});
|
|
last = msgs[msgs.length - 1];
|
|
}
|
|
const events = [...(last?.events || []), action.event];
|
|
let content = last?.content || "";
|
|
if (shouldAppendEventContent(action.event))
|
|
content += action.event.content;
|
|
const capability = last?.capability || session.activeCapability || "";
|
|
msgs[msgs.length - 1] = {
|
|
...(last || { role: "assistant", content: "" }),
|
|
content,
|
|
events,
|
|
capability,
|
|
};
|
|
return {
|
|
...state,
|
|
sessions: {
|
|
...state.sessions,
|
|
[action.key]: {
|
|
...session,
|
|
messages: msgs,
|
|
currentStage:
|
|
action.event.type === "stage_start"
|
|
? action.event.stage
|
|
: action.event.type === "stage_end"
|
|
? ""
|
|
: session.currentStage,
|
|
activeTurnId: action.event.turn_id || session.activeTurnId,
|
|
lastSeq: Math.max(session.lastSeq, action.event.seq || 0),
|
|
updatedAt: Date.now(),
|
|
},
|
|
},
|
|
};
|
|
}
|
|
case "STREAM_END":
|
|
return {
|
|
...state,
|
|
sessions: {
|
|
...state.sessions,
|
|
[action.key]: {
|
|
...(state.sessions[action.key] ?? createSessionEntry(action.key)),
|
|
isStreaming: false,
|
|
currentStage: "",
|
|
status: action.status ?? "completed",
|
|
activeTurnId:
|
|
action.status === "running"
|
|
? action.turnId ||
|
|
state.sessions[action.key]?.activeTurnId ||
|
|
null
|
|
: null,
|
|
updatedAt: Date.now(),
|
|
},
|
|
},
|
|
sidebarRefreshToken: state.sidebarRefreshToken + 1,
|
|
};
|
|
case "BIND_SERVER_SESSION": {
|
|
const current =
|
|
state.sessions[action.key] ?? createSessionEntry(action.key);
|
|
const targetKey = action.sessionId;
|
|
const existing = state.sessions[targetKey];
|
|
const merged: SessionEntry = {
|
|
...(existing ?? current),
|
|
...current,
|
|
key: targetKey,
|
|
sessionId: action.sessionId,
|
|
activeTurnId: action.turnId || current.activeTurnId,
|
|
status: current.isStreaming ? "running" : current.status,
|
|
updatedAt: Date.now(),
|
|
};
|
|
const nextSessions = { ...state.sessions };
|
|
delete nextSessions[action.key];
|
|
nextSessions[targetKey] = merged;
|
|
return {
|
|
...state,
|
|
selectedKey:
|
|
state.selectedKey === action.key ? targetKey : state.selectedKey,
|
|
sessions: nextSessions,
|
|
sidebarRefreshToken: state.sidebarRefreshToken + 1,
|
|
};
|
|
}
|
|
case "LOAD_SESSION": {
|
|
const existing =
|
|
state.sessions[action.key] ??
|
|
createSessionEntry(action.key, action.sessionId);
|
|
return {
|
|
...state,
|
|
selectedKey: action.key,
|
|
sessions: {
|
|
...state.sessions,
|
|
[action.key]: {
|
|
...existing,
|
|
key: action.key,
|
|
sessionId: action.sessionId,
|
|
enabledTools: action.tools ?? existing.enabledTools,
|
|
activeCapability:
|
|
action.capability !== undefined
|
|
? action.capability
|
|
: existing.activeCapability,
|
|
knowledgeBases: action.knowledgeBases ?? existing.knowledgeBases,
|
|
llmSelection:
|
|
action.llmSelection !== undefined
|
|
? action.llmSelection
|
|
: existing.llmSelection,
|
|
messages: action.messages,
|
|
isStreaming: (action.status || "idle") === "running",
|
|
currentStage: "",
|
|
activeTurnId: action.activeTurnId || null,
|
|
status: action.status || "idle",
|
|
language: action.language ?? existing.language,
|
|
updatedAt: Date.now(),
|
|
},
|
|
},
|
|
};
|
|
}
|
|
case "NEW_SESSION": {
|
|
const MAX_CACHED_SESSIONS = 20;
|
|
let nextSessions = {
|
|
...state.sessions,
|
|
[action.key]: createSessionEntry(action.key),
|
|
};
|
|
const keys = Object.keys(nextSessions);
|
|
if (keys.length > MAX_CACHED_SESSIONS) {
|
|
const evictable = keys
|
|
.filter(
|
|
(k) => k !== action.key && nextSessions[k].status !== "running",
|
|
)
|
|
.sort(
|
|
(a, b) => nextSessions[a].updatedAt - nextSessions[b].updatedAt,
|
|
);
|
|
const toRemove = evictable.slice(0, keys.length - MAX_CACHED_SESSIONS);
|
|
for (const k of toRemove) delete nextSessions[k];
|
|
}
|
|
return { ...state, selectedKey: action.key, sessions: nextSessions };
|
|
}
|
|
default:
|
|
return state;
|
|
}
|
|
}
|
|
|
|
const initialState: ProviderState = {
|
|
selectedKey: null,
|
|
sessions: {},
|
|
sidebarRefreshToken: 0,
|
|
};
|
|
|
|
interface ChatContextValue {
|
|
state: ChatState;
|
|
setTools: (tools: string[]) => void;
|
|
setCapability: (cap: string | null) => void;
|
|
setKBs: (kbs: string[]) => void;
|
|
setLLMSelection: (selection: LLMSelection | null) => void;
|
|
setLanguage: (lang: string) => void;
|
|
sendMessage: (
|
|
content: string,
|
|
attachments?: OutgoingAttachment[],
|
|
config?: Record<string, unknown>,
|
|
notebookReferences?: NotebookReferencePayload[],
|
|
historyReferences?: HistoryReferencePayload,
|
|
options?: SendMessageOptions,
|
|
questionNotebookReferences?: QuestionNotebookReferencePayload,
|
|
skills?: string[],
|
|
memoryReferences?: MemoryReferencePayload,
|
|
) => void;
|
|
cancelStreamingTurn: () => void;
|
|
regenerateLastMessage: () => void;
|
|
newSession: () => void;
|
|
loadSession: (sessionId: string) => Promise<void>;
|
|
selectedSessionId: string | null;
|
|
sessionStatuses: Record<string, SessionStatusSnapshot>;
|
|
sidebarRefreshToken: number;
|
|
}
|
|
|
|
const ChatCtx = createContext<ChatContextValue | null>(null);
|
|
|
|
function hydrateMessageAttachments(
|
|
attachments: SessionMessage["attachments"],
|
|
): MessageAttachment[] {
|
|
return Array.isArray(attachments)
|
|
? attachments.map((item) => ({
|
|
type: item.type,
|
|
filename: item.filename,
|
|
base64: item.base64,
|
|
url: item.url,
|
|
mime_type: item.mime_type,
|
|
id: item.id,
|
|
extracted_text: item.extracted_text,
|
|
}))
|
|
: [];
|
|
}
|
|
|
|
function asRecord(value: unknown): Record<string, unknown> | null {
|
|
return value && typeof value === "object" && !Array.isArray(value)
|
|
? (value as Record<string, unknown>)
|
|
: null;
|
|
}
|
|
|
|
function asStringArray(value: unknown): string[] {
|
|
return Array.isArray(value)
|
|
? value.filter(
|
|
(item): item is string => typeof item === "string" && item.length > 0,
|
|
)
|
|
: [];
|
|
}
|
|
|
|
function asLLMSelection(value: unknown): LLMSelection | null {
|
|
const record = asRecord(value);
|
|
const profileId =
|
|
typeof record?.profile_id === "string" ? record.profile_id.trim() : "";
|
|
const modelId =
|
|
typeof record?.model_id === "string" ? record.model_id.trim() : "";
|
|
return profileId && modelId
|
|
? { profile_id: profileId, model_id: modelId }
|
|
: null;
|
|
}
|
|
|
|
function asMemoryReferences(value: unknown): MemoryReferencePayload {
|
|
return asStringArray(value).filter(
|
|
(item): item is "summary" | "profile" =>
|
|
item === "summary" || item === "profile",
|
|
);
|
|
}
|
|
|
|
function asNotebookReferences(value: unknown): NotebookReferencePayload[] {
|
|
if (!Array.isArray(value)) return [];
|
|
return value.flatMap((item) => {
|
|
const ref = asRecord(item);
|
|
const notebookId =
|
|
typeof ref?.notebook_id === "string" ? ref.notebook_id : "";
|
|
const recordIds = asStringArray(ref?.record_ids);
|
|
return notebookId && recordIds.length
|
|
? [{ notebook_id: notebookId, record_ids: recordIds }]
|
|
: [];
|
|
});
|
|
}
|
|
|
|
function asQuestionReferences(
|
|
value: unknown,
|
|
): QuestionNotebookReferencePayload {
|
|
return Array.isArray(value)
|
|
? value
|
|
.map((item) => (typeof item === "number" ? item : Number(item)))
|
|
.filter((item) => Number.isInteger(item))
|
|
: [];
|
|
}
|
|
|
|
function hydrateRequestSnapshot(
|
|
message: SessionMessage,
|
|
content: string,
|
|
attachments: MessageAttachment[],
|
|
): MessageRequestSnapshot | undefined {
|
|
const metadata = asRecord(message.metadata);
|
|
const stored = asRecord(
|
|
metadata?.request_snapshot ?? metadata?.requestSnapshot,
|
|
);
|
|
if (!stored) return undefined;
|
|
|
|
const snapshot: MessageRequestSnapshot = {
|
|
content: typeof stored.content === "string" ? stored.content : content,
|
|
capability:
|
|
typeof stored.capability === "string"
|
|
? stored.capability
|
|
: message.capability || "",
|
|
enabledTools: asStringArray(stored.enabledTools),
|
|
knowledgeBases: asStringArray(stored.knowledgeBases),
|
|
language: typeof stored.language === "string" ? stored.language : "en",
|
|
...(attachments.length ? { attachments } : {}),
|
|
};
|
|
|
|
const config = asRecord(stored.config);
|
|
const notebookReferences = asNotebookReferences(stored.notebookReferences);
|
|
const historyReferences = asStringArray(stored.historyReferences);
|
|
const questionNotebookReferences = asQuestionReferences(
|
|
stored.questionNotebookReferences,
|
|
);
|
|
const skills = asStringArray(stored.skills);
|
|
const memoryReferences = asMemoryReferences(stored.memoryReferences);
|
|
const bookReferences = normalizeBookReferences(stored.bookReferences);
|
|
const llmSelection = asLLMSelection(stored.llmSelection);
|
|
|
|
if (config && Object.keys(config).length) snapshot.config = config;
|
|
if (notebookReferences.length)
|
|
snapshot.notebookReferences = notebookReferences;
|
|
if (historyReferences.length) snapshot.historyReferences = historyReferences;
|
|
if (questionNotebookReferences.length) {
|
|
snapshot.questionNotebookReferences = questionNotebookReferences;
|
|
}
|
|
if (bookReferences.length) snapshot.bookReferences = bookReferences;
|
|
if (skills.length) snapshot.skills = skills;
|
|
if (memoryReferences.length) snapshot.memoryReferences = memoryReferences;
|
|
if (llmSelection) snapshot.llmSelection = llmSelection;
|
|
return snapshot;
|
|
}
|
|
|
|
export function UnifiedChatProvider({
|
|
children,
|
|
}: {
|
|
children: React.ReactNode;
|
|
}) {
|
|
const [state, dispatch] = useReducer(reducer, initialState);
|
|
const stateRef = useRef(initialState);
|
|
const runnersRef = useRef<
|
|
Map<
|
|
string,
|
|
{
|
|
key: string;
|
|
client: UnifiedWSClient;
|
|
}
|
|
>
|
|
>(new Map());
|
|
const draftCounterRef = useRef(0);
|
|
const retryTimersRef = useRef<Set<ReturnType<typeof setTimeout>>>(new Set());
|
|
// Tracks in-flight regenerate requests so we can restore the popped
|
|
// assistant message if the server rejects the request (e.g. ``regenerate_busy``
|
|
// or ``nothing_to_regenerate``). Keyed by session entry key.
|
|
const pendingRegenerateRef = useRef<Map<string, MessageItem>>(new Map());
|
|
|
|
useLayoutEffect(() => {
|
|
stateRef.current = state;
|
|
}, [state]);
|
|
|
|
useEffect(
|
|
() => () => {
|
|
runnersRef.current.forEach(({ client }) => client.disconnect());
|
|
runnersRef.current.clear();
|
|
retryTimersRef.current.forEach((id) => clearTimeout(id));
|
|
retryTimersRef.current.clear();
|
|
},
|
|
[],
|
|
);
|
|
|
|
const makeDraftKey = useCallback(() => {
|
|
draftCounterRef.current += 1;
|
|
return `draft_${Date.now()}_${draftCounterRef.current}`;
|
|
}, []);
|
|
|
|
const hydrateMessages = useCallback(
|
|
(messages: SessionMessage[]): MessageItem[] => {
|
|
return messages
|
|
.filter((message) => message.role !== "system")
|
|
.map((message) => {
|
|
const raw = normalizeMessageContent(message.content as unknown);
|
|
const attachments = hydrateMessageAttachments(message.attachments);
|
|
const requestSnapshot = hydrateRequestSnapshot(
|
|
message,
|
|
raw,
|
|
attachments,
|
|
);
|
|
return {
|
|
role: message.role,
|
|
content:
|
|
message.role === "assistant"
|
|
? normalizeMarkdownForDisplay(raw)
|
|
: raw,
|
|
capability: message.capability || "",
|
|
events: Array.isArray(message.events) ? message.events : [],
|
|
attachments,
|
|
...(requestSnapshot ? { requestSnapshot } : {}),
|
|
};
|
|
});
|
|
},
|
|
[],
|
|
);
|
|
|
|
const moveRunner = useCallback((oldKey: string, newKey: string) => {
|
|
if (oldKey === newKey) return;
|
|
const runner = runnersRef.current.get(oldKey);
|
|
if (!runner) return;
|
|
runnersRef.current.delete(oldKey);
|
|
runner.key = newKey;
|
|
runnersRef.current.set(newKey, runner);
|
|
}, []);
|
|
|
|
const handleRunnerEvent = useCallback(
|
|
(runnerKey: string, event: StreamEvent) => {
|
|
const runner = runnersRef.current.get(runnerKey);
|
|
const effectiveKey = runner?.key || runnerKey;
|
|
if (event.type === "session") {
|
|
const sessionId =
|
|
(event.metadata as { session_id?: string } | undefined)?.session_id ||
|
|
event.session_id ||
|
|
"";
|
|
const turnId =
|
|
(event.metadata as { turn_id?: string } | undefined)?.turn_id ||
|
|
event.turn_id ||
|
|
null;
|
|
if (sessionId) {
|
|
dispatch({
|
|
type: "BIND_SERVER_SESSION",
|
|
key: effectiveKey,
|
|
sessionId,
|
|
turnId,
|
|
});
|
|
moveRunner(effectiveKey, sessionId);
|
|
}
|
|
return;
|
|
}
|
|
if (event.type === "done") {
|
|
const status = String(
|
|
(event.metadata as { status?: string } | undefined)?.status ||
|
|
"completed",
|
|
);
|
|
dispatch({
|
|
type: "STREAM_END",
|
|
key: effectiveKey,
|
|
status: (status as SessionRuntimeStatus) || "completed",
|
|
turnId: event.turn_id || null,
|
|
});
|
|
pendingRegenerateRef.current.delete(effectiveKey);
|
|
const runner = runnersRef.current.get(effectiveKey);
|
|
runner?.client.disconnect();
|
|
runnersRef.current.delete(effectiveKey);
|
|
return;
|
|
}
|
|
dispatch({ type: "STREAM_EVENT", key: effectiveKey, event });
|
|
if (
|
|
event.type === "error" &&
|
|
Boolean(
|
|
(event.metadata as { turn_terminal?: boolean } | undefined)
|
|
?.turn_terminal,
|
|
)
|
|
) {
|
|
const reason = String(
|
|
(event.metadata as { reason?: string } | undefined)?.reason || "",
|
|
);
|
|
// Pre-flight regenerate rejections never mutate server state, so we
|
|
// roll back the optimistic POP_LAST_ASSISTANT/STREAM_START placeholder
|
|
// to keep the transcript in sync with the server.
|
|
if (
|
|
reason === "regenerate_busy" ||
|
|
reason === "nothing_to_regenerate"
|
|
) {
|
|
const stash = pendingRegenerateRef.current.get(effectiveKey);
|
|
if (stash) {
|
|
dispatch({
|
|
type: "RESTORE_ASSISTANT",
|
|
key: effectiveKey,
|
|
message: stash,
|
|
});
|
|
}
|
|
}
|
|
pendingRegenerateRef.current.delete(effectiveKey);
|
|
const status = String(
|
|
(event.metadata as { status?: string } | undefined)?.status ||
|
|
"failed",
|
|
);
|
|
dispatch({
|
|
type: "STREAM_END",
|
|
key: effectiveKey,
|
|
status: status as SessionRuntimeStatus,
|
|
turnId: event.turn_id || null,
|
|
});
|
|
}
|
|
},
|
|
[moveRunner],
|
|
);
|
|
|
|
const ensureRunner = useCallback(
|
|
(key: string) => {
|
|
const existing = runnersRef.current.get(key);
|
|
if (existing) {
|
|
const session = stateRef.current.sessions[key];
|
|
if (session) {
|
|
existing.client.setResumeState(session.activeTurnId, session.lastSeq);
|
|
}
|
|
if (!existing.client.connected) existing.client.connect();
|
|
return existing;
|
|
}
|
|
const record = {
|
|
key,
|
|
client: new UnifiedWSClient(
|
|
(event) => handleRunnerEvent(record.key, event),
|
|
() => {
|
|
const session = stateRef.current.sessions[record.key];
|
|
if (session?.isStreaming) {
|
|
dispatch({
|
|
type: "STREAM_END",
|
|
key: record.key,
|
|
status: "failed",
|
|
});
|
|
}
|
|
},
|
|
),
|
|
};
|
|
runnersRef.current.set(key, record);
|
|
record.client.connect();
|
|
return record;
|
|
},
|
|
[handleRunnerEvent],
|
|
);
|
|
|
|
const sendThroughRunner = useCallback(
|
|
function dispatchToRunner(key: string, msg: ChatMessage, attempt = 0) {
|
|
const runner = ensureRunner(key);
|
|
if (!runner.client.connected) {
|
|
if (attempt >= 10) {
|
|
console.error("WebSocket failed to connect after retries");
|
|
dispatch({ type: "STREAM_END", key, status: "failed" });
|
|
return;
|
|
}
|
|
const timerId = setTimeout(() => {
|
|
retryTimersRef.current.delete(timerId);
|
|
dispatchToRunner(key, msg, attempt + 1);
|
|
}, 200);
|
|
retryTimersRef.current.add(timerId);
|
|
return;
|
|
}
|
|
runner.client.send(msg);
|
|
},
|
|
[ensureRunner],
|
|
);
|
|
|
|
const loadSession = useCallback(
|
|
async (sessionId: string) => {
|
|
const session = await getSession(sessionId);
|
|
const activeTurn = Array.isArray(session.active_turns)
|
|
? session.active_turns[0]
|
|
: undefined;
|
|
dispatch({
|
|
type: "LOAD_SESSION",
|
|
key: session.session_id || session.id,
|
|
sessionId: session.session_id || session.id,
|
|
messages: hydrateMessages(session.messages ?? []),
|
|
activeTurnId: activeTurn?.turn_id || activeTurn?.id || null,
|
|
status:
|
|
(session.status as SessionRuntimeStatus | undefined) ||
|
|
(activeTurn ? "running" : "idle"),
|
|
tools: Array.isArray(session.preferences?.tools)
|
|
? session.preferences.tools
|
|
: [],
|
|
capability: session.preferences?.capability || null,
|
|
knowledgeBases: Array.isArray(session.preferences?.knowledge_bases)
|
|
? session.preferences.knowledge_bases
|
|
: [],
|
|
llmSelection: asLLMSelection(session.preferences?.llm_selection),
|
|
// The Settings language is global UI state. Historical sessions may
|
|
// have stale persisted preferences, so new turns follow the current
|
|
// app language rather than the language saved when the session began.
|
|
language: readStoredLanguage(),
|
|
});
|
|
if (activeTurn?.turn_id || activeTurn?.id) {
|
|
const key = session.session_id || session.id;
|
|
sendThroughRunner(key, {
|
|
type: "subscribe_turn",
|
|
turn_id: activeTurn.turn_id || activeTurn.id,
|
|
after_seq: 0,
|
|
});
|
|
}
|
|
},
|
|
[hydrateMessages, sendThroughRunner],
|
|
);
|
|
|
|
useEffect(() => {
|
|
if (typeof window === "undefined") return;
|
|
const current = state.selectedKey
|
|
? state.sessions[state.selectedKey]
|
|
: null;
|
|
writeStoredActiveSessionId(current?.sessionId ?? null);
|
|
}, [state.selectedKey, state.sessions]);
|
|
|
|
useEffect(() => {
|
|
if (typeof window === "undefined") return;
|
|
|
|
const syncLanguage = (language: string | null | undefined) => {
|
|
dispatch({ type: "SET_LANGUAGE", lang: normalizeLanguage(language) });
|
|
};
|
|
const onLanguage = (event: Event) => {
|
|
const detail = (event as CustomEvent<{ language?: string }>).detail;
|
|
syncLanguage(detail?.language);
|
|
};
|
|
const onStorage = (event: StorageEvent) => {
|
|
if (event.key === LANGUAGE_STORAGE_KEY) syncLanguage(event.newValue);
|
|
};
|
|
|
|
window.addEventListener(LANGUAGE_EVENT, onLanguage);
|
|
window.addEventListener("storage", onStorage);
|
|
return () => {
|
|
window.removeEventListener(LANGUAGE_EVENT, onLanguage);
|
|
window.removeEventListener("storage", onStorage);
|
|
};
|
|
}, []);
|
|
|
|
// URL is now the source of truth for session loading.
|
|
// Chat pages load sessions based on URL params; no sessionStorage restore needed.
|
|
// Initialize a draft session so the provider always has a selected key.
|
|
useEffect(() => {
|
|
if (typeof window === "undefined") return;
|
|
if (!state.selectedKey) {
|
|
dispatch({ type: "NEW_SESSION", key: makeDraftKey() });
|
|
}
|
|
}, []); // eslint-disable-line react-hooks/exhaustive-deps
|
|
|
|
// Idle timeout: if a streaming session receives no events for 60s, auto-fail it.
|
|
useEffect(() => {
|
|
const IDLE_TIMEOUT_MS = 60_000;
|
|
const CHECK_INTERVAL_MS = 10_000;
|
|
|
|
const timer = setInterval(() => {
|
|
const current = stateRef.current;
|
|
for (const [key, session] of Object.entries(current.sessions)) {
|
|
if (!session.isStreaming) continue;
|
|
if (Date.now() - session.updatedAt <= IDLE_TIMEOUT_MS) continue;
|
|
|
|
dispatch({
|
|
type: "STREAM_EVENT",
|
|
key,
|
|
event: {
|
|
type: "error",
|
|
source: "client",
|
|
stage: "",
|
|
content:
|
|
"Connection timed out — no response received for 60 seconds.",
|
|
metadata: { turn_terminal: true, status: "failed" },
|
|
timestamp: Date.now() / 1000,
|
|
},
|
|
});
|
|
dispatch({ type: "STREAM_END", key, status: "failed" });
|
|
|
|
const runner = runnersRef.current.get(key);
|
|
if (runner) {
|
|
runner.client.disconnect();
|
|
runnersRef.current.delete(key);
|
|
}
|
|
}
|
|
}, CHECK_INTERVAL_MS);
|
|
|
|
return () => clearInterval(timer);
|
|
}, []);
|
|
|
|
const sendMessage = useCallback(
|
|
(
|
|
content: string,
|
|
attachments?: OutgoingAttachment[],
|
|
config?: Record<string, unknown>,
|
|
notebookReferences?: NotebookReferencePayload[],
|
|
historyReferences?: HistoryReferencePayload,
|
|
options?: SendMessageOptions,
|
|
questionNotebookReferences?: QuestionNotebookReferencePayload,
|
|
skills?: string[],
|
|
memoryReferences?: MemoryReferencePayload,
|
|
) => {
|
|
const msgAttachments = attachments?.map((a) => ({
|
|
type: a.type,
|
|
filename: a.filename,
|
|
base64: a.base64,
|
|
url: a.url,
|
|
mime_type: a.mime_type,
|
|
}));
|
|
const currentState = stateRef.current;
|
|
let key = currentState.selectedKey;
|
|
if (!key) {
|
|
key = makeDraftKey();
|
|
dispatch({ type: "NEW_SESSION", key });
|
|
}
|
|
const session = currentState.sessions[key] ?? createSessionEntry(key);
|
|
const replaySnapshot = options?.requestSnapshotOverride;
|
|
const effectiveCapability =
|
|
replaySnapshot?.capability ?? session.activeCapability;
|
|
const effectiveTools =
|
|
replaySnapshot?.enabledTools ?? session.enabledTools;
|
|
const effectiveKnowledgeBases =
|
|
replaySnapshot?.knowledgeBases ?? session.knowledgeBases;
|
|
const effectiveLLMSelection =
|
|
replaySnapshot && "llmSelection" in replaySnapshot
|
|
? (replaySnapshot.llmSelection ?? null)
|
|
: session.llmSelection;
|
|
const effectiveLanguage =
|
|
replaySnapshot?.language ?? readStoredLanguage();
|
|
const researchSources = Array.isArray(config?.sources)
|
|
? config.sources.filter(
|
|
(value): value is string => typeof value === "string",
|
|
)
|
|
: [];
|
|
const shouldSendKnowledgeBases =
|
|
effectiveTools.includes("rag") ||
|
|
(effectiveCapability === "deep_research" &&
|
|
researchSources.includes("kb"));
|
|
const effectiveSkills = replaySnapshot?.skills ?? skills;
|
|
const effectiveMemoryReferences =
|
|
replaySnapshot?.memoryReferences ?? memoryReferences;
|
|
const effectiveBookReferences =
|
|
replaySnapshot?.bookReferences ?? options?.bookReferences;
|
|
const requestSnapshot: MessageRequestSnapshot = replaySnapshot ?? {
|
|
content,
|
|
capability: effectiveCapability,
|
|
enabledTools: [...effectiveTools],
|
|
knowledgeBases: shouldSendKnowledgeBases
|
|
? [...effectiveKnowledgeBases]
|
|
: [],
|
|
language: effectiveLanguage,
|
|
...(msgAttachments?.length ? { attachments: msgAttachments } : {}),
|
|
...(config && Object.keys(config).length > 0 ? { config } : {}),
|
|
...(notebookReferences?.length ? { notebookReferences } : {}),
|
|
...(historyReferences?.length
|
|
? { historyReferences: [...historyReferences] }
|
|
: {}),
|
|
...(questionNotebookReferences?.length
|
|
? { questionNotebookReferences: [...questionNotebookReferences] }
|
|
: {}),
|
|
...(effectiveBookReferences?.length
|
|
? { bookReferences: effectiveBookReferences }
|
|
: {}),
|
|
...(effectiveSkills?.length ? { skills: [...effectiveSkills] } : {}),
|
|
...(effectiveMemoryReferences?.length
|
|
? { memoryReferences: [...effectiveMemoryReferences] }
|
|
: {}),
|
|
...(effectiveLLMSelection
|
|
? { llmSelection: effectiveLLMSelection }
|
|
: {}),
|
|
};
|
|
if (options?.displayUserMessage !== false) {
|
|
dispatch({
|
|
type: "ADD_USER_MSG",
|
|
key,
|
|
content,
|
|
capability: effectiveCapability,
|
|
attachments: msgAttachments,
|
|
requestSnapshot,
|
|
});
|
|
}
|
|
dispatch({ type: "STREAM_START", key });
|
|
const effectiveConfig =
|
|
options?.persistUserMessage === false
|
|
? { ...(config || {}), _persist_user_message: false }
|
|
: config;
|
|
sendThroughRunner(key, {
|
|
type: "start_turn",
|
|
content,
|
|
tools: effectiveTools,
|
|
capability: effectiveCapability,
|
|
knowledge_bases: shouldSendKnowledgeBases
|
|
? effectiveKnowledgeBases
|
|
: [],
|
|
session_id: session.sessionId,
|
|
attachments,
|
|
language: effectiveLanguage,
|
|
...(notebookReferences?.length
|
|
? { notebook_references: notebookReferences }
|
|
: {}),
|
|
...(historyReferences?.length
|
|
? { history_references: historyReferences }
|
|
: {}),
|
|
...(questionNotebookReferences?.length
|
|
? { question_notebook_references: questionNotebookReferences }
|
|
: {}),
|
|
...(effectiveBookReferences?.length
|
|
? { book_references: effectiveBookReferences }
|
|
: {}),
|
|
...(effectiveSkills?.length ? { skills: effectiveSkills } : {}),
|
|
...(effectiveMemoryReferences?.length
|
|
? { memory_references: effectiveMemoryReferences }
|
|
: {}),
|
|
...(effectiveLLMSelection
|
|
? { llm_selection: effectiveLLMSelection }
|
|
: {}),
|
|
...(effectiveConfig && Object.keys(effectiveConfig).length > 0
|
|
? { config: effectiveConfig }
|
|
: {}),
|
|
});
|
|
},
|
|
[makeDraftKey, sendThroughRunner],
|
|
);
|
|
|
|
const cancelStreamingTurn = useCallback(() => {
|
|
const currentState = stateRef.current;
|
|
const key = currentState.selectedKey;
|
|
if (!key) return;
|
|
const session = currentState.sessions[key];
|
|
const turnId = session?.activeTurnId;
|
|
if (!session || !turnId) return;
|
|
const runner = runnersRef.current.get(key);
|
|
if (runner?.client.connected) {
|
|
runner.client.send({ type: "cancel_turn", turn_id: turnId });
|
|
runner.client.disconnect();
|
|
runnersRef.current.delete(key);
|
|
}
|
|
dispatch({ type: "STREAM_END", key, status: "cancelled" });
|
|
}, []);
|
|
|
|
const regenerateLastMessage = useCallback(() => {
|
|
const currentState = stateRef.current;
|
|
const key = currentState.selectedKey;
|
|
if (!key) return;
|
|
const session = currentState.sessions[key];
|
|
if (!session || !session.sessionId) return;
|
|
if (session.isStreaming) return;
|
|
const lastUser = [...session.messages]
|
|
.reverse()
|
|
.find((m) => m.role === "user");
|
|
if (!lastUser) return;
|
|
// Snapshot the trailing assistant (if any) so we can put it back when the
|
|
// server rejects the request. We intentionally keep events/attachments so
|
|
// the restored bubble round-trips identically.
|
|
const lastMessage = session.messages[session.messages.length - 1];
|
|
if (lastMessage && lastMessage.role === "assistant") {
|
|
pendingRegenerateRef.current.set(key, { ...lastMessage });
|
|
} else {
|
|
pendingRegenerateRef.current.delete(key);
|
|
}
|
|
dispatch({ type: "POP_LAST_ASSISTANT", key });
|
|
dispatch({ type: "STREAM_START", key });
|
|
sendThroughRunner(key, {
|
|
type: "regenerate",
|
|
session_id: session.sessionId,
|
|
overrides: {
|
|
language: readStoredLanguage(),
|
|
},
|
|
});
|
|
}, [sendThroughRunner]);
|
|
|
|
const derivedState = useMemo<ChatState>(() => {
|
|
const current = ensureSelectedSession(state);
|
|
return {
|
|
sessionId: current.sessionId,
|
|
enabledTools: current.enabledTools,
|
|
activeCapability: current.activeCapability,
|
|
knowledgeBases: current.knowledgeBases,
|
|
llmSelection: current.llmSelection,
|
|
messages: current.messages,
|
|
isStreaming: current.isStreaming,
|
|
currentStage: current.currentStage,
|
|
language: current.language,
|
|
};
|
|
}, [state]);
|
|
|
|
const sessionStatuses = useMemo<Record<string, SessionStatusSnapshot>>(() => {
|
|
const entries: Record<string, SessionStatusSnapshot> = {};
|
|
for (const session of Object.values(state.sessions)) {
|
|
if (!session.sessionId || session.status !== "running") continue;
|
|
entries[session.sessionId] = {
|
|
sessionId: session.sessionId,
|
|
status: session.status,
|
|
activeTurnId: session.activeTurnId,
|
|
updatedAt: session.updatedAt,
|
|
};
|
|
}
|
|
return entries;
|
|
}, [state.sessions]);
|
|
|
|
const setTools = useCallback((tools: string[]) => {
|
|
dispatch({ type: "SET_TOOLS", tools });
|
|
}, []);
|
|
|
|
const setCapability = useCallback((cap: string | null) => {
|
|
dispatch({ type: "SET_CAPABILITY", cap });
|
|
}, []);
|
|
|
|
const setKBs = useCallback((kbs: string[]) => {
|
|
dispatch({ type: "SET_KB", kbs });
|
|
}, []);
|
|
|
|
const setLLMSelection = useCallback((selection: LLMSelection | null) => {
|
|
dispatch({ type: "SET_LLM_SELECTION", selection });
|
|
}, []);
|
|
|
|
const setLanguage = useCallback((lang: string) => {
|
|
dispatch({ type: "SET_LANGUAGE", lang });
|
|
}, []);
|
|
|
|
const newSession = useCallback(() => {
|
|
dispatch({ type: "NEW_SESSION", key: makeDraftKey() });
|
|
}, [makeDraftKey]);
|
|
|
|
const value: ChatContextValue = {
|
|
state: derivedState,
|
|
setTools,
|
|
setCapability,
|
|
setKBs,
|
|
setLLMSelection,
|
|
setLanguage,
|
|
sendMessage,
|
|
cancelStreamingTurn,
|
|
regenerateLastMessage,
|
|
newSession,
|
|
loadSession,
|
|
selectedSessionId: derivedState.sessionId,
|
|
sessionStatuses,
|
|
sidebarRefreshToken: state.sidebarRefreshToken,
|
|
};
|
|
|
|
return <ChatCtx.Provider value={value}>{children}</ChatCtx.Provider>;
|
|
}
|
|
|
|
export function useUnifiedChat() {
|
|
const ctx = useContext(ChatCtx);
|
|
if (!ctx)
|
|
throw new Error("useUnifiedChat must be inside UnifiedChatProvider");
|
|
return ctx;
|
|
}
|