agentface 0.3.0 → 0.4.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.
Files changed (2) hide show
  1. package/dist/agentface.mjs +111 -22
  2. package/package.json +1 -1
@@ -5,7 +5,7 @@ import { parseArgs } from "node:util";
5
5
 
6
6
  // cli/init.mjs
7
7
  import { execFile as execFile2 } from "node:child_process";
8
- import { mkdir, readFile as readFile2, writeFile } from "node:fs/promises";
8
+ import { mkdir, readFile as readFile2, rm, writeFile } from "node:fs/promises";
9
9
  import path2 from "node:path";
10
10
 
11
11
  // worker/origin.js
@@ -222,6 +222,38 @@ exports.onRenderBody = ({ setHeadComponents }) => {
222
222
  };
223
223
  `;
224
224
  };
225
+ async function project(dir) {
226
+ const pkg = JSON.parse(await read(path.join(dir, "package.json")) ?? "null");
227
+ const deps = { ...pkg?.dependencies, ...pkg?.devDependencies };
228
+ const generator = await siteGenerator(dir);
229
+ const skip = new Set(SKIP);
230
+ if (generator === "Hugo") for (const name of ["public", "resources", "themes"]) skip.add(name);
231
+ if (deps.gatsby) skip.add("public");
232
+ const framework = FRAMEWORKS.find(([dependency]) => deps[dependency])?.[1] ?? generator ?? (pkg ? "JavaScript" : "HTML");
233
+ const { files: list2, git } = await files(dir, skip);
234
+ return { deps, framework, list: list2, git };
235
+ }
236
+ async function findTags(dir) {
237
+ const { list: list2 } = await project(dir);
238
+ const tagged = [];
239
+ for (const file of list2) {
240
+ const source = await contents(dir, file);
241
+ const key = source === null ? null : tagKey(source);
242
+ if (key) tagged.push({ file, source, key });
243
+ }
244
+ return tagged;
245
+ }
246
+ function removeTag(source) {
247
+ if (source.includes(OURS)) {
248
+ const src = source.match(/['"](https?:\/\/[^'"\s]+)['"]/)?.[1];
249
+ const tag = `<script async src="${src}"></script>`;
250
+ if (src && (source === NEXT_DOCUMENT(tag) || source === GATSBY_SSR(tag))) return null;
251
+ }
252
+ return source.replace(new RegExp(`(\\r?\\n[ \\t]*)?${TAG.source}`, "gi"), (match, line, ...rest) => {
253
+ const after = rest.at(-1).slice(rest.at(-2) + match.length);
254
+ return line && !/^[ \t]*(?:\r?\n|$)/.test(after) ? line : "";
255
+ });
256
+ }
225
257
  async function byHand(dir, list2, framework, reason, how) {
226
258
  const tagged = [];
227
259
  for (const file of list2) {
@@ -232,14 +264,7 @@ async function byHand(dir, list2, framework, reason, how) {
232
264
  return { framework, heads: [], creates: [], manual: { reason, how }, tagged, key: tagged[0]?.key ?? null };
233
265
  }
234
266
  async function findHeads(dir) {
235
- const pkg = JSON.parse(await read(path.join(dir, "package.json")) ?? "null");
236
- const deps = { ...pkg?.dependencies, ...pkg?.devDependencies };
237
- const generator = await siteGenerator(dir);
238
- const skip = new Set(SKIP);
239
- if (generator === "Hugo") for (const name of ["public", "resources", "themes"]) skip.add(name);
240
- if (deps.gatsby) skip.add("public");
241
- const framework = FRAMEWORKS.find(([dependency]) => deps[dependency])?.[1] ?? generator ?? (pkg ? "JavaScript" : "HTML");
242
- const { files: list2, git } = await files(dir, skip);
267
+ const { deps, framework, list: list2, git } = await project(dir);
243
268
  for (const [dependency, [name, where]] of Object.entries(CONFIG_HEAD))
244
269
  if (deps[dependency])
245
270
  return byHand(
@@ -381,26 +406,47 @@ function run(command, args, { cwd }) {
381
406
  });
382
407
  });
383
408
  }
409
+ var bare = (origin) => origin.replace(/^https:\/\/www\./, "https://");
410
+ var sameSite = (a, b) => bare(a) === bare(b);
384
411
  async function siteFor({ id, url, dir, found, service, fetcher, exec, dryRun, log }) {
385
- if (id) return lookupSite(service, id, fetcher);
412
+ let origin;
413
+ if (url) {
414
+ try {
415
+ origin = registrableOrigin(url);
416
+ } catch (error) {
417
+ throw new Error(`Can't register ${url}. ${error.message}.`, { cause: error });
418
+ }
419
+ }
420
+ if (id) {
421
+ const site2 = await lookupSite(service, id, fetcher);
422
+ if (origin && !sameSite(site2.origin, origin))
423
+ throw new Error(
424
+ `${id} is the site for ${site2.origin}, not ${origin}. Leave out the key to register ${origin}.`
425
+ );
426
+ return site2;
427
+ }
386
428
  if (found.key) {
387
429
  try {
388
- return await lookupSite(service, found.key, fetcher);
430
+ const site2 = await lookupSite(service, found.key, fetcher);
431
+ if (!origin || sameSite(site2.origin, origin)) return site2;
432
+ log(
433
+ `This project's tag names ${found.key}, the site for ${site2.origin}. Registering ${origin} instead, as --url asks.`
434
+ );
389
435
  } catch (error) {
390
436
  if (!error.unknownSite) throw error;
391
437
  log(
392
- `Agentface no longer has ${found.key}, the site this project's tag names (an unconfirmed site is deleted after a day). Registering it again.`
438
+ `Agentface no longer has ${found.key}, the site this project's tag names (an unconfirmed site is deleted after a week). Registering it again.`
393
439
  );
394
440
  }
395
441
  }
