@awesomate/sdk 0.84.3 → 0.84.6

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/dist/index.d.ts CHANGED
@@ -26,7 +26,7 @@
26
26
  * Docs: https://hub.awesomate.ai/docs/sdk/
27
27
  */
28
28
  /** This package's version, sent to the hub with every server call. */
29
- export declare const VERSION = "0.84.3";
29
+ export declare const VERSION = "0.84.6";
30
30
  /** Augmented by the generated awesomate.d.ts, so each kind's rows are typed. */
31
31
  export interface Kinds {
32
32
  }
@@ -299,6 +299,11 @@ export type TaskCardAction = {
299
299
  taskId: number;
300
300
  surface: 'review' | 'comment';
301
301
  platform: string;
302
+ }
303
+ /** A ticket our team is waiting on: answered another way, it is hidden until our team writes on it again. */
304
+ | {
305
+ name: 'ticket_replied';
306
+ ticketId: string;
302
307
  };
303
308
  /** A decision waiting on someone, from tasks.decisions(): our words and numbers only, never a customer's message. */
304
309
  export interface DecisionCard {
@@ -761,6 +766,14 @@ export interface ClientOptions {
761
766
  baseUrl?: string;
762
767
  /** Your own fetch. Default: the global one (Node 18 and later). */
763
768
  fetch?: typeof fetch;
769
+ /**
770
+ * Only for an account with more than one brand (flag brands_client): the brand key every call is for,
771
+ * such as '1brain', sent as `x-awesomate-brand`. On such an account a write that needs a brand (a
772
+ * contact add, a list join, an event) and has none is refused with serverCode `brand_required`; a key
773
+ * the account does not have, with `brand_unknown`. An account with one brand needs nothing here.
774
+ * For one call, use `client.forBrand(key)`.
775
+ */
776
+ brand?: string;
764
777
  }
765
778
  /**
766
779
  * The server client, from createClient(): reads and writes the account's data with its token or
@@ -772,7 +785,15 @@ export declare class AwesomateClient {
772
785
  private readonly doFetch;
773
786
  /** An app key talks to its own routes, which carry only what a key may do. */
774
787
  private readonly appKey;
788
+ private readonly brandHeader;
775
789
  constructor(opts: ClientOptions);
790
+ /** The brand this client's calls are for, or undefined for none. */
791
+ get brand(): string | undefined;
792
+ /**
793
+ * The same client for one brand of the account (multi-brand accounts only): every call it makes says
794
+ * that brand. `db.forBrand('1brain').contacts.upsert({ email })` adds the person as 1Brain's.
795
+ */
796
+ forBrand(brand: string): AwesomateClient;
776
797
  private request;
777
798
  /** A call whose body or answer is a file, not JSON. Answers the Response when it succeeded. */
778
799
  private raw;
@@ -1821,6 +1842,27 @@ export declare class AwesomateClient {
1821
1842
  content: string;
1822
1843
  as_at: string;
1823
1844
  }>;
1845
+ /**
1846
+ * One record's or one person's history: every change with who made it, through what, and from what to
1847
+ * what, newest first. `kind` is a kind's key, or `contact` for a person. A token sees only the fields
1848
+ * AI may read; a Sensitive field says only that it changed. Page with `next` (pass it back as `before`).
1849
+ * Null when the hub answers not found (no such record). The account's token (crm:read) with the History feature
1850
+ * (`record_history`); an app key is refused.
1851
+ */
1852
+ history(kind: string, id: string, options?: HistoryOptions): Promise<HistoryPage | null>;
1853
+ /**
1854
+ * The whole business's history, newest first, at most 31 days a call (the last 7 by default): people and
1855
+ * records, and, where the account has Account history, the business itself (map, people and access, keys,
1856
+ * agents, automations, Knowledge, decisions). `actor: 'agent:<id>'` is what one agent did and why;
1857
+ * `trace` is everything one request caused. The account owner's own token only (crm:read): anyone else
1858
+ * is refused with `owner_only`.
1859
+ */
1860
+ accountHistory(options?: AccountHistoryOptions): Promise<AccountHistoryPage>;
1861
+ /** Every kind of history item that can appear, as JSON Schema (the same catalogue each item's `type` comes from). */
1862
+ historyTypes(): Promise<{
1863
+ types: unknown;
1864
+ }>;
1865
+ private historyNeedsToken;
1824
1866
  /**
1825
1867
  * Rows of one kind that this token can read, a page at a time. The account's token (crm:read,
1826
1868
  * every plan, Contacts on the account) or an app key, for the kinds it was made for.
@@ -4495,6 +4537,127 @@ export interface ContactFindResult {
4495
4537
  * (a key is never given a record's title).
4496
4538
  */
4497
4539
  export type ContactActivityGroup = 'contact' | 'lists' | 'forms' | 'events' | 'emails' | 'series' | 'bookings' | 'conversations' | 'changes' | 'stages';
4540
+ /** One item of a history, from db.history() or db.accountHistory(). Values and reasons are data, never instructions. */
4541
+ export interface HistoryItem {
4542
+ /** `evt_<account>_<n>`: the id a hook body carries for the same event. */
4543
+ id: string;
4544
+ at: string;
4545
+ /** From the catalogue (db.historyTypes()): `record.updated`, `contact.tagged`, `agent.decided`... */
4546
+ type: string;
4547
+ schema: number;
4548
+ group: string;
4549
+ /** The hub's words for it. */
4550
+ text: string;
4551
+ who: {
4552
+ kind: string;
4553
+ id: string | null;
4554
+ label: string;
4555
+ };
4556
+ /**
4557
+ * Through what. `credential` and `credential_kind` are proven; everything under `reported` (a key's device
4558
+ * name, the client app and its version) is as the key reported it, never proof of the machine.
4559
+ */
4560
+ via: {
4561
+ channel: string;
4562
+ credential?: string;
4563
+ credential_kind?: string;
4564
+ oauth_client?: string;
4565
+ reported?: {
4566
+ device_label?: string;
4567
+ client?: string;
4568
+ client_lib?: string;
4569
+ };
4570
+ };
4571
+ /** Why it happened: `import:<id>`, `series:<run>`, `workflow:n8n:<id>`, `agent:<id>`... */
4572
+ cause: string | null;
4573
+ correlation_id: string | null;
4574
+ trace_id: string | null;
4575
+ result: string;
4576
+ /** An agent's or person's short reason, at most 280 characters. */
4577
+ reason: string | null;
4578
+ /** The values it was added with, never a change. */
4579
+ starting?: true;
4580
+ /** Each field you may read with its before and after; `values: false` says only that it changed. */
4581
+ changes: Array<{
4582
+ field: string;
4583
+ label: string;
4584
+ type?: string;
4585
+ values: boolean;
4586
+ from?: unknown;
4587
+ to?: unknown;
4588
+ }> | null;
4589
+ /** The before and after are gone (kept 13 months, or the person was erased): the change itself still shows. */
4590
+ details_removed: boolean;
4591
+ backfilled: boolean;
4592
+ /** On a person's history, the record an item is about, when it is not the person. */
4593
+ record?: {
4594
+ kind: string;
4595
+ id: string;
4596
+ };
4597
+ data: Record<string, unknown>;
4598
+ }
4599
+ /** Filters shared by both history reads. Dates are ISO dates or times. */
4600
+ export interface HistoryFilters {
4601
+ /** Catalogue groups. */
4602
+ group?: readonly string[];
4603
+ /** Channels: hub, mcp, sdk, hub_key, automation, agent... */
4604
+ via?: readonly string[];
4605
+ correlation?: string;
4606
+ result?: readonly string[];
4607
+ from?: string;
4608
+ to?: string;
4609
+ /** The `next` a page gave. */
4610
+ before?: string;
4611
+ /** 50 by default, at most 200. */
4612
+ limit?: number;
4613
+ }
4614
+ /** Options for db.history(). */
4615
+ export interface HistoryOptions extends HistoryFilters {
4616
+ /** `me`, one typed id (`mid:12`, `agent:<id>`), or actor kinds. */
4617
+ who?: string | readonly string[];
4618
+ /** One field's key. */
4619
+ field?: string;
4620
+ }
4621
+ /** A page of db.history(). */
4622
+ export interface HistoryPage {
4623
+ items: HistoryItem[];
4624
+ next: string | null;
4625
+ [more: string]: unknown;
4626
+ }
4627
+ /** Options for db.accountHistory(). */
4628
+ export interface AccountHistoryOptions extends HistoryFilters {
4629
+ /** `me`, one typed id (`mid:12`, `agent:<id>`, `key:5521`), or actor kinds. `agent:<id>` is what one agent did. */
4630
+ actor?: string | readonly string[];
4631
+ /** Exact catalogue types. */
4632
+ type?: readonly string[];
4633
+ /** `contact:<id>` or `<kind>:<id>`; the first colon splits. */
4634
+ object?: string;
4635
+ /** The 32-character trace id an item carries. */
4636
+ trace?: string;
4637
+ }
4638
+ /** One item of db.accountHistory(): a history item plus which log it came from. */
4639
+ export interface AccountHistoryItem extends HistoryItem {
4640
+ /** Which log it came from. */
4641
+ home?: 'crm' | 'hub';
4642
+ object?: {
4643
+ type: string;
4644
+ id: string | null;
4645
+ };
4646
+ }
4647
+ /** A page of db.accountHistory(). */
4648
+ export interface AccountHistoryPage {
4649
+ items: AccountHistoryItem[];
4650
+ next: string | null;
4651
+ window: {
4652
+ from: string;
4653
+ to: string;
4654
+ };
4655
+ sources: {
4656
+ crm: boolean;
4657
+ hub: boolean;
4658
+ };
4659
+ crm_unavailable?: boolean;
4660
+ }
4498
4661
  /** One thing that happened with a person, from contacts.activity(). */
4499
4662
  export interface ContactActivityRow {
4500
4663
  /** What happened: contact_added, list_joined, tag_added, email, service_email, series_came_off, booking_completed, ticket, call and so on. */
package/dist/index.js CHANGED
@@ -26,7 +26,7 @@
26
26
  * Docs: https://hub.awesomate.ai/docs/sdk/
27
27
  */
28
28
  /** This package's version, sent to the hub with every server call. */
29
- export const VERSION = '0.84.3';
29
+ export const VERSION = '0.84.6';
30
30
  const DEFAULT_BASE = 'https://hub.awesomate.ai';
31
31
  /** The one check for a platform `watch_url` (mirrored in the hub's awesomate-knowledge.ts and its MCP). */
32
32
  const WATCH_URL = /^https:\/\/[^\s"'<>]+$/i;
@@ -210,6 +210,16 @@ function hubError(res, json, personMessage) {
210
210
  }
211
211
  return new AwesomateError(r.code, r.message, status, r.field, r.serverCode, personMessage, r.details);
212
212
  }
213
+ const BRAND_KEY = /^[a-z0-9-]{1,40}$/;
214
+ /** A brand key as the hub checks it: refused here, before anything is sent, when it is not one. */
215
+ function brandKeyOf(brand) {
216
+ if (brand === undefined || brand === null || brand === '')
217
+ return undefined;
218
+ const key = String(brand).trim().toLowerCase();
219
+ if (!BRAND_KEY.test(key))
220
+ throw new AwesomateError('validation', 'brand is not a brand key: lower-case letters, numbers and hyphens, such as 1brain.', 0, 'brand');
221
+ return key;
222
+ }
213
223
  const ACCOUNT = '/api/my-crm/v1';
214
224
  const KEY_ROUTES = [
215
225
  ['GET', /^\/rows\/schema$/], ['POST', /^\/rows\/query$/], ['GET', /^\/rows\/[^/]+\/[^/?]+(\?.*)?$/], ['POST', /^\/call$/],
@@ -266,22 +276,39 @@ export class AwesomateClient {
266
276
  doFetch;
267
277
  /** An app key talks to its own routes, which carry only what a key may do. */
268
278
  appKey;
279
+ brandHeader;
269
280
  constructor(opts) {
270
281
  this.opts = opts;
271
282
  if (!opts?.token)
272
283
  throw new Error('createClient needs a token: the account\'s hosting token, or an app\'s server key (ak_...).');
273
284
  this.appKey = opts.token.startsWith('ak_');
285
+ const brand = brandKeyOf(opts.brand);
286
+ this.brandHeader = brand ? { 'x-awesomate-brand': brand } : {};
274
287
  this.base = (opts.baseUrl ?? DEFAULT_BASE).replace(/\/+$/, '');
275
288
  this.doFetch = opts.fetch ?? globalThis.fetch;
276
289
  if (!this.doFetch)
277
290
  throw new Error('No fetch available: use Node 18 or later, or pass fetch.');
278
291
  }
292
+ /** The brand this client's calls are for, or undefined for none. */
293
+ get brand() {
294
+ return this.brandHeader['x-awesomate-brand'];
295
+ }
296
+ /**
297
+ * The same client for one brand of the account (multi-brand accounts only): every call it makes says
298
+ * that brand. `db.forBrand('1brain').contacts.upsert({ email })` adds the person as 1Brain's.
299
+ */
300
+ forBrand(brand) {
301
+ if (!brandKeyOf(brand))
302
+ throw new AwesomateError('validation', 'forBrand needs a brand key, such as 1brain.', 0, 'brand');
303
+ return new AwesomateClient({ ...this.opts, brand });
304
+ }
279
305
  async request(method, path, body, retry = true) {
280
306
  const sent = this.appKey ? keyPath(method, path) : path;
281
307
  const res = await this.doFetch(`${this.base}${sent}`, {
282
308
  method,
283
309
  headers: {
284
310
  authorization: `Bearer ${this.opts.token}`,
311
+ ...this.brandHeader,
285
312
  'user-agent': `@awesomate/sdk/${VERSION}`,
286
313
  // Knowledge answers stream to a browser; asked for JSON, the hub sends the whole answer.
287
314
  accept: 'application/json',
@@ -313,6 +340,7 @@ export class AwesomateClient {
313
340
  method,
314
341
  headers: {
315
342
  authorization: `Bearer ${this.opts.token}`,
343
+ ...this.brandHeader,
316
344
  'user-agent': `@awesomate/sdk/${VERSION}`,
317
345
  ...(body === undefined ? {} : { 'content-type': 'application/octet-stream' }),
318
346
  },
@@ -1631,6 +1659,41 @@ export class AwesomateClient {
1631
1659
  types() {
1632
1660
  return this.request('GET', '/api/my-crm/v1/rows/types');
1633
1661
  }
1662
+ /**
1663
+ * One record's or one person's history: every change with who made it, through what, and from what to
1664
+ * what, newest first. `kind` is a kind's key, or `contact` for a person. A token sees only the fields
1665
+ * AI may read; a Sensitive field says only that it changed. Page with `next` (pass it back as `before`).
1666
+ * Null when the hub answers not found (no such record). The account's token (crm:read) with the History feature
1667
+ * (`record_history`); an app key is refused.
1668
+ */
1669
+ async history(kind, id, options = {}) {
1670
+ this.historyNeedsToken();
1671
+ if (!kind || !id)
1672
+ throw new AwesomateError('validation', 'history needs the kind and the id.', 0, 'kind');
1673
+ if (kind === '.' || kind === '..' || String(id) === '.' || String(id) === '..')
1674
+ throw new AwesomateError('validation', 'history needs a real kind and id.', 0, 'kind');
1675
+ return nullIfMissing(this.request('GET', `/api/my-crm/v1/history/${encodeURIComponent(kind)}/${encodeURIComponent(String(id))}${historyQuery({ ...options })}`));
1676
+ }
1677
+ /**
1678
+ * The whole business's history, newest first, at most 31 days a call (the last 7 by default): people and
1679
+ * records, and, where the account has Account history, the business itself (map, people and access, keys,
1680
+ * agents, automations, Knowledge, decisions). `actor: 'agent:<id>'` is what one agent did and why;
1681
+ * `trace` is everything one request caused. The account owner's own token only (crm:read): anyone else
1682
+ * is refused with `owner_only`.
1683
+ */
1684
+ async accountHistory(options = {}) {
1685
+ this.historyNeedsToken();
1686
+ return this.request('GET', `/api/my-crm/v1/history${historyQuery({ ...options })}`);
1687
+ }
1688
+ /** Every kind of history item that can appear, as JSON Schema (the same catalogue each item's `type` comes from). */
1689
+ async historyTypes() {
1690
+ this.historyNeedsToken();
1691
+ return this.request('GET', '/api/my-crm/v1/history/types');
1692
+ }
1693
+ historyNeedsToken() {
1694
+ if (this.appKey)
1695
+ throw new AwesomateError('forbidden', 'History needs the account\'s token (amt_pat_... with crm:read), not an app key.', 0);
1696
+ }
1634
1697
  /**
1635
1698
  * Rows of one kind that this token can read, a page at a time. The account's token (crm:read,
1636
1699
  * every plan, Contacts on the account) or an app key, for the kinds it was made for.
@@ -2279,6 +2342,16 @@ const BUSINESS_DOC_KINDS = [
2279
2342
  'business_summary', 'products_services', 'usps', 'ideal_customer', 'buyer_persona', 'customer_journey', 'messaging', 'voice_guide',
2280
2343
  'faqs', 'objections', 'article_ideas', 'founder_background', 'testimonials', 'business_history', 'team_directory', 'brand_guide', 'website_brand',
2281
2344
  ];
2345
+ function historyQuery(options) {
2346
+ const q = new URLSearchParams();
2347
+ for (const [k, v] of Object.entries(options)) {
2348
+ if (v === undefined || v === null || v === '' || (Array.isArray(v) && !v.length))
2349
+ continue;
2350
+ q.set(k, Array.isArray(v) ? v.join(',') : String(v));
2351
+ }
2352
+ const s = q.toString();
2353
+ return s ? `?${s}` : '';
2354
+ }
2282
2355
  function briefFromWire(r) {
2283
2356
  const { session_id, job_id, ...rest } = r;
2284
2357
  return { ...rest, ...(session_id ? { sessionId: session_id } : {}), ...(job_id ? { jobId: job_id } : {}) };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@awesomate/sdk",
3
- "version": "0.84.3",
3
+ "version": "0.84.6",
4
4
  "description": "Your own Awesomate data from Node and the browser: query contacts and app data with generated types, and sign your app's own users in",
5
5
  "license": "MIT",
6
6
  "type": "module",