@drawbridge/drawbridge-utils 0.0.43 → 0.0.45

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/ai.cjs CHANGED
@@ -293,16 +293,10 @@ var google = {
293
293
  if (!content) {
294
294
  throw new Error("Could not generate from prompt. Please try again with a different prompt.");
295
295
  }
296
- if (user == null ? void 0 : user.id) {
297
- await billRequest({
298
- db,
299
- user,
300
- model,
301
- usage: tokensFromMetadata(response == null ? void 0 : response.usageMetadata),
302
- tools: toolsFromResponse(response)
303
- });
304
- }
305
- return { content };
296
+ const usage = tokensFromMetadata(response == null ? void 0 : response.usageMetadata);
297
+ const tools = toolsFromResponse(response);
298
+ const bill = (user == null ? void 0 : user.id) ? () => billRequest({ db, user, model, usage, tools }) : () => Promise.resolve();
299
+ return { content, bill };
306
300
  },
307
301
  text: async ({
308
302
  config = {},
@@ -324,16 +318,10 @@ var google = {
324
318
  if (!content) {
325
319
  throw new Error("Could not generate from prompt. Please try again with a different prompt.");
326
320
  }
327
- if (user == null ? void 0 : user.id) {
328
- await billRequest({
329
- db,
330
- user,
331
- model,
332
- usage: tokensFromMetadata(response == null ? void 0 : response.usageMetadata),
333
- tools: toolsFromResponse(response)
334
- });
335
- }
336
- return { content };
321
+ const usage = tokensFromMetadata(response == null ? void 0 : response.usageMetadata);
322
+ const tools = toolsFromResponse(response);
323
+ const bill = (user == null ? void 0 : user.id) ? () => billRequest({ db, user, model, usage, tools }) : () => Promise.resolve();
324
+ return { content, bill };
337
325
  }
338
326
  };
339
327
  // Annotate the CommonJS export names for ESM import in node:
package/dist/ai.d.cts CHANGED
@@ -247,19 +247,14 @@ const google = {
247
247
 
248
248
  }
249
249
 
250
- if( user?.id ){
250
+ const usage = tokensFromMetadata( response?.usageMetadata );
251
+ const tools = toolsFromResponse( response );
251
252
 
252
- await billRequest({
253
- db,
254
- user,
255
- model,
256
- usage : tokensFromMetadata( response?.usageMetadata ),
257
- tools : toolsFromResponse( response )
258
- });
253
+ const bill = user?.id
254
+ ? () => billRequest({ db, user, model, usage, tools })
255
+ : () => Promise.resolve();
259
256
 
260
- }
261
-
262
- return { content };
257
+ return { content, bill };
263
258
 
264
259
  },
265
260
 
@@ -287,25 +282,20 @@ const google = {
287
282
 
288
283
  const content = response?.text;
289
284
 
290
- if( !content ){
285
+ if( ! content ){
291
286
 
292
287
  throw new Error( 'Could not generate from prompt. Please try again with a different prompt.' );
293
288
 
294
289
  }
295
290
 
296
- if( user?.id ){
291
+ const usage = tokensFromMetadata( response?.usageMetadata );
292
+ const tools = toolsFromResponse( response );
297
293
 
298
- await billRequest({
299
- db,
300
- user,
301
- model,
302
- usage : tokensFromMetadata( response?.usageMetadata ),
303
- tools : toolsFromResponse( response )
304
- });
305
-
306
- }
294
+ const bill = user?.id
295
+ ? () => billRequest({ db, user, model, usage, tools })
296
+ : () => Promise.resolve();
307
297
 
308
- return { content };
298
+ return { content, bill };
309
299
 
310
300
  }
311
301
 
package/dist/ai.d.ts CHANGED
@@ -247,19 +247,14 @@ const google = {
247
247
 
248
248
  }
249
249
 
250
- if( user?.id ){
250
+ const usage = tokensFromMetadata( response?.usageMetadata );
251
+ const tools = toolsFromResponse( response );
251
252
 
252
- await billRequest({
253
- db,
254
- user,
255
- model,
256
- usage : tokensFromMetadata( response?.usageMetadata ),
257
- tools : toolsFromResponse( response )
258
- });
253
+ const bill = user?.id
254
+ ? () => billRequest({ db, user, model, usage, tools })
255
+ : () => Promise.resolve();
259
256
 
260
- }
261
-
262
- return { content };
257
+ return { content, bill };
263
258
 
264
259
  },
