@elizaos/plugin-bootstrap 1.0.0-beta.50 → 1.0.0-beta.52

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1307,6 +1307,25 @@ Response format should be formatted in a valid JSON block like this:
1307
1307
  \`\`\`
1308
1308
 
1309
1309
  Your response should include the valid JSON block and nothing else.`;
1310
+ function getFirstAvailableField(obj, fields) {
1311
+ for (const field of fields) {
1312
+ if (typeof obj[field] === "string" && obj[field].trim() !== "") {
1313
+ return obj[field];
1314
+ }
1315
+ }
1316
+ return null;
1317
+ }
1318
+ function extractReplyContent(response, replyFieldKeys) {
1319
+ const hasReplyAction = response.content.actions?.includes("REPLY");
1320
+ const text = getFirstAvailableField(response.content, replyFieldKeys);
1321
+ if (!hasReplyAction || !text) return null;
1322
+ return {
1323
+ ...response.content,
1324
+ thought: response.content.thought,
1325
+ text,
1326
+ actions: ["REPLY"]
1327
+ };
1328
+ }
1310
1329
  var replyAction = {
1311
1330
  name: "REPLY",
1312
1331
  similes: ["GREET", "REPLY_TO_MESSAGE", "SEND_REPLY", "RESPOND", "RESPONSE"],
@@ -1315,17 +1334,11 @@ var replyAction = {
1315
1334
  return true;
1316
1335
  },
1317
1336
  handler: async (runtime, message, state, _options, callback, responses) => {
1318
- const existingResponses = responses?.filter(
1319
- (response2) => response2.content.actions?.includes("REPLY") && response2.content.message
1320
- );
1321
- if (existingResponses && existingResponses.length > 0) {
1322
- for (const response2 of existingResponses) {
1323
- const responseContent2 = {
1324
- thought: response2.content.thought || "Using provided text for reply",
1325
- text: response2.content.message,
1326
- actions: ["REPLY"]
1327
- };
1328
- await callback(responseContent2);
1337
+ const replyFieldKeys = ["message", "text"];
1338
+ const existingReplies = responses?.map((r) => extractReplyContent(r, replyFieldKeys)).filter((reply) => reply !== null) ?? [];
1339
+ if (existingReplies.length > 0) {
1340
+ for (const reply of existingReplies) {
1341
+ await callback(reply);
1329
1342
  }
1330
1343
  return;
1331
1344
  }
@@ -1337,7 +1350,7 @@ var replyAction = {
1337
1350
  state,
1338
1351
  template: replyTemplate
1339
1352
  });
1340
- const response = await runtime.useModel(ModelType4.OBJECT_SMALL, {
1353
+ const response = await runtime.useModel(ModelType4.OBJECT_LARGE, {
1341
1354
  prompt
1342
1355
  });
1343
1356
  const responseContent = {
@@ -5665,237 +5678,244 @@ var messageReceivedHandler = async ({
5665
5678
  callback,
5666
5679
  onComplete
5667
5680
  }) => {
5668
- logger19.info(`[Bootstrap] Message received from ${message.entityId} in room ${message.roomId}`);
5669
- const responseId = v4_default();
5670
- if (!latestResponseIds.has(runtime.agentId)) {
5671
- latestResponseIds.set(runtime.agentId, /* @__PURE__ */ new Map());
5672
- }
5673
- const agentResponses = latestResponseIds.get(runtime.agentId);
5674
- if (!agentResponses) {
5675
- throw new Error("Agent responses map not found");
5676
- }
5677
- agentResponses.set(message.roomId, responseId);
5678
- const runId = asUUID(v4_default());
5679
- const startTime = Date.now();
5680
- await runtime.emitEvent(EventType2.RUN_STARTED, {
5681
- runtime,
5682
- runId,
5683
- messageId: message.id,
5684
- roomId: message.roomId,
5685
- entityId: message.entityId,
5686
- startTime,
5687
- status: "started",
5688
- source: "messageHandler"
5689
- });
5690
5681
  const timeoutDuration = 60 * 60 * 1e3;
5691
5682
  let timeoutId = void 0;
5692
- const timeoutPromise = new Promise((_, reject) => {
5693
- timeoutId = setTimeout(async () => {
5694
- await runtime.emitEvent(EventType2.RUN_TIMEOUT, {
5695
- runtime,
5696
- runId,
5697
- messageId: message.id,
5698
- roomId: message.roomId,
5699
- entityId: message.entityId,
5700
- startTime,
5701
- status: "timeout",
5702
- endTime: Date.now(),
5703
- duration: Date.now() - startTime,
5704
- error: "Run exceeded 60 minute timeout",
5705
- source: "messageHandler"
5706
- });
5707
- reject(new Error("Run exceeded 60 minute timeout"));
5708
- }, timeoutDuration);
5709
- });
5710
- const processingPromise = (async () => {
5711
- try {
5712
- if (message.entityId === runtime.agentId) {
5713
- logger19.debug(`[Bootstrap] Skipping message from self (${runtime.agentId})`);
5714
- throw new Error("Message is from the agent itself");
5715
- }
5716
- logger19.debug(
5717
- `[Bootstrap] Processing message: ${truncateToCompleteSentence(message.content.text || "", 50)}...`
5718
- );
5719
- logger19.debug("[Bootstrap] Saving message to memory and embeddings");
5720
- await Promise.all([
5721
- runtime.addEmbeddingToMemory(message),
5722
- runtime.createMemory(message, "messages")
5723
- ]);
5724
- const agentUserState = await runtime.getParticipantUserState(message.roomId, runtime.agentId);
5725
- if (agentUserState === "MUTED" && !message.content.text?.toLowerCase().includes(runtime.character.name.toLowerCase())) {
5726
- logger19.debug(`[Bootstrap] Ignoring muted room ${message.roomId}`);
5727
- return;
5728
- }
5729
- let state = await runtime.composeState(message, [
5730
- "ANXIETY",
5731
- "SHOULD_RESPOND",
5732
- "ENTITIES",
5733
- "CHARACTER",
5734
- "RECENT_MESSAGES"
5735
- ]);
5736
- const room = await runtime.getRoom(message.roomId);
5737
- const shouldSkipShouldRespond = room?.type === ChannelType10.DM || room?.type === ChannelType10.VOICE_DM || room?.type === ChannelType10.SELF || room?.type === ChannelType10.API;
5738
- let shouldRespond = true;
5739
- if (!shouldSkipShouldRespond) {
5740
- const shouldRespondPrompt = composePromptFromState9({
5741
- state,
5742
- template: runtime.character.templates?.shouldRespondTemplate || shouldRespondTemplate
5683
+ try {
5684
+ logger19.info(`[Bootstrap] Message received from ${message.entityId} in room ${message.roomId}`);
5685
+ const responseId = v4_default();
5686
+ if (!latestResponseIds.has(runtime.agentId)) {
5687
+ latestResponseIds.set(runtime.agentId, /* @__PURE__ */ new Map());
5688
+ }
5689
+ const agentResponses = latestResponseIds.get(runtime.agentId);
5690
+ if (!agentResponses) {
5691
+ throw new Error("Agent responses map not found");
5692
+ }
5693
+ agentResponses.set(message.roomId, responseId);
5694
+ const runId = asUUID(v4_default());
5695
+ const startTime = Date.now();
5696
+ await runtime.emitEvent(EventType2.RUN_STARTED, {
5697
+ runtime,
5698
+ runId,
5699
+ messageId: message.id,
5700
+ roomId: message.roomId,
5701
+ entityId: message.entityId,
5702
+ startTime,
5703
+ status: "started",
5704
+ source: "messageHandler"
5705
+ });
5706
+ const timeoutPromise = new Promise((_, reject) => {
5707
+ timeoutId = setTimeout(async () => {
5708
+ await runtime.emitEvent(EventType2.RUN_TIMEOUT, {
5709
+ runtime,
5710
+ runId,
5711
+ messageId: message.id,
5712
+ roomId: message.roomId,
5713
+ entityId: message.entityId,
5714
+ startTime,
5715
+ status: "timeout",
5716
+ endTime: Date.now(),
5717
+ duration: Date.now() - startTime,
5718
+ error: "Run exceeded 60 minute timeout",
5719
+ source: "messageHandler"
5743
5720
  });
5721
+ reject(new Error("Run exceeded 60 minute timeout"));
5722
+ }, timeoutDuration);
5723
+ });
5724
+ const processingPromise = (async () => {
5725
+ try {
5726
+ if (message.entityId === runtime.agentId) {
5727
+ logger19.debug(`[Bootstrap] Skipping message from self (${runtime.agentId})`);
5728
+ throw new Error("Message is from the agent itself");
5729
+ }
5744
5730
  logger19.debug(
5745
- `[Bootstrap] Evaluating response for ${runtime.character.name}
5746
- Prompt: ${shouldRespondPrompt}`
5731
+ `[Bootstrap] Processing message: ${truncateToCompleteSentence(message.content.text || "", 50)}...`
5747
5732
  );
5748
- const response = await runtime.useModel(ModelType13.TEXT_SMALL, {
5749
- prompt: shouldRespondPrompt
5750
- });
5751
- logger19.debug(`[Bootstrap] Response evaluation for ${runtime.character.name}:
5752
- ${response}`);
5753
- logger19.debug(`[Bootstrap] Response type: ${typeof response}`);
5754
- const responseObject = parseKeyValueXml(response);
5755
- logger19.debug("[Bootstrap] Parsed response:", responseObject);
5756
- shouldRespond = responseObject?.action && responseObject.action === "RESPOND";
5757
- } else {
5758
- shouldRespond = true;
5759
- }
5760
- let responseMessages = [];
5761
- if (shouldRespond) {
5762
- state = await runtime.composeState(message);
5763
- const prompt = composePromptFromState9({
5764
- state,
5765
- template: runtime.character.templates?.messageHandlerTemplate || messageHandlerTemplate
5766
- });
5767
- let responseContent = null;
5768
- let retries = 0;
5769
- const maxRetries = 3;
5770
- while (retries < maxRetries && (!responseContent?.thought || !responseContent?.actions)) {
5771
- let response = await runtime.useModel(ModelType13.TEXT_LARGE, {
5772
- prompt
5733
+ logger19.debug("[Bootstrap] Saving message to memory and embeddings");
5734
+ await Promise.all([
5735
+ runtime.addEmbeddingToMemory(message),
5736
+ runtime.createMemory(message, "messages")
5737
+ ]);
5738
+ const agentUserState = await runtime.getParticipantUserState(
5739
+ message.roomId,
5740
+ runtime.agentId
5741
+ );
5742
+ if (agentUserState === "MUTED" && !message.content.text?.toLowerCase().includes(runtime.character.name.toLowerCase())) {
5743
+ logger19.debug(`[Bootstrap] Ignoring muted room ${message.roomId}`);
5744
+ return;
5745
+ }
5746
+ let state = await runtime.composeState(
5747
+ message,
5748
+ ["ANXIETY", "SHOULD_RESPOND", "ENTITIES", "CHARACTER", "RECENT_MESSAGES"],
5749
+ true
5750
+ );
5751
+ const room = await runtime.getRoom(message.roomId);
5752
+ const shouldSkipShouldRespond = room?.type === ChannelType10.DM || room?.type === ChannelType10.VOICE_DM || room?.type === ChannelType10.SELF || room?.type === ChannelType10.API || room?.source === "client_chat";
5753
+ logger19.debug(
5754
+ `[Bootstrap] Skipping shouldRespond check for ${runtime.character.name} because ${room?.type} ${room?.source}`
5755
+ );
5756
+ let shouldRespond = true;
5757
+ if (!shouldSkipShouldRespond) {
5758
+ const shouldRespondPrompt = composePromptFromState9({
5759
+ state,
5760
+ template: runtime.character.templates?.shouldRespondTemplate || shouldRespondTemplate
5761
+ });
5762
+ logger19.debug(
5763
+ `[Bootstrap] Evaluating response for ${runtime.character.name}
5764
+ Prompt: ${shouldRespondPrompt}`
5765
+ );
5766
+ const response = await runtime.useModel(ModelType13.TEXT_SMALL, {
5767
+ prompt: shouldRespondPrompt
5768
+ });
5769
+ logger19.debug(
5770
+ `[Bootstrap] Response evaluation for ${runtime.character.name}:
5771
+ ${response}`
5772
+ );
5773
+ logger19.debug(`[Bootstrap] Response type: ${typeof response}`);
5774
+ const responseObject = parseKeyValueXml(response);
5775
+ logger19.debug("[Bootstrap] Parsed response:", responseObject);
5776
+ shouldRespond = responseObject?.action && responseObject.action === "RESPOND";
5777
+ } else {
5778
+ shouldRespond = true;
5779
+ }
5780
+ let responseMessages = [];
5781
+ if (shouldRespond) {
5782
+ state = await runtime.composeState(message);
5783
+ const prompt = composePromptFromState9({
5784
+ state,
5785
+ template: runtime.character.templates?.messageHandlerTemplate || messageHandlerTemplate
5773
5786
  });
5774
- logger19.debug("[Bootstrap] *** Raw LLM Response ***\n", response);
5775
- const parsedXml = parseKeyValueXml(response);
5776
- logger19.debug("[Bootstrap] *** Parsed XML Content ***\n", parsedXml);
5777
- if (parsedXml) {
5778
- responseContent = {
5779
- ...parsedXml,
5780
- thought: parsedXml.thought || "",
5781
- actions: parsedXml.actions || ["IGNORE"],
5782
- providers: parsedXml.providers || [],
5783
- text: parsedXml.text || "",
5784
- simple: parsedXml.simple || false
5787
+ let responseContent = null;
5788
+ let retries = 0;
5789
+ const maxRetries = 3;
5790
+ while (retries < maxRetries && (!responseContent?.thought || !responseContent?.actions)) {
5791
+ let response = await runtime.useModel(ModelType13.TEXT_LARGE, {
5792
+ prompt
5793
+ });
5794
+ logger19.debug("[Bootstrap] *** Raw LLM Response ***\n", response);
5795
+ const parsedXml = parseKeyValueXml(response);
5796
+ logger19.debug("[Bootstrap] *** Parsed XML Content ***\n", parsedXml);
5797
+ if (parsedXml) {
5798
+ responseContent = {
5799
+ ...parsedXml,
5800
+ thought: parsedXml.thought || "",
5801
+ actions: parsedXml.actions || ["IGNORE"],
5802
+ providers: parsedXml.providers || [],
5803
+ text: parsedXml.text || "",
5804
+ simple: parsedXml.simple || false
5805
+ };
5806
+ } else {
5807
+ responseContent = null;
5808
+ }
5809
+ retries++;
5810
+ if (!responseContent?.thought || !responseContent?.actions) {
5811
+ logger19.warn(
5812
+ "[Bootstrap] *** Missing required fields (thought or actions), retrying... ***"
5813
+ );
5814
+ }
5815
+ }
5816
+ const currentResponseId = agentResponses.get(message.roomId);
5817
+ if (currentResponseId !== responseId) {
5818
+ logger19.info(
5819
+ `Response discarded - newer message being processed for agent: ${runtime.agentId}, room: ${message.roomId}`
5820
+ );
5821
+ return;
5822
+ }
5823
+ if (responseContent && message.id) {
5824
+ responseContent.inReplyTo = createUniqueUuid4(runtime, message.id);
5825
+ const responseMesssage = {
5826
+ id: asUUID(v4_default()),
5827
+ entityId: runtime.agentId,
5828
+ agentId: runtime.agentId,
5829
+ content: responseContent,
5830
+ roomId: message.roomId,
5831
+ createdAt: Date.now()
5785
5832
  };
5833
+ responseMessages = [responseMesssage];
5834
+ }
5835
+ agentResponses.delete(message.roomId);
5836
+ if (agentResponses.size === 0) {
5837
+ latestResponseIds.delete(runtime.agentId);
5838
+ }
5839
+ if (responseContent?.providers?.length && responseContent?.providers?.length > 0) {
5840
+ state = await runtime.composeState(message, responseContent?.providers || []);
5841
+ }
5842
+ if (responseContent && responseContent.simple && responseContent.text && (responseContent.actions?.length === 0 || responseContent.actions?.length === 1 && responseContent.actions[0].toUpperCase() === "REPLY")) {
5843
+ await callback(responseContent);
5786
5844
  } else {
5787
- responseContent = null;
5845
+ await runtime.processActions(message, responseMessages, state, callback);
5788
5846
  }
5789
- retries++;
5790
- if (!responseContent?.thought || !responseContent?.actions) {
5791
- logger19.warn(
5792
- "[Bootstrap] *** Missing required fields (thought or actions), retrying... ***"
5847
+ await runtime.evaluate(message, state, shouldRespond, callback, responseMessages);
5848
+ } else {
5849
+ logger19.debug("[Bootstrap] Agent decided not to respond (shouldRespond is false).");
5850
+ const currentResponseId = agentResponses.get(message.roomId);
5851
+ if (currentResponseId !== responseId) {
5852
+ logger19.info(
5853
+ `Ignore response discarded - newer message being processed for agent: ${runtime.agentId}, room: ${message.roomId}`
5793
5854
  );
5855
+ return;
5794
5856
  }
5795
- }
5796
- const currentResponseId = agentResponses.get(message.roomId);
5797
- if (currentResponseId !== responseId) {
5798
- logger19.info(
5799
- `Response discarded - newer message being processed for agent: ${runtime.agentId}, room: ${message.roomId}`
5800
- );
5801
- return;
5802
- }
5803
- if (responseContent && message.id) {
5804
- responseContent.inReplyTo = createUniqueUuid4(runtime, message.id);
5805
- const responseMesssage = {
5857
+ if (!message.id) {
5858
+ logger19.error("[Bootstrap] Message ID is missing, cannot create ignore response.");
5859
+ return;
5860
+ }
5861
+ const ignoreContent = {
5862
+ thought: "Agent decided not to respond to this message.",
5863
+ actions: ["IGNORE"],
5864
+ simple: true,
5865
+ // Treat it as simple for callback purposes
5866
+ inReplyTo: createUniqueUuid4(runtime, message.id)
5867
+ // Reference original message
5868
+ };
5869
+ await callback(ignoreContent);
5870
+ const ignoreMemory = {
5806
5871
  id: asUUID(v4_default()),
5807
5872
  entityId: runtime.agentId,
5808
5873
  agentId: runtime.agentId,
5809
- content: responseContent,
5874
+ content: ignoreContent,
5810
5875
  roomId: message.roomId,
5811
5876
  createdAt: Date.now()
5812
5877
  };
5813
- responseMessages = [responseMesssage];
5814
- }
5815
- agentResponses.delete(message.roomId);
5816
- if (agentResponses.size === 0) {
5817
- latestResponseIds.delete(runtime.agentId);
5818
- }
5819
- if (responseContent?.providers?.length && responseContent?.providers?.length > 0) {
5820
- state = await runtime.composeState(message, responseContent?.providers || []);
5821
- }
5822
- if (responseContent && responseContent.simple && responseContent.text && (responseContent.actions?.length === 0 || responseContent.actions?.length === 1 && responseContent.actions[0].toUpperCase() === "REPLY")) {
5823
- await callback(responseContent);
5824
- } else {
5825
- await runtime.processActions(message, responseMessages, state, callback);
5826
- }
5827
- await runtime.evaluate(message, state, shouldRespond, callback, responseMessages);
5828
- } else {
5829
- logger19.debug("[Bootstrap] Agent decided not to respond (shouldRespond is false).");
5830
- const currentResponseId = agentResponses.get(message.roomId);
5831
- if (currentResponseId !== responseId) {
5832
- logger19.info(
5833
- `Ignore response discarded - newer message being processed for agent: ${runtime.agentId}, room: ${message.roomId}`
5834
- );
5835
- return;
5836
- }
5837
- if (!message.id) {
5838
- logger19.error("[Bootstrap] Message ID is missing, cannot create ignore response.");
5839
- return;
5878
+ await runtime.createMemory(ignoreMemory, "messages");
5879
+ logger19.debug("[Bootstrap] Saved ignore response to memory", {
5880
+ memoryId: ignoreMemory.id
5881
+ });
5882
+ agentResponses.delete(message.roomId);
5883
+ if (agentResponses.size === 0) {
5884
+ latestResponseIds.delete(runtime.agentId);
5885
+ }
5840
5886
  }
5841
- const ignoreContent = {
5842
- thought: "Agent decided not to respond to this message.",
5843
- actions: ["IGNORE"],
5844
- simple: true,
5845
- // Treat it as simple for callback purposes
5846
- inReplyTo: createUniqueUuid4(runtime, message.id)
5847
- // Reference original message
5848
- };
5849
- await callback(ignoreContent);
5850
- const ignoreMemory = {
5851
- id: asUUID(v4_default()),
5852
- entityId: runtime.agentId,
5853
- agentId: runtime.agentId,
5854
- content: ignoreContent,
5887
+ await runtime.emitEvent(EventType2.RUN_ENDED, {
5888
+ runtime,
5889
+ runId,
5890
+ messageId: message.id,
5855
5891
  roomId: message.roomId,
5856
- createdAt: Date.now()
5857
- };
5858
- await runtime.createMemory(ignoreMemory, "messages");
5859
- logger19.debug("[Bootstrap] Saved ignore response to memory", { memoryId: ignoreMemory.id });
5860
- agentResponses.delete(message.roomId);
5861
- if (agentResponses.size === 0) {
5862
- latestResponseIds.delete(runtime.agentId);
5863
- }
5892
+ entityId: message.entityId,
5893
+ startTime,
5894
+ status: "completed",
5895
+ endTime: Date.now(),
5896
+ duration: Date.now() - startTime,
5897
+ source: "messageHandler"
5898
+ });
5899
+ } catch (error) {
5900
+ await runtime.emitEvent(EventType2.RUN_ENDED, {
5901
+ runtime,
5902
+ runId,
5903
+ messageId: message.id,
5904
+ roomId: message.roomId,
5905
+ entityId: message.entityId,
5906
+ startTime,
5907
+ status: "error",
5908
+ endTime: Date.now(),
5909
+ duration: Date.now() - startTime,
5910
+ error: error.message,
5911
+ source: "messageHandler"
5912
+ });
5864
5913
  }
5865
- onComplete?.();
5866
- await runtime.emitEvent(EventType2.RUN_ENDED, {
5867
- runtime,
5868
- runId,
5869
- messageId: message.id,
5870
- roomId: message.roomId,
5871
- entityId: message.entityId,
5872
- startTime,
5873
- status: "completed",
5874
- endTime: Date.now(),
5875
- duration: Date.now() - startTime,
5876
- source: "messageHandler"
5877
- });
5878
- } catch (error) {
5879
- onComplete?.();
5880
- await runtime.emitEvent(EventType2.RUN_ENDED, {
5881
- runtime,
5882
- runId,
5883
- messageId: message.id,
5884
- roomId: message.roomId,
5885
- entityId: message.entityId,
5886
- startTime,
5887
- status: "error",
5888
- endTime: Date.now(),
5889
- duration: Date.now() - startTime,
5890
- error: error.message,
5891
- source: "messageHandler"
5892
- });
5893
- }
5894
- })();
5895
- try {
5914
+ })();
5896
5915
  await Promise.race([processingPromise, timeoutPromise]);
5897
5916
  } finally {
5898
5917
  clearTimeout(timeoutId);
5918
+ onComplete?.();
5899
5919
  }
5900
5920
  };
5901
5921
  var reactionReceivedHandler = async ({
@@ -6229,7 +6249,8 @@ var events = {
6229
6249
  await messageReceivedHandler({
6230
6250
  runtime: payload.runtime,
6231
6251
  message: payload.message,
6232
- callback: payload.callback
6252
+ callback: payload.callback,
6253
+ onComplete: payload.onComplete
6233
6254
  });
6234
6255
  }
6235
6256
  ],