From 05d86030deab3066c018ae4cc37fa586ce2264ba Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Mon, 21 Jul 2025 17:20:39 +0200 Subject: [PATCH] feat: local block loading for hub assistants --- core/config/yaml/loadLocalYamlBlocks.ts | 62 ++++++++++++++ core/config/yaml/loadYaml.ts | 102 ++++++++++------------- core/config/yaml/yamlToContinueConfig.ts | 33 ++++++++ packages/config-yaml/src/load/merge.ts | 18 +++- 4 files changed, 158 insertions(+), 57 deletions(-) create mode 100644 core/config/yaml/loadLocalYamlBlocks.ts create mode 100644 core/config/yaml/yamlToContinueConfig.ts diff --git a/core/config/yaml/loadLocalYamlBlocks.ts b/core/config/yaml/loadLocalYamlBlocks.ts new file mode 100644 index 000000000..8c3cde9cb --- /dev/null +++ b/core/config/yaml/loadLocalYamlBlocks.ts @@ -0,0 +1,62 @@ +import { + AssistantUnrolled, + ConfigResult, + PackageIdentifier, + RegistryClient, + unrollAssistantFromContent, +} from "@continuedev/config-yaml"; +import { IDE } from "../.."; +import { ControlPlaneClient } from "../../control-plane/client"; +import { LocalPlatformClient } from "./LocalPlatformClient"; + +// This is a certain approach to unrolling local YAML where it +// 1. creates an assistant out of all local blocks +// 2. unrolls it like a local assistant +export async function unrollLocalYamlBlocks( + packageIdentifiers: PackageIdentifier[], + ide: IDE, + registryClient: RegistryClient, + orgScopeId: string | null, + controlPlaneClient: ControlPlaneClient, +): Promise> { + try { + const unrollResult = await unrollAssistantFromContent( + { + uriType: "file", + filePath: "", + }, + "name: FILLER\nschema: v1\nversion: 0.0.1", + registryClient, + { + currentUserSlug: "", + onPremProxyUrl: null, + orgScopeId, + platformClient: new LocalPlatformClient( + orgScopeId, + controlPlaneClient, + ide, + ), + renderSecrets: true, + injectBlocks: packageIdentifiers, + }, + ); + const config = + "config" in unrollResult ? unrollResult.config : unrollResult; + const errors = "errors" in unrollResult ? unrollResult.errors : []; + return { + config, + errors, + configLoadInterrupted: false, + }; + } catch (error) { + let message = "An unknown error occurred while loading local YAML blocks"; + if (error instanceof Error) { + message += ": " + error.message; + } + return { + config: undefined, + errors: [{ message, fatal: false }], + configLoadInterrupted: false, + }; + } +} diff --git a/core/config/yaml/loadYaml.ts b/core/config/yaml/loadYaml.ts index 04c19b326..ef745c02e 100644 --- a/core/config/yaml/loadYaml.ts +++ b/core/config/yaml/loadYaml.ts @@ -4,11 +4,10 @@ import { ConfigResult, ConfigValidationError, isAssistantUnrolledNonNullable, - MCPServer, + mergeUnrolledAssistants, ModelRole, PackageIdentifier, RegistryClient, - Rule, TEMPLATE_VAR_REGEX, unrollAssistant, validateConfigYaml, @@ -17,13 +16,11 @@ import { dirname } from "node:path"; import { ContinueConfig, - ExperimentalMCPOptions, IContextProvider, IDE, IdeInfo, IdeSettings, ILLMLogger, - RuleWithSource, } from "../.."; import { MCPManagerSingleton } from "../../context/mcp/MCPManagerSingleton"; import DocsContextProvider from "../../context/providers/DocsContextProvider"; @@ -41,39 +38,13 @@ import { getControlPlaneEnvSync } from "../../control-plane/env"; import { getToolsForIde } from "../../tools"; import { getCleanUriPath } from "../../util/uri"; import { getAllDotContinueDefinitionFiles } from "../loadLocalAssistants"; +import { unrollLocalYamlBlocks } from "./loadLocalYamlBlocks"; import { LocalPlatformClient } from "./LocalPlatformClient"; import { llmsFromModelConfig } from "./models"; - -function convertYamlRuleToContinueRule(rule: Rule): RuleWithSource { - if (typeof rule === "string") { - return { - rule: rule, - source: "rules-block", - }; - } else { - return { - source: "rules-block", - rule: rule.rule, - globs: rule.globs, - name: rule.name, - }; - } -} - -function convertYamlMcpToContinueMcp( - server: MCPServer, -): ExperimentalMCPOptions { - return { - transport: { - type: "stdio", - command: server.command, - args: server.args ?? [], - env: server.env, - cwd: server.cwd, - } as any, // TODO: Fix the mcpServers types in config-yaml (discriminated union) - timeout: server.connectionTimeout, - }; -} +import { + convertYamlMcpToContinueMcp, + convertYamlRuleToContinueRule, +} from "./yamlToContinueConfig"; async function loadConfigYaml(options: { overrideConfigYaml: AssistantUnrolled | undefined; @@ -93,46 +64,65 @@ async function loadConfigYaml(options: { } = options; // Add local .continue blocks - const allLocalBlocks: PackageIdentifier[] = []; - for (const blockType of BLOCK_TYPES) { + const localBlockPromises = BLOCK_TYPES.map(async (blockType) => { const localBlocks = await getAllDotContinueDefinitionFiles( ide, { includeGlobal: true, includeWorkspace: true, fileExtType: "yaml" }, blockType, ); - allLocalBlocks.push( - ...localBlocks.map((b) => ({ - uriType: "file" as const, - filePath: b.path, - })), - ); - } - - const rootPath = - packageIdentifier.uriType === "file" - ? dirname(getCleanUriPath(packageIdentifier.filePath)) - : undefined; + return localBlocks.map((b) => ({ + uriType: "file" as const, + filePath: b.path, + })); + }); + const localPackageIdentifiers: PackageIdentifier[] = ( + await Promise.all(localBlockPromises) + ).flat(); // logger.info( // `Loading config.yaml from ${JSON.stringify(packageIdentifier)} with root path ${rootPath}`, // ); + // Registry client is only used if local blocks are present, but logic same for hub/local assistants + const getRegistryClient = async () => { + const rootPath = + packageIdentifier.uriType === "file" + ? dirname(getCleanUriPath(packageIdentifier.filePath)) + : undefined; + return new RegistryClient({ + accessToken: await controlPlaneClient.getAccessToken(), + apiBase: getControlPlaneEnvSync(ideSettings.continueTestEnvironment) + .CONTROL_PLANE_URL, + rootPath, + }); + }; + const errors: ConfigValidationError[] = []; let config: AssistantUnrolled | undefined; if (overrideConfigYaml) { config = overrideConfigYaml; + if (localPackageIdentifiers.length > 0) { + const unrolledLocal = await unrollLocalYamlBlocks( + localPackageIdentifiers, + ide, + await getRegistryClient(), + orgScopeId, + controlPlaneClient, + ); + if (unrolledLocal.errors) { + errors.push(...unrolledLocal.errors); + } + if (unrolledLocal.config) { + config = mergeUnrolledAssistants(config, unrolledLocal.config); + } + } } else { // This is how we allow use of blocks locally const unrollResult = await unrollAssistant( packageIdentifier, - new RegistryClient({ - accessToken: await controlPlaneClient.getAccessToken(), - apiBase: getControlPlaneEnvSync(ideSettings.continueTestEnvironment) - .CONTROL_PLANE_URL, - rootPath, - }), + await getRegistryClient(), { renderSecrets: true, currentUserSlug: "", @@ -143,7 +133,7 @@ async function loadConfigYaml(options: { controlPlaneClient, ide, ), - injectBlocks: allLocalBlocks, + injectBlocks: localPackageIdentifiers, }, ); config = unrollResult.config; diff --git a/core/config/yaml/yamlToContinueConfig.ts b/core/config/yaml/yamlToContinueConfig.ts new file mode 100644 index 000000000..f2f905787 --- /dev/null +++ b/core/config/yaml/yamlToContinueConfig.ts @@ -0,0 +1,33 @@ +import { MCPServer, Rule } from "@continuedev/config-yaml"; +import { ExperimentalMCPOptions, RuleWithSource } from "../.."; + +export function convertYamlRuleToContinueRule(rule: Rule): RuleWithSource { + if (typeof rule === "string") { + return { + rule: rule, + source: "rules-block", + }; + } else { + return { + source: "rules-block", + rule: rule.rule, + globs: rule.globs, + name: rule.name, + }; + } +} + +export function convertYamlMcpToContinueMcp( + server: MCPServer, +): ExperimentalMCPOptions { + return { + transport: { + type: "stdio", + command: server.command, + args: server.args ?? [], + env: server.env, + cwd: server.cwd, + } as any, // TODO: Fix the mcpServers types in config-yaml (discriminated union) + timeout: server.connectionTimeout, + }; +} diff --git a/packages/config-yaml/src/load/merge.ts b/packages/config-yaml/src/load/merge.ts index 7acd7db7e..eeea12ab8 100644 --- a/packages/config-yaml/src/load/merge.ts +++ b/packages/config-yaml/src/load/merge.ts @@ -1,4 +1,4 @@ -import { ConfigYaml } from "../schemas/index.js"; +import { AssistantUnrolled, ConfigYaml } from "../schemas/index.js"; export function mergePackages( current: ConfigYaml, @@ -15,3 +15,19 @@ export function mergePackages( docs: [...(current.docs ?? []), ...(incoming.docs ?? [])], }; } + +export function mergeUnrolledAssistants( + current: AssistantUnrolled, + incoming: AssistantUnrolled, +): AssistantUnrolled { + return { + ...current, + rules: [...(current.rules ?? []), ...(incoming.rules ?? [])], + models: [...(current.models ?? []), ...(incoming.models ?? [])], + docs: [...(current.docs ?? []), ...(incoming.docs ?? [])], + context: [...(current.context ?? []), ...(incoming.context ?? [])], + data: [...(current.data ?? []), ...(incoming.data ?? [])], + mcpServers: [...(current.mcpServers ?? []), ...(incoming.mcpServers ?? [])], + prompts: [...(current.prompts ?? []), ...(incoming.prompts ?? [])], + }; +}