@agency-lang/memento 0.0.0-stage → 0.1.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 +64 -2
- package/dist/agency/embed.d.ts +22 -0
- package/dist/agency/embed.js +458 -0
- package/dist/agencyApi.d.ts +98 -0
- package/dist/agencyApi.js +237 -0
- package/dist/embed.d.ts +45 -0
- package/dist/embed.js +77 -0
- package/dist/engine/files/engine.d.ts +85 -0
- package/dist/engine/files/engine.js +284 -0
- package/dist/engine/files/lock.d.ts +18 -0
- package/dist/engine/files/lock.js +247 -0
- package/dist/engine/files/manifest.d.ts +43 -0
- package/dist/engine/files/manifest.js +111 -0
- package/dist/engine/files/safe-delete.d.ts +19 -0
- package/dist/engine/files/safe-delete.js +79 -0
- package/dist/engine/files/segments.d.ts +17 -0
- package/dist/engine/files/segments.js +128 -0
- package/dist/engine/memory/engine.d.ts +27 -0
- package/dist/engine/memory/engine.js +87 -0
- package/dist/engine/open.d.ts +7 -0
- package/dist/engine/open.js +28 -0
- package/dist/engine/shared/filter.d.ts +7 -0
- package/dist/engine/shared/filter.js +72 -0
- package/dist/engine/shared/keyword.d.ts +27 -0
- package/dist/engine/shared/keyword.js +92 -0
- package/dist/engine/shared/settings.d.ts +5 -0
- package/dist/engine/shared/settings.js +9 -0
- package/dist/engine/shared/state.d.ts +58 -0
- package/dist/engine/shared/state.js +275 -0
- package/dist/engine/shared/tokens.d.ts +15 -0
- package/dist/engine/shared/tokens.js +23 -0
- package/dist/engine/shared/vectors.d.ts +36 -0
- package/dist/engine/shared/vectors.js +70 -0
- package/dist/engine/types.d.ts +121 -0
- package/dist/engine/types.js +1 -0
- package/dist/files.d.ts +75 -0
- package/dist/files.js +190 -0
- package/dist/filter.d.ts +3 -0
- package/dist/filter.js +80 -0
- package/dist/fusion.d.ts +24 -0
- package/dist/fusion.js +26 -0
- package/dist/ids.d.ts +2 -0
- package/dist/ids.js +19 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +3 -0
- package/dist/local.d.ts +36 -0
- package/dist/local.js +75 -0
- package/dist/memento.d.ts +151 -0
- package/dist/memento.js +470 -0
- package/dist/query.d.ts +50 -0
- package/dist/query.js +138 -0
- package/dist/rows.d.ts +17 -0
- package/dist/rows.js +90 -0
- package/dist/stem.d.ts +2 -0
- package/dist/stem.js +121 -0
- package/dist/types.d.ts +246 -0
- package/dist/types.js +3 -0
- package/dist/vectors.d.ts +2 -0
- package/dist/vectors.js +10 -0
- package/dist/windows.d.ts +44 -0
- package/dist/windows.js +77 -0
- package/index.agency +268 -0
- package/index.js +1803 -0
- package/package.json +55 -4
package/dist/windows.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { excerptAt } from "./query.js";
|
|
2
|
+
/** The most characters of text in one window. */
|
|
3
|
+
export const WINDOW_CHARS = 2000;
|
|
4
|
+
/** How many characters consecutive windows share. */
|
|
5
|
+
export const WINDOW_OVERLAP = 200;
|
|
6
|
+
/** A window ends at the last newline or sentence end after this share of its length, when there is one. */
|
|
7
|
+
export const WINDOW_CUT_FROM = 0.6;
|
|
8
|
+
/** The most characters of the title that go on top of a window. The text is bounded; the title is not. */
|
|
9
|
+
export const WINDOW_TITLE_CHARS = 300;
|
|
10
|
+
/** A document with no title and no text after trimming gets no windows; the model rejects an empty input. */
|
|
11
|
+
export function isBlank(doc) {
|
|
12
|
+
return doc.title.trim() === "" && doc.text.trim() === "";
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* A window whose text is blank is dropped, because the model rejects a blank
|
|
16
|
+
* input: a window that falls inside a run of whitespace, under a blank
|
|
17
|
+
* title. At least one window stays, because the document is not blank. The
|
|
18
|
+
* rest are numbered without it.
|
|
19
|
+
*/
|
|
20
|
+
export function windowsOf(doc) {
|
|
21
|
+
if (isBlank(doc)) {
|
|
22
|
+
return [];
|
|
23
|
+
}
|
|
24
|
+
const title = doc.title.trim().slice(0, WINDOW_TITLE_CHARS);
|
|
25
|
+
const windows = splitText(doc.text, WINDOW_CHARS, WINDOW_OVERLAP)
|
|
26
|
+
.map((range) => ({ ...range, text: withTitle(title, doc.text.slice(range.start, range.end)) }))
|
|
27
|
+
.filter((window) => window.text.trim() !== "");
|
|
28
|
+
return windows.map((window, n) => ({ n, ...window }));
|
|
29
|
+
}
|
|
30
|
+
function withTitle(title, text) {
|
|
31
|
+
return title === "" ? text : `${title}\n${text}`;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Cuts a text into overlapping ranges of at most `maxChars`, each ending at
|
|
35
|
+
* the last newline or sentence end in its final part when there is one. A
|
|
36
|
+
* text that fits is one range; an empty text is one empty range.
|
|
37
|
+
*/
|
|
38
|
+
export function splitText(text, maxChars, overlap) {
|
|
39
|
+
// A window ends past floor(maxChars * WINDOW_CUT_FROM) from its start, so the next start, end - overlap, moves forward only when the overlap is under that.
|
|
40
|
+
const shortestWindow = Math.floor(maxChars * WINDOW_CUT_FROM);
|
|
41
|
+
if (overlap >= shortestWindow) {
|
|
42
|
+
throw new Error(`overlap ${overlap} must be under ${shortestWindow}, the shortest window for ${maxChars} characters`);
|
|
43
|
+
}
|
|
44
|
+
if (text.length <= maxChars) {
|
|
45
|
+
return [{ start: 0, end: text.length }];
|
|
46
|
+
}
|
|
47
|
+
const ranges = [];
|
|
48
|
+
let start = 0;
|
|
49
|
+
while (start < text.length) {
|
|
50
|
+
const end = cutEnd(text, start, maxChars);
|
|
51
|
+
ranges.push({ start, end });
|
|
52
|
+
if (end >= text.length) {
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
start = end - overlap;
|
|
56
|
+
}
|
|
57
|
+
return ranges;
|
|
58
|
+
}
|
|
59
|
+
/** Where a window starting at `start` ends: at the text's end, at a break in its final part, or at `maxChars`. */
|
|
60
|
+
function cutEnd(text, start, maxChars) {
|
|
61
|
+
const hardEnd = Math.min(text.length, start + maxChars);
|
|
62
|
+
if (hardEnd === text.length) {
|
|
63
|
+
return hardEnd;
|
|
64
|
+
}
|
|
65
|
+
const from = start + Math.floor(maxChars * WINDOW_CUT_FROM);
|
|
66
|
+
const tail = text.slice(from, hardEnd);
|
|
67
|
+
const cut = Math.max(tail.lastIndexOf("\n"), tail.lastIndexOf(". "));
|
|
68
|
+
return cut > 0 ? from + cut + 1 : hardEnd;
|
|
69
|
+
}
|
|
70
|
+
/** The excerpt to show when no query word matched: the start of the document's best window. */
|
|
71
|
+
export function windowExcerpt(text, window) {
|
|
72
|
+
return excerptAt(text, window.start);
|
|
73
|
+
}
|
|
74
|
+
/** The window that is the whole text, for a document found by keyword alone. */
|
|
75
|
+
export function wholeText(text) {
|
|
76
|
+
return { n: 0, start: 0, end: text.length };
|
|
77
|
+
}
|
package/index.agency
ADDED
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
// Memento's functions for Agency programs. Every function describes the
|
|
2
|
+
// store, asks for approval, then does its work in TypeScript, in
|
|
3
|
+
// src/agencyApi.ts. The module never answers its own interrupts: the
|
|
4
|
+
// caller's handlers and policy decide every one. docs/dev/agency-module.md
|
|
5
|
+
// has the design.
|
|
6
|
+
import { cwd } from "std::system"
|
|
7
|
+
import {
|
|
8
|
+
describeStore,
|
|
9
|
+
describeNewStore,
|
|
10
|
+
describeAdd,
|
|
11
|
+
createStoreAt,
|
|
12
|
+
searchStore,
|
|
13
|
+
getStoredDocuments,
|
|
14
|
+
storeStats,
|
|
15
|
+
addDocuments,
|
|
16
|
+
removeDocuments,
|
|
17
|
+
planStoreFiles,
|
|
18
|
+
loadStoreFiles,
|
|
19
|
+
} from "./dist/agencyApi.js"
|
|
20
|
+
|
|
21
|
+
/** @module
|
|
22
|
+
@summary Search, load and edit a Memento store, with an approval before each step.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
// May a store be created in this folder, with this embedding model?
|
|
26
|
+
@always(store)
|
|
27
|
+
effect memento::create {
|
|
28
|
+
store: string;
|
|
29
|
+
model: string;
|
|
30
|
+
runsOn: string
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// May this store be read?
|
|
34
|
+
@always(store)
|
|
35
|
+
effect memento::search {
|
|
36
|
+
store: string;
|
|
37
|
+
query: string;
|
|
38
|
+
limit: number
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// May documents be added to this store? `replaces` counts the ones that
|
|
42
|
+
// overwrite a document the store already holds.
|
|
43
|
+
@always(store)
|
|
44
|
+
effect memento::add {
|
|
45
|
+
store: string;
|
|
46
|
+
documents: number;
|
|
47
|
+
replaces: number;
|
|
48
|
+
source: string
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// May documents be deleted from this store?
|
|
52
|
+
@always(store)
|
|
53
|
+
effect memento::remove {
|
|
54
|
+
store: string;
|
|
55
|
+
ids: string[]
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// May text from this store go to this embedding model?
|
|
59
|
+
@always(store, model)
|
|
60
|
+
effect memento::embed {
|
|
61
|
+
store: string;
|
|
62
|
+
model: string;
|
|
63
|
+
runsOn: string;
|
|
64
|
+
texts: number
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// `std::read` is also declared in the prelude. A module that raises an
|
|
68
|
+
// effect declares it itself. The payload is `{ dir, filename }`.
|
|
69
|
+
@alwaysUnder(dir)
|
|
70
|
+
effect std::read {
|
|
71
|
+
dir: string;
|
|
72
|
+
filename: string
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Dates are ISO strings, such as "2024-01-01". null leaves that end open. */
|
|
76
|
+
export type DateRange = {
|
|
77
|
+
after: string | null;
|
|
78
|
+
before: string | null
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Document lengths in characters. null leaves that end open. */
|
|
82
|
+
export type LengthRange = {
|
|
83
|
+
min: number | null;
|
|
84
|
+
max: number | null
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** One condition: every field that is not null must hold. */
|
|
88
|
+
export type SearchFilter = {
|
|
89
|
+
date: DateRange | null;
|
|
90
|
+
meta: object | null;
|
|
91
|
+
ids: string[] | null;
|
|
92
|
+
lengthChars: LengthRange | null
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** A document to add. createdAt is an ISO date such as "2024-03-01". */
|
|
96
|
+
export type NewDocument = {
|
|
97
|
+
id: string;
|
|
98
|
+
title: string;
|
|
99
|
+
text: string;
|
|
100
|
+
createdAt: string | null;
|
|
101
|
+
meta: object | null
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export def createStore(store: string, embeddingModel: string = ""): Result raises <memento::create> {
|
|
105
|
+
"""
|
|
106
|
+
Create an empty store in a folder. Fails when a store is already there.
|
|
107
|
+
|
|
108
|
+
@param store - The store's folder. A relative path starts at the working folder.
|
|
109
|
+
@param embeddingModel - The model that searches by meaning, as "provider:model". Leave empty for keyword search only. It cannot be changed later.
|
|
110
|
+
"""
|
|
111
|
+
const info = try describeNewStore(store, embeddingModel, cwd())
|
|
112
|
+
if (isFailure(info)) {
|
|
113
|
+
return info
|
|
114
|
+
}
|
|
115
|
+
return interrupt memento::create("Create a store here?", {
|
|
116
|
+
store: info.value.store,
|
|
117
|
+
model: info.value.embeddingModel,
|
|
118
|
+
runsOn: info.value.runsOn
|
|
119
|
+
})
|
|
120
|
+
return try createStoreAt(info.value.store, info.value.embeddingModel)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export def search(store: string, query: string, limit: number = 20, filter: SearchFilter | null = null): Result raises <memento::search, memento::embed> {
|
|
124
|
+
"""
|
|
125
|
+
Search a store in plain words. Returns the best matching documents, each with its id, title and the excerpt that matched. Call getDocuments with an id to read a whole document.
|
|
126
|
+
|
|
127
|
+
@param store - The store's folder. A relative path starts at the working folder.
|
|
128
|
+
@param query - What to look for, in plain words.
|
|
129
|
+
@param limit - The most documents to return.
|
|
130
|
+
@param filter - Limits the search by date, by a document's meta fields, by id or by length.
|
|
131
|
+
"""
|
|
132
|
+
const info = try describeStore(store, cwd())
|
|
133
|
+
if (isFailure(info)) {
|
|
134
|
+
return info
|
|
135
|
+
}
|
|
136
|
+
return interrupt memento::search("Search this store?", {
|
|
137
|
+
store: info.value.store,
|
|
138
|
+
query: query,
|
|
139
|
+
limit: limit
|
|
140
|
+
})
|
|
141
|
+
if (info.value.embeddingModel != "" && query.trim() != "") {
|
|
142
|
+
return interrupt memento::embed("Send this text to the embedding model?", {
|
|
143
|
+
store: info.value.store,
|
|
144
|
+
model: info.value.embeddingModel,
|
|
145
|
+
runsOn: info.value.runsOn,
|
|
146
|
+
texts: 1
|
|
147
|
+
})
|
|
148
|
+
}
|
|
149
|
+
return try searchStore(info.value, query, limit, filter)
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export def getDocuments(store: string, ids: string[]): Result raises <memento::search> {
|
|
153
|
+
"""
|
|
154
|
+
Read whole documents from a store by id, in the order asked. Unknown ids are left out.
|
|
155
|
+
|
|
156
|
+
@param store - The store's folder. A relative path starts at the working folder.
|
|
157
|
+
@param ids - The ids of the documents, as search returns them.
|
|
158
|
+
"""
|
|
159
|
+
const info = try describeStore(store, cwd())
|
|
160
|
+
if (isFailure(info)) {
|
|
161
|
+
return info
|
|
162
|
+
}
|
|
163
|
+
return interrupt memento::search("Read these documents from this store?", {
|
|
164
|
+
store: info.value.store,
|
|
165
|
+
query: "",
|
|
166
|
+
limit: ids.length
|
|
167
|
+
})
|
|
168
|
+
return try getStoredDocuments(info.value.store, ids)
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export def stats(store: string): Result raises <memento::search> {
|
|
172
|
+
"""
|
|
173
|
+
Count a store's documents and files, and give its size in bytes and its embedding model.
|
|
174
|
+
|
|
175
|
+
@param store - The store's folder. A relative path starts at the working folder.
|
|
176
|
+
"""
|
|
177
|
+
const info = try describeStore(store, cwd())
|
|
178
|
+
if (isFailure(info)) {
|
|
179
|
+
return info
|
|
180
|
+
}
|
|
181
|
+
return interrupt memento::search("Read this store's counts?", {
|
|
182
|
+
store: info.value.store,
|
|
183
|
+
query: "",
|
|
184
|
+
limit: 0
|
|
185
|
+
})
|
|
186
|
+
return try storeStats(info.value.store)
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export def add(store: string, documents: NewDocument[]): Result raises <memento::add, memento::embed> {
|
|
190
|
+
"""
|
|
191
|
+
Add documents to a store. A document with the id of one already there replaces it.
|
|
192
|
+
|
|
193
|
+
@param store - The store's folder. A relative path starts at the working folder.
|
|
194
|
+
@param documents - The documents to add.
|
|
195
|
+
"""
|
|
196
|
+
const info = try describeAdd(store, documents, cwd())
|
|
197
|
+
if (isFailure(info)) {
|
|
198
|
+
return info
|
|
199
|
+
}
|
|
200
|
+
return interrupt memento::add("Add these documents to this store?", {
|
|
201
|
+
store: info.value.store,
|
|
202
|
+
documents: documents.length,
|
|
203
|
+
replaces: info.value.replaces,
|
|
204
|
+
source: ""
|
|
205
|
+
})
|
|
206
|
+
if (info.value.embeddingModel != "" && documents.length > 0) {
|
|
207
|
+
return interrupt memento::embed("Send this text to the embedding model?", {
|
|
208
|
+
store: info.value.store,
|
|
209
|
+
model: info.value.embeddingModel,
|
|
210
|
+
runsOn: info.value.runsOn,
|
|
211
|
+
texts: documents.length
|
|
212
|
+
})
|
|
213
|
+
}
|
|
214
|
+
return try addDocuments(info.value, documents)
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export def addFiles(store: string, globs: string[]): Result raises <memento::add, memento::embed, std::read> {
|
|
218
|
+
"""
|
|
219
|
+
Load the files matching the globs into a store, one document per file. Files unchanged since the last load are skipped.
|
|
220
|
+
|
|
221
|
+
@param store - The store's folder. A relative path starts at the working folder.
|
|
222
|
+
@param globs - Patterns such as "~/notes/**/*.md". A relative pattern starts at the working folder.
|
|
223
|
+
"""
|
|
224
|
+
const plan = try planStoreFiles(store, globs, cwd())
|
|
225
|
+
if (isFailure(plan)) {
|
|
226
|
+
return plan
|
|
227
|
+
}
|
|
228
|
+
// Asked even when nothing changed, so no answer about the files comes back without an approval.
|
|
229
|
+
return interrupt memento::add("Add these files to this store?", {
|
|
230
|
+
store: plan.value.store,
|
|
231
|
+
documents: plan.value.changed.length,
|
|
232
|
+
replaces: plan.value.replaces,
|
|
233
|
+
source: globs.join(", ")
|
|
234
|
+
})
|
|
235
|
+
for (file in plan.value.changed) {
|
|
236
|
+
return interrupt std::read("Read this file into the store?", {
|
|
237
|
+
dir: file.dir,
|
|
238
|
+
filename: file.filename
|
|
239
|
+
})
|
|
240
|
+
}
|
|
241
|
+
if (plan.value.embeddingModel != "" && plan.value.changed.length > 0) {
|
|
242
|
+
return interrupt memento::embed("Send this text to the embedding model?", {
|
|
243
|
+
store: plan.value.store,
|
|
244
|
+
model: plan.value.embeddingModel,
|
|
245
|
+
runsOn: plan.value.runsOn,
|
|
246
|
+
texts: plan.value.changed.length
|
|
247
|
+
})
|
|
248
|
+
}
|
|
249
|
+
return try loadStoreFiles(plan.value)
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
export def remove(store: string, ids: string[]): Result raises <memento::remove> {
|
|
253
|
+
"""
|
|
254
|
+
Delete documents from a store by id. A file's document has the file's path as its id.
|
|
255
|
+
|
|
256
|
+
@param store - The store's folder. A relative path starts at the working folder.
|
|
257
|
+
@param ids - The ids of the documents to delete.
|
|
258
|
+
"""
|
|
259
|
+
const info = try describeStore(store, cwd())
|
|
260
|
+
if (isFailure(info)) {
|
|
261
|
+
return info
|
|
262
|
+
}
|
|
263
|
+
return interrupt memento::remove("Delete these documents from this store?", {
|
|
264
|
+
store: info.value.store,
|
|
265
|
+
ids: ids
|
|
266
|
+
})
|
|
267
|
+
return try removeDocuments(info.value.store, ids)
|
|
268
|
+
}
|