@bubblebridge/sdk 0.0.0-stage → 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 BubbleBridge
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,95 @@
1
- # Temporary Holding Version
1
+ # @bubblebridge/sdk
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The BubbleBridge SDK for agent builders. BubbleBridge is a proxy between users and AI agents. Your agent calls Gmail and Google Calendar through BubbleBridge. Your agent never receives the Google credentials of the user.
4
+
5
+ The package is TypeScript. It has no runtime dependency. It runs on Node.js 20 and later and in the browser.
6
+
7
+ ## Install
8
+
9
+ ```sh
10
+ npm install @bubblebridge/sdk
11
+ ```
12
+
13
+ ## Register your agent
14
+
15
+ You need a client ID and a client secret. Register your agent at `https://bubblebridge.io/build`. Keep the client secret in a secret store.
16
+
17
+ ## Send a user to the consent page
18
+
19
+ Create a `state` value and a PKCE verifier. Store both for at most ten minutes. Then send the browser to the consent page.
20
+
21
+ ```ts
22
+ import { createHash, randomBytes } from 'node:crypto';
23
+ import { BubbleBridge } from '@bubblebridge/sdk';
24
+
25
+ const bb = new BubbleBridge({ clientId, clientSecret });
26
+
27
+ const state = randomBytes(16).toString('base64url');
28
+ const codeVerifier = randomBytes(32).toString('base64url');
29
+ const codeChallenge = createHash('sha256').update(codeVerifier).digest('base64url');
30
+
31
+ const location = bb.oauth.authorizeUrl({
32
+ scopes: ['google.gmail.readonly', 'google.calendar.events'],
33
+ redirectUri,
34
+ state,
35
+ codeChallenge,
36
+ });
37
+ ```
38
+
39
+ The user clicks Allow. The browser opens `<redirectUri>?code=...&state=...`.
40
+
41
+ ## Exchange the code
42
+
43
+ Compare `state` with the stored value. Then exchange the code for the first token pair. Store the whole object under `user_id`.
44
+
45
+ ```ts
46
+ const tokens = await bb.oauth.exchangeCode({ code, codeVerifier, redirectUri });
47
+ await putUser(tokens.user_id, tokens);
48
+ ```
49
+
50
+ ## Call Google for a user
51
+
52
+ Load the stored token pair. Set `expires_in` to `0`. This forces a refresh on the first call. BubbleBridge rotates the refresh token on each refresh. `onTokensUpdated` runs after each rotation. Store the new pair every time.
53
+
54
+ ```ts
55
+ const user = bb.forUser(
56
+ { ...tokens, expires_in: 0 },
57
+ { onTokensUpdated: (fresh) => void putUser(userId, fresh) },
58
+ );
59
+
60
+ await user.google.gmail.send({ to, subject, text });
61
+ const messages = await user.google.gmail.listMessages('newer_than:1d', 20);
62
+ const events = await user.google.calendar.listEvents('primary', timeMin);
63
+ await user.google.calendar.createEvent('primary', { summary, start, end });
64
+ ```
65
+
66
+ Use one `UserClient` for all parallel calls of one user. The client runs one refresh at a time.
67
+
68
+ Use `request` for a Google path that has no helper function. Give the method, the provider path with its query string, and an optional JSON body.
69
+
70
+ ```ts
71
+ const message = await user.google.request('GET', `/gmail/v1/users/me/messages/${id}?format=metadata`);
72
+ ```
73
+
74
+ The proxy returns the Google response as is. The SDK returns it as `unknown`.
75
+
76
+ ## Scopes
77
+
78
+ | Scope | Access |
79
+ |---|---|
80
+ | `google.gmail.readonly` | Read messages. |
81
+ | `google.gmail.send` | Send messages. |
82
+ | `google.calendar.readonly` | Read events. |
83
+ | `google.calendar.events` | Read and create events. |
84
+
85
+ ## Errors
86
+
87
+ An API error throws `ApiError` with `status` and `body`. The `body` has `code`, `message`, and `request_id`. A Google error passes through the proxy and throws `UpstreamError` with the Google status and the body text. A network failure throws the `fetch` error.
88
+
89
+ ## Guide
90
+
91
+ The full builder guide is at `https://bubblebridge.io/builder-guide.txt`.
92
+
93
+ ## License
94
+
95
+ MIT
@@ -0,0 +1,17 @@
1
+ import type { Google } from './google.js';
2
+ /** One event with RFC 3339 start and end times. */
3
+ export interface NewEvent {
4
+ summary: string;
5
+ description?: string;
6
+ start: string;
7
+ end: string;
8
+ }
9
+ /** The Calendar helpers. `Google.calendar` holds it. */
10
+ export declare class Calendar {
11
+ private readonly google;
12
+ constructor(google: Google);
13
+ /** Lists the single events of a calendar that start at or after `timeMin`, in start order. */
14
+ listEvents(calendarId: string, timeMin: string): Promise<unknown>;
15
+ /** Creates the event. It returns the Calendar event resource. */
16
+ createEvent(calendarId: string, event: NewEvent): Promise<unknown>;
17
+ }
@@ -0,0 +1,25 @@
1
+ /** The Calendar helpers. `Google.calendar` holds it. */
2
+ export class Calendar {
3
+ google;
4
+ constructor(google) {
5
+ this.google = google;
6
+ }
7
+ /** Lists the single events of a calendar that start at or after `timeMin`, in start order. */
8
+ listEvents(calendarId, timeMin) {
9
+ const params = new URLSearchParams({
10
+ timeMin,
11
+ singleEvents: 'true',
12
+ orderBy: 'startTime',
13
+ });
14
+ return this.google.request('GET', `/calendar/v3/calendars/${calendarId}/events?${params}`);
15
+ }
16
+ /** Creates the event. It returns the Calendar event resource. */
17
+ createEvent(calendarId, event) {
18
+ return this.google.request('POST', `/calendar/v3/calendars/${calendarId}/events`, {
19
+ summary: event.summary,
20
+ description: event.description,
21
+ start: { dateTime: event.start },
22
+ end: { dateTime: event.end },
23
+ });
24
+ }
25
+ }
@@ -0,0 +1,14 @@
1
+ import type { ErrorBody } from './types.js';
2
+ /** An error body that the API returned. */
3
+ export declare class ApiError extends Error {
4
+ readonly status: number;
5
+ readonly body: ErrorBody;
6
+ constructor(status: number, body: ErrorBody);
7
+ }
8
+ /** A status and body that a provider returned through the proxy. */
9
+ export declare class UpstreamError extends Error {
10
+ readonly status: number;
11
+ readonly body: string;
12
+ constructor(status: number, body: string);
13
+ }
14
+ export declare function errorFromResponse(status: number, text: string): ApiError | UpstreamError;
package/dist/error.js ADDED
@@ -0,0 +1,36 @@
1
+ /** An error body that the API returned. */
2
+ export class ApiError extends Error {
3
+ status;
4
+ body;
5
+ constructor(status, body) {
6
+ super(`api error ${status} ${body.code}: ${body.message}`);
7
+ this.status = status;
8
+ this.body = body;
9
+ this.name = 'ApiError';
10
+ }
11
+ }
12
+ /** A status and body that a provider returned through the proxy. */
13
+ export class UpstreamError extends Error {
14
+ status;
15
+ body;
16
+ constructor(status, body) {
17
+ super(`upstream error ${status}: ${body}`);
18
+ this.status = status;
19
+ this.body = body;
20
+ this.name = 'UpstreamError';
21
+ }
22
+ }
23
+ export function errorFromResponse(status, text) {
24
+ const parsed = tryParseJson(text);
25
+ if (parsed?.error)
26
+ return new ApiError(status, parsed.error);
27
+ return new UpstreamError(status, text);
28
+ }
29
+ function tryParseJson(text) {
30
+ try {
31
+ return JSON.parse(text);
32
+ }
33
+ catch {
34
+ return undefined;
35
+ }
36
+ }