middleware

package
v0.0.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 20, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const ExtractedThinkingContextKey = "extractedThinking"
View Source
const PromptTokensContextKey = "promptTokens"
View Source
const ResponseTokensContextKey = "responseTokens"
View Source
const ThinkingModeContextKey = "thinkingMode"
View Source
const TotalTokensContextKey = "totalTokens"

Variables

This section is empty.

Functions

func CombineFragments

func CombineFragments(fragments []PromptFragment) string

CombineFragments joins the content of sorted PromptFragments into a single string.

func MockLLM

func MockLLM(ctx Context, prompt string, userQuery string) string

MockLLM simulates an LLM call based on context.

Types

type Context

type Context = *orderedmap.OrderedMap[string, interface{}]

Context is a flexible, extensible state object that flows through the middleware pipeline, preserving insertion order of keys.

func CloneContext

func CloneContext(ctx Context) Context

CloneContext creates a deep copy of the context map itself, but a shallow copy of the values.

type Middleware

type Middleware interface {
	// Prompt transforms context and prompt fragments before sending to an LLM.
	// It returns the potentially modified context and the new list of fragments.
	Prompt(ctx Context, fragments []PromptFragment) (Context, []PromptFragment)

	// Parse processes the LLM response and updates the context.
	// It returns the potentially modified context and the processed response string.
	Parse(ctx Context, response string) (Context, string)

	// ID returns a unique identifier for the middleware.
	ID() string
	// Name returns a human-readable name for the middleware.
	Name() string
	// Description returns a short description of the middleware's purpose.
	Description() string
}

Middleware defines the interface for components in the processing pipeline.

type MiddlewarePipeline

type MiddlewarePipeline struct {
	// contains filtered or unexported fields
}

MiddlewarePipeline manages the execution of a sequence of middlewares.

func NewMiddlewarePipeline

func NewMiddlewarePipeline() *MiddlewarePipeline

NewMiddlewarePipeline creates a new, empty middleware pipeline.

func (*MiddlewarePipeline) ExecuteParsePhase

func (p *MiddlewarePipeline) ExecuteParsePhase(
	context Context,
	llmResponse string,
) (Context, string)

ExecuteParsePhase runs the response parsing phase of the pipeline.

func (*MiddlewarePipeline) ExecutePromptPhase

func (p *MiddlewarePipeline) ExecutePromptPhase(
	initialContext Context,
	initialFragments []PromptFragment,
) (Context, []PromptFragment, string)

ExecutePromptPhase runs the prompt transformation phase of the pipeline.

func (*MiddlewarePipeline) Middlewares

func (p *MiddlewarePipeline) Middlewares() []Middleware

Middlewares returns the list of middlewares currently in the pipeline.

func (*MiddlewarePipeline) Use

func (p *MiddlewarePipeline) Use(middleware Middleware) *MiddlewarePipeline

Use adds a middleware to the pipeline.

type PromptFragment

type PromptFragment struct {
	Content  string                 `json:"content"`
	Metadata PromptFragmentMetadata `json:"metadata"`
}

PromptFragment represents a structured piece of the prompt with associated metadata.

func SortFragments

func SortFragments(fragments []PromptFragment) []PromptFragment

SortFragments sorts prompt fragments based on position ('start', 'middle', 'end') and then by priority (descending).

type PromptFragmentMetadata

type PromptFragmentMetadata struct {
	ID       string   `json:"id,omitempty"`
	Type     string   `json:"type,omitempty"`
	Position string   `json:"position,omitempty"` // "start", "middle", "end"
	Priority int      `json:"priority,omitempty"`
	Tags     []string `json:"tags,omitempty"`
}

PromptFragmentMetadata holds metadata associated with a PromptFragment.

type SystemInstructionMiddleware

type SystemInstructionMiddleware struct {
	Instructions string
}

func NewSystemInstructionMiddleware

func NewSystemInstructionMiddleware(instructions string) *SystemInstructionMiddleware

NewSystemInstructionMiddleware creates a middleware that adds a system instruction.

func (*SystemInstructionMiddleware) Description

func (m *SystemInstructionMiddleware) Description() string

func (*SystemInstructionMiddleware) ID

func (*SystemInstructionMiddleware) Name

func (*SystemInstructionMiddleware) Parse

func (m *SystemInstructionMiddleware) Parse(ctx Context, response string) (Context, string)

func (*SystemInstructionMiddleware) Prompt

func (m *SystemInstructionMiddleware) Prompt(ctx Context, fragments []PromptFragment) (Context, []PromptFragment)

type ThinkingModeMiddleware

type ThinkingModeMiddleware struct{}

func NewThinkingModeMiddleware

func NewThinkingModeMiddleware() *ThinkingModeMiddleware

NewThinkingModeMiddleware creates a middleware that handles <thinking> tags.

func (*ThinkingModeMiddleware) Description

func (m *ThinkingModeMiddleware) Description() string

func (*ThinkingModeMiddleware) ID

func (*ThinkingModeMiddleware) Name

func (m *ThinkingModeMiddleware) Name() string

func (*ThinkingModeMiddleware) Parse

func (m *ThinkingModeMiddleware) Parse(ctx Context, response string) (Context, string)

func (*ThinkingModeMiddleware) Prompt

func (m *ThinkingModeMiddleware) Prompt(ctx Context, fragments []PromptFragment) (Context, []PromptFragment)

type TokenCounterMiddleware

type TokenCounterMiddleware struct{}

func NewTokenCounterMiddleware

func NewTokenCounterMiddleware() *TokenCounterMiddleware

NewTokenCounterMiddleware creates a middleware that estimates token counts.

func (*TokenCounterMiddleware) Description

func (m *TokenCounterMiddleware) Description() string

func (*TokenCounterMiddleware) ID

func (*TokenCounterMiddleware) Name

func (m *TokenCounterMiddleware) Name() string

func (*TokenCounterMiddleware) Parse

func (m *TokenCounterMiddleware) Parse(ctx Context, response string) (Context, string)

func (*TokenCounterMiddleware) Prompt

func (m *TokenCounterMiddleware) Prompt(ctx Context, fragments []PromptFragment) (Context, []PromptFragment)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL