module

Crig

Prelude module — import commonly used types with a single include Crig::Prelude.

Constants

AGENT_TOOL_NAME = "agent_tool"
ALPHABET = "_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
DEFAULT_ID_LEN = 21
DEFAULT_MCP_TOOL_TIMEOUT = 300.seconds

Default per-call timeout applied to MCP tools (upstream DEFAULT_MCP_TOOL_TIMEOUT).

DEFAULT_OUTPUT_TOOL_NAME = "final_result"
EXTRACTOR_PREAMBLE = "You are an AI assistant whose purpose is to extract structured data from the provided text.\nYou will have access to a `submit` function that defines the structure of the data to extract from the provided text.\nUse the `submit` function to submit the structured data.\nBe sure to fill out every field and ALWAYS CALL THE `submit` function, even with default values!!!."
SUBMIT_TOOL_NAME = "submit"
UNKNOWN_AGENT_NAME = "Unnamed Agent"
UPSTREAM_COMMIT = "68b4eabb8c9cf749ca73c917b9306e97fb0eda24"
UPSTREAM_SOURCE_PATH = "vendor/rig/crates/rig-core"
UPSTREAM_SOURCE_PATH_AGENT = "vendor/rig/crates/rig-agent"
UPSTREAM_URL = "https://github.com/0xPlaygrounds/rig.git"
VERSION = "0.39.1"

Class methods

allowed_tool_names_for_choice(executable_tool_names : Set(String), tool_choice : Completion::ToolChoice | Nil, output_tool_name : String | Nil, pre_filter_tool_names : Set(String) | Nil) : Set(String)
Source
assistant_text_items_from_choice(choice : OneOrMany(Completion::AssistantContent)) : Array(Completion::AssistantContent)
Source
generate_id
Source
generate_id_with_len(len : Int32) : String
Source
merge_reasoning_blocks(accumulated_reasoning : Array(Crig::Completion::Reasoning), incoming : Crig::Completion::Reasoning) : Array(Crig::Completion::Reasoning)
Source
ordered_streaming_assistant_content(reasoning_items : Array(Completion::Reasoning), text_items : Array(Completion::AssistantContent), trailing_items : Array(Completion::AssistantContent)) : OneOrMany(Completion::AssistantContent) | Nil
Source
output_tool_callable(tool_choice : Completion::ToolChoice | Nil, output_tool_name : String) : Bool
Source
pick_output_tool_name(executable_tool_names : Set(String)) : String
Source
resolve_output_mode(has_schema : Bool, has_executable_tools : Bool, output_tool_callable : Bool, provider_composes_native : Bool, requested : OutputMode) : OutputMode
Source
stream_to_stdout(stream : Crig::StreamingCompletionResponse(R), io : IO = STDOUT) : R forall R

Consume a stream to completion while writing visible text chunks to an IO.

Source
stream_to_stdout(stream : Crig::MultiTurnStreamingResult(R), io : IO = STDOUT) : Crig::PromptResponse forall R
Source
tool_choice_permits_output_tool(tool_choice : Completion::ToolChoice | Nil) : Bool
Source
tool_definition(tool : Crig::ToolDyn, name : String | Nil = nil) : Crig::Completion::ToolDefinition
Source
tool_result_to_user_message(id : String, call_id : String | Nil, tool_result : String) : Crig::Completion::Message
Source

Macros

rig_tool(description = nil, params = nil, required = nil, &block)

Define a tool from a Crystal function. The function's parameter types are used to auto-generate a JSON Schema via json-schema. Non-nilable fields are automatically marked required; nilable fields become optional.

Minimal usage

Crig.rig_tool do
  def echo(text : String) : String
    text
  end
end

With description

Crig.rig_tool description: "Echo back the input" do
  def echo(text : String) : String
    text
  end
end

With error handling

Crig.rig_tool description: "Divide two numbers" do
  def divide(x : Int32, y : Int32) : Crig::ToolMacro::Result(Int32, Crig::ToolError)
    if y == 0
      Crig::ToolMacro::Result(Int32, Crig::ToolError).err(
        Crig::ToolError.new("Division by zero")
      )
    else
      Crig::ToolMacro::Result(Int32, Crig::ToolError).ok(x // y)
    end
  end
end

With optional parameters

nilable fields are automatically excluded from the required list:

Crig.rig_tool do
  def greet(name : String, greeting : String?) : String
    "#{greeting || "Hello"}, #{name}"
  end
end
Source

Nested types