@mappa-ai/mappa-node 1.1.1 → 1.2.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/dist/index.d.mts CHANGED
@@ -1,4 +1,9 @@
1
1
  //#region src/errors.d.ts
2
+ /**
3
+ * Symbol for custom Node.js inspect formatting.
4
+ * Ensures errors display nicely in console.log, REPL, and debuggers.
5
+ */
6
+ declare const customInspect: unique symbol;
2
7
  /**
3
8
  * Base error type for all SDK-raised errors.
4
9
  *
@@ -14,6 +19,8 @@ declare class MappaError extends Error {
14
19
  code?: string;
15
20
  cause?: unknown;
16
21
  });
22
+ toString(): string;
23
+ [customInspect](): string;
17
24
  }
18
25
  /**
19
26
  * Error returned when the API responds with a non-2xx status.
@@ -28,6 +35,7 @@ declare class ApiError extends MappaError {
28
35
  code?: string;
29
36
  details?: unknown;
30
37
  });
38
+ toString(): string;
31
39
  }
32
40
  /**
33
41
  * Error returned for HTTP 429 responses.
@@ -38,6 +46,7 @@ declare class ApiError extends MappaError {
38
46
  declare class RateLimitError extends ApiError {
39
47
  name: string;
40
48
  retryAfterMs?: number;
49
+ toString(): string;
41
50
  }
42
51
  /**
43
52
  * Error returned for authentication/authorization failures (typically 401/403).
@@ -51,6 +60,40 @@ declare class AuthError extends ApiError {
51
60
  declare class ValidationError extends ApiError {
52
61
  name: string;
53
62
  }
63
+ /**
64
+ * Error returned when the account lacks sufficient credits (HTTP 402).
65
+ *
66
+ * Use {@link InsufficientCreditsError.required} and {@link InsufficientCreditsError.available}
67
+ * to inform users how many credits are needed.
68
+ *
69
+ * @example
70
+ * ```typescript
71
+ * try {
72
+ * await mappa.reports.createJob({ ... });
73
+ * } catch (err) {
74
+ * if (err instanceof InsufficientCreditsError) {
75
+ * console.log(`Need ${err.required} credits, have ${err.available}`);
76
+ * }
77
+ * }
78
+ * ```
79
+ */
80
+ declare class InsufficientCreditsError extends ApiError {
81
+ name: string;
82
+ /** Credits required for the operation */
83
+ required: number;
84
+ /** Credits currently available */
85
+ available: number;
86
+ constructor(message: string, opts: {
87
+ status: number;
88
+ requestId?: string;
89
+ code?: string;
90
+ details?: {
91
+ required?: number;
92
+ available?: number;
93
+ };
94
+ });
95
+ toString(): string;
96
+ }
54
97
  /**
55
98
  * Error thrown by polling helpers when a job reaches the "failed" terminal state.
56
99
  */
@@ -62,6 +105,7 @@ declare class JobFailedError extends MappaError {
62
105
  code?: string;
63
106
  cause?: unknown;
64
107
  });
108
+ toString(): string;
65
109
  }
