feat: 新增参考材料后端支持
- 新增 SessionMaterial 模型 - 新增 builder_session_materials 表自动迁移 - BuilderSession 增加 Materials 字段 - CreateBuilderSession/UpdateBuilderSession 支持保存材料 - GetBuilderSession 支持加载材料 - DeleteBuilderSession 支持级联删除材料 - AssemblePrompt 支持组装材料内容到 Prompt
This commit is contained in:
@@ -102,6 +102,16 @@ func AutoMigrate() error {
|
||||
PRIMARY KEY (id),
|
||||
KEY idx_session_id (builder_session_id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`,
|
||||
|
||||
`CREATE TABLE IF NOT EXISTS builder_session_materials (
|
||||
id bigint NOT NULL AUTO_INCREMENT,
|
||||
builder_session_id bigint NOT NULL,
|
||||
title varchar(255) NOT NULL DEFAULT '',
|
||||
content text NOT NULL,
|
||||
sort_order int NOT NULL DEFAULT 0,
|
||||
PRIMARY KEY (id),
|
||||
KEY idx_session_id (builder_session_id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`,
|
||||
}
|
||||
|
||||
for _, ddl := range tables {
|
||||
|
||||
@@ -47,6 +47,11 @@ func CreateBuilderSession(w http.ResponseWriter, r *http.Request) {
|
||||
SnippetID int64 `json:"snippet_id"`
|
||||
SortOrder int `json:"sort_order"`
|
||||
} `json:"snippet_ids"`
|
||||
Materials []struct {
|
||||
Title string `json:"title"`
|
||||
Content string `json:"content"`
|
||||
SortOrder int `json:"sort_order"`
|
||||
} `json:"materials"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
fail(w, 400, "请求格式错误")
|
||||
@@ -79,6 +84,12 @@ func CreateBuilderSession(w http.ResponseWriter, r *http.Request) {
|
||||
sessionID, s.SnippetID, s.SortOrder)
|
||||
}
|
||||
|
||||
// Insert materials
|
||||
for _, m := range req.Materials {
|
||||
db.DB.Exec("INSERT INTO builder_session_materials (builder_session_id, title, content, sort_order) VALUES (?, ?, ?, ?)",
|
||||
sessionID, m.Title, m.Content, m.SortOrder)
|
||||
}
|
||||
|
||||
success(w, map[string]int64{"id": sessionID})
|
||||
}
|
||||
|
||||
@@ -121,12 +132,27 @@ func GetBuilderSession(w http.ResponseWriter, r *http.Request) {
|
||||
snippetRows.Close()
|
||||
}
|
||||
|
||||
// Load materials
|
||||
materialRows, err := db.DB.Query("SELECT id, builder_session_id, title, content, sort_order FROM builder_session_materials WHERE builder_session_id=? ORDER BY sort_order", id)
|
||||
if err == nil {
|
||||
for materialRows.Next() {
|
||||
var m models.SessionMaterial
|
||||
if err := materialRows.Scan(&m.ID, &m.BuilderSessionID, &m.Title, &m.Content, &m.SortOrder); err == nil {
|
||||
s.Materials = append(s.Materials, m)
|
||||
}
|
||||
}
|
||||
materialRows.Close()
|
||||
}
|
||||
|
||||
if s.Tags == nil {
|
||||
s.Tags = []models.SessionTag{}
|
||||
}
|
||||
if s.Snippets == nil {
|
||||
s.Snippets = []models.SessionSnippet{}
|
||||
}
|
||||
if s.Materials == nil {
|
||||
s.Materials = []models.SessionMaterial{}
|
||||
}
|
||||
|
||||
success(w, s)
|
||||
}
|
||||
@@ -151,6 +177,11 @@ func UpdateBuilderSession(w http.ResponseWriter, r *http.Request) {
|
||||
SnippetID int64 `json:"snippet_id"`
|
||||
SortOrder int `json:"sort_order"`
|
||||
} `json:"snippet_ids"`
|
||||
Materials []struct {
|
||||
Title string `json:"title"`
|
||||
Content string `json:"content"`
|
||||
SortOrder int `json:"sort_order"`
|
||||
} `json:"materials"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
fail(w, 400, "请求格式错误")
|
||||
@@ -183,6 +214,13 @@ func UpdateBuilderSession(w http.ResponseWriter, r *http.Request) {
|
||||
id, s.SnippetID, s.SortOrder)
|
||||
}
|
||||
|
||||
// Replace materials
|
||||
db.DB.Exec("DELETE FROM builder_session_materials WHERE builder_session_id=?", id)
|
||||
for _, m := range req.Materials {
|
||||
db.DB.Exec("INSERT INTO builder_session_materials (builder_session_id, title, content, sort_order) VALUES (?, ?, ?, ?)",
|
||||
id, m.Title, m.Content, m.SortOrder)
|
||||
}
|
||||
|
||||
success(w, nil)
|
||||
}
|
||||
|
||||
@@ -195,12 +233,13 @@ func DeleteBuilderSession(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
db.DB.Exec("DELETE FROM builder_session_tags WHERE builder_session_id=?", id)
|
||||
db.DB.Exec("DELETE FROM builder_session_snippets WHERE builder_session_id=?", id)
|
||||
db.DB.Exec("DELETE FROM builder_session_materials WHERE builder_session_id=?", id)
|
||||
db.DB.Exec("DELETE FROM builder_sessions WHERE id=?", id)
|
||||
success(w, nil)
|
||||
}
|
||||
|
||||
// AssemblePrompt 组装最终 Prompt
|
||||
func AssemblePrompt(projectName string, customContent string, tagOptions []models.TagOption, snippets []models.Snippet, suggestions []string) string {
|
||||
func AssemblePrompt(projectName string, customContent string, tagOptions []models.TagOption, snippets []models.Snippet, suggestions []string, materials []models.SessionMaterial) string {
|
||||
var parts []string
|
||||
|
||||
// Project context
|
||||
@@ -231,6 +270,19 @@ func AssemblePrompt(projectName string, customContent string, tagOptions []model
|
||||
parts = append(parts, fmt.Sprintf("## 参考片段\n\n%s", strings.Join(snippetParts, "\n\n")))
|
||||
}
|
||||
|
||||
// Materials
|
||||
if len(materials) > 0 {
|
||||
var materialParts []string
|
||||
for i, m := range materials {
|
||||
title := m.Title
|
||||
if title == "" {
|
||||
title = fmt.Sprintf("参考材料%d", i+1)
|
||||
}
|
||||
materialParts = append(materialParts, fmt.Sprintf("--- %s ---\n%s", title, m.Content))
|
||||
}
|
||||
parts = append(parts, fmt.Sprintf("## 参考材料\n\n%s", strings.Join(materialParts, "\n\n")))
|
||||
}
|
||||
|
||||
// Suggestions
|
||||
if len(suggestions) > 0 {
|
||||
parts = append(parts, fmt.Sprintf("## 补充建议\n\n%s", strings.Join(suggestions, "\n")))
|
||||
|
||||
@@ -43,6 +43,7 @@ type BuilderSession struct {
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
Tags []SessionTag `json:"tags,omitempty"`
|
||||
Snippets []SessionSnippet `json:"snippets,omitempty"`
|
||||
Materials []SessionMaterial `json:"materials,omitempty"`
|
||||
}
|
||||
|
||||
// SessionTag 构建会话标签关联
|
||||
@@ -61,6 +62,15 @@ type SessionSnippet struct {
|
||||
SortOrder int `json:"sort_order"`
|
||||
}
|
||||
|
||||
// SessionMaterial 构建会话参考材料
|
||||
type SessionMaterial struct {
|
||||
ID int64 `json:"id"`
|
||||
BuilderSessionID int64 `json:"builder_session_id"`
|
||||
Title string `json:"title"`
|
||||
Content string `json:"content"`
|
||||
SortOrder int `json:"sort_order"`
|
||||
}
|
||||
|
||||
// Prompt prompts 表记录(只读)
|
||||
type Prompt struct {
|
||||
ID int64 `json:"id"`
|
||||
|
||||
Reference in New Issue
Block a user