feat: multiple sessions per story with streaming AI responses
This commit is contained in:
+93
-5
@@ -157,12 +157,47 @@ export async function deleteStory(id: string): Promise<boolean> {
|
||||
|
||||
import type { GameSession } from "../types";
|
||||
|
||||
export async function getSession(storyId: string): Promise<GameSession | null> {
|
||||
export interface SessionListItem {
|
||||
id: string;
|
||||
name: string;
|
||||
messagesCount: number;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
||||
// Получить список сессий для истории
|
||||
export async function getSessionsList(
|
||||
storyId: string,
|
||||
): Promise<SessionListItem[]> {
|
||||
try {
|
||||
const response = await fetch(`${API_URL}/api/sessions/${storyId}`, {
|
||||
credentials: "include",
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return await response.json();
|
||||
} catch (error) {
|
||||
console.error("Failed to get sessions list:", error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
// Получить конкретную сессию
|
||||
export async function getSession(
|
||||
storyId: string,
|
||||
sessionId: string,
|
||||
): Promise<GameSession | null> {
|
||||
try {
|
||||
const response = await fetch(
|
||||
`${API_URL}/api/sessions/${storyId}/${sessionId}`,
|
||||
{
|
||||
credentials: "include",
|
||||
},
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
return null;
|
||||
}
|
||||
@@ -174,10 +209,12 @@ export async function getSession(storyId: string): Promise<GameSession | null> {
|
||||
}
|
||||
}
|
||||
|
||||
export async function saveSession(
|
||||
// Создать новую сессию
|
||||
export async function createSession(
|
||||
storyId: string,
|
||||
session: Omit<GameSession, "storyId">,
|
||||
): Promise<boolean> {
|
||||
name?: string,
|
||||
playerId?: string,
|
||||
): Promise<SessionListItem | null> {
|
||||
try {
|
||||
const response = await fetch(`${API_URL}/api/sessions/${storyId}`, {
|
||||
method: "POST",
|
||||
@@ -185,9 +222,39 @@ export async function saveSession(
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
credentials: "include",
|
||||
body: JSON.stringify(session),
|
||||
body: JSON.stringify({ name, playerId }),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return await response.json();
|
||||
} catch (error) {
|
||||
console.error("Failed to create session:", error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// Сохранить/обновить сессию
|
||||
export async function saveSession(
|
||||
storyId: string,
|
||||
sessionId: string,
|
||||
session: Omit<GameSession, "storyId">,
|
||||
): Promise<boolean> {
|
||||
try {
|
||||
const response = await fetch(
|
||||
`${API_URL}/api/sessions/${storyId}/${sessionId}`,
|
||||
{
|
||||
method: "PUT",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
credentials: "include",
|
||||
body: JSON.stringify(session),
|
||||
},
|
||||
);
|
||||
|
||||
return response.ok;
|
||||
} catch (error) {
|
||||
console.error("Failed to save session:", error);
|
||||
@@ -195,6 +262,27 @@ export async function saveSession(
|
||||
}
|
||||
}
|
||||
|
||||
// Удалить сессию
|
||||
export async function deleteSession(
|
||||
storyId: string,
|
||||
sessionId: string,
|
||||
): Promise<boolean> {
|
||||
try {
|
||||
const response = await fetch(
|
||||
`${API_URL}/api/sessions/${storyId}/${sessionId}`,
|
||||
{
|
||||
method: "DELETE",
|
||||
credentials: "include",
|
||||
},
|
||||
);
|
||||
|
||||
return response.ok;
|
||||
} catch (error) {
|
||||
console.error("Failed to delete session:", error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// ============ PLAYER CHARACTERS ============
|
||||
|
||||
import type { PlayerCharacter } from "../types";
|
||||
|
||||
Reference in New Issue
Block a user