apptvty 0.1.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.
@@ -0,0 +1,263 @@
1
+ interface ApptvtyConfig {
2
+ /** Your site's API key from the Apptvty dashboard (ak_...) */
3
+ apiKey: string;
4
+ /** Your site ID from the Apptvty dashboard */
5
+ siteId: string;
6
+ /**
7
+ * Apptvty API base URL. Default: https://api.apptvty.com.
8
+ * Uses APPTVTY_API_URL env var if set. Override only for self-hosted backend.
9
+ */
10
+ baseUrl?: string;
11
+ /**
12
+ * How many log entries to accumulate before flushing to the API.
13
+ * Default: 50
14
+ */
15
+ batchSize?: number;
16
+ /**
17
+ * How often (in ms) to flush accumulated logs, even if batchSize is not reached.
18
+ * Default: 5000 (5 seconds)
19
+ */
20
+ flushInterval?: number;
21
+ /**
22
+ * Whether to enable debug logging to console.
23
+ * Default: false
24
+ */
25
+ debug?: boolean;
26
+ /**
27
+ * Path at which the query page is served on the website owner's domain.
28
+ * Default: '/query'
29
+ * This path is referenced in agents.txt and sitemaps.
30
+ */
31
+ queryPath?: string;
32
+ }
33
+ interface CrawlerInfo {
34
+ isAi: boolean;
35
+ name: string | null;
36
+ organization: string | null;
37
+ /** 0.0 – 1.0 */
38
+ confidence: number;
39
+ detectionMethod: 'exact_match' | 'pattern_match' | 'heuristic' | 'none';
40
+ }
41
+ interface RequestLogEntry {
42
+ site_id: string;
43
+ timestamp: string;
44
+ method: string;
45
+ path: string;
46
+ status_code: number;
47
+ response_time_ms: number;
48
+ ip_address: string;
49
+ user_agent: string;
50
+ referer: string | null;
51
+ is_ai_crawler: boolean;
52
+ crawler_type: string | null;
53
+ crawler_organization: string | null;
54
+ confidence_score: number;
55
+ }
56
+ /**
57
+ * Sent to the Apptvty backend when an agent queries the site's /query endpoint.
58
+ */
59
+ interface QueryRequest {
60
+ site_id: string;
61
+ query: string;
62
+ agent_ua: string;
63
+ agent_ip: string;
64
+ request_id: string;
65
+ /** ISO timestamp */
66
+ timestamp: string;
67
+ /** Include sponsored ads in response (default true). Set to false to opt out. */
68
+ surface_ads?: boolean;
69
+ /** Explicit signal: "I am an AI agent/crawler" — ensures ads are surfaced. Use ?ai_crawler=1 when crawling. */
70
+ ai_crawler?: boolean;
71
+ }
72
+ /**
73
+ * The raw response from the Apptvty backend for a query.
74
+ * The SDK passes this through to the agent unchanged,
75
+ * wrapping it in the standard envelope.
76
+ */
77
+ interface BackendQueryResponse {
78
+ answer: string;
79
+ sources: QuerySource[];
80
+ confidence: number;
81
+ tokens_used: number;
82
+ /** Present only when ads are enabled for this site and a matching ad was found (1–2 ads max) */
83
+ sponsored?: SponsoredAd | SponsoredAd[];
84
+ }
85
+ interface QuerySource {
86
+ url: string;
87
+ title: string;
88
+ snippet: string;
89
+ relevance: number;
90
+ }
91
+ /**
92
+ * An ad returned from the Apptvty ad network.
93
+ *
94
+ * Advertisers pay per agentic view (impression).
95
+ * The website owner earns USDC for each impression served.
96
+ *
97
+ * The impression_id is used by the SDK to log the view back
98
+ * to Apptvty for billing. Do not modify or omit it.
99
+ */
100
+ interface SponsoredAd {
101
+ /** Human-readable label — always "Sponsored" for transparency */
102
+ label: 'Sponsored';
103
+ /** The ad copy shown to the agent */
104
+ text: string;
105
+ /** Attribution/click URL for the advertiser */
106
+ url: string;
107
+ /** Advertiser name */
108
+ advertiser: string;
109
+ /**
110
+ * Unique identifier for this ad impression.
111
+ * The SDK sends this back to Apptvty to record the view and trigger billing.
112
+ */
113
+ impression_id: string;
114
+ }
115
+ /**
116
+ * The JSON response returned by the SDK's query endpoint to the calling agent.
117
+ */
118
+ interface AgentQueryResponse {
119
+ success: true;
120
+ version: '1.0';
121
+ query: string;
122
+ answer: string;
123
+ sources: QuerySource[];
124
+ confidence: number;
125
+ /** Present when ads are enabled and an ad was matched (1–2 ads max) */
126
+ sponsored?: SponsoredAd | SponsoredAd[];
127
+ metadata: {
128
+ request_id: string;
129
+ response_time_ms: number;
130
+ tokens_used: number;
131
+ site_id: string;
132
+ timestamp: string;
133
+ };
134
+ }
135
+ /**
136
+ * Returned when the query endpoint is called without a ?q= parameter.
137
+ * Instructs agents how to use the endpoint (self-describing discovery page).
138
+ */
139
+ interface QueryEndpointDiscovery {
140
+ version: '1.0';
141
+ endpoint: string;
142
+ description: string;
143
+ /** Instructs crawlers to prefer query over full-site crawl */
144
+ crawl_policy: string;
145
+ usage: {
146
+ method: 'GET';
147
+ parameters: {
148
+ q: {
149
+ type: 'string';
150
+ required: true;
151
+ description: string;
152
+ };
153
+ lang: {
154
+ type: 'string';
155
+ required: false;
156
+ description: string;
157
+ };
158
+ surface_ads: {
159
+ type: 'boolean';
160
+ required: false;
161
+ description: string;
162
+ };
163
+ ai_crawler: {
164
+ type: 'boolean';
165
+ required: false;
166
+ description: string;
167
+ };
168
+ };
169
+ example: string;
170
+ response_format: 'application/json';
171
+ };
172
+ capabilities: string[];
173
+ rate_limit: string;
174
+ }
175
+ interface AgentErrorResponse {
176
+ success: false;
177
+ error: {
178
+ code: string;
179
+ message: string;
180
+ request_id: string;
181
+ timestamp: string;
182
+ };
183
+ }
184
+ /**
185
+ * Sent to Apptvty when an ad is shown in a query response.
186
+ * Triggers: advertiser billing, publisher USDC credit.
187
+ */
188
+ interface PageAd {
189
+ text: string;
190
+ url: string;
191
+ advertiser: string;
192
+ impression_id: string;
193
+ }
194
+ interface PageAdsResponse {
195
+ ads: PageAd[];
196
+ }
197
+ interface ImpressionLog {
198
+ impression_id: string;
199
+ site_id: string;
200
+ query: string;
201
+ agent_ua: string;
202
+ agent_ip: string;
203
+ timestamp: string;
204
+ }
205
+ /** 30-day overview returned by getSiteStats */
206
+ interface SiteOverviewStats {
207
+ total_requests_30d: number;
208
+ ai_requests_30d: number;
209
+ human_requests_30d: number;
210
+ ai_percentage: number;
211
+ unique_crawlers_30d: number;
212
+ queries_30d: number;
213
+ avg_response_ms: number;
214
+ }
215
+ /** Per-day stats returned by getSiteDailyStats */
216
+ interface DailyStat {
217
+ date: string;
218
+ total_requests: number;
219
+ ai_requests: number;
220
+ human_requests: number;
221
+ avg_response_ms: number;
222
+ unique_crawlers: number;
223
+ unique_ips: number;
224
+ }
225
+ /** Crawler type breakdown */
226
+ interface CrawlerBreakdown {
227
+ crawler_type: string;
228
+ requests: number;
229
+ percentage: number;
230
+ last_seen: string;
231
+ }
232
+ /** Recent request in activity feed */
233
+ interface RecentActivityItem {
234
+ timestamp: string;
235
+ path: string;
236
+ crawler_type: string | null;
237
+ is_ai_crawler: boolean;
238
+ status_code: number;
239
+ response_time_ms: number | null;
240
+ user_agent: string | null;
241
+ }
242
+ /** Recent agent query */
243
+ interface RecentQueryItem {
244
+ id: string;
245
+ timestamp: string;
246
+ question: string;
247
+ answer_summary: string | null;
248
+ tokens_used: number | null;
249
+ response_time_ms: number | null;
250
+ crawler_type: string | null;
251
+ success: boolean;
252
+ }
253
+ /** Wallet balance and earnings */
254
+ interface SiteWalletInfo {
255
+ company_id: string;
256
+ site_id: string;
257
+ wallet_address: string | null;
258
+ balance_usdc: number;
259
+ total_earned_usdc: number;
260
+ total_spent_usdc: number;
261
+ }
262
+
263
+ export type { ApptvtyConfig as A, BackendQueryResponse as B, CrawlerBreakdown as C, DailyStat as D, ImpressionLog as I, PageAdsResponse as P, QueryRequest as Q, RequestLogEntry as R, SiteOverviewStats as S, RecentActivityItem as a, RecentQueryItem as b, SiteWalletInfo as c, CrawlerInfo as d, AgentQueryResponse as e, QueryEndpointDiscovery as f, AgentErrorResponse as g, PageAd as h, QuerySource as i, SponsoredAd as j };
@@ -0,0 +1,263 @@
1
+ interface ApptvtyConfig {
2
+ /** Your site's API key from the Apptvty dashboard (ak_...) */
3
+ apiKey: string;
4
+ /** Your site ID from the Apptvty dashboard */
5
+ siteId: string;
6
+ /**
7
+ * Apptvty API base URL. Default: https://api.apptvty.com.
8
+ * Uses APPTVTY_API_URL env var if set. Override only for self-hosted backend.
9
+ */
10
+ baseUrl?: string;
11
+ /**
12
+ * How many log entries to accumulate before flushing to the API.
13
+ * Default: 50
14
+ */
15
+ batchSize?: number;
16
+ /**
17
+ * How often (in ms) to flush accumulated logs, even if batchSize is not reached.
18
+ * Default: 5000 (5 seconds)
19
+ */
20
+ flushInterval?: number;
21
+ /**
22
+ * Whether to enable debug logging to console.
23
+ * Default: false
24
+ */
25
+ debug?: boolean;
26
+ /**
27
+ * Path at which the query page is served on the website owner's domain.
28
+ * Default: '/query'
29
+ * This path is referenced in agents.txt and sitemaps.
30
+ */
31
+ queryPath?: string;
32
+ }
33
+ interface CrawlerInfo {
34
+ isAi: boolean;
35
+ name: string | null;
36
+ organization: string | null;
37
+ /** 0.0 – 1.0 */
38
+ confidence: number;
39
+ detectionMethod: 'exact_match' | 'pattern_match' | 'heuristic' | 'none';
40
+ }
41
+ interface RequestLogEntry {
42
+ site_id: string;
43
+ timestamp: string;
44
+ method: string;
45
+ path: string;
46
+ status_code: number;
47
+ response_time_ms: number;
48
+ ip_address: string;
49
+ user_agent: string;
50
+ referer: string | null;
51
+ is_ai_crawler: boolean;
52
+ crawler_type: string | null;
53
+ crawler_organization: string | null;
54
+ confidence_score: number;
55
+ }
56
+ /**
57
+ * Sent to the Apptvty backend when an agent queries the site's /query endpoint.
58
+ */
59
+ interface QueryRequest {
60
+ site_id: string;
61
+ query: string;
62
+ agent_ua: string;
63
+ agent_ip: string;
64
+ request_id: string;
65
+ /** ISO timestamp */
66
+ timestamp: string;
67
+ /** Include sponsored ads in response (default true). Set to false to opt out. */
68
+ surface_ads?: boolean;
69
+ /** Explicit signal: "I am an AI agent/crawler" — ensures ads are surfaced. Use ?ai_crawler=1 when crawling. */
70
+ ai_crawler?: boolean;
71
+ }
72
+ /**
73
+ * The raw response from the Apptvty backend for a query.
74
+ * The SDK passes this through to the agent unchanged,
75
+ * wrapping it in the standard envelope.
76
+ */
77
+ interface BackendQueryResponse {
78
+ answer: string;
79
+ sources: QuerySource[];
80
+ confidence: number;
81
+ tokens_used: number;
82
+ /** Present only when ads are enabled for this site and a matching ad was found (1–2 ads max) */
83
+ sponsored?: SponsoredAd | SponsoredAd[];
84
+ }
85
+ interface QuerySource {
86
+ url: string;
87
+ title: string;
88
+ snippet: string;
89
+ relevance: number;
90
+ }
91
+ /**
92
+ * An ad returned from the Apptvty ad network.
93
+ *
94
+ * Advertisers pay per agentic view (impression).
95
+ * The website owner earns USDC for each impression served.
96
+ *
97
+ * The impression_id is used by the SDK to log the view back
98
+ * to Apptvty for billing. Do not modify or omit it.
99
+ */
100
+ interface SponsoredAd {
101
+ /** Human-readable label — always "Sponsored" for transparency */
102
+ label: 'Sponsored';
103
+ /** The ad copy shown to the agent */
104
+ text: string;
105
+ /** Attribution/click URL for the advertiser */
106
+ url: string;
107
+ /** Advertiser name */
108
+ advertiser: string;
109
+ /**
110
+ * Unique identifier for this ad impression.
111
+ * The SDK sends this back to Apptvty to record the view and trigger billing.
112
+ */
113
+ impression_id: string;
114
+ }
115
+ /**
116
+ * The JSON response returned by the SDK's query endpoint to the calling agent.
117
+ */
118
+ interface AgentQueryResponse {
119
+ success: true;
120
+ version: '1.0';
121
+ query: string;
122
+ answer: string;
123
+ sources: QuerySource[];
124
+ confidence: number;
125
+ /** Present when ads are enabled and an ad was matched (1–2 ads max) */
126
+ sponsored?: SponsoredAd | SponsoredAd[];
127
+ metadata: {
128
+ request_id: string;
129
+ response_time_ms: number;
130
+ tokens_used: number;
131
+ site_id: string;
132
+ timestamp: string;
133
+ };
134
+ }
135
+ /**
136
+ * Returned when the query endpoint is called without a ?q= parameter.
137
+ * Instructs agents how to use the endpoint (self-describing discovery page).
138
+ */
139
+ interface QueryEndpointDiscovery {
140
+ version: '1.0';
141
+ endpoint: string;
142
+ description: string;
143
+ /** Instructs crawlers to prefer query over full-site crawl */
144
+ crawl_policy: string;
145
+ usage: {
146
+ method: 'GET';
147
+ parameters: {
148
+ q: {
149
+ type: 'string';
150
+ required: true;
151
+ description: string;
152
+ };
153
+ lang: {
154
+ type: 'string';
155
+ required: false;
156
+ description: string;
157
+ };
158
+ surface_ads: {
159
+ type: 'boolean';
160
+ required: false;
161
+ description: string;
162
+ };
163
+ ai_crawler: {
164
+ type: 'boolean';
165
+ required: false;
166
+ description: string;
167
+ };
168
+ };
169
+ example: string;
170
+ response_format: 'application/json';
171
+ };
172
+ capabilities: string[];
173
+ rate_limit: string;
174
+ }
175
+ interface AgentErrorResponse {
176
+ success: false;
177
+ error: {
178
+ code: string;
179
+ message: string;
180
+ request_id: string;
181
+ timestamp: string;
182
+ };
183
+ }
184
+ /**
185
+ * Sent to Apptvty when an ad is shown in a query response.
186
+ * Triggers: advertiser billing, publisher USDC credit.
187
+ */
188
+ interface PageAd {
189
+ text: string;
190
+ url: string;
191
+ advertiser: string;
192
+ impression_id: string;
193
+ }
194
+ interface PageAdsResponse {
195
+ ads: PageAd[];
196
+ }
197
+ interface ImpressionLog {
198
+ impression_id: string;
199
+ site_id: string;
200
+ query: string;
201
+ agent_ua: string;
202
+ agent_ip: string;
203
+ timestamp: string;
204
+ }
205
+ /** 30-day overview returned by getSiteStats */
206
+ interface SiteOverviewStats {
207
+ total_requests_30d: number;
208
+ ai_requests_30d: number;
209
+ human_requests_30d: number;
210
+ ai_percentage: number;
211
+ unique_crawlers_30d: number;
212
+ queries_30d: number;
213
+ avg_response_ms: number;
214
+ }
215
+ /** Per-day stats returned by getSiteDailyStats */
216
+ interface DailyStat {
217
+ date: string;
218
+ total_requests: number;
219
+ ai_requests: number;
220
+ human_requests: number;
221
+ avg_response_ms: number;
222
+ unique_crawlers: number;
223
+ unique_ips: number;
224
+ }
225
+ /** Crawler type breakdown */
226
+ interface CrawlerBreakdown {
227
+ crawler_type: string;
228
+ requests: number;
229
+ percentage: number;
230
+ last_seen: string;
231
+ }
232
+ /** Recent request in activity feed */
233
+ interface RecentActivityItem {
234
+ timestamp: string;
235
+ path: string;
236
+ crawler_type: string | null;
237
+ is_ai_crawler: boolean;
238
+ status_code: number;
239
+ response_time_ms: number | null;
240
+ user_agent: string | null;
241
+ }
242
+ /** Recent agent query */
243
+ interface RecentQueryItem {
244
+ id: string;
245
+ timestamp: string;
246
+ question: string;
247
+ answer_summary: string | null;
248
+ tokens_used: number | null;
249
+ response_time_ms: number | null;
250
+ crawler_type: string | null;
251
+ success: boolean;
252
+ }
253
+ /** Wallet balance and earnings */
254
+ interface SiteWalletInfo {
255
+ company_id: string;
256
+ site_id: string;
257
+ wallet_address: string | null;
258
+ balance_usdc: number;
259
+ total_earned_usdc: number;
260
+ total_spent_usdc: number;
261
+ }
262
+
263
+ export type { ApptvtyConfig as A, BackendQueryResponse as B, CrawlerBreakdown as C, DailyStat as D, ImpressionLog as I, PageAdsResponse as P, QueryRequest as Q, RequestLogEntry as R, SiteOverviewStats as S, RecentActivityItem as a, RecentQueryItem as b, SiteWalletInfo as c, CrawlerInfo as d, AgentQueryResponse as e, QueryEndpointDiscovery as f, AgentErrorResponse as g, PageAd as h, QuerySource as i, SponsoredAd as j };
package/package.json ADDED
@@ -0,0 +1,82 @@
1
+ {
2
+ "name": "apptvty",
3
+ "version": "0.1.2",
4
+ "description": "Server-side analytics and AEO (Agent Experience Optimization) SDK for websites. Logs agentic traffic, exposes a structured query endpoint for AI agents, and serves relevant ads on agent queries.",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
+ "bin": {
9
+ "apptvty": "dist/cli.js"
10
+ },
11
+ "exports": {
12
+ ".": {
13
+ "types": "./dist/index.d.ts",
14
+ "import": "./dist/index.mjs",
15
+ "require": "./dist/index.js"
16
+ },
17
+ "./setup": {
18
+ "types": "./dist/setup.d.ts",
19
+ "import": "./dist/setup.mjs",
20
+ "require": "./dist/setup.js"
21
+ },
22
+ "./nextjs": {
23
+ "types": "./dist/middleware/nextjs.d.ts",
24
+ "import": "./dist/middleware/nextjs.mjs",
25
+ "require": "./dist/middleware/nextjs.js"
26
+ },
27
+ "./express": {
28
+ "types": "./dist/middleware/express.d.ts",
29
+ "import": "./dist/middleware/express.mjs",
30
+ "require": "./dist/middleware/express.js"
31
+ }
32
+ },
33
+ "scripts": {
34
+ "build": "tsup",
35
+ "dev": "tsup --watch",
36
+ "typecheck": "tsc --noEmit",
37
+ "test": "jest",
38
+ "test:unit": "jest --testPathIgnorePatterns=integration",
39
+ "test:integration": "jest --testPathPattern=integration",
40
+ "prepublishOnly": "npm run build && npm run typecheck"
41
+ },
42
+ "keywords": [
43
+ "analytics",
44
+ "aeo",
45
+ "agent-experience-optimization",
46
+ "ai-crawler",
47
+ "agentic-traffic",
48
+ "sitemap",
49
+ "rag",
50
+ "apptvty"
51
+ ],
52
+ "license": "MIT",
53
+ "files": [
54
+ "dist",
55
+ "README.md"
56
+ ],
57
+ "engines": {
58
+ "node": ">=18.0.0"
59
+ },
60
+ "peerDependencies": {
61
+ "next": ">=13.0.0",
62
+ "express": ">=4.0.0"
63
+ },
64
+ "peerDependenciesMeta": {
65
+ "next": {
66
+ "optional": true
67
+ },
68
+ "express": {
69
+ "optional": true
70
+ }
71
+ },
72
+ "devDependencies": {
73
+ "@types/express": "^4.17.21",
74
+ "@types/jest": "^29.5.12",
75
+ "@types/node": "^20.12.0",
76
+ "jest": "^29.7.0",
77
+ "next": "^14.0.0",
78
+ "ts-jest": "^29.1.2",
79
+ "tsup": "^8.0.2",
80
+ "typescript": "^5.4.5"
81
+ }
82
+ }