Help us shape the future of Go! Take the 2026 Go Developer Survey

types

package
v0.9.17 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CompletionMessageRoleTypeUser      = CompletionMessageRoleType("user")
	CompletionMessageRoleTypeSystem    = CompletionMessageRoleType("system")
	CompletionMessageRoleTypeAssistant = CompletionMessageRoleType("assistant")
	CompletionMessageRoleTypeTool      = CompletionMessageRoleType("tool")
)

Chat message role defined by the OpenAI API.

View Source
const (
	PromptURLEnvVar   = "GPTSCRIPT_PROMPT_URL"
	PromptTokenEnvVar = "GPTSCRIPT_PROMPT_TOKEN"
)
View Source
const (
	DaemonPrefix    = "#!sys.daemon"
	OpenAPIPrefix   = "#!sys.openapi"
	EchoPrefix      = "#!sys.echo"
	CallPrefix      = "#!sys.call"
	MCPPrefix       = "#!mcp"
	MCPInvokePrefix = "#!sys.mcp.invoke."
	CommandPrefix   = "#!"
	PromptPrefix    = "!!"
)
View Source
const (
	ToolTypeContext    = ToolType("context")
	ToolTypeAgent      = ToolType("agent")
	ToolTypeOutput     = ToolType("output")
	ToolTypeInput      = ToolType("input")
	ToolTypeTool       = ToolType("tool")
	ToolTypeCredential = ToolType("credential")
	ToolTypeDefault    = ToolType("")

	ToolTypeAssistant = ToolType("assistant")
	ToolTypeProvider  = ToolType("provider")
)
View Source
const InvalidToolCallArgumentsKey = "_invalid_arguments"

InvalidToolCallArgumentsKey is the key under which the raw text of tool call arguments that were rejected by ValidateToolCallArguments is kept, so the history sent back to the model stays valid JSON without losing what the model actually produced.

View Source
const RawToolCallArgumentsKey = "_raw_arguments"

RawToolCallArgumentsKey is the key under which arguments that were accepted but are not a JSON object (plain text, which gptscript passes to the tool as its input) are sent back to the provider. They are kept apart from InvalidToolCallArgumentsKey so the model is never told that a call which ran had invalid arguments.

Variables

View Source
var (
	DefaultFiles = []string{"agent.gpt", "tool.gpt"}
)

Functions

func CheckStoredToolCallArguments added in v0.9.17

func CheckStoredToolCallArguments(args string) (string, error)

CheckStoredToolCallArguments is ValidateToolCallArguments for arguments read back from the history, where a rejected call is stored wrapped by InvalidToolCallArguments. It returns the raw text the model sent and why it was rejected, or the arguments unchanged and nil when they are usable.

func FirstSet

func FirstSet[T comparable](in ...T) (result T)

func GetToolRefInput

func GetToolRefInput(prg *Program, ref ToolReference, input string) (string, error)

func InvalidToolCallArguments added in v0.9.17

func InvalidToolCallArguments(args string) string

InvalidToolCallArguments returns the arguments to keep in the history for a call rejected by ValidateToolCallArguments. Arguments that are already in that form are returned unchanged.

func IsMatch

func IsMatch(subTool string) bool

func ObjectSchema

func ObjectSchema(kv ...string) *jsonschema.Schema

func ParseCredentialArgs

func ParseCredentialArgs(toolName string, input string) (string, string, string, map[string]any, error)

ParseCredentialArgs parses a credential tool name + args into a tool alias (if there is one) and a map of args. Example: "toolName as myCredential with value1 as arg1 and value2 as arg2" -> toolName, myCredential, map[string]any{"arg1": "value1", "arg2": "value2"}, nil

Arg references will be resolved based on the input. Example: - toolName: "toolName with ${var1} as arg1 and ${var2} as arg2" - input: `{"var1": "value1", "var2": "value2"}` result: toolName, "", map[string]any{"arg1": "value1", "arg2": "value2"}, nil

func PickToolName

func PickToolName(toolName string, existing map[string]struct{}) string

func SanitizeToolCallArguments added in v0.9.17

