@rool-dev/sdk 0.2.0-dev.f798776 → 0.3.0-dev.be25932

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,316 @@
1
+ import { EventEmitter } from './event-emitter.js';
2
+ import type { GraphQLClient } from './graphql.js';
3
+ import type { MediaClient } from './media.js';
4
+ import type { AuthManager } from './auth.js';
5
+ import type { Logger } from './logger.js';
6
+ import type { RoolObject, RoolObjectStat, ChannelEvents, RoolUserRole, PromptOptions, FindObjectsOptions, CreateObjectOptions, UpdateObjectOptions, MediaInfo, MediaResponse, Interaction, Channel, LinkAccess, SpaceSchema, CollectionDef, FieldDef } from './types.js';
7
+ export declare function generateEntityId(): string;
8
+ export interface ChannelConfig {
9
+ id: string;
10
+ name: string;
11
+ role: RoolUserRole;
12
+ linkAccess: LinkAccess;
13
+ /** Current user's ID (for identifying own interactions) */
14
+ userId: string;
15
+ /** Object IDs in the space (sorted by modifiedAt desc) */
16
+ objectIds: string[];
17
+ /** Object stats keyed by object ID */
18
+ objectStats: Record<string, RoolObjectStat>;
19
+ /** Collection schema */
20
+ schema: SpaceSchema;
21
+ /** Space metadata */
22
+ meta: Record<string, unknown>;
23
+ /** This channel's conversation data (undefined if new) */
24
+ channel: Channel | undefined;
25
+ /** Channel ID for this channel (required). */
26
+ channelId: string;
27
+ graphqlClient: GraphQLClient;
28
+ mediaClient: MediaClient;
29
+ graphqlUrl: string;
30
+ authManager: AuthManager;
31
+ logger: Logger;
32
+ onClose: (spaceId: string) => void;
33
+ }
34
+ /**
35
+ * A channel is a space + channelId pair.
36
+ *
37
+ * All object operations go through a channel. The channelId is fixed
38
+ * at open time and cannot be changed. To use a different channel,
39
+ * open a second one.
40
+ *
41
+ * Objects are fetched on demand from the server; only schema, metadata,
42
+ * and the channel's own conversation are cached locally. Object changes
43
+ * arrive via SSE semantic events and are emitted as SDK events.
44
+ *
45
+ * Features:
46
+ * - High-level object operations
47
+ * - Built-in undo/redo with checkpoints
48
+ * - Metadata management
49
+ * - Event emission for state changes
50
+ * - Real-time updates via space-specific subscription
51
+ */
52
+ export declare class RoolChannel extends EventEmitter<ChannelEvents> {
53
+ private _id;
54
+ private _name;
55
+ private _role;
56
+ private _linkAccess;
57
+ private _userId;
58
+ private _channelId;
59
+ private _closed;
60
+ private graphqlClient;
61
+ private mediaClient;
62
+ private subscriptionManager;
63
+ private onCloseCallback;
64
+ private _subscriptionReady;
65
+ private logger;
66
+ private _meta;
67
+ private _schema;
68
+ private _conversation;
69
+ private _objectIds;
70
+ private _objectStats;
71
+ private _pendingMutations;
72
+ private _objectResolvers;
73
+ private _objectBuffer;
74
+ constructor(config: ChannelConfig);
75
+ /**
76
+ * Wait for the real-time subscription to be established.
77
+ * Called internally by openChannel/createSpace before returning the channel.
78
+ * @internal
79
+ */
80
+ _waitForSubscription(): Promise<void>;
81
+ get id(): string;
82
+ get name(): string;
83
+ get role(): RoolUserRole;
84
+ get linkAccess(): LinkAccess;
85
+ /** Current user's ID (for identifying own interactions) */
86
+ get userId(): string;
87
+ /**
88
+ * Get the channel ID for this channel.
89
+ * Fixed at open time — cannot be changed.
90
+ */
91
+ get channelId(): string;
92
+ get isReadOnly(): boolean;
93
+ /**
94
+ * Get interactions for this channel's conversation.
95
+ */
96
+ getInteractions(): Interaction[];
97
+ /**
98
+ * Close this channel and clean up resources.
99
+ * Stops real-time subscription and unregisters from client.
100
+ */
101
+ close(): void;
102
+ /**
103
+ * Create a checkpoint (seal current batch of changes).
104
+ * @returns The checkpoint ID
105
+ */
106
+ checkpoint(label?: string): Promise<string>;
107
+ /**
108
+ * Check if undo is available.
109
+ */
110
+ canUndo(): Promise<boolean>;
111
+ /**
112
+ * Check if redo is available.
113
+ */
114
+ canRedo(): Promise<boolean>;
115
+ /**
116
+ * Undo the most recent batch of changes.
117
+ * Reverses your most recent batch (sealed or open).
118
+ * Conflicting patches (modified by others) are silently skipped.
119
+ * @returns true if undo was performed
120
+ */
121
+ undo(): Promise<boolean>;
122
+ /**
123
+ * Redo a previously undone batch of changes.
124
+ * @returns true if redo was performed
125
+ */
126
+ redo(): Promise<boolean>;
127
+ /**
128
+ * Clear checkpoint history for this conversation.
129
+ */
130
+ clearHistory(): Promise<void>;
131
+ /**
132
+ * Get an object's data by ID.
133
+ * Fetches from the server on each call.
134
+ */
135
+ getObject(objectId: string): Promise<RoolObject | undefined>;
136
+ /**
137
+ * Get an object's stat (audit information).
138
+ * Returns modification timestamp and author, or undefined if object not found.
139
+ */
140
+ stat(objectId: string): RoolObjectStat | undefined;
141
+ /**
142
+ * Find objects using structured filters and/or natural language.
143
+ *
144
+ * `where` provides exact-match filtering — values must match literally (no placeholders or operators).
145
+ * `prompt` enables AI-powered semantic queries. When both are provided, `where` and `objectIds`
146
+ * constrain the data set before the AI sees it.
147
+ *
148
+ * @param options.where - Exact-match field filter (e.g. `{ type: 'article' }`). Constrains which objects the AI can see when combined with `prompt`.
149
+ * @param options.prompt - Natural language query. Triggers AI evaluation (uses credits).
150
+ * @param options.limit - Maximum number of results to return (applies to structured filtering only; the AI controls its own result size).
151
+ * @param options.objectIds - Scope search to specific object IDs. Constrains the candidate set in both structured and AI queries.
152
+ * @param options.order - Sort order by modifiedAt: `'asc'` or `'desc'` (default: `'desc'`). Only applies to structured filtering (no `prompt`).
153
+ * @param options.ephemeral - If true, the query won't be recorded in conversation history.
154
+ * @returns The matching objects and a descriptive message.
155
+ */
156
+ findObjects(options: FindObjectsOptions): Promise<{
157
+ objects: RoolObject[];
158
+ message: string;
159
+ }>;
160
+ /**
161
+ * Get all object IDs (sync, from local cache).
162
+ * The list is loaded on open and kept current via SSE events.
163
+ * @param options.limit - Maximum number of IDs to return
164
+ * @param options.order - Sort order by modifiedAt ('asc' or 'desc', default: 'desc')
165
+ */
166
+ getObjectIds(options?: {
167
+ limit?: number;
168
+ order?: 'asc' | 'desc';
169
+ }): string[];
170
+ /**
171
+ * Create a new object with optional AI generation.
172
+ * @param options.data - Object data fields (any key-value pairs). Optionally include `id` to use a custom ID. Use {{placeholder}} for AI-generated content. Fields prefixed with _ are hidden from AI.
173
+ * @param options.ephemeral - If true, the operation won't be recorded in conversation history.
174
+ * @returns The created object (with AI-filled content) and message
175
+ */
176
+ createObject(options: CreateObjectOptions): Promise<{
177
+ object: RoolObject;
178
+ message: string;
179
+ }>;
180
+ /**
181
+ * Update an existing object.
182
+ * @param objectId - The ID of the object to update
183
+ * @param options.data - Fields to add or update. Pass null or undefined to delete a field. Use {{placeholder}} for AI-generated content. Fields prefixed with _ are hidden from AI.
184
+ * @param options.prompt - AI prompt for content editing (optional).
185
+ * @param options.ephemeral - If true, the operation won't be recorded in conversation history.
186
+ * @returns The updated object (with AI-filled content) and message
187
+ */
188
+ updateObject(objectId: string, options: UpdateObjectOptions): Promise<{
189
+ object: RoolObject;
190
+ message: string;
191
+ }>;
192
+ /**
193
+ * Delete objects by IDs.
194
+ * Other objects that reference deleted objects via data fields will retain stale ref values.
195
+ */
196
+ deleteObjects(objectIds: string[]): Promise<void>;
197
+ /**
198
+ * Get the current schema for this space.
199
+ * Returns a map of collection names to their definitions.
200
+ */
201
+ getSchema(): SpaceSchema;
202
+ /**
203
+ * Create a new collection schema.
204
+ * @param name - Collection name (must start with a letter, alphanumeric/hyphens/underscores only)
205
+ * @param fields - Field definitions for the collection
206
+ * @returns The created CollectionDef
207
+ */
208
+ createCollection(name: string, fields: FieldDef[]): Promise<CollectionDef>;
209
+ /**
210
+ * Alter an existing collection schema, replacing its field definitions.
211
+ * @param name - Name of the collection to alter
212
+ * @param fields - New field definitions (replaces all existing fields)
213
+ * @returns The updated CollectionDef
214
+ */
215
+ alterCollection(name: string, fields: FieldDef[]): Promise<CollectionDef>;
216
+ /**
217
+ * Drop a collection schema.
218
+ * @param name - Name of the collection to drop
219
+ */
220
+ dropCollection(name: string): Promise<void>;
221
+ /**
222
+ * Get the system instruction for this channel's conversation.
223
+ * Returns undefined if no system instruction is set.
224
+ */
225
+ getSystemInstruction(): string | undefined;
226
+ /**
227
+ * Set the system instruction for this channel's conversation.
228
+ * Pass null to clear the instruction.
229
+ */
230
+ setSystemInstruction(instruction: string | null): Promise<void>;
231
+ /**
232
+ * Set a space-level metadata value.
233
+ * Metadata is stored in meta and hidden from AI operations.
234
+ */
235
+ setMetadata(key: string, value: unknown): void;
236
+ /**
237
+ * Get a space-level metadata value.
238
+ */
239
+ getMetadata(key: string): unknown;
240
+ /**
241
+ * Get all space-level metadata.
242
+ */
243
+ getAllMetadata(): Record<string, unknown>;
244
+ /**
245
+ * Send a prompt to the AI agent for space manipulation.
246
+ * @returns The message from the AI and the list of objects that were created or modified
247
+ */
248
+ prompt(prompt: string, options?: PromptOptions): Promise<{
249
+ message: string;
250
+ objects: RoolObject[];
251
+ }>;
252
+ /**
253
+ * Rename this channel (conversation).
254
+ */
255
+ rename(newName: string): Promise<void>;
256
+ /**
257
+ * List all media files for this space.
258
+ */
259
+ listMedia(): Promise<MediaInfo[]>;
260
+ /**
261
+ * Upload a file to this space. Returns the URL.
262
+ */
263
+ uploadMedia(file: File | Blob | {
264
+ data: string;
265
+ contentType: string;
266
+ }): Promise<string>;
267
+ /**
268
+ * Fetch any URL, returning headers and a blob() method (like fetch Response).
269
+ * Adds auth headers for backend media URLs, fetches external URLs via server proxy if CORS blocks.
270
+ */
271
+ fetchMedia(url: string): Promise<MediaResponse>;
272
+ /**
273
+ * Delete a media file by URL.
274
+ */
275
+ deleteMedia(url: string): Promise<void>;
276
+ /**
277
+ * Register a collector that resolves when the object arrives via SSE.
278
+ * If the object is already in the buffer (arrived before collector), resolves immediately.
279
+ * @internal
280
+ */
281
+ private _collectObject;
282
+ /**
283
+ * Cancel a pending object collector (e.g., on mutation error).
284
+ * @internal
285
+ */
286
+ private _cancelCollector;
287
+ /**
288
+ * Deliver an object to a pending collector, or buffer it for later collection.
289
+ * @internal
290
+ */
291
+ private _deliverObject;
292
+ /**
293
+ * Handle a channel event from the subscription.
294
+ * @internal
295
+ */
296
+ private handleChannelEvent;
297
+ /**
298
+ * Handle an object_created SSE event.
299
+ * Deduplicates against optimistic local creates.
300
+ * @internal
301
+ */
302
+ private _handleObjectCreated;
303
+ /**
304
+ * Handle an object_updated SSE event.
305
+ * Deduplicates against optimistic local updates.
306
+ * @internal
307
+ */
308
+ private _handleObjectUpdated;
309
+ /**
310
+ * Handle an object_deleted SSE event.
311
+ * Deduplicates against optimistic local deletes.
312
+ * @internal
313
+ */
314
+ private _handleObjectDeleted;
315
+ }
316
+ //# sourceMappingURL=channel.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"channel.d.ts","sourceRoot":"","sources":["../src/channel.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAC9C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAE7C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,KAAK,EACV,UAAU,EACV,cAAc,EACd,aAAa,EACb,YAAY,EACZ,aAAa,EACb,kBAAkB,EAClB,mBAAmB,EACnB,mBAAmB,EACnB,SAAS,EACT,aAAa,EAGb,WAAW,EACX,OAAO,EACP,UAAU,EACV,WAAW,EACX,aAAa,EACb,QAAQ,EACT,MAAM,YAAY,CAAC;AAKpB,wBAAgB,gBAAgB,IAAI,MAAM,CAMzC;AAKD,MAAM,WAAW,aAAa;IAC5B,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,YAAY,CAAC;IACnB,UAAU,EAAE,UAAU,CAAC;IACvB,2DAA2D;IAC3D,MAAM,EAAE,MAAM,CAAC;IACf,0DAA0D;IAC1D,SAAS,EAAE,MAAM,EAAE,CAAC;IACpB,sCAAsC;IACtC,WAAW,EAAE,MAAM,CAAC,MA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