run_config.go
Overview
The run_config.go file defines configuration structures and utilities to manage runtime behavior for streaming modes within the system. It primarily encapsulates how streaming communication is configured and propagated through context.Context objects. This enables downstream components to modify behavior based on the specified streaming mode, supporting scenarios like no streaming, server-sent events (SSE), or bidirectional streaming.
Types and Constants
StreamingMode (type)
type StreamingMode string
StreamingMode is a custom string type that enumerates the possible streaming modes supported by the system.
Constants
StreamingModeNone: Indicates no streaming; the system operates in a standard request-response mode.StreamingModeSSE: Indicates streaming using Server-Sent Events, allowing unidirectional streaming from server to client.StreamingModeBidi: Indicates bidirectional streaming, supporting full duplex communication.
These constants define the operational mode that components can check to adapt their communication or processing logic accordingly.
RunConfig (struct)
type RunConfig struct {
StreamingMode StreamingMode
}
RunConfig holds configuration parameters that control runtime execution aspects. Currently, it contains only one field:
StreamingMode: Specifies the streaming mode to use during the run.
This struct can be extended in the future with additional runtime options.
Functions
ToContext
func ToContext(ctx context.Context, cfg *RunConfig) context.Context
Purpose: Embeds a
RunConfiginstance into acontext.Contextto propagate runtime configuration downstream.Parameters:
ctx(context.Context): The parent context into which the configuration will be injected.cfg(*RunConfig): A pointer to theRunConfiginstance to embed.
Returns: A new
context.Contextcontaining theRunConfig.Usage Example:
cfg := &runconfig.RunConfig{StreamingMode: runconfig.StreamingModeSSE}
ctx := context.Background()
ctxWithConfig := runconfig.ToContext(ctx, cfg)
// ctxWithConfig can now be passed to downstream functions
This function uses an unexported context key to safely store the RunConfig without risk of collision with other context values.
FromContext
func FromContext(ctx context.Context) *RunConfig
Purpose: Extracts the
RunConfiginstance stored in acontext.Context.Parameters:
ctx(context.Context): The context from which to retrieve the configuration.
Returns: A pointer to the
RunConfigif present; otherwise,nil.Usage Example:
cfg := runconfig.FromContext(ctx)
if cfg != nil {
// Use cfg.StreamingMode or other fields
}
This function performs a type assertion on the stored value to ensure type safety.
Implementation Details
The file uses a private type
ctxKeywith a constantrunConfigCtxKeyto create a unique context key. This pattern avoids collisions in context keys across packages.RunConfigis designed to be simple and focused on streaming mode configuration but is extensible.The choice of string-based
StreamingModeconstants facilitates easy comparison and debugging.The usage of context for passing runtime configuration is idiomatic in Go and supports cancellation, deadlines, and value propagation cleanly.
Interaction with Other System Components
This file enables components that perform streaming or communication operations to adapt their behavior based on the streaming mode set in the
RunConfig.By embedding the configuration in a context, any component receiving the context can access the runtime settings without direct coupling.
This design supports integration with agents, API handlers, or transport layers that need to handle different streaming protocols or modes dynamically.
In relation to topics like Agent Invocation Context or Agent Execution Runner, the runtime configuration can influence how data is streamed to or from agents during execution.
Diagram: Structure of run_config.go
classDiagram
class RunConfig {
+StreamingMode: StreamingMode
}
class StreamingMode {
<<enumeration>>
+StreamingModeNone
+StreamingModeSSE
+StreamingModeBidi
}
class ContextFunctions {
+ToContext(ctx, cfg) Context
+FromContext(ctx) *RunConfig
}
RunConfig --> StreamingMode
ContextFunctions ..> RunConfig : uses