func SanitizeToolCallArguments(args string) string

SanitizeToolCallArguments returns arguments that are safe to send back to a model provider as part of the chat history. A valid JSON object is returned unchanged and empty arguments become an empty object. Text that tried to be a JSON object and failed is wrapped under InvalidToolCallArgumentsKey; anything else (plain text, or JSON that is not an object) under RawToolCallArgumentsKey. Providers reject a whole request when any assistant tool call in its history carries arguments that are not a JSON object, which would otherwise break every later turn.

func SplitArg

func SplitArg(hasArg string) (prefix, arg string)

SplitArg splits a tool string into the tool name and arguments, and discards the alias if there is one. Examples: toolName => toolName, "" toolName as myAlias => toolName, "" toolName with value1 as arg1 and value2 as arg2 => toolName, "value1 as arg1 and value2 as arg2" toolName as myAlias with value1 as arg1 and value2 as arg2 => toolName, "value1 as arg1 and value2 as arg2"

func SplitToolRef

func SplitToolRef(targetToolName string) (toolName, subTool string)

func ToDisplayText

func ToDisplayText(tool Tool, input string) string

func ToSysDisplayString

func ToSysDisplayString(id string, args map[string]string) (string, error)

func ToToolName

func ToToolName(toolName, subTool string) string

func ToolNormalizer

func ToolNormalizer(tool string) string

func ValidateToolCallArguments added in v0.9.17

func ValidateToolCallArguments(args string) error

ValidateToolCallArguments reports why tool call arguments from a model cannot be trusted to say what the model meant, or nil when they can.

Arguments that look like a JSON object must be one: models that run out of output tokens stop mid-string, and running the tool on that text runs it with whatever happens to survive (an empty write, for example). A JSON object with a key repeated is rejected too, because the tool would only see one of the values. Empty arguments and plain text that never tried to be JSON are left alone: gptscript passes such text to the tool as its input.

Types

type BuiltinFunc

type BuiltinFunc func(ctx context.Context, env []string, input string, progress chan<- string) (string, error)

type ChatCompletionTool

type ChatCompletionTool struct {
	Function CompletionFunctionDefinition `json:"function,omitempty"`
}

type CompletionFunctionCall

type CompletionFunctionCall struct {
	Name      string `json:"name,omitempty"`
	Arguments string `json:"arguments,omitempty"`
}

type CompletionFunctionDefinition

type CompletionFunctionDefinition struct {
	ToolID      string             `json:"toolID,omitempty"`
	Name        string             `json:"name"`
	Description string             `json:"description,omitempty"`
	Parameters  *jsonschema.Schema `json:"parameters"`
}

type CompletionMessage

type CompletionMessage struct {
	Role    CompletionMessageRoleType `json:"role,omitempty"`
	Content []ContentPart             `json:"content,omitempty"`
	// ToolCall should be set for only messages of type "tool" and Content[0].Text should be set as the
	// result of the call describe by this field
	ToolCall *CompletionToolCall `json:"toolCall,omitempty"`
	Usage    Usage               `json:"usage,omitempty"`
}

func (CompletionMessage) ChatText

func (c CompletionMessage) ChatText() string

func (CompletionMessage) IsToolCall

func (c CompletionMessage) IsToolCall() bool

func (CompletionMessage) String

func (c CompletionMessage) String() string

type CompletionMessageRoleType

type CompletionMessageRoleType string

type CompletionRequest

type CompletionRequest struct {
	Model                string               `json:"model,omitempty"`
	InternalSystemPrompt *bool                `json:"internalSystemPrompt,omitempty"`
	Tools                []ChatCompletionTool `json:"tools,omitempty"`
	Messages             []CompletionMessage  `json:"messages,omitempty"`
	MaxTokens            int                  `json:"maxTokens,omitempty"`
	MaxContextTokens     int                  `json:"maxContextTokens,omitempty"`
	Chat                 bool                 `json:"chat,omitempty"`
	Temperature          *float32             `json:"temperature,omitempty"`
	JSONResponse         bool                 `json:"jsonResponse,omitempty"`
	Cache                *bool                `json:"cache,omitempty"`
}

