reliable-requests 0.2.1 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +40 -19
- package/LICENSE +21 -21
- package/README.md +207 -207
- package/docs/MODEL_COMPATIBILITY.md +90 -56
- package/docs/STREAM_CONTRACT.md +72 -68
- package/docs/USAGE.md +313 -299
- package/index.d.ts +138 -127
- package/index.js +12 -11
- package/package.json +67 -49
- package/src/endpoint.js +17 -17
- package/src/index.js +128 -128
- package/src/local-chat.js +409 -343
- package/src/metrics.js +46 -46
- package/src/retry.js +54 -48
- package/src/sse.js +93 -93
- package/src/stream-events.js +69 -69
|
@@ -1,56 +1,90 @@
|
|
|
1
|
-
# Model compatibility and recovery checks
|
|
2
|
-
|
|
3
|
-
Compatibility depends on the server's streaming protocol, not the model family. The library sends text chat messages and reads text deltas from Ollama NDJSON or OpenAI-compatible Chat Completions SSE. A different model name does not require a separate adapter when the server keeps that contract.
|
|
4
|
-
|
|
5
|
-
## Evidence so far
|
|
6
|
-
|
|
7
|
-
| Server | Protocol | Recorded live model | Normal response | Forced-disconnect recovery |
|
|
8
|
-
| --- | --- | --- | --- | --- |
|
|
9
|
-
| Ollama | `ollama` | `qwen2.5:0.5b` | Passed on Windows | Passed on Windows |
|
|
10
|
-
| LM Studio | `openai` | `qwen2.5-7b-instruct-uncensored` | Passed in Node and browser on Windows |
|
|
11
|
-
| llama.cpp | `openai` | Qwen 2.5 0.5B | Passed on Windows, build 12212 |
|
|
12
|
-
|
|
13
|
-
These are limited observations, not certification of every server version or model. See [the recorded live checks](https://github.com/Stairrax/reliable-requests/blob/main/docs/LIVE_TESTS_2026-11-29.md) and [the roadmap](https://github.com/Stairrax/reliable-requests/blob/main/ROADMAP.md). Automated fake-server checks verify both protocols and the recovery command without loading a model.
|
|
14
|
-
|
|
15
|
-
##
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
1
|
+
# Model compatibility and recovery checks
|
|
2
|
+
|
|
3
|
+
Compatibility depends on the server's streaming protocol, not the model family. The library sends text chat messages and reads text deltas from Ollama NDJSON or OpenAI-compatible Chat Completions SSE. A different model name does not require a separate adapter when the server keeps that contract.
|
|
4
|
+
|
|
5
|
+
## Evidence so far
|
|
6
|
+
|
|
7
|
+
| Server | Protocol | Recorded live model | Normal response | Forced-disconnect recovery |
|
|
8
|
+
| --- | --- | --- | --- | --- |
|
|
9
|
+
| Ollama | `ollama` | `qwen2.5:0.5b` | Passed on Windows | Passed on Windows |
|
|
10
|
+
| LM Studio | `openai` | `qwen2.5-7b-instruct-uncensored` | Passed in Node and browser on Windows | Passed on Windows (2026-12-02) |
|
|
11
|
+
| llama.cpp | `openai` | Qwen 2.5 0.5B | Passed on Windows, build 12212 | Passed on Windows (2026-12-02) |
|
|
12
|
+
|
|
13
|
+
These are limited observations, not certification of every server version or model. See [the recorded live checks](https://github.com/Stairrax/reliable-requests/blob/main/docs/LIVE_TESTS_2026-11-29.md), [the December 2 recovery results](https://github.com/Stairrax/reliable-requests/blob/main/docs/LIVE_TESTS_2026-12-02.md) and [the roadmap](https://github.com/Stairrax/reliable-requests/blob/main/ROADMAP.md). Automated fake-server checks verify both protocols and the recovery command without loading a model.
|
|
14
|
+
|
|
15
|
+
## Additional Windows observations
|
|
16
|
+
|
|
17
|
+
| Server | First-text and idle deadlines | Bounded stress workload |
|
|
18
|
+
| --- | --- | --- |
|
|
19
|
+
| Ollama | Passed | Default profile passed |
|
|
20
|
+
| llama.cpp | Passed | Default profile passed |
|
|
21
|
+
| LM Studio | Passed | Reduced profile passed; default profile timed out |
|
|
22
|
+
|
|
23
|
+
Live llama.cpp cross-origin streaming, client Stop and a new request passed in Edge, Chrome, Playwright Firefox and WebKit. Playwright WebKit on Windows does not verify Safari on Apple platforms. The recorded workload does not establish server-side cancellation, leak freedom or production performance.
|
|
24
|
+
|
|
25
|
+
## Browser CORS diagnostics
|
|
26
|
+
|
|
27
|
+
A browser origin includes scheme, host and port. The demo at `http://127.0.0.1:4173` and a model endpoint on another port are different origins. A JSON POST generally requires a successful CORS preflight. For an OpenAI-compatible local server, inspect it without generating text:
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
curl -i --max-time 10 -X OPTIONS http://127.0.0.1:1234/v1/chat/completions \
|
|
31
|
+
-H "Origin: http://127.0.0.1:4173" \
|
|
32
|
+
-H "Access-Control-Request-Method: POST" \
|
|
33
|
+
-H "Access-Control-Request-Headers: content-type"
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
For llama.cpp substitute its port; for Ollama use `http://127.0.0.1:11434/api/chat`. A successful preflight should permit the demo origin, POST and the requested headers. Actual responses must also permit the origin. Curl itself does not enforce browser CORS, and a successful preflight alone does not establish live browser compatibility. Check the browser network panel and the server's own CORS settings if the request fails. Keep servers bound to loopback and prefer narrowly scoped allowed origins rather than disabling browser security or exposing the server to the network.
|
|
37
|
+
|
|
38
|
+
### Recorded preflight results (2026-12-02)
|
|
39
|
+
|
|
40
|
+
| Server/configuration | HTTP status | Allow-Origin | Allow-Headers | POST allowed |
|
|
41
|
+
| --- | --- | --- | --- | --- |
|
|
42
|
+
| Ollama | 204 | http://127.0.0.1:4173 | Includes Content-Type | Yes |
|
|
43
|
+
| LM Studio before enabling CORS | 400 | Absent | Absent | Not confirmed |
|
|
44
|
+
| LM Studio after enabling CORS | 200 | * | * | Yes |
|
|
45
|
+
| llama.cpp | 200 | http://127.0.0.1:4173 | * | Yes |
|
|
46
|
+
|
|
47
|
+
llama.cpp also returned Access-Control-Allow-Credentials: true. Wildcard header permissions are sufficient for the tested non-credentialed JSON request; credentialed requests have additional CORS requirements. The observed origin values do not prove that other origins are denied. These are configuration-specific observations, not universal server defaults. The separate live browser checks are recorded in the [December 2 results](https://github.com/Stairrax/reliable-requests/blob/main/docs/LIVE_TESTS_2026-12-02.md).
|
|
48
|
+
|
|
49
|
+
## Run the same recovery check against your server
|
|
50
|
+
|
|
51
|
+
Run these commands from a repository checkout after `npm ci`. Test helpers are not shipped in the npm package. Start your local server and load a text chat model first. Use the exact model ID reported by that server.
|
|
52
|
+
|
|
53
|
+
Git Bash, Linux, or macOS:
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
MODEL_PROTOCOL=ollama MODEL_ENDPOINT=http://127.0.0.1:11434 MODEL_NAME=qwen2.5:0.5b npm run test:model:recovery
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
LM Studio, using the model from the recorded checks:
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
MODEL_PROTOCOL=openai MODEL_ENDPOINT=http://127.0.0.1:1234/v1 MODEL_NAME=qwen2.5-7b-instruct-uncensored MODEL_TIMEOUT_MS=300000 npm run test:model:recovery
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
For llama.cpp or another compatible server, set `MODEL_ENDPOINT` to its actual address and `MODEL_NAME` to its served model ID. The OpenAI endpoint may include `/v1`; a prefix such as `/inference/v1` is preserved.
|
|
66
|
+
|
|
67
|
+
PowerShell:
|
|
68
|
+
|
|
69
|
+
```powershell
|
|
70
|
+
$env:MODEL_PROTOCOL = 'openai'
|
|
71
|
+
$env:MODEL_ENDPOINT = 'http://127.0.0.1:1234/v1'
|
|
72
|
+
$env:MODEL_NAME = 'qwen2.5-7b-instruct-uncensored'
|
|
73
|
+
$env:MODEL_TIMEOUT_MS = '300000'
|
|
74
|
+
npm run test:model:recovery
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
The command starts a temporary proxy on a random loopback port. It forwards the first text event, interrupts that connection, and lets the second response complete. It checks for exactly two attempts, one cut, one retry, and one reset. The server stays running. The old `test:ollama:recovery` command remains available.
|
|
78
|
+
|
|
79
|
+
The proxy only connects to HTTP on `127.0.0.1`, requires its generated token, rejects browser-origin requests, and forwards at most two model requests. It does not forward upstream credentials or follow redirects. This is a local development helper, not a production proxy. `MODEL_TIMEOUT_MS` covers generation and retry together; the helper does not speed up inference.
|
|
80
|
+
|
|
81
|
+
## What to record
|
|
82
|
+
|
|
83
|
+
Save the commit SHA (`git rev-parse HEAD`), Node version (`node --version`), OS, server version, model ID, command, and PASS/FAIL output. Note whether the run was in Node or a browser. Do not include private prompts or credentials. A failed test needs its error and event sequence; it must not be recorded as verified support.
|
|
84
|
+
|
|
85
|
+
## Limits
|
|
86
|
+
|
|
87
|
+
- Text chat only: tool calls, image/audio inputs, and separate reasoning output are not supported by this check.
|
|
88
|
+
- The model may produce a different answer after retry. The earlier partial answer must be discarded on `reset`.
|
|
89
|
+
- A successful Node check does not verify browser CORS or browser access to local servers. Those require a separate live browser check.
|
|
90
|
+
- Slower or larger models need a suitable timeout and enough local memory. No model weights are installed or changed by these commands.
|
package/docs/STREAM_CONTRACT.md
CHANGED
|
@@ -1,68 +1,72 @@
|
|
|
1
|
-
# Stream completion and recovery contract
|
|
2
|
-
|
|
3
|
-
This document records the current behavior of `streamLocalChat`. It does not introduce a new completion mode.
|
|
4
|
-
|
|
5
|
-
## Completion
|
|
6
|
-
|
|
7
|
-
| Input | Result |
|
|
8
|
-
| --- | --- |
|
|
9
|
-
| OpenAI SSE: fully framed `data: [DONE]` event | Success |
|
|
10
|
-
| OpenAI SSE: `finish_reason` followed by EOF, without a complete DONE event | Interrupted stream |
|
|
11
|
-
| OpenAI SSE: partial DONE event or partial data event at EOF | Interrupted stream; incomplete event is discarded |
|
|
12
|
-
| Ollama NDJSON: valid event with `done: true`, including a final line without a newline | Success |
|
|
13
|
-
| Ollama NDJSON: `done_reason` without `done: true`, followed by EOF | Interrupted stream |
|
|
14
|
-
| Both: malformed event, unsupported tool output or protocol limit | Failure, no automatic retry |
|
|
15
|
-
|
|
16
|
-
A completion reason is metadata, not a substitute for the completion marker. A successful protocol completion can still represent truncated text (for example, reason `length`). No reason is fabricated when the server omits it. Each attempt starts with a fresh parser and completion state.
|
|
17
|
-
|
|
18
|
-
## Retry decisions
|
|
19
|
-
|
|
20
|
-
| Failure | Eligible for another attempt? |
|
|
21
|
-
| --- | --- |
|
|
22
|
-
| HTTP 429, 500, 502, 503, 504 | Yes |
|
|
23
|
-
| Other HTTP errors | No |
|
|
24
|
-
| Fetch or reader failure | Only if the transport predicate accepts it (default: TypeError) |
|
|
25
|
-
| EOF without completion, missing body, incomplete UTF-8 at EOF | Yes |
|
|
26
|
-
| Invalid UTF-8 detected while decoding, invalid JSON or event fields | No |
|
|
27
|
-
| Model-reported error, unsupported output, redirect, parser limit | No |
|
|
28
|
-
| Exception from an event callback or retry predicate | No |
|
|
29
|
-
| Cancellation
|
|
30
|
-
|
|
31
|
-
Eligibility is not a guarantee of a retry. The remaining retry budget, total deadline, Retry-After delay and explicit remote-retry permission still apply. The transport predicate cannot override other safety rules. A fresh generation is not a continuation and can produce different text or incur another charge.
|
|
32
|
-
|
|
33
|
-
## Event ordering
|
|
34
|
-
|
|
35
|
-
With visible text followed by a recoverable failure, the normal sequence is:
|
|
36
|
-
|
|
37
|
-
`attempt(1) -> delta(1)* -> retry(1) -> reset(2) -> attempt(2) -> delta(2)* -> done(2)`
|
|
38
|
-
|
|
39
|
-
The reset is emitted after the retry delay, before the next attempt and before its first delta. If no text was emitted, reset is omitted. If cancelled during the delay, no reset or next attempt is emitted. If a callback throws, the exception is propagated without another request. A terminal failure does not automatically clear visible text; the application must choose how to mark or remove a failed answer. The UI must process reset synchronously before appending later deltas.
|
|
40
|
-
|
|
41
|
-
## Timeout scope
|
|
42
|
-
|
|
43
|
-
The default streaming timeout is 120,000 ms; the general request wrapper defaults to 30,000 ms. Both cover their respective total operations, including retry delays (and response consumption when applicable). A synchronous callback cannot be preempted.
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
|
67
|
-
|
|
68
|
-
|
|
1
|
+
# Stream completion and recovery contract
|
|
2
|
+
|
|
3
|
+
This document records the current behavior of `streamLocalChat`. It does not introduce a new completion mode.
|
|
4
|
+
|
|
5
|
+
## Completion
|
|
6
|
+
|
|
7
|
+
| Input | Result |
|
|
8
|
+
| --- | --- |
|
|
9
|
+
| OpenAI SSE: fully framed `data: [DONE]` event | Success |
|
|
10
|
+
| OpenAI SSE: `finish_reason` followed by EOF, without a complete DONE event | Interrupted stream |
|
|
11
|
+
| OpenAI SSE: partial DONE event or partial data event at EOF | Interrupted stream; incomplete event is discarded |
|
|
12
|
+
| Ollama NDJSON: valid event with `done: true`, including a final line without a newline | Success |
|
|
13
|
+
| Ollama NDJSON: `done_reason` without `done: true`, followed by EOF | Interrupted stream |
|
|
14
|
+
| Both: malformed event, unsupported tool output or protocol limit | Failure, no automatic retry |
|
|
15
|
+
|
|
16
|
+
A completion reason is metadata, not a substitute for the completion marker. A successful protocol completion can still represent truncated text (for example, reason `length`). No reason is fabricated when the server omits it. Each attempt starts with a fresh parser and completion state.
|
|
17
|
+
|
|
18
|
+
## Retry decisions
|
|
19
|
+
|
|
20
|
+
| Failure | Eligible for another attempt? |
|
|
21
|
+
| --- | --- |
|
|
22
|
+
| HTTP 429, 500, 502, 503, 504 | Yes |
|
|
23
|
+
| Other HTTP errors | No |
|
|
24
|
+
| Fetch or reader failure | Only if the transport predicate accepts it (default: TypeError) |
|
|
25
|
+
| EOF without completion, missing body, incomplete UTF-8 at EOF | Yes |
|
|
26
|
+
| Invalid UTF-8 detected while decoding, invalid JSON or event fields | No |
|
|
27
|
+
| Model-reported error, unsupported output, redirect, parser limit | No |
|
|
28
|
+
| Exception from an event callback or retry predicate | No |
|
|
29
|
+
| Cancellation, total deadline or optional stream phase timeout reached | No |
|
|
30
|
+
|
|
31
|
+
Eligibility is not a guarantee of a retry. The remaining retry budget, total deadline, Retry-After delay and explicit remote-retry permission still apply. The transport predicate cannot override other safety rules. A fresh generation is not a continuation and can produce different text or incur another charge.
|
|
32
|
+
|
|
33
|
+
## Event ordering
|
|
34
|
+
|
|
35
|
+
With visible text followed by a recoverable failure, the normal sequence is:
|
|
36
|
+
|
|
37
|
+
`attempt(1) -> delta(1)* -> retry(1) -> reset(2) -> attempt(2) -> delta(2)* -> done(2)`
|
|
38
|
+
|
|
39
|
+
The reset is emitted after the retry delay, before the next attempt and before its first delta. If no text was emitted, reset is omitted. If cancelled during the delay, no reset or next attempt is emitted. If a callback throws, the exception is propagated without another request. A terminal failure does not automatically clear visible text; the application must choose how to mark or remove a failed answer. The UI must process reset synchronously before appending later deltas.
|
|
40
|
+
|
|
41
|
+
## Timeout scope
|
|
42
|
+
|
|
43
|
+
The default streaming timeout is 120,000 ms; the general request wrapper defaults to 30,000 ms. Both cover their respective total operations, including retry delays (and response consumption when applicable). A synchronous callback cannot be preempted.
|
|
44
|
+
|
|
45
|
+
Optional `firstTextTimeoutMs` measures from each attempt's request start (including response headers) to the first nonempty text delta. Optional `idleTimeoutMs` starts after the first nonempty text delta and restarts at each subsequent nonempty text delta. It also covers waiting for the completion marker after the last text. Heartbeats, role metadata, empty deltas and incomplete protocol frames do not restart either clock. A valid completion without any text succeeds if received within the active deadlines.
|
|
46
|
+
|
|
47
|
+
Both options are disabled when omitted and otherwise require integer milliseconds from 1 to 2147483647. Phase clocks are per attempt and inactive during retry backoff. The total deadline always remains active. A phase timeout aborts the operation with `StreamTimeoutError` (`code: 'STREAM_TIMEOUT'`, `phase: 'first-text' | 'idle'`, `timeoutMs`); it is terminal and does not trigger a retry or reset. Already emitted text stays visible. Total timeout and caller cancellation keep their existing error types. No change is made to `reliableRequest` options.
|
|
48
|
+
|
|
49
|
+
## Optional live recovery check
|
|
50
|
+
|
|
51
|
+
The existing Ollama test remains available. The shared Node.js-only command exercises both Ollama and OpenAI-compatible servers through a temporary loopback HTTP proxy. It forwards the first valid text event and cuts the first TCP connection; the second request is passed through. The proxy accepts only authenticated requests from the test client, rejects browser origins, allows at most two requests, and only connects to an HTTP server at 127.0.0.1. The first text event is re-framed using its original protocol; the second response is forwarded unchanged. Do not use this test against remote or billable endpoints. Authentication to the upstream model server is not supported by this test.
|
|
52
|
+
|
|
53
|
+
PowerShell, LM Studio (set the exact loaded model identifier):
|
|
54
|
+
|
|
55
|
+
```powershell
|
|
56
|
+
$env:MODEL_PROTOCOL = 'openai'
|
|
57
|
+
$env:MODEL_ENDPOINT = 'http://127.0.0.1:1234/v1'
|
|
58
|
+
$env:MODEL_NAME = 'your-loaded-model-id'
|
|
59
|
+
npm run test:model:recovery
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
For Ollama, set MODEL_PROTOCOL to ollama and use the Ollama endpoint and model ID. For llama.cpp, use its actual listening port (for example 8080) and served model identifier. Increase `MODEL_TIMEOUT_MS` for slow inference if necessary. Expected result: two attempts, one TCP cut, one retry, one reset, a nonempty second answer and a valid completion marker.
|
|
63
|
+
|
|
64
|
+
## Live validation record template
|
|
65
|
+
|
|
66
|
+
| Server / build | Model ID | Node / OS | Checkout SHA | Normal | TCP recovery | Cancel | Timeout | CORS / browser |
|
|
67
|
+
| --- | --- | --- | --- | --- | --- | --- | --- | --- |
|
|
68
|
+
| LM Studio | | | | | | | | |
|
|
69
|
+
| llama.cpp | | | | | | | | |
|
|
70
|
+
| Ollama | | | | | | | | |
|
|
71
|
+
|
|
72
|
+
Record PASS, FAIL, or NOT RUN separately for each check; include observed event order and errors. The synthetic TCP test uses a mock upstream. It is not evidence that a real model server passed.
|