@framers/agentos-ext-gmail-source 0.3.0 → 0.5.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.
@@ -0,0 +1,203 @@
1
+ import { type SourceQuota, type SourceRevocations, type SourceSyncHandle, type SourceSyncScanPage, type SourceSyncHistoryPage, type SourceSyncReadResult, type SourceCachePermission, type SourceCacheDecision } from '@framers/agentos-ext-account-sources';
2
+ import type { GmailOAuthClient, GmailOAuthEndpoints } from './types.js';
3
+ import { type GmailMappingBounds } from './mime.js';
4
+ /**
5
+ * The client's defensive transport limits, each a positive safe integer no greater than the value in brackets. They
6
+ * are library choices, not Google maxima: a control page past one fails without a partial page.
7
+ */
8
+ export interface GmailApiLimits {
9
+ /** Milliseconds one call and the reading of its body may take (15,000). */
10
+ readonly timeoutMs: number;
11
+ /** Labels one labels response may hold (1,000). */
12
+ readonly maxLabels: number;
13
+ /** Message references one history page may hold across its five event fields (10,000). */
14
+ readonly maxHistoryReferences: number;
15
+ /** Characters of one page token, given or returned (4,096). */
16
+ readonly maxPageTokenCharacters: number;
17
+ }
18
+ /**
19
+ * What a `GmailApi` is built from. Every value comes from trusted server configuration and the host's released
20
+ * account-source services; none comes from a browser request.
21
+ *
22
+ * @typeParam Prepared - The host's prepared-document type of its `SourceRevocations`.
23
+ */
24
+ export interface GmailApiOptions<Prepared = unknown> {
25
+ /** The dedicated OAuth client the grant was issued to. */
26
+ readonly client: GmailOAuthClient;
27
+ /** The host's revocation service; its `withGrant` serializes every refresh under the durable grant lease. */
28
+ readonly revocations: Pick<SourceRevocations<Prepared>, 'withGrant'>;
29
+ /** Answers the sync handle current for this connection at each call, so a superseded handle is refused. */
30
+ readonly currentHandle: () => SourceSyncHandle;
31
+ /** The shared quota: each token refresh and each HTTP dispatch is one `run`, and provider waits go to `defer`. */
32
+ readonly quota: Pick<SourceQuota, 'run' | 'defer'>;
33
+ /** The opaque quota key of the project, 1 to 512 letters, digits, `:`, `_` or `-`. */
34
+ readonly projectKey: string;
35
+ /** Stable host HMAC key, never an address or a rotating connection id. */
36
+ readonly mailboxKey: string;
37
+ /** The thread mapper's bounds; `maxResponseBytes` also caps every API response before decoding. */
38
+ readonly bounds: GmailMappingBounds;
39
+ /** The transport and control-page limits. */
40
+ readonly limits: GmailApiLimits;
41
+ /** The fetch every token and API call goes through; the global fetch by default. */
42
+ readonly fetchImpl?: typeof fetch;
43
+ /**
44
+ * Epoch milliseconds; `Date.now` unless given. Give it the clock the host's sync store reads: a retry time this
45
+ * client hands back is always at least one millisecond after it, whichever clock produced the time.
46
+ */
47
+ readonly clock?: () => number;
48
+ /** CI-only transport endpoints. No request-supplied origin or client value. */
49
+ readonly apiOrigin?: string;
50
+ /** CI-only OAuth stand-in endpoints, checked as `GmailOAuthFlow` checks them. */
51
+ readonly oauthEndpoints?: GmailOAuthEndpoints;
52
+ /**
53
+ * Reuse of one access token for one sync lease, shared by every client given the same holder. A held token is used
54
+ * only while `currentHandle` answers the lease it was obtained under (the same connection, generation and lease
55
+ * token) and that lease has not expired by `clock`; a 401 drops it, and the repeat refreshes as before. Without a
56
+ * holder every operation refreshes the grant.
57
+ */
58
+ readonly tokenHolder?: GmailTokenHolder;
59
+ }
60
+ /**
61
+ * A memory-only slot for one access token and the sync lease it was obtained under, given to the clients built for
62
+ * one lease so that a cycle refreshes the grant once. Create it as `{ current: null }` for each lease and drop it when
63
+ * the lease ends; never persist, log or serialize it.
64
+ */
65
+ export interface GmailTokenHolder {
66
+ /** The held token and the key of its lease, written and cleared by the clients; null when none is held. */
67
+ current: {
68
+ readonly lease: string;
69
+ readonly value: string;
70
+ } | null;
71
+ }
72
+ /** Server-memory identity result. Never serialize it into a source or telemetry. */
73
+ export interface GmailProfile {
74
+ /** The mailbox address, for the host's private comparison only. */
75
+ readonly emailAddress: string;
76
+ /** The mailbox's current history id as a decimal string. */
77
+ readonly historyId: string;
78
+ }
79
+ /** Sanitized label names and the labels response's own cache decision; a refused decision holds no names. */
80
+ export interface GmailLabels {
81
+ /** Sanitized names by label id. */
82
+ readonly names: Readonly<Record<string, string>>;
83
+ /** The decision for the labels response, independent of any thread response. */
84
+ readonly cache: SourceCacheDecision;
85
+ }
86
+ /** The per-read values of one `readThread` call, passed to the thread mapper with the response's times and headers. */
87
+ export interface GmailReadOptions {
88
+ /** The selection cutoff in epoch milliseconds. */
89
+ readonly cutoff: number;
90
+ /** The message window in milliseconds. */
91
+ readonly windowMs: number;
92
+ /** The host's authenticated relative source-detail path; never a Gmail link. */
93
+ readonly sourceDetailUrl: string;
94
+ /** The host's evidenced cache permission; null refuses durable content. */
95
+ readonly permission: SourceCachePermission | null;
96
+ /** The cycle's label snapshot from `labels`, or null to keep label ids without names. */
97
+ readonly labels: GmailLabels | null;
98
+ /** Cancels the read. */
99
+ readonly signal?: AbortSignal;
100
+ }
101
+ /**
102
+ * A client's fixed refusal; its message is `gmail_api_<reason>`. `configuration` is thrown by the constructor;
103
+ * `response_too_large` stays inside the client, which answers it as a skipped thread or an invalid control page.
104
+ */
105
+ export declare class GmailApiFault extends Error {
106
+ readonly reason: 'configuration' | 'response_too_large';
107
+ /** @param reason Why the client refused. */
108
+ constructor(reason: 'configuration' | 'response_too_large');
109
+ }
110
+ /**
111
+ * The capped, read-only Gmail API client. Each provider operation makes one attempt: one OAuth refresh inside the
112
+ * host's grant operation, or the access token a `tokenHolder` holds for the current lease, then one HTTP dispatch, each
113
+ * admitted by the shared quota with a new attempt id; a 401 drops a held token and earns one fresh token and one
114
+ * repeat of the same call. Provider waits are recorded through the quota's `defer` and thrown as `rate_limited` or
115
+ * `provider_unavailable` with their time; the durable coordinator owns retry episodes, backoff and exhaustion. Every
116
+ * failure is a fixed `SourceSyncFault`.
117
+ *
118
+ * @typeParam Prepared - The host's prepared-document type of its `SourceRevocations`.
119
+ */
120
+ export declare class GmailApi<Prepared = unknown> {
121
+ private readonly options;
122
+ private readonly origin;
123
+ private readonly send;
124
+ private readonly bounds;
125
+ private readonly limits;
126
+ private readonly mapper;
127
+ /**
128
+ * @param options The client, the host's grant and quota services, the quota keys, the bounds and limits, and the
129
+ * optional fetch, clock and CI-only endpoints.
130
+ * @throws {GmailApiFault} `configuration` for an origin override outside CI or not a loopback or `.invalid` origin,
131
+ * a limit outside its ceiling, or a malformed quota key.
132
+ * @throws {GmailMappingFault} `configuration` for invalid bounds.
133
+ */
134
+ constructor(options: GmailApiOptions<Prepared>);
135
+ private now;
136
+ private later;
137
+ private pageToken;
138
+ private pageSize;
139
+ private quotaValue;
140
+ private reserve;
141
+ private defer;
142
+ private accessToken;
143
+ private dropToken;
144
+ /** Consume/cancel the whole body within the quota callback, before releasing its slot. */
145
+ private wire;
146
+ private request;
147
+ private control;
148
+ /**
149
+ * The mailbox address and current history id, for the host's private account binding and its history anchor. The
150
+ * answer's other fields are dropped.
151
+ *
152
+ * @param signal Cancels the call.
153
+ * @throws {SourceSyncFault} A fixed failure, `invalid_response` for a malformed answer.
154
+ */
155
+ profile(signal?: AbortSignal): Promise<GmailProfile>;
156
+ /**
157
+ * The mailbox's labels: sanitized names by id under the labels response's own cache decision. A refused decision
158
+ * keeps no names.
159
+ *
160
+ * @param permission The host's evidenced cache permission; null refuses the names.
161
+ * @param signal Cancels the call.
162
+ * @throws {SourceSyncFault} A fixed failure, `invalid_response` for a malformed or oversized list.
163
+ */
164
+ labels(permission: SourceCachePermission | null, signal?: AbortSignal): Promise<GmailLabels>;
165
+ /**
166
+ * One page of candidate thread ids between `cutoff - windowMs` and `cutoff`, outside spam, trash and drafts. The
167
+ * query's whole-second bounds are widened by a second each way; the mapper enforces the exact window. Snippets and
168
+ * estimates are dropped, and no mailbox-wide completeness is claimed.
169
+ *
170
+ * @param cutoff The selection cutoff in epoch milliseconds.
171
+ * @param windowMs The window, from 1 millisecond to 90 days.
172
+ * @param pageSize Candidates per page, from 1 to 100.
173
+ * @param pageToken The resume token from the previous page, or null for the first.
174
+ * @param signal Cancels the call.
175
+ * @throws {SourceSyncFault} A fixed failure, `invalid_response` for bad arguments, a malformed page or a repeated
176
+ * page token.
177
+ */
178
+ list(cutoff: number, windowMs: number, pageSize: number, pageToken: string | null, signal?: AbortSignal): Promise<SourceSyncScanPage>;
179
+ /**
180
+ * One page of history after `fromCursor`: the thread ids of every message addition, deletion and label change, and
181
+ * of generic message entries, deduplicated and sorted, with the page's decimal-string cursor. The host persists the
182
+ * pending work and commits a terminal cursor only once every item is terminal.
183
+ *
184
+ * @param fromCursor The committed history id, a decimal string.
185
+ * @param pageSize Records per page, from 1 to 100.
186
+ * @param pageToken The resume token from the previous page, or null for the first.
187
+ * @param signal Cancels the call.
188
+ * @throws {SourceSyncFault} `history_expired` when Google no longer holds the anchor; `invalid_response` for bad
189
+ * arguments, a malformed or out-of-order page, too many references or a repeated page token; another fixed failure.
190
+ */
191
+ history(fromCursor: string, pageSize: number, pageToken: string | null, signal?: AbortSignal): Promise<SourceSyncHistoryPage>;
192
+ /**
193
+ * Reads one thread in the `full` format and maps it with its own read times and cache headers and the separate
194
+ * label snapshot. A 404 is `gone`; an oversized successful answer is skipped as `response_too_large`; any other
195
+ * result is the mapper's. The raw response never leaves the client.
196
+ *
197
+ * @param sourceId The thread id, 1 to 64 hexadecimal characters.
198
+ * @param options The cutoff, window, detail path, cache permission, label snapshot and signal.
199
+ * @throws {SourceSyncFault} A fixed failure, `invalid_response` for a malformed id.
200
+ */
201
+ readThread(sourceId: string, options: GmailReadOptions): Promise<SourceSyncReadResult>;
202
+ }
203
+ //# sourceMappingURL=gmail.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"gmail.d.ts","sourceRoot":"","sources":["../src/gmail.ts"],"names":[],"mappings":"AAUA,OAAO,EAEL,KAAK,WAAW,EAAkD,KAAK,iBAAiB,EACxF,KAAK,gBAAgB,EAAE,KAAK,kBAAkB,EAAE,KAAK,qBAAqB,EAAE,KAAK,oBAAoB,EAC5E,KAAK,qBAAqB,EAAE,KAAK,mBAAmB,EAC9E,MAAM,sCAAsC,CAAC;AAE9C,OAAO,KAA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