New threads, model and mode picker, alerts, $EDITOR and terminal attach
- c starts a draft thread (project, model, mode, worktree) launched with orchestration.launchThread - m picks model, provider options and access mode from the server's providers, following the web's provider lock; model changes ride on the next message - Alerts port T3's notification gate: bell, OSC 9/99 desktop notifications when unfocused, in-app flash - ctrl+g edits the message in $EDITOR; ctrl+j attaches to the thread's T3 terminal - Credential gains terminal:read
This commit is contained in:
1 parent
121bbd9137
commit
626d37a4e9
18 files changed
+1074
-49
No files matched your search
@@ -0,0 +1,86 @@
|
||||
import { presentThreadShell } from "@t3tools/client-runtime/state/models";
|
||||
import type { EnvironmentId, OrchestrationV2ShellSnapshot, OrchestrationV2ThreadShell, ThreadId } from "@t3tools/contracts";
|
||||
|
||||
import { createThreadNotificationGate, type ThreadNotificationKind, type ThreadNotificationObservation } from "../../vendor/t3code/apps/web/src/components/ThreadNotificationCoordinator.logic.ts";
|
||||
import { resolveStatus, type ThreadStatus } from "./threads.ts";
|
||||
|
||||
export type { ThreadNotificationKind };
|
||||
|
||||
export interface Alert {
|
||||
readonly thread: OrchestrationV2ThreadShell;
|
||||
readonly kind: ThreadNotificationKind;
|
||||
readonly status: ThreadStatus;
|
||||
/** As the web's notification titles. */
|
||||
readonly title: string;
|
||||
}
|
||||
|
||||
interface Observed extends ThreadNotificationObservation {
|
||||
readonly raw: OrchestrationV2ThreadShell;
|
||||
readonly status: ThreadStatus;
|
||||
}
|
||||
|
||||
const LOCAL_ENVIRONMENT = "local" as EnvironmentId;
|
||||
|
||||
/**
|
||||
* The web's ThreadNotificationCoordinator over the shell snapshot: attention (approval,
|
||||
* question, guest request, failure, usage limit) alerts once per run and status;
|
||||
* completion alerts after the Working shelf's grace, unless the run was stopped. Nothing
|
||||
* alerts for state that was already there when tui3 connected.
|
||||
*/
|
||||
export function createAlerts(onAlert: (alert: Alert) => void) {
|
||||
let previous = new Map<ThreadId, Observed>();
|
||||
const gate = createThreadNotificationGate<ThreadId>((threadId, kind) => {
|
||||
const state = previous.get(threadId);
|
||||
if (state) onAlert({ thread: state.raw, kind, status: state.status, title: alertTitle(kind, state.status) });
|
||||
});
|
||||
return {
|
||||
observe(snapshot: OrchestrationV2ShellSnapshot): void {
|
||||
const next = new Map<ThreadId, Observed>();
|
||||
const changed: [Observed | undefined, Observed][] = [];
|
||||
for (const raw of snapshot.threads) {
|
||||
// Subagents show under their parent, whose status covers them.
|
||||
if (raw.lineage.relationshipToParent === "subagent") continue;
|
||||
const prior = previous.get(raw.id);
|
||||
if (prior?.raw === raw) {
|
||||
next.set(raw.id, prior);
|
||||
continue;
|
||||
}
|
||||
const shell = presentThreadShell(LOCAL_ENVIRONMENT, raw);
|
||||
let status = resolveStatus(shell);
|
||||
if (status === "ready" && shell.latestRun?.status === "failed") status = "failed";
|
||||
const attention = status === "input" || status === "request" || status === "approval" || status === "failed" || status === "limited" ? `${shell.latestRun?.runId ?? ""}:${status}` : null;
|
||||
const completedAt = Date.parse(shell.latestRun?.completedAt ?? "");
|
||||
const settled = status === "ready" && shell.latestRun?.status === "completed";
|
||||
const state: Observed = { raw, status, attention, settled, completedAt: settled && Number.isFinite(completedAt) ? completedAt : null };
|
||||
next.set(raw.id, state);
|
||||
changed.push([prior, state]);
|
||||
}
|
||||
previous = next;
|
||||
gate.retain(new Set(next.keys()));
|
||||
for (const [prior, state] of changed) {
|
||||
gate.observe(state.raw.id, prior, state, { archived: state.raw.archivedAt !== null, quiet: state.raw.latestRunStopRequested === true });
|
||||
}
|
||||
},
|
||||
/** The shell went away: the next snapshot counts as a first sighting again. */
|
||||
clear(): void {
|
||||
gate.clear();
|
||||
previous = new Map();
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function alertTitle(kind: ThreadNotificationKind, status: ThreadStatus): string {
|
||||
if (kind === "completion") return "Thread completed";
|
||||
switch (status) {
|
||||
case "approval":
|
||||
return "Approval needed";
|
||||
case "limited":
|
||||
return "Usage limit reached";
|
||||
case "failed":
|
||||
return "Thread failed";
|
||||
case "request":
|
||||
return "Guest request";
|
||||
default:
|
||||
return "Input needed";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
import { deriveThreadStructureRuntime } from "@t3tools/client-runtime/state/thread-execution";
|
||||
import { threadSupportsProviderHandoff } from "@t3tools/client-runtime/state/thread-workflows";
|
||||
import { resolveProviderInstanceDisplayName } from "@t3tools/client-runtime/state/provider-instance-display";
|
||||
import {
|
||||
isProviderAvailable,
|
||||
type ModelSelection,
|
||||
type OrchestrationProjectShell,
|
||||
type OrchestrationV2ThreadProjection,
|
||||
type ProviderInstanceId,
|
||||
type ProviderOptionDescriptor,
|
||||
type ProviderOptionSelectionValue,
|
||||
type RuntimeMode,
|
||||
type ServerConfig,
|
||||
type ServerProvider,
|
||||
type ServerProviderModel,
|
||||
type ThreadEnvMode,
|
||||
} from "@t3tools/contracts";
|
||||
import { resolveNewThreadEnvMode, resolveProjectSettings } from "@t3tools/shared/projectSettings";
|
||||
|
||||
/** Providers you can pick, as the web's picker: enabled and not unavailable. */
|
||||
export function selectableProviders(config: ServerConfig | null): ServerProvider[] {
|
||||
return (config?.providers ?? []).filter((provider) => provider.enabled && isProviderAvailable(provider));
|
||||
}
|
||||
|
||||
export function providerName(provider: Pick<ServerProvider, "instanceId" | "driver" | "displayName">): string {
|
||||
return resolveProviderInstanceDisplayName(provider);
|
||||
}
|
||||
|
||||
/** As the web's default model for an instance: its default, else the first built-in model. */
|
||||
export function defaultModel(provider: ServerProvider): ServerProviderModel | undefined {
|
||||
return provider.models.find((model) => model.isDefault && !model.isCustom) ?? provider.models.find((model) => !model.isCustom) ?? provider.models[0];
|
||||
}
|
||||
|
||||
export function findModel(config: ServerConfig | null, selection: ModelSelection): { provider: ServerProvider; model: ServerProviderModel | undefined } | null {
|
||||
const provider = config?.providers.find((candidate) => candidate.instanceId === selection.instanceId);
|
||||
if (!provider) return null;
|
||||
return { provider, model: provider.models.find((model) => model.slug === selection.model || model.aliases?.includes(selection.model)) };
|
||||
}
|
||||
|
||||
/** The model's adjustable options (thinking level, speed, …), as the server describes them. */
|
||||
export function optionDescriptors(config: ServerConfig | null, selection: ModelSelection): readonly ProviderOptionDescriptor[] {
|
||||
return findModel(config, selection)?.model?.capabilities?.optionDescriptors ?? [];
|
||||
}
|
||||
|
||||
/** The option's value in this selection, else the descriptor's current or default value. */
|
||||
export function optionValue(selection: ModelSelection, descriptor: ProviderOptionDescriptor): ProviderOptionSelectionValue | undefined {
|
||||
const chosen = selection.options?.find((option) => option.id === descriptor.id)?.value;
|
||||
if (chosen !== undefined) return chosen;
|
||||
if (descriptor.type === "boolean") return descriptor.currentValue;
|
||||
return descriptor.currentValue ?? descriptor.options.find((choice) => choice.isDefault)?.id;
|
||||
}
|
||||
|
||||
export function optionValueLabel(descriptor: ProviderOptionDescriptor, value: ProviderOptionSelectionValue | undefined): string {
|
||||
if (descriptor.type === "boolean") return value === true ? "on" : "off";
|
||||
return descriptor.options.find((choice) => choice.id === value)?.label ?? String(value ?? "default");
|
||||
}
|
||||
|
||||
export function withOption(selection: ModelSelection, id: string, value: ProviderOptionSelectionValue): ModelSelection {
|
||||
return { ...selection, options: [...(selection.options ?? []).filter((option) => option.id !== id), { id, value }] };
|
||||
}
|
||||
|
||||
/** A switch to another model keeps the options the new model also has. */
|
||||
export function withModel(config: ServerConfig | null, current: ModelSelection, instanceId: ProviderInstanceId, model: string): ModelSelection {
|
||||
const next: ModelSelection = { instanceId, model };
|
||||
const descriptors = optionDescriptors(config, next);
|
||||
const kept = (current.options ?? []).filter((option) => descriptors.some((descriptor) => descriptor.id === option.id));
|
||||
return kept.length > 0 ? { ...next, options: kept } : next;
|
||||
}
|
||||
|
||||
/** "Opus 4.7 · High · Fast": the model, then each option that differs from off. */
|
||||
export function modelSummary(config: ServerConfig | null, selection: ModelSelection): string {
|
||||
const found = findModel(config, selection);
|
||||
const name = found?.model?.shortName ?? found?.model?.name ?? selection.model;
|
||||
const options = optionDescriptors(config, selection).flatMap((descriptor) => {
|
||||
const value = optionValue(selection, descriptor);
|
||||
if (descriptor.type === "boolean") return value === true ? [descriptor.label] : [];
|
||||
return value === undefined ? [] : [optionValueLabel(descriptor, value)];
|
||||
});
|
||||
return [name, ...options].join(" · ");
|
||||
}
|
||||
|
||||
/**
|
||||
* Which providers a started thread may move to, as the web's
|
||||
* getStartedThreadModelChangeBlockReason: any, before a session starts or when the
|
||||
* provider supports handoff; otherwise only the one it runs on.
|
||||
*/
|
||||
export function lockedProviderInstance(projection: OrchestrationV2ThreadProjection | null | undefined): ProviderInstanceId | null {
|
||||
if (!projection) return null;
|
||||
const runtime = deriveThreadStructureRuntime(projection);
|
||||
if (runtime === null || threadSupportsProviderHandoff(projection)) return null;
|
||||
return runtime.providerInstanceId;
|
||||
}
|
||||
|
||||
export const RUNTIME_MODES: readonly RuntimeMode[] = ["approval-required", "auto-accept-edits", "auto", "full-access"];
|
||||
|
||||
export interface DraftThread {
|
||||
readonly projectId: OrchestrationProjectShell["id"];
|
||||
readonly modelSelection: ModelSelection;
|
||||
readonly runtimeMode: RuntimeMode;
|
||||
readonly envMode: ThreadEnvMode;
|
||||
}
|
||||
|
||||
/**
|
||||
* A new thread's defaults, as the web's new-thread handler: the project's or machine's
|
||||
* default model, else the current thread's, else the first selectable provider's default;
|
||||
* the default mode; and local or worktree from the project settings.
|
||||
*/
|
||||
export function draftDefaults(input: {
|
||||
readonly config: ServerConfig | null;
|
||||
readonly project: OrchestrationProjectShell;
|
||||
readonly currentModel: ModelSelection | null;
|
||||
readonly projectHasThreads: boolean;
|
||||
}): DraftThread {
|
||||
const settings = input.config ? resolveProjectSettings(input.config.settings, input.project.id, input.project, null) : null;
|
||||
const firstProvider = selectableProviders(input.config)[0];
|
||||
const firstModel = firstProvider ? defaultModel(firstProvider) : undefined;
|
||||
const modelSelection: ModelSelection = settings?.settings.defaultModelSelection ??
|
||||
input.currentModel ?? { instanceId: (firstProvider?.instanceId ?? "codex") as ProviderInstanceId, model: firstModel?.slug ?? "default" };
|
||||
const envMode = settings
|
||||
? resolveNewThreadEnvMode({ projectSettings: settings, workspaceRoot: input.project.workspaceRoot, newProjectsRoot: null, projectHasThreads: input.projectHasThreads })
|
||||
: "local";
|
||||
return { projectId: input.project.id, modelSelection, runtimeMode: settings?.settings.defaultRuntimeMode ?? "full-access", envMode };
|
||||
}
|
||||
|
||||
/** Same model with the same effective options, counting defaults as chosen. */
|
||||
export function sameSelection(config: ServerConfig | null, a: ModelSelection, b: ModelSelection): boolean {
|
||||
if (a.instanceId !== b.instanceId || a.model !== b.model) return false;
|
||||
return optionDescriptors(config, a).every((descriptor) => optionValue(a, descriptor) === optionValue(b, descriptor));
|
||||
}
|
||||
@@ -88,3 +88,34 @@ export function paletteGroups(input: {
|
||||
export function paletteEntries(groups: readonly PaletteGroup[]): PaletteEntry[] {
|
||||
return groups.flatMap((group) => group.entries);
|
||||
}
|
||||
|
||||
/** A choice in a picker (model, option, project, terminal…). */
|
||||
export interface PickerItem {
|
||||
readonly id: string;
|
||||
readonly title: string;
|
||||
/** Shown on the right: "current", a provider, a status. */
|
||||
readonly hint?: string;
|
||||
readonly run: () => void;
|
||||
}
|
||||
|
||||
export interface Picker {
|
||||
readonly placeholder: string;
|
||||
readonly groups: readonly { readonly label: string; readonly items: readonly PickerItem[] }[];
|
||||
}
|
||||
|
||||
/** A picker's groups filtered and ranked by the query, in the palette's shape. */
|
||||
export function pickerGroups(picker: Picker, query: string): PaletteGroup[] {
|
||||
const folded = foldSearchQuery(query);
|
||||
return picker.groups
|
||||
.map((group) => {
|
||||
const items = folded
|
||||
? group.items
|
||||
.map((item) => ({ item, match: scoreSearchTerms(folded, [item.title, group.label, item.hint ?? ""], { typos: true }) }))
|
||||
.filter((candidate) => candidate.match !== null)
|
||||
.sort((a, b) => b.match!.score - a.match!.score)
|
||||
.map(({ item }) => item)
|
||||
: group.items;
|
||||
return { label: group.label, entries: items.map((item): PaletteEntry => ({ kind: "command", command: { id: item.id, title: item.title, ...(item.hint ? { shortcut: item.hint } : {}) } })) };
|
||||
})
|
||||
.filter((group) => group.entries.length > 0);
|
||||
}
|
||||
@@ -15,6 +15,12 @@ const LABELS: { readonly [K in keyof Tui3Config]: { readonly label: string; read
|
||||
sendShortcut: { label: "Send with", help: "ctrl+enter needs kitty keyboard support; alt+enter also sends", values: { enter: "enter", "ctrl-enter": "ctrl+enter (enter adds a line)" } },
|
||||
timestampFormat: { label: "Times", help: "automation schedules", values: { locale: "system locale", "12-hour": "12-hour", "24-hour": "24-hour" } },
|
||||
diffIgnoreWhitespace: { label: "Diffs", help: "whitespace-only changes", values: { true: "ignore whitespace", false: "show whitespace" } },
|
||||
notificationMode: {
|
||||
label: "Alerts",
|
||||
help: "when a thread needs you or finishes; notifications only while the terminal is unfocused",
|
||||
values: { off: "off", sound: "bell", notifications: "desktop notifications", "notifications-and-sound": "bell and desktop notifications" },
|
||||
},
|
||||
inAppNotificationsEnabled: { label: "In-app alerts", help: "a line at the bottom for other threads", values: { true: "on", false: "off" } },
|
||||
};
|
||||
|
||||
/** tui3's own settings, in display order, each cycled with enter. */
|
||||
|
||||
Reference in new issue
Block a user