const e=1e3;var t=class{deps;constructor(e){this.deps=e}async getStatus(){let e=await this.deps.store.getStats();return{totalRecords:e.totalRecords,totalFiles:e.totalFiles,lastIndexedAt:e.lastIndexedAt??null,onboarded:e.totalRecords>0}}async search(e,t){let n=t?.limit??10,i=t?.mode??`hybrid`;if(i===`keyword`)return(await this.deps.store.ftsSearch(e,{limit:n})).map(r);let a=await this.deps.embedder.embedQuery(e);if(i===`semantic`)return(await this.deps.store.search(a,{limit:n})).map(r);let[o,s]=await Promise.all([this.deps.store.search(a,{limit:n*2}),this.deps.store.ftsSearch(e,{limit:n*2}).catch(()=>[])]),c=new Map;for(let e of[...o,...s]){let t=c.get(e.record.id);(!t||e.score>t.score)&&c.set(e.record.id,e)}return[...c.values()].sort((e,t)=>t.score-e.score).slice(0,n).map(r)}async listKnowledge(){return this.deps.listCurated?(await this.deps.listCurated()).map(n):[]}async readKnowledge(e){if(!this.deps.readCurated)return null;let t=await this.deps.readCurated(e);return t?n(t):null}async getGraph(t){if(!this.deps.graphStore)return{nodes:[],edges:[]};let n=await this.deps.graphStore.findNodes(t?{namePattern:t,limit:500}:{limit:500});if(n.length===0)return{nodes:[],edges:[]};let r=t?await this.getEdgesForNodes(n):await this.deps.graphStore.findEdges({limit:e});return{nodes:n.map(e=>({id:e.id,name:e.name,type:e.type,...e.sourcePath?{sourcePath:e.sourcePath}:{}})),edges:i(r).map(e=>({fromId:e.fromId,toId:e.toId,type:e.type}))}}async getFileTree(){return this.deps.store.listSourcePaths()}async getEdgesForNodes(t){let n=this.deps.graphStore;if(!n)return[];let r=t.flatMap(t=>[n.findEdges({fromId:t.id,limit:e}),n.findEdges({toId:t.id,limit:e})]);return(await Promise.all(r)).flat()}};function n(e){return{path:e.path,title:e.title,category:e.category,tags:e.tags,content:e.content}}function r(e){return{sourcePath:e.record.sourcePath,contentType:e.record.contentType,score:e.score,content:e.record.content,headingPath:e.record.headingPath,startLine:e.record.startLine,endLine:e.record.endLine}}function i(e){let t=new Map;for(let n of e)t.set(n.id,n);return[...t.values()]}function a(e){return!Array.isArray(e?.content)||e.content.length===0?``:e.content.filter(e=>typeof e==`object`&&!!e).filter(e=>e.type===`text`&&typeof e.text==`string`).map(e=>e.text).join(`
`)}function o(e){return e?.structuredContent}function s(e){return{text:a(e),structured:o(e)}}function c(e){try{return JSON.parse(e)}catch{return}}var l=class{client;constructor(e){this.client=e}async getStatus(){let e=await this.client.callTool({name:`status`,arguments:{}}),t=d(e);if(t)return{totalRecords:g(t.totalRecords),totalFiles:g(t.totalFiles),lastIndexedAt:v(t.lastIndexedAt),onboarded:!!t.onboarded};let n=u(e);return{totalRecords:f(n,/Records:\s*(\d+)/i),totalFiles:f(n,/Files:\s*(\d+)/i),lastIndexedAt:null,onboarded:!/not onboarded/i.test(n)}}async search(e,t){let n=await this.client.callTool({name:`search`,arguments:{query:e,limit:t?.limit??10,search_mode:t?.mode??`hybrid`}}),r=d(n);return r?.results?r.results.map(e=>({sourcePath:y(e.sourcePath??e.path),contentType:y(e.contentType,`unknown`),score:g(e.score),content:y(e.content??e.snippet),headingPath:b(e.headingPath),startLine:_(e.startLine),endLine:_(e.endLine)})):p(u(n))}async listKnowledge(){let e=d(await this.client.callTool({name:`knowledge`,arguments:{action:`list`}}));return e?.entries?e.entries.map(e=>({path:y(e.path),title:y(e.title),category:y(e.category),tags:x(e.tags),content:y(e.content)})):[]}async readKnowledge(e){try{let t=u(await this.client.callTool({name:`knowledge`,arguments:{action:`read`,path:e}}));return!t||/not found/i.test(t)?null:{path:e,title:e.split(`/`).pop()??e,category:e.split(`/`).at(-2)??`uncategorized`,tags:[],content:t}}catch{return null}}async getGraph(e){let t=m(u(await this.client.callTool({name:`graph`,arguments:{action:`find_nodes`,...e?{name_pattern:e}:{},limit:500}})));return t.length===0?{nodes:[],edges:[]}:{nodes:t,edges:h(u(await this.client.callTool({name:`graph`,arguments:{action:`find_edges`,limit:1e3}})))}}async getFileTree(){let e=await this.client.callTool({name:`find`,arguments:{glob:`**/*`,limit:1e4}}),t=d(e);return t?.results?t.results.map(e=>typeof e==`string`?e:e.path??``).filter(e=>e.length>0):u(e).split(`
`).map(e=>e.trim()).filter(e=>e.length>0)}};function u(e){return a(e)}function d(e){return o(e)}function f(e,t){let n=t.exec(e);return n?Number.parseInt(n[1],10):0}function p(e){let t=[],n=e.split(/\n(?=\d+\.\s|\*\*)/);for(let e of n){let n=/(?:\*\*|`)([^*`]+?)(?:\*\*|`)/.exec(e),r=/score:\s*([\d.]+)/i.exec(e);n&&t.push({sourcePath:n[1].trim(),contentType:`unknown`,score:r?Number.parseFloat(r[1]):0,content:e.replace(/^.*\n/,``).trim().slice(0,200)})}return t}function m(e){let t=[];for(let n of e.split(`
`)){let e=/\*\*(.+?)\*\*\s*\(([^,)]+)(?:,\s*id:\s*`?([\w-]+)`?)?/.exec(n);e&&t.push({id:e[3]??`node-${t.length}`,name:e[1],type:e[2]})}return t}function h(e){let t=[];for(let n of e.split(`
`)){let e=/`?([\w-]+)`?\s*[—-]+\[(\w+)\][→>]\s*`?([\w-]+)`?/.exec(n);e&&t.push({fromId:e[1],toId:e[3],type:e[2]})}return t}function g(e){return typeof e==`number`?e:0}function _(e){return typeof e==`number`?e:void 0}function v(e){return typeof e==`string`?e:null}function y(e,t=``){return typeof e==`string`?e:t}function b(e){return typeof e==`string`?e:void 0}function x(e){return Array.isArray(e)?e.filter(e=>typeof e==`string`):[]}export{t as DirectAikitClient,l as McpAikitClient,o as extractStructured,a as extractText,s as parseToolResult,c as tryParseJson};