f5276431ea
Visitors can ask questions about the CV via a floating chat panel. The agent uses Gemini to answer questions about experience, projects, skills, and education by querying the cached CV JSON data. - internal/chat/agent.go: LLM agent with query_cv tool that searches CV data by section (experience, projects, skills, etc.) with keyword filtering - internal/chat/handler.go: POST /api/chat endpoint with session management, graceful degradation when GOOGLE_API_KEY is not set - chat-widget.html: HTMX-powered floating chat panel with Hyperscript toggle - _chat.css: Responsive chat UI with dark theme support - Wired into existing architecture via dependency injection (CVHandler, routes, main.go) — zero breaking changes, all existing tests pass
245 lines
7.2 KiB
Go
245 lines
7.2 KiB
Go
// Package chat provides an ADK Go agent that answers questions about CV data.
|
|
package chat
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/juanatsap/cv-site/internal/cache"
|
|
cvmodel "github.com/juanatsap/cv-site/internal/models/cv"
|
|
|
|
"google.golang.org/adk/agent"
|
|
"google.golang.org/adk/agent/llmagent"
|
|
"google.golang.org/adk/model"
|
|
"google.golang.org/adk/tool"
|
|
"google.golang.org/adk/tool/functiontool"
|
|
)
|
|
|
|
// NewAgent creates the CV chat agent with a query tool that reads from the data cache.
|
|
func NewAgent(llm model.LLM, dataCache *cache.DataCache) (agent.Agent, error) {
|
|
queryTool, err := newQueryCVTool(dataCache)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("query_cv tool: %w", err)
|
|
}
|
|
|
|
return llmagent.New(llmagent.Config{
|
|
Name: "cv_assistant",
|
|
Model: llm,
|
|
Description: "Answers questions about Juan Andrés Moreno Rubio's CV and professional experience.",
|
|
Instruction: `You are a helpful assistant embedded in a professional CV website.
|
|
You answer questions about the CV owner's experience, projects, skills, education, and career.
|
|
|
|
RULES:
|
|
- Use the query_cv tool to look up CV data before answering. Never make up information.
|
|
- Answer in the SAME LANGUAGE the user writes in. If they ask in Spanish, answer in Spanish.
|
|
- Be concise and direct — visitors want quick answers, not essays.
|
|
- When listing items (projects, technologies, companies), use bullet points.
|
|
- If the query_cv tool returns no results for a question, say so honestly.
|
|
- You may reference sections of the CV (e.g., "See the Projects section") to guide the visitor.
|
|
- Never reveal personal contact details (email, phone) — just point them to the contact form.
|
|
- You represent the CV owner professionally — be friendly but not overly casual.
|
|
|
|
EXAMPLES of questions you might receive:
|
|
- "How many years of experience does Juan have?"
|
|
- "What Go projects has he built?"
|
|
- "Has he worked with React?"
|
|
- "Tell me about his time at Olympic Broadcasting"
|
|
- "What certifications does he have?"`,
|
|
Tools: []tool.Tool{queryTool},
|
|
})
|
|
}
|
|
|
|
// QueryCVArgs is the input for the CV query tool.
|
|
type QueryCVArgs struct {
|
|
Section string `json:"section" jsonschema:"CV section to query: 'experience', 'projects', 'skills', 'education', 'languages', 'certifications', 'courses', 'awards', 'summary', 'all'"`
|
|
Query string `json:"query" jsonschema:"Search term to filter results (e.g. 'Go', 'React', '2019', 'Olympic'). Empty returns all items in the section."`
|
|
Language string `json:"language" jsonschema:"Language for CV data: 'en' or 'es'. Default: 'en'."`
|
|
}
|
|
|
|
// QueryCVResult contains the query results.
|
|
type QueryCVResult struct {
|
|
Section string `json:"section"`
|
|
Query string `json:"query,omitempty"`
|
|
TotalFound int `json:"total_found"`
|
|
Data string `json:"data"` // JSON-encoded results
|
|
}
|
|
|
|
func newQueryCVTool(dataCache *cache.DataCache) (tool.Tool, error) {
|
|
return functiontool.New(functiontool.Config{
|
|
Name: "query_cv",
|
|
Description: `Query the CV data to answer questions about experience, projects, skills, education, certifications, and more.
|
|
Use the 'section' parameter to target a specific area, and 'query' to filter by keyword.
|
|
Always call this tool before answering CV-related questions.`,
|
|
}, func(ctx tool.Context, args QueryCVArgs) (QueryCVResult, error) {
|
|
lang := args.Language
|
|
if lang == "" {
|
|
lang = "en"
|
|
}
|
|
|
|
cv := dataCache.GetCV(lang)
|
|
if cv == nil {
|
|
return QueryCVResult{Section: args.Section, TotalFound: 0, Data: "[]"}, nil
|
|
}
|
|
|
|
q := strings.ToLower(args.Query)
|
|
result := QueryCVResult{Section: args.Section, Query: args.Query}
|
|
|
|
switch args.Section {
|
|
case "summary":
|
|
result.Data = fmt.Sprintf(`{"summary": %q, "years_of_experience": %d}`,
|
|
cv.Summary, calculateYears())
|
|
result.TotalFound = 1
|
|
|
|
case "experience":
|
|
matches := filterExperience(cv.Experience, q)
|
|
result.TotalFound = len(matches)
|
|
result.Data = mustJSON(matches)
|
|
|
|
case "projects":
|
|
matches := filterProjects(cv.Projects, q)
|
|
result.TotalFound = len(matches)
|
|
result.Data = mustJSON(matches)
|
|
|
|
case "skills":
|
|
matches := filterSkills(cv.Skills, q)
|
|
result.TotalFound = len(matches)
|
|
result.Data = mustJSON(matches)
|
|
|
|
case "education":
|
|
result.TotalFound = len(cv.Education)
|
|
result.Data = mustJSON(cv.Education)
|
|
|
|
case "languages":
|
|
result.TotalFound = len(cv.Languages)
|
|
result.Data = mustJSON(cv.Languages)
|
|
|
|
case "certifications":
|
|
result.TotalFound = len(cv.Certifications)
|
|
result.Data = mustJSON(cv.Certifications)
|
|
|
|
case "courses":
|
|
matches := filterCourses(cv.Courses, q)
|
|
result.TotalFound = len(matches)
|
|
result.Data = mustJSON(matches)
|
|
|
|
case "awards":
|
|
result.TotalFound = len(cv.Awards)
|
|
result.Data = mustJSON(cv.Awards)
|
|
|
|
case "all":
|
|
// Return a high-level overview
|
|
overview := map[string]int{
|
|
"experience_count": len(cv.Experience),
|
|
"project_count": len(cv.Projects),
|
|
"skill_categories": len(cv.Skills.Technical),
|
|
"language_count": len(cv.Languages),
|
|
"certification_count": len(cv.Certifications),
|
|
"course_count": len(cv.Courses),
|
|
"award_count": len(cv.Awards),
|
|
}
|
|
result.TotalFound = 1
|
|
result.Data = mustJSON(overview)
|
|
|
|
default:
|
|
result.Data = `{"error": "unknown section"}`
|
|
}
|
|
|
|
return result, nil
|
|
})
|
|
}
|
|
|
|
// Filter helpers — match by keyword across relevant fields
|
|
|
|
func filterExperience(items []cvmodel.Experience, q string) []cvmodel.Experience {
|
|
if q == "" {
|
|
return items
|
|
}
|
|
var out []cvmodel.Experience
|
|
for _, e := range items {
|
|
if matchesAny(q, e.Company, e.Position, e.Location, e.StartDate, e.EndDate, e.ShortDescription) ||
|
|
matchesSlice(q, e.Technologies) || matchesSlice(q, e.Responsibilities) {
|
|
out = append(out, e)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func filterProjects(items []cvmodel.Project, q string) []cvmodel.Project {
|
|
if q == "" {
|
|
return items
|
|
}
|
|
var out []cvmodel.Project
|
|
for _, p := range items {
|
|
if matchesAny(q, p.Title, p.ShortDescription, p.Location) ||
|
|
matchesSlice(q, p.Technologies) || matchesSlice(q, p.Responsibilities) {
|
|
out = append(out, p)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func filterSkills(skills cvmodel.Skills, q string) []cvmodel.SkillCategory {
|
|
if q == "" {
|
|
return skills.Technical
|
|
}
|
|
var out []cvmodel.SkillCategory
|
|
for _, cat := range skills.Technical {
|
|
if matchesAny(q, cat.Category) || matchesSlice(q, cat.Items) {
|
|
out = append(out, cat)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func filterCourses(items []cvmodel.Course, q string) []cvmodel.Course {
|
|
if q == "" {
|
|
return items
|
|
}
|
|
var out []cvmodel.Course
|
|
for _, c := range items {
|
|
if matchesAny(q, c.Title, c.Institution, c.Description) {
|
|
out = append(out, c)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func matchesAny(q string, fields ...string) bool {
|
|
for _, f := range fields {
|
|
if strings.Contains(strings.ToLower(f), q) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func matchesSlice(q string, items []string) bool {
|
|
for _, item := range items {
|
|
if strings.Contains(strings.ToLower(item), q) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func mustJSON(v any) string {
|
|
b, err := json.Marshal(v)
|
|
if err != nil {
|
|
return "[]"
|
|
}
|
|
return string(b)
|
|
}
|
|
|
|
func calculateYears() int {
|
|
firstDay := time.Date(2005, time.April, 1, 0, 0, 0, 0, time.UTC)
|
|
now := time.Now()
|
|
years := now.Year() - firstDay.Year()
|
|
if now.Month() < firstDay.Month() ||
|
|
(now.Month() == firstDay.Month() && now.Day() < firstDay.Day()) {
|
|
years--
|
|
}
|
|
return years
|
|
}
|