trotta-mail 0.1.2 → 0.2.0

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
@@ -58,6 +58,19 @@ console.log(msg?.extracted.codes[0].value); // "481920"
58
58
  that arrives once the wait has started, so a code that came first is missed. `after` also takes a message id.
59
59
  If the code is held for review, `wait` throws `MessageHeldError` at once; a human releases it in the app.
60
60
 
61
+ ## Drive: files an agent keeps and shares
62
+
63
+ ```ts
64
+ const file = await mail.drive.upload("report.pdf", bytes, { inboxId });
65
+ const link = await mail.drive.share({ fileId: file.id, access: "named", addresses: ["riley@trotta.email"] });
66
+
67
+ // A link someone sent: look, then keep what you need.
68
+ const found = await mail.drive.openLink("https://trotta.email/s#tds_...");
69
+ const data = await mail.drive.downloadShared(found.files[0].download_url!);
70
+ ```
71
+
72
+ Every file is screened before it is kept. A key's link is judged like mail it sends: some wait for a human.
73
+
61
74
  ## What an agent reads
62
75
 
63
76
  - The safe view: hidden text removed, instructions aimed at a model marked, risky links defanged.
package/dist/index.d.ts CHANGED
@@ -3,7 +3,7 @@
3
3
  * No dependencies. Works in Node 18+, Bun, Deno, Workers and the browser.
4
4
  * Docs: https://trotta.email/docs
5
5
  */
6
- export declare const VERSION = "0.1.2";
6
+ export declare const VERSION = "0.2.0";
7
7
  export declare const DEFAULT_BASE_URL = "https://api.trotta.email";
