Class: PromptObjects::Repositories::ToolExecutionRepository

Inherits:
BaseRepository
  • Object
show all
Defined in:
lib/prompt_objects/repositories/tool_execution_repository.rb

Instance Method Summary collapse

Methods inherited from BaseRepository

#initialize

Constructor Details

This class inherits a constructor from PromptObjects::Repositories::BaseRepository

Instance Method Details

#create(run_id:, provider_tool_call_id:, batch_id:, batch_position:, capability_name:, arguments:, capability_policy_version: nil, resource_keys: [], id: SecureRandom.uuid) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/prompt_objects/repositories/tool_execution_repository.rb', line 6

def create(run_id:, provider_tool_call_id:, batch_id:, batch_position:,
           capability_name:, arguments:, capability_policy_version: nil,
           resource_keys: [], id: SecureRandom.uuid)
  transaction do |db|
    params = [
      id, provider_tool_call_id, run_id, batch_id, batch_position,
      capability_name, capability_policy_version, JSON.generate(resource_keys),
      JSON.generate(arguments)
    ]
    db.execute(<<~SQL, params)
      INSERT INTO tool_executions (
        id, provider_tool_call_id, run_id, batch_id, batch_position,
        capability_name, capability_policy_version, status, resource_keys, arguments
      ) VALUES (?, ?, ?, ?, ?, ?, ?, 'pending', ?, ?)
    SQL
  end
  get(id)
end

#get(id) ⇒ Object



25
26
27
28
# File 'lib/prompt_objects/repositories/tool_execution_repository.rb', line 25

def get(id)
  row = read { |db| db.get_first_row("SELECT * FROM tool_executions WHERE id = ?", [id]) }
  row && parse(row)
end

#list(run_id:) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/prompt_objects/repositories/tool_execution_repository.rb', line 30

def list(run_id:)
  read do |db|
    db.execute(
      "SELECT * FROM tool_executions WHERE run_id = ? ORDER BY batch_position ASC",
      [run_id]
    ).map { |row| parse(row) }
  end
end

#transition(id, status:, result: nil, error: nil, cancellation_reason: nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/prompt_objects/repositories/tool_execution_repository.rb', line 39

def transition(id, status:, result: nil, error: nil, cancellation_reason: nil)
  validate_status!(status)
  now = Time.now.utc.iso8601
  transaction do |db|
    current = db.get_first_row("SELECT * FROM tool_executions WHERE id = ?", [id])
    next nil unless current

    started_at = status.to_s == "running" ? (current["started_at"] || now) : current["started_at"]
    finished_at = terminal_status?(status) ? now : current["finished_at"]
    params = [
      status.to_s, result.nil? ? nil : JSON.generate(result), error&.class&.name,
      error&.message, cancellation_reason, started_at, finished_at, id
    ]
    db.execute(<<~SQL, params)
      UPDATE tool_executions SET
        status = ?, result = ?, error_class = ?, error_message = ?,
        cancellation_reason = ?, started_at = ?, finished_at = ?
      WHERE id = ?
    SQL
    parse(db.get_first_row("SELECT * FROM tool_executions WHERE id = ?", [id]))
  end
end