d25d482dc2
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
183 lines
5.6 KiB
TypeScript
183 lines
5.6 KiB
TypeScript
import { db } from '@sim/db'
|
|
import { workspaceFileFolder, workspaceFiles } from '@sim/db/schema'
|
|
import { and, eq, inArray, isNull } from 'drizzle-orm'
|
|
import {
|
|
WORKFLOW_CHANGELOG_BACKING_FOLDER,
|
|
WORKFLOW_PLANS_BACKING_FOLDER,
|
|
WORKSPACE_PLANS_BACKING_FOLDER,
|
|
} from '@/lib/copilot/vfs/workflow-aliases'
|
|
import {
|
|
ensureWorkspaceFileFolderPath,
|
|
listWorkspaceFileFolders,
|
|
} from '@/lib/uploads/contexts/workspace/workspace-file-folder-manager'
|
|
import {
|
|
getWorkspaceFileByName,
|
|
listWorkspaceFiles,
|
|
uploadWorkspaceFile,
|
|
type WorkspaceFileRecord,
|
|
} from '@/lib/uploads/contexts/workspace/workspace-file-manager'
|
|
|
|
export interface WorkflowAliasBacking {
|
|
changelogFolderId: string
|
|
plansRootFolderId: string
|
|
workflowPlansFolderId: string
|
|
workspacePlansFolderId: string
|
|
changelogFile: WorkspaceFileRecord | null
|
|
}
|
|
|
|
function initialChangelogContent(workflowName?: string): string {
|
|
const title = workflowName?.trim() || 'Workflow'
|
|
return `# ${title} Changelog\n`
|
|
}
|
|
|
|
export async function ensureWorkflowAliasBacking(args: {
|
|
workspaceId: string
|
|
userId: string
|
|
workflowId: string
|
|
workflowName?: string
|
|
}): Promise<WorkflowAliasBacking> {
|
|
const changelogFolderId = await ensureWorkspaceFileFolderPath({
|
|
workspaceId: args.workspaceId,
|
|
userId: args.userId,
|
|
pathSegments: [WORKFLOW_CHANGELOG_BACKING_FOLDER],
|
|
})
|
|
const plansRootFolderId = await ensureWorkspaceFileFolderPath({
|
|
workspaceId: args.workspaceId,
|
|
userId: args.userId,
|
|
pathSegments: [WORKFLOW_PLANS_BACKING_FOLDER],
|
|
})
|
|
const workflowPlansFolderId = await ensureWorkspaceFileFolderPath({
|
|
workspaceId: args.workspaceId,
|
|
userId: args.userId,
|
|
pathSegments: [WORKFLOW_PLANS_BACKING_FOLDER, args.workflowId],
|
|
})
|
|
const workspacePlansFolderId = await ensureWorkspaceFileFolderPath({
|
|
workspaceId: args.workspaceId,
|
|
userId: args.userId,
|
|
pathSegments: [WORKFLOW_PLANS_BACKING_FOLDER, WORKSPACE_PLANS_BACKING_FOLDER],
|
|
})
|
|
|
|
if (
|
|
!changelogFolderId ||
|
|
!plansRootFolderId ||
|
|
!workflowPlansFolderId ||
|
|
!workspacePlansFolderId
|
|
) {
|
|
throw new Error('Failed to provision workflow alias backing folders')
|
|
}
|
|
|
|
const changelogName = `${args.workflowId}.md`
|
|
let changelogFile = await getWorkspaceFileByName(args.workspaceId, changelogName, {
|
|
folderId: changelogFolderId,
|
|
})
|
|
if (!changelogFile) {
|
|
await uploadWorkspaceFile(
|
|
args.workspaceId,
|
|
args.userId,
|
|
Buffer.from(initialChangelogContent(args.workflowName), 'utf-8'),
|
|
changelogName,
|
|
'text/markdown',
|
|
{ folderId: changelogFolderId }
|
|
)
|
|
changelogFile = await getWorkspaceFileByName(args.workspaceId, changelogName, {
|
|
folderId: changelogFolderId,
|
|
})
|
|
}
|
|
|
|
return {
|
|
changelogFolderId,
|
|
plansRootFolderId,
|
|
workflowPlansFolderId,
|
|
workspacePlansFolderId,
|
|
changelogFile,
|
|
}
|
|
}
|
|
|
|
export async function ensureWorkspacePlanBacking(args: {
|
|
workspaceId: string
|
|
userId: string
|
|
}): Promise<{ plansRootFolderId: string; workspacePlansFolderId: string }> {
|
|
const plansRootFolderId = await ensureWorkspaceFileFolderPath({
|
|
workspaceId: args.workspaceId,
|
|
userId: args.userId,
|
|
pathSegments: [WORKFLOW_PLANS_BACKING_FOLDER],
|
|
})
|
|
const workspacePlansFolderId = await ensureWorkspaceFileFolderPath({
|
|
workspaceId: args.workspaceId,
|
|
userId: args.userId,
|
|
pathSegments: [WORKFLOW_PLANS_BACKING_FOLDER, WORKSPACE_PLANS_BACKING_FOLDER],
|
|
})
|
|
if (!plansRootFolderId || !workspacePlansFolderId) {
|
|
throw new Error('Failed to provision workspace plan backing folders')
|
|
}
|
|
return { plansRootFolderId, workspacePlansFolderId }
|
|
}
|
|
|
|
export async function cleanupWorkflowAliasBacking(args: {
|
|
workspaceId: string
|
|
workflowId: string
|
|
deletedAt?: Date
|
|
}): Promise<{ files: number; folders: number }> {
|
|
const deletedAt = args.deletedAt ?? new Date()
|
|
const folders = await listWorkspaceFileFolders(args.workspaceId, {
|
|
scope: 'all',
|
|
includeReservedSystemFolders: true,
|
|
})
|
|
const files = await listWorkspaceFiles(args.workspaceId, {
|
|
scope: 'all',
|
|
folders,
|
|
includeReservedSystemFiles: true,
|
|
})
|
|
|
|
const ownedFileIds = files
|
|
.filter((file) => {
|
|
if (file.deletedAt) return false
|
|
const changelogMatch =
|
|
file.folderPath === WORKFLOW_CHANGELOG_BACKING_FOLDER &&
|
|
file.name === `${args.workflowId}.md`
|
|
const workflowPlanMatch =
|
|
file.folderPath === `${WORKFLOW_PLANS_BACKING_FOLDER}/${args.workflowId}` ||
|
|
Boolean(file.folderPath?.startsWith(`${WORKFLOW_PLANS_BACKING_FOLDER}/${args.workflowId}/`))
|
|
return changelogMatch || workflowPlanMatch
|
|
})
|
|
.map((file) => file.id)
|
|
|
|
const ownedFolderIds = folders
|
|
.filter((folder) => {
|
|
if (folder.deletedAt) return false
|
|
return (
|
|
folder.path === `${WORKFLOW_PLANS_BACKING_FOLDER}/${args.workflowId}` ||
|
|
folder.path.startsWith(`${WORKFLOW_PLANS_BACKING_FOLDER}/${args.workflowId}/`)
|
|
)
|
|
})
|
|
.map((folder) => folder.id)
|
|
|
|
if (ownedFileIds.length > 0) {
|
|
await db
|
|
.update(workspaceFiles)
|
|
.set({ deletedAt })
|
|
.where(
|
|
and(
|
|
eq(workspaceFiles.workspaceId, args.workspaceId),
|
|
inArray(workspaceFiles.id, ownedFileIds),
|
|
isNull(workspaceFiles.deletedAt)
|
|
)
|
|
)
|
|
}
|
|
|
|
if (ownedFolderIds.length > 0) {
|
|
await db
|
|
.update(workspaceFileFolder)
|
|
.set({ deletedAt })
|
|
.where(
|
|
and(
|
|
eq(workspaceFileFolder.workspaceId, args.workspaceId),
|
|
inArray(workspaceFileFolder.id, ownedFolderIds),
|
|
isNull(workspaceFileFolder.deletedAt)
|
|
)
|
|
)
|
|
}
|
|
|
|
return { files: ownedFileIds.length, folders: ownedFolderIds.length }
|
|
}
|