@adport/mcp 0.7.0 → 0.8.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.
@@ -1,668 +0,0 @@
1
- // src/index.ts
2
- import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
3
- import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
4
- import { z } from "zod";
5
- import { registerAppResource, registerAppTool, RESOURCE_MIME_TYPE } from "@modelcontextprotocol/ext-apps/server";
6
- import { AdportError, CredentialStore, createContext } from "@adport/core";
7
- import { createGoogleModule } from "@adport/provider-google";
8
- import { createMetaModule } from "@adport/provider-meta";
9
- import { createTikTokModule } from "@adport/provider-tiktok";
10
- import { createAppleModule } from "@adport/provider-apple";
11
- import { createMicrosoftModule } from "@adport/provider-microsoft";
12
- import { createRedditModule } from "@adport/provider-reddit";
13
- import { createSnapchatModule } from "@adport/provider-snapchat";
14
- import { createSpotifyModule } from "@adport/provider-spotify";
15
- import { createPinterestModule } from "@adport/provider-pinterest";
16
- import { createLinkedInModule } from "@adport/provider-linkedin";
17
- import { createXModule } from "@adport/provider-x";
18
-
19
- // package.json
20
- var package_default = {
21
- name: "@adport/mcp",
22
- version: "0.7.0",
23
- description: "adport MCP server \u2014 the open control plane for paid media",
24
- license: "Apache-2.0",
25
- type: "module",
26
- main: "./dist/index.js",
27
- types: "./dist/index.d.ts",
28
- exports: {
29
- ".": {
30
- types: "./dist/index.d.ts",
31
- import: "./dist/index.js"
32
- }
33
- },
34
- bin: {
35
- "adport-mcp": "./dist/bin.js"
36
- },
37
- files: [
38
- "dist"
39
- ],
40
- scripts: {
41
- build: "tsup src/index.ts src/bin.ts --format esm --dts --sourcemap --clean",
42
- "render:submission-previews": "node scripts/render-submission-previews.mjs",
43
- test: "vitest run",
44
- typecheck: "tsc --noEmit",
45
- clean: "rm -rf dist"
46
- },
47
- dependencies: {
48
- "@adport/core": "workspace:*",
49
- "@adport/provider-apple": "workspace:*",
50
- "@adport/provider-google": "workspace:*",
51
- "@adport/provider-meta": "workspace:*",
52
- "@adport/provider-microsoft": "workspace:*",
53
- "@adport/provider-reddit": "workspace:*",
54
- "@adport/provider-snapchat": "workspace:*",
55
- "@adport/provider-spotify": "workspace:*",
56
- "@adport/provider-pinterest": "workspace:*",
57
- "@adport/provider-linkedin": "workspace:*",
58
- "@adport/provider-x": "workspace:*",
59
- "@adport/provider-tiktok": "workspace:*",
60
- "@modelcontextprotocol/ext-apps": "^1.7.5",
61
- "@modelcontextprotocol/sdk": "^1.30.0",
62
- zod: "^4.4.3"
63
- },
64
- devDependencies: {
65
- "@types/node": "^26.1.2",
66
- tsup: "^8.3.0",
67
- typescript: "^5.8.0",
68
- vitest: "^4.1.10"
69
- },
70
- repository: {
71
- type: "git",
72
- url: "git+https://github.com/ynnickw/adport.git",
73
- directory: "packages/mcp"
74
- },
75
- homepage: "https://github.com/ynnickw/adport#readme",
76
- bugs: {
77
- url: "https://github.com/ynnickw/adport/issues"
78
- },
79
- engines: {
80
- node: ">=22.13"
81
- },
82
- publishConfig: {
83
- access: "public"
84
- }
85
- };
86
-
87
- // src/ui.ts
88
- var ADPORT_UI_URI = "ui://adport/insight-card-v2.html";
89
- var ADPORT_UI_DOMAIN = "https://app.adport.dev";
90
- var providerNames = {
91
- google: "Google Ads",
92
- meta: "Meta Ads",
93
- tiktok: "TikTok Ads",
94
- apple: "Apple Ads",
95
- microsoft: "Microsoft Advertising",
96
- reddit: "Reddit Ads",
97
- snapchat: "Snapchat Ads",
98
- spotify: "Spotify Ads",
99
- pinterest: "Pinterest Ads",
100
- linkedin: "LinkedIn Ads",
101
- x: "X Ads",
102
- mock: "Demo",
103
- demo: "Synthetic demo"
104
- };
105
- function viewForTool(name, readOnly) {
106
- if (name === "accounts_list") return "accounts";
107
- if (name === "report") return "report";
108
- if (name === "audit_run" || name === "recommendation_dismiss") return "insights";
109
- if (!readOnly) return "operation";
110
- if (name.startsWith("recommendation") || name.startsWith("audit_")) return "insights";
111
- return void 0;
112
- }
113
- function toolTitle(name) {
114
- if (name === "accounts_list") return "Show connected ad accounts";
115
- if (name === "report") return "Show advertising performance";
116
- return name.split("_").map((part) => part.charAt(0).toUpperCase() + part.slice(1)).join(" ");
117
- }
118
- function toolInvocationLabels(view) {
119
- switch (view) {
120
- case "accounts":
121
- return { invoking: "Loading ad accounts\u2026", invoked: "Ad accounts ready" };
122
- case "report":
123
- return { invoking: "Analyzing performance\u2026", invoked: "Performance ready" };
124
- // Descriptor labels cover both preview and apply; the card shows the actual result.
125
- case "operation":
126
- return { invoking: "Processing change\u2026", invoked: "Change result ready" };
127
- case "insights":
128
- return { invoking: "Reviewing opportunities\u2026", invoked: "Opportunities ready" };
129
- }
130
- }
131
- function structuredResult(tool, view, result, approval) {
132
- const payload = result && typeof result === "object" && !Array.isArray(result) ? result : { value: result };
133
- return {
134
- ...payload,
135
- _adport: {
136
- tool,
137
- view,
138
- providerNames,
139
- ...approval ? { approval } : {}
140
- }
141
- };
142
- }
143
- var ADPORT_UI_HTML = String.raw`<!doctype html>
144
- <html lang="en">
145
- <head>
146
- <meta charset="utf-8" />
147
- <meta name="viewport" content="width=device-width,initial-scale=1" />
148
- <meta name="color-scheme" content="light dark" />
149
- <title>Adport insight</title>
150
- <style>
151
- :root {
152
- --ink: var(--color-text-primary, #171717);
153
- --muted: var(--color-text-secondary, #6f6f73);
154
- --faint: var(--color-text-tertiary, #949499);
155
- --paper: var(--color-background-primary, #ffffff);
156
- --soft: var(--color-background-secondary, #f6f6f4);
157
- --line: var(--color-border-secondary, rgba(23,23,23,.11));
158
- --orange: #ff5a1f;
159
- --green: #16845b;
160
- --red: #c8392f;
161
- --amber: #a86500;
162
- --radius: var(--border-radius-lg, 18px);
163
- --sans: var(--font-sans, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif);
164
- --mono: var(--font-mono, ui-monospace, SFMono-Regular, Menlo, monospace);
165
- }
166
- * { box-sizing: border-box }
167
- html, body { margin: 0; min-width: 0; background: transparent; color: var(--ink); font-family: var(--sans) }
168
- body { padding: 2px; }
169
- button { font: inherit }
170
- .shell { overflow: hidden; border: 1px solid var(--line); border-radius: var(--radius); background: var(--paper); box-shadow: 0 10px 32px rgba(20,20,20,.07) }
171
- .top { display:flex; align-items:center; justify-content:space-between; gap:16px; padding:14px 16px 10px }
172
- .brand { display:flex; align-items:center; gap:9px; min-width:0 }
173
- .wordmark-dot { color:#ff6b00; font-size:1.3em; line-height:0 }
174
- .brand b { font-size:12px; letter-spacing:-.02em }
175
- .context { color:var(--muted); font-size:11px; text-align:right }
176
- .content { padding:4px 16px 16px }
177
- .kpis { display:grid; grid-template-columns:repeat(4,minmax(0,1fr)); gap:8px; margin-bottom:15px }
178
- .kpi { min-width:0; padding:12px; border-radius:13px; background:var(--soft); border:1px solid color-mix(in srgb,var(--line) 70%,transparent) }
179
- button.kpi { cursor:pointer; color:var(--ink); text-align:left }
180
- button.kpi[aria-pressed="true"] { border-color:color-mix(in srgb,var(--orange) 65%,var(--line)) }
181
- .kpi small { display:block; color:var(--faint); font:600 9px/1.2 var(--mono); letter-spacing:.11em; text-transform:uppercase }
182
- .kpi strong { display:block; overflow:hidden; margin-top:8px; font-size:clamp(17px,3vw,24px); line-height:1; letter-spacing:-.04em; text-overflow:ellipsis }
183
- .panel { min-width:0; padding:13px; border:1px solid var(--line); border-radius:14px }
184
- .panel-head { display:flex; align-items:center; justify-content:space-between; gap:12px; margin-bottom:12px }
185
- .panel-head b { font-size:12px }
186
- .panel-head span { color:var(--faint); font:500 9px/1 var(--mono); text-transform:uppercase; letter-spacing:.1em }
187
- .bars { display:grid; gap:9px }
188
- .bar-row { display:grid; grid-template-columns:minmax(100px,1fr) minmax(60px,2fr) auto; align-items:center; gap:9px; font-size:11px }
189
- .bar-name { display:flex; align-items:center; gap:7px; min-width:0 }
190
- .bar-name > span:last-child { overflow:hidden; white-space:nowrap; text-overflow:ellipsis }
191
- .bar-name .logo { width:22px; height:22px; padding:4px; border-radius:6px }
192
- .track { height:7px; overflow:hidden; border-radius:99px; background:var(--soft) }
193
- .fill { height:100%; border-radius:99px; background:linear-gradient(90deg,var(--orange),#ff985e) }
194
- .tabs { display:flex; gap:6px; overflow:auto; padding-bottom:12px }
195
- .tabs button { flex:0 0 auto; cursor:pointer; padding:8px 12px; border:1px solid var(--line); border-radius:99px; background:var(--paper); color:var(--muted); font-size:12px }
196
- .tabs button[aria-pressed="true"] { color:var(--ink); border-color:var(--orange); background:color-mix(in srgb,var(--orange) 8%,var(--paper)) }
197
- button:focus-visible { outline:2px solid var(--orange); outline-offset:2px }
198
- .notice { padding:10px 12px; margin:0 0 12px; border-radius:10px; background:var(--soft); color:var(--muted); font-size:12px; line-height:1.45 }
199
- .bar-value { color:var(--muted); font:600 10px/1 var(--mono); white-space:nowrap }
200
- .list { display:grid; gap:7px }
201
- .row { display:flex; align-items:center; justify-content:space-between; gap:12px; padding:10px 11px; border:1px solid var(--line); border-radius:12px }
202
- .identity { display:flex; align-items:center; gap:10px; min-width:0 }
203
- .logo { display:grid; place-items:center; flex:0 0 auto; width:29px; height:29px; padding:6px; border:1px solid var(--line); border-radius:9px; color:var(--ink); background:var(--paper); font-weight:800; font-size:10px }
204
- .logo svg { display:block; width:100%; height:100% }
205
- .logo.reddit { background:#ff4500 }.logo.linkedin { background:#0a66c2 }.logo.spotify { background:#1db954 }.logo.pinterest { background:#e60023 }.logo.x { background:#111 }.logo.snapchat { background:#fffc00;color:#111 }
206
- .identity-copy { min-width:0 }
207
- .identity-copy b, .identity-copy span { display:block; overflow:hidden; white-space:nowrap; text-overflow:ellipsis }
208
- .identity-copy b { font-size:12px }.identity-copy span { margin-top:3px; color:var(--muted); font-size:10px }
209
- .pill { flex:0 0 auto; padding:5px 8px; border-radius:99px; color:var(--green); background:color-mix(in srgb,var(--green) 10%,transparent); font:700 9px/1 var(--mono); letter-spacing:.08em; text-transform:uppercase }
210
- .pill.warn { color:var(--amber); background:color-mix(in srgb,var(--amber) 11%,transparent) }
211
- .pill.danger { color:var(--red); background:color-mix(in srgb,var(--red) 10%,transparent) }
212
- .pill.neutral { color:var(--muted); background:var(--soft) }
213
- .operation-name { margin:2px 0 16px; font-size:14px; line-height:1.4; font-weight:600; overflow-wrap:anywhere }
214
- .comparison { width:100%; border-collapse:collapse; table-layout:fixed; margin-bottom:12px }
215
- .comparison th { padding:0 8px 8px; color:var(--muted); font-size:10px; font-weight:500; text-align:left }
216
- .comparison td { border-top:1px solid var(--line); padding:12px 8px; font-size:14px; overflow-wrap:anywhere }
217
- .comparison td:first-child { color:var(--muted); font-size:11px }
218
- .comparison td:last-child { font-weight:600 }
219
- .comparison .changed { color:var(--orange) }
220
- .adjustment { margin:8px 0; color:var(--amber); font-size:11px; line-height:1.4 }
221
- .approval { display:flex; align-items:center; gap:10px; flex-wrap:wrap; margin:12px 0 }
222
- .approve { cursor:pointer; padding:9px 16px; border:0; border-radius:99px; background:var(--ink); color:var(--paper); font-size:12px; font-weight:700 }
223
- .approve:disabled { cursor:default; opacity:.55 }
224
- .approve { position:relative; overflow:hidden; touch-action:none; user-select:none }
225
- .approve::before { content:''; position:absolute; inset:0; background:#ff650055; transform:scaleX(0); transform-origin:left }
226
- .approve.holding::before { transform:scaleX(1); transition:transform .9s linear }
227
- .approve:focus-visible { outline:2px solid var(--orange); outline-offset:3px }
228
- @media (prefers-reduced-motion:reduce) { .approve.holding::before { transition:none } }
229
- details { border-top:1px solid var(--line); padding-top:10px; color:var(--muted); font-size:11px; line-height:1.5 }
230
- summary { cursor:pointer; width:fit-content; padding:3px 0 }
231
- summary:focus-visible { outline:2px solid var(--orange); outline-offset:3px }
232
- details p { margin:8px 0; overflow-wrap:anywhere }
233
- .empty { padding:26px 12px; text-align:center; color:var(--muted); font-size:12px }
234
- @media (max-width:620px) { .kpis { grid-template-columns:repeat(2,minmax(0,1fr)) }.top,.content { padding-left:12px; padding-right:12px } }
235
- @media (max-width:420px) { .bar-row { grid-template-columns:minmax(0,1fr) auto; gap:5px }.bar-name { grid-column:1/-1 }.bar-name > span:last-child { white-space:normal }.bars { gap:12px } }
236
- @media (prefers-reduced-motion:reduce) { * { animation:none!important; transition:none!important } }
237
- :root[data-theme="dark"] { --ink:#f5f5f3;--muted:#aaa9ad;--faint:#a09fa5;--paper:#171717;--soft:#222220;--line:rgba(255,255,255,.11) }
238
- @media (prefers-color-scheme:dark) { :root:not([data-theme="light"]) { --ink:#f5f5f3;--muted:#aaa9ad;--faint:#a09fa5;--paper:#171717;--soft:#222220;--line:rgba(255,255,255,.11) } }
239
- </style>
240
- </head>
241
- <body>
242
- <main id="app" class="shell" aria-live="polite">
243
- <header class="top"><div class="brand"><b>adport<span class="wordmark-dot" aria-hidden="true">.</span></b></div></header>
244
- <section class="content"><div class="empty">Loading…</div></section>
245
- </main>
246
- <script>
247
- (() => {
248
- const app = document.getElementById('app');
249
- const state = { host: {}, result: null, reportGroup: null, reportMetric: 'spend', approving: false, approvalError: '' };
250
- const esc = (value) => String(value ?? '').replace(/[&<>"']/g, (c) => ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[c]));
251
- const arr = (value) => Array.isArray(value) ? value : [];
252
- const num = (value) => Number.isFinite(Number(value)) ? Number(value) : 0;
253
- const money = (value, currency, maximumFractionDigits=2) => currency
254
- ? new Intl.NumberFormat(state.host.locale || 'en', { style:'currency', currency, maximumFractionDigits }).format(num(value))
255
- : new Intl.NumberFormat(state.host.locale || 'en', { maximumFractionDigits }).format(num(value));
256
- const compact = (value) => new Intl.NumberFormat(state.host.locale || 'en', { notation:'compact', maximumFractionDigits:1 }).format(num(value));
257
- const available = (value) => typeof value === 'number' && Number.isFinite(value);
258
- const total = (rows, key) => rows.length && rows.every(r => available(r.metrics?.[key])) ? rows.reduce((sum,r) => sum+r.metrics[key],0) : null;
259
- const rowReturn = (row) => {
260
- const m=row.metrics || {};
261
- if (available(m.conversion_value)) return m.conversion_value;
262
- // A report can request ROAS without conversion_value. Weight reported
263
- // ratios by spend; never average campaign ratios or infer missing revenue.
264
- const value=available(m.spend) && m.spend>0 && available(m.roas) ? m.spend*m.roas : null;
265
- return available(value) ? value : null;
266
- };
267
- const currencyOf = (row) => typeof row.currency === 'string' && /^[A-Z]{3}$/.test(row.currency) ? row.currency : null;
268
- const statusOf = (status) => !status || /unknown|unspecified/i.test(status) ? {label:'Status unavailable',tone:'neutral'} : /disabled|removed|inactive|closed|suspended/i.test(status) ? {label:status,tone:'danger'} : /paused|disable$/i.test(status) ? {label:status,tone:'warn'} : /^(enabled|active|approved)$/i.test(status) ? {label:status,tone:''} : {label:status,tone:'neutral'};
269
- const notices = (data) => [...arr(data.errors).map(e => (e.provider || 'Provider')+': '+(e.message || 'Read failed')), ...arr(data.warnings).map(e => e.message || 'Some metadata is unavailable'), ...(data.truncated ? ['Partial result: row limit reached. Totals and charts cover returned rows only.'] : [])].map(text => '<p class="notice">'+esc(text)+'</p>').join('');
270
- const providerName = (id, names) => names?.[id] || String(id || 'Provider');
271
- const initials = (id) => ({snapchat:'S',spotify:'S',pinterest:'P',linkedin:'in',x:'X'}[id] || String(id || '?').slice(0,2).toUpperCase());
272
- const providerSvg = {
273
- // Unmodified official Ghost paths, matching cloud/components/snapchat-logo.tsx.
274
- snapchat:"<svg viewBox=\"0 0 1024 1024\" aria-hidden=\"true\"> <path fill=\"#fff\" d=\"M992.5,756.3c-4.2-13.9-24.3-23.7-24.3-23.7l0,0c-1.9-1-3.6-1.9-5-2.6c-33.5-16.2-63.2-35.7-88.2-57.8 c-20.1-17.8-37.3-37.4-51.1-58.2c-16.9-25.4-24.8-46.6-28.2-58.1c-1.9-7.5-1.6-10.5,0-14.4c1.3-3.3,5.2-6.4,7-7.9 c11.3-8,29.5-19.8,40.7-27c9.7-6.3,18-11.7,22.9-15.1c15.7-11,26.5-22.2,32.8-34.3c8.2-15.6,9.2-32.8,2.8-49.7 c-8.6-22.8-29.9-36.4-57-36.4c-6,0-12.2,0.7-18.4,2c-15.5,3.4-30.2,8.9-42.5,13.7c-0.9,0.4-1.9-0.3-1.8-1.3 c1.3-30.5,2.8-71.5-0.6-110.4c-3-35.2-10.3-64.9-22.1-90.8c-11.9-26-27.4-45.2-39.5-59.1C708.5,112,688.2,92.5,657.6,75 C614.6,50.4,565.6,37.9,512,37.9c-53.5,0-102.4,12.5-145.5,37.1c-32.4,18.5-53.1,39.4-62.5,50.2c-12.1,13.9-27.6,33.1-39.5,59.1 c-11.9,25.9-19.1,55.5-22.1,90.8c-3.4,39.1-2,76.8-0.6,110.4c0,1-0.9,1.7-1.9,1.3c-12.3-4.8-27-10.3-42.5-13.7 c-6.1-1.3-12.3-2-18.4-2c-27,0-48.3,13.6-57,36.4c-6.4,16.9-5.4,34.1,2.8,49.7c6.4,12.1,17.1,23.3,32.8,34.3 c4.8,3.4,13.2,8.8,22.9,15.1c10.9,7.1,28.6,18.6,40,26.5c1.4,1,6.2,4.6,7.7,8.4c1.6,4,1.9,7-0.2,15c-3.5,11.6-11.4,32.6-28,57.5 c-13.8,20.9-31,40.4-51.1,58.2c-25,22.1-54.7,41.6-88.2,57.8c-1.6,0.8-3.5,1.7-5.5,2.9l0,0c0,0-20,10.2-23.8,23.4 c-5.6,19.5,9.3,37.8,24.4,47.6c24.8,16,55,24.6,72.5,29.3c4.9,1.3,9.3,2.5,13.3,3.7c2.5,0.8,8.8,3.2,11.5,6.7 c3.4,4.4,3.8,9.8,5,15.9v0c1.9,10.3,6.2,23,18.9,31.8c14,9.6,31.7,10.3,54.2,11.2c23.5,0.9,52.7,2,86.2,13.1 c15.5,5.1,29.6,13.8,45.8,23.8c34,20.9,76.3,46.9,148.5,46.9c72.3,0,114.9-26.1,149.1-47.1c16.2-9.9,30.1-18.5,45.3-23.5 c33.5-11.1,62.7-12.2,86.2-13.1c22.5-0.9,40.2-1.5,54.2-11.2c13.6-9.4,17.5-23.4,19.3-33.9c1-5.2,1.6-9.9,4.6-13.7 c2.6-3.3,8.4-5.6,11.1-6.5c4.1-1.3,8.7-2.5,13.8-3.9c17.5-4.7,39.5-10.2,66.2-25.3C993.7,789.8,995.9,767.4,992.5,756.3z\" /> <path fill=\"#000\" d=\"M1020.3,745.5c-7.1-19.4-20.7-29.7-36.1-38.3c-2.9-1.7-5.6-3.1-7.8-4.1c-4.6-2.4-9.3-4.7-14-7.1 c-48.1-25.5-85.7-57.7-111.7-95.8c-8.8-12.9-14.9-24.5-19.2-34c-2.2-6.4-2.1-10-0.5-13.3c1.2-2.5,4.4-5.1,6.2-6.4 c8.3-5.5,16.8-11,22.6-14.7c10.3-6.7,18.5-12,23.7-15.6c19.8-13.8,33.6-28.5,42.2-44.9c12.2-23.1,13.7-49.5,4.3-74.3 c-13-34.4-45.6-55.8-85-55.8c-8.2,0-16.5,0.9-24.7,2.7c-2.2,0.5-4.3,1-6.4,1.5c0.4-23.4-0.2-48.4-2.3-72.8 c-7.4-86-37.5-131.1-68.9-167c-13.1-15-35.9-36.9-70.1-56.5C624.9,21.7,570.9,7.9,512,7.9c-58.7,0-112.7,13.8-160.4,41.1 c-34.4,19.6-57.2,41.6-70.2,56.5c-31.4,35.9-61.5,81-68.9,167c-2.1,24.4-2.6,49.4-2.3,72.8c-2.1-0.5-4.3-1-6.4-1.5 c-8.2-1.8-16.6-2.7-24.7-2.7c-39.4,0-72,21.4-85,55.8c-9.4,24.8-7.9,51.2,4.3,74.3c8.6,16.4,22.5,31.1,42.2,44.9 c5.3,3.7,13.4,9,23.7,15.6c5.6,3.6,13.7,8.9,21.7,14.2c1.2,0.8,5.5,4,7,7c1.7,3.4,1.7,7.1-0.8,13.9c-4.2,9.3-10.3,20.7-18.9,33.3 c-25.5,37.3-62,68.9-108.5,94.1c-24.7,13.1-50.3,21.8-61.1,51.2c-8.2,22.2-2.8,47.5,17.9,68.8l0,0c6.8,7.3,15.4,13.8,26.2,19.8 c25.4,14,47,20.9,64,25.6c3,0.9,9.9,3.1,12.9,5.8c7.6,6.6,6.5,16.6,16.6,31.2c6.1,9.1,13.1,15.3,18.9,19.3 c21.1,14.6,44.9,15.5,70.1,16.5c22.7,0.9,48.5,1.9,77.9,11.6c12.2,4,24.9,11.8,39.5,20.8c35.2,21.7,83.5,51.3,164.2,51.3 c80.8,0,129.3-29.8,164.8-51.5c14.6-8.9,27.2-16.7,39-20.6c29.4-9.7,55.2-10.7,77.9-11.6c25.2-1,48.9-1.9,70.1-16.5 c6.6-4.6,15-12.1,21.6-23.5c7.2-12.3,7.1-21,13.9-26.9c2.8-2.4,8.9-4.5,12.2-5.5c17.1-4.7,39-11.6,64.9-25.9 c11.5-6.3,20.4-13.2,27.5-21.1c0.1-0.1,0.2-0.2,0.3-0.3C1023.4,791.7,1028.3,767.2,1020.3,745.5z M948.6,784 c-43.8,24.2-72.9,21.6-95.5,36.1c-19.2,12.4-7.9,39.1-21.8,48.7c-17.2,11.9-67.9-0.8-133.4,20.8c-54,17.9-88.5,69.2-185.8,69.2 c-97.5,0-131-51.1-185.8-69.2c-65.5-21.6-116.3-8.9-133.4-20.8c-13.9-9.6-2.6-36.3-21.8-48.7c-22.6-14.6-51.7-12-95.5-36.1 c-27.9-15.4-12.1-24.9-2.8-29.4c158.6-76.7,183.8-195.3,185-204.2c1.4-10.6,2.9-19-8.8-29.9c-11.3-10.5-61.6-41.6-75.5-51.3 c-23.1-16.1-33.2-32.2-25.7-52c5.2-13.7,18-18.8,31.5-18.8c4.2,0,8.5,0.5,12.6,1.4c25.3,5.5,49.9,18.2,64.1,21.6 c2,0.5,3.7,0.7,5.2,0.7c7.6,0,10.2-3.8,9.7-12.5c-1.6-27.7-5.6-81.7-1.2-132.2c6-69.4,28.4-103.8,55-134.3 c12.8-14.6,72.8-78,187.5-78c115,0,174.7,63.4,187.5,78c26.6,30.4,49,64.8,55,134.3c4.4,50.5,0.6,104.5-1.2,132.2 c-0.6,9.1,2.2,12.5,9.7,12.5c1.5,0,3.3-0.2,5.2-0.7c14.2-3.4,38.8-16.1,64.1-21.6c4.1-0.9,8.4-1.4,12.6-1.4 c13.5,0,26.3,5.2,31.5,18.8c7.5,19.8-2.7,35.9-25.7,52c-13.9,9.7-64.2,40.8-75.5,51.3c-11.7,10.8-10.2,19.2-8.8,29.9 c1.1,8.9,26.4,127.5,185,204.2C960.6,759.1,976.5,768.6,948.6,784z\" /> </svg>",
275
- google:'<svg viewBox="0 0 24 24"><path fill="#34a853" d="M4 22.93a4 4 0 1 1 0-8 4 4 0 0 1 0 8Z"/><path fill="#4285f4" d="M23.464 16.929 15.463 3.072A4 4 0 0 0 8.534 7.072l8.001 13.857a4 4 0 0 0 6.929-4Z"/><path fill="#fbbc04" d="M7.514 4.844 1.565 15.148A4.5 4.5 0 0 1 4 14.43a4.5 4.5 0 0 1 4.494 4.715l3.217-5.573-3.61-6.25a4 4 0 0 1-.587-2.478Z"/></svg>',
276
- meta:'<svg viewBox="0 0 24 24"><path fill="#0866ff" d="M6.915 4.03C2.484 4.03 0 9.95 0 14.449c0 3.37 1.276 5.521 4.439 5.521 2.93 0 4.146-2.574 6.628-6.764l.942-1.664 2.335 3.895c1.883 3.15 3.38 4.533 5.53 4.533 2.788 0 4.126-1.93 4.126-5.56 0-5.53-2.73-10.38-6.8-10.38-2.04 0-3.835 1.53-5.173 3.348C10.16 5.01 8.593 4.03 6.915 4.03Zm-.04 2.606c1.55 0 2.445 1.193 3.908 3.025l-1.02 1.566c-2.22 3.41-3.576 6.155-5.323 6.155-1.23 0-2.154-1.075-2.154-2.9 0-3.755 2.088-7.846 4.588-7.846Zm10.2-.553c2.488 0 4.639 4.054 4.639 8.4 0 1.547-.368 2.899-1.839 2.899-1.193 0-2.027-1.42-4.496-5.362l-1.872-3.008c1.12-1.814 2.248-2.929 3.559-2.929Z"/></svg>',
277
- tiktok:'<svg viewBox="0 0 58 58"><path fill="#2dccd3" d="M25.98 0v37.68c0 4.94-3.55 8.11-7.88 8.11-1.43 0-2.79-.33-3.97-.95 1.5 1.91 3.87 3.01 6.47 3.01 4.32 0 7.87-3.17 7.87-8.11V2.06h6.85A20.3 20.3 0 0 1 34.87 0h-8.89Z"/><path fill="#f1204a" d="M41.73 11.11a17.48 17.48 0 0 1-4.37-9.05h-2.04c1.16 4.23 3.56 7.18 6.41 9.05Zm7.63 11.37a19.31 19.31 0 0 1-11-2.97c3.84 3.84 8.5 5.03 13.5 5.03v-8.82a17.4 17.4 0 0 1-2.5-.37v7.13Z"/><path fill="currentColor" d="M35.32 2.06h-6.85v37.68c0 4.94-3.55 8.11-7.87 8.11-2.6 0-4.97-1.1-6.47-3.01-2.48-1.29-4.19-3.82-4.19-7.16 0-5.71 4.47-8.71 10.42-8.09v-7.68C10.63 21.93 2.49 29.73 2.49 39.4c0 5.16 2.04 9.71 5.32 12.96 2.89 1.97 6.39 3.11 10.15 3.11 8.69 0 17.91-6.38 17.91-18.41V17.45c.8.8 1.63 1.48 2.49 2.06a19.31 19.31 0 0 0 11 2.97v-7.13a16.14 16.14 0 0 1-7.63-4.24c-2.85-1.87-5.25-4.82-6.41-9.05Z"/></svg>',
278
- apple:'<svg viewBox="0 0 24 24"><path fill="currentColor" d="M12.152 6.896c-.948 0-2.415-1.078-3.96-1.04-2.04.027-3.91 1.183-4.961 3.014-2.117 3.675-.546 9.103 1.519 12.09 1.013 1.454 2.208 3.09 3.792 3.039 1.52-.065 2.09-.987 3.935-.987 1.831 0 2.35.987 3.96.948 1.637-.026 2.676-1.48 3.676-2.948 1.156-1.688 1.636-3.325 1.662-3.415-.039-.013-3.182-1.221-3.22-4.857-.026-3.04 2.48-4.494 2.597-4.559-1.429-2.09-3.623-2.324-4.39-2.376-2-.156-3.675 1.09-4.61 1.09Zm3.377-3.065C16.372 2.819 16.929 1.403 16.774 0c-1.207.052-2.662.805-3.532 1.818-.78.896-1.454 2.338-1.273 3.714 1.338.104 2.715-.688 3.56-1.701Z"/></svg>',
279
- microsoft:'<svg viewBox="0 0 24 24"><path fill="#f25022" d="M0 0h11.4v11.4H0Z"/><path fill="#7fba00" d="M12.6 0H24v11.4H12.6Z"/><path fill="#00a4ef" d="M0 12.6h11.4V24H0Z"/><path fill="#ffb900" d="M12.6 12.6H24V24H12.6Z"/></svg>',
280
- reddit:'<svg viewBox="0 0 24 24"><path fill="#ff4500" d="M12 0A12 12 0 0 0 0 12a12 12 0 0 0 12 12 12 12 0 0 0 12-12A12 12 0 0 0 12 0Zm5.01 4.744c.688 0 1.25.561 1.25 1.249a1.25 1.25 0 0 1-2.498.056l-2.597-.547-.8 3.747c1.824.07 3.48.632 4.674 1.488.308-.309.73-.491 1.207-.491.968 0 1.754.786 1.754 1.754 0 .716-.435 1.333-1.01 1.614.028.169.042.342.042.52 0 2.694-3.13 4.87-7.004 4.87-3.874 0-7.004-2.176-7.004-4.87 0-.183.015-.366.043-.534A1.748 1.748 0 0 1 4.028 12c0-.968.786-1.754 1.754-1.754.463 0 .898.196 1.207.49 1.207-.883 2.878-1.43 4.744-1.487l.885-4.182a.342.342 0 0 1 .378-.239l2.906.617a1.214 1.214 0 0 1 1.108-.701Z"/></svg>'
281
- };
282
- const logo = (id) => '<span class="logo '+esc(id)+'" aria-hidden="true">'+(providerSvg[id] || esc(initials(id)))+'</span>';
283
- let syntheticResult = false;
284
- const chrome = (body, context) => '<header class="top"><div class="brand"><b>adport<span class="wordmark-dot" aria-hidden="true">.</span></b></div><span class="context">'+(syntheticResult?'Synthetic demo · ':'')+esc(context || '')+'</span></header><section class="content">'+body+'</section>';
285
- function renderAccounts(data, meta) {
286
- const accounts = arr(data.accounts), errors = arr(data.errors), names = meta.providerNames || {};
287
- const body = notices(data)+(accounts.length ? '<div class="list">'+accounts.map(a => { const status=statusOf(a.status); return '<div class="row"><div class="identity">'+logo(a.provider)+'<div class="identity-copy"><b>'+esc(a.name || 'Ad account')+'</b><span>'+esc(providerName(a.provider,names))+' · '+esc(a.id)+(a.currency ? ' · '+esc(a.currency) : '')+'</span></div></div><span class="pill '+status.tone+'">'+esc(status.label)+'</span></div>'; }).join('')+'</div>' : '<div class="empty">'+(errors.length ? 'Account inventory could not be fully loaded.' : 'No accessible ad accounts were returned.')+'</div>');
288
- app.innerHTML = chrome(body, accounts.length+' account'+(accounts.length===1?'':'s'));
289
- }
290
- function renderReport(data) {
291
- const groups = new Map();
292
- for (const row of arr(data.rows)) {
293
- const currency=currencyOf(row), key=currency || JSON.stringify([row.provider,row.accountId]);
294
- if (!groups.has(key)) groups.set(key,{key,currency,label:currency || (row.provider+' · '+row.accountId),rows:[]});
295
- groups.get(key).rows.push(row);
296
- }
297
- const choices=[...groups.values()];
298
- const selected=groups.get(state.reportGroup) || choices[0];
299
- const rows=selected?.rows || [], currency=selected?.currency;
300
- state.reportGroup=selected?.key;
301
- const tabs=choices.length>1 ? '<div class="tabs" role="group" aria-label="Report currency or account">'+choices.map((g,i)=>'<button data-group="'+i+'" aria-pressed="'+(g===selected)+'">'+esc(g.label)+'</button>').join('')+'</div>' : '';
302
- const summary=arr(data.summary?.groups).find(group=>group.key===selected?.key)?.metrics;
303
- const aggregate=key=>summary ? (available(summary[key])?summary[key]:null) : total(rows,key);
304
- const spend=aggregate('spend'), returns=rows.map(rowReturn);
305
- const value=returns.length && returns.every(available) ? returns.reduce((sum,value)=>sum+value,0) : null;
306
- const ratio=summary ? summary.roas : spend>0 && value!==null ? value/spend : null;
307
- const roas=available(ratio) ? ratio.toFixed(2)+'×' : '—';
308
- const metric=state.reportMetric, labels={spend:'Spend',clicks:'Clicks',conversions:'Conversions',roas:'ROAS'};
309
- const metricValue=r=>metric==='roas' ? (available(r.metrics?.conversion_value) && r.metrics?.spend>0 ? r.metrics.conversion_value/r.metrics.spend : r.metrics?.roas) : r.metrics?.[metric];
310
- const format=value=>metric==='spend'?money(value,currency):metric==='roas'?value.toFixed(2)+'×':compact(value);
311
- const ranked=[...rows].filter(r=>available(metricValue(r))).sort((a,b)=>metricValue(b)-metricValue(a)).slice(0,6), max=Math.max(1,...ranked.map(metricValue));
312
- const bars=ranked.length ? ranked.map(r=>'<div class="bar-row"><span class="bar-name" title="'+esc(r.entity?.name || r.entity?.id)+'">'+logo(r.provider)+'<span>'+esc(r.entity?.name || r.entity?.id || 'Entity')+'</span></span><span class="track" aria-hidden="true"><span class="fill" style="display:block;width:'+Math.max(0,metricValue(r)/max*100).toFixed(1)+'%"></span></span><span class="bar-value">'+esc(format(metricValue(r)))+'</span></div>').join('') : '<div class="empty">No '+esc(labels[metric])+' values were returned.</div>';
313
- const kpi=(key,label,value)=>'<button class="kpi" data-metric="'+key+'" aria-pressed="'+(metric===key)+'" aria-label="Chart '+esc(label)+'"><small>'+esc(label)+'</small><strong>'+esc(value)+'</strong></button>';
314
- const period=typeof data.date_range==='string' ? data.date_range.replaceAll('_',' ') : data.date_range ? data.date_range.start+' – '+data.date_range.end : 'Selected period';
315
- const body=notices(data)+tabs+(rows.length ? (!currency?'<p class="notice">Currency unavailable. These values belong to this account only; no cross-account money total is calculated.</p>':'')+'<div class="kpis" role="group" aria-label="Chart metric">'+kpi('spend','Spend',spend===null?'—':money(spend,currency))+kpi('clicks','Clicks',total(rows,'clicks')===null?'—':compact(total(rows,'clicks')))+kpi('conversions','Conversions',total(rows,'conversions')===null?'—':compact(total(rows,'conversions')))+kpi('roas','ROAS',roas)+'</div><div class="panel"><div class="panel-head"><b>'+esc(labels[metric])+'</b><span>Top '+ranked.length+' of '+rows.length+' rows</span></div><div class="bars">'+bars+'</div></div>' : '<div class="empty">'+(arr(data.errors).length?'Report unavailable for the requested providers.':'No rows returned for this period.')+'</div>');
316
- app.innerHTML=chrome(body,period+(currency?' · '+currency:''));
317
- app.querySelectorAll('[data-group]').forEach(button=>button.addEventListener('click',()=>{state.reportGroup=choices[Number(button.dataset.group)].key;renderReport(data);}));
318
- app.querySelectorAll('[data-metric]').forEach(button=>button.addEventListener('click',()=>{state.reportMetric=button.dataset.metric;renderReport(data);}));
319
- }
320
- function renderOperation(data, meta) {
321
- if (meta.tool === 'recommendation_apply' && data.result) data = data.result;
322
- const preview = data.preview || {}, pending = data.pending_operation_id, applied = data.status === 'applied' || data.applied === true;
323
- const changes = arr(preview.changes), coercions = arr(preview.coercions), deltas = arr(preview.budgetDeltas);
324
- // Review amounts must preserve micros; rounded report totals are not suitable for consent.
325
- const comparisons=deltas.map(v=>{const currency=currencyOf(v);return {label:(v.target || 'Budget')+(currency?' · '+currency:' (account units)'),before:available(v.fromMicros)?money(v.fromMicros/1e6,currency,6):'—',after:available(v.toMicros)?money(v.toMicros/1e6,currency,6):'—'};});
326
- // Split only explicit field/value arrows. Freeform JSON updates still
327
- // show the proposed payload, but never claim a previous value.
328
- for (const change of changes) {
329
- const match=String(change).match(/^~\s+(?:.+?\s+)?(status|configured_status|bidding_strategy|delivery|bid|dailyBudget\.value|DailyBudget|value)\s*:?\s*(.*?)\s*→\s*(.+)$/i);
330
- if (match) { const label=match[1].replaceAll('_',' ').replace(/([a-z])([A-Z])/g,'$1 $2');comparisons.push({label:label[0].toUpperCase()+label.slice(1),before:match[2].trim() || '—',after:match[3].trim()}); }
331
- }
332
- // Other provider plans still have a concrete change list. Show the
333
- // proposed effect without pretending we fetched the previous object.
334
- for (const change of changes.slice(0,12)) {
335
- const value=String(change).trim();
336
- if (/^~\s+(?:.+?\s+)?(status|configured_status|bidding_strategy|delivery|bid|dailyBudget\.value|DailyBudget|value)\s*:?\s*.*?\s*→\s*.+$/i.test(value)) continue;
337
- if (deltas.length && /\b(budget|amount_micros)\b/i.test(value) && /\s*→\s*/.test(value)) continue;
338
- const kind=value[0];
339
- comparisons.push({
340
- label:kind==='+'?'Create':kind==='-'?'Remove':kind==='~'?'Update':'Change',
341
- before:kind==='+'?'Not present':kind==='-'?value.slice(1).trim():'Not provided',
342
- after:kind==='-'?'Removed':/^[+~!]\s*/.test(value)?value.replace(/^[+~!]\s*/,''):value,
343
- });
344
- }
345
- if (!comparisons.length && preview.summary) comparisons.push({label:'Proposed change',before:'Not provided',after:String(preview.summary)});
346
- const title=String(preview.summary || '').match(/"([^"]+)"/)?.[1] || (comparisons.length ? '' : preview.summary);
347
- const validation=preview.serverValidated===true?'server validated':preview.serverValidated===false?'local preview · not server validated':'validation not reported';
348
- const table=comparisons.length?'<table class="comparison" aria-label="Before and after"><thead><tr><th scope="col">Change</th><th scope="col">Before</th><th scope="col">After</th></tr></thead><tbody>'+comparisons.map(v=>'<tr><td>'+esc(v.label)+'</td><td>'+esc(v.before)+'</td><td class="'+(v.before!==v.after?'changed':'')+'">'+esc(v.after)+'</td></tr>').join('')+'</tbody></table>':'<p class="notice">No change details were returned. Review the tool response before applying.</p>';
349
- const approval=meta.approval;
350
- const canApprove=typeof pending==='string' && pending.length>0 && typeof preview.summary==='string' && preview.summary.trim() && approval?.arguments && typeof approval.arguments==='object' && !Array.isArray(approval.arguments) && typeof meta.tool==='string';
351
- const action=canApprove?'<div class="approval"><button class="approve" data-approve="true" aria-label="Hold to approve and apply" '+(state.approving || state.approvalError?'disabled':'')+'>'+(state.approving?'Applying…':state.approvalError?'Check result in chat':'Hold to apply')+'</button><span class="context">Hold for 0.9s · release to cancel. Expires '+esc(data.expires_at || 'soon')+'</span></div>':'';
352
- const body=(title?'<p class="operation-name">'+esc(title)+'</p>':'')+table+(changes.length>12?'<p class="notice">Showing 12 of '+changes.length+' changes. All changes are in Details.</p>':'')+coercions.map(v=>'<p class="adjustment">'+esc(v)+'</p>').join('')+(state.approvalError?'<p class="notice" role="alert">'+esc(state.approvalError)+'</p>':'')+action+(applied?'<p class="notice">Applied. '+esc(preview.summary || 'The change was submitted.')+'</p>':'')+'<details><summary>Details</summary><p>'+esc(validation)+'</p><p>'+esc(preview.summary || 'No change details returned.')+'</p>'+changes.map(v=>'<p>'+esc(v)+'</p>').join('')+(pending?'<p>Nothing has been changed. Apply requires the matching pending operation token before it expires.</p>':'')+'<p>'+esc(meta.tool || '')+'</p></details>';
353
- app.innerHTML = chrome(body, applied?'Applied':pending?'Preview · Not applied':'Result');
354
- if (canApprove) app.querySelectorAll('[data-approve]').forEach(button=>bindHold(button,()=>approve(meta.tool,approval.arguments,pending)));
355
- }
356
- function bindHold(button,complete) {
357
- let timer=null, interacted=false;
358
- const cancel=()=>{clearTimeout(timer);timer=null;button.classList.remove('holding');};
359
- const begin=event=>{
360
- if (button.disabled || timer!==null) return;
361
- if (event.type==='keydown') {
362
- if (![' ','Enter'].includes(event.key) || event.repeat) return;
363
- event.preventDefault();
364
- } else {
365
- if (event.button!==0) return;
366
- button.setPointerCapture?.(event.pointerId);
367
- }
368
- interacted=true;button.classList.add('holding');
369
- timer=setTimeout(()=>{cancel();complete();},900);
370
- };
371
- button.addEventListener('pointerdown',begin);
372
- button.addEventListener('keydown',begin);
373
- ['pointerup','pointerleave','pointercancel','lostpointercapture','blur'].forEach(name=>button.addEventListener(name,cancel));
374
- button.addEventListener('keyup',event=>{if ([' ','Enter','Escape'].includes(event.key)) cancel();});
375
- button.addEventListener('contextmenu',event=>event.preventDefault());
376
- // Screen-reader activation has no pointer/key sequence. Retain an accessible activation path.
377
- button.addEventListener('click',event=>{if (event.detail===0 && !interacted && !button.disabled) complete();});
378
- }
379
- const pendingRequests=new Map();
380
- async function approve(tool,args,pending) {
381
- if (state.approving || state.approvalError || !state.result) return;
382
- state.approving=true;state.approvalError='';render(state.result);
383
- const requestId=id++;
384
- const response=new Promise((resolve,reject)=>{
385
- const timer=setTimeout(()=>{
386
- pendingRequests.delete(requestId);
387
- reject(new Error('No confirmation arrived. The write may have succeeded; check its status before retrying.'));
388
- },30000);
389
- pendingRequests.set(requestId,{resolve,reject,timer});
390
- });
391
- const parameters={...args,pending_operation_id:pending};
392
- // Use the native ChatGPT bridge when supplied; never dispatch a second write as fallback.
393
- if (typeof window.openai?.callTool==='function') {
394
- Promise.resolve().then(()=>window.openai.callTool(tool,parameters)).then(result=>{
395
- const request=pendingRequests.get(requestId);
396
- if (request) {clearTimeout(request.timer);pendingRequests.delete(requestId);request.resolve(result);}
397
- },error=>{
398
- const request=pendingRequests.get(requestId);
399
- if (request) {clearTimeout(request.timer);pendingRequests.delete(requestId);request.reject(error);}
400
- });
401
- } else send({jsonrpc:'2.0',id:requestId,method:'tools/call',params:{name:tool,arguments:parameters}});
402
- try {
403
- const result=await response;
404
- if (!result || result.isError) throw new Error('Adport could not confirm the change. Check its status before retrying.');
405
- let value=result.structuredContent || result;
406
- if (!result.structuredContent && Array.isArray(result.content)) {
407
- for (const item of result.content) {if (item.type==='text') {try {value=JSON.parse(item.text);break;} catch {}}}
408
- }
409
- const outcome=tool==='recommendation_apply'?value.result:value;
410
- if (outcome?.applied!==true || outcome?.status!=='applied') throw new Error('The change was not confirmed as applied. Check its status before retrying.');
411
- state.approving=false;state.approvalError='';acceptResult({structuredContent:{...value,_adport:{...(state.result.structuredContent?._adport || {}),...value._adport,approval:undefined}}});
412
- } catch (error) {
413
- state.approving=false;state.approvalError=error?.message || 'The change could not be confirmed. Check its status before retrying.';
414
- render(state.result);
415
- }
416
- }
417
- function renderInsights(data) {
418
- const items = data.finding ? [data.finding] : arr(data.findings || data.recommendations || data.results || data.entries);
419
- const body = items.length ? '<div class="list">'+items.slice(0,8).map((item,i)=>'<div class="row"><div class="identity"><span class="logo" style="background:var(--orange)">'+(i+1)+'</span><div class="identity-copy"><b>'+esc(item.title || item.summary || item.rule || 'Opportunity')+'</b><span>'+esc(item.description || item.status || item.severity || 'Review recommended')+'</span></div></div><span class="pill '+(/high|critical/i.test(item.severity || '')?'danger':'')+'">'+esc(item.severity || item.status || 'open')+'</span></div>').join('')+'</div>' : '<div class="empty">No active recommendations were returned.</div>';
420
- app.innerHTML = chrome(body, items.length+' recommendation'+(items.length===1?'':'s'));
421
- }
422
- function render(result) {
423
- let data = result?.structuredContent || result || {};
424
- if (!result?.structuredContent && Array.isArray(result?.content)) {
425
- for (const item of result.content) {
426
- if (item.type !== 'text') continue;
427
- try { const parsed = JSON.parse(item.text); if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) { data = parsed; break; } } catch {}
428
- }
429
- }
430
- const meta = data._adport || {};
431
- syntheticResult = data.data_source === 'synthetic';
432
- if (data.cancelled) {
433
- app.innerHTML=chrome('<p class="notice">'+esc(data.message || 'The host interrupted this request. Check the tool response before retrying.')+'</p>','Request interrupted');
434
- return;
435
- }
436
- if (result?.isError || data.error) {
437
- app.innerHTML=chrome('<p class="notice">'+esc(data.message || 'The request could not complete. See the tool response for details.')+'</p>','Request failed');
438
- return;
439
- }
440
- if (meta.view === 'accounts') return renderAccounts(data,meta);
441
- if (meta.view === 'report') return renderReport(data,meta);
442
- if (meta.view === 'operation') return renderOperation(data,meta);
443
- return renderInsights(data,meta);
444
- }
445
- function acceptResult(result) {
446
- if (!result || typeof result !== 'object') return;
447
- clearTimeout(resultWait);
448
- // ChatGPT can deliver the same result through both supported bridges.
449
- // Do not reset an open disclosure when the second copy arrives.
450
- if (state.result && JSON.stringify(state.result) === JSON.stringify(result)) return;
451
- state.result = result;
452
- render(result);
453
- }
454
- function receiveGlobals(globals) {
455
- if (!globals) return;
456
- const metadata = globals.toolResponseMetadata;
457
- const envelope = metadata?.mcp_tool_result || metadata?.call_tool_result;
458
- if (envelope && typeof envelope === 'object') return acceptResult(envelope);
459
- if (globals.toolOutput && typeof globals.toolOutput === 'object') acceptResult({structuredContent:globals.toolOutput});
460
- }
461
- // Some hosts mount an error result's resource but never relay its payload.
462
- // This is a delivery fallback, not a claim about tool execution or success.
463
- const resultWait = setTimeout(() => {
464
- if (!state.result) app.innerHTML=chrome('<p class="notice">The host has not provided a result to this view. Check the tool response in the conversation. This card will update if a result arrives.</p>','Result not received');
465
- }, 15000);
466
- const send = (message) => window.parent.postMessage(message,'*');
467
- const resize = typeof ResizeObserver === 'undefined' ? null : new ResizeObserver(() => {
468
- send({jsonrpc:'2.0',method:'ui/notifications/size-changed',params:{height:Math.ceil(document.body.getBoundingClientRect().height)}});
469
- });
470
- let id = 1;
471
- window.addEventListener('message', (event) => {
472
- if (event.source !== window.parent) return;
473
- const message = event.data;
474
- if (!message || message.jsonrpc !== '2.0') return;
475
- if (message.id !== undefined && pendingRequests.has(message.id)) {
476
- const request=pendingRequests.get(message.id);
477
- pendingRequests.delete(message.id);
478
- clearTimeout(request.timer);
479
- if (message.error) request.reject(message.error);
480
- else request.resolve(message.result);
481
- return;
482
- }
483
- if (message.id === 1 && message.result) {
484
- state.host = message.result.hostContext || {};
485
- document.documentElement.dataset.theme = state.host.theme || '';
486
- send({jsonrpc:'2.0',method:'ui/notifications/initialized'});
487
- resize?.observe(document.body);
488
- if (state.result) render(state.result);
489
- }
490
- if (message.method === 'ui/notifications/tool-result') acceptResult(message.params);
491
- if (message.method === 'ui/notifications/tool-cancelled') acceptResult({structuredContent:{cancelled:true,message:message.params?.reason}});
492
- if (message.method === 'ui/notifications/host-context-changed') {
493
- const previousLocale = state.host.locale;
494
- state.host = {...state.host,...message.params};
495
- document.documentElement.dataset.theme = state.host.theme || '';
496
- // Size/theme updates must not rebuild the DOM: opening Details changes
497
- // the iframe height, which some hosts echo back as a context update.
498
- if (state.result && previousLocale !== state.host.locale) {
499
- const expanded = app.querySelector('details')?.open || false;
500
- render(state.result);
501
- const details = app.querySelector('details');
502
- if (details) details.open = expanded;
503
- }
504
- }
505
- });
506
- // The compatibility bridge can hydrate after the iframe has mounted,
507
- // including full error envelopes in toolResponseMetadata.
508
- window.addEventListener('openai:set_globals', event => receiveGlobals(event.detail?.globals));
509
- send({jsonrpc:'2.0',id:id++,method:'ui/initialize',params:{appInfo:{name:'Adport Insight',version:'1.0.0'},appCapabilities:{},protocolVersion:'2026-01-26'}});
510
- receiveGlobals(window.openai);
511
- })();
512
- </script>
513
- </body>
514
- </html>`;
515
-
516
- // src/index.ts
517
- var PROVIDER_IDS = ["google", "meta", "tiktok", "apple", "microsoft", "reddit", "snapchat", "spotify", "pinterest", "linkedin", "x"];
518
- var DEFAULT_MCP_ICONS = [{
519
- src: "https://app.adport.dev/icon.svg?brand=orange-dot-v2",
520
- mimeType: "image/svg+xml",
521
- sizes: ["any"]
522
- }];
523
- async function assembleRuntime(options = {}) {
524
- const store = new CredentialStore();
525
- const modules = [];
526
- const includeMock = options.includeMock ?? process.env.ADPORT_DEMO === "true";
527
- if (!includeMock) {
528
- for (const factory of [createGoogleModule, createMetaModule, createTikTokModule, createAppleModule, createMicrosoftModule, createRedditModule, createSnapchatModule, createSpotifyModule, createPinterestModule, createLinkedInModule, createXModule]) {
529
- const module = await factory(store);
530
- if (module) modules.push(module);
531
- }
532
- }
533
- return createContext({ providerModules: modules, includeMock });
534
- }
535
- function createMcpServer({ runtime, name = "adport", version = package_default.version, icons = DEFAULT_MCP_ICONS, scopes, scopeDenials, notConnectedMessage, productionOnly = false, uiDomain = ADPORT_UI_DOMAIN }) {
536
- if (productionOnly && (runtime.dataSource === "synthetic" || runtime.ctx.providers.list().some((provider) => !PROVIDER_IDS.includes(provider.id)) || runtime.registry.list().some((tool) => /^(demo|mock|synthetic)(_|$)/.test(tool.name) || /^(demo|mock|synthetic)$/.test(tool.namespace)))) {
537
- throw new AdportError("POLICY_VIOLATION", "Demo tools and synthetic runtimes are not available on the production connector.");
538
- }
539
- const provenance = (value) => runtime.dataSource === "synthetic" ? { ...value && typeof value === "object" && !Array.isArray(value) ? value : { value }, data_source: "synthetic" } : value;
540
- const server = new McpServer({ name, version, ...icons ? { icons } : {} });
541
- registerAppResource(
542
- server,
543
- "Adport insight card",
544
- ADPORT_UI_URI,
545
- {
546
- description: "Responsive Adport accounts, performance, recommendations, and guarded-change view.",
547
- _meta: {
548
- ui: { domain: uiDomain, prefersBorder: false, csp: { connectDomains: [], resourceDomains: [] } },
549
- "openai/widgetDomain": ADPORT_UI_DOMAIN
550
- }
551
- },
552
- async () => ({
553
- contents: [{
554
- uri: ADPORT_UI_URI,
555
- mimeType: RESOURCE_MIME_TYPE,
556
- text: ADPORT_UI_HTML,
557
- _meta: {
558
- ui: { domain: uiDomain, prefersBorder: false, csp: { connectDomains: [], resourceDomains: [] } },
559
- "openai/widgetDomain": ADPORT_UI_DOMAIN
560
- }
561
- }]
562
- })
563
- );
564
- for (const tool of runtime.registry.list()) {
565
- if (productionOnly && (tool.name === "recommendation_apply" || /_api_(read|create|update|delete|remove)$/.test(tool.name))) continue;
566
- const requiredScope = tool.annotations.readOnly ? "tools:read" : "tools:write";
567
- const scopeDenial = scopes && !scopes.includes(requiredScope) ? scopeDenials?.[requiredScope] : void 0;
568
- if (scopes && !scopes.includes(requiredScope) && !scopeDenial) continue;
569
- const view = viewForTool(tool.name, tool.annotations.readOnly ?? false);
570
- const labels = view ? toolInvocationLabels(view) : void 0;
571
- const wrapOutput = !!tool.output && !(tool.output instanceof z.ZodObject);
572
- const objectOutput = tool.output ? tool.output instanceof z.ZodObject ? tool.output : z.object({ value: tool.output }) : void 0;
573
- const config = {
574
- title: toolTitle(tool.name),
575
- description: scopeDenial ? `${tool.description}
576
-
577
- Unavailable on the current plan: ${scopeDenial.message}` : tool.description,
578
- inputSchema: tool.input.shape,
579
- ...objectOutput ? { outputSchema: objectOutput.extend({
580
- ...view ? { _adport: z.object({ tool: z.literal(tool.name), view: z.literal(view), providerNames: z.record(z.string(), z.string()), approval: z.object({ arguments: z.record(z.string(), z.unknown()) }).optional() }) } : {},
581
- ...runtime.dataSource === "synthetic" ? { data_source: z.literal("synthetic") } : {}
582
- }) } : {},
583
- annotations: {
584
- title: toolTitle(tool.name),
585
- readOnlyHint: tool.annotations.readOnly ?? false,
586
- destructiveHint: tool.annotations.destructive ?? false,
587
- openWorldHint: tool.annotations.openWorld ?? false
588
- }
589
- };
590
- const callback = async (args) => {
591
- if (scopeDenial) {
592
- const payload = provenance({
593
- error: scopeDenial.code,
594
- code: scopeDenial.code,
595
- message: scopeDenial.message,
596
- ...scopeDenial.data
597
- });
598
- return {
599
- content: [{
600
- type: "text",
601
- text: JSON.stringify(payload, null, 2)
602
- }],
603
- // SDK clients validate structuredContent even on isError responses.
604
- // Errors use JSON text (also consumed by the widget), never a fake
605
- // success object to satisfy the advertised output schema.
606
- isError: true
607
- };
608
- }
609
- try {
610
- const rawResult = await runtime.registry.call(tool.name, args, runtime.ctx);
611
- const result = provenance(rawResult);
612
- const structured = provenance(wrapOutput ? { value: rawResult } : rawResult);
613
- const rawObject = rawResult && typeof rawResult === "object" && !Array.isArray(rawResult) ? rawResult : {};
614
- const nestedResult = rawObject.result && typeof rawObject.result === "object" && !Array.isArray(rawObject.result) ? rawObject.result : {};
615
- const pending = rawObject.pending_operation_id ?? nestedResult.pending_operation_id;
616
- const approval = view === "operation" && typeof pending === "string" && !args.pending_operation_id ? { arguments: { ...args } } : void 0;
617
- return {
618
- content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
619
- ...view ? { structuredContent: structuredResult(tool.name, view, structured, approval) } : tool.output ? { structuredContent: structured } : {}
620
- };
621
- } catch (err) {
622
- const payload = provenance(
623
- err instanceof AdportError ? { ...err.toJSON(), ...err.code === "NOT_CONNECTED" && notConnectedMessage ? { message: notConnectedMessage } : {} } : {
624
- error: "INTERNAL",
625
- // Unexpected exceptions can contain credentials or private paths.
626
- // Do not imply that a failed response means a write was not applied.
627
- message: "Adport could not complete this request. If this was a write, check its status before retrying. Contact support if the problem persists."
628
- }
629
- );
630
- return {
631
- content: [{ type: "text", text: JSON.stringify(payload, null, 2) }],
632
- isError: true
633
- };
634
- }
635
- };
636
- if (view) {
637
- registerAppTool(server, tool.name, {
638
- ...config,
639
- _meta: {
640
- ui: { resourceUri: ADPORT_UI_URI, visibility: ["model", "app"] },
641
- "openai/widgetAccessible": true,
642
- "openai/outputTemplate": ADPORT_UI_URI,
643
- "openai/toolInvocation/invoking": labels.invoking,
644
- "openai/toolInvocation/invoked": labels.invoked
645
- }
646
- }, callback);
647
- } else {
648
- server.registerTool(tool.name, config, callback);
649
- }
650
- }
651
- return server;
652
- }
653
- async function runStdioServer(runtime, version) {
654
- const server = createMcpServer({ runtime, version });
655
- await server.connect(new StdioServerTransport());
656
- console.error(`adport MCP server on stdio (${runtime.registry.list().length} tools, policy: ${runtime.policySource})`);
657
- if (runtime.ctx.providers.list().length === 0) {
658
- console.error("No ad providers connected. Run `adport connect <provider>` or restart with `--demo` for synthetic mock data.");
659
- }
660
- }
661
-
662
- export {
663
- PROVIDER_IDS,
664
- assembleRuntime,
665
- createMcpServer,
666
- runStdioServer
667
- };
668
- //# sourceMappingURL=chunk-WN2KEUAP.js.map