Module: Mana

Defined in:
lib/mana.rb,
lib/mana/mock.rb,
lib/mana/mixin.rb,
lib/mana/config.rb,
lib/mana/engine.rb,
lib/mana/logger.rb,
lib/mana/memory.rb,
lib/mana/context.rb,
lib/mana/version.rb,
lib/mana/compiler.rb,
lib/mana/knowledge.rb,
lib/mana/introspect.rb,
lib/mana/memory_store.rb,
lib/mana/tool_handler.rb,
lib/mana/backends/base.rb,
lib/mana/prompt_builder.rb,
lib/mana/backends/openai.rb,
lib/mana/binding_helpers.rb,
lib/mana/backends/anthropic.rb

Defined Under Namespace

Modules: Backends, BindingHelpers, Compiler, Introspect, Knowledge, Logger, Mixin, PromptBuilder, TestHelpers, ToolHandler Classes: Config, ConfigError, Context, Engine, Error, FileStore, LLMError, MaxIterationsError, Memory, MemoryStore, Mock, MockError

Constant Summary collapse

VERSION =
"0.5.14"

Class Method Summary collapse

Class Method Details

.cache_dir=(dir) ⇒ Object

Cache directory for compiled methods



110
111
112
# File 'lib/mana.rb', line 110

def cache_dir=(dir)
  Compiler.cache_dir = dir
end

.clear_tools!Object

Clear all registered tools, handlers, and prompt sections



84
85
86
87
88
# File 'lib/mana.rb', line 84

def clear_tools!
  @registered_tools = []
  @tool_handlers = {}
  @prompt_sections = []
end

.configObject

Return the global config singleton (lazy-initialized)



31
32
33
# File 'lib/mana.rb', line 31

def config
  @config ||= Config.new
end

.configure {|config| ... } ⇒ Object

Yield the config for modification, return the config instance

Yields:



36
37
38
39
# File 'lib/mana.rb', line 36

def configure
  yield(config) if block_given?
  config
end

.current_mockObject

Returns the current thread's Mock instance, or nil if not in mock mode



72
73
74
# File 'lib/mana/mock.rb', line 72

def current_mock
  Thread.current[:mana_mock]
end

.memoryObject

Access current thread's context (public API kept as memory for backward compat)



56
57
58
# File 'lib/mana.rb', line 56

def memory
  Context.current
end

.mock(&block) ⇒ Object

Run a block with a mock active. Stubs defined inside are scoped to this block. The block is instance_eval'd on the Mock so prompt is available as DSL.



46
47
48
49
50
51
52
53
54
# File 'lib/mana/mock.rb', line 46

def mock(&block)
  old_mock = Thread.current[:mana_mock]
  m = Mock.new
  Thread.current[:mana_mock] = m
  m.instance_eval(&block)
# Always restore the previous mock (supports nesting)
ensure
  Thread.current[:mana_mock] = old_mock
end

.mock!Object

Enable mock mode (for use with before/after hooks in tests)



57
58
59
# File 'lib/mana/mock.rb', line 57

def mock!
  Thread.current[:mana_mock] = Mock.new
end

.mock_active?Boolean

Check if mock mode is currently active on this thread

Returns:

  • (Boolean)


67
68
69
# File 'lib/mana/mock.rb', line 67

def mock_active?
  !Thread.current[:mana_mock].nil?
end

.model=(model) ⇒ Object

Shortcut to set the model name directly



42
43
44
# File 'lib/mana.rb', line 42

def model=(model)
  config.model = model
end

.prompt_sectionsObject

Return the list of registered prompt section blocks



100
101
102
# File 'lib/mana.rb', line 100

def prompt_sections
  @prompt_sections ||= []
end

.register_prompt_section(&block) ⇒ Object

Register a block that returns text to inject into the system prompt. The block is called each time a prompt is built. Return nil or "" to skip.



94
95
96
97
# File 'lib/mana.rb', line 94

def register_prompt_section(&block)
  @prompt_sections ||= []
  @prompt_sections << block
end

.register_tool(tool_definition, &handler) ⇒ Object

Register an external tool definition with its handler block. tool_definition is a hash with :name, :description, :input_schema. The handler block receives (input) and returns a result string.



65
66
67
68
69
70
# File 'lib/mana.rb', line 65

def register_tool(tool_definition, &handler)
  @registered_tools ||= []
  @tool_handlers ||= {}
  @registered_tools << tool_definition
  @tool_handlers[tool_definition[:name]] = handler
end

.registered_toolsObject

Return a copy of the registered tool definitions



73
74
75
76
# File 'lib/mana.rb', line 73

def registered_tools
  @registered_tools ||= []
  @registered_tools.dup
end

.reset!Object

Reset all global state: config, thread-local context and mock



47
48
49
50
51
52
53
# File 'lib/mana.rb', line 47

def reset!
  @config = Config.new
  Thread.current[:mana_context] = nil
  Thread.current[:mana_memory] = nil  # backward compat for claw transition
  Thread.current[:mana_mock] = nil
  clear_tools!
end

.source(method_name, owner: nil) ⇒ Object

View generated source for a mana-compiled method



105
106
107
# File 'lib/mana.rb', line 105

def source(method_name, owner: nil)
  Compiler.source(method_name, owner: owner)
end

.tool_handlersObject

Return the name → handler mapping for registered tools



79
80
81
# File 'lib/mana.rb', line 79

def tool_handlers
  @tool_handlers ||= {}
end

.unmock!Object

Disable mock mode and restore normal LLM calls



62
63
64
# File 'lib/mana/mock.rb', line 62

def unmock!
  Thread.current[:mana_mock] = nil
end