@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.
@@ -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 context type
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
- // Filter tools based on the function context type
224
- const isGlobalFunction = functionContext instanceof GlobalToolFunction;
225
- const availableFunctions = Array.from(this.functions.values()).filter((f) => {
226
- if (isGlobalFunction) {
227
- // Global functions can only see global tools
228
- return f.isGlobal;
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
- return !f.isGlobal;
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
+ }