@optimizely-opal/opal-tool-ocp-sdk 1.0.0-beta.7 → 1.0.0-beta.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.
Files changed (58) hide show
  1. package/dist/auth/AuthUtils.d.ts +7 -0
  2. package/dist/auth/AuthUtils.d.ts.map +1 -1
  3. package/dist/auth/AuthUtils.js +27 -0
  4. package/dist/auth/AuthUtils.js.map +1 -1
  5. package/dist/auth/AuthUtils.test.js +99 -0
  6. package/dist/auth/AuthUtils.test.js.map +1 -1
  7. package/dist/function/GlobalToolFunction.d.ts +1 -1
  8. package/dist/function/GlobalToolFunction.d.ts.map +1 -1
  9. package/dist/function/GlobalToolFunction.js +4 -1
  10. package/dist/function/GlobalToolFunction.js.map +1 -1
  11. package/dist/function/ToolFunction.d.ts +1 -1
  12. package/dist/function/ToolFunction.d.ts.map +1 -1
  13. package/dist/function/ToolFunction.js +12 -2
  14. package/dist/function/ToolFunction.js.map +1 -1
  15. package/dist/function/ToolFunction.test.js +206 -0
  16. package/dist/function/ToolFunction.test.js.map +1 -1
  17. package/dist/index.d.ts +1 -1
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +2 -1
  20. package/dist/index.js.map +1 -1
  21. package/dist/logging/ToolLogger.d.ts.map +1 -1
  22. package/dist/logging/ToolLogger.js +2 -1
  23. package/dist/logging/ToolLogger.js.map +1 -1
  24. package/dist/logging/ToolLogger.test.js +114 -2
  25. package/dist/logging/ToolLogger.test.js.map +1 -1
  26. package/dist/service/Service.d.ts +73 -0
  27. package/dist/service/Service.d.ts.map +1 -1
  28. package/dist/service/Service.js +188 -42
  29. package/dist/service/Service.js.map +1 -1
  30. package/dist/service/Service.test.js +380 -34
  31. package/dist/service/Service.test.js.map +1 -1
  32. package/dist/types/Models.d.ts +3 -1
  33. package/dist/types/Models.d.ts.map +1 -1
  34. package/dist/types/Models.js +5 -1
  35. package/dist/types/Models.js.map +1 -1
  36. package/dist/types/ToolError.d.ts +13 -0
  37. package/dist/types/ToolError.d.ts.map +1 -1
  38. package/dist/types/ToolError.js +30 -2
  39. package/dist/types/ToolError.js.map +1 -1
  40. package/dist/types/ToolError.test.js +27 -3
  41. package/dist/types/ToolError.test.js.map +1 -1
  42. package/dist/validation/ParameterValidator.test.js +2 -1
  43. package/dist/validation/ParameterValidator.test.js.map +1 -1
  44. package/package.json +1 -1
  45. package/src/auth/AuthUtils.test.ts +115 -1
  46. package/src/auth/AuthUtils.ts +30 -1
  47. package/src/function/GlobalToolFunction.ts +5 -2
  48. package/src/function/ToolFunction.test.ts +220 -0
  49. package/src/function/ToolFunction.ts +13 -4
  50. package/src/index.ts +1 -1
  51. package/src/logging/ToolLogger.test.ts +118 -2
  52. package/src/logging/ToolLogger.ts +2 -1
  53. package/src/service/Service.test.ts +474 -32
  54. package/src/service/Service.ts +245 -41
  55. package/src/types/Models.ts +3 -1
  56. package/src/types/ToolError.test.ts +33 -3
  57. package/src/types/ToolError.ts +32 -2
  58. package/src/validation/ParameterValidator.test.ts +4 -1
@@ -2,7 +2,7 @@
2
2
  import { AuthRequirement, IslandResponse, Parameter } from '../types/Models';
3
3
  import { ToolError } from '../types/ToolError';
4
4
  import * as App from '@zaiusinc/app-sdk';
