'use node';

/**
 * Shared helpers for vision model functionality.
 * Provides vision model configuration and agent creation.
 */

import type { LanguageModelV3 } from '@ai-sdk/provider';
import { Agent } from '@convex-dev/agent';

import { components } from '../../../_generated/api';
import { createDebugLog } from '../../../lib/debug_log';
import { renderPrompt } from '../../../lib/prompts/registry';

const debugLog = createDebugLog('DEBUG_IMAGE_ANALYSIS', '[VisionAgent]');

/**
 * Creates a vision agent for image analysis.
 * The caller must resolve the language model via ctx.runAction before calling
 * this. The caller is also responsible for passing `providerOptions` directly
 * to `agent.generateText({ providerOptions })` — the Agent constructor's
 * `providerOptions` field is `@deprecated` in `@convex-dev/agent` and not
 * used here.
 */
export function createVisionAgent(languageModel: LanguageModelV3): Agent {
  debugLog('Creating vision agent');
  return new Agent(components.agent, {
    name: 'vision-analyzer',
    languageModel,
    instructions: renderPrompt('vision.analyzer'),
    // Cap output to ensure the model has room to respond without OpenRouter's
    // low default truncating mid-answer.
    callSettings: { maxOutputTokens: 8192 },
  });
}
