@assistant-ui/react
Version:
TypeScript/React library for AI Chat
62 lines • 2.04 kB
JavaScript
import { resource, tapState, tapEffect } from "@assistant-ui/tap";
import { tapApi } from "../utils/tap-store/index.js";
import { tapModelContext } from "./ModelContext.js";
export const Tools = resource(({ toolkit }) => {
const [state, setState] = tapState(() => ({
tools: {},
}));
const modelContext = tapModelContext();
tapEffect(() => {
if (!toolkit)
return;
const unsubscribes = [];
// Register tool UIs (exclude symbols)
for (const [toolName, tool] of Object.entries(toolkit)) {
if (tool.render) {
unsubscribes.push(setToolUI(toolName, tool.render));
}
}
// Register tools with model context (exclude symbols)
const toolsWithoutRender = Object.entries(toolkit).reduce((acc, [name, tool]) => {
const { render, ...rest } = tool;
acc[name] = rest;
return acc;
}, {});
const modelContextProvider = {
getModelContext: () => ({
tools: toolsWithoutRender,
}),
};
unsubscribes.push(modelContext.register(modelContextProvider));
return () => {
unsubscribes.forEach((fn) => fn());
};
}, [toolkit, modelContext]);
const setToolUI = (toolName, render) => {
setState((prev) => {
return {
...prev,
tools: {
...prev.tools,
[toolName]: [...(prev.tools[toolName] ?? []), render],
},
};
});
return () => {
setState((prev) => {
return {
...prev,
tools: {
...prev.tools,
[toolName]: prev.tools[toolName]?.filter((r) => r !== render) ?? [],
},
};
});
};
};
return tapApi({
getState: () => state,
setToolUI,
});
});
//# sourceMappingURL=Tools.js.map