agentface 0.4.0 → 0.5.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/agentface.mjs +119 -37
- package/package.json +1 -1
package/dist/agentface.mjs
CHANGED
|
@@ -31,6 +31,15 @@ function registrableOrigin(input) {
|
|
|
31
31
|
return origin;
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
+
// worker/tag.js
|
|
35
|
+
var toolListUrl = (serviceOrigin, id) => `${serviceOrigin}/s/${id}.md`;
|
|
36
|
+
function siteTag(serviceOrigin, id) {
|
|
37
|
+
return `<script async src="${serviceOrigin}/s/${id}.js" data-ai-agents="Tools for AI agents on this site: ${toolListUrl(serviceOrigin, id)}"></script>`;
|
|
38
|
+
}
|
|
39
|
+
function siteNotice(serviceOrigin, id) {
|
|
40
|
+
return `<noscript>This site has tools for AI agents, which run in its pages in a browser: ${toolListUrl(serviceOrigin, id)}</noscript>`;
|
|
41
|
+
}
|
|
42
|
+
|
|
34
43
|
// cli/heads.mjs
|
|
35
44
|
import { execFile } from "node:child_process";
|
|
36
45
|
import { readdir, readFile, stat } from "node:fs/promises";
|
|
@@ -114,17 +123,23 @@ var exists = (file) => stat(file).then(
|
|
|
114
123
|
);
|
|
115
124
|
var KEY = /\/s\/(site-[a-z0-9-]+)\.js(?=[?#"'`\s]|$)/;
|
|
116
125
|
var TAG = new RegExp(`<script\\b[^>]*?${KEY.source}[^>]*?(?:\\/>|>\\s*<\\/script\\s*>)`, "gi");
|
|
126
|
+
var NOTICE = /<noscript\b[^>]*>[^<]*\/s\/(site-[a-z0-9-]+)\.md[^<]*<\/noscript\s*>/i;
|
|
127
|
+
var BODY = /<body(?=[\s>/])(?:<\?[\s\S]*?\?>|<%[\s\S]*?%>|\{[^{}]*(?:\{[^{}]*\}[^{}]*)*\}|"[^"]*"|'[^']*'|[^>])*>/i;
|
|
128
|
+
var onItsLine = (pattern) => new RegExp(`(\\r?\\n[ \\t]*)?${pattern.source}`, "gi");
|
|
117
129
|
var OURS = "Added by `npx agentface init`";
|
|
118
130
|
function tagKey(source) {
|
|
119
131
|
return source.match(KEY)?.[1] ?? null;
|
|
120
132
|
}
|
|
133
|
+
function noticeKey(source) {
|
|
134
|
+
return source.match(NOTICE)?.[1] ?? null;
|
|
135
|
+
}
|
|
121
136
|
function indentUnit(source) {
|
|
122
137
|
return source.match(/^(\t| {2,4})\S/m)?.[1] ?? " ";
|
|
123
138
|
}
|
|
124
139
|
var indentOf = (line) => line.match(/^[ \t]*/)[0];
|
|
125
140
|
function addTag(source, tag) {
|
|
126
141
|
let first = true;
|
|
127
|
-
const replaced = source.replace(
|
|
142
|
+
const replaced = source.replace(onItsLine(TAG), (match, line) => {
|
|
128
143
|
if (!first) return "";
|
|
129
144
|
first = false;
|
|
130
145
|
return (line ?? "") + tag;
|
|
@@ -151,6 +166,24 @@ function addTag(source, tag) {
|
|
|
151
166
|
const lines = [`${child}<head>`, `${child}${step}${tag}`, `${child}</head>`];
|
|
152
167
|
return source.slice(0, end) + eol + lines.join(eol) + source.slice(end);
|
|
153
168
|
}
|
|
169
|
+
function addNotice(source, notice) {
|
|
170
|
+
let first = true;
|
|
171
|
+
const replaced = source.replace(onItsLine(NOTICE), (match, line) => {
|
|
172
|
+
if (!first) return "";
|
|
173
|
+
first = false;
|
|
174
|
+
return (line ?? "") + notice;
|
|
175
|
+
});
|
|
176
|
+
if (!first) return replaced;
|
|
177
|
+
const body = BODY.exec(source);
|
|
178
|
+
if (!body) return null;
|
|
179
|
+
const end = body.index + body[0].length;
|
|
180
|
+
const next = source.slice(end).match(/^[ \t]*\r?\n([ \t]*)(.*)/);
|
|
181
|
+
if (!next) return source.slice(0, end) + notice + source.slice(end);
|
|
182
|
+
const eol = source.includes("\r\n") ? "\r\n" : "\n";
|
|
183
|
+
const outer = indentOf(source.slice(source.lastIndexOf("\n", body.index) + 1));
|
|
184
|
+
const indent = next[2].trim() && !/^<\/body\b/i.test(next[2]) ? next[1] : outer + indentUnit(source);
|
|
185
|
+
return source.slice(0, end) + eol + indent + notice + source.slice(end);
|
|
186
|
+
}
|
|
154
187
|
function gitFiles(dir) {
|
|
155
188
|
return new Promise((resolve) => {
|
|
156
189
|
execFile(
|
|
@@ -193,7 +226,7 @@ async function siteGenerator(dir) {
|
|
|
193
226
|
return "Jekyll";
|
|
194
227
|
return null;
|
|
195
228
|
}
|
|
196
|
-
var NEXT_DOCUMENT = (tag) => `// ${OURS}: Next.js's own document, plus the Agentface tag.
|
|
229
|
+
var NEXT_DOCUMENT = (tag, notice) => `// ${OURS}: Next.js's own document, plus the Agentface tag.
|
|
197
230
|
import { Html, Head, Main, NextScript } from 'next/document';
|
|
198
231
|
|
|
199
232
|
export default function Document() {
|
|
@@ -203,23 +236,28 @@ export default function Document() {
|
|
|
203
236
|
${tag}
|
|
204
237
|
</Head>
|
|
205
238
|
<body>
|
|
206
|
-
|
|
239
|
+
${notice ? ` ${notice}
|
|
240
|
+
` : ""} <Main />
|
|
207
241
|
<NextScript />
|
|
208
242
|
</body>
|
|
209
243
|
</Html>
|
|
210
244
|
);
|
|
211
245
|
}
|
|
212
246
|
`;
|
|
213
|
-
var GATSBY_SSR = (tag) => {
|
|
247
|
+
var GATSBY_SSR = (tag, notice) => {
|
|
214
248
|
const src = tag.match(/\bsrc="([^"]+)"/)[1];
|
|
249
|
+
const about = tag.match(/\bdata-ai-agents="([^"]+)"/)?.[1];
|
|
250
|
+
const text = notice?.match(/>([^<]*)</)[1];
|
|
251
|
+
const props = `key: 'agentface', async: true, src: '${src}'${about ? `, 'data-ai-agents': '${about}'` : ""}`;
|
|
215
252
|
return `// ${OURS}: puts the Agentface tag in every page's <head>.
|
|
216
253
|
const React = require('react');
|
|
217
254
|
|
|
218
|
-
exports.onRenderBody = ({ setHeadComponents }) => {
|
|
255
|
+
exports.onRenderBody = ({ setHeadComponents${text ? ", setPreBodyComponents" : ""} }) => {
|
|
219
256
|
setHeadComponents([
|
|
220
|
-
React.createElement('script', {
|
|
257
|
+
React.createElement('script', { ${props} }),
|
|
221
258
|
]);
|
|
222
|
-
};
|
|
259
|
+
${text ? ` setPreBodyComponents([React.createElement('noscript', { key: 'agentface' }, '${text}')]);
|
|
260
|
+
` : ""}};
|
|
223
261
|
`;
|
|
224
262
|
};
|
|
225
263
|
async function project(dir) {
|
|
@@ -238,30 +276,52 @@ async function findTags(dir) {
|
|
|
238
276
|
const tagged = [];
|
|
239
277
|
for (const file of list2) {
|
|
240
278
|
const source = await contents(dir, file);
|
|
241
|
-
const key = source === null ? null : tagKey(source);
|
|
279
|
+
const key = source === null ? null : tagKey(source) ?? noticeKey(source);
|
|
242
280
|
if (key) tagged.push({ file, source, key });
|
|
243
281
|
}
|
|
244
282
|
return tagged;
|
|
245
283
|
}
|
|
284
|
+
function madeByInit(source) {
|
|
285
|
+
const src = source.match(/['"](https?:\/\/[^'"\s]+)['"]/)?.[1];
|
|
286
|
+
const [, origin, id] = src?.match(/^(.+)\/s\/(site-[a-z0-9-]+)\.js$/) ?? [];
|
|
287
|
+
if (!origin) return false;
|
|
288
|
+
const made = [
|
|
289
|
+
[`<script async src="${src}"></script>`, null],
|
|
290
|
+
[siteTag(origin, id), siteNotice(origin, id)]
|
|
291
|
+
];
|
|
292
|
+
return made.some(
|
|
293
|
+
([tag, notice]) => [NEXT_DOCUMENT, GATSBY_SSR].some((write) => source === write(tag, notice))
|
|
294
|
+
);
|
|
295
|
+
}
|
|
246
296
|
function removeTag(source) {
|
|
247
|
-
if (source.includes(OURS))
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
});
|
|
297
|
+
if (source.includes(OURS) && madeByInit(source)) return null;
|
|
298
|
+
return [TAG, NOTICE].reduce(
|
|
299
|
+
(text, pattern) => text.replace(onItsLine(pattern), (match, line, ...rest) => {
|
|
300
|
+
const after = rest.at(-1).slice(rest.at(-2) + match.length);
|
|
301
|
+
return line && !/^[ \t]*(?:\r?\n|$)/.test(after) ? line : "";
|
|
302
|
+
}),
|
|
303
|
+
source
|
|
304
|
+
);
|
|
256
305
|
}
|
|
257
306
|
async function byHand(dir, list2, framework, reason, how) {
|
|
258
307
|
const tagged = [];
|
|
308
|
+
const noticed = [];
|
|
259
309
|
for (const file of list2) {
|
|
260
310
|
const text = await contents(dir, file);
|
|
261
311
|
const key = text === null ? null : tagKey(text);
|
|
262
312
|
if (key) tagged.push({ file, key });
|
|
263
|
-
|
|
264
|
-
|
|
313
|
+
if (text !== null && noticeKey(text)) noticed.push(noticeKey(text));
|
|
314
|
+
}
|
|
315
|
+
return {
|
|
316
|
+
framework,
|
|
317
|
+
heads: [],
|
|
318
|
+
bodies: [],
|
|
319
|
+
creates: [],
|
|
320
|
+
manual: { reason, how },
|
|
321
|
+
tagged,
|
|
322
|
+
noticed,
|
|
323
|
+
key: tagged[0]?.key ?? null
|
|
324
|
+
};
|
|
265
325
|
}
|
|
266
326
|
async function findHeads(dir) {
|
|
267
327
|
const { deps, framework, list: list2, git } = await project(dir);
|
|
@@ -275,6 +335,7 @@ async function findHeads(dir) {
|
|
|
275
335
|
`Add it by hand through ${where}, with this tag's src`
|
|
276
336
|
);
|
|
277
337
|
const heads = [];
|
|
338
|
+
const bodies = [];
|
|
278
339
|
for (const file of list2) {
|
|
279
340
|
const markup = MARKUP.test(file);
|
|
280
341
|
if (!markup && !LAYOUTS.some((layout) => layout.test(file)) || MAIL.test(path.posix.basename(file)))
|
|
@@ -283,6 +344,7 @@ async function findHeads(dir) {
|
|
|
283
344
|
if (text === null) continue;
|
|
284
345
|
const head = markup ? /<\/head\s*>/i.test(text) : /<\/head\s*>|<html\b/i.test(text);
|
|
285
346
|
if (head) heads.push({ file, source: text });
|
|
347
|
+
if (BODY.test(text)) bodies.push({ file, source: text });
|
|
286
348
|
}
|
|
287
349
|
const creates = [];
|
|
288
350
|
if (deps.next && !heads.some(({ file }) => /(?:^|\/)app\//.test(file))) {
|
|
@@ -316,7 +378,7 @@ async function findHeads(dir) {
|
|
|
316
378
|
"Add this line by hand, last in the <head> of the layout or template every page uses"
|
|
317
379
|
);
|
|
318
380
|
const sources = [...heads.map(({ source }) => source), ...creates.map(({ source }) => source ?? "")];
|
|
319
|
-
return { framework, heads, creates, key: sources.map(tagKey).find(Boolean) ?? null };
|
|
381
|
+
return { framework, heads, bodies, creates, key: sources.map(tagKey).find(Boolean) ?? null };
|
|
320
382
|
}
|
|
321
383
|
|
|
322
384
|
// cli/init.mjs
|
|
@@ -345,7 +407,7 @@ async function lookupSite(service, id, fetcher = fetch) {
|
|
|
345
407
|
{ unknownSite: true }
|
|
346
408
|
);
|
|
347
409
|
if (!response.ok) throw new Error(body?.message ?? `Agentface answered HTTP ${response.status}`);
|
|
348
|
-
return { ...body.site, tag: body.tag };
|
|
410
|
+
return { ...body.site, tag: body.tag, notice: body.notice ?? null };
|
|
349
411
|
}
|
|
350
412
|
async function register(service, url, fetcher = fetch) {
|
|
351
413
|
const { response, body } = await reach(
|
|
@@ -356,7 +418,7 @@ async function register(service, url, fetcher = fetch) {
|
|
|
356
418
|
);
|
|
357
419
|
if (!response.ok || !body?.site?.id)
|
|
358
420
|
throw new Error(body?.message ?? `Agentface could not register ${url} (HTTP ${response.status})`);
|
|
359
|
-
return { ...body.site, expiresAt: body.expiresAt, tag: body.tag };
|
|
421
|
+
return { ...body.site, expiresAt: body.expiresAt, tag: body.tag, notice: body.notice ?? null };
|
|
360
422
|
}
|
|
361
423
|
async function confirm(service, id, fetcher = fetch) {
|
|
362
424
|
const { response, body } = await reach(
|
|
@@ -460,7 +522,8 @@ async function siteFor({ id, url, dir, found, service, fetcher, exec, dryRun, lo
|
|
|
460
522
|
return {
|
|
461
523
|
id: "site-new",
|
|
462
524
|
origin,
|
|
463
|
-
tag:
|
|
525
|
+
tag: siteTag(service, "site-new"),
|
|
526
|
+
notice: siteNotice(service, "site-new")
|
|
464
527
|
};
|
|
465
528
|
}
|
|
466
529
|
const site = await register(service, origin + "/", fetcher);
|
|
@@ -475,21 +538,32 @@ function nextSteps(site, service) {
|
|
|
475
538
|
}
|
|
476
539
|
function prBody(site, files2, service = DEFAULT_SERVICE) {
|
|
477
540
|
const src = site.tag.match(/\bsrc="([^"]+)"/)?.[1];
|
|
541
|
+
const notice = site.notice ? ` A \`<noscript>\` line first in the body links agents that read pages without running them, and so never get the tools, to a list of them. Browsers that run scripts don't show it.` : "";
|
|
478
542
|
return `Adds the Agentface tag to ${site.origin}, so AI agents that use the site in a browser get tools made for them.
|
|
479
543
|
|
|
480
|
-
**What it does.**
|
|
544
|
+
**What it does.** It changes ${files2.map((file) => `\`${file}\``).join(", ")}. One \`<script async>\` line in the head loads ${src} on every page. It gives browser agents tools for this site (read a page as Markdown, search the site, fill in forms, close pop-ups) and reports how agents use them: which tools, how long they took and their error codes, never what anyone typed or read.${notice} People visiting the site see no change, and it sets no cookies.
|
|
481
545
|
|
|
482
546
|
**Check it.** ${nextSteps(site, service)}
|
|
483
547
|
|
|
484
|
-
**Uninstall.** Revert this pull request, or delete the line.
|
|
548
|
+
**Uninstall.** Revert this pull request, or delete ${site.notice ? "the two lines" : "the line"}.
|
|
485
549
|
`;
|
|
486
550
|
}
|
|
487
|
-
function
|
|
551
|
+
function noticeByHand(site, noticed, log) {
|
|
552
|
+
if (site.notice && !noticed.includes(site.id))
|
|
553
|
+
log(
|
|
554
|
+
`For agents that read pages without running them, also add this line first in the <body> of every page:
|
|
555
|
+
|
|
556
|
+
${site.notice}
|
|
557
|
+
`
|
|
558
|
+
);
|
|
559
|
+
}
|
|
560
|
+
function byHand2({ manual, tagged, noticed }, site, service, dryRun, log) {
|
|
488
561
|
const current = tagged.filter(({ key }) => key === site.id).map(({ file }) => file);
|
|
489
562
|
if (current.length) {
|
|
490
563
|
log(
|
|
491
564
|
`${list(current)} already ${current.length === 1 ? "has" : "have"} the Agentface tag for ${site.id}.`
|
|
492
565
|
);
|
|
566
|
+
noticeByHand(site, noticed, log);
|
|
493
567
|
log(nextSteps(site, service));
|
|
494
568
|
return { site, files: [], written: false };
|
|
495
569
|
}
|
|
@@ -505,6 +579,7 @@ function byHand2({ manual, tagged }, site, service, dryRun, log) {
|
|
|
505
579
|
${site.tag}
|
|
506
580
|
`
|
|
507
581
|
);
|
|
582
|
+
noticeByHand(site, noticed, log);
|
|
508
583
|
log(nextSteps(site, service));
|
|
509
584
|
return { site, files: [], written: false };
|
|
510
585
|
}
|
|
@@ -536,26 +611,33 @@ async function init({
|
|
|
536
611
|
if (pr && !dryRun) await readyForPr(dir, exec);
|
|
537
612
|
const site = await siteFor({ id, url, dir, found, service, fetcher, exec, dryRun, log });
|
|
538
613
|
if (found.manual) return byHand2(found, site, service, dryRun, log);
|
|
539
|
-
const
|
|
614
|
+
const before = /* @__PURE__ */ new Map();
|
|
615
|
+
const after = /* @__PURE__ */ new Map();
|
|
540
616
|
for (const { file, source } of found.heads) {
|
|
541
|
-
|
|
542
|
-
|
|
617
|
+
before.set(file, source);
|
|
618
|
+
after.set(file, addTag(source, site.tag));
|
|
543
619
|
}
|
|
620
|
+
if (site.notice)
|
|
621
|
+
for (const { file, source } of found.bodies) {
|
|
622
|
+
before.set(file, source);
|
|
623
|
+
after.set(file, addNotice(after.get(file) ?? source, site.notice) ?? after.get(file) ?? source);
|
|
624
|
+
}
|
|
544
625
|
for (const { file, content, source } of found.creates) {
|
|
545
|
-
|
|
546
|
-
|
|
626
|
+
before.set(file, source);
|
|
627
|
+
after.set(file, content(site.tag, site.notice));
|
|
547
628
|
}
|
|
629
|
+
const writes = [...after].filter(([file, content]) => content !== before.get(file));
|
|
548
630
|
const files2 = writes.map(([file]) => file);
|
|
549
|
-
const
|
|
631
|
+
const noticed = site.notice && (found.bodies.length || found.creates.length);
|
|
632
|
+
const what = `the Agentface tag${noticed ? " and its notice" : ""}`;
|
|
550
633
|
if (!writes.length) {
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
);
|
|
634
|
+
const tagged = [...found.heads, ...found.creates].map(({ file }) => file);
|
|
635
|
+
log(`${list(tagged)} already ${tagged.length === 1 ? "has" : "have"} the Agentface tag for ${site.id}.`);
|
|
554
636
|
log(nextSteps(site, service));
|
|
555
637
|
return { site, files: files2, written: false };
|
|
556
638
|
}
|
|
557
639
|
if (dryRun) {
|
|
558
|
-
log(`Would add
|
|
640
|
+
log(`Would add ${what} to ${list(files2)} (${found.framework}) for ${site.origin}.`);
|
|
559
641
|
return { site, files: files2, written: false };
|
|
560
642
|
}
|
|
561
643
|
let returnTo;
|
|
@@ -567,7 +649,7 @@ async function init({
|
|
|
567
649
|
await mkdir(path2.dirname(path2.join(dir, file)), { recursive: true });
|
|
568
650
|
await writeFile(path2.join(dir, file), content);
|
|
569
651
|
}
|
|
570
|
-
log(`Added
|
|
652
|
+
log(`Added ${what} to ${list(files2)} (${found.framework}) for ${site.origin}.`);
|
|
571
653
|
if (!pr) {
|
|
572
654
|
log(nextSteps(site, service));
|
|
573
655
|
return { site, files: files2, written: true };
|