Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 46 additions & 43 deletions clients/go/ahp/reducers.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,51 +91,51 @@ type toolCallCommon struct {
name string
displayName string
intention *string
toolInput *ahptypes.ToolInput
contributor *ahptypes.ToolCallContributor
meta ahptypes.JSONObject
}

func toolCallMeta(tc ahptypes.ToolCallState) toolCallCommon {
switch v := tc.Value.(type) {
case *ahptypes.ToolCallStreamingState:
return toolCallCommon{v.ToolCallId, v.ToolName, v.DisplayName, v.Intention, v.Contributor, v.Meta}
return toolCallCommon{v.ToolCallId, v.ToolName, v.DisplayName, v.Intention, nil, v.Contributor, v.Meta}
case *ahptypes.ToolCallPendingConfirmationState:
return toolCallCommon{v.ToolCallId, v.ToolName, v.DisplayName, v.Intention, v.Contributor, v.Meta}
return toolCallCommon{v.ToolCallId, v.ToolName, v.DisplayName, v.Intention, v.ToolInput, v.Contributor, v.Meta}
case *ahptypes.ToolCallRunningState:
return toolCallCommon{v.ToolCallId, v.ToolName, v.DisplayName, v.Intention, v.Contributor, v.Meta}
return toolCallCommon{v.ToolCallId, v.ToolName, v.DisplayName, v.Intention, v.ToolInput, v.Contributor, v.Meta}
case *ahptypes.ToolCallAuthRequiredState:
return toolCallCommon{v.ToolCallId, v.ToolName, v.DisplayName, v.Intention, v.Contributor, v.Meta}
return toolCallCommon{v.ToolCallId, v.ToolName, v.DisplayName, v.Intention, v.ToolInput, v.Contributor, v.Meta}
case *ahptypes.ToolCallPendingResultConfirmationState:
return toolCallCommon{v.ToolCallId, v.ToolName, v.DisplayName, v.Intention, v.Contributor, v.Meta}
return toolCallCommon{v.ToolCallId, v.ToolName, v.DisplayName, v.Intention, v.ToolInput, v.Contributor, v.Meta}
case *ahptypes.ToolCallCompletedState:
return toolCallCommon{v.ToolCallId, v.ToolName, v.DisplayName, v.Intention, v.Contributor, v.Meta}
return toolCallCommon{v.ToolCallId, v.ToolName, v.DisplayName, v.Intention, v.ToolInput, v.Contributor, v.Meta}
case *ahptypes.ToolCallCancelledState:
return toolCallCommon{v.ToolCallId, v.ToolName, v.DisplayName, v.Intention, v.Contributor, v.Meta}
return toolCallCommon{v.ToolCallId, v.ToolName, v.DisplayName, v.Intention, v.ToolInput, v.Contributor, v.Meta}
}
return toolCallCommon{}
}

