@nuasite/agent-summary 0.64.3 → 0.65.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/dist/agent-summary-integration.d.ts.map +1 -0
- package/dist/agent-summary-integration.js +71 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +0 -0
- package/dist/{types/utils.d.ts → utils.d.ts} +1 -1
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +151 -0
- package/package.json +7 -7
- package/src/agent-summary-integration.ts +2 -2
- package/src/index.ts +2 -2
- package/src/tsconfig.json +5 -1
- package/src/utils.ts +1 -1
- package/dist/types/agent-summary-integration.d.ts.map +0 -1
- package/dist/types/index.d.ts +0 -3
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/tsconfig.tsbuildinfo +0 -1
- package/dist/types/types.d.ts.map +0 -1
- package/dist/types/utils.d.ts.map +0 -1
- /package/dist/{types/agent-summary-integration.d.ts → agent-summary-integration.d.ts} +0 -0
- /package/dist/{types/types.d.ts → types.d.ts} +0 -0
package/dist/utils.js
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { promises as fs } from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
export const AGENTS_PATH = path.resolve('AGENTS.md');
|
|
4
|
+
export const SUMMARY_START = '<page_summary>';
|
|
5
|
+
export const SUMMARY_END = '</page_summary>';
|
|
6
|
+
export const decodeEntities = (value) => {
|
|
7
|
+
return value
|
|
8
|
+
.replace(/&/gi, '&')
|
|
9
|
+
.replace(/</gi, '<')
|
|
10
|
+
.replace(/>/gi, '>')
|
|
11
|
+
.replace(/"/gi, '"')
|
|
12
|
+
.replace(/'/gi, "'")
|
|
13
|
+
.replace(/ /gi, ' ');
|
|
14
|
+
};
|
|
15
|
+
export const sanitize = (value) => {
|
|
16
|
+
if (!value) {
|
|
17
|
+
return '';
|
|
18
|
+
}
|
|
19
|
+
const decoded = decodeEntities(value);
|
|
20
|
+
return decoded
|
|
21
|
+
.replace(/\s+/g, ' ')
|
|
22
|
+
.replace(/[\u2012-\u2015]/g, '-')
|
|
23
|
+
.replace(/\u2026/g, '...')
|
|
24
|
+
.trim();
|
|
25
|
+
};
|
|
26
|
+
export const truncate = (value, maxLength) => {
|
|
27
|
+
if (value.length <= maxLength) {
|
|
28
|
+
return value;
|
|
29
|
+
}
|
|
30
|
+
return `${value.slice(0, maxLength - 3).trimEnd()}...`;
|
|
31
|
+
};
|
|
32
|
+
export const sentence = (label, value) => {
|
|
33
|
+
const trimmed = value.trim();
|
|
34
|
+
const suffix = /[.!?]$/.test(trimmed) ? '' : '.';
|
|
35
|
+
return `${label}: ${trimmed}${suffix}`;
|
|
36
|
+
};
|
|
37
|
+
export const normalizeRoute = (pathname) => {
|
|
38
|
+
const trimmed = pathname.replace(/^\/+/, '').replace(/\/+$/, '');
|
|
39
|
+
if (trimmed.length === 0) {
|
|
40
|
+
return '/';
|
|
41
|
+
}
|
|
42
|
+
return `/${trimmed}`;
|
|
43
|
+
};
|
|
44
|
+
export const formatDestination = (value) => {
|
|
45
|
+
if (!value) {
|
|
46
|
+
return '/';
|
|
47
|
+
}
|
|
48
|
+
if (/^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(value) || value.startsWith('#')) {
|
|
49
|
+
return value;
|
|
50
|
+
}
|
|
51
|
+
return normalizeRoute(value);
|
|
52
|
+
};
|
|
53
|
+
export const resolveHtmlPath = async (distDir, pathname) => {
|
|
54
|
+
const normalizedRoute = normalizeRoute(pathname);
|
|
55
|
+
const routeSegment = normalizedRoute === '/' ? '' : normalizedRoute.slice(1);
|
|
56
|
+
const candidates = routeSegment === ''
|
|
57
|
+
? [path.join(distDir, 'index.html')]
|
|
58
|
+
: [
|
|
59
|
+
path.join(distDir, routeSegment, 'index.html'),
|
|
60
|
+
path.join(distDir, `${routeSegment}.html`),
|
|
61
|
+
];
|
|
62
|
+
for (const candidate of candidates) {
|
|
63
|
+
try {
|
|
64
|
+
const stats = await fs.stat(candidate);
|
|
65
|
+
if (stats.isFile()) {
|
|
66
|
+
return candidate;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
// Ignore missing files; try next candidate.
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return null;
|
|
74
|
+
};
|
|
75
|
+
export const extractMetaFromHtml = (route, html) => {
|
|
76
|
+
const titleMatch = html.match(/<title[^>]*>([^<]*)<\/title>/i);
|
|
77
|
+
const metaMatch = html.match(/<meta[^>]+name=["']description["'][^>]*?>/i);
|
|
78
|
+
const contentMatch = metaMatch?.[0]?.match(/content=["']([^"']*)["']/i);
|
|
79
|
+
const headingMatches = [...html.matchAll(/<h([1-6])[^>]*>([\s\S]*?)<\/h\1>/gi)];
|
|
80
|
+
const headingEntries = headingMatches
|
|
81
|
+
.map((match) => ({
|
|
82
|
+
level: `h${match[1]}`,
|
|
83
|
+
text: sanitize(match[2]?.replace(/<[^>]+>/g, '')),
|
|
84
|
+
}))
|
|
85
|
+
.filter((entry) => entry.text.length > 0);
|
|
86
|
+
const primaryHeading = headingEntries.find((entry) => entry.level === 'h1' && entry.text.length >= 8)
|
|
87
|
+
|| headingEntries.find((entry) => entry.text.length >= 16)
|
|
88
|
+
|| headingEntries[0];
|
|
89
|
+
const primaryHeadingText = primaryHeading?.text ?? '';
|
|
90
|
+
const paragraphMatches = [...html.matchAll(/<p[^>]*>([\s\S]*?)<\/p>/gi)];
|
|
91
|
+
const paragraphCandidates = paragraphMatches.map((match) => sanitize(match[1]?.replace(/<[^>]+>/g, '')));
|
|
92
|
+
const fallbackParagraph = paragraphCandidates.find((text) => text.length >= 40) ?? paragraphCandidates[0] ?? '';
|
|
93
|
+
const title = sanitize(titleMatch?.[1] ?? (primaryHeadingText || 'Untitled page'));
|
|
94
|
+
const descriptionSource = contentMatch?.[1];
|
|
95
|
+
const paragraphDescription = descriptionSource ? '' : fallbackParagraph;
|
|
96
|
+
const description = truncate((descriptionSource ? sanitize(descriptionSource) : paragraphDescription) || 'Description unavailable', 220);
|
|
97
|
+
return {
|
|
98
|
+
route: normalizeRoute(route),
|
|
99
|
+
title,
|
|
100
|
+
description,
|
|
101
|
+
headlines: headingEntries,
|
|
102
|
+
};
|
|
103
|
+
};
|
|
104
|
+
const createJsonlRecords = (pages, redirects) => {
|
|
105
|
+
const records = [];
|
|
106
|
+
for (const page of pages.sort((a, b) => a.route.localeCompare(b.route))) {
|
|
107
|
+
records.push(JSON.stringify({
|
|
108
|
+
kind: 'page',
|
|
109
|
+
route: page.route,
|
|
110
|
+
title: page.title,
|
|
111
|
+
description: page.description,
|
|
112
|
+
headlines: page.headlines,
|
|
113
|
+
}));
|
|
114
|
+
}
|
|
115
|
+
for (const redirect of redirects.sort((a, b) => a.from.localeCompare(b.from))) {
|
|
116
|
+
records.push(JSON.stringify({
|
|
117
|
+
kind: 'redirect',
|
|
118
|
+
route: redirect.from,
|
|
119
|
+
to: redirect.to,
|
|
120
|
+
status: redirect.status,
|
|
121
|
+
}));
|
|
122
|
+
}
|
|
123
|
+
return records;
|
|
124
|
+
};
|
|
125
|
+
export const updateAgentsSummary = async (pages, redirects) => {
|
|
126
|
+
const jsonlRecords = createJsonlRecords(pages, redirects);
|
|
127
|
+
const summaryBody = jsonlRecords.join('\n');
|
|
128
|
+
const summaryBlock = summaryBody
|
|
129
|
+
? `${SUMMARY_START}\n${summaryBody}\n${SUMMARY_END}\n`
|
|
130
|
+
: `${SUMMARY_START}\n${SUMMARY_END}\n`;
|
|
131
|
+
const agentsContent = await fs.readFile(AGENTS_PATH, 'utf8').catch(() => '');
|
|
132
|
+
if (agentsContent.includes(SUMMARY_START) && agentsContent.includes(SUMMARY_END)) {
|
|
133
|
+
const startIndex = agentsContent.indexOf(SUMMARY_START);
|
|
134
|
+
const summaryEndIndex = agentsContent.indexOf(SUMMARY_END, startIndex);
|
|
135
|
+
if (summaryEndIndex === -1) {
|
|
136
|
+
const recovered = `${agentsContent.slice(0, startIndex)}${summaryBlock}`;
|
|
137
|
+
await fs.writeFile(AGENTS_PATH, recovered, 'utf8');
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
let endIndex = summaryEndIndex + SUMMARY_END.length;
|
|
141
|
+
while (endIndex < agentsContent.length && (agentsContent[endIndex] === '\n' || agentsContent[endIndex] === '\r')) {
|
|
142
|
+
endIndex += 1;
|
|
143
|
+
}
|
|
144
|
+
const updated = `${agentsContent.slice(0, startIndex)}${summaryBlock}${agentsContent.slice(endIndex)}`;
|
|
145
|
+
await fs.writeFile(AGENTS_PATH, updated, 'utf8');
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
const prefix = agentsContent.trimEnd();
|
|
149
|
+
const separator = prefix.length === 0 ? '' : '\n';
|
|
150
|
+
await fs.writeFile(AGENTS_PATH, `${prefix}${separator}${summaryBlock}`, 'utf8');
|
|
151
|
+
};
|
package/package.json
CHANGED
|
@@ -14,15 +14,15 @@
|
|
|
14
14
|
"directory": "packages/agent-summary"
|
|
15
15
|
},
|
|
16
16
|
"license": "Apache-2.0",
|
|
17
|
-
"version": "0.
|
|
18
|
-
"module": "
|
|
19
|
-
"types": "
|
|
17
|
+
"version": "0.65.0",
|
|
18
|
+
"module": "./dist/index.js",
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
20
|
"type": "module",
|
|
21
21
|
"exports": {
|
|
22
22
|
".": {
|
|
23
|
-
"types": "./
|
|
24
|
-
"import": "./
|
|
25
|
-
"default": "./
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"import": "./dist/index.js",
|
|
25
|
+
"default": "./dist/index.js"
|
|
26
26
|
}
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {},
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"typescript": "^6.0.2"
|
|
36
36
|
},
|
|
37
37
|
"scripts": {
|
|
38
|
-
"prepack": "bun run ../../scripts/workspace-deps/resolve-deps.ts"
|
|
38
|
+
"prepack": "bun run ../../scripts/workspace-deps/resolve-deps.ts --compiled-exports"
|
|
39
39
|
},
|
|
40
40
|
"keywords": [
|
|
41
41
|
"devtools",
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { AstroIntegration } from 'astro'
|
|
2
2
|
import fs from 'node:fs/promises'
|
|
3
3
|
import { fileURLToPath } from 'node:url'
|
|
4
|
-
import type { PageMeta, RedirectMeta } from './types'
|
|
5
|
-
import { extractMetaFromHtml, formatDestination, normalizeRoute, resolveHtmlPath, updateAgentsSummary } from './utils'
|
|
4
|
+
import type { PageMeta, RedirectMeta } from './types.ts'
|
|
5
|
+
import { extractMetaFromHtml, formatDestination, normalizeRoute, resolveHtmlPath, updateAgentsSummary } from './utils.ts'
|
|
6
6
|
|
|
7
7
|
export const agentsSummary = (): AstroIntegration => {
|
|
8
8
|
const redirectPathnames = new Set<string>()
|
package/src/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from './agent-summary-integration'
|
|
2
|
-
export * from './types'
|
|
1
|
+
export * from './agent-summary-integration.ts'
|
|
2
|
+
export * from './types.ts'
|
package/src/tsconfig.json
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"extends": "../tsconfig.settings.json",
|
|
3
3
|
"compilerOptions": {
|
|
4
|
-
"outDir": "../dist
|
|
4
|
+
"outDir": "../dist",
|
|
5
|
+
"rootDir": ".",
|
|
6
|
+
// Compiled JS is what the published package runs (see scripts/workspace-deps).
|
|
7
|
+
"emitDeclarationOnly": false,
|
|
8
|
+
"rewriteRelativeImportExtensions": true
|
|
5
9
|
}
|
|
6
10
|
}
|
package/src/utils.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { promises as fs } from 'node:fs'
|
|
2
2
|
import path from 'node:path'
|
|
3
|
-
import type { PageMeta, RedirectMeta } from './types'
|
|
3
|
+
import type { PageMeta, RedirectMeta } from './types.ts'
|
|
4
4
|
|
|
5
5
|
export const AGENTS_PATH = path.resolve('AGENTS.md')
|
|
6
6
|
export const SUMMARY_START = '<page_summary>'
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"agent-summary-integration.d.ts","sourceRoot":"","sources":["../../src/agent-summary-integration.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,OAAO,CAAA;AAM7C,eAAO,MAAM,aAAa,QAAO,gBA2EhC,CAAA"}
|
package/dist/types/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,6BAA6B,CAAA;AAC3C,cAAc,SAAS,CAAA"}
|