@optimizely-opal/opal-tool-ocp-sdk 0.0.0-beta.1 → 0.0.0-beta.11

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.
@@ -2,6 +2,7 @@
2
2
  import { AuthRequirement, Parameter } from '../types/Models';
3
3
  import * as App from '@zaiusinc/app-sdk';
4
4
  import { logger } from '@zaiusinc/app-sdk';
5
+ import { ToolFunction } from '../function/ToolFunction';
5
6
 
6
7
 
7
8
 
@@ -22,7 +23,7 @@ export class Interaction<TAuthData> {
22
23
  public constructor(
23
24
  public name: string,
24
25
  public endpoint: string,
25
- public handler: (data: unknown, authData?: TAuthData) => Promise<InteractionResult>
26
+ public handler: (functionContext: ToolFunction, data: unknown, authData?: TAuthData) => Promise<InteractionResult>
26
27
  ) {}
27
28
  }
28
29
 
@@ -49,7 +50,7 @@ export class Tool<TAuthData> {
49
50
  public description: string,
50
51
  public parameters: Parameter[],
51
52
  public endpoint: string,
52
- public handler: (params: unknown, authData?: TAuthData) => Promise<unknown>,
53
+ public handler: (functionContext: ToolFunction, params: unknown, authData?: TAuthData) => Promise<unknown>,
53
54
  public authRequirements?: AuthRequirement[]
54
55
  ) {}
55
56
 
@@ -104,7 +105,7 @@ export class ToolsService {
104
105
  public registerTool<TAuthData>(
105
106
  name: string,
106
107
  description: string,
107
- handler: (params: unknown, authData?: TAuthData) => Promise<unknown>,
108
+ handler: (functionContext: ToolFunction, params: unknown, authData?: TAuthData) => Promise<unknown>,
108
109
  parameters: Parameter[],
109
110
  endpoint: string,
110
111
  authRequirements?: AuthRequirement[]
@@ -121,14 +122,15 @@ export class ToolsService {
121
122
  */
122
123
  public registerInteraction<TAuthData>(
123
124
  name: string,
124
- handler: (data: unknown, authData?: TAuthData) => Promise<InteractionResult>,
125
+ handler: (functionContext: ToolFunction, data: unknown, authData?: TAuthData) => Promise<InteractionResult>,
125
126
  endpoint: string
126
127
  ): void {
127
128
  const func = new Interaction<TAuthData>(name, endpoint, handler);
128
129
  this.interactions.set(endpoint, func);
129
130
  }
130
131
 
131
- public async processRequest(req: App.Request): Promise<App.Response> {
132
+ public async processRequest(req: App.Request,
133
+ functionContext: ToolFunction): Promise<App.Response> {
132
134
  if (req.path === '/discovery') {
133
135
  return new App.Response(200, { functions: Array.from(this.functions.values()).map((f) => f.toJSON()) });
134
136
  } else {
@@ -145,7 +147,7 @@ export class ToolsService {
145
147
  // Extract auth data from body JSON
146
148
  const authData = req.bodyJSON ? req.bodyJSON.auth : undefined;
147
149
 
148
- const result = await func.handler(params, authData);
150
+ const result = await func.handler(functionContext, params, authData);
149
151
  return new App.Response(200, result);
150
152
  } catch (error: any) {
151
153
  logger.error(`Error in function ${func.name}:`, error);
@@ -166,7 +168,7 @@ export class ToolsService {
166
168
  // Extract auth data from body JSON
167
169
  const authData = req.bodyJSON ? req.bodyJSON.auth : undefined;
168
170
 
169
- const result = await interaction.handler(params, authData);
171
+ const result = await interaction.handler(functionContext, params, authData);
170
172
  return new App.Response(200, result);
171
173
  } catch (error: any) {
172
174
  logger.error(`Error in function ${interaction.name}:`, error);