| 1 | package agent |
| 2 | |
| 3 | import ( |
| 4 | "reasonix/internal/event" |
| 5 | "reasonix/internal/i18n" |
| 6 | "reasonix/internal/provider" |
| 7 | ) |
| 8 | |
| 9 | type searchTurn struct { |
| 10 | calls []provider.ServerSearchCall |
| 11 | dispatched map[string]bool |
| 12 | } |
| 13 | |
| 14 | func newSearchTurn() *searchTurn { |
| 15 | return &searchTurn{dispatched: map[string]bool{}} |
| 16 | } |
| 17 | |
| 18 | func (s *searchTurn) onChunk(sink event.Sink, chunk provider.Chunk, attemptID string) { |
| 19 | if chunk.ServerSearch == nil { |
| 20 | return |
| 21 | } |
| 22 | s.calls = provider.MergeServerSearch(s.calls, *chunk.ServerSearch) |
| 23 | emitServerSearch(sink, chunk.ServerSearch, s.dispatched, attemptID) |
| 24 | } |
| 25 | |
| 26 | func emitServerSearch(sink event.Sink, call *provider.ServerSearchCall, dispatched map[string]bool, attemptID string) { |
| 27 | if call == nil { |
| 28 | return |
| 29 | } |
| 30 | display := *call |
| 31 | display.SourcesStatus = provider.ServerSearchSourcesStatus(display) |
| 32 | args := provider.FormatServerSearchArgs(call.Query) |
| 33 | completed := len(call.Results) > 0 || len(call.Raw) > 0 |
| 34 | if !dispatched[call.ID] || !completed { |
| 35 | sink.Emit(event.Event{Kind: event.ToolDispatch, Tool: event.Tool{ |
| 36 | ID: call.ID, Name: "web_search", Args: args, ReadOnly: true, AttemptID: attemptID, |
| 37 | }}) |
| 38 | dispatched[call.ID] = true |
| 39 | } |
| 40 | if !completed { |
| 41 | return |
| 42 | } |
| 43 | if provider.ServerSearchSourcesStatus(*call) == provider.SourcesNotProvided && !dispatched[call.ID+"\x00sources"] { |
| 44 | dispatched[call.ID+"\x00sources"] = true |
| 45 | sink.Emit(event.Event{Kind: event.Notice, Level: event.LevelInfo, Code: "search_sources_not_provided", Text: i18n.M.SearchSourcesNotProvided}) |
| 46 | } |
| 47 | sink.Emit(event.Event{Kind: event.ToolResult, Tool: event.Tool{ |
| 48 | ID: call.ID, Name: "web_search", Args: args, Output: provider.ServerSearchDisplayOutput(display), ReadOnly: true, AttemptID: attemptID, |
| 49 | }}) |
| 50 | } |
| 51 |