Skip to content

Commit

Permalink
fix: assistant creation and show all tools on Tools page (#156)
Browse files Browse the repository at this point in the history
Signed-off-by: Petr Bulánek <[email protected]>
  • Loading branch information
PetrBulanek authored Dec 19, 2024
1 parent 45de325 commit 436ff00
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
25 changes: 16 additions & 9 deletions src/modules/assistants/builder/AssistantBuilderProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
useCallback,
useEffect,
useMemo,
useState,
} from 'react';
import { FormProvider, useForm, UseFormReturn } from 'react-hook-form';
import {
Expand Down Expand Up @@ -96,9 +97,10 @@ interface Props {
}

export function AssistantBuilderProvider({
assistant: initialAssistant,
assistant: assistantProp,
children,
}: Props) {
const [initialAssistant, setInitialAssistant] = useState(assistantProp);
const { addToast } = useToast();
const { project, assistant } = useAppContext();
const { selectAssistant } = useAppApiContext();
Expand All @@ -123,6 +125,7 @@ export function AssistantBuilderProvider({
if (!result) return;
const assistantFromResult = decodeEntityWithMetadata<Assistant>(result);
selectAssistant(assistantFromResult);
setInitialAssistant(assistantFromResult);

if (isMdDown) {
router.push(`/${project.id}/chat/${result.id}`);
Expand Down Expand Up @@ -154,23 +157,27 @@ export function AssistantBuilderProvider({
} else {
selectAssistant(initialAssistant);
}
}, [isDuplicate, initialAssistant, selectAssistant]);

useEffect(() => {
if (assistantProp) {
setInitialAssistant(assistantProp);
}
}, [assistantProp]);

useEffect(() => {
reset(
formValuesFromAssistant(
assistantTemplate ? assistantTemplate : (initialAssistant ?? null),
assistantTemplate
? assistantTemplate
: ((assistant || initialAssistant) ?? null),
isDuplicate,
),
{
keepValues: false,
},
);
}, [
isDuplicate,
initialAssistant,
selectAssistant,
reset,
assistantTemplate,
]);
}, [isDuplicate, assistantTemplate, initialAssistant, assistant, reset]);

useEffect(() => {
if (!isEmpty(formState.dirtyFields))
Expand Down
2 changes: 1 addition & 1 deletion src/modules/tools/ToolsHome.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { ToolsList } from './ToolsList';
export function ToolsHome() {
return (
<ProjectHome>
<ToolsList type="user" />
<ToolsList />
</ProjectHome>
);
}
Expand Down
26 changes: 19 additions & 7 deletions src/modules/tools/ToolsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

'use client';
import { ToolType } from '@/app/api/threads-runs/types';
import {
Tool,
ToolResult,
Expand All @@ -26,6 +27,7 @@ import { CardsList } from '@/components/CardsList/CardsList';
import { CardsListItem } from '@/components/CardsList/CardsListItem';
import { useAppContext } from '@/layout/providers/AppProvider';
import { useModal } from '@/layout/providers/ModalProvider';
import { ProjectProvider } from '@/layout/providers/ProjectProvider';
import { InfiniteData, useQueryClient } from '@tanstack/react-query';
import { produce } from 'immer';
import { useState } from 'react';
Expand All @@ -35,10 +37,9 @@ import { useTools } from './hooks/useTools';
import { UserToolModal } from './manage/UserToolModal';
import { toolsQuery } from './queries';
import { ToolCard } from './ToolCard';
import { ProjectProvider } from '@/layout/providers/ProjectProvider';

interface Props {
type: 'user' | 'public';
type?: 'user' | 'public';
}

export function ToolsList({ type }: Props) {
Expand All @@ -49,11 +50,22 @@ export function ToolsList({ type }: Props) {

const queryClient = useQueryClient();

const publicTools = [
'code_interpreter',
'file_search',
'system',
] as ToolType[];
const userTools = ['user'] as ToolType[];

const isUserOrAllTools = !type || type === 'user';

const params: ToolsListQuery = {
type:
type === 'user'
? ['user']
: ['code_interpreter', 'file_search', 'system'],
type === 'public'
? publicTools
: type === 'user'
? userTools
: [...userTools, ...publicTools],
limit: TOOLS_PAGE_SIZE,
...order,
search: search.length ? search : undefined,
Expand Down Expand Up @@ -107,7 +119,7 @@ export function ToolsList({ type }: Props) {

return (
<CardsList<ToosListQueryOrderBy>
heading={type === 'user' ? 'Tools' : undefined}
heading={isUserOrAllTools ? 'Tools' : undefined}
totalCount={data?.totalCount ?? 0}
onSearchChange={setSearch}
onFetchNextPage={fetchNextPage}
Expand All @@ -123,7 +135,7 @@ export function ToolsList({ type }: Props) {
onChangeOrder: setOrder,
}}
newButtonProps={
type === 'user'
isUserOrAllTools
? {
title: 'New tool',
onClick: () =>
Expand Down

0 comments on commit 436ff00

Please sign in to comment.