diff --git a/apps/web/features/chat/components/message/message-discussion.tsx b/apps/web/features/chat/components/message/message-discussion.tsx
index f51b6e4c6..48ec7986e 100644
--- a/apps/web/features/chat/components/message/message-discussion.tsx
+++ b/apps/web/features/chat/components/message/message-discussion.tsx
@@ -425,6 +425,9 @@ export function MessageDiscussion({ discussion }: { discussion?: DiscussionState
? t("done")
: t("pendingTurn")}
+
{steps.map((step) => {
diff --git a/apps/web/i18n/messages/en-US/admin-discussions.json b/apps/web/i18n/messages/en-US/admin-discussions.json
index 650bb30a9..6949126ce 100644
--- a/apps/web/i18n/messages/en-US/admin-discussions.json
+++ b/apps/web/i18n/messages/en-US/admin-discussions.json
@@ -1,6 +1,6 @@
{
"title": "Multi-round discussion",
- "description": "Define multi-model discussions as a workflow: rounds run serially and steps inside a round run in parallel. Placeholders bind to real models at chat time.",
+ "description": "Define multi-model discussions as a workflow: rounds run serially, and each round can run in parallel or serial. Placeholders bind to real models at chat time.",
"newTemplate": "New template",
"loading": "Loading…",
"empty": "No discussion templates yet.",
@@ -29,8 +29,10 @@
"addRound": "Add round",
"round": "Round {round}",
"parallel": "Parallel",
+ "serial": "Serial",
"roundIndependent": "Every model answers independently in this round.",
- "roundSeesPrevious": "Every model sees the previous round and all earlier discussion in this round.",
+ "roundSeesPrevious": "In a parallel round, every model sees the previous round and all earlier discussion.",
+ "roundSerial": "A serial round runs models 1, 2, 3…N in order; later models see and respond to earlier answers from the same round.",
"addStep": "Add step",
"promptPlaceholder": "Prompt for this round",
"summary": "Summary model (final round)",
@@ -38,7 +40,7 @@
"workflow": "Workflow",
"workflowQuestion": "User question",
"workflowSummary": "Summary",
- "workflowHint": "Rounds run serially and steps inside a round run in parallel. From round 2 on, every model sees the previous round and all earlier discussion; the summary model then writes the final answer. Placeholders bind to real models at chat time; this page only defines the structure.",
+ "workflowHint": "Rounds run serially, and each round can be parallel or serial. Parallel models answer independently; serial models read and respond to earlier answers in model order. The summary model then writes the final answer. Placeholders bind to real models at chat time.",
"delete": "Delete",
"save": "Save",
"saving": "Saving…",
diff --git a/apps/web/i18n/messages/en-US/chat.json b/apps/web/i18n/messages/en-US/chat.json
index deac4aa52..c3e7691e3 100644
--- a/apps/web/i18n/messages/en-US/chat.json
+++ b/apps/web/i18n/messages/en-US/chat.json
@@ -1196,6 +1196,8 @@
"collapse": "Collapse",
"all": "All",
"round": "Round {round}",
+ "parallel": "Parallel",
+ "serial": "Serial",
"thinking": "Thinking",
"done": "Done",
"failed": "Failed",
diff --git a/apps/web/i18n/messages/zh-CN/admin-discussions.json b/apps/web/i18n/messages/zh-CN/admin-discussions.json
index 8718e0baa..a8461a4a1 100644
--- a/apps/web/i18n/messages/zh-CN/admin-discussions.json
+++ b/apps/web/i18n/messages/zh-CN/admin-discussions.json
@@ -1,6 +1,6 @@
{
"title": "多轮讨论",
- "description": "用工作流定义多模型讨论:轮次串行、轮内并行,占位符在对话时绑定真实模型。",
+ "description": "用工作流定义多模型讨论:轮次串行,每轮可选并行或串行,占位符在对话时绑定真实模型。",
"newTemplate": "新建模板",
"loading": "加载中…",
"empty": "还没有多轮讨论模板。",
@@ -29,8 +29,10 @@
"addRound": "添加轮次",
"round": "第 {round} 轮",
"parallel": "并行",
+ "serial": "串行",
"roundIndependent": "本轮各模型并行独立回答。",
- "roundSeesPrevious": "本轮每个模型都会看到上一轮及更早的讨论记录。",
+ "roundSeesPrevious": "并行轮次中,每个模型都会看到上一轮及更早的讨论记录。",
+ "roundSerial": "串行轮次按模型 1、2、3…N 执行,后续模型能看到同轮前序回答并回应分歧。",
"addStep": "添加步骤",
"promptPlaceholder": "这一轮的提示词",
"summary": "总结模型(最后一轮)",
@@ -38,7 +40,7 @@
"workflow": "工作流",
"workflowQuestion": "用户问题",
"workflowSummary": "总结",
- "workflowHint": "轮次串行执行,轮内并行;第 2 轮起每个模型都会看到上一轮及更早的讨论记录;最后由总结模型输出最终答案。占位符在对话时绑定真实模型,这里只决定结构。",
+ "workflowHint": "轮次之间串行,每轮可选并行或串行;并行模型独立回答,串行模型按编号顺序阅读并回应同轮前序回答;最后由总结模型输出最终答案。占位符在对话时绑定真实模型。",
"delete": "删除",
"save": "保存",
"saving": "保存中…",
diff --git a/apps/web/i18n/messages/zh-CN/chat.json b/apps/web/i18n/messages/zh-CN/chat.json
index 09d449d04..9de83f307 100644
--- a/apps/web/i18n/messages/zh-CN/chat.json
+++ b/apps/web/i18n/messages/zh-CN/chat.json
@@ -1196,6 +1196,8 @@
"collapse": "收起",
"all": "全部",
"round": "第 {round} 轮",
+ "parallel": "并行",
+ "serial": "串行",
"thinking": "思考中",
"done": "已完成",
"failed": "失败",
diff --git a/apps/web/shared/api/discussion-types.ts b/apps/web/shared/api/discussion-types.ts
index b83d075d9..a4424c74e 100644
--- a/apps/web/shared/api/discussion-types.ts
+++ b/apps/web/shared/api/discussion-types.ts
@@ -78,6 +78,7 @@ export type DiscussionTurn = {
export type DiscussionRuntimeRound = {
index: number;
+ parallel?: boolean;
steps: Array<{
id: string;
participant: string;
diff --git a/backend/internal/application/conversation/service_discussion.go b/backend/internal/application/conversation/service_discussion.go
index a3022f4a1..082b07111 100644
--- a/backend/internal/application/conversation/service_discussion.go
+++ b/backend/internal/application/conversation/service_discussion.go
@@ -21,6 +21,8 @@ const (
discussionModelPrefix = "discussion:"
discussionDefaultStepPrompt = "你是多模型讨论中的一员。请独立、具体地回答用户问题,指出关键假设与风险。"
discussionFollowupPrompt = "你是多模型讨论中的一员。请阅读已有讨论,补充遗漏、纠正错误、挑战薄弱推理,不要重复已经充分的内容。"
+ discussionParallelPrompt = "本轮为并行独立发言。你看不到同轮其他模型的回答,请独立判断,不要假设存在共识。"
+ discussionSerialPrompt = "本轮为串行讨论。你可以看到同轮排在你之前的模型发言。先独立判断,再针对具体分歧进行回应;指出对方依赖的假设、逻辑跳跃或缺失证据,并给出反例或替代解释。不要重复已经充分的内容,也不要为了反对而反对。"
discussionSummaryPrompt = "你是多模型讨论的主持人。请只依据成功的讨论发言,给出面向用户的最终答案;失败的发言不是观点或证据。"
discussionMaxTurnRunes = 12000
discussionMaxTranscriptRunes = 60000
@@ -146,21 +148,8 @@ func (s *Service) StreamDiscussionMessage(ctx context.Context, input DiscussionS
if err = ctx.Err(); err != nil {
break
}
- transcript := discussionTranscript(state.Turns)
- results := make([]domaindiscussion.Turn, len(round.Steps))
- var wg sync.WaitGroup
- for index, step := range round.Steps {
- wg.Add(1)
- go func(index int, step domaindiscussion.RuntimeStep) {
- defer wg.Done()
- turn := s.runDiscussionTurn(ctx, turnInput, conversation, round.Index, step, transcript, history)
- results[index] = turn
- emit("discussion_turn_done", map[string]any{"turn": discussionTurnPayloadOf(turn)})
- }(index, step)
- }
- wg.Wait()
+ results := s.runDiscussionRound(ctx, turnInput, conversation, round, &state, history, emit)
for _, turn := range results {
- state.Turns = append(state.Turns, turn)
totalInput += turn.InputTokens
totalOutput += turn.OutputTokens
totalCacheRead += turn.CacheReadTokens
@@ -266,6 +255,41 @@ func (s *Service) StreamDiscussionMessage(ctx context.Context, input DiscussionS
}, nil
}
+func (s *Service) runDiscussionRound(
+ ctx context.Context,
+ input DiscussionSendInput,
+ conversation *model.Conversation,
+ round domaindiscussion.RuntimeRound,
+ state *domaindiscussion.State,
+ history []llm.Message,
+ emit func(string, map[string]any),
+) []domaindiscussion.Turn {
+ results := make([]domaindiscussion.Turn, len(round.Steps))
+ if round.IsParallel() {
+ transcript := discussionTranscript(state.Turns)
+ var wg sync.WaitGroup
+ for index, step := range round.Steps {
+ wg.Add(1)
+ go func(index int, step domaindiscussion.RuntimeStep) {
+ defer wg.Done()
+ turn := s.runDiscussionTurn(ctx, input, conversation, round.Index, step, transcript, history, true)
+ results[index] = turn
+ emit("discussion_turn_done", map[string]any{"turn": discussionTurnPayloadOf(turn)})
+ }(index, step)
+ }
+ wg.Wait()
+ state.Turns = append(state.Turns, results...)
+ return results
+ }
+ for index, step := range round.Steps {
+ turn := s.runDiscussionTurn(ctx, input, conversation, round.Index, step, discussionTranscript(state.Turns), history, false)
+ results[index] = turn
+ state.Turns = append(state.Turns, turn)
+ emit("discussion_turn_done", map[string]any{"turn": discussionTurnPayloadOf(turn)})
+ }
+ return results
+}
+
func (s *Service) runDiscussionTurn(
ctx context.Context,
input DiscussionSendInput,
@@ -274,6 +298,7 @@ func (s *Service) runDiscussionTurn(
step domaindiscussion.RuntimeStep,
transcript string,
history []llm.Message,
+ parallel bool,
) domaindiscussion.Turn {
turn := domaindiscussion.Turn{
Round: roundIndex,
@@ -291,8 +316,13 @@ func (s *Service) runDiscussionTurn(
prompt = discussionFollowupPrompt
}
}
+ if parallel {
+ prompt += "\n\n" + discussionParallelPrompt
+ } else {
+ prompt += "\n\n" + discussionSerialPrompt
+ }
messages := append([]llm.Message{{Role: "system", Content: prompt}}, history...)
- messages = append(messages, llm.Message{Role: "user", Content: discussionTurnUserContent(input.Content, transcript)})
+ messages = append(messages, llm.Message{Role: "user", Content: discussionTurnUserContent(input.Content, transcript, parallel)})
output, promptText, promptMessages, err := s.generateDiscussion(ctx, input, conversation, step.Model, messages, func(promptText string, promptMessages []domaindiscussion.PromptMessage) {
if input.OnEvent != nil {
_ = input.OnEvent("discussion_turn_start", map[string]any{
@@ -453,14 +483,17 @@ func discussionPromptMessagesOf(messages []llm.Message) []domaindiscussion.Promp
return result
}
-// discussionTurnUserContent 组装一次轮次发言的当前用户消息:本次问题 + 本轮之前的讨论记录。
+// discussionTurnUserContent 组装一次轮次发言的当前用户消息:本次问题 + 当前模型可见的讨论记录。
// 历史对话由 discussionHistoryMessages 以真实 user/assistant 消息的形式携带,不再拼进这一段。
-func discussionTurnUserContent(question string, transcript string) string {
+func discussionTurnUserContent(question string, transcript string, parallel bool) string {
var builder strings.Builder
builder.WriteString(strings.TrimSpace(question))
if transcript != "" {
- // From the second round on, every model sees the previous round and all earlier rounds.
- builder.WriteString("\n\n上一轮及更早的讨论记录:\n")
+ if parallel {
+ builder.WriteString("\n\n此前轮次的讨论记录(不包含本轮其他模型):\n")
+ } else {
+ builder.WriteString("\n\n此前轮次及本轮在你之前的讨论记录:\n")
+ }
builder.WriteString(transcript)
}
return builder.String()
diff --git a/backend/internal/application/conversation/service_discussion_payload.go b/backend/internal/application/conversation/service_discussion_payload.go
index 0871e0fe1..5b914c5c8 100644
--- a/backend/internal/application/conversation/service_discussion_payload.go
+++ b/backend/internal/application/conversation/service_discussion_payload.go
@@ -18,8 +18,9 @@ type discussionStepPayload struct {
}
type discussionRoundPayload struct {
- Index int `json:"index"`
- Steps []discussionStepPayload `json:"steps"`
+ Index int `json:"index"`
+ Parallel bool `json:"parallel"`
+ Steps []discussionStepPayload `json:"steps"`
}
type discussionSummaryPayload struct {
@@ -92,8 +93,9 @@ func discussionRoundsPayloadOf(rounds []domaindiscussion.RuntimeRound) []discuss
payload := make([]discussionRoundPayload, 0, len(rounds))
for _, round := range rounds {
roundPayload := discussionRoundPayload{
- Index: round.Index,
- Steps: make([]discussionStepPayload, 0, len(round.Steps)),
+ Index: round.Index,
+ Parallel: round.IsParallel(),
+ Steps: make([]discussionStepPayload, 0, len(round.Steps)),
}
for _, step := range round.Steps {
roundPayload.Steps = append(roundPayload.Steps, discussionStepPayload{
diff --git a/backend/internal/application/conversation/service_discussion_test.go b/backend/internal/application/conversation/service_discussion_test.go
index e15b33f20..ec5a95857 100644
--- a/backend/internal/application/conversation/service_discussion_test.go
+++ b/backend/internal/application/conversation/service_discussion_test.go
@@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"strings"
+ "sync"
"testing"
"github.com/DEEIX-AI/DEEIX-Chat/backend/internal/application/channel"
@@ -93,23 +94,29 @@ func TestDiscussionEventPayloadsUseStateShape(t *testing.T) {
}
func TestDiscussionTurnUserContentKeepsQuestionAndTranscript(t *testing.T) {
- text := discussionTurnUserContent("现在怎么办?", "第 1 轮\n- model-a: A1")
- for _, want := range []string{"现在怎么办?", "上一轮及更早的讨论记录", "A1"} {
+ text := discussionTurnUserContent("现在怎么办?", "第 1 轮\n- model-a: A1", true)
+ for _, want := range []string{"现在怎么办?", "此前轮次的讨论记录(不包含本轮其他模型)", "A1"} {
if !strings.Contains(text, want) {
t.Fatalf("expected %q in user content, got %s", want, text)
}
}
+ serial := discussionTurnUserContent("现在怎么办?", "第 1 轮\n- model-a: A1", false)
+ if !strings.Contains(serial, "此前轮次及本轮在你之前的讨论记录") {
+ t.Fatalf("expected serial context heading, got %s", serial)
+ }
// History now travels as real user/assistant messages, not as a flattened text block.
if strings.Contains(text, "上次讨论的结果") {
t.Fatalf("prior result must not be inlined into the current question, got %s", text)
}
- if bare := discussionTurnUserContent("你好", ""); strings.Contains(bare, "讨论记录") {
+ if bare := discussionTurnUserContent("你好", "", true); strings.Contains(bare, "讨论记录") {
t.Fatalf("an empty transcript must not add a heading, got %s", bare)
}
}
type discussionStreamGatewayStub struct {
temporaryLLMGatewayStub
+ mu sync.Mutex
+ responses []string
}
func (g *discussionStreamGatewayStub) GenerateStream(
@@ -118,6 +125,12 @@ func (g *discussionStreamGatewayStub) GenerateStream(
input llm.GenerateInput,
onEvent func(llm.GenerateStreamEvent) error,
) (*llm.GenerateOutput, error) {
+ g.mu.Lock()
+ defer g.mu.Unlock()
+ if len(g.responses) > 0 {
+ g.output = &llm.GenerateOutput{Text: g.responses[0]}
+ g.responses = g.responses[1:]
+ }
if onEvent != nil {
if err := onEvent(llm.GenerateStreamEvent{Delta: "partial"}); err != nil {
return nil, err
@@ -126,13 +139,98 @@ func (g *discussionStreamGatewayStub) GenerateStream(
return g.temporaryLLMGatewayStub.GenerateStream(ctx, route, input, nil)
}
-func discussionStreamTestService() *Service {
+func discussionStreamTestServiceForModels(gateway *discussionStreamGatewayStub, models ...string) *Service {
+ routes := make(map[string]*channel.ResolvedRoute, len(models))
+ for _, modelName := range models {
+ routes[modelName] = &channel.ResolvedRoute{PlatformModelName: modelName, UpstreamModel: modelName, Protocol: llm.AdapterOpenAIChatCompletions}
+ }
return &Service{
- cfg: config.NewRuntime(config.Config{}),
- routeResolver: &textTaskRouteResolverStub{routes: map[string]*channel.ResolvedRoute{
- "model-a": {PlatformModelName: "model-a", UpstreamModel: "model-a", Protocol: llm.AdapterOpenAIChatCompletions},
- }},
- llmClient: &discussionStreamGatewayStub{},
+ cfg: config.NewRuntime(config.Config{}),
+ routeResolver: &textTaskRouteResolverStub{routes: routes},
+ llmClient: gateway,
+ }
+}
+
+func discussionStreamTestService() *Service {
+ return discussionStreamTestServiceForModels(&discussionStreamGatewayStub{}, "model-a")
+}
+
+func lastDiscussionUserContent(input llm.GenerateInput) string {
+ return input.Messages[len(input.Messages)-1].Content
+}
+
+func TestDiscussionSerialRoundBuildsIncrementalContext(t *testing.T) {
+ gateway := &discussionStreamGatewayStub{responses: []string{"first answer", "second answer", "third answer"}}
+ service := discussionStreamTestServiceForModels(gateway, "model-a", "model-b", "model-c")
+ serial := false
+ state := domaindiscussion.State{Turns: []domaindiscussion.Turn{{Round: 1, Status: "completed", Model: "previous", Content: "previous answer"}}}
+ round := domaindiscussion.RuntimeRound{Index: 2, Parallel: &serial, Steps: []domaindiscussion.RuntimeStep{
+ {ID: "s1", Participant: "p1", Model: "model-a"},
+ {ID: "s2", Participant: "p2", Model: "model-b"},
+ {ID: "s3", Participant: "p3", Model: "model-c"},
+ }}
+ results := service.runDiscussionRound(
+ context.Background(),
+ DiscussionSendInput{Content: "question"},
+ &model.Conversation{ID: 1},
+ round,
+ &state,
+ nil,
+ func(string, map[string]any) {},
+ )
+ if len(results) != 3 || results[0].Model != "model-a" || results[1].Model != "model-b" || results[2].Model != "model-c" {
+ t.Fatalf("unexpected result order: %#v", results)
+ }
+ if len(gateway.inputs) != 3 {
+ t.Fatalf("upstream calls = %d", len(gateway.inputs))
+ }
+ first := lastDiscussionUserContent(gateway.inputs[0])
+ second := lastDiscussionUserContent(gateway.inputs[1])
+ third := lastDiscussionUserContent(gateway.inputs[2])
+ if !strings.Contains(first, "previous answer") || strings.Contains(first, "first answer") {
+ t.Fatalf("unexpected first context: %s", first)
+ }
+ if !strings.Contains(second, "first answer") || strings.Contains(second, "second answer") {
+ t.Fatalf("unexpected second context: %s", second)
+ }
+ if !strings.Contains(third, "first answer") || !strings.Contains(third, "second answer") || !strings.Contains(third, "此前轮次及本轮在你之前") {
+ t.Fatalf("unexpected third context: %s", third)
+ }
+ for _, input := range gateway.inputs {
+ if !strings.Contains(input.Messages[0].Content, discussionSerialPrompt) {
+ t.Fatalf("missing serial instruction: %s", input.Messages[0].Content)
+ }
+ }
+}
+
+func TestDiscussionParallelRoundSharesPreviousContextOnly(t *testing.T) {
+ gateway := &discussionStreamGatewayStub{responses: []string{"first answer", "second answer"}}
+ service := discussionStreamTestServiceForModels(gateway, "model-a", "model-b")
+ state := domaindiscussion.State{Turns: []domaindiscussion.Turn{{Round: 1, Status: "completed", Model: "previous", Content: "previous answer"}}}
+ round := domaindiscussion.RuntimeRound{Index: 2, Steps: []domaindiscussion.RuntimeStep{
+ {ID: "s1", Participant: "p1", Model: "model-a"},
+ {ID: "s2", Participant: "p2", Model: "model-b"},
+ }}
+ results := service.runDiscussionRound(
+ context.Background(),
+ DiscussionSendInput{Content: "question"},
+ &model.Conversation{ID: 1},
+ round,
+ &state,
+ nil,
+ func(string, map[string]any) {},
+ )
+ if len(results) != 2 || len(gateway.inputs) != 2 {
+ t.Fatalf("results = %d, calls = %d", len(results), len(gateway.inputs))
+ }
+ contexts := []string{lastDiscussionUserContent(gateway.inputs[0]), lastDiscussionUserContent(gateway.inputs[1])}
+ if contexts[0] != contexts[1] || !strings.Contains(contexts[0], "previous answer") || strings.Contains(contexts[0], "first answer") || strings.Contains(contexts[0], "second answer") {
+ t.Fatalf("parallel contexts differ or include same-round answers: %#v", contexts)
+ }
+ for _, input := range gateway.inputs {
+ if !strings.Contains(input.Messages[0].Content, discussionParallelPrompt) {
+ t.Fatalf("missing parallel instruction: %s", input.Messages[0].Content)
+ }
}
}
@@ -155,6 +253,7 @@ func TestDiscussionTurnEmitsStartWithPromptBeforeDelta(t *testing.T) {
domaindiscussion.RuntimeStep{ID: "s1", Participant: "p1", Model: "model-a"},
"",
nil,
+ true,
)
if strings.Join(events, ",") != "discussion_turn_start,discussion_turn_delta" {
t.Fatalf("events = %v", events)
diff --git a/backend/internal/domain/discussion/types.go b/backend/internal/domain/discussion/types.go
index 054370bf3..0af3df4d7 100644
--- a/backend/internal/domain/discussion/types.go
+++ b/backend/internal/domain/discussion/types.go
@@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
+ "sort"
"strings"
"time"
)
@@ -52,13 +53,18 @@ type Step struct {
Prompt string
}
-// Round 是一轮讨论;同轮步骤并行,轮次之间串行。
+// Round 是一轮讨论;同轮步骤可并行或按模型顺序串行,轮次之间串行。
type Round struct {
Index int
- Parallel bool
+ Parallel *bool
Steps []Step
}
+// IsParallel 缺省按并行处理,兼容没有 parallel 字段的旧配置。
+func (r Round) IsParallel() bool {
+ return r.Parallel == nil || *r.Parallel
+}
+
// Summary 是全部轮次结束后的总结节点。
type Summary struct {
Participant string
@@ -82,8 +88,14 @@ type RuntimeStep struct {
// RuntimeRound 是运行时解析后的一轮。
type RuntimeRound struct {
- Index int
- Steps []RuntimeStep
+ Index int
+ Parallel *bool
+ Steps []RuntimeStep
+}
+
+// IsParallel 缺省按并行处理,兼容没有 parallel 字段的旧讨论记录。
+func (r RuntimeRound) IsParallel() bool {
+ return r.Parallel == nil || *r.Parallel
}
// RuntimeSummary 是运行时解析后的总结节点。
@@ -227,8 +239,9 @@ func normalizeConfig(cfg *TemplateConfig) {
for index := range cfg.Rounds {
round := &cfg.Rounds[index]
round.Index = index + 1
- if len(round.Steps) > 0 {
- round.Parallel = true
+ if round.Parallel == nil {
+ parallel := true
+ round.Parallel = ¶llel
}
for stepIndex := range round.Steps {
if strings.TrimSpace(round.Steps[stepIndex].ID) == "" {
@@ -252,12 +265,20 @@ func ResolveRuntimePlan(cfg TemplateConfig, overrides map[string]string) (Runtim
}
return ""
}
+ placeholderOrder := make(map[string]int, len(cfg.Placeholders))
+ for index, item := range cfg.Placeholders {
+ placeholderOrder[item.Key] = index
+ }
plan := RuntimePlan{
Rounds: make([]RuntimeRound, 0, len(cfg.Rounds)),
}
for _, round := range cfg.Rounds {
- runtimeRound := RuntimeRound{Index: round.Index, Steps: make([]RuntimeStep, 0, len(round.Steps))}
- for _, step := range round.Steps {
+ runtimeRound := RuntimeRound{Index: round.Index, Parallel: round.Parallel, Steps: make([]RuntimeStep, 0, len(round.Steps))}
+ steps := append([]Step(nil), round.Steps...)
+ sort.SliceStable(steps, func(left, right int) bool {
+ return placeholderOrder[steps[left].Participant] < placeholderOrder[steps[right].Participant]
+ })
+ for _, step := range steps {
model := modelFor(step.Participant)
if model == "" {
return RuntimePlan{}, fmt.Errorf("%w: model is not selected for %s", ErrTemplateInvalid, step.Participant)
diff --git a/backend/internal/domain/discussion/types_test.go b/backend/internal/domain/discussion/types_test.go
index 6e9cc6a0b..42792c8b2 100644
--- a/backend/internal/domain/discussion/types_test.go
+++ b/backend/internal/domain/discussion/types_test.go
@@ -25,7 +25,7 @@ func TestParseConfigAndResolveRuntimePlan(t *testing.T) {
if err != nil {
t.Fatalf("ParseConfig: %v", err)
}
- if len(cfg.Rounds) != 2 || cfg.Rounds[0].Index != 1 || !cfg.Rounds[0].Parallel {
+ if len(cfg.Rounds) != 2 || cfg.Rounds[0].Index != 1 || !cfg.Rounds[0].IsParallel() {
t.Fatalf("unexpected rounds: %+v", cfg.Rounds)
}
if cfg.Rounds[0].Steps[0].ID != "r1s1" {
@@ -47,6 +47,41 @@ func TestParseConfigAndResolveRuntimePlan(t *testing.T) {
}
}
+func TestResolveRuntimePlanSerialRoundUsesPlaceholderOrder(t *testing.T) {
+ cfg, err := ParseConfig(`{
+ "placeholders": [
+ {"key": "p1", "defaultModel": "model-a"},
+ {"key": "p2", "defaultModel": "model-b"},
+ {"key": "p3", "defaultModel": "model-c"},
+ {"key": "judge", "defaultModel": "model-a"}
+ ],
+ "rounds": [{"parallel": false, "steps": [
+ {"participant": "p3"},
+ {"participant": "p1"},
+ {"participant": "p2"}
+ ]}],
+ "summary": {"participant": "judge"}
+ }`)
+ if err != nil {
+ t.Fatalf("ParseConfig: %v", err)
+ }
+ if cfg.Rounds[0].IsParallel() {
+ t.Fatal("explicit serial round was overwritten")
+ }
+ plan, err := ResolveRuntimePlan(cfg, nil)
+ if err != nil {
+ t.Fatalf("ResolveRuntimePlan: %v", err)
+ }
+ if plan.Rounds[0].IsParallel() {
+ t.Fatal("runtime plan lost serial mode")
+ }
+ for index, participant := range []string{"p1", "p2", "p3"} {
+ if plan.Rounds[0].Steps[index].Participant != participant {
+ t.Fatalf("runtime steps are not in model order: %#v", plan.Rounds[0].Steps)
+ }
+ }
+}
+
func TestResolveRuntimePlanRequiresEveryModel(t *testing.T) {
cfg, err := ParseConfig(`{
"placeholders": [{"key": "p1", "label": "A"}],
diff --git a/backend/internal/transport/http/discussion/handler.go b/backend/internal/transport/http/discussion/handler.go
index f48a77ffe..663263cee 100644
--- a/backend/internal/transport/http/discussion/handler.go
+++ b/backend/internal/transport/http/discussion/handler.go
@@ -186,7 +186,7 @@ func toTemplateConfigDTO(config domaindiscussion.TemplateConfig) TemplateConfigD
})
}
for _, round := range config.Rounds {
- roundDTO := RoundDTO{Index: round.Index, Parallel: round.Parallel, Steps: make([]StepDTO, 0, len(round.Steps))}
+ roundDTO := RoundDTO{Index: round.Index, Parallel: round.IsParallel(), Steps: make([]StepDTO, 0, len(round.Steps))}
for _, step := range round.Steps {
roundDTO.Steps = append(roundDTO.Steps, StepDTO{
ID: step.ID,
diff --git a/docs/DISCUSSIONS.md b/docs/DISCUSSIONS.md
index 17ca52afe..8606e2f24 100644
--- a/docs/DISCUSSIONS.md
+++ b/docs/DISCUSSIONS.md
@@ -16,7 +16,7 @@
## 1. 概念
- **模板**在后台定义结构,不绑定真实模型;**占位符**(placeholder)在对话时绑定真实模型。
-- **轮次**(round)串行执行;**轮内步骤**(step)并行执行。
+- **轮次**(round)串行执行;每轮的**轮内步骤**(step)可配置为并行或串行,默认并行。串行按模型 1、2、3…N 的占位符顺序执行。
- **总结模型**是模板的最后一个节点,不属于任何轮次。
- 占位符分两类:`summary.participant` 指向总结占位符,其余都是讨论模型。展示名由位置决定(总结固定为「总结模型」,其余按顺序为「模型 1 … 模型 n」),不读存储的 `label`。
@@ -38,7 +38,7 @@
```
- 一轮只有一个 prompt:`round.steps[*].prompt` 始终同步为同一个值,编辑器只让用户填一次。
-- 参与模型没有先后顺序,`steps` 的数组顺序不决定展示顺序;展示顺序来自 `placeholders`(见 §5)。
+- `round.parallel` 决定执行方式,缺省为 `true`。展示与串行执行都按 `placeholders` 的模型编号排序,不依赖 `steps` 的存储顺序(见 §5)。
- 新增讨论模型会追加到每一轮;删除会从每一轮移除。
- 模板存储于 `chat_discussion_templates`,接口前缀 `/api/v1/admin/discussion-templates`。
@@ -48,11 +48,11 @@
`Service.StreamDiscussionMessage` 的顺序:
-1. `ResolvePlan` 解析模板与绑定,产出运行时的 `Rounds`(含每步绑定的模型)与 `Summary`。
+1. `ResolvePlan` 解析模板与绑定,产出运行时的 `Rounds`(含每步绑定的模型、执行方式)与 `Summary`。
2. 复用普通消息的建对逻辑,写入 user / assistant 两条消息,会话模型记为 `discussion:
`。
3. 沿当前分支取本次提问之前的对话历史(`ListMessageAncestors`),以**真实 user/assistant 消息**的形式放进 prompt,与普通对话的多轮结构一致。之前的讨论只以「你的提问 + 最终总结」出现一次,各模型的逐轮发言(`discussion_json`)不进入历史。
-4. 逐轮执行:同一轮内所有步骤 `go` 并发,整轮 `wg.Wait()` 后才进入下一轮。
-5. 每轮的 messages = `system(该轮 prompt)` + 历史消息 + 当前 user(本次问题 + 本轮之前的讨论记录);第 1 轮没有本轮记录。
+4. 逐轮执行:并行轮次共享同一份此前轮次 transcript,整轮完成后进入下一轮;串行轮次按模型顺序逐个执行,每次成功后立即把发言加入下一个模型的 transcript。
+5. 每轮的 messages = `system(该轮 prompt + 执行方式说明)` + 历史消息 + 当前 user(本次问题 + 当前模型可见的讨论记录)。串行上下文标题明确包含「本轮在你之前」,并行明确不包含同轮其他模型。
6. 讨论结束后调用总结模型(同样带历史);总结失败时用讨论记录拼出回退正文,而不是把整条消息判为失败。
7. 历史按 `ContextMaxTurns` 限制条数,并在 `generateDiscussion` 里用 `fitGenerateInputToModelBudget` 按**当前模型**的上下文预算裁掉最老的轮次(保留 system 与本次问题),与普通对话同一套规则。
8. 结束时把完整状态写入 `chat_messages.discussion_json`,并把汇总用量写入 assistant 消息。
@@ -302,3 +302,18 @@
- 后端会话包与讨论领域测试、Go 质量检查通过。
- 前端 `pnpm --filter @deeix/web check`、`pnpm --filter @deeix/web build` 通过。
- reducer 回放验证 start/delta 保留结构化轮次与总结 Prompt。
+
+## 16. v0.4.6-fork.19 变更明细
+
+### 16.1 每轮可配置并行或串行
+
+- 后台每轮新增「并行 / 串行」开关,默认并行;拖拽、保存与新增模型不会重置该选择。
+- 串行按模型 1、2、3…N 的占位符顺序执行,不依赖 steps 存储顺序。
+- 并行轮次共享此前轮次记录;串行轮次每次成功后立即把同轮发言加入后续模型上下文。
+- 两种模式分别追加执行说明,串行强调针对具体分歧回应、指出假设和证据问题,避免无意义重复或抬杠。
+- 后台工作流与讨论消息显示每轮执行方式;缺少 `parallel` 的旧配置和旧记录默认并行。
+
+### 16.2 验证
+
+- 测试覆盖串行上下文逐模型递增、并行同轮隔离、显式串行配置保留和模型顺序。
+- 后端会话包、讨论领域测试及前端检查通过。