| 1 | package slides |
| 2 | |
| 3 | // assist_ops.go turns what a model proposed into a batch this app is |
| 4 | // willing to hand a browser — the first half of the safety boundary |
| 5 | // (the second is assist.go's checkAssistOps, which applies the result to |
| 6 | // a scratch copy of the deck through the core and reads it back). |
| 7 | // |
| 8 | // Nothing a model wrote reaches a document except as an operation that |
| 9 | // came through here, and here every operation is REBUILT rather than |
| 10 | // passed on: only the op names the editor's own surfaces use, only the |
| 11 | // keys each one takes, every new id minted by this app, every reference |
| 12 | // resolved against the deck or the batch, and the scope the person |
| 13 | // chose enforced. Deck text is untrusted input to the model, so the |
| 14 | // model's output is untrusted too; what it can do is bounded by this |
| 15 | // vocabulary, not by what it was told. |
| 16 | |
| 17 | import ( |
| 18 | "encoding/json" |
| 19 | "fmt" |
| 20 | "regexp" |
| 21 | |
| 22 | "amadan.net/oficina/slides/internal/slides/deck" |
| 23 | ) |
| 24 | |
| 25 | const ( |
| 26 | // maxAssistOps is the most operations one answer may carry. A |
| 27 | // twenty-slide deck built from nothing is around two hundred. |
| 28 | maxAssistOps = 400 |
| 29 | // maxAssistMarkdown bounds one op's text, in bytes. |
| 30 | maxAssistMarkdown = 20000 |
| 31 | // maxAssistTitle bounds a deck title, in bytes. |
| 32 | maxAssistTitle = 300 |
| 33 | // maxAssistSlides is the export's own ceiling on a deck |
| 34 | // (exports.go): an answer that would take a deck past it is refused |
| 35 | // rather than leaving a deck that cannot be exported. |
| 36 | maxAssistSlides = 200 |
| 37 | ) |
| 38 | |
| 39 | // assistOpKeys is every operation the assistant may propose and the |
| 40 | // keys it may carry — the drive API's vocabulary (docs/api.md, |
| 41 | // agent_edit_contract.go) less the two asset operations: a model has no |
| 42 | // bytes to register, and removing an asset is not something a request |
| 43 | // for words should do. Any other key on an op is dropped. set_meta has |
| 44 | // no "fonts": the instructions never offer it, nothing here validates a |
| 45 | // font stack, and a family the deck's font gate does not know would be |
| 46 | // treated as ready (Claude Fable, #42). |
| 47 | var assistOpKeys = map[string][]string{ |
| 48 | "set_meta": {"title", "theme", "accent", "arrive"}, |
| 49 | "add_slide": {"id", "after", "layout", "template"}, |
| 50 | "remove_slide": {"id"}, |
| 51 | "move_slide": {"id", "after"}, |
| 52 | "duplicate_slide": {"id", "new_id", "new_block_ids"}, |
| 53 | "set_slide": {"id", "layout", "template", "background", "phone", "transition", "talk"}, |
| 54 | "set_notes": {"id", "markdown"}, |
| 55 | "add_block": {"slide", "id", "type", "props", "region", "box", "z", "after", "markdown"}, |
| 56 | "remove_block": {"slide", "id"}, |
| 57 | "move_block": {"slide", "id", "after"}, |
| 58 | "set_block": {"slide", "id", "props", "region", "box", "z", "step", "items"}, |
| 59 | "set_text": {"slide", "id", "markdown"}, |
| 60 | "set_phone": {"slide", "id", "phone"}, |
| 61 | // Scenes: deck-wide, so never under a one-slide request. No author: |
| 62 | // the server sets it from whoever acted (scene_writes.go). |
| 63 | "add_scene": {"id", "name", "code", "states", "words", "data"}, |
| 64 | "set_scene": {"id", "name", "code", "states", "words", "data", "stills"}, |
| 65 | "remove_scene": {"id"}, |
| 66 | } |
| 67 | |
| 68 | // sceneOps change the deck's scenes, which belong to no one slide. |
| 69 | var sceneOps = map[string]bool{"add_scene": true, "set_scene": true, "remove_scene": true} |
| 70 | |
| 71 | // slideOps name a slide in "id"; blockOps name one in "slide". |
| 72 | var slideOps = map[string]bool{"remove_slide": true, "move_slide": true, "duplicate_slide": true, "set_slide": true, "set_notes": true} |
| 73 | |
| 74 | var hexColour = regexp.MustCompile(`^#[0-9a-fA-F]{6}$`) |
| 75 | |
| 76 | // assistScope is what the person chose: the whole deck, or one slide. |
| 77 | type assistScope struct { |
| 78 | slide string // "" for the whole deck |
| 79 | } |
| 80 | |
| 81 | // assistRefusal is a batch this file or the core would not take. Its |
| 82 | // message is written for the MODEL — it goes back in the repair round — |
| 83 | // and is never logged: it quotes ids the model chose. |
| 84 | type assistRefusal struct{ msg string } |
| 85 | |
| 86 | func (e *assistRefusal) Error() string { return e.msg } |
| 87 | |
| 88 | func refuse(format string, args ...any) error { |
| 89 | return &assistRefusal{msg: fmt.Sprintf(format, args...)} |
| 90 | } |
| 91 | |
| 92 | // normalizedOps is a batch ready to check: the ops, and the first slide |
| 93 | // it adds (where the editor goes to show the result), if any. |
| 94 | type normalizedOps struct { |
| 95 | ops []map[string]json.RawMessage |
| 96 | focus string |
| 97 | } |
| 98 | |
| 99 | // normalizeAssistOps rebuilds raw against view under scope. |
| 100 | // |
| 101 | // Ids: every add_slide, add_block and duplicate_slide gets a fresh id |
| 102 | // from newID, whatever the model wrote. What it wrote is a placeholder, |
| 103 | // recorded so later ops in the batch can name what earlier ones made; |
| 104 | // a placeholder that collides with a real id or another placeholder is |
| 105 | // refused, since every reference to it would be ambiguous. |
| 106 | func normalizeAssistOps(view *deck.Deck, raw []map[string]json.RawMessage, scope assistScope, mint func() (string, error)) (normalizedOps, error) { |
| 107 | var out normalizedOps |
| 108 | if len(raw) > maxAssistOps { |
| 109 | return out, refuse("%d operations is more than the %d allowed in one answer", len(raw), maxAssistOps) |
| 110 | } |
| 111 | existing := map[string]bool{} |
| 112 | blocks := map[string]int{} |
| 113 | for _, s := range view.Slides { |
| 114 | existing[s.ID] = true |
| 115 | blocks[s.ID] = len(s.Blocks) |
| 116 | for _, b := range s.Blocks { |
| 117 | existing[b.ID] = true |
| 118 | } |
| 119 | } |
| 120 | for id := range view.Scenes { |
| 121 | existing[id] = true |
| 122 | } |
| 123 | placeholders := map[string]string{} |
| 124 | fresh := map[string]bool{} // slides this batch adds |
| 125 | resolve := func(ref string) string { |
| 126 | if id, ok := placeholders[ref]; ok { |
| 127 | return id |
| 128 | } |
| 129 | return ref |
| 130 | } |
| 131 | create := func(i int, given string) (string, error) { |
| 132 | if given != "" && (existing[given] || placeholders[given] != "") { |
| 133 | return "", refuse("op %d: id %q is already in use; give every new slide and block its own placeholder id", i, given) |
| 134 | } |
| 135 | id, err := mint() |
| 136 | if err != nil { |
| 137 | return "", err |
| 138 | } |
| 139 | if given != "" { |
| 140 | placeholders[given] = id |
| 141 | } |
| 142 | return id, nil |
| 143 | } |
| 144 | |
| 145 | for i, op := range raw { |
| 146 | name := rawString(op, "op") |
| 147 | keys, ok := assistOpKeys[name] |
| 148 | if !ok { |
| 149 | return out, refuse("op %d: %q is not an operation you may use", i, name) |
| 150 | } |
| 151 | o := map[string]json.RawMessage{"op": op["op"]} |
| 152 | for _, k := range keys { |
| 153 | if v, has := op[k]; has { |
| 154 | o[k] = v |
| 155 | } |
| 156 | } |
| 157 | setString := func(key, value string) { o[key], _ = json.Marshal(value) } |
| 158 | remap := func(key string) { |
| 159 | if s := rawString(o, key); s != "" { |
| 160 | setString(key, resolve(s)) |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | // Text sizes, before anything else reads them. |
| 165 | if v, has := o["markdown"]; has && len(v) > maxAssistMarkdown { |
| 166 | return out, refuse("op %d: the text is longer than %d bytes", i, maxAssistMarkdown) |
| 167 | } |
| 168 | |
| 169 | if sceneOps[name] && scope.slide != "" { |
| 170 | return out, refuse("op %d: %s changes the deck's scenes, and the request was for one slide", i, name) |
| 171 | } |
| 172 | switch name { |
| 173 | case "add_scene": |
| 174 | id, err := create(i, rawString(op, "id")) |
| 175 | if err != nil { |
| 176 | return out, err |
| 177 | } |
| 178 | setString("id", id) |
| 179 | case "set_meta": |
| 180 | if scope.slide != "" { |
| 181 | return out, refuse("op %d: set_meta changes the whole deck, and the request was for one slide", i) |
| 182 | } |
| 183 | if _, has := o["title"]; has && len(rawString(o, "title")) > maxAssistTitle { |
| 184 | return out, refuse("op %d: the title is longer than %d bytes", i, maxAssistTitle) |
| 185 | } |
| 186 | if _, has := o["theme"]; has && !validTheme(rawString(o, "theme")) { |
| 187 | return out, refuse("op %d: theme must be one of paper, ink, sunset, forest, slate, candy", i) |
| 188 | } |
| 189 | if v, has := o["accent"]; has && string(v) != "null" && !hexColour.MatchString(rawString(o, "accent")) { |
| 190 | return out, refuse("op %d: accent must be a colour like \"#c0392b\", or null", i) |
| 191 | } |
| 192 | case "add_slide": |
| 193 | id, err := create(i, rawString(op, "id")) |
| 194 | if err != nil { |
| 195 | return out, err |
| 196 | } |
| 197 | setString("id", id) |
| 198 | remap("after") |
| 199 | fresh[id] = true |
| 200 | blocks[id] = 0 |
| 201 | if out.focus == "" { |
| 202 | out.focus = id |
| 203 | } |
| 204 | case "duplicate_slide": |
| 205 | remap("id") |
| 206 | source := rawString(o, "id") |
| 207 | id, err := create(i, rawString(op, "new_id")) |
| 208 | if err != nil { |
| 209 | return out, err |
| 210 | } |
| 211 | setString("new_id", id) |
| 212 | // As many block ids as the source has, minted; the model's |
| 213 | // own list, if it sent one of the right length, names them. |
| 214 | var given []string |
| 215 | _ = json.Unmarshal(op["new_block_ids"], &given) |
| 216 | n := blocks[source] |
| 217 | ids := make([]string, n) |
| 218 | for k := range n { |
| 219 | g := "" |
| 220 | if len(given) == n { |
| 221 | g = given[k] |
| 222 | } |
| 223 | if ids[k], err = create(i, g); err != nil { |
| 224 | return out, err |
| 225 | } |
| 226 | } |
| 227 | o["new_block_ids"], _ = json.Marshal(ids) |
| 228 | fresh[id] = true |
| 229 | blocks[id] = n |
| 230 | if out.focus == "" { |
| 231 | out.focus = id |
| 232 | } |
| 233 | case "add_block": |
| 234 | remap("slide") |
| 235 | remap("after") |
| 236 | id, err := create(i, rawString(op, "id")) |
| 237 | if err != nil { |
| 238 | return out, err |
| 239 | } |
| 240 | setString("id", id) |
| 241 | blocks[rawString(o, "slide")]++ |
| 242 | default: |
| 243 | if slideOps[name] { |
| 244 | remap("id") |
| 245 | } else { |
| 246 | remap("slide") |
| 247 | remap("id") |
| 248 | } |
| 249 | remap("after") |
| 250 | } |
| 251 | if err := remapSceneProp(o, resolve); err != nil { |
| 252 | return out, refuse("op %d: %v", i, err) |
| 253 | } |
| 254 | if err := remapChildren(o, resolve); err != nil { |
| 255 | return out, refuse("op %d: %v", i, err) |
| 256 | } |
| 257 | switch name { |
| 258 | case "remove_slide": |
| 259 | delete(blocks, rawString(o, "id")) |
| 260 | case "remove_block": |
| 261 | if s := rawString(o, "slide"); blocks[s] > 0 { |
| 262 | blocks[s]-- |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | // The scope: one slide means that slide and the slides this |
| 267 | // batch adds (splitting a slide adds one after it), nothing |
| 268 | // else — and never removing or moving the slide asked about. |
| 269 | if scope.slide != "" && name != "set_meta" && name != "add_slide" { |
| 270 | target := rawString(o, "slide") |
| 271 | if slideOps[name] { |
| 272 | target = rawString(o, "id") |
| 273 | } |
| 274 | allowed := fresh[target] || (target == scope.slide && name != "remove_slide" && name != "move_slide") |
| 275 | if !allowed { |
| 276 | return out, refuse("op %d: the request was for slide %q only, and this changes another", i, scope.slide) |
| 277 | } |
| 278 | } |
| 279 | out.ops = append(out.ops, o) |
| 280 | } |
| 281 | if out.focus == "" { |
| 282 | out.focus = scope.slide |
| 283 | } |
| 284 | return out, nil |
| 285 | } |
| 286 | |
| 287 | // remapChildren resolves placeholders in a columns block's |
| 288 | // props.children, the one place an id sits inside props. |
| 289 | func remapChildren(o map[string]json.RawMessage, resolve func(string) string) error { |
| 290 | raw, has := o["props"] |
| 291 | if !has { |
| 292 | return nil |
| 293 | } |
| 294 | var props map[string]json.RawMessage |
| 295 | if err := json.Unmarshal(raw, &props); err != nil || props == nil { |
| 296 | return fmt.Errorf("props must be an object") |
| 297 | } |
| 298 | children, has := props["children"] |
| 299 | if !has { |
| 300 | return nil |
| 301 | } |
| 302 | var ids []string |
| 303 | if err := json.Unmarshal(children, &ids); err != nil { |
| 304 | return fmt.Errorf("props.children must be a list of block ids") |
| 305 | } |
| 306 | for k, id := range ids { |
| 307 | ids[k] = resolve(id) |
| 308 | } |
| 309 | props["children"], _ = json.Marshal(ids) |
| 310 | o["props"], _ = json.Marshal(props) |
| 311 | return nil |
| 312 | } |
| 313 | |
| 314 | // remapSceneProp resolves a placeholder in a scene block's props.scene, |
| 315 | // so a block can show a scene the same batch adds. |
| 316 | func remapSceneProp(o map[string]json.RawMessage, resolve func(string) string) error { |
| 317 | raw, has := o["props"] |
| 318 | if !has { |
| 319 | return nil |
| 320 | } |
| 321 | var props map[string]json.RawMessage |
| 322 | if err := json.Unmarshal(raw, &props); err != nil || props == nil { |
| 323 | return fmt.Errorf("props must be an object") |
| 324 | } |
| 325 | var scene string |
| 326 | if json.Unmarshal(props["scene"], &scene) != nil || scene == "" { |
| 327 | return nil |
| 328 | } |
| 329 | props["scene"], _ = json.Marshal(resolve(scene)) |
| 330 | o["props"], _ = json.Marshal(props) |
| 331 | return nil |
| 332 | } |
| 333 | |
| 334 | // checkAssistResult is the deck-level half: what the batch leaves. |
| 335 | // before is the deck it was proposed against. |
| 336 | func checkAssistResult(before, after *deck.Deck) error { |
| 337 | if len(before.Slides) > 0 && len(after.Slides) == 0 { |
| 338 | return refuse("those operations would remove every slide; a deck is never emptied from here") |
| 339 | } |
| 340 | if len(after.Slides) > maxAssistSlides { |
| 341 | return refuse("those operations would leave %d slides, more than the %d a deck may have", len(after.Slides), maxAssistSlides) |
| 342 | } |
| 343 | return nil |
| 344 | } |
| 345 | |