func (*CompletionRequest) GetCache

func (r *CompletionRequest) GetCache() bool

type CompletionStatus

type CompletionStatus struct {
	CompletionID    string
	Request         any
	Response        any
	Usage           Usage
	Cached          bool
	PartialResponse *CompletionMessage
}

type CompletionToolCall

type CompletionToolCall struct {
	Index    *int                   `json:"index,omitempty"`
	ID       string                 `json:"id,omitempty"`
	Function CompletionFunctionCall `json:"function,omitempty"`
}

type ContentPart

type ContentPart struct {
	Text     string              `json:"text,omitempty"`
	ToolCall *CompletionToolCall `json:"toolCall,omitempty"`
}

func Text

func Text(text string) []ContentPart

type ErrToolNotFound

type ErrToolNotFound struct {
	ToolName string
}

func NewErrToolNotFound

func NewErrToolNotFound(toolName string) *ErrToolNotFound

func (*ErrToolNotFound) Error

func (e *ErrToolNotFound) Error() string

type Field

type Field struct {
	Name        string   `json:"name,omitempty"`
	Sensitive   *bool    `json:"sensitive,omitempty"`
	Description string   `json:"description,omitempty"`
	Options     []string `json:"options,omitempty"`
}

func (*Field) UnmarshalJSON

func (f *Field) UnmarshalJSON(b []byte) error

UnmarshalJSON will unmarshal the corresponding JSON object for a Field, or a string (for backwards compatibility).

type Fields

type Fields []Field

func (*Fields) UnmarshalJSON

func (f *Fields) UnmarshalJSON(b []byte) error

UnmarshalJSON will unmarshal the corresponding JSON object for Fields, or a comma-separated strings (for backwards compatibility).

type Parameters

type Parameters struct {
	Name                string             `json:"name,omitempty"`
	Description         string             `json:"description,omitempty"`
	MaxTokens           int                `json:"maxTokens,omitempty"`
	MaxContextTokens    int                `json:"maxContextTokens,omitempty"`
	ModelName           string             `json:"modelName,omitempty"`
	ModelProvider       bool               `json:"modelProvider,omitempty"`
	JSONResponse        bool               `json:"jsonResponse,omitempty"`
	Chat                bool               `json:"chat,omitempty"`
	Temperature         *float32           `json:"temperature,omitempty"`
	Cache               *bool              `json:"cache,omitempty"`
	InternalPrompt      *bool              `json:"internalPrompt"`
	Arguments           *jsonschema.Schema `json:"arguments,omitempty"`
	Tools               []string           `json:"tools,omitempty"`
	GlobalTools         []string           `json:"globalTools,omitempty"`
	GlobalModelName     string             `json:"globalModelName,omitempty"`
	Context             []string           `json:"context,omitempty"`
	ExportContext       []string           `json:"exportContext,omitempty"`
	Export              []string           `json:"export,omitempty"`
	Agents              []string           `json:"agents,omitempty"`
	Credentials         []string           `json:"credentials,omitempty"`
	ExportCredentials   []string           `json:"exportCredentials,omitempty"`
	InputFilters        []string           `json:"inputFilters,omitempty"`
	ExportInputFilters  []string           `json:"exportInputFilters,omitempty"`
	OutputFilters       []string           `json:"outputFilters,omitempty"`
	ExportOutputFilters []string           `json:"exportOutputFilters,omitempty"`
	Blocking            bool               `json:"-"`
	Stdin               bool               `json:"stdin,omitempty"`
	Type                ToolType           `json:"type,omitempty"`
}

func (Parameters) ToolRefNames

func (p Parameters) ToolRefNames() []string

type Program

type Program struct {
	Name         string         `json:"name,omitempty"`
	EntryToolID  string         `json:"entryToolId,omitempty"`
	ToolSet      ToolSet        `json:"toolSet,omitempty"`
	OpenAPICache map[string]any `json:"-"`
}

func (Program) ChatName

func (p Program) ChatName() string

func (Program) IsChat

func (p Program) IsChat() bool

func (Program) SetBlocking

