mirror of
https://github.com/siteboon/claudecodeui.git
synced 2026-06-04 20:05:38 +08:00
refactor: move fetching messages to module
This commit is contained in:
@@ -8,8 +8,9 @@
|
||||
*/
|
||||
|
||||
import { useCallback, useMemo, useRef, useState } from 'react';
|
||||
import type { LLMProvider } from '../types/app';
|
||||
|
||||
import { authenticatedFetch } from '../utils/api';
|
||||
import type { LLMProvider } from '../types/app';
|
||||
|
||||
// ─── NormalizedMessage (mirrors server/adapters/types.js) ────────────────────
|
||||
|
||||
@@ -164,11 +165,9 @@ export function useSessionStore() {
|
||||
const has = useCallback((sessionId: string) => storeRef.current.has(sessionId), []);
|
||||
|
||||
/**
|
||||
* Fetch messages from the unified endpoint and populate serverMessages.
|
||||
* Fetch messages from the provider sessions endpoint and populate serverMessages.
|
||||
*
|
||||
* `projectId` is the DB-assigned identifier used by the backend to resolve
|
||||
* the project's on-disk directory; it replaces the legacy `projectName`
|
||||
* Claude folder encoding that callers used to pass.
|
||||
* Provider and project metadata are resolved server-side from `sessionId`.
|
||||
*/
|
||||
const fetchFromServer = useCallback(async (
|
||||
sessionId: string,
|
||||
@@ -186,16 +185,13 @@ export function useSessionStore() {
|
||||
|
||||
try {
|
||||
const params = new URLSearchParams();
|
||||
if (opts.provider) params.append('provider', opts.provider);
|
||||
if (opts.projectId) params.append('projectId', opts.projectId);
|
||||
if (opts.projectPath) params.append('projectPath', opts.projectPath);
|
||||
if (opts.limit !== null && opts.limit !== undefined) {
|
||||
params.append('limit', String(opts.limit));
|
||||
params.append('offset', String(opts.offset ?? 0));
|
||||
}
|
||||
|
||||
const qs = params.toString();
|
||||
const url = `/api/sessions/${encodeURIComponent(sessionId)}/messages${qs ? `?${qs}` : ''}`;
|
||||
const url = `/api/providers/sessions/${encodeURIComponent(sessionId)}/messages${qs ? `?${qs}` : ''}`;
|
||||
const response = await authenticatedFetch(url);
|
||||
|
||||
if (!response.ok) {
|
||||
@@ -228,9 +224,6 @@ export function useSessionStore() {
|
||||
|
||||
/**
|
||||
* Load older (paginated) messages and prepend to serverMessages.
|
||||
*
|
||||
* Accepts `projectId` (the DB primary key) so the unified messages endpoint
|
||||
* can resolve the project path through the database.
|
||||
*/
|
||||
const fetchMore = useCallback(async (
|
||||
sessionId: string,
|
||||
@@ -245,15 +238,12 @@ export function useSessionStore() {
|
||||
if (!slot.hasMore) return slot;
|
||||
|
||||
const params = new URLSearchParams();
|
||||
if (opts.provider) params.append('provider', opts.provider);
|
||||
if (opts.projectId) params.append('projectId', opts.projectId);
|
||||
if (opts.projectPath) params.append('projectPath', opts.projectPath);
|
||||
const limit = opts.limit ?? 20;
|
||||
params.append('limit', String(limit));
|
||||
params.append('offset', String(slot.offset));
|
||||
|
||||
const qs = params.toString();
|
||||
const url = `/api/sessions/${encodeURIComponent(sessionId)}/messages${qs ? `?${qs}` : ''}`;
|
||||
const url = `/api/providers/sessions/${encodeURIComponent(sessionId)}/messages${qs ? `?${qs}` : ''}`;
|
||||
|
||||
try {
|
||||
const response = await authenticatedFetch(url);
|
||||
@@ -305,14 +295,11 @@ export function useSessionStore() {
|
||||
}, [getSlot, notify]);
|
||||
|
||||
/**
|
||||
* Re-fetch serverMessages from the unified endpoint (e.g., on projects_updated).
|
||||
*
|
||||
* Uses the DB-assigned `projectId`; the legacy folder-derived projectName
|
||||
* is no longer accepted here.
|
||||
* Re-fetch serverMessages from the provider sessions endpoint.
|
||||
*/
|
||||
const refreshFromServer = useCallback(async (
|
||||
sessionId: string,
|
||||
opts: {
|
||||
_opts: {
|
||||
provider?: LLMProvider;
|
||||
projectId?: string;
|
||||
projectPath?: string;
|
||||
@@ -321,12 +308,9 @@ export function useSessionStore() {
|
||||
const slot = getSlot(sessionId);
|
||||
try {
|
||||
const params = new URLSearchParams();
|
||||
if (opts.provider) params.append('provider', opts.provider);
|
||||
if (opts.projectId) params.append('projectId', opts.projectId);
|
||||
if (opts.projectPath) params.append('projectPath', opts.projectPath);
|
||||
|
||||
const qs = params.toString();
|
||||
const url = `/api/sessions/${encodeURIComponent(sessionId)}/messages${qs ? `?${qs}` : ''}`;
|
||||
const url = `/api/providers/sessions/${encodeURIComponent(sessionId)}/messages${qs ? `?${qs}` : ''}`;
|
||||
const response = await authenticatedFetch(url);
|
||||
|
||||
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
||||
|
||||
@@ -56,20 +56,16 @@ export const api = {
|
||||
projects: () => authenticatedFetch('/api/projects'),
|
||||
projectTaskmaster: (projectId) =>
|
||||
authenticatedFetch(`/api/projects/${encodeURIComponent(projectId)}/taskmaster`),
|
||||
// Unified endpoint — all providers through one URL. The legacy `projectName`
|
||||
// query parameter is preserved on the wire (routes/messages.js still reads
|
||||
// it) but it now carries a projectId value supplied by the caller.
|
||||
unifiedSessionMessages: (sessionId, provider = 'claude', { projectId = '', projectPath = '', limit = null, offset = 0 } = {}) => {
|
||||
// Unified endpoint for persisted session messages.
|
||||
// Provider/project metadata are resolved by the backend from sessionId.
|
||||
unifiedSessionMessages: (sessionId, _provider = 'claude', { limit = null, offset = 0 } = {}) => {
|
||||
const params = new URLSearchParams();
|
||||
params.append('provider', provider);
|
||||
if (projectId) params.append('projectId', projectId);
|
||||
if (projectPath) params.append('projectPath', projectPath);
|
||||
if (limit !== null) {
|
||||
params.append('limit', String(limit));
|
||||
params.append('offset', String(offset));
|
||||
}
|
||||
const queryString = params.toString();
|
||||
return authenticatedFetch(`/api/sessions/${encodeURIComponent(sessionId)}/messages${queryString ? `?${queryString}` : ''}`);
|
||||
return authenticatedFetch(`/api/providers/sessions/${encodeURIComponent(sessionId)}/messages${queryString ? `?${queryString}` : ''}`);
|
||||
},
|
||||
renameProject: (projectId, displayName) =>
|
||||
authenticatedFetch(`/api/projects/${projectId}/rename`, {
|
||||
|
||||
Reference in New Issue
Block a user