@octopi-agent/engine 0.56.0 → 0.65.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/config-schema/context.d.ts +1 -1
- package/dist/config-schema/context.js +1 -1
- package/dist/config-schema/engine.d.ts +43 -1
- package/dist/config-schema/substrate.d.ts +42 -0
- package/dist/config-schema/substrate.js +58 -0
- package/dist/config.d.ts +39 -1
- package/dist/harness/agent/builder.d.ts +14 -0
- package/dist/harness/agent/builder.js +14 -2
- package/dist/harness/agent/config-bridge.js +3 -0
- package/dist/harness/{context/capabilities → capabilities}/document/backends/docx.d.ts +1 -1
- package/dist/harness/{context/capabilities → capabilities}/document/backends/docx.js +1 -1
- package/dist/harness/{context/capabilities → capabilities}/document/backends/office-parser.d.ts +1 -1
- package/dist/harness/{context/capabilities → capabilities}/document/backends/office-parser.js +1 -1
- package/dist/harness/{context/capabilities → capabilities}/document/backends/pdf.d.ts +1 -1
- package/dist/harness/{context/capabilities → capabilities}/document/backends/pdf.js +13 -3
- package/dist/harness/{context/capabilities → capabilities}/document/backends/plain.d.ts +1 -1
- package/dist/harness/{context/capabilities → capabilities}/document/backends/plain.js +13 -4
- package/dist/harness/{context/capabilities → capabilities}/document/backends/sheet.d.ts +1 -1
- package/dist/harness/{context/capabilities → capabilities}/document/backends/sheet.js +25 -5
- package/dist/harness/{context/capabilities → capabilities}/document/backends/xmind.d.ts +1 -1
- package/dist/harness/{context/capabilities → capabilities}/document/backends/xmind.js +1 -1
- package/dist/harness/{context/capabilities → capabilities}/document/errors.d.ts +1 -1
- package/dist/harness/{context/capabilities → capabilities}/document/errors.js +1 -1
- package/dist/harness/capabilities/document/factory.d.ts +33 -0
- package/dist/harness/capabilities/document/factory.js +37 -0
- package/dist/harness/{context/capabilities → capabilities}/document/format.d.ts +1 -1
- package/dist/harness/{context/capabilities → capabilities}/document/format.js +1 -1
- package/dist/harness/{context/capabilities → capabilities}/document/html-to-md.d.ts +1 -1
- package/dist/harness/{context/capabilities → capabilities}/document/html-to-md.js +1 -1
- package/dist/harness/{context/capabilities → capabilities}/document/index.d.ts +3 -1
- package/dist/harness/{context/capabilities → capabilities}/document/index.js +2 -1
- package/dist/harness/{context/capabilities → capabilities}/document/legacy/factory.d.ts +3 -1
- package/dist/harness/{context/capabilities → capabilities}/document/legacy/factory.js +3 -1
- package/dist/harness/{context/capabilities → capabilities}/document/legacy/soffice.d.ts +3 -1
- package/dist/harness/{context/capabilities → capabilities}/document/legacy/soffice.js +44 -1
- package/dist/harness/{context/capabilities → capabilities}/document/port.d.ts +1 -1
- package/dist/harness/{context/capabilities → capabilities}/document/port.js +4 -4
- package/dist/harness/{context/capabilities → capabilities}/document/types.d.ts +11 -1
- package/dist/harness/{context/capabilities → capabilities}/document/types.js +1 -1
- package/dist/harness/capabilities/document/worker-port.d.ts +13 -0
- package/dist/harness/capabilities/document/worker-port.js +78 -0
- package/dist/harness/context/capabilities/index.d.ts +2 -2
- package/dist/harness/context/capabilities/index.js +2 -2
- package/dist/harness/context/constitution/default-agents.md +1 -0
- package/dist/harness/context/index.d.ts +2 -2
- package/dist/harness/context/index.js +1 -1
- package/dist/harness/context/layer-types.d.ts +26 -2
- package/dist/harness/context/layer-types.js +59 -11
- package/dist/harness/context/layers.d.ts +3 -0
- package/dist/harness/context/layers.js +38 -12
- package/dist/harness/context/system-prompt-assembler.d.ts +5 -0
- package/dist/harness/context/system-prompt-assembler.js +2 -0
- package/dist/harness/extension/plugin-ecosystem/tools/knowledge.d.ts +10 -0
- package/dist/harness/extension/plugin-ecosystem/tools/knowledge.js +115 -5
- package/dist/harness/extension/plugin-ecosystem/tools/memory.js +3 -0
- package/dist/harness/index.d.ts +5 -3
- package/dist/harness/index.js +3 -2
- package/dist/harness/knowledge/adapters.d.ts +8 -5
- package/dist/harness/knowledge/adapters.js +53 -13
- package/dist/harness/knowledge/catalog-types.d.ts +4 -0
- package/dist/harness/knowledge/client.d.ts +197 -0
- package/dist/harness/knowledge/client.js +237 -0
- package/dist/harness/knowledge/connector-fetcher.d.ts +2 -2
- package/dist/harness/knowledge/connectors.d.ts +3 -3
- package/dist/harness/knowledge/connectors.js +14 -8
- package/dist/harness/knowledge/db.d.ts +42 -3
- package/dist/harness/knowledge/db.js +466 -83
- package/dist/harness/knowledge/describe.d.ts +2 -0
- package/dist/harness/knowledge/describe.js +5 -1
- package/dist/harness/knowledge/document-extract-worker.d.ts +1 -0
- package/dist/harness/knowledge/document-extract-worker.js +68 -0
- package/dist/harness/knowledge/embed-runner.d.ts +53 -0
- package/dist/harness/knowledge/embed-runner.js +171 -0
- package/dist/harness/knowledge/engine-thread.d.ts +1 -0
- package/dist/harness/knowledge/engine-thread.js +302 -0
- package/dist/harness/knowledge/extract-document.d.ts +33 -0
- package/dist/harness/knowledge/extract-document.js +86 -0
- package/dist/harness/knowledge/fetchers.d.ts +14 -3
- package/dist/harness/knowledge/fetchers.js +52 -16
- package/dist/harness/knowledge/file-identity.d.ts +53 -0
- package/dist/harness/knowledge/file-identity.js +172 -0
- package/dist/harness/knowledge/file-index-store.d.ts +50 -0
- package/dist/harness/knowledge/file-index-store.js +132 -0
- package/dist/harness/knowledge/file-limits.d.ts +86 -0
- package/dist/harness/knowledge/file-limits.js +175 -0
- package/dist/harness/knowledge/fts.d.ts +60 -0
- package/dist/harness/knowledge/fts.js +218 -0
- package/dist/harness/knowledge/grounding.d.ts +3 -2
- package/dist/harness/knowledge/http-app.d.ts +50 -0
- package/dist/harness/knowledge/http-app.js +652 -0
- package/dist/harness/knowledge/http-bridge.d.ts +49 -0
- package/dist/harness/knowledge/http-bridge.js +17 -0
- package/dist/harness/knowledge/index-store.d.ts +166 -63
- package/dist/harness/knowledge/index-store.js +1025 -282
- package/dist/harness/knowledge/index.d.ts +47 -5
- package/dist/harness/knowledge/index.js +24 -2
- package/dist/harness/knowledge/ingest.d.ts +322 -49
- package/dist/harness/knowledge/ingest.js +1383 -267
- package/dist/harness/knowledge/job-control-state.d.ts +29 -0
- package/dist/harness/knowledge/job-control-state.js +44 -0
- package/dist/harness/knowledge/job-control.d.ts +35 -0
- package/dist/harness/knowledge/job-control.js +86 -0
- package/dist/harness/knowledge/job-queue.d.ts +62 -0
- package/dist/harness/knowledge/job-queue.js +173 -0
- package/dist/harness/knowledge/knowledge-serve-child.d.ts +1 -0
- package/dist/harness/knowledge/knowledge-serve-child.js +100 -0
- package/dist/harness/knowledge/local-stack.d.ts +27 -0
- package/dist/harness/knowledge/local-stack.js +63 -0
- package/dist/harness/knowledge/membership-store.d.ts +65 -0
- package/dist/harness/knowledge/membership-store.js +198 -0
- package/dist/harness/knowledge/network-guard.d.ts +11 -2
- package/dist/harness/knowledge/network-guard.js +130 -85
- package/dist/harness/knowledge/parse-text-in-worker.d.ts +32 -0
- package/dist/harness/knowledge/parse-text-in-worker.js +158 -0
- package/dist/harness/knowledge/query-service.d.ts +272 -0
- package/dist/harness/knowledge/query-service.js +326 -0
- package/dist/harness/knowledge/query-worker-client.d.ts +96 -0
- package/dist/harness/knowledge/query-worker-client.js +207 -0
- package/dist/harness/knowledge/query-worker.d.ts +1 -0
- package/dist/harness/knowledge/query-worker.js +186 -0
- package/dist/harness/knowledge/read-http.d.ts +35 -0
- package/dist/harness/knowledge/read-http.js +373 -0
- package/dist/harness/knowledge/retriever.d.ts +30 -5
- package/dist/harness/knowledge/retriever.js +98 -35
- package/dist/harness/knowledge/secret-scan.d.ts +13 -2
- package/dist/harness/knowledge/secret-scan.js +25 -2
- package/dist/harness/knowledge/serve.d.ts +48 -0
- package/dist/harness/knowledge/serve.js +336 -0
- package/dist/harness/knowledge/source-store.d.ts +83 -16
- package/dist/harness/knowledge/source-store.js +205 -78
- package/dist/harness/knowledge/start-service-process.d.ts +49 -0
- package/dist/harness/knowledge/start-service-process.js +158 -0
- package/dist/harness/knowledge/text-parse-worker.d.ts +1 -0
- package/dist/harness/knowledge/text-parse-worker.js +54 -0
- package/dist/harness/knowledge/topics.d.ts +13 -0
- package/dist/harness/knowledge/topics.js +48 -0
- package/dist/harness/knowledge/types.d.ts +1 -1
- package/dist/harness/knowledge/types.js +1 -1
- package/dist/harness/knowledge/vector-ann.d.ts +42 -0
- package/dist/harness/knowledge/vector-ann.js +68 -0
- package/dist/harness/knowledge/writer-lock.d.ts +13 -0
- package/dist/harness/knowledge/writer-lock.js +71 -0
- package/dist/harness/knowledge/writer-service.d.ts +194 -0
- package/dist/harness/knowledge/writer-service.js +355 -0
- package/dist/harness/knowledge/writer-worker-client.d.ts +102 -0
- package/dist/harness/knowledge/writer-worker-client.js +381 -0
- package/dist/harness/knowledge/writer-worker.d.ts +1 -0
- package/dist/harness/knowledge/writer-worker.js +189 -0
- package/dist/harness/memory/index.d.ts +3 -2
- package/dist/harness/memory/index.js +3 -1
- package/dist/harness/memory/retrieval-rank.d.ts +51 -0
- package/dist/harness/memory/retrieval-rank.js +60 -0
- package/dist/harness/memory/sqlite/embedding.d.ts +5 -2
- package/dist/harness/memory/sqlite/embedding.js +109 -16
- package/dist/harness/memory/sqlite/index.d.ts +2 -2
- package/dist/harness/memory/sqlite/index.js +1 -1
- package/dist/harness/memory/sqlite/keyword-search.d.ts +41 -2
- package/dist/harness/memory/sqlite/keyword-search.js +104 -17
- package/dist/harness/memory/sqlite/memory-store.d.ts +18 -4
- package/dist/harness/memory/sqlite/memory-store.js +89 -39
- package/dist/harness/memory/store.js +39 -21
- package/dist/harness/memory/types.d.ts +7 -0
- package/dist/harness/run/runner.d.ts +1 -0
- package/dist/harness/run/runner.js +4 -2
- package/dist/harness/session/attachments/service.d.ts +1 -1
- package/dist/harness/session/attachments/service.js +1 -1
- package/dist/harness/session/tasks/index.d.ts +1 -1
- package/dist/harness/session/tasks/index.js +1 -1
- package/dist/harness/session/tasks/render.d.ts +10 -0
- package/dist/harness/session/tasks/render.js +27 -0
- package/dist/index.d.ts +4 -2
- package/dist/index.js +2 -1
- package/dist/worker-path.d.ts +8 -0
- package/dist/worker-path.js +15 -0
- package/package.json +2 -2
|
@@ -12,17 +12,28 @@ import { MEMORY_TYPES } from '../types.js';
|
|
|
12
12
|
import { mapLegacyType } from '../gates.js';
|
|
13
13
|
import { AgentDatabase } from './agent-db.js';
|
|
14
14
|
import { searchTopK, parseEmbedding, serializeEmbedding } from './vector-search.js';
|
|
15
|
-
import { buildKeywordLikeSql, scoreKeywordFields,
|
|
15
|
+
import { buildKeywordLikeSql, scoreKeywordFields, tokenizeKeywordDetail, } from './keyword-search.js';
|
|
16
16
|
import { deleteMemoryVector, ensureMemoryVecTable, searchMemoryVectors, upsertMemoryVector, } from './sqlite-vec.js';
|
|
17
|
+
import { DEFAULT_MIN_KEYWORD_SCORE, DEFAULT_MIN_SIMILARITY, DEFAULT_SIMILARITY_WEIGHT, blendRank, qualityScore, resolveRetrievalKnobs, } from '../retrieval-rank.js';
|
|
17
18
|
function emptyTypes() {
|
|
18
19
|
return { fact: 0, method: 0, norm: 0 };
|
|
19
20
|
}
|
|
21
|
+
/** text 为空时仍允许的结构过滤浏览(type/tags/channel/status;min* 单独不算) */
|
|
22
|
+
function hasStructuralFilter(query) {
|
|
23
|
+
return (query.type !== undefined ||
|
|
24
|
+
(query.tags?.length ?? 0) > 0 ||
|
|
25
|
+
query.channel !== undefined ||
|
|
26
|
+
query.status !== undefined);
|
|
27
|
+
}
|
|
20
28
|
export class SqliteMemoryStore {
|
|
21
29
|
name = 'sqlite';
|
|
22
30
|
db;
|
|
23
31
|
embedding;
|
|
24
32
|
candidateCap;
|
|
25
33
|
vectorEngine;
|
|
34
|
+
defaultMinSimilarity;
|
|
35
|
+
similarityWeight;
|
|
36
|
+
defaultMinKeywordScore;
|
|
26
37
|
vecReady = false;
|
|
27
38
|
embeddingBackfillScheduled = false;
|
|
28
39
|
constructor(db, options) {
|
|
@@ -30,6 +41,9 @@ export class SqliteMemoryStore {
|
|
|
30
41
|
this.embedding = options?.embeddingProvider ?? null;
|
|
31
42
|
this.candidateCap = Math.max(20, options?.candidateCap ?? 500);
|
|
32
43
|
this.vectorEngine = options?.vectorEngine ?? 'auto';
|
|
44
|
+
this.defaultMinSimilarity = options?.minSimilarity ?? DEFAULT_MIN_SIMILARITY;
|
|
45
|
+
this.similarityWeight = options?.similarityWeight ?? DEFAULT_SIMILARITY_WEIGHT;
|
|
46
|
+
this.defaultMinKeywordScore = options?.minKeywordScore ?? DEFAULT_MIN_KEYWORD_SCORE;
|
|
33
47
|
if (this.embedding && this.vectorEngine !== 'js') {
|
|
34
48
|
const dims = this.embedding.dimensions;
|
|
35
49
|
if (dims > 0 && (db.sqliteVecEnabled || this.vectorEngine === 'sqlite-vec' || this.vectorEngine === 'auto')) {
|
|
@@ -103,16 +117,25 @@ export class SqliteMemoryStore {
|
|
|
103
117
|
return segments.join(' | ');
|
|
104
118
|
}
|
|
105
119
|
async retrieve(query) {
|
|
106
|
-
|
|
120
|
+
const knobs = resolveRetrievalKnobs(query.minSimilarity, query.minKeywordScore, this.defaultMinSimilarity, this.defaultMinKeywordScore);
|
|
121
|
+
const q = { ...query, minSimilarity: knobs.minSimilarity, minKeywordScore: knobs.minKeywordScore };
|
|
122
|
+
if (this.embedding && q.text?.trim()) {
|
|
107
123
|
await this.maybeBackfillEmbeddings();
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
if (
|
|
111
|
-
|
|
124
|
+
// embedding 路径:仅在 embed 硬失败时降级关键词;过不了相关性地板则宁缺返回空
|
|
125
|
+
try {
|
|
126
|
+
if (this.vecReady && this.vectorEngine !== 'js') {
|
|
127
|
+
const viaVec = await this.vecRetrieveAsync(q);
|
|
128
|
+
if (viaVec.length > 0)
|
|
129
|
+
return viaVec;
|
|
130
|
+
// vec 命中为空:可能是 vec 表稀疏,继续 hybrid(仍用 embedding);不走关键词填充
|
|
131
|
+
}
|
|
132
|
+
return await this.hybridRetrieve(q, { allowKeywordFallback: false });
|
|
133
|
+
}
|
|
134
|
+
catch {
|
|
135
|
+
return this.structuredRetrieve(q);
|
|
112
136
|
}
|
|
113
|
-
return this.hybridRetrieve(query);
|
|
114
137
|
}
|
|
115
|
-
return this.structuredRetrieve(
|
|
138
|
+
return this.structuredRetrieve(q);
|
|
116
139
|
}
|
|
117
140
|
visibilityWhere(query) {
|
|
118
141
|
const params = [];
|
|
@@ -130,7 +153,7 @@ export class SqliteMemoryStore {
|
|
|
130
153
|
}
|
|
131
154
|
return { sql, params };
|
|
132
155
|
}
|
|
133
|
-
/** sqlite-vec KNN 检索(需先 embed
|
|
156
|
+
/** sqlite-vec KNN 检索(需先 embed 查询文本);过相关性地板后按混合分排序 */
|
|
134
157
|
async vecRetrieveAsync(query) {
|
|
135
158
|
if (!this.embedding || !this.vecReady)
|
|
136
159
|
return [];
|
|
@@ -143,38 +166,47 @@ export class SqliteMemoryStore {
|
|
|
143
166
|
}
|
|
144
167
|
const vis = this.visibilityWhere(query);
|
|
145
168
|
const k = Math.max(query.limit ?? 10, 20);
|
|
169
|
+
const minSim = query.minSimilarity ?? this.defaultMinSimilarity;
|
|
146
170
|
const hits = searchMemoryVectors(this.db.raw, queryVec, k * 3, vis.sql, vis.params);
|
|
147
171
|
if (hits.length === 0)
|
|
148
172
|
return [];
|
|
149
|
-
const
|
|
173
|
+
const scored = [];
|
|
150
174
|
for (const hit of hits) {
|
|
175
|
+
// vec0 distance_metric=cosine → distance ∈ [0,2],similarity = 1 - distance
|
|
176
|
+
const similarity = 1 - hit.distance;
|
|
177
|
+
if (similarity < minSim)
|
|
178
|
+
continue;
|
|
151
179
|
const row = this.db.raw.prepare('SELECT * FROM memories WHERE id = ?').get(hit.id);
|
|
152
180
|
if (!row)
|
|
153
181
|
continue;
|
|
154
|
-
|
|
155
|
-
if (
|
|
182
|
+
const entry = this.rowToEntry(row);
|
|
183
|
+
if (!this.passesApplyFilters(entry, query))
|
|
184
|
+
continue;
|
|
185
|
+
scored.push({ e: entry, similarity });
|
|
186
|
+
if (scored.length >= (query.limit ?? 10) * 2)
|
|
156
187
|
break;
|
|
157
188
|
}
|
|
158
|
-
|
|
159
|
-
if (filtered.length === 0)
|
|
189
|
+
if (scored.length === 0)
|
|
160
190
|
return [];
|
|
161
|
-
return this.
|
|
191
|
+
return this.sortLimitScored(scored, query);
|
|
162
192
|
}
|
|
163
193
|
/**
|
|
164
|
-
* 混合检索:SQL
|
|
165
|
-
*
|
|
194
|
+
* 混合检索:SQL 预筛候选 → JS 余弦 topK → 相关性地板。
|
|
195
|
+
* 命中不足 limit 时**不**用无关条目填充。
|
|
196
|
+
* @param options.allowKeywordFallback - 仅 embed 硬失败场景允许;默认 false(宁缺)
|
|
166
197
|
*/
|
|
167
|
-
async hybridRetrieve(query) {
|
|
198
|
+
async hybridRetrieve(query, options) {
|
|
168
199
|
let queryVec;
|
|
169
200
|
try {
|
|
170
201
|
queryVec = await this.embedding.embed(query.text);
|
|
171
202
|
}
|
|
172
203
|
catch {
|
|
173
|
-
// embedding
|
|
204
|
+
// embedding 服务不可用:仅此时退回关键词
|
|
174
205
|
return this.structuredRetrieve(query);
|
|
175
206
|
}
|
|
176
207
|
const vis = this.visibilityWhere(query);
|
|
177
208
|
const cap = this.candidateCap;
|
|
209
|
+
const minSim = query.minSimilarity ?? this.defaultMinSimilarity;
|
|
178
210
|
let sql = `SELECT id, type, content, source, confidence, importance, access_count, last_accessed_at, created_at, decay_factor, tags, embedding, status, channel, future_use, anchors, evidence, deleted, deleted_at, deleted_by, deleted_reason, deleted_meta, reinforced_at
|
|
179
211
|
FROM memories WHERE embedding IS NOT NULL${vis.sql}`;
|
|
180
212
|
const params = [...vis.params];
|
|
@@ -200,16 +232,20 @@ export class SqliteMemoryStore {
|
|
|
200
232
|
return { embedding: emb, item: this.rowToEntry(r) };
|
|
201
233
|
})
|
|
202
234
|
.filter((c) => c !== null);
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
if (
|
|
235
|
+
// searchTopK 的 maxDistance = 1 - minSimilarity(cosine distance)
|
|
236
|
+
const vectorResults = searchTopK(queryVec, candidates, Math.min(candidates.length, Math.max(50, (query.limit ?? 10) * 5)), 1 - minSim);
|
|
237
|
+
const scored = vectorResults
|
|
238
|
+
.map(r => ({ e: r.item, similarity: 1 - r.distance }))
|
|
239
|
+
.filter(s => s.similarity >= minSim && this.passesApplyFilters(s.e, query));
|
|
240
|
+
// 有相关命中则只返回这些(宁缺毋滥,不回填关键词噪声)
|
|
241
|
+
if (scored.length > 0) {
|
|
242
|
+
return this.sortLimitScored(scored, query);
|
|
243
|
+
}
|
|
244
|
+
// 地板之下零命中:默认不降级关键词,避免「向量说无关、关键词硬填」
|
|
245
|
+
if (options?.allowKeywordFallback) {
|
|
210
246
|
return this.structuredRetrieve(query);
|
|
211
247
|
}
|
|
212
|
-
return
|
|
248
|
+
return [];
|
|
213
249
|
}
|
|
214
250
|
structuredRetrieve(query) {
|
|
215
251
|
let sql = 'SELECT * FROM memories WHERE 1=1';
|
|
@@ -235,8 +271,8 @@ export class SqliteMemoryStore {
|
|
|
235
271
|
sql += ' AND importance >= ?';
|
|
236
272
|
params.push(query.minImportance);
|
|
237
273
|
}
|
|
238
|
-
const tokens =
|
|
239
|
-
const like = buildKeywordLikeSql(tokens);
|
|
274
|
+
const tokens = tokenizeKeywordDetail(query.text);
|
|
275
|
+
const like = buildKeywordLikeSql(tokens.all);
|
|
240
276
|
if (like.sql) {
|
|
241
277
|
sql += like.sql;
|
|
242
278
|
params.push(...like.params);
|
|
@@ -244,7 +280,8 @@ export class SqliteMemoryStore {
|
|
|
244
280
|
const rows = this.db.raw.prepare(sql).all(...params);
|
|
245
281
|
const entries = rows.map(r => this.rowToEntry(r));
|
|
246
282
|
const filtered = this.filterByTags(entries, query);
|
|
247
|
-
|
|
283
|
+
const minKeyword = query.minKeywordScore ?? this.defaultMinKeywordScore;
|
|
284
|
+
if (tokens.all.length > 0) {
|
|
248
285
|
const scored = filtered
|
|
249
286
|
.map((e) => ({
|
|
250
287
|
e,
|
|
@@ -256,10 +293,10 @@ export class SqliteMemoryStore {
|
|
|
256
293
|
evidence: e.evidence,
|
|
257
294
|
}, tokens),
|
|
258
295
|
}))
|
|
259
|
-
.filter((s) => s.score
|
|
296
|
+
.filter((s) => s.score >= minKeyword);
|
|
260
297
|
scored.sort((a, b) => {
|
|
261
|
-
const rankA = a.score * 10 + a.e
|
|
262
|
-
const rankB = b.score * 10 + b.e
|
|
298
|
+
const rankA = a.score * 10 + qualityScore(a.e);
|
|
299
|
+
const rankB = b.score * 10 + qualityScore(b.e);
|
|
263
300
|
return rankB - rankA;
|
|
264
301
|
});
|
|
265
302
|
const limit = query.limit ?? 10;
|
|
@@ -273,7 +310,15 @@ export class SqliteMemoryStore {
|
|
|
273
310
|
}
|
|
274
311
|
return Promise.resolve(results);
|
|
275
312
|
}
|
|
276
|
-
|
|
313
|
+
// 无检索词:仅结构过滤时浏览;禁止空 query 扫全库
|
|
314
|
+
if (!hasStructuralFilter(query))
|
|
315
|
+
return Promise.resolve([]);
|
|
316
|
+
filtered.sort((a, b) => qualityScore(b) - qualityScore(a));
|
|
317
|
+
return Promise.resolve(this.limitAndUpdate(filtered, query));
|
|
318
|
+
}
|
|
319
|
+
/** 单条结构化过滤(向量路径逐条判断) */
|
|
320
|
+
passesApplyFilters(entry, query) {
|
|
321
|
+
return this.applyFilters([entry], query).length > 0;
|
|
277
322
|
}
|
|
278
323
|
/** 与 InMemory 对齐:tags 交集过滤 */
|
|
279
324
|
filterByTags(entries, query) {
|
|
@@ -299,12 +344,17 @@ export class SqliteMemoryStore {
|
|
|
299
344
|
}
|
|
300
345
|
return this.filterByTags(results, query);
|
|
301
346
|
}
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
347
|
+
/** 向量命中:similarity 参与排序(禁止仅按 quality 顶掉贴题条目) */
|
|
348
|
+
sortLimitScored(scored, query) {
|
|
349
|
+
const w = this.similarityWeight;
|
|
350
|
+
scored.sort((a, b) => {
|
|
351
|
+
const rankA = blendRank(a.similarity, qualityScore(a.e), w);
|
|
352
|
+
const rankB = blendRank(b.similarity, qualityScore(b.e), w);
|
|
353
|
+
return rankB - rankA;
|
|
307
354
|
});
|
|
355
|
+
return this.limitAndUpdate(scored.map((s) => s.e), query);
|
|
356
|
+
}
|
|
357
|
+
limitAndUpdate(entries, query) {
|
|
308
358
|
const limit = query.limit ?? 10;
|
|
309
359
|
const results = entries.slice(0, limit);
|
|
310
360
|
if (query.updateAccess !== false) {
|
|
@@ -6,10 +6,18 @@
|
|
|
6
6
|
import { randomUUID } from 'node:crypto';
|
|
7
7
|
import { MEMORY_TYPES } from './types.js';
|
|
8
8
|
import { mapLegacyType } from './gates.js';
|
|
9
|
-
import { scoreKeywordFields,
|
|
9
|
+
import { scoreKeywordFields, tokenizeKeywordDetail, } from './sqlite/keyword-search.js';
|
|
10
|
+
import { DEFAULT_MIN_KEYWORD_SCORE, qualityScore, resolveRetrievalKnobs, } from './retrieval-rank.js';
|
|
10
11
|
function emptyTypes() {
|
|
11
12
|
return { fact: 0, method: 0, norm: 0 };
|
|
12
13
|
}
|
|
14
|
+
/** 是否带结构过滤(text 为空时仍可按 type/tags/channel/status 浏览;min* 单独不算) */
|
|
15
|
+
function hasStructuralFilter(query) {
|
|
16
|
+
return (query.type !== undefined ||
|
|
17
|
+
(query.tags?.length ?? 0) > 0 ||
|
|
18
|
+
query.channel !== undefined ||
|
|
19
|
+
query.status !== undefined);
|
|
20
|
+
}
|
|
13
21
|
function normalizeStatus(entry) {
|
|
14
22
|
return entry.status ?? 'active';
|
|
15
23
|
}
|
|
@@ -48,6 +56,7 @@ export class InMemoryMemoryStore {
|
|
|
48
56
|
return id;
|
|
49
57
|
}
|
|
50
58
|
async retrieve(query) {
|
|
59
|
+
const { minKeywordScore } = resolveRetrievalKnobs(query.minSimilarity, query.minKeywordScore, undefined, DEFAULT_MIN_KEYWORD_SCORE);
|
|
51
60
|
let results = Array.from(this.entries.values()).filter((e) => passesVisibility(e, query));
|
|
52
61
|
if (query.type) {
|
|
53
62
|
const types = Array.isArray(query.type) ? query.type : [query.type];
|
|
@@ -66,28 +75,37 @@ export class InMemoryMemoryStore {
|
|
|
66
75
|
if (query.minImportance !== undefined) {
|
|
67
76
|
results = results.filter((e) => e.importance >= query.minImportance);
|
|
68
77
|
}
|
|
69
|
-
const tokens =
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
}
|
|
85
|
-
else {
|
|
86
|
-
scored = results.map((e) => ({ e, score: 0 }));
|
|
78
|
+
const tokens = tokenizeKeywordDetail(query.text ?? '');
|
|
79
|
+
if (tokens.all.length === 0) {
|
|
80
|
+
// 无检索词:仅在显式结构过滤时浏览;禁止空 query 扫全库
|
|
81
|
+
if (!hasStructuralFilter(query))
|
|
82
|
+
return [];
|
|
83
|
+
results.sort((a, b) => qualityScore(b) - qualityScore(a));
|
|
84
|
+
const limit = query.limit ?? 10;
|
|
85
|
+
results = results.slice(0, limit);
|
|
86
|
+
if (query.updateAccess !== false) {
|
|
87
|
+
for (const entry of results) {
|
|
88
|
+
entry.accessCount++;
|
|
89
|
+
entry.lastAccessedAt = Date.now();
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return results;
|
|
87
93
|
}
|
|
94
|
+
const scored = results
|
|
95
|
+
.map((e) => ({
|
|
96
|
+
e,
|
|
97
|
+
score: scoreKeywordFields({
|
|
98
|
+
content: e.content,
|
|
99
|
+
tags: e.tags,
|
|
100
|
+
futureUse: e.futureUse,
|
|
101
|
+
anchors: e.anchors,
|
|
102
|
+
evidence: e.evidence,
|
|
103
|
+
}, tokens),
|
|
104
|
+
}))
|
|
105
|
+
.filter((s) => s.score >= minKeywordScore);
|
|
88
106
|
scored.sort((a, b) => {
|
|
89
|
-
const rankA = a.score * 10 + a.e
|
|
90
|
-
const rankB = b.score * 10 + b.e
|
|
107
|
+
const rankA = a.score * 10 + qualityScore(a.e);
|
|
108
|
+
const rankB = b.score * 10 + qualityScore(b.e);
|
|
91
109
|
return rankB - rankA;
|
|
92
110
|
});
|
|
93
111
|
const limit = query.limit ?? 10;
|
|
@@ -66,6 +66,13 @@ export interface MemoryQuery {
|
|
|
66
66
|
includeDeleted?: boolean;
|
|
67
67
|
status?: MemoryStatus | MemoryStatus[];
|
|
68
68
|
channel?: MemoryChannel | MemoryChannel[];
|
|
69
|
+
/**
|
|
70
|
+
* 向量路径最低余弦相似度;低于则丢弃(宁缺毋滥)。
|
|
71
|
+
* 未写出时用 store 默认(注入路径偏严,search 可放宽为 0)。
|
|
72
|
+
*/
|
|
73
|
+
minSimilarity?: number;
|
|
74
|
+
/** 关键词路径最低命中分;未写出时用 store 默认 */
|
|
75
|
+
minKeywordScore?: number;
|
|
69
76
|
}
|
|
70
77
|
/** 记忆统计 */
|
|
71
78
|
export interface MemoryStats {
|
|
@@ -14,7 +14,7 @@ import { createSessionStateMachine } from '../session/state-machine.js';
|
|
|
14
14
|
import { HeuristicTokenEstimator } from '../context/index.js';
|
|
15
15
|
import { extractLayerQuery } from '../context/layer-types.js';
|
|
16
16
|
import { withRuntimeEnvironmentInjection } from '../context/runtime-datetime.js';
|
|
17
|
-
import { renderSessionTasksInjection } from '../session/tasks/render.js';
|
|
17
|
+
import { extractTaskQueryHints, renderSessionTasksInjection } from '../session/tasks/render.js';
|
|
18
18
|
import { createRunId } from './run-scope.js';
|
|
19
19
|
import { DEFAULT_TOOL_ISOLATION, resolveToolIsolationCwd, } from '../extension/execution-environment/isolation.js';
|
|
20
20
|
import { mkdirSync } from 'node:fs';
|
|
@@ -602,6 +602,7 @@ export class SessionAwareRunner {
|
|
|
602
602
|
// 结果只写入 runContext / RunScope,不作为「Agent 当前会话状态」
|
|
603
603
|
if (this.systemPromptAssembler) {
|
|
604
604
|
try {
|
|
605
|
+
const topicHints = extractTaskQueryHints(session.tasks ?? []);
|
|
605
606
|
const assembled = await this.systemPromptAssembler({
|
|
606
607
|
sessionId,
|
|
607
608
|
agentId: _agentId,
|
|
@@ -609,6 +610,7 @@ export class SessionAwareRunner {
|
|
|
609
610
|
persona: basePrompt,
|
|
610
611
|
injectedContext: effectiveRunConfig.injectedContext,
|
|
611
612
|
contextWindow: effectiveRunConfig.contextWindow,
|
|
613
|
+
topicHints,
|
|
612
614
|
signal,
|
|
613
615
|
});
|
|
614
616
|
runContext.systemPrompt = assembled.systemPrompt;
|
|
@@ -624,7 +626,7 @@ export class SessionAwareRunner {
|
|
|
624
626
|
agentId: _agentId,
|
|
625
627
|
manifest: assembled.manifest,
|
|
626
628
|
enabledLayerIds: assembled.manifest.layers.map((l) => l.id),
|
|
627
|
-
query: extractLayerQuery(session.messages),
|
|
629
|
+
query: extractLayerQuery(session.messages, { topicHints }),
|
|
628
630
|
assembledAt,
|
|
629
631
|
},
|
|
630
632
|
});
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* 原则:原件 + .manifest.json 为权威;抽取/索引可重建。
|
|
7
7
|
*/
|
|
8
8
|
import { type AttachmentLimits, type AttachmentUploadInput, type SessionAttachment } from './types.js';
|
|
9
|
-
import type { DocumentPort } from '../../
|
|
9
|
+
import type { DocumentPort } from '../../capabilities/document/types.js';
|
|
10
10
|
export interface SessionAttachmentServiceOptions {
|
|
11
11
|
/** 通常 `OCTOPI_HOME/sessions` */
|
|
12
12
|
sessionsDir: string;
|
|
@@ -13,7 +13,7 @@ import { resolveSessionAttachmentsPaths } from './paths.js';
|
|
|
13
13
|
import { attachmentExtension, isPathInside, sanitizeAttachmentName } from './sanitize.js';
|
|
14
14
|
import { classifyAttachmentKind, extractCompanionName, extractText, isPlainReadable, } from './extract.js';
|
|
15
15
|
import { DEFAULT_ATTACHMENT_LIMITS, } from './types.js';
|
|
16
|
-
import { isDocumentExtractError } from '../../
|
|
16
|
+
import { isDocumentExtractError } from '../../capabilities/document/errors.js';
|
|
17
17
|
export class SessionAttachmentService {
|
|
18
18
|
sessionsDir;
|
|
19
19
|
limits;
|
|
@@ -7,5 +7,5 @@ export type { SessionTask, SessionTaskStatus, SessionTaskEventType } from './typ
|
|
|
7
7
|
export { ACTIVE_SESSION_TASK_STATUSES } from './types.js';
|
|
8
8
|
export { SessionTaskService } from './service.js';
|
|
9
9
|
export type { SessionTaskListFilter, SessionTaskCommandActor } from './service.js';
|
|
10
|
-
export { renderSessionTasksInjection } from './render.js';
|
|
10
|
+
export { renderSessionTasksInjection, extractTaskQueryHints } from './render.js';
|
|
11
11
|
export { createSessionTaskTools, createTaskListTool, createTaskCreateTool, createTaskPlanTool, createTaskCompleteTool, createTaskPauseTool, createTaskResumeTool, createTaskDropTool, createTaskNoteTool, } from './tools.js';
|
|
@@ -5,5 +5,5 @@
|
|
|
5
5
|
*/
|
|
6
6
|
export { ACTIVE_SESSION_TASK_STATUSES } from './types.js';
|
|
7
7
|
export { SessionTaskService } from './service.js';
|
|
8
|
-
export { renderSessionTasksInjection } from './render.js';
|
|
8
|
+
export { renderSessionTasksInjection, extractTaskQueryHints } from './render.js';
|
|
9
9
|
export { createSessionTaskTools, createTaskListTool, createTaskCreateTool, createTaskPlanTool, createTaskCompleteTool, createTaskPauseTool, createTaskResumeTool, createTaskDropTool, createTaskNoteTool, } from './tools.js';
|
|
@@ -10,3 +10,13 @@ import type { SessionTask } from './types.js';
|
|
|
10
10
|
* @returns 注入字符串;无活跃 goal 时返回空串
|
|
11
11
|
*/
|
|
12
12
|
export declare function renderSessionTasksInjection(tasks: SessionTask[]): string;
|
|
13
|
+
/**
|
|
14
|
+
* 从会话任务提取检索主题线索(Memory/Cognition 层 query 用)。
|
|
15
|
+
*
|
|
16
|
+
* 取未闭合 goal 描述 + 当前 open step,压缩为一行,避免注入全文。
|
|
17
|
+
*
|
|
18
|
+
* @param tasks - 会话全部任务
|
|
19
|
+
* @param maxChars - 线索字符上限(默认 240)
|
|
20
|
+
* @returns 单行主题线索;无活跃任务时空串
|
|
21
|
+
*/
|
|
22
|
+
export declare function extractTaskQueryHints(tasks: SessionTask[], maxChars?: number): string;
|
|
@@ -55,3 +55,30 @@ export function renderSessionTasksInjection(tasks) {
|
|
|
55
55
|
lines.push('</session_tasks>');
|
|
56
56
|
return lines.join('\n');
|
|
57
57
|
}
|
|
58
|
+
/**
|
|
59
|
+
* 从会话任务提取检索主题线索(Memory/Cognition 层 query 用)。
|
|
60
|
+
*
|
|
61
|
+
* 取未闭合 goal 描述 + 当前 open step,压缩为一行,避免注入全文。
|
|
62
|
+
*
|
|
63
|
+
* @param tasks - 会话全部任务
|
|
64
|
+
* @param maxChars - 线索字符上限(默认 240)
|
|
65
|
+
* @returns 单行主题线索;无活跃任务时空串
|
|
66
|
+
*/
|
|
67
|
+
export function extractTaskQueryHints(tasks, maxChars = 240) {
|
|
68
|
+
const goals = tasks
|
|
69
|
+
.filter((t) => !t.parentId && isActive(t.status))
|
|
70
|
+
.sort((a, b) => a.createdAt - b.createdAt);
|
|
71
|
+
if (goals.length === 0)
|
|
72
|
+
return '';
|
|
73
|
+
const parts = [];
|
|
74
|
+
for (const goal of goals.slice(0, 3)) {
|
|
75
|
+
parts.push(goal.description.trim());
|
|
76
|
+
const currentStep = tasks
|
|
77
|
+
.filter((t) => t.parentId === goal.id && t.status === 'open')
|
|
78
|
+
.sort((a, b) => (a.order ?? 0) - (b.order ?? 0))[0];
|
|
79
|
+
if (currentStep?.description) {
|
|
80
|
+
parts.push(currentStep.description.trim());
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return parts.filter(Boolean).join(';').slice(0, maxChars);
|
|
84
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -71,8 +71,10 @@ export type { AgentPersona, ModelConfig, AgentDefinition } from './harness/share
|
|
|
71
71
|
export { DefaultToolBus } from './harness/extension/plugin-ecosystem/tools/tool-bus.js';
|
|
72
72
|
export { getBuiltinTools, createShellTool, createFileReadTool, createFileWriteTool, createFileListTool } from './harness/extension/plugin-ecosystem/tools/builtin.js';
|
|
73
73
|
export { createToolSet } from './harness/extension/plugin-ecosystem/tools/tool-set.js';
|
|
74
|
-
export { createSummaryPort, applyToolOutputGate, createCompactEngine
|
|
75
|
-
export
|
|
74
|
+
export { createSummaryPort, applyToolOutputGate, createCompactEngine } from './harness/context/capabilities/index.js';
|
|
75
|
+
export { createDefaultDocumentPort, createDocumentPortFromConfig, DocumentExtractError } from './harness/capabilities/document/index.js';
|
|
76
|
+
export type { SummaryPort, SummaryPolicy, ContentUnit, ToolSummarySupport, CompactEngine } from './harness/context/capabilities/index.js';
|
|
77
|
+
export type { DocumentPort, ExtractResult, ExtractSource } from './harness/capabilities/document/index.js';
|
|
76
78
|
export type { ToolSet, ToolSetConfig } from './harness/extension/plugin-ecosystem/tools/tool-set.js';
|
|
77
79
|
export { createWebSearchTool } from './harness/extension/plugin-ecosystem/tools/web-search.js';
|
|
78
80
|
export type { WebSearchToolOptions } from './harness/extension/plugin-ecosystem/tools/web-search.js';
|
package/dist/index.js
CHANGED
|
@@ -47,7 +47,8 @@ export { DefaultSkillManager, FileSystemSkillSource } from './harness/extension/
|
|
|
47
47
|
export { DefaultToolBus } from './harness/extension/plugin-ecosystem/tools/tool-bus.js';
|
|
48
48
|
export { getBuiltinTools, createShellTool, createFileReadTool, createFileWriteTool, createFileListTool } from './harness/extension/plugin-ecosystem/tools/builtin.js';
|
|
49
49
|
export { createToolSet } from './harness/extension/plugin-ecosystem/tools/tool-set.js';
|
|
50
|
-
export { createSummaryPort, applyToolOutputGate, createCompactEngine
|
|
50
|
+
export { createSummaryPort, applyToolOutputGate, createCompactEngine } from './harness/context/capabilities/index.js';
|
|
51
|
+
export { createDefaultDocumentPort, createDocumentPortFromConfig, DocumentExtractError } from './harness/capabilities/document/index.js';
|
|
51
52
|
export { createWebSearchTool } from './harness/extension/plugin-ecosystem/tools/web-search.js';
|
|
52
53
|
export { createSessionHistoryPort, resolveHistoryAccess, } from './harness/session/history/index.js';
|
|
53
54
|
export { createSessionHistoryTools, createSessionSearchTool, createSessionReadTool, } from './harness/extension/plugin-ecosystem/tools/session-history.js';
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Module path resolver for workers and child processes.
|
|
3
|
+
*
|
|
4
|
+
* Neither Worker nor child_process can execute TypeScript. Production loads
|
|
5
|
+
* compiled files under packages/pkg/dist. Vitest imports src TypeScript, so we
|
|
6
|
+
* map src -> dist. Callers MUST pass their own `import.meta.url`.
|
|
7
|
+
*/
|
|
8
|
+
export declare function resolveWorkerUrl(moduleFile: string, fromUrl: string): URL;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Module path resolver for workers and child processes.
|
|
3
|
+
*
|
|
4
|
+
* Neither Worker nor child_process can execute TypeScript. Production loads
|
|
5
|
+
* compiled files under packages/pkg/dist. Vitest imports src TypeScript, so we
|
|
6
|
+
* map src -> dist. Callers MUST pass their own `import.meta.url`.
|
|
7
|
+
*/
|
|
8
|
+
export function resolveWorkerUrl(moduleFile, fromUrl) {
|
|
9
|
+
const normalized = fromUrl.replace(/\\/g, '/');
|
|
10
|
+
if (normalized.includes('/src/') && normalized.endsWith('.ts')) {
|
|
11
|
+
const distBase = normalized.replace('/src/', '/dist/').replace(/\.ts$/, '.js');
|
|
12
|
+
return new URL(moduleFile, distBase);
|
|
13
|
+
}
|
|
14
|
+
return new URL(moduleFile, fromUrl);
|
|
15
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@octopi-agent/engine",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.65.0",
|
|
4
4
|
"description": "Octopi Engine — Harness product domains, library integrations, and embeddable Agent.run()",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -80,7 +80,7 @@
|
|
|
80
80
|
},
|
|
81
81
|
"dependencies": {
|
|
82
82
|
"@modelcontextprotocol/sdk": "^1.29.0",
|
|
83
|
-
"@octopi-agent/core": "0.
|
|
83
|
+
"@octopi-agent/core": "0.65.0",
|
|
84
84
|
"js-yaml": "^4.3.2",
|
|
85
85
|
"zod": "^4.4.3"
|
|
86
86
|
},
|