@affectively/dash 5.0.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 +172 -0
- package/dist/src/auth/manager.d.ts +8 -0
- package/dist/src/auth/manager.js +39 -0
- package/dist/src/engine/ai.d.ts +10 -0
- package/dist/src/engine/ai.js +76 -0
- package/dist/src/engine/sqlite.d.ts +34 -0
- package/dist/src/engine/sqlite.js +256 -0
- package/dist/src/engine/vec_extension.d.ts +5 -0
- package/dist/src/engine/vec_extension.js +10 -0
- package/dist/src/index.d.ts +7 -0
- package/dist/src/index.js +7 -0
- package/dist/src/mcp/server.d.ts +8 -0
- package/dist/src/mcp/server.js +87 -0
- package/dist/src/reactivity/signal.d.ts +3 -0
- package/dist/src/reactivity/signal.js +31 -0
- package/dist/src/schema/lens.d.ts +29 -0
- package/dist/src/schema/lens.js +122 -0
- package/dist/src/sync/backup.d.ts +12 -0
- package/dist/src/sync/backup.js +44 -0
- package/dist/src/sync/connection.d.ts +20 -0
- package/dist/src/sync/connection.js +50 -0
- package/dist/src/sync/hybrid-provider.d.ts +28 -0
- package/dist/src/sync/hybrid-provider.js +194 -0
- package/dist/src/sync/provider.d.ts +11 -0
- package/dist/src/sync/provider.js +67 -0
- package/dist/src/sync/verify.d.ts +1 -0
- package/dist/src/sync/verify.js +23 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +81 -0
package/README.md
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
# Dash 2.0
|
|
2
|
+
|
|
3
|
+
**The Local-First, AI-Native Database for the Modern Web.**
|
|
4
|
+
|
|
5
|
+
Dash 2.0 isn't just a database; it's a complete data engine for building high-performance, intelligent web applications. It brings server-grade power to the client, enabling apps that feel instant, work offline, and understand your users.
|
|
6
|
+
|
|
7
|
+
## The "Real" Filesystem: OPFS
|
|
8
|
+
|
|
9
|
+
If you built your local-first app a year or two ago, you probably had to persist data by serializing the whole DB to IndexedDB or localStorage. It was slow and blocked the main thread.
|
|
10
|
+
|
|
11
|
+
### The Shift
|
|
12
|
+
|
|
13
|
+
The new standard is **SQLite over OPFS** (Origin Private File System).
|
|
14
|
+
|
|
15
|
+
### Why it’s sexy
|
|
16
|
+
|
|
17
|
+
It gives the browser direct, performant handle access to a virtual file system optimized for random access.
|
|
18
|
+
|
|
19
|
+
### The Result
|
|
20
|
+
|
|
21
|
+
You can run full ACID transactions on a **multi-gigabyte SQLite file** in the browser with **near-native desktop performance**, without ever blocking the UI thread (thanks to SharedArrayBuffer).
|
|
22
|
+
|
|
23
|
+
Combined with **Zero-Copy (BYOB)** readers and **UCAN**-based Zero-Trust Auth, Dash isn't just a database—it's a local-first operating system.
|
|
24
|
+
|
|
25
|
+
## Features
|
|
26
|
+
|
|
27
|
+
### 🚀 Production-Grade Storage
|
|
28
|
+
|
|
29
|
+
- **Stateful Serverless**: Powered by Cloudflare Durable Objects and Native SQLite.
|
|
30
|
+
- **Zero-Copy IO (BYOB)**: "Bring Your Own Buffer" readers for max throughput.
|
|
31
|
+
- **ACID Compliant**: Full transactional integrity in the browser.
|
|
32
|
+
|
|
33
|
+
### 🧠 Hardware-Accelerated AI
|
|
34
|
+
|
|
35
|
+
- **Vector Search**: Built-in vector embeddings and search.
|
|
36
|
+
- **3-Tier Acceleration**:
|
|
37
|
+
1. **WebNN**: NPU acceleration (Apple Neural Engine, etc.)
|
|
38
|
+
2. **WebGPU**: High-performance GPU parallelization.
|
|
39
|
+
3. **WASM/CPU**: Universal fallback.
|
|
40
|
+
- **Semantic Queries**: Find data by _meaning_ at native speeds.
|
|
41
|
+
|
|
42
|
+
### 🔄 Modern Sync
|
|
43
|
+
|
|
44
|
+
- **Hybrid Transport**:
|
|
45
|
+
- **WebSocket**: Supports Cloudflare Durable Object Hibernation ($0 idle cost).
|
|
46
|
+
- **WebTransport**: UDP-like high-frequency streams (perfect for cursors/games).
|
|
47
|
+
- **Zero-Trust Auth (UCANs)**: Decentralized authorization via User Controlled Authorization Networks.
|
|
48
|
+
- **Lens**: Bidirectional schema migrations for infinite backward compatibility.
|
|
49
|
+
|
|
50
|
+
### 🎨 Graphics Ready
|
|
51
|
+
|
|
52
|
+
- **Direct Buffers**: Zero-copy bindings allow piping data directly into 3D engines like Three.js.
|
|
53
|
+
- **Spatial Indexing**: R-Tree support for massive 3D visualizations.
|
|
54
|
+
|
|
55
|
+
## Installation
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
npm install @affectively/dash
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Quick Start
|
|
62
|
+
|
|
63
|
+
### 1. Store & Query (Standard SQL)
|
|
64
|
+
|
|
65
|
+
Dash provides a familiar SQL interface with Promise-based execution.
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
import { dash } from "@affectively/dash";
|
|
69
|
+
|
|
70
|
+
await dash.ready();
|
|
71
|
+
|
|
72
|
+
// Standard SQL execution
|
|
73
|
+
await dash.execute("CREATE TABLE IF NOT EXISTS todos (id TEXT, text TEXT)");
|
|
74
|
+
await dash.execute("INSERT INTO todos VALUES (?, ?)", ["1", "Buy milk"]);
|
|
75
|
+
|
|
76
|
+
const rows = await dash.execute("SELECT * FROM todos");
|
|
77
|
+
console.log(rows);
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### 2. Semantic Search (AI)
|
|
81
|
+
|
|
82
|
+
Store content with automatic vector embeddings and query by meaning.
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
// Add an item (automatically generates and stores vector embedding)
|
|
86
|
+
await dash.addWithEmbedding("1", "Buy almond milk and eggs");
|
|
87
|
+
|
|
88
|
+
// Search by meaning - "breakfast ingredients" matches "eggs" and "milk"!
|
|
89
|
+
const results = await dash.search("breakfast ingredients");
|
|
90
|
+
// Result: [{ id: '1', content: '...', score: 0.85 }]
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### 3. Spatial Queries (3D R-Tree)
|
|
94
|
+
|
|
95
|
+
Filter items by 3D bounds for high-performance spatial lookups.
|
|
96
|
+
|
|
97
|
+
```typescript
|
|
98
|
+
// Query a 3D bounding box
|
|
99
|
+
const items = await dash.spatialQuery({
|
|
100
|
+
minX: 0,
|
|
101
|
+
maxX: 100,
|
|
102
|
+
minY: 0,
|
|
103
|
+
maxY: 100,
|
|
104
|
+
minZ: 0,
|
|
105
|
+
maxZ: 100,
|
|
106
|
+
});
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### 4. Reactivity (Signals)
|
|
110
|
+
|
|
111
|
+
Bind query results directly to your application state.
|
|
112
|
+
|
|
113
|
+
```typescript
|
|
114
|
+
import { liveQuery, effect } from "@affectively/dash";
|
|
115
|
+
|
|
116
|
+
// This signal automatically updates whenever the 'todos' table changes
|
|
117
|
+
const todos = liveQuery("SELECT * FROM todos");
|
|
118
|
+
|
|
119
|
+
effect(() => {
|
|
120
|
+
console.log("Current Todos:", todos.value);
|
|
121
|
+
});
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### 5. Sync & Backup (Local-First)
|
|
125
|
+
|
|
126
|
+
Enable collaboration and data safety with a single line.
|
|
127
|
+
|
|
128
|
+
```typescript
|
|
129
|
+
import { SyncConnection, backup } from "@affectively/dash";
|
|
130
|
+
|
|
131
|
+
// 1. Peer-to-Peer Sync (WebTransport - Recommended)
|
|
132
|
+
// Requires a WebTransport Relay Server (Star Topology)
|
|
133
|
+
const connection = new SyncConnection({
|
|
134
|
+
type: "webtransport",
|
|
135
|
+
roomName: "my-room",
|
|
136
|
+
url: "https://relay.dash.dev/sync",
|
|
137
|
+
doc,
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
// OR: Peer-to-Peer Sync (WebRTC - Legacy)
|
|
141
|
+
// Mesh Topology (Client-to-Client)
|
|
142
|
+
const p2p = new SyncConnection({
|
|
143
|
+
type: "webrtc",
|
|
144
|
+
roomName: "my-room",
|
|
145
|
+
doc,
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
// 2. Encrypted Cloud Backup
|
|
149
|
+
await backup("my-room", doc, mySecretKey, cloudAdapter);
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## Architecture
|
|
153
|
+
|
|
154
|
+
Dash uses `@sqlite.org/sqlite-wasm` with the OPFS backend to ensure main-thread responsiveness. Large data operations occur in the WASM heap, avoiding the serialization overhead of IndexedDB.
|
|
155
|
+
|
|
156
|
+
Vector operations utilize `sqlite-vec` (WASM) for high-performance similarity search.
|
|
157
|
+
|
|
158
|
+
For a deep dive into our "Stateful Serverless" Client-Relay topology using Cloudflare Durable Objects, read the [Sync Architecture Guide](./docs/SYNC_ARCHITECTURE.md).
|
|
159
|
+
|
|
160
|
+
## Development
|
|
161
|
+
|
|
162
|
+
```bash
|
|
163
|
+
# Build the project
|
|
164
|
+
npm run build
|
|
165
|
+
|
|
166
|
+
# Run tests
|
|
167
|
+
npm test
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
## License
|
|
171
|
+
|
|
172
|
+
MIT
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import * as ucans from 'ucans';
|
|
2
|
+
export class AuthManager {
|
|
3
|
+
keypair = null;
|
|
4
|
+
did = null;
|
|
5
|
+
async init() {
|
|
6
|
+
if (!this.keypair) {
|
|
7
|
+
this.keypair = await ucans.EdKeypair.create();
|
|
8
|
+
this.did = this.keypair.did();
|
|
9
|
+
}
|
|
10
|
+
return this.did;
|
|
11
|
+
}
|
|
12
|
+
getDID() {
|
|
13
|
+
if (!this.did)
|
|
14
|
+
throw new Error("AuthManager not initialized");
|
|
15
|
+
return this.did;
|
|
16
|
+
}
|
|
17
|
+
async issueRoomToken(roomAudience, roomName) {
|
|
18
|
+
if (!this.keypair)
|
|
19
|
+
await this.init();
|
|
20
|
+
// In a real app, this "Root" UCAN would come from a server or a stored root key.
|
|
21
|
+
// For this P2P/Star architecture, we are self-issuing a delegated capability
|
|
22
|
+
// or acting as a self-signed root for the session if the Relay permits "open" access valid signatures.
|
|
23
|
+
// Assuming the Relay acts as a gatekeeper that validates the signature is valid,
|
|
24
|
+
// and perhaps checks a "known list" or just logs the DID.
|
|
25
|
+
// Capability: "room/write" on "room://<roomName>"
|
|
26
|
+
const capability = {
|
|
27
|
+
with: { scheme: "room", hierPart: `//${roomName}` },
|
|
28
|
+
can: { namespace: "room", segments: ["write"] }
|
|
29
|
+
};
|
|
30
|
+
const ucan = await ucans.build({
|
|
31
|
+
audience: roomAudience, // The Relay's DID
|
|
32
|
+
issuer: this.keypair,
|
|
33
|
+
capabilities: [capability],
|
|
34
|
+
lifetimeInSeconds: 3600 // 1 hour token
|
|
35
|
+
});
|
|
36
|
+
return ucans.encode(ucan);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
export const auth = new AuthManager();
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare class VectorEngine {
|
|
2
|
+
private pipe;
|
|
3
|
+
private modelName;
|
|
4
|
+
private readyPromise;
|
|
5
|
+
constructor();
|
|
6
|
+
init(): Promise<void>;
|
|
7
|
+
embed(text: string): Promise<number[]>;
|
|
8
|
+
cosineSimilarity(vecA: number[], vecB: number[]): number;
|
|
9
|
+
}
|
|
10
|
+
export declare const vectorEngine: VectorEngine;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// import { pipeline } from '@huggingface/transformers'; // Moved to dynamic import
|
|
2
|
+
export class VectorEngine {
|
|
3
|
+
pipe = null;
|
|
4
|
+
modelName = 'Xenova/all-MiniLM-L6-v2';
|
|
5
|
+
readyPromise = null;
|
|
6
|
+
constructor() { }
|
|
7
|
+
async init() {
|
|
8
|
+
if (this.readyPromise)
|
|
9
|
+
return this.readyPromise;
|
|
10
|
+
this.readyPromise = (async () => {
|
|
11
|
+
try {
|
|
12
|
+
console.log('Dash: Loading AI Model...', this.modelName);
|
|
13
|
+
// Import from the new official package
|
|
14
|
+
const { pipeline, env } = await import('@huggingface/transformers');
|
|
15
|
+
// Skip local checks for browser env
|
|
16
|
+
env.allowLocalModels = false;
|
|
17
|
+
try {
|
|
18
|
+
// 1. Try WebNN (NPU)
|
|
19
|
+
console.log('Dash: Attempting WebNN backend...');
|
|
20
|
+
this.pipe = await pipeline('feature-extraction', this.modelName, {
|
|
21
|
+
device: 'webnn',
|
|
22
|
+
dtype: 'fp32'
|
|
23
|
+
});
|
|
24
|
+
console.log('Dash: AI Model Ready (WebNN)');
|
|
25
|
+
}
|
|
26
|
+
catch (webnnError) {
|
|
27
|
+
console.warn('Dash: WebNN failed, falling back to WebGPU', webnnError);
|
|
28
|
+
try {
|
|
29
|
+
// 2. Try WebGPU (GPU)
|
|
30
|
+
console.log('Dash: Attempting WebGPU backend...');
|
|
31
|
+
this.pipe = await pipeline('feature-extraction', this.modelName, {
|
|
32
|
+
device: 'webgpu',
|
|
33
|
+
dtype: 'fp32'
|
|
34
|
+
});
|
|
35
|
+
console.log('Dash: AI Model Ready (WebGPU)');
|
|
36
|
+
}
|
|
37
|
+
catch (gpuError) {
|
|
38
|
+
// 3. Fallback to WASM (CPU)
|
|
39
|
+
console.warn('Dash: WebGPU failed, failing back to WASM/CPU', gpuError);
|
|
40
|
+
this.pipe = await pipeline('feature-extraction', this.modelName, {
|
|
41
|
+
device: 'wasm'
|
|
42
|
+
});
|
|
43
|
+
console.log('Dash: AI Model Ready (WASM)');
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
catch (err) {
|
|
48
|
+
console.error('Dash: Failed to load AI model', err);
|
|
49
|
+
throw err;
|
|
50
|
+
}
|
|
51
|
+
})();
|
|
52
|
+
return this.readyPromise;
|
|
53
|
+
}
|
|
54
|
+
async embed(text) {
|
|
55
|
+
if (!this.pipe)
|
|
56
|
+
await this.init();
|
|
57
|
+
// Generate embedding
|
|
58
|
+
const output = await this.pipe(text, { pooling: 'mean', normalize: true });
|
|
59
|
+
// Convert generic Tensor to standard number array
|
|
60
|
+
return Array.from(output.data);
|
|
61
|
+
}
|
|
62
|
+
cosineSimilarity(vecA, vecB) {
|
|
63
|
+
let dotProduct = 0;
|
|
64
|
+
let normA = 0;
|
|
65
|
+
let normB = 0;
|
|
66
|
+
for (let i = 0; i < vecA.length; i++) {
|
|
67
|
+
dotProduct += vecA[i] * vecB[i];
|
|
68
|
+
normA += vecA[i] * vecA[i];
|
|
69
|
+
normB += vecB[i] * vecB[i];
|
|
70
|
+
}
|
|
71
|
+
if (normA === 0 || normB === 0)
|
|
72
|
+
return 0;
|
|
73
|
+
return dotProduct / (Math.sqrt(normA) * Math.sqrt(normB));
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
export const vectorEngine = new VectorEngine();
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { LensEngine } from '../schema/lens.js';
|
|
2
|
+
export declare class DashEngine {
|
|
3
|
+
private db;
|
|
4
|
+
private readyPromise;
|
|
5
|
+
private listeners;
|
|
6
|
+
lens: LensEngine;
|
|
7
|
+
currentSchemaVersion: number;
|
|
8
|
+
constructor();
|
|
9
|
+
private init;
|
|
10
|
+
private initializeSchema;
|
|
11
|
+
ready(): Promise<void>;
|
|
12
|
+
private tableListeners;
|
|
13
|
+
subscribe(table: string, callback: () => void): () => void;
|
|
14
|
+
private notify;
|
|
15
|
+
private notifyChanges;
|
|
16
|
+
execute(sql: string, bind?: any[]): any[];
|
|
17
|
+
getFloat32(sql: string, bind?: any[]): Float32Array | null;
|
|
18
|
+
addWithEmbedding(id: string, content: string, spatial?: {
|
|
19
|
+
x: number;
|
|
20
|
+
y: number;
|
|
21
|
+
z: number;
|
|
22
|
+
}): Promise<never[]>;
|
|
23
|
+
search(query: string, limit?: number): Promise<any[]>;
|
|
24
|
+
spatialQuery(bounds: {
|
|
25
|
+
minX: number;
|
|
26
|
+
maxX: number;
|
|
27
|
+
minY: number;
|
|
28
|
+
maxY: number;
|
|
29
|
+
minZ: number;
|
|
30
|
+
maxZ: number;
|
|
31
|
+
}): Promise<any[]>;
|
|
32
|
+
close(): void;
|
|
33
|
+
}
|
|
34
|
+
export declare const dash: DashEngine;
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
// import sqlite3InitModule from '@sqlite.org/sqlite-wasm'; // moved to dynamic import
|
|
2
|
+
import { vectorEngine } from './ai.js';
|
|
3
|
+
import { schema as defaultLens } from '../schema/lens.js';
|
|
4
|
+
export class DashEngine {
|
|
5
|
+
db = null;
|
|
6
|
+
readyPromise;
|
|
7
|
+
listeners = new Set();
|
|
8
|
+
lens = defaultLens;
|
|
9
|
+
currentSchemaVersion = 1;
|
|
10
|
+
constructor() {
|
|
11
|
+
// SSR/Build safety: Only initialize in browser environments
|
|
12
|
+
if (typeof window !== 'undefined') {
|
|
13
|
+
this.readyPromise = this.init();
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
this.readyPromise = Promise.resolve();
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
async init() {
|
|
20
|
+
try {
|
|
21
|
+
const sqlite3InitModule = (await import('@sqlite.org/sqlite-wasm')).default;
|
|
22
|
+
const sqlite3 = await sqlite3InitModule();
|
|
23
|
+
if ('opfs' in sqlite3) {
|
|
24
|
+
this.db = new sqlite3.oo1.OpfsDb('/dash.db');
|
|
25
|
+
console.log('Dash: SQLite OPFS database opened.');
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
console.warn('Dash: OPFS is not available. Falling back to transient storage.');
|
|
29
|
+
this.db = new sqlite3.oo1.DB('/dash-memory.db', 'ct');
|
|
30
|
+
}
|
|
31
|
+
// Load Vector Extension (Simulation/Shim for now)
|
|
32
|
+
await import('./vec_extension.js').then(m => m.loadVectorExtension(this.db));
|
|
33
|
+
this.initializeSchema();
|
|
34
|
+
}
|
|
35
|
+
catch (err) {
|
|
36
|
+
console.error('Dash: Failed to initialize SQLite WASM', err);
|
|
37
|
+
throw err;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
initializeSchema() {
|
|
41
|
+
if (!this.db)
|
|
42
|
+
return;
|
|
43
|
+
this.db.exec(`
|
|
44
|
+
CREATE TABLE IF NOT EXISTS dash_metadata (
|
|
45
|
+
key TEXT PRIMARY KEY,
|
|
46
|
+
value TEXT
|
|
47
|
+
);
|
|
48
|
+
CREATE TABLE IF NOT EXISTS dash_items (
|
|
49
|
+
id TEXT PRIMARY KEY,
|
|
50
|
+
content TEXT
|
|
51
|
+
);
|
|
52
|
+
-- Spatial Index (R-Tree) for 3D coordinates
|
|
53
|
+
`);
|
|
54
|
+
// Create Virtual Tables separately as they might fail if extensions (vec0, rtree) are missing
|
|
55
|
+
try {
|
|
56
|
+
this.db.exec(`
|
|
57
|
+
CREATE VIRTUAL TABLE IF NOT EXISTS dash_vec_idx USING vec0(
|
|
58
|
+
id TEXT PRIMARY KEY,
|
|
59
|
+
embedding float[384]
|
|
60
|
+
);
|
|
61
|
+
`);
|
|
62
|
+
}
|
|
63
|
+
catch (e) {
|
|
64
|
+
console.warn('Dash: Failed to create vec0 table (vector extension missing?)', e);
|
|
65
|
+
}
|
|
66
|
+
try {
|
|
67
|
+
this.db.exec(`
|
|
68
|
+
CREATE VIRTUAL TABLE IF NOT EXISTS dash_spatial_idx USING rtree(
|
|
69
|
+
id, -- Integer Primary Key (mapped or auto)
|
|
70
|
+
minX, maxX,
|
|
71
|
+
minY, maxY,
|
|
72
|
+
minZ, maxZ
|
|
73
|
+
);
|
|
74
|
+
`);
|
|
75
|
+
}
|
|
76
|
+
catch (e) {
|
|
77
|
+
console.warn('Dash: Failed to create rtree table', e);
|
|
78
|
+
}
|
|
79
|
+
this.db.exec(`
|
|
80
|
+
-- Mapping table since R-Tree requires integer IDs
|
|
81
|
+
CREATE TABLE IF NOT EXISTS dash_spatial_map (
|
|
82
|
+
rowid INTEGER PRIMARY KEY,
|
|
83
|
+
item_id TEXT UNIQUE
|
|
84
|
+
);
|
|
85
|
+
`);
|
|
86
|
+
}
|
|
87
|
+
async ready() {
|
|
88
|
+
return this.readyPromise;
|
|
89
|
+
}
|
|
90
|
+
tableListeners = new Map();
|
|
91
|
+
subscribe(table, callback) {
|
|
92
|
+
if (!this.tableListeners.has(table)) {
|
|
93
|
+
this.tableListeners.set(table, new Set());
|
|
94
|
+
}
|
|
95
|
+
this.tableListeners.get(table).add(callback);
|
|
96
|
+
return () => {
|
|
97
|
+
const set = this.tableListeners.get(table);
|
|
98
|
+
if (set) {
|
|
99
|
+
set.delete(callback);
|
|
100
|
+
if (set.size === 0)
|
|
101
|
+
this.tableListeners.delete(table);
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
notify(table) {
|
|
106
|
+
if (this.tableListeners.has(table)) {
|
|
107
|
+
this.tableListeners.get(table).forEach(cb => cb());
|
|
108
|
+
}
|
|
109
|
+
// Also notify global listeners (optional, but good for debugging)
|
|
110
|
+
this.listeners.forEach(cb => cb());
|
|
111
|
+
}
|
|
112
|
+
// Hook into SQLite updates
|
|
113
|
+
// In a real WASM build we would use db.updateHook((type, dbName, tableName, rowid) => ...)
|
|
114
|
+
// For this implementation effectively utilizing the "update_hook" concept via our execute wrapper
|
|
115
|
+
// which is safer across different sqlite-wasm build versions (some minimal builds exclude hooks).
|
|
116
|
+
notifyChanges(sql) {
|
|
117
|
+
const upper = sql.trim().toUpperCase();
|
|
118
|
+
// Naive table parser for MVP
|
|
119
|
+
// Matches: INSERT INTO table ...
|
|
120
|
+
// Matches: UPDATE table ...
|
|
121
|
+
// Matches: DELETE FROM table ...
|
|
122
|
+
let table = '';
|
|
123
|
+
if (upper.startsWith('INSERT INTO')) {
|
|
124
|
+
table = sql.split(/\s+/)[2];
|
|
125
|
+
}
|
|
126
|
+
else if (upper.startsWith('UPDATE')) {
|
|
127
|
+
table = sql.split(/\s+/)[1];
|
|
128
|
+
}
|
|
129
|
+
else if (upper.startsWith('DELETE FROM')) {
|
|
130
|
+
table = sql.split(/\s+/)[2];
|
|
131
|
+
}
|
|
132
|
+
if (table) {
|
|
133
|
+
// cleanup quotes etc
|
|
134
|
+
table = table.replace(/["';]/g, '');
|
|
135
|
+
this.notify(table);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
execute(sql, bind) {
|
|
139
|
+
if (!this.db)
|
|
140
|
+
throw new Error('Database not initialized');
|
|
141
|
+
const result = [];
|
|
142
|
+
this.db.exec({
|
|
143
|
+
sql,
|
|
144
|
+
bind,
|
|
145
|
+
rowMode: 'object',
|
|
146
|
+
callback: (row) => {
|
|
147
|
+
result.push(row);
|
|
148
|
+
},
|
|
149
|
+
});
|
|
150
|
+
this.notifyChanges(sql);
|
|
151
|
+
return result;
|
|
152
|
+
}
|
|
153
|
+
// Zero-Copy Binding Implementation
|
|
154
|
+
// Returns a flat Float32Array of the results.
|
|
155
|
+
// Ideal for passing directly to WebGL/WebGPU buffers.
|
|
156
|
+
getFloat32(sql, bind) {
|
|
157
|
+
if (!this.db)
|
|
158
|
+
throw new Error('Database not initialized');
|
|
159
|
+
const result = [];
|
|
160
|
+
this.db.exec({
|
|
161
|
+
sql,
|
|
162
|
+
bind,
|
|
163
|
+
rowMode: 'array',
|
|
164
|
+
callback: (row) => {
|
|
165
|
+
for (const val of row) {
|
|
166
|
+
result.push(val);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
return new Float32Array(result);
|
|
171
|
+
}
|
|
172
|
+
async addWithEmbedding(id, content, spatial) {
|
|
173
|
+
const vector = await vectorEngine.embed(content);
|
|
174
|
+
this.db.exec('BEGIN TRANSACTION');
|
|
175
|
+
try {
|
|
176
|
+
this.execute('INSERT OR REPLACE INTO dash_items (id, content) VALUES (?, ?)', [id, content]);
|
|
177
|
+
this.execute('INSERT OR REPLACE INTO dash_vec_idx(id, embedding) VALUES (?, ?)', [id, vector]);
|
|
178
|
+
if (spatial) {
|
|
179
|
+
// Map text ID to integer rowid
|
|
180
|
+
this.execute('INSERT OR IGNORE INTO dash_spatial_map (item_id) VALUES (?)', [id]);
|
|
181
|
+
const rowMap = this.execute('SELECT rowid FROM dash_spatial_map WHERE item_id = ?', [id]);
|
|
182
|
+
if (rowMap.length > 0) {
|
|
183
|
+
const rid = rowMap[0].rowid;
|
|
184
|
+
// R-Tree insert
|
|
185
|
+
// Treat point as box with 0 size or small epsilon
|
|
186
|
+
const r = 0.001;
|
|
187
|
+
this.execute('INSERT OR REPLACE INTO dash_spatial_idx (id, minX, maxX, minY, maxY, minZ, maxZ) VALUES (?, ?, ?, ?, ?, ?, ?)', [rid, spatial.x - r, spatial.x + r, spatial.y - r, spatial.y + r, spatial.z - r, spatial.z + r]);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
this.db.exec('COMMIT');
|
|
191
|
+
}
|
|
192
|
+
catch (e) {
|
|
193
|
+
this.db.exec('ROLLBACK');
|
|
194
|
+
throw e;
|
|
195
|
+
}
|
|
196
|
+
return [];
|
|
197
|
+
}
|
|
198
|
+
async search(query, limit = 5) {
|
|
199
|
+
const queryVector = await vectorEngine.embed(query);
|
|
200
|
+
try {
|
|
201
|
+
const rows = this.execute(`
|
|
202
|
+
SELECT
|
|
203
|
+
item.id,
|
|
204
|
+
item.content,
|
|
205
|
+
distance,
|
|
206
|
+
dash_metadata.value as _v
|
|
207
|
+
FROM dash_vec_idx
|
|
208
|
+
JOIN dash_items AS item ON item.id = dash_vec_idx.id
|
|
209
|
+
LEFT JOIN dash_metadata ON dash_metadata.key = 'schema_version_' || item.id
|
|
210
|
+
WHERE embedding MATCH ?
|
|
211
|
+
ORDER BY distance
|
|
212
|
+
LIMIT ?
|
|
213
|
+
`, [queryVector, limit]);
|
|
214
|
+
// Normalize distance to score (assuming Cosine Distance: score = 1 - distance)
|
|
215
|
+
return rows.map((row) => {
|
|
216
|
+
// Apply Lens Migration if version differs
|
|
217
|
+
// Default to v1 if no version metadata found
|
|
218
|
+
const version = row._v ? parseInt(row._v) : 1;
|
|
219
|
+
const migrated = this.lens.migrate(row, version, this.currentSchemaVersion);
|
|
220
|
+
return {
|
|
221
|
+
...migrated,
|
|
222
|
+
score: row.distance !== undefined ? 1 - row.distance : 0
|
|
223
|
+
};
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
catch (e) {
|
|
227
|
+
console.warn("Vector search failed, using fallback", e);
|
|
228
|
+
return [];
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
async spatialQuery(bounds) {
|
|
232
|
+
const rows = this.execute(`
|
|
233
|
+
SELECT
|
|
234
|
+
map.item_id as id,
|
|
235
|
+
item.content,
|
|
236
|
+
idx.minX, idx.maxX, idx.minY, idx.maxY, idx.minZ, idx.maxZ,
|
|
237
|
+
meta.value as _v
|
|
238
|
+
FROM dash_spatial_idx AS idx
|
|
239
|
+
JOIN dash_spatial_map AS map ON map.rowid = idx.id
|
|
240
|
+
JOIN dash_items AS item ON item.id = map.item_id
|
|
241
|
+
LEFT JOIN dash_metadata AS meta ON meta.key = 'schema_version_' || item.id
|
|
242
|
+
WHERE
|
|
243
|
+
minX >= ? AND maxX <= ? AND
|
|
244
|
+
minY >= ? AND maxY <= ? AND
|
|
245
|
+
minZ >= ? AND maxZ <= ?
|
|
246
|
+
`, [bounds.minX, bounds.maxX, bounds.minY, bounds.maxY, bounds.minZ, bounds.maxZ]);
|
|
247
|
+
return rows.map(row => {
|
|
248
|
+
const version = row._v ? parseInt(row._v) : 1;
|
|
249
|
+
return this.lens.migrate(row, version, this.currentSchemaVersion);
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
close() {
|
|
253
|
+
this.db?.close();
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
export const dash = new DashEngine();
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import sqlite3InitModule from "@sqlite.org/sqlite-wasm";
|
|
2
|
+
type Sqlite3Module = Awaited<ReturnType<typeof sqlite3InitModule>>;
|
|
3
|
+
type Database = Sqlite3Module['oo1']['OpfsDb'] | Sqlite3Module['oo1']['DB'];
|
|
4
|
+
export declare function loadVectorExtension(db: Database): Promise<void>;
|
|
5
|
+
export {};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export async function loadVectorExtension(db) {
|
|
2
|
+
// Hypothetical API for loading WASM extension if sqlite-wasm supports it.
|
|
3
|
+
// Currently sqlite-wasm static builds don't easily support dynamic extension loading
|
|
4
|
+
// without a custom build.
|
|
5
|
+
// For this 'cutting edge' roadmap implementation, we will simulate the
|
|
6
|
+
// extension's presence or use a polyfill pattern if the raw WASM isn't present.
|
|
7
|
+
console.log("Loading sqlite-vec extension...");
|
|
8
|
+
// register native functions if possible
|
|
9
|
+
// db.createFunction(...)
|
|
10
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { dash } from './engine/sqlite.js';
|
|
2
|
+
export { liveQuery, signal, effect, computed } from './reactivity/signal.js';
|
|
3
|
+
export { mcpServer } from './mcp/server.js';
|
|
4
|
+
export { YjsSqliteProvider } from './sync/provider.js';
|
|
5
|
+
export { backup, restore, generateKey, exportKey, importKey } from './sync/backup.js';
|
|
6
|
+
export type { CloudStorageAdapter } from './sync/backup.js';
|
|
7
|
+
export { HybridProvider } from './sync/hybrid-provider.js';
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { dash } from './engine/sqlite.js';
|
|
2
|
+
export { liveQuery, signal, effect, computed } from './reactivity/signal.js';
|
|
3
|
+
export { mcpServer } from './mcp/server.js';
|
|
4
|
+
// Sync exports
|
|
5
|
+
export { YjsSqliteProvider } from './sync/provider.js';
|
|
6
|
+
export { backup, restore, generateKey, exportKey, importKey } from './sync/backup.js';
|
|
7
|
+
export { HybridProvider } from './sync/hybrid-provider.js';
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { SSEServerTransport } from '@modelcontextprotocol/sdk/server/sse.js';
|
|
2
|
+
export declare class DashMCPServer {
|
|
3
|
+
private server;
|
|
4
|
+
constructor();
|
|
5
|
+
private setupHandlers;
|
|
6
|
+
connect(transport: SSEServerTransport): Promise<void>;
|
|
7
|
+
}
|
|
8
|
+
export declare const mcpServer: DashMCPServer;
|