agentface 0.5.0 → 0.5.2

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 +64 -26
  2. package/package.json +6 -1
@@ -1,14 +1,14 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- // cli/agentface.mjs
3
+ // cli/agentface.mts
4
4
  import { parseArgs } from "node:util";
5
5
 
6
- // cli/init.mjs
6
+ // cli/init.mts
7
7
  import { execFile as execFile2 } from "node:child_process";
8
8
  import { mkdir, readFile as readFile2, rm, writeFile } from "node:fs/promises";
9
9
  import path2 from "node:path";
10
10
 
11
- // worker/origin.js
11
+ // worker/origin.ts
12
12
  var PRIVATE_NAMES = /(?:^|\.)(?:localhost|local|internal|lan|intranet|corp|home|arpa)$/;
13
13
  function registrableOrigin(input) {
14
14
  const text = String(input ?? "").trim();
@@ -31,19 +31,33 @@ function registrableOrigin(input) {
31
31
  return origin;
32
32
  }
33
33
 
34
- // worker/tag.js
34
+ // worker/tag.ts
35
35
  var toolListUrl = (serviceOrigin, id) => `${serviceOrigin}/s/${id}.md`;
36
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>`;
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
38
  }
39
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>`;
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
41
  }
42
42
 
43
- // cli/heads.mjs
43
+ // cli/heads.mts
44
44
  import { execFile } from "node:child_process";
45
45
  import { readdir, readFile, stat } from "node:fs/promises";
46
46
  import path from "node:path";