396
- if (!url) {
442
+ if (!origin) {
397
443
  const address = await siteUrl(dir, exec);
398
444
  if (!address)
399
445
  throw new Error(
400
446
  "Which site is this? Run this again with its address: npx agentface init --url https://example.com"
401
447
  );
402
448
  try {
403
- registrableOrigin(address.url);
449
+ origin = registrableOrigin(address.url);
404
450
  } catch (error) {
405
451
  throw new Error(
406
452
  `This project's address, ${address.url} (from ${address.from}), can't be registered. ${error.message}. If the site is served from another address, pass it with --url.`,
@@ -408,13 +454,6 @@ async function siteFor({ id, url, dir, found, service, fetcher, exec, dryRun, lo
408
454
  );
409
455
  }
410
456
  log(`Using ${address.url} (from ${address.from}). For another address, pass --url.`);
411
- url = address.url;
412
- }
413
- let origin;
414
- try {
415
- origin = registrableOrigin(url);
416
- } catch (error) {
417
- throw new Error(`Can't register ${url}. ${error.message}.`, { cause: error });
418
457
  }
419
458
  if (dryRun) {
420
459
  log(`Would register ${origin} with Agentface.`);
@@ -595,6 +634,50 @@ async function check({
595
634
  log(`Until it's confirmed, Agentface deletes ${id} at ${when(site.expiresAt)}.`);
596
635
  return 1;
597
636
  }
637
+ async function uninstall({
638
+ dir = process.cwd(),
639
+ service = DEFAULT_SERVICE,
640
+ dryRun = false,
641
+ fetch: fetcher = fetch,
642
+ log = console.log
643
+ }) {
644
+ const tagged = await findTags(dir);
645
+ if (!tagged.length) {
646
+ log("No Agentface tag in this project.");
647
+ return { files: [], deleted: [], byHand: [] };
648
+ }
649
+ const edits = [];
650
+ const deleted = [];
651
+ const byHand3 = [];
652
+ for (const { file, source } of tagged) {
653
+ const content = removeTag(source);
654
+ if (content === null) deleted.push(file);
655
+ else if (tagKey(content)) byHand3.push(file);
656
+ else edits.push([file, content]);
657
+ }
658
+ const files2 = edits.map(([file]) => file);
659
+ if (!dryRun) {
660
+ for (const [file, content] of edits) await writeFile(path2.join(dir, file), content);
661
+ for (const file of deleted) await rm(path2.join(dir, file));
662
+ }
663
+ const would = (done, undone) => dryRun ? undone : done;
664
+ if (files2.length) log(`${would("Removed", "Would remove")} the Agentface tag from ${list(files2)}.`);
665
+ if (deleted.length)
666
+ log(`${would("Deleted", "Would delete")} ${list(deleted)}, which init made for the tag.`);
667
+ if (byHand3.length)
668
+ log(
669
+ `${list(byHand3)} ${byHand3.length === 1 ? "loads" : "load"} the Agentface script some other way, such as from config: remove it there by hand.`
670
+ );
671
+ if (dryRun) return { files: files2, deleted, byHand: byHand3 };
672
+ log("Deploy it as usual: once the tag is off the site, nothing loads Agentface.");
673
+ for (const key of new Set(tagged.map(({ key: key2 }) => key2))) {
674
+ const site = await lookupSite(service, key, fetcher).catch(() => null);
675
+ if (site?.expiresAt)
676
+ log(`${key} was never confirmed, so Agentface deletes it at ${when(site.expiresAt)}.`);
677
+ else if (site) log(`${key} stays registered for ${site.origin}; \`npx agentface init\` puts it back.`);
678
+ }
679
+ return { files: files2, deleted, byHand: byHand3 };
680
+ }
598
681
 
599
682
  // cli/agentface.mjs
600
683
  var HELP = `Usage:
@@ -602,11 +685,12 @@ var HELP = `Usage:
602
685
  folder. Without a site key, registers the site first.
603
686
  agentface check Check that the deployed homepage has the tag. The first
604
687
  time, that confirms the site is yours.
688
+ agentface uninstall Remove the Agentface tag from this folder's pages.
605
689
 
606
690
  Options:
607
691
  --url The site's address, like https://example.com (init finds it when it can)
608
692
  --site check: the site key (check finds it in the project when it can)
609
- --pr Commit the change on a new branch and open a pull request (needs git and gh)
693
+ --pr init: commit the change on a new branch and open a pull request (needs git and gh)
610
694
  --dry-run Show what would change, without registering or writing anything
611
695
  --service Agentface service URL (default ${DEFAULT_SERVICE})`;
612
696
  async function main(argv, { log = console.log, dir = process.cwd(), fetch: fetcher = fetch } = {}) {
@@ -641,6 +725,11 @@ async function main(argv, { log = console.log, dir = process.cwd(), fetch: fetch
641
725
  });
642
726
  return 0;
643
727
  }
728
+ if (command === "uninstall") {
729
+ if (values.pr) throw new Error("--pr works with init only: commit the change yourself.");
730
+ await uninstall({ dir, service, dryRun: values["dry-run"], fetch: fetcher, log });
731
+ return 0;
732
+ }
644
733
  if (command === "check") return check({ site: values.site ?? arg, dir, service, fetch: fetcher, log });
645
734
  throw new Error(`Unknown command "${command}".
646
735
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agentface",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "Add the Agentface tag to a website's pages, so AI agents get tools made for it.",
5
5
  "bin": {
6
6
  "agentface": "dist/agentface.mjs"