5
- import { logger, Headers } from '@zaiusinc/app-sdk';
5
+ import { logger, Headers, getAppContext } from '@zaiusinc/app-sdk';
6
6
  import { ToolFunction } from '../function/ToolFunction';
7
7
  import { GlobalToolFunction } from '../function/GlobalToolFunction';
8
8
  import { ParameterValidator } from '../validation/ParameterValidator';
@@ -17,6 +17,27 @@ export class NestedInteractions {
17
17
  }
18
18
  }
19
19
 
20
+ /**
21
+ * Interface for tool description override data (compatible with KV store)
22
+ */
23
+ interface ToolOverride extends App.ValueHash {
24
+ name: string;
25
+ description: string;
26
+ parameters: ParameterOverride[];
27
+ }
28
+
29
+ interface ParameterOverride extends App.ValueHash {
30
+ name: string;
31
+ description: string;
32
+ }
33
+
34
+ /**
35
+ * Interface for stored override data in KV store
36
+ */
37
+ interface StoredOverrides extends App.ValueHash {
38
+ [tool_name: string]: ToolOverride;
39
+ }
40
+
20
41
  /**
21
42
  * Result type for interaction handlers
22
43
  */
@@ -96,6 +117,107 @@ export class ToolsService {
96
117
  private functions: Map<string, Tool<any>> = new Map();
97
118
  private interactions: Map<string, Interaction<any>> = new Map();
98
119
 
120
+ /**
121
+ * Generate KV store key for tool overrides
122
+ * @param appVersionId App version ID
123
+ * @param functionName Function name
124
+ * @returns KV store key
125
+ */
126
+ private getOverrideKey(appVersionId: string, functionName: string): string {
127
+ return `${appVersionId}:${functionName}:opal-tools-overrides`;
128
+ }
129
+
130
+ /**
131
+ * Get tool overrides from KV store
132
+ * @param appVersionId App version ID
133
+ * @param functionName Function name
134
+ * @returns Stored overrides or null if not found
135
+ */
136
+ private async getOverrides(appVersionId: string, functionName: string): Promise<StoredOverrides> {
137
+ const key = this.getOverrideKey(appVersionId, functionName);
138
+ return await App.storage.kvStore.get<StoredOverrides>(key);
139
+ }
140
+
141
+ /**
142
+ * Apply overrides to tool definitions
143
+ * @param tools Original tool definitions
144
+ * @param overrides Override data
145
+ * @returns Tools with overrides applied
146
+ */
147
+ private applyOverrides(tools: Array<Tool<any>>, overrides: StoredOverrides): Array<Tool<any>> {
148
+ return tools.map((tool) => {
149
+ const override = overrides[tool.name];
150
+ if (!override) {
151
+ return tool;
152
+ }
153
+
154
+ // Clone the tool and apply overrides
155
+ const overriddenTool = new Tool(
156
+ tool.name,
157
+ override.description,
158
+ tool.parameters.map((param) => {
159
+ const paramOverride = override.parameters?.find((p) => p.name === param.name);
160
+ if (paramOverride) {
161
+ return new Parameter(
162
+ param.name,
163
+ param.type,
164
+ paramOverride.description,
165
+ param.required
166
+ );
167
+ }
168
+ return param;
169
+ }),
170
+ tool.endpoint,
171
+ tool.handler,
172
+ tool.authRequirements
173
+ );
174
+ overriddenTool.httpMethod = tool.httpMethod;
175
+ return overriddenTool;
176
+ });
177
+ }
178
+
179
+ /**
180
+ * Save tool description overrides
181
+ * @param appVersionId App version ID
182
+ * @param functionName Function name
183
+ * @param overrideData Override data from request
184
+ */
185
+ public async saveToolOverrides(
186
+ appVersionId: string,
187
+ functionName: string,
188
+ overrides: StoredOverrides
189
+ ): Promise<void> {
190
+ const key = this.getOverrideKey(appVersionId, functionName);
191
+ await App.storage.kvStore.patch(key, overrides);
192
+ }
193
+
194
+ /**
195
+ * Delete tool description overrides
196
+ * @param appVersionId App version ID
197
+ * @param functionName Function name
198
+ */
199
+ public async deleteToolOverrides(appVersionId: string, functionName: string): Promise<void> {
200
+ const key = this.getOverrideKey(appVersionId, functionName);
201
+ await App.storage.kvStore.delete(key);
202
+ }
203
+
204
+ /**
205
+ * Get tool definitions with overrides applied
206
+ * @param appVersionId App version ID
207
+ * @param functionName Function name
208
+ * @returns Tool definitions with overrides applied
209
+ */
210
+ public async getToolsWithOverrides(appVersionId: string, functionName: string): Promise<Array<Tool<any>>> {
211
+ const tools = Array.from(this.functions.values());
212
+ const overrides = await this.getOverrides(appVersionId, functionName);
213
+
214
+ if (overrides) {
215
+ return this.applyOverrides(tools, overrides);
216
+ }
217
+
218
+ return tools;
219
+ }
220
+
99
221
  /**
100
222
  * Enforce OptiID authentication for tools by ensuring OptiID auth requirement is present
101
223
  * @param authRequirements Original authentication requirements
@@ -200,59 +322,141 @@ export class ToolsService {
200
322
  req: App.Request,
201
323
  functionContext: ToolFunction | GlobalToolFunction
202
324
  ): Promise<App.Response> {
325
+ // Handle discovery endpoint with overrides
203
326
  if (req.path === '/discovery') {
204
- return new App.Response(200, { functions: Array.from(this.functions.values()).map((f) => f.toJSON()) });
205
- } else {
206
- const func = this.functions.get(req.path);
207
- if (func) {
208
- try {
209
- let params;
210
- if (req.bodyJSON && req.bodyJSON.parameters) {
211
- params = req.bodyJSON.parameters;
212
- } else {
213
- params = req.bodyJSON;
214
- }
327
+ return await this.handleDiscoveryRequest(functionContext);
328
+ }
215
329
 
216
- // Validate parameters before calling the handler (only if tool has parameter definitions)
217
- // ParameterValidator.validate() throws ToolError if validation fails
218
- if (func.parameters && func.parameters.length > 0) {
219
- ParameterValidator.validate(params, func.parameters, func.endpoint);
220
- }
330
+ // Handle overrides endpoint
331
+ if (req.path === '/overrides') {
332
+ return await this.handleOverridesRequest(req, functionContext);
333
+ }
221
334
 
222
- // Extract auth data from body JSON
223
- const authData = req.bodyJSON ? req.bodyJSON.auth : undefined;
335
+ // Handle regular tool functions
336
+ const func = this.functions.get(req.path);
337
+ if (func) {
338
+ try {
339
+ let params;
340
+ if (req.bodyJSON && req.bodyJSON.parameters) {
341
+ params = req.bodyJSON.parameters;
342
+ } else {
343
+ params = req.bodyJSON;
344
+ }
224
345
 
225
- const result = await func.handler(functionContext, params, authData);
226
- return new App.Response(200, result);
227
- } catch (error: any) {
228
- logger.error(`Error in function ${func.name}:`, error);
229
- return this.formatErrorResponse(error, func.endpoint);
346
+ // Validate parameters before calling the handler (only if tool has parameter definitions)
347
+ // ParameterValidator.validate() throws ToolError if validation fails
348
+ if (func.parameters && func.parameters.length > 0) {
349
+ ParameterValidator.validate(params, func.parameters, func.endpoint);
230
350
  }
351
+
352
+ // Extract auth data from body JSON
353
+ const authData = req.bodyJSON ? req.bodyJSON.auth : undefined;
354
+ const result = await func.handler(functionContext, params, authData);
355
+ return new App.Response(200, result);
356
+ } catch (error: any) {
357
+ logger.error(`Error in function ${func.name}:`, error);
358
+ return this.formatErrorResponse(error, func.endpoint);
231
359
  }
360
+ }
232
361
 
233
- const interaction = this.interactions.get(req.path);
234
- if (interaction) {
235
- try {
236
- let params;
237
- if (req.bodyJSON && req.bodyJSON.data) {
238
- params = req.bodyJSON.data;
239
- } else {
240
- params = req.bodyJSON;
241
- }
362
+ // Handle interactions
363
+ const interaction = this.interactions.get(req.path);
364
+ if (interaction) {
365
+ try {
366
+ let params;
367
+ if (req.bodyJSON && req.bodyJSON.data) {
368
+ params = req.bodyJSON.data;
369
+ } else {
370
+ params = req.bodyJSON;
371
+ }
372
+
373
+ // Extract auth data from body JSON
374
+ const authData = req.bodyJSON ? req.bodyJSON.auth : undefined;
375
+
376
+ const result = await interaction.handler(functionContext, params, authData);
377
+ return new App.Response(200, result);
378
+ } catch (error: any) {
379
+ logger.error(`Error in function ${interaction.name}:`, error);
380
+ return this.formatErrorResponse(error, interaction.endpoint);
381
+
382
+ }
383
+ }
384
+ return new App.Response(404, 'Function not found');
385
+ }
386
+
387
+ /**
388
+ * Handle discovery endpoint with overrides applied
389
+ * @param functionContext The function context to get function name from
390
+ * @returns Response with tool definitions
391
+ */
392
+ private async handleDiscoveryRequest(functionContext: ToolFunction | GlobalToolFunction): Promise<App.Response> {
393
+ try {
394
+ // Get app version from app context
395
+ const appVersionId = getAppContext().manifest.meta.version;
396
+ // Get function name from function context
397
+ const functionName = functionContext.constructor.name;
398
+
399
+ const toolsWithOverrides = await this.getToolsWithOverrides(appVersionId, functionName);
400
+ return new App.Response(200, { functions: toolsWithOverrides.map((f) => f.toJSON()) });
401
+ } catch (error: any) {
402
+ logger.error('Error getting tools with overrides:', error);
403
+ // Fallback to original tools if override fetch fails
404
+ return new App.Response(200, { functions: Array.from(this.functions.values()).map((f) => f.toJSON()) });
405
+ }
406
+ }
242
407
 
243
- // Extract auth data from body JSON
244
- const authData = req.bodyJSON ? req.bodyJSON.auth : undefined;
408
+ /**
409
+ * Handle overrides endpoint for saving and deleting tool description overrides
410
+ * @param req The request object
411
+ * @param functionContext The function context to get function name from
412
+ * @returns Response indicating success or failure
413
+ */
414
+ private async handleOverridesRequest(
415
+ req: App.Request,
416
+ functionContext: ToolFunction | GlobalToolFunction
417
+ ): Promise<App.Response> {
418
+ if (req.method === 'PATCH') {
419
+ try {
420
+ // Get app version from app context
421
+ const appVersionId = getAppContext().manifest.meta.version;
422
+ // Get function name from function context
423
+ const functionName = functionContext.constructor.name;
245
424
 
246
- const result = await interaction.handler(functionContext, params, authData);
247
- return new App.Response(200, result);
248
- } catch (error: any) {
249
- logger.error(`Error in function ${interaction.name}:`, error);
250
- return this.formatErrorResponse(error, interaction.endpoint);
425
+ if (!req.bodyJSON?.functions || !Array.isArray(req.bodyJSON.functions)) {
426
+ return new App.Response(400, { error: 'Invalid request body. Expected functions array.' });
251
427
  }
428
+
429
+ // Convert array format to map format for storage
430
+ const overrideData: StoredOverrides = (req.bodyJSON.functions as ToolOverride[]).reduce(
431
+ (map: StoredOverrides, tool: ToolOverride) => {
432
+ map[tool.name] = tool;
433
+ return map;
434
+ },
435
+ {}
436
+ );
437
+
438
+ await this.saveToolOverrides(appVersionId, functionName, overrideData);
439
+ return new App.Response(200, { success: true });
440
+ } catch (error: any) {
441
+ logger.error('Error saving tool overrides:', error);
442
+ return new App.Response(500, { error: error.message || 'Unknown error' });
252
443
  }
444
+ } else if (req.method === 'DELETE') {
445
+ try {
446
+ // Get app version from app context
447
+ const appVersionId = getAppContext().manifest.meta.version;
448
+ // Get function name from function context
449
+ const functionName = functionContext.constructor.name;
253
450
 
254
- return new App.Response(404, 'Function not found');
451
+ await this.deleteToolOverrides(appVersionId, functionName);
452
+ return new App.Response(200, { success: true });
453
+ } catch (error: any) {
454
+ logger.error('Error deleting tool overrides:', error);
455
+ return new App.Response(500, { error: error.message || 'Unknown error' });
456
+ }
255
457
  }
458
+
459
+ return new App.Response(405, { error: 'Method not allowed' });
256
460
  }
257
461
  }
