@crewx/app 0.1.0-rc.130 → 0.1.0-rc.131
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/build-app/index.mjs +7 -7
- package/package.json +2 -2
package/build-app/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import{existsSync as
|
|
2
|
-
`}}function
|
|
3
|
-
`,toolsFileName:
|
|
4
|
-
`}}function
|
|
5
|
-
`);try{const
|
|
6
|
-
`);return 1}s(`${
|
|
7
|
-
`);return 1}}export{
|
|
1
|
+
import{randomUUID as Z}from"node:crypto";import{existsSync as E,mkdirSync as ee,mkdtempSync as te,readFileSync as _,realpathSync as U,rmSync as O,watch as re,writeFileSync as j}from"node:fs";import{createRequire as B}from"node:module";import{tmpdir as ne}from"node:os";import{basename as oe,dirname as b,isAbsolute as se,join as c,relative as h,resolve as N,sep as W}from"node:path";import{fileURLToPath as ae,pathToFileURL as F}from"node:url";import{CREWX_META_NAMESPACE as S,convertV2ToManifest as D}from"./manifest.mjs";const J=b(ae(import.meta.url));const V=B(import.meta.url);const ie=c("packages","built-in","app","build-app");const T=N(J,"..","..","..","..");const k=c(T,"packages","sdk","src","app","index.ts");const q=b(k);const x="server.ts";const $="dist/server.mjs";const K="dist/entry.mjs";const I="dist/main.mjs";const X="dist/tools.json";const ce=1;const y="manifest.json";const le=100;async function Y(e){try{return V(e)}catch(t){if(t?.code!=="ERR_REQUIRE_ASYNC_MODULE")throw t;return import(F(e).href)}}const fe=[`import App from './${x}';`,`export * from './${x}';`,"export default App;","export { AppError, CrewXApp, Tool, executeAppRequest, generateManifest, getToolDeclarations, runSkillApp } from '@crewx/sdk/app';",""].join("\n");const ue="export { runAppEntry } from '@crewx/sdk/app';\n";const de=["import { runAppEntry } from '@crewx/sdk/app';","await runAppEntry(() => import('./server.mjs'));",""].join("\n");const pe=[`import App from './${x}';`,"export default App;","export { generateToolDeclarations } from '@crewx/sdk/app';",""].join("\n");const me="export { SKILL_BACKEND_ENV_NAMES, isSkillBackendEnv } from '@crewx/sdk/app';\n";const we="dist/.env-rule.mjs";const he="import { createRequire as __crewxCreateRequire } from 'node:module'; const require = __crewxCreateRequire(import.meta.url);";const Ee={compilerOptions:{target:"ES2022",module:"ESNext",moduleResolution:"Bundler",experimentalDecorators:false,emitDecoratorMetadata:false,useDefineForClassFields:true,lib:["ES2022","esnext.decorators"],allowJs:true,checkJs:false}};function xe(e,t,n,o){let s=e.generateManifest(e.default,t);if(o!==void 0&&Array.isArray(s.actions)){s={...s,actions:s.actions.map(a=>({...a,backend:{...a.backend,env:[...o]}}))}}let r;if(n===void 0){r=D(s)}else{try{r=JSON.parse(n)}catch(i){throw new Error(`${y} is not valid JSON: ${i instanceof Error?i.message:String(i)}`)}const a=r?._meta?.[S];if(a===null||typeof a!=="object"||Array.isArray(a)){throw new Error(`${y} must have an object at _meta["${S}"]`)}a.actions=s.actions}return{fileName:y,text:`${JSON.stringify(r,null,2)}
|
|
2
|
+
`}}function ye(e,t){const n=e.generateToolDeclarations(e.default);let o;if(t===void 0){o=D({...e.default.definition})}else{try{o=JSON.parse(t)}catch(r){throw new Error(`${y} is not valid JSON: ${r instanceof Error?r.message:String(r)}`)}const s=o?._meta?.[S];if(s===null||typeof s!=="object"||Array.isArray(s)){throw new Error(`${y} must have an object at _meta["${S}"]`)}delete s.actions}return{fileName:y,text:`${JSON.stringify(o,null,2)}
|
|
3
|
+
`,toolsFileName:X,toolsText:`${JSON.stringify({schemaVersion:ce,actions:n},null,2)}
|
|
4
|
+
`}}function Ae(e,t=process.cwd()){let n;let o=false;let s=false;const r=a=>{if(n!==void 0)throw new Error("Give the app folder only once");n=N(t,a)};for(const a of e){if(a==="--check")o=true;else if(a==="--watch")s=true;else if(a.startsWith("--app-dir="))r(a.slice("--app-dir=".length));else if(a.startsWith("-"))throw new Error(`Unknown argument: ${a}`);else r(a)}if(o&&s)throw new Error("--check and --watch cannot be combined");return{appDirectory:n??t,check:o,watch:s}}function ge(e){const t=c(e,"package.json");if(!E(t))throw new Error(`${t} not found; run the build from an app folder or give the app folder`);const n=JSON.parse(_(t,"utf8"));const o=n.crewx?.app??{};if(ve(n)){if(o.env!==void 0){throw new Error("crewx.app.env is only for an App with a bin or crewx.app.command; a server.ts-only App always gets its instance and thread as this.context")}const i=o.external??[];if(!Array.isArray(i)||!i.every(d=>typeof d==="string")){throw new Error("crewx.app.external must be an array of globs")}return{external:i}}const s=o.command??Re(n);if(!Array.isArray(s)||s.length===0||!s.every(i=>typeof i==="string"&&i!=="")){throw new Error("crewx.app.command must be a non-empty array of strings")}const r=o.external??[];if(!Array.isArray(r)||!r.every(i=>typeof i==="string")){throw new Error("crewx.app.external must be an array of globs")}const a=o.env;if(a!==void 0&&(!Array.isArray(a)||!a.every(i=>typeof i==="string"))){throw new Error("crewx.app.env must be an array of environment variable names")}return{command:s,external:r,...a===void 0?{}:{env:a}}}function ve(e){return e.bin===void 0&&e.crewx?.app?.command===void 0}async function Se(e,t,n){let o;try{o=await R(e,t,n,{contents:me,sourcefile:"crewx-app-env-rule.ts",outfile:we})}catch(r){if(r.errors?.some(({text:a})=>/No matching export/u.test(a))){throw new Error("crewx.app.env needs a newer @crewx/sdk: reinstall the CrewX CLI")}throw r}const s=te(c(ne(),"crewx-app-env-"));try{const r=c(s,"rule.mjs");j(r,o.text,"utf8");const a=await Y(r);if(!a.isSkillBackendEnv(n.env)){throw new Error(`crewx.app.env must be a non-empty list of distinct names from ${a.SKILL_BACKEND_ENV_NAMES.join(", ")}`)}}finally{O(s,{recursive:true,force:true})}}function Re(e){const t=e.bin;const n=typeof t==="string"?[String(e.name??"").replace(/^@[^/]+\//u,"")]:Object.keys(t??{});if(n.length!==1||n[0]===""){throw new Error('cannot derive the skill command: give package.json exactly one "bin" name, or set crewx.app.command')}return[n[0],"app"]}function _e(e){let t="^";for(let n=0;n<e.length;n+=1){const o=e[n];if(o==="*"&&e[n+1]==="*"){t+=".*";n+=1}else if(o==="*"){t+="[^/]*"}else if(o==="?"){t+="[^/]"}else{t+=o.replace(/[\\^$+{}()[\].|]/g,"\\$&")}}return new RegExp(`${t}$`)}function be(e,t){const n=h(e,t);return n!==""&&!n.startsWith("..")&&!se(n)}function P(e){if(J!==c(T,ie))return false;if(!E(k))return false;return be(U(T),U(e))}async function Ne(e){try{const t=B(c(e,"package.json")).resolve("esbuild");return await import(F(t).href)}catch{}try{return await import("esbuild")}catch{throw new Error("cannot load esbuild: it comes with @crewx/app, so reinstall the CrewX CLI")}}const L="crewx-sdk";const ke="app/index.js";function G(){try{return V.resolve("@crewx/sdk/app")}catch{return void 0}}function $e(e,t){const n=t.external.map(_e);const o=P(e);return[{name:"crewx-sdk-app",setup(s){s.onResolve({filter:/^@crewx\/sdk\/app$/},()=>{if(o)return{path:k};const r=G();if(r===void 0){return{errors:[{text:"cannot resolve @crewx/sdk/app: it comes with the CrewX CLI, so reinstall the CLI"}]}}return{path:ke,namespace:L,pluginData:{file:r}}});s.onLoad({filter:/.*/,namespace:L},({pluginData:r})=>({contents:_(r.file,"utf8"),loader:"js",resolveDir:b(r.file)}))}},{name:"crewx-app-external-files",setup(s){if(n.length===0)return;s.onResolve({filter:/^\.\.?\//},({path:r,resolveDir:a})=>{const i=N(a,r);const d=h(e,i).split(W).join("/");if(d.startsWith("..")||!n.some(l=>l.test(d)))return void 0;const p=h(c(e,"dist"),i).split(W).join("/");return{path:p.startsWith(".")?p:`./${p}`,external:true}})}}]}const Pe={name:"crewx-app-server-import",setup(e){e.onResolve({filter:/^\.\/server\.mjs$/},({path:t})=>({path:t,external:true}))}};async function R(e,t,n,{contents:o,sourcefile:s,outfile:r,metafile:a=false,plugins:i=[]}){const d=await e.build({...P(t)?{}:{banner:{js:he}},absWorkingDir:t,stdin:{contents:o,resolveDir:t,sourcefile:s,loader:"ts"},outfile:r,bundle:true,write:false,metafile:a,format:"esm",platform:"node",target:"node22",packages:"external",plugins:[...$e(t,n),...i],tsconfigRaw:Ee,logLevel:"silent"}).catch(f=>{if(f.errors?.some(({text:w})=>/No matching export in ".*server\.ts" for import "default"/u.test(w))){throw new Error(`${x} must default-export a CrewXApp subclass with a static definition`)}throw f});const p=c(t,r);const l=d.outputFiles.find(f=>f.path===p)?.text;if(l===void 0)throw new Error(`esbuild did not produce ${r}`);if(/@crewx\/sdk(?:['"/])/u.test(l.replace(/^\s*\/\/.*$/gmu,""))){throw new Error(`${r} still imports @crewx/sdk; server.ts may import only @crewx/sdk/app`)}return{text:l,inputs:d.metafile?Object.keys(d.metafile.inputs):[]}}async function H({appDirectory:e,check:t=false,log:n=()=>{}}){if(!E(c(e,x)))throw new Error(`${c(e,x)} not found`);const o=ge(e);const s=o.command===void 0;const r=await Ne(e);if(o.env!==void 0)await Se(r,e,o);const a=c(e,"dist");const i=E(a);const d=c(a,`.server-build-${process.pid}-${Z()}.mjs`);try{const p=await R(r,e,o,{contents:fe,sourcefile:"crewx-app-server.ts",outfile:$,metafile:true});const l=s?await R(r,e,o,{contents:de,sourcefile:"crewx-app-main.ts",outfile:I,plugins:[Pe]}):await R(r,e,o,{contents:ue,sourcefile:"crewx-app-entry.ts",outfile:K});const f=s?await Ce(r,e,o):p;ee(a,{recursive:true});j(d,f.text,"utf8");const w=await Y(d);if(typeof w.default!=="function"||w.default.definition===void 0){throw new Error(`${x} must default-export a CrewXApp subclass with a static definition`)}const g=c(e,y);const A=E(g)?_(g,"utf8"):void 0;const m=s?ye(w,A):xe(w,o.command,A,o.env);const C=[{path:c(e,$),text:p.text},{path:c(e,s?I:K),text:l.text},...s?[{path:c(e,m.toolsFileName),text:m.toolsText}]:[],{path:c(e,m.fileName),text:m.text}];const M=C.filter(({path:u,text:v})=>!E(u)||_(u,"utf8")!==v);const Q=M.map(({path:u})=>h(process.cwd(),u));if(!t){for(const{path:v,text:z}of C)j(v,z,"utf8");const u=v=>h(process.cwd(),c(e,v));n(s?`Built ${u($)} and ${u(I)}, wrote ${u(X)} and regenerated ${u(m.fileName)}.`:`Built ${h(process.cwd(),c(e,$))} and regenerated ${h(process.cwd(),c(e,m.fileName))}.`)}return{stale:t?Q:[],staleFiles:t?M.map(({path:u})=>h(e,u)):[],files:C.map(({path:u})=>h(e,u)),actions:s?Oe(m.toolsText):je(m.text),mode:s?"tool":"bridge",sdkEntry:P(e)?k:G(),inputs:p.inputs.filter(u=>!u.startsWith(`${L}:`)).map(u=>N(e,u))}}finally{O(d,{force:true});if(!i&&t)O(a,{recursive:true,force:true})}}async function Ce(e,t,n){try{return await R(e,t,n,{contents:pe,sourcefile:"crewx-app-declarations.ts",outfile:"dist/.declarations-build.mjs"})}catch(o){if(o.errors?.some(({text:s})=>/No matching export/u.test(s)&&/generateToolDeclarations/u.test(s))){throw new Error("a server.ts-only App (no bin, no crewx.app.command) needs a newer @crewx/sdk: reinstall the CrewX CLI")}throw o}}function Oe(e){const t=JSON.parse(e)?.actions;return Array.isArray(t)?t.map(n=>n?.actionId).filter(n=>typeof n==="string"):[]}function je(e){const t=JSON.parse(e)?._meta?.[S]?.actions;return Array.isArray(t)?t.map(n=>n?.actionId).filter(n=>typeof n==="string"):[]}async function Te(e,t,{signal:n}={}){const o=new Map;let s=false;let r=false;let a;let i=new Set;const d=async()=>{if(s){r=true;return}s=true;try{const l=await H({appDirectory:e,log:t});i=new Set(l.inputs)}catch(l){t(`Build failed: ${l instanceof Error?l.message:String(l)}`)}finally{s=false;if(!n?.aborted)p();if(r&&!n?.aborted){r=false;void d()}}};const p=()=>{const l=new Set([e,...[...i].map(f=>b(f))]);if(P(e))l.add(q);for(const[f,w]of o){if(!l.has(f)){w.close();o.delete(f)}}for(const f of l){if(o.has(f)||!E(f))continue;o.set(f,re(f,(w,g)=>{if(g===null)return;const A=c(f,g.toString());const m=i.has(A)||A===c(e,"package.json")||f===q&&/\.tsx?$/u.test(A);if(!m)return;clearTimeout(a);a=setTimeout(()=>void d(),le)}))}};await d();t("Watching for changes. Press Ctrl+C to stop.");await new Promise(l=>{if(n===void 0)return;if(n.aborted)l();else n.addEventListener("abort",l,{once:true})});clearTimeout(a);for(const l of o.values())l.close();o.clear()}async function De(e,{cwd:t=process.cwd(),stdout:n=process.stdout,stderr:o=process.stderr}={}){const s=r=>n.write(`${r}
|
|
5
|
+
`);try{const r=Ae(e,t);if(r.watch){await Te(r.appDirectory,s);return 0}const{stale:a}=await H({appDirectory:r.appDirectory,check:r.check,log:s});if(!r.check)return 0;if(a.length>0){o.write(`Stale generated files: ${a.join(", ")}
|
|
6
|
+
`);return 1}s(`${oe(r.appDirectory)} App build artifacts are in sync.`);return 0}catch(r){o.write(`${r instanceof Error?r.message:String(r)}
|
|
7
|
+
`);return 1}}export{k as SDK_APP_SOURCE_ENTRY,H as buildApp,Ae as parseArguments,ge as readAppConfig,xe as renderActionDeclarations,ye as renderToolDeclarations,De as runScript,P as usesSdkSource,Te as watchApp};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crewx/app",
|
|
3
|
-
"version": "0.1.0-rc.
|
|
3
|
+
"version": "0.1.0-rc.131",
|
|
4
4
|
"description": "Task-scoped CLI access to CrewX Apps",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"main": "dist/src/engine.js",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
],
|
|
29
29
|
"dependencies": {
|
|
30
30
|
"esbuild": "0.27.7",
|
|
31
|
-
"@crewx/sdk": "0.9.0-rc.
|
|
31
|
+
"@crewx/sdk": "0.9.0-rc.248"
|
|
32
32
|
},
|
|
33
33
|
"keywords": [
|
|
34
34
|
"crewx",
|