Files
magpie/internal/library/instr_field.go
T
yetone cc659c7559 library: Instructions reach MiniMax Code and WorkBuddy (lysyscy8 on X)
MiniMax Code reads AGENTS.md in its data folder ($MINIMAX_DATA_DIR, else
~/.minimax) before every conversation (readGlobalInstructions in
@minimax-ai/code 0.7.6's chunks), so its target now names that file.

WorkBuddy and WorkBuddy AI keep their instructions as the string
personalization.customPrompt in app/app-config.json of their folder
(PersonalizationCollector and AppConfigService in 5.5.6's app.asar, both
builds). Target.Field says the instructions are a JSON string rather than a
file; instrRead/instrSave read and write it through edit.SetJSON/DelJSON, so
every other key, its order and indentation stay, the user's own prompt stays
in the field around magpie's marker part, and taking the part back leaves
the file byte for byte as it was (an empty personalization object magpie
made goes too). A file that is not JSON, or a field that is not text, is
unknown: reported, never written over. Import takes the user's prompt into
the library the same way.

Run: TestMiniMaxCodeInstructions, TestWorkBuddyInstructionsAreAJSONField,
TestWorkBuddyInstructionsFromNothing, TestWorkBuddyInstructionsUnreadFileIsLeftAlone,
TestWorkBuddyInstructionsImport and TestWorkBuddyAIInstructions, in a sandbox
HOME, on Linux (wsl-run) and Windows (win-run); all fail with the two target
assignments taken out (broken by hand), and pass with them. internal/library
whole on Linux (ok) and Windows: 3 reds, TestGoneAgentListedToTakeOut,
TestCheckMixedServerEachSide (both #4115) and TestPluginImportCarryOnOffRemove
(#6065), all three red the same on origin/main d1b4eff5a on Windows.
Not tried: a running WorkBuddy or mcode reading the written values (the apps'
sources were read; WorkBuddy loads its config as it starts, so a running one
may need a restart); WORKBUDDY_USER_DATA_DIR is not followed.
2026-11-11 11:54:21 +08:00

93 lines
2.9 KiB
Go

package library
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"github.com/tidwall/gjson"
"github.com/tidwall/jsonc"
"github.com/yetone/magpie/internal/edit"
)
// An agent whose instructions are a string inside a JSON settings file
// (Target.Field: WorkBuddy's personalization.customPrompt in
// app/app-config.json) is given them there. Everything else in the file is
// the app's and stays as it is, key order and indentation included; what
// is in the field is the user's own text and magpie's part around it, as
// in an instructions file.
// instrRead is what the agent reads at t.Instructions: the file, or for a
// Field target the text at its key path. A file that isn't JSON, or a
// field that holds something other than text, is an error: it is unknown
// what the user has there, and nothing is written over it.
func instrRead(t *Target) ([]byte, error) {
raw, err := edit.Read(t.Instructions)
if err != nil || t.Field == "" {
return raw, err
}
if len(bytes.TrimSpace(raw)) == 0 {
return nil, nil
}
stripped := jsonc.ToJSONInPlace(bytes.Clone(raw))
if !json.Valid(stripped) {
return nil, fmt.Errorf("%s is not JSON magpie can read", t.Instructions)
}
r := gjson.GetBytes(stripped, t.Field)
switch {
case !r.Exists() || r.Type == gjson.Null:
return nil, nil
case r.Type != gjson.String:
return nil, fmt.Errorf("%s in %s is not text: left as it is", t.Field, t.Instructions)
}
return []byte(r.String()), nil
}
// instrText is instrRead as the page reads it: trimmed, "" when unread.
func instrText(t *Target) string {
b, _ := instrRead(t)
return strings.TrimSpace(strings.ReplaceAll(string(b), "\r\n", "\n"))
}
// instrSave writes next as what the agent reads at t.Instructions; "" takes
// it out: the file, or for a Field target the field alone (and the object
// holding it, once that has nothing else, so a file the app never wrote
// into is left as it was).
func instrSave(t *Target, next string) error {
if t.Field == "" {
if next == "" {
return edit.Remove(t.Instructions)
}
return edit.WriteAtomic(t.Instructions, []byte(next))
}
if next = strings.TrimSpace(next); next != "" {
return edit.SetJSON(t.Instructions, edit.KV{Path: t.Field, Value: next})
}
if err := edit.DelJSON(t.Instructions, t.Field); err != nil {
return err
}
raw, err := edit.Read(t.Instructions)
if err != nil {
return err
}
if i := strings.LastIndex(t.Field, "."); i > 0 {
parent := gjson.GetBytes(jsonc.ToJSONInPlace(bytes.Clone(raw)), t.Field[:i])
if parent.IsObject() && len(parent.Map()) == 0 {
return edit.DelJSON(t.Instructions, t.Field[:i])
}
}
return nil
}
// instrEmpty empties what the agent reads at t.Instructions without taking
// the file away, as an apply does (#5709): an instructions file stays,
// empty, and a field goes out of its JSON.
func instrEmpty(t *Target) error {
if t.Field == "" {
return edit.WriteAtomic(t.Instructions, nil)
}
return instrSave(t, "")
}