|
| 1 | +import { tool, type PluginInput } from "@opencode-ai/plugin" |
| 2 | +import { LOOK_AT_DESCRIPTION, MULTIMODAL_LOOKER_AGENT } from "./constants" |
| 3 | +import type { LookAtArgs } from "./types" |
| 4 | +import { log } from "../../shared/logger" |
| 5 | + |
| 6 | +export function createLookAt(ctx: PluginInput) { |
| 7 | + return tool({ |
| 8 | + description: LOOK_AT_DESCRIPTION, |
| 9 | + args: { |
| 10 | + file_path: tool.schema.string().describe("Absolute path to the file to analyze"), |
| 11 | + goal: tool.schema.string().describe("What specific information to extract from the file"), |
| 12 | + }, |
| 13 | + async execute(args: LookAtArgs, toolContext) { |
| 14 | + log(`[look_at] Analyzing file: ${args.file_path}, goal: ${args.goal}`) |
| 15 | + |
| 16 | + const prompt = `Analyze this file and extract the requested information. |
| 17 | +
|
| 18 | +File path: ${args.file_path} |
| 19 | +Goal: ${args.goal} |
| 20 | +
|
| 21 | +Read the file using the Read tool, then provide ONLY the extracted information that matches the goal. |
| 22 | +Be thorough on what was requested, concise on everything else. |
| 23 | +If the requested information is not found, clearly state what is missing.` |
| 24 | + |
| 25 | + log(`[look_at] Creating session with parent: ${toolContext.sessionID}`) |
| 26 | + const createResult = await ctx.client.session.create({ |
| 27 | + body: { |
| 28 | + parentID: toolContext.sessionID, |
| 29 | + title: `look_at: ${args.goal.substring(0, 50)}`, |
| 30 | + }, |
| 31 | + }) |
| 32 | + |
| 33 | + if (createResult.error) { |
| 34 | + log(`[look_at] Session create error:`, createResult.error) |
| 35 | + return `Error: Failed to create session: ${createResult.error}` |
| 36 | + } |
| 37 | + |
| 38 | + const sessionID = createResult.data.id |
| 39 | + log(`[look_at] Created session: ${sessionID}`) |
| 40 | + |
| 41 | + log(`[look_at] Sending prompt to session ${sessionID}`) |
| 42 | + await ctx.client.session.prompt({ |
| 43 | + path: { id: sessionID }, |
| 44 | + body: { |
| 45 | + agent: MULTIMODAL_LOOKER_AGENT, |
| 46 | + tools: { |
| 47 | + task: false, |
| 48 | + call_omo_agent: false, |
| 49 | + look_at: false, |
| 50 | + }, |
| 51 | + parts: [{ type: "text", text: prompt }], |
| 52 | + }, |
| 53 | + }) |
| 54 | + |
| 55 | + log(`[look_at] Prompt sent, fetching messages...`) |
| 56 | + |
| 57 | + const messagesResult = await ctx.client.session.messages({ |
| 58 | + path: { id: sessionID }, |
| 59 | + }) |
| 60 | + |
| 61 | + if (messagesResult.error) { |
| 62 | + log(`[look_at] Messages error:`, messagesResult.error) |
| 63 | + return `Error: Failed to get messages: ${messagesResult.error}` |
| 64 | + } |
| 65 | + |
| 66 | + const messages = messagesResult.data |
| 67 | + log(`[look_at] Got ${messages.length} messages`) |
| 68 | + |
| 69 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 70 | + const lastAssistantMessage = messages |
| 71 | + .filter((m: any) => m.info.role === "assistant") |
| 72 | + .sort((a: any, b: any) => (b.info.time?.created || 0) - (a.info.time?.created || 0))[0] |
| 73 | + |
| 74 | + if (!lastAssistantMessage) { |
| 75 | + log(`[look_at] No assistant message found`) |
| 76 | + return `Error: No response from multimodal-looker agent` |
| 77 | + } |
| 78 | + |
| 79 | + log(`[look_at] Found assistant message with ${lastAssistantMessage.parts.length} parts`) |
| 80 | + |
| 81 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 82 | + const textParts = lastAssistantMessage.parts.filter((p: any) => p.type === "text") |
| 83 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 84 | + const responseText = textParts.map((p: any) => p.text).join("\n") |
| 85 | + |
| 86 | + log(`[look_at] Got response, length: ${responseText.length}`) |
| 87 | + |
| 88 | + return responseText |
| 89 | + }, |
| 90 | + }) |
| 91 | +} |
0 commit comments