@mikesaintsg/core 0.0.1 → 0.0.2
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 +175 -365
- package/dist/index.d.ts +26 -433
- package/dist/index.js +164 -746
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,365 +1,175 @@
|
|
|
1
|
-
# @mikesaintsg/core
|
|
2
|
-
|
|
3
|
-
> Shared types, contracts, bridge functions
|
|
4
|
-
|
|
5
|
-
[![
|
|
6
|
-
[![
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
- **
|
|
14
|
-
- **
|
|
15
|
-
- **
|
|
16
|
-
- **
|
|
17
|
-
- **
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
//
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
```
|
|
129
|
-
import {
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
### Embedding Adapters
|
|
178
|
-
|
|
179
|
-
Generate embeddings with OpenAI or Anthropic:
|
|
180
|
-
|
|
181
|
-
```typescript
|
|
182
|
-
import {
|
|
183
|
-
createOpenAIEmbeddingAdapter,
|
|
184
|
-
createAnthropicEmbeddingAdapter,
|
|
185
|
-
createBatchedEmbeddingAdapter,
|
|
186
|
-
createEmbeddingCache,
|
|
187
|
-
} from '@mikesaintsg/core'
|
|
188
|
-
|
|
189
|
-
// OpenAI adapter
|
|
190
|
-
const openai = createOpenAIEmbeddingAdapter({
|
|
191
|
-
apiKey: process.env.OPENAI_API_KEY,
|
|
192
|
-
model: 'text-embedding-3-small',
|
|
193
|
-
})
|
|
194
|
-
|
|
195
|
-
// Anthropic adapter (uses Voyage AI)
|
|
196
|
-
const anthropic = createAnthropicEmbeddingAdapter({
|
|
197
|
-
apiKey: process.env.VOYAGE_API_KEY,
|
|
198
|
-
model: 'voyage-2',
|
|
199
|
-
})
|
|
200
|
-
|
|
201
|
-
// Add batching and caching
|
|
202
|
-
const cache = createEmbeddingCache({ maxEntries: 1000, ttlMs: 3600000 })
|
|
203
|
-
const batched = createBatchedEmbeddingAdapter(openai, {
|
|
204
|
-
maxBatchSize: 100,
|
|
205
|
-
flushDelayMs: 50,
|
|
206
|
-
deduplicate: true,
|
|
207
|
-
cache,
|
|
208
|
-
})
|
|
209
|
-
|
|
210
|
-
// Generate embeddings
|
|
211
|
-
const embeddings = await batched.queueBatch([
|
|
212
|
-
'Hello, world!',
|
|
213
|
-
'How are you?',
|
|
214
|
-
])
|
|
215
|
-
```
|
|
216
|
-
|
|
217
|
-
### Tool Format Adapters
|
|
218
|
-
|
|
219
|
-
Convert tool schemas between formats:
|
|
220
|
-
|
|
221
|
-
```typescript
|
|
222
|
-
import {
|
|
223
|
-
createOpenAIToolFormatAdapter,
|
|
224
|
-
createAnthropicToolFormatAdapter,
|
|
225
|
-
} from '@mikesaintsg/core'
|
|
226
|
-
import type { ToolSchema } from '@mikesaintsg/core'
|
|
227
|
-
|
|
228
|
-
const schema: ToolSchema = {
|
|
229
|
-
name: 'get_weather',
|
|
230
|
-
description: 'Get the current weather',
|
|
231
|
-
parameters: {
|
|
232
|
-
type: 'object',
|
|
233
|
-
properties: {
|
|
234
|
-
location: { type: 'string', description: 'City name' },
|
|
235
|
-
},
|
|
236
|
-
required: ['location'],
|
|
237
|
-
},
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
// OpenAI format
|
|
241
|
-
const openaiAdapter = createOpenAIToolFormatAdapter()
|
|
242
|
-
const openaiTools = openaiAdapter.formatSchemas([schema])
|
|
243
|
-
|
|
244
|
-
// Anthropic format
|
|
245
|
-
const anthropicAdapter = createAnthropicToolFormatAdapter()
|
|
246
|
-
const anthropicTools = anthropicAdapter.formatSchemas([schema])
|
|
247
|
-
```
|
|
248
|
-
|
|
249
|
-
### Persistence Adapters
|
|
250
|
-
|
|
251
|
-
Store VectorStore data in different backends:
|
|
252
|
-
|
|
253
|
-
```typescript
|
|
254
|
-
import {
|
|
255
|
-
createIndexedDBVectorStorePersistence,
|
|
256
|
-
createOPFSVectorStorePersistence,
|
|
257
|
-
createHTTPVectorStorePersistence,
|
|
258
|
-
} from '@mikesaintsg/core'
|
|
259
|
-
|
|
260
|
-
// IndexedDB
|
|
261
|
-
const idbPersistence = createIndexedDBVectorStorePersistence({
|
|
262
|
-
database,
|
|
263
|
-
documentsStore: 'embeddings',
|
|
264
|
-
})
|
|
265
|
-
|
|
266
|
-
// OPFS (Origin Private File System)
|
|
267
|
-
const opfsPersistence = createOPFSVectorStorePersistence({
|
|
268
|
-
directory,
|
|
269
|
-
chunkSize: 100,
|
|
270
|
-
})
|
|
271
|
-
|
|
272
|
-
// HTTP (remote storage)
|
|
273
|
-
const httpPersistence = createHTTPVectorStorePersistence({
|
|
274
|
-
baseURL: 'https://api.example.com/vectors',
|
|
275
|
-
headers: { Authorization: 'Bearer token' },
|
|
276
|
-
timeout: 30000,
|
|
277
|
-
})
|
|
278
|
-
```
|
|
279
|
-
|
|
280
|
-
## API Reference
|
|
281
|
-
|
|
282
|
-
### Result Pattern
|
|
283
|
-
|
|
284
|
-
| Function | Description |
|
|
285
|
-
|----------|-------------|
|
|
286
|
-
| `ok(value)` | Create a success result |
|
|
287
|
-
| `err(error)` | Create a failure result |
|
|
288
|
-
| `isOk(result)` | Check if result is success |
|
|
289
|
-
| `isErr(result)` | Check if result is failure |
|
|
290
|
-
| `unwrap(result, default)` | Get value or default |
|
|
291
|
-
| `unwrapOrThrow(result)` | Get value or throw |
|
|
292
|
-
| `map(result, fn)` | Transform success value |
|
|
293
|
-
| `mapErr(result, fn)` | Transform error value |
|
|
294
|
-
| `chain(result, fn)` | Sequence operations |
|
|
295
|
-
|
|
296
|
-
### Bridge Functions
|
|
297
|
-
|
|
298
|
-
| Function | Description |
|
|
299
|
-
|----------|-------------|
|
|
300
|
-
| `createToolCallBridge(options)` | Connect tool calls to registry |
|
|
301
|
-
| `createRetrievalTool(options)` | Create vectorstore search tool |
|
|
302
|
-
| `createFormDirtyGuard(options)` | Navigation guard for dirty forms |
|
|
303
|
-
| `createSessionPersistence(options)` | Persist sessions to IndexedDB |
|
|
304
|
-
|
|
305
|
-
### Embedding Adapters
|
|
306
|
-
|
|
307
|
-
| Function | Description |
|
|
308
|
-
|----------|-------------|
|
|
309
|
-
| `createOpenAIEmbeddingAdapter(options)` | OpenAI embeddings API |
|
|
310
|
-
| `createAnthropicEmbeddingAdapter(options)` | Voyage AI embeddings |
|
|
311
|
-
| `createBatchedEmbeddingAdapter(adapter, options)` | Batching wrapper |
|
|
312
|
-
| `createEmbeddingCache(options)` | LRU cache with TTL |
|
|
313
|
-
|
|
314
|
-
### Tool Format Adapters
|
|
315
|
-
|
|
316
|
-
| Function | Description |
|
|
317
|
-
|----------|-------------|
|
|
318
|
-
| `createOpenAIToolFormatAdapter(options)` | OpenAI tool format |
|
|
319
|
-
| `createAnthropicToolFormatAdapter(options)` | Anthropic tool format |
|
|
320
|
-
|
|
321
|
-
### Persistence Adapters
|
|
322
|
-
|
|
323
|
-
| Function | Description |
|
|
324
|
-
|----------|-------------|
|
|
325
|
-
| `createIndexedDBVectorStorePersistence(options)` | IndexedDB storage |
|
|
326
|
-
| `createOPFSVectorStorePersistence(options)` | OPFS storage |
|
|
327
|
-
| `createHTTPVectorStorePersistence(options)` | HTTP storage |
|
|
328
|
-
|
|
329
|
-
## Types
|
|
330
|
-
|
|
331
|
-
All types are exported from the main entry point:
|
|
332
|
-
|
|
333
|
-
```typescript
|
|
334
|
-
import type {
|
|
335
|
-
// Result pattern
|
|
336
|
-
Result, Ok, Err,
|
|
337
|
-
|
|
338
|
-
// Embedding types
|
|
339
|
-
Embedding, EmbeddingAdapterInterface, EmbeddingModelMetadata,
|
|
340
|
-
|
|
341
|
-
// Tool types
|
|
342
|
-
ToolCall, ToolResult, ToolSchema, JSONSchema7,
|
|
343
|
-
|
|
344
|
-
// Bridge types
|
|
345
|
-
ToolCallBridgeInterface, ToolRegistryMinimal,
|
|
346
|
-
RetrievalToolOptions, VectorStoreMinimal,
|
|
347
|
-
FormDirtyGuardOptions, NavigationGuard,
|
|
348
|
-
SessionPersistenceInterface, SerializedSession,
|
|
349
|
-
|
|
350
|
-
// Persistence types
|
|
351
|
-
VectorStorePersistenceAdapterInterface, StoredDocument,
|
|
352
|
-
|
|
353
|
-
// Minimal cross-package interfaces
|
|
354
|
-
MinimalDatabaseAccess, MinimalStoreAccess,
|
|
355
|
-
MinimalDirectoryAccess, MinimalFileAccess,
|
|
356
|
-
} from '@mikesaintsg/core'
|
|
357
|
-
```
|
|
358
|
-
|
|
359
|
-
## License
|
|
360
|
-
|
|
361
|
-
MIT © mikesaintsg
|
|
362
|
-
|
|
363
|
-
## Contributing
|
|
364
|
-
|
|
365
|
-
See [CONTRIBUTING.md](./CONTRIBUTING.md) for guidelines.
|
|
1
|
+
# @mikesaintsg/core
|
|
2
|
+
|
|
3
|
+
> **Shared types, contracts, and bridge functions for the @mikesaintsg ecosystem.**
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@mikesaintsg/core)
|
|
6
|
+
[](https://bundlephobia.com/package/@mikesaintsg/core)
|
|
7
|
+
[](LICENSE)
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- ✅ **Shared Types** — Contracts used across multiple packages
|
|
14
|
+
- ✅ **Result Pattern** — Functional error handling (`ok`, `err`, `map`, `chain`)
|
|
15
|
+
- ✅ **Bridge Functions** — Connect packages without circular dependencies
|
|
16
|
+
- ✅ **Interface Definitions** — Embedding, tool format, and persistence adapters
|
|
17
|
+
- ✅ **Zero dependencies** — Built on native platform APIs
|
|
18
|
+
- ✅ **TypeScript first** — Full type safety with generics
|
|
19
|
+
- ✅ **Tree-shakeable** — ESM-only, import what you need
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install @mikesaintsg/core
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
## Quick Start
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
import { ok, err, isOk, map, chain } from '@mikesaintsg/core'
|
|
35
|
+
import type { Result } from '@mikesaintsg/core'
|
|
36
|
+
|
|
37
|
+
// Create results
|
|
38
|
+
const success = ok(42)
|
|
39
|
+
const failure = err('NOT_FOUND')
|
|
40
|
+
|
|
41
|
+
// Check and unwrap
|
|
42
|
+
if (isOk(success)) {
|
|
43
|
+
console.log(success.value) // 42
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Transform values
|
|
47
|
+
const doubled = map(ok(5), x => x * 2) // ok(10)
|
|
48
|
+
|
|
49
|
+
// Chain operations
|
|
50
|
+
function parse(s: string): Result<number, string> {
|
|
51
|
+
const n = parseInt(s, 10)
|
|
52
|
+
return isNaN(n) ? err('PARSE_ERROR') : ok(n)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const result = chain(ok('42'), parse) // ok(42)
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
---
|
|
59
|
+
|
|
60
|
+
## Documentation
|
|
61
|
+
|
|
62
|
+
📚 **[Full API Guide](./guides/core.md)** — Comprehensive documentation with examples
|
|
63
|
+
|
|
64
|
+
### Key Sections
|
|
65
|
+
|
|
66
|
+
- [Introduction](./guides/core.md#introduction) — Value proposition and use cases
|
|
67
|
+
- [Quick Start](./guides/core.md#quick-start) — Get started in minutes
|
|
68
|
+
- [Core Concepts](./guides/core.md#core-concepts) — Understand the fundamentals
|
|
69
|
+
- [Result Pattern](./guides/core.md#result-pattern) — Functional error handling
|
|
70
|
+
- [Bridge Interfaces](./guides/core.md#bridge-interfaces) — Cross-package communication
|
|
71
|
+
- [API Reference](./guides/core.md#api-reference) — Complete API documentation
|
|
72
|
+
|
|
73
|
+
---
|
|
74
|
+
|
|
75
|
+
## API Overview
|
|
76
|
+
|
|
77
|
+
### Result Functions
|
|
78
|
+
|
|
79
|
+
| Function | Description |
|
|
80
|
+
|---------------------------|----------------------------|
|
|
81
|
+
| `ok(value)` | Create a success result |
|
|
82
|
+
| `err(error)` | Create a failure result |
|
|
83
|
+
| `isOk(result)` | Check if result is success |
|
|
84
|
+
| `isErr(result)` | Check if result is failure |
|
|
85
|
+
| `unwrap(result, default)` | Get value or default |
|
|
86
|
+
| `map(result, fn)` | Transform success value |
|
|
87
|
+
| `chain(result, fn)` | Chain results (flatMap) |
|
|
88
|
+
|
|
89
|
+
### Bridge Functions
|
|
90
|
+
|
|
91
|
+
| Function | Description |
|
|
92
|
+
|-------------------------------------|----------------------------------|
|
|
93
|
+
| `createToolCallBridge(options)` | Connect tool calls to registry |
|
|
94
|
+
| `createRetrievalTool(options)` | Create vectorstore search tool |
|
|
95
|
+
| `createFormDirtyGuard(options)` | Navigation guard for dirty forms |
|
|
96
|
+
| `createSessionPersistence(options)` | Session storage adapter |
|
|
97
|
+
|
|
98
|
+
### Shared Types
|
|
99
|
+
|
|
100
|
+
| Type | Description |
|
|
101
|
+
|------------------------------|---------------------------------|
|
|
102
|
+
| `Result<T, E>` | Success or failure union |
|
|
103
|
+
| `Unsubscribe` | Cleanup function type |
|
|
104
|
+
| `EmbeddingAdapterInterface` | Embedding generation contract |
|
|
105
|
+
| `ToolFormatAdapterInterface` | Tool format conversion |
|
|
106
|
+
| `BuiltContext` | Assembled context for inference |
|
|
107
|
+
|
|
108
|
+
---
|
|
109
|
+
|
|
110
|
+
## Examples
|
|
111
|
+
|
|
112
|
+
### Result Pattern
|
|
113
|
+
|
|
114
|
+
```ts
|
|
115
|
+
import { ok, err, isOk, unwrap } from '@mikesaintsg/core'
|
|
116
|
+
|
|
117
|
+
function divide(a: number, b: number) {
|
|
118
|
+
if (b === 0) return err('DIVISION_BY_ZERO')
|
|
119
|
+
return ok(a / b)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const result = divide(10, 2)
|
|
123
|
+
const value = unwrap(result, 0) // 5
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### Tool Call Bridge
|
|
127
|
+
|
|
128
|
+
```ts
|
|
129
|
+
import { createToolCallBridge } from '@mikesaintsg/core'
|
|
130
|
+
|
|
131
|
+
const bridge = createToolCallBridge({
|
|
132
|
+
registry,
|
|
133
|
+
timeout: 30000,
|
|
134
|
+
onError: (error, toolCall) => console.error(error),
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
const result = await bridge.execute(toolCall)
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### With TypeScript
|
|
141
|
+
|
|
142
|
+
```ts
|
|
143
|
+
import type {
|
|
144
|
+
EmbeddingAdapterInterface,
|
|
145
|
+
Result
|
|
146
|
+
} from '@mikesaintsg/core'
|
|
147
|
+
|
|
148
|
+
// Type-safe adapter interface
|
|
149
|
+
const adapter: EmbeddingAdapterInterface = createAdapter()
|
|
150
|
+
|
|
151
|
+
// Inferred result types
|
|
152
|
+
function safeParse(input: string): Result<number, 'PARSE_ERROR'> {
|
|
153
|
+
const n = parseInt(input, 10)
|
|
154
|
+
return isNaN(n) ? err('PARSE_ERROR') : ok(n)
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
---
|
|
159
|
+
|
|
160
|
+
## Ecosystem Integration
|
|
161
|
+
|
|
162
|
+
| Package | Integration |
|
|
163
|
+
|--------------------------------|------------------------------------------------|
|
|
164
|
+
| `@mikesaintsg/adapters` | Provider and embedding adapter implementations |
|
|
165
|
+
| `@mikesaintsg/inference` | Uses shared types and bridge functions |
|
|
166
|
+
| `@mikesaintsg/vectorstore` | Uses embedding and persistence interfaces |
|
|
167
|
+
| `@mikesaintsg/contextprotocol` | Uses tool types and registry interface |
|
|
168
|
+
|
|
169
|
+
See [Integration Patterns](./guides/core.md#integration-patterns) for details.
|
|
170
|
+
|
|
171
|
+
---
|
|
172
|
+
|
|
173
|
+
## License
|
|
174
|
+
|
|
175
|
+
MIT © [mikesaintsg](https://github.com/mikesaintsg)
|