@aituber-onair/core
Version:
Core library for AITuber OnAir providing voice synthesis and chat processing
39 lines • 1.39 kB
JavaScript
export class ToolExecutor {
constructor() {
this.registry = new Map();
}
register(definition, fn) {
if (this.registry.has(definition.name)) {
throw new Error(`Tool '${definition.name}' already registered`);
}
this.registry.set(definition.name, { def: definition, fn });
}
async run(blocks) {
const tasks = blocks
.filter((b) => b.type === 'tool_use')
.map(async (b) => {
const entry = this.registry.get(b.name);
if (!entry) {
throw new Error(`Unhandled tool: ${b.name}`);
}
const { fn, def } = entry;
const resPromise = fn(b.input);
const result = def.config?.timeoutMs
? await Promise.race([
resPromise,
new Promise((_, rej) => setTimeout(() => rej(new Error(`${b.name} timed out`)), def.config.timeoutMs)),
])
: await resPromise;
return {
type: 'tool_result',
tool_use_id: b.id,
content: typeof result === 'string' ? result : JSON.stringify(result),
};
});
return Promise.all(tasks);
}
listDefinitions() {
return Array.from(this.registry.values()).map((r) => r.def);
}
}
//# sourceMappingURL=ToolExecutor.js.map