Skip to content

Commit

Permalink
Expose extra fetch options in useAssistant (#797)
Browse files Browse the repository at this point in the history
Signed-off-by: Alexis Rico <sferadev@gmail.com>
Co-authored-by: Max Leiter <max.leiter@vercel.com>
  • Loading branch information
SferaDev and MaxLeiter committed Nov 28, 2023
1 parent 9b7b708 commit a7dc746
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/green-grapes-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'ai': patch
---

experimental_useAssistant: Expose extra fetch options
20 changes: 15 additions & 5 deletions packages/core/react/use-assistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,21 @@ export type UseAssistantHelpers = {
error: undefined | unknown;
};

export type UseAssistantOptions = {
api: string;
threadId?: string | undefined;
credentials?: RequestCredentials;
headers?: Record<string, string> | Headers;
body?: object;
};

export function experimental_useAssistant({
api,
threadId: threadIdParam,
}: {
api: string;
threadId?: string | undefined;
}): UseAssistantHelpers {
credentials,
headers,
body,
}: UseAssistantOptions): UseAssistantHelpers {
const [messages, setMessages] = useState<Message[]>([]);
const [input, setInput] = useState('');
const [threadId, setThreadId] = useState<string | undefined>(undefined);
Expand Down Expand Up @@ -83,8 +91,10 @@ export function experimental_useAssistant({

const result = await fetch(api, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
credentials,
headers: { 'Content-Type': 'application/json', ...headers },
body: JSON.stringify({
...body,
// always use user-provided threadId when available:
threadId: threadIdParam ?? threadId ?? null,
message: input,
Expand Down

0 comments on commit a7dc746

Please sign in to comment.