Skip to content

Commit 4b5784d

Browse files
committed
refactor: update CodeSnippet and workspace handling for improved clarity and structure
1 parent 5bdf01d commit 4b5784d

8 files changed

Lines changed: 145 additions & 125 deletions

File tree

backend/docs/swagger.json

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3855,15 +3855,12 @@
38553855
"domain.CodeSnippet": {
38563856
"type": "object",
38573857
"properties": {
3858-
"container_name": {
3859-
"description": "容器名称",
3860-
"type": "string"
3861-
},
3862-
"content": {
3863-
"description": "代码片段内容",
3864-
"type": "string"
3858+
"definition": {
3859+
"description": "结构化信息",
3860+
"type": "object",
3861+
"additionalProperties": {}
38653862
},
3866-
"definition_text": {
3863+
"definitionText": {
38673864
"description": "定义文本",
38683865
"type": "string"
38693866
},
@@ -3874,18 +3871,26 @@
38743871
"type": "string"
38753872
}
38763873
},
3877-
"end_column": {
3874+
"endColumn": {
38783875
"description": "结束列号",
38793876
"type": "integer"
38803877
},
3881-
"end_line": {
3878+
"endLine": {
38823879
"description": "结束行号",
38833880
"type": "integer"
38843881
},
3885-
"hash": {
3882+
"field": {
3883+
"description": "容器名称",
3884+
"type": "string"
3885+
},
3886+
"fileHash": {
38863887
"description": "内容哈希",
38873888
"type": "string"
38883889
},
3890+
"filePath": {
3891+
"description": "文件路径",
3892+
"type": "string"
3893+
},
38893894
"id": {
38903895
"description": "代码片段ID",
38913896
"type": "string"
@@ -3910,6 +3915,10 @@
39103915
"additionalProperties": {}
39113916
}
39123917
},
3918+
"rangeText": {
3919+
"description": "代码片段内容",
3920+
"type": "string"
3921+
},
39133922
"scope": {
39143923
"description": "作用域信息",
39153924
"type": "array",
@@ -3921,22 +3930,17 @@
39213930
"description": "函数签名",
39223931
"type": "string"
39233932
},
3924-
"snippet_type": {
3925-
"description": "代码片段类型",
3926-
"type": "string"
3927-
},
3928-
"start_column": {
3933+
"startColumn": {
39293934
"description": "起始列号",
39303935
"type": "integer"
39313936
},
3932-
"start_line": {
3937+
"startLine": {
39333938
"description": "起始行号",
39343939
"type": "integer"
39353940
},
3936-
"structured_info": {
3937-
"description": "结构化信息",
3938-
"type": "object",
3939-
"additionalProperties": {}
3941+
"type": {
3942+
"description": "代码片段类型",
3943+
"type": "string"
39403944
},
39413945
"workspace_file_id": {
39423946
"description": "关联的workspace file ID",
@@ -4484,12 +4488,7 @@
44844488
"rangeText": {
44854489
"type": "string"
44864490
},
4487-
"scope": {
4488-
"type": "array",
4489-
"items": {
4490-
"type": "object"
4491-
}
4492-
},
4491+
"scope": {},
44934492
"signature": {
44944493
"type": "string"
44954494
},
@@ -5724,7 +5723,7 @@
57245723
}
57255724
]
57265725
},
5727-
"workspace_path": {
5726+
"workspacePath": {
57285727
"description": "工作区路径(必填)",
57295728
"type": "string"
57305729
}
@@ -5741,7 +5740,7 @@
57415740
"description": "代码片段名称(可选)",
57425741
"type": "string"
57435742
},
5744-
"type": {
5743+
"snippetType": {
57455744
"description": "代码片段类型(可选)",
57465745
"type": "string"
57475746
}

backend/domain/codesnippet.go

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,23 @@ type CodeSnippet struct {
5353
ID string `json:"id"` // 代码片段ID
5454
WorkspaceFileID string `json:"workspace_file_id"` // 关联的workspace file ID
5555
Name string `json:"name"` // 代码片段名称
56-
SnippetType string `json:"snippet_type"` // 代码片段类型
56+
SnippetType string `json:"type"` // 代码片段类型
5757
Language string `json:"language"` // 编程语言
58-
Content string `json:"content"` // 代码片段内容
59-
Hash string `json:"hash"` // 内容哈希
60-
StartLine int `json:"start_line"` // 起始行号
61-
EndLine int `json:"end_line"` // 结束行号
62-
StartColumn int `json:"start_column"` // 起始列号
63-
EndColumn int `json:"end_column"` // 结束列号
58+
Content string `json:"rangeText"` // 代码片段内容
59+
Hash string `json:"fileHash"` // 内容哈希
60+
StartLine int `json:"startLine"` // 起始行号
61+
EndLine int `json:"endLine"` // 结束行号
62+
StartColumn int `json:"startColumn"` // 起始列号
63+
EndColumn int `json:"endColumn"` // 结束列号
64+
FilePath string `json:"filePath"` // 文件路径
6465
Namespace string `json:"namespace"` // 命名空间
65-
ContainerName string `json:"container_name"` // 容器名称
66+
ContainerName string `json:"field"` // 容器名称
6667
Scope []string `json:"scope"` // 作用域信息
6768
Dependencies []string `json:"dependencies"` // 依赖项
6869
Parameters []map[string]any `json:"parameters"` // 参数列表
6970
Signature string `json:"signature"` // 函数签名
70-
DefinitionText string `json:"definition_text"` // 定义文本
71-
StructuredInfo map[string]any `json:"structured_info"` // 结构化信息
71+
DefinitionText string `json:"definitionText"` // 定义文本
72+
StructuredInfo map[string]any `json:"definition"` // 结构化信息
7273
}
7374

7475
func (c *CodeSnippet) From(e *db.CodeSnippet) *CodeSnippet {
@@ -99,6 +100,22 @@ func (c *CodeSnippet) From(e *db.CodeSnippet) *CodeSnippet {
99100
return c
100101
}
101102

103+
// 从包含WorkspaceFile信息的CodeSnippet创建domain对象
104+
func (c *CodeSnippet) FromWithFile(e *db.CodeSnippet) *CodeSnippet {
105+
if e == nil {
106+
return c
107+
}
108+
109+
c.From(e)
110+
111+
// 设置文件路径
112+
if e.Edges.SourceFile != nil {
113+
c.FilePath = e.Edges.SourceFile.Path
114+
}
115+
116+
return c
117+
}
118+
102119
// 工具函数
103120
func FromCodeSnippets(snippets []*db.CodeSnippet) []*CodeSnippet {
104121
result := make([]*CodeSnippet, len(snippets))

backend/domain/workspace.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -206,15 +206,15 @@ type FileMeta struct {
206206
Content string `json:"content"` // 文件内容(可选)
207207
}
208208
type IndexResult struct {
209-
Name string `json:"name"`
210-
Type string `json:"type"`
211-
FilePath string `json:"filePath"`
212-
StartLine int `json:"startLine"`
213-
EndLine int `json:"endLine"`
214-
RangeText string `json:"rangeText"`
215-
DefinitionText string `json:"definitionText"`
216-
Scope []struct{} `json:"scope"`
217-
FileHash string `json:"fileHash"`
209+
Name string `json:"name"`
210+
Type string `json:"type"`
211+
FilePath string `json:"filePath"`
212+
StartLine int `json:"startLine"`
213+
EndLine int `json:"endLine"`
214+
RangeText string `json:"rangeText"`
215+
DefinitionText string `json:"definitionText"`
216+
Scope any `json:"scope"`
217+
FileHash string `json:"fileHash"`
218218
Definition struct {
219219
Name string `json:"name"`
220220
Type string `json:"type"`

backend/internal/codesnippet/handler/http/v1/codesnippet.go

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ type GetContextReq struct {
4848
// 单个查询参数
4949
Query Query `json:"query,omitempty"` // 单个查询条件
5050

51-
Limit int `json:"limit"` // 返回结果数量限制,默认10
52-
WorkspacePath string `json:"workspace_path"` // 工作区路径(必填)
51+
Limit int `json:"limit"` // 返回结果数量限制,默认10
52+
WorkspacePath string `json:"workspacePath"` // 工作区路径(必填)
5353
}
5454

5555
// Query 批量查询条件
5656
type Query struct {
57-
Name string `json:"name,omitempty"` // 代码片段名称(可选)
58-
Type string `json:"type,omitempty"` // 代码片段类型(可选)
59-
Language string `json:"language,omitempty"` // 编程语言(可选)
57+
Name string `json:"name,omitempty"` // 代码片段名称(可选)
58+
SnippetType string `json:"snippetType,omitempty"` // 代码片段类型(可选)
59+
Language string `json:"language,omitempty"` // 编程语言(可选)
6060
}
6161

6262
// GetContext IDE端上下文检索接口
@@ -72,17 +72,23 @@ type Query struct {
7272
// @Router /api/v1/ide/codesnippet/context [post]
7373
// @Security ApiKeyAuth
7474
func (h *CodeSnippetHandler) GetContext(c *web.Context, req GetContextReq) error {
75+
h.logger.Info("GetContext called", "request", req)
76+
7577
// 设置默认限制
7678
if req.Limit <= 0 {
7779
req.Limit = 10
7880
}
7981
if req.Limit > 50 {
80-
req.Limit = 50 // 最大限制50个结果
82+
req.Limit = 50
8183
}
8284

83-
// 如果没有提供workspace_path,则返回空结果
84-
if req.WorkspacePath == "" {
85-
return c.Success([]*domain.CodeSnippet{})
85+
// 提取workspacePath变量
86+
workspacePath := req.WorkspacePath
87+
88+
// 如果没有提供workspacePath,则返回错误
89+
if workspacePath == "" {
90+
h.logger.Warn("Workspace path is required but not provided")
91+
return fmt.Errorf("workspacePath is required")
8692
}
8793

8894
// 获取用户ID,主要使用API Key认证
@@ -100,7 +106,7 @@ func (h *CodeSnippetHandler) GetContext(c *web.Context, req GetContextReq) error
100106
if len(req.Queries) > 0 {
101107
// 执行批量查询
102108
for _, query := range req.Queries {
103-
snippets, err := h.usecase.SearchByWorkspace(c.Request().Context(), userID, req.WorkspacePath, query.Name, query.Type, query.Language)
109+
snippets, err := h.usecase.SearchByWorkspace(c.Request().Context(), userID, workspacePath, query.Name, query.SnippetType, query.Language)
104110
if err != nil {
105111
h.logger.Error("failed to get context for IDE", "error", err)
106112
return err
@@ -109,7 +115,7 @@ func (h *CodeSnippetHandler) GetContext(c *web.Context, req GetContextReq) error
109115
}
110116
} else {
111117
// 执行单个查询
112-
snippets, err := h.usecase.SearchByWorkspace(c.Request().Context(), userID, req.WorkspacePath, req.Query.Name, req.Query.Type, req.Query.Language)
118+
snippets, err := h.usecase.SearchByWorkspace(c.Request().Context(), userID, workspacePath, req.Query.Name, req.Query.SnippetType, req.Query.Language)
113119
if err != nil {
114120
h.logger.Error("failed to get context for IDE", "error", err)
115121
return err
@@ -121,19 +127,6 @@ func (h *CodeSnippetHandler) GetContext(c *web.Context, req GetContextReq) error
121127
if len(allSnippets) > req.Limit {
122128
allSnippets = allSnippets[:req.Limit]
123129
}
124-
125-
h.logger.Info("IDE context retrieval completed",
126-
"userID", c.Request().Context().Value(logger.UserIDKey{}),
127-
"resultCount", len(allSnippets),
128-
"filters", map[string]interface{}{
129-
"singleQuery": map[string]string{
130-
"name": req.Query.Name,
131-
"type": req.Query.Type,
132-
"language": req.Query.Language,
133-
},
134-
"batchQueryCount": len(req.Queries),
135-
"workspace_path": req.WorkspacePath,
136-
})
137-
130+
h.logger.Info("Returning context for IDE", "count", allSnippets)
138131
return c.Success(allSnippets)
139132
}

backend/internal/codesnippet/repo/codesnippet.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ func (r *CodeSnippetRepo) ListByWorkspaceFile(ctx context.Context, workspaceFile
8484
}
8585

8686
func (r *CodeSnippetRepo) GetByID(ctx context.Context, id string) (*db.CodeSnippet, error) {
87+
// 检查ID是否为空
88+
if id == "" {
89+
r.logger.Error("code snippet ID is empty", "id", id)
90+
return nil, fmt.Errorf("invalid code snippet ID: empty ID")
91+
}
92+
8793
// 将 id 字符串转换为 UUID
8894
uuid, err := uuid.Parse(id)
8995
if err != nil {
@@ -102,6 +108,12 @@ func (r *CodeSnippetRepo) GetByID(ctx context.Context, id string) (*db.CodeSnipp
102108
}
103109

104110
func (r *CodeSnippetRepo) Delete(ctx context.Context, id string) error {
111+
// 检查ID是否为空
112+
if id == "" {
113+
r.logger.Error("code snippet ID is empty", "id", id)
114+
return fmt.Errorf("invalid code snippet ID: empty ID")
115+
}
116+
105117
// 将 id 字符串转换为 UUID
106118
uuid, err := uuid.Parse(id)
107119
if err != nil {
@@ -198,9 +210,10 @@ func (r *CodeSnippetRepo) SearchByWorkspace(ctx context.Context, userID, workspa
198210
fileIDs = append(fileIDs, file.ID)
199211
}
200212

201-
// 构建代码片段查询
213+
// 构建代码片段查询,包含WorkspaceFile信息
202214
query := r.client.CodeSnippet.Query().
203-
Where(codesnippet.WorkspaceFileIDIn(fileIDs...))
215+
Where(codesnippet.WorkspaceFileIDIn(fileIDs...)).
216+
WithSourceFile() // 预加载WorkspaceFile信息
204217

205218
// 创建一个切片来存储所有谓词
206219
var predicates []predicate.CodeSnippet

backend/internal/codesnippet/usecase/codesnippet.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func (u *CodeSnippetUsecase) SearchByWorkspace(ctx context.Context, userID, work
135135
// 将数据库模型转换为领域模型
136136
var snippets []*domain.CodeSnippet
137137
for _, dbSnippet := range dbSnippets {
138-
snippet := (&domain.CodeSnippet{}).From(dbSnippet)
138+
snippet := (&domain.CodeSnippet{}).FromWithFile(dbSnippet)
139139
snippets = append(snippets, snippet)
140140
}
141141

0 commit comments

Comments
 (0)