@justinechang39/maki
Version:
AI-powered CLI agent for file operations, CSV manipulation, todo management, and web content fetching using OpenRouter
64 lines (63 loc) • 2.59 kB
JavaScript
import { DynamicTool } from 'langchain/tools';
import { toolImplementations, tools } from '../tools/index.js';
export const createLangChainTools = (setMessages, formatToolResult) => {
return tools.map(tool => new DynamicTool({
name: tool.function.name,
description: tool.function.description,
func: async (input) => {
let args;
try {
args = typeof input === 'string' ? JSON.parse(input) : input;
}
catch {
args = input;
}
// Show executing state in UI (keep real-time feedback)
const toolId = `tool-${Date.now()}-${Math.random()}`;
setMessages((prev) => [
...prev,
{
role: 'assistant',
content: tool.function.name === 'think'
? args.thoughts
: tool.function.name,
isToolExecution: true,
toolName: tool.function.name,
isProcessing: true,
id: toolId
}
]);
try {
// Execute original tool implementation
const result = await toolImplementations[tool.function.name](args);
// Update UI with formatted result (keep custom formatting)
setMessages((prev) => prev.map(msg => msg.id === toolId
? {
role: 'assistant',
content: tool.function.name === 'think'
? args.thoughts
: formatToolResult(tool.function.name, args, result),
isProcessing: false,
isToolResult: tool.function.name !== 'think',
isThinking: tool.function.name === 'think',
id: toolId
}
: msg));
return JSON.stringify(result);
}
catch (error) {
// Update UI with error (keep error formatting)
setMessages((prev) => prev.map(msg => msg.id === toolId
? {
role: 'assistant',
content: `❌ ${tool.function.name} failed: ${error.message}`,
isProcessing: false,
isToolResult: true,
id: toolId
}
: msg));
throw error;
}
}
}));
};