agentface 0.4.0 → 0.5.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/dist/agentface.mjs +124 -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 defer src="${serviceOrigin}/s/${id}.js" data-api="This site has an API in its pages. What each function returns: ${toolListUrl(serviceOrigin, id)}"></script>`;
|
|
38
|
+
}
|
|
39
|
+
function siteNotice(serviceOrigin, id) {
|
|
40
|
+
return `<noscript>This site has an API that runs in its pages, in a browser. What each function returns: ${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,29 @@ 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 [, attribute, about] = tag.match(/\b(data-[a-z-]+)="([^"]+)"/) ?? [];
|
|
250
|
+
const text = notice?.match(/>([^<]*)</)[1];
|
|
251
|
+
const load = tag.match(/^<script (async|defer)\b/)?.[1] ?? "defer";
|
|
252
|
+
const props = `key: 'agentface', ${load}: true, src: '${src}'${about ? `, '${attribute}': '${about}'` : ""}`;
|
|
215
253
|
return `// ${OURS}: puts the Agentface tag in every page's <head>.
|
|
216
254
|
const React = require('react');
|
|
217
255
|
|
|
218
|
-
exports.onRenderBody = ({ setHeadComponents }) => {
|
|
256
|
+
exports.onRenderBody = ({ setHeadComponents${text ? ", setPreBodyComponents" : ""} }) => {
|
|
219
257
|
setHeadComponents([
|
|
220
|
-
React.createElement('script', {
|
|
258
|
+
React.createElement('script', { ${props} }),
|
|
221
259
|
]);
|
|
222
|
-
};
|
|
260
|
+
${text ? ` setPreBodyComponents([React.createElement('noscript', { key: 'agentface' }, '${text}')]);
|
|
261
|
+
` : ""}};
|
|
223
262
|
`;
|
|
224
263
|
};
|
|
225
264
|
async function project(dir) {
|
|
@@ -238,30 +277,56 @@ async function findTags(dir) {
|
|
|
238
277
|
const tagged = [];
|
|
239
278
|
for (const file of list2) {
|
|
240
279
|
const source = await contents(dir, file);
|
|
241
|
-
const key = source === null ? null : tagKey(source);
|
|
280
|
+
const key = source === null ? null : tagKey(source) ?? noticeKey(source);
|
|
242
281
|
if (key) tagged.push({ file, source, key });
|
|
243
282
|
}
|
|
244
283
|
return tagged;
|
|
245
284
|
}
|
|
285
|
+
function madeByInit(source) {
|
|
286
|
+
const gatsby = (pattern) => source.match(pattern)?.[1];
|
|
287
|
+
const load = gatsby(/key: 'agentface', (async|defer): true/);
|
|
288
|
+
const src = gatsby(/\bsrc: '([^']+)'/);
|
|
289
|
+
const [, attribute, about] = source.match(/'(data-[a-z-]+)': '([^']+)'/) ?? [];
|
|
290
|
+
const text = gatsby(/createElement\('noscript', \{ key: 'agentface' \}, '([^']*)'\)/);
|
|
291
|
+
const made = [
|
|
292
|
+
[NEXT_DOCUMENT, source.match(TAG)?.[0], source.match(NOTICE)?.[0]],
|
|
293
|
+
[
|
|
294
|
+
GATSBY_SSR,
|
|
295
|
+
load && src && `<script ${load} src="${src}"${about ? ` ${attribute}="${about}"` : ""}></script>`,
|
|
296
|
+
text && `<noscript>${text}</noscript>`
|
|
297
|
+
]
|
|
298
|
+
];
|
|
299
|
+
return made.some(([write, tag, notice]) => tag && source === write(tag, notice ?? null));
|
|
300
|
+
}
|
|
246
301
|
function removeTag(source) {
|
|
247
|
-
if (source.includes(OURS))
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
});
|
|
302
|
+
if (source.includes(OURS) && madeByInit(source)) return null;
|
|
303
|
+
return [TAG, NOTICE].reduce(
|
|
304
|
+
(text, pattern) => text.replace(onItsLine(pattern), (match, line, ...rest) => {
|
|
305
|
+
const after = rest.at(-1).slice(rest.at(-2) + match.length);
|
|
306
|
+
return line && !/^[ \t]*(?:\r?\n|$)/.test(after) ? line : "";
|
|
307
|
+
}),
|
|
308
|
+
source
|
|
309
|
+
);
|
|
256
310
|
}
|
|
257
311
|
async function byHand(dir, list2, framework, reason, how) {
|
|
258
312
|
const tagged = [];
|
|
313
|
+
const noticed = [];
|
|
259
314
|
for (const file of list2) {
|
|
260
315
|
const text = await contents(dir, file);
|
|
261
316
|
const key = text === null ? null : tagKey(text);
|
|
262
317
|
if (key) tagged.push({ file, key });
|
|
263
|
-
|
|
264
|
-
|
|
318
|
+
if (text !== null && noticeKey(text)) noticed.push(noticeKey(text));
|
|
319
|
+
}
|
|
320
|
+
return {
|
|
321
|
+
framework,
|
|
322
|
+
heads: [],
|
|
323
|
+
bodies: [],
|
|
324
|
+
creates: [],
|
|
325
|
+
manual: { reason, how },
|
|
326
|
+
tagged,
|
|
327
|
+
noticed,
|
|
328
|
+
key: tagged[0]?.key ?? null
|
|
329
|
+
};
|
|
265
330
|
}
|
|
266
331
|
async function findHeads(dir) {
|
|
267
332
|
const { deps, framework, list: list2, git } = await project(dir);
|
|
@@ -275,6 +340,7 @@ async function findHeads(dir) {
|
|
|
275
340
|
`Add it by hand through ${where}, with this tag's src`
|
|
276
341
|
);
|
|
277
342
|
const heads = [];
|
|
343
|
+
const bodies = [];
|
|
278
344
|
for (const file of list2) {
|
|
279
345
|
const markup = MARKUP.test(file);
|
|
280
346
|
if (!markup && !LAYOUTS.some((layout) => layout.test(file)) || MAIL.test(path.posix.basename(file)))
|
|
@@ -283,6 +349,7 @@ async function findHeads(dir) {
|
|
|
283
349
|
if (text === null) continue;
|
|
284
350
|
const head = markup ? /<\/head\s*>/i.test(text) : /<\/head\s*>|<html\b/i.test(text);
|
|
285
351
|
if (head) heads.push({ file, source: text });
|
|
352
|
+
if (BODY.test(text)) bodies.push({ file, source: text });
|
|
286
353
|
}
|
|
287
354
|
const creates = [];
|
|
288
355
|
if (deps.next && !heads.some(({ file }) => /(?:^|\/)app\//.test(file))) {
|
|
@@ -316,7 +383,7 @@ async function findHeads(dir) {
|
|
|
316
383
|
"Add this line by hand, last in the <head> of the layout or template every page uses"
|
|
317
384
|
);
|
|
318
385
|
const sources = [...heads.map(({ source }) => source), ...creates.map(({ source }) => source ?? "")];
|
|
319
|
-
return { framework, heads, creates, key: sources.map(tagKey).find(Boolean) ?? null };
|
|
386
|
+
return { framework, heads, bodies, creates, key: sources.map(tagKey).find(Boolean) ?? null };
|
|
320
387
|
}
|
|
321
388
|
|
|
322
389
|
// cli/init.mjs
|
|
@@ -345,7 +412,7 @@ async function lookupSite(service, id, fetcher = fetch) {
|
|
|
345
412
|
{ unknownSite: true }
|
|
346
413
|
);
|
|
347
414
|
if (!response.ok) throw new Error(body?.message ?? `Agentface answered HTTP ${response.status}`);
|
|
348
|
-
return { ...body.site, tag: body.tag };
|
|
415
|
+
return { ...body.site, tag: body.tag, notice: body.notice ?? null };
|
|
349
416
|
}
|
|
350
417
|
async function register(service, url, fetcher = fetch) {
|
|
351
418
|
const { response, body } = await reach(
|
|
@@ -356,7 +423,7 @@ async function register(service, url, fetcher = fetch) {
|
|
|
356
423
|
);
|
|
357
424
|
if (!response.ok || !body?.site?.id)
|
|
358
425
|
throw new Error(body?.message ?? `Agentface could not register ${url} (HTTP ${response.status})`);
|
|
359
|
-
return { ...body.site, expiresAt: body.expiresAt, tag: body.tag };
|
|
426
|
+
return { ...body.site, expiresAt: body.expiresAt, tag: body.tag, notice: body.notice ?? null };
|
|
360
427
|
}
|
|
361
428
|
async function confirm(service, id, fetcher = fetch) {
|
|
362
429
|
const { response, body } = await reach(
|
|
@@ -460,7 +527,8 @@ async function siteFor({ id, url, dir, found, service, fetcher, exec, dryRun, lo
|
|
|
460
527
|
return {
|
|
461
528
|
id: "site-new",
|
|
462
529
|
origin,
|
|
463
|
-
tag:
|
|
530
|
+
tag: siteTag(service, "site-new"),
|
|
531
|
+
notice: siteNotice(service, "site-new")
|
|
464
532
|
};
|
|
465
533
|
}
|
|
466
534
|
const site = await register(service, origin + "/", fetcher);
|
|
@@ -475,21 +543,32 @@ function nextSteps(site, service) {
|
|
|
475
543
|
}
|
|
476
544
|
function prBody(site, files2, service = DEFAULT_SERVICE) {
|
|
477
545
|
const src = site.tag.match(/\bsrc="([^"]+)"/)?.[1];
|
|
546
|
+
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
547
|
return `Adds the Agentface tag to ${site.origin}, so AI agents that use the site in a browser get tools made for them.
|
|
479
548
|
|
|
480
|
-
**What it does.**
|
|
549
|
+
**What it does.** It changes ${files2.map((file) => `\`${file}\``).join(", ")}. One \`<script defer>\` 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
550
|
|
|
482
551
|
**Check it.** ${nextSteps(site, service)}
|
|
483
552
|
|
|
484
|
-
**Uninstall.** Revert this pull request, or delete the line.
|
|
553
|
+
**Uninstall.** Revert this pull request, or delete ${site.notice ? "the two lines" : "the line"}.
|
|
485
554
|
`;
|
|
486
555
|
}
|
|
487
|
-
function
|
|
556
|
+
function noticeByHand(site, noticed, log) {
|
|
557
|
+
if (site.notice && !noticed.includes(site.id))
|
|
558
|
+
log(
|
|
559
|
+
`For agents that read pages without running them, also add this line first in the <body> of every page:
|
|
560
|
+
|
|
561
|
+
${site.notice}
|
|
562
|
+
`
|
|
563
|
+
);
|
|
564
|
+
}
|
|
565
|
+
function byHand2({ manual, tagged, noticed }, site, service, dryRun, log) {
|
|
488
566
|
const current = tagged.filter(({ key }) => key === site.id).map(({ file }) => file);
|
|
489
567
|
if (current.length) {
|
|
490
568
|
log(
|
|
491
569
|
`${list(current)} already ${current.length === 1 ? "has" : "have"} the Agentface tag for ${site.id}.`
|
|
492
570
|
);
|
|
571
|
+
noticeByHand(site, noticed, log);
|
|
493
572
|
log(nextSteps(site, service));
|
|
494
573
|
return { site, files: [], written: false };
|
|
495
574
|
}
|
|
@@ -505,6 +584,7 @@ function byHand2({ manual, tagged }, site, service, dryRun, log) {
|
|
|
505
584
|
${site.tag}
|
|
506
585
|
`
|
|
507
586
|
);
|
|
587
|
+
noticeByHand(site, noticed, log);
|
|
508
588
|
log(nextSteps(site, service));
|
|
509
589
|
return { site, files: [], written: false };
|
|
510
590
|
}
|
|
@@ -536,26 +616,33 @@ async function init({
|
|
|
536
616
|
if (pr && !dryRun) await readyForPr(dir, exec);
|
|
537
617
|
const site = await siteFor({ id, url, dir, found, service, fetcher, exec, dryRun, log });
|
|
538
618
|
if (found.manual) return byHand2(found, site, service, dryRun, log);
|
|
539
|
-
const
|
|
619
|
+
const before = /* @__PURE__ */ new Map();
|
|
620
|
+
const after = /* @__PURE__ */ new Map();
|
|
540
621
|
for (const { file, source } of found.heads) {
|
|
541
|
-
|
|
542
|
-
|
|
622
|
+
before.set(file, source);
|
|
623
|
+
after.set(file, addTag(source, site.tag));
|
|
543
624
|
}
|
|
625
|
+
if (site.notice)
|
|
626
|
+
for (const { file, source } of found.bodies) {
|
|
627
|
+
before.set(file, source);
|
|
628
|
+
after.set(file, addNotice(after.get(file) ?? source, site.notice) ?? after.get(file) ?? source);
|
|
629
|
+
}
|
|
544
630
|
for (const { file, content, source } of found.creates) {
|
|
545
|
-
|
|
546
|
-
|
|
631
|
+
before.set(file, source);
|
|
632
|
+
after.set(file, content(site.tag, site.notice));
|
|
547
633
|
}
|
|
634
|
+
const writes = [...after].filter(([file, content]) => content !== before.get(file));
|
|
548
635
|
const files2 = writes.map(([file]) => file);
|
|
549
|
-
const
|
|
636
|
+
const noticed = site.notice && (found.bodies.length || found.creates.length);
|
|
637
|
+
const what = `the Agentface tag${noticed ? " and its notice" : ""}`;
|
|
550
638
|
if (!writes.length) {
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
);
|
|
639
|
+
const tagged = [...found.heads, ...found.creates].map(({ file }) => file);
|
|
640
|
+
log(`${list(tagged)} already ${tagged.length === 1 ? "has" : "have"} the Agentface tag for ${site.id}.`);
|
|
554
641
|
log(nextSteps(site, service));
|
|
555
642
|
return { site, files: files2, written: false };
|
|
556
643
|
}
|
|
557
644
|
if (dryRun) {
|
|
558
|
-
log(`Would add
|
|
645
|
+
log(`Would add ${what} to ${list(files2)} (${found.framework}) for ${site.origin}.`);
|
|
559
646
|
return { site, files: files2, written: false };
|
|
560
647
|
}
|
|
561
648
|
let returnTo;
|
|
@@ -567,7 +654,7 @@ async function init({
|
|
|
567
654
|
await mkdir(path2.dirname(path2.join(dir, file)), { recursive: true });
|
|
568
655
|
await writeFile(path2.join(dir, file), content);
|
|
569
656
|
}
|
|
570
|
-
log(`Added
|
|
657
|
+
log(`Added ${what} to ${list(files2)} (${found.framework}) for ${site.origin}.`);
|
|
571
658
|
if (!pr) {
|
|
572
659
|
log(nextSteps(site, service));
|
|
573
660
|
return { site, files: files2, written: true };
|