47
+
48
+ // worker/head.ts
49
+ var ATTRIBUTES = String.raw`(?:\{[^{}]*(?:\{[^{}]*\}[^{}]*)*\}|"[^"]*"|'[^']*'|[^>"'{/]|\/(?!>))*`;
50
+ var HEAD_END = new RegExp(
51
+ String.raw`<!--[\s\S]*?(?:-->|$)|<(script|style|template|textarea|title|noscript)\b${ATTRIBUTES}(?:\/>|>[\s\S]*?(?:<\/\1\s*>|$))|<\/head\s*>|<body[\s/>]`,
52
+ "gi"
53
+ );
54
+ function headEnd(html) {
55
+ HEAD_END.lastIndex = 0;
56
+ for (let m; m = HEAD_END.exec(html); ) if (!m[1] && !m[0].startsWith("<!--")) return m.index;
57
+ return -1;
58
+ }
59
+
60
+ // cli/heads.mts
47
61
  var EXTENSIONS = [
48
62
  "html",
49
63
  "htm",
@@ -122,7 +136,11 @@ var exists = (file) => stat(file).then(
122
136
  () => false
123
137
  );
124
138
  var KEY = /\/s\/(site-[a-z0-9-]+)\.js(?=[?#"'`\s]|$)/;
125
- var TAG = new RegExp(`<script\\b[^>]*?${KEY.source}[^>]*?(?:\\/>|>\\s*<\\/script\\s*>)`, "gi");
139
+ var ATTRIBUTES2 = String.raw`(?:\{[^{}]*(?:\{[^{}]*\}[^{}]*)*\}|[^>])*?`;
140
+ var TAG = new RegExp(
141
+ `<script\\b${ATTRIBUTES2}${KEY.source}${ATTRIBUTES2}(?:\\/>|>\\s*<\\/script\\s*>)`,
142
+ "gi"
143
+ );
126
144
  var NOTICE = /<noscript\b[^>]*>[^<]*\/s\/(site-[a-z0-9-]+)\.md[^<]*<\/noscript\s*>/i;
127
145
  var BODY = /<body(?=[\s>/])(?:<\?[\s\S]*?\?>|<%[\s\S]*?%>|\{[^{}]*(?:\{[^{}]*\}[^{}]*)*\}|"[^"]*"|'[^']*'|[^>])*>/i;
128
146
  var onItsLine = (pattern) => new RegExp(`(\\r?\\n[ \\t]*)?${pattern.source}`, "gi");
@@ -146,7 +164,8 @@ function addTag(source, tag) {
146
164
  });
147
165
  if (!first) return replaced;
148
166
  const eol = source.includes("\r\n") ? "\r\n" : "\n";
149
- const close = source.search(/<\/head\s*>/i);
167
+ const at = headEnd(source);
168
+ const close = at !== -1 && /^<\/head\s*>/i.test(source.slice(at)) ? at : -1;
150
169
  if (close !== -1) {
151
170
  const lineStart = source.lastIndexOf("\n", close - 1) + 1;
152
171
  const before = source.slice(lineStart, close);
@@ -246,9 +265,10 @@ ${notice ? ` ${notice}
246
265
  `;
247
266
  var GATSBY_SSR = (tag, notice) => {
248
267
  const src = tag.match(/\bsrc="([^"]+)"/)[1];
249
- const about = tag.match(/\bdata-ai-agents="([^"]+)"/)?.[1];
268
+ const [, attribute, about] = tag.match(/\b(data-[a-z-]+)="([^"]+)"/) ?? [];
250
269
  const text = notice?.match(/>([^<]*)</)[1];
251
- const props = `key: 'agentface', async: true, src: '${src}'${about ? `, 'data-ai-agents': '${about}'` : ""}`;
270
+ const load = tag.match(/^<script (async|defer)\b/)?.[1] ?? "defer";
271
+ const props = `key: 'agentface', ${load}: true, src: '${src}'${about ? `, '${attribute}': '${about}'` : ""}`;
252
272
  return `// ${OURS}: puts the Agentface tag in every page's <head>.
253
273
  const React = require('react');
254
274
 
@@ -282,16 +302,20 @@ async function findTags(dir) {
282
302
  return tagged;
283
303
  }
284
304
  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;
305
+ const gatsby = (pattern) => source.match(pattern)?.[1];
306
+ const load = gatsby(/key: 'agentface', (async|defer): true/);
307
+ const src = gatsby(/\bsrc: '([^']+)'/);
308
+ const [, attribute, about] = source.match(/'(data-[a-z-]+)': '([^']+)'/) ?? [];
309
+ const text = gatsby(/createElement\('noscript', \{ key: 'agentface' \}, '([^']*)'\)/);
288
310
  const made = [
289
- [`<script async src="${src}"></script>`, null],
290
- [siteTag(origin, id), siteNotice(origin, id)]
311
+ [NEXT_DOCUMENT, source.match(TAG)?.[0], source.match(NOTICE)?.[0]],
312
+ [
313
+ GATSBY_SSR,
314
+ load && src && `<script ${load} src="${src}"${about ? ` ${attribute}="${about}"` : ""}></script>`,
315
+ text && `<noscript>${text}</noscript>`
316
+ ]
291
317
  ];
292
- return made.some(
293
- ([tag, notice]) => [NEXT_DOCUMENT, GATSBY_SSR].some((write) => source === write(tag, notice))
294
- );
318
+ return made.some(([write, tag, notice]) => tag && source === write(tag, notice ?? null));
295
319
  }
296
320
  function removeTag(source) {
297
321
  if (source.includes(OURS) && madeByInit(source)) return null;
@@ -381,7 +405,7 @@ async function findHeads(dir) {
381
405
  return { framework, heads, bodies, creates, key: sources.map(tagKey).find(Boolean) ?? null };
382
406
  }
383
407
 
384
- // cli/init.mjs
408
+ // cli/init.mts
385
409
  var DEFAULT_SERVICE = "https://cdn.agentfacehq.com";
386
410
  var BRANCH = "agentface";
387
411
  var readJson = async (file) => JSON.parse(await readFile2(file, "utf8").catch(() => "null"));
@@ -463,14 +487,26 @@ function run(command, args, { cwd }) {
463
487
  return new Promise((resolve, reject) => {
464
488
  execFile2(command, args, { cwd }, (error, stdout, stderr) => {
465
489
  if (error)
466
- reject(new Error(`${command} ${args.join(" ")} failed: ${(stderr || error.message).trim()}`));
490
+ reject(
491
+ new Error(`${command} ${args.join(" ")} failed: ${(stderr || error.message).trim()}`)
492
+ );
467
493
  else resolve(stdout.trim());
468
494
  });
469
495
  });
470
496
  }
471
497
  var bare = (origin) => origin.replace(/^https:\/\/www\./, "https://");
472
498
  var sameSite = (a, b) => bare(a) === bare(b);
473
- async function siteFor({ id, url, dir, found, service, fetcher, exec, dryRun, log }) {
499
+ async function siteFor({
500
+ id,
501
+ url,
502
+ dir,
503
+ found,
504
+ service,
505
+ fetcher,
506
+ exec,
507
+ dryRun,
508
+ log
509
+ }) {
474
510
  let origin;
475
511
  if (url) {
476
512
  try {
@@ -534,20 +570,21 @@ var serviceFlag = (service) => service === DEFAULT_SERVICE ? "" : ` --service ${
534
570
  var list = (files2) => files2.length <= 3 ? files2.join(", ") : `${files2.slice(0, 3).join(", ")} and ${files2.length - 3} more`;
535
571
  function nextSteps(site, service) {
536
572
  const check2 = `npx agentface check${serviceFlag(service)}`;
537
- return site.expiresAt ? `Deploy it as usual, then run \`${check2}\` here: once ${site.origin}'s homepage has the tag, that confirms the site is yours. Agentface also confirms it by itself when the site's first events arrive, but a quiet site may send none in time: until it's confirmed, Agentface deletes ${site.id} at ${when(site.expiresAt)}. If that happens first, run \`npx agentface init\` again.` : `Deploy it as usual. \`${check2}\` shows whether ${site.origin}'s homepage has the tag.`;
573
+ return site.expiresAt ? `Deploy it as usual, then run \`${check2}\` here: once ${site.origin}'s homepage has the tag, in its HTML or in the Google Tag Manager container it loads, that confirms the site is yours. Agentface also confirms it by itself when the site's first events arrive, but a quiet site may send none in time: until it's confirmed, Agentface deletes ${site.id} at ${when(site.expiresAt)}. If that happens first, run \`npx agentface init\` again.` : `Deploy it as usual. \`${check2}\` shows whether ${site.origin}'s homepage has the tag.`;
538
574
  }
539
575
  function prBody(site, files2, service = DEFAULT_SERVICE) {
540
576
  const src = site.tag.match(/\bsrc="([^"]+)"/)?.[1];
541
577
  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.` : "";
542
578
  return `Adds the Agentface tag to ${site.origin}, so AI agents that use the site in a browser get tools made for them.
543
579
 
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.
580
+ **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.
545
581
 
546
582
  **Check it.** ${nextSteps(site, service)}
547
583
 
548
584
  **Uninstall.** Revert this pull request, or delete ${site.notice ? "the two lines" : "the line"}.
549
585
  `;
550
586
  }
587
+ var GTM_HOW = "Can't change the site's code? Add the same line in Google Tag Manager instead: Tags > New > Custom HTML, paste it, set the trigger to Initialization - All Pages, then Submit and Publish the container.";
551
588
  function noticeByHand(site, noticed, log) {
552
589
  if (site.notice && !noticed.includes(site.id))
553
590
  log(
@@ -579,6 +616,7 @@ function byHand2({ manual, tagged, noticed }, site, service, dryRun, log) {
579
616
  ${site.tag}
580
617
  `
581
618
  );
619
+ if (!old.length) log(GTM_HOW);
582
620
  noticeByHand(site, noticed, log);
583
621
  log(nextSteps(site, service));
584
622
  return { site, files: [], written: false };
@@ -761,7 +799,7 @@ async function uninstall({
761
799
  return { files: files2, deleted, byHand: byHand3 };
762
800
  }
763
801
 
764
- // cli/agentface.mjs
802
+ // cli/agentface.mts
765
803
  var HELP = `Usage:
766
804
  agentface init [<site-key>] Add the Agentface tag to the pages of the site in this
767
805
  folder. Without a site key, registers the site first.
@@ -818,7 +856,7 @@ async function main(argv, { log = console.log, dir = process.cwd(), fetch: fetch
818
856
  ${HELP}`);
819
857
  }
820
858
 
821
- // cli/bin.mjs
859
+ // cli/bin.mts
822
860
  main(process.argv.slice(2)).then(
823
861
  (code) => {
824
862
  process.exitCode = code;
package/package.json CHANGED
@@ -1,7 +1,12 @@
1
1
  {
2
2
  "name": "agentface",
3
- "version": "0.5.0",
3
+ "version": "0.5.2",
4
4
  "description": "Add the Agentface tag to a website's pages, so AI agents get tools made for it.",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/agentfacehq/agentface-app.git",
8
+ "directory": "adapter/cli"
9
+ },
5
10
  "bin": {
6
11
  "agentface": "dist/agentface.mjs"
7
12
  },