| 1 | package control |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | |
| 7 | "reasonix/internal/agent" |
| 8 | "reasonix/internal/event" |
| 9 | "reasonix/internal/i18n" |
| 10 | "reasonix/internal/imageinput" |
| 11 | "reasonix/internal/provider" |
| 12 | "reasonix/internal/secrets" |
| 13 | ) |
| 14 | |
| 15 | func (c *Controller) understandReplayImages(ctx context.Context, m *provider.Message, model string, historical bool) error { |
| 16 | if m.VisionSummary != nil { |
| 17 | return nil |
| 18 | } |
| 19 | svc := imageinput.New(imageinput.Config{Model: c.visionModel, Resolve: c.visionProviderResolver, Select: c.visionModelSelector}) |
| 20 | if c.executor != nil && c.executor.ImageInput() != nil { |
| 21 | svc = c.executor.ImageInput() |
| 22 | } |
| 23 | target, err := svc.SelectModel(model, nil) |
| 24 | if errors.Is(err, imageinput.ErrNoModel) && (m.Role != provider.RoleUser || historical) { |
| 25 | noteUnreadableImages(m, len(m.ImageInputs)) |
| 26 | return nil |
| 27 | } |
| 28 | if err != nil { |
| 29 | return err |
| 30 | } |
| 31 | route, err := c.imageRequestRoute(target) |
| 32 | if err != nil { |
| 33 | return err |
| 34 | } |
| 35 | images, missing, err := c.resolveReplayImages(ctx, m.ImageInputs, route, historical || m.Role == provider.RoleTool) |
| 36 | if err != nil { |
| 37 | return err |
| 38 | } |
| 39 | if len(images) > 0 { |
| 40 | summary, err := svc.UnderstandSelected(ctx, target, images, nil, c.sink) |
| 41 | if err != nil { |
| 42 | return err |
| 43 | } |
| 44 | m.Content = imageinput.AppendSummary(m.Content, summary) |
| 45 | } |
| 46 | noteUnavailableImages(m, missing) |
| 47 | return nil |
| 48 | } |
| 49 | |
| 50 | // Only the current user attachments are required. Older inputs and images |
| 51 | // produced by an already-executed tool must not poison every later text turn. |
| 52 | // A child's image timeout may degrade; cancellation of the owning turn may not. |
| 53 | func (c *Controller) omitReplayImageFailure(ctx context.Context, m *provider.Message, err error, historical bool) bool { |
| 54 | if (!historical && m.Role != provider.RoleTool) || ctx.Err() != nil || errors.Is(err, context.Canceled) { |
| 55 | return false |
| 56 | } |
| 57 | positions := make([]int, len(m.ImageInputs)) |
| 58 | for i := range positions { |
| 59 | positions[i] = i + 1 |
| 60 | } |
| 61 | if m.Role == provider.RoleTool { |
| 62 | m.Content += "\n[Tool image inputs were not supplied because image processing failed. The tool already executed; its text result remains valid. Do not infer image contents or repeat the original action to retry image processing.]" |
| 63 | } else { |
| 64 | noteUnavailableImages(m, positions) |
| 65 | } |
| 66 | m.ImageInputs, m.Images = nil, nil |
| 67 | c.emitImageReplayWarning(ctx, err) |
| 68 | return true |
| 69 | } |
| 70 | |
| 71 | func (c *Controller) emitImageReplayWarning(ctx context.Context, err error) { |
| 72 | if c.sink == nil { |
| 73 | return |
| 74 | } |
| 75 | detail := secrets.FailureDetail(&provider.ImageInputError{Err: err}) |
| 76 | if warnings, ok := ctx.Value(imageReplayWarningsKey{}).(*imageReplayWarnings); ok && !warnings.first(detail) { |
| 77 | return |
| 78 | } |
| 79 | c.sink.Emit(event.Event{Kind: event.Notice, Level: event.LevelWarn, Code: event.NoticeCodeImageReplayUnavailable, |
| 80 | MessageID: agent.NewMessageID(), Text: i18n.M.ImageReplayUnavailable, |
| 81 | Detail: detail}) |
| 82 | } |
| 83 |