Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

1.6 KB
Raw Blame
// SiYuan - https://github.com/siyuan-note/siyuan
// Copyright (c) 2020-present, b3log.org
// SPDX-License-Identifier: AGPL-3.0-or-later

package api

import (
	"github.com/gin-gonic/gin"
	"github.com/siyuan-note/siyuan/kernel/apicontract"
	"github.com/siyuan-note/siyuan/kernel/model"
	"github.com/siyuan-note/siyuan/kernel/util"
)

var getAgentInstructions = contractHandler(apicontract.AIGetAgentInstructions, getAgentInstructionsContract)
var setAgentInstructions = contractHandler(apicontract.AISetAgentInstructions, setAgentInstructionsContract)

func agentInstructionsResponse(data util.AgentInstructions, err error) apicontract.Response[apicontract.AIAgentInstructionsData] {
	if err != nil {
		return apicontract.Failure[apicontract.AIAgentInstructionsData](-1, util.EscapeHTML(util.AgentInstructionsError(err, model.Conf.Lang)))
	}
	return apicontract.Success(apicontract.AIAgentInstructionsData{Content: data.Content, Revision: data.Revision})
}

func getAgentInstructionsContract(c *gin.Context, request apicontract.EmptyRequest) apicontract.Response[apicontract.AIAgentInstructionsData] {
	return agentInstructionsResponse(util.ReadAgentInstructions())
}

func setAgentInstructionsContract(c *gin.Context, request apicontract.AIAgentInstructionsSaveRequest) apicontract.Response[apicontract.AIAgentInstructionsData] {
	data, migrated, err := util.SaveAgentInstructions(request.Content, request.Revision)
	if migrated {
		model.IncSyncIfNeeded(util.AgentInstructionsPath(), util.LegacyAgentInstructionsPath())
	} else if err == nil {
		model.IncSyncIfNeeded(util.AgentInstructionsPath())
	}
	return agentInstructionsResponse(data, err)
}