@usewhisper/sdk 1.1.0 → 2.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 +256 -256
- package/index.d.mts +152 -1
- package/index.d.ts +152 -1
- package/index.js +164 -0
- package/index.mjs +160 -0
- package/package.json +56 -56
package/README.md
CHANGED
|
@@ -1,256 +1,256 @@
|
|
|
1
|
-
# @usewhisper/sdk
|
|
2
|
-
|
|
3
|
-
Official TypeScript SDK for [Whisper Context API](https://usewhisper.dev) - Give your AI agents perfect context.
|
|
4
|
-
|
|
5
|
-
## Installation
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
npm install @usewhisper/sdk
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
## Quick Start
|
|
12
|
-
|
|
13
|
-
```typescript
|
|
14
|
-
import { WhisperContext } from '@usewhisper/sdk';
|
|
15
|
-
|
|
16
|
-
const whisper = new WhisperContext({
|
|
17
|
-
apiKey: 'wctx_your_api_key_here'
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
// Create a project
|
|
21
|
-
const project = await whisper.createProject({
|
|
22
|
-
name: 'my-docs',
|
|
23
|
-
description: 'Documentation context'
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
// Ingest documents
|
|
27
|
-
await whisper.ingest(project.id, [
|
|
28
|
-
{
|
|
29
|
-
title: 'Authentication Guide',
|
|
30
|
-
content: 'To authenticate users, use JWT tokens...',
|
|
31
|
-
metadata: { category: 'auth' }
|
|
32
|
-
}
|
|
33
|
-
]);
|
|
34
|
-
|
|
35
|
-
// Query context
|
|
36
|
-
const result = await whisper.query({
|
|
37
|
-
project: 'my-docs',
|
|
38
|
-
query: 'How do I authenticate users?',
|
|
39
|
-
top_k: 5
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
console.log(result.context);
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
## Authentication
|
|
46
|
-
|
|
47
|
-
Get your API key from the [Whisper dashboard](https://usewhisper.dev/dashboard):
|
|
48
|
-
|
|
49
|
-
```typescript
|
|
50
|
-
const whisper = new WhisperContext({
|
|
51
|
-
apiKey: 'wctx_...', // Your API key
|
|
52
|
-
baseUrl: 'https://context.usewhisper.dev' // Optional, defaults to production
|
|
53
|
-
});
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
## Core Features
|
|
57
|
-
|
|
58
|
-
### Context Query
|
|
59
|
-
|
|
60
|
-
```typescript
|
|
61
|
-
const result = await whisper.query({
|
|
62
|
-
project: 'my-docs',
|
|
63
|
-
query: 'Your question here',
|
|
64
|
-
top_k: 10, // Number of results
|
|
65
|
-
include_memories: true, // Include conversational memory
|
|
66
|
-
include_graph: true, // Include knowledge graph
|
|
67
|
-
hybrid: true, // Hybrid vector + keyword search
|
|
68
|
-
rerank: true // Rerank results for better relevance
|
|
69
|
-
});
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
### Project Management
|
|
73
|
-
|
|
74
|
-
```typescript
|
|
75
|
-
// Create project
|
|
76
|
-
await whisper.createProject({ name: 'my-project' });
|
|
77
|
-
|
|
78
|
-
// List projects
|
|
79
|
-
const { projects } = await whisper.listProjects();
|
|
80
|
-
|
|
81
|
-
// Get project details
|
|
82
|
-
const project = await whisper.getProject(projectId);
|
|
83
|
-
|
|
84
|
-
// Delete project
|
|
85
|
-
await whisper.deleteProject(projectId);
|
|
86
|
-
```
|
|
87
|
-
|
|
88
|
-
### Data Sources
|
|
89
|
-
|
|
90
|
-
Connect 15+ auto-sync sources:
|
|
91
|
-
|
|
92
|
-
```typescript
|
|
93
|
-
// GitHub repository
|
|
94
|
-
await whisper.addSource(projectId, {
|
|
95
|
-
name: 'GitHub Repo',
|
|
96
|
-
connector_type: 'github',
|
|
97
|
-
config: {
|
|
98
|
-
repo: 'owner/repo',
|
|
99
|
-
token: 'ghp_...',
|
|
100
|
-
branch: 'main'
|
|
101
|
-
},
|
|
102
|
-
sync_schedule: '0 */6 * * *' // Sync every 6 hours
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
// Notion workspace
|
|
106
|
-
await whisper.addSource(projectId, {
|
|
107
|
-
name: 'Notion Docs',
|
|
108
|
-
connector_type: 'notion',
|
|
109
|
-
config: {
|
|
110
|
-
token: 'secret_...',
|
|
111
|
-
database_id: '...'
|
|
112
|
-
}
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
// Sync source manually
|
|
116
|
-
await whisper.syncSource(sourceId);
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
-
### Direct Ingestion
|
|
120
|
-
|
|
121
|
-
```typescript
|
|
122
|
-
await whisper.ingest(projectId, [
|
|
123
|
-
{
|
|
124
|
-
title: 'Document Title',
|
|
125
|
-
content: 'Document content...',
|
|
126
|
-
metadata: {
|
|
127
|
-
author: 'John Doe',
|
|
128
|
-
tags: ['api', 'docs']
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
]);
|
|
132
|
-
```
|
|
133
|
-
|
|
134
|
-
### Conversational Memory
|
|
135
|
-
|
|
136
|
-
```typescript
|
|
137
|
-
// Add memory
|
|
138
|
-
await whisper.addMemory({
|
|
139
|
-
project: 'my-docs',
|
|
140
|
-
content: 'User prefers dark mode',
|
|
141
|
-
memory_type: 'factual',
|
|
142
|
-
user_id: 'user123',
|
|
143
|
-
importance: 0.8
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
// Search memories
|
|
147
|
-
const { memories } = await whisper.searchMemories({
|
|
148
|
-
project: 'my-docs',
|
|
149
|
-
query: 'user preferences',
|
|
150
|
-
user_id: 'user123',
|
|
151
|
-
top_k: 10
|
|
152
|
-
});
|
|
153
|
-
```
|
|
154
|
-
|
|
155
|
-
## Supported Connectors
|
|
156
|
-
|
|
157
|
-
- **GitHub** - Repositories, issues, PRs
|
|
158
|
-
- **GitLab** - Projects, issues, MRs
|
|
159
|
-
- **Notion** - Pages, databases
|
|
160
|
-
- **Confluence** - Spaces, pages
|
|
161
|
-
- **Slack** - Channels, messages
|
|
162
|
-
- **Discord** - Channels, messages
|
|
163
|
-
- **URLs** - Web pages
|
|
164
|
-
- **Sitemaps** - Entire websites
|
|
165
|
-
- **PDFs** - PDF documents
|
|
166
|
-
- **API Specs** - OpenAPI/Swagger
|
|
167
|
-
- **Databases** - PostgreSQL, MySQL
|
|
168
|
-
- **npm** - Package documentation
|
|
169
|
-
- **PyPI** - Package documentation
|
|
170
|
-
- **arXiv** - Research papers
|
|
171
|
-
- **HuggingFace** - Model docs
|
|
172
|
-
|
|
173
|
-
## API Reference
|
|
174
|
-
|
|
175
|
-
### WhisperContext
|
|
176
|
-
|
|
177
|
-
#### Constructor
|
|
178
|
-
|
|
179
|
-
```typescript
|
|
180
|
-
new WhisperContext(config: {
|
|
181
|
-
apiKey: string;
|
|
182
|
-
baseUrl?: string;
|
|
183
|
-
})
|
|
184
|
-
```
|
|
185
|
-
|
|
186
|
-
#### Methods
|
|
187
|
-
|
|
188
|
-
**Projects:**
|
|
189
|
-
- `createProject(params)` - Create a new project
|
|
190
|
-
- `listProjects()` - List all projects
|
|
191
|
-
- `getProject(id)` - Get project details
|
|
192
|
-
- `deleteProject(id)` - Delete a project
|
|
193
|
-
|
|
194
|
-
**Sources:**
|
|
195
|
-
- `addSource(projectId, params)` - Add a data source
|
|
196
|
-
- `listSources(projectId)` - List project sources
|
|
197
|
-
- `syncSource(sourceId)` - Manually sync a source
|
|
198
|
-
- `updateSource(sourceId, params)` - Update source config
|
|
199
|
-
- `deleteSource(sourceId)` - Delete a source
|
|
200
|
-
|
|
201
|
-
**Context:**
|
|
202
|
-
- `query(params)` - Query context from your data
|
|
203
|
-
- `ingest(projectId, documents)` - Directly ingest documents
|
|
204
|
-
|
|
205
|
-
**Memory:**
|
|
206
|
-
- `addMemory(params)` - Add conversational memory
|
|
207
|
-
- `searchMemories(params)` - Search memories
|
|
208
|
-
- `listMemories(params)` - List all memories
|
|
209
|
-
- `updateMemory(id, params)` - Update a memory
|
|
210
|
-
- `deleteMemory(id)` - Delete a memory
|
|
211
|
-
|
|
212
|
-
**API Keys:**
|
|
213
|
-
- `createApiKey(params)` - Create a new API key
|
|
214
|
-
- `listApiKeys()` - List all API keys
|
|
215
|
-
- `deleteApiKey(id)` - Delete an API key
|
|
216
|
-
|
|
217
|
-
**Usage:**
|
|
218
|
-
- `getUsage(days)` - Get usage statistics
|
|
219
|
-
|
|
220
|
-
## Error Handling
|
|
221
|
-
|
|
222
|
-
```typescript
|
|
223
|
-
try {
|
|
224
|
-
const result = await whisper.query({
|
|
225
|
-
project: 'my-docs',
|
|
226
|
-
query: 'test'
|
|
227
|
-
});
|
|
228
|
-
} catch (error) {
|
|
229
|
-
if (error.message.includes('401')) {
|
|
230
|
-
console.error('Invalid API key');
|
|
231
|
-
} else if (error.message.includes('404')) {
|
|
232
|
-
console.error('Project not found');
|
|
233
|
-
} else {
|
|
234
|
-
console.error('Query failed:', error.message);
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
```
|
|
238
|
-
|
|
239
|
-
## TypeScript Support
|
|
240
|
-
|
|
241
|
-
Full TypeScript support with type definitions included:
|
|
242
|
-
|
|
243
|
-
```typescript
|
|
244
|
-
import { WhisperContext, QueryParams, QueryResult } from '@usewhisper/sdk';
|
|
245
|
-
```
|
|
246
|
-
|
|
247
|
-
## Links
|
|
248
|
-
|
|
249
|
-
- [Documentation](https://docs.usewhisper.dev)
|
|
250
|
-
- [API Reference](https://context.usewhisper.dev)
|
|
251
|
-
- [GitHub](https://github.com/usewhisper/whisper)
|
|
252
|
-
- [Website](https://usewhisper.dev)
|
|
253
|
-
|
|
254
|
-
## License
|
|
255
|
-
|
|
256
|
-
MIT
|
|
1
|
+
# @usewhisper/sdk
|
|
2
|
+
|
|
3
|
+
Official TypeScript SDK for [Whisper Context API](https://usewhisper.dev) - Give your AI agents perfect context.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @usewhisper/sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { WhisperContext } from '@usewhisper/sdk';
|
|
15
|
+
|
|
16
|
+
const whisper = new WhisperContext({
|
|
17
|
+
apiKey: 'wctx_your_api_key_here'
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
// Create a project
|
|
21
|
+
const project = await whisper.createProject({
|
|
22
|
+
name: 'my-docs',
|
|
23
|
+
description: 'Documentation context'
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
// Ingest documents
|
|
27
|
+
await whisper.ingest(project.id, [
|
|
28
|
+
{
|
|
29
|
+
title: 'Authentication Guide',
|
|
30
|
+
content: 'To authenticate users, use JWT tokens...',
|
|
31
|
+
metadata: { category: 'auth' }
|
|
32
|
+
}
|
|
33
|
+
]);
|
|
34
|
+
|
|
35
|
+
// Query context
|
|
36
|
+
const result = await whisper.query({
|
|
37
|
+
project: 'my-docs',
|
|
38
|
+
query: 'How do I authenticate users?',
|
|
39
|
+
top_k: 5
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
console.log(result.context);
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Authentication
|
|
46
|
+
|
|
47
|
+
Get your API key from the [Whisper dashboard](https://usewhisper.dev/dashboard):
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
const whisper = new WhisperContext({
|
|
51
|
+
apiKey: 'wctx_...', // Your API key
|
|
52
|
+
baseUrl: 'https://context.usewhisper.dev' // Optional, defaults to production
|
|
53
|
+
});
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Core Features
|
|
57
|
+
|
|
58
|
+
### Context Query
|
|
59
|
+
|
|
60
|
+
```typescript
|
|
61
|
+
const result = await whisper.query({
|
|
62
|
+
project: 'my-docs',
|
|
63
|
+
query: 'Your question here',
|
|
64
|
+
top_k: 10, // Number of results
|
|
65
|
+
include_memories: true, // Include conversational memory
|
|
66
|
+
include_graph: true, // Include knowledge graph
|
|
67
|
+
hybrid: true, // Hybrid vector + keyword search
|
|
68
|
+
rerank: true // Rerank results for better relevance
|
|
69
|
+
});
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Project Management
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
// Create project
|
|
76
|
+
await whisper.createProject({ name: 'my-project' });
|
|
77
|
+
|
|
78
|
+
// List projects
|
|
79
|
+
const { projects } = await whisper.listProjects();
|
|
80
|
+
|
|
81
|
+
// Get project details
|
|
82
|
+
const project = await whisper.getProject(projectId);
|
|
83
|
+
|
|
84
|
+
// Delete project
|
|
85
|
+
await whisper.deleteProject(projectId);
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Data Sources
|
|
89
|
+
|
|
90
|
+
Connect 15+ auto-sync sources:
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
// GitHub repository
|
|
94
|
+
await whisper.addSource(projectId, {
|
|
95
|
+
name: 'GitHub Repo',
|
|
96
|
+
connector_type: 'github',
|
|
97
|
+
config: {
|
|
98
|
+
repo: 'owner/repo',
|
|
99
|
+
token: 'ghp_...',
|
|
100
|
+
branch: 'main'
|
|
101
|
+
},
|
|
102
|
+
sync_schedule: '0 */6 * * *' // Sync every 6 hours
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
// Notion workspace
|
|
106
|
+
await whisper.addSource(projectId, {
|
|
107
|
+
name: 'Notion Docs',
|
|
108
|
+
connector_type: 'notion',
|
|
109
|
+
config: {
|
|
110
|
+
token: 'secret_...',
|
|
111
|
+
database_id: '...'
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
// Sync source manually
|
|
116
|
+
await whisper.syncSource(sourceId);
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### Direct Ingestion
|
|
120
|
+
|
|
121
|
+
```typescript
|
|
122
|
+
await whisper.ingest(projectId, [
|
|
123
|
+
{
|
|
124
|
+
title: 'Document Title',
|
|
125
|
+
content: 'Document content...',
|
|
126
|
+
metadata: {
|
|
127
|
+
author: 'John Doe',
|
|
128
|
+
tags: ['api', 'docs']
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
]);
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### Conversational Memory
|
|
135
|
+
|
|
136
|
+
```typescript
|
|
137
|
+
// Add memory
|
|
138
|
+
await whisper.addMemory({
|
|
139
|
+
project: 'my-docs',
|
|
140
|
+
content: 'User prefers dark mode',
|
|
141
|
+
memory_type: 'factual',
|
|
142
|
+
user_id: 'user123',
|
|
143
|
+
importance: 0.8
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
// Search memories
|
|
147
|
+
const { memories } = await whisper.searchMemories({
|
|
148
|
+
project: 'my-docs',
|
|
149
|
+
query: 'user preferences',
|
|
150
|
+
user_id: 'user123',
|
|
151
|
+
top_k: 10
|
|
152
|
+
});
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
## Supported Connectors
|
|
156
|
+
|
|
157
|
+
- **GitHub** - Repositories, issues, PRs
|
|
158
|
+
- **GitLab** - Projects, issues, MRs
|
|
159
|
+
- **Notion** - Pages, databases
|
|
160
|
+
- **Confluence** - Spaces, pages
|
|
161
|
+
- **Slack** - Channels, messages
|
|
162
|
+
- **Discord** - Channels, messages
|
|
163
|
+
- **URLs** - Web pages
|
|
164
|
+
- **Sitemaps** - Entire websites
|
|
165
|
+
- **PDFs** - PDF documents
|
|
166
|
+
- **API Specs** - OpenAPI/Swagger
|
|
167
|
+
- **Databases** - PostgreSQL, MySQL
|
|
168
|
+
- **npm** - Package documentation
|
|
169
|
+
- **PyPI** - Package documentation
|
|
170
|
+
- **arXiv** - Research papers
|
|
171
|
+
- **HuggingFace** - Model docs
|
|
172
|
+
|
|
173
|
+
## API Reference
|
|
174
|
+
|
|
175
|
+
### WhisperContext
|
|
176
|
+
|
|
177
|
+
#### Constructor
|
|
178
|
+
|
|
179
|
+
```typescript
|
|
180
|
+
new WhisperContext(config: {
|
|
181
|
+
apiKey: string;
|
|
182
|
+
baseUrl?: string;
|
|
183
|
+
})
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
#### Methods
|
|
187
|
+
|
|
188
|
+
**Projects:**
|
|
189
|
+
- `createProject(params)` - Create a new project
|
|
190
|
+
- `listProjects()` - List all projects
|
|
191
|
+
- `getProject(id)` - Get project details
|
|
192
|
+
- `deleteProject(id)` - Delete a project
|
|
193
|
+
|
|
194
|
+
**Sources:**
|
|
195
|
+
- `addSource(projectId, params)` - Add a data source
|
|
196
|
+
- `listSources(projectId)` - List project sources
|
|
197
|
+
- `syncSource(sourceId)` - Manually sync a source
|
|
198
|
+
- `updateSource(sourceId, params)` - Update source config
|
|
199
|
+
- `deleteSource(sourceId)` - Delete a source
|
|
200
|
+
|
|
201
|
+
**Context:**
|
|
202
|
+
- `query(params)` - Query context from your data
|
|
203
|
+
- `ingest(projectId, documents)` - Directly ingest documents
|
|
204
|
+
|
|
205
|
+
**Memory:**
|
|
206
|
+
- `addMemory(params)` - Add conversational memory
|
|
207
|
+
- `searchMemories(params)` - Search memories
|
|
208
|
+
- `listMemories(params)` - List all memories
|
|
209
|
+
- `updateMemory(id, params)` - Update a memory
|
|
210
|
+
- `deleteMemory(id)` - Delete a memory
|
|
211
|
+
|
|
212
|
+
**API Keys:**
|
|
213
|
+
- `createApiKey(params)` - Create a new API key
|
|
214
|
+
- `listApiKeys()` - List all API keys
|
|
215
|
+
- `deleteApiKey(id)` - Delete an API key
|
|
216
|
+
|
|
217
|
+
**Usage:**
|
|
218
|
+
- `getUsage(days)` - Get usage statistics
|
|
219
|
+
|
|
220
|
+
## Error Handling
|
|
221
|
+
|
|
222
|
+
```typescript
|
|
223
|
+
try {
|
|
224
|
+
const result = await whisper.query({
|
|
225
|
+
project: 'my-docs',
|
|
226
|
+
query: 'test'
|
|
227
|
+
});
|
|
228
|
+
} catch (error) {
|
|
229
|
+
if (error.message.includes('401')) {
|
|
230
|
+
console.error('Invalid API key');
|
|
231
|
+
} else if (error.message.includes('404')) {
|
|
232
|
+
console.error('Project not found');
|
|
233
|
+
} else {
|
|
234
|
+
console.error('Query failed:', error.message);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
## TypeScript Support
|
|
240
|
+
|
|
241
|
+
Full TypeScript support with type definitions included:
|
|
242
|
+
|
|
243
|
+
```typescript
|
|
244
|
+
import { WhisperContext, QueryParams, QueryResult } from '@usewhisper/sdk';
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
## Links
|
|
248
|
+
|
|
249
|
+
- [Documentation](https://docs.usewhisper.dev)
|
|
250
|
+
- [API Reference](https://context.usewhisper.dev)
|
|
251
|
+
- [GitHub](https://github.com/usewhisper/whisper)
|
|
252
|
+
- [Website](https://usewhisper.dev)
|
|
253
|
+
|
|
254
|
+
## License
|
|
255
|
+
|
|
256
|
+
MIT
|
package/index.d.mts
CHANGED
|
@@ -1,3 +1,154 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whisper - Simple Memory Layer for AI Agents
|
|
3
|
+
*
|
|
4
|
+
* Two methods:
|
|
5
|
+
* - getContext(): Retrieve relevant context before LLM call
|
|
6
|
+
* - capture(): Extract and store memories after LLM response
|
|
7
|
+
*
|
|
8
|
+
* Zero magic - you control when to get context and when to capture
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
interface WhisperOptions extends WhisperConfig {
|
|
12
|
+
/**
|
|
13
|
+
* Maximum context results to retrieve.
|
|
14
|
+
* Default: 10
|
|
15
|
+
*/
|
|
16
|
+
contextLimit?: number;
|
|
17
|
+
/**
|
|
18
|
+
* Which memory types to use.
|
|
19
|
+
* Default: all 7 types
|
|
20
|
+
*/
|
|
21
|
+
memoryTypes?: Array<"factual" | "preference" | "event" | "relationship" | "opinion" | "goal" | "instruction">;
|
|
22
|
+
/**
|
|
23
|
+
* Prefix for context injection.
|
|
24
|
+
* Default: "Relevant context:"
|
|
25
|
+
*/
|
|
26
|
+
contextPrefix?: string;
|
|
27
|
+
}
|
|
28
|
+
interface ContextResult {
|
|
29
|
+
context: string;
|
|
30
|
+
results: QueryResult["results"];
|
|
31
|
+
count: number;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Simple, transparent memory layer
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* import { Whisper } from '@usewhisper/sdk';
|
|
39
|
+
*
|
|
40
|
+
* const whisper = new Whisper({
|
|
41
|
+
* apiKey: process.env.WHISPER_KEY,
|
|
42
|
+
* project: 'my-app'
|
|
43
|
+
* });
|
|
44
|
+
*
|
|
45
|
+
* // BEFORE: Get relevant context
|
|
46
|
+
* const { context, results } = await whisper.getContext("What does user prefer?");
|
|
47
|
+
*
|
|
48
|
+
* // Inject context into your LLM prompt
|
|
49
|
+
* const prompt = `${context}\n\nUser: What does user prefer?`;
|
|
50
|
+
* const response = await llm.complete(prompt);
|
|
51
|
+
*
|
|
52
|
+
* // AFTER: Capture what happened
|
|
53
|
+
* await whisper.capture(response);
|
|
54
|
+
* // → Memories extracted & stored (async)
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
declare class Whisper {
|
|
58
|
+
private client;
|
|
59
|
+
private options;
|
|
60
|
+
private sessionId?;
|
|
61
|
+
private userId?;
|
|
62
|
+
constructor(options: WhisperOptions);
|
|
63
|
+
/**
|
|
64
|
+
* Set session ID for conversation tracking
|
|
65
|
+
*/
|
|
66
|
+
session(sessionId: string): this;
|
|
67
|
+
/**
|
|
68
|
+
* Set user ID for user-specific memories
|
|
69
|
+
*/
|
|
70
|
+
user(userId: string): this;
|
|
71
|
+
/**
|
|
72
|
+
* Get relevant context BEFORE your LLM call
|
|
73
|
+
*
|
|
74
|
+
* @param query - What you want to know / user question
|
|
75
|
+
* @returns Context string and raw results
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```typescript
|
|
79
|
+
* const { context, results, count } = await whisper.getContext(
|
|
80
|
+
* "What are user's preferences?",
|
|
81
|
+
* { userId: "user-123" }
|
|
82
|
+
* );
|
|
83
|
+
*
|
|
84
|
+
* // Results: [
|
|
85
|
+
* // { content: "User prefers dark mode", type: "preference", score: 0.95 },
|
|
86
|
+
* // { content: "Allergic to nuts", type: "factual", score: 0.89 }
|
|
87
|
+
* // ]
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
getContext(query: string, options?: {
|
|
91
|
+
userId?: string;
|
|
92
|
+
sessionId?: string;
|
|
93
|
+
project?: string;
|
|
94
|
+
limit?: number;
|
|
95
|
+
}): Promise<ContextResult>;
|
|
96
|
+
/**
|
|
97
|
+
* Remember what happened AFTER your LLM response
|
|
98
|
+
*
|
|
99
|
+
* Fire-and-forget - doesn't block your response
|
|
100
|
+
*
|
|
101
|
+
* @param content - What your LLM responded with
|
|
102
|
+
* @returns Promise that resolves when stored (or fails silently)
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```typescript
|
|
106
|
+
* const llmResponse = "I've set your theme to dark mode and removed nuts from recommendations.";
|
|
107
|
+
*
|
|
108
|
+
* await whisper.remember(llmResponse, { userId: "user-123" });
|
|
109
|
+
* // → Auto-extracts: "theme set to dark mode", "nut allergy"
|
|
110
|
+
* // → Stored as preferences
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
remember(content: string, options?: {
|
|
114
|
+
userId?: string;
|
|
115
|
+
sessionId?: string;
|
|
116
|
+
project?: string;
|
|
117
|
+
}): Promise<{
|
|
118
|
+
success: boolean;
|
|
119
|
+
memoryId?: string;
|
|
120
|
+
}>;
|
|
121
|
+
/**
|
|
122
|
+
* Alias for remember() - same thing
|
|
123
|
+
*/
|
|
124
|
+
capture(content: string, options?: {
|
|
125
|
+
userId?: string;
|
|
126
|
+
sessionId?: string;
|
|
127
|
+
project?: string;
|
|
128
|
+
}): Promise<{
|
|
129
|
+
success: boolean;
|
|
130
|
+
memoryId?: string;
|
|
131
|
+
}>;
|
|
132
|
+
/**
|
|
133
|
+
* Capture from multiple messages (e.g., full conversation)
|
|
134
|
+
*/
|
|
135
|
+
captureSession(messages: Array<{
|
|
136
|
+
role: string;
|
|
137
|
+
content: string;
|
|
138
|
+
}>, options?: {
|
|
139
|
+
userId?: string;
|
|
140
|
+
sessionId?: string;
|
|
141
|
+
project?: string;
|
|
142
|
+
}): Promise<{
|
|
143
|
+
success: boolean;
|
|
144
|
+
extracted: number;
|
|
145
|
+
}>;
|
|
146
|
+
/**
|
|
147
|
+
* Direct access to WhisperContext for advanced usage
|
|
148
|
+
*/
|
|
149
|
+
raw(): WhisperContext;
|
|
150
|
+
}
|
|
151
|
+
|
|
1
152
|
/**
|
|
2
153
|
* Whisper Context SDK
|
|
3
154
|
* TypeScript SDK for the Whisper Context API
|
|
@@ -611,4 +762,4 @@ declare class WhisperContext {
|
|
|
611
762
|
};
|
|
612
763
|
}
|
|
613
764
|
|
|
614
|
-
export { type Memory, type Project, type QueryParams, type QueryResult, type Source, type WhisperConfig, WhisperContext, WhisperError, type WhisperErrorCode, WhisperContext as default };
|
|
765
|
+
export { type Memory, type Project, type QueryParams, type QueryResult, type Source, Whisper, type WhisperConfig, WhisperContext, Whisper as WhisperDefault, WhisperError, type WhisperErrorCode, WhisperContext as default };
|
package/index.d.ts
CHANGED
|
@@ -1,3 +1,154 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whisper - Simple Memory Layer for AI Agents
|
|
3
|
+
*
|
|
4
|
+
* Two methods:
|
|
5
|
+
* - getContext(): Retrieve relevant context before LLM call
|
|
6
|
+
* - capture(): Extract and store memories after LLM response
|
|
7
|
+
*
|
|
8
|
+
* Zero magic - you control when to get context and when to capture
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
interface WhisperOptions extends WhisperConfig {
|
|
12
|
+
/**
|
|
13
|
+
* Maximum context results to retrieve.
|
|
14
|
+
* Default: 10
|
|
15
|
+
*/
|
|
16
|
+
contextLimit?: number;
|
|
17
|
+
/**
|
|
18
|
+
* Which memory types to use.
|
|
19
|
+
* Default: all 7 types
|
|
20
|
+
*/
|
|
21
|
+
memoryTypes?: Array<"factual" | "preference" | "event" | "relationship" | "opinion" | "goal" | "instruction">;
|
|
22
|
+
/**
|
|
23
|
+
* Prefix for context injection.
|
|
24
|
+
* Default: "Relevant context:"
|
|
25
|
+
*/
|
|
26
|
+
contextPrefix?: string;
|
|
27
|
+
}
|
|
28
|
+
interface ContextResult {
|
|
29
|
+
context: string;
|
|
30
|
+
results: QueryResult["results"];
|
|
31
|
+
count: number;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Simple, transparent memory layer
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* import { Whisper } from '@usewhisper/sdk';
|
|
39
|
+
*
|
|
40
|
+
* const whisper = new Whisper({
|
|
41
|
+
* apiKey: process.env.WHISPER_KEY,
|
|
42
|
+
* project: 'my-app'
|
|
43
|
+
* });
|
|
44
|
+
*
|
|
45
|
+
* // BEFORE: Get relevant context
|
|
46
|
+
* const { context, results } = await whisper.getContext("What does user prefer?");
|
|
47
|
+
*
|
|
48
|
+
* // Inject context into your LLM prompt
|
|
49
|
+
* const prompt = `${context}\n\nUser: What does user prefer?`;
|
|
50
|
+
* const response = await llm.complete(prompt);
|
|
51
|
+
*
|
|
52
|
+
* // AFTER: Capture what happened
|
|
53
|
+
* await whisper.capture(response);
|
|
54
|
+
* // → Memories extracted & stored (async)
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
declare class Whisper {
|
|
58
|
+
private client;
|
|
59
|
+
private options;
|
|
60
|
+
private sessionId?;
|
|
61
|
+
private userId?;
|
|
62
|
+
constructor(options: WhisperOptions);
|
|
63
|
+
/**
|
|
64
|
+
* Set session ID for conversation tracking
|
|
65
|
+
*/
|
|
66
|
+
session(sessionId: string): this;
|
|
67
|
+
/**
|
|
68
|
+
* Set user ID for user-specific memories
|
|
69
|
+
*/
|
|
70
|
+
user(userId: string): this;
|
|
71
|
+
/**
|
|
72
|
+
* Get relevant context BEFORE your LLM call
|
|
73
|
+
*
|
|
74
|
+
* @param query - What you want to know / user question
|
|
75
|
+
* @returns Context string and raw results
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```typescript
|
|
79
|
+
* const { context, results, count } = await whisper.getContext(
|
|
80
|
+
* "What are user's preferences?",
|
|
81
|
+
* { userId: "user-123" }
|
|
82
|
+
* );
|
|
83
|
+
*
|
|
84
|
+
* // Results: [
|
|
85
|
+
* // { content: "User prefers dark mode", type: "preference", score: 0.95 },
|
|
86
|
+
* // { content: "Allergic to nuts", type: "factual", score: 0.89 }
|
|
87
|
+
* // ]
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
getContext(query: string, options?: {
|
|
91
|
+
userId?: string;
|
|
92
|
+
sessionId?: string;
|
|
93
|
+
project?: string;
|
|
94
|
+
limit?: number;
|
|
95
|
+
}): Promise<ContextResult>;
|
|
96
|
+
/**
|
|
97
|
+
* Remember what happened AFTER your LLM response
|
|
98
|
+
*
|
|
99
|
+
* Fire-and-forget - doesn't block your response
|
|
100
|
+
*
|
|
101
|
+
* @param content - What your LLM responded with
|
|
102
|
+
* @returns Promise that resolves when stored (or fails silently)
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```typescript
|
|
106
|
+
* const llmResponse = "I've set your theme to dark mode and removed nuts from recommendations.";
|
|
107
|
+
*
|
|
108
|
+
* await whisper.remember(llmResponse, { userId: "user-123" });
|
|
109
|
+
* // → Auto-extracts: "theme set to dark mode", "nut allergy"
|
|
110
|
+
* // → Stored as preferences
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
remember(content: string, options?: {
|
|
114
|
+
userId?: string;
|
|
115
|
+
sessionId?: string;
|
|
116
|
+
project?: string;
|
|
117
|
+
}): Promise<{
|
|
118
|
+
success: boolean;
|
|
119
|
+
memoryId?: string;
|
|
120
|
+
}>;
|
|
121
|
+
/**
|
|
122
|
+
* Alias for remember() - same thing
|
|
123
|
+
*/
|
|
124
|
+
capture(content: string, options?: {
|
|
125
|
+
userId?: string;
|
|
126
|
+
sessionId?: string;
|
|
127
|
+
project?: string;
|
|
128
|
+
}): Promise<{
|
|
129
|
+
success: boolean;
|
|
130
|
+
memoryId?: string;
|
|
131
|
+
}>;
|
|
132
|
+
/**
|
|
133
|
+
* Capture from multiple messages (e.g., full conversation)
|
|
134
|
+
*/
|
|
135
|
+
captureSession(messages: Array<{
|
|
136
|
+
role: string;
|
|
137
|
+
content: string;
|
|
138
|
+
}>, options?: {
|
|
139
|
+
userId?: string;
|
|
140
|
+
sessionId?: string;
|
|
141
|
+
project?: string;
|
|
142
|
+
}): Promise<{
|
|
143
|
+
success: boolean;
|
|
144
|
+
extracted: number;
|
|
145
|
+
}>;
|
|
146
|
+
/**
|
|
147
|
+
* Direct access to WhisperContext for advanced usage
|
|
148
|
+
*/
|
|
149
|
+
raw(): WhisperContext;
|
|
150
|
+
}
|
|
151
|
+
|
|
1
152
|
/**
|
|
2
153
|
* Whisper Context SDK
|
|
3
154
|
* TypeScript SDK for the Whisper Context API
|
|
@@ -611,4 +762,4 @@ declare class WhisperContext {
|
|
|
611
762
|
};
|
|
612
763
|
}
|
|
613
764
|
|
|
614
|
-
export { type Memory, type Project, type QueryParams, type QueryResult, type Source, type WhisperConfig, WhisperContext, WhisperError, type WhisperErrorCode, WhisperContext as default };
|
|
765
|
+
export { type Memory, type Project, type QueryParams, type QueryResult, type Source, Whisper, type WhisperConfig, WhisperContext, Whisper as WhisperDefault, WhisperError, type WhisperErrorCode, WhisperContext as default };
|
package/index.js
CHANGED
|
@@ -20,11 +20,173 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// ../src/sdk/index.ts
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
|
+
Whisper: () => Whisper,
|
|
23
24
|
WhisperContext: () => WhisperContext,
|
|
25
|
+
WhisperDefault: () => whisper_agent_default,
|
|
24
26
|
WhisperError: () => WhisperError,
|
|
25
27
|
default: () => index_default
|
|
26
28
|
});
|
|
27
29
|
module.exports = __toCommonJS(index_exports);
|
|
30
|
+
|
|
31
|
+
// ../src/sdk/whisper-agent.ts
|
|
32
|
+
var Whisper = class {
|
|
33
|
+
client;
|
|
34
|
+
options;
|
|
35
|
+
sessionId;
|
|
36
|
+
userId;
|
|
37
|
+
constructor(options) {
|
|
38
|
+
if (!options.apiKey) {
|
|
39
|
+
throw new Error("API key is required");
|
|
40
|
+
}
|
|
41
|
+
const clientConfig = {
|
|
42
|
+
apiKey: options.apiKey,
|
|
43
|
+
baseUrl: options.baseUrl,
|
|
44
|
+
project: options.project || "default"
|
|
45
|
+
};
|
|
46
|
+
if (options.orgId) clientConfig.orgId = options.orgId;
|
|
47
|
+
if (options.timeoutMs) clientConfig.timeoutMs = options.timeoutMs;
|
|
48
|
+
if (options.retry) clientConfig.retry = options.retry;
|
|
49
|
+
this.client = new WhisperContext(clientConfig);
|
|
50
|
+
const finalRetry = options.retry || { maxAttempts: 3, baseDelayMs: 250, maxDelayMs: 2e3 };
|
|
51
|
+
this.options = {
|
|
52
|
+
apiKey: options.apiKey,
|
|
53
|
+
baseUrl: options.baseUrl || "https://context.usewhisper.dev",
|
|
54
|
+
project: options.project || "default",
|
|
55
|
+
orgId: options.orgId || "",
|
|
56
|
+
timeoutMs: options.timeoutMs || 15e3,
|
|
57
|
+
retry: finalRetry,
|
|
58
|
+
contextLimit: options.contextLimit ?? 10,
|
|
59
|
+
memoryTypes: options.memoryTypes ?? ["factual", "preference", "event", "goal", "relationship", "opinion", "instruction"],
|
|
60
|
+
contextPrefix: options.contextPrefix ?? "Relevant context:"
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Set session ID for conversation tracking
|
|
65
|
+
*/
|
|
66
|
+
session(sessionId) {
|
|
67
|
+
this.sessionId = sessionId;
|
|
68
|
+
return this;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Set user ID for user-specific memories
|
|
72
|
+
*/
|
|
73
|
+
user(userId) {
|
|
74
|
+
this.userId = userId;
|
|
75
|
+
return this;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Get relevant context BEFORE your LLM call
|
|
79
|
+
*
|
|
80
|
+
* @param query - What you want to know / user question
|
|
81
|
+
* @returns Context string and raw results
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```typescript
|
|
85
|
+
* const { context, results, count } = await whisper.getContext(
|
|
86
|
+
* "What are user's preferences?",
|
|
87
|
+
* { userId: "user-123" }
|
|
88
|
+
* );
|
|
89
|
+
*
|
|
90
|
+
* // Results: [
|
|
91
|
+
* // { content: "User prefers dark mode", type: "preference", score: 0.95 },
|
|
92
|
+
* // { content: "Allergic to nuts", type: "factual", score: 0.89 }
|
|
93
|
+
* // ]
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
async getContext(query, options) {
|
|
97
|
+
const result = await this.client.query({
|
|
98
|
+
project: options?.project ?? this.options.project,
|
|
99
|
+
query,
|
|
100
|
+
top_k: options?.limit ?? this.options.contextLimit,
|
|
101
|
+
include_memories: true,
|
|
102
|
+
user_id: options?.userId ?? this.userId,
|
|
103
|
+
session_id: options?.sessionId ?? this.sessionId
|
|
104
|
+
});
|
|
105
|
+
const context = result.results.map((r, i) => `[${i + 1}] ${r.content}`).join("\n");
|
|
106
|
+
return {
|
|
107
|
+
context: context ? `${this.options.contextPrefix}
|
|
108
|
+
${context}` : "",
|
|
109
|
+
results: result.results,
|
|
110
|
+
count: result.meta.total
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Remember what happened AFTER your LLM response
|
|
115
|
+
*
|
|
116
|
+
* Fire-and-forget - doesn't block your response
|
|
117
|
+
*
|
|
118
|
+
* @param content - What your LLM responded with
|
|
119
|
+
* @returns Promise that resolves when stored (or fails silently)
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
* ```typescript
|
|
123
|
+
* const llmResponse = "I've set your theme to dark mode and removed nuts from recommendations.";
|
|
124
|
+
*
|
|
125
|
+
* await whisper.remember(llmResponse, { userId: "user-123" });
|
|
126
|
+
* // → Auto-extracts: "theme set to dark mode", "nut allergy"
|
|
127
|
+
* // → Stored as preferences
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
async remember(content, options) {
|
|
131
|
+
if (!content || content.length < 5) {
|
|
132
|
+
return { success: false };
|
|
133
|
+
}
|
|
134
|
+
try {
|
|
135
|
+
const result = await this.client.addMemory({
|
|
136
|
+
project: options?.project ?? this.options.project,
|
|
137
|
+
content,
|
|
138
|
+
user_id: options?.userId ?? this.userId,
|
|
139
|
+
session_id: options?.sessionId ?? this.sessionId
|
|
140
|
+
});
|
|
141
|
+
return {
|
|
142
|
+
success: true,
|
|
143
|
+
memoryId: result?.id
|
|
144
|
+
};
|
|
145
|
+
} catch (error) {
|
|
146
|
+
console.error("[Whisper] Remember failed:", error);
|
|
147
|
+
return { success: false };
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Alias for remember() - same thing
|
|
152
|
+
*/
|
|
153
|
+
async capture(content, options) {
|
|
154
|
+
return this.remember(content, options);
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Capture from multiple messages (e.g., full conversation)
|
|
158
|
+
*/
|
|
159
|
+
async captureSession(messages, options) {
|
|
160
|
+
try {
|
|
161
|
+
const result = await this.client.ingestSession({
|
|
162
|
+
project: options?.project ?? this.options.project,
|
|
163
|
+
session_id: options?.sessionId ?? this.sessionId ?? "default",
|
|
164
|
+
user_id: options?.userId ?? this.userId,
|
|
165
|
+
messages: messages.filter((m) => m.role !== "system").map((m) => ({
|
|
166
|
+
role: m.role,
|
|
167
|
+
content: m.content,
|
|
168
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
169
|
+
}))
|
|
170
|
+
});
|
|
171
|
+
return {
|
|
172
|
+
success: true,
|
|
173
|
+
extracted: result?.memories_created ?? 0
|
|
174
|
+
};
|
|
175
|
+
} catch (error) {
|
|
176
|
+
console.error("[Whisper] Session capture failed:", error);
|
|
177
|
+
return { success: false, extracted: 0 };
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Direct access to WhisperContext for advanced usage
|
|
182
|
+
*/
|
|
183
|
+
raw() {
|
|
184
|
+
return this.client;
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
var whisper_agent_default = Whisper;
|
|
188
|
+
|
|
189
|
+
// ../src/sdk/index.ts
|
|
28
190
|
var WhisperError = class extends Error {
|
|
29
191
|
code;
|
|
30
192
|
status;
|
|
@@ -640,6 +802,8 @@ var WhisperContext = class _WhisperContext {
|
|
|
640
802
|
var index_default = WhisperContext;
|
|
641
803
|
// Annotate the CommonJS export names for ESM import in node:
|
|
642
804
|
0 && (module.exports = {
|
|
805
|
+
Whisper,
|
|
643
806
|
WhisperContext,
|
|
807
|
+
WhisperDefault,
|
|
644
808
|
WhisperError
|
|
645
809
|
});
|
package/index.mjs
CHANGED
|
@@ -1,3 +1,161 @@
|
|
|
1
|
+
// ../src/sdk/whisper-agent.ts
|
|
2
|
+
var Whisper = class {
|
|
3
|
+
client;
|
|
4
|
+
options;
|
|
5
|
+
sessionId;
|
|
6
|
+
userId;
|
|
7
|
+
constructor(options) {
|
|
8
|
+
if (!options.apiKey) {
|
|
9
|
+
throw new Error("API key is required");
|
|
10
|
+
}
|
|
11
|
+
const clientConfig = {
|
|
12
|
+
apiKey: options.apiKey,
|
|
13
|
+
baseUrl: options.baseUrl,
|
|
14
|
+
project: options.project || "default"
|
|
15
|
+
};
|
|
16
|
+
if (options.orgId) clientConfig.orgId = options.orgId;
|
|
17
|
+
if (options.timeoutMs) clientConfig.timeoutMs = options.timeoutMs;
|
|
18
|
+
if (options.retry) clientConfig.retry = options.retry;
|
|
19
|
+
this.client = new WhisperContext(clientConfig);
|
|
20
|
+
const finalRetry = options.retry || { maxAttempts: 3, baseDelayMs: 250, maxDelayMs: 2e3 };
|
|
21
|
+
this.options = {
|
|
22
|
+
apiKey: options.apiKey,
|
|
23
|
+
baseUrl: options.baseUrl || "https://context.usewhisper.dev",
|
|
24
|
+
project: options.project || "default",
|
|
25
|
+
orgId: options.orgId || "",
|
|
26
|
+
timeoutMs: options.timeoutMs || 15e3,
|
|
27
|
+
retry: finalRetry,
|
|
28
|
+
contextLimit: options.contextLimit ?? 10,
|
|
29
|
+
memoryTypes: options.memoryTypes ?? ["factual", "preference", "event", "goal", "relationship", "opinion", "instruction"],
|
|
30
|
+
contextPrefix: options.contextPrefix ?? "Relevant context:"
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Set session ID for conversation tracking
|
|
35
|
+
*/
|
|
36
|
+
session(sessionId) {
|
|
37
|
+
this.sessionId = sessionId;
|
|
38
|
+
return this;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Set user ID for user-specific memories
|
|
42
|
+
*/
|
|
43
|
+
user(userId) {
|
|
44
|
+
this.userId = userId;
|
|
45
|
+
return this;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Get relevant context BEFORE your LLM call
|
|
49
|
+
*
|
|
50
|
+
* @param query - What you want to know / user question
|
|
51
|
+
* @returns Context string and raw results
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```typescript
|
|
55
|
+
* const { context, results, count } = await whisper.getContext(
|
|
56
|
+
* "What are user's preferences?",
|
|
57
|
+
* { userId: "user-123" }
|
|
58
|
+
* );
|
|
59
|
+
*
|
|
60
|
+
* // Results: [
|
|
61
|
+
* // { content: "User prefers dark mode", type: "preference", score: 0.95 },
|
|
62
|
+
* // { content: "Allergic to nuts", type: "factual", score: 0.89 }
|
|
63
|
+
* // ]
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
async getContext(query, options) {
|
|
67
|
+
const result = await this.client.query({
|
|
68
|
+
project: options?.project ?? this.options.project,
|
|
69
|
+
query,
|
|
70
|
+
top_k: options?.limit ?? this.options.contextLimit,
|
|
71
|
+
include_memories: true,
|
|
72
|
+
user_id: options?.userId ?? this.userId,
|
|
73
|
+
session_id: options?.sessionId ?? this.sessionId
|
|
74
|
+
});
|
|
75
|
+
const context = result.results.map((r, i) => `[${i + 1}] ${r.content}`).join("\n");
|
|
76
|
+
return {
|
|
77
|
+
context: context ? `${this.options.contextPrefix}
|
|
78
|
+
${context}` : "",
|
|
79
|
+
results: result.results,
|
|
80
|
+
count: result.meta.total
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Remember what happened AFTER your LLM response
|
|
85
|
+
*
|
|
86
|
+
* Fire-and-forget - doesn't block your response
|
|
87
|
+
*
|
|
88
|
+
* @param content - What your LLM responded with
|
|
89
|
+
* @returns Promise that resolves when stored (or fails silently)
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```typescript
|
|
93
|
+
* const llmResponse = "I've set your theme to dark mode and removed nuts from recommendations.";
|
|
94
|
+
*
|
|
95
|
+
* await whisper.remember(llmResponse, { userId: "user-123" });
|
|
96
|
+
* // → Auto-extracts: "theme set to dark mode", "nut allergy"
|
|
97
|
+
* // → Stored as preferences
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
async remember(content, options) {
|
|
101
|
+
if (!content || content.length < 5) {
|
|
102
|
+
return { success: false };
|
|
103
|
+
}
|
|
104
|
+
try {
|
|
105
|
+
const result = await this.client.addMemory({
|
|
106
|
+
project: options?.project ?? this.options.project,
|
|
107
|
+
content,
|
|
108
|
+
user_id: options?.userId ?? this.userId,
|
|
109
|
+
session_id: options?.sessionId ?? this.sessionId
|
|
110
|
+
});
|
|
111
|
+
return {
|
|
112
|
+
success: true,
|
|
113
|
+
memoryId: result?.id
|
|
114
|
+
};
|
|
115
|
+
} catch (error) {
|
|
116
|
+
console.error("[Whisper] Remember failed:", error);
|
|
117
|
+
return { success: false };
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Alias for remember() - same thing
|
|
122
|
+
*/
|
|
123
|
+
async capture(content, options) {
|
|
124
|
+
return this.remember(content, options);
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Capture from multiple messages (e.g., full conversation)
|
|
128
|
+
*/
|
|
129
|
+
async captureSession(messages, options) {
|
|
130
|
+
try {
|
|
131
|
+
const result = await this.client.ingestSession({
|
|
132
|
+
project: options?.project ?? this.options.project,
|
|
133
|
+
session_id: options?.sessionId ?? this.sessionId ?? "default",
|
|
134
|
+
user_id: options?.userId ?? this.userId,
|
|
135
|
+
messages: messages.filter((m) => m.role !== "system").map((m) => ({
|
|
136
|
+
role: m.role,
|
|
137
|
+
content: m.content,
|
|
138
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
139
|
+
}))
|
|
140
|
+
});
|
|
141
|
+
return {
|
|
142
|
+
success: true,
|
|
143
|
+
extracted: result?.memories_created ?? 0
|
|
144
|
+
};
|
|
145
|
+
} catch (error) {
|
|
146
|
+
console.error("[Whisper] Session capture failed:", error);
|
|
147
|
+
return { success: false, extracted: 0 };
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Direct access to WhisperContext for advanced usage
|
|
152
|
+
*/
|
|
153
|
+
raw() {
|
|
154
|
+
return this.client;
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
var whisper_agent_default = Whisper;
|
|
158
|
+
|
|
1
159
|
// ../src/sdk/index.ts
|
|
2
160
|
var WhisperError = class extends Error {
|
|
3
161
|
code;
|
|
@@ -613,7 +771,9 @@ var WhisperContext = class _WhisperContext {
|
|
|
613
771
|
};
|
|
614
772
|
var index_default = WhisperContext;
|
|
615
773
|
export {
|
|
774
|
+
Whisper,
|
|
616
775
|
WhisperContext,
|
|
776
|
+
whisper_agent_default as WhisperDefault,
|
|
617
777
|
WhisperError,
|
|
618
778
|
index_default as default
|
|
619
779
|
};
|
package/package.json
CHANGED
|
@@ -1,56 +1,56 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@usewhisper/sdk",
|
|
3
|
-
"version": "
|
|
4
|
-
"scripts": {
|
|
5
|
-
"build": "tsup ../src/sdk/index.ts --format esm,cjs --dts --out-dir .",
|
|
6
|
-
"prepublishOnly": "npm run build"
|
|
7
|
-
},
|
|
8
|
-
"description": "TypeScript SDK for Whisper Context API - Add reliable context to your AI agents",
|
|
9
|
-
"main": "index.js",
|
|
10
|
-
"module": "index.mjs",
|
|
11
|
-
"types": "index.d.ts",
|
|
12
|
-
"exports": {
|
|
13
|
-
".": {
|
|
14
|
-
"types": "./index.d.ts",
|
|
15
|
-
"import": "./index.mjs",
|
|
16
|
-
"require": "./index.js"
|
|
17
|
-
}
|
|
18
|
-
},
|
|
19
|
-
"files": [
|
|
20
|
-
"index.js",
|
|
21
|
-
"index.mjs",
|
|
22
|
-
"index.d.ts",
|
|
23
|
-
"index.d.mts",
|
|
24
|
-
"README.md"
|
|
25
|
-
],
|
|
26
|
-
"keywords": [
|
|
27
|
-
"whisper",
|
|
28
|
-
"context",
|
|
29
|
-
"ai",
|
|
30
|
-
"llm",
|
|
31
|
-
"rag",
|
|
32
|
-
"embeddings",
|
|
33
|
-
"vector-search",
|
|
34
|
-
"knowledge-graph",
|
|
35
|
-
"semantic-search"
|
|
36
|
-
],
|
|
37
|
-
"author": "Whisper",
|
|
38
|
-
"license": "MIT",
|
|
39
|
-
"repository": {
|
|
40
|
-
"type": "git",
|
|
41
|
-
"url": "https://github.com/Alixus/"
|
|
42
|
-
},
|
|
43
|
-
"homepage": "https://usewhisper.dev",
|
|
44
|
-
"bugs": {
|
|
45
|
-
"url": "https://github.com/Alinxus/whisper/issues"
|
|
46
|
-
},
|
|
47
|
-
"devDependencies": {
|
|
48
|
-
"@types/node": "^22.0.0",
|
|
49
|
-
"tsup": "^8.3.0",
|
|
50
|
-
"typescript": "^5.7.0"
|
|
51
|
-
},
|
|
52
|
-
"peerDependencies": {},
|
|
53
|
-
"engines": {
|
|
54
|
-
"node": ">=18.0.0"
|
|
55
|
-
}
|
|
56
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@usewhisper/sdk",
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"scripts": {
|
|
5
|
+
"build": "tsup ../src/sdk/index.ts --format esm,cjs --dts --out-dir .",
|
|
6
|
+
"prepublishOnly": "npm run build"
|
|
7
|
+
},
|
|
8
|
+
"description": "TypeScript SDK for Whisper Context API - Add reliable context to your AI agents",
|
|
9
|
+
"main": "index.js",
|
|
10
|
+
"module": "index.mjs",
|
|
11
|
+
"types": "index.d.ts",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./index.d.ts",
|
|
15
|
+
"import": "./index.mjs",
|
|
16
|
+
"require": "./index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"index.js",
|
|
21
|
+
"index.mjs",
|
|
22
|
+
"index.d.ts",
|
|
23
|
+
"index.d.mts",
|
|
24
|
+
"README.md"
|
|
25
|
+
],
|
|
26
|
+
"keywords": [
|
|
27
|
+
"whisper",
|
|
28
|
+
"context",
|
|
29
|
+
"ai",
|
|
30
|
+
"llm",
|
|
31
|
+
"rag",
|
|
32
|
+
"embeddings",
|
|
33
|
+
"vector-search",
|
|
34
|
+
"knowledge-graph",
|
|
35
|
+
"semantic-search"
|
|
36
|
+
],
|
|
37
|
+
"author": "Whisper",
|
|
38
|
+
"license": "MIT",
|
|
39
|
+
"repository": {
|
|
40
|
+
"type": "git",
|
|
41
|
+
"url": "https://github.com/Alixus/"
|
|
42
|
+
},
|
|
43
|
+
"homepage": "https://usewhisper.dev",
|
|
44
|
+
"bugs": {
|
|
45
|
+
"url": "https://github.com/Alinxus/whisper/issues"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@types/node": "^22.0.0",
|
|
49
|
+
"tsup": "^8.3.0",
|
|
50
|
+
"typescript": "^5.7.0"
|
|
51
|
+
},
|
|
52
|
+
"peerDependencies": {},
|
|
53
|
+
"engines": {
|
|
54
|
+
"node": ">=18.0.0"
|
|
55
|
+
}
|
|
56
|
+
}
|