@optimizely-opal/opal-tool-ocp-sdk 0.0.0-OCP-1487.7 → 0.0.0-OCP-1487.9
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/README.md +56 -11
- package/dist/auth/AuthUtils.d.ts +11 -0
- package/dist/auth/AuthUtils.d.ts.map +1 -1
- package/dist/auth/AuthUtils.js +2 -1
- package/dist/auth/AuthUtils.js.map +1 -1
- package/dist/decorator/Decorator.d.ts +6 -1
- package/dist/decorator/Decorator.d.ts.map +1 -1
- package/dist/decorator/Decorator.js +32 -2
- package/dist/decorator/Decorator.js.map +1 -1
- package/dist/decorator/Decorator.test.js +182 -10
- package/dist/decorator/Decorator.test.js.map +1 -1
- package/dist/function/GlobalToolFunction.d.ts.map +1 -1
- package/dist/function/GlobalToolFunction.js +7 -1
- package/dist/function/GlobalToolFunction.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/service/Service.d.ts +11 -1
- package/dist/service/Service.d.ts.map +1 -1
- package/dist/service/Service.js +22 -8
- package/dist/service/Service.js.map +1 -1
- package/dist/utils/ImportUtils.d.ts +15 -0
- package/dist/utils/ImportUtils.d.ts.map +1 -0
- package/dist/utils/ImportUtils.js +77 -0
- package/dist/utils/ImportUtils.js.map +1 -0
- package/package.json +1 -1
- package/src/auth/AuthUtils.ts +2 -2
- package/src/decorator/Decorator.test.ts +265 -12
- package/src/decorator/Decorator.ts +43 -3
- package/src/function/GlobalToolFunction.ts +9 -2
- package/src/index.ts +1 -0
- package/src/service/Service.ts +25 -8
- package/src/utils/ImportUtils.ts +45 -0
|
@@ -53,6 +53,15 @@ export declare class Tool<TAuthData> {
|
|
|
53
53
|
export declare class ToolsService {
|
|
54
54
|
private functions;
|
|
55
55
|
private interactions;
|
|
56
|
+
/**
|
|
57
|
+
* Set the import context for tools that will be imported
|
|
58
|
+
* @param context The context type ('global' | 'regular')
|
|
59
|
+
*/
|
|
60
|
+
setImportContext(context: 'global' | 'regular'): void;
|
|
61
|
+
/**
|
|
62
|
+
* Clear the import context after import is complete
|
|
63
|
+
*/
|
|
64
|
+
clearImportContext(): void;
|
|
56
65
|
/**
|
|
57
66
|
* Enforce OptiID authentication for tools by ensuring OptiID auth requirement is present
|
|
58
67
|
* @param authRequirements Original authentication requirements
|
|
@@ -67,6 +76,7 @@ export declare class ToolsService {
|
|
|
67
76
|
* @param parameters List of parameters for the tool
|
|
68
77
|
* @param endpoint API endpoint for the tool
|
|
69
78
|
* @param authRequirements Authentication requirements (optional)
|
|
79
|
+
* @param isGlobal Whether the tool is global
|
|
70
80
|
*/
|
|
71
81
|
registerTool<TAuthData>(name: string, description: string, handler: (functionContext: ToolFunction | GlobalToolFunction, params: unknown, authData?: TAuthData) => Promise<unknown>, parameters: Parameter[], endpoint: string, authRequirements?: AuthRequirement[], isGlobal?: boolean): void;
|
|
72
82
|
/**
|
|
@@ -80,7 +90,7 @@ export declare class ToolsService {
|
|
|
80
90
|
/**
|
|
81
91
|
* Handle discovery endpoint requests with context-aware filtering
|
|
82
92
|
* @param functionContext The function context making the request
|
|
83
|
-
* @returns Response with filtered functions based on
|
|
93
|
+
* @returns Response with filtered functions based on where tools are defined/imported
|
|
84
94
|
*/
|
|
85
95
|
private handleDiscovery;
|
|
86
96
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Service.d.ts","sourceRoot":"","sources":["../../src/service/Service.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC7D,OAAO,KAAK,GAAG,MAAM,mBAAmB,CAAC;AAEzC,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AACxD,OAAO,EAAE,kBAAkB,EAAE,MAAM,gCAAgC,CAAC;AAOpE;;GAEG;AACH,qBAAa,iBAAiB;IAEnB,OAAO,EAAE,MAAM;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AQ,GACf,IAAI;IAeP;;;;;OAKG;IACI,mBAAmB,CAAC,SAAS,EAClC,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,CACP,eAAe,EAAE,YAAY,GAAG,kBAAkB,EAClD,IAAI,EAAE,OAAO,EACb,QAAQ,CAAC,EAAE,SAAS,KACjB,OAAO,CAAC,iBAAiB,CAAC,EAC/B,QAAQ,EAAE,MAAM,GACf,IAAI;IAKM,cAAc,CACzB,GAAG,EAAE,GAAG,CAAC,OAAO,EAChB,eAAe,EAAE,YAAY,GAAG,kBAAkB,GACjD,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC;IAmDxB;;;;OAIG;IACH,OAAO,CAAC,eAAe;CAexB;AAED,eAAO,MAAM,YAAY,cAAqB,CAAC"}
|
package/dist/service/Service.js
CHANGED
|
@@ -122,6 +122,19 @@ exports.Tool = Tool;
|
|
|
122
122
|
class ToolsService {
|
|
123
123
|
functions = new Map();
|
|
124
124
|
interactions = new Map();
|
|
125
|
+
/**
|
|
126
|
+
* Set the import context for tools that will be imported
|
|
127
|
+
* @param context The context type ('global' | 'regular')
|
|
128
|
+
*/
|
|
129
|
+
setImportContext(context) {
|
|
130
|
+
globalThis.__IMPORT_CONTEXT = context;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Clear the import context after import is complete
|
|
134
|
+
*/
|
|
135
|
+
clearImportContext() {
|
|
136
|
+
globalThis.__IMPORT_CONTEXT = undefined;
|
|
137
|
+
}
|
|
125
138
|
/**
|
|
126
139
|
* Enforce OptiID authentication for tools by ensuring OptiID auth requirement is present
|
|
127
140
|
* @param authRequirements Original authentication requirements
|
|
@@ -143,6 +156,7 @@ class ToolsService {
|
|
|
143
156
|
* @param parameters List of parameters for the tool
|
|
144
157
|
* @param endpoint API endpoint for the tool
|
|
145
158
|
* @param authRequirements Authentication requirements (optional)
|
|
159
|
+
* @param isGlobal Whether the tool is global
|
|
146
160
|
*/
|
|
147
161
|
registerTool(name, description, handler, parameters, endpoint, authRequirements, isGlobal = false) {
|
|
148
162
|
// Enforce OptiID authentication for all tools
|
|
@@ -211,18 +225,18 @@ class ToolsService {
|
|
|
211
225
|
/**
|
|
212
226
|
* Handle discovery endpoint requests with context-aware filtering
|
|
213
227
|
* @param functionContext The function context making the request
|
|
214
|
-
* @returns Response with filtered functions based on
|
|
228
|
+
* @returns Response with filtered functions based on where tools are defined/imported
|
|
215
229
|
*/
|
|
216
230
|
handleDiscovery(functionContext) {
|
|
217
|
-
|
|
218
|
-
const
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
return f.isGlobal;
|
|
231
|
+
const isGlobalContext = functionContext instanceof GlobalToolFunction_1.GlobalToolFunction;
|
|
232
|
+
const availableFunctions = Array.from(this.functions.values()).filter((tool) => {
|
|
233
|
+
if (isGlobalContext) {
|
|
234
|
+
// Global context can only see global tools (defined in or imported into GlobalToolFunction)
|
|
235
|
+
return tool.isGlobal;
|
|
223
236
|
}
|
|
224
237
|
else {
|
|
225
|
-
|
|
238
|
+
// Regular context can only see regular tools (defined in or imported into ToolFunction)
|
|
239
|
+
return !tool.isGlobal;
|
|
226
240
|
}
|
|
227
241
|
});
|
|
228
242
|
return new App.Response(200, { functions: availableFunctions.map((f) => f.toJSON()) });
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Service.js","sourceRoot":"","sources":["../../src/service/Service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,yCAAyC;AACzC,4CAA6D;AAC7D,uDAAyC;AACzC,+CAA2C;AAE3C,uEAAoE;AAEpE;;GAEG;AACH,MAAM,mBAAmB,GAAG,IAAI,wBAAe,CAAC,QAAQ,EAAE,SAAS,EAAE,IAAI,CAAC,CAAC;AAE3E;;GAEG;AACH,MAAa,iBAAiB;IAEnB;IACA;IAFT,YACS,OAAe,EACf,IAAa;QADb,YAAO,GAAP,OAAO,CAAQ;QACf,SAAI,GAAJ,IAAI,CAAS;IAC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,CAAC,CAAC;QAEH,OAAO,IAAI,GAAG,CAAC,QAAQ,CAAC,GAAG,EAAE,EAAE,SAAS,EAAE,kBAAkB,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,EAAE,CAAC,CAAC;IACzF,CAAC;CACF;AArKD,oCAqKC;AAEY,QAAA,YAAY,GAAG,IAAI,YAAY,EAAE,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Import a module as global tools
|
|
3
|
+
* @param modulePath The path to the module to import
|
|
4
|
+
* @param baseUrl The base URL for resolving relative imports (optional)
|
|
5
|
+
* @returns The imported module
|
|
6
|
+
*/
|
|
7
|
+
export declare function importAsGlobal<T = any>(modulePath: string, baseUrl?: string): Promise<T>;
|
|
8
|
+
/**
|
|
9
|
+
* Import a module as regular tools
|
|
10
|
+
* @param modulePath The path to the module to import
|
|
11
|
+
* @param baseUrl The base URL for resolving relative imports (optional)
|
|
12
|
+
* @returns The imported module
|
|
13
|
+
*/
|
|
14
|
+
export declare function importAsRegular<T = any>(modulePath: string, baseUrl?: string): Promise<T>;
|
|
15
|
+
//# sourceMappingURL=ImportUtils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ImportUtils.d.ts","sourceRoot":"","sources":["../../src/utils/ImportUtils.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,wBAAsB,cAAc,CAAC,CAAC,GAAG,GAAG,EAC1C,UAAU,EAAE,MAAM,EAClB,OAAO,CAAC,EAAE,MAAM,GACf,OAAO,CAAC,CAAC,CAAC,CAWZ;AAED;;;;;GAKG;AACH,wBAAsB,eAAe,CAAC,CAAC,GAAG,GAAG,EAC3C,UAAU,EAAE,MAAM,EAClB,OAAO,CAAC,EAAE,MAAM,GACf,OAAO,CAAC,CAAC,CAAC,CAWZ"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.importAsGlobal = importAsGlobal;
|
|
37
|
+
exports.importAsRegular = importAsRegular;
|
|
38
|
+
const Service_1 = require("../service/Service");
|
|
39
|
+
/**
|
|
40
|
+
* Import a module as global tools
|
|
41
|
+
* @param modulePath The path to the module to import
|
|
42
|
+
* @param baseUrl The base URL for resolving relative imports (optional)
|
|
43
|
+
* @returns The imported module
|
|
44
|
+
*/
|
|
45
|
+
async function importAsGlobal(modulePath, baseUrl) {
|
|
46
|
+
try {
|
|
47
|
+
Service_1.toolsService.setImportContext('global');
|
|
48
|
+
// If baseUrl is provided, resolve the module path relative to it
|
|
49
|
+
// Otherwise, use the modulePath as-is
|
|
50
|
+
const resolvedPath = baseUrl ? new URL(modulePath, baseUrl).href : modulePath;
|
|
51
|
+
const module = await Promise.resolve(`${resolvedPath}`).then(s => __importStar(require(s)));
|
|
52
|
+
return module;
|
|
53
|
+
}
|
|
54
|
+
finally {
|
|
55
|
+
Service_1.toolsService.clearImportContext();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Import a module as regular tools
|
|
60
|
+
* @param modulePath The path to the module to import
|
|
61
|
+
* @param baseUrl The base URL for resolving relative imports (optional)
|
|
62
|
+
* @returns The imported module
|
|
63
|
+
*/
|
|
64
|
+
async function importAsRegular(modulePath, baseUrl) {
|
|
65
|
+
try {
|
|
66
|
+
Service_1.toolsService.setImportContext('regular');
|
|
67
|
+
// If baseUrl is provided, resolve the module path relative to it
|
|
68
|
+
// Otherwise, use the modulePath as-is
|
|
69
|
+
const resolvedPath = baseUrl ? new URL(modulePath, baseUrl).href : modulePath;
|
|
70
|
+
const module = await Promise.resolve(`${resolvedPath}`).then(s => __importStar(require(s)));
|
|
71
|
+
return module;
|
|
72
|
+
}
|
|
73
|
+
finally {
|
|
74
|
+
Service_1.toolsService.clearImportContext();
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
//# sourceMappingURL=ImportUtils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ImportUtils.js","sourceRoot":"","sources":["../../src/utils/ImportUtils.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAQA,wCAcC;AAQD,0CAcC;AA5CD,gDAAkD;AAElD;;;;;GAKG;AACI,KAAK,UAAU,cAAc,CAClC,UAAkB,EAClB,OAAgB;IAEhB,IAAI,CAAC;QACH,sBAAY,CAAC,gBAAgB,CAAC,QAAQ,CAAC,CAAC;QACxC,iEAAiE;QACjE,sCAAsC;QACtC,MAAM,YAAY,GAAG,OAAO,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,UAAU,CAAC;QAC9E,MAAM,MAAM,GAAG,yBAAa,YAAY,uCAAC,CAAC;QAC1C,OAAO,MAAM,CAAC;IAChB,CAAC;YAAS,CAAC;QACT,sBAAY,CAAC,kBAAkB,EAAE,CAAC;IACpC,CAAC;AACH,CAAC;AAED;;;;;GAKG;AACI,KAAK,UAAU,eAAe,CACnC,UAAkB,EAClB,OAAgB;IAEhB,IAAI,CAAC;QACH,sBAAY,CAAC,gBAAgB,CAAC,SAAS,CAAC,CAAC;QACzC,iEAAiE;QACjE,sCAAsC;QACtC,MAAM,YAAY,GAAG,OAAO,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,UAAU,CAAC;QAC9E,MAAM,MAAM,GAAG,yBAAa,YAAY,uCAAC,CAAC;QAC1C,OAAO,MAAM,CAAC;IAChB,CAAC;YAAS,CAAC;QACT,sBAAY,CAAC,kBAAkB,EAAE,CAAC;IACpC,CAAC;AACH,CAAC"}
|
package/package.json
CHANGED
package/src/auth/AuthUtils.ts
CHANGED
|
@@ -27,7 +27,7 @@ async function validateAccessToken(accessToken: string | undefined): Promise<boo
|
|
|
27
27
|
* @param request - The incoming request
|
|
28
28
|
* @returns object with authData and accessToken, or null if invalid
|
|
29
29
|
*/
|
|
30
|
-
function extractAuthData(request: any): { authData: OptiIdAuthData; accessToken: string } | null {
|
|
30
|
+
export function extractAuthData(request: any): { authData: OptiIdAuthData; accessToken: string } | null {
|
|
31
31
|
const authData = request?.bodyJSON?.auth as OptiIdAuthData;
|
|
32
32
|
const accessToken = authData?.credentials?.access_token;
|
|
33
33
|
if (!accessToken || authData?.provider?.toLowerCase() !== 'optiid') {
|
|
@@ -76,7 +76,7 @@ function shouldSkipAuth(request: any): boolean {
|
|
|
76
76
|
* @param validateOrg - Whether to validate organization ID
|
|
77
77
|
* @returns true if authentication succeeds
|
|
78
78
|
*/
|
|
79
|
-
async function authenticateRequest(request: any, validateOrg: boolean
|
|
79
|
+
async function authenticateRequest(request: any, validateOrg: boolean): Promise<boolean> {
|
|
80
80
|
if (shouldSkipAuth(request)) {
|
|
81
81
|
return true;
|
|
82
82
|
}
|
|
@@ -9,6 +9,9 @@ jest.mock('../service/Service', () => ({
|
|
|
9
9
|
registerInteraction: jest.fn()
|
|
10
10
|
}
|
|
11
11
|
}));
|
|
12
|
+
|
|
13
|
+
// Mock base class for testing auto-detection
|
|
14
|
+
class GlobalToolFunction {}
|
|
12
15
|
describe('Decorators', () => {
|
|
13
16
|
beforeEach(() => {
|
|
14
17
|
jest.clearAllMocks();
|
|
@@ -657,17 +660,15 @@ describe('Decorators', () => {
|
|
|
657
660
|
});
|
|
658
661
|
|
|
659
662
|
describe('global tool registration', () => {
|
|
660
|
-
it('should
|
|
661
|
-
const config = {
|
|
662
|
-
name: '
|
|
663
|
-
description: '
|
|
663
|
+
it('should auto-detect global tools when class extends GlobalToolFunction', () => {
|
|
664
|
+
const config: ToolConfig = {
|
|
665
|
+
name: 'autoGlobalTool',
|
|
666
|
+
description: 'An auto-detected global tool',
|
|
664
667
|
parameters: [],
|
|
665
|
-
endpoint: '/global-tool'
|
|
666
|
-
authRequirements: [],
|
|
667
|
-
isGlobal: true
|
|
668
|
+
endpoint: '/auto-global-tool'
|
|
668
669
|
};
|
|
669
670
|
|
|
670
|
-
class
|
|
671
|
+
class TestGlobalClass extends GlobalToolFunction {
|
|
671
672
|
@tool(config)
|
|
672
673
|
public async testTool(): Promise<{ result: string }> {
|
|
673
674
|
return { result: 'global-tool-result' };
|
|
@@ -675,16 +676,268 @@ describe('Decorators', () => {
|
|
|
675
676
|
}
|
|
676
677
|
|
|
677
678
|
expect(toolsService.registerTool).toHaveBeenCalledWith(
|
|
678
|
-
'
|
|
679
|
-
'
|
|
679
|
+
'autoGlobalTool',
|
|
680
|
+
'An auto-detected global tool',
|
|
681
|
+
expect.any(Function),
|
|
682
|
+
[],
|
|
683
|
+
'/auto-global-tool',
|
|
684
|
+
[],
|
|
685
|
+
true
|
|
686
|
+
);
|
|
687
|
+
|
|
688
|
+
expect(TestGlobalClass).toBeDefined();
|
|
689
|
+
});
|
|
690
|
+
|
|
691
|
+
it('should register regular tools when class does not extend GlobalToolFunction', () => {
|
|
692
|
+
const config: ToolConfig = {
|
|
693
|
+
name: 'regularTool',
|
|
694
|
+
description: 'A regular tool',
|
|
695
|
+
parameters: [],
|
|
696
|
+
endpoint: '/regular-tool'
|
|
697
|
+
};
|
|
698
|
+
|
|
699
|
+
class TestRegularClass {
|
|
700
|
+
@tool(config)
|
|
701
|
+
public async testTool(): Promise<{ result: string }> {
|
|
702
|
+
return { result: 'regular-tool-result' };
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
expect(toolsService.registerTool).toHaveBeenCalledWith(
|
|
707
|
+
'regularTool',
|
|
708
|
+
'A regular tool',
|
|
709
|
+
expect.any(Function),
|
|
710
|
+
[],
|
|
711
|
+
'/regular-tool',
|
|
712
|
+
[],
|
|
713
|
+
false
|
|
714
|
+
);
|
|
715
|
+
|
|
716
|
+
expect(TestRegularClass).toBeDefined();
|
|
717
|
+
});
|
|
718
|
+
|
|
719
|
+
it('should register tools as global when import context is set', () => {
|
|
720
|
+
// Set global import context
|
|
721
|
+
globalThis.__IMPORT_CONTEXT = 'global';
|
|
722
|
+
|
|
723
|
+
const config: ToolConfig = {
|
|
724
|
+
name: 'importedGlobalTool',
|
|
725
|
+
description: 'A tool imported as global',
|
|
726
|
+
parameters: [],
|
|
727
|
+
endpoint: '/imported-global-tool'
|
|
728
|
+
};
|
|
729
|
+
|
|
730
|
+
class TestImportedClass {
|
|
731
|
+
@tool(config)
|
|
732
|
+
public async testTool(): Promise<{ result: string }> {
|
|
733
|
+
return { result: 'imported-global-tool-result' };
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
expect(toolsService.registerTool).toHaveBeenCalledWith(
|
|
738
|
+
'importedGlobalTool',
|
|
739
|
+
'A tool imported as global',
|
|
680
740
|
expect.any(Function),
|
|
681
741
|
[],
|
|
682
|
-
'/global-tool',
|
|
742
|
+
'/imported-global-tool',
|
|
683
743
|
[],
|
|
684
744
|
true
|
|
685
745
|
);
|
|
686
746
|
|
|
687
|
-
|
|
747
|
+
// Clean up
|
|
748
|
+
globalThis.__IMPORT_CONTEXT = undefined;
|
|
749
|
+
expect(TestImportedClass).toBeDefined();
|
|
750
|
+
});
|
|
751
|
+
|
|
752
|
+
it('should register tools as regular when import context is set to regular', () => {
|
|
753
|
+
// Set regular import context
|
|
754
|
+
globalThis.__IMPORT_CONTEXT = 'regular';
|
|
755
|
+
|
|
756
|
+
const config: ToolConfig = {
|
|
757
|
+
name: 'importedRegularTool',
|
|
758
|
+
description: 'A tool imported as regular',
|
|
759
|
+
parameters: [],
|
|
760
|
+
endpoint: '/imported-regular-tool'
|
|
761
|
+
};
|
|
762
|
+
|
|
763
|
+
class TestImportedClass {
|
|
764
|
+
@tool(config)
|
|
765
|
+
public async testTool(): Promise<{ result: string }> {
|
|
766
|
+
return { result: 'imported-regular-tool-result' };
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
expect(toolsService.registerTool).toHaveBeenCalledWith(
|
|
771
|
+
'importedRegularTool',
|
|
772
|
+
'A tool imported as regular',
|
|
773
|
+
expect.any(Function),
|
|
774
|
+
[],
|
|
775
|
+
'/imported-regular-tool',
|
|
776
|
+
[],
|
|
777
|
+
false
|
|
778
|
+
);
|
|
779
|
+
|
|
780
|
+
// Clean up
|
|
781
|
+
globalThis.__IMPORT_CONTEXT = undefined;
|
|
782
|
+
expect(TestImportedClass).toBeDefined();
|
|
783
|
+
});
|
|
784
|
+
|
|
785
|
+
it('should handle mixed imports - both regular and global tools together', () => {
|
|
786
|
+
// Test importing regular tools first
|
|
787
|
+
globalThis.__IMPORT_CONTEXT = 'regular';
|
|
788
|
+
|
|
789
|
+
const regularConfig: ToolConfig = {
|
|
790
|
+
name: 'mixedRegularTool',
|
|
791
|
+
description: 'A regular tool in mixed scenario',
|
|
792
|
+
parameters: [],
|
|
793
|
+
endpoint: '/mixed-regular-tool'
|
|
794
|
+
};
|
|
795
|
+
|
|
796
|
+
class RegularToolClass {
|
|
797
|
+
@tool(regularConfig)
|
|
798
|
+
public async regularTool(): Promise<{ result: string }> {
|
|
799
|
+
return { result: 'mixed-regular-result' };
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
// Switch to global context
|
|
804
|
+
globalThis.__IMPORT_CONTEXT = 'global';
|
|
805
|
+
|
|
806
|
+
const globalConfig: ToolConfig = {
|
|
807
|
+
name: 'mixedGlobalTool',
|
|
808
|
+
description: 'A global tool in mixed scenario',
|
|
809
|
+
parameters: [],
|
|
810
|
+
endpoint: '/mixed-global-tool'
|
|
811
|
+
};
|
|
812
|
+
|
|
813
|
+
class GlobalToolClass {
|
|
814
|
+
@tool(globalConfig)
|
|
815
|
+
public async globalTool(): Promise<{ result: string }> {
|
|
816
|
+
return { result: 'mixed-global-result' };
|
|
817
|
+
}
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
// Clear context
|
|
821
|
+
globalThis.__IMPORT_CONTEXT = undefined;
|
|
822
|
+
|
|
823
|
+
// Verify regular tool was registered as regular
|
|
824
|
+
expect(toolsService.registerTool).toHaveBeenCalledWith(
|
|
825
|
+
'mixedRegularTool',
|
|
826
|
+
'A regular tool in mixed scenario',
|
|
827
|
+
expect.any(Function),
|
|
828
|
+
[],
|
|
829
|
+
'/mixed-regular-tool',
|
|
830
|
+
[],
|
|
831
|
+
false
|
|
832
|
+
);
|
|
833
|
+
|
|
834
|
+
// Verify global tool was registered as global
|
|
835
|
+
expect(toolsService.registerTool).toHaveBeenCalledWith(
|
|
836
|
+
'mixedGlobalTool',
|
|
837
|
+
'A global tool in mixed scenario',
|
|
838
|
+
expect.any(Function),
|
|
839
|
+
[],
|
|
840
|
+
'/mixed-global-tool',
|
|
841
|
+
[],
|
|
842
|
+
true
|
|
843
|
+
);
|
|
844
|
+
|
|
845
|
+
expect(RegularToolClass).toBeDefined();
|
|
846
|
+
expect(GlobalToolClass).toBeDefined();
|
|
847
|
+
});
|
|
848
|
+
|
|
849
|
+
it('should handle context switching correctly within same test', () => {
|
|
850
|
+
let callCount = 0;
|
|
851
|
+
const originalRegisterTool = jest.mocked(toolsService.registerTool);
|
|
852
|
+
|
|
853
|
+
// Track call order and contexts
|
|
854
|
+
originalRegisterTool.mockImplementation(() => {
|
|
855
|
+
callCount++;
|
|
856
|
+
// Store the isGlobal flag from each call for verification
|
|
857
|
+
});
|
|
858
|
+
|
|
859
|
+
// First: Import as regular
|
|
860
|
+
globalThis.__IMPORT_CONTEXT = 'regular';
|
|
861
|
+
|
|
862
|
+
class FirstClass {
|
|
863
|
+
@tool({
|
|
864
|
+
name: 'contextTest1',
|
|
865
|
+
description: 'First context test',
|
|
866
|
+
parameters: [],
|
|
867
|
+
endpoint: '/context-test-1'
|
|
868
|
+
})
|
|
869
|
+
public async tool1() {
|
|
870
|
+
return { result: 'test1' };
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
// Second: Switch to global
|
|
875
|
+
globalThis.__IMPORT_CONTEXT = 'global';
|
|
876
|
+
|
|
877
|
+
class SecondClass {
|
|
878
|
+
@tool({
|
|
879
|
+
name: 'contextTest2',
|
|
880
|
+
description: 'Second context test',
|
|
881
|
+
parameters: [],
|
|
882
|
+
endpoint: '/context-test-2'
|
|
883
|
+
})
|
|
884
|
+
public async tool2() {
|
|
885
|
+
return { result: 'test2' };
|
|
886
|
+
}
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
// Third: Clear context (should fallback to class inheritance)
|
|
890
|
+
globalThis.__IMPORT_CONTEXT = undefined;
|
|
891
|
+
|
|
892
|
+
class ThirdClass {
|
|
893
|
+
@tool({
|
|
894
|
+
name: 'contextTest3',
|
|
895
|
+
description: 'Third context test',
|
|
896
|
+
parameters: [],
|
|
897
|
+
endpoint: '/context-test-3'
|
|
898
|
+
})
|
|
899
|
+
public async tool3() {
|
|
900
|
+
return { result: 'test3' };
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
// Verify the sequence of calls
|
|
905
|
+
expect(toolsService.registerTool).toHaveBeenNthCalledWith(
|
|
906
|
+
callCount - 2, // First call
|
|
907
|
+
'contextTest1',
|
|
908
|
+
'First context test',
|
|
909
|
+
expect.any(Function),
|
|
910
|
+
[],
|
|
911
|
+
'/context-test-1',
|
|
912
|
+
[],
|
|
913
|
+
false // Should be regular
|
|
914
|
+
);
|
|
915
|
+
|
|
916
|
+
expect(toolsService.registerTool).toHaveBeenNthCalledWith(
|
|
917
|
+
callCount - 1, // Second call
|
|
918
|
+
'contextTest2',
|
|
919
|
+
'Second context test',
|
|
920
|
+
expect.any(Function),
|
|
921
|
+
[],
|
|
922
|
+
'/context-test-2',
|
|
923
|
+
[],
|
|
924
|
+
true // Should be global
|
|
925
|
+
);
|
|
926
|
+
|
|
927
|
+
expect(toolsService.registerTool).toHaveBeenNthCalledWith(
|
|
928
|
+
callCount, // Third call
|
|
929
|
+
'contextTest3',
|
|
930
|
+
'Third context test',
|
|
931
|
+
expect.any(Function),
|
|
932
|
+
[],
|
|
933
|
+
'/context-test-3',
|
|
934
|
+
[],
|
|
935
|
+
false // Should be regular (fallback to class check, ThirdClass doesn't extend GlobalToolFunction)
|
|
936
|
+
);
|
|
937
|
+
|
|
938
|
+
expect(FirstClass).toBeDefined();
|
|
939
|
+
expect(SecondClass).toBeDefined();
|
|
940
|
+
expect(ThirdClass).toBeDefined();
|
|
688
941
|
});
|
|
689
942
|
});
|
|
690
943
|
});
|
|
@@ -1,6 +1,44 @@
|
|
|
1
1
|
import { Parameter, AuthRequirement, ParameterType } from '../types/Models';
|
|
2
2
|
import { toolsService } from '../service/Service';
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Global context flag to track import context
|
|
6
|
+
*/
|
|
7
|
+
declare global {
|
|
8
|
+
var __IMPORT_CONTEXT: 'global' | 'regular' | undefined;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Helper function to check if a class extends GlobalToolFunction
|
|
13
|
+
*/
|
|
14
|
+
function isGlobalToolFunction(constructor: any): boolean {
|
|
15
|
+
// Walk up the prototype chain to check for GlobalToolFunction
|
|
16
|
+
let currentConstructor = constructor;
|
|
17
|
+
while (currentConstructor) {
|
|
18
|
+
if (currentConstructor.name === 'GlobalToolFunction') {
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
currentConstructor = Object.getPrototypeOf(currentConstructor);
|
|
22
|
+
}
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Determine if tool should be registered as global based on definition context or import context
|
|
28
|
+
*/
|
|
29
|
+
function shouldRegisterAsGlobal(targetConstructor: any): boolean {
|
|
30
|
+
// First check if there's an active import context
|
|
31
|
+
if (globalThis.__IMPORT_CONTEXT === 'global') {
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
if (globalThis.__IMPORT_CONTEXT === 'regular') {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Fallback to checking where the tool is defined
|
|
39
|
+
return isGlobalToolFunction(targetConstructor);
|
|
40
|
+
}
|
|
41
|
+
|
|
4
42
|
/**
|
|
5
43
|
* Configuration for @tool decorator
|
|
6
44
|
*/
|
|
@@ -10,7 +48,6 @@ export interface ToolConfig {
|
|
|
10
48
|
parameters: ParameterConfig[];
|
|
11
49
|
authRequirements?: AuthRequirementConfig[];
|
|
12
50
|
endpoint: string;
|
|
13
|
-
isGlobal?: boolean;
|
|
14
51
|
}
|
|
15
52
|
|
|
16
53
|
/**
|
|
@@ -70,7 +107,10 @@ export function tool(config: ToolConfig) {
|
|
|
70
107
|
return originalMethod.call(instance, params, authData);
|
|
71
108
|
};
|
|
72
109
|
|
|
73
|
-
//
|
|
110
|
+
// Determine if this tool should be registered as global based on definition or import context
|
|
111
|
+
const isGlobal = shouldRegisterAsGlobal(target.constructor);
|
|
112
|
+
|
|
113
|
+
// Register tool with global flag based on where it's defined or imported
|
|
74
114
|
toolsService.registerTool(
|
|
75
115
|
config.name,
|
|
76
116
|
config.description,
|
|
@@ -78,7 +118,7 @@ export function tool(config: ToolConfig) {
|
|
|
78
118
|
parameters,
|
|
79
119
|
config.endpoint,
|
|
80
120
|
authRequirements,
|
|
81
|
-
|
|
121
|
+
isGlobal
|
|
82
122
|
);
|
|
83
123
|
};
|
|
84
124
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { GlobalFunction, Response, amendLogContext } from '@zaiusinc/app-sdk';
|
|
2
|
-
import { authenticateGlobalRequest } from '../auth/AuthUtils';
|
|
2
|
+
import { authenticateGlobalRequest, extractAuthData } from '../auth/AuthUtils';
|
|
3
3
|
import { toolsService } from '../service/Service';
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -24,7 +24,14 @@ export abstract class GlobalToolFunction extends GlobalFunction {
|
|
|
24
24
|
* @returns Response as the HTTP response
|
|
25
25
|
*/
|
|
26
26
|
public async perform(): Promise<Response> {
|
|
27
|
-
|
|
27
|
+
// Extract customer_id from auth data for global context attribution
|
|
28
|
+
const authInfo = extractAuthData(this.request);
|
|
29
|
+
const customerId = authInfo?.authData?.credentials?.customer_id;
|
|
30
|
+
|
|
31
|
+
amendLogContext({
|
|
32
|
+
opalThreadId: this.request.headers.get('x-opal-thread-id') || '',
|
|
33
|
+
customerId: customerId || ''
|
|
34
|
+
});
|
|
28
35
|
|
|
29
36
|
if (!(await this.authorizeRequest())) {
|
|
30
37
|
return new Response(403, { error: 'Forbidden' });
|
package/src/index.ts
CHANGED
|
@@ -3,4 +3,5 @@ export * from './function/GlobalToolFunction';
|
|
|
3
3
|
export * from './types/Models';
|
|
4
4
|
export * from './decorator/Decorator';
|
|
5
5
|
export * from './auth/TokenVerifier';
|
|
6
|
+
export * from './utils/ImportUtils';
|
|
6
7
|
export { Tool, Interaction, InteractionResult } from './service/Service';
|