-
Notifications
You must be signed in to change notification settings - Fork 308
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Tool / Tool Renderer Components and Hooks (#262)
- Loading branch information
Showing
9 changed files
with
141 additions
and
7 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,96 @@ | ||
--- | ||
title: Tool Renderers | ||
--- | ||
|
||
You can show a custom UI when a tool is called to let the user know what is happening. | ||
|
||
### Tool Renderer Components | ||
|
||
```tsx | ||
import { makeToolRenderer } from '@assistant-ui/react/experimental'; | ||
|
||
type WebSearchArgs = { | ||
query: string; | ||
}; | ||
|
||
type WebSearchResult = { | ||
title: string; | ||
description: string; | ||
url: string; | ||
}; | ||
|
||
const WebSearch = makeToolRenderer<WebSearchArgs, WebSearchResult>({ | ||
name: 'web_search', | ||
render: ({ part, status }) => { | ||
return ( | ||
<p>web_search({part.args.query})</p> | ||
); | ||
}, | ||
}); | ||
``` | ||
|
||
You can put this component anywhere in your app inside the `<AssistantRuntimeProvider />` component. | ||
|
||
```tsx {1, 8} | ||
import { useWebSearch } from '@/tools/useWebSearch'; | ||
|
||
const MyApp = () => { | ||
... | ||
return ( | ||
<AssistantRuntimeProvider runtime={runtime}> | ||
... | ||
<WebSearch /> | ||
... | ||
</AssistantRuntimeProvider> | ||
); | ||
}; | ||
``` | ||
|
||
### Tool Renderer Hooks | ||
|
||
|
||
```tsx | ||
import { makeToolRenderer } from '@assistant-ui/react/experimental'; | ||
|
||
type WebSearchArgs = { | ||
query: string; | ||
}; | ||
|
||
type WebSearchResult = { | ||
title: string; | ||
description: string; | ||
url: string; | ||
}; | ||
|
||
const useWebSearch = makeToolRenderer<WebSearchArgs, WebSearchResult>({ | ||
name: 'web_search', | ||
render: ({ part, status }) => { | ||
return ( | ||
<p>web_search({part.args.query})</p> | ||
); | ||
}, | ||
}); | ||
``` | ||
|
||
|
||
You can use this hook anywhere in your app inside the `<AssistantRuntimeProvider />` component. | ||
|
||
```tsx {1, 4} | ||
import { useWebSearch } from '@/tools/useWebSearch'; | ||
|
||
const MyComponent = () => { | ||
useWebSearch(); | ||
|
||
... | ||
}; | ||
|
||
const MyApp = () => { | ||
... | ||
return ( | ||
<AssistantRuntimeProvider runtime={runtime}> | ||
... | ||
<MyComponent /> | ||
... | ||
</AssistantRuntimeProvider> | ||
); | ||
}; |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export default { | ||
ToolRenderers: "Tool Renderers", | ||
Branching: "Branching", | ||
Editing: "Editing", | ||
}; |
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
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
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
"use client"; | ||
import { type AssistantToolProps, useAssistantTool } from "./useAssistantTool"; | ||
|
||
export const makeTool = <TArgs, TResult>( | ||
tool: AssistantToolProps<TArgs, TResult>, | ||
) => { | ||
const Tool = () => { | ||
useAssistantTool(tool); | ||
return null; | ||
}; | ||
return Tool; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
"use client"; | ||
import { | ||
type AssistantToolRendererProps, | ||
useAssistantToolRenderer, | ||
} from "./useAssistantToolRenderer"; | ||
|
||
export const makeToolRenderer = <TArgs, TResult>( | ||
tool: AssistantToolRendererProps<TArgs, TResult>, | ||
) => { | ||
const ToolRenderer = () => { | ||
useAssistantToolRenderer(tool); | ||
return null; | ||
}; | ||
return ToolRenderer; | ||
}; |
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
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