oficina / sheets Public

Clone
git clone https://amadan.net/oficina/sheets

Plain git — no account needed to clone.

Download

Download this file

1package sheets
2
3import (
4 "context"
5 "encoding/json"
6
7 access "amadan.net/oficina/agent-access.git"
8 strictjson "github.com/go-json-experiment/json"
9)
10
11func sheetAgentObject(properties map[string]any, required ...string) json.RawMessage {
12 raw, _ := json.Marshal(map[string]any{"type": "object", "properties": properties, "required": required, "additionalProperties": false})
13 return raw
14}
15func sheetAgentString(max int) map[string]any {
16 return map[string]any{"type": "string", "maxLength": max}
17}
18func sheetAgentInteger(max int) map[string]any {
19 return map[string]any{"type": "integer", "minimum": 0, "maximum": max}
20}
21func sheetAgentIDSchema() map[string]any {
22 return map[string]any{"type": "string", "pattern": "^[1-9][0-9]{0,18}$"}
23}
24func (a *sheetAgent) registry() (*access.Registry, error) {
25 input := func(properties map[string]any, required ...string) json.RawMessage {
26 properties["team"] = map[string]any{"type": "string", "minLength": 1, "maxLength": 128}
27 return sheetAgentObject(properties, append([]string{"team"}, required...)...)
28 }
29 page := func(field string, item json.RawMessage) json.RawMessage {
30 return sheetAgentObject(map[string]any{field: map[string]any{"type": "array", "maxItems": 100, "items": item}, "next_cursor": sheetAgentString(19)}, field, "next_cursor")
31 }
32 summary := sheetAgentObject(map[string]any{"id": sheetAgentIDSchema(), "title": sheetAgentString(1024), "url": sheetAgentString(4096)}, "id", "title", "url")
33 tab := sheetAgentObject(map[string]any{"id": sheetAgentIDSchema(), "name": sheetAgentString(1024)}, "id", "name")
34 cell := sheetAgentObject(map[string]any{"row": sheetAgentInteger(4999), "column": sheetAgentInteger(599), "raw": sheetAgentString(8192)}, "row", "column", "raw")
35 value := sheetAgentObject(map[string]any{"row": sheetAgentInteger(4999), "column": sheetAgentInteger(599), "raw": sheetAgentString(8192), "kind": map[string]any{"enum": []string{"empty", "number", "string", "boolean", "error"}}, "value": map[string]any{"type": []string{"null", "number", "string", "boolean"}}, "display": sheetAgentString(sheetAgentLimit)}, "row", "column", "raw", "kind", "value", "display")
36 ops := []access.Operation{
37 {Name: "sheets_set_range", Description: "Atomically edit 1–500 cells at expected_revision. Keep cell order and client UUID on retry; revision in the acknowledgement is the original committed revision. Read again for current values.", Scope: "sheets:write", Idempotent: true, Input: input(map[string]any{"sheet_id": sheetAgentIDSchema(), "tab_id": sheetAgentIDSchema(), "expected_revision": sheetAgentIDSchema(), "client_id": sheetAgentString(36), "cells": map[string]any{"type": "array", "minItems": 1, "maxItems": 500, "items": cell}}, "sheet_id", "tab_id", "expected_revision", "client_id", "cells"), Output: sheetAgentObject(map[string]any{"sheet_id": sheetAgentIDSchema(), "tab_id": sheetAgentIDSchema(), "revision": sheetAgentIDSchema(), "url": sheetAgentString(4096)}, "sheet_id", "tab_id", "revision", "url"), Invoke: func(ctx context.Context, p access.Principal, raw json.RawMessage) (any, error) {
38 var in sheetAgentSetRange
39 if err := strictjson.Unmarshal(raw, &in); err != nil {
40 return nil, access.Fail("invalid", "Invalid range edit arguments.")
41 }
42 return a.setRange(ctx, p, sheetAgentMember(ctx), in)
43 }},
44 {Name: "sheets_list", Description: "List spreadsheets currently readable in this team.", Scope: "sheets:read", ReadOnly: true, Idempotent: true, Input: input(map[string]any{"limit": map[string]any{"type": "integer", "minimum": 1, "maximum": 100}, "cursor": sheetAgentIDSchema()}), Output: page("sheets", summary), Invoke: func(ctx context.Context, p access.Principal, raw json.RawMessage) (any, error) {
45 var in struct {
46 Limit int `json:"limit"`
47 Cursor string `json:"cursor"`
48 }
49 if json.Unmarshal(raw, &in) != nil {
50 return nil, access.Fail("invalid", "Invalid pagination arguments.")
51 }
52 return a.list(ctx, sheetAgentMember(ctx), in.Limit, in.Cursor)
53 }},
54 {Name: "sheets_list_tabs", Description: "List tabs in one currently readable spreadsheet.", Scope: "sheets:read", ReadOnly: true, Idempotent: true, Input: input(map[string]any{"sheet_id": sheetAgentIDSchema(), "limit": map[string]any{"type": "integer", "minimum": 1, "maximum": 100}, "cursor": sheetAgentIDSchema()}, "sheet_id"), Output: page("tabs", tab), Invoke: func(ctx context.Context, p access.Principal, raw json.RawMessage) (any, error) {
55 var in struct {
56 SheetID string `json:"sheet_id"`
57 Limit int `json:"limit"`
58 Cursor string `json:"cursor"`
59 }
60 if json.Unmarshal(raw, &in) != nil {
61 return nil, access.Fail("invalid", "Invalid tab pagination arguments.")
62 }
63 id, err := sheetAgentID(in.SheetID)
64 if err != nil {
65 return nil, err
66 }
67 return a.tabs(ctx, sheetAgentMember(ctx), id, in.Limit, in.Cursor)
68 }},
69 {Name: "sheets_get_range", Description: "Read at most 500 cells with raw input, calculated values and one coherent tab revision.", Scope: "sheets:read", ReadOnly: true, Idempotent: true, Input: input(map[string]any{"sheet_id": sheetAgentIDSchema(), "tab_id": sheetAgentIDSchema(), "row": sheetAgentInteger(4999), "column": sheetAgentInteger(599), "height": map[string]any{"type": "integer", "minimum": 1, "maximum": 500}, "width": map[string]any{"type": "integer", "minimum": 1, "maximum": 500}}, "sheet_id", "tab_id", "row", "column", "height", "width"), Output: sheetAgentObject(map[string]any{"sheet_id": sheetAgentIDSchema(), "tab_id": sheetAgentIDSchema(), "revision": sheetAgentIDSchema(), "cells": map[string]any{"type": "array", "maxItems": 500, "items": value}}, "sheet_id", "tab_id", "revision", "cells"), Invoke: func(ctx context.Context, p access.Principal, raw json.RawMessage) (any, error) {
70 var in struct {
71 SheetID string `json:"sheet_id"`
72 TabID string `json:"tab_id"`
73 Row int `json:"row"`
74 Column int `json:"column"`
75 Height int `json:"height"`
76 Width int `json:"width"`
77 }
78 if json.Unmarshal(raw, &in) != nil {
79 return nil, access.Fail("invalid", "Invalid range arguments.")
80 }
81 sheet, e := sheetAgentID(in.SheetID)
82 if e != nil {
83 return nil, e
84 }
85 tab, e := sheetAgentID(in.TabID)
86 if e != nil {
87 return nil, e
88 }
89 return a.readRange(ctx, sheetAgentMember(ctx), sheet, tab, in.Row, in.Column, in.Height, in.Width)
90 }},
91 {Name: "sheets_create", Description: "Create a spreadsheet and first tab with up to 500 initial cells; reuse the same client UUID only for retries.", Scope: "sheets:write", Idempotent: true, Input: input(map[string]any{"client_id": sheetAgentString(36), "title": map[string]any{"type": "string", "minLength": 1, "maxLength": 200}, "tab_name": map[string]any{"type": "string", "minLength": 1, "maxLength": 200}, "cells": map[string]any{"type": "array", "maxItems": 500, "items": cell}}, "client_id", "title", "tab_name", "cells"), Output: sheetAgentObject(map[string]any{"sheet_id": sheetAgentIDSchema(), "tab_id": sheetAgentIDSchema(), "revision": sheetAgentIDSchema(), "url": sheetAgentString(4096)}, "sheet_id", "tab_id", "revision", "url"), Invoke: func(ctx context.Context, p access.Principal, raw json.RawMessage) (any, error) {
92 var in sheetAgentCreate
93 if json.Unmarshal(raw, &in) != nil {
94 return nil, access.Fail("invalid", "Invalid initial sheet arguments.")
95 }
96 return a.create(ctx, p, sheetAgentMember(ctx), in)
97 }},
98 }
99 for i := range ops {
100 scope := ops[i].Scope
101 invoke := ops[i].Invoke
102 ops[i].Available = func(p access.Principal) bool { return p.Allows(a.team, scope) }
103 ops[i].Invoke = func(ctx context.Context, p access.Principal, raw json.RawMessage) (any, error) {
104 if len(raw) > sheetAgentLimit {
105 return nil, access.Fail("too_large", "The operation input exceeds 1 MiB.")
106 }
107 var in struct {
108 Team string `json:"team"`
109 }
110 if strictjson.Unmarshal(raw, &in) != nil {
111 return nil, access.Fail("invalid", "Send valid Unicode JSON.")
112 }
113 if in.Team != a.team {
114 return nil, access.Fail("forbidden", "This instance does not serve that team.")
115 }
116 member, err := a.member(ctx, p, scope == "sheets:write")
117 if err != nil {
118 return nil, err
119 }
120 state, ok := ctx.Value(sheetAgentKey{}).(*sheetAgentContext)
121 if !ok || state == nil {
122 return nil, access.Fail("unavailable", "Request authority is unavailable.")
123 }
124 if state.member.ID != member.ID || state.member.Address != member.Address {
125 return nil, access.Fail("forbidden", "The current identity changed.")
126 }
127 state.member = member
128 return invoke(ctx, p, raw)
129 }
130 }
131 return access.NewRegistry(ops...)
132}
133func sheetAgentMember(ctx context.Context) Member {
134 if state, ok := ctx.Value(sheetAgentKey{}).(*sheetAgentContext); ok && state != nil {
135 return state.member
136 }
137 return Member{}
138}
139func AgentContracts() ([]access.Operation, error) {
140 a := &sheetAgent{team: "contract"}
141 registry, err := a.registry()
142 if err != nil {
143 return nil, err
144 }
145 return registry.List(access.Principal{Subject: "contract", Teams: []string{"contract"}, Scopes: []string{"sheets:read", "sheets:write"}}), nil
146}
147