ccc727d1bb
- Added internal URL completion types and router support for async scheme completion handlers. - Implemented `complete()` for local, memory, agent, artifact, omp, rule, and skill handlers with robust completion fallbacks. - Added internal URL autocomplete flow with context extraction, fuzzy matching, ranking, and a 25-item suggestion cap. - Integrated internal URL completion into prompt and editor autocomplete behaviors and added completion tests.
46 lines
1.2 KiB
TypeScript
46 lines
1.2 KiB
TypeScript
/**
|
|
* Protocol handler for rule:// URLs.
|
|
*
|
|
* URL forms:
|
|
* - rule://<name> - Reads rule content
|
|
*/
|
|
import { getActiveRules } from "../capability/rule";
|
|
import type { InternalResource, InternalUrl, ProtocolHandler, UrlCompletion } from "./types";
|
|
|
|
export class RuleProtocolHandler implements ProtocolHandler {
|
|
readonly scheme = "rule";
|
|
readonly immutable = true;
|
|
|
|
async resolve(url: InternalUrl): Promise<InternalResource> {
|
|
const rules = getActiveRules();
|
|
|
|
const ruleName = url.rawHost || url.hostname;
|
|
if (!ruleName) {
|
|
throw new Error("rule:// URL requires a rule name: rule://<name>");
|
|
}
|
|
|
|
const rule = rules.find(r => r.name === ruleName);
|
|
if (!rule) {
|
|
const available = rules.map(r => r.name);
|
|
const availableStr = available.length > 0 ? available.join(", ") : "none";
|
|
throw new Error(`Unknown rule: ${ruleName}\nAvailable: ${availableStr}`);
|
|
}
|
|
|
|
return {
|
|
url: url.href,
|
|
content: rule.content,
|
|
contentType: "text/markdown",
|
|
size: Buffer.byteLength(rule.content, "utf-8"),
|
|
sourcePath: rule.path,
|
|
notes: [],
|
|
};
|
|
}
|
|
|
|
async complete(): Promise<UrlCompletion[]> {
|
|
return getActiveRules().map(rule => ({
|
|
value: rule.name,
|
|
...(rule.description ? { description: rule.description } : {}),
|
|
}));
|
|
}
|
|
}
|