Skip to content

Commit 1915ccc

Browse files
authored
Merge of #10497
2 parents 94b3caa + 0432252 commit 1915ccc

File tree

7 files changed

+72
-68
lines changed

7 files changed

+72
-68
lines changed

apps/app/src/features/mastra/client/components/Sidebar/AiAssistantSubstance.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
useAiAssistantSidebar,
77
useSWRxAiAssistants,
88
} from '../../../../openai/client/stores/ai-assistant';
9-
import { useSWRINFxRecentThreads } from '../../../../openai/client/stores/thread';
9+
import { useSWRINFxRecentThreads } from '../../stores/thread';
1010
import { AiAssistantList } from './AiAssistantList';
1111
import { ThreadList } from './ThreadList';
1212

apps/app/src/features/mastra/client/components/Sidebar/ThreadList.tsx

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,19 @@ import { useTranslation } from 'react-i18next';
55

66
import InfiniteScroll from '~/client/components/InfiniteScroll';
77
import { toastError, toastSuccess } from '~/client/util/toastr';
8-
import {
9-
useSWRINFxRecentThreads,
10-
useSWRMUTxThreads,
11-
} from '~/features/openai/client/stores/thread';
8+
import { useSWRMUTxThreads } from '~/features/openai/client/stores/thread';
129
import loggerFactory from '~/utils/logger';
1310

1411
import { deleteThread } from '../../../../openai/client/services/thread';
1512
import { useAiAssistantSidebar } from '../../../../openai/client/stores/ai-assistant';
13+
import { useSWRINFxRecentThreads } from '../../stores/thread';
1614

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

