@danypops/pi-packed 0.22.0 → 0.22.2
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/client.js +1 -1
- package/extension/src/profile.ts +1 -34
- package/package.json +7 -7
package/dist/client.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{spawn as
|
|
1
|
+
import{spawn as N}from"node:child_process";import{accessSync as f,constants as I,readFileSync as T}from"node:fs";import{delimiter as S,dirname as y,join as U,resolve as k}from"node:path";import{fileURLToPath as P}from"node:url";class q{baseUrl;token;opsPath;transport;label;constructor(z,G,J){this.baseUrl=z;this.token=G;this.label=J.label,this.opsPath=J.opsPath??"/api/v1/ops",this.transport=J.transport??fetch}authedRequest(z,G={}){return this.transport(new Request(`${this.baseUrl}${z}`,{...G,headers:{...G.headers,authorization:`Bearer ${this.token}`}}))}async call(z,G){let J=await this.authedRequest(this.opsPath,{method:"POST",headers:{"content-type":"application/json"},body:JSON.stringify({op:z,input:G})}),Z=await J.json();if(!J.ok)throw Error(Z.error??`${this.label} operation failed with HTTP ${J.status}`);return Z.result}async operations(){let z=await this.authedRequest(this.opsPath),G=await z.json();if(!z.ok)throw Error(G.error??`${this.label} discovery failed with HTTP ${z.status}`);return G.operations??[]}async ready(){let z=await this.authedRequest("/ready");if(z.status===503)return!1;if(!z.ok)throw Error(`${this.label} readiness check failed with HTTP ${z.status}`);return!0}async health(){let z=await this.authedRequest("/health"),G=await z.json();if(!z.ok||G.ok!==!0||typeof G.version!=="string")throw Error(G.error??`${this.label} health check failed`);return{ok:!0,version:G.version}}}import{chmodSync as i,existsSync as p,mkdirSync as o,readFileSync as _,renameSync as n,rmSync as t,writeFileSync as a}from"node:fs";import{homedir as R,tmpdir as b}from"node:os";import{dirname as zz,join as W,win32 as Y}from"node:path";var L="127.0.0.1";function B(z,G={}){let J=G.platform??process.platform,Z=G.home??R();if(J==="darwin")return E(z,Z);if(J==="win32")return g(z,G.env??process.env,Z);return M(z,G,Z)}function M(z,G,J){let Z=G.env??process.env,$=G.uid??process.getuid?.()??0,K=Z.XDG_DATA_HOME??W(J,".local","share"),Q=Z.XDG_STATE_HOME??W(J,".local","state"),C=Z.XDG_RUNTIME_DIR??W("/run","user",String($)),D=Z.XDG_CONFIG_HOME??W(J,".config");return{database:W(K,z.stateDirectoryName,z.databaseFilename),token:W(Q,z.stateDirectoryName,z.tokenFilename),handle:W(C,z.stateDirectoryName,z.handleFilename),serviceDescriptor:W(D,"systemd","user",z.systemdUnitName)}}function E(z,G){let J=W(G,"Library"),Z=W(J,"Application Support",z.stateDirectoryName);return{database:W(Z,z.databaseFilename),token:W(Z,z.tokenFilename),handle:W(b(),z.stateDirectoryName,z.handleFilename),serviceDescriptor:W(Z,z.systemdUnitName)}}function g(z,G,J){let Z=G.LOCALAPPDATA??Y.join(J,"AppData","Local"),$=G.APPDATA??Y.join(J,"AppData","Roaming"),K=Y.join(Z,z.stateDirectoryName,"Data");return{database:Y.join(K,z.databaseFilename),token:Y.join(K,z.tokenFilename),handle:Y.join(Z,"Temp",z.stateDirectoryName,z.handleFilename),serviceDescriptor:Y.join($,z.stateDirectoryName,"Config",z.systemdUnitName)}}function X(z){try{let G=JSON.parse(_(z,"utf8"));if(G.host!==L||!Number.isInteger(G.port)||G.port<1||G.port>65535||!Number.isInteger(G.pid)||G.tokenPath!==void 0&&typeof G.tokenPath!=="string")return null;return G}catch{return null}}import{execFileSync as A}from"node:child_process";import{fileURLToPath as w}from"node:url";function H(z,G){let J=G.runCommand(process.execPath,[G.armadaCliPath,"status","--json"]);if(!J.ok)return!1;try{return JSON.parse(J.output).vehicles?.some(($)=>$.name===z)??!1}catch{return!1}}function O(){return{armadaCliPath:w(import.meta.resolve("@danypops/armada/cli")),runCommand:(z,G,J)=>{try{return{ok:!0,output:A(z,G,{encoding:"utf8",input:J,stdio:[J===void 0?"ignore":"pipe","pipe","pipe"]})}}catch(Z){let $=Z;return{ok:!1,output:($.stdout??"")+($.stderr??$.message)}}}}}class V extends Error{notADaemon;constructor(z,G){super(z);this.notADaemon=G;this.name="InstallServiceError"}}var u="pi-packed",F="pi-packed.service";function j(z={}){let G=z.env??process.env;if(G.PI_PACKED_HOME){let Z=k(G.PI_PACKED_HOME);return{token:`${Z}/token`,handle:`${Z}/handle.json`,serviceDescriptor:`${Z}/${F}`}}let J=B({stateDirectoryName:"pi-packed",databaseFilename:"packages.db",tokenFilename:"token",handleFilename:"handle.json",systemdUnitName:F},z);return{token:J.token,handle:J.handle,serviceDescriptor:J.serviceDescriptor}}function Bz(z=j()){let G=X(z.handle);if(!G)return;let J;try{J=T(z.token,"utf8").trim()}catch{return}if(!/^[a-f0-9]{64}$/.test(J))return;return{baseUrl:`http://${G.host}:${G.port}`,token:J}}function v(){return H(u,O())}function h(z,G=30000){return(J)=>z(new Request(J,{signal:AbortSignal.any([J.signal,AbortSignal.timeout(G)])}))}class x{rpc;constructor(z,G,J=fetch){this.rpc=new q(z,G,{label:"Packed",transport:h(J)})}call(z,G){return this.rpc.call(z,G)}health(){return this.rpc.health()}search(z,G,J=!1){return this.call("package.search",{query:z,limit:G,offline:J})}info(z){return this.call("package.info",{name:z})}installed(){return this.call("package.installed",{})}async updates(){return(await this.call("package.updates",{})).updates}security(){return this.call("package.security.get",{})}setMutationApproval(z,G=!1){return this.call("package.security.set",{mutationApproval:z,approved:G})}async install(z,G=!1){let J=await this.call("package.install",{source:z,approved:G});if(!J.ok)throw Error(J.output);return J.output}async installService(z,G=!1){let J=await this.call("package.install_service",{source:z,approved:G});if(!J.ok)throw new V(J.output,J.notADaemon);return{output:J.output,spec:J.spec}}async remove(z,G=!1){let J=await this.call("package.remove",{name:z,approved:G});if(!J.ok)throw Error(J.output);return J.output}async update(z,G=!1,J){let Z=await this.call("package.update",{source:z,approved:G,target:J});if(!Z.ok)throw Error(Z.output);return{output:Z.output,reloadRequired:Z.reloadRequired??!0,alreadyUpToDate:Z.alreadyUpToDate??!1,pinned:Z.pinned??!1,previousVersion:Z.previousVersion,currentVersion:Z.currentVersion,outOfRangeUpdateAvailable:Z.outOfRangeUpdateAvailable,pinnedSourceRequiresTarget:Z.pinnedSourceRequiresTarget,replaced:Z.replaced,before:Z.before,after:Z.after,rollback:Z.rollback}}setupPlan(z,G=!1){return this.call("setup.plan",{manifestPath:z,prune:G})}setupApply(z,G=!1,J=!1){return this.call("setup.apply",{manifestPath:z,approved:G,prune:J})}listResources(z){return this.call("resources.list",{projectRoot:z})}async toggleResource(z,G,J,Z,$,K=!1){let Q=await this.call("resources.toggle",{source:z,field:G,path:J,enabled:Z,projectRoot:$,approved:K});if(!Q.ok)throw Error(Q.output);return Q.output}piStatus(){return this.call("pi.status",{})}}async function m(z=j(),G=fetch){let J=X(z.handle);if(!J)throw Error("Packed daemon is not running");let Z;try{Z=T(z.token,"utf8").trim()}catch{throw Error("Packed daemon token is unavailable")}if(!/^[a-f0-9]{64}$/.test(Z))throw Error("Packed daemon token is invalid");let $=new x(`http://${J.host}:${J.port}`,Z,G);return await $.health(),$}async function d(z){try{return await z.connect()}catch{}let G=z.retryAttempts??30,J=z.retryDelayMs??100,Z=z.isServiceInstalled();if(!Z)z.spawn();for(let K=0;K<G;K++){await z.sleep(J);try{return await z.connect()}catch{}}let $=G*J/1000;throw Error(Z?`The managed Packed service is registered but unreachable after ${$} seconds. Its supervisor may have stopped it or it may be failing; restart the managed service and inspect its status and logs. Packed will not auto-spawn a competing process.`:`Packed daemon did not become ready within ${$} seconds`)}function c(z=process.env){if(z.PI_PACKED_PI_BIN||z.PI_BIN)return;let G=z.PATH;if(!G)return;let J=process.platform==="win32"?["pi.exe","pi.cmd","pi.bat","pi"]:["pi"];for(let Z of G.split(S)){if(!Z)continue;for(let $ of J){let K=U(Z,$);try{return f(K,I.X_OK),K}catch{}}}return}async function Hz(z=j(),G=fetch){return d({connect:()=>m(z,G),isServiceInstalled:()=>v(),spawn:()=>{let J=process.env.PI_PACKED_BIN,Z=J??process.env.PI_PACKED_BUN??"bun",$=J?["serve"]:[U(y(P(import.meta.url)),"../service/src/cli/cli.ts"),"serve"],K=c(),Q=N(Z,$,{detached:!0,stdio:"ignore",env:{...process.env,DAEMON_KIT_LAUNCH_PROVENANCE:"auto-spawn",...K?{PI_BIN:K}:{}}});Q.once("error",()=>{}),Q.unref()},sleep:(J)=>new Promise((Z)=>setTimeout(Z,J))})}export{Bz as resolveVehicleClientTarget,c as resolvePiBinForSpawn,j as resolvePackedClientPaths,Hz as ensurePackedClient,d as ensureClient,m as connectPackedClient,x as PackedClient,V as InstallServiceError};
|
package/extension/src/profile.ts
CHANGED
|
@@ -96,22 +96,6 @@ export function loadProfiles(agentDir: string, cwd: string, projectTrusted: bool
|
|
|
96
96
|
return { ...globalProfiles, ...readProfileFile(join(cwd, CONFIG_DIR_NAME, PROFILE_FILE)) };
|
|
97
97
|
}
|
|
98
98
|
|
|
99
|
-
function loadDefaultProfile(cwd: string, projectTrusted: boolean): string | undefined {
|
|
100
|
-
if (!projectTrusted) return undefined;
|
|
101
|
-
try {
|
|
102
|
-
const path = join(cwd, "pi-setup.json");
|
|
103
|
-
if (!existsSync(path)) return undefined;
|
|
104
|
-
const stat = lstatSync(path);
|
|
105
|
-
if (!stat.isFile() || stat.isSymbolicLink() || stat.size > 1024 * 1024) return undefined;
|
|
106
|
-
const value = JSON.parse(readFileSync(path, "utf8")) as { schemaVersion?: unknown; defaultProfile?: unknown };
|
|
107
|
-
return value.schemaVersion === 1 && typeof value.defaultProfile === "string" && PROFILE_NAME.test(value.defaultProfile)
|
|
108
|
-
? value.defaultProfile
|
|
109
|
-
: undefined;
|
|
110
|
-
} catch {
|
|
111
|
-
return undefined;
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
99
|
function loadLastProfile(agentDir: string): string | undefined {
|
|
116
100
|
try {
|
|
117
101
|
const path = join(agentDir, LAST_PROFILE_FILE);
|
|
@@ -299,7 +283,7 @@ export function registerProfiles(pi: ExtensionAPI): void {
|
|
|
299
283
|
pi.on("before_agent_start", (event) =>
|
|
300
284
|
activeProfile?.instructions ? { systemPrompt: `${event.systemPrompt}\n\n${activeProfile.instructions}` } : undefined,
|
|
301
285
|
);
|
|
302
|
-
pi.on("session_start", async (
|
|
286
|
+
pi.on("session_start", async (_event, ctx) => {
|
|
303
287
|
try {
|
|
304
288
|
profiles = loadProfiles(agentDir, ctx.cwd, ctx.isProjectTrusted());
|
|
305
289
|
} catch (error) {
|
|
@@ -317,15 +301,6 @@ export function registerProfiles(pi: ExtensionAPI): void {
|
|
|
317
301
|
ctx.ui.notify(`Profile "${flag}" activated`, "info");
|
|
318
302
|
return;
|
|
319
303
|
}
|
|
320
|
-
const defaultProfile = loadDefaultProfile(ctx.cwd, ctx.isProjectTrusted());
|
|
321
|
-
if ((event.reason === "reload" || event.reason === "new") && defaultProfile) {
|
|
322
|
-
if (profiles[defaultProfile]) await apply(defaultProfile, profiles[defaultProfile]!, ctx, false);
|
|
323
|
-
else {
|
|
324
|
-
ctx.ui.notify(`Default profile "${defaultProfile}" is not defined`, "warning");
|
|
325
|
-
setStatus(ctx);
|
|
326
|
-
}
|
|
327
|
-
return;
|
|
328
|
-
}
|
|
329
304
|
const state = [...ctx.sessionManager.getEntries()]
|
|
330
305
|
.reverse()
|
|
331
306
|
.find(
|
|
@@ -340,14 +315,6 @@ export function registerProfiles(pi: ExtensionAPI): void {
|
|
|
340
315
|
setStatus(ctx);
|
|
341
316
|
return;
|
|
342
317
|
}
|
|
343
|
-
if (defaultProfile) {
|
|
344
|
-
if (profiles[defaultProfile]) await apply(defaultProfile, profiles[defaultProfile]!, ctx, false);
|
|
345
|
-
else {
|
|
346
|
-
ctx.ui.notify(`Default profile "${defaultProfile}" is not defined`, "warning");
|
|
347
|
-
setStatus(ctx);
|
|
348
|
-
}
|
|
349
|
-
return;
|
|
350
|
-
}
|
|
351
318
|
const last = loadLastProfile(agentDir);
|
|
352
319
|
if (last && profiles[last]) {
|
|
353
320
|
await apply(last, profiles[last]!, ctx, false);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@danypops/pi-packed",
|
|
3
|
-
"version": "0.22.
|
|
3
|
+
"version": "0.22.2",
|
|
4
4
|
"description": "Pi package lifecycle, validation, daemon, tools, profiles, and TUI",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -27,13 +27,13 @@
|
|
|
27
27
|
"typecheck": "bunx tsc --noEmit"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@danypops/armada": "^0.3
|
|
30
|
+
"@danypops/armada": "^0.4.3",
|
|
31
31
|
"@danypops/packed": "^0.7.0",
|
|
32
|
-
"@danypops/pi-extension-harness": "^0.
|
|
33
|
-
"@danypops/vehicle-client": "^0.
|
|
34
|
-
"@danypops/vehicle-client-pi": "^0.
|
|
35
|
-
"@danypops/vehicle-core": "^0.
|
|
36
|
-
"@danypops/vehicle-server": "^0.
|
|
32
|
+
"@danypops/pi-extension-harness": "^0.7.0",
|
|
33
|
+
"@danypops/vehicle-client": "^0.8.1",
|
|
34
|
+
"@danypops/vehicle-client-pi": "^0.31.2",
|
|
35
|
+
"@danypops/vehicle-core": "^0.15.0",
|
|
36
|
+
"@danypops/vehicle-server": "^0.22.0",
|
|
37
37
|
"jiti": "^2.7.0",
|
|
38
38
|
"malevich-tui-components": "^0.21.1",
|
|
39
39
|
"publint": "0.3.22",
|