feat(gateway): record how long each chat tool call took and show it per row

This commit is contained in:
sam committed 2026-10-24 20:44:40 +11:00
1 parent a3eeeca672
commit e100b01c52
9 files changed
+149 -109

No files matched your search

+19 -5
View File
@@ -7,6 +7,7 @@ import (
"log/slog"
"path/filepath"
"strings"
"time"
"github.com/samcharles93/ai-sdk/chat"
"github.com/samcharles93/ai-sdk/core"
@@ -228,6 +229,22 @@ func runChatTurn(ctx context.Context, llm *runtime.Runtime, chatModel string, op
return text, used, nil
}
// pendingToolCall is a tool invocation awaiting its result.
type pendingToolCall struct {
name string
parameters string
started time.Time
}
// took is the call's running time so far, zero for a result with no
// recorded invocation.
func (c pendingToolCall) took() int64 {
if c.started.IsZero() {
return 0
}
return time.Since(c.started).Milliseconds()
}
// drainChatStream consumes a model stream to close, reporting assistant text
// and each completed tool call to turn, and returns the assembled reply.
//
@@ -239,11 +256,6 @@ func runChatTurn(ctx context.Context, llm *runtime.Runtime, chatModel string, op
// no outcome yet, so reporting both would show every tool twice, once with
// nothing to say.
func drainChatStream(parts <-chan core.StreamPart, turn gateway.TurnStream, icons map[string]string) string {
type pendingToolCall struct {
name string
parameters string
}
var sb strings.Builder
pending := make(map[string]pendingToolCall)
for part := range parts {
@@ -263,6 +275,7 @@ func drainChatStream(parts <-chan core.StreamPart, turn gateway.TurnStream, icon
pending[part.ToolCall.ToolCallID] = pendingToolCall{
name: part.ToolCall.ToolName,
parameters: gateway.SummarizeToolParameters(part.ToolCall.Input),
started: time.Now(),
}
case core.StreamPartToolResult:
if part.ToolResult == nil || turn == nil {
@@ -281,6 +294,7 @@ func drainChatStream(parts <-chan core.StreamPart, turn gateway.TurnStream, icon
Parameters: call.parameters,
Output: part.ToolResult.Output,
Err: part.ToolResult.Error,
DurationMS: call.took(),
})
for _, ref := range multimodalMediaRefs(part.ToolResult.Output) {
// Path and URL are alternatives, not a pair: the channel
+12 -2
View File
@@ -378,6 +378,7 @@ type ToolCall struct {
Parameters string `protobuf:"bytes,3,opt,name=parameters,proto3" json:"parameters,omitempty"`
Output string `protobuf:"bytes,4,opt,name=output,proto3" json:"output,omitempty"`
Error string `protobuf:"bytes,5,opt,name=error,proto3" json:"error,omitempty"`
DurationMs int64 `protobuf:"varint,6,opt,name=duration_ms,json=durationMs,proto3" json:"duration_ms,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
@@ -447,6 +448,13 @@ func (x *ToolCall) GetError() string {
return ""
}
func (x *ToolCall) GetDurationMs() int64 {
if x != nil {
return x.DurationMs
}
return 0
}
type Media struct {
state protoimpl.MessageState `protogen:"open.v1"`
ToolName string `protobuf:"bytes,1,opt,name=tool_name,json=toolName,proto3" json:"tool_name,omitempty"`
@@ -4028,7 +4036,7 @@ const file_gateway_v1_chat_proto_rawDesc = "" +
"\x05media\x18\v \x03(\v2\x11.gateway.v1.MediaR\x05media\x12\x1b\n" +
"\tsender_id\x18\f \x01(\tR\bsenderIdJ\x04\b\t\x10\n" +
"R\n" +
"budget_key\"|\n" +
"budget_key\"\x9d\x01\n" +
"\bToolCall\x12\x0e\n" +
"\x02id\x18\x01 \x01(\tR\x02id\x12\x12\n" +
"\x04name\x18\x02 \x01(\tR\x04name\x12\x1e\n" +
@@ -4036,7 +4044,9 @@ const file_gateway_v1_chat_proto_rawDesc = "" +
"parameters\x18\x03 \x01(\tR\n" +
"parameters\x12\x16\n" +
"\x06output\x18\x04 \x01(\tR\x06output\x12\x14\n" +
"\x05error\x18\x05 \x01(\tR\x05error\"\xf0\x02\n" +
"\x05error\x18\x05 \x01(\tR\x05error\x12\x1f\n" +
"\vduration_ms\x18\x06 \x01(\x03R\n" +
"durationMs\"\xf0\x02\n" +
"\x05Media\x12\x1b\n" +
"\ttool_name\x18\x01 \x01(\tR\btoolName\x12\x12\n" +
"\x04type\x18\x02 \x01(\tR\x04type\x12\x17\n" +
+2
View File
@@ -22,6 +22,8 @@ type ToolCallEvent struct {
// Err is non-empty when the tool failed, in which case Output is
// unreliable and usually empty.
Err string
// DurationMS is how long the tool took to run, zero when unmeasured.
DurationMS int64 `json:",omitempty"`
}
// MediaEvent reports one media attachment produced during a turn, so a
@@ -91,6 +91,7 @@ func toolProto(v messaging.ToolCallEvent) *pb.ToolCall {
Parameters: v.Parameters,
Output: v.Output,
Error: v.Err,
DurationMs: v.DurationMS,
}
}
@@ -104,6 +105,7 @@ func toolValue(v *pb.ToolCall) messaging.ToolCallEvent {
Parameters: v.Parameters,
Output: v.Output,
Err: v.Error,
DurationMS: v.DurationMs,
}
}
+4 -1
View File
@@ -131,6 +131,7 @@ type chatToolView struct {
Parameters string `json:"parameters,omitempty"`
Summary string `json:"summary"`
Failed bool `json:"failed"`
DurationMS int64 `json:"duration_ms,omitempty"`
}
func (s *Server) chatReady(w http.ResponseWriter) (*ChatService, bool) {
@@ -239,7 +240,7 @@ func (s *Server) handleChatTurns(w http.ResponseWriter, r *http.Request) {
for _, tool := range turn.ToolCalls {
view.ToolCalls = append(view.ToolCalls, chatToolView{
ID: tool.ID, Name: tool.Name, Parameters: tool.Parameters,
Summary: tool.Summary(), Failed: tool.Err != "",
Summary: tool.Summary(), Failed: tool.Err != "", DurationMS: tool.DurationMS,
})
}
views = append(views, view)
@@ -318,6 +319,7 @@ type chatStreamEvent struct {
ToolCallID string `json:"tool_call_id,omitempty"`
Parameters string `json:"parameters,omitempty"`
Failed bool `json:"failed,omitempty"`
DurationMS int64 `json:"duration_ms,omitempty"`
SessionID string `json:"session_id,omitempty"`
// Path and Label carry a dashboard_navigate result so the browser can
// render a clickable chip that routes the operator to the page. Present
@@ -366,6 +368,7 @@ func (s chatStreamSink) ToolCall(event messaging.ToolCallEvent) {
Parameters: event.Parameters,
Text: event.Summary(),
Failed: event.Err != "",
DurationMS: event.DurationMS,
})
}