66
110
  /**
67
111
  * Error thrown by polling helpers when a job reaches the "canceled" terminal state.
@@ -73,9 +117,25 @@ declare class JobCanceledError extends MappaError {
73
117
  requestId?: string;
74
118
  cause?: unknown;
75
119
  });
120
+ toString(): string;
76
121
  }
77
122
  //#endregion
78
123
  //#region src/resources/transport.d.ts
124
+ /**
125
+ * Options for SSE streaming.
126
+ */
127
+ type SSEStreamOptions = {
128
+ signal?: AbortSignal;
129
+ lastEventId?: string;
130
+ };
131
+ /**
132
+ * A parsed SSE event.
133
+ */
134
+ type SSEEvent<T = unknown> = {
135
+ id?: string;
136
+ event: string;
137
+ data: T;
138
+ };
79
139
  type Telemetry = {
80
140
  onRequest?: (ctx: {
81
141
  method: string;
@@ -125,6 +185,30 @@ declare class Transport {
125
185
  private readonly opts;
126
186
  private readonly fetchImpl;
127
187
  constructor(opts: TransportOptions);
188
+ /**
189
+ * Stream SSE events from a given path.
190
+ *
191
+ * Uses native `fetch` with streaming response body (not browser-only `EventSource`).
192
+ * Parses SSE format manually from the `ReadableStream`.
193
+ */
194
+ streamSSE<T>(path: string, opts?: SSEStreamOptions): AsyncGenerator<SSEEvent<T>>;
195
+ /**
196
+ * Parse SSE events from a ReadableStream.
197
+ *
198
+ * SSE format:
199
+ * ```
200
+ * id: <id>
201
+ * event: <type>
202
+ * data: <json>
203
+ *
204
+ * ```
205
+ * Each event is terminated by an empty line.
206
+ */
207
+ private parseSSEStream;
208
+ /**
209
+ * Parse a single SSE event from text.
210
+ */
211
+ private parseSSEEvent;
128
212
  request<T>(req: RequestOptions): Promise<TransportResponse<T>>;
129
213
  }
130
214
  //#endregion
@@ -307,7 +391,7 @@ type ReportCreateJobRequest = {
307
391
  /**
308
392
  * Select the target entity for analysis.
309
393
  *
310
- * When omitted, the API defaults to the dominant speaker.
394
+ * @defaultValue `{ strategy: "dominant" }` - analyzes the dominant speaker
311
395
  */
312
396
  target?: TargetSelector;
313
397
  options?: {
@@ -332,6 +416,7 @@ type ReportCreateJobRequest = {
332
416
  type ReportBase = {
333
417
  id: string;
334
418
  createdAt: string;
419
+ jobId?: string;
335
420
  subject?: Subject;
336
421
  media: {
337
422
  url?: string;
@@ -348,12 +433,14 @@ type ReportBase = {
348
433
  type MarkdownReport = ReportBase & {
349
434
  output: {
350
435
  type: "markdown";
436
+ template: ReportTemplateId;
351
437
  };
352
438
  markdown: string;
353
439
  };
354
440
  type JsonReport = ReportBase & {
355
441
  output: {
356
442
  type: "json";
443
+ template: ReportTemplateId;
357
444
  };
358
445
  sections: Array<{
359
446
  section_title: string;
@@ -363,7 +450,7 @@ type JsonReport = ReportBase & {
363
450
  type PdfReport = ReportBase & {
364
451
  output: {
365
452
  type: "pdf";
366
- template: string;
453
+ template: ReportTemplateId;
367
454
  };
368
455
  markdown: string;
369
456
  pdfUrl: string;
@@ -371,10 +458,10 @@ type PdfReport = ReportBase & {
371
458
  type UrlReport = ReportBase & {
372
459
  output: {
373
460
  type: "url";
374
- template: string;
461
+ template: ReportTemplateId;
375
462
  };
376
- markdown?: string;
377
- sections?: Array<{
463
+ markdown: string;
464
+ sections: Array<{
378
465
  section_title: string;
379
466
  section_content: JsonValue;
380
467
  }>;
@@ -489,7 +576,7 @@ type CreditUsage = {
489
576
  modelVersion?: string;
490
577
  };
491
578
  /**
492
- * Options for long-polling job completion.
579
+ * Options for waiting on job completion.
493
580
  */
494
581
  type WaitOptions = {
495
582
  /**
@@ -498,18 +585,6 @@ type WaitOptions = {
498
585
  * @defaultValue 300000
499
586
  */
500
587
  timeoutMs?: number;
501
- /**
502
- * Initial polling interval.
503
- *
504
- * @defaultValue 1000
505
- */
506
- pollIntervalMs?: number;
507
- /**
508
- * Maximum polling interval used with exponential backoff.
509
- *
510
- * @defaultValue 10000
511
- */
512
- maxPollIntervalMs?: number;
513
588
  /**
514
589
  * Optional callback invoked on meaningful job state transitions.
515
590
  */
@@ -984,16 +1059,30 @@ declare class JobsResource {
984
1059
  requestId?: string;
985
1060
  signal?: AbortSignal;
986
1061
  }): Promise<Job>;
1062
+ /**
1063
+ * Wait for a job to reach a terminal state.
1064
+ *
1065
+ * Uses SSE streaming internally for efficient real-time updates.
1066
+ */
987
1067
  wait(jobId: string, opts?: WaitOptions): Promise<Job>;
988
1068
  /**
989
- * Public stream API.
990
- * If you add SSE later, keep this signature and switch implementation internally.
991
- * For now, it yields events based on polling. Use `AbortSignal` to cancel streaming.
1069
+ * Stream job events via SSE.
1070
+ *
1071
+ * Yields events as they arrive from the server. Use `AbortSignal` to cancel streaming.
1072
+ * Automatically handles reconnection with `Last-Event-ID` for up to 3 retries.
992
1073
  */
993
1074
  stream(jobId: string, opts?: {
994
1075
  signal?: AbortSignal;
995
1076
  onEvent?: (e: JobEvent) => void;
996
1077
  }): AsyncIterable<JobEvent>;
1078
+ /**
1079
+ * Map an SSE event to a JobEvent.
1080
+ */
1081
+ private mapSSEToJobEvent;
1082
+ /**
1083
+ * Exponential backoff with jitter for reconnection.
1084
+ */
1085
+ private backoff;
997
1086
  }
998
1087
  //#endregion
999
1088
  //#region src/resources/reports.d.ts
@@ -1078,6 +1167,7 @@ declare class ReportsResource {
1078
1167
  *
1079
1168
  * Behavior:
1080
1169
  * - Validates {@link MediaIdRef} at runtime (must provide `{ mediaId }`).
1170
+ * - Defaults to `{ strategy: "dominant" }` when `target` is omitted.
1081
1171
  * - Applies an idempotency key: uses `req.idempotencyKey` when provided; otherwise generates a best-effort default.
1082
1172
  * - Forwards `req.requestId` to the transport for end-to-end correlation.
1083
1173
  *
@@ -1244,5 +1334,21 @@ declare class Mappa {
1244
1334
  * Type guard for catching SDK errors.
1245
1335
  */
1246
1336
  declare function isMappaError(err: unknown): err is MappaError;
1337
+ /**
1338
+ * Type guard for insufficient credits errors.
1339
+ *
1340
+ * @example
1341
+ * ```typescript
1342
+ * try {
1343
+ * await mappa.reports.createJob({ ... });
1344
+ * } catch (err) {
1345
+ * if (isInsufficientCreditsError(err)) {
1346
+ * console.log(`Need ${err.required} credits, have ${err.available}`);
1347
+ * }
1348
+ * }
1349
+ * ```
1350
+ */
1351
+ declare function isInsufficientCreditsError(err: unknown): err is InsufficientCreditsError;
1247
1352
  //#endregion
1248
- export { ApiError, AuthError, CreditBalance, CreditTransaction, CreditTransactionType, CreditUsage, CursorPage, CursorPaginationParams, Entity, EntityTagsResult, FeedbackReceipt, FileDeleteReceipt, Job, JobCanceledError, JobCreditReservation, JobEvent, JobFailedError, JobStage, JobStatus, JsonReport, JsonValue, ListEntitiesOptions, ListEntitiesResponse, Mappa, MappaError, MarkdownReport, MediaFile, MediaIdRef, MediaObject, MediaProcessingStatus, MediaRef, MediaRetention, OffsetPage, OffsetPaginationParams, PdfReport, RateLimitError, Report, ReportBase, ReportCreateJobRequest, ReportJobReceipt, ReportOutput, ReportOutputType, ReportRunHandle, ReportTemplateId, ReportTemplateParamsMap, RetentionLockResult, Subject, TargetDominant, TargetEntityId, TargetFor, TargetMagicHint, TargetOnMiss, TargetSelector, TargetStrategy, TargetStrategyMap, TargetTimeRange, TargetTimeRangeStrategy, UrlReport, Usage, ValidationError, WaitOptions, WebhookConfig, hasEntity, isJsonReport, isMappaError, isMarkdownReport, isPdfReport, isUrlReport };
1353
+ export { ApiError, AuthError, CreditBalance, CreditTransaction, CreditTransactionType, CreditUsage, CursorPage, CursorPaginationParams, Entity, EntityTagsResult, FeedbackReceipt, FileDeleteReceipt, InsufficientCreditsError, Job, JobCanceledError, JobCreditReservation, JobEvent, JobFailedError, JobStage, JobStatus, JsonReport, JsonValue, ListEntitiesOptions, ListEntitiesResponse, Mappa, MappaError, MarkdownReport, MediaFile, MediaIdRef, MediaObject, MediaProcessingStatus, MediaRef, MediaRetention, OffsetPage, OffsetPaginationParams, PdfReport, RateLimitError, Report, ReportBase, ReportCreateJobRequest, ReportJobReceipt, ReportOutput, ReportOutputType, ReportRunHandle, ReportTemplateId, ReportTemplateParamsMap, RetentionLockResult, Subject, TargetDominant, TargetEntityId, TargetFor, TargetMagicHint, TargetOnMiss, TargetSelector, TargetStrategy, TargetStrategyMap, TargetTimeRange, TargetTimeRangeStrategy, UrlReport, Usage, ValidationError, WaitOptions, WebhookConfig, hasEntity, isInsufficientCreditsError, isJsonReport, isMappaError, isMarkdownReport, isPdfReport, isUrlReport };
1354
+ //# sourceMappingURL=index.d.mts.map