@skathio/somi 5.0.2 → 5.0.4
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/.claude-plugin/icon.png +0 -0
- package/.claude-plugin/marketplace.json +1 -1
- package/.claude-plugin/plugin.json +1 -1
- package/.copilot-extension/extension.json +1 -1
- package/.copilot-extension/marketplace.json +1 -1
- package/README.md +4 -2
- package/VERSION +1 -1
- package/hooks/session-start/inject-digest.mjs +16 -0
- package/package.json +1 -1
- package/scripts/lib/advisor.mjs +2 -2
- package/scripts/lib/doctor.mjs +11 -2
- package/scripts/lib/init.mjs +62 -11
- package/scripts/lib/launch.mjs +65 -0
- package/scripts/somi.mjs +50 -12
package/.claude-plugin/icon.png
CHANGED
|
Binary file
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"url": "https://github.com/skathio/somi.git"
|
|
14
14
|
},
|
|
15
15
|
"description": "A workflow plugin for coding agents: say what you want or pass a ticket, and it picks the right workflow and keeps the cost down, while your agent stays in charge.",
|
|
16
|
-
"version": "5.0.
|
|
16
|
+
"version": "5.0.4",
|
|
17
17
|
"tags": [
|
|
18
18
|
"workflow",
|
|
19
19
|
"agents",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "somi",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.4",
|
|
4
4
|
"displayName": "SoMi",
|
|
5
5
|
"description": "A workflow plugin for coding agents: say what you want or pass a ticket, and it picks the right workflow and keeps the cost down, while your agent stays in charge.",
|
|
6
6
|
"keywords": [
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "somi",
|
|
3
3
|
"displayName": "SoMi",
|
|
4
|
-
"version": "5.0.
|
|
4
|
+
"version": "5.0.4",
|
|
5
5
|
"description": "A workflow plugin for coding agents: say what you want or pass a ticket, and it picks the right workflow and keeps the cost down, while your agent stays in charge.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"workflow",
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"name": "somi",
|
|
12
12
|
"source": "github:skathio/somi",
|
|
13
13
|
"description": "A workflow plugin for coding agents: say what you want or pass a ticket, and it picks the right workflow and keeps the cost down, while your agent stays in charge.",
|
|
14
|
-
"version": "5.0.
|
|
14
|
+
"version": "5.0.4",
|
|
15
15
|
"tags": [
|
|
16
16
|
"workflow",
|
|
17
17
|
"agents",
|
package/README.md
CHANGED
|
@@ -57,18 +57,20 @@ Start with SoMi as the session's agent and just talk to it:
|
|
|
57
57
|
claude --agent somi:somi # or: copilot --agent somi:somi
|
|
58
58
|
```
|
|
59
59
|
|
|
60
|
+
To start that way with the model your config sets for the conversation's tier, `somi launch claude` or `somi launch copilot` starts the host with `--agent somi:somi` and that model, and Copilot in experimental mode for `/somi-deliver`. Anything after it goes to the host. Ask `/somi-config` for the full command, and wrap it in an alias or function of your own, on Linux, macOS or Windows.
|
|
61
|
+
|
|
60
62
|
Everything is done from the chat. Ask for a change, a bug fix, a refactor, a plan, a design, a review, an upgrade or the pull request text in plain words, or paste a ticket, and SoMi's agent picks the workflow. Outside SoMi's agent, plain prompts are not routed, and SoMi says so at session start. In a Copilot session already running, `/agent somi` switches to it. Six commands do the rest:
|
|
61
63
|
|
|
62
64
|
| Command | What it does |
|
|
63
65
|
|---|---|
|
|
64
66
|
| `/somi <anything>` | Routes anything, the way the agent does with plain words. Use it from another agent. |
|
|
65
|
-
| `/somi-deliver <work or ticket>` | Runs large work as SoMi's staged workflow. Only you can start it. On Copilot,
|
|
67
|
+
| `/somi-deliver <work or ticket>` | Runs large work as SoMi's staged workflow. Only you can start it. On Copilot, it needs `/experimental`; SoMi registers it at session start, so a session already open gets it after `/clear`. |
|
|
66
68
|
| `/somi-review` | Reviews the current changes, whether or not the diff calls for it. |
|
|
67
69
|
| `/somi-quality [commit \| repo]` | Checks the code against the quality limits: the branch, one commit, or the whole codebase. |
|
|
68
70
|
| `/somi-status` | Shows the open plan, decisions and findings. |
|
|
69
71
|
| `/somi-config` | Shows and changes the models each tier uses and the quality limits, and checks the setup. |
|
|
70
72
|
|
|
71
|
-
On Claude Code they also answer to `/somi:somi-review` and so on. On Copilot, SoMi's commands are listed as skills
|
|
73
|
+
On Claude Code they also answer to `/somi:somi-review` and so on. On Copilot, SoMi's commands are listed as skills, by their plain names only: type `/somi-deliver`, not `/somi:somi-deliver`.
|
|
72
74
|
|
|
73
75
|
## Set your tiers
|
|
74
76
|
|
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
5.0.
|
|
1
|
+
5.0.4
|
|
@@ -5,8 +5,10 @@
|
|
|
5
5
|
import fs from 'node:fs';
|
|
6
6
|
import path from 'node:path';
|
|
7
7
|
import { pluginRoot, projectRoot, readPayload, runHook } from '../lib/common.mjs';
|
|
8
|
+
import { globalConfigPath, readConfigFile } from '../../scripts/lib/config-files.mjs';
|
|
8
9
|
import { sandboxSettings, settingsWarnings } from '../../scripts/lib/doctor.mjs';
|
|
9
10
|
import { stateIsTracked } from '../../scripts/lib/git-tree.mjs';
|
|
11
|
+
import { copilotWorkflowsOn, installCopilotExtension, isInside } from '../../scripts/lib/init.mjs';
|
|
10
12
|
import { readLedger } from '../../scripts/lib/ledger.mjs';
|
|
11
13
|
import { planStatus } from '../../scripts/lib/plan.mjs';
|
|
12
14
|
import { listShards } from '../../scripts/lib/shards.mjs';
|
|
@@ -28,6 +30,19 @@ function projectParts(project) {
|
|
|
28
30
|
return parts;
|
|
29
31
|
}
|
|
30
32
|
|
|
33
|
+
// Copilot loads a workflow only from an extension, so the user's copy is written here, where a missing step would otherwise surface
|
|
34
|
+
// as /somi-deliver failing. It is said only when it was written, because a session that started before then does not have it yet.
|
|
35
|
+
// A copy inside the project, such as a vendored one, serves only that project, so it does not become every project's.
|
|
36
|
+
function copilotWorkflows(root, project) {
|
|
37
|
+
if (isInside(project, root)) return [];
|
|
38
|
+
try {
|
|
39
|
+
const file = globalConfigPath();
|
|
40
|
+
if (!copilotWorkflowsOn(file ? readConfigFile(file) : null)) return [];
|
|
41
|
+
const { written } = installCopilotExtension({ pluginRoot: root });
|
|
42
|
+
return written ? ['SoMi just registered its staged workflow, /somi-deliver, with Copilot CLI for every project. It loads from the next session or after /clear. Mention it only if the user asks for /somi-deliver.'] : [];
|
|
43
|
+
} catch { return []; }
|
|
44
|
+
}
|
|
45
|
+
|
|
31
46
|
runHook(() => {
|
|
32
47
|
const payload = readPayload();
|
|
33
48
|
const root = pluginRoot();
|
|
@@ -38,6 +53,7 @@ runHook(() => {
|
|
|
38
53
|
const warnings = settingsWarnings(host, sandboxSettings({ host, root: projectRoot() }));
|
|
39
54
|
if (warnings.length > 0) parts.push(`WARNING: the OS sandbox, SoMi's security boundary, needs attention: ${warnings[0]}. Hooks are tripwires, not a boundary. Tell the user once, in one line, and do not repeat it. If they ask for the detail, run \`somi doctor\` and relay it.`);
|
|
40
55
|
parts.push(...projectParts(projectRoot()));
|
|
56
|
+
if (host === 'copilot') parts.push(...copilotWorkflows(root, projectRoot()));
|
|
41
57
|
const notice = updateNotice();
|
|
42
58
|
if (notice) parts.push(notice);
|
|
43
59
|
// Claude Code names the active agent here. Copilot CLI does not, so there the model is told how to tell from its own instructions.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@skathio/somi",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.4",
|
|
4
4
|
"scripts": {
|
|
5
5
|
"test": "node --test --test-reporter=spec tests/*.test.mjs",
|
|
6
6
|
"test:coverage": "node -e \"require('fs').mkdirSync('coverage',{recursive:true})\" && node --test --experimental-test-coverage --test-reporter=spec --test-reporter-destination=stdout --test-reporter=lcov --test-reporter-destination=coverage/lcov.info tests/*.test.mjs",
|
package/scripts/lib/advisor.mjs
CHANGED
|
@@ -117,8 +117,8 @@ const LARGE = (request) => String(request ?? '').length > LONG_REQUEST;
|
|
|
117
117
|
// A project's config is cloned input and may only tighten a limit, so a change the user wants goes in their own file.
|
|
118
118
|
// Everything here is done from the chat: the agent runs SoMi's own commands by their full path, because no somi command is on the PATH.
|
|
119
119
|
const QUALITY_CONFIG = `Relay this briefly. To change a quality limit, write it under quality in the file named by global_config, or under quality.commit or quality.pr for one scope, never in the project's .somi/config.json, where only a tighter value counts. Before you write a value looser than the current limit, ask the user and wait for a yes. quality_timeout_seconds is how long the gate may run before it counts as failed: to change it, write quality.timeout_seconds there, a whole number from ${QUALITY_TIMEOUT.min} to ${QUALITY_TIMEOUT.max}.`;
|
|
120
|
-
const SETUP = (host) => `To check SoMi's setup, run \`${somiCommand('doctor')}\` and relay what it finds. After tiers.main changes, run \`${somiCommand('setup')}\`. To connect another plugin to a step, run \`${somiCommand('slot <step> <plugin:component>')}\` with the step and component the user names, and \`${somiCommand('slot --list')}\` to show what is connected.${host === 'claude' ? '' :
|
|
121
|
-
const STAGED_ADVICE = (host) => `The user typed /somi-deliver to run this work in stages. Start the workflow below by its name with its args unchanged${host === 'claude' ? ', using the Workflow tool: it is a saved workflow, so no script and no authoring guide are needed' : ', as Copilot runs workflows in experimental mode'}. Tell the user it is running and end your turn. When it returns, do not read or check the work again, because the workflow ran the checks: record its reviews with somi_review, passing tree as review_tree, and tell the user its summary and status in a few lines. If you cannot start it, say why, and call somi_route again with the same request and without staged for the steps.`;
|
|
120
|
+
const SETUP = (host) => `To check SoMi's setup, run \`${somiCommand('doctor')}\` and relay what it finds. After tiers.main changes, run \`${somiCommand('setup')}\`. To connect another plugin to a step, run \`${somiCommand('slot <step> <plugin:component>')}\` with the step and component the user names, and \`${somiCommand('slot --list')}\` to show what is connected. To start sessions with SoMi's agent on its tier's model, \`${somiCommand('launch claude')}\` or \`${somiCommand('launch copilot')}\` does it, with anything after it passed to the host: give the user that command for an alias of their own.${host === 'claude' ? '' : ' SoMi registers the staged workflow, /somi-deliver, with Copilot for every project at session start, so there is nothing to run for it: a session that started before that gets it after /clear, and Copilot runs it only in experimental mode.'}`;
|
|
121
|
+
const STAGED_ADVICE = (host) => `The user typed /somi-deliver to run this work in stages. Start the workflow below by its name with its args unchanged${host === 'claude' ? ', using the Workflow tool: it is a saved workflow, so no script and no authoring guide are needed' : ', as Copilot runs workflows in experimental mode. If Copilot says no workflow is registered by that name, this session started before SoMi registered it: reload extensions with the extensions reload tool, which registers it without losing the conversation, then start the workflow again; failing that, tell the user that /clear or a new session loads it. /experimental must be on'}. Tell the user it is running and end your turn. When it returns, do not read or check the work again, because the workflow ran the checks: record its reviews with somi_review, passing tree as review_tree, and tell the user its summary and status in a few lines. If you cannot start it, say why, and call somi_route again with the same request and without staged for the steps.`;
|
|
122
122
|
const SUGGEST_STAGED = 'This looks like large work. Tell the user once that typing /somi-deliver with it runs it in stages, each on its tier, and that only they can start that. Then follow steps.';
|
|
123
123
|
|
|
124
124
|
// With no rule to go on and the whole table to choose from, a request is new work. Asking the model to pick costs a turn and lets it
|
package/scripts/lib/doctor.mjs
CHANGED
|
@@ -6,6 +6,7 @@ import fs from 'node:fs';
|
|
|
6
6
|
import os from 'node:os';
|
|
7
7
|
import path from 'node:path';
|
|
8
8
|
import { spawnSync } from 'node:child_process';
|
|
9
|
+
import { copilotExtensionFile, copilotWorkflowsOn, projectExtensionWarnings } from './init.mjs';
|
|
9
10
|
import { resolveSpawn } from './tiers.mjs';
|
|
10
11
|
|
|
11
12
|
const WSL_REMEDY = 'Native Windows has no OS sandbox for agents. Run the agent inside WSL2 and run this again there.';
|
|
@@ -64,6 +65,14 @@ export function duplicateHookWarnings(root) {
|
|
|
64
65
|
return fs.existsSync(file) ? [`${file} was written by an earlier somi init, and Copilot CLI now runs the plugin's own hooks, so every SoMi hook runs twice; delete that file`] : [];
|
|
65
66
|
}
|
|
66
67
|
|
|
68
|
+
// The session-start hook writes the user's copy, so none there means the hooks did not run or the global config turned it off. Only the
|
|
69
|
+
// global config says it is off, as only it can turn it off.
|
|
70
|
+
export function copilotWorkflowWarnings(host, root, { config = {}, home = os.homedir(), env = process.env } = {}) {
|
|
71
|
+
if (host !== 'copilot' || !copilotWorkflowsOn(config)) return [];
|
|
72
|
+
const missing = fs.existsSync(copilotExtensionFile(env, home)) ? [] : [`${copilotExtensionFile(env, home)} is missing, so /somi-deliver has no workflow to run; start a new session, or run \`somi init --host copilot\``];
|
|
73
|
+
return [...missing, ...projectExtensionWarnings(root)];
|
|
74
|
+
}
|
|
75
|
+
|
|
67
76
|
// A plugin's hooks can fail to load without any error. The audit hook runs on every tool call, so a log with nothing recent means none of them ran.
|
|
68
77
|
export function hookWarnings(root, { now = Date.now(), env = process.env } = {}) {
|
|
69
78
|
const log = env.SOMI_AUDIT_LOG || path.join(root, '.somi', 'audit.log');
|
|
@@ -71,7 +80,7 @@ export function hookWarnings(root, { now = Date.now(), env = process.env } = {})
|
|
|
71
80
|
return ['no hook has logged a tool call here in the last hour, so the tripwire hooks may not be running on this host; run this from inside an agent session, and check that the SoMi plugin is enabled'];
|
|
72
81
|
}
|
|
73
82
|
|
|
74
|
-
export function runDoctor({ root, host, config = {}, platform = process.platform, env = process.env, home = os.homedir(), exec = defaultExec, removeFile = fs.rmSync, pid = process.pid }) {
|
|
83
|
+
export function runDoctor({ root, host, config = {}, globalConfig = {}, platform = process.platform, env = process.env, home = os.homedir(), exec = defaultExec, removeFile = fs.rmSync, pid = process.pid }) {
|
|
75
84
|
if (platform === 'win32') return { ok: false, verdict: 'FAIL', host, remedy: WSL_REMEDY, probes: [], warnings: [] };
|
|
76
85
|
const probePath = path.join(home, `.somi-sandbox-probe-${pid}`);
|
|
77
86
|
const touch = exec('touch', [probePath]);
|
|
@@ -85,7 +94,7 @@ export function runDoctor({ root, host, config = {}, platform = process.platform
|
|
|
85
94
|
const ok = probes.every((p) => p.blocked);
|
|
86
95
|
return {
|
|
87
96
|
ok, verdict: ok ? 'PASS' : 'FAIL', host, probes,
|
|
88
|
-
warnings: [...settingsWarnings(host, sandboxSettings({ host, root, home, env })), ...tierWarnings(config, host), ...autoWarnings(host, { home, env }), ...hookWarnings(root, { env }), ...duplicateHookWarnings(root)],
|
|
97
|
+
warnings: [...settingsWarnings(host, sandboxSettings({ host, root, home, env })), ...tierWarnings(config, host), ...autoWarnings(host, { home, env }), ...hookWarnings(root, { env }), ...duplicateHookWarnings(root), ...copilotWorkflowWarnings(host, root, { config: globalConfig, home, env })],
|
|
89
98
|
remedy: ok ? undefined : 'Enable the sandbox: `somi doctor --profile` prints the settings to merge into the host\'s own. Then check again from inside an agent session, because a command typed at a `!` prompt usually bypasses the sandbox.',
|
|
90
99
|
};
|
|
91
100
|
}
|
package/scripts/lib/init.mjs
CHANGED
|
@@ -1,23 +1,74 @@
|
|
|
1
|
-
// init.mjs:
|
|
2
|
-
//
|
|
1
|
+
// init.mjs: registers SoMi's staged workflows with Copilot CLI, which loads a workflow only from an extension. Copilot also reads
|
|
2
|
+
// extensions from the user's own folder, so one file there serves every project, and the session-start hook keeps it pointing at the
|
|
3
|
+
// installed plugin. Nothing is written into a project, and the Claude Code plugin install needs none of this.
|
|
3
4
|
|
|
4
5
|
import fs from 'node:fs';
|
|
6
|
+
import os from 'node:os';
|
|
5
7
|
import path from 'node:path';
|
|
6
8
|
import { pathToFileURL } from 'node:url';
|
|
7
9
|
|
|
8
|
-
|
|
10
|
+
const MARK = '// Written by SoMi:';
|
|
11
|
+
// An earlier somi init wrote this into the project. With the user's copy as well, both register the same workflow names.
|
|
12
|
+
const PROJECT_MARK = '// Written by somi init:';
|
|
13
|
+
|
|
14
|
+
const isLink = (p) => { try { return fs.lstatSync(p).isSymbolicLink(); } catch { return false; } };
|
|
15
|
+
|
|
16
|
+
export const copilotHomeExists = (env = process.env, home = os.homedir()) => fs.existsSync(env.COPILOT_HOME || path.join(home, '.copilot'));
|
|
17
|
+
|
|
18
|
+
export const copilotExtensionFile = (env = process.env, home = os.homedir()) => path.join(env.COPILOT_HOME || path.join(home, '.copilot'), 'extensions', 'somi', 'extension.mjs');
|
|
19
|
+
|
|
20
|
+
// A few lines that load SoMi's own module from the plugin. When the plugin is gone, it joins with nothing rather than failing to load.
|
|
9
21
|
export function copilotExtension(pluginRoot) {
|
|
10
22
|
const lib = pathToFileURL(path.join(pluginRoot, 'scripts', 'lib', 'copilot-workflow.mjs')).href;
|
|
11
|
-
return
|
|
23
|
+
return `${MARK} registers SoMi's staged workflows with Copilot CLI for every project. SoMi rewrites it at session start while the plugin is
|
|
24
|
+
// installed. To turn it off, set "copilot": { "workflows": false } in SoMi's global config and delete this folder.
|
|
25
|
+
import fs from 'node:fs';
|
|
26
|
+
import { defineWorkflow, joinSession } from '@github/copilot-sdk/extension';
|
|
27
|
+
|
|
28
|
+
const lib = new URL(${JSON.stringify(lib)});
|
|
29
|
+
if (fs.existsSync(lib)) {
|
|
30
|
+
const { registerCopilotWorkflows } = await import(lib.href);
|
|
31
|
+
await registerCopilotWorkflows({ defineWorkflow, joinSession });
|
|
32
|
+
} else {
|
|
33
|
+
await joinSession({});
|
|
34
|
+
}
|
|
35
|
+
`;
|
|
12
36
|
}
|
|
13
37
|
|
|
14
|
-
|
|
15
|
-
|
|
38
|
+
/** Writes the user's copy, or brings it up to date. A file SoMi did not write is left alone, and a link is never written through. */
|
|
39
|
+
export function installCopilotExtension({ pluginRoot, env = process.env, home = os.homedir() }) {
|
|
40
|
+
const file = copilotExtensionFile(env, home);
|
|
41
|
+
const text = copilotExtension(pluginRoot);
|
|
42
|
+
try {
|
|
43
|
+
if (isLink(path.dirname(file)) || isLink(file)) return { written: false, file, reason: 'it is a link, and SoMi does not write through one' };
|
|
44
|
+
if (fs.existsSync(file)) {
|
|
45
|
+
const current = fs.readFileSync(file, 'utf8');
|
|
46
|
+
if (current === text) return { written: false, current: true, file };
|
|
47
|
+
if (!current.startsWith(MARK)) return { written: false, file, reason: 'a different extension.mjs is already there; SoMi leaves it alone' };
|
|
48
|
+
}
|
|
49
|
+
// A session starting at the same moment reads the old file or the new one, never half of one.
|
|
50
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
51
|
+
const temp = `${file}.${process.pid}.tmp`;
|
|
52
|
+
fs.writeFileSync(temp, text);
|
|
53
|
+
fs.renameSync(temp, file);
|
|
54
|
+
return { written: true, file };
|
|
55
|
+
} catch (e) {
|
|
56
|
+
return { written: false, file, reason: e.message };
|
|
57
|
+
}
|
|
16
58
|
}
|
|
17
59
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
60
|
+
/** Whether p is dir or inside it. A folder whose name starts with two dots is inside. */
|
|
61
|
+
export function isInside(dir, p) {
|
|
62
|
+
const rel = path.relative(dir, p);
|
|
63
|
+
return !(rel === '..' || rel.startsWith(`..${path.sep}`) || path.isAbsolute(rel));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** The global config can turn the user's copy off. A project's cannot, because the file serves every project. */
|
|
67
|
+
export const copilotWorkflowsOn = (globalConfig) => globalConfig?.copilot?.workflows !== false;
|
|
68
|
+
|
|
69
|
+
export function projectExtensionWarnings(root) {
|
|
70
|
+
const file = path.join(root, '.github', 'extensions', 'somi', 'extension.mjs');
|
|
71
|
+
let text = '';
|
|
72
|
+
try { text = fs.readFileSync(file, 'utf8'); } catch { return []; }
|
|
73
|
+
return text.startsWith(PROJECT_MARK) ? [`${file} was written by an earlier somi init, and SoMi now registers its workflows once for every project, so they are registered twice here; delete that folder`] : [];
|
|
23
74
|
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// launch.mjs: starts Claude Code or Copilot CLI with SoMi's agent, on the model the user's global config sets for the conversation's
|
|
2
|
+
// tier. Only the global config counts here: a project's is cloned input, and it should not pick the model a session starts on.
|
|
3
|
+
|
|
4
|
+
import fs from 'node:fs';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { mainTier, resolveSpawn } from './tiers.mjs';
|
|
7
|
+
|
|
8
|
+
export const LAUNCH_HOSTS = Object.freeze(['claude', 'copilot']);
|
|
9
|
+
const AGENT = 'somi:somi';
|
|
10
|
+
|
|
11
|
+
const passed = (extra, flag) => extra.some((a) => a === flag || a.startsWith(`${flag}=`));
|
|
12
|
+
|
|
13
|
+
/** The command and arguments to run. What the user passes comes last, and a flag they pass themselves is not added. */
|
|
14
|
+
export function launchPlan({ host, config = {}, extra = [] }) {
|
|
15
|
+
const tier = mainTier(config);
|
|
16
|
+
const { model } = resolveSpawn(config, host, tier);
|
|
17
|
+
const args = [
|
|
18
|
+
...(passed(extra, '--agent') ? [] : ['--agent', AGENT]),
|
|
19
|
+
...(model && !passed(extra, '--model') ? ['--model', model] : []),
|
|
20
|
+
// /somi-deliver runs as a Copilot workflow, which needs experimental mode.
|
|
21
|
+
...(host === 'copilot' && !passed(extra, '--experimental') && !passed(extra, '--no-experimental') ? ['--experimental'] : []),
|
|
22
|
+
...extra,
|
|
23
|
+
];
|
|
24
|
+
const warnings = model ? [] : [`no model is set for tier ${tier} on ${host}, so ${host} starts on its own default; set tiers.${host}.${tier} in SoMi's global config`];
|
|
25
|
+
return { command: host, args, tier, model, warnings };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Windows: an npm install puts each host on the PATH as a .cmd shim, which only cmd.exe can start, so the line is quoted for cmd.exe
|
|
29
|
+
// and for the C runtime that splits it again, the way cross-spawn does it. A native .exe starts directly, as it does everywhere else.
|
|
30
|
+
const META = /([()\][%!^"`<>&|;, *?])/g;
|
|
31
|
+
const escapeCommand = (file) => file.replace(META, '^$1');
|
|
32
|
+
function escapeArgument(arg, shim) {
|
|
33
|
+
const quoted = `"${String(arg).replace(/(\\*)"/g, '$1$1\\"').replace(/(\\*)$/, '$1$1')}"`.replace(META, '^$1');
|
|
34
|
+
// A batch file that forwards %* hands its arguments to cmd.exe once more, so they are escaped for that pass too.
|
|
35
|
+
return shim ? quoted.replace(META, '^$1') : quoted;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const isFile = (f) => { try { return fs.statSync(f).isFile(); } catch { return false; } };
|
|
39
|
+
const readText = (f) => fs.readFileSync(f, 'utf8');
|
|
40
|
+
|
|
41
|
+
// Every package manager's shim (npm, pnpm, Yarn, nvm-windows, volta) forwards %*, wherever it is installed, so the file's own text
|
|
42
|
+
// decides. One that cannot be read is escaped twice: too many carets only garble an argument, too few let cmd.exe run part of it.
|
|
43
|
+
function forwardsArguments(file, read) {
|
|
44
|
+
try { return read(file).includes('%*'); } catch { return true; }
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Where Windows finds a command: each PATH folder in turn, trying each PATHEXT extension. */
|
|
48
|
+
export function findOnWindowsPath(name, env, exists = isFile) {
|
|
49
|
+
const exts = (env.PATHEXT || '.COM;.EXE;.BAT;.CMD').split(';').filter(Boolean);
|
|
50
|
+
for (const dir of (env.PATH ?? env.Path ?? '').split(';').filter(Boolean)) {
|
|
51
|
+
for (const ext of exts) if (exists(path.win32.join(dir, `${name}${ext}`))) return path.win32.join(dir, `${name}${ext}`);
|
|
52
|
+
}
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** How to spawn the command without a shell on POSIX, and on Windows whether it is an .exe or a .cmd shim. Null when it is not found. */
|
|
57
|
+
export function spawnPlan(command, args, { platform = process.platform, env = process.env, exists = isFile, read = readText } = {}) {
|
|
58
|
+
if (platform !== 'win32') return { file: command, args, options: {} };
|
|
59
|
+
const found = findOnWindowsPath(command, env, exists);
|
|
60
|
+
if (!found) return null;
|
|
61
|
+
if (!/\.(cmd|bat)$/i.test(found)) return { file: found, args, options: {} };
|
|
62
|
+
const shim = forwardsArguments(found, read);
|
|
63
|
+
const line = [escapeCommand(found), ...args.map((a) => escapeArgument(a, shim))].join(' ');
|
|
64
|
+
return { file: env.ComSpec || env.COMSPEC || 'cmd.exe', args: ['/d', '/s', '/c', `"${line}"`], options: { windowsVerbatimArguments: true } };
|
|
65
|
+
}
|
package/scripts/somi.mjs
CHANGED
|
@@ -12,20 +12,24 @@
|
|
|
12
12
|
// somi status | migrate
|
|
13
13
|
// somi setup [--host claude|copilot] makes SoMi the session agent, on the model set for its tier
|
|
14
14
|
// somi doctor [--host claude|copilot] [--profile]
|
|
15
|
-
// somi init --host copilot
|
|
15
|
+
// somi init [--host copilot] registers the staged workflow with Copilot CLI, once for every project
|
|
16
|
+
// somi launch claude|copilot [args...] starts the host with SoMi's agent, on the model set for its tier
|
|
16
17
|
//
|
|
17
18
|
// Exit codes: 0 done, 1 a check, the doctor or the pull request gate failed, 64 the request was refused.
|
|
18
19
|
|
|
20
|
+
import { spawn } from 'node:child_process';
|
|
19
21
|
import fs from 'node:fs';
|
|
22
|
+
import os from 'node:os';
|
|
20
23
|
import path from 'node:path';
|
|
21
24
|
import { parseArgs } from 'node:util';
|
|
22
25
|
import { fileURLToPath } from 'node:url';
|
|
23
26
|
import { advise } from './lib/advisor.mjs';
|
|
24
27
|
import { runCheck } from './lib/check.mjs';
|
|
25
|
-
import { ConfigFileError, loadConfig } from './lib/config-files.mjs';
|
|
28
|
+
import { ConfigFileError, globalConfigPath, loadConfig, readConfigFile } from './lib/config-files.mjs';
|
|
26
29
|
import { duplicateHookWarnings, runDoctor } from './lib/doctor.mjs';
|
|
27
30
|
import { commitBase, listFiles, repoRoot, resolveRef, snapshotTree, stateIsTracked, workBase } from './lib/git-tree.mjs';
|
|
28
|
-
import {
|
|
31
|
+
import { copilotHomeExists, installCopilotExtension, projectExtensionWarnings } from './lib/init.mjs';
|
|
32
|
+
import { LAUNCH_HOSTS, launchPlan, spawnPlan } from './lib/launch.mjs';
|
|
29
33
|
import { addToLedger, readLedger } from './lib/ledger.mjs';
|
|
30
34
|
import { migrate } from './lib/migrate.mjs';
|
|
31
35
|
import { lastCheck, migratedFindings, pullRequest } from './lib/pr.mjs';
|
|
@@ -125,9 +129,9 @@ function doctor(args) {
|
|
|
125
129
|
}
|
|
126
130
|
let root = process.cwd();
|
|
127
131
|
try { root = repoRoot(root); } catch { /* not in a repository */ }
|
|
128
|
-
let
|
|
129
|
-
try {
|
|
130
|
-
const report = runDoctor({ root, host, config });
|
|
132
|
+
let loaded = null;
|
|
133
|
+
try { loaded = loadConfig(root); } catch { /* reported elsewhere */ }
|
|
134
|
+
const report = runDoctor({ root, host, config: loaded?.config ?? {}, globalConfig: loaded?.global.config ?? {} });
|
|
131
135
|
print(report);
|
|
132
136
|
return report.ok ? 0 : 1;
|
|
133
137
|
}
|
|
@@ -142,13 +146,46 @@ function slotCommand(ctx, args) {
|
|
|
142
146
|
return 0;
|
|
143
147
|
}
|
|
144
148
|
|
|
149
|
+
// The session-start hook does this on its own, so init is for a session that started before the plugin was installed. Without --host it
|
|
150
|
+
// finds Copilot CLI by its environment or its folder, and on Claude Code it has nothing to do.
|
|
151
|
+
const RELOAD = 'A file that is current is not yet a loaded extension. In the open session, reload extensions (the extensions reload tool) to register the workflow without losing the conversation, or start a new session or /clear. Copilot runs workflows in experimental mode: start with --experimental, or turn it on with /experimental.';
|
|
145
152
|
function initCommand(ctx, args) {
|
|
146
153
|
const { values } = parseArgs({ args, options: { host: { type: 'string' } } });
|
|
147
|
-
if (values.host !== 'copilot') throw new StateError('init takes --host copilot
|
|
148
|
-
|
|
149
|
-
const
|
|
150
|
-
|
|
151
|
-
|
|
154
|
+
if (values.host !== undefined && values.host !== 'copilot' && values.host !== 'claude') throw new StateError('init takes --host copilot or --host claude');
|
|
155
|
+
// An explicit SOMI_HOST wins; the folder is only a guess for when nothing says which host this is.
|
|
156
|
+
const named = ctx.env.SOMI_HOST === 'claude' || ctx.env.SOMI_HOST === 'copilot';
|
|
157
|
+
const host = values.host ?? (detectHost(ctx.env) === 'copilot' || (!named && copilotHomeExists(ctx.env)) ? 'copilot' : 'claude');
|
|
158
|
+
if (host === 'claude') { print({ host, extension: null, workflows: 'The Claude Code plugin needs no setup.' }); return 0; }
|
|
159
|
+
const extension = installCopilotExtension({ pluginRoot: PLUGIN_ROOT, env: ctx.env });
|
|
160
|
+
const warnings = [...duplicateHookWarnings(ctx.root), ...projectExtensionWarnings(ctx.root)];
|
|
161
|
+
print({ host, extension, workflows: RELOAD, ...(warnings.length ? { warnings } : {}) });
|
|
162
|
+
return extension.written || extension.current ? 0 : 1;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function globalConfig() {
|
|
166
|
+
const file = globalConfigPath();
|
|
167
|
+
try { return (file && readConfigFile(file)) ?? {}; } catch (e) {
|
|
168
|
+
if (!(e instanceof ConfigFileError)) throw e;
|
|
169
|
+
process.stderr.write('somi: ignoring an unreadable global config; the built-in tiers apply\n');
|
|
170
|
+
return {};
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// The host owns the terminal while it runs. Ctrl-C reaches it from the terminal directly, so this process only waits for it.
|
|
175
|
+
function launch(args) {
|
|
176
|
+
const [host, ...extra] = args;
|
|
177
|
+
if (!LAUNCH_HOSTS.includes(host)) throw new StateError(`launch takes ${LAUNCH_HOSTS.join(' or ')}, then any arguments for it`);
|
|
178
|
+
const plan = launchPlan({ host, config: globalConfig(), extra });
|
|
179
|
+
for (const w of plan.warnings) process.stderr.write(`somi: ${w}\n`);
|
|
180
|
+
const run = spawnPlan(plan.command, plan.args);
|
|
181
|
+
if (!run) throw new StateError(`${host} is not on the PATH`);
|
|
182
|
+
for (const sig of ['SIGINT', 'SIGQUIT']) process.on(sig, () => {});
|
|
183
|
+
return new Promise((resolve, reject) => {
|
|
184
|
+
const child = spawn(run.file, run.args, { stdio: 'inherit', ...run.options });
|
|
185
|
+
for (const sig of ['SIGTERM', 'SIGHUP']) process.on(sig, () => child.kill(sig));
|
|
186
|
+
child.on('error', (e) => reject(e.code === 'ENOENT' ? new StateError(`${host} is not on the PATH`) : e));
|
|
187
|
+
child.on('exit', (code, signal) => resolve(code ?? 128 + (os.constants.signals[signal] ?? 0)));
|
|
188
|
+
});
|
|
152
189
|
}
|
|
153
190
|
|
|
154
191
|
const printed = (value, ok = true) => { print(value); return ok ? 0 : 1; };
|
|
@@ -190,8 +227,9 @@ const COMMANDS = {
|
|
|
190
227
|
function main(argv) {
|
|
191
228
|
const [command, ...args] = argv;
|
|
192
229
|
if (command === 'doctor') return doctor(args);
|
|
230
|
+
if (command === 'launch') return launch(args);
|
|
193
231
|
const run = Object.hasOwn(COMMANDS, command ?? '') ? COMMANDS[command] : null;
|
|
194
|
-
if (!run) throw new StateError('somi takes route, review, check, quality, status, pr, plan, setup, slot, ledger, migrate, doctor or
|
|
232
|
+
if (!run) throw new StateError('somi takes route, review, check, quality, status, pr, plan, setup, slot, ledger, migrate, doctor, init or launch');
|
|
195
233
|
return run(makeContext(), args);
|
|
196
234
|
}
|
|
197
235
|
|