Class: AgentC::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/agent_c/session.rb

Defined Under Namespace

Classes: Configuration

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(agent_db_path:, project:, logger: Logger.new("/dev/null"), workspace_dir: Dir.pwd, run_id: Time.now.to_i, i18n_path: nil, max_spend_project: nil, max_spend_run: nil, ruby_llm: {}, extra_tools: {}, chat_provider: ->(**params) { create_chat(**params) }) ⇒ Session

Returns a new instance of Session.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/agent_c/session.rb', line 21

def initialize(
  agent_db_path:,
  project:,
  logger: Logger.new("/dev/null"),
  workspace_dir: Dir.pwd,
  run_id: Time.now.to_i,
  i18n_path: nil,
  max_spend_project: nil,
  max_spend_run: nil,
  ruby_llm: {},
  extra_tools: {},
  chat_provider: ->(**params) { create_chat(**params) }
)
  @agent_db_path = agent_db_path
  @project = project
  @logger = logger
  @workspace_dir = workspace_dir
  @run_id = run_id
  @i18n_path = i18n_path
  @max_spend_project = max_spend_project
  @max_spend_run = max_spend_run
  @ruby_llm = ruby_llm

  unless extra_tools.is_a?(Hash)
    raise ArgumentError, "extra_tools must be a hash mapping name to class or instance"
  end
  @extra_tools = extra_tools

  @chat_provider = chat_provider

  unless agent_db_path.match(/.sqlite3?$/)
    raise ArgumentError, "agent_db_path must end with '.sqlite3' or '.sqlite'"
  end

  # Load i18n path if provided
  if i18n_path
    I18n.load_path << i18n_path
  end
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



19
20
21
# File 'lib/agent_c/session.rb', line 19

def logger
  @logger
end

Instance Method Details

#agent_storeObject



148
149
150
151
152
153
154
# File 'lib/agent_c/session.rb', line 148

def agent_store
  @agent_store ||= Db::Store.new(
    path: @agent_db_path,
    logger: @logger,
    versioned: false
  )
end

#chat(tools: Tools::NAMES.keys, cached_prompts: [], workspace_dir: nil, record: nil) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/agent_c/session.rb', line 61

def chat(
  tools: Tools::NAMES.keys,
  cached_prompts: [],
  workspace_dir: nil,
  record: nil
)
  @chat_provider.call(
    tools: tools,
    cached_prompts: cached_prompts,
    workspace_dir: workspace_dir || config.workspace_dir,
    record: record,
    session: self
  )
end

#configObject



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/agent_c/session.rb', line 125

def config
  @config ||= Configuration.new(
    agent_db_path: @agent_db_path,
    logger: @logger,
    i18n_path: @i18n_path,
    workspace_dir: @workspace_dir,
    project: @project,
    run_id: @run_id,
    max_spend_project: @max_spend_project,
    max_spend_run: @max_spend_run,
    extra_tools: @extra_tools,
  )
end

#costObject



156
157
158
159
160
161
162
# File 'lib/agent_c/session.rb', line 156

def cost
  Costs::Data.calculate(
    agent_store: agent_store,
    project: config.project,
    run_id: config.run_id
  )
end

#prompt(tool_args: {}, tools: Tools::NAMES.keys + config.extra_tools.keys, cached_prompt: [], prompt:, schema:, on_chat_created: ->(*) {}) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/agent_c/session.rb', line 76

def prompt(
  tool_args: {},
  tools: Tools::NAMES.keys + config.extra_tools.keys,
  cached_prompt: [],
  prompt:,
  schema:,
  on_chat_created: ->(*) {}
)
  workspace_dir = tool_args[:workspace_dir] || config.workspace_dir

  resolved_tools = (
    tools
      .map { |value|
        Tools.resolve(
          value:,
          available_tools: Tools::NAMES.merge(config.extra_tools),
          args: tool_args,
          workspace_dir: config.workspace_dir
        )
      }
  )

  chat_instance = chat(
    tools: resolved_tools,
    cached_prompts: cached_prompt,
    workspace_dir: workspace_dir
  )
  on_chat_created.call(chat_instance.id)

  message = Array(prompt).join("\n")

  begin
    result = chat_instance.get(message, schema: Schema.result(&schema))

    Agent::ChatResponse.new(
      chat_id: chat_instance.id,
      raw_response: result,
    )
  rescue => e
    Agent::ChatResponse.new(
      chat_id: chat_instance.id,
      raw_response: {
        "status" => "error",
        "message" => ["#{e.class.name}:#{e.message}", e.backtrace].join("\n")
      },
    )
  end
end

#ruby_llm_contextObject



139
140
141
142
143
144
145
146
# File 'lib/agent_c/session.rb', line 139

def ruby_llm_context
  @ruby_llm_context ||= RubyLLM.context do |ctx_config|
    @ruby_llm.each do |key, value|
      ctx_config.public_send("#{key}=", value)
    end
    ctx_config.use_new_acts_as = true
  end
end