Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
useAiAssistantSidebar,
useSWRxAiAssistants,
} from '../../../../openai/client/stores/ai-assistant';
import { useSWRINFxRecentThreads } from '../../../../openai/client/stores/thread';
import { useSWRINFxRecentThreads } from '../../stores/thread';
import { AiAssistantList } from './AiAssistantList';
import { ThreadList } from './ThreadList';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,19 @@ import { useTranslation } from 'react-i18next';

import InfiniteScroll from '~/client/components/InfiniteScroll';
import { toastError, toastSuccess } from '~/client/util/toastr';
import {
useSWRINFxRecentThreads,
useSWRMUTxThreads,
} from '~/features/openai/client/stores/thread';
import { useSWRMUTxThreads } from '~/features/openai/client/stores/thread';
import loggerFactory from '~/utils/logger';

import { deleteThread } from '../../../../openai/client/services/thread';
import { useAiAssistantSidebar } from '../../../../openai/client/stores/ai-assistant';
import { useSWRINFxRecentThreads } from '../../stores/thread';

const logger = loggerFactory('growi:openai:client:components:ThreadList');

export const ThreadList: React.FC = () => {
const swrInifiniteThreads = useSWRINFxRecentThreads();
const swrInfiniteThreads = useSWRINFxRecentThreads();
const { t } = useTranslation();
const { data, mutate: mutateRecentThreads } = swrInifiniteThreads;
const { data, mutate: mutateRecentThreads } = swrInfiniteThreads;
const {
openChat,
data: aiAssistantSidebarData,
Expand All @@ -29,11 +27,9 @@ export const ThreadList: React.FC = () => {
aiAssistantSidebarData?.aiAssistantData?._id,
);

const isEmpty = data?.[0]?.paginateResult.totalDocs === 0;
const isEmpty = data?.[0]?.total === 0;
const isReachingEnd =
isEmpty ||
(data != null &&
data[data.length - 1].paginateResult.hasNextPage === false);
isEmpty || (data != null && data[data.length - 1]?.hasMore === false);

const deleteThreadHandler = useCallback(
async (aiAssistantId: string, threadRelationId: string) => {
Expand Down Expand Up @@ -70,20 +66,20 @@ export const ThreadList: React.FC = () => {
return (
<ul className="list-group">
<InfiniteScroll
swrInifiniteResponse={swrInifiniteThreads}
swrInifiniteResponse={swrInfiniteThreads}
isReachingEnd={isReachingEnd}
>
{data
?.flatMap((thread) => thread.paginateResult.docs)
?.flatMap((threadData) => threadData.threads)
.map((thread) => (
<li key={thread._id} className="list-group-item border-0 p-0">
<li key={thread.id} className="list-group-item border-0 p-0">
<button
type="button"
className="btn btn-link list-group-item-action border-0 d-flex align-items-center rounded-1"
onClick={(e) => {
e.stopPropagation();
openChat(thread.aiAssistant, thread);
}}
// onClick={(e) => {
// e.stopPropagation();
// openChat(thread.aiAssistant, thread);
// }}
onMouseDown={(e) => {
e.preventDefault();
}}
Expand All @@ -102,13 +98,13 @@ export const ThreadList: React.FC = () => {
<button
type="button"
className="btn btn-link text-secondary p-0"
onClick={(e) => {
e.stopPropagation();
deleteThreadHandler(
getIdStringForRef(thread.aiAssistant),
thread._id,
);
}}
// onClick={(e) => {
// e.stopPropagation();
// deleteThreadHandler(
// getIdStringForRef(thread.aiAssistant),
// thread._id,
// );
// }}
>
<span className="material-symbols-outlined fs-5">
delete
Expand Down
40 changes: 40 additions & 0 deletions apps/app/src/features/mastra/client/stores/thread.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import type { PaginationInfo, StorageThreadType } from '@mastra/core';
import type { SWRConfiguration } from 'swr';
import type { SWRInfiniteResponse } from 'swr/infinite';
import useSWRInfinite from 'swr/infinite';

import { apiv3Get } from '~/client/util/apiv3-client';

type PaginatedThread = PaginationInfo & { threads: StorageThreadType[] };

const getRecentThreadsKey = (
pageIndex: number,
previousPageData: PaginatedThread | null,
): [string, number, number] | null => {
if (previousPageData != null && !previousPageData.hasMore) {
return null;
}

const PER_PAGE = 20;
const page = pageIndex;

return ['/mastra/threads', page, PER_PAGE];
};

export const useSWRINFxRecentThreads = (
config?: SWRConfiguration,
): SWRInfiniteResponse<PaginatedThread, Error> => {
return useSWRInfinite(
(pageIndex, previousPageData) =>
getRecentThreadsKey(pageIndex, previousPageData),
([endpoint, page, perPage]) =>
apiv3Get<{ paginatedThread: PaginatedThread }>(endpoint, {
page,
perPage,
}).then((response) => response.data.paginatedThread),
{
...config,
initialSize: 0,
},
);
};
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

features/openai/client/stores/thread.tsx にあった実装を features/mastra に持ってきて、新設した API GET: /mastra/thresds に対してリクエストできるようにした

4 changes: 2 additions & 2 deletions apps/app/src/features/mastra/server/routes/get-threads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ export const getThreadsFactory: GetThreadsFactory = (crowi) => {
);
}

const threads = await memory.getThreadsByResourceIdPaginated({
const paginatedThread = await memory.getThreadsByResourceIdPaginated({
resourceId: req.user._id.toString(),
page: req.query.page,
perPage: req.query.perPage,
orderBy: req.query.orderBy,
sortDirection: req.query.sortDirection,
});

return res.apiv3({ threads });
return res.apiv3({ paginatedThread });
} catch (err) {
logger.error(err);
return res.apiv3Err(new ErrorV3('Failed to get threads'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ type FileSearchParameters = {
vectorStoreId: string;
};

type FileSearchResult = {
text: string;
};

export const fileSearch = async ({
prompt,
instruction,
vectorStoreId,
}: FileSearchParameters) => {
}: FileSearchParameters): Promise<FileSearchResult> => {
const openai = getOpenaiProvider();
const model = configManager.getConfig('openai:assistantModel:chat');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from 'reactstrap';

import { apiv3Post } from '~/client/util/apiv3-client';
import { useSWRINFxRecentThreads } from '~/features/mastra/client/stores/thread';
import {
type SseMessage,
SseMessageSchema,
Expand All @@ -28,7 +29,7 @@ import { ThreadType } from '../../interfaces/thread-relation';
// import { AiAssistantChatInitialView } from '../components/AiAssistant/AiAssistantSidebar/AiAssistantChatInitialView';
import { useAiAssistantSidebar } from '../stores/ai-assistant';
import { useSWRMUTxMessages } from '../stores/message';
import { useSWRINFxRecentThreads, useSWRMUTxThreads } from '../stores/thread';
import { useSWRMUTxThreads } from '../stores/thread';

type CreateThread = (
aiAssistantId: string,
Expand Down
41 changes: 2 additions & 39 deletions apps/app/src/features/openai/client/stores/thread.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import type { SWRConfiguration, SWRResponse } from 'swr';
import type { SWRResponse } from 'swr';
import useSWRImmutable from 'swr/immutable';
import type { SWRInfiniteResponse } from 'swr/infinite';
import useSWRInfinite from 'swr/infinite';
import useSWRMutation, { type SWRMutationResponse } from 'swr/mutation';

import { apiv3Get } from '~/client/util/apiv3-client';
import type {
IThreadRelationHasId,
IThreadRelationPaginate,
} from '~/features/openai/interfaces/thread-relation';
import type { IThreadRelationHasId } from '~/features/openai/interfaces/thread-relation';

const getKey = (aiAssistantId?: string) =>
aiAssistantId != null ? [`/openai/threads/${aiAssistantId}`] : null;
Expand All @@ -33,35 +28,3 @@ export const useSWRMUTxThreads = (
{ revalidate: true },
);
};

const getRecentThreadsKey = (
pageIndex: number,
previousPageData: IThreadRelationPaginate | null,
): [string, number, number] | null => {
if (previousPageData && !previousPageData.paginateResult.hasNextPage) {
return null;
}

const PER_PAGE = 20;
const page = pageIndex + 1;

return ['/openai/threads/recent', page, PER_PAGE];
};

export const useSWRINFxRecentThreads = (
config?: SWRConfiguration,
): SWRInfiniteResponse<IThreadRelationPaginate, Error> => {
return useSWRInfinite(
(pageIndex, previousPageData) =>
getRecentThreadsKey(pageIndex, previousPageData),
([endpoint, page, limit]) =>
apiv3Get<IThreadRelationPaginate>(endpoint, { page, limit }).then(
(response) => response.data,
),
{
...config,
revalidateFirstPage: false,
revalidateAll: true,
},
);
};