@agentuity/runtime 0.1.13 → 0.1.14
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/package.json +8 -8
- package/src/_config.ts +13 -0
- package/src/_standalone.ts +167 -19
- package/src/index.ts +1 -0
- package/dist/_config.d.ts +0 -90
- package/dist/_config.d.ts.map +0 -1
- package/dist/_config.js +0 -135
- package/dist/_config.js.map +0 -1
- package/dist/_context.d.ts +0 -76
- package/dist/_context.d.ts.map +0 -1
- package/dist/_context.js +0 -147
- package/dist/_context.js.map +0 -1
- package/dist/_events.d.ts +0 -64
- package/dist/_events.d.ts.map +0 -1
- package/dist/_events.js +0 -92
- package/dist/_events.js.map +0 -1
- package/dist/_idle.d.ts +0 -7
- package/dist/_idle.d.ts.map +0 -1
- package/dist/_idle.js +0 -10
- package/dist/_idle.js.map +0 -1
- package/dist/_metadata.d.ts +0 -117
- package/dist/_metadata.d.ts.map +0 -1
- package/dist/_metadata.js +0 -246
- package/dist/_metadata.js.map +0 -1
- package/dist/_process-protection.d.ts +0 -25
- package/dist/_process-protection.d.ts.map +0 -1
- package/dist/_process-protection.js +0 -65
- package/dist/_process-protection.js.map +0 -1
- package/dist/_server.d.ts +0 -46
- package/dist/_server.d.ts.map +0 -1
- package/dist/_server.js +0 -85
- package/dist/_server.js.map +0 -1
- package/dist/_services.d.ts +0 -21
- package/dist/_services.d.ts.map +0 -1
- package/dist/_services.js +0 -248
- package/dist/_services.js.map +0 -1
- package/dist/_standalone.d.ts +0 -168
- package/dist/_standalone.d.ts.map +0 -1
- package/dist/_standalone.js +0 -441
- package/dist/_standalone.js.map +0 -1
- package/dist/_tokens.d.ts +0 -12
- package/dist/_tokens.d.ts.map +0 -1
- package/dist/_tokens.js +0 -96
- package/dist/_tokens.js.map +0 -1
- package/dist/_util.d.ts +0 -16
- package/dist/_util.d.ts.map +0 -1
- package/dist/_util.js +0 -54
- package/dist/_util.js.map +0 -1
- package/dist/_validation.d.ts +0 -89
- package/dist/_validation.d.ts.map +0 -1
- package/dist/_validation.js +0 -29
- package/dist/_validation.js.map +0 -1
- package/dist/_waituntil.d.ts +0 -18
- package/dist/_waituntil.d.ts.map +0 -1
- package/dist/_waituntil.js +0 -97
- package/dist/_waituntil.js.map +0 -1
- package/dist/agent.d.ts +0 -1210
- package/dist/agent.d.ts.map +0 -1
- package/dist/agent.js +0 -903
- package/dist/agent.js.map +0 -1
- package/dist/app.d.ts +0 -322
- package/dist/app.d.ts.map +0 -1
- package/dist/app.js +0 -160
- package/dist/app.js.map +0 -1
- package/dist/bun-s3-patch.d.ts +0 -37
- package/dist/bun-s3-patch.d.ts.map +0 -1
- package/dist/bun-s3-patch.js +0 -139
- package/dist/bun-s3-patch.js.map +0 -1
- package/dist/cors.d.ts +0 -42
- package/dist/cors.d.ts.map +0 -1
- package/dist/cors.js +0 -117
- package/dist/cors.js.map +0 -1
- package/dist/devmode.d.ts +0 -3
- package/dist/devmode.d.ts.map +0 -1
- package/dist/devmode.js +0 -167
- package/dist/devmode.js.map +0 -1
- package/dist/eval.d.ts +0 -91
- package/dist/eval.d.ts.map +0 -1
- package/dist/eval.js +0 -16
- package/dist/eval.js.map +0 -1
- package/dist/handlers/cron.d.ts +0 -47
- package/dist/handlers/cron.d.ts.map +0 -1
- package/dist/handlers/cron.js +0 -49
- package/dist/handlers/cron.js.map +0 -1
- package/dist/handlers/index.d.ts +0 -5
- package/dist/handlers/index.d.ts.map +0 -1
- package/dist/handlers/index.js +0 -5
- package/dist/handlers/index.js.map +0 -1
- package/dist/handlers/sse.d.ts +0 -91
- package/dist/handlers/sse.d.ts.map +0 -1
- package/dist/handlers/sse.js +0 -213
- package/dist/handlers/sse.js.map +0 -1
- package/dist/handlers/stream.d.ts +0 -52
- package/dist/handlers/stream.d.ts.map +0 -1
- package/dist/handlers/stream.js +0 -116
- package/dist/handlers/stream.js.map +0 -1
- package/dist/handlers/websocket.d.ts +0 -49
- package/dist/handlers/websocket.d.ts.map +0 -1
- package/dist/handlers/websocket.js +0 -143
- package/dist/handlers/websocket.js.map +0 -1
- package/dist/index.d.ts +0 -71
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -58
- package/dist/index.js.map +0 -1
- package/dist/logger/console.d.ts +0 -70
- package/dist/logger/console.d.ts.map +0 -1
- package/dist/logger/console.js +0 -274
- package/dist/logger/console.js.map +0 -1
- package/dist/logger/index.d.ts +0 -3
- package/dist/logger/index.d.ts.map +0 -1
- package/dist/logger/index.js +0 -3
- package/dist/logger/index.js.map +0 -1
- package/dist/logger/internal.d.ts +0 -79
- package/dist/logger/internal.d.ts.map +0 -1
- package/dist/logger/internal.js +0 -133
- package/dist/logger/internal.js.map +0 -1
- package/dist/logger/logger.d.ts +0 -41
- package/dist/logger/logger.d.ts.map +0 -1
- package/dist/logger/logger.js +0 -2
- package/dist/logger/logger.js.map +0 -1
- package/dist/logger/user.d.ts +0 -8
- package/dist/logger/user.d.ts.map +0 -1
- package/dist/logger/user.js +0 -7
- package/dist/logger/user.js.map +0 -1
- package/dist/logger/util.d.ts +0 -11
- package/dist/logger/util.d.ts.map +0 -1
- package/dist/logger/util.js +0 -77
- package/dist/logger/util.js.map +0 -1
- package/dist/middleware.d.ts +0 -112
- package/dist/middleware.d.ts.map +0 -1
- package/dist/middleware.js +0 -507
- package/dist/middleware.js.map +0 -1
- package/dist/otel/config.d.ts +0 -19
- package/dist/otel/config.d.ts.map +0 -1
- package/dist/otel/config.js +0 -26
- package/dist/otel/config.js.map +0 -1
- package/dist/otel/console.d.ts +0 -33
- package/dist/otel/console.d.ts.map +0 -1
- package/dist/otel/console.js +0 -86
- package/dist/otel/console.js.map +0 -1
- package/dist/otel/exporters/index.d.ts +0 -4
- package/dist/otel/exporters/index.d.ts.map +0 -1
- package/dist/otel/exporters/index.js +0 -4
- package/dist/otel/exporters/index.js.map +0 -1
- package/dist/otel/exporters/jsonl-log-exporter.d.ts +0 -36
- package/dist/otel/exporters/jsonl-log-exporter.d.ts.map +0 -1
- package/dist/otel/exporters/jsonl-log-exporter.js +0 -103
- package/dist/otel/exporters/jsonl-log-exporter.js.map +0 -1
- package/dist/otel/exporters/jsonl-metric-exporter.d.ts +0 -40
- package/dist/otel/exporters/jsonl-metric-exporter.d.ts.map +0 -1
- package/dist/otel/exporters/jsonl-metric-exporter.js +0 -104
- package/dist/otel/exporters/jsonl-metric-exporter.js.map +0 -1
- package/dist/otel/exporters/jsonl-trace-exporter.d.ts +0 -36
- package/dist/otel/exporters/jsonl-trace-exporter.d.ts.map +0 -1
- package/dist/otel/exporters/jsonl-trace-exporter.js +0 -111
- package/dist/otel/exporters/jsonl-trace-exporter.js.map +0 -1
- package/dist/otel/fetch.d.ts +0 -12
- package/dist/otel/fetch.d.ts.map +0 -1
- package/dist/otel/fetch.js +0 -82
- package/dist/otel/fetch.js.map +0 -1
- package/dist/otel/http.d.ts +0 -16
- package/dist/otel/http.d.ts.map +0 -1
- package/dist/otel/http.js +0 -44
- package/dist/otel/http.js.map +0 -1
- package/dist/otel/logger.d.ts +0 -37
- package/dist/otel/logger.d.ts.map +0 -1
- package/dist/otel/logger.js +0 -268
- package/dist/otel/logger.js.map +0 -1
- package/dist/otel/otel.d.ts +0 -65
- package/dist/otel/otel.d.ts.map +0 -1
- package/dist/otel/otel.js +0 -261
- package/dist/otel/otel.js.map +0 -1
- package/dist/router.d.ts +0 -100
- package/dist/router.d.ts.map +0 -1
- package/dist/router.js +0 -163
- package/dist/router.js.map +0 -1
- package/dist/services/evalrun/composite.d.ts +0 -21
- package/dist/services/evalrun/composite.d.ts.map +0 -1
- package/dist/services/evalrun/composite.js +0 -26
- package/dist/services/evalrun/composite.js.map +0 -1
- package/dist/services/evalrun/http.d.ts +0 -24
- package/dist/services/evalrun/http.d.ts.map +0 -1
- package/dist/services/evalrun/http.js +0 -86
- package/dist/services/evalrun/http.js.map +0 -1
- package/dist/services/evalrun/index.d.ts +0 -5
- package/dist/services/evalrun/index.d.ts.map +0 -1
- package/dist/services/evalrun/index.js +0 -5
- package/dist/services/evalrun/index.js.map +0 -1
- package/dist/services/evalrun/json.d.ts +0 -21
- package/dist/services/evalrun/json.d.ts.map +0 -1
- package/dist/services/evalrun/json.js +0 -38
- package/dist/services/evalrun/json.js.map +0 -1
- package/dist/services/evalrun/local.d.ts +0 -19
- package/dist/services/evalrun/local.d.ts.map +0 -1
- package/dist/services/evalrun/local.js +0 -22
- package/dist/services/evalrun/local.js.map +0 -1
- package/dist/services/local/_db.d.ts +0 -4
- package/dist/services/local/_db.d.ts.map +0 -1
- package/dist/services/local/_db.js +0 -123
- package/dist/services/local/_db.js.map +0 -1
- package/dist/services/local/_router.d.ts +0 -3
- package/dist/services/local/_router.d.ts.map +0 -1
- package/dist/services/local/_router.js +0 -28
- package/dist/services/local/_router.js.map +0 -1
- package/dist/services/local/_util.d.ts +0 -18
- package/dist/services/local/_util.d.ts.map +0 -1
- package/dist/services/local/_util.js +0 -44
- package/dist/services/local/_util.js.map +0 -1
- package/dist/services/local/index.d.ts +0 -7
- package/dist/services/local/index.d.ts.map +0 -1
- package/dist/services/local/index.js +0 -7
- package/dist/services/local/index.js.map +0 -1
- package/dist/services/local/keyvalue.d.ts +0 -17
- package/dist/services/local/keyvalue.d.ts.map +0 -1
- package/dist/services/local/keyvalue.js +0 -125
- package/dist/services/local/keyvalue.js.map +0 -1
- package/dist/services/local/stream.d.ts +0 -12
- package/dist/services/local/stream.d.ts.map +0 -1
- package/dist/services/local/stream.js +0 -262
- package/dist/services/local/stream.js.map +0 -1
- package/dist/services/local/vector.d.ts +0 -17
- package/dist/services/local/vector.d.ts.map +0 -1
- package/dist/services/local/vector.js +0 -303
- package/dist/services/local/vector.js.map +0 -1
- package/dist/services/sandbox/http.d.ts +0 -13
- package/dist/services/sandbox/http.d.ts.map +0 -1
- package/dist/services/sandbox/http.js +0 -130
- package/dist/services/sandbox/http.js.map +0 -1
- package/dist/services/sandbox/index.d.ts +0 -2
- package/dist/services/sandbox/index.d.ts.map +0 -1
- package/dist/services/sandbox/index.js +0 -2
- package/dist/services/sandbox/index.js.map +0 -1
- package/dist/services/session/composite.d.ts +0 -21
- package/dist/services/session/composite.d.ts.map +0 -1
- package/dist/services/session/composite.js +0 -26
- package/dist/services/session/composite.js.map +0 -1
- package/dist/services/session/http.d.ts +0 -34
- package/dist/services/session/http.d.ts.map +0 -1
- package/dist/services/session/http.js +0 -80
- package/dist/services/session/http.js.map +0 -1
- package/dist/services/session/index.d.ts +0 -5
- package/dist/services/session/index.d.ts.map +0 -1
- package/dist/services/session/index.js +0 -5
- package/dist/services/session/index.js.map +0 -1
- package/dist/services/session/json.d.ts +0 -22
- package/dist/services/session/json.d.ts.map +0 -1
- package/dist/services/session/json.js +0 -35
- package/dist/services/session/json.js.map +0 -1
- package/dist/services/session/local.d.ts +0 -19
- package/dist/services/session/local.d.ts.map +0 -1
- package/dist/services/session/local.js +0 -23
- package/dist/services/session/local.js.map +0 -1
- package/dist/services/thread/local.d.ts +0 -20
- package/dist/services/thread/local.d.ts.map +0 -1
- package/dist/services/thread/local.js +0 -158
- package/dist/services/thread/local.js.map +0 -1
- package/dist/session.d.ts +0 -734
- package/dist/session.d.ts.map +0 -1
- package/dist/session.js +0 -1139
- package/dist/session.js.map +0 -1
- package/dist/validator.d.ts +0 -142
- package/dist/validator.d.ts.map +0 -1
- package/dist/validator.js +0 -149
- package/dist/validator.js.map +0 -1
- package/dist/web.d.ts +0 -8
- package/dist/web.d.ts.map +0 -1
- package/dist/web.js +0 -66
- package/dist/web.js.map +0 -1
- package/dist/workbench.d.ts +0 -17
- package/dist/workbench.d.ts.map +0 -1
- package/dist/workbench.js +0 -507
- package/dist/workbench.js.map +0 -1
package/dist/session.d.ts
DELETED
|
@@ -1,734 +0,0 @@
|
|
|
1
|
-
/** biome-ignore-all lint/suspicious/noExplicitAny: anys are great */
|
|
2
|
-
import type { Context } from 'hono';
|
|
3
|
-
import { type Env } from './app';
|
|
4
|
-
import type { AppState } from './index';
|
|
5
|
-
/**
|
|
6
|
-
* Result of parsing serialized thread data.
|
|
7
|
-
* @internal
|
|
8
|
-
*/
|
|
9
|
-
export interface ParsedThreadData {
|
|
10
|
-
flatStateJson?: string;
|
|
11
|
-
metadata?: Record<string, unknown>;
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* Parse serialized thread data, handling both old (flat state) and new ({ state, metadata }) formats.
|
|
15
|
-
* @internal
|
|
16
|
-
*/
|
|
17
|
-
export declare function parseThreadData(raw: string | undefined): ParsedThreadData;
|
|
18
|
-
export type ThreadEventName = 'destroyed';
|
|
19
|
-
export type SessionEventName = 'completed';
|
|
20
|
-
/**
|
|
21
|
-
* Represents a merge operation for thread state.
|
|
22
|
-
* Used when state is modified without being loaded first.
|
|
23
|
-
*/
|
|
24
|
-
export interface MergeOperation {
|
|
25
|
-
op: 'set' | 'delete' | 'clear' | 'push';
|
|
26
|
-
key?: string;
|
|
27
|
-
value?: unknown;
|
|
28
|
-
maxRecords?: number;
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Async thread state storage with lazy loading.
|
|
32
|
-
*
|
|
33
|
-
* State is only fetched from storage when first accessed via a read operation.
|
|
34
|
-
* Write operations can be batched and sent as a merge command without loading.
|
|
35
|
-
*
|
|
36
|
-
* @example
|
|
37
|
-
* ```typescript
|
|
38
|
-
* // Read triggers lazy load
|
|
39
|
-
* const count = await ctx.thread.state.get<number>('messageCount');
|
|
40
|
-
*
|
|
41
|
-
* // Write queues operation (may not trigger load)
|
|
42
|
-
* await ctx.thread.state.set('messageCount', (count ?? 0) + 1);
|
|
43
|
-
*
|
|
44
|
-
* // Check state status
|
|
45
|
-
* if (ctx.thread.state.dirty) {
|
|
46
|
-
* console.log('State has pending changes');
|
|
47
|
-
* }
|
|
48
|
-
* ```
|
|
49
|
-
*/
|
|
50
|
-
export interface ThreadState {
|
|
51
|
-
/**
|
|
52
|
-
* Whether state has been loaded from storage.
|
|
53
|
-
* True when state has been fetched via a read operation.
|
|
54
|
-
*/
|
|
55
|
-
readonly loaded: boolean;
|
|
56
|
-
/**
|
|
57
|
-
* Whether state has pending changes.
|
|
58
|
-
* True when there are queued writes (pending-writes state) or
|
|
59
|
-
* modifications after loading (loaded state with changes).
|
|
60
|
-
*/
|
|
61
|
-
readonly dirty: boolean;
|
|
62
|
-
/**
|
|
63
|
-
* Get a value from thread state.
|
|
64
|
-
* Triggers lazy load if state hasn't been fetched yet.
|
|
65
|
-
*/
|
|
66
|
-
get<T = unknown>(key: string): Promise<T | undefined>;
|
|
67
|
-
/**
|
|
68
|
-
* Set a value in thread state.
|
|
69
|
-
* If state hasn't been loaded, queues the operation for merge.
|
|
70
|
-
*/
|
|
71
|
-
set<T = unknown>(key: string, value: T): Promise<void>;
|
|
72
|
-
/**
|
|
73
|
-
* Check if a key exists in thread state.
|
|
74
|
-
* Triggers lazy load if state hasn't been fetched yet.
|
|
75
|
-
*/
|
|
76
|
-
has(key: string): Promise<boolean>;
|
|
77
|
-
/**
|
|
78
|
-
* Delete a key from thread state.
|
|
79
|
-
* If state hasn't been loaded, queues the operation for merge.
|
|
80
|
-
*/
|
|
81
|
-
delete(key: string): Promise<void>;
|
|
82
|
-
/**
|
|
83
|
-
* Clear all thread state.
|
|
84
|
-
* If state hasn't been loaded, queues a clear operation for merge.
|
|
85
|
-
*/
|
|
86
|
-
clear(): Promise<void>;
|
|
87
|
-
/**
|
|
88
|
-
* Get all entries as key-value pairs.
|
|
89
|
-
* Triggers lazy load if state hasn't been fetched yet.
|
|
90
|
-
*/
|
|
91
|
-
entries<T = unknown>(): Promise<[string, T][]>;
|
|
92
|
-
/**
|
|
93
|
-
* Get all keys.
|
|
94
|
-
* Triggers lazy load if state hasn't been fetched yet.
|
|
95
|
-
*/
|
|
96
|
-
keys(): Promise<string[]>;
|
|
97
|
-
/**
|
|
98
|
-
* Get all values.
|
|
99
|
-
* Triggers lazy load if state hasn't been fetched yet.
|
|
100
|
-
*/
|
|
101
|
-
values<T = unknown>(): Promise<T[]>;
|
|
102
|
-
/**
|
|
103
|
-
* Get the number of entries in state.
|
|
104
|
-
* Triggers lazy load if state hasn't been fetched yet.
|
|
105
|
-
*/
|
|
106
|
-
size(): Promise<number>;
|
|
107
|
-
/**
|
|
108
|
-
* Push a value to an array in thread state.
|
|
109
|
-
* If the key doesn't exist, creates a new array with the value.
|
|
110
|
-
* If state hasn't been loaded, queues the operation for efficient merge.
|
|
111
|
-
*
|
|
112
|
-
* @param key - The key of the array to push to
|
|
113
|
-
* @param value - The value to push
|
|
114
|
-
* @param maxRecords - Optional maximum number of records to keep (sliding window)
|
|
115
|
-
*
|
|
116
|
-
* @example
|
|
117
|
-
* ```typescript
|
|
118
|
-
* // Efficiently append messages without loading entire array
|
|
119
|
-
* await ctx.thread.state.push('messages', { role: 'user', content: 'Hello' });
|
|
120
|
-
* await ctx.thread.state.push('messages', { role: 'assistant', content: 'Hi!' });
|
|
121
|
-
*
|
|
122
|
-
* // Keep only the last 100 messages
|
|
123
|
-
* await ctx.thread.state.push('messages', newMessage, 100);
|
|
124
|
-
* ```
|
|
125
|
-
*/
|
|
126
|
-
push<T = unknown>(key: string, value: T, maxRecords?: number): Promise<void>;
|
|
127
|
-
}
|
|
128
|
-
type ThreadEventCallback<T extends Thread> = (eventName: 'destroyed', thread: T) => Promise<void> | void;
|
|
129
|
-
type SessionEventCallback<T extends Session> = (eventName: 'completed', session: T) => Promise<void> | void;
|
|
130
|
-
/**
|
|
131
|
-
* Represents a conversation thread that persists across multiple sessions.
|
|
132
|
-
* Threads maintain state and can contain multiple request-response sessions.
|
|
133
|
-
*
|
|
134
|
-
* Threads are automatically managed by the runtime and stored in cookies.
|
|
135
|
-
* They expire after 1 hour of inactivity by default.
|
|
136
|
-
*
|
|
137
|
-
* @example
|
|
138
|
-
* ```typescript
|
|
139
|
-
* // Access thread in agent handler
|
|
140
|
-
* const agent = createAgent('conversation', {
|
|
141
|
-
* handler: async (ctx, input) => {
|
|
142
|
-
* // Get thread ID
|
|
143
|
-
* ctx.logger.info('Thread: %s', ctx.thread.id);
|
|
144
|
-
*
|
|
145
|
-
* // Store data in thread state (persists across sessions)
|
|
146
|
-
* const count = await ctx.thread.state.get<number>('conversationCount') ?? 0;
|
|
147
|
-
* await ctx.thread.state.set('conversationCount', count + 1);
|
|
148
|
-
*
|
|
149
|
-
* // Access metadata
|
|
150
|
-
* const meta = await ctx.thread.getMetadata();
|
|
151
|
-
* await ctx.thread.setMetadata({ ...meta, lastAccess: Date.now() });
|
|
152
|
-
*
|
|
153
|
-
* // Listen for thread destruction
|
|
154
|
-
* ctx.thread.addEventListener('destroyed', (eventName, thread) => {
|
|
155
|
-
* ctx.logger.info('Thread destroyed: %s', thread.id);
|
|
156
|
-
* });
|
|
157
|
-
*
|
|
158
|
-
* return 'Response';
|
|
159
|
-
* }
|
|
160
|
-
* });
|
|
161
|
-
* ```
|
|
162
|
-
*/
|
|
163
|
-
export interface Thread {
|
|
164
|
-
/**
|
|
165
|
-
* Unique thread identifier (e.g., "thrd_a1b2c3d4...").
|
|
166
|
-
* Stored in cookie and persists across requests.
|
|
167
|
-
*/
|
|
168
|
-
id: string;
|
|
169
|
-
/**
|
|
170
|
-
* Thread-scoped state storage with async lazy-loading.
|
|
171
|
-
* State is only fetched from storage when first accessed via a read operation.
|
|
172
|
-
*
|
|
173
|
-
* @example
|
|
174
|
-
* ```typescript
|
|
175
|
-
* // Read triggers lazy load
|
|
176
|
-
* const count = await ctx.thread.state.get<number>('messageCount');
|
|
177
|
-
* // Write may queue operation without loading
|
|
178
|
-
* await ctx.thread.state.set('messageCount', (count ?? 0) + 1);
|
|
179
|
-
* ```
|
|
180
|
-
*/
|
|
181
|
-
state: ThreadState;
|
|
182
|
-
/**
|
|
183
|
-
* Get thread metadata (lazy-loaded).
|
|
184
|
-
* Unlike state, metadata is stored unencrypted for efficient filtering.
|
|
185
|
-
*
|
|
186
|
-
* @example
|
|
187
|
-
* ```typescript
|
|
188
|
-
* const meta = await ctx.thread.getMetadata();
|
|
189
|
-
* console.log(meta.userId);
|
|
190
|
-
* ```
|
|
191
|
-
*/
|
|
192
|
-
getMetadata(): Promise<Record<string, unknown>>;
|
|
193
|
-
/**
|
|
194
|
-
* Set thread metadata (full replace).
|
|
195
|
-
*
|
|
196
|
-
* @example
|
|
197
|
-
* ```typescript
|
|
198
|
-
* await ctx.thread.setMetadata({ userId: 'user123', department: 'sales' });
|
|
199
|
-
* ```
|
|
200
|
-
*/
|
|
201
|
-
setMetadata(metadata: Record<string, unknown>): Promise<void>;
|
|
202
|
-
/**
|
|
203
|
-
* Register an event listener for when the thread is destroyed.
|
|
204
|
-
* Thread is destroyed when it expires or is manually destroyed.
|
|
205
|
-
*
|
|
206
|
-
* @param eventName - Must be 'destroyed'
|
|
207
|
-
* @param callback - Function called when thread is destroyed
|
|
208
|
-
*
|
|
209
|
-
* @example
|
|
210
|
-
* ```typescript
|
|
211
|
-
* ctx.thread.addEventListener('destroyed', (eventName, thread) => {
|
|
212
|
-
* ctx.logger.info('Cleaning up thread: %s', thread.id);
|
|
213
|
-
* });
|
|
214
|
-
* ```
|
|
215
|
-
*/
|
|
216
|
-
addEventListener(eventName: 'destroyed', callback: (eventName: 'destroyed', thread: Thread) => Promise<void> | void): void;
|
|
217
|
-
/**
|
|
218
|
-
* Remove a previously registered 'destroyed' event listener.
|
|
219
|
-
*
|
|
220
|
-
* @param eventName - Must be 'destroyed'
|
|
221
|
-
* @param callback - The callback function to remove
|
|
222
|
-
*/
|
|
223
|
-
removeEventListener(eventName: 'destroyed', callback: (eventName: 'destroyed', thread: Thread) => Promise<void> | void): void;
|
|
224
|
-
/**
|
|
225
|
-
* Manually destroy the thread and clean up resources.
|
|
226
|
-
* Fires the 'destroyed' event and removes thread from storage.
|
|
227
|
-
*
|
|
228
|
-
* @example
|
|
229
|
-
* ```typescript
|
|
230
|
-
* // Permanently delete the thread from storage
|
|
231
|
-
* await ctx.thread.destroy();
|
|
232
|
-
* ```
|
|
233
|
-
*/
|
|
234
|
-
destroy(): Promise<void>;
|
|
235
|
-
/**
|
|
236
|
-
* Check if the thread has any data.
|
|
237
|
-
* Returns true if thread state is empty (no data to save).
|
|
238
|
-
* This is async because it may need to check lazy-loaded state.
|
|
239
|
-
*
|
|
240
|
-
* @example
|
|
241
|
-
* ```typescript
|
|
242
|
-
* if (await ctx.thread.empty()) {
|
|
243
|
-
* // Thread has no data, won't be persisted
|
|
244
|
-
* }
|
|
245
|
-
* ```
|
|
246
|
-
*/
|
|
247
|
-
empty(): Promise<boolean>;
|
|
248
|
-
}
|
|
249
|
-
/**
|
|
250
|
-
* Represents a single request-response session within a thread.
|
|
251
|
-
* Sessions are scoped to a single agent execution and its sub-agent calls.
|
|
252
|
-
*
|
|
253
|
-
* Each HTTP request creates a new session with a unique ID, but shares the same thread.
|
|
254
|
-
*
|
|
255
|
-
* @example
|
|
256
|
-
* ```typescript
|
|
257
|
-
* const agent = createAgent('request-handler', {
|
|
258
|
-
* handler: async (ctx, input) => {
|
|
259
|
-
* // Get session ID (unique per request)
|
|
260
|
-
* ctx.logger.info('Session: %s', ctx.session.id);
|
|
261
|
-
*
|
|
262
|
-
* // Store data in session state (only for this request)
|
|
263
|
-
* ctx.session.state.set('startTime', Date.now());
|
|
264
|
-
*
|
|
265
|
-
* // Access parent thread
|
|
266
|
-
* ctx.logger.info('Thread: %s', ctx.session.thread.id);
|
|
267
|
-
*
|
|
268
|
-
* // Listen for session completion
|
|
269
|
-
* ctx.session.addEventListener('completed', (eventName, session) => {
|
|
270
|
-
* const duration = Date.now() - (session.state.get('startTime') as number);
|
|
271
|
-
* ctx.logger.info('Session completed in %dms', duration);
|
|
272
|
-
* });
|
|
273
|
-
*
|
|
274
|
-
* return 'Response';
|
|
275
|
-
* }
|
|
276
|
-
* });
|
|
277
|
-
* ```
|
|
278
|
-
*/
|
|
279
|
-
export interface Session {
|
|
280
|
-
/**
|
|
281
|
-
* Unique session identifier for this request.
|
|
282
|
-
* Changes with each HTTP request, even within the same thread.
|
|
283
|
-
*/
|
|
284
|
-
id: string;
|
|
285
|
-
/**
|
|
286
|
-
* The parent thread this session belongs to.
|
|
287
|
-
* Multiple sessions can share the same thread.
|
|
288
|
-
*/
|
|
289
|
-
thread: Thread;
|
|
290
|
-
/**
|
|
291
|
-
* Session-scoped state storage that only exists for this request.
|
|
292
|
-
* Use this for temporary data that shouldn't persist across requests.
|
|
293
|
-
*
|
|
294
|
-
* @example
|
|
295
|
-
* ```typescript
|
|
296
|
-
* ctx.session.state.set('requestStartTime', Date.now());
|
|
297
|
-
* ```
|
|
298
|
-
*/
|
|
299
|
-
state: Map<string, unknown>;
|
|
300
|
-
/**
|
|
301
|
-
* Unencrypted metadata for filtering and querying sessions.
|
|
302
|
-
* Unlike state, metadata is stored as-is in the database with GIN indexes
|
|
303
|
-
* for efficient filtering. Initialized to empty object, only persisted if non-empty.
|
|
304
|
-
*
|
|
305
|
-
* @example
|
|
306
|
-
* ```typescript
|
|
307
|
-
* ctx.session.metadata.userId = 'user123';
|
|
308
|
-
* ctx.session.metadata.requestType = 'chat';
|
|
309
|
-
* ```
|
|
310
|
-
*/
|
|
311
|
-
metadata: Record<string, unknown>;
|
|
312
|
-
/**
|
|
313
|
-
* Register an event listener for when the session completes.
|
|
314
|
-
* Fired after the agent handler returns and response is sent.
|
|
315
|
-
*
|
|
316
|
-
* @param eventName - Must be 'completed'
|
|
317
|
-
* @param callback - Function called when session completes
|
|
318
|
-
*
|
|
319
|
-
* @example
|
|
320
|
-
* ```typescript
|
|
321
|
-
* ctx.session.addEventListener('completed', (eventName, session) => {
|
|
322
|
-
* ctx.logger.info('Session finished: %s', session.id);
|
|
323
|
-
* });
|
|
324
|
-
* ```
|
|
325
|
-
*/
|
|
326
|
-
addEventListener(eventName: 'completed', callback: (eventName: 'completed', session: Session) => Promise<void> | void): void;
|
|
327
|
-
/**
|
|
328
|
-
* Remove a previously registered 'completed' event listener.
|
|
329
|
-
*
|
|
330
|
-
* @param eventName - Must be 'completed'
|
|
331
|
-
* @param callback - The callback function to remove
|
|
332
|
-
*/
|
|
333
|
-
removeEventListener(eventName: 'completed', callback: (eventName: 'completed', session: Session) => Promise<void> | void): void;
|
|
334
|
-
/**
|
|
335
|
-
* Return the session data as a serializable string or return undefined if not
|
|
336
|
-
* data should be serialized.
|
|
337
|
-
*/
|
|
338
|
-
serializeUserData(): string | undefined;
|
|
339
|
-
}
|
|
340
|
-
/**
|
|
341
|
-
* Represent an interface for handling how thread ids are generated or restored.
|
|
342
|
-
*/
|
|
343
|
-
export interface ThreadIDProvider {
|
|
344
|
-
/**
|
|
345
|
-
* A function that should return a thread id to be used for the incoming request.
|
|
346
|
-
* The returning thread id must be globally unique and must start with the prefix
|
|
347
|
-
* thrd_ such as `thrd_212c16896b974ffeb21a748f0eeba620`. The max length of the
|
|
348
|
-
* string is 64 characters and the min length is 32 characters long
|
|
349
|
-
* (including the prefix). The characters after the prefix must match the
|
|
350
|
-
* regular expression [a-zA-Z0-9].
|
|
351
|
-
*
|
|
352
|
-
* @param appState - The app state from createApp setup function
|
|
353
|
-
* @param ctx - Hono request context
|
|
354
|
-
* @returns The thread id to use (can be async for signed cookies)
|
|
355
|
-
*/
|
|
356
|
-
getThreadId(appState: AppState, ctx: Context<Env>): string | Promise<string>;
|
|
357
|
-
}
|
|
358
|
-
/**
|
|
359
|
-
* Provider interface for managing thread lifecycle and persistence.
|
|
360
|
-
* Implement this to customize how threads are stored and retrieved.
|
|
361
|
-
*
|
|
362
|
-
* The default implementation (DefaultThreadProvider) stores threads in-memory
|
|
363
|
-
* with cookie-based identification and 1-hour expiration.
|
|
364
|
-
*
|
|
365
|
-
* Thread state is serialized using `getSerializedState()` which returns a JSON
|
|
366
|
-
* envelope: `{ "state": {...}, "metadata": {...} }`. Use `parseThreadData()` to
|
|
367
|
-
* correctly parse both old (flat) and new (envelope) formats on restore.
|
|
368
|
-
*
|
|
369
|
-
* @example
|
|
370
|
-
* ```typescript
|
|
371
|
-
* class RedisThreadProvider implements ThreadProvider {
|
|
372
|
-
* private redis: Redis;
|
|
373
|
-
*
|
|
374
|
-
* async initialize(appState: AppState): Promise<void> {
|
|
375
|
-
* this.redis = await connectRedis();
|
|
376
|
-
* }
|
|
377
|
-
*
|
|
378
|
-
* async restore(ctx: Context<Env>): Promise<Thread> {
|
|
379
|
-
* const threadId = ctx.req.header('x-thread-id') || getCookie(ctx, 'atid') || generateId('thrd');
|
|
380
|
-
* const data = await this.redis.get(`thread:${threadId}`);
|
|
381
|
-
*
|
|
382
|
-
* // Parse stored data, handling both old and new formats
|
|
383
|
-
* const { flatStateJson, metadata } = parseThreadData(data);
|
|
384
|
-
* const thread = new DefaultThread(this, threadId, flatStateJson, metadata);
|
|
385
|
-
*
|
|
386
|
-
* // Populate state from parsed data
|
|
387
|
-
* if (flatStateJson) {
|
|
388
|
-
* const stateObj = JSON.parse(flatStateJson);
|
|
389
|
-
* for (const [key, value] of Object.entries(stateObj)) {
|
|
390
|
-
* thread.state.set(key, value);
|
|
391
|
-
* }
|
|
392
|
-
* }
|
|
393
|
-
* return thread;
|
|
394
|
-
* }
|
|
395
|
-
*
|
|
396
|
-
* async save(thread: Thread): Promise<void> {
|
|
397
|
-
* if (thread instanceof DefaultThread && thread.isDirty()) {
|
|
398
|
-
* await this.redis.setex(
|
|
399
|
-
* `thread:${thread.id}`,
|
|
400
|
-
* 3600,
|
|
401
|
-
* thread.getSerializedState()
|
|
402
|
-
* );
|
|
403
|
-
* }
|
|
404
|
-
* }
|
|
405
|
-
*
|
|
406
|
-
* async destroy(thread: Thread): Promise<void> {
|
|
407
|
-
* await this.redis.del(`thread:${thread.id}`);
|
|
408
|
-
* }
|
|
409
|
-
* }
|
|
410
|
-
*
|
|
411
|
-
* // Use custom provider
|
|
412
|
-
* const app = await createApp({
|
|
413
|
-
* services: {
|
|
414
|
-
* thread: new RedisThreadProvider()
|
|
415
|
-
* }
|
|
416
|
-
* });
|
|
417
|
-
* ```
|
|
418
|
-
*/
|
|
419
|
-
export interface ThreadProvider {
|
|
420
|
-
/**
|
|
421
|
-
* Initialize the provider when the app starts.
|
|
422
|
-
* Use this to set up connections, start cleanup intervals, etc.
|
|
423
|
-
*
|
|
424
|
-
* @param appState - The app state from createApp setup function
|
|
425
|
-
*/
|
|
426
|
-
initialize(appState: AppState): Promise<void>;
|
|
427
|
-
/**
|
|
428
|
-
* Set the provider to use for generating / restoring the thread id
|
|
429
|
-
* on new requests. Overrides the built-in provider when set.
|
|
430
|
-
*
|
|
431
|
-
* @param provider - the provider implementation
|
|
432
|
-
*/
|
|
433
|
-
setThreadIDProvider(provider: ThreadIDProvider): void;
|
|
434
|
-
/**
|
|
435
|
-
* Restore or create a thread from the HTTP request context.
|
|
436
|
-
* Should check cookies for existing thread ID or create a new one.
|
|
437
|
-
*
|
|
438
|
-
* @param ctx - Hono request context
|
|
439
|
-
* @returns The restored or newly created thread
|
|
440
|
-
*/
|
|
441
|
-
restore(ctx: Context<Env>): Promise<Thread>;
|
|
442
|
-
/**
|
|
443
|
-
* Persist thread state to storage.
|
|
444
|
-
* Called periodically to save thread data.
|
|
445
|
-
*
|
|
446
|
-
* @param thread - The thread to save
|
|
447
|
-
*/
|
|
448
|
-
save(thread: Thread): Promise<void>;
|
|
449
|
-
/**
|
|
450
|
-
* Destroy a thread and clean up resources.
|
|
451
|
-
* Should fire the 'destroyed' event and remove from storage.
|
|
452
|
-
*
|
|
453
|
-
* @param thread - The thread to destroy
|
|
454
|
-
*/
|
|
455
|
-
destroy(thread: Thread): Promise<void>;
|
|
456
|
-
}
|
|
457
|
-
/**
|
|
458
|
-
* Provider interface for managing session lifecycle and persistence.
|
|
459
|
-
* Implement this to customize how sessions are stored and retrieved.
|
|
460
|
-
*
|
|
461
|
-
* The default implementation (DefaultSessionProvider) stores sessions in-memory
|
|
462
|
-
* and automatically cleans them up after completion.
|
|
463
|
-
*
|
|
464
|
-
* @example
|
|
465
|
-
* ```typescript
|
|
466
|
-
* class PostgresSessionProvider implements SessionProvider {
|
|
467
|
-
* private db: Database;
|
|
468
|
-
*
|
|
469
|
-
* async initialize(appState: AppState): Promise<void> {
|
|
470
|
-
* this.db = appState.db;
|
|
471
|
-
* }
|
|
472
|
-
*
|
|
473
|
-
* async restore(thread: Thread, sessionId: string): Promise<Session> {
|
|
474
|
-
* const row = await this.db.query(
|
|
475
|
-
* 'SELECT state FROM sessions WHERE id = $1',
|
|
476
|
-
* [sessionId]
|
|
477
|
-
* );
|
|
478
|
-
* const session = new DefaultSession(thread, sessionId);
|
|
479
|
-
* if (row) {
|
|
480
|
-
* session.state = new Map(JSON.parse(row.state));
|
|
481
|
-
* }
|
|
482
|
-
* return session;
|
|
483
|
-
* }
|
|
484
|
-
*
|
|
485
|
-
* async save(session: Session): Promise<void> {
|
|
486
|
-
* await this.db.query(
|
|
487
|
-
* 'INSERT INTO sessions (id, thread_id, state) VALUES ($1, $2, $3)',
|
|
488
|
-
* [session.id, session.thread.id, JSON.stringify([...session.state])]
|
|
489
|
-
* );
|
|
490
|
-
* }
|
|
491
|
-
* }
|
|
492
|
-
*
|
|
493
|
-
* // Use custom provider
|
|
494
|
-
* const app = await createApp({
|
|
495
|
-
* services: {
|
|
496
|
-
* session: new PostgresSessionProvider()
|
|
497
|
-
* }
|
|
498
|
-
* });
|
|
499
|
-
* ```
|
|
500
|
-
*/
|
|
501
|
-
export interface SessionProvider {
|
|
502
|
-
/**
|
|
503
|
-
* Initialize the provider when the app starts.
|
|
504
|
-
* Use this to set up database connections or other resources.
|
|
505
|
-
*
|
|
506
|
-
* @param appState - The app state from createApp setup function
|
|
507
|
-
*/
|
|
508
|
-
initialize(appState: AppState): Promise<void>;
|
|
509
|
-
/**
|
|
510
|
-
* Restore or create a session for the given thread and session ID.
|
|
511
|
-
* Should load existing session data or create a new session.
|
|
512
|
-
*
|
|
513
|
-
* @param thread - The parent thread for this session
|
|
514
|
-
* @param sessionId - The unique session identifier
|
|
515
|
-
* @returns The restored or newly created session
|
|
516
|
-
*/
|
|
517
|
-
restore(thread: Thread, sessionId: string): Promise<Session>;
|
|
518
|
-
/**
|
|
519
|
-
* Persist session state and fire completion events.
|
|
520
|
-
* Called after the agent handler completes.
|
|
521
|
-
*
|
|
522
|
-
* @param session - The session to save
|
|
523
|
-
*/
|
|
524
|
-
save(session: Session): Promise<void>;
|
|
525
|
-
}
|
|
526
|
-
export declare function generateId(prefix?: string): string;
|
|
527
|
-
/**
|
|
528
|
-
* Validates a thread ID against runtime constraints:
|
|
529
|
-
* - Must start with 'thrd_'
|
|
530
|
-
* - Must be at least 32 characters long (including prefix)
|
|
531
|
-
* - Must be less than 64 characters long
|
|
532
|
-
* - Must contain only [a-zA-Z0-9] after 'thrd_' prefix (no dashes for maximum randomness)
|
|
533
|
-
*/
|
|
534
|
-
export declare function isValidThreadId(threadId: string): boolean;
|
|
535
|
-
/**
|
|
536
|
-
* Validates a thread ID and throws detailed error messages for debugging.
|
|
537
|
-
* @param threadId The thread ID to validate
|
|
538
|
-
* @throws Error with detailed message if validation fails
|
|
539
|
-
*/
|
|
540
|
-
export declare function validateThreadIdOrThrow(threadId: string): void;
|
|
541
|
-
/**
|
|
542
|
-
* Determines if the connection is secure (HTTPS) by checking the request protocol
|
|
543
|
-
* and x-forwarded-proto header (for reverse proxy scenarios).
|
|
544
|
-
* Defaults to false (HTTP) if unable to determine.
|
|
545
|
-
*/
|
|
546
|
-
export declare function isSecureConnection(ctx: Context<Env>): boolean;
|
|
547
|
-
/**
|
|
548
|
-
* Signs a thread ID using HMAC SHA-256 and returns it in the format: threadId;signature
|
|
549
|
-
* Format: thrd_abc123;base64signature
|
|
550
|
-
*/
|
|
551
|
-
export declare function signThreadId(threadId: string, secret: string): Promise<string>;
|
|
552
|
-
/**
|
|
553
|
-
* Verifies a signed thread ID header and returns the thread ID if valid, or undefined if invalid.
|
|
554
|
-
* Expected format: thrd_abc123;base64signature
|
|
555
|
-
*/
|
|
556
|
-
export declare function verifySignedThreadId(signedValue: string, secret: string): Promise<string | undefined>;
|
|
557
|
-
/**
|
|
558
|
-
* DefaultThreadIDProvider will look for an HTTP header `x-thread-id` first,
|
|
559
|
-
* then fall back to a signed cookie named `atid`, and use that as the thread id.
|
|
560
|
-
* If not found, generate a new one. Validates incoming thread IDs against
|
|
561
|
-
* runtime constraints. Uses AGENTUITY_SDK_KEY for signing, falls back to 'agentuity'.
|
|
562
|
-
*/
|
|
563
|
-
export declare class DefaultThreadIDProvider implements ThreadIDProvider {
|
|
564
|
-
private getSecret;
|
|
565
|
-
getThreadId(_appState: AppState, ctx: Context<Env>): Promise<string>;
|
|
566
|
-
}
|
|
567
|
-
type LazyStateStatus = 'idle' | 'pending-writes' | 'loaded';
|
|
568
|
-
type RestoreFn = () => Promise<{
|
|
569
|
-
state: Map<string, unknown>;
|
|
570
|
-
metadata: Record<string, unknown>;
|
|
571
|
-
}>;
|
|
572
|
-
export declare class LazyThreadState implements ThreadState {
|
|
573
|
-
#private;
|
|
574
|
-
constructor(restoreFn: RestoreFn);
|
|
575
|
-
get loaded(): boolean;
|
|
576
|
-
get dirty(): boolean;
|
|
577
|
-
private ensureLoaded;
|
|
578
|
-
private doLoad;
|
|
579
|
-
get<T = unknown>(key: string): Promise<T | undefined>;
|
|
580
|
-
set<T = unknown>(key: string, value: T): Promise<void>;
|
|
581
|
-
has(key: string): Promise<boolean>;
|
|
582
|
-
delete(key: string): Promise<void>;
|
|
583
|
-
clear(): Promise<void>;
|
|
584
|
-
entries<T = unknown>(): Promise<[string, T][]>;
|
|
585
|
-
keys(): Promise<string[]>;
|
|
586
|
-
values<T = unknown>(): Promise<T[]>;
|
|
587
|
-
size(): Promise<number>;
|
|
588
|
-
push<T = unknown>(key: string, value: T, maxRecords?: number): Promise<void>;
|
|
589
|
-
/**
|
|
590
|
-
* Get the current status for save logic
|
|
591
|
-
* @internal
|
|
592
|
-
*/
|
|
593
|
-
getStatus(): LazyStateStatus;
|
|
594
|
-
/**
|
|
595
|
-
* Get pending operations for merge command
|
|
596
|
-
* @internal
|
|
597
|
-
*/
|
|
598
|
-
getPendingOperations(): MergeOperation[];
|
|
599
|
-
/**
|
|
600
|
-
* Get serialized state for full save.
|
|
601
|
-
* Ensures state is loaded before serializing.
|
|
602
|
-
* @internal
|
|
603
|
-
*/
|
|
604
|
-
getSerializedState(): Promise<Record<string, unknown>>;
|
|
605
|
-
}
|
|
606
|
-
export declare class DefaultThread implements Thread {
|
|
607
|
-
#private;
|
|
608
|
-
readonly id: string;
|
|
609
|
-
readonly state: LazyThreadState;
|
|
610
|
-
private provider;
|
|
611
|
-
constructor(provider: ThreadProvider, id: string, restoreFn: RestoreFn, initialMetadata?: Record<string, unknown>);
|
|
612
|
-
private ensureMetadataLoaded;
|
|
613
|
-
private doLoadMetadata;
|
|
614
|
-
getMetadata(): Promise<Record<string, unknown>>;
|
|
615
|
-
setMetadata(metadata: Record<string, unknown>): Promise<void>;
|
|
616
|
-
addEventListener(eventName: ThreadEventName, callback: ThreadEventCallback<any>): void;
|
|
617
|
-
removeEventListener(eventName: ThreadEventName, callback: ThreadEventCallback<any>): void;
|
|
618
|
-
fireEvent(eventName: ThreadEventName): Promise<void>;
|
|
619
|
-
destroy(): Promise<void>;
|
|
620
|
-
/**
|
|
621
|
-
* Check if thread has any data (state or metadata)
|
|
622
|
-
*/
|
|
623
|
-
empty(): Promise<boolean>;
|
|
624
|
-
/**
|
|
625
|
-
* Check if thread needs saving
|
|
626
|
-
* @internal
|
|
627
|
-
*/
|
|
628
|
-
needsSave(): boolean;
|
|
629
|
-
/**
|
|
630
|
-
* Get the save mode for this thread
|
|
631
|
-
* @internal
|
|
632
|
-
*/
|
|
633
|
-
getSaveMode(): 'none' | 'merge' | 'full';
|
|
634
|
-
/**
|
|
635
|
-
* Get pending operations for merge command
|
|
636
|
-
* @internal
|
|
637
|
-
*/
|
|
638
|
-
getPendingOperations(): MergeOperation[];
|
|
639
|
-
/**
|
|
640
|
-
* Get metadata for saving (returns null if not loaded/modified)
|
|
641
|
-
* @internal
|
|
642
|
-
*/
|
|
643
|
-
getMetadataForSave(): Record<string, unknown> | undefined;
|
|
644
|
-
/**
|
|
645
|
-
* Get serialized state for full save.
|
|
646
|
-
* Ensures state is loaded before serializing.
|
|
647
|
-
* @internal
|
|
648
|
-
*/
|
|
649
|
-
getSerializedState(): Promise<string>;
|
|
650
|
-
}
|
|
651
|
-
export declare class DefaultSession implements Session {
|
|
652
|
-
readonly id: string;
|
|
653
|
-
readonly thread: Thread;
|
|
654
|
-
readonly state: Map<string, unknown>;
|
|
655
|
-
metadata: Record<string, unknown>;
|
|
656
|
-
constructor(thread: Thread, id: string, metadata?: Record<string, unknown>);
|
|
657
|
-
addEventListener(eventName: SessionEventName, callback: SessionEventCallback<any>): void;
|
|
658
|
-
removeEventListener(eventName: SessionEventName, callback: SessionEventCallback<any>): void;
|
|
659
|
-
fireEvent(eventName: SessionEventName): Promise<void>;
|
|
660
|
-
/**
|
|
661
|
-
* Serialize session state to JSON string for persistence.
|
|
662
|
-
* Returns undefined if state is empty or exceeds 1MB limit.
|
|
663
|
-
* @internal
|
|
664
|
-
*/
|
|
665
|
-
serializeUserData(): string | undefined;
|
|
666
|
-
}
|
|
667
|
-
/**
|
|
668
|
-
* WebSocket client for thread state persistence.
|
|
669
|
-
*
|
|
670
|
-
* **WARNING: This class is exported for testing purposes only and is subject to change
|
|
671
|
-
* without notice. Do not use this class directly in production code.**
|
|
672
|
-
*
|
|
673
|
-
* @internal
|
|
674
|
-
* @experimental
|
|
675
|
-
*/
|
|
676
|
-
/**
|
|
677
|
-
* Configuration options for ThreadWebSocketClient
|
|
678
|
-
*/
|
|
679
|
-
export interface ThreadWebSocketClientOptions {
|
|
680
|
-
/** Connection timeout in milliseconds (default: 10000) */
|
|
681
|
-
connectionTimeoutMs?: number;
|
|
682
|
-
/** Request timeout in milliseconds (default: 10000) */
|
|
683
|
-
requestTimeoutMs?: number;
|
|
684
|
-
/** Base delay for reconnection backoff in milliseconds (default: 1000) */
|
|
685
|
-
reconnectBaseDelayMs?: number;
|
|
686
|
-
/** Maximum delay for reconnection backoff in milliseconds (default: 30000) */
|
|
687
|
-
reconnectMaxDelayMs?: number;
|
|
688
|
-
/** Maximum number of reconnection attempts (default: 5) */
|
|
689
|
-
maxReconnectAttempts?: number;
|
|
690
|
-
}
|
|
691
|
-
export declare class ThreadWebSocketClient {
|
|
692
|
-
private ws;
|
|
693
|
-
private authenticated;
|
|
694
|
-
private pendingRequests;
|
|
695
|
-
private reconnectAttempts;
|
|
696
|
-
private maxReconnectAttempts;
|
|
697
|
-
private apiKey;
|
|
698
|
-
private wsUrl;
|
|
699
|
-
private wsConnecting;
|
|
700
|
-
private reconnectTimer;
|
|
701
|
-
private isDisposed;
|
|
702
|
-
private initialConnectResolve;
|
|
703
|
-
private initialConnectReject;
|
|
704
|
-
private connectionTimeoutMs;
|
|
705
|
-
private requestTimeoutMs;
|
|
706
|
-
private reconnectBaseDelayMs;
|
|
707
|
-
private reconnectMaxDelayMs;
|
|
708
|
-
constructor(apiKey: string, wsUrl: string, options?: ThreadWebSocketClientOptions);
|
|
709
|
-
connect(): Promise<void>;
|
|
710
|
-
restore(threadId: string): Promise<string | undefined>;
|
|
711
|
-
save(threadId: string, userData: string, threadMetadata?: Record<string, unknown>): Promise<void>;
|
|
712
|
-
delete(threadId: string): Promise<void>;
|
|
713
|
-
merge(threadId: string, operations: MergeOperation[], metadata?: Record<string, unknown>): Promise<void>;
|
|
714
|
-
cleanup(): void;
|
|
715
|
-
}
|
|
716
|
-
export declare class DefaultThreadProvider implements ThreadProvider {
|
|
717
|
-
private appState;
|
|
718
|
-
private wsClient;
|
|
719
|
-
private wsConnecting;
|
|
720
|
-
private threadIDProvider;
|
|
721
|
-
initialize(appState: AppState): Promise<void>;
|
|
722
|
-
setThreadIDProvider(provider: ThreadIDProvider): void;
|
|
723
|
-
restore(ctx: Context<Env>): Promise<Thread>;
|
|
724
|
-
save(thread: Thread): Promise<void>;
|
|
725
|
-
destroy(thread: Thread): Promise<void>;
|
|
726
|
-
}
|
|
727
|
-
export declare class DefaultSessionProvider implements SessionProvider {
|
|
728
|
-
private sessions;
|
|
729
|
-
initialize(_appState: AppState): Promise<void>;
|
|
730
|
-
restore(thread: Thread, sessionId: string): Promise<Session>;
|
|
731
|
-
save(session: Session): Promise<void>;
|
|
732
|
-
}
|
|
733
|
-
export {};
|
|
734
|
-
//# sourceMappingURL=session.d.ts.map
|