| 1 | package tool |
| 2 | |
| 3 | import "context" |
| 4 | |
| 5 | // ProgressFunc receives a chunk of a tool's combined output as it is produced, so |
| 6 | // a long-running tool (bash) can stream progress to a frontend before it returns. |
| 7 | type ProgressFunc func(chunk string) |
| 8 | |
| 9 | type progressKey struct{} |
| 10 | |
| 11 | // WithProgress stamps ctx with a progress sink the executing tool may call; the |
| 12 | // agent sets it per call so the chunk reaches the right tool card. |
| 13 | func WithProgress(ctx context.Context, fn ProgressFunc) context.Context { |
| 14 | return context.WithValue(ctx, progressKey{}, fn) |
| 15 | } |
| 16 | |
| 17 | // ProgressFrom returns the progress sink, if one was stamped (ok is false for a |
| 18 | // plain context — headless tests or calls outside the run loop). |
| 19 | func ProgressFrom(ctx context.Context) (ProgressFunc, bool) { |
| 20 | fn, ok := ctx.Value(progressKey{}).(ProgressFunc) |
| 21 | return fn, ok && fn != nil |
| 22 | } |
| 23 |