|
| 1 | +/* |
| 2 | +Copyright 2026 The Faros Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package api |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "errors" |
| 22 | + "io" |
| 23 | + "sort" |
| 24 | + "strings" |
| 25 | + "sync" |
| 26 | + |
| 27 | + "github.com/cloudwego/eino/adk" |
| 28 | + "github.com/cloudwego/eino/callbacks" |
| 29 | + einomodel "github.com/cloudwego/eino/components/model" |
| 30 | + "github.com/cloudwego/eino/schema" |
| 31 | +) |
| 32 | + |
| 33 | +func projectEinoAssistantRunOptions(req projectAssistantRunRequest, runState *projectEinoAssistantRunState) []adk.AgentRunOption { |
| 34 | + handler := newProjectEinoAssistantModelCallbackHandler(req.StreamCallbacks, runState) |
| 35 | + if handler == nil { |
| 36 | + return nil |
| 37 | + } |
| 38 | + return []adk.AgentRunOption{adk.WithCallbacks(handler)} |
| 39 | +} |
| 40 | + |
| 41 | +func newProjectEinoAssistantModelCallbackHandler(streamCallbacks projectAssistantStreamCallbacks, runState *projectEinoAssistantRunState) callbacks.Handler { |
| 42 | + recorder := &projectEinoAssistantModelCallbackRecorder{ |
| 43 | + streamCallbacks: streamCallbacks, |
| 44 | + runState: runState, |
| 45 | + } |
| 46 | + return callbacks.NewHandlerBuilder(). |
| 47 | + OnStartFn(func(ctx context.Context, _ *callbacks.RunInfo, input callbacks.CallbackInput) context.Context { |
| 48 | + recorder.recordModelInput(input) |
| 49 | + return ctx |
| 50 | + }). |
| 51 | + OnEndFn(func(ctx context.Context, _ *callbacks.RunInfo, output callbacks.CallbackOutput) context.Context { |
| 52 | + recorder.recordModelOutput(output) |
| 53 | + return ctx |
| 54 | + }). |
| 55 | + OnEndWithStreamOutputFn(func(ctx context.Context, _ *callbacks.RunInfo, output *schema.StreamReader[callbacks.CallbackOutput]) context.Context { |
| 56 | + recorder.recordModelStream(output) |
| 57 | + return ctx |
| 58 | + }). |
| 59 | + Build() |
| 60 | +} |
| 61 | + |
| 62 | +type projectEinoAssistantModelCallbackRecorder struct { |
| 63 | + streamCallbacks projectAssistantStreamCallbacks |
| 64 | + runState *projectEinoAssistantRunState |
| 65 | + |
| 66 | + mu sync.Mutex |
| 67 | + reportedToolPreparation bool |
| 68 | +} |
| 69 | + |
| 70 | +func (r *projectEinoAssistantModelCallbackRecorder) recordModelInput(input callbacks.CallbackInput) { |
| 71 | + modelInput := einomodel.ConvCallbackInput(input) |
| 72 | + if modelInput == nil || len(modelInput.Messages) == 0 { |
| 73 | + return |
| 74 | + } |
| 75 | + r.runState.RecordModelInput(projectEinoMessagesToChat(modelInput.Messages)) |
| 76 | +} |
| 77 | + |
| 78 | +func (r *projectEinoAssistantModelCallbackRecorder) recordModelOutput(output callbacks.CallbackOutput) { |
| 79 | + modelOutput := einomodel.ConvCallbackOutput(output) |
| 80 | + if modelOutput == nil || modelOutput.Message == nil { |
| 81 | + return |
| 82 | + } |
| 83 | + reply := projectEinoAssistantReplyFromMessage(modelOutput.Message) |
| 84 | + if strings.TrimSpace(reply.Content) == "" && len(reply.ToolCalls) == 0 { |
| 85 | + return |
| 86 | + } |
| 87 | + r.runState.RecordAssistantReply(reply) |
| 88 | +} |
| 89 | + |
| 90 | +func (r *projectEinoAssistantModelCallbackRecorder) recordModelStream(output *schema.StreamReader[callbacks.CallbackOutput]) { |
| 91 | + if output == nil { |
| 92 | + return |
| 93 | + } |
| 94 | + defer output.Close() |
| 95 | + |
| 96 | + var content strings.Builder |
| 97 | + toolCalls := map[int]chatToolCall{} |
| 98 | + for { |
| 99 | + chunk, err := output.Recv() |
| 100 | + if errors.Is(err, io.EOF) { |
| 101 | + break |
| 102 | + } |
| 103 | + if err != nil { |
| 104 | + return |
| 105 | + } |
| 106 | + modelOutput := einomodel.ConvCallbackOutput(chunk) |
| 107 | + if modelOutput == nil || modelOutput.Message == nil { |
| 108 | + continue |
| 109 | + } |
| 110 | + msg := modelOutput.Message |
| 111 | + if msg.Content != "" { |
| 112 | + content.WriteString(msg.Content) |
| 113 | + if r.streamCallbacks.OnChunk != nil { |
| 114 | + r.streamCallbacks.OnChunk(msg.Content) |
| 115 | + } |
| 116 | + } |
| 117 | + if len(msg.ToolCalls) > 0 { |
| 118 | + r.reportToolPreparation() |
| 119 | + projectEinoMergeToolCalls(toolCalls, msg.ToolCalls) |
| 120 | + } |
| 121 | + } |
| 122 | + reply := projectAssistantReply{ |
| 123 | + Content: content.String(), |
| 124 | + ToolCalls: projectEinoSortedToolCalls(toolCalls), |
| 125 | + } |
| 126 | + if strings.TrimSpace(reply.Content) == "" && len(reply.ToolCalls) == 0 { |
| 127 | + return |
| 128 | + } |
| 129 | + r.runState.RecordAssistantReply(reply) |
| 130 | +} |
| 131 | + |
| 132 | +func (r *projectEinoAssistantModelCallbackRecorder) reportToolPreparation() { |
| 133 | + if r.streamCallbacks.OnStatus == nil { |
| 134 | + return |
| 135 | + } |
| 136 | + r.mu.Lock() |
| 137 | + defer r.mu.Unlock() |
| 138 | + if r.reportedToolPreparation { |
| 139 | + return |
| 140 | + } |
| 141 | + r.reportedToolPreparation = true |
| 142 | + r.streamCallbacks.OnStatus("Preparing action") |
| 143 | +} |
| 144 | + |
| 145 | +func projectEinoAssistantReplyFromMessage(msg *schema.Message) projectAssistantReply { |
| 146 | + if msg == nil { |
| 147 | + return projectAssistantReply{} |
| 148 | + } |
| 149 | + return projectAssistantReply{ |
| 150 | + Content: msg.Content, |
| 151 | + ToolCalls: projectEinoToolCallsToChat(msg.ToolCalls), |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +func projectEinoMergeToolCalls(out map[int]chatToolCall, toolCalls []schema.ToolCall) { |
| 156 | + for position, toolCall := range toolCalls { |
| 157 | + index := position |
| 158 | + if toolCall.Index != nil { |
| 159 | + index = *toolCall.Index |
| 160 | + } |
| 161 | + existing := out[index] |
| 162 | + if existing.ID == "" { |
| 163 | + existing.ID = toolCall.ID |
| 164 | + } |
| 165 | + if existing.Type == "" { |
| 166 | + existing.Type = projectEinoToolCallType(toolCall.Type) |
| 167 | + } |
| 168 | + if existing.Function.Name == "" { |
| 169 | + existing.Function.Name = toolCall.Function.Name |
| 170 | + } |
| 171 | + existing.Function.Arguments += toolCall.Function.Arguments |
| 172 | + if len(toolCall.Extra) > 0 { |
| 173 | + if existing.ExtraContent == nil { |
| 174 | + existing.ExtraContent = map[string]any{} |
| 175 | + } |
| 176 | + for key, value := range toolCall.Extra { |
| 177 | + existing.ExtraContent[key] = value |
| 178 | + } |
| 179 | + } |
| 180 | + out[index] = existing |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +func projectEinoSortedToolCalls(toolCalls map[int]chatToolCall) []chatToolCall { |
| 185 | + if len(toolCalls) == 0 { |
| 186 | + return nil |
| 187 | + } |
| 188 | + indices := make([]int, 0, len(toolCalls)) |
| 189 | + for index := range toolCalls { |
| 190 | + indices = append(indices, index) |
| 191 | + } |
| 192 | + sort.Ints(indices) |
| 193 | + out := make([]chatToolCall, 0, len(toolCalls)) |
| 194 | + for _, index := range indices { |
| 195 | + out = append(out, toolCalls[index]) |
| 196 | + } |
| 197 | + return out |
| 198 | +} |
0 commit comments