ava-langgraph-narrative-intelligence 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +268 -0
- package/dist/graphs/index.cjs +1511 -0
- package/dist/graphs/index.cjs.map +1 -0
- package/dist/graphs/index.d.cts +2 -0
- package/dist/graphs/index.d.ts +2 -0
- package/dist/graphs/index.js +1468 -0
- package/dist/graphs/index.js.map +1 -0
- package/dist/index-Btxk3nQm.d.cts +430 -0
- package/dist/index-CgXXxuIH.d.ts +430 -0
- package/dist/index-CweT-D3c.d.cts +122 -0
- package/dist/index-D-zWH42e.d.cts +66 -0
- package/dist/index-D71kh3nE.d.cts +213 -0
- package/dist/index-DApls3w2.d.ts +66 -0
- package/dist/index-UamXITgg.d.ts +122 -0
- package/dist/index-v9AlRC0M.d.ts +213 -0
- package/dist/index.cjs +2753 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +6 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +2654 -0
- package/dist/index.js.map +1 -0
- package/dist/integrations/index.cjs +654 -0
- package/dist/integrations/index.cjs.map +1 -0
- package/dist/integrations/index.d.cts +2 -0
- package/dist/integrations/index.d.ts +2 -0
- package/dist/integrations/index.js +614 -0
- package/dist/integrations/index.js.map +1 -0
- package/dist/ncp-tXS9Jr9e.d.cts +132 -0
- package/dist/ncp-tXS9Jr9e.d.ts +132 -0
- package/dist/nodes/index.cjs +226 -0
- package/dist/nodes/index.cjs.map +1 -0
- package/dist/nodes/index.d.cts +2 -0
- package/dist/nodes/index.d.ts +2 -0
- package/dist/nodes/index.js +196 -0
- package/dist/nodes/index.js.map +1 -0
- package/dist/schemas/index.cjs +550 -0
- package/dist/schemas/index.cjs.map +1 -0
- package/dist/schemas/index.d.cts +2 -0
- package/dist/schemas/index.d.ts +2 -0
- package/dist/schemas/index.js +484 -0
- package/dist/schemas/index.js.map +1 -0
- package/dist/unified_state_bridge-CIDm1kuf.d.cts +266 -0
- package/dist/unified_state_bridge-CIDm1kuf.d.ts +266 -0
- package/package.json +91 -0
- package/src/graphs/coherence_engine.ts +1027 -0
- package/src/graphs/index.ts +47 -0
- package/src/graphs/three_universe_processor.ts +1136 -0
- package/src/index.ts +181 -0
- package/src/integrations/index.ts +17 -0
- package/src/integrations/redis_state.ts +691 -0
- package/src/nodes/emotional_classifier.ts +289 -0
- package/src/nodes/index.ts +17 -0
- package/src/schemas/index.ts +75 -0
- package/src/schemas/ncp.ts +312 -0
- package/src/schemas/unified_state_bridge.ts +681 -0
- package/src/tests/coherence_engine.test.ts +273 -0
- package/src/tests/three_universe_processor.test.ts +309 -0
- package/src/tests/unified_state_bridge.test.ts +360 -0
package/README.md
ADDED
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
# @langchain/langgraph-narrative-intelligence
|
|
2
|
+
|
|
3
|
+
Narrative Intelligence Toolkit for JavaScript/TypeScript - a complete port of the Python `narrative-intelligence` package.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
This package provides the core narrative intelligence components for the LangGraph ecosystem:
|
|
8
|
+
|
|
9
|
+
- **Three-Universe Processing** - Analyze events through Engineer (Mia), Ceremony (Ava8), and Story Engine (Miette) perspectives
|
|
10
|
+
- **Narrative Coherence Analysis** - Score narrative quality across 5 dimensions with actionable gap identification
|
|
11
|
+
- **Story Beat Classification** - Emotional tone detection using rule-based and LLM classification
|
|
12
|
+
- **State Management** - Redis-backed persistence for cross-system state sharing
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @langchain/langgraph-narrative-intelligence
|
|
18
|
+
# or
|
|
19
|
+
yarn add @langchain/langgraph-narrative-intelligence
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
### Optional Dependencies
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
# For LangGraph integration
|
|
26
|
+
npm install @langchain/langgraph
|
|
27
|
+
|
|
28
|
+
# For Redis persistence
|
|
29
|
+
npm install ioredis
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Quick Start
|
|
33
|
+
|
|
34
|
+
### Three-Universe Processing
|
|
35
|
+
|
|
36
|
+
Process any event through three interpretive lenses:
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
import { ThreeUniverseProcessor } from "@langchain/langgraph-narrative-intelligence";
|
|
40
|
+
|
|
41
|
+
const processor = new ThreeUniverseProcessor();
|
|
42
|
+
|
|
43
|
+
// Process a GitHub event
|
|
44
|
+
const analysis = processor.process(
|
|
45
|
+
{ content: "feat: implement new feature together with team" },
|
|
46
|
+
"github.push"
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
console.log(`Lead universe: ${analysis.leadUniverse}`); // "ceremony" (collaborative work)
|
|
50
|
+
console.log(`Coherence: ${analysis.coherenceScore}`); // 0.75
|
|
51
|
+
|
|
52
|
+
// Access individual perspectives
|
|
53
|
+
console.log(analysis.engineer.intent); // "feature_implementation"
|
|
54
|
+
console.log(analysis.ceremony.intent); // "co_creation"
|
|
55
|
+
console.log(analysis.storyEngine.intent); // "rising_action"
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Narrative Coherence Analysis
|
|
59
|
+
|
|
60
|
+
Analyze the quality of story beats:
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
import {
|
|
64
|
+
NarrativeCoherenceEngine,
|
|
65
|
+
createStoryBeat,
|
|
66
|
+
NarrativeFunction,
|
|
67
|
+
} from "@langchain/langgraph-narrative-intelligence";
|
|
68
|
+
|
|
69
|
+
const engine = new NarrativeCoherenceEngine();
|
|
70
|
+
|
|
71
|
+
const beats = [
|
|
72
|
+
createStoryBeat("beat_1", 1, "Setup", NarrativeFunction.INCITING_INCIDENT, 1),
|
|
73
|
+
createStoryBeat("beat_2", 2, "Rising action", NarrativeFunction.RISING_ACTION, 2),
|
|
74
|
+
createStoryBeat("beat_3", 3, "Climax", NarrativeFunction.CLIMAX, 3),
|
|
75
|
+
createStoryBeat("beat_4", 4, "Resolution", NarrativeFunction.RESOLUTION, 3),
|
|
76
|
+
];
|
|
77
|
+
|
|
78
|
+
const result = engine.analyze(beats);
|
|
79
|
+
|
|
80
|
+
// Overall coherence score
|
|
81
|
+
console.log(`Overall: ${result.coherenceScore.overall}%`);
|
|
82
|
+
|
|
83
|
+
// Component scores
|
|
84
|
+
console.log(`Narrative Flow: ${result.coherenceScore.narrativeFlow.score}%`);
|
|
85
|
+
console.log(`Pacing: ${result.coherenceScore.pacing.score}%`);
|
|
86
|
+
|
|
87
|
+
// Identified gaps
|
|
88
|
+
for (const gap of result.gaps) {
|
|
89
|
+
console.log(`Gap: ${gap.description} (${gap.severity})`);
|
|
90
|
+
console.log(`Route to: ${gap.suggestedRoute}`);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Trinity assessment (Mia/Miette/Ava8)
|
|
94
|
+
console.log(`Mia (structure): ${result.trinityAssessment.mia}`);
|
|
95
|
+
console.log(`Miette (emotion): ${result.trinityAssessment.miette}`);
|
|
96
|
+
console.log(`Ava8 (atmosphere): ${result.trinityAssessment.ava8}`);
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### Unified Narrative State
|
|
100
|
+
|
|
101
|
+
Create and manage narrative state across systems:
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
import {
|
|
105
|
+
createUnifiedNarrativeState,
|
|
106
|
+
addBeat,
|
|
107
|
+
createStoryBeat,
|
|
108
|
+
NarrativeFunction,
|
|
109
|
+
} from "@langchain/langgraph-narrative-intelligence";
|
|
110
|
+
|
|
111
|
+
// Create a new state with default characters (Mia, Ava8, Miette)
|
|
112
|
+
const state = createUnifiedNarrativeState("story_123", "session_456");
|
|
113
|
+
|
|
114
|
+
// Add a beat
|
|
115
|
+
const beat = createStoryBeat(
|
|
116
|
+
"beat_1",
|
|
117
|
+
1,
|
|
118
|
+
"The hero begins their journey",
|
|
119
|
+
NarrativeFunction.INCITING_INCIDENT,
|
|
120
|
+
1,
|
|
121
|
+
{
|
|
122
|
+
emotionalTone: "hopeful",
|
|
123
|
+
thematicTags: ["journey", "transformation"],
|
|
124
|
+
}
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
addBeat(state, beat);
|
|
128
|
+
|
|
129
|
+
console.log(`Act: ${state.position.act}`);
|
|
130
|
+
console.log(`Beat count: ${state.position.beatCount}`);
|
|
131
|
+
console.log(`Lead universe: ${state.position.leadUniverse}`);
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### Redis Persistence
|
|
135
|
+
|
|
136
|
+
Use Redis for cross-system state sharing:
|
|
137
|
+
|
|
138
|
+
```typescript
|
|
139
|
+
import { NarrativeRedisManager, createRedisConfig } from "@langchain/langgraph-narrative-intelligence";
|
|
140
|
+
|
|
141
|
+
const manager = new NarrativeRedisManager(createRedisConfig({
|
|
142
|
+
url: "redis://localhost:6379",
|
|
143
|
+
}));
|
|
144
|
+
|
|
145
|
+
await manager.connect();
|
|
146
|
+
|
|
147
|
+
// Get or create state
|
|
148
|
+
const state = await manager.getOrCreateState("story_123", "session_456");
|
|
149
|
+
|
|
150
|
+
// Add beats
|
|
151
|
+
await manager.addBeatToSession("session_456", beat);
|
|
152
|
+
|
|
153
|
+
// Cache analysis
|
|
154
|
+
await manager.cacheEventAnalysis("event_123", analysis);
|
|
155
|
+
|
|
156
|
+
await manager.disconnect();
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
## The Three Universes
|
|
160
|
+
|
|
161
|
+
The three-universe model from `multiverse_3act`:
|
|
162
|
+
|
|
163
|
+
| Universe | Character | Focus | Keywords |
|
|
164
|
+
|----------|-----------|-------|----------|
|
|
165
|
+
| **ENGINEER** | Mia (The Builder) | Technical precision | feat:, fix:, refactor |
|
|
166
|
+
| **CEREMONY** | Ava8 (The Keeper) | Relational protocols | together, thanks, collaborate |
|
|
167
|
+
| **STORY_ENGINE** | Miette (The Weaver) | Narrative patterns | begin, climax, resolution |
|
|
168
|
+
|
|
169
|
+
## Coherence Dimensions
|
|
170
|
+
|
|
171
|
+
The coherence engine analyzes 5 dimensions:
|
|
172
|
+
|
|
173
|
+
1. **Narrative Flow** - Smooth transitions, logical causality
|
|
174
|
+
2. **Character Consistency** - Voice consistency, arc progression
|
|
175
|
+
3. **Pacing** - Tension/relief distribution, climax positioning
|
|
176
|
+
4. **Theme Saturation** - Theme presence and payoff
|
|
177
|
+
5. **Continuity** - Timeline consistency, sequence ordering
|
|
178
|
+
|
|
179
|
+
## Gap Types
|
|
180
|
+
|
|
181
|
+
Identified gaps are categorized and routed:
|
|
182
|
+
|
|
183
|
+
| Gap Type | Route To | Description |
|
|
184
|
+
|----------|----------|-------------|
|
|
185
|
+
| STRUCTURAL | Structurist | Missing beats, incomplete arcs |
|
|
186
|
+
| THEMATIC | Structurist | Underdeveloped themes |
|
|
187
|
+
| CHARACTER | Storyteller | Inconsistent character voice |
|
|
188
|
+
| SENSORY | Storyteller | Lacking grounding detail |
|
|
189
|
+
| CONTINUITY | Author | Timeline inconsistencies |
|
|
190
|
+
|
|
191
|
+
## Emotional Tones
|
|
192
|
+
|
|
193
|
+
The emotional classifier recognizes these tones:
|
|
194
|
+
|
|
195
|
+
- Devastating, Hopeful, Tense, Joyful
|
|
196
|
+
- Melancholic, Triumphant, Fearful, Peaceful
|
|
197
|
+
- Conflicted, Resigned
|
|
198
|
+
|
|
199
|
+
## Integration with narrative-tracing
|
|
200
|
+
|
|
201
|
+
Connect to the `@langchain/langchain-narrative-tracing` package:
|
|
202
|
+
|
|
203
|
+
```typescript
|
|
204
|
+
import { LangGraphBridge } from "@langchain/langchain-narrative-tracing";
|
|
205
|
+
import { ThreeUniverseProcessor } from "@langchain/langgraph-narrative-intelligence";
|
|
206
|
+
|
|
207
|
+
const bridge = new LangGraphBridge(handler);
|
|
208
|
+
|
|
209
|
+
const processor = new ThreeUniverseProcessor({
|
|
210
|
+
tracingCallback: bridge.createThreeUniverseCallback(),
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
// All analyses now get traced to Langfuse
|
|
214
|
+
const analysis = processor.process(event, "github.push");
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
## API Reference
|
|
218
|
+
|
|
219
|
+
### Exports
|
|
220
|
+
|
|
221
|
+
```typescript
|
|
222
|
+
// Main exports
|
|
223
|
+
export {
|
|
224
|
+
// Processing
|
|
225
|
+
ThreeUniverseProcessor,
|
|
226
|
+
NarrativeCoherenceEngine,
|
|
227
|
+
EmotionalBeatClassifierNode,
|
|
228
|
+
|
|
229
|
+
// State
|
|
230
|
+
UnifiedNarrativeState,
|
|
231
|
+
createUnifiedNarrativeState,
|
|
232
|
+
|
|
233
|
+
// Types
|
|
234
|
+
Universe,
|
|
235
|
+
NarrativeFunction,
|
|
236
|
+
NarrativePhase,
|
|
237
|
+
GapType,
|
|
238
|
+
GapSeverity,
|
|
239
|
+
EmotionalTone,
|
|
240
|
+
|
|
241
|
+
// Redis
|
|
242
|
+
NarrativeRedisManager,
|
|
243
|
+
MockRedis,
|
|
244
|
+
} from "@langchain/langgraph-narrative-intelligence";
|
|
245
|
+
|
|
246
|
+
// Subpath exports
|
|
247
|
+
import * as schemas from "@langchain/langgraph-narrative-intelligence/schemas";
|
|
248
|
+
import * as graphs from "@langchain/langgraph-narrative-intelligence/graphs";
|
|
249
|
+
import * as nodes from "@langchain/langgraph-narrative-intelligence/nodes";
|
|
250
|
+
import * as integrations from "@langchain/langgraph-narrative-intelligence/integrations";
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
## Python Parity
|
|
254
|
+
|
|
255
|
+
This package is a complete TypeScript port of the Python `narrative-intelligence` package, maintaining full feature parity:
|
|
256
|
+
|
|
257
|
+
| Python Module | TypeScript Module |
|
|
258
|
+
|---------------|-------------------|
|
|
259
|
+
| `narrative_intelligence/schemas/unified_state_bridge.py` | `schemas/unified_state_bridge.ts` |
|
|
260
|
+
| `narrative_intelligence/schemas/ncp.py` | `schemas/ncp.ts` |
|
|
261
|
+
| `narrative_intelligence/graphs/three_universe_processor.py` | `graphs/three_universe_processor.ts` |
|
|
262
|
+
| `narrative_intelligence/graphs/coherence_engine.py` | `graphs/coherence_engine.ts` |
|
|
263
|
+
| `narrative_intelligence/nodes/emotional_classifier.py` | `nodes/emotional_classifier.ts` |
|
|
264
|
+
| `narrative_intelligence/integrations/redis_state.py` | `integrations/redis_state.ts` |
|
|
265
|
+
|
|
266
|
+
## License
|
|
267
|
+
|
|
268
|
+
MIT
|