| 1 | package slides |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | |
| 7 | access "amadan.net/oficina/agent-access.git" |
| 8 | strictjson "github.com/go-json-experiment/json" |
| 9 | ) |
| 10 | |
| 11 | func slideAgentObject(props map[string]any, required ...string) json.RawMessage { |
| 12 | b, _ := json.Marshal(map[string]any{"type": "object", "properties": props, "required": required, "additionalProperties": false}) |
| 13 | return b |
| 14 | } |
| 15 | func slideAgentString(max int) map[string]any { |
| 16 | return map[string]any{"type": "string", "maxLength": max} |
| 17 | } |
| 18 | func (a *slideAgent) registry() (*access.Registry, error) { |
| 19 | input := func(props map[string]any, required ...string) json.RawMessage { |
| 20 | props["team"] = map[string]any{"type": "string", "minLength": 1, "maxLength": 128} |
| 21 | return slideAgentObject(props, append([]string{"team"}, required...)...) |
| 22 | } |
| 23 | sqid := map[string]any{"type": "string", "pattern": "^[23456789abcdefghjkmnpqrstuvwxyz]{10}$"} |
| 24 | revision := map[string]any{"type": "string", "pattern": "^(0|[1-9][0-9]{0,18})$"} |
| 25 | summary := slideAgentObject(map[string]any{"sqid": sqid, "title": slideAgentString(slideAgentLimit), "theme": slideAgentString(128), "revision": revision}, "sqid", "title", "theme", "revision") |
| 26 | initial := slideAgentObject(map[string]any{"heading": slideAgentString(128 << 10), "body_markdown": slideAgentString(128 << 10), "notes_markdown": slideAgentString(128 << 10)}, "heading", "body_markdown", "notes_markdown") |
| 27 | ops := []access.Operation{ |
| 28 | {Name: "slides_list_decks", Description: "List currently readable presentations in this team.", Scope: "slides:read", ReadOnly: true, Idempotent: true, Input: input(map[string]any{"limit": map[string]any{"type": "integer", "minimum": 1, "maximum": 100}, "cursor": map[string]any{"type": "string", "pattern": "^[1-9][0-9]{0,18}$"}}), Output: slideAgentObject(map[string]any{"decks": map[string]any{"type": "array", "maxItems": 100, "items": summary}, "next_cursor": slideAgentString(19)}, "decks", "next_cursor"), Invoke: func(ctx context.Context, p access.Principal, raw json.RawMessage) (any, error) { |
| 29 | var in struct { |
| 30 | Limit int `json:"limit"` |
| 31 | Cursor string `json:"cursor"` |
| 32 | } |
| 33 | if json.Unmarshal(raw, &in) != nil { |
| 34 | return nil, access.Fail("invalid", "Invalid pagination arguments.") |
| 35 | } |
| 36 | return a.list(ctx, slideAgentMember(ctx), in.Limit, in.Cursor) |
| 37 | }}, |
| 38 | {Name: "slides_get_deck", Description: "Read a presentation's canonical block tree and speaker notes at one revision.", Scope: "slides:read", ReadOnly: true, Idempotent: true, Input: input(map[string]any{"sqid": sqid}, "sqid"), Output: slideAgentObject(map[string]any{"sqid": sqid, "revision": revision, "deck": map[string]any{"type": "object"}}, "sqid", "revision", "deck"), Invoke: func(ctx context.Context, p access.Principal, raw json.RawMessage) (any, error) { |
| 39 | var in struct { |
| 40 | Sqid string `json:"sqid"` |
| 41 | } |
| 42 | if json.Unmarshal(raw, &in) != nil { |
| 43 | return nil, access.Fail("invalid", "Invalid deck identifier.") |
| 44 | } |
| 45 | return a.get(ctx, slideAgentMember(ctx), in.Sqid) |
| 46 | }}, |
| 47 | {Name: "slides_create_deck", Description: "Create a private presentation with up to fifty initial slides. Reuse the same client UUID only for retries.", Scope: "slides:write", Idempotent: true, Input: input(map[string]any{"client_id": slideAgentString(36), "title": map[string]any{"type": "string", "minLength": 1, "maxLength": 512}, "theme": map[string]any{"enum": themeIDs}, "slides": map[string]any{"type": "array", "minItems": 1, "maxItems": 50, "items": initial}}, "client_id", "title", "slides"), Output: slideAgentObject(map[string]any{"sqid": sqid, "url": slideAgentString(4096), "revision": revision}, "sqid", "url", "revision"), Invoke: func(ctx context.Context, p access.Principal, raw json.RawMessage) (any, error) { |
| 48 | var in slideAgentCreate |
| 49 | if json.Unmarshal(raw, &in) != nil { |
| 50 | return nil, access.Fail("invalid", "Invalid initial deck arguments.") |
| 51 | } |
| 52 | return a.create(ctx, p, slideAgentMember(ctx), in) |
| 53 | }}, |
| 54 | } |
| 55 | ops = append(ops, a.updateOperation(input, sqid, revision)) |
| 56 | for i := range ops { |
| 57 | scope, invoke := ops[i].Scope, ops[i].Invoke |
| 58 | ops[i].Available = func(p access.Principal) bool { return p.Allows(a.team, scope) } |
| 59 | ops[i].Invoke = func(ctx context.Context, p access.Principal, raw json.RawMessage) (any, error) { |
| 60 | if len(raw) > slideAgentLimit { |
| 61 | return nil, access.Fail("too_large", "The operation input exceeds 1 MiB.") |
| 62 | } |
| 63 | var in struct { |
| 64 | Team string `json:"team"` |
| 65 | } |
| 66 | if strictjson.Unmarshal(raw, &in) != nil { |
| 67 | return nil, access.Fail("invalid", "Send valid Unicode JSON.") |
| 68 | } |
| 69 | if in.Team != a.team { |
| 70 | return nil, access.Fail("forbidden", "This instance does not serve that team.") |
| 71 | } |
| 72 | u, e := a.member(ctx, p, scope == "slides:write") |
| 73 | if e != nil { |
| 74 | return nil, e |
| 75 | } |
| 76 | state, ok := ctx.Value(slideAgentKey{}).(*slideAgentContext) |
| 77 | if !ok || state == nil { |
| 78 | return nil, access.Fail("unavailable", "Request authority is unavailable.") |
| 79 | } |
| 80 | if state.member.ID != u.ID || state.member.Email != u.Email { |
| 81 | return nil, access.Fail("forbidden", "The current identity changed.") |
| 82 | } |
| 83 | return invoke(ctx, p, raw) |
| 84 | } |
| 85 | } |
| 86 | return access.NewRegistry(ops...) |
| 87 | } |
| 88 | func AgentContracts() ([]access.Operation, error) { |
| 89 | a := &slideAgent{team: "contract"} |
| 90 | r, e := a.registry() |
| 91 | if e != nil { |
| 92 | return nil, e |
| 93 | } |
| 94 | return r.List(access.Principal{Subject: "contract", Teams: []string{"contract"}, Scopes: []string{"slides:read", "slides:write"}}), nil |
| 95 | } |
| 96 | |