@scout9/app 1.0.0-alpha.0.5.7 → 1.0.0-alpha.0.5.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/public.d.ts CHANGED
@@ -359,6 +359,12 @@ export type Agent = {
359
359
  model?: ("Scout9" | "bard" | "openai");
360
360
  transcripts?: Message[][] | undefined;
361
361
  audios?: any[] | undefined;
362
+ pmt?: {
363
+ tag?: string;
364
+ ingress: "auto" | "manual" | "app" | "webhook";
365
+ llm?: string;
366
+ scout9?: string;
367
+ }
362
368
  };
363
369
 
364
370
  export type AgentConfiguration = Agent & {id: string};
@@ -630,7 +636,7 @@ export type WorkflowEvent = {
630
636
  conversation: Conversation;
631
637
  context?: any;
632
638
  message: Message;
633
- agent: Omit<AgentConfiguration, 'transcripts' | 'audios' | 'includedLocations' | 'excludedLocations' | 'model' | 'context'>;
639
+ agent: Omit<AgentConfiguration, 'transcripts' | 'audios' | 'includedLocations' | 'excludedLocations' | 'model' | 'context' | 'pmt'>;
634
640
  customer: Customer;
635
641
  intent: IntentWorkflowEvent;
636
642
  stagnationCount: number;
@@ -657,7 +663,7 @@ export type WorkflowResponseSlotBase = {
657
663
  forwardNote?: string | undefined;
658
664
  instructions?: Instruction[] | undefined;
659
665
  removeInstructions?: string[] | undefined;
660
- message?: string | undefined;
666
+ message?: string | {content: string; transform?: boolean} | undefined;
661
667
  secondsDelay?: number | undefined;
662
668
  scheduled?: number | undefined;
663
669
  contextUpsert?: {
@@ -62,27 +62,11 @@ import MacroGlobals from './globals.js';
62
62
  * @property {WorkflowResponseSlotBaseWithKeywords[]} withoutCondition.instruction - Array of slots with keywords.
63
63
  */
64
64
 
65
- // export type WorkflowResponseSlotBase = {
66
- // /** Forward input information of a conversation */
67
- // forward?: Forward | undefined;
68
- // /** Note to provide to the agent, recommend using forward object api instead */
69
- // forwardNote?: string | undefined;
70
- // instructions?: Instruction[] | undefined;
71
- // removeInstructions?: string[] | undefined;
72
- // message?: string | undefined;
73
- // secondsDelay?: number | undefined;
74
- // scheduled?: number | undefined;
75
- // contextUpsert?: {
76
- // [x: string]: any;
77
- // } | undefined;
78
- // resetIntent?: boolean | undefined;
79
- // followup?: Followup | undefined;
80
- // };
81
-
82
65
 
83
66
  /**
84
67
  * Event macros to be used inside your scout9 auto reply workflows
85
68
  * @typedef {Object} EventMacros
69
+ * @property {function(string, [ContextExamples]): EventMacros} context
86
70
  * @property {function(Record<string, any>): EventMacros} upsert
87
71
  * @property {function(string, (Date | string | OptionsFollowup)): EventMacros} followup
88
72
  * @property {AnticipateFunction} anticipate
@@ -287,6 +271,13 @@ function EventMacrosFactory() {
287
271
  return this;
288
272
  },
289
273
 
274
+ /**
275
+ * Same as the context
276
+ */
277
+ // context() {
278
+ // @TODO insert context proxy
279
+ // },
280
+
290
281
  /**
291
282
  * If conversation is not stagnant, return instructions to guide next auto reply response, otherwise it will forward the conversation
292
283
  * @param {string} instruction
@@ -45,7 +45,12 @@ export const AgentSchema = z.object({
45
45
  excludedLocations: z.array(z.string({description: 'Locations the agent is excluded from'})).optional(),
46
46
  model: z.enum(['Scout9', 'bard', 'openai']).optional().default('openai'),
47
47
  transcripts: z.array(z.array(MessageSchema)).optional(),
48
- audios: z.array(z.any()).optional()
48
+ audios: z.array(z.any()).optional(),
49
+ pmt: z.object({
50
+ tag: z.string().optional(),
51
+ ingress: z.enum(["auto", "manual", "app", "workflow"]),
52
+ llm: z.string().optional(),
53
+ }).optional()
49
54
  });
50
55
 
51
56
  export const PersonaSchema = AgentSchema;
@@ -134,7 +134,8 @@ export const WorkflowEventSchema = z.object({
134
134
  includedLocations: true,
135
135
  excludedLocations: true,
136
136
  model: true,
137
- context: true
137
+ context: true,
138
+ pmt: true
138
139
  }),
139
140
  customer: CustomerSchema,
140
141
  intent: IntentWorkflowEventSchema,
@@ -151,7 +152,10 @@ export const WorkflowResponseSlotBaseSchema = z.object({
151
152
  .optional(),
152
153
  instructions: InstructionSchema.optional(),
153
154
  removeInstructions: z.array(z.string()).optional(),
154
- message: z.string().optional(),
155
+ message: z.union([z.string(), z.object({
156
+ content: z.string(),
157
+ transform: z.boolean().optional()
158
+ })]).optional(),
155
159
  secondsDelay: z.number().optional(),
156
160
  scheduled: z.number().optional(),
157
161
  contextUpsert: ConversationContext.optional(),
@@ -65,6 +65,12 @@
65
65
  * @returns {Promise<import('@scout9/admin').GenerateResponse>}
66
66
  */
67
67
 
68
+ /**
69
+ * @callback TransformerFun
70
+ * @param {import('@scout9/admin').PmtTransformRequest} data - data to generate from
71
+ * @returns {Promise<import('@scout9/admin').PmtTransformResponse>}
72
+ */
73
+
68
74
  /**
69
75
  * @callback IdGeneratorFun
70
76
  * @param {import('@scout9/app').Message['role']} prefix
@@ -84,6 +90,7 @@
84
90
  * @property {ParseFun} parser
85
91
  * @property {WorkflowFun} workflow
86
92
  * @property {GenerateFun} generator
93
+ * @property {TransformerFun} transformer
87
94
  * @property {IdGeneratorFun} idGenerator
88
95
  * @property {StatusCallback | undefined} [progress]
89
96
  */
@@ -110,6 +117,7 @@ export const Spirits = {
110
117
  parser,
111
118
  workflow,
112
119
  generator,
120
+ transformer,
113
121
  idGenerator,
114
122
  progress = (message, level, type, payload) => {
115
123
  },
@@ -349,6 +357,7 @@ export const Spirits = {
349
357
  }, []));
350
358
  const hasNoInstructions = slots.every(s => !s.instructions || (Array.isArray(s.instructions) && s.instructions.length === 0));
351
359
  const hasNoCustomMessage = slots.every(s => !s.message);
360
+ const messagesToTransform = slots.filter(s => !!s.message && typeof s.message === 'object' && !!s.message.transform);
352
361
  const previousLockAttempt = conversation.lockAttempts || 0; // Used to track
353
362
 
354
363
  if (hasNoInstructions && noNewContext) {
@@ -596,6 +605,43 @@ export const Spirits = {
596
605
  conversation = lockConversation(conversation, 'API: ' + e.message);
597
606
  }
598
607
  }
608
+
609
+ if (messagesToTransform.length && transformer) {
610
+ try {
611
+ for (const messageToTransform of messagesToTransform) {
612
+ const transformResponse = await transformer({
613
+ message: messagesToTransform,
614
+ persona,
615
+ customer: customer.id,
616
+ messages,
617
+ context: context
618
+ });
619
+
620
+ progress('Generated response', 'success', undefined, undefined);
621
+ // Check if already had message
622
+ const agentMessages = messages.filter(m => m.role === 'agent');
623
+ const lastAgentMessage = agentMessages[agentMessages.length - 1];
624
+ if (lastAgentMessage && lastAgentMessage.content === transformResponse.message) {
625
+ // Error should not have happened
626
+ conversation = lockConversation(conversation, 'Duplicate message');
627
+ } else {
628
+ messages.push({
629
+ id: idGenerator('agent'),
630
+ role: 'agent',
631
+ content: transformResponse.message,
632
+ time: new Date().toISOString()
633
+ });
634
+ progress('Added agent message', 'info', 'ADD_MESSAGE', messages[messages.length - 1]);
635
+ }
636
+
637
+ }
638
+ } catch (e) {
639
+ console.error(`Locking conversation, error transforming response: ${e.message}`);
640
+ conversation = lockConversation(conversation, 'API: ' + e.message);
641
+ }
642
+ } else if (messagesToTransform.length) {
643
+ console.warn(`No transformer provided`)
644
+ }
599
645
  }
600
646
 
601
647
  progress('Parsing message', 'info', 'SET_PROCESSING', null);