An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:sparkles: Support Model Context Protocol (MCP) https://github.com/siyuan-note/siyuan/issues/13795
Daniel253bc32e07ca
diff --git a/kernel/agent/tools.go b/kernel/agent/tools.goindex f4741f893d..c2bc812ca5 100644--- a/kernel/agent/tools.go+++ b/kernel/agent/tools.go@@ -65,15 +65,31 @@ func executeTool(tc openai.ToolCall, sessionID string) (string, bool) {}func convertSchema(schema tools.ToolSchema) any {+ // 根级 anyOf 常见于 Zod 生成的 schema,取第一个 object 变体展开。+ if schema.Type == "" && len(schema.AnyOf) > 0 {+ for _, variant := range schema.AnyOf {+ if variant.Type == "object" || len(variant.Properties) > 0 {+ return convertSchema(variant)+ }+ }+ }+props := make(map[string]any)for name, prop := range schema.Properties {props[name] = convertProperty(prop)}+ schemaType := schema.Type+ if schemaType == "" && len(props) > 0 {+ schemaType = "object"+ }+schemaMap := map[string]any{- "type": schema.Type,"properties": props,}+ if schemaType != "" {+ schemaMap["type"] = schemaType+ }if len(schema.Required) > 0 {reqVals := make([]any, len(schema.Required))for i, v := range schema.Required {@@ -85,8 +101,16 @@ func convertSchema(schema tools.ToolSchema) any {}func convertProperty(prop tools.Property) map[string]any {- p := map[string]any{- "type": prop.Type,+ // Zod 可选字段常生成 anyOf: [{type: T}, {type: null}],简化为单一类型即可。+ if prop.Type == "" && len(prop.AnyOf) > 0 {+ if simplified := simplifyNullUnionProp(prop); simplified != nil {+ return simplified+ }+ }++ p := map[string]any{}+ if prop.Type != "" {+ p["type"] = prop.Type}if prop.Description != "" {p["description"] = prop.Description@@ -115,9 +139,54 @@ func convertProperty(prop tools.Property) map[string]any {}p["required"] = reqVals}+ if len(prop.AnyOf) > 0 {+ p["anyOf"] = convertPropArray(prop.AnyOf)+ }+ if len(prop.OneOf) > 0 {+ p["oneOf"] = convertPropArray(prop.OneOf)+ }+ if len(prop.AllOf) > 0 {+ p["allOf"] = convertPropArray(prop.AllOf)+ }return p}+// simplifyNullUnionProp 将 anyOf: [T, null] 形式的 Zod 可选字段简化为 T。+func simplifyNullUnionProp(prop tools.Property) map[string]any {+ var candidate *tools.Property+ for i := range prop.AnyOf {+ p := &prop.AnyOf[i]+ if p.Type == "null" {+ continue+ }+ if len(p.OneOf) > 0 || len(p.AnyOf) > 0 || len(p.AllOf) > 0 {+ return nil+ }+ if candidate != nil {+ return nil+ }+ candidate = p+ }+ if candidate == nil {+ return nil+ }+ result := convertProperty(*candidate)+ if prop.Description != "" {+ if _, ok := result["description"]; !ok {+ result["description"] = prop.Description+ }+ }+ return result+}++func convertPropArray(props []tools.Property) []any {+ result := make([]any, len(props))+ for i, prop := range props {+ result[i] = convertProperty(prop)+ }+ return result+}+func resultToString(result tools.CallToolResult) string {var parts []stringfor _, item := range result.Content {diff --git a/kernel/agent/tools_test.go b/kernel/agent/tools_test.gonew file mode 100644index 0000000000..1b77898c48--- /dev/null+++ b/kernel/agent/tools_test.go@@ -0,0 +1,91 @@+// SiYuan - Refactor your thinking+// Copyright (c) 2020-present, b3log.org+//+// This program is free software: you can redistribute it and/or modify+// it under the terms of the GNU Affero General Public License as published by+// the Free Software Foundation, either version 3 of the License, or+// (at your option) any later version.+//+// This program is distributed in the hope that it will be useful,+// but WITHOUT ANY WARRANTY; without even the implied warranty of+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+// GNU Affero General Public License for more details.+//+// You should have received a copy of the GNU Affero General Public License+// along with this program. If not, see <https://www.gnu.org/licenses/>.++package agent++import (+ "encoding/json"+ "testing"++ "github.com/siyuan-note/siyuan/kernel/mcp/tools"+)++func TestConvertSchemaZodOptionalFields(t *testing.T) {+ schema := tools.ToolSchema{+ Type: "object",+ Properties: map[string]tools.Property{+ "title": {Type: "string", Description: "task title"},+ "content": {+ AnyOf: []tools.Property{+ {Type: "string"},+ {Type: "null"},+ },+ Description: "optional content",+ },+ },+ Required: []string{"title"},+ }++ out := convertSchema(schema).(map[string]any)+ if out["type"] != "object" {+ t.Fatalf("expected root type object, got %#v", out["type"])+ }++ props := out["properties"].(map[string]any)+ content := props["content"].(map[string]any)+ if content["type"] != "string" {+ t.Fatalf("expected simplified content type string, got %#v", content)+ }+ if _, ok := content["type"]; ok {+ if content["type"] == "" {+ t.Fatal("content type must not be empty string")+ }+ }+ if _, ok := content["anyOf"]; ok {+ t.Fatalf("expected anyOf to be simplified away, got %#v", content)+ }++ raw, err := json.Marshal(out)+ if err != nil {+ t.Fatal(err)+ }+ if string(raw) == "" {+ t.Fatal("expected non-empty json")+ }+}++func TestConvertSchemaRootAnyOf(t *testing.T) {+ schema := tools.ToolSchema{+ AnyOf: []tools.ToolSchema{+ {+ Type: "object",+ Properties: map[string]tools.Property{+ "title": {Type: "string"},+ },+ Required: []string{"title"},+ },+ },+ }++ out := convertSchema(schema).(map[string]any)+ if out["type"] != "object" {+ t.Fatalf("expected root type object, got %#v", out["type"])+ }+ props := out["properties"].(map[string]any)+ if len(props) != 1 {+ t.Fatalf("expected 1 property, got %d", len(props))+ }+}