constile-agent-sdk 0.1.0 → 0.1.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/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # constile-agent-sdk
2
2
 
3
3
  For AI agents that fetch web pages for a person. `sponsoredFetch()` works like
4
- `fetch()`, and when a site answers with a Constile offer, it applies the
5
- person's saved choice: accept the offer, ask them first, or decline.
4
+ `fetch()`, and when a site answers with a Constile ad, it applies the
5
+ person's saved choice: accept the ad, ask them first, or decline.
6
6
 
7
7
  Constile lets publishers keep being paid when assistants read their pages.
8
8
  Instead of the page's ads, the person sees one labeled sponsored message in your
@@ -31,7 +31,7 @@ const result = await sponsoredFetch("https://example.com/articles/espresso-grind
31
31
  if (result.kind === "content") {
32
32
  const page = await result.response.text();
33
33
  if (result.sponsorship) {
34
- // An accepted offer: show this message to the person exactly, labeled as advertising.
34
+ // An accepted ad: show this message to the person exactly, labeled as advertising.
35
35
  show(result.sponsorship.message);
36
36
  await result.sponsorship.reportPrepared(); // it's in the answer you're about to send
37
37
  // ...and once the person has actually seen it:
@@ -44,24 +44,27 @@ if (result.kind === "content") {
44
44
  // accepted is a "content" result, as above.
45
45
  }
46
46
  } else {
47
- // "declined": the person chose to decline offers. Answer without this page.
47
+ // "declined": the person chose to decline Constile ads. Answer without this page.
48
48
  }
49
49
  ```
50
50
 
51
51
  Ask the person once and save their answer, so your agent doesn't have to ask on
52
52
  every page.
53
53
 
54
- Some offers can be accepted only after the person has seen the message
54
+ Some ads can be accepted only after the person has seen the message
55
55
  (`result.deliverFirst` is `true`). Show it first, then call
56
56
  `result.accept("deliver_now")`.
57
57
 
58
58
  ## API
59
59
 
60
+ In the API, the ad a site serves is an offer (`MachineOffer`).
61
+
60
62
  | Export | What it is |
61
63
  |---|---|
62
- | `sponsoredFetch(url, options)` | Fetches the page and handles an offer. `options.policy` is `"accept"`, `"ask"`, or `"decline"`; `options.userAgent` identifies your agent; `options.fetch` replaces the global `fetch`. |
64
+ | `sponsoredFetch(url, options)` | Fetches the page and handles a Constile ad. `options.policy` is `"accept"`, `"ask"`, or `"decline"`; `options.userAgent` identifies your agent; `options.fetch` replaces the global `fetch`. An ad is used only when its links are on the page's own site or Constile's network (`options.networkOrigins`, default `https://constile.app`). |
63
65
  | `consentQuestion(offer)` | One or two sentences to ask the person. |
64
- | `parseOfferPage(text, url)` | Reads an offer from a page's text, for pages fetched some other way. |
66
+ | `parseOfferPage(text, url)` | Reads an ad from a page's text, for pages fetched some other way. |
67
+ | `offerLinksAllowed(offer, url, networks?)` | Whether every link in an ad is on the page's own site or the network. Check it before you follow an ad you read with `parseOfferPage`. |
65
68
  | `MachineOffer`, `SponsoredResult`, `Sponsorship`, `ConsentPolicy` | The types. |
66
69
 
67
70
  ## Support
package/dist/index.d.ts CHANGED
@@ -1,18 +1,12 @@
1
1
  /**
2
- * Client for agents that fetch pages for a person. sponsoredFetch() works like
3
- * fetch(), and when a site answers with a Constile offer it applies
4
- * the person's standing choice:
2
+ * Client for agents that fetch pages for a person. sponsoredFetch() works like fetch(), and when a site answers
3
+ * with a Constile ad it applies the person's standing choice (ask once, store it, reuse it):
5
4
  *
6
- * "accept" take the offer with deferred delivery and return the article,
7
- * plus the sponsored message the agent must show the person.
8
- * An offer without deferred delivery (actions.defer missing) can't
9
- * be taken before the person sees the message, so it comes back as
10
- * an offer: show the message, then call accept("deliver_now").
11
- * "ask" return the offer so the agent can ask the person first
12
- * "decline" return the offer as declined; no link is requested
13
- *
14
- * Ask the person once, store their choice, and reuse it, so the agent doesn't
15
- * have to ask on every page.
5
+ * "accept" take the ad with deferred delivery and return the article plus the sponsored message to show.
6
+ * An ad without deferred delivery (actions.defer missing) comes back as kind "offer" instead: show
7
+ * the message, then call accept("deliver_now").
8
+ * "ask" return the ad (kind "offer") so the agent can ask the person first
9
+ * "decline" return it as declined; no link is requested
16
10
  */
17
11
  type MachineOffer = {
18
12
  constile: "1";
@@ -54,13 +48,25 @@ export type SponsoredResult = {
54
48
  offer: MachineOffer;
55
49
  };
56
50
  export type SponsoredFetchOptions = {
51
+ /** The person's standing choice about offers. */
57
52
  policy: ConsentPolicy;
58
53
  /** Identify your agent with a stable product token, for example "ExampleAssistant-User/1.0". */
59
54
  userAgent: string;
55
+ /** Fetches in place of the global fetch. */
60
56
  fetch?: (req: Request) => Promise<Response>;
57
+ /**
58
+ * Where an offer's links may be besides the page's own site. Default: Constile's network. An offer with a link
59
+ * anywhere else is ignored, so a page can't point the agent's requests at other hosts, such as its local network.
60
+ */
61
+ networkOrigins?: string[];
61
62
  };
63
+ /** Constile's network, where offers' accept and report links can be. */
64
+ export declare const CONSTILE_NETWORK = "https://constile.app";
65
+ /** True when each of the offer's links is on the page's own site or one of `networks`. */
66
+ export declare function offerLinksAllowed(offer: MachineOffer, pageUrl: string, networks?: string[]): boolean;
62
67
  /** Reads an offer from a page without the Constile-Offer header (another implementation, or a stripped proxy). */
63
68
  export declare function parseOfferPage(text: string, pageUrl: string): MachineOffer | null;
69
+ /** Fetches a page and applies `policy` if the site answers with an offer. */
64
70
  export declare function sponsoredFetch(url: string, opts: SponsoredFetchOptions): Promise<SponsoredResult>;
65
71
  /** One or two sentences an agent can use to ask the person. */
66
72
  export declare function consentQuestion(offer: MachineOffer): string;
package/dist/index.js CHANGED
@@ -1,6 +1,23 @@
1
- // constile-agent-sdk 0.1.0
1
+ // constile-agent-sdk 0.1.1
2
2
 
3
3
  // packages/agent-sdk/src/index.ts
4
+ var CONSTILE_NETWORK = "https://constile.app";
5
+ function linkAllowed(link, pageOrigin, allowed) {
6
+ if (typeof link !== "string") return false;
7
+ try {
8
+ const u = new URL(link);
9
+ return allowed.has(u.origin) && (u.protocol === "https:" || u.origin === pageOrigin);
10
+ } catch {
11
+ return false;
12
+ }
13
+ }
14
+ function offerLinksAllowed(offer, pageUrl, networks = [CONSTILE_NETWORK]) {
15
+ const page = new URL(pageUrl).origin;
16
+ const allowed = /* @__PURE__ */ new Set([page, ...networks.map((n) => new URL(n).origin)]);
17
+ const a = offer?.actions;
18
+ if (!a || typeof a.deliver_now !== "string") return false;
19
+ return [a.deliver_now, a.defer, a.prepared, a.content].every((l) => l === void 0 || linkAllowed(l, page, allowed));
20
+ }
4
21
  var OFFER_MARKER = "SPONSORED ACCESS OFFER";
5
22
  function parseOfferPage(text, pageUrl) {
6
23
  if (!text.includes(OFFER_MARKER)) return null;
@@ -31,33 +48,36 @@ function parseOfferPage(text, pageUrl) {
31
48
  function decodeEntities(s) {
32
49
  return s.replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&quot;/g, '"').replace(/&#39;/g, "'").replace(/&amp;/g, "&");
33
50
  }
34
- async function getOffer(res, url, doFetch, ua) {
51
+ async function getOffer(res, url, doFetch, ua, networks) {
52
+ const page = new URL(url).origin;
35
53
  const header = res.headers.get("constile-offer");
36
- if (header) {
37
- const r = await doFetch(new Request(header, { headers: { "user-agent": ua, accept: "application/json" } }));
38
- if (r.ok) {
54
+ if (header && linkAllowed(new URL(header, url).href, page, /* @__PURE__ */ new Set([page]))) {
55
+ const r = await doFetch(new Request(new URL(header, url), { headers: { "user-agent": ua, accept: "application/json" } }));
56
+ const offer2 = r.ok ? await r.json().catch(() => null) : null;
57
+ if (offer2 && offerLinksAllowed(offer2, url, networks)) {
39
58
  await res.body?.cancel();
40
- return { offer: await r.json(), body: null };
59
+ return { offer: offer2, body: null };
41
60
  }
42
61
  }
43
62
  const type = res.headers.get("content-type") ?? "";
44
63
  if (!/text\/(html|plain)/.test(type)) return { offer: null, body: null };
45
64
  const body = await res.text();
46
- return { offer: parseOfferPage(body, url), body };
65
+ const offer = parseOfferPage(body, url);
66
+ return { offer: offer && offerLinksAllowed(offer, url, networks) ? offer : null, body };
47
67
  }
48
68
  async function sponsoredFetch(url, opts) {
49
69
  const doFetch = opts.fetch ?? ((r) => fetch(r));
50
70
  const ua = opts.userAgent;
51
71
  const res = await doFetch(new Request(url, { headers: { "user-agent": ua, accept: "text/html,text/plain;q=0.9,*/*;q=0.5" } }));
52
72
  if (res.status !== 200) return { kind: "content", response: res, sponsorship: null };
53
- const { offer, body } = await getOffer(res, url, doFetch, ua);
73
+ const { offer, body } = await getOffer(res, res.url || url, doFetch, ua, opts.networkOrigins ?? [CONSTILE_NETWORK]);
54
74
  if (!offer) {
55
75
  const response = body === null ? res : new Response(body, { status: res.status, headers: res.headers });
56
76
  return { kind: "content", response, sponsorship: null };
57
77
  }
58
78
  const deliverFirst = !offer.actions.defer;
59
79
  const accept = async (mode = deliverFirst ? "deliver_now" : "defer") => {
60
- if (mode === "defer" && !offer.actions.defer) throw new Error("This offer can't be deferred: show the message, then accept with deliver_now.");
80
+ if (mode === "defer" && !offer.actions.defer) throw new Error("This ad can't be deferred: show the message, then accept with deliver_now.");
61
81
  const action = mode === "defer" ? offer.actions.defer : offer.actions.deliver_now;
62
82
  const r = await doFetch(new Request(action, { method: "POST", headers: { "user-agent": ua } }));
63
83
  const report = (u) => async () => (await doFetch(new Request(u, { method: "POST", headers: { "user-agent": ua } }))).ok;
@@ -78,10 +98,12 @@ async function sponsoredFetch(url, opts) {
78
98
  }
79
99
  function consentQuestion(offer) {
80
100
  const title = offer.resource.title ? `"${offer.resource.title}"` : "this article";
81
- return `${title} is free to read if I include one clearly labeled sponsored line in my answer. It costs you nothing and shares nothing about you. Should I accept, and should I do that automatically from now on?`;
101
+ return `${title} is free to read if I include one clearly labeled sponsored message in my answer. It costs you nothing and shares nothing about you. Should I accept, and should I do that automatically from now on?`;
82
102
  }
83
103
  export {
104
+ CONSTILE_NETWORK,
84
105
  consentQuestion,
106
+ offerLinksAllowed,
85
107
  parseOfferPage,
86
108
  sponsoredFetch
87
109
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "constile-agent-sdk",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "For AI agents that fetch pages for a person: handles Constile's sponsored access, with the person's saved choice.",
5
5
  "license": "MIT",
6
6
  "author": "CCC2C Inc. <hello@constile.app>",