265
260
 
@@ -287,25 +282,20 @@ const google = {
287
282
 
288
283
  const content = response?.text;
289
284
 
290
- if( !content ){
285
+ if( ! content ){
291
286
 
292
287
  throw new Error( 'Could not generate from prompt. Please try again with a different prompt.' );
293
288
 
294
289
  }
295
290
 
296
- if( user?.id ){
291
+ const usage = tokensFromMetadata( response?.usageMetadata );
292
+ const tools = toolsFromResponse( response );
297
293
 
298
- await billRequest({
299
- db,
300
- user,
301
- model,
302
- usage : tokensFromMetadata( response?.usageMetadata ),
303
- tools : toolsFromResponse( response )
304
- });
305
-
306
- }
294
+ const bill = user?.id
295
+ ? () => billRequest({ db, user, model, usage, tools })
296
+ : () => Promise.resolve();
307
297
 
308
- return { content };
298
+ return { content, bill };
309
299
 
310
300
  }
311
301
 
package/dist/ai.js CHANGED
@@ -263,16 +263,10 @@ var google = {
263
263
  if (!content) {
264
264
  throw new Error("Could not generate from prompt. Please try again with a different prompt.");
265
265
  }
266
- if (user == null ? void 0 : user.id) {
267
- await billRequest({
268
- db,
269
- user,
270
- model,
271
- usage: tokensFromMetadata(response == null ? void 0 : response.usageMetadata),
272
- tools: toolsFromResponse(response)
273
- });
274
- }
275
- return { content };
266
+ const usage = tokensFromMetadata(response == null ? void 0 : response.usageMetadata);
267
+ const tools = toolsFromResponse(response);
268
+ const bill = (user == null ? void 0 : user.id) ? () => billRequest({ db, user, model, usage, tools }) : () => Promise.resolve();
269
+ return { content, bill };
276
270
  },
277
271
  text: async ({
278
272
  config = {},
@@ -294,16 +288,10 @@ var google = {
294
288
  if (!content) {
295
289
  throw new Error("Could not generate from prompt. Please try again with a different prompt.");
296
290
  }
297
- if (user == null ? void 0 : user.id) {
298
- await billRequest({
299
- db,
300
- user,
301
- model,
302
- usage: tokensFromMetadata(response == null ? void 0 : response.usageMetadata),
303
- tools: toolsFromResponse(response)
304
- });
305
- }
306
- return { content };
291
+ const usage = tokensFromMetadata(response == null ? void 0 : response.usageMetadata);
292
+ const tools = toolsFromResponse(response);
293
+ const bill = (user == null ? void 0 : user.id) ? () => billRequest({ db, user, model, usage, tools }) : () => Promise.resolve();
294
+ return { content, bill };
307
295
  }
308
296
  };
309
297
  export {
@@ -32,11 +32,14 @@ __export(oauth_exports, {
32
32
  compare: () => compare,
33
33
  createOAuthClient: () => createOAuthClient,
34
34
  developer: () => developer,
35
+ developerOptions: () => developerOptions,
35
36
  generateToken: () => generateToken,
36
37
  hashToken: () => hashToken,
37
38
  intersect: () => intersect,
38
39
  isDeveloperAllowed: () => isDeveloperAllowed,
39
40
  parse: () => parse,
41
+ scopeLabels: () => scopeLabels,
42
+ scopeOptions: () => scopeOptions,
40
43
  scopes: () => scopes,
41
44
  validate: () => validate
42
45
  });
@@ -113,6 +116,29 @@ var intersect = (requested, allowed) => {
113
116
  return r.filter((scope) => a.includes(scope));
114
117
  };
115
118
  var isDeveloperAllowed = (scope) => developer.includes(scope);
119
+ var scopeLabels = {
120
+ "profile:read": "Read own profile",
121
+ "profile:write": "Update own profile",
122
+ "organization:read": "Read organization info",
123
+ "organization:write": "Manage organization",
124
+ "campaigns:read": "Read campaigns",
125
+ "campaigns:write": "Manage campaigns",
126
+ "contacts:read": "Read contacts",
127
+ "contacts:write": "Manage contacts",
128
+ "workflows:read": "Read workflows",
129
+ "workflows:write": "Manage workflows",
130
+ "connections:read": "Read integrations",
131
+ "connections:write": "Manage integrations",
132
+ "billing:read": "Read billing",
133
+ "billing:write": "Manage billing",
134
+ admin: "Admin access"
135
+ };
136
+ var toOption = (scope) => ({
137
+ key: scopeLabels[scope] || scope,
138
+ value: scope
139
+ });
140
+ var developerOptions = developer.map(toOption);
141
+ var scopeOptions = scopes.map(toOption);
116
142
  var hashToken = (raw) => hash(raw, process.env.OAUTH_TOKEN_HMAC_KEY);
117
143
  var generateToken = () => generate(32, "base64url");
118
144
  var REFRESH_LEAD_SECONDS = 60;
@@ -186,11 +212,14 @@ var createOAuthClient = ({
186
212
  compare,
187
213
  createOAuthClient,
188
214
  developer,
215
+ developerOptions,
189
216
  generateToken,
190
217
  hashToken,
191
218
  intersect,
192
219
  isDeveloperAllowed,
193
220
  parse,
221
+ scopeLabels,
222
+ scopeOptions,
194
223
  scopes,
195
224
  validate
196
225
  });
@@ -83,6 +83,38 @@ const intersect = ( requested, allowed ) => {
83
83
 
84
84
  const isDeveloperAllowed = ( scope ) => developer.includes( scope );
85
85
 
86
+ // User-facing labels for each scope — used by UI components rendering
87
+ // scope pickers. Centralized here so the dashboard, future admin tools,
88
+ // and any other consumer surface the same strings.
89
+ const scopeLabels = {
90
+ 'profile:read' : 'Read own profile',
91
+ 'profile:write' : 'Update own profile',
92
+ 'organization:read' : 'Read organization info',
93
+ 'organization:write' : 'Manage organization',
94
+ 'campaigns:read' : 'Read campaigns',
95
+ 'campaigns:write' : 'Manage campaigns',
96
+ 'contacts:read' : 'Read contacts',
97
+ 'contacts:write' : 'Manage contacts',
98
+ 'workflows:read' : 'Read workflows',
99
+ 'workflows:write' : 'Manage workflows',
100
+ 'connections:read' : 'Read integrations',
101
+ 'connections:write' : 'Manage integrations',
102
+ 'billing:read' : 'Read billing',
103
+ 'billing:write' : 'Manage billing',
104
+ admin : 'Admin access'
105
+ };
106
+
107
+ const toOption = ( scope ) => ({
108
+ key : scopeLabels[ scope ] || scope,
109
+ value : scope
110
+ });
111
+
112
+ // `{ key, value }` option arrays ready to feed any standard checkbox-list
113
+ // component. Derived from the canonical scope arrays so vocabulary
114
+ // changes auto-propagate to every consumer on the next utils bump.
115
+ const developerOptions = developer.map( toOption );
116
+ const scopeOptions = scopes.map( toOption );
117
+
86
118
  // ─────────────────────────────────────────────────────────────────────────
87
119
  // Token primitives — convenience wrappers over drawbridge-utils/token that
88
120
  // fix the OAuth-specific conventions (32 bytes base64url, env-keyed HMAC)
@@ -228,4 +260,4 @@ const createOAuthClient = ({
228
260
 
229
261
  };
230
262
 
231
- export { createOAuthClient, developer, generateToken, hashToken, intersect, isDeveloperAllowed, parse, scopes, validate };
263
+ export { createOAuthClient, developer, developerOptions, generateToken, hashToken, intersect, isDeveloperAllowed, parse, scopeLabels, scopeOptions, scopes, validate };
@@ -83,6 +83,38 @@ const intersect = ( requested, allowed ) => {
83
83
 
84
84
  const isDeveloperAllowed = ( scope ) => developer.includes( scope );
85
85
 
86
+ // User-facing labels for each scope — used by UI components rendering
87
+ // scope pickers. Centralized here so the dashboard, future admin tools,
88
+ // and any other consumer surface the same strings.
89
+ const scopeLabels = {
90
+ 'profile:read' : 'Read own profile',
91
+ 'profile:write' : 'Update own profile',
92
+ 'organization:read' : 'Read organization info',
93
+ 'organization:write' : 'Manage organization',
94
+ 'campaigns:read' : 'Read campaigns',
95
+ 'campaigns:write' : 'Manage campaigns',
96
+ 'contacts:read' : 'Read contacts',
97
+ 'contacts:write' : 'Manage contacts',
98
+ 'workflows:read' : 'Read workflows',
99
+ 'workflows:write' : 'Manage workflows',
100
+ 'connections:read' : 'Read integrations',
101
+ 'connections:write' : 'Manage integrations',
102
+ 'billing:read' : 'Read billing',
103
+ 'billing:write' : 'Manage billing',
104
+ admin : 'Admin access'
105
+ };
106
+
107
+ const toOption = ( scope ) => ({
108
+ key : scopeLabels[ scope ] || scope,
109
+ value : scope
110
+ });
111
+
112
+ // `{ key, value }` option arrays ready to feed any standard checkbox-list
113
+ // component. Derived from the canonical scope arrays so vocabulary
114
+ // changes auto-propagate to every consumer on the next utils bump.
115
+ const developerOptions = developer.map( toOption );
116
+ const scopeOptions = scopes.map( toOption );
117
+
86
118
  // ─────────────────────────────────────────────────────────────────────────
87
119
  // Token primitives — convenience wrappers over drawbridge-utils/token that
88
120
  // fix the OAuth-specific conventions (32 bytes base64url, env-keyed HMAC)
@@ -228,4 +260,4 @@ const createOAuthClient = ({
228
260
 
229
261
  };
230
262
 
231
- export { createOAuthClient, developer, generateToken, hashToken, intersect, isDeveloperAllowed, parse, scopes, validate };
263
+ export { createOAuthClient, developer, developerOptions, generateToken, hashToken, intersect, isDeveloperAllowed, parse, scopeLabels, scopeOptions, scopes, validate };
@@ -69,6 +69,29 @@ var intersect = (requested, allowed) => {
69
69
  return r.filter((scope) => a.includes(scope));
70
70
  };
71
71
  var isDeveloperAllowed = (scope) => developer.includes(scope);
72
+ var scopeLabels = {
73
+ "profile:read": "Read own profile",
74
+ "profile:write": "Update own profile",
75
+ "organization:read": "Read organization info",
76
+ "organization:write": "Manage organization",
77
+ "campaigns:read": "Read campaigns",
78
+ "campaigns:write": "Manage campaigns",
79
+ "contacts:read": "Read contacts",
80
+ "contacts:write": "Manage contacts",
81
+ "workflows:read": "Read workflows",
82
+ "workflows:write": "Manage workflows",
83
+ "connections:read": "Read integrations",
84
+ "connections:write": "Manage integrations",
85
+ "billing:read": "Read billing",
86
+ "billing:write": "Manage billing",
87
+ admin: "Admin access"
88
+ };
89
+ var toOption = (scope) => ({
90
+ key: scopeLabels[scope] || scope,
91
+ value: scope
92
+ });
93
+ var developerOptions = developer.map(toOption);
94
+ var scopeOptions = scopes.map(toOption);
72
95
  var hashToken = (raw) => hash(raw, process.env.OAUTH_TOKEN_HMAC_KEY);
73
96
  var generateToken = () => generate(32, "base64url");
74
97
  var REFRESH_LEAD_SECONDS = 60;
@@ -141,11 +164,14 @@ export {
141
164
  compare,
142
165
  createOAuthClient,
143
166
  developer,
167
+ developerOptions,
144
168
  generateToken,
145
169
  hashToken,
146
170
  intersect,
147
171
  isDeveloperAllowed,
148
172
  parse,
173
+ scopeLabels,
174
+ scopeOptions,
149
175
  scopes,
150
176
  validate
151
177
  };
@@ -59,6 +59,23 @@ var compare = (a, b) => {
59
59
  };
60
60
 
61
61
  // lib/oauth/index.js
62
+ var scopes = [
63
+ "profile:read",
64
+ "profile:write",
65
+ "organization:read",
66
+ "organization:write",
67
+ "campaigns:read",
68
+ "campaigns:write",
69
+ "contacts:read",
70
+ "contacts:write",
71
+ "workflows:read",
72
+ "workflows:write",
73
+ "connections:read",
74
+ "connections:write",
75
+ "billing:read",
76
+ "billing:write",
77
+ "admin"
78
+ ];
62
79
  var developer = [
63
80
  "profile:read",
64
81
  "organization:read",
@@ -81,6 +98,29 @@ var intersect = (requested, allowed) => {
81
98
  const a = parse(allowed);
82
99
  return r.filter((scope) => a.includes(scope));
83
100
  };
101
+ var scopeLabels = {
102
+ "profile:read": "Read own profile",
103
+ "profile:write": "Update own profile",
104
+ "organization:read": "Read organization info",
105
+ "organization:write": "Manage organization",
106
+ "campaigns:read": "Read campaigns",
107
+ "campaigns:write": "Manage campaigns",
108
+ "contacts:read": "Read contacts",
109
+ "contacts:write": "Manage contacts",
110
+ "workflows:read": "Read workflows",
111
+ "workflows:write": "Manage workflows",
112
+ "connections:read": "Read integrations",
113
+ "connections:write": "Manage integrations",
114
+ "billing:read": "Read billing",
115
+ "billing:write": "Manage billing",
116
+ admin: "Admin access"
117
+ };
118
+ var toOption = (scope) => ({
119
+ key: scopeLabels[scope] || scope,
120
+ value: scope
121
+ });
122
+ var developerOptions = developer.map(toOption);
123
+ var scopeOptions = scopes.map(toOption);
84
124
  var hashToken = (raw) => hash(raw, process.env.OAUTH_TOKEN_HMAC_KEY);
85
125
  var generateToken = () => generate(32, "base64url");
86
126
 
@@ -24,6 +24,23 @@ var compare = (a, b) => {
24
24
  };
25
25
 
26
26
  // lib/oauth/index.js
27
+ var scopes = [
28
+ "profile:read",
29
+ "profile:write",
30
+ "organization:read",
31
+ "organization:write",
32
+ "campaigns:read",
33
+ "campaigns:write",
34
+ "contacts:read",
35
+ "contacts:write",
36
+ "workflows:read",
37
+ "workflows:write",
38
+ "connections:read",
39
+ "connections:write",
40
+ "billing:read",
41
+ "billing:write",
42
+ "admin"
43
+ ];
27
44
  var developer = [
28
45
  "profile:read",
29
46
  "organization:read",
@@ -46,6 +63,29 @@ var intersect = (requested, allowed) => {
46
63
  const a = parse(allowed);
47
64
  return r.filter((scope) => a.includes(scope));
48
65
  };
66
+ var scopeLabels = {
67
+ "profile:read": "Read own profile",
68
+ "profile:write": "Update own profile",
69
+ "organization:read": "Read organization info",
70
+ "organization:write": "Manage organization",
71
+ "campaigns:read": "Read campaigns",
72
+ "campaigns:write": "Manage campaigns",
73
+ "contacts:read": "Read contacts",
74
+ "contacts:write": "Manage contacts",
75
+ "workflows:read": "Read workflows",
76
+ "workflows:write": "Manage workflows",
77
+ "connections:read": "Read integrations",
78
+ "connections:write": "Manage integrations",
79
+ "billing:read": "Read billing",
80
+ "billing:write": "Manage billing",
81
+ admin: "Admin access"
82
+ };
83
+ var toOption = (scope) => ({
84
+ key: scopeLabels[scope] || scope,
85
+ value: scope
86
+ });
87
+ var developerOptions = developer.map(toOption);
88
+ var scopeOptions = scopes.map(toOption);
49
89
  var hashToken = (raw) => hash(raw, process.env.OAUTH_TOKEN_HMAC_KEY);
50
90
  var generateToken = () => generate(32, "base64url");
51
91
 
package/package.json CHANGED
@@ -130,5 +130,5 @@
130
130
  "build": "tsup && npm publish"
131
131
  },
132
132
  "types": "dist/index.d.ts",
133
- "version": "0.0.43"
133
+ "version": "0.0.45"
134
134
  }