pi-resource-search 0.1.1 → 0.1.2

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.
@@ -3,7 +3,7 @@ import { homedir } from "node:os";
3
3
  import { join, resolve } from "node:path";
4
4
  import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
5
5
  import {
6
- fuzzyFilter,
6
+ fuzzyMatch,
7
7
  type AutocompleteItem,
8
8
  type AutocompleteProvider,
9
9
  type AutocompleteSuggestions,
@@ -123,6 +123,36 @@ function queryToken(text: string): string | undefined {
123
123
  return text.match(/(?:^|[ \t])\/([^\s/]*)$/)?.[1];
124
124
  }
125
125
 
126
+ // 命中档位:名称前缀 > 名称子串 > 名称模糊 > 描述/类型模糊
127
+ // 档位差固定 1000 分,保证名称命中永远排在描述命中前面
128
+ const TIER_NAME_PREFIX = 0;
129
+ const TIER_NAME_SUBSTRING = 1;
130
+ const TIER_NAME_FUZZY = 2;
131
+ const TIER_TEXT_FUZZY = 3;
132
+
133
+ function scoreResource(resource: Resource, query: string): number | undefined {
134
+ const q = query.toLowerCase();
135
+ const name = resource.name.toLowerCase();
136
+ if (name.startsWith(q)) return TIER_NAME_PREFIX * 1000 + (name.length - q.length);
137
+ const index = name.indexOf(q);
138
+ if (index >= 0) return TIER_NAME_SUBSTRING * 1000 + index;
139
+ const nameMatch = fuzzyMatch(q, name);
140
+ if (nameMatch.matches) return TIER_NAME_FUZZY * 1000 + nameMatch.score;
141
+ const textMatch = fuzzyMatch(q, `${resource.description} ${resource.kind}`);
142
+ if (textMatch.matches) return TIER_TEXT_FUZZY * 1000 + textMatch.score;
143
+ return undefined;
144
+ }
145
+
146
+ export function rankResources(resources: Resource[], query: string): Resource[] {
147
+ const scored: { resource: Resource; score: number }[] = [];
148
+ for (const resource of resources) {
149
+ const score = scoreResource(resource, query);
150
+ if (score !== undefined) scored.push({ resource, score });
151
+ }
152
+ scored.sort((a, b) => a.score - b.score);
153
+ return scored.map((entry) => entry.resource);
154
+ }
155
+
126
156
  function toItem(resource: Resource): AutocompleteItem {
127
157
  return { value: resource.value, label: `${resource.kind} ${resource.name}`, description: resource.description };
128
158
  }
@@ -145,7 +175,7 @@ function createProvider(pi: ExtensionAPI, current: AutocompleteProvider, resourc
145
175
  }
146
176
 
147
177
  const all = [...commands, ...resources];
148
- const matches = fuzzyFilter(all, query, (item) => `${item.name} ${item.description} ${item.kind}`).slice(0, MAX_RESULTS);
178
+ const matches = rankResources(all, query).slice(0, MAX_RESULTS);
149
179
  if (options.signal.aborted) return null;
150
180
  const native = await current.getSuggestions(lines, cursorLine, cursorCol, options);
151
181
  if (options.signal.aborted) return null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-resource-search",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Search Pi commands, skills, extensions, and installed packages from /keyword autocomplete.",
5
5
  "keywords": [
6
6
  "pi-package",
@@ -10,6 +10,14 @@
10
10
  "autocomplete"
11
11
  ],
12
12
  "license": "MIT",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "https://github.com/hagertygodoy398-tech/pi-resource-search.git"
16
+ },
17
+ "bugs": {
18
+ "url": "https://github.com/hagertygodoy398-tech/pi-resource-search/issues"
19
+ },
20
+ "homepage": "https://github.com/hagertygodoy398-tech/pi-resource-search#readme",
13
21
  "type": "module",
14
22
  "files": [
15
23
  "extensions",