fix(text): 文本生成 - 修复声明式协议流式请求与增量输出 (#558)
Original-Commit: b2fef297e8a50d519a0480d120a32eb81234a48f fix(text): 文本生成 - 修复声明式协议流式请求与增量输出
This commit is contained in:
1 parent
b57767eb8b
commit
6a3e454141
5 files changed
+178
-2
No files matched your search
@@ -728,6 +728,46 @@ func runTextTask(ctx context.Context, input canvasGenerationInput) (map[string]i
|
||||
return runLegacyTextTask(ctx, input)
|
||||
}
|
||||
|
||||
// Known text protocols keep the plugin's request mapping and host transport,
|
||||
// while sharing the SSE parser used by text generation and Agent requests.
|
||||
func executeProtocolCreateRequest(ctx context.Context, input canvasGenerationInput, spec protocol.RequestSpec) ([]byte, *protocol.Result, error) {
|
||||
wire := input.Config.InterfaceType
|
||||
if wire == string(model.ChannelInterfaceOpenAIResponse) {
|
||||
wire = "responses"
|
||||
}
|
||||
if input.Mode != "text" || !input.StreamText || (wire != "chat-completion" && wire != "responses" && wire != "claude-api") {
|
||||
data, err := executeProtocolRequest(ctx, input.Config, spec)
|
||||
return data, nil, err
|
||||
}
|
||||
body := protocolBodyObject(spec.Body)
|
||||
if body == nil {
|
||||
return nil, nil, errors.New("声明式流式文本请求体必须是 JSON 对象")
|
||||
}
|
||||
body["stream"] = true
|
||||
if wire == "chat-completion" {
|
||||
if err := ensureChatCompletionStreamUsage(body); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
}
|
||||
spec.Body = body
|
||||
parser := newStreamingAgentParser(wire, input.OnTextDelta)
|
||||
parser.emitReasoning = input.OnReasoningDelta
|
||||
data, mimeType, err := executeProtocolBinaryRequestWithConsumer(ctx, input.Config, spec, parser.consume)
|
||||
if err != nil || !strings.Contains(strings.ToLower(mimeType), "event-stream") {
|
||||
return data, nil, err
|
||||
}
|
||||
parser.flush()
|
||||
parsed, err := parser.result()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
text := stringField(parsed, "text")
|
||||
if text == "" {
|
||||
return nil, nil, errors.New("流式文本接口没有返回内容")
|
||||
}
|
||||
return data, &protocol.Result{Text: text, Reasoning: stringField(parsed, "reasoning")}, nil
|
||||
}
|
||||
|
||||
func runLegacyTextTask(ctx context.Context, input canvasGenerationInput) (map[string]interface{}, error) {
|
||||
responseInput, err := textResponseInput(input)
|
||||
if err != nil {
|
||||
|
||||
Reference in new issue
Block a user