briyah 2.0.2 → 2.0.4
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/LICENSE +216 -0
- package/README.md +43 -7
- package/data/common/config/LICENSE-model-prices.md +65 -0
- package/data/common/config/markup +1 -1
- package/data/common/config/model_prices.json +47443 -46295
- package/data/common/prompts/narrator/create_story.prompt +2 -0
- package/data/packs/dnd5e/agents/player.json +11 -11
- package/data/packs/dnd5e/prompts/character_creator/defaults_config.json +21 -20
- package/data/packs/dnd5e/prompts/character_creator/perceive.prompt +19 -12
- package/data/packs/dnd5e/prompts/dnd_player/perceive.prompt +10 -6
- package/dist-sdk/server/src/ai/LLM/anthropic.service.d.ts +103 -0
- package/dist-sdk/server/src/ai/LLM/anthropic.service.js +173 -2
- package/dist-sdk/server/src/ai/LLM/base-ai.service.d.ts +172 -3
- package/dist-sdk/server/src/ai/LLM/base-ai.service.js +241 -5
- package/dist-sdk/server/src/ai/LLM/comfyui-client.d.ts +26 -0
- package/dist-sdk/server/src/ai/LLM/comfyui-client.js +25 -0
- package/dist-sdk/server/src/ai/LLM/comfyui-workflow.d.ts +54 -0
- package/dist-sdk/server/src/ai/LLM/comfyui-workflow.js +59 -1
- package/dist-sdk/server/src/ai/LLM/comfyui.service.d.ts +21 -1
- package/dist-sdk/server/src/ai/LLM/comfyui.service.js +25 -1
- package/dist-sdk/server/src/ai/LLM/deepseek.service.d.ts +52 -0
- package/dist-sdk/server/src/ai/LLM/deepseek.service.js +102 -3
- package/dist-sdk/server/src/ai/LLM/fal.service.d.ts +25 -0
- package/dist-sdk/server/src/ai/LLM/fal.service.js +55 -1
- package/dist-sdk/server/src/ai/LLM/googleai.service.d.ts +19 -0
- package/dist-sdk/server/src/ai/LLM/googleai.service.js +42 -1
- package/dist-sdk/server/src/ai/LLM/grok.service.d.ts +22 -0
- package/dist-sdk/server/src/ai/LLM/grok.service.js +54 -2
- package/dist-sdk/server/src/ai/LLM/mock.service.js +31 -4
- package/dist-sdk/server/src/ai/LLM/openai.service.d.ts +30 -0
- package/dist-sdk/server/src/ai/LLM/openai.service.js +80 -2
- package/dist-sdk/server/src/ai/LLM/self-hosted.service.d.ts +21 -0
- package/dist-sdk/server/src/ai/LLM/self-hosted.service.js +44 -2
- package/dist-sdk/server/src/ai/LLM/together.service.d.ts +30 -0
- package/dist-sdk/server/src/ai/LLM/together.service.js +60 -2
- package/dist-sdk/server/src/ai/agent-config.d.ts +15 -0
- package/dist-sdk/server/src/ai/agent-factory.d.ts +16 -0
- package/dist-sdk/server/src/ai/agent-factory.js +20 -0
- package/dist-sdk/server/src/ai/agent-message.service.d.ts +30 -0
- package/dist-sdk/server/src/ai/agent-message.service.js +30 -0
- package/dist-sdk/server/src/ai/agent-store.service.d.ts +80 -0
- package/dist-sdk/server/src/ai/agent-store.service.js +166 -5
- package/dist-sdk/server/src/ai/agent.d.ts +196 -0
- package/dist-sdk/server/src/ai/agent.js +224 -11
- package/dist-sdk/server/src/ai/ai-debug-logger.d.ts +18 -0
- package/dist-sdk/server/src/ai/ai-debug-logger.js +19 -0
- package/dist-sdk/server/src/ai/ai-factory.service.d.ts +9 -0
- package/dist-sdk/server/src/ai/ai-factory.service.js +9 -0
- package/dist-sdk/server/src/ai/artifact.service.d.ts +131 -0
- package/dist-sdk/server/src/ai/artifact.service.js +148 -3
- package/dist-sdk/server/src/ai/attached-file.service.d.ts +87 -0
- package/dist-sdk/server/src/ai/attached-file.service.js +105 -0
- package/dist-sdk/server/src/ai/model_prices.d.ts +30 -0
- package/dist-sdk/server/src/ai/model_prices.js +91 -5
- package/dist-sdk/server/src/ai/pack-artifacts.d.ts +84 -0
- package/dist-sdk/server/src/ai/pack-artifacts.js +168 -0
- package/dist-sdk/server/src/ai/published-agents.service.d.ts +25 -0
- package/dist-sdk/server/src/ai/published-agents.service.js +30 -3
- package/dist-sdk/server/src/app/balance-message.service.d.ts +24 -0
- package/dist-sdk/server/src/app/balance-message.service.js +24 -0
- package/dist-sdk/server/src/app/balance.module.d.ts +4 -0
- package/dist-sdk/server/src/app/balance.module.js +4 -0
- package/dist-sdk/server/src/app/balance.service.d.ts +37 -0
- package/dist-sdk/server/src/app/balance.service.js +54 -0
- package/dist-sdk/server/src/app/stripe.controller.d.ts +21 -0
- package/dist-sdk/server/src/app/stripe.controller.js +35 -0
- package/dist-sdk/server/src/app/stripe.service.d.ts +43 -0
- package/dist-sdk/server/src/app/stripe.service.js +62 -0
- package/dist-sdk/server/src/app/transaction.service.d.ts +27 -0
- package/dist-sdk/server/src/app/transaction.service.js +36 -0
- package/dist-sdk/server/src/app/user-service-factory.d.ts +8 -0
- package/dist-sdk/server/src/app/user-service-factory.js +38 -1
- package/dist-sdk/server/src/app/user-service-manager.d.ts +27 -0
- package/dist-sdk/server/src/app/user-service-manager.js +43 -0
- package/dist-sdk/server/src/app.controller.d.ts +87 -0
- package/dist-sdk/server/src/app.controller.js +272 -12
- package/dist-sdk/server/src/app.module.js +28 -6
- package/dist-sdk/server/src/app.service.d.ts +1662 -0
- package/dist-sdk/server/src/app.service.js +1862 -6
- package/dist-sdk/server/src/auth/agent-access.decorator.d.ts +17 -0
- package/dist-sdk/server/src/auth/agent-access.decorator.js +17 -0
- package/dist-sdk/server/src/auth/auth.controller.js +46 -0
- package/dist-sdk/server/src/auth/auth.module.js +3 -2
- package/dist-sdk/server/src/auth/auth.service.d.ts +33 -0
- package/dist-sdk/server/src/auth/auth.service.js +39 -3
- package/dist-sdk/server/src/auth/jwt-auth.guard.js +18 -0
- package/dist-sdk/server/src/auth/jwt.strategy.js +2 -2
- package/dist-sdk/server/src/auth/owner-only.decorator.d.ts +9 -0
- package/dist-sdk/server/src/auth/owner-only.decorator.js +9 -0
- package/dist-sdk/server/src/auth/phone-validation.service.d.ts +25 -0
- package/dist-sdk/server/src/auth/phone-validation.service.js +30 -0
- package/dist-sdk/server/src/auth/rate-limit.service.d.ts +19 -0
- package/dist-sdk/server/src/auth/rate-limit.service.js +24 -0
- package/dist-sdk/server/src/auth/room-access.decorator.d.ts +8 -0
- package/dist-sdk/server/src/auth/room-access.decorator.js +8 -0
- package/dist-sdk/server/src/auth/scopes.decorator.d.ts +7 -0
- package/dist-sdk/server/src/auth/scopes.decorator.js +7 -0
- package/dist-sdk/server/src/auth/scopes.guard.d.ts +16 -0
- package/dist-sdk/server/src/auth/scopes.guard.js +30 -0
- package/dist-sdk/server/src/auth/session.guard.d.ts +4 -0
- package/dist-sdk/server/src/auth/session.guard.js +8 -0
- package/dist-sdk/server/src/auth/twilio.service.d.ts +27 -0
- package/dist-sdk/server/src/auth/twilio.service.js +30 -4
- package/dist-sdk/server/src/auth/users.service.d.ts +30 -0
- package/dist-sdk/server/src/auth/users.service.js +42 -0
- package/dist-sdk/server/src/common/errors.d.ts +20 -0
- package/dist-sdk/server/src/common/errors.js +21 -0
- package/dist-sdk/server/src/config/env-config.d.ts +7 -0
- package/dist-sdk/server/src/config/prompt-resolver.d.ts +180 -0
- package/dist-sdk/server/src/config/prompt-resolver.js +182 -0
- package/dist-sdk/server/src/config/user-config.service.d.ts +59 -0
- package/dist-sdk/server/src/config/user-config.service.js +57 -0
- package/dist-sdk/server/src/config/user-paths.service.d.ts +61 -0
- package/dist-sdk/server/src/config/user-paths.service.js +67 -0
- package/dist-sdk/server/src/documents/pdf-conversion.d.ts +121 -1
- package/dist-sdk/server/src/documents/pdf-conversion.js +94 -0
- package/dist-sdk/server/src/packs/pack-instantiation.service.d.ts +98 -0
- package/dist-sdk/server/src/packs/pack-instantiation.service.js +99 -0
- package/dist-sdk/server/src/packs/pack-manifest.d.ts +100 -0
- package/dist-sdk/server/src/packs/pack-manifest.js +58 -0
- package/dist-sdk/server/src/packs/pack.service.d.ts +99 -1
- package/dist-sdk/server/src/packs/pack.service.js +98 -1
- package/dist-sdk/server/src/room/artifact-store.service.d.ts +27 -0
- package/dist-sdk/server/src/room/artifact-store.service.js +49 -3
- package/dist-sdk/server/src/room/artifact.d.ts +18 -0
- package/dist-sdk/server/src/room/message.d.ts +64 -0
- package/dist-sdk/server/src/room/message.js +77 -2
- package/dist-sdk/server/src/room/nickname.d.ts +27 -0
- package/dist-sdk/server/src/room/nickname.js +41 -0
- package/dist-sdk/server/src/room/published-rooms.service.js +2 -0
- package/dist-sdk/server/src/room/room-config.d.ts +26 -0
- package/dist-sdk/server/src/room/room-factory.d.ts +12 -0
- package/dist-sdk/server/src/room/room-factory.js +14 -0
- package/dist-sdk/server/src/room/room-message.service.d.ts +28 -0
- package/dist-sdk/server/src/room/room-message.service.js +28 -0
- package/dist-sdk/server/src/room/room-store.service.d.ts +177 -0
- package/dist-sdk/server/src/room/room-store.service.js +242 -5
- package/dist-sdk/server/src/room/room.d.ts +906 -1
- package/dist-sdk/server/src/room/room.js +1525 -34
- package/dist-sdk/server/src/rpg/rpg-models.d.ts +50 -0
- package/dist-sdk/server/src/rpg/rpg-models.js +53 -0
- package/dist-sdk/server/src/rpg/rpg-roster.d.ts +38 -3
- package/dist-sdk/server/src/rpg/rpg-roster.js +49 -49
- package/dist-sdk/server/src/rpg/rpg.service.d.ts +87 -0
- package/dist-sdk/server/src/rpg/rpg.service.js +108 -5
- package/dist-sdk/server/src/sdk/briyah-config.d.ts +57 -0
- package/dist-sdk/server/src/sdk/briyah-config.js +17 -0
- package/dist-sdk/server/src/sdk/briyah.d.ts +228 -0
- package/dist-sdk/server/src/sdk/briyah.js +285 -7
- package/dist-sdk/server/src/sdk/index.d.ts +0 -1
- package/dist-sdk/server/src/sdk/index.js +24 -3
- package/dist-sdk/server/src/shared/shared.module.d.ts +8 -0
- package/dist-sdk/server/src/shared/shared.module.js +13 -5
- package/dist-sdk/server/src/story/story-message.service.d.ts +43 -0
- package/dist-sdk/server/src/story/story-message.service.js +43 -0
- package/dist-sdk/server/src/story/story-progress.service.d.ts +23 -0
- package/dist-sdk/server/src/story/story-progress.service.js +23 -0
- package/dist-sdk/server/src/story/story-store.service.d.ts +92 -0
- package/dist-sdk/server/src/story/story-store.service.js +136 -1
- package/dist-sdk/server/src/story/story.service.d.ts +398 -0
- package/dist-sdk/server/src/story/story.service.js +829 -25
- package/dist-sdk/server/src/tools/core/index.d.ts +25 -0
- package/dist-sdk/server/src/tools/core/index.js +31 -0
- package/dist-sdk/server/src/tools/core/rename.tool.d.ts +10 -0
- package/dist-sdk/server/src/tools/core/rename.tool.js +72 -0
- package/dist-sdk/server/src/tools/native-tool-registry.service.d.ts +22 -0
- package/dist-sdk/server/src/tools/native-tool-registry.service.js +22 -0
- package/dist-sdk/server/src/tools/native-tool.types.d.ts +186 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/backgrounds.d.ts +29 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/backgrounds.js +29 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/cantrips.d.ts +46 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/cantrips.js +40 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/feats.d.ts +33 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/feats.js +19 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/gear.d.ts +37 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/gear.generated.d.ts +17 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/gear.generated.js +17 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/gear.js +27 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/kits.d.ts +36 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/kits.js +32 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/magic-items.d.ts +32 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/magic-items.generated.d.ts +13 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/magic-items.generated.js +13 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/magic-items.js +18 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/monsters.d.ts +54 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/monsters.generated.d.ts +13 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/monsters.generated.js +13 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/monsters.js +20 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/races.d.ts +36 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/races.js +18 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/spells.d.ts +29 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/spells.generated.d.ts +13 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/spells.generated.js +13 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/spells.js +21 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/srd.d.ts +95 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/srd.js +66 -2
- package/dist-sdk/server/src/tools/packs/dnd5e/data/weapons.d.ts +50 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/data/weapons.js +28 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/bestiary.d.ts +92 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/bestiary.js +152 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/campaign-items.d.ts +28 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/campaign-items.js +29 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/cantrips.d.ts +31 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/cantrips.js +36 -2
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/character.d.ts +261 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/character.js +401 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/coerce.d.ts +11 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/coerce.js +11 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/dice.d.ts +65 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/dice.js +67 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/encounter.d.ts +73 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/encounter.js +63 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/encumbrance.d.ts +66 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/encumbrance.js +59 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/feats.d.ts +54 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/feats.js +49 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/foes.d.ts +42 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/foes.js +59 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/guides.d.ts +72 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/guides.js +63 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/items.d.ts +57 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/items.js +54 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/kits.d.ts +81 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/kits.js +85 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/races.d.ts +60 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/races.js +64 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/rules.d.ts +169 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/rules.js +203 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/sheet.d.ts +192 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/sheet.js +144 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/spells.d.ts +56 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/spells.js +54 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/weapons.d.ts +65 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/weapons.js +73 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/index.d.ts +18 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/index.js +18 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/tools/check.tool.d.ts +4 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/tools/check.tool.js +116 -1
- package/dist-sdk/server/src/tools/packs/dnd5e/tools/class.tool.d.ts +14 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/tools/class.tool.js +17 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/tools/dice.tools.d.ts +1 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/tools/dice.tools.js +35 -1
- package/dist-sdk/server/src/tools/packs/dnd5e/tools/encounter.tool.d.ts +6 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/tools/encounter.tool.js +25 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/tools/item.tool.d.ts +7 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/tools/item.tool.js +13 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/tools/monster.tool.d.ts +8 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/tools/monster.tool.js +55 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/tools/shared.d.ts +164 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/tools/shared.js +188 -1
- package/dist-sdk/server/src/tools/packs/dnd5e/tools/sheet.tools.d.ts +1 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/tools/sheet.tools.js +125 -1
- package/dist-sdk/server/src/tools/packs/dnd5e/tools/spell.tool.d.ts +13 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/tools/spell.tool.js +18 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/tools/state.tools.d.ts +1 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/tools/state.tools.js +23 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/tools/world.tools.d.ts +1 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/tools/world.tools.js +35 -0
- package/dist-sdk/server/src/tools/packs/index.d.ts +52 -2
- package/dist-sdk/server/src/tools/packs/index.js +83 -9
- package/dist-sdk/server/src/tools/shared/coerce.d.ts +95 -0
- package/dist-sdk/server/src/tools/shared/coerce.js +98 -0
- package/dist-sdk/server/src/tools/tool-execution.service.d.ts +73 -0
- package/dist-sdk/server/src/tools/tool-execution.service.js +74 -0
- package/dist-sdk/server/src/tools/tool-store.service.d.ts +11 -0
- package/dist-sdk/server/src/tools/tool-store.service.js +13 -1
- package/dist-sdk/shared/types/app.types.d.ts +285 -14
- package/dist-sdk/shared/types/app.types.js +3 -0
- package/package.json +167 -139
- package/data/common/config/random_words.txt +0 -2241
- package/data/common/story_ideas/biblical.json +0 -122
- package/data/common/story_ideas/fantasy.json +0 -82
- package/data/common/story_ideas/historical_fiction.json +0 -82
- package/data/common/story_ideas/science_fiction.json +0 -82
- package/data/packs/acts/agents/character-guide.json +0 -6
- package/data/packs/acts/agents/discipleship-mentor.json +0 -6
- package/data/packs/acts/agents/mentor-coach.json +0 -6
- package/data/packs/acts/agents/student.json +0 -13
- package/data/packs/acts/agents/table-moderator.json +0 -6
- package/data/packs/acts/artifacts/chapter-01-acts-1.md +0 -405
- package/data/packs/acts/artifacts/chapter-02-acts-2.md +0 -443
- package/data/packs/acts/artifacts/chapter-03-acts-3.md +0 -409
- package/data/packs/acts/artifacts/chapter-04-acts-4.md +0 -438
- package/data/packs/acts/artifacts/chapter-05-acts-5.md +0 -434
- package/data/packs/acts/artifacts/chapter-06-acts-6.md +0 -441
- package/data/packs/acts/artifacts/chapter-07-acts-7.md +0 -472
- package/data/packs/acts/artifacts/chapter-08-acts-8.md +0 -512
- package/data/packs/acts/artifacts/chapter-09-acts-9.md +0 -346
- package/data/packs/acts/artifacts/chapter-10-acts-10.md +0 -367
- package/data/packs/acts/artifacts/chapter-11-acts-11.md +0 -391
- package/data/packs/acts/artifacts/chapter-12-acts-12.md +0 -390
- package/data/packs/acts/artifacts/chapter-13-acts-13.md +0 -388
- package/data/packs/acts/artifacts/chapter-14-acts-14.md +0 -466
- package/data/packs/acts/artifacts/chapter-15-acts-15.md +0 -413
- package/data/packs/acts/artifacts/chapter-16-acts-16.md +0 -585
- package/data/packs/acts/artifacts/chapter-17-acts-17.md +0 -428
- package/data/packs/acts/artifacts/chapter-18-acts-18.md +0 -486
- package/data/packs/acts/artifacts/chapter-19-acts-19.md +0 -461
- package/data/packs/acts/artifacts/chapter-20-acts-20.md +0 -389
- package/data/packs/acts/artifacts/chapter-21-acts-21.md +0 -452
- package/data/packs/acts/artifacts/chapter-22-acts-22.md +0 -424
- package/data/packs/acts/artifacts/chapter-23-acts-23.md +0 -395
- package/data/packs/acts/artifacts/chapter-24-acts-24.md +0 -446
- package/data/packs/acts/artifacts/chapter-25-acts-25.md +0 -391
- package/data/packs/acts/artifacts/chapter-26-acts-26.md +0 -431
- package/data/packs/acts/artifacts/chapter-27-acts-27.md +0 -530
- package/data/packs/acts/artifacts/chapter-28-acts-28.md +0 -1871
- package/data/packs/acts/artifacts/chapters.manifest.json +0 -170
- package/data/packs/acts/artifacts/character_guide-creation.md +0 -112
- package/data/packs/acts/artifacts/discipleship_mentor-procedure.md +0 -154
- package/data/packs/acts/artifacts/discipleship_mentor-systems.md +0 -72
- package/data/packs/acts/artifacts/manifest.json +0 -197
- package/data/packs/acts/artifacts/stage-guide.md +0 -94
- package/data/packs/acts/pack.json +0 -6
- package/data/packs/acts/prompts/acts_player/defaults_config.json +0 -5
- package/data/packs/acts/prompts/acts_player/perceive.json +0 -29
- package/data/packs/acts/prompts/acts_player/perceive.prompt +0 -87
- package/data/packs/acts/prompts/acts_player/system_instruction.prompt +0 -0
- package/data/packs/acts/prompts/acts_room_moderator/defaults_config.json +0 -9
- package/data/packs/acts/prompts/acts_room_moderator/moderate.json +0 -34
- package/data/packs/acts/prompts/acts_room_moderator/moderate.prompt +0 -70
- package/data/packs/acts/prompts/acts_room_moderator/system_instruction.prompt +0 -0
- package/data/packs/acts/prompts/character_guide/defaults_config.json +0 -18
- package/data/packs/acts/prompts/character_guide/perceive.json +0 -29
- package/data/packs/acts/prompts/character_guide/perceive.prompt +0 -140
- package/data/packs/acts/prompts/character_guide/system_instruction.prompt +0 -0
- package/data/packs/acts/prompts/discipleship_mentor/defaults_config.json +0 -23
- package/data/packs/acts/prompts/discipleship_mentor/perceive.json +0 -33
- package/data/packs/acts/prompts/discipleship_mentor/perceive.prompt +0 -245
- package/data/packs/acts/prompts/discipleship_mentor/system_instruction.prompt +0 -0
- package/data/packs/acts/prompts/mentor_coach/defaults_config.json +0 -13
- package/data/packs/acts/prompts/mentor_coach/perceive.json +0 -29
- package/data/packs/acts/prompts/mentor_coach/perceive.prompt +0 -87
- package/data/packs/acts/prompts/mentor_coach/system_instruction.prompt +0 -0
- package/data/packs/acts/rooms/session.json +0 -14
- package/data/packs/acts/rooms/training.json +0 -58
- package/dist-sdk/server/src/tools/packs/acts/data/cards.d.ts +0 -53
- package/dist-sdk/server/src/tools/packs/acts/data/cards.js +0 -54
- package/dist-sdk/server/src/tools/packs/acts/data/character-cards.generated.d.ts +0 -2
- package/dist-sdk/server/src/tools/packs/acts/data/character-cards.generated.js +0 -185
- package/dist-sdk/server/src/tools/packs/acts/data/encounters.d.ts +0 -12
- package/dist-sdk/server/src/tools/packs/acts/data/encounters.js +0 -102
- package/dist-sdk/server/src/tools/packs/acts/data/event-cards.generated.d.ts +0 -2
- package/dist-sdk/server/src/tools/packs/acts/data/event-cards.generated.js +0 -196
- package/dist-sdk/server/src/tools/packs/acts/data/item-cards.generated.d.ts +0 -2
- package/dist-sdk/server/src/tools/packs/acts/data/item-cards.generated.js +0 -1311
- package/dist-sdk/server/src/tools/packs/acts/engine/cards.d.ts +0 -5
- package/dist-sdk/server/src/tools/packs/acts/engine/cards.js +0 -27
- package/dist-sdk/server/src/tools/packs/acts/engine/challenge.d.ts +0 -37
- package/dist-sdk/server/src/tools/packs/acts/engine/challenge.js +0 -120
- package/dist-sdk/server/src/tools/packs/acts/engine/dice.d.ts +0 -20
- package/dist-sdk/server/src/tools/packs/acts/engine/dice.js +0 -41
- package/dist-sdk/server/src/tools/packs/acts/engine/sheet.d.ts +0 -54
- package/dist-sdk/server/src/tools/packs/acts/engine/sheet.js +0 -200
- package/dist-sdk/server/src/tools/packs/acts/index.d.ts +0 -2
- package/dist-sdk/server/src/tools/packs/acts/index.js +0 -24
- package/dist-sdk/server/src/tools/packs/acts/tools/award.tool.d.ts +0 -2
- package/dist-sdk/server/src/tools/packs/acts/tools/award.tool.js +0 -184
- package/dist-sdk/server/src/tools/packs/acts/tools/challenge.tool.d.ts +0 -2
- package/dist-sdk/server/src/tools/packs/acts/tools/challenge.tool.js +0 -107
- package/dist-sdk/server/src/tools/packs/acts/tools/chapter.tool.d.ts +0 -3
- package/dist-sdk/server/src/tools/packs/acts/tools/chapter.tool.js +0 -146
- package/dist-sdk/server/src/tools/packs/acts/tools/check.tool.d.ts +0 -4
- package/dist-sdk/server/src/tools/packs/acts/tools/check.tool.js +0 -241
- package/dist-sdk/server/src/tools/packs/acts/tools/encounter.tool.d.ts +0 -3
- package/dist-sdk/server/src/tools/packs/acts/tools/encounter.tool.js +0 -72
- package/dist-sdk/server/src/tools/packs/acts/tools/faith.tool.d.ts +0 -2
- package/dist-sdk/server/src/tools/packs/acts/tools/faith.tool.js +0 -75
- package/dist-sdk/server/src/tools/packs/acts/tools/shared.d.ts +0 -20
- package/dist-sdk/server/src/tools/packs/acts/tools/shared.js +0 -70
- package/dist-sdk/server/src/tools/packs/acts/tools/sheet.tools.d.ts +0 -4
- package/dist-sdk/server/src/tools/packs/acts/tools/sheet.tools.js +0 -215
- package/docs/.nojekyll +0 -1
- package/docs/assets/hierarchy.js +0 -1
- package/docs/assets/highlight.css +0 -113
- package/docs/assets/icons.js +0 -18
- package/docs/assets/icons.svg +0 -1
- package/docs/assets/main.js +0 -60
- package/docs/assets/navigation.js +0 -1
- package/docs/assets/search.js +0 -1
- package/docs/assets/style.css +0 -1648
- package/docs/classes/Agent.html +0 -801
- package/docs/classes/Briyah.html +0 -296
- package/docs/classes/BriyahConfigService.html +0 -119
- package/docs/classes/NativeToolRegistry.html +0 -166
- package/docs/classes/PackInstantiationService.html +0 -157
- package/docs/classes/PackService.html +0 -247
- package/docs/classes/Room.html +0 -1338
- package/docs/classes/RoomMessage.html +0 -290
- package/docs/enums/MessageAction.html +0 -136
- package/docs/functions/actsTools.html +0 -44
- package/docs/functions/availableToolPacks.html +0 -41
- package/docs/functions/buildMarkdown.html +0 -54
- package/docs/functions/dnd5eTools.html +0 -44
- package/docs/functions/installToolPacks.html +0 -48
- package/docs/functions/pageMarker.html +0 -45
- package/docs/functions/registerToolPacks.html +0 -54
- package/docs/functions/splitMarkdownIntoPages.html +0 -51
- package/docs/functions/stripWrappingFence.html +0 -52
- package/docs/hierarchy.html +0 -46
- package/docs/index.html +0 -595
- package/docs/interfaces/AgentCreationOptions.html +0 -125
- package/docs/interfaces/AgentInfo.html +0 -218
- package/docs/interfaces/AgentMessagesResponse.html +0 -74
- package/docs/interfaces/AppService.html +0 -3337
- package/docs/interfaces/Artifact.html +0 -95
- package/docs/interfaces/ArtifactMetadata.html +0 -112
- package/docs/interfaces/ArtifactWriteOptions.html +0 -90
- package/docs/interfaces/AttachDocumentResponse.html +0 -62
- package/docs/interfaces/BriyahConfigOptions.html +0 -124
- package/docs/interfaces/ChapterInfo.html +0 -62
- package/docs/interfaces/Character.html +0 -86
- package/docs/interfaces/ConversionAgent.html +0 -78
- package/docs/interfaces/CreateAgentResponse.html +0 -56
- package/docs/interfaces/CreateRoomResponse.html +0 -62
- package/docs/interfaces/CreateStoryResponse.html +0 -56
- package/docs/interfaces/FileList.html +0 -56
- package/docs/interfaces/FileMetadata.html +0 -89
- package/docs/interfaces/IConfigService.html +0 -79
- package/docs/interfaces/InstantiateModels.html +0 -64
- package/docs/interfaces/InstantiateRoomResult.html +0 -70
- package/docs/interfaces/LoadedPack.html +0 -131
- package/docs/interfaces/LoggingOptions.html +0 -92
- package/docs/interfaces/Message.html +0 -84
- package/docs/interfaces/ModelChoice.html +0 -73
- package/docs/interfaces/ModelInfo.html +0 -74
- package/docs/interfaces/NativeToolDefinition.html +0 -107
- package/docs/interfaces/PackAgentTemplate.html +0 -101
- package/docs/interfaces/PackRoomTemplate.html +0 -108
- package/docs/interfaces/PackView.html +0 -172
- package/docs/interfaces/PdfConversionOptions.html +0 -102
- package/docs/interfaces/PdfConversionResult.html +0 -97
- package/docs/interfaces/PdfMarkdownOptions.html +0 -198
- package/docs/interfaces/PdfMarkdownResult.html +0 -106
- package/docs/interfaces/PdfPageMarkdown.html +0 -94
- package/docs/interfaces/PreparedPromptResponse.html +0 -62
- package/docs/interfaces/ProcessTextResponse.html +0 -86
- package/docs/interfaces/PromptFile.html +0 -70
- package/docs/interfaces/PromptFileContent.html +0 -76
- package/docs/interfaces/PromptFilesResponse.html +0 -56
- package/docs/interfaces/PromptFolder.html +0 -80
- package/docs/interfaces/PromptFolderDefaults.html +0 -155
- package/docs/interfaces/PromptFolderDefaultsResponse.html +0 -79
- package/docs/interfaces/PromptFoldersResponse.html +0 -56
- package/docs/interfaces/RoomDetails.html +0 -151
- package/docs/interfaces/RoomErrorUpdate.html +0 -82
- package/docs/interfaces/RoomInfo.html +0 -88
- package/docs/interfaces/RoomMessageUpdate.html +0 -113
- package/docs/interfaces/RoomMessagesResponse.html +0 -62
- package/docs/interfaces/SendMessageResult.html +0 -62
- package/docs/interfaces/StoryErrorEvent.html +0 -84
- package/docs/interfaces/StoryIdea.html +0 -74
- package/docs/interfaces/StoryInfo.html +0 -198
- package/docs/interfaces/StoryIntroduceCharacterEvent.html +0 -79
- package/docs/interfaces/StoryProgressChapterEvent.html +0 -72
- package/docs/interfaces/StoryState.html +0 -108
- package/docs/interfaces/StoryStateEvent.html +0 -76
- package/docs/interfaces/ToolArtifact.html +0 -85
- package/docs/interfaces/ToolCaller.html +0 -77
- package/docs/interfaces/ToolDefinition.html +0 -80
- package/docs/interfaces/ToolHelpers.html +0 -105
- package/docs/interfaces/ToolPackDocuments.html +0 -100
- package/docs/interfaces/ToolParameter.html +0 -74
- package/docs/interfaces/ToolRoomAccess.html +0 -227
- package/docs/interfaces/ToolSummary.html +0 -74
- package/docs/interfaces/ToolsResponse.html +0 -68
- package/docs/interfaces/Transaction.html +0 -104
- package/docs/interfaces/TransactionHistoryResponse.html +0 -62
- package/docs/modules.html +0 -53
- package/docs/types/PackOrigin.html +0 -34
- package/docs/types/PromptScope.html +0 -34
- package/docs/types/RoomTurnMode.html +0 -42
- package/docs/types/RoomUpdate.html +0 -36
- package/docs/types/RosterOverrides.html +0 -42
- package/docs/types/ToolContext.html +0 -40
- package/docs/types/ToolRunResult.html +0 -34
- package/src/tools/packs/dnd5e/README.md +0 -867
|
@@ -25,6 +25,7 @@ let FalAiService = class FalAiService extends base_ai_service_1.BaseAiService {
|
|
|
25
25
|
this._isAvailable = false;
|
|
26
26
|
return;
|
|
27
27
|
}
|
|
28
|
+
// Configure fal client
|
|
28
29
|
client_1.fal.config({ credentials: apiKey });
|
|
29
30
|
logger_1.logger.debug('FalAI service initialized successfully');
|
|
30
31
|
}
|
|
@@ -59,6 +60,7 @@ let FalAiService = class FalAiService extends base_ai_service_1.BaseAiService {
|
|
|
59
60
|
}
|
|
60
61
|
const data = await response.json();
|
|
61
62
|
const models = [];
|
|
63
|
+
// Transform API response to ModelInfo format
|
|
62
64
|
if (data.models && Array.isArray(data.models)) {
|
|
63
65
|
for (const model of data.models) {
|
|
64
66
|
models.push({
|
|
@@ -77,6 +79,13 @@ let FalAiService = class FalAiService extends base_ai_service_1.BaseAiService {
|
|
|
77
79
|
return [];
|
|
78
80
|
}
|
|
79
81
|
}
|
|
82
|
+
/**
|
|
83
|
+
* Adds a message to the agent history in the appropriate format for this service
|
|
84
|
+
* @param agent The agent to add the message to
|
|
85
|
+
* @param message The formatted message to add
|
|
86
|
+
* @param fromSelf Whether the message was generated by this agent (true) or received from outside (false)
|
|
87
|
+
* @param developer Whether the message is a system instruction from the developer
|
|
88
|
+
*/
|
|
80
89
|
addToConversationHistory(agent, message, fromSelf = false, developer = false) {
|
|
81
90
|
if (!agent.history) {
|
|
82
91
|
agent.history = [];
|
|
@@ -92,20 +101,27 @@ let FalAiService = class FalAiService extends base_ai_service_1.BaseAiService {
|
|
|
92
101
|
});
|
|
93
102
|
agent.trimHistoryIfNeeded();
|
|
94
103
|
}
|
|
95
|
-
async textPrompt(agent, prompt, jsonSchema = null, saveResponse = true, promptInstructions = null,
|
|
104
|
+
async textPrompt(agent, prompt, jsonSchema = null, saveResponse = true, promptInstructions = null,
|
|
105
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
106
|
+
_cacheConfig = null, maxOutputChars = 0) {
|
|
107
|
+
// CHECK BALANCE BEFORE AI CALL (unless disabled)
|
|
96
108
|
if (agent.balanceService && !agent.disableBalanceCheck) {
|
|
97
109
|
const hasSufficientBalance = agent.balanceService.hasSufficientBalance();
|
|
98
110
|
if (!hasSufficientBalance) {
|
|
99
111
|
throw new errors_1.InsufficientBalanceError('Insufficient balance. Please add funds to continue using AI services.');
|
|
100
112
|
}
|
|
101
113
|
}
|
|
114
|
+
// Build messages array locally without modifying agent.history yet
|
|
115
|
+
// Prepend identity and system instruction at the beginning
|
|
102
116
|
const identity = `Your agent identity is ${agent.agentName}: ${agent.description}`;
|
|
103
117
|
const instructions = promptInstructions || agent.systemInstruction;
|
|
118
|
+
// Build full prompt for fal.ai any-llm
|
|
104
119
|
let fullPrompt = '';
|
|
105
120
|
if (instructions) {
|
|
106
121
|
fullPrompt += instructions + '\n\n';
|
|
107
122
|
}
|
|
108
123
|
fullPrompt += identity + '\n\n';
|
|
124
|
+
// Add attached files as text (fal.ai doesn't support native file uploads)
|
|
109
125
|
if (agent.attachedFiles && agent.attachedFiles.length > 0) {
|
|
110
126
|
for (const file of agent.attachedFiles) {
|
|
111
127
|
if (file.serviceMetadata?.fileData) {
|
|
@@ -115,12 +131,15 @@ let FalAiService = class FalAiService extends base_ai_service_1.BaseAiService {
|
|
|
115
131
|
}
|
|
116
132
|
}
|
|
117
133
|
}
|
|
134
|
+
// Add conversation history
|
|
118
135
|
for (const msg of agent.history) {
|
|
119
136
|
fullPrompt += `${msg.role}: ${msg.content}\n\n`;
|
|
120
137
|
}
|
|
138
|
+
// Add JSON schema instruction if provided
|
|
121
139
|
if (jsonSchema) {
|
|
122
140
|
fullPrompt += `You must respond with valid JSON matching this schema: ${JSON.stringify(jsonSchema)}\n\n`;
|
|
123
141
|
}
|
|
142
|
+
// Add user prompt
|
|
124
143
|
if (prompt) {
|
|
125
144
|
fullPrompt += `user: ${prompt}\n\n`;
|
|
126
145
|
}
|
|
@@ -129,6 +148,7 @@ let FalAiService = class FalAiService extends base_ai_service_1.BaseAiService {
|
|
|
129
148
|
let estimatedInputTokens = 0;
|
|
130
149
|
let estimatedOutputTokens = 0;
|
|
131
150
|
try {
|
|
151
|
+
// Estimate input tokens (rough estimate: 4 characters per token)
|
|
132
152
|
estimatedInputTokens = Math.ceil(fullPrompt.length / 4);
|
|
133
153
|
const result = (await client_1.fal.subscribe('fal-ai/any-llm', {
|
|
134
154
|
input: {
|
|
@@ -136,6 +156,7 @@ let FalAiService = class FalAiService extends base_ai_service_1.BaseAiService {
|
|
|
136
156
|
prompt: fullPrompt,
|
|
137
157
|
},
|
|
138
158
|
}));
|
|
159
|
+
// Extract response
|
|
139
160
|
if (result.output) {
|
|
140
161
|
responseText = result.output;
|
|
141
162
|
}
|
|
@@ -148,6 +169,7 @@ let FalAiService = class FalAiService extends base_ai_service_1.BaseAiService {
|
|
|
148
169
|
else {
|
|
149
170
|
responseText = '';
|
|
150
171
|
}
|
|
172
|
+
// Estimate output tokens
|
|
151
173
|
estimatedOutputTokens = Math.ceil(responseText.length / 4);
|
|
152
174
|
if (jsonSchema) {
|
|
153
175
|
responseText = this.trimToJson(responseText);
|
|
@@ -155,6 +177,7 @@ let FalAiService = class FalAiService extends base_ai_service_1.BaseAiService {
|
|
|
155
177
|
else {
|
|
156
178
|
responseText = this.trimResponseText(responseText);
|
|
157
179
|
}
|
|
180
|
+
// Track token usage (using estimates since fal.ai may not return usage)
|
|
158
181
|
const usage = result.usage || {
|
|
159
182
|
prompt_tokens: estimatedInputTokens,
|
|
160
183
|
completion_tokens: estimatedOutputTokens,
|
|
@@ -172,6 +195,7 @@ let FalAiService = class FalAiService extends base_ai_service_1.BaseAiService {
|
|
|
172
195
|
}
|
|
173
196
|
throw error;
|
|
174
197
|
}
|
|
198
|
+
// Only modify agent.history if saveResponse=true
|
|
175
199
|
if (saveResponse) {
|
|
176
200
|
this.addToConversationHistory(agent, prompt, false);
|
|
177
201
|
this.addToConversationHistory(agent, responseText, true);
|
|
@@ -188,9 +212,18 @@ let FalAiService = class FalAiService extends base_ai_service_1.BaseAiService {
|
|
|
188
212
|
}
|
|
189
213
|
return responseText;
|
|
190
214
|
}
|
|
215
|
+
/**
|
|
216
|
+
* Compute the cost of a text message with markup for Fal AI
|
|
217
|
+
* Handles model name lookup with fal_ai/ prefix if needed
|
|
218
|
+
* @param agent The agent making the request
|
|
219
|
+
* @param usage Usage object from the API response (or estimated)
|
|
220
|
+
* @param estimatedInputTokens Fallback estimate if usage not provided
|
|
221
|
+
* @param estimatedOutputTokens Fallback estimate if usage not provided
|
|
222
|
+
*/
|
|
191
223
|
computeMessageCost(agent, usage, estimatedInputTokens, estimatedOutputTokens) {
|
|
192
224
|
const inputTokens = usage?.prompt_tokens || estimatedInputTokens || 0;
|
|
193
225
|
const outputTokens = usage?.completion_tokens || estimatedOutputTokens || 0;
|
|
226
|
+
// Try to find model with current name, then try with fal_ai/ prefix
|
|
194
227
|
let modelInfo = (0, model_prices_1.getModelPrices)()[agent.modelName];
|
|
195
228
|
if (!modelInfo && !agent.modelName.startsWith('fal_ai/')) {
|
|
196
229
|
modelInfo = (0, model_prices_1.getModelPrices)()[`fal_ai/${agent.modelName}`];
|
|
@@ -199,10 +232,13 @@ let FalAiService = class FalAiService extends base_ai_service_1.BaseAiService {
|
|
|
199
232
|
logger_1.logger.warn(`No price info found for model ${agent.modelName}. Skipping cost tracking.`);
|
|
200
233
|
return;
|
|
201
234
|
}
|
|
235
|
+
// Simple calculation
|
|
202
236
|
const inputCost = inputTokens * (modelInfo.input_cost_per_token || 0);
|
|
203
237
|
const outputCost = outputTokens * (modelInfo.output_cost_per_token || 0);
|
|
238
|
+
// Calculate markup: 2x output cost (unless disabled)
|
|
204
239
|
const markup = agent.disableMarkup ? 0 : outputCost * agent.markupRate;
|
|
205
240
|
const cost = inputCost + outputCost + markup;
|
|
241
|
+
// Update agent totals
|
|
206
242
|
agent.totalInputTokens += inputTokens;
|
|
207
243
|
agent.totalOutputTokens += outputTokens;
|
|
208
244
|
agent.totalCost += cost;
|
|
@@ -213,10 +249,21 @@ let FalAiService = class FalAiService extends base_ai_service_1.BaseAiService {
|
|
|
213
249
|
else {
|
|
214
250
|
logger_1.logger.log(`Cost for ${agent.agentName} message: ${cost.toFixed(4)} (markup: ${markup.toFixed(4)})`);
|
|
215
251
|
}
|
|
252
|
+
// Decrement balance with markup
|
|
216
253
|
if (agent.balanceService && cost > 0) {
|
|
217
254
|
agent.balanceService.decrementBalance(cost, markup);
|
|
218
255
|
}
|
|
219
256
|
}
|
|
257
|
+
/**
|
|
258
|
+
* Generates an image from a text prompt with optional reference images
|
|
259
|
+
* @param agent The agent making the request
|
|
260
|
+
* @param prompt Text description of the image to generate
|
|
261
|
+
* @param width
|
|
262
|
+
* @param height
|
|
263
|
+
* @param quality
|
|
264
|
+
* @param referenceImageArtifactIds Optional array of artifact IDs containing reference images
|
|
265
|
+
* @returns Promise resolving to JSON string with { artifactId }
|
|
266
|
+
*/
|
|
220
267
|
computeImageCost(agent, centsPerImage) {
|
|
221
268
|
let modelInfo = (0, model_prices_1.getModelPrices)()[agent.modelName];
|
|
222
269
|
if (!modelInfo && !agent.modelName.startsWith('fal_ai/')) {
|
|
@@ -268,15 +315,19 @@ let FalAiService = class FalAiService extends base_ai_service_1.BaseAiService {
|
|
|
268
315
|
input: input,
|
|
269
316
|
}));
|
|
270
317
|
this.computeImageCost(agent, centsPerImage);
|
|
318
|
+
// 7. Extract image from result
|
|
271
319
|
let base64Data;
|
|
272
320
|
if (result.data.images && result.data.images[0]) {
|
|
273
321
|
const image = result.data.images[0];
|
|
322
|
+
// Check if it's a URL or base64
|
|
274
323
|
if (image.url) {
|
|
324
|
+
// Download image and convert to base64
|
|
275
325
|
const imageResponse = await fetch(image.url);
|
|
276
326
|
const imageBuffer = Buffer.from(await imageResponse.arrayBuffer());
|
|
277
327
|
base64Data = imageBuffer.toString('base64');
|
|
278
328
|
}
|
|
279
329
|
else if (image.content) {
|
|
330
|
+
// Already base64
|
|
280
331
|
base64Data = image.content;
|
|
281
332
|
}
|
|
282
333
|
else {
|
|
@@ -284,6 +335,7 @@ let FalAiService = class FalAiService extends base_ai_service_1.BaseAiService {
|
|
|
284
335
|
}
|
|
285
336
|
}
|
|
286
337
|
else if (result.data.image) {
|
|
338
|
+
// Some models return single image object
|
|
287
339
|
if (result.data.image.url) {
|
|
288
340
|
const imageResponse = await fetch(result.data.image.url);
|
|
289
341
|
const imageBuffer = Buffer.from(await imageResponse.arrayBuffer());
|
|
@@ -299,7 +351,9 @@ let FalAiService = class FalAiService extends base_ai_service_1.BaseAiService {
|
|
|
299
351
|
else {
|
|
300
352
|
throw new Error('Unexpected response format from Fal AI');
|
|
301
353
|
}
|
|
354
|
+
// 8. Save generated image as artifact
|
|
302
355
|
const artifactId = await this.saveImageAsArtifact(agent, base64Data, prompt);
|
|
356
|
+
// 9. Return artifact ID
|
|
303
357
|
return { artifactId };
|
|
304
358
|
}
|
|
305
359
|
catch (error) {
|
|
@@ -8,13 +8,32 @@ export declare class GoogleAiService extends BaseAiService {
|
|
|
8
8
|
supportsImageGeneration(): boolean;
|
|
9
9
|
supportsPromptCaching(): boolean;
|
|
10
10
|
fetchModelsFromApi(): Promise<ModelInfo[]>;
|
|
11
|
+
/**
|
|
12
|
+
* Adds a message to the agent history in Google AI format
|
|
13
|
+
* @param agent The agent to add the message to
|
|
14
|
+
* @param message The formatted message to add
|
|
15
|
+
* @param fromSelf Whether the message was generated by this agent (true) or received from outside (false)
|
|
16
|
+
*/
|
|
11
17
|
addToConversationHistory(agent: Agent, message: string, fromSelf?: boolean): void;
|
|
12
18
|
textPrompt(agent: Agent, prompt: string, jsonSchema?: any, saveResponse?: boolean, promptInstructions?: string, _cacheConfig?: any, maxOutputChars?: number): Promise<any>;
|
|
13
19
|
generateImage(agent: Agent, prompt: string, imageProperties?: Record<string, any>): Promise<{
|
|
14
20
|
artifactId?: string;
|
|
15
21
|
error?: any;
|
|
16
22
|
}>;
|
|
23
|
+
/**
|
|
24
|
+
* Compute the cost of a message with markup
|
|
25
|
+
* @param agent The agent making the request
|
|
26
|
+
* @param usage Usage metadata object
|
|
27
|
+
*/
|
|
17
28
|
private computeMessageCost;
|
|
29
|
+
/**
|
|
30
|
+
* Compute the cost of image generation with markup (hardcoded to 0 for images)
|
|
31
|
+
* @param agent The agent making the request
|
|
32
|
+
* @param modelName The model name used for image generation
|
|
33
|
+
*/
|
|
18
34
|
private computeImageCost;
|
|
35
|
+
/**
|
|
36
|
+
* Override the formatMessage method to handle Google AI specific message format
|
|
37
|
+
*/
|
|
19
38
|
formatMessage(message: any): string;
|
|
20
39
|
}
|
|
@@ -44,6 +44,7 @@ let GoogleAiService = class GoogleAiService extends base_ai_service_1.BaseAiServ
|
|
|
44
44
|
return false;
|
|
45
45
|
}
|
|
46
46
|
async fetchModelsFromApi() {
|
|
47
|
+
// Try to fetch models from API, fall back to curated list
|
|
47
48
|
try {
|
|
48
49
|
const response = await this.googleAI.models.list();
|
|
49
50
|
return response.page.map((model) => {
|
|
@@ -60,6 +61,12 @@ let GoogleAiService = class GoogleAiService extends base_ai_service_1.BaseAiServ
|
|
|
60
61
|
logger_1.logger.warn('Could not fetch models from Google AI API, using fallback list:', error.message);
|
|
61
62
|
}
|
|
62
63
|
}
|
|
64
|
+
/**
|
|
65
|
+
* Adds a message to the agent history in Google AI format
|
|
66
|
+
* @param agent The agent to add the message to
|
|
67
|
+
* @param message The formatted message to add
|
|
68
|
+
* @param fromSelf Whether the message was generated by this agent (true) or received from outside (false)
|
|
69
|
+
*/
|
|
63
70
|
addToConversationHistory(agent, message, fromSelf = false) {
|
|
64
71
|
if (!agent.history) {
|
|
65
72
|
agent.history = [];
|
|
@@ -72,15 +79,20 @@ let GoogleAiService = class GoogleAiService extends base_ai_service_1.BaseAiServ
|
|
|
72
79
|
});
|
|
73
80
|
agent.trimHistoryIfNeeded();
|
|
74
81
|
}
|
|
75
|
-
async textPrompt(agent, prompt, jsonSchema = null, saveResponse = true, promptInstructions = null,
|
|
82
|
+
async textPrompt(agent, prompt, jsonSchema = null, saveResponse = true, promptInstructions = null,
|
|
83
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
84
|
+
_cacheConfig = null, maxOutputChars = 0) {
|
|
85
|
+
// CHECK BALANCE BEFORE AI CALL (unless disabled)
|
|
76
86
|
if (agent.balanceService && !agent.disableBalanceCheck) {
|
|
77
87
|
const hasSufficientBalance = agent.balanceService.hasSufficientBalance();
|
|
78
88
|
if (!hasSufficientBalance) {
|
|
79
89
|
throw new errors_1.InsufficientBalanceError('Insufficient balance. Please add funds to continue using AI services.');
|
|
80
90
|
}
|
|
81
91
|
}
|
|
92
|
+
// Build messages array
|
|
82
93
|
const identity = `Your agent identity is ${agent.agentName}: ${agent.description}`;
|
|
83
94
|
const messages = [{ role: 'user', parts: [{ text: identity }] }];
|
|
95
|
+
// Add attached files as text (Google AI supports native file uploads, but we'll use text for simplicity)
|
|
84
96
|
if (agent.attachedFiles && agent.attachedFiles.length > 0) {
|
|
85
97
|
for (const file of agent.attachedFiles) {
|
|
86
98
|
if (file.serviceMetadata?.fileData) {
|
|
@@ -93,19 +105,25 @@ let GoogleAiService = class GoogleAiService extends base_ai_service_1.BaseAiServ
|
|
|
93
105
|
}
|
|
94
106
|
}
|
|
95
107
|
}
|
|
108
|
+
// Add conversation history
|
|
96
109
|
messages.push(...agent.history);
|
|
110
|
+
// Add user prompt
|
|
97
111
|
if (prompt) {
|
|
98
112
|
messages.push({
|
|
99
113
|
role: 'user',
|
|
100
114
|
parts: [{ text: prompt }],
|
|
101
115
|
});
|
|
102
116
|
}
|
|
117
|
+
// Configure generation
|
|
103
118
|
const generationConfig = {
|
|
104
119
|
maxOutputTokens: 8192,
|
|
105
120
|
temperature: 0,
|
|
106
121
|
};
|
|
122
|
+
// Add JSON schema if provided
|
|
107
123
|
if (jsonSchema) {
|
|
108
124
|
generationConfig.responseMimeType = 'application/json';
|
|
125
|
+
// Remove properties that aren't part of the schema spec
|
|
126
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
109
127
|
const { name: _name, description: _description, ...schemaOnly } = jsonSchema;
|
|
110
128
|
generationConfig.responseSchema = schemaOnly;
|
|
111
129
|
}
|
|
@@ -122,6 +140,7 @@ let GoogleAiService = class GoogleAiService extends base_ai_service_1.BaseAiServ
|
|
|
122
140
|
let responseText;
|
|
123
141
|
try {
|
|
124
142
|
const response = await this.googleAI.models.generateContent(requestParams);
|
|
143
|
+
// Extract response text
|
|
125
144
|
if (response.text) {
|
|
126
145
|
responseText = response.text;
|
|
127
146
|
}
|
|
@@ -131,6 +150,7 @@ let GoogleAiService = class GoogleAiService extends base_ai_service_1.BaseAiServ
|
|
|
131
150
|
else {
|
|
132
151
|
responseText = '';
|
|
133
152
|
}
|
|
153
|
+
// Trim response
|
|
134
154
|
if (jsonSchema) {
|
|
135
155
|
responseText = this.trimToJson(responseText);
|
|
136
156
|
}
|
|
@@ -138,6 +158,7 @@ let GoogleAiService = class GoogleAiService extends base_ai_service_1.BaseAiServ
|
|
|
138
158
|
responseText = this.trimResponseText(responseText);
|
|
139
159
|
}
|
|
140
160
|
this.debugLogResponse(agent.agentName, responseText);
|
|
161
|
+
// Track token usage
|
|
141
162
|
if (response.usageMetadata) {
|
|
142
163
|
this.computeMessageCost(agent, response.usageMetadata);
|
|
143
164
|
}
|
|
@@ -146,6 +167,7 @@ let GoogleAiService = class GoogleAiService extends base_ai_service_1.BaseAiServ
|
|
|
146
167
|
logger_1.logger.error('Error calling Google AI:', error);
|
|
147
168
|
throw error;
|
|
148
169
|
}
|
|
170
|
+
// Only modify agent.history if saveResponse=true
|
|
149
171
|
if (saveResponse) {
|
|
150
172
|
this.addToConversationHistory(agent, prompt, false);
|
|
151
173
|
this.addToConversationHistory(agent, responseText, true);
|
|
@@ -192,6 +214,11 @@ let GoogleAiService = class GoogleAiService extends base_ai_service_1.BaseAiServ
|
|
|
192
214
|
return { error };
|
|
193
215
|
}
|
|
194
216
|
}
|
|
217
|
+
/**
|
|
218
|
+
* Compute the cost of a message with markup
|
|
219
|
+
* @param agent The agent making the request
|
|
220
|
+
* @param usage Usage metadata object
|
|
221
|
+
*/
|
|
195
222
|
computeMessageCost(agent, usage) {
|
|
196
223
|
const inputTokens = usage.promptTokenCount || 0;
|
|
197
224
|
const outputTokens = usage.candidatesTokenCount || 0;
|
|
@@ -202,17 +229,25 @@ let GoogleAiService = class GoogleAiService extends base_ai_service_1.BaseAiServ
|
|
|
202
229
|
}
|
|
203
230
|
const inputCost = inputTokens * modelInfo.input_cost_per_token;
|
|
204
231
|
const outputCost = outputTokens * modelInfo.output_cost_per_token;
|
|
232
|
+
// Calculate markup: 2x output cost (unless disabled)
|
|
205
233
|
const markup = agent.disableMarkup ? 0 : outputCost * agent.markupRate;
|
|
206
234
|
const cost = inputCost + outputCost + markup;
|
|
235
|
+
// Update agent totals
|
|
207
236
|
agent.totalInputTokens += inputTokens;
|
|
208
237
|
agent.totalOutputTokens += outputTokens;
|
|
209
238
|
agent.totalCost += cost;
|
|
210
239
|
agent.totalMarkup += markup;
|
|
211
240
|
logger_1.logger.log(`Cost for ${agent.agentName} message: ${cost} (markup: ${markup.toFixed(4)})`);
|
|
241
|
+
// Decrement balance with markup
|
|
212
242
|
if (agent.balanceService && cost > 0) {
|
|
213
243
|
agent.balanceService.decrementBalance(cost, markup);
|
|
214
244
|
}
|
|
215
245
|
}
|
|
246
|
+
/**
|
|
247
|
+
* Compute the cost of image generation with markup (hardcoded to 0 for images)
|
|
248
|
+
* @param agent The agent making the request
|
|
249
|
+
* @param modelName The model name used for image generation
|
|
250
|
+
*/
|
|
216
251
|
computeImageCost(agent, modelName, centsPerImage) {
|
|
217
252
|
const modelInfo = (0, model_prices_1.getModelPrices)()[modelName];
|
|
218
253
|
if (!modelInfo || !modelInfo.output_cost_per_image) {
|
|
@@ -228,15 +263,21 @@ let GoogleAiService = class GoogleAiService extends base_ai_service_1.BaseAiServ
|
|
|
228
263
|
return;
|
|
229
264
|
}
|
|
230
265
|
const outputCost = modelInfo.output_cost_per_image;
|
|
266
|
+
// Calculate markup: hardcoded to 0 for image generation
|
|
231
267
|
const markup = outputCost * 0;
|
|
232
268
|
const cost = outputCost + markup;
|
|
269
|
+
// Update agent totals
|
|
233
270
|
agent.totalCost += cost;
|
|
234
271
|
agent.totalMarkup += markup;
|
|
235
272
|
logger_1.logger.log(`Cost for image generation: ${cost} (markup: ${markup.toFixed(4)})`);
|
|
273
|
+
// Decrement balance with markup
|
|
236
274
|
if (agent.balanceService && cost > 0) {
|
|
237
275
|
agent.balanceService.decrementBalance(cost, markup);
|
|
238
276
|
}
|
|
239
277
|
}
|
|
278
|
+
/**
|
|
279
|
+
* Override the formatMessage method to handle Google AI specific message format
|
|
280
|
+
*/
|
|
240
281
|
formatMessage(message) {
|
|
241
282
|
if (!message) {
|
|
242
283
|
return 'No message';
|
|
@@ -7,9 +7,31 @@ export declare class GrokAiService extends BaseAiService {
|
|
|
7
7
|
getServiceName(): string;
|
|
8
8
|
getModelPricePrefix(): string;
|
|
9
9
|
fetchModelsFromApi(): Promise<ModelInfo[]>;
|
|
10
|
+
/**
|
|
11
|
+
* Uploads a document to Grok's Files API
|
|
12
|
+
* @param agent The agent to attach the document to
|
|
13
|
+
* @param fileName The name of the file
|
|
14
|
+
* @param fileData The file data as a Uint8Array
|
|
15
|
+
* @param fullContext
|
|
16
|
+
* @returns A success message with the file ID
|
|
17
|
+
*/
|
|
10
18
|
attachDocument(agent: Agent, fileName: string, fileData: Uint8Array): Promise<string>;
|
|
11
19
|
finishAgent(agent: Agent): Promise<void>;
|
|
12
20
|
textPrompt(agent: Agent, prompt: string, jsonSchema?: any, saveResponse?: boolean, promptInstructions?: string, _cacheConfig?: any, maxOutputChars?: number): Promise<any>;
|
|
21
|
+
/**
|
|
22
|
+
* Adds a message to the agent history in the appropriate format for this service
|
|
23
|
+
* @param agent The agent to add the message to
|
|
24
|
+
* @param message The formatted message to add
|
|
25
|
+
* @param fromSelf Whether the message was generated by this agent (true) or received from outside (false)
|
|
26
|
+
* @param developer Whether the message is a system instruction from the developer
|
|
27
|
+
*/
|
|
13
28
|
addToConversationHistory(agent: Agent, message: string, fromSelf?: boolean, developer?: boolean): void;
|
|
29
|
+
/**
|
|
30
|
+
* Compute the cost of a message with markup and cache-aware pricing for Grok
|
|
31
|
+
* Grok provides a 75% discount on cached tokens (no cache creation charge)
|
|
32
|
+
* Handles model name lookup with xai/ prefix if needed
|
|
33
|
+
* @param agent The agent making the request
|
|
34
|
+
* @param usage Usage object from the API response
|
|
35
|
+
*/
|
|
14
36
|
private computeMessageCost;
|
|
15
37
|
}
|
|
@@ -30,7 +30,7 @@ let GrokAiService = class GrokAiService extends base_ai_service_1.BaseAiService
|
|
|
30
30
|
const options = {
|
|
31
31
|
apiKey: apiKey,
|
|
32
32
|
baseURL: 'https://api.x.ai/v1',
|
|
33
|
-
timeout: 600000,
|
|
33
|
+
timeout: 600000, // 10 minutes for very long requests (e.g., story imports)
|
|
34
34
|
maxRetries: 3,
|
|
35
35
|
};
|
|
36
36
|
this.openai = new openai_1.OpenAI(options);
|
|
@@ -59,27 +59,40 @@ let GrokAiService = class GrokAiService extends base_ai_service_1.BaseAiService
|
|
|
59
59
|
};
|
|
60
60
|
});
|
|
61
61
|
}
|
|
62
|
+
/**
|
|
63
|
+
* Uploads a document to Grok's Files API
|
|
64
|
+
* @param agent The agent to attach the document to
|
|
65
|
+
* @param fileName The name of the file
|
|
66
|
+
* @param fileData The file data as a Uint8Array
|
|
67
|
+
* @param fullContext
|
|
68
|
+
* @returns A success message with the file ID
|
|
69
|
+
*/
|
|
62
70
|
async attachDocument(agent, fileName, fileData) {
|
|
63
71
|
if (agent.fullContextFiles) {
|
|
64
72
|
return super.attachDocument(agent, fileName, fileData);
|
|
65
73
|
}
|
|
74
|
+
// Check if we already uploaded this file to Grok
|
|
66
75
|
const existingMetadata = agent.attachedFiles.find((f) => f.fileName === fileName && f.serviceMetadata?.serviceName === this.getServiceName());
|
|
67
76
|
if (existingMetadata?.serviceMetadata?.resourceId) {
|
|
77
|
+
// File already uploaded to Grok
|
|
68
78
|
const fileId = existingMetadata.serviceMetadata.resourceId;
|
|
69
79
|
return `File found with Grok file id: ${fileId}`;
|
|
70
80
|
}
|
|
81
|
+
// Upload file to Grok using OpenAI SDK
|
|
71
82
|
let fileInput = fileData;
|
|
72
83
|
let fileToUpload = await (0, openai_2.toFile)(fileInput, fileName);
|
|
73
84
|
const fileObject = await this.openai.files.create({
|
|
74
85
|
file: fileToUpload,
|
|
75
86
|
purpose: 'assistants',
|
|
76
87
|
});
|
|
88
|
+
// Update file metadata with Grok-specific information
|
|
77
89
|
const additionalMetadata = {
|
|
78
90
|
serviceMetadata: {
|
|
79
91
|
serviceName: this.getServiceName(),
|
|
80
92
|
resourceId: fileObject.id,
|
|
81
93
|
},
|
|
82
94
|
};
|
|
95
|
+
// Find and update existing metadata
|
|
83
96
|
const fileMetadataIndex = agent.attachedFiles.findIndex((f) => f.fileName === fileName);
|
|
84
97
|
if (fileMetadataIndex >= 0) {
|
|
85
98
|
agent.attachedFiles[fileMetadataIndex] = {
|
|
@@ -92,6 +105,7 @@ let GrokAiService = class GrokAiService extends base_ai_service_1.BaseAiService
|
|
|
92
105
|
async finishAgent(agent) {
|
|
93
106
|
if (!agent)
|
|
94
107
|
return;
|
|
108
|
+
// Delete all Grok files using metadata
|
|
95
109
|
for (const metadata of agent.attachedFiles) {
|
|
96
110
|
if (metadata.serviceMetadata?.serviceName === this.getServiceName() &&
|
|
97
111
|
metadata.serviceMetadata?.resourceId) {
|
|
@@ -105,13 +119,18 @@ let GrokAiService = class GrokAiService extends base_ai_service_1.BaseAiService
|
|
|
105
119
|
}
|
|
106
120
|
}
|
|
107
121
|
}
|
|
108
|
-
async textPrompt(agent, prompt, jsonSchema = null, saveResponse = true, promptInstructions = null,
|
|
122
|
+
async textPrompt(agent, prompt, jsonSchema = null, saveResponse = true, promptInstructions = null,
|
|
123
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
124
|
+
_cacheConfig = null, maxOutputChars = 0) {
|
|
125
|
+
// CHECK BALANCE BEFORE AI CALL (unless disabled)
|
|
109
126
|
if (agent.balanceService && !agent.disableBalanceCheck) {
|
|
110
127
|
const hasSufficientBalance = agent.balanceService.hasSufficientBalance();
|
|
111
128
|
if (!hasSufficientBalance) {
|
|
112
129
|
throw new errors_1.InsufficientBalanceError('Insufficient balance. Please add funds to continue using AI services.');
|
|
113
130
|
}
|
|
114
131
|
}
|
|
132
|
+
// Build messages array locally without modifying agent.history yet
|
|
133
|
+
// Prepend identity and system instruction at the beginning
|
|
115
134
|
const identity = `Your agent identity is ${agent.agentName}: ${agent.description}`;
|
|
116
135
|
const instructions = promptInstructions || agent.systemInstruction;
|
|
117
136
|
const messages = [];
|
|
@@ -124,6 +143,7 @@ let GrokAiService = class GrokAiService extends base_ai_service_1.BaseAiService
|
|
|
124
143
|
role: 'system',
|
|
125
144
|
content: [{ type: 'text', text: identity }],
|
|
126
145
|
});
|
|
146
|
+
// Add system messages for each attached file (tells Grok what files are available)
|
|
127
147
|
if (agent.attachedFiles && agent.attachedFiles.length > 0) {
|
|
128
148
|
for (const file of agent.attachedFiles) {
|
|
129
149
|
if (file.serviceMetadata?.serviceName === this.getServiceName() &&
|
|
@@ -149,17 +169,21 @@ let GrokAiService = class GrokAiService extends base_ai_service_1.BaseAiService
|
|
|
149
169
|
role: 'user',
|
|
150
170
|
content: [
|
|
151
171
|
{ type: 'text', text: textContent },
|
|
172
|
+
// {type: 'file', file: {filename: file.fileName, file_data: base64Data }}
|
|
152
173
|
],
|
|
153
174
|
});
|
|
154
175
|
}
|
|
155
176
|
}
|
|
156
177
|
}
|
|
178
|
+
// Handle attached artifacts
|
|
157
179
|
if (agent.attachedArtifacts && agent.attachedArtifacts.length > 0) {
|
|
158
180
|
for (const artifactId of agent.attachedArtifacts) {
|
|
159
181
|
const content = agent.artifactService.getArtifact(artifactId);
|
|
182
|
+
// Only include text artifacts (skip binary/image artifacts)
|
|
160
183
|
if (content && typeof content === 'string') {
|
|
161
184
|
const metadata = agent.artifactService.getArtifactMetadata(artifactId);
|
|
162
185
|
const fileName = metadata ? `${metadata.name}.md` : 'artifact.md';
|
|
186
|
+
// Add as text message
|
|
163
187
|
messages.push({
|
|
164
188
|
role: 'user',
|
|
165
189
|
content: [
|
|
@@ -172,7 +196,9 @@ let GrokAiService = class GrokAiService extends base_ai_service_1.BaseAiService
|
|
|
172
196
|
}
|
|
173
197
|
}
|
|
174
198
|
}
|
|
199
|
+
// Add conversation history
|
|
175
200
|
messages.push(...agent.history);
|
|
201
|
+
// Add user prompt to local array
|
|
176
202
|
if (prompt) {
|
|
177
203
|
messages.push({
|
|
178
204
|
role: 'user',
|
|
@@ -196,6 +222,7 @@ let GrokAiService = class GrokAiService extends base_ai_service_1.BaseAiService
|
|
|
196
222
|
responseText = this.trimToJson(responseText);
|
|
197
223
|
}
|
|
198
224
|
this.debugLogResponse(agent.agentName, responseText);
|
|
225
|
+
// Track token usage if available, including cache-specific metrics
|
|
199
226
|
if (response.usage) {
|
|
200
227
|
this.computeMessageCost(agent, response.usage);
|
|
201
228
|
}
|
|
@@ -204,15 +231,27 @@ let GrokAiService = class GrokAiService extends base_ai_service_1.BaseAiService
|
|
|
204
231
|
logger_1.logger.error(error);
|
|
205
232
|
throw error;
|
|
206
233
|
}
|
|
234
|
+
// Only modify agent.history if saveResponse=true
|
|
235
|
+
// Note: We don't save the temporary system instructions to history
|
|
207
236
|
if (saveResponse) {
|
|
208
237
|
this.addToConversationHistory(agent, prompt, false);
|
|
209
238
|
this.addToConversationHistory(agent, responseText, true);
|
|
210
239
|
}
|
|
240
|
+
// If saveResponse=false, don't modify history at all
|
|
211
241
|
if (jsonSchema)
|
|
212
242
|
return this.parseStructuredResponse(responseText);
|
|
213
243
|
return responseText;
|
|
214
244
|
}
|
|
245
|
+
/**
|
|
246
|
+
* Adds a message to the agent history in the appropriate format for this service
|
|
247
|
+
* @param agent The agent to add the message to
|
|
248
|
+
* @param message The formatted message to add
|
|
249
|
+
* @param fromSelf Whether the message was generated by this agent (true) or received from outside (false)
|
|
250
|
+
* @param developer Whether the message is a system instruction from the developer
|
|
251
|
+
*/
|
|
215
252
|
addToConversationHistory(agent, message, fromSelf = false, developer = false) {
|
|
253
|
+
// Default implementation for most services
|
|
254
|
+
// Subclasses should override this if they have a different history format
|
|
216
255
|
if (!agent.history) {
|
|
217
256
|
agent.history = [];
|
|
218
257
|
}
|
|
@@ -227,11 +266,19 @@ let GrokAiService = class GrokAiService extends base_ai_service_1.BaseAiService
|
|
|
227
266
|
});
|
|
228
267
|
agent.trimHistoryIfNeeded();
|
|
229
268
|
}
|
|
269
|
+
/**
|
|
270
|
+
* Compute the cost of a message with markup and cache-aware pricing for Grok
|
|
271
|
+
* Grok provides a 75% discount on cached tokens (no cache creation charge)
|
|
272
|
+
* Handles model name lookup with xai/ prefix if needed
|
|
273
|
+
* @param agent The agent making the request
|
|
274
|
+
* @param usage Usage object from the API response
|
|
275
|
+
*/
|
|
230
276
|
computeMessageCost(agent, usage) {
|
|
231
277
|
const totalPromptTokens = usage.prompt_tokens || 0;
|
|
232
278
|
const cachedTokens = usage.prompt_tokens_details?.cached_tokens || 0;
|
|
233
279
|
const uncachedPromptTokens = totalPromptTokens - cachedTokens;
|
|
234
280
|
const completionTokens = usage.completion_tokens || 0;
|
|
281
|
+
// Try to find model with current name, then try with xai/ prefix
|
|
235
282
|
let modelInfo = (0, model_prices_1.getModelPrices)()[agent.modelName];
|
|
236
283
|
if (!modelInfo && !agent.modelName.startsWith('xai/')) {
|
|
237
284
|
modelInfo = (0, model_prices_1.getModelPrices)()[`xai/${agent.modelName}`];
|
|
@@ -240,20 +287,25 @@ let GrokAiService = class GrokAiService extends base_ai_service_1.BaseAiService
|
|
|
240
287
|
logger_1.logger.error(`No price info found for model ${agent.modelName} or xai/${agent.modelName}`);
|
|
241
288
|
return;
|
|
242
289
|
}
|
|
290
|
+
// Calculate cache-aware costs (Grok: 75% discount on cached)
|
|
243
291
|
const uncachedCost = uncachedPromptTokens * modelInfo.input_cost_per_token;
|
|
244
292
|
const cachedCost = cachedTokens *
|
|
245
293
|
(modelInfo.cache_read_input_token_cost || modelInfo.input_cost_per_token * 0.25);
|
|
246
294
|
const outputCost = completionTokens * modelInfo.output_cost_per_token;
|
|
295
|
+
// Calculate markup: 2x output cost only (unless disabled)
|
|
247
296
|
const markup = agent.disableMarkup ? 0 : outputCost * agent.markupRate;
|
|
248
297
|
const cost = uncachedCost + cachedCost + outputCost + markup;
|
|
298
|
+
// Update agent totals
|
|
249
299
|
agent.totalInputTokens += totalPromptTokens;
|
|
250
300
|
agent.totalOutputTokens += completionTokens;
|
|
251
301
|
agent.totalCost += cost;
|
|
252
302
|
agent.totalMarkup += markup;
|
|
253
303
|
logger_1.logger.log(`Cost for ${agent.agentName} message: ${cost} (markup: ${markup.toFixed(4)})`);
|
|
304
|
+
// Log cache metrics for monitoring
|
|
254
305
|
if (cachedTokens > 0) {
|
|
255
306
|
logger_1.logger.log(`Cache metrics for ${agent.agentName}: cached=${cachedTokens}, uncached=${uncachedPromptTokens}`);
|
|
256
307
|
}
|
|
308
|
+
// Decrement balance with markup
|
|
257
309
|
if (agent.balanceService && cost > 0) {
|
|
258
310
|
agent.balanceService.decrementBalance(cost, markup);
|
|
259
311
|
}
|