An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
♻️ Migrate cloud API contracts https://github.com/siyuan-note/siyuan/issues/19378
Daniel70734191a8ea
diff --git a/app/src/types/api/index.d.ts b/app/src/types/api/index.d.tsindex de9a806489..1dc0155309 100644--- a/app/src/types/api/index.d.ts+++ b/app/src/types/api/index.d.ts@@ -108,6 +108,14 @@ export type ChildBlock = { "content"?: string; "id": string; "markdown"?: stringexport type CloseNotebookRequestInput = { "notebook": string; };+export type CloudBackup = { "hSize": string; "saveDir": string; "size": number; "updated": string; };++export type CloudReminderRequestInput = { "content": string; "id": string; "timed": string; };++export type CloudSpaceData = { "backup": CloudBackup | null; "hAssetSize": string; "hExchangeSize": string; "hSize": string; "hTotalSize": string; "hTrafficAPIGet": string; "hTrafficAPIPut": string; "hTrafficDownloadSize": string; "hTrafficUploadSize": string; "sync": CloudSync | null; };++export type CloudSync = { "cloudName": string; "hSize": string; "saveDir": string; "size": number; "updated": string; };+export type ContentWordCountRequestInput = { "content": string; "reqId"?: JSONValue | null; };export type CopyStdMarkdownRequestInput = { "adjustHeadingLevel"?: boolean | null; "assetsDestSpace2Underscore"?: boolean | null; "fillCSSVar"?: boolean | null; "id": string; "imgTag"?: boolean | null; };@@ -614,8 +622,6 @@ export type APILegacyPOSTPath ="/api/clipboard/prepareRichText" |"/api/clipboard/readFilePaths" |"/api/clipboard/writeFilePath" |- "/api/cloud/getCloudSpace" |- "/api/cloud/setCloudReminder" |"/api/export/copyExportFile" |"/api/export/export2Liandi" |"/api/export/exportAsFile" |@@ -1288,6 +1294,16 @@ export interface APIPOSTRoutes {response: { "code": 0; "data": BroadcastPublishData; "msg": string; } | { "code": -1 | 1; "data": { "closeTimeout": number; } | null; "msg": string; };body: "multipart";};+ "/api/cloud/getCloudSpace": {+ request: EmptyRequestInput;+ response: { "code": 0; "data": CloudSpaceData; "msg": string; } | { "code": -1 | 1; "data": { "closeTimeout": number; } | null; "msg": string; };+ body: "none";+ };+ "/api/cloud/setCloudReminder": {+ request: CloudReminderRequestInput;+ response: { "code": 0; "data": null; "msg": string; } | { "code": -1; "data": { "closeTimeout": number; } | null; "msg": string; };+ body: "json";+ };"/api/convert/pandoc": {request: PandocRequestInput;response: { "code": 0; "data": PandocData; "msg": string; } | { "code": -1; "data": { "closeTimeout": number; } | null; "msg": string; };diff --git a/kernel/api/block.go b/kernel/api/block.goindex 8a3163519b..9d7f00abee 100644--- a/kernel/api/block.go+++ b/kernel/api/block.go@@ -19,7 +19,6 @@ package apiimport ("errors""fmt"- "net/http""strings""github.com/88250/gulu"@@ -289,26 +288,12 @@ var setBlockReminder = contractHandler(apicontract.SetBlockReminder, func(c *ginreturn apicontract.Success(apicontract.Null{})})-func setCloudReminder(c *gin.Context) {- ret := gulu.Ret.NewResult()- defer c.JSON(http.StatusOK, ret)-- arg, ok := util.JsonArg(c, ret)- if !ok {- return- }-- id := arg["id"].(string)- timed := arg["timed"].(string) // yyyyMMddHHmmss- content := arg["content"].(string)- err := model.SetCloudReminder(id, content, timed)- if err != nil {- ret.Code = -1- ret.Msg = err.Error()- ret.Data = map[string]any{"closeTimeout": 7000}- return+var setCloudReminder = contractHandler(apicontract.SetCloudReminder, func(c *gin.Context, request apicontract.CloudReminderRequest) apicontract.Response[apicontract.Null] {+ if err := model.SetCloudReminder(request.ID, request.Content, request.Timed); err != nil {+ return apicontract.FailureWithTimeout[apicontract.Null](-1, err.Error(), 7000)}-}+ return apicontract.Success(apicontract.Null{})+})var getUnfoldedParentID = contractHandler(apicontract.GetUnfoldedParentID, func(c *gin.Context, request apicontract.BlockQueryRequest) apicontract.Response[apicontract.UnfoldedParentData] {boxID, err := holdContractBlockRequest(c, request.Notebook, request.ID, request.IDs, false)diff --git a/kernel/api/contract_cloud.go b/kernel/api/contract_cloud.gonew file mode 100644index 0000000000..66aea6d9d3--- /dev/null+++ b/kernel/api/contract_cloud.go@@ -0,0 +1,20 @@+package api++import (+ "github.com/siyuan-note/siyuan/kernel/apicontract"+ "github.com/siyuan-note/siyuan/kernel/model"+)++func cloudSyncContract(value *model.Sync) *apicontract.CloudSync {+ if value == nil {+ return nil+ }+ return &apicontract.CloudSync{Size: value.Size, HSize: value.HSize, Updated: value.Updated, CloudName: value.CloudName, SaveDir: value.SaveDir}+}++func cloudBackupContract(value *model.Backup) *apicontract.CloudBackup {+ if value == nil {+ return nil+ }+ return &apicontract.CloudBackup{Size: value.Size, HSize: value.HSize, Updated: value.Updated, SaveDir: value.SaveDir}+}diff --git a/kernel/api/contract_cloud_test.go b/kernel/api/contract_cloud_test.gonew file mode 100644index 0000000000..5ab3562bf6--- /dev/null+++ b/kernel/api/contract_cloud_test.go@@ -0,0 +1,45 @@+package api++import (+ "encoding/json"+ "github.com/gin-gonic/gin"+ "github.com/siyuan-note/siyuan/kernel/apicontract"+ "github.com/siyuan-note/siyuan/kernel/model"+ "net/http/httptest"+ "strings"+ "testing"+)++func TestAPIContractCloudPayloads(t *testing.T) {+ for _, value := range []*model.Sync{nil, {}, {Size: 12345, HSize: "12 KB", Updated: "date", CloudName: "cloud", SaveDir: "local"}} {+ before, _ := json.Marshal(value)+ after, err := json.Marshal(cloudSyncContract(value))+ if err != nil || string(before) != string(after) {+ t.Fatalf("sync payload changed: %s != %s, %v", before, after, err)+ }+ }+ for _, value := range []*model.Backup{nil, {}, {Size: 12345, HSize: "12 KB", Updated: "date", SaveDir: "local"}} {+ before, _ := json.Marshal(value)+ after, err := json.Marshal(cloudBackupContract(value))+ if err != nil || string(before) != string(after) {+ t.Fatalf("backup payload changed: %s != %s, %v", before, after, err)+ }+ }+}++func TestAPIContractCloudReminderArguments(t *testing.T) {+ request, err := apicontract.SetCloudReminder.Decode(strings.NewReader(`{"id":" id ","timed":" time ","content":" content "}`))+ if err != nil || request.ID != " id " || request.Timed != " time " || request.Content != " content " {+ t.Fatalf("reminder arguments changed: %#v, %v", request, err)+ }+ engine := gin.New()+ engine.POST("/api/cloud/setCloudReminder", setCloudReminder)+ for _, body := range []string{`{}`, `{"id":"id","timed":"0","content":null}`} {+ recorder := httptest.NewRecorder()+ engine.ServeHTTP(recorder, httptest.NewRequest("POST", "/api/cloud/setCloudReminder", strings.NewReader(body)))+ requireAPIContract(t, "POST", "/api/cloud/setCloudReminder", recorder)+ if !strings.Contains(recorder.Body.String(), `"code":-1`) {+ t.Fatalf("invalid reminder accepted: %s", recorder.Body.String())+ }+ }+}diff --git a/kernel/api/repo.go b/kernel/api/repo.goindex 2f9c7762af..9925685462 100644--- a/kernel/api/repo.go+++ b/kernel/api/repo.go@@ -204,31 +204,19 @@ func diffRepoSnapshots(c *gin.Context) {}}-func getCloudSpace(c *gin.Context) {- ret := gulu.Ret.NewResult()- defer c.JSON(http.StatusOK, ret)-- sync, backup, hSize, hAssetSize, hTotalSize, exchangeSize, hTrafficUploadSize, hTrafficDownloadSize, htrafficAPIGet, hTrafficAPIPut, err := model.GetCloudSpace()+var getCloudSpace = contractHandler(apicontract.GetCloudSpace, func(c *gin.Context, request apicontract.EmptyRequest) apicontract.Response[apicontract.CloudSpaceData] {+ sync, backup, hSize, hAssetSize, hTotalSize, exchangeSize, hTrafficUploadSize, hTrafficDownloadSize, hTrafficAPIGet, hTrafficAPIPut, err := model.GetCloudSpace()if err != nil {- ret.Code = 1- ret.Msg = err.Error()util.PushErrMsg(err.Error(), 3000)- return- }-- ret.Data = map[string]any{- "sync": sync,- "backup": backup,- "hAssetSize": hAssetSize,- "hSize": hSize,- "hTotalSize": hTotalSize,- "hExchangeSize": exchangeSize,- "hTrafficUploadSize": hTrafficUploadSize,- "hTrafficDownloadSize": hTrafficDownloadSize,- "hTrafficAPIGet": htrafficAPIGet,- "hTrafficAPIPut": hTrafficAPIPut,- }-}+ return apicontract.Failure[apicontract.CloudSpaceData](1, err.Error())+ }+ return apicontract.Success(apicontract.CloudSpaceData{+ Sync: cloudSyncContract(sync), Backup: cloudBackupContract(backup),+ HAssetSize: hAssetSize, HSize: hSize, HTotalSize: hTotalSize, HExchangeSize: exchangeSize,+ HTrafficUploadSize: hTrafficUploadSize, HTrafficDownloadSize: hTrafficDownloadSize,+ HTrafficAPIGet: hTrafficAPIGet, HTrafficAPIPut: hTrafficAPIPut,+ })+})func checkoutRepo(c *gin.Context) {ret := gulu.Ret.NewResult()diff --git a/kernel/apicontract/cloud.go b/kernel/apicontract/cloud.gonew file mode 100644index 0000000000..471e9f7004--- /dev/null+++ b/kernel/apicontract/cloud.go@@ -0,0 +1,35 @@+package apicontract++type CloudReminderRequest struct {+ ID string `json:"id"`+ Timed string `json:"timed"`+ Content string `json:"content"`+}++type CloudBackup struct {+ Size int64 `json:"size"`+ HSize string `json:"hSize"`+ Updated string `json:"updated"`+ SaveDir string `json:"saveDir"`+}++type CloudSync struct {+ Size int64 `json:"size"`+ HSize string `json:"hSize"`+ Updated string `json:"updated"`+ CloudName string `json:"cloudName"`+ SaveDir string `json:"saveDir"`+}++type CloudSpaceData struct {+ Sync *CloudSync `json:"sync"`+ Backup *CloudBackup `json:"backup"`+ HAssetSize string `json:"hAssetSize"`+ HSize string `json:"hSize"`+ HTotalSize string `json:"hTotalSize"`+ HExchangeSize string `json:"hExchangeSize"`+ HTrafficUploadSize string `json:"hTrafficUploadSize"`+ HTrafficDownloadSize string `json:"hTrafficDownloadSize"`+ HTrafficAPIGet string `json:"hTrafficAPIGet"`+ HTrafficAPIPut string `json:"hTrafficAPIPut"`+}diff --git a/kernel/apicontract/contracts.go b/kernel/apicontract/contracts.goindex f3d432677b..759616d81e 100644--- a/kernel/apicontract/contracts.go+++ b/kernel/apicontract/contracts.go@@ -435,3 +435,6 @@ var GetBroadcastChannelInfo = define[BroadcastChannelRequest, BroadcastChannelDavar GetBroadcastChannels = define[EmptyRequest, BroadcastChannelsData]("getChannels", "/api/broadcast/getChannels", NoBody, ResponseOptions{}, "POST")var BroadcastPublish = define[MultipartFields, BroadcastPublishData]("broadcastPublish", "/api/broadcast/publish", MultipartBody, ResponseOptions{AdditionalCodes: []int{1}}, "POST")++var SetCloudReminder = define[CloudReminderRequest, Null]("setCloudReminder", "/api/cloud/setCloudReminder", JSONBody, ResponseOptions{}, "POST")+var GetCloudSpace = define[EmptyRequest, CloudSpaceData]("getCloudSpace", "/api/cloud/getCloudSpace", NoBody, ResponseOptions{AdditionalCodes: []int{1}}, "POST")diff --git a/kernel/apicontract/legacy_routes.json b/kernel/apicontract/legacy_routes.jsonindex 0cf8b3e4f6..404649a853 100644--- a/kernel/apicontract/legacy_routes.json+++ b/kernel/apicontract/legacy_routes.json@@ -159,8 +159,6 @@{"method":"POST","path":"/api/clipboard/prepareRichText","handler":"prepareRichText"},{"method":"POST","path":"/api/clipboard/readFilePaths","handler":"readFilePaths"},{"method":"POST","path":"/api/clipboard/writeFilePath","handler":"writeFilePath"},- {"method":"POST","path":"/api/cloud/getCloudSpace","handler":"getCloudSpace"},- {"method":"POST","path":"/api/cloud/setCloudReminder","handler":"setCloudReminder"},{"method":"POST","path":"/api/export/copyExportFile","handler":"copyExportFile"},{"method":"POST","path":"/api/export/export2Liandi","handler":"export2Liandi"},{"method":"POST","path":"/api/export/exportAsFile","handler":"exportAsFile"},diff --git a/kernel/apicontract/schema.json b/kernel/apicontract/schema.jsonindex cd5840322b..fc37cc5b57 100644--- a/kernel/apicontract/schema.json+++ b/kernel/apicontract/schema.json@@ -1581,6 +1581,140 @@],"additionalProperties": false},+ "CloudBackup": {+ "type": "object",+ "properties": {+ "hSize": {+ "type": "string"+ },+ "saveDir": {+ "type": "string"+ },+ "size": {+ "type": "integer"+ },+ "updated": {+ "type": "string"+ }+ },+ "required": [+ "hSize",+ "saveDir",+ "size",+ "updated"+ ],+ "additionalProperties": false+ },+ "CloudReminderRequestInput": {+ "type": "object",+ "properties": {+ "content": {+ "type": "string"+ },+ "id": {+ "type": "string"+ },+ "timed": {+ "type": "string"+ }+ },+ "required": [+ "content",+ "id",+ "timed"+ ],+ "additionalProperties": false+ },+ "CloudSpaceData": {+ "type": "object",+ "properties": {+ "backup": {+ "anyOf": [+ {+ "$ref": "#/$defs/CloudBackup"+ },+ {+ "type": "null"+ }+ ]+ },+ "hAssetSize": {+ "type": "string"+ },+ "hExchangeSize": {+ "type": "string"+ },+ "hSize": {+ "type": "string"+ },+ "hTotalSize": {+ "type": "string"+ },+ "hTrafficAPIGet": {+ "type": "string"+ },+ "hTrafficAPIPut": {+ "type": "string"+ },+ "hTrafficDownloadSize": {+ "type": "string"+ },+ "hTrafficUploadSize": {+ "type": "string"+ },+ "sync": {+ "anyOf": [+ {+ "$ref": "#/$defs/CloudSync"+ },+ {+ "type": "null"+ }+ ]+ }+ },+ "required": [+ "backup",+ "hAssetSize",+ "hExchangeSize",+ "hSize",+ "hTotalSize",+ "hTrafficAPIGet",+ "hTrafficAPIPut",+ "hTrafficDownloadSize",+ "hTrafficUploadSize",+ "sync"+ ],+ "additionalProperties": false+ },+ "CloudSync": {+ "type": "object",+ "properties": {+ "cloudName": {+ "type": "string"+ },+ "hSize": {+ "type": "string"+ },+ "saveDir": {+ "type": "string"+ },+ "size": {+ "type": "integer"+ },+ "updated": {+ "type": "string"+ }+ },+ "required": [+ "cloudName",+ "hSize",+ "saveDir",+ "size",+ "updated"+ ],+ "additionalProperties": false+ },"ContentWordCountRequestInput": {"type": "object","properties": {@@ -13842,6 +13976,157 @@]}},+ {+ "method": "POST",+ "path": "/api/cloud/getCloudSpace",+ "handler": "getCloudSpace",+ "body": "none",+ "request": {+ "$ref": "#/$defs/EmptyRequestInput"+ },+ "response": {+ "anyOf": [+ {+ "type": "object",+ "properties": {+ "code": {+ "type": "integer",+ "enum": [+ 0+ ]+ },+ "data": {+ "$ref": "#/$defs/CloudSpaceData"+ },+ "msg": {+ "type": "string"+ }+ },+ "required": [+ "code",+ "msg",+ "data"+ ],+ "additionalProperties": false+ },+ {+ "type": "object",+ "properties": {+ "code": {+ "type": "integer",+ "enum": [+ -1,+ 1+ ]+ },+ "data": {+ "anyOf": [+ {+ "type": "object",+ "properties": {+ "closeTimeout": {+ "type": "number"+ }+ },+ "required": [+ "closeTimeout"+ ],+ "additionalProperties": false+ },+ {+ "type": "null"+ }+ ]+ },+ "msg": {+ "type": "string"+ }+ },+ "required": [+ "code",+ "msg",+ "data"+ ],+ "additionalProperties": true+ }+ ]+ }+ },+ {+ "method": "POST",+ "path": "/api/cloud/setCloudReminder",+ "handler": "setCloudReminder",+ "body": "json",+ "request": {+ "$ref": "#/$defs/CloudReminderRequestInput"+ },+ "response": {+ "anyOf": [+ {+ "type": "object",+ "properties": {+ "code": {+ "type": "integer",+ "enum": [+ 0+ ]+ },+ "data": {+ "type": "null"+ },+ "msg": {+ "type": "string"+ }+ },+ "required": [+ "code",+ "msg",+ "data"+ ],+ "additionalProperties": false+ },+ {+ "type": "object",+ "properties": {+ "code": {+ "type": "integer",+ "enum": [+ -1+ ]+ },+ "data": {+ "anyOf": [+ {+ "type": "object",+ "properties": {+ "closeTimeout": {+ "type": "number"+ }+ },+ "required": [+ "closeTimeout"+ ],+ "additionalProperties": false+ },+ {+ "type": "null"+ }+ ]+ },+ "msg": {+ "type": "string"+ }+ },+ "required": [+ "code",+ "msg",+ "data"+ ],+ "additionalProperties": true+ }+ ]+ }+ },{"method": "POST","path": "/api/convert/pandoc",