-
Notifications
You must be signed in to change notification settings - Fork 115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Meru][Assistant Builder] Improved description suggestion #4559
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -6,6 +6,7 @@ import { | |||||
Page, | ||||||
PencilSquareIcon, | ||||||
SparklesIcon, | ||||||
Spinner2, | ||||||
} from "@dust-tt/sparkle"; | ||||||
import type { | ||||||
APIError, | ||||||
|
@@ -14,16 +15,24 @@ import type { | |||||
WorkspaceType, | ||||||
} from "@dust-tt/types"; | ||||||
import { Err, Ok } from "@dust-tt/types"; | ||||||
import React, { useCallback, useEffect, useRef, useState } from "react"; | ||||||
import React, { | ||||||
useCallback, | ||||||
useContext, | ||||||
useEffect, | ||||||
useRef, | ||||||
useState, | ||||||
} from "react"; | ||||||
|
||||||
import { AvatarPicker } from "@app/components/assistant_builder/AssistantBuilderAvatarPicker"; | ||||||
import { | ||||||
DROID_AVATAR_URLS, | ||||||
SPIRIT_AVATAR_URLS, | ||||||
} from "@app/components/assistant_builder/shared"; | ||||||
import type { AssistantBuilderState } from "@app/components/assistant_builder/types"; | ||||||
import { SendNotificationsContext } from "@app/components/sparkle/Notification"; | ||||||
import { isDevelopmentOrDustWorkspace } from "@app/lib/development"; | ||||||
import { debounce } from "@app/lib/utils/debounce"; | ||||||
import { as } from "fp-ts/lib/Option"; | ||||||
|
||||||
export function removeLeadingAt(handle: string) { | ||||||
return handle.startsWith("@") ? handle.slice(1) : handle; | ||||||
|
@@ -134,6 +143,7 @@ export default function NamingScreen({ | |||||
setEdited: (edited: boolean) => void; | ||||||
assistantHandleError: string | null; | ||||||
}) { | ||||||
const sendNotification = useContext(SendNotificationsContext); | ||||||
const [isAvatarModalOpen, setIsAvatarModalOpen] = useState(false); | ||||||
|
||||||
// Name suggestions handling | ||||||
|
@@ -168,35 +178,74 @@ export default function NamingScreen({ | |||||
]); | ||||||
|
||||||
// Description suggestions handling | ||||||
const [descriptionSuggestions, setDescriptionSuggestions] = | ||||||
useState<BuilderSuggestionsType>({ | ||||||
status: "unavailable", | ||||||
reason: "irrelevant", | ||||||
}); | ||||||
|
||||||
const [descriptionSuggestionsIndex, setDescriptionSuggestionIndex] = | ||||||
useState(0); | ||||||
const [generatingDescription, setGeneratingDescription] = useState(false); | ||||||
const [descriptionIsGenerated, setDescriptionIsGenerated] = useState(false); | ||||||
|
||||||
const updateDescriptionSuggestions = useCallback(async () => { | ||||||
const descriptionSuggestions = await getDescriptionSuggestions({ | ||||||
const suggestDescription = useCallback( | ||||||
async (fromUserClick?: boolean) => { | ||||||
setGeneratingDescription(true); | ||||||
const notifyError = fromUserClick ? sendNotification : console.log; | ||||||
const descriptionSuggestions = await getDescriptionSuggestions({ | ||||||
owner, | ||||||
instructions: builderState.instructions || "", | ||||||
name: builderState.handle || "", | ||||||
}); | ||||||
if (descriptionSuggestions.isOk()) { | ||||||
const suggestion = | ||||||
descriptionSuggestions.value.status === "ok" && | ||||||
descriptionSuggestions.value.suggestions.length > 0 | ||||||
? descriptionSuggestions.value.suggestions[0] | ||||||
: null; | ||||||
if (suggestion) { | ||||||
setBuilderState((state) => ({ | ||||||
...state, | ||||||
description: suggestion, | ||||||
})); | ||||||
setDescriptionIsGenerated(true); | ||||||
} else { | ||||||
const errorMessage = | ||||||
descriptionSuggestions.value.status === "unavailable" | ||||||
? descriptionSuggestions.value.reason || | ||||||
"No suggestions available" | ||||||
: "No suggestions available"; | ||||||
notifyError({ | ||||||
type: "error", | ||||||
title: "Error generating description suggestion.", | ||||||
description: errorMessage, | ||||||
}); | ||||||
} | ||||||
} else { | ||||||
notifyError({ | ||||||
type: "error", | ||||||
title: "Error generating description suggestion.", | ||||||
description: descriptionSuggestions.error.message, | ||||||
}); | ||||||
} | ||||||
setGeneratingDescription(false); | ||||||
}, | ||||||
[ | ||||||
owner, | ||||||
instructions: builderState.instructions || "", | ||||||
name: builderState.handle || "", | ||||||
}); | ||||||
if (descriptionSuggestions.isOk()) { | ||||||
setDescriptionSuggestions(descriptionSuggestions.value); | ||||||
} | ||||||
}, [owner, builderState.instructions, builderState.handle]); | ||||||
builderState.instructions, | ||||||
builderState.handle, | ||||||
setBuilderState, | ||||||
sendNotification, | ||||||
] | ||||||
); | ||||||
|
||||||
useEffect(() => { | ||||||
if (isDevelopmentOrDustWorkspace(owner)) { | ||||||
void updateDescriptionSuggestions(); | ||||||
if ( | ||||||
!builderState.description?.trim() && | ||||||
builderState.instructions?.trim() && | ||||||
!generatingDescription | ||||||
) { | ||||||
void suggestDescription(); | ||||||
} | ||||||
} | ||||||
}, [owner, updateDescriptionSuggestions]); | ||||||
|
||||||
const suggestionsAvailable = | ||||||
descriptionSuggestions.status === "ok" && | ||||||
descriptionSuggestions.suggestions.length > 0; | ||||||
// Here we only want to run this effect once | ||||||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||||||
}, []); | ||||||
|
||||||
return ( | ||||||
<> | ||||||
|
@@ -299,62 +348,54 @@ export default function NamingScreen({ | |||||
</div> | ||||||
<div className="flex flex-col gap-4"> | ||||||
<div> | ||||||
<Page.SectionHeader title="Description" /> | ||||||
<div className="flex gap-1"> | ||||||
<Page.SectionHeader title="Description" /> | ||||||
{generatingDescription && <Spinner2 size="sm" />} | ||||||
</div> | ||||||
<div className="text-sm font-normal text-element-700"> | ||||||
Describe for others the assistant’s purpose. | ||||||
Describe for others the assistant’s purpose.{" "} | ||||||
</div> | ||||||
</div> | ||||||
|
||||||
<div className="flex items-center gap-2"> | ||||||
<div className="flex-grow"> | ||||||
<Input | ||||||
placeholder={ | ||||||
suggestionsAvailable | ||||||
? "Click on sparkles to generate a description" | ||||||
: "Answer questions about sales, translate from English to French…" | ||||||
generatingDescription | ||||||
? "Generating description..." | ||||||
: "Click on sparkles to generate a description" | ||||||
} | ||||||
value={builderState.description} | ||||||
value={generatingDescription ? "" : builderState.description} | ||||||
onChange={(value) => { | ||||||
setEdited(true); | ||||||
setDescriptionIsGenerated(false); | ||||||
setBuilderState((state) => ({ | ||||||
...state, | ||||||
description: value, | ||||||
})); | ||||||
}} | ||||||
error={null} // TODO ? | ||||||
name="assistantDescription" | ||||||
className="text-sm" | ||||||
disabled={generatingDescription} | ||||||
/> | ||||||
</div> | ||||||
{isDevelopmentOrDustWorkspace(owner) && ( | ||||||
<IconButton | ||||||
icon={SparklesIcon} | ||||||
size="md" | ||||||
disabled={generatingDescription} | ||||||
onClick={async () => { | ||||||
if (!suggestionsAvailable) return; | ||||||
setEdited(true); | ||||||
setBuilderState((state) => ({ | ||||||
...state, | ||||||
description: | ||||||
descriptionSuggestions.suggestions[ | ||||||
descriptionSuggestionsIndex | ||||||
], | ||||||
})); | ||||||
if (descriptionSuggestionsIndex === 1) { | ||||||
await updateDescriptionSuggestions(); | ||||||
setDescriptionSuggestionIndex(0); | ||||||
} else { | ||||||
setDescriptionSuggestionIndex( | ||||||
descriptionSuggestionsIndex + 1 | ||||||
); | ||||||
if ( | ||||||
!builderState.description?.trim() || | ||||||
descriptionIsGenerated || | ||||||
confirm( | ||||||
"Heads up! This will overwrite your current description. Are you sure you want to proceed?" | ||||||
) | ||||||
) { | ||||||
await suggestDescription(); | ||||||
} | ||||||
}} | ||||||
disabled={!suggestionsAvailable} | ||||||
tooltip={ | ||||||
suggestionsAvailable | ||||||
? "Click to generate a description" | ||||||
: "Description generation not yet available" | ||||||
} | ||||||
tooltip="Click to generate a description" | ||||||
/> | ||||||
)} | ||||||
</div> | ||||||
|
@@ -373,23 +414,19 @@ async function getNamingSuggestions({ | |||||
instructions: string; | ||||||
description: string; | ||||||
}): Promise<Result<BuilderSuggestionsType, APIError>> { | ||||||
const res = await fetch(`/api/w/${owner.sId}/assistant/builder/suggestions`, { | ||||||
method: "POST", | ||||||
headers: { | ||||||
"Content-Type": "application/json", | ||||||
}, | ||||||
body: JSON.stringify({ | ||||||
type: "name", | ||||||
inputs: { instructions, description }, | ||||||
}), | ||||||
}); | ||||||
if (!res.ok) { | ||||||
return new Err({ | ||||||
type: "internal_server_error", | ||||||
message: "Failed to get suggestions", | ||||||
}); | ||||||
} | ||||||
return new Ok(await res.json()); | ||||||
return await fetchWithErr( | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
`/api/w/${owner.sId}/assistant/builder/suggestions`, | ||||||
{ | ||||||
method: "POST", | ||||||
headers: { | ||||||
"Content-Type": "application/json", | ||||||
}, | ||||||
body: JSON.stringify({ | ||||||
type: "name", | ||||||
inputs: { instructions, description }, | ||||||
}), | ||||||
} | ||||||
); | ||||||
} | ||||||
|
||||||
async function getDescriptionSuggestions({ | ||||||
|
@@ -401,7 +438,7 @@ async function getDescriptionSuggestions({ | |||||
instructions: string; | ||||||
name: string; | ||||||
}): Promise<Result<BuilderSuggestionsType, APIError>> { | ||||||
const res = await fetch(`/api/w/${owner.sId}/assistant/builder/suggestions`, { | ||||||
return fetchWithErr(`/api/w/${owner.sId}/assistant/builder/suggestions`, { | ||||||
method: "POST", | ||||||
headers: { | ||||||
"Content-Type": "application/json", | ||||||
|
@@ -411,11 +448,28 @@ async function getDescriptionSuggestions({ | |||||
inputs: { instructions, name }, | ||||||
}), | ||||||
}); | ||||||
if (!res.ok) { | ||||||
} | ||||||
|
||||||
async function fetchWithErr<T>( | ||||||
input: RequestInfo | URL, | ||||||
init?: RequestInit | ||||||
): Promise<Result<T, APIError>> { | ||||||
try { | ||||||
const res = await fetch(input, init); | ||||||
if (!res.ok) { | ||||||
return new Err({ | ||||||
type: "internal_server_error", | ||||||
message: `Failed to fetch: ${ | ||||||
res.statusText | ||||||
}\nResponse: ${await res.text()}`, | ||||||
}); | ||||||
} | ||||||
|
||||||
return new Ok((await res.json()) as T); | ||||||
} catch (e) { | ||||||
return new Err({ | ||||||
type: "internal_server_error", | ||||||
message: "Failed to get suggestions", | ||||||
message: `Failed to fetch.\nError: ${e}`, | ||||||
}); | ||||||
} | ||||||
return new Ok(await res.json()); | ||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