oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package equipo
2
3import (
4 "context"
5 "errors"
6 "net/http"
7 "strings"
8 "time"
9
10 "github.com/go-chi/chi/v5"
11
12 "amadan.net/oficina/equipo/internal/equipo/chat"
13 "amadan.net/oficina/equipo/internal/wire"
14)
15
16// chatEnded is the note under the person's latest turn when it ended
17// unanswered: what happened, then the one thing to do about it. Retry
18// offers Try again (chatTurnRetry); SignIn is where to sign in again.
19type chatEnded struct {
20 ID, Line string
21 Retry bool
22 SignIn string
23}
24
25// failedAccount is the account a failed turn last ran on (from its usage
26// rows): its connection, label, state and when its limit resets.
27type failedAccount struct {
28 ID, Label, State string
29 ResetsAt *time.Time
30}
31
32// namesAccount are the failures whose line names the account.
33var namesAccount = map[string]bool{"provider_failed": true, "account_unavailable": true, "sign_in_again": true, "limited": true, "provider_rejected": true}
34
35// signInPage is where a connection is signed in again.
36const signInPage = "/conversation/settings/connections"
37
38// endedNote is the note for t, a person turn that failed or was stopped;
39// acct is the account it last ran on, nil when Equipo does not know it.
40func endedNote(t wire.Turn, acct *failedAccount, now time.Time) chatEnded {
41 n := chatEnded{ID: t.ID}
42 if t.State != wire.TurnFailed {
43 n.Line = copyText("chat." + t.State)
44 return n
45 }
46 // A batch (messages sent while another turn ran) says them all.
47 fallback := "chat.failed"
48 if len(t.MessageIDs) > 1 {
49 fallback = "chat.failed_many"
50 }
51 named := func(unknown string) string {
52 who := copyText(unknown)
53 if acct != nil {
54 who = acct.Label
55 }
56 return strings.ReplaceAll(copyText("chat.failed."+t.Reason), "{account}", who)
57 }
58 switch t.Reason {
59 case "turn_timeout", "policy_rejected":
60 n.Line, n.Retry = copyText("chat.failed."+t.Reason), true
61 case "runner_restarted", "interrupted":
62 n.Line, n.Retry = copyText("chat.failed.runner_restarted"), true
63 case "provider_failed", "provider_rejected":
64 n.Line, n.Retry = named("chat.failed.the_model"), true
65 case "account_unavailable":
66 n.Line, n.Retry = named("chat.failed.an_account"), true
67 case "sign_in_again":
68 // Another try fails the same way until the account is signed in.
69 n.Line, n.SignIn = copyText(fallback), signInPage
70 if acct != nil {
71 n.Line, n.SignIn = named(""), signInPage+"#connection-"+acct.ID
72 }
73 case "limited":
74 // Try again only once the limit has reset, as far as Equipo knows.
75 n.Line = copyText(fallback)
76 if acct != nil {
77 n.Line = named("")
78 n.Retry = acct.State == wire.ConnHealthy || acct.ResetsAt != nil && !now.Before(*acct.ResetsAt)
79 }
80 case wire.ReasonAttachmentUnavailable, "runner_too_old":
81 n.Line = copyText("chat.failed." + t.Reason)
82 case wire.ReasonStorageFull:
83 n.Line = copyText("chat.failed_storage")
84 default:
85 n.Line, n.Retry = copyText(fallback), true
86 }
87 return n
88}
89
90// endedFor is the note for the person's latest turn, with the account it
91// last ran on when its line names one.
92func (a *application) endedFor(ctx context.Context, address string, t wire.Turn) (*chatEnded, error) {
93 var acct *failedAccount
94 if t.State == wire.TurnFailed && namesAccount[t.Reason] {
95 conn, label, ok, err := a.store.Chat().TurnAccount(ctx, address, t.ID)
96 if err != nil {
97 return nil, err
98 }
99 if ok {
100 acct = &failedAccount{ID: conn.ID, Label: label.Named, State: conn.State}
101 if conn.Headroom != nil {
102 acct.ResetsAt = conn.Headroom.ResetsAt
103 }
104 }
105 }
106 n := endedNote(t, acct, a.store.Chat().Now())
107 return &n, nil
108}
109
110// chatTurnRetry is Try again under a failed reply: the retry a device
111// makes. Tried again already, or overtaken by a newer turn, the page
112// shows how things stand; a script reads 409.
113func (a *application) chatTurnRetry(w http.ResponseWriter, r *http.Request) {
114 w.Header().Set("Cache-Control", "no-store")
115 ctx, address, ok := sessionContext(r)
116 if !ok {
117 a.failure(w, r, ErrForbidden)
118 return
119 }
120 _, err := a.store.Chat().Retry(ctx, address, chi.URLParam(r, "id"))
121 script := strings.Contains(r.Header.Get("Accept"), "application/json")
122 switch {
123 case err != nil && !errors.Is(err, chat.ErrNotRetryable):
124 a.failure(w, r, chatError(err))
125 case err != nil && script:
126 runnerJSON(w, http.StatusConflict, struct{}{})
127 case script:
128 runnerJSON(w, http.StatusOK, struct{}{})
129 default:
130 http.Redirect(w, r, "/conversation", http.StatusSeeOther)
131 }
132}
133