feat: P1 合盘/星座/问答与测测完整设计包及模拟器取证工具
落地 synastry/star/ask API 与 H5 页面,补齐 cece-frontend-re complete-design 证据文档,并加入 Android 模拟器截图抓取脚本。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
// Package deepseek calls DeepSeek OpenAI-compatible Chat Completions API.
|
||||
package deepseek
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/yuxingu/digital-psychology/apps/api/internal/config"
|
||||
)
|
||||
|
||||
// Message is one chat turn.
|
||||
type Message struct {
|
||||
Role string `json:"role"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
// Client talks to DeepSeek.
|
||||
type Client struct {
|
||||
cfg config.DeepSeekConfig
|
||||
http *http.Client
|
||||
}
|
||||
|
||||
// New builds a client from config.
|
||||
func New(cfg config.DeepSeekConfig) *Client {
|
||||
sec := cfg.TimeoutSec
|
||||
if sec <= 0 {
|
||||
sec = 60
|
||||
}
|
||||
return &Client{
|
||||
cfg: cfg,
|
||||
http: &http.Client{
|
||||
Timeout: time.Duration(sec) * time.Second,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Enabled mirrors config.
|
||||
func (c *Client) Enabled() bool {
|
||||
return c != nil && c.cfg.Enabled()
|
||||
}
|
||||
|
||||
type chatRequest struct {
|
||||
Model string `json:"model"`
|
||||
Messages []Message `json:"messages"`
|
||||
Stream bool `json:"stream"`
|
||||
}
|
||||
|
||||
type chatResponse struct {
|
||||
Choices []struct {
|
||||
Message Message `json:"message"`
|
||||
} `json:"choices"`
|
||||
Error *struct {
|
||||
Message string `json:"message"`
|
||||
} `json:"error"`
|
||||
}
|
||||
|
||||
// Chat sends messages and returns assistant text.
|
||||
func (c *Client) Chat(ctx context.Context, messages []Message) (string, error) {
|
||||
if !c.Enabled() {
|
||||
return "", fmt.Errorf("deepseek api_key not configured")
|
||||
}
|
||||
base := c.cfg.BaseURL
|
||||
if base == "" {
|
||||
base = "https://api.deepseek.com"
|
||||
}
|
||||
model := c.cfg.Model
|
||||
if model == "" {
|
||||
model = "deepseek-chat"
|
||||
}
|
||||
|
||||
body, err := json.Marshal(chatRequest{Model: model, Messages: messages, Stream: false})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, base+"/chat/completions", bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", "Bearer "+c.cfg.APIKey)
|
||||
|
||||
res, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
raw, err := io.ReadAll(io.LimitReader(res.Body, 2<<20))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
var out chatResponse
|
||||
if err := json.Unmarshal(raw, &out); err != nil {
|
||||
return "", fmt.Errorf("deepseek decode: %w body=%s", err, truncate(string(raw), 200))
|
||||
}
|
||||
if out.Error != nil && out.Error.Message != "" {
|
||||
return "", fmt.Errorf("deepseek: %s", out.Error.Message)
|
||||
}
|
||||
if res.StatusCode >= 300 {
|
||||
return "", fmt.Errorf("deepseek HTTP %d: %s", res.StatusCode, truncate(string(raw), 200))
|
||||
}
|
||||
if len(out.Choices) == 0 || strings.TrimSpace(out.Choices[0].Message.Content) == "" {
|
||||
return "", fmt.Errorf("deepseek empty choices")
|
||||
}
|
||||
return strings.TrimSpace(out.Choices[0].Message.Content), nil
|
||||
}
|
||||
|
||||
func truncate(s string, n int) string {
|
||||
if len(s) <= n {
|
||||
return s
|
||||
}
|
||||
return s[:n] + "…"
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package deepseek
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/yuxingu/digital-psychology/apps/api/internal/config"
|
||||
)
|
||||
|
||||
func TestChat_success(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Header.Get("Authorization") != "Bearer test-key" {
|
||||
t.Fatalf("auth header: %s", r.Header.Get("Authorization"))
|
||||
}
|
||||
var req chatRequest
|
||||
_ = json.NewDecoder(r.Body).Decode(&req)
|
||||
if req.Model != "deepseek-chat" {
|
||||
t.Fatalf("model=%s", req.Model)
|
||||
}
|
||||
_ = json.NewEncoder(w).Encode(chatResponse{
|
||||
Choices: []struct {
|
||||
Message Message `json:"message"`
|
||||
}{{Message: Message{Role: "assistant", Content: "你好,我是成长助手"}}},
|
||||
})
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
c := New(config.DeepSeekConfig{
|
||||
APIKey: "test-key", BaseURL: srv.URL, Model: "deepseek-chat", TimeoutSec: 5,
|
||||
})
|
||||
out, err := c.Chat(context.Background(), []Message{{Role: "user", Content: "hi"}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if out != "你好,我是成长助手" {
|
||||
t.Fatalf("got %q", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnabled(t *testing.T) {
|
||||
if New(config.DeepSeekConfig{}).Enabled() {
|
||||
t.Fatal("empty key should disable")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user