@granular-software/sdk 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,595 @@
1
+ /**
2
+ * @module @granular-software/sdk/types
3
+ * Type definitions for the Granular SDK
4
+ */
5
+ /**
6
+ * Configuration for the Granular client
7
+ */
8
+ interface GranularOptions {
9
+ /** Your Granular API key */
10
+ apiKey: string;
11
+ /** Optional API URL (for on-prem or testing) */
12
+ apiUrl?: string;
13
+ }
14
+ /**
15
+ * A user/subject object returned from recordUser()
16
+ */
17
+ interface User {
18
+ /** Internal subject ID */
19
+ subjectId: string;
20
+ /** External identity ID (e.g. Auth0 ID) */
21
+ identityId: string;
22
+ /** User's display name */
23
+ name?: string;
24
+ /** User's email */
25
+ email?: string;
26
+ /** Permission profile IDs to be assigned when connecting */
27
+ permissions: string[];
28
+ }
29
+ /**
30
+ * Options for recording a user
31
+ */
32
+ interface RecordUserOptions {
33
+ /** External user/identity ID (e.g. your Auth0 or database user ID) */
34
+ userId: string;
35
+ /** User's display name */
36
+ name?: string;
37
+ /** User's email */
38
+ email?: string;
39
+ /** Permission profile IDs to assign when connecting to sandboxes */
40
+ permissions?: string[];
41
+ }
42
+ /**
43
+ * Subject as returned from the API
44
+ */
45
+ interface Subject {
46
+ subjectId: string;
47
+ tenantId: string;
48
+ identityId: string;
49
+ email?: string | null;
50
+ name?: string | null;
51
+ metadata?: Record<string, unknown>;
52
+ createdAt: number;
53
+ updatedAt: number;
54
+ }
55
+ /**
56
+ * Options for connecting to a sandbox
57
+ */
58
+ interface ConnectOptions {
59
+ /** The sandbox name or ID to connect to */
60
+ sandbox: string;
61
+ /** The user to connect as (from recordUser()) */
62
+ user: User;
63
+ }
64
+ /**
65
+ * A sandbox container
66
+ */
67
+ interface Sandbox {
68
+ sandboxId: string;
69
+ tenantId: string;
70
+ name: string;
71
+ description?: string | null;
72
+ createdAt: number;
73
+ updatedAt: number;
74
+ }
75
+ /**
76
+ * Data for creating a new sandbox
77
+ */
78
+ interface CreateSandboxData {
79
+ name: string;
80
+ description?: string;
81
+ }
82
+ /**
83
+ * List response for sandboxes
84
+ */
85
+ interface SandboxListResponse {
86
+ items: Sandbox[];
87
+ }
88
+ /**
89
+ * Rules defining what tools and resources are allowed/denied
90
+ */
91
+ interface PermissionRules {
92
+ /** Tool access rules */
93
+ tools?: {
94
+ /** Patterns for allowed tools (e.g. ["*"] for all, ["read_*"] for prefix match) */
95
+ allow?: string[];
96
+ /** Patterns for denied tools */
97
+ deny?: string[];
98
+ };
99
+ /** Resource access rules */
100
+ resources?: {
101
+ /** Patterns for allowed resources */
102
+ allow?: string[];
103
+ /** Patterns for denied resources */
104
+ deny?: string[];
105
+ };
106
+ }
107
+ /**
108
+ * A permission profile defines access controls for an environment
109
+ */
110
+ interface PermissionProfile {
111
+ permissionProfileId: string;
112
+ sandboxId: string;
113
+ name: string;
114
+ rules: PermissionRules;
115
+ createdAt: number;
116
+ updatedAt: number;
117
+ }
118
+ /**
119
+ * Data for creating a new permission profile
120
+ */
121
+ interface CreatePermissionProfileData {
122
+ name: string;
123
+ rules: PermissionRules;
124
+ }
125
+ /**
126
+ * List response for permission profiles
127
+ */
128
+ interface PermissionProfileListResponse {
129
+ items: PermissionProfile[];
130
+ }
131
+ /**
132
+ * An assignment links a subject to a sandbox with a permission profile
133
+ */
134
+ interface Assignment {
135
+ assignmentId: string;
136
+ tenantId: string;
137
+ subjectId: string;
138
+ sandboxId: string;
139
+ permissionProfileId: string;
140
+ createdAt: number;
141
+ createdBy?: string | null;
142
+ }
143
+ /**
144
+ * List response for assignments
145
+ */
146
+ interface AssignmentListResponse {
147
+ items: Assignment[];
148
+ }
149
+ /**
150
+ * Build policy for environments
151
+ */
152
+ interface BuildPolicy {
153
+ mode: 'current' | 'pinned';
154
+ buildId?: string;
155
+ }
156
+ /**
157
+ * An environment links a user (subject) to a sandbox with specific permissions
158
+ */
159
+ interface EnvironmentData {
160
+ environmentId: string;
161
+ sandboxId: string;
162
+ buildId: string;
163
+ subjectId: string;
164
+ permissionProfileId: string;
165
+ buildPolicy: BuildPolicy;
166
+ createdAt: number;
167
+ updatedAt: number;
168
+ }
169
+ /**
170
+ * Data for creating a new environment
171
+ */
172
+ interface CreateEnvironmentData {
173
+ /** The user/subject ID to create the environment for */
174
+ subjectId: string;
175
+ /** The permission profile to apply (optional - uses assignment if not specified) */
176
+ permissionProfileId?: string | null;
177
+ /** Build policy (defaults to current build) */
178
+ buildPolicy?: BuildPolicy;
179
+ }
180
+ /**
181
+ * List response for environments
182
+ */
183
+ interface EnvironmentListResponse {
184
+ items: EnvironmentData[];
185
+ }
186
+ /**
187
+ * A manifest describes the structure and behavior of a sandbox
188
+ */
189
+ interface Manifest {
190
+ manifestId: string;
191
+ sandboxId: string;
192
+ version: string;
193
+ digest: string;
194
+ content?: Record<string, unknown>;
195
+ createdAt: number;
196
+ locked?: boolean;
197
+ }
198
+ /**
199
+ * List response for manifests
200
+ */
201
+ interface ManifestListResponse {
202
+ items: Manifest[];
203
+ }
204
+ type BuildStatus = 'queued' | 'building' | 'completed' | 'failed' | 'canceled';
205
+ /**
206
+ * A build represents a compiled version of a manifest
207
+ */
208
+ interface Build {
209
+ buildId: string;
210
+ sandboxId: string;
211
+ manifestId: string;
212
+ status: BuildStatus;
213
+ graphBinaryId?: string | null;
214
+ logsUri?: string | null;
215
+ createdAt: number;
216
+ updatedAt: number;
217
+ isCurrent?: boolean;
218
+ }
219
+ /**
220
+ * List response for builds
221
+ */
222
+ interface BuildListResponse {
223
+ items: Build[];
224
+ }
225
+ /**
226
+ * Tool handler for static/global tools: receives (params)
227
+ */
228
+ type ToolHandler = (input: any) => Promise<unknown>;
229
+ /**
230
+ * Tool handler for instance methods: receives (objectId, params)
231
+ */
232
+ type InstanceToolHandler = (id: string, input: any) => Promise<unknown>;
233
+ /**
234
+ * Tool schema for publishing to the server.
235
+ *
236
+ * Tools come in three flavours:
237
+ *
238
+ * 1. **Instance methods** — set `className`, omit `static`.
239
+ * In the sandbox: `tolkien.get_bio({ detailed: true })`
240
+ * Handler signature: `(objectId: string, params: any) => any`
241
+ *
242
+ * 2. **Static methods** — set `className` + `static: true`.
243
+ * In the sandbox: `Author.search({ query: 'tolkien' })`
244
+ * Handler signature: `(params: any) => any`
245
+ *
246
+ * 3. **Global tools** — omit `className`.
247
+ * In the sandbox: `global_search({ query: 'rings' })`
248
+ * Handler signature: `(params: any) => any`
249
+ *
250
+ * Both `inputSchema` and `outputSchema` accept JSON Schema objects.
251
+ * The `outputSchema` drives the return type in the auto-generated
252
+ * TypeScript declarations that sandbox code imports from `./sandbox-tools`.
253
+ */
254
+ interface ToolSchema {
255
+ name: string;
256
+ description: string;
257
+ /** JSON Schema for the tool's input parameters */
258
+ inputSchema: Record<string, unknown>;
259
+ /**
260
+ * JSON Schema for the tool's return value.
261
+ * Used to generate typed return types in the sandbox TypeScript declarations.
262
+ *
263
+ * @example
264
+ * ```typescript
265
+ * outputSchema: {
266
+ * type: 'object',
267
+ * properties: {
268
+ * bio: { type: 'string', description: 'The biography text' },
269
+ * source: { type: 'string', description: 'Source of the bio' },
270
+ * },
271
+ * required: ['bio'],
272
+ * }
273
+ * // Generates: Promise<{ bio: string; source?: string }>
274
+ * ```
275
+ */
276
+ outputSchema?: Record<string, unknown>;
277
+ stability?: 'stable' | 'experimental' | 'deprecated';
278
+ provenance?: {
279
+ source: 'mcp' | 'custom';
280
+ };
281
+ tags?: string[];
282
+ /**
283
+ * The class this tool belongs to (e.g., `'author'`, `'book'`).
284
+ * When set, the tool becomes a method on the auto-generated class.
285
+ * Omit for global tools (standalone exported functions).
286
+ */
287
+ className?: string;
288
+ /**
289
+ * If `true`, this is a static/class-level method (no object ID required).
290
+ * If `false` or omitted and `className` is set, this is an instance method
291
+ * that operates on a specific object (the object's real-world ID is
292
+ * passed as the first argument to the handler).
293
+ */
294
+ static?: boolean;
295
+ }
296
+ /**
297
+ * Tool with handler — what users provide to `publishTools()`.
298
+ *
299
+ * - **Instance methods** (`className` set, `static` omitted):
300
+ * handler receives `(objectId: string, params: any)`
301
+ * - **Static methods** (`className` set, `static: true`):
302
+ * handler receives `(params: any)`
303
+ * - **Global tools** (no `className`):
304
+ * handler receives `(params: any)`
305
+ */
306
+ interface ToolWithHandler extends ToolSchema {
307
+ handler: ToolHandler | InstanceToolHandler;
308
+ }
309
+ /**
310
+ * Result from publishing tools
311
+ */
312
+ interface PublishToolsResult {
313
+ accepted: boolean;
314
+ domainRevision: string;
315
+ rejected?: Array<{
316
+ name: string;
317
+ reason: string;
318
+ }>;
319
+ }
320
+ /**
321
+ * Domain state response
322
+ */
323
+ interface DomainState {
324
+ activeDomainRevision?: string;
325
+ tools?: Array<{
326
+ name: string;
327
+ description?: string;
328
+ inputSchema?: Record<string, unknown>;
329
+ outputSchema?: Record<string, unknown>;
330
+ }>;
331
+ [key: string]: unknown;
332
+ }
333
+ type JobStatus = 'queued' | 'running' | 'succeeded' | 'failed' | 'timeout' | 'canceled';
334
+ /**
335
+ * Result from submitting a job
336
+ */
337
+ interface JobSubmitResult {
338
+ jobId: string;
339
+ }
340
+ /**
341
+ * Represents a job executed in the sandbox
342
+ */
343
+ interface Job {
344
+ /** Unique Job ID */
345
+ id: string;
346
+ /** Current status of the job */
347
+ status: JobStatus;
348
+ /** Promise that resolves with the job result */
349
+ result: Promise<unknown>;
350
+ /** Subscribe to job events */
351
+ on(event: string, handler: (data: unknown) => void): void;
352
+ }
353
+ interface Prompt {
354
+ id: string;
355
+ type: 'confirm' | 'choice' | 'input';
356
+ title: string;
357
+ message: string;
358
+ options?: string[];
359
+ defaultValue?: unknown;
360
+ }
361
+ interface WSClientOptions {
362
+ url: string;
363
+ sessionId: string;
364
+ token: string;
365
+ }
366
+ interface RPCRequest {
367
+ type: 'rpc';
368
+ method: string;
369
+ params: unknown;
370
+ id: string;
371
+ }
372
+ interface RPCResponse {
373
+ type: 'rpc_result' | 'rpc_error';
374
+ id: string;
375
+ result?: unknown;
376
+ error?: {
377
+ code: number;
378
+ message: string;
379
+ data?: unknown;
380
+ };
381
+ }
382
+ interface SyncMessage {
383
+ type: 'sync';
384
+ message?: string | number[] | Uint8Array;
385
+ data?: number[];
386
+ }
387
+ interface RPCRequestFromServer {
388
+ type: 'rpc';
389
+ method: string;
390
+ params: unknown;
391
+ id: string;
392
+ }
393
+ interface ToolInvokeParams {
394
+ callId: string;
395
+ toolName: string;
396
+ input: unknown;
397
+ }
398
+ interface ToolResultParams {
399
+ callId: string;
400
+ result?: unknown;
401
+ error?: string | {
402
+ code: string;
403
+ message: string;
404
+ };
405
+ }
406
+ /**
407
+ * A model reference as returned from relationship queries
408
+ */
409
+ interface ModelRef {
410
+ path: string;
411
+ label?: string;
412
+ }
413
+ /**
414
+ * Relationship info as returned from the GraphQL API.
415
+ * Represents a typed, bidirectional relationship between two model types,
416
+ * seen from one model's perspective.
417
+ */
418
+ interface RelationshipInfo {
419
+ /** Unique name of this relationship definition */
420
+ name: string;
421
+ /** The submodel on this model that holds the relationship */
422
+ local_submodel: ModelRef;
423
+ /** Whether this side is a "many" collection */
424
+ local_is_many: boolean;
425
+ /** The submodel on the foreign model */
426
+ foreign_submodel: ModelRef;
427
+ /** Whether the foreign side is a "many" collection */
428
+ foreign_is_many: boolean;
429
+ /** The foreign model type */
430
+ foreign_model: ModelRef;
431
+ /** Computed relationship kind: "one_to_one" | "one_to_many" | "many_to_one" | "many_to_many" */
432
+ relationship_kind: 'one_to_one' | 'one_to_many' | 'many_to_one' | 'many_to_many';
433
+ }
434
+ /**
435
+ * Options for defining a relationship between two model types
436
+ */
437
+ interface DefineRelationshipOptions {
438
+ /** The model to define the relationship on (the "left" / "local" type) */
439
+ model: string;
440
+ /** The submodel name on the local model (e.g., "books") */
441
+ localSubmodel: string;
442
+ /** Whether the local side is "many" */
443
+ localIsMany: boolean;
444
+ /** The foreign model type (e.g., "book") */
445
+ foreignModel: string;
446
+ /** The submodel name on the foreign model (e.g., "author") */
447
+ foreignSubmodel: string;
448
+ /** Whether the foreign side is "many" */
449
+ foreignIsMany: boolean;
450
+ /** Optional relationship name (auto-generated if omitted) */
451
+ name?: string;
452
+ }
453
+ /**
454
+ * Options for creating or updating a class instance in the graph.
455
+ *
456
+ * `recordObject` uses the graph's `instantiate` (find-or-create) semantics:
457
+ * if an instance with the given `id` already exists under the class, its
458
+ * fields are updated in place; otherwise a new instance is created.
459
+ */
460
+ interface RecordObjectOptions {
461
+ /** The class to instantiate (e.g., "author") */
462
+ className: string;
463
+ /**
464
+ * Real-world object ID. Unique within its class, but two objects of
465
+ * different classes may share the same ID. Internally the SDK derives
466
+ * a unique graph path as `{className}__{id}`.
467
+ */
468
+ id: string;
469
+ /** Optional display label (defaults to `id`) */
470
+ label?: string;
471
+ /** Scalar field values to set on the instance */
472
+ fields?: Record<string, string | number | boolean | null>;
473
+ /**
474
+ * Relationship attachments.
475
+ * Keys are relationship submodel names. Values are real-world IDs
476
+ * (not graph paths) — the SDK resolves them using the foreign class
477
+ * derived from the relationship definition.
478
+ * - For a "one" side: pass a single target ID (string)
479
+ * - For a "many" side: pass an array of target IDs
480
+ */
481
+ relationships?: Record<string, string | string[]>;
482
+ }
483
+ /**
484
+ * Return value from `recordObject()`
485
+ */
486
+ interface RecordObjectResult {
487
+ /** The internal graph path (e.g., "author__tolkien") */
488
+ path: string;
489
+ /** The real-world object ID as provided by the caller (e.g., "tolkien") */
490
+ id: string;
491
+ /** Whether the instance was newly created (false = updated) */
492
+ created: boolean;
493
+ }
494
+ /**
495
+ * Property specification in a manifest operation
496
+ */
497
+ interface ManifestPropertySpec {
498
+ value?: string | number | boolean;
499
+ ref?: string;
500
+ instanceOf?: string;
501
+ create?: string;
502
+ has?: Record<string, ManifestPropertySpec>;
503
+ type?: string;
504
+ description?: string;
505
+ required?: boolean;
506
+ }
507
+ /**
508
+ * Relationship definition between two classes
509
+ */
510
+ interface ManifestRelationshipDef {
511
+ /** Optional name (auto-generated from left_right if omitted) */
512
+ name?: string;
513
+ /** Left model path */
514
+ left: string;
515
+ /** Right model path */
516
+ right: string;
517
+ /** Submodel name on the left model */
518
+ leftSubmodel: string;
519
+ /** Submodel name on the right model */
520
+ rightSubmodel: string;
521
+ /** Whether the left side is a collection */
522
+ leftIsMany: boolean;
523
+ /** Whether the right side is a collection */
524
+ rightIsMany: boolean;
525
+ }
526
+ /**
527
+ * A single operation in a manifest volume
528
+ */
529
+ interface ManifestOperation {
530
+ /** Create a new model/class */
531
+ create?: string;
532
+ /** Target an existing model for modification */
533
+ on?: string;
534
+ /** Extend from a parent class */
535
+ extends?: string;
536
+ /** Instantiate a type */
537
+ instanceOf?: string;
538
+ /** Define submodels/fields */
539
+ has?: Record<string, ManifestPropertySpec>;
540
+ /** Define a relationship between two classes */
541
+ defineRelationship?: ManifestRelationshipDef;
542
+ }
543
+ /**
544
+ * A volume in a manifest
545
+ */
546
+ /**
547
+ * Import descriptor for referencing modules
548
+ */
549
+ interface ManifestImport {
550
+ /** Alias prefix used in operations (e.g., "@std") */
551
+ alias: string;
552
+ /** Module name (e.g., "standard_modules") */
553
+ name: string;
554
+ /** Version label (e.g., "prod", "v1.2.3") */
555
+ label?: string;
556
+ }
557
+ interface ManifestVolume {
558
+ name: string;
559
+ scope: 'sandbox' | 'build' | 'user';
560
+ imports?: ManifestImport[];
561
+ operations: ManifestOperation[];
562
+ }
563
+ /**
564
+ * A manifest defines the structure of a sandbox's data model
565
+ */
566
+ interface ManifestContent {
567
+ schemaVersion: 2;
568
+ name: string;
569
+ description?: string;
570
+ volumes: ManifestVolume[];
571
+ }
572
+ /**
573
+ * Result from a GraphQL query execution
574
+ */
575
+ interface GraphQLResult<T = any> {
576
+ data?: T;
577
+ errors?: Array<{
578
+ message: string;
579
+ locations?: Array<{
580
+ line: number;
581
+ column: number;
582
+ }>;
583
+ path?: Array<string | number>;
584
+ extensions?: Record<string, any>;
585
+ }>;
586
+ }
587
+ interface APIError {
588
+ error: string;
589
+ message?: string;
590
+ }
591
+ interface DeleteResponse {
592
+ deleted: boolean;
593
+ }
594
+
595
+ export type { APIError as $, AssignmentListResponse as A, BuildPolicy as B, ConnectOptions as C, DomainState as D, EnvironmentData as E, Prompt as F, GranularOptions as G, RPCRequest as H, InstanceToolHandler as I, Job as J, RPCResponse as K, SyncMessage as L, ModelRef as M, RPCRequestFromServer as N, ToolInvokeParams as O, PublishToolsResult as P, ToolResultParams as Q, RecordUserOptions as R, SandboxListResponse as S, ToolWithHandler as T, User as U, ManifestPropertySpec as V, WSClientOptions as W, ManifestRelationshipDef as X, ManifestOperation as Y, ManifestImport as Z, ManifestVolume as _, ToolHandler as a, GraphQLResult as b, DefineRelationshipOptions as c, RelationshipInfo as d, ManifestContent as e, RecordObjectOptions as f, RecordObjectResult as g, Sandbox as h, CreateSandboxData as i, DeleteResponse as j, PermissionProfile as k, CreatePermissionProfileData as l, CreateEnvironmentData as m, Subject as n, ToolSchema as o, PermissionRules as p, PermissionProfileListResponse as q, Assignment as r, EnvironmentListResponse as s, Manifest as t, ManifestListResponse as u, BuildStatus as v, Build as w, BuildListResponse as x, JobStatus as y, JobSubmitResult as z };