99 lines
2.3 KiB
TypeScript
99 lines
2.3 KiB
TypeScript
import { AssistantUnrolled, ModelConfig } from "@continuedev/config-yaml";
|
|
import {
|
|
BaseLlmApi,
|
|
constructLlmApi,
|
|
LLMConfig,
|
|
} from "@continuedev/openai-adapters";
|
|
import {
|
|
Configuration,
|
|
DefaultApi,
|
|
} from "@continuedev/sdk/dist/api/dist/index.js";
|
|
|
|
import { AuthConfig } from "./auth/workos.js";
|
|
import { env } from "./env.js";
|
|
import { getUniqueId } from "./util/uniqueId.js";
|
|
import { getVersion } from "./version.js";
|
|
|
|
/**
|
|
* Creates user-agent header value for CLI requests
|
|
*/
|
|
function getUserAgent(): string {
|
|
const version = getVersion();
|
|
return `Continue-CLI/${version}`;
|
|
}
|
|
|
|
/**
|
|
* Merges user-agent header into request options
|
|
*/
|
|
function _mergeUserAgentIntoRequestOptions(
|
|
requestOptions: ModelConfig["requestOptions"],
|
|
): ModelConfig["requestOptions"] {
|
|
return {
|
|
...requestOptions,
|
|
headers: {
|
|
...requestOptions?.headers,
|
|
"user-agent": getUserAgent(),
|
|
"x-continue-unique-id": getUniqueId(),
|
|
},
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Creates an LLM API instance from a ModelConfig and auth configuration
|
|
*/
|
|
export function createLlmApi(
|
|
model: ModelConfig,
|
|
_authConfig: AuthConfig,
|
|
): BaseLlmApi | null {
|
|
const config: LLMConfig = {
|
|
provider: model.provider as any,
|
|
model: model.model,
|
|
apiKey: model.apiKey,
|
|
apiBase: model.apiBase,
|
|
requestOptions: model.requestOptions,
|
|
env: model.env,
|
|
};
|
|
|
|
return constructLlmApi(config) ?? null;
|
|
}
|
|
|
|
export function getLlmApi(
|
|
assistant: AssistantUnrolled,
|
|
authConfig: AuthConfig,
|
|
): [BaseLlmApi, ModelConfig] {
|
|
if (!assistant.models || assistant.models.length === 0) {
|
|
throw new Error("No models found in the configured assistant");
|
|
}
|
|
|
|
const model = assistant.models?.find(
|
|
(model) =>
|
|
model?.roles?.includes("chat") || (model && model.roles === undefined),
|
|
);
|
|
|
|
if (!model) {
|
|
throw new Error(
|
|
"No models with the chat role found in the configured assistant",
|
|
);
|
|
}
|
|
|
|
const llmApi = createLlmApi(model, authConfig);
|
|
|
|
if (!llmApi) {
|
|
throw new Error(
|
|
"Failed to initialize LLM. Please check your configuration.",
|
|
);
|
|
}
|
|
|
|
return [llmApi, model];
|
|
}
|
|
|
|
export function getApiClient(
|
|
accessToken: string | undefined | null,
|
|
): DefaultApi {
|
|
return new DefaultApi(
|
|
new Configuration({
|
|
basePath: env.apiBase.replace(/\/$/, ""),
|
|
accessToken: accessToken ?? undefined,
|
|
}),
|
|
);
|
|
}
|