@botpress/runtime 1.2.4 → 1.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/dist/_types/channels.d.ts +18 -0
  2. package/dist/_types/channels.d.ts.map +1 -0
  3. package/dist/_types/conversations.d.ts +9 -0
  4. package/dist/_types/conversations.d.ts.map +1 -0
  5. package/dist/_types/integrations.d.ts +13 -0
  6. package/dist/_types/integrations.d.ts.map +1 -0
  7. package/dist/definition.d.ts +1 -0
  8. package/dist/definition.d.ts.map +1 -1
  9. package/dist/definition.js +45914 -9
  10. package/dist/definition.js.map +4 -4
  11. package/dist/internal.d.ts +1 -0
  12. package/dist/internal.d.ts.map +1 -1
  13. package/dist/internal.js +277 -164
  14. package/dist/internal.js.map +4 -4
  15. package/dist/library.d.ts +5 -1
  16. package/dist/library.d.ts.map +1 -1
  17. package/dist/library.js +276 -164
  18. package/dist/library.js.map +4 -4
  19. package/dist/primitives/conversation-instance.d.ts +42 -0
  20. package/dist/primitives/conversation-instance.d.ts.map +1 -0
  21. package/dist/primitives/conversation.d.ts +30 -64
  22. package/dist/primitives/conversation.d.ts.map +1 -1
  23. package/dist/primitives/definition.d.ts +1 -1
  24. package/dist/primitives/definition.d.ts.map +1 -1
  25. package/dist/primitives/index.d.ts +3 -0
  26. package/dist/primitives/index.d.ts.map +1 -1
  27. package/dist/primitives/workflow-instance.d.ts +0 -2
  28. package/dist/primitives/workflow-instance.d.ts.map +1 -1
  29. package/dist/primitives/workflow.d.ts +1 -2
  30. package/dist/primitives/workflow.d.ts.map +1 -1
  31. package/dist/runtime/adk.d.ts +1 -1
  32. package/dist/runtime/adk.d.ts.map +1 -1
  33. package/dist/runtime/autonomous.d.ts.map +1 -1
  34. package/dist/runtime/handlers/conversation-matching.d.ts +17 -0
  35. package/dist/runtime/handlers/conversation-matching.d.ts.map +1 -0
  36. package/dist/runtime/handlers/conversation.d.ts.map +1 -1
  37. package/dist/runtime/handlers/event.d.ts.map +1 -1
  38. package/dist/runtime/tracked-state.d.ts +16 -5
  39. package/dist/runtime/tracked-state.d.ts.map +1 -1
  40. package/dist/runtime.js +311 -207
  41. package/dist/runtime.js.map +4 -4
  42. package/package.json +1 -1
package/dist/library.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { Autonomous } from "./runtime/autonomous";
2
2
  export { z } from "@botpress/sdk";
3
3
  export { context } from "./runtime/index";
4
- export { Action, Knowledge, Conversation, Trigger, Table, Workflow, Primitives, DataSource, } from "./primitives";
4
+ export { Action, Knowledge, Conversation, Trigger, Table, Workflow, Primitives, DataSource, BaseConversationInstance, } from "./primitives";
5
5
  export { isWorkflowDataRequest } from "./primitives/workflow-utils";
6
6
  export { Autonomous } from "./runtime/autonomous";
7
7
  export type Model = Autonomous.Model;
@@ -13,4 +13,8 @@ export { defineConfig } from "./define-config";
13
13
  export type { AgentConfig } from "./define-config";
14
14
  export { adk } from "./runtime/adk";
15
15
  export type { ADK, Project, Integration } from "./runtime/adk";
16
+ export type { ConversationDefinitions } from "./_types/conversations";
17
+ export type { WorkflowDefinitions } from "./_types/workflows";
18
+ export type { Triggers } from "./_types/triggers";
19
+ export type { Channels, ChannelSpec } from "./_types/channels";
16
20
  //# sourceMappingURL=library.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"library.d.ts","sourceRoot":"","sources":["../src/library.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAElD,OAAO,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAElC,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAE1C,OAAO,EACL,MAAM,EACN,SAAS,EACT,YAAY,EACZ,OAAO,EACP,KAAK,EACL,QAAQ,EACR,UAAU,EACV,UAAU,GACX,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,qBAAqB,EAAE,MAAM,6BAA6B,CAAC;AAEpE,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAGlD,MAAM,MAAM,KAAK,GAAG,UAAU,CAAC,KAAK,CAAC;AAErC,cAAc,SAAS,CAAC;AACxB,cAAc,UAAU,CAAC;AAEzB,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC5C,OAAO,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAE5C,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC/C,YAAY,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAEnD,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AACpC,YAAY,EAAE,GAAG,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC"}
