diff --git a/apps/rowboat/app/lib/copilot/copilot.ts b/apps/rowboat/app/lib/copilot/copilot.ts index 1c637339..1c8b7d36 100644 --- a/apps/rowboat/app/lib/copilot/copilot.ts +++ b/apps/rowboat/app/lib/copilot/copilot.ts @@ -1,6 +1,6 @@ import z from "zod"; import { createOpenAI } from "@ai-sdk/openai"; -import { generateObject, streamText } from "ai"; +import { generateObject, generateText, streamText } from "ai"; import { WithStringId } from "../types/types"; import { Workflow, WorkflowTool } from "../types/workflow_types"; import { CopilotChatContext, CopilotMessage } from "../types/copilot_types"; @@ -96,20 +96,44 @@ ${JSON.stringify(simplifiedDataSources)} return prompt; } -async function getDynamicToolsPrompt(userQuery: string, workflow: z.infer): Promise { +async function getDynamicToolsPrompt(messages: z.infer[], workflow: z.infer): Promise { console.log('--- [Co-pilot] Entering Dynamic Tool Creation ---'); if (!USE_COMPOSIO_TOOLS) { console.log('[Co-pilot] Dynamic tool creation is disabled.'); return ''; } + // first, check if we need to search for tools at all + const startTime = Date.now(); + const { text } = await generateText({ + model: openai(COPILOT_MODEL), + system: `Your task is to determine if a tool search is required based on the user's request. Respond with a single word: 'yes' or 'no'. + +Say 'yes' if: +- The user explicitly mentions a tool or a capability that requires a tool (e.g., "send an email", "connect to an API"). +- The user describes a goal or workflow that implies the need for external actions or data (e.g., "build an agent to manage my files"). +- The user asks for a tool to be added to the workflow. +- The user asks questions bout how to perform a task. + +Otherwise, say 'no'. Also important to remember if a tool has already been searched and is in context there is no need to search again.`, + messages, + }); + console.log("[Co-pilot] Sending the following messages to the LLM for tool search check:", JSON.stringify(messages, null, 2)); + const endTime = Date.now(); + console.log(`[Co-pilot] Tool search check took ${endTime - startTime}ms`); + console.log("[Co-pilot] LLM response:", text); + if (text.toLowerCase() !== "yes") { + console.log('[Co-pilot] No tool search needed.'); + return ''; + } + const composio = new Composio(); // Step 1: Search for relevant tool slugs console.log('[Co-pilot] 🚀 Searching for relevant tools...'); const searchResult = await composio.tools.execute('COMPOSIO_SEARCH_TOOLS', { userId: '0000-0000-0000', // hmmmmm - arguments: { use_case: userQuery }, + arguments: { use_case: messages[messages.length - 1].content }, // use last message }); if (!searchResult.successful || !Array.isArray(searchResult.data?.results)) { @@ -145,7 +169,7 @@ async function getDynamicToolsPrompt(userQuery: string, workflow: z.infer