Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8481e20015 | ||
|
|
0b3a318d19 | ||
|
|
64a6b8c451 |
No files matched your search
@@ -2,7 +2,6 @@ import * as NodeOS from "node:os";
|
||||
|
||||
import { assert, expect, it } from "@effect/vitest";
|
||||
import * as ConfigProvider from "effect/ConfigProvider";
|
||||
import * as Duration from "effect/Duration";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as FileSystem from "effect/FileSystem";
|
||||
import * as Layer from "effect/Layer";
|
||||
@@ -17,28 +16,10 @@ import {
|
||||
import * as NetService from "@t3tools/shared/Net";
|
||||
import * as NodeServices from "@effect/platform-node/NodeServices";
|
||||
import { deriveServerPaths } from "../config.ts";
|
||||
import { DurationFromString, resolveServerConfig } from "./config.ts";
|
||||
import { resolveServerConfig } from "./config.ts";
|
||||
|
||||
const encodeDesktopBootstrap = Schema.encodeEffect(Schema.fromJsonString(DesktopBackendBootstrap));
|
||||
|
||||
it.effect("decodes compact duration shorthands", () =>
|
||||
Effect.gen(function* () {
|
||||
const duration = yield* Schema.decodeUnknownEffect(DurationFromString)("5m");
|
||||
|
||||
assert.strictEqual(Duration.toMillis(duration), Duration.toMillis(Duration.minutes(5)));
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("rejects invalid duration strings", () =>
|
||||
Effect.gen(function* () {
|
||||
const error = yield* Effect.flip(
|
||||
Schema.decodeUnknownEffect(DurationFromString)("not-a-duration"),
|
||||
);
|
||||
|
||||
assert.include(String(error), "Invalid duration");
|
||||
}),
|
||||
);
|
||||
|
||||
const makeDesktopBootstrap = (
|
||||
overrides: Partial<DesktopBackendBootstrapValue> = {},
|
||||
): DesktopBackendBootstrapValue => ({
|
||||
|
||||
@@ -395,9 +395,9 @@ export const resolveCliAuthConfig = (
|
||||
|
||||
const DurationShorthandPattern = /^(?<value>\d+)(?<unit>ms|s|m|h|d|w)$/i;
|
||||
|
||||
const parseDurationInput = (value: string): Option.Option<Duration.Duration> => {
|
||||
const parseDurationInput = (value: string): Duration.Duration | null => {
|
||||
const trimmed = value.trim();
|
||||
if (trimmed.length === 0) return Option.none();
|
||||
if (trimmed.length === 0) return null;
|
||||
|
||||
const shorthand = DurationShorthandPattern.exec(trimmed);
|
||||
const normalizedInput = shorthand?.groups
|
||||
@@ -405,32 +405,35 @@ const parseDurationInput = (value: string): Option.Option<Duration.Duration> =>
|
||||
const amountText = shorthand.groups.value;
|
||||
const unitText = shorthand.groups.unit;
|
||||
if (typeof amountText !== "string" || typeof unitText !== "string") {
|
||||
return Option.none<Duration.Input>();
|
||||
return null;
|
||||
}
|
||||
|
||||
const amount = Number.parseInt(amountText, 10);
|
||||
if (!Number.isFinite(amount)) return Option.none<Duration.Input>();
|
||||
if (!Number.isFinite(amount)) return null;
|
||||
|
||||
switch (unitText.toLowerCase()) {
|
||||
case "ms":
|
||||
return Option.some(`${amount} millis` as Duration.Input);
|
||||
return `${amount} millis`;
|
||||
case "s":
|
||||
return Option.some(`${amount} seconds` as Duration.Input);
|
||||
return `${amount} seconds`;
|
||||
case "m":
|
||||
return Option.some(`${amount} minutes` as Duration.Input);
|
||||
return `${amount} minutes`;
|
||||
case "h":
|
||||
return Option.some(`${amount} hours` as Duration.Input);
|
||||
return `${amount} hours`;
|
||||
case "d":
|
||||
return Option.some(`${amount} days` as Duration.Input);
|
||||
return `${amount} days`;
|
||||
case "w":
|
||||
return Option.some(`${amount} weeks` as Duration.Input);
|
||||
return `${amount} weeks`;
|
||||
default:
|
||||
return Option.none<Duration.Input>();
|
||||
return null;
|
||||
}
|
||||
})()
|
||||
: Option.some(trimmed as Duration.Input);
|
||||
: (trimmed as Duration.Input);
|
||||
|
||||
return Option.flatMap(normalizedInput, (input) => Duration.fromInput(input));
|
||||
if (normalizedInput === null) return null;
|
||||
|
||||
const decoded = Duration.fromInput(normalizedInput as Duration.Input);
|
||||
return Option.isSome(decoded) ? decoded.value : null;
|
||||
};
|
||||
|
||||
export const DurationFromString = Schema.String.pipe(
|
||||
@@ -439,15 +442,14 @@ export const DurationFromString = Schema.String.pipe(
|
||||
SchemaTransformation.transformOrFail({
|
||||
decode: (value) => {
|
||||
const duration = parseDurationInput(value);
|
||||
return Option.match(duration, {
|
||||
onSome: Effect.succeed,
|
||||
onNone: () =>
|
||||
Effect.fail(
|
||||
new SchemaIssue.InvalidValue(Option.some(value), {
|
||||
message: "Invalid duration. Use values like 5m, 1h, 30d, or 15 minutes.",
|
||||
}),
|
||||
),
|
||||
});
|
||||
if (duration !== null) {
|
||||
return Effect.succeed(duration);
|
||||
}
|
||||
return Effect.fail(
|
||||
new SchemaIssue.InvalidValue(Option.some(value), {
|
||||
message: "Invalid duration. Use values like 5m, 1h, 30d, or 15 minutes.",
|
||||
}),
|
||||
);
|
||||
},
|
||||
encode: (duration) => Effect.succeed(Duration.format(duration)),
|
||||
}),
|
||||
|
||||
@@ -5,12 +5,27 @@ import {
|
||||
} from "@t3tools/contracts";
|
||||
import { resolveSelectableModel } from "@t3tools/shared/model";
|
||||
import { LegendList, type LegendListRef } from "@legendapp/list/react";
|
||||
import { memo, useMemo, useState, useCallback, useEffect, useLayoutEffect, useRef } from "react";
|
||||
import {
|
||||
memo,
|
||||
useMemo,
|
||||
useState,
|
||||
useCallback,
|
||||
useEffect,
|
||||
useLayoutEffect,
|
||||
useRef,
|
||||
type ChangeEvent,
|
||||
type KeyboardEvent,
|
||||
type RefObject,
|
||||
} from "react";
|
||||
import { SearchIcon } from "lucide-react";
|
||||
import { ModelListRow } from "./ModelListRow";
|
||||
import { ModelPickerSidebar } from "./ModelPickerSidebar";
|
||||
import { isModelPickerNewModel } from "./modelPickerModelHighlights";
|
||||
import { buildModelPickerSearchText, scoreModelPickerSearch } from "./modelPickerSearch";
|
||||
import {
|
||||
buildModelPickerSearchText,
|
||||
normalizeModelPickerSearchQuery,
|
||||
scoreModelPickerSearch,
|
||||
} from "./modelPickerSearch";
|
||||
import { Combobox, ComboboxEmpty, ComboboxInput, ComboboxListVirtualized } from "../ui/combobox";
|
||||
import { ModelEsque } from "./providerIconUtils";
|
||||
import {
|
||||
@@ -43,6 +58,72 @@ type ModelPickerItem = {
|
||||
|
||||
const EMPTY_MODEL_JUMP_LABELS = new Map<string, string>();
|
||||
|
||||
const ModelPickerSearchInput = memo(function ModelPickerSearchInput(props: {
|
||||
inputRef: RefObject<HTMLInputElement | null>;
|
||||
onNormalizedSearchQueryChange: (query: string) => void;
|
||||
onRequestClose?: () => void;
|
||||
onSelectHighlightedModel: () => boolean;
|
||||
}) {
|
||||
const { inputRef, onNormalizedSearchQueryChange, onRequestClose, onSelectHighlightedModel } =
|
||||
props;
|
||||
const [rawSearchQuery, setRawSearchQuery] = useState("");
|
||||
const publishedSearchQueryRef = useRef(normalizeModelPickerSearchQuery(rawSearchQuery));
|
||||
|
||||
const handleSearchChange = useCallback(
|
||||
(event: ChangeEvent<HTMLInputElement>) => {
|
||||
const nextRawSearchQuery = event.target.value;
|
||||
setRawSearchQuery(nextRawSearchQuery);
|
||||
|
||||
const nextSearchQuery = normalizeModelPickerSearchQuery(nextRawSearchQuery);
|
||||
if (nextSearchQuery === publishedSearchQueryRef.current) {
|
||||
return;
|
||||
}
|
||||
publishedSearchQueryRef.current = nextSearchQuery;
|
||||
onNormalizedSearchQueryChange(nextSearchQuery);
|
||||
},
|
||||
[onNormalizedSearchQueryChange],
|
||||
);
|
||||
|
||||
const handleSearchKeyDown = useCallback(
|
||||
(event: KeyboardEvent<HTMLInputElement>) => {
|
||||
if (event.key === "Escape") {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
onRequestClose?.();
|
||||
return;
|
||||
}
|
||||
if (event.key === "Enter" && onSelectHighlightedModel()) {
|
||||
(event as typeof event & { preventBaseUIHandler?: () => void }).preventBaseUIHandler?.();
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
return;
|
||||
}
|
||||
event.stopPropagation();
|
||||
},
|
||||
[onRequestClose, onSelectHighlightedModel],
|
||||
);
|
||||
|
||||
return (
|
||||
<ComboboxInput
|
||||
ref={inputRef}
|
||||
className="[&_input]:h-6.5 [&_input]:font-sans [&_input]:leading-6.5"
|
||||
inputClassName="rounded-none bg-transparent text-sm"
|
||||
placeholder="Search models..."
|
||||
showTrigger={false}
|
||||
startAddon={
|
||||
<SearchIcon className="-translate-x-0.5 size-4 shrink-0 text-muted-foreground/55" />
|
||||
}
|
||||
value={rawSearchQuery}
|
||||
onChange={handleSearchChange}
|
||||
onKeyDown={handleSearchKeyDown}
|
||||
onMouseDown={(event) => event.stopPropagation()}
|
||||
onTouchStart={(event) => event.stopPropagation()}
|
||||
size="sm"
|
||||
unstyled
|
||||
/>
|
||||
);
|
||||
});
|
||||
|
||||
// Split a `${instanceId}:${slug}` combobox key back into its pieces. Slugs
|
||||
// can contain colons (e.g. some vendor model ids), so we only split on the
|
||||
// first colon — anything after that is the slug.
|
||||
@@ -221,7 +302,7 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {
|
||||
}, [modelOptionsByInstance, entryByInstanceId, readyInstanceSet]);
|
||||
|
||||
const isLocked = props.lockedProvider !== null;
|
||||
const isSearching = searchQuery.trim().length > 0;
|
||||
const isSearching = searchQuery.length > 0;
|
||||
const lockedDisabledInstanceIds = useMemo(() => {
|
||||
if (!isLocked) {
|
||||
return undefined;
|
||||
@@ -261,7 +342,7 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {
|
||||
let result = flatModels;
|
||||
|
||||
// Apply tokenized fuzzy search across the combined provider/model search fields.
|
||||
if (searchQuery.trim()) {
|
||||
if (searchQuery) {
|
||||
const rankedMatches = result
|
||||
.map((model) => ({
|
||||
model,
|
||||
@@ -385,6 +466,15 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {
|
||||
},
|
||||
[entryByInstanceId, getModelDisabledReason, modelOptionsByInstance, onInstanceModelChange],
|
||||
);
|
||||
const selectHighlightedModel = useCallback((): boolean => {
|
||||
const highlightedModelKey = highlightedModelKeyRef.current;
|
||||
if (!highlightedModelKey) {
|
||||
return false;
|
||||
}
|
||||
const { instanceId, slug } = splitInstanceModelKey(highlightedModelKey);
|
||||
handleModelSelect(slug, instanceId);
|
||||
return true;
|
||||
}, [handleModelSelect]);
|
||||
|
||||
const toggleFavorite = useCallback(
|
||||
(instanceId: ProviderInstanceId, model: string) => {
|
||||
@@ -577,42 +667,11 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {
|
||||
{/* Search bar */}
|
||||
<div className="px-4 pt-2.5">
|
||||
<div className="-translate-y-px border-b border-border/70 pb-2.5 transition-colors focus-within:border-ring">
|
||||
<ComboboxInput
|
||||
ref={searchInputRef}
|
||||
className="[&_input]:h-6.5 [&_input]:font-sans [&_input]:leading-6.5"
|
||||
inputClassName="rounded-none bg-transparent text-sm"
|
||||
placeholder="Search models..."
|
||||
showTrigger={false}
|
||||
startAddon={
|
||||
<SearchIcon className="-translate-x-0.5 size-4 shrink-0 text-muted-foreground/55" />
|
||||
}
|
||||
value={searchQuery}
|
||||
onChange={(e) => setSearchQuery(e.target.value)}
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === "Escape") {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
props.onRequestClose?.();
|
||||
return;
|
||||
}
|
||||
if (e.key === "Enter" && highlightedModelKeyRef.current) {
|
||||
(
|
||||
e as typeof e & { preventBaseUIHandler?: () => void }
|
||||
).preventBaseUIHandler?.();
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
const { instanceId, slug } = splitInstanceModelKey(
|
||||
highlightedModelKeyRef.current,
|
||||
);
|
||||
handleModelSelect(slug, instanceId);
|
||||
return;
|
||||
}
|
||||
e.stopPropagation();
|
||||
}}
|
||||
onMouseDown={(e) => e.stopPropagation()}
|
||||
onTouchStart={(e) => e.stopPropagation()}
|
||||
size="sm"
|
||||
unstyled
|
||||
<ModelPickerSearchInput
|
||||
inputRef={searchInputRef}
|
||||
onNormalizedSearchQueryChange={setSearchQuery}
|
||||
onSelectHighlightedModel={selectHighlightedModel}
|
||||
{...(props.onRequestClose ? { onRequestClose: props.onRequestClose } : {})}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
import { describe, expect, it } from "vite-plus/test";
|
||||
|
||||
import { buildModelPickerSearchText, scoreModelPickerSearch } from "./modelPickerSearch";
|
||||
import {
|
||||
buildModelPickerSearchText,
|
||||
normalizeModelPickerSearchQuery,
|
||||
scoreModelPickerSearch,
|
||||
} from "./modelPickerSearch";
|
||||
|
||||
describe("buildModelPickerSearchText", () => {
|
||||
it("builds provider-agnostic search text from generic fields", () => {
|
||||
@@ -15,6 +19,13 @@ describe("buildModelPickerSearchText", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("normalizeModelPickerSearchQuery", () => {
|
||||
it("collapses search text to the effective query used by ranking", () => {
|
||||
expect(normalizeModelPickerSearchQuery(" GPT 5.5 ")).toBe("gpt 5.5");
|
||||
expect(normalizeModelPickerSearchQuery(" ")).toBe("");
|
||||
});
|
||||
});
|
||||
|
||||
describe("scoreModelPickerSearch", () => {
|
||||
it("matches typo-tolerant multi-token queries", () => {
|
||||
expect(
|
||||
|
||||
@@ -52,6 +52,13 @@ export function buildModelPickerSearchText(model: ModelPickerSearchableModel): s
|
||||
);
|
||||
}
|
||||
|
||||
export function normalizeModelPickerSearchQuery(query: string): string {
|
||||
return normalizeSearchQuery(query)
|
||||
.split(/\s+/u)
|
||||
.filter((token) => token.length > 0)
|
||||
.join(" ");
|
||||
}
|
||||
|
||||
export function scoreModelPickerSearch(
|
||||
model: ModelPickerSearchableModel,
|
||||
query: string,
|
||||
|
||||
@@ -3,7 +3,7 @@ import {
|
||||
type GitActionProgressEvent,
|
||||
type GitRunStackedActionResult,
|
||||
} from "@t3tools/contracts";
|
||||
import { assert, describe, expect, it } from "@effect/vitest";
|
||||
import { describe, expect, it } from "@effect/vitest";
|
||||
import * as Cause from "effect/Cause";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Layer from "effect/Layer";
|
||||
@@ -64,16 +64,7 @@ function progress<T extends GitActionProgressEvent>(event: T): T {
|
||||
}
|
||||
|
||||
describe("vcsActionState", () => {
|
||||
it("round-trips target keys through a Schema JSON codec", () => {
|
||||
const key = getVcsActionTargetKey({ environmentId, cwd });
|
||||
|
||||
if (key === null) {
|
||||
assert.fail("expected target key to be encoded");
|
||||
}
|
||||
assert.deepStrictEqual(parseVcsActionTargetKey(key), { environmentId, cwd });
|
||||
});
|
||||
|
||||
it("preserves malformed target key diagnostics without copying the key", () => {
|
||||
it("preserves malformed target key diagnostics and the native cause without copying the key", () => {
|
||||
const key = "not-json-with-credential=do-not-log";
|
||||
let error: unknown;
|
||||
|
||||
@@ -83,17 +74,16 @@ describe("vcsActionState", () => {
|
||||
error = cause;
|
||||
}
|
||||
|
||||
assert.instanceOf(error, VcsActionTargetKeyParseError);
|
||||
const parseError = error as VcsActionTargetKeyParseError;
|
||||
assert.strictEqual(parseError.keyLength, key.length);
|
||||
assert.notProperty(parseError, "key");
|
||||
assert.notInclude(parseError.message, key);
|
||||
expect(error).toBeInstanceOf(VcsActionTargetKeyParseError);
|
||||
expect(error).toMatchObject({ keyLength: key.length, cause: expect.any(SyntaxError) });
|
||||
expect(error).not.toHaveProperty("key");
|
||||
expect((error as Error).message).not.toContain(key);
|
||||
});
|
||||
|
||||
it("rejects invalid target key shapes", () => {
|
||||
const key = JSON.stringify([environmentId]);
|
||||
|
||||
assert.throws(() => parseVcsActionTargetKey(key), VcsActionTargetKeyParseError);
|
||||
expect(() => parseVcsActionTargetKey(key)).toThrowError(VcsActionTargetKeyParseError);
|
||||
});
|
||||
|
||||
it("projects phase and hook progress without owning the async operation", () => {
|
||||
|
||||
@@ -157,9 +157,9 @@ export const EMPTY_VCS_ACTION_STATE = Object.freeze<VcsActionState>({
|
||||
|
||||
const nowMs = (): number => DateTime.toEpochMillis(DateTime.nowUnsafe());
|
||||
let nextLocalActionId = 0;
|
||||
const VcsActionTargetKeyJson = Schema.fromJsonString(Schema.Tuple([EnvironmentId, Schema.String]));
|
||||
const decodeVcsActionTargetKey = Schema.decodeUnknownSync(VcsActionTargetKeyJson);
|
||||
const encodeVcsActionTargetKey = Schema.encodeSync(VcsActionTargetKeyJson);
|
||||
const decodeVcsActionTargetKey = Schema.decodeUnknownSync(
|
||||
Schema.Tuple([EnvironmentId, Schema.String]),
|
||||
);
|
||||
|
||||
export const vcsActionStateAtom = Atom.family((key: string) => {
|
||||
return Atom.make(EMPTY_VCS_ACTION_STATE).pipe(
|
||||
@@ -177,12 +177,12 @@ export function getVcsActionTargetKey(target: VcsActionTarget): string | null {
|
||||
if (target.environmentId === null || target.cwd === null) {
|
||||
return null;
|
||||
}
|
||||
return encodeVcsActionTargetKey([target.environmentId, target.cwd]);
|
||||
return JSON.stringify([target.environmentId, target.cwd]);
|
||||
}
|
||||
|
||||
export function parseVcsActionTargetKey(key: string): ResolvedVcsActionTarget {
|
||||
try {
|
||||
const [environmentId, cwd] = decodeVcsActionTargetKey(key);
|
||||
const [environmentId, cwd] = decodeVcsActionTargetKey(JSON.parse(key));
|
||||
return { environmentId, cwd };
|
||||
} catch (cause) {
|
||||
throw new VcsActionTargetKeyParseError({ keyLength: key.length, cause });
|
||||
|
||||
Reference in new issue
Block a user