@cilow/sdk 0.2.0 → 0.2.1
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 +412 -199
- package/dist/client.d.mts +224 -0
- package/dist/client.d.ts +224 -0
- package/dist/client.js +509 -0
- package/dist/client.js.map +1 -0
- package/dist/client.mjs +505 -0
- package/dist/client.mjs.map +1 -0
- package/dist/index.d.mts +94 -390
- package/dist/index.d.ts +94 -390
- package/dist/index.js +745 -350
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +732 -318
- package/dist/index.mjs.map +1 -0
- package/dist/providers/langchain.js +821 -0
- package/dist/providers/langchain.js.map +1 -0
- package/dist/providers/langchain.mjs +816 -0
- package/dist/providers/langchain.mjs.map +1 -0
- package/dist/providers/openai.js +737 -0
- package/dist/providers/openai.js.map +1 -0
- package/dist/providers/openai.mjs +732 -0
- package/dist/providers/openai.mjs.map +1 -0
- package/dist/providers/vercel.js +866 -0
- package/dist/providers/vercel.js.map +1 -0
- package/dist/providers/vercel.mjs +860 -0
- package/dist/providers/vercel.mjs.map +1 -0
- package/dist/react/hooks.d.mts +327 -0
- package/dist/react/hooks.d.ts +327 -0
- package/dist/react/hooks.js +1183 -0
- package/dist/react/hooks.js.map +1 -0
- package/dist/react/hooks.mjs +1172 -0
- package/dist/react/hooks.mjs.map +1 -0
- package/dist/types.d.mts +494 -0
- package/dist/types.d.ts +494 -0
- package/dist/types.js +18 -0
- package/dist/types.js.map +1 -0
- package/dist/types.mjs +14 -0
- package/dist/types.mjs.map +1 -0
- package/dist/websocket.d.mts +160 -0
- package/dist/websocket.d.ts +160 -0
- package/dist/websocket.js +342 -0
- package/dist/websocket.js.map +1 -0
- package/dist/websocket.mjs +339 -0
- package/dist/websocket.mjs.map +1 -0
- package/package.json +90 -31
- package/LICENSE +0 -21
package/README.md
CHANGED
|
@@ -1,16 +1,6 @@
|
|
|
1
1
|
# @cilow/sdk
|
|
2
2
|
|
|
3
|
-
TypeScript
|
|
4
|
-
|
|
5
|
-
## Features
|
|
6
|
-
|
|
7
|
-
- **Semantic Memory Storage** - Store and retrieve memories using vector embeddings
|
|
8
|
-
- **Multi-tier Storage** - Hot/Warm/Cold tiers with automatic optimization
|
|
9
|
-
- **Tag-based Organization** - Organize memories with flexible tagging
|
|
10
|
-
- **Full-text + Semantic Search** - Hybrid search combining BM25 and embeddings
|
|
11
|
-
- **Knowledge Graph** - Entity extraction and relationship mapping
|
|
12
|
-
- **Multi-tenant Support** - User isolation for secure multi-user apps
|
|
13
|
-
- **TypeScript First** - Full type safety with comprehensive types
|
|
3
|
+
Complete TypeScript SDK for Cilow - AI Memory Infrastructure with multi-framework support.
|
|
14
4
|
|
|
15
5
|
## Installation
|
|
16
6
|
|
|
@@ -25,297 +15,520 @@ pnpm add @cilow/sdk
|
|
|
25
15
|
## Quick Start
|
|
26
16
|
|
|
27
17
|
```typescript
|
|
28
|
-
import {
|
|
18
|
+
import { Cilow } from '@cilow/sdk';
|
|
29
19
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
apiKey: '
|
|
33
|
-
baseUrl: 'https://api.cilow.ai', // or your self-hosted URL
|
|
20
|
+
const cilow = new Cilow({
|
|
21
|
+
apiUrl: 'https://api.cilow.ai',
|
|
22
|
+
apiKey: 'your-api-key'
|
|
34
23
|
});
|
|
35
24
|
|
|
36
|
-
//
|
|
37
|
-
|
|
38
|
-
content: 'User prefers TypeScript over JavaScript for backend development',
|
|
39
|
-
tags: ['preference', 'programming', 'backend'],
|
|
40
|
-
});
|
|
25
|
+
// Store a memory
|
|
26
|
+
await cilow.remember("User prefers dark mode", { tags: ["preference", "ui"] });
|
|
41
27
|
|
|
42
|
-
// Search memories
|
|
43
|
-
const
|
|
44
|
-
query: 'What programming languages does the user prefer?',
|
|
45
|
-
limit: 5,
|
|
46
|
-
});
|
|
28
|
+
// Search memories
|
|
29
|
+
const memories = await cilow.recall("user preferences");
|
|
47
30
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
// {
|
|
51
|
-
// memoryId: 'mem_xxx',
|
|
52
|
-
// content: 'User prefers TypeScript over JavaScript...',
|
|
53
|
-
// similarity: 0.92,
|
|
54
|
-
// score: 0.18,
|
|
55
|
-
// tags: ['preference', 'programming', 'backend']
|
|
56
|
-
// }
|
|
57
|
-
// ]
|
|
31
|
+
// Delete memories
|
|
32
|
+
await cilow.forget({ tags: ["temporary"] });
|
|
58
33
|
```
|
|
59
34
|
|
|
60
|
-
##
|
|
35
|
+
## Features
|
|
61
36
|
|
|
62
|
-
|
|
37
|
+
- **Simple API**: `remember`, `recall`, `forget` for easy memory management
|
|
38
|
+
- **Full TypeScript Support**: Complete type definitions for all APIs
|
|
39
|
+
- **Framework Integrations**: Vercel AI SDK, OpenAI, LangChain
|
|
40
|
+
- **React Hooks**: `useMemory`, `useRecall`, `useMemoryContext`
|
|
41
|
+
- **WebSocket Support**: Real-time memory updates and graph changes
|
|
42
|
+
- **Multi-tier Memory**: Hot, warm, cold memory tiers for optimal performance
|
|
63
43
|
|
|
64
|
-
|
|
65
|
-
const client = new CilowClient({
|
|
66
|
-
apiKey: 'cilow_your_api_key',
|
|
67
|
-
});
|
|
68
|
-
```
|
|
44
|
+
## Core Client
|
|
69
45
|
|
|
70
|
-
###
|
|
46
|
+
### Basic Operations
|
|
71
47
|
|
|
72
48
|
```typescript
|
|
73
|
-
|
|
74
|
-
|
|
49
|
+
import { Cilow } from '@cilow/sdk';
|
|
50
|
+
|
|
51
|
+
const cilow = new Cilow({
|
|
52
|
+
apiUrl: 'https://api.cilow.ai',
|
|
53
|
+
apiKey: 'your-api-key'
|
|
75
54
|
});
|
|
76
55
|
|
|
77
|
-
//
|
|
78
|
-
const
|
|
79
|
-
|
|
56
|
+
// Store a memory
|
|
57
|
+
const memoryId = await cilow.remember("Important information", {
|
|
58
|
+
tags: ["important"],
|
|
59
|
+
userId: "user-123",
|
|
60
|
+
metadata: { source: "chat" }
|
|
61
|
+
});
|
|
80
62
|
|
|
81
|
-
//
|
|
82
|
-
|
|
63
|
+
// Search memories with semantic similarity
|
|
64
|
+
const results = await cilow.recall("important information", {
|
|
65
|
+
limit: 10,
|
|
66
|
+
minRelevance: 0.5,
|
|
67
|
+
tags: ["important"]
|
|
68
|
+
});
|
|
83
69
|
|
|
84
|
-
|
|
70
|
+
// Delete memories
|
|
71
|
+
await cilow.forget({ memoryId: "mem-123" });
|
|
72
|
+
await cilow.forget({ tags: ["old"], userId: "user-123" });
|
|
73
|
+
```
|
|
85
74
|
|
|
86
|
-
###
|
|
75
|
+
### Advanced Operations
|
|
87
76
|
|
|
88
77
|
```typescript
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
78
|
+
// Get a specific memory
|
|
79
|
+
const memory = await cilow.getMemory("mem-123");
|
|
80
|
+
|
|
81
|
+
// Update a memory
|
|
82
|
+
const updated = await cilow.updateMemory("mem-123", {
|
|
83
|
+
content: "Updated content",
|
|
84
|
+
tags: ["updated"]
|
|
96
85
|
});
|
|
97
|
-
```
|
|
98
86
|
|
|
99
|
-
|
|
87
|
+
// List memories with pagination
|
|
88
|
+
const list = await cilow.listMemories({
|
|
89
|
+
limit: 20,
|
|
90
|
+
offset: 0,
|
|
91
|
+
userId: "user-123",
|
|
92
|
+
tags: ["preference"]
|
|
93
|
+
});
|
|
100
94
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
limit: 10,
|
|
106
|
-
minScore: 0.5,
|
|
95
|
+
// Get context for AI prompts
|
|
96
|
+
const context = await cilow.getContext("recent discussions", {
|
|
97
|
+
maxTokens: 2000,
|
|
98
|
+
userId: "user-123"
|
|
107
99
|
});
|
|
108
100
|
|
|
109
|
-
//
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
101
|
+
// Store conversation turns
|
|
102
|
+
await cilow.storeConversation({
|
|
103
|
+
userMessage: "What's the weather?",
|
|
104
|
+
assistantResponse: "The weather is sunny today.",
|
|
105
|
+
userId: "user-123",
|
|
106
|
+
sessionId: "session-abc"
|
|
114
107
|
});
|
|
108
|
+
|
|
109
|
+
// Get statistics
|
|
110
|
+
const stats = await cilow.getStats();
|
|
111
|
+
console.log(`Total memories: ${stats.totalMemories}`);
|
|
115
112
|
```
|
|
116
113
|
|
|
117
|
-
###
|
|
114
|
+
### Graph Operations
|
|
118
115
|
|
|
119
116
|
```typescript
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
117
|
+
// Create graph nodes
|
|
118
|
+
const node = await cilow.createGraphNode("Person", "John Doe", {
|
|
119
|
+
email: "john@example.com"
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
// Create relationships
|
|
123
|
+
const edge = await cilow.createGraphEdge(
|
|
124
|
+
node.id,
|
|
125
|
+
"other-node-id",
|
|
126
|
+
"KNOWS",
|
|
127
|
+
{ since: "2024" }
|
|
128
|
+
);
|
|
129
|
+
|
|
130
|
+
// Traverse the graph
|
|
131
|
+
const subgraph = await cilow.traverseGraph({
|
|
132
|
+
startNodeId: node.id,
|
|
133
|
+
maxDepth: 3,
|
|
134
|
+
relationshipTypes: ["KNOWS", "WORKS_WITH"]
|
|
123
135
|
});
|
|
124
136
|
```
|
|
125
137
|
|
|
126
|
-
|
|
138
|
+
## Vercel AI SDK Integration
|
|
127
139
|
|
|
128
140
|
```typescript
|
|
129
|
-
|
|
130
|
-
|
|
141
|
+
import { createCilowTools, createCilowContext } from '@cilow/sdk/providers/vercel';
|
|
142
|
+
import { streamText } from 'ai';
|
|
143
|
+
import { anthropic } from '@ai-sdk/anthropic';
|
|
144
|
+
|
|
145
|
+
// Create tools for AI to use
|
|
146
|
+
const tools = createCilowTools({
|
|
147
|
+
apiUrl: 'https://api.cilow.ai',
|
|
148
|
+
apiKey: 'your-key',
|
|
149
|
+
defaultUserId: 'user-123'
|
|
150
|
+
});
|
|
131
151
|
|
|
132
|
-
|
|
152
|
+
// Use with streamText
|
|
153
|
+
const result = await streamText({
|
|
154
|
+
model: anthropic('claude-sonnet-4-20250514'),
|
|
155
|
+
tools,
|
|
156
|
+
messages: [
|
|
157
|
+
{ role: 'user', content: 'What do you remember about me?' }
|
|
158
|
+
]
|
|
159
|
+
});
|
|
133
160
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
161
|
+
// Or use context injection
|
|
162
|
+
const getContext = createCilowContext({
|
|
163
|
+
apiUrl: 'https://api.cilow.ai',
|
|
164
|
+
apiKey: 'your-key',
|
|
165
|
+
maxTokens: 2000
|
|
166
|
+
});
|
|
137
167
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
168
|
+
const context = await getContext('previous discussions');
|
|
169
|
+
|
|
170
|
+
const response = await streamText({
|
|
171
|
+
model: anthropic('claude-sonnet-4-20250514'),
|
|
172
|
+
system: `You are a helpful assistant.\n\nContext:\n${context.context}`,
|
|
173
|
+
prompt: 'Continue our discussion'
|
|
143
174
|
});
|
|
144
175
|
```
|
|
145
176
|
|
|
146
|
-
###
|
|
177
|
+
### Context Middleware
|
|
147
178
|
|
|
148
179
|
```typescript
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
//
|
|
157
|
-
|
|
158
|
-
|
|
180
|
+
import { createContextMiddleware } from '@cilow/sdk/providers/vercel';
|
|
181
|
+
|
|
182
|
+
const withMemory = createContextMiddleware({
|
|
183
|
+
apiUrl: 'https://api.cilow.ai',
|
|
184
|
+
apiKey: 'your-key'
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
// Automatically inject memory context
|
|
188
|
+
const messagesWithContext = await withMemory(originalMessages, {
|
|
189
|
+
userId: 'user-123',
|
|
190
|
+
maxTokens: 2000
|
|
191
|
+
});
|
|
159
192
|
```
|
|
160
193
|
|
|
161
|
-
##
|
|
194
|
+
## OpenAI SDK Integration
|
|
162
195
|
|
|
163
196
|
```typescript
|
|
164
|
-
|
|
165
|
-
|
|
197
|
+
import OpenAI from 'openai';
|
|
198
|
+
import { createCilowOpenAI } from '@cilow/sdk/providers/openai';
|
|
166
199
|
|
|
167
|
-
|
|
168
|
-
await client.removeTags(memoryId, ['follow-up']);
|
|
200
|
+
const openai = new OpenAI();
|
|
169
201
|
|
|
170
|
-
|
|
171
|
-
|
|
202
|
+
const cilowAI = createCilowOpenAI(openai, {
|
|
203
|
+
apiUrl: 'https://api.cilow.ai',
|
|
204
|
+
apiKey: 'cilow-key',
|
|
205
|
+
autoStore: true // Automatically store conversations
|
|
206
|
+
});
|
|
172
207
|
|
|
173
|
-
//
|
|
174
|
-
const
|
|
208
|
+
// Memory context is automatically injected
|
|
209
|
+
const { completion, memoriesUsed } = await cilowAI.chat.completions.create({
|
|
210
|
+
model: 'gpt-4',
|
|
211
|
+
messages: [
|
|
212
|
+
{ role: 'user', content: 'Based on our previous discussions, what should I focus on?' }
|
|
213
|
+
]
|
|
214
|
+
});
|
|
175
215
|
|
|
176
|
-
|
|
177
|
-
|
|
216
|
+
console.log(`Used ${memoriesUsed} memories for context`);
|
|
217
|
+
console.log(completion.choices[0].message.content);
|
|
178
218
|
|
|
179
|
-
//
|
|
180
|
-
|
|
219
|
+
// Direct memory access
|
|
220
|
+
await cilowAI.remember("Important insight", { tags: ["insight"] });
|
|
221
|
+
const memories = await cilowAI.recall("insights");
|
|
181
222
|
```
|
|
182
223
|
|
|
183
|
-
##
|
|
224
|
+
## LangChain Integration
|
|
225
|
+
|
|
226
|
+
### Memory Class
|
|
184
227
|
|
|
185
228
|
```typescript
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
229
|
+
import { CilowMemory } from '@cilow/sdk/providers/langchain';
|
|
230
|
+
import { ConversationChain } from 'langchain/chains';
|
|
231
|
+
import { ChatOpenAI } from '@langchain/openai';
|
|
232
|
+
|
|
233
|
+
const memory = new CilowMemory({
|
|
234
|
+
apiUrl: 'https://api.cilow.ai',
|
|
235
|
+
apiKey: 'cilow-key',
|
|
236
|
+
userId: 'user-123',
|
|
237
|
+
sessionId: 'session-abc'
|
|
191
238
|
});
|
|
192
239
|
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
context: 'User is preparing for Q1 planning',
|
|
240
|
+
const chain = new ConversationChain({
|
|
241
|
+
llm: new ChatOpenAI(),
|
|
242
|
+
memory
|
|
197
243
|
});
|
|
198
244
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
// List all agents
|
|
203
|
-
const agents = await client.listAgents();
|
|
204
|
-
|
|
205
|
-
// Delete agent
|
|
206
|
-
await client.deleteAgent(agentId);
|
|
245
|
+
const response = await chain.call({
|
|
246
|
+
input: 'What did we discuss last time?'
|
|
247
|
+
});
|
|
207
248
|
```
|
|
208
249
|
|
|
209
|
-
|
|
250
|
+
### Retriever for RAG
|
|
210
251
|
|
|
211
252
|
```typescript
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
name: 'Production API Key',
|
|
215
|
-
expiresAt: new Date('2025-01-01'),
|
|
216
|
-
});
|
|
253
|
+
import { CilowRetriever } from '@cilow/sdk/providers/langchain';
|
|
254
|
+
import { RetrievalQAChain } from 'langchain/chains';
|
|
217
255
|
|
|
218
|
-
|
|
219
|
-
|
|
256
|
+
const retriever = new CilowRetriever({
|
|
257
|
+
apiUrl: 'https://api.cilow.ai',
|
|
258
|
+
apiKey: 'cilow-key',
|
|
259
|
+
topK: 5
|
|
260
|
+
});
|
|
220
261
|
|
|
221
|
-
|
|
222
|
-
|
|
262
|
+
const chain = RetrievalQAChain.fromLLM(
|
|
263
|
+
new ChatOpenAI(),
|
|
264
|
+
retriever
|
|
265
|
+
);
|
|
223
266
|
|
|
224
|
-
|
|
225
|
-
|
|
267
|
+
const answer = await chain.call({
|
|
268
|
+
query: 'What are the key points from our meetings?'
|
|
269
|
+
});
|
|
226
270
|
```
|
|
227
271
|
|
|
228
|
-
|
|
272
|
+
### Vector Store
|
|
229
273
|
|
|
230
274
|
```typescript
|
|
231
|
-
|
|
232
|
-
const sessions = await client.listSessions();
|
|
275
|
+
import { CilowVectorStore } from '@cilow/sdk/providers/langchain';
|
|
233
276
|
|
|
234
|
-
|
|
235
|
-
|
|
277
|
+
const vectorStore = new CilowVectorStore({
|
|
278
|
+
apiUrl: 'https://api.cilow.ai',
|
|
279
|
+
apiKey: 'cilow-key'
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
// Add documents
|
|
283
|
+
await vectorStore.addDocuments([
|
|
284
|
+
{ pageContent: 'Meeting notes from Monday', metadata: { topic: 'standup' } },
|
|
285
|
+
{ pageContent: 'Product requirements document', metadata: { topic: 'product' } }
|
|
286
|
+
]);
|
|
236
287
|
|
|
237
|
-
//
|
|
238
|
-
await
|
|
288
|
+
// Similarity search
|
|
289
|
+
const results = await vectorStore.similaritySearch('standup notes', 5);
|
|
239
290
|
|
|
240
|
-
//
|
|
241
|
-
await
|
|
291
|
+
// With scores
|
|
292
|
+
const resultsWithScores = await vectorStore.similaritySearchWithScore('standup notes');
|
|
242
293
|
```
|
|
243
294
|
|
|
244
|
-
##
|
|
295
|
+
## React Hooks
|
|
245
296
|
|
|
246
|
-
```
|
|
297
|
+
```tsx
|
|
247
298
|
import {
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
299
|
+
CilowProvider,
|
|
300
|
+
useMemory,
|
|
301
|
+
useRecall,
|
|
302
|
+
useMemoryContext,
|
|
303
|
+
useMemorySubscription,
|
|
304
|
+
useDebouncedSearch
|
|
305
|
+
} from '@cilow/sdk/react';
|
|
306
|
+
|
|
307
|
+
// Wrap your app with the provider
|
|
308
|
+
function App() {
|
|
309
|
+
return (
|
|
310
|
+
<CilowProvider
|
|
311
|
+
apiUrl="https://api.cilow.ai"
|
|
312
|
+
apiKey="your-key"
|
|
313
|
+
userId="user-123"
|
|
314
|
+
enableWebSocket={true}
|
|
315
|
+
>
|
|
316
|
+
<YourApp />
|
|
317
|
+
</CilowProvider>
|
|
318
|
+
);
|
|
319
|
+
}
|
|
253
320
|
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
321
|
+
// Use memory operations
|
|
322
|
+
function MemoryManager() {
|
|
323
|
+
const { remember, forget, isLoading, error } = useMemory();
|
|
324
|
+
|
|
325
|
+
const handleSave = async () => {
|
|
326
|
+
await remember("Important note", { tags: ["note"] });
|
|
327
|
+
};
|
|
328
|
+
|
|
329
|
+
return (
|
|
330
|
+
<button onClick={handleSave} disabled={isLoading}>
|
|
331
|
+
Save Memory
|
|
332
|
+
</button>
|
|
333
|
+
);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// Search memories
|
|
337
|
+
function SearchComponent() {
|
|
338
|
+
const { data, search, isLoading } = useRecall();
|
|
339
|
+
|
|
340
|
+
const handleSearch = () => {
|
|
341
|
+
search("user preferences", { limit: 10 });
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
return (
|
|
345
|
+
<div>
|
|
346
|
+
<button onClick={handleSearch}>Search</button>
|
|
347
|
+
{data.map(result => (
|
|
348
|
+
<div key={result.memory.id}>
|
|
349
|
+
<p>{result.memory.content}</p>
|
|
350
|
+
<span>Score: {(result.score * 100).toFixed(0)}%</span>
|
|
351
|
+
</div>
|
|
352
|
+
))}
|
|
353
|
+
</div>
|
|
354
|
+
);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
// Debounced search
|
|
358
|
+
function LiveSearch() {
|
|
359
|
+
const { query, setQuery, results, isLoading } = useDebouncedSearch({
|
|
360
|
+
delay: 300,
|
|
361
|
+
minLength: 2
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
return (
|
|
365
|
+
<div>
|
|
366
|
+
<input
|
|
367
|
+
value={query}
|
|
368
|
+
onChange={e => setQuery(e.target.value)}
|
|
369
|
+
placeholder="Search memories..."
|
|
370
|
+
/>
|
|
371
|
+
{isLoading && <p>Searching...</p>}
|
|
372
|
+
{results.map(r => (
|
|
373
|
+
<div key={r.memory.id}>{r.memory.content}</div>
|
|
374
|
+
))}
|
|
375
|
+
</div>
|
|
376
|
+
);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
// Get context for AI
|
|
380
|
+
function ChatWithContext() {
|
|
381
|
+
const { getContext, isLoading } = useMemoryContext();
|
|
382
|
+
|
|
383
|
+
const sendMessage = async (message: string) => {
|
|
384
|
+
const { context, memoriesUsed } = await getContext(message);
|
|
385
|
+
|
|
386
|
+
// Use context with your AI provider
|
|
387
|
+
const response = await callAI({
|
|
388
|
+
system: `Context:\n${context}`,
|
|
389
|
+
message
|
|
390
|
+
});
|
|
391
|
+
};
|
|
392
|
+
|
|
393
|
+
return <ChatUI onSend={sendMessage} />;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
// Real-time updates
|
|
397
|
+
function LiveMemoryFeed() {
|
|
398
|
+
const { latestEvent, events, isConnected } = useMemorySubscription({
|
|
399
|
+
userId: 'user-123',
|
|
400
|
+
eventTypes: ['memory.created', 'memory.updated']
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
useEffect(() => {
|
|
404
|
+
if (latestEvent?.type === 'memory.created') {
|
|
405
|
+
console.log('New memory:', latestEvent.memory);
|
|
406
|
+
}
|
|
407
|
+
}, [latestEvent]);
|
|
408
|
+
|
|
409
|
+
return (
|
|
410
|
+
<div>
|
|
411
|
+
<p>Status: {isConnected ? 'Connected' : 'Disconnected'}</p>
|
|
412
|
+
<p>Events: {events.length}</p>
|
|
413
|
+
</div>
|
|
414
|
+
);
|
|
266
415
|
}
|
|
267
416
|
```
|
|
268
417
|
|
|
269
|
-
##
|
|
418
|
+
## WebSocket Support
|
|
270
419
|
|
|
271
420
|
```typescript
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
421
|
+
import { CilowWebSocket } from '@cilow/sdk/websocket';
|
|
422
|
+
|
|
423
|
+
const ws = new CilowWebSocket({
|
|
424
|
+
apiUrl: 'https://api.cilow.ai',
|
|
425
|
+
apiKey: 'your-key',
|
|
426
|
+
reconnectAttempts: 5,
|
|
427
|
+
heartbeatInterval: 30000
|
|
428
|
+
});
|
|
275
429
|
|
|
276
|
-
|
|
277
|
-
|
|
430
|
+
// Connect
|
|
431
|
+
await ws.connect();
|
|
278
432
|
|
|
279
|
-
|
|
280
|
-
|
|
433
|
+
// Subscribe to events
|
|
434
|
+
ws.subscribe({ userId: 'user-123' });
|
|
281
435
|
|
|
282
|
-
|
|
283
|
-
|
|
436
|
+
// Listen for memory events
|
|
437
|
+
ws.onMemoryCreated((event) => {
|
|
438
|
+
console.log('New memory:', event.memory);
|
|
284
439
|
});
|
|
440
|
+
|
|
441
|
+
ws.onMemoryUpdated((event) => {
|
|
442
|
+
console.log('Updated memory:', event.memory);
|
|
443
|
+
});
|
|
444
|
+
|
|
445
|
+
ws.onMemoryDeleted((event) => {
|
|
446
|
+
console.log('Deleted memory ID:', event.memoryId);
|
|
447
|
+
});
|
|
448
|
+
|
|
449
|
+
// Listen for graph events
|
|
450
|
+
ws.onGraphNodeCreated((event) => {
|
|
451
|
+
console.log('New node:', event.node);
|
|
452
|
+
});
|
|
453
|
+
|
|
454
|
+
// Connection status
|
|
455
|
+
ws.onConnectionStatus((event) => {
|
|
456
|
+
console.log('Connection:', event.status, event.reason);
|
|
457
|
+
});
|
|
458
|
+
|
|
459
|
+
// Wait for specific event
|
|
460
|
+
const event = await ws.once('memory.created', 5000);
|
|
461
|
+
|
|
462
|
+
// Disconnect
|
|
463
|
+
ws.disconnect();
|
|
285
464
|
```
|
|
286
465
|
|
|
287
|
-
## Types
|
|
466
|
+
## TypeScript Types
|
|
288
467
|
|
|
289
|
-
|
|
468
|
+
The SDK exports comprehensive TypeScript types:
|
|
290
469
|
|
|
291
470
|
```typescript
|
|
292
471
|
import type {
|
|
472
|
+
// Core types
|
|
293
473
|
Memory,
|
|
294
|
-
|
|
474
|
+
MemorySummary,
|
|
475
|
+
MemoryTier,
|
|
295
476
|
SearchResult,
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
477
|
+
|
|
478
|
+
// Graph types
|
|
479
|
+
GraphNode,
|
|
480
|
+
GraphEdge,
|
|
481
|
+
GraphSubgraph,
|
|
482
|
+
|
|
483
|
+
// Event types
|
|
484
|
+
CilowEvent,
|
|
485
|
+
MemoryCreatedEvent,
|
|
486
|
+
MemoryUpdatedEvent,
|
|
487
|
+
|
|
488
|
+
// Configuration
|
|
489
|
+
CilowConfig,
|
|
490
|
+
WebSocketConfig,
|
|
491
|
+
|
|
492
|
+
// API types
|
|
493
|
+
PaginatedResponse,
|
|
494
|
+
ContextResult,
|
|
301
495
|
} from '@cilow/sdk';
|
|
302
496
|
```
|
|
303
497
|
|
|
304
|
-
##
|
|
305
|
-
|
|
306
|
-
Cilow can be self-hosted. Point the SDK to your instance:
|
|
498
|
+
## Error Handling
|
|
307
499
|
|
|
308
500
|
```typescript
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
501
|
+
import { CilowApiError, isApiError } from '@cilow/sdk';
|
|
502
|
+
|
|
503
|
+
try {
|
|
504
|
+
await cilow.getMemory('invalid-id');
|
|
505
|
+
} catch (error) {
|
|
506
|
+
if (error instanceof CilowApiError) {
|
|
507
|
+
console.log('API Error:', error.message);
|
|
508
|
+
console.log('Code:', error.code);
|
|
509
|
+
console.log('Status:', error.statusCode);
|
|
510
|
+
console.log('Details:', error.details);
|
|
511
|
+
}
|
|
512
|
+
}
|
|
313
513
|
```
|
|
314
514
|
|
|
315
|
-
##
|
|
515
|
+
## Configuration Options
|
|
316
516
|
|
|
317
|
-
|
|
318
|
-
|
|
517
|
+
```typescript
|
|
518
|
+
const cilow = new Cilow({
|
|
519
|
+
// Required
|
|
520
|
+
apiUrl: 'https://api.cilow.ai',
|
|
521
|
+
apiKey: 'your-api-key',
|
|
522
|
+
|
|
523
|
+
// Optional
|
|
524
|
+
timeout: 30000, // Request timeout (ms)
|
|
525
|
+
retries: 3, // Retry attempts
|
|
526
|
+
debug: false, // Enable debug logging
|
|
527
|
+
headers: { // Custom headers
|
|
528
|
+
'X-Custom-Header': 'value'
|
|
529
|
+
}
|
|
530
|
+
});
|
|
531
|
+
```
|
|
319
532
|
|
|
320
533
|
## License
|
|
321
534
|
|