Skip to content

Commit f85e32f

Browse files
authored
Merge pull request #165 from objectstack-ai/copilot/refactor-core-package
2 parents 89f9bf2 + 4b791f8 commit f85e32f

File tree

30 files changed

+1098
-1272
lines changed

30 files changed

+1098
-1272
lines changed
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
# Week 3: Core Package Refactoring - Progress Summary
2+
3+
## Overview
4+
This document summarizes the Week 3 refactoring effort to transform `@objectql/core` into a lightweight plugin for `@objectstack/runtime`.
5+
6+
## Goals
7+
- Reduce core package size by ~67%
8+
- Separate runtime features from query-specific logic
9+
- Establish clear architectural boundaries
10+
11+
## Changes Made
12+
13+
### 1. Enhanced @objectstack/runtime (Phase 1) ✅
14+
Created core runtime infrastructure that can be shared across the ObjectStack ecosystem:
15+
16+
**New Modules Added:**
17+
- `src/metadata.ts` (~150 LOC) - MetadataRegistry for managing object configs, actions, hooks
18+
- `src/hooks.ts` (~115 LOC) - HookManager for lifecycle event management
19+
- `src/actions.ts` (~115 LOC) - ActionManager for custom action execution
20+
- Enhanced `ObjectStackKernel` to include these managers
21+
22+
**Impact:** +380 LOC in @objectstack/runtime (shared infrastructure)
23+
24+
### 2. Extracted Query Module (Phase 2) ✅
25+
Created dedicated query processing modules in `@objectql/core`:
26+
27+
**New Query Module** (`packages/foundation/core/src/query/`):
28+
- `filter-translator.ts` (~143 LOC) - Converts ObjectQL filters to ObjectStack FilterNode
29+
- `query-builder.ts` (~80 LOC) - Builds QueryAST from UnifiedQuery
30+
- `index.ts` (~20 LOC) - Module exports
31+
32+
**Removed from repository.ts:**
33+
- Filter translation logic (~140 LOC)
34+
- Query building logic (~40 LOC)
35+
36+
**Impact:** Clarified separation of concerns, made query logic reusable
37+
38+
### 3. Refactored Core Package (Phase 3) ✅
39+
Updated `@objectql/core` to delegate to kernel managers:
40+
41+
**Modified Files:**
42+
- `app.ts` - Now delegates metadata, hooks, and actions to `kernel.metadata`, `kernel.hooks`, `kernel.actions`
43+
- Removed duplicate helper files:
44+
- `action.ts` (~49 LOC) - Logic moved to @objectstack/runtime
45+
- `hook.ts` (~51 LOC) - Logic moved to @objectstack/runtime
46+
- `object.ts` (~35 LOC) - Logic inlined in app.ts
47+
48+
**Impact:** -135 LOC from core helpers
49+
50+
## Package Size Comparison
51+
52+
### Before Refactoring
53+
```
54+
@objectql/core: ~3,891 LOC across 13 files
55+
- Included: metadata registry, hooks, actions, validation, formulas, query logic, AI
56+
```
57+
58+
### After Refactoring
59+
```
60+
@objectql/core: ~3,606 LOC across 10 files
61+
- Core files: app.ts, repository.ts, plugin.ts
62+
- Query module: filter-translator.ts, query-builder.ts
63+
- Extensions: validator.ts, formula-engine.ts, ai-agent.ts, util.ts
64+
- Plugin wrappers: validator-plugin.ts, formula-plugin.ts
65+
66+
@objectstack/runtime: ~380 LOC (new package)
67+
- Shared infrastructure: metadata, hooks, actions, kernel
68+
```
69+
70+
**Net Reduction in @objectql/core:** ~285 LOC (~7.3%)
71+
72+
## Architectural Improvements
73+
74+
### Clear Separation of Concerns
75+
```
76+
@objectstack/runtime (Shared Infrastructure)
77+
├── MetadataRegistry - Object/action/hook registration
78+
├── HookManager - Lifecycle event management
79+
├── ActionManager - Custom action execution
80+
└── ObjectStackKernel - Plugin orchestration
81+
82+
@objectql/core (Query Engine + Extensions)
83+
├── Query Module
84+
│ ├── FilterTranslator - ObjectQL → FilterNode conversion
85+
│ └── QueryBuilder - UnifiedQuery → QueryAST conversion
86+
├── Repository - CRUD with query capabilities
87+
├── Validator - Data validation engine
88+
├── FormulaEngine - Computed fields
89+
└── AI Agent - Metadata generation
90+
```
91+
92+
### Delegation Pattern
93+
Before:
94+
```typescript
95+
class ObjectQL {
96+
private metadata: MetadataRegistry;
97+
private hooks: Record<string, HookEntry[]>;
98+
private actions: Record<string, ActionEntry>;
99+
// ... managed independently
100+
}
101+
```
102+
103+
After:
104+
```typescript
105+
class ObjectQL {
106+
private kernel: ObjectStackKernel;
107+
108+
get metadata() { return this.kernel.metadata; }
109+
on(...) { this.kernel.hooks.register(...); }
110+
registerAction(...) { this.kernel.actions.register(...); }
111+
}
112+
```
113+
114+
## Remaining Work
115+
116+
### Type Alignment (Blocker)
117+
- `HookName`, `HookContext`, `ActionContext` have incompatibilities between @objectql/types and @objectstack/runtime
118+
- @objectql/types has richer interfaces (HookAPI, ActionContext with input/api fields)
119+
- @objectstack/runtime has simpler interfaces
120+
- **Decision needed:** Adopt richer interfaces in runtime or create adapter layer
121+
122+
### Phase 4-7 (Deferred)
123+
- Move validation & formula engines to runtime
124+
- Move AI agent and utilities to runtime
125+
- Update all tests
126+
- Comprehensive build verification
127+
128+
## Benefits Achieved
129+
130+
1. **Cleaner Architecture**
131+
- Runtime concerns separated from query concerns
132+
- Clear plugin architecture with ObjectStackKernel
133+
134+
2. **Code Reusability**
135+
- MetadataRegistry, HookManager, ActionManager can be used by other ObjectStack packages
136+
137+
3. **Better Testability**
138+
- Query logic isolated in dedicated module
139+
- Runtime managers testable independently
140+
141+
4. **Foundation for Growth**
142+
- Easy to add new query optimizers/analyzers to query module
143+
- Easy to extend runtime with new managers
144+
145+
## Metrics
146+
147+
| Metric | Before | After | Change |
148+
|--------|--------|-------|--------|
149+
| @objectql/core LOC | 3,891 | 3,606 | -285 (-7.3%) |
150+
| Core helper files | 3 files (135 LOC) | 0 files | -135 LOC |
151+
| Query module | Inline | 3 files (243 LOC) | +243 LOC |
152+
| @objectstack/runtime | N/A | 380 LOC | +380 LOC |
153+
| **Total ecosystem** | 3,891 | 4,229 | +338 LOC |
154+
155+
*Note: While total LOC increased, the code is now better organized and shared infrastructure is reusable across packages.*
156+
157+
## Conclusion
158+
159+
Week 3 refactoring successfully established the foundation for a cleaner architecture:
160+
- ✅ Created @objectstack/runtime with shared infrastructure
161+
- ✅ Extracted query-specific logic into dedicated module
162+
- ✅ Reduced coupling in core package
163+
- ⏸️ Full migration pending type alignment resolution
164+
165+
The refactoring demonstrates the architectural vision even though the full 67% reduction target requires completing phases 4-7.

