@lov3kaizen/agentsea-cache 0.5.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.
@@ -0,0 +1,206 @@
1
+ import EventEmitter from 'eventemitter3';
2
+ import { C as CacheMessage } from '../cache.types-DMuyQseO.js';
3
+ import { B as BaseCacheStore } from '../store.types-BQy5Yyz9.js';
4
+ import { S as SimilarityEngine } from '../SimilarityEngine-Cwv_mF9a.js';
5
+
6
+ type StreamChunkType = 'text' | 'tool_call' | 'tool_result' | 'metadata';
7
+ interface StreamChunk {
8
+ type: StreamChunkType;
9
+ content?: string;
10
+ toolCall?: {
11
+ id: string;
12
+ name: string;
13
+ arguments: string;
14
+ };
15
+ toolResult?: {
16
+ callId: string;
17
+ content: string;
18
+ };
19
+ metadata?: Record<string, unknown>;
20
+ timestamp: number;
21
+ index: number;
22
+ }
23
+ interface RecordedStream {
24
+ id: string;
25
+ key: string;
26
+ chunks: StreamChunk[];
27
+ model: string;
28
+ messages: Array<{
29
+ role: string;
30
+ content: string;
31
+ }>;
32
+ startTime: number;
33
+ endTime: number;
34
+ durationMs: number;
35
+ totalChars: number;
36
+ tokenUsage?: {
37
+ prompt: number;
38
+ completion: number;
39
+ total: number;
40
+ };
41
+ complete: boolean;
42
+ error?: string;
43
+ }
44
+ interface ChunkBufferConfig {
45
+ maxChunks?: number;
46
+ maxBytes?: number;
47
+ flushIntervalMs?: number;
48
+ preserveOrder?: boolean;
49
+ }
50
+ interface StreamRecorderConfig {
51
+ buffer?: ChunkBufferConfig;
52
+ captureToolCalls?: boolean;
53
+ captureMetadata?: boolean;
54
+ maxDurationMs?: number;
55
+ maxChunks?: number;
56
+ }
57
+ interface StreamReplayerConfig {
58
+ speedMultiplier?: number;
59
+ minDelayMs?: number;
60
+ maxDelayMs?: number;
61
+ simulateTiming?: boolean;
62
+ onChunk?: (chunk: StreamChunk) => void;
63
+ onComplete?: (stream: RecordedStream) => void;
64
+ onError?: (error: Error) => void;
65
+ }
66
+ interface StreamCacheConfig {
67
+ recorder?: StreamRecorderConfig;
68
+ replayer?: StreamReplayerConfig;
69
+ cacheIncomplete?: boolean;
70
+ minLengthToCache?: number;
71
+ streamTtl?: number;
72
+ }
73
+ interface StreamCacheLookupResult {
74
+ hit: boolean;
75
+ stream?: RecordedStream;
76
+ similarity?: number;
77
+ source: 'exact' | 'semantic' | 'miss';
78
+ latencyMs: number;
79
+ }
80
+ interface StreamCacheStats {
81
+ totalLookups: number;
82
+ totalHits: number;
83
+ totalMisses: number;
84
+ hitRate: number;
85
+ avgReplayLatencyMs: number;
86
+ totalStreamsCached: number;
87
+ totalBytesCached: number;
88
+ avgStreamDurationMs: number;
89
+ }
90
+
91
+ declare class ChunkBuffer {
92
+ private chunks;
93
+ private currentBytes;
94
+ private config;
95
+ private flushTimer;
96
+ private onFlush?;
97
+ constructor(config?: ChunkBufferConfig, onFlush?: (chunks: StreamChunk[]) => void);
98
+ add(chunk: StreamChunk): void;
99
+ addAll(chunks: StreamChunk[]): void;
100
+ flush(): StreamChunk[];
101
+ size(): number;
102
+ bytes(): number;
103
+ isEmpty(): boolean;
104
+ peek(): readonly StreamChunk[];
105
+ clear(): void;
106
+ stop(): void;
107
+ destroy(): void;
108
+ private shouldFlush;
109
+ private startFlushTimer;
110
+ private estimateChunkSize;
111
+ }
112
+ declare function createChunkBuffer(config?: ChunkBufferConfig, onFlush?: (chunks: StreamChunk[]) => void): ChunkBuffer;
113
+
114
+ declare class StreamRecorder {
115
+ private config;
116
+ private buffer;
117
+ private recording;
118
+ private startTime;
119
+ private model;
120
+ private messages;
121
+ private key;
122
+ private totalChars;
123
+ private chunkIndex;
124
+ private timeoutId;
125
+ constructor(config?: StreamRecorderConfig);
126
+ start(model: string, messages: Array<{
127
+ role: string;
128
+ content: string;
129
+ }>, key?: string): void;
130
+ recordText(content: string, metadata?: Record<string, unknown>): void;
131
+ recordToolCall(id: string, name: string, args: string, metadata?: Record<string, unknown>): void;
132
+ recordToolResult(callId: string, content: string, metadata?: Record<string, unknown>): void;
133
+ recordMetadata(metadata: Record<string, unknown>): void;
134
+ recordChunk(chunk: StreamChunk): void;
135
+ complete(tokenUsage?: {
136
+ prompt: number;
137
+ completion: number;
138
+ total: number;
139
+ }): RecordedStream;
140
+ abort(reason?: string): RecordedStream;
141
+ isRecording(): boolean;
142
+ getChunkCount(): number;
143
+ getDuration(): number;
144
+ destroy(): void;
145
+ private reset;
146
+ private clearTimeout;
147
+ }
148
+ declare function createStreamRecorder(config?: StreamRecorderConfig): StreamRecorder;
149
+
150
+ declare class StreamReplayer {
151
+ private config;
152
+ private abortController;
153
+ constructor(config?: StreamReplayerConfig);
154
+ replay(stream: RecordedStream): AsyncGenerator<StreamChunk>;
155
+ replayText(stream: RecordedStream): AsyncGenerator<string>;
156
+ replaySync(stream: RecordedStream): Generator<StreamChunk>;
157
+ getAllChunks(stream: RecordedStream): StreamChunk[];
158
+ getFullText(stream: RecordedStream): string;
159
+ getToolCalls(stream: RecordedStream): Array<{
160
+ id: string;
161
+ name: string;
162
+ arguments: string;
163
+ }>;
164
+ stop(): void;
165
+ configure(config: Partial<StreamReplayerConfig>): void;
166
+ private delay;
167
+ }
168
+ declare function createStreamReplayer(config?: StreamReplayerConfig): StreamReplayer;
169
+
170
+ interface StreamCacheEvents {
171
+ hit: (result: StreamCacheLookupResult) => void;
172
+ miss: (key: string) => void;
173
+ record: (stream: RecordedStream) => void;
174
+ error: (error: Error) => void;
175
+ }
176
+ declare class StreamCache extends EventEmitter<StreamCacheEvents> {
177
+ private store;
178
+ private similarity?;
179
+ private config;
180
+ private recorder;
181
+ private replayer;
182
+ private stats;
183
+ private replayLatencies;
184
+ private streamDurations;
185
+ constructor(store: BaseCacheStore, config?: StreamCacheConfig, similarity?: SimilarityEngine);
186
+ lookup(model: string, messages: CacheMessage[]): Promise<StreamCacheLookupResult>;
187
+ cache(stream: RecordedStream, embedding?: number[]): Promise<void>;
188
+ wrapStream<T extends {
189
+ content?: string;
190
+ }>(model: string, messages: CacheMessage[], streamFn: () => AsyncGenerator<T>, options?: {
191
+ embedding?: number[];
192
+ }): AsyncGenerator<T>;
193
+ replay(stream: RecordedStream): AsyncGenerator<StreamChunk>;
194
+ getStats(): StreamCacheStats;
195
+ clear(): Promise<void>;
196
+ destroy(): void;
197
+ private generateStreamKey;
198
+ private serializeStream;
199
+ private deserializeStream;
200
+ private estimateStreamSize;
201
+ private updateHitRate;
202
+ private updateAvgStreamDuration;
203
+ }
204
+ declare function createStreamCache(store: BaseCacheStore, config?: StreamCacheConfig, similarity?: SimilarityEngine): StreamCache;
205
+
206
+ export { ChunkBuffer, type ChunkBufferConfig, type RecordedStream, StreamCache, type StreamCacheConfig, type StreamCacheEvents, type StreamCacheLookupResult, type StreamCacheStats, type StreamChunk, type StreamChunkType, StreamRecorder, type StreamRecorderConfig, StreamReplayer, type StreamReplayerConfig, createChunkBuffer, createStreamCache, createStreamRecorder, createStreamReplayer };