briyah 2.0.3 → 2.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +55 -8
- 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/config/story_models.json +3 -3
- package/data/common/prompts/illustrator/describe_scene.prompt +4 -4
- package/data/common/prompts/narrator/create_story.prompt +2 -0
- package/data/common/prompts/perceive.prompt +1 -1
- package/data/packs/dnd5e/prompts/battle_master/perceive.prompt +1 -1
- package/data/packs/dnd5e/prompts/campaign_designer/perceive.prompt +1 -1
- package/data/packs/dnd5e/prompts/character_creator/perceive.prompt +795 -787
- package/data/packs/dnd5e/prompts/dungeon_master/defaults_config.json +2 -1
- package/data/packs/dnd5e/prompts/dungeon_master/perceive.prompt +1 -1
- package/data/packs/dnd5e/rooms/campaign.json +18 -15
- package/data/packs/dnd5e/rooms/table.json +17 -16
- package/dist-sdk/server/src/ai/LLM/anthropic.service.d.ts +103 -0
- package/dist-sdk/server/src/ai/LLM/anthropic.service.js +174 -3
- package/dist-sdk/server/src/ai/LLM/base-ai.service.d.ts +194 -3
- package/dist-sdk/server/src/ai/LLM/base-ai.service.js +299 -19
- package/dist-sdk/server/src/ai/LLM/blank-image.d.ts +36 -0
- package/dist-sdk/server/src/ai/LLM/blank-image.js +80 -0
- 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 +103 -4
- package/dist-sdk/server/src/ai/LLM/fal.service.d.ts +25 -0
- package/dist-sdk/server/src/ai/LLM/fal.service.js +56 -2
- package/dist-sdk/server/src/ai/LLM/googleai.service.d.ts +19 -0
- package/dist-sdk/server/src/ai/LLM/googleai.service.js +43 -2
- package/dist-sdk/server/src/ai/LLM/grok.service.d.ts +22 -0
- package/dist-sdk/server/src/ai/LLM/grok.service.js +55 -3
- 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 +81 -3
- 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 +45 -3
- package/dist-sdk/server/src/ai/LLM/together.service.d.ts +30 -0
- package/dist-sdk/server/src/ai/LLM/together.service.js +61 -3
- package/dist-sdk/server/src/ai/agent-config.d.ts +15 -1
- 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 -7
- package/dist-sdk/server/src/ai/agent.d.ts +197 -2
- package/dist-sdk/server/src/ai/agent.js +225 -14
- 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 +46 -3
- 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 +120 -0
- package/dist-sdk/server/src/app.controller.js +365 -14
- package/dist-sdk/server/src/app.module.js +28 -6
- package/dist-sdk/server/src/app.service.d.ts +1710 -4
- package/dist-sdk/server/src/app.service.js +1938 -20
- 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 +44 -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 +104 -0
- package/dist-sdk/server/src/packs/pack-manifest.d.ts +114 -0
- package/dist-sdk/server/src/packs/pack-manifest.js +69 -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/illustration/illustration.service.d.ts +211 -0
- package/dist-sdk/server/src/room/illustration/illustration.service.js +771 -0
- package/dist-sdk/server/src/room/illustration/illustration.types.d.ts +101 -0
- package/dist-sdk/server/src/room/illustration/illustration.types.js +2 -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 +26 -0
- package/dist-sdk/server/src/room/nickname.js +26 -0
- package/dist-sdk/server/src/room/published-rooms.service.js +2 -0
- package/dist-sdk/server/src/room/room-config.d.ts +84 -1
- package/dist-sdk/server/src/room/room-config.js +19 -0
- package/dist-sdk/server/src/room/room-factory.d.ts +20 -0
- package/dist-sdk/server/src/room/room-factory.js +25 -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 +184 -0
- package/dist-sdk/server/src/room/room-store.service.js +280 -6
- package/dist-sdk/server/src/room/room.d.ts +981 -1
- package/dist-sdk/server/src/room/room.js +1531 -27
- 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 +37 -0
- package/dist-sdk/server/src/rpg/rpg-roster.js +37 -0
- package/dist-sdk/server/src/rpg/rpg.service.d.ts +87 -0
- package/dist-sdk/server/src/rpg/rpg.service.js +108 -1
- 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.js +23 -0
- 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 +366 -9
- package/dist-sdk/server/src/story/story.service.js +825 -374
- package/dist-sdk/server/src/tools/core/illustrate.tool.d.ts +14 -0
- package/dist-sdk/server/src/tools/core/illustrate.tool.js +80 -0
- package/dist-sdk/server/src/tools/core/index.d.ts +22 -0
- package/dist-sdk/server/src/tools/core/index.js +24 -1
- package/dist-sdk/server/src/tools/core/rename.tool.d.ts +8 -0
- package/dist-sdk/server/src/tools/core/rename.tool.js +11 -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 +266 -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 +201 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/sheet.js +149 -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 +194 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/tools/shared.js +263 -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 +127 -2
- 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 +49 -0
- package/dist-sdk/server/src/tools/packs/index.js +66 -0
- 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 +107 -3
- package/dist-sdk/server/src/tools/tool-execution.service.js +163 -19
- 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 +328 -13
- package/dist-sdk/shared/types/app.types.js +18 -1
- package/package.json +167 -168
- 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/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 -1434
- package/docs/classes/RoomMessage.html +0 -290
- package/docs/enums/MessageAction.html +0 -136
- 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 -51
- 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 -3340
- 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 -260
- 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
|
@@ -1,4 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Per-user Briyah settings.
|
|
4
|
+
*
|
|
5
|
+
* Today that is only which packs the user has enabled, but it exists as its own
|
|
6
|
+
* file rather than a field somewhere for two reasons.
|
|
7
|
+
*
|
|
8
|
+
* **It is not `user-preferences.json`.** That file is the host's: its contract
|
|
9
|
+
* says preferences are application-defined and returned as-is, and
|
|
10
|
+
* `saveUserPreferences` writes the whole object. A host storing a theme there
|
|
11
|
+
* would wipe pack enablement on the next save.
|
|
12
|
+
*
|
|
13
|
+
* **It is not on `UserPathsService`.** That resolves paths, and it is faked with
|
|
14
|
+
* bare object literals across the specs — every method added to it has to be
|
|
15
|
+
* added to those fakes too.
|
|
16
|
+
*/
|
|
2
17
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
18
|
if (k2 === undefined) k2 = k;
|
|
4
19
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
@@ -42,10 +57,23 @@ class UserConfigService {
|
|
|
42
57
|
userPaths;
|
|
43
58
|
cached = null;
|
|
44
59
|
cachedAtMs = 0;
|
|
60
|
+
/**
|
|
61
|
+
* How long a read is trusted before the file is consulted again.
|
|
62
|
+
*
|
|
63
|
+
* `UserServiceManager` keeps an `AppService` for 30 minutes, so caching for the
|
|
64
|
+
* life of the instance would mean a second tab, another process or the SDK
|
|
65
|
+
* toggling a pack and this one never noticing. A second is long enough to
|
|
66
|
+
* collapse the burst of reads a single page load produces and short enough that
|
|
67
|
+
* nobody sees a stale list.
|
|
68
|
+
*/
|
|
45
69
|
static CACHE_MS = 1_000;
|
|
46
70
|
constructor(userPaths) {
|
|
47
71
|
this.userPaths = userPaths;
|
|
48
72
|
}
|
|
73
|
+
/**
|
|
74
|
+
* Reads this user's settings.
|
|
75
|
+
* @returns The settings, or an empty object when there is no file or it is unreadable
|
|
76
|
+
*/
|
|
49
77
|
get() {
|
|
50
78
|
const now = Date.now();
|
|
51
79
|
if (this.cached && now - this.cachedAtMs < UserConfigService.CACHE_MS) {
|
|
@@ -55,7 +83,15 @@ class UserConfigService {
|
|
|
55
83
|
this.cachedAtMs = now;
|
|
56
84
|
return this.cached;
|
|
57
85
|
}
|
|
86
|
+
/**
|
|
87
|
+
* Merges a patch into this user's settings.
|
|
88
|
+
* @param patch The fields to change
|
|
89
|
+
* @returns The settings as they now stand
|
|
90
|
+
*/
|
|
58
91
|
update(patch) {
|
|
92
|
+
// Read through the file rather than the cache: another process may have
|
|
93
|
+
// written since, and losing its change to a stale merge is worse than the
|
|
94
|
+
// extra read.
|
|
59
95
|
const merged = { ...this.read(), ...patch };
|
|
60
96
|
fs.mkdirSync(this.userPaths.getUserConfigDir(), { recursive: true });
|
|
61
97
|
fs.writeFileSync(this.configPath(), JSON.stringify(merged, null, 2), 'utf8');
|
|
@@ -63,9 +99,23 @@ class UserConfigService {
|
|
|
63
99
|
this.cachedAtMs = Date.now();
|
|
64
100
|
return merged;
|
|
65
101
|
}
|
|
102
|
+
/**
|
|
103
|
+
* The packs this user has enabled.
|
|
104
|
+
*
|
|
105
|
+
* Names only — whether a pack of that name is actually installed is the pack
|
|
106
|
+
* registry's business, so a pack that was enabled and then removed from disk
|
|
107
|
+
* stays in the list and comes back if it is reinstalled.
|
|
108
|
+
*
|
|
109
|
+
* @returns The enabled pack names
|
|
110
|
+
*/
|
|
66
111
|
enabledPacks() {
|
|
67
112
|
return this.get().enabledPacks ?? [];
|
|
68
113
|
}
|
|
114
|
+
/**
|
|
115
|
+
* Turns one pack on or off, leaving the rest alone.
|
|
116
|
+
* @param packName The pack to change
|
|
117
|
+
* @param enabled Whether it should be on
|
|
118
|
+
*/
|
|
69
119
|
setPackEnabled(packName, enabled) {
|
|
70
120
|
const current = new Set(this.enabledPacks());
|
|
71
121
|
if (enabled) {
|
|
@@ -79,6 +129,13 @@ class UserConfigService {
|
|
|
79
129
|
configPath() {
|
|
80
130
|
return path.join(this.userPaths.getUserConfigDir(), CONFIG_FILE);
|
|
81
131
|
}
|
|
132
|
+
/**
|
|
133
|
+
* Reads the file, treating anything unreadable as absent.
|
|
134
|
+
*
|
|
135
|
+
* Throwing here would take down every page that lists a prompt or an artifact,
|
|
136
|
+
* because enablement is consulted on all of them. A corrupt file costs the user
|
|
137
|
+
* their pack selection, which they can set again.
|
|
138
|
+
*/
|
|
82
139
|
read() {
|
|
83
140
|
const file = this.configPath();
|
|
84
141
|
if (!fs.existsSync(file)) {
|
|
@@ -14,15 +14,76 @@ export declare class UserPathsService {
|
|
|
14
14
|
getUploadDir(): string;
|
|
15
15
|
getUserConfigDir(): string;
|
|
16
16
|
getCommonConfigDir(): string;
|
|
17
|
+
/**
|
|
18
|
+
* Stores configuration data to a file
|
|
19
|
+
* @param configName The name of the configuration file
|
|
20
|
+
* @param data The data to store
|
|
21
|
+
*/
|
|
17
22
|
store(configName: string, data: string): void;
|
|
23
|
+
/**
|
|
24
|
+
* Loads configuration data from a file
|
|
25
|
+
* @param configName The name of the configuration file
|
|
26
|
+
* @returns The configuration data as a string, or null if the file doesn't exist
|
|
27
|
+
*/
|
|
18
28
|
load(configName: string): string | null;
|
|
29
|
+
/**
|
|
30
|
+
* Checks if a configuration file exists
|
|
31
|
+
* @param configName The name of the configuration file
|
|
32
|
+
* @returns True if the file exists, false otherwise
|
|
33
|
+
*/
|
|
19
34
|
exists(configName: string): boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Gets the common prompts directory (shared across all users)
|
|
37
|
+
* @returns The common prompts directory path
|
|
38
|
+
*/
|
|
20
39
|
getCommonPromptsDir(): string;
|
|
40
|
+
/**
|
|
41
|
+
* Gets the user-specific prompts directory
|
|
42
|
+
* @returns The user prompts directory path
|
|
43
|
+
*/
|
|
21
44
|
getUserPromptsDir(): string;
|
|
45
|
+
/**
|
|
46
|
+
* Gets the user data directory
|
|
47
|
+
* @returns The user data directory path
|
|
48
|
+
*/
|
|
22
49
|
getUserDataDir(): string;
|
|
50
|
+
/**
|
|
51
|
+
* Gets the directory holding installed packs, one subdirectory per pack.
|
|
52
|
+
*
|
|
53
|
+
* Unlike the directories above, the pack directories are **not** created by
|
|
54
|
+
* this constructor. The set of packs is dynamic — a host adds one by copying
|
|
55
|
+
* it in — so there is nothing to pre-create, and this constructor runs once
|
|
56
|
+
* per user-service creation.
|
|
57
|
+
*
|
|
58
|
+
* @returns The installed packs directory path
|
|
59
|
+
*/
|
|
23
60
|
getPacksDir(): string;
|
|
61
|
+
/**
|
|
62
|
+
* Gets one installed pack's directory.
|
|
63
|
+
* @param packName The pack name, already validated by the caller
|
|
64
|
+
* @returns The pack directory path, which may not exist
|
|
65
|
+
*/
|
|
24
66
|
getPackDir(packName: string): string;
|
|
67
|
+
/**
|
|
68
|
+
* Gets the user's own pack directory.
|
|
69
|
+
*
|
|
70
|
+
* Holds two things that need no distinguishing at lookup time: copy-on-write
|
|
71
|
+
* overlays of installed packs, and packs the user created themselves. Which
|
|
72
|
+
* one a subdirectory is depends only on whether {@link getPackDir} exists for
|
|
73
|
+
* the same name, and only the pack listing has to care.
|
|
74
|
+
*
|
|
75
|
+
* @returns The user packs directory path
|
|
76
|
+
*/
|
|
25
77
|
getUserPacksDir(): string;
|
|
78
|
+
/**
|
|
79
|
+
* Gets one of the user's pack directories.
|
|
80
|
+
* @param packName The pack name, already validated by the caller
|
|
81
|
+
* @returns The directory path, which may not exist
|
|
82
|
+
*/
|
|
26
83
|
getUserPackDir(packName: string): string;
|
|
84
|
+
/**
|
|
85
|
+
* Gets the current user ID
|
|
86
|
+
* @returns The user ID from authentication context or 'admin' as fallback
|
|
87
|
+
*/
|
|
27
88
|
getUserId(): string;
|
|
28
89
|
}
|
|
@@ -47,12 +47,16 @@ class UserPathsService {
|
|
|
47
47
|
userDataDir;
|
|
48
48
|
userId;
|
|
49
49
|
constructor(userId, baseDataPath) {
|
|
50
|
+
// Use the provided userId directly
|
|
50
51
|
this.userId = userId;
|
|
52
|
+
// Priority: constructor param > env var > default
|
|
51
53
|
const dataPath = baseDataPath || process.env.BRIYAH_DATA_PATH || path.resolve(process.cwd(), 'briyah-data');
|
|
54
|
+
// Create the base data directory if it doesn't exist
|
|
52
55
|
this.dataDir = path.resolve(dataPath);
|
|
53
56
|
if (!fs.existsSync(this.dataDir)) {
|
|
54
57
|
fs.mkdirSync(this.dataDir, { recursive: true });
|
|
55
58
|
}
|
|
59
|
+
// Common directories (shared across all users)
|
|
56
60
|
const commonDir = path.resolve(this.dataDir, 'common');
|
|
57
61
|
this.commonConfigDir = path.resolve(commonDir, 'config');
|
|
58
62
|
if (!fs.existsSync(this.commonConfigDir)) {
|
|
@@ -62,6 +66,7 @@ class UserPathsService {
|
|
|
62
66
|
if (!fs.existsSync(this.commonPromptsDir)) {
|
|
63
67
|
fs.mkdirSync(this.commonPromptsDir, { recursive: true });
|
|
64
68
|
}
|
|
69
|
+
// User-specific directories
|
|
65
70
|
this.userDataDir = path.resolve(this.dataDir, 'user', this.userId);
|
|
66
71
|
this.userPromptsDir = path.resolve(this.userDataDir, 'prompts');
|
|
67
72
|
if (!fs.existsSync(this.userPromptsDir)) {
|
|
@@ -75,6 +80,7 @@ class UserPathsService {
|
|
|
75
80
|
if (!fs.existsSync(this.userConfigDir)) {
|
|
76
81
|
fs.mkdirSync(this.userConfigDir, { recursive: true });
|
|
77
82
|
}
|
|
83
|
+
// For backward compatibility, promptsDir returns user prompts
|
|
78
84
|
this.promptsDir = this.userPromptsDir;
|
|
79
85
|
}
|
|
80
86
|
getDataDir() {
|
|
@@ -92,10 +98,20 @@ class UserPathsService {
|
|
|
92
98
|
getCommonConfigDir() {
|
|
93
99
|
return this.commonConfigDir;
|
|
94
100
|
}
|
|
101
|
+
/**
|
|
102
|
+
* Stores configuration data to a file
|
|
103
|
+
* @param configName The name of the configuration file
|
|
104
|
+
* @param data The data to store
|
|
105
|
+
*/
|
|
95
106
|
store(configName, data) {
|
|
96
107
|
const configPath = path.join(this.getUserConfigDir(), configName);
|
|
97
108
|
fs.writeFileSync(configPath, data);
|
|
98
109
|
}
|
|
110
|
+
/**
|
|
111
|
+
* Loads configuration data from a file
|
|
112
|
+
* @param configName The name of the configuration file
|
|
113
|
+
* @returns The configuration data as a string, or null if the file doesn't exist
|
|
114
|
+
*/
|
|
99
115
|
load(configName) {
|
|
100
116
|
const configPath = path.join(this.getUserConfigDir(), configName);
|
|
101
117
|
if (!fs.existsSync(configPath)) {
|
|
@@ -103,31 +119,82 @@ class UserPathsService {
|
|
|
103
119
|
}
|
|
104
120
|
return fs.readFileSync(configPath, 'utf8');
|
|
105
121
|
}
|
|
122
|
+
/**
|
|
123
|
+
* Checks if a configuration file exists
|
|
124
|
+
* @param configName The name of the configuration file
|
|
125
|
+
* @returns True if the file exists, false otherwise
|
|
126
|
+
*/
|
|
106
127
|
exists(configName) {
|
|
107
128
|
const configPath = path.join(this.getUserConfigDir(), configName);
|
|
108
129
|
return fs.existsSync(configPath);
|
|
109
130
|
}
|
|
131
|
+
/**
|
|
132
|
+
* Gets the common prompts directory (shared across all users)
|
|
133
|
+
* @returns The common prompts directory path
|
|
134
|
+
*/
|
|
110
135
|
getCommonPromptsDir() {
|
|
111
136
|
return this.commonPromptsDir;
|
|
112
137
|
}
|
|
138
|
+
/**
|
|
139
|
+
* Gets the user-specific prompts directory
|
|
140
|
+
* @returns The user prompts directory path
|
|
141
|
+
*/
|
|
113
142
|
getUserPromptsDir() {
|
|
114
143
|
return this.userPromptsDir;
|
|
115
144
|
}
|
|
145
|
+
/**
|
|
146
|
+
* Gets the user data directory
|
|
147
|
+
* @returns The user data directory path
|
|
148
|
+
*/
|
|
116
149
|
getUserDataDir() {
|
|
117
150
|
return this.userDataDir;
|
|
118
151
|
}
|
|
152
|
+
/**
|
|
153
|
+
* Gets the directory holding installed packs, one subdirectory per pack.
|
|
154
|
+
*
|
|
155
|
+
* Unlike the directories above, the pack directories are **not** created by
|
|
156
|
+
* this constructor. The set of packs is dynamic — a host adds one by copying
|
|
157
|
+
* it in — so there is nothing to pre-create, and this constructor runs once
|
|
158
|
+
* per user-service creation.
|
|
159
|
+
*
|
|
160
|
+
* @returns The installed packs directory path
|
|
161
|
+
*/
|
|
119
162
|
getPacksDir() {
|
|
120
163
|
return path.resolve(this.dataDir, 'packs');
|
|
121
164
|
}
|
|
165
|
+
/**
|
|
166
|
+
* Gets one installed pack's directory.
|
|
167
|
+
* @param packName The pack name, already validated by the caller
|
|
168
|
+
* @returns The pack directory path, which may not exist
|
|
169
|
+
*/
|
|
122
170
|
getPackDir(packName) {
|
|
123
171
|
return path.resolve(this.getPacksDir(), packName);
|
|
124
172
|
}
|
|
173
|
+
/**
|
|
174
|
+
* Gets the user's own pack directory.
|
|
175
|
+
*
|
|
176
|
+
* Holds two things that need no distinguishing at lookup time: copy-on-write
|
|
177
|
+
* overlays of installed packs, and packs the user created themselves. Which
|
|
178
|
+
* one a subdirectory is depends only on whether {@link getPackDir} exists for
|
|
179
|
+
* the same name, and only the pack listing has to care.
|
|
180
|
+
*
|
|
181
|
+
* @returns The user packs directory path
|
|
182
|
+
*/
|
|
125
183
|
getUserPacksDir() {
|
|
126
184
|
return path.resolve(this.userDataDir, 'packs');
|
|
127
185
|
}
|
|
186
|
+
/**
|
|
187
|
+
* Gets one of the user's pack directories.
|
|
188
|
+
* @param packName The pack name, already validated by the caller
|
|
189
|
+
* @returns The directory path, which may not exist
|
|
190
|
+
*/
|
|
128
191
|
getUserPackDir(packName) {
|
|
129
192
|
return path.resolve(this.getUserPacksDir(), packName);
|
|
130
193
|
}
|
|
194
|
+
/**
|
|
195
|
+
* Gets the current user ID
|
|
196
|
+
* @returns The user ID from authentication context or 'admin' as fallback
|
|
197
|
+
*/
|
|
131
198
|
getUserId() {
|
|
132
199
|
return this.userId;
|
|
133
200
|
}
|
|
@@ -1,43 +1,163 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turning a PDF into Markdown, one page at a time.
|
|
3
|
+
*
|
|
4
|
+
* The machinery only: splitting the document, bounding how many pages are in
|
|
5
|
+
* flight, retrying a page that fails, and assembling the result. Rendering a page
|
|
6
|
+
* is the caller's job — it supplies a {@link RenderPage} that owns the agent, the
|
|
7
|
+
* model and the prompt — so this module has no dependency on AppService and can be
|
|
8
|
+
* tested without an LLM.
|
|
9
|
+
*
|
|
10
|
+
* Three failure modes are handled here because each one was silently losing pages:
|
|
11
|
+
*
|
|
12
|
+
* - **An empty response.** The conversion occasionally returns nothing at all. Left
|
|
13
|
+
* alone the page reaches the reader as a gap with no indication anything was
|
|
14
|
+
* dropped, which is worse than an error.
|
|
15
|
+
* - **A thrown error.** A rate limit is the common one, and it is precisely what a
|
|
16
|
+
* large document provokes by converting every page at once. Retried with backoff.
|
|
17
|
+
* - **A page that never settles.** Raced against a timeout inside the retry, so a
|
|
18
|
+
* stalled page is retried rather than lost.
|
|
19
|
+
*
|
|
20
|
+
* A page that exhausts its attempts does not fail the document. It comes back with
|
|
21
|
+
* empty markdown and an `error`, the assembled Markdown carries a marker where the
|
|
22
|
+
* page should have been, and the caller is told which pages to re-run.
|
|
23
|
+
*/
|
|
24
|
+
/** One page's conversion, successful or not. */
|
|
1
25
|
export interface PdfPageMarkdown {
|
|
26
|
+
/** 1-based, as a reader counts pages. */
|
|
2
27
|
pageNum: number;
|
|
28
|
+
/** The page's Markdown, or empty when every attempt failed. */
|
|
3
29
|
markdown: string;
|
|
30
|
+
/** How many attempts it took (or were spent failing). */
|
|
4
31
|
attempts: number;
|
|
32
|
+
/** Why the page could not be converted. Absent on success. */
|
|
5
33
|
error?: string;
|
|
34
|
+
/**
|
|
35
|
+
* Every agent the page was attempted with, for cost accounting — one per
|
|
36
|
+
* attempt, because a failed attempt still spent tokens.
|
|
37
|
+
*/
|
|
6
38
|
agents: ConversionAgent[];
|
|
7
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* What this module needs of an agent: somewhere to read the cost from. Narrower
|
|
42
|
+
* than `Agent` on purpose, so the module stays testable with a stub.
|
|
43
|
+
*/
|
|
8
44
|
export interface ConversionAgent {
|
|
9
45
|
totalCost: number;
|
|
10
46
|
finishAgent(): Promise<void>;
|
|
11
47
|
}
|
|
12
|
-
|
|
48
|
+
/** Renders one page of the document to Markdown. Supplied by the caller. */
|
|
49
|
+
export type RenderPage = (
|
|
50
|
+
/** A one-page PDF, ready to attach. */
|
|
51
|
+
pageBuffer: Uint8Array, pageNum: number, pageCount: number) => Promise<{
|
|
13
52
|
markdown: string;
|
|
14
53
|
agent?: ConversionAgent;
|
|
15
54
|
}>;
|
|
55
|
+
/** How a conversion should be run. Every field has an environment-backed default. */
|
|
16
56
|
export interface PdfConversionOptions {
|
|
57
|
+
/**
|
|
58
|
+
* How many pages may be converted at once.
|
|
59
|
+
*
|
|
60
|
+
* The reason this exists: converting every page concurrently is what a 146-page
|
|
61
|
+
* document did before there was a cap, and the provider rate-limited most of
|
|
62
|
+
* them. Defaults to `PDF_CONVERT_CONCURRENCY` or 20.
|
|
63
|
+
*/
|
|
17
64
|
concurrency?: number;
|
|
65
|
+
/** Attempts per page before giving up. Defaults to `PDF_CONVERT_PAGE_ATTEMPTS` or 3. */
|
|
18
66
|
attempts?: number;
|
|
67
|
+
/** Per-attempt timeout. Defaults to `PDF_CONVERT_PAGE_TIMEOUT_MS` or 2 minutes. */
|
|
19
68
|
pageTimeoutMs?: number;
|
|
69
|
+
/** Base delay for the exponential backoff between attempts. Defaults to 1000ms. */
|
|
20
70
|
retryBackoffMs?: number;
|
|
71
|
+
/** Called as pages complete, for a caller with somewhere to show it. */
|
|
21
72
|
onProgress?: (message: string) => void;
|
|
22
73
|
}
|
|
74
|
+
/** A finished conversion. */
|
|
23
75
|
export interface PdfConversionResult {
|
|
24
76
|
pages: PdfPageMarkdown[];
|
|
77
|
+
/** Every page concatenated, with `<!-- Page N -->` markers and a header. */
|
|
25
78
|
markdown: string;
|
|
79
|
+
/** The pages that exhausted their attempts, in order. Empty on a clean run. */
|
|
26
80
|
failedPages: number[];
|
|
27
81
|
pageCount: number;
|
|
82
|
+
/** Summed across every page's agent. */
|
|
28
83
|
totalCost: number;
|
|
84
|
+
/** Every agent created, so a caller can finish them or bill them. */
|
|
29
85
|
agents: ConversionAgent[];
|
|
30
86
|
}
|
|
87
|
+
/** The retry policy, resolved from the environment. */
|
|
31
88
|
export interface ConversionLimits {
|
|
32
89
|
concurrency: number;
|
|
33
90
|
attempts: number;
|
|
34
91
|
pageTimeoutMs: number;
|
|
35
92
|
backoffMs: number;
|
|
36
93
|
}
|
|
94
|
+
/**
|
|
95
|
+
* The configured limits, so a caller converting a document whole rather than
|
|
96
|
+
* page-by-page retries on the same policy as this module does.
|
|
97
|
+
*
|
|
98
|
+
* @param overrides Anything the caller wants to pin
|
|
99
|
+
* @returns The resolved limits
|
|
100
|
+
*/
|
|
37
101
|
export declare function conversionLimits(overrides?: PdfConversionOptions): ConversionLimits;
|
|
102
|
+
/**
|
|
103
|
+
* Unwraps a response the model fenced as one ```markdown block.
|
|
104
|
+
*
|
|
105
|
+
* The prompt asks for bare Markdown and mostly gets it, but a page comes back
|
|
106
|
+
* wrapped often enough that the fence markers end up as literal content in the
|
|
107
|
+
* artifact. Only a fence that opens at the very start and closes at the very end is
|
|
108
|
+
* stripped, so a page whose real content is a code block is left alone.
|
|
109
|
+
*
|
|
110
|
+
* @param text The model's response
|
|
111
|
+
* @returns The response with a wrapping fence removed
|
|
112
|
+
*/
|
|
38
113
|
export declare function stripWrappingFence(text: string): string;
|
|
114
|
+
/** The marker written between pages. An HTML comment, so it does not render. */
|
|
39
115
|
export declare function pageMarker(pageNum: number): string;
|
|
116
|
+
/**
|
|
117
|
+
* Splits a converted document back into its pages on the `<!-- Page N -->` markers.
|
|
118
|
+
*
|
|
119
|
+
* The inverse of {@link buildMarkdown}, and the reason the markers are machine
|
|
120
|
+
* readable at all: re-running the pages that failed means finding them, converting
|
|
121
|
+
* them, and splicing them back.
|
|
122
|
+
*
|
|
123
|
+
* @param markdown A document this module produced, or any Markdown
|
|
124
|
+
* @returns One entry per page; the whole document as page 1 when it carries no markers
|
|
125
|
+
*/
|
|
40
126
|
export declare function splitMarkdownIntoPages(markdown: string): PdfPageMarkdown[];
|
|
127
|
+
/**
|
|
128
|
+
* Assembles the pages into one document.
|
|
129
|
+
*
|
|
130
|
+
* A run that lost pages says so **at the top** as well as in place. An inline
|
|
131
|
+
* marker alone is easy to scroll past in a 146-page transcript, and a reader who
|
|
132
|
+
* does not notice it treats a document with holes in it as complete.
|
|
133
|
+
*
|
|
134
|
+
* @param pages The converted pages, in order
|
|
135
|
+
* @param pageCount How many pages the document had
|
|
136
|
+
* @returns The assembled Markdown
|
|
137
|
+
*/
|
|
41
138
|
export declare function buildMarkdown(pages: readonly PdfPageMarkdown[], pageCount: number): string;
|
|
139
|
+
/**
|
|
140
|
+
* Runs `worker` over `count` items with at most `limit` in flight.
|
|
141
|
+
*
|
|
142
|
+
* A rolling pool rather than sequential batches: a batch barrier makes every
|
|
143
|
+
* finished page in a batch wait on its slowest sibling, which on a document whose
|
|
144
|
+
* pages vary in size is most of the wall clock.
|
|
145
|
+
*
|
|
146
|
+
* `worker` must not reject — a rejection here would abandon the remaining items.
|
|
147
|
+
*
|
|
148
|
+
* @param count How many items there are
|
|
149
|
+
* @param limit Maximum concurrent workers
|
|
150
|
+
* @param worker Receives the item index; must resolve
|
|
151
|
+
* @returns Results in item order
|
|
152
|
+
*/
|
|
42
153
|
export declare function runWithConcurrency<T>(count: number, limit: number, worker: (index: number) => Promise<T>): Promise<T[]>;
|
|
154
|
+
/**
|
|
155
|
+
* Converts every page of a PDF to Markdown.
|
|
156
|
+
*
|
|
157
|
+
* @param pdfBuffer The whole document
|
|
158
|
+
* @param renderPage Converts one page; supplied by the caller
|
|
159
|
+
* @param options Concurrency, attempts, timeout and progress
|
|
160
|
+
* @returns The pages, the assembled Markdown, and what failed
|
|
161
|
+
* @throws If the PDF cannot be parsed at all, or carries no pages
|
|
162
|
+
*/
|
|
43
163
|
export declare function convertPdfToMarkdown(pdfBuffer: Uint8Array, renderPage: RenderPage, options?: PdfConversionOptions): Promise<PdfConversionResult>;
|
|
@@ -1,4 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Turning a PDF into Markdown, one page at a time.
|
|
4
|
+
*
|
|
5
|
+
* The machinery only: splitting the document, bounding how many pages are in
|
|
6
|
+
* flight, retrying a page that fails, and assembling the result. Rendering a page
|
|
7
|
+
* is the caller's job — it supplies a {@link RenderPage} that owns the agent, the
|
|
8
|
+
* model and the prompt — so this module has no dependency on AppService and can be
|
|
9
|
+
* tested without an LLM.
|
|
10
|
+
*
|
|
11
|
+
* Three failure modes are handled here because each one was silently losing pages:
|
|
12
|
+
*
|
|
13
|
+
* - **An empty response.** The conversion occasionally returns nothing at all. Left
|
|
14
|
+
* alone the page reaches the reader as a gap with no indication anything was
|
|
15
|
+
* dropped, which is worse than an error.
|
|
16
|
+
* - **A thrown error.** A rate limit is the common one, and it is precisely what a
|
|
17
|
+
* large document provokes by converting every page at once. Retried with backoff.
|
|
18
|
+
* - **A page that never settles.** Raced against a timeout inside the retry, so a
|
|
19
|
+
* stalled page is retried rather than lost.
|
|
20
|
+
*
|
|
21
|
+
* A page that exhausts its attempts does not fail the document. It comes back with
|
|
22
|
+
* empty markdown and an `error`, the assembled Markdown carries a marker where the
|
|
23
|
+
* page should have been, and the caller is told which pages to re-run.
|
|
24
|
+
*/
|
|
2
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
26
|
exports.conversionLimits = conversionLimits;
|
|
4
27
|
exports.stripWrappingFence = stripWrappingFence;
|
|
@@ -12,10 +35,18 @@ const DEFAULT_CONCURRENCY = 20;
|
|
|
12
35
|
const DEFAULT_ATTEMPTS = 3;
|
|
13
36
|
const DEFAULT_PAGE_TIMEOUT_MS = 2 * 60 * 1000;
|
|
14
37
|
const DEFAULT_BACKOFF_MS = 1000;
|
|
38
|
+
/** Reads a positive integer from the environment, falling back when unset or unusable. */
|
|
15
39
|
function envInt(name, fallback) {
|
|
16
40
|
const parsed = Number(process.env[name]);
|
|
17
41
|
return Number.isFinite(parsed) && parsed > 0 ? Math.floor(parsed) : fallback;
|
|
18
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* The configured limits, so a caller converting a document whole rather than
|
|
45
|
+
* page-by-page retries on the same policy as this module does.
|
|
46
|
+
*
|
|
47
|
+
* @param overrides Anything the caller wants to pin
|
|
48
|
+
* @returns The resolved limits
|
|
49
|
+
*/
|
|
19
50
|
function conversionLimits(overrides = {}) {
|
|
20
51
|
return {
|
|
21
52
|
concurrency: overrides.concurrency ?? envInt('PDF_CONVERT_CONCURRENCY', DEFAULT_CONCURRENCY),
|
|
@@ -24,15 +55,38 @@ function conversionLimits(overrides = {}) {
|
|
|
24
55
|
backoffMs: overrides.retryBackoffMs ?? DEFAULT_BACKOFF_MS,
|
|
25
56
|
};
|
|
26
57
|
}
|
|
58
|
+
/**
|
|
59
|
+
* Unwraps a response the model fenced as one ```markdown block.
|
|
60
|
+
*
|
|
61
|
+
* The prompt asks for bare Markdown and mostly gets it, but a page comes back
|
|
62
|
+
* wrapped often enough that the fence markers end up as literal content in the
|
|
63
|
+
* artifact. Only a fence that opens at the very start and closes at the very end is
|
|
64
|
+
* stripped, so a page whose real content is a code block is left alone.
|
|
65
|
+
*
|
|
66
|
+
* @param text The model's response
|
|
67
|
+
* @returns The response with a wrapping fence removed
|
|
68
|
+
*/
|
|
27
69
|
function stripWrappingFence(text) {
|
|
28
70
|
const trimmed = (text || '').trim();
|
|
29
71
|
const match = /^```[a-zA-Z0-9_-]*\r?\n([\s\S]*?)\r?\n?```$/.exec(trimmed);
|
|
30
72
|
return match ? match[1].trim() : trimmed;
|
|
31
73
|
}
|
|
74
|
+
/** The marker written between pages. An HTML comment, so it does not render. */
|
|
32
75
|
function pageMarker(pageNum) {
|
|
33
76
|
return `<!-- Page ${pageNum} -->`;
|
|
34
77
|
}
|
|
78
|
+
/** Header comment prefixes dropped when splitting a document back into pages. */
|
|
35
79
|
const HEADER_PREFIXES = ['<!-- Document Markdown', '<!-- Converted:', '<!-- Total Pages:'];
|
|
80
|
+
/**
|
|
81
|
+
* Splits a converted document back into its pages on the `<!-- Page N -->` markers.
|
|
82
|
+
*
|
|
83
|
+
* The inverse of {@link buildMarkdown}, and the reason the markers are machine
|
|
84
|
+
* readable at all: re-running the pages that failed means finding them, converting
|
|
85
|
+
* them, and splicing them back.
|
|
86
|
+
*
|
|
87
|
+
* @param markdown A document this module produced, or any Markdown
|
|
88
|
+
* @returns One entry per page; the whole document as page 1 when it carries no markers
|
|
89
|
+
*/
|
|
36
90
|
function splitMarkdownIntoPages(markdown) {
|
|
37
91
|
const pages = [];
|
|
38
92
|
const markerPattern = /^<!--\s*Page\s+(\d+)\s*-->$/i;
|
|
@@ -60,6 +114,17 @@ function splitMarkdownIntoPages(markdown) {
|
|
|
60
114
|
}
|
|
61
115
|
return pages;
|
|
62
116
|
}
|
|
117
|
+
/**
|
|
118
|
+
* Assembles the pages into one document.
|
|
119
|
+
*
|
|
120
|
+
* A run that lost pages says so **at the top** as well as in place. An inline
|
|
121
|
+
* marker alone is easy to scroll past in a 146-page transcript, and a reader who
|
|
122
|
+
* does not notice it treats a document with holes in it as complete.
|
|
123
|
+
*
|
|
124
|
+
* @param pages The converted pages, in order
|
|
125
|
+
* @param pageCount How many pages the document had
|
|
126
|
+
* @returns The assembled Markdown
|
|
127
|
+
*/
|
|
63
128
|
function buildMarkdown(pages, pageCount) {
|
|
64
129
|
const failed = pages.filter((page) => !page.markdown.trim()).map((page) => page.pageNum);
|
|
65
130
|
const parts = [
|
|
@@ -80,6 +145,20 @@ function buildMarkdown(pages, pageCount) {
|
|
|
80
145
|
}
|
|
81
146
|
return parts.join('\n');
|
|
82
147
|
}
|
|
148
|
+
/**
|
|
149
|
+
* Runs `worker` over `count` items with at most `limit` in flight.
|
|
150
|
+
*
|
|
151
|
+
* A rolling pool rather than sequential batches: a batch barrier makes every
|
|
152
|
+
* finished page in a batch wait on its slowest sibling, which on a document whose
|
|
153
|
+
* pages vary in size is most of the wall clock.
|
|
154
|
+
*
|
|
155
|
+
* `worker` must not reject — a rejection here would abandon the remaining items.
|
|
156
|
+
*
|
|
157
|
+
* @param count How many items there are
|
|
158
|
+
* @param limit Maximum concurrent workers
|
|
159
|
+
* @param worker Receives the item index; must resolve
|
|
160
|
+
* @returns Results in item order
|
|
161
|
+
*/
|
|
83
162
|
async function runWithConcurrency(count, limit, worker) {
|
|
84
163
|
const results = new Array(count);
|
|
85
164
|
let next = 0;
|
|
@@ -94,6 +173,7 @@ async function runWithConcurrency(count, limit, worker) {
|
|
|
94
173
|
await Promise.all(Array.from({ length: Math.max(1, Math.min(limit, count)) }, runner));
|
|
95
174
|
return results;
|
|
96
175
|
}
|
|
176
|
+
/** Races a promise against a timeout, clearing the timer either way. */
|
|
97
177
|
async function withTimeout(work, ms, label) {
|
|
98
178
|
let timer;
|
|
99
179
|
const timeout = new Promise((_, reject) => {
|
|
@@ -107,6 +187,15 @@ async function withTimeout(work, ms, label) {
|
|
|
107
187
|
}
|
|
108
188
|
}
|
|
109
189
|
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
190
|
+
/**
|
|
191
|
+
* Converts every page of a PDF to Markdown.
|
|
192
|
+
*
|
|
193
|
+
* @param pdfBuffer The whole document
|
|
194
|
+
* @param renderPage Converts one page; supplied by the caller
|
|
195
|
+
* @param options Concurrency, attempts, timeout and progress
|
|
196
|
+
* @returns The pages, the assembled Markdown, and what failed
|
|
197
|
+
* @throws If the PDF cannot be parsed at all, or carries no pages
|
|
198
|
+
*/
|
|
110
199
|
async function convertPdfToMarkdown(pdfBuffer, renderPage, options = {}) {
|
|
111
200
|
const pdfDoc = await pdf_lib_1.PDFDocument.load(pdfBuffer);
|
|
112
201
|
const pageCount = pdfDoc.getPageCount();
|
|
@@ -135,6 +224,7 @@ async function convertPdfToMarkdown(pdfBuffer, renderPage, options = {}) {
|
|
|
135
224
|
agents,
|
|
136
225
|
};
|
|
137
226
|
}
|
|
227
|
+
/** Extracts one page and converts it, retrying an empty response or a thrown error. */
|
|
138
228
|
async function convertOnePage(pdfDoc, pageIndex, pageCount, renderPage, limits) {
|
|
139
229
|
const pageNum = pageIndex + 1;
|
|
140
230
|
let pageBuffer;
|
|
@@ -145,6 +235,8 @@ async function convertOnePage(pdfDoc, pageIndex, pageCount, renderPage, limits)
|
|
|
145
235
|
pageBuffer = await singlePage.save();
|
|
146
236
|
}
|
|
147
237
|
catch (error) {
|
|
238
|
+
// Extraction is local work: if it fails the page is unreadable and no
|
|
239
|
+
// number of LLM calls will change that, so it is not retried.
|
|
148
240
|
return {
|
|
149
241
|
pageNum,
|
|
150
242
|
markdown: '',
|
|
@@ -158,6 +250,8 @@ async function convertOnePage(pdfDoc, pageIndex, pageCount, renderPage, limits)
|
|
|
158
250
|
for (let attempt = 1; attempt <= limits.attempts; attempt++) {
|
|
159
251
|
try {
|
|
160
252
|
const rendered = await withTimeout(renderPage(pageBuffer, pageNum, pageCount), limits.pageTimeoutMs, `page ${pageNum}`);
|
|
253
|
+
// Collected whether or not the attempt worked: a failed attempt still
|
|
254
|
+
// spent tokens, and dropping it would under-report the conversion's cost.
|
|
161
255
|
if (rendered.agent)
|
|
162
256
|
agents.push(rendered.agent);
|
|
163
257
|
const markdown = stripWrappingFence(rendered.markdown);
|