Skip to content

[Changes] Expose metadata for use in react assistant API #1

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions content/docs/07-reference/ai-sdk-ui/20-use-assistant.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ This works in conjunction with [`AssistantResponse`](./assistant-response) in th
description:
"Set the current thread ID. Specifying a thread ID will switch to that thread, if it exists. If set to 'undefined', a new thread will be created. For both cases, `threadId` will be updated with the new value and `messages` will be cleared.",
},
// Lines 117-121 have been added by Hypercontext for internal use
[
name: 'sendMessageMetadata',
type: 'boolean',
description: 'An optional flag to send metadata in the message object. Defaults to `false`.',
],
{
name: 'input',
type: 'string',
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"@playwright/test": "^1.44.1",
"chalk": "^5.3.0",
"eslint": "^7.32.0",
"eslint-config-vercel-ai": "workspace:*",
"eslint-config-vercel-ai": "tools/eslint-config",
"husky": "^8.0.0",
"lint-staged": "^15.2.4",
"next": "15.0.0-canary.23",
Expand Down Expand Up @@ -57,5 +57,6 @@
"singleQuote": true,
"arrowParens": "avoid",
"trailingComma": "all"
}
},
"workspaces": ["tools/eslint-config"]
}
4 changes: 2 additions & 2 deletions packages/ai/react/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
useCompletion as useCompletionReact,
useAssistant as useAssistantReact,
experimental_useObject as experimental_useObjectReact,
} from '@ai-sdk/react';
} from '../../react/src';

export const useChat = useChatReact;
export const useCompletion = useCompletionReact;
Expand All @@ -14,4 +14,4 @@ export type {
Message,
UseChatOptions,
UseChatHelpers,
} from '@ai-sdk/react';
} from '../../react/src';
15 changes: 13 additions & 2 deletions packages/react/src/use-assistant.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { isAbortError } from '@ai-sdk/provider-utils';
import { isAbortError } from '../../provider-utils/src';
import {
AssistantStatus,
CreateMessage,
Message,
UseAssistantOptions,
generateId,
readDataStream,
} from '@ai-sdk/ui-utils';
} from '../../ui-utils/src';
import { useCallback, useRef, useState } from 'react';

// use function to allow for mocking in tests:
Expand Down Expand Up @@ -89,6 +89,13 @@ Abort the current request immediately, keep the generated tokens if any.
* The error thrown during the assistant message processing, if any.
*/
error: undefined | Error;

/**
* Added by Hypercontext for internal use
*
* An optional flag to retrieve metadata of each message
*/
sendMessageMetadata?: boolean;
};

export function useAssistant({
Expand All @@ -99,6 +106,7 @@ export function useAssistant({
body,
onError,
fetch,
sendMessageMetadata, // Added by Hypercontext for internal use
}: UseAssistantOptions): UseAssistantHelpers {
const [messages, setMessages] = useState<Message[]>([]);
const [input, setInput] = useState('');
Expand Down Expand Up @@ -187,6 +195,7 @@ export function useAssistant({
id: value.id,
role: value.role,
content: value.content[0].text.value,
...(sendMessageMetadata ? { metadata: value.metadata} : {}), // Added by Hypercontext for internal use
},
]);
break;
Expand All @@ -202,6 +211,7 @@ export function useAssistant({
id: lastMessage.id,
role: lastMessage.role,
content: lastMessage.content + value,
metadata: lastMessage.metadata, // Added by Hypercontext for internal use
},
];
});
Expand Down Expand Up @@ -285,6 +295,7 @@ export function useAssistant({
setMessages,
threadId: currentThreadId,
setThreadId,
sendMessageMetadata, // Added by Hypercontext for internal use
input,
setInput,
handleInputChange,
Expand Down
9 changes: 9 additions & 0 deletions packages/ui-utils/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,14 @@ Text content of the message.
*/
content: string;

/**
* Added by Hypercontext for internal use
*
* If the sendMessageMetadata flag is enabled, this could be defined.
* Otherwise it should be null
*/
metadata?: unknown;

/**
* Additional attachments to be sent along with the message.
*/
Expand Down Expand Up @@ -609,6 +617,7 @@ export type AssistantMessage = {
value: string;
};
}>;
metadata?: unknown; // Added by Hypercontext for internal use
};

/*
Expand Down
7 changes: 7 additions & 0 deletions packages/ui-utils/src/use-assistant-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,11 @@ Custom fetch implementation. You can use it as a middleware to intercept request
or to provide a custom fetch implementation for e.g. testing.
*/
fetch?: FetchFunction;

/**
* Added by Hypercontext for internal use
*
* An optional flag to retrieve metadata of each message
*/
sendMessageMetadata?: boolean;
};