58 lines
1.9 KiB
Go
58 lines
1.9 KiB
Go
package judgegate
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
|
|
"github.com/samcharles93/archipelago/sdk/guard"
|
|
)
|
|
|
|
// maxInputChars bounds how much of an action's input a judge model is shown.
|
|
const maxInputChars = 4000
|
|
|
|
// Action is an action as a judge model is shown it. Eval cases write the same
|
|
// shape, so what is measured is what runs.
|
|
type Action struct {
|
|
Connector string `json:"connector"`
|
|
Name string `json:"name"`
|
|
Input json.RawMessage `json:"input,omitempty"`
|
|
Target Target `json:"target"`
|
|
Effect Effect `json:"effect"`
|
|
}
|
|
|
|
// Target is what the action acts on.
|
|
type Target struct {
|
|
Kind string `json:"kind"`
|
|
ID string `json:"id,omitempty"`
|
|
Environment string `json:"environment,omitempty"`
|
|
Disposable bool `json:"disposable,omitempty"`
|
|
}
|
|
|
|
// Effect is what the connector says the action does.
|
|
type Effect struct {
|
|
ReadOnly bool `json:"read_only"`
|
|
Reversibility string `json:"reversibility,omitempty"`
|
|
Summary string `json:"summary,omitempty"`
|
|
}
|
|
|
|
var reversibility = map[int]string{1: "undone automatically", 2: "recoverable", 3: "irreversible"}
|
|
|
|
// ActionOf presents a guard action to a judge model.
|
|
func ActionOf(a guard.Action) Action {
|
|
return Action{
|
|
Connector: a.Connector, Name: a.Name, Input: clipJSON(a.Input),
|
|
Target: Target{Kind: a.Target.Kind, ID: a.Target.ID, Environment: a.Target.Environment, Disposable: a.Target.Disposable},
|
|
Effect: Effect{ReadOnly: a.Effect.ReadOnly, Reversibility: reversibility[a.Effect.Reversibility], Summary: a.Effect.Summary},
|
|
}
|
|
}
|
|
|
|
// clipJSON keeps input whole when it fits, and otherwise shows its start as a
|
|
// JSON string so the state stays valid JSON.
|
|
func clipJSON(in []byte) json.RawMessage {
|
|
if len(in) <= maxInputChars {
|
|
return in
|
|
}
|
|
cut, _ := json.Marshal(strings.ToValidUTF8(string(in[:maxInputChars]), "") + "... (cut)")
|
|
return cut
|
|
}
|