@vpxa/aikit 0.1.331 → 0.1.333
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/package.json +1 -2
- package/packages/browser/dist/index.js +1 -1
- package/packages/server/dist/bin.js +1 -1
- package/packages/server/dist/index.js +1 -1
- package/packages/server/dist/{server-BlN0Hbye.js → server-CijzehmX.js} +1 -1
- package/packages/server/dist/{server-BPpxMa6G.js → server-CmZQTw9I.js} +1 -1
- package/packages/server/dist/{server-http-BPeLCfm4.js → server-http-BSr-jLTL.js} +1 -1
- package/packages/server/dist/{server-http-D0sBtn4y.js → server-http-DMSGWnQe.js} +1 -1
- package/packages/server/dist/{server-stdio-uwA3lNY2.js → server-stdio-BkUtsb3W.js} +1 -1
- package/packages/server/dist/{server-stdio-C24ENRdr.js → server-stdio-DM3dQOzx.js} +1 -1
- package/scaffold/dist/adapters/skills.mjs +1 -1
- package/packages/viewers/README.md +0 -798
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vpxa/aikit",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.333",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Local-first AI developer toolkit — knowledge base, code analysis, context management, and developer tools for LLM agents",
|
|
6
6
|
"license": "MIT",
|
|
@@ -47,7 +47,6 @@
|
|
|
47
47
|
"!scaffold/adapters/",
|
|
48
48
|
"!scaffold/generated/",
|
|
49
49
|
"!packages/viewers/",
|
|
50
|
-
"packages/viewers/dist/*.html",
|
|
51
50
|
"!scaffold/README.md",
|
|
52
51
|
"!prompt-analysis-output/",
|
|
53
52
|
"!reports/",
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{exec as e,execFile as t,execFileSync as n,execSync as r}from"node:child_process";import{existsSync as i,mkdirSync as a,readFileSync as o,unlinkSync as s,writeFileSync as c}from"node:fs";import{homedir as l,platform as u,tmpdir as d}from"node:os";import{join as f}from"node:path";import{z as p}from"zod";const m=`0.26.0`,h=`.agent-browser-version`;function g(e){return typeof e==`string`?e.trim():e instanceof Buffer?e.toString(`utf8`).trim():``}function _(){return f(l(),`.aikit`,`browsers`)}function v(e){return e.browsersPath??process.env.AIKIT_BROWSER_PATH??process.env.AIKIT_BROWSER_BROWSERS_PATH??_()}function y(){try{let e=r(`${u()===`win32`?`where`:`which`} agent-browser`,{encoding:`utf8`,stdio:`pipe`,timeout:3e3}).trim().split(/\r?\n/)[0];if(!e)return null;if(u()===`win32`){if(i(`${e}.cmd`))return`${e}.cmd`;if(i(`${e}.bat`))return`${e}.bat`;if(i(`${e}.exe`))return`${e}.exe`}return i(e)?e:null}catch{return null}}function ee(e){return f(e,`agent-browser`,te())}function te(){switch(u()){case`win32`:return`agent-browser.exe`;default:return`agent-browser`}}function ne(e,t){try{c(f(e,h),t,`utf8`)}catch{}}function re(e){try{return o(f(e,h),`utf8`).trim()}catch{return null}}function ie(e){let t=re(e);return t?ae(t,m)>=0:!1}function ae(e,t){let n=e.split(`.`).map(Number),r=t.split(`.`).map(Number);for(let e=0;e<Math.max(n.length,r.length);e++){let t=n[e]||0,i=r[e]||0;if(t!==i)return t-i}return 0}function oe(e){return!!(i(f(e,`chrome`))||i(f(l(),`.cache`,`agent-browser`,`chrome`)))}async function b(e,t){let n=y();if(n)return t?.(`Using globally installed agent-browser`),n;let o=v(e);i(o)||a(o,{recursive:!0});let s=ee(o);if(i(s)&&ie(o))return t?.(`Using cached agent-browser at ${s}`),s;t?.(`Downloading agent-browser (first use)...`);try{r(`npx -y agent-browser@latest install --install-dir "${o}"`,{stdio:`pipe`,timeout:12e4,encoding:`utf8`,env:{...process.env,AGENT_BROWSER_CACHE_DIR:o}})}catch(e){let t=e,n=g(t.stderr)||g(t.stdout)||t.message||`Unknown agent-browser install failure`;throw Error(`Failed to install agent-browser: ${n}`)}let c=s;try{let e=r(`npx -y -p agent-browser which agent-browser 2>/dev/null || true`,{encoding:`utf8`,timeout:1e4,stdio:`pipe`}).trim();e&&i(e)&&(c=e,ne(o,m))}catch{try{let e=r(`npx -y agent-browser --version`,{encoding:`utf8`,timeout:1e4,stdio:`pipe`}).trim();e&&ne(o,e)}catch{}}return t?.(`agent-browser ready`),c}async function se(e){try{return n(e,[`--version`],{encoding:`utf8`,stdio:`pipe`,timeout:5e3}).trim()}catch{return null}}function ce(e){if(y())return!0;let t=v(e);return oe(t)||i(ee(t))}const le=[`ui`,`headless`];function x(e){return le.includes(e)?e:`headless`}function S(e){let t=[];return e===`headless`&&t.push(`--headless`),t}function ue(e){return{headless:e===`headless`,args:S(e)}}var C=class{tabs=new Map;labelToId=new Map;register(e,t,n,r){return this.tabs.set(e,{tabId:e,url:t,title:n??t,label:r,createdAt:new Date}),r&&this.labelToId.set(r,e),e}resolve(e){return this.tabs.has(e)?e:this.labelToId.get(e)||e}getTab(e){let t=this.tabs.get(e);if(!t)throw Error(`Tab not found: ${e}`);return t}updateTabInfo(e,t,n){let r=this.tabs.get(e);r&&this.tabs.set(e,{...r,url:t,title:n})}setSnapshot(e,t){let n=this.tabs.get(e);n&&this.tabs.set(e,{...n,snapshot:t})}getSnapshot(e){return this.tabs.get(e)?.snapshot}async removeTab(e){let t=this.tabs.get(e);t&&(t.label&&this.labelToId.delete(t.label),this.tabs.delete(e))}listTabs(){return[...this.tabs.values()]}listPages(){return this.listTabs().map(e=>({pageId:e.tabId,url:e.url,title:e.title,label:e.label,createdAt:e.createdAt}))}async closeAll(){this.tabs.clear(),this.labelToId.clear()}clear(){this.tabs.clear(),this.labelToId.clear()}get size(){return this.tabs.size}},de=class{tabs=new C;registerPage(e,t,n,r){return this.tabs.register(e,t,n,r)}getPage(e){throw Error(`Direct Playwright Page access no longer supported with agent-browser. Use engine.exec() or engine.tabExec() for browser operations.`)}getPageInfo(e){let t=this.tabs.getTab(e);return{pageId:t.tabId,url:t.url,title:t.title,label:t.label,createdAt:t.createdAt}}resolvePageId(e){return this.tabs.resolve(e)}setSnapshot(e,t){this.tabs.setSnapshot(e,t)}getSnapshot(e){return this.tabs.getSnapshot(e)}updatePageInfo(e,t,n){this.tabs.updateTabInfo(e,t,n)}async removePage(e){await this.tabs.removeTab(e)}listPages(){return this.tabs.listPages()}async closeAll(){await this.tabs.closeAll()}get size(){return this.tabs.size}};const w={defaultMode:`ui`,browsersPath:null,userDataDirRoot:null,idleShutdownMinutes:10,allowInternalSchemes:!1,allowLoopback:!1,evalTimeoutMs:1e4,evalMaxResultBytes:262144,redactPasswordFieldsInScreenshots:!0,engine:`chrome`};var T=class{binaryPath=null;_currentMode=null;_isLaunched=!1;idleTimer=null;tabs=new C;config;constructor(e={}){this.config={...w,...e}}get currentMode(){return this._currentMode}async launch(e,t){if(!this._isLaunched){this._currentMode=x(e??this.config.defaultMode),this.binaryPath=await b(this.config,t);try{let e=await this.exec(`--version`);t?.(`agent-browser ${e.stdout.trim()}`)}catch(e){throw Error(`agent-browser binary failed: ${e instanceof Error?e.message:String(e)}`)}this._isLaunched=!0,this.resetIdleTimer()}}async exec(e,...t){if(!this.binaryPath)throw Error(`agent-browser not launched. Call launch() first, or use getEngine() singleton.`);let n=this.binaryPath;u()===`win32`&&!n.endsWith(`.cmd`)&&!n.endsWith(`.bat`)&&!n.endsWith(`.exe`)&&i(`${n}.cmd`)&&(n=`${n}.cmd`);let r=[e,...t];try{return await this.execViaExecFile(n,r)}catch(e){if(u()===`win32`&&(n.endsWith(`.cmd`)||n.endsWith(`.bat`))&&e?.code===`EINVAL`){let e=`"${n}" ${r.map(e=>/[^\w/:@.\-_{}()/\\]/.test(e)?`"${e.replace(/"/g,`""`)}"`:e).join(` `)}`;return await this.execViaShell(e)}throw e}}execViaExecFile(e,n){return new Promise((r,i)=>{t(e,n,{timeout:this.config.evalTimeoutMs},(e,t,n)=>{if(e){if(e.code===`ENOENT`){i(Error(`agent-browser binary not found at ${this.binaryPath}`));return}r({stdout:t??``,stderr:n??``,exitCode:e.code?Number(e.code):0});return}r({stdout:t??``,stderr:n??``,exitCode:0})})})}execViaShell(t){return new Promise((n,r)=>{e(t,{timeout:this.config.evalTimeoutMs},(e,t,i)=>{if(e&&e.code===`ENOENT`){r(Error(`agent-browser binary not found at ${this.binaryPath}`));return}n({stdout:t??``,stderr:i??``,exitCode:e?.code?Number(e.code):0})})})}async tabExec(e,t,...n){return this.exec(`tab`,e,t,...n)}async open(e,t,n){await this.launch(t);let r=await this.exec(`open`,e);return this.tabs.register(`t0`,e,r.stdout.trim(),n),this.resetIdleTimer(),`t0`}async screenshot(e,t){let n=t?.format??`png`,r=n===`jpeg`?`image/jpeg`:`image/png`,i=n===`jpeg`?`jpg`:`png`,a=f(d(),`aikit-screenshot-${Date.now()}.${i}`),c=[a];t?.fullPage&&c.push(`--full`),t?.format===`jpeg`&&c.push(`--screenshot-format`,`jpeg`),t?.quality&&c.push(`--screenshot-quality`,String(t.quality)),await this.exec(`screenshot`,...c);let l=o(a),u=l.toString(`base64`);try{s(a)}catch{}return this.resetIdleTimer(),{base64:u,mimeType:r,bytes:l.length}}async readSnapshot(e,t=!1){let n=t?[`-c`,`--json`]:[`-i`,`--json`],r=await this.tabExec(e,`snapshot`,...n);return this.resetIdleTimer(),r.stdout}async navigate(e,t){let n=await this.tabExec(e,`goto`,t);return this.resetIdleTimer(),n}async close(){if(this.stopIdleTimer(),this._isLaunched){try{await this.exec(`close`).catch(()=>{})}catch{}this.tabs.clear(),this._isLaunched=!1,this._currentMode=null}}isLaunched(){return this._isLaunched}getConfig(){return this.config}getBinaryPath(){return this.binaryPath}resolvePageId(e){return this.tabs.resolve(e)}resetIdleTimer(){this.stopIdleTimer();let e=setTimeout(()=>{this.close()},this.config.idleShutdownMinutes*6e4);e.unref?.(),this.idleTimer=e}stopIdleTimer(){this.idleTimer&&=(clearTimeout(this.idleTimer),null)}};let E=null;function D(e){return E||=new T(e),E}async function fe(){E&&=(await E.close(),null)}function O(e,t){try{let n=new URL(e),r=n.hostname.toLowerCase(),i=n.protocol.toLowerCase();return[`file:`,`chrome:`,`chrome-extension:`,`data:`,`javascript:`].includes(i)?t?.allowInternalSchemes?{allowed:!0}:{allowed:!1,reason:`Scheme '${i}' is blocked for security`}:[`169.254.169.254`,`metadata.google.internal`,`metadata.instance`].includes(r)?{allowed:!1,reason:`Cloud metadata endpoint '${r}' is blocked`}:n.hostname===`localhost`||r===`127.0.0.1`||r===`::1`||r.startsWith(`10.`)||r.startsWith(`172.`)||r.startsWith(`192.168.`)?t?.allowLoopback?{allowed:!0}:{allowed:!1,reason:`Internal host '${r}' requires allowLoopback=true`}:{allowed:!0}}catch{return{allowed:!1,reason:`Invalid URL`}}}function k(e,t){return Buffer.byteLength(e,`utf8`)<=t?{valid:!0,result:e,truncated:!1}:{valid:!0,result:`${Buffer.from(e).subarray(0,t).toString(`utf8`)}\n... [truncated]`,truncated:!0,reason:`Result exceeded ${t} bytes, truncated to ${t} bytes`}}const pe=[`ui`,`headless`];function me(e){return typeof e==`object`&&e?e:null}function A(e){return typeof e==`string`&&e.length>0?e:void 0}function j(e){return typeof e==`boolean`?e:void 0}function M(e){return typeof e==`number`&&Number.isFinite(e)?e:void 0}function N(e){return typeof e==`string`&&pe.includes(e)?e:void 0}function P(e){let t=process.env[e];if(!t)return;let n=Number(t);return Number.isFinite(n)?n:void 0}function he(e){let t=me(e.browser)??{};return{...w,defaultMode:N(process.env.AIKIT_BROWSER_DEFAULT_MODE)??N(t.defaultMode)??w.defaultMode,browsersPath:A(process.env.AIKIT_BROWSER_PATH)??A(process.env.AIKIT_BROWSER_BROWSERS_PATH)??A(t.browsersPath)??w.browsersPath,userDataDirRoot:A(t.userDataDirRoot)??w.userDataDirRoot,idleShutdownMinutes:P(`AIKIT_BROWSER_IDLE_MINUTES`)??M(t.idleShutdownMinutes)??w.idleShutdownMinutes,allowInternalSchemes:j(t.allowInternalSchemes)??w.allowInternalSchemes,allowLoopback:j(t.allowLoopback)??w.allowLoopback,evalTimeoutMs:P(`AIKIT_BROWSER_EVAL_TIMEOUT_MS`)??M(t.evalTimeoutMs)??w.evalTimeoutMs,evalMaxResultBytes:M(t.evalMaxResultBytes)??w.evalMaxResultBytes,redactPasswordFieldsInScreenshots:j(t.redactPasswordFieldsInScreenshots)??w.redactPasswordFieldsInScreenshots,engine:A(t.engine)??w.engine,proxy:A(t.proxy),viewport:A(t.viewport)}}function F(e,t){return{content:[{type:`text`,text:e}],structuredContent:t}}function I(e,t){let n=e.selector??e.ref??(e.element?`text=${e.element}`:void 0);if(!n)throw Error(`${t} requires selector, ref, or element`);return n}function ge(e){return async({pageId:t,kind:n,ref:r,selector:i,element:a,text:o,key:s,value:c,fromRef:l,fromSelector:u,toRef:d,toSelector:f})=>{let p=D(e),m=p.resolvePageId(t);switch(n){case`click`:{let e=I({ref:r,selector:i,element:a},`browser_act(click)`);await p.tabExec(m,`click`,e);break}case`type`:{let e=I({ref:r,selector:i,element:a},`browser_act(type)`);await p.tabExec(m,`fill`,e,o??``);break}case`press`:{let e=I({ref:r,selector:i,element:a},`browser_act(press)`);if(!s)throw Error(`browser_act(press) requires key`);await p.tabExec(m,`press`,e,s);break}case`hover`:{let e=I({ref:r,selector:i,element:a},`browser_act(hover)`);await p.tabExec(m,`hover`,e);break}case`drag`:{let e=I({ref:l??r,selector:u??i,element:a},`browser_act(drag) source`),t=I({ref:d,selector:f??c,element:a},`browser_act(drag) target`);await p.tabExec(m,`drag`,e,t);break}case`select`:{let e=I({ref:r,selector:i,element:a},`browser_act(select)`);if(c===void 0)throw Error(`browser_act(select) requires value`);await p.tabExec(m,`select`,e,c);break}case`scroll`:{if(i||r||a){let e=I({ref:r,selector:i,element:a},`browser_act(scroll)`);await p.tabExec(m,`scrollintoview`,e);break}let e=c??`down 500`;await p.tabExec(m,`scroll`,e);break}case`upload`:{let e=I({ref:r,selector:i,element:a},`browser_act(upload)`);if(!c)throw Error(`value (file path) required for upload`);await p.tabExec(m,`upload`,e,c);break}}return p.resetIdleTimer(),F(`ok`,{ok:!0})}}function _e(e){return async({pageId:t,subAction:n,level:r,bufferSize:i})=>{let a=D(e),o=a.resolvePageId(t);switch(n){case`enable`:{let e=[`--enable`];return i&&e.push(`--buffer-size`,String(i)),r&&e.push(`--level`,r),await a.tabExec(o,`console`,...e),a.resetIdleTimer(),F(`Console capture enabled`,{enabled:!0})}case`get`:{let e=[`--json`];r&&e.push(`--level`,r);let t=await a.tabExec(o,`console`,...e);a.resetIdleTimer();let n=[];try{n=JSON.parse(t.stdout)}catch{n=[{raw:t.stdout}]}return F(`Console has ${n.length} entries`,{entries:n,count:n.length})}case`clear`:return await a.tabExec(o,`console`,`clear`),a.resetIdleTimer(),F(`Console cleared`,{cleared:!0});default:return F(`Unknown console sub-action`,{error:`Unknown console sub-action`})}}}function ve(e){return async({pageId:t,accept:n,promptText:r})=>{let i=D(e),a=i.resolvePageId(t);if(n){let e=r?[`accept`,`--text`,r]:[`accept`];await i.tabExec(a,`dialog`,...e)}else await i.tabExec(a,`dialog`,`dismiss`);return i.resetIdleTimer(),F(`Dialog ${n?`accepted`:`dismissed`}`,{accepted:n})}}function ye(e){return async({pageId:t})=>{let n=D(e),r=n.resolvePageId(t),i=await n.tabExec(r,`diff`,`snapshot`);return n.resetIdleTimer(),F(i.stdout,{pageId:r,diff:i.stdout})}}function be(e){return async({pageId:t,code:n,timeoutMs:r})=>{let i=D(e),a=i.resolvePageId(t),o=r??e.evalTimeoutMs,s=await i.tabExec(a,`evaluate`,`--timeout=${o}`,`--`,n);i.resetIdleTimer();let c=k(s.stdout,e.evalMaxResultBytes);return F(c.result,{pageId:a,result:c.result,truncated:c.truncated})}}function xe(e){return async({pageId:t,url:n,method:r,headers:i,body:a,timeoutMs:o,includeHeaders:s})=>{let c=D(e),l=c.resolvePageId(t),u=o??e.evalTimeoutMs,d=Se(n,r??`GET`,s??!0,i,a),f=await c.tabExec(l,`evaluate`,`--timeout=${u}`,`--`,d);return c.resetIdleTimer(),F(f.stdout,{pageId:l,response:f.stdout})}}function Se(e,t,n,r,i){let a={method:t,headers:r??{}};i&&[`POST`,`PUT`,`PATCH`].includes(t.toUpperCase())&&(a.body=i);let o=JSON.stringify(a);return n?`fetch(${JSON.stringify(e)}, ${o}).then(async r => {
|
|
1
|
+
import{exec as e,execFile as t,execFileSync as n,execSync as r}from"node:child_process";import{existsSync as i,mkdirSync as a,readFileSync as o,unlinkSync as s,writeFileSync as c}from"node:fs";import{homedir as l,platform as u,tmpdir as d}from"node:os";import{join as f}from"node:path";import{z as p}from"zod";const m=`0.26.0`,h=`.agent-browser-version`;function g(e){return typeof e==`string`?e.trim():e instanceof Buffer?e.toString(`utf8`).trim():``}function _(){return f(l(),`.aikit`,`browsers`)}function v(e){return e.browsersPath??process.env.AIKIT_BROWSER_PATH??process.env.AIKIT_BROWSER_BROWSERS_PATH??_()}function y(){try{let e=r(`${u()===`win32`?`where`:`which`} agent-browser`,{encoding:`utf8`,stdio:`pipe`,timeout:3e3}).trim().split(/\r?\n/)[0];if(!e)return null;if(u()===`win32`){if(i(`${e}.cmd`))return`${e}.cmd`;if(i(`${e}.bat`))return`${e}.bat`;if(i(`${e}.exe`))return`${e}.exe`}return i(e)?e:null}catch{return null}}function ee(e){return f(e,`agent-browser`,te())}function te(){switch(u()){case`win32`:return`agent-browser.exe`;default:return`agent-browser`}}function ne(e,t){try{c(f(e,h),t,`utf8`)}catch{}}function re(e){try{return o(f(e,h),`utf8`).trim()}catch{return null}}function ie(e){let t=re(e);return t?ae(t,m)>=0:!1}function ae(e,t){let n=e.split(`.`).map(Number),r=t.split(`.`).map(Number);for(let e=0;e<Math.max(n.length,r.length);e++){let t=n[e]||0,i=r[e]||0;if(t!==i)return t-i}return 0}function oe(e){return!!(i(f(e,`chrome`))||i(f(l(),`.cache`,`agent-browser`,`chrome`)))}async function b(e,t){let n=y();if(n)return t?.(`Using globally installed agent-browser`),n;let o=v(e);i(o)||a(o,{recursive:!0});let s=ee(o);if(i(s)&&ie(o))return t?.(`Using cached agent-browser at ${s}`),s;t?.(`Downloading agent-browser (first use)...`);try{r(`npx -y agent-browser@latest install --install-dir "${o}"`,{stdio:`pipe`,timeout:12e4,encoding:`utf8`,env:{...process.env,AGENT_BROWSER_CACHE_DIR:o}})}catch(e){let t=e,n=g(t.stderr)||g(t.stdout)||t.message||`Unknown agent-browser install failure`;throw Error(`Failed to install agent-browser: ${n}`)}let c=s;try{let e=r(`npx -y -p agent-browser which agent-browser 2>/dev/null || true`,{encoding:`utf8`,timeout:1e4,stdio:`pipe`}).trim();e&&i(e)&&(c=e,ne(o,m))}catch{try{let e=r(`npx -y agent-browser --version`,{encoding:`utf8`,timeout:1e4,stdio:`pipe`}).trim();e&&ne(o,e)}catch{}}return t?.(`agent-browser ready`),c}async function se(e){try{return n(e,[`--version`],{encoding:`utf8`,stdio:`pipe`,timeout:5e3}).trim()}catch{return null}}function ce(e){if(y())return!0;let t=v(e);return oe(t)||i(ee(t))}const le=[`ui`,`headless`];function x(e){return le.includes(e)?e:`headless`}function S(e){let t=[];return e===`headless`&&t.push(`--headless`),t}function ue(e){return{headless:e===`headless`,args:S(e)}}var C=class{tabs=new Map;labelToId=new Map;register(e,t,n,r){return this.tabs.set(e,{tabId:e,url:t,title:n??t,label:r,createdAt:new Date}),r&&this.labelToId.set(r,e),e}resolve(e){return this.tabs.has(e)?e:this.labelToId.get(e)||e}getTab(e){let t=this.tabs.get(e);if(!t)throw Error(`Tab not found: ${e}`);return t}updateTabInfo(e,t,n){let r=this.tabs.get(e);r&&this.tabs.set(e,{...r,url:t,title:n})}setSnapshot(e,t){let n=this.tabs.get(e);n&&this.tabs.set(e,{...n,snapshot:t})}getSnapshot(e){return this.tabs.get(e)?.snapshot}async removeTab(e){let t=this.tabs.get(e);t&&(t.label&&this.labelToId.delete(t.label),this.tabs.delete(e))}listTabs(){return[...this.tabs.values()]}listPages(){return this.listTabs().map(e=>({pageId:e.tabId,url:e.url,title:e.title,label:e.label,createdAt:e.createdAt}))}async closeAll(){this.tabs.clear(),this.labelToId.clear()}clear(){this.tabs.clear(),this.labelToId.clear()}get size(){return this.tabs.size}},de=class{tabs=new C;registerPage(e,t,n,r){return this.tabs.register(e,t,n,r)}getPage(e){throw Error(`Direct Playwright Page access no longer supported with agent-browser. Use engine.exec() or engine.tabExec() for browser operations.`)}getPageInfo(e){let t=this.tabs.getTab(e);return{pageId:t.tabId,url:t.url,title:t.title,label:t.label,createdAt:t.createdAt}}resolvePageId(e){return this.tabs.resolve(e)}setSnapshot(e,t){this.tabs.setSnapshot(e,t)}getSnapshot(e){return this.tabs.getSnapshot(e)}updatePageInfo(e,t,n){this.tabs.updateTabInfo(e,t,n)}async removePage(e){await this.tabs.removeTab(e)}listPages(){return this.tabs.listPages()}async closeAll(){await this.tabs.closeAll()}get size(){return this.tabs.size}};const w={defaultMode:`ui`,browsersPath:null,userDataDirRoot:null,idleShutdownMinutes:10,allowInternalSchemes:!1,allowLoopback:!1,evalTimeoutMs:3e4,evalMaxResultBytes:262144,redactPasswordFieldsInScreenshots:!0,engine:`chrome`};var T=class{binaryPath=null;_currentMode=null;_isLaunched=!1;idleTimer=null;tabs=new C;config;constructor(e={}){this.config={...w,...e}}get currentMode(){return this._currentMode}async launch(e,t){let n=x(e??this.config.defaultMode);if(!(this._isLaunched&&this._currentMode===n)){this._isLaunched&&this._currentMode!==n&&await this.close(),this._currentMode=n,this.binaryPath=await b(this.config,t);try{let e=await this.exec(`--version`);t?.(`agent-browser ${e.stdout.trim()}`)}catch(e){throw Error(`agent-browser binary failed: ${e instanceof Error?e.message:String(e)}`)}this._isLaunched=!0,this.resetIdleTimer()}}async exec(e,...t){if(!this.binaryPath)throw Error(`agent-browser not launched. Call launch() first, or use getEngine() singleton.`);let n=this.binaryPath;u()===`win32`&&!n.endsWith(`.cmd`)&&!n.endsWith(`.bat`)&&!n.endsWith(`.exe`)&&i(`${n}.cmd`)&&(n=`${n}.cmd`);let r=[e,...t];if(u()===`win32`&&(n.endsWith(`.cmd`)||n.endsWith(`.bat`))){let e=`"${n}" ${r.map(e=>/[^\w/:@.\-_{}()/\\]/.test(e)?`"${e.replace(/"/g,`""`)}"`:e).join(` `)}`;return await this.execViaShell(e)}return await this.execViaExecFile(n,r)}execViaExecFile(e,n){return new Promise((r,i)=>{t(e,n,{timeout:this.config.evalTimeoutMs},(t,a,o)=>{if(t){let s=t;if(s.code===`ENOENT`){i(Error(`agent-browser binary not found at ${this.binaryPath}`));return}if(s.killed||s.signal){i(Error(`agent-browser command timed out after ${this.config.evalTimeoutMs}ms: ${e} ${n.join(` `)}`));return}r({stdout:a??``,stderr:o??``,exitCode:typeof s.code==`number`?s.code:1});return}r({stdout:a??``,stderr:o??``,exitCode:0})}).stdin?.end()})}execViaShell(t){return new Promise((n,r)=>{e(t,{timeout:this.config.evalTimeoutMs},(e,i,a)=>{if(e){let o=e;if(o.code===`ENOENT`){r(Error(`agent-browser binary not found at ${this.binaryPath}`));return}if(o.killed||o.signal){r(Error(`agent-browser command timed out after ${this.config.evalTimeoutMs}ms: ${t}`));return}n({stdout:i??``,stderr:a??``,exitCode:typeof o.code==`number`?o.code:1});return}n({stdout:i??``,stderr:a??``,exitCode:0})})})}async tabExec(e,t,...n){return this.exec(`tab`,e,t,...n)}async open(e,t,n){await this.launch(t);let r=this._currentMode===`ui`?[`open`,e,`--headed`]:[`open`,e],i=await this.exec(...r);if(i.exitCode!==0)throw Error(`agent-browser failed to open URL (exit ${i.exitCode}): ${i.stderr||i.stdout||`Unknown error`}`);return this.tabs.register(`t0`,e,i.stdout.trim(),n),this.resetIdleTimer(),`t0`}async screenshot(e,t){let n=t?.format??`png`,r=n===`jpeg`?`image/jpeg`:`image/png`,i=n===`jpeg`?`jpg`:`png`,a=f(d(),`aikit-screenshot-${Date.now()}.${i}`),c=[a];t?.fullPage&&c.push(`--full`),t?.format===`jpeg`&&c.push(`--screenshot-format`,`jpeg`),t?.quality&&c.push(`--screenshot-quality`,String(t.quality)),await this.exec(`screenshot`,...c);let l=o(a),u=l.toString(`base64`);try{s(a)}catch{}return this.resetIdleTimer(),{base64:u,mimeType:r,bytes:l.length}}async readSnapshot(e,t=!1){let n=t?[`-c`,`--json`]:[`-i`,`--json`],r=await this.tabExec(e,`snapshot`,...n);return this.resetIdleTimer(),r.stdout}async navigate(e,t){let n=await this.tabExec(e,`goto`,t);return this.resetIdleTimer(),n}async close(){if(this.stopIdleTimer(),this._isLaunched){try{await this.exec(`close`).catch(()=>{})}catch{}this.tabs.clear(),this._isLaunched=!1,this._currentMode=null}}isLaunched(){return this._isLaunched}getConfig(){return this.config}getBinaryPath(){return this.binaryPath}resolvePageId(e){return this.tabs.resolve(e)}resetIdleTimer(){this.stopIdleTimer();let e=setTimeout(()=>{this.close()},this.config.idleShutdownMinutes*6e4);e.unref?.(),this.idleTimer=e}stopIdleTimer(){this.idleTimer&&=(clearTimeout(this.idleTimer),null)}};let E=null;function D(e){return E||=new T(e),E}async function fe(){E&&=(await E.close(),null)}function O(e,t){try{let n=new URL(e),r=n.hostname.toLowerCase(),i=n.protocol.toLowerCase();return[`file:`,`chrome:`,`chrome-extension:`,`data:`,`javascript:`].includes(i)?t?.allowInternalSchemes?{allowed:!0}:{allowed:!1,reason:`Scheme '${i}' is blocked for security`}:[`169.254.169.254`,`metadata.google.internal`,`metadata.instance`].includes(r)?{allowed:!1,reason:`Cloud metadata endpoint '${r}' is blocked`}:n.hostname===`localhost`||r===`127.0.0.1`||r===`::1`||r.startsWith(`10.`)||r.startsWith(`172.`)||r.startsWith(`192.168.`)?t?.allowLoopback?{allowed:!0}:{allowed:!1,reason:`Internal host '${r}' requires allowLoopback=true`}:{allowed:!0}}catch{return{allowed:!1,reason:`Invalid URL`}}}function k(e,t){return Buffer.byteLength(e,`utf8`)<=t?{valid:!0,result:e,truncated:!1}:{valid:!0,result:`${Buffer.from(e).subarray(0,t).toString(`utf8`)}\n... [truncated]`,truncated:!0,reason:`Result exceeded ${t} bytes, truncated to ${t} bytes`}}const pe=[`ui`,`headless`];function me(e){return typeof e==`object`&&e?e:null}function A(e){return typeof e==`string`&&e.length>0?e:void 0}function j(e){return typeof e==`boolean`?e:void 0}function M(e){return typeof e==`number`&&Number.isFinite(e)?e:void 0}function N(e){return typeof e==`string`&&pe.includes(e)?e:void 0}function P(e){let t=process.env[e];if(!t)return;let n=Number(t);return Number.isFinite(n)?n:void 0}function he(e){let t=me(e.browser)??{};return{...w,defaultMode:N(process.env.AIKIT_BROWSER_DEFAULT_MODE)??N(t.defaultMode)??w.defaultMode,browsersPath:A(process.env.AIKIT_BROWSER_PATH)??A(process.env.AIKIT_BROWSER_BROWSERS_PATH)??A(t.browsersPath)??w.browsersPath,userDataDirRoot:A(t.userDataDirRoot)??w.userDataDirRoot,idleShutdownMinutes:P(`AIKIT_BROWSER_IDLE_MINUTES`)??M(t.idleShutdownMinutes)??w.idleShutdownMinutes,allowInternalSchemes:j(t.allowInternalSchemes)??w.allowInternalSchemes,allowLoopback:j(t.allowLoopback)??w.allowLoopback,evalTimeoutMs:P(`AIKIT_BROWSER_EVAL_TIMEOUT_MS`)??M(t.evalTimeoutMs)??w.evalTimeoutMs,evalMaxResultBytes:M(t.evalMaxResultBytes)??w.evalMaxResultBytes,redactPasswordFieldsInScreenshots:j(t.redactPasswordFieldsInScreenshots)??w.redactPasswordFieldsInScreenshots,engine:A(t.engine)??w.engine,proxy:A(t.proxy),viewport:A(t.viewport)}}function F(e,t){return{content:[{type:`text`,text:e}],structuredContent:t}}function I(e,t){let n=e.selector??e.ref??(e.element?`text=${e.element}`:void 0);if(!n)throw Error(`${t} requires selector, ref, or element`);return n}function ge(e){return async({pageId:t,kind:n,ref:r,selector:i,element:a,text:o,key:s,value:c,fromRef:l,fromSelector:u,toRef:d,toSelector:f})=>{let p=D(e),m=p.resolvePageId(t);switch(n){case`click`:{let e=I({ref:r,selector:i,element:a},`browser_act(click)`);await p.tabExec(m,`click`,e);break}case`type`:{let e=I({ref:r,selector:i,element:a},`browser_act(type)`);await p.tabExec(m,`fill`,e,o??``);break}case`press`:{let e=I({ref:r,selector:i,element:a},`browser_act(press)`);if(!s)throw Error(`browser_act(press) requires key`);await p.tabExec(m,`press`,e,s);break}case`hover`:{let e=I({ref:r,selector:i,element:a},`browser_act(hover)`);await p.tabExec(m,`hover`,e);break}case`drag`:{let e=I({ref:l??r,selector:u??i,element:a},`browser_act(drag) source`),t=I({ref:d,selector:f??c,element:a},`browser_act(drag) target`);await p.tabExec(m,`drag`,e,t);break}case`select`:{let e=I({ref:r,selector:i,element:a},`browser_act(select)`);if(c===void 0)throw Error(`browser_act(select) requires value`);await p.tabExec(m,`select`,e,c);break}case`scroll`:{if(i||r||a){let e=I({ref:r,selector:i,element:a},`browser_act(scroll)`);await p.tabExec(m,`scrollintoview`,e);break}let e=c??`down 500`;await p.tabExec(m,`scroll`,e);break}case`upload`:{let e=I({ref:r,selector:i,element:a},`browser_act(upload)`);if(!c)throw Error(`value (file path) required for upload`);await p.tabExec(m,`upload`,e,c);break}}return p.resetIdleTimer(),F(`ok`,{ok:!0})}}function _e(e){return async({pageId:t,subAction:n,level:r,bufferSize:i})=>{let a=D(e),o=a.resolvePageId(t);switch(n){case`enable`:{let e=[`--enable`];return i&&e.push(`--buffer-size`,String(i)),r&&e.push(`--level`,r),await a.tabExec(o,`console`,...e),a.resetIdleTimer(),F(`Console capture enabled`,{enabled:!0})}case`get`:{let e=[`--json`];r&&e.push(`--level`,r);let t=await a.tabExec(o,`console`,...e);a.resetIdleTimer();let n=[];try{n=JSON.parse(t.stdout)}catch{n=[{raw:t.stdout}]}return F(`Console has ${n.length} entries`,{entries:n,count:n.length})}case`clear`:return await a.tabExec(o,`console`,`clear`),a.resetIdleTimer(),F(`Console cleared`,{cleared:!0});default:return F(`Unknown console sub-action`,{error:`Unknown console sub-action`})}}}function ve(e){return async({pageId:t,accept:n,promptText:r})=>{let i=D(e),a=i.resolvePageId(t);if(n){let e=r?[`accept`,`--text`,r]:[`accept`];await i.tabExec(a,`dialog`,...e)}else await i.tabExec(a,`dialog`,`dismiss`);return i.resetIdleTimer(),F(`Dialog ${n?`accepted`:`dismissed`}`,{accepted:n})}}function ye(e){return async({pageId:t})=>{let n=D(e),r=n.resolvePageId(t),i=await n.tabExec(r,`diff`,`snapshot`);return n.resetIdleTimer(),F(i.stdout,{pageId:r,diff:i.stdout})}}function be(e){return async({pageId:t,code:n,timeoutMs:r})=>{let i=D(e),a=i.resolvePageId(t),o=r??e.evalTimeoutMs,s=await i.tabExec(a,`evaluate`,`--timeout=${o}`,`--`,n);i.resetIdleTimer();let c=k(s.stdout,e.evalMaxResultBytes);return F(c.result,{pageId:a,result:c.result,truncated:c.truncated})}}function xe(e){return async({pageId:t,url:n,method:r,headers:i,body:a,timeoutMs:o,includeHeaders:s})=>{let c=D(e),l=c.resolvePageId(t),u=o??e.evalTimeoutMs,d=Se(n,r??`GET`,s??!0,i,a),f=await c.tabExec(l,`evaluate`,`--timeout=${u}`,`--`,d);return c.resetIdleTimer(),F(f.stdout,{pageId:l,response:f.stdout})}}function Se(e,t,n,r,i){let a={method:t,headers:r??{}};i&&[`POST`,`PUT`,`PATCH`].includes(t.toUpperCase())&&(a.body=i);let o=JSON.stringify(a);return n?`fetch(${JSON.stringify(e)}, ${o}).then(async r => {
|
|
2
2
|
const text = await r.text();
|
|
3
3
|
const headers = {};
|
|
4
4
|
r.headers.forEach((v, k) => { headers[k] = v; });
|
|
@@ -5,4 +5,4 @@ import{fileURLToPath as e,pathToFileURL as t}from"node:url";import{parseArgs as
|
|
|
5
5
|
`).length,fileHash:this.hash(e.content),indexedAt:t,origin:`curated`,tags:e.frontmatter.tags,category:e.frontmatter.category,version:e.frontmatter.version}});try{return await this.store.upsert(i,r),e.length}catch(t){R.error(`Failed to upsert curated batch`,{batchSize:e.length,...a(t)});for(let t of e)n.push(`${t.relativePath}: upsert failed`);return 0}}catch(r){if(e.length===1)return R.error(`Failed to embed curated item`,{relativePath:e[0].relativePath,...a(r)}),n.push(`${e[0].relativePath}: reindex failed`),0;R.warn(`Curated embed batch failed, retrying with smaller chunks`,{batchSize:e.length,...a(r)});let i=Math.ceil(e.length/2),o=e.slice(0,i),s=e.slice(i);return await this.embedAndUpsertBatch(o,t,n)+await this.embedAndUpsertBatch(s,t,n)}}gitCommitKnowledge(e,t,n){try{if(!p(this.curatedDir))return;let r=this.knowledgeRefForPath(e);if(!r)return;m(r,`entry.md`,t,n,this.curatedDir)}catch{}}gitDeleteKnowledgeRef(e){try{if(!p(this.curatedDir))return;let t=this.knowledgeRefForPath(e);if(!t)return;h([`update-ref`,`-d`,t],this.curatedDir)}catch{}}knowledgeRefForPath(e){let t=e.replace(/\.md$/,``).split(`/`).map(e=>g(e)).join(`/`);return t.split(`/`).every(e=>f.test(e))?`${L}/${t}`:null}async indexCuratedFile(e,t,n){let r=await this.embedder.embed(t),i=`.ai/curated/${e}`,a=new Date().toISOString(),o={id:this.hashId(i,0),content:t,sourcePath:i,contentType:`curated-knowledge`,headingPath:n.title,chunkIndex:0,totalChunks:1,startLine:1,endLine:t.split(`
|
|
6
6
|
`).length,fileHash:this.hash(t),indexedAt:a,origin:`curated`,tags:n.tags,category:n.category,version:n.version};await this.store.upsert([o],[r])}async indexCuratedFileBestEffort(e,t,n,i){if(r.instance().isDegraded(`embedder`)){R.debug(`Skipping vector indexing — embedder degraded`,{relativePath:e,operation:i,subsystem:`embedder`});return}try{await this.indexCuratedFile(e,t,n)}catch(t){R.warn(`Curated file persisted but vector indexing deferred`,{relativePath:e,operation:i,...a(t)})}}async discoverCategories(){return this.adapter.listDirectories()}guardPath(e){let t=e.replace(/^\.ai\/curated\//,``);if(t.endsWith(`.md`)||(t+=`.md`),t.includes(`..`)||c(t))throw Error(`Invalid path: ${t}. Must be relative within .ai/curated/ directory.`);let n=t.split(`/`)[0];return this.validateCategoryName(n),t}validateCategoryName(e){if(!/^[a-z][a-z0-9-]*$/.test(e))throw Error(`Invalid category name: "${e}". Must be lowercase kebab-case (e.g., "decisions", "api-contracts").`)}validateContentSize(e){if(Buffer.byteLength(e,`utf-8`)>I)throw Error(`Content exceeds maximum size of ${I/1024}KB`)}slugify(e){return e.toLowerCase().replace(/[^a-z0-9]+/g,`-`).replace(/^-|-$/g,``).slice(0,80)}normalizeTags(e){return[...new Set(e.map(e=>e.trim()).filter(Boolean))]}sameTags(e,t){if(e.length!==t.length)return!1;let n=new Set(e);return t.every(e=>n.has(e))}ensureCategoryPath(e,t){if(!e.startsWith(`${t}/`))throw Error(`Curated path "${e}" must stay within category "${t}"`)}async uniqueRelativePath(e,t){let n=`${e}/${t}.md`;if(!await this.adapter.exists(n))return n;for(let n=2;n<=100;n++){let r=`${e}/${t}-${n}.md`;if(!await this.adapter.exists(r))return r}throw Error(`Too many entries with slug "${t}" in category "${e}"`)}hash(e){return d(`sha256`).update(e).digest(`hex`).slice(0,16)}hashId(e,t){return this.hash(`${e}::${t}`)}serializeFile(e,t){return`${[`---`,`title: "${t.title.replace(/"/g,`\\"`)}"`,`category: ${t.category}`,`tags: [${t.tags.map(e=>`"${e}"`).join(`, `)}]`,`created: ${t.created}`,`updated: ${t.updated}`,`version: ${t.version}`,`origin: ${t.origin}`,`changelog:`,...t.changelog.map(e=>` - version: ${e.version}\n date: ${e.date}\n reason: "${e.reason.replace(/"/g,`\\"`)}"`),`---`].join(`
|
|
7
7
|
`)}\n\n${e}\n`}parseFile(e){let t=e.match(/^---\n([\s\S]*?)\n---\n\n?([\s\S]*)$/);if(!t)return{frontmatter:{title:`Untitled`,category:`notes`,tags:[],created:``,updated:``,version:1,origin:`curated`,changelog:[]},content:e};let n=t[1],r=t[2].trim(),i={},a=[],o=n.split(`
|
|
8
|
-
`),s=!1,c={};for(let e of o){if(/^changelog:\s*$/.test(e)){s=!0;continue}if(s){let t=e.match(/^\s+-\s+version:\s*(\d+)$/);if(t){c.version!=null&&a.push(c),c={version:parseInt(t[1],10)};continue}let n=e.match(/^\s+date:\s*(.+)$/);if(n){c.date=n[1].trim();continue}let r=e.match(/^\s+reason:\s*"?(.*?)"?\s*$/);if(r){c.reason=r[1];continue}/^\w/.test(e)&&(s=!1,c.version!=null&&a.push(c),c={});continue}let t=e.match(/^(\w+):\s*(.*)$/);if(t){let e=t[1],n=t[2];typeof n==`string`&&n.startsWith(`[`)&&n.endsWith(`]`)?n=n.slice(1,-1).split(`,`).map(e=>e.trim().replace(/^"|"$/g,``)).filter(e=>e.length>0):typeof n==`string`&&/^\d+$/.test(n)?n=parseInt(n,10):typeof n==`string`&&n.startsWith(`"`)&&n.endsWith(`"`)&&(n=n.slice(1,-1)),i[e]=n}}return c.version!=null&&a.push(c),{frontmatter:{title:i.title??`Untitled`,category:i.category??`notes`,tags:i.tags??[],created:i.created??``,updated:i.updated??``,version:i.version??1,origin:`curated`,changelog:a},content:r}}};const B=i(`server`);function V(e,t){return t?{version:e,...a(t)}:{version:e}}function H(){return process.env.AIKIT_TRANSPORT?process.env.AIKIT_TRANSPORT:process.stdin.isTTY?`http`:`stdio`}function U(){let e=process.argv[1];if(!e)return!1;try{return import.meta.url===t(e).href}catch{return!1}}function W(){return U()?n({allowPositionals:!0,options:{transport:{type:`string`,default:H()},port:{type:`string`,default:process.env.AIKIT_PORT??`3210`}}}).values:{transport:H(),port:process.env.AIKIT_PORT??`3210`}}async function G(){let e=C(),t=W();if(process.on(`unhandledRejection`,t=>{B.error(`Unhandled rejection`,V(e,t))}),process.on(`uncaughtException`,t=>{B.error(`Uncaught exception — exiting`,V(e,t)),process.exit(1)}),B.info(`Starting MCP AI Kit server`,{version:e}),t.transport===`http`){let{startHttpMode:n}=await import(`./server-http-
|
|
8
|
+
`),s=!1,c={};for(let e of o){if(/^changelog:\s*$/.test(e)){s=!0;continue}if(s){let t=e.match(/^\s+-\s+version:\s*(\d+)$/);if(t){c.version!=null&&a.push(c),c={version:parseInt(t[1],10)};continue}let n=e.match(/^\s+date:\s*(.+)$/);if(n){c.date=n[1].trim();continue}let r=e.match(/^\s+reason:\s*"?(.*?)"?\s*$/);if(r){c.reason=r[1];continue}/^\w/.test(e)&&(s=!1,c.version!=null&&a.push(c),c={});continue}let t=e.match(/^(\w+):\s*(.*)$/);if(t){let e=t[1],n=t[2];typeof n==`string`&&n.startsWith(`[`)&&n.endsWith(`]`)?n=n.slice(1,-1).split(`,`).map(e=>e.trim().replace(/^"|"$/g,``)).filter(e=>e.length>0):typeof n==`string`&&/^\d+$/.test(n)?n=parseInt(n,10):typeof n==`string`&&n.startsWith(`"`)&&n.endsWith(`"`)&&(n=n.slice(1,-1)),i[e]=n}}return c.version!=null&&a.push(c),{frontmatter:{title:i.title??`Untitled`,category:i.category??`notes`,tags:i.tags??[],created:i.created??``,updated:i.updated??``,version:i.version??1,origin:`curated`,changelog:a},content:r}}};const B=i(`server`);function V(e,t){return t?{version:e,...a(t)}:{version:e}}function H(){return process.env.AIKIT_TRANSPORT?process.env.AIKIT_TRANSPORT:process.stdin.isTTY?`http`:`stdio`}function U(){let e=process.argv[1];if(!e)return!1;try{return import.meta.url===t(e).href}catch{return!1}}function W(){return U()?n({allowPositionals:!0,options:{transport:{type:`string`,default:H()},port:{type:`string`,default:process.env.AIKIT_PORT??`3210`}}}).values:{transport:H(),port:process.env.AIKIT_PORT??`3210`}}async function G(){let e=C(),t=W();if(process.on(`unhandledRejection`,t=>{B.error(`Unhandled rejection`,V(e,t))}),process.on(`uncaughtException`,t=>{B.error(`Uncaught exception — exiting`,V(e,t)),process.exit(1)}),B.info(`Starting MCP AI Kit server`,{version:e}),t.transport===`http`){let{startHttpMode:n}=await import(`./server-http-DMSGWnQe.js`);await n(e,t.port)}else{let{startStdioMode:t}=await import(`./server-stdio-DM3dQOzx.js`);await t(e)}}G();export{w as a,O as i,F as n,T as o,P as r,D as s,z as t};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{a as e,i as t,n,r,t as i}from"./server-utils-De-aZNQa.js";import{n as a,r as o,t as s}from"./workspace-bootstrap-BJloolzr.js";import{t as c}from"./curated-manager-BrgM_znO.js";import{pathToFileURL as l}from"node:url";import{parseArgs as u}from"node:util";import{createLogger as d,serializeError as f}from"../../core/dist/index.js";const p=d(`server`);function m(e,t){return t?{version:e,...f(t)}:{version:e}}function h(){return process.env.AIKIT_TRANSPORT?process.env.AIKIT_TRANSPORT:process.stdin.isTTY?`http`:`stdio`}function g(){let e=process.argv[1];if(!e)return!1;try{return import.meta.url===l(e).href}catch{return!1}}function _(){return g()?u({allowPositionals:!0,options:{transport:{type:`string`,default:h()},port:{type:`string`,default:process.env.AIKIT_PORT??`3210`}}}).values:{transport:h(),port:process.env.AIKIT_PORT??`3210`}}async function v(){let e=t(),n=_();if(process.on(`unhandledRejection`,t=>{p.error(`Unhandled rejection`,m(e,t))}),process.on(`uncaughtException`,t=>{p.error(`Uncaught exception — exiting`,m(e,t)),process.exit(1)}),p.info(`Starting MCP AI Kit server`,{version:e}),n.transport===`http`){let{startHttpMode:t}=await import(`./server-http-
|
|
1
|
+
import{a as e,i as t,n,r,t as i}from"./server-utils-De-aZNQa.js";import{n as a,r as o,t as s}from"./workspace-bootstrap-BJloolzr.js";import{t as c}from"./curated-manager-BrgM_znO.js";import{pathToFileURL as l}from"node:url";import{parseArgs as u}from"node:util";import{createLogger as d,serializeError as f}from"../../core/dist/index.js";const p=d(`server`);function m(e,t){return t?{version:e,...f(t)}:{version:e}}function h(){return process.env.AIKIT_TRANSPORT?process.env.AIKIT_TRANSPORT:process.stdin.isTTY?`http`:`stdio`}function g(){let e=process.argv[1];if(!e)return!1;try{return import.meta.url===l(e).href}catch{return!1}}function _(){return g()?u({allowPositionals:!0,options:{transport:{type:`string`,default:h()},port:{type:`string`,default:process.env.AIKIT_PORT??`3210`}}}).values:{transport:h(),port:process.env.AIKIT_PORT??`3210`}}async function v(){let e=t(),n=_();if(process.on(`unhandledRejection`,t=>{p.error(`Unhandled rejection`,m(e,t))}),process.on(`uncaughtException`,t=>{p.error(`Uncaught exception — exiting`,m(e,t)),process.exit(1)}),p.info(`Starting MCP AI Kit server`,{version:e}),n.transport===`http`){let{startHttpMode:t}=await import(`./server-http-BSr-jLTL.js`);await t(e,n.port)}else{let{startStdioMode:t}=await import(`./server-stdio-BkUtsb3W.js`);await t(e)}}export{c as CuratedKnowledgeManager,s as applyWorkspaceRoots,a as bootstrapWorkspaceRoots,i as createSlidingWindowRateLimiter,n as getSessionIdHeader,v as main,r as readPositiveIntEnv,t as readVersion,e as resolveCorsOrigin,o as selectWorkspaceRoot};
|
|
@@ -2930,7 +2930,7 @@ window.addEventListener('message', function(e) {
|
|
|
2930
2930
|
}
|
|
2931
2931
|
});
|
|
2932
2932
|
<\/script>
|
|
2933
|
-
</body>`)}function qh(e){if(!e.headersSent)try{e.writeHead(500,{"Content-Type":`text/plain`}),e.end(`Internal Server Error`)}catch{try{e.end()}catch{}}}async function Jh(e){let t=Qn(),n=`ss-${Date.now()}`;await t.launch(`headless`),await t.exec(`tab`,`new`,e).catch(()=>t.exec(`open`,e)),await t.exec(`wait`,`--load`,`networkidle`).catch(()=>{});try{let{base64:e}=await t.screenshot(n,{fullPage:!0});return Buffer.from(e,`base64`)}finally{t.exec(`tab`,`close`).catch(()=>{})}}async function Yh(e,t){if(!t){e.writeHead(503,{"Content-Type":`text/plain; charset=utf-8`}),e.end(`Screenshot target is not ready.`);return}try{let n=await Jh(t);e.writeHead(200,{"Content-Type":`image/png`,"Cache-Control":`no-store`}),e.end(n)}catch(t){let n=t instanceof Error?t.message:String(t);console.warn(`[present] Screenshot capture failed:`,n),qh(e)}}function Xh(e,t){return e.replace(/'\/callback'/g,`'/present/${t}/callback'`).replace(/"\/callback"/g,`"/present/${t}/callback"`)}function Zh(e,t){return e.replace(/src="\/viewer"/g,`src="/present/${t}/viewer"`)}async function Qh(e,t){if(Tg(`<html><body style="font-family:system-ui;padding:2rem;color:#888;text-align:center"><p>Content opened in browser window.</p></body></html>`),e.data!=null&&t.inputSchema){let n=dn({data:e.data,schema:t.inputSchema});if(!n.valid){let e=n.errors.map(e=>` ${e.path}: ${e.message}${e.expected?` (expected: ${e.expected}, got: ${e.received})`:``}`).join(`
|
|
2933
|
+
</body>`)}function qh(e){if(!e.headersSent)try{e.writeHead(500,{"Content-Type":`text/plain`}),e.end(`Internal Server Error`)}catch{try{e.end()}catch{}}}async function Jh(e){let t=Qn(),n=`ss-${Date.now()}`;t.isLaunched()||await t.launch(`headless`),await t.exec(`tab`,`new`,e).catch(()=>t.exec(`open`,e)),await t.exec(`wait`,`--load`,`networkidle`).catch(()=>{});try{let{base64:e}=await t.screenshot(n,{fullPage:!0});return Buffer.from(e,`base64`)}finally{t.exec(`tab`,`close`).catch(()=>{})}}async function Yh(e,t){if(!t){e.writeHead(503,{"Content-Type":`text/plain; charset=utf-8`}),e.end(`Screenshot target is not ready.`);return}try{let n=await Jh(t);e.writeHead(200,{"Content-Type":`image/png`,"Cache-Control":`no-store`}),e.end(n)}catch(t){let n=t instanceof Error?t.message:String(t);console.warn(`[present] Screenshot capture failed:`,n),qh(e)}}function Xh(e,t){return e.replace(/'\/callback'/g,`'/present/${t}/callback'`).replace(/"\/callback"/g,`"/present/${t}/callback"`)}function Zh(e,t){return e.replace(/src="\/viewer"/g,`src="/present/${t}/viewer"`)}async function Qh(e,t){if(Tg(`<html><body style="font-family:system-ui;padding:2rem;color:#888;text-align:center"><p>Content opened in browser window.</p></body></html>`),e.data!=null&&t.inputSchema){let n=dn({data:e.data,schema:t.inputSchema});if(!n.valid){let e=n.errors.map(e=>` ${e.path}: ${e.message}${e.expected?` (expected: ${e.expected}, got: ${e.received})`:``}`).join(`
|
|
2934
2934
|
`);return{content:[{type:`text`,text:`[ERROR:VALIDATION] Template "${t.id}" data validation failed:\n${e}\n\nExpected schema: ${JSON.stringify(t.inputSchema,null,2)}`}],structuredContent:{kind:`error`,error:{code:`VIEWER_DATA_INVALID`,message:`Template "${t.id}" data validation failed:\n${e}\n\nExpected schema: ${JSON.stringify(t.inputSchema,null,2)}`}},isError:!0}}}let n=Kh(ym(t.resolveHtml(),e.data??null,t.injectId,t.transformData)),r=Ah(),i=Ie(16).toString(`hex`),a=r.createSession({surfaceId:`viewer`,transport:`viewer`,nonce:i});if(!a.ok)return{content:[{type:`text`,text:`${e.title}\n\nUnable to start local browser transport.`}],structuredContent:{kind:`error`,error:{code:`BROWSER_START_FAILED`,message:a.error.message}},isError:!0};let o=a.session.id,s=dh(`viewer`,e.actions??[],r,o,i),c=Zh(Or({title:e.title,subtitle:e.description,html:[`<div class="present-viewer-container">`,` <iframe class="present-viewer-iframe" src="/viewer" sandbox="allow-scripts allow-same-origin"></iframe>`,` ${Gh(i)}`,`</div>`].join(`
|
|
2935
2935
|
`),css:[[`.present-viewer-container {`,` flex: 1;`,` min-height: calc(100vh - 12rem);`,` position: relative;`,` border-radius: 0.75rem;`,` overflow: hidden;`,` border: none;`,`}`,`.present-viewer-iframe {`,` border: none;`,` width: 100%;`,` height: 100%;`,` position: absolute;`,` inset: 0;`,` border-radius: inherit;`,`}`].join(`
|
|
2936
2936
|
`)],islands:[],payload:void 0,nonce:i,generatedAt:new Date().toISOString(),colorScheme:e.colorScheme,lang:e.lang,dir:e.dir,transport:`browser`,exportPolicy:`local-interactive`,footerContentHtml:fh}),o);Dh.set(o,{callbackSession:s,html:c,viewerHtml:n,hasClientConnected:!1,createdAt:Date.now()});let l=``;try{l=`http://127.0.0.1:${await jh()}/present/${o}`}catch(t){return Dh.delete(o),r.removeSession(o),{content:[{type:`text`,text:`${e.title}\n\nUnable to start local browser transport.`}],structuredContent:{kind:`error`,error:{code:`BROWSER_START_FAILED`,message:t instanceof Error?t.message:`Unable to start browser transport`}},isError:!0}}return await Hh(l),setTimeout(()=>{Dh.delete(o),r.removeSession(o)},xh),{content:[{type:`text`,text:Uh(e,l,`Rendered.`)}],structuredContent:{kind:`rendered`,reason:`no-response-required`}}}async function $h(e){Tg(`<html><body style="font-family:system-ui;padding:2rem;color:#888;text-align:center"><p>Content opened in browser window.</p></body></html>`);let t=Ah(),n=Ie(16).toString(`hex`),r=Ir(e,{transport:`browser`,registry:Hm,blockVendorScripts:Km(),nonce:n}),i=t.createSession({surfaceId:r.surfaceId,transport:`browser`,nonce:n});if(!i.ok)return{content:[{type:`text`,text:`${e.title}\n\nUnable to start local browser transport.`}],structuredContent:{kind:`error`,error:{code:`BROWSER_START_FAILED`,message:i.error.message}},isError:!0};let a=i.session.id,o=dh(r.surfaceId,r.actions,t,a,n),s=yh(r.vendorScripts,`browser`);bh();let c=r.actions.length>0?zh(r.actions):``,l=r.actions.length>0?Bh(r.actions,o.nonce):``,u=Xh(Qg(Or({title:e.title,subtitle:e.description,html:[r.html,c,l].filter(Boolean).join(`
|
|
@@ -2931,7 +2931,7 @@ window.addEventListener('message', function(e) {
|
|
|
2931
2931
|
}
|
|
2932
2932
|
});
|
|
2933
2933
|
<\/script>
|
|
2934
|
-
</body>`)}function qh(e){if(!e.headersSent)try{e.writeHead(500,{"Content-Type":`text/plain`}),e.end(`Internal Server Error`)}catch{try{e.end()}catch{}}}async function Jh(e){let t=Xn(),n=`ss-${Date.now()}`;await t.launch(`headless`),await t.exec(`tab`,`new`,e).catch(()=>t.exec(`open`,e)),await t.exec(`wait`,`--load`,`networkidle`).catch(()=>{});try{let{base64:e}=await t.screenshot(n,{fullPage:!0});return Buffer.from(e,`base64`)}finally{t.exec(`tab`,`close`).catch(()=>{})}}async function Yh(e,t){if(!t){e.writeHead(503,{"Content-Type":`text/plain; charset=utf-8`}),e.end(`Screenshot target is not ready.`);return}try{let n=await Jh(t);e.writeHead(200,{"Content-Type":`image/png`,"Cache-Control":`no-store`}),e.end(n)}catch(t){let n=t instanceof Error?t.message:String(t);console.warn(`[present] Screenshot capture failed:`,n),qh(e)}}function Xh(e,t){return e.replace(/'\/callback'/g,`'/present/${t}/callback'`).replace(/"\/callback"/g,`"/present/${t}/callback"`)}function Zh(e,t){return e.replace(/src="\/viewer"/g,`src="/present/${t}/viewer"`)}async function Qh(e,t){if(Tg(`<html><body style="font-family:system-ui;padding:2rem;color:#888;text-align:center"><p>Content opened in browser window.</p></body></html>`),e.data!=null&&t.inputSchema){let n=dn({data:e.data,schema:t.inputSchema});if(!n.valid){let e=n.errors.map(e=>` ${e.path}: ${e.message}${e.expected?` (expected: ${e.expected}, got: ${e.received})`:``}`).join(`
|
|
2934
|
+
</body>`)}function qh(e){if(!e.headersSent)try{e.writeHead(500,{"Content-Type":`text/plain`}),e.end(`Internal Server Error`)}catch{try{e.end()}catch{}}}async function Jh(e){let t=Xn(),n=`ss-${Date.now()}`;t.isLaunched()||await t.launch(`headless`),await t.exec(`tab`,`new`,e).catch(()=>t.exec(`open`,e)),await t.exec(`wait`,`--load`,`networkidle`).catch(()=>{});try{let{base64:e}=await t.screenshot(n,{fullPage:!0});return Buffer.from(e,`base64`)}finally{t.exec(`tab`,`close`).catch(()=>{})}}async function Yh(e,t){if(!t){e.writeHead(503,{"Content-Type":`text/plain; charset=utf-8`}),e.end(`Screenshot target is not ready.`);return}try{let n=await Jh(t);e.writeHead(200,{"Content-Type":`image/png`,"Cache-Control":`no-store`}),e.end(n)}catch(t){let n=t instanceof Error?t.message:String(t);console.warn(`[present] Screenshot capture failed:`,n),qh(e)}}function Xh(e,t){return e.replace(/'\/callback'/g,`'/present/${t}/callback'`).replace(/"\/callback"/g,`"/present/${t}/callback"`)}function Zh(e,t){return e.replace(/src="\/viewer"/g,`src="/present/${t}/viewer"`)}async function Qh(e,t){if(Tg(`<html><body style="font-family:system-ui;padding:2rem;color:#888;text-align:center"><p>Content opened in browser window.</p></body></html>`),e.data!=null&&t.inputSchema){let n=dn({data:e.data,schema:t.inputSchema});if(!n.valid){let e=n.errors.map(e=>` ${e.path}: ${e.message}${e.expected?` (expected: ${e.expected}, got: ${e.received})`:``}`).join(`
|
|
2935
2935
|
`);return{content:[{type:`text`,text:`[ERROR:VALIDATION] Template "${t.id}" data validation failed:\n${e}\n\nExpected schema: ${JSON.stringify(t.inputSchema,null,2)}`}],structuredContent:{kind:`error`,error:{code:`VIEWER_DATA_INVALID`,message:`Template "${t.id}" data validation failed:\n${e}\n\nExpected schema: ${JSON.stringify(t.inputSchema,null,2)}`}},isError:!0}}}let n=Kh(ym(t.resolveHtml(),e.data??null,t.injectId,t.transformData)),r=Ah(),i=Ie(16).toString(`hex`),a=r.createSession({surfaceId:`viewer`,transport:`viewer`,nonce:i});if(!a.ok)return{content:[{type:`text`,text:`${e.title}\n\nUnable to start local browser transport.`}],structuredContent:{kind:`error`,error:{code:`BROWSER_START_FAILED`,message:a.error.message}},isError:!0};let o=a.session.id,s=dh(`viewer`,e.actions??[],r,o,i),c=Zh(Or({title:e.title,subtitle:e.description,html:[`<div class="present-viewer-container">`,` <iframe class="present-viewer-iframe" src="/viewer" sandbox="allow-scripts allow-same-origin"></iframe>`,` ${Gh(i)}`,`</div>`].join(`
|
|
2936
2936
|
`),css:[[`.present-viewer-container {`,` flex: 1;`,` min-height: calc(100vh - 12rem);`,` position: relative;`,` border-radius: 0.75rem;`,` overflow: hidden;`,` border: none;`,`}`,`.present-viewer-iframe {`,` border: none;`,` width: 100%;`,` height: 100%;`,` position: absolute;`,` inset: 0;`,` border-radius: inherit;`,`}`].join(`
|
|
2937
2937
|
`)],islands:[],payload:void 0,nonce:i,generatedAt:new Date().toISOString(),colorScheme:e.colorScheme,lang:e.lang,dir:e.dir,transport:`browser`,exportPolicy:`local-interactive`,footerContentHtml:fh}),o);Dh.set(o,{callbackSession:s,html:c,viewerHtml:n,hasClientConnected:!1,createdAt:Date.now()});let l=``;try{l=`http://127.0.0.1:${await jh()}/present/${o}`}catch(t){return Dh.delete(o),r.removeSession(o),{content:[{type:`text`,text:`${e.title}\n\nUnable to start local browser transport.`}],structuredContent:{kind:`error`,error:{code:`BROWSER_START_FAILED`,message:t instanceof Error?t.message:`Unable to start browser transport`}},isError:!0}}return await Hh(l),setTimeout(()=>{Dh.delete(o),r.removeSession(o)},xh),{content:[{type:`text`,text:Uh(e,l,`Rendered.`)}],structuredContent:{kind:`rendered`,reason:`no-response-required`}}}async function $h(e){Tg(`<html><body style="font-family:system-ui;padding:2rem;color:#888;text-align:center"><p>Content opened in browser window.</p></body></html>`);let t=Ah(),n=Ie(16).toString(`hex`),r=Ir(e,{transport:`browser`,registry:Hm,blockVendorScripts:Km(),nonce:n}),i=t.createSession({surfaceId:r.surfaceId,transport:`browser`,nonce:n});if(!i.ok)return{content:[{type:`text`,text:`${e.title}\n\nUnable to start local browser transport.`}],structuredContent:{kind:`error`,error:{code:`BROWSER_START_FAILED`,message:i.error.message}},isError:!0};let a=i.session.id,o=dh(r.surfaceId,r.actions,t,a,n),s=yh(r.vendorScripts,`browser`);bh();let c=r.actions.length>0?zh(r.actions):``,l=r.actions.length>0?Bh(r.actions,o.nonce):``,u=Xh(Qg(Or({title:e.title,subtitle:e.description,html:[r.html,c,l].filter(Boolean).join(`
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{a as e,n as t,r as n,t as r}from"./server-utils-De-aZNQa.js";import{n as i,t as a}from"./startup-maintenance-D0Uhpi3k.js";import{createLogger as o,serializeError as s,setDetailedErrorLoggingEnabled as c}from"../../core/dist/index.js";import{randomUUID as l}from"node:crypto";const u=`__pending__:`;function d(e){return e.startsWith(u)}function f(e){let t=e.headers[`mcp-session-id`];return Array.isArray(t)?t[0]:t}function p(e,t,n,r){e.status(t).json({jsonrpc:`2.0`,error:{code:n,message:r},id:null})}var m=class{options;runtimes=new Map;maxSessions;sessionTimeoutMs;now;constructor(e){this.options=e,this.maxSessions=e.maxSessions??8,this.sessionTimeoutMs=(e.sessionTimeoutMinutes??30)*60*1e3,this.now=e.now??(()=>Date.now())}hasSession(e){return this.runtimes.has(e)}getSessionCount(){return this.runtimes.size}async handleRequest(e,t,n=e.body){let r=f(e),i=r?this.runtimes.get(r):void 0;if(r&&!i){p(t,404,-32001,`Session not found`);return}if(!i){if(e.method!==`POST`){p(t,400,-32e3,`Session required`);return}if(i=await this.createRuntime(t),!i)return}await this.withRuntimeLock(i,async()=>{await i.transport.handleRequest(e,t,n),i.lastAccessAt=this.now();let r=i.transport.sessionId??i.id;e.method!==`DELETE`&&!d(r)&&this.options.onSessionActivity?.(r),e.method===`DELETE`&&!d(r)&&await this.closeSession(r,{closeTransport:!1})})}async closeExpiredSessions(){let e=[...this.runtimes.values()].filter(e=>this.now()-e.lastAccessAt>=this.sessionTimeoutMs).map(e=>e.id);for(let t of e)await this.closeSession(t);return e.length}async closeSession(e,t={}){let n=this.runtimes.get(e);return n?(this.runtimes.delete(e),t.notifySessionEnd!==!1&&!d(e)&&this.options.onSessionEnd?.(e),t.closeTransport!==!1&&await n.transport.close().catch(()=>void 0),await n.server.close().catch(()=>void 0),!0):!1}async closeAll(){let e=[...this.runtimes.keys()];for(let t of e)await this.closeSession(t)}async createRuntime(e){if(await this.closeExpiredSessions(),this.runtimes.size>=this.maxSessions){p(e,503,-32003,`Session capacity reached`);return}let t=this.now(),n=await this.options.createServer(),r={id:`${u}${l()}`,transport:void 0,createdAt:t,lastAccessAt:t,server:n,requestChain:Promise.resolve()},i=this.options.createTransport({sessionIdGenerator:()=>l(),onsessioninitialized:async e=>{this.runtimes.delete(r.id),r.id=e,this.runtimes.set(e,r),this.options.onSessionStart?.(e)},onsessionclosed:async e=>{e&&await this.closeSession(e,{closeTransport:!1})}});return r.transport=i,i.onclose=()=>{let e=r.transport.sessionId??r.id;this.closeSession(e,{closeTransport:!1})},this.runtimes.set(r.id,r),await n.connect(i),r}async withRuntimeLock(e,t){let n=e.requestChain,r;e.requestChain=new Promise(e=>{r=e}),await n;try{await t()}finally{r()}}};function h(e){let t=e.includes(`T`)?e:`${e.replace(` `,`T`)}Z`;return Date.parse(t)}var g=class{stateStore;options;gcTimer=null;constructor(e,t={}){this.stateStore=e,this.options=t}onSessionStart(e,t){this.stateStore.sessionCreate(e,t)}onSessionActivity(e){this.stateStore.sessionTouch(e)}onSessionEnd(e){this.stateStore.sessionDelete(e),Promise.resolve(this.options.onSessionEndMaintenance?.(e)).catch(()=>{})}startGC(){if(this.gcTimer)return;let e=(this.options.gcIntervalMinutes??5)*60*1e3;this.gcTimer=setInterval(()=>{this.runGC()},e),this.gcTimer.unref()}runGC(){let e=this.getStaleSessionIds();for(let t of e)this.options.onBeforeSessionDelete?.(t),Promise.resolve(this.options.onSessionEndMaintenance?.(t)).catch(()=>{}),this.stateStore.sessionDelete(t);return e.length}stop(){this.gcTimer&&=(clearInterval(this.gcTimer),null)}getActiveSessions(){return this.stateStore.sessionList().length}listSessions(){return this.stateStore.sessionList()}getStaleSessionIds(e=Date.now()){let t=(this.options.staleTimeoutMinutes??30)*60*1e3;return this.stateStore.sessionList().filter(n=>{let r=h(n.lastActivity);return Number.isFinite(r)&&e-r>=t}).map(e=>e.sessionId)}};const _=o(`server`);async function v(o,u){let[{default:d},{loadConfig:f,resolveIndexMode:p},{registerDashboardRoutes:h,resolveDashboardDir:v},{registerSettingsRoutes:y,resolveSettingsDir:b},{createSettingsRouter:x},{authMiddleware:S,getOrCreateToken:C}]=await Promise.all([import(`express`),import(`./config-B-wvmMyo.js`),import(`./dashboard-static-Dw7Nsq4f.js`),import(`./settings-static-BpQgaMRs.js`),import(`./routes-C7bDyCOW.js`),import(`./auth-7LFAZQBu.js`).then(e=>e.t)]),w=f();c(w.logging?.errorDetails===!0),w.configError&&_.warn(`Config load failure`,{error:w.configError}),_.info(`Config loaded`,{sourceCount:w.sources.length,storePath:w.store.path});let T=d();T.use(d.json({limit:`1mb`}));let E=Number(u),D=`http://localhost:${E}`,O=process.env.AIKIT_CORS_ORIGIN??D,k=process.env.AIKIT_ALLOW_ANY_ORIGIN===`true`,A=n(`AIKIT_HTTP_MAX_SESSIONS`,8),j=n(`AIKIT_HTTP_SESSION_TIMEOUT_MINUTES`,30),M=n(`AIKIT_HTTP_SESSION_GC_INTERVAL_MINUTES`,5),N=r({limit:100,windowMs:6e4}),P=!1;T.use((t,n,r)=>{let i=Array.isArray(t.headers.origin)?t.headers.origin[0]:t.headers.origin,a=e({requestOrigin:i,configuredOrigin:O,allowAnyOrigin:k,fallbackOrigin:D});if(a.warn&&!P&&(P=!0,_.warn(`Rejected non-local CORS origin while AIKIT_CORS_ORIGIN=*`,{origin:i,allowAnyOriginEnv:`AIKIT_ALLOW_ANY_ORIGIN=true`})),i&&!a.allowOrigin){n.status(403).json({error:`Origin not allowed`});return}if(a.allowOrigin&&n.setHeader(`Access-Control-Allow-Origin`,a.allowOrigin),n.setHeader(`Access-Control-Allow-Methods`,`GET, POST, PUT, PATCH, DELETE, OPTIONS`),n.setHeader(`Access-Control-Allow-Headers`,`Content-Type, Authorization, Mcp-Session-Id, Mcp-Protocol-Version, Last-Event-ID`),n.setHeader(`Access-Control-Expose-Headers`,`Mcp-Session-Id`),t.method===`OPTIONS`){n.status(204).end();return}r()});let F=C();T.use(S(F)),T.use(`/mcp`,(e,n,r)=>{let i=t(e)??e.ip??e.socket.remoteAddress??`anonymous`;if(N.allow(i)){r();return}let a=Math.max(1,Math.ceil(N.getRetryAfterMs(i)/1e3));n.setHeader(`Retry-After`,String(a)),n.status(429).json({jsonrpc:`2.0`,error:{code:-32003,message:`Rate limit exceeded`},id:null})});let I;T.use(`/mcp`,(e,t,n)=>{if(!I){let t=e.headers[`x-workspace-root`];typeof t==`string`&&t.length>0&&(I=t,_.debug(`Captured workspace root from proxy header`,{wsRoot:t}))}n()}),h(T,v(),_);let L=new Date().toISOString();T.use(`/settings/api`,x({log:_,mcpInfo:()=>({transport:`http`,port:E,pid:process.pid,startedAt:L})})),y(T,b(),_),T.get(`/health`,(e,t)=>{t.json({status:`ok`})});let R=!1,z=null,B=null,V=null,H=null,U=null,W=null,G=null,K=null,q=Promise.resolve(),J=async(e,n)=>{if(!R||!V||!H){n.status(503).json({jsonrpc:`2.0`,error:{code:-32603,message:`Server initializing — please retry in a few seconds`},id:null});return}let r=q,i;q=new Promise(e=>{i=e});let a={POST:6e4,GET:1e4,DELETE:1e4}[e.method]??3e4;if(!await Promise.race([r.then(()=>!0),new Promise(t=>{let n=setTimeout(()=>{_.warn(`mcpLock timed out waiting for previous request, resetting lock chain`,{method:e.method,timeoutMs:a}),t(!1)},a);n.unref&&n.unref()})])&&(q=Promise.resolve(),i=()=>{},W)){let e=W;W=null,G=null,e.close().catch(()=>{})}try{let r=t(e);if(!W){if(r){n.status(404).json({jsonrpc:`2.0`,error:{code:-32001,message:`Session not found`},id:null});return}let e=new H({sessionIdGenerator:()=>l(),onsessioninitialized:async e=>{G=e,B?.onSessionStart(e,{transport:`http`})},onsessionclosed:async e=>{e&&B?.onSessionEnd(e),G=null}});e.onclose=()=>{W===e&&(W=null),G===e.sessionId&&(G=null)},W=e,await V.connect(e)}let i=W;await i.handleRequest(e,n,e.body),e.method!==`DELETE`&&(!r&&i.sessionId?(G=i.sessionId,B?.onSessionStart(i.sessionId,{transport:`http`}),B?.onSessionActivity(i.sessionId)):r&&B?.onSessionActivity(r))}catch(e){if(_.error(`MCP handler error`,s(e)),!n.headersSent){let t=e instanceof Error?e.message:String(e),r=t.includes(`Not Acceptable`);n.status(r?406:500).json({jsonrpc:`2.0`,error:{code:r?-32e3:-32603,message:r?t:`Internal server error`},id:null})}}finally{i()}},Y=async(e,n)=>{let r=t(e);if(U&&(!W||r!==G)){await U.handleRequest(e,n,e.body);return}await J(e,n)};T.post(`/mcp`,Y),T.get(`/mcp`,Y),T.delete(`/mcp`,Y);let X=T.listen(E,`127.0.0.1`,()=>{_.info(`MCP server listening`,{url:`http://127.0.0.1:${E}/mcp`,port:E}),setTimeout(async()=>{try{typeof I==`string`&&I.length>0&&(w.sources[0]={path:I,excludePatterns:w.sources[0]?.excludePatterns??[]},w.allRoots=[I],_.debug(`Workspace root applied from proxy header`,{wsRoot:I}));let[{createLazyServer:e,createMcpServer:t,ALL_TOOL_NAMES:n},{StreamableHTTPServerTransport:r},{checkForUpdates:c,autoUpgradeScaffold:l}]=await Promise.all([import(`./server-BlN0Hbye.js`),import(`@modelcontextprotocol/sdk/server/streamableHttp.js`),import(`./version-check-CggUKvv8.js`)]);c(),l(),setInterval(c,1440*60*1e3).unref();let u=!1,d=p(w),f=e(w,d);K=async()=>{f.aikit&&await Promise.all([f.aikit.embedder.shutdown?.().catch(()=>{})??Promise.resolve(),f.aikit.graphStore.close().catch(()=>{}),f.aikit.closeStateStore?.().catch(()=>{})??Promise.resolve(),f.aikit.store.close().catch(()=>{})])},V=f.server,H=r,R=!0,_.debug(`MCP server configured (lazy — AI Kit initializing in background)`,{toolCount:n.length,resourceCount:2}),f.startInit(),f.ready.then(async()=>{try{if(!f.aikit)throw Error(`AI Kit components are not available after initialization`);B=new g(f.aikit.stateStore,{staleTimeoutMinutes:j,gcIntervalMinutes:M,onBeforeSessionDelete:e=>{if(G===e&&W){let e=W;W=null,G=null,e.close().catch(()=>void 0)}U?.closeSession(e,{notifySessionEnd:!1})},onSessionEndMaintenance:async()=>{if(!f.aikit?.curated||!f.aikit?.stateStore)return;let{pruneLessons:e}=await import(`./evolution-BX_zTSdj.js`).then(e=>e.t),t=await e(f.aikit.curated,f.aikit.stateStore,{dryRun:!1});t.pruned.length>0&&_.info(`Session-end lesson prune`,{pruned:t.pruned.length})}}),U=new m({createServer:()=>{if(!f.aikit)throw Error(`AI Kit components are not available after initialization`);return t(f.aikit,w)},createTransport:e=>new r(e),maxSessions:A,sessionTimeoutMinutes:j,onSessionStart:e=>B?.onSessionStart(e,{transport:`http`}),onSessionActivity:e=>B?.onSessionActivity(e),onSessionEnd:e=>B?.onSessionEnd(e)}),B.startGC(),G&&(B.onSessionStart(G,{transport:`http`}),B.onSessionActivity(G)),_.info(`HTTP session runtime ready`,{maxSessions:A,sessionTimeoutMinutes:j,gcIntervalMinutes:M})}catch(e){_.error(`Failed to start session manager`,s(e)),R=!1,u=!0,V=null,H=null,K=null}}),d===`auto`?f.ready.then(async()=>{try{let e=w.sources.map(e=>e.path).join(`, `);_.info(`Running initial index`,{sourcePaths:e}),await f.runInitialIndex(),_.info(`Initial index complete`)}catch(e){_.error(`Initial index failed; will retry on aikit_reindex`,i(o,e))}}):d===`smart`?u||f.ready.then(async()=>{try{if(!f.aikit)throw Error(`AI Kit components are not available after initialization`);let{SmartIndexScheduler:e}=await import(`../../indexer/dist/index.js`),t=new e(f.aikit.indexer,w,f.aikit.store),n=f.aikit.store;z=t,t.start(),n.onBeforeClose&&n.onBeforeClose(()=>t.stop()),f.setSmartScheduler(t),_.debug(`Smart index scheduler started (HTTP mode)`)}catch(e){_.error(`Failed to start smart index scheduler`,i(o,e))}}):_.info(`Initial full indexing skipped in HTTP mode`,{indexMode:d}),f.ready.catch(e=>{_.error(`AI Kit initialization failed`,i(o,e)),R=!1,u=!0,V=null,H=null,K=null}),a(f.ready,()=>f.aikit?{curated:f.aikit.curated,stateStore:f.aikit.stateStore}:null,o)}catch(e){_.error(`Failed to load server modules`,i(o,e)),R=!1,K=null}},100)}),Z=!1,Q=async e=>{Z||(Z=!0,_.info(`Shutdown signal received`,{signal:e}),z?.stop(),B?.stop(),await import(`../../tools/dist/index.js`).then(({processStopAll:e})=>e()).catch(()=>{}),await U?.closeAll().catch(()=>void 0),G&&B?.onSessionEnd(G),W&&(await W.close().catch(()=>void 0),W=null,G=null),X.close(),V&&await V.close(),await K?.().catch(()=>void 0),process.exit(0))};process.on(`SIGINT`,()=>Q(`SIGINT`)),process.on(`SIGTERM`,()=>Q(`SIGTERM`))}export{v as startHttpMode};
|
|
1
|
+
import{a as e,n as t,r as n,t as r}from"./server-utils-De-aZNQa.js";import{n as i,t as a}from"./startup-maintenance-D0Uhpi3k.js";import{createLogger as o,serializeError as s,setDetailedErrorLoggingEnabled as c}from"../../core/dist/index.js";import{randomUUID as l}from"node:crypto";const u=`__pending__:`;function d(e){return e.startsWith(u)}function f(e){let t=e.headers[`mcp-session-id`];return Array.isArray(t)?t[0]:t}function p(e,t,n,r){e.status(t).json({jsonrpc:`2.0`,error:{code:n,message:r},id:null})}var m=class{options;runtimes=new Map;maxSessions;sessionTimeoutMs;now;constructor(e){this.options=e,this.maxSessions=e.maxSessions??8,this.sessionTimeoutMs=(e.sessionTimeoutMinutes??30)*60*1e3,this.now=e.now??(()=>Date.now())}hasSession(e){return this.runtimes.has(e)}getSessionCount(){return this.runtimes.size}async handleRequest(e,t,n=e.body){let r=f(e),i=r?this.runtimes.get(r):void 0;if(r&&!i){p(t,404,-32001,`Session not found`);return}if(!i){if(e.method!==`POST`){p(t,400,-32e3,`Session required`);return}if(i=await this.createRuntime(t),!i)return}await this.withRuntimeLock(i,async()=>{await i.transport.handleRequest(e,t,n),i.lastAccessAt=this.now();let r=i.transport.sessionId??i.id;e.method!==`DELETE`&&!d(r)&&this.options.onSessionActivity?.(r),e.method===`DELETE`&&!d(r)&&await this.closeSession(r,{closeTransport:!1})})}async closeExpiredSessions(){let e=[...this.runtimes.values()].filter(e=>this.now()-e.lastAccessAt>=this.sessionTimeoutMs).map(e=>e.id);for(let t of e)await this.closeSession(t);return e.length}async closeSession(e,t={}){let n=this.runtimes.get(e);return n?(this.runtimes.delete(e),t.notifySessionEnd!==!1&&!d(e)&&this.options.onSessionEnd?.(e),t.closeTransport!==!1&&await n.transport.close().catch(()=>void 0),await n.server.close().catch(()=>void 0),!0):!1}async closeAll(){let e=[...this.runtimes.keys()];for(let t of e)await this.closeSession(t)}async createRuntime(e){if(await this.closeExpiredSessions(),this.runtimes.size>=this.maxSessions){p(e,503,-32003,`Session capacity reached`);return}let t=this.now(),n=await this.options.createServer(),r={id:`${u}${l()}`,transport:void 0,createdAt:t,lastAccessAt:t,server:n,requestChain:Promise.resolve()},i=this.options.createTransport({sessionIdGenerator:()=>l(),onsessioninitialized:async e=>{this.runtimes.delete(r.id),r.id=e,this.runtimes.set(e,r),this.options.onSessionStart?.(e)},onsessionclosed:async e=>{e&&await this.closeSession(e,{closeTransport:!1})}});return r.transport=i,i.onclose=()=>{let e=r.transport.sessionId??r.id;this.closeSession(e,{closeTransport:!1})},this.runtimes.set(r.id,r),await n.connect(i),r}async withRuntimeLock(e,t){let n=e.requestChain,r;e.requestChain=new Promise(e=>{r=e}),await n;try{await t()}finally{r()}}};function h(e){let t=e.includes(`T`)?e:`${e.replace(` `,`T`)}Z`;return Date.parse(t)}var g=class{stateStore;options;gcTimer=null;constructor(e,t={}){this.stateStore=e,this.options=t}onSessionStart(e,t){this.stateStore.sessionCreate(e,t)}onSessionActivity(e){this.stateStore.sessionTouch(e)}onSessionEnd(e){this.stateStore.sessionDelete(e),Promise.resolve(this.options.onSessionEndMaintenance?.(e)).catch(()=>{})}startGC(){if(this.gcTimer)return;let e=(this.options.gcIntervalMinutes??5)*60*1e3;this.gcTimer=setInterval(()=>{this.runGC()},e),this.gcTimer.unref()}runGC(){let e=this.getStaleSessionIds();for(let t of e)this.options.onBeforeSessionDelete?.(t),Promise.resolve(this.options.onSessionEndMaintenance?.(t)).catch(()=>{}),this.stateStore.sessionDelete(t);return e.length}stop(){this.gcTimer&&=(clearInterval(this.gcTimer),null)}getActiveSessions(){return this.stateStore.sessionList().length}listSessions(){return this.stateStore.sessionList()}getStaleSessionIds(e=Date.now()){let t=(this.options.staleTimeoutMinutes??30)*60*1e3;return this.stateStore.sessionList().filter(n=>{let r=h(n.lastActivity);return Number.isFinite(r)&&e-r>=t}).map(e=>e.sessionId)}};const _=o(`server`);async function v(o,u){let[{default:d},{loadConfig:f,resolveIndexMode:p},{registerDashboardRoutes:h,resolveDashboardDir:v},{registerSettingsRoutes:y,resolveSettingsDir:b},{createSettingsRouter:x},{authMiddleware:S,getOrCreateToken:C}]=await Promise.all([import(`express`),import(`./config-B-wvmMyo.js`),import(`./dashboard-static-Dw7Nsq4f.js`),import(`./settings-static-BpQgaMRs.js`),import(`./routes-C7bDyCOW.js`),import(`./auth-7LFAZQBu.js`).then(e=>e.t)]),w=f();c(w.logging?.errorDetails===!0),w.configError&&_.warn(`Config load failure`,{error:w.configError}),_.info(`Config loaded`,{sourceCount:w.sources.length,storePath:w.store.path});let T=d();T.use(d.json({limit:`1mb`}));let E=Number(u),D=`http://localhost:${E}`,O=process.env.AIKIT_CORS_ORIGIN??D,k=process.env.AIKIT_ALLOW_ANY_ORIGIN===`true`,A=n(`AIKIT_HTTP_MAX_SESSIONS`,8),j=n(`AIKIT_HTTP_SESSION_TIMEOUT_MINUTES`,30),M=n(`AIKIT_HTTP_SESSION_GC_INTERVAL_MINUTES`,5),N=r({limit:100,windowMs:6e4}),P=!1;T.use((t,n,r)=>{let i=Array.isArray(t.headers.origin)?t.headers.origin[0]:t.headers.origin,a=e({requestOrigin:i,configuredOrigin:O,allowAnyOrigin:k,fallbackOrigin:D});if(a.warn&&!P&&(P=!0,_.warn(`Rejected non-local CORS origin while AIKIT_CORS_ORIGIN=*`,{origin:i,allowAnyOriginEnv:`AIKIT_ALLOW_ANY_ORIGIN=true`})),i&&!a.allowOrigin){n.status(403).json({error:`Origin not allowed`});return}if(a.allowOrigin&&n.setHeader(`Access-Control-Allow-Origin`,a.allowOrigin),n.setHeader(`Access-Control-Allow-Methods`,`GET, POST, PUT, PATCH, DELETE, OPTIONS`),n.setHeader(`Access-Control-Allow-Headers`,`Content-Type, Authorization, Mcp-Session-Id, Mcp-Protocol-Version, Last-Event-ID`),n.setHeader(`Access-Control-Expose-Headers`,`Mcp-Session-Id`),t.method===`OPTIONS`){n.status(204).end();return}r()});let F=C();T.use(S(F)),T.use(`/mcp`,(e,n,r)=>{let i=t(e)??e.ip??e.socket.remoteAddress??`anonymous`;if(N.allow(i)){r();return}let a=Math.max(1,Math.ceil(N.getRetryAfterMs(i)/1e3));n.setHeader(`Retry-After`,String(a)),n.status(429).json({jsonrpc:`2.0`,error:{code:-32003,message:`Rate limit exceeded`},id:null})});let I;T.use(`/mcp`,(e,t,n)=>{if(!I){let t=e.headers[`x-workspace-root`];typeof t==`string`&&t.length>0&&(I=t,_.debug(`Captured workspace root from proxy header`,{wsRoot:t}))}n()}),h(T,v(),_);let L=new Date().toISOString();T.use(`/settings/api`,x({log:_,mcpInfo:()=>({transport:`http`,port:E,pid:process.pid,startedAt:L})})),y(T,b(),_),T.get(`/health`,(e,t)=>{t.json({status:`ok`})});let R=!1,z=null,B=null,V=null,H=null,U=null,W=null,G=null,K=null,q=Promise.resolve(),J=async(e,n)=>{if(!R||!V||!H){n.status(503).json({jsonrpc:`2.0`,error:{code:-32603,message:`Server initializing — please retry in a few seconds`},id:null});return}let r=q,i;q=new Promise(e=>{i=e});let a={POST:6e4,GET:1e4,DELETE:1e4}[e.method]??3e4;if(!await Promise.race([r.then(()=>!0),new Promise(t=>{let n=setTimeout(()=>{_.warn(`mcpLock timed out waiting for previous request, resetting lock chain`,{method:e.method,timeoutMs:a}),t(!1)},a);n.unref&&n.unref()})])&&(q=Promise.resolve(),i=()=>{},W)){let e=W;W=null,G=null,e.close().catch(()=>{})}try{let r=t(e);if(!W){if(r){n.status(404).json({jsonrpc:`2.0`,error:{code:-32001,message:`Session not found`},id:null});return}let e=new H({sessionIdGenerator:()=>l(),onsessioninitialized:async e=>{G=e,B?.onSessionStart(e,{transport:`http`})},onsessionclosed:async e=>{e&&B?.onSessionEnd(e),G=null}});e.onclose=()=>{W===e&&(W=null),G===e.sessionId&&(G=null)},W=e,await V.connect(e)}let i=W;await i.handleRequest(e,n,e.body),e.method!==`DELETE`&&(!r&&i.sessionId?(G=i.sessionId,B?.onSessionStart(i.sessionId,{transport:`http`}),B?.onSessionActivity(i.sessionId)):r&&B?.onSessionActivity(r))}catch(e){if(_.error(`MCP handler error`,s(e)),!n.headersSent){let t=e instanceof Error?e.message:String(e),r=t.includes(`Not Acceptable`);n.status(r?406:500).json({jsonrpc:`2.0`,error:{code:r?-32e3:-32603,message:r?t:`Internal server error`},id:null})}}finally{i()}},Y=async(e,n)=>{let r=t(e);if(U&&(!W||r!==G)){await U.handleRequest(e,n,e.body);return}await J(e,n)};T.post(`/mcp`,Y),T.get(`/mcp`,Y),T.delete(`/mcp`,Y);let X=T.listen(E,`127.0.0.1`,()=>{_.info(`MCP server listening`,{url:`http://127.0.0.1:${E}/mcp`,port:E}),setTimeout(async()=>{try{typeof I==`string`&&I.length>0&&(w.sources[0]={path:I,excludePatterns:w.sources[0]?.excludePatterns??[]},w.allRoots=[I],_.debug(`Workspace root applied from proxy header`,{wsRoot:I}));let[{createLazyServer:e,createMcpServer:t,ALL_TOOL_NAMES:n},{StreamableHTTPServerTransport:r},{checkForUpdates:c,autoUpgradeScaffold:l}]=await Promise.all([import(`./server-CijzehmX.js`),import(`@modelcontextprotocol/sdk/server/streamableHttp.js`),import(`./version-check-CggUKvv8.js`)]);c(),l(),setInterval(c,1440*60*1e3).unref();let u=!1,d=p(w),f=e(w,d);K=async()=>{f.aikit&&await Promise.all([f.aikit.embedder.shutdown?.().catch(()=>{})??Promise.resolve(),f.aikit.graphStore.close().catch(()=>{}),f.aikit.closeStateStore?.().catch(()=>{})??Promise.resolve(),f.aikit.store.close().catch(()=>{})])},V=f.server,H=r,R=!0,_.debug(`MCP server configured (lazy — AI Kit initializing in background)`,{toolCount:n.length,resourceCount:2}),f.startInit(),f.ready.then(async()=>{try{if(!f.aikit)throw Error(`AI Kit components are not available after initialization`);B=new g(f.aikit.stateStore,{staleTimeoutMinutes:j,gcIntervalMinutes:M,onBeforeSessionDelete:e=>{if(G===e&&W){let e=W;W=null,G=null,e.close().catch(()=>void 0)}U?.closeSession(e,{notifySessionEnd:!1})},onSessionEndMaintenance:async()=>{if(!f.aikit?.curated||!f.aikit?.stateStore)return;let{pruneLessons:e}=await import(`./evolution-BX_zTSdj.js`).then(e=>e.t),t=await e(f.aikit.curated,f.aikit.stateStore,{dryRun:!1});t.pruned.length>0&&_.info(`Session-end lesson prune`,{pruned:t.pruned.length})}}),U=new m({createServer:()=>{if(!f.aikit)throw Error(`AI Kit components are not available after initialization`);return t(f.aikit,w)},createTransport:e=>new r(e),maxSessions:A,sessionTimeoutMinutes:j,onSessionStart:e=>B?.onSessionStart(e,{transport:`http`}),onSessionActivity:e=>B?.onSessionActivity(e),onSessionEnd:e=>B?.onSessionEnd(e)}),B.startGC(),G&&(B.onSessionStart(G,{transport:`http`}),B.onSessionActivity(G)),_.info(`HTTP session runtime ready`,{maxSessions:A,sessionTimeoutMinutes:j,gcIntervalMinutes:M})}catch(e){_.error(`Failed to start session manager`,s(e)),R=!1,u=!0,V=null,H=null,K=null}}),d===`auto`?f.ready.then(async()=>{try{let e=w.sources.map(e=>e.path).join(`, `);_.info(`Running initial index`,{sourcePaths:e}),await f.runInitialIndex(),_.info(`Initial index complete`)}catch(e){_.error(`Initial index failed; will retry on aikit_reindex`,i(o,e))}}):d===`smart`?u||f.ready.then(async()=>{try{if(!f.aikit)throw Error(`AI Kit components are not available after initialization`);let{SmartIndexScheduler:e}=await import(`../../indexer/dist/index.js`),t=new e(f.aikit.indexer,w,f.aikit.store),n=f.aikit.store;z=t,t.start(),n.onBeforeClose&&n.onBeforeClose(()=>t.stop()),f.setSmartScheduler(t),_.debug(`Smart index scheduler started (HTTP mode)`)}catch(e){_.error(`Failed to start smart index scheduler`,i(o,e))}}):_.info(`Initial full indexing skipped in HTTP mode`,{indexMode:d}),f.ready.catch(e=>{_.error(`AI Kit initialization failed`,i(o,e)),R=!1,u=!0,V=null,H=null,K=null}),a(f.ready,()=>f.aikit?{curated:f.aikit.curated,stateStore:f.aikit.stateStore}:null,o)}catch(e){_.error(`Failed to load server modules`,i(o,e)),R=!1,K=null}},100)}),Z=!1,Q=async e=>{Z||(Z=!0,_.info(`Shutdown signal received`,{signal:e}),z?.stop(),B?.stop(),await import(`../../tools/dist/index.js`).then(({processStopAll:e})=>e()).catch(()=>{}),await U?.closeAll().catch(()=>void 0),G&&B?.onSessionEnd(G),W&&(await W.close().catch(()=>void 0),W=null,G=null),X.close(),V&&await V.close(),await K?.().catch(()=>void 0),process.exit(0))};process.on(`SIGINT`,()=>Q(`SIGINT`)),process.on(`SIGTERM`,()=>Q(`SIGTERM`))}export{v as startHttpMode};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import{a as e,i as t,o as n,s as r}from"./bin.js";import{n as i,t as a}from"./startup-maintenance-L9NUOBVy.js";import{createLogger as o,serializeError as s,setDetailedErrorLoggingEnabled as c}from"../../core/dist/index.js";import{randomUUID as l}from"node:crypto";const u=`__pending__:`;function d(e){return e.startsWith(u)}function f(e){let t=e.headers[`mcp-session-id`];return Array.isArray(t)?t[0]:t}function p(e,t,n,r){e.status(t).json({jsonrpc:`2.0`,error:{code:n,message:r},id:null})}var m=class{options;runtimes=new Map;maxSessions;sessionTimeoutMs;now;constructor(e){this.options=e,this.maxSessions=e.maxSessions??8,this.sessionTimeoutMs=(e.sessionTimeoutMinutes??30)*60*1e3,this.now=e.now??(()=>Date.now())}hasSession(e){return this.runtimes.has(e)}getSessionCount(){return this.runtimes.size}async handleRequest(e,t,n=e.body){let r=f(e),i=r?this.runtimes.get(r):void 0;if(r&&!i){p(t,404,-32001,`Session not found`);return}if(!i){if(e.method!==`POST`){p(t,400,-32e3,`Session required`);return}if(i=await this.createRuntime(t),!i)return}await this.withRuntimeLock(i,async()=>{await i.transport.handleRequest(e,t,n),i.lastAccessAt=this.now();let r=i.transport.sessionId??i.id;e.method!==`DELETE`&&!d(r)&&this.options.onSessionActivity?.(r),e.method===`DELETE`&&!d(r)&&await this.closeSession(r,{closeTransport:!1})})}async closeExpiredSessions(){let e=[...this.runtimes.values()].filter(e=>this.now()-e.lastAccessAt>=this.sessionTimeoutMs).map(e=>e.id);for(let t of e)await this.closeSession(t);return e.length}async closeSession(e,t={}){let n=this.runtimes.get(e);return n?(this.runtimes.delete(e),t.notifySessionEnd!==!1&&!d(e)&&this.options.onSessionEnd?.(e),t.closeTransport!==!1&&await n.transport.close().catch(()=>void 0),await n.server.close().catch(()=>void 0),!0):!1}async closeAll(){let e=[...this.runtimes.keys()];for(let t of e)await this.closeSession(t)}async createRuntime(e){if(await this.closeExpiredSessions(),this.runtimes.size>=this.maxSessions){p(e,503,-32003,`Session capacity reached`);return}let t=this.now(),n=await this.options.createServer(),r={id:`${u}${l()}`,transport:void 0,createdAt:t,lastAccessAt:t,server:n,requestChain:Promise.resolve()},i=this.options.createTransport({sessionIdGenerator:()=>l(),onsessioninitialized:async e=>{this.runtimes.delete(r.id),r.id=e,this.runtimes.set(e,r),this.options.onSessionStart?.(e)},onsessionclosed:async e=>{e&&await this.closeSession(e,{closeTransport:!1})}});return r.transport=i,i.onclose=()=>{let e=r.transport.sessionId??r.id;this.closeSession(e,{closeTransport:!1})},this.runtimes.set(r.id,r),await n.connect(i),r}async withRuntimeLock(e,t){let n=e.requestChain,r;e.requestChain=new Promise(e=>{r=e}),await n;try{await t()}finally{r()}}};function h(e){let t=e.includes(`T`)?e:`${e.replace(` `,`T`)}Z`;return Date.parse(t)}var g=class{stateStore;options;gcTimer=null;constructor(e,t={}){this.stateStore=e,this.options=t}onSessionStart(e,t){this.stateStore.sessionCreate(e,t)}onSessionActivity(e){this.stateStore.sessionTouch(e)}onSessionEnd(e){this.stateStore.sessionDelete(e),Promise.resolve(this.options.onSessionEndMaintenance?.(e)).catch(()=>{})}startGC(){if(this.gcTimer)return;let e=(this.options.gcIntervalMinutes??5)*60*1e3;this.gcTimer=setInterval(()=>{this.runGC()},e),this.gcTimer.unref()}runGC(){let e=this.getStaleSessionIds();for(let t of e)this.options.onBeforeSessionDelete?.(t),Promise.resolve(this.options.onSessionEndMaintenance?.(t)).catch(()=>{}),this.stateStore.sessionDelete(t);return e.length}stop(){this.gcTimer&&=(clearInterval(this.gcTimer),null)}getActiveSessions(){return this.stateStore.sessionList().length}listSessions(){return this.stateStore.sessionList()}getStaleSessionIds(e=Date.now()){let t=(this.options.staleTimeoutMinutes??30)*60*1e3;return this.stateStore.sessionList().filter(n=>{let r=h(n.lastActivity);return Number.isFinite(r)&&e-r>=t}).map(e=>e.sessionId)}};const _=o(`server`);async function v(o,u){let[{default:d},{loadConfig:f,resolveIndexMode:p},{registerDashboardRoutes:h,resolveDashboardDir:v},{registerSettingsRoutes:y,resolveSettingsDir:b},{createSettingsRouter:x},{authMiddleware:S,getOrCreateToken:C}]=await Promise.all([import(`express`),import(`./config-Bx85fwRX.js`),import(`./dashboard-static-dPnij4uF.js`),import(`./settings-static-MepJZjer.js`),import(`./routes-CfG5gdSR.js`),import(`./auth-bEP-6uqy.js`)]),w=f();c(w.logging?.errorDetails===!0),w.configError&&_.warn(`Config load failure`,{error:w.configError}),_.info(`Config loaded`,{sourceCount:w.sources.length,storePath:w.store.path});let T=d();T.use(d.json({limit:`1mb`}));let E=Number(u),D=`http://localhost:${E}`,O=process.env.AIKIT_CORS_ORIGIN??D,k=process.env.AIKIT_ALLOW_ANY_ORIGIN===`true`,A=n(`AIKIT_HTTP_MAX_SESSIONS`,8),j=n(`AIKIT_HTTP_SESSION_TIMEOUT_MINUTES`,30),M=n(`AIKIT_HTTP_SESSION_GC_INTERVAL_MINUTES`,5),N=t({limit:100,windowMs:6e4}),P=!1;T.use((e,t,n)=>{let i=Array.isArray(e.headers.origin)?e.headers.origin[0]:e.headers.origin,a=r({requestOrigin:i,configuredOrigin:O,allowAnyOrigin:k,fallbackOrigin:D});if(a.warn&&!P&&(P=!0,_.warn(`Rejected non-local CORS origin while AIKIT_CORS_ORIGIN=*`,{origin:i,allowAnyOriginEnv:`AIKIT_ALLOW_ANY_ORIGIN=true`})),i&&!a.allowOrigin){t.status(403).json({error:`Origin not allowed`});return}if(a.allowOrigin&&t.setHeader(`Access-Control-Allow-Origin`,a.allowOrigin),t.setHeader(`Access-Control-Allow-Methods`,`GET, POST, PUT, PATCH, DELETE, OPTIONS`),t.setHeader(`Access-Control-Allow-Headers`,`Content-Type, Authorization, Mcp-Session-Id, Mcp-Protocol-Version, Last-Event-ID`),t.setHeader(`Access-Control-Expose-Headers`,`Mcp-Session-Id`),e.method===`OPTIONS`){t.status(204).end();return}n()});let F=C();T.use(S(F)),T.use(`/mcp`,(t,n,r)=>{let i=e(t)??t.ip??t.socket.remoteAddress??`anonymous`;if(N.allow(i)){r();return}let a=Math.max(1,Math.ceil(N.getRetryAfterMs(i)/1e3));n.setHeader(`Retry-After`,String(a)),n.status(429).json({jsonrpc:`2.0`,error:{code:-32003,message:`Rate limit exceeded`},id:null})});let I;T.use(`/mcp`,(e,t,n)=>{if(!I){let t=e.headers[`x-workspace-root`];typeof t==`string`&&t.length>0&&(I=t,_.debug(`Captured workspace root from proxy header`,{wsRoot:t}))}n()}),h(T,v(),_);let L=new Date().toISOString();T.use(`/settings/api`,x({log:_,mcpInfo:()=>({transport:`http`,port:E,pid:process.pid,startedAt:L})})),y(T,b(),_),T.get(`/health`,(e,t)=>{t.json({status:`ok`})});let R=!1,z=null,B=null,V=null,H=null,U=null,W=null,G=null,K=null,q=Promise.resolve(),J=async(t,n)=>{if(!R||!V||!H){n.status(503).json({jsonrpc:`2.0`,error:{code:-32603,message:`Server initializing — please retry in a few seconds`},id:null});return}let r=q,i;q=new Promise(e=>{i=e});let a={POST:6e4,GET:1e4,DELETE:1e4}[t.method]??3e4;if(!await Promise.race([r.then(()=>!0),new Promise(e=>{let n=setTimeout(()=>{_.warn(`mcpLock timed out waiting for previous request, resetting lock chain`,{method:t.method,timeoutMs:a}),e(!1)},a);n.unref&&n.unref()})])&&(q=Promise.resolve(),i=()=>{},W)){let e=W;W=null,G=null,e.close().catch(()=>{})}try{let r=e(t);if(!W){if(r){n.status(404).json({jsonrpc:`2.0`,error:{code:-32001,message:`Session not found`},id:null});return}let e=new H({sessionIdGenerator:()=>l(),onsessioninitialized:async e=>{G=e,B?.onSessionStart(e,{transport:`http`})},onsessionclosed:async e=>{e&&B?.onSessionEnd(e),G=null}});e.onclose=()=>{W===e&&(W=null),G===e.sessionId&&(G=null)},W=e,await V.connect(e)}let i=W;await i.handleRequest(t,n,t.body),t.method!==`DELETE`&&(!r&&i.sessionId?(G=i.sessionId,B?.onSessionStart(i.sessionId,{transport:`http`}),B?.onSessionActivity(i.sessionId)):r&&B?.onSessionActivity(r))}catch(e){if(_.error(`MCP handler error`,s(e)),!n.headersSent){let t=e instanceof Error?e.message:String(e),r=t.includes(`Not Acceptable`);n.status(r?406:500).json({jsonrpc:`2.0`,error:{code:r?-32e3:-32603,message:r?t:`Internal server error`},id:null})}}finally{i()}},Y=async(t,n)=>{let r=e(t);if(U&&(!W||r!==G)){await U.handleRequest(t,n,t.body);return}await J(t,n)};T.post(`/mcp`,Y),T.get(`/mcp`,Y),T.delete(`/mcp`,Y);let X=T.listen(E,`127.0.0.1`,()=>{_.info(`MCP server listening`,{url:`http://127.0.0.1:${E}/mcp`,port:E}),setTimeout(async()=>{try{typeof I==`string`&&I.length>0&&(w.sources[0]={path:I,excludePatterns:w.sources[0]?.excludePatterns??[]},w.allRoots=[I],_.debug(`Workspace root applied from proxy header`,{wsRoot:I}));let[{createLazyServer:e,createMcpServer:t,ALL_TOOL_NAMES:n},{StreamableHTTPServerTransport:r},{checkForUpdates:c,autoUpgradeScaffold:l}]=await Promise.all([import(`./server-BPpxMa6G.js`),import(`@modelcontextprotocol/sdk/server/streamableHttp.js`),import(`./version-check-CdBHTxtt.js`)]);c(),l(),setInterval(c,1440*60*1e3).unref();let u=!1,d=p(w),f=e(w,d);K=async()=>{f.aikit&&await Promise.all([f.aikit.embedder.shutdown?.().catch(()=>{})??Promise.resolve(),f.aikit.graphStore.close().catch(()=>{}),f.aikit.closeStateStore?.().catch(()=>{})??Promise.resolve(),f.aikit.store.close().catch(()=>{})])},V=f.server,H=r,R=!0,_.debug(`MCP server configured (lazy — AI Kit initializing in background)`,{toolCount:n.length,resourceCount:2}),f.startInit(),f.ready.then(async()=>{try{if(!f.aikit)throw Error(`AI Kit components are not available after initialization`);B=new g(f.aikit.stateStore,{staleTimeoutMinutes:j,gcIntervalMinutes:M,onBeforeSessionDelete:e=>{if(G===e&&W){let e=W;W=null,G=null,e.close().catch(()=>void 0)}U?.closeSession(e,{notifySessionEnd:!1})},onSessionEndMaintenance:async()=>{if(!f.aikit?.curated||!f.aikit?.stateStore)return;let{pruneLessons:e}=await import(`./evolution-DWaEE6XW.js`).then(e=>e.t),t=await e(f.aikit.curated,f.aikit.stateStore,{dryRun:!1});t.pruned.length>0&&_.info(`Session-end lesson prune`,{pruned:t.pruned.length})}}),U=new m({createServer:()=>{if(!f.aikit)throw Error(`AI Kit components are not available after initialization`);return t(f.aikit,w)},createTransport:e=>new r(e),maxSessions:A,sessionTimeoutMinutes:j,onSessionStart:e=>B?.onSessionStart(e,{transport:`http`}),onSessionActivity:e=>B?.onSessionActivity(e),onSessionEnd:e=>B?.onSessionEnd(e)}),B.startGC(),G&&(B.onSessionStart(G,{transport:`http`}),B.onSessionActivity(G)),_.info(`HTTP session runtime ready`,{maxSessions:A,sessionTimeoutMinutes:j,gcIntervalMinutes:M})}catch(e){_.error(`Failed to start session manager`,s(e)),R=!1,u=!0,V=null,H=null,K=null}}),d===`auto`?f.ready.then(async()=>{try{let e=w.sources.map(e=>e.path).join(`, `);_.info(`Running initial index`,{sourcePaths:e}),await f.runInitialIndex(),_.info(`Initial index complete`)}catch(e){_.error(`Initial index failed; will retry on aikit_reindex`,i(o,e))}}):d===`smart`?u||f.ready.then(async()=>{try{if(!f.aikit)throw Error(`AI Kit components are not available after initialization`);let{SmartIndexScheduler:e}=await import(`../../indexer/dist/index.js`),t=new e(f.aikit.indexer,w,f.aikit.store),n=f.aikit.store;z=t,t.start(),n.onBeforeClose&&n.onBeforeClose(()=>t.stop()),f.setSmartScheduler(t),_.debug(`Smart index scheduler started (HTTP mode)`)}catch(e){_.error(`Failed to start smart index scheduler`,i(o,e))}}):_.info(`Initial full indexing skipped in HTTP mode`,{indexMode:d}),f.ready.catch(e=>{_.error(`AI Kit initialization failed`,i(o,e)),R=!1,u=!0,V=null,H=null,K=null}),a(f.ready,()=>f.aikit?{curated:f.aikit.curated,stateStore:f.aikit.stateStore}:null,o)}catch(e){_.error(`Failed to load server modules`,i(o,e)),R=!1,K=null}},100)}),Z=!1,Q=async e=>{Z||(Z=!0,_.info(`Shutdown signal received`,{signal:e}),z?.stop(),B?.stop(),await import(`../../tools/dist/index.js`).then(({processStopAll:e})=>e()).catch(()=>{}),await U?.closeAll().catch(()=>void 0),G&&B?.onSessionEnd(G),W&&(await W.close().catch(()=>void 0),W=null,G=null),X.close(),V&&await V.close(),await K?.().catch(()=>void 0),process.exit(0))};process.on(`SIGINT`,()=>Q(`SIGINT`)),process.on(`SIGTERM`,()=>Q(`SIGTERM`))}export{v as startHttpMode};
|
|
2
|
+
import{a as e,i as t,o as n,s as r}from"./bin.js";import{n as i,t as a}from"./startup-maintenance-L9NUOBVy.js";import{createLogger as o,serializeError as s,setDetailedErrorLoggingEnabled as c}from"../../core/dist/index.js";import{randomUUID as l}from"node:crypto";const u=`__pending__:`;function d(e){return e.startsWith(u)}function f(e){let t=e.headers[`mcp-session-id`];return Array.isArray(t)?t[0]:t}function p(e,t,n,r){e.status(t).json({jsonrpc:`2.0`,error:{code:n,message:r},id:null})}var m=class{options;runtimes=new Map;maxSessions;sessionTimeoutMs;now;constructor(e){this.options=e,this.maxSessions=e.maxSessions??8,this.sessionTimeoutMs=(e.sessionTimeoutMinutes??30)*60*1e3,this.now=e.now??(()=>Date.now())}hasSession(e){return this.runtimes.has(e)}getSessionCount(){return this.runtimes.size}async handleRequest(e,t,n=e.body){let r=f(e),i=r?this.runtimes.get(r):void 0;if(r&&!i){p(t,404,-32001,`Session not found`);return}if(!i){if(e.method!==`POST`){p(t,400,-32e3,`Session required`);return}if(i=await this.createRuntime(t),!i)return}await this.withRuntimeLock(i,async()=>{await i.transport.handleRequest(e,t,n),i.lastAccessAt=this.now();let r=i.transport.sessionId??i.id;e.method!==`DELETE`&&!d(r)&&this.options.onSessionActivity?.(r),e.method===`DELETE`&&!d(r)&&await this.closeSession(r,{closeTransport:!1})})}async closeExpiredSessions(){let e=[...this.runtimes.values()].filter(e=>this.now()-e.lastAccessAt>=this.sessionTimeoutMs).map(e=>e.id);for(let t of e)await this.closeSession(t);return e.length}async closeSession(e,t={}){let n=this.runtimes.get(e);return n?(this.runtimes.delete(e),t.notifySessionEnd!==!1&&!d(e)&&this.options.onSessionEnd?.(e),t.closeTransport!==!1&&await n.transport.close().catch(()=>void 0),await n.server.close().catch(()=>void 0),!0):!1}async closeAll(){let e=[...this.runtimes.keys()];for(let t of e)await this.closeSession(t)}async createRuntime(e){if(await this.closeExpiredSessions(),this.runtimes.size>=this.maxSessions){p(e,503,-32003,`Session capacity reached`);return}let t=this.now(),n=await this.options.createServer(),r={id:`${u}${l()}`,transport:void 0,createdAt:t,lastAccessAt:t,server:n,requestChain:Promise.resolve()},i=this.options.createTransport({sessionIdGenerator:()=>l(),onsessioninitialized:async e=>{this.runtimes.delete(r.id),r.id=e,this.runtimes.set(e,r),this.options.onSessionStart?.(e)},onsessionclosed:async e=>{e&&await this.closeSession(e,{closeTransport:!1})}});return r.transport=i,i.onclose=()=>{let e=r.transport.sessionId??r.id;this.closeSession(e,{closeTransport:!1})},this.runtimes.set(r.id,r),await n.connect(i),r}async withRuntimeLock(e,t){let n=e.requestChain,r;e.requestChain=new Promise(e=>{r=e}),await n;try{await t()}finally{r()}}};function h(e){let t=e.includes(`T`)?e:`${e.replace(` `,`T`)}Z`;return Date.parse(t)}var g=class{stateStore;options;gcTimer=null;constructor(e,t={}){this.stateStore=e,this.options=t}onSessionStart(e,t){this.stateStore.sessionCreate(e,t)}onSessionActivity(e){this.stateStore.sessionTouch(e)}onSessionEnd(e){this.stateStore.sessionDelete(e),Promise.resolve(this.options.onSessionEndMaintenance?.(e)).catch(()=>{})}startGC(){if(this.gcTimer)return;let e=(this.options.gcIntervalMinutes??5)*60*1e3;this.gcTimer=setInterval(()=>{this.runGC()},e),this.gcTimer.unref()}runGC(){let e=this.getStaleSessionIds();for(let t of e)this.options.onBeforeSessionDelete?.(t),Promise.resolve(this.options.onSessionEndMaintenance?.(t)).catch(()=>{}),this.stateStore.sessionDelete(t);return e.length}stop(){this.gcTimer&&=(clearInterval(this.gcTimer),null)}getActiveSessions(){return this.stateStore.sessionList().length}listSessions(){return this.stateStore.sessionList()}getStaleSessionIds(e=Date.now()){let t=(this.options.staleTimeoutMinutes??30)*60*1e3;return this.stateStore.sessionList().filter(n=>{let r=h(n.lastActivity);return Number.isFinite(r)&&e-r>=t}).map(e=>e.sessionId)}};const _=o(`server`);async function v(o,u){let[{default:d},{loadConfig:f,resolveIndexMode:p},{registerDashboardRoutes:h,resolveDashboardDir:v},{registerSettingsRoutes:y,resolveSettingsDir:b},{createSettingsRouter:x},{authMiddleware:S,getOrCreateToken:C}]=await Promise.all([import(`express`),import(`./config-Bx85fwRX.js`),import(`./dashboard-static-dPnij4uF.js`),import(`./settings-static-MepJZjer.js`),import(`./routes-CfG5gdSR.js`),import(`./auth-bEP-6uqy.js`)]),w=f();c(w.logging?.errorDetails===!0),w.configError&&_.warn(`Config load failure`,{error:w.configError}),_.info(`Config loaded`,{sourceCount:w.sources.length,storePath:w.store.path});let T=d();T.use(d.json({limit:`1mb`}));let E=Number(u),D=`http://localhost:${E}`,O=process.env.AIKIT_CORS_ORIGIN??D,k=process.env.AIKIT_ALLOW_ANY_ORIGIN===`true`,A=n(`AIKIT_HTTP_MAX_SESSIONS`,8),j=n(`AIKIT_HTTP_SESSION_TIMEOUT_MINUTES`,30),M=n(`AIKIT_HTTP_SESSION_GC_INTERVAL_MINUTES`,5),N=t({limit:100,windowMs:6e4}),P=!1;T.use((e,t,n)=>{let i=Array.isArray(e.headers.origin)?e.headers.origin[0]:e.headers.origin,a=r({requestOrigin:i,configuredOrigin:O,allowAnyOrigin:k,fallbackOrigin:D});if(a.warn&&!P&&(P=!0,_.warn(`Rejected non-local CORS origin while AIKIT_CORS_ORIGIN=*`,{origin:i,allowAnyOriginEnv:`AIKIT_ALLOW_ANY_ORIGIN=true`})),i&&!a.allowOrigin){t.status(403).json({error:`Origin not allowed`});return}if(a.allowOrigin&&t.setHeader(`Access-Control-Allow-Origin`,a.allowOrigin),t.setHeader(`Access-Control-Allow-Methods`,`GET, POST, PUT, PATCH, DELETE, OPTIONS`),t.setHeader(`Access-Control-Allow-Headers`,`Content-Type, Authorization, Mcp-Session-Id, Mcp-Protocol-Version, Last-Event-ID`),t.setHeader(`Access-Control-Expose-Headers`,`Mcp-Session-Id`),e.method===`OPTIONS`){t.status(204).end();return}n()});let F=C();T.use(S(F)),T.use(`/mcp`,(t,n,r)=>{let i=e(t)??t.ip??t.socket.remoteAddress??`anonymous`;if(N.allow(i)){r();return}let a=Math.max(1,Math.ceil(N.getRetryAfterMs(i)/1e3));n.setHeader(`Retry-After`,String(a)),n.status(429).json({jsonrpc:`2.0`,error:{code:-32003,message:`Rate limit exceeded`},id:null})});let I;T.use(`/mcp`,(e,t,n)=>{if(!I){let t=e.headers[`x-workspace-root`];typeof t==`string`&&t.length>0&&(I=t,_.debug(`Captured workspace root from proxy header`,{wsRoot:t}))}n()}),h(T,v(),_);let L=new Date().toISOString();T.use(`/settings/api`,x({log:_,mcpInfo:()=>({transport:`http`,port:E,pid:process.pid,startedAt:L})})),y(T,b(),_),T.get(`/health`,(e,t)=>{t.json({status:`ok`})});let R=!1,z=null,B=null,V=null,H=null,U=null,W=null,G=null,K=null,q=Promise.resolve(),J=async(t,n)=>{if(!R||!V||!H){n.status(503).json({jsonrpc:`2.0`,error:{code:-32603,message:`Server initializing — please retry in a few seconds`},id:null});return}let r=q,i;q=new Promise(e=>{i=e});let a={POST:6e4,GET:1e4,DELETE:1e4}[t.method]??3e4;if(!await Promise.race([r.then(()=>!0),new Promise(e=>{let n=setTimeout(()=>{_.warn(`mcpLock timed out waiting for previous request, resetting lock chain`,{method:t.method,timeoutMs:a}),e(!1)},a);n.unref&&n.unref()})])&&(q=Promise.resolve(),i=()=>{},W)){let e=W;W=null,G=null,e.close().catch(()=>{})}try{let r=e(t);if(!W){if(r){n.status(404).json({jsonrpc:`2.0`,error:{code:-32001,message:`Session not found`},id:null});return}let e=new H({sessionIdGenerator:()=>l(),onsessioninitialized:async e=>{G=e,B?.onSessionStart(e,{transport:`http`})},onsessionclosed:async e=>{e&&B?.onSessionEnd(e),G=null}});e.onclose=()=>{W===e&&(W=null),G===e.sessionId&&(G=null)},W=e,await V.connect(e)}let i=W;await i.handleRequest(t,n,t.body),t.method!==`DELETE`&&(!r&&i.sessionId?(G=i.sessionId,B?.onSessionStart(i.sessionId,{transport:`http`}),B?.onSessionActivity(i.sessionId)):r&&B?.onSessionActivity(r))}catch(e){if(_.error(`MCP handler error`,s(e)),!n.headersSent){let t=e instanceof Error?e.message:String(e),r=t.includes(`Not Acceptable`);n.status(r?406:500).json({jsonrpc:`2.0`,error:{code:r?-32e3:-32603,message:r?t:`Internal server error`},id:null})}}finally{i()}},Y=async(t,n)=>{let r=e(t);if(U&&(!W||r!==G)){await U.handleRequest(t,n,t.body);return}await J(t,n)};T.post(`/mcp`,Y),T.get(`/mcp`,Y),T.delete(`/mcp`,Y);let X=T.listen(E,`127.0.0.1`,()=>{_.info(`MCP server listening`,{url:`http://127.0.0.1:${E}/mcp`,port:E}),setTimeout(async()=>{try{typeof I==`string`&&I.length>0&&(w.sources[0]={path:I,excludePatterns:w.sources[0]?.excludePatterns??[]},w.allRoots=[I],_.debug(`Workspace root applied from proxy header`,{wsRoot:I}));let[{createLazyServer:e,createMcpServer:t,ALL_TOOL_NAMES:n},{StreamableHTTPServerTransport:r},{checkForUpdates:c,autoUpgradeScaffold:l}]=await Promise.all([import(`./server-CmZQTw9I.js`),import(`@modelcontextprotocol/sdk/server/streamableHttp.js`),import(`./version-check-CdBHTxtt.js`)]);c(),l(),setInterval(c,1440*60*1e3).unref();let u=!1,d=p(w),f=e(w,d);K=async()=>{f.aikit&&await Promise.all([f.aikit.embedder.shutdown?.().catch(()=>{})??Promise.resolve(),f.aikit.graphStore.close().catch(()=>{}),f.aikit.closeStateStore?.().catch(()=>{})??Promise.resolve(),f.aikit.store.close().catch(()=>{})])},V=f.server,H=r,R=!0,_.debug(`MCP server configured (lazy — AI Kit initializing in background)`,{toolCount:n.length,resourceCount:2}),f.startInit(),f.ready.then(async()=>{try{if(!f.aikit)throw Error(`AI Kit components are not available after initialization`);B=new g(f.aikit.stateStore,{staleTimeoutMinutes:j,gcIntervalMinutes:M,onBeforeSessionDelete:e=>{if(G===e&&W){let e=W;W=null,G=null,e.close().catch(()=>void 0)}U?.closeSession(e,{notifySessionEnd:!1})},onSessionEndMaintenance:async()=>{if(!f.aikit?.curated||!f.aikit?.stateStore)return;let{pruneLessons:e}=await import(`./evolution-DWaEE6XW.js`).then(e=>e.t),t=await e(f.aikit.curated,f.aikit.stateStore,{dryRun:!1});t.pruned.length>0&&_.info(`Session-end lesson prune`,{pruned:t.pruned.length})}}),U=new m({createServer:()=>{if(!f.aikit)throw Error(`AI Kit components are not available after initialization`);return t(f.aikit,w)},createTransport:e=>new r(e),maxSessions:A,sessionTimeoutMinutes:j,onSessionStart:e=>B?.onSessionStart(e,{transport:`http`}),onSessionActivity:e=>B?.onSessionActivity(e),onSessionEnd:e=>B?.onSessionEnd(e)}),B.startGC(),G&&(B.onSessionStart(G,{transport:`http`}),B.onSessionActivity(G)),_.info(`HTTP session runtime ready`,{maxSessions:A,sessionTimeoutMinutes:j,gcIntervalMinutes:M})}catch(e){_.error(`Failed to start session manager`,s(e)),R=!1,u=!0,V=null,H=null,K=null}}),d===`auto`?f.ready.then(async()=>{try{let e=w.sources.map(e=>e.path).join(`, `);_.info(`Running initial index`,{sourcePaths:e}),await f.runInitialIndex(),_.info(`Initial index complete`)}catch(e){_.error(`Initial index failed; will retry on aikit_reindex`,i(o,e))}}):d===`smart`?u||f.ready.then(async()=>{try{if(!f.aikit)throw Error(`AI Kit components are not available after initialization`);let{SmartIndexScheduler:e}=await import(`../../indexer/dist/index.js`),t=new e(f.aikit.indexer,w,f.aikit.store),n=f.aikit.store;z=t,t.start(),n.onBeforeClose&&n.onBeforeClose(()=>t.stop()),f.setSmartScheduler(t),_.debug(`Smart index scheduler started (HTTP mode)`)}catch(e){_.error(`Failed to start smart index scheduler`,i(o,e))}}):_.info(`Initial full indexing skipped in HTTP mode`,{indexMode:d}),f.ready.catch(e=>{_.error(`AI Kit initialization failed`,i(o,e)),R=!1,u=!0,V=null,H=null,K=null}),a(f.ready,()=>f.aikit?{curated:f.aikit.curated,stateStore:f.aikit.stateStore}:null,o)}catch(e){_.error(`Failed to load server modules`,i(o,e)),R=!1,K=null}},100)}),Z=!1,Q=async e=>{Z||(Z=!0,_.info(`Shutdown signal received`,{signal:e}),z?.stop(),B?.stop(),await import(`../../tools/dist/index.js`).then(({processStopAll:e})=>e()).catch(()=>{}),await U?.closeAll().catch(()=>void 0),G&&B?.onSessionEnd(G),W&&(await W.close().catch(()=>void 0),W=null,G=null),X.close(),V&&await V.close(),await K?.().catch(()=>void 0),process.exit(0))};process.on(`SIGINT`,()=>Q(`SIGINT`)),process.on(`SIGTERM`,()=>Q(`SIGTERM`))}export{v as startHttpMode};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{n as e}from"./workspace-bootstrap-BJloolzr.js";import{n as t,t as n}from"./startup-maintenance-D0Uhpi3k.js";import{t as r}from"./repair-json-B6Q_HRoP.js";import{createLogger as i,serializeError as a,setDetailedErrorLoggingEnabled as o}from"../../core/dist/index.js";const s=i(`server`);var c=class{buffer;append(e){this.buffer=this.buffer?Buffer.concat([this.buffer,e]):e}readMessage(){if(!this.buffer)return null;for(let e=0;e<this.buffer.length;e++){if(this.buffer[e]!==10)continue;let t=this.buffer.toString(`utf8`,0,e).replace(/\r$/,``),n=e+1;try{let e=JSON.parse(t);return this.buffer=this.buffer.subarray(n),e}catch{try{let e=r(t);return s.warn(`JSON parse failed on MCP message — repair succeeded`,{preview:t.slice(0,80)}),this.buffer=this.buffer.subarray(n),e}catch{}}}return null}clear(){this.buffer=void 0}};async function l(r){let[{loadConfig:i,reconfigureForWorkspace:l,resolveIndexMode:u},{createLazyServer:d},{checkForUpdates:f,autoUpgradeScaffold:p},{RootsListChangedNotificationSchema:m}]=await Promise.all([import(`./config-B-wvmMyo.js`),import(`./server-
|
|
1
|
+
import{n as e}from"./workspace-bootstrap-BJloolzr.js";import{n as t,t as n}from"./startup-maintenance-D0Uhpi3k.js";import{t as r}from"./repair-json-B6Q_HRoP.js";import{createLogger as i,serializeError as a,setDetailedErrorLoggingEnabled as o}from"../../core/dist/index.js";const s=i(`server`);var c=class{buffer;append(e){this.buffer=this.buffer?Buffer.concat([this.buffer,e]):e}readMessage(){if(!this.buffer)return null;for(let e=0;e<this.buffer.length;e++){if(this.buffer[e]!==10)continue;let t=this.buffer.toString(`utf8`,0,e).replace(/\r$/,``),n=e+1;try{let e=JSON.parse(t);return this.buffer=this.buffer.subarray(n),e}catch{try{let e=r(t);return s.warn(`JSON parse failed on MCP message — repair succeeded`,{preview:t.slice(0,80)}),this.buffer=this.buffer.subarray(n),e}catch{}}}return null}clear(){this.buffer=void 0}};async function l(r){let[{loadConfig:i,reconfigureForWorkspace:l,resolveIndexMode:u},{createLazyServer:d},{checkForUpdates:f,autoUpgradeScaffold:p},{RootsListChangedNotificationSchema:m}]=await Promise.all([import(`./config-B-wvmMyo.js`),import(`./server-CijzehmX.js`),import(`./version-check-CggUKvv8.js`),import(`@modelcontextprotocol/sdk/types.js`)]),h=i();o(h.logging?.errorDetails===!0),h.configError&&s.warn(`Config load failure`,{error:h.configError}),s.info(`Config loaded`,{sourceCount:h.sources.length,storePath:h.store.path}),p(),setInterval(f,1440*60*1e3).unref();let g=u(h),_=d(h,g),{server:v,startInit:y,ready:b,runInitialIndex:x}=_,{StdioServerTransport:S}=await import(`@modelcontextprotocol/sdk/server/stdio.js`),C=new S;if(typeof C._readBuffer?.readMessage!=`function`)throw Error(`Cannot install JSON repair: StdioServerTransport._readBuffer has unexpected shape`);C._readBuffer=new c,s.debug(`JSON repair installed on stdio transport`),await v.connect(C),s.debug(`MCP server started`,{transport:`stdio`}),await e({config:h,indexMode:g,log:s,rootsChangedNotificationSchema:m,reconfigureForWorkspace:l,runInitialIndex:x,server:v,startInit:y,version:r});let w=null,T=null,E=!1,D=async e=>{E||(E=!0,s.info(`Shutdown signal received`,{signal:e}),w&&=(clearTimeout(w),null),T?.stop(),await import(`../../tools/dist/index.js`).then(({processStopAll:e})=>e()).catch(()=>{}),await C.close().catch(()=>void 0),await v.close().catch(()=>void 0),_.aikit&&await Promise.all([_.aikit.embedder.shutdown?.().catch(()=>{})??Promise.resolve(),_.aikit.graphStore.close().catch(()=>{}),_.aikit.closeStateStore?.().catch(()=>{})??Promise.resolve(),_.aikit.store.close().catch(()=>{})]),process.exit(0))},O=()=>{w&&clearTimeout(w),w=setTimeout(async()=>{s.info(`Auto-shutdown: no activity for 30 minutes — releasing resources`);try{let e=_.aikit;e&&(e.embedder.shutdown?.().catch(()=>{}),e.store.releaseMemory?.(),e.graphStore.releaseMemory?.())}catch(e){s.warn(`Resource release failed during shutdown`,a(e))}},18e5),w.unref&&w.unref()};O(),process.stdin.on(`data`,()=>O()),process.stdin.on(`end`,()=>void D(`stdin-end`)),process.stdin.on(`close`,()=>void D(`stdin-close`)),process.stdin.on(`error`,()=>void D(`stdin-error`)),process.on(`SIGINT`,()=>void D(`SIGINT`)),process.on(`SIGTERM`,()=>void D(`SIGTERM`)),b.catch(e=>{s.error(`Initialization failed — server will continue with limited tools`,t(r,e))}),g===`smart`?b.then(async()=>{try{if(!_.aikit)throw Error(`AI Kit components are not available after initialization`);let{SmartIndexScheduler:e}=await import(`../../indexer/dist/index.js`),t=new e(_.aikit.indexer,h,_.aikit.store);T=t;let n=_.aikit.store;t.start(),n.onBeforeClose&&n.onBeforeClose(()=>t.stop()),_.setSmartScheduler(t),s.debug(`Smart index scheduler started (stdio mode)`)}catch(e){s.error(`Failed to start smart index scheduler`,t(r,e))}}).catch(e=>s.error(`AI Kit init failed for smart scheduler`,t(r,e))):s.warn(`Initial full indexing skipped; use aikit_reindex to index manually`,{indexMode:g}),n(b,()=>_.aikit?{curated:_.aikit.curated,stateStore:_.aikit.stateStore}:null,r)}export{l as startStdioMode};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import{r as e}from"./bin.js";import{n as t,t as n}from"./startup-maintenance-L9NUOBVy.js";import{t as r}from"./repair-json-D4mft_HA.js";import{createLogger as i,serializeError as a,setDetailedErrorLoggingEnabled as o}from"../../core/dist/index.js";const s=i(`server`);var c=class{buffer;append(e){this.buffer=this.buffer?Buffer.concat([this.buffer,e]):e}readMessage(){if(!this.buffer)return null;for(let e=0;e<this.buffer.length;e++){if(this.buffer[e]!==10)continue;let t=this.buffer.toString(`utf8`,0,e).replace(/\r$/,``),n=e+1;try{let e=JSON.parse(t);return this.buffer=this.buffer.subarray(n),e}catch{try{let e=r(t);return s.warn(`JSON parse failed on MCP message — repair succeeded`,{preview:t.slice(0,80)}),this.buffer=this.buffer.subarray(n),e}catch{}}}return null}clear(){this.buffer=void 0}};async function l(r){let[{loadConfig:i,reconfigureForWorkspace:l,resolveIndexMode:u},{createLazyServer:d},{checkForUpdates:f,autoUpgradeScaffold:p},{RootsListChangedNotificationSchema:m}]=await Promise.all([import(`./config-Bx85fwRX.js`),import(`./server-
|
|
2
|
+
import{r as e}from"./bin.js";import{n as t,t as n}from"./startup-maintenance-L9NUOBVy.js";import{t as r}from"./repair-json-D4mft_HA.js";import{createLogger as i,serializeError as a,setDetailedErrorLoggingEnabled as o}from"../../core/dist/index.js";const s=i(`server`);var c=class{buffer;append(e){this.buffer=this.buffer?Buffer.concat([this.buffer,e]):e}readMessage(){if(!this.buffer)return null;for(let e=0;e<this.buffer.length;e++){if(this.buffer[e]!==10)continue;let t=this.buffer.toString(`utf8`,0,e).replace(/\r$/,``),n=e+1;try{let e=JSON.parse(t);return this.buffer=this.buffer.subarray(n),e}catch{try{let e=r(t);return s.warn(`JSON parse failed on MCP message — repair succeeded`,{preview:t.slice(0,80)}),this.buffer=this.buffer.subarray(n),e}catch{}}}return null}clear(){this.buffer=void 0}};async function l(r){let[{loadConfig:i,reconfigureForWorkspace:l,resolveIndexMode:u},{createLazyServer:d},{checkForUpdates:f,autoUpgradeScaffold:p},{RootsListChangedNotificationSchema:m}]=await Promise.all([import(`./config-Bx85fwRX.js`),import(`./server-CmZQTw9I.js`),import(`./version-check-CdBHTxtt.js`),import(`@modelcontextprotocol/sdk/types.js`)]),h=i();o(h.logging?.errorDetails===!0),h.configError&&s.warn(`Config load failure`,{error:h.configError}),s.info(`Config loaded`,{sourceCount:h.sources.length,storePath:h.store.path}),p(),setInterval(f,1440*60*1e3).unref();let g=u(h),_=d(h,g),{server:v,startInit:y,ready:b,runInitialIndex:x}=_,{StdioServerTransport:S}=await import(`@modelcontextprotocol/sdk/server/stdio.js`),C=new S;if(typeof C._readBuffer?.readMessage!=`function`)throw Error(`Cannot install JSON repair: StdioServerTransport._readBuffer has unexpected shape`);C._readBuffer=new c,s.debug(`JSON repair installed on stdio transport`),await v.connect(C),s.debug(`MCP server started`,{transport:`stdio`}),await e({config:h,indexMode:g,log:s,rootsChangedNotificationSchema:m,reconfigureForWorkspace:l,runInitialIndex:x,server:v,startInit:y,version:r});let w=null,T=null,E=!1,D=async e=>{E||(E=!0,s.info(`Shutdown signal received`,{signal:e}),w&&=(clearTimeout(w),null),T?.stop(),await import(`../../tools/dist/index.js`).then(({processStopAll:e})=>e()).catch(()=>{}),await C.close().catch(()=>void 0),await v.close().catch(()=>void 0),_.aikit&&await Promise.all([_.aikit.embedder.shutdown?.().catch(()=>{})??Promise.resolve(),_.aikit.graphStore.close().catch(()=>{}),_.aikit.closeStateStore?.().catch(()=>{})??Promise.resolve(),_.aikit.store.close().catch(()=>{})]),process.exit(0))},O=()=>{w&&clearTimeout(w),w=setTimeout(async()=>{s.info(`Auto-shutdown: no activity for 30 minutes — releasing resources`);try{let e=_.aikit;e&&(e.embedder.shutdown?.().catch(()=>{}),e.store.releaseMemory?.(),e.graphStore.releaseMemory?.())}catch(e){s.warn(`Resource release failed during shutdown`,a(e))}},18e5),w.unref&&w.unref()};O(),process.stdin.on(`data`,()=>O()),process.stdin.on(`end`,()=>void D(`stdin-end`)),process.stdin.on(`close`,()=>void D(`stdin-close`)),process.stdin.on(`error`,()=>void D(`stdin-error`)),process.on(`SIGINT`,()=>void D(`SIGINT`)),process.on(`SIGTERM`,()=>void D(`SIGTERM`)),b.catch(e=>{s.error(`Initialization failed — server will continue with limited tools`,t(r,e))}),g===`smart`?b.then(async()=>{try{if(!_.aikit)throw Error(`AI Kit components are not available after initialization`);let{SmartIndexScheduler:e}=await import(`../../indexer/dist/index.js`),t=new e(_.aikit.indexer,h,_.aikit.store);T=t;let n=_.aikit.store;t.start(),n.onBeforeClose&&n.onBeforeClose(()=>t.stop()),_.setSmartScheduler(t),s.debug(`Smart index scheduler started (stdio mode)`)}catch(e){s.error(`Failed to start smart index scheduler`,t(r,e))}}).catch(e=>s.error(`AI Kit init failed for smart scheduler`,t(r,e))):s.warn(`Initial full indexing skipped; use aikit_reindex to index manually`,{indexMode:g}),n(b,()=>_.aikit?{curated:_.aikit.curated,stateStore:_.aikit.stateStore}:null,r)}export{l as startStdioMode};
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import{execSync as e}from"node:child_process";import{existsSync as t,readFileSync as n,statSync as r}from"node:fs";import{dirname as i,join as a,resolve as o}from"node:path";import{fileURLToPath as s}from"node:url";import{SKILLS as c}from"../definitions/skills/index.mjs";import{ALL_BLOCK_DOCS as l,BLOCK_DOCS_BY_SKILL as u,BLOCK_TYPE_LIST as d}from"../generated/block-docs.mjs";const f=i(s(import.meta.url)),p=t(a(f,`..`,`..`,`packages`))?o(f,`..`,`..`):o(f,`..`,`..`,`..`),m=a(p,`packages`,`viewers
|
|
1
|
+
import{execSync as e}from"node:child_process";import{existsSync as t,readFileSync as n,statSync as r}from"node:fs";import{dirname as i,join as a,resolve as o}from"node:path";import{fileURLToPath as s}from"node:url";import{SKILLS as c}from"../definitions/skills/index.mjs";import{ALL_BLOCK_DOCS as l,BLOCK_DOCS_BY_SKILL as u,BLOCK_TYPE_LIST as d}from"../generated/block-docs.mjs";const f=i(s(import.meta.url)),p=t(a(f,`..`,`..`,`packages`))?o(f,`..`,`..`):o(f,`..`,`..`,`..`),m=a(p,`packages`,`server`,`viewers`),h=a(p,`packages`,`viewers`),g={"c4-architecture":[`c4-viewer.html`,`canvas.html`],docs:[`canvas.html`,`tour-viewer.html`]},_={bySkill:u,all:l,typeList:d};function v(){let n=a(m,`c4-viewer.html`);if(t(n)){let e=r(n).mtimeMs,i=[`c4-viewer.html`,`tour-viewer.html`,`index.html`].map(e=>a(h,e)).filter(e=>t(e)).map(e=>r(e).mtimeMs);if(Math.max(...i,0)<=e)return}try{process.stderr.write(`Building viewer assets...
|
|
2
2
|
`),e(`pnpm build`,{cwd:h,stdio:`inherit`})}catch{process.stderr.write(`Warning: could not build viewer assets. Viewer HTML files will be missing from skill output.
|
|
3
3
|
`)}}function y(){let e=[],r=!1;for(let[i,o]of Object.entries(c)){let s=typeof o==`function`?o({blockDocs:_}):o;for(let{file:t,content:n}of s)e.push({path:`${i}/${t}`,content:n});let c=g[i];if(c){r||=(v(),!0);for(let r of c){let o=a(m,r);t(o)&&e.push({path:`${i}/assets/${r}`,content:n(o,`utf8`)})}}}return e}export{y as generateSkills};
|
|
@@ -1,798 +0,0 @@
|
|
|
1
|
-
# AI Kit Viewers
|
|
2
|
-
|
|
3
|
-
Self-contained HTML viewers for AI Kit skills. Each viewer is built from React 19 source, bundled with Vite, flattened into a single HTML file with `vite-plugin-singlefile`, and then embedded into scaffold skill definitions.
|
|
4
|
-
|
|
5
|
-
This folder is a standalone package. It is not part of the parent pnpm workspace. Install and build it from this directory with `--ignore-workspace`.
|
|
6
|
-
|
|
7
|
-
## Overview
|
|
8
|
-
|
|
9
|
-
Use this package when a skill needs a rich interactive artifact that can ship as one HTML asset.
|
|
10
|
-
|
|
11
|
-
Current viewers:
|
|
12
|
-
|
|
13
|
-
- `c4-viewer.html` -> C4 architecture viewer powered by ReactFlow and ELK layout
|
|
14
|
-
- `tour-viewer.html` -> interactive code tour viewer powered by ReactFlow and a BFS tree layout
|
|
15
|
-
|
|
16
|
-
Why single-file HTML:
|
|
17
|
-
|
|
18
|
-
- AI Kit skill definitions embed assets as template literals in `.mjs` files
|
|
19
|
-
- A self-contained HTML file avoids external JS, CSS, font, and image dependencies
|
|
20
|
-
- Embedding one file is simpler and more reliable than coordinating asset directories
|
|
21
|
-
- Browser rendering is predictable inside skill-driven workflows
|
|
22
|
-
|
|
23
|
-
## Quick Start
|
|
24
|
-
|
|
25
|
-
Run these commands from `packages/viewers`.
|
|
26
|
-
|
|
27
|
-
```bash
|
|
28
|
-
pnpm install --ignore-workspace
|
|
29
|
-
pnpm build
|
|
30
|
-
pnpm embed
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
Available scripts:
|
|
34
|
-
|
|
35
|
-
```bash
|
|
36
|
-
pnpm dev:c4
|
|
37
|
-
pnpm dev:tour
|
|
38
|
-
pnpm build
|
|
39
|
-
pnpm embed
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
What each command does:
|
|
43
|
-
|
|
44
|
-
- `pnpm install --ignore-workspace` installs this package independently from the monorepo root
|
|
45
|
-
- `pnpm dev:c4` serves the C4 entry HTML with `vite.c4.config.ts`
|
|
46
|
-
- `pnpm dev:tour` serves the Tour entry HTML with `vite.tour.config.ts`
|
|
47
|
-
- `pnpm build` writes single-file artifacts into `dist/`
|
|
48
|
-
- `pnpm embed` injects built HTML into scaffold skill definitions
|
|
49
|
-
|
|
50
|
-
After `pnpm embed`, rebuild the parent scaffold output from the `aikit-mcp` root if you need refreshed generated artifacts. `embed.mjs` prints that reminder explicitly.
|
|
51
|
-
|
|
52
|
-
## Package Layout
|
|
53
|
-
|
|
54
|
-
```text
|
|
55
|
-
viewers/
|
|
56
|
-
├── README.md
|
|
57
|
-
├── package.json
|
|
58
|
-
├── tsconfig.json
|
|
59
|
-
├── pnpm-lock.yaml
|
|
60
|
-
├── .gitignore
|
|
61
|
-
├── c4-viewer.html
|
|
62
|
-
├── tour-viewer.html
|
|
63
|
-
├── vite.c4.config.ts
|
|
64
|
-
├── vite.tour.config.ts
|
|
65
|
-
├── embed.mjs
|
|
66
|
-
├── src/
|
|
67
|
-
│ ├── env.d.ts
|
|
68
|
-
│ ├── shared/
|
|
69
|
-
│ │ ├── theme.css
|
|
70
|
-
│ │ ├── types.ts
|
|
71
|
-
│ │ ├── ThemeProvider.tsx
|
|
72
|
-
│ │ ├── Layout.tsx
|
|
73
|
-
│ │ ├── Header.tsx
|
|
74
|
-
│ │ ├── Footer.tsx
|
|
75
|
-
│ │ ├── Sidebar.tsx
|
|
76
|
-
│ │ ├── Card.tsx
|
|
77
|
-
│ │ ├── Badge.tsx
|
|
78
|
-
│ │ ├── simple-template.css
|
|
79
|
-
│ │ └── simple-template.html
|
|
80
|
-
│ ├── c4/
|
|
81
|
-
│ │ ├── C4Viewer.tsx
|
|
82
|
-
│ │ └── main.tsx
|
|
83
|
-
│ └── tour/
|
|
84
|
-
│ ├── TourViewer.tsx
|
|
85
|
-
│ └── main.tsx
|
|
86
|
-
└── dist/
|
|
87
|
-
├── c4-viewer.html
|
|
88
|
-
└── tour-viewer.html
|
|
89
|
-
```
|
|
90
|
-
|
|
91
|
-
Important distinction:
|
|
92
|
-
|
|
93
|
-
- Root `*-viewer.html` files are Vite entry templates
|
|
94
|
-
- `dist/*.html` files are the final self-contained artifacts
|
|
95
|
-
- `embed.mjs` reads from `dist/`, not from the root entry templates
|
|
96
|
-
- `package.json` `exports` points at the built viewer HTML assets, which keeps workspace health satisfied without changing the standalone build flow
|
|
97
|
-
|
|
98
|
-
## Architecture
|
|
99
|
-
|
|
100
|
-
## Build Stack
|
|
101
|
-
|
|
102
|
-
- React 19 for the viewer UI
|
|
103
|
-
- `@xyflow/react` v12 for graph rendering and interaction
|
|
104
|
-
- `elkjs` for auto-layout in the C4 viewer
|
|
105
|
-
- Vite 8 for dev/build
|
|
106
|
-
- `vite-plugin-singlefile` to inline JS and CSS into one HTML document
|
|
107
|
-
|
|
108
|
-
## Runtime Shape
|
|
109
|
-
|
|
110
|
-
Each full viewer follows the same structure:
|
|
111
|
-
|
|
112
|
-
1. Root HTML entry provides a `<div id="root"></div>` and optional JSON payload script tag
|
|
113
|
-
2. `src/<viewer>/main.tsx` reads `window.__VIEWER_CONFIG__` first, then falls back to legacy inline JSON
|
|
114
|
-
3. The viewer component wraps its content in `ThemeProvider`
|
|
115
|
-
4. `Layout`, `Header`, and `Footer` provide the shared shell
|
|
116
|
-
5. ReactFlow renders the interactive canvas
|
|
117
|
-
6. Vite emits a single HTML file into `dist/`
|
|
118
|
-
7. `embed.mjs` injects that HTML into the relevant skill definition file
|
|
119
|
-
|
|
120
|
-
## Shared Template System
|
|
121
|
-
|
|
122
|
-
`src/shared/` is the design system and shell contract for full viewers.
|
|
123
|
-
|
|
124
|
-
Treat it as the source of truth for:
|
|
125
|
-
|
|
126
|
-
- theme tokens
|
|
127
|
-
- layout variants
|
|
128
|
-
- header and footer behavior
|
|
129
|
-
- shared TypeScript interfaces
|
|
130
|
-
- reusable UI primitives
|
|
131
|
-
|
|
132
|
-
Two template tiers live here:
|
|
133
|
-
|
|
134
|
-
- Full template: React + ReactFlow viewers such as C4 and Tour
|
|
135
|
-
- Simple template: `simple-template.html` + `simple-template.css` for pure HTML/CSS rendering patterns
|
|
136
|
-
|
|
137
|
-
Use the full template for interactive graph viewers. Use the simple template only when a viewer does not need React or graph interaction.
|
|
138
|
-
|
|
139
|
-
## Shared Components
|
|
140
|
-
|
|
141
|
-
Every new full viewer should use the shared shell components unless there is a strong reason not to.
|
|
142
|
-
|
|
143
|
-
## `ThemeProvider`
|
|
144
|
-
|
|
145
|
-
Purpose:
|
|
146
|
-
|
|
147
|
-
- owns the light/dark/system theme mode state
|
|
148
|
-
- resolves system preference with `matchMedia('(prefers-color-scheme: dark)')`
|
|
149
|
-
- persists mode to `localStorage` under `aikit-theme-mode`
|
|
150
|
-
- writes the active theme to `document.documentElement.dataset.theme`
|
|
151
|
-
|
|
152
|
-
API:
|
|
153
|
-
|
|
154
|
-
```tsx
|
|
155
|
-
<ThemeProvider defaultMode="system">...</ThemeProvider>
|
|
156
|
-
```
|
|
157
|
-
|
|
158
|
-
Context values:
|
|
159
|
-
|
|
160
|
-
```ts
|
|
161
|
-
{
|
|
162
|
-
mode: 'light' | 'dark' | 'system';
|
|
163
|
-
resolvedMode: 'light' | 'dark';
|
|
164
|
-
setMode: (mode: ThemeMode) => void;
|
|
165
|
-
toggleMode: () => void;
|
|
166
|
-
}
|
|
167
|
-
```
|
|
168
|
-
|
|
169
|
-
Rules:
|
|
170
|
-
|
|
171
|
-
- Full viewers must be wrapped in `ThemeProvider`
|
|
172
|
-
- `defaultMode` should stay `system` unless you have a documented override
|
|
173
|
-
- Theme toggle behavior should remain centralized here, not reimplemented in individual viewers
|
|
174
|
-
|
|
175
|
-
## `Layout`
|
|
176
|
-
|
|
177
|
-
Purpose:
|
|
178
|
-
|
|
179
|
-
- applies the top-level CSS Grid layout class
|
|
180
|
-
|
|
181
|
-
Props:
|
|
182
|
-
|
|
183
|
-
```ts
|
|
184
|
-
interface LayoutProps {
|
|
185
|
-
children: React.ReactNode;
|
|
186
|
-
variant?: 'full' | 'sidebar-left' | 'sidebar-right';
|
|
187
|
-
className?: string;
|
|
188
|
-
style?: React.CSSProperties;
|
|
189
|
-
}
|
|
190
|
-
```
|
|
191
|
-
|
|
192
|
-
Usage:
|
|
193
|
-
|
|
194
|
-
```tsx
|
|
195
|
-
<Layout variant="full">
|
|
196
|
-
<Header />
|
|
197
|
-
<main />
|
|
198
|
-
<Footer />
|
|
199
|
-
</Layout>
|
|
200
|
-
```
|
|
201
|
-
|
|
202
|
-
## `Header`
|
|
203
|
-
|
|
204
|
-
Purpose:
|
|
205
|
-
|
|
206
|
-
- renders the sticky top bar
|
|
207
|
-
- shows the viewer title and optional subtitle
|
|
208
|
-
- exposes the shared theme toggle button
|
|
209
|
-
|
|
210
|
-
Props:
|
|
211
|
-
|
|
212
|
-
```ts
|
|
213
|
-
interface HeaderConfig {
|
|
214
|
-
title: string;
|
|
215
|
-
subtitle?: string;
|
|
216
|
-
showThemeToggle?: boolean;
|
|
217
|
-
}
|
|
218
|
-
```
|
|
219
|
-
|
|
220
|
-
Defaults:
|
|
221
|
-
|
|
222
|
-
- `title` defaults to `AI KIT`
|
|
223
|
-
- `showThemeToggle` defaults to `true`
|
|
224
|
-
|
|
225
|
-
Usage:
|
|
226
|
-
|
|
227
|
-
```tsx
|
|
228
|
-
<Header title="My Viewer" subtitle="Context for this artifact" />
|
|
229
|
-
```
|
|
230
|
-
|
|
231
|
-
Rule:
|
|
232
|
-
|
|
233
|
-
- Pass a viewer-specific title instead of changing the shared default
|
|
234
|
-
|
|
235
|
-
## `Footer`
|
|
236
|
-
|
|
237
|
-
Purpose:
|
|
238
|
-
|
|
239
|
-
- renders attribution
|
|
240
|
-
- optionally shows a timestamp
|
|
241
|
-
- optionally shows a legend
|
|
242
|
-
|
|
243
|
-
Props:
|
|
244
|
-
|
|
245
|
-
```ts
|
|
246
|
-
interface FooterConfig {
|
|
247
|
-
showTimestamp?: boolean;
|
|
248
|
-
showAttribution?: boolean;
|
|
249
|
-
legend?: Array<{ label: string; color: string; icon?: string }>;
|
|
250
|
-
}
|
|
251
|
-
```
|
|
252
|
-
|
|
253
|
-
Usage:
|
|
254
|
-
|
|
255
|
-
```tsx
|
|
256
|
-
<Footer
|
|
257
|
-
legend={[
|
|
258
|
-
{ label: 'system', color: 'var(--c4-system)' },
|
|
259
|
-
{ label: 'container', color: 'var(--c4-container)' },
|
|
260
|
-
]}
|
|
261
|
-
/>
|
|
262
|
-
```
|
|
263
|
-
|
|
264
|
-
Rule:
|
|
265
|
-
|
|
266
|
-
- Leave attribution enabled unless the host explicitly requires a different footer contract
|
|
267
|
-
|
|
268
|
-
## `Sidebar`
|
|
269
|
-
|
|
270
|
-
Purpose:
|
|
271
|
-
|
|
272
|
-
- provides a collapsible inspector panel
|
|
273
|
-
- supports left or right placement
|
|
274
|
-
- becomes an overlay on mobile
|
|
275
|
-
|
|
276
|
-
Props:
|
|
277
|
-
|
|
278
|
-
```ts
|
|
279
|
-
interface SidebarProps {
|
|
280
|
-
children: React.ReactNode;
|
|
281
|
-
position?: 'left' | 'right';
|
|
282
|
-
collapsible?: boolean;
|
|
283
|
-
defaultCollapsed?: boolean;
|
|
284
|
-
width?: number;
|
|
285
|
-
className?: string;
|
|
286
|
-
title?: string;
|
|
287
|
-
}
|
|
288
|
-
```
|
|
289
|
-
|
|
290
|
-
Use this when a viewer needs metadata, filters, or a drill-down panel. Do not build a custom sidebar first.
|
|
291
|
-
|
|
292
|
-
## `Card`
|
|
293
|
-
|
|
294
|
-
Purpose:
|
|
295
|
-
|
|
296
|
-
- reusable surface for detail panes, metrics, or inspector content
|
|
297
|
-
- supports a gradient orb effect and clickable states
|
|
298
|
-
|
|
299
|
-
Props:
|
|
300
|
-
|
|
301
|
-
```ts
|
|
302
|
-
{
|
|
303
|
-
children: React.ReactNode;
|
|
304
|
-
className?: string;
|
|
305
|
-
gradient?: boolean;
|
|
306
|
-
onClick?: () => void;
|
|
307
|
-
variant?: 'default' | 'outlined' | 'elevated';
|
|
308
|
-
}
|
|
309
|
-
```
|
|
310
|
-
|
|
311
|
-
Rule:
|
|
312
|
-
|
|
313
|
-
- Use `gradient` for featured blocks only. Do not turn every card into a visual accent.
|
|
314
|
-
|
|
315
|
-
## `Badge`
|
|
316
|
-
|
|
317
|
-
Purpose:
|
|
318
|
-
|
|
319
|
-
- compact status and category labels
|
|
320
|
-
|
|
321
|
-
Props:
|
|
322
|
-
|
|
323
|
-
```ts
|
|
324
|
-
{
|
|
325
|
-
children: React.ReactNode;
|
|
326
|
-
className?: string;
|
|
327
|
-
variant?: 'default' | 'primary' | 'success' | 'warning' | 'error' | 'info';
|
|
328
|
-
size?: 'sm' | 'md';
|
|
329
|
-
}
|
|
330
|
-
```
|
|
331
|
-
|
|
332
|
-
Rule:
|
|
333
|
-
|
|
334
|
-
- Prefer semantic variants over custom inline styling
|
|
335
|
-
|
|
336
|
-
## `types.ts`
|
|
337
|
-
|
|
338
|
-
Purpose:
|
|
339
|
-
|
|
340
|
-
- shared viewer contract for theme, layout, header, footer, C4 data, tour data, and the top-level `ViewerConfig`
|
|
341
|
-
|
|
342
|
-
Use existing types first. Extend them only when a new viewer needs a real shared contract.
|
|
343
|
-
|
|
344
|
-
## Theme System
|
|
345
|
-
|
|
346
|
-
Theme state is split across React state and CSS custom properties.
|
|
347
|
-
|
|
348
|
-
Mechanics:
|
|
349
|
-
|
|
350
|
-
- `ThemeProvider` tracks `mode: 'light' | 'dark' | 'system'`
|
|
351
|
-
- `resolvedMode` is always `'light'` or `'dark'`
|
|
352
|
-
- the provider writes `data-theme="light"` or `data-theme="dark"` on `<html>`
|
|
353
|
-
- `theme.css` defines the token sets for `:root` and `[data-theme="dark"]`
|
|
354
|
-
- if no explicit mode is active, `@media (prefers-color-scheme: dark)` provides the system fallback
|
|
355
|
-
|
|
356
|
-
### Key Theme Tokens
|
|
357
|
-
|
|
358
|
-
Core surface tokens:
|
|
359
|
-
|
|
360
|
-
- `--background`
|
|
361
|
-
- `--foreground`
|
|
362
|
-
- `--card`
|
|
363
|
-
- `--card-foreground`
|
|
364
|
-
- `--primary`
|
|
365
|
-
- `--primary-foreground`
|
|
366
|
-
- `--muted`
|
|
367
|
-
- `--muted-foreground`
|
|
368
|
-
- `--accent`
|
|
369
|
-
- `--accent-foreground`
|
|
370
|
-
- `--border`
|
|
371
|
-
|
|
372
|
-
Supplementary tokens used by shared components:
|
|
373
|
-
|
|
374
|
-
- `--sidebar`
|
|
375
|
-
- `--sidebar-foreground`
|
|
376
|
-
- `--color-success`
|
|
377
|
-
- `--color-warning`
|
|
378
|
-
- `--color-error`
|
|
379
|
-
- `--color-info`
|
|
380
|
-
- spacing tokens such as `--space-1` through `--space-24`
|
|
381
|
-
- shape and motion tokens such as `--radius`, `--shadow-*`, and `--transition-*`
|
|
382
|
-
|
|
383
|
-
C4-specific tokens:
|
|
384
|
-
|
|
385
|
-
- `--c4-person`
|
|
386
|
-
- `--c4-system`
|
|
387
|
-
- `--c4-container`
|
|
388
|
-
- `--c4-component`
|
|
389
|
-
- `--c4-database`
|
|
390
|
-
- `--c4-queue`
|
|
391
|
-
- `--c4-external`
|
|
392
|
-
- `--c4-boundary-border`
|
|
393
|
-
|
|
394
|
-
Conventions:
|
|
395
|
-
|
|
396
|
-
- Do not hardcode colors in new viewer UI code
|
|
397
|
-
- Read from CSS variables with `var(--token)`
|
|
398
|
-
- If you need a new semantic token, add it to `theme.css` instead of scattering literals
|
|
399
|
-
- Treat any remaining literal-color spots in older code as legacy, not as a pattern to copy
|
|
400
|
-
|
|
401
|
-
Note on palette language:
|
|
402
|
-
|
|
403
|
-
- The system is designed around shared CSS custom-property tokens and a Nord-inspired dark palette
|
|
404
|
-
- The current implementation stores concrete color values directly in `theme.css`
|
|
405
|
-
- If you migrate tokens to `oklch()`, keep the same semantic token names so viewer code stays unchanged
|
|
406
|
-
|
|
407
|
-
## Data Format
|
|
408
|
-
|
|
409
|
-
New viewers should prefer `window.__VIEWER_CONFIG__`.
|
|
410
|
-
|
|
411
|
-
Top-level shape:
|
|
412
|
-
|
|
413
|
-
```ts
|
|
414
|
-
interface ViewerConfig {
|
|
415
|
-
type: 'c4' | 'tour' | 'custom';
|
|
416
|
-
theme?: Partial<ThemeConfig>;
|
|
417
|
-
layout?: Partial<LayoutConfig>;
|
|
418
|
-
header?: Partial<HeaderConfig>;
|
|
419
|
-
footer?: Partial<FooterConfig>;
|
|
420
|
-
data: unknown;
|
|
421
|
-
}
|
|
422
|
-
```
|
|
423
|
-
|
|
424
|
-
## Preferred Injection Pattern
|
|
425
|
-
|
|
426
|
-
Use this in `src/<name>/main.tsx`:
|
|
427
|
-
|
|
428
|
-
```tsx
|
|
429
|
-
const config = (window as any).__VIEWER_CONFIG__;
|
|
430
|
-
const dataEl = document.getElementById('<viewer>-data');
|
|
431
|
-
const data = config?.data || (dataEl ? JSON.parse(dataEl.textContent || '{}') : {});
|
|
432
|
-
```
|
|
433
|
-
|
|
434
|
-
Why this order matters:
|
|
435
|
-
|
|
436
|
-
- `window.__VIEWER_CONFIG__` is the current contract for AI Kit-driven rendering
|
|
437
|
-
- inline script tags keep local preview and legacy flows working
|
|
438
|
-
- the fallback keeps the entry HTML usable as a manual demo and as a development fixture
|
|
439
|
-
|
|
440
|
-
## Current Viewers
|
|
441
|
-
|
|
442
|
-
### C4 Viewer
|
|
443
|
-
|
|
444
|
-
Source files:
|
|
445
|
-
|
|
446
|
-
- `src/c4/C4Viewer.tsx`
|
|
447
|
-
- `src/c4/main.tsx`
|
|
448
|
-
- `c4-viewer.html`
|
|
449
|
-
- `vite.c4.config.ts`
|
|
450
|
-
|
|
451
|
-
Behavior:
|
|
452
|
-
|
|
453
|
-
- reads `window.__VIEWER_CONFIG__` when `type === 'c4'`
|
|
454
|
-
- falls back to legacy `window.__C4_DATA__` and then `#diagram-data`
|
|
455
|
-
- normalizes legacy `elements` -> `nodes` and `relationships` -> `edges`
|
|
456
|
-
- uses ELK layered layout for automatic node positioning
|
|
457
|
-
- builds a legend for the footer from the node types in the payload
|
|
458
|
-
|
|
459
|
-
ReactFlow conventions already used here:
|
|
460
|
-
|
|
461
|
-
- `animated: true` on edges
|
|
462
|
-
- `proOptions: { hideAttribution: true }`
|
|
463
|
-
- includes `<Background />`, `<Controls />`, and `<MiniMap />`
|
|
464
|
-
- wraps the canvas in `ReactFlowProvider`
|
|
465
|
-
|
|
466
|
-
Data shape:
|
|
467
|
-
|
|
468
|
-
```ts
|
|
469
|
-
interface C4ViewerConfig {
|
|
470
|
-
title?: string;
|
|
471
|
-
description?: string;
|
|
472
|
-
layout?: {
|
|
473
|
-
direction?: string;
|
|
474
|
-
spacing?: number;
|
|
475
|
-
layerSpacing?: number;
|
|
476
|
-
};
|
|
477
|
-
nodes: Array<{
|
|
478
|
-
id: string;
|
|
479
|
-
type: 'person' | 'system' | 'container' | 'component' | 'database' | 'queue' | 'external' | 'boundary';
|
|
480
|
-
label: string;
|
|
481
|
-
technology?: string;
|
|
482
|
-
description?: string;
|
|
483
|
-
}>;
|
|
484
|
-
edges: Array<{
|
|
485
|
-
source: string;
|
|
486
|
-
target: string;
|
|
487
|
-
label?: string;
|
|
488
|
-
style?: 'sync' | 'async' | 'dashed' | 'dotted' | 'solid';
|
|
489
|
-
}>;
|
|
490
|
-
}
|
|
491
|
-
```
|
|
492
|
-
|
|
493
|
-
Example payload:
|
|
494
|
-
|
|
495
|
-
```html
|
|
496
|
-
<script type="application/json" id="diagram-data">
|
|
497
|
-
{
|
|
498
|
-
"title": "Sample System",
|
|
499
|
-
"description": "Replace this JSON with your C4 diagram data",
|
|
500
|
-
"layout": { "direction": "DOWN", "spacing": 80, "layerSpacing": 120 },
|
|
501
|
-
"nodes": [
|
|
502
|
-
{ "id": "user", "type": "person", "label": "User", "description": "End user" },
|
|
503
|
-
{ "id": "web", "type": "system", "label": "Web App", "technology": "React", "description": "Frontend" }
|
|
504
|
-
],
|
|
505
|
-
"edges": [
|
|
506
|
-
{ "source": "user", "target": "web", "label": "Uses" }
|
|
507
|
-
]
|
|
508
|
-
}
|
|
509
|
-
</script>
|
|
510
|
-
```
|
|
511
|
-
|
|
512
|
-
### Tour Viewer
|
|
513
|
-
|
|
514
|
-
Source files:
|
|
515
|
-
|
|
516
|
-
- `src/tour/TourViewer.tsx`
|
|
517
|
-
- `src/tour/main.tsx`
|
|
518
|
-
- `tour-viewer.html`
|
|
519
|
-
- `vite.tour.config.ts`
|
|
520
|
-
|
|
521
|
-
Behavior:
|
|
522
|
-
|
|
523
|
-
- reads `window.__VIEWER_CONFIG__` first
|
|
524
|
-
- falls back to `#tour-data`
|
|
525
|
-
- computes a BFS layout from `steps` and `dependencies`
|
|
526
|
-
- renders a graph above and a detail panel below
|
|
527
|
-
- supports active-step selection and previous/next navigation
|
|
528
|
-
|
|
529
|
-
ReactFlow conventions already used here:
|
|
530
|
-
|
|
531
|
-
- `animated: true` on edges
|
|
532
|
-
- `proOptions: { hideAttribution: true }`
|
|
533
|
-
- includes `<Background />`, `<Controls />`, and `<MiniMap />`
|
|
534
|
-
- wraps the canvas in `ReactFlowProvider`
|
|
535
|
-
|
|
536
|
-
Current data shape:
|
|
537
|
-
|
|
538
|
-
```ts
|
|
539
|
-
{
|
|
540
|
-
title?: string;
|
|
541
|
-
description?: string;
|
|
542
|
-
estimatedTime?: string;
|
|
543
|
-
steps: Array<{
|
|
544
|
-
stepNumber: number;
|
|
545
|
-
id: string;
|
|
546
|
-
title: string;
|
|
547
|
-
file: string;
|
|
548
|
-
explanation: string;
|
|
549
|
-
learnsConcept: string;
|
|
550
|
-
duration: string;
|
|
551
|
-
}>;
|
|
552
|
-
dependencies: Array<{
|
|
553
|
-
source: string;
|
|
554
|
-
target: string;
|
|
555
|
-
}>;
|
|
556
|
-
}
|
|
557
|
-
```
|
|
558
|
-
|
|
559
|
-
## How to Create a New Viewer
|
|
560
|
-
|
|
561
|
-
Use this process for every new full viewer.
|
|
562
|
-
|
|
563
|
-
### 1. Create the source directory
|
|
564
|
-
|
|
565
|
-
```text
|
|
566
|
-
src/<name>/
|
|
567
|
-
<Name>Viewer.tsx
|
|
568
|
-
main.tsx
|
|
569
|
-
```
|
|
570
|
-
|
|
571
|
-
### 2. Create `<Name>Viewer.tsx`
|
|
572
|
-
|
|
573
|
-
Use the shared shell.
|
|
574
|
-
|
|
575
|
-
```tsx
|
|
576
|
-
import { ReactFlowProvider, ReactFlow, Background, Controls, MiniMap } from '@xyflow/react';
|
|
577
|
-
import '@xyflow/react/dist/style.css';
|
|
578
|
-
|
|
579
|
-
import { ThemeProvider } from '../shared/ThemeProvider';
|
|
580
|
-
import { Layout } from '../shared/Layout';
|
|
581
|
-
import { Header } from '../shared/Header';
|
|
582
|
-
import { Footer } from '../shared/Footer';
|
|
583
|
-
import '../shared/theme.css';
|
|
584
|
-
|
|
585
|
-
interface MyViewerData {
|
|
586
|
-
title?: string;
|
|
587
|
-
description?: string;
|
|
588
|
-
nodes: Array<{ id: string; label: string }>;
|
|
589
|
-
edges: Array<{ source: string; target: string }>;
|
|
590
|
-
}
|
|
591
|
-
|
|
592
|
-
function MyViewerInner({ data }: { data: MyViewerData }) {
|
|
593
|
-
return (
|
|
594
|
-
<Layout variant="full">
|
|
595
|
-
<Header title={data.title || 'My Viewer'} subtitle={data.description} showThemeToggle={true} />
|
|
596
|
-
<div style={{ minHeight: 0, height: '100%', overflow: 'hidden' }}>
|
|
597
|
-
<ReactFlow
|
|
598
|
-
nodes={[]}
|
|
599
|
-
edges={[]}
|
|
600
|
-
proOptions={{ hideAttribution: true }}
|
|
601
|
-
>
|
|
602
|
-
<Background color="var(--muted)" gap={24} size={1.5} />
|
|
603
|
-
<Controls />
|
|
604
|
-
<MiniMap />
|
|
605
|
-
</ReactFlow>
|
|
606
|
-
</div>
|
|
607
|
-
<Footer />
|
|
608
|
-
</Layout>
|
|
609
|
-
);
|
|
610
|
-
}
|
|
611
|
-
|
|
612
|
-
export function MyViewer({ data }: { data: MyViewerData }) {
|
|
613
|
-
return (
|
|
614
|
-
<ThemeProvider>
|
|
615
|
-
<ReactFlowProvider>
|
|
616
|
-
<MyViewerInner data={data} />
|
|
617
|
-
</ReactFlowProvider>
|
|
618
|
-
</ThemeProvider>
|
|
619
|
-
);
|
|
620
|
-
}
|
|
621
|
-
```
|
|
622
|
-
|
|
623
|
-
### 3. Create `main.tsx`
|
|
624
|
-
|
|
625
|
-
Use the standard data loader.
|
|
626
|
-
|
|
627
|
-
```tsx
|
|
628
|
-
import { createRoot } from 'react-dom/client';
|
|
629
|
-
import { MyViewer } from './MyViewer';
|
|
630
|
-
|
|
631
|
-
const config = (window as any).__VIEWER_CONFIG__;
|
|
632
|
-
const dataEl = document.getElementById('my-viewer-data');
|
|
633
|
-
const data = config?.data || (dataEl ? JSON.parse(dataEl.textContent || '{}') : {});
|
|
634
|
-
|
|
635
|
-
createRoot(document.getElementById('root')!).render(<MyViewer data={data} />);
|
|
636
|
-
```
|
|
637
|
-
|
|
638
|
-
### 4. Create the root entry HTML template
|
|
639
|
-
|
|
640
|
-
Name it `<name>-viewer.html` and keep it minimal.
|
|
641
|
-
|
|
642
|
-
```html
|
|
643
|
-
<!DOCTYPE html>
|
|
644
|
-
<html lang="en">
|
|
645
|
-
<head>
|
|
646
|
-
<meta charset="UTF-8" />
|
|
647
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
648
|
-
<title>AI KIT - My Viewer</title>
|
|
649
|
-
</head>
|
|
650
|
-
<body>
|
|
651
|
-
<script type="application/json" id="my-viewer-data">
|
|
652
|
-
{
|
|
653
|
-
"title": "Sample Viewer",
|
|
654
|
-
"description": "Replace this JSON with sample viewer data"
|
|
655
|
-
}
|
|
656
|
-
</script>
|
|
657
|
-
<div id="root"></div>
|
|
658
|
-
<script type="module" src="./src/my-viewer/main.tsx"></script>
|
|
659
|
-
</body>
|
|
660
|
-
</html>
|
|
661
|
-
```
|
|
662
|
-
|
|
663
|
-
### 5. Create `vite.<name>.config.ts`
|
|
664
|
-
|
|
665
|
-
Copy an existing Vite config and change the input file.
|
|
666
|
-
|
|
667
|
-
```ts
|
|
668
|
-
import react from '@vitejs/plugin-react';
|
|
669
|
-
import { defineConfig } from 'vite';
|
|
670
|
-
import { viteSingleFile } from 'vite-plugin-singlefile';
|
|
671
|
-
|
|
672
|
-
export default defineConfig({
|
|
673
|
-
plugins: [react(), viteSingleFile()],
|
|
674
|
-
root: '.',
|
|
675
|
-
build: {
|
|
676
|
-
outDir: 'dist',
|
|
677
|
-
emptyOutDir: false,
|
|
678
|
-
rollupOptions: {
|
|
679
|
-
input: 'my-viewer.html',
|
|
680
|
-
},
|
|
681
|
-
},
|
|
682
|
-
});
|
|
683
|
-
```
|
|
684
|
-
|
|
685
|
-
### 6. Update `package.json`
|
|
686
|
-
|
|
687
|
-
Add a dev script and include the new Vite config in the build flow.
|
|
688
|
-
|
|
689
|
-
Example:
|
|
690
|
-
|
|
691
|
-
```json
|
|
692
|
-
{
|
|
693
|
-
"scripts": {
|
|
694
|
-
"dev:my-viewer": "vite --config vite.my-viewer.config.ts",
|
|
695
|
-
"build": "vite build --config vite.c4.config.ts && vite build --config vite.tour.config.ts && vite build --config vite.my-viewer.config.ts"
|
|
696
|
-
}
|
|
697
|
-
}
|
|
698
|
-
```
|
|
699
|
-
|
|
700
|
-
### 7. Build locally
|
|
701
|
-
|
|
702
|
-
```bash
|
|
703
|
-
pnpm build
|
|
704
|
-
```
|
|
705
|
-
|
|
706
|
-
Confirm that `dist/my-viewer.html` exists and opens correctly.
|
|
707
|
-
|
|
708
|
-
### 8. Embed if the viewer belongs in a skill asset
|
|
709
|
-
|
|
710
|
-
Update `embed.mjs` if the new viewer should be injected into a skill file.
|
|
711
|
-
|
|
712
|
-
Current embed targets:
|
|
713
|
-
|
|
714
|
-
- `dist/c4-viewer.html` -> `../../definitions/skills/c4-architecture.mjs`
|
|
715
|
-
- `dist/tour-viewer.html` -> `../../definitions/skills/docs.mjs`
|
|
716
|
-
|
|
717
|
-
Pattern:
|
|
718
|
-
|
|
719
|
-
```js
|
|
720
|
-
await updateSkillAsset('my-skill.mjs', 'my-viewer.html', myViewerHtml);
|
|
721
|
-
```
|
|
722
|
-
|
|
723
|
-
Then run:
|
|
724
|
-
|
|
725
|
-
```bash
|
|
726
|
-
pnpm embed
|
|
727
|
-
```
|
|
728
|
-
|
|
729
|
-
## Build and Deploy Flow
|
|
730
|
-
|
|
731
|
-
The end-to-end path is:
|
|
732
|
-
|
|
733
|
-
```text
|
|
734
|
-
src/<viewer>/*
|
|
735
|
-
-> root <name>-viewer.html entry template
|
|
736
|
-
-> vite.<name>.config.ts
|
|
737
|
-
-> dist/<name>-viewer.html
|
|
738
|
-
-> embed.mjs
|
|
739
|
-
-> scaffold/definitions/skills/*.mjs asset arrays
|
|
740
|
-
```
|
|
741
|
-
|
|
742
|
-
`embed.mjs` behavior matters:
|
|
743
|
-
|
|
744
|
-
- it reads the built HTML from `dist/`
|
|
745
|
-
- it escapes backslashes, backticks, and `${` for safe template-literal embedding
|
|
746
|
-
- it replaces an existing asset entry if the same file marker is already present
|
|
747
|
-
- otherwise it appends a new asset object before the closing `];`
|
|
748
|
-
|
|
749
|
-
That replacement strategy is designed to recover cleanly from previous partial embeds.
|
|
750
|
-
|
|
751
|
-
## Conventions for LLM Agents
|
|
752
|
-
|
|
753
|
-
Follow these rules when modifying or creating viewers.
|
|
754
|
-
|
|
755
|
-
1. Full viewers must use `ThemeProvider`, `Layout`, `Header`, and `Footer`.
|
|
756
|
-
2. Use shared components before creating new shell primitives.
|
|
757
|
-
3. Do not hardcode colors in new code. Use `var(--token)` from `theme.css`.
|
|
758
|
-
4. Theme state belongs on `<html data-theme="light|dark">` and is resolved by `ThemeProvider`.
|
|
759
|
-
5. Keep `ThemeMode` as `'light' | 'dark' | 'system'` unless a cross-viewer contract change is intentional.
|
|
760
|
-
6. `Header` defaults to `AI KIT`. Pass the viewer-specific title through props.
|
|
761
|
-
7. Root `*-viewer.html` files are entry templates, not build artifacts.
|
|
762
|
-
8. Each viewer gets its own `vite.<name>.config.ts` file.
|
|
763
|
-
9. ReactFlow viewers must include `<Background />`, `<Controls />`, and `<MiniMap />`.
|
|
764
|
-
10. ReactFlow edges should be animated unless the viewer has a documented reason not to animate them.
|
|
765
|
-
11. Set `proOptions: { hideAttribution: true }` on ReactFlow instances.
|
|
766
|
-
12. Keep the package standalone. Use `pnpm install --ignore-workspace` in this folder.
|
|
767
|
-
13. Prefer `window.__VIEWER_CONFIG__` and retain an inline JSON fallback for previewability.
|
|
768
|
-
14. Reuse shared types from `src/shared/types.ts` when possible.
|
|
769
|
-
15. If you add a shared token or component, document it here in the same change.
|
|
770
|
-
16. If you add a viewer intended for a skill, wire it into `embed.mjs` and verify the target skill file.
|
|
771
|
-
|
|
772
|
-
## Common Mistakes
|
|
773
|
-
|
|
774
|
-
- Editing `dist/*.html` directly instead of source files
|
|
775
|
-
- Treating root `*-viewer.html` as final output
|
|
776
|
-
- Running `pnpm install` without `--ignore-workspace`
|
|
777
|
-
- Recreating theme logic inside a viewer instead of using `ThemeProvider`
|
|
778
|
-
- Adding bespoke layout wrappers instead of using `Layout`
|
|
779
|
-
- Copying literal colors from older code instead of using theme tokens
|
|
780
|
-
- Forgetting to add a new Vite config to the build script
|
|
781
|
-
- Forgetting to update `embed.mjs` for a new skill-bound viewer
|
|
782
|
-
|
|
783
|
-
## Minimal Checklist for a New Viewer
|
|
784
|
-
|
|
785
|
-
- Source lives under `src/<name>/`
|
|
786
|
-
- Root entry HTML exists as `<name>-viewer.html`
|
|
787
|
-
- Vite config exists as `vite.<name>.config.ts`
|
|
788
|
-
- Viewer uses `ThemeProvider`, `Layout`, `Header`, and `Footer`
|
|
789
|
-
- ReactFlow config includes `Background`, `Controls`, `MiniMap`, animated edges, and hidden attribution
|
|
790
|
-
- Data loader prefers `window.__VIEWER_CONFIG__`
|
|
791
|
-
- `package.json` scripts are updated
|
|
792
|
-
- `pnpm build` succeeds
|
|
793
|
-
- `pnpm embed` updated and run if needed
|
|
794
|
-
- This README updated if the shared contract changed
|
|
795
|
-
|
|
796
|
-
## Source of Truth
|
|
797
|
-
|
|
798
|
-
When this README and the code disagree, trust the code in this folder. Then update the README in the same change.
|