feat(dingtalk): enable secure multimodal image QA

This commit is contained in:
姚章浩
2026-07-16 00:17:16 +08:00
parent 4022040e98
commit c9c4f0aaa5
59 changed files with 3279 additions and 429 deletions
+36 -38
View File
@@ -122,7 +122,7 @@ func (c *RemoteAPIChat) processStream(
for {
response, err := stream.Recv()
if err != nil {
if err == io.EOF {
if err == io.EOF && state.lastFinishReason != "" {
logUsage(ctx, c.modelName, state.usage)
toolCalls := state.buildOrderedToolCalls()
streamChan <- types.StreamResponse{
@@ -134,9 +134,10 @@ func (c *RemoteAPIChat) processStream(
FinishReason: state.lastFinishReason,
}
} else {
logger.Warnf(ctx, "Chat completion stream ended before finish reason")
streamChan <- types.StreamResponse{
ResponseType: types.ResponseTypeError,
Content: err.Error(),
Content: "chat completion stream failed",
Done: true,
}
}
@@ -178,23 +179,26 @@ func (c *RemoteAPIChat) processRawHTTPStream(
for {
event, err := reader.ReadEvent()
if err != nil {
if err == io.EOF {
if err == io.EOF && state.lastFinishReason != "" {
logUsage(ctx, c.modelName, state.usage)
toolCalls := state.buildOrderedToolCalls()
streamChan <- types.StreamResponse{
ResponseType: types.ResponseTypeAnswer,
Content: "",
Done: true,
ToolCalls: toolCalls,
ToolCalls: state.buildOrderedToolCalls(),
Usage: state.usage,
FinishReason: state.lastFinishReason,
}
return
}
if err != io.EOF {
logger.Warnf(ctx, "Chat stream read failed")
} else {
logger.Errorf(ctx, "Stream read error: %v", err)
streamChan <- types.StreamResponse{
ResponseType: types.ResponseTypeError,
Content: err.Error(),
Done: true,
}
logger.Warnf(ctx, "Chat stream ended before finish reason")
}
streamChan <- types.StreamResponse{
ResponseType: types.ResponseTypeError,
Content: "chat stream ended before completion",
Done: true,
}
return
}
@@ -204,6 +208,14 @@ func (c *RemoteAPIChat) processRawHTTPStream(
}
if event.Done {
if state.lastFinishReason == "" {
streamChan <- types.StreamResponse{
ResponseType: types.ResponseTypeError,
Content: "chat stream ended before completion",
Done: true,
}
return
}
logUsage(ctx, c.modelName, state.usage)
toolCalls := state.buildOrderedToolCalls()
streamChan <- types.StreamResponse{
@@ -212,6 +224,7 @@ func (c *RemoteAPIChat) processRawHTTPStream(
Done: true,
ToolCalls: toolCalls,
Usage: state.usage,
FinishReason: state.lastFinishReason,
}
return
}
@@ -241,8 +254,13 @@ func (c *RemoteAPIChat) processRawHTTPStream(
}
if err := json.Unmarshal(event.Data, &streamResp); err != nil {
logger.Errorf(ctx, "Failed to parse stream response: %v", err)
continue
logger.Warnf(ctx, "Failed to parse chat stream response")
streamChan <- types.StreamResponse{
ResponseType: types.ResponseTypeError,
Content: "chat stream response was invalid",
Done: true,
}
return
}
if streamResp.Usage != nil {
@@ -453,38 +471,18 @@ func (c *RemoteAPIChat) processStreamDelta(
streamChan <- types.StreamResponse{
ResponseType: types.ResponseTypeAnswer,
Content: delta.Content,
Done: isDone,
Done: false,
ToolCalls: state.buildOrderedToolCalls(),
FinishReason: string(choice.FinishReason),
}
}
if isDone && len(state.toolCallMap) > 0 {
streamChan <- types.StreamResponse{
ResponseType: types.ResponseTypeAnswer,
Content: "",
Done: true,
ToolCalls: state.buildOrderedToolCalls(),
FinishReason: string(choice.FinishReason),
}
}
// Ensure thinking done is sent when stream finishes without any answer content
// (e.g., model only produced reasoning then hit finish_reason with empty content).
// Ensure thinking done is sent when the provider supplied a finish reason.
// The outer stream reader emits the sole terminal answer after it confirms
// the provider ended successfully.
if isDone {
state.finish(streamChan)
}
// Catch-all: isDone but none of the above branches sent a response with
// FinishReason (empty content, no tool calls, no thinking). This prevents
// the finish_reason from being lost in the streaming pipeline.
if isDone && delta.Content == "" && len(state.toolCallMap) == 0 {
streamChan <- types.StreamResponse{
ResponseType: types.ResponseTypeAnswer,
Done: true,
FinishReason: string(choice.FinishReason),
}
}
}
// processToolCallsDelta 处理 tool calls 的增量更新