258
462
 
@@ -143,7 +143,9 @@ export class IslandConfig {
143
143
 
144
144
  public constructor(
145
145
  public fields: IslandField[],
146
- public actions: IslandAction[]
146
+ public actions: IslandAction[],
147
+ public type?: 'card' | 'button',
148
+ public icon?: string,
147
149
  ) {}
148
150
  }
149
151
 
@@ -5,7 +5,8 @@ describe('ToolError', () => {
5
5
  it('should create error with message and default status 500', () => {
6
6
  const error = new ToolError('Something went wrong');
7
7
 
8
- expect(error.message).toBe('Something went wrong');
8
+ expect(error.message).toBe('[500] Something went wrong');
9
+ expect(error.title).toBe('Something went wrong');
9
10
  expect(error.status).toBe(500);
10
11
  expect(error.detail).toBeUndefined();
11
12
  expect(error.name).toBe('ToolError');
@@ -14,7 +15,8 @@ describe('ToolError', () => {
14
15
  it('should create error with custom status code', () => {
15
16
  const error = new ToolError('Not found', 404);
16
17
 
17
- expect(error.message).toBe('Not found');
18
+ expect(error.message).toBe('[404] Not found');
19
+ expect(error.title).toBe('Not found');
18
20
  expect(error.status).toBe(404);
19
21
  expect(error.detail).toBeUndefined();
20
22
  });
@@ -22,11 +24,39 @@ describe('ToolError', () => {
22
24
  it('should create error with message, status, and detail', () => {
23
25
  const error = new ToolError('Validation failed', 400, 'Email format is invalid');
24
26
 
25
- expect(error.message).toBe('Validation failed');
27
+ expect(error.message).toBe('[400] Validation failed: Email format is invalid');
28
+ expect(error.title).toBe('Validation failed');
26
29
  expect(error.status).toBe(400);
27
30
  expect(error.detail).toBe('Email format is invalid');
28
31
  });
29
32
 
33
+ it('should create error with errors array in message', () => {
34
+ const errors = [
35
+ { field: 'email', message: 'Invalid email format' },
36
+ { field: 'age', message: 'Must be a positive number' }
37
+ ];
38
+ const error = new ToolError('Validation failed', 400, "See 'errors' field for details.", errors);
39
+
40
+ expect(error.message).toBe(
41
+ '[400] Validation failed: email (Invalid email format); age (Must be a positive number)'
42
+ );
43
+ expect(error.title).toBe('Validation failed');
44
+ expect(error.status).toBe(400);
45
+ expect(error.detail).toBe("See 'errors' field for details.");
46
+ expect(error.errors).toEqual(errors);
47
+ });
48
+
49
+ it('should create error with single error in message', () => {
50
+ const errors = [{ field: 'username', message: 'Required field is missing' }];
51
+ const error = new ToolError('Validation failed', 400, undefined, errors);
52
+
53
+ expect(error.message).toBe('[400] Validation failed: username (Required field is missing)');
54
+ expect(error.title).toBe('Validation failed');
55
+ expect(error.status).toBe(400);
56
+ expect(error.detail).toBeUndefined();
57
+ expect(error.errors).toEqual(errors);
58
+ });
59
+
30
60
  it('should be an instance of Error', () => {
31
61
  const error = new ToolError('Test error');
32
62
 
@@ -9,18 +9,27 @@ export interface ValidationError {
9
9
  /**
10
10
  * Custom error class for tool functions that supports RFC 9457 Problem Details format
11
11
  *
12
+ * The error message includes status code and details for better logging:
13
+ * - No detail: "[status] message"
14
+ * - With detail: "[status] message: detail"
15
+ * - With errors: "[status] message: field1 (error1); field2 (error2)"
16
+ *
12
17
  * @example
13
18
  * ```typescript
14
19
  * // Throw a 404 error
20
+ * // Message: "[404] Resource not found: The requested task does not exist"
15
21
  * throw new ToolError('Resource not found', 404, 'The requested task does not exist');
16
22
  *
17
23
  * // Throw a 400 error
24
+ * // Message: "[400] Invalid input: The priority must be "low", "medium", or "high""
18
25
  * throw new ToolError('Invalid input', 400, 'The priority must be "low", "medium", or "high"');
19
26
  *
20
27
  * // Throw a 500 error (default)
28
+ * // Message: "[500] Database connection failed"
21
29
  * throw new ToolError('Database connection failed');
22
30
  *
23
31
  * // Throw a validation error with multiple field errors
32
+ * // Message: "[400] Validation failed: email (Invalid email format); age (Must be a positive number)"
24
33
  * throw new ToolError('Validation failed', 400, "See 'errors' field for details.", [
25
34
  * { field: 'email', message: 'Invalid email format' },
26
35
  * { field: 'age', message: 'Must be a positive number' }
@@ -43,6 +52,11 @@ export class ToolError extends Error {
43
52
  */
44
53
  public readonly errors?: ValidationError[];
45
54
 
55
+ /**
56
+ * The title field for RFC 9457 format (same as message when no detail is provided)
57
+ */
58
+ public readonly title: string;
59
+
46
60
  /**
47
61
  * Create a new ToolError
48
62
  *
@@ -57,8 +71,24 @@ export class ToolError extends Error {
57
71
  detail?: string,
58
72
  errors?: ValidationError[]
59
73
  ) {
60
- super(message);
74
+ // Build comprehensive error message for logging/debugging
75
+ // Format: [status] message: details
76
+ let fullMessage = `[${status}] ${message}`;
77
+
78
+ if (errors && errors.length > 0) {
79
+ // Errors take precedence: field1 (message1); field2 (message2)
80
+ const errorDetails = errors
81
+ .map((err) => `${err.field} (${err.message})`)
82
+ .join('; ');
83
+ fullMessage += `: ${errorDetails}`;
84
+ } else if (detail) {
85
+ // Include detail if no errors
86
+ fullMessage += `: ${detail}`;
87
+ }
88
+
89
+ super(fullMessage);
61
90
  this.name = 'ToolError';
91
+ this.title = message;
62
92
  this.status = status;
63
93
  this.detail = detail;
64
94
  this.errors = errors;
@@ -77,7 +107,7 @@ export class ToolError extends Error {
77
107
  */
78
108
  public toProblemDetails(instance: string): Record<string, unknown> {
79
109
  const problemDetails: Record<string, unknown> = {
80
- title: this.message,
110
+ title: this.title,
81
111
  status: this.status,
82
112
  instance
83
113
  };
@@ -49,7 +49,10 @@ describe('ParameterValidator', () => {
49
49
  expect(error).toBeInstanceOf(ToolError);
50
50
  const toolError = error as ToolError;
51
51
  expect(toolError.status).toBe(400);
52
- expect(toolError.message).toBe('One or more validation errors occurred.');
52
+ expect(toolError.message).toBe(
53
+ "[400] One or more validation errors occurred.: email (Required parameter 'email' is missing)"
54
+ );
55
+ expect(toolError.title).toBe('One or more validation errors occurred.');
53
56
  expect(toolError.detail).toBe("See 'errors' field for details.");
54
57
  expect(toolError.errors).toHaveLength(1);
55
58
  expect(toolError.errors).toEqual([{