AI SDK 7 with React
Install matching SDK/provider packages and AI SDK 7 with its matching React package. Install/configure the backend, current grants and a published Agent first. The app supplies a host JWT and no inference key.
Connect
import { CortexRuntimeClient } from '@cortexmemory/sdk/runtime';
import { CortexRemoteTransport } from '@cortexmemory/vercel-ai-provider/remote';
import type { RuntimeObservation, RuntimeRunHandle } from '@cortexmemory/sdk/runtime';
export async function connectChat(options: {
deploymentUrl: string;
assetHttpUrl: string;
agentId: string;
agentVersion: number;
fetchToken: (request: { forceRefreshToken: boolean }) => Promise<string | null>;
onRun: (handle: RuntimeRunHandle) => void;
render: (observation: RuntimeObservation) => void;
clearPrivateViews: () => void;
}) {
const client = new CortexRuntimeClient({
deploymentUrl: options.deploymentUrl,
assetHttpUrl: options.assetHttpUrl,
fetchToken: options.fetchToken,
onAuthChange: authenticated => {
if (!authenticated) options.clearPrivateViews();
},
});
try {
await client.session();
await client.getDefinition(options.agentId, options.agentVersion);
const transport = new CortexRemoteTransport({
client, agentId: options.agentId, agentVersion: options.agentVersion,
onRun: options.onRun, onObservation: options.render,
});
return { client, transport, close: async () => {
transport.disconnect();
await client.close();
} };
} catch (error) {
await client.close();
throw error;
}
}
Create a stable connection/transport for the protected view. Call its close() on unmount/sign-out and clear all protected displays when auth or resource authority is lost.
Render
'use client';
import { useChat } from '@ai-sdk/react';
import type { CortexRemoteTransport } from '@cortexmemory/vercel-ai-provider/remote';
export function ChatView({ transport }: { transport: CortexRemoteTransport }) {
const { messages, sendMessage, status } = useChat({ transport });
return <section aria-label="Chat">
<p aria-live="polite">{status}</p>
{messages.map(message => <p key={message.id}>
{message.parts.map((part, index) => part.type === 'text'
? <span key={index}>{part.text}</span> : null)}
</p>)}
<button onClick={() => void sendMessage({ text: 'Hello' })}>Send hello</button>
</section>;
}
This minimal view shows text; a full host renders approvals and tool/artifact/media/status data parts too. onObservation exposes separate response/memory/graph states even after response completion.
sendMessage creates a new text intent. Retain message/request identity for replay after ambiguous submission. Use resumeStream to reconnect observation; transport.disconnect() closes only observation and transport.stop() requests run cancellation.
Body/header/metadata request overrides, non-text input and regeneration are rejected by this transport. Authentication belongs to the runtime token callback; models, context, tools and budgets belong to the published backend policy.
See API reference and advanced usage.