@personize/sdk 0.5.2 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -110,10 +110,32 @@ console.log(ctx.data.compiledContext);
110
110
  ### Prompt Execution
111
111
 
112
112
  ```typescript
113
- // Simple prompt
113
+ // Simple prompt (defaults to 'pro' tier)
114
114
  const response = await client.ai.prompt({
115
115
  prompt: 'Summarize our Q4 sales strategy',
116
116
  });
117
+ console.log(response.data?.metadata?.creditsCharged); // credits used
118
+
119
+ // With explicit tier
120
+ const fast = await client.ai.prompt({
121
+ prompt: 'Quick summary of today',
122
+ tier: 'basic', // cheapest, fastest
123
+ });
124
+
125
+ const premium = await client.ai.prompt({
126
+ prompt: 'Deep analysis of market trends',
127
+ tier: 'ultra', // highest quality model
128
+ });
129
+
130
+ // BYOK — use your own API key + model + provider (Pro/Enterprise plans only)
131
+ const byok = await client.ai.prompt({
132
+ prompt: 'Generate a report',
133
+ openrouterApiKey: 'sk-or-v1-...', // your OpenRouter key
134
+ model: 'anthropic/claude-sonnet-4-20250514',
135
+ provider: 'openrouter',
136
+ });
137
+ // BYOK billing: time-based (10 credits base + 10/extra minute) instead of per-token
138
+ // Without BYOK, model/provider are rejected — use tier to control quality level
117
139
 
118
140
  // Multi-step instructions with evaluation
119
141
  const result = await client.ai.prompt({
@@ -130,7 +152,6 @@ const analysis = await client.ai.prompt({
130
152
  attachments: [
131
153
  { name: 'dashboard.png', mimeType: 'image/png', data: base64EncodedImage },
132
154
  ],
133
- model: 'anthropic/claude-sonnet-4-20250514',
134
155
  });
135
156
 
136
157
  // Multimodal — PDF extraction via URL
@@ -168,14 +189,15 @@ console.log(research.data?.evaluation?.finalScore);
168
189
 
169
190
  ```typescript
170
191
  // Memorize content (dual extraction: structured + free-form)
