@hirefinn/sdk 0.5.0 → 0.5.2

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.
@@ -1,7 +1,7 @@
1
1
  import { FinnApprovalTimeoutError } from './errors.js';
2
2
  import { afterStrategy, beforeStrategy, cursorStrategy, offsetStrategy, pageStrategy } from './pagination.js';
3
3
  import { Resource } from './resource.js';
4
- import { AgentsGenerated, ApprovalsGenerated, AudiencesGenerated, CallsGenerated, CasesGenerated, ChatGenerated, ContactsGenerated, DeploymentsGenerated, DoNotCallGenerated, FinnsGenerated, PhoneNumbersGenerated, VoicesGenerated, WalletGenerated, } from './generated/resources.js';
4
+ import { AgentsGenerated, ApprovalsGenerated, AssistantsGenerated, AudiencesGenerated, CallsGenerated, CasesGenerated, ChatGenerated, ContactsGenerated, DeploymentsGenerated, DoNotCallGenerated, PhoneNumbersGenerated, VoicesGenerated, WalletGenerated, } from './generated/resources.js';
5
5
  export { Crm, Dialer, Organization, Sales, Webhooks } from './generated/resources.js';
6
6
  // ---------------------------------------------------------------- Voice
7
7
  export class Calls extends CallsGenerated {
@@ -20,36 +20,41 @@ export class Calls extends CallsGenerated {
20
20
  return this.do('get', '/calls/{call_uuid}', { path: { call_uuid: callUuid } }, options);
21
21
  }
22
22
  }
