@gitpagedocs/tools 1.1.62 → 1.1.64
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/README.md +33 -33
- package/package.json +1 -1
- package/src/ai/bootstrap.ts +50 -50
- package/src/ai/catalog.ts +188 -188
- package/src/ai/factory.ts +14 -14
- package/src/ai/http/streaming.ts +60 -60
- package/src/ai/index.ts +27 -27
- package/src/ai/legacy-adapter.ts +33 -33
- package/src/ai/model-registry.ts +13 -13
- package/src/ai/providers/anthropic-provider.ts +83 -83
- package/src/ai/providers/cohere-provider.ts +66 -66
- package/src/ai/providers/gemini-provider.ts +84 -84
- package/src/ai/providers/ollama-provider.ts +69 -69
- package/src/ai/providers/openai-compatible-provider.ts +75 -75
- package/src/ai/providers/shared.ts +72 -72
- package/src/ai/registry.ts +29 -29
- package/src/cache/file-cache.ts +86 -86
- package/src/cache/index.ts +9 -9
- package/src/cache/memory-cache.ts +47 -47
- package/src/cache/web-storage-cache.ts +91 -91
- package/src/config/config-loader.ts +59 -59
- package/src/config/index.ts +1 -1
- package/src/constants/config.ts +8 -8
- package/src/constants/index.ts +1 -1
- package/src/crypto/doc-access.ts +62 -62
- package/src/crypto/index.ts +4 -4
- package/src/crypto/node-crypto-service.ts +87 -87
- package/src/crypto/web-crypto-service.ts +102 -102
- package/src/crypto/web.ts +4 -4
- package/src/documentation/doc-generator.ts +74 -74
- package/src/documentation/doc-updater.ts +47 -47
- package/src/documentation/index.ts +18 -18
- package/src/documentation/marker-patcher.ts +33 -33
- package/src/documentation/sections.ts +84 -84
- package/src/errors/app-error.ts +84 -84
- package/src/errors/index.ts +1 -1
- package/src/filesystem/file-service.ts +121 -121
- package/src/filesystem/index.ts +2 -2
- package/src/index.ts +25 -25
- package/src/logger/index.ts +2 -2
- package/src/logger/logger.ts +73 -73
- package/src/logger/redaction.ts +43 -43
- package/src/platform/index.ts +1 -1
- package/src/platform/user-config-dir.ts +48 -48
- package/src/ports/ai.ts +109 -109
- package/src/ports/cache.ts +16 -16
- package/src/ports/config.ts +21 -21
- package/src/ports/crypto.ts +33 -33
- package/src/ports/index.ts +15 -15
- package/src/ports/logger.ts +23 -23
- package/src/ports/security.ts +42 -42
- package/src/security/credential-vault.ts +117 -117
- package/src/security/file-vault-storage.ts +25 -25
- package/src/security/index.ts +8 -8
- package/src/security/migrate-plaintext-key.ts +38 -38
- package/src/security/password-gate.ts +62 -62
- package/src/security/web-storage-vault-storage.ts +23 -23
- package/src/security/web.ts +9 -9
package/src/errors/app-error.ts
CHANGED
|
@@ -1,84 +1,84 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Error taxonomy for @gitpagedocs/tools.
|
|
3
|
-
*
|
|
4
|
-
* Every domain error extends AppError so consumers can branch on a stable
|
|
5
|
-
* machine-readable `code` and surface `details` without leaking secrets.
|
|
6
|
-
*/
|
|
7
|
-
export type ErrorCode =
|
|
8
|
-
| "APP_ERROR"
|
|
9
|
-
| "VALIDATION_ERROR"
|
|
10
|
-
| "PROVIDER_ERROR"
|
|
11
|
-
| "CONFIGURATION_ERROR"
|
|
12
|
-
| "DOCUMENTATION_ERROR"
|
|
13
|
-
| "REPOSITORY_ERROR"
|
|
14
|
-
| "SECURITY_ERROR"
|
|
15
|
-
| "CACHE_ERROR";
|
|
16
|
-
|
|
17
|
-
export interface AppErrorOptions {
|
|
18
|
-
/** Machine-readable, stable across versions. */
|
|
19
|
-
readonly code?: ErrorCode;
|
|
20
|
-
/** Underlying error, preserved for diagnostics. */
|
|
21
|
-
readonly cause?: unknown;
|
|
22
|
-
/** Structured, secret-free context. */
|
|
23
|
-
readonly details?: Record<string, unknown>;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export class AppError extends Error {
|
|
27
|
-
readonly code: ErrorCode;
|
|
28
|
-
readonly details?: Record<string, unknown>;
|
|
29
|
-
|
|
30
|
-
constructor(message: string, options: AppErrorOptions = {}) {
|
|
31
|
-
super(message, options.cause === undefined ? undefined : { cause: options.cause });
|
|
32
|
-
this.name = new.target.name;
|
|
33
|
-
this.code = options.code ?? "APP_ERROR";
|
|
34
|
-
this.details = options.details;
|
|
35
|
-
// Preserve prototype chain when targeting ES with downleveled classes.
|
|
36
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export class ValidationError extends AppError {
|
|
41
|
-
constructor(message: string, options: Omit<AppErrorOptions, "code"> = {}) {
|
|
42
|
-
super(message, { ...options, code: "VALIDATION_ERROR" });
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export class ProviderError extends AppError {
|
|
47
|
-
constructor(message: string, options: Omit<AppErrorOptions, "code"> = {}) {
|
|
48
|
-
super(message, { ...options, code: "PROVIDER_ERROR" });
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export class ConfigurationError extends AppError {
|
|
53
|
-
constructor(message: string, options: Omit<AppErrorOptions, "code"> = {}) {
|
|
54
|
-
super(message, { ...options, code: "CONFIGURATION_ERROR" });
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export class DocumentationError extends AppError {
|
|
59
|
-
constructor(message: string, options: Omit<AppErrorOptions, "code"> = {}) {
|
|
60
|
-
super(message, { ...options, code: "DOCUMENTATION_ERROR" });
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export class RepositoryError extends AppError {
|
|
65
|
-
constructor(message: string, options: Omit<AppErrorOptions, "code"> = {}) {
|
|
66
|
-
super(message, { ...options, code: "REPOSITORY_ERROR" });
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export class SecurityError extends AppError {
|
|
71
|
-
constructor(message: string, options: Omit<AppErrorOptions, "code"> = {}) {
|
|
72
|
-
super(message, { ...options, code: "SECURITY_ERROR" });
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export class CacheError extends AppError {
|
|
77
|
-
constructor(message: string, options: Omit<AppErrorOptions, "code"> = {}) {
|
|
78
|
-
super(message, { ...options, code: "CACHE_ERROR" });
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
export function isAppError(value: unknown): value is AppError {
|
|
83
|
-
return value instanceof AppError;
|
|
84
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Error taxonomy for @gitpagedocs/tools.
|
|
3
|
+
*
|
|
4
|
+
* Every domain error extends AppError so consumers can branch on a stable
|
|
5
|
+
* machine-readable `code` and surface `details` without leaking secrets.
|
|
6
|
+
*/
|
|
7
|
+
export type ErrorCode =
|
|
8
|
+
| "APP_ERROR"
|
|
9
|
+
| "VALIDATION_ERROR"
|
|
10
|
+
| "PROVIDER_ERROR"
|
|
11
|
+
| "CONFIGURATION_ERROR"
|
|
12
|
+
| "DOCUMENTATION_ERROR"
|
|
13
|
+
| "REPOSITORY_ERROR"
|
|
14
|
+
| "SECURITY_ERROR"
|
|
15
|
+
| "CACHE_ERROR";
|
|
16
|
+
|
|
17
|
+
export interface AppErrorOptions {
|
|
18
|
+
/** Machine-readable, stable across versions. */
|
|
19
|
+
readonly code?: ErrorCode;
|
|
20
|
+
/** Underlying error, preserved for diagnostics. */
|
|
21
|
+
readonly cause?: unknown;
|
|
22
|
+
/** Structured, secret-free context. */
|
|
23
|
+
readonly details?: Record<string, unknown>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export class AppError extends Error {
|
|
27
|
+
readonly code: ErrorCode;
|
|
28
|
+
readonly details?: Record<string, unknown>;
|
|
29
|
+
|
|
30
|
+
constructor(message: string, options: AppErrorOptions = {}) {
|
|
31
|
+
super(message, options.cause === undefined ? undefined : { cause: options.cause });
|
|
32
|
+
this.name = new.target.name;
|
|
33
|
+
this.code = options.code ?? "APP_ERROR";
|
|
34
|
+
this.details = options.details;
|
|
35
|
+
// Preserve prototype chain when targeting ES with downleveled classes.
|
|
36
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export class ValidationError extends AppError {
|
|
41
|
+
constructor(message: string, options: Omit<AppErrorOptions, "code"> = {}) {
|
|
42
|
+
super(message, { ...options, code: "VALIDATION_ERROR" });
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export class ProviderError extends AppError {
|
|
47
|
+
constructor(message: string, options: Omit<AppErrorOptions, "code"> = {}) {
|
|
48
|
+
super(message, { ...options, code: "PROVIDER_ERROR" });
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export class ConfigurationError extends AppError {
|
|
53
|
+
constructor(message: string, options: Omit<AppErrorOptions, "code"> = {}) {
|
|
54
|
+
super(message, { ...options, code: "CONFIGURATION_ERROR" });
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export class DocumentationError extends AppError {
|
|
59
|
+
constructor(message: string, options: Omit<AppErrorOptions, "code"> = {}) {
|
|
60
|
+
super(message, { ...options, code: "DOCUMENTATION_ERROR" });
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export class RepositoryError extends AppError {
|
|
65
|
+
constructor(message: string, options: Omit<AppErrorOptions, "code"> = {}) {
|
|
66
|
+
super(message, { ...options, code: "REPOSITORY_ERROR" });
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export class SecurityError extends AppError {
|
|
71
|
+
constructor(message: string, options: Omit<AppErrorOptions, "code"> = {}) {
|
|
72
|
+
super(message, { ...options, code: "SECURITY_ERROR" });
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export class CacheError extends AppError {
|
|
77
|
+
constructor(message: string, options: Omit<AppErrorOptions, "code"> = {}) {
|
|
78
|
+
super(message, { ...options, code: "CACHE_ERROR" });
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function isAppError(value: unknown): value is AppError {
|
|
83
|
+
return value instanceof AppError;
|
|
84
|
+
}
|
package/src/errors/index.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from "./app-error";
|
|
1
|
+
export * from "./app-error";
|
|
@@ -1,121 +1,121 @@
|
|
|
1
|
-
import path from "node:path";
|
|
2
|
-
import { readFile, writeFile, mkdir, readdir, stat } from "node:fs/promises";
|
|
3
|
-
import { RepositoryError, ValidationError } from "../errors/app-error";
|
|
4
|
-
|
|
5
|
-
const DEFAULT_IGNORE = new Set([
|
|
6
|
-
"node_modules", ".git", ".next", "out", "dist", "prebuilt", ".turbo", "coverage",
|
|
7
|
-
]);
|
|
8
|
-
|
|
9
|
-
export interface ListOptions {
|
|
10
|
-
recursive?: boolean;
|
|
11
|
-
maxEntries?: number;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export interface SearchOptions {
|
|
15
|
-
/** Restrict to files whose path includes this substring (e.g. ".ts"). */
|
|
16
|
-
extension?: string;
|
|
17
|
-
maxResults?: number;
|
|
18
|
-
maxFileBytes?: number;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export interface SearchMatch {
|
|
22
|
-
readonly file: string;
|
|
23
|
-
readonly line: number;
|
|
24
|
-
readonly text: string;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const TEXT_EXT = /\.(md|markdown|mdx|txt|json|ya?ml|ts|tsx|js|jsx|mjs|cjs|css|scss|html?|svg|toml|env)$/i;
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Root-bounded filesystem service. All paths are resolved relative to `root`
|
|
31
|
-
* and may not escape it — the single safe FS surface for the CLI and MCP.
|
|
32
|
-
*/
|
|
33
|
-
export class FileService {
|
|
34
|
-
constructor(private readonly root: string) {}
|
|
35
|
-
|
|
36
|
-
private resolveInside(relativePath: string): string {
|
|
37
|
-
const abs = path.resolve(this.root, relativePath);
|
|
38
|
-
const rel = path.relative(this.root, abs);
|
|
39
|
-
if (rel.startsWith("..") || path.isAbsolute(rel)) {
|
|
40
|
-
throw new ValidationError(`Path escapes the project root: ${relativePath}`);
|
|
41
|
-
}
|
|
42
|
-
return abs;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
private toRel(abs: string): string {
|
|
46
|
-
return path.relative(this.root, abs).split(path.sep).join("/");
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
async list(relativeDir = ".", options: ListOptions = {}): Promise<string[]> {
|
|
50
|
-
const { recursive = false, maxEntries = 2000 } = options;
|
|
51
|
-
const start = this.resolveInside(relativeDir);
|
|
52
|
-
const out: string[] = [];
|
|
53
|
-
|
|
54
|
-
const walk = async (dir: string): Promise<void> => {
|
|
55
|
-
if (out.length >= maxEntries) return;
|
|
56
|
-
let entries: import("node:fs").Dirent[];
|
|
57
|
-
try {
|
|
58
|
-
entries = await readdir(dir, { withFileTypes: true });
|
|
59
|
-
} catch (cause) {
|
|
60
|
-
throw new RepositoryError(`Cannot read directory: ${this.toRel(dir)}`, { cause });
|
|
61
|
-
}
|
|
62
|
-
for (const entry of entries) {
|
|
63
|
-
if (DEFAULT_IGNORE.has(entry.name)) continue;
|
|
64
|
-
if (out.length >= maxEntries) break;
|
|
65
|
-
const abs = path.join(dir, entry.name);
|
|
66
|
-
out.push(this.toRel(abs) + (entry.isDirectory() ? "/" : ""));
|
|
67
|
-
if (recursive && entry.isDirectory()) await walk(abs);
|
|
68
|
-
}
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
await walk(start);
|
|
72
|
-
return out.sort();
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
async read(relativePath: string, maxBytes = 512_000): Promise<string> {
|
|
76
|
-
const abs = this.resolveInside(relativePath);
|
|
77
|
-
const info = await stat(abs).catch((cause) => {
|
|
78
|
-
throw new RepositoryError(`File not found: ${relativePath}`, { cause });
|
|
79
|
-
});
|
|
80
|
-
if (info.size > maxBytes) {
|
|
81
|
-
throw new ValidationError(`File too large (${info.size} bytes > ${maxBytes}): ${relativePath}`);
|
|
82
|
-
}
|
|
83
|
-
return readFile(abs, "utf8");
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
async write(relativePath: string, content: string): Promise<string> {
|
|
87
|
-
const abs = this.resolveInside(relativePath);
|
|
88
|
-
await mkdir(path.dirname(abs), { recursive: true });
|
|
89
|
-
await writeFile(abs, content, "utf8");
|
|
90
|
-
return this.toRel(abs);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
async search(query: string, options: SearchOptions = {}): Promise<SearchMatch[]> {
|
|
94
|
-
if (!query.trim()) throw new ValidationError("Search query must not be empty.");
|
|
95
|
-
const { extension, maxResults = 200, maxFileBytes = 512_000 } = options;
|
|
96
|
-
const files = await this.list(".", { recursive: true, maxEntries: 5000 });
|
|
97
|
-
const matches: SearchMatch[] = [];
|
|
98
|
-
const needle = query.toLowerCase();
|
|
99
|
-
|
|
100
|
-
for (const file of files) {
|
|
101
|
-
if (file.endsWith("/")) continue;
|
|
102
|
-
if (extension && !file.endsWith(extension)) continue;
|
|
103
|
-
if (!extension && !TEXT_EXT.test(file)) continue;
|
|
104
|
-
if (matches.length >= maxResults) break;
|
|
105
|
-
let content: string;
|
|
106
|
-
try {
|
|
107
|
-
content = await this.read(file, maxFileBytes);
|
|
108
|
-
} catch {
|
|
109
|
-
continue;
|
|
110
|
-
}
|
|
111
|
-
const lines = content.split("\n");
|
|
112
|
-
for (let i = 0; i < lines.length; i += 1) {
|
|
113
|
-
if (lines[i].toLowerCase().includes(needle)) {
|
|
114
|
-
matches.push({ file, line: i + 1, text: lines[i].trim().slice(0, 240) });
|
|
115
|
-
if (matches.length >= maxResults) break;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
return matches;
|
|
120
|
-
}
|
|
121
|
-
}
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { readFile, writeFile, mkdir, readdir, stat } from "node:fs/promises";
|
|
3
|
+
import { RepositoryError, ValidationError } from "../errors/app-error";
|
|
4
|
+
|
|
5
|
+
const DEFAULT_IGNORE = new Set([
|
|
6
|
+
"node_modules", ".git", ".next", "out", "dist", "prebuilt", ".turbo", "coverage",
|
|
7
|
+
]);
|
|
8
|
+
|
|
9
|
+
export interface ListOptions {
|
|
10
|
+
recursive?: boolean;
|
|
11
|
+
maxEntries?: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface SearchOptions {
|
|
15
|
+
/** Restrict to files whose path includes this substring (e.g. ".ts"). */
|
|
16
|
+
extension?: string;
|
|
17
|
+
maxResults?: number;
|
|
18
|
+
maxFileBytes?: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface SearchMatch {
|
|
22
|
+
readonly file: string;
|
|
23
|
+
readonly line: number;
|
|
24
|
+
readonly text: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const TEXT_EXT = /\.(md|markdown|mdx|txt|json|ya?ml|ts|tsx|js|jsx|mjs|cjs|css|scss|html?|svg|toml|env)$/i;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Root-bounded filesystem service. All paths are resolved relative to `root`
|
|
31
|
+
* and may not escape it — the single safe FS surface for the CLI and MCP.
|
|
32
|
+
*/
|
|
33
|
+
export class FileService {
|
|
34
|
+
constructor(private readonly root: string) {}
|
|
35
|
+
|
|
36
|
+
private resolveInside(relativePath: string): string {
|
|
37
|
+
const abs = path.resolve(this.root, relativePath);
|
|
38
|
+
const rel = path.relative(this.root, abs);
|
|
39
|
+
if (rel.startsWith("..") || path.isAbsolute(rel)) {
|
|
40
|
+
throw new ValidationError(`Path escapes the project root: ${relativePath}`);
|
|
41
|
+
}
|
|
42
|
+
return abs;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
private toRel(abs: string): string {
|
|
46
|
+
return path.relative(this.root, abs).split(path.sep).join("/");
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async list(relativeDir = ".", options: ListOptions = {}): Promise<string[]> {
|
|
50
|
+
const { recursive = false, maxEntries = 2000 } = options;
|
|
51
|
+
const start = this.resolveInside(relativeDir);
|
|
52
|
+
const out: string[] = [];
|
|
53
|
+
|
|
54
|
+
const walk = async (dir: string): Promise<void> => {
|
|
55
|
+
if (out.length >= maxEntries) return;
|
|
56
|
+
let entries: import("node:fs").Dirent[];
|
|
57
|
+
try {
|
|
58
|
+
entries = await readdir(dir, { withFileTypes: true });
|
|
59
|
+
} catch (cause) {
|
|
60
|
+
throw new RepositoryError(`Cannot read directory: ${this.toRel(dir)}`, { cause });
|
|
61
|
+
}
|
|
62
|
+
for (const entry of entries) {
|
|
63
|
+
if (DEFAULT_IGNORE.has(entry.name)) continue;
|
|
64
|
+
if (out.length >= maxEntries) break;
|
|
65
|
+
const abs = path.join(dir, entry.name);
|
|
66
|
+
out.push(this.toRel(abs) + (entry.isDirectory() ? "/" : ""));
|
|
67
|
+
if (recursive && entry.isDirectory()) await walk(abs);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
await walk(start);
|
|
72
|
+
return out.sort();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async read(relativePath: string, maxBytes = 512_000): Promise<string> {
|
|
76
|
+
const abs = this.resolveInside(relativePath);
|
|
77
|
+
const info = await stat(abs).catch((cause) => {
|
|
78
|
+
throw new RepositoryError(`File not found: ${relativePath}`, { cause });
|
|
79
|
+
});
|
|
80
|
+
if (info.size > maxBytes) {
|
|
81
|
+
throw new ValidationError(`File too large (${info.size} bytes > ${maxBytes}): ${relativePath}`);
|
|
82
|
+
}
|
|
83
|
+
return readFile(abs, "utf8");
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async write(relativePath: string, content: string): Promise<string> {
|
|
87
|
+
const abs = this.resolveInside(relativePath);
|
|
88
|
+
await mkdir(path.dirname(abs), { recursive: true });
|
|
89
|
+
await writeFile(abs, content, "utf8");
|
|
90
|
+
return this.toRel(abs);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async search(query: string, options: SearchOptions = {}): Promise<SearchMatch[]> {
|
|
94
|
+
if (!query.trim()) throw new ValidationError("Search query must not be empty.");
|
|
95
|
+
const { extension, maxResults = 200, maxFileBytes = 512_000 } = options;
|
|
96
|
+
const files = await this.list(".", { recursive: true, maxEntries: 5000 });
|
|
97
|
+
const matches: SearchMatch[] = [];
|
|
98
|
+
const needle = query.toLowerCase();
|
|
99
|
+
|
|
100
|
+
for (const file of files) {
|
|
101
|
+
if (file.endsWith("/")) continue;
|
|
102
|
+
if (extension && !file.endsWith(extension)) continue;
|
|
103
|
+
if (!extension && !TEXT_EXT.test(file)) continue;
|
|
104
|
+
if (matches.length >= maxResults) break;
|
|
105
|
+
let content: string;
|
|
106
|
+
try {
|
|
107
|
+
content = await this.read(file, maxFileBytes);
|
|
108
|
+
} catch {
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
const lines = content.split("\n");
|
|
112
|
+
for (let i = 0; i < lines.length; i += 1) {
|
|
113
|
+
if (lines[i].toLowerCase().includes(needle)) {
|
|
114
|
+
matches.push({ file, line: i + 1, text: lines[i].trim().slice(0, 240) });
|
|
115
|
+
if (matches.length >= maxResults) break;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return matches;
|
|
120
|
+
}
|
|
121
|
+
}
|
package/src/filesystem/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { FileService } from "./file-service";
|
|
2
|
-
export type { ListOptions, SearchOptions, SearchMatch } from "./file-service";
|
|
1
|
+
export { FileService } from "./file-service";
|
|
2
|
+
export type { ListOptions, SearchOptions, SearchMatch } from "./file-service";
|
package/src/index.ts
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
// @gitpagedocs/tools — shared business-logic core.
|
|
2
|
-
//
|
|
3
|
-
// The single home for domain logic consumed by the frontend, the CLI and the
|
|
4
|
-
// MCP server. Consumers import from here (or subpath modules) and depend on the
|
|
5
|
-
// port interfaces, never on concrete adapters they don't need.
|
|
6
|
-
|
|
7
|
-
// Port contracts (type-only).
|
|
8
|
-
export type * from "./ports";
|
|
9
|
-
|
|
10
|
-
// Phase 4 — cross-cutting core implementations.
|
|
11
|
-
export * from "./errors";
|
|
12
|
-
export * from "./logger";
|
|
13
|
-
export * from "./crypto";
|
|
14
|
-
export * from "./cache";
|
|
15
|
-
export * from "./security";
|
|
16
|
-
export * from "./config";
|
|
17
|
-
export * from "./constants";
|
|
18
|
-
export * from "./platform";
|
|
19
|
-
|
|
20
|
-
// Phase 5 — shared AI system.
|
|
21
|
-
export * from "./ai";
|
|
22
|
-
|
|
23
|
-
// Phase 7 — filesystem + documentation services.
|
|
24
|
-
export * from "./filesystem";
|
|
25
|
-
export * from "./documentation";
|
|
1
|
+
// @gitpagedocs/tools — shared business-logic core.
|
|
2
|
+
//
|
|
3
|
+
// The single home for domain logic consumed by the frontend, the CLI and the
|
|
4
|
+
// MCP server. Consumers import from here (or subpath modules) and depend on the
|
|
5
|
+
// port interfaces, never on concrete adapters they don't need.
|
|
6
|
+
|
|
7
|
+
// Port contracts (type-only).
|
|
8
|
+
export type * from "./ports";
|
|
9
|
+
|
|
10
|
+
// Phase 4 — cross-cutting core implementations.
|
|
11
|
+
export * from "./errors";
|
|
12
|
+
export * from "./logger";
|
|
13
|
+
export * from "./crypto";
|
|
14
|
+
export * from "./cache";
|
|
15
|
+
export * from "./security";
|
|
16
|
+
export * from "./config";
|
|
17
|
+
export * from "./constants";
|
|
18
|
+
export * from "./platform";
|
|
19
|
+
|
|
20
|
+
// Phase 5 — shared AI system.
|
|
21
|
+
export * from "./ai";
|
|
22
|
+
|
|
23
|
+
// Phase 7 — filesystem + documentation services.
|
|
24
|
+
export * from "./filesystem";
|
|
25
|
+
export * from "./documentation";
|
package/src/logger/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from "./logger";
|
|
2
|
-
export { redact, redactFields, REDACTED } from "./redaction";
|
|
1
|
+
export * from "./logger";
|
|
2
|
+
export { redact, redactFields, REDACTED } from "./redaction";
|
package/src/logger/logger.ts
CHANGED
|
@@ -1,73 +1,73 @@
|
|
|
1
|
-
import type { Logger, LogFields, LogLevel, LogSink } from "../ports/logger";
|
|
2
|
-
import { redact, redactFields } from "./redaction";
|
|
3
|
-
|
|
4
|
-
const LEVEL_ORDER: Record<LogLevel, number> = { debug: 10, info: 20, warn: 30, error: 40 };
|
|
5
|
-
|
|
6
|
-
export interface LoggerOptions {
|
|
7
|
-
readonly level?: LogLevel;
|
|
8
|
-
readonly sink?: LogSink;
|
|
9
|
-
readonly baseFields?: LogFields;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
/** Sink that writes redacted records to the console. */
|
|
13
|
-
export class ConsoleSink implements LogSink {
|
|
14
|
-
write(level: LogLevel, message: string, fields: LogFields): void {
|
|
15
|
-
const safeMessage = redact(message) as string;
|
|
16
|
-
const hasFields = Object.keys(fields).length > 0;
|
|
17
|
-
const payload = hasFields ? redactFields(fields as Record<string, unknown>) : undefined;
|
|
18
|
-
const line = `[${level}] ${safeMessage}`;
|
|
19
|
-
if (level === "error") console.error(line, payload ?? "");
|
|
20
|
-
else if (level === "warn") console.warn(line, payload ?? "");
|
|
21
|
-
else console.log(line, payload ?? "");
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/** Discards all records (useful for tests / silent CLI runs). */
|
|
26
|
-
export class NullSink implements LogSink {
|
|
27
|
-
write(): void {
|
|
28
|
-
/* no-op */
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
class StandardLogger implements Logger {
|
|
33
|
-
private readonly level: LogLevel;
|
|
34
|
-
private readonly sink: LogSink;
|
|
35
|
-
private readonly baseFields: LogFields;
|
|
36
|
-
|
|
37
|
-
constructor(options: LoggerOptions) {
|
|
38
|
-
this.level = options.level ?? "info";
|
|
39
|
-
this.sink = options.sink ?? new ConsoleSink();
|
|
40
|
-
this.baseFields = options.baseFields ?? {};
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
private emit(level: LogLevel, message: string, fields?: LogFields): void {
|
|
44
|
-
if (LEVEL_ORDER[level] < LEVEL_ORDER[this.level]) return;
|
|
45
|
-
const merged = fields ? { ...this.baseFields, ...fields } : this.baseFields;
|
|
46
|
-
this.sink.write(level, message, merged);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
debug(message: string, fields?: LogFields): void {
|
|
50
|
-
this.emit("debug", message, fields);
|
|
51
|
-
}
|
|
52
|
-
info(message: string, fields?: LogFields): void {
|
|
53
|
-
this.emit("info", message, fields);
|
|
54
|
-
}
|
|
55
|
-
warn(message: string, fields?: LogFields): void {
|
|
56
|
-
this.emit("warn", message, fields);
|
|
57
|
-
}
|
|
58
|
-
error(message: string, fields?: LogFields): void {
|
|
59
|
-
this.emit("error", message, fields);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
child(fields: LogFields): Logger {
|
|
63
|
-
return new StandardLogger({
|
|
64
|
-
level: this.level,
|
|
65
|
-
sink: this.sink,
|
|
66
|
-
baseFields: { ...this.baseFields, ...fields },
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
export function createLogger(options: LoggerOptions = {}): Logger {
|
|
72
|
-
return new StandardLogger(options);
|
|
73
|
-
}
|
|
1
|
+
import type { Logger, LogFields, LogLevel, LogSink } from "../ports/logger";
|
|
2
|
+
import { redact, redactFields } from "./redaction";
|
|
3
|
+
|
|
4
|
+
const LEVEL_ORDER: Record<LogLevel, number> = { debug: 10, info: 20, warn: 30, error: 40 };
|
|
5
|
+
|
|
6
|
+
export interface LoggerOptions {
|
|
7
|
+
readonly level?: LogLevel;
|
|
8
|
+
readonly sink?: LogSink;
|
|
9
|
+
readonly baseFields?: LogFields;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Sink that writes redacted records to the console. */
|
|
13
|
+
export class ConsoleSink implements LogSink {
|
|
14
|
+
write(level: LogLevel, message: string, fields: LogFields): void {
|
|
15
|
+
const safeMessage = redact(message) as string;
|
|
16
|
+
const hasFields = Object.keys(fields).length > 0;
|
|
17
|
+
const payload = hasFields ? redactFields(fields as Record<string, unknown>) : undefined;
|
|
18
|
+
const line = `[${level}] ${safeMessage}`;
|
|
19
|
+
if (level === "error") console.error(line, payload ?? "");
|
|
20
|
+
else if (level === "warn") console.warn(line, payload ?? "");
|
|
21
|
+
else console.log(line, payload ?? "");
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Discards all records (useful for tests / silent CLI runs). */
|
|
26
|
+
export class NullSink implements LogSink {
|
|
27
|
+
write(): void {
|
|
28
|
+
/* no-op */
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
class StandardLogger implements Logger {
|
|
33
|
+
private readonly level: LogLevel;
|
|
34
|
+
private readonly sink: LogSink;
|
|
35
|
+
private readonly baseFields: LogFields;
|
|
36
|
+
|
|
37
|
+
constructor(options: LoggerOptions) {
|
|
38
|
+
this.level = options.level ?? "info";
|
|
39
|
+
this.sink = options.sink ?? new ConsoleSink();
|
|
40
|
+
this.baseFields = options.baseFields ?? {};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
private emit(level: LogLevel, message: string, fields?: LogFields): void {
|
|
44
|
+
if (LEVEL_ORDER[level] < LEVEL_ORDER[this.level]) return;
|
|
45
|
+
const merged = fields ? { ...this.baseFields, ...fields } : this.baseFields;
|
|
46
|
+
this.sink.write(level, message, merged);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
debug(message: string, fields?: LogFields): void {
|
|
50
|
+
this.emit("debug", message, fields);
|
|
51
|
+
}
|
|
52
|
+
info(message: string, fields?: LogFields): void {
|
|
53
|
+
this.emit("info", message, fields);
|
|
54
|
+
}
|
|
55
|
+
warn(message: string, fields?: LogFields): void {
|
|
56
|
+
this.emit("warn", message, fields);
|
|
57
|
+
}
|
|
58
|
+
error(message: string, fields?: LogFields): void {
|
|
59
|
+
this.emit("error", message, fields);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
child(fields: LogFields): Logger {
|
|
63
|
+
return new StandardLogger({
|
|
64
|
+
level: this.level,
|
|
65
|
+
sink: this.sink,
|
|
66
|
+
baseFields: { ...this.baseFields, ...fields },
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function createLogger(options: LoggerOptions = {}): Logger {
|
|
72
|
+
return new StandardLogger(options);
|
|
73
|
+
}
|