@jmcombs/pi-1password 2.3.1 → 2.3.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/bash-env.ts ADDED
@@ -0,0 +1,78 @@
1
+ /**
2
+ * Shell env merge + fail-closed user_bash operations wrapper.
3
+ *
4
+ * `executeBashWithOperations` does not pass `env`. When the caller omits it,
5
+ * rebuild pi's shell base from public `getAgentDir()` (feature-detected) at
6
+ * call time, then overlay 1Password-injected vars. Never return an invalid
7
+ * `{ operations }` / `{ result }` object; `undefined` means continue.
8
+ */
9
+
10
+ import { delimiter, join } from "node:path";
11
+ import * as piRuntime from "@earendil-works/pi-coding-agent";
12
+
13
+ export function mergeShellEnv(
14
+ base: NodeJS.ProcessEnv,
15
+ overlay: NodeJS.ProcessEnv,
16
+ ): NodeJS.ProcessEnv {
17
+ return { ...base, ...overlay };
18
+ }
19
+
20
+ function rebuildShellEnv(): NodeJS.ProcessEnv {
21
+ const base: NodeJS.ProcessEnv = { ...process.env };
22
+ if (typeof piRuntime.getAgentDir !== "function") {
23
+ return base;
24
+ }
25
+ const binDir = join(piRuntime.getAgentDir(), "bin");
26
+ const pathKey = Object.keys(base).find((key) => key.toLowerCase() === "path") ?? "PATH";
27
+ const currentPath = base[pathKey] ?? "";
28
+ const pathEntries = currentPath.split(delimiter).filter(Boolean);
29
+ if (!pathEntries.includes(binDir)) {
30
+ base[pathKey] = [binDir, currentPath].filter(Boolean).join(delimiter);
31
+ }
32
+ return base;
33
+ }
34
+
35
+ type ExecOptions = {
36
+ onData: (data: Buffer) => void;
37
+ signal?: AbortSignal;
38
+ timeout?: number;
39
+ env?: NodeJS.ProcessEnv;
40
+ };
41
+
42
+ type ExecFn = (
43
+ command: string,
44
+ cwd: string,
45
+ options: ExecOptions,
46
+ ) => Promise<{ exitCode: number | null }>;
47
+
48
+ export type UserBashOperationsResult = {
49
+ operations: { exec: ExecFn };
50
+ };
51
+
52
+ export function userBashOperationsResult(
53
+ createLocal: () => unknown,
54
+ overlay: () => NodeJS.ProcessEnv,
55
+ ): UserBashOperationsResult | undefined {
56
+ try {
57
+ const created: unknown = createLocal();
58
+ if (typeof created !== "object" || created === null) {
59
+ return undefined;
60
+ }
61
+ if (!("exec" in created) || typeof created.exec !== "function") {
62
+ return undefined;
63
+ }
64
+ const innerExec = created.exec as ExecFn;
65
+ const exec: ExecFn = async (command, cwd, options) => {
66
+ const base = options.env === undefined ? rebuildShellEnv() : options.env;
67
+ return innerExec(command, cwd, {
68
+ onData: options.onData,
69
+ signal: options.signal,
70
+ timeout: options.timeout,
71
+ env: mergeShellEnv(base, overlay()),
72
+ });
73
+ };
74
+ return { operations: { exec } };
75
+ } catch {
76
+ return undefined;
77
+ }
78
+ }
package/index.ts CHANGED
@@ -19,6 +19,7 @@ import { homedir } from "node:os";
19
19
  import { dirname, join } from "node:path";
20
20
  import { fileURLToPath } from "node:url";
21
21
  import { promisify } from "node:util";
22
+ import type { JsonValue } from "@earendil-works/pi-ai";
22
23
  import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
23
24
  // `createLocalBashOperations` is NOT on oh-my-pi's shim. Accessing it through a
24
25
  // namespace import makes a missing member `undefined` at runtime rather than a
