Skip to content

Commit 597a67b

Browse files
committed
implement two tool functions in the backend interface
1 parent 2644d9b commit 597a67b

File tree

3 files changed

+85
-2
lines changed

3 files changed

+85
-2
lines changed

pkg/aiusechat/openaicomp/openaicomp-convertmessage.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"strings"
1515

1616
"github.com/wavetermdev/waveterm/pkg/aiusechat/aiutil"
17+
"github.com/wavetermdev/waveterm/pkg/aiusechat/chatstore"
1718
"github.com/wavetermdev/waveterm/pkg/aiusechat/uctypes"
1819
"github.com/wavetermdev/waveterm/pkg/wavebase"
1920
)
@@ -292,3 +293,54 @@ func ConvertAIChatToUIChat(aiChat uctypes.AIChat) (*uctypes.UIChat, error) {
292293

293294
return uiChat, nil
294295
}
296+
297+
// GetFunctionCallInputByToolCallId searches for a tool call by ID in the chat history
298+
func GetFunctionCallInputByToolCallId(aiChat uctypes.AIChat, toolCallId string) *uctypes.AIFunctionCallInput {
299+
for _, genMsg := range aiChat.NativeMessages {
300+
compMsg, ok := genMsg.(*CompletionsChatMessage)
301+
if !ok {
302+
continue
303+
}
304+
idx := compMsg.Message.FindToolCallIndex(toolCallId)
305+
if idx == -1 {
306+
continue
307+
}
308+
toolCall := compMsg.Message.ToolCalls[idx]
309+
return &uctypes.AIFunctionCallInput{
310+
CallId: toolCall.ID,
311+
Name: toolCall.Function.Name,
312+
Arguments: toolCall.Function.Arguments,
313+
ToolUseData: toolCall.ToolUseData,
314+
}
315+
}
316+
return nil
317+
}
318+
319+
// UpdateToolUseData updates the ToolUseData for a specific tool call in the chat history
320+
func UpdateToolUseData(chatId string, callId string, newToolUseData *uctypes.UIMessageDataToolUse) error {
321+
chat := chatstore.DefaultChatStore.Get(chatId)
322+
if chat == nil {
323+
return fmt.Errorf("chat not found: %s", chatId)
324+
}
325+
326+
for _, genMsg := range chat.NativeMessages {
327+
compMsg, ok := genMsg.(*CompletionsChatMessage)
328+
if !ok {
329+
continue
330+
}
331+
idx := compMsg.Message.FindToolCallIndex(callId)
332+
if idx == -1 {
333+
continue
334+
}
335+
updatedMsg := compMsg.Copy()
336+
updatedMsg.Message.ToolCalls[idx].ToolUseData = newToolUseData
337+
aiOpts := &uctypes.AIOptsType{
338+
APIType: chat.APIType,
339+
Model: chat.Model,
340+
APIVersion: chat.APIVersion,
341+
}
342+
return chatstore.DefaultChatStore.PostMessage(chatId, aiOpts, updatedMsg)
343+
}
344+
345+
return fmt.Errorf("tool call with callId %s not found in chat %s", callId, chatId)
346+
}

pkg/aiusechat/openaicomp/openaicomp-types.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ func (cm *CompletionsMessage) clean() *CompletionsMessage {
4040
return &rtn
4141
}
4242

43+
func (cm *CompletionsMessage) FindToolCallIndex(toolCallId string) int {
44+
for i, tc := range cm.ToolCalls {
45+
if tc.ID == toolCallId {
46+
return i
47+
}
48+
}
49+
return -1
50+
}
51+
4352
type ToolDefinition struct {
4453
Type string `json:"type"` // "function"
4554
Function ToolFunctionDef `json:"function"`
@@ -138,3 +147,25 @@ func (m *CompletionsChatMessage) GetUsage() *uctypes.AIUsage {
138147
OutputTokens: m.Usage.CompletionTokens,
139148
}
140149
}
150+
151+
func (m *CompletionsChatMessage) Copy() *CompletionsChatMessage {
152+
if m == nil {
153+
return nil
154+
}
155+
copy := *m
156+
if len(m.Message.ToolCalls) > 0 {
157+
copy.Message.ToolCalls = make([]ToolCall, len(m.Message.ToolCalls))
158+
for i, tc := range m.Message.ToolCalls {
159+
copy.Message.ToolCalls[i] = tc
160+
if tc.ToolUseData != nil {
161+
toolUseDataCopy := *tc.ToolUseData
162+
copy.Message.ToolCalls[i].ToolUseData = &toolUseDataCopy
163+
}
164+
}
165+
}
166+
if m.Usage != nil {
167+
usageCopy := *m.Usage
168+
copy.Usage = &usageCopy
169+
}
170+
return &copy
171+
}

pkg/aiusechat/usechat-backend.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func (b *openaiCompletionsBackend) RunChatStep(
141141
}
142142

143143
func (b *openaiCompletionsBackend) UpdateToolUseData(chatId string, toolCallId string, toolUseData *uctypes.UIMessageDataToolUse) error {
144-
return fmt.Errorf("tools not supported in openai-comp backend")
144+
return openaicomp.UpdateToolUseData(chatId, toolCallId, toolUseData)
145145
}
146146

147147
func (b *openaiCompletionsBackend) ConvertToolResultsToNativeChatMessage(toolResults []uctypes.AIToolResult) ([]uctypes.GenAIMessage, error) {
@@ -153,7 +153,7 @@ func (b *openaiCompletionsBackend) ConvertAIMessageToNativeChatMessage(message u
153153
}
154154

155155
func (b *openaiCompletionsBackend) GetFunctionCallInputByToolCallId(aiChat uctypes.AIChat, toolCallId string) *uctypes.AIFunctionCallInput {
156-
return nil
156+
return openaicomp.GetFunctionCallInputByToolCallId(aiChat, toolCallId)
157157
}
158158

159159
func (b *openaiCompletionsBackend) ConvertAIChatToUIChat(aiChat uctypes.AIChat) (*uctypes.UIChat, error) {

0 commit comments

Comments
 (0)