From ce637eca812e47c3b80d6878c611362e05bb2f1b Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Sun, 4 Oct 2026 21:49:01 -0500 Subject: [PATCH] Lenshift openai_stream: assemble streamed responses, 23 fixtures from real streams (plan 19.7 item 6a) A streamed /v1/chat/completions body (SSE) is assembled into one assistant message and parsed with the openai rules. Incomplete streams reject every call as truncated, never guessed; malformed streams are rejected whole. Six fixtures are the real Ollama, llama.cpp and SGLang stream captures byte for byte. Both dialect runners now compare every recorded digest. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 1 + README.md | 2 +- STATUS.md | 1 + .../assembled-arguments-not-json.json | 28 +++ .../capture-llamacpp-no-reasoning.json | 51 ++++ .../capture-llamacpp-reasoning.json | 51 ++++ .../capture-ollama-no-reasoning.json | 51 ++++ .../capture-ollama-reasoning.json | 51 ++++ .../capture-sglang-no-reasoning.json | 51 ++++ .../capture-sglang-reasoning.json | 51 ++++ .../openai_stream/crlf-comments-no-space.json | 47 ++++ .../cut-after-complete-arguments.json | 28 +++ .../openai_stream/cut-mid-arguments.json | 28 +++ .../openai_stream/error-chunk-not-json.json | 28 +++ .../openai_stream/error-conflicting-name.json | 28 +++ .../openai_stream/error-event-after-done.json | 28 +++ .../error-fragment-without-index.json | 28 +++ .../openai_stream/error-input-not-text.json | 41 ++++ .../openai_stream/error-second-choice.json | 28 +++ .../fixtures/openai_stream/finish-length.json | 28 +++ .../openai_stream/fragmented-call.json | 47 ++++ .../openai_stream/plain-text-cut.json | 22 ++ .../openai_stream/plain-text-stop.json | 22 ++ .../openai_stream/sglang-null-repeats.json | 47 ++++ .../openai_stream/two-calls-interleaved.json | 70 ++++++ .../unterminated-last-event.json | 28 +++ python/interplane/lenshift/__init__.py | 3 +- python/interplane/lenshift/openai_stream.py | 153 ++++++++++++ python/tests/test_lenshift.py | 8 +- rust/crates/interplane-lenshift/src/lib.rs | 9 +- .../interplane-lenshift/src/openai_stream.rs | 230 ++++++++++++++++++ .../tests/dialect_fixtures.rs | 21 +- spec/LENSHIFT.md | 61 ++++- 33 files changed, 1363 insertions(+), 8 deletions(-) create mode 100644 dialects/fixtures/openai_stream/assembled-arguments-not-json.json create mode 100644 dialects/fixtures/openai_stream/capture-llamacpp-no-reasoning.json create mode 100644 dialects/fixtures/openai_stream/capture-llamacpp-reasoning.json create mode 100644 dialects/fixtures/openai_stream/capture-ollama-no-reasoning.json create mode 100644 dialects/fixtures/openai_stream/capture-ollama-reasoning.json create mode 100644 dialects/fixtures/openai_stream/capture-sglang-no-reasoning.json create mode 100644 dialects/fixtures/openai_stream/capture-sglang-reasoning.json create mode 100644 dialects/fixtures/openai_stream/crlf-comments-no-space.json create mode 100644 dialects/fixtures/openai_stream/cut-after-complete-arguments.json create mode 100644 dialects/fixtures/openai_stream/cut-mid-arguments.json create mode 100644 dialects/fixtures/openai_stream/error-chunk-not-json.json create mode 100644 dialects/fixtures/openai_stream/error-conflicting-name.json create mode 100644 dialects/fixtures/openai_stream/error-event-after-done.json create mode 100644 dialects/fixtures/openai_stream/error-fragment-without-index.json create mode 100644 dialects/fixtures/openai_stream/error-input-not-text.json create mode 100644 dialects/fixtures/openai_stream/error-second-choice.json create mode 100644 dialects/fixtures/openai_stream/finish-length.json create mode 100644 dialects/fixtures/openai_stream/fragmented-call.json create mode 100644 dialects/fixtures/openai_stream/plain-text-cut.json create mode 100644 dialects/fixtures/openai_stream/plain-text-stop.json create mode 100644 dialects/fixtures/openai_stream/sglang-null-repeats.json create mode 100644 dialects/fixtures/openai_stream/two-calls-interleaved.json create mode 100644 dialects/fixtures/openai_stream/unterminated-last-event.json create mode 100644 python/interplane/lenshift/openai_stream.py create mode 100644 rust/crates/interplane-lenshift/src/openai_stream.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index ae607e4..6d5158f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ - Adapter subset runners (plan cut R0, prerequisite for REPORT-0.3): the normative T3/T4 subset rule and fixture translation (`spec/CORE.md`, Adapter subsets; `conformance/adapter-translation.json`), `t3_subset` (43) and `t4_subset` (31) frozen in `TRUST-DIGEST.txt`, T3 runner `python -m interplane_adapter_odysseus.t3` and T4 runner `adapters/aien/tests/t4_corpus.rs` run through the real Pipeline and adapter and wired into CI; violations 0 and content-derived 0 over both subsets. - Protocol case A15 as a corpus fixture (plan cut R0b): `approval/17-provider-fails-after-approval` (approved effect fails at the provider after the approval was spent; reported, request terminal, second continuation refused). Mock `execute_raises` on T1/T2; on T4 a real write into a missing directory, rejected by the AIEN write provider. Corpus 106 rows, gate I 60 cases, T4 subset 32; T3 unchanged at 43. - Trust run tooling (plan cut R1a): both conformance runners write `.inputs.json` (full input ledger) next to `.results.json` under `--dump`, byte-identical across Rust and Python (the Rust results dump now nulls `duration_ms` like Python). `bench/tools/trust_run.py` runs every offline leg of PROTOCOL-0.3 (T1, T2, both negative-control matrices, validator, TRUST-DIGEST, T3, T4) and computes gates P, X, I, E, A, N from the raw files. The dev run found a spec-versus-protocol conflict: results the pipeline synthesizes after a raising or foreign-result `execute` carried null `content_kind` and `trust` (CORE.md said so), which gate P2 counts as missing. Both pipelines now label them `tool_result` + `unknown`, the schema reading of a null label, so behaviour is unchanged. +- Streamed responses (plan 19.7 item 6, streaming half): Lenshift dialect `openai_stream` in Rust and Python takes a streamed `/v1/chat/completions` body (Server-Sent Events) exactly as sent, assembles the fragments into one assistant message and parses it with the `openai` rules (`spec/LENSHIFT.md`). A stream without `finish_reason` `stop`, `tool_calls` or `function_call` rejects every call as "truncated tool call"; malformed streams (conflicting id or name, second choice, fragment without index, data after `[DONE]`, non-JSON chunk) are rejected whole. 23 dialect fixtures, six of them the real Ollama, llama.cpp and SGLang stream captures byte for byte. Both dialect fixture runners now also compare every digest a fixture records. ## Unreleased (0.2, CrossAxis lane) diff --git a/README.md b/README.md index 50f9a0d..05271fa 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ and execution. INTERPLANE only standardizes what crosses between them and makes | **Crossveil** | the authority-boundary contract: two runtime callbacks, a lifecycle with fail-closed invariants, trust classes | see `STATUS.md` | | **CrossAxis** | explicit, versioned capability mapping; a deterministic domain selector with a before/after receipt | minimal in 0.1 | | **Interplane Probe** | measured capability qualification of an OpenAI-compatible endpoint | see `STATUS.md` | -| **Conformance** | 25 canonical cases plus 31 dialect fixtures; Rust and Python runners must agree byte for byte | see `STATUS.md` | +| **Conformance** | 106 conformance cases plus 54 dialect fixtures; Rust and Python runners must agree byte for byte | see `STATUS.md` | | Vectorveil | the envelope and canonical JSON; no transport framework | envelope only | | RelayLine | the event vocabulary for multi-round exchanges | contract only | diff --git a/STATUS.md b/STATUS.md index 2dfffdc..f532bdd 100644 --- a/STATUS.md +++ b/STATUS.md @@ -9,6 +9,7 @@ byte-identical) · `qualified` (measured against a real model/backend). | Core schemas | specified | specified | validated | 10 schemas under spec/schemas, JSON Schema 2020-12 | | Core types + validation + JCS | conformant | conformant | 27 cases + digest/jcs-01 | verdict files byte-identical | | Lenshift openai | conformant | conformant | 8 dialect fixtures | | +| Lenshift openai_stream | conformant | conformant | 23 dialect fixtures (6 real streams from Ollama, llama.cpp, SGLang) | SSE body assembled into one message; incomplete streams rejected, never guessed; digests compared across languages | | Lenshift qwen35 | conformant, qualified | conformant, qualified | 10 dialect fixtures + real captures | real Qwen3.5-9B captures under qualification/captures | | Lenshift aien_legacy | tested | tested | 13 dialect fixtures, JCS-identical | aien-cli textual `` JSON (client.rs:381-419); unterminated calls rejected | | Lenshift ajax | reserved | reserved | none | no official artifact (2026-10-04) | diff --git a/dialects/fixtures/openai_stream/assembled-arguments-not-json.json b/dialects/fixtures/openai_stream/assembled-arguments-not-json.json new file mode 100644 index 0000000..72cf769 --- /dev/null +++ b/dialects/fixtures/openai_stream/assembled-arguments-not-json.json @@ -0,0 +1,28 @@ +{ + "dialect": "openai_stream", + "name": "assembled-arguments-not-json", + "description": "Complete stream whose assembled arguments are not JSON: the openai rule rejects it.", + "input": "data: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"name\":\"read_file\",\"arguments\":\"{\\\"path\\\": \"},\"id\":\"call_x\",\"type\":\"function\"}]},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"tool_calls\"}]}\n\ndata: [DONE]\n\n", + "model": "mock-model", + "trace_id": "trace-dialect", + "turn": 0, + "parser_version": "1.0.0", + "reasoning_present": false, + "expected": { + "text": "", + "intents": [], + "rejected": [ + { + "index": 0, + "code": "malformed_tool_call", + "message": "arguments is not valid JSON" + } + ], + "partial": false, + "dialect_version": "1" + }, + "expected_digests": { + "reasoning_digest": null, + "source_digests": [] + } +} diff --git a/dialects/fixtures/openai_stream/capture-llamacpp-no-reasoning.json b/dialects/fixtures/openai_stream/capture-llamacpp-no-reasoning.json new file mode 100644 index 0000000..f37edc0 --- /dev/null +++ b/dialects/fixtures/openai_stream/capture-llamacpp-no-reasoning.json @@ -0,0 +1,51 @@ +{ + "dialect": "openai_stream", + "name": "capture-llamacpp-no-reasoning", + "description": "Real llamacpp stream (Qwen3.5-9B), byte-identical to the capture's response body.", + "capture": { + "path": "qualification/captures/qwen35-llamacpp/h2-streaming-reasoning-effort-none.json", + "sha256_response": "63ac71e151f390838efaf63bfd665bd8c1dbf132f72cb2466f164e9b0fd5235e" + }, + "input": "data: {\"choices\":[{\"finish_reason\":null,\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":null}}],\"created\":1791150474,\"id\":\"chatcmpl-lIa2kwiprQNbvuqqkel7LEZy3IFWVHed\",\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"b1-a7b94df\",\"object\":\"chat.completion.chunk\"}\n\ndata: {\"choices\":[{\"finish_reason\":null,\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"id\":\"GK5nMMC5K2IRyLrrCixDAvn1Cwu93zFi\",\"type\":\"function\",\"function\":{\"name\":\"read_file\",\"arguments\":\"{\"}}]}}],\"created\":1791150474,\"id\":\"chatcmpl-lIa2kwiprQNbvuqqkel7LEZy3IFWVHed\",\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"b1-a7b94df\",\"object\":\"chat.completion.chunk\"}\n\ndata: {\"choices\":[{\"finish_reason\":null,\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"\\\"path\\\":\\\"\"}}]}}],\"created\":1791150475,\"id\":\"chatcmpl-lIa2kwiprQNbvuqqkel7LEZy3IFWVHed\",\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"b1-a7b94df\",\"object\":\"chat.completion.chunk\"}\n\ndata: {\"choices\":[{\"finish_reason\":null,\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"README\"}}]}}],\"created\":1791150475,\"id\":\"chatcmpl-lIa2kwiprQNbvuqqkel7LEZy3IFWVHed\",\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"b1-a7b94df\",\"object\":\"chat.completion.chunk\"}\n\ndata: {\"choices\":[{\"finish_reason\":null,\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\".md\"}}]}}],\"created\":1791150475,\"id\":\"chatcmpl-lIa2kwiprQNbvuqqkel7LEZy3IFWVHed\",\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"b1-a7b94df\",\"object\":\"chat.completion.chunk\"}\n\ndata: {\"choices\":[{\"finish_reason\":null,\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"\\\"\"}}]}}],\"created\":1791150475,\"id\":\"chatcmpl-lIa2kwiprQNbvuqqkel7LEZy3IFWVHed\",\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"b1-a7b94df\",\"object\":\"chat.completion.chunk\"}\n\ndata: {\"choices\":[{\"finish_reason\":null,\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"}\"}}]}}],\"created\":1791150476,\"id\":\"chatcmpl-lIa2kwiprQNbvuqqkel7LEZy3IFWVHed\",\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"b1-a7b94df\",\"object\":\"chat.completion.chunk\"}\n\ndata: {\"choices\":[{\"finish_reason\":\"tool_calls\",\"index\":0,\"delta\":{}}],\"created\":1791150476,\"id\":\"chatcmpl-lIa2kwiprQNbvuqqkel7LEZy3IFWVHed\",\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"b1-a7b94df\",\"object\":\"chat.completion.chunk\",\"timings\":{\"cache_n\":490,\"prompt_n\":4,\"prompt_ms\":209.528,\"prompt_per_token_ms\":52.382,\"prompt_per_second\":19.090527280363485,\"predicted_n\":26,\"predicted_ms\":2153.61,\"predicted_per_token_ms\":86.1444,\"predicted_per_second\":11.6084156370002}}\n\ndata: [DONE]\n\n", + "model": "mock-model", + "trace_id": "trace-dialect", + "turn": 0, + "parser_version": "1.0.0", + "reasoning_present": false, + "expected": { + "text": "", + "intents": [ + { + "kind": "tool_request", + "request_id": "GK5nMMC5K2IRyLrrCixDAvn1Cwu93zFi", + "tool": { + "name": "read_file", + "namespace": null + }, + "arguments": { + "path": "README.md" + }, + "provenance": { + "dialect": "openai_stream", + "dialect_version": "1", + "model": "mock-model", + "parser_version": "1.0.0", + "source_turn": 0, + "source_call_id": "GK5nMMC5K2IRyLrrCixDAvn1Cwu93zFi", + "source_digest": null, + "raw_name": "read_file", + "coercion": "none" + } + } + ], + "rejected": [], + "partial": false, + "dialect_version": "1" + }, + "expected_digests": { + "reasoning_digest": null, + "source_digests": [ + "sha256:1feb7a680de5357b3e9c0f1823f3416e0c851c78c213d4da6ffceb009c4c39b4" + ] + } +} diff --git a/dialects/fixtures/openai_stream/capture-llamacpp-reasoning.json b/dialects/fixtures/openai_stream/capture-llamacpp-reasoning.json new file mode 100644 index 0000000..4a0c82f --- /dev/null +++ b/dialects/fixtures/openai_stream/capture-llamacpp-reasoning.json @@ -0,0 +1,51 @@ +{ + "dialect": "openai_stream", + "name": "capture-llamacpp-reasoning", + "description": "Real llamacpp stream (Qwen3.5-9B), byte-identical to the capture's response body.", + "capture": { + "path": "qualification/captures/qwen35-llamacpp/h1-streaming.json", + "sha256_response": "9bee3fdacf241dae35b86ffd09ee1f968dfc99894d7858bb03b584fe62de2835" + }, + "input": "data: {\"choices\":[{\"finish_reason\":null,\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":null}}],\"created\":1791150473,\"id\":\"chatcmpl-haLWBtmmP6yo7kZFz7sGaEc4GN796icd\",\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"b1-a7b94df\",\"object\":\"chat.completion.chunk\"}\n\ndata: {\"choices\":[{\"finish_reason\":null,\"index\":0,\"delta\":{\"reasoning_content\":\"The\"}}],\"created\":1791150473,\"id\":\"chatcmpl-haLWBtmmP6yo7kZFz7sGaEc4GN796icd\",\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"b1-a7b94df\",\"object\":\"chat.completion.chunk\"}\n\ndata: {\"choices\":[{\"finish_reason\":null,\"index\":0,\"delta\":{\"reasoning_content\":\" user\"}}],\"created\":1791150473,\"id\":\"chatcmpl-haLWBtmmP6yo7kZFz7sGaEc4GN796icd\",\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"b1-a7b94df\",\"object\":\"chat.completion.chunk\"}\n\ndata: {\"choices\":[{\"finish_reason\":null,\"index\":0,\"delta\":{\"reasoning_content\":\" wants\"}}],\"created\":1791150473,\"id\":\"chatcmpl-haLWBtmmP6yo7kZFz7sGaEc4GN796icd\",\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"b1-a7b94df\",\"object\":\"chat.completion.chunk\"}\n\ndata: {\"choices\":[{\"finish_reason\":null,\"index\":0,\"delta\":{\"reasoning_content\":\" me\"}}],\"created\":1791150473,\"id\":\"chatcmpl-haLWBtmmP6yo7kZFz7sGaEc4GN796icd\",\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"b1-a7b94df\",\"object\":\"chat.completion.chunk\"}\n\ndata: {\"choices\":[{\"finish_reason\":null,\"index\":0,\"delta\":{\"reasoning_content\":\" to\"}}],\"created\":1791150473,\"id\":\"chatcmpl-haLWBtmmP6yo7kZFz7sGaEc4GN796icd\",\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"b1-a7b94df\",\"object\":\"chat.completion.chunk\"}\n\ndata: {\"choices\":[{\"finish_reason\":null,\"index\":0,\"delta\":{\"reasoning_content\":\" read\"}}],\"created\":1791150473,\"id\":\"chatcmpl-haLWBtmmP6yo7kZFz7sGaEc4GN796icd\",\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"b1-a7b94df\",\"object\":\"chat.completion.chunk\"}\n\ndata: {\"choices\":[{\"finish_reason\":null,\"index\":0,\"delta\":{\"reasoning_content\":\" the\"}}],\"created\":1791150473,\"id\":\"chatcmpl-haLWBtmmP6yo7kZFz7sGaEc4GN796icd\",\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"b1-a7b94df\",\"object\":\"chat.completion.chunk\"}\n\ndata: {\"choices\":[{\"finish_reason\":null,\"index\":0,\"delta\":{\"reasoning_content\":\" README\"}}],\"created\":1791150473,\"id\":\"chatcmpl-haLWBtmmP6yo7kZFz7sGaEc4GN796icd\",\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"b1-a7b94df\",\"object\":\"chat.completion.chunk\"}\n\ndata: {\"choices\":[{\"finish_reason\":null,\"index\":0,\"delta\":{\"reasoning_content\":\".md\"}}],\"created\":1791150474,\"id\":\"chatcmpl-haLWBtmmP6yo7kZFz7sGaEc4GN796icd\",\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"b1-a7b94df\",\"object\":\"chat.completion.chunk\"}\n\ndata: {\"choices\":[{\"finish_reason\":null,\"index\":0,\"delta\":{\"reasoning_content\":\" file\"}}],\"created\":1791150474,\"id\":\"chatcmpl-haLWBtmmP6yo7kZFz7sGaEc4GN796icd\",\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"b1-a7b94df\",\"object\":\"chat.completion.chunk\"}\n\ndata: {\"choices\":[{\"finish_reason\":null,\"index\":0,\"delta\":{\"reasoning_content\":\".\"}}],\"created\":1791150474,\"id\":\"chatcmpl-haLWBtmmP6yo7kZFz7sGaEc4GN796icd\",\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"b1-a7b94df\",\"object\":\"chat.completion.chunk\"}\n\ndata: {\"choices\":[{\"finish_reason\":null,\"index\":0,\"delta\":{\"reasoning_content\":\" I\"}}],\"created\":1791150474,\"id\":\"chatcmpl-haLWBtmmP6yo7kZFz7sGaEc4GN796icd\",\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"b1-a7b94df\",\"object\":\"chat.completion.chunk\"}\n\ndata: {\"choices\":[{\"finish_reason\":null,\"index\":0,\"delta\":{\"reasoning_content\":\" need\"}}],\"created\":1791150474,\"id\":\"chatcmpl-haLWBtmmP6yo7kZFz7sGaEc4GN796icd\",\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"b1-a7b94df\",\"object\":\"chat.completion.chunk\"}\n\ndata: {\"choices\":[{\"finish_reason\":null,\"index\":0,\"delta\":{\"reasoning_content\":\" to\"}}],\"created\":1791150474,\"id\":\"chatcmpl-haLWBtmmP6yo7kZFz7sGaEc4GN796icd\",\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"b1-a7b94df\",\"object\":\"chat.completion.chunk\"}\n\ndata: {\"choices\":[{\"finish_reason\":null,\"index\":0,\"delta\":{\"reasoning_content\":\" use\"}}],\"created\":1791150474,\"id\":\"chatcmpl-haLWBtmmP6yo7kZFz7sGaEc4GN796icd\",\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"b1-a7b94df\",\"object\":\"chat.completion.chunk\"}\n\ndata: {\"choices\":[{\"finish_reason\":null,\"index\":0,\"delta\":{\"reasoning_content\":\" the\"}}],\"created\":1791150474,\"id\":\"chatcmpl-haLWBtmmP6yo7kZFz7sGaEc4GN796icd\",\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"b1-a7b94df\",\"object\":\"chat.completion.chunk\"}\n\ndata: {\"choices\":[{\"finish_reason\":null,\"index\":0,\"delta\":{\"reasoning_content\":\" read\"}}],\"created\":1791150474,\"id\":\"chatcmpl-haLWBtmmP6yo7kZFz7sGaEc4GN796icd\",\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"b1-a7b94df\",\"object\":\"chat.completion.chunk\"}\n\ndata: {\"choices\":[{\"finish_reason\":null,\"index\":0,\"delta\":{\"reasoning_content\":\"_file\"}}],\"created\":1791150474,\"id\":\"chatcmpl-haLWBtmmP6yo7kZFz7sGaEc4GN796icd\",\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"b1-a7b94df\",\"object\":\"chat.completion.chunk\"}\n\ndata: {\"choices\":[{\"finish_reason\":null,\"index\":0,\"delta\":{\"reasoning_content\":\" function\"}}],\"created\":1791150475,\"id\":\"chatcmpl-haLWBtmmP6yo7kZFz7sGaEc4GN796icd\",\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"b1-a7b94df\",\"object\":\"chat.completion.chunk\"}\n\ndata: {\"choices\":[{\"finish_reason\":null,\"index\":0,\"delta\":{\"reasoning_content\":\" with\"}}],\"created\":1791150475,\"id\":\"chatcmpl-haLWBtmmP6yo7kZFz7sGaEc4GN796icd\",\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"b1-a7b94df\",\"object\":\"chat.completion.chunk\"}\n\ndata: {\"choices\":[{\"finish_reason\":null,\"index\":0,\"delta\":{\"reasoning_content\":\" the\"}}],\"created\":1791150475,\"id\":\"chatcmpl-haLWBtmmP6yo7kZFz7sGaEc4GN796icd\",\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"b1-a7b94df\",\"object\":\"chat.completion.chunk\"}\n\ndata: {\"choices\":[{\"finish_reason\":null,\"index\":0,\"delta\":{\"reasoning_content\":\" path\"}}],\"created\":1791150475,\"id\":\"chatcmpl-haLWBtmmP6yo7kZFz7sGaEc4GN796icd\",\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"b1-a7b94df\",\"object\":\"chat.completion.chunk\"}\n\ndata: {\"choices\":[{\"finish_reason\":null,\"index\":0,\"delta\":{\"reasoning_content\":\" \\\"\"}}],\"created\":1791150475,\"id\":\"chatcmpl-haLWBtmmP6yo7kZFz7sGaEc4GN796icd\",\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"b1-a7b94df\",\"object\":\"chat.completion.chunk\"}\n\ndata: {\"choices\":[{\"finish_reason\":null,\"index\":0,\"delta\":{\"reasoning_content\":\"README\"}}],\"created\":1791150475,\"id\":\"chatcmpl-haLWBtmmP6yo7kZFz7sGaEc4GN796icd\",\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"b1-a7b94df\",\"object\":\"chat.completion.chunk\"}\n\ndata: {\"choices\":[{\"finish_reason\":null,\"index\":0,\"delta\":{\"reasoning_content\":\".md\"}}],\"created\":1791150475,\"id\":\"chatcmpl-haLWBtmmP6yo7kZFz7sGaEc4GN796icd\",\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"b1-a7b94df\",\"object\":\"chat.completion.chunk\"}\n\ndata: {\"choices\":[{\"finish_reason\":null,\"index\":0,\"delta\":{\"reasoning_content\":\"\\\".\"}}],\"created\":1791150475,\"id\":\"chatcmpl-haLWBtmmP6yo7kZFz7sGaEc4GN796icd\",\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"b1-a7b94df\",\"object\":\"chat.completion.chunk\"}\n\ndata: {\"choices\":[{\"finish_reason\":null,\"index\":0,\"delta\":{\"reasoning_content\":\"\\n\"}}],\"created\":1791150475,\"id\":\"chatcmpl-haLWBtmmP6yo7kZFz7sGaEc4GN796icd\",\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"b1-a7b94df\",\"object\":\"chat.completion.chunk\"}\n\ndata: {\"choices\":[{\"finish_reason\":null,\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"id\":\"Y9jcnXV85rtOZuU6FbeVhS1p7ZsoJuKQ\",\"type\":\"function\",\"function\":{\"name\":\"read_file\",\"arguments\":\"{\"}}]}}],\"created\":1791150477,\"id\":\"chatcmpl-haLWBtmmP6yo7kZFz7sGaEc4GN796icd\",\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"b1-a7b94df\",\"object\":\"chat.completion.chunk\"}\n\ndata: {\"choices\":[{\"finish_reason\":null,\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"\\\"path\\\":\\\"\"}}]}}],\"created\":1791150477,\"id\":\"chatcmpl-haLWBtmmP6yo7kZFz7sGaEc4GN796icd\",\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"b1-a7b94df\",\"object\":\"chat.completion.chunk\"}\n\ndata: {\"choices\":[{\"finish_reason\":null,\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"README\"}}]}}],\"created\":1791150477,\"id\":\"chatcmpl-haLWBtmmP6yo7kZFz7sGaEc4GN796icd\",\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"b1-a7b94df\",\"object\":\"chat.completion.chunk\"}\n\ndata: {\"choices\":[{\"finish_reason\":null,\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\".md\"}}]}}],\"created\":1791150477,\"id\":\"chatcmpl-haLWBtmmP6yo7kZFz7sGaEc4GN796icd\",\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"b1-a7b94df\",\"object\":\"chat.completion.chunk\"}\n\ndata: {\"choices\":[{\"finish_reason\":null,\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"\\\"\"}}]}}],\"created\":1791150478,\"id\":\"chatcmpl-haLWBtmmP6yo7kZFz7sGaEc4GN796icd\",\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"b1-a7b94df\",\"object\":\"chat.completion.chunk\"}\n\ndata: {\"choices\":[{\"finish_reason\":null,\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"}\"}}]}}],\"created\":1791150478,\"id\":\"chatcmpl-haLWBtmmP6yo7kZFz7sGaEc4GN796icd\",\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"b1-a7b94df\",\"object\":\"chat.completion.chunk\"}\n\ndata: {\"choices\":[{\"finish_reason\":\"tool_calls\",\"index\":0,\"delta\":{}}],\"created\":1791150478,\"id\":\"chatcmpl-haLWBtmmP6yo7kZFz7sGaEc4GN796icd\",\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"b1-a7b94df\",\"object\":\"chat.completion.chunk\",\"timings\":{\"cache_n\":488,\"prompt_n\":4,\"prompt_ms\":207.387,\"prompt_per_token_ms\":51.84675,\"prompt_per_second\":19.287612048971248,\"predicted_n\":55,\"predicted_ms\":5335.215,\"predicted_per_token_ms\":98.80027777777778,\"predicted_per_second\":10.121429033319181}}\n\ndata: [DONE]\n\n", + "model": "mock-model", + "trace_id": "trace-dialect", + "turn": 0, + "parser_version": "1.0.0", + "reasoning_present": true, + "expected": { + "text": "", + "intents": [ + { + "kind": "tool_request", + "request_id": "Y9jcnXV85rtOZuU6FbeVhS1p7ZsoJuKQ", + "tool": { + "name": "read_file", + "namespace": null + }, + "arguments": { + "path": "README.md" + }, + "provenance": { + "dialect": "openai_stream", + "dialect_version": "1", + "model": "mock-model", + "parser_version": "1.0.0", + "source_turn": 0, + "source_call_id": "Y9jcnXV85rtOZuU6FbeVhS1p7ZsoJuKQ", + "source_digest": null, + "raw_name": "read_file", + "coercion": "none" + } + } + ], + "rejected": [], + "partial": false, + "dialect_version": "1" + }, + "expected_digests": { + "reasoning_digest": "sha256:c8f1cfda572878e39a70cfdcbea89897bdeba8d15d1d193bd52370c61556c28c", + "source_digests": [ + "sha256:2cdbad92712513f22e83b1e29ea0643a2b7d946e7fe4d3b8a025079248c10f20" + ] + } +} diff --git a/dialects/fixtures/openai_stream/capture-ollama-no-reasoning.json b/dialects/fixtures/openai_stream/capture-ollama-no-reasoning.json new file mode 100644 index 0000000..a4a69e2 --- /dev/null +++ b/dialects/fixtures/openai_stream/capture-ollama-no-reasoning.json @@ -0,0 +1,51 @@ +{ + "dialect": "openai_stream", + "name": "capture-ollama-no-reasoning", + "description": "Real ollama stream (Qwen3.5-9B), byte-identical to the capture's response body.", + "capture": { + "path": "qualification/captures/qwen35-ollama/h2-streaming-reasoning-effort-none.json", + "sha256_response": "efec12328920dc70371a8995a13fe4f445fcfb665fa7b12ed8cad867ecc7f31a" + }, + "input": "data: {\"id\":\"chatcmpl-406\",\"object\":\"chat.completion.chunk\",\"created\":1791145858,\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"fp_ollama\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"\",\"tool_calls\":[{\"id\":\"call_wjh7d815\",\"index\":0,\"type\":\"function\",\"function\":{\"name\":\"read_file\",\"arguments\":\"{\\\"path\\\":\\\"README.md\\\"}\"}}]},\"finish_reason\":null}]}\n\ndata: {\"id\":\"chatcmpl-406\",\"object\":\"chat.completion.chunk\",\"created\":1791145858,\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"fp_ollama\",\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"tool_calls\"}]}\n\ndata: [DONE]\n\n", + "model": "mock-model", + "trace_id": "trace-dialect", + "turn": 0, + "parser_version": "1.0.0", + "reasoning_present": false, + "expected": { + "text": "", + "intents": [ + { + "kind": "tool_request", + "request_id": "call_wjh7d815", + "tool": { + "name": "read_file", + "namespace": null + }, + "arguments": { + "path": "README.md" + }, + "provenance": { + "dialect": "openai_stream", + "dialect_version": "1", + "model": "mock-model", + "parser_version": "1.0.0", + "source_turn": 0, + "source_call_id": "call_wjh7d815", + "source_digest": null, + "raw_name": "read_file", + "coercion": "none" + } + } + ], + "rejected": [], + "partial": false, + "dialect_version": "1" + }, + "expected_digests": { + "reasoning_digest": null, + "source_digests": [ + "sha256:f07114a61f0ab51bf3d5764c8d7cc2f3d351412cd2b4281f5289e62f2ca0ea90" + ] + } +} diff --git a/dialects/fixtures/openai_stream/capture-ollama-reasoning.json b/dialects/fixtures/openai_stream/capture-ollama-reasoning.json new file mode 100644 index 0000000..97dfd26 --- /dev/null +++ b/dialects/fixtures/openai_stream/capture-ollama-reasoning.json @@ -0,0 +1,51 @@ +{ + "dialect": "openai_stream", + "name": "capture-ollama-reasoning", + "description": "Real ollama stream (Qwen3.5-9B), byte-identical to the capture's response body.", + "capture": { + "path": "qualification/captures/qwen35-ollama/h1-streaming.json", + "sha256_response": "17a307e79a3aa5ad1440f6817f726706b08aac812f128415c4bf6f09ecb41324" + }, + "input": "data: {\"id\":\"chatcmpl-192\",\"object\":\"chat.completion.chunk\",\"created\":1791145856,\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"fp_ollama\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"\",\"reasoning\":\"The\"},\"finish_reason\":null}]}\n\ndata: {\"id\":\"chatcmpl-192\",\"object\":\"chat.completion.chunk\",\"created\":1791145856,\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"fp_ollama\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"\",\"reasoning\":\" user\"},\"finish_reason\":null}]}\n\ndata: {\"id\":\"chatcmpl-192\",\"object\":\"chat.completion.chunk\",\"created\":1791145856,\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"fp_ollama\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"\",\"reasoning\":\" wants\"},\"finish_reason\":null}]}\n\ndata: {\"id\":\"chatcmpl-192\",\"object\":\"chat.completion.chunk\",\"created\":1791145856,\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"fp_ollama\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"\",\"reasoning\":\" me\"},\"finish_reason\":null}]}\n\ndata: {\"id\":\"chatcmpl-192\",\"object\":\"chat.completion.chunk\",\"created\":1791145856,\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"fp_ollama\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"\",\"reasoning\":\" to\"},\"finish_reason\":null}]}\n\ndata: {\"id\":\"chatcmpl-192\",\"object\":\"chat.completion.chunk\",\"created\":1791145856,\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"fp_ollama\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"\",\"reasoning\":\" read\"},\"finish_reason\":null}]}\n\ndata: {\"id\":\"chatcmpl-192\",\"object\":\"chat.completion.chunk\",\"created\":1791145856,\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"fp_ollama\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"\",\"reasoning\":\" a\"},\"finish_reason\":null}]}\n\ndata: {\"id\":\"chatcmpl-192\",\"object\":\"chat.completion.chunk\",\"created\":1791145856,\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"fp_ollama\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"\",\"reasoning\":\" file\"},\"finish_reason\":null}]}\n\ndata: {\"id\":\"chatcmpl-192\",\"object\":\"chat.completion.chunk\",\"created\":1791145856,\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"fp_ollama\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"\",\"reasoning\":\" called\"},\"finish_reason\":null}]}\n\ndata: {\"id\":\"chatcmpl-192\",\"object\":\"chat.completion.chunk\",\"created\":1791145856,\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"fp_ollama\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"\",\"reasoning\":\" README\"},\"finish_reason\":null}]}\n\ndata: {\"id\":\"chatcmpl-192\",\"object\":\"chat.completion.chunk\",\"created\":1791145856,\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"fp_ollama\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"\",\"reasoning\":\".md\"},\"finish_reason\":null}]}\n\ndata: {\"id\":\"chatcmpl-192\",\"object\":\"chat.completion.chunk\",\"created\":1791145856,\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"fp_ollama\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"\",\"reasoning\":\".\"},\"finish_reason\":null}]}\n\ndata: {\"id\":\"chatcmpl-192\",\"object\":\"chat.completion.chunk\",\"created\":1791145856,\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"fp_ollama\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"\",\"reasoning\":\" I\"},\"finish_reason\":null}]}\n\ndata: {\"id\":\"chatcmpl-192\",\"object\":\"chat.completion.chunk\",\"created\":1791145856,\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"fp_ollama\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"\",\"reasoning\":\" need\"},\"finish_reason\":null}]}\n\ndata: {\"id\":\"chatcmpl-192\",\"object\":\"chat.completion.chunk\",\"created\":1791145856,\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"fp_ollama\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"\",\"reasoning\":\" to\"},\"finish_reason\":null}]}\n\ndata: {\"id\":\"chatcmpl-192\",\"object\":\"chat.completion.chunk\",\"created\":1791145856,\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"fp_ollama\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"\",\"reasoning\":\" use\"},\"finish_reason\":null}]}\n\ndata: {\"id\":\"chatcmpl-192\",\"object\":\"chat.completion.chunk\",\"created\":1791145856,\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"fp_ollama\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"\",\"reasoning\":\" the\"},\"finish_reason\":null}]}\n\ndata: {\"id\":\"chatcmpl-192\",\"object\":\"chat.completion.chunk\",\"created\":1791145856,\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"fp_ollama\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"\",\"reasoning\":\" read\"},\"finish_reason\":null}]}\n\ndata: {\"id\":\"chatcmpl-192\",\"object\":\"chat.completion.chunk\",\"created\":1791145856,\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"fp_ollama\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"\",\"reasoning\":\"_file\"},\"finish_reason\":null}]}\n\ndata: {\"id\":\"chatcmpl-192\",\"object\":\"chat.completion.chunk\",\"created\":1791145856,\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"fp_ollama\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"\",\"reasoning\":\" function\"},\"finish_reason\":null}]}\n\ndata: {\"id\":\"chatcmpl-192\",\"object\":\"chat.completion.chunk\",\"created\":1791145856,\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"fp_ollama\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"\",\"reasoning\":\" with\"},\"finish_reason\":null}]}\n\ndata: {\"id\":\"chatcmpl-192\",\"object\":\"chat.completion.chunk\",\"created\":1791145856,\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"fp_ollama\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"\",\"reasoning\":\" the\"},\"finish_reason\":null}]}\n\ndata: {\"id\":\"chatcmpl-192\",\"object\":\"chat.completion.chunk\",\"created\":1791145856,\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"fp_ollama\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"\",\"reasoning\":\" path\"},\"finish_reason\":null}]}\n\ndata: {\"id\":\"chatcmpl-192\",\"object\":\"chat.completion.chunk\",\"created\":1791145856,\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"fp_ollama\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"\",\"reasoning\":\" parameter\"},\"finish_reason\":null}]}\n\ndata: {\"id\":\"chatcmpl-192\",\"object\":\"chat.completion.chunk\",\"created\":1791145856,\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"fp_ollama\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"\",\"reasoning\":\" set\"},\"finish_reason\":null}]}\n\ndata: {\"id\":\"chatcmpl-192\",\"object\":\"chat.completion.chunk\",\"created\":1791145856,\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"fp_ollama\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"\",\"reasoning\":\" to\"},\"finish_reason\":null}]}\n\ndata: {\"id\":\"chatcmpl-192\",\"object\":\"chat.completion.chunk\",\"created\":1791145856,\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"fp_ollama\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"\",\"reasoning\":\" \\\"\"},\"finish_reason\":null}]}\n\ndata: {\"id\":\"chatcmpl-192\",\"object\":\"chat.completion.chunk\",\"created\":1791145856,\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"fp_ollama\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"\",\"reasoning\":\"README\"},\"finish_reason\":null}]}\n\ndata: {\"id\":\"chatcmpl-192\",\"object\":\"chat.completion.chunk\",\"created\":1791145856,\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"fp_ollama\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"\",\"reasoning\":\".md\"},\"finish_reason\":null}]}\n\ndata: {\"id\":\"chatcmpl-192\",\"object\":\"chat.completion.chunk\",\"created\":1791145856,\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"fp_ollama\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"\",\"reasoning\":\"\\\".\"},\"finish_reason\":null}]}\n\ndata: {\"id\":\"chatcmpl-192\",\"object\":\"chat.completion.chunk\",\"created\":1791145856,\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"fp_ollama\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"\",\"tool_calls\":[{\"id\":\"call_croxjxnr\",\"index\":0,\"type\":\"function\",\"function\":{\"name\":\"read_file\",\"arguments\":\"{\\\"path\\\":\\\"README.md\\\"}\"}}]},\"finish_reason\":null}]}\n\ndata: {\"id\":\"chatcmpl-192\",\"object\":\"chat.completion.chunk\",\"created\":1791145856,\"model\":\"qwen3.5:9b\",\"system_fingerprint\":\"fp_ollama\",\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"tool_calls\"}]}\n\ndata: [DONE]\n\n", + "model": "mock-model", + "trace_id": "trace-dialect", + "turn": 0, + "parser_version": "1.0.0", + "reasoning_present": true, + "expected": { + "text": "", + "intents": [ + { + "kind": "tool_request", + "request_id": "call_croxjxnr", + "tool": { + "name": "read_file", + "namespace": null + }, + "arguments": { + "path": "README.md" + }, + "provenance": { + "dialect": "openai_stream", + "dialect_version": "1", + "model": "mock-model", + "parser_version": "1.0.0", + "source_turn": 0, + "source_call_id": "call_croxjxnr", + "source_digest": null, + "raw_name": "read_file", + "coercion": "none" + } + } + ], + "rejected": [], + "partial": false, + "dialect_version": "1" + }, + "expected_digests": { + "reasoning_digest": "sha256:eea0a45bad28a42b247c552c3a3e0d08bc12a408654fe823d545053455b57170", + "source_digests": [ + "sha256:c5b6238987b38fc04a8b48adcb93a3b6daa9681266fa438779e2a5a6b13c027e" + ] + } +} diff --git a/dialects/fixtures/openai_stream/capture-sglang-no-reasoning.json b/dialects/fixtures/openai_stream/capture-sglang-no-reasoning.json new file mode 100644 index 0000000..2041b80 --- /dev/null +++ b/dialects/fixtures/openai_stream/capture-sglang-no-reasoning.json @@ -0,0 +1,51 @@ +{ + "dialect": "openai_stream", + "name": "capture-sglang-no-reasoning", + "description": "Real sglang stream (Qwen3.5-9B), byte-identical to the capture's response body.", + "capture": { + "path": "qualification/captures/qwen35-sglang/h2-streaming-reasoning-effort-none.json", + "sha256_response": "a7cea3420e99e274b755238ec0bd739fdd5edd868d07b50dc0a4d2f1e7eeeced" + }, + "input": "data: {\"id\":\"e3e82c9bee314533b97f4e28fe7708c5\",\"object\":\"chat.completion.chunk\",\"created\":1791149946,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":null,\"role\":\"assistant\",\"content\":\"\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"e3e82c9bee314533b97f4e28fe7708c5\",\"object\":\"chat.completion.chunk\",\"created\":1791149947,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"role\":null,\"content\":null,\"reasoning_content\":null,\"tool_calls\":[{\"id\":\"call_c2a76d88e9a14c4fa132adfe\",\"index\":0,\"type\":\"function\",\"function\":{\"name\":\"read_file\",\"arguments\":\"\"}}]},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}],\"usage\":null}\n\ndata: {\"id\":\"e3e82c9bee314533b97f4e28fe7708c5\",\"object\":\"chat.completion.chunk\",\"created\":1791149949,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"role\":null,\"content\":null,\"reasoning_content\":null,\"tool_calls\":[{\"id\":null,\"index\":0,\"type\":\"function\",\"function\":{\"name\":null,\"arguments\":\"{\"}}]},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}],\"usage\":null}\n\ndata: {\"id\":\"e3e82c9bee314533b97f4e28fe7708c5\",\"object\":\"chat.completion.chunk\",\"created\":1791149949,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"role\":null,\"content\":null,\"reasoning_content\":null,\"tool_calls\":[{\"id\":null,\"index\":0,\"type\":\"function\",\"function\":{\"name\":null,\"arguments\":\"\\\"path\\\": \\\"README.md\\\"\"}}]},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}],\"usage\":null}\n\ndata: {\"id\":\"e3e82c9bee314533b97f4e28fe7708c5\",\"object\":\"chat.completion.chunk\",\"created\":1791149949,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"role\":null,\"content\":null,\"reasoning_content\":null,\"tool_calls\":[{\"id\":null,\"index\":0,\"type\":\"function\",\"function\":{\"name\":null,\"arguments\":\"}\"}}]},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}],\"usage\":null}\n\ndata: {\"id\":\"e3e82c9bee314533b97f4e28fe7708c5\",\"object\":\"chat.completion.chunk\",\"created\":1791149950,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":null},\"logprobs\":null,\"finish_reason\":\"tool_calls\",\"matched_stop\":248046}]}\n\ndata: [DONE]\n\n", + "model": "mock-model", + "trace_id": "trace-dialect", + "turn": 0, + "parser_version": "1.0.0", + "reasoning_present": false, + "expected": { + "text": "", + "intents": [ + { + "kind": "tool_request", + "request_id": "call_c2a76d88e9a14c4fa132adfe", + "tool": { + "name": "read_file", + "namespace": null + }, + "arguments": { + "path": "README.md" + }, + "provenance": { + "dialect": "openai_stream", + "dialect_version": "1", + "model": "mock-model", + "parser_version": "1.0.0", + "source_turn": 0, + "source_call_id": "call_c2a76d88e9a14c4fa132adfe", + "source_digest": null, + "raw_name": "read_file", + "coercion": "none" + } + } + ], + "rejected": [], + "partial": false, + "dialect_version": "1" + }, + "expected_digests": { + "reasoning_digest": null, + "source_digests": [ + "sha256:8dd5635dc9ef6551fcd624b0d4ab65ab7138934432d9bdb253274fc938349072" + ] + } +} diff --git a/dialects/fixtures/openai_stream/capture-sglang-reasoning.json b/dialects/fixtures/openai_stream/capture-sglang-reasoning.json new file mode 100644 index 0000000..2b12c43 --- /dev/null +++ b/dialects/fixtures/openai_stream/capture-sglang-reasoning.json @@ -0,0 +1,51 @@ +{ + "dialect": "openai_stream", + "name": "capture-sglang-reasoning", + "description": "Real sglang stream (Qwen3.5-9B), byte-identical to the capture's response body.", + "capture": { + "path": "qualification/captures/qwen35-sglang/h1-streaming.json", + "sha256_response": "51278649525526a86c8c999054aed9e5446cbfe52dc046199804008029311663" + }, + "input": "data: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149936,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":null,\"role\":\"assistant\",\"content\":\"\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149936,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\"The\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149936,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\" user\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149936,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\" wants\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149936,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\" me\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149936,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\" to\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149936,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\" read\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149936,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\" a\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149936,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\" file\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149937,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\" called\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149937,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\" README\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149937,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\".md\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149937,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\" and\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149937,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\" tell\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149937,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\" them\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149937,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\" what\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149938,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\" it\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149938,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\" says\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149938,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\".\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149938,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\" I\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149938,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\" need\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149938,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\" to\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149938,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\" use\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149939,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\" the\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149939,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\" read\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149939,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\"_file\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149939,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\" function\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149939,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\" to\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149939,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\" read\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149939,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\" this\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149940,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\" file\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149940,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\".\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149940,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\" The\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149940,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\" path\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149940,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\" should\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149940,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\" be\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149940,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\" \\\"\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149941,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\"README\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149941,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\".md\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149941,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\"\\\"\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149941,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\" since\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149941,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\" that\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149941,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\"'s\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149941,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\" the\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149941,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\" filename\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149942,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\" they\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149942,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\" specified\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149942,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\".\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149942,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\"\\n\"},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}]}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149942,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"role\":null,\"content\":\"\\n\\n\",\"reasoning_content\":null,\"tool_calls\":null},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}],\"usage\":null}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149943,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"role\":null,\"content\":null,\"reasoning_content\":null,\"tool_calls\":[{\"id\":\"call_5bd6383799ff46958c4f599c\",\"index\":0,\"type\":\"function\",\"function\":{\"name\":\"read_file\",\"arguments\":\"\"}}]},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}],\"usage\":null}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149945,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"role\":null,\"content\":null,\"reasoning_content\":null,\"tool_calls\":[{\"id\":null,\"index\":0,\"type\":\"function\",\"function\":{\"name\":null,\"arguments\":\"{\"}}]},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}],\"usage\":null}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149945,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"role\":null,\"content\":null,\"reasoning_content\":null,\"tool_calls\":[{\"id\":null,\"index\":0,\"type\":\"function\",\"function\":{\"name\":null,\"arguments\":\"\\\"path\\\": \\\"README.md\\\"\"}}]},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}],\"usage\":null}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149946,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"role\":null,\"content\":null,\"reasoning_content\":null,\"tool_calls\":[{\"id\":null,\"index\":0,\"type\":\"function\",\"function\":{\"name\":null,\"arguments\":\"}\"}}]},\"logprobs\":null,\"finish_reason\":null,\"matched_stop\":null}],\"usage\":null}\n\ndata: {\"id\":\"735162cd70064794a79fc631d9db5927\",\"object\":\"chat.completion.chunk\",\"created\":1791149946,\"model\":\"Qwen/Qwen3.5-9B\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":null},\"logprobs\":null,\"finish_reason\":\"tool_calls\",\"matched_stop\":248046}]}\n\ndata: [DONE]\n\n", + "model": "mock-model", + "trace_id": "trace-dialect", + "turn": 0, + "parser_version": "1.0.0", + "reasoning_present": true, + "expected": { + "text": "\n\n", + "intents": [ + { + "kind": "tool_request", + "request_id": "call_5bd6383799ff46958c4f599c", + "tool": { + "name": "read_file", + "namespace": null + }, + "arguments": { + "path": "README.md" + }, + "provenance": { + "dialect": "openai_stream", + "dialect_version": "1", + "model": "mock-model", + "parser_version": "1.0.0", + "source_turn": 0, + "source_call_id": "call_5bd6383799ff46958c4f599c", + "source_digest": null, + "raw_name": "read_file", + "coercion": "none" + } + } + ], + "rejected": [], + "partial": false, + "dialect_version": "1" + }, + "expected_digests": { + "reasoning_digest": "sha256:19fa8d808b18b3fb848df4a31801c7fc0ec45e92d381efdcff7fb65810c6dbdd", + "source_digests": [ + "sha256:f185291530944f599c677a9e9cdfc2fb5f10c4c1267af84577e902e3052d0e8d" + ] + } +} diff --git a/dialects/fixtures/openai_stream/crlf-comments-no-space.json b/dialects/fixtures/openai_stream/crlf-comments-no-space.json new file mode 100644 index 0000000..088a93c --- /dev/null +++ b/dialects/fixtures/openai_stream/crlf-comments-no-space.json @@ -0,0 +1,47 @@ +{ + "dialect": "openai_stream", + "name": "crlf-comments-no-space", + "description": "CRLF line ends, comment lines, an event field and 'data:' without a space assemble normally.", + "input": ": keep-alive\r\n\r\nevent: message\r\ndata:{\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":null},\"finish_reason\":null}]}\r\n\r\ndata:{\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"name\":\"read_file\",\"arguments\":\"{\"},\"id\":\"call_s\",\"type\":\"function\"}]},\"finish_reason\":null}]}\r\n\r\ndata:{\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"\\\"path\\\":\\\"\"}}]},\"finish_reason\":null}]}\r\n\r\ndata:{\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"README\"}}]},\"finish_reason\":null}]}\r\n\r\ndata:{\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\".md\"}}]},\"finish_reason\":null}]}\r\n\r\ndata:{\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"\\\"\"}}]},\"finish_reason\":null}]}\r\n\r\ndata:{\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"}\"}}]},\"finish_reason\":null}]}\r\n\r\ndata:{\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"tool_calls\"}]}\r\n\r\ndata:[DONE]\r\n\r\n", + "model": "mock-model", + "trace_id": "trace-dialect", + "turn": 0, + "parser_version": "1.0.0", + "reasoning_present": false, + "expected": { + "text": "", + "intents": [ + { + "kind": "tool_request", + "request_id": "call_s", + "tool": { + "name": "read_file", + "namespace": null + }, + "arguments": { + "path": "README.md" + }, + "provenance": { + "dialect": "openai_stream", + "dialect_version": "1", + "model": "mock-model", + "parser_version": "1.0.0", + "source_turn": 0, + "source_call_id": "call_s", + "source_digest": null, + "raw_name": "read_file", + "coercion": "none" + } + } + ], + "rejected": [], + "partial": false, + "dialect_version": "1" + }, + "expected_digests": { + "reasoning_digest": null, + "source_digests": [ + "sha256:32b1d04f496503314ad28520d5bc1d8b591575b4de12c9f7480fd2ab92eeea2a" + ] + } +} diff --git a/dialects/fixtures/openai_stream/cut-after-complete-arguments.json b/dialects/fixtures/openai_stream/cut-after-complete-arguments.json new file mode 100644 index 0000000..6bed0bd --- /dev/null +++ b/dialects/fixtures/openai_stream/cut-after-complete-arguments.json @@ -0,0 +1,28 @@ +{ + "dialect": "openai_stream", + "name": "cut-after-complete-arguments", + "description": "Arguments already parse but no finish_reason arrived: still rejected.", + "input": "data: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":null},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"name\":\"read_file\",\"arguments\":\"{\"},\"id\":\"call_s\",\"type\":\"function\"}]},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"\\\"path\\\":\\\"\"}}]},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"README\"}}]},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\".md\"}}]},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"\\\"\"}}]},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"}\"}}]},\"finish_reason\":null}]}\n\n", + "model": "mock-model", + "trace_id": "trace-dialect", + "turn": 0, + "parser_version": "1.0.0", + "reasoning_present": false, + "expected": { + "text": "", + "intents": [], + "rejected": [ + { + "index": 0, + "code": "malformed_tool_call", + "message": "truncated tool call" + } + ], + "partial": true, + "dialect_version": "1" + }, + "expected_digests": { + "reasoning_digest": null, + "source_digests": [] + } +} diff --git a/dialects/fixtures/openai_stream/cut-mid-arguments.json b/dialects/fixtures/openai_stream/cut-mid-arguments.json new file mode 100644 index 0000000..e15fa74 --- /dev/null +++ b/dialects/fixtures/openai_stream/cut-mid-arguments.json @@ -0,0 +1,28 @@ +{ + "dialect": "openai_stream", + "name": "cut-mid-arguments", + "description": "Stream ends mid-arguments with no finish_reason: the call is rejected, never guessed.", + "input": "data: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":null},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"name\":\"read_file\",\"arguments\":\"{\"},\"id\":\"call_s\",\"type\":\"function\"}]},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"\\\"path\\\":\\\"\"}}]},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"README\"}}]},\"finish_reason\":null}]}\n\n", + "model": "mock-model", + "trace_id": "trace-dialect", + "turn": 0, + "parser_version": "1.0.0", + "reasoning_present": false, + "expected": { + "text": "", + "intents": [], + "rejected": [ + { + "index": 0, + "code": "malformed_tool_call", + "message": "truncated tool call" + } + ], + "partial": true, + "dialect_version": "1" + }, + "expected_digests": { + "reasoning_digest": null, + "source_digests": [] + } +} diff --git a/dialects/fixtures/openai_stream/error-chunk-not-json.json b/dialects/fixtures/openai_stream/error-chunk-not-json.json new file mode 100644 index 0000000..f62ed2e --- /dev/null +++ b/dialects/fixtures/openai_stream/error-chunk-not-json.json @@ -0,0 +1,28 @@ +{ + "dialect": "openai_stream", + "name": "error-chunk-not-json", + "description": "A chunk that is not JSON: stream error.", + "input": "data: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":null},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"name\":\"read_file\",\"arguments\":\"{\"},\"id\":\"call_s\",\"type\":\"function\"}]},\"finish_reason\":null}]}\n\ndata: {not json\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"tool_calls\"}]}\n\ndata: [DONE]\n\n", + "model": "mock-model", + "trace_id": "trace-dialect", + "turn": 0, + "parser_version": "1.0.0", + "reasoning_present": false, + "expected": { + "text": "", + "intents": [], + "rejected": [ + { + "index": 0, + "code": "malformed_tool_call", + "message": "stream chunk is not a JSON object" + } + ], + "partial": false, + "dialect_version": "1" + }, + "expected_digests": { + "reasoning_digest": null, + "source_digests": [] + } +} diff --git a/dialects/fixtures/openai_stream/error-conflicting-name.json b/dialects/fixtures/openai_stream/error-conflicting-name.json new file mode 100644 index 0000000..47a1407 --- /dev/null +++ b/dialects/fixtures/openai_stream/error-conflicting-name.json @@ -0,0 +1,28 @@ +{ + "dialect": "openai_stream", + "name": "error-conflicting-name", + "description": "A later fragment names a different tool for the same index: stream error.", + "input": "data: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":null},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"name\":\"read_file\",\"arguments\":\"{\"},\"id\":\"call_s\",\"type\":\"function\"}]},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"\\\"path\\\":\\\"\"}}]},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"name\":\"delete_file\"}}]},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"tool_calls\"}]}\n\ndata: [DONE]\n\n", + "model": "mock-model", + "trace_id": "trace-dialect", + "turn": 0, + "parser_version": "1.0.0", + "reasoning_present": false, + "expected": { + "text": "", + "intents": [], + "rejected": [ + { + "index": 0, + "code": "malformed_tool_call", + "message": "conflicting tool call name" + } + ], + "partial": false, + "dialect_version": "1" + }, + "expected_digests": { + "reasoning_digest": null, + "source_digests": [] + } +} diff --git a/dialects/fixtures/openai_stream/error-event-after-done.json b/dialects/fixtures/openai_stream/error-event-after-done.json new file mode 100644 index 0000000..e538651 --- /dev/null +++ b/dialects/fixtures/openai_stream/error-event-after-done.json @@ -0,0 +1,28 @@ +{ + "dialect": "openai_stream", + "name": "error-event-after-done", + "description": "Data after [DONE]: stream error.", + "input": "data: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":null},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"name\":\"read_file\",\"arguments\":\"{\"},\"id\":\"call_s\",\"type\":\"function\"}]},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"\\\"path\\\":\\\"\"}}]},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"README\"}}]},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\".md\"}}]},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"\\\"\"}}]},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"}\"}}]},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"tool_calls\"}]}\n\ndata: [DONE]\n\ndata: {}\n\n", + "model": "mock-model", + "trace_id": "trace-dialect", + "turn": 0, + "parser_version": "1.0.0", + "reasoning_present": false, + "expected": { + "text": "", + "intents": [], + "rejected": [ + { + "index": 0, + "code": "malformed_tool_call", + "message": "event after [DONE]" + } + ], + "partial": false, + "dialect_version": "1" + }, + "expected_digests": { + "reasoning_digest": null, + "source_digests": [] + } +} diff --git a/dialects/fixtures/openai_stream/error-fragment-without-index.json b/dialects/fixtures/openai_stream/error-fragment-without-index.json new file mode 100644 index 0000000..2405f65 --- /dev/null +++ b/dialects/fixtures/openai_stream/error-fragment-without-index.json @@ -0,0 +1,28 @@ +{ + "dialect": "openai_stream", + "name": "error-fragment-without-index", + "description": "A tool call fragment without index: stream error.", + "input": "data: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"id\":\"call_n\",\"function\":{\"name\":\"read_file\",\"arguments\":\"{}\"}}]},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"tool_calls\"}]}\n\ndata: [DONE]\n\n", + "model": "mock-model", + "trace_id": "trace-dialect", + "turn": 0, + "parser_version": "1.0.0", + "reasoning_present": false, + "expected": { + "text": "", + "intents": [], + "rejected": [ + { + "index": 0, + "code": "malformed_tool_call", + "message": "tool call fragment has no index" + } + ], + "partial": false, + "dialect_version": "1" + }, + "expected_digests": { + "reasoning_digest": null, + "source_digests": [] + } +} diff --git a/dialects/fixtures/openai_stream/error-input-not-text.json b/dialects/fixtures/openai_stream/error-input-not-text.json new file mode 100644 index 0000000..041219c --- /dev/null +++ b/dialects/fixtures/openai_stream/error-input-not-text.json @@ -0,0 +1,41 @@ +{ + "dialect": "openai_stream", + "name": "error-input-not-text", + "description": "Input that is not a string (an already-assembled message): rejected.", + "input": { + "role": "assistant", + "content": null, + "tool_calls": [ + { + "id": "call_t", + "type": "function", + "function": { + "name": "read_file", + "arguments": "{}" + } + } + ] + }, + "model": "mock-model", + "trace_id": "trace-dialect", + "turn": 0, + "parser_version": "1.0.0", + "reasoning_present": false, + "expected": { + "text": "", + "intents": [], + "rejected": [ + { + "index": 0, + "code": "malformed_tool_call", + "message": "stream is not text" + } + ], + "partial": false, + "dialect_version": "1" + }, + "expected_digests": { + "reasoning_digest": null, + "source_digests": [] + } +} diff --git a/dialects/fixtures/openai_stream/error-second-choice.json b/dialects/fixtures/openai_stream/error-second-choice.json new file mode 100644 index 0000000..b9b3e01 --- /dev/null +++ b/dialects/fixtures/openai_stream/error-second-choice.json @@ -0,0 +1,28 @@ +{ + "dialect": "openai_stream", + "name": "error-second-choice", + "description": "A chunk for choice index 1: stream error.", + "input": "data: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":null},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"name\":\"read_file\",\"arguments\":\"{\"},\"id\":\"call_s\",\"type\":\"function\"}]},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":1,\"delta\":{\"content\":\"x\"},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"tool_calls\"}]}\n\ndata: [DONE]\n\n", + "model": "mock-model", + "trace_id": "trace-dialect", + "turn": 0, + "parser_version": "1.0.0", + "reasoning_present": false, + "expected": { + "text": "", + "intents": [], + "rejected": [ + { + "index": 0, + "code": "malformed_tool_call", + "message": "multiple choices are not supported" + } + ], + "partial": false, + "dialect_version": "1" + }, + "expected_digests": { + "reasoning_digest": null, + "source_digests": [] + } +} diff --git a/dialects/fixtures/openai_stream/finish-length.json b/dialects/fixtures/openai_stream/finish-length.json new file mode 100644 index 0000000..e686973 --- /dev/null +++ b/dialects/fixtures/openai_stream/finish-length.json @@ -0,0 +1,28 @@ +{ + "dialect": "openai_stream", + "name": "finish-length", + "description": "finish_reason length: the call is rejected as truncated.", + "input": "data: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":null},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"name\":\"read_file\",\"arguments\":\"{\"},\"id\":\"call_s\",\"type\":\"function\"}]},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"\\\"path\\\":\\\"\"}}]},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"README\"}}]},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\".md\"}}]},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"\\\"\"}}]},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"}\"}}]},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"length\"}]}\n\ndata: [DONE]\n\n", + "model": "mock-model", + "trace_id": "trace-dialect", + "turn": 0, + "parser_version": "1.0.0", + "reasoning_present": false, + "expected": { + "text": "", + "intents": [], + "rejected": [ + { + "index": 0, + "code": "malformed_tool_call", + "message": "truncated tool call" + } + ], + "partial": true, + "dialect_version": "1" + }, + "expected_digests": { + "reasoning_digest": null, + "source_digests": [] + } +} diff --git a/dialects/fixtures/openai_stream/fragmented-call.json b/dialects/fixtures/openai_stream/fragmented-call.json new file mode 100644 index 0000000..4feab2b --- /dev/null +++ b/dialects/fixtures/openai_stream/fragmented-call.json @@ -0,0 +1,47 @@ +{ + "dialect": "openai_stream", + "name": "fragmented-call", + "description": "llama.cpp-style fragments: id, name and '{' first, arguments in later pieces.", + "input": "data: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":null},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"name\":\"read_file\",\"arguments\":\"{\"},\"id\":\"call_s\",\"type\":\"function\"}]},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"\\\"path\\\":\\\"\"}}]},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"README\"}}]},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\".md\"}}]},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"\\\"\"}}]},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"}\"}}]},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"tool_calls\"}]}\n\ndata: [DONE]\n\n", + "model": "mock-model", + "trace_id": "trace-dialect", + "turn": 0, + "parser_version": "1.0.0", + "reasoning_present": false, + "expected": { + "text": "", + "intents": [ + { + "kind": "tool_request", + "request_id": "call_s", + "tool": { + "name": "read_file", + "namespace": null + }, + "arguments": { + "path": "README.md" + }, + "provenance": { + "dialect": "openai_stream", + "dialect_version": "1", + "model": "mock-model", + "parser_version": "1.0.0", + "source_turn": 0, + "source_call_id": "call_s", + "source_digest": null, + "raw_name": "read_file", + "coercion": "none" + } + } + ], + "rejected": [], + "partial": false, + "dialect_version": "1" + }, + "expected_digests": { + "reasoning_digest": null, + "source_digests": [ + "sha256:32b1d04f496503314ad28520d5bc1d8b591575b4de12c9f7480fd2ab92eeea2a" + ] + } +} diff --git a/dialects/fixtures/openai_stream/plain-text-cut.json b/dialects/fixtures/openai_stream/plain-text-cut.json new file mode 100644 index 0000000..28c5414 --- /dev/null +++ b/dialects/fixtures/openai_stream/plain-text-cut.json @@ -0,0 +1,22 @@ +{ + "dialect": "openai_stream", + "name": "plain-text-cut", + "description": "A plain answer cut off without finish_reason: text kept, no call, partial false.", + "input": "data: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"Hel\"},\"finish_reason\":null}]}\n\n", + "model": "mock-model", + "trace_id": "trace-dialect", + "turn": 0, + "parser_version": "1.0.0", + "reasoning_present": false, + "expected": { + "text": "Hel", + "intents": [], + "rejected": [], + "partial": false, + "dialect_version": "1" + }, + "expected_digests": { + "reasoning_digest": null, + "source_digests": [] + } +} diff --git a/dialects/fixtures/openai_stream/plain-text-stop.json b/dialects/fixtures/openai_stream/plain-text-stop.json new file mode 100644 index 0000000..81e70b8 --- /dev/null +++ b/dialects/fixtures/openai_stream/plain-text-stop.json @@ -0,0 +1,22 @@ +{ + "dialect": "openai_stream", + "name": "plain-text-stop", + "description": "A streamed plain answer with no tool call.", + "input": "data: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"Hel\"},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"lo.\"},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"stop\"}]}\n\ndata: [DONE]\n\n", + "model": "mock-model", + "trace_id": "trace-dialect", + "turn": 0, + "parser_version": "1.0.0", + "reasoning_present": false, + "expected": { + "text": "Hello.", + "intents": [], + "rejected": [], + "partial": false, + "dialect_version": "1" + }, + "expected_digests": { + "reasoning_digest": null, + "source_digests": [] + } +} diff --git a/dialects/fixtures/openai_stream/sglang-null-repeats.json b/dialects/fixtures/openai_stream/sglang-null-repeats.json new file mode 100644 index 0000000..f1bc406 --- /dev/null +++ b/dialects/fixtures/openai_stream/sglang-null-repeats.json @@ -0,0 +1,47 @@ +{ + "dialect": "openai_stream", + "name": "sglang-null-repeats", + "description": "SGLang-style later fragments repeat id null and name null; text before the call is kept.", + "input": "data: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"\\n\\n\"},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"id\":\"call_g\",\"type\":\"function\",\"function\":{\"name\":\"read_file\",\"arguments\":\"\"}}]},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"id\":null,\"type\":\"function\",\"function\":{\"name\":null,\"arguments\":\"{\\\"path\\\": \\\"/a\\\"}\"}}]},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"tool_calls\"}]}\n\ndata: [DONE]\n\n", + "model": "mock-model", + "trace_id": "trace-dialect", + "turn": 0, + "parser_version": "1.0.0", + "reasoning_present": false, + "expected": { + "text": "\n\n", + "intents": [ + { + "kind": "tool_request", + "request_id": "call_g", + "tool": { + "name": "read_file", + "namespace": null + }, + "arguments": { + "path": "/a" + }, + "provenance": { + "dialect": "openai_stream", + "dialect_version": "1", + "model": "mock-model", + "parser_version": "1.0.0", + "source_turn": 0, + "source_call_id": "call_g", + "source_digest": null, + "raw_name": "read_file", + "coercion": "none" + } + } + ], + "rejected": [], + "partial": false, + "dialect_version": "1" + }, + "expected_digests": { + "reasoning_digest": null, + "source_digests": [ + "sha256:f039fe8a1314c12ca5e6ea704e84ae4937e4f7f20bf14b96d897cf7b25b6d841" + ] + } +} diff --git a/dialects/fixtures/openai_stream/two-calls-interleaved.json b/dialects/fixtures/openai_stream/two-calls-interleaved.json new file mode 100644 index 0000000..ef869a7 --- /dev/null +++ b/dialects/fixtures/openai_stream/two-calls-interleaved.json @@ -0,0 +1,70 @@ +{ + "dialect": "openai_stream", + "name": "two-calls-interleaved", + "description": "Fragments of two calls interleave; calls come out in index order.", + "input": "data: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":1,\"function\":{\"name\":\"list_dir\",\"arguments\":\"{\\\"path\\\":\"},\"id\":\"call_b\",\"type\":\"function\"}]},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"name\":\"read_file\",\"arguments\":\"{\\\"path\\\":\"},\"id\":\"call_a\",\"type\":\"function\"}]},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":1,\"function\":{\"arguments\":\"\\\"/d\\\"}\"}}]},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"\\\"/a\\\"}\"}}]},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"tool_calls\"}]}\n\ndata: [DONE]\n\n", + "model": "mock-model", + "trace_id": "trace-dialect", + "turn": 0, + "parser_version": "1.0.0", + "reasoning_present": false, + "expected": { + "text": "", + "intents": [ + { + "kind": "tool_request", + "request_id": "call_a", + "tool": { + "name": "read_file", + "namespace": null + }, + "arguments": { + "path": "/a" + }, + "provenance": { + "dialect": "openai_stream", + "dialect_version": "1", + "model": "mock-model", + "parser_version": "1.0.0", + "source_turn": 0, + "source_call_id": "call_a", + "source_digest": null, + "raw_name": "read_file", + "coercion": "none" + } + }, + { + "kind": "tool_request", + "request_id": "call_b", + "tool": { + "name": "list_dir", + "namespace": null + }, + "arguments": { + "path": "/d" + }, + "provenance": { + "dialect": "openai_stream", + "dialect_version": "1", + "model": "mock-model", + "parser_version": "1.0.0", + "source_turn": 0, + "source_call_id": "call_b", + "source_digest": null, + "raw_name": "list_dir", + "coercion": "none" + } + } + ], + "rejected": [], + "partial": false, + "dialect_version": "1" + }, + "expected_digests": { + "reasoning_digest": null, + "source_digests": [ + "sha256:d7a9dbd01921c66fb13d02b1b91208e2247a61c6784f59bb5086f8304dbf3675", + "sha256:ac62fffc0ee784cc3b787d643bdf1aacafc3c3b73ba1ab577bc9f31eef1cf31e" + ] + } +} diff --git a/dialects/fixtures/openai_stream/unterminated-last-event.json b/dialects/fixtures/openai_stream/unterminated-last-event.json new file mode 100644 index 0000000..b5bf70c --- /dev/null +++ b/dialects/fixtures/openai_stream/unterminated-last-event.json @@ -0,0 +1,28 @@ +{ + "dialect": "openai_stream", + "name": "unterminated-last-event", + "description": "The finish chunk is not ended by a blank line, so it is discarded and the call is truncated.", + "input": "data: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":null},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"name\":\"read_file\",\"arguments\":\"{\"},\"id\":\"call_s\",\"type\":\"function\"}]},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"\\\"path\\\":\\\"\"}}]},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"README\"}}]},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\".md\"}}]},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"\\\"\"}}]},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"}\"}}]},\"finish_reason\":null}]}\n\ndata: {\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"tool_calls\"}]}", + "model": "mock-model", + "trace_id": "trace-dialect", + "turn": 0, + "parser_version": "1.0.0", + "reasoning_present": false, + "expected": { + "text": "", + "intents": [], + "rejected": [ + { + "index": 0, + "code": "malformed_tool_call", + "message": "truncated tool call" + } + ], + "partial": true, + "dialect_version": "1" + }, + "expected_digests": { + "reasoning_digest": null, + "source_digests": [] + } +} diff --git a/python/interplane/lenshift/__init__.py b/python/interplane/lenshift/__init__.py index 978b1ad..e755c9d 100644 --- a/python/interplane/lenshift/__init__.py +++ b/python/interplane/lenshift/__init__.py @@ -43,8 +43,9 @@ register = default_registry.register get = default_registry.get names = default_registry.names -from . import aien_legacy, openai, qwen35 # noqa: E402 (registered below; ajax is reserved, not registered) +from . import aien_legacy, openai, openai_stream, qwen35 # noqa: E402 (registered below; ajax is reserved, not registered) register("aien_legacy", aien_legacy) register("openai", openai) +register("openai_stream", openai_stream) register("qwen35", qwen35) diff --git a/python/interplane/lenshift/openai_stream.py b/python/interplane/lenshift/openai_stream.py new file mode 100644 index 0000000..d3f8c44 --- /dev/null +++ b/python/interplane/lenshift/openai_stream.py @@ -0,0 +1,153 @@ +"""Lenshift dialect `openai_stream` (version 1): a streamed OpenAI-compatible response body (SSE text). + +The fragments are assembled into one assistant message, which is parsed with the ``openai`` rules. +A stream that did not finish cleanly is rejected call by call ("truncated tool call"), never guessed. +""" + +from typing import Any, Optional + +from ..core import jcs, text_digest +from . import openai +from ._common import loads_strict, new_turn, reject + +NAME = "openai_stream" +COMPLETE = ("stop", "tool_calls", "function_call") + + +class StreamError(ValueError): + pass + + +def events(body: str) -> list: + """The data of each complete SSE event, in order.""" + out, data = [], [] + for line in body.split("\n"): + if line.endswith("\r"): + line = line[:-1] + if line == "": + if data: + out.append("\n".join(data)) + data = [] + elif line == "data": + data.append("") + elif line.startswith("data:"): + value = line[5:] + data.append(value[1:] if value.startswith(" ") else value) + return out + + +def _u64(value: Any) -> Optional[int]: + """``value`` when it is a JSON integer in 0..2^64-1 (what a strict reader holds as u64), else None.""" + if type(value) is int and 0 <= value <= 0xFFFFFFFFFFFFFFFF: + return value + return None + + +def _keep_first(slot: dict, key: str, value: Any, what: str) -> None: + if not isinstance(value, str) or not value: + return + if slot.get(key) is None: + slot[key] = value + elif slot[key] != value: + raise StreamError(f"conflicting tool call {what}") + + +def assemble(body: str): + """``(message, finish_reason)`` for a stream body; raises ``StreamError``.""" + text, reasoning, calls = [], [], {} + finish: Optional[str] = None + done = False + for data in events(body): + if done: + raise StreamError("event after [DONE]") + if data == "[DONE]": + done = True + continue + try: + chunk = loads_strict(data) + except ValueError: + chunk = None + if not isinstance(chunk, dict): + raise StreamError("stream chunk is not a JSON object") + choices = chunk.get("choices") + if not isinstance(choices, list): + continue + for choice in choices: + if not isinstance(choice, dict): + raise StreamError("stream choice is not an object") + if "index" in choice and _u64(choice["index"]) != 0: + raise StreamError("multiple choices are not supported") + delta = choice.get("delta") + if isinstance(delta, dict): + if isinstance(delta.get("content"), str): + text.append(delta["content"]) + for key in ("reasoning_content", "reasoning"): + if isinstance(delta.get(key), str): + reasoning.append(delta[key]) + fragments = delta.get("tool_calls") + for frag in fragments if isinstance(fragments, list) else []: + index = _u64(frag.get("index")) if isinstance(frag, dict) else None + if index is None: + raise StreamError("tool call fragment has no index") + slot = calls.setdefault(index, {}) + _keep_first(slot, "id", frag.get("id"), "id") + function = frag.get("function") + if isinstance(function, dict): + _keep_first(slot, "name", function.get("name"), "name") + if isinstance(function.get("arguments"), str): + slot["arguments"] = slot.get("arguments", "") + function["arguments"] + reason = choice.get("finish_reason") + if reason is not None and not isinstance(reason, str): + raise StreamError("finish_reason is not a string") + if reason is not None: + if finish is None: + finish = reason + elif finish != reason: + raise StreamError("conflicting finish_reason") + message = {"role": "assistant", "content": "".join(text)} + if reasoning: + message["reasoning_content"] = "".join(reasoning) + if calls: + out = [] + for index in sorted(calls): + slot = calls[index] + function = {k: slot[k] for k in ("name", "arguments") if slot.get(k) is not None} + call = {"type": "function", "function": function} + if slot.get("id") is not None: + call["id"] = slot["id"] + out.append(call) + message["tool_calls"] = out + return message, finish + + +def _retag(turn) -> None: + turn.dialect = NAME + for intent in turn.intents: + intent.provenance["dialect"] = NAME + + +def parse(input: Any, model: str, trace_id: str, turn: int): + try: + if not isinstance(input, str): + raise StreamError("stream is not text") + message, finish = assemble(input) + except StreamError as e: + out = new_turn(NAME, model) + body = input if isinstance(input, str) else jcs(input) + out.rejected.append(reject(0, str(e), text_digest(body))) + return out + out = openai.parse(message, model, trace_id, turn) + _retag(out) + if finish in COMPLETE: + return out + cut = [ + (index, intent.provenance["source_digest"], intent.provenance.get("source_call_id")) + for index, intent in zip(out.call_indexes, out.intents) + ] + [(r["index"], r["source_digest"], r.call_id) for r in out.rejected] + out.intents, out.call_indexes = [], [] + out.rejected = [reject(i, "truncated tool call", d, c) for i, d, c in sorted(cut, key=lambda x: x[0])] + out.partial = bool(cut) + return out + + +render_result = openai.render_result diff --git a/python/tests/test_lenshift.py b/python/tests/test_lenshift.py index 340b5bc..3359ad5 100644 --- a/python/tests/test_lenshift.py +++ b/python/tests/test_lenshift.py @@ -34,10 +34,16 @@ def test_dialect_fixture(path): turn.intents ) assert all(r["source_digest"] for r in turn.rejected) + digests = fx.get("expected_digests", {}) + if digests.get("reasoning_digest") is not None: + assert turn.reasoning_digest == digests["reasoning_digest"] + for k, want in enumerate(digests.get("source_digests", [])): + if want is not None: + assert turn.intents[k].provenance["source_digest"] == want def test_registry(): - assert lenshift.names() == ["aien_legacy", "openai", "qwen35"] + assert lenshift.names() == ["aien_legacy", "openai", "openai_stream", "qwen35"] with pytest.raises(lenshift.UnsupportedDialect) as e: lenshift.get("ajax") assert e.value.code == "unsupported_dialect" diff --git a/rust/crates/interplane-lenshift/src/lib.rs b/rust/crates/interplane-lenshift/src/lib.rs index cfa798a..4da2b2a 100644 --- a/rust/crates/interplane-lenshift/src/lib.rs +++ b/rust/crates/interplane-lenshift/src/lib.rs @@ -11,6 +11,7 @@ use serde_json::{json, Map, Value}; pub mod aien_legacy; pub mod ajax; pub mod openai; +pub mod openai_stream; pub mod qwen35; /// What a dialect needs to know to fill request ids and provenance. @@ -72,11 +73,12 @@ impl DialectRegistry { pub fn new() -> Self { Self::default() } - /// `aien_legacy`, `openai` and `qwen35`. `ajax` is reserved and deliberately absent. + /// `aien_legacy`, `openai`, `openai_stream` and `qwen35`. `ajax` is reserved and deliberately absent. pub fn with_defaults() -> Self { let mut r = Self::new(); r.register(Box::new(aien_legacy::AienLegacy)); r.register(Box::new(openai::OpenAi)); + r.register(Box::new(openai_stream::OpenAiStream)); r.register(Box::new(qwen35::Qwen35)); r } @@ -220,7 +222,10 @@ mod tests { #[test] fn unknown_dialect_is_unsupported() { let r = DialectRegistry::with_defaults(); - assert_eq!(r.names(), vec!["aien_legacy", "openai", "qwen35"]); + assert_eq!( + r.names(), + vec!["aien_legacy", "openai", "openai_stream", "qwen35"] + ); assert!(r.get("openai").is_ok()); assert_eq!( r.get("frobnicate").err(), diff --git a/rust/crates/interplane-lenshift/src/openai_stream.rs b/rust/crates/interplane-lenshift/src/openai_stream.rs new file mode 100644 index 0000000..da948a1 --- /dev/null +++ b/rust/crates/interplane-lenshift/src/openai_stream.rs @@ -0,0 +1,230 @@ +//! Dialect `openai_stream` (version 1): a streamed OpenAI-compatible response body (SSE text). +//! The fragments are assembled into one assistant message, which is parsed with the `openai` +//! rules. A stream that did not finish cleanly is rejected call by call, never guessed. +use std::collections::BTreeMap; + +use interplane_core::{digest, ErrorCode, ToolRequest, ToolResult}; +use serde_json::{Map, Value}; + +use crate::openai::OpenAi; +use crate::*; + +pub struct OpenAiStream; + +const NAME: &str = "openai_stream"; +const COMPLETE: [&str; 3] = ["stop", "tool_calls", "function_call"]; + +/// The data of each complete SSE event, in order. +pub fn events(body: &str) -> Vec { + let (mut out, mut data) = (Vec::new(), Vec::<&str>::new()); + for line in body.split('\n') { + let line = line.strip_suffix('\r').unwrap_or(line); + if line.is_empty() { + if !data.is_empty() { + out.push(data.join("\n")); + } + data.clear(); + } else if line == "data" { + data.push(""); + } else if let Some(v) = line.strip_prefix("data:") { + data.push(v.strip_prefix(' ').unwrap_or(v)); + } + } + out +} + +#[derive(Default)] +struct Slot { + id: Option, + name: Option, + arguments: Option, +} + +fn keep_first(slot: &mut Option, value: Option<&Value>, what: &str) -> Result<(), String> { + let Some(v) = value.and_then(Value::as_str).filter(|s| !s.is_empty()) else { + return Ok(()); + }; + match slot { + None => *slot = Some(v.to_string()), + Some(old) if old != v => return Err(format!("conflicting tool call {what}")), + Some(_) => {} + } + Ok(()) +} + +/// `(message, finish_reason)` for a stream body, or the stream error. +pub fn assemble(body: &str) -> Result<(Value, Option), String> { + let (mut text, mut reasoning) = (String::new(), String::new()); + let mut any_reasoning = false; + let mut calls: BTreeMap = BTreeMap::new(); + let mut finish: Option = None; + let mut done = false; + for data in events(body) { + if done { + return Err("event after [DONE]".into()); + } + if data == "[DONE]" { + done = true; + continue; + } + let chunk = match serde_json::from_str::(&data) { + Ok(Value::Object(m)) => m, + _ => return Err("stream chunk is not a JSON object".into()), + }; + let Some(choices) = chunk.get("choices").and_then(Value::as_array) else { + continue; + }; + for choice in choices { + let Some(choice) = choice.as_object() else { + return Err("stream choice is not an object".into()); + }; + if choice.get("index").is_some_and(|i| i.as_u64() != Some(0)) { + return Err("multiple choices are not supported".into()); + } + if let Some(delta) = choice.get("delta").and_then(Value::as_object) { + if let Some(s) = delta.get("content").and_then(Value::as_str) { + text.push_str(s); + } + for key in ["reasoning_content", "reasoning"] { + if let Some(s) = delta.get(key).and_then(Value::as_str) { + reasoning.push_str(s); + any_reasoning = true; + } + } + for frag in delta + .get("tool_calls") + .and_then(Value::as_array) + .into_iter() + .flatten() + { + let Some(index) = frag.get("index").and_then(Value::as_u64) else { + return Err("tool call fragment has no index".into()); + }; + let slot = calls.entry(index).or_default(); + keep_first(&mut slot.id, frag.get("id"), "id")?; + if let Some(function) = frag.get("function").and_then(Value::as_object) { + keep_first(&mut slot.name, function.get("name"), "name")?; + if let Some(s) = function.get("arguments").and_then(Value::as_str) { + slot.arguments.get_or_insert_with(String::new).push_str(s); + } + } + } + } + match choice.get("finish_reason") { + None | Some(Value::Null) => {} + Some(Value::String(r)) => match &finish { + None => finish = Some(r.clone()), + Some(f) if f != r => return Err("conflicting finish_reason".into()), + Some(_) => {} + }, + Some(_) => return Err("finish_reason is not a string".into()), + } + } + } + let mut message = Map::new(); + message.insert("role".into(), "assistant".into()); + message.insert("content".into(), text.into()); + if any_reasoning { + message.insert("reasoning_content".into(), reasoning.into()); + } + if !calls.is_empty() { + let out: Vec = calls + .into_values() + .map(|s| { + let mut function = Map::new(); + if let Some(n) = s.name { + function.insert("name".into(), n.into()); + } + if let Some(a) = s.arguments { + function.insert("arguments".into(), a.into()); + } + let mut call = Map::new(); + call.insert("type".into(), "function".into()); + call.insert("function".into(), function.into()); + if let Some(id) = s.id { + call.insert("id".into(), id.into()); + } + Value::Object(call) + }) + .collect(); + message.insert("tool_calls".into(), out.into()); + } + Ok((Value::Object(message), finish)) +} + +impl Dialect for OpenAiStream { + fn name(&self) -> &str { + NAME + } + fn version(&self) -> &str { + "1" + } + + fn parse(&self, input: &Value, ctx: &ParseContext) -> LenshiftTurn { + let assembled = match input.as_str() { + Some(body) => assemble(body), + None => Err("stream is not text".into()), + }; + let (message, finish) = match assembled { + Ok(v) => v, + Err(e) => { + let source_digest = match input.as_str() { + Some(body) => text_digest(body), + None => digest(input), + }; + return LenshiftTurn { + dialect: NAME.into(), + dialect_version: "1".into(), + parser_version: PARSER_VERSION.into(), + model: ctx.model.clone(), + text: String::new(), + reasoning_digest: None, + intents: vec![], + rejected: vec![RejectedCall { + index: 0, + code: ErrorCode::MalformedToolCall, + message: e, + source_digest: Some(source_digest), + request_id: assign_request_id(None, ctx, 0), + }], + partial: false, + intent_indices: vec![], + }; + } + }; + let mut turn = OpenAi.parse(&message, ctx); + turn.dialect = NAME.into(); + for intent in &mut turn.intents { + intent.provenance.dialect = NAME.into(); + } + if finish.as_deref().is_some_and(|f| COMPLETE.contains(&f)) { + return turn; + } + let intents = std::mem::take(&mut turn.intents); + let indices = std::mem::take(&mut turn.intent_indices); + let mut cut: Vec = indices + .into_iter() + .zip(intents) + .map(|(index, i)| RejectedCall { + index, + code: ErrorCode::MalformedToolCall, + message: String::new(), + source_digest: i.provenance.source_digest, + request_id: i.request_id, + }) + .chain(std::mem::take(&mut turn.rejected)) + .collect(); + cut.sort_by_key(|r| r.index); + for r in &mut cut { + r.code = ErrorCode::MalformedToolCall; + r.message = "truncated tool call".into(); + } + turn.partial = !cut.is_empty(); + turn.rejected = cut; + turn + } + + fn render_result(&self, result: &ToolResult, intent: Option<&ToolRequest>) -> Value { + OpenAi.render_result(result, intent) + } +} diff --git a/rust/crates/interplane-lenshift/tests/dialect_fixtures.rs b/rust/crates/interplane-lenshift/tests/dialect_fixtures.rs index 0960714..0bd6667 100644 --- a/rust/crates/interplane-lenshift/tests/dialect_fixtures.rs +++ b/rust/crates/interplane-lenshift/tests/dialect_fixtures.rs @@ -80,8 +80,27 @@ fn dialect_fixtures_match() { } } assert_eq!(Value::Array(rej), want_rej, "{name} rejected"); + // Digests are masked above; where the Python reference recorded them in + // `expected_digests` (null = not recorded), they must match too. + let d = &fx["expected_digests"]; + if !d["reasoning_digest"].is_null() { + assert_eq!( + json!(t.reasoning_digest), + d["reasoning_digest"], + "{name} reasoning digest" + ); + } + if let Some(want) = d["source_digests"].as_array() { + for (k, w) in want.iter().enumerate().filter(|(_, w)| !w.is_null()) { + assert_eq!( + json!(t.intents[k].provenance.source_digest), + *w, + "{name} source digest {k}" + ); + } + } n += 1; } } - assert!(n >= 31, "expected the 31 dialect fixtures, ran {n}"); + assert!(n >= 54, "expected the 54 dialect fixtures, ran {n}"); } diff --git a/spec/LENSHIFT.md b/spec/LENSHIFT.md index ae145cd..7d3fe90 100644 --- a/spec/LENSHIFT.md +++ b/spec/LENSHIFT.md @@ -33,8 +33,8 @@ turn so ids are filled in one step.) ## Dialect `openai` (version 1) Input: one assistant message object `{role, content, tool_calls?, reasoning_content?}` as returned -by an OpenAI-compatible `/v1/chat/completions` (non-streamed, or streamed-and-assembled by the -caller). +by an OpenAI-compatible `/v1/chat/completions` (non-streamed, or streamed and assembled by the +caller; dialect `openai_stream` below assembles a stream). - `tool_calls[i].function.name` -> raw_name; `tool_calls[i].id` -> source_call_id. - `function.arguments` is a JSON string: parse; must be an object. Not JSON -> `malformed_tool_call` @@ -48,6 +48,58 @@ Render result -> `{"role":"tool","tool_call_id":," where content is `data` JSON-serialized (canonical) for `ok`, otherwise `{"error": {"code":..., "message":...}, "status": ...}` serialized. Never raw secrets. +## Dialect `openai_stream` (version 1) + +Input: the body of a streamed OpenAI-compatible `/v1/chat/completions` response (`stream: true`), as +one string of Server-Sent Events, exactly as the server sent it. Lenshift assembles the fragments +into one assistant message and parses it with the `openai` rules above. Grounded in the six streamed +captures under `qualification/captures/qwen35-{ollama,llamacpp,sglang}/h{1,2}-*.json`: Ollama sends +each tool call whole; llama.cpp sends `id`, `name` and `"{"` first and the arguments in later pieces; +SGLang does the same but repeats `"id": null, "name": null` in every later piece and sends +`content: "\n\n"` before the call. + +Framing (the `data` subset of the WHATWG Server-Sent Events format): +- Split on `\n`; a trailing `\r` on a line is removed. A line `data` or `data:` appends + `` (one leading space removed) to the event's data lines. Lines starting with `:` and all + other fields (`event`, `id`, `retry`) are ignored. A blank line ends an event; its data is the data + lines joined with `\n`. An event not ended by a blank line before the end of input is discarded. +- Event data `[DONE]` ends the stream; any later event is a stream error. Any other data must be a + JSON object (strict JSON, as for arguments); otherwise a stream error. + +Assembly, chunk by chunk in arrival order: +- `choices` absent, not an array, or empty (for example a usage chunk): ignored. A choice that is + not an object: stream error "stream choice is not an object". A choice with an `index` that is not + the integer `0`: stream error "multiple choices are not supported" (an absent `index` counts as 0). +- `delta.content` and `delta.reasoning_content` / `delta.reasoning` strings are concatenated; `null` + is skipped. `delta.role` is ignored. +- `delta.tool_calls[]` (ignored when not an array): each fragment must be an object with an integer + `index` from 0 to 2^64-1 (`1.0` is not an integer), else stream error "tool call fragment has no + index". Per index: the first non-empty string `id` and the first non-empty string + `function.name` are kept; a later, different non-empty value is a stream error ("conflicting tool + call id" / "conflicting tool call name"); `null` and `""` are skipped. `function.arguments` + strings are concatenated. +- `finish_reason`: must be a string or `null`, else stream error "finish_reason is not a string"; the + first non-null value is kept; a later, different non-null value is a stream + error "conflicting finish_reason". + +Assembled message: `{"role": "assistant", "content": , "tool_calls": [...]}` with calls in +ascending `index`, each `{"type": "function", "function": {"name", "arguments"}}` plus `"id"` when one +arrived; `"reasoning_content"` is present when reasoning text arrived. A call with no name or no +arguments keeps the field absent and is rejected by the `openai` rules. + +Completeness: the stream is complete only when `finish_reason` is `stop`, `tool_calls` or +`function_call`. Otherwise (absent, `length`, `content_filter`, anything else) every call of the turn +is rejected `malformed_tool_call` "truncated tool call" with its own index, digest and id, and +`partial = true` when there was at least one call. Text is kept. A cut-off stream is never guessed, +even when a call's arguments already parse. + +Stream errors and non-string input: no intents, `text = ""`, one `rejected` entry at index 0 +(`malformed_tool_call`, the error message, `source_digest` = sha256 of the body text, or of its JCS +form for a non-string), `partial = false`. + +Intents carry `dialect = "openai_stream"`, `dialect_version = "1"`; `source_digest` is of the +assembled call object (JCS), as for `openai`. Render result is the same as `openai`. + ## Dialect `qwen35` (version 1) Grounded in the official `Qwen/Qwen3.5-9B` chat template (sha256 a4aee8af…, lines 53, 105-137). @@ -156,6 +208,11 @@ the abstraction test for that. ## Fixtures `dialects/fixtures//.json`: `{"input": ..., "model": ..., "expected": LenshiftTurn-without-digests, "expected_digests": {...}}`. +Required openai_stream fixtures: every streamed capture under `qualification/captures` byte for byte +(with its `sha256_response`); fragmented call; repeated null fragments; two interleaved calls; cut +mid-arguments; cut after complete arguments; `finish_reason` length; unterminated last event; CRLF +and comment framing; streamed plain text, complete and cut; assembled arguments not JSON; each stream +error. Fixtures record `expected_digests`, and both runners compare every recorded digest. Required qwen35 fixtures: valid single call; multiple calls; malformed (unclosed function); reasoning plus call; plain answer with no tool; unknown tool name (parses fine); partial/truncated call; multi-line parameter value; JSON-typed parameter; hermes_json form. Required aien_legacy fixtures: valid single call; multiple calls; reasoning plus call; repaired bracket; repaired missing braces; trailing comma (rejected); single quotes (rejected); unterminated (rejected, partial); fenced json fallback; placeholder name (rejected); missing arguments; arguments not object; plain answer. Required openai fixtures: