@hex4c59/pi-ext-config-sync 0.0.0-stage → 0.1.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/src/plan.ts ADDED
@@ -0,0 +1,123 @@
1
+ import type { FilePlan } from "./types.ts";
2
+
3
+ type Hashes = Record<string, string>;
4
+
5
+ export function planPull(base: Hashes, local: Hashes, remote: Hashes): FilePlan {
6
+ const plan: FilePlan = {
7
+ write: [],
8
+ delete: [],
9
+ conflicts: [],
10
+ keepLocal: [],
11
+ unchanged: [],
12
+ };
13
+ for (const path of unionKeys(base, local, remote)) {
14
+ const baseHash = getHash(base, path);
15
+ const localHash = getHash(local, path);
16
+ const remoteHash = getHash(remote, path);
17
+ if (remoteHash === null) {
18
+ if (baseHash === null) {
19
+ plan.keepLocal.push(path);
20
+ } else if (localHash === null) {
21
+ plan.unchanged.push(path);
22
+ } else if (localHash === baseHash) {
23
+ plan.delete.push(path);
24
+ } else {
25
+ plan.conflicts.push(path);
26
+ }
27
+ continue;
28
+ }
29
+ if (localHash === null) {
30
+ if (baseHash === null) {
31
+ plan.write.push(path);
32
+ } else if (remoteHash === baseHash) {
33
+ plan.keepLocal.push(path);
34
+ } else {
35
+ plan.conflicts.push(path);
36
+ }
37
+ continue;
38
+ }
39
+ if (localHash === remoteHash) {
40
+ plan.unchanged.push(path);
41
+ } else if (baseHash !== null && localHash === baseHash) {
42
+ plan.write.push(path);
43
+ } else if (baseHash !== null && remoteHash === baseHash) {
44
+ plan.keepLocal.push(path);
45
+ } else {
46
+ plan.conflicts.push(path);
47
+ }
48
+ }
49
+ return plan;
50
+ }
51
+
52
+ export function planPush(base: Hashes, local: Hashes, remote: Hashes): FilePlan {
53
+ const plan: FilePlan = {
54
+ write: [],
55
+ delete: [],
56
+ conflicts: [],
57
+ keepLocal: [],
58
+ unchanged: [],
59
+ };
60
+ for (const path of unionKeys(base, local, remote)) {
61
+ const baseHash = getHash(base, path);
62
+ const localHash = getHash(local, path);
63
+ const remoteHash = getHash(remote, path);
64
+ if (localHash === null) {
65
+ if (remoteHash === null) {
66
+ plan.unchanged.push(path);
67
+ } else if (baseHash !== null && remoteHash === baseHash) {
68
+ plan.delete.push(path);
69
+ } else {
70
+ plan.conflicts.push(path);
71
+ }
72
+ continue;
73
+ }
74
+ if (remoteHash === null) {
75
+ if (baseHash === null) {
76
+ plan.write.push(path);
77
+ } else {
78
+ plan.conflicts.push(path);
79
+ }
80
+ continue;
81
+ }
82
+ if (localHash === remoteHash) {
83
+ plan.unchanged.push(path);
84
+ } else if (baseHash !== null && remoteHash === baseHash) {
85
+ plan.write.push(path);
86
+ } else if (baseHash !== null && localHash === baseHash) {
87
+ plan.conflicts.push(path);
88
+ } else {
89
+ plan.conflicts.push(path);
90
+ }
91
+ }
92
+ return plan;
93
+ }
94
+
95
+ export function takeRemoteForConflicts(
96
+ plan: FilePlan,
97
+ remote: Hashes,
98
+ paths: string[],
99
+ ): FilePlan {
100
+ for (const path of paths) {
101
+ if (Object.hasOwn(remote, path)) plan.write.push(path);
102
+ else plan.delete.push(path);
103
+ }
104
+ return plan;
105
+ }
106
+
107
+ export function hashesFromFiles(files: Iterable<{ remotePath: string; sha256: string }>): Hashes {
108
+ const hashes: Hashes = {};
109
+ for (const file of files) hashes[file.remotePath] = file.sha256;
110
+ return hashes;
111
+ }
112
+
113
+ function unionKeys(...maps: Hashes[]): string[] {
114
+ const keys = new Set<string>();
115
+ for (const map of maps) {
116
+ for (const key of Object.keys(map)) keys.add(key);
117
+ }
118
+ return [...keys].sort();
119
+ }
120
+
121
+ function getHash(map: Hashes, path: string): string | null {
122
+ return Object.hasOwn(map, path) ? map[path] : null;
123
+ }
@@ -0,0 +1,89 @@
1
+ import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
2
+ import { truncateToWidth } from "@earendil-works/pi-tui";
3
+
4
+ const MAX_PASSPHRASE_LENGTH = 256;
5
+
6
+ const SHORT_KEYS: Record<string, string> = {
7
+ up: "↑",
8
+ down: "↓",
9
+ enter: "Enter",
10
+ escape: "Esc",
11
+ backspace: "Backspace",
12
+ };
13
+
14
+ /**
15
+ * 以掩码方式读取口令。pi 没有内置的密码输入控件,所以这里用自定义组件替换编辑器;
16
+ * 非 TUI 模式的输入会回显,直接拒绝并提示改用 TUI。
17
+ */
18
+ export async function askMaskedPassphrase(
19
+ ctx: ExtensionContext,
20
+ title: string,
21
+ ): Promise<string | undefined> {
22
+ if (!ctx.hasUI || ctx.mode !== "tui") {
23
+ ctx.ui.notify("Passphrase entry requires TUI mode.", "warning");
24
+ return undefined;
25
+ }
26
+ return ctx.ui.custom<string | undefined>((tui, theme, keybindings, done) => {
27
+ let value = "";
28
+ const keyName = (id: Parameters<typeof keybindings.getKeys>[0]): string => {
29
+ const key = keybindings.getKeys(id)[0] ?? "";
30
+ return SHORT_KEYS[key] ?? key;
31
+ };
32
+ const submit = keyName("tui.input.submit");
33
+ const cancel = keyName("tui.select.cancel");
34
+ const backspace = keyName("tui.editor.deleteCharBackward");
35
+ const hints = `${submit} to confirm · ${cancel} to cancel · ${backspace} to delete`;
36
+ return {
37
+ render(width: number): string[] {
38
+ const mask = value.length === 0 ? theme.fg("dim", "(empty)") : "•".repeat(value.length);
39
+ const lines = [
40
+ "",
41
+ ` ${theme.fg("accent", theme.bold(title))}`,
42
+ "",
43
+ ` ${mask}`,
44
+ "",
45
+ ` ${theme.fg("muted", hints)}`,
46
+ "",
47
+ ];
48
+ return lines.map((line) => truncateToWidth(line, Math.max(1, width), ""));
49
+ },
50
+ handleInput(data: string): void {
51
+ if (keybindings.matches(data, "tui.select.cancel")) {
52
+ done(undefined);
53
+ return;
54
+ }
55
+ if (
56
+ keybindings.matches(data, "tui.input.submit")
57
+ || keybindings.matches(data, "tui.select.confirm")
58
+ ) {
59
+ done(value);
60
+ return;
61
+ }
62
+ if (keybindings.matches(data, "tui.editor.deleteCharBackward")) {
63
+ value = value.slice(0, -1);
64
+ tui.requestRender();
65
+ return;
66
+ }
67
+ if (
68
+ keybindings.matches(data, "tui.editor.deleteWordBackward")
69
+ || keybindings.matches(data, "tui.editor.deleteToLineStart")
70
+ ) {
71
+ value = "";
72
+ tui.requestRender();
73
+ return;
74
+ }
75
+ if (data.startsWith("\x1b")) return;
76
+ const printable = [...data]
77
+ .filter((character) => {
78
+ const code = character.codePointAt(0) ?? 0;
79
+ return code >= 32 && code !== 127;
80
+ })
81
+ .join("");
82
+ if (printable.length === 0) return;
83
+ value = (value + printable).slice(0, MAX_PASSPHRASE_LENGTH);
84
+ tui.requestRender();
85
+ },
86
+ invalidate(): void {},
87
+ };
88
+ });
89
+ }
package/src/types.ts ADDED
@@ -0,0 +1,121 @@
1
+ export const CONFIG_SYNC_DIR = "config-sync";
2
+ export const MANIFEST_PATH = "manifest.json";
3
+ export const SECRETS_PATH = "secrets.enc";
4
+ export const AGENT_PATH_PREFIX = "agent/";
5
+ export const AUTH_PROVIDER_ID = "config-sync";
6
+ export const MANIFEST_TOOL = "pi-config-sync";
7
+
8
+ export const DEFAULT_LOCAL_SETTINGS_KEYS = [
9
+ "shellPath",
10
+ "lsp",
11
+ "lastChangelogVersion",
12
+ "extensionChangelogVersions",
13
+ "sessionDir",
14
+ ];
15
+
16
+ export const DEFAULT_EXCLUDE_AUTH_PROVIDERS = [
17
+ AUTH_PROVIDER_ID,
18
+ "github-copilot",
19
+ "qoder",
20
+ "qoder-cn",
21
+ "trae-cn",
22
+ "workbuddy",
23
+ "workbuddy-ai",
24
+ ];
25
+
26
+ export interface SyncConfig {
27
+ version: 1;
28
+ clientId: string;
29
+ repo: string;
30
+ branch: string;
31
+ autoPull: boolean;
32
+ allowPublicRepo: boolean;
33
+ localSettingsKeys: string[];
34
+ includeAuthProviders: string[];
35
+ excludeAuthProviders: string[];
36
+ }
37
+
38
+ export interface SyncBase {
39
+ commit: string;
40
+ hashes: Record<string, string>;
41
+ }
42
+
43
+ export interface SyncRecord {
44
+ action: "pull" | "push";
45
+ at: string;
46
+ commit: string;
47
+ files: number;
48
+ conflicts: number;
49
+ }
50
+
51
+ export interface SyncState {
52
+ version: 1;
53
+ deviceId: string;
54
+ login: string;
55
+ scopes: string[];
56
+ base: SyncBase | null;
57
+ lastSync: SyncRecord | null;
58
+ }
59
+
60
+ export interface SyncManifest {
61
+ version: 1;
62
+ tool: typeof MANIFEST_TOOL;
63
+ deviceId: string;
64
+ pushedAt: string;
65
+ files: Record<string, string>;
66
+ }
67
+
68
+ export interface RemoteFile {
69
+ path: string;
70
+ bytes: Buffer;
71
+ sha256: string;
72
+ }
73
+
74
+ export interface RemoteSnapshot {
75
+ commit: string;
76
+ branch: string;
77
+ manifest: SyncManifest | null;
78
+ files: Map<string, RemoteFile>;
79
+ foreign: boolean;
80
+ }
81
+
82
+ export interface PullResult {
83
+ changed: number;
84
+ deleted: number;
85
+ conflicts: number;
86
+ secretsApplied: boolean;
87
+ secretsSkipped: boolean;
88
+ commit: string;
89
+ }
90
+
91
+ export interface PushResult {
92
+ files: number;
93
+ commit: string;
94
+ conflicts: number;
95
+ forced: boolean;
96
+ }
97
+
98
+ export interface FilePlan {
99
+ write: string[];
100
+ delete: string[];
101
+ conflicts: string[];
102
+ keepLocal: string[];
103
+ unchanged: string[];
104
+ }
105
+
106
+ export interface SyncUi {
107
+ notify(message: string, type?: "info" | "warning" | "error"): void;
108
+ confirm(title: string, message: string): Promise<boolean>;
109
+ select(title: string, options: string[]): Promise<string | undefined>;
110
+ askText(title: string, placeholder?: string): Promise<string | undefined>;
111
+ askPassphrase(title: string): Promise<string | undefined>;
112
+ copyText(text: string): void;
113
+ setStatus(key: string, text: string | undefined): void;
114
+ }
115
+
116
+ export class SyncError extends Error {
117
+ constructor(message: string) {
118
+ super(message);
119
+ this.name = "SyncError";
120
+ }
121
+ }