Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(agent): preserve sandbox artifacts through streaming (#19355)

Sbaaoui Idrissd796dcfb6f52
diff --git a/agent/sandbox/executor_manager/services/execution.py b/agent/sandbox/executor_manager/services/execution.py
index d8a36bdae3..935d94b3a6 100644
--- a/agent/sandbox/executor_manager/services/execution.py
+++ b/agent/sandbox/executor_manager/services/execution.py
@@ -262,6 +262,7 @@ async def execute_code(req: CodeExecutionRequest):
if returncode == 0:
clean_stdout, structured_result = _extract_result_envelope(stdout)
+ await _promote_root_artifacts(container, task_id)
artifacts = await _collect_artifacts(container, task_id, workdir)
return CodeExecutionResult(
status=ResultStatus.SUCCESS,
@@ -329,6 +330,31 @@ MAX_ARTIFACT_SIZE = 10 * 1024 * 1024 # 10MB per file
async def _collect_artifacts(container: str, task_id: str, host_workdir: str) -> list[ArtifactItem]:
artifacts_path = f"/workspace/{task_id}/artifacts"
+ return await _collect_artifacts_from_path(container, artifacts_path)
+
+
+async def _promote_root_artifacts(container: str, task_id: str) -> None:
+ """Move allowlisted outputs into the request's artifacts directory."""
+ task_root = f"/workspace/{task_id}"
+ artifacts = f"{task_root}/artifacts"
+ excluded = {"main.py", "runner.py", "args.json"}
+ returncode, _, _ = await async_run_command("docker", "exec", container, "mkdir", "-p", artifacts, timeout=5)
+ if returncode != 0:
+ return
+ returncode, stdout, _ = await async_run_command("docker", "exec", container, "find", task_root, "-maxdepth", "1", "-type", "f", "-print0", timeout=5)
+ if returncode != 0:
+ return
+ for path in stdout.split("\0"):
+ name = path.rsplit("/", 1)[-1]
+ if not name or name in excluded or name.startswith(".") or any(ord(char) < 32 or ord(char) == 127 for char in name):
+ continue
+ if os.path.splitext(name)[1].lower() not in ALLOWED_ARTIFACT_EXTENSIONS:
+ continue
+ await async_run_command("docker", "exec", container, "mv", path, f"{artifacts}/{name}", timeout=5)
+
+
+async def _collect_artifacts_from_path(container: str, artifacts_path: str, excluded: set[str] | None = None) -> list[ArtifactItem]:
+ excluded = excluded or set()
# List files in the artifacts directory inside the container
returncode, stdout, _ = await async_run_command(
@@ -341,14 +367,17 @@ async def _collect_artifacts(container: str, task_id: str, host_workdir: str) ->
"1",
"-type",
"f",
+ "-print0",
timeout=5,
)
if returncode != 0 or not stdout.strip():
return []
- raw_names = [line.split("/")[-1] for line in stdout.strip().splitlines() if line.strip()]
+ raw_names = [path.rsplit("/", 1)[-1] for path in stdout.split("\0") if path]
# Sanitize: reject names with path traversal or control characters
- filenames = [n for n in raw_names if n and "/" not in n and "\\" not in n and ".." not in n and not n.startswith(".")]
+ filenames = [
+ n for n in raw_names if n and n not in excluded and "/" not in n and "\\" not in n and ".." not in n and not n.startswith(".") and not any(ord(char) < 32 or ord(char) == 127 for char in n)
+ ]
if not filenames:
return []
diff --git a/agent/sandbox/executor_manager/tests/test_artifact_promotion.py b/agent/sandbox/executor_manager/tests/test_artifact_promotion.py
new file mode 100644
index 0000000000..9b0df4e503
--- /dev/null
+++ b/agent/sandbox/executor_manager/tests/test_artifact_promotion.py
@@ -0,0 +1,46 @@
+import asyncio
+import unittest
+from unittest.mock import patch
+
+from services import execution
+
+
+class ArtifactPromotionTest(unittest.TestCase):
+ def test_promote_root_artifacts_moves_task_output(self):
+ calls = []
+
+ async def run_command(*args, **kwargs):
+ calls.append(args)
+ if args[-7:] == ("find", "/workspace/task-id", "-maxdepth", "1", "-type", "f", "-print0"):
+ return 0, "/workspace/task-id/chart.png\0", ""
+ return 0, "", ""
+
+ with patch.object(execution, "async_run_command", run_command):
+ asyncio.run(execution._promote_root_artifacts("runner", "task-id"))
+
+ self.assertIn(
+ (
+ "docker",
+ "exec",
+ "runner",
+ "mv",
+ "/workspace/task-id/chart.png",
+ "/workspace/task-id/artifacts/chart.png",
+ ),
+ calls,
+ )
+ self.assertNotIn(("docker", "exec", "runner", "find", "/workspace", "-maxdepth", "1", "-type", "f", "-print0"), calls)
+
+ def test_promote_root_artifacts_skips_control_characters(self):
+ calls = []
+
+ async def run_command(*args, **kwargs):
+ calls.append(args)
+ if args[-7:] == ("find", "/workspace/task-id", "-maxdepth", "1", "-type", "f", "-print0"):
+ return 0, "/workspace/task-id/chart\ncopy.png\0", ""
+ return 0, "", ""
+
+ with patch.object(execution, "async_run_command", run_command):
+ asyncio.run(execution._promote_root_artifacts("runner", "task-id"))
+
+ self.assertFalse(any(call[3] == "mv" for call in calls))
diff --git a/internal/agent/component/agent.go b/internal/agent/component/agent.go
index b65bb2f06c..59c3fe425f 100644
--- a/internal/agent/component/agent.go
+++ b/internal/agent/component/agent.go
@@ -174,6 +174,11 @@ func runEinoReActAgent(ctx context.Context, p AgentParam) (*schema.Message, erro
// the final model response, so max_rounds=1 can complete a tool call and
// produce its answer instead of failing with "exceeds max steps".
maxSteps := p.MaxRounds*2 + 1
+ if hasCodeExecTool(p.Tools) {
+ // CodeExec user-code failures return a non-zero tool result, allowing
+ // one repair attempt without changing other agents' step budget.
+ maxSteps += 2
+ }
agent, err := react.NewAgent(ctx, &react.AgentConfig{
ToolCallingModel: chatModel,
@@ -369,6 +374,10 @@ func emitAgentModelStreams(ctx context.Context, future react.MessageFuture) <-ch
if msg == nil {
continue
}
+ if msg.Role == schema.Tool {
+ recordArtifactsFromToolMessage(ctx, msg)
+ continue
+ }
if msg.Role != "" && msg.Role != schema.Assistant {
continue
}
@@ -640,6 +649,16 @@ func optimizeMultiTurnQuestion(ctx context.Context, db *gorm.DB, p AgentParam, h
return strings.TrimSpace(resp.Content), nil
}
+func hasCodeExecTool(names []string) bool {
+ for _, name := range names {
+ switch strings.ToLower(strings.TrimSpace(name)) {
+ case "codeexec", "code_exec", "execute_code":
+ return true
+ }
+ }
+ return false
+}
+
func buildAgentTools(ctx context.Context, p AgentParam) ([]einotool.BaseTool, error) {
tools, err := agenttool.BuildAll(p.Tools, p.ToolParams)
if err != nil {
@@ -905,6 +924,7 @@ func (c *AgentComponent) invokeNow(ctx context.Context, db *gorm.DB, inputs map[
}
}
+ ctx = prepareArtifactCollector(ctx)
msg, err := agentRunner(ctx, p)
// Tool-call memory summarization. After the ReAct loop
// completes, summarize the tool calls via an LLM and append to
@@ -1109,29 +1129,38 @@ type artifactEntry struct {
URL string `json:"url"`
}
-// artifactCollectorKey is the context key used to stash the
-// MessageFuture from react.WithMessageFuture() so the AgentComponent
-// can collect artifacts after the ReAct loop finishes. The collector
-// is created per-invocation in runEinoReActAgent.
+// artifactCollectorKey is the context key used to share the
+// MessageFuture between Agent.Invoke and runEinoReActAgent.
type artifactCollectorKey struct{}
-// setArtifactCollector registers the MessageFuture for this agent run
-// in the context. It is called from runEinoReActAgent after
-// react.WithMessageFuture() returns a future.
+type artifactCollector struct {
+ future react.MessageFuture
+ artifacts []artifactEntry
+}
+
+func prepareArtifactCollector(ctx context.Context) context.Context {
+ if _, ok := ctx.Value(artifactCollectorKey{}).(*artifactCollector); ok {
+ return ctx
+ }
+ return context.WithValue(ctx, artifactCollectorKey{}, &artifactCollector{})
+}
+
+// setArtifactCollector registers the MessageFuture for this agent run.
+// Invoke creates the holder before entering the runner so the runner can
+// publish its future without replacing the caller's immutable context.
func setArtifactCollector(ctx context.Context, future react.MessageFuture) context.Context {
- return context.WithValue(ctx, artifactCollectorKey{}, future)
+ if collector, ok := ctx.Value(artifactCollectorKey{}).(*artifactCollector); ok {
+ collector.future = future
+ return ctx
+ }
+ return context.WithValue(ctx, artifactCollectorKey{}, &artifactCollector{future: future})
}
// getArtifactCollector retrieves the MessageFuture registered for the
-// current agent run. Returns nil when no collector was registered
-// (e.g., tests that stub agentRunner).
+// current agent run. Returns nil when no collector was registered.
func getArtifactCollector(ctx context.Context) react.MessageFuture {
- v := ctx.Value(artifactCollectorKey{})
- if v == nil {
- return nil
- }
- if f, ok := v.(react.MessageFuture); ok {
- return f
+ if collector, ok := ctx.Value(artifactCollectorKey{}).(*artifactCollector); ok {
+ return collector.future
}
return nil
}
@@ -1147,6 +1176,10 @@ func getArtifactCollector(ctx context.Context) react.MessageFuture {
//
// { "_ARTIFACTS": [{ "name": "report.pdf", "url": "https://..." }, ...] }
func collectArtifactsFromToolCalls(ctx context.Context, _ *schema.Message) []artifactEntry {
+ collector, _ := ctx.Value(artifactCollectorKey{}).(*artifactCollector)
+ if collector != nil && len(collector.artifacts) > 0 {
+ return append([]artifactEntry(nil), collector.artifacts...)
+ }
future := getArtifactCollector(ctx)
if future == nil {
return nil
@@ -1183,6 +1216,28 @@ func collectArtifactsFromToolCalls(ctx context.Context, _ *schema.Message) []art
return out
}
+func recordArtifactsFromToolMessage(ctx context.Context, msg *schema.Message) {
+ collector, ok := ctx.Value(artifactCollectorKey{}).(*artifactCollector)
+ if !ok {
+ return
+ }
+ for _, artifact := range extractArtifactsFromToolMessage(msg) {
+ if artifact.Name == "" || artifact.URL == "" {
+ continue
+ }
+ seen := false
+ for _, existing := range collector.artifacts {
+ if existing.URL == artifact.URL {
+ seen = true
+ break
+ }
+ }
+ if !seen {
+ collector.artifacts = append(collector.artifacts, artifact)
+ }
+ }
+}
+
// extractArtifactsFromToolMessage parses the JSON payload of a tool
// response message and returns the `_ARTIFACTS` list. The payload is
// read from msg.Content when it is non-empty; otherwise the first text
@@ -1215,6 +1270,15 @@ func extractArtifactsFromToolMessage(msg *schema.Message) []artifactEntry {
}
name, _ := m["name"].(string)
url, _ := m["url"].(string)
+ if url == "" {
+ if content, ok := m["content_b64"].(string); ok && content != "" {
+ mime, _ := m["mime_type"].(string)
+ if mime == "" {
+ mime = "application/octet-stream"
+ }
+ url = "data:" + mime + ";base64," + content
+ }
+ }
if name == "" || url == "" {
continue
}
diff --git a/internal/agent/component/agent_artifact_test.go b/internal/agent/component/agent_artifact_test.go
index 9f82d7d571..24e5f750c5 100644
--- a/internal/agent/component/agent_artifact_test.go
+++ b/internal/agent/component/agent_artifact_test.go
@@ -138,6 +138,27 @@ func TestAgent_ReActAgent_CollectsArtifactsFromCodeExecTool(t *testing.T) {
}
}
+func TestExtractArtifactsFromToolMessageAcceptsSandboxContent(t *testing.T) {
+ msg := &schema.Message{Role: schema.Tool, Content: `{"_ARTIFACTS":[{"name":"chart.png","mime_type":"image/png","content_b64":"aW1hZ2U="}]}`}
+ got := extractArtifactsFromToolMessage(msg)
+ if len(got) != 1 || got[0].URL != "data:image/png;base64,aW1hZ2U=" {
+ t.Fatalf("got %#v, want sandbox data URL", got)
+ }
+}
+
+func TestArtifactCollectorPreparedByInvokeReceivesRunnerFuture(t *testing.T) {
+ ctx := prepareArtifactCollector(t.Context())
+ ctx = setArtifactCollector(ctx, newSliceFuture(nil))
+ if getArtifactCollector(ctx) == nil {
+ t.Fatal("runner future was not visible to Agent.Invoke")
+ }
+ recordArtifactsFromToolMessage(ctx, &schema.Message{Role: schema.Tool, Content: `{"_ARTIFACTS":[{"name":"chart.png","mime_type":"image/png","content_b64":"aW1hZ2U="}]}`})
+ got := collectArtifactsFromToolCalls(ctx, nil)
+ if len(got) != 1 || got[0].URL != "data:image/png;base64,aW1hZ2U=" {
+ t.Fatalf("got %#v, want streamed sandbox artifact", got)
+ }
+}
+
func drainFutureMessages(t *testing.T, future react.MessageFuture) []*schema.Message {
t.Helper()
var out []*schema.Message
diff --git a/internal/agent/component/agent_test.go b/internal/agent/component/agent_test.go
index 89634f7880..3db5ae3a05 100644
--- a/internal/agent/component/agent_test.go
+++ b/internal/agent/component/agent_test.go
@@ -110,6 +110,17 @@ func TestScanAllStreamForToolCallAllowsDirectAnswer(t *testing.T) {
}
}
+func TestHasCodeExecTool(t *testing.T) {
+ for _, name := range []string{"CodeExec", "code_exec", "execute_code"} {
+ if !hasCodeExecTool([]string{name}) {
+ t.Fatalf("hasCodeExecTool(%q) = false, want true", name)
+ }
+ }
+ if hasCodeExecTool([]string{"Retrieval"}) {
+ t.Fatal("hasCodeExecTool(Retrieval) = true, want false")
+ }
+}
+
type textThenToolCallModel struct {
turn int
boundTools []*schema.ToolInfo
diff --git a/internal/agent/component/production_chain_fixes_test.go b/internal/agent/component/production_chain_fixes_test.go
index 7cad93e019..9be875b30c 100644
--- a/internal/agent/component/production_chain_fixes_test.go
+++ b/internal/agent/component/production_chain_fixes_test.go
@@ -761,8 +761,8 @@ func TestCodeExec_LegacyDSLWrapperPreservesExecutionError(t *testing.T) {
}
out, err := c.Invoke(t.Context(), nil, map[string]any{})
- if err == nil {
- t.Fatal("CodeExec.Invoke: want wrapped execution error, got nil")
+ if err == nil || !strings.Contains(err.Error(), "Container pool is busy") {
+ t.Fatalf("CodeExec.Invoke error = %v, want terminal sandbox error", err)
}
if got, _ := out["_ERROR"].(string); got != "Container pool is busy" {
t.Errorf("CodeExec _ERROR = %v, want sandbox execution error", out["_ERROR"])
diff --git a/internal/agent/tool/code_exec.go b/internal/agent/tool/code_exec.go
index f2336b4f97..72572d8a45 100644
--- a/internal/agent/tool/code_exec.go
+++ b/internal/agent/tool/code_exec.go
@@ -161,6 +161,9 @@ func (c *CodeExecTool) InvokableRun(ctx context.Context, argumentsInJSON string,
zap.Int("script_len", len(req.Script)))
resp, err := client.ExecuteCode(ctx, req)
if err != nil {
+ // Providers return user-code failures as non-zero SandboxResponses.
+ // An error here is a sandbox or transport failure and must stop the
+ // ReAct loop instead of prompting retries against broken infrastructure.
return codeExecStubResult(err.Error()), err
}
out, mErr := codeExecResultJSON(resp)
diff --git a/internal/agent/tool/code_exec_test.go b/internal/agent/tool/code_exec_test.go
index 3028932194..4050a70d59 100644
--- a/internal/agent/tool/code_exec_test.go
+++ b/internal/agent/tool/code_exec_test.go
@@ -81,6 +81,23 @@ func TestCodeExec_AcceptsLangAlias(t *testing.T) {
}
}
+func TestCodeExec_ReturnsSandboxFailureAsTerminalError(t *testing.T) {
+ prev := GetSandboxClient()
+ SetSandboxClient(stubSandbox(func(context.Context, SandboxRequest) (*SandboxResponse, error) {
+ return nil, errors.New("provider unavailable")
+ }))
+ t.Cleanup(func() { SetSandboxClient(prev) })
+
+ out, err := NewCodeExecTool().InvokableRun(t.Context(), `{"language":"python","code":"def main(): pass"}`)
+ if err == nil || !strings.Contains(err.Error(), "provider unavailable") {
+ t.Fatalf("InvokableRun error = %v, want provider unavailable", err)
+ }
+ var got codeExecResult
+ if json.Unmarshal([]byte(out), &got) != nil || !strings.Contains(got.Error, "provider unavailable") {
+ t.Fatalf("result = %s, want error envelope", out)
+ }
+}
+
func TestCodeExec_Info(t *testing.T) {
t.Parallel()
ctx := t.Context()
diff --git a/internal/entity/models/base_model.go b/internal/entity/models/base_model.go
index a16e4c30f9..a642d097ed 100644
--- a/internal/entity/models/base_model.go
+++ b/internal/entity/models/base_model.go
@@ -562,7 +562,11 @@ func buildRequestBody(cfg *ChatConfig, modelName string, messages []Message, str
if cfg.ToolChoice != nil {
toolChoice = *cfg.ToolChoice
}
- reqBody["tool_choice"] = toolChoice
+ if cfg.ToolChoiceValue != nil {
+ reqBody["tool_choice"] = cfg.ToolChoiceValue
+ } else {
+ reqBody["tool_choice"] = toolChoice
+ }
}
}
@@ -614,7 +618,11 @@ func applyChatToolConfig(reqBody map[string]interface{}, chatConfig *ChatConfig)
}
reqBody["tools"] = chatConfig.Tools
if chatConfig.ToolChoice != nil {
- reqBody["tool_choice"] = *chatConfig.ToolChoice
+ if chatConfig.ToolChoiceValue != nil {
+ reqBody["tool_choice"] = chatConfig.ToolChoiceValue
+ } else {
+ reqBody["tool_choice"] = *chatConfig.ToolChoice
+ }
}
}
diff --git a/internal/entity/models/llm.go b/internal/entity/models/llm.go
index d3cdecf3ea..b0617cdd35 100644
--- a/internal/entity/models/llm.go
+++ b/internal/entity/models/llm.go
@@ -208,6 +208,11 @@ func (m *EinoChatModel) Generate(ctx context.Context, msgs []*schema.Message, op
if err != nil {
return nil, err
}
+ if containsToolResult(internal) {
+ choice := "auto"
+ chatCfg.ToolChoice = &choice
+ chatCfg.ToolChoiceValue = nil
+ }
resp, err := m.inner.ModelDriver.ChatWithMessages(ctx, *m.inner.ModelName, internal, m.inner.APIConfig, chatCfg, nil)
if err != nil {
return nil, fmt.Errorf("models: EinoChatModel.Generate(%s): %w", *m.inner.ModelName, err)
@@ -224,6 +229,15 @@ func (m *EinoChatModel) Generate(ctx context.Context, msgs []*schema.Message, op
return fromInternalResponse(resp), nil
}
+func containsToolResult(messages []Message) bool {
+ for _, message := range messages {
+ if message.Role == "tool" {
+ return true
+ }
+ }
+ return false
+}
+
func (m *EinoChatModel) chatConfigForGenerate() (*ChatConfig, error) {
if len(m.tools) == 0 {
return m.chatCfg, nil
@@ -239,7 +253,25 @@ func (m *EinoChatModel) chatConfigForGenerate() (*ChatConfig, error) {
}
cfg.Tools = tools
choice := "auto"
+ for _, tool := range m.tools {
+ if tool != nil && tool.Name == "execute_code" {
+ // MiniMax may answer with prose instead of emitting the callable
+ // CodeExec request. Require one tool dispatch for code-exec agents;
+ // the subsequent ReAct turn remains free to produce the final text.
+ choice = "required"
+ break
+ }
+ }
cfg.ToolChoice = &choice
+ for _, tool := range m.tools {
+ if tool != nil && tool.Name == "execute_code" {
+ cfg.ToolChoiceValue = map[string]any{
+ "type": "function",
+ "function": map[string]any{"name": "execute_code"},
+ }
+ break
+ }
+ }
return cfg, nil
}
@@ -338,10 +370,32 @@ func (m *EinoChatModel) Stream(ctx context.Context, msgs []*schema.Message, opts
return nil, fmt.Errorf("models: EinoChatModel: nil model name")
}
internalMessage := toInternalMessages(msgs)
+ // Some OpenAI-compatible providers (including the configured MiniMax
+ // endpoint) stream tool intent as ordinary prose. Use the provider's
+ // non-streaming parser for tool-bound turns so structured tool_calls are
+ // preserved; ReAct still streams the final answer turn normally.
+ if len(m.tools) > 0 && !containsToolResult(internalMessage) {
+ msg, err := m.Generate(ctx, msgs, opts...)
+ if err != nil {
+ return nil, err
+ }
+ sr, sw := schema.Pipe[*schema.Message](1)
+ if !sw.Send(msg, nil) {
+ sw.Close()
+ return sr, nil
+ }
+ sw.Close()
+ return sr, nil
+ }
chatCfg, err := m.chatConfigForGenerate()
if err != nil {
return nil, err
}
+ if containsToolResult(internalMessage) {
+ choice := "auto"
+ chatCfg.ToolChoice = &choice
+ chatCfg.ToolChoiceValue = nil
+ }
sr, sw := schema.Pipe[*schema.Message](1)
var sendMu sync.Mutex
diff --git a/internal/entity/models/llm_test.go b/internal/entity/models/llm_test.go
index bd2145847f..a3a150b33b 100644
--- a/internal/entity/models/llm_test.go
+++ b/internal/entity/models/llm_test.go
@@ -10,6 +10,46 @@ import (
"github.com/cloudwego/eino/schema"
)
+func TestEinoChatModelRequiresExecuteCodeTool(t *testing.T) {
+ name := "chat"
+ base := NewChatModel(&streamSentinelDriver{}, &name, &APIConfig{})
+ model := NewEinoChatModel(base, nil)
+ bound, err := model.WithTools([]*schema.ToolInfo{{Name: "execute_code"}})
+ if err != nil {
+ t.Fatal(err)
+ }
+ cfg, err := bound.(*EinoChatModel).chatConfigForGenerate()
+ if err != nil {
+ t.Fatal(err)
+ }
+ if cfg.ToolChoice == nil || *cfg.ToolChoice != "required" {
+ t.Fatalf("ToolChoice = %v, want required", cfg.ToolChoice)
+ }
+ choice, ok := cfg.ToolChoiceValue.(map[string]any)
+ if !ok || choice["type"] != "function" {
+ t.Fatalf("ToolChoiceValue = %#v, want named function choice", cfg.ToolChoiceValue)
+ }
+}
+
+func TestEinoChatModelAllowsFinalAnswerAfterToolResult(t *testing.T) {
+ name := "chat"
+ driver := &captureToolDriver{resp: &ChatResponse{}}
+ model := NewEinoChatModel(NewChatModel(driver, &name, &APIConfig{}), nil)
+ bound, err := model.WithTools([]*schema.ToolInfo{{Name: "execute_code"}})
+ if err != nil {
+ t.Fatal(err)
+ }
+ if _, err := bound.Generate(t.Context(), []*schema.Message{
+ schema.UserMessage("make a chart"),
+ {Role: schema.Tool, ToolCallID: "call-1", Content: "done"},
+ }); err != nil {
+ t.Fatal(err)
+ }
+ if driver.lastConfig.ToolChoice == nil || *driver.lastConfig.ToolChoice != "auto" || driver.lastConfig.ToolChoiceValue != nil {
+ t.Fatalf("tool result choice = %#v / %#v, want auto / nil", driver.lastConfig.ToolChoice, driver.lastConfig.ToolChoiceValue)
+ }
+}
+
func TestEinoChatModelStreamFiltersDoneSentinel(t *testing.T) {
modelName := "chat"
driver := &streamSentinelDriver{captureToolDriver: &captureToolDriver{}}
@@ -189,6 +229,12 @@ func TestEinoChatModelStreamWithToolsStreamsFinalAnswer(t *testing.T) {
if msg == nil || msg.Content != answer {
t.Fatalf("stream message = %#v, want final answer content", msg)
}
+ if driver.streamCalls != 1 || driver.generateCalls != 0 {
+ t.Fatalf("stream/generate calls = %d/%d, want 1/0", driver.streamCalls, driver.generateCalls)
+ }
+ if driver.lastConfig.ToolChoice == nil || *driver.lastConfig.ToolChoice != "auto" || driver.lastConfig.ToolChoiceValue != nil {
+ t.Fatalf("tool result choice = %#v / %#v, want auto / nil", driver.lastConfig.ToolChoice, driver.lastConfig.ToolChoiceValue)
+ }
}
func TestToInternalMessagesPreservesToolMessages(t *testing.T) {
@@ -222,8 +268,10 @@ func TestToInternalMessagesPreservesToolMessages(t *testing.T) {
}
type captureToolDriver struct {
- resp *ChatResponse
- lastConfig *ChatConfig
+ resp *ChatResponse
+ lastConfig *ChatConfig
+ generateCalls int
+ streamCalls int
}
type streamSentinelDriver struct {
@@ -247,10 +295,12 @@ func (d *captureToolDriver) NewInstance(baseURL map[string]string) ModelDriver {
func (d *captureToolDriver) Name() string { return "capture" }
func (d *captureToolDriver) ChatWithMessages(ctx context.Context, _ string, _ []Message, _ *APIConfig, cfg *ChatConfig, modelUsage *common.ModelUsage) (*ChatResponse, error) {
d.lastConfig = cfg
+ d.generateCalls++
return d.resp, nil
}
func (d *captureToolDriver) ChatStreamlyWithSender(ctx context.Context, _ string, _ []Message, _ *APIConfig, cfg *ChatConfig, _ *common.ModelUsage, sender func(*string, *string) error) error {
d.lastConfig = cfg
+ d.streamCalls++
if d.resp == nil {
return nil
}
diff --git a/internal/entity/models/types.go b/internal/entity/models/types.go
index 5541d0e004..ebae5789a3 100644
--- a/internal/entity/models/types.go
+++ b/internal/entity/models/types.go
@@ -185,6 +185,7 @@ type ChatConfig struct {
Verbosity *string
Tools interface{} `json:"tools,omitempty"`
ToolChoice *string `json:"tool_choice,omitempty"`
+ ToolChoiceValue any `json:"-"`
ToolCallsResult *[]map[string]interface{} `json:"-"`
// UsageResult receives the token usage extracted from the final
// streaming chunk when stream_options.include_usage is true.