constile-agent-sdk 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +73 -2
- package/dist/index.d.ts +66 -0
- package/dist/index.js +87 -0
- package/package.json +32 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 CCC2C Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,74 @@
|
|
|
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.
|
|
6
|
+
|
|
7
|
+
Constile lets publishers keep being paid when assistants read their pages.
|
|
8
|
+
Instead of the page's ads, the person sees one labeled sponsored message in your
|
|
9
|
+
agent's answer, and the advertiser pays the publisher. It costs the person
|
|
10
|
+
nothing and shares nothing about them.
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
npm install constile-agent-sdk
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
It has no dependencies and runs anywhere with `fetch`: Node.js 18 or later,
|
|
19
|
+
Deno, Bun, Cloudflare Workers, and browsers' service workers.
|
|
20
|
+
|
|
21
|
+
## Use it
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { consentQuestion, sponsoredFetch } from "constile-agent-sdk";
|
|
25
|
+
|
|
26
|
+
const result = await sponsoredFetch("https://example.com/articles/espresso-grinders", {
|
|
27
|
+
policy: "ask", // or "accept" / "decline", from the person's saved choice
|
|
28
|
+
userAgent: "ExampleAssistant-User/1.0", // a stable product token for your agent
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
if (result.kind === "content") {
|
|
32
|
+
const page = await result.response.text();
|
|
33
|
+
if (result.sponsorship) {
|
|
34
|
+
// An accepted offer: show this message to the person exactly, labeled as advertising.
|
|
35
|
+
show(result.sponsorship.message);
|
|
36
|
+
await result.sponsorship.reportPrepared(); // it's in the answer you're about to send
|
|
37
|
+
// ...and once the person has actually seen it:
|
|
38
|
+
await result.sponsorship.reportDelivered();
|
|
39
|
+
}
|
|
40
|
+
} else if (result.kind === "offer") {
|
|
41
|
+
// Ask the person first. consentQuestion() gives you the words.
|
|
42
|
+
if (await ask(consentQuestion(result.offer))) {
|
|
43
|
+
const accepted = await result.accept();
|
|
44
|
+
// accepted is a "content" result, as above.
|
|
45
|
+
}
|
|
46
|
+
} else {
|
|
47
|
+
// "declined": the person chose to decline offers. Answer without this page.
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Ask the person once and save their answer, so your agent doesn't have to ask on
|
|
52
|
+
every page.
|
|
53
|
+
|
|
54
|
+
Some offers can be accepted only after the person has seen the message
|
|
55
|
+
(`result.deliverFirst` is `true`). Show it first, then call
|
|
56
|
+
`result.accept("deliver_now")`.
|
|
57
|
+
|
|
58
|
+
## API
|
|
59
|
+
|
|
60
|
+
| Export | What it is |
|
|
61
|
+
|---|---|
|
|
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`. |
|
|
63
|
+
| `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. |
|
|
65
|
+
| `MachineOffer`, `SponsoredResult`, `Sponsorship`, `ConsentPolicy` | The types. |
|
|
66
|
+
|
|
67
|
+
## Support
|
|
68
|
+
|
|
69
|
+
Write to <hello@constile.app>. For an assistant that runs MCP servers, see
|
|
70
|
+
[`constile-mcp`](https://www.npmjs.com/package/constile-mcp).
|
|
71
|
+
|
|
72
|
+
## License
|
|
73
|
+
|
|
74
|
+
MIT. See LICENSE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
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:
|
|
5
|
+
*
|
|
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.
|
|
16
|
+
*/
|
|
17
|
+
type MachineOffer = {
|
|
18
|
+
constile: "1";
|
|
19
|
+
offer_version: string;
|
|
20
|
+
token: string;
|
|
21
|
+
resource: { url: string; title: string };
|
|
22
|
+
sponsored_message: string;
|
|
23
|
+
expires_at: number;
|
|
24
|
+
/** Where the person, or the assistant for them, can report the ad. */
|
|
25
|
+
report_url?: string;
|
|
26
|
+
/** defer and prepared are missing when the offer can only be accepted by showing the message now. */
|
|
27
|
+
actions: { deliver_now: string; defer?: string; prepared?: string; content: string };
|
|
28
|
+
rules: string[];
|
|
29
|
+
};
|
|
30
|
+
export type { MachineOffer };
|
|
31
|
+
export type ConsentPolicy = "accept" | "ask" | "decline";
|
|
32
|
+
export type Sponsorship = {
|
|
33
|
+
offer: MachineOffer;
|
|
34
|
+
/** Show this to the person exactly, labeled as advertising. */
|
|
35
|
+
message: string;
|
|
36
|
+
/** Call before finishing, if the message is in the answer you're about to send. */
|
|
37
|
+
reportPrepared: () => Promise<boolean>;
|
|
38
|
+
/** Call only after the person was actually shown the message. */
|
|
39
|
+
reportDelivered: () => Promise<boolean>;
|
|
40
|
+
};
|
|
41
|
+
export type SponsoredResult = {
|
|
42
|
+
kind: "content";
|
|
43
|
+
response: Response;
|
|
44
|
+
sponsorship: Sponsorship | null;
|
|
45
|
+
}
|
|
46
|
+
/** deliverFirst: the offer can only be accepted after the person has been shown the message. */
|
|
47
|
+
| {
|
|
48
|
+
kind: "offer";
|
|
49
|
+
offer: MachineOffer;
|
|
50
|
+
deliverFirst: boolean;
|
|
51
|
+
accept: (mode?: "defer" | "deliver_now") => Promise<SponsoredResult>;
|
|
52
|
+
} | {
|
|
53
|
+
kind: "declined";
|
|
54
|
+
offer: MachineOffer;
|
|
55
|
+
};
|
|
56
|
+
export type SponsoredFetchOptions = {
|
|
57
|
+
policy: ConsentPolicy;
|
|
58
|
+
/** Identify your agent with a stable product token, for example "ExampleAssistant-User/1.0". */
|
|
59
|
+
userAgent: string;
|
|
60
|
+
fetch?: (req: Request) => Promise<Response>;
|
|
61
|
+
};
|
|
62
|
+
/** Reads an offer from a page without the Constile-Offer header (another implementation, or a stripped proxy). */
|
|
63
|
+
export declare function parseOfferPage(text: string, pageUrl: string): MachineOffer | null;
|
|
64
|
+
export declare function sponsoredFetch(url: string, opts: SponsoredFetchOptions): Promise<SponsoredResult>;
|
|
65
|
+
/** One or two sentences an agent can use to ask the person. */
|
|
66
|
+
export declare function consentQuestion(offer: MachineOffer): string;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// constile-agent-sdk 0.1.0
|
|
2
|
+
|
|
3
|
+
// packages/agent-sdk/src/index.ts
|
|
4
|
+
var OFFER_MARKER = "SPONSORED ACCESS OFFER";
|
|
5
|
+
function parseOfferPage(text, pageUrl) {
|
|
6
|
+
if (!text.includes(OFFER_MARKER)) return null;
|
|
7
|
+
const link = (kind) => new RegExp(`(https?://[^\\s<"']+/sponsored/${kind}/([A-Za-z0-9_-]+))`).exec(text);
|
|
8
|
+
const defer = link("deferred");
|
|
9
|
+
const deliver = link("delivered");
|
|
10
|
+
const prepared = link("prepared");
|
|
11
|
+
if (!deliver || defer && !prepared) return null;
|
|
12
|
+
const msg = /SPONSORED MESSAGE \(preserve exactly, including disclosure and URL\):\s*(?:<br>\s*)?([\s\S]*?)(?:<\/p>|\n\s*\n)/.exec(text);
|
|
13
|
+
const title = /The article "([^"]*)"/.exec(text);
|
|
14
|
+
const token = deliver[2];
|
|
15
|
+
const root = new URL(pageUrl).origin;
|
|
16
|
+
return {
|
|
17
|
+
constile: "1",
|
|
18
|
+
offer_version: "unknown",
|
|
19
|
+
token,
|
|
20
|
+
resource: { url: pageUrl, title: title?.[1] ?? "" },
|
|
21
|
+
sponsored_message: decodeEntities((msg?.[1] ?? "").trim()),
|
|
22
|
+
expires_at: 0,
|
|
23
|
+
actions: {
|
|
24
|
+
deliver_now: deliver[1],
|
|
25
|
+
...defer && prepared ? { defer: defer[1], prepared: prepared[1] } : {},
|
|
26
|
+
content: `${root}/sponsored/content/${token}`
|
|
27
|
+
},
|
|
28
|
+
rules: []
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
function decodeEntities(s) {
|
|
32
|
+
return s.replace(/</g, "<").replace(/>/g, ">").replace(/"/g, '"').replace(/'/g, "'").replace(/&/g, "&");
|
|
33
|
+
}
|
|
34
|
+
async function getOffer(res, url, doFetch, ua) {
|
|
35
|
+
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) {
|
|
39
|
+
await res.body?.cancel();
|
|
40
|
+
return { offer: await r.json(), body: null };
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const type = res.headers.get("content-type") ?? "";
|
|
44
|
+
if (!/text\/(html|plain)/.test(type)) return { offer: null, body: null };
|
|
45
|
+
const body = await res.text();
|
|
46
|
+
return { offer: parseOfferPage(body, url), body };
|
|
47
|
+
}
|
|
48
|
+
async function sponsoredFetch(url, opts) {
|
|
49
|
+
const doFetch = opts.fetch ?? ((r) => fetch(r));
|
|
50
|
+
const ua = opts.userAgent;
|
|
51
|
+
const res = await doFetch(new Request(url, { headers: { "user-agent": ua, accept: "text/html,text/plain;q=0.9,*/*;q=0.5" } }));
|
|
52
|
+
if (res.status !== 200) return { kind: "content", response: res, sponsorship: null };
|
|
53
|
+
const { offer, body } = await getOffer(res, url, doFetch, ua);
|
|
54
|
+
if (!offer) {
|
|
55
|
+
const response = body === null ? res : new Response(body, { status: res.status, headers: res.headers });
|
|
56
|
+
return { kind: "content", response, sponsorship: null };
|
|
57
|
+
}
|
|
58
|
+
const deliverFirst = !offer.actions.defer;
|
|
59
|
+
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.");
|
|
61
|
+
const action = mode === "defer" ? offer.actions.defer : offer.actions.deliver_now;
|
|
62
|
+
const r = await doFetch(new Request(action, { method: "POST", headers: { "user-agent": ua } }));
|
|
63
|
+
const report = (u) => async () => (await doFetch(new Request(u, { method: "POST", headers: { "user-agent": ua } }))).ok;
|
|
64
|
+
return {
|
|
65
|
+
kind: "content",
|
|
66
|
+
response: r,
|
|
67
|
+
sponsorship: {
|
|
68
|
+
offer,
|
|
69
|
+
message: offer.sponsored_message,
|
|
70
|
+
reportPrepared: offer.actions.prepared ? report(offer.actions.prepared) : async () => false,
|
|
71
|
+
reportDelivered: report(offer.actions.deliver_now)
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
if (opts.policy === "decline") return { kind: "declined", offer };
|
|
76
|
+
if (opts.policy === "ask" || deliverFirst) return { kind: "offer", offer, deliverFirst, accept };
|
|
77
|
+
return accept("defer");
|
|
78
|
+
}
|
|
79
|
+
function consentQuestion(offer) {
|
|
80
|
+
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?`;
|
|
82
|
+
}
|
|
83
|
+
export {
|
|
84
|
+
consentQuestion,
|
|
85
|
+
parseOfferPage,
|
|
86
|
+
sponsoredFetch
|
|
87
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "constile-agent-sdk",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "For AI agents that fetch pages for a person: handles Constile's sponsored access, with the person's saved choice.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "CCC2C Inc. <hello@constile.app>",
|
|
7
|
+
"homepage": "https://constile.app",
|
|
8
|
+
"bugs": {
|
|
9
|
+
"email": "hello@constile.app"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"constile",
|
|
13
|
+
"ai agents",
|
|
14
|
+
"sponsored access",
|
|
15
|
+
"fetch"
|
|
16
|
+
],
|
|
17
|
+
"type": "module",
|
|
18
|
+
"sideEffects": false,
|
|
19
|
+
"engines": {
|
|
20
|
+
"node": ">=18"
|
|
21
|
+
},
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"import": "./dist/index.js"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"files": [
|
|
30
|
+
"dist",
|
|
31
|
+
"README.md",
|
|
32
|
+
"LICENSE"
|
|
33
|
+
]
|
|
34
|
+
}
|