briyah 1.3.4 → 1.4.0
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/README.md +689 -619
- package/data/common/artifacts/dnd5e/campaign_designer-setting.md +112 -0
- package/data/common/artifacts/dnd5e/character_creator-basics.md +40 -0
- package/data/common/artifacts/dnd5e/class-barbarian.md +64 -0
- package/data/common/artifacts/dnd5e/class-bard.md +63 -0
- package/data/common/artifacts/dnd5e/class-cleric.md +60 -0
- package/data/common/artifacts/dnd5e/class-druid.md +56 -0
- package/data/common/artifacts/dnd5e/class-fighter.md +53 -0
- package/data/common/artifacts/dnd5e/class-monk.md +77 -0
- package/data/common/artifacts/dnd5e/class-paladin.md +66 -0
- package/data/common/artifacts/dnd5e/class-ranger.md +73 -0
- package/data/common/artifacts/dnd5e/class-rogue.md +62 -0
- package/data/common/artifacts/dnd5e/class-sorcerer.md +67 -0
- package/data/common/artifacts/dnd5e/class-warlock.md +83 -0
- package/data/common/artifacts/dnd5e/class-wizard.md +51 -0
- package/data/common/artifacts/dnd5e/dungeon_master-procedure.md +348 -0
- package/data/common/artifacts/dnd5e/manifest.json +95 -0
- package/data/common/config/comfyui-workflows/flux-kontext.edit.json +98 -0
- package/data/common/config/comfyui-workflows/flux-kontext.t2i.json +80 -0
- package/data/common/config/comfyui-workflows/flux2-dev.edit.json +125 -0
- package/data/common/config/comfyui-workflows/flux2-dev.t2i.json +96 -0
- package/data/common/config/image_models_full.json +186 -99
- package/data/common/config/markup +1 -1
- package/data/common/config/model_prices.json +1250 -237
- package/data/common/prompts/campaign_designer/defaults_config.json +16 -0
- package/data/common/prompts/campaign_designer/perceive.json +29 -0
- package/data/common/prompts/campaign_designer/perceive.prompt +345 -0
- package/data/common/prompts/campaign_designer/system_instruction.prompt +0 -0
- package/data/common/prompts/character_creator/defaults_config.json +31 -0
- package/data/common/prompts/character_creator/perceive.json +29 -0
- package/data/common/prompts/character_creator/perceive.prompt +621 -0
- package/data/common/prompts/character_creator/system_instruction.prompt +0 -0
- package/data/common/prompts/dnd_player/defaults_config.json +5 -0
- package/data/common/prompts/dnd_player/perceive.json +29 -0
- package/data/common/prompts/dnd_player/perceive.prompt +312 -0
- package/data/common/prompts/dnd_player/system_instruction.prompt +0 -0
- package/data/common/prompts/dnd_room_moderator/defaults_config.json +9 -0
- package/data/common/prompts/dnd_room_moderator/moderate.json +34 -0
- package/data/common/prompts/dnd_room_moderator/moderate.prompt +291 -0
- package/data/common/prompts/dnd_room_moderator/system_instruction.prompt +0 -0
- package/data/common/prompts/dungeon_master/compact_agent.prompt +24 -0
- package/data/common/prompts/dungeon_master/defaults_config.json +29 -0
- package/data/common/prompts/dungeon_master/perceive.json +33 -0
- package/data/common/prompts/dungeon_master/perceive.prompt +757 -0
- package/data/common/prompts/dungeon_master/system_instruction.prompt +0 -0
- package/data/common/prompts/room_moderator/moderate.json +34 -29
- package/data/common/prompts/room_moderator/moderate.prompt +230 -210
- package/data/common/prompts/story_moderator/moderate.prompt +5 -2
- package/data/common/prompts/story_moderator/moderate_to_alice.mock +6 -6
- package/data/common/prompts/story_moderator/moderate_to_bob.mock +6 -6
- package/dist-sdk/server/src/ai/LLM/anthropic.service.d.ts +2 -0
- package/dist-sdk/server/src/ai/LLM/anthropic.service.js +34 -22
- package/dist-sdk/server/src/ai/LLM/base-ai.service.d.ts +2 -0
- package/dist-sdk/server/src/ai/LLM/base-ai.service.js +8 -3
- package/dist-sdk/server/src/ai/LLM/comfyui-client.d.ts +29 -0
- package/dist-sdk/server/src/ai/LLM/comfyui-client.js +142 -0
- package/dist-sdk/server/src/ai/LLM/comfyui-workflow.d.ts +19 -0
- package/dist-sdk/server/src/ai/LLM/comfyui-workflow.js +217 -0
- package/dist-sdk/server/src/ai/LLM/comfyui.module.d.ts +2 -0
- package/dist-sdk/server/src/ai/LLM/comfyui.module.js +21 -0
- package/dist-sdk/server/src/ai/LLM/comfyui.service.d.ts +26 -0
- package/dist-sdk/server/src/ai/LLM/comfyui.service.js +191 -0
- package/dist-sdk/server/src/ai/LLM/mock.service.d.ts +1 -0
- package/dist-sdk/server/src/ai/LLM/mock.service.js +3 -0
- package/dist-sdk/server/src/ai/LLM/self-hosted.module.d.ts +2 -0
- package/dist-sdk/server/src/ai/LLM/self-hosted.module.js +21 -0
- package/dist-sdk/server/src/ai/LLM/self-hosted.service.d.ts +13 -0
- package/dist-sdk/server/src/ai/LLM/self-hosted.service.js +205 -0
- package/dist-sdk/server/src/ai/agent-config.d.ts +1 -0
- package/dist-sdk/server/src/ai/agent-store.service.js +6 -4
- package/dist-sdk/server/src/ai/agent.d.ts +7 -0
- package/dist-sdk/server/src/ai/agent.js +33 -2
- package/dist-sdk/server/src/ai/ai-factory.module.js +4 -0
- package/dist-sdk/server/src/ai/ai-factory.service.d.ts +5 -1
- package/dist-sdk/server/src/ai/ai-factory.service.js +16 -2
- package/dist-sdk/server/src/ai/artifact.service.d.ts +2 -0
- package/dist-sdk/server/src/ai/artifact.service.js +29 -1
- package/dist-sdk/server/src/ai/pack-artifacts.d.ts +7 -0
- package/dist-sdk/server/src/ai/pack-artifacts.js +155 -0
- package/dist-sdk/server/src/app/stripe.controller.js +5 -0
- package/dist-sdk/server/src/app.controller.d.ts +18 -18
- package/dist-sdk/server/src/app.controller.js +249 -25
- package/dist-sdk/server/src/app.service.d.ts +30 -17
- package/dist-sdk/server/src/app.service.js +304 -36
- package/dist-sdk/server/src/auth/auth.controller.js +3 -2
- package/dist-sdk/server/src/auth/owner-only.decorator.d.ts +1 -0
- package/dist-sdk/server/src/auth/owner-only.decorator.js +8 -0
- package/dist-sdk/server/src/auth/twilio.service.d.ts +5 -1
- package/dist-sdk/server/src/auth/twilio.service.js +5 -3
- package/dist-sdk/server/src/config/user-paths.service.d.ts +2 -0
- package/dist-sdk/server/src/config/user-paths.service.js +8 -0
- package/dist-sdk/server/src/room/artifact-store.service.js +10 -2
- package/dist-sdk/server/src/room/artifact.d.ts +1 -0
- package/dist-sdk/server/src/room/message.d.ts +2 -1
- package/dist-sdk/server/src/room/message.js +5 -4
- package/dist-sdk/server/src/room/room-config.d.ts +3 -0
- package/dist-sdk/server/src/room/room-store.service.d.ts +4 -2
- package/dist-sdk/server/src/room/room-store.service.js +46 -12
- package/dist-sdk/server/src/room/room.d.ts +29 -3
- package/dist-sdk/server/src/room/room.js +390 -122
- package/dist-sdk/server/src/sdk/briyah.js +21 -3
- package/dist-sdk/server/src/sdk/index.d.ts +4 -1
- package/dist-sdk/server/src/sdk/index.js +9 -1
- package/dist-sdk/server/src/story/story.service.js +21 -12
- package/dist-sdk/server/src/tools/native-tool.types.d.ts +31 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/backgrounds.d.ts +22 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/backgrounds.js +59 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/feats.d.ts +24 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/feats.js +11 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/gear.d.ts +5 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/gear.generated.d.ts +1 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/gear.generated.js +154 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/gear.js +9 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/magic-items.d.ts +20 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/magic-items.generated.d.ts +2 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/magic-items.generated.js +1824 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/magic-items.js +6 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/monsters.d.ts +44 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/monsters.generated.d.ts +2 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/monsters.generated.js +17293 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/monsters.js +5 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/races.d.ts +41 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/races.js +250 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/spells.d.ts +16 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/spells.generated.d.ts +2 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/spells.generated.js +3760 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/spells.js +12 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/srd.d.ts +63 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/srd.js +449 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/weapons.d.ts +23 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/weapons.js +88 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/backgrounds.d.ts +20 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/backgrounds.js +109 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/bestiary.d.ts +30 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/bestiary.js +329 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/campaign-items.d.ts +9 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/campaign-items.js +100 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/character.d.ts +117 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/character.js +1057 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/coerce.d.ts +13 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/coerce.js +147 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/dice.d.ts +27 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/dice.js +263 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/encumbrance.d.ts +26 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/encumbrance.js +129 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/feats.d.ts +22 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/feats.js +127 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/items.d.ts +33 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/items.js +137 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/races.d.ts +26 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/races.js +182 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/rules.d.ts +48 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/rules.js +344 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/sheet.d.ts +98 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/sheet.js +456 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/spells.d.ts +27 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/spells.js +123 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/weapons.d.ts +23 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/weapons.js +201 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/index.d.ts +3 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/index.js +35 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/tools/check.tool.d.ts +2 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/tools/check.tool.js +323 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/tools/dice.tools.d.ts +3 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/tools/dice.tools.js +117 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/tools/item.tool.d.ts +2 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/tools/item.tool.js +122 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/tools/monster.tool.d.ts +2 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/tools/monster.tool.js +157 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/tools/shared.d.ts +32 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/tools/shared.js +203 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/tools/sheet.tools.d.ts +4 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/tools/sheet.tools.js +465 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/tools/spell.tool.d.ts +2 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/tools/spell.tool.js +116 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/tools/state.tools.d.ts +4 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/tools/state.tools.js +234 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/tools/world.tools.d.ts +6 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/tools/world.tools.js +616 -0
- package/dist-sdk/server/src/tools/packs/index.d.ts +7 -0
- package/dist-sdk/server/src/tools/packs/index.js +45 -0
- package/dist-sdk/server/src/tools/tool-execution.service.d.ts +2 -2
- package/dist-sdk/server/src/tools/tool-execution.service.js +7 -6
- package/dist-sdk/shared/types/app.types.d.ts +63 -0
- package/docs/assets/hierarchy.js +1 -1
- package/docs/assets/navigation.js +1 -1
- package/docs/assets/search.js +1 -1
- package/docs/classes/Agent.html +51 -15
- package/docs/classes/Briyah.html +14 -14
- package/docs/classes/BriyahConfigService.html +5 -5
- package/docs/classes/NativeToolRegistry.html +21 -0
- package/docs/classes/Room.html +79 -29
- package/docs/classes/RoomMessage.html +32 -15
- package/docs/enums/MessageAction.html +3 -3
- package/docs/functions/availableToolPacks.html +2 -0
- package/docs/functions/dnd5eTools.html +5 -0
- package/docs/functions/enabledToolPacks.html +2 -0
- package/docs/functions/registerToolPacks.html +6 -0
- package/docs/hierarchy.html +1 -1
- package/docs/index.html +47 -3
- package/docs/interfaces/AgentInfo.html +17 -2
- package/docs/interfaces/AgentMessagesResponse.html +2 -2
- package/docs/interfaces/AppService.html +230 -181
- package/docs/interfaces/Artifact.html +15 -3
- package/docs/interfaces/ArtifactMetadata.html +5 -2
- package/docs/interfaces/ArtifactWriteOptions.html +21 -0
- package/docs/interfaces/AttachDocumentResponse.html +2 -2
- package/docs/interfaces/BriyahConfigOptions.html +9 -9
- package/docs/interfaces/ChapterInfo.html +2 -2
- package/docs/interfaces/Character.html +2 -2
- package/docs/interfaces/CreateAgentResponse.html +2 -2
- package/docs/interfaces/CreateRoomResponse.html +2 -2
- package/docs/interfaces/CreateStoryResponse.html +2 -2
- package/docs/interfaces/FileList.html +2 -2
- package/docs/interfaces/FileMetadata.html +3 -3
- package/docs/interfaces/IConfigService.html +3 -3
- package/docs/interfaces/LoggingOptions.html +6 -6
- package/docs/interfaces/Message.html +2 -2
- package/docs/interfaces/ModelInfo.html +2 -2
- package/docs/interfaces/NativeToolDefinition.html +11 -7
- package/docs/interfaces/PreparedPromptResponse.html +2 -2
- package/docs/interfaces/ProcessTextResponse.html +2 -2
- package/docs/interfaces/PromptFile.html +2 -2
- package/docs/interfaces/PromptFileContent.html +2 -2
- package/docs/interfaces/PromptFilesResponse.html +2 -2
- package/docs/interfaces/PromptFolder.html +2 -2
- package/docs/interfaces/PromptFoldersResponse.html +2 -2
- package/docs/interfaces/RoomDetails.html +15 -2
- package/docs/interfaces/RoomInfo.html +3 -2
- package/docs/interfaces/RoomMessageUpdate.html +2 -2
- package/docs/interfaces/RoomMessagesResponse.html +2 -2
- package/docs/interfaces/SendMessageResult.html +2 -2
- package/docs/interfaces/StoryErrorEvent.html +3 -3
- package/docs/interfaces/StoryIdea.html +2 -2
- package/docs/interfaces/StoryInfo.html +3 -3
- package/docs/interfaces/StoryIntroduceCharacterEvent.html +3 -3
- package/docs/interfaces/StoryProgressChapterEvent.html +3 -3
- package/docs/interfaces/StoryState.html +5 -5
- package/docs/interfaces/StoryStateEvent.html +3 -3
- package/docs/interfaces/ToolArtifact.html +8 -0
- package/docs/interfaces/ToolCaller.html +8 -0
- package/docs/interfaces/ToolDefinition.html +2 -2
- package/docs/interfaces/ToolHelpers.html +7 -3
- package/docs/interfaces/ToolParameter.html +2 -2
- package/docs/interfaces/ToolRoomAccess.html +51 -0
- package/docs/interfaces/ToolSummary.html +3 -2
- package/docs/interfaces/ToolsResponse.html +2 -2
- package/docs/interfaces/Transaction.html +2 -2
- package/docs/interfaces/TransactionHistoryResponse.html +2 -2
- package/docs/modules.html +1 -1
- package/docs/types/PromptScope.html +1 -1
- package/docs/types/ToolContext.html +2 -2
- package/docs/types/ToolRunResult.html +1 -1
- package/package.json +7 -4
|
@@ -6,14 +6,21 @@ const message_1 = require("./message");
|
|
|
6
6
|
const crypto_1 = require("crypto");
|
|
7
7
|
const errors_1 = require("../common/errors");
|
|
8
8
|
const logger_1 = require("../common/logger");
|
|
9
|
+
const MAX_DESCRIPTION_CHARS = 500;
|
|
9
10
|
class Room {
|
|
10
|
-
static
|
|
11
|
+
static maxToolIterations() {
|
|
12
|
+
const configured = Number(process.env.ROOM_MAX_TOOL_ITERATIONS);
|
|
13
|
+
return Number.isFinite(configured) && configured > 0 ? Math.floor(configured) : 12;
|
|
14
|
+
}
|
|
11
15
|
id;
|
|
12
16
|
name;
|
|
13
17
|
goal;
|
|
14
18
|
baseRoomDir;
|
|
15
19
|
agents = [];
|
|
16
20
|
roomLeader;
|
|
21
|
+
turnMode = 'broadcast';
|
|
22
|
+
moderator = '';
|
|
23
|
+
lastModeratedAudience = [];
|
|
17
24
|
roomMessages = [];
|
|
18
25
|
pendingMessages;
|
|
19
26
|
nextMessageIndex = 0;
|
|
@@ -122,15 +129,25 @@ class Room {
|
|
|
122
129
|
}
|
|
123
130
|
return '';
|
|
124
131
|
}
|
|
132
|
+
getTurnMode() {
|
|
133
|
+
return this.turnMode;
|
|
134
|
+
}
|
|
135
|
+
setTurnMode(mode) {
|
|
136
|
+
this.turnMode = mode === 'directed' ? 'directed' : 'broadcast';
|
|
137
|
+
}
|
|
138
|
+
getModerator() {
|
|
139
|
+
return this.moderator || '';
|
|
140
|
+
}
|
|
141
|
+
setModerator(agentNickname) {
|
|
142
|
+
this.moderator = agentNickname || '';
|
|
143
|
+
}
|
|
125
144
|
getAgentCount() {
|
|
126
145
|
return this.agents.length;
|
|
127
146
|
}
|
|
128
147
|
ready() {
|
|
129
148
|
if (this.isAdjourned || this.processingInProgress) {
|
|
130
149
|
this.isAdjourned = false;
|
|
131
|
-
this.
|
|
132
|
-
this.setCurrentSpeaker(this.getHumanAgentName());
|
|
133
|
-
this.notifyStateChange();
|
|
150
|
+
this.endTurn({ to: this.getHumanAgentName() || null });
|
|
134
151
|
}
|
|
135
152
|
}
|
|
136
153
|
getCurrentSpeaker() {
|
|
@@ -143,6 +160,19 @@ class Room {
|
|
|
143
160
|
isAgentsTurn(agentNickname) {
|
|
144
161
|
return this.currentSpeaker === agentNickname;
|
|
145
162
|
}
|
|
163
|
+
endTurn(floor, opts = {}) {
|
|
164
|
+
if (floor === 'release') {
|
|
165
|
+
this.currentSpeaker = null;
|
|
166
|
+
}
|
|
167
|
+
else if (floor !== 'keep') {
|
|
168
|
+
this.currentSpeaker = floor.to ?? null;
|
|
169
|
+
}
|
|
170
|
+
this.processingInProgress = false;
|
|
171
|
+
this.notifyStateChange();
|
|
172
|
+
if (opts.drain && this.pendingMessages && this.pendingMessages.length > 0) {
|
|
173
|
+
this.processPendingMessages();
|
|
174
|
+
}
|
|
175
|
+
}
|
|
146
176
|
getRoomMessageStats() {
|
|
147
177
|
let numMessages = this.roomMessages.length;
|
|
148
178
|
let numChars = this.roomMessages.map((m) => m.content?.length).reduce((a, b) => a + b, 0);
|
|
@@ -197,6 +227,15 @@ class Room {
|
|
|
197
227
|
setArtifacts(artifacts) {
|
|
198
228
|
this.artifacts = [...artifacts];
|
|
199
229
|
}
|
|
230
|
+
namesNobodyInADirectedRoom(action, targets) {
|
|
231
|
+
if (this.turnMode !== 'directed')
|
|
232
|
+
return false;
|
|
233
|
+
if (action !== app_types_1.MessageAction.SPEAK && action !== app_types_1.MessageAction.WHISPER)
|
|
234
|
+
return false;
|
|
235
|
+
if (!targets || targets.length === 0)
|
|
236
|
+
return false;
|
|
237
|
+
return !targets.some((name) => this.agents.some((a) => this.checkAgentNicknameMatch(a.agentNickname, name)));
|
|
238
|
+
}
|
|
200
239
|
parseAgentResponse(agent, response) {
|
|
201
240
|
let needsModeration = false;
|
|
202
241
|
let targets = [];
|
|
@@ -243,15 +282,36 @@ class Room {
|
|
|
243
282
|
if (normalizedAction === app_types_1.MessageAction.COMMISSION && targets.length === 0) {
|
|
244
283
|
needsModeration = true;
|
|
245
284
|
}
|
|
285
|
+
if (this.namesNobodyInADirectedRoom(normalizedAction, targets)) {
|
|
286
|
+
logger_1.logger.warn(`Room ${this.name}: ${agent.agentNickname} addressed "${targets.join(', ')}", who is not in the room; moderating rather than widening`);
|
|
287
|
+
needsModeration = true;
|
|
288
|
+
}
|
|
246
289
|
}
|
|
247
290
|
if (needsModeration) {
|
|
248
|
-
|
|
291
|
+
const moderatedContent = response && typeof response === 'object'
|
|
292
|
+
? (response.content ?? JSON.stringify(response))
|
|
293
|
+
: content;
|
|
294
|
+
return new message_1.RoomMessage(agent.agentNickname, app_types_1.MessageAction.MODERATE, moderatedContent, [], null, situation);
|
|
249
295
|
}
|
|
250
296
|
situation = response.situation || undefined;
|
|
251
297
|
const msg = new message_1.RoomMessage(agent.agentNickname, normalizedAction, response.content, targets, response.name, situation);
|
|
252
298
|
if (response.changes && typeof response.changes === 'object') {
|
|
253
299
|
msg.changes = response.changes;
|
|
254
300
|
}
|
|
301
|
+
if (response.relayAs) {
|
|
302
|
+
const relayAs = Room.coerceMessageAction(response.relayAs);
|
|
303
|
+
const relayable = [
|
|
304
|
+
app_types_1.MessageAction.SPEAK,
|
|
305
|
+
app_types_1.MessageAction.WHISPER,
|
|
306
|
+
app_types_1.MessageAction.SHOUT,
|
|
307
|
+
];
|
|
308
|
+
if (relayAs && relayable.includes(relayAs)) {
|
|
309
|
+
msg.relayAs = relayAs;
|
|
310
|
+
}
|
|
311
|
+
else {
|
|
312
|
+
logger_1.logger.warn(`Moderator ${agent.agentNickname} asked to relay as "${response.relayAs}", which is not one of speak, whisper or shout; relaying unchanged`);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
255
315
|
return msg;
|
|
256
316
|
}
|
|
257
317
|
static extractPreamble(response) {
|
|
@@ -312,8 +372,9 @@ class Room {
|
|
|
312
372
|
});
|
|
313
373
|
}
|
|
314
374
|
buildCharacterPrivateContextDigest() {
|
|
375
|
+
const leader = this.getRoomLeader();
|
|
315
376
|
return this.agents
|
|
316
|
-
.filter((a) => a.
|
|
377
|
+
.filter((a) => a.agentNickname !== leader && a.privateContext && a.privateContext.trim())
|
|
317
378
|
.map((a) => `### ${a.agentNickname}\n${a.privateContext}`)
|
|
318
379
|
.join('\n\n');
|
|
319
380
|
}
|
|
@@ -347,17 +408,34 @@ class Room {
|
|
|
347
408
|
}
|
|
348
409
|
return tools;
|
|
349
410
|
}
|
|
411
|
+
static renderArtifactForPrompt(artifact) {
|
|
412
|
+
const kept = [];
|
|
413
|
+
let inState = false;
|
|
414
|
+
for (const line of (artifact.body || '').split('\n')) {
|
|
415
|
+
if (!inState && line.trim() === '```briyah-state') {
|
|
416
|
+
inState = true;
|
|
417
|
+
continue;
|
|
418
|
+
}
|
|
419
|
+
if (inState) {
|
|
420
|
+
if (line.trim() === '```')
|
|
421
|
+
inState = false;
|
|
422
|
+
continue;
|
|
423
|
+
}
|
|
424
|
+
kept.push(line);
|
|
425
|
+
}
|
|
426
|
+
return kept.join('\n').replace(/\n{3,}/g, '\n\n').trim();
|
|
427
|
+
}
|
|
350
428
|
async buildPerceiveVariables(agent) {
|
|
351
429
|
const visibleArtifacts = this.getVisibleArtifacts(agent.agentNickname)
|
|
352
|
-
.map((a) => `${a.name} (by ${a.creator}): ${a
|
|
430
|
+
.map((a) => `${a.name} (by ${a.creator}): ${Room.renderArtifactForPrompt(a)}`)
|
|
353
431
|
.join('\n\n');
|
|
354
|
-
const
|
|
355
|
-
const invisibleArtifactNames =
|
|
356
|
-
.filter((a) => !
|
|
432
|
+
const readable = this.getReadableArtifacts(agent.agentNickname);
|
|
433
|
+
const invisibleArtifactNames = this.getArtifacts()
|
|
434
|
+
.filter((a) => !readable.includes(a))
|
|
357
435
|
.map((a) => a.name)
|
|
358
436
|
.join(', ');
|
|
359
|
-
const
|
|
360
|
-
const humanCharacterName =
|
|
437
|
+
const humanCharacterNames = this.getHumanAgents().map((a) => a.agentNickname);
|
|
438
|
+
const humanCharacterName = humanCharacterNames[0] ?? 'Player';
|
|
361
439
|
const maxOutputChars = 4096;
|
|
362
440
|
return {
|
|
363
441
|
agentName: agent.agentName,
|
|
@@ -372,6 +450,7 @@ class Room {
|
|
|
372
450
|
invisibleArtifactNames: invisibleArtifactNames,
|
|
373
451
|
humanCharacterName: humanCharacterName,
|
|
374
452
|
humanAgentName: humanCharacterName,
|
|
453
|
+
humanCharacterNames: humanCharacterNames,
|
|
375
454
|
maxOutputChars: maxOutputChars,
|
|
376
455
|
availableTools: await this.buildAvailableTools(agent),
|
|
377
456
|
};
|
|
@@ -388,6 +467,14 @@ class Room {
|
|
|
388
467
|
prompt = prompt ? `${prompt}\n\n${charactersPresent}` : charactersPresent;
|
|
389
468
|
}
|
|
390
469
|
}
|
|
470
|
+
const transientArtifacts = this.getTransientArtifacts(agent.agentNickname);
|
|
471
|
+
if (transientArtifacts.length > 0) {
|
|
472
|
+
const rendered = transientArtifacts
|
|
473
|
+
.map((a) => `${a.name} (by ${a.creator}):\n${Room.renderArtifactForPrompt(a)}`)
|
|
474
|
+
.join('\n\n');
|
|
475
|
+
const block = `Your current records (up to date as of this moment):\n\n${rendered}`;
|
|
476
|
+
prompt = prompt ? `${prompt}\n\n${block}` : block;
|
|
477
|
+
}
|
|
391
478
|
if (agent.privateContext && agent.privateContext.trim()) {
|
|
392
479
|
const privateBlock = `Your private context (known only to you):\n${agent.privateContext}`;
|
|
393
480
|
prompt = prompt ? `${prompt}\n\n${privateBlock}` : privateBlock;
|
|
@@ -411,7 +498,8 @@ class Room {
|
|
|
411
498
|
async runAgentTurn(agent, heardMessage, opts = {}) {
|
|
412
499
|
const variables = await this.buildPerceiveVariables(agent);
|
|
413
500
|
let response = await this.promptAgentOnce(agent, this.buildTransientPrompt(agent, heardMessage), variables);
|
|
414
|
-
|
|
501
|
+
const maxIterations = Room.maxToolIterations();
|
|
502
|
+
for (let i = 0; i < maxIterations; i++) {
|
|
415
503
|
if (this.isPaused || this.isAdjourned)
|
|
416
504
|
return null;
|
|
417
505
|
let feedback;
|
|
@@ -441,6 +529,13 @@ class Room {
|
|
|
441
529
|
agent.addToConversationHistory(`From system: You have reached the tool-call limit for this turn. Respond now with an audible action (speak, whisper, think, ...).`, false);
|
|
442
530
|
agent.save();
|
|
443
531
|
response = await this.promptAgentOnce(agent, this.buildTransientPrompt(agent, null), variables);
|
|
532
|
+
if (response.action === app_types_1.MessageAction.PUBLISH && response.name) {
|
|
533
|
+
this.publishArtifact(response.name, agent.agentNickname, response.content, response.targets);
|
|
534
|
+
logger_1.logger.warn(`Room ${this.name}: ${agent.agentNickname} published "${response.name}" on its final tool call; publishing it and ending the turn`);
|
|
535
|
+
}
|
|
536
|
+
else if (response.action === app_types_1.MessageAction.EXECUTE) {
|
|
537
|
+
logger_1.logger.warn(`Room ${this.name}: ${agent.agentNickname} still wanted tool "${response.name}" after the limit; it was not run`);
|
|
538
|
+
}
|
|
444
539
|
if (response.action === app_types_1.MessageAction.EXECUTE ||
|
|
445
540
|
response.action === app_types_1.MessageAction.PUBLISH) {
|
|
446
541
|
response = new message_1.RoomMessage(agent.agentNickname, app_types_1.MessageAction.THINK, response.content, []);
|
|
@@ -450,18 +545,102 @@ class Room {
|
|
|
450
545
|
async executeToolForAgent(agent, message) {
|
|
451
546
|
const toolName = message.name;
|
|
452
547
|
const result = this.toolExecutionService
|
|
453
|
-
? await this.toolExecutionService.runForAgent(toolName, message.content, agent.toolNames || [], this.currentToolContext)
|
|
548
|
+
? await this.toolExecutionService.runForAgent(toolName, message.content, agent.toolNames || [], this.currentToolContext, this.buildToolRoomAccess(agent))
|
|
454
549
|
: { ok: false, error: 'Tool execution is not available in this room' };
|
|
455
|
-
const argsPreview = message.content
|
|
456
|
-
|
|
457
|
-
: message.content || '';
|
|
458
|
-
const statusText = 'error' in result ? `error: ${result.error}` : 'ok';
|
|
550
|
+
const argsPreview = Room.previewForToolLog(message.content || '', Room.TOOL_LOG_ARGS_PREVIEW);
|
|
551
|
+
const statusText = 'error' in result ? `error: ${result.error}` : Room.describeToolResult(result.result);
|
|
459
552
|
this.addRoomMessage(new message_1.RoomMessage(agent.agentNickname, app_types_1.MessageAction.TOOL, `${toolName}(${argsPreview}) → ${statusText}`));
|
|
460
553
|
return `From system: Result of tool "${toolName}":\n${JSON.stringify(result)}`;
|
|
461
554
|
}
|
|
555
|
+
static TOOL_LOG_PREVIEW = 200;
|
|
556
|
+
static TOOL_LOG_ARGS_PREVIEW = 4000;
|
|
557
|
+
static previewForToolLog(text, limit = Room.TOOL_LOG_PREVIEW) {
|
|
558
|
+
return text.length > limit ? `${text.slice(0, limit)}...` : text;
|
|
559
|
+
}
|
|
560
|
+
static describeToolResult(value) {
|
|
561
|
+
if (value === undefined || value === null)
|
|
562
|
+
return 'ok';
|
|
563
|
+
if (typeof value === 'object') {
|
|
564
|
+
for (const field of ['detail', 'summary']) {
|
|
565
|
+
const line = value[field];
|
|
566
|
+
if (typeof line === 'string' && line.trim()) {
|
|
567
|
+
return Room.previewForToolLog(line.trim());
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
const rendered = typeof value === 'string' ? value : JSON.stringify(value);
|
|
572
|
+
return rendered ? Room.previewForToolLog(rendered) : 'ok';
|
|
573
|
+
}
|
|
574
|
+
buildToolRoomAccess(agent) {
|
|
575
|
+
const toToolArtifact = (a) => a
|
|
576
|
+
? {
|
|
577
|
+
name: a.name,
|
|
578
|
+
creator: a.creator,
|
|
579
|
+
body: a.body,
|
|
580
|
+
viewers: a.viewers || [],
|
|
581
|
+
transient: !!a.transient,
|
|
582
|
+
}
|
|
583
|
+
: undefined;
|
|
584
|
+
const write = (name, body, options) => {
|
|
585
|
+
if (!body || !body.trim()) {
|
|
586
|
+
throw new Error(`Refusing to write an empty body to artifact "${name}"; that would delete it`);
|
|
587
|
+
}
|
|
588
|
+
const existing = this.findArtifact(name);
|
|
589
|
+
let creator = agent.agentNickname;
|
|
590
|
+
if (existing) {
|
|
591
|
+
creator = existing.creator;
|
|
592
|
+
}
|
|
593
|
+
else if (options?.creator) {
|
|
594
|
+
const named = this.agents.find((a) => this.checkAgentNicknameMatch(a.agentNickname, options.creator));
|
|
595
|
+
if (named)
|
|
596
|
+
creator = named.agentNickname;
|
|
597
|
+
}
|
|
598
|
+
const finalViewers = options?.viewers ||
|
|
599
|
+
(existing && existing.viewers && existing.viewers.length > 0
|
|
600
|
+
? existing.viewers
|
|
601
|
+
: this.agents.map((a) => a.agentNickname));
|
|
602
|
+
return toToolArtifact(this.publishArtifact(name, creator, body, finalViewers, options?.transient));
|
|
603
|
+
};
|
|
604
|
+
return {
|
|
605
|
+
caller: {
|
|
606
|
+
nickname: agent.agentNickname,
|
|
607
|
+
isHuman: !!agent.isControlledByHuman,
|
|
608
|
+
isLeader: agent.agentNickname === this.getRoomLeader(),
|
|
609
|
+
},
|
|
610
|
+
agentNicknames: this.agents.map((a) => a.agentNickname),
|
|
611
|
+
roomId: this.getId(),
|
|
612
|
+
roomName: this.getName(),
|
|
613
|
+
get: (name) => toToolArtifact(this.findArtifact(name)),
|
|
614
|
+
list: () => this.artifacts.map((a) => a.name),
|
|
615
|
+
put: write,
|
|
616
|
+
update: (name, mutate, options) => {
|
|
617
|
+
const current = toToolArtifact(this.findArtifact(name));
|
|
618
|
+
const body = mutate(current);
|
|
619
|
+
if (body === null)
|
|
620
|
+
return current;
|
|
621
|
+
return write(name, body, options);
|
|
622
|
+
},
|
|
623
|
+
remove: (name) => this.deleteArtifact(name),
|
|
624
|
+
setDescription: (nickname, description) => {
|
|
625
|
+
const trimmed = (description || '').trim();
|
|
626
|
+
if (!trimmed) {
|
|
627
|
+
throw new Error(`Refusing to blank the description of "${nickname}"; it is how the other agents identify them`);
|
|
628
|
+
}
|
|
629
|
+
const target = this.agents.find((a) => this.checkAgentNicknameMatch(a.agentNickname, nickname));
|
|
630
|
+
if (!target)
|
|
631
|
+
return false;
|
|
632
|
+
target.description = trimmed.slice(0, MAX_DESCRIPTION_CHARS);
|
|
633
|
+
target.save();
|
|
634
|
+
return true;
|
|
635
|
+
},
|
|
636
|
+
};
|
|
637
|
+
}
|
|
462
638
|
getAgentAiResponse(agent, message, onResponse) {
|
|
463
639
|
this.runAgentTurn(agent, message)
|
|
464
|
-
.then((answer) =>
|
|
640
|
+
.then(async (answer) => {
|
|
641
|
+
await this.compactIfOversized(agent);
|
|
642
|
+
onResponse(answer);
|
|
643
|
+
})
|
|
465
644
|
.catch((e) => {
|
|
466
645
|
if (e instanceof errors_1.InsufficientBalanceError) {
|
|
467
646
|
this.handleInsufficientBalance(e);
|
|
@@ -473,13 +652,29 @@ class Room {
|
|
|
473
652
|
onResponse(null);
|
|
474
653
|
});
|
|
475
654
|
}
|
|
476
|
-
|
|
477
|
-
if (!
|
|
478
|
-
onResponse(null);
|
|
655
|
+
async compactIfOversized(agent) {
|
|
656
|
+
if (!agent.needsCompaction())
|
|
479
657
|
return;
|
|
658
|
+
try {
|
|
659
|
+
await agent.compact();
|
|
660
|
+
}
|
|
661
|
+
catch (e) {
|
|
662
|
+
logger_1.logger.warn(`Could not compact ${agent.agentName}, continuing with full history: ${e.message}`);
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
hear(agent, message) {
|
|
666
|
+
if (!message.canHear(agent.agentNickname)) {
|
|
667
|
+
return false;
|
|
480
668
|
}
|
|
481
669
|
agent.addToConversationHistory(message, false);
|
|
482
670
|
agent.save();
|
|
671
|
+
return true;
|
|
672
|
+
}
|
|
673
|
+
perceive(agent, message, onResponse) {
|
|
674
|
+
if (!this.hear(agent, message)) {
|
|
675
|
+
onResponse(null);
|
|
676
|
+
return;
|
|
677
|
+
}
|
|
483
678
|
if (!agent.canRespond(message)) {
|
|
484
679
|
onResponse(null);
|
|
485
680
|
return;
|
|
@@ -490,13 +685,16 @@ class Room {
|
|
|
490
685
|
this.getAgentAiResponse(agent, message, onResponse);
|
|
491
686
|
}
|
|
492
687
|
async moderate(moderatorAgent, message, onResponse) {
|
|
493
|
-
const
|
|
494
|
-
const
|
|
688
|
+
const humanAgents = this.getHumanAgents();
|
|
689
|
+
const humanAgent = humanAgents[0];
|
|
690
|
+
const humanAgentNames = humanAgents.map((a) => a.agentNickname);
|
|
691
|
+
const humanAgentName = humanAgentNames[0] ?? 'Player';
|
|
495
692
|
const visibleArtifacts = this.getVisibleArtifacts(moderatorAgent.agentNickname)
|
|
496
|
-
.map((a) => `${a.name} (by ${a.creator}): ${a
|
|
693
|
+
.map((a) => `${a.name} (by ${a.creator}): ${Room.renderArtifactForPrompt(a)}`)
|
|
497
694
|
.join('\n\n');
|
|
695
|
+
const moderatorReadable = this.getReadableArtifacts(moderatorAgent.agentNickname);
|
|
498
696
|
const invisibleArtifactNames = this.getArtifacts()
|
|
499
|
-
.filter((a) => !
|
|
697
|
+
.filter((a) => !moderatorReadable.includes(a))
|
|
500
698
|
.map((a) => a.name)
|
|
501
699
|
.join(', ');
|
|
502
700
|
const variables = {
|
|
@@ -510,6 +708,7 @@ class Room {
|
|
|
510
708
|
scenario: this.getGoal(),
|
|
511
709
|
humanAgent: humanAgent,
|
|
512
710
|
humanAgentName: humanAgentName,
|
|
711
|
+
humanAgentNames: humanAgentNames,
|
|
513
712
|
visibleArtifacts: visibleArtifacts,
|
|
514
713
|
invisibleArtifactNames: invisibleArtifactNames,
|
|
515
714
|
};
|
|
@@ -518,6 +717,9 @@ ${this.buildSituationHeader()}
|
|
|
518
717
|
Sender: ${message.getSender()}
|
|
519
718
|
Recent Message: ${message.getContent()}
|
|
520
719
|
`;
|
|
720
|
+
if (this.lastModeratedAudience.length > 0) {
|
|
721
|
+
prompt += `\nAudience of the previous message: ${this.lastModeratedAudience.join(', ')}\n`;
|
|
722
|
+
}
|
|
521
723
|
const characterContexts = this.buildCharacterPrivateContextDigest();
|
|
522
724
|
if (characterContexts) {
|
|
523
725
|
prompt += `\nCharacter Inventories and Conditions:\n${characterContexts}\n`;
|
|
@@ -543,8 +745,7 @@ Recent Message: ${message.getContent()}
|
|
|
543
745
|
this.notifyError(error);
|
|
544
746
|
const balanceMessage = new message_1.RoomMessage('System', app_types_1.MessageAction.SYSTEM, `Conversation paused: ${error.message}`, [], null, null);
|
|
545
747
|
this.addRoomMessage(balanceMessage);
|
|
546
|
-
this.
|
|
547
|
-
this.notifyStateChange();
|
|
748
|
+
this.endTurn('keep');
|
|
548
749
|
if (onResponse) {
|
|
549
750
|
onResponse(null);
|
|
550
751
|
}
|
|
@@ -552,7 +753,7 @@ Recent Message: ${message.getContent()}
|
|
|
552
753
|
findArtifact(name) {
|
|
553
754
|
return this.artifacts.find((a) => a.name === name);
|
|
554
755
|
}
|
|
555
|
-
publishArtifact(name, creator, body, viewers = []) {
|
|
756
|
+
publishArtifact(name, creator, body, viewers = [], transient) {
|
|
556
757
|
if (!body || body.trim().length === 0) {
|
|
557
758
|
this.deleteArtifact(name);
|
|
558
759
|
return null;
|
|
@@ -563,6 +764,9 @@ Recent Message: ${message.getContent()}
|
|
|
563
764
|
this.artifacts[existingIndex].body = body;
|
|
564
765
|
this.artifacts[existingIndex].creator = creator;
|
|
565
766
|
this.artifacts[existingIndex].viewers = viewers.length > 0 ? viewers : [creator];
|
|
767
|
+
if (transient !== undefined) {
|
|
768
|
+
this.artifacts[existingIndex].transient = transient;
|
|
769
|
+
}
|
|
566
770
|
returnedArtifact = this.artifacts[existingIndex];
|
|
567
771
|
}
|
|
568
772
|
else {
|
|
@@ -571,6 +775,7 @@ Recent Message: ${message.getContent()}
|
|
|
571
775
|
creator,
|
|
572
776
|
body,
|
|
573
777
|
viewers: viewers.length > 0 ? viewers : [creator],
|
|
778
|
+
...(transient !== undefined && { transient }),
|
|
574
779
|
};
|
|
575
780
|
this.artifacts.push(newArtifact);
|
|
576
781
|
returnedArtifact = newArtifact;
|
|
@@ -579,6 +784,7 @@ Recent Message: ${message.getContent()}
|
|
|
579
784
|
this.roomStoreService.saveArtifactFile(this.id, this.storageDir, returnedArtifact);
|
|
580
785
|
}
|
|
581
786
|
this.saveMetadata();
|
|
787
|
+
this.notifyStateChange();
|
|
582
788
|
return returnedArtifact;
|
|
583
789
|
}
|
|
584
790
|
deleteArtifact(name) {
|
|
@@ -592,11 +798,17 @@ Recent Message: ${message.getContent()}
|
|
|
592
798
|
}
|
|
593
799
|
return true;
|
|
594
800
|
}
|
|
595
|
-
|
|
801
|
+
getReadableArtifacts(agentNickname) {
|
|
596
802
|
return this.artifacts.filter((a) => a.creator === agentNickname ||
|
|
597
803
|
(a.viewers && a.viewers.includes(agentNickname)) ||
|
|
598
804
|
!a.viewers);
|
|
599
805
|
}
|
|
806
|
+
getVisibleArtifacts(agentNickname) {
|
|
807
|
+
return this.getReadableArtifacts(agentNickname).filter((a) => !a.transient);
|
|
808
|
+
}
|
|
809
|
+
getTransientArtifacts(agentNickname) {
|
|
810
|
+
return this.getReadableArtifacts(agentNickname).filter((a) => a.transient);
|
|
811
|
+
}
|
|
600
812
|
getId() {
|
|
601
813
|
return this.id;
|
|
602
814
|
}
|
|
@@ -657,13 +869,40 @@ Recent Message: ${message.getContent()}
|
|
|
657
869
|
this.onError(error);
|
|
658
870
|
}
|
|
659
871
|
}
|
|
872
|
+
getHumanAgents() {
|
|
873
|
+
return this.agents.filter((agent) => agent.isControlledByHuman);
|
|
874
|
+
}
|
|
875
|
+
getDefaultHumanAgent() {
|
|
876
|
+
return this.agents.find((agent) => agent.isControlledByHuman);
|
|
877
|
+
}
|
|
878
|
+
getHumanAgentByName(sender) {
|
|
879
|
+
if (!sender)
|
|
880
|
+
return undefined;
|
|
881
|
+
return this.getHumanAgents().find((agent) => this.checkAgentNicknameMatch(agent.agentNickname, sender));
|
|
882
|
+
}
|
|
883
|
+
isHumanNickname(nickname) {
|
|
884
|
+
return !!nickname && !!this.getHumanAgentByName(nickname);
|
|
885
|
+
}
|
|
886
|
+
isPlayerAside(message) {
|
|
887
|
+
if (message.getAction() !== app_types_1.MessageAction.WHISPER)
|
|
888
|
+
return false;
|
|
889
|
+
const targets = message.getTargets();
|
|
890
|
+
if (targets.length === 0)
|
|
891
|
+
return false;
|
|
892
|
+
return targets.every((target) => this.isHumanNickname(target));
|
|
893
|
+
}
|
|
660
894
|
getHumanAgentName() {
|
|
661
|
-
|
|
662
|
-
return humanAgent ? humanAgent.agentNickname : '';
|
|
895
|
+
return this.getDefaultHumanAgent()?.agentNickname ?? '';
|
|
663
896
|
}
|
|
664
897
|
getModeratorAgent() {
|
|
665
|
-
if (this.
|
|
666
|
-
|
|
898
|
+
if (this.moderator) {
|
|
899
|
+
const named = this.agents.find((a) => this.checkAgentNicknameMatch(a.agentNickname, this.moderator));
|
|
900
|
+
if (named)
|
|
901
|
+
return named;
|
|
902
|
+
logger_1.logger.warn(`Room ${this.name}: configured moderator "${this.moderator}" is not in the room; falling back to the first agent`);
|
|
903
|
+
}
|
|
904
|
+
const candidates = this.agents.filter((a) => !a.isControlledByHuman);
|
|
905
|
+
return candidates.find((a) => a.hasPrompt('moderate')) ?? candidates[0];
|
|
667
906
|
}
|
|
668
907
|
getSituation() {
|
|
669
908
|
return this.currentSituation;
|
|
@@ -747,8 +986,24 @@ Recent Message: ${message.getContent()}
|
|
|
747
986
|
if (!message.sender) {
|
|
748
987
|
message.sender = 'system';
|
|
749
988
|
}
|
|
750
|
-
const humanAgent = this.
|
|
751
|
-
const isHumanMessage = humanAgent
|
|
989
|
+
const humanAgent = this.getHumanAgentByName(message.sender);
|
|
990
|
+
const isHumanMessage = !!humanAgent;
|
|
991
|
+
if (isHumanMessage && humanAgent.useModerator && message.action !== app_types_1.MessageAction.MODERATE) {
|
|
992
|
+
logger_1.logger.log(`${humanAgent.agentNickname} uses the moderator; sending as moderate rather than ${message.action}`);
|
|
993
|
+
message.action = app_types_1.MessageAction.MODERATE;
|
|
994
|
+
message.targets = [];
|
|
995
|
+
}
|
|
996
|
+
if (isHumanMessage && this.isPlayerAside(message)) {
|
|
997
|
+
for (const target of message.getTargets()) {
|
|
998
|
+
const player = this.getHumanAgentByName(target);
|
|
999
|
+
if (player)
|
|
1000
|
+
this.hear(player, message);
|
|
1001
|
+
}
|
|
1002
|
+
humanAgent.addToConversationHistory(message, true);
|
|
1003
|
+
humanAgent.save();
|
|
1004
|
+
this.addRoomMessage(message);
|
|
1005
|
+
return { accepted: true };
|
|
1006
|
+
}
|
|
752
1007
|
if (isHumanMessage && this.isPaused) {
|
|
753
1008
|
if (clearQueue) {
|
|
754
1009
|
this.pendingMessages = [];
|
|
@@ -760,7 +1015,7 @@ Recent Message: ${message.getContent()}
|
|
|
760
1015
|
return { accepted: true };
|
|
761
1016
|
}
|
|
762
1017
|
if (isHumanMessage) {
|
|
763
|
-
if (this.currentSpeaker !== null && this.currentSpeaker
|
|
1018
|
+
if (this.currentSpeaker !== null && !this.isHumanNickname(this.currentSpeaker)) {
|
|
764
1019
|
logger_1.logger.warn(`Human agent ${humanAgent.agentNickname} tried to speak out of turn. Current speaker: ${this.currentSpeaker}`);
|
|
765
1020
|
return {
|
|
766
1021
|
accepted: false,
|
|
@@ -781,11 +1036,10 @@ Recent Message: ${message.getContent()}
|
|
|
781
1036
|
return this.processingInProgress;
|
|
782
1037
|
}
|
|
783
1038
|
isWaitingForHuman() {
|
|
784
|
-
|
|
785
|
-
if (!humanAgent) {
|
|
1039
|
+
if (this.getHumanAgents().length === 0) {
|
|
786
1040
|
return false;
|
|
787
1041
|
}
|
|
788
|
-
if (this.currentSpeaker
|
|
1042
|
+
if (this.isHumanNickname(this.currentSpeaker)) {
|
|
789
1043
|
return true;
|
|
790
1044
|
}
|
|
791
1045
|
return (this.currentSpeaker === null &&
|
|
@@ -794,7 +1048,7 @@ Recent Message: ${message.getContent()}
|
|
|
794
1048
|
}
|
|
795
1049
|
pause() {
|
|
796
1050
|
this.isPaused = true;
|
|
797
|
-
const humanAgent = this.
|
|
1051
|
+
const humanAgent = this.getDefaultHumanAgent();
|
|
798
1052
|
if (humanAgent) {
|
|
799
1053
|
this.currentSpeaker = humanAgent.agentNickname;
|
|
800
1054
|
}
|
|
@@ -812,6 +1066,8 @@ Recent Message: ${message.getContent()}
|
|
|
812
1066
|
this.pause();
|
|
813
1067
|
}
|
|
814
1068
|
checkAgentNicknameMatch(existingNickname, targetNickname) {
|
|
1069
|
+
if (!existingNickname || !targetNickname)
|
|
1070
|
+
return false;
|
|
815
1071
|
if (existingNickname.toLowerCase() === targetNickname.toLowerCase())
|
|
816
1072
|
return true;
|
|
817
1073
|
const nicknameFirstWord = existingNickname.split(/\s+/)[0];
|
|
@@ -819,13 +1075,20 @@ Recent Message: ${message.getContent()}
|
|
|
819
1075
|
return true;
|
|
820
1076
|
return false;
|
|
821
1077
|
}
|
|
1078
|
+
deliverModerated(recipient, message, content) {
|
|
1079
|
+
if (recipient.isControlledByHuman) {
|
|
1080
|
+
recipient.addToConversationHistory(`From: ${message.sender}\n${content}`, false);
|
|
1081
|
+
}
|
|
1082
|
+
else {
|
|
1083
|
+
recipient.addToConversationHistory(message, false);
|
|
1084
|
+
}
|
|
1085
|
+
recipient.save();
|
|
1086
|
+
}
|
|
822
1087
|
handleModeratorResponse(moderatorMessage, originalMessage) {
|
|
823
1088
|
try {
|
|
824
1089
|
let responderNickname = moderatorMessage.name;
|
|
825
1090
|
const roomLeaderName = this.getRoomLeader();
|
|
826
|
-
const targets = moderatorMessage.targets || [];
|
|
827
|
-
if (!targets.includes(roomLeaderName))
|
|
828
|
-
targets.push(roomLeaderName);
|
|
1091
|
+
const targets = [...(moderatorMessage.targets || [])];
|
|
829
1092
|
if (this.onModeratorResponseCallback) {
|
|
830
1093
|
this.onModeratorResponseCallback(moderatorMessage);
|
|
831
1094
|
}
|
|
@@ -843,65 +1106,58 @@ Recent Message: ${message.getContent()}
|
|
|
843
1106
|
if (!targets.includes(roomLeaderName))
|
|
844
1107
|
targets.push(roomLeaderName);
|
|
845
1108
|
}
|
|
846
|
-
|
|
1109
|
+
let responderAgent = this.agents.find((a) => this.checkAgentNicknameMatch(a.agentNickname, responderNickname));
|
|
1110
|
+
if (!responderAgent) {
|
|
1111
|
+
let roomLeaderAgent = this.agents.find((a) => a.agentNickname === roomLeaderName);
|
|
1112
|
+
if (!roomLeaderAgent)
|
|
1113
|
+
throw new Error(`Room leader agent not found`);
|
|
1114
|
+
logger_1.logger.warn(`Responder agent "${responderNickname}" not found; falling back to room leader: ${roomLeaderAgent.agentNickname}`);
|
|
1115
|
+
responderAgent = roomLeaderAgent;
|
|
1116
|
+
responderNickname = roomLeaderAgent.agentNickname;
|
|
1117
|
+
}
|
|
1118
|
+
if (!targets.some((target) => this.checkAgentNicknameMatch(responderAgent.agentNickname, target))) {
|
|
1119
|
+
targets.push(responderAgent.agentNickname);
|
|
1120
|
+
}
|
|
1121
|
+
logger_1.logger.log(`Moderator routing: sender=${originalMessage.sender} action=${moderatorMessage.action}${moderatorMessage.relayAs ? ` as ${moderatorMessage.relayAs}` : ''}, name=${responderNickname}, targets=${targets.join(', ')}`);
|
|
1122
|
+
const relayed = new message_1.RoomMessage(originalMessage.sender, moderatorMessage.relayAs ?? originalMessage.action, content, targets, originalMessage.name, originalMessage.situation);
|
|
847
1123
|
if (moderatorMessage.action === app_types_1.MessageAction.RELAY) {
|
|
848
|
-
this.addRoomMessage(
|
|
1124
|
+
this.addRoomMessage(relayed, {
|
|
1125
|
+
suppressSituationUpdate: true,
|
|
1126
|
+
});
|
|
849
1127
|
}
|
|
850
1128
|
for (const recipientName of targets) {
|
|
851
|
-
if (recipientName
|
|
1129
|
+
if (this.checkAgentNicknameMatch(responderAgent.agentNickname, recipientName)) {
|
|
852
1130
|
continue;
|
|
1131
|
+
}
|
|
853
1132
|
if (recipientName === originalMessage.sender)
|
|
854
1133
|
continue;
|
|
855
1134
|
const recipient = this.agents.find((a) => this.checkAgentNicknameMatch(a.agentNickname, recipientName));
|
|
856
|
-
if (recipient)
|
|
857
|
-
|
|
858
|
-
recipient.save();
|
|
859
|
-
}
|
|
860
|
-
}
|
|
861
|
-
let responderAgent = this.agents.find((a) => this.checkAgentNicknameMatch(a.agentNickname, responderNickname));
|
|
862
|
-
if (!responderAgent) {
|
|
863
|
-
let roomLeaderAgent = this.agents.find((a) => a.agentNickname === this.getRoomLeader());
|
|
864
|
-
if (!roomLeaderAgent)
|
|
865
|
-
throw new Error(`Room leader agent not found`);
|
|
866
|
-
logger_1.logger.warn(`Responder agent "${responderNickname}" not found; falling back to room leader: ${roomLeaderAgent.agentNickname}`);
|
|
867
|
-
responderAgent = roomLeaderAgent;
|
|
1135
|
+
if (recipient)
|
|
1136
|
+
this.deliverModerated(recipient, relayed, content);
|
|
868
1137
|
}
|
|
1138
|
+
this.lastModeratedAudience = [...targets];
|
|
869
1139
|
if (responderAgent.isControlledByHuman) {
|
|
870
1140
|
logger_1.logger.log(`Waiting for human agent ${responderNickname} to respond`);
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
this.processingInProgress = false;
|
|
874
|
-
this.notifyStateChange();
|
|
875
|
-
this.setCurrentSpeaker(responderNickname);
|
|
1141
|
+
this.deliverModerated(responderAgent, relayed, content);
|
|
1142
|
+
this.endTurn({ to: responderNickname });
|
|
876
1143
|
return;
|
|
877
1144
|
}
|
|
878
1145
|
this.setCurrentSpeaker(responderNickname);
|
|
879
|
-
|
|
880
|
-
this.perceive(responderAgent, originalMessage, (response) => {
|
|
1146
|
+
this.perceive(responderAgent, relayed, (response) => {
|
|
881
1147
|
if (this.isPaused) {
|
|
882
|
-
this.
|
|
883
|
-
this.notifyStateChange();
|
|
1148
|
+
this.endTurn('keep');
|
|
884
1149
|
return;
|
|
885
1150
|
}
|
|
886
1151
|
if (response !== null) {
|
|
887
1152
|
this.pendingMessages = [response, ...this.pendingMessages];
|
|
888
1153
|
}
|
|
889
|
-
this.
|
|
890
|
-
this.processingInProgress = false;
|
|
891
|
-
this.notifyStateChange();
|
|
892
|
-
if (this.pendingMessages && this.pendingMessages.length > 0) {
|
|
893
|
-
this.processPendingMessages();
|
|
894
|
-
}
|
|
1154
|
+
this.endTurn('release', { drain: true });
|
|
895
1155
|
});
|
|
896
1156
|
}
|
|
897
1157
|
catch (error) {
|
|
898
1158
|
logger_1.logger.error('Error parsing moderator response:', error);
|
|
899
1159
|
logger_1.logger.error('Moderator message content:', moderatorMessage.content);
|
|
900
|
-
this.
|
|
901
|
-
this.notifyStateChange();
|
|
902
|
-
if (this.pendingMessages && this.pendingMessages.length > 0) {
|
|
903
|
-
this.processPendingMessages();
|
|
904
|
-
}
|
|
1160
|
+
this.endTurn('release', { drain: true });
|
|
905
1161
|
}
|
|
906
1162
|
}
|
|
907
1163
|
async runDeliverTurn(agent, commission) {
|
|
@@ -969,10 +1225,8 @@ Recent Message: ${message.getContent()}
|
|
|
969
1225
|
}
|
|
970
1226
|
const settled = await Promise.allSettled(aiTargets.map((t) => this.runDeliverTurn(t, msg)));
|
|
971
1227
|
if (this.isPaused || this.isAdjourned || !this.processingInProgress) {
|
|
972
|
-
if (this.processingInProgress)
|
|
973
|
-
this.
|
|
974
|
-
this.notifyStateChange();
|
|
975
|
-
}
|
|
1228
|
+
if (this.processingInProgress)
|
|
1229
|
+
this.endTurn('keep');
|
|
976
1230
|
return;
|
|
977
1231
|
}
|
|
978
1232
|
const delivers = settled.map((result, i) => {
|
|
@@ -1003,11 +1257,7 @@ Recent Message: ${message.getContent()}
|
|
|
1003
1257
|
}
|
|
1004
1258
|
catch (error) {
|
|
1005
1259
|
logger_1.logger.error('Error handling commission message:', error);
|
|
1006
|
-
this.
|
|
1007
|
-
this.notifyStateChange();
|
|
1008
|
-
if (this.pendingMessages && this.pendingMessages.length > 0) {
|
|
1009
|
-
this.processPendingMessages();
|
|
1010
|
-
}
|
|
1260
|
+
this.endTurn('release', { drain: true });
|
|
1011
1261
|
}
|
|
1012
1262
|
}
|
|
1013
1263
|
async respondToCommissioner(commissioner, note) {
|
|
@@ -1021,9 +1271,7 @@ Recent Message: ${message.getContent()}
|
|
|
1021
1271
|
this.addRoomMessage(new message_1.RoomMessage('system', app_types_1.MessageAction.SYSTEM, note, [
|
|
1022
1272
|
commissioner.agentNickname,
|
|
1023
1273
|
]));
|
|
1024
|
-
this.
|
|
1025
|
-
this.notifyStateChange();
|
|
1026
|
-
this.setCurrentSpeaker(commissioner.agentNickname);
|
|
1274
|
+
this.endTurn({ to: commissioner.agentNickname });
|
|
1027
1275
|
return;
|
|
1028
1276
|
}
|
|
1029
1277
|
let reply = null;
|
|
@@ -1041,11 +1289,7 @@ Recent Message: ${message.getContent()}
|
|
|
1041
1289
|
this.pendingMessages = [...(this.pendingMessages || []), reply];
|
|
1042
1290
|
}
|
|
1043
1291
|
}
|
|
1044
|
-
this.
|
|
1045
|
-
this.notifyStateChange();
|
|
1046
|
-
if (this.pendingMessages && this.pendingMessages.length > 0) {
|
|
1047
|
-
this.processPendingMessages();
|
|
1048
|
-
}
|
|
1292
|
+
this.endTurn(this.isPaused ? 'keep' : 'release', { drain: true });
|
|
1049
1293
|
}
|
|
1050
1294
|
processPendingMessages() {
|
|
1051
1295
|
if (this.isPaused ||
|
|
@@ -1067,8 +1311,7 @@ Recent Message: ${message.getContent()}
|
|
|
1067
1311
|
if (this.isAdjourned) {
|
|
1068
1312
|
logger_1.logger.log(`Room ${this.name} has been adjourned. Message processing stopped.`);
|
|
1069
1313
|
this.pendingMessages = null;
|
|
1070
|
-
this.
|
|
1071
|
-
this.notifyStateChange();
|
|
1314
|
+
this.endTurn('release');
|
|
1072
1315
|
return;
|
|
1073
1316
|
}
|
|
1074
1317
|
const moderateMessages = messages.filter((m) => m.getAction() === app_types_1.MessageAction.MODERATE);
|
|
@@ -1080,8 +1323,7 @@ Recent Message: ${message.getContent()}
|
|
|
1080
1323
|
const moderator = this.getModeratorAgent();
|
|
1081
1324
|
if (!moderator) {
|
|
1082
1325
|
logger_1.logger.warn('MODERATE messages found but no moderator agent in room');
|
|
1083
|
-
this.
|
|
1084
|
-
this.notifyStateChange();
|
|
1326
|
+
this.endTurn('release');
|
|
1085
1327
|
return;
|
|
1086
1328
|
}
|
|
1087
1329
|
const originalMessage = moderateMessages[0];
|
|
@@ -1094,29 +1336,20 @@ Recent Message: ${message.getContent()}
|
|
|
1094
1336
|
}
|
|
1095
1337
|
await this.moderate(moderator, originalMessage, (response) => {
|
|
1096
1338
|
if (this.isPaused) {
|
|
1097
|
-
this.
|
|
1098
|
-
this.notifyStateChange();
|
|
1339
|
+
this.endTurn('keep');
|
|
1099
1340
|
return;
|
|
1100
1341
|
}
|
|
1101
1342
|
if (response !== null) {
|
|
1102
1343
|
this.handleModeratorResponse(response, originalMessage);
|
|
1103
1344
|
}
|
|
1104
1345
|
else {
|
|
1105
|
-
this.
|
|
1106
|
-
this.notifyStateChange();
|
|
1107
|
-
if (this.pendingMessages && this.pendingMessages.length > 0) {
|
|
1108
|
-
this.processPendingMessages();
|
|
1109
|
-
}
|
|
1346
|
+
this.endTurn('release', { drain: true });
|
|
1110
1347
|
}
|
|
1111
1348
|
});
|
|
1112
1349
|
};
|
|
1113
1350
|
dispatchToModerator().catch((error) => {
|
|
1114
1351
|
logger_1.logger.error('Error dispatching message to moderator:', error);
|
|
1115
|
-
this.
|
|
1116
|
-
this.notifyStateChange();
|
|
1117
|
-
if (this.pendingMessages && this.pendingMessages.length > 0) {
|
|
1118
|
-
this.processPendingMessages();
|
|
1119
|
-
}
|
|
1352
|
+
this.endTurn('release', { drain: true });
|
|
1120
1353
|
});
|
|
1121
1354
|
return;
|
|
1122
1355
|
}
|
|
@@ -1141,14 +1374,29 @@ Recent Message: ${message.getContent()}
|
|
|
1141
1374
|
void this.handleCommissionMessage(nextMessage);
|
|
1142
1375
|
return;
|
|
1143
1376
|
}
|
|
1144
|
-
this.
|
|
1145
|
-
|
|
1377
|
+
const responders = this.selectResponders(nextMessage);
|
|
1378
|
+
for (const agent of this.agents) {
|
|
1379
|
+
if (responders.includes(agent))
|
|
1380
|
+
continue;
|
|
1381
|
+
this.hear(agent, nextMessage);
|
|
1382
|
+
}
|
|
1383
|
+
const humanResponders = responders.filter((a) => a.isControlledByHuman);
|
|
1384
|
+
if (humanResponders.length > 0 && humanResponders.length === responders.length) {
|
|
1385
|
+
for (const human of humanResponders) {
|
|
1386
|
+
this.hear(human, nextMessage);
|
|
1387
|
+
}
|
|
1388
|
+
this.endTurn({ to: humanResponders[0].agentNickname });
|
|
1389
|
+
return;
|
|
1390
|
+
}
|
|
1391
|
+
this.setCurrentSpeaker(this.turnMode === 'directed' && responders.length > 0
|
|
1392
|
+
? responders[0].agentNickname
|
|
1393
|
+
: nextMessage.targets[0]);
|
|
1394
|
+
let agentsInProgress = responders.length;
|
|
1146
1395
|
if (agentsInProgress === 0) {
|
|
1147
|
-
this.
|
|
1148
|
-
this.notifyStateChange();
|
|
1396
|
+
this.endTurn('release', { drain: true });
|
|
1149
1397
|
return;
|
|
1150
1398
|
}
|
|
1151
|
-
for (const agent of
|
|
1399
|
+
for (const agent of responders) {
|
|
1152
1400
|
this.perceive(agent, nextMessage, (message) => {
|
|
1153
1401
|
agentsInProgress--;
|
|
1154
1402
|
if (message !== null) {
|
|
@@ -1163,15 +1411,35 @@ Recent Message: ${message.getContent()}
|
|
|
1163
1411
|
}
|
|
1164
1412
|
}
|
|
1165
1413
|
if (agentsInProgress === 0) {
|
|
1166
|
-
this.
|
|
1167
|
-
this.notifyStateChange();
|
|
1168
|
-
if (this.pendingMessages && this.pendingMessages.length > 0) {
|
|
1169
|
-
this.processPendingMessages();
|
|
1170
|
-
}
|
|
1414
|
+
this.endTurn(this.isPaused ? 'keep' : 'release', { drain: true });
|
|
1171
1415
|
}
|
|
1172
1416
|
});
|
|
1173
1417
|
}
|
|
1174
1418
|
}
|
|
1419
|
+
selectResponders(message) {
|
|
1420
|
+
if (this.turnMode !== 'directed') {
|
|
1421
|
+
return [...this.agents];
|
|
1422
|
+
}
|
|
1423
|
+
const action = message.getAction();
|
|
1424
|
+
if (action === app_types_1.MessageAction.THINK || action === app_types_1.MessageAction.TOOL) {
|
|
1425
|
+
return [];
|
|
1426
|
+
}
|
|
1427
|
+
for (const name of message.getTargets() || []) {
|
|
1428
|
+
const target = this.agents.find((a) => this.checkAgentNicknameMatch(a.agentNickname, name));
|
|
1429
|
+
if (!target)
|
|
1430
|
+
continue;
|
|
1431
|
+
if (target.agentNickname === message.getSender())
|
|
1432
|
+
continue;
|
|
1433
|
+
return [target];
|
|
1434
|
+
}
|
|
1435
|
+
const leader = this.agents.find((a) => a.agentNickname === this.getRoomLeader());
|
|
1436
|
+
if (leader && leader.agentNickname !== message.getSender()) {
|
|
1437
|
+
return [leader];
|
|
1438
|
+
}
|
|
1439
|
+
const others = this.agents.filter((a) => a.agentNickname !== message.getSender() && message.canHear(a.agentNickname));
|
|
1440
|
+
logger_1.logger.warn(`Directed room ${this.name}: neither a target nor the leader could answer ${message.getSender()}; widening to ${others.length} other agent(s)`);
|
|
1441
|
+
return others;
|
|
1442
|
+
}
|
|
1175
1443
|
filterConcurrentMessages(messages) {
|
|
1176
1444
|
if (messages.length == 0)
|
|
1177
1445
|
return null;
|