/** Node-free constants and types shared by the Host and browser halves. */ /** Plugin-owned status endpoint consumed by its browser half. */ export const CODEBUDDY_STATUS_PATH = '/plugins/dsh-codebuddy-cli/status' /** * Plugin-owned write endpoint for the enabled-model selection. * * The card writes its selection through this route rather than the host's * generic settings form: the choice is a set of checkboxes over the live * catalog, which a schema-rendered string-array field cannot express. The * handler applies the same loopback gate as the status route and additionally * requires a loopback `Origin`, because unlike the GET it mutates state. */ export const CODEBUDDY_MODELS_PATH = '/plugins/dsh-codebuddy-cli/enabled-models' /** * Plugin-owned write endpoint for the composer dock's foreign-provider switch. * * The card toggles it from its own settings surface rather than the generated * form, so the change takes effect on the next status read instead of waiting * for a host restart. Same loopback/Origin/JSON gates as the enabled-model * write route, because it mutates the settings document too. */ export const CODEBUDDY_DOCK_PREFERENCE_PATH = '/plugins/dsh-codebuddy-cli/dock-preference' /** * The entry switches route: GET reads, POST writes. * * Declared next to the other route paths so every address this plugin serves is * in one place; the shape it carries is `CodeBuddyEntrySwitches` below. */ export const CODEBUDDY_ENTRY_SWITCHES_PATH = '/plugins/dsh-codebuddy-cli/entry-switches' /** * The three built-in entry points, each independently switchable. * * Every field defaults to `true`: an untouched install shows all three, which is * what it did before these switches existed. `false` means "do not register", * because leaving a registration in place only to hide it still costs the host a * slot entry (the sidebar row would occupy a line in the host's own list). */ export interface CodeBuddyEntrySwitches { /** * Always `true`, and not switchable. * * This row is the recovery path: with it off *and* the sidebar row off, no * surface would be left that could turn either back on, so the plugin would be * unreachable short of a reinstall. The host therefore reports it as on and * rejects an attempt to set it false; the field stays in the payload so the * client reads one stable shape. */ settingsSection: boolean /** The main sidebar row and the `main` panel behind it. */ sidebarPanel: boolean /** The configuration block on the Plugins page (bundle + row views). */ pluginsConfig: boolean } /** All three on — what an install without a stored preference gets. */ export const DEFAULT_ENTRY_SWITCHES: CodeBuddyEntrySwitches = { settingsSection: true, sidebarPanel: true, pluginsConfig: true, } /** * One piece of the composer credit line. * * Declared here rather than imported from `index.ts` because this module is the * wire contract the browser half reads: it must stay free of host-side imports * (the client bundle would otherwise pull the whole provider in). `index.ts` * derives its schema from this list, so the two cannot drift. */ export const CODEBUDDY_DOCK_SEGMENTS = ['credit', 'provider', 'model', 'rate'] as const export type CodeBuddyDockSegment = (typeof CODEBUDDY_DOCK_SEGMENTS)[number] /** * Plugin-owned daily check-in endpoint. * * The card's check-in button POSTs here; the host half forwards the request * to the CodeBuddy CN daily check-in upstream with the plugin-resolved * credential. Same loopback/Origin gates as the enabled-model write route. */ export const CODEBUDDY_CHECKIN_PATH = '/plugins/dsh-codebuddy-cli/check-in' /** * Plugin-owned account management endpoints. * * The card's account panel uses these routes to start an OAuth login, poll for * completion, switch the active account, and remove an account. All routes * apply the same loopback/Origin gates as the other write routes. */ export const CODEBUDDY_LOGIN_START_PATH = '/plugins/dsh-codebuddy-cli/login/start' export const CODEBUDDY_LOGIN_POLL_PATH = '/plugins/dsh-codebuddy-cli/login/poll' export const CODEBUDDY_SWITCH_ACCOUNT_PATH = '/plugins/dsh-codebuddy-cli/accounts/switch' export const CODEBUDDY_DELETE_ACCOUNT_PATH = '/plugins/dsh-codebuddy-cli/accounts/delete' /** Optional per-account targeting on the check-in write. */ export interface CodeBuddyCheckInRequest { /** Stable account id; absent targets the resolved (active) account. */ id?: string } /** Daily check-in outcome status, already mapped for display. */ export type CodeBuddyCheckInStatus = 'ok' | 'already' | 'failed' /** * The daily check-in answer the card renders. `message` is upstream text * (success confirmation, the "already" reason, or a failure detail) and is * shown verbatim next to the localized status. */ export interface CodeBuddyCheckInOutcome { status: CodeBuddyCheckInStatus message: string } /** * The provider id this plugin registers in the Harness LLM seam. * * Shared with the browser half so the composer dock can match the session's * `modelSelection` projection against this provider before reading a rate; * the host-side spelling lives in `adapter.ts` (`CODEBUDDY_PROVIDER`) and a * test asserts the two stay in sync. */ export const CODEBUDDY_PROVIDER_ID = 'codebuddy-cli' /** One billing package and its remaining credit, as the card renders it. */ export interface CodeBuddyWebCreditAccount { /** Package code as the upstream reports it (may be absent). */ packageCode?: string packageName: string /** Total capacity of this package. */ total: number /** Remaining capacity of this package. */ remain: number /** Used capacity of this package. */ used: number /** Backward-compatible alias for the total. */ size: number /** Expiry as epoch milliseconds; absent means long-lived. */ expireAtMs?: number /** Whether the package has already expired. */ expired: boolean /** Whether the package expires within the soon window (7 days). */ expiringSoon: boolean } /** Aggregated credit answer rendered by the plugin card. */ export interface CodeBuddyWebCredits { total: number accounts: readonly CodeBuddyWebCreditAccount[] } /** Billing convenience facts for one model, rendered as card badges. */ export interface CodeBuddyWebModelBadge { id: string name: string /** Whether the model is currently free (`x0.00` credits). */ free?: boolean /** Promotional badges, e.g. `限时免费`, `夜间折扣`. */ badges?: readonly string[] /** * Credits multiplier in display form, e.g. `x0.79`. Unlike the model * picker's copy, the card renders through the browser locale, so this value * may be interpolated into a localized sentence rather than shown bare. */ credits?: string } /** * Whole-catalog rate and display-name maps, keyed by model id. * * The composer dock needs the multiplier of the *currently selected* model — * any model in the catalog, not only the promo rows the card's `models` list * carries. Shipping every model's rate as a compact map (instead of only the * promo subset) lets the dock resolve any selection without enlarging the * per-model badge shape; the map stays small (≈15 ids), so the document's * size is unaffected. */ export interface CodeBuddyWebRateMap { /** Normalized multiplier per model id, e.g. `{ 'glm-5.3': 'x0.79' }`. */ readonly rates: Readonly> /** Display name per model id, so the dock can echo the selection's name. */ readonly names: Readonly> } /** * One selectable model as the card's checkbox list renders it. * * This is the *whole* catalog, unlike {@link CodeBuddyWebModelBadge}'s promo * subset: the user has to be able to check a model that carries no discount. * `enabled` is the effective answer the Host computed (an absent or empty * selection resolves to every model enabled), so the card never re-derives * that rule. */ export interface CodeBuddyWebModelChoice { id: string name: string /** Whether this model is currently offered in the model pickers. */ enabled: boolean /** Whether the model is currently free (`x0.00` credits). */ free?: boolean /** Promotional badges, e.g. `限时免费`, `夜间折扣`. */ badges?: readonly string[] /** Normalized credits multiplier in display form, e.g. `x0.79`. */ credits?: string } /** The enabled-model selection as the card reads and writes it. */ export interface CodeBuddyWebModelSelection { /** Every served model with its effective offered state, in catalog order. */ readonly choices: readonly CodeBuddyWebModelChoice[] /** * Whether the stored selection restricts anything. False means the section * carries no selection (or an empty one), so every model is offered and the * card can say so rather than showing 15 checked boxes as a deliberate choice. */ readonly restricted: boolean /** * Whether the Host can persist a selection. A profile with no settings * provider serves models but stores nothing, so the card disables its * controls instead of failing the write. */ readonly writable: boolean } /** Request body of the enabled-model write route. */ export interface CodeBuddyEnabledModelsRequest { /** Model ids to offer. An empty array clears the restriction. */ readonly enabledModels: readonly string[] } /** One stored account as the card's account panel renders it. */ export interface CodeBuddyWebAccount { /** Stable client-side id. */ id: string /** Upstream user id. */ uid: string nickname?: string domain: string enterpriseId?: string /** Access token expiry, epoch milliseconds. */ expiresAtMs: number /** Whether this is the currently active account. */ active: boolean /** Full credit resources for this account (rendered as package bars). */ credits?: CodeBuddyWebCredits /** Whether the account checked in today (undefined = unknown). */ checkedInToday?: boolean /** When the credit answer was last fetched (epoch ms). */ creditUpdatedAtMs?: number /** Whether a credit fetch failed for this account (message retained). */ creditError?: string } /** The login start response the card renders. */ export interface CodeBuddyLoginStartResult { /** Whether the login URL was generated successfully. */ ok: boolean /** The URL the user should open in their browser. */ authUrl?: string /** Internal state id used for polling. */ state?: string /** Error message when ok is false. */ error?: string } /** The login poll response the card renders. */ export interface CodeBuddyLoginPollResult { /** Whether the login has completed (success or failure). */ done: boolean /** The new account summary when login succeeded. */ account?: CodeBuddyWebAccount /** Error message when login failed. */ error?: string } /** Plugin-owned credit-statistics endpoint (aggregates usage across accounts). */ export const CODEBUDDY_CREDIT_STATS_PATH = '/plugins/dsh-codebuddy-cli/credit-stats' /** * Import-account endpoint: adds an identity from a pasted refresh/access token * pair instead of the browser sign-in flow. * * `refreshToken` is the primary input — it is the long-lived credential and the * Host exchanges it for a fresh access token before storing anything, so the * stored record is always current. `accessToken` is accepted but optional: the * upstream refresh endpoint authenticates on `X-Refresh-Token` alone, so a * pasted access token only saves one round trip. */ export const CODEBUDDY_IMPORT_ACCOUNT_PATH = '/plugins/dsh-codebuddy-cli/accounts/import' /** Request body of the import-account route. */ export interface CodeBuddyImportAccountRequest { /** * Long-lived credential; the Host refreshes it to obtain the access token it * stores. Required — without it the account could never be renewed. */ readonly refreshToken: string /** * Optional current access token. Accepted for convenience (a user copying * both values pastes both), but never required and never trusted for * identity: the refresh answer supersedes it. */ readonly accessToken?: string /** * Login domain, e.g. `www.codebuddy.cn` or `workbuddy.ai`. Selects which * upstream the account talks to. When omitted the Host derives it from the * refresh answer, or falls back to the CN region. */ readonly domain?: string /** * Identity hints for the rare case where neither the token pair nor the * refresh answer supplies them. Both are display/attribution metadata. */ readonly nickname?: string readonly enterpriseId?: string } /** * Import-account answer. * * A failed import is reported with `ok: false` and a message, and — unlike the * login poll — nothing is written: an unverified token pair must never reach * the account store. */ export interface CodeBuddyImportAccountResult { readonly ok: boolean /** The stored account when the import succeeded. */ readonly account?: CodeBuddyWebAccount /** * Credit ledger read during the import probe, so the new card renders its * balance immediately instead of staying empty until the next status poll. * Absent when the credits call failed — the import still succeeds. */ readonly credits?: CodeBuddyWebCredits /** Human-readable failure reason when `ok` is false. */ readonly error?: string } /** One per-request usage row, as the statistics panel renders it. */ export interface CodeBuddyWebUsageRow { requestId: string model: string client: string credit: number requestTime: string date: string /** Owning account id (filled on per-account aggregation). */ accountId?: string /** Owning account display name. */ accountName?: string } /** Per-model aggregation for one day or the whole window. */ export interface CodeBuddyWebUsageModel { model: string requestCount: number credit: number } /** Daily usage point for the trend chart. */ export interface CodeBuddyWebUsageDaily { date: string usage: number models?: readonly CodeBuddyWebUsageModel[] } /** Per-account usage summary. */ export interface CodeBuddyWebUsageAccount { accountId: string accountName: string ok: boolean usageToday: number | null usage7Days: number | null usageThisMonth: number | null error?: string /** This account's own daily series, for per-account trend filtering. */ daily?: readonly CodeBuddyWebUsageDaily[] /** This account's own per-model aggregation. */ models?: readonly CodeBuddyWebUsageModel[] } /** Aggregated credit statistics document for the panel. */ export interface CodeBuddyCreditStats { status: 'complete' | 'partial' | 'unavailable' rangeStart: string rangeEnd: string collectedAt: number summary: { usageToday: number usage7Days: number usageThisMonth: number } daily: readonly CodeBuddyWebUsageDaily[] models: readonly CodeBuddyWebUsageModel[] requests: readonly CodeBuddyWebUsageRow[] accounts: readonly CodeBuddyWebUsageAccount[] detailLimit: number } /** The JSON document the plugin card renders. */ export type CodeBuddyWebStatus = | { status: 'signed-out' /** Stored accounts for the account management panel (may be empty). */ accounts?: readonly CodeBuddyWebAccount[] /** * Whether the composer dock row renders on a foreign provider. Carried on * every document kind because the dock reads it while signed out too. */ showDockOnOtherProviders?: boolean /** Whether the Host can persist `showDockOnOtherProviders`. */ showDockOnOtherProvidersWritable?: boolean /** * Which pieces the composer line renders. Absent on a Host that predates * the option; the client then uses the same default the schema declares. */ dockSegments?: readonly CodeBuddyDockSegment[] /** Whether the Host can persist `dockSegments`. */ dockSegmentsWritable?: boolean } | { status: 'signed-in' nickname?: string domain?: string source?: 'cli' | 'dsh' expiresAt?: number credits?: CodeBuddyWebCredits creditsError?: string /** Billing convenience facts for the models the plugin serves. */ models?: readonly CodeBuddyWebModelBadge[] /** Whole-catalog rate/name maps for the composer dock's current-model read. */ catalog?: CodeBuddyWebRateMap /** Every served model with its offered state, backing the card's checkbox list. */ selection?: CodeBuddyWebModelSelection /** Stored accounts for the account management panel. */ accounts?: readonly CodeBuddyWebAccount[] /** See the signed-out variant: the dock's foreign-provider preference. */ showDockOnOtherProviders?: boolean /** See the signed-out variant: whether that preference can be saved. */ showDockOnOtherProvidersWritable?: boolean /** See the signed-out variant: the composer line's segment choice. */ dockSegments?: readonly CodeBuddyDockSegment[] /** See the signed-out variant: whether the segment choice can be saved. */ dockSegmentsWritable?: boolean } | { status: 'error'; message: string }