@bayonai/sdk 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 BayonAI
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 ADDED
@@ -0,0 +1,19 @@
1
+ # @bayonai/sdk
2
+
3
+ TypeScript SDK for the BayonAI Firebase API.
4
+
5
+ ```ts
6
+ import { createBayonAiClient } from "@bayonai/sdk";
7
+
8
+ const bayonai = createBayonAiClient({
9
+ baseUrl: "https://api-q2ifyofooa-uc.a.run.app/api",
10
+ apiKey: process.env.BAYONAI_API_KEY,
11
+ });
12
+
13
+ const response = await bayonai.conversations.send({
14
+ personaId: "journalapp",
15
+ messages: [{ role: "user", content: "TEST" }],
16
+ });
17
+ ```
18
+
19
+ The SDK keeps the API's chat contract intact: `messages` is sent to BayonAI as a stringified JSON array.
package/dist/index.cjs ADDED
@@ -0,0 +1,268 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BayonAiError = void 0;
4
+ exports.createBayonAiClient = createBayonAiClient;
5
+ class BayonAiError extends Error {
6
+ details;
7
+ endpoint;
8
+ kind;
9
+ status;
10
+ constructor({ details, endpoint, kind, message, status, }) {
11
+ super(message);
12
+ this.name = "BayonAiError";
13
+ this.details = details;
14
+ this.endpoint = endpoint;
15
+ this.kind = kind;
16
+ this.status = status;
17
+ }
18
+ }
19
+ exports.BayonAiError = BayonAiError;
20
+ function createBayonAiClient(config) {
21
+ const transport = createTransport(config);
22
+ return {
23
+ a2a: {
24
+ agentCard: (personaId, options) => transport(`/agent/card/${encodePathPart(personaId)}`, {
25
+ method: "GET",
26
+ options,
27
+ }),
28
+ },
29
+ admin: {
30
+ contacts: {
31
+ send: (body, options) => post(transport, "/admin/contacts", body, options),
32
+ },
33
+ conversations: {
34
+ all: (body, options) => post(transport, "/admin/convo/all", body, options),
35
+ },
36
+ interfaces: {
37
+ add: (body, options) => post(transport, "/admin/interfaces/add", body, options),
38
+ all: (body, options) => post(transport, "/admin/interfaces/all", body, options),
39
+ delete: (body, options) => post(transport, "/admin/interfaces/delete", body, options),
40
+ update: (body, options) => post(transport, "/admin/interfaces/update", body, options),
41
+ },
42
+ organizations: {
43
+ all: (body = {}, options) => post(transport, "/admin/org/all", body, options),
44
+ customMany: (body, options) => post(transport, "/admin/org/custom/many", body, options),
45
+ customSet: (body, options) => post(transport, "/admin/org/custom/set", body, options),
46
+ mine: (body = {}, options) => post(transport, "/admin/org/mine", body, options),
47
+ role: (body = {}, options) => post(transport, "/admin/org/role", body, options),
48
+ },
49
+ personas: {
50
+ createOrUpdate: (body, options) => post(transport, "/admin/personas", body, options),
51
+ get: (body, options) => post(transport, "/admin/personas/get", body, options),
52
+ set: (body, options) => post(transport, "/admin/personas/set", body, options),
53
+ systemPrompt: (body, options) => post(transport, "/admin/personas/sys_prompt", body, options),
54
+ },
55
+ stats: {
56
+ events: (body, options) => post(transport, "/admin/stats/events", body, options),
57
+ requests: (body, options) => post(transport, "/admin/stats/requests", body, options),
58
+ },
59
+ tracking: {
60
+ consumption: (body, options) => post(transport, "/admin/tracking/consumption", body, options),
61
+ events: (body, options) => post(transport, "/admin/tracking/events", body, options),
62
+ requests: (body, options) => post(transport, "/admin/tracking/requests", body, options),
63
+ usage: (body, options) => post(transport, "/admin/tracking/usage", body, options),
64
+ },
65
+ users: {
66
+ all: (body, options) => post(transport, "/admin/users/all", body, options),
67
+ current: (body = {}, options) => post(transport, "/admin/users/current", body, options),
68
+ get: (body, options) => post(transport, "/admin/users/get", body, options),
69
+ stats: (body, options) => post(transport, "/admin/users/stats", body, options),
70
+ },
71
+ },
72
+ agents: {
73
+ assigned: (body = {}, options) => post(transport, "/agents/assigned", body, options),
74
+ get: (id, options) => transport("/agents/get", {
75
+ method: "GET",
76
+ options,
77
+ query: { id },
78
+ }),
79
+ getPost: (id, options) => post(transport, "/agents/get", { id }, options),
80
+ },
81
+ conversations: {
82
+ all: (body = {}, options) => post(transport, "/convo/all", body, options),
83
+ close: (convoId, options) => post(transport, "/convo/close", { convoId }, options),
84
+ last: (body, options) => post(transport, "/convo/last", body, options),
85
+ own: (convoId, options) => post(transport, "/convo/own", { convoId }, options),
86
+ send: (body, options) => post(transport, "/convo/send", {
87
+ ...omitUndefined(body),
88
+ messages: JSON.stringify(body.messages),
89
+ }, options),
90
+ star: (convoId, star, options) => post(transport, "/convo/star", { convoId, star }, options),
91
+ summary: (convoId, options) => post(transport, "/convo/summary", { convoId }, options),
92
+ title: (convoId, options) => post(transport, "/convo/title", { convoId }, options),
93
+ setTitle: (convoId, title, options) => post(transport, "/convo/title/set", { convoId, title }, options),
94
+ },
95
+ externalAuth: {
96
+ createToken: (body, options) => post(transport, "/external/auth/token", body, options),
97
+ },
98
+ externalUsers: {
99
+ me: (options) => transport("/external/users/me", { method: "GET", options }),
100
+ },
101
+ feedback: {
102
+ submit: (body, options) => post(transport, "/feedback", body, options),
103
+ },
104
+ platform: {
105
+ const: {
106
+ get: (name, options) => transport(`/const/${encodePathPart(name)}`, {
107
+ method: "GET",
108
+ options,
109
+ }),
110
+ },
111
+ data: {
112
+ reload: (body = {}, options) => post(transport, "/data/reload", body, options),
113
+ },
114
+ log: {
115
+ write: (body = {}, options) => post(transport, "/log", body, options),
116
+ },
117
+ messenger: {
118
+ receive: (body, options) => post(transport, "/messenger/receive", body, options),
119
+ send: (body, options) => post(transport, "/messenger/send", body, options),
120
+ webhook: (query = {}, options) => transport("/messenger/webhook", {
121
+ method: "GET",
122
+ options,
123
+ query,
124
+ }),
125
+ },
126
+ respondIo: {
127
+ convoClosed: (orgId, body, options) => post(transport, `/respondio/${encodePathPart(orgId)}/convo_closed`, body, options),
128
+ convoOpened: (orgId, body, options) => post(transport, `/respondio/${encodePathPart(orgId)}/convo_opened`, body, options),
129
+ log: (orgId, body = {}, options) => post(transport, `/respondio/${encodePathPart(orgId)}/log`, body, options),
130
+ messageReceived: (orgId, body, options) => post(transport, `/respondio/${encodePathPart(orgId)}/message_received`, body, options),
131
+ messageSent: (orgId, body, options) => post(transport, `/respondio/${encodePathPart(orgId)}/message_sent`, body, options),
132
+ },
133
+ twilio: {
134
+ receive: (body, options) => post(transport, "/twilio/receive", body, options),
135
+ send: (query = {}, options) => transport("/twilio/send", { method: "GET", options, query }),
136
+ },
137
+ whatsapp: {
138
+ send: (body, options) => post(transport, "/whatsapp/send", body, options),
139
+ subscribe: (query = {}, options) => transport("/whatsapp/subscribe", {
140
+ method: "GET",
141
+ options,
142
+ query,
143
+ }),
144
+ webhook: (body, options) => post(transport, "/whatsapp/webhook", body, options),
145
+ },
146
+ },
147
+ raw: {
148
+ get: (path, query, options) => transport(path, { method: "GET", options, query }),
149
+ post: (path, body, options) => transport(path, { body, method: "POST", options }),
150
+ },
151
+ stats: {
152
+ current: (query = {}, options) => transport("/stats", { method: "GET", options, query }),
153
+ events: (body, options) => post(transport, "/stats/events", body, options),
154
+ },
155
+ subscriptions: {
156
+ extend: (body, options) => post(transport, "/subscriptions/extend", body, options),
157
+ level: (body, options) => post(transport, "/subscriptions/level", body, options),
158
+ },
159
+ };
160
+ }
161
+ function createTransport(config) {
162
+ return async function request(endpoint, { body, method = "POST", options, query } = {}) {
163
+ const fetchImpl = config.fetch ?? globalThis.fetch;
164
+ if (!fetchImpl) {
165
+ throw new BayonAiError({
166
+ endpoint,
167
+ kind: "config",
168
+ message: "A fetch implementation is required.",
169
+ });
170
+ }
171
+ const url = buildUrl(config.baseUrl, endpoint, query);
172
+ let response;
173
+ try {
174
+ response = await fetchImpl(url, {
175
+ method,
176
+ headers: buildHeaders(config, options, body !== undefined),
177
+ ...(body === undefined ? {} : { body: JSON.stringify(omitUndefined(body)) }),
178
+ });
179
+ }
180
+ catch (error) {
181
+ throw new BayonAiError({
182
+ details: sanitizeDetails(error),
183
+ endpoint,
184
+ kind: "network",
185
+ message: `BayonAI request failed for ${endpoint}.`,
186
+ });
187
+ }
188
+ const data = await readResponseJson(response, endpoint);
189
+ if (!response.ok) {
190
+ throw new BayonAiError({
191
+ details: sanitizeDetails(data),
192
+ endpoint,
193
+ kind: "http",
194
+ message: `BayonAI request failed with HTTP ${response.status}.`,
195
+ status: response.status,
196
+ });
197
+ }
198
+ return data;
199
+ };
200
+ }
201
+ function post(transport, endpoint, body, options) {
202
+ return transport(endpoint, { body, method: "POST", options });
203
+ }
204
+ function buildHeaders(config, options, hasBody) {
205
+ const headers = {
206
+ ...(hasBody ? { "Content-Type": "application/json" } : {}),
207
+ ...(options?.headers ?? {}),
208
+ };
209
+ if (options?.bearerToken) {
210
+ headers.Authorization = `Bearer ${options.bearerToken}`;
211
+ return headers;
212
+ }
213
+ const apiKey = options?.apiKey ?? config.apiKey;
214
+ if (apiKey) {
215
+ headers["x-api-key"] = apiKey;
216
+ }
217
+ return headers;
218
+ }
219
+ function buildUrl(baseUrl, endpoint, query) {
220
+ const normalizedBase = baseUrl.replace(/\/+$/, "");
221
+ const normalizedEndpoint = endpoint.startsWith("/") ? endpoint : `/${endpoint}`;
222
+ const url = new URL(`${normalizedBase}${normalizedEndpoint}`);
223
+ Object.entries(query ?? {}).forEach(([key, value]) => {
224
+ if (value !== undefined) {
225
+ url.searchParams.set(key, String(value));
226
+ }
227
+ });
228
+ return url.toString();
229
+ }
230
+ function encodePathPart(value) {
231
+ return encodeURIComponent(value);
232
+ }
233
+ async function readResponseJson(response, endpoint) {
234
+ const contentType = response.headers.get("content-type") ?? "";
235
+ if (!contentType.includes("application/json")) {
236
+ if (response.ok) {
237
+ return response.text();
238
+ }
239
+ return null;
240
+ }
241
+ try {
242
+ return await response.json();
243
+ }
244
+ catch (error) {
245
+ throw new BayonAiError({
246
+ details: sanitizeDetails(error),
247
+ endpoint,
248
+ kind: "parse",
249
+ message: `BayonAI returned invalid JSON for ${endpoint}.`,
250
+ status: response.status,
251
+ });
252
+ }
253
+ }
254
+ function omitUndefined(value) {
255
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
256
+ return value;
257
+ }
258
+ return Object.fromEntries(Object.entries(value).filter(([, entryValue]) => entryValue !== undefined));
259
+ }
260
+ function sanitizeDetails(value) {
261
+ if (!value || typeof value !== "object") {
262
+ return value;
263
+ }
264
+ if (value instanceof Error) {
265
+ return { message: value.message };
266
+ }
267
+ return Object.fromEntries(Object.entries(value).filter(([key]) => !/(key|token|authorization|secret|password)/i.test(key)));
268
+ }
@@ -0,0 +1,191 @@
1
+ export type JsonRecord = Record<string, unknown>;
2
+ export type BayonAiClientConfig = {
3
+ apiKey?: string;
4
+ baseUrl: string;
5
+ fetch?: typeof fetch;
6
+ };
7
+ export type BayonAiRequestOptions = {
8
+ apiKey?: string;
9
+ bearerToken?: string;
10
+ headers?: Record<string, string>;
11
+ };
12
+ export type BayonAiChatMessage = {
13
+ role: string;
14
+ content: string;
15
+ metadata?: JsonRecord;
16
+ };
17
+ export type BayonAiAttachment = {
18
+ path: string;
19
+ contentType: string;
20
+ };
21
+ export type BayonAiChatRequest = {
22
+ attachments?: BayonAiAttachment[];
23
+ convoId?: string;
24
+ messages: BayonAiChatMessage[];
25
+ model?: string;
26
+ orgId?: string;
27
+ personaId: string;
28
+ };
29
+ export type BayonAiChatResponse = {
30
+ convoId?: string;
31
+ message: BayonAiChatMessage;
32
+ messages?: BayonAiChatMessage[];
33
+ };
34
+ export type ExternalAuthTokenRequest = {
35
+ bio?: string;
36
+ displayName?: string;
37
+ email?: string;
38
+ expiresIn?: number;
39
+ externalUserId: string;
40
+ metadata?: JsonRecord;
41
+ photoURL?: string;
42
+ subscription?: JsonRecord;
43
+ };
44
+ export type ExternalAuthTokenResponse = {
45
+ accessToken: string;
46
+ expiresIn: number;
47
+ tokenType: string;
48
+ user: JsonRecord;
49
+ };
50
+ export type BayonAiErrorKind = "http" | "network" | "parse" | "config";
51
+ export declare class BayonAiError extends Error {
52
+ readonly details?: unknown;
53
+ readonly endpoint: string;
54
+ readonly kind: BayonAiErrorKind;
55
+ readonly status?: number;
56
+ constructor({ details, endpoint, kind, message, status, }: {
57
+ details?: unknown;
58
+ endpoint: string;
59
+ kind: BayonAiErrorKind;
60
+ message: string;
61
+ status?: number;
62
+ });
63
+ }
64
+ export declare function createBayonAiClient(config: BayonAiClientConfig): {
65
+ a2a: {
66
+ agentCard: <T = JsonRecord>(personaId: string, options?: BayonAiRequestOptions) => Promise<T>;
67
+ };
68
+ admin: {
69
+ contacts: {
70
+ send: <T = JsonRecord>(body: JsonRecord, options?: BayonAiRequestOptions) => Promise<T>;
71
+ };
72
+ conversations: {
73
+ all: <T = JsonRecord>(body: JsonRecord, options?: BayonAiRequestOptions) => Promise<T>;
74
+ };
75
+ interfaces: {
76
+ add: <T = JsonRecord>(body: JsonRecord, options?: BayonAiRequestOptions) => Promise<T>;
77
+ all: <T = JsonRecord>(body: JsonRecord, options?: BayonAiRequestOptions) => Promise<T>;
78
+ delete: <T = JsonRecord>(body: JsonRecord, options?: BayonAiRequestOptions) => Promise<T>;
79
+ update: <T = JsonRecord>(body: JsonRecord, options?: BayonAiRequestOptions) => Promise<T>;
80
+ };
81
+ organizations: {
82
+ all: <T = JsonRecord>(body?: JsonRecord, options?: BayonAiRequestOptions) => Promise<T>;
83
+ customMany: <T = JsonRecord>(body: JsonRecord, options?: BayonAiRequestOptions) => Promise<T>;
84
+ customSet: <T = JsonRecord>(body: JsonRecord, options?: BayonAiRequestOptions) => Promise<T>;
85
+ mine: <T = JsonRecord>(body?: JsonRecord, options?: BayonAiRequestOptions) => Promise<T>;
86
+ role: <T = {
87
+ role?: string;
88
+ }>(body?: JsonRecord, options?: BayonAiRequestOptions) => Promise<T>;
89
+ };
90
+ personas: {
91
+ createOrUpdate: <T = JsonRecord>(body: JsonRecord, options?: BayonAiRequestOptions) => Promise<T>;
92
+ get: <T = JsonRecord>(body: JsonRecord, options?: BayonAiRequestOptions) => Promise<T>;
93
+ set: <T = JsonRecord>(body: JsonRecord, options?: BayonAiRequestOptions) => Promise<T>;
94
+ systemPrompt: <T = JsonRecord>(body: JsonRecord, options?: BayonAiRequestOptions) => Promise<T>;
95
+ };
96
+ stats: {
97
+ events: <T = JsonRecord>(body: JsonRecord, options?: BayonAiRequestOptions) => Promise<T>;
98
+ requests: <T = JsonRecord>(body: JsonRecord, options?: BayonAiRequestOptions) => Promise<T>;
99
+ };
100
+ tracking: {
101
+ consumption: <T = JsonRecord>(body: JsonRecord, options?: BayonAiRequestOptions) => Promise<T>;
102
+ events: <T = JsonRecord>(body: JsonRecord, options?: BayonAiRequestOptions) => Promise<T>;
103
+ requests: <T = JsonRecord>(body: JsonRecord, options?: BayonAiRequestOptions) => Promise<T>;
104
+ usage: <T = JsonRecord>(body: JsonRecord, options?: BayonAiRequestOptions) => Promise<T>;
105
+ };
106
+ users: {
107
+ all: <T = JsonRecord>(body: JsonRecord, options?: BayonAiRequestOptions) => Promise<T>;
108
+ current: <T = JsonRecord>(body?: JsonRecord, options?: BayonAiRequestOptions) => Promise<T>;
109
+ get: <T = JsonRecord>(body: JsonRecord, options?: BayonAiRequestOptions) => Promise<T>;
110
+ stats: <T = JsonRecord>(body: JsonRecord, options?: BayonAiRequestOptions) => Promise<T>;
111
+ };
112
+ };
113
+ agents: {
114
+ assigned: <T = JsonRecord>(body?: JsonRecord, options?: BayonAiRequestOptions) => Promise<T>;
115
+ get: <T = JsonRecord>(id: string, options?: BayonAiRequestOptions) => Promise<T>;
116
+ getPost: <T = JsonRecord>(id: string, options?: BayonAiRequestOptions) => Promise<T>;
117
+ };
118
+ conversations: {
119
+ all: <T = JsonRecord>(body?: JsonRecord, options?: BayonAiRequestOptions) => Promise<T>;
120
+ close: <T = JsonRecord>(convoId: string, options?: BayonAiRequestOptions) => Promise<T>;
121
+ last: <T = JsonRecord>(body: JsonRecord, options?: BayonAiRequestOptions) => Promise<T>;
122
+ own: <T = JsonRecord>(convoId: string, options?: BayonAiRequestOptions) => Promise<T>;
123
+ send: (body: BayonAiChatRequest, options?: BayonAiRequestOptions) => Promise<BayonAiChatResponse>;
124
+ star: <T = JsonRecord>(convoId: string, star: boolean, options?: BayonAiRequestOptions) => Promise<T>;
125
+ summary: <T = {
126
+ convoId: string;
127
+ summary: string;
128
+ }>(convoId: string, options?: BayonAiRequestOptions) => Promise<T>;
129
+ title: <T = {
130
+ convoId: string;
131
+ title: string;
132
+ }>(convoId: string, options?: BayonAiRequestOptions) => Promise<T>;
133
+ setTitle: <T = {
134
+ convoId: string;
135
+ title: string;
136
+ }>(convoId: string, title: string, options?: BayonAiRequestOptions) => Promise<T>;
137
+ };
138
+ externalAuth: {
139
+ createToken: (body: ExternalAuthTokenRequest, options?: BayonAiRequestOptions) => Promise<ExternalAuthTokenResponse>;
140
+ };
141
+ externalUsers: {
142
+ me: <T = JsonRecord>(options?: BayonAiRequestOptions) => Promise<T>;
143
+ };
144
+ feedback: {
145
+ submit: <T = JsonRecord>(body: JsonRecord, options?: BayonAiRequestOptions) => Promise<T>;
146
+ };
147
+ platform: {
148
+ const: {
149
+ get: <T = JsonRecord>(name: string, options?: BayonAiRequestOptions) => Promise<T>;
150
+ };
151
+ data: {
152
+ reload: <T = JsonRecord>(body?: JsonRecord, options?: BayonAiRequestOptions) => Promise<T>;
153
+ };
154
+ log: {
155
+ write: <T = JsonRecord>(body?: JsonRecord, options?: BayonAiRequestOptions) => Promise<T>;
156
+ };
157
+ messenger: {
158
+ receive: <T = JsonRecord>(body: JsonRecord, options?: BayonAiRequestOptions) => Promise<T>;
159
+ send: <T = JsonRecord>(body: JsonRecord, options?: BayonAiRequestOptions) => Promise<T>;
160
+ webhook: <T = JsonRecord>(query?: Record<string, string | number | boolean | undefined>, options?: BayonAiRequestOptions) => Promise<T>;
161
+ };
162
+ respondIo: {
163
+ convoClosed: <T = JsonRecord>(orgId: string, body: JsonRecord, options?: BayonAiRequestOptions) => Promise<T>;
164
+ convoOpened: <T = JsonRecord>(orgId: string, body: JsonRecord, options?: BayonAiRequestOptions) => Promise<T>;
165
+ log: <T = JsonRecord>(orgId: string, body?: JsonRecord, options?: BayonAiRequestOptions) => Promise<T>;
166
+ messageReceived: <T = JsonRecord>(orgId: string, body: JsonRecord, options?: BayonAiRequestOptions) => Promise<T>;
167
+ messageSent: <T = JsonRecord>(orgId: string, body: JsonRecord, options?: BayonAiRequestOptions) => Promise<T>;
168
+ };
169
+ twilio: {
170
+ receive: <T = JsonRecord>(body: JsonRecord, options?: BayonAiRequestOptions) => Promise<T>;
171
+ send: <T = JsonRecord>(query?: Record<string, string | number | boolean | undefined>, options?: BayonAiRequestOptions) => Promise<T>;
172
+ };
173
+ whatsapp: {
174
+ send: <T = JsonRecord>(body: JsonRecord, options?: BayonAiRequestOptions) => Promise<T>;
175
+ subscribe: <T = JsonRecord>(query?: Record<string, string | number | boolean | undefined>, options?: BayonAiRequestOptions) => Promise<T>;
176
+ webhook: <T = JsonRecord>(body: JsonRecord, options?: BayonAiRequestOptions) => Promise<T>;
177
+ };
178
+ };
179
+ raw: {
180
+ get: <T = unknown>(path: string, query?: Record<string, string | number | boolean | undefined>, options?: BayonAiRequestOptions) => Promise<T>;
181
+ post: <T = unknown>(path: string, body?: unknown, options?: BayonAiRequestOptions) => Promise<T>;
182
+ };
183
+ stats: {
184
+ current: <T = JsonRecord>(query?: Record<string, string | number | boolean | undefined>, options?: BayonAiRequestOptions) => Promise<T>;
185
+ events: <T = JsonRecord>(body: JsonRecord, options?: BayonAiRequestOptions) => Promise<T>;
186
+ };
187
+ subscriptions: {
188
+ extend: <T = JsonRecord>(body: JsonRecord, options?: BayonAiRequestOptions) => Promise<T>;
189
+ level: <T = JsonRecord>(body: JsonRecord, options?: BayonAiRequestOptions) => Promise<T>;
190
+ };
191
+ };
package/dist/index.js ADDED
@@ -0,0 +1,263 @@
1
+ export class BayonAiError extends Error {
2
+ details;
3
+ endpoint;
4
+ kind;
5
+ status;
6
+ constructor({ details, endpoint, kind, message, status, }) {
7
+ super(message);
8
+ this.name = "BayonAiError";
9
+ this.details = details;
10
+ this.endpoint = endpoint;
11
+ this.kind = kind;
12
+ this.status = status;
13
+ }
14
+ }
15
+ export function createBayonAiClient(config) {
16
+ const transport = createTransport(config);
17
+ return {
18
+ a2a: {
19
+ agentCard: (personaId, options) => transport(`/agent/card/${encodePathPart(personaId)}`, {
20
+ method: "GET",
21
+ options,
22
+ }),
23
+ },
24
+ admin: {
25
+ contacts: {
26
+ send: (body, options) => post(transport, "/admin/contacts", body, options),
27
+ },
28
+ conversations: {
29
+ all: (body, options) => post(transport, "/admin/convo/all", body, options),
30
+ },
31
+ interfaces: {
32
+ add: (body, options) => post(transport, "/admin/interfaces/add", body, options),
33
+ all: (body, options) => post(transport, "/admin/interfaces/all", body, options),
34
+ delete: (body, options) => post(transport, "/admin/interfaces/delete", body, options),
35
+ update: (body, options) => post(transport, "/admin/interfaces/update", body, options),
36
+ },
37
+ organizations: {
38
+ all: (body = {}, options) => post(transport, "/admin/org/all", body, options),
39
+ customMany: (body, options) => post(transport, "/admin/org/custom/many", body, options),
40
+ customSet: (body, options) => post(transport, "/admin/org/custom/set", body, options),
41
+ mine: (body = {}, options) => post(transport, "/admin/org/mine", body, options),
42
+ role: (body = {}, options) => post(transport, "/admin/org/role", body, options),
43
+ },
44
+ personas: {
45
+ createOrUpdate: (body, options) => post(transport, "/admin/personas", body, options),
46
+ get: (body, options) => post(transport, "/admin/personas/get", body, options),
47
+ set: (body, options) => post(transport, "/admin/personas/set", body, options),
48
+ systemPrompt: (body, options) => post(transport, "/admin/personas/sys_prompt", body, options),
49
+ },
50
+ stats: {
51
+ events: (body, options) => post(transport, "/admin/stats/events", body, options),
52
+ requests: (body, options) => post(transport, "/admin/stats/requests", body, options),
53
+ },
54
+ tracking: {
55
+ consumption: (body, options) => post(transport, "/admin/tracking/consumption", body, options),
56
+ events: (body, options) => post(transport, "/admin/tracking/events", body, options),
57
+ requests: (body, options) => post(transport, "/admin/tracking/requests", body, options),
58
+ usage: (body, options) => post(transport, "/admin/tracking/usage", body, options),
59
+ },
60
+ users: {
61
+ all: (body, options) => post(transport, "/admin/users/all", body, options),
62
+ current: (body = {}, options) => post(transport, "/admin/users/current", body, options),
63
+ get: (body, options) => post(transport, "/admin/users/get", body, options),
64
+ stats: (body, options) => post(transport, "/admin/users/stats", body, options),
65
+ },
66
+ },
67
+ agents: {
68
+ assigned: (body = {}, options) => post(transport, "/agents/assigned", body, options),
69
+ get: (id, options) => transport("/agents/get", {
70
+ method: "GET",
71
+ options,
72
+ query: { id },
73
+ }),
74
+ getPost: (id, options) => post(transport, "/agents/get", { id }, options),
75
+ },
76
+ conversations: {
77
+ all: (body = {}, options) => post(transport, "/convo/all", body, options),
78
+ close: (convoId, options) => post(transport, "/convo/close", { convoId }, options),
79
+ last: (body, options) => post(transport, "/convo/last", body, options),
80
+ own: (convoId, options) => post(transport, "/convo/own", { convoId }, options),
81
+ send: (body, options) => post(transport, "/convo/send", {
82
+ ...omitUndefined(body),
83
+ messages: JSON.stringify(body.messages),
84
+ }, options),
85
+ star: (convoId, star, options) => post(transport, "/convo/star", { convoId, star }, options),
86
+ summary: (convoId, options) => post(transport, "/convo/summary", { convoId }, options),
87
+ title: (convoId, options) => post(transport, "/convo/title", { convoId }, options),
88
+ setTitle: (convoId, title, options) => post(transport, "/convo/title/set", { convoId, title }, options),
89
+ },
90
+ externalAuth: {
91
+ createToken: (body, options) => post(transport, "/external/auth/token", body, options),
92
+ },
93
+ externalUsers: {
94
+ me: (options) => transport("/external/users/me", { method: "GET", options }),
95
+ },
96
+ feedback: {
97
+ submit: (body, options) => post(transport, "/feedback", body, options),
98
+ },
99
+ platform: {
100
+ const: {
101
+ get: (name, options) => transport(`/const/${encodePathPart(name)}`, {
102
+ method: "GET",
103
+ options,
104
+ }),
105
+ },
106
+ data: {
107
+ reload: (body = {}, options) => post(transport, "/data/reload", body, options),
108
+ },
109
+ log: {
110
+ write: (body = {}, options) => post(transport, "/log", body, options),
111
+ },
112
+ messenger: {
113
+ receive: (body, options) => post(transport, "/messenger/receive", body, options),
114
+ send: (body, options) => post(transport, "/messenger/send", body, options),
115
+ webhook: (query = {}, options) => transport("/messenger/webhook", {
116
+ method: "GET",
117
+ options,
118
+ query,
119
+ }),
120
+ },
121
+ respondIo: {
122
+ convoClosed: (orgId, body, options) => post(transport, `/respondio/${encodePathPart(orgId)}/convo_closed`, body, options),
123
+ convoOpened: (orgId, body, options) => post(transport, `/respondio/${encodePathPart(orgId)}/convo_opened`, body, options),
124
+ log: (orgId, body = {}, options) => post(transport, `/respondio/${encodePathPart(orgId)}/log`, body, options),
125
+ messageReceived: (orgId, body, options) => post(transport, `/respondio/${encodePathPart(orgId)}/message_received`, body, options),
126
+ messageSent: (orgId, body, options) => post(transport, `/respondio/${encodePathPart(orgId)}/message_sent`, body, options),
127
+ },
128
+ twilio: {
129
+ receive: (body, options) => post(transport, "/twilio/receive", body, options),
130
+ send: (query = {}, options) => transport("/twilio/send", { method: "GET", options, query }),
131
+ },
132
+ whatsapp: {
133
+ send: (body, options) => post(transport, "/whatsapp/send", body, options),
134
+ subscribe: (query = {}, options) => transport("/whatsapp/subscribe", {
135
+ method: "GET",
136
+ options,
137
+ query,
138
+ }),
139
+ webhook: (body, options) => post(transport, "/whatsapp/webhook", body, options),
140
+ },
141
+ },
142
+ raw: {
143
+ get: (path, query, options) => transport(path, { method: "GET", options, query }),
144
+ post: (path, body, options) => transport(path, { body, method: "POST", options }),
145
+ },
146
+ stats: {
147
+ current: (query = {}, options) => transport("/stats", { method: "GET", options, query }),
148
+ events: (body, options) => post(transport, "/stats/events", body, options),
149
+ },
150
+ subscriptions: {
151
+ extend: (body, options) => post(transport, "/subscriptions/extend", body, options),
152
+ level: (body, options) => post(transport, "/subscriptions/level", body, options),
153
+ },
154
+ };
155
+ }
156
+ function createTransport(config) {
157
+ return async function request(endpoint, { body, method = "POST", options, query } = {}) {
158
+ const fetchImpl = config.fetch ?? globalThis.fetch;
159
+ if (!fetchImpl) {
160
+ throw new BayonAiError({
161
+ endpoint,
162
+ kind: "config",
163
+ message: "A fetch implementation is required.",
164
+ });
165
+ }
166
+ const url = buildUrl(config.baseUrl, endpoint, query);
167
+ let response;
168
+ try {
169
+ response = await fetchImpl(url, {
170
+ method,
171
+ headers: buildHeaders(config, options, body !== undefined),
172
+ ...(body === undefined ? {} : { body: JSON.stringify(omitUndefined(body)) }),
173
+ });
174
+ }
175
+ catch (error) {
176
+ throw new BayonAiError({
177
+ details: sanitizeDetails(error),
178
+ endpoint,
179
+ kind: "network",
180
+ message: `BayonAI request failed for ${endpoint}.`,
181
+ });
182
+ }
183
+ const data = await readResponseJson(response, endpoint);
184
+ if (!response.ok) {
185
+ throw new BayonAiError({
186
+ details: sanitizeDetails(data),
187
+ endpoint,
188
+ kind: "http",
189
+ message: `BayonAI request failed with HTTP ${response.status}.`,
190
+ status: response.status,
191
+ });
192
+ }
193
+ return data;
194
+ };
195
+ }
196
+ function post(transport, endpoint, body, options) {
197
+ return transport(endpoint, { body, method: "POST", options });
198
+ }
199
+ function buildHeaders(config, options, hasBody) {
200
+ const headers = {
201
+ ...(hasBody ? { "Content-Type": "application/json" } : {}),
202
+ ...(options?.headers ?? {}),
203
+ };
204
+ if (options?.bearerToken) {
205
+ headers.Authorization = `Bearer ${options.bearerToken}`;
206
+ return headers;
207
+ }
208
+ const apiKey = options?.apiKey ?? config.apiKey;
209
+ if (apiKey) {
210
+ headers["x-api-key"] = apiKey;
211
+ }
212
+ return headers;
213
+ }
214
+ function buildUrl(baseUrl, endpoint, query) {
215
+ const normalizedBase = baseUrl.replace(/\/+$/, "");
216
+ const normalizedEndpoint = endpoint.startsWith("/") ? endpoint : `/${endpoint}`;
217
+ const url = new URL(`${normalizedBase}${normalizedEndpoint}`);
218
+ Object.entries(query ?? {}).forEach(([key, value]) => {
219
+ if (value !== undefined) {
220
+ url.searchParams.set(key, String(value));
221
+ }
222
+ });
223
+ return url.toString();
224
+ }
225
+ function encodePathPart(value) {
226
+ return encodeURIComponent(value);
227
+ }
228
+ async function readResponseJson(response, endpoint) {
229
+ const contentType = response.headers.get("content-type") ?? "";
230
+ if (!contentType.includes("application/json")) {
231
+ if (response.ok) {
232
+ return response.text();
233
+ }
234
+ return null;
235
+ }
236
+ try {
237
+ return await response.json();
238
+ }
239
+ catch (error) {
240
+ throw new BayonAiError({
241
+ details: sanitizeDetails(error),
242
+ endpoint,
243
+ kind: "parse",
244
+ message: `BayonAI returned invalid JSON for ${endpoint}.`,
245
+ status: response.status,
246
+ });
247
+ }
248
+ }
249
+ function omitUndefined(value) {
250
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
251
+ return value;
252
+ }
253
+ return Object.fromEntries(Object.entries(value).filter(([, entryValue]) => entryValue !== undefined));
254
+ }
255
+ function sanitizeDetails(value) {
256
+ if (!value || typeof value !== "object") {
257
+ return value;
258
+ }
259
+ if (value instanceof Error) {
260
+ return { message: value.message };
261
+ }
262
+ return Object.fromEntries(Object.entries(value).filter(([key]) => !/(key|token|authorization|secret|password)/i.test(key)));
263
+ }
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@bayonai/sdk",
3
+ "version": "0.1.0",
4
+ "description": "TypeScript SDK for the BayonAI Firebase API.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "main": "./dist/index.cjs",
8
+ "module": "./dist/index.js",
9
+ "types": "./dist/index.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "import": "./dist/index.js",
14
+ "require": "./dist/index.cjs"
15
+ }
16
+ },
17
+ "files": [
18
+ "dist",
19
+ "README.md",
20
+ "LICENSE"
21
+ ],
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "git+https://github.com/BayonAI/bounded.git",
25
+ "directory": "packages/bayonai-sdk"
26
+ },
27
+ "bugs": {
28
+ "url": "https://github.com/BayonAI/bounded/issues"
29
+ },
30
+ "homepage": "https://bayonai.com",
31
+ "keywords": [
32
+ "bayonai",
33
+ "gai",
34
+ "firebase",
35
+ "ai",
36
+ "sdk"
37
+ ],
38
+ "scripts": {
39
+ "build": "tsc -p tsconfig.json && tsc -p tsconfig.cjs.json && node scripts/write-cjs.mjs",
40
+ "lint": "tsc --noEmit -p tsconfig.json",
41
+ "test": "vitest run",
42
+ "pack": "pnpm pack"
43
+ },
44
+ "devDependencies": {
45
+ "typescript": "^5",
46
+ "vitest": "^4.1.6"
47
+ }
48
+ }