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/index.d.ts CHANGED
@@ -1,127 +1,138 @@
1
- export type ModelProtocol = 'openai' | 'ollama';
2
-
3
- export interface RetryContext {
4
- attempt: number;
5
- method: string;
6
- phase: 'fetch' | 'read';
7
- }
8
-
9
- export type RetryPredicate = (error: unknown, context: RetryContext) => boolean;
10
- export type FetchImplementation = (input: string | URL, init?: RequestInit) => Promise<Response>;
11
-
12
- export interface RequestOptions extends RequestInit {
13
- timeoutMs?: number;
14
- retries?: number;
15
- fetchImpl?: FetchImplementation;
16
- shouldRetryError?: RetryPredicate;
17
- }
18
-
19
- export interface ConsumingRequestOptions<T> extends RequestOptions {
20
- consume: (response: Response, signal: AbortSignal) => T | PromiseLike<T>;
21
- }
22
-
23
- export function reliableRequest<T>(input: string | URL, options: ConsumingRequestOptions<T>): Promise<T>;
24
- export function reliableRequest(input: string | URL, options?: RequestOptions & { consume?: undefined }): Promise<Response>;
25
-
26
- export type StreamEvent =
27
- | { type: 'attempt'; attempt: number }
28
- | { type: 'delta'; attempt: number; text: string }
29
- | { type: 'retry'; attempt: number; error: unknown; partial: boolean; delayMs: number }
30
- | { type: 'reset'; attempt: number }
31
- | { type: 'done'; attempt: number; finishReason?: string };
32
-
33
- export interface ChatMessage {
34
- role: string;
35
- content: string;
36
- }
37
-
38
- export interface StreamLocalChatOptions {
39
- endpoint: string | URL;
40
- protocol?: ModelProtocol;
41
- model: string;
42
- messages: ChatMessage[];
43
- timeoutMs?: number;
44
- retries?: number;
45
- signal?: AbortSignal;
46
- onEvent: (event: StreamEvent) => void;
47
- headers?: HeadersInit;
48
- fetchImpl?: FetchImplementation;
49
- shouldRetryError?: RetryPredicate;
50
- allowRemoteRetries?: boolean;
51
- maxLineChars?: number;
52
- maxEventChars?: number;
53
- }
54
-
55
- export interface StreamResult {
56
- attempts: number;
57
- finishReason?: string;
58
- }
59
-
60
- export function streamLocalChat(options: StreamLocalChatOptions): Promise<StreamResult>;
61
-
62
- export interface StreamMetricsSnapshot {
63
- readonly attempts: number;
64
- readonly retries: number;
65
- readonly resets: number;
66
- readonly deltaEvents: number;
67
- readonly receivedCharacters: number;
68
- readonly visibleCharacters: number;
69
- readonly timeToFirstDeltaMs: number | null;
70
- readonly completed: boolean;
71
- readonly elapsedMs: number;
72
- }
73
-
74
- export interface StreamMetrics {
75
- record(event: StreamEvent): void;
76
- finish(): void;
77
- snapshot(): StreamMetricsSnapshot;
78
- }
79
-
80
- export function createStreamMetrics(options?: { now?: () => number }): StreamMetrics;
81
-
82
- export class RequestTimeoutError extends Error {
83
- constructor(timeoutMs: number, message?: string);
84
- code: 'REQUEST_TIMEOUT';
85
- timeoutMs: number;
86
- }
87
-
88
- export class HttpError extends Error {
89
- constructor(response: Response, attempts: number);
90
- code: 'HTTP_ERROR';
91
- status: number;
92
- response: Response;
93
- attempts: number;
94
- }
95
-
96
- export class StreamInterruptedError extends Error {
97
- constructor(message?: string);
98
- code: 'STREAM_INTERRUPTED';
99
- }
100
-
101
- export class ModelRedirectError extends Error {
102
- constructor();
103
- code: 'MODEL_REDIRECT';
104
- }
105
-
106
- export class UnsupportedModelOutputError extends Error {
107
- constructor();
108
- code: 'UNSUPPORTED_MODEL_OUTPUT';
109
- }
110
-
111
- export class InvalidStreamEventError extends Error {
112
- constructor(protocol: ModelProtocol, field?: string);
113
- code: 'INVALID_STREAM_EVENT';
114
- field: string | undefined;
115
- }
116
-
117
- export class StreamLineTooLongError extends Error {
118
- constructor(maxLineChars: number);
119
- code: 'STREAM_LINE_TOO_LONG';
120
- maxLineChars: number;
121
- }
122
-
123
- export class StreamEventTooLongError extends Error {
124
- constructor(maxEventChars: number);
125
- code: 'STREAM_EVENT_TOO_LONG';
126
- maxEventChars: number;
127
- }
1
+ export type ModelProtocol = 'openai' | 'ollama';
2
+
3
+ export interface RetryContext {
4
+ attempt: number;
5
+ method: string;
6
+ phase: 'fetch' | 'read';
7
+ }
8
+
9
+ export type RetryPredicate = (error: unknown, context: RetryContext) => boolean;
10
+ export type FetchImplementation = (input: string | URL, init?: RequestInit) => Promise<Response>;
11
+
12
+ export interface RequestOptions extends RequestInit {
13
+ timeoutMs?: number;
14
+ retries?: number;
15
+ fetchImpl?: FetchImplementation;
16
+ shouldRetryError?: RetryPredicate;
17
+ }
18
+
19
+ export interface ConsumingRequestOptions<T> extends RequestOptions {
20
+ consume: (response: Response, signal: AbortSignal) => T | PromiseLike<T>;
21
+ }
22
+
23
+ export function reliableRequest<T>(input: string | URL, options: ConsumingRequestOptions<T>): Promise<T>;
24
+ export function reliableRequest(input: string | URL, options?: RequestOptions & { consume?: undefined }): Promise<Response>;
25
+
26
+ export type StreamEvent =
27
+ | { type: 'attempt'; attempt: number }
28
+ | { type: 'delta'; attempt: number; text: string }
29
+ | { type: 'retry'; attempt: number; error: unknown; partial: boolean; delayMs: number }
30
+ | { type: 'reset'; attempt: number }
31
+ | { type: 'done'; attempt: number; finishReason?: string };
32
+
33
+ export interface ChatMessage {
34
+ role: string;
35
+ content: string;
36
+ }
37
+
38
+ export interface StreamLocalChatOptions {
39
+ endpoint: string | URL;
40
+ protocol?: ModelProtocol;
41
+ model: string;
42
+ messages: ChatMessage[];
43
+ timeoutMs?: number;
44
+ firstTextTimeoutMs?: number;
45
+ idleTimeoutMs?: number;
46
+ retries?: number;
47
+ signal?: AbortSignal;
48
+ onEvent: (event: StreamEvent) => void;
49
+ headers?: HeadersInit;
50
+ fetchImpl?: FetchImplementation;
51
+ shouldRetryError?: RetryPredicate;
52
+ allowRemoteRetries?: boolean;
53
+ maxLineChars?: number;
54
+ maxEventChars?: number;
55
+ }
56
+
57
+ export interface StreamResult {
58
+ attempts: number;
59
+ finishReason?: string;
60
+ }
61
+
62
+ export function streamLocalChat(options: StreamLocalChatOptions): Promise<StreamResult>;
63
+
64
+ export interface StreamMetricsSnapshot {
65
+ readonly attempts: number;
66
+ readonly retries: number;
67
+ readonly resets: number;
68
+ readonly deltaEvents: number;
69
+ readonly receivedCharacters: number;
70
+ readonly visibleCharacters: number;
71
+ readonly timeToFirstDeltaMs: number | null;
72
+ readonly completed: boolean;
73
+ readonly elapsedMs: number;
74
+ }
75
+
76
+ export interface StreamMetrics {
77
+ record(event: StreamEvent): void;
78
+ finish(): void;
79
+ snapshot(): StreamMetricsSnapshot;
80
+ }
81
+
82
+ export function createStreamMetrics(options?: { now?: () => number }): StreamMetrics;
83
+
84
+ export class RequestTimeoutError extends Error {
85
+ constructor(timeoutMs: number, message?: string);
86
+ code: 'REQUEST_TIMEOUT';
87
+ timeoutMs: number;
88
+ }
89
+
90
+ export class HttpError extends Error {
91
+ constructor(response: Response, attempts: number);
92
+ code: 'HTTP_ERROR';
93
+ status: number;
94
+ response: Response;
95
+ attempts: number;
96
+ }
97
+
98
+ export type StreamTimeoutPhase = 'first-text' | 'idle';
99
+
100
+ export class StreamTimeoutError extends Error {
101
+ constructor(phase: StreamTimeoutPhase, timeoutMs: number);
102
+ code: 'STREAM_TIMEOUT';
103
+ phase: StreamTimeoutPhase;
104
+ timeoutMs: number;
105
+ }
106
+
107
+ export class StreamInterruptedError extends Error {
108
+ constructor(message?: string);
109
+ code: 'STREAM_INTERRUPTED';
110
+ }
111
+
112
+ export class ModelRedirectError extends Error {
113
+ constructor();
114
+ code: 'MODEL_REDIRECT';
115
+ }
116
+
117
+ export class UnsupportedModelOutputError extends Error {
118
+ constructor();
119
+ code: 'UNSUPPORTED_MODEL_OUTPUT';
120
+ }
121
+
122
+ export class InvalidStreamEventError extends Error {
123
+ constructor(protocol: ModelProtocol, field?: string);
124
+ code: 'INVALID_STREAM_EVENT';
125
+ field: string | undefined;
126
+ }
127
+
128
+ export class StreamLineTooLongError extends Error {
129
+ constructor(maxLineChars: number);
130
+ code: 'STREAM_LINE_TOO_LONG';
131
+ maxLineChars: number;
132
+ }
133
+
134
+ export class StreamEventTooLongError extends Error {
135
+ constructor(maxEventChars: number);
136
+ code: 'STREAM_EVENT_TOO_LONG';
137
+ maxEventChars: number;
138
+ }
package/index.js CHANGED
@@ -1,11 +1,12 @@
1
- export { reliableRequest, RequestTimeoutError, HttpError } from './src/index.js';
2
- export {
3
- streamLocalChat,
4
- StreamInterruptedError,
5
- ModelRedirectError,
6
- UnsupportedModelOutputError,
7
- InvalidStreamEventError,
8
- StreamLineTooLongError,
9
- StreamEventTooLongError,
10
- } from './src/local-chat.js';
11
- export { createStreamMetrics } from './src/metrics.js';
1
+ export { reliableRequest, RequestTimeoutError, HttpError } from './src/index.js';
2
+ export {
3
+ streamLocalChat,
4
+ StreamInterruptedError,
5
+ StreamTimeoutError,
6
+ ModelRedirectError,
7
+ UnsupportedModelOutputError,
8
+ InvalidStreamEventError,
9
+ StreamLineTooLongError,
10
+ StreamEventTooLongError,
11
+ } from './src/local-chat.js';
12
+ export { createStreamMetrics } from './src/metrics.js';
package/package.json CHANGED
@@ -1,49 +1,67 @@
1
- {
2
- "name": "reliable-requests",
3
- "version": "0.2.1",
4
- "type": "module",
5
- "engines": {
6
- "node": ">=18"
7
- },
8
- "scripts": {
9
- "test": "node --test",
10
- "test:model": "node examples/verify-local-model.mjs",
11
- "demo": "node examples/demo/server.mjs",
12
- "test:browser": "node scripts/browser-smoke.mjs",
13
- "test:ollama:recovery": "node examples/verify-ollama-recovery.mjs",
14
- "test:ollama:stress": "node examples/verify-ollama-stress.mjs",
15
- "test:package": "node scripts/verify-package.mjs",
16
- "check": "npm test && npm run test:package",
17
- "test:model:recovery": "node examples/verify-model-recovery.mjs"
18
- },
19
- "license": "MIT",
20
- "description": "Resilient local-model text streams with explicit reset on retry",
21
- "main": "./index.js",
22
- "types": "./index.d.ts",
23
- "exports": {
24
- ".": {
25
- "types": "./index.d.ts",
26
- "import": "./index.js"
27
- }
28
- },
29
- "files": [
30
- "index.js",
31
- "index.d.ts",
32
- "src/",
33
- "docs/USAGE.md",
34
- "docs/STREAM_CONTRACT.md",
35
- "docs/MODEL_COMPATIBILITY.md",
36
- "CHANGELOG.md"
37
- ],
38
- "repository": {
39
- "type": "git",
40
- "url": "git+https://github.com/Stairrax/reliable-requests.git"
41
- },
42
- "bugs": {
43
- "url": "https://github.com/Stairrax/reliable-requests/issues"
44
- },
45
- "homepage": "https://github.com/Stairrax/reliable-requests#readme",
46
- "devDependencies": {
47
- "typescript": "5.9.3"
48
- }
49
- }
1
+ {
2
+ "name": "reliable-requests",
3
+ "version": "0.3.1",
4
+ "type": "module",
5
+ "engines": {
6
+ "node": ">=18"
7
+ },
8
+ "scripts": {
9
+ "test": "node --test",
10
+ "test:model": "node examples/verify-local-model.mjs",
11
+ "demo": "node examples/demo/server.mjs",
12
+ "test:browser": "node scripts/browser-smoke.mjs",
13
+ "test:browser:live": "node scripts/browser-live.mjs",
14
+ "test:ollama:recovery": "node examples/verify-ollama-recovery.mjs",
15
+ "test:ollama:stress": "node examples/verify-ollama-stress.mjs",
16
+ "test:model:stress": "node --expose-gc examples/verify-model-stress.mjs",
17
+ "test:package": "node scripts/verify-package.mjs",
18
+ "check": "npm test && npm run test:package",
19
+ "test:model:recovery": "node examples/verify-model-recovery.mjs"
20
+ },
21
+ "license": "MIT",
22
+ "description": "Resilient local-model text streams with explicit reset on retry",
23
+ "keywords": [
24
+ "fetch",
25
+ "http",
26
+ "retry",
27
+ "timeout",
28
+ "streaming",
29
+ "sse",
30
+ "llm",
31
+ "local-ai",
32
+ "ollama",
33
+ "llama-cpp",
34
+ "lm-studio",
35
+ "fault-tolerance",
36
+ "typescript",
37
+ "zero-dependencies"
38
+ ],
39
+ "main": "./index.js",
40
+ "types": "./index.d.ts",
41
+ "exports": {
42
+ ".": {
43
+ "types": "./index.d.ts",
44
+ "import": "./index.js"
45
+ }
46
+ },
47
+ "files": [
48
+ "index.js",
49
+ "index.d.ts",
50
+ "src/",
51
+ "docs/USAGE.md",
52
+ "docs/STREAM_CONTRACT.md",
53
+ "docs/MODEL_COMPATIBILITY.md",
54
+ "CHANGELOG.md"
55
+ ],
56
+ "repository": {
57
+ "type": "git",
58
+ "url": "git+https://github.com/Stairrax/reliable-requests.git"
59
+ },
60
+ "bugs": {
61
+ "url": "https://github.com/Stairrax/reliable-requests/issues"
62
+ },
63
+ "homepage": "https://github.com/Stairrax/reliable-requests#readme",
64
+ "devDependencies": {
65
+ "typescript": "5.9.3"
66
+ }
67
+ }
package/src/endpoint.js CHANGED
@@ -1,17 +1,17 @@
1
- const HTTP_PROTOCOLS = new Set(['http:', 'https:']);
2
-
3
- export function parseEndpoint(endpoint) {
4
- const url = new URL(endpoint);
5
- if (!HTTP_PROTOCOLS.has(url.protocol)) throw new TypeError('endpoint must use HTTP or HTTPS');
6
- if (url.username || url.password) throw new TypeError('endpoint must not contain credentials');
7
- return url;
8
- }
9
-
10
- export function isLoopback(url) {
11
- if (!HTTP_PROTOCOLS.has(url.protocol)) return false;
12
- const host = url.hostname;
13
- if (host === 'localhost' || host === '[::1]') return true;
14
- const octets = host.split('.');
15
- if (octets.length !== 4 || octets[0] !== '127') return false;
16
- return octets.every(octet => /^(0|[1-9][0-9]{0,2})$/.test(octet) && Number(octet) <= 255);
17
- }
1
+ const HTTP_PROTOCOLS = new Set(['http:', 'https:']);
2
+
3
+ export function parseEndpoint(endpoint) {
4
+ const url = new URL(endpoint);
5
+ if (!HTTP_PROTOCOLS.has(url.protocol)) throw new TypeError('endpoint must use HTTP or HTTPS');
6
+ if (url.username || url.password) throw new TypeError('endpoint must not contain credentials');
7
+ return url;
8
+ }
9
+
10
+ export function isLoopback(url) {
11
+ if (!HTTP_PROTOCOLS.has(url.protocol)) return false;
12
+ const host = url.hostname;
13
+ if (host === 'localhost' || host === '[::1]') return true;
14
+ const octets = host.split('.');
15
+ if (octets.length !== 4 || octets[0] !== '127') return false;
16
+ return octets.every(octet => /^(0|[1-9][0-9]{0,2})$/.test(octet) && Number(octet) <= 255);
17
+ }