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.

refactor(knowledge): use slices.Contains for slice membership checks (#271) (#272)

fx-kurumi0b9a3e6fcc31
diff --git a/internal/knowledge/client.go b/internal/knowledge/client.go
index 4958d3a5..6d31613d 100644
--- a/internal/knowledge/client.go
+++ b/internal/knowledge/client.go
@@ -8,6 +8,7 @@ import (
"encoding/json"
"errors"
"fmt"
+ "slices"
"strings"
"sync"
"time"
@@ -28,12 +29,7 @@ const (
var ReadOnlyTools = []string{ToolSearch, ToolFetch, ToolListSources, ToolGetSource}
func IsReadOnlyTool(name string) bool {
- for _, tool := range ReadOnlyTools {
- if tool == name {
- return true
- }
- }
- return false
+ return slices.Contains(ReadOnlyTools, name)
}
var ErrNotConfigured = errors.New("知识库未接入:请在「知识库 → 接入配置」填写地址和 Token 并启用")
@@ -213,13 +209,7 @@ func SourceAllowed(allowed []string, source string) bool {
if len(allowed) == 0 {
return true
}
- source = strings.TrimSpace(source)
- for _, s := range allowed {
- if s == source {
- return true
- }
- }
- return false
+ return slices.Contains(allowed, strings.TrimSpace(source))
}
func cleanList(in []string) []string {
diff --git a/internal/knowledge/contains_test.go b/internal/knowledge/contains_test.go
new file mode 100644
index 00000000..240e0fad
--- /dev/null
+++ b/internal/knowledge/contains_test.go
@@ -0,0 +1,52 @@
+package knowledge
+
+import (
+ "slices"
+ "testing"
+)
+
+func TestIsReadOnlyToolTable(t *testing.T) {
+ cases := map[string]bool{
+ ToolSearch: true, ToolFetch: true, ToolListSources: true, ToolGetSource: true,
+ ToolWrite: false, "": false, " kb_search": false, "KB_SEARCH": false,
+ }
+ for name, want := range cases {
+ if got := IsReadOnlyTool(name); got != want {
+ t.Errorf("IsReadOnlyTool(%q) = %v, want %v", name, got, want)
+ }
+ }
+}
+
+func TestSourceAllowedTable(t *testing.T) {
+ cases := []struct {
+ allowed []string
+ source string
+ want bool
+ }{
+ {nil, "x", true},
+ {[]string{}, "x", true},
+ {[]string{" ", ""}, "x", true},
+ {[]string{"a"}, "a", true},
+ {[]string{" a "}, "a", true},
+ {[]string{"a"}, " a ", true},
+ {[]string{"a", "b"}, "b", true},
+ {[]string{"a"}, "x", false},
+ {[]string{"a"}, "", false},
+ {[]string{"a"}, "A", false},
+ }
+ for _, c := range cases {
+ if got := SourceAllowed(c.allowed, c.source); got != c.want {
+ t.Errorf("SourceAllowed(%q, %q) = %v, want %v", c.allowed, c.source, got, c.want)
+ }
+ }
+}
+
+func TestAppendUniqueKeepsOrder(t *testing.T) {
+ var list []string
+ for _, v := range []string{"b", "a", "b", "c", "a"} {
+ list = appendUnique(list, v)
+ }
+ if want := []string{"b", "a", "c"}; !slices.Equal(list, want) {
+ t.Fatalf("appendUnique = %#v, want %#v", list, want)
+ }
+}
diff --git a/internal/knowledge/service.go b/internal/knowledge/service.go
index 7b343d3a..f3a00f37 100644
--- a/internal/knowledge/service.go
+++ b/internal/knowledge/service.go
@@ -3,6 +3,7 @@ package knowledge
import (
"context"
"net/url"
+ "slices"
"sort"
"strings"
"time"
@@ -476,10 +477,8 @@ func cleanSources(in []string) []string {
}
func appendUnique(list []string, v string) []string {
- for _, existing := range list {
- if existing == v {
- return list
- }
+ if slices.Contains(list, v) {
+ return list
}
return append(list, v)
}