-
Notifications
You must be signed in to change notification settings - Fork 0
fix: multi tool rendering #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,9 +27,9 @@ def to_h | |
| ancestor_entries = forked_transcript ? load_ancestor_entries(fork_parent_id) : [] | ||
| raw_entries = ancestor_entries + session_entries | ||
| tool_name_by_call_id = build_tool_call_name_map(raw_entries) | ||
| ancestor_count = ancestor_entries.map { |entry| map_entry(entry, tool_name_by_call_id) }.compact.length | ||
| ancestor_count = ancestor_entries.sum { |entry| map_entries(entry, tool_name_by_call_id).length } | ||
| entries = normalize_parent_links( | ||
| raw_entries.map { |entry| map_entry(entry, tool_name_by_call_id) }.compact, | ||
| raw_entries.flat_map { |entry| map_entries(entry, tool_name_by_call_id) }, | ||
| raw_entries, | ||
| ) | ||
| leaf_id = entries.last&.dig(:id) | ||
|
|
@@ -166,39 +166,41 @@ def nearest_mapped_parent_id(parent_id, mapped_ids, raw_parent_by_id) | |
| nil | ||
| end | ||
|
|
||
| def map_entry(entry, tool_name_by_call_id) | ||
| def map_entries(entry, tool_name_by_call_id) | ||
| case entry[:type] | ||
| when "message" | ||
| map_message_entry(entry, tool_name_by_call_id) | ||
| map_message_entries(entry, tool_name_by_call_id) | ||
| when "reasoning_change" | ||
| { | ||
| [{ | ||
| id: entry[:id], | ||
| parentId: entry[:parent_id], | ||
| timestamp: entry[:timestamp], | ||
| type: "reasoningChange", | ||
| reasoning: entry.dig(:data, :reasoning).to_s, | ||
| } | ||
| }] | ||
| when "model_change" | ||
| { | ||
| [{ | ||
| id: entry[:id], | ||
| parentId: entry[:parent_id], | ||
| timestamp: entry[:timestamp], | ||
| type: "modelChange", | ||
| modelId: entry.dig(:data, :model_id).to_s, | ||
| } | ||
| }] | ||
| when "compaction" | ||
| { | ||
| [{ | ||
| id: entry[:id], | ||
| parentId: entry[:parent_id], | ||
| timestamp: entry[:timestamp], | ||
| type: "compaction", | ||
| summary: compaction_summary(entry), | ||
| tokensBefore: compaction_tokens_before(entry), | ||
| } | ||
| }] | ||
| else | ||
| [] | ||
| end | ||
| end | ||
|
|
||
| def map_message_entry(entry, tool_name_by_call_id) | ||
| def map_message_entries(entry, tool_name_by_call_id) | ||
| data = entry[:data] | ||
|
|
||
| mapped = { | ||
|
|
@@ -210,16 +212,19 @@ def map_message_entry(entry, tool_name_by_call_id) | |
| } | ||
|
|
||
| if data[:role] == "user" && tool_result_message?(data) | ||
| tool_result_block = data[:content].find { |block| block[:type] == "tool_result" } | ||
| mapped[:message] = { | ||
| role: "toolResult", | ||
| toolCallId: tool_result_block[:tool_use_id], | ||
| toolName: tool_name_by_call_id[tool_result_block[:tool_use_id]], | ||
| content: normalize_tool_result_content(tool_result_block[:content]), | ||
| isError: false, | ||
| details: nil, | ||
| } | ||
| return mapped | ||
| return data[:content].each_with_index.map do |tool_result_block, index| | ||
| mapped.merge( | ||
| id: tool_result_entry_id(entry[:id], index, data[:content].length), | ||
| message: { | ||
| role: "toolResult", | ||
| toolCallId: tool_result_block[:tool_use_id], | ||
| toolName: tool_name_by_call_id[tool_result_block[:tool_use_id]], | ||
| content: normalize_tool_result_content(tool_result_block[:content]), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When iterating over |
||
| isError: false, | ||
| details: nil, | ||
| }, | ||
| ) | ||
| end | ||
| end | ||
|
|
||
| mapped[:message] = { | ||
|
|
@@ -232,7 +237,13 @@ def map_message_entry(entry, tool_name_by_call_id) | |
| usage: normalize_usage(entry[:usage] || data[:usage]), | ||
| } | ||
|
|
||
| mapped | ||
| [mapped] | ||
| end | ||
|
|
||
| def tool_result_entry_id(event_id, index, result_count) | ||
| return event_id if result_count == 1 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
|
||
| "#{event_id}:tool-result:#{index}" | ||
| end | ||
|
|
||
| def tool_result_message?(data) | ||
|
|
@@ -254,6 +265,7 @@ def normalize_usage(usage) | |
| output: usage[:total_output_tokens] || usage[:output] || usage[:output_tokens] || output_details[:output_tokens] || 0, | ||
| cacheRead: usage[:cache_read] || input_details[:cache_read_input_tokens] || input_details[:cached_tokens] || 0, | ||
| cacheWrite: usage[:cache_write] || input_details[:cache_creation_input_tokens] || 0, | ||
| cost: (usage.dig(:cost, :total) || 0).to_f, | ||
| } | ||
| end | ||
|
|
||
|
|
@@ -429,7 +441,7 @@ def build_tool_pair_entry(parent_entry:, tool_id:, tool_name:, input:, result_co | |
| end | ||
|
|
||
| def compute_stats(entries) | ||
| stats = { developer: 0, user: 0, assistant: 0, tool_results: 0, compactions: 0, tool_calls: 0, tokens: Hash.new(0), models: Set.new } | ||
| stats = { developer: 0, user: 0, assistant: 0, tool_results: 0, tool_calls: 0, cost: 0.0, compactions: 0, tokens: Hash.new(0), models: Set.new } | ||
| entries.each do |entry| | ||
| if entry[:type] == "compaction" | ||
| stats[:compactions] += 1 | ||
|
|
@@ -446,7 +458,10 @@ def compute_stats(entries) | |
| stats[:assistant] += 1 | ||
| stats[:models] << [msg[:provider], msg[:model]].compact.join("/") if msg[:model] | ||
| stats[:tool_calls] += msg[:content].count { |block| block[:type] == "toolCall" } | ||
| [:input, :output, :cacheRead, :cacheWrite].each { |key| stats[:tokens][key] += msg[:usage][key].to_i } if msg[:usage] | ||
| if msg[:usage] | ||
| [:input, :output, :cacheRead, :cacheWrite].each { |key| stats[:tokens][key] += msg[:usage][key].to_i } | ||
| stats[:cost] += msg[:usage][:cost] | ||
| end | ||
| end | ||
| stats | ||
| end | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ancestor_countis now computed by summing the lengths of mapped entry arrays. Ensure thatmap_entriesis pure and side-effect free, since it is called twice for ancestor entries — once here and once during theflat_maponraw_entries. Ifmap_entrieshas any side effects or is expensive, this double invocation could be problematic. Consider computing it from the already-mappedentriesslice instead.