| 1 | package assist |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "strings" |
| 7 | "unicode/utf8" |
| 8 | ) |
| 9 | |
| 10 | // MaxSummary is the most of the model's summary a person is shown, in |
| 11 | // runes. A summary is a sentence or two; anything longer is cut at a |
| 12 | // word, not refused, since the operations are what matter. |
| 13 | const MaxSummary = 400 |
| 14 | |
| 15 | // Reply is what the model proposed: operations, unvalidated, and a |
| 16 | // sentence saying what they do. Every op stays raw JSON so the caller |
| 17 | // rebuilds it key by key rather than trusting its shape. |
| 18 | type Reply struct { |
| 19 | Summary string |
| 20 | Ops []map[string]json.RawMessage |
| 21 | } |
| 22 | |
| 23 | // ParseReply reads the model's content as one JSON object |
| 24 | // {"summary": "...", "ops": [...]}. |
| 25 | // |
| 26 | // Tolerant of the wrapping models put around JSON even when asked not |
| 27 | // to (a ```json fence, a sentence before it): the object is taken from |
| 28 | // the first '{' to the last '}'. Strict about the object itself: it |
| 29 | // must decode, "ops" must be an array of objects, and nothing may |
| 30 | // follow it. Unknown top-level keys are ignored — they carry nothing |
| 31 | // the caller would act on. Every failure is ErrMalformed and quotes |
| 32 | // nothing. |
| 33 | func ParseReply(content string) (Reply, error) { |
| 34 | start := strings.IndexByte(content, '{') |
| 35 | end := strings.LastIndexByte(content, '}') |
| 36 | if start < 0 || end < start { |
| 37 | return Reply{}, ErrMalformed |
| 38 | } |
| 39 | dec := json.NewDecoder(bytes.NewReader([]byte(content[start : end+1]))) |
| 40 | var out struct { |
| 41 | Summary json.RawMessage `json:"summary"` |
| 42 | Ops []map[string]json.RawMessage `json:"ops"` |
| 43 | } |
| 44 | if err := dec.Decode(&out); err != nil { |
| 45 | return Reply{}, ErrMalformed |
| 46 | } |
| 47 | if _, err := dec.Token(); err == nil { |
| 48 | // Two objects pretending to be one. |
| 49 | return Reply{}, ErrMalformed |
| 50 | } |
| 51 | if out.Ops == nil { |
| 52 | return Reply{}, ErrMalformed |
| 53 | } |
| 54 | for _, op := range out.Ops { |
| 55 | if op == nil { |
| 56 | return Reply{}, ErrMalformed |
| 57 | } |
| 58 | } |
| 59 | var summary string |
| 60 | if len(out.Summary) > 0 && string(out.Summary) != "null" { |
| 61 | if err := json.Unmarshal(out.Summary, &summary); err != nil { |
| 62 | return Reply{}, ErrMalformed |
| 63 | } |
| 64 | } |
| 65 | return Reply{Summary: clip(strings.TrimSpace(summary), MaxSummary), Ops: out.Ops}, nil |
| 66 | } |
| 67 | |
| 68 | // clip shortens s to at most n runes, at a space where there is one |
| 69 | // near the end, with an ellipsis. |
| 70 | func clip(s string, n int) string { |
| 71 | if utf8.RuneCountInString(s) <= n { |
| 72 | return s |
| 73 | } |
| 74 | r := []rune(s)[:n] |
| 75 | cut := string(r) |
| 76 | if i := strings.LastIndexByte(cut, ' '); i > len(cut)*3/4 { |
| 77 | cut = cut[:i] |
| 78 | } |
| 79 | return strings.TrimRight(cut, " ,.;:") + "…" |
| 80 | } |
| 81 | |