@jals1212/pi-spaces 0.2.0 → 0.3.0

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/CHANGELOG.md CHANGED
@@ -6,6 +6,25 @@ commands and options and a patch version fixes.
6
6
 
7
7
  ## Unreleased
8
8
 
9
+ ## 0.3.0
10
+
11
+ ### Added
12
+
13
+ - Pi knows the space it runs in. Every Pi with the extension sets
14
+ `PI_SPACE` (unset in the base home), `PI_SPACE_HOME` (its agent home) and
15
+ `PI_SPACES_COMMAND` (its configured value, else `pi`), with or without a
16
+ switch, and the system prompt gets a `<pi_space>` section with the same
17
+ facts. `spaceInfo`, `exportSpaceInfo` and `spacePrompt` in the core build
18
+ them.
19
+ - A `pi-spaces` skill, declared in the package, that tells agents what a
20
+ space is, how to read the current space, home and command, and which
21
+ `/space` or `pi-space` action to use.
22
+
23
+ ### Changed
24
+
25
+ - The status line shows the active space as `◆ <name>` instead of
26
+ `space: <name>`, so it stands out among other extensions' statuses.
27
+
9
28
  ## 0.2.0
10
29
 
11
30
  ### Added
package/README.md CHANGED
@@ -65,7 +65,7 @@ A new space starts empty except for a `settings.json` that declares this package
65
65
  | `/space remove <name> [--yes] [--no-backup]` | Deletes a space after asking, then asks whether to keep a backup in `<spaces folder>/.backups` (keeping one is first in the list; dismissing the question keeps the space). `--yes` skips the questions and keeps a backup; it is required without a dialog (print and JSON modes). `--no-backup` deletes without one. |
66
66
  | `pi --space <name>` | Starts Pi directly in a space, keeping the prompt and every other option. |
67
67
 
68
- The status line shows `space: <name>` while a space is active, and nothing in the base home.
68
+ The status line shows `◆ <name>` while a space is active, and nothing in the base home.
69
69
 
70
70
  Choosing an argument such as `switch` from the `/space ` suggestions only fills the line; press Enter again to run it. `/space` and Enter opens the picker in one step.
71
71
 
@@ -99,6 +99,22 @@ Set for the Pi a switch starts:
99
99
  | `PI_SPACE` | The space's name. Unset in the base home. |
100
100
  | `PI_SPACES_BASE_HOME` | The home Pi started with, so `base` and the spaces folder survive switches. |
101
101
 
102
+ Set in every Pi this extension runs in, even one started without a switch, for its tools and the programs they start:
103
+
104
+ | Variable | Meaning |
105
+ | --- | --- |
106
+ | `PI_SPACE` | The active space's name, unset in the base home: the same variable a switch sets. |
107
+ | `PI_SPACE_HOME` | The agent home this Pi runs with: the space's folder, or the base home. |
108
+ | `PI_SPACES_COMMAND` | Kept when it has a value; set to `pi` when it is unset or blank. |
109
+
110
+ ### What the model is told
111
+
112
+ Before each run the system prompt gets a `<pi_space>` section with the same three facts: the space (or the base home), the agent home, and the command, with `<command> --space <name>` and `/space` as the ways to change space. The model knows where it runs without reading the environment. A Pi without structured prompt sections gets no section; the variables are still set.
113
+
114
+ ### The `pi-spaces` skill
115
+
116
+ The package also ships a `pi-spaces` skill (`skills/pi-spaces/SKILL.md`). It tells agents what a space is, how to read the current space, home and command, and which `/space` or `pi-space` action to use or propose for listing, creating, switching and removing spaces. Pi loads it when a task is about spaces; `/skill:pi-spaces` loads it by hand.
117
+
102
118
  ### Exit status
103
119
 
104
120
  | Status | `pi --space` |