23
- export class Finns extends FinnsGenerated {
23
+ /** Assistants (voice agents): `/assistants`. Reached as `client.assistants` (and, deprecated, `client.finns`). */
24
+ export class Assistants extends AssistantsGenerated {
24
25
  list(query, options) {
25
- return this.do('get', '/finns', { query }, options);
26
+ return this.do('get', '/assistants', { query }, options);
26
27
  }
27
28
  listAll(query, options) {
28
- return this.pager('/finns', offsetStrategy(), query, undefined, options);
29
+ return this.pager('/assistants', offsetStrategy(), query, undefined, options);
29
30
  }
30
31
  create(body, options) {
31
- return this.do('post', '/finns', { body }, options);
32
+ return this.do('post', '/assistants', { body }, options);
32
33
  }
33
- retrieve(finnId, options) {
34
- return this.do('get', '/finns/{finn_id}', { path: { finn_id: finnId } }, options);
34
+ retrieve(assistantId, options) {
35
+ return this.do('get', '/assistants/{assistant_id}', { path: { assistant_id: assistantId } }, options);
35
36
  }
36
- update(finnId, body, options) {
37
- return this.do('patch', '/finns/{finn_id}', { path: { finn_id: finnId }, body }, options);
37
+ update(assistantId, body, options) {
38
+ return this.do('patch', '/assistants/{assistant_id}', { path: { assistant_id: assistantId }, body }, options);
38
39
  }
39
- delete(finnId, options) {
40
- return this.do('delete', '/finns/{finn_id}', { path: { finn_id: finnId } }, options);
40
+ delete(assistantId, options) {
41
+ return this.do('delete', '/assistants/{assistant_id}', { path: { assistant_id: assistantId } }, options);
41
42
  }
42
- listKnowledgeBase(finnId, options) {
43
- return this.do('get', '/finns/{finn_id}/knowledge_base', { path: { finn_id: finnId } }, options);
43
+ listKnowledgeBase(assistantId, options) {
44
+ return this.do('get', '/assistants/{assistant_id}/knowledge_base', { path: { assistant_id: assistantId } }, options);
44
45
  }
45
46
  /** JSON body, or a FormData with the file for multipart upload. */
46
- addKnowledgeBaseDocument(finnId, body, options) {
47
- return this.do('post', '/finns/{finn_id}/knowledge_base', { path: { finn_id: finnId }, body }, options);
47
+ addKnowledgeBaseDocument(assistantId, body, options) {
48
+ return this.do('post', '/assistants/{assistant_id}/knowledge_base', { path: { assistant_id: assistantId }, body }, options);
48
49
  }
49
- postCallAnalysis(finnId, query, options) {
50
- return this.do('get', '/finns/{finn_id}/post_call_analysis', { path: { finn_id: finnId }, query }, options);
50
+ postCallAnalysis(assistantId, query, options) {
51
+ return this.do('get', '/assistants/{assistant_id}/post_call_analysis', { path: { assistant_id: assistantId }, query }, options);
51
52
  }
52
53
  }
54
+ /**
55
+ * @deprecated Renamed to {@link Assistants}; `Finns` is the same class and will be removed in a future major version.
56
+ */
57
+ export const Finns = Assistants;
53
58
  export class Voices extends VoicesGenerated {
54
59
  list(query, options) {
55
60
  return this.do('get', '/voices', { query }, options);
@@ -81,9 +81,9 @@ export interface RequestOptions {
81
81
  * Pass `false` to send none, even on an endpoint where the SDK would make one up.
82
82
  */
83
83
  idempotencyKey?: string | false;
84
- /** Org to act in (Finn-Org header), for tokens that can use more than one org. */
84
+ /** Org to act in (X-Org-Id header, formerly Finn-Org), for tokens that can use more than one org. */
85
85
  org?: string;
86
- /** API version date (Finn-Version header), e.g. `2026-11-01`. */
86
+ /** API version date (Api-Version header, formerly Finn-Version), e.g. `2026-11-01`. */
87
87
  version?: string;
88
88
  /** Retries after the first attempt. */
89
89
  maxRetries?: number;
@@ -2,21 +2,59 @@ import { FinnSdkError } from './errors.js';
2
2
  /**
3
3
  * Webhook signature checks.
4
4
  *
5
- * Finn signs every delivery with `X-Finn-Signature: t=<unix seconds>,v1=<hex HMAC-SHA256(secret, "<t>.<raw body>")>`
6
- * and names the event in `X-Finn-Event`. verifyWebhook() checks the signature in constant time and refuses
7
- * deliveries whose timestamp is too far from now, so a captured request cannot be replayed later.
5
+ * Finn signs every delivery with `X-Webhook-Signature: t=<unix seconds>,v1=<hex HMAC-SHA256(secret, "<t>.<raw body>")>`
6
+ * and names the event in `X-Webhook-Event`. Deliveries also carry the same values under the older `X-Finn-*` names
7
+ * until those are retired; the readers here take the neutral name first and fall back to the old one.
8
+ * verifyWebhook() checks the signature in constant time and refuses deliveries whose timestamp is too far from now,
9
+ * so a captured request cannot be replayed later.
8
10
  */
9
- /** Header that carries the signature. */
10
- export declare const WEBHOOK_SIGNATURE_HEADER = "X-Finn-Signature";
11
- /** Header that carries the event name. */
12
- export declare const WEBHOOK_EVENT_HEADER = "X-Finn-Event";
11
+ /** Webhook header names. Read these first. */
12
+ export declare const WEBHOOK_HEADERS: {
13
+ readonly signature: "X-Webhook-Signature";
14
+ readonly event: "X-Webhook-Event";
15
+ readonly delivery: "X-Webhook-Delivery";
16
+ readonly timestamp: "X-Webhook-Timestamp";
17
+ };
18
+ /** The older names for the same headers, sent alongside {@link WEBHOOK_HEADERS} until they are retired. */
19
+ export declare const LEGACY_WEBHOOK_HEADERS: {
20
+ readonly signature: "X-Finn-Signature";
21
+ readonly event: "X-Finn-Event";
22
+ readonly delivery: "X-Finn-Delivery";
23
+ readonly timestamp: "X-Finn-Timestamp";
24
+ };
25
+ export type WebhookHeaderKind = keyof typeof WEBHOOK_HEADERS;
26
+ /**
27
+ * Header that carries the signature (older name).
28
+ * @deprecated Use `WEBHOOK_HEADERS.signature` (`X-Webhook-Signature`); verifyWebhook() reads either.
29
+ */
30
+ export declare const WEBHOOK_SIGNATURE_HEADER: "X-Finn-Signature";
31
+ /**
32
+ * Header that carries the event name (older name).
33
+ * @deprecated Use `WEBHOOK_HEADERS.event` (`X-Webhook-Event`); readWebhookHeader() reads either.
34
+ */
35
+ export declare const WEBHOOK_EVENT_HEADER: "X-Finn-Event";
13
36
  /** Default largest gap, in seconds, between the signed timestamp and now. */
14
37
  export declare const DEFAULT_WEBHOOK_TOLERANCE_SECONDS = 300;
15
38
  /**
16
39
  * The body of a webhook delivery (the Finn envelope). Endpoints imported from Finn Chat with the `chat` envelope get
17
40
  * Finn Chat's own body instead: `{ id, event, createdAt, orgId, data }`.
18
41
  */
19
- export interface WebhookEvent<T = Record<string, unknown>> {
42
+ /**
43
+ * The fields a call webhook's `data` uses to name the agent (`call.completed`, `call.analyzed`, transcript events).
44
+ * `agent_id` / `agent_name` are the current names; servers before the rename send only `finn_id` / `finn_name`, and
45
+ * newer servers send both with the same values until the old names retire. Read `agent_id ?? finn_id`.
46
+ */
47
+ export interface WebhookAgentFields {
48
+ /** The agent (assistant) that handled the call. */
49
+ agent_id?: string | null;
50
+ /** The agent's name, where the event carries one. */
51
+ agent_name?: string | null;
52
+ /** @deprecated Old name of `agent_id`, same value. */
53
+ finn_id?: string | null;
54
+ /** @deprecated Old name of `agent_name`, same value. */
55
+ finn_name?: string | null;
56
+ }
57
+ export interface WebhookEvent<T = Record<string, unknown> & WebhookAgentFields> {
20
58
  /** e.g. `call.completed`, `contact.created`, `case.completed`. */
21
59
  event: string;
22
60
  /** Unique per event, and the same on every retry or redelivery of it: dedupe on it. */
@@ -33,10 +71,13 @@ export interface WebhookEvent<T = Record<string, unknown>> {
33
71
  export declare class FinnWebhookVerificationError extends FinnSdkError {
34
72
  constructor(message: string);
35
73
  }
74
+ /** The neutral name of {@link FinnWebhookVerificationError}: the same class, so `instanceof` works with either name. */
75
+ export declare const WebhookVerificationError: typeof FinnWebhookVerificationError;
76
+ export type WebhookVerificationError = FinnWebhookVerificationError;
36
77
  export interface VerifyWebhookOptions {
37
78
  /** The raw request body, exactly as received (not re-serialised JSON). */
38
79
  payload: string | Uint8Array;
39
- /** The `X-Finn-Signature` value, or the request's headers to read it from. */
80
+ /** The `X-Webhook-Signature` (or older `X-Finn-Signature`) value, or the request's headers to read it from. */
40
81
  header?: string | null;
41
82
  headers?: Headers | Record<string, string | string[] | undefined>;
42
83
  /** The endpoint's signing secret (`whsec_...`). Pass several while you rotate secrets. */
@@ -51,6 +92,13 @@ export declare function parseSignatureHeader(header: string): {
51
92
  timestamp: number;
52
93
  signatures: string[];
53
94
  };
95
+ /**
96
+ * Reads a webhook header from a request's headers (a `Headers` or a Node headers object, any case): the neutral
97
+ * `X-Webhook-*` name first, then the older `X-Finn-*` name. Returns null when neither is set.
98
+ *
99
+ * const event = readWebhookHeader(req.headers, 'event'); // 'call.completed'
100
+ */
101
+ export declare function readWebhookHeader(headers: VerifyWebhookOptions['headers'], kind: WebhookHeaderKind): string | null;
54
102
  /** Compares two byte arrays in time that depends only on their length. */
55
103
  export declare function timingSafeEqual(a: Uint8Array, b: Uint8Array): boolean;
56
104
  /** Hex HMAC-SHA256 of `<timestamp>.<payload>`, the way Finn signs deliveries. */
@@ -64,4 +112,4 @@ export declare function signWebhookPayload(secret: string, timestamp: number | s
64
112
  * ...
65
113
  * });
66
114
  */
67
- export declare function verifyWebhook<T = Record<string, unknown>>(options: VerifyWebhookOptions): Promise<WebhookEvent<T>>;
115
+ export declare function verifyWebhook<T = Record<string, unknown> & WebhookAgentFields>(options: VerifyWebhookOptions): Promise<WebhookEvent<T>>;
@@ -2,14 +2,36 @@ import { FinnSdkError } from './errors.js';
2
2
  /**
3
3
  * Webhook signature checks.
4
4
  *
5
- * Finn signs every delivery with `X-Finn-Signature: t=<unix seconds>,v1=<hex HMAC-SHA256(secret, "<t>.<raw body>")>`
6
- * and names the event in `X-Finn-Event`. verifyWebhook() checks the signature in constant time and refuses
7
- * deliveries whose timestamp is too far from now, so a captured request cannot be replayed later.
5
+ * Finn signs every delivery with `X-Webhook-Signature: t=<unix seconds>,v1=<hex HMAC-SHA256(secret, "<t>.<raw body>")>`
6
+ * and names the event in `X-Webhook-Event`. Deliveries also carry the same values under the older `X-Finn-*` names
7
+ * until those are retired; the readers here take the neutral name first and fall back to the old one.
8
+ * verifyWebhook() checks the signature in constant time and refuses deliveries whose timestamp is too far from now,
9
+ * so a captured request cannot be replayed later.
8
10
  */
9
- /** Header that carries the signature. */
10
- export const WEBHOOK_SIGNATURE_HEADER = 'X-Finn-Signature';
11
- /** Header that carries the event name. */
12
- export const WEBHOOK_EVENT_HEADER = 'X-Finn-Event';
11
+ /** Webhook header names. Read these first. */
12
+ export const WEBHOOK_HEADERS = {
13
+ signature: 'X-Webhook-Signature',
14
+ event: 'X-Webhook-Event',
15
+ delivery: 'X-Webhook-Delivery',
16
+ timestamp: 'X-Webhook-Timestamp',
17
+ };
18
+ /** The older names for the same headers, sent alongside {@link WEBHOOK_HEADERS} until they are retired. */
19
+ export const LEGACY_WEBHOOK_HEADERS = {
20
+ signature: 'X-Finn-Signature',
21
+ event: 'X-Finn-Event',
22
+ delivery: 'X-Finn-Delivery',
23
+ timestamp: 'X-Finn-Timestamp',
24
+ };
25
+ /**
26
+ * Header that carries the signature (older name).
27
+ * @deprecated Use `WEBHOOK_HEADERS.signature` (`X-Webhook-Signature`); verifyWebhook() reads either.
28
+ */
29
+ export const WEBHOOK_SIGNATURE_HEADER = LEGACY_WEBHOOK_HEADERS.signature;
30
+ /**
31
+ * Header that carries the event name (older name).
32
+ * @deprecated Use `WEBHOOK_HEADERS.event` (`X-Webhook-Event`); readWebhookHeader() reads either.
33
+ */
34
+ export const WEBHOOK_EVENT_HEADER = LEGACY_WEBHOOK_HEADERS.event;
13
35
  /** Default largest gap, in seconds, between the signed timestamp and now. */
14
36
  export const DEFAULT_WEBHOOK_TOLERANCE_SECONDS = 300;
15
37
  /** The delivery's signature is missing, malformed, wrong or too old. Answer the request with a 400. */
@@ -19,6 +41,8 @@ export class FinnWebhookVerificationError extends FinnSdkError {
19
41
  this.name = 'FinnWebhookVerificationError';
20
42
  }
21
43
  }
44
+ /** The neutral name of {@link FinnWebhookVerificationError}: the same class, so `instanceof` works with either name. */
45
+ export const WebhookVerificationError = FinnWebhookVerificationError;
22
46
  /** Splits `t=...,v1=...` into its timestamp and v1 signatures. */
23
47
  export function parseSignatureHeader(header) {
24
48
  let timestamp = NaN;
@@ -36,18 +60,27 @@ export function parseSignatureHeader(header) {
36
60
  }
37
61
  return { timestamp, signatures };
38
62
  }
39
- function readHeader(headers) {
40
- if (!headers)
41
- return null;
63
+ function readOne(headers, name) {
42
64
  if (typeof headers.get === 'function')
43
- return headers.get(WEBHOOK_SIGNATURE_HEADER);
44
- const want = WEBHOOK_SIGNATURE_HEADER.toLowerCase();
65
+ return headers.get(name) || null;
66
+ const want = name.toLowerCase();
45
67
  for (const [k, v] of Object.entries(headers)) {
46
68
  if (k.toLowerCase() === want)
47
- return Array.isArray(v) ? (v[0] ?? null) : (v ?? null);
69
+ return (Array.isArray(v) ? v[0] : v) || null;
48
70
  }
49
71
  return null;
50
72
  }
73
+ /**
74
+ * Reads a webhook header from a request's headers (a `Headers` or a Node headers object, any case): the neutral
75
+ * `X-Webhook-*` name first, then the older `X-Finn-*` name. Returns null when neither is set.
76
+ *
77
+ * const event = readWebhookHeader(req.headers, 'event'); // 'call.completed'
78
+ */
79
+ export function readWebhookHeader(headers, kind) {
80
+ if (!headers)
81
+ return null;
82
+ return readOne(headers, WEBHOOK_HEADERS[kind]) ?? readOne(headers, LEGACY_WEBHOOK_HEADERS[kind]);
83
+ }
51
84
  async function subtleCrypto() {
52
85
  const c = globalThis.crypto;
53
86
  if (c?.subtle)
@@ -107,12 +140,13 @@ export async function verifyWebhook(options) {
107
140
  const secrets = (typeof options.secret === 'string' ? [options.secret] : [...options.secret]).filter((s) => !!s);
108
141
  if (secrets.length === 0)
109
142
  throw new FinnWebhookVerificationError('Missing webhook signing secret.');
110
- const header = options.header ?? readHeader(options.headers);
111
- if (!header)
112
- throw new FinnWebhookVerificationError(`Missing ${WEBHOOK_SIGNATURE_HEADER} header.`);
143
+ const header = options.header ?? readWebhookHeader(options.headers, 'signature');
144
+ if (!header) {
145
+ throw new FinnWebhookVerificationError(`Missing ${WEBHOOK_HEADERS.signature} (or ${LEGACY_WEBHOOK_HEADERS.signature}) header.`);
146
+ }
113
147
  const { timestamp, signatures } = parseSignatureHeader(header);
114
148
  if (!Number.isFinite(timestamp) || signatures.length === 0) {
115
- throw new FinnWebhookVerificationError(`Malformed ${WEBHOOK_SIGNATURE_HEADER} header: expected t=<unix>,v1=<hex>.`);
149
+ throw new FinnWebhookVerificationError(`Malformed ${WEBHOOK_HEADERS.signature} header: expected t=<unix>,v1=<hex>.`);
116
150
  }
117
151
  const tolerance = options.toleranceSeconds ?? DEFAULT_WEBHOOK_TOLERANCE_SECONDS;
118
152
  const now = options.now ?? Math.floor(Date.now() / 1000);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hirefinn/sdk",
3
- "version": "0.5.0",
3
+ "version": "0.5.2",
4
4
  "description": "Typed TypeScript client for the Finn public API (hirefinn.ai).",
5
5
  "license": "MIT",
6
6
  "type": "module",