Files
PTSRaiAgent/web/src/session/SessionContext.tsx
T
ElebobaandClaude Opus 5.5 31585c7f35
CI / ruff · pytest (pull_request) Successful in 2m41s
feat(bot): every free-text message gets the agent's answer; every demo entry is a private copy
- Idle chat: all messages go to the agent (greetings, side-effect and substitute questions included); check-in
  signals are still recorded silently, quit intent keeps its rule reaction. Seen live: «какие будут побочки»
  was logged as a complaint («Спасибо, отметил»), «привет» got a canned phrase.
- Agent prompt: warm short replies to small talk; substitutes → only the doctor changes the drug.
- ЕМИАС demo persona button creates a private copy too (starting from consent + questionnaires, subject to
  the screening switch); the web app drops a stored session of the old shared demo account.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-04 22:27:26 +03:00

44 lines
1.5 KiB
TypeScript

import { createContext, useCallback, useContext, useMemo, useState, type ReactNode } from "react";
import type { AuthResult } from "../api/types";
import { sessionStore, type Session } from "./store";
interface SessionApi {
session: Session | null;
signIn: (auth: AuthResult) => void;
/** Leaves the account. Anonymous history is always wiped: it cannot be restored anyway. */
signOut: (opts?: { wipe?: boolean }) => void;
}
const Ctx = createContext<SessionApi | null>(null);
export function SessionProvider({ children }: { children: ReactNode }) {
// The old shared demo account ("tok-andrey") is retired: every visitor now gets a private copy.
const [session, setSession] = useState<Session | null>(() => {
const s = sessionStore.load();
return s && s.token === "tok-andrey" ? null : s;
});
const signIn = useCallback((auth: AuthResult) => {
const next: Session = { token: auth.token, mode: auth.mode, displayName: auth.display_name };
sessionStore.save(next);
setSession(next);
}, []);
const signOut = useCallback((opts?: { wipe?: boolean }) => {
setSession((current) => {
sessionStore.clear(current, opts?.wipe ?? current?.mode === "anonymous");
return null;
});
}, []);
const value = useMemo(() => ({ session, signIn, signOut }), [session, signIn, signOut]);
return <Ctx.Provider value={value}>{children}</Ctx.Provider>;
}
export function useSession(): SessionApi {
const value = useContext(Ctx);
if (!value) throw new Error("useSession outside SessionProvider");
return value;
}