package/dist/command.js CHANGED
@@ -157,7 +157,7 @@ function switchTo(name , deps ) {
157
157
  /** Status line text: the active space, or nothing in the base home. */
158
158
  export function statusText(dir , currentHome ) {
159
159
  const active = activeSpace(dir, currentHome);
160
- return active === undefined ? undefined : `space: ${active}`;
160
+ return active === undefined ? undefined : `◆ ${active}`;
161
161
  }
162
162
 
163
163
  export function completeSpaceArgs(prefix , dir ) {
@@ -0,0 +1,63 @@
1
+ // What this Pi process knows about its space: the active space, the agent home and the command
2
+ // that starts Pi. It is exported to the environment (for tools and child processes) and told to the model.
3
+ import { resolve } from "node:path";
4
+ import { activeSpace, } from "./spaces.js";
5
+
6
+
7
+
8
+
9
+
10
+
11
+
12
+
13
+
14
+
15
+ /**
16
+ * Read once, when the extension loads: Pi keeps its agent home for the whole process, and
17
+ * the active space cannot be renamed or removed, so the facts hold until Pi exits.
18
+ */
19
+ export function spaceInfo(dir , agentHome , env ) {
20
+ return {
21
+ space: activeSpace(dir, agentHome),
22
+ home: resolve(agentHome),
23
+ // A blank value counts as unset, so the default is pi, as in the pi-space command.
24
+ command: env.PI_SPACES_COMMAND?.trim() || "pi",
25
+ };
26
+ }
27
+
28
+ /** Sets PI_SPACE (removed in the base home), PI_SPACE_HOME and PI_SPACES_COMMAND. */
29
+ export function exportSpaceInfo(env , info ) {
30
+ if (info.space === undefined) delete env.PI_SPACE;
31
+ else env.PI_SPACE = info.space;
32
+ env.PI_SPACE_HOME = info.home;
33
+ env.PI_SPACES_COMMAND = info.command;
34
+ }
35
+
36
+ /** The system prompt section that tells the model where it runs. */
37
+ export function spacePrompt(info ) {
38
+ const where = info.space === undefined
39
+ ? "This Pi runs in the base home, outside any space."
40
+ : `This Pi runs in the space "${info.space}" (PI_SPACE).`;
41
+ const home = `Agent home: ${info.home} (PI_SPACE_HOME).`;
42
+ const start = `\`${info.command} --space <name>\` starts it in a space`;
43
+ const manage = "`/space` lists, creates, removes and switches spaces";
44
+ const command = `Command that starts Pi: ${info.command} (PI_SPACES_COMMAND); ${start}, and ${manage}.`;
45
+ return [where, home, command].join("\n");
46
+ }
47
+
48
+ /** The part of a run's structured prompt options this package writes. */
49
+
50
+
51
+
52
+
53
+ /**
54
+ * Sets the <pi_space> section in a run's structured prompt options, creating the section map when
55
+ * it is missing and replacing an earlier pi_space section. Other sections and the rest of the
56
+ * prompt are left alone. Without structured options (an older Pi) nothing is added; the
57
+ * environment still has the facts.
58
+ */
59
+ export function addSpaceSection(options , info ) {
60
+ if (options === undefined) return;
61
+ options.sections ??= {};
62
+ options.sections.pi_space = spacePrompt(info);
63
+ }
@@ -4,6 +4,7 @@ import { dirname, join } from "node:path";
4
4
  import { fileURLToPath } from "node:url";
5
5
  import { type ExtensionAPI, getAgentDir } from "@earendil-works/pi-coding-agent";
6
6
  import { completeSpaceArgs, runSpaceCommand, statusText } from "../src/command.ts";
7
+ import { addSpaceSection, exportSpaceInfo, spaceInfo } from "../src/context.ts";
7
8
  import { planStartup, relaunchCommand, runNow, scheduleRelaunch } from "../src/relaunch.ts";
8
9
  import { findOwnDeclaration, spacesDir } from "../src/spaces.ts";
9
10
 
@@ -20,6 +21,11 @@ export default function (pi: ExtensionAPI) {
20
21
  }
21
22
  if (plan.kind === "relaunch") process.exit(runNow(plan.command, plan.env));
22
23
 
24
+ // Where this Pi runs: exported for tools and the programs they start, and told to the model before each run.
25
+ const info = spaceInfo(spacesDir(), getAgentDir(), process.env);
26
+ exportSpaceInfo(process.env, info);
27
+ pi.on("before_agent_start", (event) => addSpaceSection(event.systemPromptOptions, info));
28
+
23
29
  pi.registerFlag("space", { description: "Start Pi in this space (see /space list)", type: "string" });
24
30
 
25
31
  pi.on("session_start", (_event, ctx) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jals1212/pi-spaces",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Named spaces for Pi: keep several agent homes (sign-ins, packages, settings) and pick one by name.",
5
5
  "license": "MIT",
6
6
  "author": "jals1212",
@@ -25,12 +25,16 @@
25
25
  "pi": {
26
26
  "extensions": [
27
27
  "./extensions/spaces.ts"
28
+ ],
29
+ "skills": [
30
+ "./skills"
28
31
  ]
29
32
  },
30
33
  "files": [
31
34
  "dist",
32
35
  "src",
33
36
  "extensions",
37
+ "skills",
34
38
  "README.md",
35
39
  "CHANGELOG.md",
36
40
  "LICENSE"
@@ -41,7 +45,10 @@
41
45
  "scripts": {
42
46
  "build": "node --disable-warning=ExperimentalWarning scripts/build.mjs",
43
47
  "prepack": "npm run build",
44
- "test": "node scripts/link-self.mjs && npm run build && node --test 'tests/*.test.ts' && npm test --prefix cli"
48
+ "test": "node scripts/link-self.mjs && npm run build && node --test \"tests/*.test.ts\" && npm test --prefix cli"
49
+ },
50
+ "devDependencies": {
51
+ "cross-spawn": "^7.0.6"
45
52
  },
46
53
  "peerDependencies": {
47
54
  "@earendil-works/pi-coding-agent": "*",
@@ -0,0 +1,48 @@
1
+ ---
2
+ name: pi-spaces
3
+ description: "Trigger: pi-spaces, space, spaces, /space, pi-space, pi --space, PI_SPACE, PI_SPACE_HOME, PI_SPACES_COMMAND, which space, agent home, base home. Know which Pi space this session runs in, its home and start command, and how spaces are listed, created, switched and removed."
4
+ license: MIT
5
+ metadata:
6
+ author: jals1212
7
+ version: "1.0"
8
+ ---
9
+
10
+ ## Activation Contract
11
+
12
+ Use when a task depends on which Pi agent home this session uses (settings, sign-ins, installed packages, sessions), or when the user asks about spaces. A space is a separate Pi agent home kept in a folder of the spaces folder; the base home is the one Pi started with. Pi reads its home once at startup, so one process stays in one space.
13
+
14
+ ## Hard Rules
15
+
16
+ - Read where you run from the `<pi_space>` system prompt section; in a shell, from `PI_SPACE` (unset in the base home), `PI_SPACE_HOME` (this Pi's agent home) and `PI_SPACES_COMMAND` (the program that starts Pi, default `pi`). Never guess from `~/.pi/agent`.
17
+ - Files of this session's home (`settings.json`, `extensions/`, `sessions/`, installed packages) live under `PI_SPACE_HOME`; `pi install` and settings changes apply to this space only.
18
+ - Never read `auth.json` or other credential files in any home.
19
+ - You cannot move this session to another space. Ask the user to run `/space switch <name>` (interactive terminal only; Pi restarts), or start another Pi with `$PI_SPACES_COMMAND --space <name>`.
20
+ - Never set `PI_CODING_AGENT_DIR` yourself, and never create, rename or delete space folders with file commands; use `/space` or `pi-space`.
21
+ - Removing a space deletes everything in it: only on the user's explicit request, and keep the backup unless they say otherwise.
22
+
23
+ ## Decision Gates
24
+
25
+ | Need | Action |
26
+ | --- | --- |
27
+ | Which space, home, command | `<pi_space>` section, or `echo "$PI_SPACE|$PI_SPACE_HOME|$PI_SPACES_COMMAND"` |
28
+ | List spaces from a shell | `pi-space list` (optional `@jals1212/pi-spaces-cli`) |
29
+ | Spaces folder | `PI_SPACES_DIR`, else `spaces` next to the base home (`PI_SPACES_BASE_HOME`, else this home) |
30
+ | Create a space | `pi-space --new <name>`, or ask the user for `/space new <name>` |
31
+ | Work in another space | User runs `/space switch <name>`, or `$PI_SPACES_COMMAND --space <name> "<prompt>"` |
32
+ | Remove a space | User request only: `pi-space --remove <name> --yes` keeps a backup in `.backups` |
33
+
34
+ Names: lowercase letters, digits, `-` and `_`, starting with a letter or digit, at most 64 characters; `base` is reserved for the base home.
35
+
36
+ ## Execution Steps
37
+
38
+ 1. Read the `<pi_space>` section or the three variables before touching any home file.
39
+ 2. Pick the action from Decision Gates; prefer telling the user the `/space` command when it needs the interactive terminal.
40
+ 3. Report the space and home you acted on.
41
+
42
+ ## Output Contract
43
+
44
+ State the space (or "base home"), the agent home path, and the command or `/space` action used or proposed.
45
+
46
+ ## References
47
+
48
+ - `../../README.md` — commands, environment variables and how switching works.
package/src/command.ts CHANGED
@@ -157,7 +157,7 @@ function switchTo(name: string, deps: SpaceCommandDeps): void {
157
157
  /** Status line text: the active space, or nothing in the base home. */
158
158
  export function statusText(dir: string, currentHome: string): string | undefined {
159
159
  const active = activeSpace(dir, currentHome);
160
- return active === undefined ? undefined : `space: ${active}`;
160
+ return active === undefined ? undefined : `◆ ${active}`;
161
161
  }
162
162
 
163
163
  export function completeSpaceArgs(prefix: string, dir: string): { value: string; label: string }[] | null {
package/src/context.ts ADDED
@@ -0,0 +1,63 @@
1
+ // What this Pi process knows about its space: the active space, the agent home and the command
2
+ // that starts Pi. It is exported to the environment (for tools and child processes) and told to the model.
3
+ import { resolve } from "node:path";
4
+ import { activeSpace, type Env } from "./spaces.ts";
5
+
6
+ export interface SpaceInfo {
7
+ /** The active space, or undefined in the base home. */
8
+ space: string | undefined;
9
+ /** The agent home this Pi runs with. */
10
+ home: string;
11
+ /** PI_SPACES_COMMAND, the program that starts Pi (default pi). */
12
+ command: string;
13
+ }
14
+
15
+ /**
16
+ * Read once, when the extension loads: Pi keeps its agent home for the whole process, and
17
+ * the active space cannot be renamed or removed, so the facts hold until Pi exits.
18
+ */
19
+ export function spaceInfo(dir: string, agentHome: string, env: Env): SpaceInfo {
20
+ return {
21
+ space: activeSpace(dir, agentHome),
22
+ home: resolve(agentHome),
23
+ // A blank value counts as unset, so the default is pi, as in the pi-space command.
24
+ command: env.PI_SPACES_COMMAND?.trim() || "pi",
25
+ };
26
+ }
27
+
28
+ /** Sets PI_SPACE (removed in the base home), PI_SPACE_HOME and PI_SPACES_COMMAND. */
29
+ export function exportSpaceInfo(env: Env, info: SpaceInfo): void {
30
+ if (info.space === undefined) delete env.PI_SPACE;
31
+ else env.PI_SPACE = info.space;
32
+ env.PI_SPACE_HOME = info.home;
33
+ env.PI_SPACES_COMMAND = info.command;
34
+ }
35
+
36
+ /** The system prompt section that tells the model where it runs. */
37
+ export function spacePrompt(info: SpaceInfo): string {
38
+ const where = info.space === undefined
39
+ ? "This Pi runs in the base home, outside any space."
40
+ : `This Pi runs in the space "${info.space}" (PI_SPACE).`;
41
+ const home = `Agent home: ${info.home} (PI_SPACE_HOME).`;
42
+ const start = `\`${info.command} --space <name>\` starts it in a space`;
43
+ const manage = "`/space` lists, creates, removes and switches spaces";
44
+ const command = `Command that starts Pi: ${info.command} (PI_SPACES_COMMAND); ${start}, and ${manage}.`;
45
+ return [where, home, command].join("\n");
46
+ }
47
+
48
+ /** The part of a run's structured prompt options this package writes. */
49
+ export interface PromptSections {
50
+ sections?: Record<string, string>;
51
+ }
52
+
53
+ /**
54
+ * Sets the <pi_space> section in a run's structured prompt options, creating the section map when
55
+ * it is missing and replacing an earlier pi_space section. Other sections and the rest of the
56
+ * prompt are left alone. Without structured options (an older Pi) nothing is added; the
57
+ * environment still has the facts.
58
+ */
59
+ export function addSpaceSection(options: PromptSections | undefined, info: SpaceInfo): void {
60
+ if (options === undefined) return;
61
+ options.sections ??= {};
62
+ options.sections.pi_space = spacePrompt(info);
63
+ }