func (p Program) SetBlocking() Program

func (Program) TopLevelTools

func (p Program) TopLevelTools() (result []Tool)

type Prompt

type Prompt struct {
	Message   string            `json:"message,omitempty"`
	Fields    Fields            `json:"fields,omitempty"`
	Sensitive bool              `json:"sensitive,omitempty"`
	Metadata  map[string]string `json:"metadata,omitempty"`
}

type Repo

type Repo struct {
	// VCS The VCS type, such as "git"
	VCS string
	// The URL where the VCS repo can be found
	Root string
	// The path in the repo of this source. This should refer to a directory and not the actual file
	Path string
	// The filename of the source in the repo, relative to Path
	Name string
	// The revision of this source
	Revision string
}

type Tool

type Tool struct {
	ToolDef `json:",inline"`

	ID          string                     `json:"id,omitempty"`
	ToolMapping map[string][]ToolReference `json:"toolMapping,omitempty"`
	LocalTools  map[string]string          `json:"localTools,omitempty"`
	Source      ToolSource                 `json:"source,omitempty"`
	WorkingDir  string                     `json:"workingDir,omitempty"`
}

func (*Tool) AddToolMapping

func (t *Tool) AddToolMapping(name string, tool Tool)

func (Tool) GetChatCompletionTools

func (t Tool) GetChatCompletionTools(prg Program, agentGroup ...ToolReference) (result []ChatCompletionTool, err error)

func (Tool) GetInterpreter

func (t Tool) GetInterpreter() string

func (Tool) GetNextAgentGroup

func (t Tool) GetNextAgentGroup(prg *Program, agentGroup []ToolReference, toolID string) (result []ToolReference, _ error)

func (Tool) GetToolRefsFromNames

func (t Tool) GetToolRefsFromNames(names []string) (result []ToolReference, _ error)

func (Tool) GetToolsByType

func (t Tool) GetToolsByType(prg *Program, toolType ToolType) ([]ToolReference, error)

func (Tool) IsAgentsOnly

func (t Tool) IsAgentsOnly() bool

func (Tool) IsCall

func (t Tool) IsCall() bool

func (Tool) IsCommand

func (t Tool) IsCommand() bool

func (Tool) IsDaemon

func (t Tool) IsDaemon() bool

func (Tool) IsEcho

func (t Tool) IsEcho() bool

func (Tool) IsHTTP

func (t Tool) IsHTTP() bool

func (Tool) IsMCP

func (t Tool) IsMCP() bool

func (Tool) IsMCPInvoke

func (t Tool) IsMCPInvoke() bool

func (Tool) IsNoop

func (t Tool) IsNoop() bool

func (Tool) IsOpenAPI

func (t Tool) IsOpenAPI() bool

type ToolDef

type ToolDef struct {
	Parameters   `json:",inline"`
	Instructions string            `json:"instructions,omitempty"`
	BuiltinFunc  BuiltinFunc       `json:"-"`
	MetaData     map[string]string `json:"metaData,omitempty"`
}

func (ToolDef) Print

func (t ToolDef) Print() string

func (ToolDef) String

func (t ToolDef) String() string

type ToolReference

type ToolReference struct {
	Named     string `json:"named,omitempty"`
	Reference string `json:"reference,omitempty"`
	Arg       string `json:"arg,omitempty"`
	ToolID    string `json:"toolID,omitempty"`
}

type ToolSet

type ToolSet map[string]Tool

type ToolSource

type ToolSource struct {
	Location string `json:"location,omitempty"`
	LineNo   int    `json:"lineNo,omitempty"`
	Repo     *Repo  `json:"repo,omitempty"`
}

func (ToolSource) IsGit

func (t ToolSource) IsGit() bool

func (ToolSource) String

func (t ToolSource) String() string

type ToolType

type ToolType string

type Usage

type Usage struct {
	PromptTokens     int `json:"promptTokens,omitempty"`
	CompletionTokens int `json:"completionTokens,omitempty"`
	TotalTokens      int `json:"totalTokens,omitempty"`
}

Jump to

Keyboard shortcuts

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