@botpress/sdk 1.5.0 → 1.6.1
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/.turbo/turbo-build.log +3 -3
- package/dist/bot/index.d.ts +1 -0
- package/dist/bot/types/common.d.ts +0 -6
- package/dist/bot/types/generic.d.ts +10 -7
- package/dist/bot/types/generic.test.d.ts +1 -0
- package/dist/fixtures.d.ts +77 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/integration/index.d.ts +1 -0
- package/dist/integration/types/generic.d.ts +32 -34
- package/dist/integration/types/generic.test.d.ts +1 -0
- package/dist/message.d.ts +6 -6
- package/dist/utils/type-utils.d.ts +8 -2
- package/package.json +1 -1
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
|
|
2
|
-
> @botpress/sdk@1.
|
|
2
|
+
> @botpress/sdk@1.6.1 build /home/runner/work/botpress/botpress/packages/sdk
|
|
3
3
|
> pnpm build:type && pnpm build:node
|
|
4
4
|
|
|
5
5
|
|
|
6
|
-
> @botpress/sdk@1.
|
|
6
|
+
> @botpress/sdk@1.6.1 build:type /home/runner/work/botpress/botpress/packages/sdk
|
|
7
7
|
> tsc --emitDeclarationOnly --declaration
|
|
8
8
|
|
|
9
9
|
|
|
10
|
-
> @botpress/sdk@1.
|
|
10
|
+
> @botpress/sdk@1.6.1 build:node /home/runner/work/botpress/botpress/packages/sdk
|
|
11
11
|
> ts-node -T build.ts
|
|
12
12
|
|
package/dist/bot/index.d.ts
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
1
|
import { Join, UnionToIntersection, Split, Cast } from '../../utils/type-utils';
|
|
2
2
|
import { BaseBot } from './generic';
|
|
3
|
-
/**
|
|
4
|
-
* 0. Definitions
|
|
5
|
-
*/
|
|
6
3
|
export type EventDefinition = BaseBot['events'][string];
|
|
7
4
|
export type StateDefinition = BaseBot['states'][string];
|
|
8
5
|
export type IntegrationInstanceDefinition = BaseBot['integrations'][string];
|
|
@@ -13,9 +10,6 @@ export type IntegrationInstanceMessageDefinition = IntegrationInstanceChannelDef
|
|
|
13
10
|
export type IntegrationInstanceEventDefinition = IntegrationInstanceDefinition['events'][string];
|
|
14
11
|
export type IntegrationInstanceStateDefinition = IntegrationInstanceDefinition['states'][string];
|
|
15
12
|
export type IntegrationInstanceUserDefinition = IntegrationInstanceDefinition['user'];
|
|
16
|
-
/**
|
|
17
|
-
* 1. Enumerations
|
|
18
|
-
*/
|
|
19
13
|
type ActionKey<TIntegrationName extends string, TActionName extends string> = string extends TIntegrationName ? string : string extends TActionName ? string : Join<[TIntegrationName, ':', TActionName]>;
|
|
20
14
|
export type EnumerateActions<TBot extends BaseBot> = UnionToIntersection<{
|
|
21
15
|
[TIntegrationName in keyof TBot['integrations']]: {
|
|
@@ -1,18 +1,21 @@
|
|
|
1
|
-
import { BaseIntegration } from '../../integration/types/generic';
|
|
1
|
+
import { BaseIntegration, DefaultIntegration, InputBaseIntegration } from '../../integration/types/generic';
|
|
2
2
|
import * as utils from '../../utils/type-utils';
|
|
3
3
|
export * from '../../integration/types/generic';
|
|
4
|
+
export type BaseAction = {
|
|
5
|
+
input: any;
|
|
6
|
+
output: any;
|
|
7
|
+
};
|
|
4
8
|
export type BaseBot = {
|
|
5
9
|
integrations: Record<string, BaseIntegration>;
|
|
6
10
|
events: Record<string, any>;
|
|
7
11
|
states: Record<string, any>;
|
|
8
|
-
actions: Record<string,
|
|
12
|
+
actions: Record<string, BaseAction>;
|
|
9
13
|
};
|
|
10
|
-
|
|
11
|
-
* Usefull for tests, allows to create a bot with only the properties you want to override
|
|
12
|
-
*/
|
|
13
|
-
export type MakeBot<B extends Partial<BaseBot>> = {
|
|
14
|
-
integrations: utils.Default<B['integrations'], BaseBot['integrations']>;
|
|
14
|
+
export type DefaultBot<B extends utils.DeepPartial<BaseBot>> = {
|
|
15
15
|
events: utils.Default<B['events'], BaseBot['events']>;
|
|
16
16
|
states: utils.Default<B['states'], BaseBot['states']>;
|
|
17
17
|
actions: utils.Default<B['actions'], BaseBot['actions']>;
|
|
18
|
+
integrations: undefined extends B['integrations'] ? BaseBot['integrations'] : {
|
|
19
|
+
[K in keyof B['integrations']]: DefaultIntegration<utils.Cast<B['integrations'][K], InputBaseIntegration>>;
|
|
20
|
+
};
|
|
18
21
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { DefaultBot } from './bot/types/generic';
|
|
2
|
+
import { DefaultChannel, DefaultIntegration } from './integration/types/generic';
|
|
3
|
+
type _FooBarBazIntegration = {
|
|
4
|
+
actions: {
|
|
5
|
+
doFoo: {
|
|
6
|
+
input: {
|
|
7
|
+
inputFoo: string;
|
|
8
|
+
};
|
|
9
|
+
output: {
|
|
10
|
+
outputFoo: string;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
doBar: {
|
|
14
|
+
input: {
|
|
15
|
+
inputBar: number;
|
|
16
|
+
};
|
|
17
|
+
output: {
|
|
18
|
+
outputBar: number;
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
doBaz: {
|
|
22
|
+
input: {
|
|
23
|
+
inputBaz: boolean;
|
|
24
|
+
};
|
|
25
|
+
output: {
|
|
26
|
+
outputBaz: boolean;
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
events: {
|
|
31
|
+
onFoo: {
|
|
32
|
+
eventFoo: string;
|
|
33
|
+
};
|
|
34
|
+
onBar: {
|
|
35
|
+
eventBar: number;
|
|
36
|
+
};
|
|
37
|
+
onBaz: {
|
|
38
|
+
eventBaz: boolean;
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
channels: {
|
|
42
|
+
channelFoo: DefaultChannel<{
|
|
43
|
+
messages: {
|
|
44
|
+
messageFoo: {
|
|
45
|
+
foo: string;
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
}>;
|
|
49
|
+
channelBar: DefaultChannel<{
|
|
50
|
+
messages: {
|
|
51
|
+
messageBar: {
|
|
52
|
+
bar: number;
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
}>;
|
|
56
|
+
channelBaz: DefaultChannel<{
|
|
57
|
+
messages: {
|
|
58
|
+
messageBaz: {
|
|
59
|
+
baz: boolean;
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
}>;
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
export type FooBarBazIntegration = DefaultIntegration<_FooBarBazIntegration>;
|
|
66
|
+
export type FooBarBazBot = DefaultBot<{
|
|
67
|
+
integrations: {
|
|
68
|
+
fooBarBaz: _FooBarBazIntegration;
|
|
69
|
+
};
|
|
70
|
+
}>;
|
|
71
|
+
export type EmptyBot = DefaultBot<{
|
|
72
|
+
integrations: {};
|
|
73
|
+
events: {};
|
|
74
|
+
states: {};
|
|
75
|
+
actions: {};
|
|
76
|
+
}>;
|
|
77
|
+
export {};
|
package/dist/index.d.ts
CHANGED
|
@@ -3,12 +3,12 @@ export * from './const';
|
|
|
3
3
|
export * from './serve';
|
|
4
4
|
export * from './zui';
|
|
5
5
|
export { isApiError, RuntimeError, } from '@botpress/client';
|
|
6
|
-
export { IntegrationDefinition, IntegrationDefinitionProps, IntegrationImplementation as Integration, IntegrationImplementationProps as IntegrationProps, IntegrationLogger, IntegrationSpecificClient, TagDefinition, ConfigurationDefinition, AdditionalConfigurationDefinition, EventDefinition, ChannelDefinition, MessageDefinition, ActionDefinition, StateDefinition, UserDefinition, SecretDefinition, EntityDefinition, } from './integration';
|
|
6
|
+
export { DefaultIntegration, IntegrationDefinition, IntegrationDefinitionProps, IntegrationImplementation as Integration, IntegrationImplementationProps as IntegrationProps, IntegrationLogger, IntegrationSpecificClient, TagDefinition, ConfigurationDefinition, AdditionalConfigurationDefinition, EventDefinition, ChannelDefinition, MessageDefinition, ActionDefinition, StateDefinition, UserDefinition, SecretDefinition, EntityDefinition, } from './integration';
|
|
7
7
|
export {
|
|
8
8
|
/**
|
|
9
9
|
* @deprecated use Context exported from '.botpress' instead
|
|
10
10
|
*/
|
|
11
11
|
IntegrationContext, } from './integration/server';
|
|
12
|
-
export { BotDefinition, BotDefinitionProps, BotImplementation as Bot, BotImplementationProps as BotProps, BotSpecificClient, TagDefinition as BotTagDefinition, StateType as BotStateType, StateDefinition as BotStateDefinition, RecurringEventDefinition as BotRecurringEventDefinition, EventDefinition as BotEventDefinition, ConfigurationDefinition as BotConfigurationDefinition, UserDefinition as BotUserDefinition, ConversationDefinition as BotConversationDefinition, MessageDefinition as BotMessageDefinition, ActionDefinition as BotActionDefinition, } from './bot';
|
|
12
|
+
export { DefaultBot, BotDefinition, BotDefinitionProps, BotImplementation as Bot, BotImplementationProps as BotProps, BotSpecificClient, TagDefinition as BotTagDefinition, StateType as BotStateType, StateDefinition as BotStateDefinition, RecurringEventDefinition as BotRecurringEventDefinition, EventDefinition as BotEventDefinition, ConfigurationDefinition as BotConfigurationDefinition, UserDefinition as BotUserDefinition, ConversationDefinition as BotConversationDefinition, MessageDefinition as BotMessageDefinition, ActionDefinition as BotActionDefinition, } from './bot';
|
|
13
13
|
export { InterfaceDeclaration, InterfaceDeclarationProps, } from './interface';
|
|
14
14
|
export { IntegrationPackage, InterfacePackage, } from './package';
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var Ce=Object.create;var w=Object.defineProperty;var Ie=Object.getOwnPropertyDescriptor;var _e=Object.getOwnPropertyNames;var Ee=Object.getPrototypeOf,Se=Object.prototype.hasOwnProperty;var I=(t,e)=>{for(var n in e)w(t,n,{get:e[n],enumerable:!0})},P=(t,e,n,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let a of _e(e))!Se.call(t,a)&&a!==n&&w(t,a,{get:()=>e[a],enumerable:!(i=Ie(e,a))||i.enumerable});return t},l=(t,e,n)=>(P(t,e,"default"),n&&P(n,e,"default")),Q=(t,e,n)=>(n=t!=null?Ce(Ee(t)):{},P(e||!t||!t.__esModule?w(n,"default",{value:t,enumerable:!0}):n,t)),xe=t=>P(w({},"__esModule",{value:!0}),t);var h={};I(h,{Bot:()=>G,BotDefinition:()=>F,BotSpecificClient:()=>T,Integration:()=>K,IntegrationDefinition:()=>U,IntegrationSpecificClient:()=>v,InterfaceDeclaration:()=>j,RuntimeError:()=>N.RuntimeError,botIdHeader:()=>_,botUserIdHeader:()=>z,configurationHeader:()=>E,configurationTypeHeader:()=>J,integrationIdHeader:()=>$,isApiError:()=>N.isApiError,messages:()=>Z,operationHeader:()=>S,parseBody:()=>f,serve:()=>x,typeHeader:()=>q,webhookIdHeader:()=>V});module.exports=xe(h);var Z={};I(Z,{defaults:()=>Ae,markdown:()=>De});var r={};I(r,{default:()=>L});var X=require("@bpinternal/zui");l(r,require("@bpinternal/zui"));var L=X.z;var g=r.z.string().min(1),ee=r.z.object({text:g}),te=r.z.object({markdown:g}),ne=r.z.object({imageUrl:g}),se=r.z.object({audioUrl:g}),ie=r.z.object({videoUrl:g}),oe=r.z.object({fileUrl:g,title:g.optional()}),ae=r.z.object({latitude:r.z.number(),longitude:r.z.number(),address:r.z.string().optional(),title:r.z.string().optional()}),re=r.z.object({title:g,subtitle:g.optional(),imageUrl:g.optional(),actions:r.z.array(r.z.object({action:r.z.enum(["postback","url","say"]),label:g,value:g}))}),Y=r.z.object({text:g,options:r.z.array(r.z.object({label:g,value:g}))}),ke=r.z.object({items:r.z.array(re)}),Pe=r.z.union([r.z.object({type:r.z.literal("text"),payload:ee}),r.z.object({type:r.z.literal("markdown"),payload:te}),r.z.object({type:r.z.literal("image"),payload:ne}),r.z.object({type:r.z.literal("audio"),payload:se}),r.z.object({type:r.z.literal("video"),payload:ie}),r.z.object({type:r.z.literal("file"),payload:oe}),r.z.object({type:r.z.literal("location"),payload:ae})]),we=r.z.object({items:r.z.array(Pe)}),De={schema:te},Ae={text:{schema:ee},image:{schema:ne},audio:{schema:se},video:{schema:ie},file:{schema:oe},location:{schema:ae},carousel:{schema:ke},card:{schema:re},dropdown:{schema:Y},choice:{schema:Y},bloc:{schema:we}};var _="x-bot-id",z="x-bot-user-id",$="x-integration-id",V="x-webhook-id",J="x-bp-configuration-type",E="x-bp-configuration",S="x-bp-operation",q="x-bp-type";var ce=require("node:http");var B=console;function f(t){if(!t.body)throw new Error("Missing body");return JSON.parse(t.body)}async function x(t,e=8072,n=Oe){let i=(0,ce.createServer)(async(a,o)=>{try{let s=await Ue(a);if(s.path==="/health"){o.writeHead(200).end("ok");return}let c=await t(s);o.writeHead(c?.status??200,c?.headers??{}).end(c?.body??"{}")}catch(s){B.error("Error while handling request",{error:s?.message??"Internal error occured"}),o.writeHead(500).end(JSON.stringify({error:s?.message??"Internal error occured"}))}});return i.listen(e,()=>n(e)),i}async function Ue(t){let e=await Re(t),n={};for(let a=0;a<t.rawHeaders.length;a+=2){let o=t.rawHeaders[a].toLowerCase(),s=t.rawHeaders[a+1];n[o]=s}let i=new URL(t.url??"",t.headers.host?`http://${t.headers.host}`:"http://botpress.cloud");return{body:e,path:i.pathname,query:Me(i.search,"?"),headers:n,method:t.method?.toUpperCase()??"GET"}}function Me(t,e){return t.indexOf(e)===0?t.slice(e.length):t}async function Re(t){return new Promise((e,n)=>{if(t.method!=="POST"&&t.method!=="PUT"&&t.method!=="PATCH")return e(void 0);let i="";t.on("data",a=>i+=a.toString()),t.on("error",a=>n(a)),t.on("end",()=>e(i))})}function Oe(t){B.info(`Listening on port ${t}`)}l(h,r,module.exports);var N=require("@botpress/client");var m={};I(m,{mapValues:()=>He,pairs:()=>pe});var pe=t=>Object.entries(t),He=(t,e)=>Object.fromEntries(pe(t).map(([n,i])=>[n,e(i,n)]));var y={};I(y,{safePush:()=>Ke});var Ke=(t,e)=>t?[...t,e]:[e];var A=Symbol("schemaName"),le=t=>t?m.mapValues(t,(n,i)=>({...n,[A]:i})):{},ge=t=>A in t&&t[A]!==void 0,de=t=>t[A];var U=class{constructor(e){this.props=e;this.name=e.name,this.version=e.version,this.icon=e.icon,this.readme=e.readme,this.title=e.title,this.identifier=e.identifier,this.description=e.description,this.configuration=e.configuration,this.configurations=e.configurations,this.events=e.events,this.actions=e.actions,this.channels=e.channels,this.states=e.states,this.user=e.user,this.secrets=e.secrets,this.entities=e.entities,this.interfaces=e.interfaces}name;version;title;description;icon;readme;configuration;configurations;events;actions;channels;states;user;secrets;identifier;entities;interfaces;extend(e,n){let i=n(le(this.entities)),a=m.pairs(i).find(([d,u])=>!ge(u));if(a)throw new Error(`Cannot extend interface "${e.definition.name}" with entity "${a[0]}"; the provided schema is not part of the integration's entities.`);let o=this;o.interfaces??={};let s=m.mapValues(i,d=>({name:de(d),schema:d.schema})),c=Object.values(s).map(d=>d.name),p=c.length===0?e.definition.name:`${e.definition.name}<${c.join(",")}>`;return o.interfaces[p]={...e,entities:s},this}};var b=require("@botpress/client");var ue=require("@botpress/client"),M={retries:3,retryCondition:t=>ue.axiosRetry.isNetworkOrIdempotentRequestError(t)||[429,502].includes(t.response?.status??0),retryDelay:t=>t*1e3};var v=class{constructor(e){this._client=e}createConversation=e=>this._client.createConversation(e);getConversation=e=>this._client.getConversation(e);listConversations=e=>this._client.listConversations(e);getOrCreateConversation=e=>this._client.getOrCreateConversation(e);updateConversation=e=>this._client.updateConversation(e);deleteConversation=e=>this._client.deleteConversation(e);listParticipants=e=>this._client.listParticipants(e);addParticipant=e=>this._client.addParticipant(e);getParticipant=e=>this._client.getParticipant(e);removeParticipant=e=>this._client.removeParticipant(e);createEvent=e=>this._client.createEvent(e);getEvent=e=>this._client.getEvent(e);listEvents=e=>this._client.listEvents(e);createMessage=e=>this._client.createMessage(e);getOrCreateMessage=e=>this._client.getOrCreateMessage(e);getMessage=e=>this._client.getMessage(e);updateMessage=e=>this._client.updateMessage(e);listMessages=e=>this._client.listMessages(e);deleteMessage=e=>this._client.deleteMessage(e);createUser=e=>this._client.createUser(e);getUser=e=>this._client.getUser(e);listUsers=e=>this._client.listUsers(e);getOrCreateUser=e=>this._client.getOrCreateUser(e);updateUser=e=>this._client.updateUser(e);deleteUser=e=>this._client.deleteUser(e);getState=e=>this._client.getState(e);setState=e=>this._client.setState(e);getOrSetState=e=>this._client.getOrSetState(e);patchState=e=>this._client.patchState(e);configureIntegration=e=>this._client.configureIntegration(e);uploadFile=e=>this._client.uploadFile(e);upsertFile=e=>this._client.upsertFile(e);deleteFile=e=>this._client.deleteFile(e);listFiles=e=>this._client.listFiles(e);getFile=e=>this._client.getFile(e);updateFileMetadata=e=>this._client.updateFileMetadata(e)};var R=class{_cost=0;get cost(){return this._cost}setCost(e){this._cost=e}toJSON(){return{cost:this.cost}}};var fe=require("@bpinternal/zui");var Fe=fe.z.enum(["webhook_received","message_created","action_triggered","register","unregister","ping","create_user","create_conversation"]),me=t=>{let e=t[_],n=t[z],i=t[$],a=t[V],o=t[J],s=t[E],c=Fe.parse(t[S]);if(!e)throw new Error("Missing bot headers");if(!n)throw new Error("Missing bot user headers");if(!i)throw new Error("Missing integration headers");if(!a)throw new Error("Missing webhook headers");if(!s)throw new Error("Missing configuration headers");if(!c)throw new Error("Missing operation headers");return{botId:e,botUserId:n,integrationId:i,webhookId:a,operation:c,configurationType:o??null,configuration:s?JSON.parse(Buffer.from(s,"base64").toString("utf-8")):{}}};var W=Q(require("util")),O=t=>{if(process.env.BP_LOG_FORMAT==="json")return JSON.stringify({msg:W.default.format(...t),visible_to_bot_owner:!0});{let[e,...n]=t;return W.default.format(`[For Bot Owner] ${e}`,...n)}},H={forBot:()=>({info:(...t)=>{console.info(O(t))},warn:(...t)=>{console.warn(O(t))},error:(...t)=>{console.error(O(t))},debug:(...t)=>{console.debug(O(t))}})};var he=t=>async e=>{let n=me(e.headers),i=new b.Client({botId:n.botId,integrationId:n.integrationId,retry:M}),a=new v(i),o={ctx:n,req:e,client:a,logger:H,instance:t};try{let s;switch(n.operation){case"webhook_received":s=await je(o);break;case"register":s=await Ne(o);break;case"unregister":s=await Le(o);break;case"message_created":s=await $e(o);break;case"action_triggered":s=await Ve(o);break;case"ping":s=await Ge(o);break;case"create_user":s=await Ze(o);break;case"create_conversation":s=await ze(o);break;default:throw new Error(`Unknown operation ${n.operation}`)}return s?{...s,status:s.status??200}:{status:200}}catch(s){if((0,b.isApiError)(s)){let p=new b.RuntimeError(s.message,s);return H.forBot().error(p.message),{status:p.code,body:JSON.stringify(p.toJSON())}}console.error(s);let c=new b.RuntimeError("An unexpected error occurred in the integration. Bot owners: Check logs for more informations. Integration owners: throw a RuntimeError to return a custom error message instead.");return H.forBot().error(c.message),{status:c.code,body:JSON.stringify(c.toJSON())}}},Ge=async t=>{},je=async({client:t,ctx:e,req:n,logger:i,instance:a})=>{let{req:o}=f(n);return a.webhook({client:t,ctx:e,req:o,logger:i})},Ne=async({client:t,ctx:e,req:n,logger:i,instance:a})=>{if(!a.register)return;let{webhookUrl:o}=f(n);await a.register({client:t,ctx:e,webhookUrl:o,logger:i})},Le=async({client:t,ctx:e,req:n,logger:i,instance:a})=>{if(!a.unregister)return;let{webhookUrl:o}=f(n);await a.unregister({ctx:e,webhookUrl:o,client:t,logger:i})},Ze=async({client:t,ctx:e,req:n,logger:i,instance:a})=>{if(!a.createUser)return;let{tags:o}=f(n);return await a.createUser({ctx:e,client:t,tags:o,logger:i})},ze=async({client:t,ctx:e,req:n,logger:i,instance:a})=>{if(!a.createConversation)return;let{channel:o,tags:s}=f(n);return await a.createConversation({ctx:e,client:t,channel:o,tags:s,logger:i})},$e=async({ctx:t,req:e,client:n,logger:i,instance:a})=>{let{conversation:o,user:s,type:c,payload:p,message:d}=f(e),u=a.channels[o.channel];if(!u)throw new Error(`Channel ${o.channel} not found`);let k=u.messages[c];if(!k)throw new Error(`Message of type ${c} not found in channel ${o.channel}`);await k({ctx:t,conversation:o,message:d,user:s,type:c,client:n,payload:p,ack:async({tags:ve})=>{await n.updateMessage({id:d.id,tags:ve})},logger:i})},Ve=async({req:t,ctx:e,client:n,logger:i,instance:a})=>{let{input:o,type:s}=f(t);if(!s)throw new Error("Missing action type");let c=a.actions[s];if(!c)throw new Error(`Action ${s} not found`);let p=new R,u={output:await c({ctx:e,input:o,client:n,type:s,logger:i,metadata:p}),meta:p.toJSON()};return{body:JSON.stringify(u)}};var K=class{constructor(e){this.props=e;this.actions=e.actions,this.channels=e.channels,this.register=e.register,this.unregister=e.unregister,this.createUser=e.createUser,this.createConversation=e.createConversation,this.webhook=e.handler}actions;channels;register;unregister;createUser;createConversation;webhook;handler=he(this);start=e=>x(this.handler,e)};var F=class{constructor(e){this.props=e;this.integrations=e.integrations,this.user=e.user,this.conversation=e.conversation,this.message=e.message,this.states=e.states,this.configuration=e.configuration,this.events=e.events,this.recurringEvents=e.recurringEvents,this.actions=e.actions}integrations;user;conversation;message;states;configuration;events;recurringEvents;actions;add(e,n){let i=this;return i.integrations||(i.integrations={}),i.integrations[e.definition.name]={enabled:n.enabled,...e,configurationType:n.configurationType,configuration:n.configuration},this}};var be=Q(require("@botpress/client"));var T=class{constructor(e,n={before:{},after:{}}){this._client=e;this._hooks=n}getConversation=e=>this._run("getConversation",e);listConversations=e=>this._run("listConversations",e);updateConversation=e=>this._run("updateConversation",e);deleteConversation=e=>this._run("deleteConversation",e);listParticipants=e=>this._run("listParticipants",e);addParticipant=e=>this._run("addParticipant",e);getParticipant=e=>this._run("getParticipant",e);removeParticipant=e=>this._run("removeParticipant",e);getEvent=e=>this._run("getEvent",e);listEvents=e=>this._run("listEvents",e);createMessage=e=>this._run("createMessage",e);getOrCreateMessage=e=>this._run("getOrCreateMessage",e);getMessage=e=>this._run("getMessage",e);updateMessage=e=>this._run("updateMessage",e);listMessages=e=>this._run("listMessages",e);deleteMessage=e=>this._run("deleteMessage",e);getUser=e=>this._run("getUser",e);listUsers=e=>this._run("listUsers",e);updateUser=e=>this._run("updateUser",e);deleteUser=e=>this._run("deleteUser",e);getState=e=>this._run("getState",e);setState=e=>this._run("setState",e);getOrSetState=e=>this._run("getOrSetState",e);patchState=e=>this._run("patchState",e);callAction=e=>this._run("callAction",e);uploadFile=e=>this._run("uploadFile",e);upsertFile=e=>this._run("upsertFile",e);deleteFile=e=>this._run("deleteFile",e);listFiles=e=>this._run("listFiles",e);getFile=e=>this._run("getFile",e);updateFileMetadata=e=>this._run("updateFileMetadata",e);searchFiles=e=>this._run("searchFiles",e);createConversation=e=>this._client.createConversation(e);getOrCreateConversation=e=>this._client.getOrCreateConversation(e);createUser=e=>this._client.createUser(e);getOrCreateUser=e=>this._client.getOrCreateUser(e);_run=async(e,n)=>{let i=this._hooks.before[e];i&&(n=await i(n));let a=await this._client[e](n),o=this._hooks.after[e];return o&&(a=await o(a)),a}};var ye=require("@bpinternal/zui");var Je=ye.z.enum(["event_received","register","unregister","ping","action_triggered"]),Te=t=>{let e=t[_],n=t[E],i=t[q],a=Je.parse(t[S]);if(!e)throw new Error("Missing bot headers");if(!i)throw new Error("Missing type headers");if(!n)throw new Error("Missing configuration headers");if(!a)throw new Error("Missing operation headers");return{botId:e,operation:a,type:i,configuration:n?JSON.parse(Buffer.from(n,"base64").toString("utf-8")):{}}};var C={status:200},Be=t=>async e=>{let n=Te(e.headers),i=new be.Client({botId:n.botId,retry:M}),a=new T(i,{before:{createMessage:async s=>{for(let c of t.hooks.before_outgoing_message[s.type]??[])s=(await c({client:new T(i),ctx:n,data:s}))?.data??s;return s},callAction:async s=>{for(let c of t.hooks.before_call_action[s.type]??[])s=(await c({client:new T(i),ctx:n,data:s}))?.data??s;return s}},after:{createMessage:async s=>{for(let c of t.hooks.after_outgoing_message[s.message.type]??[])s=(await c({client:new T(i),ctx:n,data:s}))?.data??s;return s},callAction:async s=>{for(let c of t.hooks.after_call_action[s.output.type]??[])s=(await c({client:new T(i),ctx:n,data:s}))?.data??s;return s}}}),o={req:e,ctx:n,client:a,self:t};switch(n.operation){case"action_triggered":return await Ye(o);case"event_received":return await Xe(o);case"register":return await We(o);case"unregister":return await Qe(o);case"ping":return await qe(o);default:throw new Error(`Unknown operation ${n.operation}`)}},qe=async({ctx:t})=>(B.info(`Received ${t.operation} operation for bot ${t.botId} of type ${t.type}`),C),We=async t=>C,Qe=async t=>C,Xe=async({ctx:t,req:e,client:n,self:i})=>{B.debug(`Received event ${t.type}`);let a=f(e);if(t.type==="message_created"){let c=a.event,p=c.payload.message;for(let u of i.hooks.before_incoming_message[p.type]??[])p=(await u({client:n,ctx:t,data:p}))?.data??p;let d={user:c.payload.user,conversation:c.payload.conversation,states:c.payload.states,message:p,event:c};for(let u of i.messageHandlers)await u({...d,client:n,ctx:t,self:i});for(let u of i.hooks.after_incoming_message[p.type]??[])p=(await u({client:n,ctx:t,data:p}))?.data??p;return C}if(t.type==="state_expired"){let p={state:a.event.payload.state};for(let d of i.stateExpiredHandlers)await d({...p,client:n,ctx:t,self:i});return C}let o=a.event;for(let c of i.hooks.before_incoming_event[o.type]??[])o=(await c({client:n,ctx:t,data:o}))?.data??o;let s={event:o};for(let c of i.eventHandlers)await c({...s,client:n,ctx:t,self:i});for(let c of i.hooks.after_incoming_event[o.type]??[])o=(await c({client:n,ctx:t,data:o}))?.data??o;return C},Ye=async({ctx:t,req:e,client:n,self:i})=>{let{input:a,type:o}=f(e);if(!o)throw new Error("Missing action type");let s=i.actionHandlers[o];if(!s)throw new Error(`Action ${o} not found`);let p={output:await s({ctx:t,input:a,client:n,type:o,self:i})};return{status:200,body:JSON.stringify(p)}};var G=class{constructor(e){this.props=e;this.actionHandlers=e.actions}actionHandlers;messageHandlers=[];eventHandlers=[];stateExpiredHandlers=[];hooks={before_incoming_event:{},before_incoming_message:{},before_outgoing_message:{},before_call_action:{},after_incoming_event:{},after_incoming_message:{},after_outgoing_message:{},after_call_action:{}};message=e=>{this.messageHandlers.push(e)};event=e=>{this.eventHandlers.push(e)};stateExpired=e=>{this.stateExpiredHandlers.push(e)};hook={before_incoming_event:(e,n)=>{this.hooks.before_incoming_event[e]=y.safePush(this.hooks.before_incoming_event[e],n)},before_incoming_message:(e,n)=>{this.hooks.before_incoming_message[e]=y.safePush(this.hooks.before_incoming_message[e],n)},before_outgoing_message:(e,n)=>{this.hooks.before_outgoing_message[e]=y.safePush(this.hooks.before_outgoing_message[e],n)},before_call_action:(e,n)=>{this.hooks.before_call_action[e]=y.safePush(this.hooks.before_call_action[e],n)},after_incoming_event:(e,n)=>{this.hooks.after_incoming_event[e]=y.safePush(this.hooks.after_incoming_event[e],n)},after_incoming_message:(e,n)=>{this.hooks.after_incoming_message[e]=y.safePush(this.hooks.after_incoming_message[e],n)},after_outgoing_message:(e,n)=>{this.hooks.after_outgoing_message[e]=y.safePush(this.hooks.after_outgoing_message[e],n)},after_call_action:(e,n)=>{this.hooks.after_call_action[e]=y.safePush(this.hooks.after_call_action[e],n)}};handler=Be(this);start=e=>x(this.handler,e)};var j=class{constructor(e){this.props=e;this.name=e.name,this.version=e.version,this.entities=e.entities??{},this.templateName=e.templateName;let n=this._getEntityReference(this.entities),i=e.events===void 0?{}:m.mapValues(e.events,s=>({...s,schema:s.schema(n)})),a=e.actions===void 0?{}:m.mapValues(e.actions,s=>({...s,input:{...s.input,schema:s.input.schema(n)},output:{...s.output,schema:s.output.schema(n)}})),o=e.channels===void 0?{}:m.mapValues(e.channels,s=>({...s,messages:m.mapValues(s.messages,c=>({...c,schema:c.schema(n)}))}));this.events=i,this.actions=a,this.channels=o}name;version;entities;events;actions;channels;templateName;_getEntityReference=e=>{let n={};for(let i of Object.keys(e))n[i]=L.ref(i);return n}};0&&(module.exports={Bot,BotDefinition,BotSpecificClient,Integration,IntegrationDefinition,IntegrationSpecificClient,InterfaceDeclaration,RuntimeError,botIdHeader,botUserIdHeader,configurationHeader,configurationTypeHeader,integrationIdHeader,isApiError,messages,operationHeader,parseBody,serve,typeHeader,webhookIdHeader});
|
|
1
|
+
"use strict";var Ie=Object.create;var D=Object.defineProperty;var Ce=Object.getOwnPropertyDescriptor;var _e=Object.getOwnPropertyNames;var Ee=Object.getPrototypeOf,Se=Object.prototype.hasOwnProperty;var C=(t,e)=>{for(var n in e)D(t,n,{get:e[n],enumerable:!0})},P=(t,e,n,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let a of _e(e))!Se.call(t,a)&&a!==n&&D(t,a,{get:()=>e[a],enumerable:!(i=Ce(e,a))||i.enumerable});return t},l=(t,e,n)=>(P(t,e,"default"),n&&P(n,e,"default")),Q=(t,e,n)=>(n=t!=null?Ie(Ee(t)):{},P(e||!t||!t.__esModule?D(n,"default",{value:t,enumerable:!0}):n,t)),xe=t=>P(D({},"__esModule",{value:!0}),t);var h={};C(h,{Bot:()=>G,BotDefinition:()=>F,BotSpecificClient:()=>T,Integration:()=>K,IntegrationDefinition:()=>U,IntegrationSpecificClient:()=>v,InterfaceDeclaration:()=>j,RuntimeError:()=>N.RuntimeError,botIdHeader:()=>_,botUserIdHeader:()=>z,configurationHeader:()=>E,configurationTypeHeader:()=>J,integrationIdHeader:()=>$,isApiError:()=>N.isApiError,messages:()=>Z,operationHeader:()=>S,parseBody:()=>f,serve:()=>x,typeHeader:()=>q,webhookIdHeader:()=>V});module.exports=xe(h);var Z={};C(Z,{defaults:()=>Ae,markdown:()=>we});var r={};C(r,{default:()=>L});var X=require("@bpinternal/zui");l(r,require("@bpinternal/zui"));var L=X.z;var g=r.z.string().min(1),ee=r.z.object({text:g}),te=r.z.object({markdown:g}),ne=r.z.object({imageUrl:g}),se=r.z.object({audioUrl:g}),ie=r.z.object({videoUrl:g}),oe=r.z.object({fileUrl:g,title:g.optional()}),ae=r.z.object({latitude:r.z.number(),longitude:r.z.number(),address:r.z.string().optional(),title:r.z.string().optional()}),re=r.z.object({title:g,subtitle:g.optional(),imageUrl:g.optional(),actions:r.z.array(r.z.object({action:r.z.enum(["postback","url","say"]),label:g,value:g}))}),Y=r.z.object({text:g,options:r.z.array(r.z.object({label:g,value:g}))}),ke=r.z.object({items:r.z.array(re)}),Pe=r.z.union([r.z.object({type:r.z.literal("text"),payload:ee}),r.z.object({type:r.z.literal("markdown"),payload:te}),r.z.object({type:r.z.literal("image"),payload:ne}),r.z.object({type:r.z.literal("audio"),payload:se}),r.z.object({type:r.z.literal("video"),payload:ie}),r.z.object({type:r.z.literal("file"),payload:oe}),r.z.object({type:r.z.literal("location"),payload:ae})]),De=r.z.object({items:r.z.array(Pe)}),we={schema:te},Ae={text:{schema:ee},image:{schema:ne},audio:{schema:se},video:{schema:ie},file:{schema:oe},location:{schema:ae},carousel:{schema:ke},card:{schema:re},dropdown:{schema:Y},choice:{schema:Y},bloc:{schema:De}};var _="x-bot-id",z="x-bot-user-id",$="x-integration-id",V="x-webhook-id",J="x-bp-configuration-type",E="x-bp-configuration",S="x-bp-operation",q="x-bp-type";var ce=require("node:http");var B=console;function f(t){if(!t.body)throw new Error("Missing body");return JSON.parse(t.body)}async function x(t,e=8072,n=Oe){let i=(0,ce.createServer)(async(a,o)=>{try{let s=await Ue(a);if(s.path==="/health"){o.writeHead(200).end("ok");return}let c=await t(s);o.writeHead(c?.status??200,c?.headers??{}).end(c?.body??"{}")}catch(s){B.error("Error while handling request",{error:s?.message??"Internal error occured"}),o.writeHead(500).end(JSON.stringify({error:s?.message??"Internal error occured"}))}});return i.listen(e,()=>n(e)),i}async function Ue(t){let e=await Re(t),n={};for(let a=0;a<t.rawHeaders.length;a+=2){let o=t.rawHeaders[a].toLowerCase(),s=t.rawHeaders[a+1];n[o]=s}let i=new URL(t.url??"",t.headers.host?`http://${t.headers.host}`:"http://botpress.cloud");return{body:e,path:i.pathname,query:Me(i.search,"?"),headers:n,method:t.method?.toUpperCase()??"GET"}}function Me(t,e){return t.indexOf(e)===0?t.slice(e.length):t}async function Re(t){return new Promise((e,n)=>{if(t.method!=="POST"&&t.method!=="PUT"&&t.method!=="PATCH")return e(void 0);let i="";t.on("data",a=>i+=a.toString()),t.on("error",a=>n(a)),t.on("end",()=>e(i))})}function Oe(t){B.info(`Listening on port ${t}`)}l(h,r,module.exports);var N=require("@botpress/client");var m={};C(m,{mapValues:()=>He,pairs:()=>pe});var pe=t=>Object.entries(t),He=(t,e)=>Object.fromEntries(pe(t).map(([n,i])=>[n,e(i,n)]));var y={};C(y,{safePush:()=>Ke});var Ke=(t,e)=>t?[...t,e]:[e];var A=Symbol("schemaName"),le=t=>t?m.mapValues(t,(n,i)=>({...n,[A]:i})):{},ge=t=>A in t&&t[A]!==void 0,de=t=>t[A];var U=class{constructor(e){this.props=e;this.name=e.name,this.version=e.version,this.icon=e.icon,this.readme=e.readme,this.title=e.title,this.identifier=e.identifier,this.description=e.description,this.configuration=e.configuration,this.configurations=e.configurations,this.events=e.events,this.actions=e.actions,this.channels=e.channels,this.states=e.states,this.user=e.user,this.secrets=e.secrets,this.entities=e.entities,this.interfaces=e.interfaces}name;version;title;description;icon;readme;configuration;configurations;events;actions;channels;states;user;secrets;identifier;entities;interfaces;extend(e,n){let i=n(le(this.entities)),a=m.pairs(i).find(([d,u])=>!ge(u));if(a)throw new Error(`Cannot extend interface "${e.definition.name}" with entity "${a[0]}"; the provided schema is not part of the integration's entities.`);let o=this;o.interfaces??={};let s=m.mapValues(i,d=>({name:de(d),schema:d.schema})),c=Object.values(s).map(d=>d.name),p=c.length===0?e.definition.name:`${e.definition.name}<${c.join(",")}>`;return o.interfaces[p]={...e,entities:s},this}};var b=require("@botpress/client");var ue=require("@botpress/client"),M={retries:3,retryCondition:t=>ue.axiosRetry.isNetworkOrIdempotentRequestError(t)||[429,502].includes(t.response?.status??0),retryDelay:t=>t*1e3};var v=class{constructor(e){this._client=e}createConversation=e=>this._client.createConversation(e);getConversation=e=>this._client.getConversation(e);listConversations=e=>this._client.listConversations(e);getOrCreateConversation=e=>this._client.getOrCreateConversation(e);updateConversation=e=>this._client.updateConversation(e);deleteConversation=e=>this._client.deleteConversation(e);listParticipants=e=>this._client.listParticipants(e);addParticipant=e=>this._client.addParticipant(e);getParticipant=e=>this._client.getParticipant(e);removeParticipant=e=>this._client.removeParticipant(e);createEvent=e=>this._client.createEvent(e);getEvent=e=>this._client.getEvent(e);listEvents=e=>this._client.listEvents(e);createMessage=e=>this._client.createMessage(e);getOrCreateMessage=e=>this._client.getOrCreateMessage(e);getMessage=e=>this._client.getMessage(e);updateMessage=e=>this._client.updateMessage(e);listMessages=e=>this._client.listMessages(e);deleteMessage=e=>this._client.deleteMessage(e);createUser=e=>this._client.createUser(e);getUser=e=>this._client.getUser(e);listUsers=e=>this._client.listUsers(e);getOrCreateUser=e=>this._client.getOrCreateUser(e);updateUser=e=>this._client.updateUser(e);deleteUser=e=>this._client.deleteUser(e);getState=e=>this._client.getState(e);setState=e=>this._client.setState(e);getOrSetState=e=>this._client.getOrSetState(e);patchState=e=>this._client.patchState(e);configureIntegration=e=>this._client.configureIntegration(e);uploadFile=e=>this._client.uploadFile(e);upsertFile=e=>this._client.upsertFile(e);deleteFile=e=>this._client.deleteFile(e);listFiles=e=>this._client.listFiles(e);getFile=e=>this._client.getFile(e);updateFileMetadata=e=>this._client.updateFileMetadata(e)};var R=class{_cost=0;get cost(){return this._cost}setCost(e){this._cost=e}toJSON(){return{cost:this.cost}}};var fe=require("@bpinternal/zui");var Fe=fe.z.enum(["webhook_received","message_created","action_triggered","register","unregister","ping","create_user","create_conversation"]),me=t=>{let e=t[_],n=t[z],i=t[$],a=t[V],o=t[J],s=t[E],c=Fe.parse(t[S]);if(!e)throw new Error("Missing bot headers");if(!n)throw new Error("Missing bot user headers");if(!i)throw new Error("Missing integration headers");if(!a)throw new Error("Missing webhook headers");if(!s)throw new Error("Missing configuration headers");if(!c)throw new Error("Missing operation headers");return{botId:e,botUserId:n,integrationId:i,webhookId:a,operation:c,configurationType:o??null,configuration:s?JSON.parse(Buffer.from(s,"base64").toString("utf-8")):{}}};var W=Q(require("util")),O=t=>{if(process.env.BP_LOG_FORMAT==="json")return JSON.stringify({msg:W.default.format(...t),visible_to_bot_owner:!0});{let[e,...n]=t;return W.default.format(`[For Bot Owner] ${e}`,...n)}},H={forBot:()=>({info:(...t)=>{console.info(O(t))},warn:(...t)=>{console.warn(O(t))},error:(...t)=>{console.error(O(t))},debug:(...t)=>{console.debug(O(t))}})};var he=t=>async e=>{let n=me(e.headers),i=new b.Client({botId:n.botId,integrationId:n.integrationId,retry:M}),a=new v(i),o={ctx:n,req:e,client:a,logger:H,instance:t};try{let s;switch(n.operation){case"webhook_received":s=await je(o);break;case"register":s=await Ne(o);break;case"unregister":s=await Le(o);break;case"message_created":s=await $e(o);break;case"action_triggered":s=await Ve(o);break;case"ping":s=await Ge(o);break;case"create_user":s=await Ze(o);break;case"create_conversation":s=await ze(o);break;default:throw new Error(`Unknown operation ${n.operation}`)}return s?{...s,status:s.status??200}:{status:200}}catch(s){if((0,b.isApiError)(s)){let p=new b.RuntimeError(s.message,s);return H.forBot().error(p.message),{status:p.code,body:JSON.stringify(p.toJSON())}}console.error(s);let c=new b.RuntimeError("An unexpected error occurred in the integration. Bot owners: Check logs for more informations. Integration owners: throw a RuntimeError to return a custom error message instead.");return H.forBot().error(c.message),{status:c.code,body:JSON.stringify(c.toJSON())}}},Ge=async t=>{},je=async({client:t,ctx:e,req:n,logger:i,instance:a})=>{let{req:o}=f(n);return a.webhook({client:t,ctx:e,req:o,logger:i})},Ne=async({client:t,ctx:e,req:n,logger:i,instance:a})=>{if(!a.register)return;let{webhookUrl:o}=f(n);await a.register({client:t,ctx:e,webhookUrl:o,logger:i})},Le=async({client:t,ctx:e,req:n,logger:i,instance:a})=>{if(!a.unregister)return;let{webhookUrl:o}=f(n);await a.unregister({ctx:e,webhookUrl:o,client:t,logger:i})},Ze=async({client:t,ctx:e,req:n,logger:i,instance:a})=>{if(!a.createUser)return;let{tags:o}=f(n);return await a.createUser({ctx:e,client:t,tags:o,logger:i})},ze=async({client:t,ctx:e,req:n,logger:i,instance:a})=>{if(!a.createConversation)return;let{channel:o,tags:s}=f(n);return await a.createConversation({ctx:e,client:t,channel:o,tags:s,logger:i})},$e=async({ctx:t,req:e,client:n,logger:i,instance:a})=>{let{conversation:o,user:s,type:c,payload:p,message:d}=f(e),u=a.channels[o.channel];if(!u)throw new Error(`Channel ${o.channel} not found`);let k=u.messages[c];if(!k)throw new Error(`Message of type ${c} not found in channel ${o.channel}`);await k({ctx:t,conversation:o,message:d,user:s,type:c,client:n,payload:p,ack:async({tags:ve})=>{await n.updateMessage({id:d.id,tags:ve})},logger:i})},Ve=async({req:t,ctx:e,client:n,logger:i,instance:a})=>{let{input:o,type:s}=f(t);if(!s)throw new Error("Missing action type");let c=a.actions[s];if(!c)throw new Error(`Action ${s} not found`);let p=new R,u={output:await c({ctx:e,input:o,client:n,type:s,logger:i,metadata:p}),meta:p.toJSON()};return{body:JSON.stringify(u)}};var K=class{constructor(e){this.props=e;this.actions=e.actions,this.channels=e.channels,this.register=e.register,this.unregister=e.unregister,this.createUser=e.createUser,this.createConversation=e.createConversation,this.webhook=e.handler}actions;channels;register;unregister;createUser;createConversation;webhook;handler=he(this);start=e=>x(this.handler,e)};var F=class{constructor(e){this.props=e;this.integrations=e.integrations,this.user=e.user,this.conversation=e.conversation,this.message=e.message,this.states=e.states,this.configuration=e.configuration,this.events=e.events,this.recurringEvents=e.recurringEvents,this.actions=e.actions}integrations;user;conversation;message;states;configuration;events;recurringEvents;actions;add(e,n){let i=this;return i.integrations||(i.integrations={}),i.integrations[e.definition.name]={enabled:n.enabled,...e,configurationType:n.configurationType,configuration:n.configuration},this}};var be=Q(require("@botpress/client"));var T=class{constructor(e,n={before:{},after:{}}){this._client=e;this._hooks=n}getConversation=e=>this._run("getConversation",e);listConversations=e=>this._run("listConversations",e);updateConversation=e=>this._run("updateConversation",e);deleteConversation=e=>this._run("deleteConversation",e);listParticipants=e=>this._run("listParticipants",e);addParticipant=e=>this._run("addParticipant",e);getParticipant=e=>this._run("getParticipant",e);removeParticipant=e=>this._run("removeParticipant",e);getEvent=e=>this._run("getEvent",e);listEvents=e=>this._run("listEvents",e);createMessage=e=>this._run("createMessage",e);getOrCreateMessage=e=>this._run("getOrCreateMessage",e);getMessage=e=>this._run("getMessage",e);updateMessage=e=>this._run("updateMessage",e);listMessages=e=>this._run("listMessages",e);deleteMessage=e=>this._run("deleteMessage",e);getUser=e=>this._run("getUser",e);listUsers=e=>this._run("listUsers",e);updateUser=e=>this._run("updateUser",e);deleteUser=e=>this._run("deleteUser",e);getState=e=>this._run("getState",e);setState=e=>this._run("setState",e);getOrSetState=e=>this._run("getOrSetState",e);patchState=e=>this._run("patchState",e);callAction=e=>this._run("callAction",e);uploadFile=e=>this._run("uploadFile",e);upsertFile=e=>this._run("upsertFile",e);deleteFile=e=>this._run("deleteFile",e);listFiles=e=>this._run("listFiles",e);getFile=e=>this._run("getFile",e);updateFileMetadata=e=>this._run("updateFileMetadata",e);searchFiles=e=>this._run("searchFiles",e);createConversation=e=>this._client.createConversation(e);getOrCreateConversation=e=>this._client.getOrCreateConversation(e);createUser=e=>this._client.createUser(e);getOrCreateUser=e=>this._client.getOrCreateUser(e);_run=async(e,n)=>{let i=this._hooks.before[e];i&&(n=await i(n));let a=await this._client[e](n),o=this._hooks.after[e];return o&&(a=await o(a)),a}};var ye=require("@bpinternal/zui");var Je=ye.z.enum(["event_received","register","unregister","ping","action_triggered"]),Te=t=>{let e=t[_],n=t[E],i=t[q],a=Je.parse(t[S]);if(!e)throw new Error("Missing bot headers");if(!i)throw new Error("Missing type headers");if(!n)throw new Error("Missing configuration headers");if(!a)throw new Error("Missing operation headers");return{botId:e,operation:a,type:i,configuration:n?JSON.parse(Buffer.from(n,"base64").toString("utf-8")):{}}};var I={status:200},Be=t=>async e=>{let n=Te(e.headers),i=new be.Client({botId:n.botId,retry:M}),a=new T(i,{before:{createMessage:async s=>{for(let c of t.hooks.before_outgoing_message[s.type]??[])s=(await c({client:new T(i),ctx:n,data:s}))?.data??s;return s},callAction:async s=>{for(let c of t.hooks.before_call_action[s.type]??[])s=(await c({client:new T(i),ctx:n,data:s}))?.data??s;return s}},after:{createMessage:async s=>{for(let c of t.hooks.after_outgoing_message[s.message.type]??[])s=(await c({client:new T(i),ctx:n,data:s}))?.data??s;return s},callAction:async s=>{for(let c of t.hooks.after_call_action[s.output.type]??[])s=(await c({client:new T(i),ctx:n,data:s}))?.data??s;return s}}}),o={req:e,ctx:n,client:a,self:t};switch(n.operation){case"action_triggered":return await Ye(o);case"event_received":return await Xe(o);case"register":return await We(o);case"unregister":return await Qe(o);case"ping":return await qe(o);default:throw new Error(`Unknown operation ${n.operation}`)}},qe=async({ctx:t})=>(B.info(`Received ${t.operation} operation for bot ${t.botId} of type ${t.type}`),I),We=async t=>I,Qe=async t=>I,Xe=async({ctx:t,req:e,client:n,self:i})=>{B.debug(`Received event ${t.type}`);let a=f(e);if(t.type==="message_created"){let c=a.event,p=c.payload.message;for(let u of i.hooks.before_incoming_message[p.type]??[])p=(await u({client:n,ctx:t,data:p}))?.data??p;let d={user:c.payload.user,conversation:c.payload.conversation,states:c.payload.states,message:p,event:c};for(let u of i.messageHandlers)await u({...d,client:n,ctx:t,self:i});for(let u of i.hooks.after_incoming_message[p.type]??[])p=(await u({client:n,ctx:t,data:p}))?.data??p;return I}if(t.type==="state_expired"){let p={state:a.event.payload.state};for(let d of i.stateExpiredHandlers)await d({...p,client:n,ctx:t,self:i});return I}let o=a.event;for(let c of i.hooks.before_incoming_event[o.type]??[])o=(await c({client:n,ctx:t,data:o}))?.data??o;let s={event:o};for(let c of i.eventHandlers)await c({...s,client:n,ctx:t,self:i});for(let c of i.hooks.after_incoming_event[o.type]??[])o=(await c({client:n,ctx:t,data:o}))?.data??o;return I},Ye=async({ctx:t,req:e,client:n,self:i})=>{let{input:a,type:o}=f(e);if(!o)throw new Error("Missing action type");let s=i.actionHandlers[o];if(!s)throw new Error(`Action ${o} not found`);let p={output:await s({ctx:t,input:a,client:n,type:o,self:i})};return{status:200,body:JSON.stringify(p)}};var G=class{constructor(e){this.props=e;this.actionHandlers=e.actions}actionHandlers;messageHandlers=[];eventHandlers=[];stateExpiredHandlers=[];hooks={before_incoming_event:{},before_incoming_message:{},before_outgoing_message:{},before_call_action:{},after_incoming_event:{},after_incoming_message:{},after_outgoing_message:{},after_call_action:{}};message=e=>{this.messageHandlers.push(e)};event=e=>{this.eventHandlers.push(e)};stateExpired=e=>{this.stateExpiredHandlers.push(e)};hook={before_incoming_event:(e,n)=>{this.hooks.before_incoming_event[e]=y.safePush(this.hooks.before_incoming_event[e],n)},before_incoming_message:(e,n)=>{this.hooks.before_incoming_message[e]=y.safePush(this.hooks.before_incoming_message[e],n)},before_outgoing_message:(e,n)=>{this.hooks.before_outgoing_message[e]=y.safePush(this.hooks.before_outgoing_message[e],n)},before_call_action:(e,n)=>{this.hooks.before_call_action[e]=y.safePush(this.hooks.before_call_action[e],n)},after_incoming_event:(e,n)=>{this.hooks.after_incoming_event[e]=y.safePush(this.hooks.after_incoming_event[e],n)},after_incoming_message:(e,n)=>{this.hooks.after_incoming_message[e]=y.safePush(this.hooks.after_incoming_message[e],n)},after_outgoing_message:(e,n)=>{this.hooks.after_outgoing_message[e]=y.safePush(this.hooks.after_outgoing_message[e],n)},after_call_action:(e,n)=>{this.hooks.after_call_action[e]=y.safePush(this.hooks.after_call_action[e],n)}};handler=Be(this);start=e=>x(this.handler,e)};var j=class{constructor(e){this.props=e;this.name=e.name,this.version=e.version,this.entities=e.entities??{},this.templateName=e.templateName;let n=this._getEntityReference(this.entities),i=e.events===void 0?{}:m.mapValues(e.events,s=>({...s,schema:s.schema(n)})),a=e.actions===void 0?{}:m.mapValues(e.actions,s=>({...s,input:{...s.input,schema:s.input.schema(n)},output:{...s.output,schema:s.output.schema(n)}})),o=e.channels===void 0?{}:m.mapValues(e.channels,s=>({...s,messages:m.mapValues(s.messages,c=>({...c,schema:c.schema(n)}))}));this.events=i,this.actions=a,this.channels=o}name;version;entities;events;actions;channels;templateName;_getEntityReference=e=>{let n={};for(let i of Object.keys(e))n[i]=L.ref(i);return n}};0&&(module.exports={Bot,BotDefinition,BotSpecificClient,Integration,IntegrationDefinition,IntegrationSpecificClient,InterfaceDeclaration,RuntimeError,botIdHeader,botUserIdHeader,configurationHeader,configurationTypeHeader,integrationIdHeader,isApiError,messages,operationHeader,parseBody,serve,typeHeader,webhookIdHeader});
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/index.ts", "../src/message.ts", "../src/zui.ts", "../src/const.ts", "../src/serve.ts", "../src/log.ts", "../src/utils/record-utils.ts", "../src/utils/array-utils.ts", "../src/integration/definition/branded-schema.ts", "../src/integration/definition/index.ts", "../src/integration/server/index.ts", "../src/retry.ts", "../src/integration/client/index.ts", "../src/integration/server/action-metadata.ts", "../src/integration/server/context.ts", "../src/integration/server/logger.ts", "../src/integration/implementation.ts", "../src/bot/definition.ts", "../src/bot/server/index.ts", "../src/bot/client/index.ts", "../src/bot/server/context.ts", "../src/bot/implementation.ts", "../src/interface/definition.ts"],
|
|
4
|
-
"sourcesContent": ["export * as messages from './message'\nexport * from './const'\nexport * from './serve'\nexport * from './zui'\n\nexport {\n //\n isApiError,\n RuntimeError,\n} from '@botpress/client'\n\nexport {\n IntegrationDefinition,\n IntegrationDefinitionProps,\n IntegrationImplementation as Integration,\n IntegrationImplementationProps as IntegrationProps,\n IntegrationLogger,\n IntegrationSpecificClient,\n TagDefinition,\n ConfigurationDefinition,\n AdditionalConfigurationDefinition,\n EventDefinition,\n ChannelDefinition,\n MessageDefinition,\n ActionDefinition,\n StateDefinition,\n UserDefinition,\n SecretDefinition,\n EntityDefinition,\n} from './integration'\n\nexport {\n /**\n * @deprecated use Context exported from '.botpress' instead\n */\n IntegrationContext,\n} from './integration/server'\n\nexport {\n BotDefinition,\n BotDefinitionProps,\n BotImplementation as Bot,\n BotImplementationProps as BotProps,\n BotSpecificClient,\n TagDefinition as BotTagDefinition,\n StateType as BotStateType,\n StateDefinition as BotStateDefinition,\n RecurringEventDefinition as BotRecurringEventDefinition,\n EventDefinition as BotEventDefinition,\n ConfigurationDefinition as BotConfigurationDefinition,\n UserDefinition as BotUserDefinition,\n ConversationDefinition as BotConversationDefinition,\n MessageDefinition as BotMessageDefinition,\n ActionDefinition as BotActionDefinition,\n} from './bot'\n\nexport {\n //\n InterfaceDeclaration,\n InterfaceDeclarationProps,\n} from './interface'\n\nexport {\n //\n IntegrationPackage,\n InterfacePackage,\n} from './package'\n", "import { z } from './zui'\n\nconst NonEmptyString = z.string().min(1)\n\nconst textMessageSchema = z.object({\n text: NonEmptyString,\n})\n\nconst markdownMessageSchema = z.object({\n markdown: NonEmptyString,\n})\n\nconst imageMessageSchema = z.object({\n imageUrl: NonEmptyString,\n})\n\nconst audioMessageSchema = z.object({\n audioUrl: NonEmptyString,\n})\n\nconst videoMessageSchema = z.object({\n videoUrl: NonEmptyString,\n})\n\nconst fileMessageSchema = z.object({\n fileUrl: NonEmptyString,\n title: NonEmptyString.optional(),\n})\n\nconst locationMessageSchema = z.object({\n latitude: z.number(),\n longitude: z.number(),\n address: z.string().optional(),\n title: z.string().optional(),\n})\n\nconst cardSchema = z.object({\n title: NonEmptyString,\n subtitle: NonEmptyString.optional(),\n imageUrl: NonEmptyString.optional(),\n actions: z.array(\n z.object({\n action: z.enum(['postback', 'url', 'say']),\n label: NonEmptyString,\n value: NonEmptyString,\n })\n ),\n})\n\nconst choiceSchema = z.object({\n text: NonEmptyString,\n options: z.array(\n z.object({\n label: NonEmptyString,\n value: NonEmptyString,\n })\n ),\n})\n\nconst carouselSchema = z.object({\n items: z.array(cardSchema),\n})\n\nconst blocSchema = z.union([\n z.object({ type: z.literal('text'), payload: textMessageSchema }),\n z.object({ type: z.literal('markdown'), payload: markdownMessageSchema }),\n z.object({ type: z.literal('image'), payload: imageMessageSchema }),\n z.object({ type: z.literal('audio'), payload: audioMessageSchema }),\n z.object({ type: z.literal('video'), payload: videoMessageSchema }),\n z.object({ type: z.literal('file'), payload: fileMessageSchema }),\n z.object({ type: z.literal('location'), payload: locationMessageSchema }),\n])\n\nconst blocsSchema = z.object({\n items: z.array(blocSchema),\n})\n\n/**\n * @deprecated use `text` instead\n */\nexport const markdown = { schema: markdownMessageSchema }\nexport const defaults = {\n text: { schema: textMessageSchema },\n image: { schema: imageMessageSchema },\n audio: { schema: audioMessageSchema },\n video: { schema: videoMessageSchema },\n file: { schema: fileMessageSchema },\n location: { schema: locationMessageSchema },\n carousel: { schema: carouselSchema },\n card: { schema: cardSchema },\n dropdown: { schema: choiceSchema },\n choice: { schema: choiceSchema },\n bloc: { schema: blocsSchema },\n} as const // should use satisfies operator but this works for older versions of TS\n", "import { z } from '@bpinternal/zui'\nexport * from '@bpinternal/zui'\n\nexport type GenericZuiSchema<\n A extends Record<string, z.ZodTypeAny> = Record<string, z.ZodTypeAny>,\n R extends z.ZodTypeAny = z.ZodTypeAny\n> = (typeArguments: A) => R\n\nexport default z\n", "export const botIdHeader = 'x-bot-id'\nexport const botUserIdHeader = 'x-bot-user-id'\nexport const integrationIdHeader = 'x-integration-id'\nexport const webhookIdHeader = 'x-webhook-id'\n\nexport const configurationTypeHeader = 'x-bp-configuration-type'\nexport const configurationHeader = 'x-bp-configuration'\nexport const operationHeader = 'x-bp-operation'\nexport const typeHeader = 'x-bp-type'\n", "import { createServer, IncomingMessage, Server } from 'node:http'\nimport { log } from './log'\n\nexport type Request = {\n body?: string\n path: string\n query: string\n method: string\n headers: { [key: string]: string | undefined }\n}\n\nexport type Response = {\n body?: string\n headers?: { [key: string]: string }\n status?: number\n}\n\nexport type Handler = (req: Request) => Promise<Response | void>\n\nexport function parseBody<T>(req: Request): T {\n if (!req.body) {\n throw new Error('Missing body')\n }\n return JSON.parse(req.body)\n}\n\nexport async function serve(\n handler: Handler,\n port: number = 8072,\n callback: (port: number) => void = defaultCallback\n): Promise<Server> {\n /* eslint-disable @typescript-eslint/no-misused-promises */\n const server = createServer(async (req, res) => {\n try {\n const request = await mapIncomingMessageToRequest(req)\n if (request.path === '/health') {\n res.writeHead(200).end('ok')\n return\n }\n const response = await handler(request)\n res.writeHead(response?.status ?? 200, response?.headers ?? {}).end(response?.body ?? '{}')\n } catch (e: any) {\n log.error('Error while handling request', { error: e?.message ?? 'Internal error occured' })\n res.writeHead(500).end(JSON.stringify({ error: e?.message ?? 'Internal error occured' }))\n }\n })\n\n server.listen(port, () => callback(port))\n return server\n}\n\nasync function mapIncomingMessageToRequest(incoming: IncomingMessage): Promise<Request> {\n const body = await readBody(incoming)\n const headers = {} as Request['headers']\n\n for (let i = 0; i < incoming.rawHeaders.length; i += 2) {\n const key = incoming.rawHeaders[i]!.toLowerCase()\n const value = incoming.rawHeaders[i + 1]!\n headers[key] = value\n }\n\n const url = new URL(\n incoming.url ?? '',\n incoming.headers.host ? `http://${incoming.headers.host}` : 'http://botpress.cloud'\n )\n\n return {\n body,\n path: url.pathname,\n query: trimPrefix(url.search, '?'),\n headers,\n method: incoming.method?.toUpperCase() ?? 'GET',\n }\n}\n\nfunction trimPrefix(value: string, prefix: string) {\n return value.indexOf(prefix) === 0 ? value.slice(prefix.length) : value\n}\n\nasync function readBody(incoming: IncomingMessage) {\n return new Promise<string | undefined>((resolve, reject) => {\n if (incoming.method !== 'POST' && incoming.method !== 'PUT' && incoming.method !== 'PATCH') {\n return resolve(undefined)\n }\n\n let body = ''\n\n incoming.on('data', (chunk) => (body += chunk.toString()))\n incoming.on('error', (e) => reject(e))\n incoming.on('end', () => resolve(body))\n })\n}\n\nfunction defaultCallback(port: number) {\n log.info(`Listening on port ${port}`)\n}\n", "export type Logger = {\n debug(message: string, metadata?: any): void\n info(message: string, metadata?: any): void\n warn(message: string, metadata?: any): void\n error(message: string, metadata?: any): void\n}\nexport const log: Logger = console\n", "export const pairs = <K extends string, V>(obj: Record<K, V>) => Object.entries(obj) as [K, V][]\nexport const mapValues = <K extends string, V, R>(obj: Record<K, V>, fn: (value: V, key: K) => R): Record<K, R> =>\n Object.fromEntries(pairs(obj).map(([key, value]) => [key, fn(value, key)])) as Record<K, R>\n", "export const safePush = <T>(arr: T[] | undefined, value: T): T[] => (arr ? [...arr, value] : [value])\n", "import * as utils from '../../utils'\nimport { z } from '../../zui'\n\nconst schemaName = Symbol('schemaName')\n\ntype BaseSchemas = Record<string, z.ZodSchema>\n\nexport type SchemaStoreProps<TSchemas extends BaseSchemas = BaseSchemas> = {\n [K in keyof TSchemas]: {\n schema: TSchemas[K]\n }\n}\n\nexport type BrandedSchema<TSchema extends BaseSchemas[string] = BaseSchemas[string]> = {\n schema: TSchema\n [schemaName]: string\n}\n\nexport type SchemaStore<TSchemas extends BaseSchemas = BaseSchemas> = {\n [K in keyof TSchemas]: BrandedSchema<TSchemas[K]>\n}\n\nexport const createStore = <TSchemas extends BaseSchemas>(\n props: SchemaStoreProps<TSchemas> | undefined\n): SchemaStore<TSchemas> => {\n if (!props) {\n return {} as SchemaStore<TSchemas>\n }\n const store: SchemaStore<BaseSchemas> = utils.records.mapValues(props, (e, k) => ({ ...e, [schemaName]: k }))\n return store as SchemaStore<TSchemas>\n}\n\nexport const isBranded = (schema: BrandedSchema | { schema: z.ZodSchema }): schema is BrandedSchema => {\n return schemaName in schema && schema[schemaName] !== undefined\n}\n\nexport const getName = (schema: BrandedSchema): string => {\n return schema[schemaName]\n}\n", "import { InterfacePackage } from '../../package'\nimport * as utils from '../../utils'\nimport { z } from '../../zui'\nimport { SchemaStore, BrandedSchema, createStore, isBranded, getName } from './branded-schema'\nimport { BaseConfig, BaseEvents, BaseActions, BaseChannels, BaseStates, BaseEntities, BaseConfigs } from './generic'\nimport {\n ConfigurationDefinition,\n EventDefinition,\n ChannelDefinition,\n ActionDefinition,\n StateDefinition,\n UserDefinition,\n SecretDefinition,\n EntityDefinition,\n AdditionalConfigurationDefinition,\n} from './types'\n\nexport * from './types'\n\nexport type InterfaceInstance = InterfacePackage & {\n entities: Record<\n string,\n {\n name: string\n schema: z.AnyZodObject\n }\n >\n}\n\nexport type IntegrationDefinitionProps<\n TConfig extends BaseConfig = BaseConfig,\n TConfigs extends BaseConfigs = BaseConfigs,\n TEvents extends BaseEvents = BaseEvents,\n TActions extends BaseActions = BaseActions,\n TChannels extends BaseChannels = BaseChannels,\n TStates extends BaseStates = BaseStates,\n TEntities extends BaseEntities = BaseEntities\n> = {\n name: string\n version: string\n\n title?: string\n description?: string\n icon?: string\n readme?: string\n\n identifier?: {\n extractScript?: string\n fallbackHandlerScript?: string\n }\n\n configuration?: ConfigurationDefinition<TConfig>\n configurations?: {\n [K in keyof TConfigs]: AdditionalConfigurationDefinition<TConfigs[K]>\n }\n\n events?: { [K in keyof TEvents]: EventDefinition<TEvents[K]> }\n\n actions?: {\n [K in keyof TActions]: ActionDefinition<TActions[K]>\n }\n\n channels?: {\n [K in keyof TChannels]: ChannelDefinition<TChannels[K]>\n }\n\n states?: {\n [K in keyof TStates]: StateDefinition<TStates[K]>\n }\n\n user?: UserDefinition\n\n secrets?: Record<string, SecretDefinition>\n\n entities?: {\n [K in keyof TEntities]: EntityDefinition<TEntities[K]>\n }\n\n interfaces?: Record<string, InterfaceInstance>\n}\n\ntype EntitiesOfPackage<TPackage extends InterfacePackage> = {\n [K in keyof TPackage['definition']['entities']]: NonNullable<TPackage['definition']['entities']>[K]['schema']\n}\n\ntype ExtensionBuilderInput<TIntegrationEntities extends BaseEntities> = SchemaStore<TIntegrationEntities>\n\ntype ExtensionBuilderOutput<TInterfaceEntities extends BaseEntities> = {\n [K in keyof TInterfaceEntities]: BrandedSchema<z.ZodSchema<z.infer<TInterfaceEntities[K]>>>\n}\n\ntype ExtensionBuilder<TIntegrationEntities extends BaseEntities, TInterfaceEntities extends BaseEntities> = (\n input: ExtensionBuilderInput<TIntegrationEntities>\n) => ExtensionBuilderOutput<TInterfaceEntities>\n\nexport class IntegrationDefinition<\n TConfig extends BaseConfig = BaseConfig,\n TConfigs extends BaseConfigs = BaseConfigs,\n TEvents extends BaseEvents = BaseEvents,\n TActions extends BaseActions = BaseActions,\n TChannels extends BaseChannels = BaseChannels,\n TStates extends BaseStates = BaseStates,\n TEntities extends BaseEntities = BaseEntities\n> {\n public readonly name: this['props']['name']\n public readonly version: this['props']['version']\n public readonly title: this['props']['title']\n public readonly description: this['props']['description']\n public readonly icon: this['props']['icon']\n public readonly readme: this['props']['readme']\n public readonly configuration: this['props']['configuration']\n public readonly configurations: this['props']['configurations']\n public readonly events: this['props']['events']\n public readonly actions: this['props']['actions']\n public readonly channels: this['props']['channels']\n public readonly states: this['props']['states']\n public readonly user: this['props']['user']\n public readonly secrets: this['props']['secrets']\n public readonly identifier: this['props']['identifier']\n public readonly entities: this['props']['entities']\n public readonly interfaces: this['props']['interfaces']\n public constructor(\n public readonly props: IntegrationDefinitionProps<\n TConfig,\n TConfigs,\n TEvents,\n TActions,\n TChannels,\n TStates,\n TEntities\n >\n ) {\n this.name = props.name\n this.version = props.version\n this.icon = props.icon\n this.readme = props.readme\n this.title = props.title\n this.identifier = props.identifier\n this.description = props.description\n this.configuration = props.configuration\n this.configurations = props.configurations\n this.events = props.events\n this.actions = props.actions\n this.channels = props.channels\n this.states = props.states\n this.user = props.user\n this.secrets = props.secrets\n this.entities = props.entities\n this.interfaces = props.interfaces\n }\n\n public extend<P extends InterfacePackage>(\n interfacePkg: P,\n builder: ExtensionBuilder<TEntities, EntitiesOfPackage<P>>\n ): this {\n const extensionBuilderOutput = builder(createStore(this.entities))\n const unbrandedEntity = utils.records.pairs(extensionBuilderOutput).find(([_k, e]) => !isBranded(e))\n if (unbrandedEntity) {\n // this means the user tried providing a plain schema without referencing an entity from the integration\n throw new Error(\n `Cannot extend interface \"${interfacePkg.definition.name}\" with entity \"${unbrandedEntity[0]}\"; the provided schema is not part of the integration's entities.`\n )\n }\n\n const self = this as utils.types.Writable<IntegrationDefinition>\n self.interfaces ??= {}\n\n const interfaceTypeArguments = utils.records.mapValues(extensionBuilderOutput, (e) => ({\n name: getName(e),\n schema: e.schema as z.AnyZodObject,\n }))\n\n const entityNames = Object.values(interfaceTypeArguments).map((e) => e.name)\n\n const key =\n entityNames.length === 0\n ? interfacePkg.definition.name\n : `${interfacePkg.definition.name}<${entityNames.join(',')}>`\n\n self.interfaces[key] = {\n ...interfacePkg,\n entities: interfaceTypeArguments,\n }\n\n return this\n }\n}\n", "import { isApiError, Client, RuntimeError } from '@botpress/client'\nimport { retryConfig } from '../../retry'\nimport { Request, Response, parseBody } from '../../serve'\nimport { IntegrationSpecificClient } from '../client'\nimport { BaseIntegration } from '../types'\nimport { ActionMetadataStore } from './action-metadata'\nimport { extractContext } from './context'\nimport { integrationLogger } from './logger'\nimport {\n CommonHandlerProps,\n IntegrationHandlers,\n WebhookPayload,\n ActionPayload,\n MessagePayload,\n RegisterPayload,\n CreateUserPayload,\n UnregisterPayload,\n CreateConversationPayload,\n} from './types'\n\nexport * from './types'\nexport * from './logger'\n\ntype ServerProps = CommonHandlerProps<BaseIntegration> & {\n req: Request\n instance: IntegrationHandlers<BaseIntegration>\n}\n\nexport const integrationHandler =\n (instance: IntegrationHandlers<BaseIntegration>) =>\n async (req: Request): Promise<Response | void> => {\n const ctx = extractContext(req.headers)\n\n const vanillaClient = new Client({\n botId: ctx.botId,\n integrationId: ctx.integrationId,\n retry: retryConfig,\n })\n const client = new IntegrationSpecificClient<BaseIntegration>(vanillaClient)\n\n const props = {\n ctx,\n req,\n client,\n logger: integrationLogger,\n instance,\n }\n\n try {\n let response: Response | void\n switch (ctx.operation) {\n case 'webhook_received':\n response = await onWebhook(props)\n break\n case 'register':\n response = await onRegister(props)\n break\n case 'unregister':\n response = await onUnregister(props)\n break\n case 'message_created':\n response = await onMessageCreated(props)\n break\n case 'action_triggered':\n response = await onActionTriggered(props)\n break\n case 'ping':\n response = await onPing(props)\n break\n case 'create_user':\n response = await onCreateUser(props)\n break\n case 'create_conversation':\n response = await onCreateConversation(props)\n break\n default:\n throw new Error(`Unknown operation ${ctx.operation}`)\n }\n return response ? { ...response, status: response.status ?? 200 } : { status: 200 }\n } catch (thrown) {\n if (isApiError(thrown)) {\n const runtimeError = new RuntimeError(thrown.message, thrown)\n integrationLogger.forBot().error(runtimeError.message)\n\n return { status: runtimeError.code, body: JSON.stringify(runtimeError.toJSON()) }\n }\n\n // prints the error in the integration logs\n console.error(thrown)\n\n const runtimeError = new RuntimeError(\n 'An unexpected error occurred in the integration. Bot owners: Check logs for more informations. Integration owners: throw a RuntimeError to return a custom error message instead.'\n )\n integrationLogger.forBot().error(runtimeError.message)\n return { status: runtimeError.code, body: JSON.stringify(runtimeError.toJSON()) }\n }\n }\n\nconst onPing = async (_: ServerProps) => {}\n\nconst onWebhook = async ({ client, ctx, req: incomingRequest, logger, instance }: ServerProps) => {\n const { req } = parseBody<WebhookPayload>(incomingRequest)\n return instance.webhook({ client, ctx, req, logger })\n}\n\nconst onRegister = async ({ client, ctx, req, logger, instance }: ServerProps) => {\n if (!instance.register) {\n return\n }\n const { webhookUrl } = parseBody<RegisterPayload>(req)\n await instance.register({ client, ctx, webhookUrl, logger })\n}\n\nconst onUnregister = async ({ client, ctx, req, logger, instance }: ServerProps) => {\n if (!instance.unregister) {\n return\n }\n const { webhookUrl } = parseBody<UnregisterPayload>(req)\n await instance.unregister({ ctx, webhookUrl, client, logger })\n}\n\nconst onCreateUser = async ({ client, ctx, req, logger, instance }: ServerProps) => {\n if (!instance.createUser) {\n return\n }\n const { tags } = parseBody<CreateUserPayload<BaseIntegration>>(req)\n return await instance.createUser({ ctx, client, tags, logger })\n}\n\nconst onCreateConversation = async ({ client, ctx, req, logger, instance }: ServerProps) => {\n if (!instance.createConversation) {\n return\n }\n const { channel, tags } = parseBody<CreateConversationPayload<BaseIntegration>>(req)\n return await instance.createConversation({ ctx, client, channel, tags, logger })\n}\n\nconst onMessageCreated = async ({ ctx, req, client, logger, instance }: ServerProps) => {\n const { conversation, user, type, payload, message } = parseBody<MessagePayload<BaseIntegration, string, string>>(req)\n\n const channelHandler = instance.channels[conversation.channel]\n\n if (!channelHandler) {\n throw new Error(`Channel ${conversation.channel} not found`)\n }\n\n const messageHandler = channelHandler.messages[type]\n\n if (!messageHandler) {\n throw new Error(`Message of type ${type} not found in channel ${conversation.channel}`)\n }\n\n type UpdateMessageProps = Parameters<(typeof client)['updateMessage']>[0]\n const ack = async ({ tags }: Pick<UpdateMessageProps, 'tags'>) => {\n await client.updateMessage({\n id: message.id,\n tags,\n })\n }\n\n await messageHandler({ ctx, conversation, message, user, type, client, payload, ack, logger })\n}\n\nconst onActionTriggered = async ({ req, ctx, client, logger, instance }: ServerProps) => {\n const { input, type } = parseBody<ActionPayload<string, any>>(req)\n\n if (!type) {\n throw new Error('Missing action type')\n }\n\n const action = instance.actions[type]\n\n if (!action) {\n throw new Error(`Action ${type} not found`)\n }\n\n const metadata = new ActionMetadataStore()\n const output = await action({ ctx, input, client, type, logger, metadata })\n\n const response = { output, meta: metadata.toJSON() }\n return {\n body: JSON.stringify(response),\n }\n}\n", "import { RetryConfig, axiosRetry } from '@botpress/client'\n\nexport const retryConfig: RetryConfig = {\n retries: 3,\n retryCondition: (err) =>\n axiosRetry.isNetworkOrIdempotentRequestError(err) || [429, 502].includes(err.response?.status ?? 0),\n retryDelay: (retryCount) => retryCount * 1000,\n}\n", "/* eslint-disable brace-style */\nimport * as client from '@botpress/client'\nimport * as common from '../types'\nimport * as types from './types'\n\nexport * from './types'\n\n/**\n * Just like the regular botpress client, but typed with the integration's properties.\n */\nexport class IntegrationSpecificClient<TIntegration extends common.BaseIntegration>\n implements types.ClientOperations<TIntegration>\n{\n public constructor(private readonly _client: client.Client) {}\n\n public createConversation: types.CreateConversation<TIntegration> = ((x) =>\n this._client.createConversation(x)) as types.CreateConversation<TIntegration>\n public getConversation: types.GetConversation<TIntegration> = ((x) =>\n this._client.getConversation(x)) as types.GetConversation<TIntegration>\n public listConversations: types.ListConversations<TIntegration> = ((x) =>\n this._client.listConversations(x)) as types.ListConversations<TIntegration>\n public getOrCreateConversation: types.GetOrCreateConversation<TIntegration> = ((x) =>\n this._client.getOrCreateConversation(x)) as types.GetOrCreateConversation<TIntegration>\n public updateConversation: types.UpdateConversation<TIntegration> = ((x) =>\n this._client.updateConversation(x)) as types.UpdateConversation<TIntegration>\n public deleteConversation: types.DeleteConversation<TIntegration> = ((x) =>\n this._client.deleteConversation(x)) as types.DeleteConversation<TIntegration>\n\n public listParticipants: types.ListParticipants<TIntegration> = ((x) =>\n this._client.listParticipants(x)) as types.ListParticipants<TIntegration>\n public addParticipant: types.AddParticipant<TIntegration> = ((x) =>\n this._client.addParticipant(x)) as types.AddParticipant<TIntegration>\n public getParticipant: types.GetParticipant<TIntegration> = ((x) =>\n this._client.getParticipant(x)) as types.GetParticipant<TIntegration>\n public removeParticipant: types.RemoveParticipant<TIntegration> = ((x) =>\n this._client.removeParticipant(x)) as types.RemoveParticipant<TIntegration>\n\n public createEvent: types.CreateEvent<TIntegration> = ((x) =>\n this._client.createEvent(x)) as types.CreateEvent<TIntegration>\n public getEvent: types.GetEvent<TIntegration> = ((x) => this._client.getEvent(x)) as types.GetEvent<TIntegration>\n public listEvents: types.ListEvents<TIntegration> = ((x) =>\n this._client.listEvents(x)) as types.ListEvents<TIntegration>\n\n public createMessage: types.CreateMessage<TIntegration> = ((x) =>\n this._client.createMessage(x)) as types.CreateMessage<TIntegration>\n public getOrCreateMessage: types.GetOrCreateMessage<TIntegration> = ((x) =>\n this._client.getOrCreateMessage(x)) as types.GetOrCreateMessage<TIntegration>\n public getMessage: types.GetMessage<TIntegration> = ((x) =>\n this._client.getMessage(x)) as types.GetMessage<TIntegration>\n public updateMessage: types.UpdateMessage<TIntegration> = ((x) =>\n this._client.updateMessage(x)) as types.UpdateMessage<TIntegration>\n public listMessages: types.ListMessages<TIntegration> = ((x) =>\n this._client.listMessages(x)) as types.ListMessages<TIntegration>\n public deleteMessage: types.DeleteMessage<TIntegration> = ((x) =>\n this._client.deleteMessage(x)) as types.DeleteMessage<TIntegration>\n\n public createUser: types.CreateUser<TIntegration> = ((x) =>\n this._client.createUser(x)) as types.CreateUser<TIntegration>\n public getUser: types.GetUser<TIntegration> = ((x) => this._client.getUser(x)) as types.GetUser<TIntegration>\n public listUsers: types.ListUsers<TIntegration> = (x) => this._client.listUsers(x)\n public getOrCreateUser: types.GetOrCreateUser<TIntegration> = ((x) =>\n this._client.getOrCreateUser(x)) as types.GetOrCreateUser<TIntegration>\n public updateUser: types.UpdateUser<TIntegration> = ((x) =>\n this._client.updateUser(x)) as types.UpdateUser<TIntegration>\n public deleteUser: types.DeleteUser<TIntegration> = (x) => this._client.deleteUser(x)\n\n public getState: types.GetState<TIntegration> = ((x) => this._client.getState(x)) as types.GetState<TIntegration>\n public setState: types.SetState<TIntegration> = ((x) => this._client.setState(x)) as types.SetState<TIntegration>\n public getOrSetState: types.GetOrSetState<TIntegration> = ((x) =>\n this._client.getOrSetState(x)) as types.GetOrSetState<TIntegration>\n public patchState: types.PatchState<TIntegration> = ((x) =>\n this._client.patchState(x)) as types.PatchState<TIntegration>\n\n public configureIntegration: types.ConfigureIntegration<TIntegration> = (x) => this._client.configureIntegration(x)\n\n public uploadFile: types.UploadFile<TIntegration> = (x) => this._client.uploadFile(x)\n public upsertFile: types.UpsertFile<TIntegration> = (x) => this._client.upsertFile(x)\n public deleteFile: types.DeleteFile<TIntegration> = (x) => this._client.deleteFile(x)\n public listFiles: types.ListFiles<TIntegration> = (x) => this._client.listFiles(x)\n public getFile: types.GetFile<TIntegration> = (x) => this._client.getFile(x)\n public updateFileMetadata: types.UpdateFileMetadata<TIntegration> = (x) => this._client.updateFileMetadata(x)\n}\n", "export type ActionMetadata = {\n cost: number\n}\n\nexport class ActionMetadataStore {\n private _cost: number = 0\n\n public get cost(): number {\n return this._cost\n }\n\n public setCost(cost: number): void {\n this._cost = cost\n }\n\n public toJSON(): ActionMetadata {\n return {\n cost: this.cost,\n }\n }\n}\n", "import { z } from '@bpinternal/zui'\nimport {\n botIdHeader,\n botUserIdHeader,\n configurationHeader,\n configurationTypeHeader,\n integrationIdHeader,\n operationHeader,\n webhookIdHeader,\n} from '../../const'\nimport { IntegrationContext } from './types'\n\nexport const integrationOperationSchema = z.enum([\n 'webhook_received',\n 'message_created',\n 'action_triggered',\n 'register',\n 'unregister',\n 'ping',\n 'create_user',\n 'create_conversation',\n])\n\nexport const extractContext = (headers: Record<string, string | undefined>): IntegrationContext => {\n const botId = headers[botIdHeader]\n const botUserId = headers[botUserIdHeader]\n const integrationId = headers[integrationIdHeader]\n const webhookId = headers[webhookIdHeader]\n const configurationType = headers[configurationTypeHeader]\n const base64Configuration = headers[configurationHeader]\n const operation = integrationOperationSchema.parse(headers[operationHeader])\n\n if (!botId) {\n throw new Error('Missing bot headers')\n }\n\n if (!botUserId) {\n throw new Error('Missing bot user headers')\n }\n\n if (!integrationId) {\n throw new Error('Missing integration headers')\n }\n\n if (!webhookId) {\n throw new Error('Missing webhook headers')\n }\n\n if (!base64Configuration) {\n throw new Error('Missing configuration headers')\n }\n\n if (!operation) {\n throw new Error('Missing operation headers')\n }\n\n return {\n botId,\n botUserId,\n integrationId,\n webhookId,\n operation,\n configurationType: configurationType ?? null,\n configuration: base64Configuration ? JSON.parse(Buffer.from(base64Configuration, 'base64').toString('utf-8')) : {},\n }\n}\n", "import util from 'util'\n\nconst serializeForBotMessage = (args: Parameters<typeof util.format>) => {\n if (process.env['BP_LOG_FORMAT'] === 'json') {\n return JSON.stringify({ msg: util.format(...args), visible_to_bot_owner: true })\n } else {\n const [format, ...param] = args\n return util.format(`[For Bot Owner] ${format}`, ...param)\n }\n}\n\nexport const integrationLogger = {\n /**\n * Use this function to log messages that will be displayed to the Bot Owner.\n */\n forBot: () => {\n return {\n info: (...args: Parameters<typeof console.info>) => {\n console.info(serializeForBotMessage(args))\n },\n warn: (...args: Parameters<typeof console.warn>) => {\n console.warn(serializeForBotMessage(args))\n },\n error: (...args: Parameters<typeof console.error>) => {\n console.error(serializeForBotMessage(args))\n },\n debug: (...args: Parameters<typeof console.debug>) => {\n console.debug(serializeForBotMessage(args))\n },\n }\n },\n}\n\nexport type IntegrationLogger = typeof integrationLogger\n", "import type { Server } from 'node:http'\nimport { serve } from '../serve'\nimport {\n RegisterHandler as RegisterFunction,\n UnregisterHandler as UnregisterFunction,\n WebhookHandler as WebhookFunction,\n CreateUserHandler as CreateUserFunction,\n CreateConversationHandler as CreateConversationFunction,\n ActionHandlers as ActionFunctions,\n ChannelHandlers as ChannelFunctions,\n integrationHandler,\n} from './server'\nimport { BaseIntegration } from './types'\n\nexport type IntegrationImplementationProps<TIntegration extends BaseIntegration = BaseIntegration> = {\n register: RegisterFunction<TIntegration>\n unregister: UnregisterFunction<TIntegration>\n handler: WebhookFunction<TIntegration>\n /**\n * @deprecated\n */\n createUser?: CreateUserFunction<TIntegration>\n /**\n * @deprecated\n */\n createConversation?: CreateConversationFunction<TIntegration>\n actions: ActionFunctions<TIntegration>\n channels: ChannelFunctions<TIntegration>\n}\n\nexport class IntegrationImplementation<TIntegration extends BaseIntegration = BaseIntegration> {\n public readonly actions: IntegrationImplementationProps<TIntegration>['actions']\n public readonly channels: IntegrationImplementationProps<TIntegration>['channels']\n public readonly register: IntegrationImplementationProps<TIntegration>['register']\n public readonly unregister: IntegrationImplementationProps<TIntegration>['unregister']\n public readonly createUser: IntegrationImplementationProps<TIntegration>['createUser']\n public readonly createConversation: IntegrationImplementationProps<TIntegration>['createConversation']\n public readonly webhook: IntegrationImplementationProps<TIntegration>['handler']\n\n public constructor(public readonly props: IntegrationImplementationProps<TIntegration>) {\n this.actions = props.actions\n this.channels = props.channels\n this.register = props.register\n this.unregister = props.unregister\n this.createUser = props.createUser\n this.createConversation = props.createConversation\n this.webhook = props.handler\n }\n\n public readonly handler = integrationHandler(this as IntegrationImplementation<any>)\n public readonly start = (port?: number): Promise<Server> => serve(this.handler, port)\n}\n", "import { IntegrationPackage } from '../package'\nimport { SchemaDefinition } from '../schema'\nimport { ValueOf, Writable } from '../utils/type-utils'\nimport z, { AnyZodObject } from '../zui'\n\ntype BaseStates = Record<string, AnyZodObject>\ntype BaseEvents = Record<string, AnyZodObject>\ntype BaseActions = Record<string, AnyZodObject>\n\nexport type TagDefinition = {\n title?: string\n description?: string\n}\n\nexport type StateType = 'conversation' | 'user' | 'bot'\n\nexport type StateDefinition<TState extends BaseStates[string] = BaseStates[string]> = SchemaDefinition<TState> & {\n type: StateType\n expiry?: number\n}\n\nexport type RecurringEventDefinition<TEvents extends BaseEvents = BaseEvents> = {\n [K in keyof TEvents]: {\n type: K\n payload: z.infer<TEvents[K]>\n schedule: { cron: string }\n }\n}[keyof TEvents]\n\nexport type EventDefinition<TEvent extends BaseEvents[string] = BaseEvents[string]> = SchemaDefinition<TEvent>\n\nexport type ConfigurationDefinition = SchemaDefinition\n\nexport type UserDefinition = {\n tags?: Record<string, TagDefinition>\n}\n\nexport type ConversationDefinition = {\n tags?: Record<string, TagDefinition>\n}\n\nexport type MessageDefinition = {\n tags?: Record<string, TagDefinition>\n}\n\nexport type ActionDefinition<TAction extends BaseActions[string] = BaseActions[string]> = {\n title?: string\n description?: string\n input: SchemaDefinition<TAction>\n output: SchemaDefinition<AnyZodObject> // cannot infer both input and output types (typescript limitation)\n}\n\nexport type IntegrationConfigInstance<I extends IntegrationPackage = IntegrationPackage> = {\n enabled: boolean\n} & (\n | {\n configurationType?: null\n configuration: z.infer<NonNullable<I['definition']['configuration']>['schema']>\n }\n | ValueOf<{\n [K in keyof NonNullable<I['definition']['configurations']>]: {\n configurationType: K\n configuration: z.infer<NonNullable<I['definition']['configurations']>[K]['schema']>\n }\n }>\n)\n\nexport type IntegrationInstance = IntegrationPackage & IntegrationConfigInstance\n\nexport type BotDefinitionProps<\n TStates extends BaseStates = BaseStates,\n TEvents extends BaseEvents = BaseEvents,\n TActions extends BaseActions = BaseActions\n> = {\n integrations?: {\n [K: string]: IntegrationInstance\n }\n user?: UserDefinition\n conversation?: ConversationDefinition\n message?: MessageDefinition\n states?: {\n [K in keyof TStates]: StateDefinition<TStates[K]>\n }\n configuration?: ConfigurationDefinition\n events?: {\n [K in keyof TEvents]: EventDefinition<TEvents[K]>\n }\n recurringEvents?: Record<string, RecurringEventDefinition<TEvents>>\n actions?: {\n [K in keyof TActions]: ActionDefinition<TActions[K]>\n }\n}\n\nexport class BotDefinition<\n TStates extends BaseStates = BaseStates,\n TEvents extends BaseEvents = BaseEvents,\n TActions extends BaseActions = BaseActions\n> {\n public readonly integrations: this['props']['integrations']\n public readonly user: this['props']['user']\n public readonly conversation: this['props']['conversation']\n public readonly message: this['props']['message']\n public readonly states: this['props']['states']\n public readonly configuration: this['props']['configuration']\n public readonly events: this['props']['events']\n public readonly recurringEvents: this['props']['recurringEvents']\n public readonly actions: this['props']['actions']\n public constructor(public readonly props: BotDefinitionProps<TStates, TEvents, TActions>) {\n this.integrations = props.integrations\n this.user = props.user\n this.conversation = props.conversation\n this.message = props.message\n this.states = props.states\n this.configuration = props.configuration\n this.events = props.events\n this.recurringEvents = props.recurringEvents\n this.actions = props.actions\n }\n\n public add<I extends IntegrationPackage>(integrationPkg: I, config: IntegrationConfigInstance<I>): this {\n const self = this as Writable<BotDefinition>\n if (!self.integrations) {\n self.integrations = {}\n }\n\n self.integrations[integrationPkg.definition.name] = {\n enabled: config.enabled,\n ...integrationPkg,\n configurationType: config.configurationType as string,\n configuration: config.configuration,\n }\n return this\n }\n}\n", "import * as client from '@botpress/client'\nimport { log } from '../../log'\nimport { retryConfig } from '../../retry'\nimport { Request, Response, parseBody } from '../../serve'\nimport * as utils from '../../utils/type-utils'\nimport { BotSpecificClient } from '../client'\nimport * as common from '../types'\nimport { extractContext } from './context'\nimport * as types from './types'\n\nexport * from './types'\n\ntype ServerProps = types.CommonHandlerProps<common.BaseBot> & {\n req: Request\n}\n\nconst SUCCESS_RESPONSE = { status: 200 }\n\nexport const botHandler =\n (bot: types.BotHandlers<common.BaseBot>) =>\n async (req: Request): Promise<Response | void> => {\n const ctx = extractContext(req.headers)\n\n const vanillaClient = new client.Client({\n botId: ctx.botId,\n retry: retryConfig,\n })\n const botClient = new BotSpecificClient<common.BaseBot>(vanillaClient, {\n before: {\n createMessage: async (req) => {\n for (const handler of bot.hooks.before_outgoing_message[req.type] ?? []) {\n const hookOutput = await handler({\n client: new BotSpecificClient(vanillaClient),\n ctx,\n data: req,\n })\n req = hookOutput?.data ?? req\n }\n return req\n },\n callAction: async (req) => {\n for (const handler of bot.hooks.before_call_action[req.type] ?? []) {\n const hookOutput = await handler({\n client: new BotSpecificClient(vanillaClient),\n ctx,\n data: req,\n })\n req = hookOutput?.data ?? req\n }\n return req\n },\n },\n after: {\n createMessage: async (res) => {\n for (const handler of bot.hooks.after_outgoing_message[res.message.type] ?? []) {\n const hookOutput = await handler({\n client: new BotSpecificClient(vanillaClient),\n ctx,\n data: res,\n })\n res = hookOutput?.data ?? res\n }\n return res\n },\n callAction: async (res) => {\n for (const handler of bot.hooks.after_call_action[res.output.type] ?? []) {\n const hookOutput = await handler({\n client: new BotSpecificClient(vanillaClient),\n ctx,\n data: res,\n })\n res = hookOutput?.data ?? res\n }\n return res\n },\n },\n })\n\n const props: ServerProps = {\n req,\n ctx,\n client: botClient,\n self: bot,\n }\n\n switch (ctx.operation) {\n case 'action_triggered':\n return await onActionTriggered(props)\n case 'event_received':\n return await onEventReceived(props)\n case 'register':\n return await onRegister(props)\n case 'unregister':\n return await onUnregister(props)\n case 'ping':\n return await onPing(props)\n default:\n throw new Error(`Unknown operation ${ctx.operation}`)\n }\n }\n\nconst onPing = async ({ ctx }: ServerProps): Promise<Response> => {\n log.info(`Received ${ctx.operation} operation for bot ${ctx.botId} of type ${ctx.type}`)\n return SUCCESS_RESPONSE\n}\n\nconst onRegister = async (_: ServerProps): Promise<Response> => SUCCESS_RESPONSE\n\nconst onUnregister = async (_: ServerProps): Promise<Response> => SUCCESS_RESPONSE\n\nconst onEventReceived = async ({ ctx, req, client, self }: ServerProps): Promise<Response> => {\n log.debug(`Received event ${ctx.type}`)\n\n const body = parseBody<types.EventPayload<common.BaseBot>>(req)\n\n if (ctx.type === 'message_created') {\n const event = body.event\n let message: client.Message = event.payload.message\n for (const handler of self.hooks.before_incoming_message[message.type] ?? []) {\n const hookOutput = await handler({\n client,\n ctx,\n data: message,\n })\n message = hookOutput?.data ?? message\n }\n\n const messagePayload: types.MessagePayload<common.BaseBot> = {\n user: event.payload.user,\n conversation: event.payload.conversation,\n states: event.payload.states,\n message,\n event,\n }\n for (const handler of self.messageHandlers) {\n await handler({\n ...messagePayload,\n client,\n ctx,\n self,\n })\n }\n\n for (const handler of self.hooks.after_incoming_message[message.type] ?? []) {\n const hookOutput = await handler({\n client,\n ctx,\n data: message,\n })\n message = hookOutput?.data ?? message\n }\n\n return SUCCESS_RESPONSE\n }\n\n if (ctx.type === 'state_expired') {\n const event = body.event\n const statePayload: types.StateExpiredPayload<common.BaseBot> = { state: event.payload.state }\n for (const handler of self.stateExpiredHandlers) {\n await handler({\n ...statePayload,\n client,\n ctx,\n self,\n })\n }\n return SUCCESS_RESPONSE\n }\n\n let event = body.event\n for (const handler of self.hooks.before_incoming_event[event.type] ?? []) {\n const hookOutput = await handler({\n client,\n ctx,\n data: event,\n })\n event = hookOutput?.data ?? event\n }\n\n const eventPayload = { event }\n for (const handler of self.eventHandlers) {\n await handler({\n ...eventPayload,\n client,\n ctx,\n self,\n })\n }\n\n for (const handler of self.hooks.after_incoming_event[event.type] ?? []) {\n const hookOutput = await handler({\n client,\n ctx,\n data: event,\n })\n event = hookOutput?.data ?? event\n }\n\n return SUCCESS_RESPONSE\n}\n\nconst onActionTriggered = async ({ ctx, req, client, self }: ServerProps): Promise<Response> => {\n type AnyActionPayload = utils.ValueOf<types.ActionHandlerPayloads<common.BaseBot>>\n const { input, type } = parseBody<AnyActionPayload>(req)\n\n if (!type) {\n throw new Error('Missing action type')\n }\n\n const action = self.actionHandlers[type]\n\n if (!action) {\n throw new Error(`Action ${type} not found`)\n }\n\n const output = await action({ ctx, input, client, type, self })\n\n const response = { output }\n return {\n status: 200,\n body: JSON.stringify(response),\n }\n}\n", "import * as client from '@botpress/client'\nimport * as common from '../types'\nimport * as types from './types'\n\nexport * from './types'\n\n/**\n * Just like the regular botpress client, but typed with the bot's properties.\n */\nexport class BotSpecificClient<TBot extends common.BaseBot> implements types.ClientOperations<TBot> {\n public constructor(private _client: client.Client, private _hooks: types.ClientHooks = { before: {}, after: {} }) {}\n\n public getConversation: types.GetConversation<TBot> = ((x) =>\n this._run('getConversation', x)) as types.GetConversation<TBot>\n public listConversations: types.ListConversations<TBot> = ((x) =>\n this._run('listConversations', x)) as types.ListConversations<TBot>\n public updateConversation: types.UpdateConversation<TBot> = ((x) =>\n this._run('updateConversation', x)) as types.UpdateConversation<TBot>\n public deleteConversation: types.DeleteConversation<TBot> = ((x) =>\n this._run('deleteConversation', x)) as types.DeleteConversation<TBot>\n public listParticipants: types.ListParticipants<TBot> = ((x) =>\n this._run('listParticipants', x)) as types.ListParticipants<TBot>\n public addParticipant: types.AddParticipant<TBot> = ((x) =>\n this._run('addParticipant', x)) as types.AddParticipant<TBot>\n public getParticipant: types.GetParticipant<TBot> = ((x) =>\n this._run('getParticipant', x)) as types.GetParticipant<TBot>\n public removeParticipant: types.RemoveParticipant<TBot> = ((x) =>\n this._run('removeParticipant', x)) as types.RemoveParticipant<TBot>\n public getEvent: types.GetEvent<TBot> = ((x) => this._run('getEvent', x)) as types.GetEvent<TBot>\n public listEvents: types.ListEvents<TBot> = ((x) => this._run('listEvents', x)) as types.ListEvents<TBot>\n public createMessage: types.CreateMessage<TBot> = ((x) => this._run('createMessage', x)) as types.CreateMessage<TBot>\n public getOrCreateMessage: types.GetOrCreateMessage<TBot> = ((x) =>\n this._run('getOrCreateMessage', x)) as types.GetOrCreateMessage<TBot>\n public getMessage: types.GetMessage<TBot> = ((x) => this._run('getMessage', x)) as types.GetMessage<TBot>\n public updateMessage: types.UpdateMessage<TBot> = ((x) => this._run('updateMessage', x)) as types.UpdateMessage<TBot>\n public listMessages: types.ListMessages<TBot> = ((x) => this._run('listMessages', x)) as types.ListMessages<TBot>\n public deleteMessage: types.DeleteMessage<TBot> = ((x) => this._run('deleteMessage', x)) as types.DeleteMessage<TBot>\n public getUser: types.GetUser<TBot> = ((x) => this._run('getUser', x)) as types.GetUser<TBot>\n public listUsers: types.ListUsers<TBot> = ((x) => this._run('listUsers', x)) as types.ListUsers<TBot>\n public updateUser: types.UpdateUser<TBot> = ((x) => this._run('updateUser', x)) as types.UpdateUser<TBot>\n public deleteUser: types.DeleteUser<TBot> = ((x) => this._run('deleteUser', x)) as types.DeleteUser<TBot>\n public getState: types.GetState<TBot> = ((x) => this._run('getState', x)) as types.GetState<TBot>\n public setState: types.SetState<TBot> = ((x) => this._run('setState', x)) as types.SetState<TBot>\n public getOrSetState: types.GetOrSetState<TBot> = ((x) => this._run('getOrSetState', x)) as types.GetOrSetState<TBot>\n public patchState: types.PatchState<TBot> = ((x) => this._run('patchState', x)) as types.PatchState<TBot>\n public callAction: types.CallAction<TBot> = ((x) => this._run('callAction', x)) as types.CallAction<TBot>\n public uploadFile: types.UploadFile<TBot> = ((x) => this._run('uploadFile', x)) as types.UploadFile<TBot>\n public upsertFile: types.UpsertFile<TBot> = ((x) => this._run('upsertFile', x)) as types.UpsertFile<TBot>\n public deleteFile: types.DeleteFile<TBot> = ((x) => this._run('deleteFile', x)) as types.DeleteFile<TBot>\n public listFiles: types.ListFiles<TBot> = ((x) => this._run('listFiles', x)) as types.ListFiles<TBot>\n public getFile: types.GetFile<TBot> = ((x) => this._run('getFile', x)) as types.GetFile<TBot>\n public updateFileMetadata: types.UpdateFileMetadata<TBot> = ((x) =>\n this._run('updateFileMetadata', x)) as types.UpdateFileMetadata<TBot>\n public searchFiles: types.SearchFiles<TBot> = ((x) => this._run('searchFiles', x)) as types.SearchFiles<TBot>\n\n /**\n * @deprecated Use `callAction` to delegate the conversation creation to an integration.\n */\n public createConversation: types.CreateConversation<TBot> = (x) => this._client.createConversation(x)\n /**\n * @deprecated Use `callAction` to delegate the conversation creation to an integration.\n */\n public getOrCreateConversation: types.GetOrCreateConversation<TBot> = (x) => this._client.getOrCreateConversation(x)\n /**\n * @deprecated Use `callAction` to delegate the user creation to an integration.\n */\n public createUser: types.CreateUser<TBot> = (x) => this._client.createUser(x)\n /**\n * @deprecated Use `callAction` to delegate the user creation to an integration.\n */\n public getOrCreateUser: types.GetOrCreateUser<TBot> = (x) => this._client.getOrCreateUser(x)\n\n private _run = async <K extends client.Operation>(\n operation: K,\n req: client.ClientInputs[K]\n ): Promise<client.ClientOutputs[K]> => {\n const before = this._hooks.before[operation]\n if (before) {\n req = await before(req)\n }\n\n let res = (await this._client[operation](req as any)) as client.ClientOutputs[K]\n\n const after = this._hooks.after[operation]\n if (after) {\n res = await after(res)\n }\n\n return res\n }\n}\n", "import { z } from '@bpinternal/zui'\nimport { botIdHeader, configurationHeader, operationHeader, typeHeader } from '../../const'\nimport { BotContext } from './types'\n\nconst botOperationSchema = z.enum(['event_received', 'register', 'unregister', 'ping', 'action_triggered'])\nexport const extractContext = (headers: Record<string, string | undefined>): BotContext => {\n const botId = headers[botIdHeader]\n const base64Configuration = headers[configurationHeader]\n const type = headers[typeHeader]\n const operation = botOperationSchema.parse(headers[operationHeader])\n\n if (!botId) {\n throw new Error('Missing bot headers')\n }\n\n if (!type) {\n throw new Error('Missing type headers')\n }\n\n if (!base64Configuration) {\n throw new Error('Missing configuration headers')\n }\n\n if (!operation) {\n throw new Error('Missing operation headers')\n }\n\n return {\n botId,\n operation,\n type,\n configuration: base64Configuration ? JSON.parse(Buffer.from(base64Configuration, 'base64').toString('utf-8')) : {},\n }\n}\n", "import type { Server } from 'node:http'\nimport { serve } from '../serve'\nimport * as utils from '../utils'\nimport {\n botHandler,\n MessageHandler,\n EventHandler,\n StateExpiredHandler,\n HookImplementationsMap,\n HookDefinitions,\n HookImplementations,\n ActionHandlers,\n BotHandlers,\n} from './server'\nimport { BaseBot } from './types'\n\nexport type BotImplementationProps<TBot extends BaseBot = BaseBot> = {\n actions: ActionHandlers<TBot>\n}\n\nexport class BotImplementation<TBot extends BaseBot = BaseBot> {\n public readonly actionHandlers: ActionHandlers<TBot>\n public readonly messageHandlers: MessageHandler<TBot>[] = []\n public readonly eventHandlers: EventHandler<TBot>[] = []\n public readonly stateExpiredHandlers: StateExpiredHandler<TBot>[] = []\n public readonly hooks: HookImplementationsMap<TBot> = {\n before_incoming_event: {},\n before_incoming_message: {},\n before_outgoing_message: {},\n before_call_action: {},\n after_incoming_event: {},\n after_incoming_message: {},\n after_outgoing_message: {},\n after_call_action: {},\n }\n\n public constructor(public readonly props: BotImplementationProps<TBot>) {\n this.actionHandlers = props.actions\n }\n\n public readonly message = (handler: MessageHandler<TBot>): void => {\n this.messageHandlers.push(handler)\n }\n\n public readonly event = (handler: EventHandler<TBot>): void => {\n this.eventHandlers.push(handler)\n }\n\n public readonly stateExpired = (handler: StateExpiredHandler<TBot>): void => {\n this.stateExpiredHandlers.push(handler)\n }\n\n public readonly hook = {\n before_incoming_event: <T extends keyof HookDefinitions<TBot>['before_incoming_event']>(\n type: T,\n handler: HookImplementations<TBot>['before_incoming_event'][T]\n ) => {\n this.hooks.before_incoming_event[type] = utils.arrays.safePush(this.hooks.before_incoming_event[type], handler)\n },\n before_incoming_message: <T extends keyof HookDefinitions<TBot>['before_incoming_message']>(\n type: T,\n handler: HookImplementations<TBot>['before_incoming_message'][T]\n ) => {\n this.hooks.before_incoming_message[type] = utils.arrays.safePush(\n this.hooks.before_incoming_message[type],\n handler\n )\n },\n before_outgoing_message: <T extends keyof HookDefinitions<TBot>['before_outgoing_message']>(\n type: T,\n handler: HookImplementations<TBot>['before_outgoing_message'][T]\n ) => {\n this.hooks.before_outgoing_message[type] = utils.arrays.safePush(\n this.hooks.before_outgoing_message[type],\n handler\n )\n },\n before_call_action: <T extends keyof HookDefinitions<TBot>['before_call_action']>(\n type: T,\n handler: HookImplementations<TBot>['before_call_action'][T]\n ) => {\n this.hooks.before_call_action[type] = utils.arrays.safePush(this.hooks.before_call_action[type], handler)\n },\n after_incoming_event: <T extends keyof HookDefinitions<TBot>['after_incoming_event']>(\n type: T,\n handler: HookImplementations<TBot>['after_incoming_event'][T]\n ) => {\n this.hooks.after_incoming_event[type] = utils.arrays.safePush(this.hooks.after_incoming_event[type], handler)\n },\n after_incoming_message: <T extends keyof HookDefinitions<TBot>['after_incoming_message']>(\n type: T,\n handler: HookImplementations<TBot>['after_incoming_message'][T]\n ) => {\n this.hooks.after_incoming_message[type] = utils.arrays.safePush(this.hooks.after_incoming_message[type], handler)\n },\n after_outgoing_message: <T extends keyof HookDefinitions<TBot>['after_outgoing_message']>(\n type: T,\n handler: HookImplementations<TBot>['after_outgoing_message'][T]\n ) => {\n this.hooks.after_outgoing_message[type] = utils.arrays.safePush(this.hooks.after_outgoing_message[type], handler)\n },\n after_call_action: <T extends keyof HookDefinitions<TBot>['after_call_action']>(\n type: T,\n handler: HookImplementations<TBot>['after_call_action'][T]\n ) => {\n this.hooks.after_call_action[type] = utils.arrays.safePush(this.hooks.after_call_action[type], handler)\n },\n }\n\n public readonly handler = botHandler(this as BotHandlers<any>)\n\n public readonly start = (port?: number): Promise<Server> => serve(this.handler, port)\n}\n", "import { ActionDefinition, ChannelDefinition, EntityDefinition, EventDefinition } from '../integration/definition'\nimport * as utils from '../utils'\nimport z, { AnyZodObject, GenericZuiSchema, ZodRef } from '../zui'\n\ntype BaseEvents = Record<string, AnyZodObject>\ntype BaseActions = Record<string, AnyZodObject>\ntype BaseMessages = Record<string, AnyZodObject>\ntype BaseChannels = Record<string, BaseMessages>\ntype BaseEntities = Record<string, AnyZodObject>\n\ntype EntityReferences<TEntities extends BaseEntities> = {\n [K in keyof TEntities]: ZodRef\n}\n\ntype GenericEventDefinition<TEntities extends BaseEntities, TEvent extends BaseEvents[string] = BaseEvents[string]> = {\n schema: GenericZuiSchema<EntityReferences<TEntities>, TEvent>\n}\n\ntype GenericChannelDefinition<\n TEntities extends BaseEntities,\n TChannel extends BaseChannels[string] = BaseChannels[string]\n> = {\n messages: {\n [K in keyof TChannel]: {\n schema: GenericZuiSchema<EntityReferences<TEntities>, TChannel[K]>\n }\n }\n}\n\ntype GenericActionDefinition<\n TEntities extends BaseEntities,\n TAction extends BaseActions[string] = BaseActions[string]\n> = {\n billable?: boolean\n cacheable?: boolean\n input: { schema: GenericZuiSchema<EntityReferences<TEntities>, TAction> }\n output: { schema: GenericZuiSchema<EntityReferences<TEntities>, AnyZodObject> }\n}\n\nexport type InterfaceDeclarationProps<\n TEntities extends BaseEntities = BaseEntities,\n TActions extends BaseActions = BaseActions,\n TEvents extends BaseEntities = BaseEntities,\n TChannels extends BaseChannels = BaseChannels\n> = {\n name: string\n version: string\n\n entities?: {\n [K in keyof TEntities]: EntityDefinition<TEntities[K]>\n }\n\n events?: { [K in keyof TEvents]: GenericEventDefinition<TEntities, TEvents[K]> }\n\n actions?: {\n [K in keyof TActions]: GenericActionDefinition<TEntities, TActions[K]>\n }\n\n channels?: {\n [K in keyof TChannels]: GenericChannelDefinition<TEntities, TChannels[K]>\n }\n\n templateName?: string\n}\n\nexport class InterfaceDeclaration<\n TEntities extends BaseEntities = BaseEntities,\n TActions extends BaseActions = BaseActions,\n TEvents extends BaseEvents = BaseEvents,\n TChannels extends BaseChannels = BaseChannels\n> {\n public readonly name: this['props']['name']\n public readonly version: this['props']['version']\n\n public readonly entities: { [K in keyof TEntities]: EntityDefinition<TEntities[K]> }\n public readonly events: { [K in keyof TEvents]: EventDefinition<TEvents[K]> }\n public readonly actions: { [K in keyof TActions]: ActionDefinition<TActions[K]> }\n public readonly channels: { [K in keyof TChannels]: ChannelDefinition<TChannels[K]> }\n\n public readonly templateName: this['props']['templateName']\n\n public constructor(public readonly props: InterfaceDeclarationProps<TEntities, TActions, TEvents, TChannels>) {\n this.name = props.name\n this.version = props.version\n this.entities = props.entities ?? ({} as this['entities'])\n this.templateName = props.templateName\n\n const entityReferences = this._getEntityReference(this.entities)\n\n const events: Record<string, EventDefinition> =\n props.events === undefined\n ? {}\n : utils.records.mapValues(\n props.events,\n (event): EventDefinition => ({\n ...event,\n schema: event.schema(entityReferences),\n })\n )\n\n const actions: Record<string, ActionDefinition> =\n props.actions === undefined\n ? {}\n : utils.records.mapValues(\n props.actions,\n (action): ActionDefinition => ({\n ...action,\n input: {\n ...action.input,\n schema: action.input.schema(entityReferences),\n },\n output: {\n ...action.output,\n schema: action.output.schema(entityReferences),\n },\n })\n )\n\n const channels: Record<string, ChannelDefinition> =\n props.channels === undefined\n ? {}\n : utils.records.mapValues(\n props.channels,\n (channel): ChannelDefinition => ({\n ...channel,\n messages: utils.records.mapValues(channel.messages, (message) => ({\n ...message,\n schema: message.schema(entityReferences),\n })),\n })\n )\n\n this.events = events as this['events']\n this.actions = actions as this['actions']\n this.channels = channels as this['channels']\n }\n\n private _getEntityReference = (entities: this['entities']): EntityReferences<TEntities> => {\n const entityReferences: Record<string, ZodRef> = {} as EntityReferences<TEntities>\n for (const entityName of Object.keys(entities)) {\n entityReferences[entityName] = z.ref(entityName)\n }\n return entityReferences as EntityReferences<TEntities>\n }\n}\n"],
|
|
4
|
+
"sourcesContent": ["export * as messages from './message'\nexport * from './const'\nexport * from './serve'\nexport * from './zui'\n\nexport {\n //\n isApiError,\n RuntimeError,\n} from '@botpress/client'\n\nexport {\n DefaultIntegration,\n IntegrationDefinition,\n IntegrationDefinitionProps,\n IntegrationImplementation as Integration,\n IntegrationImplementationProps as IntegrationProps,\n IntegrationLogger,\n IntegrationSpecificClient,\n TagDefinition,\n ConfigurationDefinition,\n AdditionalConfigurationDefinition,\n EventDefinition,\n ChannelDefinition,\n MessageDefinition,\n ActionDefinition,\n StateDefinition,\n UserDefinition,\n SecretDefinition,\n EntityDefinition,\n} from './integration'\n\nexport {\n /**\n * @deprecated use Context exported from '.botpress' instead\n */\n IntegrationContext,\n} from './integration/server'\n\nexport {\n DefaultBot,\n BotDefinition,\n BotDefinitionProps,\n BotImplementation as Bot,\n BotImplementationProps as BotProps,\n BotSpecificClient,\n TagDefinition as BotTagDefinition,\n StateType as BotStateType,\n StateDefinition as BotStateDefinition,\n RecurringEventDefinition as BotRecurringEventDefinition,\n EventDefinition as BotEventDefinition,\n ConfigurationDefinition as BotConfigurationDefinition,\n UserDefinition as BotUserDefinition,\n ConversationDefinition as BotConversationDefinition,\n MessageDefinition as BotMessageDefinition,\n ActionDefinition as BotActionDefinition,\n} from './bot'\n\nexport {\n //\n InterfaceDeclaration,\n InterfaceDeclarationProps,\n} from './interface'\n\nexport {\n //\n IntegrationPackage,\n InterfacePackage,\n} from './package'\n", "import { z } from './zui'\n\nconst NonEmptyString = z.string().min(1)\n\nconst textMessageSchema = z.object({\n text: NonEmptyString,\n})\n\nconst markdownMessageSchema = z.object({\n markdown: NonEmptyString,\n})\n\nconst imageMessageSchema = z.object({\n imageUrl: NonEmptyString,\n})\n\nconst audioMessageSchema = z.object({\n audioUrl: NonEmptyString,\n})\n\nconst videoMessageSchema = z.object({\n videoUrl: NonEmptyString,\n})\n\nconst fileMessageSchema = z.object({\n fileUrl: NonEmptyString,\n title: NonEmptyString.optional(),\n})\n\nconst locationMessageSchema = z.object({\n latitude: z.number(),\n longitude: z.number(),\n address: z.string().optional(),\n title: z.string().optional(),\n})\n\nconst cardSchema = z.object({\n title: NonEmptyString,\n subtitle: NonEmptyString.optional(),\n imageUrl: NonEmptyString.optional(),\n actions: z.array(\n z.object({\n action: z.enum(['postback', 'url', 'say']),\n label: NonEmptyString,\n value: NonEmptyString,\n })\n ),\n})\n\nconst choiceSchema = z.object({\n text: NonEmptyString,\n options: z.array(\n z.object({\n label: NonEmptyString,\n value: NonEmptyString,\n })\n ),\n})\n\nconst carouselSchema = z.object({\n items: z.array(cardSchema),\n})\n\nconst blocSchema = z.union([\n z.object({ type: z.literal('text'), payload: textMessageSchema }),\n z.object({ type: z.literal('markdown'), payload: markdownMessageSchema }),\n z.object({ type: z.literal('image'), payload: imageMessageSchema }),\n z.object({ type: z.literal('audio'), payload: audioMessageSchema }),\n z.object({ type: z.literal('video'), payload: videoMessageSchema }),\n z.object({ type: z.literal('file'), payload: fileMessageSchema }),\n z.object({ type: z.literal('location'), payload: locationMessageSchema }),\n])\n\nconst blocsSchema = z.object({\n items: z.array(blocSchema),\n})\n\n/**\n * @deprecated use `text` instead\n */\nexport const markdown = { schema: markdownMessageSchema }\nexport const defaults = {\n text: { schema: textMessageSchema },\n image: { schema: imageMessageSchema },\n audio: { schema: audioMessageSchema },\n video: { schema: videoMessageSchema },\n file: { schema: fileMessageSchema },\n location: { schema: locationMessageSchema },\n carousel: { schema: carouselSchema },\n card: { schema: cardSchema },\n dropdown: { schema: choiceSchema },\n choice: { schema: choiceSchema },\n bloc: { schema: blocsSchema },\n} as const // should use satisfies operator but this works for older versions of TS\n", "import { z } from '@bpinternal/zui'\nexport * from '@bpinternal/zui'\n\nexport type GenericZuiSchema<\n A extends Record<string, z.ZodTypeAny> = Record<string, z.ZodTypeAny>,\n R extends z.ZodTypeAny = z.ZodTypeAny\n> = (typeArguments: A) => R\n\nexport default z\n", "export const botIdHeader = 'x-bot-id'\nexport const botUserIdHeader = 'x-bot-user-id'\nexport const integrationIdHeader = 'x-integration-id'\nexport const webhookIdHeader = 'x-webhook-id'\n\nexport const configurationTypeHeader = 'x-bp-configuration-type'\nexport const configurationHeader = 'x-bp-configuration'\nexport const operationHeader = 'x-bp-operation'\nexport const typeHeader = 'x-bp-type'\n", "import { createServer, IncomingMessage, Server } from 'node:http'\nimport { log } from './log'\n\nexport type Request = {\n body?: string\n path: string\n query: string\n method: string\n headers: { [key: string]: string | undefined }\n}\n\nexport type Response = {\n body?: string\n headers?: { [key: string]: string }\n status?: number\n}\n\nexport type Handler = (req: Request) => Promise<Response | void>\n\nexport function parseBody<T>(req: Request): T {\n if (!req.body) {\n throw new Error('Missing body')\n }\n return JSON.parse(req.body)\n}\n\nexport async function serve(\n handler: Handler,\n port: number = 8072,\n callback: (port: number) => void = defaultCallback\n): Promise<Server> {\n /* eslint-disable @typescript-eslint/no-misused-promises */\n const server = createServer(async (req, res) => {\n try {\n const request = await mapIncomingMessageToRequest(req)\n if (request.path === '/health') {\n res.writeHead(200).end('ok')\n return\n }\n const response = await handler(request)\n res.writeHead(response?.status ?? 200, response?.headers ?? {}).end(response?.body ?? '{}')\n } catch (e: any) {\n log.error('Error while handling request', { error: e?.message ?? 'Internal error occured' })\n res.writeHead(500).end(JSON.stringify({ error: e?.message ?? 'Internal error occured' }))\n }\n })\n\n server.listen(port, () => callback(port))\n return server\n}\n\nasync function mapIncomingMessageToRequest(incoming: IncomingMessage): Promise<Request> {\n const body = await readBody(incoming)\n const headers = {} as Request['headers']\n\n for (let i = 0; i < incoming.rawHeaders.length; i += 2) {\n const key = incoming.rawHeaders[i]!.toLowerCase()\n const value = incoming.rawHeaders[i + 1]!\n headers[key] = value\n }\n\n const url = new URL(\n incoming.url ?? '',\n incoming.headers.host ? `http://${incoming.headers.host}` : 'http://botpress.cloud'\n )\n\n return {\n body,\n path: url.pathname,\n query: trimPrefix(url.search, '?'),\n headers,\n method: incoming.method?.toUpperCase() ?? 'GET',\n }\n}\n\nfunction trimPrefix(value: string, prefix: string) {\n return value.indexOf(prefix) === 0 ? value.slice(prefix.length) : value\n}\n\nasync function readBody(incoming: IncomingMessage) {\n return new Promise<string | undefined>((resolve, reject) => {\n if (incoming.method !== 'POST' && incoming.method !== 'PUT' && incoming.method !== 'PATCH') {\n return resolve(undefined)\n }\n\n let body = ''\n\n incoming.on('data', (chunk) => (body += chunk.toString()))\n incoming.on('error', (e) => reject(e))\n incoming.on('end', () => resolve(body))\n })\n}\n\nfunction defaultCallback(port: number) {\n log.info(`Listening on port ${port}`)\n}\n", "export type Logger = {\n debug(message: string, metadata?: any): void\n info(message: string, metadata?: any): void\n warn(message: string, metadata?: any): void\n error(message: string, metadata?: any): void\n}\nexport const log: Logger = console\n", "export const pairs = <K extends string, V>(obj: Record<K, V>) => Object.entries(obj) as [K, V][]\nexport const mapValues = <K extends string, V, R>(obj: Record<K, V>, fn: (value: V, key: K) => R): Record<K, R> =>\n Object.fromEntries(pairs(obj).map(([key, value]) => [key, fn(value, key)])) as Record<K, R>\n", "export const safePush = <T>(arr: T[] | undefined, value: T): T[] => (arr ? [...arr, value] : [value])\n", "import * as utils from '../../utils'\nimport { z } from '../../zui'\n\nconst schemaName = Symbol('schemaName')\n\ntype BaseSchemas = Record<string, z.ZodSchema>\n\nexport type SchemaStoreProps<TSchemas extends BaseSchemas = BaseSchemas> = {\n [K in keyof TSchemas]: {\n schema: TSchemas[K]\n }\n}\n\nexport type BrandedSchema<TSchema extends BaseSchemas[string] = BaseSchemas[string]> = {\n schema: TSchema\n [schemaName]: string\n}\n\nexport type SchemaStore<TSchemas extends BaseSchemas = BaseSchemas> = {\n [K in keyof TSchemas]: BrandedSchema<TSchemas[K]>\n}\n\nexport const createStore = <TSchemas extends BaseSchemas>(\n props: SchemaStoreProps<TSchemas> | undefined\n): SchemaStore<TSchemas> => {\n if (!props) {\n return {} as SchemaStore<TSchemas>\n }\n const store: SchemaStore<BaseSchemas> = utils.records.mapValues(props, (e, k) => ({ ...e, [schemaName]: k }))\n return store as SchemaStore<TSchemas>\n}\n\nexport const isBranded = (schema: BrandedSchema | { schema: z.ZodSchema }): schema is BrandedSchema => {\n return schemaName in schema && schema[schemaName] !== undefined\n}\n\nexport const getName = (schema: BrandedSchema): string => {\n return schema[schemaName]\n}\n", "import { InterfacePackage } from '../../package'\nimport * as utils from '../../utils'\nimport { z } from '../../zui'\nimport { SchemaStore, BrandedSchema, createStore, isBranded, getName } from './branded-schema'\nimport { BaseConfig, BaseEvents, BaseActions, BaseChannels, BaseStates, BaseEntities, BaseConfigs } from './generic'\nimport {\n ConfigurationDefinition,\n EventDefinition,\n ChannelDefinition,\n ActionDefinition,\n StateDefinition,\n UserDefinition,\n SecretDefinition,\n EntityDefinition,\n AdditionalConfigurationDefinition,\n} from './types'\n\nexport * from './types'\n\nexport type InterfaceInstance = InterfacePackage & {\n entities: Record<\n string,\n {\n name: string\n schema: z.AnyZodObject\n }\n >\n}\n\nexport type IntegrationDefinitionProps<\n TConfig extends BaseConfig = BaseConfig,\n TConfigs extends BaseConfigs = BaseConfigs,\n TEvents extends BaseEvents = BaseEvents,\n TActions extends BaseActions = BaseActions,\n TChannels extends BaseChannels = BaseChannels,\n TStates extends BaseStates = BaseStates,\n TEntities extends BaseEntities = BaseEntities\n> = {\n name: string\n version: string\n\n title?: string\n description?: string\n icon?: string\n readme?: string\n\n identifier?: {\n extractScript?: string\n fallbackHandlerScript?: string\n }\n\n configuration?: ConfigurationDefinition<TConfig>\n configurations?: {\n [K in keyof TConfigs]: AdditionalConfigurationDefinition<TConfigs[K]>\n }\n\n events?: { [K in keyof TEvents]: EventDefinition<TEvents[K]> }\n\n actions?: {\n [K in keyof TActions]: ActionDefinition<TActions[K]>\n }\n\n channels?: {\n [K in keyof TChannels]: ChannelDefinition<TChannels[K]>\n }\n\n states?: {\n [K in keyof TStates]: StateDefinition<TStates[K]>\n }\n\n user?: UserDefinition\n\n secrets?: Record<string, SecretDefinition>\n\n entities?: {\n [K in keyof TEntities]: EntityDefinition<TEntities[K]>\n }\n\n interfaces?: Record<string, InterfaceInstance>\n}\n\ntype EntitiesOfPackage<TPackage extends InterfacePackage> = {\n [K in keyof TPackage['definition']['entities']]: NonNullable<TPackage['definition']['entities']>[K]['schema']\n}\n\ntype ExtensionBuilderInput<TIntegrationEntities extends BaseEntities> = SchemaStore<TIntegrationEntities>\n\ntype ExtensionBuilderOutput<TInterfaceEntities extends BaseEntities> = {\n [K in keyof TInterfaceEntities]: BrandedSchema<z.ZodSchema<z.infer<TInterfaceEntities[K]>>>\n}\n\ntype ExtensionBuilder<TIntegrationEntities extends BaseEntities, TInterfaceEntities extends BaseEntities> = (\n input: ExtensionBuilderInput<TIntegrationEntities>\n) => ExtensionBuilderOutput<TInterfaceEntities>\n\nexport class IntegrationDefinition<\n TConfig extends BaseConfig = BaseConfig,\n TConfigs extends BaseConfigs = BaseConfigs,\n TEvents extends BaseEvents = BaseEvents,\n TActions extends BaseActions = BaseActions,\n TChannels extends BaseChannels = BaseChannels,\n TStates extends BaseStates = BaseStates,\n TEntities extends BaseEntities = BaseEntities\n> {\n public readonly name: this['props']['name']\n public readonly version: this['props']['version']\n public readonly title: this['props']['title']\n public readonly description: this['props']['description']\n public readonly icon: this['props']['icon']\n public readonly readme: this['props']['readme']\n public readonly configuration: this['props']['configuration']\n public readonly configurations: this['props']['configurations']\n public readonly events: this['props']['events']\n public readonly actions: this['props']['actions']\n public readonly channels: this['props']['channels']\n public readonly states: this['props']['states']\n public readonly user: this['props']['user']\n public readonly secrets: this['props']['secrets']\n public readonly identifier: this['props']['identifier']\n public readonly entities: this['props']['entities']\n public readonly interfaces: this['props']['interfaces']\n public constructor(\n public readonly props: IntegrationDefinitionProps<\n TConfig,\n TConfigs,\n TEvents,\n TActions,\n TChannels,\n TStates,\n TEntities\n >\n ) {\n this.name = props.name\n this.version = props.version\n this.icon = props.icon\n this.readme = props.readme\n this.title = props.title\n this.identifier = props.identifier\n this.description = props.description\n this.configuration = props.configuration\n this.configurations = props.configurations\n this.events = props.events\n this.actions = props.actions\n this.channels = props.channels\n this.states = props.states\n this.user = props.user\n this.secrets = props.secrets\n this.entities = props.entities\n this.interfaces = props.interfaces\n }\n\n public extend<P extends InterfacePackage>(\n interfacePkg: P,\n builder: ExtensionBuilder<TEntities, EntitiesOfPackage<P>>\n ): this {\n const extensionBuilderOutput = builder(createStore(this.entities))\n const unbrandedEntity = utils.records.pairs(extensionBuilderOutput).find(([_k, e]) => !isBranded(e))\n if (unbrandedEntity) {\n // this means the user tried providing a plain schema without referencing an entity from the integration\n throw new Error(\n `Cannot extend interface \"${interfacePkg.definition.name}\" with entity \"${unbrandedEntity[0]}\"; the provided schema is not part of the integration's entities.`\n )\n }\n\n const self = this as utils.types.Writable<IntegrationDefinition>\n self.interfaces ??= {}\n\n const interfaceTypeArguments = utils.records.mapValues(extensionBuilderOutput, (e) => ({\n name: getName(e),\n schema: e.schema as z.AnyZodObject,\n }))\n\n const entityNames = Object.values(interfaceTypeArguments).map((e) => e.name)\n\n const key =\n entityNames.length === 0\n ? interfacePkg.definition.name\n : `${interfacePkg.definition.name}<${entityNames.join(',')}>`\n\n self.interfaces[key] = {\n ...interfacePkg,\n entities: interfaceTypeArguments,\n }\n\n return this\n }\n}\n", "import { isApiError, Client, RuntimeError } from '@botpress/client'\nimport { retryConfig } from '../../retry'\nimport { Request, Response, parseBody } from '../../serve'\nimport { IntegrationSpecificClient } from '../client'\nimport { BaseIntegration } from '../types'\nimport { ActionMetadataStore } from './action-metadata'\nimport { extractContext } from './context'\nimport { integrationLogger } from './logger'\nimport {\n CommonHandlerProps,\n IntegrationHandlers,\n WebhookPayload,\n ActionPayload,\n MessagePayload,\n RegisterPayload,\n CreateUserPayload,\n UnregisterPayload,\n CreateConversationPayload,\n} from './types'\n\nexport * from './types'\nexport * from './logger'\n\ntype ServerProps = CommonHandlerProps<BaseIntegration> & {\n req: Request\n instance: IntegrationHandlers<BaseIntegration>\n}\n\nexport const integrationHandler =\n (instance: IntegrationHandlers<BaseIntegration>) =>\n async (req: Request): Promise<Response | void> => {\n const ctx = extractContext(req.headers)\n\n const vanillaClient = new Client({\n botId: ctx.botId,\n integrationId: ctx.integrationId,\n retry: retryConfig,\n })\n const client = new IntegrationSpecificClient<BaseIntegration>(vanillaClient)\n\n const props = {\n ctx,\n req,\n client,\n logger: integrationLogger,\n instance,\n }\n\n try {\n let response: Response | void\n switch (ctx.operation) {\n case 'webhook_received':\n response = await onWebhook(props)\n break\n case 'register':\n response = await onRegister(props)\n break\n case 'unregister':\n response = await onUnregister(props)\n break\n case 'message_created':\n response = await onMessageCreated(props)\n break\n case 'action_triggered':\n response = await onActionTriggered(props)\n break\n case 'ping':\n response = await onPing(props)\n break\n case 'create_user':\n response = await onCreateUser(props)\n break\n case 'create_conversation':\n response = await onCreateConversation(props)\n break\n default:\n throw new Error(`Unknown operation ${ctx.operation}`)\n }\n return response ? { ...response, status: response.status ?? 200 } : { status: 200 }\n } catch (thrown) {\n if (isApiError(thrown)) {\n const runtimeError = new RuntimeError(thrown.message, thrown)\n integrationLogger.forBot().error(runtimeError.message)\n\n return { status: runtimeError.code, body: JSON.stringify(runtimeError.toJSON()) }\n }\n\n // prints the error in the integration logs\n console.error(thrown)\n\n const runtimeError = new RuntimeError(\n 'An unexpected error occurred in the integration. Bot owners: Check logs for more informations. Integration owners: throw a RuntimeError to return a custom error message instead.'\n )\n integrationLogger.forBot().error(runtimeError.message)\n return { status: runtimeError.code, body: JSON.stringify(runtimeError.toJSON()) }\n }\n }\n\nconst onPing = async (_: ServerProps) => {}\n\nconst onWebhook = async ({ client, ctx, req: incomingRequest, logger, instance }: ServerProps) => {\n const { req } = parseBody<WebhookPayload>(incomingRequest)\n return instance.webhook({ client, ctx, req, logger })\n}\n\nconst onRegister = async ({ client, ctx, req, logger, instance }: ServerProps) => {\n if (!instance.register) {\n return\n }\n const { webhookUrl } = parseBody<RegisterPayload>(req)\n await instance.register({ client, ctx, webhookUrl, logger })\n}\n\nconst onUnregister = async ({ client, ctx, req, logger, instance }: ServerProps) => {\n if (!instance.unregister) {\n return\n }\n const { webhookUrl } = parseBody<UnregisterPayload>(req)\n await instance.unregister({ ctx, webhookUrl, client, logger })\n}\n\nconst onCreateUser = async ({ client, ctx, req, logger, instance }: ServerProps) => {\n if (!instance.createUser) {\n return\n }\n const { tags } = parseBody<CreateUserPayload<BaseIntegration>>(req)\n return await instance.createUser({ ctx, client, tags, logger })\n}\n\nconst onCreateConversation = async ({ client, ctx, req, logger, instance }: ServerProps) => {\n if (!instance.createConversation) {\n return\n }\n const { channel, tags } = parseBody<CreateConversationPayload<BaseIntegration>>(req)\n return await instance.createConversation({ ctx, client, channel, tags, logger })\n}\n\nconst onMessageCreated = async ({ ctx, req, client, logger, instance }: ServerProps) => {\n const { conversation, user, type, payload, message } = parseBody<MessagePayload<BaseIntegration, string, string>>(req)\n\n const channelHandler = instance.channels[conversation.channel]\n\n if (!channelHandler) {\n throw new Error(`Channel ${conversation.channel} not found`)\n }\n\n const messageHandler = channelHandler.messages[type]\n\n if (!messageHandler) {\n throw new Error(`Message of type ${type} not found in channel ${conversation.channel}`)\n }\n\n type UpdateMessageProps = Parameters<(typeof client)['updateMessage']>[0]\n const ack = async ({ tags }: Pick<UpdateMessageProps, 'tags'>) => {\n await client.updateMessage({\n id: message.id,\n tags,\n })\n }\n\n await messageHandler({ ctx, conversation, message, user, type, client, payload, ack, logger })\n}\n\nconst onActionTriggered = async ({ req, ctx, client, logger, instance }: ServerProps) => {\n const { input, type } = parseBody<ActionPayload<string, any>>(req)\n\n if (!type) {\n throw new Error('Missing action type')\n }\n\n const action = instance.actions[type]\n\n if (!action) {\n throw new Error(`Action ${type} not found`)\n }\n\n const metadata = new ActionMetadataStore()\n const output = await action({ ctx, input, client, type, logger, metadata })\n\n const response = { output, meta: metadata.toJSON() }\n return {\n body: JSON.stringify(response),\n }\n}\n", "import { RetryConfig, axiosRetry } from '@botpress/client'\n\nexport const retryConfig: RetryConfig = {\n retries: 3,\n retryCondition: (err) =>\n axiosRetry.isNetworkOrIdempotentRequestError(err) || [429, 502].includes(err.response?.status ?? 0),\n retryDelay: (retryCount) => retryCount * 1000,\n}\n", "/* eslint-disable brace-style */\nimport * as client from '@botpress/client'\nimport * as common from '../types'\nimport * as types from './types'\n\nexport * from './types'\n\n/**\n * Just like the regular botpress client, but typed with the integration's properties.\n */\nexport class IntegrationSpecificClient<TIntegration extends common.BaseIntegration>\n implements types.ClientOperations<TIntegration>\n{\n public constructor(private readonly _client: client.Client) {}\n\n public createConversation: types.CreateConversation<TIntegration> = ((x) =>\n this._client.createConversation(x)) as types.CreateConversation<TIntegration>\n public getConversation: types.GetConversation<TIntegration> = ((x) =>\n this._client.getConversation(x)) as types.GetConversation<TIntegration>\n public listConversations: types.ListConversations<TIntegration> = ((x) =>\n this._client.listConversations(x)) as types.ListConversations<TIntegration>\n public getOrCreateConversation: types.GetOrCreateConversation<TIntegration> = ((x) =>\n this._client.getOrCreateConversation(x)) as types.GetOrCreateConversation<TIntegration>\n public updateConversation: types.UpdateConversation<TIntegration> = ((x) =>\n this._client.updateConversation(x)) as types.UpdateConversation<TIntegration>\n public deleteConversation: types.DeleteConversation<TIntegration> = ((x) =>\n this._client.deleteConversation(x)) as types.DeleteConversation<TIntegration>\n\n public listParticipants: types.ListParticipants<TIntegration> = ((x) =>\n this._client.listParticipants(x)) as types.ListParticipants<TIntegration>\n public addParticipant: types.AddParticipant<TIntegration> = ((x) =>\n this._client.addParticipant(x)) as types.AddParticipant<TIntegration>\n public getParticipant: types.GetParticipant<TIntegration> = ((x) =>\n this._client.getParticipant(x)) as types.GetParticipant<TIntegration>\n public removeParticipant: types.RemoveParticipant<TIntegration> = ((x) =>\n this._client.removeParticipant(x)) as types.RemoveParticipant<TIntegration>\n\n public createEvent: types.CreateEvent<TIntegration> = ((x) =>\n this._client.createEvent(x)) as types.CreateEvent<TIntegration>\n public getEvent: types.GetEvent<TIntegration> = ((x) => this._client.getEvent(x)) as types.GetEvent<TIntegration>\n public listEvents: types.ListEvents<TIntegration> = ((x) =>\n this._client.listEvents(x)) as types.ListEvents<TIntegration>\n\n public createMessage: types.CreateMessage<TIntegration> = ((x) =>\n this._client.createMessage(x)) as types.CreateMessage<TIntegration>\n public getOrCreateMessage: types.GetOrCreateMessage<TIntegration> = ((x) =>\n this._client.getOrCreateMessage(x)) as types.GetOrCreateMessage<TIntegration>\n public getMessage: types.GetMessage<TIntegration> = ((x) =>\n this._client.getMessage(x)) as types.GetMessage<TIntegration>\n public updateMessage: types.UpdateMessage<TIntegration> = ((x) =>\n this._client.updateMessage(x)) as types.UpdateMessage<TIntegration>\n public listMessages: types.ListMessages<TIntegration> = ((x) =>\n this._client.listMessages(x)) as types.ListMessages<TIntegration>\n public deleteMessage: types.DeleteMessage<TIntegration> = ((x) =>\n this._client.deleteMessage(x)) as types.DeleteMessage<TIntegration>\n\n public createUser: types.CreateUser<TIntegration> = ((x) =>\n this._client.createUser(x)) as types.CreateUser<TIntegration>\n public getUser: types.GetUser<TIntegration> = ((x) => this._client.getUser(x)) as types.GetUser<TIntegration>\n public listUsers: types.ListUsers<TIntegration> = (x) => this._client.listUsers(x)\n public getOrCreateUser: types.GetOrCreateUser<TIntegration> = ((x) =>\n this._client.getOrCreateUser(x)) as types.GetOrCreateUser<TIntegration>\n public updateUser: types.UpdateUser<TIntegration> = ((x) =>\n this._client.updateUser(x)) as types.UpdateUser<TIntegration>\n public deleteUser: types.DeleteUser<TIntegration> = (x) => this._client.deleteUser(x)\n\n public getState: types.GetState<TIntegration> = ((x) => this._client.getState(x)) as types.GetState<TIntegration>\n public setState: types.SetState<TIntegration> = ((x) => this._client.setState(x)) as types.SetState<TIntegration>\n public getOrSetState: types.GetOrSetState<TIntegration> = ((x) =>\n this._client.getOrSetState(x)) as types.GetOrSetState<TIntegration>\n public patchState: types.PatchState<TIntegration> = ((x) =>\n this._client.patchState(x)) as types.PatchState<TIntegration>\n\n public configureIntegration: types.ConfigureIntegration<TIntegration> = (x) => this._client.configureIntegration(x)\n\n public uploadFile: types.UploadFile<TIntegration> = (x) => this._client.uploadFile(x)\n public upsertFile: types.UpsertFile<TIntegration> = (x) => this._client.upsertFile(x)\n public deleteFile: types.DeleteFile<TIntegration> = (x) => this._client.deleteFile(x)\n public listFiles: types.ListFiles<TIntegration> = (x) => this._client.listFiles(x)\n public getFile: types.GetFile<TIntegration> = (x) => this._client.getFile(x)\n public updateFileMetadata: types.UpdateFileMetadata<TIntegration> = (x) => this._client.updateFileMetadata(x)\n}\n", "export type ActionMetadata = {\n cost: number\n}\n\nexport class ActionMetadataStore {\n private _cost: number = 0\n\n public get cost(): number {\n return this._cost\n }\n\n public setCost(cost: number): void {\n this._cost = cost\n }\n\n public toJSON(): ActionMetadata {\n return {\n cost: this.cost,\n }\n }\n}\n", "import { z } from '@bpinternal/zui'\nimport {\n botIdHeader,\n botUserIdHeader,\n configurationHeader,\n configurationTypeHeader,\n integrationIdHeader,\n operationHeader,\n webhookIdHeader,\n} from '../../const'\nimport { IntegrationContext } from './types'\n\nexport const integrationOperationSchema = z.enum([\n 'webhook_received',\n 'message_created',\n 'action_triggered',\n 'register',\n 'unregister',\n 'ping',\n 'create_user',\n 'create_conversation',\n])\n\nexport const extractContext = (headers: Record<string, string | undefined>): IntegrationContext => {\n const botId = headers[botIdHeader]\n const botUserId = headers[botUserIdHeader]\n const integrationId = headers[integrationIdHeader]\n const webhookId = headers[webhookIdHeader]\n const configurationType = headers[configurationTypeHeader]\n const base64Configuration = headers[configurationHeader]\n const operation = integrationOperationSchema.parse(headers[operationHeader])\n\n if (!botId) {\n throw new Error('Missing bot headers')\n }\n\n if (!botUserId) {\n throw new Error('Missing bot user headers')\n }\n\n if (!integrationId) {\n throw new Error('Missing integration headers')\n }\n\n if (!webhookId) {\n throw new Error('Missing webhook headers')\n }\n\n if (!base64Configuration) {\n throw new Error('Missing configuration headers')\n }\n\n if (!operation) {\n throw new Error('Missing operation headers')\n }\n\n return {\n botId,\n botUserId,\n integrationId,\n webhookId,\n operation,\n configurationType: configurationType ?? null,\n configuration: base64Configuration ? JSON.parse(Buffer.from(base64Configuration, 'base64').toString('utf-8')) : {},\n }\n}\n", "import util from 'util'\n\nconst serializeForBotMessage = (args: Parameters<typeof util.format>) => {\n if (process.env['BP_LOG_FORMAT'] === 'json') {\n return JSON.stringify({ msg: util.format(...args), visible_to_bot_owner: true })\n } else {\n const [format, ...param] = args\n return util.format(`[For Bot Owner] ${format}`, ...param)\n }\n}\n\nexport const integrationLogger = {\n /**\n * Use this function to log messages that will be displayed to the Bot Owner.\n */\n forBot: () => {\n return {\n info: (...args: Parameters<typeof console.info>) => {\n console.info(serializeForBotMessage(args))\n },\n warn: (...args: Parameters<typeof console.warn>) => {\n console.warn(serializeForBotMessage(args))\n },\n error: (...args: Parameters<typeof console.error>) => {\n console.error(serializeForBotMessage(args))\n },\n debug: (...args: Parameters<typeof console.debug>) => {\n console.debug(serializeForBotMessage(args))\n },\n }\n },\n}\n\nexport type IntegrationLogger = typeof integrationLogger\n", "import type { Server } from 'node:http'\nimport { serve } from '../serve'\nimport {\n RegisterHandler as RegisterFunction,\n UnregisterHandler as UnregisterFunction,\n WebhookHandler as WebhookFunction,\n CreateUserHandler as CreateUserFunction,\n CreateConversationHandler as CreateConversationFunction,\n ActionHandlers as ActionFunctions,\n ChannelHandlers as ChannelFunctions,\n integrationHandler,\n} from './server'\nimport { BaseIntegration } from './types'\n\nexport type IntegrationImplementationProps<TIntegration extends BaseIntegration = BaseIntegration> = {\n register: RegisterFunction<TIntegration>\n unregister: UnregisterFunction<TIntegration>\n handler: WebhookFunction<TIntegration>\n /**\n * @deprecated\n */\n createUser?: CreateUserFunction<TIntegration>\n /**\n * @deprecated\n */\n createConversation?: CreateConversationFunction<TIntegration>\n actions: ActionFunctions<TIntegration>\n channels: ChannelFunctions<TIntegration>\n}\n\nexport class IntegrationImplementation<TIntegration extends BaseIntegration = BaseIntegration> {\n public readonly actions: IntegrationImplementationProps<TIntegration>['actions']\n public readonly channels: IntegrationImplementationProps<TIntegration>['channels']\n public readonly register: IntegrationImplementationProps<TIntegration>['register']\n public readonly unregister: IntegrationImplementationProps<TIntegration>['unregister']\n public readonly createUser: IntegrationImplementationProps<TIntegration>['createUser']\n public readonly createConversation: IntegrationImplementationProps<TIntegration>['createConversation']\n public readonly webhook: IntegrationImplementationProps<TIntegration>['handler']\n\n public constructor(public readonly props: IntegrationImplementationProps<TIntegration>) {\n this.actions = props.actions\n this.channels = props.channels\n this.register = props.register\n this.unregister = props.unregister\n this.createUser = props.createUser\n this.createConversation = props.createConversation\n this.webhook = props.handler\n }\n\n public readonly handler = integrationHandler(this as IntegrationImplementation<any>)\n public readonly start = (port?: number): Promise<Server> => serve(this.handler, port)\n}\n", "import { IntegrationPackage } from '../package'\nimport { SchemaDefinition } from '../schema'\nimport { ValueOf, Writable } from '../utils/type-utils'\nimport z, { AnyZodObject } from '../zui'\n\ntype BaseStates = Record<string, AnyZodObject>\ntype BaseEvents = Record<string, AnyZodObject>\ntype BaseActions = Record<string, AnyZodObject>\n\nexport type TagDefinition = {\n title?: string\n description?: string\n}\n\nexport type StateType = 'conversation' | 'user' | 'bot'\n\nexport type StateDefinition<TState extends BaseStates[string] = BaseStates[string]> = SchemaDefinition<TState> & {\n type: StateType\n expiry?: number\n}\n\nexport type RecurringEventDefinition<TEvents extends BaseEvents = BaseEvents> = {\n [K in keyof TEvents]: {\n type: K\n payload: z.infer<TEvents[K]>\n schedule: { cron: string }\n }\n}[keyof TEvents]\n\nexport type EventDefinition<TEvent extends BaseEvents[string] = BaseEvents[string]> = SchemaDefinition<TEvent>\n\nexport type ConfigurationDefinition = SchemaDefinition\n\nexport type UserDefinition = {\n tags?: Record<string, TagDefinition>\n}\n\nexport type ConversationDefinition = {\n tags?: Record<string, TagDefinition>\n}\n\nexport type MessageDefinition = {\n tags?: Record<string, TagDefinition>\n}\n\nexport type ActionDefinition<TAction extends BaseActions[string] = BaseActions[string]> = {\n title?: string\n description?: string\n input: SchemaDefinition<TAction>\n output: SchemaDefinition<AnyZodObject> // cannot infer both input and output types (typescript limitation)\n}\n\nexport type IntegrationConfigInstance<I extends IntegrationPackage = IntegrationPackage> = {\n enabled: boolean\n} & (\n | {\n configurationType?: null\n configuration: z.infer<NonNullable<I['definition']['configuration']>['schema']>\n }\n | ValueOf<{\n [K in keyof NonNullable<I['definition']['configurations']>]: {\n configurationType: K\n configuration: z.infer<NonNullable<I['definition']['configurations']>[K]['schema']>\n }\n }>\n)\n\nexport type IntegrationInstance = IntegrationPackage & IntegrationConfigInstance\n\nexport type BotDefinitionProps<\n TStates extends BaseStates = BaseStates,\n TEvents extends BaseEvents = BaseEvents,\n TActions extends BaseActions = BaseActions\n> = {\n integrations?: {\n [K: string]: IntegrationInstance\n }\n user?: UserDefinition\n conversation?: ConversationDefinition\n message?: MessageDefinition\n states?: {\n [K in keyof TStates]: StateDefinition<TStates[K]>\n }\n configuration?: ConfigurationDefinition\n events?: {\n [K in keyof TEvents]: EventDefinition<TEvents[K]>\n }\n recurringEvents?: Record<string, RecurringEventDefinition<TEvents>>\n actions?: {\n [K in keyof TActions]: ActionDefinition<TActions[K]>\n }\n}\n\nexport class BotDefinition<\n TStates extends BaseStates = BaseStates,\n TEvents extends BaseEvents = BaseEvents,\n TActions extends BaseActions = BaseActions\n> {\n public readonly integrations: this['props']['integrations']\n public readonly user: this['props']['user']\n public readonly conversation: this['props']['conversation']\n public readonly message: this['props']['message']\n public readonly states: this['props']['states']\n public readonly configuration: this['props']['configuration']\n public readonly events: this['props']['events']\n public readonly recurringEvents: this['props']['recurringEvents']\n public readonly actions: this['props']['actions']\n public constructor(public readonly props: BotDefinitionProps<TStates, TEvents, TActions>) {\n this.integrations = props.integrations\n this.user = props.user\n this.conversation = props.conversation\n this.message = props.message\n this.states = props.states\n this.configuration = props.configuration\n this.events = props.events\n this.recurringEvents = props.recurringEvents\n this.actions = props.actions\n }\n\n public add<I extends IntegrationPackage>(integrationPkg: I, config: IntegrationConfigInstance<I>): this {\n const self = this as Writable<BotDefinition>\n if (!self.integrations) {\n self.integrations = {}\n }\n\n self.integrations[integrationPkg.definition.name] = {\n enabled: config.enabled,\n ...integrationPkg,\n configurationType: config.configurationType as string,\n configuration: config.configuration,\n }\n return this\n }\n}\n", "import * as client from '@botpress/client'\nimport { log } from '../../log'\nimport { retryConfig } from '../../retry'\nimport { Request, Response, parseBody } from '../../serve'\nimport * as utils from '../../utils/type-utils'\nimport { BotSpecificClient } from '../client'\nimport * as common from '../types'\nimport { extractContext } from './context'\nimport * as types from './types'\n\nexport * from './types'\n\ntype ServerProps = types.CommonHandlerProps<common.BaseBot> & {\n req: Request\n}\n\nconst SUCCESS_RESPONSE = { status: 200 }\n\nexport const botHandler =\n (bot: types.BotHandlers<common.BaseBot>) =>\n async (req: Request): Promise<Response | void> => {\n const ctx = extractContext(req.headers)\n\n const vanillaClient = new client.Client({\n botId: ctx.botId,\n retry: retryConfig,\n })\n const botClient = new BotSpecificClient<common.BaseBot>(vanillaClient, {\n before: {\n createMessage: async (req) => {\n for (const handler of bot.hooks.before_outgoing_message[req.type] ?? []) {\n const hookOutput = await handler({\n client: new BotSpecificClient(vanillaClient),\n ctx,\n data: req,\n })\n req = hookOutput?.data ?? req\n }\n return req\n },\n callAction: async (req) => {\n for (const handler of bot.hooks.before_call_action[req.type] ?? []) {\n const hookOutput = await handler({\n client: new BotSpecificClient(vanillaClient),\n ctx,\n data: req,\n })\n req = hookOutput?.data ?? req\n }\n return req\n },\n },\n after: {\n createMessage: async (res) => {\n for (const handler of bot.hooks.after_outgoing_message[res.message.type] ?? []) {\n const hookOutput = await handler({\n client: new BotSpecificClient(vanillaClient),\n ctx,\n data: res,\n })\n res = hookOutput?.data ?? res\n }\n return res\n },\n callAction: async (res) => {\n for (const handler of bot.hooks.after_call_action[res.output.type] ?? []) {\n const hookOutput = await handler({\n client: new BotSpecificClient(vanillaClient),\n ctx,\n data: res,\n })\n res = hookOutput?.data ?? res\n }\n return res\n },\n },\n })\n\n const props: ServerProps = {\n req,\n ctx,\n client: botClient,\n self: bot,\n }\n\n switch (ctx.operation) {\n case 'action_triggered':\n return await onActionTriggered(props)\n case 'event_received':\n return await onEventReceived(props)\n case 'register':\n return await onRegister(props)\n case 'unregister':\n return await onUnregister(props)\n case 'ping':\n return await onPing(props)\n default:\n throw new Error(`Unknown operation ${ctx.operation}`)\n }\n }\n\nconst onPing = async ({ ctx }: ServerProps): Promise<Response> => {\n log.info(`Received ${ctx.operation} operation for bot ${ctx.botId} of type ${ctx.type}`)\n return SUCCESS_RESPONSE\n}\n\nconst onRegister = async (_: ServerProps): Promise<Response> => SUCCESS_RESPONSE\n\nconst onUnregister = async (_: ServerProps): Promise<Response> => SUCCESS_RESPONSE\n\nconst onEventReceived = async ({ ctx, req, client, self }: ServerProps): Promise<Response> => {\n log.debug(`Received event ${ctx.type}`)\n\n const body = parseBody<types.EventPayload<common.BaseBot>>(req)\n\n if (ctx.type === 'message_created') {\n const event = body.event\n let message: client.Message = event.payload.message\n for (const handler of self.hooks.before_incoming_message[message.type] ?? []) {\n const hookOutput = await handler({\n client,\n ctx,\n data: message,\n })\n message = hookOutput?.data ?? message\n }\n\n const messagePayload: types.MessagePayload<common.BaseBot> = {\n user: event.payload.user,\n conversation: event.payload.conversation,\n states: event.payload.states,\n message,\n event,\n }\n for (const handler of self.messageHandlers) {\n await handler({\n ...messagePayload,\n client,\n ctx,\n self,\n })\n }\n\n for (const handler of self.hooks.after_incoming_message[message.type] ?? []) {\n const hookOutput = await handler({\n client,\n ctx,\n data: message,\n })\n message = hookOutput?.data ?? message\n }\n\n return SUCCESS_RESPONSE\n }\n\n if (ctx.type === 'state_expired') {\n const event = body.event\n const statePayload: types.StateExpiredPayload<common.BaseBot> = { state: event.payload.state }\n for (const handler of self.stateExpiredHandlers) {\n await handler({\n ...statePayload,\n client,\n ctx,\n self,\n })\n }\n return SUCCESS_RESPONSE\n }\n\n let event = body.event\n for (const handler of self.hooks.before_incoming_event[event.type] ?? []) {\n const hookOutput = await handler({\n client,\n ctx,\n data: event,\n })\n event = hookOutput?.data ?? event\n }\n\n const eventPayload = { event }\n for (const handler of self.eventHandlers) {\n await handler({\n ...eventPayload,\n client,\n ctx,\n self,\n })\n }\n\n for (const handler of self.hooks.after_incoming_event[event.type] ?? []) {\n const hookOutput = await handler({\n client,\n ctx,\n data: event,\n })\n event = hookOutput?.data ?? event\n }\n\n return SUCCESS_RESPONSE\n}\n\nconst onActionTriggered = async ({ ctx, req, client, self }: ServerProps): Promise<Response> => {\n type AnyActionPayload = utils.ValueOf<types.ActionHandlerPayloads<common.BaseBot>>\n const { input, type } = parseBody<AnyActionPayload>(req)\n\n if (!type) {\n throw new Error('Missing action type')\n }\n\n const action = self.actionHandlers[type]\n\n if (!action) {\n throw new Error(`Action ${type} not found`)\n }\n\n const output = await action({ ctx, input, client, type, self })\n\n const response = { output }\n return {\n status: 200,\n body: JSON.stringify(response),\n }\n}\n", "import * as client from '@botpress/client'\nimport * as common from '../types'\nimport * as types from './types'\n\nexport * from './types'\n\n/**\n * Just like the regular botpress client, but typed with the bot's properties.\n */\nexport class BotSpecificClient<TBot extends common.BaseBot> implements types.ClientOperations<TBot> {\n public constructor(private _client: client.Client, private _hooks: types.ClientHooks = { before: {}, after: {} }) {}\n\n public getConversation: types.GetConversation<TBot> = ((x) =>\n this._run('getConversation', x)) as types.GetConversation<TBot>\n public listConversations: types.ListConversations<TBot> = ((x) =>\n this._run('listConversations', x)) as types.ListConversations<TBot>\n public updateConversation: types.UpdateConversation<TBot> = ((x) =>\n this._run('updateConversation', x)) as types.UpdateConversation<TBot>\n public deleteConversation: types.DeleteConversation<TBot> = ((x) =>\n this._run('deleteConversation', x)) as types.DeleteConversation<TBot>\n public listParticipants: types.ListParticipants<TBot> = ((x) =>\n this._run('listParticipants', x)) as types.ListParticipants<TBot>\n public addParticipant: types.AddParticipant<TBot> = ((x) =>\n this._run('addParticipant', x)) as types.AddParticipant<TBot>\n public getParticipant: types.GetParticipant<TBot> = ((x) =>\n this._run('getParticipant', x)) as types.GetParticipant<TBot>\n public removeParticipant: types.RemoveParticipant<TBot> = ((x) =>\n this._run('removeParticipant', x)) as types.RemoveParticipant<TBot>\n public getEvent: types.GetEvent<TBot> = ((x) => this._run('getEvent', x)) as types.GetEvent<TBot>\n public listEvents: types.ListEvents<TBot> = ((x) => this._run('listEvents', x)) as types.ListEvents<TBot>\n public createMessage: types.CreateMessage<TBot> = ((x) => this._run('createMessage', x)) as types.CreateMessage<TBot>\n public getOrCreateMessage: types.GetOrCreateMessage<TBot> = ((x) =>\n this._run('getOrCreateMessage', x)) as types.GetOrCreateMessage<TBot>\n public getMessage: types.GetMessage<TBot> = ((x) => this._run('getMessage', x)) as types.GetMessage<TBot>\n public updateMessage: types.UpdateMessage<TBot> = ((x) => this._run('updateMessage', x)) as types.UpdateMessage<TBot>\n public listMessages: types.ListMessages<TBot> = ((x) => this._run('listMessages', x)) as types.ListMessages<TBot>\n public deleteMessage: types.DeleteMessage<TBot> = ((x) => this._run('deleteMessage', x)) as types.DeleteMessage<TBot>\n public getUser: types.GetUser<TBot> = ((x) => this._run('getUser', x)) as types.GetUser<TBot>\n public listUsers: types.ListUsers<TBot> = ((x) => this._run('listUsers', x)) as types.ListUsers<TBot>\n public updateUser: types.UpdateUser<TBot> = ((x) => this._run('updateUser', x)) as types.UpdateUser<TBot>\n public deleteUser: types.DeleteUser<TBot> = ((x) => this._run('deleteUser', x)) as types.DeleteUser<TBot>\n public getState: types.GetState<TBot> = ((x) => this._run('getState', x)) as types.GetState<TBot>\n public setState: types.SetState<TBot> = ((x) => this._run('setState', x)) as types.SetState<TBot>\n public getOrSetState: types.GetOrSetState<TBot> = ((x) => this._run('getOrSetState', x)) as types.GetOrSetState<TBot>\n public patchState: types.PatchState<TBot> = ((x) => this._run('patchState', x)) as types.PatchState<TBot>\n public callAction: types.CallAction<TBot> = ((x) => this._run('callAction', x)) as types.CallAction<TBot>\n public uploadFile: types.UploadFile<TBot> = ((x) => this._run('uploadFile', x)) as types.UploadFile<TBot>\n public upsertFile: types.UpsertFile<TBot> = ((x) => this._run('upsertFile', x)) as types.UpsertFile<TBot>\n public deleteFile: types.DeleteFile<TBot> = ((x) => this._run('deleteFile', x)) as types.DeleteFile<TBot>\n public listFiles: types.ListFiles<TBot> = ((x) => this._run('listFiles', x)) as types.ListFiles<TBot>\n public getFile: types.GetFile<TBot> = ((x) => this._run('getFile', x)) as types.GetFile<TBot>\n public updateFileMetadata: types.UpdateFileMetadata<TBot> = ((x) =>\n this._run('updateFileMetadata', x)) as types.UpdateFileMetadata<TBot>\n public searchFiles: types.SearchFiles<TBot> = ((x) => this._run('searchFiles', x)) as types.SearchFiles<TBot>\n\n /**\n * @deprecated Use `callAction` to delegate the conversation creation to an integration.\n */\n public createConversation: types.CreateConversation<TBot> = (x) => this._client.createConversation(x)\n /**\n * @deprecated Use `callAction` to delegate the conversation creation to an integration.\n */\n public getOrCreateConversation: types.GetOrCreateConversation<TBot> = (x) => this._client.getOrCreateConversation(x)\n /**\n * @deprecated Use `callAction` to delegate the user creation to an integration.\n */\n public createUser: types.CreateUser<TBot> = (x) => this._client.createUser(x)\n /**\n * @deprecated Use `callAction` to delegate the user creation to an integration.\n */\n public getOrCreateUser: types.GetOrCreateUser<TBot> = (x) => this._client.getOrCreateUser(x)\n\n private _run = async <K extends client.Operation>(\n operation: K,\n req: client.ClientInputs[K]\n ): Promise<client.ClientOutputs[K]> => {\n const before = this._hooks.before[operation]\n if (before) {\n req = await before(req)\n }\n\n let res = (await this._client[operation](req as any)) as client.ClientOutputs[K]\n\n const after = this._hooks.after[operation]\n if (after) {\n res = await after(res)\n }\n\n return res\n }\n}\n", "import { z } from '@bpinternal/zui'\nimport { botIdHeader, configurationHeader, operationHeader, typeHeader } from '../../const'\nimport { BotContext } from './types'\n\nconst botOperationSchema = z.enum(['event_received', 'register', 'unregister', 'ping', 'action_triggered'])\nexport const extractContext = (headers: Record<string, string | undefined>): BotContext => {\n const botId = headers[botIdHeader]\n const base64Configuration = headers[configurationHeader]\n const type = headers[typeHeader]\n const operation = botOperationSchema.parse(headers[operationHeader])\n\n if (!botId) {\n throw new Error('Missing bot headers')\n }\n\n if (!type) {\n throw new Error('Missing type headers')\n }\n\n if (!base64Configuration) {\n throw new Error('Missing configuration headers')\n }\n\n if (!operation) {\n throw new Error('Missing operation headers')\n }\n\n return {\n botId,\n operation,\n type,\n configuration: base64Configuration ? JSON.parse(Buffer.from(base64Configuration, 'base64').toString('utf-8')) : {},\n }\n}\n", "import type { Server } from 'node:http'\nimport { serve } from '../serve'\nimport * as utils from '../utils'\nimport {\n botHandler,\n MessageHandler,\n EventHandler,\n StateExpiredHandler,\n HookImplementationsMap,\n HookDefinitions,\n HookImplementations,\n ActionHandlers,\n BotHandlers,\n} from './server'\nimport { BaseBot } from './types'\n\nexport type BotImplementationProps<TBot extends BaseBot = BaseBot> = {\n actions: ActionHandlers<TBot>\n}\n\nexport class BotImplementation<TBot extends BaseBot = BaseBot> {\n public readonly actionHandlers: ActionHandlers<TBot>\n public readonly messageHandlers: MessageHandler<TBot>[] = []\n public readonly eventHandlers: EventHandler<TBot>[] = []\n public readonly stateExpiredHandlers: StateExpiredHandler<TBot>[] = []\n public readonly hooks: HookImplementationsMap<TBot> = {\n before_incoming_event: {},\n before_incoming_message: {},\n before_outgoing_message: {},\n before_call_action: {},\n after_incoming_event: {},\n after_incoming_message: {},\n after_outgoing_message: {},\n after_call_action: {},\n }\n\n public constructor(public readonly props: BotImplementationProps<TBot>) {\n this.actionHandlers = props.actions\n }\n\n public readonly message = (handler: MessageHandler<TBot>): void => {\n this.messageHandlers.push(handler)\n }\n\n public readonly event = (handler: EventHandler<TBot>): void => {\n this.eventHandlers.push(handler)\n }\n\n public readonly stateExpired = (handler: StateExpiredHandler<TBot>): void => {\n this.stateExpiredHandlers.push(handler)\n }\n\n public readonly hook = {\n before_incoming_event: <T extends keyof HookDefinitions<TBot>['before_incoming_event']>(\n type: T,\n handler: HookImplementations<TBot>['before_incoming_event'][T]\n ) => {\n this.hooks.before_incoming_event[type] = utils.arrays.safePush(this.hooks.before_incoming_event[type], handler)\n },\n before_incoming_message: <T extends keyof HookDefinitions<TBot>['before_incoming_message']>(\n type: T,\n handler: HookImplementations<TBot>['before_incoming_message'][T]\n ) => {\n this.hooks.before_incoming_message[type] = utils.arrays.safePush(\n this.hooks.before_incoming_message[type],\n handler\n )\n },\n before_outgoing_message: <T extends keyof HookDefinitions<TBot>['before_outgoing_message']>(\n type: T,\n handler: HookImplementations<TBot>['before_outgoing_message'][T]\n ) => {\n this.hooks.before_outgoing_message[type] = utils.arrays.safePush(\n this.hooks.before_outgoing_message[type],\n handler\n )\n },\n before_call_action: <T extends keyof HookDefinitions<TBot>['before_call_action']>(\n type: T,\n handler: HookImplementations<TBot>['before_call_action'][T]\n ) => {\n this.hooks.before_call_action[type] = utils.arrays.safePush(this.hooks.before_call_action[type], handler)\n },\n after_incoming_event: <T extends keyof HookDefinitions<TBot>['after_incoming_event']>(\n type: T,\n handler: HookImplementations<TBot>['after_incoming_event'][T]\n ) => {\n this.hooks.after_incoming_event[type] = utils.arrays.safePush(this.hooks.after_incoming_event[type], handler)\n },\n after_incoming_message: <T extends keyof HookDefinitions<TBot>['after_incoming_message']>(\n type: T,\n handler: HookImplementations<TBot>['after_incoming_message'][T]\n ) => {\n this.hooks.after_incoming_message[type] = utils.arrays.safePush(this.hooks.after_incoming_message[type], handler)\n },\n after_outgoing_message: <T extends keyof HookDefinitions<TBot>['after_outgoing_message']>(\n type: T,\n handler: HookImplementations<TBot>['after_outgoing_message'][T]\n ) => {\n this.hooks.after_outgoing_message[type] = utils.arrays.safePush(this.hooks.after_outgoing_message[type], handler)\n },\n after_call_action: <T extends keyof HookDefinitions<TBot>['after_call_action']>(\n type: T,\n handler: HookImplementations<TBot>['after_call_action'][T]\n ) => {\n this.hooks.after_call_action[type] = utils.arrays.safePush(this.hooks.after_call_action[type], handler)\n },\n }\n\n public readonly handler = botHandler(this as BotHandlers<any>)\n\n public readonly start = (port?: number): Promise<Server> => serve(this.handler, port)\n}\n", "import { ActionDefinition, ChannelDefinition, EntityDefinition, EventDefinition } from '../integration/definition'\nimport * as utils from '../utils'\nimport z, { AnyZodObject, GenericZuiSchema, ZodRef } from '../zui'\n\ntype BaseEvents = Record<string, AnyZodObject>\ntype BaseActions = Record<string, AnyZodObject>\ntype BaseMessages = Record<string, AnyZodObject>\ntype BaseChannels = Record<string, BaseMessages>\ntype BaseEntities = Record<string, AnyZodObject>\n\ntype EntityReferences<TEntities extends BaseEntities> = {\n [K in keyof TEntities]: ZodRef\n}\n\ntype GenericEventDefinition<TEntities extends BaseEntities, TEvent extends BaseEvents[string] = BaseEvents[string]> = {\n schema: GenericZuiSchema<EntityReferences<TEntities>, TEvent>\n}\n\ntype GenericChannelDefinition<\n TEntities extends BaseEntities,\n TChannel extends BaseChannels[string] = BaseChannels[string]\n> = {\n messages: {\n [K in keyof TChannel]: {\n schema: GenericZuiSchema<EntityReferences<TEntities>, TChannel[K]>\n }\n }\n}\n\ntype GenericActionDefinition<\n TEntities extends BaseEntities,\n TAction extends BaseActions[string] = BaseActions[string]\n> = {\n billable?: boolean\n cacheable?: boolean\n input: { schema: GenericZuiSchema<EntityReferences<TEntities>, TAction> }\n output: { schema: GenericZuiSchema<EntityReferences<TEntities>, AnyZodObject> }\n}\n\nexport type InterfaceDeclarationProps<\n TEntities extends BaseEntities = BaseEntities,\n TActions extends BaseActions = BaseActions,\n TEvents extends BaseEntities = BaseEntities,\n TChannels extends BaseChannels = BaseChannels\n> = {\n name: string\n version: string\n\n entities?: {\n [K in keyof TEntities]: EntityDefinition<TEntities[K]>\n }\n\n events?: { [K in keyof TEvents]: GenericEventDefinition<TEntities, TEvents[K]> }\n\n actions?: {\n [K in keyof TActions]: GenericActionDefinition<TEntities, TActions[K]>\n }\n\n channels?: {\n [K in keyof TChannels]: GenericChannelDefinition<TEntities, TChannels[K]>\n }\n\n templateName?: string\n}\n\nexport class InterfaceDeclaration<\n TEntities extends BaseEntities = BaseEntities,\n TActions extends BaseActions = BaseActions,\n TEvents extends BaseEvents = BaseEvents,\n TChannels extends BaseChannels = BaseChannels\n> {\n public readonly name: this['props']['name']\n public readonly version: this['props']['version']\n\n public readonly entities: { [K in keyof TEntities]: EntityDefinition<TEntities[K]> }\n public readonly events: { [K in keyof TEvents]: EventDefinition<TEvents[K]> }\n public readonly actions: { [K in keyof TActions]: ActionDefinition<TActions[K]> }\n public readonly channels: { [K in keyof TChannels]: ChannelDefinition<TChannels[K]> }\n\n public readonly templateName: this['props']['templateName']\n\n public constructor(public readonly props: InterfaceDeclarationProps<TEntities, TActions, TEvents, TChannels>) {\n this.name = props.name\n this.version = props.version\n this.entities = props.entities ?? ({} as this['entities'])\n this.templateName = props.templateName\n\n const entityReferences = this._getEntityReference(this.entities)\n\n const events: Record<string, EventDefinition> =\n props.events === undefined\n ? {}\n : utils.records.mapValues(\n props.events,\n (event): EventDefinition => ({\n ...event,\n schema: event.schema(entityReferences),\n })\n )\n\n const actions: Record<string, ActionDefinition> =\n props.actions === undefined\n ? {}\n : utils.records.mapValues(\n props.actions,\n (action): ActionDefinition => ({\n ...action,\n input: {\n ...action.input,\n schema: action.input.schema(entityReferences),\n },\n output: {\n ...action.output,\n schema: action.output.schema(entityReferences),\n },\n })\n )\n\n const channels: Record<string, ChannelDefinition> =\n props.channels === undefined\n ? {}\n : utils.records.mapValues(\n props.channels,\n (channel): ChannelDefinition => ({\n ...channel,\n messages: utils.records.mapValues(channel.messages, (message) => ({\n ...message,\n schema: message.schema(entityReferences),\n })),\n })\n )\n\n this.events = events as this['events']\n this.actions = actions as this['actions']\n this.channels = channels as this['channels']\n }\n\n private _getEntityReference = (entities: this['entities']): EntityReferences<TEntities> => {\n const entityReferences: Record<string, ZodRef> = {} as EntityReferences<TEntities>\n for (const entityName of Object.keys(entities)) {\n entityReferences[entityName] = z.ref(entityName)\n }\n return entityReferences as EntityReferences<TEntities>\n }\n}\n"],
|
|
5
5
|
"mappings": "mnBAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,SAAAE,EAAA,kBAAAC,EAAA,sBAAAC,EAAA,gBAAAC,EAAA,0BAAAC,EAAA,8BAAAC,EAAA,yBAAAC,EAAA,gDAAAC,EAAA,oBAAAC,EAAA,wBAAAC,EAAA,4BAAAC,EAAA,wBAAAC,EAAA,yCAAAC,EAAA,oBAAAC,EAAA,cAAAC,EAAA,UAAAC,EAAA,eAAAC,EAAA,oBAAAC,IAAA,eAAAC,GAAApB,GCAA,IAAAqB,EAAA,GAAAC,EAAAD,EAAA,cAAAE,GAAA,aAAAC,KCAA,IAAAC,EAAA,GAAAC,EAAAD,EAAA,aAAAE,IAAA,IAAAC,EAAkB,2BAClBC,EAAAJ,EAAc,4BAOd,IAAOE,EAAQ,IDNf,IAAMG,EAAiB,IAAE,OAAO,EAAE,IAAI,CAAC,EAEjCC,GAAoB,IAAE,OAAO,CACjC,KAAMD,CACR,CAAC,EAEKE,GAAwB,IAAE,OAAO,CACrC,SAAUF,CACZ,CAAC,EAEKG,GAAqB,IAAE,OAAO,CAClC,SAAUH,CACZ,CAAC,EAEKI,GAAqB,IAAE,OAAO,CAClC,SAAUJ,CACZ,CAAC,EAEKK,GAAqB,IAAE,OAAO,CAClC,SAAUL,CACZ,CAAC,EAEKM,GAAoB,IAAE,OAAO,CACjC,QAASN,EACT,MAAOA,EAAe,SAAS,CACjC,CAAC,EAEKO,GAAwB,IAAE,OAAO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UACZH,EAAM,SACLQ,IAAgC,CAC/B,GAAGA,EACH,SAAgBL,EAAQ,UAAUK,EAAQ,SAAWC,IAAa,CAChE,GAAGA,EACH,OAAQA,EAAQ,OAAOR,CAAgB,CACzC,EAAE,CACJ,EACF,EAEN,KAAK,OAASC,EACd,KAAK,QAAUG,EACf,KAAK,SAAWE,CAClB,CAhEgB,KACA,QAEA,SACA,OACA,QACA,SAEA,aA0DR,oBAAuBG,GAA4D,CACzF,IAAMT,EAA2C,CAAC,EAClD,QAAWU,KAAc,OAAO,KAAKD,CAAQ,EAC3CT,EAAiBU,CAAU,EAAIC,EAAE,IAAID,CAAU,EAEjD,OAAOV,CACT,CACF",
|
|
6
6
|
"names": ["src_exports", "__export", "BotImplementation", "BotDefinition", "BotSpecificClient", "IntegrationImplementation", "IntegrationDefinition", "IntegrationSpecificClient", "InterfaceDeclaration", "botIdHeader", "botUserIdHeader", "configurationHeader", "configurationTypeHeader", "integrationIdHeader", "message_exports", "operationHeader", "parseBody", "serve", "typeHeader", "webhookIdHeader", "__toCommonJS", "message_exports", "__export", "defaults", "markdown", "zui_exports", "__export", "zui_default", "import_zui", "__reExport", "NonEmptyString", "textMessageSchema", "markdownMessageSchema", "imageMessageSchema", "audioMessageSchema", "videoMessageSchema", "fileMessageSchema", "locationMessageSchema", "cardSchema", "choiceSchema", "carouselSchema", "blocSchema", "blocsSchema", "markdown", "defaults", "botIdHeader", "botUserIdHeader", "integrationIdHeader", "webhookIdHeader", "configurationTypeHeader", "configurationHeader", "operationHeader", "typeHeader", "import_node_http", "log", "parseBody", "req", "serve", "handler", "port", "callback", "defaultCallback", "server", "res", "request", "mapIncomingMessageToRequest", "response", "e", "log", "incoming", "body", "readBody", "headers", "i", "key", "value", "url", "trimPrefix", "prefix", "resolve", "reject", "chunk", "__reExport", "src_exports", "zui_exports", "import_client", "record_utils_exports", "__export", "mapValues", "pairs", "obj", "fn", "key", "value", "array_utils_exports", "__export", "safePush", "arr", "value", "schemaName", "createStore", "props", "record_utils_exports", "e", "k", "isBranded", "schema", "getName", "IntegrationDefinition", "props", "interfacePkg", "builder", "extensionBuilderOutput", "createStore", "unbrandedEntity", "record_utils_exports", "_k", "e", "isBranded", "self", "interfaceTypeArguments", "getName", "entityNames", "key", "import_client", "import_client", "retryConfig", "err", "retryCount", "IntegrationSpecificClient", "_client", "x", "ActionMetadataStore", "cost", "import_zui", "integrationOperationSchema", "extractContext", "headers", "botId", "botIdHeader", "botUserId", "botUserIdHeader", "integrationId", "integrationIdHeader", "webhookId", "webhookIdHeader", "configurationType", "configurationTypeHeader", "base64Configuration", "configurationHeader", "operation", "operationHeader", "import_util", "serializeForBotMessage", "args", "util", "format", "param", "integrationLogger", "integrationHandler", "instance", "req", "ctx", "extractContext", "vanillaClient", "retryConfig", "client", "IntegrationSpecificClient", "props", "integrationLogger", "response", "onWebhook", "onRegister", "onUnregister", "onMessageCreated", "onActionTriggered", "onPing", "onCreateUser", "onCreateConversation", "thrown", "runtimeError", "_", "incomingRequest", "logger", "parseBody", "webhookUrl", "tags", "channel", "conversation", "user", "type", "payload", "message", "channelHandler", "messageHandler", "input", "action", "metadata", "ActionMetadataStore", "IntegrationImplementation", "props", "integrationHandler", "port", "serve", "BotDefinition", "props", "integrationPkg", "config", "self", "client", "BotSpecificClient", "_client", "_hooks", "x", "operation", "req", "before", "res", "after", "import_zui", "botOperationSchema", "extractContext", "headers", "botId", "botIdHeader", "base64Configuration", "configurationHeader", "type", "typeHeader", "operation", "operationHeader", "SUCCESS_RESPONSE", "botHandler", "bot", "req", "ctx", "extractContext", "vanillaClient", "retryConfig", "botClient", "BotSpecificClient", "handler", "res", "props", "onActionTriggered", "onEventReceived", "onRegister", "onUnregister", "onPing", "log", "_", "client", "self", "body", "parseBody", "event", "message", "messagePayload", "statePayload", "eventPayload", "input", "type", "action", "response", "BotImplementation", "props", "handler", "type", "array_utils_exports", "botHandler", "port", "serve", "InterfaceDeclaration", "props", "entityReferences", "events", "record_utils_exports", "event", "actions", "action", "channels", "channel", "message", "entities", "entityName", "zui_default"]
|
|
7
7
|
}
|
|
@@ -1,54 +1,52 @@
|
|
|
1
1
|
import * as utils from '../../utils/type-utils';
|
|
2
|
+
export type BaseMessage = {
|
|
3
|
+
tags: Record<string, any>;
|
|
4
|
+
};
|
|
5
|
+
export type BaseConversation = {
|
|
6
|
+
tags: Record<string, any>;
|
|
7
|
+
};
|
|
8
|
+
export type BaseChannel = {
|
|
9
|
+
messages: Record<string, any>;
|
|
10
|
+
message: BaseMessage;
|
|
11
|
+
conversation: BaseConversation;
|
|
12
|
+
};
|
|
13
|
+
export type BaseUser = {
|
|
14
|
+
tags: Record<string, any>;
|
|
15
|
+
};
|
|
16
|
+
export type BaseAction = {
|
|
17
|
+
input: any;
|
|
18
|
+
output: any;
|
|
19
|
+
};
|
|
2
20
|
export type BaseIntegration = {
|
|
3
21
|
name: string;
|
|
4
22
|
version: string;
|
|
5
23
|
configuration: any;
|
|
6
24
|
configurations: Record<string, any>;
|
|
7
|
-
actions: Record<string,
|
|
8
|
-
channels: Record<string,
|
|
9
|
-
messages: Record<string, any>;
|
|
10
|
-
message: {
|
|
11
|
-
tags: Record<string, any>;
|
|
12
|
-
};
|
|
13
|
-
conversation: {
|
|
14
|
-
tags: Record<string, any>;
|
|
15
|
-
creation: {
|
|
16
|
-
enabled: boolean;
|
|
17
|
-
requiredTags: string[];
|
|
18
|
-
};
|
|
19
|
-
};
|
|
20
|
-
}>;
|
|
25
|
+
actions: Record<string, BaseAction>;
|
|
26
|
+
channels: Record<string, BaseChannel>;
|
|
21
27
|
events: Record<string, any>;
|
|
22
28
|
states: Record<string, any>;
|
|
23
|
-
user:
|
|
24
|
-
tags: Record<string, any>;
|
|
25
|
-
creation: {
|
|
26
|
-
enabled: boolean;
|
|
27
|
-
requiredTags: string[];
|
|
28
|
-
};
|
|
29
|
-
};
|
|
29
|
+
user: BaseUser;
|
|
30
30
|
entities: Record<string, any>;
|
|
31
31
|
};
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Usefull for tests, allows to create an integration with only the properties you want to override
|
|
42
|
-
*/
|
|
43
|
-
export type MakeIntegration<I extends Partial<BaseIntegration>> = {
|
|
32
|
+
export type InputBaseChannel = utils.DeepPartial<BaseChannel>;
|
|
33
|
+
export type DefaultChannel<C extends InputBaseChannel> = {
|
|
34
|
+
messages: utils.Default<C['messages'], BaseChannel['messages']>;
|
|
35
|
+
message: utils.Default<C['message'], BaseChannel['message']>;
|
|
36
|
+
conversation: utils.Default<C['conversation'], BaseChannel['conversation']>;
|
|
37
|
+
};
|
|
38
|
+
export type InputBaseIntegration = utils.DeepPartial<BaseIntegration>;
|
|
39
|
+
export type DefaultIntegration<I extends InputBaseIntegration> = {
|
|
44
40
|
name: utils.Default<I['name'], BaseIntegration['name']>;
|
|
45
41
|
version: utils.Default<I['version'], BaseIntegration['version']>;
|
|
46
42
|
configuration: utils.Default<I['configuration'], BaseIntegration['configuration']>;
|
|
47
43
|
configurations: utils.Default<I['configurations'], BaseIntegration['configurations']>;
|
|
48
44
|
actions: utils.Default<I['actions'], BaseIntegration['actions']>;
|
|
49
|
-
channels: utils.Default<I['channels'], BaseIntegration['channels']>;
|
|
50
45
|
events: utils.Default<I['events'], BaseIntegration['events']>;
|
|
51
46
|
states: utils.Default<I['states'], BaseIntegration['states']>;
|
|
52
47
|
user: utils.Default<I['user'], BaseIntegration['user']>;
|
|
53
48
|
entities: utils.Default<I['entities'], BaseIntegration['entities']>;
|
|
49
|
+
channels: undefined extends I['channels'] ? BaseIntegration['channels'] : {
|
|
50
|
+
[K in keyof I['channels']]: DefaultChannel<utils.Cast<I['channels'][K], InputBaseChannel>>;
|
|
51
|
+
};
|
|
54
52
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/message.d.ts
CHANGED
|
@@ -97,43 +97,43 @@ export declare const defaults: {
|
|
|
97
97
|
label: string;
|
|
98
98
|
}>, "many">;
|
|
99
99
|
}, "strip", import("@bpinternal/zui").ZodTypeAny, {
|
|
100
|
-
title: string;
|
|
101
100
|
actions: {
|
|
102
101
|
value: string;
|
|
103
102
|
action: "postback" | "url" | "say";
|
|
104
103
|
label: string;
|
|
105
104
|
}[];
|
|
105
|
+
title: string;
|
|
106
106
|
imageUrl?: string | undefined;
|
|
107
107
|
subtitle?: string | undefined;
|
|
108
108
|
}, {
|
|
109
|
-
title: string;
|
|
110
109
|
actions: {
|
|
111
110
|
value: string;
|
|
112
111
|
action: "postback" | "url" | "say";
|
|
113
112
|
label: string;
|
|
114
113
|
}[];
|
|
114
|
+
title: string;
|
|
115
115
|
imageUrl?: string | undefined;
|
|
116
116
|
subtitle?: string | undefined;
|
|
117
117
|
}>, "many">;
|
|
118
118
|
}, "strip", import("@bpinternal/zui").ZodTypeAny, {
|
|
119
119
|
items: {
|
|
120
|
-
title: string;
|
|
121
120
|
actions: {
|
|
122
121
|
value: string;
|
|
123
122
|
action: "postback" | "url" | "say";
|
|
124
123
|
label: string;
|
|
125
124
|
}[];
|
|
125
|
+
title: string;
|
|
126
126
|
imageUrl?: string | undefined;
|
|
127
127
|
subtitle?: string | undefined;
|
|
128
128
|
}[];
|
|
129
129
|
}, {
|
|
130
130
|
items: {
|
|
131
|
-
title: string;
|
|
132
131
|
actions: {
|
|
133
132
|
value: string;
|
|
134
133
|
action: "postback" | "url" | "say";
|
|
135
134
|
label: string;
|
|
136
135
|
}[];
|
|
136
|
+
title: string;
|
|
137
137
|
imageUrl?: string | undefined;
|
|
138
138
|
subtitle?: string | undefined;
|
|
139
139
|
}[];
|
|
@@ -158,21 +158,21 @@ export declare const defaults: {
|
|
|
158
158
|
label: string;
|
|
159
159
|
}>, "many">;
|
|
160
160
|
}, "strip", import("@bpinternal/zui").ZodTypeAny, {
|
|
161
|
-
title: string;
|
|
162
161
|
actions: {
|
|
163
162
|
value: string;
|
|
164
163
|
action: "postback" | "url" | "say";
|
|
165
164
|
label: string;
|
|
166
165
|
}[];
|
|
166
|
+
title: string;
|
|
167
167
|
imageUrl?: string | undefined;
|
|
168
168
|
subtitle?: string | undefined;
|
|
169
169
|
}, {
|
|
170
|
-
title: string;
|
|
171
170
|
actions: {
|
|
172
171
|
value: string;
|
|
173
172
|
action: "postback" | "url" | "say";
|
|
174
173
|
label: string;
|
|
175
174
|
}[];
|
|
175
|
+
title: string;
|
|
176
176
|
imageUrl?: string | undefined;
|
|
177
177
|
subtitle?: string | undefined;
|
|
178
178
|
}>;
|
|
@@ -23,6 +23,12 @@ export type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) ex
|
|
|
23
23
|
export type ToSealedRecord<R extends Record<string, any>> = {
|
|
24
24
|
[K in keyof R as string extends K ? never : K]: R[K];
|
|
25
25
|
};
|
|
26
|
-
|
|
26
|
+
type NormalizeObject<T extends object> = T extends infer O ? {
|
|
27
27
|
[K in keyof O]: Normalize<O[K]>;
|
|
28
|
-
} : never
|
|
28
|
+
} : never;
|
|
29
|
+
export type Normalize<T> = T extends (...args: infer A) => infer R ? (...args: Normalize<A>) => Normalize<R> : T extends Array<infer E> ? Array<Normalize<E>> : T extends ReadonlyArray<infer E> ? ReadonlyArray<Normalize<E>> : T extends Promise<infer R> ? Promise<Normalize<R>> : T extends Buffer ? Buffer : T extends object ? NormalizeObject<T> : T;
|
|
30
|
+
type DeepPartialObject<T extends object> = T extends infer O ? {
|
|
31
|
+
[K in keyof O]?: DeepPartial<O[K]>;
|
|
32
|
+
} : never;
|
|
33
|
+
export type DeepPartial<T> = T extends (...args: infer A) => infer R ? (...args: DeepPartial<A>) => DeepPartial<R> : T extends Array<infer E> ? Array<DeepPartial<E>> : T extends ReadonlyArray<infer E> ? ReadonlyArray<DeepPartial<E>> : T extends Promise<infer R> ? Promise<DeepPartial<R>> : T extends Buffer ? Buffer : T extends object ? DeepPartialObject<T> : T;
|
|
34
|
+
export {};
|