| 1 | package control |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "slices" |
| 8 | |
| 9 | "reasonix/internal/agent" |
| 10 | "reasonix/internal/attachment" |
| 11 | "reasonix/internal/i18n" |
| 12 | "reasonix/internal/provider" |
| 13 | ) |
| 14 | |
| 15 | // imageReadError distinguishes local attachment loss from upload/auth failures. |
| 16 | type imageReadError struct{ error } |
| 17 | |
| 18 | func (e imageReadError) Unwrap() error { return e.error } |
| 19 | |
| 20 | func lastImageRequestTurn(messages []provider.Message) int { |
| 21 | for i, m := range slices.Backward(messages) { |
| 22 | if m.Role == provider.RoleUser && !m.LocalOnly && !agent.IsHostGeneratedUserMessage(m) { |
| 23 | return i |
| 24 | } |
| 25 | } |
| 26 | return 0 // no known user boundary: keep all inputs required |
| 27 | } |
| 28 | |
| 29 | // Resolve independently so a missing old attachment does not discard its |
| 30 | // healthy siblings. The durable references and UI transcript remain intact. |
| 31 | func (c *Controller) resolveReplayImages(ctx context.Context, inputs []attachment.ImageInput, route ImageRequestRoute, historical bool) ([]string, []int, error) { |
| 32 | var images []string |
| 33 | var missing []int |
| 34 | for i, input := range inputs { |
| 35 | if err := ctx.Err(); err != nil { |
| 36 | return nil, nil, err |
| 37 | } |
| 38 | resolved, err := c.resolveImageInputsForRoute(ctx, []attachment.ImageInput{input}, route) |
| 39 | if err == nil { |
| 40 | images = append(images, resolved...) |
| 41 | continue |
| 42 | } |
| 43 | if ctx.Err() != nil || errors.Is(err, context.Canceled) { |
| 44 | return nil, nil, err |
| 45 | } |
| 46 | if historical { |
| 47 | missing = append(missing, i+1) |
| 48 | c.emitImageReplayWarning(ctx, err) |
| 49 | continue |
| 50 | } |
| 51 | var item attachment.Error |
| 52 | if errors.As(err, &item) { |
| 53 | item.Index = i + 1 |
| 54 | return nil, nil, item |
| 55 | } |
| 56 | return nil, nil, err |
| 57 | } |
| 58 | return images, missing, nil |
| 59 | } |
| 60 | |
| 61 | func noteUnavailableImages(m *provider.Message, positions []int) { |
| 62 | if len(positions) == 0 { |
| 63 | return |
| 64 | } |
| 65 | m.Content += fmt.Sprintf("\n[Historical image inputs %v are unavailable. Their contents have not been supplied in this request. Continue with available text and images; if those missing images are needed, ask the user to attach them again. Do not infer their contents.]", positions) |
| 66 | } |
| 67 | |
| 68 | func noteUnreadableImages(m *provider.Message, count int) { |
| 69 | positions := make([]int, count) |
| 70 | for i := range positions { |
| 71 | positions[i] = i + 1 |
| 72 | } |
| 73 | m.Content += fmt.Sprintf("\n[Image inputs %v were not supplied: the selected model cannot read images and no image understanding model is configured. Do not infer their contents; tell the user to pick a model that reads images or set an image understanding model.]", positions) |
| 74 | } |
| 75 | |
| 76 | func imageRequestFailure(ctx context.Context, err error) error { |
| 77 | if ctx.Err() != nil { |
| 78 | return ctx.Err() |
| 79 | } |
| 80 | if errors.Is(err, context.Canceled) { |
| 81 | return err |
| 82 | } |
| 83 | return fmt.Errorf("%s: %w", i18n.M.ImageRequestRecovery, &provider.ImageInputError{Err: err}) |
| 84 | } |
| 85 |