Documentation
¶
Overview ¶
Package tool defines callback inputs and outputs for tool execution.
Index ¶
- func CompositeInterrupt(ctx context.Context, info any, state any, errs ...error) error
- func GetImplSpecificOptions[T any](base *T, opts ...Option) *T
- func GetInterruptState[T any](ctx context.Context) (wasInterrupted bool, hasState bool, state T)
- func GetResumeContext[T any](ctx context.Context) (isResumeTarget bool, hasData bool, data T)
- func Interrupt(ctx context.Context, info any) error
- func StatefulInterrupt(ctx context.Context, info any, state any) error
- type BaseTool
- type CallbackInput
- type CallbackOutput
- type InvokableTool
- type Option
- type StreamableTool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CompositeInterrupt ¶ added in v0.7.22
CompositeInterrupt creates an interrupt that aggregates multiple sub-interrupts. Use this when a tool internally executes a graph or other interruptible components.
Parameters:
- ctx: The context passed to InvokableRun/StreamableRun
- info: User-facing information for this tool's interrupt
- state: Internal state to persist for this tool
- errs: Interrupt errors from sub-components (graphs, other tools, etc.)
Example:
func (t *MyTool) InvokableRun(ctx context.Context, args string, opts ...Option) (string, error) {
result, err := t.internalGraph.Invoke(ctx, input)
if err != nil {
if _, ok := tool.IsInterruptError(err); ok {
return "", tool.CompositeInterrupt(ctx, "graph interrupted", myState, err)
}
return "", err
}
return result, nil
}
func GetImplSpecificOptions ¶
GetImplSpecificOptions provides tool author the ability to extract their own custom options from the unified Option type. T: the type of the impl specific options struct. This function should be used within the tool implementation's InvokableRun or StreamableRun functions. It is recommended to provide a base T as the first argument, within which the tool author can provide default values for the impl specific options. eg.
type customOptions struct {
conf string
}
defaultOptions := &customOptions{}
customOptions := tool.GetImplSpecificOptions(defaultOptions, opts...)
func GetInterruptState ¶ added in v0.7.22
GetInterruptState checks if the tool was previously interrupted and retrieves saved state.
Returns:
- wasInterrupted: true if this tool was part of a previous interruption
- hasState: true if state was saved and successfully cast to type T
- state: the saved state (zero value if hasState is false)
Example:
func (t *MyTool) InvokableRun(ctx context.Context, args string, opts ...Option) (string, error) {
wasInterrupted, hasState, state := tool.GetInterruptState[MyState](ctx)
if wasInterrupted && hasState {
// Continue from saved state
return continueFrom(state), nil
}
// First run
return "", tool.StatefulInterrupt(ctx, "need input", MyState{Step: 1})
}
func GetResumeContext ¶ added in v0.7.22
GetResumeContext checks if this tool is the explicit target of a resume operation.
Returns:
- isResumeTarget: true if this tool was explicitly targeted for resume
- hasData: true if resume data was provided
- data: the resume data (zero value if hasData is false)
Use this to differentiate between:
- Being resumed as the target (should proceed with work)
- Being re-executed because a sibling was resumed (should re-interrupt)
Example:
func (t *MyTool) InvokableRun(ctx context.Context, args string, opts ...Option) (string, error) {
wasInterrupted, _, _ := tool.GetInterruptState[any](ctx)
if !wasInterrupted {
return "", tool.Interrupt(ctx, "need confirmation")
}
isTarget, hasData, data := tool.GetResumeContext[string](ctx)
if !isTarget {
// Not our turn - re-interrupt
return "", tool.Interrupt(ctx, nil)
}
if hasData {
return data, nil
}
return "default result", nil
}
func Interrupt ¶ added in v0.7.22
Interrupt pauses tool execution and signals the orchestration layer to checkpoint. The tool can be resumed later with optional data.
Parameters:
- ctx: The context passed to InvokableRun/StreamableRun
- info: User-facing information about why the tool is interrupting (e.g., "needs user confirmation")
Returns an error that should be returned from InvokableRun/StreamableRun.
Example:
func (t *MyTool) InvokableRun(ctx context.Context, args string, opts ...Option) (string, error) {
if needsConfirmation(args) {
return "", tool.Interrupt(ctx, "Please confirm this action")
}
return doWork(args), nil
}
func StatefulInterrupt ¶ added in v0.7.22
StatefulInterrupt pauses tool execution with state preservation. Use this when the tool has internal state that must be restored on resume.
Parameters:
- ctx: The context passed to InvokableRun/StreamableRun
- info: User-facing information about the interrupt
- state: Internal state to persist (must be gob-serializable)
Example:
func (t *MyTool) InvokableRun(ctx context.Context, args string, opts ...Option) (string, error) {
wasInterrupted, hasState, state := tool.GetInterruptState[MyState](ctx)
if !wasInterrupted {
// First run - interrupt with state
return "", tool.StatefulInterrupt(ctx, "processing", MyState{Step: 1})
}
// Resumed - continue from saved state
return continueFrom(state), nil
}
Types ¶
type CallbackInput ¶
type CallbackInput struct {
// ArgumentsInJSON is the arguments in json format for the tool.
ArgumentsInJSON string
// Extra is the extra information for the tool.
Extra map[string]any
}
CallbackInput is the input for the tool callback.
func ConvCallbackInput ¶
func ConvCallbackInput(src callbacks.CallbackInput) *CallbackInput
ConvCallbackInput converts the callback input to the tool callback input.
type CallbackOutput ¶
type CallbackOutput struct {
// Response is the response for the tool.
Response string
// Extra is the extra information for the tool.
Extra map[string]any
}
CallbackOutput is the output for the tool callback.
func ConvCallbackOutput ¶
func ConvCallbackOutput(src callbacks.CallbackOutput) *CallbackOutput
ConvCallbackOutput converts the callback output to the tool callback output.
type InvokableTool ¶
type InvokableTool interface {
BaseTool
// InvokableRun call function with arguments in JSON format
InvokableRun(ctx context.Context, argumentsInJSON string, opts ...Option) (string, error)
}
InvokableTool the tool for ChatModel intent recognition and ToolsNode execution.
type Option ¶
type Option struct {
// contains filtered or unexported fields
}
Option defines call option for InvokableTool or StreamableTool component, which is part of component interface signature. Each tool implementation could define its own options struct and option funcs within its own package, then wrap the impl specific option funcs into this type, before passing to InvokableRun or StreamableRun.
func WrapImplSpecificOptFn ¶
WrapImplSpecificOptFn wraps the impl specific option functions into Option type. T: the type of the impl specific options struct. Tool implementations are required to use this function to convert its own option functions into the unified Option type. For example, if the tool defines its own options struct:
type customOptions struct {
conf string
}
Then the tool needs to provide an option function as such:
func WithConf(conf string) Option {
return WrapImplSpecificOptFn(func(o *customOptions) {
o.conf = conf
}
}
.