@luckystack/sync 0.1.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.
@@ -0,0 +1,88 @@
1
+ import { syncMessage } from '@luckystack/core';
2
+ import { Socket } from 'socket.io';
3
+
4
+ declare function handleSyncRequest({ msg, socket, token }: {
5
+ msg: syncMessage;
6
+ socket: Socket;
7
+ token: string | null;
8
+ }): Promise<boolean | undefined>;
9
+
10
+ interface HttpSyncRequestParams {
11
+ name: string;
12
+ cb?: string;
13
+ data: Record<string, unknown>;
14
+ receiver: string;
15
+ ignoreSelf?: boolean;
16
+ token: string | null;
17
+ requesterIp?: string;
18
+ xLanguageHeader?: string | string[];
19
+ acceptLanguageHeader?: string | string[];
20
+ stream?: (payload: HttpSyncStreamEvent) => void;
21
+ /**
22
+ * Optional AbortSignal. The HTTP server (`@luckystack/server`) wires this
23
+ * to `req.on('close', ...)` so a closed SSE connection aborts in-flight
24
+ * stream emits. Sync handlers receive it as `abortSignal` in params.
25
+ */
26
+ abortSignal?: AbortSignal;
27
+ }
28
+ interface HttpSyncResponse {
29
+ status: 'success' | 'error';
30
+ message: string;
31
+ errorCode?: string;
32
+ errorParams?: {
33
+ key: string;
34
+ value: string | number | boolean;
35
+ }[];
36
+ httpStatus?: number;
37
+ }
38
+ type SyncStreamPayload = Record<string, unknown>;
39
+ type HttpSyncStreamEvent = SyncStreamPayload;
40
+ declare function handleHttpSyncRequest({ name, cb, data, receiver, ignoreSelf, token, requesterIp, xLanguageHeader, acceptLanguageHeader, stream, abortSignal, }: HttpSyncRequestParams): Promise<HttpSyncResponse>;
41
+
42
+ interface CreateStreamThrottleOptions {
43
+ /**
44
+ * Flush buffered text once it crosses this many characters. Default: 32.
45
+ * Lower = more updates, more network traffic.
46
+ * Higher = fewer updates, choppier UI.
47
+ */
48
+ flushAtChars?: number;
49
+ /**
50
+ * Flush buffered text after this many milliseconds even if the char
51
+ * threshold hasn't been hit. Default: 50ms — fast enough that the user
52
+ * perceives "live typing", slow enough that 50–100 tokens batch into a
53
+ * single message on a fast LLM stream.
54
+ *
55
+ * Set to `false` to disable the timer (only flush at char threshold or
56
+ * on explicit `flush()`).
57
+ */
58
+ flushEveryMs?: number | false;
59
+ /**
60
+ * Field name on the emitted payload that carries the buffered text.
61
+ * Default: `'chunk'`. Override if your stream payload uses a different
62
+ * key (e.g. `'text'`, `'delta'`).
63
+ */
64
+ field?: string;
65
+ }
66
+ interface StreamThrottle {
67
+ /** Append text to the buffer. May trigger a flush. */
68
+ push: (text: string, emit: (payload: Record<string, unknown>) => void) => void;
69
+ /** Force-flush whatever is in the buffer right now. Call after the source loop ends. */
70
+ flush: (emit: (payload: Record<string, unknown>) => void) => void;
71
+ /** Drop the buffered text without emitting. Useful on abort. */
72
+ reset: () => void;
73
+ }
74
+ /**
75
+ * Build a chunk throttle for streaming use cases. Designed for LLM token
76
+ * streams where the provider yields very small pieces (3–10 chars) and you
77
+ * don't want to send a separate socket message for each one.
78
+ *
79
+ * @example
80
+ * const throttle = createStreamThrottle({ flushEveryMs: 50, flushAtChars: 32 });
81
+ * for await (const chunk of openaiStream) {
82
+ * throttle.push(chunk.text, broadcastStream);
83
+ * }
84
+ * throttle.flush(broadcastStream);
85
+ */
86
+ declare const createStreamThrottle: (options?: CreateStreamThrottleOptions) => StreamThrottle;
87
+
88
+ export { type CreateStreamThrottleOptions, type HttpSyncStreamEvent, type StreamThrottle, createStreamThrottle, handleHttpSyncRequest, handleSyncRequest };