@semiont/make-meaning 0.2.43 → 0.2.46

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 CHANGED
@@ -6,14 +6,18 @@
6
6
  [![npm downloads](https://img.shields.io/npm/dm/@semiont/make-meaning.svg)](https://www.npmjs.com/package/@semiont/make-meaning)
7
7
  [![License](https://img.shields.io/npm/l/@semiont/make-meaning.svg)](https://github.com/The-AI-Alliance/semiont/blob/main/LICENSE)
8
8
 
9
- **Making meaning from resources through context assembly, pattern detection, and relationship reasoning.**
9
+ **Making meaning from resources through actors, context assembly, and relationship reasoning.**
10
10
 
11
- This package transforms raw resources into meaningful, interconnected knowledge through:
11
+ This package implements the actor model from [ARCHITECTURE.md](../../docs/ARCHITECTURE.md). It owns the **Knowledge Base** and the actors that interface with it:
12
12
 
13
- - **Context Assembly**: Gathering resource metadata, content, and annotations from distributed storage
14
- - **Pattern Detection**: AI-powered discovery of semantic patterns (comments, highlights, assessments, tags)
15
- - **Relationship Reasoning**: Navigating connections between resources through graph traversal
16
- - **Job Workers**: Asynchronous processing of detection tasks with progress tracking
13
+ - **Stower** (write) the single write gateway to the Knowledge Base
14
+ - **Gatherer** (read) handles all browse reads, context assembly, and entity type listing
15
+ - **Binder** (search/link) searches KB stores for entity resolution and graph queries
16
+ - **CloneTokenManager** (yield) manages clone token lifecycle for resource cloning
17
+
18
+ All actors subscribe to the EventBus via RxJS pipelines. They expose only `initialize()` and `stop()` — no public business methods. Callers communicate with actors by putting events on the bus.
19
+
20
+ The EventBus is a **complete interface** for all knowledge-domain operations. HTTP routes in the backend are thin wrappers that delegate to EventBus actors. The system can operate entirely without HTTP — see `EventBusClient` in `@semiont/api-client`.
17
21
 
18
22
  ## Quick Start
19
23
 
@@ -23,319 +27,194 @@ npm install @semiont/make-meaning
23
27
 
24
28
  ### Start Make-Meaning Service
25
29
 
26
- The simplest way to use make-meaning infrastructure is through the service module:
27
-
28
30
  ```typescript
29
31
  import { startMakeMeaning } from '@semiont/make-meaning';
30
- import type { EnvironmentConfig } from '@semiont/core';
32
+ import { EventBus } from '@semiont/core';
33
+ import type { EnvironmentConfig, Logger } from '@semiont/core';
31
34
 
32
- // Start all infrastructure (job queue, workers, graph consumer)
33
- const makeMeaning = await startMakeMeaning(config);
35
+ // EventBus is created outside make-meaning it is not encapsulated by this package
36
+ const eventBus = new EventBus();
34
37
 
35
- // Access job queue for route handlers
36
- const jobQueue = makeMeaning.jobQueue;
38
+ // Start all infrastructure
39
+ const makeMeaning = await startMakeMeaning(config, eventBus, logger);
40
+
41
+ // Access components
42
+ const { kb, jobQueue, stower, gatherer, binder, cloneTokenManager } = makeMeaning;
37
43
 
38
44
  // Graceful shutdown
39
45
  await makeMeaning.stop();
40
46
  ```
41
47
 
42
48
  This single call initializes:
43
- - Job queue
44
- - All 6 detection/generation workers
45
- - Graph consumer (event-to-graph synchronization)
46
- - Shared event store connection
47
-
48
- ### Assemble Resource Context
49
-
50
- ```typescript
51
- import { ResourceContext } from '@semiont/make-meaning';
52
-
53
- const resource = await ResourceContext.getResourceMetadata(resourceId, config);
54
- const resources = await ResourceContext.listResources({ createdAfter: '2024-01-01' }, config);
55
- const withContent = await ResourceContext.addContentPreviews(resources, config);
56
- ```
49
+ - **KnowledgeBase** — groups EventStore, ViewStorage, RepresentationStore, GraphDatabase
50
+ - **Stower** subscribes to write commands on EventBus
51
+ - **Gatherer** subscribes to browse reads, gather context, and entity type listing on EventBus
52
+ - **Binder** subscribes to search and referenced-by queries on EventBus
53
+ - **CloneTokenManager** — subscribes to clone token operations on EventBus
54
+ - **GraphDBConsumer** event-to-graph synchronization (RxJS burst-buffered pipeline)
55
+ - **JobQueue** — background job processing queue + job status subscription
56
+ - **6 annotation workers** — poll job queue for async AI tasks
57
57
 
58
- ### Work with Annotations
58
+ ### Create a Resource (via EventBus)
59
59
 
60
60
  ```typescript
61
- import { AnnotationContext } from '@semiont/make-meaning';
62
-
63
- // Get all annotations for a resource
64
- const annotations = await AnnotationContext.getResourceAnnotations(resourceId, config);
65
-
66
- // Build LLM context for an annotation (includes surrounding text)
67
- const context = await AnnotationContext.buildLLMContext(
68
- annotationUri,
69
- resourceId,
70
- config,
71
- { contextLines: 5 }
61
+ import { ResourceOperations } from '@semiont/make-meaning';
62
+ import { userId } from '@semiont/core';
63
+
64
+ const result = await ResourceOperations.createResource(
65
+ {
66
+ name: 'My Document',
67
+ content: Buffer.from('Document content here'),
68
+ format: 'text/plain',
69
+ language: 'en',
70
+ },
71
+ userId('user-123'),
72
+ eventBus,
73
+ config.services.backend.publicURL,
72
74
  );
73
75
  ```
74
76
 
75
- ### Detect Semantic Patterns
77
+ `ResourceOperations.createResource` emits `yield:create` on the EventBus. The Stower subscribes to this event, persists the resource to the EventStore and ContentStore, and emits `yield:created` back on the bus.
76
78
 
77
- ```typescript
78
- import { AnnotationDetection } from '@semiont/make-meaning';
79
-
80
- // AI-powered detection of passages that merit commentary
81
- const comments = await AnnotationDetection.detectComments(
82
- resourceId,
83
- config,
84
- 'Focus on technical explanations',
85
- 'educational',
86
- 0.7
87
- );
79
+ ### Gather Context (via EventBus)
88
80
 
89
- // Detect passages that should be highlighted
90
- const highlights = await AnnotationDetection.detectHighlights(
91
- resourceId,
92
- config,
93
- 'Find key definitions and important concepts',
94
- 0.5
95
- );
81
+ ```typescript
82
+ import { firstValueFrom, race, filter, timeout } from 'rxjs';
96
83
 
97
- // Detect and extract structured tags from text using ontology schemas
98
- const tags = await AnnotationDetection.detectTags(
84
+ // Emit gather request
85
+ eventBus.get('gather:requested').next({
86
+ annotationUri,
99
87
  resourceId,
100
- config,
101
- 'irac', // Schema ID from @semiont/ontology
102
- 'issue' // Category within the schema
88
+ options: { contextLines: 5 },
89
+ });
90
+
91
+ // Await result
92
+ const result = await firstValueFrom(
93
+ race(
94
+ eventBus.get('gather:complete').pipe(filter(e => e.annotationUri === annotationUri)),
95
+ eventBus.get('gather:failed').pipe(filter(e => e.annotationUri === annotationUri)),
96
+ ).pipe(timeout(30_000)),
103
97
  );
104
98
  ```
105
99
 
106
- ### Navigate Resource Relationships
107
-
108
- ```typescript
109
- import { GraphContext } from '@semiont/make-meaning';
110
-
111
- // Find resources that link to this resource (backlinks)
112
- const backlinks = await GraphContext.getBacklinks(resourceId, config);
113
-
114
- // Find shortest path between two resources
115
- const paths = await GraphContext.findPath(fromResourceId, toResourceId, config, 3);
116
-
117
- // Full-text search across all resources
118
- const results = await GraphContext.searchResources('neural networks', config, 10);
119
- ```
120
-
121
- ### Use Individual Workers (Advanced)
122
-
123
- For fine-grained control, workers can be instantiated directly:
124
-
125
- ```typescript
126
- import {
127
- ReferenceDetectionWorker,
128
- HighlightDetectionWorker,
129
- GenerationWorker,
130
- } from '@semiont/make-meaning';
131
- import { JobQueue } from '@semiont/jobs';
132
- import { createEventStore } from '@semiont/event-sourcing';
133
-
134
- // Create shared dependencies
135
- const jobQueue = new JobQueue({ dataDir: './data' });
136
- await jobQueue.initialize();
137
- const eventStore = createEventStore('./data', 'http://localhost:3000');
138
-
139
- // Create workers with explicit dependencies
140
- const referenceWorker = new ReferenceDetectionWorker(jobQueue, config, eventStore);
141
- const highlightWorker = new HighlightDetectionWorker(jobQueue, config, eventStore);
142
- const generationWorker = new GenerationWorker(jobQueue, config, eventStore);
143
-
144
- // Start workers
145
- await Promise.all([
146
- referenceWorker.start(),
147
- highlightWorker.start(),
148
- generationWorker.start(),
149
- ]);
150
- ```
151
-
152
- **Note**: In most cases, use `startMakeMeaning()` instead, which handles all initialization automatically.
153
-
154
- ## Documentation
155
-
156
- - **[API Reference](./docs/api-reference.md)** - Complete API documentation for all classes and methods
157
- - **[Job Workers](./docs/job-workers.md)** - Asynchronous task processing with progress tracking
158
- - **[Architecture](./docs/architecture.md)** - System design and data flow
159
- - **[Examples](./docs/examples.md)** - Common use cases and patterns
160
-
161
- ## Philosophy
162
-
163
- Resources don't exist in isolation. A document becomes meaningful when we understand its annotations, its relationships to other resources, and the patterns within its content. `@semiont/make-meaning` provides the infrastructure to:
164
-
165
- 1. **Assemble context** from event-sourced storage
166
- 2. **Detect patterns** using AI inference
167
- 3. **Reason about relationships** through graph traversal
168
-
169
- This is the "applied meaning-making" layer - it sits between low-level AI primitives ([@semiont/inference](../inference/)) and high-level application orchestration ([apps/backend](../../apps/backend/)).
170
-
171
- ## Infrastructure Ownership
172
-
173
- **MakeMeaningService is the single source of truth for all infrastructure:**
100
+ ## Architecture
174
101
 
175
- ```typescript
176
- import { startMakeMeaning } from '@semiont/make-meaning';
102
+ ### Actor Model
177
103
 
178
- // Create ALL infrastructure once at startup
179
- const makeMeaning = await startMakeMeaning(config);
104
+ All meaningful actions flow through the EventBus. The three KB actors are reactive — they subscribe via RxJS pipelines in `initialize()` and communicate results by emitting on the bus.
180
105
 
181
- // Access infrastructure components
182
- const { eventStore, graphDb, repStore, inferenceClient, jobQueue } = makeMeaning;
106
+ ```mermaid
107
+ graph TB
108
+ Routes["Backend Routes"] -->|commands| BUS["Event Bus"]
109
+ Workers["Job Workers"] -->|commands| BUS
110
+ EBC["EventBusClient"] -->|commands| BUS
111
+
112
+ BUS -->|"yield:create, mark:create,<br/>mark:delete, job:*"| STOWER["Stower<br/>(write)"]
113
+ BUS -->|"browse:*, gather:*,<br/>mark:entity-types-*"| GATHERER["Gatherer<br/>(read)"]
114
+ BUS -->|"bind:search-*,<br/>bind:referenced-by-*"| BINDER["Binder<br/>(search/link)"]
115
+ BUS -->|"yield:clone-*"| CTM["CloneTokenManager<br/>(clone)"]
116
+
117
+ STOWER -->|persist| KB["Knowledge Base"]
118
+ GATHERER -->|query| KB
119
+ BINDER -->|query| KB
120
+ CTM -->|query| KB
121
+
122
+ STOWER -->|"yield:created, mark:created"| BUS
123
+ GATHERER -->|"browse:*-result,<br/>gather:complete"| BUS
124
+ BINDER -->|"bind:search-results,<br/>bind:referenced-by-result"| BUS
125
+ CTM -->|"yield:clone-token-generated,<br/>yield:clone-resource-result"| BUS
126
+
127
+ classDef bus fill:#e8a838,stroke:#b07818,stroke-width:3px,color:#000,font-weight:bold
128
+ classDef actor fill:#5a9a6a,stroke:#3d6644,stroke-width:2px,color:#fff
129
+ classDef kb fill:#8b6b9d,stroke:#6b4a7a,stroke-width:2px,color:#fff
130
+ classDef caller fill:#4a90a4,stroke:#2c5f7a,stroke-width:2px,color:#fff
131
+
132
+ class BUS bus
133
+ class STOWER,GATHERER,BINDER,CTM actor
134
+ class KB kb
135
+ class Routes,Workers,EBC caller
183
136
  ```
184
137
 
185
- **What MakeMeaningService Owns:**
138
+ ### Knowledge Base
186
139
 
187
- 1. **EventStore** - Event log and materialized views (single source of truth)
188
- 2. **GraphDatabase** - Graph database connection for relationships and traversal
189
- 3. **RepresentationStore** - Content-addressed document storage
190
- 4. **InferenceClient** - LLM client for AI operations
191
- 5. **JobQueue** - Background job processing queue
192
- 6. **Workers** - All 6 detection/generation workers
193
- 7. **GraphDBConsumer** - Event-to-graph synchronization
140
+ The Knowledge Base is an inert store it has no intelligence, no goals, no decisions. It groups four subsystems:
194
141
 
195
- **Critical Design Rule:**
142
+ | Store | Implementation | Purpose |
143
+ |-------|---------------|---------|
144
+ | **Event Log** | `EventStore` | Immutable append-only log of all domain events |
145
+ | **Materialized Views** | `ViewStorage` | Denormalized projections for fast reads |
146
+ | **Content Store** | `RepresentationStore` | Content-addressed binary storage (SHA-256) |
147
+ | **Graph** | `GraphDatabase` | Eventually consistent relationship projection |
196
148
 
197
149
  ```typescript
198
- // CORRECT: Access infrastructure from MakeMeaningService
199
- const { graphDb } = makeMeaning;
150
+ import { createKnowledgeBase } from '@semiont/make-meaning';
200
151
 
201
- // WRONG: NEVER create infrastructure outside of startMakeMeaning()
202
- const graphDb = await getGraphDatabase(config); // NEVER DO THIS
203
- const repStore = new FilesystemRepresentationStore(...); // NEVER DO THIS
204
- const eventStore = createEventStore(...); // NEVER DO THIS
152
+ const kb = createKnowledgeBase(eventStore, basePath, projectRoot, graphDb, logger);
153
+ // kb.eventStore, kb.views, kb.content, kb.graph
205
154
  ```
206
155
 
207
- **Why This Matters:**
208
-
209
- - **Single initialization** - All infrastructure created once, shared everywhere
210
- - **No resource leaks** - Single connection per resource type (database, storage, etc.)
211
- - **Consistent configuration** - Same config across all components
212
- - **Testability** - Single injection point for mocking
213
- - **Lifecycle management** - Centralized shutdown via `makeMeaning.stop()`
214
-
215
- **Implementation Pattern:**
216
-
217
- - Backend creates MakeMeaningService in [apps/backend/src/index.ts:56](../../apps/backend/src/index.ts#L56)
218
- - Routes access via Hono context: `c.get('makeMeaning')`
219
- - Services receive infrastructure as parameters (dependency injection)
220
- - Workers receive EventStore and InferenceClient via constructor
221
-
222
- This architectural pattern prevents duplicate connections, ensures consistent state, and provides clear ownership boundaries across the entire system.
223
-
224
- ## Architecture
225
-
226
- Three-layer design separating concerns:
156
+ ### EventBus Ownership
227
157
 
228
- ```mermaid
229
- graph TB
230
- Backend["<b>apps/backend</b><br/>Job orchestration, HTTP APIs, streaming"]
231
- MakeMeaning["<b>@semiont/make-meaning</b><br/>Context assembly, detection/generation,<br/>prompt engineering, response parsing,<br/>job workers"]
232
- Inference["<b>@semiont/inference</b><br/>AI primitives only:<br/>generateText, client management"]
158
+ The EventBus is created by the backend (or script) and passed into `startMakeMeaning()` as a dependency. Make-meaning does not own or encapsulate the EventBus — it is shared across the entire system.
233
159
 
234
- Backend --> MakeMeaning
235
- MakeMeaning --> Inference
236
-
237
- style Backend fill:#e1f5ff
238
- style MakeMeaning fill:#fff4e6
239
- style Inference fill:#f3e5f5
240
- ```
241
-
242
- **Key principles:**
243
-
244
- - **Centralized infrastructure**: All infrastructure owned by MakeMeaningService (single initialization point)
245
- - **Event-sourced context**: Resources and annotations assembled from event streams
246
- - **Content-addressed storage**: Content retrieved using checksums (deduplication, caching)
247
- - **Graph-backed relationships**: @semiont/graph provides traversal for backlinks, paths, connections
248
- - **Explicit dependencies**: Workers receive infrastructure via constructor (dependency injection, no singletons)
249
- - **No ad-hoc creation**: Routes and services NEVER create their own infrastructure instances
160
+ ## Documentation
250
161
 
251
- See [Architecture](./docs/architecture.md) for complete details.
162
+ - **[Architecture](./docs/architecture.md)** Actor model, data flow, storage architecture
163
+ - **[API Reference](./docs/api-reference.md)** — Context modules and operations
164
+ - **[Examples](./docs/examples.md)** — Common use cases and patterns
165
+ - **[Job Workers](./docs/job-workers.md)** — Async annotation workers (in @semiont/jobs)
166
+ - **[Scripting](./docs/SCRIPTING.md)** — Direct scripting without HTTP backend
252
167
 
253
168
  ## Exports
254
169
 
255
- ### Service Module (Primary)
256
-
257
- - `startMakeMeaning(config)` - Initialize all make-meaning infrastructure
258
- - `MakeMeaningService` - Type for service return value
259
- - `GraphDBConsumer` - Graph consumer class (for advanced use)
170
+ ### Service (Primary)
260
171
 
261
- ### Context Assembly
172
+ - `startMakeMeaning(config, eventBus, logger)` — Initialize all infrastructure
173
+ - `MakeMeaningService` — Type for service return value
262
174
 
263
- - `ResourceContext` - Resource metadata and content
264
- - `AnnotationContext` - Annotation queries and context building
265
- - `GraphContext` - Graph traversal and search
175
+ ### Knowledge Base
266
176
 
267
- ### Detection & Generation
177
+ - `createKnowledgeBase(...)` Factory function
178
+ - `KnowledgeBase` — Interface grouping the four KB stores
268
179
 
269
- - `AnnotationDetection` - AI-powered semantic pattern detection (orchestrates detection pipeline)
270
- - `MotivationPrompts` - Prompt builders for comment/highlight/assessment/tag detection
271
- - `MotivationParsers` - Response parsers with offset validation
272
- - `extractEntities` - Entity extraction with context-based disambiguation
273
- - `generateResourceFromTopic` - Markdown resource generation with language support
274
- - `generateResourceSummary` - Resource summarization
275
- - `generateReferenceSuggestions` - Smart suggestion generation
180
+ ### Actors
276
181
 
277
- ### Job Workers (Advanced)
182
+ - `Stower` Write gateway actor
183
+ - `Gatherer` — Read actor (browse reads, context assembly, entity type listing)
184
+ - `Binder` — Search/link actor (entity resolution, referenced-by queries)
185
+ - `CloneTokenManager` — Clone token lifecycle actor (yield domain)
278
186
 
279
- - `ReferenceDetectionWorker` - Entity reference detection
280
- - `GenerationWorker` - AI content generation
281
- - `HighlightDetectionWorker` - Highlight detection
282
- - `CommentDetectionWorker` - Comment detection
283
- - `AssessmentDetectionWorker` - Assessment detection
284
- - `TagDetectionWorker` - Structured tag detection
187
+ ### Operations
285
188
 
286
- **Note**: Workers are typically managed by `startMakeMeaning()`, not instantiated directly.
189
+ - `ResourceOperations` Resource CRUD (emits commands to EventBus)
190
+ - `AnnotationOperations` — Annotation CRUD (emits commands to EventBus)
287
191
 
288
- See [Job Workers](./docs/job-workers.md) for implementation details.
192
+ ### Context Assembly
289
193
 
290
- ### Types
194
+ - `ResourceContext` — Resource metadata queries from ViewStorage
195
+ - `AnnotationContext` — Annotation queries and LLM context building
196
+ - `GraphContext` — Graph traversal and search
197
+ - `LLMContext` — Resource-level LLM context assembly
291
198
 
292
- ```typescript
293
- export type {
294
- CommentMatch,
295
- HighlightMatch,
296
- AssessmentMatch,
297
- TagMatch,
298
- } from './detection/motivation-parsers';
299
-
300
- export type { ExtractedEntity } from './detection/entity-extractor';
301
- ```
199
+ ### Generation
302
200
 
303
- ## Configuration
201
+ - `generateResourceSummary` — Resource summarization
202
+ - `generateReferenceSuggestions` — Smart suggestion generation
304
203
 
305
- All methods require an `EnvironmentConfig` object:
204
+ ### Graph
306
205
 
307
- ```typescript
308
- import type { EnvironmentConfig } from '@semiont/core';
309
-
310
- const config: EnvironmentConfig = {
311
- services: {
312
- backend: {
313
- publicURL: 'http://localhost:3000',
314
- },
315
- openai: {
316
- apiKey: process.env.OPENAI_API_KEY!,
317
- model: 'gpt-4o-mini',
318
- temperature: 0.7,
319
- },
320
- },
321
- storage: {
322
- base: '/path/to/storage',
323
- },
324
- };
325
- ```
206
+ - `GraphDBConsumer` — Event-to-graph synchronization
326
207
 
327
208
  ## Dependencies
328
209
 
329
- `@semiont/make-meaning` builds on several core packages:
330
-
331
- - **[@semiont/core](../core/)**: Core types and utilities
332
- - **[@semiont/api-client](../api-client/)**: OpenAPI-generated types
333
- - **[@semiont/event-sourcing](../event-sourcing/)**: Event store and view storage
334
- - **[@semiont/content](../content/)**: Content-addressed storage
335
- - **[@semiont/graph](../graph/)**: Neo4j graph database client
336
- - **[@semiont/ontology](../ontology/)**: Schema definitions for tags
337
- - **[@semiont/inference](../inference/)**: AI primitives (prompts, parsers, generateText)
338
- - **[@semiont/jobs](../jobs/)**: Job queue and worker base class
210
+ - **[@semiont/core](../core/)** Core types, EventBus, utilities
211
+ - **[@semiont/api-client](../api-client/)** — OpenAPI-generated types
212
+ - **[@semiont/event-sourcing](../event-sourcing/)** Event store and view storage
213
+ - **[@semiont/content](../content/)** — Content-addressed storage
214
+ - **[@semiont/graph](../graph/)** Graph database abstraction
215
+ - **[@semiont/ontology](../ontology/)** Schema definitions for tags
216
+ - **[@semiont/inference](../inference/)** AI primitives (generateText)
217
+ - **[@semiont/jobs](../jobs/)** Job queue and annotation workers
339
218
 
340
219
  ## Testing
341
220