1917
export const ThreadList: React.FC = () => {
20-
const swrInifiniteThreads = useSWRINFxRecentThreads();
18+
const swrInfiniteThreads = useSWRINFxRecentThreads();
2119
const { t } = useTranslation();
22-
const { data, mutate: mutateRecentThreads } = swrInifiniteThreads;
20+
const { data, mutate: mutateRecentThreads } = swrInfiniteThreads;
2321
const {
2422
openChat,
2523
data: aiAssistantSidebarData,
@@ -29,11 +27,9 @@ export const ThreadList: React.FC = () => {
2927
aiAssistantSidebarData?.aiAssistantData?._id,
3028
);
3129

32-
const isEmpty = data?.[0]?.paginateResult.totalDocs === 0;
30+
const isEmpty = data?.[0]?.total === 0;
3331
const isReachingEnd =
34-
isEmpty ||
35-
(data != null &&
36-
data[data.length - 1].paginateResult.hasNextPage === false);
32+
isEmpty || (data != null && data[data.length - 1]?.hasMore === false);
3733

3834
const deleteThreadHandler = useCallback(
3935
async (aiAssistantId: string, threadRelationId: string) => {
@@ -70,20 +66,20 @@ export const ThreadList: React.FC = () => {
7066
return (
7167
<ul className="list-group">
7268
<InfiniteScroll
73-
swrInifiniteResponse={swrInifiniteThreads}
69+
swrInifiniteResponse={swrInfiniteThreads}
7470
isReachingEnd={isReachingEnd}
7571
>
7672
{data
77-
?.flatMap((thread) => thread.paginateResult.docs)
73+
?.flatMap((threadData) => threadData.threads)
7874
.map((thread) => (
79-
<li key={thread._id} className="list-group-item border-0 p-0">
75+
<li key={thread.id} className="list-group-item border-0 p-0">
8076
<button
8177
type="button"
8278
className="btn btn-link list-group-item-action border-0 d-flex align-items-center rounded-1"
83-
onClick={(e) => {
84-
e.stopPropagation();
85-
openChat(thread.aiAssistant, thread);
86-
}}
79+
// onClick={(e) => {
80+
// e.stopPropagation();
81+
// openChat(thread.aiAssistant, thread);
82+
// }}
8783
onMouseDown={(e) => {
8884
e.preventDefault();
8985
}}
@@ -102,13 +98,13 @@ export const ThreadList: React.FC = () => {
10298
<button
10399
type="button"
104100
className="btn btn-link text-secondary p-0"
105-
onClick={(e) => {
106-
e.stopPropagation();
107-
deleteThreadHandler(
108-
getIdStringForRef(thread.aiAssistant),
109-
thread._id,
110-
);
111-
}}
101+
// onClick={(e) => {
102+
// e.stopPropagation();
103+
// deleteThreadHandler(
104+
// getIdStringForRef(thread.aiAssistant),
105+
// thread._id,
106+
// );
107+
// }}
112108
>
113109
<span className="material-symbols-outlined fs-5">
114110
delete
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import type { PaginationInfo, StorageThreadType } from '@mastra/core';
2+
import type { SWRConfiguration } from 'swr';
3+
import type { SWRInfiniteResponse } from 'swr/infinite';
4+
import useSWRInfinite from 'swr/infinite';
5+
6+
import { apiv3Get } from '~/client/util/apiv3-client';
7+
8+
type PaginatedThread = PaginationInfo & { threads: StorageThreadType[] };
9+
10+
const getRecentThreadsKey = (
11+
pageIndex: number,
12+
previousPageData: PaginatedThread | null,
13+
): [string, number, number] | null => {
14+
if (previousPageData != null && !previousPageData.hasMore) {
15+
return null;
16+
}
17+
18+
const PER_PAGE = 20;
19+
const page = pageIndex;
20+
21+
return ['/mastra/threads', page, PER_PAGE];
22+
};
23+
24+
export const useSWRINFxRecentThreads = (
25+
config?: SWRConfiguration,
26+
): SWRInfiniteResponse<PaginatedThread, Error> => {
27+
return useSWRInfinite(
28+
(pageIndex, previousPageData) =>
29+
getRecentThreadsKey(pageIndex, previousPageData),
30+
([endpoint, page, perPage]) =>
31+
apiv3Get<{ paginatedThread: PaginatedThread }>(endpoint, {
32+
page,
33+
perPage,
34+
}).then((response) => response.data.paginatedThread),
35+
{
36+
...config,
37+
initialSize: 0,
38+
},
39+
);
40+
};

apps/app/src/features/mastra/server/routes/get-threads.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,15 @@ export const getThreadsFactory: GetThreadsFactory = (crowi) => {
7373
);
7474
}
7575

76-
const threads = await memory.getThreadsByResourceIdPaginated({
76+
const paginatedThread = await memory.getThreadsByResourceIdPaginated({
7777
resourceId: req.user._id.toString(),
7878
page: req.query.page,
7979
perPage: req.query.perPage,
8080
orderBy: req.query.orderBy,
8181
sortDirection: req.query.sortDirection,
8282
});
8383

84-
return res.apiv3({ threads });
84+
return res.apiv3({ paginatedThread });
8585
} catch (err) {
8686
logger.error(err);
8787
return res.apiv3Err(new ErrorV3('Failed to get threads'));

apps/app/src/features/mastra/server/services/ai-sdk-modules/file-search.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@ type FileSearchParameters = {
1010
vectorStoreId: string;
1111
};
1212

13+
type FileSearchResult = {
14+
text: string;
15+
};
16+
1317
export const fileSearch = async ({
1418
prompt,
1519
instruction,
1620
vectorStoreId,
17-
}: FileSearchParameters) => {
21+
}: FileSearchParameters): Promise<FileSearchResult> => {
1822
const openai = getOpenaiProvider();
1923
const model = configManager.getConfig('openai:assistantModel:chat');
2024

apps/app/src/features/openai/client/services/knowledge-assistant.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
} from 'reactstrap';
1212

1313
import { apiv3Post } from '~/client/util/apiv3-client';
14+
import { useSWRINFxRecentThreads } from '~/features/mastra/client/stores/thread';
1415
import {
1516
type SseMessage,
1617
SseMessageSchema,
@@ -28,7 +29,7 @@ import { ThreadType } from '../../interfaces/thread-relation';
2829
// import { AiAssistantChatInitialView } from '../components/AiAssistant/AiAssistantSidebar/AiAssistantChatInitialView';
2930
import { useAiAssistantSidebar } from '../stores/ai-assistant';
3031
import { useSWRMUTxMessages } from '../stores/message';
31-
import { useSWRINFxRecentThreads, useSWRMUTxThreads } from '../stores/thread';
32+
import { useSWRMUTxThreads } from '../stores/thread';
3233

3334
type CreateThread = (
3435
aiAssistantId: string,
Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
import type { SWRConfiguration, SWRResponse } from 'swr';
1+
import type { SWRResponse } from 'swr';
22
import useSWRImmutable from 'swr/immutable';
3-
import type { SWRInfiniteResponse } from 'swr/infinite';
4-
import useSWRInfinite from 'swr/infinite';
53
import useSWRMutation, { type SWRMutationResponse } from 'swr/mutation';
64

75
import { apiv3Get } from '~/client/util/apiv3-client';
8-
import type {
9-
IThreadRelationHasId,
10-
IThreadRelationPaginate,
11-
} from '~/features/openai/interfaces/thread-relation';
6+
import type { IThreadRelationHasId } from '~/features/openai/interfaces/thread-relation';
127

138
const getKey = (aiAssistantId?: string) =>
149
aiAssistantId != null ? [`/openai/threads/${aiAssistantId}`] : null;
@@ -33,35 +28,3 @@ export const useSWRMUTxThreads = (
3328
{ revalidate: true },
3429
);
3530
};
36-
37-
const getRecentThreadsKey = (
38-
pageIndex: number,
39-
previousPageData: IThreadRelationPaginate | null,
40-
): [string, number, number] | null => {
41-
if (previousPageData && !previousPageData.paginateResult.hasNextPage) {
42-
return null;
43-
}
44-
45-
const PER_PAGE = 20;
46-
const page = pageIndex + 1;
47-
48-
return ['/openai/threads/recent', page, PER_PAGE];
49-
};
50-
51-
export const useSWRINFxRecentThreads = (
52-
config?: SWRConfiguration,
53-
): SWRInfiniteResponse<IThreadRelationPaginate, Error> => {
54-
return useSWRInfinite(
55-
(pageIndex, previousPageData) =>
56-
getRecentThreadsKey(pageIndex, previousPageData),
57-
([endpoint, page, limit]) =>
58-
apiv3Get<IThreadRelationPaginate>(endpoint, { page, limit }).then(
59-
(response) => response.data,
60-
),
61-
{
62-
...config,
63-
revalidateFirstPage: false,
64-
revalidateAll: true,
65-
},
66-
);
67-
};

0 commit comments

Comments
 (0)