Ask state and events carry files and can_attach (Task 6)
Human
Prompt
Build Base's half of files in Ask, slice 1, task by task from the approved plan, tests first.
From this commit's own message. The prompt ledger has no entries for it — which is what a squashed, rebased or amended commit looks like, and what every commit looks like in a repo without a ledger.
Summary
Message.Clean keeps only attachments with an upload id and P7's seven fields (unknown kind is a file); project() and the snapshot views use it; /ask/state gains can_attach (ready only); native contract pins the new fields; base-native-v1.md documents attachments, can_attach and the Files routes. Fixes a relay bug the new test caught (the message was cleaned before it was decoded). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
9 files changed,
+289
−26
docs/api/base-native-v1.md+80 −7internal/equipo/chat.go+1 −1internal/equipo/files.go+38 −0internal/web/ask.go+12 −2internal/web/ask_files.go+3 −8internal/web/ask_relay.go+5 −2internal/web/relay_internal_test.go+24 −0internal/webtest/ask_files_test.go+78 −4internal/webtest/ask_native_contract_test.go+48 −2
diff --git a/docs/api/base-native-v1.md b/docs/api/base-native-v1.md| index 24fdf29..d2a29a5 100644 |
| --- a/docs/api/base-native-v1.md |
| +++ b/docs/api/base-native-v1.md |
| @@ -111,7 +111,7 @@ for a form). Every answer is `no-store`. |
| ```json |
| {"state": "ready", "messages": [Message…], "jobs": [Job…], "cards": [Card…], "turn": Turn|null, |
| "status": "Thinking…", "cursor": 42, "renew_after": "2026-11-28T10:00:00Z", "equipo_origin": "https://equipo.acme.example", |
| - "disconnected": false} |
| + "disconnected": false, "can_attach": true} |
| ``` |
| - `messages`, `jobs` and `cards` are never `null`. Outside `ready`, |
| @@ -131,10 +131,22 @@ for a form). Every answer is `no-store`. |
| While it is, no client sets Ask up by itself; the app offers Connect |
| instead. `state` is then `connect`, as after any Disconnect, or `pending` |
| while a code from the web's Connect waits for approval (still `true`). |
| +- `can_attach` is `true` only in `ready`, when Equipo says the person's |
| + runner can take files. Offer attaching only then; an older Equipo never |
| + says so. |
| -**Message** `{"id", "role": "person"|"assistant", "text", "turn_id", "created_at"}`. |
| +**Message** `{"id", "role": "person"|"assistant", "text", "turn_id", "created_at", "attachments"?}`. |
| An assistant's `text` may carry the little Markdown `ask.js` draws (lists, |
| -links to http(s) only, bold and italics). |
| +links to http(s) only, bold and italics). `attachments` is there only on a |
| +person's message with files, in the order they were attached; its `text` |
| +may then be `""`. Until the app draws files, show their names as a line |
| +under the text, so a bubble is never empty. |
| + |
| +**Attachment** `{"id", "kind": "image"|"file", "name", "media_type", "size", "width", "height"}`. |
| +`size` is in bytes; `width` and `height` are an image's upright size, `0` |
| +for a file. Treat any other `kind` as `file`. Read it from Base, never from |
| +Equipo: `GET /ask/attachments/{id}`, and an image's thumbnail (at most |
| +1024 px) at `/ask/attachments/{id}/preview` (see Files). |
| **Turn** `{"id", "kind", "state": "queued"|"running"|"waiting"|"replied"|"failed"|"cancelled", "reason"}`. |
| @@ -184,12 +196,23 @@ data: {"type":"message","data":Message} |
| ### `POST /ask/messages` |
| -Form: `text` (1 to 16 KiB after trimming) and `key`, a fresh idempotency key |
| -per message (at most 128 bytes, not starting `reply:` or `card:`). A resend |
| -after a network error reuses the key: Equipo keeps one message per key. |
| +Form: `text` (at most 16 KiB after trimming; valid UTF-8, no NUL), up to |
| +five `attachments` (upload ids, in order; see Files) and `key`, a fresh |
| +idempotency key per message (at most 128 bytes, not starting `reply:`, |
| +`card:` or `handoff:`). Text may be empty when there are attachments. A |
| +resend after a network error reuses the key: Equipo keeps one message per |
| +key, with its files. |
| - `200 {"message": Message, "turn": Turn}`. |
| -- `422 {"state":"ready"}`: empty or too long. |
| +- `422 {"state":"ready"}`: empty, too long, not text, or more than five |
| + attachments or one that isn't an upload id. Nothing was sent. |
| +- With attachments only: |
| + - `409 {"state":"ready","files":"unavailable"}`: the person's runner |
| + can't take files right now. Ask stays ready; nothing was stored. Drop |
| + the files (stop offering them: `can_attach` is now `false`) and the |
| + words can go alone. |
| + - `422 {"state":"ready","files":"stale"}`: an upload is gone (dropped |
| + after 24 hours unsent, removed, or already sent). Attach it again. |
| ### `POST /ask/turns/{id}/cancel` |
| @@ -232,6 +255,56 @@ outcome (a resend after a network error or a `502` reuses it). |
| | `502` | `{"state":"unavailable"}` | Equipo didn't answer. Try again with the same key. | |
| | `400` | plain text | The form couldn't be read. | |
| +### Files |
| + |
| +An upload belongs to the person until it is sent, or for 24 hours; at |
| +most 20 wait unsent. Base keeps no copy: it relays to Equipo with the |
| +person's key. Equipo's limits: 10 MiB a file, at most 12,000 px a side and |
| +40 megapixels an image, and nine types (PNG, JPEG, WebP, GIF, PDF, plain |
| +text, Markdown, CSV, JSON), sniffed from the bytes. HEIC is refused. |
| + |
| +#### `POST /ask/attachments` |
| + |
| +The file's bytes as the body (with `Content-Length`), its type in |
| +`Content-Type`, its name in `Content-Disposition` (`filename`, or |
| +`filename*` beyond ASCII). Not a form, and no key: a retry is a second |
| +upload. |
| + |
| +- `201 Attachment`. |
| +- `413 {"code":"attachment_too_large"}`: over 10 MiB, refused before the |
| + body is read when the length says so. |
| +- Equipo's refusals, with its status: `415 {"code":"attachment_type"}`, |
| + `415 {"code":"attachment_heic"}`, `413 {"code":"attachment_pixels"}`, |
| + `409 {"code":"attachment_limit"}` (20 wait unsent: send or remove some). |
| +- `503 {"code":"busy"}` with `Retry-After` (seconds): every upload slot is |
| + busy. Try again after it, by itself. |
| +- `409 {"state"}` and `502 {"state":"unavailable"}` as for every write. |
| + |
| +#### `DELETE /ask/attachments/{id}` |
| + |
| +Removes an unsent upload, freeing its place among the 20. `204`, also when |
| +it was gone already. `409 {"state":"ready","code":"attachment_sent"}` once |
| +it is on a message. `404` for anything that isn't an upload id. |
| + |
| +#### `POST /ask/attachments/discard` |
| + |
| +The web page's beacon as it goes away: form, up to five `id`. Base deletes |
| +each as best it can and always answers `204`. |
| + |
| +#### `GET /ask/attachments/{id}`, `GET /ask/attachments/{id}/preview` |
| + |
| +The file, streamed (`HEAD` too). `/preview` is an image's thumbnail, and |
| +`404` for any other file. PNG, JPEG, WebP and GIF are `inline`; everything |
| +else is an `attachment` download (text with `charset=utf-8`), and a type |
| +outside the nine is `application/octet-stream`. Every answer has |
| +`X-Content-Type-Options: nosniff` and a sandboxing CSP. A preview may be |
| +cached by this client for an hour (`private, max-age=3600`); the file |
| +itself is `no-store`. |
| + |
| +- `404` (empty) for a file that isn't the person's, or without a live Ask |
| + connection; never a redirect. |
| +- `502` (empty) when Equipo fails or is slow. |
| + |
| ### `POST /ask/disconnect` |
| Ends the person's Ask connection, revoking the key in Equipo, whichever |
diff --git a/internal/equipo/chat.go b/internal/equipo/chat.go| index b387865..d18d676 100644 |
| --- a/internal/equipo/chat.go |
| +++ b/internal/equipo/chat.go |
| @@ -30,7 +30,7 @@ func InFlight(state string) bool { |
| // Message is one line of the conversation; Role is person or assistant. |
| // Attachments are the files the person sent with it, in their order |
| -// (Clean keeps only a usable id). |
| +// (Clean keeps those Base passes on). |
| type Message struct { |
| ID string `json:"id"` |
| Role string `json:"role"` |
diff --git a/internal/equipo/files.go b/internal/equipo/files.go| index 00a184b..7889128 100644 |
| --- a/internal/equipo/files.go |
| +++ b/internal/equipo/files.go |
| @@ -2,6 +2,9 @@ package equipo |
| import ( |
| "bytes" |
| + "regexp" |
| + "strings" |
| + "unicode/utf8" |
| "context" |
| "encoding/json" |
| "errors" |
| @@ -28,6 +31,41 @@ type Attachment struct { |
| Height int `json:"height"` |
| } |
| +// ValidAttachmentID reports whether id has the shape of an Equipo |
| +// attachment id: Base calls Equipo with no other. |
| +func ValidAttachmentID(id string) bool { return attachmentID.MatchString(id) } |
| + |
| +var attachmentID = regexp.MustCompile(`^at_[a-z0-9]{8,60}$`) |
| + |
| +// Clean is the message as Base passes it on: only the attachments whose id |
| +// it would call Equipo with, each with a kind Base draws (an unknown kind |
| +// is a file), a name of valid UTF-8 (at most 255 bytes; "file" when |
| +// empty), and no negative size or side. Nil when none are left, so a |
| +// message without files is what it always was. |
| +func (m Message) Clean() Message { |
| + var out []Attachment |
| + for _, a := range m.Attachments { |
| + if !ValidAttachmentID(a.ID) { |
| + continue |
| + } |
| + if a.Kind != AttachmentImage { |
| + a.Kind = AttachmentFile |
| + } |
| + a.Name = strings.ToValidUTF8(a.Name, "") |
| + for len(a.Name) > 255 { |
| + _, n := utf8.DecodeLastRuneInString(a.Name) |
| + a.Name = a.Name[:len(a.Name)-n] |
| + } |
| + if strings.TrimSpace(a.Name) == "" { |
| + a.Name = "file" |
| + } |
| + a.Size, a.Width, a.Height = max(a.Size, 0), max(a.Width, 0), max(a.Height, 0) |
| + out = append(out, a) |
| + } |
| + m.Attachments = out |
| + return m |
| +} |
| + |
| // The kinds of attachment. |
| const ( |
| AttachmentImage = "image" |
diff --git a/internal/web/ask.go b/internal/web/ask.go| index d803638..b7ffb21 100644 |
| --- a/internal/web/ask.go |
| +++ b/internal/web/ask.go |
| @@ -225,6 +225,9 @@ type askView struct { |
| // Notice is a line the chat says once (how connecting Oficina ended), |
| // in the conversation after its newest line (NoticeAfter). |
| Notice string |
| + // CanAttach says the person's runner can take files: the paperclip |
| + // shows (with scripts) only then. |
| + CanAttach bool |
| // base is Base's own origin: the only place a connect-Oficina link may |
| // go and keep its rel. |
| base string |
| @@ -427,7 +430,11 @@ func turnStatus(t *equipo.Turn) string { |
| // other card to show, and the newest turn that has not ended, of those the |
| // person sees running (never a quiet team_update turn, plan decision D9). |
| func (v *askView) fill(s equipo.Snapshot) { |
| - v.Messages = s.Messages |
| + v.Messages = make([]equipo.Message, len(s.Messages)) |
| + for i, m := range s.Messages { |
| + v.Messages[i] = m.Clean() |
| + } |
| + v.CanAttach = s.CanAttach |
| var shown []equipo.Card |
| jobs := map[string]int{} |
| for _, c := range s.Cards { |
| @@ -598,8 +605,11 @@ func (a *app) askStateJSON(w http.ResponseWriter, r *http.Request) { |
| // Disconnected: the member disconnected Ask, so no app sets it up |
| // again by itself. |
| Disconnected bool `json:"disconnected"` |
| + // CanAttach: the person's runner can take files, so the paperclip |
| + // may show. |
| + CanAttach bool `json:"can_attach"` |
| }{v.State, append([]equipo.Message{}, v.Messages...), append([]jobView{}, v.Jobs...), append([]equipo.Card{}, v.Cards...), v.Turn, v.Status, cursor, |
| - renew, a.ask.client.Origin(), me.AskDisconnected} |
| + renew, a.ask.client.Origin(), me.AskDisconnected, v.CanAttach && v.State == askReady} |
| writeJSON(w, http.StatusOK, out) |
| } |
diff --git a/internal/web/ask_files.go b/internal/web/ask_files.go| index 48eb175..72b8fe3 100644 |
| --- a/internal/web/ask_files.go |
| +++ b/internal/web/ask_files.go |
| @@ -6,7 +6,6 @@ import ( |
| "io" |
| "mime" |
| "net/http" |
| - "regexp" |
| "strconv" |
| "strings" |
| "sync/atomic" |
| @@ -59,10 +58,6 @@ func newUploads(b UploadBounds) *uploads { |
| return &uploads{slots: make(chan struct{}, max(b.Slots, 1)), b: b} |
| } |
| -// attachmentID is the shape of an Equipo attachment id; anything else is |
| -// refused before Equipo is called. |
| -var attachmentID = regexp.MustCompile(`^at_[a-z0-9]{8,60}$`) |
| - |
| // The four image types Base shows inline (plan P3); the other five of |
| // Equipo's nine go as downloads, and anything else as an octet stream. |
| var ( |
| @@ -188,7 +183,7 @@ func retryAfter(s string) string { |
| // is 409 {"state":"ready","code":"attachment_sent"}. |
| func (a *app) askDeleteAttachment(w http.ResponseWriter, r *http.Request) { |
| id := chi.URLParam(r, "id") |
| - if !attachmentID.MatchString(id) { |
| + if !equipo.ValidAttachmentID(id) { |
| http.NotFound(w, r) |
| return |
| } |
| @@ -229,7 +224,7 @@ func (a *app) askDiscard(w http.ResponseWriter, r *http.Request) { |
| ctx := context.WithoutCancel(r.Context()) |
| ids := r.PostForm["id"] |
| for _, id := range ids[:min(len(ids), equipo.MaxFiles)] { |
| - if !attachmentID.MatchString(id) { |
| + if !equipo.ValidAttachmentID(id) { |
| continue |
| } |
| err := a.ask.client.DeleteAttachment(ctx, sec.Token, id) |
| @@ -250,7 +245,7 @@ func (a *app) askDiscard(w http.ResponseWriter, r *http.Request) { |
| func (a *app) askAttachment(preview bool) http.HandlerFunc { |
| return func(w http.ResponseWriter, r *http.Request) { |
| id := chi.URLParam(r, "id") |
| - if !attachmentID.MatchString(id) { |
| + if !equipo.ValidAttachmentID(id) { |
| http.NotFound(w, r) |
| return |
| } |
diff --git a/internal/web/ask_relay.go b/internal/web/ask_relay.go| index 5e44afb..e6aa83b 100644 |
| --- a/internal/web/ask_relay.go |
| +++ b/internal/web/ask_relay.go |
| @@ -122,7 +122,7 @@ func (a *app) askSend(w http.ResponseWriter, r *http.Request) { |
| valid := len(files) <= equipo.MaxFiles && (text != "" || len(files) > 0) && len(text) <= maxAskText && |
| utf8.ValidString(text) && !strings.ContainsRune(text, 0) |
| for _, id := range files { |
| - valid = valid && attachmentID.MatchString(id) |
| + valid = valid && equipo.ValidAttachmentID(id) |
| } |
| if !valid { |
| if wantsJSON(r) { |
| @@ -457,7 +457,10 @@ func project(typ string, raw json.RawMessage, base string) (any, bool) { |
| switch typ { |
| case "message": |
| var m equipo.Message |
| - return m, json.Unmarshal(raw, &m) == nil && m.ID != "" |
| + if json.Unmarshal(raw, &m) != nil || m.ID == "" { |
| + return nil, false |
| + } |
| + return m.Clean(), true |
| case "turn": |
| var t equipo.Turn |
| return t, json.Unmarshal(raw, &t) == nil && t.ID != "" |
diff --git a/internal/web/relay_internal_test.go b/internal/web/relay_internal_test.go| index c686a38..01674ff 100644 |
| --- a/internal/web/relay_internal_test.go |
| +++ b/internal/web/relay_internal_test.go |
| @@ -274,3 +274,27 @@ func TestRelayConnectCardKeepsItsTurn(t *testing.T) { |
| t.Errorf("the card's own turn_id passed:\n%s", out) |
| } |
| } |
| + |
| +// A message's files pass with the fields Base draws them from, and |
| +// nothing else of Equipo's: no URL, no hash, no id Base would not call |
| +// Equipo with; an unknown kind is a file. |
| +func TestRelayMessageFiles(t *testing.T) { |
| + files := `{"cursor":47,"type":"message","data":{"id":"m_3","revision":1,"role":"person","text":"","turn_id":"t_2","created_at":"2026-11-26T10:00:00Z",` + |
| + `"attachments":[{"id":"at_abcdefgh12","revision":2,"kind":"image","name":"shot.png","media_type":"image/png","size":2048,"sha256":"feedface","width":640,"height":480,"message_id":"m_3","created_at":"2026-11-26T10:00:00Z","url":"https://equipo.example/api/v1/attachments/at_abcdefgh12"},` + |
| + `{"id":"https://equipo.example/x","kind":"file","name":"bad.txt"},` + |
| + `{"id":"at_zzzzzzzz99","kind":"video","name":"notes.md","media_type":"text/markdown","size":12,"width":0,"height":0}]}}` |
| + out := relayTo(t, strings.NewReader(frame("47", "message", files)), time.Second, func() {}) |
| + for _, banned := range []string{"equipo.example", "feedface", "message_id", "revision", "video", "bad.txt"} { |
| + if strings.Contains(out, banned) { |
| + t.Errorf("relayed %q:\n%s", banned, out) |
| + } |
| + } |
| + want := `"attachments":[{"id":"at_abcdefgh12","kind":"image","name":"shot.png","media_type":"image/png","size":2048,"width":640,"height":480},` + |
| + `{"id":"at_zzzzzzzz99","kind":"file","name":"notes.md","media_type":"text/markdown","size":12,"width":0,"height":0}]` |
| + if !strings.Contains(out, want) { |
| + t.Errorf("relay lacks %s:\n%s", want, out) |
| + } |
| + if out := relayTo(t, strings.NewReader(frame("42", "message", msgEvent)), time.Second, func() {}); strings.Contains(out, "attachments") { |
| + t.Errorf("a message without files gained some:\n%s", out) |
| + } |
| +} |
diff --git a/internal/webtest/ask_files_test.go b/internal/webtest/ask_files_test.go| index 0cb07cf..24808ee 100644 |
| --- a/internal/webtest/ask_files_test.go |
| +++ b/internal/webtest/ask_files_test.go |
| @@ -80,13 +80,15 @@ func TestAskUploadRelays(t *testing.T) { |
| shot := pngFile(t, 30, 20) |
| r := a.upload("Screen Shot día.png", "image/png", shot) |
| id := uploaded(t, r) |
| + wantKeys(t, "upload", r.Body, "id", "kind", "name", "media_type", "size", "width", "height") |
| var at struct { |
| - Kind, Name, MediaType string `json:"media_type"` |
| - Width, Height int |
| - Size int64 |
| + Kind, Name string |
| + MediaType string `json:"media_type"` |
| + Width, Height int |
| + Size int64 |
| } |
| _ = json.Unmarshal([]byte(r.Body), &at) |
| - if r.Header.Get("Cache-Control") != "no-store" || at.Width != 30 || at.Height != 20 || at.Size != int64(len(shot)) { |
| + if r.Header.Get("Cache-Control") != "no-store" || at.Kind != "image" || at.MediaType != "image/png" || at.Width != 30 || at.Height != 20 || at.Size != int64(len(shot)) { |
| t.Fatalf("answer: %v %s", r.Header, r.Body) |
| } |
| data, name, media, _, ok := f.Upload(id) |
| @@ -645,3 +647,75 @@ func TestAskSendFilesRefused(t *testing.T) { |
| t.Fatalf("old runner, files: %d %s", r.Code, r.Body) |
| } |
| } |
| + |
| +// The conversation read afresh carries each message's files, and says |
| +// the paperclip may show only when Equipo says the runner can take them. |
| +func TestAskStateCarriesFiles(t *testing.T) { |
| + a, f := newAskApp(t) |
| + a.connectEquipo(f) |
| + var st struct { |
| + CanAttach bool `json:"can_attach"` |
| + Messages []struct { |
| + Attachments []map[string]any |
| + } |
| + } |
| + read := func() { |
| + t.Helper() |
| + st.Messages = nil |
| + if r := a.get("/ask/state"); r.Code != 200 || json.Unmarshal([]byte(r.Body), &st) != nil { |
| + t.Fatalf("state: %d %s", r.Code, r.Body) |
| + } |
| + } |
| + if read(); st.CanAttach { |
| + t.Fatal("can_attach before Equipo said so") |
| + } |
| + f.FilesReady(true) |
| + f.Hold = true |
| + if read(); !st.CanAttach { |
| + t.Fatal("no can_attach") |
| + } |
| + id := uploaded(t, a.upload("q3.csv", "text/csv", []byte("a,b\n"))) |
| + if r := a.postJSON("/ask/messages", url.Values{"attachments": {id}}); r.Code != 200 { |
| + t.Fatalf("send: %d %s", r.Code, r.Body) |
| + } |
| + read() |
| + got := st.Messages[len(st.Messages)-1].Attachments |
| + if len(got) != 1 || got[0]["id"] != id || got[0]["name"] != "q3.csv" || got[0]["kind"] != "file" || len(got[0]) != 7 { |
| + t.Fatalf("files: %+v", got) |
| + } |
| + f.NoRunner() |
| + if read(); st.CanAttach { |
| + t.Fatal("can_attach without a runner") |
| + } |
| +} |
| + |
| +// A message event carries its files. |
| +func TestAskEventsCarryFiles(t *testing.T) { |
| + a, f := newAskApp(t) |
| + a.connectEquipo(f) |
| + f.FilesReady(true) |
| + var st struct{ Cursor int64 } |
| + _ = json.Unmarshal([]byte(a.get("/ask/state").Body), &st) |
| + id := uploaded(t, a.upload("shot.png", "image/png", pngFile(t, 5, 4))) |
| + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| + defer cancel() |
| + req, _ := http.NewRequestWithContext(ctx, http.MethodGet, a.base+"/ask/events?after="+strconv64(st.Cursor), nil) |
| + res, err := a.client.Do(req) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + defer res.Body.Close() |
| + a.postJSON("/ask/messages", url.Values{"text": {"this"}, "attachments": {id}}) |
| + sc := bufio.NewScanner(res.Body) |
| + for sc.Scan() { |
| + line := sc.Text() |
| + if strings.HasPrefix(line, "data: ") && strings.Contains(line, `"role":"person"`) { |
| + if !strings.Contains(line, `"attachments":[{"id":"`+id+`","kind":"image","name":"shot.png","media_type":"image/png","size":`) || |
| + !strings.Contains(line, `"width":5,"height":4}]`) { |
| + t.Fatalf("message event: %s", line) |
| + } |
| + return |
| + } |
| + } |
| + t.Fatal("no message event") |
| +} |
diff --git a/internal/webtest/ask_native_contract_test.go b/internal/webtest/ask_native_contract_test.go| index 19a3fc3..2ad304f 100644 |
| --- a/internal/webtest/ask_native_contract_test.go |
| +++ b/internal/webtest/ask_native_contract_test.go |
| @@ -17,6 +17,7 @@ import ( |
| "time" |
| "amadan.net/oficina/base/internal/config" |
| + "amadan.net/oficina/base/internal/equipo" |
| "amadan.net/oficina/base/internal/equipo/equipotest" |
| ) |
| @@ -100,13 +101,13 @@ func (a *testApp) askState() (askState, Resp) { |
| return s, r |
| } |
| -var stateKeys = []string{"state", "messages", "jobs", "cards", "turn", "status", "cursor", "renew_after", "equipo_origin", "disconnected"} |
| +var stateKeys = []string{"state", "messages", "jobs", "cards", "turn", "status", "cursor", "renew_after", "equipo_origin", "disconnected", "can_attach"} |
| func TestAskContractBeforeConnecting(t *testing.T) { |
| _, app, f := newNativeAskApp(t) |
| s, r := app.askState() |
| wantKeys(t, "state", r.Body, stateKeys...) |
| - if strings.TrimSpace(r.Body) != `{"state":"connect","messages":[],"jobs":[],"cards":[],"turn":null,"status":"","cursor":0,"renew_after":"","equipo_origin":"`+f.URL+`","disconnected":false}` || s.State != "connect" { |
| + if strings.TrimSpace(r.Body) != `{"state":"connect","messages":[],"jobs":[],"cards":[],"turn":null,"status":"","cursor":0,"renew_after":"","equipo_origin":"`+f.URL+`","disconnected":false,"can_attach":false}` || s.State != "connect" { |
| t.Errorf("not connected: %s", r.Body) |
| } |
| wantBody(t, "send", app.nativeForm("/ask/messages", url.Values{"text": {"Hello"}, "key": {"k1"}}), 409, `{"state":"connect"}`) |
| @@ -537,3 +538,48 @@ func TestAskContractCarryOn(t *testing.T) { |
| _ = json.Unmarshal(s.Jobs[0], &j) |
| wantBody(t, "not paused", app.nativeForm("/ask/cards/"+job+"/carry_on", url.Values{"revision": {strconv.FormatInt(j.Revision, 10)}, "key": {"s-3"}}), 409, `{"state":"ready"}`) |
| } |
| + |
| +// Files (docs/api/base-native-v1.md, "Files"): a message with files carries them as |
| +// attachments; a message without has no such key; the state says |
| +// can_attach; Remove and a read are Base's own routes. |
| +func TestAskContractFiles(t *testing.T) { |
| + web, app, f := newNativeAskApp(t) |
| + web.approveEquipo(f) |
| + f.FilesReady(true) |
| + f.Hold = true |
| + // This harness serves Base in-process, where no read deadline can be |
| + // set, so the upload is made straight to Equipo with the member's |
| + // bearer; TestAskUploadRelays pins POST /ask/attachments's answer. |
| + tokens := f.Tokens() |
| + if len(tokens) != 1 { |
| + t.Fatalf("tokens: %v", tokens) |
| + } |
| + at, err := equipo.New(f.URL).Upload(context.Background(), tokens[0], "notes.md", "text/markdown", []byte("# Notes")) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + attachmentKeys := []string{"id", "kind", "name", "media_type", "size", "width", "height"} |
| + sent := app.nativeForm("/ask/messages", url.Values{"attachments": {at.ID}, "key": {"native-files"}}) |
| + if sent.Code != 200 { |
| + t.Fatalf("send: %d %s", sent.Code, sent.Body) |
| + } |
| + var out struct{ Message json.RawMessage } |
| + _ = json.Unmarshal([]byte(sent.Body), &out) |
| + wantKeys(t, "message with files", string(out.Message), "id", "role", "text", "turn_id", "created_at", "attachments") |
| + var m struct{ Attachments []json.RawMessage } |
| + _ = json.Unmarshal(out.Message, &m) |
| + if len(m.Attachments) != 1 { |
| + t.Fatalf("message: %s", out.Message) |
| + } |
| + wantKeys(t, "attachment", string(m.Attachments[0]), attachmentKeys...) |
| + s, r := app.askState() |
| + if !strings.Contains(r.Body, `"can_attach":true`) || len(s.Messages) == 0 { |
| + t.Fatalf("state: %s", r.Body) |
| + } |
| + if got := app.nativeGet("/ask/attachments/" + at.ID); got.Code != 200 || got.Body != "# Notes" { |
| + t.Fatalf("read: %d %s", got.Code, got.Body) |
| + } |
| + another := app.nativeForm("/ask/messages", url.Values{"text": {"no files"}, "key": {"native-words"}}) |
| + _ = json.Unmarshal([]byte(another.Body), &out) |
| + wantKeys(t, "message without files", string(out.Message), "id", "role", "text", "turn_id", "created_at") |
| +} |