171
- // Tip: prepend identity field hints to ensure demographic properties are captured
172
- await client.memory.memorize({
173
- content: 'Also extract First Name, Company Name, and Job Title if mentioned.\n\nMeeting notes: John prefers email contact. He is VP of Sales at Acme Corp.',
192
+ // Response includes recordId for immediate use in recall/digest calls
193
+ const memorized = await client.memory.memorize({
194
+ content: 'Meeting notes: John prefers email contact. He is VP of Sales at Acme Corp.',
174
195
  speaker: 'Sales Rep',
175
196
  enhanced: true,
176
197
  tags: ['meetings'],
177
198
  email: 'john@example.com',
178
199
  });
200
+ console.log(memorized.data?.recordId); // REC#<hex> — deterministic, use for recall/digest
179
201
 
180
202
  // Smart recall — advanced recall with reflection
181
203
  const results = await client.memory.smartRecall({
@@ -186,11 +208,12 @@ const results = await client.memory.smartRecall({
186
208
  generate_answer: true,
187
209
  });
188
210
 
189
- // Fast recall — low-latency mode (~500ms)
211
+ // Fast recall — low-latency mode (~500ms), guaranteed min 10 results
190
212
  const fast = await client.memory.smartRecall({
191
213
  query: 'contact info for John',
192
214
  email: 'john@example.com',
193
215
  fast_mode: true,
216
+ // min_results: 10 (default in fast_mode — always returns top N even below score threshold)
194
217
  });
195
218
 
196
219
  // Direct recall — DynamoDB lookup: properties + freeform memories (no AI, type required)
@@ -296,6 +319,95 @@ const evaluation = await client.evaluate.memorizationAccuracy({
296
319
  console.log(evaluation.data.summary.propertiesOptimized);
297
320
  ```
298
321
 
322
+ ## Tiers & Pricing
323
+
324
+ ### Generate Tiers (Prompt)
325
+
326
+ | Tier | Input Credits/1K tokens | Output Credits/1K tokens | Best For |
327
+ | :--- | :--- | :--- | :--- |
328
+ | `basic` | 0.2 | 0.4 | High-volume, simple tasks |
329
+ | `pro` | 0.5 | 1.0 | Balanced quality & cost (default) |
330
+ | `ultra` | 1.0 | 2.5 | Complex reasoning, highest quality |
331
+
332
+ ### Memorize Tiers
333
+
334
+ | Tier | Credits/1K tokens | Best For |
335
+ | :--- | :--- | :--- |
336
+ | `basic` | 1.0 | Bulk imports, simple data |
337
+ | `pro` | 2.5 | General use (default) |
338
+ | `pro_fast` | 3.5 | Pro quality, lower latency |
339
+ | `ultra` | 7.0 | Maximum extraction quality |
340
+
341
+ ### Retrieval (Recall & Smart Guidelines)
342
+
343
+ | Operation | Mode | Credits/Call |
344
+ | :--- | :--- | :--- |
345
+ | Smart Recall | `fast_mode: true` | 0.1 |
346
+ | Smart Recall | `fast_mode: false` (deep) | 0.2 |
347
+ | Smart Guidelines | `mode: 'fast'` | 0.1 |
348
+ | Smart Guidelines | `mode: 'deep'` | 0.5 |
349
+
350
+ 1 credit = $0.01.
351
+
352
+ ### Direct Providers
353
+
354
+ ### BYOK (Bring Your Own Key)
355
+
356
+ Pro and Enterprise plans can pass their own API key. Billing switches to time-based: **10 credits base + 10 credits per additional minute**.
357
+
358
+ When using BYOK, you **must** provide both `model` and `provider`. Without BYOK, `model` and `provider` are rejected — use `tier` to control quality level.
359
+
360
+ ```typescript
361
+ // BYOK: must specify all three
362
+ await client.ai.prompt({
363
+ prompt: '...',
364
+ openrouterApiKey: 'sk-or-v1-...',
365
+ model: 'anthropic/claude-sonnet-4-20250514',
366
+ provider: 'openrouter',
367
+ });
368
+
369
+ // Without BYOK: use tier (model/provider auto-selected)
370
+ await client.ai.prompt({
371
+ prompt: '...',
372
+ tier: 'pro', // basic, pro (default), ultra
373
+ });
374
+ ```
375
+
376
+ **Error cases:**
377
+ - `model`/`provider` without `openrouterApiKey` → `400 BYOK required`
378
+ - `openrouterApiKey` without `model`/`provider` → `400 model and provider required`
379
+ - BYOK on a plan that doesn't allow it → `403 byok_not_allowed`
380
+
381
+ Response metadata includes `byok: true` and `creditsCharged` reflecting time-based billing.
382
+
383
+ ## Best Practices: Query Crafting for smartRecall
384
+
385
+ The `smartRecall` endpoint uses vector similarity search. Query quality directly impacts result relevance. When building AI pipelines that call `smartRecall`, the **AI agent is responsible for crafting embedding-friendly queries**.
386
+
387
+ **Do:**
388
+ - Use specific, descriptive queries that match the language of stored data
389
+ - Include entity names, property names, or domain-specific terms
390
+ - Example: `"John Smith role title company background"` instead of `"Tell me about this contact"`
391
+
392
+ **Don't:**
393
+ - Use vague meta-queries like `"What do we know?"` or `"Tell me everything"`
394
+ - Use task-oriented queries like `"open tasks pending action items"` when only profile data was memorized
395
+
396
+ **Example — AI pipeline pattern:**
397
+ ```typescript
398
+ // BAD: vague query → low similarity scores → few or no results
399
+ const bad = await client.memory.smartRecall({ query: 'Tell me about this contact', email });
400
+
401
+ // GOOD: specific query targeting stored data types
402
+ const good = await client.memory.smartRecall({
403
+ query: `${contactName} role company background interests preferences`,
404
+ email,
405
+ fast_mode: true,
406
+ });
407
+ ```
408
+
409
+ **Guaranteed minimum results:** In `fast_mode`, `smartRecall` guarantees at least 10 results (configurable via `min_results`) even when scores fall below the threshold. This ensures your AI workflow always has context to reason about — it can then decide whether the data is sufficient or not.
410
+
299
411
  ## Configuration
300
412
 
301
413
  | Option | Type | Required | Description |
@@ -315,6 +427,19 @@ const client = new Personize({
315
427
  });
316
428
  ```
317
429
 
430
+ ## Migration from 0.5.x
431
+
432
+ **New in 0.6.0:**
433
+
434
+ | Feature | Details |
435
+ | :--- | :--- |
436
+ | `tier` on `PromptOptions` | Select generate tier: `basic`, `pro` (default), `ultra` |
437
+ | `tier` on `MemorizeOptions` / `BatchMemorizeOptions` | Select memorize tier: `basic`, `pro`, `pro_fast`, `ultra` |
438
+ | `openrouterApiKey` on `PromptOptions` | BYOK — use your own API key (Pro/Enterprise plans). Requires `model` + `provider`. |
439
+ | `model` / `provider` on `PromptOptions` | Custom model/provider selection. **Requires BYOK** (`openrouterApiKey`). Without BYOK, use `tier`. |
440
+ | `creditsCharged` in response metadata | Credits consumed by the request |
441
+ | SmartGuidelines `mode: 'full'` → `mode: 'deep'` | Renamed for consistency. `'full'` still accepted for backward compatibility. |
442
+
318
443
  ## Migration from 0.3.x
319
444
 
320
445
  **Breaking changes in 0.4.0:**
package/dist/types.d.ts CHANGED
@@ -245,8 +245,8 @@ export interface SmartGuidelinesOptions {
245
245
  tags?: string[];
246
246
  excludeTags?: string[];
247
247
  model?: string;
248
- /** Routing mode: "fast" for instant embedding-only (~200ms), "full" for deep LLM routing (~3s), "auto" to let the system decide. Default: "auto". */
249
- mode?: 'fast' | 'full' | 'auto';
248
+ /** Routing mode: "fast" for instant embedding-only (~200ms), "deep" for LLM routing (~3s), "auto" to let the system decide. "full" is accepted as alias for "deep". Default: "auto". */
249
+ mode?: 'fast' | 'deep' | 'full' | 'auto';
250
250
  /** Minimum cosine similarity score (0-1) for fast mode results. Lower values return more results. Default: 0.4 for supplementary, 0.7 for critical. */
251
251
  minScore?: number;
252
252
  /** Session ID for conversation continuity. */
@@ -290,8 +290,10 @@ export interface SmartGuidelinesUsage {
290
290
  export type SmartContextUsage = SmartGuidelinesUsage;
291
291
  export interface SmartGuidelinesResponse {
292
292
  success: boolean;
293
- /** Which routing mode was actually used. */
294
- mode: 'fast' | 'full';
293
+ /** Which routing mode was actually used. "full" may appear for backward compat (equivalent to "deep"). */
294
+ mode: 'fast' | 'deep' | 'full';
295
+ /** Credits charged for this request. */
296
+ creditsCharged?: number;
295
297
  /** LLM analysis of the task. Null in fast mode. */
296
298
  analysis?: SmartGuidelinesAnalysis | null;
297
299
  selection: SmartGuidelinesSelection[];
@@ -389,8 +391,14 @@ export interface PromptOptions {
389
391
  maxSteps?: number;
390
392
  }>;
391
393
  stream?: boolean;
394
+ /** LLM model. Requires BYOK (`openrouterApiKey`). Without BYOK, use `tier` instead — model is auto-selected. */
392
395
  model?: string;
396
+ /** LLM provider: 'openai' | 'anthropic' | 'google' | 'xai' | 'deepseek' | 'openrouter'. Requires BYOK (`openrouterApiKey`). Without BYOK, provider is auto-selected by tier. */
393
397
  provider?: string;
398
+ /** Generate tier: 'basic' (fast/cheap), 'pro' (balanced, default), 'ultra' (highest quality). Determines default model and credit rate. Used when no BYOK key is provided. */
399
+ tier?: 'basic' | 'pro' | 'ultra';
400
+ /** Bring Your Own Key: pass your own OpenRouter (or direct provider) API key. When provided, `model` and `provider` are required. Time-based billing instead of per-token. Requires Pro or Enterprise plan. */
401
+ openrouterApiKey?: string;
394
402
  context?: string;
395
403
  sessionId?: string;
396
404
  /**
@@ -441,6 +449,12 @@ export interface PromptResponse {
441
449
  metadata?: {
442
450
  model: string;
443
451
  provider: string;
452
+ /** Generate tier used for billing. */
453
+ tier?: 'basic' | 'pro' | 'ultra';
454
+ /** Credits charged for this request. 1 credit = $0.01. */
455
+ creditsCharged?: number;
456
+ /** True when BYOK (Bring Your Own Key) billing was applied. */
457
+ byok?: boolean;
444
458
  usage?: {
445
459
  promptTokens: number;
446
460
  completionTokens: number;
@@ -521,6 +535,8 @@ export interface AgentRunOptions {
521
535
  export interface MemorizeOptions {
522
536
  /** Content to memorize. */
523
537
  content: string;
538
+ /** Memorize tier: 'basic' (fast), 'pro' (balanced, default), 'pro_fast' (pro speed), 'ultra' (highest extraction). */
539
+ tier?: 'basic' | 'pro' | 'pro_fast' | 'ultra';
524
540
  /** Speaker/source label. */
525
541
  speaker?: string;
526
542
  /** Timestamp of the content. */
@@ -561,10 +577,14 @@ export interface SmartRecallOptions {
561
577
  minScore?: number;
562
578
  /** CRM record ID for entity scoping. */
563
579
  record_id?: string;
580
+ /** CRM record ID (camelCase alias). */
581
+ recordId?: string;
564
582
  /** Filter results to a specific entity by email. */
565
583
  email?: string;
566
584
  /** Website URL for entity scoping. */
567
585
  website_url?: string;
586
+ /** Website URL (camelCase alias). */
587
+ websiteUrl?: string;
568
588
  /** Entity type filter (e.g. 'Contact', 'Company'). */
569
589
  type?: string;
570
590
  /** Scope results to specific collections by ID. */
@@ -590,6 +610,11 @@ export interface SmartRecallOptions {
590
610
  * In fast_mode, defaults to 0.3 if not specified.
591
611
  */
592
612
  min_score?: number;
613
+ /**
614
+ * Guarantee at least this many results even if they fall below min_score.
615
+ * In fast_mode, defaults to 10. Set to 0 to disable (strict score cutoff).
616
+ */
617
+ min_results?: number;
593
618
  /** Metadata filters for narrowing results. */
594
619
  filters?: Record<string, unknown>;
595
620
  }
@@ -602,10 +627,14 @@ export interface RecallOptions {
602
627
  type: string;
603
628
  /** CRM record ID for entity scoping. */
604
629
  record_id?: string;
630
+ /** CRM record ID (camelCase alias). */
631
+ recordId?: string;
605
632
  /** Filter results to a specific entity by email. */
606
633
  email?: string;
607
634
  /** Website URL for entity scoping. */
608
635
  website_url?: string;
636
+ /** Website URL (camelCase alias). */
637
+ websiteUrl?: string;
609
638
  /** Additional filters. */
610
639
  filters?: Record<string, unknown>;
611
640
  }
@@ -634,6 +663,8 @@ export interface BatchMemorizeMapping {
634
663
  export interface BatchMemorizeOptions {
635
664
  /** Source system label (e.g. 'Hubspot', 'Salesforce') */
636
665
  source: string;
666
+ /** Memorize tier: 'basic' | 'pro' | 'pro_fast' | 'ultra'. Default: 'pro'. */
667
+ tier?: 'basic' | 'pro' | 'pro_fast' | 'ultra';
637
668
  /** Mapping configuration for the sync */
638
669
  mapping: BatchMemorizeMapping;
639
670
  /** Array of source data rows (key-value objects matching sourceField names) */
@@ -646,20 +677,32 @@ export interface BatchMemorizeOptions {
646
677
  export interface SmartDigestOptions {
647
678
  /** CRM record ID */
648
679
  record_id?: string;
680
+ /** CRM record ID (camelCase alias). */
681
+ recordId?: string;
649
682
  /** Contact email */
650
683
  email?: string;
651
684
  /** Company website URL */
652
685
  website_url?: string;
686
+ /** Website URL (camelCase alias). */
687
+ websiteUrl?: string;
653
688
  /** Entity type ('Contact', 'Company'). Auto-inferred if omitted. */
654
689
  type?: string;
655
690
  /** Max tokens for compiled context (default: 1000) */
656
691
  token_budget?: number;
692
+ /** Max tokens (camelCase alias). */
693
+ tokenBudget?: number;
657
694
  /** Max free-form memories to include (default: 20) */
658
695
  max_memories?: number;
696
+ /** Max memories (camelCase alias). */
697
+ maxMemories?: number;
659
698
  /** Include DynamoDB property snapshot (default: true) */
660
699
  include_properties?: boolean;
700
+ /** Include properties (camelCase alias). */
701
+ includeProperties?: boolean;
661
702
  /** Include LanceDB free-form memories (default: true) */
662
703
  include_memories?: boolean;
704
+ /** Include memories (camelCase alias). */
705
+ includeMemories?: boolean;
663
706
  }
664
707
  export interface SmartDigestResponse {
665
708
  success: boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@personize/sdk",
3
- "version": "0.5.2",
3
+ "version": "0.6.1",
4
4
  "description": "Official Personize SDK",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",