// toolCallInvocationAndInput pulls the optional invocationMessage and
// toolInput fields out of whichever non-terminal variant tc points to.
// Returns (StringOrMarkdown{}, nil) for variants that don't carry them.
func toolCallInvocationAndInput(tc ahptypes.ToolCallState) (ahptypes.StringOrMarkdown, *string) {
// toolCallInvocationMessage pulls the invocation message from a non-terminal
// tool-call variant.
func toolCallInvocationMessage(tc ahptypes.ToolCallState) ahptypes.StringOrMarkdown {
switch v := tc.Value.(type) {
case *ahptypes.ToolCallStreamingState:
var im ahptypes.StringOrMarkdown
if v.InvocationMessage != nil {
im = *v.InvocationMessage
}
return im, nil
return im
case *ahptypes.ToolCallPendingConfirmationState:
return v.InvocationMessage, v.ToolInput
return v.InvocationMessage
case *ahptypes.ToolCallRunningState:
return v.InvocationMessage, v.ToolInput
return v.InvocationMessage
case *ahptypes.ToolCallAuthRequiredState:
return v.InvocationMessage, v.ToolInput
return v.InvocationMessage
case *ahptypes.ToolCallPendingResultConfirmationState:
return v.InvocationMessage, v.ToolInput
return v.InvocationMessage
}
return ahptypes.StringOrMarkdown{}, nil
return ahptypes.StringOrMarkdown{}
}

func toolCallID(tc ahptypes.ToolCallState) string {
Expand Down Expand Up @@ -221,17 +221,17 @@ func endTurn(state *ahptypes.ChatState, turnID string, duration int64, turnState
continue
}
common := toolCallMeta(tc.ToolCall)
invocation, toolInput := toolCallInvocationAndInput(tc.ToolCall)
invocation := toolCallInvocationMessage(tc.ToolCall)
cancelled := &ahptypes.ToolCallCancelledState{
Status: ahptypes.ToolCallStatusCancelled,
ToolCallId: common.id,
ToolName: common.name,
DisplayName: common.displayName,
Intention: common.intention,
ToolInput: common.toolInput,
Contributor: common.contributor,
Meta: common.meta,
InvocationMessage: invocation,
ToolInput: toolInput,
Reason: ahptypes.ToolCallCancellationReasonSkipped,
}
parts = append(parts, ahptypes.ResponsePart{Value: &ahptypes.ToolCallResponsePart{
Expand Down Expand Up @@ -1026,18 +1026,20 @@ func applyToolCallDelta(state *ahptypes.ChatState, a *ahptypes.ChatToolCallDelta
if !ok {
return tc
}
current := ""
if s.PartialInput != nil {
current = *s.PartialInput
}
joined := current + a.Content
s.PartialInput = &joined
if a.Meta != nil {
s.Meta = a.Meta
}
if a.Content != nil {
partial := ""
if s.PartialInput != nil {
partial = *s.PartialInput
}
partial += *a.Content
s.PartialInput = &partial
}
if a.InvocationMessage != nil {
im := *a.InvocationMessage
s.InvocationMessage = &im
invocationMessage := *a.InvocationMessage
s.InvocationMessage = &invocationMessage
}
return tc
})
Expand Down Expand Up @@ -1067,6 +1069,9 @@ func applyToolCallReady(state *ahptypes.ChatState, a *ahptypes.ChatToolCallReady
if a.Intention != nil {
common.intention = a.Intention
}
if a.ToolInput != nil {
common.toolInput = a.ToolInput
}
common.contributor = refineToolCallContributor(common.contributor, a.Contributor)
if a.Meta != nil {
common.meta = a.Meta
Expand All @@ -1086,10 +1091,10 @@ func applyToolCallReady(state *ahptypes.ChatState, a *ahptypes.ChatToolCallReady
ToolName: common.name,
DisplayName: common.displayName,
Intention: common.intention,
ToolInput: common.toolInput,
Contributor: common.contributor,
Meta: common.meta,
InvocationMessage: a.InvocationMessage,
ToolInput: a.ToolInput,
Confirmed: *a.Confirmed,
}}
}
Expand All @@ -1099,20 +1104,17 @@ func applyToolCallReady(state *ahptypes.ChatState, a *ahptypes.ChatToolCallReady
ToolName: common.name,
DisplayName: common.displayName,
Intention: common.intention,
ToolInput: common.toolInput,
Contributor: common.contributor,
Meta: common.meta,
InvocationMessage: a.InvocationMessage,
ToolInput: a.ToolInput,
ConfirmationTitle: a.ConfirmationTitle,
RiskAssessment: a.RiskAssessment,
Edits: a.Edits,
Editable: a.Editable,
Options: a.Options,
}
if pending != nil {
if next.ToolInput == nil {
next.ToolInput = pending.ToolInput
}
if next.ConfirmationTitle == nil {
next.ConfirmationTitle = pending.ConfirmationTitle
}
Expand Down Expand Up @@ -1157,8 +1159,9 @@ func applyToolCallConfirmed(state *ahptypes.ChatState, a *ahptypes.ChatToolCallC
selected := resolveSelectedOption(s.Options, a.SelectedOptionId)
if a.Approved {
toolInput := s.ToolInput
if a.EditedToolInput != nil {
toolInput = a.EditedToolInput
if a.EditedToolInput != nil && toolInput != nil && toolInput.Inline != nil {
inline := *a.EditedToolInput
toolInput = &ahptypes.ToolInput{Inline: &inline}
}
meta := s.Meta
if a.Meta != nil {
Expand All @@ -1174,10 +1177,10 @@ func applyToolCallConfirmed(state *ahptypes.ChatState, a *ahptypes.ChatToolCallC
ToolName: s.ToolName,
DisplayName: s.DisplayName,
Intention: s.Intention,
ToolInput: toolInput,
Contributor: s.Contributor,
Meta: meta,
InvocationMessage: s.InvocationMessage,
ToolInput: toolInput,
Confirmed: confirmed,
SelectedOption: selected,
}}
Expand All @@ -1196,10 +1199,10 @@ func applyToolCallConfirmed(state *ahptypes.ChatState, a *ahptypes.ChatToolCallC
ToolName: s.ToolName,
DisplayName: s.DisplayName,
Intention: s.Intention,
ToolInput: s.ToolInput,
Contributor: s.Contributor,
Meta: meta,
InvocationMessage: s.InvocationMessage,
ToolInput: s.ToolInput,
Reason: reason,
ReasonMessage: a.ReasonMessage,
UserSuggestion: a.UserSuggestion,
Expand All @@ -1216,7 +1219,7 @@ func applyToolCallComplete(state *ahptypes.ChatState, a *ahptypes.ChatToolCallCo
}
var (
invocation ahptypes.StringOrMarkdown
toolInput *string
toolInput *ahptypes.ToolInput
confirmed = ahptypes.ToolCallConfirmationReasonNotNeeded
selectedOption *ahptypes.ConfirmationOption
preAuthContent []ahptypes.ToolResultContent
Expand Down Expand Up @@ -1266,10 +1269,10 @@ func applyToolCallComplete(state *ahptypes.ChatState, a *ahptypes.ChatToolCallCo
ToolName: common.name,
DisplayName: common.displayName,
Intention: common.intention,
ToolInput: toolInput,
Contributor: common.contributor,
Meta: common.meta,
InvocationMessage: invocation,
ToolInput: toolInput,
Success: a.Result.Success,
PastTenseMessage: a.Result.PastTenseMessage,
Content: append([]ahptypes.ToolResultContent(nil), content...),
Expand All @@ -1285,10 +1288,10 @@ func applyToolCallComplete(state *ahptypes.ChatState, a *ahptypes.ChatToolCallCo
ToolName: common.name,
DisplayName: common.displayName,
Intention: common.intention,
ToolInput: toolInput,
Contributor: common.contributor,
Meta: common.meta,
InvocationMessage: invocation,
ToolInput: toolInput,
Success: a.Result.Success,
PastTenseMessage: a.Result.PastTenseMessage,
Content: append([]ahptypes.ToolResultContent(nil), content...),
Expand Down Expand Up @@ -1317,10 +1320,10 @@ func applyToolCallResultConfirmed(state *ahptypes.ChatState, a *ahptypes.ChatToo
ToolName: s.ToolName,
DisplayName: s.DisplayName,
Intention: s.Intention,
ToolInput: s.ToolInput,
Contributor: s.Contributor,
Meta: meta,
InvocationMessage: s.InvocationMessage,
ToolInput: s.ToolInput,
Success: s.Success,
PastTenseMessage: s.PastTenseMessage,
Content: s.Content,
Expand All @@ -1340,10 +1343,10 @@ func applyToolCallResultConfirmed(state *ahptypes.ChatState, a *ahptypes.ChatToo
ToolName: s.ToolName,
DisplayName: s.DisplayName,
Intention: s.Intention,
ToolInput: s.ToolInput,
Contributor: s.Contributor,
Meta: meta,
InvocationMessage: s.InvocationMessage,
ToolInput: s.ToolInput,
Reason: ahptypes.ToolCallCancellationReasonResultDenied,
SelectedOption: s.SelectedOption,
}}
Expand Down Expand Up @@ -1372,10 +1375,10 @@ func applyToolCallAuthRequired(state *ahptypes.ChatState, a *ahptypes.ChatToolCa
ToolName: s.ToolName,
DisplayName: s.DisplayName,
Intention: s.Intention,
ToolInput: s.ToolInput,
Contributor: s.Contributor,
Meta: meta,
InvocationMessage: s.InvocationMessage,
ToolInput: s.ToolInput,
Confirmed: s.Confirmed,
SelectedOption: s.SelectedOption,
Auth: a.Auth,
Expand All @@ -1400,10 +1403,10 @@ func applyToolCallAuthResolved(state *ahptypes.ChatState, a *ahptypes.ChatToolCa
ToolName: s.ToolName,
DisplayName: s.DisplayName,
Intention: s.Intention,
ToolInput: s.ToolInput,
Contributor: s.Contributor,
Meta: meta,
InvocationMessage: s.InvocationMessage,
ToolInput: s.ToolInput,
Confirmed: s.Confirmed,
SelectedOption: s.SelectedOption,
Content: append([]ahptypes.ToolResultContent(nil), s.Content...),
Expand Down
8 changes: 4 additions & 4 deletions clients/go/ahptypes/actions.generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,8 @@ type ChatToolCallDeltaAction struct {
// contain escape sequences).
Meta map[string]json.RawMessage `json:"_meta,omitempty"`
Type ActionType `json:"type"`
// Partial parameter content to append
Content string `json:"content"`
// Partial parameter content to append, if provided by the host.
Content *string `json:"content,omitempty"`
// Updated progress message
InvocationMessage *StringOrMarkdown `json:"invocationMessage,omitempty"`
}
Expand Down Expand Up @@ -358,8 +358,8 @@ type ChatToolCallReadyAction struct {
Intention *string `json:"intention,omitempty"`
// Message describing what the tool will do or what confirmation is needed
InvocationMessage StringOrMarkdown `json:"invocationMessage"`
// Raw tool input
ToolInput *string `json:"toolInput,omitempty"`
// Final tool input
ToolInput *ToolInput `json:"toolInput,omitempty"`
// Short title for the confirmation prompt (e.g. `"Run in terminal"`, `"Write file"`)
ConfirmationTitle *StringOrMarkdown `json:"confirmationTitle,omitempty"`
// Risk assessment that informed the confirmation requirement.
Expand Down
Loading
Loading