Files
prompt-generator/internal/handlers/suggestions.go
T
wonder 01ecdc8c23 feat: initialize project skeleton with Go backend, database, and config
- Go module with standard library + gorilla/sessions + mysql driver
- Config: env-based configuration with .env file support
- Database: MySQL connection with auto-migration for 6 tables
- Seed data: 12 system tags with options, 8 system snippets
- Auth: session cookie middleware
- Handlers: auth, tags, snippets, builder, claude, dashboard, suggestions, settings
- LLM: OpenAI-compatible client with 30s timeout
- Docker: Dockerfile + docker-compose.yml
- .env.example with all configuration options

Co-Authored-By: Claude <noreply@anthropic.com>
2026-06-26 14:58:15 +08:00

60 lines
1.3 KiB
Go

package handlers
import (
"encoding/json"
"net/http"
"prompt-generator/internal/llm"
"prompt-generator/internal/models"
)
func GetSuggestions(w http.ResponseWriter, r *http.Request) {
var req struct {
CurrentPrompt string `json:"current_prompt"`
ClaudeMD string `json:"claude_md"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
fail(w, 400, "请求格式错误")
return
}
if req.CurrentPrompt == "" {
fail(w, 400, "当前 Prompt 不能为空")
return
}
client := llm.NewClient(cfg.LLMAPIBaseURL, cfg.LLMAPIKey, cfg.LLMModelName)
suggestions, err := client.GetSuggestions(req.CurrentPrompt, req.ClaudeMD)
if err != nil {
fail(w, 500, "获取建议失败: "+err.Error())
return
}
success(w, suggestions)
}
// parseSuggestions 尝试从 LLM 响应中解析建议列表
func parseSuggestions(data string) []models.Suggestion {
// Try to find JSON array in the response
start := -1
end := -1
for i := 0; i < len(data); i++ {
if data[i] == '[' && start == -1 {
start = i
}
if data[i] == ']' {
end = i + 1
}
}
if start == -1 || end == -1 {
return nil
}
var suggestions []models.Suggestion
if err := json.Unmarshal([]byte(data[start:end]), &suggestions); err != nil {
return nil
}
return suggestions
}