@deepagents/context 0.6.0 → 0.8.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.
@@ -0,0 +1,245 @@
1
+ /**
2
+ * Graph-based context store types and abstract interface.
3
+ *
4
+ * The storage model uses a DAG (Directed Acyclic Graph) for messages:
5
+ * - Messages are immutable nodes with parentId forming the graph
6
+ * - Branches are pointers to head (tip) messages
7
+ * - Checkpoints are pointers to specific messages
8
+ * - History is preserved through branching (rewind creates new branch)
9
+ */
10
+ /**
11
+ * Data for creating/storing a chat.
12
+ */
13
+ export interface ChatData {
14
+ id: string;
15
+ title?: string;
16
+ metadata?: Record<string, unknown>;
17
+ createdAt: number;
18
+ updatedAt: number;
19
+ }
20
+ /**
21
+ * Information about a chat for listing.
22
+ */
23
+ export interface ChatInfo {
24
+ id: string;
25
+ title?: string;
26
+ messageCount: number;
27
+ branchCount: number;
28
+ createdAt: number;
29
+ updatedAt: number;
30
+ }
31
+ /**
32
+ * Data for creating/storing a message (graph node).
33
+ */
34
+ export interface MessageData {
35
+ id: string;
36
+ chatId: string;
37
+ parentId: string | null;
38
+ name: string;
39
+ type?: string;
40
+ data: unknown;
41
+ persist: boolean;
42
+ createdAt: number;
43
+ }
44
+ /**
45
+ * Message with computed properties for listing.
46
+ */
47
+ export interface MessageInfo extends MessageData {
48
+ hasChildren: boolean;
49
+ }
50
+ /**
51
+ * Data for creating/storing a branch.
52
+ * A branch is a pointer to a head message in the graph.
53
+ */
54
+ export interface BranchData {
55
+ id: string;
56
+ chatId: string;
57
+ name: string;
58
+ headMessageId: string | null;
59
+ isActive: boolean;
60
+ createdAt: number;
61
+ }
62
+ /**
63
+ * Information about a branch for listing.
64
+ */
65
+ export interface BranchInfo {
66
+ id: string;
67
+ name: string;
68
+ headMessageId: string | null;
69
+ isActive: boolean;
70
+ messageCount: number;
71
+ createdAt: number;
72
+ }
73
+ /**
74
+ * Data for creating/storing a checkpoint.
75
+ * A checkpoint is a pointer to a specific message in the graph.
76
+ */
77
+ export interface CheckpointData {
78
+ id: string;
79
+ chatId: string;
80
+ name: string;
81
+ messageId: string;
82
+ createdAt: number;
83
+ }
84
+ /**
85
+ * Information about a checkpoint for listing.
86
+ */
87
+ export interface CheckpointInfo {
88
+ id: string;
89
+ name: string;
90
+ messageId: string;
91
+ createdAt: number;
92
+ }
93
+ /**
94
+ * Options for searching messages.
95
+ */
96
+ export interface SearchOptions {
97
+ /** Only search in specific roles (e.g., ['user', 'assistant']) */
98
+ roles?: string[];
99
+ /** Maximum results to return (default: 20) */
100
+ limit?: number;
101
+ }
102
+ /**
103
+ * Search result with relevance ranking.
104
+ */
105
+ export interface SearchResult {
106
+ /** The matched message */
107
+ message: MessageData;
108
+ /** BM25 relevance score (lower = more relevant) */
109
+ rank: number;
110
+ /** Highlighted snippet with matched terms */
111
+ snippet?: string;
112
+ }
113
+ /**
114
+ * A node in the visualization graph.
115
+ */
116
+ export interface GraphNode {
117
+ id: string;
118
+ parentId: string | null;
119
+ role: string;
120
+ content: string;
121
+ createdAt: number;
122
+ }
123
+ /**
124
+ * A branch pointer for visualization.
125
+ */
126
+ export interface GraphBranch {
127
+ name: string;
128
+ headMessageId: string | null;
129
+ isActive: boolean;
130
+ }
131
+ /**
132
+ * A checkpoint pointer for visualization.
133
+ */
134
+ export interface GraphCheckpoint {
135
+ name: string;
136
+ messageId: string;
137
+ }
138
+ /**
139
+ * Complete graph data for visualization.
140
+ */
141
+ export interface GraphData {
142
+ chatId: string;
143
+ nodes: GraphNode[];
144
+ branches: GraphBranch[];
145
+ checkpoints: GraphCheckpoint[];
146
+ }
147
+ /**
148
+ * Abstract base class for graph-based context storage.
149
+ *
150
+ * Implementations provide persistence for the message graph, branches,
151
+ * and checkpoints. The graph model enables:
152
+ * - Branching: rewind creates a new branch, original stays intact
153
+ * - Checkpoints: pointers to specific messages for easy restore
154
+ * - No data loss: soft delete only, all history preserved
155
+ */
156
+ export declare abstract class ContextStore {
157
+ /**
158
+ * Create a new chat.
159
+ */
160
+ abstract createChat(chat: ChatData): Promise<void>;
161
+ /**
162
+ * Get a chat by ID.
163
+ */
164
+ abstract getChat(chatId: string): Promise<ChatData | undefined>;
165
+ /**
166
+ * Update chat metadata.
167
+ */
168
+ abstract updateChat(chatId: string, updates: Partial<Pick<ChatData, 'title' | 'metadata' | 'updatedAt'>>): Promise<void>;
169
+ /**
170
+ * List all chats, sorted by updatedAt descending.
171
+ */
172
+ abstract listChats(): Promise<ChatInfo[]>;
173
+ /**
174
+ * Add a message to the graph.
175
+ */
176
+ abstract addMessage(message: MessageData): Promise<void>;
177
+ /**
178
+ * Get a message by ID.
179
+ */
180
+ abstract getMessage(messageId: string): Promise<MessageData | undefined>;
181
+ /**
182
+ * Walk up the parent chain from a head message, returning messages in
183
+ * chronological order (root first).
184
+ */
185
+ abstract getMessageChain(headId: string): Promise<MessageData[]>;
186
+ /**
187
+ * Check if a message has children (is a fork point).
188
+ */
189
+ abstract hasChildren(messageId: string): Promise<boolean>;
190
+ /**
191
+ * Create a new branch.
192
+ */
193
+ abstract createBranch(branch: BranchData): Promise<void>;
194
+ /**
195
+ * Get a branch by chat ID and name.
196
+ */
197
+ abstract getBranch(chatId: string, name: string): Promise<BranchData | undefined>;
198
+ /**
199
+ * Get the active branch for a chat.
200
+ */
201
+ abstract getActiveBranch(chatId: string): Promise<BranchData | undefined>;
202
+ /**
203
+ * Set a branch as active (and deactivate others).
204
+ */
205
+ abstract setActiveBranch(chatId: string, branchId: string): Promise<void>;
206
+ /**
207
+ * Update a branch's head message.
208
+ */
209
+ abstract updateBranchHead(branchId: string, messageId: string | null): Promise<void>;
210
+ /**
211
+ * List all branches for a chat.
212
+ */
213
+ abstract listBranches(chatId: string): Promise<BranchInfo[]>;
214
+ /**
215
+ * Create a checkpoint.
216
+ */
217
+ abstract createCheckpoint(checkpoint: CheckpointData): Promise<void>;
218
+ /**
219
+ * Get a checkpoint by chat ID and name.
220
+ */
221
+ abstract getCheckpoint(chatId: string, name: string): Promise<CheckpointData | undefined>;
222
+ /**
223
+ * List all checkpoints for a chat.
224
+ */
225
+ abstract listCheckpoints(chatId: string): Promise<CheckpointInfo[]>;
226
+ /**
227
+ * Delete a checkpoint.
228
+ */
229
+ abstract deleteCheckpoint(chatId: string, name: string): Promise<void>;
230
+ /**
231
+ * Search messages using full-text search.
232
+ *
233
+ * @param chatId - The chat to search in
234
+ * @param query - FTS5 query string (supports AND, OR, NOT, phrases, prefix*)
235
+ * @param options - Search options
236
+ * @returns Search results ordered by relevance (lower rank = more relevant)
237
+ */
238
+ abstract searchMessages(chatId: string, query: string, options?: SearchOptions): Promise<SearchResult[]>;
239
+ /**
240
+ * Get the complete graph data for a chat.
241
+ * Returns all messages, branches, and checkpoints.
242
+ */
243
+ abstract getGraph(chatId: string): Promise<GraphData>;
244
+ }
245
+ //# sourceMappingURL=store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../../src/lib/store/store.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAMH;;GAEG;AACH,MAAM,WAAW,QAAQ;IACvB,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACnC,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED;;GAEG;AACH,MAAM,WAAW,QAAQ;IACvB,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,YAAY,EAAE,MAAM,CAAC;IACrB,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;CACnB;AAMD;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,GAA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;IAEpE;;OAEG;IACH,QAAQ,CAAC,aAAa,CACpB,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,MAAM,GACX,OAAO,CAAC,cAAc,GAAG,SAAS,CAAC;IAEtC;;OAEG;IACH,QAAQ,CAAC,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,EAAE,CAAC;IAEnE;;OAEG;IACH,QAAQ,CAAC,gBAAgB,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAMtE;;;;;;;OAOG;IACH,QAAQ,CAAC,cAAc,CACrB,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,MAAM,EACb,OAAO,CAAC,EAAE,aAAa,GACtB,OAAO,CAAC,YAAY,EAAE,CAAC;IAM1B;;;OAGG;IACH,QAAQ,CAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,CAAC;CACtD"}
@@ -0,0 +1,15 @@
1
+ import type { GraphData } from './store/store.ts';
2
+ /**
3
+ * Render a graph as ASCII art.
4
+ *
5
+ * @param data - The graph data to visualize
6
+ * @returns ASCII art representation of the graph
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * const graph = await store.getGraph('my-chat');
11
+ * console.log(visualizeGraph(graph));
12
+ * ```
13
+ */
14
+ export declare function visualizeGraph(data: GraphData): string;
15
+ //# sourceMappingURL=visualize.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"visualize.d.ts","sourceRoot":"","sources":["../../src/lib/visualize.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAa,MAAM,kBAAkB,CAAC;AAE7D;;;;;;;;;;;GAWG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,SAAS,GAAG,MAAM,CAgFtD"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=usage.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"usage.d.ts","sourceRoot":"","sources":["../src/usage.ts"],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deepagents/context",
3
- "version": "0.6.0",
3
+ "version": "0.8.0",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",