@slates/provider-handler 1.0.0-rc.3 → 1.0.0-rc.32
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 +1475 -2
- package/dist/index.d.cts +8 -0
- package/dist/index.d.ts +6 -3
- package/dist/index.module.js +1452 -2
- package/package.json +11 -10
- package/src/attachments.test.ts +171 -0
- package/src/index.ts +1060 -137
- package/src/pQueue.test.ts +18 -0
- package/src/pQueue.ts +18 -0
- package/src/spec.test.ts +83 -0
- package/src/spec.ts +150 -21
- package/src/validation.ts +1 -1
- package/src/webhook.test.ts +65 -0
- package/src/webhook.ts +69 -0
- 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.module.js
CHANGED
|
@@ -1,2 +1,1452 @@
|
|
|
1
|
-
import{ServiceError as t,validationError as e,badRequestError as n,notFoundError as r,preconditionFailedError as o}from"@lowerdeck/error";import{createSlatesProviderProtoHandler as i}from"@slates/proto";import{SlateDefaultPollingIntervalSeconds as a,SlateLogger as u,SlateContext as s}from"@slates/provider";import c from"zod";function l(t,e){if(!{}.hasOwnProperty.call(t,e))throw new TypeError("attempted to use private field on non-instance");return t}var h=0;function p(t){return"__private_"+h+++"_"+t}function f(){return f=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var r in n)({}).hasOwnProperty.call(n,r)&&(t[r]=n[r])}return t},f.apply(null,arguments)}function d(t){var e=function(t){if("object"!=typeof t||!t)return t;var e=t[Symbol.toPrimitive];if(void 0!==e){var n=e.call(t,"string");if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t);return"symbol"==typeof e?e:e+""}var m=function(n,r,o,i){var a=n.safeParse(r);if(!a.success)throw new t(function(t,n,r){return e({message:n,entity:t,errors:r.issues.map(function(t){return f({},t,{path:t.path.map(function(t){return String(t)})})})})}(o,i,a.error));return a.data},v=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")}})},g=function(e,r){var o=e.spec.auth.authStack.find(function(t){return t.key==r});if(!o)throw new t(n({message:"Invalid authentication method ID: "+r}));return o},P=function(t,e){var n;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:v(null!=(n=e.inputSchema)?n:c.object({})),outputSchema:v(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}}}},y=function(e,n){var o=e.actions.find(function(t){return t.key==n});if(!o)throw new t(r("action",n));return o},w=function(e,r,o){var i=y(e,o);if(i.type!=r)throw new t(n({message:"Action with ID "+o+" is not of type "+r}));return i},I=function(t,e){var n;return f({},{id:e.key,name:e.name,description:e.description,instructions:e.instructions,constraints:e.constraints,tags:e.tags,metadata:e.metadata,inputSchema:v(e.inputSchema),outputSchema:v(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:a}:{type:"webhook",autoRegistration:!!e.autoRegisterWebhook,autoUnregistration:!!e.autoUnregisterWebhook}})},b=/*#__PURE__*/p("value"),k=/*#__PURE__*/function(){function t(t){Object.defineProperty(this,b,{writable:!0,value:void 0}),l(this,b)[b]=t}var e,n,r=t.prototype;return r.get=function(){return l(this,b)[b]},r.set=function(t){l(this,b)[b]=t},e=t,(n=[{key:"value",get:function(){return l(this,b)[b]}}])&&function(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,d(r.key),r)}}(e.prototype,n),Object.defineProperty(e,"prototype",{writable:!1}),e}(),S=function(e,r){return i(function(i){try{var a=new k(null),c=new k(null),l=new k(null),h=new k(null),p=new k(null),d=new u(r),b=function(){var e=a.get(),n=c.get();if(!e||!n)throw new t(o({message:"Connection context has not been initialized"}));return{protocol:e,participants:n}},S=function(){var n=b(),r=h.get(),i=p.get(),a=l.get();if(!r||!i||!a&&e.spec.auth.authStack.length>0)throw new t(o({message:"Session context has not been initialized"}));return f({},n,{config:r.value,session:i,auth:a})};return i.onNotification("slates/hello",function(t){var e=t.params;try{return a.set(e.protocol),Promise.resolve()}catch(t){return Promise.reject(t)}}),i.onNotification("slates/participant.set",function(e){var n=e.params;try{if(!a.get())throw new t(o({message:"Connection protocol has not been initialized"}));return c.set(n.participants),Promise.resolve()}catch(t){return Promise.reject(t)}}),i.onNotification("slates/auth.set",function(t){var n=t.params;try{b(),g(e,n.authenticationMethodId);var r=m(e.spec.authSchema,n.output,"auth","Invalid authentication output for method ID: "+n.authenticationMethodId);return l.set({authenticationMethodId:n.authenticationMethodId,output:r}),Promise.resolve()}catch(t){return Promise.reject(t)}}),i.onNotification("slates/config.set",function(t){var n=t.params;try{b();var r=m(e.spec.configSchema,n.config,"config","Invalid configuration");return h.set({value:r}),Promise.resolve()}catch(t){return Promise.reject(t)}}),i.onNotification("slates/session.start",function(t){var e=t.params;try{return b(),p.set({id:e.sessionId,state:e.state}),Promise.resolve()}catch(t){return Promise.reject(t)}}),i.onRequest("slates/config.changed",function(t){var n=t.params;try{b();var r=m(e.spec.config.configSchema,n.newConfig,"config","Invalid configuration"),o=e.spec.config.handlers.configChanged;return o?Promise.resolve(o({previousConfig:n.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)}}),i.onRequest("slates/config.get_default",function(t){try{b();var n=e.spec.config.handlers.getDefaultConfig;return n?Promise.resolve(n()).then(function(t){return{config:t}}):Promise.resolve({config:null})}catch(t){return Promise.reject(t)}}),i.onRequest("slates/config.schema.get",function(t){try{return b(),Promise.resolve({schema:v(e.spec.configSchema)})}catch(t){return Promise.reject(t)}}),i.onRequest("slates/provider.identify",function(t){try{return b(),Promise.resolve({protocol:"slates@2026-01-01",provider:{type:"provider",id:e.spec.key,name:e.spec.name,description:e.spec.description,metadata:e.spec.parameters.metadata}})}catch(t){return Promise.reject(t)}}),i.onRequest("slates/auth.methods.list",function(t){try{return b(),Promise.resolve({authenticationMethods:e.spec.auth.authStack.map(function(t){return P(e,t)})})}catch(t){return Promise.reject(t)}}),i.onRequest("slates/auth.method.get",function(t){var n=t.params;try{b();var r=g(e,n.authenticationMethodId);return Promise.resolve({authenticationMethod:P(e,r)})}catch(t){return Promise.reject(t)}}),i.onRequest("slates/auth.input.get_default",function(t){var n=t.params;try{b();var r=g(e,n.authenticationMethodId);return r.getDefaultInput?Promise.resolve(r.getDefaultInput()).then(function(t){return{input:t}}):Promise.resolve({input:null})}catch(t){return Promise.reject(t)}}),i.onRequest("slates/auth.input.changed",function(t){var n=t.params;try{b();var r=g(e,n.authenticationMethodId);return r.onInputChanged?Promise.resolve(r.onInputChanged({previousInput:n.previousInput,newInput:n.newInput})).then(function(t){var e;return{success:!0,input:null!=(e=null==t?void 0:t.input)?e:n.newInput}}):Promise.resolve({success:!0,input:n.newInput})}catch(t){return Promise.reject(t)}}),i.onRequest("slates/auth.output.get",function(t){var n=t.params;try{var r,o=function(t){return r?t:{output:a}};b();var i=g(e,n.authenticationMethodId),a=n.input;i.inputSchema&&(a=m(i.inputSchema,a,"auth","Invalid authentication input for method ID: "+n.authenticationMethodId));var u=function(){if("getOutput"in i)return Promise.resolve(i.getOutput({input:a})).then(function(t){return r=1,{output:t.output}})}();return Promise.resolve(u&&u.then?u.then(o):o(u))}catch(t){return Promise.reject(t)}}),i.onRequest("slates/auth.authorization_callback.handle",function(n){var r=n.params;try{var i,a=function(e){if(i)return e;throw new t(o({message:"Authentication method does not support authorization callback handling: "+r.authenticationMethodId}))};b();var u=g(e,r.authenticationMethodId),s=function(){if("handleCallback"in u)return Promise.resolve(u.handleCallback({code:r.code,state:r.state,redirectUri:r.redirectUri,input:r.input,clientId:r.clientId,clientSecret:r.clientSecret,scopes:r.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)}}),i.onRequest("slates/auth.authorization_url.get",function(n){var r=n.params;try{var i,a=function(e){if(i)return e;throw new t(o({message:"Authentication method does not support authorization URL retrieval: "+r.authenticationMethodId}))};b();var u=g(e,r.authenticationMethodId),s=function(){if("getAuthorizationUrl"in u)return Promise.resolve(u.getAuthorizationUrl({redirectUri:r.redirectUri,state:r.state,input:r.input,clientId:r.clientId,clientSecret:r.clientSecret,scopes:r.scopes})).then(function(t){return i=1,{authorizationUrl:t.url,input:t.input}})}();return Promise.resolve(s&&s.then?s.then(a):a(s))}catch(t){return Promise.reject(t)}}),i.onRequest("slates/auth.profile.get",function(n){var r=n.params;try{var i,a=function(e){if(i)return e;throw new t(o({message:"Authentication method does not support profile retrieval: "+r.authenticationMethodId}))};b();var u=g(e,r.authenticationMethodId),s=function(){if(u.getProfile)return Promise.resolve(u.getProfile({output:r.output,input:r.input,scopes:r.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)}}),i.onRequest("slates/auth.token_refresh.handle",function(n){var r=n.params;try{var i,a=function(e){if(i)return e;throw new t(o({message:"Authentication method does not support token refresh handling: "+r.authenticationMethodId}))};b();var u=g(e,r.authenticationMethodId),s=function(){if("handleTokenRefresh"in u&&u.handleTokenRefresh)return Promise.resolve(u.handleTokenRefresh({output:r.output,input:r.input,clientId:r.clientId,clientSecret:r.clientSecret,scopes:r.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)}}),i.onRequest("slates/actions.list",function(t){try{return b(),Promise.resolve({actions:e.actions.map(function(t){return I(0,t)})})}catch(t){return Promise.reject(t)}}),i.onRequest("slates/action.get",function(t){var n=t.params;try{b();var r=y(e,n.actionId);return Promise.resolve({action:I(0,r)})}catch(t){return Promise.reject(t)}}),i.onRequest("slates/action.tool.invoke",function(t){var n=t.params;try{var r,o=S(),i=w(e,"tool",n.actionId),a=m(i.inputSchema,n.input,"input","Invalid input for tool ID: "+n.actionId);return Promise.resolve(i.handleInvocation(new s(o.config,a,null==(r=o.auth)?void 0:r.output,e.spec,d))).then(function(t){return{output:t.output,message:t.message}})}catch(t){return Promise.reject(t)}}),i.onRequest("slates/action.trigger.map_event",function(t){var n=t.params;try{var r,o=S(),i=w(e,"trigger",n.actionId),a=m(i.inputSchema,n.input,"input","Invalid event for trigger ID: "+n.actionId);return Promise.resolve(i.handleEvent(new s(o.config,a,null==(r=o.auth)?void 0:r.output,e.spec,d))).then(function(t){return{id:t.id,type:t.type,output:t.output}})}catch(t){return Promise.reject(t)}}),i.onRequest("slates/action.trigger.poll_events",function(r){var o=r.params;try{var i,a=S(),u=w(e,"trigger",o.actionId);if(!u.pollEvents)throw new t(n({message:"Trigger action does not support polling: "+o.actionId}));return Promise.resolve(u.pollEvents(new s(a.config,{state:o.state},null==(i=a.auth)?void 0:i.output,e.spec,d))).then(function(t){return{inputs:t.inputs,updatedState:t.updatedState}})}catch(t){return Promise.reject(t)}}),i.onRequest("slates/action.trigger.webhook_handle",function(r){var o=r.params;try{var i,a=S(),u=w(e,"trigger",o.actionId);if(!u.handleRequest)throw new t(n({message:"Trigger action does not support webhook requests: "+o.actionId}));var c=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});return Promise.resolve(u.handleRequest(new s(a.config,{request:c,state:o.state},null==(i=a.auth)?void 0:i.output,e.spec,d))).then(function(t){return{inputs:t.inputs,updatedState:t.updatedState}})}catch(t){return Promise.reject(t)}}),i.onRequest("slates/action.trigger.webhook_register",function(r){var o=r.params;try{var i,a=S(),u=w(e,"trigger",o.actionId);if(!u.autoRegisterWebhook)throw new t(n({message:"Trigger action does not support webhook auto-registration: "+o.actionId}));return Promise.resolve(u.autoRegisterWebhook(new s(a.config,{webhookBaseUrl:o.webhookBaseUrl},null==(i=a.auth)?void 0:i.output,e.spec,d))).then(function(t){return{registrationDetails:t.registrationDetails,state:t.state}})}catch(t){return Promise.reject(t)}}),i.onRequest("slates/action.trigger.webhook_unregister",function(r){var o=r.params;try{var i,a=S(),u=w(e,"trigger",o.actionId);if(!u.autoUnregisterWebhook)throw new t(n({message:"Trigger action does not support webhook auto-unregistration: "+o.actionId}));return Promise.resolve(u.autoUnregisterWebhook(new s(a.config,{webhookBaseUrl:o.webhookBaseUrl,registrationDetails:o.registrationDetails,state:o.state},null==(i=a.auth)?void 0:i.output,e.spec,d))).then(function(){return{}})}catch(t){return Promise.reject(t)}}),Promise.resolve()}catch(t){return Promise.reject(t)}})};export{S as createProviderHandler};
|
|
2
|
-
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { badRequestError as badRequestError3, preconditionFailedError, ServiceError as ServiceError4 } from "@lowerdeck/error";
|
|
3
|
+
import {
|
|
4
|
+
createSlatesProviderProtoHandler,
|
|
5
|
+
SLATES_PROTOCOL_VERSION
|
|
6
|
+
} from "@slates/proto";
|
|
7
|
+
import {
|
|
8
|
+
redactUrlAttachmentSecrets,
|
|
9
|
+
runWithContext,
|
|
10
|
+
SlateContext,
|
|
11
|
+
SlateLogger,
|
|
12
|
+
SlatePublicContext,
|
|
13
|
+
uploadAttachmentDirect
|
|
14
|
+
} from "@slates/provider";
|
|
15
|
+
|
|
16
|
+
// src/pQueue.ts
|
|
17
|
+
import PQueueImport from "p-queue";
|
|
18
|
+
var resolveDefaultExport = (value) => {
|
|
19
|
+
let current = value;
|
|
20
|
+
for (let i = 0; i < 4; i++) {
|
|
21
|
+
if (typeof current === "function") return current;
|
|
22
|
+
if (current && typeof current === "object" && "default" in current) {
|
|
23
|
+
current = current.default;
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
throw new TypeError("Module export is not a constructor");
|
|
29
|
+
};
|
|
30
|
+
var PQueue = resolveDefaultExport(PQueueImport);
|
|
31
|
+
|
|
32
|
+
// src/spec.ts
|
|
33
|
+
import { badRequestError, notFoundError, ServiceError as ServiceError2 } from "@lowerdeck/error";
|
|
34
|
+
import {
|
|
35
|
+
SlateDefaultPollingIntervalSeconds
|
|
36
|
+
} from "@slates/provider";
|
|
37
|
+
import z from "zod";
|
|
38
|
+
|
|
39
|
+
// src/validation.ts
|
|
40
|
+
import { ServiceError, validationError } from "@lowerdeck/error";
|
|
41
|
+
var zodToValidationError = (entity, message, e) => {
|
|
42
|
+
return validationError({
|
|
43
|
+
message,
|
|
44
|
+
entity,
|
|
45
|
+
errors: e.issues.map((i) => ({
|
|
46
|
+
...i,
|
|
47
|
+
path: i.path.map((p) => String(p))
|
|
48
|
+
}))
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
var validate = (schema, data, entity, message) => {
|
|
52
|
+
let result = schema.safeParse(data);
|
|
53
|
+
if (!result.success) {
|
|
54
|
+
throw new ServiceError(zodToValidationError(entity, message, result.error));
|
|
55
|
+
}
|
|
56
|
+
return result.data;
|
|
57
|
+
};
|
|
58
|
+
var toJsonSchema = (schema) => schema.toJSONSchema({
|
|
59
|
+
unrepresentable: "any",
|
|
60
|
+
override: (ctx) => {
|
|
61
|
+
let def = ctx.zodSchema._zod.def;
|
|
62
|
+
if (def.type === "date") {
|
|
63
|
+
ctx.jsonSchema.type = "string";
|
|
64
|
+
ctx.jsonSchema.format = "date-time";
|
|
65
|
+
}
|
|
66
|
+
if (def.type === "bigint") {
|
|
67
|
+
ctx.jsonSchema.type = "number";
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
// src/spec.ts
|
|
73
|
+
var getAuthMethod = (slate, authenticationMethodId) => {
|
|
74
|
+
let authMethod = slate.spec.auth.authStack.find((m) => m.key === authenticationMethodId);
|
|
75
|
+
if (!authMethod) {
|
|
76
|
+
throw new ServiceError2(
|
|
77
|
+
badRequestError({
|
|
78
|
+
message: `Invalid authentication method ID: ${authenticationMethodId}`
|
|
79
|
+
})
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
return authMethod;
|
|
83
|
+
};
|
|
84
|
+
var mapAuthMethod = (slate, m) => ({
|
|
85
|
+
id: m.key,
|
|
86
|
+
name: m.name,
|
|
87
|
+
type: m.type,
|
|
88
|
+
scopes: "scopes" in m ? m.scopes.map((s) => ({
|
|
89
|
+
id: s.scope,
|
|
90
|
+
title: s.title,
|
|
91
|
+
description: s.description
|
|
92
|
+
})) : void 0,
|
|
93
|
+
inputSchema: toJsonSchema(m.inputSchema ?? z.object({})),
|
|
94
|
+
outputSchema: toJsonSchema(slate.spec.auth.outputSchema),
|
|
95
|
+
capabilities: {
|
|
96
|
+
getDefaultInput: { enabled: !!("getDefaultInput" in m && m.getDefaultInput) },
|
|
97
|
+
handleTokenRefresh: {
|
|
98
|
+
enabled: !!("handleTokenRefresh" in m && m.handleTokenRefresh)
|
|
99
|
+
},
|
|
100
|
+
handleChangedInput: {
|
|
101
|
+
enabled: !!m.onInputChanged
|
|
102
|
+
},
|
|
103
|
+
getProfile: { enabled: !!m.getProfile }
|
|
104
|
+
},
|
|
105
|
+
docs: m.docs ?? []
|
|
106
|
+
});
|
|
107
|
+
var getAction = (slate, actionId) => {
|
|
108
|
+
let action = slate.actions.find((m) => m.key === actionId);
|
|
109
|
+
if (!action) {
|
|
110
|
+
throw new ServiceError2(notFoundError(`action`, actionId));
|
|
111
|
+
}
|
|
112
|
+
return action;
|
|
113
|
+
};
|
|
114
|
+
var getAdapter = (slate, adapterId) => {
|
|
115
|
+
let adapter = slate.adapters.find((m) => m.id === adapterId);
|
|
116
|
+
if (!adapter) {
|
|
117
|
+
throw new ServiceError2(notFoundError(`adapter`, adapterId));
|
|
118
|
+
}
|
|
119
|
+
return adapter;
|
|
120
|
+
};
|
|
121
|
+
var mapAdapter = (adapter) => ({
|
|
122
|
+
id: adapter.id,
|
|
123
|
+
name: adapter.name,
|
|
124
|
+
capabilities: adapter.capabilities
|
|
125
|
+
});
|
|
126
|
+
var getActionWithType = (slate, type, actionId) => {
|
|
127
|
+
let action = getAction(slate, actionId);
|
|
128
|
+
if (action.type !== type) {
|
|
129
|
+
throw new ServiceError2(
|
|
130
|
+
badRequestError({
|
|
131
|
+
message: `Action with ID ${actionId} is not of type ${type}`
|
|
132
|
+
})
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
return action;
|
|
136
|
+
};
|
|
137
|
+
var mapAction = (_slate, a) => {
|
|
138
|
+
let base = {
|
|
139
|
+
id: a.key,
|
|
140
|
+
name: a.name,
|
|
141
|
+
description: a.description,
|
|
142
|
+
instructions: a.instructions,
|
|
143
|
+
constraints: a.constraints,
|
|
144
|
+
tags: a.tags,
|
|
145
|
+
metadata: a.metadata,
|
|
146
|
+
scopes: a.scopes,
|
|
147
|
+
authMethods: a.authMethods,
|
|
148
|
+
docs: a.docs ?? [],
|
|
149
|
+
...a.adapter ? { adapter: a.adapter } : {},
|
|
150
|
+
inputSchema: toJsonSchema(a.inputSchema),
|
|
151
|
+
outputSchema: toJsonSchema(a.outputSchema)
|
|
152
|
+
};
|
|
153
|
+
if (a.type === "tool") {
|
|
154
|
+
return {
|
|
155
|
+
...base,
|
|
156
|
+
type: "action.tool",
|
|
157
|
+
capabilities: {},
|
|
158
|
+
isPublic: a.isPublic
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
return {
|
|
162
|
+
...base,
|
|
163
|
+
type: "action.trigger",
|
|
164
|
+
capabilities: {},
|
|
165
|
+
triggerGroupId: a.triggerGroup.key
|
|
166
|
+
};
|
|
167
|
+
};
|
|
168
|
+
var isMappableTrigger = (action) => action.type !== "trigger" || !!action.triggerGroup;
|
|
169
|
+
var getMappableAction = (slate, actionId) => {
|
|
170
|
+
let action = getAction(slate, actionId);
|
|
171
|
+
if (!isMappableTrigger(action)) {
|
|
172
|
+
throw new ServiceError2(notFoundError(`action`, actionId));
|
|
173
|
+
}
|
|
174
|
+
return action;
|
|
175
|
+
};
|
|
176
|
+
var getTriggerGroup = (slate, triggerGroupId) => {
|
|
177
|
+
let group = slate.triggerGroups.find((g) => g.key === triggerGroupId);
|
|
178
|
+
if (!group) {
|
|
179
|
+
throw new ServiceError2(notFoundError(`trigger_group`, triggerGroupId));
|
|
180
|
+
}
|
|
181
|
+
return group;
|
|
182
|
+
};
|
|
183
|
+
var getTriggersForGroup = (slate, triggerGroupId) => slate.actions.filter(
|
|
184
|
+
(action) => action.type === "trigger" && action.triggerGroup?.key === triggerGroupId
|
|
185
|
+
);
|
|
186
|
+
var evaluateTriggerMatches = (slate, triggerGroupId, payload) => getTriggersForGroup(slate, triggerGroupId).filter((trigger) => trigger.matches(payload)).map((trigger) => trigger.key);
|
|
187
|
+
var getWebhookAutoRegistration = (group) => {
|
|
188
|
+
if (group.source !== "webhook" || !group.webhook?.autoRegistration) {
|
|
189
|
+
throw new ServiceError2(
|
|
190
|
+
badRequestError({
|
|
191
|
+
message: `Trigger group does not support webhook auto-registration: ${group.key}`
|
|
192
|
+
})
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
return group.webhook.autoRegistration;
|
|
196
|
+
};
|
|
197
|
+
var getWebhookManualRegistration = (group) => {
|
|
198
|
+
if (group.source !== "webhook" || !group.webhook?.manualRegistration) {
|
|
199
|
+
throw new ServiceError2(
|
|
200
|
+
badRequestError({
|
|
201
|
+
message: `Trigger group does not support manual webhook registration: ${group.key}`
|
|
202
|
+
})
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
return group.webhook.manualRegistration;
|
|
206
|
+
};
|
|
207
|
+
var mapTriggerGroup = (group) => ({
|
|
208
|
+
id: group.key,
|
|
209
|
+
name: group.name,
|
|
210
|
+
description: group.description,
|
|
211
|
+
metadata: group.metadata,
|
|
212
|
+
invocation: group.source === "polling" ? {
|
|
213
|
+
type: "polling",
|
|
214
|
+
intervalSeconds: group.polling?.intervalSeconds ?? SlateDefaultPollingIntervalSeconds
|
|
215
|
+
} : {
|
|
216
|
+
type: "webhook",
|
|
217
|
+
registration: group.webhook?.manualRegistration ? {
|
|
218
|
+
mode: "manual",
|
|
219
|
+
userConfigSchema: toJsonSchema(
|
|
220
|
+
group.webhook.manualRegistration.userConfigSchema
|
|
221
|
+
),
|
|
222
|
+
fullConfigSchema: toJsonSchema(
|
|
223
|
+
group.webhook.manualRegistration.fullConfigSchema
|
|
224
|
+
)
|
|
225
|
+
} : { mode: "auto" }
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
// src/state.ts
|
|
230
|
+
var State = class {
|
|
231
|
+
#value;
|
|
232
|
+
get value() {
|
|
233
|
+
return this.#value;
|
|
234
|
+
}
|
|
235
|
+
get() {
|
|
236
|
+
return this.#value;
|
|
237
|
+
}
|
|
238
|
+
set(value) {
|
|
239
|
+
this.#value = value;
|
|
240
|
+
}
|
|
241
|
+
constructor(initialValue) {
|
|
242
|
+
this.#value = initialValue;
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
// src/webhook.ts
|
|
247
|
+
import { badRequestError as badRequestError2, ServiceError as ServiceError3 } from "@lowerdeck/error";
|
|
248
|
+
var SLATE_WEBHOOK_RESPONSE_MAX_BODY_BYTES = 1024 * 1024;
|
|
249
|
+
var serializeBody = (body) => {
|
|
250
|
+
if (body === null) return null;
|
|
251
|
+
if (body.byteLength > SLATE_WEBHOOK_RESPONSE_MAX_BODY_BYTES) {
|
|
252
|
+
throw new ServiceError3(
|
|
253
|
+
badRequestError2({
|
|
254
|
+
message: `Webhook response body exceeds the ${SLATE_WEBHOOK_RESPONSE_MAX_BODY_BYTES}-byte limit`
|
|
255
|
+
})
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
return {
|
|
259
|
+
encoding: "base64",
|
|
260
|
+
content: Buffer.from(body).toString("base64")
|
|
261
|
+
};
|
|
262
|
+
};
|
|
263
|
+
var headersToObject = (headers) => {
|
|
264
|
+
let result = {};
|
|
265
|
+
headers.forEach((value, key) => {
|
|
266
|
+
result[key] = value;
|
|
267
|
+
});
|
|
268
|
+
return result;
|
|
269
|
+
};
|
|
270
|
+
var validateStatus = (status) => {
|
|
271
|
+
if (!Number.isInteger(status) || status < 100 || status > 599) {
|
|
272
|
+
throw new ServiceError3(
|
|
273
|
+
badRequestError2({
|
|
274
|
+
message: "Webhook response status must be an integer between 100 and 599"
|
|
275
|
+
})
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
var serializeWebhookHttpResponse = async (response) => {
|
|
280
|
+
if (response instanceof Response) {
|
|
281
|
+
validateStatus(response.status);
|
|
282
|
+
return {
|
|
283
|
+
status: response.status,
|
|
284
|
+
headers: headersToObject(response.headers),
|
|
285
|
+
body: serializeBody(
|
|
286
|
+
response.body === null ? null : new Uint8Array(await response.arrayBuffer())
|
|
287
|
+
)
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
let status = response.status ?? 200;
|
|
291
|
+
validateStatus(status);
|
|
292
|
+
let body = typeof response.body === "string" ? new TextEncoder().encode(response.body) : response.body ?? null;
|
|
293
|
+
return {
|
|
294
|
+
status,
|
|
295
|
+
headers: response.headers ?? {},
|
|
296
|
+
body: serializeBody(body)
|
|
297
|
+
};
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
// src/index.ts
|
|
301
|
+
var DEFAULT_MAX_ATTACHMENT_SIZE_BYTES = 1e8;
|
|
302
|
+
var routeAttachmentsThroughDirectUpload = async (attachments, live) => {
|
|
303
|
+
if (!attachments || attachments.length === 0 || !live) return attachments;
|
|
304
|
+
let contentEntries = attachments.map((attachment, index) => ({ attachment, index })).filter((entry) => entry.attachment.content.type === "content");
|
|
305
|
+
if (contentEntries.length === 0) return attachments;
|
|
306
|
+
let queue = new PQueue({ concurrency: 10 });
|
|
307
|
+
let replacements = /* @__PURE__ */ new Map();
|
|
308
|
+
await Promise.all(
|
|
309
|
+
contentEntries.map(
|
|
310
|
+
(entry) => queue.add(async () => {
|
|
311
|
+
try {
|
|
312
|
+
let body = entry.attachment.content.type === "content" ? entry.attachment.content.encoding === "base64" ? Buffer.from(entry.attachment.content.content, "base64") : Buffer.from(entry.attachment.content.content, "utf-8") : Buffer.alloc(0);
|
|
313
|
+
replacements.set(
|
|
314
|
+
entry.index,
|
|
315
|
+
await uploadAttachmentDirect({
|
|
316
|
+
live,
|
|
317
|
+
mimeType: entry.attachment.mimeType,
|
|
318
|
+
body
|
|
319
|
+
})
|
|
320
|
+
);
|
|
321
|
+
} catch {
|
|
322
|
+
replacements.set(entry.index, null);
|
|
323
|
+
}
|
|
324
|
+
})
|
|
325
|
+
)
|
|
326
|
+
);
|
|
327
|
+
return attachments.map(
|
|
328
|
+
(attachment, index) => replacements.has(index) ? replacements.get(index) : attachment
|
|
329
|
+
).filter((a) => a != null);
|
|
330
|
+
};
|
|
331
|
+
var isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
332
|
+
var getObjectKeyCount = (value) => isRecord(value) ? Object.keys(value).length : void 0;
|
|
333
|
+
var DOWNLOAD_ATTACHMENT_URL_KEYS = /* @__PURE__ */ new Set([
|
|
334
|
+
"downloadUrl",
|
|
335
|
+
"fileUrl",
|
|
336
|
+
"temporaryDownloadUrl",
|
|
337
|
+
"webContentLink"
|
|
338
|
+
]);
|
|
339
|
+
var isAttachmentUrl = (value) => /^[a-z][a-z0-9+.-]*:\/\//i.test(value);
|
|
340
|
+
var collectOutputUrlAttachments = (value, seen = /* @__PURE__ */ new Set()) => {
|
|
341
|
+
if (Array.isArray(value)) {
|
|
342
|
+
return value.flatMap((item) => collectOutputUrlAttachments(item, seen));
|
|
343
|
+
}
|
|
344
|
+
if (!isRecord(value)) {
|
|
345
|
+
return [];
|
|
346
|
+
}
|
|
347
|
+
let attachments = [];
|
|
348
|
+
for (let [key, nestedValue] of Object.entries(value)) {
|
|
349
|
+
if (DOWNLOAD_ATTACHMENT_URL_KEYS.has(key) && typeof nestedValue === "string" && nestedValue.length > 0 && isAttachmentUrl(nestedValue) && !seen.has(nestedValue)) {
|
|
350
|
+
seen.add(nestedValue);
|
|
351
|
+
attachments.push({
|
|
352
|
+
content: {
|
|
353
|
+
type: "url",
|
|
354
|
+
url: nestedValue
|
|
355
|
+
}
|
|
356
|
+
});
|
|
357
|
+
continue;
|
|
358
|
+
}
|
|
359
|
+
attachments.push(...collectOutputUrlAttachments(nestedValue, seen));
|
|
360
|
+
}
|
|
361
|
+
return attachments;
|
|
362
|
+
};
|
|
363
|
+
var mergeAttachments = (explicitAttachments, output) => {
|
|
364
|
+
let attachments = [...explicitAttachments ?? []];
|
|
365
|
+
let seen = new Set(attachments.map((attachment) => JSON.stringify(attachment)));
|
|
366
|
+
for (let attachment of collectOutputUrlAttachments(output)) {
|
|
367
|
+
let key = JSON.stringify(attachment);
|
|
368
|
+
if (seen.has(key)) continue;
|
|
369
|
+
seen.add(key);
|
|
370
|
+
attachments.push(attachment);
|
|
371
|
+
}
|
|
372
|
+
return attachments.length > 0 ? attachments : void 0;
|
|
373
|
+
};
|
|
374
|
+
var toErrorMetadata = (error) => {
|
|
375
|
+
if (error instanceof Error) {
|
|
376
|
+
return {
|
|
377
|
+
errorName: error.name,
|
|
378
|
+
errorMessage: error.message,
|
|
379
|
+
errorStack: error.stack
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
return {
|
|
383
|
+
errorValue: String(error)
|
|
384
|
+
};
|
|
385
|
+
};
|
|
386
|
+
var formatEntityLabel = (name, key) => `"${name}" (${key})`;
|
|
387
|
+
var resolveTraceMessage = (message, result) => typeof message === "function" ? message(result) : message;
|
|
388
|
+
var createProviderHandler = (slate, listeners) => createSlatesProviderProtoHandler(async (manager) => {
|
|
389
|
+
let protocol = new State(null);
|
|
390
|
+
let participants = new State(null);
|
|
391
|
+
let auth = new State(null);
|
|
392
|
+
let config = new State(null);
|
|
393
|
+
let session = new State(null);
|
|
394
|
+
let hubCapabilities = new State(null);
|
|
395
|
+
let liveInvocation = new State(
|
|
396
|
+
null
|
|
397
|
+
);
|
|
398
|
+
let getLiveInvocation = () => {
|
|
399
|
+
let directUpload = hubCapabilities.get()?.attachments?.directUpload;
|
|
400
|
+
let live = liveInvocation.get();
|
|
401
|
+
if (!directUpload?.enabled || !live) return null;
|
|
402
|
+
return {
|
|
403
|
+
...live,
|
|
404
|
+
maxAttachmentSizeBytes: directUpload.maxAttachmentSizeBytes ?? DEFAULT_MAX_ATTACHMENT_SIZE_BYTES
|
|
405
|
+
};
|
|
406
|
+
};
|
|
407
|
+
let supportsTriggerGroups = () => !!hubCapabilities.get()?.triggers;
|
|
408
|
+
let logger = new SlateLogger(listeners);
|
|
409
|
+
let providerTrace = {
|
|
410
|
+
providerId: slate.spec.key,
|
|
411
|
+
providerName: slate.spec.name
|
|
412
|
+
};
|
|
413
|
+
let traceProviderCall = async (trace, handler) => {
|
|
414
|
+
let startedAt = Date.now();
|
|
415
|
+
logger.info({
|
|
416
|
+
...providerTrace,
|
|
417
|
+
...trace.metadata,
|
|
418
|
+
component: trace.component,
|
|
419
|
+
functionName: trace.functionName,
|
|
420
|
+
phase: "start",
|
|
421
|
+
message: trace.message
|
|
422
|
+
});
|
|
423
|
+
try {
|
|
424
|
+
let result = await handler();
|
|
425
|
+
let successMessage = resolveTraceMessage(trace.successMessage, result);
|
|
426
|
+
logger.info({
|
|
427
|
+
...providerTrace,
|
|
428
|
+
...trace.metadata,
|
|
429
|
+
...trace.onSuccess?.(result) ?? {},
|
|
430
|
+
component: trace.component,
|
|
431
|
+
functionName: trace.functionName,
|
|
432
|
+
phase: "success",
|
|
433
|
+
durationMs: Date.now() - startedAt,
|
|
434
|
+
message: successMessage
|
|
435
|
+
});
|
|
436
|
+
return result;
|
|
437
|
+
} catch (error) {
|
|
438
|
+
logger.error({
|
|
439
|
+
...providerTrace,
|
|
440
|
+
...trace.metadata,
|
|
441
|
+
...toErrorMetadata(error),
|
|
442
|
+
component: trace.component,
|
|
443
|
+
functionName: trace.functionName,
|
|
444
|
+
phase: "error",
|
|
445
|
+
durationMs: Date.now() - startedAt,
|
|
446
|
+
message: trace.errorMessage ?? `${typeof trace.successMessage === "string" ? trace.successMessage : trace.message} failed`
|
|
447
|
+
});
|
|
448
|
+
throw error;
|
|
449
|
+
}
|
|
450
|
+
};
|
|
451
|
+
let getContextBasic = () => {
|
|
452
|
+
let currentProtocol = protocol.get();
|
|
453
|
+
let currentParticipants = participants.get();
|
|
454
|
+
if (!currentProtocol || !currentParticipants) {
|
|
455
|
+
throw new ServiceError4(
|
|
456
|
+
preconditionFailedError({
|
|
457
|
+
message: "Connection context has not been initialized"
|
|
458
|
+
})
|
|
459
|
+
);
|
|
460
|
+
}
|
|
461
|
+
return {
|
|
462
|
+
protocol: currentProtocol,
|
|
463
|
+
participants: currentParticipants
|
|
464
|
+
};
|
|
465
|
+
};
|
|
466
|
+
let getContextFull = () => {
|
|
467
|
+
let basic = getContextBasic();
|
|
468
|
+
let currentConfig = config.get();
|
|
469
|
+
let currentSession = session.get();
|
|
470
|
+
let currentAuth = auth.get();
|
|
471
|
+
if (!currentConfig || !currentSession || !currentAuth && slate.spec.auth.authStack.length > 0) {
|
|
472
|
+
throw new ServiceError4(
|
|
473
|
+
preconditionFailedError({
|
|
474
|
+
message: "Session context has not been initialized"
|
|
475
|
+
})
|
|
476
|
+
);
|
|
477
|
+
}
|
|
478
|
+
return {
|
|
479
|
+
...basic,
|
|
480
|
+
config: currentConfig.value,
|
|
481
|
+
session: currentSession,
|
|
482
|
+
auth: currentAuth
|
|
483
|
+
};
|
|
484
|
+
};
|
|
485
|
+
let getAuthConfig = () => config.get()?.value ?? {};
|
|
486
|
+
let getEmptyContext = () => new SlateContext({}, {}, {}, slate.spec, logger);
|
|
487
|
+
let getAuthContext = () => new SlateContext(getAuthConfig(), {}, {}, slate.spec, logger);
|
|
488
|
+
let withRequestTraces = (context, result) => {
|
|
489
|
+
let requestTraces = context.getHttpTraces();
|
|
490
|
+
return requestTraces.length > 0 ? { ...result, requestTraces } : result;
|
|
491
|
+
};
|
|
492
|
+
manager.onNotification("slates/hello", async ({ params }) => {
|
|
493
|
+
protocol.set(params.protocol);
|
|
494
|
+
});
|
|
495
|
+
manager.onNotification("slates/participant.set", async ({ params }) => {
|
|
496
|
+
if (!protocol.get()) {
|
|
497
|
+
throw new ServiceError4(
|
|
498
|
+
preconditionFailedError({ message: "Connection protocol has not been initialized" })
|
|
499
|
+
);
|
|
500
|
+
}
|
|
501
|
+
participants.set(params.participants);
|
|
502
|
+
});
|
|
503
|
+
manager.onNotification("slates/auth.set", async ({ params }) => {
|
|
504
|
+
getContextBasic();
|
|
505
|
+
getAuthMethod(slate, params.authenticationMethodId);
|
|
506
|
+
let valRes = validate(
|
|
507
|
+
slate.spec.authSchema,
|
|
508
|
+
params.output,
|
|
509
|
+
"auth",
|
|
510
|
+
`Invalid authentication output for method ID: ${params.authenticationMethodId}`
|
|
511
|
+
);
|
|
512
|
+
auth.set({
|
|
513
|
+
authenticationMethodId: params.authenticationMethodId,
|
|
514
|
+
output: valRes
|
|
515
|
+
});
|
|
516
|
+
});
|
|
517
|
+
manager.onNotification("slates/config.set", async ({ params }) => {
|
|
518
|
+
getContextBasic();
|
|
519
|
+
let value = validate(
|
|
520
|
+
slate.spec.configSchema,
|
|
521
|
+
params.config,
|
|
522
|
+
"config",
|
|
523
|
+
"Invalid configuration"
|
|
524
|
+
);
|
|
525
|
+
config.set({ value });
|
|
526
|
+
});
|
|
527
|
+
manager.onNotification("slates/session.start", async ({ params }) => {
|
|
528
|
+
getContextBasic();
|
|
529
|
+
session.set({
|
|
530
|
+
id: params.sessionId,
|
|
531
|
+
state: params.state
|
|
532
|
+
});
|
|
533
|
+
});
|
|
534
|
+
manager.onNotification("slates/hub.capabilities.set", async ({ params }) => {
|
|
535
|
+
hubCapabilities.set(params.capabilities);
|
|
536
|
+
});
|
|
537
|
+
manager.onNotification("slates/hub.live_invocation.set", async ({ params }) => {
|
|
538
|
+
liveInvocation.set({
|
|
539
|
+
token: params.token,
|
|
540
|
+
baseUrl: params.baseUrl
|
|
541
|
+
});
|
|
542
|
+
});
|
|
543
|
+
manager.onRequest("slates/provider.capabilities.get", async () => ({
|
|
544
|
+
capabilities: {
|
|
545
|
+
hub: {
|
|
546
|
+
capabilitiesNotification: true,
|
|
547
|
+
liveInvocation: true
|
|
548
|
+
},
|
|
549
|
+
provider: {
|
|
550
|
+
triggerGroups: (slate.triggerGroups?.length ?? 0) > 0 && (slate.actions ?? []).every(isMappableTrigger)
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
}));
|
|
554
|
+
manager.onRequest("slates/config.changed", async ({ params }) => {
|
|
555
|
+
getContextBasic();
|
|
556
|
+
let newConfig = validate(
|
|
557
|
+
slate.spec.config.configSchema,
|
|
558
|
+
params.newConfig,
|
|
559
|
+
"config",
|
|
560
|
+
"Invalid configuration"
|
|
561
|
+
);
|
|
562
|
+
let configChanged = slate.spec.config.handlers.configChanged;
|
|
563
|
+
if (!configChanged) {
|
|
564
|
+
return { success: true, config: newConfig };
|
|
565
|
+
}
|
|
566
|
+
let context = getEmptyContext();
|
|
567
|
+
let updatedConfig = await traceProviderCall(
|
|
568
|
+
{
|
|
569
|
+
component: "config",
|
|
570
|
+
functionName: "configChanged",
|
|
571
|
+
message: "Running config change handler",
|
|
572
|
+
successMessage: "Config change handler completed",
|
|
573
|
+
metadata: {
|
|
574
|
+
hasPreviousConfig: params.previousConfig !== null,
|
|
575
|
+
newConfigKeyCount: getObjectKeyCount(newConfig)
|
|
576
|
+
},
|
|
577
|
+
onSuccess: (result) => ({
|
|
578
|
+
returnedConfig: !!result?.config
|
|
579
|
+
})
|
|
580
|
+
},
|
|
581
|
+
() => runWithContext(
|
|
582
|
+
context,
|
|
583
|
+
async () => configChanged({
|
|
584
|
+
previousConfig: params.previousConfig,
|
|
585
|
+
newConfig
|
|
586
|
+
})
|
|
587
|
+
)
|
|
588
|
+
);
|
|
589
|
+
return withRequestTraces(context, {
|
|
590
|
+
success: true,
|
|
591
|
+
config: updatedConfig?.config ?? newConfig
|
|
592
|
+
});
|
|
593
|
+
});
|
|
594
|
+
manager.onRequest("slates/config.get_default", async () => {
|
|
595
|
+
getContextBasic();
|
|
596
|
+
let getDefaultConfig = slate.spec.config.handlers.getDefaultConfig;
|
|
597
|
+
if (!getDefaultConfig) {
|
|
598
|
+
return { config: null };
|
|
599
|
+
}
|
|
600
|
+
let context = getEmptyContext();
|
|
601
|
+
let defaultConfig = await traceProviderCall(
|
|
602
|
+
{
|
|
603
|
+
component: "config",
|
|
604
|
+
functionName: "getDefaultConfig",
|
|
605
|
+
message: "Getting default config",
|
|
606
|
+
successMessage: "Default config retrieved",
|
|
607
|
+
onSuccess: (result) => ({
|
|
608
|
+
configKeyCount: getObjectKeyCount(result)
|
|
609
|
+
})
|
|
610
|
+
},
|
|
611
|
+
() => runWithContext(context, async () => getDefaultConfig())
|
|
612
|
+
);
|
|
613
|
+
return withRequestTraces(context, {
|
|
614
|
+
config: defaultConfig ?? null
|
|
615
|
+
});
|
|
616
|
+
});
|
|
617
|
+
manager.onRequest("slates/config.schema.get", async () => {
|
|
618
|
+
getContextBasic();
|
|
619
|
+
return {
|
|
620
|
+
schema: toJsonSchema(slate.spec.configSchema),
|
|
621
|
+
docs: slate.spec.config.docsReferences ?? []
|
|
622
|
+
};
|
|
623
|
+
});
|
|
624
|
+
manager.onRequest("slates/provider.identify", async () => {
|
|
625
|
+
getContextBasic();
|
|
626
|
+
return {
|
|
627
|
+
protocol: SLATES_PROTOCOL_VERSION,
|
|
628
|
+
provider: {
|
|
629
|
+
type: "provider",
|
|
630
|
+
id: slate.spec.key,
|
|
631
|
+
name: slate.spec.name,
|
|
632
|
+
description: slate.spec.description,
|
|
633
|
+
metadata: slate.spec.parameters.metadata
|
|
634
|
+
},
|
|
635
|
+
docs: slate.spec.docs ?? []
|
|
636
|
+
};
|
|
637
|
+
});
|
|
638
|
+
manager.onRequest("slates/auth.methods.list", async () => {
|
|
639
|
+
getContextBasic();
|
|
640
|
+
return {
|
|
641
|
+
authenticationMethods: slate.spec.auth.authStack.map((m) => mapAuthMethod(slate, m))
|
|
642
|
+
};
|
|
643
|
+
});
|
|
644
|
+
manager.onRequest("slates/auth.method.get", async ({ params }) => {
|
|
645
|
+
getContextBasic();
|
|
646
|
+
let authMethod = getAuthMethod(slate, params.authenticationMethodId);
|
|
647
|
+
return {
|
|
648
|
+
authenticationMethod: mapAuthMethod(slate, authMethod)
|
|
649
|
+
};
|
|
650
|
+
});
|
|
651
|
+
manager.onRequest("slates/auth.input.get_default", async ({ params }) => {
|
|
652
|
+
getContextBasic();
|
|
653
|
+
let authMethod = getAuthMethod(slate, params.authenticationMethodId);
|
|
654
|
+
if (!authMethod.getDefaultInput) {
|
|
655
|
+
return { input: null };
|
|
656
|
+
}
|
|
657
|
+
let context = getEmptyContext();
|
|
658
|
+
let input = await traceProviderCall(
|
|
659
|
+
{
|
|
660
|
+
component: "auth",
|
|
661
|
+
functionName: "getDefaultInput",
|
|
662
|
+
message: "Getting default authentication input",
|
|
663
|
+
successMessage: "Default authentication input retrieved",
|
|
664
|
+
metadata: {
|
|
665
|
+
authenticationMethodId: params.authenticationMethodId,
|
|
666
|
+
authenticationMethodName: authMethod.name
|
|
667
|
+
},
|
|
668
|
+
onSuccess: (result) => ({
|
|
669
|
+
inputKeyCount: getObjectKeyCount(result)
|
|
670
|
+
})
|
|
671
|
+
},
|
|
672
|
+
() => runWithContext(context, () => authMethod.getDefaultInput())
|
|
673
|
+
);
|
|
674
|
+
return withRequestTraces(context, { input });
|
|
675
|
+
});
|
|
676
|
+
manager.onRequest("slates/auth.input.changed", async ({ params }) => {
|
|
677
|
+
getContextBasic();
|
|
678
|
+
let authMethod = getAuthMethod(slate, params.authenticationMethodId);
|
|
679
|
+
if (!authMethod.onInputChanged) {
|
|
680
|
+
return { success: true, input: params.newInput };
|
|
681
|
+
}
|
|
682
|
+
let context = getEmptyContext();
|
|
683
|
+
let updatedInput = await traceProviderCall(
|
|
684
|
+
{
|
|
685
|
+
component: "auth",
|
|
686
|
+
functionName: "onInputChanged",
|
|
687
|
+
message: "Running authentication input change handler",
|
|
688
|
+
successMessage: "Authentication input change handler completed",
|
|
689
|
+
metadata: {
|
|
690
|
+
authenticationMethodId: params.authenticationMethodId,
|
|
691
|
+
authenticationMethodName: authMethod.name,
|
|
692
|
+
hasPreviousInput: params.previousInput !== null,
|
|
693
|
+
newInputKeyCount: getObjectKeyCount(params.newInput)
|
|
694
|
+
},
|
|
695
|
+
onSuccess: (result) => ({
|
|
696
|
+
returnedInput: !!result?.input
|
|
697
|
+
})
|
|
698
|
+
},
|
|
699
|
+
() => runWithContext(
|
|
700
|
+
context,
|
|
701
|
+
() => authMethod.onInputChanged({
|
|
702
|
+
previousInput: params.previousInput,
|
|
703
|
+
newInput: params.newInput
|
|
704
|
+
})
|
|
705
|
+
)
|
|
706
|
+
);
|
|
707
|
+
return withRequestTraces(context, {
|
|
708
|
+
success: true,
|
|
709
|
+
input: updatedInput?.input ?? params.newInput
|
|
710
|
+
});
|
|
711
|
+
});
|
|
712
|
+
manager.onRequest("slates/auth.output.get", async ({ params }) => {
|
|
713
|
+
getContextBasic();
|
|
714
|
+
let authMethod = getAuthMethod(slate, params.authenticationMethodId);
|
|
715
|
+
let input = params.input;
|
|
716
|
+
if (authMethod.inputSchema) {
|
|
717
|
+
input = validate(
|
|
718
|
+
authMethod.inputSchema,
|
|
719
|
+
input,
|
|
720
|
+
"auth",
|
|
721
|
+
`Invalid authentication input for method ID: ${params.authenticationMethodId}`
|
|
722
|
+
);
|
|
723
|
+
}
|
|
724
|
+
if ("getOutput" in authMethod) {
|
|
725
|
+
let context = getAuthContext();
|
|
726
|
+
let outputRes = await traceProviderCall(
|
|
727
|
+
{
|
|
728
|
+
component: "auth",
|
|
729
|
+
functionName: "getOutput",
|
|
730
|
+
message: "Getting authentication output",
|
|
731
|
+
successMessage: "Authentication output retrieved",
|
|
732
|
+
metadata: {
|
|
733
|
+
authenticationMethodId: params.authenticationMethodId,
|
|
734
|
+
authenticationMethodName: authMethod.name,
|
|
735
|
+
inputKeyCount: getObjectKeyCount(input)
|
|
736
|
+
},
|
|
737
|
+
onSuccess: (result) => ({
|
|
738
|
+
outputKeyCount: getObjectKeyCount(result.output),
|
|
739
|
+
scopeCount: result.scopes?.length ?? 0
|
|
740
|
+
})
|
|
741
|
+
},
|
|
742
|
+
() => runWithContext(
|
|
743
|
+
context,
|
|
744
|
+
() => authMethod.getOutput({
|
|
745
|
+
input,
|
|
746
|
+
config: getAuthConfig()
|
|
747
|
+
})
|
|
748
|
+
)
|
|
749
|
+
);
|
|
750
|
+
return withRequestTraces(context, {
|
|
751
|
+
output: outputRes.output,
|
|
752
|
+
scopes: outputRes.scopes
|
|
753
|
+
});
|
|
754
|
+
}
|
|
755
|
+
return { output: input };
|
|
756
|
+
});
|
|
757
|
+
manager.onRequest("slates/auth.authorization_callback.handle", async ({ params }) => {
|
|
758
|
+
getContextBasic();
|
|
759
|
+
let authMethod = getAuthMethod(slate, params.authenticationMethodId);
|
|
760
|
+
if ("handleCallback" in authMethod) {
|
|
761
|
+
let context = getEmptyContext();
|
|
762
|
+
let callbackRes = await traceProviderCall(
|
|
763
|
+
{
|
|
764
|
+
component: "auth",
|
|
765
|
+
functionName: "handleCallback",
|
|
766
|
+
message: "Handling authentication callback",
|
|
767
|
+
successMessage: "Authentication callback handled",
|
|
768
|
+
metadata: {
|
|
769
|
+
authenticationMethodId: params.authenticationMethodId,
|
|
770
|
+
authenticationMethodName: authMethod.name,
|
|
771
|
+
scopeCount: params.scopes.length,
|
|
772
|
+
hasCallbackState: !!params.callbackState
|
|
773
|
+
},
|
|
774
|
+
onSuccess: (result) => ({
|
|
775
|
+
outputKeyCount: getObjectKeyCount(result.output),
|
|
776
|
+
returnedInput: !!result.input,
|
|
777
|
+
returnedScopeCount: result.scopes?.length
|
|
778
|
+
})
|
|
779
|
+
},
|
|
780
|
+
() => runWithContext(
|
|
781
|
+
context,
|
|
782
|
+
() => authMethod.handleCallback({
|
|
783
|
+
code: params.code,
|
|
784
|
+
state: params.state,
|
|
785
|
+
redirectUri: params.redirectUri,
|
|
786
|
+
input: params.input,
|
|
787
|
+
clientId: params.clientId,
|
|
788
|
+
clientSecret: params.clientSecret,
|
|
789
|
+
scopes: params.scopes,
|
|
790
|
+
callbackParams: params.callbackParams || {},
|
|
791
|
+
callbackState: params.callbackState || {},
|
|
792
|
+
config: getAuthConfig()
|
|
793
|
+
})
|
|
794
|
+
)
|
|
795
|
+
);
|
|
796
|
+
return withRequestTraces(context, {
|
|
797
|
+
output: callbackRes.output,
|
|
798
|
+
input: callbackRes.input,
|
|
799
|
+
scopes: callbackRes.scopes
|
|
800
|
+
});
|
|
801
|
+
}
|
|
802
|
+
throw new ServiceError4(
|
|
803
|
+
preconditionFailedError({
|
|
804
|
+
message: `Authentication method does not support authorization callback handling: ${params.authenticationMethodId}`
|
|
805
|
+
})
|
|
806
|
+
);
|
|
807
|
+
});
|
|
808
|
+
manager.onRequest("slates/auth.authorization_url.get", async ({ params }) => {
|
|
809
|
+
getContextBasic();
|
|
810
|
+
let authMethod = getAuthMethod(slate, params.authenticationMethodId);
|
|
811
|
+
if ("getAuthorizationUrl" in authMethod) {
|
|
812
|
+
let context = getEmptyContext();
|
|
813
|
+
let urlRes = await traceProviderCall(
|
|
814
|
+
{
|
|
815
|
+
component: "auth",
|
|
816
|
+
functionName: "getAuthorizationUrl",
|
|
817
|
+
message: "Getting authentication authorization URL",
|
|
818
|
+
successMessage: "Authentication authorization URL retrieved",
|
|
819
|
+
metadata: {
|
|
820
|
+
authenticationMethodId: params.authenticationMethodId,
|
|
821
|
+
authenticationMethodName: authMethod.name,
|
|
822
|
+
scopeCount: params.scopes.length,
|
|
823
|
+
inputKeyCount: getObjectKeyCount(params.input)
|
|
824
|
+
},
|
|
825
|
+
onSuccess: (result) => ({
|
|
826
|
+
returnedInput: !!result.input,
|
|
827
|
+
hasCallbackState: !!result.callbackState
|
|
828
|
+
})
|
|
829
|
+
},
|
|
830
|
+
() => runWithContext(
|
|
831
|
+
context,
|
|
832
|
+
() => authMethod.getAuthorizationUrl({
|
|
833
|
+
redirectUri: params.redirectUri,
|
|
834
|
+
state: params.state,
|
|
835
|
+
input: params.input,
|
|
836
|
+
clientId: params.clientId,
|
|
837
|
+
clientSecret: params.clientSecret,
|
|
838
|
+
scopes: params.scopes,
|
|
839
|
+
config: getAuthConfig()
|
|
840
|
+
})
|
|
841
|
+
)
|
|
842
|
+
);
|
|
843
|
+
return withRequestTraces(context, {
|
|
844
|
+
authorizationUrl: urlRes.url,
|
|
845
|
+
input: urlRes.input,
|
|
846
|
+
callbackState: urlRes.callbackState
|
|
847
|
+
});
|
|
848
|
+
}
|
|
849
|
+
throw new ServiceError4(
|
|
850
|
+
preconditionFailedError({
|
|
851
|
+
message: `Authentication method does not support authorization URL retrieval: ${params.authenticationMethodId}`
|
|
852
|
+
})
|
|
853
|
+
);
|
|
854
|
+
});
|
|
855
|
+
manager.onRequest("slates/auth.profile.get", async ({ params }) => {
|
|
856
|
+
getContextBasic();
|
|
857
|
+
let authMethod = getAuthMethod(slate, params.authenticationMethodId);
|
|
858
|
+
if (authMethod.getProfile) {
|
|
859
|
+
let context = getAuthContext();
|
|
860
|
+
let profileRes = await traceProviderCall(
|
|
861
|
+
{
|
|
862
|
+
component: "auth",
|
|
863
|
+
functionName: "getProfile",
|
|
864
|
+
message: "Getting authentication profile",
|
|
865
|
+
successMessage: "Authentication profile retrieved",
|
|
866
|
+
metadata: {
|
|
867
|
+
authenticationMethodId: params.authenticationMethodId,
|
|
868
|
+
authenticationMethodName: authMethod.name,
|
|
869
|
+
scopeCount: params.scopes.length,
|
|
870
|
+
inputKeyCount: getObjectKeyCount(params.input),
|
|
871
|
+
outputKeyCount: getObjectKeyCount(params.output)
|
|
872
|
+
},
|
|
873
|
+
onSuccess: (result) => ({
|
|
874
|
+
profileKeyCount: getObjectKeyCount(result.profile)
|
|
875
|
+
})
|
|
876
|
+
},
|
|
877
|
+
() => runWithContext(context, () => {
|
|
878
|
+
if (authMethod.type === "auth.oauth") {
|
|
879
|
+
return authMethod.getProfile({
|
|
880
|
+
output: params.output,
|
|
881
|
+
input: params.input,
|
|
882
|
+
scopes: params.scopes,
|
|
883
|
+
config: getAuthConfig()
|
|
884
|
+
});
|
|
885
|
+
}
|
|
886
|
+
return authMethod.getProfile({
|
|
887
|
+
output: params.output,
|
|
888
|
+
input: params.input,
|
|
889
|
+
scopes: params.scopes,
|
|
890
|
+
config: getAuthConfig()
|
|
891
|
+
});
|
|
892
|
+
})
|
|
893
|
+
);
|
|
894
|
+
return withRequestTraces(context, {
|
|
895
|
+
profile: profileRes.profile
|
|
896
|
+
});
|
|
897
|
+
}
|
|
898
|
+
throw new ServiceError4(
|
|
899
|
+
preconditionFailedError({
|
|
900
|
+
message: `Authentication method does not support profile retrieval: ${params.authenticationMethodId}`
|
|
901
|
+
})
|
|
902
|
+
);
|
|
903
|
+
});
|
|
904
|
+
manager.onRequest("slates/auth.token_refresh.handle", async ({ params }) => {
|
|
905
|
+
getContextBasic();
|
|
906
|
+
let authMethod = getAuthMethod(slate, params.authenticationMethodId);
|
|
907
|
+
if ("handleTokenRefresh" in authMethod && authMethod.handleTokenRefresh) {
|
|
908
|
+
let context = getAuthContext();
|
|
909
|
+
let refreshRes = await traceProviderCall(
|
|
910
|
+
{
|
|
911
|
+
component: "auth",
|
|
912
|
+
functionName: "handleTokenRefresh",
|
|
913
|
+
message: "Refreshing authentication token",
|
|
914
|
+
successMessage: "Authentication token refreshed",
|
|
915
|
+
metadata: {
|
|
916
|
+
authenticationMethodId: params.authenticationMethodId,
|
|
917
|
+
authenticationMethodName: authMethod.name,
|
|
918
|
+
scopeCount: params.scopes.length,
|
|
919
|
+
inputKeyCount: getObjectKeyCount(params.input),
|
|
920
|
+
outputKeyCount: getObjectKeyCount(params.output)
|
|
921
|
+
},
|
|
922
|
+
onSuccess: (result) => ({
|
|
923
|
+
refreshedOutputKeyCount: getObjectKeyCount(result.output),
|
|
924
|
+
returnedInput: !!result.input
|
|
925
|
+
})
|
|
926
|
+
},
|
|
927
|
+
() => runWithContext(context, () => {
|
|
928
|
+
if (authMethod.type === "auth.oauth") {
|
|
929
|
+
return authMethod.handleTokenRefresh({
|
|
930
|
+
output: params.output,
|
|
931
|
+
input: params.input,
|
|
932
|
+
clientId: params.clientId,
|
|
933
|
+
clientSecret: params.clientSecret,
|
|
934
|
+
scopes: params.scopes,
|
|
935
|
+
config: getAuthConfig()
|
|
936
|
+
});
|
|
937
|
+
}
|
|
938
|
+
return authMethod.handleTokenRefresh({
|
|
939
|
+
output: params.output,
|
|
940
|
+
input: params.input,
|
|
941
|
+
clientId: params.clientId,
|
|
942
|
+
clientSecret: params.clientSecret,
|
|
943
|
+
scopes: params.scopes,
|
|
944
|
+
config: getAuthConfig()
|
|
945
|
+
});
|
|
946
|
+
})
|
|
947
|
+
);
|
|
948
|
+
return withRequestTraces(context, {
|
|
949
|
+
output: refreshRes.output,
|
|
950
|
+
input: refreshRes.input
|
|
951
|
+
});
|
|
952
|
+
}
|
|
953
|
+
throw new ServiceError4(
|
|
954
|
+
preconditionFailedError({
|
|
955
|
+
message: `Authentication method does not support token refresh handling: ${params.authenticationMethodId}`
|
|
956
|
+
})
|
|
957
|
+
);
|
|
958
|
+
});
|
|
959
|
+
manager.onRequest("slates/actions.list", async ({ params }) => {
|
|
960
|
+
getContextBasic();
|
|
961
|
+
let actions = params.includeAdapterActions ? slate.actions : slate.actions.filter((action) => !action.adapter);
|
|
962
|
+
actions = actions.filter(isMappableTrigger);
|
|
963
|
+
if (!supportsTriggerGroups()) {
|
|
964
|
+
actions = actions.filter((action) => action.type !== "trigger");
|
|
965
|
+
}
|
|
966
|
+
return {
|
|
967
|
+
actions: actions.map((a) => mapAction(slate, a))
|
|
968
|
+
};
|
|
969
|
+
});
|
|
970
|
+
manager.onRequest("slates/adapters.list", async () => {
|
|
971
|
+
getContextBasic();
|
|
972
|
+
return {
|
|
973
|
+
adapters: slate.adapters.map(mapAdapter)
|
|
974
|
+
};
|
|
975
|
+
});
|
|
976
|
+
manager.onRequest("slates/adapter.get", async ({ params }) => {
|
|
977
|
+
getContextBasic();
|
|
978
|
+
let adapter = getAdapter(slate, params.adapterId);
|
|
979
|
+
return {
|
|
980
|
+
adapter: mapAdapter(adapter)
|
|
981
|
+
};
|
|
982
|
+
});
|
|
983
|
+
manager.onRequest("slates/action.get", async ({ params }) => {
|
|
984
|
+
getContextBasic();
|
|
985
|
+
let action = getMappableAction(slate, params.actionId);
|
|
986
|
+
return {
|
|
987
|
+
action: mapAction(slate, action)
|
|
988
|
+
};
|
|
989
|
+
});
|
|
990
|
+
manager.onRequest("slates/trigger_groups.list", async () => {
|
|
991
|
+
getContextBasic();
|
|
992
|
+
return {
|
|
993
|
+
triggerGroups: slate.triggerGroups.map(mapTriggerGroup)
|
|
994
|
+
};
|
|
995
|
+
});
|
|
996
|
+
manager.onRequest("slates/trigger_group.get", async ({ params }) => {
|
|
997
|
+
getContextBasic();
|
|
998
|
+
let triggerGroup = getTriggerGroup(slate, params.triggerGroupId);
|
|
999
|
+
return {
|
|
1000
|
+
triggerGroup: mapTriggerGroup(triggerGroup)
|
|
1001
|
+
};
|
|
1002
|
+
});
|
|
1003
|
+
manager.onRequest("slates/action.tool.invoke", async ({ params }) => {
|
|
1004
|
+
let action = getActionWithType(slate, "tool", params.actionId);
|
|
1005
|
+
let input = validate(
|
|
1006
|
+
action.inputSchema,
|
|
1007
|
+
params.input,
|
|
1008
|
+
"input",
|
|
1009
|
+
`Invalid input for tool ID: ${params.actionId}`
|
|
1010
|
+
);
|
|
1011
|
+
let invoke = async (context) => {
|
|
1012
|
+
let res = await traceProviderCall(
|
|
1013
|
+
{
|
|
1014
|
+
component: "action",
|
|
1015
|
+
functionName: "handleInvocation",
|
|
1016
|
+
message: `Starting tool ${formatEntityLabel(action.name, action.key)}`,
|
|
1017
|
+
successMessage: `Completed tool ${formatEntityLabel(action.name, action.key)}`,
|
|
1018
|
+
errorMessage: `Tool ${formatEntityLabel(action.name, action.key)} failed`,
|
|
1019
|
+
metadata: {
|
|
1020
|
+
actionId: action.key,
|
|
1021
|
+
actionName: action.name,
|
|
1022
|
+
actionType: action.type,
|
|
1023
|
+
isPublic: action.isPublic,
|
|
1024
|
+
inputKeyCount: getObjectKeyCount(input)
|
|
1025
|
+
},
|
|
1026
|
+
onSuccess: (result) => ({
|
|
1027
|
+
hasMessage: !!result.message,
|
|
1028
|
+
actionResultMessage: result.message,
|
|
1029
|
+
outputKeyCount: getObjectKeyCount(result.output),
|
|
1030
|
+
attachmentCount: result.attachments?.length
|
|
1031
|
+
})
|
|
1032
|
+
},
|
|
1033
|
+
() => runWithContext(context, () => action.handleInvocation(context))
|
|
1034
|
+
);
|
|
1035
|
+
let contextAttachments = await context._finalizeAttachments();
|
|
1036
|
+
let merged = mergeAttachments(
|
|
1037
|
+
[...res.attachments ?? [], ...contextAttachments],
|
|
1038
|
+
res.output
|
|
1039
|
+
);
|
|
1040
|
+
let redacted = redactUrlAttachmentSecrets(
|
|
1041
|
+
merged,
|
|
1042
|
+
context._getAuthConfigForRedaction()
|
|
1043
|
+
);
|
|
1044
|
+
let finalAttachments = await routeAttachmentsThroughDirectUpload(
|
|
1045
|
+
redacted,
|
|
1046
|
+
getLiveInvocation()
|
|
1047
|
+
);
|
|
1048
|
+
return withRequestTraces(context, {
|
|
1049
|
+
output: res.output,
|
|
1050
|
+
message: res.message,
|
|
1051
|
+
attachments: finalAttachments
|
|
1052
|
+
});
|
|
1053
|
+
};
|
|
1054
|
+
if (action.isPublic) {
|
|
1055
|
+
getContextBasic();
|
|
1056
|
+
return invoke(new SlatePublicContext(input, slate.spec, logger, getLiveInvocation()));
|
|
1057
|
+
}
|
|
1058
|
+
let ctx = getContextFull();
|
|
1059
|
+
return invoke(
|
|
1060
|
+
new SlateContext(
|
|
1061
|
+
ctx.config,
|
|
1062
|
+
input,
|
|
1063
|
+
ctx.auth?.output,
|
|
1064
|
+
slate.spec,
|
|
1065
|
+
logger,
|
|
1066
|
+
getLiveInvocation()
|
|
1067
|
+
)
|
|
1068
|
+
);
|
|
1069
|
+
});
|
|
1070
|
+
manager.onRequest("slates/action.trigger.map_event", async ({ params }) => {
|
|
1071
|
+
let ctx = getContextFull();
|
|
1072
|
+
let action = getActionWithType(slate, "trigger", params.actionId);
|
|
1073
|
+
let input = validate(
|
|
1074
|
+
action.inputSchema,
|
|
1075
|
+
params.input,
|
|
1076
|
+
"input",
|
|
1077
|
+
`Invalid event for trigger ID: ${params.actionId}`
|
|
1078
|
+
);
|
|
1079
|
+
let context = new SlateContext(ctx.config, input, ctx.auth?.output, slate.spec, logger);
|
|
1080
|
+
let res = await traceProviderCall(
|
|
1081
|
+
{
|
|
1082
|
+
component: "action",
|
|
1083
|
+
functionName: "handleEvent",
|
|
1084
|
+
message: `Mapping event for trigger ${formatEntityLabel(action.name, action.key)}`,
|
|
1085
|
+
successMessage: (result) => `Mapped trigger event "${result.type}" for ${formatEntityLabel(action.name, action.key)}`,
|
|
1086
|
+
errorMessage: `Trigger ${formatEntityLabel(action.name, action.key)} failed while mapping an event`,
|
|
1087
|
+
metadata: {
|
|
1088
|
+
actionId: action.key,
|
|
1089
|
+
actionName: action.name,
|
|
1090
|
+
actionType: action.type,
|
|
1091
|
+
inputKeyCount: getObjectKeyCount(input)
|
|
1092
|
+
},
|
|
1093
|
+
onSuccess: (result) => ({
|
|
1094
|
+
eventType: result.type,
|
|
1095
|
+
hasEventId: !!result.id,
|
|
1096
|
+
outputKeyCount: getObjectKeyCount(result.output)
|
|
1097
|
+
})
|
|
1098
|
+
},
|
|
1099
|
+
() => runWithContext(context, () => action.map(context))
|
|
1100
|
+
);
|
|
1101
|
+
return withRequestTraces(context, { id: res.id, type: res.type, output: res.output });
|
|
1102
|
+
});
|
|
1103
|
+
manager.onRequest("slates/action.trigger.poll_events", async ({ params }) => {
|
|
1104
|
+
getContextBasic();
|
|
1105
|
+
if (supportsTriggerGroups()) {
|
|
1106
|
+
throw new ServiceError4(
|
|
1107
|
+
badRequestError3({
|
|
1108
|
+
message: `Legacy trigger polling is disabled once trigger_group support is announced: ${params.actionId}`
|
|
1109
|
+
})
|
|
1110
|
+
);
|
|
1111
|
+
}
|
|
1112
|
+
return { inputs: [], updatedState: params.state };
|
|
1113
|
+
});
|
|
1114
|
+
manager.onRequest("slates/action.trigger.webhook_handle", async ({ params }) => {
|
|
1115
|
+
getContextBasic();
|
|
1116
|
+
if (supportsTriggerGroups()) {
|
|
1117
|
+
throw new ServiceError4(
|
|
1118
|
+
badRequestError3({
|
|
1119
|
+
message: `Legacy trigger webhook handling is disabled once trigger_group support is announced: ${params.actionId}`
|
|
1120
|
+
})
|
|
1121
|
+
);
|
|
1122
|
+
}
|
|
1123
|
+
return { inputs: [], updatedState: params.state, response: null };
|
|
1124
|
+
});
|
|
1125
|
+
manager.onRequest("slates/action.trigger.webhook_register", async ({ params }) => {
|
|
1126
|
+
getContextBasic();
|
|
1127
|
+
if (supportsTriggerGroups()) {
|
|
1128
|
+
throw new ServiceError4(
|
|
1129
|
+
badRequestError3({
|
|
1130
|
+
message: `Legacy trigger webhook registration is disabled once trigger_group support is announced: ${params.actionId}`
|
|
1131
|
+
})
|
|
1132
|
+
);
|
|
1133
|
+
}
|
|
1134
|
+
return { registrationDetails: null };
|
|
1135
|
+
});
|
|
1136
|
+
manager.onRequest("slates/action.trigger.webhook_unregister", async ({ params }) => {
|
|
1137
|
+
getContextBasic();
|
|
1138
|
+
if (supportsTriggerGroups()) {
|
|
1139
|
+
throw new ServiceError4(
|
|
1140
|
+
badRequestError3({
|
|
1141
|
+
message: `Legacy trigger webhook unregistration is disabled once trigger_group support is announced: ${params.actionId}`
|
|
1142
|
+
})
|
|
1143
|
+
);
|
|
1144
|
+
}
|
|
1145
|
+
return {};
|
|
1146
|
+
});
|
|
1147
|
+
manager.onRequest("slates/trigger_group.webhook.targets_list", async ({ params }) => {
|
|
1148
|
+
let ctx = getContextFull();
|
|
1149
|
+
let group = getTriggerGroup(slate, params.triggerGroupId);
|
|
1150
|
+
let autoRegistration = getWebhookAutoRegistration(group);
|
|
1151
|
+
let context = new SlateContext(
|
|
1152
|
+
ctx.config,
|
|
1153
|
+
{ pageToken: params.pageToken ?? null },
|
|
1154
|
+
ctx.auth?.output,
|
|
1155
|
+
slate.spec,
|
|
1156
|
+
logger
|
|
1157
|
+
);
|
|
1158
|
+
let res = await traceProviderCall(
|
|
1159
|
+
{
|
|
1160
|
+
component: "action",
|
|
1161
|
+
functionName: "webhookTargetList",
|
|
1162
|
+
message: `Listing webhook targets for trigger group ${formatEntityLabel(group.name, group.key)}`,
|
|
1163
|
+
successMessage: (result) => `Listed ${result.targets.length} webhook target(s) for trigger group ${formatEntityLabel(group.name, group.key)}`,
|
|
1164
|
+
errorMessage: `Trigger group ${formatEntityLabel(group.name, group.key)} failed while listing webhook targets`,
|
|
1165
|
+
metadata: {
|
|
1166
|
+
triggerGroupId: group.key,
|
|
1167
|
+
triggerGroupName: group.name
|
|
1168
|
+
},
|
|
1169
|
+
onSuccess: (result) => ({
|
|
1170
|
+
targetCount: result.targets.length,
|
|
1171
|
+
hasNextPageToken: result.nextPageToken !== null
|
|
1172
|
+
})
|
|
1173
|
+
},
|
|
1174
|
+
() => runWithContext(context, () => autoRegistration.webhookTargetList(context))
|
|
1175
|
+
);
|
|
1176
|
+
return withRequestTraces(context, {
|
|
1177
|
+
targets: res.targets,
|
|
1178
|
+
nextPageToken: res.nextPageToken ?? null
|
|
1179
|
+
});
|
|
1180
|
+
});
|
|
1181
|
+
manager.onRequest("slates/trigger_group.webhook.register", async ({ params }) => {
|
|
1182
|
+
let ctx = getContextFull();
|
|
1183
|
+
let group = getTriggerGroup(slate, params.triggerGroupId);
|
|
1184
|
+
let autoRegistration = getWebhookAutoRegistration(group);
|
|
1185
|
+
let context = new SlateContext(
|
|
1186
|
+
ctx.config,
|
|
1187
|
+
{
|
|
1188
|
+
webhookTargetIdentifier: params.webhookTargetIdentifier,
|
|
1189
|
+
webhookTargetPayload: params.webhookTargetPayload,
|
|
1190
|
+
webhookUrl: params.webhookUrl
|
|
1191
|
+
},
|
|
1192
|
+
ctx.auth?.output,
|
|
1193
|
+
slate.spec,
|
|
1194
|
+
logger
|
|
1195
|
+
);
|
|
1196
|
+
let res = await traceProviderCall(
|
|
1197
|
+
{
|
|
1198
|
+
component: "action",
|
|
1199
|
+
functionName: "webhookRegister",
|
|
1200
|
+
message: `Registering webhook for trigger group ${formatEntityLabel(group.name, group.key)}`,
|
|
1201
|
+
successMessage: `Registered webhook for trigger group ${formatEntityLabel(group.name, group.key)}`,
|
|
1202
|
+
errorMessage: `Trigger group ${formatEntityLabel(group.name, group.key)} failed while registering a webhook`,
|
|
1203
|
+
metadata: {
|
|
1204
|
+
triggerGroupId: group.key,
|
|
1205
|
+
triggerGroupName: group.name,
|
|
1206
|
+
webhookTargetIdentifier: params.webhookTargetIdentifier
|
|
1207
|
+
}
|
|
1208
|
+
},
|
|
1209
|
+
() => runWithContext(context, () => autoRegistration.webhookRegister(context))
|
|
1210
|
+
);
|
|
1211
|
+
return withRequestTraces(context, {
|
|
1212
|
+
webhookRegistrationIdentifier: res.webhookRegistrationIdentifier,
|
|
1213
|
+
webhookRegistrationPayload: res.webhookRegistrationPayload
|
|
1214
|
+
});
|
|
1215
|
+
});
|
|
1216
|
+
manager.onRequest("slates/trigger_group.webhook.unregister", async ({ params }) => {
|
|
1217
|
+
let ctx = getContextFull();
|
|
1218
|
+
let group = getTriggerGroup(slate, params.triggerGroupId);
|
|
1219
|
+
let autoRegistration = getWebhookAutoRegistration(group);
|
|
1220
|
+
let context = new SlateContext(
|
|
1221
|
+
ctx.config,
|
|
1222
|
+
{
|
|
1223
|
+
webhookRegistrationIdentifier: params.webhookRegistrationIdentifier,
|
|
1224
|
+
webhookRegistrationPayload: params.webhookRegistrationPayload
|
|
1225
|
+
},
|
|
1226
|
+
ctx.auth?.output,
|
|
1227
|
+
slate.spec,
|
|
1228
|
+
logger
|
|
1229
|
+
);
|
|
1230
|
+
await traceProviderCall(
|
|
1231
|
+
{
|
|
1232
|
+
component: "action",
|
|
1233
|
+
functionName: "webhookUnregister",
|
|
1234
|
+
message: `Unregistering webhook for trigger group ${formatEntityLabel(group.name, group.key)}`,
|
|
1235
|
+
successMessage: `Unregistered webhook for trigger group ${formatEntityLabel(group.name, group.key)}`,
|
|
1236
|
+
errorMessage: `Trigger group ${formatEntityLabel(group.name, group.key)} failed while unregistering a webhook`,
|
|
1237
|
+
metadata: {
|
|
1238
|
+
triggerGroupId: group.key,
|
|
1239
|
+
triggerGroupName: group.name,
|
|
1240
|
+
webhookRegistrationIdentifier: params.webhookRegistrationIdentifier
|
|
1241
|
+
}
|
|
1242
|
+
},
|
|
1243
|
+
() => runWithContext(context, () => autoRegistration.webhookUnregister(context))
|
|
1244
|
+
);
|
|
1245
|
+
return withRequestTraces(context, {});
|
|
1246
|
+
});
|
|
1247
|
+
manager.onRequest("slates/trigger_group.webhook.manual_setup", async ({ params }) => {
|
|
1248
|
+
getContextBasic();
|
|
1249
|
+
let group = getTriggerGroup(slate, params.triggerGroupId);
|
|
1250
|
+
let manualRegistration = getWebhookManualRegistration(group);
|
|
1251
|
+
let context = new SlateContext(
|
|
1252
|
+
{},
|
|
1253
|
+
{ webhookUrl: params.webhookUrl },
|
|
1254
|
+
{},
|
|
1255
|
+
slate.spec,
|
|
1256
|
+
logger
|
|
1257
|
+
);
|
|
1258
|
+
let res = await traceProviderCall(
|
|
1259
|
+
{
|
|
1260
|
+
component: "action",
|
|
1261
|
+
functionName: "webhookManualSetup",
|
|
1262
|
+
message: `Building manual webhook setup for trigger group ${formatEntityLabel(group.name, group.key)}`,
|
|
1263
|
+
successMessage: `Built manual webhook setup for trigger group ${formatEntityLabel(group.name, group.key)}`,
|
|
1264
|
+
errorMessage: `Trigger group ${formatEntityLabel(group.name, group.key)} failed while building a manual webhook setup`,
|
|
1265
|
+
metadata: {
|
|
1266
|
+
triggerGroupId: group.key,
|
|
1267
|
+
triggerGroupName: group.name
|
|
1268
|
+
}
|
|
1269
|
+
},
|
|
1270
|
+
() => runWithContext(context, () => manualRegistration.setup(context))
|
|
1271
|
+
);
|
|
1272
|
+
return {
|
|
1273
|
+
webhookSetupDocument: res.webhookSetupDocument,
|
|
1274
|
+
partialWebhookRegistrationPayload: res.partialWebhookRegistrationPayload
|
|
1275
|
+
};
|
|
1276
|
+
});
|
|
1277
|
+
manager.onRequest("slates/trigger_group.webhook.manual_finish", async ({ params }) => {
|
|
1278
|
+
getContextBasic();
|
|
1279
|
+
let group = getTriggerGroup(slate, params.triggerGroupId);
|
|
1280
|
+
let manualRegistration = getWebhookManualRegistration(group);
|
|
1281
|
+
if (!manualRegistration.finish) {
|
|
1282
|
+
throw new ServiceError4(
|
|
1283
|
+
badRequestError3({
|
|
1284
|
+
message: `Trigger group does not support manual webhook finish: ${params.triggerGroupId}`
|
|
1285
|
+
})
|
|
1286
|
+
);
|
|
1287
|
+
}
|
|
1288
|
+
let context = new SlateContext(
|
|
1289
|
+
{},
|
|
1290
|
+
{
|
|
1291
|
+
webhookUrl: params.webhookUrl,
|
|
1292
|
+
partialWebhookRegistrationPayload: params.partialWebhookRegistrationPayload,
|
|
1293
|
+
userWebhookRegistrationPayload: params.userWebhookRegistrationPayload
|
|
1294
|
+
},
|
|
1295
|
+
{},
|
|
1296
|
+
slate.spec,
|
|
1297
|
+
logger
|
|
1298
|
+
);
|
|
1299
|
+
let res = await traceProviderCall(
|
|
1300
|
+
{
|
|
1301
|
+
component: "action",
|
|
1302
|
+
functionName: "webhookManualFinish",
|
|
1303
|
+
message: `Finishing manual webhook setup for trigger group ${formatEntityLabel(group.name, group.key)}`,
|
|
1304
|
+
successMessage: `Finished manual webhook setup for trigger group ${formatEntityLabel(group.name, group.key)}`,
|
|
1305
|
+
errorMessage: `Trigger group ${formatEntityLabel(group.name, group.key)} failed while finishing a manual webhook setup`,
|
|
1306
|
+
metadata: {
|
|
1307
|
+
triggerGroupId: group.key,
|
|
1308
|
+
triggerGroupName: group.name
|
|
1309
|
+
}
|
|
1310
|
+
},
|
|
1311
|
+
() => runWithContext(context, () => manualRegistration.finish(context))
|
|
1312
|
+
);
|
|
1313
|
+
return withRequestTraces(context, {
|
|
1314
|
+
webhookRegistrationPayload: res.webhookRegistrationPayload
|
|
1315
|
+
});
|
|
1316
|
+
});
|
|
1317
|
+
manager.onRequest("slates/trigger_group.webhook.process", async ({ params }) => {
|
|
1318
|
+
let group = getTriggerGroup(slate, params.triggerGroupId);
|
|
1319
|
+
if (group.source !== "webhook" || !group.webhook) {
|
|
1320
|
+
throw new ServiceError4(
|
|
1321
|
+
badRequestError3({
|
|
1322
|
+
message: `Trigger group does not support webhook processing: ${params.triggerGroupId}`
|
|
1323
|
+
})
|
|
1324
|
+
);
|
|
1325
|
+
}
|
|
1326
|
+
let req = new Request(params.url, {
|
|
1327
|
+
method: params.method,
|
|
1328
|
+
headers: params.headers,
|
|
1329
|
+
body: params.body ? Uint8Array.from(atob(params.body.content), (c) => c.charCodeAt(0)) : null
|
|
1330
|
+
});
|
|
1331
|
+
let context = new SlateContext(
|
|
1332
|
+
{},
|
|
1333
|
+
{
|
|
1334
|
+
request: req,
|
|
1335
|
+
webhookRegistrationPayload: params.webhookRegistrationPayload
|
|
1336
|
+
},
|
|
1337
|
+
{},
|
|
1338
|
+
slate.spec,
|
|
1339
|
+
logger
|
|
1340
|
+
);
|
|
1341
|
+
let res = await traceProviderCall(
|
|
1342
|
+
{
|
|
1343
|
+
component: "action",
|
|
1344
|
+
functionName: "webhookProcess",
|
|
1345
|
+
message: `Processing webhook request for trigger group ${formatEntityLabel(group.name, group.key)}`,
|
|
1346
|
+
successMessage: (result) => `Extracted ${result.events.length} event(s) from webhook request for trigger group ${formatEntityLabel(group.name, group.key)}`,
|
|
1347
|
+
errorMessage: `Trigger group ${formatEntityLabel(group.name, group.key)} failed while processing a webhook request`,
|
|
1348
|
+
metadata: {
|
|
1349
|
+
triggerGroupId: group.key,
|
|
1350
|
+
triggerGroupName: group.name,
|
|
1351
|
+
requestMethod: params.method,
|
|
1352
|
+
hasRequestBody: !!params.body
|
|
1353
|
+
},
|
|
1354
|
+
onSuccess: (result) => ({
|
|
1355
|
+
eventCount: result.events.length,
|
|
1356
|
+
hasResponse: result.response !== void 0
|
|
1357
|
+
})
|
|
1358
|
+
},
|
|
1359
|
+
() => runWithContext(context, () => group.webhook.process(context))
|
|
1360
|
+
);
|
|
1361
|
+
for (let event of res.events) {
|
|
1362
|
+
if (!event.matchers) {
|
|
1363
|
+
throw new ServiceError4(
|
|
1364
|
+
preconditionFailedError({
|
|
1365
|
+
message: `Trigger group "${group.key}" process handler must return matchers for every event (return [] if there is nothing to assert)`
|
|
1366
|
+
})
|
|
1367
|
+
);
|
|
1368
|
+
}
|
|
1369
|
+
}
|
|
1370
|
+
let response = res.response === void 0 ? void 0 : await serializeWebhookHttpResponse(res.response);
|
|
1371
|
+
return withRequestTraces(context, {
|
|
1372
|
+
events: res.events.map((event) => ({
|
|
1373
|
+
matchers: event.matchers,
|
|
1374
|
+
payload: event.payload,
|
|
1375
|
+
idempotencyKey: event.idempotencyKey,
|
|
1376
|
+
triggerIds: evaluateTriggerMatches(slate, group.key, event.payload)
|
|
1377
|
+
})),
|
|
1378
|
+
response
|
|
1379
|
+
});
|
|
1380
|
+
});
|
|
1381
|
+
manager.onRequest("slates/trigger_group.routing_matchers.get", async ({ params }) => {
|
|
1382
|
+
let ctx = getContextFull();
|
|
1383
|
+
let group = getTriggerGroup(slate, params.triggerGroupId);
|
|
1384
|
+
let context = new SlateContext(ctx.config, {}, ctx.auth?.output, slate.spec, logger);
|
|
1385
|
+
let matchers = await traceProviderCall(
|
|
1386
|
+
{
|
|
1387
|
+
component: "action",
|
|
1388
|
+
functionName: "routingMatchers",
|
|
1389
|
+
message: `Getting routing matchers for trigger group ${formatEntityLabel(group.name, group.key)}`,
|
|
1390
|
+
successMessage: (result) => `Retrieved ${result.length} routing matcher(s) for trigger group ${formatEntityLabel(group.name, group.key)}`,
|
|
1391
|
+
errorMessage: `Trigger group ${formatEntityLabel(group.name, group.key)} failed while getting routing matchers`,
|
|
1392
|
+
metadata: {
|
|
1393
|
+
triggerGroupId: group.key,
|
|
1394
|
+
triggerGroupName: group.name
|
|
1395
|
+
},
|
|
1396
|
+
onSuccess: (result) => ({
|
|
1397
|
+
matcherCount: result.length
|
|
1398
|
+
})
|
|
1399
|
+
},
|
|
1400
|
+
() => runWithContext(context, () => group.routingMatchers(context))
|
|
1401
|
+
);
|
|
1402
|
+
return withRequestTraces(context, { matchers });
|
|
1403
|
+
});
|
|
1404
|
+
manager.onRequest("slates/trigger_group.polling.poll", async ({ params }) => {
|
|
1405
|
+
let ctx = getContextFull();
|
|
1406
|
+
let group = getTriggerGroup(slate, params.triggerGroupId);
|
|
1407
|
+
if (group.source !== "polling" || !group.polling) {
|
|
1408
|
+
throw new ServiceError4(
|
|
1409
|
+
badRequestError3({
|
|
1410
|
+
message: `Trigger group does not support polling: ${params.triggerGroupId}`
|
|
1411
|
+
})
|
|
1412
|
+
);
|
|
1413
|
+
}
|
|
1414
|
+
let context = new SlateContext(
|
|
1415
|
+
ctx.config,
|
|
1416
|
+
{ state: params.state },
|
|
1417
|
+
ctx.auth?.output,
|
|
1418
|
+
slate.spec,
|
|
1419
|
+
logger
|
|
1420
|
+
);
|
|
1421
|
+
let res = await traceProviderCall(
|
|
1422
|
+
{
|
|
1423
|
+
component: "action",
|
|
1424
|
+
functionName: "pollEvents",
|
|
1425
|
+
message: `Polling events for trigger group ${formatEntityLabel(group.name, group.key)}`,
|
|
1426
|
+
successMessage: (result) => `Polled ${result.events.length} event(s) for trigger group ${formatEntityLabel(group.name, group.key)}`,
|
|
1427
|
+
errorMessage: `Trigger group ${formatEntityLabel(group.name, group.key)} failed while polling events`,
|
|
1428
|
+
metadata: {
|
|
1429
|
+
triggerGroupId: group.key,
|
|
1430
|
+
triggerGroupName: group.name,
|
|
1431
|
+
hasPreviousState: params.state !== null
|
|
1432
|
+
},
|
|
1433
|
+
onSuccess: (result) => ({
|
|
1434
|
+
eventCount: result.events.length,
|
|
1435
|
+
hasUpdatedState: result.updatedState !== void 0
|
|
1436
|
+
})
|
|
1437
|
+
},
|
|
1438
|
+
() => runWithContext(context, () => group.polling.pollEvents(context))
|
|
1439
|
+
);
|
|
1440
|
+
return withRequestTraces(context, {
|
|
1441
|
+
updatedState: res.updatedState,
|
|
1442
|
+
events: res.events.map((event) => ({
|
|
1443
|
+
payload: event.payload,
|
|
1444
|
+
idempotencyKey: event.idempotencyKey,
|
|
1445
|
+
triggerIds: evaluateTriggerMatches(slate, group.key, event.payload)
|
|
1446
|
+
}))
|
|
1447
|
+
});
|
|
1448
|
+
});
|
|
1449
|
+
});
|
|
1450
|
+
export {
|
|
1451
|
+
createProviderHandler
|
|
1452
|
+
};
|