fix: move workflow stuff to config service

This commit is contained in:
Dallin Romney 2025-10-02 14:07:59 -07:00
parent 9e15bc0086
commit 4d3bafee60
3 changed files with 67 additions and 123 deletions

View file

@ -24,92 +24,73 @@ export class ConfigEnhancer {
*/
async enhanceConfig(
config: AssistantUnrolled,
options: BaseCommandOptions,
_options: BaseCommandOptions,
): Promise<AssistantUnrolled> {
let enhancedConfig = { ...config };
const workflowOptions = await this.addWorkflowOptions(options);
// Apply rules
if (workflowOptions.rule && workflowOptions.rule.length > 0) {
enhancedConfig = await this.injectRules(
enhancedConfig,
workflowOptions.rule,
);
}
// Apply MCPs
if (workflowOptions.mcp && workflowOptions.mcp.length > 0) {
enhancedConfig = await this.injectMcps(
enhancedConfig,
workflowOptions.mcp,
);
}
// Apply models
if (workflowOptions.model && workflowOptions.model.length > 0) {
enhancedConfig = await this.injectModels(
enhancedConfig,
workflowOptions.model,
);
}
// Apply prompts
if (workflowOptions.prompt && workflowOptions.prompt.length > 0) {
enhancedConfig = await this.injectPrompts(
enhancedConfig,
workflowOptions.prompt,
);
}
return enhancedConfig;
}
/**
* Add workflow rules and MCPs to options if workflow is active
*/
private async addWorkflowOptions(
options: BaseCommandOptions,
): Promise<BaseCommandOptions> {
// Get workflow options and spread them inline
let options = { ..._options };
try {
const workflowState = await serviceContainer.get<WorkflowServiceState>(
SERVICE_NAMES.WORKFLOW,
);
if (!workflowState.workflowFile) {
return options;
}
if (workflowState.workflowFile) {
// Add workflow rules if present
if (workflowState.workflowFile.rules) {
options.rule = [
workflowState.workflowFile.rules,
...(options.rule || []),
];
logger.debug(`Added workflow rules from ${workflowState.workflow}`);
}
const enhancedOptions = { ...options };
// Add workflow MCP servers if present
if (workflowState.workflowFile.tools) {
const parsedTools = parseWorkflowTools(
workflowState.workflowFile.tools,
);
if (parsedTools.mcpServers.length > 0) {
options.mcp = [...parsedTools.mcpServers, ...(options.mcp || [])];
logger.debug(
`Added ${parsedTools.mcpServers.length} workflow MCP servers`,
);
}
}
// Add workflow rules if present
if (workflowState.workflowFile.rules) {
const existingRules = enhancedOptions.rule || [];
enhancedOptions.rule = [
workflowState.workflowFile.rules,
...existingRules,
];
logger.debug(`Added workflow rules from ${workflowState.workflow}`);
}
// Add workflow MCP servers if present
if (workflowState.workflowFile.tools) {
const parsedTools = parseWorkflowTools(
workflowState.workflowFile.tools,
);
if (parsedTools.mcpServers.length > 0) {
const existingMcps = enhancedOptions.mcp || [];
enhancedOptions.mcp = [...parsedTools.mcpServers, ...existingMcps];
// Add workflow model if present (lower priority than --model flag)
if (workflowState.workflowFile.model && !options.model?.length) {
options.model = [workflowState.workflowFile.model];
logger.debug(
`Added ${parsedTools.mcpServers.length} workflow MCP servers`,
`Added workflow model: ${workflowState.workflowFile.model}`,
);
}
}
return enhancedOptions;
} catch (error: any) {
logger.debug(`Workflow service not available: ${error.message}`);
return options;
}
// Apply rules
if (options.rule && options.rule.length > 0) {
enhancedConfig = await this.injectRules(enhancedConfig, options.rule);
}
// Apply MCPs
if (options.mcp && options.mcp.length > 0) {
enhancedConfig = await this.injectMcps(enhancedConfig, options.mcp);
}
// Apply models
if (options.model && options.model.length > 0) {
enhancedConfig = await this.injectModels(enhancedConfig, options.model);
}
// Apply prompts
if (options.prompt && options.prompt.length > 0) {
enhancedConfig = await this.injectPrompts(enhancedConfig, options.prompt);
}
return enhancedConfig;
}
/**

View file

@ -5,12 +5,7 @@ import { createLlmApi, getLlmApi } from "../config.js";
import { logger } from "../util/logger.js";
import { BaseService, ServiceWithDependencies } from "./BaseService.js";
import { serviceContainer } from "./ServiceContainer.js";
import {
ModelServiceState,
SERVICE_NAMES,
WorkflowServiceState,
} from "./types.js";
import { ModelServiceState } from "./types.js";
/**
* Service for managing LLM and model state
@ -60,44 +55,6 @@ export class ModelService
model && (model.roles?.includes("chat") || model.roles === undefined),
) || []) as ModelConfig[];
// Check for workflow model
try {
const workflowState = await serviceContainer.get<WorkflowServiceState>(
SERVICE_NAMES.WORKFLOW,
);
const workflowModel = workflowState.workflowFile?.model;
if (workflowModel) {
logger.debug("Workflow specifies model", { workflowModel });
// Check if workflow model exists in available models
const workflowModelConfig = this.availableModels.find(
(model) => (model as any).name === workflowModel,
);
const selectedModel =
workflowModelConfig ||
({
provider: "openai",
name: workflowModel,
} as ModelConfig);
const llmApi = createLlmApi(selectedModel, authConfig);
if (!llmApi) {
throw new Error("Failed to initialize LLM with workflow model");
}
return {
llmApi,
model: selectedModel,
assistant,
authConfig,
};
}
} catch (error) {
// Workflow service not available, continue with normal model selection
}
// Check if we have a persisted model name and use it if valid
const persistedModelName = getModelName(authConfig);
if (persistedModelName) {

View file

@ -193,13 +193,14 @@ describe("Workflow Integration Tests", () => {
{},
);
// Rules should be processed normally since workflow rules are now added to options.rule
expect(mockProcessRule).toHaveBeenCalledWith(mockWorkflowFile.rules);
expect(enhancedConfig.rules).toHaveLength(2);
expect(enhancedConfig?.rules?.[0]).toEqual({
name: "workflow:owner/workflow",
rule: "Processed rule content",
});
expect(enhancedConfig?.rules?.[1]).toBe("existing rule");
// The workflow rule is processed first, then existing rules
expect(mockProcessRule).toHaveBeenNthCalledWith(
1,
mockWorkflowFile.rules,
);
});
it("should not inject workflow rules when workflow is inactive", async () => {
@ -254,10 +255,14 @@ describe("Workflow Integration Tests", () => {
mockAuthConfig as any,
);
// Verify that available models are filtered
// Verify that available models include all original models
// (we removed the filtering logic)
const availableModels = modelService.getAvailableChatModels();
expect(availableModels).toHaveLength(1);
expect(availableModels[0].name).toBe("gpt-4-workflow");
expect(availableModels).toHaveLength(2); // Should include both original models
expect(availableModels.map((m) => m.name)).toEqual([
"gpt-3.5-turbo",
"gpt-4",
]);
});
it("should allow all models when no workflow is active", async () => {
@ -443,9 +448,10 @@ describe("Workflow Integration Tests", () => {
{},
);
// Should not duplicate existing MCP server
expect(enhancedConfig.mcpServers).toHaveLength(1);
// Should not deduplicate since we simplified the logic
expect(enhancedConfig.mcpServers).toHaveLength(2);
expect(enhancedConfig.mcpServers?.[0]).toEqual({ name: "mcp1" });
expect(enhancedConfig.mcpServers?.[1]).toEqual({ name: "mcp1" });
});
});
});