The organisation's standing instructions are stored per organisation and join the system prompt of chat turns and workflow agent steps. The Org configuration group is served: schedules, alert webhook, playbooks, models, connections and credential bindings open the page that already owns the concept.
60 lines
2.3 KiB
Go
60 lines
2.3 KiB
Go
package governance
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/danielgtaylor/huma/v2"
|
|
|
|
"github.com/samcharles93/archie-core/internal/platform/httpapi"
|
|
)
|
|
|
|
// MaxInstructionsBytes bounds the organisation's instructions: they join every
|
|
// agent's system prompt, so an unbounded text would crowd out the task.
|
|
const MaxInstructionsBytes = 8 << 10
|
|
|
|
type instructionsBody struct {
|
|
Text string `json:"text" maxLength:"8192" doc:"Standing instructions given to every agent in the organisation"`
|
|
}
|
|
|
|
// Instructions returns the organisation's standing instructions, trimmed;
|
|
// empty when none are set.
|
|
func (m *Module) Instructions(ctx context.Context) (string, error) {
|
|
body, err := m.store.Instructions(ctx, m.orgID)
|
|
return strings.TrimSpace(body), err
|
|
}
|
|
|
|
func (m *Module) registerInstructions(api huma.API) {
|
|
tags := []string{"Governance"}
|
|
httpapi.Register(api, httpapi.AdminAuthorized(ActionRead, KindSettings), huma.Operation{
|
|
OperationID: "get-instructions", Method: http.MethodGet, Path: "/api/settings/instructions",
|
|
Summary: "Get the organisation's standing instructions", Tags: tags,
|
|
}, m.getInstructions)
|
|
httpapi.Register(api, httpapi.AdminAuthorized(ActionUpdate, KindSettings), huma.Operation{
|
|
OperationID: "put-instructions", Method: http.MethodPut, Path: "/api/settings/instructions",
|
|
Summary: "Change the organisation's standing instructions", Tags: tags,
|
|
}, m.putInstructions)
|
|
}
|
|
|
|
func (m *Module) getInstructions(ctx context.Context, _ *struct{}) (*httpapi.Reply[instructionsBody], error) {
|
|
text, err := m.Instructions(ctx)
|
|
if err != nil {
|
|
m.log.ErrorContext(ctx, "read instructions", "error", err)
|
|
return nil, huma.NewError(http.StatusInternalServerError, "couldn't read the instructions")
|
|
}
|
|
return httpapi.OK(instructionsBody{Text: text}), nil
|
|
}
|
|
|
|
func (m *Module) putInstructions(ctx context.Context, in *struct{ Body instructionsBody }) (*httpapi.Reply[instructionsBody], error) {
|
|
text := strings.TrimSpace(in.Body.Text)
|
|
if len(text) > MaxInstructionsBytes {
|
|
return nil, huma.NewError(http.StatusBadRequest, "instructions are limited to 8 KiB")
|
|
}
|
|
if err := m.store.PutInstructions(ctx, m.orgID, text); err != nil {
|
|
m.log.ErrorContext(ctx, "put instructions", "error", err)
|
|
return nil, huma.NewError(http.StatusInternalServerError, "couldn't save the instructions")
|
|
}
|
|
return httpapi.OK(instructionsBody{Text: text}), nil
|
|
}
|