UNPKG

grix-connector

Version:

Connect local AI coding agents (Claude, Codex, Gemini, Qwen, DeepSeek, Cursor, OpenCode, Pi, OpenHuman, Reasonix) to the Grix scheduling platform. Also serves as an OpenClaw plugin for Grix channel transport.

9 lines (7 loc) 2.41 kB
import{mkdir as y,readFile as g,rename as $,writeFile as _}from"node:fs/promises";import{readFileSync as h}from"node:fs";import{dirname as C,isAbsolute as b,join as u}from"node:path";import{homedir as E}from"node:os";let x=Promise.resolve();function v(e){return e||process.env.CODEX_HOME||u(E(),".codex")}function T(e){return((e||"codex").split(/[\\/]/).pop()?.toLowerCase()||"codex").replace(/\.(exe|cmd|bat)$/i,"")==="codex"}async function A(e,o={}){if(!b(e))throw new Error(`Codex trusted project path must be absolute: ${e}`);const r=u(v(o.codexHome),"config.toml"),n=async()=>{const i=await j(r),t=k(i,e);return t===i?{configPath:r,changed:!1}:(await O(r,t),{configPath:r,changed:!0})},s=x.then(n,n);return x=s.catch(()=>{}),s}function k(e,o){const r=`[projects.${K(o)}]`,n=e.split(/\r?\n/),s=n.findIndex(t=>t.trim()===r);if(s===-1){const t=e.trimEnd(),c=`${r} trust_level = "trusted" `;return t?`${t} ${c}`:c}let i=n.length;for(let t=s+1;t<n.length;t+=1)if(/^\s*\[.*\]\s*$/.test(n[t])){i=t;break}for(let t=s+1;t<i;t+=1)if(/^\s*trust_level\s*=/.test(n[t]))return/^\s*trust_level\s*=\s*"trusted"\s*(?:#.*)?$/.test(n[t])?e:(n[t]='trust_level = "trusted"',w(n,e));return n.splice(s+1,0,'trust_level = "trusted"'),w(n,e)}async function j(e){try{return await g(e,"utf8")}catch(o){if(o.code==="ENOENT")return"";throw o}}async function O(e,o){await y(C(e),{recursive:!0});const r=`${e}.${process.pid}.${Date.now()}.tmp`;await _(r,o,{mode:384}),await $(r,e)}function K(e){return`"${e.replace(/\\/g,"\\\\").replace(/"/g,'\\"')}"`}function w(e,o){const r=e.join(` `);return o.endsWith(` `)?r:r.replace(/\n$/,"")}function F(e){const o=v(e);let r,n;try{const i=u(o,"config.toml"),t=h(i,"utf8"),c=t.match(/^\s*model_provider\s*=\s*"([^"]+)"/m);if(c){const d=c[1],a=t.indexOf(`[model_providers.${d}]`);if(a>=0){const l=t.indexOf(` [`,a+1),f=l>=0?t.slice(a,l):t.slice(a),m=f.match(/^\s*base_url\s*=\s*"([^"]+)"/m);m&&(r=m[1].trim());const p=f.match(/^\s*env_key\s*=\s*"([^"]+)"/m);p&&(n=p[1].trim())}}}catch{}let s;if(n){const i=(process.env[n]??"").trim();i&&(s=i)}if(!s)try{const i=u(o,"auth.json"),t=JSON.parse(h(i,"utf8")),c=t.OPENAI_API_KEY;if(typeof c=="string"&&c.trim()&&(s=c.trim()),!s){const a=t.tokens?.access_token;typeof a=="string"&&a.trim()&&(s=a.trim())}}catch{}return{baseUrl:r,apiKey:s}}export{A as ensureCodexProjectTrusted,T as isCodexCommand,F as readCodexProviderSettings,v as resolveCodexHome};