🎮 Текущий прогресс
- Сообщений
- {session.messages.length}
+ Сессий
+ {sessionsInfo.count}
- Локация
-
- {session.currentState.location || "Неизвестно"}
-
+ Сообщений
+ {sessionsInfo.totalMessages}
≈ Токенов
- {formatTokens(estimateTokens(session.messages))}
+ {formatTokens(sessionsInfo.totalMessages * 50)}
@@ -328,7 +324,7 @@ export default function StoryDetailPage() {
✏️ Редактировать
diff --git a/src/services/api.ts b/src/services/api.ts
index 3bc39a9..0bf03cc 100644
--- a/src/services/api.ts
+++ b/src/services/api.ts
@@ -157,12 +157,47 @@ export async function deleteStory(id: string): Promise {
import type { GameSession } from "../types";
-export async function getSession(storyId: string): Promise {
+export interface SessionListItem {
+ id: string;
+ name: string;
+ messagesCount: number;
+ createdAt: Date;
+ updatedAt: Date;
+}
+
+// Получить список сессий для истории
+export async function getSessionsList(
+ storyId: string,
+): Promise {
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 {
+ 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 {
}
}
-export async function saveSession(
+// Создать новую сессию
+export async function createSession(
storyId: string,
- session: Omit,
-): Promise {
+ name?: string,
+ playerId?: string,
+): Promise {
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,
+): Promise {
+ 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 {
+ 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";
diff --git a/src/types/index.ts b/src/types/index.ts
index 90e55c1..12e3379 100644
--- a/src/types/index.ts
+++ b/src/types/index.ts
@@ -53,6 +53,7 @@ export interface GameSession {
id?: string;
storyId: string;
playerId?: string; // ID выбранного персонажа игрока
+ name?: string; // Название сессии (например "Попытка 1", "Злой путь")
messages: ChatMessage[];
currentState: {
location: string;