akm-cli
Version:
akm (Agent Knowledge Manager) — a portable, local-first capability library for AI agents. Discover, load, share, and improve reusable skills, scripts, workflows, and knowledge across any shell-capable coding agent, including Claude Code, OpenCode, and Cur
48 lines (47 loc) • 2.19 kB
JavaScript
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
/**
* How akm reads a connection's `endpoint`: the OpenAI-compatible base URL
* (`…/v1`) or the chat-completions URL itself. The endpoint is a URL, so only
* its pathname is edited here; a query string (an `api-version`, say) stays
* where it is, after the pathname, and a fragment, which is never sent, goes.
* Every consumer of an endpoint (the chat client, model listing, health probes,
* setup, the stored-key binding, akm's own runtime) reads it through this
* one module.
*/
/** The operation a complete endpoint URL may end with, in any case. */
const OPERATION_SUFFIX = /\/(?:chat\/completions|completions|embeddings|models)$/i;
function parse(endpoint) {
try {
return new URL(endpoint);
}
catch {
return undefined;
}
}
/** The pathname of the endpoint's base: no trailing slash, no operation (`/chat/completions`, `/completions`, `/embeddings`, `/models`). */
function basePathname(url) {
return url.pathname.replace(/\/+$/, "").replace(OPERATION_SUFFIX, "");
}
function withPathname(url, pathname) {
url.hash = "";
url.pathname = pathname;
const text = url.toString();
// A bare host serializes with a "/" the endpoint never had.
return pathname === "" && url.search === "" ? text.replace(/\/$/, "") : text;
}
/** The base of an OpenAI-compatible endpoint: the URL without an operation suffix or a trailing slash. */
export function openAiBaseUrl(endpoint) {
const url = parse(endpoint);
return url ? withPathname(url, basePathname(url)) : endpoint;
}
/** The URL of one operation (`chat/completions`, `models`) of the endpoint. */
export function openAiOperationUrl(endpoint, operation) {
const url = parse(endpoint);
return url ? withPathname(url, `${basePathname(url)}/${operation}`) : `${endpoint}/${operation}`;
}
/** Whether two endpoints are the same server: the same base, however either is spelled. */
export function sameEndpoint(a, b) {
return openAiBaseUrl(a) === openAiBaseUrl(b);
}