工具与外部系统(TypeScript)
工具接口
type Tool = (args: Record<string, any>) => Promise<any>;
const search: Tool = async ({ q }) => ({ items: [`${q}-A`, `${q}-B`] });
const fetchJson: Tool = async ({ url }) => (await (await fetch(url)).json());
调度与并发
const runTools = async (q: string) => {
const [s, j] = await Promise.all([
search({ q }),
fetchJson({ url: "https://api.example.com/data" }),
]);
return { s, j };
};