Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Restore proactive onboarding and deduplicate gateway model choices (#1226)

Regan Bell9f421216859e
diff --git a/plugins/web-ui/src/chat.ts b/plugins/web-ui/src/chat.ts
index 9ea6e0788..2e931306d 100644
--- a/plugins/web-ui/src/chat.ts
+++ b/plugins/web-ui/src/chat.ts
@@ -129,6 +129,7 @@ import {
clearWorking,
conversationBackground,
isAbandonedNewChat,
+ shouldStartProactiveOpener,
markWorking,
watchActivityLabel,
} from "./session-list";
@@ -511,10 +512,17 @@ export function createChatSurface(
scopeId: string | null,
messages: ReturnType<typeof entriesToMessages>,
): boolean {
- if (appState.me?.suggestedActivitiesGeneration || appState.me?.suggestedActivities?.length) return false;
- if (proactiveOpenerStarted || sessionId !== null || scopeId !== null || messages.length > 0) return false;
- if (!sessionsState.loaded) return false;
- if (sessionsState.list.some((s) => s.id)) return false;
+ if (
+ !shouldStartProactiveOpener({
+ started: proactiveOpenerStarted,
+ sessionId,
+ scopeId,
+ messageCount: messages.length,
+ loaded: sessionsState.loaded,
+ sessions: sessionsState.list,
+ })
+ )
+ return false;
proactiveOpenerStarted = true;
agent.state.messages = [{ role: "user", content: "", opener: true } as unknown as AgentMessage];
agent.streamFn = makeOpenerStreamFn(threadRef, agent, currentTurnOptions, onWork, runSlot);
diff --git a/plugins/web-ui/src/session-list.ts b/plugins/web-ui/src/session-list.ts
index 0878826d4..e99c1743a 100644
--- a/plugins/web-ui/src/session-list.ts
+++ b/plugins/web-ui/src/session-list.ts
@@ -211,3 +211,21 @@ export function conversationBackground(
const row = list.find((s) => (sessionId ? s.id === sessionId : Boolean(threadRef) && s.threadRef === threadRef));
return row ? rowIndicators(row, null).background : null;
}
+
+export function shouldStartProactiveOpener(state: {
+ started: boolean;
+ sessionId: string | null;
+ scopeId: string | null;
+ messageCount: number;
+ loaded: boolean;
+ sessions: readonly Pick<CoreSession, "id" | "threadRef">[];
+}): boolean {
+ return (
+ !state.started &&
+ state.sessionId === null &&
+ state.scopeId === null &&
+ state.messageCount === 0 &&
+ state.loaded &&
+ !state.sessions.some((session) => session.id && !session.threadRef.startsWith("cron:"))
+ );
+}
diff --git a/plugins/web-ui/test/session-list.test.ts b/plugins/web-ui/test/session-list.test.ts
index c9fb79a16..37b1956f3 100644
--- a/plugins/web-ui/test/session-list.test.ts
+++ b/plugins/web-ui/test/session-list.test.ts
@@ -4,6 +4,7 @@ import {
activityOf,
applySessionState,
isAbandonedNewChat,
+ shouldStartProactiveOpener,
bumpActivity,
chatBrowseStatusMatches,
clearWorking,
@@ -493,3 +494,25 @@ test("a new chat with anything worth keeping is not abandoned", () => {
assert.equal(isAbandonedNewChat({ ...base, nextThreadRef: "web:alice:t1" }), false, "remounting the same chat");
assert.equal(isAbandonedNewChat({ ...base, threadRef: null }), false, "no chat mounted");
});
+
+test("first personal chat opens proactively even when background suggestions already ran", () => {
+ const first = {
+ started: false,
+ sessionId: null,
+ scopeId: null,
+ messageCount: 0,
+ loaded: true,
+ sessions: [],
+ };
+ assert.equal(shouldStartProactiveOpener(first), true);
+ assert.equal(shouldStartProactiveOpener({ ...first, sessions: [saved("refresh", "cron:refresh:fire:1")] }), true);
+ assert.equal(shouldStartProactiveOpener({ ...first, sessions: [pending("web:alice:draft")] }), true);
+ for (const threadRef of ["web:alice:previous", "dm:D123:123.456", "ch:C123:123.456"]) {
+ assert.equal(shouldStartProactiveOpener({ ...first, sessions: [saved("previous", threadRef)] }), false);
+ }
+ assert.equal(shouldStartProactiveOpener({ ...first, started: true }), false);
+ assert.equal(shouldStartProactiveOpener({ ...first, loaded: false }), false);
+ assert.equal(shouldStartProactiveOpener({ ...first, sessionId: "existing" }), false);
+ assert.equal(shouldStartProactiveOpener({ ...first, scopeId: "channel:C123" }), false);
+ assert.equal(shouldStartProactiveOpener({ ...first, messageCount: 1 }), false);
+});
diff --git a/src/api/runtime-config.ts b/src/api/runtime-config.ts
index a9d467caf..851b46110 100644
--- a/src/api/runtime-config.ts
+++ b/src/api/runtime-config.ts
@@ -33,7 +33,7 @@ export type RuntimeDeps = Pick<
export function runtimeFallback(ctx: { deps: RuntimeDeps }): { harnessId: HarnessId; modelId: string } {
const harnessId = isHarnessId(ctx.deps.harnessId) ? ctx.deps.harnessId : "pi";
- const gatewayIds = gatewayModelCatalog().map((model) => model.id);
+ const gatewayIds = gatewayModelCatalog(true).map((model) => model.id);
const providers =
gatewayIds.length && ctx.deps.providerKeys
? { ...ctx.deps.providerKeys, modelIds: new Set(gatewayIds) }
diff --git a/src/model/gateway-catalog.ts b/src/model/gateway-catalog.ts
index ebd0e9a3f..f1e776fe6 100644
--- a/src/model/gateway-catalog.ts
+++ b/src/model/gateway-catalog.ts
@@ -1,6 +1,7 @@
import type { Api, Model } from "@earendil-works/pi-ai";
import type { ModelGatewayTransportConfig } from "./provider-endpoints.ts";
import { GATEWAY_MODEL_PREFIX, GATEWAY_PROVIDER, setGatewayModels } from "./gateway-models.ts";
+import { modelOfferedInWebui, selectableBaseModels } from "./pi-models.ts";
const MAX_BYTES = 2 * 1024 * 1024;
const MAX_MODELS = 1_000;
@@ -159,7 +160,19 @@ export function createGatewayCatalog(
discovered = true;
expiresAt = now() + TTL_MS;
retryAt = expiresAt;
- setGatewayModels(models);
+ const offeredAliases = new Set(
+ selectableBaseModels()
+ .filter(({ id }) => modelOfferedInWebui(id))
+ .map(({ id }) => id),
+ );
+ setGatewayModels(
+ models,
+ Object.entries(config.models).flatMap(([id, target]) =>
+ !id.startsWith(GATEWAY_MODEL_PREFIX) && offeredAliases.has(id) && next[id]
+ ? [GATEWAY_MODEL_PREFIX + target]
+ : [],
+ ),
+ );
} catch {
retryAt = now() + RETRY_MS;
if (discovered || Object.keys(config.models).length === 0) {
diff --git a/src/model/gateway-models.ts b/src/model/gateway-models.ts
index e0aef4eb1..79bf6917a 100644
--- a/src/model/gateway-models.ts
+++ b/src/model/gateway-models.ts
@@ -6,6 +6,7 @@ export const GATEWAY_MODEL_PREFIX = "gateway/";
let models = new Map<string, Model<Api>>();
let version = 0;
let snapshot = "[]";
+let aliasedIds = new Set<string>();
export function isGatewayModelId(id: string): boolean {
return id.startsWith(GATEWAY_MODEL_PREFIX);
@@ -15,10 +16,11 @@ export function gatewayModelsVersion(): number {
return version;
}
-export function setGatewayModels(next: readonly Model<Api>[]): void {
- const serialized = JSON.stringify(next);
+export function setGatewayModels(next: readonly Model<Api>[], aliases: readonly string[] = []): void {
+ const serialized = JSON.stringify([next, aliases]);
if (serialized === snapshot) return;
models = new Map(next.map((model) => [model.id, model]));
+ aliasedIds = new Set(aliases);
snapshot = serialized;
version += 1;
}
@@ -27,8 +29,10 @@ export function resolveGatewayModel(id: string): Model<Api> | undefined {
return models.get(id);
}
-export function gatewayModelCatalog(): Array<{ id: string; name: string; provider: string }> {
- return [...models.values()].map(({ id, name, provider }) => ({ id, name, provider }));
+export function gatewayModelCatalog(includeAliased = false): Array<{ id: string; name: string; provider: string }> {
+ return [...models.values()]
+ .filter(({ id }) => includeAliased || !aliasedIds.has(id))
+ .map(({ id, name, provider }) => ({ id, name, provider }));
}
export function gatewayModelsJson(): Record<string, unknown> {
diff --git a/src/model/pi-models.ts b/src/model/pi-models.ts
index 26332f1d3..ae1e2e8db 100644
--- a/src/model/pi-models.ts
+++ b/src/model/pi-models.ts
@@ -289,7 +289,7 @@ export function overlayModelCatalog(): Array<{ id: string; name: string; provide
export function defaultWebuiModelIds(): readonly string[] {
return [
...DEFAULT_WEBUI_MODEL_IDS,
- ...gatewayModelCatalog().map((m) => m.id),
+ ...gatewayModelCatalog(true).map((m) => m.id),
...[...overlays.values()].filter((m) => m.webui).map((m) => m.id),
];
}
diff --git a/test/gateway-catalog.test.ts b/test/gateway-catalog.test.ts
index 3e7f9f594..00e5a67d6 100644
--- a/test/gateway-catalog.test.ts
+++ b/test/gateway-catalog.test.ts
@@ -229,3 +229,42 @@ test("removed gateway selections never fall back to a different org or direct mo
store.setRuntimeSelection(personal, { harnessId: "pi", modelId: "gateway/removed" });
assert.throws(() => resolveRuntimeChoice(store, org, personal, fallback), /Gateway model is unavailable/);
});
+
+test("gateway aliases hide duplicate picker options while preserving saved routes", async () => {
+ const target = "anthropic/claude-opus-5";
+ const f = fixture([group(target)], { "claude-opus-5": target });
+ await f.catalog.refresh();
+ const ids = builtInModelCatalog().map((m) => m.id);
+ assert.ok(ids.includes("claude-opus-5"));
+ assert.ok(!ids.includes(`gateway/${target}`));
+ for (const id of ["claude-opus-5", `gateway/${target}`]) {
+ assert.equal(modelGatewayRequest(f.catalog.transport, resolveModel(id)!)?.target, target);
+ assert.equal(
+ validateWebTurnModelOptions({ model: id }, null, {
+ anthropic: false,
+ openai: false,
+ openrouter: false,
+ modelIds: new Set([id]),
+ }),
+ null,
+ );
+ }
+ f.listing([]);
+ f.tick();
+ await f.catalog.refresh();
+ assert.equal(f.catalog.transport.models["claude-opus-5"], undefined);
+ assert.equal(resolveModel(`gateway/${target}`), undefined);
+});
+
+test("unknown aliases do not hide the usable discovered model", async () => {
+ const f = fixture([group()], { "unknown-alias": "vendor/new-model" });
+ await f.catalog.refresh();
+ assert.ok(builtInModelCatalog().some((m) => m.id === "gateway/vendor/new-model"));
+});
+
+test("resolvable aliases outside the picker do not hide discovered models", async () => {
+ assert.ok(resolveModel("gpt-4o"));
+ const f = fixture([group("openai/gpt-4o")], { "gpt-4o": "openai/gpt-4o" });
+ await f.catalog.refresh();
+ assert.ok(builtInModelCatalog().some((m) => m.id === "gateway/openai/gpt-4o"));
+});