/**
* Every link the factory hands out (card links, report links, health links,
* agent briefs, PR references) starts from one base URL: the
* `publicBaseUrl` setting when set (for example `https://factory.kody.build`),
* else the app's hosted kody.run URL. Card links use the short form
* `/c/`; `/cards/` keeps working.
*/
import { packageContext } from 'kody:runtime'
import { loadSettings, normalizeBaseUrl } from './settings.ts'
export const DEFAULT_HOSTED_URL = 'https://kody-factory--kody.kody.run'
/** The hosted kody.run URL of this app, no trailing slash. */
export function hostedBaseUrl(): string {
return normalizeBaseUrl(packageContext?.hostedUrl) || DEFAULT_HOSTED_URL
}
/** Base for links: `publicBaseUrl` when set, else the hosted URL. */
export async function appBaseUrl(): Promise {
try {
return (await loadSettings()).publicBaseUrl || hostedBaseUrl()
} catch {
return hostedBaseUrl()
}
}
/** Short, stable card link: `/c/`. */
export function cardLink(base: string, cardId: string): string {
return `${base.replace(/\/+$/, '')}/c/${encodeURIComponent(cardId)}`
}
export function reportLink(base: string, reportId: string): string {
return `${base.replace(/\/+$/, '')}/reports/${encodeURIComponent(reportId)}`
}