Skip to content

Commit 9fe8142

Browse files
authored
Fix an issue where Ask AI was erroring due to an object being passed as a param. (#2607)
1 parent 57cdd25 commit 9fe8142

File tree

5 files changed

+36
-31
lines changed

5 files changed

+36
-31
lines changed

.changeset/sweet-bikes-jog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'gitbook': minor
3+
---
4+
5+
Fix an issue where Ask AI was erroring due to an object being passed as a param.

packages/gitbook/src/components/Search/HighlightQuery.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ export function HighlightQuery(props: {
3030
const matches = matchString(text, query);
3131

3232
return (
33-
<div className={tcls('whitespace-break-spaces')}>
33+
<span className={tcls('whitespace-break-spaces')}>
3434
{matches.map((entry, index) => (
3535
<span key={index} className={tcls(entry.match ? highlight : null)}>
3636
{entry.text}
3737
</span>
3838
))}
39-
</div>
39+
</span>
4040
);
4141
}
4242

packages/gitbook/src/components/Search/SearchAskAnswer.tsx

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
'use client';
2+
13
import { Icon } from '@gitbook/icons';
2-
import React from 'react';
4+
import React, { useState } from 'react';
35
import { atom, useRecoilState } from 'recoil';
46

57
import { Loading } from '@/components/primitives';
@@ -14,23 +16,23 @@ import { AskAnswerResult, AskAnswerSource, streamAskQuestion } from './server-ac
1416
import { useSearch, useSearchLink } from './useSearch';
1517
import { Link } from '../primitives';
1618

17-
/**
18-
* Store the state of the answer in a global state so that it can be
19-
* accessed from anywhere to show a loading indicator.
20-
*/
21-
export const searchAskState = atom<
19+
type SearchState =
2220
| {
2321
type: 'answer';
24-
answer: AskAnswerResult | null;
22+
answer: AskAnswerResult;
2523
}
2624
| {
2725
type: 'error';
2826
}
2927
| {
3028
type: 'loading';
31-
}
32-
| null
33-
>({
29+
};
30+
31+
/**
32+
- * Store the state of the answer in a global state so that it can be
33+
- * accessed from anywhere to show a loading indicator.
34+
- */
35+
export const searchAskState = atom<SearchState | null>({
3436
key: 'searchAskState',
3537
default: null,
3638
});
@@ -44,6 +46,7 @@ export function SearchAskAnswer(props: { pointer: SiteContentPointer; query: str
4446
const language = useLanguage();
4547
const [, setSearchState] = useSearch();
4648
const [state, setState] = useRecoilState(searchAskState);
49+
const { organizationId, siteId, siteSpaceId } = pointer;
4750

4851
React.useEffect(() => {
4952
let cancelled = false;
@@ -53,7 +56,9 @@ export function SearchAskAnswer(props: { pointer: SiteContentPointer; query: str
5356
});
5457

5558
(async () => {
56-
const stream = iterateStreamResponse(streamAskQuestion(pointer, query));
59+
const stream = iterateStreamResponse(
60+
streamAskQuestion(organizationId, siteId, siteSpaceId ?? null, query),
61+
);
5762

5863
setSearchState((prev) =>
5964
prev
@@ -92,7 +97,7 @@ export function SearchAskAnswer(props: { pointer: SiteContentPointer; query: str
9297
cancelled = true;
9398
}
9499
};
95-
}, [pointer, query, setSearchState, setState]);
100+
}, [organizationId, siteId, siteSpaceId, query]);
96101

97102
React.useEffect(() => {
98103
return () => {
@@ -109,15 +114,9 @@ export function SearchAskAnswer(props: { pointer: SiteContentPointer; query: str
109114
return (
110115
<div className={tcls('max-h-[60vh]', 'overflow-y-auto')}>
111116
{state?.type === 'answer' ? (
112-
<>
113-
{state.answer ? (
114-
<React.Suspense fallback={loading}>
115-
<TransitionAnswerBody answer={state.answer} placeholder={loading} />
116-
</React.Suspense>
117-
) : (
118-
<div className={tcls('p-4')}>{t(language, 'search_ask_no_answer')}</div>
119-
)}
120-
</>
117+
<React.Suspense fallback={loading}>
118+
<TransitionAnswerBody answer={state.answer} placeholder={loading} />
119+
</React.Suspense>
121120
) : null}
122121
{state?.type === 'error' ? (
123122
<div className={tcls('p-4')}>{t(language, 'search_ask_error')}</div>

packages/gitbook/src/components/Search/SearchModal.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use client';
22

3-
import { Collection, Site } from '@gitbook/api';
43
import { Icon } from '@gitbook/icons';
54
import { AnimatePresence, motion } from 'framer-motion';
65
import { useRouter } from 'next/navigation';

packages/gitbook/src/components/Search/server-actions.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,24 +154,26 @@ export async function searchSiteSpaceContent(
154154
* Server action to ask a question in a space.
155155
*/
156156
export const streamAskQuestion = streamResponse(async function* (
157-
pointer: api.SiteContentPointer,
157+
organizationId: string,
158+
siteId: string,
159+
siteSpaceId: string | null,
158160
question: string,
159161
) {
160162
const stream = api.api().orgs.streamAskInSite(
161-
pointer.organizationId,
162-
pointer.siteId,
163+
organizationId,
164+
siteId,
163165
{
164166
question,
165-
context: pointer.siteSpaceId
167+
context: siteSpaceId
166168
? {
167-
siteSpaceId: pointer.siteSpaceId,
169+
siteSpaceId,
168170
}
169171
: undefined,
170172
scope: {
171173
mode: 'default',
172174

173175
// Include the current site space regardless.
174-
includedSiteSpaces: pointer.siteSpaceId ? [pointer.siteSpaceId] : undefined,
176+
includedSiteSpaces: siteSpaceId ? [siteSpaceId] : undefined,
175177
},
176178
},
177179
{ format: 'document' },
@@ -221,7 +223,7 @@ async function transformAnswer(
221223
const { pages } = await api.getSpaceContentData({ spaceId }, undefined);
222224
spaceData.set(spaceId, pages);
223225
},
224-
{ concurrency: 10 },
226+
{ concurrency: 3 },
225227
);
226228

227229
const sources = answer.sources

0 commit comments

Comments
 (0)