import z from 'zod';
import formatChatHistoryAsString from '../utils/formatHistory.js';
import { getSuggestionGeneratorPrompt } from '../prompts/suggestions.js';
import type { BaseLLM } from '../models/base/llm.js';
import type { ChatTurnMessage } from '../types.js';

type Input = { chatHistory: ChatTurnMessage[]; locale?: string };

export async function generateSuggestions(input: Input, llm: BaseLLM): Promise<string[]> {
  const schema = z.object({
    suggestions: z.array(z.string()).describe('List of suggested questions or prompts'),
  });

  const res = await llm.generateObject<{ suggestions: string[] }>({
    schema,
    messages: [
      { role: 'system', content: getSuggestionGeneratorPrompt(input.locale) },
      {
        role: 'user',
        content: `<chat_history>\n${formatChatHistoryAsString(input.chatHistory)}\n</chat_history>`,
      },
    ],
  });

  return res.suggestions;
}
