sideby 0.19.0 → 0.20.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/README.md +7 -4
- package/ai-tasks.json +28 -0
- package/dist/build.json +1 -1
- package/dist/src/cli/commands.d.ts +22 -1
- package/dist/src/cli/commands.js +815 -21
- package/dist/src/cli/format.d.ts +4 -0
- package/dist/src/cli/format.js +42 -4
- package/dist/src/cli/json-schemas.js +334 -4
- package/dist/src/cli/main.js +159 -13
- package/dist/src/core/accounts.d.ts +6 -0
- package/dist/src/core/accounts.js +12 -0
- package/dist/src/core/autostart.d.ts +11 -0
- package/dist/src/core/autostart.js +32 -5
- package/dist/src/core/browser-profiles.d.ts +23 -2
- package/dist/src/core/browser-profiles.js +148 -37
- package/dist/src/core/claude-settings-file.d.ts +18 -0
- package/dist/src/core/claude-settings-file.js +52 -3
- package/dist/src/core/config.d.ts +5 -0
- package/dist/src/core/config.js +12 -1
- package/dist/src/core/edit-settings.d.ts +7 -0
- package/dist/src/core/edit-settings.js +10 -5
- package/dist/src/core/file-memo.js +26 -4
- package/dist/src/core/history-group.d.ts +28 -0
- package/dist/src/core/history-group.js +47 -0
- package/dist/src/core/history-index.d.ts +145 -0
- package/dist/src/core/history-index.js +447 -0
- package/dist/src/core/history-project.d.ts +13 -0
- package/dist/src/core/history-project.js +87 -0
- package/dist/src/core/history.d.ts +210 -0
- package/dist/src/core/history.js +952 -0
- package/dist/src/core/inbound.d.ts +62 -0
- package/dist/src/core/inbound.js +215 -0
- package/dist/src/core/launch.d.ts +9 -1
- package/dist/src/core/launch.js +58 -24
- package/dist/src/core/live-sessions-copy.d.ts +5 -0
- package/dist/src/core/live-sessions-copy.js +46 -0
- package/dist/src/core/live-sessions-procs.d.ts +89 -0
- package/dist/src/core/live-sessions-procs.js +296 -0
- package/dist/src/core/live-sessions-title.d.ts +28 -0
- package/dist/src/core/live-sessions-title.js +80 -0
- package/dist/src/core/live-sessions.d.ts +68 -0
- package/dist/src/core/live-sessions.js +163 -0
- package/dist/src/core/login-check.d.ts +15 -0
- package/dist/src/core/login-check.js +32 -0
- package/dist/src/core/messages.d.ts +83 -0
- package/dist/src/core/messages.js +433 -0
- package/dist/src/core/project-categories.d.ts +14 -0
- package/dist/src/core/project-categories.js +48 -0
- package/dist/src/core/quota-fetch.d.ts +3 -3
- package/dist/src/core/quota-fetch.js +5 -5
- package/dist/src/core/quota-levels.d.ts +5 -0
- package/dist/src/core/quota-levels.js +5 -0
- package/dist/src/core/remote-control.d.ts +2 -0
- package/dist/src/core/remote-control.js +34 -59
- package/dist/src/core/run-link.d.ts +76 -0
- package/dist/src/core/run-link.js +245 -0
- package/dist/src/core/same-login.d.ts +4 -3
- package/dist/src/core/same-login.js +10 -7
- package/dist/src/core/session-select.d.ts +31 -0
- package/dist/src/core/session-select.js +44 -0
- package/dist/src/families/browser.d.ts +7 -18
- package/dist/src/families/browser.js +19 -8
- package/dist/src/families/claude/deliver.d.ts +2 -0
- package/dist/src/families/claude/deliver.js +43 -0
- package/dist/src/families/claude/history.d.ts +2 -0
- package/dist/src/families/claude/history.js +192 -0
- package/dist/src/families/claude/live-sessions.d.ts +13 -0
- package/dist/src/families/claude/live-sessions.js +117 -0
- package/dist/src/families/claude/messaging.d.ts +69 -0
- package/dist/src/families/claude/messaging.js +165 -0
- package/dist/src/families/claude/mod.d.ts +26 -0
- package/dist/src/families/claude/mod.js +105 -0
- package/dist/src/families/claude/session-files.d.ts +21 -0
- package/dist/src/families/claude/session-files.js +54 -0
- package/dist/src/families/codex/deliver.d.ts +5 -0
- package/dist/src/families/codex/deliver.js +76 -0
- package/dist/src/families/codex/history.d.ts +7 -0
- package/dist/src/families/codex/history.js +174 -0
- package/dist/src/families/codex/index.js +2 -0
- package/dist/src/families/codex/live-sessions.d.ts +12 -0
- package/dist/src/families/codex/live-sessions.js +193 -0
- package/dist/src/families/codex/quota.d.ts +3 -0
- package/dist/src/families/codex/quota.js +123 -0
- package/dist/src/families/codex/rate-limits.js +2 -0
- package/dist/src/families/codex/rollout.js +1 -1
- package/dist/src/families/grok/browser.d.ts +3 -0
- package/dist/src/families/grok/browser.js +5 -0
- package/dist/src/families/grok/history.d.ts +2 -0
- package/dist/src/families/grok/history.js +157 -0
- package/dist/src/families/grok/live-sessions.d.ts +4 -0
- package/dist/src/families/grok/live-sessions.js +80 -0
- package/dist/src/families/grok/login-key.d.ts +14 -0
- package/dist/src/families/grok/login-key.js +37 -0
- package/dist/src/families/history.d.ts +20 -0
- package/dist/src/families/history.js +81 -0
- package/dist/src/families/live-sessions.d.ts +26 -0
- package/dist/src/families/live-sessions.js +9 -0
- package/dist/src/families/login-keys.js +5 -1
- package/dist/src/families/messaging.d.ts +27 -0
- package/dist/src/families/messaging.js +6 -0
- package/dist/src/families/pi/history.d.ts +2 -0
- package/dist/src/families/pi/history.js +124 -0
- package/dist/src/families/pi/live-sessions.d.ts +2 -0
- package/dist/src/families/pi/live-sessions.js +70 -0
- package/dist/src/families/shared/history-text.d.ts +65 -0
- package/dist/src/families/shared/history-text.js +261 -0
- package/dist/src/menubar/hotkey.d.ts +86 -0
- package/dist/src/menubar/hotkey.js +159 -0
- package/dist/src/menubar/script.d.ts +2 -0
- package/dist/src/menubar/script.js +118 -22
- package/dist/src/menubar/snapshot.d.ts +8 -4
- package/dist/src/menubar/snapshot.js +45 -13
- package/dist/src/menubar/strings.d.ts +24 -6
- package/dist/src/menubar/strings.js +24 -6
- package/dist/src/panel/handler.d.ts +69 -0
- package/dist/src/panel/handler.js +686 -15
- package/dist/src/panel/i18n.d.ts +304 -3
- package/dist/src/panel/i18n.js +669 -67
- package/dist/src/panel/orca.d.ts +48 -0
- package/dist/src/panel/orca.js +153 -0
- package/dist/src/panel/page-logic.d.ts +255 -13
- package/dist/src/panel/page-logic.js +483 -1
- package/dist/src/panel/page-script.js +2265 -207
- package/dist/src/panel/page-styles.js +404 -32
- package/dist/src/panel/page.js +1 -0
- package/dist/src/plugins/account-script.js +61 -27
- package/dist/src/plugins/check.js +16 -4
- package/dist/src/plugins/loader.d.ts +8 -1
- package/dist/src/plugins/loader.js +72 -7
- package/dist/src/runtime.d.ts +5 -0
- package/dist/src/runtime.js +27 -5
- package/dist/src/types.d.ts +116 -1
- package/dist/testing/index.d.ts +5 -0
- package/dist/testing/index.js +5 -0
- package/mods/claude/.claude-plugin/plugin.json +7 -0
- package/mods/claude/hooks/hooks.json +1 -0
- package/mods/claude/hooks/register.js +261 -0
- package/package.json +5 -2
- package/schemas/autostart.json +74 -1
- package/schemas/browser-set.json +110 -0
- package/schemas/browser.json +22 -2
- package/schemas/config.json +26 -1
- package/schemas/history-index.json +209 -0
- package/schemas/history-projects.json +133 -0
- package/schemas/history-show.json +275 -0
- package/schemas/history.json +410 -0
- package/schemas/menubar-hotkey.json +139 -0
- package/schemas/menubar-snapshot.json +36 -2
- package/schemas/menubar.json +74 -1
- package/schemas/messages-inbound.json +258 -0
- package/schemas/messages.json +164 -0
- package/schemas/send.json +165 -0
- package/schemas/sessions-copy.json +113 -0
- package/schemas/sessions-open-run.json +97 -0
- package/schemas/sessions.json +237 -0
- package/skills/sideby/SKILL.md +10 -5
- package/skills/sideby/references/accounts.md +11 -0
- package/skills/sideby/references/ai-tasks.md +17 -1
- package/skills/sideby/references/panel.md +6 -4
- package/skills/sideby/references/quota.md +2 -2
- package/skills/sideby/references/sessions.md +79 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Env } from '../../types.ts';
|
|
2
|
+
export declare const MOD_MIN_VERSION = "2.1.287";
|
|
3
|
+
/** The mod folder: three levels above this file in a checkout, four in the published `dist/src`. */
|
|
4
|
+
export declare function claudeModDir(): string | undefined;
|
|
5
|
+
/** `2.1.304` from `claude --version` output such as `2.1.304 (Claude Code)`. */
|
|
6
|
+
export declare function parseClaudeVersion(out: string): string | undefined;
|
|
7
|
+
export declare function versionAtLeast(version: string, min: string): boolean;
|
|
8
|
+
export type VersionRunner = (bin: string, env: Env) => Promise<string>;
|
|
9
|
+
/**
|
|
10
|
+
* The version of the Claude Code at `bin`, asked once per installed binary: the answer is kept in
|
|
11
|
+
* `<state>/cache/claude-version.json` with the binary's real path, size and change time, and asked again when any
|
|
12
|
+
* of them differs (an update replaces the binary or moves the link). Undefined when it cannot be told.
|
|
13
|
+
*/
|
|
14
|
+
export declare function claudeVersion(bin: string, env: Env, stateDir: string, run?: VersionRunner): Promise<string | undefined>;
|
|
15
|
+
/**
|
|
16
|
+
* `args` with `--plugin-dir <mod>` in front: a subcommand (`mcp list`) refuses it after its name, and nothing in front
|
|
17
|
+
* can turn it into prompt text. Or `args` unchanged when the mod does not apply: a headless run, no mod folder in the package, Claude Code not
|
|
18
|
+
* found or older than MOD_MIN_VERSION.
|
|
19
|
+
*/
|
|
20
|
+
export declare function withClaudeMod(args: readonly string[], opts: {
|
|
21
|
+
bin: string | null;
|
|
22
|
+
env: Env;
|
|
23
|
+
stateDir: string;
|
|
24
|
+
dir?: string;
|
|
25
|
+
run?: VersionRunner;
|
|
26
|
+
}): Promise<string[]>;
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// sideby's Claude Code mod (spec §3.32 step 3, ADR-0039): `mods/claude` in the package, loaded with `--plugin-dir`
|
|
2
|
+
// so nothing is written into the Account directory. Claude Code runs mods from 2.1.287 on ("Mods are on by default.
|
|
3
|
+
// In the terminal, use Claude Code v2.1.287 or later", code.claude.com/docs/en/plugins/mods/overview, read
|
|
4
|
+
// 2026-10-20); an older Claude Code would load the folder as a plugin it cannot read, so it gets no flag.
|
|
5
|
+
import { execFile } from 'node:child_process';
|
|
6
|
+
import { existsSync } from 'node:fs';
|
|
7
|
+
import { mkdir, readFile, realpath, stat } from 'node:fs/promises';
|
|
8
|
+
import { dirname, join } from 'node:path';
|
|
9
|
+
import { fileURLToPath } from 'node:url';
|
|
10
|
+
import { writeFileAtomic } from '../../core/fs-safe.js';
|
|
11
|
+
export const MOD_MIN_VERSION = '2.1.287';
|
|
12
|
+
/** The mod folder: three levels above this file in a checkout, four in the published `dist/src`. */
|
|
13
|
+
export function claudeModDir() {
|
|
14
|
+
for (const rel of ['../../../mods/claude', '../../../../mods/claude']) {
|
|
15
|
+
const dir = fileURLToPath(new URL(rel, import.meta.url));
|
|
16
|
+
if (existsSync(join(dir, '.claude-plugin', 'plugin.json')))
|
|
17
|
+
return dir;
|
|
18
|
+
}
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
/** `2.1.304` from `claude --version` output such as `2.1.304 (Claude Code)`. */
|
|
22
|
+
export function parseClaudeVersion(out) {
|
|
23
|
+
return /^\s*(\d+\.\d+\.\d+)/.exec(out)?.[1];
|
|
24
|
+
}
|
|
25
|
+
export function versionAtLeast(version, min) {
|
|
26
|
+
const a = version.split('.').map(Number);
|
|
27
|
+
const b = min.split('.').map(Number);
|
|
28
|
+
for (let i = 0; i < 3; i++) {
|
|
29
|
+
const d = (a[i] ?? 0) - (b[i] ?? 0);
|
|
30
|
+
if (d !== 0)
|
|
31
|
+
return d > 0;
|
|
32
|
+
}
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
const runVersion = (bin, env) => new Promise((resolve, reject) => {
|
|
36
|
+
execFile(bin, ['--version'], { env: env, timeout: 5000 }, (err, out) => err ? reject(err) : resolve(String(out)));
|
|
37
|
+
});
|
|
38
|
+
/**
|
|
39
|
+
* The version of the Claude Code at `bin`, asked once per installed binary: the answer is kept in
|
|
40
|
+
* `<state>/cache/claude-version.json` with the binary's real path, size and change time, and asked again when any
|
|
41
|
+
* of them differs (an update replaces the binary or moves the link). Undefined when it cannot be told.
|
|
42
|
+
*/
|
|
43
|
+
export async function claudeVersion(bin, env, stateDir, run = runVersion) {
|
|
44
|
+
let path;
|
|
45
|
+
let mtimeMs;
|
|
46
|
+
let size;
|
|
47
|
+
try {
|
|
48
|
+
path = await realpath(bin);
|
|
49
|
+
({ mtimeMs, size } = await stat(path));
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
const file = join(stateDir, 'cache', 'claude-version.json');
|
|
55
|
+
try {
|
|
56
|
+
const kept = JSON.parse(await readFile(file, 'utf8'));
|
|
57
|
+
if (kept.format === 1 &&
|
|
58
|
+
kept.path === path &&
|
|
59
|
+
kept.mtimeMs === mtimeMs &&
|
|
60
|
+
kept.size === size &&
|
|
61
|
+
typeof kept.version === 'string')
|
|
62
|
+
return kept.version;
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
// Not kept yet, or unreadable: ask the binary.
|
|
66
|
+
}
|
|
67
|
+
let version;
|
|
68
|
+
try {
|
|
69
|
+
version = parseClaudeVersion(await run(path, env));
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
if (!version)
|
|
75
|
+
return undefined;
|
|
76
|
+
try {
|
|
77
|
+
await mkdir(dirname(file), { recursive: true, mode: 0o700 });
|
|
78
|
+
const entry = { format: 1, path, mtimeMs, size, version };
|
|
79
|
+
await writeFileAtomic(file, `${JSON.stringify(entry)}\n`, 0o600);
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
// Asked again next time.
|
|
83
|
+
}
|
|
84
|
+
return version;
|
|
85
|
+
}
|
|
86
|
+
/** Options after which Claude Code prints and exits, so there is nothing for the mod to show. */
|
|
87
|
+
const HEADLESS = new Set(['-p', '--print']);
|
|
88
|
+
/**
|
|
89
|
+
* `args` with `--plugin-dir <mod>` in front: a subcommand (`mcp list`) refuses it after its name, and nothing in front
|
|
90
|
+
* can turn it into prompt text. Or `args` unchanged when the mod does not apply: a headless run, no mod folder in the package, Claude Code not
|
|
91
|
+
* found or older than MOD_MIN_VERSION.
|
|
92
|
+
*/
|
|
93
|
+
export async function withClaudeMod(args, opts) {
|
|
94
|
+
const end = args.indexOf('--');
|
|
95
|
+
const options = end === -1 ? args : args.slice(0, end);
|
|
96
|
+
if (options.some((a) => HEADLESS.has(a)))
|
|
97
|
+
return [...args];
|
|
98
|
+
const dir = opts.dir ?? claudeModDir();
|
|
99
|
+
if (!dir || !opts.bin)
|
|
100
|
+
return [...args];
|
|
101
|
+
const version = await claudeVersion(opts.bin, opts.env, opts.stateDir, opts.run);
|
|
102
|
+
if (!version || !versionAtLeast(version, MOD_MIN_VERSION))
|
|
103
|
+
return [...args];
|
|
104
|
+
return ['--plugin-dir', dir, ...args];
|
|
105
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type ProcessInfo } from '../../core/live-sessions-procs.ts';
|
|
2
|
+
/** A session record as Claude Code writes it; callers pick only the fields they need. */
|
|
3
|
+
export type SessionRecord = Record<string, unknown>;
|
|
4
|
+
/** Where Claude Code keeps the record of the session process `pid` runs. */
|
|
5
|
+
export declare const sessionRecordPath: (accountDir: string, pid: number) => string;
|
|
6
|
+
/** The record at `file`, or null when it cannot be read or is not a JSON object. */
|
|
7
|
+
export declare function readSessionRecord(file: string): Promise<SessionRecord | null>;
|
|
8
|
+
/** When the record's process started (`procStart`, UTC, in ms), or NaN when it does not say. */
|
|
9
|
+
export declare const recordStart: (rec: SessionRecord) => number;
|
|
10
|
+
/**
|
|
11
|
+
* Whether `proc` is the process that wrote the record: the pid may have been reused since, so the record's start
|
|
12
|
+
* time must be readable and match. Without one the record cannot be tied to the process.
|
|
13
|
+
*/
|
|
14
|
+
export declare function recordRunsOn(rec: SessionRecord, proc: ProcessInfo | undefined): proc is ProcessInfo;
|
|
15
|
+
/** How far back from the end of a transcript sideby looks; Claude Code repeats what it needs as it goes. */
|
|
16
|
+
export declare const TAIL_BYTES: number;
|
|
17
|
+
/**
|
|
18
|
+
* The lines of the last `TAIL_BYTES` of a file that contain `needle`. The first line is dropped only when the window
|
|
19
|
+
* cuts it, which the byte just before the window tells (anything but a newline).
|
|
20
|
+
*/
|
|
21
|
+
export declare function tailLinesWith(file: string, needle: string): Promise<string[]>;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// Claude Code's own session files (spec §3.32), read one way wherever sideby needs them: the per-process record
|
|
2
|
+
// `<dir>/sessions/<pid>.json` that Live Sessions lists and Messages delivers through, and the tail of a session
|
|
3
|
+
// transcript (title, permission mode, read receipts). Tried with Claude Code 2.1.304 on 2026-10-20.
|
|
4
|
+
import { open, readFile } from 'node:fs/promises';
|
|
5
|
+
import { join } from 'node:path';
|
|
6
|
+
import { statOrNull } from '../../core/fs-safe.js';
|
|
7
|
+
import { parseLstart, sameStart } from '../../core/live-sessions-procs.js';
|
|
8
|
+
/** Where Claude Code keeps the record of the session process `pid` runs. */
|
|
9
|
+
export const sessionRecordPath = (accountDir, pid) => join(accountDir, 'sessions', `${pid}.json`);
|
|
10
|
+
/** The record at `file`, or null when it cannot be read or is not a JSON object. */
|
|
11
|
+
export async function readSessionRecord(file) {
|
|
12
|
+
try {
|
|
13
|
+
const parsed = JSON.parse(await readFile(file, 'utf8'));
|
|
14
|
+
return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed : null;
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/** When the record's process started (`procStart`, UTC, in ms), or NaN when it does not say. */
|
|
21
|
+
export const recordStart = (rec) => parseLstart(typeof rec.procStart === 'string' ? rec.procStart : '');
|
|
22
|
+
/**
|
|
23
|
+
* Whether `proc` is the process that wrote the record: the pid may have been reused since, so the record's start
|
|
24
|
+
* time must be readable and match. Without one the record cannot be tied to the process.
|
|
25
|
+
*/
|
|
26
|
+
export function recordRunsOn(rec, proc) {
|
|
27
|
+
const started = recordStart(rec);
|
|
28
|
+
return proc !== undefined && !Number.isNaN(started) && sameStart(proc, started);
|
|
29
|
+
}
|
|
30
|
+
/** How far back from the end of a transcript sideby looks; Claude Code repeats what it needs as it goes. */
|
|
31
|
+
export const TAIL_BYTES = 256 * 1024;
|
|
32
|
+
/**
|
|
33
|
+
* The lines of the last `TAIL_BYTES` of a file that contain `needle`. The first line is dropped only when the window
|
|
34
|
+
* cuts it, which the byte just before the window tells (anything but a newline).
|
|
35
|
+
*/
|
|
36
|
+
export async function tailLinesWith(file, needle) {
|
|
37
|
+
const st = await statOrNull(file);
|
|
38
|
+
if (!st?.isFile())
|
|
39
|
+
return [];
|
|
40
|
+
const fh = await open(file, 'r');
|
|
41
|
+
try {
|
|
42
|
+
const len = Math.min(st.size, TAIL_BYTES);
|
|
43
|
+
const cut = len < st.size ? 1 : 0;
|
|
44
|
+
const buf = Buffer.alloc(len + cut);
|
|
45
|
+
await fh.read(buf, 0, len + cut, st.size - len - cut);
|
|
46
|
+
const lines = buf.subarray(cut).toString('utf8').split('\n');
|
|
47
|
+
if (cut && buf[0] !== 0x0a)
|
|
48
|
+
lines.shift();
|
|
49
|
+
return lines.filter((l) => l.includes(needle));
|
|
50
|
+
}
|
|
51
|
+
finally {
|
|
52
|
+
await fh.close();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Delivery, DeliveryRequest } from '../messaging.ts';
|
|
2
|
+
export declare const QUEUE_TIMEOUT_MS = 20000;
|
|
3
|
+
/** Runs `codex <args>` and resolves to its exit code; its output is ignored, as it may hold account details. */
|
|
4
|
+
export declare function runCodex(args: readonly string[], env: NodeJS.ProcessEnv, timeoutMs: number): Promise<number>;
|
|
5
|
+
export declare function deliverCodex(req: DeliveryRequest): Promise<Delivery>;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// Delivering a Message into a running Codex session (spec §3.32 step 4, ADR-0040): Codex's own `codex queue`, which
|
|
2
|
+
// queues text for an existing session; the session takes it as its next turn once the current one ends, or within
|
|
3
|
+
// about 10 s when it is idle (codex-cli 0.163.0). Codex reads its own login; sideby reads no credential. Only the
|
|
4
|
+
// paste line goes on the command line, never the Message's text: other users on the machine can see arguments.
|
|
5
|
+
import { spawn } from 'node:child_process';
|
|
6
|
+
import { tmpdir } from 'node:os';
|
|
7
|
+
import { accountEnv } from '../../core/launch.js';
|
|
8
|
+
export const QUEUE_TIMEOUT_MS = 20_000;
|
|
9
|
+
/** How long the queue process gets to end after SIGTERM before it is sent SIGKILL. */
|
|
10
|
+
const KILL_GRACE_MS = 2_000;
|
|
11
|
+
/** Runs `codex <args>` and resolves to its exit code; its output is ignored, as it may hold account details. */
|
|
12
|
+
export function runCodex(args, env, timeoutMs) {
|
|
13
|
+
return new Promise((resolve, reject) => {
|
|
14
|
+
const child = spawn('codex', args, { cwd: tmpdir(), env, stdio: 'ignore' });
|
|
15
|
+
const timer = setTimeout(() => {
|
|
16
|
+
// Only this short-lived queue process is ended (by its own pid), never a Codex session.
|
|
17
|
+
child.kill('SIGTERM');
|
|
18
|
+
const pid = child.pid;
|
|
19
|
+
setTimeout(() => {
|
|
20
|
+
if (child.exitCode === null && child.signalCode === null && pid !== undefined)
|
|
21
|
+
try {
|
|
22
|
+
process.kill(pid, 'SIGKILL');
|
|
23
|
+
}
|
|
24
|
+
catch { }
|
|
25
|
+
}, KILL_GRACE_MS).unref();
|
|
26
|
+
reject(new Error('Codex did not answer in time'));
|
|
27
|
+
}, timeoutMs);
|
|
28
|
+
child.once('error', (err) => {
|
|
29
|
+
clearTimeout(timer);
|
|
30
|
+
reject(new Error(err.code === 'ENOENT' ? 'Codex is not on PATH' : 'Codex could not start'));
|
|
31
|
+
});
|
|
32
|
+
child.once('close', (code) => {
|
|
33
|
+
clearTimeout(timer);
|
|
34
|
+
resolve(code ?? 1);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
export async function deliverCodex(req) {
|
|
39
|
+
const { rt, to, acct, pid, procs, deps } = req;
|
|
40
|
+
const family = rt.families.get('codex');
|
|
41
|
+
if (!family)
|
|
42
|
+
return { status: 'paste-needed', note: 'the Codex plugin is not loaded; paste the line into its tab' };
|
|
43
|
+
// Looked at again right before queueing: the session may have ended, or its process moved on to another one.
|
|
44
|
+
if (!(await procs.get(pid)) || (to.transcript && !(await procs.openFiles(pid)).includes(to.transcript)))
|
|
45
|
+
return {
|
|
46
|
+
status: 'paste-needed',
|
|
47
|
+
note: `${to.label} stopped running; start it again, then paste this line into its terminal tab`,
|
|
48
|
+
};
|
|
49
|
+
const args = [
|
|
50
|
+
...(family.defaultArgs?.(acct, []) ?? []),
|
|
51
|
+
'queue',
|
|
52
|
+
'--thread',
|
|
53
|
+
to.sessionId,
|
|
54
|
+
'--message',
|
|
55
|
+
req.pasteText,
|
|
56
|
+
];
|
|
57
|
+
let code;
|
|
58
|
+
try {
|
|
59
|
+
code = await (deps.runCodex ?? runCodex)(args, accountEnv(family, acct, rt.env), deps.timeoutMs ?? QUEUE_TIMEOUT_MS);
|
|
60
|
+
}
|
|
61
|
+
catch (err) {
|
|
62
|
+
return {
|
|
63
|
+
status: 'paste-needed',
|
|
64
|
+
note: `sideby could not send it to ${to.label} (${err.message}); paste the line into its terminal tab`,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
if (code !== 0)
|
|
68
|
+
return {
|
|
69
|
+
status: 'paste-needed',
|
|
70
|
+
note: `Codex could not take the message (\`codex queue\` exited with ${code}; requires Codex 0.163+ and a saved session). Paste the line into its terminal tab`,
|
|
71
|
+
};
|
|
72
|
+
return {
|
|
73
|
+
status: 'delivered',
|
|
74
|
+
note: `sent to ${to.label}; Codex reads it when its current turn ends`,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { HistoryReader } from '../history.ts';
|
|
2
|
+
/**
|
|
3
|
+
* A person started the Session in Codex itself (the TUI, the desktop app or an editor), not `codex exec`, an
|
|
4
|
+
* automation or another agent driving Codex's app server.
|
|
5
|
+
*/
|
|
6
|
+
export declare function codexInteractive(meta: Record<string, unknown>): boolean;
|
|
7
|
+
export declare const codexHistory: HistoryReader;
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
// Codex's Session History (spec §3.33): one `sessions/YYYY/MM/DD/rollout-<time>-<id>.jsonl` per Session (tried with
|
|
2
|
+
// Codex CLI 0.163.0 on 2026-10-20). The first line, `session_meta`, gives the id, working directory, branch and how the
|
|
3
|
+
// Session was started; only those fields are read from it, never its account ids or instructions. Titles live apart,
|
|
4
|
+
// in `session_index.jsonl` (`thread_name`).
|
|
5
|
+
import { join } from 'node:path';
|
|
6
|
+
import { blockText, dirEntries, fileLines, historyFile, isoTime, jsonText, parseRecord, SessionText, str, titleFromText, typedText, } from '../shared/history-text.js';
|
|
7
|
+
const ROLLOUT = /^rollout-.+\.jsonl$/;
|
|
8
|
+
const WANTED = /"type":"(session_meta|turn_context|response_item)"/;
|
|
9
|
+
// Tool output and reasoning items, never read: their (often huge) lines are not parsed.
|
|
10
|
+
const SKIPPED = /"type":"(function_call_output|custom_tool_call_output|local_shell_call_output|reasoning)"/;
|
|
11
|
+
/**
|
|
12
|
+
* A person started the Session in Codex itself (the TUI, the desktop app or an editor), not `codex exec`, an
|
|
13
|
+
* automation or another agent driving Codex's app server.
|
|
14
|
+
*/
|
|
15
|
+
export function codexInteractive(meta) {
|
|
16
|
+
const source = meta.source;
|
|
17
|
+
if (source !== 'cli' && source !== 'vscode')
|
|
18
|
+
return false;
|
|
19
|
+
if (meta.thread_source === 'automation')
|
|
20
|
+
return false;
|
|
21
|
+
const originator = str(meta.originator);
|
|
22
|
+
return !originator || /codex/i.test(originator);
|
|
23
|
+
}
|
|
24
|
+
async function* rollouts(dir) {
|
|
25
|
+
for (const e of await dirEntries(dir)) {
|
|
26
|
+
const p = join(dir, e.name);
|
|
27
|
+
if (e.isDirectory())
|
|
28
|
+
yield* rollouts(p);
|
|
29
|
+
else if (e.isFile() && ROLLOUT.test(e.name))
|
|
30
|
+
yield p;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
// A reply the model wrote for the person (`phase` commentary or final_answer). A message on another `channel` than
|
|
34
|
+
// `final` (a reasoning channel) is not one, so it never reaches the History Index.
|
|
35
|
+
const isReply = (m) => m.channel === undefined || m.channel === 'final';
|
|
36
|
+
export const codexHistory = {
|
|
37
|
+
family: 'codex',
|
|
38
|
+
async list(account) {
|
|
39
|
+
const out = [];
|
|
40
|
+
for await (const p of rollouts(join(account.dir, 'sessions'))) {
|
|
41
|
+
const f = await historyFile(p);
|
|
42
|
+
if (f)
|
|
43
|
+
out.push(f);
|
|
44
|
+
}
|
|
45
|
+
return out;
|
|
46
|
+
},
|
|
47
|
+
async parse(file) {
|
|
48
|
+
let sessionId;
|
|
49
|
+
let cwd;
|
|
50
|
+
let branch;
|
|
51
|
+
let model;
|
|
52
|
+
let startedAt;
|
|
53
|
+
let updatedAt;
|
|
54
|
+
let interactive = false;
|
|
55
|
+
const text = new SessionText();
|
|
56
|
+
for await (const line of fileLines(file.path)) {
|
|
57
|
+
if (!WANTED.test(line) || SKIPPED.test(line))
|
|
58
|
+
continue;
|
|
59
|
+
const rec = parseRecord(line);
|
|
60
|
+
if (!rec)
|
|
61
|
+
continue;
|
|
62
|
+
const at = isoTime(rec.timestamp);
|
|
63
|
+
if (at) {
|
|
64
|
+
startedAt ??= at;
|
|
65
|
+
updatedAt = at;
|
|
66
|
+
}
|
|
67
|
+
const p = (rec.payload ?? {});
|
|
68
|
+
if (rec.type === 'session_meta') {
|
|
69
|
+
sessionId ??= str(p.id) ?? str(p.session_id);
|
|
70
|
+
cwd ??= str(p.cwd);
|
|
71
|
+
branch ??= str(p.git?.branch);
|
|
72
|
+
interactive = codexInteractive(p);
|
|
73
|
+
}
|
|
74
|
+
else if (rec.type === 'turn_context') {
|
|
75
|
+
model = str(p.model) ?? model;
|
|
76
|
+
}
|
|
77
|
+
else if (p.type === 'message') {
|
|
78
|
+
const item = p;
|
|
79
|
+
if (item.role === 'user') {
|
|
80
|
+
const t = typedText(blockText(item.content, ['input_text']));
|
|
81
|
+
if (t)
|
|
82
|
+
text.prompt(t);
|
|
83
|
+
}
|
|
84
|
+
else if (item.role === 'assistant' && isReply(item)) {
|
|
85
|
+
const t = blockText(item.content, ['output_text']).trim();
|
|
86
|
+
if (t)
|
|
87
|
+
text.reply(t);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
if (!sessionId || !text.count)
|
|
92
|
+
return undefined;
|
|
93
|
+
const s = {
|
|
94
|
+
sessionId,
|
|
95
|
+
titleFrom: text.first ? 'prompt' : 'none',
|
|
96
|
+
messages: text.count,
|
|
97
|
+
script: !interactive,
|
|
98
|
+
prompts: text.prompts,
|
|
99
|
+
replies: text.replies,
|
|
100
|
+
...(text.cut ? { cut: true } : {}),
|
|
101
|
+
};
|
|
102
|
+
if (text.first)
|
|
103
|
+
s.title = titleFromText(text.first);
|
|
104
|
+
if (cwd)
|
|
105
|
+
s.cwd = cwd;
|
|
106
|
+
if (branch)
|
|
107
|
+
s.branch = branch;
|
|
108
|
+
if (model)
|
|
109
|
+
s.model = model;
|
|
110
|
+
if (startedAt)
|
|
111
|
+
s.startedAt = startedAt;
|
|
112
|
+
if (updatedAt)
|
|
113
|
+
s.updatedAt = updatedAt;
|
|
114
|
+
return s;
|
|
115
|
+
},
|
|
116
|
+
async *messages(path) {
|
|
117
|
+
for await (const line of fileLines(path)) {
|
|
118
|
+
if (!line.includes('"type":"response_item"') || SKIPPED.test(line))
|
|
119
|
+
continue;
|
|
120
|
+
const rec = parseRecord(line);
|
|
121
|
+
const p = (rec?.payload ?? {});
|
|
122
|
+
const at = isoTime(rec?.timestamp);
|
|
123
|
+
const when = at ? { at } : {};
|
|
124
|
+
if (p.type === 'message' && p.role === 'user') {
|
|
125
|
+
const text = typedText(blockText(p.content, ['input_text']));
|
|
126
|
+
if (text)
|
|
127
|
+
yield { role: 'user', text, ...when };
|
|
128
|
+
}
|
|
129
|
+
else if (p.type === 'message' && p.role === 'assistant' && isReply(p)) {
|
|
130
|
+
const text = blockText(p.content, ['output_text']).trim();
|
|
131
|
+
if (text)
|
|
132
|
+
yield { role: 'assistant', text, ...when };
|
|
133
|
+
}
|
|
134
|
+
else if (p.type === 'function_call' ||
|
|
135
|
+
p.type === 'custom_tool_call' ||
|
|
136
|
+
p.type === 'local_shell_call') {
|
|
137
|
+
const tool = str(p.name) ?? 'shell';
|
|
138
|
+
yield { role: 'tool', text: tool, tool, ...when };
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
},
|
|
142
|
+
async *toolText(path) {
|
|
143
|
+
for await (const line of fileLines(path)) {
|
|
144
|
+
if (!line.includes('"type":"response_item"'))
|
|
145
|
+
continue;
|
|
146
|
+
const p = (parseRecord(line)?.payload ?? {});
|
|
147
|
+
if (p.type === 'function_call')
|
|
148
|
+
yield jsonText(p.arguments);
|
|
149
|
+
else if (p.type === 'custom_tool_call')
|
|
150
|
+
yield jsonText(p.input);
|
|
151
|
+
else if (p.type === 'local_shell_call')
|
|
152
|
+
yield jsonText(p.action);
|
|
153
|
+
else if (/^(function_call|custom_tool_call|local_shell_call)_output$/.test(String(p.type)))
|
|
154
|
+
yield jsonText(p.output);
|
|
155
|
+
}
|
|
156
|
+
},
|
|
157
|
+
async titles(account) {
|
|
158
|
+
const out = new Map();
|
|
159
|
+
try {
|
|
160
|
+
for await (const line of fileLines(join(account.dir, 'session_index.jsonl'))) {
|
|
161
|
+
const rec = parseRecord(line);
|
|
162
|
+
const id = str(rec?.id);
|
|
163
|
+
const name = str(rec?.thread_name)?.trim();
|
|
164
|
+
if (id && name)
|
|
165
|
+
out.set(id, titleFromText(name));
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
catch (err) {
|
|
169
|
+
if (err.code !== 'ENOENT')
|
|
170
|
+
throw err;
|
|
171
|
+
}
|
|
172
|
+
return out;
|
|
173
|
+
},
|
|
174
|
+
};
|
|
@@ -6,6 +6,7 @@ import { nestedFileWrittenAt, resumeSubcommandSession } from '../shared/sessions
|
|
|
6
6
|
import { topLevelTomlString } from '../shared/toml.js';
|
|
7
7
|
import { codexHandoff } from './handoff.js';
|
|
8
8
|
import { codexModelChoice } from './model.js';
|
|
9
|
+
import { codexQuotaFetch } from './quota.js';
|
|
9
10
|
import { readCodexQuota, readCodexUsage } from './rollout.js';
|
|
10
11
|
import { readCodexTuning } from './tuning.js';
|
|
11
12
|
const FILE_STORE_KEY = 'cli_auth_credentials_store';
|
|
@@ -80,6 +81,7 @@ export const codexFamily = {
|
|
|
80
81
|
modelChoice: codexModelChoice,
|
|
81
82
|
identity: codexIdentity,
|
|
82
83
|
readQuota: (account) => readCodexQuota(account.dir),
|
|
84
|
+
fetchQuota: codexQuotaFetch,
|
|
83
85
|
readUsage: (account, ctx) => readCodexUsage(account.dir, ctx.now),
|
|
84
86
|
readTuning: readCodexTuning,
|
|
85
87
|
resumedSession: resumeSubcommandSession,
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Account } from '../../types.ts';
|
|
2
|
+
import type { LiveSessionReader } from '../live-sessions.ts';
|
|
3
|
+
/**
|
|
4
|
+
* The subcommand of a `codex` process (`exec`, `app-server`), or undefined for a plain session or when it cannot
|
|
5
|
+
* be told. `argv` are the arguments after the program; `exact` says they come word by word from the system. From the
|
|
6
|
+
* flattened `ps` line a word could be the start of a quoted prompt (`codex "mcp integration needs fixing"`), so a
|
|
7
|
+
* server word counts there only when nothing but options follows it.
|
|
8
|
+
*/
|
|
9
|
+
export declare function codexSubcommand(argv: readonly string[], exact: boolean): string | undefined;
|
|
10
|
+
/** Whether a running `codex` with this `CODEX_HOME` belongs to the Account. */
|
|
11
|
+
export declare const codexHomeIs: (account: Account, codexHome: string | undefined) => boolean;
|
|
12
|
+
export declare const codexLiveSessions: LiveSessionReader;
|