1
+ {"version":3,"file":"library.d.ts","sourceRoot":"","sources":["../src/library.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAGlD,OAAO,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAElC,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAE1C,OAAO,EACL,MAAM,EACN,SAAS,EACT,YAAY,EACZ,OAAO,EACP,KAAK,EACL,QAAQ,EACR,UAAU,EACV,UAAU,EACV,wBAAwB,GACzB,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,qBAAqB,EAAE,MAAM,6BAA6B,CAAC;AAEpE,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAGlD,MAAM,MAAM,KAAK,GAAG,UAAU,CAAC,KAAK,CAAC;AAErC,cAAc,SAAS,CAAC;AACxB,cAAc,UAAU,CAAC;AAEzB,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC5C,OAAO,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAE5C,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC/C,YAAY,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAEnD,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AACpC,YAAY,EAAE,GAAG,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAG/D,YAAY,EAAE,uBAAuB,EAAE,MAAM,wBAAwB,CAAC;AACtE,YAAY,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AAC9D,YAAY,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAClD,YAAY,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC"}
package/dist/library.js CHANGED
@@ -48,7 +48,7 @@ var init_define_BUILD = __esm({
48
48
  var define_PACKAGE_VERSIONS_default;
49
49
  var init_define_PACKAGE_VERSIONS = __esm({
50
50
  "<define:__PACKAGE_VERSIONS__>"() {
51
- define_PACKAGE_VERSIONS_default = { runtime: "1.2.4", adk: "not-installed", sdk: "4.16.0", llmz: "0.0.26", zai: "2.1.16", cognitive: "0.1.47" };
51
+ define_PACKAGE_VERSIONS_default = { runtime: "1.2.5", adk: "not-installed", sdk: "4.16.0", llmz: "0.0.26", zai: "2.1.16", cognitive: "0.1.47" };
52
52
  }
53
53
  });
54
54
 
@@ -27463,6 +27463,47 @@ var require_indexv2 = __commonJS({
27463
27463
  }
27464
27464
  });
27465
27465
 
27466
+ // src/utilities/trigger-tags.ts
27467
+ var trigger_tags_exports = {};
27468
+ __export(trigger_tags_exports, {
27469
+ getTriggerSubscriptionTags: () => getTriggerSubscriptionTags,
27470
+ getTriggerTagName: () => getTriggerTagName,
27471
+ getTriggerTagValue: () => getTriggerTagValue,
27472
+ isConversationSubscribedToTrigger: () => isConversationSubscribedToTrigger
27473
+ });
27474
+ import crypto2 from "crypto";
27475
+ var hashString, getTriggerTagName, getTriggerTagValue, getTriggerSubscriptionTags, isConversationSubscribedToTrigger;
27476
+ var init_trigger_tags = __esm({
27477
+ "src/utilities/trigger-tags.ts"() {
27478
+ "use strict";
27479
+ init_define_BUILD();
27480
+ init_define_PACKAGE_VERSIONS();
27481
+ hashString = (str) => {
27482
+ return crypto2.createHash("md5").update(str).digest("hex").substring(0, 5).toUpperCase();
27483
+ };
27484
+ getTriggerTagName = (triggerName) => {
27485
+ return `trigger${hashString(triggerName)}`;
27486
+ };
27487
+ getTriggerTagValue = (key) => {
27488
+ return key ?? "*";
27489
+ };
27490
+ getTriggerSubscriptionTags = (triggerName, key) => {
27491
+ return {
27492
+ name: getTriggerTagName(triggerName),
27493
+ value: getTriggerTagValue(key)
27494
+ };
27495
+ };
27496
+ isConversationSubscribedToTrigger = (conversationTags, triggerName, triggerKey) => {
27497
+ const tagName = getTriggerTagName(triggerName);
27498
+ const tagValue = conversationTags[tagName];
27499
+ if (!tagValue) {
27500
+ return false;
27501
+ }
27502
+ return tagValue === "*" || tagValue === triggerKey;
27503
+ };
27504
+ }
27505
+ });
27506
+
27466
27507
  // ../../node_modules/.bun/@isaacs+balanced-match@4.0.1/node_modules/@isaacs/balanced-match/dist/esm/index.js
27467
27508
  var balanced, maybeMatch, range;
27468
27509
  var init_esm3 = __esm({
@@ -40218,9 +40259,6 @@ var Autonomous;
40218
40259
  Autonomous2.DefaultExit = _DefaultExit;
40219
40260
  function createKnowledgeSearchTool(knowledgeBases) {
40220
40261
  const kbNames = knowledgeBases.map((kb) => kb.name);
40221
- for (const kb of knowledgeBases) {
40222
- console.log(kb.name, kb.description);
40223
- }
40224
40262
  const description = knowledgeBases.map((kb) => `- "${kb.name}": ${kb.description || "No description"}`).join("\n");
40225
40263
  return new LlmzTool({
40226
40264
  name: "search_knowledge",
@@ -40245,6 +40283,8 @@ If the question is not related to the knowledge bases, do NOT use this tool.`.tr
40245
40283
  const { passages } = await client.searchFiles({
40246
40284
  query,
40247
40285
  withContext: true,
40286
+ includeBreadcrumb: true,
40287
+ contextDepth: 4,
40248
40288
  tags: {
40249
40289
  [WellKnownTags.knowledge.KNOWLEDGE]: "true",
40250
40290
  [WellKnownTags.knowledge.KNOWLEDGE_BASE_NAME]: kbNames
@@ -40288,12 +40328,12 @@ If the question is not related to the knowledge bases, do NOT use this tool.`.tr
40288
40328
  citationMetadata.sourceId = tags[WellKnownTags.knowledge.KNOWLEDGE_SOURCE_ID];
40289
40329
  }
40290
40330
  const { tag } = citations.registerSource(citationMetadata);
40291
- message.push(`<${tag} file="${p.file.key}">`);
40292
- message.push(`**${citationMetadata.title || p.file.key}**`);
40331
+ message.push(
40332
+ `<${tag} file="${p.file.key}" title="${citationMetadata.title || p.file.key}">`
40333
+ );
40293
40334
  message.push(p.content);
40294
40335
  message.push(`</${tag}>`);
40295
40336
  }
40296
- console.log(message.join("\n").trim());
40297
40337
  throw new Autonomous2.ThinkSignal(
40298
40338
  `We got the search results. When answering the question, you MUST add inline the citations used (eg: "Yes, the price is $10${example} ...")`,
40299
40339
  message.join("\n").trim()
@@ -40990,36 +41030,9 @@ function installStructuredLogging() {
40990
41030
  }
40991
41031
 
40992
41032
  // src/runtime.ts
41033
+ init_trigger_tags();
40993
41034
  import { z as z4 } from "@botpress/sdk";
40994
41035
 
40995
- // src/utilities/trigger-tags.ts
40996
- init_define_BUILD();
40997
- init_define_PACKAGE_VERSIONS();
40998
- import crypto2 from "crypto";
40999
- var hashString = (str) => {
41000
- return crypto2.createHash("md5").update(str).digest("hex").substring(0, 5).toUpperCase();
41001
- };
41002
- var getTriggerTagName = (triggerName) => {
41003
- return `trigger${hashString(triggerName)}`;
41004
- };
41005
- var getTriggerTagValue = (key) => {
41006
- return key ?? "*";
41007
- };
41008
- var getTriggerSubscriptionTags = (triggerName, key) => {
41009
- return {
41010
- name: getTriggerTagName(triggerName),
41011
- value: getTriggerTagValue(key)
41012
- };
41013
- };
41014
- var isConversationSubscribedToTrigger = (conversationTags, triggerName, triggerKey) => {
41015
- const tagName = getTriggerTagName(triggerName);
41016
- const tagValue = conversationTags[tagName];
41017
- if (!tagValue) {
41018
- return false;
41019
- }
41020
- return tagValue === "*" || tagValue === triggerKey;
41021
- };
41022
-
41023
41036
  // src/types.ts
41024
41037
  init_define_BUILD();
41025
41038
  init_define_PACKAGE_VERSIONS();
@@ -41621,12 +41634,6 @@ var BaseWorkflowInstance = class _BaseWorkflowInstance {
41621
41634
  workflow;
41622
41635
  // @internal
41623
41636
  TrackedState;
41624
- set state(state) {
41625
- this.TrackedState.value = state;
41626
- }
41627
- get state() {
41628
- return this.TrackedState.value;
41629
- }
41630
41637
  constructor(workflow, client) {
41631
41638
  const definition = adk.project.workflows.find(
41632
41639
  (w) => w.name === workflow.name
@@ -41636,7 +41643,7 @@ var BaseWorkflowInstance = class _BaseWorkflowInstance {
41636
41643
  client: client._inner,
41637
41644
  id: workflow.id,
41638
41645
  schema: definition?.stateSchema,
41639
- name: "workflowState"
41646
+ name: BUILT_IN_STATES.workflowState
41640
41647
  });
41641
41648
  this.id = workflow.id;
41642
41649
  this.name = workflow.name;
@@ -41767,14 +41774,25 @@ var BaseWorkflowInstance = class _BaseWorkflowInstance {
41767
41774
  "Workflow execution state is not loaded"
41768
41775
  );
41769
41776
  workflowExecutionState.value.executionCount++;
41777
+ if (!this.TrackedState.value) {
41778
+ this.TrackedState.value = {};
41779
+ }
41780
+ const trackedState = this.TrackedState;
41781
+ const stateProxy = new Proxy(this.TrackedState.value, {
41782
+ set(target, prop, value) {
41783
+ const result2 = Reflect.set(target, prop, value);
41784
+ trackedState.markDirty();
41785
+ return result2;
41786
+ }
41787
+ });
41770
41788
  const result = await context.run(
41771
41789
  {
41772
41790
  ...ctx,
41773
41791
  workflowControlContext
41774
41792
  },
41775
- async () => handler.call(this, {
41793
+ async () => handler({
41776
41794
  input: this.input,
41777
- state: this.state,
41795
+ state: stateProxy,
41778
41796
  step,
41779
41797
  client: this.client,
41780
41798
  execute: this.execute.bind(this)
@@ -41840,7 +41858,7 @@ function createWorkflowExecutionState(client, workflowId) {
41840
41858
  client,
41841
41859
  id: workflowId,
41842
41860
  schema: workflowExecutionContextSchema,
41843
- name: "workflowExecutionContext"
41861
+ name: BUILT_IN_STATES.workflowSteps
41844
41862
  });
41845
41863
  }
41846
41864
 
@@ -41867,8 +41885,6 @@ var BaseWorkflow = class {
41867
41885
  /** @internal */
41868
41886
  schedule;
41869
41887
  timeout = (0, import_ms.default)("5m");
41870
- // Runtime state
41871
- state;
41872
41888
  constructor(props) {
41873
41889
  this.name = props.name;
41874
41890
  if (props.description !== void 0) {
@@ -42231,10 +42247,15 @@ var EMPTY_STATE = {
42231
42247
  };
42232
42248
  var MAX_SWAP_FILE_SIZE = import_bytes2.default.parse("100MB");
42233
42249
  var BUILT_IN_STATES = {
42234
- user: "userState",
42250
+ /** Generic conversation-specific state (user-defined per conversation) */
42235
42251
  conversation: "state",
42252
+ /** User-specific state (persists across conversations per user) */
42253
+ user: "userState",
42254
+ /** Bot-wide global state (persists across all conversations) */
42236
42255
  bot: "botState",
42256
+ /** Workflow-specific state (persists across workflow executions) */
42237
42257
  workflowState: "workflowState",
42258
+ /** Workflow cached steps executions */
42238
42259
  workflowSteps: "workflowSteps"
42239
42260
  };
42240
42261
  var TrackedState = class _TrackedState {
@@ -42307,6 +42328,7 @@ var TrackedState = class _TrackedState {
42307
42328
  const client = context.get("client")._inner;
42308
42329
  const botId = context.get("botId", { optional: true });
42309
42330
  const user2 = context.get("user", { optional: true });
42331
+ const conversation = context.get("conversation", { optional: true });
42310
42332
  if (botId) {
42311
42333
  _TrackedState.create({
42312
42334
  client,
@@ -42325,6 +42347,23 @@ var TrackedState = class _TrackedState {
42325
42347
  schema: adk.project.config.user?.state || z12.object({})
42326
42348
  });
42327
42349
  }
42350
+ if (conversation) {
42351
+ const definition = adk.project.conversations.find((c) => {
42352
+ const def = c.getDefinition();
42353
+ if (typeof def.channel === "string") {
42354
+ return def.channel === conversation.channel || def.channel === "*";
42355
+ } else {
42356
+ return def.channel.includes(conversation.channel);
42357
+ }
42358
+ });
42359
+ _TrackedState.create({
42360
+ client,
42361
+ name: BUILT_IN_STATES.conversation,
42362
+ type: "conversation",
42363
+ id: conversation.id,
42364
+ schema: definition?.schema || z12.object({})
42365
+ });
42366
+ }
42328
42367
  const states = context.get("states", { optional: true });
42329
42368
  const promises = Promise.allSettled(
42330
42369
  states?.map((state) => state.load()) ?? []
@@ -42371,16 +42410,23 @@ var TrackedState = class _TrackedState {
42371
42410
  } else {
42372
42411
  this.value = value;
42373
42412
  }
42374
- if ((this.value == null || this.value === void 0) && this.state && "parse" in this.state) {
42375
- try {
42376
- this.value = this.state.parse({});
42377
- this._isDirty = true;
42378
- } catch (error) {
42413
+ if (this.value == null || this.value === void 0) {
42414
+ if (this.state && "parse" in this.state) {
42379
42415
  try {
42380
- this.value = this.state.parse(void 0);
42416
+ this.value = this.state.parse({});
42381
42417
  this._isDirty = true;
42382
- } catch {
42418
+ } catch (error) {
42419
+ try {
42420
+ this.value = this.state.parse(void 0);
42421
+ this._isDirty = true;
42422
+ } catch {
42423
+ this.value = {};
42424
+ this._isDirty = true;
42425
+ }
42383
42426
  }
42427
+ } else {
42428
+ this.value = {};
42429
+ this._isDirty = true;
42384
42430
  }
42385
42431
  }
42386
42432
  try {
@@ -42976,24 +43022,145 @@ init_define_BUILD();
42976
43022
  init_define_PACKAGE_VERSIONS();
42977
43023
  import { z as z16 } from "@botpress/sdk";
42978
43024
  import { setTimeout as setTimeout2 } from "node:timers/promises";
43025
+
43026
+ // src/primitives/conversation-instance.ts
43027
+ init_define_BUILD();
43028
+ init_define_PACKAGE_VERSIONS();
43029
+ var BaseConversationInstance = class {
43030
+ id;
43031
+ channel;
43032
+ integration;
43033
+ tags;
43034
+ conversation;
43035
+ // @internal
43036
+ client;
43037
+ // @internal
43038
+ TrackedState;
43039
+ constructor(conversation, client) {
43040
+ this.id = conversation.id;
43041
+ this.channel = conversation.channel;
43042
+ this.integration = conversation.integration;
43043
+ this.tags = conversation.tags;
43044
+ this.conversation = conversation;
43045
+ this.client = client;
43046
+ const states = context.get("states", { optional: true });
43047
+ const existingState = states?.find(
43048
+ (s) => s.type === "conversation" && s.id === conversation.id && s.name === BUILT_IN_STATES.conversation
43049
+ );
43050
+ if (!existingState) {
43051
+ throw new Error(
43052
+ `Conversation state not found for conversation ${conversation.id}. Make sure TrackedState.loadAll() is called before creating conversation instances.`
43053
+ );
43054
+ }
43055
+ this.TrackedState = existingState;
43056
+ }
43057
+ /**
43058
+ * Send a message to this conversation
43059
+ */
43060
+ async send(message) {
43061
+ try {
43062
+ const chat = context.get("chat");
43063
+ await trackPromise(
43064
+ chat.sendMessage({
43065
+ type: message.type,
43066
+ payload: message.payload
43067
+ })
43068
+ );
43069
+ } catch (err) {
43070
+ console.error("Error sending message in conversation:", err);
43071
+ }
43072
+ }
43073
+ /**
43074
+ * Start typing indicator
43075
+ */
43076
+ async startTyping() {
43077
+ const mapping = InterfaceMappings.getIntegrationAction(
43078
+ "typingIndicator",
43079
+ "startTypingIndicator",
43080
+ this.integration
43081
+ );
43082
+ if (mapping) {
43083
+ const message = context.get("message", { optional: true });
43084
+ await this.client.callAction({
43085
+ type: mapping,
43086
+ input: {
43087
+ conversationId: this.id,
43088
+ messageId: message?.id
43089
+ }
43090
+ }).catch(() => {
43091
+ });
43092
+ }
43093
+ }
43094
+ /**
43095
+ * Stop typing indicator
43096
+ */
43097
+ async stopTyping() {
43098
+ const mapping = InterfaceMappings.getIntegrationAction(
43099
+ "typingIndicator",
43100
+ "stopTypingIndicator",
43101
+ this.integration
43102
+ );
43103
+ if (mapping) {
43104
+ const message = context.get("message", { optional: true });
43105
+ await this.client.callAction({
43106
+ type: mapping,
43107
+ input: {
43108
+ conversationId: this.id,
43109
+ messageId: message?.id
43110
+ }
43111
+ }).catch(() => {
43112
+ });
43113
+ }
43114
+ }
43115
+ /**
43116
+ * Subscribe to a trigger
43117
+ */
43118
+ async subscribeToTrigger(triggerName, key) {
43119
+ const { getTriggerSubscriptionTags: getTriggerSubscriptionTags2, isConversationSubscribedToTrigger: isConversationSubscribedToTrigger2 } = await Promise.resolve().then(() => (init_trigger_tags(), trigger_tags_exports));
43120
+ if (isConversationSubscribedToTrigger2(this.tags, triggerName, key)) {
43121
+ return;
43122
+ }
43123
+ const { name, value } = getTriggerSubscriptionTags2(triggerName, key);
43124
+ await this.client.updateConversation({
43125
+ id: this.id,
43126
+ tags: {
43127
+ [name]: value
43128
+ }
43129
+ });
43130
+ }
43131
+ /**
43132
+ * Unsubscribe from a trigger
43133
+ */
43134
+ async unsubscribeFromTrigger(triggerName, key) {
43135
+ const { getTriggerSubscriptionTags: getTriggerSubscriptionTags2, isConversationSubscribedToTrigger: isConversationSubscribedToTrigger2 } = await Promise.resolve().then(() => (init_trigger_tags(), trigger_tags_exports));
43136
+ if (!isConversationSubscribedToTrigger2(this.tags, triggerName, key)) {
43137
+ return;
43138
+ }
43139
+ const { name } = getTriggerSubscriptionTags2(triggerName, key);
43140
+ await this.client.updateConversation({
43141
+ id: this.id,
43142
+ tags: {
43143
+ [name]: ""
43144
+ }
43145
+ });
43146
+ }
43147
+ };
43148
+
43149
+ // src/primitives/conversation.ts
42979
43150
  var ConversationHandler = Symbol.for("conversation.handler");
42980
43151
  var Typings3;
42981
43152
  ((Typings8) => {
42982
43153
  Typings8.Primitive = "conversation";
42983
43154
  })(Typings3 || (Typings3 = {}));
42984
43155
  var BaseConversation = class {
42985
- integration;
42986
43156
  channel;
42987
43157
  /** @internal */
42988
43158
  schema;
42989
43159
  #handler;
42990
- #state;
42991
43160
  #startFromTrigger;
42992
- #interruptionSignal;
42993
43161
  constructor(props) {
42994
43162
  this.channel = props.channel;
42995
43163
  this.schema = props.state ?? z16.object({}).passthrough();
42996
- this.integration = props.channel.split(".")[0];
42997
43164
  this.#handler = props.handler;
42998
43165
  if (props.startFromTrigger) {
42999
43166
  this.#startFromTrigger = props.startFromTrigger;
@@ -43005,31 +43172,34 @@ var BaseConversation = class {
43005
43172
  }
43006
43173
  /** @internal */
43007
43174
  getDefinition() {
43008
- return {
43009
- type: "conversation",
43010
- channel: this.channel
43011
- };
43012
- }
43013
- set state(state) {
43014
- this.#state = state;
43015
- }
43016
- get state() {
43017
- return this.#state;
43018
- }
43019
- get id() {
43020
- return context.get("conversation").id;
43021
- }
43022
- get tags() {
43023
- return context.get("conversation").tags;
43175
+ if (this.channel === "*") {
43176
+ return {
43177
+ type: "conversation",
43178
+ channel: "*"
43179
+ };
43180
+ } else if (Array.isArray(this.channel)) {
43181
+ return {
43182
+ type: "conversation",
43183
+ channel: this.channel
43184
+ };
43185
+ } else {
43186
+ return {
43187
+ type: "conversation",
43188
+ channel: this.channel
43189
+ };
43190
+ }
43024
43191
  }
43025
- // TODO: separate the handler from the conversation definition
43026
- // TODO: each execution should have its own instance to avoid state conflicts etc
43192
+ /** @internal */
43027
43193
  async [ConversationHandler]() {
43028
43194
  const message = context.get("message", { optional: true });
43029
43195
  const event = context.get("event", { optional: true });
43030
43196
  const chat = context.get("chat");
43031
43197
  const client = context.get("client");
43032
- const conversationId = this.id;
43198
+ const botpressConversation = context.get("conversation");
43199
+ const conversationInstance = new BaseConversationInstance(
43200
+ botpressConversation,
43201
+ client
43202
+ );
43033
43203
  let type;
43034
43204
  let requestObject = void 0;
43035
43205
  if (message) {
@@ -43054,11 +43224,10 @@ var BaseConversation = class {
43054
43224
  type = "event";
43055
43225
  }
43056
43226
  const controller = new AbortController();
43057
- this.#interruptionSignal = controller.signal;
43058
43227
  void span(
43059
43228
  "interruption.check",
43060
43229
  {
43061
- conversationId
43230
+ conversationId: conversationInstance.id
43062
43231
  },
43063
43232
  async (s) => {
43064
43233
  async function checkNewUserMessage() {
@@ -43066,7 +43235,7 @@ var BaseConversation = class {
43066
43235
  return;
43067
43236
  }
43068
43237
  const { events } = await client.listEvents({
43069
- conversationId,
43238
+ conversationId: conversationInstance.id,
43070
43239
  status: "pending"
43071
43240
  });
43072
43241
  const newEvents = events.filter(
@@ -43122,100 +43291,36 @@ var BaseConversation = class {
43122
43291
  const execute = Autonomous.createExecute({
43123
43292
  mode: "chat",
43124
43293
  defaultModel: adk.project.config.defaultModels.autonomous,
43125
- ...this.#interruptionSignal && {
43126
- interruption: this.#interruptionSignal
43294
+ interruption: controller.signal
43295
+ });
43296
+ if (!conversationInstance.TrackedState.value) {
43297
+ conversationInstance.TrackedState.value = {};
43298
+ }
43299
+ const stateProxy = new Proxy(conversationInstance.TrackedState.value, {
43300
+ set(target, prop, value) {
43301
+ const result = Reflect.set(target, prop, value);
43302
+ conversationInstance.TrackedState.markDirty();
43303
+ return result;
43127
43304
  }
43128
43305
  });
43129
- await this.#handler.call(this, {
43306
+ await this.#handler({
43130
43307
  type,
43131
43308
  message,
43132
43309
  event,
43133
43310
  request: requestObject,
43311
+ conversation: conversationInstance,
43312
+ state: stateProxy,
43313
+ client,
43134
43314
  execute
43135
43315
  });
43136
43316
  controller.abort();
43137
43317
  }
43138
- async subscribeToTrigger(triggerName, key) {
43139
- if (isConversationSubscribedToTrigger(this.tags, triggerName, key)) {
43140
- return;
43141
- }
43142
- const conversation = context.get("conversation");
43143
- const client = context.get("client");
43144
- const { name, value } = getTriggerSubscriptionTags(triggerName, key);
43145
- await client.updateConversation({
43146
- id: conversation.id,
43147
- tags: {
43148
- [name]: value
43149
- }
43150
- });
43151
- }
43152
- async unsubscribeFromTrigger(triggerName, key) {
43153
- if (!isConversationSubscribedToTrigger(this.tags, triggerName, key)) {
43154
- return;
43155
- }
43156
- const conversation = context.get("conversation");
43157
- const client = context.get("client");
43158
- const { name } = getTriggerSubscriptionTags(triggerName, key);
43159
- await client.updateConversation({
43160
- id: conversation.id,
43161
- tags: {
43162
- [name]: ""
43163
- }
43164
- });
43165
- }
43166
- async startTyping() {
43167
- const conversation = context.get("conversation", { optional: true });
43168
- const message = context.get("message", { optional: true });
43169
- const mapping = InterfaceMappings.getIntegrationAction(
43170
- "typingIndicator",
43171
- "startTypingIndicator",
43172
- conversation?.integration
43173
- );
43174
- if (conversation && mapping) {
43175
- await context.get("client").callAction({
43176
- type: mapping,
43177
- input: {
43178
- conversationId: conversation.id,
43179
- messageId: message?.id
43180
- }
43181
- }).catch(() => {
43182
- });
43183
- }
43184
- }
43185
- async stopTyping() {
43186
- const conversation = context.get("conversation", { optional: true });
43187
- const message = context.get("message", { optional: true });
43188
- const mapping = InterfaceMappings.getIntegrationAction(
43189
- "typingIndicator",
43190
- "stopTypingIndicator",
43191
- conversation?.integration
43192
- );
43193
- if (conversation && mapping) {
43194
- await context.get("client").callAction({
43195
- type: mapping,
43196
- input: {
43197
- conversationId: conversation.id,
43198
- messageId: message?.id
43199
- }
43200
- }).catch(() => {
43201
- });
43202
- }
43203
- }
43204
- async send(message) {
43205
- try {
43206
- const chat = context.get("chat");
43207
- await trackPromise(
43208
- chat.sendMessage({
43209
- type: message.type,
43210
- payload: message.payload
43211
- })
43212
- );
43213
- } catch (err) {
43214
- console.error("Error getting context in conversation.send:", err);
43215
- }
43216
- }
43217
43318
  };
43218
43319
 
43320
+ // src/runtime/handlers/conversation-matching.ts
43321
+ init_define_BUILD();
43322
+ init_define_PACKAGE_VERSIONS();
43323
+
43219
43324
  // src/runtime/handlers/trigger.ts
43220
43325
  init_define_BUILD();
43221
43326
  init_define_PACKAGE_VERSIONS();
@@ -43240,7 +43345,12 @@ var Definitions;
43240
43345
  ((Definitions2) => {
43241
43346
  const conversationDefinitionSchema = z17.object({
43242
43347
  type: z17.literal("conversation"),
43243
- channel: z17.string().min(1, "Channel must be a non-empty string").max(255, "Channel must be less than 255 characters").regex(/^[a-zA-Z0-9._-]+$/, "Channel must be a valid identifier")
43348
+ channel: z17.union([
43349
+ z17.string().min(1, "Channel must be a non-empty string").max(255, "Channel must be less than 255 characters").regex(/^(\*|[a-zA-Z0-9._-]+)$/, "Channel must be a valid identifier or glob '*'"),
43350
+ z17.array(
43351
+ z17.string().min(1, "Channel must be a non-empty string").max(255, "Channel must be less than 255 characters").regex(/^[a-zA-Z0-9._-]+$/, "Channel must be a valid identifier")
43352
+ )
43353
+ ])
43244
43354
  });
43245
43355
  const workflowDefinitionSchema = z17.object({
43246
43356
  type: z17.literal("workflow"),
@@ -45864,6 +45974,7 @@ var Primitives;
45864
45974
  ((Primitives2) => {
45865
45975
  Primitives2.Definitions = Definitions;
45866
45976
  Primitives2.BaseConversation = BaseConversation;
45977
+ Primitives2.BaseConversationInstance = BaseConversationInstance;
45867
45978
  Primitives2.Conversation = Typings3;
45868
45979
  Primitives2.BaseKnowledge = BaseKnowledge;
45869
45980
  Primitives2.Knowledge = Typings4;
@@ -45892,6 +46003,7 @@ init_define_PACKAGE_VERSIONS();
45892
46003
  export {
45893
46004
  Action,
45894
46005
  Autonomous,
46006
+ BaseConversationInstance,
45895
46007
  BaseConversation as Conversation,
45896
46008
  DataSource2 as DataSource,
45897
46009
  Errors,