@mscode-pl/feed-lint 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.md +282 -2
- package/dist/cli/colors.d.ts +13 -0
- package/dist/cli/colors.js +23 -0
- package/dist/cli/config.d.ts +11 -0
- package/dist/cli/config.js +59 -0
- package/dist/cli/format.d.ts +23 -0
- package/dist/cli/format.js +129 -0
- package/dist/cli/input.d.ts +3 -0
- package/dist/cli/input.js +27 -0
- package/dist/cli/main.d.ts +2 -0
- package/dist/cli/main.js +151 -0
- package/dist/core/fixes.d.ts +1 -0
- package/dist/core/fixes.js +41 -0
- package/dist/core/gtin.d.ts +5 -0
- package/dist/core/gtin.js +22 -0
- package/dist/core/lint.d.ts +19 -0
- package/dist/core/lint.js +46 -0
- package/dist/core/parse.d.ts +33 -0
- package/dist/core/parse.js +188 -0
- package/dist/core/report.d.ts +28 -0
- package/dist/core/report.js +29 -0
- package/dist/core/urls.d.ts +23 -0
- package/dist/core/urls.js +114 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +5 -0
- package/dist/mcp/server.d.ts +19 -0
- package/dist/mcp/server.js +118 -0
- package/dist/mcp/tools.d.ts +74 -0
- package/dist/mcp/tools.js +124 -0
- package/dist/rules/ceneo.d.ts +7 -0
- package/dist/rules/ceneo.js +222 -0
- package/dist/rules/google.d.ts +5 -0
- package/dist/rules/google.js +278 -0
- package/dist/rules/text.d.ts +5 -0
- package/dist/rules/text.js +21 -0
- package/package.json +63 -6
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { productFields } from '../rules/google.js';
|
|
2
|
+
const DOCS = 'https://github.com/mscode-pl/feed-lint#what-it-checks';
|
|
3
|
+
export const URL_RULES = {
|
|
4
|
+
'url/unreachable': { level: 'error', title: 'Link or image does not load (HTTP 404, 5xx, timeout or network error)', url: DOCS },
|
|
5
|
+
'url/blocked': {
|
|
6
|
+
level: 'warning',
|
|
7
|
+
title: 'Link or image refused the check (HTTP 401, 403 or 429); make sure Googlebot and the Ceneo crawler are allowed',
|
|
8
|
+
url: DOCS,
|
|
9
|
+
},
|
|
10
|
+
'url/redirect': { level: 'warning', title: 'Link or image redirects; use the final URL', url: DOCS },
|
|
11
|
+
};
|
|
12
|
+
const GOOGLE_URL_FIELDS = ['link', 'mobile_link', 'image_link', 'additional_image_link'];
|
|
13
|
+
// FNV-1a; only used to count unique URLs past the limit without keeping them in memory.
|
|
14
|
+
function hash(text) {
|
|
15
|
+
let h = 0x811c9dc5;
|
|
16
|
+
for (let i = 0; i < text.length; i++)
|
|
17
|
+
h = Math.imul(h ^ text.charCodeAt(i), 0x01000193);
|
|
18
|
+
return h >>> 0;
|
|
19
|
+
}
|
|
20
|
+
export class UrlCollector {
|
|
21
|
+
limit;
|
|
22
|
+
urls = new Map();
|
|
23
|
+
overflow = new Set();
|
|
24
|
+
constructor(limit = 200) {
|
|
25
|
+
this.limit = limit;
|
|
26
|
+
}
|
|
27
|
+
add(record, format, atom = false) {
|
|
28
|
+
if (format === 'google') {
|
|
29
|
+
const fields = productFields(record, atom);
|
|
30
|
+
const id = fields.get('id')?.[0]?.text.trim() || undefined;
|
|
31
|
+
for (const name of GOOGLE_URL_FIELDS) {
|
|
32
|
+
for (const c of fields.get(name) ?? [])
|
|
33
|
+
this.put(c.text.trim() || c.attrs.href?.trim(), { line: c.line, id, detail: name });
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
const id = record.attrs.id?.trim() || undefined;
|
|
38
|
+
this.put(record.attrs.url?.trim(), { line: record.line, id, detail: 'url' });
|
|
39
|
+
for (const img of record.children.find((c) => c.name === 'imgs')?.children ?? []) {
|
|
40
|
+
this.put(img.attrs.url?.trim(), { line: img.line, id, detail: `<${img.name}>` });
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
put(url, sample) {
|
|
45
|
+
if (!url || !/^https?:\/\//i.test(url) || this.urls.has(url))
|
|
46
|
+
return;
|
|
47
|
+
if (this.limit > 0 && this.urls.size >= this.limit)
|
|
48
|
+
this.overflow.add(hash(url));
|
|
49
|
+
else
|
|
50
|
+
this.urls.set(url, sample);
|
|
51
|
+
}
|
|
52
|
+
async check(report, options = {}) {
|
|
53
|
+
const entries = [...this.urls];
|
|
54
|
+
let next = 0;
|
|
55
|
+
const worker = async () => {
|
|
56
|
+
while (next < entries.length) {
|
|
57
|
+
const [url, sample] = entries[next++];
|
|
58
|
+
const problem = await probe(url, options);
|
|
59
|
+
if (problem)
|
|
60
|
+
report(problem.rule, { ...sample, detail: `${sample.detail}: ${problem.detail} ${url}` });
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
await Promise.all(Array.from({ length: Math.min(options.concurrency ?? 8, entries.length) }, worker));
|
|
64
|
+
return { total: this.urls.size + this.overflow.size, checked: entries.length };
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
68
|
+
async function probe(url, options) {
|
|
69
|
+
const doFetch = options.fetch ?? fetch;
|
|
70
|
+
const request = async (method) => {
|
|
71
|
+
const response = await doFetch(url, {
|
|
72
|
+
method,
|
|
73
|
+
redirect: 'manual',
|
|
74
|
+
signal: AbortSignal.timeout(options.timeoutMs ?? 10_000),
|
|
75
|
+
headers: options.userAgent ? { 'user-agent': options.userAgent } : {},
|
|
76
|
+
});
|
|
77
|
+
await response.body?.cancel();
|
|
78
|
+
return response;
|
|
79
|
+
};
|
|
80
|
+
try {
|
|
81
|
+
let response = await request('HEAD');
|
|
82
|
+
if (response.status === 429) {
|
|
83
|
+
const wait = Number(response.headers.get('retry-after'));
|
|
84
|
+
await sleep(Math.min(Number.isFinite(wait) && wait > 0 ? wait * 1000 : 1000, 5000));
|
|
85
|
+
response = await request('HEAD');
|
|
86
|
+
}
|
|
87
|
+
// Some servers and CDNs refuse HEAD; the browser would use GET.
|
|
88
|
+
if ([403, 405, 501].includes(response.status))
|
|
89
|
+
response = await request('GET');
|
|
90
|
+
const status = response.status;
|
|
91
|
+
if (status >= 300 && status < 400) {
|
|
92
|
+
let target = '';
|
|
93
|
+
try {
|
|
94
|
+
const location = response.headers.get('location');
|
|
95
|
+
if (location)
|
|
96
|
+
target = ` to ${new URL(location, url).href}`;
|
|
97
|
+
}
|
|
98
|
+
catch {
|
|
99
|
+
target = ' to an invalid Location';
|
|
100
|
+
}
|
|
101
|
+
return { rule: 'url/redirect', detail: `HTTP ${status}${target}` };
|
|
102
|
+
}
|
|
103
|
+
if (status === 401 || status === 403 || status === 429)
|
|
104
|
+
return { rule: 'url/blocked', detail: `HTTP ${status}` };
|
|
105
|
+
if (status >= 400)
|
|
106
|
+
return { rule: 'url/unreachable', detail: `HTTP ${status}` };
|
|
107
|
+
return undefined;
|
|
108
|
+
}
|
|
109
|
+
catch (error) {
|
|
110
|
+
const e = error;
|
|
111
|
+
const detail = e.name === 'TimeoutError' ? 'timeout' : (e.cause?.code ?? e.message);
|
|
112
|
+
return { rule: 'url/unreachable', detail };
|
|
113
|
+
}
|
|
114
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { lintFeed, RULES } from './core/lint.js';
|
|
2
|
+
export type { LintOptions, LintResult } from './core/lint.js';
|
|
3
|
+
export { parseFeed } from './core/parse.js';
|
|
4
|
+
export type { FeedFormat, FeedInfo, XmlNode } from './core/parse.js';
|
|
5
|
+
export type { Level, RuleInfo, RuleResult, Sample } from './core/report.js';
|
|
6
|
+
export type { UrlCheckOptions, UrlStats } from './core/urls.js';
|
|
7
|
+
export { checkGtin } from './core/gtin.js';
|
|
8
|
+
export { fixFor } from './core/fixes.js';
|
|
9
|
+
export { formatText, formatMarkdown, formatGithub } from './cli/format.js';
|
|
10
|
+
export type { FeedResult, Format } from './cli/format.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export declare const PROTOCOL_VERSIONS: string[];
|
|
2
|
+
type Id = string | number | null;
|
|
3
|
+
interface JsonRpcError {
|
|
4
|
+
code: number;
|
|
5
|
+
message: string;
|
|
6
|
+
}
|
|
7
|
+
export type Response = {
|
|
8
|
+
jsonrpc: '2.0';
|
|
9
|
+
id: Id;
|
|
10
|
+
result: unknown;
|
|
11
|
+
} | {
|
|
12
|
+
jsonrpc: '2.0';
|
|
13
|
+
id: Id;
|
|
14
|
+
error: JsonRpcError;
|
|
15
|
+
};
|
|
16
|
+
export declare function handle(message: unknown): Promise<Response | Response[] | undefined>;
|
|
17
|
+
export declare function handleLine(line: string): Promise<Response | Response[] | undefined>;
|
|
18
|
+
export declare function serve(input?: NodeJS.ReadableStream, output?: Pick<NodeJS.WritableStream, 'write'>): Promise<void>;
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import { createInterface } from 'node:readline';
|
|
3
|
+
import { callTool, ToolInputError, TOOLS } from './tools.js';
|
|
4
|
+
// Newest first. The tools use nothing that differs between these versions.
|
|
5
|
+
export const PROTOCOL_VERSIONS = ['2025-11-25', '2025-06-18', '2025-03-26', '2024-11-05'];
|
|
6
|
+
const PARSE_ERROR = -32700;
|
|
7
|
+
const INVALID_REQUEST = -32600;
|
|
8
|
+
const METHOD_NOT_FOUND = -32601;
|
|
9
|
+
const INVALID_PARAMS = -32602;
|
|
10
|
+
class RpcError extends Error {
|
|
11
|
+
code;
|
|
12
|
+
constructor(code, message) {
|
|
13
|
+
super(message);
|
|
14
|
+
this.code = code;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
function packageVersion() {
|
|
18
|
+
const pkg = JSON.parse(readFileSync(new URL('../../package.json', import.meta.url), 'utf8'));
|
|
19
|
+
return pkg.version;
|
|
20
|
+
}
|
|
21
|
+
const isObject = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
22
|
+
async function dispatch(method, params) {
|
|
23
|
+
switch (method) {
|
|
24
|
+
case 'initialize': {
|
|
25
|
+
const requested = params.protocolVersion;
|
|
26
|
+
return {
|
|
27
|
+
protocolVersion: typeof requested === 'string' && PROTOCOL_VERSIONS.includes(requested)
|
|
28
|
+
? requested
|
|
29
|
+
: PROTOCOL_VERSIONS[0],
|
|
30
|
+
capabilities: { tools: { listChanged: false } },
|
|
31
|
+
serverInfo: { name: 'feed-lint', title: 'feed-lint', version: packageVersion() },
|
|
32
|
+
instructions: 'Checks Google Merchant and Ceneo XML product feeds. Call lint_feed with a file path or URL, fix errors first (they get products rejected), then warnings. Use explain_rule for the fix and the source in the specification.',
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
case 'ping':
|
|
36
|
+
return {};
|
|
37
|
+
case 'tools/list':
|
|
38
|
+
return { tools: TOOLS };
|
|
39
|
+
case 'tools/call': {
|
|
40
|
+
if (typeof params.name !== 'string')
|
|
41
|
+
throw new RpcError(INVALID_PARAMS, '"name" must be a string.');
|
|
42
|
+
const args = params.arguments ?? {};
|
|
43
|
+
if (!isObject(args))
|
|
44
|
+
throw new RpcError(INVALID_PARAMS, '"arguments" must be an object.');
|
|
45
|
+
try {
|
|
46
|
+
return await callTool(params.name, args);
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
if (error instanceof ToolInputError)
|
|
50
|
+
throw new RpcError(INVALID_PARAMS, error.message);
|
|
51
|
+
throw error;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
default:
|
|
55
|
+
throw new RpcError(METHOD_NOT_FOUND, `Method not found: ${method}`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
async function handleOne(message) {
|
|
59
|
+
if (!isObject(message) || message.jsonrpc !== '2.0' || typeof message.method !== 'string') {
|
|
60
|
+
const id = isObject(message) && (typeof message.id === 'string' || typeof message.id === 'number') ? message.id : null;
|
|
61
|
+
// Responses from the client (to requests this server never sends) are ignored.
|
|
62
|
+
if (isObject(message) && ('result' in message || 'error' in message))
|
|
63
|
+
return undefined;
|
|
64
|
+
return { jsonrpc: '2.0', id, error: { code: INVALID_REQUEST, message: 'Invalid request.' } };
|
|
65
|
+
}
|
|
66
|
+
const isNotification = !('id' in message);
|
|
67
|
+
const id = (message.id ?? null);
|
|
68
|
+
const params = isObject(message.params) ? message.params : {};
|
|
69
|
+
try {
|
|
70
|
+
const result = await dispatch(message.method, params);
|
|
71
|
+
return isNotification ? undefined : { jsonrpc: '2.0', id, result };
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
if (isNotification)
|
|
75
|
+
return undefined;
|
|
76
|
+
const code = error instanceof RpcError ? error.code : -32603;
|
|
77
|
+
const text = error instanceof Error ? error.message : String(error);
|
|
78
|
+
return { jsonrpc: '2.0', id, error: { code, message: text } };
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
export async function handle(message) {
|
|
82
|
+
if (!Array.isArray(message))
|
|
83
|
+
return handleOne(message);
|
|
84
|
+
if (!message.length) {
|
|
85
|
+
return { jsonrpc: '2.0', id: null, error: { code: INVALID_REQUEST, message: 'Empty batch.' } };
|
|
86
|
+
}
|
|
87
|
+
const responses = (await Promise.all(message.map(handleOne))).filter((r) => r !== undefined);
|
|
88
|
+
return responses.length ? responses : undefined;
|
|
89
|
+
}
|
|
90
|
+
export async function handleLine(line) {
|
|
91
|
+
if (!line.trim())
|
|
92
|
+
return undefined;
|
|
93
|
+
let message;
|
|
94
|
+
try {
|
|
95
|
+
message = JSON.parse(line);
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
return { jsonrpc: '2.0', id: null, error: { code: PARSE_ERROR, message: 'Parse error.' } };
|
|
99
|
+
}
|
|
100
|
+
return handle(message);
|
|
101
|
+
}
|
|
102
|
+
export function serve(input = process.stdin, output = process.stdout) {
|
|
103
|
+
const pending = new Set();
|
|
104
|
+
const lines = createInterface({ input, crlfDelay: Infinity });
|
|
105
|
+
lines.on('line', (line) => {
|
|
106
|
+
const task = handleLine(line).then((response) => {
|
|
107
|
+
if (response)
|
|
108
|
+
output.write(`${JSON.stringify(response)}\n`);
|
|
109
|
+
});
|
|
110
|
+
pending.add(task);
|
|
111
|
+
void task.finally(() => pending.delete(task));
|
|
112
|
+
});
|
|
113
|
+
return new Promise((resolve) => {
|
|
114
|
+
lines.on('close', () => {
|
|
115
|
+
void Promise.allSettled([...pending]).then(() => resolve());
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
export interface ToolResult {
|
|
2
|
+
content: {
|
|
3
|
+
type: 'text';
|
|
4
|
+
text: string;
|
|
5
|
+
}[];
|
|
6
|
+
isError?: boolean;
|
|
7
|
+
}
|
|
8
|
+
export declare class ToolInputError extends Error {
|
|
9
|
+
}
|
|
10
|
+
export declare const TOOLS: ({
|
|
11
|
+
name: string;
|
|
12
|
+
title: string;
|
|
13
|
+
description: string;
|
|
14
|
+
inputSchema: {
|
|
15
|
+
type: string;
|
|
16
|
+
properties: {
|
|
17
|
+
path: {
|
|
18
|
+
type: string;
|
|
19
|
+
description: string;
|
|
20
|
+
};
|
|
21
|
+
url: {
|
|
22
|
+
type: string;
|
|
23
|
+
description: string;
|
|
24
|
+
};
|
|
25
|
+
xml: {
|
|
26
|
+
type: string;
|
|
27
|
+
description: string;
|
|
28
|
+
};
|
|
29
|
+
ignore: {
|
|
30
|
+
type: string;
|
|
31
|
+
items: {
|
|
32
|
+
type: string;
|
|
33
|
+
};
|
|
34
|
+
description: string;
|
|
35
|
+
};
|
|
36
|
+
check_urls: {
|
|
37
|
+
type: string;
|
|
38
|
+
description: string;
|
|
39
|
+
};
|
|
40
|
+
rule?: undefined;
|
|
41
|
+
};
|
|
42
|
+
additionalProperties: boolean;
|
|
43
|
+
required?: undefined;
|
|
44
|
+
};
|
|
45
|
+
annotations: {
|
|
46
|
+
readOnlyHint: boolean;
|
|
47
|
+
openWorldHint: boolean;
|
|
48
|
+
};
|
|
49
|
+
} | {
|
|
50
|
+
name: string;
|
|
51
|
+
title: string;
|
|
52
|
+
description: string;
|
|
53
|
+
inputSchema: {
|
|
54
|
+
type: string;
|
|
55
|
+
properties: {
|
|
56
|
+
rule: {
|
|
57
|
+
type: string;
|
|
58
|
+
description: string;
|
|
59
|
+
};
|
|
60
|
+
path?: undefined;
|
|
61
|
+
url?: undefined;
|
|
62
|
+
xml?: undefined;
|
|
63
|
+
ignore?: undefined;
|
|
64
|
+
check_urls?: undefined;
|
|
65
|
+
};
|
|
66
|
+
required: string[];
|
|
67
|
+
additionalProperties: boolean;
|
|
68
|
+
};
|
|
69
|
+
annotations: {
|
|
70
|
+
readOnlyHint: boolean;
|
|
71
|
+
openWorldHint: boolean;
|
|
72
|
+
};
|
|
73
|
+
})[];
|
|
74
|
+
export declare function callTool(name: string, args: Record<string, unknown>): Promise<ToolResult>;
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { painter } from '../cli/colors.js';
|
|
2
|
+
import { loadConfig } from '../cli/config.js';
|
|
3
|
+
import { formatText } from '../cli/format.js';
|
|
4
|
+
import { openInput, USER_AGENT } from '../cli/input.js';
|
|
5
|
+
import { fixFor } from '../core/fixes.js';
|
|
6
|
+
import { lintFeed, RULES } from '../core/lint.js';
|
|
7
|
+
export class ToolInputError extends Error {
|
|
8
|
+
}
|
|
9
|
+
export const TOOLS = [
|
|
10
|
+
{
|
|
11
|
+
name: 'lint_feed',
|
|
12
|
+
title: 'Lint a product feed',
|
|
13
|
+
description: 'Check a Google Merchant (RSS or Atom) or Ceneo XML product feed and list problems grouped by rule, with counts, line numbers and product ids: missing fields, wrong prices, invalid GTINs, duplicate ids, bad categories and more. Pass a file path, an http(s) URL or the XML itself. With none of them, lints the feeds from feed-lint.config.json. Follow up with explain_rule for a fix.',
|
|
14
|
+
inputSchema: {
|
|
15
|
+
type: 'object',
|
|
16
|
+
properties: {
|
|
17
|
+
path: { type: 'string', description: 'Feed file relative to the working directory, e.g. "public/feeds/google.xml". Use one of path, url or xml.' },
|
|
18
|
+
url: { type: 'string', description: 'Feed URL, e.g. "https://shop.example/feed/google.xml".' },
|
|
19
|
+
xml: { type: 'string', description: 'Feed XML as text, for small feeds or a single product.' },
|
|
20
|
+
ignore: { type: 'array', items: { type: 'string' }, description: 'Rules to skip, e.g. ["google/identifiers"].' },
|
|
21
|
+
check_urls: {
|
|
22
|
+
type: 'boolean',
|
|
23
|
+
description: 'Also request product links and images (up to 200 unique URLs). Slow; use when asked to check links.',
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
additionalProperties: false,
|
|
27
|
+
},
|
|
28
|
+
annotations: { readOnlyHint: true, openWorldHint: true },
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
name: 'explain_rule',
|
|
32
|
+
title: 'Explain a feed rule',
|
|
33
|
+
description: 'Explain one rule reported by lint_feed: what it checks, how severe it is, how to fix it and where the specification says so.',
|
|
34
|
+
inputSchema: {
|
|
35
|
+
type: 'object',
|
|
36
|
+
properties: {
|
|
37
|
+
rule: { type: 'string', description: 'Rule id from lint_feed, e.g. "google/gtin-checksum" or "ceneo/avail-value".' },
|
|
38
|
+
},
|
|
39
|
+
required: ['rule'],
|
|
40
|
+
additionalProperties: false,
|
|
41
|
+
},
|
|
42
|
+
annotations: { readOnlyHint: true, openWorldHint: false },
|
|
43
|
+
},
|
|
44
|
+
];
|
|
45
|
+
const text = (value, extra = {}) => ({
|
|
46
|
+
content: [{ type: 'text', text: value }],
|
|
47
|
+
...extra,
|
|
48
|
+
});
|
|
49
|
+
function optionalString(args, key) {
|
|
50
|
+
const value = args[key];
|
|
51
|
+
if (value === undefined)
|
|
52
|
+
return undefined;
|
|
53
|
+
if (typeof value !== 'string' || !value.trim())
|
|
54
|
+
throw new ToolInputError(`"${key}" must be a non-empty string.`);
|
|
55
|
+
return value;
|
|
56
|
+
}
|
|
57
|
+
function optionalList(args, key) {
|
|
58
|
+
const value = args[key];
|
|
59
|
+
if (value === undefined)
|
|
60
|
+
return undefined;
|
|
61
|
+
if (!Array.isArray(value) || value.some((v) => typeof v !== 'string')) {
|
|
62
|
+
throw new ToolInputError(`"${key}" must be an array of strings.`);
|
|
63
|
+
}
|
|
64
|
+
return value;
|
|
65
|
+
}
|
|
66
|
+
async function lintTool(args) {
|
|
67
|
+
const path = optionalString(args, 'path');
|
|
68
|
+
const url = optionalString(args, 'url');
|
|
69
|
+
const xml = optionalString(args, 'xml');
|
|
70
|
+
if ([path, url, xml].filter(Boolean).length > 1)
|
|
71
|
+
throw new ToolInputError('Pass only one of "path", "url" or "xml".');
|
|
72
|
+
if (url && !/^https?:\/\//i.test(url))
|
|
73
|
+
throw new ToolInputError('"url" must start with http:// or https://.');
|
|
74
|
+
if (args.check_urls !== undefined && typeof args.check_urls !== 'boolean')
|
|
75
|
+
throw new ToolInputError('"check_urls" must be true or false.');
|
|
76
|
+
const config = await loadConfig();
|
|
77
|
+
const ignore = optionalList(args, 'ignore') ?? config.ignore;
|
|
78
|
+
const unknown = (ignore ?? []).filter((r) => !RULES[r]);
|
|
79
|
+
if (unknown.length)
|
|
80
|
+
throw new ToolInputError(`Unknown rule ${unknown.join(', ')}.`);
|
|
81
|
+
const options = {
|
|
82
|
+
ignore,
|
|
83
|
+
checkUrls: (args.check_urls ?? config.checkUrls) ? { limit: config.urlLimit, userAgent: USER_AGENT } : false,
|
|
84
|
+
};
|
|
85
|
+
const results = [];
|
|
86
|
+
if (xml) {
|
|
87
|
+
results.push({ feed: '<xml>', ...(await lintFeed([xml], options)) });
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
const feeds = path ? [path] : url ? [url] : (config.feeds ?? []);
|
|
91
|
+
if (!feeds.length)
|
|
92
|
+
throw new ToolInputError('Pass "path", "url" or "xml". There is no feed-lint.config.json with "feeds" to fall back on.');
|
|
93
|
+
for (const feed of feeds)
|
|
94
|
+
results.push({ feed, ...(await lintFeed(await openInput(feed), options)) });
|
|
95
|
+
}
|
|
96
|
+
return text(formatText(results, { verbose: true, paint: painter(false) }));
|
|
97
|
+
}
|
|
98
|
+
function explainRule(args) {
|
|
99
|
+
const rule = optionalString(args, 'rule');
|
|
100
|
+
if (!rule)
|
|
101
|
+
throw new ToolInputError('"rule" is required.');
|
|
102
|
+
const info = RULES[rule];
|
|
103
|
+
if (!info) {
|
|
104
|
+
const words = (rule.split('/').pop() ?? '').split('-').filter((w) => w.length > 2);
|
|
105
|
+
const similar = Object.keys(RULES)
|
|
106
|
+
.filter((r) => words.some((w) => (r.split('/').pop() ?? '').includes(w)))
|
|
107
|
+
.slice(0, 8);
|
|
108
|
+
throw new ToolInputError(`Unknown rule "${rule}".${similar.length ? ` Similar: ${similar.join(', ')}.` : ''}`);
|
|
109
|
+
}
|
|
110
|
+
const severity = info.level === 'error' ? 'Error: the product or the whole feed is rejected.' : 'Warning: the product is accepted but may be limited or disapproved later.';
|
|
111
|
+
return text([`${info.title} (${rule})`, severity, '', `Fix: ${fixFor(rule)}`, '', `Source: ${info.url}`].join('\n'));
|
|
112
|
+
}
|
|
113
|
+
export async function callTool(name, args) {
|
|
114
|
+
try {
|
|
115
|
+
if (name === 'lint_feed')
|
|
116
|
+
return await lintTool(args);
|
|
117
|
+
if (name === 'explain_rule')
|
|
118
|
+
return explainRule(args);
|
|
119
|
+
}
|
|
120
|
+
catch (error) {
|
|
121
|
+
return text(error instanceof Error ? error.message : String(error), { isError: true });
|
|
122
|
+
}
|
|
123
|
+
throw new ToolInputError(`Unknown tool "${name}".`);
|
|
124
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { FeedInfo, ParseResult, XmlNode } from '../core/parse.js';
|
|
2
|
+
import type { Report, RuleInfo } from '../core/report.js';
|
|
3
|
+
export declare const CENEO_RULES: Record<string, RuleInfo>;
|
|
4
|
+
export declare function ceneoFileChecks(info: FeedInfo, report: Report): void;
|
|
5
|
+
export declare function ceneoChecker(report: Report, parsed: ParseResult): ((offer: XmlNode) => void) & {
|
|
6
|
+
end: () => void;
|
|
7
|
+
};
|