@squidcloud/cli 1.0.145-beta → 1.0.146-beta

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,2340 @@
1
+ ---
2
+ name: Squid Development
3
+ description: Provides comprehensive, code-verified knowledge about developing applications with Squid. Use this skill proactively before writing, editing, or creating any code that uses Squid's Client SDK, Backend SDK, or CLI to ensure compliance with Squid's APIs and best practices.
4
+ ---
5
+
6
+ # Squid Development Skill (Verified)
7
+
8
+ This skill provides comprehensive, code-verified knowledge about developing applications with Squid.
9
+
10
+ ## Overview
11
+
12
+ Squid is a backend-as-a-service platform that provides:
13
+ - **Client SDK** (`@squidcloud/client`) - TypeScript/JavaScript SDK for frontend
14
+ - **Backend SDK** (`@squidcloud/backend`) - TypeScript SDK with decorators for backend
15
+ - **CLI** (`@squidcloud/cli`) - Local development and deployment tools
16
+ - **Built-in integrations** - Databases, queues, storage, AI, APIs
17
+
18
+ ## Squid Architecture: The Three Parts
19
+
20
+ When developing with Squid, you work with three distinct but interconnected parts:
21
+
22
+ ### 1. Client (Frontend/External Services)
23
+
24
+ **What it is:** Any application that needs to interact with your Squid backend - web apps, mobile apps, desktop apps, or even other servers.
25
+
26
+ **How it works:**
27
+ - Initializes the Squid Client SDK (`@squidcloud/client`)
28
+ - Communicates with Squid functionality over HTTP/WebSocket
29
+ - Can interact with AI agents, modify agent settings, query databases, call backend functions, etc.
30
+ - Optionally uses authentication (OAuth2.0) for user-specific access
31
+ - Respects security rules defined in the backend
32
+
33
+ **Common use cases:**
34
+ ```typescript
35
+ // Web/mobile application
36
+ const squid = new Squid({
37
+ appId: 'your-app-id',
38
+ region: 'us-east-1.aws',
39
+ authProvider: { /* user auth */ }
40
+ });
41
+
42
+ // Interact with agents
43
+ const agent = squid.ai().agent('support-agent');
44
+ const response = await agent.ask('How do I reset my password?');
45
+
46
+ // Query databases
47
+ const users = await squid.collection('users').query().eq('active', true).snapshot();
48
+
49
+ // Call backend functions
50
+ const result = await squid.executeFunction('processPayment', paymentData);
51
+ ```
52
+
53
+ **Key point:** Clients have limited, controlled access based on security rules you define in the backend.
54
+
55
+ ### 2. Squid Backend (Your Server-Side Code)
56
+
57
+ **What it is:** TypeScript code that runs on Squid's infrastructure, containing your business logic, security rules, and integrations.
58
+
59
+ **How it works:**
60
+ - Contains services that extend `SquidService`
61
+ - Uses **decorators** to define functionality:
62
+ - `@executable()` - Backend functions callable from clients
63
+ - `@webhook()` - HTTP endpoints for external services
64
+ - `@trigger()` - React to database changes
65
+ - `@scheduler()` - Run code on a schedule (cron jobs)
66
+ - `@secureDatabase()`, `@secureCollection()` - Define who can access data
67
+ - `@secureAiAgent()` - Control agent access
68
+ - `@aiFunction()` - Functions that AI agents can call
69
+ - And many more decorators for security, rate limiting, etc.
70
+ - Has **full access** to all Squid functionality via pre-initialized `this.squid` client (with API key permissions)
71
+ - Deployed to Squid infrastructure using `squid deploy`
72
+
73
+ **Example backend service:**
74
+ ```typescript
75
+ import { SquidService, executable, scheduler, trigger, secureCollection } from '@squidcloud/backend';
76
+
77
+ export class MyService extends SquidService {
78
+ // Backend function callable from client
79
+ @executable()
80
+ async processPayment(amount: number, userId: string): Promise<PaymentResult> {
81
+ // Full access to Squid - this.squid has API key permissions
82
+ const user = await this.squid.collection('users').doc({ id: userId }).snapshot();
83
+
84
+ // Your business logic here
85
+ return { success: true, transactionId: '...' };
86
+ }
87
+
88
+ // React to database changes
89
+ @trigger({ id: 'new-user', collection: 'users', mutationTypes: ['insert'] })
90
+ async onNewUser(request: TriggerRequest<User>): Promise<void> {
91
+ await sendWelcomeEmail(request.docAfter);
92
+ }
93
+
94
+ // Scheduled task (cron job)
95
+ @scheduler({ id: 'daily-report', cron: '0 0 * * *' })
96
+ async generateDailyReport(): Promise<void> {
97
+ // Runs every day at midnight
98
+ const stats = await this.calculateStats();
99
+ await this.sendReport(stats);
100
+ }
101
+
102
+ // Security rule - who can read users collection
103
+ @secureCollection('users', 'read')
104
+ allowUserRead(context: QueryContext): boolean {
105
+ const userId = this.getUserAuth()?.userId;
106
+ // Only allow users to read their own data
107
+ return context.isSubqueryOf('id', '==', userId);
108
+ }
109
+
110
+ // Function that AI agents can call
111
+ @aiFunction('Gets order status', [
112
+ { name: 'orderId', type: 'string', required: true }
113
+ ])
114
+ async getOrderStatus({ orderId }: { orderId: string }): Promise<string> {
115
+ const order = await this.squid.collection('orders').doc({ id: orderId }).snapshot();
116
+ return order?.status || 'not found';
117
+ }
118
+ }
119
+ ```
120
+
121
+ **Key points:**
122
+ - Backend code has **full permissions** (API key access)
123
+ - Defines **what clients can and cannot do** through security decorators
124
+ - Contains **business logic** that shouldn't be exposed to clients
125
+ - Automatically deployed and scaled by Squid
126
+
127
+ ### 3. Squid Console (Web UI)
128
+
129
+ **What it is:** A web-based management interface at https://console.getsquid.ai/ for setting up and managing your Squid applications.
130
+
131
+ **What you can do in the Console:**
132
+ - **Organizations & Applications:**
133
+ - Create organizations (teams/workspaces)
134
+ - Create applications (projects)
135
+ - Manage API keys and secrets
136
+ - View application logs and metrics
137
+
138
+ - **AI Studio:**
139
+ - Create and configure AI agents visually
140
+ - Set up agent instructions, models, and guardrails
141
+ - Test agents in real-time
142
+ - Connect agents to functions and knowledge bases
143
+
144
+ - **Knowledge Bases:**
145
+ - Create knowledge bases for RAG (Retrieval Augmented Generation)
146
+ - Upload documents and manage contexts
147
+ - Configure embedding models
148
+
149
+ - **Integrations:**
150
+ - Connect databases (PostgreSQL, MySQL, MongoDB, etc.)
151
+ - Connect SaaS services (Stripe, Slack, etc.)
152
+ - Configure OAuth integrations
153
+ - Set up storage providers (AWS S3, etc.)
154
+ - Configure message queues (Kafka, etc.)
155
+
156
+ - **Monitoring & Debugging:**
157
+ - View real-time logs
158
+ - Monitor API usage
159
+ - Debug agent conversations
160
+ - Track performance metrics
161
+
162
+ **Console vs SDK:**
163
+ - **Console is great for:** Initial setup, visual agent configuration, quick testing, monitoring
164
+ - **SDK is more powerful for:** Programmatic control, CI/CD pipelines, dynamic configuration, complex automation
165
+
166
+ **Example: Everything in Console can be done via SDK (except creating orgs/apps):**
167
+
168
+ ```typescript
169
+ // Console: Click to create an agent with AI Studio
170
+ // SDK equivalent:
171
+ const agent = squid.ai().agent('my-agent');
172
+ await agent.upsert({
173
+ description: 'Customer support agent',
174
+ options: {
175
+ model: 'gpt-4o',
176
+ instructions: 'You are a helpful support agent...'
177
+ }
178
+ });
179
+
180
+ // Console: Upload document to knowledge base
181
+ // SDK equivalent:
182
+ const kb = squid.ai().knowledgeBase('docs');
183
+ await kb.upsertContext({
184
+ contextId: 'doc-1',
185
+ text: 'Product documentation...'
186
+ }, pdfFile);
187
+
188
+ // Console: Configure database integration
189
+ // SDK equivalent:
190
+ const integrations = squid.admin().integrations();
191
+ await integrations.upsertIntegration({
192
+ integrationId: 'my-db',
193
+ type: 'postgres',
194
+ connectionString: 'postgresql://...'
195
+ });
196
+ ```
197
+
198
+ **Key point:** The Console provides a visual interface for management, but everything (except org/app creation) can be automated through the SDK for more flexibility and control.
199
+
200
+ ### How They Work Together
201
+
202
+ ```
203
+ ┌─────────────────────┐
204
+ │ CLIENT (Web/ │
205
+ │ Mobile/Server) │
206
+ │ │
207
+ │ - Squid SDK init │
208
+ │ - User auth │
209
+ │ - Limited access │
210
+ └──────────┬──────────┘
211
+
212
+ │ HTTP/WebSocket
213
+
214
+
215
+ ┌─────────────────────┐
216
+ │ SQUID BACKEND │
217
+ │ (Your Code) │
218
+ │ │
219
+ │ - SquidService │
220
+ │ - Decorators │
221
+ │ - Full permissions │
222
+ │ - Business logic │
223
+ │ - Security rules │
224
+ └──────────┬──────────┘
225
+
226
+ │ Deployed via CLI
227
+ │ or managed via
228
+
229
+ ┌─────────────────────┐
230
+ │ SQUID CONSOLE │
231
+ │ (Web UI) │
232
+ │ │
233
+ │ - Manage apps │
234
+ │ - AI Studio │
235
+ │ - Integrations │
236
+ │ - Monitoring │
237
+ └─────────────────────┘
238
+ ```
239
+
240
+ **Development workflow:**
241
+ 1. Create application in **Squid Console**
242
+ 2. Develop backend services in **Squid Backend** (local development with `squid start`)
243
+ 3. Deploy backend to Squid infrastructure (`squid deploy`)
244
+ 4. Build **Client** applications that use the Squid Client SDK
245
+ 5. Use **Console** for monitoring and management, or use **SDK** for programmatic control
246
+
247
+ ## CLI Commands
248
+
249
+ ### Installation
250
+ ```bash
251
+ npm install -g @squidcloud/cli
252
+ ```
253
+
254
+ ### `squid init <project-name>`
255
+ Creates new backend project with `.env` and example service.
256
+ The backend project has access to an already initialized client SDK with API key (full permissions)
257
+
258
+ ```bash
259
+ squid init backend --appId YOUR_APP_ID --apiKey YOUR_API_KEY --environmentId dev --squidDeveloperId YOUR_DEV_ID --region us-east-1.aws
260
+ ```
261
+
262
+ ### `squid start`
263
+ Runs backend locally with hot-reload, connects to Squid Cloud via reverse proxy.
264
+
265
+ ```bash
266
+ cd backend
267
+ squid start
268
+ ```
269
+
270
+ **Extended logging in `.env`:**
271
+ ```env
272
+ SQUID_LOG_TYPES=QUERY,MUTATION,AI,API,ERROR
273
+ ```
274
+
275
+ ### `squid deploy`
276
+ Builds and deploys to Squid Cloud.
277
+
278
+ ```bash
279
+ squid deploy [--apiKey KEY] [--environmentId prod] [--skipBuild]
280
+ ```
281
+
282
+ ### `squid build`
283
+ Builds backend project.
284
+
285
+ ```bash
286
+ squid build [--dev] [--skip-version-check]
287
+ ```
288
+
289
+ ## Client SDK
290
+
291
+ ### Where Can You Use the SDK?
292
+
293
+ The Squid Client SDK can be instantiated in various environments:
294
+ - **Web applications** (vanilla JavaScript, React, Vue, Angular, etc.)
295
+ - **Node.js servers** (Express, Fastify, etc.)
296
+ - **Mobile applications** (React Native, etc.)
297
+ - **Desktop applications** (Electron, etc.)
298
+
299
+ **Important:** When writing backend code that runs on Squid's infrastructure, the Squid client is **already initialized and available with API key and full permissions** for you (see Backend SDK section). You don't need to manually create a new instance.
300
+
301
+ ### Initialization
302
+
303
+ #### Configuration Options
304
+
305
+ **Required Parameters:**
306
+ - **`appId`** (string): Your Squid application identifier. Can be found in the Squid Console.
307
+ - **`region`** (string): The region where your Squid application is deployed:
308
+ - AWS regions: `'us-east-1.aws'`, `'ap-south-1.aws'`, `'eu-central-1.aws'`
309
+ - GCP regions: `'us-central1.gcp'`
310
+
311
+ **Optional Parameters:**
312
+ - **`apiKey`** (string): API key that bypasses security rules and provides full administrative access.
313
+ - **IMPORTANT**: Has **complete control over your application** and bypasses all security rules
314
+ - Only use in trusted environments (backend servers, admin tools, development)
315
+ - **Never expose in client-side code** (web browsers, mobile apps)
316
+ - **`authProvider`** (object): Authentication provider that supplies OAuth2.0 tokens for the current user
317
+ - `integrationId` (string): The ID of your auth integration
318
+ - `getToken` (function): Returns a promise that resolves to the user's auth token (or undefined if not authenticated)
319
+ - Authentication details are **available in backend functions** via the execution context
320
+ - **`environmentId`** (string): Environment identifier (dev, staging, prod). Defaults to production.
321
+ - **`squidDeveloperId`** (string): Your developer identifier, used for local development and debugging.
322
+ - **`consoleRegion`** (string): Console region (optional)
323
+
324
+ #### Basic Setup
325
+
326
+ ```typescript
327
+ import { Squid } from '@squidcloud/client';
328
+
329
+ const squid = new Squid({
330
+ appId: 'your-app-id',
331
+ region: 'us-east-1.aws',
332
+ environmentId: 'dev' // optional
333
+ });
334
+ ```
335
+
336
+ #### Authentication: API Key vs Auth Provider
337
+
338
+ **Using API Key (Server-Side Only):**
339
+ ```typescript
340
+ const squid = new Squid({
341
+ appId: 'your-app-id',
342
+ region: 'us-east-1.aws',
343
+ apiKey: 'your-api-key' // Full admin access
344
+ });
345
+ ```
346
+ - **Use cases**: Backend servers, admin tools, scripts, development environments
347
+ - **Security**: Has full control - bypasses all security rules
348
+ - **Warning**: Never use in client-side code
349
+
350
+ **Using Auth Provider (Client-Side):**
351
+ ```typescript
352
+ const squid = new Squid({
353
+ appId: 'your-app-id',
354
+ region: 'us-east-1.aws',
355
+ authProvider: {
356
+ integrationId: 'auth-integration-id',
357
+ getToken: async () => {
358
+ // Return user's OAuth token
359
+ return await getCurrentUserToken();
360
+ }
361
+ }
362
+ });
363
+ ```
364
+ - **Use cases**: Web apps, mobile apps, any user-facing application
365
+ - **Security**: Respects security rules based on authenticated user
366
+ - **Backend access**: User authentication details are available in backend functions through the execution context
367
+
368
+ **Setting Auth Provider After Initialization:**
369
+ ```typescript
370
+ const squid = new Squid({
371
+ appId: 'your-app-id',
372
+ region: 'us-east-1.aws'
373
+ });
374
+
375
+ // Later, when user logs in
376
+ squid.setAuthProvider({
377
+ integrationId: 'auth-integration-id',
378
+ getToken: async () => userAuthToken
379
+ });
380
+ ```
381
+
382
+ ### Database & Data Management
383
+
384
+ Squid provides database functionality similar to Firestore but more powerful, with collections, document references, and real-time capabilities. Squid supports multiple database types including NoSQL databases (like MongoDB) and relational databases (like PostgreSQL, MySQL).
385
+
386
+ #### Security Rules
387
+
388
+ Every database operation in Squid can be secured using **security rules**. Security rules are backend functions decorated with `@secureDatabase` or `@secureCollection` that contain business logic to determine whether an operation is allowed.
389
+
390
+ ```typescript
391
+ // Example security rule for a collection (read operations)
392
+ @secureCollection('users', 'read')
393
+ async canReadUsers(context: QueryContext<User>): Promise<boolean> {
394
+ // Your business logic here
395
+ // context.collectionName, context.integrationId, context.limit, etc.
396
+ return true; // or false based on your logic
397
+ }
398
+
399
+ // Example security rule for mutations (insert, update, delete)
400
+ @secureCollection('users', 'update')
401
+ async canUpdateUsers(context: MutationContext): Promise<boolean> {
402
+ // Access document before and after the mutation
403
+ const before = context.before;
404
+ const after = context.after;
405
+
406
+ // Check if specific paths were affected
407
+ if (context.affectsPath('email')) {
408
+ return false; // Don't allow email changes
409
+ }
410
+
411
+ return true;
412
+ }
413
+
414
+ // Database-wide security rule
415
+ @secureDatabase('insert', 'my-integration-id')
416
+ async canInsertAnywhere(context: MutationContext): Promise<boolean> {
417
+ // Your business logic here
418
+ return context.after?.createdBy === 'admin';
419
+ }
420
+ ```
421
+
422
+ These decorated functions return a boolean (or `Promise<boolean>`) indicating whether the operation is allowed. The developer has full control to implement any business logic needed for authorization.
423
+
424
+ **Context types:**
425
+ - `QueryContext` - Used for 'read' and 'all' operations (exported from `@squidcloud/backend`)
426
+ - `MutationContext` - Used for 'insert', 'update', 'delete' operations (exported from `@squidcloud/backend`)
427
+
428
+ Security decorators (`@secureDatabase`, `@secureCollection`) are exported from `@squidcloud/backend`.
429
+
430
+ #### Collection Access
431
+
432
+ A collection represents a set of documents (similar to a table in relational databases or a collection in NoSQL databases).
433
+
434
+ ```typescript
435
+ // Get collection reference in the built-in database
436
+ const users = squid.collection<User>('users');
437
+
438
+ // Get collection reference in a specific integration
439
+ const orders = squid.collection<Order>('orders', 'postgres-db');
440
+ ```
441
+
442
+ **Type parameter**: The generic type `<T>` defines the structure of documents in the collection.
443
+
444
+ **Document References - The `doc()` method has different behaviors:**
445
+
446
+ **1. No parameters - `doc()`**
447
+ Generates a new document ID (useful for creating new documents):
448
+ ```typescript
449
+ // For built_in_db without schema - generates a random ID
450
+ const newDocRef = collection.doc();
451
+ await newDocRef.insert({ name: 'John', age: 30 });
452
+
453
+ // For other integrations - creates a placeholder that gets resolved on insert
454
+ const newDocRef = collection.doc();
455
+ ```
456
+
457
+ **2. String parameter - `doc(stringId)`**
458
+ **Only supported for `built_in_db` integration without a defined schema:**
459
+ ```typescript
460
+ // Valid only for built_in_db collections without schema
461
+ const docRef = collection.doc('my-doc-id');
462
+ const docRef = collection.doc('user-12345');
463
+ ```
464
+ **Important**: For collections with defined schemas or non-built_in_db integrations, you must use object format.
465
+
466
+ **3. Object parameter - `doc({id: value})`**
467
+ Used for collections with defined primary keys. The object maps primary key field names to their values:
468
+ ```typescript
469
+ // Single primary key field "id"
470
+ const docRef = collection.doc({ id: 'user-123' });
471
+
472
+ // Single primary key field "userId"
473
+ const docRef = collection.doc({ userId: 42 });
474
+
475
+ // Composite primary key (id1, id2)
476
+ const docRef = collection.doc({ id1: 'part1', id2: 'part2' });
477
+
478
+ // Partial primary key - missing fields generated on server if supported
479
+ const docRef = collection.doc({ id1: 'part1' }); // id2 generated on insert
480
+ ```
481
+
482
+ **Key points about document IDs:**
483
+ - For `built_in_db` without schema: can use string IDs or object format
484
+ - For `built_in_db` with schema: must use object format matching primary key fields
485
+ - For other integrations: must use object format matching primary key fields
486
+ - Partial primary keys: missing fields may be auto-generated on insert (if integration supports it)
487
+ - Empty `doc()`: generates a placeholder ID that gets resolved when document is created
488
+
489
+ ```typescript
490
+ // Examples:
491
+
492
+ // Insert
493
+ await userDoc.insert({ name: 'John', email: 'john@example.com' });
494
+
495
+ // Update (partial)
496
+ await userDoc.update({ name: 'Jane' });
497
+
498
+ // Update specific path
499
+ await userDoc.setInPath('address.street', 'Main St');
500
+
501
+ // Delete specific path
502
+ await userDoc.deleteInPath('tempData');
503
+
504
+ // Delete document
505
+ await userDoc.delete();
506
+
507
+ // Get data (promise)
508
+ const userData = await userDoc.snapshot();
509
+ console.log(userData);
510
+
511
+ // Get data (observable - realtime)
512
+ userDoc.snapshots().subscribe(data => {
513
+ console.log(data);
514
+ });
515
+
516
+ // Get cached data (no server fetch)
517
+ const cached = userDoc.peek();
518
+
519
+ // Check if document has data populated
520
+ if (userDoc.hasData) {
521
+ console.log('Document is loaded');
522
+ // Access data directly (with defensive copy)
523
+ console.log(userDoc.data);
524
+ }
525
+
526
+ // Bulk operations
527
+ await users.insertMany([
528
+ { id: 'user-1', data: { name: 'Alice' } },
529
+ { id: 'user-2', data: { name: 'Bob' } }
530
+ ]);
531
+
532
+ await users.deleteMany(['user-1', 'user-2']);
533
+ ```
534
+
535
+ #### Real-time Subscriptions
536
+
537
+ Squid provides real-time data synchronization similar to Firestore. Subscribe to document or query changes and receive updates automatically.
538
+
539
+ **Document Subscriptions:**
540
+ ```typescript
541
+ // Subscribe to document changes
542
+ const subscription = docRef.snapshots().subscribe((userData) => {
543
+ if (userData) {
544
+ console.log('Document updated:', userData);
545
+ } else {
546
+ console.log('Document deleted or does not exist');
547
+ }
548
+ });
549
+
550
+ // Unsubscribe when done
551
+ subscription.unsubscribe();
552
+ ```
553
+
554
+ **Query Subscriptions:**
555
+ ```typescript
556
+ // Subscribe to query results - ALWAYS use dereference()
557
+ const subscription = collection
558
+ .query()
559
+ .eq('status', 'active')
560
+ .gte('age', 18)
561
+ .dereference() // Important: Converts DocumentReferences to actual data
562
+ .snapshots()
563
+ .subscribe((users) => {
564
+ console.log('Active users updated:', users);
565
+ // users is Array<User> with actual data
566
+ });
567
+
568
+ // Unsubscribe when done
569
+ subscription.unsubscribe();
570
+ ```
571
+
572
+ **Real-time Updates:**
573
+ When data changes on the server (from any client or backend operation):
574
+ - Document subscriptions receive the updated document data
575
+ - Query subscriptions receive the updated query results
576
+ - Updates are pushed to clients via WebSocket connections
577
+ - Changes are automatically reflected in the local data
578
+
579
+ **Important**: Always unsubscribe from subscriptions when they're no longer needed to prevent memory leaks.
580
+
581
+ ### Database - Queries
582
+
583
+ **ALL Available Operators:**
584
+ - Comparison: `eq`, `neq`, `gt`, `gte`, `lt`, `lte`
585
+ - Arrays: `in`, `nin`, `arrayIncludesSome`, `arrayIncludesAll`, `arrayNotIncludes`
586
+ - Patterns: `like`, `notLike` (% = any chars, _ = one char)
587
+
588
+ ```typescript
589
+ // Basic query
590
+ const activeUsers = await users.query()
591
+ .eq('status', 'active')
592
+ .gt('age', 18)
593
+ .sortBy('name', true) // true = ascending
594
+ .limit(100) // max 20000, default 1000
595
+ .snapshot();
596
+
597
+ activeUsers.data.forEach(doc => {
598
+ console.log(doc.data); // Document data
599
+ });
600
+
601
+ // Realtime query
602
+ users.query()
603
+ .eq('status', 'active')
604
+ .snapshots().subscribe(snapshot => {
605
+ snapshot.data.forEach(doc => console.log(doc.data));
606
+ });
607
+
608
+ // Pattern matching (CASE-INSENSITIVE by default)
609
+ const results = await users.query()
610
+ .like('email', '%.com') // case-insensitive by default
611
+ .snapshot();
612
+
613
+ // Case-sensitive pattern matching
614
+ const caseSensitive = await users.query()
615
+ .like('name', 'John%', true) // third parameter: caseSensitive = true
616
+ .snapshot();
617
+
618
+ // Array operators
619
+ const tagged = await posts.query()
620
+ .in('category', ['tech', 'news'])
621
+ .arrayIncludesSome('tags', ['urgent', 'important'])
622
+ .snapshot();
623
+
624
+ // Using where() method (all operators above are shortcuts for where)
625
+ const results1 = await users.query().eq('status', 'active').snapshot();
626
+ const results2 = await users.query().where('status', '==', 'active').snapshot();
627
+ // These are equivalent
628
+
629
+ // OR queries - combine multiple queries with OR logic
630
+ const query1 = users.query().eq('status', 'active');
631
+ const query2 = users.query().eq('status', 'pending');
632
+ const orResults = await users.or(query1, query2)
633
+ .dereference()
634
+ .snapshot();
635
+ // Returns documents matching either query
636
+ // Note: Results are merged and deduplicated
637
+
638
+ // Multiple sort
639
+ const sorted = await users.query()
640
+ .sortBy('lastName', true)
641
+ .sortBy('firstName', true)
642
+ .limit(50)
643
+ .snapshot();
644
+
645
+ // Join Queries - combine data from multiple collections
646
+ // Start with joinQuery() and alias
647
+ const results = await teachers
648
+ .joinQuery('teacher') // Alias for teachers collection
649
+ .join(
650
+ students.query(),
651
+ 'students', // Alias for students collection
652
+ { left: 'id', right: 'teacherId' } // Join condition
653
+ )
654
+ .dereference() // Important: converts refs to actual data
655
+ .snapshot();
656
+ // Results: Array<{ teacher: Teacher, students?: Student }>
657
+
658
+ // Inner join (only matching records)
659
+ const innerResults = await teachers
660
+ .joinQuery('teacher')
661
+ .join(
662
+ students.query(),
663
+ 'students',
664
+ { left: 'id', right: 'teacherId' },
665
+ { isInner: true } // Inner join option
666
+ )
667
+ .dereference()
668
+ .snapshot();
669
+
670
+ // Multi-level joins
671
+ const threeWay = await collection1
672
+ .joinQuery('a')
673
+ .join(collection2.query(), 'b', { left: 'id', right: 'parentId' })
674
+ .join(collection3.query(), 'c', { left: 'id', right: 'grandParentId' })
675
+ .dereference()
676
+ .snapshot();
677
+
678
+ // Grouped joins (nests one-to-many as arrays)
679
+ const grouped = await teachers
680
+ .joinQuery('teacher')
681
+ .join(students.query(), 'students', { left: 'id', right: 'teacherId' })
682
+ .grouped()
683
+ .dereference()
684
+ .snapshot();
685
+ // Results: Array<{ teacher: Teacher, students: Student[] }>
686
+
687
+ // Dereference - Converts DocumentReferences to actual data
688
+ // WITHOUT dereference: returns Array<DocumentReference<User>>
689
+ const refs = await users.query().eq('active', true).snapshot();
690
+ // refs.data[0].data to access actual data
691
+
692
+ // WITH dereference: returns Array<User> directly
693
+ const userData = await users.query()
694
+ .eq('active', true)
695
+ .dereference()
696
+ .snapshot();
697
+ // userData[0] is the actual user object
698
+
699
+ // ALWAYS use dereference() for:
700
+ // - Real-time subscriptions (makes working with data easier)
701
+ // - When you need document data directly
702
+ // DON'T use dereference() if:
703
+ // - You need DocumentReference methods like .update() or .delete()
704
+
705
+ // Pagination
706
+ const pagination = users.query()
707
+ .sortBy('createdAt', false)
708
+ .dereference()
709
+ .paginate({
710
+ pageSize: 50, // Number of items per page (default: 100)
711
+ subscribe: true // Subscribe to real-time updates (default: true)
712
+ });
713
+
714
+ const firstPage = await pagination.first(); // Jump to first page
715
+ const nextPage = await pagination.next(); // Go to next page
716
+ const prevPage = await pagination.prev(); // Go to previous page
717
+
718
+ // Check pagination state
719
+ console.log(firstPage.hasNext); // boolean
720
+ console.log(firstPage.hasPrev); // boolean
721
+
722
+ // Watch changes
723
+ users.query()
724
+ .eq('status', 'active')
725
+ .changes()
726
+ .subscribe(changes => {
727
+ console.log('Inserts:', changes.inserts);
728
+ console.log('Updates:', changes.updates);
729
+ console.log('Deletes:', changes.deletes);
730
+ });
731
+ ```
732
+
733
+ **Note:** `offset()` does NOT exist - use `paginate()` for pagination.
734
+
735
+ ### Database - Transactions
736
+
737
+ ```typescript
738
+ await squid.runInTransaction(async (txId) => {
739
+ const userRef = squid.collection('users').doc('user-1');
740
+ const accountRef = squid.collection('accounts').doc('acc-1');
741
+
742
+ // Pass txId as last parameter to each operation
743
+ // Use incrementInPath/decrementInPath for numeric operations
744
+ await userRef.decrementInPath('balance', 100, txId);
745
+ await accountRef.incrementInPath('balance', 100, txId);
746
+
747
+ // Both commit together or rollback together
748
+ });
749
+ ```
750
+
751
+ ### Database - Numeric Operations
752
+
753
+ ```typescript
754
+ // Increment/decrement
755
+ await userDoc.incrementInPath('loginCount', 1);
756
+ await userDoc.decrementInPath('credits', 50);
757
+
758
+ // For arrays/objects, use update() with full new value
759
+ const currentData = await userDoc.snapshot();
760
+ await userDoc.update({
761
+ tags: [...currentData.tags, 'newTag'],
762
+ notifications: [...currentData.notifications, { msg: 'Hi' }]
763
+ });
764
+ ```
765
+
766
+ ### Database - Native Queries
767
+
768
+ ```typescript
769
+ // SQL (PostgreSQL, MySQL, etc.) - uses ${param} syntax
770
+ const users = await squid.executeNativeRelationalQuery<User[]>(
771
+ 'postgres-db',
772
+ 'SELECT * FROM users WHERE age > ${minAge}',
773
+ { minAge: 18 }
774
+ );
775
+
776
+ // MongoDB aggregation
777
+ const orders = await squid.executeNativeMongoQuery<Order[]>(
778
+ 'mongo-db',
779
+ 'orders',
780
+ [
781
+ { $match: { status: 'completed' } },
782
+ { $group: { _id: '$userId', total: { $sum: '$amount' } } }
783
+ ]
784
+ );
785
+
786
+ // Elasticsearch
787
+ const products = await squid.executeNativeElasticQuery(
788
+ 'elastic-db',
789
+ 'products',
790
+ { query: { match: { name: 'laptop' } } },
791
+ '_search', // endpoint (optional)
792
+ 'GET' // method (optional)
793
+ );
794
+
795
+ // Pure (Finos Legend Pure Language) - uses ${param} syntax
796
+ const items = await squid.executeNativePureQuery(
797
+ 'my-db',
798
+ 'from products->filter(p | $p.price < ${maxPrice})',
799
+ { maxPrice: 1000 }
800
+ );
801
+ ```
802
+
803
+ ### Database - Security (@secureDatabase, @secureCollection)
804
+
805
+ Secure your database operations with backend decorators. These are backend-only decorators that define who can access your data.
806
+
807
+ **Backend - Secure entire database:**
808
+ ```typescript
809
+ import { SquidService, secureDatabase, QueryContext, MutationContext } from '@squidcloud/backend';
810
+
811
+ export class MyService extends SquidService {
812
+ @secureDatabase('read')
813
+ allowRead(context: QueryContext<User>): boolean {
814
+ const userId = this.getUserAuth()?.userId;
815
+ if (!userId) return false;
816
+ return context.isSubqueryOf('userId', '==', userId);
817
+ }
818
+
819
+ @secureDatabase('write')
820
+ allowWrite(context: MutationContext<User>): boolean {
821
+ return this.isAuthenticated();
822
+ }
823
+
824
+ @secureDatabase('all')
825
+ allowAll(): boolean {
826
+ return this.isAuthenticated();
827
+ }
828
+ }
829
+ ```
830
+
831
+ **Types:** `'read'`, `'write'`, `'update'`, `'insert'`, `'delete'`, `'all'`
832
+
833
+ **Backend - Secure specific collection:**
834
+ ```typescript
835
+ // QueryContext methods (for 'read' operations)
836
+ @secureCollection('users', 'read')
837
+ allowUserRead(context: QueryContext): boolean {
838
+ const userId = this.getUserAuth()?.userId;
839
+ if (!userId) return false;
840
+ // Check if query filters on a specific field
841
+ return context.isSubqueryOf('id', '==', userId);
842
+ }
843
+
844
+ // MutationContext methods (for 'insert', 'update', 'delete' operations)
845
+ @secureCollection('users', 'update')
846
+ allowUserUpdate(context: MutationContext<User>): boolean {
847
+ const userId = this.getUserAuth()?.userId;
848
+ if (!userId) return false;
849
+
850
+ // context.before: document state before mutation
851
+ // context.after: document state after mutation
852
+
853
+ // Check if specific paths were affected
854
+ if (context.affectsPath('email')) {
855
+ return false; // Don't allow email changes
856
+ }
857
+
858
+ // Check ownership
859
+ return context.after?.id === userId;
860
+ }
861
+ ```
862
+
863
+ **Context types (exported from `@squidcloud/backend`):**
864
+ - `QueryContext<T>` - Used for 'read' and 'all' operations
865
+ - `isSubqueryOf(field, operator, value)` - Check if query filters on field
866
+ - `collectionName`, `integrationId`, `limit` properties
867
+ - `MutationContext<T>` - Used for 'insert', 'update', 'delete' operations
868
+ - `affectsPath(path)` - Check if specific field path was modified
869
+ - `before` - Document before mutation (undefined for insert)
870
+ - `after` - Document after mutation (undefined for delete)
871
+
872
+ ### Backend Functions (@executable)
873
+
874
+ Backend functions allow you to write custom server-side logic that can be called from the client.
875
+
876
+ **Backend - Define the function:**
877
+ ```typescript
878
+ import { SquidService, executable, SquidFile } from '@squidcloud/backend';
879
+
880
+ export class MyService extends SquidService {
881
+ @executable()
882
+ async greetUser(name: string): Promise<string> {
883
+ return `Hello, ${name}`;
884
+ }
885
+
886
+ @executable()
887
+ async uploadFile(file: SquidFile): Promise<Result> {
888
+ console.log(file.originalName, file.size, file.data);
889
+ return { success: true };
890
+ }
891
+ }
892
+ ```
893
+
894
+ **Client - Call the function:**
895
+ ```typescript
896
+ // Execute @executable() decorated function
897
+ const result = await squid.executeFunction('greetUser', 'John');
898
+ const typedResult = await squid.executeFunction<string>('greetUser', 'John');
899
+
900
+ // With headers
901
+ const result = await squid.executeFunctionWithHeaders(
902
+ 'processPayment',
903
+ { 'X-Custom-Header': 'value' },
904
+ paymentData
905
+ );
906
+ ```
907
+
908
+ ### Webhooks (@webhook)
909
+
910
+ Webhooks allow you to create publicly accessible HTTP endpoints that can receive data from external services.
911
+
912
+ **Backend - Define the webhook:**
913
+ ```typescript
914
+ import { SquidService, webhook, WebhookRequest } from '@squidcloud/backend';
915
+
916
+ export class MyService extends SquidService {
917
+ @webhook('github-events')
918
+ async handleGithub(request: WebhookRequest): Promise<any> {
919
+ console.log(request.body);
920
+ console.log(request.headers);
921
+ console.log(request.queryParams);
922
+ console.log(request.httpMethod); // 'post' | 'get' | 'put' | 'delete'
923
+ console.log(request.files);
924
+
925
+ return this.createWebhookResponse({ received: true }, 200);
926
+ }
927
+ }
928
+ ```
929
+
930
+ **Webhook URL:** `https://<appId>.<region>.squid.cloud/webhooks/<webhook-id>`
931
+
932
+ **Client - Call webhook programmatically (optional):**
933
+ ```typescript
934
+ // Usually webhooks are called by external services, but you can also call them from client
935
+ const result = await squid.executeWebhook<Response>('github-events', {
936
+ headers: { 'X-GitHub-Event': 'push' },
937
+ queryParams: { ref: 'main' },
938
+ body: { commits: [...] },
939
+ files: [file1, file2]
940
+ });
941
+ ```
942
+
943
+ ### AI - Supported Models
944
+
945
+ Squid supports multiple AI providers and models for different use cases:
946
+
947
+ **Chat Models** (for AI Agents, AI Query, etc.):
948
+
949
+ *OpenAI:*
950
+ - `o1`, `o3`, `o3-mini`, `o4-mini`
951
+ - `gpt-5`, `gpt-5-mini`, `gpt-5-nano`
952
+ - `gpt-4.1` (default), `gpt-4.1-mini`, `gpt-4.1-nano`
953
+ - `gpt-4o`, `gpt-4o-mini`
954
+
955
+ *Anthropic (Claude):*
956
+ - `claude-3-7-sonnet-latest`
957
+ - `claude-haiku-4-5-20251001`
958
+ - `claude-opus-4-20250514`, `claude-opus-4-1-20250805`, `claude-opus-4-5-20251101`
959
+ - `claude-sonnet-4-20250514`, `claude-sonnet-4-5-20250929`
960
+
961
+ *Google (Gemini):*
962
+ - `gemini-2.5-pro`, `gemini-2.5-flash`, `gemini-2.5-flash-lite`
963
+
964
+ *xAI (Grok):*
965
+ - `grok-3`, `grok-3-fast`, `grok-3-mini`, `grok-3-mini-fast`
966
+ - `grok-4`, `grok-4-fast-reasoning`, `grok-4-fast-non-reasoning`
967
+ - Note: `grok-3-mini` is ~10x less expensive than `grok-3`
968
+ - Note: `*-fast` models are ~2x more expensive and only marginally faster
969
+
970
+ **Embedding Models** (for Knowledge Bases):
971
+ - `text-embedding-3-small` (OpenAI, default) - 8,192 token limit
972
+ - `voyage-3-large` (Voyage) - 32,000 token limit
973
+
974
+ **Image Generation Models:**
975
+ - `dall-e-3` (OpenAI)
976
+ - `stable-diffusion-core` (Stability)
977
+ - `flux-pro-1.1`, `flux-kontext-pro` (Flux)
978
+
979
+ **Audio Models:**
980
+
981
+ *Transcription:*
982
+ - `whisper-1`
983
+ - `gpt-4o-transcribe`, `gpt-4o-mini-transcribe`
984
+
985
+ *Text-to-Speech:*
986
+ - `tts-1`, `tts-1-hd`, `gpt-4o-mini-tts`
987
+
988
+ ### AI - Agents
989
+
990
+ AI Agents are powerful, configurable AI assistants that can interact with your data, call functions, connect to integrations, and collaborate with other agents.
991
+
992
+ **An AI Agent can:**
993
+ - Maintain conversation history (**memory is enabled by default**)
994
+ - Call backend functions decorated with `@aiFunction`
995
+ - Query databases and APIs through connected integrations
996
+ - Search knowledge bases for relevant information
997
+ - Collaborate with other AI agents
998
+ - Process voice input and generate voice output
999
+ - Accept files as part of chat requests
1000
+
1001
+ **Creating and Managing Agents:**
1002
+
1003
+ ```typescript
1004
+ const aiClient = squid.ai();
1005
+
1006
+ // Get the built-in agent (no ID required)
1007
+ const builtInAgent = aiClient.agent();
1008
+
1009
+ // Get a custom agent by ID
1010
+ const myAgent = aiClient.agent('my-agent-id');
1011
+
1012
+ // Get an agent with an Agent API key (allows calling without App API key, bypasses @secureAiAgent methods)
1013
+ const agentWithKey = aiClient.agent('my-agent-id', {
1014
+ apiKey: 'your-agent-api-key'
1015
+ });
1016
+
1017
+ // Create or update an agent
1018
+ await myAgent.upsert({
1019
+ description: 'Customer support assistant',
1020
+ isPublic: false, // Whether the agent is publicly accessible
1021
+ auditLog: true, // Enable audit logging for compliance
1022
+ options: {
1023
+ model: 'gpt-4o', // or 'claude-sonnet-4-5-20250929', 'gemini-2.5-flash'
1024
+ instructions: 'You are a helpful customer support assistant. Be concise and professional.',
1025
+ temperature: 0.7
1026
+ }
1027
+ });
1028
+
1029
+ // Get agent details
1030
+ const agentInfo = await myAgent.get();
1031
+ console.log(agentInfo.id, agentInfo.description, agentInfo.options.model);
1032
+
1033
+ // Update specific properties
1034
+ await myAgent.updateInstructions('You are a technical support specialist.');
1035
+ await myAgent.updateModel('claude-sonnet-4-5-20250929');
1036
+ await myAgent.updateGuardrails(['no-harmful-content']);
1037
+
1038
+ // Delete an agent
1039
+ await myAgent.delete();
1040
+
1041
+ // List all agents
1042
+ const agents = await squid.ai().listAgents();
1043
+ agents.forEach(agent => console.log(`${agent.id}: ${agent.description}`));
1044
+
1045
+ // Manage agent API keys
1046
+ const apiKey = await myAgent.regenerateApiKey();
1047
+ console.log('New API key:', apiKey);
1048
+
1049
+ const existingKey = await myAgent.getApiKey();
1050
+ console.log('Current API key:', existingKey);
1051
+
1052
+ // Update agent options at specific path
1053
+ await myAgent.setAgentOptionInPath('temperature', 0.9);
1054
+ await myAgent.setAgentOptionInPath('options.maxTokens', 2000);
1055
+
1056
+ // Update connected agents
1057
+ await myAgent.updateConnectedAgents([
1058
+ { agentId: 'helper-agent', description: 'Helps with tasks' }
1059
+ ]);
1060
+
1061
+ // Update or delete custom guardrails
1062
+ await myAgent.updateCustomGuardrails('Never reveal sensitive information');
1063
+ await myAgent.deleteCustomGuardrail();
1064
+ ```
1065
+
1066
+ **Agent Lifecycle:**
1067
+ 1. **Creation** - Use `upsert()` to create a new agent with an ID
1068
+ 2. **Active** - Agent can process requests
1069
+ 3. **Update** - Use `upsert()` or specific update methods
1070
+ 4. **Deletion** - Use `delete()` to permanently remove the agent
1071
+
1072
+ **Important Notes:**
1073
+ - Agent IDs are permanent - once created, you cannot change the ID
1074
+ - Deleting an agent does not delete associated chat history
1075
+ - The built-in agent (accessed via `agent()` with no ID) cannot be deleted
1076
+ - **Agent API Keys**: You can pass an options object with an `apiKey` when calling `agent()`:
1077
+ - Allows calling agent methods without requiring an App API key
1078
+ - Bypasses any `@secureAiAgent` security methods when calling agent ask functions
1079
+ - Useful for direct agent access without app-level authentication
1080
+
1081
+ **Connecting Resources to Agents:**
1082
+
1083
+ Agents become powerful when connected to resources. You can connect agents to:
1084
+ - **Other AI agents** (`connectedAgents`) - Agent collaboration
1085
+ - **Backend functions** (`functions`) - Custom logic via `@aiFunction` decorators
1086
+ - **Integrations** (`connectedIntegrations`) - Database/API queries
1087
+ - **Knowledge bases** (`connectedKnowledgeBases`) - RAG (Retrieval Augmented Generation)
1088
+
1089
+ **1. Connected Agents (`connectedAgents`):**
1090
+
1091
+ Allow one agent to call another agent as a specialized sub-task handler.
1092
+
1093
+ ```typescript
1094
+ // Define connected agents in upsert or per-request
1095
+ const response = await agent.ask('Analyze our sales data and send report to team', {
1096
+ connectedAgents: [
1097
+ {
1098
+ agentId: 'data-analyst-agent',
1099
+ description: 'Analyzes sales data and generates reports'
1100
+ },
1101
+ {
1102
+ agentId: 'email-sender-agent',
1103
+ description: 'Sends emails to team members'
1104
+ }
1105
+ ]
1106
+ });
1107
+
1108
+ // The main agent can now call these sub-agents when needed
1109
+ // Agent orchestration happens automatically based on the prompt
1110
+ ```
1111
+
1112
+ **2. Functions (`functions`):**
1113
+
1114
+ Connect backend functions decorated with `@aiFunction` that the agent can call.
1115
+
1116
+ ```typescript
1117
+ // Backend function
1118
+ @aiFunction('Gets current inventory for a product', [
1119
+ { name: 'productId', type: 'string', required: true, description: 'Product ID' }
1120
+ ])
1121
+ async getInventory({ productId }: { productId: string }): Promise<number> {
1122
+ return await db.inventory.get(productId);
1123
+ }
1124
+
1125
+ // Client - Agent uses the function
1126
+ const response = await agent.ask('How many units of product-123 do we have?', {
1127
+ functions: ['getInventory'] // Function name or ID
1128
+ });
1129
+ // Agent automatically calls getInventory() and includes result in response
1130
+
1131
+ // With predefined parameters (hidden from AI)
1132
+ const response = await agent.ask('Send notification', {
1133
+ functions: [
1134
+ {
1135
+ functionId: 'sendEmail',
1136
+ context: { apiKey: 'secret-key' } // Passed to function but hidden from AI
1137
+ }
1138
+ ]
1139
+ });
1140
+ ```
1141
+
1142
+ **3. Connected Integrations (`connectedIntegrations`):**
1143
+
1144
+ Connect database or API integrations so the agent can query them directly.
1145
+
1146
+ ```typescript
1147
+ const response = await agent.ask('Show me active users from last week', {
1148
+ connectedIntegrations: [
1149
+ {
1150
+ integrationId: 'postgres-db',
1151
+ integrationType: 'database',
1152
+ description: 'Main application database with users, orders, and products',
1153
+ instructions: 'Only query the users table unless explicitly asked otherwise',
1154
+ options: {
1155
+ // Integration-specific options (varies by integration type)
1156
+ }
1157
+ },
1158
+ {
1159
+ integrationId: 'stripe-api',
1160
+ integrationType: 'api',
1161
+ description: 'Stripe payment API for retrieving payment information'
1162
+ }
1163
+ ]
1164
+ });
1165
+
1166
+ // Agent can now query the database and API to answer questions
1167
+ ```
1168
+
1169
+ **4. Connected Knowledge Bases (`connectedKnowledgeBases`):**
1170
+
1171
+ Connect knowledge bases for RAG (Retrieval Augmented Generation) - agent retrieves relevant context before answering.
1172
+
1173
+ ```typescript
1174
+ const response = await agent.ask('What is our return policy?', {
1175
+ connectedKnowledgeBases: [
1176
+ {
1177
+ knowledgeBaseId: 'company-policies-kb',
1178
+ description: 'Use this when asked about company policies, procedures, or guidelines'
1179
+ },
1180
+ {
1181
+ knowledgeBaseId: 'product-docs-kb',
1182
+ description: 'Use this when asked about product features, specifications, or usage'
1183
+ }
1184
+ ]
1185
+ });
1186
+
1187
+ // Agent searches knowledge bases for relevant information before answering
1188
+ ```
1189
+
1190
+ **Complete Agent Chat Options:**
1191
+
1192
+ ```typescript
1193
+ const agent = squid.ai().agent('my-agent');
1194
+
1195
+ // Chat (streaming) - Returns RxJS Observable
1196
+ // IMPORTANT: Streaming behavior:
1197
+ // - NO connected resources: streams token-by-token
1198
+ // - HAS connected resources: emits ONCE with complete response
1199
+ const chatObs = agent.chat('What is your return policy?', {
1200
+ // Memory management
1201
+ memoryOptions: {
1202
+ memoryMode: 'read-write', // 'none' | 'read-only' | 'read-write'
1203
+ memoryId: 'user-123', // Unique per user/session
1204
+ expirationMinutes: 1440 // 24 hours
1205
+ },
1206
+
1207
+ // Connected resources (can also be set on agent.upsert)
1208
+ connectedAgents: [{ agentId: 'specialist-agent', description: 'Handles X' }],
1209
+ functions: ['function1', 'function2'], // or with context: [{ functionId: 'fn', context: {...} }]
1210
+ connectedIntegrations: [{ integrationId: 'db', integrationType: 'database', description: '...' }],
1211
+ connectedKnowledgeBases: [{ knowledgeBaseId: 'kb1', description: 'When to use this KB' }],
1212
+
1213
+ // Model & generation
1214
+ model: 'gpt-4o', // Override agent's default model
1215
+ temperature: 0.7,
1216
+ maxTokens: 4000,
1217
+ maxOutputTokens: 2000,
1218
+ instructions: 'Additional instructions for this request only',
1219
+ responseFormat: 'json_object', // or 'text'
1220
+ verbosity: 'medium', // 'low' | 'medium' | 'high' (OpenAI only)
1221
+ reasoningEffort: 'high', // 'minimal' | 'low' | 'medium' | 'high' (for reasoning models)
1222
+
1223
+ // Context & RAG
1224
+ disableContext: false, // Disable all context
1225
+ enablePromptRewriteForRag: false, // Rewrite prompt for better RAG results
1226
+ includeReference: false, // Include source references in response
1227
+ contextMetadataFilterForKnowledgeBase: {
1228
+ 'kb-id': { category: 'documentation' } // Filter KB contexts by metadata
1229
+ },
1230
+
1231
+ // Guardrails & quotas
1232
+ guardrails: ['no-harmful-content', 'no-pii'], // Preset safety rules
1233
+ quotas: {
1234
+ maxAiCallStackSize: 5 // Max depth for nested agent calls
1235
+ },
1236
+
1237
+ // Files & voice
1238
+ fileUrls: [
1239
+ { id: 'file1', type: 'image', purpose: 'context', url: 'https://...', description: 'Product image' }
1240
+ ],
1241
+ voiceOptions: {
1242
+ modelName: 'tts-1',
1243
+ voice: 'alloy', // alloy, ash, ballad, coral, echo, fable, onyx, nova, sage, shimmer, verse
1244
+ speed: 1.0
1245
+ },
1246
+
1247
+ // Advanced
1248
+ smoothTyping: true, // Smooth typing effect (default: true)
1249
+ useCodeInterpreter: 'llm', // 'none' | 'llm' (OpenAI/Gemini only)
1250
+ executionPlanOptions: {
1251
+ enabled: true, // Agent plans which resources to use
1252
+ model: 'gpt-4o',
1253
+ reasoningEffort: 'high',
1254
+ allowClarificationQuestions: false // Allow agent to ask follow-up questions (default: false)
1255
+ },
1256
+ agentContext: { userId: '123', role: 'admin' }, // Global context passed to all functions
1257
+ includeMetadata: false // Include context metadata in response
1258
+ });
1259
+
1260
+ chatObs.subscribe(text => {
1261
+ console.log(text);
1262
+ });
1263
+
1264
+ // Ask (complete response - same options as chat)
1265
+ const response = await agent.ask('Question?', { /* same options */ });
1266
+
1267
+ // Ask with annotations (includes file references, citations)
1268
+ const result = await agent.askWithAnnotations('Question?', { /* same options as ask */ });
1269
+ console.log(result.responseString);
1270
+ console.log(result.annotations); // File references, sources, etc.
1271
+
1272
+ // Ask asynchronously (doesn't wait for response, uses job system)
1273
+ await agent.askAsync('Long running task', 'job-id-123', { /* same options */ });
1274
+ // Check job status later with squid.job().getJob('job-id-123')
1275
+
1276
+ // Voice responses
1277
+ const voiceResult = await agent.askWithVoiceResponse('Hello', {
1278
+ voiceOptions: { modelName: 'tts-1', voice: 'alloy', speed: 1.0 }
1279
+ });
1280
+ console.log(voiceResult.responseString);
1281
+ console.log(voiceResult.voiceResponseFile);
1282
+
1283
+ // Transcribe audio and chat
1284
+ const transcribeResult = await agent.transcribeAndChat(audioFile);
1285
+ console.log(transcribeResult.transcribedPrompt);
1286
+ transcribeResult.responseStream.subscribe(text => console.log(text));
1287
+
1288
+ // Transcribe audio and ask (non-streaming)
1289
+ const askResult = await agent.transcribeAndAsk(audioFile, { /* ask options */ });
1290
+ console.log(askResult.transcribedPrompt);
1291
+ console.log(askResult.response);
1292
+
1293
+ // Transcribe audio and get voice response
1294
+ const voiceResponse = await agent.transcribeAndAskWithVoiceResponse(audioFile, {
1295
+ voiceOptions: { modelName: 'tts-1', voice: 'alloy' }
1296
+ });
1297
+ console.log(voiceResponse.transcribedPrompt);
1298
+ console.log(voiceResponse.responseString);
1299
+ console.log(voiceResponse.voiceResponseFile);
1300
+
1301
+ // Get chat history
1302
+ const history = await agent.getChatHistory('memory-id-123');
1303
+
1304
+ // Semantic search across agent's knowledge
1305
+ const results = await agent.search({ prompt: 'product docs', limit: 10 });
1306
+
1307
+ // Provide feedback on responses
1308
+ await agent.provideFeedback('thumbs_up'); // or 'thumbs_down'
1309
+
1310
+ // Observe status updates (for long-running operations)
1311
+ agent.observeStatusUpdates().subscribe(status => {
1312
+ console.log(status.title, status.tags);
1313
+ });
1314
+ ```
1315
+
1316
+ **Agent Security (@secureAiAgent):**
1317
+
1318
+ Secure agent access on the backend:
1319
+
1320
+ **Backend:**
1321
+ ```typescript
1322
+ import { SquidService, secureAiAgent } from '@squidcloud/backend';
1323
+
1324
+ export class MyService extends SquidService {
1325
+ // Secure specific agent
1326
+ @secureAiAgent('customer-support-agent')
1327
+ allowCustomerAgent(): boolean {
1328
+ return this.isAuthenticated();
1329
+ }
1330
+
1331
+ // Secure all agents
1332
+ @secureAiAgent()
1333
+ allowAllAgents(): boolean {
1334
+ return this.isAuthenticated();
1335
+ }
1336
+ }
1337
+ ```
1338
+
1339
+ **Important Notes:**
1340
+ - Connected resources can be set on agent creation/update OR per-request
1341
+ - Per-request settings override agent-level settings
1342
+ - Agent automatically decides when to use connected resources based on the prompt
1343
+ - `executionPlanOptions` enables the agent to create a plan before using resources (improves accuracy)
1344
+ - `chatId` and `profileId` are deprecated - use `memoryOptions` instead
1345
+
1346
+ ### AI - Knowledge Bases
1347
+
1348
+ ```typescript
1349
+ const kb = squid.ai().knowledgeBase('product-docs');
1350
+
1351
+ // Get knowledge base details
1352
+ const kbDetails = await kb.getKnowledgeBase();
1353
+ console.log(kbDetails);
1354
+
1355
+ // Create or update knowledge base
1356
+ await kb.upsertKnowledgeBase({
1357
+ description: 'Product documentation knowledge base',
1358
+ // ...other options
1359
+ });
1360
+
1361
+ // Delete knowledge base
1362
+ await kb.delete();
1363
+
1364
+ // List all knowledge bases
1365
+ const allKBs = await squid.ai().listKnowledgeBases();
1366
+
1367
+ // Upsert context (with or without file)
1368
+ await kb.upsertContext({
1369
+ contextId: 'doc-123',
1370
+ text: 'Product documentation...',
1371
+ metadata: { category: 'user-guide', version: '2.0' }
1372
+ }, optionalFile);
1373
+
1374
+ // Batch upsert
1375
+ await kb.upsertContexts([
1376
+ { contextId: 'doc-1', text: '...' },
1377
+ { contextId: 'doc-2', text: '...' }
1378
+ ], optionalFiles);
1379
+
1380
+ // Search with prompt
1381
+ const results = await kb.searchContextsWithPrompt({
1382
+ prompt: 'How do I reset password?',
1383
+ limit: 5,
1384
+ metadata: { category: 'user-guide' }
1385
+ });
1386
+
1387
+ // Search by context ID
1388
+ const related = await kb.searchContextsWithContextId({
1389
+ contextId: 'doc-123',
1390
+ limit: 10
1391
+ });
1392
+
1393
+ // Semantic search (returns chunks)
1394
+ const searchResults = await kb.search({
1395
+ prompt: 'authentication',
1396
+ limit: 10
1397
+ });
1398
+
1399
+ // Get context
1400
+ const context = await kb.getContext('doc-123');
1401
+
1402
+ // List contexts
1403
+ const allContexts = await kb.listContexts(1000); // truncateTextAfter
1404
+ const contextIds = await kb.listContextIds();
1405
+
1406
+ // Download context
1407
+ const download = await kb.downloadContext('doc-123');
1408
+
1409
+ // Delete contexts
1410
+ await kb.deleteContext('doc-123');
1411
+ await kb.deleteContexts(['doc-1', 'doc-2']);
1412
+
1413
+ // List all knowledge bases
1414
+ const allKBs = await squid.ai().listKnowledgeBases();
1415
+ ```
1416
+
1417
+ ### AI - Files
1418
+
1419
+ Manage files stored with AI providers (OpenAI, etc.) for use with agents.
1420
+
1421
+ ```typescript
1422
+ const aiFiles = squid.ai().files('openai'); // provider: 'openai' | 'anthropic' | etc.
1423
+
1424
+ // Upload file to AI provider
1425
+ const fileId = await aiFiles.uploadFile({
1426
+ file: myFile, // Browser File object
1427
+ purpose: 'assistants' // Purpose for the file
1428
+ });
1429
+ console.log('Uploaded file ID:', fileId);
1430
+
1431
+ // Delete file from AI provider
1432
+ const deleted = await aiFiles.deleteFile(fileId);
1433
+ console.log('File deleted:', deleted);
1434
+ ```
1435
+
1436
+ ### AI - Image & Audio
1437
+
1438
+ ```typescript
1439
+ // Image generation
1440
+ const imageUrl = await squid.ai().image().generate(
1441
+ 'A futuristic city',
1442
+ { size: '1024x1024', quality: 'hd' }
1443
+ );
1444
+
1445
+ // Remove background
1446
+ const noBgBase64 = await squid.ai().image().removeBackground(imageFile);
1447
+
1448
+ // Transcribe audio
1449
+ const text = await squid.ai().audio().transcribe(audioFile, {
1450
+ language: 'en'
1451
+ });
1452
+
1453
+ // Text-to-speech
1454
+ const audioFile = await squid.ai().audio().createSpeech(
1455
+ 'Hello world',
1456
+ { modelName: 'tts-1', voice: 'alloy', speed: 1.0 }
1457
+ );
1458
+ ```
1459
+
1460
+ ### AI - Query & API Call
1461
+
1462
+ AI Query allows you to query your database using natural language prompts. Squid's AI converts your prompt into database queries and returns the results.
1463
+
1464
+ ```typescript
1465
+ // Natural language database query (basic)
1466
+ const result = await squid.ai().executeAiQuery(
1467
+ 'built_in_db',
1468
+ 'Show me all active users who registered last month'
1469
+ );
1470
+
1471
+ // With options
1472
+ const result = await squid.ai().executeAiQuery(
1473
+ 'built_in_db',
1474
+ 'Show me all active users who registered last month',
1475
+ {
1476
+ instructions: 'Only query the users collection',
1477
+ generateWalkthrough: true, // Get step-by-step explanation
1478
+ enableRawResults: true, // Include raw query results
1479
+ selectCollectionsOptions: {
1480
+ collectionsToUse: ['users'] // Limit to specific collections
1481
+ },
1482
+ generateQueryOptions: {
1483
+ maxErrorCorrections: 3 // Number of retry attempts
1484
+ },
1485
+ analyzeResultsOptions: {
1486
+ enableCodeInterpreter: true // Enable code execution for result analysis
1487
+ }
1488
+ }
1489
+ );
1490
+
1491
+ // AI-powered API call
1492
+ const apiResult = await squid.ai().executeAiApiCall(
1493
+ 'stripe-api',
1494
+ 'Create a $50 charge for customer cus_123',
1495
+ ['create-charge'], // allowed endpoints
1496
+ true // provide explanation
1497
+ );
1498
+ ```
1499
+
1500
+ ### AI - Document Extraction & PDF Creation
1501
+
1502
+ Extract structured data from documents and create PDFs programmatically.
1503
+
1504
+ ```typescript
1505
+ const extraction = squid.extraction();
1506
+
1507
+ // Extract data from document file
1508
+ const extractedData = await extraction.extractDataFromDocumentFile(
1509
+ myPdfFile, // File object
1510
+ {
1511
+ schema: {
1512
+ fields: [
1513
+ { name: 'invoiceNumber', type: 'string', description: 'Invoice number' },
1514
+ { name: 'total', type: 'number', description: 'Total amount' },
1515
+ { name: 'date', type: 'date', description: 'Invoice date' }
1516
+ ]
1517
+ }
1518
+ }
1519
+ );
1520
+ console.log(extractedData);
1521
+
1522
+ // Extract data from document URL
1523
+ const urlData = await extraction.extractDataFromDocumentUrl(
1524
+ 'https://example.com/document.pdf',
1525
+ {
1526
+ schema: {
1527
+ fields: [
1528
+ { name: 'title', type: 'string' },
1529
+ { name: 'author', type: 'string' }
1530
+ ]
1531
+ }
1532
+ }
1533
+ );
1534
+
1535
+ // Create PDF from HTML or markdown
1536
+ const pdfResponse = await extraction.createPdf({
1537
+ content: '<h1>Hello</h1><p>This is a PDF</p>',
1538
+ contentType: 'html', // or 'markdown'
1539
+ options: {
1540
+ pageSize: 'A4',
1541
+ margin: { top: '1cm', bottom: '1cm', left: '1cm', right: '1cm' }
1542
+ }
1543
+ });
1544
+ console.log('PDF created:', pdfResponse.url);
1545
+ ```
1546
+
1547
+ **Use cases:**
1548
+ - Invoice/receipt processing
1549
+ - Form data extraction
1550
+ - PDF report generation
1551
+ - Document digitization
1552
+ - Automated data entry
1553
+
1554
+ ### AI - Application Settings
1555
+
1556
+ Manage AI provider settings and API keys for your application.
1557
+
1558
+ ```typescript
1559
+ const aiClient = squid.ai();
1560
+
1561
+ // Get application AI settings
1562
+ const settings = await aiClient.getApplicationAiSettings();
1563
+ console.log(settings);
1564
+
1565
+ // Set application AI settings
1566
+ await aiClient.setApplicationAiSettings({
1567
+ defaultModel: 'gpt-4o',
1568
+ // ... other settings
1569
+ });
1570
+
1571
+ // Set AI provider API key secret
1572
+ await aiClient.setAiProviderApiKeySecret(
1573
+ 'openai', // providerType: 'openai' | 'anthropic' | 'google' | etc.
1574
+ 'OPENAI_API_KEY' // secret key name from secrets manager
1575
+ );
1576
+ ```
1577
+
1578
+ ### Storage
1579
+
1580
+ ```typescript
1581
+ const storage = squid.storage(); // built_in_storage
1582
+ const s3 = squid.storage('aws-s3-integration');
1583
+
1584
+ // Upload file
1585
+ await storage.uploadFile('/documents', file, 3600); // 1 hour expiration
1586
+
1587
+ // Get download URL
1588
+ const url = await storage.getDownloadUrl('/documents/report.pdf', 3600);
1589
+ console.log(url.url);
1590
+
1591
+ // Get file metadata
1592
+ const metadata = await storage.getFileMetadata('/documents/report.pdf');
1593
+ console.log(metadata.filename, metadata.size, metadata.lastModified);
1594
+
1595
+ // List directory
1596
+ const contents = await storage.listDirectoryContents('/documents');
1597
+ console.log(contents.files, contents.directories);
1598
+
1599
+ // Delete files
1600
+ await storage.deleteFile('/documents/report.pdf');
1601
+ await storage.deleteFiles(['/docs/file1.pdf', '/docs/file2.pdf']);
1602
+ ```
1603
+
1604
+ ### External OAuth Integration
1605
+
1606
+ Manage OAuth tokens for external integrations. Squid automatically handles token refresh when tokens expire.
1607
+
1608
+ ```typescript
1609
+ const externalAuth = squid.externalAuth('google-oauth-integration');
1610
+
1611
+ // Save authorization code (exchange for access token)
1612
+ const tokenResponse = await externalAuth.saveAuthCode(
1613
+ 'authorization-code-from-oauth-flow',
1614
+ 'user-identifier' // Unique identifier for this user
1615
+ );
1616
+ console.log('Access token:', tokenResponse.accessToken);
1617
+ console.log('Refresh token:', tokenResponse.refreshToken);
1618
+ console.log('Expires at:', tokenResponse.expiresAt);
1619
+
1620
+ // Get access token (auto-refreshes if expired)
1621
+ const token = await externalAuth.getAccessToken('user-identifier');
1622
+ console.log('Current access token:', token.accessToken);
1623
+ ```
1624
+
1625
+ **Use cases:**
1626
+ - Google Drive/Calendar integration
1627
+ - GitHub OAuth
1628
+ - Slack OAuth
1629
+ - Any external service requiring OAuth 2.0
1630
+ - Squid handles token refresh automatically
1631
+
1632
+ ### Queues
1633
+
1634
+ ```typescript
1635
+ const queue = squid.queue<Message>('notifications');
1636
+ const kafkaQueue = squid.queue<Event>('events', 'kafka-integration');
1637
+
1638
+ // Produce messages
1639
+ await queue.produce([
1640
+ { type: 'email', to: 'user@example.com' },
1641
+ { type: 'sms', to: '+1234567890' }
1642
+ ]);
1643
+
1644
+ // Consume messages (observable)
1645
+ const subscription = queue.consume<Message>().subscribe(message => {
1646
+ console.log('Received:', message);
1647
+ });
1648
+
1649
+ // Unsubscribe
1650
+ subscription.unsubscribe();
1651
+ ```
1652
+
1653
+ ### Distributed Locks
1654
+
1655
+ ```typescript
1656
+ // Acquire and release manually
1657
+ const lock = await squid.acquireLock('payment-processing');
1658
+ try {
1659
+ await processPayment();
1660
+ } finally {
1661
+ lock.release(); // Note: release() is synchronous
1662
+ }
1663
+
1664
+ // Acquire with auto-release timeout (maxHoldTimeMillis)
1665
+ const lockWithTimeout = await squid.acquireLock('my-mutex', {
1666
+ maxHoldTimeMillis: 30000 // Auto-release after 30 seconds
1667
+ });
1668
+
1669
+ // Access lock properties
1670
+ console.log(lock.resourceId); // The mutex name: 'payment-processing'
1671
+ console.log(lock.lockId); // Unique ID for this lock instance
1672
+ console.log(lock.isReleased()); // Check if released: true/false
1673
+
1674
+ // Observe release (RxJS Observable)
1675
+ const sub = lock.observeRelease().subscribe(() => {
1676
+ console.log('Lock was released');
1677
+ });
1678
+ // Don't forget to unsubscribe when done
1679
+ sub.unsubscribe();
1680
+
1681
+ // Automatic release with withLock (recommended)
1682
+ const result = await squid.withLock('payment-processing', async (lock) => {
1683
+ // Critical section - only one client can execute at a time
1684
+ await processPayment();
1685
+ return 'success';
1686
+ });
1687
+
1688
+ // Lock is automatically released even if callback throws
1689
+ ```
1690
+
1691
+ **Important notes:**
1692
+ - Lock is automatically released if WebSocket connection is lost
1693
+ - If lock is already held by another client, `acquireLock()` will reject
1694
+ - `maxHoldTimeMillis` option automatically releases lock after specified duration
1695
+ - Use `@secureDistributedLock(mutexName?)` decorator to secure lock access
1696
+ - Without API key, you must define security rules for locks
1697
+
1698
+ ### API Integration
1699
+
1700
+ ```typescript
1701
+ const api = squid.api();
1702
+
1703
+ // HTTP methods - All return HttpResponse<T> with status, headers, and body
1704
+ const customerResponse = await api.get<Customer>('stripe-api', 'get-customer', {
1705
+ pathParams: { customerId: 'cus_123' },
1706
+ queryParams: { expand: 'subscriptions' }
1707
+ });
1708
+ // Access response details
1709
+ console.log(customerResponse.status); // HTTP status code
1710
+ console.log(customerResponse.headers); // Response headers
1711
+ console.log(customerResponse.body); // Parsed response body
1712
+
1713
+ const chargeResponse = await api.post<Charge, ChargeRequest>(
1714
+ 'stripe-api',
1715
+ 'create-charge',
1716
+ { amount: 1000, currency: 'usd' },
1717
+ { headers: { 'Idempotency-Key': 'unique' } }
1718
+ );
1719
+
1720
+ // Generic request method (for custom HTTP methods or complex requests)
1721
+ const response = await api.request<ResponseType, RequestType>(
1722
+ 'integration-id',
1723
+ 'endpoint-id',
1724
+ requestBody,
1725
+ { headers: {}, queryParams: {} },
1726
+ 'POST' // or 'GET', 'PUT', 'PATCH', 'DELETE'
1727
+ );
1728
+
1729
+ // Other methods: put, patch, delete
1730
+ await api.put(integrationId, endpointId, body, options);
1731
+ await api.patch(integrationId, endpointId, body, options);
1732
+ await api.delete(integrationId, endpointId, body, options);
1733
+ ```
1734
+
1735
+ ### Web
1736
+
1737
+ ```typescript
1738
+ const web = squid.web();
1739
+
1740
+ // AI-powered web search
1741
+ const results = await web.aiSearch('latest AI developments');
1742
+ console.log(results.markdownContent);
1743
+ console.log(results.citations);
1744
+
1745
+ // Get URL content (as markdown)
1746
+ const content = await web.getUrlContent('https://example.com/article');
1747
+
1748
+ // URL shortening
1749
+ const shortUrl = await web.createShortUrl('https://very-long-url.com', 86400);
1750
+ console.log(shortUrl.url, shortUrl.id);
1751
+
1752
+ const shortUrls = await web.createShortUrls(['url1', 'url2'], 86400);
1753
+
1754
+ await web.deleteShortUrl(shortUrlId);
1755
+ await web.deleteShortUrls([id1, id2]);
1756
+ ```
1757
+
1758
+ ### Schedulers
1759
+
1760
+ ```typescript
1761
+ const schedulers = squid.schedulers;
1762
+
1763
+ // List all
1764
+ const all = await schedulers.list();
1765
+
1766
+ // Enable/disable
1767
+ await schedulers.enable('daily-cleanup');
1768
+ await schedulers.enable(['scheduler-1', 'scheduler-2']);
1769
+
1770
+ await schedulers.disable('hourly-sync');
1771
+ await schedulers.disable(['scheduler-1', 'scheduler-2']);
1772
+ ```
1773
+
1774
+ ### Observability & Metrics
1775
+
1776
+ ```typescript
1777
+ const obs = squid.observability;
1778
+
1779
+ // Report metric
1780
+ await obs.reportMetric({
1781
+ name: 'api_request_count',
1782
+ value: 1,
1783
+ tags: { endpoint: '/users', method: 'GET' },
1784
+ timestamp: Date.now()
1785
+ });
1786
+
1787
+ // Query metrics
1788
+ const metrics = await obs.queryMetrics({
1789
+ metricNames: ['api_request_count'],
1790
+ startTime: startTimestamp,
1791
+ endTime: endTimestamp,
1792
+ groupBy: ['endpoint'],
1793
+ filters: { method: 'GET' },
1794
+ aggregation: 'sum'
1795
+ });
1796
+
1797
+ // Flush pending
1798
+ await obs.flush();
1799
+ ```
1800
+
1801
+ ### Custom Notifications
1802
+
1803
+ Send custom messages between clients for real-time coordination.
1804
+
1805
+ ```typescript
1806
+ const notifications = squid.getNotificationClient();
1807
+
1808
+ // Observe incoming notifications
1809
+ const subscription = notifications.observeNotifications().subscribe(payload => {
1810
+ console.log('Received notification:', payload);
1811
+ // payload can be any serializable data
1812
+ });
1813
+
1814
+ // Publish notification to specific clients - can be done only if Squid was initialized with API key.
1815
+ await notifications.publishNotification(
1816
+ { type: 'update', message: 'Data changed' }, // payload
1817
+ ['client-id-1', 'client-id-2'] // target client IDs
1818
+ );
1819
+
1820
+ // Unsubscribe when done
1821
+ subscription.unsubscribe();
1822
+ ```
1823
+
1824
+ **Use cases:**
1825
+ - Real-time coordination between users
1826
+ - Custom event notifications
1827
+ - Client-to-client messaging
1828
+ - Broadcast updates to specific users
1829
+
1830
+ ### Jobs
1831
+
1832
+ Jobs allow you to track the status of long-running asynchronous operations. Each job has a unique ID that can be used to query its status or wait for completion.
1833
+
1834
+ **Important:** Job IDs must be globally unique and kept private. Anyone who knows a job ID can query its status or result.
1835
+
1836
+ ```typescript
1837
+ const jobClient = squid.job();
1838
+
1839
+ // Get job status (returns AsyncJob with status: 'pending' | 'completed' | 'failed')
1840
+ const job = await jobClient.getJob<Result>('job-123');
1841
+ if (job) {
1842
+ console.log('Status:', job.status);
1843
+ if (job.status === 'completed') {
1844
+ console.log('Result:', job.result);
1845
+ } else if (job.status === 'failed') {
1846
+ console.log('Error:', job.error);
1847
+ }
1848
+ }
1849
+
1850
+ // Wait for completion (resolves when job finishes or throws if job fails)
1851
+ const result = await jobClient.awaitJob<Result>('job-123');
1852
+ console.log('Job completed with result:', result);
1853
+ ```
1854
+
1855
+ ### Admin - Integrations
1856
+
1857
+ **Important:** Admin methods require initialization with an API key. They cannot be used with user authentication.
1858
+
1859
+ ```typescript
1860
+ // Squid must be initialized with apiKey
1861
+ const squid = new Squid({ appId: 'app-id', region: 'us-east-1.aws', apiKey: 'your-api-key' });
1862
+
1863
+ const integrations = squid.admin().integrations();
1864
+
1865
+ // List all or by type
1866
+ const all = await integrations.list();
1867
+ const databases = await integrations.list('database');
1868
+
1869
+ // Get one
1870
+ const integration = await integrations.get('postgres-db');
1871
+
1872
+ // Create/update
1873
+ await integrations.upsertIntegration({
1874
+ integrationId: 'my-postgres',
1875
+ type: 'postgres',
1876
+ connectionString: 'postgresql://...'
1877
+ });
1878
+
1879
+ // Delete
1880
+ await integrations.delete('old-integration');
1881
+ await integrations.deleteMany(['int-1', 'int-2']);
1882
+
1883
+ // Get integration schema
1884
+ const schema = await integrations.getIntegrationSchema<MySchemaType>('postgres-db');
1885
+ console.log(schema);
1886
+
1887
+ // Set integration schema
1888
+ await integrations.setIntegrationSchema('postgres-db', {
1889
+ collections: [
1890
+ {
1891
+ name: 'users',
1892
+ fields: [
1893
+ { name: 'id', type: 'string', primaryKey: true },
1894
+ { name: 'email', type: 'string' },
1895
+ { name: 'name', type: 'string' }
1896
+ ]
1897
+ }
1898
+ ]
1899
+ });
1900
+ ```
1901
+
1902
+ ### Admin - Secrets
1903
+
1904
+ ```typescript
1905
+ const secrets = squid.admin().secrets();
1906
+
1907
+ // Get secret
1908
+ const value = await secrets.get('STRIPE_KEY');
1909
+
1910
+ // Get all
1911
+ const all = await secrets.getAll();
1912
+
1913
+ // Create/update
1914
+ await secrets.upsert('API_KEY', 'secret-value');
1915
+ await secrets.upsertMany([
1916
+ { key: 'KEY1', value: 'val1' },
1917
+ { key: 'KEY2', value: 'val2' }
1918
+ ]);
1919
+
1920
+ // Delete
1921
+ await secrets.delete('OLD_KEY');
1922
+ await secrets.deleteMany(['KEY1', 'KEY2']);
1923
+
1924
+ // API Keys
1925
+ const apiKeys = secrets.apiKeys;
1926
+ await apiKeys.upsert('my-key');
1927
+ const key = await apiKeys.get('my-key');
1928
+ await apiKeys.delete('my-key');
1929
+ ```
1930
+
1931
+ ## Backend SDK
1932
+
1933
+ ### SquidService Base Class
1934
+
1935
+ All backend services extend `SquidService`:
1936
+
1937
+ ```typescript
1938
+ import { SquidService } from '@squidcloud/backend';
1939
+
1940
+ export class MyService extends SquidService {
1941
+ // Decorated methods
1942
+ }
1943
+ ```
1944
+
1945
+ **Important:** In backend code running on Squid's infrastructure, the Squid client is **already initialized and available** via `this.squid`. You don't need to manually create a new instance or provide configuration parameters - simply use the pre-configured client.
1946
+
1947
+ **Available properties:**
1948
+
1949
+ ```typescript
1950
+ this.region // 'local' during dev, region in production
1951
+ this.backendBaseUrl
1952
+ this.secrets // From Squid Console
1953
+ this.apiKeys
1954
+ this.context // RunContext (appId, clientId, sourceIp, headers, openApiContext)
1955
+ this.squid // Pre-initialized Squid client instance (ready to use)
1956
+ this.assetsDirectory // Path to public/ folder
1957
+ ```
1958
+
1959
+ **Auth methods:**
1960
+
1961
+ ```typescript
1962
+ // Get user authentication (JWT token)
1963
+ this.getUserAuth() // AuthWithBearer | undefined
1964
+ // Returns: { type: 'Bearer', userId: string, expiration: number, attributes: Record<string, any>, jwt?: string }
1965
+
1966
+ // Get API key authentication
1967
+ this.getApiKeyAuth() // AuthWithApiKey | undefined
1968
+ // Returns: { type: 'ApiKey', apiKey: string }
1969
+
1970
+ this.isAuthenticated() // boolean - true if user token OR API key
1971
+ this.assertIsAuthenticated() // throws if not authenticated
1972
+ this.assertApiKeyCall() // throws if not API key auth
1973
+ ```
1974
+
1975
+ **Helper methods:**
1976
+
1977
+ ```typescript
1978
+ // Create webhook response (for @webhook decorated functions)
1979
+ this.createWebhookResponse(body?, statusCode?, headers?)
1980
+ // Throws webhook response immediately (interrupts execution)
1981
+ this.throwWebhookResponse({ body?, statusCode?, headers? })
1982
+
1983
+ // Create OpenAPI response (for OpenAPI/tsoa decorated functions)
1984
+ this.createOpenApiResponse(body?, statusCode?, headers?)
1985
+ // Throws OpenAPI response immediately (interrupts execution)
1986
+ this.throwOpenApiResponse({ body?, statusCode?, headers? })
1987
+
1988
+ // Convert browser File to SquidFile for OpenAPI file returns
1989
+ await this.convertToSquidFile(file: File): Promise<SquidFile>
1990
+
1991
+ // Publish AI status update to specific client (used in @aiFunction)
1992
+ await this.publishAiStatusUpdate(update: AiStatusMessage, clientId: ClientId)
1993
+ ```
1994
+
1995
+ ### Backend Decorators
1996
+
1997
+ This section contains backend decorators that don't have corresponding client methods (triggers, schedulers, security rules, etc.).
1998
+
1999
+ #### @trigger(options)
2000
+ Responds to database collection changes.
2001
+
2002
+ ```typescript
2003
+ @trigger({ id: 'user-created', collection: 'users', mutationTypes: ['insert'] })
2004
+ async onUserCreated(request: TriggerRequest<User>): Promise<void> {
2005
+ console.log(request.mutationType); // 'insert' | 'update' | 'delete'
2006
+ console.log(request.docBefore);
2007
+ console.log(request.docAfter);
2008
+ }
2009
+ ```
2010
+
2011
+ #### @scheduler(options)
2012
+ Schedules periodic execution using cron expressions.
2013
+
2014
+ ```typescript
2015
+ import { CronExpression } from '@squidcloud/backend';
2016
+
2017
+ // Using cron string directly
2018
+ @scheduler({ id: 'daily-cleanup', cron: '0 0 * * *' }) // Daily at midnight UTC
2019
+ async cleanup(): Promise<void> {
2020
+ console.log('Running cleanup');
2021
+ }
2022
+
2023
+ // Using predefined CronExpression enum
2024
+ @scheduler({ id: 'hourly-sync', cron: CronExpression.EVERY_HOUR })
2025
+ async hourlySync(): Promise<void> {
2026
+ console.log('Running hourly sync');
2027
+ }
2028
+
2029
+ @scheduler({ id: 'weekday-morning', cron: CronExpression.MONDAY_TO_FRIDAY_AT_9AM })
2030
+ async weekdayTask(): Promise<void> {
2031
+ console.log('Running weekday morning task');
2032
+ }
2033
+
2034
+ @scheduler({ id: 'frequent', cron: CronExpression.EVERY_5_MINUTES, exclusive: false })
2035
+ async frequentTask(): Promise<void> {
2036
+ // exclusive: false allows concurrent runs
2037
+ }
2038
+ ```
2039
+
2040
+ **Common CronExpression values:**
2041
+ - `EVERY_SECOND`, `EVERY_5_SECONDS`, `EVERY_10_SECONDS`, `EVERY_30_SECONDS`
2042
+ - `EVERY_MINUTE`, `EVERY_5_MINUTES`, `EVERY_10_MINUTES`, `EVERY_30_MINUTES`
2043
+ - `EVERY_HOUR`, `EVERY_2_HOURS`, `EVERY_3_HOURS`, etc.
2044
+ - `EVERY_DAY_AT_MIDNIGHT`, `EVERY_DAY_AT_NOON`, `EVERY_DAY_AT_1AM`, etc.
2045
+ - `EVERY_WEEKDAY`, `EVERY_WEEKEND`, `EVERY_WEEK`
2046
+ - `MONDAY_TO_FRIDAY_AT_9AM`, `MONDAY_TO_FRIDAY_AT_5PM`, etc.
2047
+ - `EVERY_1ST_DAY_OF_MONTH_AT_MIDNIGHT`, `EVERY_QUARTER`, `EVERY_YEAR`
2048
+
2049
+ #### @secureTopic(topicName, type, integrationId?)
2050
+ Secures queue topics.
2051
+
2052
+ ```typescript
2053
+ @secureTopic('notifications', 'read')
2054
+ allowRead(): boolean {
2055
+ return this.isAuthenticated();
2056
+ }
2057
+
2058
+ @secureTopic('notifications', 'write')
2059
+ allowWrite(context: TopicWriteContext<any>): boolean {
2060
+ return context.messages.length <= 100;
2061
+ }
2062
+ ```
2063
+
2064
+ Types: `'read'`, `'write'`, `'all'`
2065
+
2066
+ #### @secureStorage(type, integrationId?)
2067
+ Secures storage operations.
2068
+
2069
+ ```typescript
2070
+ @secureStorage('write')
2071
+ allowWrite(context: StorageContext): boolean {
2072
+ return !context.pathsInBucket.some(p => p.startsWith('/admin'));
2073
+ }
2074
+ ```
2075
+
2076
+ Types: `'read'`, `'write'`, `'update'`, `'insert'`, `'delete'`, `'all'`
2077
+
2078
+ #### @secureApi(integrationId, endpointId?)
2079
+ Secures API integrations.
2080
+
2081
+ ```typescript
2082
+ @secureApi('stripe-api', 'create-charge')
2083
+ allowCharge(context: ApiCallContext): boolean {
2084
+ const amount = (context.body as any)?.amount;
2085
+ return amount < 10000;
2086
+ }
2087
+
2088
+ @secureApi('external-api') // Secures entire API
2089
+ allowApi(): boolean {
2090
+ return this.isAuthenticated();
2091
+ }
2092
+ ```
2093
+
2094
+ #### @secureNativeQuery(integrationId)
2095
+ Secures native database queries.
2096
+
2097
+ ```typescript
2098
+ @secureNativeQuery('postgres-db')
2099
+ allowQuery(context: NativeQueryContext): boolean {
2100
+ if (context.type === 'relational') {
2101
+ return !context.query.toUpperCase().includes('DROP');
2102
+ }
2103
+ return true;
2104
+ }
2105
+ ```
2106
+
2107
+ #### @secureAiQuery(integrationId?)
2108
+ Secures AI query execution.
2109
+
2110
+ ```typescript
2111
+ @secureAiQuery()
2112
+ allowAiQuery(context: AiQueryContext): boolean {
2113
+ return this.isAuthenticated() && context.prompt.length < 1000;
2114
+ }
2115
+ ```
2116
+
2117
+ #### @secureAiAgent(agentId?)
2118
+ Secures AI agent access.
2119
+
2120
+ ```typescript
2121
+ @secureAiAgent('customer-bot')
2122
+ allowAgent(context: SecureAiAgentContext): boolean {
2123
+ return !context.prompt?.includes('admin');
2124
+ }
2125
+
2126
+ @secureAiAgent() // All agents
2127
+ allowAllAgents(): boolean {
2128
+ return this.isAuthenticated();
2129
+ }
2130
+ ```
2131
+
2132
+ #### @secureDistributedLock(mutexName?)
2133
+ Secures distributed lock access.
2134
+
2135
+ ```typescript
2136
+ // Secure specific mutex
2137
+ @secureDistributedLock('payment-processing')
2138
+ allowPaymentLock(context: DistributedLockContext): boolean {
2139
+ // context.mutex contains the mutex name
2140
+ return this.isAuthenticated() && context.mutex === 'payment-processing';
2141
+ }
2142
+
2143
+ // Secure all mutexes
2144
+ @secureDistributedLock()
2145
+ allowAllLocks(context: DistributedLockContext): boolean {
2146
+ return this.isAuthenticated();
2147
+ }
2148
+ ```
2149
+
2150
+ **Important:** Without API key, you must define `@secureDistributedLock` decorators to allow lock acquisition.
2151
+
2152
+ #### @aiFunction(options)
2153
+ Exposes function to AI agents. Agents automatically call these functions based on user prompts.
2154
+
2155
+ ```typescript
2156
+ // Simple form: description and params array
2157
+ @aiFunction('Returns the names of the pirates crew on a given ship name', [
2158
+ { name: 'shipName', type: 'string', required: true, description: 'The name of the ship' }
2159
+ ])
2160
+ async getShipCrew({ shipName }: { shipName: string }): Promise<string[]> {
2161
+ const crew = await getCrew(shipName);
2162
+ return crew.map(m => m.name);
2163
+ }
2164
+
2165
+ // Options object form with more control
2166
+ @aiFunction({
2167
+ id: 'custom-function-id', // Optional custom ID
2168
+ description: 'Get user profile',
2169
+ params: [
2170
+ { name: 'userId', type: 'string', required: true, description: 'User ID' }
2171
+ ],
2172
+ attributes: {
2173
+ integrationType: ['salesforce'] // Only available when specific integrations connected
2174
+ }
2175
+ })
2176
+ async getUserProfile(
2177
+ params: { userId: string },
2178
+ context: AiFunctionCallContext
2179
+ ): Promise<User> {
2180
+ console.log('Agent ID:', context.agentId);
2181
+ console.log('Integration ID:', context.integrationId);
2182
+ return { userId: params.userId, name: 'John' };
2183
+ }
2184
+
2185
+ // Parameter types: 'string' | 'number' | 'boolean' | 'date' | 'files'
2186
+ @aiFunction('Books a hotel room', [
2187
+ { name: 'hotelName', type: 'string', required: true, description: 'Name of hotel' },
2188
+ { name: 'checkInDate', type: 'date', required: true, description: 'Check-in date' },
2189
+ { name: 'numberOfGuests', type: 'number', required: true, description: 'Number of guests' },
2190
+ { name: 'breakfast', type: 'boolean', required: false, description: 'Include breakfast' },
2191
+ { name: 'roomType', type: 'string', required: true, description: 'Type of room',
2192
+ enum: ['single', 'double', 'suite'] }
2193
+ ])
2194
+ async bookHotel(args: BookingArgs): Promise<string> {
2195
+ return `Booked ${args.roomType} room at ${args.hotelName}`;
2196
+ }
2197
+
2198
+ // Using predefined parameters (hidden from AI)
2199
+ // Call from client with: agent.ask(prompt, {
2200
+ // functions: ['sendEmail'],
2201
+ // predefinedParams: { sendEmail: { apiKey: 'secret' } }
2202
+ // })
2203
+ @aiFunction('Sends an email', [
2204
+ { name: 'recipient', type: 'string', required: true, description: 'Email recipient' },
2205
+ { name: 'subject', type: 'string', required: true, description: 'Email subject' },
2206
+ { name: 'body', type: 'string', required: true, description: 'Email body' }
2207
+ ])
2208
+ async sendEmail(args: { recipient: string; subject: string; body: string; apiKey?: string }): Promise<string> {
2209
+ await emailService.send(args.apiKey, args.recipient, args.subject, args.body);
2210
+ return 'Email sent successfully';
2211
+ }
2212
+ ```
2213
+
2214
+ **Using functions from client:**
2215
+ ```typescript
2216
+ const response = await agent.ask('What is the crew of the Black Pearl?', {
2217
+ functions: ['getShipCrew', 'getShipDetails'] // Function names or custom IDs
2218
+ });
2219
+ ```
2220
+
2221
+ #### @limits(options)
2222
+ Rate/quota limiting.
2223
+
2224
+ ```typescript
2225
+ // Simple rate limit (5 QPS globally)
2226
+ @limits({ rateLimit: 5 })
2227
+ async limited(): Promise<void> {}
2228
+
2229
+ // Quota (100 calls/month per user)
2230
+ @limits({ quotaLimit: { value: 100, scope: 'user', renewPeriod: 'monthly' } })
2231
+ async quotaLimited(): Promise<void> {}
2232
+
2233
+ // Multiple limits
2234
+ @limits({
2235
+ rateLimit: [
2236
+ { value: 100, scope: 'global' },
2237
+ { value: 10, scope: 'user' }
2238
+ ],
2239
+ quotaLimit: [
2240
+ { value: 10000, scope: 'global', renewPeriod: 'monthly' },
2241
+ { value: 500, scope: 'user', renewPeriod: 'weekly' }
2242
+ ]
2243
+ })
2244
+ async multiLimited(): Promise<void> {}
2245
+ ```
2246
+
2247
+ Scopes: `'global'`, `'user'`, `'ip'`
2248
+ Periods: `'hourly'`, `'daily'`, `'weekly'`, `'monthly'`, `'quarterly'`, `'annually'`
2249
+
2250
+ ## Common Patterns
2251
+
2252
+ ### Authentication
2253
+
2254
+ Client:
2255
+ ```typescript
2256
+ const squid = new Squid({
2257
+ appId: 'YOUR_APP_ID',
2258
+ region: 'us-east-1.aws',
2259
+ authProvider: {
2260
+ integrationId: 'auth0',
2261
+ getToken: async () => await auth0.getAccessTokenSilently()
2262
+ }
2263
+ });
2264
+ ```
2265
+
2266
+ Backend:
2267
+
2268
+ ```typescript
2269
+ @executable()
2270
+ async getUserData(): Promise<UserData> {
2271
+ this.assertIsAuthenticated();
2272
+ const userId = this.getUserAuth()?.userId;
2273
+ if (!userId) throw new Error('User not authenticated');
2274
+ return await fetchUserData(userId);
2275
+ }
2276
+
2277
+ @secureCollection('users', 'read')
2278
+ allowRead(context: QueryContext): boolean {
2279
+ const userId = this.getUserAuth()?.userId;
2280
+ if (!userId) return false;
2281
+ return context.isSubqueryOf('id', '==', userId);
2282
+ }
2283
+ ```
2284
+
2285
+ ### File Upload
2286
+
2287
+ Client:
2288
+ ```typescript
2289
+ const file: File = ...; // from input
2290
+ await squid.storage().uploadFile('/uploads', file);
2291
+ // OR
2292
+ await squid.executeFunction('processFile', file);
2293
+ ```
2294
+
2295
+ Backend:
2296
+ ```typescript
2297
+ @executable()
2298
+ async processFile(file: SquidFile): Promise<Result> {
2299
+ console.log(file.originalName, file.size, file.mimetype);
2300
+ const content = new TextDecoder().decode(file.data);
2301
+ return { processed: true };
2302
+ }
2303
+ ```
2304
+
2305
+ ### Using Squid Client in Backend
2306
+
2307
+ ```typescript
2308
+ export class MyService extends SquidService {
2309
+ @executable()
2310
+ async aggregateStats(userId: string): Promise<Stats> {
2311
+ const orders = await this.squid.collection('orders')
2312
+ .query()
2313
+ .eq('userId', userId)
2314
+ .snapshot();
2315
+
2316
+ return { totalOrders: orders.data.length };
2317
+ }
2318
+ }
2319
+ ```
2320
+
2321
+ ## Best Practices
2322
+
2323
+ 1. **Always secure collections/topics/storage** - Default deny
2324
+ 2. **Validate input in executables** - Check auth and params
2325
+ 3. **Use transactions for multi-doc updates** - Atomic operations
2326
+ 4. **Limit query results** - Default 1000, max 20000
2327
+ 5. **Use snapshots for one-time data** - Use subscriptions for realtime
2328
+ 6. **Batch operations when possible** - insertMany, deleteMany
2329
+ 7. **Use memoryOptions for AI conversations** - Not deprecated chatId
2330
+ 8. **Test in dev before prod deployment** - `squid deploy --environmentId dev`
2331
+
2332
+ ## Documentation
2333
+
2334
+ - Docs: https://docs.getsquid.ai/
2335
+ - Console: https://console.getsquid.ai/
2336
+ - Samples: https://github.com/squid-cloud-samples
2337
+
2338
+ ---
2339
+
2340
+ **This skill is verified against the actual Squid source code and contains only accurate, tested APIs.**