perf(*): 创作工作台 - 优化首页按需加载与画布交互并修复历史迁移冲突
This commit is contained in:
1 parent
165b4443ab
commit
58f01d1eb6
107 files changed
+3025
-1089
No files matched your search
@@ -46,6 +46,7 @@ type canvasGenerationInput struct {
|
||||
Mask *providerMedia `json:"mask"`
|
||||
Metadata map[string]interface{} `json:"metadata"`
|
||||
AgentRequests *agentToolRequests `json:"agentRequests"`
|
||||
TextOptions canvasTextOptions `json:"textOptions"`
|
||||
ImageCapability *ImageCapabilityConfig `json:"-"`
|
||||
StreamText bool `json:"-"` // 分镜请求使用上游 SSE 保活;最终结构仍在流结束后统一校验。
|
||||
MaxOutputTokens int `json:"-"`
|
||||
@@ -53,6 +54,11 @@ type canvasGenerationInput struct {
|
||||
VideoCapability *VideoCapabilityConfig `json:"-"`
|
||||
}
|
||||
|
||||
type canvasTextOptions struct {
|
||||
Stream *bool `json:"stream"`
|
||||
Thinking bool `json:"thinking"`
|
||||
}
|
||||
|
||||
type agentToolRequests struct {
|
||||
Responses map[string]interface{} `json:"responses"`
|
||||
ChatCompletion map[string]interface{} `json:"chatCompletion"`
|
||||
@@ -335,8 +341,10 @@ func (s *Service) processCanvasGenerationTask(ctx context.Context, userID string
|
||||
ctx = withProviderOutboundPolicy(ctx, input.Config)
|
||||
var textPublisher *taskTextStreamPublisher
|
||||
if input.Mode == "text" && strings.HasPrefix(taskType, "canvas_text") {
|
||||
input.StreamText = input.TextOptions.Stream == nil || *input.TextOptions.Stream
|
||||
}
|
||||
if input.Mode == "text" && strings.HasPrefix(taskType, "canvas_text") && input.StreamText {
|
||||
textPublisher = newTaskTextStreamPublisher(s, userID, taskExecutionID(ctx))
|
||||
input.StreamText = true
|
||||
input.OnTextDelta = textPublisher.Publish
|
||||
defer textPublisher.Close()
|
||||
}
|
||||
@@ -673,6 +681,8 @@ func parseAgentToolPayload(payload map[string]interface{}, protocol string) (map
|
||||
switch stringField(item, "type") {
|
||||
case "text":
|
||||
result["text"] = result["text"].(string) + stringField(item, "text")
|
||||
case "thinking":
|
||||
result["reasoning"] = resultString(result, "reasoning") + firstNonEmptyString(stringField(item, "thinking"), stringField(item, "text"))
|
||||
case "tool_use":
|
||||
arguments, err := json.Marshal(item["input"])
|
||||
if err != nil {
|
||||
@@ -885,6 +895,8 @@ func (p *streamingAgentParser) consumeClaudeEvent(payload map[string]interface{}
|
||||
switch stringField(block, "type") {
|
||||
case "text":
|
||||
p.appendText(stringField(block, "text"))
|
||||
case "thinking":
|
||||
p.reasoning.WriteString(firstNonEmptyString(stringField(block, "thinking"), stringField(block, "text")))
|
||||
case "tool_use":
|
||||
arguments := ""
|
||||
if input := block["input"]; input != nil {
|
||||
@@ -900,6 +912,9 @@ func (p *streamingAgentParser) consumeClaudeEvent(payload map[string]interface{}
|
||||
if stringField(delta, "type") == "text_delta" {
|
||||
p.appendText(stringField(delta, "text"))
|
||||
}
|
||||
if stringField(delta, "type") == "thinking_delta" {
|
||||
p.reasoning.WriteString(firstNonEmptyString(stringField(delta, "thinking"), stringField(delta, "text")))
|
||||
}
|
||||
if stringField(delta, "type") == "input_json_delta" {
|
||||
p.toolCall(index).arguments += stringField(delta, "partial_json")
|
||||
}
|
||||
@@ -909,6 +924,11 @@ func (p *streamingAgentParser) consumeClaudeEvent(payload map[string]interface{}
|
||||
}
|
||||
}
|
||||
|
||||
func resultString(value map[string]interface{}, key string) string {
|
||||
text, _ := value[key].(string)
|
||||
return text
|
||||
}
|
||||
|
||||
func (p *streamingAgentParser) appendText(delta string) {
|
||||
if delta == "" {
|
||||
return
|
||||
@@ -2006,8 +2026,9 @@ func runLegacyTextTask(ctx context.Context, input canvasGenerationInput) (map[st
|
||||
return nil, err
|
||||
}
|
||||
body := map[string]interface{}{"model": input.Config.Model, "input": responseInput}
|
||||
applyTextThinking(body, input, "responses")
|
||||
applyTextOutputLimit(body, input.MaxOutputTokens, "max_output_tokens")
|
||||
text, err := requestTextProvider(ctx, input.Config, "/responses", body, "responses", input.StreamText, input.OnTextDelta)
|
||||
result, err := requestTextProvider(ctx, input.Config, "/responses", body, "responses", input.StreamText, input.OnTextDelta)
|
||||
if err != nil {
|
||||
if !shouldFallbackTextToChat(err) {
|
||||
return nil, err
|
||||
@@ -2018,7 +2039,7 @@ func runLegacyTextTask(ctx context.Context, input canvasGenerationInput) (map[st
|
||||
}
|
||||
return nil, fmt.Errorf("文本接口请求失败:Responses API %v;Chat Completions %v", err, chatErr)
|
||||
}
|
||||
return map[string]interface{}{"mode": "text", "text": text}, nil
|
||||
return providerTextTaskResult(result), nil
|
||||
}
|
||||
|
||||
func runResponsesTextTask(ctx context.Context, input canvasGenerationInput) (map[string]interface{}, error) {
|
||||
@@ -2027,12 +2048,13 @@ func runResponsesTextTask(ctx context.Context, input canvasGenerationInput) (map
|
||||
return nil, err
|
||||
}
|
||||
body := map[string]interface{}{"model": input.Config.Model, "input": responseInput}
|
||||
applyTextThinking(body, input, "responses")
|
||||
applyTextOutputLimit(body, input.MaxOutputTokens, "max_output_tokens")
|
||||
text, err := requestTextProvider(ctx, input.Config, "/responses", body, "responses", input.StreamText, input.OnTextDelta)
|
||||
result, err := requestTextProvider(ctx, input.Config, "/responses", body, "responses", input.StreamText, input.OnTextDelta)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]interface{}{"mode": "text", "text": text}, nil
|
||||
return providerTextTaskResult(result), nil
|
||||
}
|
||||
|
||||
func runChatCompletionsTextTask(ctx context.Context, input canvasGenerationInput) (map[string]interface{}, error) {
|
||||
@@ -2047,12 +2069,13 @@ func runChatCompletionsTextTask(ctx context.Context, input canvasGenerationInput
|
||||
}
|
||||
messages = append(messages, map[string]interface{}{"role": "user", "content": userContent})
|
||||
body := map[string]interface{}{"model": input.Config.Model, "messages": messages}
|
||||
applyTextThinking(body, input, "chat-completion")
|
||||
applyTextOutputLimit(body, input.MaxOutputTokens, "max_tokens")
|
||||
text, err := requestTextProvider(ctx, input.Config, "/chat/completions", body, "chat-completion", input.StreamText, input.OnTextDelta)
|
||||
result, err := requestTextProvider(ctx, input.Config, "/chat/completions", body, "chat-completion", input.StreamText, input.OnTextDelta)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]interface{}{"mode": "text", "text": text}, nil
|
||||
return providerTextTaskResult(result), nil
|
||||
}
|
||||
|
||||
func runClaudeTextTask(ctx context.Context, input canvasGenerationInput) (map[string]interface{}, error) {
|
||||
@@ -2073,14 +2096,42 @@ func runClaudeTextTask(ctx context.Context, input canvasGenerationInput) (map[st
|
||||
maxTokens = input.MaxOutputTokens
|
||||
}
|
||||
body := map[string]interface{}{"model": input.Config.Model, "max_tokens": maxTokens, "messages": messages}
|
||||
applyTextThinking(body, input, "claude-api")
|
||||
if systemPrompt := strings.TrimSpace(input.Config.SystemPrompt); systemPrompt != "" {
|
||||
body["system"] = systemPrompt
|
||||
}
|
||||
text, err := requestTextProvider(ctx, input.Config, "/messages", body, "claude-api", input.StreamText, input.OnTextDelta)
|
||||
result, err := requestTextProvider(ctx, input.Config, "/messages", body, "claude-api", input.StreamText, input.OnTextDelta)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]interface{}{"mode": "text", "text": text}, nil
|
||||
return providerTextTaskResult(result), nil
|
||||
}
|
||||
|
||||
func applyTextThinking(body map[string]interface{}, input canvasGenerationInput, protocol string) {
|
||||
if !input.TextOptions.Thinking {
|
||||
return
|
||||
}
|
||||
switch protocol {
|
||||
case "responses":
|
||||
body["reasoning"] = map[string]interface{}{"effort": "medium", "summary": "auto"}
|
||||
case "chat-completion":
|
||||
body["reasoning_effort"] = "medium"
|
||||
case "claude-api":
|
||||
body["thinking"] = map[string]interface{}{"type": "enabled", "budget_tokens": 1024}
|
||||
}
|
||||
}
|
||||
|
||||
type providerTextResult struct {
|
||||
Text string
|
||||
Reasoning string
|
||||
}
|
||||
|
||||
func providerTextTaskResult(result providerTextResult) map[string]interface{} {
|
||||
payload := map[string]interface{}{"mode": "text", "text": result.Text}
|
||||
if strings.TrimSpace(result.Reasoning) != "" {
|
||||
payload["reasoning"] = result.Reasoning
|
||||
}
|
||||
return payload
|
||||
}
|
||||
|
||||
func applyTextOutputLimit(body map[string]interface{}, limit int, field string) {
|
||||
@@ -4564,51 +4615,41 @@ func runSeedanceAgentPlanVideoTask(ctx context.Context, input canvasGenerationIn
|
||||
return nil, fmt.Errorf("%s视频生成超时", providerName)
|
||||
}
|
||||
|
||||
func requestTextProvider(ctx context.Context, config providerConfig, path string, body map[string]interface{}, protocol string, stream bool, onDelta func(string)) (string, error) {
|
||||
func requestTextProvider(ctx context.Context, config providerConfig, path string, body map[string]interface{}, protocol string, stream bool, onDelta func(string)) (providerTextResult, error) {
|
||||
if stream {
|
||||
metadata, _ := ctx.Value(providerAnalyticsKey{}).(providerAnalyticsContext)
|
||||
if protocol == "chat-completion" && metadata.BillingMode == "token" {
|
||||
if err := ensureChatCompletionStreamUsage(body); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
return postStreamingText(ctx, config, path, body, protocol, onDelta)
|
||||
return postStreamingTextResult(ctx, config, path, body, protocol, onDelta)
|
||||
}
|
||||
var payload map[string]interface{}
|
||||
if err := postJSON(ctx, config, path, body, &payload); err != nil {
|
||||
return "", err
|
||||
return providerTextResult{}, err
|
||||
}
|
||||
text := extractTextPayload(payload, protocol)
|
||||
if text == "" {
|
||||
return "", errors.New("文本接口没有返回内容")
|
||||
parsed, err := parseAgentToolPayload(payload, protocol)
|
||||
if err != nil {
|
||||
return providerTextResult{}, err
|
||||
}
|
||||
return text, nil
|
||||
result := providerTextResult{Text: stringField(parsed, "text"), Reasoning: stringField(parsed, "reasoning")}
|
||||
if result.Text == "" {
|
||||
return providerTextResult{}, errors.New("文本接口没有返回内容")
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func postStreamingText(ctx context.Context, config providerConfig, path string, body map[string]interface{}, protocol string, onDelta func(string)) (string, error) {
|
||||
// 只把分镜规划/修复切到上游 SSE,完整 JSON 仍在流结束后校验,避免半截结构污染画布。
|
||||
body["stream"] = true
|
||||
parser := newStreamingTextDeltaParser(protocol, onDelta)
|
||||
data, mimeType, err := postStreamingBinary(ctx, config, path, body, parser.consume)
|
||||
result, err := postStreamingTextResult(ctx, config, path, body, protocol, onDelta)
|
||||
return result.Text, err
|
||||
}
|
||||
|
||||
func postStreamingTextResult(ctx context.Context, config providerConfig, path string, body map[string]interface{}, protocol string, onDelta func(string)) (providerTextResult, error) {
|
||||
// 文本创作与 Agent 共用同一套 SSE 解析,确保正文、推理摘要和供应商错误语义一致。
|
||||
parsed, err := postStreamingAgent(ctx, config, path, body, protocol, onDelta)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return providerTextResult{}, err
|
||||
}
|
||||
parser.flush()
|
||||
if !strings.Contains(strings.ToLower(mimeType), "event-stream") {
|
||||
var payload map[string]interface{}
|
||||
if err := json.Unmarshal(data, &payload); err != nil {
|
||||
return "", fmt.Errorf("流式文本接口返回格式无效:%w", err)
|
||||
}
|
||||
if err := validateTextPayload(payload); err != nil {
|
||||
return "", err
|
||||
}
|
||||
text := extractTextPayload(payload, protocol)
|
||||
if text == "" {
|
||||
return "", errors.New("文本接口没有返回内容")
|
||||
}
|
||||
return text, nil
|
||||
result := providerTextResult{Text: stringField(parsed, "text"), Reasoning: stringField(parsed, "reasoning")}
|
||||
if result.Text == "" {
|
||||
return providerTextResult{}, errors.New("流式文本接口没有返回内容")
|
||||
}
|
||||
return parseTextEventStream(data, protocol)
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func extractTextPayload(payload map[string]interface{}, protocol string) string {
|
||||
|
||||
Reference in new issue
Block a user