Skip to content

armgit / ARTEX

Public Imported from GitHub
0

ARTEX - AI autonomous penetration testing system (English translation; upstream: Autumn-27/ARTEX, AGPL-3.0)

feat(llm): 每个 LLM 配置支持流式/非流式切换(默认流式)

Autumn-27ab558d69bf34
diff --git a/agent/capture_usage_test.go b/agent/capture_usage_test.go
index c76f79c..5b6389c 100644
--- a/agent/capture_usage_test.go
+++ b/agent/capture_usage_test.go
@@ -19,6 +19,17 @@ func (p captureUsageProvider) Stream(ctx context.Context, _ llm.CompletionReques
return func(yield func(llm.StreamEvent, error) bool) { p.stream(ctx, yield) }
}
+func (p captureUsageProvider) Complete(ctx context.Context, req llm.CompletionRequest) (llm.Message, string, llm.Usage, error) {
+ acc := llm.NewAccumulator()
+ for ev, err := range p.Stream(ctx, req) {
+ if err != nil {
+ return llm.Message{}, "", llm.Usage{}, err
+ }
+ acc.Add(ev)
+ }
+ return acc.Message(), acc.StopReason, acc.Usage, nil
+}
+
func TestCaptureRunPersistsUsageOnProviderFailure(t *testing.T) {
wantErr := errors.New("provider failed after reporting usage")
provider := captureUsageProvider{stream: func(_ context.Context, yield func(llm.StreamEvent, error) bool) {
diff --git a/agent/chat.go b/agent/chat.go
index 41be93b..0f38582 100644
--- a/agent/chat.go
+++ b/agent/chat.go
@@ -6,13 +6,13 @@ import (
"path/filepath"
"time"
+ "github.com/Autumn-27/artex/db"
+ "github.com/Autumn-27/artex/guard"
"github.com/Autumn-27/norma/agentcore"
"github.com/Autumn-27/norma/llm"
"github.com/Autumn-27/norma/permission"
actool "github.com/Autumn-27/norma/tool"
"github.com/Autumn-27/norma/transcript"
- "github.com/Autumn-27/artex/db"
- "github.com/Autumn-27/artex/guard"
)
// ChatAgent is the generic, task-independent conversational runner behind the chat
@@ -22,21 +22,28 @@ import (
// LS/Glob/Grep) plus whatever skills/MCP the key is made visible; NO pentest
// graph/task context is injected (that stays exclusive to MainAgent).
type ChatAgent struct {
- prov llm.Provider
- model string
- workDir string
- tx *transcript.Store
- window int
- proxyAddr string
- proxyCACert string
- webSearch WebSearchOpts
- guard *guard.Guard // optional; nil disables intercept hooks for chat
+ prov llm.Provider
+ model string
+ workDir string
+ tx *transcript.Store
+ window int
+ proxyAddr string
+ proxyCACert string
+ webSearch WebSearchOpts
+ guard *guard.Guard // optional; nil disables intercept hooks for chat
+ nonStreamingFn func() bool // resolver: use non-streaming (Complete) path? (nil = streaming)
}
func NewChatAgent(prov llm.Provider, model, workDir string, tx *transcript.Store, window int) *ChatAgent {
return &ChatAgent{prov: prov, model: model, workDir: workDir, tx: tx, window: window}
}
+// SetNonStreaming wires a resolver deciding whether chat runs use the
+// non-streaming model path (true = non-streaming). nil/unset = streaming.
+func (c *ChatAgent) SetNonStreaming(fn func() bool) { c.nonStreamingFn = fn }
+
+func (c *ChatAgent) nonStreaming() bool { return c.nonStreamingFn != nil && c.nonStreamingFn() }
+
// SetProxy points the chat agent's WebFetch/Bash at the recording proxy plus the
// CA cert it trusts (empty addr = direct). Kept for parity with the other agents.
func (c *ChatAgent) SetProxy(addr, caCert string) { c.proxyAddr, c.proxyCACert = addr, caCert }
@@ -115,18 +122,19 @@ func (c *ChatAgent) Chat(ctx context.Context, agentKey, sessionID, message strin
BraveSearchAPIKey: ws.BraveKey,
TavilySearchAPIKey: ws.TavilyKey,
WebSearchProxy: ws.Proxy,
- BashEnv: proxyEnv(c.proxyAddr, c.proxyCACert), // Bash 子命令默认走代理+信任 CA
- WorkingDir: sessionWorkDir,
- MaxTurns: maxTurns,
- MaxDuration: maxDuration,
- Compaction: compactionConfig(c.window),
- Todos: actool.NewTodoStore(),
+ BashEnv: proxyEnv(c.proxyAddr, c.proxyCACert), // Bash 子命令默认走代理+信任 CA
+ WorkingDir: sessionWorkDir,
+ MaxTurns: maxTurns,
+ MaxDuration: maxDuration,
+ Compaction: compactionConfig(c.window),
+ Todos: actool.NewTodoStore(),
// large tool output spills to cmd-output/ under the session dir.
// 截断上限用 SDK 默认(tool.Capture 的 30000 字符)。
ToolOutputDir: filepath.Join(sessionWorkDir, "cmd-output"),
// 命中预算(步数)→ SDK 跑收尾:输出一句总结。Prompt 与收尾轮数按本 agent key 后台可编辑
// (自定义 agent 各自一份;留空/0 用通用默认:10 轮)。
- Settlement: wrapupSettlement(agentKey, nil),
+ Settlement: wrapupSettlement(agentKey, nil),
+ NonStreaming: c.nonStreaming(), // 该 profile 选非流式时走 Provider.Complete
}
if c.guard != nil {
opts.Hooks = c.guard.Hooks()
diff --git a/agent/goals.go b/agent/goals.go
index 388515a..b98e9fe 100644
--- a/agent/goals.go
+++ b/agent/goals.go
@@ -98,13 +98,13 @@ func DecomposeGoals(ctx context.Context, prov llm.Provider, dataDir, goalText, d
if prov == nil {
return nil
}
- return DecomposeGoalsWithProvider(ctx, prov, dataDir, goalText, desc, as, ts, taskID, emit)
+ return DecomposeGoalsWithProvider(ctx, prov, dataDir, goalText, desc, as, ts, taskID, false, emit)
}
// DecomposeGoalsWithProvider is the task-runtime variant used when a task has an
// ordered provider chain. It preserves the same tools and write behavior while
// letting the caller own provider selection/failover.
-func DecomposeGoalsWithProvider(ctx context.Context, prov llm.Provider, dataDir, goalText, desc string, as *db.AssetStore, ts *db.ExplorationStore, taskID int64, emit func(db.Activity)) []GoalSpec {
+func DecomposeGoalsWithProvider(ctx context.Context, prov llm.Provider, dataDir, goalText, desc string, as *db.AssetStore, ts *db.ExplorationStore, taskID int64, nonStreaming bool, emit func(db.Activity)) []GoalSpec {
if prov == nil {
return nil
}
@@ -145,7 +145,8 @@ func DecomposeGoalsWithProvider(ctx context.Context, prov llm.Provider, dataDir,
PermissionMode: acperm.ModeBypass,
DisableBackgroundTasks: true,
// 3 步(抽约束 → 登记范围 → 拆目标)各需一次工具调用,给足回合避免收尾前漏调 set_goals。
- MaxTurns: 8,
+ MaxTurns: 8,
+ NonStreaming: nonStreaming, // 该 profile 选非流式时走 Provider.Complete
}, userMsg, captureEmit)
// set_goals persisted the goals directly; read them back so the caller sees what
// was written (empty slice ⇒ the LLM produced nothing ⇒ caller falls back).
diff --git a/agent/mainagent.go b/agent/mainagent.go
index 99531df..9c40eca 100644
--- a/agent/mainagent.go
+++ b/agent/mainagent.go
@@ -17,19 +17,26 @@ import (
// (→planner) or direct high-priority intents (→frontier). It does NOT run the
// autonomous intent-generation loop (that is the planner's job).
type MainAgent struct {
- prov llm.Provider
- model string
- tx *transcript.Store // raw LLM conversation persistence (nil = off)
- window int // context window in tokens (for compaction)
- windowFn func() int // optional dynamic task-chain minimum
- maxTurns int // max agent turns per run (0 = unlimited)
- proxyAddr string // recording proxy for WebFetch (empty = direct)
- proxyCACert string // recording proxy's CA cert path (HTTPS verify)
- webSearch WebSearchOpts // web_search tool backend selection (off by default)
- workDir string // shared work dir (surfaced in prompt as artifact-output target)
- steerWork func(intentID int64, msg string) error // engine callback: steer a running work (nil = off)
+ prov llm.Provider
+ model string
+ tx *transcript.Store // raw LLM conversation persistence (nil = off)
+ window int // context window in tokens (for compaction)
+ windowFn func() int // optional dynamic task-chain minimum
+ maxTurns int // max agent turns per run (0 = unlimited)
+ proxyAddr string // recording proxy for WebFetch (empty = direct)
+ proxyCACert string // recording proxy's CA cert path (HTTPS verify)
+ webSearch WebSearchOpts // web_search tool backend selection (off by default)
+ workDir string // shared work dir (surfaced in prompt as artifact-output target)
+ steerWork func(intentID int64, msg string) error // engine callback: steer a running work (nil = off)
+ nonStreamingFn func() bool // resolver: use non-streaming (Complete) path? (nil = streaming)
}
+// SetNonStreaming wires a resolver deciding whether runs use the non-streaming
+// model path (true = non-streaming). nil/unset = streaming (default).
+func (m *MainAgent) SetNonStreaming(fn func() bool) { m.nonStreamingFn = fn }
+
+func (m *MainAgent) nonStreaming() bool { return m.nonStreamingFn != nil && m.nonStreamingFn() }
+
func NewMainAgent(prov llm.Provider, model, workDir string, tx *transcript.Store, window, maxTurns int) *MainAgent {
return &MainAgent{prov: prov, model: model, workDir: workDir, tx: tx, window: window, maxTurns: maxTurns}
}
@@ -128,7 +135,8 @@ func (m *MainAgent) Chat(ctx context.Context, taskID int64, as *db.AssetStore, t
Compaction: compactionConfig(m.compactionWindow()), // long chats stay within the window
Todos: actool.NewTodoStore(), // 会话级临时待办(TodoWrite),纯规划用,退出即丢
// 命中预算(步数)→ SDK 跑收尾:向用户输出一句进展总结。Prompt 与收尾轮数可后台编辑(默认 10 轮)。
- Settlement: wrapupSettlement("mainagent", nil),
+ Settlement: wrapupSettlement("mainagent", nil),
+ NonStreaming: m.nonStreaming(), // 该 profile 选非流式时走 Provider.Complete
}
if m.tx != nil { // persist raw human↔AI conversation; one accumulating file per task
opts.Transcript = m.tx
diff --git a/agent/planner.go b/agent/planner.go
index dfacc91..01c1bb8 100644
--- a/agent/planner.go
+++ b/agent/planner.go
@@ -20,19 +20,20 @@ import (
// assets, judges whether the task goal is met, and emits 0..N exploration intents
// into the frontier. It is the sole intent generator.
type Planner struct {
- prov llm.Provider
- model string
- tx *transcript.Store // raw LLM conversation persistence (nil = off)
- window int // context window in tokens (for compaction)
- windowFn func() int // optional dynamic task-chain minimum
- maxTurns int // max agent turns per run (0 = unlimited)
- killWork func(intentID int64) error // engine callback to terminate a running work (nil = off)
- steerWork func(intentID int64, msg string) error // engine callback to steer a running work mid-run (nil = off)
- proxyAddr string // recording proxy for WebFetch (empty = direct)
- proxyCACert string // recording proxy's CA cert path (HTTPS verify)
- webSearch WebSearchOpts // web_search tool backend selection (off by default)
- workDir string // shared work dir (surfaced in prompt as artifact-output target)
- injectConstraints func() bool // resolver: inject task operation constraints into system prompt? (nil = yes)
+ prov llm.Provider
+ model string
+ tx *transcript.Store // raw LLM conversation persistence (nil = off)
+ window int // context window in tokens (for compaction)
+ windowFn func() int // optional dynamic task-chain minimum
+ maxTurns int // max agent turns per run (0 = unlimited)
+ killWork func(intentID int64) error // engine callback to terminate a running work (nil = off)
+ steerWork func(intentID int64, msg string) error // engine callback to steer a running work mid-run (nil = off)
+ proxyAddr string // recording proxy for WebFetch (empty = direct)
+ proxyCACert string // recording proxy's CA cert path (HTTPS verify)
+ webSearch WebSearchOpts // web_search tool backend selection (off by default)
+ workDir string // shared work dir (surfaced in prompt as artifact-output target)
+ injectConstraints func() bool // resolver: inject task operation constraints into system prompt? (nil = yes)
+ nonStreamingFn func() bool // resolver: use non-streaming (Complete) path? (nil = streaming)
// todos keeps ONE plan-scratchpad per task (keyed by exploration id) so the
// planner's multi-step plan survives across wake-ups — each Plan() is a fresh
@@ -48,6 +49,12 @@ func NewPlanner(prov llm.Provider, model, workDir string, tx *transcript.Store,
func (p *Planner) SetCompactionWindowResolver(fn func() int) { p.windowFn = fn }
+// SetNonStreaming wires a resolver deciding whether runs use the non-streaming
+// model path (true = non-streaming). nil/unset = streaming (default).
+func (p *Planner) SetNonStreaming(fn func() bool) { p.nonStreamingFn = fn }
+
+func (p *Planner) nonStreaming() bool { return p.nonStreamingFn != nil && p.nonStreamingFn() }
+
func (p *Planner) compactionWindow() int {
if p.windowFn != nil {
return p.windowFn()
@@ -353,7 +360,8 @@ func (p *Planner) Plan(ctx context.Context, taskID int64, as *db.AssetStore, ts
// 命中【本轮】步数预算→ SDK 跑收尾:把本轮已想清楚的结论落地(该派的 add_intent、
// 能证的 prove_goal、串行链记 TodoWrite),而非停止规划——planner 之后仍会被反复唤醒。
// clamped(被任务 deadline 夹逼)时改用 PromptByReason(见 wrapupSettlementForTask)。
- Settlement: settle,
+ Settlement: settle,
+ NonStreaming: p.nonStreaming(), // 该 profile 选非流式时走 Provider.Complete
}
if p.tx != nil { // persist raw LLM conversation; one accumulating file per task's planner
opts.Transcript = p.tx
diff --git a/agent/provider.go b/agent/provider.go
index 3a576e7..f14a37d 100644
--- a/agent/provider.go
+++ b/agent/provider.go
@@ -52,6 +52,11 @@ type Config struct {
// "" = 不发送(默认); "low"/"medium"/"high"/"xhigh"/"max" = 对应强度.
// OpenAI 映射为顶层 reasoning_effort;Anthropic 映射为 output_config.effort.
ReasoningEffort string
+ // Stream 控制该 profile 是否使用流式(SSE)接口。true(默认)= 流式;false = 真·
+ // 非流式(发 stream:false,一次性拿完整 JSON,走 Provider.Complete)。非流式可绕开
+ // 某些网关糟糕的 SSE 实现(空帧、思考字段丢帧),代价是失去运行中的实时进度/实时
+ // token 计数。映射为 agentcore.Options.NonStreaming = !Stream。
+ Stream bool
}
// compaction window resolution bounds (in K tokens). Below the floor the
@@ -117,6 +122,8 @@ func FromEnv() (Config, bool) {
BaseURL: os.Getenv("ARTEX_LLM_BASE_URL"),
Model: os.Getenv("ARTEX_LLM_MODEL"),
Proxy: strings.TrimSpace(os.Getenv("ARTEX_LLM_PROXY")),
+ // 默认流式;ARTEX_LLM_STREAM=false/0/off 显式关闭走非流式。
+ Stream: !isFalsy(os.Getenv("ARTEX_LLM_STREAM")),
}
switch prov {
case "openai":
@@ -148,6 +155,7 @@ func ConfigFrom(provider, model, baseURL, apiKey, proxy string) Config {
BaseURL: strings.TrimRight(strings.TrimSpace(baseURL), "/"),
APIKey: strings.TrimSpace(apiKey),
Proxy: strings.TrimSpace(proxy),
+ Stream: true, // 默认流式;调用方按 profile 覆盖
}
switch strings.TrimSpace(provider) {
case "openai":
@@ -168,6 +176,16 @@ func ConfigFrom(provider, model, baseURL, apiKey, proxy string) Config {
return c
}
+// isFalsy reports whether an env-var string explicitly requests "off". Empty or
+// unrecognized → false (so an unset var keeps the streaming default).
+func isFalsy(s string) bool {
+ switch strings.ToLower(strings.TrimSpace(s)) {
+ case "0", "false", "off", "no":
+ return true
+ }
+ return false
+}
+
// Provider returns the short provider name ("anthropic"/"openai").
func (c Config) Provider() string {
if c.Format == llm.FormatOpenAI {
@@ -343,6 +361,7 @@ func TestConnection(ctx context.Context, c Config) (time.Duration, error) {
PermissionMode: acperm.ModeBypass,
MaxTurns: 1,
MaxTokens: 8192,
+ NonStreaming: !c.Stream, // 用该 profile 的真实收发模式做连接测试
}, "ping")
return time.Since(start), err
}
diff --git a/agent/worker.go b/agent/worker.go
index f2e5ce8..b6c6e8c 100644
--- a/agent/worker.go
+++ b/agent/worker.go
@@ -62,8 +62,19 @@ type Worker struct {
// injected into the worker system prompt. Read per run so the settings toggle
// takes effect without rebuilding the agent. nil = inject (default).
injectConstraints func() bool
+ // nonStreamingFn resolves whether this run uses the non-streaming (Complete)
+ // path. Read per run so a profile/task toggle takes effect without rebuilding
+ // the agent. nil = streaming (default).
+ nonStreamingFn func() bool
}
+// SetNonStreaming wires a resolver deciding whether runs use the non-streaming
+// model path (true = non-streaming). nil/unset = streaming (default). Read per
+// run so a profile or task-chain toggle takes effect without rebuilding.
+func (w *Worker) SetNonStreaming(fn func() bool) { w.nonStreamingFn = fn }
+
+func (w *Worker) nonStreaming() bool { return w.nonStreamingFn != nil && w.nonStreamingFn() }
+
// SetConstraintInject wires a resolver deciding whether this task's operation
// constraints get injected into the worker system prompt. nil = inject (default).
func (w *Worker) SetConstraintInject(fn func() bool) { w.injectConstraints = fn }
@@ -337,6 +348,7 @@ func (w *Worker) Execute(ctx context.Context, name string, taskID int64, as *db.
ToolOutputDir: cmdOutDir(runDir),
Compaction: compactionConfig(w.compactionWindow()), // long tool-heavy runs stay within the window
Todos: actool.NewTodoStore(), // 会话级临时待办(TodoWrite),纯规划用,退出即丢
+ NonStreaming: w.nonStreaming(), // 该 profile 选非流式时走 Provider.Complete
}
if hooks != nil { // typed-nil guard: only set when concrete (avoids harness panic)
opts.Hooks = hooks
diff --git a/db/config.go b/db/config.go
index 40684d6..410994a 100644
--- a/db/config.go
+++ b/db/config.go
@@ -39,18 +39,23 @@ type LLMProfile struct {
// usable when an agent/task binds it explicitly, it just never gets picked up
// as a fallback target.
PoolExclude bool `json:"pool_exclude"`
+ // Streaming selects the wire protocol: true (default) = streaming (SSE);
+ // false = real non-streaming (stream:false, single JSON response via
+ // Provider.Complete). Non-streaming sidesteps flaky gateway SSE at the cost of
+ // live in-run progress. Maps to agent.Config.Stream.
+ Streaming bool `json:"streaming"`
}
// profileCols is the read column list (hint variant, no api key) shared by the
// list query; profileColsKey is the same with api_key for the single-row loads.
-const profileCols = `id,name,format,COALESCE(base_url,''),COALESCE(proxy,''),model,COALESCE(api_key_hint,''),rate_per_second,rate_per_minute,context_window_k,COALESCE(reasoning_effort,''),is_default,priority,pool_exclude,COALESCE(thinking_type,'')`
-const profileColsKey = `id,name,format,COALESCE(base_url,''),COALESCE(proxy,''),model,COALESCE(api_key,''),rate_per_second,rate_per_minute,context_window_k,COALESCE(reasoning_effort,''),is_default,priority,pool_exclude,COALESCE(thinking_type,'')`
+const profileCols = `id,name,format,COALESCE(base_url,''),COALESCE(proxy,''),model,COALESCE(api_key_hint,''),rate_per_second,rate_per_minute,context_window_k,COALESCE(reasoning_effort,''),is_default,priority,pool_exclude,COALESCE(thinking_type,''),COALESCE(streaming,true)`
+const profileColsKey = `id,name,format,COALESCE(base_url,''),COALESCE(proxy,''),model,COALESCE(api_key,''),rate_per_second,rate_per_minute,context_window_k,COALESCE(reasoning_effort,''),is_default,priority,pool_exclude,COALESCE(thinking_type,''),COALESCE(streaming,true)`
// scanProfile reads one row in the profileCols / profileColsKey column order. The
// 7th column lands in APIKeyHint or APIKey depending on which list the caller used.
func scanProfile(sc interface{ Scan(...any) error }, into *string, p *LLMProfile) error {
return sc.Scan(&p.ID, &p.Name, &p.Format, &p.BaseURL, &p.Proxy, &p.Model, into,
- &p.RatePerSecond, &p.RatePerMinute, &p.ContextWindowK, &p.ReasoningEffort, &p.IsDefault, &p.Priority, &p.PoolExclude, &p.ThinkingType)
+ &p.RatePerSecond, &p.RatePerMinute, &p.ContextWindowK, &p.ReasoningEffort, &p.IsDefault, &p.Priority, &p.PoolExclude, &p.ThinkingType, &p.Streaming)
}
func (d *DB) ListProfiles() ([]*LLMProfile, error) {
@@ -123,18 +128,18 @@ func (d *DB) SaveProfile(p *LLMProfile) (int64, error) {
}
if p.ID == 0 {
var id int64
- err := d.QueryRow(`INSERT INTO llm_profiles(name,format,base_url,proxy,model,api_key,api_key_hint,rate_per_second,rate_per_minute,context_window_k,reasoning_effort,priority,pool_exclude,thinking_type)
-VALUES ($1,$2,NULLIF($3,''),NULLIF($4,''),$5,NULLIF($6,''),NULLIF($7,''),$8,$9,$10,$11,$12,$13,$14) RETURNING id`,
- p.Name, p.Format, p.BaseURL, p.Proxy, p.Model, p.APIKey, hint, p.RatePerSecond, p.RatePerMinute, p.ContextWindowK, p.ReasoningEffort, p.Priority, p.PoolExclude, p.ThinkingType).Scan(&id)
+ err := d.QueryRow(`INSERT INTO llm_profiles(name,format,base_url,proxy,model,api_key,api_key_hint,rate_per_second,rate_per_minute,context_window_k,reasoning_effort,priority,pool_exclude,thinking_type,streaming)
+VALUES ($1,$2,NULLIF($3,''),NULLIF($4,''),$5,NULLIF($6,''),NULLIF($7,''),$8,$9,$10,$11,$12,$13,$14,$15) RETURNING id`,
+ p.Name, p.Format, p.BaseURL, p.Proxy, p.Model, p.APIKey, hint, p.RatePerSecond, p.RatePerMinute, p.ContextWindowK, p.ReasoningEffort, p.Priority, p.PoolExclude, p.ThinkingType, p.Streaming).Scan(&id)
return id, err
}
if p.APIKey == "" {
- _, err := d.Exec(`UPDATE llm_profiles SET name=$1,format=$2,base_url=NULLIF($3,''),proxy=NULLIF($4,''),model=$5,rate_per_second=$6,rate_per_minute=$7,context_window_k=$8,reasoning_effort=$9,priority=$10,pool_exclude=$11,thinking_type=$12 WHERE id=$13`,
- p.Name, p.Format, p.BaseURL, p.Proxy, p.Model, p.RatePerSecond, p.RatePerMinute, p.ContextWindowK, p.ReasoningEffort, p.Priority, p.PoolExclude, p.ThinkingType, p.ID)
+ _, err := d.Exec(`UPDATE llm_profiles SET name=$1,format=$2,base_url=NULLIF($3,''),proxy=NULLIF($4,''),model=$5,rate_per_second=$6,rate_per_minute=$7,context_window_k=$8,reasoning_effort=$9,priority=$10,pool_exclude=$11,thinking_type=$12,streaming=$13 WHERE id=$14`,
+ p.Name, p.Format, p.BaseURL, p.Proxy, p.Model, p.RatePerSecond, p.RatePerMinute, p.ContextWindowK, p.ReasoningEffort, p.Priority, p.PoolExclude, p.ThinkingType, p.Streaming, p.ID)
return p.ID, err
}
- _, err := d.Exec(`UPDATE llm_profiles SET name=$1,format=$2,base_url=NULLIF($3,''),proxy=NULLIF($4,''),model=$5,api_key=$6,api_key_hint=$7,rate_per_second=$8,rate_per_minute=$9,context_window_k=$10,reasoning_effort=$11,priority=$12,pool_exclude=$13,thinking_type=$14 WHERE id=$15`,
- p.Name, p.Format, p.BaseURL, p.Proxy, p.Model, p.APIKey, hint, p.RatePerSecond, p.RatePerMinute, p.ContextWindowK, p.ReasoningEffort, p.Priority, p.PoolExclude, p.ThinkingType, p.ID)
+ _, err := d.Exec(`UPDATE llm_profiles SET name=$1,format=$2,base_url=NULLIF($3,''),proxy=NULLIF($4,''),model=$5,api_key=$6,api_key_hint=$7,rate_per_second=$8,rate_per_minute=$9,context_window_k=$10,reasoning_effort=$11,priority=$12,pool_exclude=$13,thinking_type=$14,streaming=$15 WHERE id=$16`,
+ p.Name, p.Format, p.BaseURL, p.Proxy, p.Model, p.APIKey, hint, p.RatePerSecond, p.RatePerMinute, p.ContextWindowK, p.ReasoningEffort, p.Priority, p.PoolExclude, p.ThinkingType, p.Streaming, p.ID)
return p.ID, err
}
diff --git a/db/schema.sql b/db/schema.sql
index b1a53fe..8875d83 100644
--- a/db/schema.sql
+++ b/db/schema.sql
@@ -303,6 +303,8 @@ CREATE TABLE IF NOT EXISTS llm_profiles (
-- pool_exclude true=不作为故障转移目标(仍可被 agent/任务显式绑定使用)。
priority INTEGER NOT NULL DEFAULT 0,
pool_exclude BOOLEAN NOT NULL DEFAULT false,
+ -- streaming=true(默认)走流式 SSE;false 走真·非流式(stream:false,一次性 JSON)。
+ streaming BOOLEAN NOT NULL DEFAULT true,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
@@ -313,6 +315,8 @@ CREATE TRIGGER trg_llm_upd BEFORE UPDATE ON llm_profiles
-- 轮询顺位/排除标记;补旧库。默认 0 / false = 全部配置都参与轮询。
ALTER TABLE llm_profiles ADD COLUMN IF NOT EXISTS priority INTEGER NOT NULL DEFAULT 0;
ALTER TABLE llm_profiles ADD COLUMN IF NOT EXISTS pool_exclude BOOLEAN NOT NULL DEFAULT false;
+-- 流式开关;补旧库。默认 true = 保持既有的流式行为,旧配置无感升级。
+ALTER TABLE llm_profiles ADD COLUMN IF NOT EXISTS streaming BOOLEAN NOT NULL DEFAULT true;
-- 思考开关字段 thinking_type,从旧的单一 reasoning_effort 语义一次性拆分而来。
-- schema.sql 每次启动都执行,故迁移必须只跑一次:仅当该列尚不存在时才回填,
diff --git a/go.mod b/go.mod
index d56340a..481403a 100644
--- a/go.mod
+++ b/go.mod
@@ -3,7 +3,7 @@ module github.com/Autumn-27/artex
go 1.26.3
require (
- github.com/Autumn-27/norma v0.2.9
+ github.com/Autumn-27/norma v0.3.0
github.com/golang-jwt/jwt/v5 v5.3.1
github.com/jackc/pgx/v5 v5.10.0
github.com/lqqyt2423/go-mitmproxy v1.9.2
diff --git a/go.sum b/go.sum
index 835017c..06acf18 100644
--- a/go.sum
+++ b/go.sum
@@ -1,5 +1,5 @@
-github.com/Autumn-27/norma v0.2.9 h1:VTt8Oe7XoiV85Dc+QOPu+zQ7IykQRm2aDTEbLEzbBe4=
-github.com/Autumn-27/norma v0.2.9/go.mod h1:BudWrf7iHl96FgHqFr/HwN3mKYmGjJDt+xL2ZHXfFZw=
+github.com/Autumn-27/norma v0.3.0 h1:nbaoeLj65ElFwWdy44U0QCXPe0idaw8tdyyuU/Sdbuw=
+github.com/Autumn-27/norma v0.3.0/go.mod h1:BudWrf7iHl96FgHqFr/HwN3mKYmGjJDt+xL2ZHXfFZw=
github.com/andybalholm/brotli v1.2.1 h1:R+f5xP285VArJDRgowrfb9DqL18yVK0gKAW/F+eTWro=
github.com/andybalholm/brotli v1.2.1/go.mod h1:rzTDkvFWvIrjDXZHkuS16NPggd91W3kUSvPlQ1pLaKY=
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 h1:DklsrG3dyBCFEj5IhUbnKptjxatkF07cF2ak3yi77so=
diff --git a/llmpool/pool.go b/llmpool/pool.go
index 30b3665..be0ddff 100644
--- a/llmpool/pool.go
+++ b/llmpool/pool.go
@@ -122,6 +122,44 @@ func (p *Pool) Stream(ctx context.Context, req llm.CompletionRequest) iter.Seq2[
}
}
+// Complete implements llm.Provider with failover for non-streaming calls. A
+// non-streaming request is atomic — it never delivers partial output — so every
+// failover-eligible failure lands in the safe window and the next member can be
+// tried without risk of duplicated output. Mirrors Stream's health-tripping and
+// chain-exhaustion behavior.
+func (p *Pool) Complete(ctx context.Context, req llm.CompletionRequest) (llm.Message, string, llm.Usage, error) {
+ order := p.order(req)
+ var lastErr error
+ for i, m := range order {
+ msg, sr, usage, err := m.Prov.Complete(ctx, req)
+ if err == nil {
+ p.health.Pass(m.ID)
+ return msg, sr, usage, nil
+ }
+ if !shouldFailover(ctx, err) {
+ // Non-failover error (e.g. ctx cancel, deterministic 4xx): surface as-is
+ // without tripping health, matching Stream's non-failover path.
+ p.health.Pass(m.ID)
+ return llm.Message{}, "", llm.Usage{}, err
+ }
+ lastErr = err
+ hard := isHardFailure(err)
+ if p.health.Trip(m.ID, trimErr(err), hard) {
+ log.Printf("[llmpool] 配置 %q(%s) 已熔断:%s", m.Name, m.Model, trimErr(err))
+ }
+ if i+1 < len(order) {
+ n := order[i+1]
+ log.Printf("[llmpool] LLM 故障转移:%q(%s) → %q(%s),原因:%s",
+ m.Name, m.Model, n.Name, n.Model, trimErr(err))
+ }
+ }
+ if lastErr == nil {
+ lastErr = ErrExhausted
+ }
+ log.Printf("[llmpool] 轮询链已耗尽(%d 个配置全部失败),最后错误:%s", len(order), trimErr(lastErr))
+ return llm.Message{}, "", llm.Usage{}, fmt.Errorf("%w:%v", ErrExhausted, lastErr)
+}
+
// order picks the members to try, in order: skip those in a cooling-off window
// and those whose context window can't hold this request, then rotate within each
// equal-priority group so same-priority profiles share the load. Never returns an
diff --git a/llmpool/pool_test.go b/llmpool/pool_test.go
index b1e69c8..21b7568 100644
--- a/llmpool/pool_test.go
+++ b/llmpool/pool_test.go
@@ -55,6 +55,17 @@ func (f *fakeProv) Stream(ctx context.Context, req llm.CompletionRequest) iter.S
}
}
+func (f *fakeProv) Complete(ctx context.Context, req llm.CompletionRequest) (llm.Message, string, llm.Usage, error) {
+ acc := llm.NewAccumulator()
+ for ev, err := range f.Stream(ctx, req) {
+ if err != nil {
+ return llm.Message{}, "", llm.Usage{}, err
+ }
+ acc.Add(ev)
+ }
+ return acc.Message(), acc.StopReason, acc.Usage, nil
+}
+
// ok builds a provider that always succeeds with one text delta.
func okProv(name, text string) *fakeProv {
return &fakeProv{name: name, events: [][]llm.StreamEvent{{{Type: llm.SETextDelta, Text: text}}}}
diff --git a/llmrec/llmrec.go b/llmrec/llmrec.go
index 5fc77d1..3dbdd90 100644
--- a/llmrec/llmrec.go
+++ b/llmrec/llmrec.go
@@ -39,10 +39,10 @@ func TaskIDFrom(ctx context.Context) string {
// Recorder wraps an llm.Provider and records every completion call.
type Recorder struct {
- inner llm.Provider
- pg *db.DB
- model string // model name (from config, not in CompletionRequest)
- prof string // LLM profile name (from llm_profiles)
+ inner llm.Provider
+ pg *db.DB
+ model string // model name (from config, not in CompletionRequest)
+ prof string // LLM profile name (from llm_profiles)
// thinkingType / reasoningEffort 是配置级思考参数(思考开关 / 思考强度)。它们在
// norma 的 buildBody() 里从 provider 配置注入真正的 HTTP body,不出现在
// CompletionRequest 上,故 Recorder 需在此单独带一份,序列化时写进录制。
@@ -185,6 +185,58 @@ func (r *Recorder) Stream(ctx context.Context, req llm.CompletionRequest) iter.S
}
}
+// Complete implements llm.Provider for non-streaming calls, recording the same
+// metering + body trace as Stream. Metering (token usage) is always written;
+// the heavy request/response body trace is gated by the llm_record setting. The
+// raw wire bodies are captured by the shared HTTP transport (via NewCapture on
+// ctx), identical to the streaming path — a non-streaming response is a single
+// JSON body the transport tees just the same.
+func (r *Recorder) Complete(ctx context.Context, req llm.CompletionRequest) (llm.Message, string, llm.Usage, error) {
+ recordBodies := r.enabled == nil || r.enabled()
+ start := time.Now()
+ session := transcript.SessionIDFrom(ctx)
+ parsedID, worker := parseSession(session)
+ expID := db.ParseExpID(parsedID)
+ taskID := TaskIDFrom(ctx)
+ if taskID == "" {
+ taskID = parsedID
+ }
+
+ reqBody := ""
+ var capt *Capture
+ if recordBodies {
+ reqBody = r.serializeRequest(req)
+ ctx, capt = NewCapture(ctx)
+ }
+
+ msg, stopReason, usage, err := r.inner.Complete(ctx, req)
+
+ status := "ok"
+ if err != nil {
+ status = "error"
+ }
+ // Lightweight metering row — always written.
+ r.recordUsage(taskID, expID, worker, usage, int(time.Since(start).Milliseconds()), status)
+ // Heavy trace row — only when body recording is on.
+ if recordBodies {
+ text, thinking := msg.Text(), thinkingText(msg)
+ r.record(req, session, taskID, worker, reqBody, capt, start, text, thinking, usage, stopReason, err)
+ }
+ return msg, stopReason, usage, err
+}
+
+// thinkingText concatenates the thinking blocks of a message (the non-streaming
+// counterpart of the streaming path's accumulated thinkingBuf).
+func thinkingText(msg llm.Message) string {
+ var b strings.Builder
+ for _, bl := range msg.Content {
+ if bl.Type == llm.BlockThinking && bl.Thinking != "" {
+ b.WriteString(bl.Thinking)
+ }
+ }
+ return b.String()
+}
+
// recordUsage appends one lightweight metering row to llm_usage (no bodies). Skips
// zero-token calls with no model, which carry nothing worth metering.
func (r *Recorder) recordUsage(taskID string, expID int64, worker string, usage llm.Usage, latencyMs int, status string) {
diff --git a/server/goals.go b/server/goals.go
index dbc497d..c8e6d98 100644
--- a/server/goals.go
+++ b/server/goals.go
@@ -383,7 +383,8 @@ func (s *Server) createGoals(ctx context.Context, t *Task, emit func(db.Activity
}
taskID, _ := strconv.ParseInt(t.ID, 10, 64)
s.engine.BeginLLMCall(t.ID)
- decomposed := agent.DecomposeGoalsWithProvider(ctx, s.agentsForTask(t).runtime, s.m.dir, t.Goal, t.Description, as, t.Store, taskID, emit)
+ goalRuntime := s.agentsForTask(t).runtime
+ decomposed := agent.DecomposeGoalsWithProvider(ctx, goalRuntime, s.m.dir, t.Goal, t.Description, as, t.Store, taskID, goalRuntime.nonStreaming(), emit)
s.engine.EndLLMCall(t.ID)
for _, g := range decomposed {
if strings.TrimSpace(g.Text) != "" {
diff --git a/server/server.go b/server/server.go
index 999fcb6..461b4a4 100644
--- a/server/server.go
+++ b/server/server.go
@@ -305,6 +305,7 @@ func (s *Server) loadLLMConfig() (agent.Config, bool) {
cfg.ContextWindowK = p.ContextWindowK
cfg.ThinkingType = p.ThinkingType
cfg.ReasoningEffort = p.ReasoningEffort
+ cfg.Stream = p.Streaming
if cfg.APIKey == "" {
return cfg, false
}
@@ -322,14 +323,15 @@ func (s *Server) saveLLMConfig(cfg agent.Config) error {
}
var id int64
// agent.Config carries no failover fields, so carry the stored ones forward —
- // otherwise this legacy endpoint would silently reset the profile's priority
- // and pool_exclude to zero values on every save.
+ // otherwise this legacy endpoint would silently reset the profile's priority,
+ // pool_exclude, and streaming to zero values on every save.
var priority int
var poolExclude bool
+ streaming := true // 旧库/新建默认流式
if profs, _ := s.m.pg.ListProfiles(); profs != nil {
for _, p := range profs {
if p.Name == "default" {
- id, priority, poolExclude = p.ID, p.Priority, p.PoolExclude
+ id, priority, poolExclude, streaming = p.ID, p.Priority, p.PoolExclude, p.Streaming
break
}
}
@@ -338,7 +340,7 @@ func (s *Server) saveLLMConfig(cfg agent.Config) error {
ID: id, Name: "default", Format: format, Model: cfg.Model, BaseURL: cfg.BaseURL, Proxy: cfg.Proxy,
APIKey: cfg.APIKey, RatePerSecond: cfg.RatePerSecond, RatePerMinute: cfg.RatePerMinute,
ContextWindowK: cfg.ContextWindowK, ThinkingType: cfg.ThinkingType, ReasoningEffort: cfg.ReasoningEffort, IsDefault: true,
- Priority: priority, PoolExclude: poolExclude,
+ Priority: priority, PoolExclude: poolExclude, Streaming: streaming,
})
if err != nil {
return err
@@ -388,6 +390,7 @@ func (s *Server) buildPlannerWorker(pinID *int64, gProv llm.Provider, gCfg agent
wk.SetMemory(memory.NewStore(filepath.Join(s.m.dir, "memory")))
wk.SetWebSearch(s.webSearchFor("worker"))
wk.SetConstraintInject(s.constraintInjectWorker) // 操作约束注入 worker(可配置,默认开;每轮读)
+ wk.SetNonStreaming(nonStreamingResolver(wCfg)) // 该 profile 选非流式时走 Provider.Complete
pProv, pCfg := s.providerForAgent("planner", pinID, gProv, gCfg)
pl := agent.NewPlanner(pProv, pCfg.Model, s.m.dir, tx, pCfg.CompactionWindow(), s.agentMaxTurns("planner"))
pl.SetKillWork(s.engine.KillWork) // planner kill_work → terminate a running work
@@ -395,9 +398,18 @@ func (s *Server) buildPlannerWorker(pinID *int64, gProv llm.Provider, gCfg agent
pl.SetProxy(s.m.ProxyAddr(), s.m.ProxyCACert()) // WebFetch through the recording proxy
pl.SetWebSearch(s.webSearchFor("planner"))
pl.SetConstraintInject(s.constraintInjectPlanner) // 操作约束注入 planner(可配置,默认开;每轮读)
+ pl.SetNonStreaming(nonStreamingResolver(pCfg)) // 该 profile 选非流式时走 Provider.Complete
return pl, wk
}
+// nonStreamingResolver returns a resolver capturing a profile's streaming choice.
+// The agents read it per run; a profile change rebuilds the agents (applyLLM),
+// so the captured value is always the one in effect for this build.
+func nonStreamingResolver(cfg agent.Config) func() bool {
+ nonStreaming := !cfg.Stream
+ return func() bool { return nonStreaming }
+}
+
// applyLLM (re)builds the planner/worker/main-agent from cfg and installs them on
// the running engine as the GLOBAL active pair. Safe to call at runtime (UI configures LLM).
func (s *Server) applyLLM(cfg agent.Config) error {
@@ -441,12 +453,14 @@ func (s *Server) applyLLM(cfg agent.Config) error {
s.mainAgent.SetProxy(s.m.ProxyAddr(), s.m.ProxyCACert()) // WebFetch through the recording proxy
s.mainAgent.SetWebSearch(s.webSearchFor("mainagent"))
s.mainAgent.SetSteerWork(s.engine.SteerWork) // steer_work:人对运行中 work 实时纠偏
+ s.mainAgent.SetNonStreaming(nonStreamingResolver(mCfg))
// chat agent serves MANY custom agents by key → it holds the GLOBAL opts
// (backend/key) and gates Enabled per-conversation-agent at Chat time. 对话始终用激活配置。
s.chatAgent = agent.NewChatAgent(prov, cfg.Model, s.m.dir, tx, win) // chat page runner
s.chatAgent.SetProxy(s.m.ProxyAddr(), s.m.ProxyCACert())
s.chatAgent.SetWebSearch(s.m.WebSearchOpts())
s.chatAgent.SetGuard(s.chatGuard())
+ s.chatAgent.SetNonStreaming(nonStreamingResolver(cfg))
s.llmProv = prov
s.llmCfg = cfg
s.llmOn = true
@@ -472,6 +486,7 @@ func (s *Server) loadProfileConfig(id int64) (agent.Config, bool) {
cfg.ContextWindowK = p.ContextWindowK
cfg.ThinkingType = p.ThinkingType
cfg.ReasoningEffort = p.ReasoningEffort
+ cfg.Stream = p.Streaming
if cfg.APIKey == "" {
return cfg, false
}
@@ -623,6 +638,7 @@ func (s *Server) chatAgentForProfile(id int64) *agent.ChatAgent {
ca.SetProxy(s.m.ProxyAddr(), s.m.ProxyCACert())
ca.SetWebSearch(s.m.WebSearchOpts())
ca.SetGuard(s.chatGuard())
+ ca.SetNonStreaming(nonStreamingResolver(cfg))
s.profMu.Lock()
if ex := s.profChatAgents[id]; ex != nil { // lost the race → keep the winner
ca = ex
diff --git a/server/server_mgmt.go b/server/server_mgmt.go
index 3b2fad7..02baba2 100644
--- a/server/server_mgmt.go
+++ b/server/server_mgmt.go
@@ -1736,10 +1736,14 @@ func (s *Server) pgSaveProfile(w http.ResponseWriter, r *http.Request) {
return
}
// APIKey has json:"-" on db.LLMProfile so it is never leaked to the UI on
- // read; accept it here via a sibling field for create/update.
+ // read; accept it here via a sibling field for create/update. Streaming is a
+ // *bool shadowing the embedded json:"streaming": an absent field must default
+ // to streaming (true), which a plain bool zero value (false = non-streaming)
+ // would get wrong — legacy/partial clients that never send it stay streaming.
var body struct {
db.LLMProfile
- APIKey string `json:"api_key"`
+ APIKey string `json:"api_key"`
+ Streaming *bool `json:"streaming"`
}
if err := decode(r, &body); err != nil {
writeErr(w, 400, err.Error())
@@ -1747,6 +1751,7 @@ func (s *Server) pgSaveProfile(w http.ResponseWriter, r *http.Request) {
}
p := body.LLMProfile
p.APIKey = body.APIKey
+ p.Streaming = body.Streaming == nil || *body.Streaming
id, err := pg.SaveProfile(&p)
if err != nil {
writeErr(w, 500, err.Error())
diff --git a/server/task_llm.go b/server/task_llm.go
index 01a94b7..5a22600 100644
--- a/server/task_llm.go
+++ b/server/task_llm.go
@@ -139,6 +139,36 @@ func (r *taskLLMRuntime) current() (*Task, int64, int64, llm.Provider, error) {
return t, 0, pt.LLMChainRevision, prov, nil
}
+// nonStreaming reports whether the task's currently-active LLM source is set to
+// non-streaming. It mirrors current()'s source precedence (agent binding →
+// active chain profile → global) but reads only the streaming flag. Read-only
+// and best-effort: any resolution error falls back to streaming (false), the
+// safe default. If failover switches to a profile with a different streaming
+// setting, the change takes effect on the next agent run (a fresh Session is
+// built per run in captureRun).
+func (r *taskLLMRuntime) nonStreaming() bool {
+ taskNum, err := parseTaskID(r.taskID)
+ if err != nil {
+ return false
+ }
+ pt, err := r.s.m.pg.GetTask(taskNum)
+ if err != nil || pt == nil {
+ return false
+ }
+ if _, cfg, ok := r.s.agentBindingProvider(r.agentKey); ok {
+ return !cfg.Stream
+ }
+ if len(pt.LLMProfileIDs) > 0 && pt.ActiveLLMProfileID != nil {
+ if _, cfg, ok := r.s.providerForProfile(*pt.ActiveLLMProfileID); ok {
+ return !cfg.Stream
+ }
+ }
+ if _, cfg, ok := r.s.globalProvider(); ok {
+ return !cfg.Stream
+ }
+ return false
+}
+
func parseTaskID(id string) (int64, error) {
n, err := strconv.ParseInt(id, 10, 64)
if err != nil || n <= 0 {
@@ -147,9 +177,11 @@ func parseTaskID(id string) (int64, error) {
return n, nil
}
-func (r *taskLLMRuntime) Stream(ctx context.Context, req llm.CompletionRequest) iter.Seq2[llm.StreamEvent, error] {
- ctx = llmrec.WithTaskID(ctx, r.taskID)
- hooks := taskLLMStreamHooks{
+// streamHooks builds the failover/exhaustion callbacks shared by Stream and
+// Complete: how to read the current profile selection, how to mark it quota
+// exhausted, and how to emit a failover transition.
+func (r *taskLLMRuntime) streamHooks() taskLLMStreamHooks {
+ return taskLLMStreamHooks{
current: func() (taskLLMSelection, error) {
task, profileID, revision, provider, err := r.current()
return taskLLMSelection{task: task, profileID: profileID, revision: revision, provider: provider}, err
@@ -169,7 +201,70 @@ func (r *taskLLMRuntime) Stream(ctx context.Context, req llm.CompletionRequest)
r.s.emitTaskLLMTransition(selection.task, transition, cause)
},
}
- return streamTaskLLM(ctx, r.taskID, req, hooks)
+}
+
+func (r *taskLLMRuntime) Stream(ctx context.Context, req llm.CompletionRequest) iter.Seq2[llm.StreamEvent, error] {
+ ctx = llmrec.WithTaskID(ctx, r.taskID)
+ return streamTaskLLM(ctx, r.taskID, req, r.streamHooks())
+}
+
+// Complete is the non-streaming counterpart of Stream. A non-streaming call is
+// atomic — it never delivers partial output — so every failure is safe to retry
+// on the same provider or fail over to the next profile without risking
+// duplicated model output or tool execution (the "committed" bookkeeping the
+// streaming path needs is unnecessary here).
+func (r *taskLLMRuntime) Complete(ctx context.Context, req llm.CompletionRequest) (llm.Message, string, llm.Usage, error) {
+ ctx = llmrec.WithTaskID(ctx, r.taskID)
+ return completeTaskLLM(ctx, r.taskID, req, r.streamHooks())
+}
+
+func completeTaskLLM(ctx context.Context, taskID string, req llm.CompletionRequest, hooks taskLLMStreamHooks) (llm.Message, string, llm.Usage, error) {
+ for {
+ selection, err := hooks.current()
+ if err != nil {
+ return llm.Message{}, "", llm.Usage{}, err
+ }
+ var (
+ msg llm.Message
+ sr string
+ usage llm.Usage
+ callErr error
+ )
+ // 同 provider 安全窗口重试:非流式调用要么整体成功、要么整体失败,没有
+ // 中途已交付输出的问题,所以任何瞬时失败都可原样重试。
+ for attempt := 0; ; attempt++ {
+ msg, sr, usage, callErr = selection.provider.Complete(ctx, req)
+ if callErr != nil && ctx.Err() == nil &&
+ attempt < sameProviderStreamRetries && isRetryableStreamError(callErr) {
+ backoff := sameProviderRetryBackoff(attempt)
+ log.Printf("[task-llm] task %s 非流式调用失败,%v 后同 provider 重试 (%d/%d): %v",
+ taskID, backoff, attempt+1, sameProviderStreamRetries, callErr)
+ if sleepCtx(ctx, backoff) {
+ break // 退避期间 ctx 取消 → 停止重试
+ }
+ continue
+ }
+ break
+ }
+ if callErr == nil {
+ return msg, sr, usage, nil
+ }
+ // profileID=0: 显式链已被清空;非额度错误:透传。二者都不改任务 failover 状态。
+ if selection.profileID == 0 || !isQuotaExhaustedError(callErr) {
+ return llm.Message{}, "", llm.Usage{}, callErr
+ }
+ transition, markErr := hooks.exhaust(selection, callErr)
+ if markErr != nil {
+ return llm.Message{}, "", llm.Usage{}, fmt.Errorf("mark profile quota exhausted after %v: %w", callErr, markErr)
+ }
+ if transition.Advanced && !transition.Stale && hooks.transition != nil {
+ hooks.transition(selection, transition, callErr)
+ }
+ if !transition.Stale && transition.NextProfileID == nil {
+ return llm.Message{}, "", llm.Usage{}, &taskLLMError{taskID: taskID, chainExhausted: transition.ChainExhausted, cause: callErr}
+ }
+ // 没有任何输出交付给调用方,换下一个 profile 重放同一逻辑请求是安全的。
+ }
}
func streamTaskLLM(ctx context.Context, taskID string, req llm.CompletionRequest, hooks taskLLMStreamHooks) iter.Seq2[llm.StreamEvent, error] {
@@ -435,6 +530,7 @@ func (s *Server) agentsForTask(t *Task) *taskAgentBundle {
window := workerRuntime.CompactionWindow()
wk := agent.NewWorker(workerRuntime, "task-router", s.m.dir, tx, window, s.agentMaxTurns("worker"))
wk.SetCompactionWindowResolver(workerRuntime.CompactionWindow)
+ wk.SetNonStreaming(workerRuntime.nonStreaming) // 按任务当前激活 profile 的流式开关(每轮读)
wk.SetRunTimeout(time.Duration(s.agentRunSeconds("worker")) * time.Second)
wk.SetProxy(s.m.ProxyAddr(), s.m.ProxyCACert())
wk.SetMemory(memory.NewStore(filepath.Join(s.m.dir, "memory")))
@@ -442,6 +538,7 @@ func (s *Server) agentsForTask(t *Task) *taskAgentBundle {
wk.SetConstraintInject(s.constraintInjectWorker) // 操作约束注入 worker(可配置,默认开)
pl := agent.NewPlanner(plannerRuntime, "task-router", s.m.dir, tx, plannerRuntime.CompactionWindow(), s.agentMaxTurns("planner"))
pl.SetCompactionWindowResolver(plannerRuntime.CompactionWindow)
+ pl.SetNonStreaming(plannerRuntime.nonStreaming)
pl.SetKillWork(s.engine.KillWork)
pl.SetSteerWork(s.engine.SteerWork)
pl.SetProxy(s.m.ProxyAddr(), s.m.ProxyCACert())
@@ -449,6 +546,7 @@ func (s *Server) agentsForTask(t *Task) *taskAgentBundle {
pl.SetConstraintInject(s.constraintInjectPlanner) // 操作约束注入 planner(可配置,默认开)
main := agent.NewMainAgent(mainRuntime, "task-router", s.m.dir, tx, mainRuntime.CompactionWindow(), s.agentMaxTurns("mainagent"))
main.SetCompactionWindowResolver(mainRuntime.CompactionWindow)
+ main.SetNonStreaming(mainRuntime.nonStreaming)
main.SetProxy(s.m.ProxyAddr(), s.m.ProxyCACert())
main.SetWebSearch(s.webSearchFor("mainagent"))
main.SetSteerWork(s.engine.SteerWork) // steer_work:人对运行中 work 实时纠偏
diff --git a/server/task_llm_test.go b/server/task_llm_test.go
index a72ae34..2d83bce 100644
--- a/server/task_llm_test.go
+++ b/server/task_llm_test.go
@@ -36,6 +36,23 @@ func (p *scriptedLLMProvider) Stream(context.Context, llm.CompletionRequest) ite
}
}
+func (p *scriptedLLMProvider) Complete(ctx context.Context, req llm.CompletionRequest) (llm.Message, string, llm.Usage, error) {
+ return accumulateStreamForTest(ctx, p.Stream, req)
+}
+
+// accumulateStreamForTest drains a mock's Stream to satisfy the non-streaming
+// Complete method.
+func accumulateStreamForTest(ctx context.Context, stream func(context.Context, llm.CompletionRequest) iter.Seq2[llm.StreamEvent, error], req llm.CompletionRequest) (llm.Message, string, llm.Usage, error) {
+ acc := llm.NewAccumulator()
+ for ev, err := range stream(ctx, req) {
+ if err != nil {
+ return llm.Message{}, "", llm.Usage{}, err
+ }
+ acc.Add(ev)
+ }
+ return acc.Message(), acc.StopReason, acc.Usage, nil
+}
+
// withZeroRetryBackoff sets the same-provider retry backoff to zero for the
// duration of a (serial) test and returns a restore func for defer.
func withZeroRetryBackoff() func() {
@@ -72,6 +89,10 @@ func (p *flakyThenOKProvider) Stream(context.Context, llm.CompletionRequest) ite
}
}
+func (p *flakyThenOKProvider) Complete(ctx context.Context, req llm.CompletionRequest) (llm.Message, string, llm.Usage, error) {
+ return accumulateStreamForTest(ctx, p.Stream, req)
+}
+
func collectTaskLLMStream(seq iter.Seq2[llm.StreamEvent, error]) ([]llm.StreamEvent, error) {
var events []llm.StreamEvent
var streamErr error
diff --git a/web/src/app/(main)/system/llm/page.tsx b/web/src/app/(main)/system/llm/page.tsx
index b08c6ef..db51f48 100644
--- a/web/src/app/(main)/system/llm/page.tsx
+++ b/web/src/app/(main)/system/llm/page.tsx
@@ -309,6 +309,7 @@ function ProfileSheet({
const [effort, setEffort] = React.useState(NONE);
const [priority, setPriority] = React.useState("0"); // 轮询顺位;越大越先
const [poolExclude, setPoolExclude] = React.useState(false);
+ const [streaming, setStreaming] = React.useState(true); // true=流式(默认);false=非流式
const [testing, setTesting] = React.useState(false);
const [saving, setSaving] = React.useState(false);
const [models, setModels] = React.useState<string[]>([]);
@@ -331,6 +332,7 @@ function ProfileSheet({
setEffort(fromStore(profile?.reasoning_effort));
setPriority(String(profile?.priority ?? 0));
setPoolExclude(profile?.pool_exclude ?? false);
+ setStreaming(profile?.streaming ?? true);
setApiKey("");
setKeyHint(profile?.api_key_hint ?? "");
setModels([]);
@@ -398,6 +400,7 @@ function ProfileSheet({
reasoning_effort: toStore(effort),
priority: Number(priority) || 0,
pool_exclude: poolExclude,
+ streaming,
});
if (isNew) toast.success(`已新建:${name.trim()}(在卡片上「设为激活」以启用)`);
else toast.success(profile?.is_default ? "已保存,激活配置即时生效,无需重启" : "已保存");
@@ -595,6 +598,16 @@ function ProfileSheet({
</div>
<Switch checked={poolExclude} onCheckedChange={setPoolExclude} aria-label="不参与轮询" />
</div>
+ <div className="flex items-center justify-between gap-4 border-t pt-3">
+ <div className="grid gap-0.5">
+ <Label className="text-sm">流式输出 · streaming</Label>
+ <p className="text-muted-foreground text-xs">
+ 开启(默认)走流式 SSE,有运行中实时进度与实时 token 计数。 关闭则走真·非流式(stream:false,
+ 一次性返回完整响应)——可绕开部分网关糟糕的 SSE 实现(空帧 / 思考字段丢帧), 代价是失去运行中的实时进度。
+ </p>
+ </div>
+ <Switch checked={streaming} onCheckedChange={setStreaming} aria-label="流式输出" />
+ </div>
</div>
<div className="grid gap-3 rounded-lg border p-3">
diff --git a/web/src/lib/api.ts b/web/src/lib/api.ts
index 9bb02e9..f606f6c 100644
--- a/web/src/lib/api.ts
+++ b/web/src/lib/api.ts
@@ -672,6 +672,7 @@ export const api = {
reasoning_effort?: string; // ""(不发送)|"low"|"medium"|"high"|"xhigh"|"max"
priority?: number; // 轮询顺位,越大越先用
pool_exclude?: boolean; // true=不作为故障转移目标
+ streaming?: boolean; // true(默认)=流式 | false=非流式
}) => post<{ id: number }>("/llm/profiles", p),
deleteLLMProfile: (id: string) => del<{ deleted: number }>(`/llm/profiles/${id}`),
activateLLMProfile: (id: string) => post<{ ok: boolean }>("/llm/profiles/active", { id: Number(id) }),
diff --git a/web/src/lib/types.ts b/web/src/lib/types.ts
index f9ada0d..da51599 100644
--- a/web/src/lib/types.ts
+++ b/web/src/lib/types.ts
@@ -799,6 +799,8 @@ export interface LLMProfile {
priority?: number;
// true = 不作为故障转移目标(仍可被 agent/任务显式绑定使用)。
pool_exclude?: boolean;
+ // true(默认)= 流式(SSE) | false = 真·非流式(stream:false,一次性返回)。
+ streaming?: boolean;
}
// ---- LLM 轮询(故障转移)----