import type {
	ChatCompletionSystemMessageParam,
	ChatCompletionUserMessageParam
} from 'openai/resources/index.mjs'
import type { Tool } from '../shared'
import { getDocumentationTool } from '../navigator/core'

export const CHAT_SYSTEM_PROMPT = `
You are Windmill's intelligent assistant, designed to answer questions about its functionality. It is your only purpose to help the user in the context of the windmill application.
Windmill is an open-source developer platform for building internal tools, API integrations, background jobs, workflows, and user interfaces. It offers a unified system where scripts are automatically turned into sharable UIs and can be composed into flows or embedded in custom applications.

You have access to these tools:
1. Get documentation for user requests (get_documentation)

INSTRUCTIONS:
- When user asks about something, use the get_documentation tool to retrieve accurate information about how to fulfill the user's request.
- Complete your response with precisions about how it works based on the documentation. Also drop a link to the relevant documentation if possible.
- If the user asks about something that you are unsure about, say that you are not sure about the answer and suggest to ask the question to the windmill team.

GENERAL PRINCIPLES:
- Be concise but thorough
- Maintain a friendly, professional tone
- If you encounter an error or can't complete a request, explain why and suggest alternatives
`

export const askTools: Tool<{}>[] = [getDocumentationTool]

export function prepareAskSystemMessage(customPrompt?: string): ChatCompletionSystemMessageParam {
	let content = CHAT_SYSTEM_PROMPT

	// If there's a custom prompt, append it to the system prompt
	if (customPrompt?.trim()) {
		content = `${content}\n\nUSER GIVEN INSTRUCTIONS:\n${customPrompt.trim()}`
	}

	return {
		role: 'system',
		content
	}
}

export function prepareAskUserMessage(instructions: string): ChatCompletionUserMessageParam {
	return {
		role: 'user',
		content: instructions
	}
}
