@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
package/src/service/Service.ts
CHANGED
|
@@ -89,6 +89,21 @@ export class ToolsService {
|
|
|
89
89
|
private functions: Map<string, Tool<any>> = new Map();
|
|
90
90
|
private interactions: Map<string, Interaction<any>> = new Map();
|
|
91
91
|
|
|
92
|
+
/**
|
|
93
|
+
* Set the import context for tools that will be imported
|
|
94
|
+
* @param context The context type ('global' | 'regular')
|
|
95
|
+
*/
|
|
96
|
+
public setImportContext(context: 'global' | 'regular'): void {
|
|
97
|
+
globalThis.__IMPORT_CONTEXT = context;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Clear the import context after import is complete
|
|
102
|
+
*/
|
|
103
|
+
public clearImportContext(): void {
|
|
104
|
+
globalThis.__IMPORT_CONTEXT = undefined;
|
|
105
|
+
}
|
|
106
|
+
|
|
92
107
|
/**
|
|
93
108
|
* Enforce OptiID authentication for tools by ensuring OptiID auth requirement is present
|
|
94
109
|
* @param authRequirements Original authentication requirements
|
|
@@ -113,6 +128,7 @@ export class ToolsService {
|
|
|
113
128
|
* @param parameters List of parameters for the tool
|
|
114
129
|
* @param endpoint API endpoint for the tool
|
|
115
130
|
* @param authRequirements Authentication requirements (optional)
|
|
131
|
+
* @param isGlobal Whether the tool is global
|
|
116
132
|
*/
|
|
117
133
|
public registerTool<TAuthData>(
|
|
118
134
|
name: string,
|
|
@@ -217,17 +233,18 @@ export class ToolsService {
|
|
|
217
233
|
/**
|
|
218
234
|
* Handle discovery endpoint requests with context-aware filtering
|
|
219
235
|
* @param functionContext The function context making the request
|
|
220
|
-
* @returns Response with filtered functions based on
|
|
236
|
+
* @returns Response with filtered functions based on where tools are defined/imported
|
|
221
237
|
*/
|
|
222
238
|
private handleDiscovery(functionContext: ToolFunction | GlobalToolFunction): App.Response {
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
const availableFunctions = Array.from(this.functions.values()).filter((
|
|
226
|
-
if (
|
|
227
|
-
// Global
|
|
228
|
-
return
|
|
239
|
+
const isGlobalContext = functionContext instanceof GlobalToolFunction;
|
|
240
|
+
|
|
241
|
+
const availableFunctions = Array.from(this.functions.values()).filter((tool) => {
|
|
242
|
+
if (isGlobalContext) {
|
|
243
|
+
// Global context can only see global tools (defined in or imported into GlobalToolFunction)
|
|
244
|
+
return tool.isGlobal;
|
|
229
245
|
} else {
|
|
230
|
-
|
|
246
|
+
// Regular context can only see regular tools (defined in or imported into ToolFunction)
|
|
247
|
+
return !tool.isGlobal;
|
|
231
248
|
}
|
|
232
249
|
});
|
|
233
250
|
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { toolsService } from '../service/Service';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Import a module as global tools
|
|
5
|
+
* @param modulePath The path to the module to import
|
|
6
|
+
* @param baseUrl The base URL for resolving relative imports (optional)
|
|
7
|
+
* @returns The imported module
|
|
8
|
+
*/
|
|
9
|
+
export async function importAsGlobal<T = any>(
|
|
10
|
+
modulePath: string,
|
|
11
|
+
baseUrl?: string
|
|
12
|
+
): Promise<T> {
|
|
13
|
+
try {
|
|
14
|
+
toolsService.setImportContext('global');
|
|
15
|
+
// If baseUrl is provided, resolve the module path relative to it
|
|
16
|
+
// Otherwise, use the modulePath as-is
|
|
17
|
+
const resolvedPath = baseUrl ? new URL(modulePath, baseUrl).href : modulePath;
|
|
18
|
+
const module = await import(resolvedPath);
|
|
19
|
+
return module;
|
|
20
|
+
} finally {
|
|
21
|
+
toolsService.clearImportContext();
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Import a module as regular tools
|
|
27
|
+
* @param modulePath The path to the module to import
|
|
28
|
+
* @param baseUrl The base URL for resolving relative imports (optional)
|
|
29
|
+
* @returns The imported module
|
|
30
|
+
*/
|
|
31
|
+
export async function importAsRegular<T = any>(
|
|
32
|
+
modulePath: string,
|
|
33
|
+
baseUrl?: string
|
|
34
|
+
): Promise<T> {
|
|
35
|
+
try {
|
|
36
|
+
toolsService.setImportContext('regular');
|
|
37
|
+
// If baseUrl is provided, resolve the module path relative to it
|
|
38
|
+
// Otherwise, use the modulePath as-is
|
|
39
|
+
const resolvedPath = baseUrl ? new URL(modulePath, baseUrl).href : modulePath;
|
|
40
|
+
const module = await import(resolvedPath);
|
|
41
|
+
return module;
|
|
42
|
+
} finally {
|
|
43
|
+
toolsService.clearImportContext();
|
|
44
|
+
}
|
|
45
|
+
}
|