Module: Mistri

Defined in:
lib/mistri.rb,
lib/mistri/mcp.rb,
lib/mistri/sse.rb,
lib/mistri/edit.rb,
lib/mistri/tool.rb,
lib/mistri/agent.rb,
lib/mistri/child.rb,
lib/mistri/event.rb,
lib/mistri/locks.rb,
lib/mistri/skill.rb,
lib/mistri/tools.rb,
lib/mistri/usage.rb,
lib/mistri/budget.rb,
lib/mistri/errors.rb,
lib/mistri/memory.rb,
lib/mistri/models.rb,
lib/mistri/result.rb,
lib/mistri/schema.rb,
lib/mistri/skills.rb,
lib/mistri/console.rb,
lib/mistri/content.rb,
lib/mistri/message.rb,
lib/mistri/session.rb,
lib/mistri/spawner.rb,
lib/mistri/version.rb,
lib/mistri/reminder.rb,
lib/mistri/compactor.rb,
lib/mistri/mcp/oauth.rb,
lib/mistri/mcp/wires.rb,
lib/mistri/sinks/sse.rb,
lib/mistri/sub_agent.rb,
lib/mistri/tool_call.rb,
lib/mistri/transport.rb,
lib/mistri/compaction.rb,
lib/mistri/definition.rb,
lib/mistri/mcp/client.rb,
lib/mistri/dispatchers.rb,
lib/mistri/stop_reason.rb,
lib/mistri/task_output.rb,
lib/mistri/tool_result.rb,
lib/mistri/abort_signal.rb,
lib/mistri/partial_json.rb,
lib/mistri/retry_policy.rb,
lib/mistri/stores/jsonl.rb,
lib/mistri/tool_context.rb,
lib/mistri/stores/memory.rb,
lib/mistri/tool_executor.rb,
lib/mistri/providers/fake.rb,
lib/mistri/sinks/coalesced.rb,
lib/mistri/tools/edit_file.rb,
lib/mistri/tools/read_file.rb,
lib/mistri/providers/gemini.rb,
lib/mistri/providers/openai.rb,
lib/mistri/tools/list_files.rb,
lib/mistri/tools/write_file.rb,
lib/mistri/workspace/memory.rb,
lib/mistri/workspace/single.rb,
lib/mistri/locks/rails_cache.rb,
lib/mistri/tools/read_memory.rb,
lib/mistri/sinks/action_cable.rb,
lib/mistri/tools/find_in_file.rb,
lib/mistri/providers/anthropic.rb,
lib/mistri/tools/update_memory.rb,
lib/mistri/workspace/directory.rb,
lib/mistri/stores/active_record.rb,
lib/mistri/workspace/active_record.rb,
lib/mistri/providers/gemini/assembler.rb,
lib/mistri/providers/openai/assembler.rb,
lib/mistri/providers/gemini/serializer.rb,
lib/mistri/providers/openai/serializer.rb,
lib/generators/mistri/mcp/mcp_generator.rb,
lib/mistri/providers/anthropic/assembler.rb,
lib/mistri/providers/anthropic/serializer.rb,
lib/generators/mistri/install/install_generator.rb

Overview

Opt-in, exactly like Stores::ActiveRecord: the gem never requires Rails. require "mistri/locks/rails_cache".

Defined Under Namespace

Modules: Console, Content, Dispatchers, Edit, ErrorData, Generators, Locks, MCP, Models, PartialJson, Providers, Sinks, Skills, StopReason, Stores, TaskOutput, ToolExecutor, Tools, Workspace Classes: AbortError, AbortSignal, Agent, AuthenticationError, Budget, BudgetError, Child, Compaction, CompactionError, Compactor, ConfigurationError, Definition, EditError, Error, Event, Memory, Message, OverloadedError, ProviderError, RateLimitError, Reminder, Result, RetryPolicy, SSE, Schema, SchemaError, ServerError, Session, Skill, Spawner, SubAgent, Tool, ToolCall, ToolContext, ToolResult, Transport, Usage

Constant Summary collapse

PROVIDERS =
{ anthropic: Providers::Anthropic, openai: Providers::OpenAI,
gemini: Providers::Gemini }.freeze
API_KEY_ENV =
{ anthropic: "ANTHROPIC_API_KEY", openai: "OPENAI_API_KEY",
gemini: "GEMINI_API_KEY" }.freeze
VERSION =
"0.5.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.locksObject

Returns the value of attribute locks.



65
66
67
# File 'lib/mistri.rb', line 65

def locks
  @locks
end

Class Method Details

.agent(model, api_key: nil, provider_options: {}, **agent_options) ⇒ Object

Build an agent for a model in one call: infers and constructs the provider, then wraps it in the loop.

agent = Mistri.agent("claude-opus-4-8", tools: [weather], system: "Be brief.")
agent.run("Weather in Lahore?") { |event| ... }


89
90
91
92
# File 'lib/mistri.rb', line 89

def agent(model, api_key: nil, provider_options: {}, **agent_options)
  built = provider(model, api_key: api_key, **provider_options)
  Agent.new(provider: built, **agent_options)
end

.infer_provider_name(model) ⇒ Object



100
101
102
103
104
105
106
107
# File 'lib/mistri.rb', line 100

def infer_provider_name(model)
  case model.to_s
  when /\Aclaude/ then :anthropic
  when /\A(gpt|o\d|chatgpt)/ then :openai
  when /\Agemini/ then :gemini
  else raise ConfigurationError, "cannot infer a provider from #{model.inspect}"
  end
end

.provider(model, api_key: nil) ⇒ Object

Build a provider for a model, inferring which one from the model id and reading its key from the environment unless one is passed.

Mistri.provider("claude-opus-4-8")
Mistri.provider("gpt-5.5", api_key: key, reasoning: { effort: "high" })

Raises:



75
76
77
78
79
80
81
82
# File 'lib/mistri.rb', line 75

def provider(model, api_key: nil, **)
  name = provider_name(model)
  klass = PROVIDERS.fetch(name) { raise ConfigurationError, "no provider for #{model.inspect}" }
  key = api_key || ENV.fetch(API_KEY_ENV.fetch(name), nil)
  raise ConfigurationError, "no API key for #{name}" if key.to_s.empty?

  klass.new(api_key: key, model: model, **)
end

.provider_name(model) ⇒ Object

Catalogued models infer directly; unknown ids fall back to the id prefix, so a brand-new model works before it is catalogued.



96
97
98
# File 'lib/mistri.rb', line 96

def provider_name(model)
  Models.find(model)&.provider || infer_provider_name(model)
end