reliable-requests 0.2.1 → 0.3.0
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 +33 -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 +51 -49
- package/src/endpoint.js +17 -17
- package/src/index.js +128 -128
- package/src/local-chat.js +403 -343
- package/src/metrics.js +46 -46
- package/src/retry.js +48 -48
- package/src/sse.js +93 -93
- package/src/stream-events.js +69 -69
package/CHANGELOG.md
CHANGED
|
@@ -1,19 +1,33 @@
|
|
|
1
|
-
# Changelog
|
|
2
|
-
|
|
3
|
-
##
|
|
4
|
-
|
|
5
|
-
-
|
|
6
|
-
-
|
|
7
|
-
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.3.0 — prepared 2026-12-02
|
|
4
|
+
|
|
5
|
+
- Add optional first-text and text-idle deadlines for model streams with a distinct public `StreamTimeoutError`, leaving existing timeout and retry behavior unchanged when omitted.
|
|
6
|
+
- Include TypeScript declarations and package consumer checks for the new timeout options and error.
|
|
7
|
+
- Add optional live browser verification with Stop, late-event checks and a subsequent completed generation.
|
|
8
|
+
- Add bounded cross-protocol model stress diagnostics, including a reduced profile for slower models, overlapping clients, repeated cancellations and client memory observations.
|
|
9
|
+
- Record manual Windows recovery, phase-timeout and CORS preflight observations on Ollama, LM Studio and llama.cpp. Default stress runs passed on Ollama and llama.cpp; LM Studio passed the reduced workload after a default-profile timeout.
|
|
10
|
+
- Add release-only npm trusted publishing validation and archive verification, with no publishing on pull requests.
|
|
11
|
+
- Run core CI on test and documentation branches.
|
|
12
|
+
|
|
13
|
+
## 0.2.1 — prepared 2026-12-02
|
|
14
|
+
|
|
15
|
+
- Unify the local recovery test proxy and CLI across Ollama and OpenAI-compatible servers; preserve the original Ollama command.
|
|
16
|
+
- Document and test completion markers, retry/reset ordering, cancellation, callback failures and fresh attempt metadata.
|
|
17
|
+
- Verify one-choice requests and real TCP fault recovery against fake upstream servers. Live LM Studio and llama.cpp recovery is still pending.
|
|
18
|
+
|
|
19
|
+
- Keep fault-proxy requests text-only by forwarding only role and content fields.
|
|
20
|
+
- Include the model compatibility and stream contract guides in the npm archive and verify packaged documentation links.
|
|
21
|
+
- Verify archive, installed package and lockfile versions against the source manifest instead of hard-coding the previous release number.
|
|
22
|
+
|
|
23
|
+
## 0.2.0 — 2026-11-30
|
|
24
|
+
|
|
25
|
+
- Add the public `reliable-requests` entry for HTTP requests, local model streams, metrics, and public error classes. Internal module paths are not package exports.
|
|
26
|
+
- Include TypeScript declarations with inferred `consume` results and discriminated stream events. Runtime code remains JavaScript with no runtime dependencies.
|
|
27
|
+
- Define the ESM package contents and verify an installed npm archive in a clean consumer project, including NodeNext and Bundler type resolution.
|
|
28
|
+
- Use MIT for the current source and published package. The existing v0.1.0 tag and release retain GPL-3.0-only.
|
|
29
|
+
- Preserve the existing request, retry, reset, and cancellation behavior.
|
|
30
|
+
|
|
31
|
+
## 0.1.0
|
|
32
|
+
|
|
33
|
+
Initial GitHub release: HTTP request deadlines and retries, Ollama/OpenAI-compatible streams, reset events, metrics, and the browser demo. Released under GPL-3.0-only; not published to npm.
|
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2026 Stairrax
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Stairrax
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,207 +1,207 @@
|
|
|
1
|
-
<div align="center">
|
|
2
|
-
|
|
3
|
-
<h1>reliable-requests</h1>
|
|
4
|
-
|
|
5
|
-
<strong>Recover from interrupted AI streams without mixing two different answers.</strong>
|
|
6
|
-
|
|
7
|
-
A small JavaScript toolkit for resilient HTTP requests and local AI streaming.<br>
|
|
8
|
-
No runtime dependencies. No telemetry. No framework required.
|
|
9
|
-
|
|
10
|
-
[](https://www.npmjs.com/package/reliable-requests)
|
|
11
|
-
[](LICENSE)
|
|
12
|
-
[](package.json)
|
|
13
|
-
[](package.json)
|
|
14
|
-
[](index.d.ts)
|
|
15
|
-
|
|
16
|
-
[](https://github.com/Stairrax/reliable-requests/actions/workflows/test.yml)
|
|
17
|
-
[](https://github.com/Stairrax/reliable-requests/actions/workflows/browser.yml)
|
|
18
|
-
|
|
19
|
-
[Quick start](#quick-start) • [Demo](#try-the-demo) • [Documentation](#documentation) • [Contributing](#contributing)
|
|
20
|
-
|
|
21
|
-
</div>
|
|
22
|
-
|
|
23
|
-
---
|
|
24
|
-
|
|
25
|
-
## Why reliable-requests?
|
|
26
|
-
|
|
27
|
-
Local AI servers can stop responding halfway through an answer. Retrying without clearing the partial output can leave two unrelated generations mixed together.
|
|
28
|
-
|
|
29
|
-
reliable-requests makes recovery explicit: when a retry starts a new generation after partial output, the application receives a `reset` event.
|
|
30
|
-
|
|
31
|
-
- **Recover cleanly.** Discard the old partial answer before displaying the new one.
|
|
32
|
-
- **Stop waiting forever.** Keep one deadline across attempts, response reading and retry delays when using the streaming API.
|
|
33
|
-
- **Stay in control.** Cancel generation at any time. Remote model retries require explicit opt-in.
|
|
34
|
-
- **Keep it lightweight.** Native Fetch API, ESM, zero runtime dependencies.
|
|
35
|
-
|
|
36
|
-
## Features
|
|
37
|
-
|
|
38
|
-
- 🔄 Automatic restart of interrupted text streams with an explicit reset event
|
|
39
|
-
- 🔌 Ollama NDJSON and OpenAI-compatible SSE support
|
|
40
|
-
- ⏱️ Shared total deadline, cancellation and jittered retries with Retry-After support
|
|
41
|
-
- 🧩 Incremental parsing of multiline SSE events and fragmented UTF-8
|
|
42
|
-
- 🛡️ Bounded protocol line and event sizes
|
|
43
|
-
- 📊 Optional local stream metrics without telemetry
|
|
44
|
-
- 📦 TypeScript declarations included
|
|
45
|
-
- 🧪 Automated tests, real HTTP fault injection and an interactive browser demo
|
|
46
|
-
|
|
47
|
-
## Requirements
|
|
48
|
-
|
|
49
|
-
- Node.js 18+ or a modern browser with Fetch API support
|
|
50
|
-
- An ES module environment; browser npm imports need a bundler or import map
|
|
51
|
-
- A running model server and an installed model for live AI requests only
|
|
52
|
-
|
|
53
|
-
The simulated demo and core automated tests do not require a model or GPU.
|
|
54
|
-
|
|
55
|
-
## Quick start
|
|
56
|
-
|
|
57
|
-
### 1. Install
|
|
58
|
-
|
|
59
|
-
```bash
|
|
60
|
-
npm install reliable-requests
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
### 2. Stream a local model
|
|
64
|
-
|
|
65
|
-
Start Ollama with an installed model, then run this example in an ES module:
|
|
66
|
-
|
|
67
|
-
```js
|
|
68
|
-
import { streamLocalChat } from 'reliable-requests';
|
|
69
|
-
|
|
70
|
-
let answer = '';
|
|
71
|
-
|
|
72
|
-
await streamLocalChat({
|
|
73
|
-
endpoint: 'http://127.0.0.1:11434',
|
|
74
|
-
protocol: 'ollama',
|
|
75
|
-
model: 'qwen2.5:0.5b',
|
|
76
|
-
messages: [{ role: 'user', content: 'Hello!' }],
|
|
77
|
-
onEvent(event) {
|
|
78
|
-
if (event.type === 'reset') answer = '';
|
|
79
|
-
if (event.type === 'delta') answer += event.text;
|
|
80
|
-
if (event.type === 'done') console.log(answer);
|
|
81
|
-
},
|
|
82
|
-
});
|
|
83
|
-
```
|
|
84
|
-
|
|
85
|
-
Replace the model name with one you have installed. For LM Studio or llama.cpp, use `protocol: 'openai'` and your server's endpoint.
|
|
86
|
-
|
|
87
|
-
For ordinary HTTP requests, the package also exports `reliableRequest`:
|
|
88
|
-
|
|
89
|
-
```js
|
|
90
|
-
import { reliableRequest } from 'reliable-requests';
|
|
91
|
-
|
|
92
|
-
const data = await reliableRequest('https://example.com/api/data', {
|
|
93
|
-
timeoutMs: 8_000,
|
|
94
|
-
retries: 2,
|
|
95
|
-
consume: response => response.json(),
|
|
96
|
-
});
|
|
97
|
-
```
|
|
98
|
-
|
|
99
|
-
Without a `consume` callback, the general HTTP wrapper's deadline ends when response headers arrive. See the [usage guide](docs/USAGE.md) for cancellation, TypeScript configuration and metrics.
|
|
100
|
-
|
|
101
|
-
## Try the demo
|
|
102
|
-
|
|
103
|
-
```bash
|
|
104
|
-
git clone https://github.com/Stairrax/reliable-requests.git
|
|
105
|
-
cd reliable-requests
|
|
106
|
-
npm run demo
|
|
107
|
-
```
|
|
108
|
-
|
|
109
|
-
Open [localhost:4173](http://127.0.0.1:4173) and select **Simulate interruption**. Watch an unfinished answer disappear before a fresh generation is displayed.
|
|
110
|
-
|
|
111
|
-
Switch to **Live local model** to connect to your own model server. The server must allow the demo's browser origin through CORS.
|
|
112
|
-
|
|
113
|
-
## How it works
|
|
114
|
-
|
|
115
|
-
```mermaid
|
|
116
|
-
flowchart LR
|
|
117
|
-
A[Start generation] --> B[Stream text]
|
|
118
|
-
B --> C{Completed?}
|
|
119
|
-
C -->|Yes| D[Done]
|
|
120
|
-
C -->|Recoverable failure| E{Retry allowed?}
|
|
121
|
-
E -->|Yes| F[Reset if partial text exists]
|
|
122
|
-
F --> A
|
|
123
|
-
E -->|No| G[Report error]
|
|
124
|
-
C -->|Terminal failure| G
|
|
125
|
-
```
|
|
126
|
-
|
|
127
|
-
A retry starts a **new generation**, not a continuation of the old one. Results may differ, and another request may consume more compute or incur additional charges. Automatic retries of remote model requests require explicit permission.
|
|
128
|
-
|
|
129
|
-
OpenAI-compatible streams require a complete `[DONE]` event; Ollama streams require `done: true`. The library handles text-only generation and does not execute tool calls.
|
|
130
|
-
|
|
131
|
-
## Compatibility
|
|
132
|
-
|
|
133
|
-
| Server | Protocol | Live streaming | Live interruption recovery |
|
|
134
|
-
| --- | --- | --- | --- |
|
|
135
|
-
| Ollama | NDJSON | Verified | Verified |
|
|
136
|
-
| LM Studio | SSE | Verified |
|
|
137
|
-
| llama.cpp | SSE | Verified |
|
|
138
|
-
|
|
139
|
-
Automated tests cover Node.js 18 and 22, both protocols and simulated browser flows in Chromium, Firefox and WebKit.
|
|
140
|
-
|
|
141
|
-
See the [live test record](https://github.com/Stairrax/reliable-requests/blob/main/docs/LIVE_TESTS_2026-11-29.md) and [roadmap](https://github.com/Stairrax/reliable-requests/blob/main/ROADMAP.md) for the exact scope and remaining work.
|
|
142
|
-
|
|
143
|
-
## Testing
|
|
144
|
-
|
|
145
|
-
```bash
|
|
146
|
-
npm ci
|
|
147
|
-
npm run check
|
|
148
|
-
```
|
|
149
|
-
|
|
150
|
-
This runs the automated test suite and verifies the actual npm package, including TypeScript consumer checks.
|
|
151
|
-
|
|
152
|
-
For an installed Ollama model (Git Bash, macOS or Linux):
|
|
153
|
-
|
|
154
|
-
```bash
|
|
155
|
-
MODEL_NAME=qwen2.5:0.5b npm run test:model
|
|
156
|
-
MODEL_NAME=qwen2.5:0.5b npm run test:ollama:recovery
|
|
157
|
-
```
|
|
158
|
-
|
|
159
|
-
The recovery test deliberately cuts one local connection without stopping Ollama. For LM Studio and llama.cpp, use `npm run test:model:recovery` with `MODEL_PROTOCOL=openai`, the server endpoint and exact model ID. Use only a local, non-billable server. See the [model compatibility guide](docs/MODEL_COMPATIBILITY.md) for Bash and PowerShell commands.
|
|
160
|
-
|
|
161
|
-
## Project layout
|
|
162
|
-
|
|
163
|
-
```text
|
|
164
|
-
reliable-requests/
|
|
165
|
-
├── index.js Public package entry
|
|
166
|
-
├── index.d.ts TypeScript declarations
|
|
167
|
-
├── src/
|
|
168
|
-
│ ├── index.js General HTTP wrapper
|
|
169
|
-
│ ├── local-chat.js Model streaming and recovery
|
|
170
|
-
│ ├── sse.js Incremental stream parsers
|
|
171
|
-
│ ├── stream-events.js Event validation
|
|
172
|
-
│ ├── retry.js Shared retry policy
|
|
173
|
-
│ ├── endpoint.js Endpoint validation
|
|
174
|
-
│ └── metrics.js Local stream metrics
|
|
175
|
-
├── examples/ Browser demo and live model checks
|
|
176
|
-
├── test/ Unit and HTTP integration tests
|
|
177
|
-
├── scripts/ Browser and package checks
|
|
178
|
-
└── docs/ Detailed documentation
|
|
179
|
-
```
|
|
180
|
-
|
|
181
|
-
## Documentation
|
|
182
|
-
|
|
183
|
-
| Guide | Description |
|
|
184
|
-
| --- | --- |
|
|
185
|
-
| [Usage](docs/USAGE.md) | API, configuration, errors and examples |
|
|
186
|
-
| [Stream contract](docs/STREAM_CONTRACT.md) | Completion, retries and event ordering |
|
|
187
|
-
| [Model compatibility](docs/MODEL_COMPATIBILITY.md) | Live recovery checks for both protocols |
|
|
188
|
-
| [Roadmap](https://github.com/Stairrax/reliable-requests/blob/main/ROADMAP.md) | Current progress and planned improvements |
|
|
189
|
-
| [Changelog](CHANGELOG.md) | Release history |
|
|
190
|
-
| [Release checklist](https://github.com/Stairrax/reliable-requests/blob/main/docs/RELEASING.md) | Package release process |
|
|
191
|
-
| [Live test record](https://github.com/Stairrax/reliable-requests/blob/main/docs/LIVE_TESTS_2026-11-29.md) | Recorded model compatibility checks |
|
|
192
|
-
|
|
193
|
-
## Contributing
|
|
194
|
-
|
|
195
|
-
Issues and pull requests are welcome. Bug reports, reproducible stream failures, tests and documentation improvements are especially useful.
|
|
196
|
-
|
|
197
|
-
Keep the runtime dependency-free, preserve explicit retry semantics, and read [CONTRIBUTING.md](https://github.com/Stairrax/reliable-requests/blob/main/CONTRIBUTING.md) before opening a PR. For vulnerability reports, see [SECURITY.md](https://github.com/Stairrax/reliable-requests/blob/main/SECURITY.md).
|
|
198
|
-
|
|
199
|
-
## Acknowledgements
|
|
200
|
-
|
|
201
|
-
Thanks to [TaKeSh13](https://github.com/GiTakeshi) for helping discuss and develop solutions for local stream completion and callback reliability in [PR #4](https://github.com/Stairrax/reliable-requests/pull/4).
|
|
202
|
-
|
|
203
|
-
## License
|
|
204
|
-
|
|
205
|
-
[MIT](LICENSE) © 2026 Stairrax.
|
|
206
|
-
|
|
207
|
-
Free to use, modify and distribute, including in commercial and closed-source applications, subject to the MIT license. Provided without warranty.
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+
<h1>reliable-requests</h1>
|
|
4
|
+
|
|
5
|
+
<strong>Recover from interrupted AI streams without mixing two different answers.</strong>
|
|
6
|
+
|
|
7
|
+
A small JavaScript toolkit for resilient HTTP requests and local AI streaming.<br>
|
|
8
|
+
No runtime dependencies. No telemetry. No framework required.
|
|
9
|
+
|
|
10
|
+
[](https://www.npmjs.com/package/reliable-requests)
|
|
11
|
+
[](LICENSE)
|
|
12
|
+
[](package.json)
|
|
13
|
+
[](package.json)
|
|
14
|
+
[](index.d.ts)
|
|
15
|
+
|
|
16
|
+
[](https://github.com/Stairrax/reliable-requests/actions/workflows/test.yml)
|
|
17
|
+
[](https://github.com/Stairrax/reliable-requests/actions/workflows/browser.yml)
|
|
18
|
+
|
|
19
|
+
[Quick start](#quick-start) • [Demo](#try-the-demo) • [Documentation](#documentation) • [Contributing](#contributing)
|
|
20
|
+
|
|
21
|
+
</div>
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
## Why reliable-requests?
|
|
26
|
+
|
|
27
|
+
Local AI servers can stop responding halfway through an answer. Retrying without clearing the partial output can leave two unrelated generations mixed together.
|
|
28
|
+
|
|
29
|
+
reliable-requests makes recovery explicit: when a retry starts a new generation after partial output, the application receives a `reset` event.
|
|
30
|
+
|
|
31
|
+
- **Recover cleanly.** Discard the old partial answer before displaying the new one.
|
|
32
|
+
- **Stop waiting forever.** Keep one deadline across attempts, response reading and retry delays when using the streaming API.
|
|
33
|
+
- **Stay in control.** Cancel generation at any time. Remote model retries require explicit opt-in.
|
|
34
|
+
- **Keep it lightweight.** Native Fetch API, ESM, zero runtime dependencies.
|
|
35
|
+
|
|
36
|
+
## Features
|
|
37
|
+
|
|
38
|
+
- 🔄 Automatic restart of interrupted text streams with an explicit reset event
|
|
39
|
+
- 🔌 Ollama NDJSON and OpenAI-compatible SSE support
|
|
40
|
+
- ⏱️ Shared total deadline, optional first-text and idle deadlines, cancellation and jittered retries with Retry-After support
|
|
41
|
+
- 🧩 Incremental parsing of multiline SSE events and fragmented UTF-8
|
|
42
|
+
- 🛡️ Bounded protocol line and event sizes
|
|
43
|
+
- 📊 Optional local stream metrics without telemetry
|
|
44
|
+
- 📦 TypeScript declarations included
|
|
45
|
+
- 🧪 Automated tests, real HTTP fault injection and an interactive browser demo
|
|
46
|
+
|
|
47
|
+
## Requirements
|
|
48
|
+
|
|
49
|
+
- Node.js 18+ or a modern browser with Fetch API support
|
|
50
|
+
- An ES module environment; browser npm imports need a bundler or import map
|
|
51
|
+
- A running model server and an installed model for live AI requests only
|
|
52
|
+
|
|
53
|
+
The simulated demo and core automated tests do not require a model or GPU.
|
|
54
|
+
|
|
55
|
+
## Quick start
|
|
56
|
+
|
|
57
|
+
### 1. Install
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
npm install reliable-requests
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### 2. Stream a local model
|
|
64
|
+
|
|
65
|
+
Start Ollama with an installed model, then run this example in an ES module:
|
|
66
|
+
|
|
67
|
+
```js
|
|
68
|
+
import { streamLocalChat } from 'reliable-requests';
|
|
69
|
+
|
|
70
|
+
let answer = '';
|
|
71
|
+
|
|
72
|
+
await streamLocalChat({
|
|
73
|
+
endpoint: 'http://127.0.0.1:11434',
|
|
74
|
+
protocol: 'ollama',
|
|
75
|
+
model: 'qwen2.5:0.5b',
|
|
76
|
+
messages: [{ role: 'user', content: 'Hello!' }],
|
|
77
|
+
onEvent(event) {
|
|
78
|
+
if (event.type === 'reset') answer = '';
|
|
79
|
+
if (event.type === 'delta') answer += event.text;
|
|
80
|
+
if (event.type === 'done') console.log(answer);
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Replace the model name with one you have installed. For LM Studio or llama.cpp, use `protocol: 'openai'` and your server's endpoint.
|
|
86
|
+
|
|
87
|
+
For ordinary HTTP requests, the package also exports `reliableRequest`:
|
|
88
|
+
|
|
89
|
+
```js
|
|
90
|
+
import { reliableRequest } from 'reliable-requests';
|
|
91
|
+
|
|
92
|
+
const data = await reliableRequest('https://example.com/api/data', {
|
|
93
|
+
timeoutMs: 8_000,
|
|
94
|
+
retries: 2,
|
|
95
|
+
consume: response => response.json(),
|
|
96
|
+
});
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Without a `consume` callback, the general HTTP wrapper's deadline ends when response headers arrive. See the [usage guide](docs/USAGE.md) for cancellation, TypeScript configuration and metrics.
|
|
100
|
+
|
|
101
|
+
## Try the demo
|
|
102
|
+
|
|
103
|
+
```bash
|
|
104
|
+
git clone https://github.com/Stairrax/reliable-requests.git
|
|
105
|
+
cd reliable-requests
|
|
106
|
+
npm run demo
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
Open [localhost:4173](http://127.0.0.1:4173) and select **Simulate interruption**. Watch an unfinished answer disappear before a fresh generation is displayed.
|
|
110
|
+
|
|
111
|
+
Switch to **Live local model** to connect to your own model server. The server must allow the demo's browser origin through CORS.
|
|
112
|
+
|
|
113
|
+
## How it works
|
|
114
|
+
|
|
115
|
+
```mermaid
|
|
116
|
+
flowchart LR
|
|
117
|
+
A[Start generation] --> B[Stream text]
|
|
118
|
+
B --> C{Completed?}
|
|
119
|
+
C -->|Yes| D[Done]
|
|
120
|
+
C -->|Recoverable failure| E{Retry allowed?}
|
|
121
|
+
E -->|Yes| F[Reset if partial text exists]
|
|
122
|
+
F --> A
|
|
123
|
+
E -->|No| G[Report error]
|
|
124
|
+
C -->|Terminal failure| G
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
A retry starts a **new generation**, not a continuation of the old one. Results may differ, and another request may consume more compute or incur additional charges. Automatic retries of remote model requests require explicit permission.
|
|
128
|
+
|
|
129
|
+
OpenAI-compatible streams require a complete `[DONE]` event; Ollama streams require `done: true`. The library handles text-only generation and does not execute tool calls.
|
|
130
|
+
|
|
131
|
+
## Compatibility
|
|
132
|
+
|
|
133
|
+
| Server | Protocol | Live streaming | Live interruption recovery |
|
|
134
|
+
| --- | --- | --- | --- |
|
|
135
|
+
| Ollama | NDJSON | Verified | Verified |
|
|
136
|
+
| LM Studio | SSE | Verified | Verified |
|
|
137
|
+
| llama.cpp | SSE | Verified | Verified |
|
|
138
|
+
|
|
139
|
+
Automated tests cover Node.js 18 and 22, both protocols and simulated browser flows in Chromium, Firefox and WebKit. On Windows, live phase-timeout checks passed on all three servers, live llama.cpp browser checks passed in Edge, Chrome, Playwright Firefox and WebKit, and bounded stress checks passed on Ollama and llama.cpp and under a reduced profile on LM Studio (the default profile timed out). These are limited observations with Qwen models, not a guarantee of compatibility with every server, browser or model.
|
|
140
|
+
|
|
141
|
+
See the [November 29 live test record](https://github.com/Stairrax/reliable-requests/blob/main/docs/LIVE_TESTS_2026-11-29.md), [December 2 recovery, browser, timeout and stress results](https://github.com/Stairrax/reliable-requests/blob/main/docs/LIVE_TESTS_2026-12-02.md) and [roadmap](https://github.com/Stairrax/reliable-requests/blob/main/ROADMAP.md) for the exact scope and remaining work.
|
|
142
|
+
|
|
143
|
+
## Testing
|
|
144
|
+
|
|
145
|
+
```bash
|
|
146
|
+
npm ci
|
|
147
|
+
npm run check
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
This runs the automated test suite and verifies the actual npm package, including TypeScript consumer checks.
|
|
151
|
+
|
|
152
|
+
For an installed Ollama model (Git Bash, macOS or Linux):
|
|
153
|
+
|
|
154
|
+
```bash
|
|
155
|
+
MODEL_NAME=qwen2.5:0.5b npm run test:model
|
|
156
|
+
MODEL_NAME=qwen2.5:0.5b npm run test:ollama:recovery
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
The recovery test deliberately cuts one local connection without stopping Ollama. For LM Studio and llama.cpp, use `npm run test:model:recovery` with `MODEL_PROTOCOL=openai`, the server endpoint and exact model ID. Use only a local, non-billable server. See the [model compatibility guide](docs/MODEL_COMPATIBILITY.md) for Bash and PowerShell commands.
|
|
160
|
+
|
|
161
|
+
## Project layout
|
|
162
|
+
|
|
163
|
+
```text
|
|
164
|
+
reliable-requests/
|
|
165
|
+
├── index.js Public package entry
|
|
166
|
+
├── index.d.ts TypeScript declarations
|
|
167
|
+
├── src/
|
|
168
|
+
│ ├── index.js General HTTP wrapper
|
|
169
|
+
│ ├── local-chat.js Model streaming and recovery
|
|
170
|
+
│ ├── sse.js Incremental stream parsers
|
|
171
|
+
│ ├── stream-events.js Event validation
|
|
172
|
+
│ ├── retry.js Shared retry policy
|
|
173
|
+
│ ├── endpoint.js Endpoint validation
|
|
174
|
+
│ └── metrics.js Local stream metrics
|
|
175
|
+
├── examples/ Browser demo and live model checks
|
|
176
|
+
├── test/ Unit and HTTP integration tests
|
|
177
|
+
├── scripts/ Browser and package checks
|
|
178
|
+
└── docs/ Detailed documentation
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## Documentation
|
|
182
|
+
|
|
183
|
+
| Guide | Description |
|
|
184
|
+
| --- | --- |
|
|
185
|
+
| [Usage](docs/USAGE.md) | API, configuration, errors and examples |
|
|
186
|
+
| [Stream contract](docs/STREAM_CONTRACT.md) | Completion, retries and event ordering |
|
|
187
|
+
| [Model compatibility](docs/MODEL_COMPATIBILITY.md) | Live recovery checks for both protocols |
|
|
188
|
+
| [Roadmap](https://github.com/Stairrax/reliable-requests/blob/main/ROADMAP.md) | Current progress and planned improvements |
|
|
189
|
+
| [Changelog](CHANGELOG.md) | Release history |
|
|
190
|
+
| [Release checklist](https://github.com/Stairrax/reliable-requests/blob/main/docs/RELEASING.md) | Package release process |
|
|
191
|
+
| [Live test record](https://github.com/Stairrax/reliable-requests/blob/main/docs/LIVE_TESTS_2026-11-29.md) | Recorded model compatibility checks |
|
|
192
|
+
|
|
193
|
+
## Contributing
|
|
194
|
+
|
|
195
|
+
Issues and pull requests are welcome. Bug reports, reproducible stream failures, tests and documentation improvements are especially useful.
|
|
196
|
+
|
|
197
|
+
Keep the runtime dependency-free, preserve explicit retry semantics, and read [CONTRIBUTING.md](https://github.com/Stairrax/reliable-requests/blob/main/CONTRIBUTING.md) before opening a PR. For vulnerability reports, see [SECURITY.md](https://github.com/Stairrax/reliable-requests/blob/main/SECURITY.md).
|
|
198
|
+
|
|
199
|
+
## Acknowledgements
|
|
200
|
+
|
|
201
|
+
Thanks to [TaKeSh13](https://github.com/GiTakeshi) for helping discuss and develop solutions for local stream completion and callback reliability in [PR #4](https://github.com/Stairrax/reliable-requests/pull/4).
|
|
202
|
+
|
|
203
|
+
## License
|
|
204
|
+
|
|
205
|
+
[MIT](LICENSE) © 2026 Stairrax.
|
|
206
|
+
|
|
207
|
+
Free to use, modify and distribute, including in commercial and closed-source applications, subject to the MIT license. Provided without warranty.
|