95 lines
2.6 KiB
TypeScript
95 lines
2.6 KiB
TypeScript
|
|
import { and, eq, sql } from "../drizzle.js";
|
||
|
|
import { db } from "../db.js";
|
||
|
|
import { convoyEvents, objects } from "../schema.js";
|
||
|
|
import {
|
||
|
|
convoyObjectContentSchema,
|
||
|
|
type ConvoyObjectContent,
|
||
|
|
type ConvoyStatus,
|
||
|
|
} from "@tasks/shared/convoy-lifecycle";
|
||
|
|
|
||
|
|
export async function findConvoyBySlug(workspaceId: string, slug: string) {
|
||
|
|
const [row] = await db
|
||
|
|
.select()
|
||
|
|
.from(objects)
|
||
|
|
.where(
|
||
|
|
and(
|
||
|
|
eq(objects.workspaceId, workspaceId),
|
||
|
|
eq(objects.type, "project"),
|
||
|
|
sql`${objects.content} ->> 'convoySlug' = ${slug}`,
|
||
|
|
),
|
||
|
|
)
|
||
|
|
.limit(1);
|
||
|
|
return row ?? null;
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function loadConvoyProject(convoyId: string, workspaceId: string) {
|
||
|
|
const [row] = await db
|
||
|
|
.select()
|
||
|
|
.from(objects)
|
||
|
|
.where(
|
||
|
|
and(
|
||
|
|
eq(objects.id, convoyId),
|
||
|
|
eq(objects.workspaceId, workspaceId),
|
||
|
|
eq(objects.type, "project"),
|
||
|
|
),
|
||
|
|
)
|
||
|
|
.limit(1);
|
||
|
|
return row ?? null;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function parseConvoyContent(content: unknown): ConvoyObjectContent | null {
|
||
|
|
const parsed = convoyObjectContentSchema.safeParse(content);
|
||
|
|
return parsed.success ? parsed.data : null;
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function insertConvoyEvent(input: {
|
||
|
|
workspaceId: string;
|
||
|
|
convoyId: string;
|
||
|
|
briefId?: string | null;
|
||
|
|
role: string;
|
||
|
|
actor?: string | null;
|
||
|
|
eventType: string;
|
||
|
|
classification?: string | null;
|
||
|
|
skipFlags?: string[] | null;
|
||
|
|
durationS?: number | null;
|
||
|
|
outcome?: string | null;
|
||
|
|
repo?: string | null;
|
||
|
|
multitaskGroup?: string | null;
|
||
|
|
modelAlias?: string | null;
|
||
|
|
providerType?: string | null;
|
||
|
|
modelId?: string | null;
|
||
|
|
modelTier?: string | null;
|
||
|
|
estimatedCostUsd?: string | null;
|
||
|
|
metadata?: Record<string, unknown> | null;
|
||
|
|
}) {
|
||
|
|
const [row] = await db
|
||
|
|
.insert(convoyEvents)
|
||
|
|
.values({
|
||
|
|
workspaceId: input.workspaceId,
|
||
|
|
convoyId: input.convoyId,
|
||
|
|
briefId: input.briefId ?? null,
|
||
|
|
role: input.role,
|
||
|
|
actor: input.actor ?? null,
|
||
|
|
eventType: input.eventType,
|
||
|
|
classification: input.classification ?? null,
|
||
|
|
skipFlags: input.skipFlags ?? null,
|
||
|
|
durationS: input.durationS ?? null,
|
||
|
|
outcome: input.outcome ?? null,
|
||
|
|
repo: input.repo ?? null,
|
||
|
|
multitaskGroup: input.multitaskGroup ?? null,
|
||
|
|
modelAlias: input.modelAlias ?? null,
|
||
|
|
providerType: input.providerType ?? null,
|
||
|
|
modelId: input.modelId ?? null,
|
||
|
|
modelTier: input.modelTier ?? null,
|
||
|
|
estimatedCostUsd: input.estimatedCostUsd ?? null,
|
||
|
|
metadata: input.metadata ?? null,
|
||
|
|
})
|
||
|
|
.returning();
|
||
|
|
return row;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function currentConvoyStatus(row: { status: string | null }): ConvoyStatus {
|
||
|
|
const status = row.status ?? "draft";
|
||
|
|
return status as ConvoyStatus;
|
||
|
|
}
|