Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

【架构优化】从 workbenchReferences 中拆出引用排序 sortAssistantReferences 与分页 pageAssistantReferences (#550)

fx-kurumi28dcf005c4b7
diff --git a/internal/httpapi/assistant_workbench.go b/internal/httpapi/assistant_workbench.go
index 8f0f7295..dbb3a52b 100644
--- a/internal/httpapi/assistant_workbench.go
+++ b/internal/httpapi/assistant_workbench.go
@@ -468,37 +468,44 @@ func (h *assistantHandlers) workbenchReferences(c *gin.Context) {
for _, v := range h.repo.Repositories() {
add("repository", v.ID, v.Name, v.ProjectID, false)
}
- sort.Slice(out, func(i, j int) bool {
- if out[i].Name != out[j].Name {
- return out[i].Name < out[j].Name
- }
- if out[i].Type != out[j].Type {
- return out[i].Type < out[j].Type
- }
- return out[i].ID < out[j].ID
- })
- offset, _ := strconv.Atoi(c.Query("offset"))
- if offset < 0 {
- offset = 0
- }
- if offset > len(out) {
- offset = len(out)
- }
- end := offset + 30
- if end > len(out) {
- end = len(out)
- }
+ sortAssistantReferences(out)
if global {
- next := 0
- if end < len(out) {
- next = end
- }
- c.JSON(200, gin.H{"items": out[offset:end], "nextOffset": next})
+ offset, _ := strconv.Atoi(c.Query("offset"))
+ items, next := pageAssistantReferences(out, offset, workbenchReferencePageSize)
+ c.JSON(200, gin.H{"items": items, "nextOffset": next})
return
}
c.JSON(200, out)
}
+// workbenchReferencePageSize is the page size of the global reference picker.
+const workbenchReferencePageSize = 30
+
+// sortAssistantReferences orders references by name, then type, then ID.
+func sortAssistantReferences(refs []domain.AssistantReference) {
+ sort.Slice(refs, func(i, j int) bool {
+ if refs[i].Name != refs[j].Name {
+ return refs[i].Name < refs[j].Name
+ }
+ if refs[i].Type != refs[j].Type {
+ return refs[i].Type < refs[j].Type
+ }
+ return refs[i].ID < refs[j].ID
+ })
+}
+
+// pageAssistantReferences returns the page starting at offset (clamped into
+// range) and the offset of the next page, or 0 when this is the last page.
+func pageAssistantReferences(refs []domain.AssistantReference, offset, size int) ([]domain.AssistantReference, int) {
+ offset = max(0, min(offset, len(refs)))
+ end := min(offset+size, len(refs))
+ next := 0
+ if end < len(refs) {
+ next = end
+ }
+ return refs[offset:end], next
+}
+
func workbenchAttachmentDir(userID int64) string {
return filepath.Join(assistantSessionRoot(userID), "attachments")
}
diff --git a/internal/httpapi/assistant_workbench_references_page_test.go b/internal/httpapi/assistant_workbench_references_page_test.go
new file mode 100644
index 00000000..08842281
--- /dev/null
+++ b/internal/httpapi/assistant_workbench_references_page_test.go
@@ -0,0 +1,117 @@
+package httpapi
+
+import (
+ "bytes"
+ "encoding/json"
+ "fmt"
+ "reflect"
+ "testing"
+
+ "agentflow/internal/domain"
+)
+
+func TestSortAssistantReferencesOrdersByNameTypeID(t *testing.T) {
+ refs := []domain.AssistantReference{
+ {Type: "skill", ID: "b", Name: "deploy"},
+ {Type: "agent", ID: "z", Name: "deploy"},
+ {Type: "skill", ID: "a", Name: "deploy"},
+ {Type: "agent", ID: "x", Name: "build"},
+ }
+ sortAssistantReferences(refs)
+ got := []string{}
+ for _, ref := range refs {
+ got = append(got, ref.Name+"/"+ref.Type+"/"+ref.ID)
+ }
+ want := []string{"build/agent/x", "deploy/agent/z", "deploy/skill/a", "deploy/skill/b"}
+ if !reflect.DeepEqual(got, want) {
+ t.Fatalf("order = %v, want %v", got, want)
+ }
+}
+
+func TestPageAssistantReferencesClampsOffsetAndReportsNext(t *testing.T) {
+ refs := make([]domain.AssistantReference, 5)
+ for i := range refs {
+ refs[i].ID = string(rune('a' + i))
+ }
+ cases := []struct {
+ offset, size int
+ ids string
+ next int
+ }{
+ {offset: 0, size: 2, ids: "ab", next: 2},
+ {offset: 2, size: 2, ids: "cd", next: 4},
+ {offset: 4, size: 2, ids: "e", next: 0},
+ {offset: -3, size: 2, ids: "ab", next: 2},
+ {offset: 9, size: 2, ids: "", next: 0},
+ {offset: 0, size: 5, ids: "abcde", next: 0},
+ }
+ for _, tc := range cases {
+ page, next := pageAssistantReferences(refs, tc.offset, tc.size)
+ ids := ""
+ for _, ref := range page {
+ ids += ref.ID
+ }
+ if page == nil || ids != tc.ids || next != tc.next {
+ t.Fatalf("offset=%d size=%d: ids=%q next=%d nil=%v, want ids=%q next=%d", tc.offset, tc.size, ids, next, page == nil, tc.ids, tc.next)
+ }
+ }
+}
+
+// The global picker must return sorted pages of workbenchReferencePageSize
+// items and hand out the next offset until the last page.
+func TestWorkbenchReferencesGlobalScopeReturnsSortedPages(t *testing.T) {
+ h, u := workbenchFixture(t)
+ const total = workbenchReferencePageSize + 5
+ // Agents and skills interleave by name, so the merged list is only in
+ // order if the handler sorts it after collecting every type.
+ for i := total - 1; i >= 0; i-- {
+ name := fmt.Sprintf("pagecase-%02d", i)
+ var err error
+ if i%2 == 0 {
+ err = h.repo.PutAgent(domain.AgentDefinition{ID: name, ProjectID: "workbench", Name: name})
+ } else {
+ err = h.repo.PutSkill(domain.SkillDefinition{ID: name, ProjectID: "workbench", Name: name})
+ }
+ if err != nil {
+ t.Fatal(err)
+ }
+ }
+ page := func(offset int) ([]domain.AssistantReference, int) {
+ t.Helper()
+ c, rec := workbenchContext(u, "GET", fmt.Sprintf("/api/assistant/references?scope=all&q=pagecase&offset=%d", offset), bytes.NewBuffer(nil))
+ h.workbenchReferences(c)
+ var result struct {
+ Items []domain.AssistantReference `json:"items"`
+ NextOffset int `json:"nextOffset"`
+ }
+ if rec.Code != 200 {
+ t.Fatalf("offset=%d status=%d %s", offset, rec.Code, rec.Body.String())
+ }
+ if err := json.Unmarshal(rec.Body.Bytes(), &result); err != nil {
+ t.Fatal(err)
+ }
+ return result.Items, result.NextOffset
+ }
+ ids := func(refs []domain.AssistantReference) []string {
+ out := []string{}
+ for _, ref := range refs {
+ out = append(out, ref.ID)
+ }
+ return out
+ }
+ want := func(from, to int) []string {
+ out := []string{}
+ for i := from; i < to; i++ {
+ out = append(out, fmt.Sprintf("pagecase-%02d", i))
+ }
+ return out
+ }
+ items, next := page(0)
+ if got := ids(items); !reflect.DeepEqual(got, want(0, workbenchReferencePageSize)) || next != workbenchReferencePageSize {
+ t.Fatalf("first page = %v next=%d", got, next)
+ }
+ items, next = page(next)
+ if got := ids(items); !reflect.DeepEqual(got, want(workbenchReferencePageSize, total)) || next != 0 {
+ t.Fatalf("last page = %v next=%d", got, next)
+ }
+}