@mikesaintsg/core 0.0.2 → 0.0.4
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 +186 -41
- package/dist/index.d.ts +383 -124
- package/dist/index.js +98 -107
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -13,7 +13,9 @@
|
|
|
13
13
|
- ✅ **Shared Types** — Contracts used across multiple packages
|
|
14
14
|
- ✅ **Result Pattern** — Functional error handling (`ok`, `err`, `map`, `chain`)
|
|
15
15
|
- ✅ **Bridge Functions** — Connect packages without circular dependencies
|
|
16
|
-
- ✅ **
|
|
16
|
+
- ✅ **Adapter Interfaces** — Embedding, tool format, persistence, and policy adapters
|
|
17
|
+
- ✅ **Base Error Class** — `EcosystemError` for all package-specific errors
|
|
18
|
+
- ✅ **Content Hashing** — SHA-256 based deduplication support
|
|
17
19
|
- ✅ **Zero dependencies** — Built on native platform APIs
|
|
18
20
|
- ✅ **TypeScript first** — Full type safety with generics
|
|
19
21
|
- ✅ **Tree-shakeable** — ESM-only, import what you need
|
|
@@ -30,6 +32,8 @@ npm install @mikesaintsg/core
|
|
|
30
32
|
|
|
31
33
|
## Quick Start
|
|
32
34
|
|
|
35
|
+
### Result Pattern
|
|
36
|
+
|
|
33
37
|
```ts
|
|
34
38
|
import { ok, err, isOk, map, chain } from '@mikesaintsg/core'
|
|
35
39
|
import type { Result } from '@mikesaintsg/core'
|
|
@@ -55,6 +59,33 @@ function parse(s: string): Result<number, string> {
|
|
|
55
59
|
const result = chain(ok('42'), parse) // ok(42)
|
|
56
60
|
```
|
|
57
61
|
|
|
62
|
+
### Content Hashing
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
import { computeContentHash } from '@mikesaintsg/core'
|
|
66
|
+
|
|
67
|
+
const hash = await computeContentHash('Hello, world!')
|
|
68
|
+
// 'a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e'
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Tool Call Bridge
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
import { createToolCallBridge } from '@mikesaintsg/core'
|
|
75
|
+
|
|
76
|
+
const bridge = createToolCallBridge({
|
|
77
|
+
registry,
|
|
78
|
+
timeout: 30000,
|
|
79
|
+
onError: (error, toolCall) => console.error(error),
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
// Execute single call
|
|
83
|
+
const result = await bridge.execute(toolCall)
|
|
84
|
+
|
|
85
|
+
// Execute multiple calls in parallel
|
|
86
|
+
const results = await bridge.execute([call1, call2, call3])
|
|
87
|
+
```
|
|
88
|
+
|
|
58
89
|
---
|
|
59
90
|
|
|
60
91
|
## Documentation
|
|
@@ -64,7 +95,6 @@ const result = chain(ok('42'), parse) // ok(42)
|
|
|
64
95
|
### Key Sections
|
|
65
96
|
|
|
66
97
|
- [Introduction](./guides/core.md#introduction) — Value proposition and use cases
|
|
67
|
-
- [Quick Start](./guides/core.md#quick-start) — Get started in minutes
|
|
68
98
|
- [Core Concepts](./guides/core.md#core-concepts) — Understand the fundamentals
|
|
69
99
|
- [Result Pattern](./guides/core.md#result-pattern) — Functional error handling
|
|
70
100
|
- [Bridge Interfaces](./guides/core.md#bridge-interfaces) — Cross-package communication
|
|
@@ -76,17 +106,33 @@ const result = chain(ok('42'), parse) // ok(42)
|
|
|
76
106
|
|
|
77
107
|
### Result Functions
|
|
78
108
|
|
|
79
|
-
| Function
|
|
80
|
-
|
|
81
|
-
| `ok(value)`
|
|
82
|
-
| `err(error)`
|
|
83
|
-
| `isOk(result)`
|
|
84
|
-
| `isErr(result)`
|
|
85
|
-
| `unwrap(result, default)`
|
|
86
|
-
| `
|
|
87
|
-
| `
|
|
109
|
+
| Function | Description |
|
|
110
|
+
|------------------------------|------------------------------|
|
|
111
|
+
| `ok(value)` | Create a success result |
|
|
112
|
+
| `err(error)` | Create a failure result |
|
|
113
|
+
| `isOk(result)` | Check if result is success |
|
|
114
|
+
| `isErr(result)` | Check if result is failure |
|
|
115
|
+
| `unwrap(result, default)` | Get value or default |
|
|
116
|
+
| `unwrapOrThrow(result)` | Get value or throw error |
|
|
117
|
+
| `map(result, fn)` | Transform success value |
|
|
118
|
+
| `mapErr(result, fn)` | Transform error value |
|
|
119
|
+
| `chain(result, fn)` | Chain results (flatMap) |
|
|
120
|
+
|
|
121
|
+
### Content Hashing
|
|
88
122
|
|
|
89
|
-
|
|
123
|
+
| Function | Description |
|
|
124
|
+
|------------------------------|------------------------------|
|
|
125
|
+
| `computeContentHash(text)` | SHA-256 hash for text |
|
|
126
|
+
|
|
127
|
+
### Bridge Helper Functions
|
|
128
|
+
|
|
129
|
+
| Function | Description |
|
|
130
|
+
|------------------------------|-----------------------------------|
|
|
131
|
+
| `isToolCall(value)` | Type guard for ToolCall objects |
|
|
132
|
+
| `shouldSkipFormGuard(...)` | Check if form guard should skip |
|
|
133
|
+
| `formatScoredResult(result)` | Default result formatter |
|
|
134
|
+
|
|
135
|
+
### Factory Functions
|
|
90
136
|
|
|
91
137
|
| Function | Description |
|
|
92
138
|
|-------------------------------------|----------------------------------|
|
|
@@ -95,15 +141,57 @@ const result = chain(ok('42'), parse) // ok(42)
|
|
|
95
141
|
| `createFormDirtyGuard(options)` | Navigation guard for dirty forms |
|
|
96
142
|
| `createSessionPersistence(options)` | Session storage adapter |
|
|
97
143
|
|
|
144
|
+
### Error Handling
|
|
145
|
+
|
|
146
|
+
| Export | Description |
|
|
147
|
+
|------------------------|------------------------------------|
|
|
148
|
+
| `EcosystemError` | Base error class for all packages |
|
|
149
|
+
| `isEcosystemError(e)` | Type guard for ecosystem errors |
|
|
150
|
+
|
|
98
151
|
### Shared Types
|
|
99
152
|
|
|
100
|
-
| Type
|
|
101
|
-
|
|
102
|
-
| `Result<T, E>`
|
|
103
|
-
| `
|
|
104
|
-
| `
|
|
105
|
-
| `
|
|
106
|
-
| `
|
|
153
|
+
| Type | Description |
|
|
154
|
+
|-------------------------------------------|---------------------------------------|
|
|
155
|
+
| `Result<T, E>` | Success or failure union |
|
|
156
|
+
| `Ok<T>`, `Err<E>` | Result discriminants |
|
|
157
|
+
| `Unsubscribe` | Cleanup function type |
|
|
158
|
+
| `DestroyFn` | Resource cleanup function |
|
|
159
|
+
| `SubscriptionToHook<T>` | Convert subscriptions to hooks |
|
|
160
|
+
| `Embedding` | Float32Array embedding vector |
|
|
161
|
+
| `ToolCall`, `ToolResult`, `ToolSchema` | Tool-related types |
|
|
162
|
+
| `ScoredResult` | Search result with score |
|
|
163
|
+
| `ContextFrame`, `BuiltContext` | Context management types |
|
|
164
|
+
| `TokenBudgetState`, `TokenBudgetLevel` | Token budget types |
|
|
165
|
+
|
|
166
|
+
### Adapter Interfaces
|
|
167
|
+
|
|
168
|
+
| Interface | Category | Description |
|
|
169
|
+
|-------------------------------------------|---------------|--------------------------------|
|
|
170
|
+
| `EmbeddingAdapterInterface` | Source | Embedding generation |
|
|
171
|
+
| `ToolFormatAdapterInterface` | Transform | Provider tool formatting |
|
|
172
|
+
| `SimilarityAdapterInterface` | Transform | Vector similarity computation |
|
|
173
|
+
| `DeduplicationAdapterInterface` | Transform | Frame deduplication |
|
|
174
|
+
| `TruncationAdapterInterface` | Transform | Context truncation |
|
|
175
|
+
| `PriorityAdapterInterface` | Transform | Frame priority ordering |
|
|
176
|
+
| `RetryAdapterInterface` | Policy | Retry behavior |
|
|
177
|
+
| `RateLimitAdapterInterface` | Policy | Rate limiting |
|
|
178
|
+
| `EmbeddingCacheAdapterInterface` | Enhancement | Embedding caching |
|
|
179
|
+
| `BatchAdapterInterface` | Enhancement | Batch processing |
|
|
180
|
+
| `RerankerAdapterInterface` | Enhancement | Result reranking |
|
|
181
|
+
| `VectorStorePersistenceAdapterInterface` | Persistence | Vector storage |
|
|
182
|
+
|
|
183
|
+
### Minimal Cross-Package Interfaces
|
|
184
|
+
|
|
185
|
+
| Interface | Description |
|
|
186
|
+
|-----------------------------|--------------------------------------|
|
|
187
|
+
| `MinimalDatabaseAccess<T>` | IndexedDB access without dependency |
|
|
188
|
+
| `MinimalStoreAccess<T>` | Store operations interface |
|
|
189
|
+
| `MinimalDirectoryAccess` | OPFS directory access |
|
|
190
|
+
| `MinimalFileAccess` | OPFS file access |
|
|
191
|
+
| `ToolRegistryMinimal` | Tool registry without dependency |
|
|
192
|
+
| `VectorStoreMinimal<T>` | Vector store without dependency |
|
|
193
|
+
| `FormMinimal<T>` | Form state without dependency |
|
|
194
|
+
| `SerializableSession` | Session serialization interface |
|
|
107
195
|
|
|
108
196
|
---
|
|
109
197
|
|
|
@@ -112,7 +200,7 @@ const result = chain(ok('42'), parse) // ok(42)
|
|
|
112
200
|
### Result Pattern
|
|
113
201
|
|
|
114
202
|
```ts
|
|
115
|
-
import { ok, err, isOk, unwrap } from '@mikesaintsg/core'
|
|
203
|
+
import { ok, err, isOk, unwrap, map, chain } from '@mikesaintsg/core'
|
|
116
204
|
|
|
117
205
|
function divide(a: number, b: number) {
|
|
118
206
|
if (b === 0) return err('DIVISION_BY_ZERO')
|
|
@@ -121,52 +209,109 @@ function divide(a: number, b: number) {
|
|
|
121
209
|
|
|
122
210
|
const result = divide(10, 2)
|
|
123
211
|
const value = unwrap(result, 0) // 5
|
|
212
|
+
|
|
213
|
+
// Chain operations
|
|
214
|
+
const doubled = chain(divide(10, 2), n => ok(n * 2)) // ok(10)
|
|
124
215
|
```
|
|
125
216
|
|
|
126
|
-
###
|
|
217
|
+
### Custom Error Class
|
|
127
218
|
|
|
128
219
|
```ts
|
|
129
|
-
import {
|
|
220
|
+
import { EcosystemError, isEcosystemError } from '@mikesaintsg/core'
|
|
130
221
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
222
|
+
type StorageErrorCode = 'NOT_FOUND' | 'QUOTA_EXCEEDED' | 'WRITE_FAILED'
|
|
223
|
+
|
|
224
|
+
class StorageError extends EcosystemError {
|
|
225
|
+
readonly code: StorageErrorCode
|
|
226
|
+
|
|
227
|
+
constructor(code: StorageErrorCode, message: string, cause?: Error) {
|
|
228
|
+
super(message, cause)
|
|
229
|
+
this.code = code
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
try {
|
|
234
|
+
throw new StorageError('NOT_FOUND', 'Document not found')
|
|
235
|
+
} catch (error) {
|
|
236
|
+
if (isEcosystemError(error)) {
|
|
237
|
+
console.log(error.code) // 'NOT_FOUND'
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
### Retrieval Tool
|
|
243
|
+
|
|
244
|
+
```ts
|
|
245
|
+
import { createRetrievalTool } from '@mikesaintsg/core'
|
|
246
|
+
|
|
247
|
+
const { schema, handler } = createRetrievalTool({
|
|
248
|
+
vectorStore,
|
|
249
|
+
name: 'search_docs',
|
|
250
|
+
description: 'Search documentation for relevant information',
|
|
251
|
+
defaultLimit: 5,
|
|
252
|
+
scoreThreshold: 0.7,
|
|
135
253
|
})
|
|
136
254
|
|
|
137
|
-
|
|
255
|
+
// Register with tool registry
|
|
256
|
+
registry.register(schema, handler)
|
|
138
257
|
```
|
|
139
258
|
|
|
140
|
-
###
|
|
259
|
+
### Session Persistence
|
|
141
260
|
|
|
142
261
|
```ts
|
|
143
|
-
import
|
|
144
|
-
EmbeddingAdapterInterface,
|
|
145
|
-
Result
|
|
146
|
-
} from '@mikesaintsg/core'
|
|
262
|
+
import { createSessionPersistence } from '@mikesaintsg/core'
|
|
147
263
|
|
|
148
|
-
|
|
149
|
-
|
|
264
|
+
const persistence = createSessionPersistence({
|
|
265
|
+
database,
|
|
266
|
+
storeName: 'chat_sessions',
|
|
267
|
+
autoprune: 7 * 24 * 60 * 60 * 1000, // 7 days
|
|
268
|
+
})
|
|
150
269
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
const n = parseInt(input, 10)
|
|
154
|
-
return isNaN(n) ? err('PARSE_ERROR') : ok(n)
|
|
155
|
-
}
|
|
270
|
+
await persistence.save('session-1', session)
|
|
271
|
+
const loaded = await persistence.load('session-1')
|
|
156
272
|
```
|
|
157
273
|
|
|
274
|
+
### Form Dirty Guard
|
|
275
|
+
|
|
276
|
+
```ts
|
|
277
|
+
import { createFormDirtyGuard } from '@mikesaintsg/core'
|
|
278
|
+
|
|
279
|
+
const guard = createFormDirtyGuard({
|
|
280
|
+
form,
|
|
281
|
+
confirmFn: (message) => window.confirm(message),
|
|
282
|
+
message: 'You have unsaved changes. Continue?',
|
|
283
|
+
excludePages: ['logout'],
|
|
284
|
+
})
|
|
285
|
+
|
|
286
|
+
router.beforeNavigate(guard)
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
---
|
|
290
|
+
|
|
291
|
+
## Constants
|
|
292
|
+
|
|
293
|
+
| Constant | Value | Description |
|
|
294
|
+
|------------------------------|--------|--------------------------------|
|
|
295
|
+
| `BRIDGE_DEFAULT_TIMEOUT` | 30000 | Default bridge timeout (ms) |
|
|
296
|
+
| `RETRIEVAL_DEFAULT_LIMIT` | 10 | Default retrieval result limit |
|
|
297
|
+
| `RETRIEVAL_MAX_LIMIT` | 100 | Maximum retrieval result limit |
|
|
298
|
+
| `FORM_DIRTY_DEFAULT_MESSAGE` | string | Default form guard message |
|
|
299
|
+
|
|
158
300
|
---
|
|
159
301
|
|
|
160
302
|
## Ecosystem Integration
|
|
161
303
|
|
|
162
304
|
| Package | Integration |
|
|
163
305
|
|--------------------------------|------------------------------------------------|
|
|
164
|
-
| `@mikesaintsg/adapters` |
|
|
306
|
+
| `@mikesaintsg/adapters` | Implements adapter interfaces |
|
|
165
307
|
| `@mikesaintsg/inference` | Uses shared types and bridge functions |
|
|
166
308
|
| `@mikesaintsg/vectorstore` | Uses embedding and persistence interfaces |
|
|
167
309
|
| `@mikesaintsg/contextprotocol` | Uses tool types and registry interface |
|
|
310
|
+
| `@mikesaintsg/contextbuilder` | Uses context frame and budget types |
|
|
311
|
+
| `@mikesaintsg/indexeddb` | Implements MinimalDatabaseAccess |
|
|
312
|
+
| `@mikesaintsg/filesystem` | Implements MinimalDirectoryAccess |
|
|
168
313
|
|
|
169
|
-
See [Integration
|
|
314
|
+
See [Integration Guide](./guides/integration.md) for detailed patterns.
|
|
170
315
|
|
|
171
316
|
---
|
|
172
317
|
|