-
Notifications
You must be signed in to change notification settings - Fork 0
Add useGraphCha #11
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
Merged
Merged
Add useGraphCha #11
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
3f0017b
feat: add React integration with useGraphChat hook and utility functi…
cantemizyurek c90e9a6
refactor: optimize setActiveNodes logic in useGraphChat hook for bett…
cantemizyurek ff3580d
fix: correct TypeScript type assertion in useGraphChat hook for impro…
cantemizyurek 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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,11 @@ | ||
| --- | ||
| "ai-sdk-graph": minor | ||
| --- | ||
|
|
||
| Add React integration with `useGraphChat` hook | ||
|
|
||
| - New `ai-sdk-graph/react` export with `useGraphChat` hook that wraps `@ai-sdk/react`'s `useChat` | ||
| - Automatically handles graph-specific data parts: state changes, node start/end, and suspense events | ||
| - Exposes `state` and `activeNodes` from the hook for tracking graph execution | ||
| - Added utility functions `isGraphDataPart` and `stripGraphDataParts` to filter graph data parts from messages | ||
| - Added optional peer dependencies for `react` and `@ai-sdk/react` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,112 @@ | ||
| 'use client' | ||
|
|
||
| import { useState, useCallback, useMemo } from 'react' | ||
| import { useChat, type UseChatOptions, type UseChatHelpers } from '@ai-sdk/react' | ||
| import { | ||
| DefaultChatTransport, | ||
| type UIMessage, | ||
| type ChatInit, | ||
| type HttpChatTransportInitOptions, | ||
| } from 'ai' | ||
| import { stripGraphDataParts } from './utils' | ||
|
|
||
| export interface UseGraphChatOptions< | ||
| State extends Record<string, unknown>, | ||
| UI_MESSAGE extends UIMessage = UIMessage | ||
| > extends Omit<ChatInit<UI_MESSAGE>, 'transport' | 'onData'> { | ||
| onStateChange?: (state: State) => void | ||
| onNodeStart?: (nodeId: string) => void | ||
| onNodeEnd?: (nodeId: string) => void | ||
| onNodeSuspense?: (nodeId: string, data: unknown) => void | ||
| transportOptions?: Omit<HttpChatTransportInitOptions<UI_MESSAGE>, 'prepareSendMessagesRequest'> | ||
| prepareSendMessagesRequest?: HttpChatTransportInitOptions<UI_MESSAGE>['prepareSendMessagesRequest'] | ||
| experimental_throttle?: number | ||
| resume?: boolean | ||
| } | ||
|
|
||
| export interface UseGraphChatHelpers< | ||
| State extends Record<string, unknown>, | ||
| UI_MESSAGE extends UIMessage = UIMessage | ||
| > extends UseChatHelpers<UI_MESSAGE> { | ||
| state: State | null | ||
| activeNodes: string[] | ||
| } | ||
|
|
||
| export function useGraphChat< | ||
| State extends Record<string, unknown>, | ||
| UI_MESSAGE extends UIMessage = UIMessage | ||
| >( | ||
| options: UseGraphChatOptions<State, UI_MESSAGE> = {} | ||
| ): UseGraphChatHelpers<State, UI_MESSAGE> { | ||
| const { | ||
| onStateChange, | ||
| onNodeStart, | ||
| onNodeEnd, | ||
| onNodeSuspense, | ||
| transportOptions, | ||
| prepareSendMessagesRequest: customPrepareRequest, | ||
| ...chatInitOptions | ||
| } = options | ||
|
|
||
| const [graphState, setGraphState] = useState<State | null>(null) | ||
| const [activeNodes, setActiveNodes] = useState<string[]>([]) | ||
|
|
||
| const handleData = useCallback( | ||
| (dataPart: { type: string; data: unknown }) => { | ||
| if (dataPart.type === 'data-state') { | ||
| const newState = dataPart.data as State | ||
| setGraphState(newState) | ||
| onStateChange?.(newState) | ||
| } else if (dataPart.type === 'data-node-start') { | ||
| const nodeId = dataPart.data as string | ||
| setActiveNodes((prev) => [...prev, nodeId]) | ||
| onNodeStart?.(nodeId) | ||
| } else if (dataPart.type === 'data-node-end') { | ||
| const nodeId = dataPart.data as string | ||
| setActiveNodes((prev) => prev.filter(node => node !== nodeId)) | ||
| onNodeEnd?.(nodeId) | ||
| } else if (dataPart.type === 'data-node-suspense') { | ||
| const { nodeId, data } = dataPart.data as { nodeId: string; data: unknown } | ||
| onNodeSuspense?.(nodeId, data) | ||
| setActiveNodes([]) | ||
| } | ||
| }, | ||
| [onStateChange, onNodeStart, onNodeEnd, onNodeSuspense] | ||
| ) | ||
|
|
||
| const transport = useMemo(() => { | ||
| return new DefaultChatTransport<UI_MESSAGE>({ | ||
| ...transportOptions, | ||
| prepareSendMessagesRequest: (requestOptions) => { | ||
| const strippedMessages = stripGraphDataParts(requestOptions.messages) | ||
|
|
||
| if (customPrepareRequest) { | ||
| return customPrepareRequest({ | ||
| ...requestOptions, | ||
| messages: strippedMessages, | ||
| }) | ||
| } | ||
|
|
||
| return { | ||
| body: { | ||
| id: requestOptions.id, | ||
| messages: strippedMessages, | ||
| trigger: requestOptions.trigger, | ||
| }, | ||
| } | ||
| }, | ||
| }) | ||
| }, [transportOptions, customPrepareRequest]) | ||
|
|
||
| const chatHelpers = useChat<UI_MESSAGE>({ | ||
| ...chatInitOptions, | ||
| transport, | ||
| onData: handleData, | ||
| }) | ||
|
|
||
| return { | ||
| ...chatHelpers, | ||
| state: graphState, | ||
| activeNodes, | ||
| } | ||
| } | ||
This file contains hidden or 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
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.
🧩 Analysis chain
🌐 Web query:
ai package v6.0.48 DefaultChatTransport prepareSendMessagesRequest return shape💡 Result:
prepareSendMessagesRequest should return an object describing the HTTP request — typically some subset of { api, headers, credentials, body } (e.g. { api: string, headers?: HeadersInit, credentials?: RequestCredentials, body?: Record<string,any> }). [1][2]
Sources:
Transport implementation incomplete—missing
apifield in prepareSendMessagesRequest return.The
prepareSendMessagesRequestshould return{ api, headers?, credentials?, body? }according to DefaultChatTransport v6.0.48. The current code returns only{ body: {...} }, omitting theapifield which is required to specify the endpoint. While the message stripping and delegation flow are solid, add theapifield (or ensure it's handled elsewhere in transportOptions) to complete the transport configuration.🤖 Prompt for AI Agents