@botpress/runtime 1.13.16 → 1.13.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/definition.d.ts +1 -0
- package/dist/definition.d.ts.map +1 -1
- package/dist/definition.js +2935 -2322
- package/dist/definition.js.map +4 -4
- package/dist/internal.js +632 -21
- package/dist/internal.js.map +3 -3
- package/dist/library.d.ts +1 -0
- package/dist/library.d.ts.map +1 -1
- package/dist/library.js +633 -21
- package/dist/library.js.map +3 -3
- package/dist/primitives/conversation-instance.d.ts.map +1 -1
- package/dist/primitives/conversation.d.ts +11 -0
- package/dist/primitives/conversation.d.ts.map +1 -1
- package/dist/runtime/autonomous.d.ts +1 -2
- package/dist/runtime/autonomous.d.ts.map +1 -1
- package/dist/runtime/chat/chat.d.ts +18 -18
- package/dist/runtime/chat/chat.d.ts.map +1 -1
- package/dist/runtime/chat/index.d.ts +1 -1
- package/dist/runtime/chat/index.d.ts.map +1 -1
- package/dist/runtime/chat/transcript.d.ts +1 -0
- package/dist/runtime/chat/transcript.d.ts.map +1 -1
- package/dist/runtime/context/context.d.ts +2 -2
- package/dist/runtime/context/context.d.ts.map +1 -1
- package/dist/runtime/handlers/conversation.d.ts.map +1 -1
- package/dist/runtime/handlers/event.d.ts.map +1 -1
- package/dist/runtime/tracked-tags.d.ts +21 -0
- package/dist/runtime/tracked-tags.d.ts.map +1 -1
- package/dist/runtime.js +4327 -4232
- package/dist/runtime.js.map +4 -4
- package/package.json +1 -1
package/dist/internal.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.13.
|
|
51
|
+
define_PACKAGE_VERSIONS_default = { runtime: "1.13.17", adk: "1.13.17", sdk: "5.0.2", llmz: "0.0.37", zai: "2.5.6", cognitive: "0.3.3" };
|
|
52
52
|
}
|
|
53
53
|
});
|
|
54
54
|
|
|
@@ -36393,7 +36393,13 @@ var init_tracked_state = __esm({
|
|
|
36393
36393
|
function isSystemTag(key) {
|
|
36394
36394
|
return key.includes(":");
|
|
36395
36395
|
}
|
|
36396
|
-
|
|
36396
|
+
function isBuiltInTag(key) {
|
|
36397
|
+
return key in BUILT_IN_TAGS.bot || key in BUILT_IN_TAGS.user || key in BUILT_IN_TAGS.conversation || key in BUILT_IN_TAGS.message || key in BUILT_IN_TAGS.workflow;
|
|
36398
|
+
}
|
|
36399
|
+
function getBuiltInTagsForType(type) {
|
|
36400
|
+
return Object.keys(BUILT_IN_TAGS[type]);
|
|
36401
|
+
}
|
|
36402
|
+
var BUILT_IN_TAGS, TrackedTags;
|
|
36397
36403
|
var init_tracked_tags = __esm({
|
|
36398
36404
|
"src/runtime/tracked-tags.ts"() {
|
|
36399
36405
|
"use strict";
|
|
@@ -36402,6 +36408,19 @@ var init_tracked_tags = __esm({
|
|
|
36402
36408
|
init_context();
|
|
36403
36409
|
init_tracing();
|
|
36404
36410
|
init_adk();
|
|
36411
|
+
BUILT_IN_TAGS = {
|
|
36412
|
+
conversation: {
|
|
36413
|
+
/** Last synced message timestamp - automatically updated when messages are processed */
|
|
36414
|
+
adkSyncTs: {
|
|
36415
|
+
title: "ADK Sync Timestamp",
|
|
36416
|
+
description: "Built-in: Last synced message timestamp. Automatically updated by the ADK when messages are processed."
|
|
36417
|
+
}
|
|
36418
|
+
},
|
|
36419
|
+
user: {},
|
|
36420
|
+
bot: {},
|
|
36421
|
+
message: {},
|
|
36422
|
+
workflow: {}
|
|
36423
|
+
};
|
|
36405
36424
|
TrackedTags = class _TrackedTags {
|
|
36406
36425
|
type;
|
|
36407
36426
|
id;
|
|
@@ -36431,10 +36450,18 @@ var init_tracked_tags = __esm({
|
|
|
36431
36450
|
return match2;
|
|
36432
36451
|
}
|
|
36433
36452
|
const instance = new _TrackedTags(props);
|
|
36453
|
+
const builtInTagKeys = getBuiltInTagsForType(props.type);
|
|
36454
|
+
const builtInTagDefaults = {};
|
|
36455
|
+
for (const key of builtInTagKeys) {
|
|
36456
|
+
builtInTagDefaults[key] = void 0;
|
|
36457
|
+
}
|
|
36434
36458
|
if (props.initialTags) {
|
|
36435
|
-
instance._tags = { ...props.initialTags };
|
|
36436
|
-
instance._initialTags = { ...props.initialTags };
|
|
36459
|
+
instance._tags = { ...builtInTagDefaults, ...props.initialTags };
|
|
36460
|
+
instance._initialTags = { ...builtInTagDefaults, ...props.initialTags };
|
|
36437
36461
|
instance._loaded = true;
|
|
36462
|
+
} else {
|
|
36463
|
+
instance._tags = { ...builtInTagDefaults };
|
|
36464
|
+
instance._initialTags = { ...builtInTagDefaults };
|
|
36438
36465
|
}
|
|
36439
36466
|
tags?.push(instance);
|
|
36440
36467
|
return instance;
|
|
@@ -36533,9 +36560,14 @@ var init_tracked_tags = __esm({
|
|
|
36533
36560
|
id: this.id
|
|
36534
36561
|
},
|
|
36535
36562
|
async () => {
|
|
36563
|
+
const builtInTagKeys = getBuiltInTagsForType(this.type);
|
|
36564
|
+
const builtInTagDefaults = {};
|
|
36565
|
+
for (const key of builtInTagKeys) {
|
|
36566
|
+
builtInTagDefaults[key] = void 0;
|
|
36567
|
+
}
|
|
36536
36568
|
const tags = await this.fetchTags();
|
|
36537
|
-
this._tags = { ...tags };
|
|
36538
|
-
this._initialTags = { ...tags };
|
|
36569
|
+
this._tags = { ...builtInTagDefaults, ...tags };
|
|
36570
|
+
this._initialTags = { ...builtInTagDefaults, ...tags };
|
|
36539
36571
|
this._loaded = true;
|
|
36540
36572
|
}
|
|
36541
36573
|
);
|
|
@@ -36656,7 +36688,7 @@ var init_tracked_tags = __esm({
|
|
|
36656
36688
|
if (value === void 0 || isSystemTag(key)) {
|
|
36657
36689
|
continue;
|
|
36658
36690
|
}
|
|
36659
|
-
if (validKeys.size === 0 || validKeys.has(key)) {
|
|
36691
|
+
if (isBuiltInTag(key) || validKeys.size === 0 || validKeys.has(key)) {
|
|
36660
36692
|
tagsForApi[key] = value;
|
|
36661
36693
|
} else {
|
|
36662
36694
|
skippedTags.push(key);
|
|
@@ -36779,6 +36811,99 @@ var init_handlers = __esm({
|
|
|
36779
36811
|
});
|
|
36780
36812
|
|
|
36781
36813
|
// src/runtime/chat/truncate-object.ts
|
|
36814
|
+
function truncateObject(value, maxChars) {
|
|
36815
|
+
let currentSize = 0;
|
|
36816
|
+
function truncateValue(val) {
|
|
36817
|
+
if (currentSize >= maxChars) {
|
|
36818
|
+
return void 0;
|
|
36819
|
+
}
|
|
36820
|
+
if (val === null || val === void 0) {
|
|
36821
|
+
const size = 4;
|
|
36822
|
+
currentSize += size;
|
|
36823
|
+
return val;
|
|
36824
|
+
}
|
|
36825
|
+
if (typeof val === "boolean") {
|
|
36826
|
+
const size = val ? 4 : 5;
|
|
36827
|
+
currentSize += size;
|
|
36828
|
+
return val;
|
|
36829
|
+
}
|
|
36830
|
+
if (typeof val === "number") {
|
|
36831
|
+
const size = String(val).length;
|
|
36832
|
+
currentSize += size;
|
|
36833
|
+
return val;
|
|
36834
|
+
}
|
|
36835
|
+
if (typeof val === "string") {
|
|
36836
|
+
const availableChars2 = maxChars - currentSize;
|
|
36837
|
+
if (availableChars2 <= 0) {
|
|
36838
|
+
return void 0;
|
|
36839
|
+
}
|
|
36840
|
+
if (val.length <= availableChars2) {
|
|
36841
|
+
currentSize += val.length;
|
|
36842
|
+
return val;
|
|
36843
|
+
}
|
|
36844
|
+
const truncated2 = val.slice(0, availableChars2);
|
|
36845
|
+
currentSize += truncated2.length;
|
|
36846
|
+
return truncated2;
|
|
36847
|
+
}
|
|
36848
|
+
if (Array.isArray(val)) {
|
|
36849
|
+
currentSize += 2;
|
|
36850
|
+
const result2 = [];
|
|
36851
|
+
for (let i = 0; i < val.length; i++) {
|
|
36852
|
+
if (currentSize >= maxChars) {
|
|
36853
|
+
break;
|
|
36854
|
+
}
|
|
36855
|
+
if (i > 0) {
|
|
36856
|
+
currentSize += 1;
|
|
36857
|
+
}
|
|
36858
|
+
const truncated2 = truncateValue(val[i]);
|
|
36859
|
+
if (truncated2 !== void 0 || val[i] === void 0) {
|
|
36860
|
+
result2.push(truncated2);
|
|
36861
|
+
} else {
|
|
36862
|
+
break;
|
|
36863
|
+
}
|
|
36864
|
+
}
|
|
36865
|
+
return result2;
|
|
36866
|
+
}
|
|
36867
|
+
if (typeof val === "object") {
|
|
36868
|
+
currentSize += 2;
|
|
36869
|
+
const result2 = {};
|
|
36870
|
+
const entries = Object.entries(val);
|
|
36871
|
+
for (let i = 0; i < entries.length; i++) {
|
|
36872
|
+
if (currentSize >= maxChars) {
|
|
36873
|
+
break;
|
|
36874
|
+
}
|
|
36875
|
+
const [key, value2] = entries[i];
|
|
36876
|
+
if (i > 0) {
|
|
36877
|
+
currentSize += 1;
|
|
36878
|
+
}
|
|
36879
|
+
const keySize = key.length + 3;
|
|
36880
|
+
if (currentSize + keySize >= maxChars) {
|
|
36881
|
+
break;
|
|
36882
|
+
}
|
|
36883
|
+
currentSize += keySize;
|
|
36884
|
+
const truncatedValue = truncateValue(value2);
|
|
36885
|
+
if (truncatedValue !== void 0 || value2 === void 0) {
|
|
36886
|
+
result2[key] = truncatedValue;
|
|
36887
|
+
}
|
|
36888
|
+
}
|
|
36889
|
+
return result2;
|
|
36890
|
+
}
|
|
36891
|
+
const str = String(val);
|
|
36892
|
+
const availableChars = maxChars - currentSize;
|
|
36893
|
+
if (availableChars <= 0) {
|
|
36894
|
+
return void 0;
|
|
36895
|
+
}
|
|
36896
|
+
if (str.length <= availableChars) {
|
|
36897
|
+
currentSize += str.length;
|
|
36898
|
+
return str;
|
|
36899
|
+
}
|
|
36900
|
+
const truncated = str.slice(0, availableChars);
|
|
36901
|
+
currentSize += truncated.length;
|
|
36902
|
+
return truncated;
|
|
36903
|
+
}
|
|
36904
|
+
const result = truncateValue(value);
|
|
36905
|
+
return { result, size: currentSize };
|
|
36906
|
+
}
|
|
36782
36907
|
var init_truncate_object = __esm({
|
|
36783
36908
|
"src/runtime/chat/truncate-object.ts"() {
|
|
36784
36909
|
"use strict";
|
|
@@ -36788,6 +36913,46 @@ var init_truncate_object = __esm({
|
|
|
36788
36913
|
});
|
|
36789
36914
|
|
|
36790
36915
|
// src/runtime/chat/truncate-transcript.ts
|
|
36916
|
+
function truncateTranscript(transcript, options) {
|
|
36917
|
+
const { maxSize, maxSizePerItem } = options;
|
|
36918
|
+
const result = [];
|
|
36919
|
+
let totalSize = 0;
|
|
36920
|
+
for (let i = transcript.length - 1; i >= 0; i--) {
|
|
36921
|
+
const item = transcript[i];
|
|
36922
|
+
const itemSize = getItemSize(item);
|
|
36923
|
+
const effectiveSize = maxSizePerItem ? Math.min(itemSize, maxSizePerItem) : itemSize;
|
|
36924
|
+
if (totalSize + effectiveSize > maxSize) {
|
|
36925
|
+
break;
|
|
36926
|
+
}
|
|
36927
|
+
let processedItem = item;
|
|
36928
|
+
if (maxSizePerItem && itemSize > maxSizePerItem) {
|
|
36929
|
+
if ("content" in item && item.content) {
|
|
36930
|
+
processedItem = {
|
|
36931
|
+
...item,
|
|
36932
|
+
content: item.content.slice(0, maxSizePerItem)
|
|
36933
|
+
};
|
|
36934
|
+
} else if (item.role === "event" && item.payload) {
|
|
36935
|
+
const truncated = truncateObject(item.payload, maxSizePerItem);
|
|
36936
|
+
processedItem = {
|
|
36937
|
+
...item,
|
|
36938
|
+
payload: truncated.result
|
|
36939
|
+
};
|
|
36940
|
+
}
|
|
36941
|
+
}
|
|
36942
|
+
result.unshift(processedItem);
|
|
36943
|
+
totalSize += effectiveSize;
|
|
36944
|
+
}
|
|
36945
|
+
return result;
|
|
36946
|
+
}
|
|
36947
|
+
function getItemSize(item) {
|
|
36948
|
+
if ("content" in item && item.content) {
|
|
36949
|
+
return item.content.length;
|
|
36950
|
+
}
|
|
36951
|
+
if (item.role === "event" && item.payload) {
|
|
36952
|
+
return JSON.stringify(item.payload).length;
|
|
36953
|
+
}
|
|
36954
|
+
return 0;
|
|
36955
|
+
}
|
|
36791
36956
|
var init_truncate_transcript = __esm({
|
|
36792
36957
|
"src/runtime/chat/truncate-transcript.ts"() {
|
|
36793
36958
|
"use strict";
|
|
@@ -37108,12 +37273,13 @@ ${indent}`);
|
|
|
37108
37273
|
}
|
|
37109
37274
|
return value;
|
|
37110
37275
|
}
|
|
37111
|
-
var dedent;
|
|
37276
|
+
var dedent, dedent_default;
|
|
37112
37277
|
var init_dedent = __esm({
|
|
37113
37278
|
"../../node_modules/dedent/dist/dedent.mjs"() {
|
|
37114
37279
|
init_define_BUILD();
|
|
37115
37280
|
init_define_PACKAGE_VERSIONS();
|
|
37116
37281
|
dedent = createDedent({});
|
|
37282
|
+
dedent_default = dedent;
|
|
37117
37283
|
}
|
|
37118
37284
|
});
|
|
37119
37285
|
|
|
@@ -37128,6 +37294,9 @@ function isEventMessage(event) {
|
|
|
37128
37294
|
}
|
|
37129
37295
|
return false;
|
|
37130
37296
|
}
|
|
37297
|
+
function isMessage(message) {
|
|
37298
|
+
return message !== null && typeof message === "object" && "id" in message && "type" in message && "payload" in message;
|
|
37299
|
+
}
|
|
37131
37300
|
var init_events2 = __esm({
|
|
37132
37301
|
"src/utilities/events.ts"() {
|
|
37133
37302
|
"use strict";
|
|
@@ -37138,7 +37307,7 @@ var init_events2 = __esm({
|
|
|
37138
37307
|
|
|
37139
37308
|
// src/runtime/config.ts
|
|
37140
37309
|
import { limitConfigs as limitConfigs2 } from "@bpinternal/const";
|
|
37141
|
-
var Transcript;
|
|
37310
|
+
var Transcript, Analysis, Config;
|
|
37142
37311
|
var init_config = __esm({
|
|
37143
37312
|
"src/runtime/config.ts"() {
|
|
37144
37313
|
"use strict";
|
|
@@ -37154,12 +37323,23 @@ var init_config = __esm({
|
|
|
37154
37323
|
/** Max bytes for a single message in the transcript */
|
|
37155
37324
|
TRANSCRIPT_ITEM_MAX_BYTES: 1e4
|
|
37156
37325
|
};
|
|
37326
|
+
Analysis = {
|
|
37327
|
+
/** How frequently the analysis will be run */
|
|
37328
|
+
ANALYSIS_FREQUENCY_CRON: "*/5 * * * *",
|
|
37329
|
+
// Every 5 minutes
|
|
37330
|
+
/** How many conversations to process in parallel */
|
|
37331
|
+
CONCURRENT_ANALYSIS_LIMIT: 5
|
|
37332
|
+
};
|
|
37333
|
+
Config = {
|
|
37334
|
+
Transcript,
|
|
37335
|
+
Analysis
|
|
37336
|
+
};
|
|
37157
37337
|
}
|
|
37158
37338
|
});
|
|
37159
37339
|
|
|
37160
37340
|
// src/runtime/chat/components.ts
|
|
37161
37341
|
import { messages, z as z14 } from "@botpress/sdk";
|
|
37162
|
-
var DefaultMessageTypes, TextComponent, AudioComponent, ImageComponent, VideoComponent, LocationComponent, ChoiceComponent, DropdownComponent, CarouselComponent;
|
|
37342
|
+
var DefaultMessageTypes, TextComponent, AudioComponent, ImageComponent, VideoComponent, LocationComponent, ChoiceComponent, DropdownComponent, CarouselComponent, BUILT_IN_INTEGRATIONS, DefaultComponents;
|
|
37163
37343
|
var init_components = __esm({
|
|
37164
37344
|
"src/runtime/chat/components.ts"() {
|
|
37165
37345
|
"use strict";
|
|
@@ -37445,11 +37625,23 @@ yield <Message>
|
|
|
37445
37625
|
})
|
|
37446
37626
|
}
|
|
37447
37627
|
});
|
|
37628
|
+
BUILT_IN_INTEGRATIONS = ["webchat", "slack", "teams", "telegram", "whatsapp", "chat"];
|
|
37629
|
+
DefaultComponents = {
|
|
37630
|
+
Audio: AudioComponent,
|
|
37631
|
+
Image: ImageComponent,
|
|
37632
|
+
Video: VideoComponent,
|
|
37633
|
+
Location: LocationComponent,
|
|
37634
|
+
Choice: ChoiceComponent,
|
|
37635
|
+
Dropdown: DropdownComponent,
|
|
37636
|
+
Carousel: CarouselComponent,
|
|
37637
|
+
Text: TextComponent
|
|
37638
|
+
};
|
|
37448
37639
|
}
|
|
37449
37640
|
});
|
|
37450
37641
|
|
|
37451
37642
|
// src/runtime/chat/chat.ts
|
|
37452
|
-
import { Chat, isAnyComponent as isAnyComponent2 } from "llmz";
|
|
37643
|
+
import { Chat as _llmzChat, isAnyComponent as isAnyComponent2 } from "llmz";
|
|
37644
|
+
var Chat;
|
|
37453
37645
|
var init_chat = __esm({
|
|
37454
37646
|
"src/runtime/chat/chat.ts"() {
|
|
37455
37647
|
"use strict";
|
|
@@ -37463,6 +37655,426 @@ var init_chat = __esm({
|
|
|
37463
37655
|
init_adk();
|
|
37464
37656
|
init_config();
|
|
37465
37657
|
init_components();
|
|
37658
|
+
init_tracked_tags();
|
|
37659
|
+
Chat = class extends _llmzChat {
|
|
37660
|
+
_transcript;
|
|
37661
|
+
client;
|
|
37662
|
+
conversation;
|
|
37663
|
+
botId;
|
|
37664
|
+
logger;
|
|
37665
|
+
citations;
|
|
37666
|
+
trackedTags;
|
|
37667
|
+
componentRegistry = /* @__PURE__ */ new Map();
|
|
37668
|
+
constructor(context3) {
|
|
37669
|
+
super({
|
|
37670
|
+
components: async () => this.getComponents(),
|
|
37671
|
+
transcript: async () => this.fetchTranscript(),
|
|
37672
|
+
handler: async (message) => this.handle(message)
|
|
37673
|
+
});
|
|
37674
|
+
this.client = context3.client;
|
|
37675
|
+
this.conversation = context3.conversation;
|
|
37676
|
+
this.botId = context3.botId;
|
|
37677
|
+
this.logger = context3.logger;
|
|
37678
|
+
this.citations = context3.citations;
|
|
37679
|
+
this.trackedTags = TrackedTags.create({
|
|
37680
|
+
type: "conversation",
|
|
37681
|
+
client: this.client._inner,
|
|
37682
|
+
id: this.conversation.id,
|
|
37683
|
+
initialTags: this.conversation.tags
|
|
37684
|
+
});
|
|
37685
|
+
if (BUILT_IN_INTEGRATIONS.includes(this.conversation.integration)) {
|
|
37686
|
+
this.registerComponent({ component: DefaultComponents.Audio });
|
|
37687
|
+
this.registerComponent({ component: DefaultComponents.Image });
|
|
37688
|
+
this.registerComponent({ component: DefaultComponents.Video });
|
|
37689
|
+
this.registerComponent({ component: DefaultComponents.Carousel });
|
|
37690
|
+
this.registerComponent({ component: DefaultComponents.Choice });
|
|
37691
|
+
this.registerComponent({ component: DefaultComponents.Dropdown });
|
|
37692
|
+
this.registerComponent({ component: DefaultComponents.Location });
|
|
37693
|
+
} else {
|
|
37694
|
+
this.registerComponent({ component: DefaultComponents.Text });
|
|
37695
|
+
}
|
|
37696
|
+
}
|
|
37697
|
+
/**
|
|
37698
|
+
* Register a component with an optional handler
|
|
37699
|
+
*/
|
|
37700
|
+
registerComponent(registration) {
|
|
37701
|
+
const componentName = registration.component.definition.name.toLowerCase();
|
|
37702
|
+
this.componentRegistry.set(componentName, registration);
|
|
37703
|
+
return this;
|
|
37704
|
+
}
|
|
37705
|
+
/**
|
|
37706
|
+
* Remove a component by name
|
|
37707
|
+
*/
|
|
37708
|
+
removeComponent(name) {
|
|
37709
|
+
this.componentRegistry.delete(name.toLowerCase());
|
|
37710
|
+
return this;
|
|
37711
|
+
}
|
|
37712
|
+
/**
|
|
37713
|
+
* Get all registered components
|
|
37714
|
+
*/
|
|
37715
|
+
async getComponents() {
|
|
37716
|
+
return Array.from(this.componentRegistry.values()).map((reg) => reg.component);
|
|
37717
|
+
}
|
|
37718
|
+
/**
|
|
37719
|
+
* Clear the entire transcript
|
|
37720
|
+
*/
|
|
37721
|
+
async clearTranscript() {
|
|
37722
|
+
this._transcript = [];
|
|
37723
|
+
}
|
|
37724
|
+
/**
|
|
37725
|
+
* Prepend items to the transcript
|
|
37726
|
+
*/
|
|
37727
|
+
async prependToTranscript(items) {
|
|
37728
|
+
if (!this._transcript) {
|
|
37729
|
+
throw new Error("Transcript not loaded yet \u2013 please call fetchTranscript() first");
|
|
37730
|
+
}
|
|
37731
|
+
this._transcript = [...items, ...this._transcript];
|
|
37732
|
+
}
|
|
37733
|
+
/**
|
|
37734
|
+
* Get a copy of the current transcript
|
|
37735
|
+
*/
|
|
37736
|
+
async getTranscript() {
|
|
37737
|
+
if (!this._transcript) {
|
|
37738
|
+
await this.fetchTranscript();
|
|
37739
|
+
}
|
|
37740
|
+
return [...this._transcript];
|
|
37741
|
+
}
|
|
37742
|
+
/**
|
|
37743
|
+
* Replace the entire transcript
|
|
37744
|
+
*/
|
|
37745
|
+
async setTranscript(items) {
|
|
37746
|
+
this._transcript = [...items];
|
|
37747
|
+
}
|
|
37748
|
+
async fetchTranscript() {
|
|
37749
|
+
if (this._transcript) return this._transcript;
|
|
37750
|
+
return await span(
|
|
37751
|
+
"chat.fetchTranscript",
|
|
37752
|
+
{
|
|
37753
|
+
conversationId: this.conversation.id
|
|
37754
|
+
},
|
|
37755
|
+
async () => {
|
|
37756
|
+
const since = this.trackedTags.tags["adkSyncTs"] || "";
|
|
37757
|
+
const fetchUnseenMessages = async () => {
|
|
37758
|
+
return this.client._inner.list.messages({
|
|
37759
|
+
conversationId: this.conversation.id,
|
|
37760
|
+
afterDate: since ? new Date(since).toISOString() : void 0,
|
|
37761
|
+
beforeDate: ""
|
|
37762
|
+
}).collect({ limit: 250 }).then((res) => since ? res.filter((m) => new Date(m.createdAt) > new Date(since)) : res).then((res) => res.sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime()));
|
|
37763
|
+
};
|
|
37764
|
+
const fetchTranscriptMessages = async () => {
|
|
37765
|
+
const { state } = await this.client.getOrSetState({
|
|
37766
|
+
id: this.conversation.id,
|
|
37767
|
+
type: "conversation",
|
|
37768
|
+
name: "conversation",
|
|
37769
|
+
payload: { transcript: [] }
|
|
37770
|
+
});
|
|
37771
|
+
this._transcript = state.payload.transcript;
|
|
37772
|
+
};
|
|
37773
|
+
const [unseenMessages] = await Promise.all([fetchUnseenMessages(), fetchTranscriptMessages()]);
|
|
37774
|
+
if (unseenMessages.length > 0) {
|
|
37775
|
+
await Promise.allSettled(
|
|
37776
|
+
unseenMessages.map(async (msg) => {
|
|
37777
|
+
await this.addMessage(msg);
|
|
37778
|
+
})
|
|
37779
|
+
);
|
|
37780
|
+
}
|
|
37781
|
+
return this._transcript;
|
|
37782
|
+
}
|
|
37783
|
+
);
|
|
37784
|
+
}
|
|
37785
|
+
async compactTranscript() {
|
|
37786
|
+
return await span(
|
|
37787
|
+
"chat.compactTranscript",
|
|
37788
|
+
{
|
|
37789
|
+
conversationId: this.conversation.id
|
|
37790
|
+
},
|
|
37791
|
+
async () => {
|
|
37792
|
+
if (!this._transcript) {
|
|
37793
|
+
throw new Error("Transcript not loaded yet \u2013 please call fetchTranscript() first");
|
|
37794
|
+
}
|
|
37795
|
+
const items = this._transcript?.splice(
|
|
37796
|
+
0,
|
|
37797
|
+
// We want to keep the last N items as higher-precision context
|
|
37798
|
+
Math.max(this._transcript.length - Config.Transcript.SUMMARY_END_PADDING, 0)
|
|
37799
|
+
);
|
|
37800
|
+
try {
|
|
37801
|
+
const summary = await adk.zai.summarize(JSON.stringify(items, null, 2), {
|
|
37802
|
+
length: 250,
|
|
37803
|
+
prompt: dedent_default`
|
|
37804
|
+
You are a transcript summarizer tasked with compressing a long conversation between a user and an AI agent.
|
|
37805
|
+
Your goal is to drastically reduce the transcript length while retaining all key information, decisions, facts, user goals, and outputs.
|
|
37806
|
+
|
|
37807
|
+
Apply the following rules:
|
|
37808
|
+
- Collapse repetitive or verbose exchanges into concise summaries.
|
|
37809
|
+
- Remove fluff, filler words, greetings, small talk, and off-topic digressions.
|
|
37810
|
+
- If multiple turns convey the same intent or clarification, merge them.
|
|
37811
|
+
- Always preserve important questions, answers, decisions, tool calls, results, failures, corrections, and feedback.
|
|
37812
|
+
- Prefer bullet points, numbered lists, or structured sections if it increases clarity.
|
|
37813
|
+
- Assume the summary will be reused for resuming the session or training the assistant, so it must be precise, useful, and unambiguous.
|
|
37814
|
+
|
|
37815
|
+
The final output should feel like a high-value executive summary of a working session, not a chat log.
|
|
37816
|
+
`.trim()
|
|
37817
|
+
});
|
|
37818
|
+
this._transcript?.unshift({
|
|
37819
|
+
id: `summary-${Date.now()}`,
|
|
37820
|
+
role: "summary",
|
|
37821
|
+
content: summary
|
|
37822
|
+
});
|
|
37823
|
+
} catch (err) {
|
|
37824
|
+
this.logger.error("Error compacting transcript", err);
|
|
37825
|
+
this._transcript = [...items ?? [], ...this._transcript];
|
|
37826
|
+
}
|
|
37827
|
+
}
|
|
37828
|
+
);
|
|
37829
|
+
}
|
|
37830
|
+
getRemainingContextSpace() {
|
|
37831
|
+
if (!this._transcript) {
|
|
37832
|
+
throw new Error("Transcript not loaded yet \u2013 please call fetchTranscript() first");
|
|
37833
|
+
}
|
|
37834
|
+
const transcriptSize = JSON.stringify(this._transcript).length;
|
|
37835
|
+
return {
|
|
37836
|
+
messages: Math.max(0, Config.Transcript.SUMMARY_MAX_MESSAGES - this._transcript.length),
|
|
37837
|
+
bytes: Math.max(0, Config.Transcript.SUMMARY_MAX_BYTES - transcriptSize)
|
|
37838
|
+
};
|
|
37839
|
+
}
|
|
37840
|
+
shouldCompactTranscript() {
|
|
37841
|
+
const remaining = this.getRemainingContextSpace();
|
|
37842
|
+
return remaining.bytes <= 0 || remaining.messages <= 0;
|
|
37843
|
+
}
|
|
37844
|
+
async saveTranscript() {
|
|
37845
|
+
if (!this._transcript) return;
|
|
37846
|
+
return await span(
|
|
37847
|
+
"chat.saveTranscript",
|
|
37848
|
+
{
|
|
37849
|
+
conversationId: this.conversation.id
|
|
37850
|
+
},
|
|
37851
|
+
async () => {
|
|
37852
|
+
if (this.shouldCompactTranscript()) {
|
|
37853
|
+
await this.compactTranscript();
|
|
37854
|
+
}
|
|
37855
|
+
const truncated = truncateTranscript(this._transcript, {
|
|
37856
|
+
maxSize: Config.Transcript.SUMMARY_MAX_BYTES,
|
|
37857
|
+
maxSizePerItem: Config.Transcript.TRANSCRIPT_ITEM_MAX_BYTES
|
|
37858
|
+
});
|
|
37859
|
+
await this.client.setState({
|
|
37860
|
+
id: this.conversation.id,
|
|
37861
|
+
type: "conversation",
|
|
37862
|
+
name: "conversation",
|
|
37863
|
+
payload: { transcript: truncated }
|
|
37864
|
+
});
|
|
37865
|
+
}
|
|
37866
|
+
);
|
|
37867
|
+
}
|
|
37868
|
+
async sendMessage(message) {
|
|
37869
|
+
return await span(
|
|
37870
|
+
"chat.sendMessage",
|
|
37871
|
+
{
|
|
37872
|
+
conversationId: this.conversation.id,
|
|
37873
|
+
direction: "outgoing",
|
|
37874
|
+
integration: this.conversation.integration || "-",
|
|
37875
|
+
channel: this.conversation.channel || "-",
|
|
37876
|
+
userId: this.botId,
|
|
37877
|
+
botId: this.botId,
|
|
37878
|
+
"message.type": message.type
|
|
37879
|
+
},
|
|
37880
|
+
async (s) => {
|
|
37881
|
+
const [payload, citations] = this.citations.removeCitationsFromObject(message.payload);
|
|
37882
|
+
const { message: created } = await this.client.createMessage({
|
|
37883
|
+
conversationId: this.conversation.id,
|
|
37884
|
+
tags: message.tags || {},
|
|
37885
|
+
userId: this.botId,
|
|
37886
|
+
type: message.type.toLowerCase(),
|
|
37887
|
+
payload: {
|
|
37888
|
+
...payload,
|
|
37889
|
+
metadata: { citations }
|
|
37890
|
+
}
|
|
37891
|
+
});
|
|
37892
|
+
s.setAttribute("messageId", created.id);
|
|
37893
|
+
await this.addMessage(created);
|
|
37894
|
+
return created;
|
|
37895
|
+
}
|
|
37896
|
+
);
|
|
37897
|
+
}
|
|
37898
|
+
async transformMessage(message) {
|
|
37899
|
+
let clone = structuredClone(message);
|
|
37900
|
+
clone = await this.__temporary__fixTelegramImage(clone);
|
|
37901
|
+
const attachments = clone.type === "image" && clone.payload.imageUrl ? [
|
|
37902
|
+
{
|
|
37903
|
+
type: "image",
|
|
37904
|
+
url: clone.payload.imageUrl
|
|
37905
|
+
}
|
|
37906
|
+
] : [];
|
|
37907
|
+
if (clone.type === "bloc") {
|
|
37908
|
+
clone.payload.items = clone.payload.items.filter((item) => {
|
|
37909
|
+
if (item.type === "image" && item.payload.imageUrl) {
|
|
37910
|
+
attachments.push({
|
|
37911
|
+
type: "image",
|
|
37912
|
+
url: item.payload.imageUrl
|
|
37913
|
+
});
|
|
37914
|
+
return false;
|
|
37915
|
+
}
|
|
37916
|
+
return true;
|
|
37917
|
+
});
|
|
37918
|
+
}
|
|
37919
|
+
if (message.direction === "outgoing") {
|
|
37920
|
+
return {
|
|
37921
|
+
id: message.id,
|
|
37922
|
+
role: "assistant",
|
|
37923
|
+
content: JSON.stringify(
|
|
37924
|
+
{
|
|
37925
|
+
type: message.type,
|
|
37926
|
+
payload: message.payload
|
|
37927
|
+
},
|
|
37928
|
+
null,
|
|
37929
|
+
2
|
|
37930
|
+
),
|
|
37931
|
+
createdAt: message.createdAt
|
|
37932
|
+
};
|
|
37933
|
+
}
|
|
37934
|
+
if (message.direction === "incoming") {
|
|
37935
|
+
return {
|
|
37936
|
+
id: message.id,
|
|
37937
|
+
role: "user",
|
|
37938
|
+
content: JSON.stringify(
|
|
37939
|
+
{
|
|
37940
|
+
type: message.type,
|
|
37941
|
+
payload: message.payload
|
|
37942
|
+
},
|
|
37943
|
+
null,
|
|
37944
|
+
2
|
|
37945
|
+
),
|
|
37946
|
+
createdAt: message.createdAt,
|
|
37947
|
+
attachments
|
|
37948
|
+
};
|
|
37949
|
+
}
|
|
37950
|
+
return null;
|
|
37951
|
+
}
|
|
37952
|
+
async transformEvent(event) {
|
|
37953
|
+
return {
|
|
37954
|
+
id: event.id,
|
|
37955
|
+
role: "event",
|
|
37956
|
+
name: event.type,
|
|
37957
|
+
payload: event.payload,
|
|
37958
|
+
createdAt: event.createdAt
|
|
37959
|
+
};
|
|
37960
|
+
}
|
|
37961
|
+
async handle(message) {
|
|
37962
|
+
return await span(
|
|
37963
|
+
"chat.sendMessage",
|
|
37964
|
+
{
|
|
37965
|
+
conversationId: this.conversation.id,
|
|
37966
|
+
direction: "outgoing",
|
|
37967
|
+
integration: this.conversation.integration || "-",
|
|
37968
|
+
channel: this.conversation.channel || "-",
|
|
37969
|
+
userId: this.botId,
|
|
37970
|
+
botId: this.botId,
|
|
37971
|
+
"message.type": message.type
|
|
37972
|
+
},
|
|
37973
|
+
async () => {
|
|
37974
|
+
if (message.type === "MESSAGE") {
|
|
37975
|
+
message = Message.parse(message);
|
|
37976
|
+
if (typeof message.props.text === "string") {
|
|
37977
|
+
message.children.push(message.props.text);
|
|
37978
|
+
}
|
|
37979
|
+
for (const msg of message.children) {
|
|
37980
|
+
if (isAnyComponent2(msg)) {
|
|
37981
|
+
await this.handle(msg);
|
|
37982
|
+
} else if (typeof msg === "string" && msg.trim().length) {
|
|
37983
|
+
await this.sendMessage({
|
|
37984
|
+
type: "text",
|
|
37985
|
+
payload: { text: msg }
|
|
37986
|
+
});
|
|
37987
|
+
}
|
|
37988
|
+
}
|
|
37989
|
+
return;
|
|
37990
|
+
}
|
|
37991
|
+
const registration = this.componentRegistry.get(message.type.toLowerCase());
|
|
37992
|
+
let componentRegistration = registration;
|
|
37993
|
+
if (!componentRegistration) {
|
|
37994
|
+
for (const [_2, reg] of this.componentRegistry) {
|
|
37995
|
+
if (reg.component.definition.aliases?.map((x) => x.toLowerCase()).includes(message.type.toLowerCase())) {
|
|
37996
|
+
componentRegistration = reg;
|
|
37997
|
+
break;
|
|
37998
|
+
}
|
|
37999
|
+
}
|
|
38000
|
+
}
|
|
38001
|
+
if (!componentRegistration) {
|
|
38002
|
+
throw new Error(`Could not find component for message type "${message.type}"`);
|
|
38003
|
+
}
|
|
38004
|
+
if (componentRegistration.handler) {
|
|
38005
|
+
await componentRegistration.handler(message);
|
|
38006
|
+
return;
|
|
38007
|
+
}
|
|
38008
|
+
await this.sendMessage({
|
|
38009
|
+
type: componentRegistration.component.definition.name,
|
|
38010
|
+
payload: message.props
|
|
38011
|
+
});
|
|
38012
|
+
}
|
|
38013
|
+
);
|
|
38014
|
+
}
|
|
38015
|
+
/**
|
|
38016
|
+
* This is a temporary workaround as the Content-Type of images on Telegram are "binary/octet-stream"
|
|
38017
|
+
* And OpenAI integration does not support this type. It needs to be "image/jpeg" or "image/png".
|
|
38018
|
+
* This function fetches the image from Telegram, uploads it to the file storage, and replaces the URL in the message.
|
|
38019
|
+
*/
|
|
38020
|
+
async __temporary__fixTelegramImage(message) {
|
|
38021
|
+
if (this.conversation.integration !== "telegram") {
|
|
38022
|
+
return message;
|
|
38023
|
+
}
|
|
38024
|
+
const clone = structuredClone(message);
|
|
38025
|
+
if (clone.type === "image" && clone.payload.imageUrl) {
|
|
38026
|
+
const arrayBuffer = await (await fetch(clone.payload.imageUrl)).arrayBuffer();
|
|
38027
|
+
const buffer = Buffer.from(arrayBuffer);
|
|
38028
|
+
const fileName = clone.payload.imageUrl.split("/").pop() || "image.jpg";
|
|
38029
|
+
const { file } = await this.client.uploadFile({
|
|
38030
|
+
key: `telegram/${this.conversation.id}/${clone.id}/${fileName}`,
|
|
38031
|
+
content: buffer,
|
|
38032
|
+
index: false,
|
|
38033
|
+
accessPolicies: ["public_content"],
|
|
38034
|
+
publicContentImmediatelyAccessible: true,
|
|
38035
|
+
expiresAt: new Date(Date.now() + 1e3 * 60 * 60 * 24).toISOString()
|
|
38036
|
+
// 1 day
|
|
38037
|
+
});
|
|
38038
|
+
clone.payload.imageUrl = file.url;
|
|
38039
|
+
}
|
|
38040
|
+
return clone;
|
|
38041
|
+
}
|
|
38042
|
+
async addEvent(event) {
|
|
38043
|
+
if (!isEvent(event)) {
|
|
38044
|
+
return;
|
|
38045
|
+
}
|
|
38046
|
+
const item = await this.transformEvent(event);
|
|
38047
|
+
if (!item || this._transcript?.find((m) => m.id === event.id)) {
|
|
38048
|
+
return;
|
|
38049
|
+
}
|
|
38050
|
+
let insertIndex = this._transcript?.findIndex(
|
|
38051
|
+
(m) => m.createdAt && new Date(m.createdAt) > new Date(event.createdAt)
|
|
38052
|
+
);
|
|
38053
|
+
this._transcript?.splice(
|
|
38054
|
+
insertIndex === -1 || insertIndex === void 0 ? this._transcript.length : insertIndex,
|
|
38055
|
+
0,
|
|
38056
|
+
item
|
|
38057
|
+
);
|
|
38058
|
+
}
|
|
38059
|
+
async addMessage(message) {
|
|
38060
|
+
if (!isMessage(message)) {
|
|
38061
|
+
return;
|
|
38062
|
+
}
|
|
38063
|
+
const item = await this.transformMessage(message);
|
|
38064
|
+
if (!item || this._transcript?.find((m) => m.id === message.id)) {
|
|
38065
|
+
return;
|
|
38066
|
+
}
|
|
38067
|
+
let insertIndex = this._transcript?.findIndex(
|
|
38068
|
+
(m) => m.createdAt && item.createdAt && new Date(m.createdAt) > new Date(item.createdAt)
|
|
38069
|
+
);
|
|
38070
|
+
this._transcript?.splice(
|
|
38071
|
+
insertIndex === -1 || insertIndex === void 0 ? this._transcript.length : insertIndex,
|
|
38072
|
+
0,
|
|
38073
|
+
item
|
|
38074
|
+
);
|
|
38075
|
+
this.trackedTags.tags["adkSyncTs"] = this._transcript.at(-1).createdAt;
|
|
38076
|
+
}
|
|
38077
|
+
};
|
|
37466
38078
|
}
|
|
37467
38079
|
});
|
|
37468
38080
|
|
|
@@ -37594,14 +38206,11 @@ var init_conversation_instance = __esm({
|
|
|
37594
38206
|
const mapping = interfaceMappings.getIntegrationAction("typingIndicator", "startTypingIndicator", this.integration);
|
|
37595
38207
|
if (mapping) {
|
|
37596
38208
|
const message = context.get("message", { optional: true });
|
|
37597
|
-
if (!message) {
|
|
37598
|
-
return;
|
|
37599
|
-
}
|
|
37600
38209
|
await this.client.callAction({
|
|
37601
38210
|
type: mapping,
|
|
37602
38211
|
input: {
|
|
37603
38212
|
conversationId: this.id,
|
|
37604
|
-
messageId: message
|
|
38213
|
+
messageId: message?.id || ""
|
|
37605
38214
|
}
|
|
37606
38215
|
}).catch(() => {
|
|
37607
38216
|
});
|
|
@@ -37614,14 +38223,11 @@ var init_conversation_instance = __esm({
|
|
|
37614
38223
|
const mapping = interfaceMappings.getIntegrationAction("typingIndicator", "stopTypingIndicator", this.integration);
|
|
37615
38224
|
if (mapping) {
|
|
37616
38225
|
const message = context.get("message", { optional: true });
|
|
37617
|
-
if (!message) {
|
|
37618
|
-
return;
|
|
37619
|
-
}
|
|
37620
38226
|
await this.client.callAction({
|
|
37621
38227
|
type: mapping,
|
|
37622
38228
|
input: {
|
|
37623
38229
|
conversationId: this.id,
|
|
37624
|
-
messageId: message
|
|
38230
|
+
messageId: message?.id || ""
|
|
37625
38231
|
}
|
|
37626
38232
|
}).catch(() => {
|
|
37627
38233
|
});
|
|
@@ -37647,6 +38253,7 @@ var init_conversation = __esm({
|
|
|
37647
38253
|
init_events2();
|
|
37648
38254
|
init_conversation_instance();
|
|
37649
38255
|
init_workflow_instance();
|
|
38256
|
+
init_chat();
|
|
37650
38257
|
ConversationHandler = Symbol.for("conversation.handler");
|
|
37651
38258
|
((Typings8) => {
|
|
37652
38259
|
Typings8.Primitive = "conversation";
|
|
@@ -37661,12 +38268,15 @@ var init_conversation = __esm({
|
|
|
37661
38268
|
events;
|
|
37662
38269
|
/** @internal */
|
|
37663
38270
|
schema;
|
|
38271
|
+
/** @internal */
|
|
38272
|
+
chatFactory;
|
|
37664
38273
|
#handler;
|
|
37665
38274
|
constructor(props) {
|
|
37666
38275
|
this.channel = props.channel;
|
|
37667
38276
|
this.events = props.events ?? [];
|
|
37668
38277
|
this.schema = props.state ?? z16.object({}).passthrough();
|
|
37669
38278
|
this.#handler = props.handler;
|
|
38279
|
+
this.chatFactory = props.chat ?? (({ context: context3 }) => new Chat(context3));
|
|
37670
38280
|
}
|
|
37671
38281
|
/** @internal */
|
|
37672
38282
|
getDefinition() {
|
|
@@ -37804,7 +38414,8 @@ var init_conversation = __esm({
|
|
|
37804
38414
|
conversation: conversationInstance,
|
|
37805
38415
|
state: stateProxy,
|
|
37806
38416
|
client: client2,
|
|
37807
|
-
execute
|
|
38417
|
+
execute,
|
|
38418
|
+
chat
|
|
37808
38419
|
});
|
|
37809
38420
|
controller.abort();
|
|
37810
38421
|
void startTypingPromise.then(() => conversationInstance.stopTyping().catch(() => {
|
|
@@ -37831,7 +38442,6 @@ var init_conversation2 = __esm({
|
|
|
37831
38442
|
init_define_PACKAGE_VERSIONS();
|
|
37832
38443
|
init_runtime2();
|
|
37833
38444
|
init_conversation();
|
|
37834
|
-
init_chat2();
|
|
37835
38445
|
init_tracing();
|
|
37836
38446
|
init_adk();
|
|
37837
38447
|
init_conversation_matching();
|
|
@@ -38026,6 +38636,7 @@ var init_library = __esm({
|
|
|
38026
38636
|
init_runtime2();
|
|
38027
38637
|
init_client();
|
|
38028
38638
|
init_primitives();
|
|
38639
|
+
init_chat2();
|
|
38029
38640
|
init_workflow_utils();
|
|
38030
38641
|
init_events2();
|
|
38031
38642
|
init_autonomous();
|