ApplyStreamInjection wraps data and error channels for streaming injection. If injection is nil, returns channels unchanged. For "replace" timing: drains upstream channels, returns fabricated stream. For "append" timing: wraps dataChan with format-specific stream injector.
( dataChan <-chan []byte, errChan <-chan *interfaces.ErrorMessage, injection *config.ToolCallInjectionRule, format, model string, )
| 55 | // For "replace" timing: drains upstream channels, returns fabricated stream. |
| 56 | // For "append" timing: wraps dataChan with format-specific stream injector. |
| 57 | func ApplyStreamInjection( |
| 58 | dataChan <-chan []byte, |
| 59 | errChan <-chan *interfaces.ErrorMessage, |
| 60 | injection *config.ToolCallInjectionRule, |
| 61 | format, model string, |
| 62 | ) (<-chan []byte, <-chan *interfaces.ErrorMessage) { |
| 63 | if injection == nil { |
| 64 | return dataChan, errChan |
| 65 | } |
| 66 | f := toolinjection.GetFormat(format) |
| 67 | if f == nil { |
| 68 | return dataChan, errChan |
| 69 | } |
| 70 | |
| 71 | if injection.Timing == "replace" { |
| 72 | // Drain upstream channels in background. |
| 73 | go func() { |
| 74 | for range dataChan { |
| 75 | } |
| 76 | }() |
| 77 | if errChan != nil { |
| 78 | go func() { |
| 79 | for range errChan { |
| 80 | } |
| 81 | }() |
| 82 | } |
| 83 | |
| 84 | // Fabricate a complete stream. |
| 85 | chunks := f.FabricateStream(injection, model) |
| 86 | fakeChan := make(chan []byte, len(chunks)) |
| 87 | for _, chunk := range chunks { |
| 88 | fakeChan <- chunk |
| 89 | } |
| 90 | close(fakeChan) |
| 91 | return fakeChan, nil |
| 92 | } |
| 93 | |
| 94 | // Append mode: wrap the real stream. |
| 95 | return f.InjectStream(dataChan, injection, model), errChan |
| 96 | } |
no test coverage detected