8
8
  export type Address = {
9
9
  address: string;
@@ -14,7 +14,7 @@ export type View = "safe" | "full";
14
14
  export type Folder = "inbox" | "held" | "sent" | "drafts" | "scheduled" | "spam" | "trash" | "all";
15
15
  export type Scope = "mail:read" | "mail:send" | "mail:full" | "guard:release" | "admin";
16
16
  export type MessageStatus = "delivered" | "held" | "rejected" | "draft" | "scheduled" | "sending" | "pending_approval" | "queued" | "sent" | "bounced" | "failed" | "blocked";
17
- export type EventType = "message.received" | "message.held" | "message.rejected" | "message.released" | "message.sent" | "message.delivered" | "message.bounced" | "message.complained" | "message.blocked" | "message.pending_approval" | "message.scheduled" | "message.schedule_failed" | "inbox.created" | "domain.verified";
17
+ export type EventType = "message.received" | "message.held" | "message.rejected" | "message.released" | "message.sent" | "message.delivered" | "message.bounced" | "message.complained" | "message.blocked" | "message.pending_approval" | "message.scheduled" | "message.schedule_failed" | "inbox.created" | "domain.verified" | "drive.file_added" | "drive.share_requested";
18
18
  /** A list answer: the items, plus the cursor for the next page (null on the last one). */
19
19
  export type Page<T> = T[] & {
20
20
  nextCursor: string | null;
@@ -692,8 +692,105 @@ export type Options = {
692
692
  baseUrl?: string;
693
693
  fetch?: typeof fetch;
694
694
  };
695
+ export type DriveFile = {
696
+ id: string;
697
+ inbox_id: string;
698
+ folder_id: string | null;
699
+ name: string;
700
+ size: number;
701
+ content_type: string;
702
+ verdict: "clean" | "caution";
703
+ reason: string | null;
704
+ warnings: string[];
705
+ has_text: boolean;
706
+ downloadable: boolean;
707
+ source: "upload" | "mail" | "link";
708
+ on_drive_link: boolean;
709
+ shared: boolean;
710
+ created_at: string;
711
+ trashed_at: string | null;
712
+ };
713
+ export type DriveFolder = {
714
+ id: string;
715
+ inbox_id: string;
716
+ name: string;
717
+ files: number;
718
+ shared: boolean;
719
+ created_at: string;
720
+ };
721
+ export type DriveShare = {
722
+ id: string;
723
+ inbox_id: string;
724
+ kind: "file" | "folder" | "drive";
725
+ file_id: string | null;
726
+ folder_id: string | null;
727
+ name: string | null;
728
+ url: string | null;
729
+ access: "link" | "named";
730
+ addresses: string[];
731
+ has_password: boolean;
732
+ expires_at: string | null;
733
+ /** "pending": a human approves it before it opens. */
734
+ status: "active" | "pending";
735
+ pending_reason: string | null;
736
+ disabled: string | null;
737
+ opens: number;
738
+ created_at: string;
739
+ };
740
+ export type DriveListing = {
741
+ inbox_id: string;
742
+ address: string;
743
+ access: "off" | "files" | "share";
744
+ can_share: boolean;
745
+ folders: DriveFolder[];
746
+ data: DriveFile[];
747
+ next_cursor: string | null;
748
+ drive_link: DriveShare | null;
749
+ usage: {
750
+ used: number | null;
751
+ limit: number | null;
752
+ };
753
+ max_file_bytes: number;
754
+ };
755
+ export type OpenedLink = {
756
+ link: {
757
+ id: string;
758
+ kind: "file" | "folder" | "drive";
759
+ access: "link" | "named";
760
+ expires_at: string | null;
761
+ };
762
+ owner: {
763
+ name: string | null;
764
+ address: string;
765
+ kind: "agent" | "human";
766
+ };
767
+ title: string;
768
+ files: {
769
+ id: string;
770
+ name: string;
771
+ size: number;
772
+ verdict: "clean" | "caution";
773
+ warnings: string[];
774
+ download_url: string | null;
775
+ text_url?: string | null;
776
+ }[];
777
+ download_expires_in: number;
778
+ };
779
+ export type ShareInput = {
780
+ fileId?: string;
781
+ folderId?: string;
782
+ /** The inbox's Drive link, instead of a file or folder. */
783
+ drive?: boolean;
784
+ inboxId?: string;
785
+ access?: "link" | "named";
786
+ addresses?: string[];
787
+ password?: string;
788
+ expiresInDays?: number;
789
+ };
695
790
  export type RequestOptions = {
696
791
  body?: unknown;
792
+ /** Bytes sent as they are, in place of `body`. */
793
+ raw?: Uint8Array | ArrayBuffer | Blob;
697
794
  query?: Record<string, unknown>;
698
795
  headers?: Record<string, string>;
699
796
  /** False for the few routes that take no key (sign-up, scan). */
@@ -711,6 +808,7 @@ export declare class TrottaMail {
711
808
  claimUrl: string | undefined;
712
809
  readonly inboxes: Inboxes;
713
810
  readonly messages: Messages;
811
+ readonly drive: Drive;
714
812
  readonly drafts: Drafts;
715
813
  readonly threads: Threads;
716
814
  readonly guard: Guard;
@@ -793,6 +891,84 @@ export declare class Inboxes {
793
891
  */
794
892
  wait(inboxId: string, params?: WaitParams): Promise<Message | null>;
795
893
  }
894
+ /**
895
+ * The files an inbox keeps and the links that share them. Every inbox has a Drive.
896
+ * A file is screened before it is kept. A key reaches a human's Drive only once that human opens it, and a key's
897
+ * sharing is judged like mail it sends: some links wait for a human (`status: "pending"`).
898
+ * Docs: https://trotta.email/docs/drive
899
+ */
900
+ export declare class Drive {
901
+ private readonly client;
902
+ constructor(client: TrottaMail);
903
+ /** Files and folders. `all` lists every folder's files; a key limited to one inbox may leave `inboxId` out. */
904
+ list(params?: {
905
+ inboxId?: string;
906
+ folderId?: string;
907
+ q?: string;
908
+ trashed?: boolean;
909
+ all?: boolean;
910
+ limit?: number;
911
+ cursor?: string;
912
+ }): Promise<DriveListing>;
913
+ /** Keep a file. How large it may be, and how much in all, depends on the plan. */
914
+ upload(name: string, data: Uint8Array | ArrayBuffer | Blob, params?: {
915
+ inboxId?: string;
916
+ folderId?: string;
917
+ contentType?: string;
918
+ }): Promise<DriveFile>;
919
+ /** Keep an email attachment in Drive. */
920
+ saveAttachment(messageId: string, attachmentId: string, params?: {
921
+ inboxId?: string;
922
+ name?: string;
923
+ folderId?: string;
924
+ }): Promise<DriveFile>;
925
+ /** Keep a file from a link someone sent. It is screened again, for you. */
926
+ saveFromLink(link: string, params?: {
927
+ fileId?: string;
928
+ password?: string;
929
+ inboxId?: string;
930
+ name?: string;
931
+ folderId?: string;
932
+ }): Promise<DriveFile>;
933
+ get(fileId: string): Promise<DriveFile>;
934
+ /** The file's bytes. A key without mail:full is refused a file that was not screened in full. */
935
+ download(fileId: string): Promise<Uint8Array>;
936
+ /** The readable text inside a file (check `has_text`), with instructions aimed at an AI taken out. */
937
+ text(fileId: string, view?: View): Promise<AttachmentText>;
938
+ /** Rename a file, or put it on or take it off the Drive link. */
939
+ update(fileId: string, change: {
940
+ name?: string;
941
+ onDriveLink?: boolean;
942
+ }): Promise<DriveFile>;
943
+ /** Into a folder, or out of any (null). */
944
+ move(fileId: string, folderId: string | null): Promise<DriveFile>;
945
+ /** Move to the Drive trash; its links end. `permanent` takes a human, or a key with guard:release. */
946
+ delete(fileId: string, params?: {
947
+ permanent?: boolean;
948
+ }): Promise<void>;
949
+ restore(fileId: string): Promise<DriveFile>;
950
+ createFolder(name: string, params?: {
951
+ inboxId?: string;
952
+ }): Promise<DriveFolder>;
953
+ /** The folder goes; its files stay in the Drive. */
954
+ deleteFolder(folderId: string): Promise<void>;
955
+ shares(params?: {
956
+ inboxId?: string;
957
+ status?: DriveShare["status"];
958
+ }): Promise<Page<DriveShare>>;
959
+ /** A link to a file, a folder, or the Drive link. A key's link lasts 30 days unless you say, 90 at most. */
960
+ share(input: ShareInput): Promise<DriveShare>;
961
+ /** Turn a link off. It stops at once. */
962
+ unshare(shareId: string): Promise<void>;
963
+ /** Turn on a link that waits for a human (a human, or guard:release). */
964
+ approve(shareId: string): Promise<DriveShare>;
965
+ /** What a Drive link holds. Needs no key, except for a link made for named addresses. */
966
+ openLink(link: string, params?: {
967
+ password?: string;
968
+ }): Promise<OpenedLink>;
969
+ /** The bytes behind a `download_url` that `openLink` gave. Only addresses of this API are fetched. */
970
+ downloadShared(downloadUrl: string): Promise<Uint8Array>;
971
+ }
796
972
  export declare class Messages {
797
973
  private readonly client;
798
974
  constructor(client: TrottaMail);
package/dist/index.js CHANGED
@@ -3,7 +3,7 @@
3
3
  * No dependencies. Works in Node 18+, Bun, Deno, Workers and the browser.
4
4
  * Docs: https://trotta.email/docs
5
5
  */
6
- export const VERSION = "0.1.2";
6
+ export const VERSION = "0.2.0";
7
7
  export const DEFAULT_BASE_URL = "https://api.trotta.email";
8
8
  /** An error answered by the API. `code` is the API's code, `fix` says what to do next. */
9
9
  export class TrottaMailError extends Error {
@@ -210,6 +210,7 @@ export class TrottaMail {
210
210
  claimUrl;
211
211
  inboxes;
212
212
  messages;
213
+ drive;
213
214
  drafts;
214
215
  threads;
215
216
  guard;
@@ -227,6 +228,7 @@ export class TrottaMail {
227
228
  this.fetcher = given ? (input, init) => given(input, init) : (input, init) => fetch(input, init);
228
229
  this.inboxes = new Inboxes(this);
229
230
  this.messages = new Messages(this);
231
+ this.drive = new Drive(this);
230
232
  this.drafts = new Drafts(this);
231
233
  this.threads = new Threads(this);
232
234
  this.guard = new Guard(this);
@@ -284,10 +286,12 @@ export class TrottaMail {
284
286
  }
285
287
  if (opts.body !== undefined)
286
288
  headers["content-type"] = "application/json";
289
+ else if (opts.raw !== undefined)
290
+ headers["content-type"] ??= "application/octet-stream";
287
291
  const response = await this.fetcher(`${this.baseUrl}/v1${path}${query(opts.query ?? {})}`, {
288
292
  method,
289
293
  headers,
290
- body: opts.body !== undefined ? JSON.stringify(opts.body) : undefined,
294
+ body: opts.body !== undefined ? JSON.stringify(opts.body) : opts.raw,
291
295
  signal: opts.signal,
292
296
  // The key rides in a header. A redirect is never followed, so it can never be handed to another host.
293
297
  redirect: "error",
@@ -376,6 +380,113 @@ export class Inboxes {
376
380
  }
377
381
  }
378
382
  }
383
+ /** What comes after /v1/share/ in a download_url: nothing else is fetched. */
384
+ const SHARED = /^[A-Za-z0-9_]+\/files\/[A-Za-z0-9_]+(\?[A-Za-z0-9._=&-]*)?$/;
385
+ /**
386
+ * The files an inbox keeps and the links that share them. Every inbox has a Drive.
387
+ * A file is screened before it is kept. A key reaches a human's Drive only once that human opens it, and a key's
388
+ * sharing is judged like mail it sends: some links wait for a human (`status: "pending"`).
389
+ * Docs: https://trotta.email/docs/drive
390
+ */
391
+ export class Drive {
392
+ client;
393
+ constructor(client) {
394
+ this.client = client;
395
+ }
396
+ /** Files and folders. `all` lists every folder's files; a key limited to one inbox may leave `inboxId` out. */
397
+ list(params = {}) {
398
+ const query = {
399
+ inbox_id: params.inboxId, folder_id: params.folderId, q: params.q, trashed: params.trashed ? "true" : undefined,
400
+ all: params.all ? "true" : undefined, limit: params.limit, cursor: params.cursor,
401
+ };
402
+ return this.client.request("GET", "/drive", { query });
403
+ }
404
+ /** Keep a file. How large it may be, and how much in all, depends on the plan. */
405
+ upload(name, data, params = {}) {
406
+ const query = { name, inbox_id: params.inboxId, folder_id: params.folderId };
407
+ const headers = params.contentType ? { "content-type": params.contentType } : {};
408
+ return this.client.request("POST", "/drive/upload", { query, raw: data, headers });
409
+ }
410
+ /** Keep an email attachment in Drive. */
411
+ saveAttachment(messageId, attachmentId, params = {}) {
412
+ const body = { inbox_id: params.inboxId, name: params.name, folder_id: params.folderId, from_message: { message_id: messageId, attachment_id: attachmentId } };
413
+ return this.client.request("POST", "/drive/files", { body });
414
+ }
415
+ /** Keep a file from a link someone sent. It is screened again, for you. */
416
+ saveFromLink(link, params = {}) {
417
+ const body = { inbox_id: params.inboxId, name: params.name, folder_id: params.folderId, from_share: { link, file_id: params.fileId, password: params.password } };
418
+ return this.client.request("POST", "/drive/files", { body });
419
+ }
420
+ get(fileId) {
421
+ return this.client.request("GET", `/drive/files/${seg(fileId)}`);
422
+ }
423
+ /** The file's bytes. A key without mail:full is refused a file that was not screened in full. */
424
+ async download(fileId) {
425
+ const response = await this.client.fetchRaw("GET", `/drive/files/${seg(fileId)}/content`);
426
+ return new Uint8Array(await response.arrayBuffer());
427
+ }
428
+ /** The readable text inside a file (check `has_text`), with instructions aimed at an AI taken out. */
429
+ text(fileId, view = "safe") {
430
+ return this.client.request("GET", `/drive/files/${seg(fileId)}/text`, { query: { view } });
431
+ }
432
+ /** Rename a file, or put it on or take it off the Drive link. */
433
+ update(fileId, change) {
434
+ return this.client.request("PATCH", `/drive/files/${seg(fileId)}`, { body: { name: change.name, on_drive_link: change.onDriveLink } });
435
+ }
436
+ /** Into a folder, or out of any (null). */
437
+ move(fileId, folderId) {
438
+ return this.client.request("PATCH", `/drive/files/${seg(fileId)}`, { body: { folder_id: folderId } });
439
+ }
440
+ /** Move to the Drive trash; its links end. `permanent` takes a human, or a key with guard:release. */
441
+ delete(fileId, params = {}) {
442
+ return this.client.request("DELETE", `/drive/files/${seg(fileId)}`, { query: { permanent: params.permanent ? "true" : undefined } });
443
+ }
444
+ restore(fileId) {
445
+ return this.client.request("POST", `/drive/files/${seg(fileId)}/restore`);
446
+ }
447
+ createFolder(name, params = {}) {
448
+ return this.client.request("POST", "/drive/folders", { body: { name, inbox_id: params.inboxId } });
449
+ }
450
+ /** The folder goes; its files stay in the Drive. */
451
+ deleteFolder(folderId) {
452
+ return this.client.request("DELETE", `/drive/folders/${seg(folderId)}`);
453
+ }
454
+ async shares(params = {}) {
455
+ const query = { inbox_id: params.inboxId, status: params.status };
456
+ return page(await this.client.request("GET", "/drive/shares", { query }));
457
+ }
458
+ /** A link to a file, a folder, or the Drive link. A key's link lasts 30 days unless you say, 90 at most. */
459
+ share(input) {
460
+ const body = {
461
+ file_id: input.fileId, folder_id: input.folderId, drive: input.drive || undefined, inbox_id: input.inboxId,
462
+ access: input.access ?? "link", addresses: input.addresses, password: input.password, expires_in_days: input.expiresInDays,
463
+ };
464
+ return this.client.request("POST", "/drive/shares", { body });
465
+ }
466
+ /** Turn a link off. It stops at once. */
467
+ unshare(shareId) {
468
+ return this.client.request("DELETE", `/drive/shares/${seg(shareId)}`);
469
+ }
470
+ /** Turn on a link that waits for a human (a human, or guard:release). */
471
+ approve(shareId) {
472
+ return this.client.request("POST", `/drive/shares/${seg(shareId)}/approve`);
473
+ }
474
+ /** What a Drive link holds. Needs no key, except for a link made for named addresses. */
475
+ openLink(link, params = {}) {
476
+ return this.client.request("POST", "/share/open", { body: { link, password: params.password }, authenticated: Boolean(this.client.apiKey) });
477
+ }
478
+ /** The bytes behind a `download_url` that `openLink` gave. Only addresses of this API are fetched. */
479
+ async downloadShared(downloadUrl) {
480
+ const prefix = `${this.client.baseUrl}/v1/share/`;
481
+ const rest = downloadUrl.startsWith(prefix) ? downloadUrl.slice(prefix.length) : "";
482
+ if (!SHARED.test(rest))
483
+ throw new Error("That is not a download address of this API.");
484
+ const [path, search = ""] = rest.split("?");
485
+ const query = { ticket: new URLSearchParams(search).get("ticket") ?? undefined };
486
+ const response = await this.client.fetchRaw("GET", `/share/${path}`, { query, authenticated: false });
487
+ return new Uint8Array(await response.arrayBuffer());
488
+ }
489
+ }
379
490
  export class Messages {
380
491
  client;
381
492
  constructor(client) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "trotta-mail",
3
- "version": "0.1.2",
3
+ "version": "0.2.0",
4
4
  "description": "Trotta Mail: the secure pre-delivery inbox for AI agents and humans. Give an AI agent its own email inbox in one call, with every message screened before it is delivered. TypeScript SDK, no dependencies.",
5
5
  "keywords": [
6
6
  "email",