opencode-openai-session-router 0.0.0-stage → 0.1.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/LICENSE +674 -0
- package/README.md +217 -2
- package/dist/accounts.d.ts +31 -0
- package/dist/bun.lock.txt +671 -0
- package/dist/chunks/index-85yd61pa.js +29 -0
- package/dist/chunks/index-n7sr1hee.js +803 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +1842 -0
- package/dist/network.d.ts +8 -0
- package/dist/panel-contract.d.ts +14 -0
- package/dist/persistence.d.ts +40 -0
- package/dist/relay-protocol.d.ts +86 -0
- package/dist/relay.d.ts +50 -0
- package/dist/retry-control.d.ts +41 -0
- package/dist/router.d.ts +55 -0
- package/dist/rpc.d.ts +98 -0
- package/dist/rpc.js +7 -0
- package/dist/runtime.d.ts +94 -0
- package/dist/settings.d.ts +32 -0
- package/dist/sidebar-colors.d.ts +10 -0
- package/dist/sidebar-model.d.ts +82 -0
- package/dist/sidebar.d.ts +33 -0
- package/dist/sidebar.js +12 -0
- package/dist/tui.d.ts +3 -0
- package/dist/tui.js +25 -0
- package/package.json +52 -4
- package/scripts/build.ts +39 -0
- package/src/accounts.ts +130 -0
- package/src/index.ts +203 -0
- package/src/network.ts +25 -0
- package/src/panel-contract.ts +15 -0
- package/src/persistence.ts +96 -0
- package/src/relay-protocol.ts +157 -0
- package/src/relay.ts +593 -0
- package/src/retry-control.ts +92 -0
- package/src/router.ts +156 -0
- package/src/rpc.ts +28 -0
- package/src/runtime.ts +319 -0
- package/src/settings.ts +68 -0
- package/src/sidebar-colors.ts +132 -0
- package/src/sidebar-model.ts +257 -0
- package/src/sidebar.tsx +438 -0
- package/src/tui.tsx +12 -0
- package/tsconfig.build.json +12 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { RGBA } from "@opentui/core";
|
|
2
|
+
import type { SidebarAgent } from "./sidebar-model";
|
|
3
|
+
|
|
4
|
+
export type SidebarColor = string | RGBA;
|
|
5
|
+
|
|
6
|
+
function normalizeColor(value: unknown): SidebarColor | undefined {
|
|
7
|
+
if (typeof value === "string" && value.startsWith("#")) return value;
|
|
8
|
+
if (!value || typeof value !== "object") return undefined;
|
|
9
|
+
const buffer = (value as { buffer?: unknown }).buffer;
|
|
10
|
+
if (buffer && typeof buffer === "object" && "length" in buffer &&
|
|
11
|
+
typeof (buffer as { length: unknown }).length === "number") {
|
|
12
|
+
try {
|
|
13
|
+
const channels = Array.from(buffer as ArrayLike<number>).slice(0, 4);
|
|
14
|
+
if (channels.length === 4 && channels.every(Number.isFinite)) {
|
|
15
|
+
return RGBA.fromArray(Uint16Array.from(channels));
|
|
16
|
+
}
|
|
17
|
+
} catch {
|
|
18
|
+
// Fall through to the public toInts representation.
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
const toInts = (value as { toInts?: unknown }).toInts;
|
|
22
|
+
if (typeof toInts !== "function") return undefined;
|
|
23
|
+
try {
|
|
24
|
+
const channels = toInts.call(value) as unknown;
|
|
25
|
+
if (!Array.isArray(channels) || channels.length < 3 ||
|
|
26
|
+
!channels.slice(0, 4).every((channel) => typeof channel === "number" && Number.isFinite(channel))) {
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
const [red, green, blue, alpha = 255] = channels as number[];
|
|
30
|
+
return RGBA.fromInts(red, green, blue, alpha);
|
|
31
|
+
} catch {
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function colorAt400(scale: unknown): SidebarColor | undefined {
|
|
37
|
+
if (!scale || typeof scale !== "object") return undefined;
|
|
38
|
+
return normalizeColor((scale as Record<string | number, unknown>)[400]);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function rgba(color: SidebarColor): RGBA {
|
|
42
|
+
return typeof color === "string" ? RGBA.fromHex(color) : color;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Reproduces the public theme's categorical-400 palette used for agent colors. */
|
|
46
|
+
export function categoricalAgentPalette(categorical: unknown): SidebarColor[] {
|
|
47
|
+
if (!Array.isArray(categorical)) return [];
|
|
48
|
+
const palette: SidebarColor[] = [];
|
|
49
|
+
for (const scale of categorical) {
|
|
50
|
+
const color = colorAt400(scale);
|
|
51
|
+
if (!color) continue;
|
|
52
|
+
let duplicate = false;
|
|
53
|
+
try {
|
|
54
|
+
const candidate = rgba(color);
|
|
55
|
+
duplicate = palette.some((existing) => rgba(existing).equals(candidate));
|
|
56
|
+
} catch {
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
if (!duplicate) palette.push(color);
|
|
60
|
+
}
|
|
61
|
+
return palette;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function configuredAgentColor(value: string | undefined): value is string {
|
|
65
|
+
return !!value && value.startsWith("#");
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Mirrors OMO-Slim's explicit-color, roster-index, then muted fallback order. */
|
|
69
|
+
export function resolveAgentColor(
|
|
70
|
+
agentID: string | undefined,
|
|
71
|
+
roster: readonly SidebarAgent[],
|
|
72
|
+
palette: readonly SidebarColor[],
|
|
73
|
+
muted: SidebarColor,
|
|
74
|
+
): SidebarColor {
|
|
75
|
+
const configured = roster.find((agent) => agent.id === agentID)?.color;
|
|
76
|
+
if (configuredAgentColor(configured)) return configured;
|
|
77
|
+
|
|
78
|
+
const visibleRoster = roster.filter((agent) => !agent.hidden);
|
|
79
|
+
const index = visibleRoster.findIndex((agent) => agent.id === agentID);
|
|
80
|
+
if (roster.length && palette.length) {
|
|
81
|
+
return palette[Math.max(index, 0) % palette.length];
|
|
82
|
+
}
|
|
83
|
+
return configuredAgentColor(configured) ? configured : muted;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function findThemeColor(
|
|
87
|
+
theme: unknown,
|
|
88
|
+
paths: readonly (readonly (string | number)[])[],
|
|
89
|
+
): SidebarColor | undefined {
|
|
90
|
+
for (const path of paths) {
|
|
91
|
+
let value: unknown = theme;
|
|
92
|
+
for (const key of path) {
|
|
93
|
+
if (!value || typeof value !== "object") {
|
|
94
|
+
value = undefined;
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
value = (value as Record<string | number, unknown>)[key];
|
|
98
|
+
}
|
|
99
|
+
const normalized = normalizeColor(value);
|
|
100
|
+
if (normalized) return normalized;
|
|
101
|
+
}
|
|
102
|
+
return undefined;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function themeColor(
|
|
106
|
+
theme: unknown,
|
|
107
|
+
paths: readonly (readonly (string | number)[])[],
|
|
108
|
+
fallback: SidebarColor,
|
|
109
|
+
): SidebarColor {
|
|
110
|
+
return findThemeColor(theme, paths) ?? fallback;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function resolveHoverBackground(theme: unknown): SidebarColor {
|
|
114
|
+
try {
|
|
115
|
+
const hover = findThemeColor(theme, [["hover"]]);
|
|
116
|
+
if (hover && (typeof hover === "string" ? RGBA.fromHex(hover) : hover).a > 0) return hover;
|
|
117
|
+
const background = findThemeColor(theme, [["background", "base"], ["background", "default"], ["background"]]) ??
|
|
118
|
+
RGBA.fromHex("#111111");
|
|
119
|
+
const text = findThemeColor(theme, [["text", "base"], ["text", "default"], ["text"]]) ??
|
|
120
|
+
RGBA.fromHex("#ffffff");
|
|
121
|
+
const bg = typeof background === "string" ? RGBA.fromHex(background) : background;
|
|
122
|
+
const fg = typeof text === "string" ? RGBA.fromHex(text) : text;
|
|
123
|
+
return RGBA.fromValues(
|
|
124
|
+
bg.r * 0.82 + fg.r * 0.18,
|
|
125
|
+
bg.g * 0.82 + fg.g * 0.18,
|
|
126
|
+
bg.b * 0.82 + fg.b * 0.18,
|
|
127
|
+
1,
|
|
128
|
+
);
|
|
129
|
+
} catch {
|
|
130
|
+
return "#2a2a2a";
|
|
131
|
+
}
|
|
132
|
+
}
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
export interface SidebarSession {
|
|
2
|
+
readonly id: string;
|
|
3
|
+
readonly parentID?: string;
|
|
4
|
+
readonly agent?: string;
|
|
5
|
+
readonly title?: string;
|
|
6
|
+
readonly time: {
|
|
7
|
+
readonly updated: number;
|
|
8
|
+
readonly busy?: number;
|
|
9
|
+
readonly idle?: number;
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface SidebarRow {
|
|
14
|
+
readonly sessionID: string;
|
|
15
|
+
readonly agent: string;
|
|
16
|
+
readonly accountIndex: number | null;
|
|
17
|
+
readonly orchestrator: boolean;
|
|
18
|
+
readonly title: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export const BASELINE_SESSIONS = 10;
|
|
22
|
+
export const BASELINE_CHILDREN = BASELINE_SESSIONS - 1;
|
|
23
|
+
export const PAGE_SIZE = 10;
|
|
24
|
+
|
|
25
|
+
export interface SidebarAgent {
|
|
26
|
+
readonly id: string;
|
|
27
|
+
readonly name: string;
|
|
28
|
+
readonly color?: string;
|
|
29
|
+
readonly hidden?: boolean;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface RouterSnapshotLike {
|
|
33
|
+
readonly enabled: boolean;
|
|
34
|
+
readonly instanceID: string;
|
|
35
|
+
readonly revision: number;
|
|
36
|
+
readonly assignments: readonly {
|
|
37
|
+
readonly sessionID: string;
|
|
38
|
+
readonly accountIndex: number | null;
|
|
39
|
+
}[];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface SnapshotEpochState {
|
|
43
|
+
readonly snapshot?: RouterSnapshotLike;
|
|
44
|
+
readonly requestToken: number;
|
|
45
|
+
readonly requestBaseInstanceID?: string;
|
|
46
|
+
readonly requestBaseRevision?: number;
|
|
47
|
+
readonly retiredInstanceIDs: ReadonlySet<string>;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function initialSnapshotEpochState(): SnapshotEpochState {
|
|
51
|
+
return { requestToken: 0, retiredInstanceIDs: new Set() };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function beginSnapshotRequest(state: SnapshotEpochState): SnapshotEpochState {
|
|
55
|
+
return {
|
|
56
|
+
...state,
|
|
57
|
+
requestToken: state.requestToken + 1,
|
|
58
|
+
requestBaseInstanceID: state.snapshot?.instanceID,
|
|
59
|
+
requestBaseRevision: state.snapshot?.revision,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function snapshotRequestIsAuthoritative(
|
|
64
|
+
state: SnapshotEpochState,
|
|
65
|
+
requestToken: number,
|
|
66
|
+
baseInstanceID?: string,
|
|
67
|
+
baseRevision?: number,
|
|
68
|
+
): boolean {
|
|
69
|
+
return state.requestToken === requestToken &&
|
|
70
|
+
state.snapshot?.instanceID === baseInstanceID &&
|
|
71
|
+
state.snapshot?.revision === baseRevision;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function acceptSnapshotEvent(
|
|
75
|
+
state: SnapshotEpochState,
|
|
76
|
+
incoming: RouterSnapshotLike,
|
|
77
|
+
): SnapshotEpochState {
|
|
78
|
+
if (state.retiredInstanceIDs.has(incoming.instanceID)) return state;
|
|
79
|
+
const current = state.snapshot;
|
|
80
|
+
if (current?.instanceID === incoming.instanceID && incoming.revision < current.revision) return state;
|
|
81
|
+
if (current?.instanceID === incoming.instanceID) return { ...state, snapshot: incoming };
|
|
82
|
+
const retired = new Set(state.retiredInstanceIDs);
|
|
83
|
+
if (current) retired.add(current.instanceID);
|
|
84
|
+
return {
|
|
85
|
+
snapshot: incoming,
|
|
86
|
+
requestToken: state.requestToken + 1,
|
|
87
|
+
retiredInstanceIDs: retired,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function acceptSnapshotFetch(
|
|
92
|
+
state: SnapshotEpochState,
|
|
93
|
+
incoming: RouterSnapshotLike,
|
|
94
|
+
requestToken: number,
|
|
95
|
+
): SnapshotEpochState {
|
|
96
|
+
if (requestToken !== state.requestToken || state.retiredInstanceIDs.has(incoming.instanceID)) return state;
|
|
97
|
+
const current = state.snapshot;
|
|
98
|
+
if (current?.instanceID === incoming.instanceID) {
|
|
99
|
+
return incoming.revision < current.revision ? state : { ...state, snapshot: incoming };
|
|
100
|
+
}
|
|
101
|
+
if (current && current.instanceID !== state.requestBaseInstanceID) return state;
|
|
102
|
+
const retired = new Set(state.retiredInstanceIDs);
|
|
103
|
+
if (current) retired.add(current.instanceID);
|
|
104
|
+
return { ...state, snapshot: incoming, retiredInstanceIDs: retired };
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function navigateSidebarRow(
|
|
108
|
+
sessionID: string,
|
|
109
|
+
navigate: (sessionID: string) => void,
|
|
110
|
+
): void {
|
|
111
|
+
navigate(sessionID);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export function moreCount(totalChildren: number, visibleChildren: number): number {
|
|
115
|
+
return Math.min(PAGE_SIZE, Math.max(0, totalChildren - visibleChildren));
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function nextVisibleChildren(visibleChildren: number, totalChildren: number): number {
|
|
119
|
+
return visibleChildren + moreCount(totalChildren, visibleChildren);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function toggleSidebarCollapsed(collapsed: boolean): boolean {
|
|
123
|
+
return !collapsed;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export interface PrimaryPointerEvent {
|
|
127
|
+
readonly button: number;
|
|
128
|
+
readonly x: number;
|
|
129
|
+
readonly y: number;
|
|
130
|
+
readonly isDragging?: boolean;
|
|
131
|
+
readonly stopPropagation: () => void;
|
|
132
|
+
readonly preventDefault: () => void;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export function createPrimaryClickHandler(activate: (target: string) => void) {
|
|
136
|
+
let pressed: { readonly target: string; readonly x: number; readonly y: number } | undefined;
|
|
137
|
+
const consume = (event: PrimaryPointerEvent) => {
|
|
138
|
+
event.stopPropagation();
|
|
139
|
+
event.preventDefault();
|
|
140
|
+
};
|
|
141
|
+
return {
|
|
142
|
+
down(target: string, event: PrimaryPointerEvent) {
|
|
143
|
+
consume(event);
|
|
144
|
+
pressed = event.button === 0 && !event.isDragging
|
|
145
|
+
? { target, x: event.x, y: event.y }
|
|
146
|
+
: undefined;
|
|
147
|
+
},
|
|
148
|
+
up(target: string, event: PrimaryPointerEvent): boolean {
|
|
149
|
+
consume(event);
|
|
150
|
+
const matchingTarget = pressed?.target === target;
|
|
151
|
+
const completedClick = event.button === 0 &&
|
|
152
|
+
(!event.isDragging || (pressed?.x === event.x && pressed?.y === event.y));
|
|
153
|
+
pressed = undefined;
|
|
154
|
+
if (matchingTarget && completedClick) activate(target);
|
|
155
|
+
return matchingTarget && completedClick;
|
|
156
|
+
},
|
|
157
|
+
cancel(target: string, event: PrimaryPointerEvent) {
|
|
158
|
+
consume(event);
|
|
159
|
+
if (pressed?.target === target) pressed = undefined;
|
|
160
|
+
},
|
|
161
|
+
cancelAll(event: PrimaryPointerEvent) {
|
|
162
|
+
consume(event);
|
|
163
|
+
pressed = undefined;
|
|
164
|
+
},
|
|
165
|
+
invalidate(target: string) {
|
|
166
|
+
if (pressed?.target === target) pressed = undefined;
|
|
167
|
+
},
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export interface RoutingToggleOptions {
|
|
172
|
+
readonly snapshot?: RouterSnapshotLike;
|
|
173
|
+
readonly directory?: string;
|
|
174
|
+
readonly isPending: () => boolean;
|
|
175
|
+
readonly setPending: (pending: boolean) => void;
|
|
176
|
+
readonly isCurrent: () => boolean;
|
|
177
|
+
readonly setEnabled: (enabled: boolean, directory: string) => Promise<RouterSnapshotLike>;
|
|
178
|
+
readonly accept: (snapshot: RouterSnapshotLike) => void;
|
|
179
|
+
readonly fail: () => void;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export async function requestRoutingToggle(options: RoutingToggleOptions): Promise<void> {
|
|
183
|
+
if (!options.snapshot || !options.directory || options.isPending() || !options.isCurrent()) return;
|
|
184
|
+
options.setPending(true);
|
|
185
|
+
try {
|
|
186
|
+
const result = await options.setEnabled(!options.snapshot.enabled, options.directory);
|
|
187
|
+
if (options.isCurrent()) options.accept(result);
|
|
188
|
+
} catch {
|
|
189
|
+
if (options.isCurrent()) options.fail();
|
|
190
|
+
} finally {
|
|
191
|
+
if (options.isCurrent()) options.setPending(false);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function visibleTitle(agent: string, title: string | undefined, sessionID: string): string {
|
|
196
|
+
const role = agent.trim() || "Unknown";
|
|
197
|
+
const nativeTitle = title || sessionID.slice(-8);
|
|
198
|
+
if (!title) return `${role}: ${nativeTitle}`;
|
|
199
|
+
if (nativeTitle.toLocaleLowerCase().startsWith(`${role.toLocaleLowerCase()}:`)) return nativeTitle;
|
|
200
|
+
return `${role}: ${nativeTitle}`;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export function projectSidebar(
|
|
204
|
+
rootID: string,
|
|
205
|
+
sessions: readonly SidebarSession[],
|
|
206
|
+
accountBySession: ReadonlyMap<string, number | null>,
|
|
207
|
+
visibleChildren = BASELINE_CHILDREN,
|
|
208
|
+
agents: readonly SidebarAgent[] = [],
|
|
209
|
+
): { rows: SidebarRow[]; totalChildren: number } {
|
|
210
|
+
const root = sessions.find((session) => session.id === rootID);
|
|
211
|
+
if (!root || root.parentID) return { rows: [], totalChildren: 0 };
|
|
212
|
+
|
|
213
|
+
const descendants = new Set<string>();
|
|
214
|
+
let changed = true;
|
|
215
|
+
while (changed) {
|
|
216
|
+
changed = false;
|
|
217
|
+
for (const session of sessions) {
|
|
218
|
+
if (session.parentID === rootID || (session.parentID && descendants.has(session.parentID))) {
|
|
219
|
+
if (session.id !== rootID && !descendants.has(session.id)) {
|
|
220
|
+
descendants.add(session.id);
|
|
221
|
+
changed = true;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const children = sessions
|
|
228
|
+
.filter((session) => descendants.has(session.id))
|
|
229
|
+
.sort((a, b) =>
|
|
230
|
+
Math.max(b.time.updated, b.time.busy ?? 0, b.time.idle ?? 0) -
|
|
231
|
+
Math.max(a.time.updated, a.time.busy ?? 0, a.time.idle ?? 0) ||
|
|
232
|
+
a.id.localeCompare(b.id),
|
|
233
|
+
);
|
|
234
|
+
return {
|
|
235
|
+
totalChildren: children.length,
|
|
236
|
+
rows: [
|
|
237
|
+
{
|
|
238
|
+
sessionID: root.id,
|
|
239
|
+
agent: "orchestrator",
|
|
240
|
+
accountIndex: accountBySession.get(root.id) ?? null,
|
|
241
|
+
orchestrator: true,
|
|
242
|
+
title: visibleTitle("Orchestrator", root.title, root.id),
|
|
243
|
+
},
|
|
244
|
+
...children.slice(0, visibleChildren).map((session) => ({
|
|
245
|
+
sessionID: session.id,
|
|
246
|
+
agent: agents.find((agent) => agent.id === session.agent)?.name ?? session.agent ?? "Unknown",
|
|
247
|
+
accountIndex: accountBySession.get(session.id) ?? null,
|
|
248
|
+
orchestrator: false,
|
|
249
|
+
title: visibleTitle(
|
|
250
|
+
agents.find((agent) => agent.id === session.agent)?.name ?? session.agent ?? "Unknown",
|
|
251
|
+
session.title,
|
|
252
|
+
session.id,
|
|
253
|
+
),
|
|
254
|
+
})),
|
|
255
|
+
],
|
|
256
|
+
};
|
|
257
|
+
}
|