main
c8b91bc ยท 4 months ago 7 commits
 1package handlers
 2
 3import (
 4	"dnsense/ai"
 5	"dnsense/dns"
 6	"fmt"
 7)
 8
 9type command struct {
10	prompt string
11}
12
13var commands = map[string]command{
14	"explain": {
15		prompt: "Explain '%s' in 2-3 sentences. Be concise, technical, and clear. No markdown.",
16	},
17	"tldr": {
18		prompt: "Give a TL;DR of '%s' in one sentence, max 200 characters. No markdown.",
19	},
20	"define": {
21		prompt: "Define '%s' in one sentence like a dictionary. Max 200 characters. No markdown.",
22	},
23	"ask": {
24		prompt: "Answer this question factually and concisely in 2-3 sentences. Be precise. No markdown. No preamble. Question: '%s'",
25	},
26}
27
28func handleCMD(req *dns.Message, cmdName string, input string) *dns.Message {
29	cmd, ok := commands[cmdName]
30	if !ok {
31		return errorResponse(req, "unknown command: "+cmdName)
32	}
33
34	text, err := ai.Ask(fmt.Sprintf(cmd.prompt, input))
35	if err != nil {
36		return errorResponse(req, "ai error: "+err.Error())
37	}
38
39	rr, err := dns.NewTXTRecord(req.Questions[0].Name, 300, text)
40	if err != nil {
41		return errorResponse(req, "encoding error")
42	}
43
44	resp := baseResponse(req)
45	resp.Answers = append(resp.Answers, *rr)
46	return resp
47}