Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 2 additions & 14 deletions app/.oxlintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@
"vn/known-color-steps": "error",
"vn/resource-read-latest": "error",
"vn/row-columns": "error",
"vn/no-query-result-cast": "error"
"vn/no-query-result-cast": "error",
"vn/no-query-type-redeclare": "error"
},
"overrides": [
{
Expand All @@ -100,19 +101,6 @@
"files": ["**/*.test.ts"],
"rules": { "vn/no-router-actions-in-server": "off" }
},
{
// Casts left to clean up. Remove a file once its local copy of the
// query's type is gone.
"files": [
"src/components/ai/LLMSidebar.tsx",
"src/routes/embeddings/[[]id].tsx",
"src/routes/umap/[[]id]/index.tsx",
"src/routes/ai/prompts/[[]id].tsx",
"src/routes/ai/index.tsx",
"src/routes/ai/runs/[[]id].tsx"
],
"rules": { "vn/no-query-result-cast": "off" }
},
{
"files": ["src/lib/collections/colors.ts"],
"rules": { "vn/color-tokens": "off" }
Expand Down
44 changes: 44 additions & 0 deletions app/lint/oxlint-plugin-vn.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// Repo-specific guard rules for oxlint, loaded through `jsPlugins` in
// .oxlintrc.json. Each rule reports every node that matches its selector.
import { readdirSync, readFileSync } from "node:fs";
import path from "node:path";

const selectorRule = (selector, message) => ({
create(context) {
return {
Expand Down Expand Up @@ -163,6 +166,46 @@ const rowColumns = {
},
};

// Type names that a `*.queries.ts` module exports, with that module's path.
// Read once per lint run.
const queryTypes = () => {
const src = path.join(import.meta.dirname, "../src");
const names = new Map();
for (const file of readdirSync(src, { recursive: true })) {
if (!file.endsWith(".queries.ts")) continue;
const text = readFileSync(path.join(src, file), "utf8");
for (const [, name] of text.matchAll(
/^export (?:type|interface) (\w+)/gm,
)) {
names.set(name, `~/${file.slice(0, -3)}`);
}
}
return names;
};

let queryTypeNames;

// A second declaration of a query's type compiles for as long as its fields
// stay assignable, so it drifts without an error.
const noQueryTypeRedeclare = {
create(context) {
if (context.filename.endsWith(".queries.ts")) return {};
queryTypeNames ??= queryTypes();
const check = ({ id }) => {
const from = queryTypeNames.get(id.name);
if (!from) return;
context.report({
node: id,
message: `\`${id.name}\` is a query module's type. Import it from ${from}, or give a different type a different name (derive it with Pick or Omit).`,
});
};
return {
TSTypeAliasDeclaration: check,
TSInterfaceDeclaration: check,
};
},
};

export default {
meta: { name: "vn" },
rules: {
Expand All @@ -184,6 +227,7 @@ export default {
":matches(Property[key.name=/^(color|bg|background|backgroundColor|borderColor|outlineColor|fill|stroke|stopColor)$/], JSXAttribute[name.name=/^(color|bg|background|backgroundColor|borderColor|outlineColor|fill|stroke|stopColor)$/]) > Literal[value=/^#[0-9a-fA-F]{3,8}$/]",
"Use a color token (fg.*, bg.*, border.*, or a palette step) instead of a raw hex color, so light and dark mode and the palette stay in step.",
),
"no-query-type-redeclare": noQueryTypeRedeclare,
"no-query-result-cast": selectorRule(
"TSAsExpression:matches([expression.callee.name=/^fetch[A-Z]/], [expression.argument.callee.name=/^fetch[A-Z]/])",
"Do not cast a fetch* query's result. Its return type is the contract: import the type from the query module, or derive it with Awaited<ReturnType<typeof fetchX>>, instead of copying it here.",
Expand Down
4 changes: 2 additions & 2 deletions app/src/components/ai-dashboard/PromptDialogs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import { Text } from "~/components/ui/text";
import { Textarea } from "~/components/ui/textarea";
import { deletePrompt, updatePrompt } from "~/services/prompts/prompts.actions";
import { fetchPrompts } from "~/services/prompts/prompts.queries";
import type { DashboardPrompt } from "./ai-dashboard-model";
import type { Prompt } from "~/services/prompts/prompts.queries";

type PromptDialogProps = {
prompt: DashboardPrompt | null;
prompt: Prompt | null;
onClose: () => void;
};

Expand Down
9 changes: 5 additions & 4 deletions app/src/components/ai-dashboard/PromptsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import { IconButton } from "~/components/ui/icon-button";
import * as Table from "~/components/ui/table";
import { Text } from "~/components/ui/text";
import { formatRelativeTimestamp } from "~/features/umap/format";
import type { DashboardPrompt, PromptRunStats } from "./ai-dashboard-model";
import type { Prompt } from "~/services/prompts/prompts.queries";
import type { PromptRunStats } from "./ai-dashboard-model";

type PromptsTableProps = {
prompts: DashboardPrompt[];
prompts: Prompt[];
stats: Map<string, PromptRunStats>;
onEdit: (prompt: DashboardPrompt) => void;
onDelete: (prompt: DashboardPrompt) => void;
onEdit: (prompt: Prompt) => void;
onDelete: (prompt: Prompt) => void;
};

const stop = (event: MouseEvent) => event.stopPropagation();
Expand Down
2 changes: 1 addition & 1 deletion app/src/components/ai-dashboard/RunStatusIcon.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AlertTriangleIcon, CheckCircle2Icon, XCircleIcon } from "lucide-solid";
import { Match, Switch } from "solid-js";
import { Box } from "styled-system/jsx";
import type { RunStatus } from "./ai-dashboard-model";
import type { RunStatus } from "~/services/ai/ai-runs.queries";

const labels: Record<RunStatus, string> = {
SUCCESS: "Succeeded",
Expand Down
40 changes: 15 additions & 25 deletions app/src/components/ai-dashboard/ai-dashboard-model.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,19 @@
export type RunStatus = "SUCCESS" | "ERROR" | "PARTIAL";
import type {
PromptRunItem,
RunStatus,
} from "~/services/ai/ai-runs.queries";

export type DashboardPrompt = {
id: string;
task: string;
description?: string | null;
defaultModel: string;
defaultTemp: number;
defaultTopP?: number | null;
activeVersion?: {
id: string;
template: string;
system?: string | null;
} | null;
};

export type DashboardRun = {
id: string;
status: RunStatus;
model: string;
createdAt: string;
error?: string | null;
outputHtml?: string | null;
promptId?: string | null;
promptTask?: string | null;
};
export type DashboardRun = Pick<
PromptRunItem,
| "id"
| "status"
| "model"
| "createdAt"
| "error"
| "outputHtml"
| "promptId"
| "promptTask"
>;

export type PromptRunStats = {
count: number;
Expand Down
19 changes: 7 additions & 12 deletions app/src/components/ai/LLMSidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
fetchChatThreads,
type ChatThreadDetail,
type ChatThreadPreview,
type ChatThreadStatus,
} from "~/services/ai/ai-chat.queries";
import {
createChatThread,
Expand All @@ -40,12 +41,6 @@ import {
} from "~/services/ai/ai-chat.actions";
import { createKeptAsync, onDemand } from "~/utils/kept-async";

type ChatThreadStatus = "ACTIVE" | "LOADING" | "DONE";

type ThreadPreview = ChatThreadPreview;

type ThreadDetail = ChatThreadDetail;

const TRANSIENT_SERVER_FUNCTION_ERROR_FRAGMENT =
"Malformed server function stream";

Expand Down Expand Up @@ -93,18 +88,18 @@ export function useLLMSidebar() {
setInitialized(true);
};
const threads = createKeptAsync(() => {
if (!initialized()) return Promise.resolve<ThreadPreview[]>([]);
if (!initialized()) return Promise.resolve<ChatThreadPreview[]>([]);
return recoverServerFunctionCall(
() => fetchChatThreads(),
"fetchChatThreads"
);
}, onDemand);
const thread = createKeptAsync(() => {
if (!initialized()) return Promise.resolve<ThreadDetail | null>(null);
if (!initialized()) return Promise.resolve<ChatThreadDetail | null>(null);
const id = selectedId();
if (!id) return Promise.resolve<ThreadDetail | null>(null);
if (!id) return Promise.resolve<ChatThreadDetail | null>(null);
return recoverServerFunctionCall(
() => fetchChatThread(id) as Promise<ThreadDetail | null>,
() => fetchChatThread(id),
"fetchChatThread"
);
}, onDemand);
Expand Down Expand Up @@ -358,10 +353,10 @@ export function useLLMSidebar() {
function LLMSidebarView(props: {
open: Accessor<boolean>;
onClose: () => void;
threads: Accessor<ThreadPreview[]>;
threads: Accessor<ChatThreadPreview[]>;
selectedId: Accessor<string | undefined>;
onSelect: (id: string) => void;
thread: Accessor<ThreadDetail | undefined>;
thread: Accessor<ChatThreadDetail | undefined>;
onRefresh: () => void;
onCreateThread: (noteId: string, title?: string) => Promise<void>;
onSend: (text: string) => Promise<void>;
Expand Down
18 changes: 4 additions & 14 deletions app/src/components/editor/ui/PromptsManagerModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,12 @@ import { Textarea } from "~/components/ui/textarea";
import { Text } from "~/components/ui/text";
import { Box, Grid, HStack, Stack } from "styled-system/jsx";
import { SimpleDialog } from "~/components/ui/simple-dialog";
import { fetchPrompts } from "~/services/prompts/prompts.queries";
import {
fetchPrompts,
type Prompt,
} from "~/services/prompts/prompts.queries";
import { createPromptVersion, createPrompt, revisePrompt } from "~/services/prompts/prompts.actions";

type Prompt = {
id: string;
task: string;
description?: string | null;
defaultModel: string;
defaultTemp: number;
defaultTopP?: number | null;
activeVersion?: {
id: string;
template: string;
system?: string | null;
} | null;
};
export function usePromptsManagerModal() {
const [open, setOpen] = createSignal(false);
const prompts = createKeptAsync(() => fetchPrompts());
Expand Down
4 changes: 2 additions & 2 deletions app/src/features/umap/UmapRunMetadataCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { Heading } from "~/components/ui/heading";
import { Link } from "~/components/ui/link";
import { Text } from "~/components/ui/text";
import { formatRelativeTimestamp } from "~/features/umap/format";
import type { UmapRunMeta } from "~/features/umap/detail-types";
import type { UmapRunDetail } from "~/services/umap/umap.queries";
import { Box, Grid, Stack } from "styled-system/jsx";

type UmapRunMetadataCardProps = {
meta: UmapRunMeta;
meta: UmapRunDetail;
};

export function UmapRunMetadataCard(props: UmapRunMetadataCardProps) {
Expand Down
28 changes: 5 additions & 23 deletions app/src/features/umap/detail-types.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,10 @@
import type { UmapRegionsSnapshot } from "./region-types";

export type UmapPoint = { docId: string; x: number; y: number; z?: number | null };

export type UmapRunMeta = {
id: string;
dims: number;
params?: Record<string, unknown> | null;
embeddingRunId: string;
createdAt: string;
count?: number;
hasArtifact?: boolean;
artifactPath?: string | null;
regions?: UmapRegionsSnapshot | null;
regionCount?: number;
};

export type UmapRunPointsData = {
runId: string;
dims: number;
points: UmapPoint[];
};
import type {
UmapPoint,
UmapRunDetail,
} from "~/services/umap/umap.queries";

export type UmapDetailData = {
meta: UmapRunMeta;
meta: UmapRunDetail;
dims: number;
points: UmapPoint[];
};
2 changes: 1 addition & 1 deletion app/src/layout/umap-normalize.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { UmapRegionsSnapshot } from "~/features/umap/region-types";
import type { UmapPoint } from "~/types/notes";
import type { UmapPoint } from "~/services/umap/umap.queries";

type UmapNormalization = {
centerX: number;
Expand Down
12 changes: 5 additions & 7 deletions app/src/routes/ai/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { PaidCallsTable } from "~/components/ai-dashboard/PaidCallsTable";
import { PromptsTable } from "~/components/ai-dashboard/PromptsTable";
import { RunsTable } from "~/components/ai-dashboard/RunsTable";
import {
type DashboardPrompt,
type DashboardRun,
RUN_STATUS_FILTERS,
parseRunStatusFilter,
runMatchesQuery,
Expand All @@ -20,16 +18,16 @@ import { Button } from "~/components/ui/button";
import { Input } from "~/components/ui/input";
import { Text } from "~/components/ui/text";
import { fetchPromptRuns } from "~/services/ai/ai-runs.queries";
import { fetchPrompts } from "~/services/prompts/prompts.queries";
import { fetchPrompts, type Prompt } from "~/services/prompts/prompts.queries";

const RUN_LIMIT = 100;

const AiDashboard: VoidComponent = () => {
const prompts = createAsync(() => fetchPrompts() as Promise<DashboardPrompt[]>);
const runs = createAsync(() => fetchPromptRuns({ limit: RUN_LIMIT }) as Promise<DashboardRun[]>);
const prompts = createAsync(() => fetchPrompts());
const runs = createAsync(() => fetchPromptRuns({ limit: RUN_LIMIT }));
const [params, setParams] = useSearchParams();
const [editing, setEditing] = createSignal<DashboardPrompt | null>(null);
const [deleting, setDeleting] = createSignal<DashboardPrompt | null>(null);
const [editing, setEditing] = createSignal<Prompt | null>(null);
const [deleting, setDeleting] = createSignal<Prompt | null>(null);

const status = () => parseRunStatusFilter(typeof params.status === "string" ? params.status : undefined);
const query = () => (typeof params.q === "string" ? params.q : "");
Expand Down
25 changes: 2 additions & 23 deletions app/src/routes/ai/prompts/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,6 @@ import {
updatePrompt,
} from "~/services/prompts/prompts.actions";

type PromptVersion = {
id: string;
template: string;
system?: string | null;
modelOverride?: string | null;
tempOverride?: number | null;
topPOverride?: number | null;
createdAt?: string;
};

type PromptFull = {
id: string;
task: string;
description?: string | null;
defaultModel: string;
defaultTemp: number;
defaultTopP?: number | null;
activeVersion?: PromptVersion | null;
versions: PromptVersion[];
};

const formatOverrides = (version: {
modelOverride?: string | null;
tempOverride?: number | null;
Expand Down Expand Up @@ -85,8 +64,8 @@ export const PromptDetailPage: VoidComponent = () => {
const params = useParams();
const navigate = useNavigate();
const prompt = createAsync(() => {
if (!params.id) return Promise.resolve<PromptFull | null>(null);
return fetchPrompt(params.id) as Promise<PromptFull | null>;
if (!params.id) return Promise.resolve(null);
return fetchPrompt(params.id);
});
const runs = createAsync(() => {
if (!params.id) return Promise.resolve([] as PromptRunItem[]);
Expand Down
Loading
Loading