项目文件夹

文件
wehub-resource-sync e30e75b5d4
Changesets / Create Version PR (push) Has been cancelled
Deploy Shadcn Registry / Deploy Production (push) Has been cancelled
Template Metrics / LOC + Bundle Size (push) Has been cancelled
Code Quality / Oxlint + Oxfmt (push) Has been cancelled
Code Quality / Template Sync (push) Has been cancelled
Code Quality / Build Changed Packages (push) Has been cancelled
Code Quality / Test Changed Packages (push) Has been cancelled
Deploy Expo Example / Deploy Production (push) Has been cancelled
Deploy Ink Example / Deploy Production (push) Has been cancelled
Python Tests / pytest (assistant-stream, 3.10) (push) Has been cancelled
Python Tests / pytest (assistant-stream, 3.12) (push) Has been cancelled
Python Tests / pytest (assistant-ui-sync-server-api, 3.10) (push) Has been cancelled
Python Tests / pytest (assistant-ui-sync-server-api, 3.12) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:40:13 +08:00

102 行
2.4 KiB
TypeScript

"use client";
import {
AssistantRuntimeProvider,
Tools,
type AssistantTransportConnectionMetadata,
unstable_createMessageConverter as createMessageConverter,
useAui,
useAssistantTransportRuntime,
} from "@assistant-ui/react";
import {
convertLangChainMessages,
type LangChainMessage,
} from "@assistant-ui/react-langgraph";
import type { ReactNode } from "react";
import toolkit from "./toolkit";
type MyRuntimeProviderProps = {
children: ReactNode;
};
type State = {
messages: LangChainMessage[];
};
const LangChainMessageConverter = createMessageConverter(
convertLangChainMessages,
);
const converter = (
state: State,
connectionMetadata: AssistantTransportConnectionMetadata,
) => {
const optimisticStateMessages = connectionMetadata.pendingCommands.map(
(c): LangChainMessage[] => {
if (c.type === "add-message") {
return [
{
type: "human" as const,
content: [
{
type: "text" as const,
text: c.message.parts
.map((p) => (p.type === "text" ? p.text : ""))
.join("\n"),
},
],
},
];
}
return [];
},
);
const messages = [...state.messages, ...optimisticStateMessages.flat()];
return {
messages: LangChainMessageConverter.toThreadMessages(messages),
isRunning: connectionMetadata.isSending || false,
};
};
export function MyRuntimeProvider({ children }: MyRuntimeProviderProps) {
const runtime = useAssistantTransportRuntime({
initialState: {
messages: [],
},
api: process.env.NEXT_PUBLIC_API_URL || "http://localhost:8010/assistant",
converter,
headers: async () => ({
"Test-Header": "test-value",
}),
body: {
"Test-Body": "test-value",
},
prepareSendCommandsRequest: (body) => {
console.log("Assistant transport request tools:", body.tools);
return body;
},
onResponse: () => {
console.log("Response received from server");
},
onFinish: () => {
console.log("Conversation completed");
},
onError: (error: Error) => {
console.error("Assistant transport error:", error);
},
onCancel: () => {
console.log("Request cancelled");
},
});
const aui = useAui({
tools: Tools({ toolkit }),
});
return (
<AssistantRuntimeProvider aui={aui} runtime={runtime}>
{children}
</AssistantRuntimeProvider>
);
}