@positronic/core 0.0.3 → 0.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/package.json +5 -1
- package/CLAUDE.md +0 -141
- package/dist/src/dsl/brain-runner.test.js +0 -733
- package/dist/src/dsl/brain.test.js +0 -4225
- package/dist/src/test-utils.js +0 -474
- package/dist/src/testing.js +0 -3
- package/dist/types/test-utils.d.ts +0 -94
- package/dist/types/test-utils.d.ts.map +0 -1
- package/dist/types/testing.d.ts +0 -2
- package/dist/types/testing.d.ts.map +0 -1
- package/docs/core-testing-guide.md +0 -289
- package/src/adapters/types.ts +0 -5
- package/src/clients/types.ts +0 -54
- package/src/dsl/brain-runner.test.ts +0 -384
- package/src/dsl/brain-runner.ts +0 -111
- package/src/dsl/brain.test.ts +0 -1981
- package/src/dsl/brain.ts +0 -740
- package/src/dsl/constants.ts +0 -16
- package/src/dsl/json-patch.ts +0 -42
- package/src/dsl/types.ts +0 -13
- package/src/index.ts +0 -36
- package/src/resources/resource-loader.ts +0 -8
- package/src/resources/resources.ts +0 -267
- package/src/test-utils.ts +0 -254
- package/test/resources.test.ts +0 -248
- package/tsconfig.json +0 -10
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@positronic/core",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -9,6 +9,10 @@
|
|
|
9
9
|
"main": "dist/src/index.js",
|
|
10
10
|
"types": "dist/types/index.d.ts",
|
|
11
11
|
"license": "MIT",
|
|
12
|
+
"files": [
|
|
13
|
+
"dist",
|
|
14
|
+
"package.json"
|
|
15
|
+
],
|
|
12
16
|
"exports": {
|
|
13
17
|
".": {
|
|
14
18
|
"types": "./dist/types/index.d.ts",
|
package/CLAUDE.md
DELETED
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
# CLAUDE.md
|
|
2
|
-
|
|
3
|
-
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
|
4
|
-
|
|
5
|
-
## Package Overview
|
|
6
|
-
|
|
7
|
-
This is the `@positronic/core` package - the core framework for building AI-powered "brains" with stateful workflows. It provides:
|
|
8
|
-
|
|
9
|
-
- A fluent DSL for defining AI workflows (Brain DSL)
|
|
10
|
-
- Event-driven state management using JSON patches
|
|
11
|
-
- Resource management system with type-safe access
|
|
12
|
-
- Integration with AI clients for structured object generation
|
|
13
|
-
- Adapter pattern for extensibility
|
|
14
|
-
|
|
15
|
-
## Key Commands
|
|
16
|
-
|
|
17
|
-
### Development
|
|
18
|
-
|
|
19
|
-
- `npm test` - Run all tests silently
|
|
20
|
-
- `npm run build` - Build the package (runs TypeScript compilation then SWC)
|
|
21
|
-
- `npm run clean` - Clean build artifacts and dependencies
|
|
22
|
-
- `npm run tsc` - Run TypeScript compiler only (generates type declarations)
|
|
23
|
-
- `npm run swc` - Run SWC transpilation only
|
|
24
|
-
|
|
25
|
-
### Testing
|
|
26
|
-
|
|
27
|
-
Tests use Jest and are located alongside source files and must be run from the monorepo root (`*.test.ts`):
|
|
28
|
-
|
|
29
|
-
- Run from monorepo root: `npm test -- packages/core`
|
|
30
|
-
- Run specific test: `npm test -- brain.test.ts`
|
|
31
|
-
- Run with pattern: `npm test -- -t "should create a brain"`
|
|
32
|
-
|
|
33
|
-
## Core Architecture
|
|
34
|
-
|
|
35
|
-
### Brain DSL (`src/dsl/brain.ts`)
|
|
36
|
-
|
|
37
|
-
**Implementation Details:**
|
|
38
|
-
|
|
39
|
-
- **Block System**: The DSL builds an internal array of `Block` objects (union type of `StepBlock` and `BrainBlock`)
|
|
40
|
-
- **Type Chain**: Each method returns `this.nextBrain<TNewState>()` which creates a new Brain instance with updated state type
|
|
41
|
-
- **State Type Inference**: Uses TypeScript generics to thread state types through the chain:
|
|
42
|
-
```typescript
|
|
43
|
-
class Brain<TOptions, TState, TServices> {
|
|
44
|
-
step<TNewState>(...): Brain<TOptions, TNewState, TServices>
|
|
45
|
-
}
|
|
46
|
-
```
|
|
47
|
-
- **Brain Name Uniqueness**: Enforced at runtime via global `brainNames` Set (disabled in tests)
|
|
48
|
-
- **Services Pattern**: Services are stored in a private field and passed through the chain via `nextBrain()`
|
|
49
|
-
- **Default Options**: Merged with runtime options in the `run()` method
|
|
50
|
-
- **Prompt Step**: Special step type that wraps `generateObject` call and merges response into state
|
|
51
|
-
|
|
52
|
-
### Event System
|
|
53
|
-
|
|
54
|
-
**Implementation Details:**
|
|
55
|
-
|
|
56
|
-
- **Event Types**: Defined as const object `BRAIN_EVENTS` in `constants.ts`
|
|
57
|
-
- **Event Hierarchy**: Base `BaseEvent` → `BrainBaseEvent` (includes title/description) → specific event types
|
|
58
|
-
- **BrainEventStream**: Internal class that manages event generation during execution
|
|
59
|
-
- **Step Execution**: Generates events in sequence: STEP_STATUS → STEP_START → (execution) → STEP_COMPLETE → STEP_STATUS
|
|
60
|
-
- **Error Handling**: Errors emit ERROR event but execution continues to COMPLETE
|
|
61
|
-
- **Nested Brain Events**: Inner brain events are yielded directly, maintaining full event stream
|
|
62
|
-
|
|
63
|
-
### State Management
|
|
64
|
-
|
|
65
|
-
**Implementation Details:**
|
|
66
|
-
|
|
67
|
-
- **JSON Patch Library**: Uses `fast-json-patch` for RFC 6902 operations
|
|
68
|
-
- **Patch Creation**: `createPatch()` filters out non-standard operations to ensure compatibility
|
|
69
|
-
- **Patch Application**: `applyPatches()` handles both single patches and arrays of patches
|
|
70
|
-
- **State Cloning**: Uses `structuredClone()` for deep copying state objects
|
|
71
|
-
- **Patch Storage**: Each `Step` object stores its patch after execution
|
|
72
|
-
- **State Reconstruction**: BrainRunner applies patches sequentially to rebuild state
|
|
73
|
-
|
|
74
|
-
### Resources System (`src/resources/`)
|
|
75
|
-
|
|
76
|
-
**Implementation Details:**
|
|
77
|
-
|
|
78
|
-
- **Proxy Magic**: Uses ES6 Proxy to create dynamic property access from manifest
|
|
79
|
-
- **Path Resolution**: `findResourceByPath()` handles both exact matches and extension-optional lookup
|
|
80
|
-
- **Ambiguity Handling**: Throws helpful errors when multiple files match without extension
|
|
81
|
-
- **Lazy Loading**: Resources are not loaded until `load()`, `loadText()`, or `loadBinary()` is called
|
|
82
|
-
- **Type Checking**: Runtime validation ensures correct method usage (e.g., `loadText()` on binary resources throws)
|
|
83
|
-
- **Recursive Proxies**: Nested manifests create nested proxy objects for directory-like access
|
|
84
|
-
- **Special Methods**: `loadText(path)` and `loadBinary(path)` are available at every level for direct path access
|
|
85
|
-
|
|
86
|
-
### Client Integration (`src/clients/types.ts`)
|
|
87
|
-
|
|
88
|
-
**Implementation Details:**
|
|
89
|
-
|
|
90
|
-
- **Interface Design**: Single method interface for easy mocking and implementation
|
|
91
|
-
- **Parameter Handling**: Implementations should merge `prompt` with `messages` array
|
|
92
|
-
- **Schema Usage**: `schemaName` and `schemaDescription` are for client-specific features (e.g., Claude's tool use)
|
|
93
|
-
- **Type Safety**: Return type uses `z.infer<T>` for compile-time type inference
|
|
94
|
-
|
|
95
|
-
### BrainRunner (`src/dsl/brain-runner.ts`)
|
|
96
|
-
|
|
97
|
-
**Implementation Details:**
|
|
98
|
-
|
|
99
|
-
- **Immutable Configuration**: Each `with*` method returns a new BrainRunner instance
|
|
100
|
-
- **Event Dispatch**: Uses `Promise.all()` to dispatch events to adapters concurrently
|
|
101
|
-
- **State Tracking**: Maintains `currentState` and applies patches as steps complete
|
|
102
|
-
- **Step Counting**: Tracks step number for `endAfter` early termination
|
|
103
|
-
- **Restart Logic**: Applies patches from `initialCompletedSteps` before execution begins
|
|
104
|
-
- **Type Parameters**: Properly threads through brain's generic types for type safety
|
|
105
|
-
- **Return Value**: Returns final state after all events processed (or early termination)
|
|
106
|
-
|
|
107
|
-
### Adapter Pattern (`src/adapters/types.ts`)
|
|
108
|
-
|
|
109
|
-
**Implementation Details:**
|
|
110
|
-
|
|
111
|
-
- **Simple Interface**: Single `dispatch(event)` method for maximum flexibility
|
|
112
|
-
- **Async Support**: Can return `void` or `Promise<void>` for async operations
|
|
113
|
-
- **Generic Options**: `Adapter<Options>` allows type-safe event options
|
|
114
|
-
- **Event Handling**: Adapters receive ALL events - must filter if only interested in specific types
|
|
115
|
-
- **Error Handling**: BrainRunner doesn't catch adapter errors - adapters must handle their own errors
|
|
116
|
-
|
|
117
|
-
## Testing
|
|
118
|
-
|
|
119
|
-
See the comprehensive testing guide: @docs/core-testing-guide.md
|
|
120
|
-
|
|
121
|
-
## Type System Implementation
|
|
122
|
-
|
|
123
|
-
- **Module System**: Pure ESM with `.js` extensions in imports (even for `.ts` files)
|
|
124
|
-
- **Type Exports**: Separate type declarations in `dist/types` via `tsc`
|
|
125
|
-
- **Generic Threading**: Brain class uses 3 generic parameters: `<TOptions, TState, TServices>`
|
|
126
|
-
- **Type Inference Chain**: Each step method returns `Brain<TOptions, TNewState, TServices>`
|
|
127
|
-
- **Zod Integration**: Exported as peer dependency to avoid version conflicts
|
|
128
|
-
- **Type Guards**: `isResourceEntry()` for discriminating union types in resources
|
|
129
|
-
|
|
130
|
-
## Critical Implementation Details
|
|
131
|
-
|
|
132
|
-
- **Immutable State**: Steps MUST return new objects - mutating state breaks patch generation
|
|
133
|
-
- **Brain Name Registry**: Global `brainNames` Set prevents duplicates (disabled via `NODE_ENV=test`)
|
|
134
|
-
- **Lazy Resource Loading**: `ResourceLoader.load()` only called when `loadText()`/`loadBinary()` invoked
|
|
135
|
-
- **Event Correlation**: All events include `brainRunId` (generated via `uuid.v4()` if not provided)
|
|
136
|
-
- **Patch Optimization**: `createPatch()` produces minimal diffs - empty patches for identical states
|
|
137
|
-
- **Options Merging**: Runtime options override default options set via `.withOptions()`
|
|
138
|
-
- **Services Timing**: `.withServices()` must be called before steps - services stored in private field
|
|
139
|
-
- **Step ID Generation**: Each step gets UUID via `Step` constructor for tracking
|
|
140
|
-
- **Error Serialization**: Errors converted to `SerializedError` with name/message/stack
|
|
141
|
-
- **Clone Strategy**: Uses `structuredClone()` for deep state copies (not JSON parse/stringify)
|