@khanglvm/outline-cli 0.1.7 → 0.2.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/src/tools.js CHANGED
@@ -11,6 +11,7 @@ import { NAVIGATION_TOOLS } from "./tools.navigation.js";
11
11
  import { MUTATION_TOOLS } from "./tools.mutation.js";
12
12
  import { PLATFORM_TOOLS } from "./tools.platform.js";
13
13
  import { EXTENDED_TOOLS } from "./tools.extended.js";
14
+ import { MEMORY_TOOLS, memoryResolveTool, recordToolObservation } from "./memory-store.js";
14
15
  import { validateToolArgs } from "./tool-arg-schemas.js";
15
16
  import { summarizeSafeText } from "./summary-redaction.js";
16
17
  import {
@@ -107,6 +108,175 @@ function normalizeDocumentRow(row, view = "summary", excerptChars = 280) {
107
108
  return summary;
108
109
  }
109
110
 
111
+ function nonEmptyString(value) {
112
+ const text = String(value || "").trim();
113
+ return text || "";
114
+ }
115
+
116
+ function firstFilterReference(args = {}, keys = [], arrayKeys = []) {
117
+ for (const key of keys) {
118
+ const value = nonEmptyString(args[key]);
119
+ if (value) {
120
+ return { key, value };
121
+ }
122
+ }
123
+ for (const key of arrayKeys) {
124
+ if (!Array.isArray(args[key])) {
125
+ continue;
126
+ }
127
+ for (const item of args[key]) {
128
+ const value = nonEmptyString(item);
129
+ if (value) {
130
+ return { key, value };
131
+ }
132
+ }
133
+ }
134
+ return null;
135
+ }
136
+
137
+ function resolutionFromFilterCandidate(resolved, ref, config, args = {}) {
138
+ const candidates = resolved.result?.candidates || [];
139
+ const topCandidate = candidates[0] || null;
140
+ const strict = args.strict !== false;
141
+ const strictThreshold = Math.max(0, toInteger(args.strictThreshold, 85));
142
+ const topScore = Number(topCandidate?.score || 0);
143
+
144
+ if (!topCandidate) {
145
+ return {
146
+ error: {
147
+ failed: {
148
+ kind: ref.key,
149
+ value: ref.value,
150
+ status: "not_found",
151
+ },
152
+ memory: resolved.result?.memory || null,
153
+ },
154
+ };
155
+ }
156
+ if (strict && topScore < strictThreshold) {
157
+ return {
158
+ error: {
159
+ failed: {
160
+ kind: ref.key,
161
+ value: ref.value,
162
+ status: "low_confidence",
163
+ strictThreshold,
164
+ },
165
+ candidate: topCandidate,
166
+ candidates,
167
+ memory: resolved.result?.memory || null,
168
+ },
169
+ };
170
+ }
171
+
172
+ const live = (resolved.result?.live || []).find((item) => item?.ok && item.candidate?.id === topCandidate.id)
173
+ || (resolved.result?.live || [])[0]
174
+ || null;
175
+ if (live && !live.ok) {
176
+ return {
177
+ error: {
178
+ failed: {
179
+ kind: ref.key,
180
+ value: ref.value,
181
+ status: "hydrate_failed",
182
+ error: live.error,
183
+ statusCode: live.status,
184
+ },
185
+ candidate: topCandidate,
186
+ memory: resolved.result?.memory || live.memory || null,
187
+ },
188
+ };
189
+ }
190
+
191
+ return {
192
+ id: topCandidate.id,
193
+ resultField: config.resultField,
194
+ resolution: compactValue({
195
+ mode: "memory",
196
+ kind: ref.key,
197
+ value: ref.value,
198
+ id: topCandidate.id,
199
+ name: topCandidate.name,
200
+ email: topCandidate.email,
201
+ candidate: topCandidate,
202
+ live,
203
+ memory: resolved.result?.memory || null,
204
+ }) || { id: topCandidate.id },
205
+ };
206
+ }
207
+
208
+ async function resolveDocumentFilter(ctx, args = {}, config) {
209
+ const exact = nonEmptyString(args[config.idKey]);
210
+ if (exact) {
211
+ return { id: exact, resultField: config.resultField };
212
+ }
213
+ const ref = firstFilterReference(args, config.queryKeys, config.arrayQueryKeys);
214
+ if (!ref) {
215
+ return null;
216
+ }
217
+
218
+ const threshold = Math.max(0, toInteger(args.strictThreshold, 85));
219
+ const resolved = await memoryResolveTool(ctx, {
220
+ query: ref.value,
221
+ type: config.type,
222
+ profile: args.profile,
223
+ limit: Math.max(1, toInteger(args.resolveLimit, 5)),
224
+ hydrateLimit: 1,
225
+ maxAgeHours: args.maxAgeHours,
226
+ minScore: args.minScore === undefined && args.strict !== false ? threshold : args.minScore,
227
+ strict: args.strict,
228
+ strictThreshold: args.strictThreshold,
229
+ fallbackSearch: args.fallbackSearch,
230
+ fallbackMinScore: args.fallbackMinScore === undefined && args.strict !== false ? threshold : args.fallbackMinScore,
231
+ fallbackLimit: args.fallbackLimit,
232
+ refresh: args.refresh !== false,
233
+ maxAttempts: args.maxAttempts,
234
+ });
235
+
236
+ return {
237
+ ...resolutionFromFilterCandidate(resolved, ref, config, args),
238
+ resultField: config.resultField,
239
+ };
240
+ }
241
+
242
+ async function resolveDocumentFilters(ctx, args = {}) {
243
+ const filters = [
244
+ await resolveDocumentFilter(ctx, args, {
245
+ type: "collection",
246
+ idKey: "collectionId",
247
+ resultField: "collectionId",
248
+ queryKeys: ["collectionQuery", "collectionRef"],
249
+ arrayQueryKeys: ["collectionQueries", "collectionRefs"],
250
+ }),
251
+ await resolveDocumentFilter(ctx, args, {
252
+ type: "user",
253
+ idKey: "userId",
254
+ resultField: "userId",
255
+ queryKeys: ["userQuery", "userRef"],
256
+ arrayQueryKeys: ["userQueries", "userRefs"],
257
+ }),
258
+ ].filter(Boolean);
259
+
260
+ const failed = filters.find((filter) => filter?.error);
261
+ if (failed) {
262
+ return { error: failed.error, resultField: failed.resultField };
263
+ }
264
+
265
+ const ids = {};
266
+ const resolution = {};
267
+ for (const filter of filters) {
268
+ ids[filter.resultField] = filter.id;
269
+ if (filter.resolution) {
270
+ resolution[filter.resultField] = filter.resolution;
271
+ }
272
+ }
273
+
274
+ return {
275
+ ids,
276
+ resolution: Object.keys(resolution).length > 0 ? resolution : null,
277
+ };
278
+ }
279
+
110
280
  function normalizeCollectionRow(row, view = "summary") {
111
281
  if (view === "full") {
112
282
  return row;
@@ -274,10 +444,27 @@ async function documentsSearchTool(ctx, args) {
274
444
  throw new CliError("documents.search requires args.query or args.queries[]");
275
445
  }
276
446
 
447
+ const resolvedFilters = await resolveDocumentFilters(ctx, args);
448
+ if (resolvedFilters.error) {
449
+ return {
450
+ tool: "documents.search",
451
+ mode,
452
+ profile: ctx.profile.id,
453
+ result: compactValue({
454
+ ok: false,
455
+ status: resolvedFilters.error.status || "not_found",
456
+ [resolvedFilters.resultField || "filterId"]: "",
457
+ resolution: resolvedFilters.error,
458
+ data: [],
459
+ merged: [],
460
+ }) || {},
461
+ };
462
+ }
463
+
277
464
  const baseBody = compactValue({
278
- collectionId: args.collectionId,
465
+ collectionId: resolvedFilters.ids?.collectionId || args.collectionId,
279
466
  documentId: args.documentId,
280
- userId: args.userId,
467
+ userId: resolvedFilters.ids?.userId || args.userId,
281
468
  statusFilter: normalizeStatusFilter(args.statusFilter),
282
469
  dateFilter: args.dateFilter,
283
470
  shareId: args.shareId,
@@ -357,6 +544,7 @@ async function documentsSearchTool(ctx, args) {
357
544
  result: {
358
545
  ...perQuery[0],
359
546
  merged,
547
+ resolution: resolvedFilters.resolution,
360
548
  },
361
549
  };
362
550
  }
@@ -369,18 +557,34 @@ async function documentsSearchTool(ctx, args) {
369
557
  result: {
370
558
  perQuery,
371
559
  merged,
560
+ resolution: resolvedFilters.resolution,
372
561
  },
373
562
  };
374
563
  }
375
564
 
376
565
  async function documentsListTool(ctx, args) {
566
+ const resolvedFilters = await resolveDocumentFilters(ctx, args);
567
+ if (resolvedFilters.error) {
568
+ return {
569
+ tool: "documents.list",
570
+ profile: ctx.profile.id,
571
+ result: compactValue({
572
+ ok: false,
573
+ status: resolvedFilters.error.status || "not_found",
574
+ [resolvedFilters.resultField || "filterId"]: "",
575
+ resolution: resolvedFilters.error,
576
+ data: [],
577
+ }) || {},
578
+ };
579
+ }
580
+
377
581
  const body = compactValue({
378
582
  limit: toInteger(args.limit, 25),
379
583
  offset: toInteger(args.offset, 0),
380
584
  sort: args.sort,
381
585
  direction: args.direction,
382
- collectionId: args.collectionId,
383
- userId: args.userId,
586
+ collectionId: resolvedFilters.ids?.collectionId || args.collectionId,
587
+ userId: resolvedFilters.ids?.userId || args.userId,
384
588
  backlinkDocumentId: args.backlinkDocumentId,
385
589
  statusFilter: normalizeStatusFilter(args.statusFilter),
386
590
  }) || {};
@@ -413,7 +617,9 @@ async function documentsListTool(ctx, args) {
413
617
  return {
414
618
  tool: "documents.list",
415
619
  profile: ctx.profile.id,
416
- result: payload,
620
+ result: resolvedFilters.resolution
621
+ ? compactValue({ ...payload, resolution: resolvedFilters.resolution }) || payload
622
+ : payload,
417
623
  };
418
624
  }
419
625
 
@@ -829,7 +1035,7 @@ export const TOOLS = {
829
1035
  },
830
1036
  "documents.search": {
831
1037
  signature:
832
- "documents.search(args: { query?: string; queries?: string[]; mode?: 'semantic' | 'titles'; limit?: number; offset?: number; collectionId?: string; documentId?: string; userId?: string; statusFilter?: string[]; dateFilter?: 'day'|'week'|'month'|'year'; snippetMinWords?: number; snippetMaxWords?: number; sort?: string; direction?: 'ASC'|'DESC'; view?: 'summary'|'ids'|'full'; includePolicies?: boolean; merge?: boolean; concurrency?: number; })",
1038
+ "documents.search(args: { query?: string; queries?: string[]; mode?: 'semantic' | 'titles'; limit?: number; offset?: number; collectionId?: string; collectionQuery?: string; userId?: string; userQuery?: string; documentId?: string; statusFilter?: string[]; dateFilter?: 'day'|'week'|'month'|'year'; snippetMinWords?: number; snippetMaxWords?: number; sort?: string; direction?: 'ASC'|'DESC'; view?: 'summary'|'ids'|'full'; includePolicies?: boolean; merge?: boolean; concurrency?: number; })",
833
1039
  description: "Search documents with single or multi-query batch in one invocation.",
834
1040
  usageExample: {
835
1041
  tool: "documents.search",
@@ -843,6 +1049,7 @@ export const TOOLS = {
843
1049
  },
844
1050
  bestPractices: [
845
1051
  "Prefer `queries[]` batch mode to reduce round trips.",
1052
+ "Pass collectionQuery/collectionRefs or userQuery/userRefs when filtering by a remembered collection or user name.",
846
1053
  "Use `view=ids` for planning and follow with documents.info only on selected IDs.",
847
1054
  "Tune snippetMinWords/snippetMaxWords to control context window size.",
848
1055
  ],
@@ -850,7 +1057,7 @@ export const TOOLS = {
850
1057
  },
851
1058
  "documents.list": {
852
1059
  signature:
853
- "documents.list(args?: { limit?: number; offset?: number; sort?: string; direction?: 'ASC'|'DESC'; collectionId?: string; parentDocumentId?: string | null; rootOnly?: boolean; userId?: string; statusFilter?: string[]; view?: 'ids'|'summary'|'full'; includePolicies?: boolean })",
1060
+ "documents.list(args?: { limit?: number; offset?: number; sort?: string; direction?: 'ASC'|'DESC'; collectionId?: string; collectionQuery?: string; userId?: string; userQuery?: string; parentDocumentId?: string | null; rootOnly?: boolean; statusFilter?: string[]; view?: 'ids'|'summary'|'full'; includePolicies?: boolean })",
854
1061
  description: "List documents with filtering and pagination.",
855
1062
  usageExample: {
856
1063
  tool: "documents.list",
@@ -863,6 +1070,7 @@ export const TOOLS = {
863
1070
  },
864
1071
  bestPractices: [
865
1072
  "Use small page sizes (10-25) and iterate with offset.",
1073
+ "Pass collectionQuery/collectionRefs or userQuery/userRefs to resolve remembered filters before listing.",
866
1074
  "Use rootOnly=true (or parentDocumentId=null) to list only collection root pages.",
867
1075
  "Use summary view unless the full document body is required.",
868
1076
  ],
@@ -1009,6 +1217,7 @@ export const TOOLS = {
1009
1217
  ...MUTATION_TOOLS,
1010
1218
  ...EXTENDED_TOOLS,
1011
1219
  ...PLATFORM_TOOLS,
1220
+ ...MEMORY_TOOLS,
1012
1221
  };
1013
1222
 
1014
1223
  const TOOL_ALIAS_DEFS = [
@@ -1305,6 +1514,14 @@ export async function invokeTool(ctx, name, args = {}) {
1305
1514
  }
1306
1515
  : result;
1307
1516
 
1517
+ try {
1518
+ await recordToolObservation(ctx, resolution.resolvedName, normalizedArgs, enriched);
1519
+ } catch (err) {
1520
+ if (ctx?.memory?.strict === true) {
1521
+ throw err;
1522
+ }
1523
+ }
1524
+
1308
1525
  if (normalizedArgs.compact ?? true) {
1309
1526
  return compactValue(enriched) || {};
1310
1527
  }