@agentled/cli
Version:
CLI for Agentled — manage workflows, apps, and knowledge from the command line. Zero context-window cost for AI agents.
72 lines • 2.91 kB
JavaScript
function isRecord(value) {
return !!value && typeof value === 'object' && !Array.isArray(value);
}
function firstString(...values) {
for (const value of values) {
if (typeof value !== 'string')
continue;
const trimmed = value.trim();
if (trimmed)
return trimmed;
}
return undefined;
}
export function slugifyWorkspaceRef(value) {
const slug = value
.trim()
.toLowerCase()
.replace(/^ws_/, '')
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-+|-+$/g, '');
return slug || 'workspace';
}
function isWorkspaceIdRef(value, workspaceId) {
if (!value)
return false;
return value.trim() === workspaceId.trim()
|| slugifyWorkspaceRef(value) === slugifyWorkspaceRef(workspaceId);
}
function firstNonWorkspaceIdString(workspaceId, ...values) {
for (const value of values) {
const candidate = firstString(value);
if (!candidate || isWorkspaceIdRef(candidate, workspaceId))
continue;
return candidate;
}
return undefined;
}
/**
* The external workspace endpoint returns
* `{ workspace: { id, name }, company, team, knowledgeLists }`, while
* older/internal callers may still hand us a top-level workspace object.
* Normalize both shapes and fall back to the active auth profile. Workspace ids
* are valid identities, but never valid local folder slugs: if no slug, alias, or
* name can be resolved, fail instead of creating an `agentled_<workspaceId>/`
* folder.
*/
export function resolveWorkspaceMeta(input) {
const root = isRecord(input.response) ? input.response : {};
const responseWorkspace = isRecord(root.workspace) ? root.workspace : root;
const company = isRecord(root.company) ? root.company : {};
const auth = input.authWorkspace ?? {};
const workspaceId = firstString(responseWorkspace.id, responseWorkspace.workspaceId, root.workspaceId, auth.id);
if (!workspaceId) {
throw new Error('Authenticated, but no workspace id was found in the workspace response or active auth profile.');
}
const name = firstString(responseWorkspace.name, root.name, company.name, auth.name);
if (!name) {
throw new Error('Authenticated, but no workspace name or slug was found. Refusing to create an agentled_<workspaceId> folder.');
}
const slugSource = firstNonWorkspaceIdString(workspaceId, input.slugOverride, responseWorkspace.slug, root.slug, auth.alias, name);
if (!slugSource) {
throw new Error('Authenticated, but no workspace slug, alias, or name was found. Refusing to create an agentled_<workspaceId> folder.');
}
return {
workspaceId,
slug: slugifyWorkspaceRef(slugSource),
name,
apiBase: input.apiBase,
syncedAt: input.syncedAt ?? new Date().toISOString(),
};
}
//# sourceMappingURL=workspace-meta.js.map