@jmcombs/pi-1password 2.5.0 → 2.6.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/index.ts +37 -134
- package/package.json +4 -1
package/index.ts
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
*/
|
|
15
15
|
|
|
16
16
|
import { exec } from "node:child_process";
|
|
17
|
-
import {
|
|
17
|
+
import { readFile } from "node:fs/promises";
|
|
18
18
|
import { homedir } from "node:os";
|
|
19
19
|
import { dirname, join } from "node:path";
|
|
20
20
|
import { fileURLToPath } from "node:url";
|
|
@@ -26,9 +26,10 @@ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
|
26
26
|
// hard ESM link error that would take this whole module (and every consumer that
|
|
27
27
|
// imports it, e.g. via `resolveSecret`/`onboardSecret`) down on that runtime.
|
|
28
28
|
import * as piRuntime from "@earendil-works/pi-coding-agent";
|
|
29
|
-
// `createBashTool`
|
|
30
|
-
//
|
|
31
|
-
import { createBashTool
|
|
29
|
+
// `createBashTool` is always present on the pi runtime (and on oh-my-pi's
|
|
30
|
+
// legacy-pi compat shim), so it stays as a static named import.
|
|
31
|
+
import { createBashTool } from "@earendil-works/pi-coding-agent";
|
|
32
|
+
import { agentAuthPath, updateAuthJson } from "@jmcombs/pi-agent-files";
|
|
32
33
|
import { type Static, Type } from "typebox";
|
|
33
34
|
|
|
34
35
|
import { mergeShellEnv, userBashOperationsResult } from "./bash-env.js";
|
|
@@ -361,50 +362,32 @@ async function loadCuratedPlugins(): Promise<CuratedPlugin[]> {
|
|
|
361
362
|
}
|
|
362
363
|
|
|
363
364
|
/**
|
|
364
|
-
*
|
|
365
|
-
*
|
|
366
|
-
*
|
|
365
|
+
* Add (or overwrite) a top-level KEY: "!op read 'op://...'" entry in auth.json.
|
|
366
|
+
* The value stays a string. A new auth.json is mode 0600; an existing file keeps
|
|
367
|
+
* its mode. Supports optional overwrite.
|
|
367
368
|
*/
|
|
368
369
|
async function addAuthEntry(
|
|
369
370
|
envVar: string,
|
|
370
371
|
opRef: string,
|
|
371
372
|
options: { overwrite?: boolean } = {},
|
|
372
373
|
): Promise<{ success: boolean; message: string; path: string }> {
|
|
373
|
-
const
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
const raw = await readFile(authPath, "utf8");
|
|
381
|
-
const parsed: unknown = JSON.parse(raw);
|
|
382
|
-
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
383
|
-
for (const [k, v] of Object.entries(parsed as Record<string, unknown>)) {
|
|
384
|
-
existing.set(k, v);
|
|
385
|
-
}
|
|
374
|
+
const authPath = agentAuthPath();
|
|
375
|
+
let skipped = false;
|
|
376
|
+
await updateAuthJson((current) => {
|
|
377
|
+
const existing = current ?? {};
|
|
378
|
+
if (Object.hasOwn(existing, envVar) && !options.overwrite) {
|
|
379
|
+
skipped = true;
|
|
380
|
+
return undefined;
|
|
386
381
|
}
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
const alreadyExists = existing.has(envVar);
|
|
392
|
-
|
|
393
|
-
if (alreadyExists && !options.overwrite) {
|
|
382
|
+
return { ...existing, [envVar]: `!op read '${opRef}'` };
|
|
383
|
+
});
|
|
384
|
+
if (skipped) {
|
|
394
385
|
return {
|
|
395
386
|
success: false,
|
|
396
387
|
message: `Key "${envVar}" already exists in auth.json. Pick a different env var name or remove the old entry first.`,
|
|
397
388
|
path: authPath,
|
|
398
389
|
};
|
|
399
390
|
}
|
|
400
|
-
|
|
401
|
-
// Convention: single quotes around the op:// ref
|
|
402
|
-
existing.set(envVar, `!op read '${opRef}'`);
|
|
403
|
-
|
|
404
|
-
const content = `${JSON.stringify(Object.fromEntries(existing), null, 2)}\n`;
|
|
405
|
-
await writeFile(authPath, content, "utf8");
|
|
406
|
-
await chmod(authPath, 0o600);
|
|
407
|
-
|
|
408
391
|
return { success: true, message: "Entry added.", path: authPath };
|
|
409
392
|
}
|
|
410
393
|
|
|
@@ -412,7 +395,7 @@ async function addAuthEntry(
|
|
|
412
395
|
|
|
413
396
|
/** Absolute path to the agent's auth.json (honors PI_CODING_AGENT_DIR). */
|
|
414
397
|
function getAuthFilePath(): string {
|
|
415
|
-
return
|
|
398
|
+
return agentAuthPath();
|
|
416
399
|
}
|
|
417
400
|
|
|
418
401
|
/**
|
|
@@ -440,95 +423,9 @@ export interface ProviderWriteResult {
|
|
|
440
423
|
readonly alreadyExists?: boolean;
|
|
441
424
|
}
|
|
442
425
|
|
|
443
|
-
/**
|
|
444
|
-
* Acquire an exclusive advisory lock via an `O_EXCL` lockfile next to auth.json.
|
|
445
|
-
* Retries with jittered backoff; force-clears a lock older than the timeout
|
|
446
|
-
* (treated as stale). Returns a release function that removes the lockfile.
|
|
447
|
-
*/
|
|
448
|
-
async function acquireAuthLock(lockPath: string): Promise<() => Promise<void>> {
|
|
449
|
-
const timeoutMs = 5000;
|
|
450
|
-
const start = Date.now();
|
|
451
|
-
for (;;) {
|
|
452
|
-
try {
|
|
453
|
-
// "wx" = O_CREAT | O_EXCL | O_WRONLY — fails if the lockfile already exists.
|
|
454
|
-
const handle = await open(lockPath, "wx");
|
|
455
|
-
await handle.close();
|
|
456
|
-
return async (): Promise<void> => {
|
|
457
|
-
try {
|
|
458
|
-
await unlink(lockPath);
|
|
459
|
-
} catch {
|
|
460
|
-
// Already gone — nothing to release.
|
|
461
|
-
}
|
|
462
|
-
};
|
|
463
|
-
} catch (e) {
|
|
464
|
-
const code = (e as NodeJS.ErrnoException).code;
|
|
465
|
-
if (code !== "EEXIST") throw e;
|
|
466
|
-
if (Date.now() - start > timeoutMs) {
|
|
467
|
-
// Presumed stale (holder crashed); clear it and retry.
|
|
468
|
-
try {
|
|
469
|
-
await unlink(lockPath);
|
|
470
|
-
} catch {
|
|
471
|
-
// Someone else cleared it first.
|
|
472
|
-
}
|
|
473
|
-
}
|
|
474
|
-
await new Promise((resolve) => setTimeout(resolve, 25 + Math.floor(Math.random() * 25)));
|
|
475
|
-
}
|
|
476
|
-
}
|
|
477
|
-
}
|
|
478
|
-
|
|
479
|
-
/**
|
|
480
|
-
* Read-modify-write auth.json under an exclusive lock, then commit atomically via
|
|
481
|
-
* temp-write + rename. `mutator` returns the next object to persist, or `null` to
|
|
482
|
-
* abort with no write. Every intermediate file is chmod 0600.
|
|
483
|
-
*/
|
|
484
|
-
async function mutateAuthFileLocked(
|
|
485
|
-
mutator: (current: Record<string, unknown>) => Record<string, unknown> | null,
|
|
486
|
-
): Promise<{ changed: boolean }> {
|
|
487
|
-
const authDir = getAgentDir();
|
|
488
|
-
const authPath = join(authDir, "auth.json");
|
|
489
|
-
const lockPath = `${authPath}.lock`;
|
|
490
|
-
|
|
491
|
-
await mkdir(authDir, { recursive: true });
|
|
492
|
-
const release = await acquireAuthLock(lockPath);
|
|
493
|
-
try {
|
|
494
|
-
let current: Record<string, unknown> = {};
|
|
495
|
-
try {
|
|
496
|
-
const raw = await readFile(authPath, "utf8");
|
|
497
|
-
const parsed: unknown = JSON.parse(raw);
|
|
498
|
-
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
499
|
-
current = parsed as Record<string, unknown>;
|
|
500
|
-
}
|
|
501
|
-
} catch {
|
|
502
|
-
// Missing or invalid JSON → start fresh.
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
const next = mutator(current);
|
|
506
|
-
if (next === null) return { changed: false };
|
|
507
|
-
|
|
508
|
-
const content = `${JSON.stringify(next, null, 2)}\n`;
|
|
509
|
-
const tmpPath = `${authPath}.tmp.${process.pid}.${Math.random().toString(36).slice(2)}`;
|
|
510
|
-
try {
|
|
511
|
-
await writeFile(tmpPath, content, { encoding: "utf8", mode: 0o600 });
|
|
512
|
-
await chmod(tmpPath, 0o600);
|
|
513
|
-
await rename(tmpPath, authPath);
|
|
514
|
-
await chmod(authPath, 0o600);
|
|
515
|
-
} catch (e) {
|
|
516
|
-
try {
|
|
517
|
-
await unlink(tmpPath);
|
|
518
|
-
} catch {
|
|
519
|
-
// Temp file may not exist — ignore.
|
|
520
|
-
}
|
|
521
|
-
throw e;
|
|
522
|
-
}
|
|
523
|
-
return { changed: true };
|
|
524
|
-
} finally {
|
|
525
|
-
await release();
|
|
526
|
-
}
|
|
527
|
-
}
|
|
528
|
-
|
|
529
426
|
/**
|
|
530
427
|
* Write a provider-shaped entry `{[name]:{type:"api_key",key}}` to auth.json under
|
|
531
|
-
* the lock
|
|
428
|
+
* the shared lock. `key` is either a `!op read '<ref>'` command (vault) or a literal
|
|
532
429
|
* secret (manual). Refuses to clobber an existing key unless `overwrite` is set.
|
|
533
430
|
*/
|
|
534
431
|
export async function writeProviderAuthEntry(
|
|
@@ -537,12 +434,17 @@ export async function writeProviderAuthEntry(
|
|
|
537
434
|
options: { overwrite?: boolean } = {},
|
|
538
435
|
): Promise<ProviderWriteResult> {
|
|
539
436
|
let alreadyExists = false;
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
437
|
+
let skipped = false;
|
|
438
|
+
await updateAuthJson((current) => {
|
|
439
|
+
const existing = current ?? {};
|
|
440
|
+
alreadyExists = Object.hasOwn(existing, name);
|
|
441
|
+
if (alreadyExists && !options.overwrite) {
|
|
442
|
+
skipped = true;
|
|
443
|
+
return undefined;
|
|
444
|
+
}
|
|
445
|
+
return { ...existing, [name]: { type: "api_key", key } };
|
|
544
446
|
});
|
|
545
|
-
if (
|
|
447
|
+
if (skipped) {
|
|
546
448
|
return {
|
|
547
449
|
success: false,
|
|
548
450
|
alreadyExists: true,
|
|
@@ -558,13 +460,14 @@ export async function writeProviderAuthEntry(
|
|
|
558
460
|
*/
|
|
559
461
|
export async function deleteAuthEntry(name: string): Promise<{ ok: boolean }> {
|
|
560
462
|
let existed = false;
|
|
561
|
-
await
|
|
562
|
-
|
|
463
|
+
await updateAuthJson((current) => {
|
|
464
|
+
const existing = current ?? {};
|
|
465
|
+
if (!Object.hasOwn(existing, name)) {
|
|
563
466
|
existed = false;
|
|
564
|
-
return
|
|
467
|
+
return undefined;
|
|
565
468
|
}
|
|
566
469
|
existed = true;
|
|
567
|
-
const next = { ...
|
|
470
|
+
const next = { ...existing };
|
|
568
471
|
delete next[name];
|
|
569
472
|
return next;
|
|
570
473
|
});
|
|
@@ -927,7 +830,7 @@ export default async function (pi: ExtensionAPI): Promise<void> {
|
|
|
927
830
|
description:
|
|
928
831
|
"Guided setup: pick from supported tools or enter a custom op:// reference, write '!op read ...' entry to ~/.pi/agent/auth.json for transparent injection.",
|
|
929
832
|
handler: async (_args, ctx) => {
|
|
930
|
-
const authPath =
|
|
833
|
+
const authPath = agentAuthPath();
|
|
931
834
|
|
|
932
835
|
ctx.ui.notify("1Password Onboard — transparent env injection setup", "info");
|
|
933
836
|
|
|
@@ -1105,7 +1008,7 @@ export default async function (pi: ExtensionAPI): Promise<void> {
|
|
|
1105
1008
|
}
|
|
1106
1009
|
|
|
1107
1010
|
if (writeRes.success) {
|
|
1108
|
-
ctx.ui.notify(`✅ Success! ${finalEnv} added to auth.json
|
|
1011
|
+
ctx.ui.notify(`✅ Success! ${finalEnv} added to auth.json.`, "info");
|
|
1109
1012
|
const doReload = await confirmInBorderedPopup(ctx, {
|
|
1110
1013
|
title: "Activate now?",
|
|
1111
1014
|
message: "Run /reload so the spawn hook starts injecting it this session?",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jmcombs/pi-1password",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.6.0",
|
|
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": {
|
|
@@ -43,6 +43,9 @@
|
|
|
43
43
|
"engines": {
|
|
44
44
|
"node": ">=22.19.0"
|
|
45
45
|
},
|
|
46
|
+
"dependencies": {
|
|
47
|
+
"@jmcombs/pi-agent-files": "^0.0.0 || ^1.0.0"
|
|
48
|
+
},
|
|
46
49
|
"peerDependencies": {
|
|
47
50
|
"@earendil-works/pi-coding-agent": "*",
|
|
48
51
|
"@earendil-works/pi-tui": "*",
|