@aaryanbindage/omnicontext 0.0.0-stage → 0.1.3
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/LICENSE +21 -0
- package/README.md +159 -2
- package/dist/omnicontext.mjs +149 -0
- package/package.json +58 -4
- package/src/cli.js +200 -0
- package/src/context.js +85 -0
- package/src/index.js +5 -0
- package/src/install.js +67 -0
- package/src/mcp.js +323 -0
- package/src/resolve.js +55 -0
- package/src/store.js +303 -0
- package/src/sync.js +127 -0
package/src/sync.js
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
// Cross-device sync: the data folder is a git repo pushed to a (private) remote you own.
|
|
2
|
+
|
|
3
|
+
import { execFileSync } from 'node:child_process';
|
|
4
|
+
import fs from 'node:fs';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import os from 'node:os';
|
|
7
|
+
|
|
8
|
+
function git(home, args, { timeout = 30000, allowFail = false } = {}) {
|
|
9
|
+
try {
|
|
10
|
+
return execFileSync('git', args, {
|
|
11
|
+
cwd: home,
|
|
12
|
+
encoding: 'utf8',
|
|
13
|
+
timeout,
|
|
14
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
15
|
+
windowsHide: true,
|
|
16
|
+
env: { ...process.env, GIT_TERMINAL_PROMPT: '0' },
|
|
17
|
+
}).trim();
|
|
18
|
+
} catch (err) {
|
|
19
|
+
if (allowFail) return null;
|
|
20
|
+
const msg = (err.stderr || err.message || '').toString().trim();
|
|
21
|
+
throw new Error(`git ${args[0]} failed: ${msg}`);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function gitAvailable() {
|
|
26
|
+
try {
|
|
27
|
+
execFileSync('git', ['--version'], { stdio: 'ignore', windowsHide: true });
|
|
28
|
+
return true;
|
|
29
|
+
} catch {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function syncStatus(home) {
|
|
35
|
+
if (!gitAvailable()) return { configured: false, git: false };
|
|
36
|
+
const isRepo = fs.existsSync(path.join(home, '.git'));
|
|
37
|
+
const remote = isRepo ? git(home, ['remote', 'get-url', 'origin'], { allowFail: true }) : null;
|
|
38
|
+
const dirty = isRepo ? !!git(home, ['status', '--porcelain'], { allowFail: true }) : false;
|
|
39
|
+
const last = isRepo ? git(home, ['log', '-1', '--format=%cI %s'], { allowFail: true }) : null;
|
|
40
|
+
return { configured: !!remote, git: true, isRepo, remote, dirty, lastCommit: last };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function ensureRepo(home) {
|
|
44
|
+
if (!fs.existsSync(path.join(home, '.git'))) {
|
|
45
|
+
git(home, ['init', '-q']);
|
|
46
|
+
git(home, ['symbolic-ref', 'HEAD', 'refs/heads/main']);
|
|
47
|
+
}
|
|
48
|
+
// Commits need an identity; use a local one if the user has none configured.
|
|
49
|
+
if (!git(home, ['config', 'user.email'], { allowFail: true })) {
|
|
50
|
+
git(home, ['config', 'user.email', 'omnicontext@localhost']);
|
|
51
|
+
git(home, ['config', 'user.name', 'OmniContext']);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function commitAll(home, device) {
|
|
56
|
+
git(home, ['add', '-A']);
|
|
57
|
+
if (git(home, ['status', '--porcelain'])) {
|
|
58
|
+
git(home, ['commit', '-q', '-m', `sync from ${device} ${new Date().toISOString()}`]);
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Connect the data folder to a git remote and do a first sync. */
|
|
65
|
+
export function initSync(home, remote, device = os.hostname()) {
|
|
66
|
+
ensureRepo(home);
|
|
67
|
+
if (git(home, ['remote', 'get-url', 'origin'], { allowFail: true })) git(home, ['remote', 'set-url', 'origin', remote]);
|
|
68
|
+
else git(home, ['remote', 'add', 'origin', remote]);
|
|
69
|
+
return sync(home, device, { allowUnrelated: true });
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Commit local changes, merge remote (local wins on conflicting lines), push. */
|
|
73
|
+
export function sync(home, device = os.hostname(), { allowUnrelated = false, timeout = 30000 } = {}) {
|
|
74
|
+
const st = syncStatus(home);
|
|
75
|
+
if (!st.git) return { ok: false, reason: 'git is not installed' };
|
|
76
|
+
if (!st.configured) return { ok: false, reason: 'sync not configured — run `omnicontext sync --github` or `--remote <url>`' };
|
|
77
|
+
const committed = commitAll(home, device);
|
|
78
|
+
let pulled = false;
|
|
79
|
+
if (git(home, ['fetch', '-q', 'origin'], { timeout, allowFail: true }) !== null) {
|
|
80
|
+
const hasRemoteMain = git(home, ['rev-parse', '-q', '--verify', 'origin/main'], { allowFail: true });
|
|
81
|
+
if (hasRemoteMain) {
|
|
82
|
+
const before = git(home, ['rev-parse', 'HEAD'], { allowFail: true });
|
|
83
|
+
const args = ['merge', '-q', '--no-edit', '-X', 'ours', 'origin/main'];
|
|
84
|
+
if (allowUnrelated) args.push('--allow-unrelated-histories');
|
|
85
|
+
if (git(home, args, { allowFail: true }) === null) {
|
|
86
|
+
git(home, ['merge', '--abort'], { allowFail: true });
|
|
87
|
+
return { ok: false, reason: 'merge failed; resolve manually in ' + home };
|
|
88
|
+
}
|
|
89
|
+
pulled = before !== git(home, ['rev-parse', 'HEAD'], { allowFail: true });
|
|
90
|
+
}
|
|
91
|
+
} else {
|
|
92
|
+
return { ok: false, committed, reason: 'could not reach remote (offline?)' };
|
|
93
|
+
}
|
|
94
|
+
const pushed = git(home, ['push', '-q', '-u', 'origin', 'main'], { timeout, allowFail: true }) !== null;
|
|
95
|
+
return { ok: pushed, committed, pulled, pushed, reason: pushed ? undefined : 'push failed' };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Only fetch + merge (fast path for session start). */
|
|
99
|
+
export function pull(home, { timeout = 8000 } = {}) {
|
|
100
|
+
const st = syncStatus(home);
|
|
101
|
+
if (!st.configured) return { ok: false };
|
|
102
|
+
commitAll(home, os.hostname());
|
|
103
|
+
if (git(home, ['fetch', '-q', 'origin'], { timeout, allowFail: true }) === null) return { ok: false, reason: 'offline' };
|
|
104
|
+
if (!git(home, ['rev-parse', '-q', '--verify', 'origin/main'], { allowFail: true })) return { ok: true };
|
|
105
|
+
const ok = git(home, ['merge', '-q', '--no-edit', '-X', 'ours', 'origin/main'], { allowFail: true }) !== null;
|
|
106
|
+
if (!ok) git(home, ['merge', '--abort'], { allowFail: true });
|
|
107
|
+
return { ok };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Create a private GitHub repo with the `gh` CLI and connect it. */
|
|
111
|
+
export function createGithubRemote(home, name = 'omnicontext-data', device) {
|
|
112
|
+
const gh = (args) =>
|
|
113
|
+
execFileSync('gh', args, { encoding: 'utf8', windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] }).trim();
|
|
114
|
+
let login;
|
|
115
|
+
try {
|
|
116
|
+
login = gh(['api', 'user', '--jq', '.login']);
|
|
117
|
+
} catch {
|
|
118
|
+
throw new Error('GitHub CLI (gh) not found or not logged in. Install https://cli.github.com and run `gh auth login`.');
|
|
119
|
+
}
|
|
120
|
+
const full = `${login}/${name}`;
|
|
121
|
+
try {
|
|
122
|
+
gh(['repo', 'view', full, '--json', 'name']);
|
|
123
|
+
} catch {
|
|
124
|
+
gh(['repo', 'create', full, '--private', '--description', 'My OmniContext data (profile, projects, handoffs)']);
|
|
125
|
+
}
|
|
126
|
+
return initSync(home, `https://github.com/${full}.git`, device);
|
|
127
|
+
}
|