|
| 1 | +# @stackmemoryai/sdk |
| 2 | + |
| 3 | +TypeScript SDK for [StackMemory](https://github.com/stackmemoryai/stackmemory) — content cache, skill packs, and provenance tracking for AI agent workflows. |
| 4 | + |
| 5 | +## Install |
| 6 | + |
| 7 | +```bash |
| 8 | +npm install @stackmemoryai/sdk |
| 9 | +``` |
| 10 | + |
| 11 | +## Quick Start |
| 12 | + |
| 13 | +```ts |
| 14 | +import { StackMemory } from '@stackmemoryai/sdk'; |
| 15 | + |
| 16 | +const sm = new StackMemory(); |
| 17 | + |
| 18 | +// Content-hash token cache — dedup LLM context |
| 19 | +sm.cache.put('function add(a, b) { return a + b; }', 'file:math.ts'); |
| 20 | +const result = sm.cache.lookup('function add(a, b) { return a + b; }'); |
| 21 | +console.log(result.hit); // true |
| 22 | +console.log(result.tokensSaved); // ~11 |
| 23 | + |
| 24 | +// Skill packs — versioned agent bundles |
| 25 | +sm.packs.install({ |
| 26 | + manifest: { |
| 27 | + name: 'coding/typescript-react', |
| 28 | + version: '1.0.0', |
| 29 | + description: 'TypeScript + React patterns', |
| 30 | + author: 'stackmemory', |
| 31 | + license: 'MIT', |
| 32 | + }, |
| 33 | + instructions: 'Use functional components with hooks...', |
| 34 | +}); |
| 35 | +const packs = sm.packs.list(); |
| 36 | +const found = sm.packs.search('typescript'); |
| 37 | + |
| 38 | +// Provenance — trace events with source lineage |
| 39 | +sm.provenance.record({ |
| 40 | + timestamp: new Date().toISOString(), |
| 41 | + traceId: 'trace-001', |
| 42 | + sessionId: 'sess-001', |
| 43 | + tenantId: 'team-alpha', |
| 44 | + actor: { host: 'claude-code', agent: 'sdk', user: 'dev' }, |
| 45 | + operation: 'query', |
| 46 | + inputs: { q: 'auth middleware' }, |
| 47 | + outputs: { result: '3 files found' }, |
| 48 | + tokensIn: 150, |
| 49 | + tokensOut: 80, |
| 50 | + costUsd: 0.002, |
| 51 | + provenance: { |
| 52 | + sources: [{ system: 'github', externalId: 'PR-42', fetchedAt: new Date().toISOString() }], |
| 53 | + derivation: [], |
| 54 | + confidence: 0.85, |
| 55 | + }, |
| 56 | +}); |
| 57 | + |
| 58 | +// Decision confidence scoring |
| 59 | +const score = sm.scoreConfidence('we decided to use SQLite for local storage'); |
| 60 | +console.log(score); // { confidence: 0.3, classification: 'discard', signals: { triggerPhrases: ['we decided'] } } |
| 61 | + |
| 62 | +sm.close(); |
| 63 | +``` |
| 64 | + |
| 65 | +## API |
| 66 | + |
| 67 | +### `new StackMemory(config?)` |
| 68 | + |
| 69 | +| Option | Type | Default | Description | |
| 70 | +|--------|------|---------|-------------| |
| 71 | +| `dataDir` | `string` | `~/.stackmemory` | Directory for SQLite databases | |
| 72 | +| `logLevel` | `'debug' \| 'info' \| 'warn' \| 'error' \| 'silent'` | `'warn'` | Log verbosity | |
| 73 | + |
| 74 | +### `sm.cache` — Content Cache |
| 75 | + |
| 76 | +| Method | Description | |
| 77 | +|--------|-------------| |
| 78 | +| `put(content, source?, metadata?)` | Cache content, returns `CacheEntry` | |
| 79 | +| `lookup(content, source?)` | Check cache, returns `CacheLookupResult` with `hit` and `tokensSaved` | |
| 80 | +| `getStats()` | Aggregate stats: entries, tokens cached/saved, hit rate, top sources | |
| 81 | +| `evict(olderThan?)` | Remove old entries, returns count removed | |
| 82 | +| `clear()` | Remove all entries | |
| 83 | + |
| 84 | +### `sm.packs` — Skill Pack Registry |
| 85 | + |
| 86 | +| Method | Description | |
| 87 | +|--------|-------------| |
| 88 | +| `install(pack, source?)` | Install a skill pack (upserts on name match) | |
| 89 | +| `uninstall(name)` | Remove a pack | |
| 90 | +| `get(name)` | Get pack by name (e.g. `"coding/typescript-react"`) | |
| 91 | +| `list(opts?)` | List packs, optionally filter by `namespace` or `runtime` | |
| 92 | +| `search(query)` | Full-text search across name, description, instructions | |
| 93 | +| `getByTool(toolName)` | Find the pack that provides a specific MCP tool | |
| 94 | + |
| 95 | +### `sm.provenance` — Provenance Store |
| 96 | + |
| 97 | +| Method | Description | |
| 98 | +|--------|-------------| |
| 99 | +| `record(event)` | Persist a `TraceEvent` | |
| 100 | +| `get(traceId)` | Retrieve by trace ID | |
| 101 | +| `query(opts?)` | Filter by `sessionId`, `tenantId`, `operation`, `since`, `limit` | |
| 102 | +| `supersede(traceId, supersededBy)` | Mark a trace as superseded | |
| 103 | +| `getLineage(traceId)` | Follow `parentTraceId` chain to root | |
| 104 | +| `getStats(tenantId?)` | Aggregate: total events, tokens, cost, avg confidence | |
| 105 | + |
| 106 | +### `sm.scoreConfidence(text, context?)` |
| 107 | + |
| 108 | +Score text for decision confidence. Returns `{ confidence, signals, classification }`. |
| 109 | + |
| 110 | +### Standalone exports |
| 111 | + |
| 112 | +```ts |
| 113 | +import { scoreConfidence, estimateTokens, hashContent } from '@stackmemoryai/sdk'; |
| 114 | +import { parsePackYaml, loadPackFromDir } from '@stackmemoryai/sdk'; |
| 115 | +``` |
| 116 | + |
| 117 | +## License |
| 118 | + |
| 119 | +MIT |
0 commit comments