@mscode-pl/email-lint 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +274 -3
- package/data/LICENSE-caniemail +21 -0
- package/data/caniemail.json +1 -0
- 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 +56 -0
- package/dist/cli/files.d.ts +7 -0
- package/dist/cli/files.js +44 -0
- package/dist/cli/format.d.ts +23 -0
- package/dist/cli/format.js +204 -0
- package/dist/cli/interactive.d.ts +1 -0
- package/dist/cli/interactive.js +154 -0
- package/dist/cli/main.d.ts +2 -0
- package/dist/cli/main.js +142 -0
- package/dist/core/baseline.d.ts +1 -0
- package/dist/core/baseline.js +24 -0
- package/dist/core/clients.d.ts +22 -0
- package/dist/core/clients.js +65 -0
- package/dist/core/data.d.ts +20 -0
- package/dist/core/data.js +6 -0
- package/dist/core/lint.d.ts +27 -0
- package/dist/core/lint.js +217 -0
- package/dist/core/rules.d.ts +23 -0
- package/dist/core/rules.js +164 -0
- package/dist/core/selectors.d.ts +15 -0
- package/dist/core/selectors.js +47 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +6 -0
- package/dist/mcp/fixes.d.ts +16 -0
- package/dist/mcp/fixes.js +48 -0
- package/dist/mcp/server.d.ts +19 -0
- package/dist/mcp/server.js +118 -0
- package/dist/mcp/tools.d.ts +83 -0
- package/dist/mcp/tools.js +161 -0
- package/package.json +70 -6
- package/skills/email-lint/SKILL.md +68 -0
- package/skills/email-lint/references/fixes.md +58 -0
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
const escape = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
2
|
+
const props = (...names) => (d) => names.includes(d.prop);
|
|
3
|
+
const propMatch = (re) => (d) => re.test(d.prop);
|
|
4
|
+
const valueMatch = (re) => (d) => re.test(d.value);
|
|
5
|
+
const declValue = (prop, re) => (d) => d.prop === prop && re.test(d.value);
|
|
6
|
+
const media = (re) => (a) => a.name === 'media' && re.test(a.params);
|
|
7
|
+
const tags = (...names) => (e) => names.includes(e.tag);
|
|
8
|
+
const hasAttr = (...names) => (e) => names.some((n) => n in e.attrs);
|
|
9
|
+
const inputType = (tag, type) => (e) => e.tag === tag && (e.attrs.type ?? '').toLowerCase() === type;
|
|
10
|
+
const hrefStarts = (prefix) => (e) => e.tag === 'a' && (e.attrs.href ?? '').trim().toLowerCase().startsWith(prefix);
|
|
11
|
+
const keyword = (words) => new RegExp(`(?<![\\w-])(${words})(?![\\w-])`, 'i');
|
|
12
|
+
const fn = (names) => new RegExp(`(?<![\\w-])(${names})\\(`, 'i');
|
|
13
|
+
const manual = {
|
|
14
|
+
'css-at-font-face': { atRule: (a) => a.name === 'font-face' },
|
|
15
|
+
'css-at-import': { atRule: (a) => a.name === 'import' },
|
|
16
|
+
'css-at-keyframes': { atRule: (a) => /^(-webkit-)?keyframes$/.test(a.name) },
|
|
17
|
+
'css-at-media': { atRule: (a) => a.name === 'media' },
|
|
18
|
+
'css-at-supports': { atRule: (a) => a.name === 'supports' },
|
|
19
|
+
'css-at-media-device-pixel-ratio': { atRule: media(/device-pixel-ratio/i) },
|
|
20
|
+
'css-at-media-hover': { atRule: media(/\((any-)?hover\b/i) },
|
|
21
|
+
'css-at-media-orientation': { atRule: media(/\borientation\b/i) },
|
|
22
|
+
'css-at-media-prefers-color-scheme': { atRule: media(/prefers-color-scheme/i) },
|
|
23
|
+
'css-at-media-prefers-reduced-motion': { atRule: media(/prefers-reduced-motion/i) },
|
|
24
|
+
'css-block-inline-size': { decl: props('block-size', 'inline-size') },
|
|
25
|
+
'css-inline-size': { decl: props('inline-size') },
|
|
26
|
+
'css-border-inline-block': { decl: props('border-inline', 'border-block') },
|
|
27
|
+
'css-border-inline-block-longhand': {
|
|
28
|
+
decl: propMatch(/^border-(inline|block)-(width|style|color)$/),
|
|
29
|
+
},
|
|
30
|
+
'css-border-inline-block-individual': {
|
|
31
|
+
decl: propMatch(/^border-(inline|block)-(start|end)(-(width|style|color))?$/),
|
|
32
|
+
},
|
|
33
|
+
'css-border-radius-logical': { decl: propMatch(/^border-(start|end)-(start|end)-radius$/) },
|
|
34
|
+
'css-color-scheme': { decl: props('color-scheme') },
|
|
35
|
+
'css-column-layout-properties': {
|
|
36
|
+
decl: propMatch(/^(columns|column-(width|span|fill|rule(-width|-style|-color)?))$/),
|
|
37
|
+
},
|
|
38
|
+
'css-display-flex': { decl: declValue('display', keyword('flex|inline-flex')) },
|
|
39
|
+
'css-display-grid': { decl: declValue('display', keyword('grid|inline-grid')) },
|
|
40
|
+
'css-display-none': { decl: declValue('display', keyword('none')) },
|
|
41
|
+
'css-flex-direction': { decl: props('flex-direction') },
|
|
42
|
+
'css-flex-wrap': { decl: declValue('flex-wrap', keyword('wrap|wrap-reverse')) },
|
|
43
|
+
'css-font': { decl: props('font') },
|
|
44
|
+
'css-gap': { decl: props('gap', 'column-gap', 'row-gap', 'grid-gap') },
|
|
45
|
+
'css-grid-template': { decl: propMatch(/^grid-template/) },
|
|
46
|
+
'css-important': { decl: (d) => d.important },
|
|
47
|
+
'css-intrinsic-size': { decl: valueMatch(keyword('fit-content|min-content|max-content')) },
|
|
48
|
+
'css-left-right-top-bottom': { decl: props('left', 'right', 'top', 'bottom') },
|
|
49
|
+
'css-margin-block-start-end': { decl: props('margin-block-start', 'margin-block-end') },
|
|
50
|
+
'css-margin-inline-block': { decl: props('margin-inline', 'margin-block') },
|
|
51
|
+
'css-margin-inline-start-end': { decl: props('margin-inline-start', 'margin-inline-end') },
|
|
52
|
+
'css-padding-block-start-end': { decl: props('padding-block-start', 'padding-block-end') },
|
|
53
|
+
'css-padding-inline-block': { decl: props('padding-inline', 'padding-block') },
|
|
54
|
+
'css-padding-inline-start-end': {
|
|
55
|
+
decl: props('padding-inline-start', 'padding-inline-end'),
|
|
56
|
+
},
|
|
57
|
+
'css-modern-color': { decl: valueMatch(fn('lch|oklch|lab|oklab')) },
|
|
58
|
+
'css-sytem-ui': {
|
|
59
|
+
decl: valueMatch(keyword('system-ui|ui-serif|ui-sans-serif|ui-rounded|ui-monospace')),
|
|
60
|
+
},
|
|
61
|
+
'css-unit-calc': { decl: valueMatch(fn('calc')) },
|
|
62
|
+
'css-variables': { decl: (d) => d.prop.startsWith('--') || fn('var').test(d.value) },
|
|
63
|
+
'css-unit-initial': { decl: valueMatch(keyword('initial')) },
|
|
64
|
+
'css-selector-grouping': { selector: (s) => s.grouping },
|
|
65
|
+
'css-selector-child': { selector: (s) => s.child },
|
|
66
|
+
'css-selector-adjacent-sibling': { selector: (s) => s.adjacent },
|
|
67
|
+
'css-selector-general-sibling': { selector: (s) => s.generalSibling },
|
|
68
|
+
'css-selector-descendant': { selector: (s) => s.descendant },
|
|
69
|
+
'css-selector-type': { selector: (s) => s.type },
|
|
70
|
+
'css-selector-universal': { selector: (s) => s.universal },
|
|
71
|
+
'css-selector-class': { selector: (s) => s.cls },
|
|
72
|
+
'css-selector-id': { selector: (s) => s.id },
|
|
73
|
+
'css-selector-attribute': { selector: (s) => s.attribute },
|
|
74
|
+
'css-selector-chaining': { selector: (s) => s.chaining },
|
|
75
|
+
'html-address': { element: tags('address') },
|
|
76
|
+
'html-base': { element: tags('base') },
|
|
77
|
+
'html-h1-h6': { element: tags('h1', 'h2', 'h3', 'h4', 'h5', 'h6') },
|
|
78
|
+
'html-lists': { element: tags('ul', 'ol', 'dl') },
|
|
79
|
+
'html-svg': { element: tags('svg') },
|
|
80
|
+
'html-semantics': {
|
|
81
|
+
element: tags('article', 'aside', 'details', 'figcaption', 'figure', 'footer', 'header', 'main', 'mark', 'nav', 'section', 'summary', 'time'),
|
|
82
|
+
},
|
|
83
|
+
'html-image-maps': { element: (e) => e.tag === 'map' || 'usemap' in e.attrs },
|
|
84
|
+
'html-anchor-links': { element: hrefStarts('#') },
|
|
85
|
+
'html-mailto-links': { element: hrefStarts('mailto:') },
|
|
86
|
+
'html-meta-color-scheme': {
|
|
87
|
+
element: (e) => e.tag === 'meta' && /^(color-scheme|supported-color-schemes)$/i.test(e.attrs.name ?? ''),
|
|
88
|
+
},
|
|
89
|
+
'html-srcset': { element: hasAttr('srcset', 'sizes') },
|
|
90
|
+
'html-command-attribute': { element: hasAttr('command', 'commandfor') },
|
|
91
|
+
'html-button-reset': { element: inputType('button', 'reset') },
|
|
92
|
+
'html-button-submit': { element: inputType('button', 'submit') },
|
|
93
|
+
'html-input-checkbox': { element: inputType('input', 'checkbox') },
|
|
94
|
+
'html-input-hidden': { element: inputType('input', 'hidden') },
|
|
95
|
+
'html-input-radio': { element: inputType('input', 'radio') },
|
|
96
|
+
'html-input-reset': { element: inputType('input', 'reset') },
|
|
97
|
+
'html-input-submit': { element: inputType('input', 'submit') },
|
|
98
|
+
'html-input-text': { element: inputType('input', 'text') },
|
|
99
|
+
amp: { element: (e) => e.tag === 'html' && ('⚡4email' in e.attrs || 'amp4email' in e.attrs) },
|
|
100
|
+
};
|
|
101
|
+
const UNIT_PREFIX = String.raw `(?<![\w#.-])-?(?:\d+\.?\d*|\.\d+)`;
|
|
102
|
+
function autoRule(feature) {
|
|
103
|
+
const title = feature.title.trim();
|
|
104
|
+
if (feature.category === 'css') {
|
|
105
|
+
let m;
|
|
106
|
+
if ((m = title.match(/^([a-z-]+)(?: property)?$/))) {
|
|
107
|
+
if (m[1] === 'inert')
|
|
108
|
+
return { element: hasAttr('inert') };
|
|
109
|
+
return { decl: props(m[1]) };
|
|
110
|
+
}
|
|
111
|
+
if ((m = title.match(/^(::?)([a-z-]+)(\(\))?$/))) {
|
|
112
|
+
const name = escape(m[2]) + (m[3] ? String.raw `\(` : '(?![\\w-])');
|
|
113
|
+
// `::before` also has a legacy single-colon form.
|
|
114
|
+
const re = m[1] === '::' ? new RegExp(`::?${name}`) : new RegExp(`(?<!:):${name}`);
|
|
115
|
+
return { selector: (s) => re.test(s.raw) };
|
|
116
|
+
}
|
|
117
|
+
if (title === 'lang()') {
|
|
118
|
+
return { selector: (s) => /:lang\(/.test(s.raw) };
|
|
119
|
+
}
|
|
120
|
+
if ((m = title.match(/^([a-z-]+)\(\)$/))) {
|
|
121
|
+
return { decl: valueMatch(fn(escape(m[1]))) };
|
|
122
|
+
}
|
|
123
|
+
if ((m = title.match(/^([a-z%]+) unit$/))) {
|
|
124
|
+
const re = new RegExp(`${UNIT_PREFIX}${escape(m[1])}${m[1] === '%' ? '' : '(?![\\w-])'}`, 'i');
|
|
125
|
+
return { decl: valueMatch(re) };
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
if (feature.category === 'html') {
|
|
129
|
+
let m;
|
|
130
|
+
if ((m = title.match(/^<([a-z0-9]+)> element$/)))
|
|
131
|
+
return { element: tags(m[1]) };
|
|
132
|
+
if ((m = title.match(/^([a-z-]+) attribute$/)))
|
|
133
|
+
return { element: hasAttr(m[1]) };
|
|
134
|
+
}
|
|
135
|
+
return undefined;
|
|
136
|
+
}
|
|
137
|
+
export function buildRules(features) {
|
|
138
|
+
const rules = new Map();
|
|
139
|
+
for (const feature of features) {
|
|
140
|
+
const rule = manual[feature.slug] ?? autoRule(feature);
|
|
141
|
+
if (rule)
|
|
142
|
+
rules.set(feature.slug, rule);
|
|
143
|
+
}
|
|
144
|
+
return rules;
|
|
145
|
+
}
|
|
146
|
+
export const DOCUMENT_FEATURES = new Set([
|
|
147
|
+
'css-comments',
|
|
148
|
+
'css-nesting',
|
|
149
|
+
'html-comments',
|
|
150
|
+
'html-doctype',
|
|
151
|
+
'image-apng',
|
|
152
|
+
'image-avif',
|
|
153
|
+
'image-base64',
|
|
154
|
+
'image-bmp',
|
|
155
|
+
'image-gif',
|
|
156
|
+
'image-heif',
|
|
157
|
+
'image-ico',
|
|
158
|
+
'image-jpg',
|
|
159
|
+
'image-mp4',
|
|
160
|
+
'image-png',
|
|
161
|
+
'image-svg',
|
|
162
|
+
'image-tiff',
|
|
163
|
+
'image-webp',
|
|
164
|
+
]);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export interface SelectorInfo {
|
|
2
|
+
raw: string;
|
|
3
|
+
grouping: boolean;
|
|
4
|
+
child: boolean;
|
|
5
|
+
adjacent: boolean;
|
|
6
|
+
generalSibling: boolean;
|
|
7
|
+
descendant: boolean;
|
|
8
|
+
type: boolean;
|
|
9
|
+
universal: boolean;
|
|
10
|
+
cls: boolean;
|
|
11
|
+
id: boolean;
|
|
12
|
+
attribute: boolean;
|
|
13
|
+
chaining: boolean;
|
|
14
|
+
}
|
|
15
|
+
export declare function analyzeSelector(selector: string): SelectorInfo;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
export function analyzeSelector(selector) {
|
|
2
|
+
let s = selector.replace(/"[^"]*"|'[^']*'/g, '""').replace(/\[[^\]]*\]/g, '[]');
|
|
3
|
+
// Collapse pseudo-class arguments so `:not(.a > b)` does not count as a child combinator.
|
|
4
|
+
while (/\([^()]+\)/.test(s))
|
|
5
|
+
s = s.replace(/\([^()]+\)/g, '()');
|
|
6
|
+
const parts = s.split(',').map((p) => p.trim()).filter(Boolean);
|
|
7
|
+
const info = {
|
|
8
|
+
raw: selector,
|
|
9
|
+
grouping: parts.length > 1,
|
|
10
|
+
child: false,
|
|
11
|
+
adjacent: false,
|
|
12
|
+
generalSibling: false,
|
|
13
|
+
descendant: false,
|
|
14
|
+
type: false,
|
|
15
|
+
universal: false,
|
|
16
|
+
cls: false,
|
|
17
|
+
id: false,
|
|
18
|
+
attribute: false,
|
|
19
|
+
chaining: false,
|
|
20
|
+
};
|
|
21
|
+
for (const part of parts) {
|
|
22
|
+
const tight = part.replace(/\s*([>+~])\s*/g, '$1');
|
|
23
|
+
if (tight.includes('>'))
|
|
24
|
+
info.child = true;
|
|
25
|
+
if (tight.includes('+'))
|
|
26
|
+
info.adjacent = true;
|
|
27
|
+
if (tight.includes('~'))
|
|
28
|
+
info.generalSibling = true;
|
|
29
|
+
if (/\S\s+\S/.test(tight))
|
|
30
|
+
info.descendant = true;
|
|
31
|
+
for (const compound of tight.split(/[\s>+~]+/)) {
|
|
32
|
+
if (/^[a-z][\w-]*/i.test(compound))
|
|
33
|
+
info.type = true;
|
|
34
|
+
if (compound.startsWith('*'))
|
|
35
|
+
info.universal = true;
|
|
36
|
+
if (/\.[\w-]/.test(compound))
|
|
37
|
+
info.cls = true;
|
|
38
|
+
if (/#[\w-]/.test(compound))
|
|
39
|
+
info.id = true;
|
|
40
|
+
if (compound.includes('[]'))
|
|
41
|
+
info.attribute = true;
|
|
42
|
+
if ((compound.match(/\.[\w-]+|#[\w-]+|\[\]/g) ?? []).length >= 2)
|
|
43
|
+
info.chaining = true;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return info;
|
|
47
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export { lint } from './core/lint.js';
|
|
2
|
+
export type { LintOptions, LintResult, Issue, ParseError } from './core/lint.js';
|
|
3
|
+
export { analyzeSelector } from './core/selectors.js';
|
|
4
|
+
export type { SelectorInfo } from './core/selectors.js';
|
|
5
|
+
export { BASELINE_FEATURES } from './core/baseline.js';
|
|
6
|
+
export { DEFAULT_CLIENTS, SKIPPED_PLATFORMS } from './core/clients.js';
|
|
7
|
+
export type { ClientSupport, Status } from './core/clients.js';
|
|
8
|
+
export { loadDataset } from './core/data.js';
|
|
9
|
+
export type { Dataset, Feature } from './core/data.js';
|
|
10
|
+
export { formatText, formatMarkdown, formatGithub } from './cli/format.js';
|
|
11
|
+
export type { FileResult, Format } from './cli/format.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { lint } from './core/lint.js';
|
|
2
|
+
export { analyzeSelector } from './core/selectors.js';
|
|
3
|
+
export { BASELINE_FEATURES } from './core/baseline.js';
|
|
4
|
+
export { DEFAULT_CLIENTS, SKIPPED_PLATFORMS } from './core/clients.js';
|
|
5
|
+
export { loadDataset } from './core/data.js';
|
|
6
|
+
export { formatText, formatMarkdown, formatGithub } from './cli/format.js';
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export type FixKind = 'must-fix' | 'degradation';
|
|
2
|
+
export interface Fix {
|
|
3
|
+
kind: FixKind;
|
|
4
|
+
text: string;
|
|
5
|
+
}
|
|
6
|
+
interface FixTable {
|
|
7
|
+
rows: {
|
|
8
|
+
pattern: RegExp;
|
|
9
|
+
fix: Fix;
|
|
10
|
+
}[];
|
|
11
|
+
fallback: string;
|
|
12
|
+
}
|
|
13
|
+
export declare function parseFixes(markdown: string): FixTable;
|
|
14
|
+
export declare function findFix(slug: string): Fix | undefined;
|
|
15
|
+
export declare function fallbackAdvice(): string;
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
let cached;
|
|
3
|
+
export function parseFixes(markdown) {
|
|
4
|
+
const rows = [];
|
|
5
|
+
const fallback = [];
|
|
6
|
+
let section;
|
|
7
|
+
for (const line of markdown.split(/\r?\n/)) {
|
|
8
|
+
const heading = line.match(/^## (.+)$/);
|
|
9
|
+
if (heading) {
|
|
10
|
+
const title = heading[1].toLowerCase();
|
|
11
|
+
section = title.startsWith('must fix')
|
|
12
|
+
? 'must-fix'
|
|
13
|
+
: title.startsWith('acceptable')
|
|
14
|
+
? 'degradation'
|
|
15
|
+
: title.startsWith('anything not listed')
|
|
16
|
+
? 'fallback'
|
|
17
|
+
: undefined;
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
if (section === 'fallback') {
|
|
21
|
+
if (line.trim())
|
|
22
|
+
fallback.push(line.trim());
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
if (!section || !line.startsWith('|'))
|
|
26
|
+
continue;
|
|
27
|
+
const [first, ...rest] = line.slice(1, line.lastIndexOf('|')).split('|');
|
|
28
|
+
const slugs = [...first.matchAll(/`([^`]+)`/g)].map((m) => m[1]);
|
|
29
|
+
const text = rest.join('|').trim();
|
|
30
|
+
if (!slugs.length || !text)
|
|
31
|
+
continue;
|
|
32
|
+
for (const slug of slugs) {
|
|
33
|
+
const source = slug.replace(/[.+?^${}()[\]\\]/g, '\\$&').replaceAll('*', '.*');
|
|
34
|
+
rows.push({ pattern: new RegExp(`^${source}$`), fix: { kind: section, text } });
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return { rows, fallback: fallback.join(' ') };
|
|
38
|
+
}
|
|
39
|
+
function table() {
|
|
40
|
+
cached ??= parseFixes(readFileSync(new URL('../../skills/email-lint/references/fixes.md', import.meta.url), 'utf8'));
|
|
41
|
+
return cached;
|
|
42
|
+
}
|
|
43
|
+
export function findFix(slug) {
|
|
44
|
+
return table().rows.find((row) => row.pattern.test(slug))?.fix;
|
|
45
|
+
}
|
|
46
|
+
export function fallbackAdvice() {
|
|
47
|
+
return table().fallback;
|
|
48
|
+
}
|
|
@@ -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: 'email-lint', title: 'email-lint', version: packageVersion() },
|
|
32
|
+
instructions: 'Checks rendered email HTML against caniemail.com data. Call lint_email on the built HTML, then explain_feature for each issue that affects clients the audience uses. Fix layout breakage first; decoration that a client drops is acceptable.',
|
|
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,83 @@
|
|
|
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
|
+
html: {
|
|
18
|
+
type: string;
|
|
19
|
+
description: string;
|
|
20
|
+
};
|
|
21
|
+
path: {
|
|
22
|
+
type: string;
|
|
23
|
+
description: string;
|
|
24
|
+
};
|
|
25
|
+
clients: {
|
|
26
|
+
type: string;
|
|
27
|
+
items: {
|
|
28
|
+
type: string;
|
|
29
|
+
};
|
|
30
|
+
description: string;
|
|
31
|
+
};
|
|
32
|
+
ignore: {
|
|
33
|
+
type: string;
|
|
34
|
+
items: {
|
|
35
|
+
type: string;
|
|
36
|
+
};
|
|
37
|
+
description: string;
|
|
38
|
+
};
|
|
39
|
+
strict: {
|
|
40
|
+
type: string;
|
|
41
|
+
description: string;
|
|
42
|
+
};
|
|
43
|
+
slug?: undefined;
|
|
44
|
+
};
|
|
45
|
+
additionalProperties: boolean;
|
|
46
|
+
required?: undefined;
|
|
47
|
+
};
|
|
48
|
+
annotations: {
|
|
49
|
+
readOnlyHint: boolean;
|
|
50
|
+
openWorldHint: boolean;
|
|
51
|
+
};
|
|
52
|
+
} | {
|
|
53
|
+
name: string;
|
|
54
|
+
title: string;
|
|
55
|
+
description: string;
|
|
56
|
+
inputSchema: {
|
|
57
|
+
type: string;
|
|
58
|
+
properties: {
|
|
59
|
+
slug: {
|
|
60
|
+
type: string;
|
|
61
|
+
description: string;
|
|
62
|
+
};
|
|
63
|
+
clients: {
|
|
64
|
+
type: string;
|
|
65
|
+
items: {
|
|
66
|
+
type: string;
|
|
67
|
+
};
|
|
68
|
+
description: string;
|
|
69
|
+
};
|
|
70
|
+
html?: undefined;
|
|
71
|
+
path?: undefined;
|
|
72
|
+
ignore?: undefined;
|
|
73
|
+
strict?: undefined;
|
|
74
|
+
};
|
|
75
|
+
required: string[];
|
|
76
|
+
additionalProperties: boolean;
|
|
77
|
+
};
|
|
78
|
+
annotations: {
|
|
79
|
+
readOnlyHint: boolean;
|
|
80
|
+
openWorldHint: boolean;
|
|
81
|
+
};
|
|
82
|
+
})[];
|
|
83
|
+
export declare function callTool(name: string, args: Record<string, unknown>): Promise<ToolResult>;
|