Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Handle missing Gemini interaction IDs https://github.com/siyuan-note/siyuan/issues/20560

Daniel2b8a11b81059
diff --git a/kernel/util/gemini_interactions_stream.go b/kernel/util/gemini_interactions_stream.go
index 331e6a3022..9b2c889f2e 100644
--- a/kernel/util/gemini_interactions_stream.go
+++ b/kernel/util/gemini_interactions_stream.go
@@ -104,9 +104,12 @@ func (s *geminiInteractionsStream) recv() (ret openai.ChatCompletionStreamRespon
delta := openai.ChatCompletionStreamChoiceDelta{}
switch event.Type {
case "interaction.created":
- if s.started || json.Unmarshal(event.Interaction, &s.response) != nil || s.response.ID == "" || len(s.response.Steps) != 0 {
+ var created *geminiInteraction
+ if s.started || json.Unmarshal(event.Interaction, &created) != nil || created == nil || len(created.Steps) != 0 {
return ret, errors.New("invalid Gemini interaction.created")
}
+ // store=false 的响应可能没有交互 ID,通过当前流及步骤顺序关联后续事件。
+ s.response = *created
s.started = true
case "step.start", "step.delta", "step.stop":
s.size += len(data)
@@ -212,10 +215,14 @@ func (s *geminiInteractionsStream) recv() (ret openai.ChatCompletionStreamRespon
return ret, fmt.Errorf("unsupported Gemini delta %q", event.Delta.Type)
}
case "interaction.completed":
- var completed geminiInteraction
- if json.Unmarshal(event.Interaction, &completed) != nil || completed.ID != s.response.ID {
+ var completed *geminiInteraction
+ if json.Unmarshal(event.Interaction, &completed) != nil || completed == nil ||
+ (completed.ID != "" && s.response.ID != "" && completed.ID != s.response.ID) {
return ret, errors.New("invalid Gemini interaction.completed")
}
+ if s.response.ID == "" {
+ s.response.ID = completed.ID
+ }
for _, step := range s.steps {
if !step.stopped {
return ret, errors.New("Gemini interaction ended with an incomplete step")
@@ -241,6 +248,7 @@ func (s *geminiInteractionsStream) recv() (ret openai.ChatCompletionStreamRespon
s.content = &AIMessageContent{Protocol: GeminiProtocolInteractions, Version: geminiContentVersion,
Blocks: CloneOpenAIResponseOutput(s.response.Steps)}
// 只有完整且成功的终止事件才能向 Agent 交付工具调用。
+ ret.ID = s.response.ID
ret.Usage = &completion.Usage
ret.Choices = []openai.ChatCompletionStreamChoice{{Index: 0, FinishReason: completion.Choices[0].FinishReason,
Delta: openai.ChatCompletionStreamChoiceDelta{ToolCalls: completion.Choices[0].Message.ToolCalls}}}
diff --git a/kernel/util/gemini_interactions_test.go b/kernel/util/gemini_interactions_test.go
index 953a8a37b4..466b46d897 100644
--- a/kernel/util/gemini_interactions_test.go
+++ b/kernel/util/gemini_interactions_test.go
@@ -75,6 +75,55 @@ func TestGeminiInteractionsStreamNativeHistory(t *testing.T) {
}
}
+func TestGeminiInteractionsStreamOptionalInteractionID(t *testing.T) {
+ for _, test := range []struct {
+ name string
+ created string
+ completed string
+ wantID string
+ }{
+ {name: "empty", created: `"id":"",`, completed: `"id":"",`},
+ {name: "omitted"},
+ {name: "created-only", created: `"id":"i1",`, wantID: "i1"},
+ {name: "completed-only", completed: `"id":"i1",`, wantID: "i1"},
+ } {
+ t.Run(test.name, func(t *testing.T) {
+ stream := geminiTestStream(
+ `{"event_type":"interaction.created","interaction":{`+test.created+`"status":"in_progress","object":"interaction","model":"gemini-4-argon"}}`,
+ `{"event_type":"step.start","index":0,"step":{"type":"thought"}}`,
+ `{"event_type":"step.delta","index":0,"delta":{"type":"thought_summary","content":{"type":"text","text":"reason"}}}`,
+ `{"event_type":"step.delta","index":0,"delta":{"type":"thought_signature","signature":"opaque"}}`,
+ `{"event_type":"step.stop","index":0}`,
+ `{"event_type":"step.start","index":1,"step":{"type":"function_call","id":"call1","name":"lookup","arguments":{}}}`,
+ `{"event_type":"step.stop","index":1}`,
+ `{"event_type":"interaction.completed","interaction":{`+test.completed+`"status":"requires_action"}}`)
+ defer stream.Close()
+ reasoning, calls := "", 0
+ for {
+ chunk, err := stream.Recv()
+ if errors.Is(err, io.EOF) {
+ break
+ }
+ if err != nil {
+ t.Fatal(err)
+ }
+ for _, choice := range chunk.Choices {
+ reasoning += choice.Delta.ReasoningContent
+ calls += len(choice.Delta.ToolCalls)
+ if len(choice.Delta.ToolCalls) > 0 && (choice.FinishReason != openai.FinishReasonToolCalls || chunk.ID != test.wantID) {
+ t.Fatalf("invalid terminal tool response: %+v", chunk)
+ }
+ }
+ }
+ native := stream.NativeContent()
+ if reasoning != "reason" || calls != 1 || native == nil || len(native.Blocks) != 2 ||
+ !strings.Contains(string(native.Blocks[0]), "opaque") {
+ t.Fatalf("lost stateless output: reasoning=%q calls=%d native=%+v", reasoning, calls, native)
+ }
+ })
+ }
+}
+
func TestGeminiInteractionsParallelToolsAndReplay(t *testing.T) {
stream := geminiTestStream(geminiTestCreated,
`{"event_type":"step.start","index":0,"step":{"type":"thought","signature":"opaque"}}`,
@@ -148,6 +197,12 @@ func TestGeminiInteractionsParallelToolsAndReplay(t *testing.T) {
func TestGeminiInteractionsRejectIncompleteStreams(t *testing.T) {
for name, events := range map[string][]string{
+ "null-created": {`{"event_type":"interaction.created","interaction":null}`},
+ "missing-created-body": {`{"event_type":"interaction.created"}`},
+ "duplicate-created": {geminiTestCreated, geminiTestCreated},
+ "created-with-output": {`{"event_type":"interaction.created","interaction":{"id":"i1","steps":[{"type":"thought"}]}}`},
+ "null-completed": {geminiTestCreated, `{"event_type":"interaction.completed","interaction":null}`},
+ "mismatched-id": {geminiTestCreated, `{"event_type":"interaction.completed","interaction":{"id":"different","status":"completed"}}`},
"eof": {geminiTestCreated},
"done-without-terminal": {geminiTestCreated, `[DONE]`},
"missing-start": {geminiTestCompleted},