@thinkermd/cli 0.1.7 → 0.1.9
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/dist/tk.js +7 -7
- package/package.json +1 -1
package/dist/tk.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import{Command as
|
|
2
|
+
import{Command as Ao}from"commander";import Pe from"chalk";import{join as at}from"path";import{writeFile as Wt}from"fs/promises";import b from"chalk";import Ue from"ora";import{homedir as Lt}from"os";import{join as J}from"path";import{readFile as Ut,writeFile as Bt,mkdir as $e}from"fs/promises";import{existsSync as Kt}from"fs";var y=".thinker.md",A="config.json",et="structure.json",O="CONTEXT.md",Pt="contexts",St="thinker.md.tar.gz",$t="metadata.json",It="latest",ot="repos",rt="branches";var Nt="thinkermd-meta-repos",_t="1.0",gt="full",At="incremental",Ot=["node_modules",".git","dist","build",".next",".nuxt","coverage","__pycache__",".pytest_cache","venv",".venv","vendor","target",y],Ft=3,jt=1e3,Mt="[Describe the purpose",Dt=100;var U=J(Lt(),".thinkermd","config.json"),H="https://app.thinkermd.dev";function ft(){return process.env.META_REPO_ID&&process.env.JOB_TOKEN?"sandbox":"local"}function Ie(){return{mode:"sandbox",metaRepoId:process.env.META_REPO_ID,branch:process.env.BRANCH,commitSha:process.env.COMMIT_SHA,previousCommitSha:process.env.PREVIOUS_COMMIT_SHA||void 0,jobId:process.env.JOB_ID,callbackUrl:process.env.CALLBACK_URL,jobToken:process.env.JOB_TOKEN,r2Endpoint:process.env.R2_ENDPOINT,r2AccessKeyId:process.env.R2_ACCESS_KEY_ID,r2SecretAccessKey:process.env.R2_SECRET_ACCESS_KEY,r2Bucket:process.env.R2_BUCKET}}async function $(){try{if(Kt(U)){let e=await Ut(U,"utf-8");return JSON.parse(e)}}catch{}return{}}async function nt(e){let t=J(Lt(),".thinkermd");await $e(t,{recursive:!0,mode:448}),await Bt(U,JSON.stringify(e,null,2),{mode:384})}async function P(){return ft()==="sandbox"?Ie():{mode:"local",...await $()}}var q={version:_t,ignore:[...Ot]};async function F(e){let t=J(e,y,A);try{if(Kt(t)){let o=await Ut(t,"utf-8");return JSON.parse(o)}}catch{}return null}async function it(e,t){let o=J(e,y,A);await Bt(o,JSON.stringify(t,null,2))}function Gt(){let t=["META_REPO_ID","BRANCH","COMMIT_SHA","JOB_ID","CALLBACK_URL","JOB_TOKEN","R2_ENDPOINT","R2_ACCESS_KEY_ID","R2_SECRET_ACCESS_KEY","R2_BUCKET"].filter(o=>!process.env[o]);if(t.length>0)throw new Error(`Missing required environment variables: ${t.join(", ")}`)}function I(e){return J(e,y)}function Y(e){return J(e,y,Pt)}import{execFile as Ne}from"child_process";import{promisify as _e}from"util";var Ae=_e(Ne);async function X(e,t="."){let{stdout:o}=await Ae("git",e,{cwd:t});return o.trim()}async function S(e="."){try{return await X(["branch","--show-current"],e)}catch(t){throw new Error(`Failed to get current branch: ${t}`)}}async function _(e="."){try{return await X(["rev-parse","HEAD"],e)}catch(t){throw new Error(`Failed to get current commit: ${t}`)}}async function T(e="."){try{return await X(["rev-parse","--show-toplevel"],e)}catch(t){throw new Error(`Not a git repository: ${t}`)}}async function N(e="."){try{return await X(["rev-parse","--git-dir"],e),!0}catch{return!1}}async function Jt(e=".",t){try{return await X(["log","-1","--format=%s",t||"HEAD"],e)}catch(o){throw new Error(`Failed to get commit message: ${o}`)}}async function ht(e="."){try{return await X(["remote","get-url","origin"],e)}catch{return null}}async function z(e="."){let t=await ht(e);if(!t)return null;let o=t.match(/[/:]([\w-]+)\/([\w.-]+?)(\.git)?$/);return o?`${o[1]}/${o[2]}`:null}import{join as B,relative as Oe,dirname as Fe}from"path";import{readFile as je}from"fs/promises";import{existsSync as Ht}from"fs";async function Me(){let e=await import("ignore");return(e.default??e)()}var De=[".git",".thinker.md","node_modules","__pycache__",".pytest_cache","venv",".venv",".next",".nuxt","dist","build","coverage","target",".idea",".vscode","*.log","*.lock","package-lock.json","yarn.lock","bun.lockb","pnpm-lock.yaml","Cargo.lock","poetry.lock","Gemfile.lock","*.pyc","*.pyo","*.class","*.o","*.so","*.dylib","*.dll","*.exe"];async function Le(e){let t=await Me();t.add(De),t.add(q.ignore||[]);try{let o=B(e,".gitignore");if(Ht(o)){let r=await je(o,"utf-8");t.add(r)}}catch{}return t}async function st(e){let t=await Le(e),o=new Set,r={total:0};await Xt(e,e,t,o,r);let n=Array.from(o).sort();return{version:"1.0",generatedAt:new Date().toISOString(),directories:n,directoryCount:n.length,fileCount:r.total}}async function Xt(e,t,o,r,n){let{readdir:i,stat:s}=await import("fs/promises"),a;try{a=await i(t)}catch{return}for(let l of a){let c=B(t,l),d=Oe(e,c);if(!(o.ignores(d)||o.ignores(d+"/")))try{let u=await s(c);u.isDirectory()?(r.add(d),await Xt(e,c,o,r,n)):u.isFile()&&n.total++}catch{}}}async function zt(e){let t=B(e,".thinker.md","contexts"),o=[];return Ht(B(e,".thinker.md","CONTEXT.md"))&&o.push(""),await Vt(t,"",o),o}async function Vt(e,t,o){let{readdir:r,stat:n}=await import("fs/promises"),i=t?B(e,t):e,s;try{s=await r(i)}catch{return}for(let a of s){let l=B(i,a),c=t?B(t,a):a;try{(await n(l)).isDirectory()?await Vt(e,c,o):a==="CONTEXT.md"&&o.push(Fe(c))}catch{}}}async function h(e,t,o){let r=await P();if(r.mode!=="sandbox"||!r.callbackUrl||!r.jobToken)return;let n=r.callbackUrl.replace("/callback","/progress"),i={type:"progress",status:"processing",step:e,progress:t,message:o};try{let s=await fetch(n,{method:"POST",headers:{Authorization:`Bearer ${r.jobToken}`,"Content-Type":"application/json"},body:JSON.stringify(i)});s.ok||console.warn(`[Progress] Update failed: ${s.status} ${s.statusText}`)}catch(s){console.warn("[Progress] Update error:",s)}}async function qt(e,t){let o=await P();if(o.mode!=="sandbox"||!o.callbackUrl||!o.jobToken)return;let r={jobId:o.jobId,status:"completed",artifactUrl:e,stats:t};try{let n=await fetch(o.callbackUrl,{method:"POST",headers:{Authorization:`Bearer ${o.jobToken}`,"Content-Type":"application/json"},body:JSON.stringify(r)});if(!n.ok)throw console.error(`[Callback] Completion failed: ${n.status} ${n.statusText}`),new Error(`Callback failed: ${n.status}`);console.log("[Callback] Job completed successfully")}catch(n){throw console.error("[Callback] Completion error:",n),n}}async function Yt(e,t){let r=`# ${t||"Project Root"}
|
|
3
3
|
|
|
4
4
|
<!--
|
|
5
5
|
This context file should describe:
|
|
@@ -24,8 +24,8 @@ This context file should describe:
|
|
|
24
24
|
## Dependencies
|
|
25
25
|
|
|
26
26
|
[Note any important dependencies or relationships]
|
|
27
|
-
`;await Wt(e,r)}async function Be(e,t,o=!1){let{existsSync:r}=await import("fs"),n=
|
|
28
|
-
This appears to be the first generation. Use 'tk init' to create initial structure.`),process.exit(0)),t.succeed(`Found version: ${a.slice(0,7)}`)}await h("pull",30,`Found version: ${a.slice(0,7)}`),t.start("Downloading thinker.md.tar.gz...");let
|
|
27
|
+
`;await Wt(e,r)}async function Be(e,t,o=!1){let{existsSync:r}=await import("fs"),n=Y(e),i=0,s=0,a=at(I(e),"CONTEXT.md");o&&r(a)?s++:(await Yt(a,""),i++);for(let l of t){let c=at(n,l),d=at(c,"CONTEXT.md"),{mkdir:u}=await import("fs/promises");if(await u(c,{recursive:!0}),o&&r(d)){s++;continue}await Yt(d,l),i++}return{created:i,skipped:s}}async function Qt(e){let t=Ue(),o=process.cwd();try{await N(o)||(console.error(b.red("Error: Not a git repository")),console.log("Please run this command from the root of a git repository."),process.exit(1));let r=await T(o),n=I(r),{stat:i}=await import("fs/promises");try{await i(n),!e.force&&!e.merge&&(console.error(b.yellow("Warning: .thinker.md/ already exists")),console.log("Use --force to reinitialize or --merge to add missing contexts"),process.exit(1)),e.merge?console.log(b.cyan("Merging: adding missing CONTEXT.md files...")):console.log(b.yellow("Reinitializing .thinker.md/..."))}catch{}await h("init",0,"Scanning repository structure..."),t.start("Scanning repository structure...");let s=await st(r);t.succeed(`Found ${s.directoryCount} directories, ${s.fileCount} files`),await h("init",30,`Found ${s.directoryCount} directories`);let a=await S(r),l=await _(r),c=await z(r)||"unknown";t.start("Creating .thinker.md/ structure...");let{mkdir:d}=await import("fs/promises");await d(n,{recursive:!0}),await d(Y(r),{recursive:!0});let u=await P(),g={...q,version:"1.0",projectId:u.metaRepoId||`local-${Date.now()}`,name:c,ignore:q.ignore||[],generatedAt:new Date().toISOString(),generatedBy:"tk-cli"};await it(r,g),t.succeed("Created config.json"),await h("init",50,"Created config.json");let f={...s,commitSha:l,branch:a};await Wt(at(n,"structure.json"),JSON.stringify(f,null,2)),t.succeed("Created structure.json"),await h("init",60,"Created structure.json"),t.start(e.merge?"Adding missing CONTEXT.md files...":"Creating empty CONTEXT.md files...");let{created:p,skipped:C}=await Be(r,s.directories,e.merge);e.merge?(t.succeed(`Added ${p} new CONTEXT.md files (${C} existing skipped)`),await h("init",100,`Added ${p} new CONTEXT.md files`)):(t.succeed(`Created ${p} empty CONTEXT.md files`),await h("init",100,`Created ${p} CONTEXT.md files`)),console.log(),e.merge?(console.log(b.green("\u2713 Merged .thinker.md/")),console.log(),console.log(`Added: ${b.cyan(`${p}`)} new CONTEXT.md files`),console.log(`Skipped: ${b.gray(`${C}`)} existing files`)):(console.log(b.green("\u2713 Initialized .thinker.md/")),console.log(),console.log("Created:"),console.log(` ${b.cyan("config.json")} - Project configuration`),console.log(` ${b.cyan("structure.json")} - Directory structure`),console.log(` ${b.cyan(`${p} CONTEXT.md`)} files - Ready for documentation`)),console.log(),console.log("Next steps:"),console.log(" 1. Fill in the CONTEXT.md files with documentation"),console.log(" 2. Run "+b.cyan("tk validate")+" to check structure"),console.log(" 3. Run "+b.cyan("tk push")+" to upload to storage")}catch(r){t.fail("Initialization failed"),console.error(b.red(`Error: ${r}`)),process.exit(1)}}import{join as yt}from"path";import L from"chalk";import Ke from"ora";async function wt(e){let t=[],o=[],r=0,n=I(e),i=Y(e),{stat:s,readFile:a}=await import("fs/promises");try{await s(n)}catch{return t.push({type:"structure_mismatch",path:y,message:`${y}/ directory not found. Run 'tk init' first.`}),{valid:!1,errors:t,warnings:o,validContextCount:0,directoryCount:0}}let l=await F(e);l?(l.version||t.push({type:"invalid_config",path:`${y}/${A}`,message:`${A} missing required field: version`}),l.projectId||t.push({type:"invalid_config",path:`${y}/${A}`,message:`${A} missing required field: projectId`})):t.push({type:"invalid_config",path:`${y}/${A}`,message:`${A} not found or invalid`});let c=null;try{let p=yt(n,et),C=await a(p,"utf-8");c=JSON.parse(C)}catch{t.push({type:"structure_mismatch",path:`${y}/${et}`,message:`${et} not found or invalid`})}let d=await st(e),u=yt(n,O);try{await s(u),r++}catch{t.push({type:"missing_context",path:O,message:`Root ${O} not found`})}for(let p of d.directories){let C=yt(i,p,O);try{if((await s(C)).isFile()){r++;let x=await a(C,"utf-8");(x.trim().length<Dt||x.includes(Mt))&&o.push({type:"empty_context",path:`${p}/${O}`,message:"Context file appears to be empty or contains only template"})}}catch{t.push({type:"missing_context",path:`${p}/${O}`,message:`Missing context file for directory: ${p}`})}}let g=await zt(e),f=new Set(d.directories);for(let p of g)p===""||p==="."||f.has(p)||o.push({type:"orphan_context",path:`${p}/${O}`,message:`Context exists for non-existent directory: ${p}`});return{valid:t.length===0,errors:t,warnings:o,validContextCount:r,directoryCount:d.directoryCount}}async function Zt(e){let t=Ke(),o=process.cwd();try{await N(o)||(console.error(L.red("Error: Not a git repository")),process.exit(1));let r=await T(o);await h("validate",0,"Validating structure..."),t.start(`Validating ${y}/ structure...`);let n=await wt(r);if(t.stop(),await h("validate",100,"Validation complete"),!e.quiet){if(console.log(),n.errors.length>0){console.log(L.red(`\u2717 ${n.errors.length} error(s):`));for(let i of n.errors)console.log(L.red(` \u2022 ${i.path}: ${i.message}`));console.log()}if(n.warnings.length>0){console.log(L.yellow(`\u26A0 ${n.warnings.length} warning(s):`));for(let i of n.warnings)console.log(L.yellow(` \u2022 ${i.path}: ${i.message}`));console.log()}n.valid?console.log(L.green(`\u2713 Validation passed: ${n.validContextCount} contexts valid`)):console.log(L.red(`\u2717 Validation failed: ${n.errors.length} error(s), ${n.warnings.length} warning(s)`))}n.valid||process.exit(1)}catch(r){t.fail("Validation failed"),console.error(L.red(`Error: ${r}`)),process.exit(1)}}import{join as ie}from"path";import{writeFile as Ve}from"fs/promises";import j from"chalk";import qe from"ora";import*as se from"tar";import{S3Client as Ge,PutObjectCommand as Ct,GetObjectCommand as te}from"@aws-sdk/client-s3";var xt=null;function W(){if(!xt){let e=process.env.R2_ENDPOINT,t=process.env.R2_ACCESS_KEY_ID,o=process.env.R2_SECRET_ACCESS_KEY;if(!e||!t||!o)throw new Error("R2 credentials not configured. Required: R2_ENDPOINT, R2_ACCESS_KEY_ID, R2_SECRET_ACCESS_KEY");xt=new Ge({region:"auto",endpoint:e,credentials:{accessKeyId:t,secretAccessKey:o}})}return xt}function Q(){return process.env.R2_BUCKET||Nt}async function Z(e,t,o=Ft,r=jt){let n;for(let i=1;i<=o;i++)try{return await e()}catch(s){if(n=s instanceof Error?s:new Error(String(s)),n.name==="NoSuchKey"||n.message.includes("credentials")||n.message.includes("AccessDenied"))throw n;if(i<o){let a=r*Math.pow(2,i-1);console.log(`[Storage] ${t} failed (attempt ${i}/${o}), retrying in ${a}ms...`),await new Promise(l=>setTimeout(l,a))}}throw n}function ee(e,t,o){return`${ot}/${e}/${rt}/${t}/${o}/${St}`}function Je(e,t,o){return`${ot}/${e}/${rt}/${t}/${o}/${$t}`}function oe(e,t){return`${ot}/${e}/${rt}/${t}/${It}`}async function He(e,t,o,r){let n=W(),i=ee(e,t,o);return await Z(()=>n.send(new Ct({Bucket:Q(),Key:i,Body:r,ContentType:"application/gzip"})),"uploadArchive"),console.log(`[Storage] Uploaded archive to ${i}`),i}async function Xe(e,t,o,r){let n=W(),i=Je(e,t,o);await Z(()=>n.send(new Ct({Bucket:Q(),Key:i,Body:JSON.stringify(r,null,2),ContentType:"application/json"})),"uploadMetadata"),console.log(`[Storage] Uploaded metadata to ${i}`)}async function ze(e,t,o){let r=W(),n=oe(e,t);await Z(()=>r.send(new Ct({Bucket:Q(),Key:n,Body:o,ContentType:"text/plain"})),"updateLatestPointer"),console.log(`[Storage] Updated latest pointer for ${t} -> ${o}`)}async function ct(e,t){let o=W(),r=oe(e,t);try{return(await(await Z(()=>o.send(new te({Bucket:Q(),Key:r})),"getLatestCommitSha")).Body?.transformToString())?.trim()||null}catch(n){if(n instanceof Error&&"name"in n&&n.name==="NoSuchKey")return null;throw n}}async function re(e,t,o){let r=W(),n=ee(e,t,o);try{let s=await(await Z(()=>r.send(new te({Bucket:Q(),Key:n})),"downloadArchive")).Body?.transformToByteArray();return s?Buffer.from(s):null}catch(i){if(i instanceof Error&&"name"in i&&i.name==="NoSuchKey")return null;throw i}}async function ne(e,t,o,r,n){let i=await He(e,t,o,r);return await Xe(e,t,o,n),await ze(e,t,o),i}async function Ye(e){let{readdir:t,stat:o}=await import("fs/promises"),r=0;async function n(i){let s;try{s=await t(i)}catch{return}for(let a of s){let l=ie(i,a);try{(await o(l)).isDirectory()?await n(l):a==="CONTEXT.md"&&r++}catch{}}}return await n(e),r}async function ae(e){let t=qe(),o=process.cwd();try{await N(o)||(console.error(j.red("Error: Not a git repository")),process.exit(1));let r=await T(o),n=await P(),i;if(n.mode==="sandbox"&&n.metaRepoId)i=n.metaRepoId;else{let p=await F(r);p?.projectId?i=p.projectId:(console.error(j.red("Error: No project ID found. Run 'tk init' first or set META_REPO_ID.")),process.exit(1))}let s=e.branch||n.branch||await S(r);await h("pull",0,`Fetching latest context for branch '${s}'...`),console.log(`Fetching latest context for branch '${j.cyan(s)}'...`),t.start("Looking up latest version...");let a;if(e.commit)a=e.commit,t.succeed(`Using specified commit: ${a.slice(0,7)}`);else{try{a=await ct(i,s)}catch(p){throw p instanceof Error&&p.message.includes("R2 credentials")&&(t.fail("R2 credentials not configured"),console.error(j.red(p.message)),process.exit(1)),p}a||(t.info("No previous context found for this branch"),console.log(`
|
|
28
|
+
This appears to be the first generation. Use 'tk init' to create initial structure.`),process.exit(0)),t.succeed(`Found version: ${a.slice(0,7)}`)}await h("pull",30,`Found version: ${a.slice(0,7)}`),t.start("Downloading thinker.md.tar.gz...");let l=await re(i,s,a);l||(t.fail("Archive not found"),console.error(j.red(`No archive found for commit ${a.slice(0,7)}`)),process.exit(1)),t.succeed(`Downloaded archive: ${l.length} bytes`),await h("pull",60,"Extracting archive...");let c=ie(r,".thinker.md.tar.gz");await Ve(c,l),t.start("Extracting to .thinker.md/...");let d=I(r),{rm:u}=await import("fs/promises");try{await u(d,{recursive:!0,force:!0})}catch{}await se.extract({file:c,cwd:r}),await u(c,{force:!0}),t.succeed("Extracted .thinker.md/"),await h("pull",80,"Updating config...");let g=await F(r);g&&(g.pulledFrom=a,g.pulledAt=new Date().toISOString(),await it(r,g));let f=await Ye(d);await h("pull",100,`Pulled ${f} context files`),console.log(),console.log(j.green(`\u2713 Pulled ${f} context files`)),console.log(),console.log(`From: ${j.cyan(`${s}@${a.slice(0,7)}`)}`),console.log(),console.log("Next steps:"),console.log(" 1. Review and update context files as needed"),console.log(" 2. Run "+j.cyan("tk validate")+" to check structure"),console.log(" 3. Run "+j.cyan("tk push")+" to upload changes")}catch(r){t.fail("Pull failed"),console.error(j.red(`Error: ${r}`)),process.exit(1)}}import{join as ce}from"path";import R from"chalk";import We from"ora";import*as le from"tar";async function Qe(e){let{readdir:t,stat:o}=await import("fs/promises"),r=0,n=0;async function i(s){let a;try{a=await t(s)}catch{return}for(let l of a){let c=ce(s,l);try{(await o(c)).isDirectory()?await i(c):(n++,l===O&&r++)}catch{}}}return await i(e),{contextFiles:r,totalFiles:n}}async function de(e){let t=We(),o=process.cwd();try{if(ft()==="sandbox")try{Gt()}catch(E){console.error(R.red(`Sandbox environment error: ${E}`)),process.exit(1)}await N(o)||(console.error(R.red("Error: Not a git repository")),process.exit(1));let r=await T(o),n=await P(),i=I(r),{stat:s,rm:a}=await import("fs/promises");try{await s(i)}catch{console.error(R.red(`Error: ${y}/ not found`)),console.log("Run 'tk init' first to create the structure."),process.exit(1)}if(await h("push",0,"Validating structure..."),!e.skipValidation){t.start(`Validating ${y}/ structure...`);let E=await wt(r);if(!E.valid){t.fail("Validation failed"),console.log(),console.log(R.red("Fix the following errors before pushing:"));for(let tt of E.errors)console.log(R.red(` \u2022 ${tt.path}: ${tt.message}`));process.exit(1)}t.succeed("Validation passed"),E.warnings.length>0&&console.log(R.yellow(` ${E.warnings.length} warning(s)`))}await h("push",20,"Validation passed");let l;if(n.mode==="sandbox"&&n.metaRepoId)l=n.metaRepoId;else{let E=await F(r);E?.projectId?l=E.projectId:(console.error(R.red("Error: No project ID found. Set META_REPO_ID environment variable.")),process.exit(1))}let c=n.branch||await S(r),d=n.commitSha||await _(r),u=await Jt(r,d);await h("push",30,"Packaging .thinker.md/..."),t.start(`Packaging ${y}/...`);let g=ce(r,`${y}.tar.gz`);await le.create({file:g,cwd:r,gzip:!0},[y]);let{readFile:f}=await import("fs/promises"),p=await f(g);await a(g,{force:!0}),t.succeed(`Packaged archive: ${p.length} bytes`),await h("push",60,"Uploading to storage...");let C=await Qe(i),G=await F(r),x=e.forceFull?gt:G?.pulledFrom?At:gt,vt={branch:c,commitSha:d,commitMessage:u,mode:x,parentCommitSha:G?.pulledFrom,generatedAt:new Date().toISOString(),jobId:n.jobId||`local-${Date.now()}`,tokensUsed:0,filesAnalyzed:C.totalFiles,contextFilesGenerated:C.contextFiles};t.start("Uploading to storage...");try{let E=await ne(l,c,d,p,vt);if(t.succeed("Uploaded to storage"),await h("push",90,"Finalizing..."),n.mode==="sandbox"&&n.callbackUrl){let tt={filesAnalyzed:C.totalFiles,contextFilesGenerated:C.contextFiles,tokensUsed:0,mode:x};await qt(E,tt)}await h("push",100,"Done!"),console.log(),console.log(R.green("\u2713 Successfully pushed to storage")),console.log(),console.log(`Branch: ${R.cyan(c)}`),console.log(`Commit: ${R.cyan(d.slice(0,7))}`),console.log(`Mode: ${R.cyan(x)}`),console.log(`Context files: ${R.cyan(C.contextFiles)}`),n.mode==="sandbox"&&(console.log(),console.log(R.green("\u2713 Job completed")))}catch(E){t.fail("Upload failed"),E instanceof Error&&E.message.includes("R2 credentials")?(console.error(R.red(E.message)),console.log(),console.log("Required environment variables:"),console.log(" R2_ENDPOINT"),console.log(" R2_ACCESS_KEY_ID"),console.log(" R2_SECRET_ACCESS_KEY"),console.log(" R2_BUCKET")):console.error(R.red(`Error: ${E}`)),process.exit(1)}}catch(r){t.fail("Push failed"),console.error(R.red(`Error: ${r}`)),process.exit(1)}}import k from"chalk";import pe from"ora";import{createServer as ue}from"http";import{randomBytes as Ze}from"crypto";import{exec as to}from"child_process";import{promisify as eo}from"util";var Et=eo(to);async function oo(e){let t=process.platform;try{t==="darwin"?await Et(`open "${e}"`):t==="win32"?await Et(`start "" "${e}"`):await Et(`xdg-open "${e}"`)}catch{}}async function me(e=9876){return new Promise(t=>{let o=ue();o.listen(e,()=>{let r=o.address(),n=typeof r=="object"&&r?r.port:e;o.close(()=>t(n))}),o.on("error",()=>{t(me(e+1))})})}function ro(e,t,o=3e5){return new Promise((r,n)=>{let i=null,s,a=()=>{s&&clearTimeout(s),i&&(i.close(),i=null)};s=setTimeout(()=>{a(),n(new Error("Authentication timed out. Please try again."))},o),i=ue((l,c)=>{let d=new URL(l.url||"",`http://localhost:${e}`);if(d.pathname==="/callback"){let u=d.searchParams.get("token"),g=d.searchParams.get("user_id"),f=d.searchParams.get("email");u&&g&&f?(c.writeHead(200,{"Content-Type":"text/html; charset=utf-8"}),c.end(`<!DOCTYPE html>
|
|
29
29
|
<html lang="en">
|
|
30
30
|
<head>
|
|
31
31
|
<meta charset="UTF-8">
|
|
@@ -100,7 +100,7 @@ This appears to be the first generation. Use 'tk init' to create initial structu
|
|
|
100
100
|
<p>You can now close this window and return to your terminal.</p>
|
|
101
101
|
</div>
|
|
102
102
|
</body>
|
|
103
|
-
</html>`),a(),r({token:m,userId:g,email:f})):(l.writeHead(400,{"Content-Type":"text/plain"}),l.end("Missing required parameters"))}else l.writeHead(404,{"Content-Type":"text/plain"}),l.end("Not found")}),i.listen(e)})}async function no(e,t){try{let o=await fetch(`${e}/api/auth/validate`,{method:"GET",headers:{Authorization:`Bearer ${t}`}});return o.ok?{valid:!0,userId:(await o.json()).userId}:o.status===401?{valid:!1,error:"Invalid or expired token"}:{valid:!1,error:`API error: ${o.status} ${o.statusText}`}}catch(o){return{valid:!1,error:`Connection failed: ${o}`}}}async function ge(e){let t=pe(),o=e.apiUrl||J;if(e.token){t.start("Validating token...");let n=await no(o,e.token);n.valid||(t.fail("Authentication failed"),console.error(k.red(`Error: ${n.error}`)),process.exit(1));let i={apiToken:e.token,apiUrl:o,userId:n.userId};await rt(i),t.succeed("Authenticated successfully"),console.log(),console.log(`Config saved to: ${k.cyan(L)}`);return}let r=await $();if(r.apiToken){console.log(k.green("You are already logged in.")),console.log(`User ID: ${k.cyan(r.userId||"unknown")}`),console.log(),console.log("To re-authenticate, run:"),console.log(k.cyan(" tk login --token NEW_TOKEN")),console.log(),console.log("Or to login with a different account:"),console.log(k.cyan(" tk logout && tk login"));return}console.log(k.bold("ThinkerMD Login")),console.log(),t.start("Setting up authentication...");try{let n=await ue(),i=Ze(16).toString("hex"),s=`${o}/cli/auth?port=${n}&state=${i}`;t.succeed("Ready for authentication"),console.log(),console.log("Opening browser for authentication..."),console.log(),console.log("If the browser doesn't open, visit this URL manually:"),console.log(k.cyan(s)),console.log(),await oo(s),t.start("Waiting for authentication...");let{token:a,userId:c,email:l}=await ro(n,i);await rt({apiToken:a,apiUrl:o,userId:c,email:l}),t.succeed("Authenticated successfully"),console.log(),console.log(`Logged in as: ${k.cyan(l)}`),console.log(`Config saved to: ${k.cyan(L)}`)}catch(n){t.fail("Authentication failed"),console.error(k.red(`Error: ${n instanceof Error?n.message:n}`)),console.log(),console.log("You can also authenticate manually:"),console.log(k.cyan(" tk login --token YOUR_TOKEN")),console.log(),console.log(`Get your token at: ${k.cyan(`${o}/dashboard/settings`)}`),process.exit(1)}}async function fe(){let e=pe();e.start("Logging out..."),await rt({}),e.succeed("Logged out successfully"),console.log(),console.log(`Config cleared from: ${k.cyan(L)}`)}import{join as he}from"path";import u from"chalk";import io from"ora";async function so(e){let{readFile:t}=await import("fs/promises"),{existsSync:o}=await import("fs"),r=he(I(e),"structure.json");try{if(o(r)){let n=await t(r,"utf-8");return JSON.parse(n)}}catch{}return null}async function ao(e){let{readdir:t,stat:o}=await import("fs/promises"),r=0;async function n(i){let s;try{s=await t(i)}catch{return}for(let a of s){let c=he(i,a);try{(await o(c)).isDirectory()?await n(c):a==="CONTEXT.md"&&r++}catch{}}}return await n(e),r}async function ye(){let e=io(),t=process.cwd();try{await _(t)||(console.error(u.red("Error: Not a git repository")),process.exit(1));let o=await R(t),r=await P(),n=I(o);console.log(u.bold("ThinkerMD Status")),console.log();let i=await X(o),s=r.branch||await S(o),a=r.commitSha||await N(o);console.log(u.dim("Repository:")),console.log(` Name: ${u.cyan(i||"unknown")}`),console.log(` Branch: ${u.cyan(s)}`),console.log(` Commit: ${u.cyan(a.slice(0,7))}`),console.log();let{stat:c}=await import("fs/promises"),l=!1;try{await c(n),l=!0}catch{}if(!l){console.log(u.dim("Local Context:")),console.log(` Status: ${u.yellow("Not initialized")}`),console.log(),console.log("Run "+u.cyan("tk init")+" to create .thinker.md/");return}console.log(u.dim("Local Context:"));let d=await F(o),m=await so(o),g=await ao(n);if(console.log(` Status: ${u.green("Initialized")}`),console.log(` Project ID: ${u.cyan(d?.projectId||"unknown")}`),console.log(` Context files: ${u.cyan(g)}`),m&&(console.log(` Directories: ${u.cyan(m.directoryCount)}`),console.log(` Files tracked: ${u.cyan(m.fileCount)}`)),d?.generatedAt){let p=new Date(d.generatedAt);console.log(` Generated: ${u.cyan(p.toLocaleString())}`)}d?.pulledFrom&&console.log(` Pulled from: ${u.cyan(d.pulledFrom.slice(0,7))}`),console.log();let f=r.metaRepoId||d?.projectId;if(f&&r.r2Endpoint){console.log(u.dim("Remote Storage:")),e.start("Checking remote...");try{let p=await at(f,s);p?(e.stop(),console.log(` Latest commit: ${u.cyan(p.slice(0,7))}`),p===a?console.log(` Status: ${u.green("Up to date")}`):d?.pulledFrom===p?console.log(` Status: ${u.yellow("Local changes not pushed")}`):(console.log(` Status: ${u.yellow("Remote has newer version")}`),console.log(),console.log("Run "+u.cyan("tk pull")+" to download latest context"))):(e.stop(),console.log(` Status: ${u.yellow("No remote context found")}`),console.log(),console.log("Run "+u.cyan("tk push")+" to upload context"))}catch{e.stop(),console.log(` Status: ${u.dim("Unable to check (no credentials)")}`)}}console.log(),console.log(u.dim("Mode:")),console.log(` ${r.mode==="sandbox"?u.cyan("Sandbox"):u.cyan("Local")}`),r.mode==="sandbox"&&console.log(` Job ID: ${u.cyan(r.jobId||"unknown")}`)}catch(o){e.fail("Failed to get status"),console.error(u.red(`Error: ${o}`)),process.exit(1)}}import w from"chalk";import co from"ora";async function we(e){let t=co(),o=process.cwd();try{await _(o)||(console.error(w.red("Error: Not a git repository")),process.exit(1));let r=await R(o);if((await P()).mode==="sandbox"){console.error(w.yellow("Note: In sandbox mode, generation is triggered automatically.")),console.log("Use 'tk init' to create structure and 'tk push' when done.");return}let i=await $();i.apiToken||(console.error(w.red("Error: Not logged in")),console.log(),console.log("Please authenticate first:"),console.log(w.cyan(" tk login --token YOUR_TOKEN")),process.exit(1));let s=i.apiUrl||J,a=await X(r),c=e.branch||await S(r),l=await N(r);a||(console.error(w.red("Error: Unable to determine repository name")),console.log("Make sure this repository has a remote origin configured."),process.exit(1)),console.log(w.bold("Trigger Context Generation")),console.log(),console.log(`Repository: ${w.cyan(a)}`),console.log(`Branch: ${w.cyan(c)}`),console.log(`Commit: ${w.cyan(l.slice(0,7))}`),console.log(),t.start("Looking up meta-repository...");let d=await fetch(`${s}/api/meta-repositories/lookup?repo=${encodeURIComponent(a)}`,{headers:{Authorization:`Bearer ${i.apiToken}`}});d.ok||(d.status===404&&(t.fail("Meta-repository not found"),console.log(),console.log("This repository is not connected to ThinkerMD. Please add it at:"),console.log(w.cyan(`${s}/dashboard`)),process.exit(1)),t.fail("API error"),console.error(w.red(`Error: ${d.status} ${d.statusText}`)),process.exit(1));let m=await d.json();t.succeed(`Found meta-repository: ${m.id}`),t.start("Triggering generation job...");let g=await fetch(`${s}/api/jobs/generate`,{method:"POST",headers:{Authorization:`Bearer ${i.apiToken}`,"Content-Type":"application/json"},body:JSON.stringify({metaRepositoryId:m.id,commitSha:l,branch:c,priority:"normal"})});if(!g.ok){t.fail("Failed to trigger generation");let p=await g.json().catch(()=>({}));console.error(w.red(`Error: ${p.error||g.statusText}`)),process.exit(1)}let f=await g.json();if(t.succeed("Generation job queued"),console.log(),console.log(`Job ID: ${w.cyan(f.jobId)}`),console.log(`Status: ${w.cyan(f.status)}`),f.estimatedPosition&&console.log(`Queue position: ${w.cyan(f.estimatedPosition)}`),console.log(),console.log("Track progress at:"),console.log(w.cyan(`${s}/dashboard/repositories/${m.id}/jobs/${f.jobId}`)),e.wait){console.log(),t.start("Waiting for completion...");let p=!1,C="";for(;!p;){await new Promise(x=>setTimeout(x,5e3));let K=await fetch(`${s}/api/jobs/${f.jobId}`,{headers:{Authorization:`Bearer ${i.apiToken}`}});if(K.ok){let x=await K.json();x.status!==C&&(C=x.status,t.text=`Status: ${x.status} (${x.progress||0}%)`),(x.status==="completed"||x.status==="failed")&&(p=!0,x.status==="completed"?(t.succeed("Generation completed!"),console.log(),console.log("Run "+w.cyan("tk pull")+" to download the generated context")):(t.fail("Generation failed"),x.errorMessage&&console.error(w.red(`Error: ${x.errorMessage}`))))}}}}catch(r){t.fail("Failed to trigger generation"),console.error(w.red(`Error: ${r}`)),process.exit(1)}}import bt from"chalk";import{Hono as Eo}from"hono";import M from"chalk";import{createServer as To}from"http";import{homedir as Te}from"os";import{join as Re}from"path";import{readFile as mo,writeFile as uo,mkdir as go}from"fs/promises";import{existsSync as fo}from"fs";async function lo(){return(await $()).apiUrl||process.env.THINKERMD_API_URL||J}async function po(){return(await $()).apiToken||process.env.THINKERMD_API_TOKEN||null}async function ct(e,t={}){let o=await lo(),r=await po();if(!r)throw new Error("Not authenticated. Run `tk login` first.");let{method:n="GET",body:i,params:s}=t,a=`${o}${e}`;if(s){let l=new URLSearchParams(s);a+=`?${l.toString()}`}let c=await fetch(a,{method:n,headers:{Authorization:`Bearer ${r}`,"Content-Type":"application/json"},body:i?JSON.stringify(i):void 0});if(!c.ok){let l=await c.json().catch(()=>({}));throw new Error(l.error||`API request failed: ${c.status} ${c.statusText}`)}return c.json()}async function xe(e,t,o={}){return ct("/api/mcp/search",{method:"POST",body:{repositoryId:e,query:t,branch:o.branch,limit:o.limit||10}})}async function Et(e,t,o={}){return ct("/api/mcp/read",{params:{repositoryId:e,filePath:t,...o.branch&&{branch:o.branch}}})}async function Ce(e,t={}){return ct("/api/mcp/files",{params:{repositoryId:e,...t.branch&&{branch:t.branch}}})}async function Ee(e){try{let t=await ct("/api/user/repositories",{params:{remoteUrl:e}});return t.repositories&&t.repositories.length>0?t.repositories[0]:null}catch{return null}}var Tt=Re(Te(),".thinkermd","cache","repo-mapping.json"),ho=24*60*60*1e3;async function yo(){try{if(fo(Tt)){let e=await mo(Tt,"utf-8");return JSON.parse(e)}}catch{}return{mappings:{}}}async function wo(e){let t=Re(Te(),".thinkermd","cache");await go(t,{recursive:!0}),await uo(Tt,JSON.stringify(e,null,2))}async function V(e){let t;try{t=await R(e)}catch{return console.error(`[RepoResolver] Not a git repository: ${e}`),null}let o=await ft(t);if(!o)return console.error(`[RepoResolver] No git remote found for: ${t}`),null;let r=xo(o),n=await yo(),i=n.mappings[r];if(i&&Date.now()-i.cachedAt<ho){console.log(`[RepoResolver] Cache hit for: ${r}`);let l=await S(t),d=await N(t);return{repositoryId:i.repositoryId,fullName:i.fullName,remoteUrl:r,gitRoot:t,branch:l||"main",commit:d||"HEAD"}}console.log(`[RepoResolver] Querying API for: ${r}`);let s=await Ee(r);if(!s)return console.error(`[RepoResolver] Repository not found in ThinkerMD: ${r}`),null;n.mappings[r]={remoteUrl:r,repositoryId:s.id,fullName:s.fullName,cachedAt:Date.now()},await wo(n);let a=await S(t),c=await N(t);return{repositoryId:s.id,fullName:s.fullName,remoteUrl:r,gitRoot:t,branch:a||"main",commit:c||"HEAD"}}function xo(e){return e=e.replace(/\.git$/,""),e.startsWith("git@")&&(e=e.replace(/^git@/,"https://").replace(/:([^/])/,"/$1")),e.startsWith("http://")&&(e=e.replace("http://","https://")),e}async function lt(e){let{path:t,query:o,limit:r=10}=e,n=await V(t);if(!n)throw new Error(`Could not resolve repository for path: ${t}`);return{...await xe(n.repositoryId,o,{branch:n.branch,limit:r}),repositoryId:n.repositoryId,fullName:n.fullName}}async function dt(e){let{path:t,filePath:o}=e,r=await V(t);if(!r)throw new Error(`Could not resolve repository for path: ${t}`);return{...await Et(r.repositoryId,o,{branch:r.branch}),repositoryId:r.repositoryId,fullName:r.fullName}}async function pt(e){let{path:t}=e,o=await V(t);if(!o)throw new Error(`Could not resolve repository for path: ${t}`);let r=t;t.startsWith(o.gitRoot)&&(r=t.slice(o.gitRoot.length).replace(/^\//,""));let n=r?`.thinker.md/contexts/${r}/CONTEXT.md`:".thinker.md/contexts/CONTEXT.md";return{...await Et(o.repositoryId,n,{branch:o.branch}),repositoryId:o.repositoryId,fullName:o.fullName}}async function mt(e){let{path:t}=e,o=await V(t);if(!o)throw new Error(`Could not resolve repository for path: ${t}`);return{...await Ce(o.repositoryId,{branch:o.branch}),repositoryId:o.repositoryId,fullName:o.fullName}}async function be(e){return V(e.path)}import{McpServer as Co}from"@modelcontextprotocol/sdk/server/mcp.js";import{z as B}from"zod";function ke(){let e=new Co({name:"thinkermd",version:"0.1.0"});return e.tool("thinkermd_search","Semantic search across context files in the repository. Use this to find relevant documentation and context about the codebase.",{query:B.string().describe("Search query - what you're looking for"),path:B.string().optional().describe("Path to directory (defaults to current working directory)"),limit:B.number().optional().default(10).describe("Maximum number of results to return")},async({query:t,path:o,limit:r})=>{try{let n=o||process.cwd(),i=await lt({path:n,query:t,limit:r});return{content:[{type:"text",text:JSON.stringify(i,null,2)}]}}catch(n){return{content:[{type:"text",text:`Error: ${n instanceof Error?n.message:"Search failed"}`}],isError:!0}}}),e.tool("thinkermd_read","Read a specific context file from the repository. Use this to get the full content of a CONTEXT.md file.",{filePath:B.string().describe("Path to the context file relative to repository root"),path:B.string().optional().describe("Path to repository directory (defaults to current working directory)")},async({filePath:t,path:o})=>{try{let r=o||process.cwd();return{content:[{type:"text",text:(await dt({path:r,filePath:t})).content}]}}catch(r){return{content:[{type:"text",text:`Error: ${r instanceof Error?r.message:"Read failed"}`}],isError:!0}}}),e.tool("thinkermd_get_context","Get the CONTEXT.md file for a specific directory. This provides contextual documentation about that part of the codebase.",{path:B.string().describe("Path to the directory to get context for")},async({path:t})=>{try{return{content:[{type:"text",text:(await pt({path:t})).content}]}}catch(o){return{content:[{type:"text",text:`Error: ${o instanceof Error?o.message:"Get context failed"}`}],isError:!0}}}),e.tool("thinkermd_list_files","List all CONTEXT.md files available in the repository. Use this to discover what documentation is available.",{path:B.string().optional().describe("Path to repository directory (defaults to current working directory)")},async({path:t})=>{try{let o=t||process.cwd(),r=await mt({path:o});return{content:[{type:"text",text:JSON.stringify(r,null,2)}]}}catch(o){return{content:[{type:"text",text:`Error: ${o instanceof Error?o.message:"List files failed"}`}],isError:!0}}}),e}import{SSEServerTransport as Ro}from"@modelcontextprotocol/sdk/server/sse.js";var Rt=new Map,bo=ke();async function ko(e,t){console.log("[MCP] New SSE connection");let o=new Ro("/mcp/messages",t),r=crypto.randomUUID();Rt.set(r,o),t.setHeader("X-MCP-Session-Id",r),await bo.connect(o),t.on("close",()=>{Rt.delete(r),console.log("[MCP] SSE connection closed")})}async function vo(e,t){let o=e.headers["x-mcp-session-id"];if(!o){t.writeHead(400,{"Content-Type":"application/json"}),t.end(JSON.stringify({error:"Missing X-MCP-Session-Id header"}));return}let r=Rt.get(o);if(!r){t.writeHead(400,{"Content-Type":"application/json"}),t.end(JSON.stringify({error:"Invalid session"}));return}try{await r.handlePostMessage(e,t)}catch(n){console.error("[MCP] Message handling error:",n),t.headersSent||(t.writeHead(500,{"Content-Type":"application/json"}),t.end(JSON.stringify({error:"Failed to process message"})))}}function Po(){let e=new Eo;return e.get("/health",t=>t.json({status:"ok",version:"0.1.0"})),e.post("/mcp/search",async t=>{try{let o=await t.req.json(),{path:r,query:n,limit:i}=o;if(!r||!n)return t.json({error:"path and query are required"},400);let s=await lt({path:r,query:n,limit:i});return t.json(s)}catch(o){return console.error("[MCP] Search error:",o),t.json({error:o instanceof Error?o.message:"Search failed"},500)}}),e.get("/mcp/read",async t=>{try{let o=t.req.query("path"),r=t.req.query("filePath");if(!o||!r)return t.json({error:"path and filePath are required"},400);let n=await dt({path:o,filePath:r});return t.json(n)}catch(o){return console.error("[MCP] Read error:",o),t.json({error:o instanceof Error?o.message:"Read failed"},500)}}),e.get("/mcp/context",async t=>{try{let o=t.req.query("path");if(!o)return t.json({error:"path is required"},400);let r=await pt({path:o});return t.json(r)}catch(o){return console.error("[MCP] Get context error:",o),t.json({error:o instanceof Error?o.message:"Get context failed"},500)}}),e.get("/mcp/files",async t=>{try{let o=t.req.query("path");if(!o)return t.json({error:"path is required"},400);let r=await mt({path:o});return t.json(r)}catch(o){return console.error("[MCP] List files error:",o),t.json({error:o instanceof Error?o.message:"List files failed"},500)}}),e.get("/mcp/repo",async t=>{try{let o=t.req.query("path");if(!o)return t.json({error:"path is required"},400);let r=await be({path:o});return r?t.json({connected:!0,...r}):t.json({connected:!1,message:"Repository not found in ThinkerMD"})}catch(o){return console.error("[MCP] Get repo info error:",o),t.json({error:o instanceof Error?o.message:"Get repo info failed"},500)}}),e}function So(e,t){let o=`http://localhost:${e}/mcp`,r=64,n=40,i=t.length>n?t.slice(0,n-3)+"...":t;console.log(M.cyan(`
|
|
103
|
+
</html>`),a(),r({token:u,userId:g,email:f})):(c.writeHead(400,{"Content-Type":"text/plain"}),c.end("Missing required parameters"))}else c.writeHead(404,{"Content-Type":"text/plain"}),c.end("Not found")}),i.listen(e)})}async function no(e,t){try{let o=await fetch(`${e}/api/auth/validate`,{method:"GET",headers:{Authorization:`Bearer ${t}`}});return o.ok?{valid:!0,userId:(await o.json()).userId}:o.status===401?{valid:!1,error:"Invalid or expired token"}:{valid:!1,error:`API error: ${o.status} ${o.statusText}`}}catch(o){return{valid:!1,error:`Connection failed: ${o}`}}}async function ge(e){let t=pe(),o=e.apiUrl||H;if(e.token){t.start("Validating token...");let n=await no(o,e.token);n.valid||(t.fail("Authentication failed"),console.error(k.red(`Error: ${n.error}`)),process.exit(1));let i={apiToken:e.token,apiUrl:o,userId:n.userId};await nt(i),t.succeed("Authenticated successfully"),console.log(),console.log(`Config saved to: ${k.cyan(U)}`);return}let r=await $();if(r.apiToken){console.log(k.green("You are already logged in.")),console.log(`User ID: ${k.cyan(r.userId||"unknown")}`),console.log(),console.log("To re-authenticate, run:"),console.log(k.cyan(" tk login --token NEW_TOKEN")),console.log(),console.log("Or to login with a different account:"),console.log(k.cyan(" tk logout && tk login"));return}console.log(k.bold("ThinkerMD Login")),console.log(),t.start("Setting up authentication...");try{let n=await me(),i=Ze(16).toString("hex"),s=`${o}/cli/auth?port=${n}&state=${i}`;t.succeed("Ready for authentication"),console.log(),console.log("Opening browser for authentication..."),console.log(),console.log("If the browser doesn't open, visit this URL manually:"),console.log(k.cyan(s)),console.log(),await oo(s),t.start("Waiting for authentication...");let{token:a,userId:l,email:c}=await ro(n,i);await nt({apiToken:a,apiUrl:o,userId:l,email:c}),t.succeed("Authenticated successfully"),console.log(),console.log(`Logged in as: ${k.cyan(c)}`),console.log(`Config saved to: ${k.cyan(U)}`)}catch(n){t.fail("Authentication failed"),console.error(k.red(`Error: ${n instanceof Error?n.message:n}`)),console.log(),console.log("You can also authenticate manually:"),console.log(k.cyan(" tk login --token YOUR_TOKEN")),console.log(),console.log(`Get your token at: ${k.cyan(`${o}/dashboard/settings`)}`),process.exit(1)}}async function fe(){let e=pe();e.start("Logging out..."),await nt({}),e.succeed("Logged out successfully"),console.log(),console.log(`Config cleared from: ${k.cyan(U)}`)}import{join as he}from"path";import m from"chalk";import io from"ora";async function so(e){let{readFile:t}=await import("fs/promises"),{existsSync:o}=await import("fs"),r=he(I(e),"structure.json");try{if(o(r)){let n=await t(r,"utf-8");return JSON.parse(n)}}catch{}return null}async function ao(e){let{readdir:t,stat:o}=await import("fs/promises"),r=0;async function n(i){let s;try{s=await t(i)}catch{return}for(let a of s){let l=he(i,a);try{(await o(l)).isDirectory()?await n(l):a==="CONTEXT.md"&&r++}catch{}}}return await n(e),r}async function ye(){let e=io(),t=process.cwd();try{await N(t)||(console.error(m.red("Error: Not a git repository")),process.exit(1));let o=await T(t),r=await P(),n=I(o);console.log(m.bold("ThinkerMD Status")),console.log();let i=await z(o),s=r.branch||await S(o),a=r.commitSha||await _(o);console.log(m.dim("Repository:")),console.log(` Name: ${m.cyan(i||"unknown")}`),console.log(` Branch: ${m.cyan(s)}`),console.log(` Commit: ${m.cyan(a.slice(0,7))}`),console.log();let{stat:l}=await import("fs/promises"),c=!1;try{await l(n),c=!0}catch{}if(!c){console.log(m.dim("Local Context:")),console.log(` Status: ${m.yellow("Not initialized")}`),console.log(),console.log("Run "+m.cyan("tk init")+" to create .thinker.md/");return}console.log(m.dim("Local Context:"));let d=await F(o),u=await so(o),g=await ao(n);if(console.log(` Status: ${m.green("Initialized")}`),console.log(` Project ID: ${m.cyan(d?.projectId||"unknown")}`),console.log(` Context files: ${m.cyan(g)}`),u&&(console.log(` Directories: ${m.cyan(u.directoryCount)}`),console.log(` Files tracked: ${m.cyan(u.fileCount)}`)),d?.generatedAt){let p=new Date(d.generatedAt);console.log(` Generated: ${m.cyan(p.toLocaleString())}`)}d?.pulledFrom&&console.log(` Pulled from: ${m.cyan(d.pulledFrom.slice(0,7))}`),console.log();let f=r.metaRepoId||d?.projectId;if(f&&r.r2Endpoint){console.log(m.dim("Remote Storage:")),e.start("Checking remote...");try{let p=await ct(f,s);p?(e.stop(),console.log(` Latest commit: ${m.cyan(p.slice(0,7))}`),p===a?console.log(` Status: ${m.green("Up to date")}`):d?.pulledFrom===p?console.log(` Status: ${m.yellow("Local changes not pushed")}`):(console.log(` Status: ${m.yellow("Remote has newer version")}`),console.log(),console.log("Run "+m.cyan("tk pull")+" to download latest context"))):(e.stop(),console.log(` Status: ${m.yellow("No remote context found")}`),console.log(),console.log("Run "+m.cyan("tk push")+" to upload context"))}catch{e.stop(),console.log(` Status: ${m.dim("Unable to check (no credentials)")}`)}}console.log(),console.log(m.dim("Mode:")),console.log(` ${r.mode==="sandbox"?m.cyan("Sandbox"):m.cyan("Local")}`),r.mode==="sandbox"&&console.log(` Job ID: ${m.cyan(r.jobId||"unknown")}`)}catch(o){e.fail("Failed to get status"),console.error(m.red(`Error: ${o}`)),process.exit(1)}}import w from"chalk";import co from"ora";async function we(e){let t=co(),o=process.cwd();try{await N(o)||(console.error(w.red("Error: Not a git repository")),process.exit(1));let r=await T(o);if((await P()).mode==="sandbox"){console.error(w.yellow("Note: In sandbox mode, generation is triggered automatically.")),console.log("Use 'tk init' to create structure and 'tk push' when done.");return}let i=await $();i.apiToken||(console.error(w.red("Error: Not logged in")),console.log(),console.log("Please authenticate first:"),console.log(w.cyan(" tk login --token YOUR_TOKEN")),process.exit(1));let s=i.apiUrl||H,a=await z(r),l=e.branch||await S(r),c=await _(r);a||(console.error(w.red("Error: Unable to determine repository name")),console.log("Make sure this repository has a remote origin configured."),process.exit(1)),console.log(w.bold("Trigger Context Generation")),console.log(),console.log(`Repository: ${w.cyan(a)}`),console.log(`Branch: ${w.cyan(l)}`),console.log(`Commit: ${w.cyan(c.slice(0,7))}`),console.log(),t.start("Looking up meta-repository...");let d=await fetch(`${s}/api/meta-repositories/lookup?repo=${encodeURIComponent(a)}`,{headers:{Authorization:`Bearer ${i.apiToken}`}});d.ok||(d.status===404&&(t.fail("Meta-repository not found"),console.log(),console.log("This repository is not connected to ThinkerMD. Please add it at:"),console.log(w.cyan(`${s}/dashboard`)),process.exit(1)),t.fail("API error"),console.error(w.red(`Error: ${d.status} ${d.statusText}`)),process.exit(1));let u=await d.json();t.succeed(`Found meta-repository: ${u.id}`),t.start("Triggering generation job...");let g=await fetch(`${s}/api/jobs/generate`,{method:"POST",headers:{Authorization:`Bearer ${i.apiToken}`,"Content-Type":"application/json"},body:JSON.stringify({metaRepositoryId:u.id,commitSha:c,branch:l,priority:"normal"})});if(!g.ok){t.fail("Failed to trigger generation");let p=await g.json().catch(()=>({}));console.error(w.red(`Error: ${p.error||g.statusText}`)),process.exit(1)}let f=await g.json();if(t.succeed("Generation job queued"),console.log(),console.log(`Job ID: ${w.cyan(f.jobId)}`),console.log(`Status: ${w.cyan(f.status)}`),f.estimatedPosition&&console.log(`Queue position: ${w.cyan(f.estimatedPosition)}`),console.log(),console.log("Track progress at:"),console.log(w.cyan(`${s}/dashboard/repositories/${u.id}/jobs/${f.jobId}`)),e.wait){console.log(),t.start("Waiting for completion...");let p=!1,C="";for(;!p;){await new Promise(x=>setTimeout(x,5e3));let G=await fetch(`${s}/api/jobs/${f.jobId}`,{headers:{Authorization:`Bearer ${i.apiToken}`}});if(G.ok){let x=await G.json();x.status!==C&&(C=x.status,t.text=`Status: ${x.status} (${x.progress||0}%)`),(x.status==="completed"||x.status==="failed")&&(p=!0,x.status==="completed"?(t.succeed("Generation completed!"),console.log(),console.log("Run "+w.cyan("tk pull")+" to download the generated context")):(t.fail("Generation failed"),x.errorMessage&&console.error(w.red(`Error: ${x.errorMessage}`))))}}}}catch(r){t.fail("Failed to trigger generation"),console.error(w.red(`Error: ${r}`)),process.exit(1)}}import bt from"chalk";import{Hono as Eo}from"hono";import M from"chalk";import{createServer as Ro}from"http";import{homedir as Re}from"os";import{join as Te}from"path";import{readFile as uo,writeFile as mo,mkdir as go}from"fs/promises";import{existsSync as fo}from"fs";async function lo(){return(await $()).apiUrl||process.env.THINKERMD_API_URL||H}async function po(){return(await $()).apiToken||process.env.THINKERMD_API_TOKEN||null}async function lt(e,t={}){let o=await lo(),r=await po();if(!r)throw new Error("Not authenticated. Run `tk login` first.");let{method:n="GET",body:i,params:s}=t,a=`${o}${e}`;if(s){let c=new URLSearchParams(s);a+=`?${c.toString()}`}let l=await fetch(a,{method:n,headers:{Authorization:`Bearer ${r}`,"Content-Type":"application/json"},body:i?JSON.stringify(i):void 0});if(!l.ok){let c=await l.json().catch(()=>({}));throw new Error(c.error||`API request failed: ${l.status} ${l.statusText}`)}return l.json()}async function xe(e,t,o={}){return lt("/api/mcp/search",{method:"POST",body:{repositoryId:e,query:t,branch:o.branch,limit:o.limit||10}})}async function Rt(e,t,o={}){return lt("/api/mcp/read",{params:{repositoryId:e,filePath:t,...o.branch&&{branch:o.branch}}})}async function Ce(e,t={}){return lt("/api/mcp/files",{params:{repositoryId:e,...t.branch&&{branch:t.branch}}})}async function Ee(e){try{let t=await lt("/api/user/repositories",{params:{remoteUrl:e}});return t.repositories&&t.repositories.length>0?t.repositories[0]:null}catch{return null}}var Tt=Te(Re(),".thinkermd","cache","repo-mapping.json"),ho=24*60*60*1e3;async function yo(){try{if(fo(Tt)){let e=await uo(Tt,"utf-8");return JSON.parse(e)}}catch{}return{mappings:{}}}async function wo(e){let t=Te(Re(),".thinkermd","cache");await go(t,{recursive:!0}),await mo(Tt,JSON.stringify(e,null,2))}async function V(e){let t;try{t=await T(e)}catch{return console.error(`[RepoResolver] Not a git repository: ${e}`),null}let o=await ht(t);if(!o)return console.error(`[RepoResolver] No git remote found for: ${t}`),null;let r=xo(o),n=await yo(),i=n.mappings[r];if(i&&Date.now()-i.cachedAt<ho){console.log(`[RepoResolver] Cache hit for: ${r}`);let c=await S(t),d=await _(t);return{repositoryId:i.repositoryId,fullName:i.fullName,remoteUrl:r,gitRoot:t,branch:c||"main",commit:d||"HEAD"}}console.log(`[RepoResolver] Querying API for: ${r}`);let s=await Ee(r);if(!s)return console.error(`[RepoResolver] Repository not found in ThinkerMD: ${r}`),null;n.mappings[r]={remoteUrl:r,repositoryId:s.id,fullName:s.fullName,cachedAt:Date.now()},await wo(n);let a=await S(t),l=await _(t);return{repositoryId:s.id,fullName:s.fullName,remoteUrl:r,gitRoot:t,branch:a||"main",commit:l||"HEAD"}}function xo(e){return e=e.replace(/\.git$/,""),e.startsWith("git@")&&(e=e.replace(/^git@/,"https://").replace(/:([^/])/,"/$1")),e.startsWith("http://")&&(e=e.replace("http://","https://")),e}async function dt(e){let{path:t,query:o,limit:r=10}=e,n=await V(t);if(!n)throw new Error(`Could not resolve repository for path: ${t}`);return{...await xe(n.repositoryId,o,{branch:n.branch,limit:r}),repositoryId:n.repositoryId,fullName:n.fullName}}async function pt(e){let{path:t,filePath:o}=e,r=await V(t);if(!r)throw new Error(`Could not resolve repository for path: ${t}`);return{...await Rt(r.repositoryId,o,{branch:r.branch}),repositoryId:r.repositoryId,fullName:r.fullName}}async function ut(e){let{path:t}=e,o=await V(t);if(!o)throw new Error(`Could not resolve repository for path: ${t}`);let r=t;t.startsWith(o.gitRoot)&&(r=t.slice(o.gitRoot.length).replace(/^\//,""));let n=r?`.thinker.md/contexts/${r}/CONTEXT.md`:".thinker.md/contexts/CONTEXT.md";return{...await Rt(o.repositoryId,n,{branch:o.branch}),repositoryId:o.repositoryId,fullName:o.fullName}}async function mt(e){let{path:t}=e,o=await V(t);if(!o)throw new Error(`Could not resolve repository for path: ${t}`);return{...await Ce(o.repositoryId,{branch:o.branch}),repositoryId:o.repositoryId,fullName:o.fullName}}async function be(e){return V(e.path)}import{McpServer as Co}from"@modelcontextprotocol/sdk/server/mcp.js";import{z as K}from"zod";function ke(){let e=new Co({name:"thinkermd",version:"0.1.0"});return e.tool("thinkermd_search","Semantic search across context files in the repository. Use this to find relevant documentation and context about the codebase.",{query:K.string().describe("Search query - what you're looking for"),path:K.string().optional().describe("Path to directory (defaults to current working directory)"),limit:K.number().optional().default(10).describe("Maximum number of results to return")},async({query:t,path:o,limit:r})=>{try{let n=o||process.cwd(),i=await dt({path:n,query:t,limit:r});return{content:[{type:"text",text:JSON.stringify(i,null,2)}]}}catch(n){return{content:[{type:"text",text:`Error: ${n instanceof Error?n.message:"Search failed"}`}],isError:!0}}}),e.tool("thinkermd_read","Read a specific context file from the repository. Use this to get the full content of a CONTEXT.md file.",{filePath:K.string().describe("Path to the context file relative to repository root"),path:K.string().optional().describe("Path to repository directory (defaults to current working directory)")},async({filePath:t,path:o})=>{try{let r=o||process.cwd();return{content:[{type:"text",text:(await pt({path:r,filePath:t})).content}]}}catch(r){return{content:[{type:"text",text:`Error: ${r instanceof Error?r.message:"Read failed"}`}],isError:!0}}}),e.tool("thinkermd_get_context","Get the CONTEXT.md file for a specific directory. This provides contextual documentation about that part of the codebase.",{path:K.string().describe("Path to the directory to get context for")},async({path:t})=>{try{return{content:[{type:"text",text:(await ut({path:t})).content}]}}catch(o){return{content:[{type:"text",text:`Error: ${o instanceof Error?o.message:"Get context failed"}`}],isError:!0}}}),e.tool("thinkermd_list_files","List all CONTEXT.md files available in the repository. Use this to discover what documentation is available.",{path:K.string().optional().describe("Path to repository directory (defaults to current working directory)")},async({path:t})=>{try{let o=t||process.cwd(),r=await mt({path:o});return{content:[{type:"text",text:JSON.stringify(r,null,2)}]}}catch(o){return{content:[{type:"text",text:`Error: ${o instanceof Error?o.message:"List files failed"}`}],isError:!0}}}),e}import{StreamableHTTPServerTransport as To}from"@modelcontextprotocol/sdk/server/streamableHttp.js";import{isInitializeRequest as bo}from"@modelcontextprotocol/sdk/types.js";var D=new Map;async function ko(e){let t=[];for await(let o of e)t.push(o);return Buffer.concat(t).toString()}async function vo(e,t){let o=e.headers["mcp-session-id"],r=await ko(e),n;try{n=JSON.parse(r)}catch{t.writeHead(400,{"Content-Type":"application/json"}),t.end(JSON.stringify({jsonrpc:"2.0",error:{code:-32700,message:"Parse error: Invalid JSON"},id:null}));return}if(o&&D.has(o)){await D.get(o).handleRequest(e,t,r);return}if(!o&&bo(n)){console.log("[MCP] New session initializing...");let i=new To({sessionIdGenerator:()=>crypto.randomUUID(),onsessioninitialized:a=>{D.set(a,i),console.log(`[MCP] Session started: ${a}`)}});i.onclose=()=>{let a=i.sessionId;a&&D.has(a)&&(console.log(`[MCP] Session closed: ${a}`),D.delete(a))},await ke().connect(i),await i.handleRequest(e,t,r);return}t.writeHead(400,{"Content-Type":"application/json"}),t.end(JSON.stringify({jsonrpc:"2.0",error:{code:-32e3,message:"Bad Request: No valid session ID provided"},id:null}))}async function Po(e,t){let o=e.headers["mcp-session-id"];if(!o||!D.has(o)){t.writeHead(400,{"Content-Type":"application/json"}),t.end(JSON.stringify({error:"Invalid or missing session ID"}));return}console.log(`[MCP] SSE stream requested for session: ${o}`),await D.get(o).handleRequest(e,t)}async function So(e,t){let o=e.headers["mcp-session-id"];if(!o||!D.has(o)){t.writeHead(400,{"Content-Type":"application/json"}),t.end(JSON.stringify({error:"Invalid or missing session ID"}));return}console.log(`[MCP] Session termination requested: ${o}`),await D.get(o).handleRequest(e,t)}async function $o(e,t){try{e.method==="POST"?await vo(e,t):e.method==="GET"?await Po(e,t):e.method==="DELETE"?await So(e,t):(t.writeHead(405,{"Content-Type":"application/json"}),t.end(JSON.stringify({error:"Method not allowed"})))}catch(o){console.error("[MCP] Error handling request:",o),t.headersSent||(t.writeHead(500,{"Content-Type":"application/json"}),t.end(JSON.stringify({jsonrpc:"2.0",error:{code:-32603,message:"Internal server error"},id:null})))}}function Io(){let e=new Eo;return e.get("/health",t=>t.json({status:"ok",version:"0.1.0"})),e.post("/mcp/search",async t=>{try{let o=await t.req.json(),{path:r,query:n,limit:i}=o;if(!r||!n)return t.json({error:"path and query are required"},400);let s=await dt({path:r,query:n,limit:i});return t.json(s)}catch(o){return console.error("[MCP] Search error:",o),t.json({error:o instanceof Error?o.message:"Search failed"},500)}}),e.get("/mcp/read",async t=>{try{let o=t.req.query("path"),r=t.req.query("filePath");if(!o||!r)return t.json({error:"path and filePath are required"},400);let n=await pt({path:o,filePath:r});return t.json(n)}catch(o){return console.error("[MCP] Read error:",o),t.json({error:o instanceof Error?o.message:"Read failed"},500)}}),e.get("/mcp/context",async t=>{try{let o=t.req.query("path");if(!o)return t.json({error:"path is required"},400);let r=await ut({path:o});return t.json(r)}catch(o){return console.error("[MCP] Get context error:",o),t.json({error:o instanceof Error?o.message:"Get context failed"},500)}}),e.get("/mcp/files",async t=>{try{let o=t.req.query("path");if(!o)return t.json({error:"path is required"},400);let r=await mt({path:o});return t.json(r)}catch(o){return console.error("[MCP] List files error:",o),t.json({error:o instanceof Error?o.message:"List files failed"},500)}}),e.get("/mcp/repo",async t=>{try{let o=t.req.query("path");if(!o)return t.json({error:"path is required"},400);let r=await be({path:o});return r?t.json({connected:!0,...r}):t.json({connected:!1,message:"Repository not found in ThinkerMD"})}catch(o){return console.error("[MCP] Get repo info error:",o),t.json({error:o instanceof Error?o.message:"Get repo info failed"},500)}}),e}function No(e,t){let o=`http://localhost:${e}/mcp`,r=64,n=40,i=t.length>n?t.slice(0,n-3)+"...":t;console.log(M.cyan(`
|
|
104
104
|
\u2554${"\u2550".repeat(r)}\u2557
|
|
105
105
|
\u2551${" ".repeat(r)}\u2551
|
|
106
106
|
\u2551 \u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2557\u2588\u2588\u2557 \u2588\u2588\u2557\u2588\u2588\u2557\u2588\u2588\u2588\u2557 \u2588\u2588\u2557\u2588\u2588\u2557 \u2588\u2588\u2557\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2557\u2588\u2588\u2588\u2588\u2588\u2588\u2557 \u2551
|
|
@@ -112,17 +112,17 @@ This appears to be the first generation. Use 'tk init' to create initial structu
|
|
|
112
112
|
\u2551 .md \u2551
|
|
113
113
|
\u2551${" ".repeat(r)}\u2551
|
|
114
114
|
\u2560${"\u2550".repeat(r)}\u2563
|
|
115
|
-
\u2551${" ".repeat(r)}\u2551`));let s=` \u2713 Logged in as: ${i}`,a=s+" ".repeat(Math.max(0,r-s.length));console.log(M.cyan("\u2551")+a+M.cyan("\u2551"));let
|
|
115
|
+
\u2551${" ".repeat(r)}\u2551`));let s=` \u2713 Logged in as: ${i}`,a=s+" ".repeat(Math.max(0,r-s.length));console.log(M.cyan("\u2551")+a+M.cyan("\u2551"));let l=` \u2713 Daemon running on: ${o}`,c=l+" ".repeat(Math.max(0,r-l.length));console.log(M.cyan("\u2551")+c+M.cyan("\u2551")),console.log(M.cyan(`\u2551${" ".repeat(r)}\u2551
|
|
116
116
|
\u2560${"\u2550".repeat(r)}\u2563
|
|
117
117
|
\u2551 Add to your AI tool config: \u2551
|
|
118
118
|
\u2551${" ".repeat(r)}\u2551
|
|
119
119
|
\u2551 { \u2551
|
|
120
120
|
\u2551 "mcpServers": { \u2551
|
|
121
121
|
\u2551 "thinkermd": { \u2551
|
|
122
|
-
\u2551 "type": "http", \u2551`));let d=` "url": "${o}"`,
|
|
122
|
+
\u2551 "type": "http", \u2551`));let d=` "url": "${o}"`,u=" "+d+" ".repeat(Math.max(0,r-3-d.length));console.log(M.cyan("\u2551")+u+M.cyan("\u2551")),console.log(M.cyan(`\u2551 } \u2551
|
|
123
123
|
\u2551 } \u2551
|
|
124
124
|
\u2551 } \u2551
|
|
125
125
|
\u2551${" ".repeat(r)}\u2551
|
|
126
126
|
\u2551 ${M.yellow("Press Ctrl+C to stop")} \u2551
|
|
127
127
|
\u255A${"\u2550".repeat(r)}\u255D
|
|
128
|
-
`))}async function ve(e){let{port:t}=e,o=
|
|
128
|
+
`))}async function ve(e){let{port:t}=e,o=Io(),r=await $(),n=r.email||r.userId||"authenticated user";Ro(async(s,a)=>{let l=new URL(s.url||"/",`http://localhost:${t}`);if(l.pathname==="/mcp"){await $o(s,a);return}let c={};for(let[u,g]of Object.entries(s.headers))typeof g=="string"?c[u]=g:Array.isArray(g)&&(c[u]=g.join(", "));let d=await o.fetch(new Request(l.toString(),{method:s.method,headers:c,body:s.method!=="GET"&&s.method!=="HEAD"?s:void 0,duplex:"half"}));if(a.writeHead(d.status,Object.fromEntries(d.headers.entries())),d.body){let u=d.body.getReader();try{for(;;){let{done:g,value:f}=await u.read();if(g)break;a.write(f)}}finally{u.releaseLock()}}a.end()}).listen(t,()=>{No(t,n)}),await new Promise(()=>{})}var _o=3456;async function kt(e){(await $()).apiToken||(console.error(bt.red("Not authenticated. Run `tk login` first.")),process.exit(1));let o=e.port?parseInt(e.port,10):_o;(isNaN(o)||o<1||o>65535)&&(console.error(bt.red(`Invalid port: ${e.port}`)),process.exit(1));try{await ve({port:o})}catch(r){console.error(bt.red("Failed to start daemon:"),r),process.exit(1)}}var v=new Ao;v.name("tk").description("ThinkerMD CLI - Manage .thinker.md/ context files").version("0.1.0");v.command("init").description("Create .thinker.md/ structure with empty CONTEXT.md files").option("-f, --force","Reinitialize even if .thinker.md/ exists").option("-m, --merge","Only add missing CONTEXT.md files, keep existing ones").action(e=>Qt(e));v.command("validate").description("Verify .thinker.md/ structure matches repository").option("-q, --quiet","Only output errors, no summary").action(e=>Zt(e));v.command("pull").description("Download previous context from storage").option("-b, --branch <branch>","Specify branch to pull from").option("-c, --commit <sha>","Pull specific commit version").action(e=>ae(e));v.command("push").description("Validate and upload .thinker.md/ to storage").option("--skip-validation","Skip validation before push").option("--force-full","Force full generation mode (ignore previous context)").action(e=>de(e));v.command("login").description("Authenticate with ThinkerMD API").option("-t, --token <token>","API token").option("--api-url <url>","Custom API URL").action(e=>ge(e));v.command("logout").description("Remove saved authentication").action(()=>fe());v.command("status").description("Show status of .thinker.md/ context").action(()=>ye());v.command("generate").description("Trigger context generation via API").option("-b, --branch <branch>","Specify branch to generate for").option("-w, --wait","Wait for generation to complete").action(e=>we(e));v.command("serve").description("Start MCP daemon for AI assistant integration").option("-p, --port <port>","Port to listen on","3456").action(e=>kt(e));v.on("command:*",()=>{console.error(Pe.red(`Unknown command: ${v.args.join(" ")}`)),console.log(),v.help()});async function Se(){try{if(process.argv.length===2){await kt({port:"3456"});return}await v.parseAsync(process.argv)}catch(e){e instanceof Error&&console.error(Pe.red(`Error: ${e.message}`)),process.exit(1)}}Se().catch(e=>{console.error("Fatal error:",e),process.exit(1)});
|