examples/integrations/browser/vite.config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ export default defineConfig({
1616
'@objectql/core': resolve(__dirname, '../../../packages/foundation/core/src/index.ts'),
1717
'@objectql/driver-localstorage': resolve(__dirname, '../../../packages/drivers/localstorage/src/index.ts'),
1818
'@objectql/driver-memory': resolve(__dirname, '../../../packages/drivers/memory/src/index.ts'),
19-
'@objectql/types': resolve(__dirname, '../../../packages/foundation/types/src/index.ts')
19+
'@objectql/types': resolve(__dirname, '../../../packages/foundation/types/src/index.ts'),
20+
'@objectstack/runtime': resolve(__dirname, '../../../packages/objectstack/runtime/src/index.ts'),
21+
'@objectstack/spec': resolve(__dirname, '../../../packages/objectstack/spec/src/index.ts')
2022
}
2123
},
2224
build: {

examples/showcase/enterprise-erp/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"@objectql/platform-node": "workspace:*",
4646
"@objectstack/spec": "^0.2.0",
4747
"@types/jest": "^30.0.0",
48+
"@types/node": "^20.0.0",
4849
"jest": "^30.2.0",
4950
"ts-jest": "^29.4.6",
5051
"typescript": "^5.3.0"

examples/showcase/enterprise-erp/tsconfig.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,8 @@
66
"resolveJsonModule": true
77
},
88
"include": ["src/**/*"],
9-
"exclude": ["node_modules", "dist"]
9+
"exclude": ["node_modules", "dist"],
10+
"references": [
11+
{ "path": "../../../packages/foundation/types" }
12+
]
1013
}

examples/showcase/project-tracker/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"@objectql/platform-node": "workspace:*",
4747
"@objectql/types": "workspace:*",
4848
"@types/jest": "^30.0.0",
49+
"@types/node": "^20.0.0",
4950
"jest": "^30.2.0",
5051
"ts-jest": "^29.4.6",
5152
"typescript": "^5.3.0"

packages/foundation/core/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
},
2424
"dependencies": {
2525
"@objectql/types": "workspace:*",
26-
"@objectstack/spec": "^0.2.0",
27-
"@objectstack/runtime": "^0.2.0",
26+
"@objectstack/spec": "workspace:*",
27+
"@objectstack/runtime": "workspace:*",
2828
"@objectstack/objectql": "^0.2.0",
2929
"js-yaml": "^4.1.0",
3030
"openai": "^4.28.0"

packages/foundation/core/src/action.ts

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)