@slates/provider-handler 1.0.0-rc.6 → 1.0.0-rc.8
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/dist/index.cjs +1000 -2
- package/dist/index.d.cts +8 -0
- package/dist/index.d.ts +6 -3
- package/dist/index.module.js +972 -2
- package/package.json +10 -9
- package/src/index.ts +550 -84
- package/src/spec.ts +9 -8
- package/src/validation.ts +1 -1
- package/dist/action/action.d.ts +0 -90
- package/dist/action/action.d.ts.map +0 -1
- package/dist/action/builder.d.ts +0 -27
- package/dist/action/builder.d.ts.map +0 -1
- package/dist/action/index.d.ts +0 -5
- package/dist/action/index.d.ts.map +0 -1
- package/dist/action/tool.d.ts +0 -11
- package/dist/action/tool.d.ts.map +0 -1
- package/dist/action/trigger.d.ts +0 -14
- package/dist/action/trigger.d.ts.map +0 -1
- package/dist/auth/auth.d.ts +0 -26
- package/dist/auth/auth.d.ts.map +0 -1
- package/dist/auth/index.d.ts +0 -3
- package/dist/auth/index.d.ts.map +0 -1
- package/dist/auth/types.d.ts +0 -148
- package/dist/auth/types.d.ts.map +0 -1
- package/dist/axios/index.d.ts +0 -4
- package/dist/axios/index.d.ts.map +0 -1
- package/dist/config/config.d.ts +0 -22
- package/dist/config/config.d.ts.map +0 -1
- package/dist/config/index.d.ts +0 -2
- package/dist/config/index.d.ts.map +0 -1
- package/dist/context/context.d.ts +0 -20
- package/dist/context/context.d.ts.map +0 -1
- package/dist/context/hook.d.ts +0 -4
- package/dist/context/hook.d.ts.map +0 -1
- package/dist/context/index.d.ts +0 -2
- package/dist/context/index.d.ts.map +0 -1
- package/dist/error/base.d.ts +0 -5
- package/dist/error/base.d.ts.map +0 -1
- package/dist/error/declaration.d.ts +0 -6
- package/dist/error/declaration.d.ts.map +0 -1
- package/dist/error/index.d.ts +0 -3
- package/dist/error/index.d.ts.map +0 -1
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.modern.js +0 -2
- package/dist/index.modern.js.map +0 -1
- package/dist/index.module.js.map +0 -1
- package/dist/index.umd.js +0 -2
- package/dist/index.umd.js.map +0 -1
- package/dist/spec.d.ts +0 -14
- package/dist/spec.d.ts.map +0 -1
- package/dist/specification/index.d.ts +0 -3
- package/dist/specification/index.d.ts.map +0 -1
- package/dist/specification/slate.d.ts +0 -15
- package/dist/specification/slate.d.ts.map +0 -1
- package/dist/specification/specification.d.ts +0 -30
- package/dist/specification/specification.d.ts.map +0 -1
- package/dist/specification/zero.d.ts +0 -13
- package/dist/specification/zero.d.ts.map +0 -1
- package/dist/state.d.ts +0 -8
- package/dist/state.d.ts.map +0 -1
- package/dist/tokens.d.ts +0 -34
- package/dist/tokens.d.ts.map +0 -1
- package/dist/validation.d.ts +0 -5
- package/dist/validation.d.ts.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -1,2 +1,1000 @@
|
|
|
1
|
-
var t=require("@lowerdeck/error"),e=require("@slates/proto"),r=require("@slates/provider");function n(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var o=/*#__PURE__*/n(require("zod"));function i(t,e){if(!{}.hasOwnProperty.call(t,e))throw new TypeError("attempted to use private field on non-instance");return t}var a=0;function u(t){return"__private_"+a+++"_"+t}function s(){return s=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)({}).hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t},s.apply(null,arguments)}function c(t){var e=function(t){if("object"!=typeof t||!t)return t;var e=t[Symbol.toPrimitive];if(void 0!==e){var r=e.call(t,"string");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t);return"symbol"==typeof e?e:e+""}var l=function(e,r,n,o){var i=e.safeParse(r);if(!i.success)throw new t.ServiceError(function(e,r,n){return t.validationError({message:r,entity:e,errors:n.issues.map(function(t){return s({},t,{path:t.path.map(function(t){return String(t)})})})})}(n,o,i.error));return i.data},h=function(t){return t.toJSONSchema({unrepresentable:"any",override:function(t){var e=t.zodSchema._zod.def;"date"===e.type&&(t.jsonSchema.type="string",t.jsonSchema.format="date-time"),"bigint"===e.type&&(t.jsonSchema.type="number")}})},p=function(e,r){var n=e.spec.auth.authStack.find(function(t){return t.key==r});if(!n)throw new t.ServiceError(t.badRequestError({message:"Invalid authentication method ID: "+r}));return n},d=function(t,e){var r;return{id:e.key,name:e.name,type:e.type,scopes:"scopes"in e?e.scopes.map(function(t){return{id:t.scope,title:t.title,description:t.description}}):void 0,inputSchema:h(null!=(r=e.inputSchema)?r:o.default.object({})),outputSchema:h(t.spec.auth.outputSchema),capabilities:{getDefaultInput:{enabled:!(!("getDefaultInput"in e)||!e.getDefaultInput)},handleTokenRefresh:{enabled:!(!("handleTokenRefresh"in e)||!e.handleTokenRefresh)},handleChangedInput:{enabled:!!e.onInputChanged},getProfile:{enabled:!!e.getProfile}}}},f=function(e,r){var n=e.actions.find(function(t){return t.key==r});if(!n)throw new t.ServiceError(t.notFoundError("action",r));return n},v=function(e,r,n){var o=f(e,n);if(o.type!=r)throw new t.ServiceError(t.badRequestError({message:"Action with ID "+n+" is not of type "+r}));return o},m=function(t,e){var n;return s({},{id:e.key,name:e.name,description:e.description,instructions:e.instructions,constraints:e.constraints,tags:e.tags,metadata:e.metadata,inputSchema:h(e.inputSchema),outputSchema:h(e.outputSchema)},"tool"==e.type?{type:"action.tool",capabilities:{}}:{type:"action.trigger",capabilities:{},invocation:"polling"==e.source?{type:"polling",intervalSeconds:null!=(n=e.polling.intervalInSeconds)?n:r.SlateDefaultPollingIntervalSeconds}:{type:"webhook",autoRegistration:!!e.autoRegisterWebhook,autoUnregistration:!!e.autoUnregisterWebhook}})},g=/*#__PURE__*/u("value"),P=/*#__PURE__*/function(){function t(t){Object.defineProperty(this,g,{writable:!0,value:void 0}),i(this,g)[g]=t}var e,r,n=t.prototype;return n.get=function(){return i(this,g)[g]},n.set=function(t){i(this,g)[g]=t},e=t,(r=[{key:"value",get:function(){return i(this,g)[g]}}])&&function(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,c(n.key),n)}}(e.prototype,r),Object.defineProperty(e,"prototype",{writable:!1}),e}();exports.createProviderHandler=function(n,o){return e.createSlatesProviderProtoHandler(function(e){try{var i=new P(null),a=new P(null),u=new P(null),c=new P(null),g=new P(null),y=new r.SlateLogger(o),w=function(){var e=i.get(),r=a.get();if(!e||!r)throw new t.ServiceError(t.preconditionFailedError({message:"Connection context has not been initialized"}));return{protocol:e,participants:r}},S=function(){var e=w(),r=c.get(),o=g.get(),i=u.get();if(!r||!o||!i&&n.spec.auth.authStack.length>0)throw new t.ServiceError(t.preconditionFailedError({message:"Session context has not been initialized"}));return s({},e,{config:r.value,session:o,auth:i})},I=function(){return new r.SlateContext({},{},{},n.spec,y)};return e.onNotification("slates/hello",function(t){var e=t.params;try{return i.set(e.protocol),Promise.resolve()}catch(t){return Promise.reject(t)}}),e.onNotification("slates/participant.set",function(e){var r=e.params;try{if(!i.get())throw new t.ServiceError(t.preconditionFailedError({message:"Connection protocol has not been initialized"}));return a.set(r.participants),Promise.resolve()}catch(t){return Promise.reject(t)}}),e.onNotification("slates/auth.set",function(t){var e=t.params;try{w(),p(n,e.authenticationMethodId);var r=l(n.spec.authSchema,e.output,"auth","Invalid authentication output for method ID: "+e.authenticationMethodId);return u.set({authenticationMethodId:e.authenticationMethodId,output:r}),Promise.resolve()}catch(t){return Promise.reject(t)}}),e.onNotification("slates/config.set",function(t){var e=t.params;try{w();var r=l(n.spec.configSchema,e.config,"config","Invalid configuration");return c.set({value:r}),Promise.resolve()}catch(t){return Promise.reject(t)}}),e.onNotification("slates/session.start",function(t){var e=t.params;try{return w(),g.set({id:e.sessionId,state:e.state}),Promise.resolve()}catch(t){return Promise.reject(t)}}),e.onRequest("slates/config.changed",function(t){var e=t.params;try{w();var r=l(n.spec.config.configSchema,e.newConfig,"config","Invalid configuration"),o=n.spec.config.handlers.configChanged;return o?Promise.resolve(o({previousConfig:e.previousConfig,newConfig:r})).then(function(t){var e;return{success:!0,config:null!=(e=null==t?void 0:t.config)?e:r}}):Promise.resolve({success:!0,config:r})}catch(t){return Promise.reject(t)}}),e.onRequest("slates/config.get_default",function(t){try{w();var e=n.spec.config.handlers.getDefaultConfig;return e?Promise.resolve(e()).then(function(t){return{config:t}}):Promise.resolve({config:null})}catch(t){return Promise.reject(t)}}),e.onRequest("slates/config.schema.get",function(t){try{return w(),Promise.resolve({schema:h(n.spec.configSchema)})}catch(t){return Promise.reject(t)}}),e.onRequest("slates/provider.identify",function(t){try{return w(),Promise.resolve({protocol:"slates@2026-01-01",provider:{type:"provider",id:n.spec.key,name:n.spec.name,description:n.spec.description,metadata:n.spec.parameters.metadata}})}catch(t){return Promise.reject(t)}}),e.onRequest("slates/auth.methods.list",function(t){try{return w(),Promise.resolve({authenticationMethods:n.spec.auth.authStack.map(function(t){return d(n,t)})})}catch(t){return Promise.reject(t)}}),e.onRequest("slates/auth.method.get",function(t){var e=t.params;try{w();var r=p(n,e.authenticationMethodId);return Promise.resolve({authenticationMethod:d(n,r)})}catch(t){return Promise.reject(t)}}),e.onRequest("slates/auth.input.get_default",function(t){var e=t.params;try{w();var o=p(n,e.authenticationMethodId);return o.getDefaultInput?Promise.resolve(r.runWithContext(I(),function(){return o.getDefaultInput()})).then(function(t){return{input:t}}):Promise.resolve({input:null})}catch(t){return Promise.reject(t)}}),e.onRequest("slates/auth.input.changed",function(t){var e=t.params;try{w();var o=p(n,e.authenticationMethodId);return o.onInputChanged?Promise.resolve(r.runWithContext(I(),function(){return o.onInputChanged({previousInput:e.previousInput,newInput:e.newInput})})).then(function(t){var r;return{success:!0,input:null!=(r=null==t?void 0:t.input)?r:e.newInput}}):Promise.resolve({success:!0,input:e.newInput})}catch(t){return Promise.reject(t)}}),e.onRequest("slates/auth.output.get",function(t){var e=t.params;try{var o,i=function(t){return o?t:{output:u}};w();var a=p(n,e.authenticationMethodId),u=e.input;a.inputSchema&&(u=l(a.inputSchema,u,"auth","Invalid authentication input for method ID: "+e.authenticationMethodId));var s=function(){if("getOutput"in a)return Promise.resolve(r.runWithContext(I(),function(){return a.getOutput({input:u})})).then(function(t){return o=1,{output:t.output}})}();return Promise.resolve(s&&s.then?s.then(i):i(s))}catch(t){return Promise.reject(t)}}),e.onRequest("slates/auth.authorization_callback.handle",function(e){var o=e.params;try{var i,a=function(e){if(i)return e;throw new t.ServiceError(t.preconditionFailedError({message:"Authentication method does not support authorization callback handling: "+o.authenticationMethodId}))};w();var u=p(n,o.authenticationMethodId),s=function(){if("handleCallback"in u)return Promise.resolve(r.runWithContext(I(),function(){return u.handleCallback({code:o.code,state:o.state,redirectUri:o.redirectUri,input:o.input,clientId:o.clientId,clientSecret:o.clientSecret,scopes:o.scopes,callbackState:o.callbackState||{}})})).then(function(t){return i=1,{output:t.output,input:t.input}})}();return Promise.resolve(s&&s.then?s.then(a):a(s))}catch(t){return Promise.reject(t)}}),e.onRequest("slates/auth.authorization_url.get",function(e){var o=e.params;try{var i,a=function(e){if(i)return e;throw new t.ServiceError(t.preconditionFailedError({message:"Authentication method does not support authorization URL retrieval: "+o.authenticationMethodId}))};w();var u=p(n,o.authenticationMethodId),s=function(){if("getAuthorizationUrl"in u)return Promise.resolve(r.runWithContext(I(),function(){return u.getAuthorizationUrl({redirectUri:o.redirectUri,state:o.state,input:o.input,clientId:o.clientId,clientSecret:o.clientSecret,scopes:o.scopes})})).then(function(t){return i=1,{authorizationUrl:t.url,input:t.input,callbackState:t.callbackState}})}();return Promise.resolve(s&&s.then?s.then(a):a(s))}catch(t){return Promise.reject(t)}}),e.onRequest("slates/auth.profile.get",function(e){var o=e.params;try{var i,a=function(e){if(i)return e;throw new t.ServiceError(t.preconditionFailedError({message:"Authentication method does not support profile retrieval: "+o.authenticationMethodId}))};w();var u=p(n,o.authenticationMethodId),s=function(){if(u.getProfile)return Promise.resolve(r.runWithContext(I(),function(){return u.getProfile({output:o.output,input:o.input,scopes:o.scopes})})).then(function(t){return i=1,{profile:t.profile}})}();return Promise.resolve(s&&s.then?s.then(a):a(s))}catch(t){return Promise.reject(t)}}),e.onRequest("slates/auth.token_refresh.handle",function(e){var o=e.params;try{var i,a=function(e){if(i)return e;throw new t.ServiceError(t.preconditionFailedError({message:"Authentication method does not support token refresh handling: "+o.authenticationMethodId}))};w();var u=p(n,o.authenticationMethodId),s=function(){if("handleTokenRefresh"in u&&u.handleTokenRefresh)return Promise.resolve(r.runWithContext(I(),function(){return u.handleTokenRefresh({output:o.output,input:o.input,clientId:o.clientId,clientSecret:o.clientSecret,scopes:o.scopes})})).then(function(t){return i=1,{output:t.output,input:t.input}})}();return Promise.resolve(s&&s.then?s.then(a):a(s))}catch(t){return Promise.reject(t)}}),e.onRequest("slates/actions.list",function(t){try{return w(),Promise.resolve({actions:n.actions.map(function(t){return m(0,t)})})}catch(t){return Promise.reject(t)}}),e.onRequest("slates/action.get",function(t){var e=t.params;try{w();var r=f(n,e.actionId);return Promise.resolve({action:m(0,r)})}catch(t){return Promise.reject(t)}}),e.onRequest("slates/action.tool.invoke",function(t){var e=t.params;try{var o,i=S(),a=v(n,"tool",e.actionId),u=l(a.inputSchema,e.input,"input","Invalid input for tool ID: "+e.actionId),s=new r.SlateContext(i.config,u,null==(o=i.auth)?void 0:o.output,n.spec,y);return Promise.resolve(r.runWithContext(s,function(){return a.handleInvocation(s)})).then(function(t){return{output:t.output,message:t.message}})}catch(t){return Promise.reject(t)}}),e.onRequest("slates/action.trigger.map_event",function(t){var e=t.params;try{var o,i=S(),a=v(n,"trigger",e.actionId),u=l(a.inputSchema,e.input,"input","Invalid event for trigger ID: "+e.actionId),s=new r.SlateContext(i.config,u,null==(o=i.auth)?void 0:o.output,n.spec,y);return Promise.resolve(r.runWithContext(s,function(){return a.handleEvent(s)})).then(function(t){return{id:t.id,type:t.type,output:t.output}})}catch(t){return Promise.reject(t)}}),e.onRequest("slates/action.trigger.poll_events",function(e){var o=e.params;try{var i,a=S(),u=v(n,"trigger",o.actionId);if(!u.pollEvents)throw new t.ServiceError(t.badRequestError({message:"Trigger action does not support polling: "+o.actionId}));var s=new r.SlateContext(a.config,{state:o.state},null==(i=a.auth)?void 0:i.output,n.spec,y);return Promise.resolve(r.runWithContext(s,function(){return u.pollEvents(s)})).then(function(t){return{inputs:t.inputs,updatedState:t.updatedState}})}catch(t){return Promise.reject(t)}}),e.onRequest("slates/action.trigger.webhook_handle",function(e){var o=e.params;try{var i,a=S(),u=v(n,"trigger",o.actionId);if(!u.handleRequest)throw new t.ServiceError(t.badRequestError({message:"Trigger action does not support webhook requests: "+o.actionId}));var s=new Request(o.url,{method:o.method,headers:o.headers,body:o.body?Uint8Array.from(atob(o.body.content),function(t){return t.charCodeAt(0)}):null}),c=new r.SlateContext(a.config,{request:s,state:o.state},null==(i=a.auth)?void 0:i.output,n.spec,y);return Promise.resolve(r.runWithContext(c,function(){return u.handleRequest(c)})).then(function(t){return{inputs:t.inputs,updatedState:t.updatedState}})}catch(t){return Promise.reject(t)}}),e.onRequest("slates/action.trigger.webhook_register",function(e){var o=e.params;try{var i,a=S(),u=v(n,"trigger",o.actionId);if(!u.autoRegisterWebhook)throw new t.ServiceError(t.badRequestError({message:"Trigger action does not support webhook auto-registration: "+o.actionId}));var s=new r.SlateContext(a.config,{webhookBaseUrl:o.webhookBaseUrl},null==(i=a.auth)?void 0:i.output,n.spec,y);return Promise.resolve(r.runWithContext(s,function(){return u.autoRegisterWebhook(s)})).then(function(t){return{registrationDetails:t.registrationDetails,state:t.state}})}catch(t){return Promise.reject(t)}}),e.onRequest("slates/action.trigger.webhook_unregister",function(e){var o=e.params;try{var i,a=S(),u=v(n,"trigger",o.actionId);if(!u.autoUnregisterWebhook)throw new t.ServiceError(t.badRequestError({message:"Trigger action does not support webhook auto-unregistration: "+o.actionId}));var s=new r.SlateContext(a.config,{webhookBaseUrl:o.webhookBaseUrl,registrationDetails:o.registrationDetails,state:o.state},null==(i=a.auth)?void 0:i.output,n.spec,y);return Promise.resolve(r.runWithContext(s,function(){return u.autoUnregisterWebhook(s)})).then(function(){return{}})}catch(t){return Promise.reject(t)}}),Promise.resolve()}catch(t){return Promise.reject(t)}})};
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
createProviderHandler: () => createProviderHandler
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(index_exports);
|
|
36
|
+
var import_error3 = require("@lowerdeck/error");
|
|
37
|
+
var import_proto = require("@slates/proto");
|
|
38
|
+
var import_provider2 = require("@slates/provider");
|
|
39
|
+
|
|
40
|
+
// src/spec.ts
|
|
41
|
+
var import_error2 = require("@lowerdeck/error");
|
|
42
|
+
var import_provider = require("@slates/provider");
|
|
43
|
+
var import_zod = __toESM(require("zod"), 1);
|
|
44
|
+
|
|
45
|
+
// src/validation.ts
|
|
46
|
+
var import_error = require("@lowerdeck/error");
|
|
47
|
+
var zodToValidationError = (entity, message, e) => {
|
|
48
|
+
return (0, import_error.validationError)({
|
|
49
|
+
message,
|
|
50
|
+
entity,
|
|
51
|
+
errors: e.issues.map((i) => ({
|
|
52
|
+
...i,
|
|
53
|
+
path: i.path.map((p) => String(p))
|
|
54
|
+
}))
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
var validate = (schema, data, entity, message) => {
|
|
58
|
+
let result = schema.safeParse(data);
|
|
59
|
+
if (!result.success) {
|
|
60
|
+
throw new import_error.ServiceError(zodToValidationError(entity, message, result.error));
|
|
61
|
+
}
|
|
62
|
+
return result.data;
|
|
63
|
+
};
|
|
64
|
+
var toJsonSchema = (schema) => schema.toJSONSchema({
|
|
65
|
+
unrepresentable: "any",
|
|
66
|
+
override: (ctx) => {
|
|
67
|
+
let def = ctx.zodSchema._zod.def;
|
|
68
|
+
if (def.type === "date") {
|
|
69
|
+
ctx.jsonSchema.type = "string";
|
|
70
|
+
ctx.jsonSchema.format = "date-time";
|
|
71
|
+
}
|
|
72
|
+
if (def.type === "bigint") {
|
|
73
|
+
ctx.jsonSchema.type = "number";
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
// src/spec.ts
|
|
79
|
+
var getAuthMethod = (slate, authenticationMethodId) => {
|
|
80
|
+
let authMethod = slate.spec.auth.authStack.find((m) => m.key === authenticationMethodId);
|
|
81
|
+
if (!authMethod) {
|
|
82
|
+
throw new import_error2.ServiceError(
|
|
83
|
+
(0, import_error2.badRequestError)({
|
|
84
|
+
message: `Invalid authentication method ID: ${authenticationMethodId}`
|
|
85
|
+
})
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
return authMethod;
|
|
89
|
+
};
|
|
90
|
+
var mapAuthMethod = (slate, m) => ({
|
|
91
|
+
id: m.key,
|
|
92
|
+
name: m.name,
|
|
93
|
+
type: m.type,
|
|
94
|
+
scopes: "scopes" in m ? m.scopes.map((s) => ({
|
|
95
|
+
id: s.scope,
|
|
96
|
+
title: s.title,
|
|
97
|
+
description: s.description
|
|
98
|
+
})) : void 0,
|
|
99
|
+
inputSchema: toJsonSchema(m.inputSchema ?? import_zod.default.object({})),
|
|
100
|
+
outputSchema: toJsonSchema(slate.spec.auth.outputSchema),
|
|
101
|
+
capabilities: {
|
|
102
|
+
getDefaultInput: { enabled: !!("getDefaultInput" in m && m.getDefaultInput) },
|
|
103
|
+
handleTokenRefresh: {
|
|
104
|
+
enabled: !!("handleTokenRefresh" in m && m.handleTokenRefresh)
|
|
105
|
+
},
|
|
106
|
+
handleChangedInput: {
|
|
107
|
+
enabled: !!m.onInputChanged
|
|
108
|
+
},
|
|
109
|
+
getProfile: { enabled: !!m.getProfile }
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
var getAction = (slate, actionId) => {
|
|
113
|
+
let action = slate.actions.find((m) => m.key === actionId);
|
|
114
|
+
if (!action) {
|
|
115
|
+
throw new import_error2.ServiceError((0, import_error2.notFoundError)(`action`, actionId));
|
|
116
|
+
}
|
|
117
|
+
return action;
|
|
118
|
+
};
|
|
119
|
+
var getActionWithType = (slate, type, actionId) => {
|
|
120
|
+
let action = getAction(slate, actionId);
|
|
121
|
+
if (action.type !== type) {
|
|
122
|
+
throw new import_error2.ServiceError(
|
|
123
|
+
(0, import_error2.badRequestError)({
|
|
124
|
+
message: `Action with ID ${actionId} is not of type ${type}`
|
|
125
|
+
})
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
return action;
|
|
129
|
+
};
|
|
130
|
+
var mapAction = (_slate, a) => {
|
|
131
|
+
let base = {
|
|
132
|
+
id: a.key,
|
|
133
|
+
name: a.name,
|
|
134
|
+
description: a.description,
|
|
135
|
+
instructions: a.instructions,
|
|
136
|
+
constraints: a.constraints,
|
|
137
|
+
tags: a.tags,
|
|
138
|
+
metadata: a.metadata,
|
|
139
|
+
scopes: a.scopes,
|
|
140
|
+
inputSchema: toJsonSchema(a.inputSchema),
|
|
141
|
+
outputSchema: toJsonSchema(a.outputSchema)
|
|
142
|
+
};
|
|
143
|
+
if (a.type === "tool") {
|
|
144
|
+
return {
|
|
145
|
+
...base,
|
|
146
|
+
type: "action.tool",
|
|
147
|
+
capabilities: {}
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
return {
|
|
151
|
+
...base,
|
|
152
|
+
type: "action.trigger",
|
|
153
|
+
capabilities: {},
|
|
154
|
+
invocation: a.source === "polling" ? {
|
|
155
|
+
type: "polling",
|
|
156
|
+
intervalSeconds: a.polling.intervalInSeconds ?? import_provider.SlateDefaultPollingIntervalSeconds
|
|
157
|
+
} : {
|
|
158
|
+
type: "webhook",
|
|
159
|
+
autoRegistration: !!a.autoRegisterWebhook,
|
|
160
|
+
autoUnregistration: !!a.autoUnregisterWebhook
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
// src/state.ts
|
|
166
|
+
var State = class {
|
|
167
|
+
#value;
|
|
168
|
+
get value() {
|
|
169
|
+
return this.#value;
|
|
170
|
+
}
|
|
171
|
+
get() {
|
|
172
|
+
return this.#value;
|
|
173
|
+
}
|
|
174
|
+
set(value) {
|
|
175
|
+
this.#value = value;
|
|
176
|
+
}
|
|
177
|
+
constructor(initialValue) {
|
|
178
|
+
this.#value = initialValue;
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
// src/index.ts
|
|
183
|
+
var isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
184
|
+
var getObjectKeyCount = (value) => isRecord(value) ? Object.keys(value).length : void 0;
|
|
185
|
+
var DOWNLOAD_ATTACHMENT_URL_KEYS = /* @__PURE__ */ new Set([
|
|
186
|
+
"downloadUrl",
|
|
187
|
+
"fileUrl",
|
|
188
|
+
"temporaryDownloadUrl",
|
|
189
|
+
"webContentLink"
|
|
190
|
+
]);
|
|
191
|
+
var isAttachmentUrl = (value) => /^[a-z][a-z0-9+.-]*:\/\//i.test(value);
|
|
192
|
+
var collectOutputUrlAttachments = (value, seen = /* @__PURE__ */ new Set()) => {
|
|
193
|
+
if (Array.isArray(value)) {
|
|
194
|
+
return value.flatMap((item) => collectOutputUrlAttachments(item, seen));
|
|
195
|
+
}
|
|
196
|
+
if (!isRecord(value)) {
|
|
197
|
+
return [];
|
|
198
|
+
}
|
|
199
|
+
let attachments = [];
|
|
200
|
+
for (let [key, nestedValue] of Object.entries(value)) {
|
|
201
|
+
if (DOWNLOAD_ATTACHMENT_URL_KEYS.has(key) && typeof nestedValue === "string" && nestedValue.length > 0 && isAttachmentUrl(nestedValue) && !seen.has(nestedValue)) {
|
|
202
|
+
seen.add(nestedValue);
|
|
203
|
+
attachments.push({
|
|
204
|
+
content: {
|
|
205
|
+
type: "url",
|
|
206
|
+
url: nestedValue
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
attachments.push(...collectOutputUrlAttachments(nestedValue, seen));
|
|
212
|
+
}
|
|
213
|
+
return attachments;
|
|
214
|
+
};
|
|
215
|
+
var mergeAttachments = (explicitAttachments, output) => {
|
|
216
|
+
let attachments = [...explicitAttachments ?? []];
|
|
217
|
+
let seen = new Set(attachments.map((attachment) => JSON.stringify(attachment)));
|
|
218
|
+
for (let attachment of collectOutputUrlAttachments(output)) {
|
|
219
|
+
let key = JSON.stringify(attachment);
|
|
220
|
+
if (seen.has(key)) continue;
|
|
221
|
+
seen.add(key);
|
|
222
|
+
attachments.push(attachment);
|
|
223
|
+
}
|
|
224
|
+
return attachments.length > 0 ? attachments : void 0;
|
|
225
|
+
};
|
|
226
|
+
var toErrorMetadata = (error) => {
|
|
227
|
+
if (error instanceof Error) {
|
|
228
|
+
return {
|
|
229
|
+
errorName: error.name,
|
|
230
|
+
errorMessage: error.message,
|
|
231
|
+
errorStack: error.stack
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
return {
|
|
235
|
+
errorValue: String(error)
|
|
236
|
+
};
|
|
237
|
+
};
|
|
238
|
+
var formatEntityLabel = (name, key) => `"${name}" (${key})`;
|
|
239
|
+
var resolveTraceMessage = (message, result) => typeof message === "function" ? message(result) : message;
|
|
240
|
+
var createProviderHandler = (slate, listeners) => (0, import_proto.createSlatesProviderProtoHandler)(async (manager) => {
|
|
241
|
+
let protocol = new State(null);
|
|
242
|
+
let participants = new State(null);
|
|
243
|
+
let auth = new State(null);
|
|
244
|
+
let config = new State(null);
|
|
245
|
+
let session = new State(null);
|
|
246
|
+
let logger = new import_provider2.SlateLogger(listeners);
|
|
247
|
+
let providerTrace = {
|
|
248
|
+
providerId: slate.spec.key,
|
|
249
|
+
providerName: slate.spec.name
|
|
250
|
+
};
|
|
251
|
+
let traceProviderCall = async (trace, handler) => {
|
|
252
|
+
let startedAt = Date.now();
|
|
253
|
+
logger.info({
|
|
254
|
+
...providerTrace,
|
|
255
|
+
...trace.metadata,
|
|
256
|
+
component: trace.component,
|
|
257
|
+
functionName: trace.functionName,
|
|
258
|
+
phase: "start",
|
|
259
|
+
message: trace.message
|
|
260
|
+
});
|
|
261
|
+
try {
|
|
262
|
+
let result = await handler();
|
|
263
|
+
let successMessage = resolveTraceMessage(trace.successMessage, result);
|
|
264
|
+
logger.info({
|
|
265
|
+
...providerTrace,
|
|
266
|
+
...trace.metadata,
|
|
267
|
+
...trace.onSuccess?.(result) ?? {},
|
|
268
|
+
component: trace.component,
|
|
269
|
+
functionName: trace.functionName,
|
|
270
|
+
phase: "success",
|
|
271
|
+
durationMs: Date.now() - startedAt,
|
|
272
|
+
message: successMessage
|
|
273
|
+
});
|
|
274
|
+
return result;
|
|
275
|
+
} catch (error) {
|
|
276
|
+
logger.error({
|
|
277
|
+
...providerTrace,
|
|
278
|
+
...trace.metadata,
|
|
279
|
+
...toErrorMetadata(error),
|
|
280
|
+
component: trace.component,
|
|
281
|
+
functionName: trace.functionName,
|
|
282
|
+
phase: "error",
|
|
283
|
+
durationMs: Date.now() - startedAt,
|
|
284
|
+
message: trace.errorMessage ?? `${typeof trace.successMessage === "string" ? trace.successMessage : trace.message} failed`
|
|
285
|
+
});
|
|
286
|
+
throw error;
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
let getContextBasic = () => {
|
|
290
|
+
let currentProtocol = protocol.get();
|
|
291
|
+
let currentParticipants = participants.get();
|
|
292
|
+
if (!currentProtocol || !currentParticipants) {
|
|
293
|
+
throw new import_error3.ServiceError(
|
|
294
|
+
(0, import_error3.preconditionFailedError)({
|
|
295
|
+
message: "Connection context has not been initialized"
|
|
296
|
+
})
|
|
297
|
+
);
|
|
298
|
+
}
|
|
299
|
+
return {
|
|
300
|
+
protocol: currentProtocol,
|
|
301
|
+
participants: currentParticipants
|
|
302
|
+
};
|
|
303
|
+
};
|
|
304
|
+
let getContextFull = () => {
|
|
305
|
+
let basic = getContextBasic();
|
|
306
|
+
let currentConfig = config.get();
|
|
307
|
+
let currentSession = session.get();
|
|
308
|
+
let currentAuth = auth.get();
|
|
309
|
+
if (!currentConfig || !currentSession || !currentAuth && slate.spec.auth.authStack.length > 0) {
|
|
310
|
+
throw new import_error3.ServiceError(
|
|
311
|
+
(0, import_error3.preconditionFailedError)({
|
|
312
|
+
message: "Session context has not been initialized"
|
|
313
|
+
})
|
|
314
|
+
);
|
|
315
|
+
}
|
|
316
|
+
return {
|
|
317
|
+
...basic,
|
|
318
|
+
config: currentConfig.value,
|
|
319
|
+
session: currentSession,
|
|
320
|
+
auth: currentAuth
|
|
321
|
+
};
|
|
322
|
+
};
|
|
323
|
+
let getEmptyContext = () => new import_provider2.SlateContext({}, {}, {}, slate.spec, logger);
|
|
324
|
+
let withRequestTraces = (context, result) => {
|
|
325
|
+
let requestTraces = context.getHttpTraces();
|
|
326
|
+
return requestTraces.length > 0 ? { ...result, requestTraces } : result;
|
|
327
|
+
};
|
|
328
|
+
manager.onNotification("slates/hello", async ({ params }) => {
|
|
329
|
+
protocol.set(params.protocol);
|
|
330
|
+
});
|
|
331
|
+
manager.onNotification("slates/participant.set", async ({ params }) => {
|
|
332
|
+
if (!protocol.get()) {
|
|
333
|
+
throw new import_error3.ServiceError(
|
|
334
|
+
(0, import_error3.preconditionFailedError)({ message: "Connection protocol has not been initialized" })
|
|
335
|
+
);
|
|
336
|
+
}
|
|
337
|
+
participants.set(params.participants);
|
|
338
|
+
});
|
|
339
|
+
manager.onNotification("slates/auth.set", async ({ params }) => {
|
|
340
|
+
getContextBasic();
|
|
341
|
+
getAuthMethod(slate, params.authenticationMethodId);
|
|
342
|
+
let valRes = validate(
|
|
343
|
+
slate.spec.authSchema,
|
|
344
|
+
params.output,
|
|
345
|
+
"auth",
|
|
346
|
+
`Invalid authentication output for method ID: ${params.authenticationMethodId}`
|
|
347
|
+
);
|
|
348
|
+
auth.set({
|
|
349
|
+
authenticationMethodId: params.authenticationMethodId,
|
|
350
|
+
output: valRes
|
|
351
|
+
});
|
|
352
|
+
});
|
|
353
|
+
manager.onNotification("slates/config.set", async ({ params }) => {
|
|
354
|
+
getContextBasic();
|
|
355
|
+
let value = validate(
|
|
356
|
+
slate.spec.configSchema,
|
|
357
|
+
params.config,
|
|
358
|
+
"config",
|
|
359
|
+
"Invalid configuration"
|
|
360
|
+
);
|
|
361
|
+
config.set({ value });
|
|
362
|
+
});
|
|
363
|
+
manager.onNotification("slates/session.start", async ({ params }) => {
|
|
364
|
+
getContextBasic();
|
|
365
|
+
session.set({
|
|
366
|
+
id: params.sessionId,
|
|
367
|
+
state: params.state
|
|
368
|
+
});
|
|
369
|
+
});
|
|
370
|
+
manager.onRequest("slates/config.changed", async ({ params }) => {
|
|
371
|
+
getContextBasic();
|
|
372
|
+
let newConfig = validate(
|
|
373
|
+
slate.spec.config.configSchema,
|
|
374
|
+
params.newConfig,
|
|
375
|
+
"config",
|
|
376
|
+
"Invalid configuration"
|
|
377
|
+
);
|
|
378
|
+
let configChanged = slate.spec.config.handlers.configChanged;
|
|
379
|
+
if (!configChanged) {
|
|
380
|
+
return { success: true, config: newConfig };
|
|
381
|
+
}
|
|
382
|
+
let context = getEmptyContext();
|
|
383
|
+
let updatedConfig = await traceProviderCall(
|
|
384
|
+
{
|
|
385
|
+
component: "config",
|
|
386
|
+
functionName: "configChanged",
|
|
387
|
+
message: "Running config change handler",
|
|
388
|
+
successMessage: "Config change handler completed",
|
|
389
|
+
metadata: {
|
|
390
|
+
hasPreviousConfig: params.previousConfig !== null,
|
|
391
|
+
newConfigKeyCount: getObjectKeyCount(newConfig)
|
|
392
|
+
},
|
|
393
|
+
onSuccess: (result) => ({
|
|
394
|
+
returnedConfig: !!result?.config
|
|
395
|
+
})
|
|
396
|
+
},
|
|
397
|
+
() => (0, import_provider2.runWithContext)(
|
|
398
|
+
context,
|
|
399
|
+
async () => configChanged({
|
|
400
|
+
previousConfig: params.previousConfig,
|
|
401
|
+
newConfig
|
|
402
|
+
})
|
|
403
|
+
)
|
|
404
|
+
);
|
|
405
|
+
return withRequestTraces(context, {
|
|
406
|
+
success: true,
|
|
407
|
+
config: updatedConfig?.config ?? newConfig
|
|
408
|
+
});
|
|
409
|
+
});
|
|
410
|
+
manager.onRequest("slates/config.get_default", async () => {
|
|
411
|
+
getContextBasic();
|
|
412
|
+
let getDefaultConfig = slate.spec.config.handlers.getDefaultConfig;
|
|
413
|
+
if (!getDefaultConfig) {
|
|
414
|
+
return { config: null };
|
|
415
|
+
}
|
|
416
|
+
let context = getEmptyContext();
|
|
417
|
+
let defaultConfig = await traceProviderCall(
|
|
418
|
+
{
|
|
419
|
+
component: "config",
|
|
420
|
+
functionName: "getDefaultConfig",
|
|
421
|
+
message: "Getting default config",
|
|
422
|
+
successMessage: "Default config retrieved",
|
|
423
|
+
onSuccess: (result) => ({
|
|
424
|
+
configKeyCount: getObjectKeyCount(result)
|
|
425
|
+
})
|
|
426
|
+
},
|
|
427
|
+
() => (0, import_provider2.runWithContext)(context, async () => getDefaultConfig())
|
|
428
|
+
);
|
|
429
|
+
return withRequestTraces(context, {
|
|
430
|
+
config: defaultConfig ?? null
|
|
431
|
+
});
|
|
432
|
+
});
|
|
433
|
+
manager.onRequest("slates/config.schema.get", async () => {
|
|
434
|
+
getContextBasic();
|
|
435
|
+
return { schema: toJsonSchema(slate.spec.configSchema) };
|
|
436
|
+
});
|
|
437
|
+
manager.onRequest("slates/provider.identify", async () => {
|
|
438
|
+
getContextBasic();
|
|
439
|
+
return {
|
|
440
|
+
protocol: import_proto.SLATES_PROTOCOL_VERSION,
|
|
441
|
+
provider: {
|
|
442
|
+
type: "provider",
|
|
443
|
+
id: slate.spec.key,
|
|
444
|
+
name: slate.spec.name,
|
|
445
|
+
description: slate.spec.description,
|
|
446
|
+
metadata: slate.spec.parameters.metadata
|
|
447
|
+
}
|
|
448
|
+
};
|
|
449
|
+
});
|
|
450
|
+
manager.onRequest("slates/auth.methods.list", async () => {
|
|
451
|
+
getContextBasic();
|
|
452
|
+
return {
|
|
453
|
+
authenticationMethods: slate.spec.auth.authStack.map((m) => mapAuthMethod(slate, m))
|
|
454
|
+
};
|
|
455
|
+
});
|
|
456
|
+
manager.onRequest("slates/auth.method.get", async ({ params }) => {
|
|
457
|
+
getContextBasic();
|
|
458
|
+
let authMethod = getAuthMethod(slate, params.authenticationMethodId);
|
|
459
|
+
return {
|
|
460
|
+
authenticationMethod: mapAuthMethod(slate, authMethod)
|
|
461
|
+
};
|
|
462
|
+
});
|
|
463
|
+
manager.onRequest("slates/auth.input.get_default", async ({ params }) => {
|
|
464
|
+
getContextBasic();
|
|
465
|
+
let authMethod = getAuthMethod(slate, params.authenticationMethodId);
|
|
466
|
+
if (!authMethod.getDefaultInput) {
|
|
467
|
+
return { input: null };
|
|
468
|
+
}
|
|
469
|
+
let context = getEmptyContext();
|
|
470
|
+
let input = await traceProviderCall(
|
|
471
|
+
{
|
|
472
|
+
component: "auth",
|
|
473
|
+
functionName: "getDefaultInput",
|
|
474
|
+
message: "Getting default authentication input",
|
|
475
|
+
successMessage: "Default authentication input retrieved",
|
|
476
|
+
metadata: {
|
|
477
|
+
authenticationMethodId: params.authenticationMethodId,
|
|
478
|
+
authenticationMethodName: authMethod.name
|
|
479
|
+
},
|
|
480
|
+
onSuccess: (result) => ({
|
|
481
|
+
inputKeyCount: getObjectKeyCount(result)
|
|
482
|
+
})
|
|
483
|
+
},
|
|
484
|
+
() => (0, import_provider2.runWithContext)(context, () => authMethod.getDefaultInput())
|
|
485
|
+
);
|
|
486
|
+
return withRequestTraces(context, { input });
|
|
487
|
+
});
|
|
488
|
+
manager.onRequest("slates/auth.input.changed", async ({ params }) => {
|
|
489
|
+
getContextBasic();
|
|
490
|
+
let authMethod = getAuthMethod(slate, params.authenticationMethodId);
|
|
491
|
+
if (!authMethod.onInputChanged) {
|
|
492
|
+
return { success: true, input: params.newInput };
|
|
493
|
+
}
|
|
494
|
+
let context = getEmptyContext();
|
|
495
|
+
let updatedInput = await traceProviderCall(
|
|
496
|
+
{
|
|
497
|
+
component: "auth",
|
|
498
|
+
functionName: "onInputChanged",
|
|
499
|
+
message: "Running authentication input change handler",
|
|
500
|
+
successMessage: "Authentication input change handler completed",
|
|
501
|
+
metadata: {
|
|
502
|
+
authenticationMethodId: params.authenticationMethodId,
|
|
503
|
+
authenticationMethodName: authMethod.name,
|
|
504
|
+
hasPreviousInput: params.previousInput !== null,
|
|
505
|
+
newInputKeyCount: getObjectKeyCount(params.newInput)
|
|
506
|
+
},
|
|
507
|
+
onSuccess: (result) => ({
|
|
508
|
+
returnedInput: !!result?.input
|
|
509
|
+
})
|
|
510
|
+
},
|
|
511
|
+
() => (0, import_provider2.runWithContext)(
|
|
512
|
+
context,
|
|
513
|
+
() => authMethod.onInputChanged({
|
|
514
|
+
previousInput: params.previousInput,
|
|
515
|
+
newInput: params.newInput
|
|
516
|
+
})
|
|
517
|
+
)
|
|
518
|
+
);
|
|
519
|
+
return withRequestTraces(context, {
|
|
520
|
+
success: true,
|
|
521
|
+
input: updatedInput?.input ?? params.newInput
|
|
522
|
+
});
|
|
523
|
+
});
|
|
524
|
+
manager.onRequest("slates/auth.output.get", async ({ params }) => {
|
|
525
|
+
getContextBasic();
|
|
526
|
+
let authMethod = getAuthMethod(slate, params.authenticationMethodId);
|
|
527
|
+
let input = params.input;
|
|
528
|
+
if (authMethod.inputSchema) {
|
|
529
|
+
input = validate(
|
|
530
|
+
authMethod.inputSchema,
|
|
531
|
+
input,
|
|
532
|
+
"auth",
|
|
533
|
+
`Invalid authentication input for method ID: ${params.authenticationMethodId}`
|
|
534
|
+
);
|
|
535
|
+
}
|
|
536
|
+
if ("getOutput" in authMethod) {
|
|
537
|
+
let context = getEmptyContext();
|
|
538
|
+
let outputRes = await traceProviderCall(
|
|
539
|
+
{
|
|
540
|
+
component: "auth",
|
|
541
|
+
functionName: "getOutput",
|
|
542
|
+
message: "Getting authentication output",
|
|
543
|
+
successMessage: "Authentication output retrieved",
|
|
544
|
+
metadata: {
|
|
545
|
+
authenticationMethodId: params.authenticationMethodId,
|
|
546
|
+
authenticationMethodName: authMethod.name,
|
|
547
|
+
inputKeyCount: getObjectKeyCount(input)
|
|
548
|
+
},
|
|
549
|
+
onSuccess: (result) => ({
|
|
550
|
+
outputKeyCount: getObjectKeyCount(result.output)
|
|
551
|
+
})
|
|
552
|
+
},
|
|
553
|
+
() => (0, import_provider2.runWithContext)(context, () => authMethod.getOutput({ input }))
|
|
554
|
+
);
|
|
555
|
+
return withRequestTraces(context, { output: outputRes.output });
|
|
556
|
+
}
|
|
557
|
+
return { output: input };
|
|
558
|
+
});
|
|
559
|
+
manager.onRequest("slates/auth.authorization_callback.handle", async ({ params }) => {
|
|
560
|
+
getContextBasic();
|
|
561
|
+
let authMethod = getAuthMethod(slate, params.authenticationMethodId);
|
|
562
|
+
if ("handleCallback" in authMethod) {
|
|
563
|
+
let context = getEmptyContext();
|
|
564
|
+
let callbackRes = await traceProviderCall(
|
|
565
|
+
{
|
|
566
|
+
component: "auth",
|
|
567
|
+
functionName: "handleCallback",
|
|
568
|
+
message: "Handling authentication callback",
|
|
569
|
+
successMessage: "Authentication callback handled",
|
|
570
|
+
metadata: {
|
|
571
|
+
authenticationMethodId: params.authenticationMethodId,
|
|
572
|
+
authenticationMethodName: authMethod.name,
|
|
573
|
+
scopeCount: params.scopes.length,
|
|
574
|
+
hasCallbackState: !!params.callbackState
|
|
575
|
+
},
|
|
576
|
+
onSuccess: (result) => ({
|
|
577
|
+
outputKeyCount: getObjectKeyCount(result.output),
|
|
578
|
+
returnedInput: !!result.input,
|
|
579
|
+
returnedScopeCount: result.scopes?.length
|
|
580
|
+
})
|
|
581
|
+
},
|
|
582
|
+
() => (0, import_provider2.runWithContext)(
|
|
583
|
+
context,
|
|
584
|
+
() => authMethod.handleCallback({
|
|
585
|
+
code: params.code,
|
|
586
|
+
state: params.state,
|
|
587
|
+
redirectUri: params.redirectUri,
|
|
588
|
+
input: params.input,
|
|
589
|
+
clientId: params.clientId,
|
|
590
|
+
clientSecret: params.clientSecret,
|
|
591
|
+
scopes: params.scopes,
|
|
592
|
+
callbackState: params.callbackState || {}
|
|
593
|
+
})
|
|
594
|
+
)
|
|
595
|
+
);
|
|
596
|
+
return withRequestTraces(context, {
|
|
597
|
+
output: callbackRes.output,
|
|
598
|
+
input: callbackRes.input,
|
|
599
|
+
scopes: callbackRes.scopes
|
|
600
|
+
});
|
|
601
|
+
}
|
|
602
|
+
throw new import_error3.ServiceError(
|
|
603
|
+
(0, import_error3.preconditionFailedError)({
|
|
604
|
+
message: `Authentication method does not support authorization callback handling: ${params.authenticationMethodId}`
|
|
605
|
+
})
|
|
606
|
+
);
|
|
607
|
+
});
|
|
608
|
+
manager.onRequest("slates/auth.authorization_url.get", async ({ params }) => {
|
|
609
|
+
getContextBasic();
|
|
610
|
+
let authMethod = getAuthMethod(slate, params.authenticationMethodId);
|
|
611
|
+
if ("getAuthorizationUrl" in authMethod) {
|
|
612
|
+
let context = getEmptyContext();
|
|
613
|
+
let urlRes = await traceProviderCall(
|
|
614
|
+
{
|
|
615
|
+
component: "auth",
|
|
616
|
+
functionName: "getAuthorizationUrl",
|
|
617
|
+
message: "Getting authentication authorization URL",
|
|
618
|
+
successMessage: "Authentication authorization URL retrieved",
|
|
619
|
+
metadata: {
|
|
620
|
+
authenticationMethodId: params.authenticationMethodId,
|
|
621
|
+
authenticationMethodName: authMethod.name,
|
|
622
|
+
scopeCount: params.scopes.length,
|
|
623
|
+
inputKeyCount: getObjectKeyCount(params.input)
|
|
624
|
+
},
|
|
625
|
+
onSuccess: (result) => ({
|
|
626
|
+
returnedInput: !!result.input,
|
|
627
|
+
hasCallbackState: !!result.callbackState
|
|
628
|
+
})
|
|
629
|
+
},
|
|
630
|
+
() => (0, import_provider2.runWithContext)(
|
|
631
|
+
context,
|
|
632
|
+
() => authMethod.getAuthorizationUrl({
|
|
633
|
+
redirectUri: params.redirectUri,
|
|
634
|
+
state: params.state,
|
|
635
|
+
input: params.input,
|
|
636
|
+
clientId: params.clientId,
|
|
637
|
+
clientSecret: params.clientSecret,
|
|
638
|
+
scopes: params.scopes
|
|
639
|
+
})
|
|
640
|
+
)
|
|
641
|
+
);
|
|
642
|
+
return withRequestTraces(context, {
|
|
643
|
+
authorizationUrl: urlRes.url,
|
|
644
|
+
input: urlRes.input,
|
|
645
|
+
callbackState: urlRes.callbackState
|
|
646
|
+
});
|
|
647
|
+
}
|
|
648
|
+
throw new import_error3.ServiceError(
|
|
649
|
+
(0, import_error3.preconditionFailedError)({
|
|
650
|
+
message: `Authentication method does not support authorization URL retrieval: ${params.authenticationMethodId}`
|
|
651
|
+
})
|
|
652
|
+
);
|
|
653
|
+
});
|
|
654
|
+
manager.onRequest("slates/auth.profile.get", async ({ params }) => {
|
|
655
|
+
getContextBasic();
|
|
656
|
+
let authMethod = getAuthMethod(slate, params.authenticationMethodId);
|
|
657
|
+
if (authMethod.getProfile) {
|
|
658
|
+
let context = getEmptyContext();
|
|
659
|
+
let profileRes = await traceProviderCall(
|
|
660
|
+
{
|
|
661
|
+
component: "auth",
|
|
662
|
+
functionName: "getProfile",
|
|
663
|
+
message: "Getting authentication profile",
|
|
664
|
+
successMessage: "Authentication profile retrieved",
|
|
665
|
+
metadata: {
|
|
666
|
+
authenticationMethodId: params.authenticationMethodId,
|
|
667
|
+
authenticationMethodName: authMethod.name,
|
|
668
|
+
scopeCount: params.scopes.length,
|
|
669
|
+
inputKeyCount: getObjectKeyCount(params.input),
|
|
670
|
+
outputKeyCount: getObjectKeyCount(params.output)
|
|
671
|
+
},
|
|
672
|
+
onSuccess: (result) => ({
|
|
673
|
+
profileKeyCount: getObjectKeyCount(result.profile)
|
|
674
|
+
})
|
|
675
|
+
},
|
|
676
|
+
() => (0, import_provider2.runWithContext)(
|
|
677
|
+
context,
|
|
678
|
+
() => authMethod.getProfile({
|
|
679
|
+
output: params.output,
|
|
680
|
+
input: params.input,
|
|
681
|
+
scopes: params.scopes
|
|
682
|
+
})
|
|
683
|
+
)
|
|
684
|
+
);
|
|
685
|
+
return withRequestTraces(context, {
|
|
686
|
+
profile: profileRes.profile
|
|
687
|
+
});
|
|
688
|
+
}
|
|
689
|
+
throw new import_error3.ServiceError(
|
|
690
|
+
(0, import_error3.preconditionFailedError)({
|
|
691
|
+
message: `Authentication method does not support profile retrieval: ${params.authenticationMethodId}`
|
|
692
|
+
})
|
|
693
|
+
);
|
|
694
|
+
});
|
|
695
|
+
manager.onRequest("slates/auth.token_refresh.handle", async ({ params }) => {
|
|
696
|
+
getContextBasic();
|
|
697
|
+
let authMethod = getAuthMethod(slate, params.authenticationMethodId);
|
|
698
|
+
if ("handleTokenRefresh" in authMethod && authMethod.handleTokenRefresh) {
|
|
699
|
+
let context = getEmptyContext();
|
|
700
|
+
let refreshRes = await traceProviderCall(
|
|
701
|
+
{
|
|
702
|
+
component: "auth",
|
|
703
|
+
functionName: "handleTokenRefresh",
|
|
704
|
+
message: "Refreshing authentication token",
|
|
705
|
+
successMessage: "Authentication token refreshed",
|
|
706
|
+
metadata: {
|
|
707
|
+
authenticationMethodId: params.authenticationMethodId,
|
|
708
|
+
authenticationMethodName: authMethod.name,
|
|
709
|
+
scopeCount: params.scopes.length,
|
|
710
|
+
inputKeyCount: getObjectKeyCount(params.input),
|
|
711
|
+
outputKeyCount: getObjectKeyCount(params.output)
|
|
712
|
+
},
|
|
713
|
+
onSuccess: (result) => ({
|
|
714
|
+
refreshedOutputKeyCount: getObjectKeyCount(result.output),
|
|
715
|
+
returnedInput: !!result.input
|
|
716
|
+
})
|
|
717
|
+
},
|
|
718
|
+
() => (0, import_provider2.runWithContext)(
|
|
719
|
+
context,
|
|
720
|
+
() => authMethod.handleTokenRefresh({
|
|
721
|
+
output: params.output,
|
|
722
|
+
input: params.input,
|
|
723
|
+
clientId: params.clientId,
|
|
724
|
+
clientSecret: params.clientSecret,
|
|
725
|
+
scopes: params.scopes
|
|
726
|
+
})
|
|
727
|
+
)
|
|
728
|
+
);
|
|
729
|
+
return withRequestTraces(context, {
|
|
730
|
+
output: refreshRes.output,
|
|
731
|
+
input: refreshRes.input
|
|
732
|
+
});
|
|
733
|
+
}
|
|
734
|
+
throw new import_error3.ServiceError(
|
|
735
|
+
(0, import_error3.preconditionFailedError)({
|
|
736
|
+
message: `Authentication method does not support token refresh handling: ${params.authenticationMethodId}`
|
|
737
|
+
})
|
|
738
|
+
);
|
|
739
|
+
});
|
|
740
|
+
manager.onRequest("slates/actions.list", async () => {
|
|
741
|
+
getContextBasic();
|
|
742
|
+
return {
|
|
743
|
+
actions: slate.actions.map((a) => mapAction(slate, a))
|
|
744
|
+
};
|
|
745
|
+
});
|
|
746
|
+
manager.onRequest("slates/action.get", async ({ params }) => {
|
|
747
|
+
getContextBasic();
|
|
748
|
+
let action = getAction(slate, params.actionId);
|
|
749
|
+
return {
|
|
750
|
+
action: mapAction(slate, action)
|
|
751
|
+
};
|
|
752
|
+
});
|
|
753
|
+
manager.onRequest("slates/action.tool.invoke", async ({ params }) => {
|
|
754
|
+
let ctx = getContextFull();
|
|
755
|
+
let action = getActionWithType(slate, "tool", params.actionId);
|
|
756
|
+
let input = validate(
|
|
757
|
+
action.inputSchema,
|
|
758
|
+
params.input,
|
|
759
|
+
"input",
|
|
760
|
+
`Invalid input for tool ID: ${params.actionId}`
|
|
761
|
+
);
|
|
762
|
+
let context = new import_provider2.SlateContext(ctx.config, input, ctx.auth?.output, slate.spec, logger);
|
|
763
|
+
let res = await traceProviderCall(
|
|
764
|
+
{
|
|
765
|
+
component: "action",
|
|
766
|
+
functionName: "handleInvocation",
|
|
767
|
+
message: `Starting tool ${formatEntityLabel(action.name, action.key)}`,
|
|
768
|
+
successMessage: `Completed tool ${formatEntityLabel(action.name, action.key)}`,
|
|
769
|
+
errorMessage: `Tool ${formatEntityLabel(action.name, action.key)} failed`,
|
|
770
|
+
metadata: {
|
|
771
|
+
actionId: action.key,
|
|
772
|
+
actionName: action.name,
|
|
773
|
+
actionType: action.type,
|
|
774
|
+
inputKeyCount: getObjectKeyCount(input)
|
|
775
|
+
},
|
|
776
|
+
onSuccess: (result) => ({
|
|
777
|
+
hasMessage: !!result.message,
|
|
778
|
+
actionResultMessage: result.message,
|
|
779
|
+
outputKeyCount: getObjectKeyCount(result.output),
|
|
780
|
+
attachmentCount: result.attachments?.length
|
|
781
|
+
})
|
|
782
|
+
},
|
|
783
|
+
() => (0, import_provider2.runWithContext)(context, () => action.handleInvocation(context))
|
|
784
|
+
);
|
|
785
|
+
return withRequestTraces(context, {
|
|
786
|
+
output: res.output,
|
|
787
|
+
message: res.message,
|
|
788
|
+
attachments: mergeAttachments(res.attachments, res.output)
|
|
789
|
+
});
|
|
790
|
+
});
|
|
791
|
+
manager.onRequest("slates/action.trigger.map_event", async ({ params }) => {
|
|
792
|
+
let ctx = getContextFull();
|
|
793
|
+
let action = getActionWithType(slate, "trigger", params.actionId);
|
|
794
|
+
let input = validate(
|
|
795
|
+
action.inputSchema,
|
|
796
|
+
params.input,
|
|
797
|
+
"input",
|
|
798
|
+
`Invalid event for trigger ID: ${params.actionId}`
|
|
799
|
+
);
|
|
800
|
+
let context = new import_provider2.SlateContext(ctx.config, input, ctx.auth?.output, slate.spec, logger);
|
|
801
|
+
let res = await traceProviderCall(
|
|
802
|
+
{
|
|
803
|
+
component: "action",
|
|
804
|
+
functionName: "handleEvent",
|
|
805
|
+
message: `Mapping event for trigger ${formatEntityLabel(action.name, action.key)}`,
|
|
806
|
+
successMessage: (result) => `Mapped trigger event "${result.type}" for ${formatEntityLabel(action.name, action.key)}`,
|
|
807
|
+
errorMessage: `Trigger ${formatEntityLabel(action.name, action.key)} failed while mapping an event`,
|
|
808
|
+
metadata: {
|
|
809
|
+
actionId: action.key,
|
|
810
|
+
actionName: action.name,
|
|
811
|
+
actionType: action.type,
|
|
812
|
+
inputKeyCount: getObjectKeyCount(input)
|
|
813
|
+
},
|
|
814
|
+
onSuccess: (result) => ({
|
|
815
|
+
eventType: result.type,
|
|
816
|
+
hasEventId: !!result.id,
|
|
817
|
+
outputKeyCount: getObjectKeyCount(result.output)
|
|
818
|
+
})
|
|
819
|
+
},
|
|
820
|
+
() => (0, import_provider2.runWithContext)(context, () => action.handleEvent(context))
|
|
821
|
+
);
|
|
822
|
+
return withRequestTraces(context, { id: res.id, type: res.type, output: res.output });
|
|
823
|
+
});
|
|
824
|
+
manager.onRequest("slates/action.trigger.poll_events", async ({ params }) => {
|
|
825
|
+
let ctx = getContextFull();
|
|
826
|
+
let action = getActionWithType(slate, "trigger", params.actionId);
|
|
827
|
+
if (!action.pollEvents) {
|
|
828
|
+
throw new import_error3.ServiceError(
|
|
829
|
+
(0, import_error3.badRequestError)({
|
|
830
|
+
message: `Trigger action does not support polling: ${params.actionId}`
|
|
831
|
+
})
|
|
832
|
+
);
|
|
833
|
+
}
|
|
834
|
+
let context = new import_provider2.SlateContext(
|
|
835
|
+
ctx.config,
|
|
836
|
+
{ state: params.state },
|
|
837
|
+
ctx.auth?.output,
|
|
838
|
+
slate.spec,
|
|
839
|
+
logger
|
|
840
|
+
);
|
|
841
|
+
let res = await traceProviderCall(
|
|
842
|
+
{
|
|
843
|
+
component: "action",
|
|
844
|
+
functionName: "pollEvents",
|
|
845
|
+
message: `Polling events for trigger ${formatEntityLabel(action.name, action.key)}`,
|
|
846
|
+
successMessage: (result) => `Polled ${result.inputs.length} event(s) for trigger ${formatEntityLabel(action.name, action.key)}`,
|
|
847
|
+
errorMessage: `Trigger ${formatEntityLabel(action.name, action.key)} failed while polling events`,
|
|
848
|
+
metadata: {
|
|
849
|
+
actionId: action.key,
|
|
850
|
+
actionName: action.name,
|
|
851
|
+
actionType: action.type,
|
|
852
|
+
hasPreviousState: params.state !== null
|
|
853
|
+
},
|
|
854
|
+
onSuccess: (result) => ({
|
|
855
|
+
inputCount: result.inputs.length,
|
|
856
|
+
hasUpdatedState: result.updatedState !== void 0
|
|
857
|
+
})
|
|
858
|
+
},
|
|
859
|
+
() => (0, import_provider2.runWithContext)(context, () => action.pollEvents(context))
|
|
860
|
+
);
|
|
861
|
+
return withRequestTraces(context, {
|
|
862
|
+
inputs: res.inputs,
|
|
863
|
+
updatedState: res.updatedState
|
|
864
|
+
});
|
|
865
|
+
});
|
|
866
|
+
manager.onRequest("slates/action.trigger.webhook_handle", async ({ params }) => {
|
|
867
|
+
let ctx = getContextFull();
|
|
868
|
+
let action = getActionWithType(slate, "trigger", params.actionId);
|
|
869
|
+
if (!action.handleRequest) {
|
|
870
|
+
throw new import_error3.ServiceError(
|
|
871
|
+
(0, import_error3.badRequestError)({
|
|
872
|
+
message: `Trigger action does not support webhook requests: ${params.actionId}`
|
|
873
|
+
})
|
|
874
|
+
);
|
|
875
|
+
}
|
|
876
|
+
let req = new Request(params.url, {
|
|
877
|
+
method: params.method,
|
|
878
|
+
headers: params.headers,
|
|
879
|
+
body: params.body ? Uint8Array.from(atob(params.body.content), (c) => c.charCodeAt(0)) : null
|
|
880
|
+
});
|
|
881
|
+
let context = new import_provider2.SlateContext(
|
|
882
|
+
ctx.config,
|
|
883
|
+
{ request: req, state: params.state },
|
|
884
|
+
ctx.auth?.output,
|
|
885
|
+
slate.spec,
|
|
886
|
+
logger
|
|
887
|
+
);
|
|
888
|
+
let res = await traceProviderCall(
|
|
889
|
+
{
|
|
890
|
+
component: "action",
|
|
891
|
+
functionName: "handleRequest",
|
|
892
|
+
message: `Handling webhook request for trigger ${formatEntityLabel(action.name, action.key)}`,
|
|
893
|
+
successMessage: (result) => `Received ${result.inputs.length} webhook event(s) for trigger ${formatEntityLabel(action.name, action.key)}`,
|
|
894
|
+
errorMessage: `Trigger ${formatEntityLabel(action.name, action.key)} failed while handling a webhook request`,
|
|
895
|
+
metadata: {
|
|
896
|
+
actionId: action.key,
|
|
897
|
+
actionName: action.name,
|
|
898
|
+
actionType: action.type,
|
|
899
|
+
requestMethod: params.method,
|
|
900
|
+
hasRequestBody: !!params.body,
|
|
901
|
+
hasPreviousState: params.state !== null
|
|
902
|
+
},
|
|
903
|
+
onSuccess: (result) => ({
|
|
904
|
+
inputCount: result.inputs.length,
|
|
905
|
+
hasUpdatedState: result.updatedState !== void 0
|
|
906
|
+
})
|
|
907
|
+
},
|
|
908
|
+
() => (0, import_provider2.runWithContext)(context, () => action.handleRequest(context))
|
|
909
|
+
);
|
|
910
|
+
return withRequestTraces(context, {
|
|
911
|
+
inputs: res.inputs,
|
|
912
|
+
updatedState: res.updatedState
|
|
913
|
+
});
|
|
914
|
+
});
|
|
915
|
+
manager.onRequest("slates/action.trigger.webhook_register", async ({ params }) => {
|
|
916
|
+
let ctx = getContextFull();
|
|
917
|
+
let action = getActionWithType(slate, "trigger", params.actionId);
|
|
918
|
+
if (!action.autoRegisterWebhook) {
|
|
919
|
+
throw new import_error3.ServiceError(
|
|
920
|
+
(0, import_error3.badRequestError)({
|
|
921
|
+
message: `Trigger action does not support webhook auto-registration: ${params.actionId}`
|
|
922
|
+
})
|
|
923
|
+
);
|
|
924
|
+
}
|
|
925
|
+
let context = new import_provider2.SlateContext(
|
|
926
|
+
ctx.config,
|
|
927
|
+
{ webhookBaseUrl: params.webhookBaseUrl },
|
|
928
|
+
ctx.auth?.output,
|
|
929
|
+
slate.spec,
|
|
930
|
+
logger
|
|
931
|
+
);
|
|
932
|
+
let res = await traceProviderCall(
|
|
933
|
+
{
|
|
934
|
+
component: "action",
|
|
935
|
+
functionName: "autoRegisterWebhook",
|
|
936
|
+
message: `Registering webhook for trigger ${formatEntityLabel(action.name, action.key)}`,
|
|
937
|
+
successMessage: `Registered webhook for trigger ${formatEntityLabel(action.name, action.key)}`,
|
|
938
|
+
errorMessage: `Trigger ${formatEntityLabel(action.name, action.key)} failed while registering a webhook`,
|
|
939
|
+
metadata: {
|
|
940
|
+
actionId: action.key,
|
|
941
|
+
actionName: action.name,
|
|
942
|
+
actionType: action.type
|
|
943
|
+
},
|
|
944
|
+
onSuccess: (result) => ({
|
|
945
|
+
hasRegistrationDetails: result.registrationDetails !== void 0,
|
|
946
|
+
hasState: result.state !== void 0
|
|
947
|
+
})
|
|
948
|
+
},
|
|
949
|
+
() => (0, import_provider2.runWithContext)(context, () => action.autoRegisterWebhook(context))
|
|
950
|
+
);
|
|
951
|
+
return withRequestTraces(context, {
|
|
952
|
+
registrationDetails: res.registrationDetails,
|
|
953
|
+
state: res.state
|
|
954
|
+
});
|
|
955
|
+
});
|
|
956
|
+
manager.onRequest("slates/action.trigger.webhook_unregister", async ({ params }) => {
|
|
957
|
+
let ctx = getContextFull();
|
|
958
|
+
let action = getActionWithType(slate, "trigger", params.actionId);
|
|
959
|
+
if (!action.autoUnregisterWebhook) {
|
|
960
|
+
throw new import_error3.ServiceError(
|
|
961
|
+
(0, import_error3.badRequestError)({
|
|
962
|
+
message: `Trigger action does not support webhook auto-unregistration: ${params.actionId}`
|
|
963
|
+
})
|
|
964
|
+
);
|
|
965
|
+
}
|
|
966
|
+
let context = new import_provider2.SlateContext(
|
|
967
|
+
ctx.config,
|
|
968
|
+
{
|
|
969
|
+
webhookBaseUrl: params.webhookBaseUrl,
|
|
970
|
+
registrationDetails: params.registrationDetails,
|
|
971
|
+
state: params.state
|
|
972
|
+
},
|
|
973
|
+
ctx.auth?.output,
|
|
974
|
+
slate.spec,
|
|
975
|
+
logger
|
|
976
|
+
);
|
|
977
|
+
await traceProviderCall(
|
|
978
|
+
{
|
|
979
|
+
component: "action",
|
|
980
|
+
functionName: "autoUnregisterWebhook",
|
|
981
|
+
message: `Unregistering webhook for trigger ${formatEntityLabel(action.name, action.key)}`,
|
|
982
|
+
successMessage: `Unregistered webhook for trigger ${formatEntityLabel(action.name, action.key)}`,
|
|
983
|
+
errorMessage: `Trigger ${formatEntityLabel(action.name, action.key)} failed while unregistering a webhook`,
|
|
984
|
+
metadata: {
|
|
985
|
+
actionId: action.key,
|
|
986
|
+
actionName: action.name,
|
|
987
|
+
actionType: action.type,
|
|
988
|
+
hasRegistrationDetails: params.registrationDetails !== null,
|
|
989
|
+
hasPreviousState: params.state !== null
|
|
990
|
+
}
|
|
991
|
+
},
|
|
992
|
+
() => (0, import_provider2.runWithContext)(context, () => action.autoUnregisterWebhook(context))
|
|
993
|
+
);
|
|
994
|
+
return withRequestTraces(context, {});
|
|
995
|
+
});
|
|
996
|
+
});
|
|
997
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
998
|
+
0 && (module.exports = {
|
|
999
|
+
createProviderHandler
|
|
1000
|
+
});
|