@ziptie-soft/orca-local-decider 0.0.0 → 1.0.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/CHANGELOG.md +26 -0
- package/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +346 -1
- package/images/cover.png +0 -0
- package/images/icon.png +0 -0
- package/orca-plugin.json +218 -0
- package/package.json +45 -5
- package/src/agent-terminals.ts +88 -0
- package/src/bounded.ts +22 -0
- package/src/breaker.ts +28 -0
- package/src/core/canonical.ts +24 -0
- package/src/core/guard.ts +503 -0
- package/src/core/ids.ts +2 -0
- package/src/core/input-cap.ts +71 -0
- package/src/core/permission-input.ts +42 -0
- package/src/core/shell.ts +586 -0
- package/src/core/subject.ts +244 -0
- package/src/core/truncate.ts +203 -0
- package/src/gate.ts +155 -0
- package/src/gateway.ts +289 -0
- package/src/guard-result.ts +28 -0
- package/src/hook-watch.ts +76 -0
- package/src/launch-command.ts +274 -0
- package/src/launcher/launch.ts +892 -0
- package/src/launcher/local-token.ts +26 -0
- package/src/launcher/socket-path.ts +24 -0
- package/src/launcher/transport.ts +141 -0
- package/src/orca-types.ts +107 -0
- package/src/panel/model.ts +205 -0
- package/src/panel/orca-panel.d.ts +90 -0
- package/src/panel/panel.tsx +278 -0
- package/src/panel-api.ts +83 -0
- package/src/panel-protocol.ts +86 -0
- package/src/routing.ts +204 -0
- package/src/send-guard.ts +43 -0
- package/src/settings.ts +82 -0
- package/src/status.ts +146 -0
- package/src/turn-key.ts +13 -0
- package/src/turn-outcome.ts +199 -0
- package/src/worker.ts +521 -0
- package/src/workspaces.ts +87 -0
- package/src/wrapper-env.ts +19 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reads the gateway's local control token from its config directory. The token is never logged:
|
|
3
|
+
* callers send it only to a loopback URL or the Unix socket, and never store or show it.
|
|
4
|
+
*/
|
|
5
|
+
import { readFileSync } from 'node:fs';
|
|
6
|
+
import { homedir } from 'node:os';
|
|
7
|
+
import { join } from 'node:path';
|
|
8
|
+
|
|
9
|
+
/** The gateway's config directory: `LOCAL_DECIDER_CONFIG_DIR`, then XDG, then `~/.config`. */
|
|
10
|
+
export function configDir(env: NodeJS.ProcessEnv): string {
|
|
11
|
+
const explicit = env['LOCAL_DECIDER_CONFIG_DIR'];
|
|
12
|
+
if (explicit !== undefined && explicit !== '') return explicit;
|
|
13
|
+
const xdg = env['XDG_CONFIG_HOME'];
|
|
14
|
+
return join(xdg !== undefined && xdg !== '' ? xdg : join(homedir(), '.config'), 'local-decider');
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** The control token, or undefined when the file is missing or malformed. */
|
|
18
|
+
export function readControlToken(env: NodeJS.ProcessEnv): string | undefined {
|
|
19
|
+
try {
|
|
20
|
+
const token = readFileSync(join(configDir(env), 'control-token'), 'utf8').trim();
|
|
21
|
+
// Same format as the gateway's `isLocalToken`.
|
|
22
|
+
return /^[A-Za-z0-9_-]{43}$/.test(token) ? token : undefined;
|
|
23
|
+
} catch {
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { homedir } from 'node:os';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
|
|
4
|
+
/** Shared client rule: POSIX sockets are disabled when sun_path would exceed 100 bytes. */
|
|
5
|
+
export function gatewaySocketPath(
|
|
6
|
+
runDir: string,
|
|
7
|
+
platform: NodeJS.Platform = process.platform,
|
|
8
|
+
): string | null {
|
|
9
|
+
if (platform === 'win32') return null;
|
|
10
|
+
const path = join(runDir, 'gateway.sock');
|
|
11
|
+
return Buffer.byteLength(path) > 100 ? null : path;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Resolve the same runtime socket from a client's sanitized environment. */
|
|
15
|
+
export function gatewaySocketPathForEnv(
|
|
16
|
+
env: NodeJS.ProcessEnv,
|
|
17
|
+
home = homedir(),
|
|
18
|
+
platform: NodeJS.Platform = process.platform,
|
|
19
|
+
): string | null {
|
|
20
|
+
const data =
|
|
21
|
+
env['LOCAL_DECIDER_HOME'] ??
|
|
22
|
+
join(env['XDG_DATA_HOME'] ?? join(home, '.local', 'share'), 'local-decider');
|
|
23
|
+
return gatewaySocketPath(join(data, 'run'), platform);
|
|
24
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { request as httpRequest } from 'node:http';
|
|
2
|
+
import type { IncomingHttpHeaders, IncomingMessage, RequestOptions } from 'node:http';
|
|
3
|
+
|
|
4
|
+
const MAX_RESPONSE_BYTES = 8 * 1024 * 1024;
|
|
5
|
+
|
|
6
|
+
type FetchInput = string | URL | Request;
|
|
7
|
+
|
|
8
|
+
function requestUrl(input: FetchInput): URL {
|
|
9
|
+
if (input instanceof Request) return new URL(input.url);
|
|
10
|
+
return input instanceof URL ? input : new URL(input);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function headersOf(headers: RequestInit['headers']): Record<string, string> {
|
|
14
|
+
const out: Record<string, string> = {};
|
|
15
|
+
if (headers === undefined) return out;
|
|
16
|
+
new Headers(headers).forEach((value, key) => {
|
|
17
|
+
out[key] = value;
|
|
18
|
+
});
|
|
19
|
+
return out;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async function bodyOf(input: FetchInput, init: RequestInit): Promise<Buffer | undefined> {
|
|
23
|
+
const body =
|
|
24
|
+
init.body !== undefined ? init.body : input instanceof Request ? input.body : undefined;
|
|
25
|
+
if (body === undefined || body === null) return undefined;
|
|
26
|
+
if (typeof body === 'string') return Buffer.from(body);
|
|
27
|
+
if (body instanceof Uint8Array) return Buffer.from(body);
|
|
28
|
+
if (body instanceof ArrayBuffer) return Buffer.from(body);
|
|
29
|
+
return Buffer.from(await new Response(body).arrayBuffer());
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function responseHeaders(headers: IncomingHttpHeaders): Headers {
|
|
33
|
+
const result = new Headers();
|
|
34
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
35
|
+
if (value === undefined) continue;
|
|
36
|
+
for (const item of Array.isArray(value) ? value : [value]) result.append(key, item);
|
|
37
|
+
}
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function abortError(): Error {
|
|
42
|
+
return new DOMException('The operation was aborted', 'AbortError');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function readResponse(
|
|
46
|
+
response: IncomingMessage,
|
|
47
|
+
signal: AbortSignal | null,
|
|
48
|
+
redirect: string,
|
|
49
|
+
): Promise<Response> {
|
|
50
|
+
return new Promise((resolve, reject) => {
|
|
51
|
+
const chunks: Buffer[] = [];
|
|
52
|
+
let size = 0;
|
|
53
|
+
const status = response.statusCode ?? 500;
|
|
54
|
+
if (redirect === 'error' && status >= 300 && status < 400) {
|
|
55
|
+
response.destroy();
|
|
56
|
+
reject(new TypeError('redirects are not supported by socketFetch'));
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
response.on('data', (chunk: Buffer | string) => {
|
|
60
|
+
const bytes = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
|
61
|
+
size += bytes.length;
|
|
62
|
+
if (size > MAX_RESPONSE_BYTES) {
|
|
63
|
+
response.destroy(new Error('socketFetch response exceeded 8 MiB'));
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
chunks.push(bytes);
|
|
67
|
+
});
|
|
68
|
+
response.once('error', reject);
|
|
69
|
+
response.once('end', () => {
|
|
70
|
+
if (signal?.aborted) {
|
|
71
|
+
reject(abortError());
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
const body = status === 204 || status === 304 ? null : Buffer.concat(chunks);
|
|
75
|
+
resolve(
|
|
76
|
+
new Response(body, {
|
|
77
|
+
status,
|
|
78
|
+
...(response.statusMessage === undefined ? {} : { statusText: response.statusMessage }),
|
|
79
|
+
headers: responseHeaders(response.headers),
|
|
80
|
+
}),
|
|
81
|
+
);
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function optionsFor(
|
|
87
|
+
url: URL,
|
|
88
|
+
init: RequestInit,
|
|
89
|
+
input: FetchInput,
|
|
90
|
+
headers: Record<string, string>,
|
|
91
|
+
socketPath: string,
|
|
92
|
+
): RequestOptions {
|
|
93
|
+
return {
|
|
94
|
+
socketPath,
|
|
95
|
+
path: `${url.pathname}${url.search}`,
|
|
96
|
+
method: init.method ?? (input instanceof Request ? input.method : 'GET'),
|
|
97
|
+
headers,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Fetch-compatible HTTP transport over a private Unix socket. It never follows redirects. */
|
|
102
|
+
export function socketFetch(socketPath: string): typeof fetch {
|
|
103
|
+
return async (input: FetchInput, init: RequestInit = {}) => {
|
|
104
|
+
const url = requestUrl(input);
|
|
105
|
+
const headers = headersOf(input instanceof Request ? input.headers : undefined);
|
|
106
|
+
Object.assign(headers, headersOf(init.headers));
|
|
107
|
+
const body = await bodyOf(input, init);
|
|
108
|
+
if (
|
|
109
|
+
body !== undefined &&
|
|
110
|
+
!Object.keys(headers).some((key) => key.toLowerCase() === 'content-length')
|
|
111
|
+
) {
|
|
112
|
+
headers['content-length'] = String(body.length);
|
|
113
|
+
}
|
|
114
|
+
const signal =
|
|
115
|
+
init.signal === undefined ? (input instanceof Request ? input.signal : null) : init.signal;
|
|
116
|
+
if (signal?.aborted) throw abortError();
|
|
117
|
+
const req = httpRequest(optionsFor(url, init, input, headers, socketPath));
|
|
118
|
+
const abort = (): void => {
|
|
119
|
+
req.destroy(abortError());
|
|
120
|
+
};
|
|
121
|
+
signal?.addEventListener('abort', abort, { once: true });
|
|
122
|
+
try {
|
|
123
|
+
if (signal?.aborted) throw abortError();
|
|
124
|
+
const response = new Promise<Response>((resolve, reject) => {
|
|
125
|
+
req.once('error', reject);
|
|
126
|
+
req.once('response', (res) => {
|
|
127
|
+
void readResponse(
|
|
128
|
+
res,
|
|
129
|
+
signal,
|
|
130
|
+
init.redirect ?? (input instanceof Request ? input.redirect : 'follow'),
|
|
131
|
+
).then(resolve, reject);
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
if (body !== undefined) req.write(body);
|
|
135
|
+
req.end();
|
|
136
|
+
return await response;
|
|
137
|
+
} finally {
|
|
138
|
+
signal?.removeEventListener('abort', abort);
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The parts of Orca's worker API (`orca/worker`) this plugin uses, written out as structural types
|
|
3
|
+
* so the package typechecks without the generated `.orca/types` (which `orca plugin types` writes
|
|
4
|
+
* and git ignores). Orca passes the real object to `activate(orca)`.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export interface Disposable {
|
|
8
|
+
dispose(): void;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** `agent.tool.before`: an agent in an Orca terminal is about to run a tool. */
|
|
12
|
+
export interface ToolBeforeEvent {
|
|
13
|
+
readonly agent: string;
|
|
14
|
+
readonly workspaceId: string | null;
|
|
15
|
+
readonly terminalId?: string;
|
|
16
|
+
readonly tool: string;
|
|
17
|
+
readonly input: unknown;
|
|
18
|
+
readonly callId?: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Returning nothing passes the call on unchanged; `{ deny }` refuses it. */
|
|
22
|
+
export type ToolBeforeResult = { readonly deny: string } | undefined;
|
|
23
|
+
|
|
24
|
+
/** `agent.launch.before`: Orca is about to start an agent CLI in a terminal. */
|
|
25
|
+
export interface LaunchBeforeEvent {
|
|
26
|
+
readonly agent: string;
|
|
27
|
+
readonly workspaceId: string | null;
|
|
28
|
+
readonly workspaceKind: 'worktree' | 'folder' | null;
|
|
29
|
+
readonly terminalId?: string;
|
|
30
|
+
readonly command: string;
|
|
31
|
+
readonly env: Readonly<Record<string, string>>;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** What `next(event)` returns: returning it launches with the (changed) event. */
|
|
35
|
+
export interface HookContinue {
|
|
36
|
+
readonly __hookContinue: true;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export type LaunchBeforeResult = HookContinue | { readonly deny: string } | undefined;
|
|
40
|
+
|
|
41
|
+
/** `terminal.send`: Orca, its CLI or a plugin is about to type text into a terminal. */
|
|
42
|
+
export interface TerminalSendEvent {
|
|
43
|
+
readonly terminalId: string;
|
|
44
|
+
readonly text: string;
|
|
45
|
+
readonly enter: boolean;
|
|
46
|
+
readonly inputKind: 'driving' | 'orchestration' | 'launch';
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** `agent.status.changed`: an agent's status changed; `terminalId` names its terminal, if any. */
|
|
50
|
+
export interface AgentStatusEvent {
|
|
51
|
+
readonly workspaceId?: string | null;
|
|
52
|
+
/** Host API 1.7+: the id `workspace.list` gives, which `agents.list` takes. */
|
|
53
|
+
readonly workspaceListId?: string;
|
|
54
|
+
readonly paneKey?: string;
|
|
55
|
+
readonly terminalId?: string;
|
|
56
|
+
readonly title?: string;
|
|
57
|
+
readonly state?: 'working' | 'blocked' | 'waiting' | 'done';
|
|
58
|
+
readonly receivedAt?: number;
|
|
59
|
+
/** `outcome` says why the main agent stopped; a person's stop is `cancellation` or the like. */
|
|
60
|
+
readonly mainAgent?: { readonly state?: string; readonly outcome?: string };
|
|
61
|
+
readonly sessionId?: string;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface OrcaWorker {
|
|
65
|
+
readonly hooks: {
|
|
66
|
+
on(
|
|
67
|
+
hook: 'agent.tool.before',
|
|
68
|
+
handler: (event: ToolBeforeEvent) => ToolBeforeResult | Promise<ToolBeforeResult>,
|
|
69
|
+
): Disposable;
|
|
70
|
+
on(
|
|
71
|
+
hook: 'agent.launch.before',
|
|
72
|
+
handler: (
|
|
73
|
+
event: LaunchBeforeEvent,
|
|
74
|
+
next: (event?: LaunchBeforeEvent) => HookContinue,
|
|
75
|
+
) => LaunchBeforeResult | Promise<LaunchBeforeResult>,
|
|
76
|
+
): Disposable;
|
|
77
|
+
on(
|
|
78
|
+
hook: 'terminal.send',
|
|
79
|
+
handler: (
|
|
80
|
+
event: TerminalSendEvent,
|
|
81
|
+
) => { readonly deny: string } | undefined | Promise<{ readonly deny: string } | undefined>,
|
|
82
|
+
): Disposable;
|
|
83
|
+
};
|
|
84
|
+
/** Absent in a host without worker events (and in the test fakes that do not need them). */
|
|
85
|
+
readonly events?: {
|
|
86
|
+
on(event: 'agent.status.changed', handler: (event: AgentStatusEvent) => void): Disposable;
|
|
87
|
+
};
|
|
88
|
+
readonly commands: {
|
|
89
|
+
register(commandId: string, handler: (args: unknown) => unknown): Disposable;
|
|
90
|
+
};
|
|
91
|
+
readonly settings: {
|
|
92
|
+
get(): Promise<Record<string, unknown>>;
|
|
93
|
+
onDidChange(listener: (change: { readonly key?: string }) => void): Disposable;
|
|
94
|
+
};
|
|
95
|
+
readonly host: {
|
|
96
|
+
call(method: string, params?: unknown): Promise<unknown>;
|
|
97
|
+
};
|
|
98
|
+
/** Absent in a host without panels (and in the test fakes that do not need them). */
|
|
99
|
+
readonly panels?: {
|
|
100
|
+
handle(
|
|
101
|
+
name: string,
|
|
102
|
+
handler: (input: unknown, context: { readonly signal: AbortSignal }) => unknown,
|
|
103
|
+
): Disposable;
|
|
104
|
+
post(event: string, payload?: unknown): void;
|
|
105
|
+
};
|
|
106
|
+
log(message: string): void;
|
|
107
|
+
}
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The panel's view model: pure functions from the worker's `PanelState` to plain rows. No `orca`
|
|
3
|
+
* import, so node --test covers it; `panel.tsx` only draws what these return.
|
|
4
|
+
*/
|
|
5
|
+
import type { PolicyMode } from '../gateway.ts';
|
|
6
|
+
import type { CommandRiskStats, ControlState, PanelState } from '../panel-protocol.ts';
|
|
7
|
+
import { modeMeaning, statusView, type Tone } from '../status.ts';
|
|
8
|
+
|
|
9
|
+
export interface Row {
|
|
10
|
+
readonly label: string;
|
|
11
|
+
readonly value: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface ModeOption {
|
|
15
|
+
readonly value: PolicyMode;
|
|
16
|
+
readonly label: string;
|
|
17
|
+
readonly description: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface StatsView {
|
|
21
|
+
readonly rows: readonly Row[];
|
|
22
|
+
/** Shown instead of the rows when there is nothing to show yet. */
|
|
23
|
+
readonly empty?: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const MODE_OPTIONS: readonly ModeOption[] = [
|
|
27
|
+
{
|
|
28
|
+
value: 'off',
|
|
29
|
+
label: 'Off',
|
|
30
|
+
description:
|
|
31
|
+
'Adds no prompts. Claude Code, Codex and pi still ask by their own permission rules.',
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
value: 'shadow',
|
|
35
|
+
label: 'Shadow',
|
|
36
|
+
description: 'Rates every call and records it; nothing is asked or blocked.',
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
value: 'act',
|
|
40
|
+
label: 'Act',
|
|
41
|
+
description:
|
|
42
|
+
'Asks you before calls it rates risky and blocks ones it rates dangerous. It never approves anything.',
|
|
43
|
+
},
|
|
44
|
+
];
|
|
45
|
+
|
|
46
|
+
const BADGE_TEXT: Readonly<Record<PanelState['health'], string>> = {
|
|
47
|
+
ok: 'Gateway ok',
|
|
48
|
+
degraded: 'Gateway degraded',
|
|
49
|
+
down: 'Gateway down',
|
|
50
|
+
offline: 'Gateway offline',
|
|
51
|
+
rejected: 'Gateway refuses this plugin',
|
|
52
|
+
unknown: 'Checking the gateway',
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export function healthBadge(state: PanelState): { text: string; tone: Tone } {
|
|
56
|
+
return { text: BADGE_TEXT[state.health], tone: statusView(state).tone };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function modeOptions(): readonly ModeOption[] {
|
|
60
|
+
return MODE_OPTIONS;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const CONTROL_REASONS: Readonly<Record<ControlState, string | undefined>> = {
|
|
64
|
+
'ok': undefined,
|
|
65
|
+
'no-token': 'Needs the local control token. Run `local-decider gateway ensure` on this machine.',
|
|
66
|
+
'remote': 'Needs a local gateway: run the local-decider CLI on the gateway host.',
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
/** Why control buttons are disabled, or undefined when they are not. */
|
|
70
|
+
export function controlReason(control: ControlState): string | undefined {
|
|
71
|
+
return CONTROL_REASONS[control];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const OFFLINE_REASON = 'gateway offline';
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Why the engine and mode buttons are disabled: the control reason, else `gateway offline`, else
|
|
78
|
+
* undefined.
|
|
79
|
+
*/
|
|
80
|
+
export function actionReason(state: PanelState): string | undefined {
|
|
81
|
+
return controlReason(state.control) ?? (state.health === 'offline' ? OFFLINE_REASON : undefined);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The mode the radio shows: the person's pick, else the gateway's mode. Nothing when the mode is
|
|
86
|
+
* unknown or control is not `ok`, so the radio never claims a mode it cannot know.
|
|
87
|
+
*/
|
|
88
|
+
export function pickedMode(state: PanelState, picked: PolicyMode | null): PolicyMode | undefined {
|
|
89
|
+
if (picked !== null) return picked;
|
|
90
|
+
if (state.control !== 'ok' || state.gatewayMode === 'unknown') return undefined;
|
|
91
|
+
return state.gatewayMode;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** What the Offline callout offers: a Start button only for a gateway this machine can start. */
|
|
95
|
+
export function offlineStart(state: PanelState): { canStart: boolean; note?: string } {
|
|
96
|
+
return state.control === 'remote'
|
|
97
|
+
? { canStart: false, note: 'Remote gateways are started on their host.' }
|
|
98
|
+
: { canStart: true };
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function modeLine(mode: PolicyMode | 'unknown'): string {
|
|
102
|
+
return `${mode} (${modeMeaning(mode)})`;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function fixed(label: string, value: string | undefined): Row[] {
|
|
106
|
+
return value === undefined || value === '' ? [] : [{ label, value }];
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function ms(value: number | undefined): string | undefined {
|
|
110
|
+
return value === undefined ? undefined : `${String(Math.round(value))} ms`;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function engineValue(state: PanelState): string | undefined {
|
|
114
|
+
const { engine, model, engineState } = state.gateway ?? {};
|
|
115
|
+
const parts = [engine, model].filter((p) => p !== undefined);
|
|
116
|
+
if (parts.length === 0) return undefined;
|
|
117
|
+
return engineState === undefined ? parts.join(' · ') : `${parts.join(' · ')} (${engineState})`;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function latency(p50: number | undefined, p95: number | undefined): string | undefined {
|
|
121
|
+
const parts = [
|
|
122
|
+
p50 === undefined ? undefined : `p50 ${ms(p50) ?? ''}`,
|
|
123
|
+
p95 === undefined ? undefined : `p95 ${ms(p95) ?? ''}`,
|
|
124
|
+
].filter((p) => p !== undefined);
|
|
125
|
+
return parts.length === 0 ? undefined : parts.join(' · ');
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** The gateway card's rows; a field the gateway did not report is left out. */
|
|
129
|
+
export function gatewayRows(state: PanelState): Row[] {
|
|
130
|
+
const transport = state.transport === 'socket' ? 'unix socket' : 'tcp';
|
|
131
|
+
return [
|
|
132
|
+
{ label: 'URL', value: `${state.url} (${transport})` },
|
|
133
|
+
...fixed('Version', state.gateway?.version),
|
|
134
|
+
...fixed('Engine', engineValue(state)),
|
|
135
|
+
...fixed('Latency', latency(state.gateway?.p50Ms, state.gateway?.p95Ms)),
|
|
136
|
+
...fixed('Last decision', state.lastDecision),
|
|
137
|
+
...fixed('Last route', state.lastRoute),
|
|
138
|
+
...fixed('Last start', state.launch),
|
|
139
|
+
];
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function actionsValue(actions: Readonly<Record<string, number>>): string {
|
|
143
|
+
const parts = Object.entries(actions)
|
|
144
|
+
.toSorted(([, a], [, b]) => b - a)
|
|
145
|
+
.map(([action, n]) => `${action} ${String(n)}`);
|
|
146
|
+
return parts.length === 0 ? 'none' : parts.join(' · ');
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** The last-7-days rows; `null` (not loaded, or the gateway has no row) gives an empty state. */
|
|
150
|
+
export function statsRows(stats: CommandRiskStats | null): StatsView {
|
|
151
|
+
if (stats === null) return { rows: [], empty: 'No stats loaded yet.' };
|
|
152
|
+
return {
|
|
153
|
+
rows: [
|
|
154
|
+
{ label: 'Decisions', value: String(stats.decisions) },
|
|
155
|
+
{ label: 'Enforced', value: String(stats.enforced) },
|
|
156
|
+
{ label: 'Actions', value: actionsValue(stats.actions) },
|
|
157
|
+
{ label: 'Errors', value: `${String(stats.errors)} (busy ${String(stats.busy)})` },
|
|
158
|
+
...fixed('Latency', latency(stats.p50Ms, stats.p95Ms)),
|
|
159
|
+
],
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** The routing setting, and what the gateway's `routing` mode makes of it. */
|
|
164
|
+
function routingValue(state: PanelState): string {
|
|
165
|
+
if (state.settings.routing === 'off') return 'off';
|
|
166
|
+
const mode = state.routingMode;
|
|
167
|
+
if (mode === 'act') return 'on (gateway routing act: launches get the routed model)';
|
|
168
|
+
if (mode === 'shadow') return 'on (gateway routing shadow: recorded, not applied)';
|
|
169
|
+
return mode === 'off' ? 'on (gateway routing off)' : 'on';
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function on(flag: boolean): string {
|
|
173
|
+
return flag ? 'on' : 'off';
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** The end-of-turn setting, and what the gateway's `turn-outcome` mode makes of it. */
|
|
177
|
+
function turnOutcomeValue(state: PanelState): string {
|
|
178
|
+
if (state.settings.turnOutcome === 'off') return 'off';
|
|
179
|
+
const mode = state.turnOutcomeMode;
|
|
180
|
+
if (mode === 'act') return 'on (gateway turn-outcome act: notifications shown)';
|
|
181
|
+
if (mode === 'shadow') return 'on (gateway turn-outcome shadow: recorded, not shown)';
|
|
182
|
+
return mode === 'off' ? 'on (gateway turn-outcome off)' : 'on';
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** The read-only "This plugin" rows. */
|
|
186
|
+
export function pluginRows(state: PanelState): Row[] {
|
|
187
|
+
const missing = state.hooksMissing ?? 0;
|
|
188
|
+
return [
|
|
189
|
+
{ label: 'Mode', value: state.mode },
|
|
190
|
+
{ label: 'When unsure', value: state.askAction },
|
|
191
|
+
{ label: 'Decision timeout', value: `${String(state.settings.timeoutMs)} ms` },
|
|
192
|
+
{ label: 'Start the gateway', value: on(state.settings.autoStart) },
|
|
193
|
+
{ label: 'Load the model at start-up', value: on(state.settings.preloadModel) },
|
|
194
|
+
{ label: 'Routing', value: routingValue(state) },
|
|
195
|
+
{ label: 'End of turn', value: turnOutcomeValue(state) },
|
|
196
|
+
...(missing > 0
|
|
197
|
+
? [
|
|
198
|
+
{
|
|
199
|
+
label: 'Agent hooks',
|
|
200
|
+
value: `not seen from ${String(missing)} launched agent${missing === 1 ? '' : 's'}`,
|
|
201
|
+
},
|
|
202
|
+
]
|
|
203
|
+
: []),
|
|
204
|
+
];
|
|
205
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The part of `orca` and `orca/jsx-runtime` that `panel.tsx` uses, written out as ambient types so
|
|
3
|
+
* the package typechecks without the generated `.orca/types` (see `../orca-types.ts`). Orca supplies
|
|
4
|
+
* the real module when it loads the panel. When `orca plugin types` has written `.orca/types`, run
|
|
5
|
+
* the Orca checks in a copy without this file, so the two declarations do not meet.
|
|
6
|
+
*/
|
|
7
|
+
declare module 'orca' {
|
|
8
|
+
export type Json = null | boolean | number | string | Json[] | { [key: string]: Json };
|
|
9
|
+
export type Tone = 'neutral' | 'accent' | 'success' | 'warning' | 'danger';
|
|
10
|
+
export type Gap = 'none' | 'xs' | 'sm' | 'md' | 'lg';
|
|
11
|
+
export type IconName = string;
|
|
12
|
+
export type OrcaNode = unknown;
|
|
13
|
+
|
|
14
|
+
export interface Atom<T> {
|
|
15
|
+
readonly __value?: T;
|
|
16
|
+
}
|
|
17
|
+
export function atom<T>(initial: T): Atom<T>;
|
|
18
|
+
export function read<T>(a: Atom<T>): T;
|
|
19
|
+
export function set<T>(a: Atom<T>, value: T): void;
|
|
20
|
+
export function update<T>(a: Atom<T>, updater: (previous: T) => T): void;
|
|
21
|
+
|
|
22
|
+
export interface Disposable {
|
|
23
|
+
dispose(): void;
|
|
24
|
+
}
|
|
25
|
+
export interface PanelContext {
|
|
26
|
+
readonly subscriptions: Disposable[];
|
|
27
|
+
readonly signal: AbortSignal;
|
|
28
|
+
}
|
|
29
|
+
export const plugin: {
|
|
30
|
+
request(name: string, input?: Json, options?: { timeoutMs?: number }): Promise<Json>;
|
|
31
|
+
on(event: string, handler: (payload: Json) => void): Disposable;
|
|
32
|
+
};
|
|
33
|
+
export const shell: { openExternal(url: string): Promise<void> };
|
|
34
|
+
|
|
35
|
+
interface Children {
|
|
36
|
+
children?: unknown;
|
|
37
|
+
}
|
|
38
|
+
export function Stack(props: Children & { gap?: Gap }): OrcaNode;
|
|
39
|
+
export function Row(props: Children & { gap?: Gap; justify?: string; align?: string }): OrcaNode;
|
|
40
|
+
export function Text(props: Children & { tone?: Tone; muted?: boolean; size?: string }): OrcaNode;
|
|
41
|
+
export function Heading(props: Children & { level?: 1 | 2 | 3 }): OrcaNode;
|
|
42
|
+
export function Badge(props: Children & { tone?: Tone }): OrcaNode;
|
|
43
|
+
export function Button(
|
|
44
|
+
props: Children & {
|
|
45
|
+
tone?: Tone;
|
|
46
|
+
variant?: string;
|
|
47
|
+
icon?: IconName;
|
|
48
|
+
tooltip?: string;
|
|
49
|
+
disabled?: boolean;
|
|
50
|
+
onPress?: () => unknown;
|
|
51
|
+
},
|
|
52
|
+
): OrcaNode;
|
|
53
|
+
export function IconButton(props: {
|
|
54
|
+
icon: IconName;
|
|
55
|
+
label: string;
|
|
56
|
+
tooltip?: string;
|
|
57
|
+
disabled?: boolean;
|
|
58
|
+
onPress?: () => unknown;
|
|
59
|
+
}): OrcaNode;
|
|
60
|
+
export function Card(props: Children & { title?: string; description?: string }): OrcaNode;
|
|
61
|
+
export function Callout(props: Children & { tone?: Tone; title?: string }): OrcaNode;
|
|
62
|
+
export function KeyValue(props: {
|
|
63
|
+
label?: string;
|
|
64
|
+
items: readonly { label: string; value: string }[];
|
|
65
|
+
}): OrcaNode;
|
|
66
|
+
export function RadioGroup(props: {
|
|
67
|
+
label: string;
|
|
68
|
+
value?: string;
|
|
69
|
+
options: readonly { value: string; label: string; description?: string }[];
|
|
70
|
+
disabled?: boolean;
|
|
71
|
+
onChange?: (value: string) => void;
|
|
72
|
+
}): OrcaNode;
|
|
73
|
+
export function Collapsible(props: Children & { title: string }): OrcaNode;
|
|
74
|
+
export function Code(props: Children & { block?: boolean }): OrcaNode;
|
|
75
|
+
export function Link(props: Children & { onPress?: () => unknown }): OrcaNode;
|
|
76
|
+
export function EmptyState(props: { title: string; description?: string }): OrcaNode;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
declare module 'orca/jsx-runtime' {
|
|
80
|
+
export const Fragment: unique symbol;
|
|
81
|
+
export function jsx(type: unknown, props: unknown, key?: string): unknown;
|
|
82
|
+
export function jsxs(type: unknown, props: unknown, key?: string): unknown;
|
|
83
|
+
export namespace JSX {
|
|
84
|
+
type Element = unknown;
|
|
85
|
+
interface ElementChildrenAttribute {
|
|
86
|
+
children: unknown;
|
|
87
|
+
}
|
|
88
|
+
interface IntrinsicElements {}
|
|
89
|
+
}
|
|
90
|
+
}
|