import { createWorkspace, listWorkspaces } from "./anythingllmClient"; import type { AnythingWorkspace } from "./types"; export interface EnsuredWorkspace { readonly workspace: AnythingWorkspace; readonly created: boolean; } export const ensureWorkspaceForRepoName = async ( baseUrl: string, apiKey: string, repoName: string, ): Promise => { const key = repoName.trim(); if (key.length === 0) { throw new Error("repo/workspace name is empty"); } const all = await listWorkspaces(baseUrl, apiKey); const byName = all.find((w) => w.name === key); const bySlug = all.find((w) => w.slug === key); const found = byName ?? bySlug; if (found) { return { workspace: found, created: false }; } const created = await createWorkspace(baseUrl, apiKey, key); return { workspace: created, created: true }; };