@@ -30,7 +31,9 @@ import * as piRuntime from "@earendil-works/pi-coding-agent";
30
31
  import { createBashTool, getAgentDir } from "@earendil-works/pi-coding-agent";
31
32
  import { type Static, Type } from "typebox";
32
33
 
34
+ import { mergeShellEnv, userBashOperationsResult } from "./bash-env.js";
33
35
  import type { UiContext } from "./credential-api.js";
36
+ import { opAccountFromUnknown } from "./op-account.js";
34
37
  import {
35
38
  confirmInBorderedPopup,
36
39
  inputInBorderedPopup,
@@ -70,7 +73,7 @@ interface OpStatus {
70
73
  * Determined passively: no unlock, no Touch ID prompt.
71
74
  */
72
75
  configured: boolean;
73
- account: Record<string, unknown> | null;
76
+ account: ReturnType<typeof opAccountFromUnknown>;
74
77
  }
75
78
 
76
79
  interface PluginInspection {
@@ -152,13 +155,13 @@ export async function getOpStatus(): Promise<OpStatus> {
152
155
  // signedIn — DIAGNOSTIC ONLY (see the OpStatus.signedIn doc). A
153
156
  // non-zero `op whoami` is expected under app-integration and is NOT a gate.
154
157
  let signedIn = false;
155
- let account: Record<string, unknown> | null = null;
158
+ let account: ReturnType<typeof opAccountFromUnknown> = null;
156
159
  try {
157
160
  const { stdout: whoamiOut } = await execAsync("op whoami --format json", {
158
161
  encoding: "utf8",
159
162
  timeout: 5000,
160
163
  });
161
- account = JSON.parse(whoamiOut) as Record<string, unknown>;
164
+ account = opAccountFromUnknown(JSON.parse(whoamiOut));
162
165
  signedIn = true;
163
166
  } catch {
164
167
  signedIn = false;
@@ -212,13 +215,9 @@ function formatOpStatus(status: OpStatus): string {
212
215
  if (!status.configured) {
213
216
  return `op ${status.version ?? "unknown"} is installed but no 1Password account is configured. Run 'op signin', or set OP_SERVICE_ACCOUNT_TOKEN (or OP_CONNECT_HOST + OP_CONNECT_TOKEN).`;
214
217
  }
215
- const acct = status.account ?? {};
216
- const name =
217
- (acct.name as string | undefined) ??
218
- (acct.email as string | undefined) ??
219
- (acct.account_uuid as string | undefined) ??
220
- null;
221
- const url = (acct.url as string | undefined) ?? null;
218
+ const acct = status.account;
219
+ const name = acct?.name ?? acct?.email ?? acct?.account_uuid ?? null;
220
+ const url = acct?.url ?? null;
222
221
  if (status.signedIn && name) {
223
222
  return `op ${status.version ?? "unknown"} — signed in as ${name}${url ? ` (${url})` : ""}`;
224
223
  }
@@ -789,7 +788,7 @@ export default async function (pi: ExtensionAPI): Promise<void> {
789
788
  spawnHook: ({ command, cwd: hookCwd, env }) => ({
790
789
  command,
791
790
  cwd: hookCwd,
792
- env: { ...env, ...currentShellEnv },
791
+ env: mergeShellEnv(env, currentShellEnv),
793
792
  }),
794
793
  });
795
794
  pi.registerTool(injectedBash);
@@ -798,7 +797,12 @@ export default async function (pi: ExtensionAPI): Promise<void> {
798
797
  // exposes `createLocalBashOperations` (real pi does; oh-my-pi's compat shim does
799
798
  // not). Absent it, we skip the hook rather than crash the module load.
800
799
  if (typeof piRuntime.createLocalBashOperations === "function") {
801
- pi.on("user_bash", () => ({ operations: piRuntime.createLocalBashOperations() }));
800
+ pi.on("user_bash", () =>
801
+ userBashOperationsResult(
802
+ () => piRuntime.createLocalBashOperations(),
803
+ () => currentShellEnv,
804
+ ),
805
+ );
802
806
  } else if (process.env.HEADROOM_DEBUG) {
803
807
  console.error(
804
808
  "[1password] createLocalBashOperations unavailable on this runtime; user `!` 1P injection disabled (transparent agent-bash injection still active).",
@@ -849,10 +853,30 @@ export default async function (pi: ExtensionAPI): Promise<void> {
849
853
  return {
850
854
  report: report.trim(),
851
855
  details: {
852
- opStatus: status,
853
- pluginInspections: inspections,
856
+ opStatus: {
857
+ available: status.available,
858
+ version: status.version,
859
+ signedIn: status.signedIn,
860
+ configured: status.configured,
861
+ account:
862
+ status.account === null
863
+ ? null
864
+ : {
865
+ ...(status.account.name !== undefined ? { name: status.account.name } : {}),
866
+ ...(status.account.email !== undefined ? { email: status.account.email } : {}),
867
+ ...(status.account.account_uuid !== undefined
868
+ ? { account_uuid: status.account.account_uuid }
869
+ : {}),
870
+ ...(status.account.url !== undefined ? { url: status.account.url } : {}),
871
+ },
872
+ },
873
+ pluginInspections: inspections.map((item) => ({
874
+ plugin: item.plugin,
875
+ ...(item.output !== undefined ? { output: item.output } : {}),
876
+ ...(item.error !== undefined ? { error: item.error } : {}),
877
+ })),
854
878
  injectedShellEnvNames: injectedNames,
855
- },
879
+ } satisfies JsonValue,
856
880
  };
857
881
  }
858
882
 
package/op-account.ts ADDED
@@ -0,0 +1,21 @@
1
+ /**
2
+ * JSON-compatible 1Password account fields from `op whoami --format json`.
3
+ * Copies only the four string keys; every other property is dropped.
4
+ */
5
+
6
+ export function opAccountFromUnknown(
7
+ value: unknown,
8
+ ): { name?: string; email?: string; account_uuid?: string; url?: string } | null {
9
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
10
+ return null;
11
+ }
12
+
13
+ const account: { name?: string; email?: string; account_uuid?: string; url?: string } = {};
14
+ if ("name" in value && typeof value.name === "string") account.name = value.name;
15
+ if ("email" in value && typeof value.email === "string") account.email = value.email;
16
+ if ("account_uuid" in value && typeof value.account_uuid === "string") {
17
+ account.account_uuid = value.account_uuid;
18
+ }
19
+ if ("url" in value && typeof value.url === "string") account.url = value.url;
20
+ return account;
21
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jmcombs/pi-1password",
3
- "version": "2.3.1",
3
+ "version": "2.3.2",
4
4
  "description": "1Password integration for the Pi coding agent. Read secrets and run commands with 1Password credential injection via the op CLI.",
5
5
  "homepage": "https://github.com/jmcombs/pi-extensions/tree/main/packages/1password",
6
6
  "repository": {
@@ -18,7 +18,9 @@
18
18
  "types": "./index.ts",
19
19
  "files": [
20
20
  "index.ts",
21
+ "bash-env.ts",
21
22
  "credential-api.ts",
23
+ "op-account.ts",
22
24
  "data/",
23
25
  "ui/",
24
26
  "README.md",
@@ -47,8 +49,8 @@
47
49
  "typebox": "*"
48
50
  },
49
51
  "devDependencies": {
50
- "@earendil-works/pi-coding-agent": "^0.87.1",
52
+ "@earendil-works/pi-coding-agent": "^0.99.1",
51
53
  "@earendil-works/pi-tui": "*",
52
- "typebox": "^1.3.27"
54
+ "typebox": "^1.3.34"
53
55
  }
54
56
  }