feat(inventory): add v2 parsing and generation support

Add full AI integration for inventory v2 format:

**Parsing (NEW: inventoryParser.js, 125 lines):**
- extractInventoryData() - Parse multi-line v2 format from AI responses
  - Extracts "On Person: ..." section
  - Extracts multiple "Stored - [Location]: ..." sections
  - Extracts "Assets: ..." section
  - Returns InventoryV2 object
- extractLegacyInventory() - Fallback parser for old v1 format
- extractInventory() - Main function that tries v2 first, falls back to v1

**Parsing Integration (parser.js):**
- Import extractInventory() from inventoryParser
- Replace old single-line regex with v2-aware extraction
- Use feature flag to switch between v1/v2 parsing modes
- Maintains backward compatibility with FEATURE_FLAGS.useNewInventory

**Generation (promptBuilder.js, 60 lines changed):**
- NEW: buildInventorySummary() - Converts v2 object to multi-line text
  - Formats "On Person: ..." line
  - Formats multiple "Stored - [Location]: ..." lines
  - Formats "Assets: ..." line
  - Handles legacy v1 string format for backward compat
- Update generateTrackerInstructions() with v2 format spec:
  - Shows AI how to format inventory in multi-line v2 structure
  - Includes note about multiple storage locations
  - Falls back to v1 format when feature flag disabled
- Update generateContextualSummary() to use buildInventorySummary()
  - Converts v2 inventory to readable context for separate mode

**Format Examples:**

AI Output (v2 format):
```
On Person: Sword (equipped), 3x Health Potions, Leather Armor
Stored - Home: Spare clothes, Tools, 50 gold coins
Stored - Bank: Family heirloom, Important documents
Assets: Motorcycle (garage), Downtown apartment (owned)
```

Parsed Result:
```js
{
  version: 2,
  onPerson: "Sword (equipped), 3x Health Potions, Leather Armor",
  stored: {
    "Home": "Spare clothes, Tools, 50 gold coins",
    "Bank": "Family heirloom, Important documents"
  },
  assets: "Motorcycle (garage), Downtown apartment (owned)"
}
```

Changes:
- NEW: src/systems/generation/inventoryParser.js (125 lines)
- MODIFIED: src/systems/generation/parser.js (+14 lines)
- MODIFIED: src/systems/generation/promptBuilder.js (+60 lines)

Part of inventory system v2 implementation
Dependencies: v2 types, migration utility, persistence integration
This commit is contained in:
Lucas 'Paperboy' Rose-Winters
2025-10-17 15:10:31 +11:00
parent 39ec36f105
commit b00bae905f
3 changed files with 216 additions and 10 deletions
+15 -6
View File
@@ -3,8 +3,9 @@
* Handles parsing of AI responses to extract tracker data
*/
import { extensionSettings } from '../../core/state.js';
import { extensionSettings, FEATURE_FLAGS } from '../../core/state.js';
import { saveSettings } from '../../core/persistence.js';
import { extractInventory } from './inventoryParser.js';
/**
* Parses the model response to extract the different data sections.
@@ -91,8 +92,19 @@ export function parseUserStats(statsText) {
}
}
// Extract inventory
const inventoryMatch = statsText.match(/Inventory:\s*(.+)/i);
// Extract inventory - use v2 parser if feature flag enabled, otherwise fallback to v1
if (FEATURE_FLAGS.useNewInventory) {
const inventoryData = extractInventory(statsText);
if (inventoryData) {
extensionSettings.userStats.inventory = inventoryData;
}
} else {
// Legacy v1 parsing for backward compatibility
const inventoryMatch = statsText.match(/Inventory:\s*(.+)/i);
if (inventoryMatch) {
extensionSettings.userStats.inventory = inventoryMatch[1].trim();
}
}
if (healthMatch) extensionSettings.userStats.health = parseInt(healthMatch[1]);
if (satietyMatch) extensionSettings.userStats.satiety = parseInt(satietyMatch[1]);
@@ -103,9 +115,6 @@ export function parseUserStats(statsText) {
extensionSettings.userStats.mood = moodMatch[1].trim(); // Emoji
extensionSettings.userStats.conditions = moodMatch[2].trim(); // Conditions
}
if (inventoryMatch) {
extensionSettings.userStats.inventory = inventoryMatch[1].trim();
}
saveSettings();
} catch (error) {