|
| 1 | +package openai |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/mudler/LocalAI/core/schema" |
| 5 | + . "github.com/onsi/ginkgo/v2" |
| 6 | + . "github.com/onsi/gomega" |
| 7 | +) |
| 8 | + |
| 9 | +// drainChannel reads everything currently buffered on a channel without |
| 10 | +// blocking on close. The helper test channels are sized for the assertions. |
| 11 | +func drainChannel(ch <-chan schema.OpenAIResponse) []schema.OpenAIResponse { |
| 12 | + var out []schema.OpenAIResponse |
| 13 | + for { |
| 14 | + select { |
| 15 | + case r, ok := <-ch: |
| 16 | + if !ok { |
| 17 | + return out |
| 18 | + } |
| 19 | + out = append(out, r) |
| 20 | + default: |
| 21 | + return out |
| 22 | + } |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +// nameOf returns the name of the first tool call carried on the choice's |
| 27 | +// delta, or "" if none. |
| 28 | +func nameOf(r schema.OpenAIResponse) string { |
| 29 | + if len(r.Choices) == 0 || r.Choices[0].Delta == nil { |
| 30 | + return "" |
| 31 | + } |
| 32 | + if len(r.Choices[0].Delta.ToolCalls) == 0 { |
| 33 | + return "" |
| 34 | + } |
| 35 | + return r.Choices[0].Delta.ToolCalls[0].FunctionCall.Name |
| 36 | +} |
| 37 | + |
| 38 | +var _ = Describe("emitJSONToolCallDeltas", func() { |
| 39 | + const ( |
| 40 | + id = "test-stream" |
| 41 | + model = "test-model" |
| 42 | + created = 1700000000 |
| 43 | + ) |
| 44 | + |
| 45 | + // The case that motivated this helper. With the previous version of |
| 46 | + // the streaming worker, ParseJSONIterative would hand back a stub |
| 47 | + // object like `{"4310046988783340008":1}` after the model had only |
| 48 | + // emitted `{`. The worker bumped lastEmittedCount unconditionally, |
| 49 | + // which permanently gated off content emission for the rest of the |
| 50 | + // stream (qwen3-4b with stream:true + tools dribbled only `{"` to |
| 51 | + // the client and then nothing). See issue #9988. |
| 52 | + Context("partial stub without a usable name", func() { |
| 53 | + It("does NOT bump lastEmittedCount and emits nothing", func() { |
| 54 | + responses := make(chan schema.OpenAIResponse, 4) |
| 55 | + // What ParseJSONIterative used to return for `{`: |
| 56 | + stubResults := []map[string]any{ |
| 57 | + {"4310046988783340008": float64(1)}, |
| 58 | + } |
| 59 | + |
| 60 | + next := emitJSONToolCallDeltas(stubResults, 0, id, model, created, responses) |
| 61 | + |
| 62 | + Expect(next).To(Equal(0), |
| 63 | + "lastEmittedCount must NOT advance past a stub without a name "+ |
| 64 | + "— otherwise content emission gets permanently gated off") |
| 65 | + Expect(drainChannel(responses)).To(BeEmpty(), |
| 66 | + "no tool_call chunk should be emitted for a stub without a name") |
| 67 | + }) |
| 68 | + }) |
| 69 | + |
| 70 | + // No-regression #1: the autoparser-correctly-working path. When the |
| 71 | + // C++ autoparser classifies tool calls itself, the raw text result is |
| 72 | + // cleared and ParseJSONIterative on it returns no results — this |
| 73 | + // helper must be a no-op so the deferred end-of-stream code can emit |
| 74 | + // the tool calls from TokenUsage.ChatDeltas. |
| 75 | + Context("empty jsonResults (autoparser-correctly-working path)", func() { |
| 76 | + It("is a no-op and leaves lastEmittedCount unchanged", func() { |
| 77 | + responses := make(chan schema.OpenAIResponse, 4) |
| 78 | + next := emitJSONToolCallDeltas(nil, 0, id, model, created, responses) |
| 79 | + Expect(next).To(Equal(0)) |
| 80 | + Expect(drainChannel(responses)).To(BeEmpty()) |
| 81 | + }) |
| 82 | + |
| 83 | + It("leaves a non-zero lastEmittedCount unchanged when later called with the same length", func() { |
| 84 | + responses := make(chan schema.OpenAIResponse, 4) |
| 85 | + results := []map[string]any{ |
| 86 | + {"name": "search", "arguments": map[string]any{"q": "hi"}}, |
| 87 | + } |
| 88 | + // First call emits the one available tool call. |
| 89 | + next := emitJSONToolCallDeltas(results, 0, id, model, created, responses) |
| 90 | + Expect(next).To(Equal(1)) |
| 91 | + Expect(drainChannel(responses)).To(HaveLen(1)) |
| 92 | + |
| 93 | + // Subsequent chunks haven't grown the slice — must be a no-op. |
| 94 | + next = emitJSONToolCallDeltas(results, next, id, model, created, responses) |
| 95 | + Expect(next).To(Equal(1)) |
| 96 | + Expect(drainChannel(responses)).To(BeEmpty()) |
| 97 | + }) |
| 98 | + }) |
| 99 | + |
| 100 | + // No-regression #2: the normal completed-JSON path. When the model |
| 101 | + // emits a real, complete tool call as JSON in raw content (e.g. qwen3 |
| 102 | + // without jinja but with tools), we should emit exactly one tool_call |
| 103 | + // SSE chunk on the first call and become a no-op on later calls. |
| 104 | + Context("single complete tool call", func() { |
| 105 | + It("emits one tool_call chunk and bumps lastEmittedCount to 1", func() { |
| 106 | + responses := make(chan schema.OpenAIResponse, 4) |
| 107 | + results := []map[string]any{ |
| 108 | + { |
| 109 | + "name": "search", |
| 110 | + "arguments": map[string]any{ |
| 111 | + "q": "hello", |
| 112 | + }, |
| 113 | + }, |
| 114 | + } |
| 115 | + |
| 116 | + next := emitJSONToolCallDeltas(results, 0, id, model, created, responses) |
| 117 | + |
| 118 | + Expect(next).To(Equal(1)) |
| 119 | + out := drainChannel(responses) |
| 120 | + Expect(out).To(HaveLen(1)) |
| 121 | + Expect(nameOf(out[0])).To(Equal("search")) |
| 122 | + Expect(out[0].Choices[0].Delta.ToolCalls[0].FunctionCall.Arguments). |
| 123 | + To(ContainSubstring(`"q":"hello"`)) |
| 124 | + }) |
| 125 | + |
| 126 | + It("accepts arguments already serialized as a string", func() { |
| 127 | + responses := make(chan schema.OpenAIResponse, 4) |
| 128 | + results := []map[string]any{ |
| 129 | + { |
| 130 | + "name": "search", |
| 131 | + "arguments": `{"q":"hello"}`, |
| 132 | + }, |
| 133 | + } |
| 134 | + |
| 135 | + emitJSONToolCallDeltas(results, 0, id, model, created, responses) |
| 136 | + |
| 137 | + out := drainChannel(responses) |
| 138 | + Expect(out).To(HaveLen(1)) |
| 139 | + Expect(out[0].Choices[0].Delta.ToolCalls[0].FunctionCall.Arguments). |
| 140 | + To(Equal(`{"q":"hello"}`)) |
| 141 | + }) |
| 142 | + }) |
| 143 | + |
| 144 | + // No-regression #3: multiple tool calls (parallel tool calling). |
| 145 | + // Both must be emitted, lastEmittedCount must end at 2. |
| 146 | + Context("multiple complete tool calls", func() { |
| 147 | + It("emits one chunk per tool call and bumps lastEmittedCount to len(results)", func() { |
| 148 | + responses := make(chan schema.OpenAIResponse, 8) |
| 149 | + results := []map[string]any{ |
| 150 | + {"name": "search", "arguments": map[string]any{"q": "a"}}, |
| 151 | + {"name": "browse", "arguments": map[string]any{"url": "b"}}, |
| 152 | + } |
| 153 | + |
| 154 | + next := emitJSONToolCallDeltas(results, 0, id, model, created, responses) |
| 155 | + |
| 156 | + Expect(next).To(Equal(2)) |
| 157 | + out := drainChannel(responses) |
| 158 | + Expect(out).To(HaveLen(2)) |
| 159 | + Expect(nameOf(out[0])).To(Equal("search")) |
| 160 | + Expect(nameOf(out[1])).To(Equal("browse")) |
| 161 | + }) |
| 162 | + }) |
| 163 | + |
| 164 | + // The streaming-tail case: incremental chunks. First parse returns |
| 165 | + // one complete tool call followed by a partial stub; later chunks |
| 166 | + // complete the second tool call. We must emit the first immediately |
| 167 | + // and the second on the later call — without ever bumping past the |
| 168 | + // stub mid-stream. |
| 169 | + Context("partial tail behind a real tool call", func() { |
| 170 | + It("emits the complete entry, stops at the stub, and resumes once the tail completes", func() { |
| 171 | + responses := make(chan schema.OpenAIResponse, 8) |
| 172 | + |
| 173 | + // Chunk 1: one real call + a partial stub for the next. |
| 174 | + chunk1 := []map[string]any{ |
| 175 | + {"name": "search", "arguments": map[string]any{"q": "a"}}, |
| 176 | + {"4310046988783340008": float64(1)}, |
| 177 | + } |
| 178 | + next := emitJSONToolCallDeltas(chunk1, 0, id, model, created, responses) |
| 179 | + Expect(next).To(Equal(1), |
| 180 | + "must NOT advance to 2 — the stub at index 1 has no usable name") |
| 181 | + out := drainChannel(responses) |
| 182 | + Expect(out).To(HaveLen(1)) |
| 183 | + Expect(nameOf(out[0])).To(Equal("search")) |
| 184 | + |
| 185 | + // Chunk 2: the stub completes into a real call. |
| 186 | + chunk2 := []map[string]any{ |
| 187 | + {"name": "search", "arguments": map[string]any{"q": "a"}}, |
| 188 | + {"name": "browse", "arguments": map[string]any{"url": "b"}}, |
| 189 | + } |
| 190 | + next = emitJSONToolCallDeltas(chunk2, next, id, model, created, responses) |
| 191 | + Expect(next).To(Equal(2)) |
| 192 | + out = drainChannel(responses) |
| 193 | + Expect(out).To(HaveLen(1)) |
| 194 | + Expect(nameOf(out[0])).To(Equal("browse")) |
| 195 | + }) |
| 196 | + }) |
| 197 | +}) |
0 commit comments