Add direct session URL route
This commit is contained in:
@@ -7,11 +7,15 @@ import {
|
|||||||
useChatRuntime,
|
useChatRuntime,
|
||||||
} from "@assistant-ui/react-ai-sdk";
|
} from "@assistant-ui/react-ai-sdk";
|
||||||
import type { UIMessage } from "ai";
|
import type { UIMessage } from "ai";
|
||||||
import { useCallback, useMemo } from "react";
|
import { useCallback, useEffect, useMemo, useRef } from "react";
|
||||||
import {
|
import {
|
||||||
ActivityPanel,
|
ActivityPanel,
|
||||||
ActivityProvider,
|
ActivityProvider,
|
||||||
} from "@/components/assistant-ui/activity-panel";
|
} from "@/components/assistant-ui/activity-panel";
|
||||||
|
import {
|
||||||
|
fetchLatestRunStatus,
|
||||||
|
toReplayRepository,
|
||||||
|
} from "@/components/assistant-ui/thread-list";
|
||||||
import { Thread } from "@/components/assistant-ui/thread";
|
import { Thread } from "@/components/assistant-ui/thread";
|
||||||
import { ThreadListSidebar } from "@/components/assistant-ui/threadlist-sidebar";
|
import { ThreadListSidebar } from "@/components/assistant-ui/threadlist-sidebar";
|
||||||
import {
|
import {
|
||||||
@@ -27,8 +31,13 @@ import {
|
|||||||
import { ClawSessionReplayProvider } from "@/lib/claw-session-replay";
|
import { ClawSessionReplayProvider } from "@/lib/claw-session-replay";
|
||||||
import { ClawAccountGate, useClawAccount } from "./claw-account-gate";
|
import { ClawAccountGate, useClawAccount } from "./claw-account-gate";
|
||||||
|
|
||||||
export const Assistant = () => {
|
type AssistantProps = {
|
||||||
|
initialSessionId?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const Assistant = ({ initialSessionId }: AssistantProps = {}) => {
|
||||||
const { account, isLoading, setAccount } = useClawAccount();
|
const { account, isLoading, setAccount } = useClawAccount();
|
||||||
|
const loadedInitialSessionRef = useRef<string | null>(null);
|
||||||
const transport = useMemo(
|
const transport = useMemo(
|
||||||
() =>
|
() =>
|
||||||
new AssistantChatTransport({
|
new AssistantChatTransport({
|
||||||
@@ -81,6 +90,41 @@ export const Assistant = () => {
|
|||||||
[runtime],
|
[runtime],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const sessionId = initialSessionId?.trim();
|
||||||
|
if (!account || !sessionId) return;
|
||||||
|
if (loadedInitialSessionRef.current === sessionId) return;
|
||||||
|
loadedInitialSessionRef.current = sessionId;
|
||||||
|
const targetSessionId = sessionId;
|
||||||
|
let cancelled = false;
|
||||||
|
|
||||||
|
async function loadSession() {
|
||||||
|
try {
|
||||||
|
writeActiveSessionId(targetSessionId);
|
||||||
|
const response = await fetch(`/api/claw/sessions/${targetSessionId}`, {
|
||||||
|
cache: "no-store",
|
||||||
|
});
|
||||||
|
if (!response.ok || cancelled) return;
|
||||||
|
const payload = await response.json();
|
||||||
|
const runStatus = await fetchLatestRunStatus(targetSessionId);
|
||||||
|
if (cancelled) return;
|
||||||
|
replaySession(
|
||||||
|
targetSessionId,
|
||||||
|
toReplayRepository(payload, targetSessionId, runStatus),
|
||||||
|
);
|
||||||
|
} catch {
|
||||||
|
if (!cancelled) {
|
||||||
|
loadedInitialSessionRef.current = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void loadSession();
|
||||||
|
return () => {
|
||||||
|
cancelled = true;
|
||||||
|
};
|
||||||
|
}, [account, initialSessionId, replaySession]);
|
||||||
|
|
||||||
if (isLoading) {
|
if (isLoading) {
|
||||||
return (
|
return (
|
||||||
<div className="flex min-h-dvh items-center justify-center bg-background text-muted-foreground text-sm">
|
<div className="flex min-h-dvh items-center justify-center bg-background text-muted-foreground text-sm">
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
import { Assistant } from "../../assistant";
|
||||||
|
|
||||||
|
export default async function SessionPage({
|
||||||
|
params,
|
||||||
|
}: {
|
||||||
|
params: Promise<{ sessionId: string }>;
|
||||||
|
}) {
|
||||||
|
const { sessionId } = await params;
|
||||||
|
return <Assistant initialSessionId={decodeURIComponent(sessionId)} />;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user