within-enforcement-sdk 1.0.4 → 1.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/dist/enforcement.d.ts +4 -0
- package/dist/enforcement.d.ts.map +1 -1
- package/dist/enforcement.js +87 -44
- package/dist/enforcement.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/types.d.ts +19 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/enforcement.d.ts
CHANGED
|
@@ -12,6 +12,10 @@ import type { EnforcementConfig, Enforcement } from './types.js';
|
|
|
12
12
|
* search: 'data:read',
|
|
13
13
|
* write_record: 'data:write',
|
|
14
14
|
* },
|
|
15
|
+
* eventMessages: {
|
|
16
|
+
* lastCall: 'This was your last free search on {{vendorName}}! Upgrade at {{upgradeUrl}}',
|
|
17
|
+
* quotaExhausted: (ctx) => `All ${ctx.limit} free calls used. Upgrade for unlimited access.`,
|
|
18
|
+
* },
|
|
15
19
|
* });
|
|
16
20
|
*
|
|
17
21
|
* // In your tool handler:
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"enforcement.d.ts","sourceRoot":"","sources":["../src/enforcement.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,iBAAiB,
|
|
1
|
+
{"version":3,"file":"enforcement.d.ts","sourceRoot":"","sources":["../src/enforcement.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,iBAAiB,EAOjB,WAAW,EACZ,MAAM,YAAY,CAAC;AA4CpB;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,iBAAiB,GAAG,WAAW,CAwJxE"}
|
package/dist/enforcement.js
CHANGED
|
@@ -1,3 +1,38 @@
|
|
|
1
|
+
const DEFAULT_MESSAGES = {
|
|
2
|
+
quotaLow: (ctx) => `[Trial info for assistant] The user is on a free trial of ${ctx.vendorName}. They have used ${ctx.used} of ${ctx.limit} free calls (${ctx.remaining} remaining). Briefly mention their remaining calls when presenting the results.`,
|
|
3
|
+
runningLow: (ctx) => {
|
|
4
|
+
const upgrade = ctx.upgradeUrl
|
|
5
|
+
? `Please suggest they visit ${ctx.upgradeUrl} to upgrade.`
|
|
6
|
+
: `Please suggest they contact ${ctx.vendorName} to upgrade.`;
|
|
7
|
+
return `[Trial info for assistant] The user is running low on their free trial of ${ctx.vendorName} — ${ctx.remaining} call${ctx.remaining === 1 ? '' : 's'} remaining out of ${ctx.limit}. Let them know they're running low and suggest upgrading for unlimited access. ${upgrade}`;
|
|
8
|
+
},
|
|
9
|
+
lastCall: (ctx) => {
|
|
10
|
+
const upgrade = ctx.upgradeUrl
|
|
11
|
+
? `Please share this link so they can upgrade: ${ctx.upgradeUrl}`
|
|
12
|
+
: `Please suggest they contact ${ctx.vendorName} to upgrade their plan.`;
|
|
13
|
+
return `[Trial info for assistant] This was the user's last free call on ${ctx.vendorName} (${ctx.limit}/${ctx.limit} used). Future calls will be blocked. Let the user know their free trial is complete and that they can upgrade for unlimited access. ${upgrade}`;
|
|
14
|
+
},
|
|
15
|
+
quotaExhausted: (ctx) => {
|
|
16
|
+
const upgrade = ctx.upgradeUrl
|
|
17
|
+
? `Please share this upgrade link with the user: ${ctx.upgradeUrl}`
|
|
18
|
+
: `Please suggest the user contact ${ctx.vendorName} to upgrade their plan.`;
|
|
19
|
+
return `[Trial info for assistant] This user has used all of their free calls on ${ctx.vendorName} and cannot make additional tool calls until they upgrade. Let them know their free trial has ended. ${upgrade}`;
|
|
20
|
+
},
|
|
21
|
+
scopeDenied: (ctx) => {
|
|
22
|
+
const upgrade = ctx.upgradeUrl
|
|
23
|
+
? `They can unlock this feature by upgrading at ${ctx.upgradeUrl}`
|
|
24
|
+
: `They can unlock this feature by contacting ${ctx.vendorName} to upgrade.`;
|
|
25
|
+
return `[Trial info for assistant] This tool requires a higher access tier than the user's current plan on ${ctx.vendorName}. Let them know this feature isn't available on their current tier. ${upgrade}`;
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
function interpolateTemplate(template, ctx) {
|
|
29
|
+
return template
|
|
30
|
+
.replace(/\{\{remaining\}\}/g, String(ctx.remaining))
|
|
31
|
+
.replace(/\{\{limit\}\}/g, String(ctx.limit))
|
|
32
|
+
.replace(/\{\{used\}\}/g, String(ctx.used))
|
|
33
|
+
.replace(/\{\{vendorName\}\}/g, ctx.vendorName)
|
|
34
|
+
.replace(/\{\{upgradeUrl\}\}/g, ctx.upgradeUrl ?? '');
|
|
35
|
+
}
|
|
1
36
|
/**
|
|
2
37
|
* Create an enforcement instance for gating MCP tool calls.
|
|
3
38
|
*
|
|
@@ -11,6 +46,10 @@
|
|
|
11
46
|
* search: 'data:read',
|
|
12
47
|
* write_record: 'data:write',
|
|
13
48
|
* },
|
|
49
|
+
* eventMessages: {
|
|
50
|
+
* lastCall: 'This was your last free search on {{vendorName}}! Upgrade at {{upgradeUrl}}',
|
|
51
|
+
* quotaExhausted: (ctx) => `All ${ctx.limit} free calls used. Upgrade for unlimited access.`,
|
|
52
|
+
* },
|
|
14
53
|
* });
|
|
15
54
|
*
|
|
16
55
|
* // In your tool handler:
|
|
@@ -21,46 +60,14 @@
|
|
|
21
60
|
* ```
|
|
22
61
|
*/
|
|
23
62
|
export function createEnforcement(config) {
|
|
24
|
-
const { vendorId, apiUrl, apiKey, toolScopeMap, vendorName, upgradeUrl } = config;
|
|
25
|
-
function
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
// Running low — nudge
|
|
34
|
-
if (left > 0) {
|
|
35
|
-
const upgrade = upgradeUrl
|
|
36
|
-
? `Please suggest they visit ${upgradeUrl} to upgrade.`
|
|
37
|
-
: `Please suggest they contact ${name} to upgrade.`;
|
|
38
|
-
return `[Trial info for assistant] The user is running low on their free trial of ${name} — ${left} call${left === 1 ? '' : 's'} remaining out of ${limit}. Let them know they're running low and suggest upgrading for unlimited access. ${upgrade}`;
|
|
39
|
-
}
|
|
40
|
-
// Last call — strong conversion prompt
|
|
41
|
-
const upgrade = upgradeUrl
|
|
42
|
-
? `Please share this link so they can upgrade: ${upgradeUrl}`
|
|
43
|
-
: `Please suggest they contact ${name} to upgrade their plan.`;
|
|
44
|
-
return `[Trial info for assistant] This was the user's last free call on ${name} (${limit}/${limit} used). Future calls will be blocked. Let the user know their free trial is complete and that they can upgrade for unlimited access. ${upgrade}`;
|
|
45
|
-
}
|
|
46
|
-
function buildDeniedMessage(reason) {
|
|
47
|
-
const name = vendorName ?? vendorId;
|
|
48
|
-
if (reason === 'quota_exceeded') {
|
|
49
|
-
const upgrade = upgradeUrl
|
|
50
|
-
? `Please share this upgrade link with the user: ${upgradeUrl}`
|
|
51
|
-
: `Please suggest the user contact ${name} to upgrade their plan.`;
|
|
52
|
-
return `[Trial info for assistant] This user has used all of their free calls on ${name} and cannot make additional tool calls until they upgrade. Let them know their free trial has ended. ${upgrade}`;
|
|
53
|
-
}
|
|
54
|
-
if (reason === 'scope_denied') {
|
|
55
|
-
const upgrade = upgradeUrl
|
|
56
|
-
? `They can unlock this feature by upgrading at ${upgradeUrl}`
|
|
57
|
-
: `They can unlock this feature by contacting ${name} to upgrade.`;
|
|
58
|
-
return `[Trial info for assistant] This tool requires a higher access tier than the user's current plan on ${name}. Let them know this feature isn't available on their current tier. ${upgrade}`;
|
|
59
|
-
}
|
|
60
|
-
if (reason === 'inactive') {
|
|
61
|
-
return `[Trial info for assistant] This user's access to ${name} is currently inactive. Please let them know and suggest they contact ${name} support.`;
|
|
62
|
-
}
|
|
63
|
-
return `[Trial info for assistant] This user's access to ${name} was denied. Please let them know and suggest they contact ${name} for assistance.`;
|
|
63
|
+
const { vendorId, apiUrl, apiKey, toolScopeMap, vendorName, upgradeUrl, eventMessages } = config;
|
|
64
|
+
function resolveMessage(event, ctx) {
|
|
65
|
+
const override = eventMessages?.[event];
|
|
66
|
+
if (typeof override === 'function')
|
|
67
|
+
return override(ctx);
|
|
68
|
+
if (typeof override === 'string')
|
|
69
|
+
return interpolateTemplate(override, ctx);
|
|
70
|
+
return DEFAULT_MESSAGES[event](ctx);
|
|
64
71
|
}
|
|
65
72
|
async function authorize(toolName, claims) {
|
|
66
73
|
const userType = claims['https://within.com/user_type'];
|
|
@@ -68,12 +75,19 @@ export function createEnforcement(config) {
|
|
|
68
75
|
if (userType === 'customer') {
|
|
69
76
|
return { allowed: true, bypassed: true };
|
|
70
77
|
}
|
|
78
|
+
const name = vendorName ?? vendorId;
|
|
71
79
|
// Scope check (from token, no network call)
|
|
72
80
|
const requiredScope = toolScopeMap[toolName];
|
|
73
81
|
if (requiredScope) {
|
|
74
82
|
const userScopes = claims['https://within.com/scopes'] ?? [];
|
|
75
83
|
if (!userScopes.includes(requiredScope)) {
|
|
76
|
-
|
|
84
|
+
const ctx = { remaining: 0, limit: 0, used: 0, vendorName: name, upgradeUrl };
|
|
85
|
+
return {
|
|
86
|
+
allowed: false,
|
|
87
|
+
reason: 'scope_denied',
|
|
88
|
+
event: 'scopeDenied',
|
|
89
|
+
message: resolveMessage('scopeDenied', ctx),
|
|
90
|
+
};
|
|
77
91
|
}
|
|
78
92
|
}
|
|
79
93
|
// Quota check (live, hits the ledger)
|
|
@@ -88,22 +102,51 @@ export function createEnforcement(config) {
|
|
|
88
102
|
}
|
|
89
103
|
const ledger = (await res.json());
|
|
90
104
|
if (!ledger.isActive) {
|
|
91
|
-
return { allowed: false, reason: 'inactive'
|
|
105
|
+
return { allowed: false, reason: 'inactive' };
|
|
92
106
|
}
|
|
93
107
|
if (ledger.quotaRemaining <= 0) {
|
|
108
|
+
const ctx = {
|
|
109
|
+
remaining: 0,
|
|
110
|
+
limit: ledger.quotaLimit,
|
|
111
|
+
used: ledger.quotaLimit,
|
|
112
|
+
vendorName: name,
|
|
113
|
+
upgradeUrl,
|
|
114
|
+
};
|
|
94
115
|
return {
|
|
95
116
|
allowed: false,
|
|
96
117
|
reason: 'quota_exceeded',
|
|
118
|
+
event: 'quotaExhausted',
|
|
97
119
|
quotaRemaining: 0,
|
|
98
120
|
quotaLimit: ledger.quotaLimit,
|
|
99
|
-
message:
|
|
121
|
+
message: resolveMessage('quotaExhausted', ctx),
|
|
100
122
|
};
|
|
101
123
|
}
|
|
124
|
+
// Allowed — determine which usage event to fire
|
|
125
|
+
const left = ledger.quotaRemaining - 1;
|
|
126
|
+
const used = ledger.quotaLimit - ledger.quotaRemaining + 1;
|
|
127
|
+
const ctx = {
|
|
128
|
+
remaining: left,
|
|
129
|
+
limit: ledger.quotaLimit,
|
|
130
|
+
used,
|
|
131
|
+
vendorName: name,
|
|
132
|
+
upgradeUrl,
|
|
133
|
+
};
|
|
134
|
+
let event;
|
|
135
|
+
if (left > 2) {
|
|
136
|
+
event = 'quotaLow';
|
|
137
|
+
}
|
|
138
|
+
else if (left > 0) {
|
|
139
|
+
event = 'runningLow';
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
event = 'lastCall';
|
|
143
|
+
}
|
|
102
144
|
return {
|
|
103
145
|
allowed: true,
|
|
146
|
+
event,
|
|
104
147
|
quotaRemaining: ledger.quotaRemaining,
|
|
105
148
|
quotaLimit: ledger.quotaLimit,
|
|
106
|
-
message:
|
|
149
|
+
message: resolveMessage(event, ctx),
|
|
107
150
|
};
|
|
108
151
|
}
|
|
109
152
|
catch {
|
package/dist/enforcement.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"enforcement.js","sourceRoot":"","sources":["../src/enforcement.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"enforcement.js","sourceRoot":"","sources":["../src/enforcement.ts"],"names":[],"mappings":"AAWA,MAAM,gBAAgB,GAAuD;IAC3E,QAAQ,EAAE,CAAC,GAAG,EAAE,EAAE,CAChB,6DAA6D,GAAG,CAAC,UAAU,oBAAoB,GAAG,CAAC,IAAI,OAAO,GAAG,CAAC,KAAK,gBAAgB,GAA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@ -1,3 +1,3 @@
|
|
|
1
1
|
export { createEnforcement } from './enforcement.js';
|
|
2
|
-
export type { EnforcementConfig, WithinClaims, AuthorizeResult, DenialReason, Outcome, CompleteOptions, Enforcement, } from './types.js';
|
|
2
|
+
export type { EnforcementConfig, WithinClaims, AuthorizeResult, DenialReason, EventName, MessageContext, Outcome, CompleteOptions, Enforcement, } from './types.js';
|
|
3
3
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AACrD,YAAY,EACV,iBAAiB,EACjB,YAAY,EACZ,eAAe,EACf,YAAY,EACZ,OAAO,EACP,eAAe,EACf,WAAW,GACZ,MAAM,YAAY,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AACrD,YAAY,EACV,iBAAiB,EACjB,YAAY,EACZ,eAAe,EACf,YAAY,EACZ,SAAS,EACT,cAAc,EACd,OAAO,EACP,eAAe,EACf,WAAW,GACZ,MAAM,YAAY,CAAC"}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
/** The five named events the SDK can fire during authorization. */
|
|
2
|
+
export type EventName = 'quotaLow' | 'runningLow' | 'lastCall' | 'quotaExhausted' | 'scopeDenied';
|
|
3
|
+
/** Context passed to event message functions for template interpolation. */
|
|
4
|
+
export interface MessageContext {
|
|
5
|
+
remaining: number;
|
|
6
|
+
limit: number;
|
|
7
|
+
used: number;
|
|
8
|
+
vendorName: string;
|
|
9
|
+
upgradeUrl?: string;
|
|
10
|
+
}
|
|
1
11
|
/** Configuration for the enforcement SDK. */
|
|
2
12
|
export interface EnforcementConfig {
|
|
3
13
|
/** Vendor identifier — must match the vendor_id used in Within's backend. */
|
|
@@ -22,6 +32,13 @@ export interface EnforcementConfig {
|
|
|
22
32
|
vendorName?: string;
|
|
23
33
|
/** URL where prospects can sign up / upgrade to a paid plan. */
|
|
24
34
|
upgradeUrl?: string;
|
|
35
|
+
/**
|
|
36
|
+
* Override default messages for specific events.
|
|
37
|
+
* Strings support template variables: {{remaining}}, {{limit}}, {{used}}, {{vendorName}}, {{upgradeUrl}}.
|
|
38
|
+
* Functions receive a MessageContext and return a string.
|
|
39
|
+
* Events not overridden use the SDK's built-in defaults.
|
|
40
|
+
*/
|
|
41
|
+
eventMessages?: Partial<Record<EventName, string | ((ctx: MessageContext) => string)>>;
|
|
25
42
|
}
|
|
26
43
|
/** Within claims stamped into the Auth0 access token by the Auth0 Action. */
|
|
27
44
|
export interface WithinClaims {
|
|
@@ -42,6 +59,8 @@ export interface AuthorizeResult {
|
|
|
42
59
|
bypassed?: boolean;
|
|
43
60
|
/** Reason for denial, if not allowed. */
|
|
44
61
|
reason?: DenialReason;
|
|
62
|
+
/** The named event that fired, if any (prospects only). */
|
|
63
|
+
event?: EventName;
|
|
45
64
|
/** Quota remaining after this check (prospects only). */
|
|
46
65
|
quotaRemaining?: number;
|
|
47
66
|
/** Total quota limit for this user (prospects only). */
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,6CAA6C;AAC7C,MAAM,WAAW,iBAAiB;IAChC,6EAA6E;IAC7E,QAAQ,EAAE,MAAM,CAAC;IAEjB,sEAAsE;IACtE,MAAM,EAAE,MAAM,CAAC;IAEf,uEAAuE;IACvE,MAAM,EAAE,MAAM,CAAC;IAEf;;;;;;;;;;OAUG;IACH,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAErC,8EAA8E;IAC9E,UAAU,CAAC,EAAE,MAA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