@ahtmljs/agent 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.
Files changed (2) hide show
  1. package/README.md +98 -0
  2. package/package.json +33 -5
package/README.md ADDED
@@ -0,0 +1,98 @@
1
+ # @ahtmljs/agent
2
+
3
+ Client SDK for consuming **[AHTML](https://github.com/DibbayajyotiRoy/AHTML)** —
4
+ the HTML of the agent web.
5
+
6
+ Fetch typed semantic snapshots from any AHTML-emitting site, cache by
7
+ ETag, replay diffs, run actions with dry-run support, and measure token
8
+ cost using the industry-standard tokenizers (`gpt-tokenizer`,
9
+ `@anthropic-ai/tokenizer`).
10
+
11
+ ```bash
12
+ npm install @ahtmljs/agent @ahtmljs/schema
13
+ # optional tokenizer peers (for token cost measurement)
14
+ npm install gpt-tokenizer @anthropic-ai/tokenizer
15
+ ```
16
+
17
+ ## Fetching a snapshot
18
+
19
+ ```ts
20
+ import { AHTMLClient } from '@ahtmljs/agent';
21
+
22
+ const client = new AHTMLClient({ agent: 'MyAgent/1.0' });
23
+
24
+ // Default Accept is `application/ahtml+text` (compact, token-optimal).
25
+ const snap = await client.fetch('https://shop.com/ahtml/products/mbp-14');
26
+
27
+ console.log(snap.entities[0]); // typed Product
28
+ console.log(snap.actions); // typed action contracts
29
+ ```
30
+
31
+ Subsequent fetches send `If-None-Match: <etag>` automatically. If the
32
+ server has a `?since=<etag>` diff endpoint, the client uses it
33
+ transparently — the returned snapshot is the same shape, the wire cost
34
+ is much less.
35
+
36
+ ## Running an action safely
37
+
38
+ ```ts
39
+ import { runAction } from '@ahtmljs/agent';
40
+
41
+ const action = snap.actions.find((a) => a.id === 'purchase')!;
42
+
43
+ // Dry-run first — hits action.preview_url, returns intended changes.
44
+ const preview = await runAction(snap, action, { sku: 'MBP14', quantity: 1 }, {
45
+ dryRun: true,
46
+ });
47
+ // → { status: 'dry_run',
48
+ // would_charge: { amount: 1999, currency: 'USD' },
49
+ // would_side_effects: ['charge_card', 'email_buyer', 'decrement_stock'] }
50
+
51
+ // Then commit, with explicit confirmation if the contract requires it.
52
+ const result = await runAction(snap, action, { sku: 'MBP14', quantity: 1 }, {
53
+ confirm: true, // required because action.confirmation === 'required'
54
+ bearer: process.env.OAUTH!, // required because action.auth === 'required'
55
+ });
56
+ // → { status: 'executed', output: Receipt, http_status: 200 }
57
+ ```
58
+
59
+ `runAction` refuses to fire an action whose `confirmation: 'required'`
60
+ unless `{ confirm: true }` is passed — gives your agent a built-in
61
+ safety gate.
62
+
63
+ ## Measuring token cost
64
+
65
+ ```ts
66
+ import { countTokensGpt, countTokensClaude, measure } from '@ahtmljs/agent';
67
+
68
+ await countTokensGpt(text, 'o200k_base'); // OpenAI tiktoken (GPT-4o, o-series)
69
+ await countTokensGpt(text, 'cl100k_base'); // OpenAI tiktoken (GPT-4, 3.5)
70
+ await countTokensClaude(text); // Anthropic official Claude tokenizer
71
+
72
+ // Everything in one shot:
73
+ await measure(text);
74
+ // → { bytes, bytes_gzip, tokens_openai_cl100k, tokens_openai_o200k, tokens_anthropic }
75
+ ```
76
+
77
+ These wrap the **actual tokenizers** OpenAI and Anthropic use internally
78
+ (`gpt-tokenizer` and `@anthropic-ai/tokenizer`). No `text.length / 4`
79
+ approximations.
80
+
81
+ ## What is AHTML?
82
+
83
+ AHTML turns any website into an MCP server, an OpenAPI provider, a
84
+ JSON-LD source, and a token-optimal semantic snapshot — all from one
85
+ plugin. This package is the client SDK that consumes those snapshots.
86
+
87
+ If you're building an AI agent that browses the web, you want this.
88
+
89
+ ## Documentation
90
+
91
+ - **Repository:** [`DibbayajyotiRoy/AHTML`](https://github.com/DibbayajyotiRoy/AHTML)
92
+ - **Spec:** [`SPEC.md`](https://github.com/DibbayajyotiRoy/AHTML/blob/main/SPEC.md)
93
+ - **For AI assistants:** [`docs/agents.md`](https://github.com/DibbayajyotiRoy/AHTML/blob/main/docs/agents.md)
94
+ - **Recipes (including dry-run, diff crawling):** [`docs/recipes.md`](https://github.com/DibbayajyotiRoy/AHTML/blob/main/docs/recipes.md)
95
+
96
+ ## License
97
+
98
+ MIT
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ahtmljs/agent",
3
- "version": "0.1.0",
4
- "description": "AHTML agent SDK — fetch snapshots, cache by ETag, run actions with dry-run support, and measure token cost using industry-standard tokenizers.",
3
+ "version": "0.1.1",
4
+ "description": "AHTML agent SDK — fetch snapshots from any AHTML-emitting site, cache by ETag, run actions with dry-run support, and measure token cost using the industry-standard tokenizers.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",
@@ -9,14 +9,43 @@
9
9
  ".": { "types": "./dist/index.d.ts", "import": "./dist/index.js" },
10
10
  "./tokens": { "types": "./dist/tokens.d.ts", "import": "./dist/tokens.js" }
11
11
  },
12
- "files": ["dist"],
12
+ "files": ["dist", "README.md"],
13
13
  "publishConfig": { "access": "public" },
14
14
  "scripts": {
15
15
  "build": "tsc -p tsconfig.json",
16
16
  "dev": "tsc -p tsconfig.json --watch"
17
17
  },
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/DibbayajyotiRoy/AHTML.git",
21
+ "directory": "packages/agent"
22
+ },
23
+ "homepage": "https://github.com/DibbayajyotiRoy/AHTML#readme",
24
+ "bugs": {
25
+ "url": "https://github.com/DibbayajyotiRoy/AHTML/issues",
26
+ "email": "rdibbayajyoti@gmail.com"
27
+ },
28
+ "author": {
29
+ "name": "Dibbayajyoti Roy",
30
+ "email": "rdibbayajyoti@gmail.com",
31
+ "url": "https://github.com/DibbayajyotiRoy"
32
+ },
33
+ "keywords": [
34
+ "ahtml",
35
+ "agent",
36
+ "agent-web",
37
+ "ai",
38
+ "llm",
39
+ "client",
40
+ "sdk",
41
+ "tokenizer",
42
+ "tiktoken",
43
+ "mcp",
44
+ "model-context-protocol",
45
+ "crawler"
46
+ ],
18
47
  "dependencies": {
19
- "@ahtmljs/schema": "0.1.0"
48
+ "@ahtmljs/schema": "0.1.1"
20
49
  },
21
50
  "devDependencies": {
22
51
  "@types/node": "^22.0.0"
@@ -29,6 +58,5 @@
29
58
  "gpt-tokenizer": { "optional": true },
30
59
  "@anthropic-ai/tokenizer": { "optional": true }
31
60
  },
32
- "keywords": ["ahtml", "agent", "mcp", "llm", "tokenizer", "crawler"],
33
61
  "license": "MIT"
34
62
  }