2025-12-23 11:22:59 +09:00
|
|
|
import { extname, basename } from "node:path"
|
2025-12-27 23:47:59 +09:00
|
|
|
import { pathToFileURL } from "node:url"
|
2025-12-27 17:56:40 +08:00
|
|
|
import { tool, type PluginInput, type ToolDefinition } from "@opencode-ai/plugin"
|
2025-12-13 15:25:29 +09:00
|
|
|
import { LOOK_AT_DESCRIPTION, MULTIMODAL_LOOKER_AGENT } from "./constants"
|
|
|
|
|
import type { LookAtArgs } from "./types"
|
|
|
|
|
import { log } from "../../shared/logger"
|
|
|
|
|
|
2026-01-15 14:49:56 +09:00
|
|
|
interface LookAtArgsWithAlias extends LookAtArgs {
|
|
|
|
|
path?: string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function normalizeArgs(args: LookAtArgsWithAlias): LookAtArgs {
|
|
|
|
|
return {
|
|
|
|
|
file_path: args.file_path ?? args.path ?? "",
|
|
|
|
|
goal: args.goal ?? "",
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function validateArgs(args: LookAtArgs): string | null {
|
|
|
|
|
if (!args.file_path) {
|
|
|
|
|
return `Error: Missing required parameter 'file_path'. Usage: look_at(file_path="/path/to/file", goal="what to extract")`
|
|
|
|
|
}
|
|
|
|
|
if (!args.goal) {
|
|
|
|
|
return `Error: Missing required parameter 'goal'. Usage: look_at(file_path="/path/to/file", goal="what to extract")`
|
|
|
|
|
}
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-23 11:22:59 +09:00
|
|
|
function inferMimeType(filePath: string): string {
|
|
|
|
|
const ext = extname(filePath).toLowerCase()
|
|
|
|
|
const mimeTypes: Record<string, string> = {
|
|
|
|
|
".jpg": "image/jpeg",
|
|
|
|
|
".jpeg": "image/jpeg",
|
|
|
|
|
".png": "image/png",
|
|
|
|
|
".webp": "image/webp",
|
2025-12-30 10:05:54 +09:00
|
|
|
".heic": "image/heic",
|
|
|
|
|
".heif": "image/heif",
|
|
|
|
|
".mp4": "video/mp4",
|
|
|
|
|
".mpeg": "video/mpeg",
|
|
|
|
|
".mpg": "video/mpeg",
|
|
|
|
|
".mov": "video/mov",
|
|
|
|
|
".avi": "video/avi",
|
|
|
|
|
".flv": "video/x-flv",
|
|
|
|
|
".webm": "video/webm",
|
|
|
|
|
".wmv": "video/wmv",
|
|
|
|
|
".3gpp": "video/3gpp",
|
|
|
|
|
".3gp": "video/3gpp",
|
|
|
|
|
".wav": "audio/wav",
|
|
|
|
|
".mp3": "audio/mp3",
|
|
|
|
|
".aiff": "audio/aiff",
|
|
|
|
|
".aac": "audio/aac",
|
|
|
|
|
".ogg": "audio/ogg",
|
|
|
|
|
".flac": "audio/flac",
|
2025-12-23 11:22:59 +09:00
|
|
|
".pdf": "application/pdf",
|
|
|
|
|
".txt": "text/plain",
|
2025-12-30 10:05:54 +09:00
|
|
|
".csv": "text/csv",
|
|
|
|
|
".md": "text/md",
|
|
|
|
|
".html": "text/html",
|
2025-12-23 11:22:59 +09:00
|
|
|
".json": "application/json",
|
|
|
|
|
".xml": "application/xml",
|
|
|
|
|
".js": "text/javascript",
|
2025-12-30 10:05:54 +09:00
|
|
|
".py": "text/x-python",
|
2025-12-23 11:22:59 +09:00
|
|
|
}
|
|
|
|
|
return mimeTypes[ext] || "application/octet-stream"
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-27 17:56:40 +08:00
|
|
|
export function createLookAt(ctx: PluginInput): ToolDefinition {
|
2025-12-13 15:25:29 +09:00
|
|
|
return tool({
|
|
|
|
|
description: LOOK_AT_DESCRIPTION,
|
|
|
|
|
args: {
|
|
|
|
|
file_path: tool.schema.string().describe("Absolute path to the file to analyze"),
|
|
|
|
|
goal: tool.schema.string().describe("What specific information to extract from the file"),
|
|
|
|
|
},
|
2026-01-15 14:49:56 +09:00
|
|
|
async execute(rawArgs: LookAtArgs, toolContext) {
|
|
|
|
|
const args = normalizeArgs(rawArgs as LookAtArgsWithAlias)
|
|
|
|
|
const validationError = validateArgs(args)
|
|
|
|
|
if (validationError) {
|
|
|
|
|
log(`[look_at] Validation failed: ${validationError}`)
|
|
|
|
|
return validationError
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-13 15:25:29 +09:00
|
|
|
log(`[look_at] Analyzing file: ${args.file_path}, goal: ${args.goal}`)
|
|
|
|
|
|
2025-12-23 11:22:59 +09:00
|
|
|
const mimeType = inferMimeType(args.file_path)
|
|
|
|
|
const filename = basename(args.file_path)
|
|
|
|
|
|
2025-12-13 15:25:29 +09:00
|
|
|
const prompt = `Analyze this file and extract the requested information.
|
|
|
|
|
|
|
|
|
|
Goal: ${args.goal}
|
|
|
|
|
|
2025-12-23 11:22:59 +09:00
|
|
|
Provide ONLY the extracted information that matches the goal.
|
2025-12-13 15:25:29 +09:00
|
|
|
Be thorough on what was requested, concise on everything else.
|
|
|
|
|
If the requested information is not found, clearly state what is missing.`
|
|
|
|
|
|
|
|
|
|
log(`[look_at] Creating session with parent: ${toolContext.sessionID}`)
|
2026-01-13 06:27:56 +01:00
|
|
|
const parentSession = await ctx.client.session.get({
|
|
|
|
|
path: { id: toolContext.sessionID },
|
|
|
|
|
}).catch(() => null)
|
|
|
|
|
const parentDirectory = parentSession?.data?.directory ?? ctx.directory
|
|
|
|
|
|
2025-12-13 15:25:29 +09:00
|
|
|
const createResult = await ctx.client.session.create({
|
|
|
|
|
body: {
|
|
|
|
|
parentID: toolContext.sessionID,
|
|
|
|
|
title: `look_at: ${args.goal.substring(0, 50)}`,
|
|
|
|
|
},
|
2026-01-13 06:27:56 +01:00
|
|
|
query: {
|
|
|
|
|
directory: parentDirectory,
|
|
|
|
|
},
|
2025-12-13 15:25:29 +09:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (createResult.error) {
|
|
|
|
|
log(`[look_at] Session create error:`, createResult.error)
|
|
|
|
|
return `Error: Failed to create session: ${createResult.error}`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const sessionID = createResult.data.id
|
|
|
|
|
log(`[look_at] Created session: ${sessionID}`)
|
|
|
|
|
|
2025-12-23 11:22:59 +09:00
|
|
|
log(`[look_at] Sending prompt with file passthrough to session ${sessionID}`)
|
2025-12-13 15:25:29 +09:00
|
|
|
await ctx.client.session.prompt({
|
|
|
|
|
path: { id: sessionID },
|
|
|
|
|
body: {
|
|
|
|
|
agent: MULTIMODAL_LOOKER_AGENT,
|
|
|
|
|
tools: {
|
|
|
|
|
task: false,
|
|
|
|
|
call_omo_agent: false,
|
|
|
|
|
look_at: false,
|
2025-12-23 11:22:59 +09:00
|
|
|
read: false,
|
2025-12-13 15:25:29 +09:00
|
|
|
},
|
2025-12-23 11:22:59 +09:00
|
|
|
parts: [
|
|
|
|
|
{ type: "text", text: prompt },
|
2025-12-27 23:47:59 +09:00
|
|
|
{ type: "file", mime: mimeType, url: pathToFileURL(args.file_path).href, filename },
|
2025-12-23 11:22:59 +09:00
|
|
|
],
|
2025-12-13 15:25:29 +09:00
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
log(`[look_at] Prompt sent, fetching messages...`)
|
|
|
|
|
|
|
|
|
|
const messagesResult = await ctx.client.session.messages({
|
|
|
|
|
path: { id: sessionID },
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (messagesResult.error) {
|
|
|
|
|
log(`[look_at] Messages error:`, messagesResult.error)
|
|
|
|
|
return `Error: Failed to get messages: ${messagesResult.error}`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const messages = messagesResult.data
|
|
|
|
|
log(`[look_at] Got ${messages.length} messages`)
|
|
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
|
const lastAssistantMessage = messages
|
|
|
|
|
.filter((m: any) => m.info.role === "assistant")
|
|
|
|
|
.sort((a: any, b: any) => (b.info.time?.created || 0) - (a.info.time?.created || 0))[0]
|
|
|
|
|
|
|
|
|
|
if (!lastAssistantMessage) {
|
|
|
|
|
log(`[look_at] No assistant message found`)
|
|
|
|
|
return `Error: No response from multimodal-looker agent`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log(`[look_at] Found assistant message with ${lastAssistantMessage.parts.length} parts`)
|
|
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
|
const textParts = lastAssistantMessage.parts.filter((p: any) => p.type === "text")
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
|
const responseText = textParts.map((p: any) => p.text).join("\n")
|
|
|
|
|
|
|
|
|
|
log(`[look_at] Got response, length: ${responseText.length}`)
|
|
|
|
|
|
|
|
|
|
return responseText
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}
|