Class: AgentClientProtocol::Contrib::ToolCallTracker

Inherits:
Object
  • Object
show all
Defined in:
lib/agent_client_protocol/contrib/tool_call_tracker.rb

Defined Under Namespace

Classes: TrackedToolCallView

Instance Method Summary collapse

Constructor Details

#initialize(id_factory: -> { SecureRandom.hex(16) }) ⇒ ToolCallTracker

Returns a new instance of ToolCallTracker.



14
15
16
17
# File 'lib/agent_client_protocol/contrib/tool_call_tracker.rb', line 14

def initialize(id_factory: -> { SecureRandom.hex(16) })
  @id_factory = id_factory
  @tracked = {} # external_id -> TrackedToolCallView
end

Instance Method Details

#append_stream_text(external_id, text, title: nil, status: nil) ⇒ Object

Raises:

  • (ArgumentError)


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/agent_client_protocol/contrib/tool_call_tracker.rb', line 71

def append_stream_text(external_id, text, title: nil, status: nil)
  view = @tracked[external_id]
  raise ArgumentError, "Unknown tool call: #{external_id}" unless view

  view.stream_text = (view.stream_text || "") + text
  view.title = title if title
  view.status = status if status

  Schema::ToolCallUpdate.new(
    tool_call_id: view.tool_call_id,
    title: title,
    status: status,
    raw_output: view.stream_text
  )
end

#forget(external_id) ⇒ Object



110
111
112
# File 'lib/agent_client_protocol/contrib/tool_call_tracker.rb', line 110

def forget(external_id)
  @tracked.delete(external_id)
end

#progress(external_id, title: nil, kind: nil, status: nil, content: nil, locations: nil, raw_input: nil, raw_output: nil) ⇒ Object

Raises:

  • (ArgumentError)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/agent_client_protocol/contrib/tool_call_tracker.rb', line 47

def progress(external_id, title: nil, kind: nil, status: nil, content: nil, locations: nil, raw_input: nil, raw_output: nil)
  view = @tracked[external_id]
  raise ArgumentError, "Unknown tool call: #{external_id}" unless view

  view.title = title if title
  view.kind = kind if kind
  view.status = status if status
  view.content = content if content
  view.locations = locations if locations
  view.raw_input = raw_input if raw_input
  view.raw_output = raw_output if raw_output

  Schema::ToolCallUpdate.new(
    tool_call_id: view.tool_call_id,
    title: title,
    kind: kind,
    status: status,
    content: content,
    locations: locations,
    raw_input: raw_input,
    raw_output: raw_output
  )
end

#start(external_id, title:, kind: nil, status: nil, content: nil, locations: nil, raw_input: nil, raw_output: nil) ⇒ Object



19
20
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
# File 'lib/agent_client_protocol/contrib/tool_call_tracker.rb', line 19

def start(external_id, title:, kind: nil, status: nil, content: nil, locations: nil, raw_input: nil, raw_output: nil)
  tool_call_id = @id_factory.call
  view = TrackedToolCallView.new(
    external_id: external_id,
    tool_call_id: tool_call_id,
    title: title,
    kind: kind,
    status: status || Schema::ToolCallStatus::PENDING,
    content: content || [],
    locations: locations || [],
    raw_input: raw_input,
    raw_output: raw_output,
    stream_text: ""
  )
  @tracked[external_id] = view

  Schema::ToolCall.new(
    tool_call_id: tool_call_id,
    title: title,
    kind: kind,
    status: status || Schema::ToolCallStatus::PENDING,
    content: content,
    locations: locations,
    raw_input: raw_input,
    raw_output: raw_output
  )
end

#tool_call_model(external_id) ⇒ Object

Raises:

  • (ArgumentError)


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/agent_client_protocol/contrib/tool_call_tracker.rb', line 94

def tool_call_model(external_id)
  v = @tracked[external_id]
  raise ArgumentError, "Unknown tool call: #{external_id}" unless v

  Schema::ToolCallUpdate.new(
    tool_call_id: v.tool_call_id,
    title: v.title,
    kind: v.kind,
    status: v.status,
    content: v.content,
    locations: v.locations,
    raw_input: v.raw_input,
    raw_output: v.raw_output
  )
end

#view(external_id) ⇒ Object

Raises:

  • (ArgumentError)


87
88
89
90
91
92
# File 'lib/agent_client_protocol/contrib/tool_call_tracker.rb', line 87

def view(external_id)
  view = @tracked[external_id]
  raise ArgumentError, "Unknown tool call: #{external_id}" unless view

  TrackedToolCallView.new(**view.to_h)
end