@meyverick/omnicode 0.0.1
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 +191 -0
- package/README.md +44 -0
- package/package.json +26 -0
- package/src/bin/omnicode-runtime.sh +121 -0
- package/src/bin/omnicode.js +165 -0
- package/src/installer/lib.js +40 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
Copyright 2026 meyverick
|
|
180
|
+
|
|
181
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
182
|
+
you may not use this file except in compliance with the License.
|
|
183
|
+
You may obtain a copy of the License at
|
|
184
|
+
|
|
185
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
186
|
+
|
|
187
|
+
Unless required by applicable law or agreed to in writing, software
|
|
188
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
189
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
190
|
+
See the License for the specific language governing permissions and
|
|
191
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# omnicode
|
|
2
|
+
|
|
3
|
+
The Ubuntu command-line entrypoint for running OpenCode through OmniRoute.
|
|
4
|
+
|
|
5
|
+
## What is omnicode?
|
|
6
|
+
|
|
7
|
+
`omnicode` is a thin wrapper that launches OpenCode through OmniRoute on Ubuntu. It expects you to install the underlying tools yourself, then handles optional GrayMatter and OpenSpec initialization, background OmniRoute lifecycle, and project-local OpenCode sessions.
|
|
8
|
+
|
|
9
|
+
## Why
|
|
10
|
+
|
|
11
|
+
Because I'm lazy. Not "I'll automate this repetitive task" lazy, but "I keep opening a second terminal just to run `omniroute`, then manually init GrayMatter, then manually init OpenSpec, then forget which session ID I was using" lazy.
|
|
12
|
+
|
|
13
|
+
Yes, I know OmniRoute is designed to run on a server. But I don't have a server at home, and I still want to save tokens by routing requests through it locally. Bonus: I get to use a bunch of free tiers from different providers without having to remember which API key goes where.
|
|
14
|
+
|
|
15
|
+
So I wrote this wrapper. It starts OmniRoute, inits GrayMatter and OpenSpec quietly in the background, remembers my session per project, and shuts everything down when I'm done. I run one command and pretend I'm a responsible developer with proper infrastructure.
|
|
16
|
+
|
|
17
|
+
## Features
|
|
18
|
+
|
|
19
|
+
- Thin npm global command for Ubuntu.
|
|
20
|
+
- Automatic session resume per project using the OpenCode database.
|
|
21
|
+
- Background OmniRoute lifecycle with cleanup when OpenCode exits.
|
|
22
|
+
- Quiet GrayMatter and OpenSpec initialization with captured logs.
|
|
23
|
+
- Check runtime status with `omnicode --status`.
|
|
24
|
+
- Print version with `omnicode --version`.
|
|
25
|
+
|
|
26
|
+
## Explore
|
|
27
|
+
|
|
28
|
+
- [Getting Started](https://github.com/meyverick/omnicode/wiki/Getting-Started) - install, run, and uninstall.
|
|
29
|
+
- [How it works](https://github.com/meyverick/omnicode/wiki/How-it-works) - runtime flow, session handling, and lifecycle.
|
|
30
|
+
- [Configuration](https://github.com/meyverick/omnicode/wiki/Configuration) - paths and environment overrides.
|
|
31
|
+
- [Troubleshooting](https://github.com/meyverick/omnicode/wiki/Troubleshooting) - common issues and rollback.
|
|
32
|
+
|
|
33
|
+
## Acknowledgments
|
|
34
|
+
|
|
35
|
+
Built on top of:
|
|
36
|
+
|
|
37
|
+
- [OpenCode](https://github.com/opencode-ai/opencode) - AI-powered coding assistant
|
|
38
|
+
- [OmniRoute](https://github.com/diegosouzapw/OmniRoute) - model routing layer
|
|
39
|
+
- [OpenSpec](https://github.com/Fission-AI/OpenSpec) - AI agent workflow framework
|
|
40
|
+
- [GrayMatter](https://github.com/angelnicolasc/graymatter) - persistent memory for AI agents
|
|
41
|
+
|
|
42
|
+
## License
|
|
43
|
+
|
|
44
|
+
[Apache 2.0](LICENSE)
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@meyverick/omnicode",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Ubuntu command-line entrypoint for OpenCode through OmniRoute",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"omnicode": "src/bin/omnicode.js"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"test": "node --test test/**/*.test.js"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"src"
|
|
14
|
+
],
|
|
15
|
+
"engines": {
|
|
16
|
+
"node": ">=26.0.0"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"opencode",
|
|
20
|
+
"omniroute",
|
|
21
|
+
"ubuntu",
|
|
22
|
+
"cli"
|
|
23
|
+
],
|
|
24
|
+
"author": "meyverick",
|
|
25
|
+
"license": "Apache-2.0"
|
|
26
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
set -euo pipefail
|
|
3
|
+
|
|
4
|
+
SESSION_FLAG="${1:-}"
|
|
5
|
+
SESSION_ID="${2:-}"
|
|
6
|
+
RUNTIME_DIR="$HOME/.local/share/omnicode"
|
|
7
|
+
LOG_FILE="$RUNTIME_DIR/omniroute.log"
|
|
8
|
+
GRAYMATTER_LOG="$RUNTIME_DIR/graymatter-init.log"
|
|
9
|
+
OPENSPEC_LOG="$RUNTIME_DIR/openspec-init.log"
|
|
10
|
+
PID_FILE="$RUNTIME_DIR/omniroute.pid"
|
|
11
|
+
MAX_OMNI_WAIT=30
|
|
12
|
+
OMNI_CHECK_DELAY=1
|
|
13
|
+
|
|
14
|
+
export PATH="$PATH:$HOME/.local/bin:/usr/local/bin:/usr/bin:/bin"
|
|
15
|
+
|
|
16
|
+
umask 0077
|
|
17
|
+
mkdir -p "$RUNTIME_DIR"
|
|
18
|
+
|
|
19
|
+
is_pid_alive() {
|
|
20
|
+
local pid="${1:-}"
|
|
21
|
+
[[ -n "$pid" ]] && kill -0 "$pid" >/dev/null 2>&1
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
is_omniroute_running() {
|
|
25
|
+
pgrep -f "omniroute" >/dev/null 2>&1
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
is_opencode_running() {
|
|
29
|
+
pgrep -f "opencode" >/dev/null 2>&1
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
start_omniroute() {
|
|
33
|
+
if is_omniroute_running; then
|
|
34
|
+
echo "[omnicode] omniroute already running"
|
|
35
|
+
return 0
|
|
36
|
+
fi
|
|
37
|
+
|
|
38
|
+
echo "[omnicode] starting omniroute..."
|
|
39
|
+
: > "$LOG_FILE"
|
|
40
|
+
nohup omniroute --no-open >>"$LOG_FILE" 2>&1 &
|
|
41
|
+
local pid=$!
|
|
42
|
+
if [[ -e "$PID_FILE" ]]; then
|
|
43
|
+
echo "[omnicode] ERROR: PID file already exists at $PID_FILE" >&2
|
|
44
|
+
exit 1
|
|
45
|
+
fi
|
|
46
|
+
touch "$PID_FILE"
|
|
47
|
+
chmod 600 "$PID_FILE"
|
|
48
|
+
printf '%s\n' "$pid" > "$PID_FILE"
|
|
49
|
+
|
|
50
|
+
local waited=0
|
|
51
|
+
while [[ "$waited" -lt "$MAX_OMNI_WAIT" ]]; do
|
|
52
|
+
sleep "$OMNI_CHECK_DELAY"
|
|
53
|
+
waited=$((waited + OMNI_CHECK_DELAY))
|
|
54
|
+
|
|
55
|
+
if ! is_pid_alive "$pid"; then
|
|
56
|
+
echo "[omnicode] ERROR: omniroute exited during startup. Log: $LOG_FILE" >&2
|
|
57
|
+
exit 1
|
|
58
|
+
fi
|
|
59
|
+
|
|
60
|
+
if is_omniroute_running; then
|
|
61
|
+
echo "[omnicode] omniroute started (pid: $pid)"
|
|
62
|
+
return 0
|
|
63
|
+
fi
|
|
64
|
+
done
|
|
65
|
+
|
|
66
|
+
echo "[omnicode] ERROR: omniroute did not become ready. Log: $LOG_FILE" >&2
|
|
67
|
+
exit 1
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
stop_omniroute_if_idle() {
|
|
71
|
+
if is_opencode_running; then
|
|
72
|
+
return 0
|
|
73
|
+
fi
|
|
74
|
+
|
|
75
|
+
if [[ -f "$PID_FILE" ]]; then
|
|
76
|
+
local pid
|
|
77
|
+
pid="$(cat "$PID_FILE" 2>/dev/null || true)"
|
|
78
|
+
if is_pid_alive "$pid"; then
|
|
79
|
+
echo "[omnicode] no opencode left -> stopping omniroute (pid: $pid)"
|
|
80
|
+
kill "$pid" 2>/dev/null || true
|
|
81
|
+
fi
|
|
82
|
+
rm -f "$PID_FILE"
|
|
83
|
+
fi
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
cleanup() {
|
|
87
|
+
stop_omniroute_if_idle
|
|
88
|
+
}
|
|
89
|
+
trap cleanup EXIT INT TERM
|
|
90
|
+
|
|
91
|
+
if command -v graymatter >/dev/null 2>&1; then
|
|
92
|
+
echo "[omnicode] graymatter: initializing"
|
|
93
|
+
if graymatter init --only opencode >"$GRAYMATTER_LOG" 2>&1; then
|
|
94
|
+
echo "[omnicode] graymatter: ready"
|
|
95
|
+
else
|
|
96
|
+
echo "[omnicode] WARNING: graymatter init failed; continuing. Log: $GRAYMATTER_LOG"
|
|
97
|
+
fi
|
|
98
|
+
else
|
|
99
|
+
echo "[omnicode] graymatter: not installed, skipping"
|
|
100
|
+
fi
|
|
101
|
+
|
|
102
|
+
if command -v openspec >/dev/null 2>&1; then
|
|
103
|
+
echo "[omnicode] openspec: initializing"
|
|
104
|
+
if openspec init --force --tools opencode >"$OPENSPEC_LOG" 2>&1; then
|
|
105
|
+
echo "[omnicode] openspec: ready"
|
|
106
|
+
else
|
|
107
|
+
echo "[omnicode] WARNING: openspec init failed; continuing. Log: $OPENSPEC_LOG"
|
|
108
|
+
fi
|
|
109
|
+
else
|
|
110
|
+
echo "[omnicode] openspec: not installed, skipping"
|
|
111
|
+
fi
|
|
112
|
+
|
|
113
|
+
start_omniroute
|
|
114
|
+
|
|
115
|
+
if [[ "$SESSION_FLAG" == "-s" && -n "$SESSION_ID" ]]; then
|
|
116
|
+
echo "[omnicode] launching opencode (session: $SESSION_ID)"
|
|
117
|
+
opencode -s "$SESSION_ID"
|
|
118
|
+
else
|
|
119
|
+
echo "[omnicode] launching opencode (new session)"
|
|
120
|
+
opencode
|
|
121
|
+
fi
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { execFileSync, spawn } from "node:child_process";
|
|
3
|
+
import { existsSync, readFileSync, realpathSync } from "node:fs";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
import os from "node:os";
|
|
7
|
+
|
|
8
|
+
import { commandExists } from "../installer/lib.js";
|
|
9
|
+
|
|
10
|
+
const __dirname = fileURLToPath(new URL(".", import.meta.url));
|
|
11
|
+
const runtimeScript = join(__dirname, "omnicode-runtime.sh");
|
|
12
|
+
const packageJsonPath = join(__dirname, "..", "..", "package.json");
|
|
13
|
+
|
|
14
|
+
const SESSION_ID_RE = /^[a-zA-Z0-9_-]+$/;
|
|
15
|
+
|
|
16
|
+
export function printUsage() {
|
|
17
|
+
console.log(`Usage: omnicode [-s <session_id>] [-c] [--status] [--version]`);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function getVersion() {
|
|
21
|
+
const pkg = JSON.parse(readFileSync(packageJsonPath, "utf8"));
|
|
22
|
+
return pkg.version;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function isProcessRunning(name) {
|
|
26
|
+
try {
|
|
27
|
+
execFileSync("pgrep", ["-x", name], { stdio: "ignore" });
|
|
28
|
+
return true;
|
|
29
|
+
} catch {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function getProcessStatus() {
|
|
35
|
+
return {
|
|
36
|
+
opencode: isProcessRunning("opencode"),
|
|
37
|
+
omniroute: isProcessRunning("omniroute"),
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function printStatus(status = getProcessStatus()) {
|
|
42
|
+
console.log(`[omnicode] opencode: ${status.opencode ? "running" : "stopped"}`);
|
|
43
|
+
console.log(`[omnicode] omniroute: ${status.omniroute ? "running" : "stopped"}`);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function parseArgs(argv) {
|
|
47
|
+
let sessionId = null;
|
|
48
|
+
let continueSession = false;
|
|
49
|
+
for (let i = 2; i < argv.length; i++) {
|
|
50
|
+
const arg = argv[i];
|
|
51
|
+
if (arg === "-h" || arg === "--help") {
|
|
52
|
+
printUsage();
|
|
53
|
+
process.exit(0);
|
|
54
|
+
}
|
|
55
|
+
if (arg === "-v" || arg === "--version") {
|
|
56
|
+
console.log(getVersion());
|
|
57
|
+
process.exit(0);
|
|
58
|
+
}
|
|
59
|
+
if (arg === "--status" || arg === "status") {
|
|
60
|
+
printStatus();
|
|
61
|
+
process.exit(0);
|
|
62
|
+
}
|
|
63
|
+
if (arg === "-c" || arg === "--continue") {
|
|
64
|
+
continueSession = true;
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
if (arg === "-s") {
|
|
68
|
+
sessionId = argv[++i];
|
|
69
|
+
if (!sessionId) {
|
|
70
|
+
console.error("[omnicode] ERROR: -s requires a value");
|
|
71
|
+
process.exit(2);
|
|
72
|
+
}
|
|
73
|
+
} else if (arg.startsWith("-s")) {
|
|
74
|
+
sessionId = arg.slice(2);
|
|
75
|
+
} else {
|
|
76
|
+
console.error(`[omnicode] ERROR: unknown option ${arg}`);
|
|
77
|
+
printUsage();
|
|
78
|
+
process.exit(2);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (sessionId && !SESSION_ID_RE.test(sessionId)) {
|
|
82
|
+
console.error(`[omnicode] ERROR: invalid session ID format`);
|
|
83
|
+
process.exit(2);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return { sessionId, continueSession };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export async function getLatestSessionId(directory = realpathSync(process.cwd())) {
|
|
90
|
+
const dbPath = join(os.homedir(), ".local", "share", "opencode", "opencode.db");
|
|
91
|
+
if (!existsSync(dbPath)) return null;
|
|
92
|
+
|
|
93
|
+
try {
|
|
94
|
+
const { DatabaseSync } = await import("node:sqlite");
|
|
95
|
+
const db = new DatabaseSync(dbPath, { readOnly: true });
|
|
96
|
+
const row = db.prepare(
|
|
97
|
+
"SELECT id FROM session WHERE directory = ? ORDER BY time_updated DESC LIMIT 1"
|
|
98
|
+
).get(directory);
|
|
99
|
+
db.close();
|
|
100
|
+
return row ? row.id : null;
|
|
101
|
+
} catch {
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export async function resolveSessionMode(sessionId, continueSession = false, latestSessionId = null) {
|
|
107
|
+
if (sessionId) return { flag: "-s", id: sessionId };
|
|
108
|
+
if (latestSessionId === null) latestSessionId = await getLatestSessionId();
|
|
109
|
+
if (latestSessionId) return { flag: "-s", id: latestSessionId };
|
|
110
|
+
return { flag: null, id: null };
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function buildRuntimeArgs(mode) {
|
|
114
|
+
const args = [runtimeScript];
|
|
115
|
+
if (mode.flag === "-s") {
|
|
116
|
+
args.push("-s", mode.id);
|
|
117
|
+
}
|
|
118
|
+
return args;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async function main() {
|
|
122
|
+
const args = parseArgs(process.argv);
|
|
123
|
+
|
|
124
|
+
const missing = ["opencode", "omniroute"].filter((cmd) => !commandExists(cmd));
|
|
125
|
+
if (missing.length > 0) {
|
|
126
|
+
console.error(`[omnicode] ERROR: missing required tool(s): ${missing.join(", ")}`);
|
|
127
|
+
console.error("[omnicode] Install them before running omnicode. See the README for instructions.");
|
|
128
|
+
process.exit(1);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const mode = await resolveSessionMode(args.sessionId, args.continueSession);
|
|
132
|
+
const childArgs = buildRuntimeArgs(mode);
|
|
133
|
+
|
|
134
|
+
return new Promise((resolve, reject) => {
|
|
135
|
+
const child = spawn("bash", childArgs, {
|
|
136
|
+
stdio: "inherit",
|
|
137
|
+
cwd: process.cwd(),
|
|
138
|
+
});
|
|
139
|
+
child.on("error", reject);
|
|
140
|
+
child.on("close", (code) => {
|
|
141
|
+
if (code !== 0) {
|
|
142
|
+
process.exit(code);
|
|
143
|
+
} else {
|
|
144
|
+
resolve();
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function isMainModule() {
|
|
151
|
+
const entry = process.argv[1];
|
|
152
|
+
if (!entry) return false;
|
|
153
|
+
try {
|
|
154
|
+
return realpathSync(entry) === fileURLToPath(import.meta.url);
|
|
155
|
+
} catch {
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (isMainModule()) {
|
|
161
|
+
main().catch((err) => {
|
|
162
|
+
console.error("[omnicode] ERROR:", err.message);
|
|
163
|
+
process.exit(1);
|
|
164
|
+
});
|
|
165
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { execFileSync, spawn } from "node:child_process";
|
|
2
|
+
import { existsSync, mkdirSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import os from "node:os";
|
|
5
|
+
|
|
6
|
+
export function commandExists(command) {
|
|
7
|
+
try {
|
|
8
|
+
execFileSync("which", [command], { stdio: "ignore" });
|
|
9
|
+
return true;
|
|
10
|
+
} catch {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function run(command, args = [], opts = {}) {
|
|
16
|
+
const quoted = [command, ...args.map((a) => (a.includes(" ") ? `"${a}"` : a))].join(" ");
|
|
17
|
+
console.log(`[omnicode] $ ${quoted}`);
|
|
18
|
+
return new Promise((resolve, reject) => {
|
|
19
|
+
const child = spawn(command, args, {
|
|
20
|
+
stdio: "inherit",
|
|
21
|
+
shell: false,
|
|
22
|
+
cwd: process.cwd(),
|
|
23
|
+
...opts,
|
|
24
|
+
});
|
|
25
|
+
child.on("error", reject);
|
|
26
|
+
child.on("close", (code) => {
|
|
27
|
+
if (code !== 0) {
|
|
28
|
+
reject(new Error(`Command failed with exit code ${code}: ${quoted}`));
|
|
29
|
+
} else {
|
|
30
|
+
resolve();
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function getRuntimeDir() {
|
|
37
|
+
const dir = join(os.homedir(), ".local", "share", "omnicode");
|
|
38
|
+
if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
|
|
39
|
+
return dir;
|
|
40
|
+
}
|