@strands-agents/sdk 1.15.0 → 1.16.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/dist/src/agent/agent.d.ts +4 -0
- package/dist/src/agent/agent.d.ts.map +1 -1
- package/dist/src/agent/agent.js +56 -16
- package/dist/src/agent/agent.js.map +1 -1
- package/dist/src/background-tasks/background-tasks.d.ts +41 -0
- package/dist/src/background-tasks/background-tasks.d.ts.map +1 -0
- package/dist/src/background-tasks/background-tasks.js +402 -0
- package/dist/src/background-tasks/background-tasks.js.map +1 -0
- package/dist/src/background-tasks/errors.d.ts +7 -0
- package/dist/src/background-tasks/errors.d.ts.map +1 -1
- package/dist/src/background-tasks/errors.js +10 -0
- package/dist/src/background-tasks/errors.js.map +1 -1
- package/dist/src/background-tasks/in-process/engine.d.ts +3 -4
- package/dist/src/background-tasks/in-process/engine.d.ts.map +1 -1
- package/dist/src/background-tasks/in-process/engine.js +9 -25
- package/dist/src/background-tasks/in-process/engine.js.map +1 -1
- package/dist/src/background-tasks/in-process/manager.d.ts +41 -0
- package/dist/src/background-tasks/in-process/manager.d.ts.map +1 -0
- package/dist/src/background-tasks/in-process/manager.js +214 -0
- package/dist/src/background-tasks/in-process/manager.js.map +1 -0
- package/dist/src/background-tasks/in-process/types.d.ts +9 -14
- package/dist/src/background-tasks/in-process/types.d.ts.map +1 -1
- package/dist/src/background-tasks/manager.d.ts +59 -0
- package/dist/src/background-tasks/manager.d.ts.map +1 -0
- package/dist/src/background-tasks/manager.js +2 -0
- package/dist/src/background-tasks/manager.js.map +1 -0
- package/dist/src/background-tasks/timer.d.ts +3 -0
- package/dist/src/background-tasks/timer.d.ts.map +1 -0
- package/dist/src/background-tasks/timer.js +11 -0
- package/dist/src/background-tasks/timer.js.map +1 -0
- package/dist/src/background-tasks/types.d.ts +60 -0
- package/dist/src/background-tasks/types.d.ts.map +1 -0
- package/dist/src/background-tasks/types.js +11 -0
- package/dist/src/background-tasks/types.js.map +1 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/storage/local-file-storage.d.ts +4 -6
- package/dist/src/storage/local-file-storage.d.ts.map +1 -1
- package/dist/src/storage/local-file-storage.js +12 -10
- package/dist/src/storage/local-file-storage.js.map +1 -1
- package/dist/src/storage/search/keyword.d.ts +1 -0
- package/dist/src/storage/search/keyword.d.ts.map +1 -1
- package/dist/src/storage/search/keyword.js +124 -0
- package/dist/src/storage/search/keyword.js.map +1 -1
- package/dist/src/storage/search/qmd.d.ts +66 -0
- package/dist/src/storage/search/qmd.d.ts.map +1 -0
- package/dist/src/storage/search/qmd.js +124 -0
- package/dist/src/storage/search/qmd.js.map +1 -0
- package/dist/src/tools/executors/executor.d.ts +11 -1
- package/dist/src/tools/executors/executor.d.ts.map +1 -1
- package/dist/src/tools/executors/executor.js +50 -16
- package/dist/src/tools/executors/executor.js.map +1 -1
- package/dist/src/tsconfig.tsbuildinfo +1 -1
- package/dist/src/vended-memory-stores/file-memory-store/store.d.ts +9 -0
- package/dist/src/vended-memory-stores/file-memory-store/store.d.ts.map +1 -1
- package/dist/src/vended-memory-stores/file-memory-store/store.js +18 -13
- package/dist/src/vended-memory-stores/file-memory-store/store.js.map +1 -1
- package/dist/src/vended-tools/file-editor/file-editor.js +35 -30
- package/dist/src/vended-tools/file-editor/file-editor.js.map +1 -1
- package/package.json +14 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"local-file-storage.d.ts","sourceRoot":"","sources":["../../../src/storage/local-file-storage.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAA;AACjD,OAAO,KAAK,EAAE,OAAO,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAA;AAiBhE;;;;;;;;;;;;;;;;;GAiBG;AACH,qBAAa,gBAAiB,YAAW,OAAO;IAC9C,OAAO,CAAC,QAAQ,CAAC,QAA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IAK9D;;;;;;;OAOG;IACH,UAAU,CAAC,OAAO,EAAE,OAAO,GAAG,gBAAgB;IAK9C;;;;;;OAMG;IACG,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC;IA6BzD;;;;;;OAMG;IACG,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,GAAG,IAAI,CAAC;IAqBnD;;;;;OAKG;IACG,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAqBxC;;;;;;OAMG;IACG,IAAI,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAa7C,OAAO,CAAC,QAAQ;YAKF,aAAa;YA2Bb,gBAAgB;IA2B9B;;;;;OAKG;IACG,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,mBAAmB,EAAE,CAAC;IAI3D;;OAEG;IACH,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,gBAAgB;CAO5C"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { StorageError } from '../errors.js';
|
|
2
|
-
import {
|
|
2
|
+
import { NAMESPACED, normalizeKey, normalizePrefix } from './storage.js';
|
|
3
3
|
import { KeywordSearchStrategy } from './search/keyword.js';
|
|
4
4
|
/**
|
|
5
5
|
* Returns true if the error represents a missing or non-directory path (ENOENT or ENOTDIR).
|
|
@@ -33,12 +33,16 @@ function isNotFoundError(error) {
|
|
|
33
33
|
export class LocalFileStorage {
|
|
34
34
|
_baseDir;
|
|
35
35
|
_sandbox;
|
|
36
|
+
/** The resolved root directory for this storage instance. */
|
|
37
|
+
get baseDir() {
|
|
38
|
+
return this._baseDir;
|
|
39
|
+
}
|
|
36
40
|
/**
|
|
37
41
|
* @param baseDir - Root directory under which keys are stored. Defaults to `./.strands/`.
|
|
38
42
|
* @param sandbox - Optional sandbox to route I/O through. Usually set via {@link forSandbox}.
|
|
39
43
|
*/
|
|
40
44
|
constructor(baseDir = './.strands/', sandbox) {
|
|
41
|
-
this._baseDir = baseDir;
|
|
45
|
+
this._baseDir = baseDir.replace(/\/{2,}/g, '/').replace(/(.)\/$/, '$1');
|
|
42
46
|
this._sandbox = sandbox;
|
|
43
47
|
}
|
|
44
48
|
/**
|
|
@@ -239,16 +243,14 @@ export class LocalFileStorage {
|
|
|
239
243
|
return KeywordSearchStrategy.search(this, query);
|
|
240
244
|
}
|
|
241
245
|
/**
|
|
242
|
-
* Returns a
|
|
243
|
-
* The returned view preserves `forSandbox` for single-level namespacing;
|
|
244
|
-
* nested `.namespace()` calls on the view do not carry sandbox routing.
|
|
246
|
+
* Returns a namespaced `LocalFileStorage` rooted at the subdirectory.
|
|
245
247
|
*/
|
|
246
248
|
namespace(prefix) {
|
|
247
|
-
const
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
249
|
+
const normalized = normalizePrefix(prefix);
|
|
250
|
+
const subDir = normalized ? `${this._baseDir.replace(/\/$/, '')}/${normalized}` : this._baseDir;
|
|
251
|
+
const scoped = new LocalFileStorage(subDir, this._sandbox);
|
|
252
|
+
Object.defineProperty(scoped, NAMESPACED, { value: true });
|
|
253
|
+
return scoped;
|
|
252
254
|
}
|
|
253
255
|
}
|
|
254
256
|
//# sourceMappingURL=local-file-storage.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"local-file-storage.js","sourceRoot":"","sources":["../../../src/storage/local-file-storage.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AAC3C,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"local-file-storage.js","sourceRoot":"","sources":["../../../src/storage/local-file-storage.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AAC3C,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,cAAc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AC,KAAK,CAAC,IAAI,CAAA;gBACxE,IAAI,KAAK,CAAC,KAAK,EAAE,CAAC;oBAChB,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,IAAI,CAAC,GAAG,OAAO,IAAI,KAAK,CAAC,IAAI,EAAE,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAA;gBACnE,CAAC;qBAAM,CAAC;oBACN,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;gBACtB,CAAC;YACH,CAAC;YACD,OAAO,KAAK,CAAA;QACd,CAAC,CAAA;QAED,OAAO,IAAI,CAAC,GAAG,EAAE,SAAS,CAAC,CAAA;IAC7B,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,MAAM,CAAC,KAAa;QACxB,OAAO,qBAAqB,CAAC,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,CAAA;IAClD,CAAC;IAED;;OAEG;IACH,SAAS,CAAC,MAAc;QACtB,MAAM,UAAU,GAAG,eAAe,CAAC,MAAM,CAAC,CAAA;QAC1C,MAAM,MAAM,GAAG,UAAU,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,UAAU,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAA;QAC/F,MAAM,MAAM,GAAG,IAAI,gBAAgB,CAAC,MAAM,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAA;QAC1D,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,UAAU,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAA;QAC1D,OAAO,MAAM,CAAA;IACf,CAAC;CACF"}
|
|
@@ -3,6 +3,7 @@ import type { SearchStrategy } from './types.js';
|
|
|
3
3
|
export declare function tokenize(text: string): Set<string>;
|
|
4
4
|
/** Lexical relevance score: distinct query tokens that appear in the content. Returns 0 when there is no overlap. */
|
|
5
5
|
export declare function tokenOverlapScore(queryTokens: Set<string>, content: string): number;
|
|
6
|
+
export declare const STOP_WORDS: Set<string>;
|
|
6
7
|
/**
|
|
7
8
|
* Keyword search strategy using token-overlap scoring.
|
|
8
9
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"keyword.d.ts","sourceRoot":"","sources":["../../../../src/storage/search/keyword.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAEhD,4EAA4E;AAC5E,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC,CAOlD;AAED,qHAAqH;AACrH,wBAAgB,iBAAiB,CAAC,WAAW,EAAE,GAAG,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,CAMnF;AAED;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,qBAAqB,EAAE,cAkBnC,CAAA"}
|
|
1
|
+
{"version":3,"file":"keyword.d.ts","sourceRoot":"","sources":["../../../../src/storage/search/keyword.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAEhD,4EAA4E;AAC5E,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC,CAOlD;AAED,qHAAqH;AACrH,wBAAgB,iBAAiB,CAAC,WAAW,EAAE,GAAG,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,CAMnF;AAED,eAAO,MAAM,UAAU,aA2HrB,CAAA;AAEF;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,qBAAqB,EAAE,cAkBnC,CAAA"}
|
|
@@ -14,6 +14,130 @@ export function tokenOverlapScore(queryTokens, content) {
|
|
|
14
14
|
}
|
|
15
15
|
return score;
|
|
16
16
|
}
|
|
17
|
+
export const STOP_WORDS = new Set([
|
|
18
|
+
'a',
|
|
19
|
+
'an',
|
|
20
|
+
'the',
|
|
21
|
+
'is',
|
|
22
|
+
'are',
|
|
23
|
+
'was',
|
|
24
|
+
'were',
|
|
25
|
+
'be',
|
|
26
|
+
'been',
|
|
27
|
+
'being',
|
|
28
|
+
'have',
|
|
29
|
+
'has',
|
|
30
|
+
'had',
|
|
31
|
+
'do',
|
|
32
|
+
'does',
|
|
33
|
+
'did',
|
|
34
|
+
'will',
|
|
35
|
+
'would',
|
|
36
|
+
'could',
|
|
37
|
+
'should',
|
|
38
|
+
'may',
|
|
39
|
+
'might',
|
|
40
|
+
'shall',
|
|
41
|
+
'can',
|
|
42
|
+
'need',
|
|
43
|
+
'dare',
|
|
44
|
+
'ought',
|
|
45
|
+
'used',
|
|
46
|
+
'to',
|
|
47
|
+
'of',
|
|
48
|
+
'in',
|
|
49
|
+
'for',
|
|
50
|
+
'on',
|
|
51
|
+
'with',
|
|
52
|
+
'at',
|
|
53
|
+
'by',
|
|
54
|
+
'from',
|
|
55
|
+
'as',
|
|
56
|
+
'into',
|
|
57
|
+
'through',
|
|
58
|
+
'during',
|
|
59
|
+
'before',
|
|
60
|
+
'after',
|
|
61
|
+
'above',
|
|
62
|
+
'below',
|
|
63
|
+
'between',
|
|
64
|
+
'out',
|
|
65
|
+
'off',
|
|
66
|
+
'over',
|
|
67
|
+
'under',
|
|
68
|
+
'again',
|
|
69
|
+
'further',
|
|
70
|
+
'then',
|
|
71
|
+
'once',
|
|
72
|
+
'and',
|
|
73
|
+
'but',
|
|
74
|
+
'or',
|
|
75
|
+
'nor',
|
|
76
|
+
'not',
|
|
77
|
+
'so',
|
|
78
|
+
'yet',
|
|
79
|
+
'both',
|
|
80
|
+
'either',
|
|
81
|
+
'neither',
|
|
82
|
+
'each',
|
|
83
|
+
'every',
|
|
84
|
+
'all',
|
|
85
|
+
'any',
|
|
86
|
+
'few',
|
|
87
|
+
'more',
|
|
88
|
+
'most',
|
|
89
|
+
'other',
|
|
90
|
+
'some',
|
|
91
|
+
'such',
|
|
92
|
+
'no',
|
|
93
|
+
'only',
|
|
94
|
+
'own',
|
|
95
|
+
'same',
|
|
96
|
+
'than',
|
|
97
|
+
'too',
|
|
98
|
+
'very',
|
|
99
|
+
'just',
|
|
100
|
+
'because',
|
|
101
|
+
'about',
|
|
102
|
+
'up',
|
|
103
|
+
'its',
|
|
104
|
+
'it',
|
|
105
|
+
'he',
|
|
106
|
+
'she',
|
|
107
|
+
'they',
|
|
108
|
+
'we',
|
|
109
|
+
'you',
|
|
110
|
+
'i',
|
|
111
|
+
'me',
|
|
112
|
+
'him',
|
|
113
|
+
'her',
|
|
114
|
+
'us',
|
|
115
|
+
'them',
|
|
116
|
+
'my',
|
|
117
|
+
'your',
|
|
118
|
+
'his',
|
|
119
|
+
'our',
|
|
120
|
+
'their',
|
|
121
|
+
'this',
|
|
122
|
+
'that',
|
|
123
|
+
'these',
|
|
124
|
+
'those',
|
|
125
|
+
'what',
|
|
126
|
+
'which',
|
|
127
|
+
'who',
|
|
128
|
+
'whom',
|
|
129
|
+
'when',
|
|
130
|
+
'where',
|
|
131
|
+
'why',
|
|
132
|
+
'how',
|
|
133
|
+
'if',
|
|
134
|
+
'while',
|
|
135
|
+
'although',
|
|
136
|
+
'though',
|
|
137
|
+
'unless',
|
|
138
|
+
'until',
|
|
139
|
+
'since',
|
|
140
|
+
]);
|
|
17
141
|
/**
|
|
18
142
|
* Keyword search strategy using token-overlap scoring.
|
|
19
143
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"keyword.js","sourceRoot":"","sources":["../../../../src/storage/search/keyword.ts"],"names":[],"mappings":"AAGA,4EAA4E;AAC5E,MAAM,UAAU,QAAQ,CAAC,IAAY;IACnC,OAAO,IAAI,GAAG,CACZ,IAAI;SACD,WAAW,EAAE;SACb,KAAK,CAAC,kBAAkB,CAAC;SACzB,MAAM,CAAC,OAAO,CAAC,CACnB,CAAA;AACH,CAAC;AAED,qHAAqH;AACrH,MAAM,UAAU,iBAAiB,CAAC,WAAwB,EAAE,OAAe;IACzE,IAAI,K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AI,WAAW,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;YAC/C,MAAM,KAAK,GAAG,iBAAiB,CAAC,WAAW,EAAE,GAAG,GAAG,IAAI,OAAO,EAAE,CAAC,CAAA;YACjE,IAAI,KAAK,GAAG,CAAC;gBAAE,MAAM,CAAC,IAAI,CAAC,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC,CAAA;QAC5C,CAAC;QAED,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,KAAK,CAAC,CAAA;QACxC,OAAO,MAAM,CAAA;IACf,CAAC;CACF,CAAA"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { Storage, StorageSearchResult } from '../storage.js';
|
|
2
|
+
import type { SearchStrategy } from './types.js';
|
|
3
|
+
/** Configuration for {@link QmdSearchStrategy}. */
|
|
4
|
+
export interface QmdSearchStrategyConfig {
|
|
5
|
+
/** Path to the SQLite database file for the QMD index. Defaults to `.<dir>-qmd.sqlite` alongside the storage directory. */
|
|
6
|
+
dbPath?: string;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* BM25 full-text search strategy powered by QMD.
|
|
10
|
+
*
|
|
11
|
+
* Maintains a SQLite-backed inverted index over the storage contents and uses
|
|
12
|
+
* BM25 scoring for relevance ranking. Accounts for term frequency, inverse
|
|
13
|
+
* document frequency, and document length normalization.
|
|
14
|
+
*
|
|
15
|
+
* Works only with {@link LocalFileStorage} — reads `baseDir` from the storage
|
|
16
|
+
* instance to know where files live on disk. Throws if passed a storage backend
|
|
17
|
+
* without a `baseDir` property.
|
|
18
|
+
*
|
|
19
|
+
* Requires `@tobilu/qmd` as a peer dependency.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* import { QmdSearchStrategy } from '@strands-agents/sdk/storage/search/qmd'
|
|
24
|
+
* import { LocalFileStorage } from '@strands-agents/sdk/storage'
|
|
25
|
+
*
|
|
26
|
+
* const storage = new LocalFileStorage('./memory/')
|
|
27
|
+
* const store = new FileMemoryStore({
|
|
28
|
+
* storage,
|
|
29
|
+
* search: new QmdSearchStrategy(),
|
|
30
|
+
* })
|
|
31
|
+
*
|
|
32
|
+
* const results = await store.search('authentication flow')
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export declare class QmdSearchStrategy implements SearchStrategy {
|
|
36
|
+
private _store;
|
|
37
|
+
private _storagePath;
|
|
38
|
+
private readonly _config;
|
|
39
|
+
constructor(config?: QmdSearchStrategyConfig);
|
|
40
|
+
/**
|
|
41
|
+
* Searches stored content using BM25 full-text search.
|
|
42
|
+
*
|
|
43
|
+
* Triggers a re-index of the backing filesystem before searching to ensure
|
|
44
|
+
* results reflect the latest writes. For high-throughput workloads, prefer
|
|
45
|
+
* calling {@link update} on a schedule rather than relying on per-search sync.
|
|
46
|
+
*
|
|
47
|
+
* @param storage - A LocalFileStorage instance (reads `baseDir` for the index path)
|
|
48
|
+
* @param query - Natural-language search query
|
|
49
|
+
* @returns Matched keys with BM25 relevance scores, ranked best-first
|
|
50
|
+
* @throws Error if storage is not a filesystem storage or `@tobilu/qmd` is not installed
|
|
51
|
+
*/
|
|
52
|
+
search(storage: Storage, query: string): Promise<StorageSearchResult[]>;
|
|
53
|
+
/**
|
|
54
|
+
* Re-indexes the backing storage directory without performing a search.
|
|
55
|
+
*
|
|
56
|
+
* @param storage - A LocalFileStorage instance
|
|
57
|
+
*/
|
|
58
|
+
update(storage: Storage): Promise<void>;
|
|
59
|
+
/**
|
|
60
|
+
* Closes the QMD store and releases resources (SQLite connection).
|
|
61
|
+
*/
|
|
62
|
+
close(): Promise<void>;
|
|
63
|
+
private _ensureStore;
|
|
64
|
+
private _resolveStoragePath;
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=qmd.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"qmd.d.ts","sourceRoot":"","sources":["../../../../src/storage/search/qmd.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAA;AACjE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAOhD,mDAAmD;AACnD,MAAM,WAAW,uBAAuB;IACtC,2HAA2H;IAC3H,MAAM,CAAC,EAAE,MAAM,CAAA;CAChB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,qBAAa,iBAAkB,YAAW,cAAc;IACtD,OAAO,CAAC,MAAM,CAAsB;IACpC,OAAO,CAAC,YAAY,CAAoB;IACxC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAyB;gBAErC,MAAM,CAAC,EAAE,uBAAuB;IAI5C;;;;;;;;;;;OAWG;IACG,MAAM,CAAC,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,mBAAmB,EAAE,CAAC;IAY7E;;;;OAIG;IACG,MAAM,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;IAK7C;;OAEG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;YAQd,YAAY;IA6B1B,OAAO,CAAC,mBAAmB;CAK5B"}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { STOP_WORDS, tokenize } from './keyword.js';
|
|
2
|
+
const COLLECTION_NAME = 'storage';
|
|
3
|
+
const COLLECTION_PREFIX = `${COLLECTION_NAME}/`;
|
|
4
|
+
/**
|
|
5
|
+
* BM25 full-text search strategy powered by QMD.
|
|
6
|
+
*
|
|
7
|
+
* Maintains a SQLite-backed inverted index over the storage contents and uses
|
|
8
|
+
* BM25 scoring for relevance ranking. Accounts for term frequency, inverse
|
|
9
|
+
* document frequency, and document length normalization.
|
|
10
|
+
*
|
|
11
|
+
* Works only with {@link LocalFileStorage} — reads `baseDir` from the storage
|
|
12
|
+
* instance to know where files live on disk. Throws if passed a storage backend
|
|
13
|
+
* without a `baseDir` property.
|
|
14
|
+
*
|
|
15
|
+
* Requires `@tobilu/qmd` as a peer dependency.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* import { QmdSearchStrategy } from '@strands-agents/sdk/storage/search/qmd'
|
|
20
|
+
* import { LocalFileStorage } from '@strands-agents/sdk/storage'
|
|
21
|
+
*
|
|
22
|
+
* const storage = new LocalFileStorage('./memory/')
|
|
23
|
+
* const store = new FileMemoryStore({
|
|
24
|
+
* storage,
|
|
25
|
+
* search: new QmdSearchStrategy(),
|
|
26
|
+
* })
|
|
27
|
+
*
|
|
28
|
+
* const results = await store.search('authentication flow')
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export class QmdSearchStrategy {
|
|
32
|
+
_store;
|
|
33
|
+
_storagePath;
|
|
34
|
+
_config;
|
|
35
|
+
constructor(config) {
|
|
36
|
+
this._config = config ?? {};
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Searches stored content using BM25 full-text search.
|
|
40
|
+
*
|
|
41
|
+
* Triggers a re-index of the backing filesystem before searching to ensure
|
|
42
|
+
* results reflect the latest writes. For high-throughput workloads, prefer
|
|
43
|
+
* calling {@link update} on a schedule rather than relying on per-search sync.
|
|
44
|
+
*
|
|
45
|
+
* @param storage - A LocalFileStorage instance (reads `baseDir` for the index path)
|
|
46
|
+
* @param query - Natural-language search query
|
|
47
|
+
* @returns Matched keys with BM25 relevance scores, ranked best-first
|
|
48
|
+
* @throws Error if storage is not a filesystem storage or `@tobilu/qmd` is not installed
|
|
49
|
+
*/
|
|
50
|
+
async search(storage, query) {
|
|
51
|
+
const store = await this._ensureStore(storage);
|
|
52
|
+
await store.update();
|
|
53
|
+
const ftsQuery = buildQuery(query);
|
|
54
|
+
if (!ftsQuery)
|
|
55
|
+
return [];
|
|
56
|
+
const results = await store.searchLex(ftsQuery);
|
|
57
|
+
return results.map((result) => ({
|
|
58
|
+
key: result.displayPath.slice(COLLECTION_PREFIX.length),
|
|
59
|
+
score: Math.abs(result.score) / (1 + Math.abs(result.score)),
|
|
60
|
+
}));
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Re-indexes the backing storage directory without performing a search.
|
|
64
|
+
*
|
|
65
|
+
* @param storage - A LocalFileStorage instance
|
|
66
|
+
*/
|
|
67
|
+
async update(storage) {
|
|
68
|
+
const store = await this._ensureStore(storage);
|
|
69
|
+
await store.update();
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Closes the QMD store and releases resources (SQLite connection).
|
|
73
|
+
*/
|
|
74
|
+
async close() {
|
|
75
|
+
if (this._store) {
|
|
76
|
+
await this._store.close();
|
|
77
|
+
this._store = undefined;
|
|
78
|
+
this._storagePath = undefined;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
async _ensureStore(storage) {
|
|
82
|
+
const storagePath = this._resolveStoragePath(storage);
|
|
83
|
+
if (this._store && this._storagePath === storagePath)
|
|
84
|
+
return this._store;
|
|
85
|
+
if (this._store)
|
|
86
|
+
await this.close();
|
|
87
|
+
let createStore;
|
|
88
|
+
try {
|
|
89
|
+
;
|
|
90
|
+
({ createStore } = await import('@tobilu/qmd'));
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
throw new Error('QmdSearchStrategy requires @tobilu/qmd — install it with: npm install @tobilu/qmd');
|
|
94
|
+
}
|
|
95
|
+
const { resolve, dirname, basename } = await import('node:path');
|
|
96
|
+
const { mkdir } = await import('node:fs/promises');
|
|
97
|
+
const resolvedPath = resolve(storagePath);
|
|
98
|
+
await mkdir(resolvedPath, { recursive: true });
|
|
99
|
+
const dbPath = this._config.dbPath ?? resolve(dirname(resolvedPath), `.${basename(resolvedPath)}-qmd.sqlite`);
|
|
100
|
+
this._store = await createStore({
|
|
101
|
+
dbPath,
|
|
102
|
+
config: {
|
|
103
|
+
collections: {
|
|
104
|
+
[COLLECTION_NAME]: { path: resolvedPath, pattern: '**/*' },
|
|
105
|
+
},
|
|
106
|
+
},
|
|
107
|
+
});
|
|
108
|
+
this._storagePath = storagePath;
|
|
109
|
+
return this._store;
|
|
110
|
+
}
|
|
111
|
+
_resolveStoragePath(storage) {
|
|
112
|
+
const baseDir = storage.baseDir;
|
|
113
|
+
if (typeof baseDir === 'string')
|
|
114
|
+
return baseDir;
|
|
115
|
+
throw new Error('QmdSearchStrategy requires a storage backend with a baseDir property (e.g. LocalFileStorage)');
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
function buildQuery(query) {
|
|
119
|
+
const terms = [...tokenize(query)].filter((term) => term.length > 1 && !STOP_WORDS.has(term));
|
|
120
|
+
if (terms.length === 0)
|
|
121
|
+
return null;
|
|
122
|
+
return terms.join(' ');
|
|
123
|
+
}
|
|
124
|
+
//# sourceMappingURL=qmd.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"qmd.js","sourceRoot":"","sources":["../../../../src/storage/search/qmd.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAA;AAEnD,MAAM,eAAe,GAAG,SAAS,CAAA;AACjC,MAAM,iBAAiB,GAAG,GAAG,eAAe,GAAG,CAAA;AAQ/C;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,OAAO,iBAAiB;IACpB,MAAM,CAAsB;IAC5B,YAAY,CAAoB;IACvB,OAAO,CAAyB;IAEjD,YAAY,MAAgC;QAC1C,IAAI,CAAC,OAAO,GAAG,MAAM,IAAI,EAAE,CAAA;IAC7B,CAAC;IAED;;;;;;;;;;;OAWG;IACH,KAAK,CAAC,MAAM,CAAC,OAAgB,EAAE,KAAa;QAC1C,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,YAAY,CAAC,OAAO,CAAC,CAAA;QAC9C,MAAM,KAAK,CAAC,MAAM,EAAE,CAAA;QACpB,MAAM,QAAQ,GAAG,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AAK,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAA;IAC7F,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,IAAI,CAAA;IACnC,OAAO,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;AACxB,CAAC"}
|
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import { ToolResultBlock } from '../../types/messages.js';
|
|
2
2
|
import type { Agent } from '../../agent/agent.js';
|
|
3
|
-
import type {
|
|
3
|
+
import type { BackgroundTasks } from '../../background-tasks/background-tasks.js';
|
|
4
|
+
import type { ToolUseData } from '../../hooks/events.js';
|
|
5
|
+
import type { ExecuteToolContext, MiddlewareRegistry } from '../../middleware/index.js';
|
|
4
6
|
import type { Meter } from '../../telemetry/meter.js';
|
|
5
7
|
import type { Tracer } from '../../telemetry/tracer.js';
|
|
6
8
|
import type { AgentStreamEvent, InvocationState } from '../../types/agent.js';
|
|
7
9
|
import type { Message, ToolUseBlock } from '../../types/messages.js';
|
|
10
|
+
import type { Tool, ToolContext } from '../tool.js';
|
|
8
11
|
/**
|
|
9
12
|
* Dependencies supplied for one tool-executor invocation.
|
|
10
13
|
*
|
|
@@ -21,6 +24,11 @@ export interface ToolExecutorOptions {
|
|
|
21
24
|
readonly meter: Meter;
|
|
22
25
|
/** Cancellation signal scoped to this executor invocation. */
|
|
23
26
|
readonly cancelSignal: AbortSignal;
|
|
27
|
+
readonly toolInterrupt?: ToolContext['interrupt'];
|
|
28
|
+
readonly middlewareInterrupt?: ExecuteToolContext['interrupt'];
|
|
29
|
+
readonly toolGuard?: (tool: Tool | undefined) => void;
|
|
30
|
+
readonly backgroundTasks?: BackgroundTasks;
|
|
31
|
+
readonly backgroundTaskPassId?: string;
|
|
24
32
|
}
|
|
25
33
|
/**
|
|
26
34
|
* Input for executing the tool calls from one model turn.
|
|
@@ -54,7 +62,9 @@ export declare abstract class ToolExecutor {
|
|
|
54
62
|
* @internal
|
|
55
63
|
*/
|
|
56
64
|
abstract execute(options: ToolExecutorOptions, input: ToolExecutionInput): AsyncGenerator<AgentStreamEvent, void, undefined>;
|
|
65
|
+
executeBackground(options: ToolExecutorOptions, toolUse: ToolUseData, tool: Tool, invocationState: InvocationState, onEvent: (event: AgentStreamEvent) => Promise<unknown>): Promise<ToolResultBlock>;
|
|
57
66
|
protected executeTool(options: ToolExecutorOptions, toolUseBlock: ToolUseBlock, invocationState: InvocationState): AsyncGenerator<AgentStreamEvent, ToolResultBlock, undefined>;
|
|
67
|
+
private _executeToolAttempt;
|
|
58
68
|
private _shouldRetry;
|
|
59
69
|
private _normalizeToolResultId;
|
|
60
70
|
protected _storePendingToolExecution(options: ToolExecutorOptions, assistantMessage: Message, completedToolResults: ReadonlyMap<string, ToolResultBlock>): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"executor.d.ts","sourceRoot":"","sources":["../../../../src/tools/executors/executor.ts"],"names":[],"mappings":"AAMA,OAAO,EAAa,eAAe,EAAE,MAAM,yBAAyB,CAAA;AAEpE,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,sBAAsB,CAAA;
|
|
1
|
+
{"version":3,"file":"executor.d.ts","sourceRoot":"","sources":["../../../../src/tools/executors/executor.ts"],"names":[],"mappings":"AAMA,OAAO,EAAa,eAAe,EAAE,MAAM,yBAAyB,CAAA;AAEpE,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,sBAAsB,CAAA;AACjD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,4CAA4C,CAAA;AACjF,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AACxD,OAAO,KAAK,EAAE,kBAAkB,EAAqB,kBAAkB,EAAE,MAAM,2BAA2B,CAAA;AAC1G,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,0BAA0B,C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AAE,OAAO,CAAA;IAClC,4DAA4D;IAC5D,QAAQ,CAAC,oBAAoB,CAAC,EAAE,WAAW,CAAC,MAAM,EAAE,eAAe,CAAC,CAAA;CACrE;AAED;;;;GAIG;AACH,8BAAsB,YAAY;IAChC;;;;;;;OAOG;IACH,QAAQ,CAAC,OAAO,CACd,OAAO,EAAE,mBAAmB,EAC5B,KAAK,EAAE,kBAAkB,GACxB,cAAc,CAAC,gBAAgB,EAAE,IAAI,EAAE,SAAS,CAAC;IAE9C,iBAAiB,CACrB,OAAO,EAAE,mBAAmB,EAC5B,OAAO,EAAE,WAAW,EACpB,IAAI,EAAE,IAAI,EACV,eAAe,EAAE,eAAe,EAChC,OAAO,EAAE,CAAC,KAAK,EAAE,gBAAgB,KAAK,OAAO,CAAC,OAAO,CAAC,GACrD,OAAO,CAAC,eAAe,CAAC;cAoBV,WAAW,CAC1B,OAAO,EAAE,mBAAmB,EAC5B,YAAY,EAAE,YAAY,EAC1B,eAAe,EAAE,eAAe,GAC/B,cAAc,CAAC,gBAAgB,EAAE,eAAe,EAAE,SAAS,CAAC;YAqFhD,mBAAmB;IAuBlC,OAAO,CAAC,YAAY;IAIpB,OAAO,CAAC,sBAAsB;IAe9B,SAAS,CAAC,0BAA0B,CAClC,OAAO,EAAE,mBAAmB,EAC5B,gBAAgB,EAAE,OAAO,EACzB,oBAAoB,EAAE,WAAW,CAAC,MAAM,EAAE,eAAe,CAAC,GACzD,IAAI;YAWQ,0BAA0B;YA2C1B,gBAAgB;CAiFhC"}
|
|
@@ -11,6 +11,24 @@ import { TextBlock, ToolResultBlock } from '../../types/messages.js';
|
|
|
11
11
|
* @internal
|
|
12
12
|
*/
|
|
13
13
|
export class ToolExecutor {
|
|
14
|
+
async executeBackground(options, toolUse, tool, invocationState, onEvent) {
|
|
15
|
+
while (true) {
|
|
16
|
+
const generator = this._executeToolAttempt(options, tool, toolUse, invocationState);
|
|
17
|
+
try {
|
|
18
|
+
let next = await generator.next();
|
|
19
|
+
while (!next.done) {
|
|
20
|
+
await onEvent(next.value);
|
|
21
|
+
next = await generator.next();
|
|
22
|
+
}
|
|
23
|
+
if (!this._shouldRetry(next.value, options.cancelSignal)) {
|
|
24
|
+
return this._normalizeToolResultId(next.value.result, toolUse.toolUseId);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
finally {
|
|
28
|
+
await generator.return(undefined);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
14
32
|
// Tool lookup and tool-body failures become ToolResultBlocks so the model can
|
|
15
33
|
// respond to them; lifecycle and middleware failures may still propagate.
|
|
16
34
|
async *executeTool(options, toolUseBlock, invocationState) {
|
|
@@ -58,17 +76,14 @@ export class ToolExecutor {
|
|
|
58
76
|
}
|
|
59
77
|
return this._normalizeToolResultId(afterToolCallEvent.result, toolUseBlock.toolUseId);
|
|
60
78
|
}
|
|
61
|
-
const
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
...(error !== undefined && { error }),
|
|
70
|
-
});
|
|
71
|
-
yield afterToolCallEvent;
|
|
79
|
+
const route = options.backgroundTasks?.routeToolCall(toolUse, registryTool, effectiveTool);
|
|
80
|
+
if (route === true) {
|
|
81
|
+
// No AfterToolCallEvent is emitted for the dispatch ack. BeforeToolCallEvent
|
|
82
|
+
// already fired above; the background run later emits the matching
|
|
83
|
+
// AfterToolCallEvent.
|
|
84
|
+
return this._normalizeToolResultId(await options.backgroundTasks.submitToolCall(toolUse, invocationState, options.backgroundTaskPassId, effectiveTool), toolUseBlock.toolUseId);
|
|
85
|
+
}
|
|
86
|
+
const afterToolCallEvent = yield* this._executeToolAttempt(options, effectiveTool, toolUse, invocationState, route);
|
|
72
87
|
if (this._shouldRetry(afterToolCallEvent, options.cancelSignal)) {
|
|
73
88
|
continue;
|
|
74
89
|
}
|
|
@@ -77,6 +92,19 @@ export class ToolExecutor {
|
|
|
77
92
|
return this._normalizeToolResultId(afterToolCallEvent.result, toolUseBlock.toolUseId);
|
|
78
93
|
}
|
|
79
94
|
}
|
|
95
|
+
async *_executeToolAttempt(options, tool, toolUse, invocationState, result) {
|
|
96
|
+
const toolResult = this._normalizeToolResultId(yield* this._executeToolWithMiddleware(options, tool, toolUse, invocationState, result), toolUse.toolUseId);
|
|
97
|
+
const event = new AfterToolCallEvent({
|
|
98
|
+
agent: options.agent,
|
|
99
|
+
toolUse,
|
|
100
|
+
tool,
|
|
101
|
+
result: toolResult,
|
|
102
|
+
invocationState,
|
|
103
|
+
...(toolResult.error !== undefined && { error: toolResult.error }),
|
|
104
|
+
});
|
|
105
|
+
yield event;
|
|
106
|
+
return event;
|
|
107
|
+
}
|
|
80
108
|
_shouldRetry(afterToolCallEvent, cancelSignal) {
|
|
81
109
|
return afterToolCallEvent.retry === true && !cancelSignal.aborted;
|
|
82
110
|
}
|
|
@@ -103,21 +131,25 @@ export class ToolExecutor {
|
|
|
103
131
|
completedToolResults: serializedResults,
|
|
104
132
|
});
|
|
105
133
|
}
|
|
106
|
-
async *_executeToolWithMiddleware(options, tool, toolUse, invocationState) {
|
|
134
|
+
async *_executeToolWithMiddleware(options, tool, toolUse, invocationState, result) {
|
|
107
135
|
const context = {
|
|
108
136
|
agent: options.agent,
|
|
109
137
|
tool,
|
|
110
138
|
toolUse: deepCopy(toolUse),
|
|
111
139
|
invocationState,
|
|
112
140
|
cancelSignal: options.cancelSignal,
|
|
113
|
-
interrupt:
|
|
141
|
+
interrupt: options.middlewareInterrupt ??
|
|
142
|
+
createMiddlewareInterrupt(options.agent._interruptState, `middleware:executeTool:${toolUse.toolUseId}`),
|
|
114
143
|
};
|
|
115
144
|
// async function* does not bind lexical `this`; capture the executor for the terminal callback.
|
|
116
145
|
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
117
146
|
const executor = this;
|
|
118
147
|
const middlewareResult = yield* options.middlewareRegistry.invoke(ExecuteToolStage, context, async function* (middlewareContext) {
|
|
119
|
-
|
|
120
|
-
return {
|
|
148
|
+
options.toolGuard?.(middlewareContext.tool);
|
|
149
|
+
return {
|
|
150
|
+
result: result ??
|
|
151
|
+
(yield* executor._executeToolCore(options, middlewareContext.tool, middlewareContext.toolUse, middlewareContext.invocationState)),
|
|
152
|
+
};
|
|
121
153
|
});
|
|
122
154
|
return middlewareResult.result;
|
|
123
155
|
}
|
|
@@ -144,7 +176,9 @@ export class ToolExecutor {
|
|
|
144
176
|
agent: options.agent,
|
|
145
177
|
invocationState,
|
|
146
178
|
cancelSignal: options.cancelSignal,
|
|
147
|
-
interrupt: (params) =>
|
|
179
|
+
interrupt: (params) => options.toolInterrupt
|
|
180
|
+
? options.toolInterrupt(params)
|
|
181
|
+
: interruptFromAgent(options.agent, `tool:${toolUse.toolUseId}:${params.name}`, params, 'tool'),
|
|
148
182
|
};
|
|
149
183
|
// Iterate manually to wrap raw tool events at the agent boundary and
|
|
150
184
|
// re-enter the tool span for every asynchronous step.
|