fix: cleanup workflow files
This commit is contained in:
parent
4d3bafee60
commit
8c5ca73e13
7 changed files with 509 additions and 102 deletions
|
|
@ -28,64 +28,62 @@ export class ConfigEnhancer {
|
|||
): Promise<AssistantUnrolled> {
|
||||
let enhancedConfig = { ...config };
|
||||
|
||||
// Get workflow options and spread them inline
|
||||
// Add workflow rules/mcp servers if present
|
||||
let options = { ..._options };
|
||||
try {
|
||||
const workflowState = await serviceContainer.get<WorkflowServiceState>(
|
||||
SERVICE_NAMES.WORKFLOW,
|
||||
);
|
||||
const workflowState = await serviceContainer.get<WorkflowServiceState>(
|
||||
SERVICE_NAMES.WORKFLOW,
|
||||
);
|
||||
|
||||
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}`);
|
||||
}
|
||||
if (workflowState.workflowFile) {
|
||||
if (workflowState.workflowFile.rules) {
|
||||
options.rule = [
|
||||
workflowState.workflowFile.rules,
|
||||
...(options.rule || []),
|
||||
];
|
||||
}
|
||||
|
||||
// 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 model if present (lower priority than --model flag)
|
||||
if (workflowState.workflowFile.model && !options.model?.length) {
|
||||
options.model = [workflowState.workflowFile.model];
|
||||
logger.debug(
|
||||
`Added workflow model: ${workflowState.workflowFile.model}`,
|
||||
);
|
||||
if (workflowState.workflowFile.tools) {
|
||||
const parsedTools = parseWorkflowTools(
|
||||
workflowState.workflowFile.tools,
|
||||
);
|
||||
if (parsedTools.mcpServers.length > 0) {
|
||||
options.mcp = [...parsedTools.mcpServers, ...(options.mcp || [])];
|
||||
}
|
||||
}
|
||||
} catch (error: any) {
|
||||
logger.debug(`Workflow service not available: ${error.message}`);
|
||||
|
||||
// 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 workflow model: ${workflowState.workflowFile.model}`,
|
||||
);
|
||||
}
|
||||
|
||||
// Add workflow prompt as prefix
|
||||
if (workflowState.workflowFile.prompt) {
|
||||
options.prompt = [
|
||||
workflowState.workflowFile.prompt,
|
||||
...(options.prompt || []),
|
||||
];
|
||||
logger.debug(
|
||||
`Added workflow prompt as prefix: ${workflowState.workflowFile.prompt.substring(0, 100)}...`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Apply rules
|
||||
// Inject resolved items into config
|
||||
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);
|
||||
}
|
||||
|
|
@ -183,14 +181,17 @@ export class ConfigEnhancer {
|
|||
|
||||
/**
|
||||
* Inject prompts into the configuration
|
||||
* Note: Prompts are processed at runtime via processAndCombinePrompts(),
|
||||
* not injected into the config. This method exists for consistency with
|
||||
* other injection methods but doesn't modify the config.
|
||||
*/
|
||||
private async injectPrompts(
|
||||
config: AssistantUnrolled,
|
||||
prompts: string[],
|
||||
): Promise<AssistantUnrolled> {
|
||||
// For prompts, we'll need a different approach since they become slash commands
|
||||
// This is a placeholder implementation
|
||||
logger.debug("Prompt injection not yet implemented", { prompts });
|
||||
// Prompts are handled at runtime by processAndCombinePrompts in chat.ts
|
||||
// They don't need to be injected into the configuration
|
||||
logger.debug("Prompts will be processed at runtime", { prompts });
|
||||
return config;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
import { AssistantUnrolled, ModelConfig } from "@continuedev/config-yaml";
|
||||
import { describe, expect, beforeEach, vi, test } from "vitest";
|
||||
import { beforeEach, describe, expect, test, vi } from "vitest";
|
||||
|
||||
// Mock dependencies before imports
|
||||
vi.mock("../config.js");
|
||||
vi.mock("../auth/workos.js");
|
||||
|
||||
import { AuthConfig } from "../auth/workos.js";
|
||||
import * as workos from "../auth/workos.js";
|
||||
import { AuthConfig } from "../auth/workos.js";
|
||||
import * as config from "../config.js";
|
||||
|
||||
import { ModelService } from "./ModelService.js";
|
||||
|
|
@ -357,8 +357,8 @@ describe("ModelService", () => {
|
|||
});
|
||||
|
||||
describe("getDependencies()", () => {
|
||||
test("should declare auth and config dependencies", () => {
|
||||
expect(service.getDependencies()).toEqual(["auth", "config"]);
|
||||
test("should declare auth, config, and workflow dependencies", () => {
|
||||
expect(service.getDependencies()).toEqual(["auth", "config", "workflow"]);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ export class ModelService
|
|||
* Declare dependencies on other services
|
||||
*/
|
||||
getDependencies(): string[] {
|
||||
return ["auth", "config"];
|
||||
return ["auth", "config", "workflow"];
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -41,6 +41,10 @@ export class ModelService
|
|||
async doInitialize(
|
||||
assistant: AssistantUnrolled,
|
||||
authConfig: AuthConfig,
|
||||
workflowServiceState?: {
|
||||
workflowFile: any | null;
|
||||
workflow: string | null;
|
||||
},
|
||||
): Promise<ModelServiceState> {
|
||||
logger.debug("ModelService.doInitialize called", {
|
||||
hasAssistant: !!assistant,
|
||||
|
|
@ -55,16 +59,42 @@ export class ModelService
|
|||
model && (model.roles?.includes("chat") || model.roles === undefined),
|
||||
) || []) as ModelConfig[];
|
||||
|
||||
// Check if we have a persisted model name and use it if valid
|
||||
const persistedModelName = getModelName(authConfig);
|
||||
if (persistedModelName) {
|
||||
// Check if workflow has a model specified (highest priority)
|
||||
let preferredModelName: string | null = null;
|
||||
let modelSource = "default";
|
||||
|
||||
if (workflowServiceState?.workflowFile?.model) {
|
||||
preferredModelName = workflowServiceState.workflowFile.model;
|
||||
modelSource = "workflow";
|
||||
logger.debug("Found workflow model preference", {
|
||||
workflowModel: preferredModelName,
|
||||
workflowName: workflowServiceState.workflowFile.name,
|
||||
});
|
||||
} else {
|
||||
// Fall back to persisted model name if no workflow model
|
||||
preferredModelName = getModelName(authConfig);
|
||||
if (preferredModelName) {
|
||||
modelSource = "persisted";
|
||||
}
|
||||
}
|
||||
|
||||
// Try to use the preferred model (workflow or persisted)
|
||||
if (preferredModelName) {
|
||||
// During initialization, we need to check against availableModels directly
|
||||
const modelIndex = this.availableModels.findIndex((model) => {
|
||||
const name = (model as any).name || (model as any).model;
|
||||
return name === persistedModelName;
|
||||
return name === preferredModelName;
|
||||
});
|
||||
if (modelIndex === -1) {
|
||||
// Model name not found, use default model selection
|
||||
// Preferred model not found, use default model selection
|
||||
logger.debug(
|
||||
`Preferred model "${preferredModelName}" from ${modelSource} not found, using default model`,
|
||||
{
|
||||
preferredModelName,
|
||||
modelSource,
|
||||
availableModels: this.availableModels.length,
|
||||
},
|
||||
);
|
||||
const [llmApi, model] = getLlmApi(assistant, authConfig);
|
||||
return {
|
||||
llmApi,
|
||||
|
|
@ -73,20 +103,21 @@ export class ModelService
|
|||
authConfig,
|
||||
};
|
||||
} else {
|
||||
// Use the persisted model - but we need to handle initialization specially
|
||||
// Use the preferred model - but we need to handle initialization specially
|
||||
// During init, currentState isn't set yet, so switchModel would fail
|
||||
// Instead, we'll manually switch here
|
||||
const selectedModel = this.availableModels[modelIndex];
|
||||
logger.debug("Using persisted model during initialization", {
|
||||
logger.debug(`Using ${modelSource} model during initialization`, {
|
||||
modelIndex,
|
||||
provider: selectedModel.provider,
|
||||
name: (selectedModel as any).name || "unnamed",
|
||||
modelSource,
|
||||
});
|
||||
|
||||
const llmApi = createLlmApi(selectedModel, authConfig);
|
||||
|
||||
if (!llmApi) {
|
||||
throw new Error("Failed to initialize LLM with persisted model");
|
||||
throw new Error(`Failed to initialize LLM with ${modelSource} model`);
|
||||
}
|
||||
|
||||
return {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,243 @@
|
|||
import { AssistantUnrolled, ModelConfig } from "@continuedev/config-yaml";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { AuthConfig } from "../auth/workos.js";
|
||||
import * as config from "../config.js";
|
||||
import { ModelService } from "./ModelService.js";
|
||||
import { WorkflowServiceState } from "./types.js";
|
||||
|
||||
// Mock the dependencies
|
||||
vi.mock("../auth/workos.js", () => ({
|
||||
getModelName: vi.fn(() => null),
|
||||
}));
|
||||
|
||||
vi.mock("../config.js", () => ({
|
||||
createLlmApi: vi.fn(() => ({ provider: "test" })),
|
||||
getLlmApi: vi.fn(() => [
|
||||
{ provider: "test" },
|
||||
{ provider: "test", name: "default-model" },
|
||||
]),
|
||||
}));
|
||||
|
||||
vi.mock("../util/logger.js", () => ({
|
||||
logger: {
|
||||
debug: vi.fn(),
|
||||
error: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
describe("ModelService workflow model prioritization", () => {
|
||||
let modelService: ModelService;
|
||||
let mockAssistant: AssistantUnrolled;
|
||||
let mockAuthConfig: AuthConfig;
|
||||
let mockModels: ModelConfig[];
|
||||
|
||||
beforeEach(() => {
|
||||
modelService = new ModelService();
|
||||
|
||||
mockModels = [
|
||||
{ provider: "anthropic", name: "claude-3-sonnet" } as ModelConfig,
|
||||
{ provider: "openai", name: "gpt-4" } as ModelConfig,
|
||||
{ provider: "anthropic", name: "claude-3-haiku" } as ModelConfig,
|
||||
];
|
||||
|
||||
mockAssistant = {
|
||||
models: mockModels,
|
||||
} as AssistantUnrolled;
|
||||
|
||||
mockAuthConfig = {} as AuthConfig;
|
||||
|
||||
// Reset mocks
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it("should prioritize workflow model over default when workflow file has model specified", async () => {
|
||||
const workflowServiceState: WorkflowServiceState = {
|
||||
workflowFile: {
|
||||
name: "test-workflow",
|
||||
model: "gpt-4",
|
||||
prompt: "Test workflow",
|
||||
},
|
||||
workflow: "test/workflow",
|
||||
};
|
||||
|
||||
// Mock createLlmApi to return different models based on the selected model
|
||||
const createLlmApiMock = vi.mocked(config.createLlmApi);
|
||||
createLlmApiMock.mockImplementation(
|
||||
(model: ModelConfig) =>
|
||||
({
|
||||
provider: model.provider,
|
||||
name: (model as any).name,
|
||||
}) as any,
|
||||
);
|
||||
|
||||
const result = await modelService.doInitialize(
|
||||
mockAssistant,
|
||||
mockAuthConfig,
|
||||
workflowServiceState,
|
||||
);
|
||||
|
||||
expect(result.model).toEqual(
|
||||
expect.objectContaining({
|
||||
name: "gpt-4",
|
||||
provider: "openai",
|
||||
}),
|
||||
);
|
||||
|
||||
// Should have called createLlmApi with the workflow-specified model
|
||||
expect(createLlmApiMock).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ name: "gpt-4" }),
|
||||
mockAuthConfig,
|
||||
);
|
||||
});
|
||||
|
||||
it("should fall back to persisted model when no workflow model is specified", async () => {
|
||||
const workflowServiceState: WorkflowServiceState = {
|
||||
workflowFile: {
|
||||
name: "test-workflow",
|
||||
prompt: "Test workflow",
|
||||
// No model specified
|
||||
},
|
||||
workflow: "test/workflow",
|
||||
};
|
||||
|
||||
// Mock getModelName to return a persisted model
|
||||
const { getModelName } = await import("../auth/workos.js");
|
||||
vi.mocked(getModelName).mockReturnValue("claude-3-sonnet");
|
||||
|
||||
const createLlmApiMock = vi.mocked(config.createLlmApi);
|
||||
createLlmApiMock.mockImplementation(
|
||||
(model: ModelConfig) =>
|
||||
({
|
||||
provider: model.provider,
|
||||
name: (model as any).name,
|
||||
}) as any,
|
||||
);
|
||||
|
||||
const result = await modelService.doInitialize(
|
||||
mockAssistant,
|
||||
mockAuthConfig,
|
||||
workflowServiceState,
|
||||
);
|
||||
|
||||
expect(result.model).toEqual(
|
||||
expect.objectContaining({
|
||||
name: "claude-3-sonnet",
|
||||
provider: "anthropic",
|
||||
}),
|
||||
);
|
||||
|
||||
expect(createLlmApiMock).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ name: "claude-3-sonnet" }),
|
||||
mockAuthConfig,
|
||||
);
|
||||
});
|
||||
|
||||
it("should fall back to default model when workflow model is not available", async () => {
|
||||
const workflowServiceState: WorkflowServiceState = {
|
||||
workflowFile: {
|
||||
name: "test-workflow",
|
||||
model: "non-existent-model", // Model not in available models
|
||||
prompt: "Test workflow",
|
||||
},
|
||||
workflow: "test/workflow",
|
||||
};
|
||||
|
||||
const getLlmApiMock = vi.mocked(config.getLlmApi);
|
||||
getLlmApiMock.mockReturnValue([
|
||||
{ provider: "default" } as any,
|
||||
{ provider: "anthropic", name: "claude-3-sonnet" } as ModelConfig,
|
||||
]);
|
||||
|
||||
const result = await modelService.doInitialize(
|
||||
mockAssistant,
|
||||
mockAuthConfig,
|
||||
workflowServiceState,
|
||||
);
|
||||
|
||||
expect(result.model).toEqual(
|
||||
expect.objectContaining({
|
||||
name: "claude-3-sonnet",
|
||||
provider: "anthropic",
|
||||
}),
|
||||
);
|
||||
|
||||
// Should have fallen back to getLlmApi
|
||||
expect(getLlmApiMock).toHaveBeenCalledWith(mockAssistant, mockAuthConfig);
|
||||
});
|
||||
|
||||
it("should use default model when no workflow file exists", async () => {
|
||||
const workflowServiceState: WorkflowServiceState = {
|
||||
workflowFile: null,
|
||||
workflow: null,
|
||||
};
|
||||
|
||||
// Make sure getModelName returns null (no persisted model)
|
||||
const { getModelName } = await import("../auth/workos.js");
|
||||
vi.mocked(getModelName).mockReturnValue(null);
|
||||
|
||||
const getLlmApiMock = vi.mocked(config.getLlmApi);
|
||||
getLlmApiMock.mockReturnValue([
|
||||
{ provider: "default" } as any,
|
||||
{ provider: "anthropic", name: "claude-3-sonnet" } as ModelConfig,
|
||||
]);
|
||||
|
||||
const result = await modelService.doInitialize(
|
||||
mockAssistant,
|
||||
mockAuthConfig,
|
||||
workflowServiceState,
|
||||
);
|
||||
|
||||
expect(result.model).toEqual(
|
||||
expect.objectContaining({
|
||||
name: "claude-3-sonnet",
|
||||
provider: "anthropic",
|
||||
}),
|
||||
);
|
||||
|
||||
expect(getLlmApiMock).toHaveBeenCalledWith(mockAssistant, mockAuthConfig);
|
||||
});
|
||||
|
||||
it("should prioritize workflow model over persisted model", async () => {
|
||||
const workflowServiceState: WorkflowServiceState = {
|
||||
workflowFile: {
|
||||
name: "test-workflow",
|
||||
model: "gpt-4", // Workflow specifies gpt-4
|
||||
prompt: "Test workflow",
|
||||
},
|
||||
workflow: "test/workflow",
|
||||
};
|
||||
|
||||
// Mock getModelName to return a different persisted model
|
||||
const { getModelName } = await import("../auth/workos.js");
|
||||
vi.mocked(getModelName).mockReturnValue("claude-3-haiku");
|
||||
|
||||
const createLlmApiMock = vi.mocked(config.createLlmApi);
|
||||
createLlmApiMock.mockImplementation(
|
||||
(model: ModelConfig) =>
|
||||
({
|
||||
provider: model.provider,
|
||||
name: (model as any).name,
|
||||
}) as any,
|
||||
);
|
||||
|
||||
const result = await modelService.doInitialize(
|
||||
mockAssistant,
|
||||
mockAuthConfig,
|
||||
workflowServiceState,
|
||||
);
|
||||
|
||||
// Should use workflow model (gpt-4), not persisted model (claude-3-haiku)
|
||||
expect(result.model).toEqual(
|
||||
expect.objectContaining({
|
||||
name: "gpt-4",
|
||||
provider: "openai",
|
||||
}),
|
||||
);
|
||||
|
||||
expect(createLlmApiMock).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ name: "gpt-4" }),
|
||||
mockAuthConfig,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
import { describe, expect, test, beforeEach, vi } from "vitest";
|
||||
import { beforeEach, describe, expect, test, vi } from "vitest";
|
||||
|
||||
// Mock modules
|
||||
vi.mock("../permissions/permissionsYamlLoader.js");
|
||||
|
|
@ -30,6 +30,7 @@ describe("ToolPermissionService", () => {
|
|||
currentMode: "normal",
|
||||
isHeadless: false,
|
||||
modePolicyCount: 0,
|
||||
workflowPolicyCount: 0,
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ import {
|
|||
SERVICE_NAMES,
|
||||
ServiceInitOptions,
|
||||
ServiceInitResult,
|
||||
WorkflowServiceState,
|
||||
} from "./types.js";
|
||||
import { UpdateService } from "./UpdateService.js";
|
||||
import { WorkflowService } from "./WorkflowService.js";
|
||||
|
|
@ -214,18 +215,23 @@ export async function initializeServices(
|
|||
serviceContainer.register(
|
||||
SERVICE_NAMES.MODEL,
|
||||
async () => {
|
||||
const [configState, authState] = await Promise.all([
|
||||
const [configState, authState, workflowState] = await Promise.all([
|
||||
serviceContainer.get<ConfigServiceState>(SERVICE_NAMES.CONFIG),
|
||||
serviceContainer.get<AuthServiceState>(SERVICE_NAMES.AUTH),
|
||||
serviceContainer.get<WorkflowServiceState>(SERVICE_NAMES.WORKFLOW),
|
||||
]);
|
||||
|
||||
if (!configState.config) {
|
||||
throw new Error("Config not available");
|
||||
}
|
||||
|
||||
return modelService.initialize(configState.config, authState.authConfig);
|
||||
return modelService.initialize(
|
||||
configState.config,
|
||||
authState.authConfig,
|
||||
workflowState,
|
||||
);
|
||||
},
|
||||
[SERVICE_NAMES.CONFIG, SERVICE_NAMES.AUTH], // Depends on config and auth
|
||||
[SERVICE_NAMES.CONFIG, SERVICE_NAMES.AUTH, SERVICE_NAMES.WORKFLOW], // Depends on config, auth, and workflow
|
||||
);
|
||||
|
||||
serviceContainer.register(
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { vi } from "vitest";
|
||||
|
||||
import { ConfigEnhancer } from "src/configEnhancer.js";
|
||||
import { ConfigEnhancer } from "../configEnhancer.js";
|
||||
import { ModelService } from "./ModelService.js";
|
||||
import { serviceContainer } from "./ServiceContainer.js";
|
||||
import { ToolPermissionService } from "./ToolPermissionService.js";
|
||||
|
|
@ -46,6 +46,7 @@ describe("Workflow Integration Tests", () => {
|
|||
let mockProcessRule: any;
|
||||
let mockCreateLlmApi: any;
|
||||
let mockGetLlmApi: any;
|
||||
let mockModelProcessor: any;
|
||||
|
||||
const mockWorkflowFile = {
|
||||
name: "Test Workflow",
|
||||
|
|
@ -85,6 +86,7 @@ describe("Workflow Integration Tests", () => {
|
|||
mockLoadPackageFromHub = hubLoaderModule.loadPackageFromHub as any;
|
||||
mockLoadPackagesFromHub = hubLoaderModule.loadPackagesFromHub as any;
|
||||
mockProcessRule = hubLoaderModule.processRule as any;
|
||||
mockModelProcessor = hubLoaderModule.modelProcessor;
|
||||
mockCreateLlmApi = configModule.createLlmApi as any;
|
||||
mockGetLlmApi = configModule.getLlmApi as any;
|
||||
|
||||
|
|
@ -107,6 +109,10 @@ describe("Workflow Integration Tests", () => {
|
|||
// Setup default mocks
|
||||
mockProcessRule.mockResolvedValue("Processed rule content");
|
||||
mockLoadPackagesFromHub.mockResolvedValue([{ name: "test-mcp" }]);
|
||||
mockLoadPackageFromHub.mockResolvedValue({
|
||||
name: "test-model",
|
||||
provider: "openai",
|
||||
});
|
||||
mockCreateLlmApi.mockReturnValue({ mock: "llmApi" });
|
||||
mockGetLlmApi.mockReturnValue([
|
||||
{ mock: "llmApi" },
|
||||
|
|
@ -114,8 +120,8 @@ describe("Workflow Integration Tests", () => {
|
|||
]);
|
||||
});
|
||||
|
||||
describe("WorkflowService affects ModelService", () => {
|
||||
it("should use workflow model when workflow is active", async () => {
|
||||
describe("Workflow models are injected via ConfigEnhancer", () => {
|
||||
it("should add workflow model to options when workflow is active", async () => {
|
||||
// Setup workflow service with active workflow
|
||||
mockLoadPackageFromHub.mockResolvedValue(mockWorkflowFile);
|
||||
await workflowService.initialize("owner/workflow");
|
||||
|
|
@ -123,57 +129,65 @@ describe("Workflow Integration Tests", () => {
|
|||
const workflowState = workflowService.getState();
|
||||
expect(workflowState.workflowFile?.model).toBe("gpt-4-workflow");
|
||||
|
||||
// Initialize model service - it should use the workflow model
|
||||
await modelService.initialize(
|
||||
mockAssistant as any,
|
||||
mockAuthConfig as any,
|
||||
// Test that ConfigEnhancer adds the workflow model to options
|
||||
const baseConfig = { models: [] };
|
||||
const baseOptions = {}; // No --model flag
|
||||
|
||||
const enhancedConfig = await configEnhancer.enhanceConfig(
|
||||
baseConfig as any,
|
||||
baseOptions,
|
||||
);
|
||||
|
||||
expect(mockCreateLlmApi).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
name: "gpt-4-workflow",
|
||||
provider: "openai",
|
||||
}),
|
||||
mockAuthConfig,
|
||||
// Should have loaded the workflow model via injectModels
|
||||
expect(mockLoadPackagesFromHub).toHaveBeenCalledWith(
|
||||
["gpt-4-workflow"],
|
||||
expect.anything(),
|
||||
);
|
||||
});
|
||||
|
||||
it("should use regular models when no workflow is active", async () => {
|
||||
it("should not add workflow model when no workflow is active", async () => {
|
||||
// Initialize workflow service without workflow
|
||||
await workflowService.initialize();
|
||||
|
||||
const workflowState = workflowService.getState();
|
||||
expect(workflowState.workflowFile).toBeNull();
|
||||
|
||||
// Initialize model service - it should use regular model selection
|
||||
await modelService.initialize(
|
||||
mockAssistant as any,
|
||||
mockAuthConfig as any,
|
||||
// Test that ConfigEnhancer doesn't add any workflow models
|
||||
const baseConfig = { models: [] };
|
||||
const baseOptions = {};
|
||||
|
||||
const enhancedConfig = await configEnhancer.enhanceConfig(
|
||||
baseConfig as any,
|
||||
baseOptions,
|
||||
);
|
||||
|
||||
expect(mockGetLlmApi).toHaveBeenCalledWith(mockAssistant, mockAuthConfig);
|
||||
// Should not have enhanced with any models
|
||||
expect(enhancedConfig.models).toEqual([]);
|
||||
});
|
||||
|
||||
it("should handle workflow model that exists in assistant models", async () => {
|
||||
const workflowWithExistingModel = {
|
||||
...mockWorkflowFile,
|
||||
model: "gpt-4", // This exists in mockAssistant.models
|
||||
};
|
||||
|
||||
mockLoadPackageFromHub.mockResolvedValue(workflowWithExistingModel);
|
||||
it("should respect --model flag priority over workflow model", async () => {
|
||||
// Setup workflow service with active workflow
|
||||
mockLoadPackageFromHub.mockResolvedValue(mockWorkflowFile);
|
||||
await workflowService.initialize("owner/workflow");
|
||||
|
||||
await modelService.initialize(
|
||||
mockAssistant as any,
|
||||
mockAuthConfig as any,
|
||||
// Test that --model flag takes precedence
|
||||
const baseConfig = { models: [] };
|
||||
const baseOptions = { model: ["user-specified-model"] }; // User specified --model
|
||||
|
||||
const enhancedConfig = await configEnhancer.enhanceConfig(
|
||||
baseConfig as any,
|
||||
baseOptions,
|
||||
);
|
||||
|
||||
expect(mockCreateLlmApi).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
name: "gpt-4",
|
||||
provider: "openai",
|
||||
}),
|
||||
mockAuthConfig,
|
||||
// Should process the user model, not the workflow model
|
||||
expect(mockLoadPackagesFromHub).toHaveBeenCalledWith(
|
||||
["user-specified-model"],
|
||||
expect.anything(),
|
||||
);
|
||||
// Workflow model should not be added to the options since --model was provided
|
||||
expect(mockLoadPackagesFromHub).not.toHaveBeenCalledWith(
|
||||
["gpt-4-workflow"],
|
||||
expect.anything(),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
|
@ -319,11 +333,13 @@ describe("Workflow Integration Tests", () => {
|
|||
expect(enhancedConfig.rules?.[0]).toBe("existing rule");
|
||||
});
|
||||
|
||||
it("should handle missing service container gracefully", async () => {
|
||||
// Mock service container to throw error
|
||||
vi.spyOn(serviceContainer, "get").mockRejectedValue(
|
||||
new Error("Service not ready"),
|
||||
);
|
||||
// Removed test for missing service container since workflow service
|
||||
// should always be initialized before ConfigEnhancer is called
|
||||
|
||||
it("should inject workflow prompt when workflow is active", async () => {
|
||||
// Setup workflow service with active workflow
|
||||
mockLoadPackageFromHub.mockResolvedValue(mockWorkflowFile);
|
||||
await workflowService.initialize("owner/workflow");
|
||||
|
||||
const baseConfig = {
|
||||
rules: ["existing rule"],
|
||||
|
|
@ -334,9 +350,118 @@ describe("Workflow Integration Tests", () => {
|
|||
{},
|
||||
);
|
||||
|
||||
// Should return config unchanged
|
||||
expect(enhancedConfig.rules).toHaveLength(1);
|
||||
expect(enhancedConfig.rules?.[0]).toBe("existing rule");
|
||||
// Prompts should be processed at runtime, not injected into config
|
||||
// The workflow prompt is added to options.prompt and will be processed
|
||||
// by processAndCombinePrompts() in chat.ts
|
||||
expect(enhancedConfig.rules).toHaveLength(2);
|
||||
});
|
||||
|
||||
it("should add workflow prompt as prefix to existing prompts", async () => {
|
||||
// Setup workflow service with active workflow
|
||||
mockLoadPackageFromHub.mockResolvedValue(mockWorkflowFile);
|
||||
await workflowService.initialize("owner/workflow");
|
||||
|
||||
// Test that the workflow prompt gets added to options.prompt as prefix
|
||||
const baseOptions = { prompt: ["existing-prompt"] };
|
||||
const modifiedOptions = { ...baseOptions };
|
||||
|
||||
// Simulate what ConfigEnhancer does internally
|
||||
const workflowState = workflowService.getState();
|
||||
if (workflowState.workflowFile?.prompt) {
|
||||
modifiedOptions.prompt = [
|
||||
workflowState.workflowFile.prompt,
|
||||
...(modifiedOptions.prompt || []),
|
||||
];
|
||||
}
|
||||
|
||||
expect(modifiedOptions.prompt).toEqual([
|
||||
"You are a workflow assistant.",
|
||||
"existing-prompt",
|
||||
]);
|
||||
});
|
||||
|
||||
it("should not add workflow prompt when workflow has no prompt", async () => {
|
||||
const workflowWithoutPrompt = {
|
||||
...mockWorkflowFile,
|
||||
prompt: undefined,
|
||||
};
|
||||
|
||||
mockLoadPackageFromHub.mockResolvedValue(workflowWithoutPrompt);
|
||||
await workflowService.initialize("owner/workflow");
|
||||
|
||||
const baseOptions = { prompt: ["existing-prompt"] };
|
||||
const modifiedOptions = { ...baseOptions };
|
||||
|
||||
// Simulate what ConfigEnhancer does internally
|
||||
const workflowState = workflowService.getState();
|
||||
if (workflowState.workflowFile?.prompt) {
|
||||
modifiedOptions.prompt = [
|
||||
workflowState.workflowFile.prompt,
|
||||
...(modifiedOptions.prompt || []),
|
||||
];
|
||||
}
|
||||
|
||||
expect(modifiedOptions.prompt).toEqual(["existing-prompt"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("ConfigEnhancer prompt integration", () => {
|
||||
it("should modify options to include workflow prompt as prefix", async () => {
|
||||
// Setup workflow service with active workflow
|
||||
mockLoadPackageFromHub.mockResolvedValue(mockWorkflowFile);
|
||||
await workflowService.initialize("owner/workflow");
|
||||
|
||||
// Create a spy to verify the internal behavior of ConfigEnhancer
|
||||
const originalGet = serviceContainer.get;
|
||||
const getSpy = vi
|
||||
.spyOn(serviceContainer, "get")
|
||||
.mockImplementation(async (serviceName: string) => {
|
||||
if (serviceName === SERVICE_NAMES.WORKFLOW) {
|
||||
return workflowService.getState();
|
||||
}
|
||||
return originalGet.call(serviceContainer, serviceName);
|
||||
});
|
||||
|
||||
const baseOptions = { prompt: ["user-prompt"] };
|
||||
|
||||
// This should internally modify the options to include workflow prompt
|
||||
await configEnhancer.enhanceConfig({} as any, baseOptions);
|
||||
|
||||
// The ConfigEnhancer should have been called with workflow service
|
||||
expect(getSpy).toHaveBeenCalledWith(SERVICE_NAMES.WORKFLOW);
|
||||
|
||||
// Clean up
|
||||
getSpy.mockRestore();
|
||||
});
|
||||
|
||||
it("should work end-to-end with workflow prompt processing", async () => {
|
||||
// Setup workflow service with active workflow
|
||||
mockLoadPackageFromHub.mockResolvedValue(mockWorkflowFile);
|
||||
await workflowService.initialize("owner/workflow");
|
||||
|
||||
const workflowState = workflowService.getState();
|
||||
expect(workflowState.workflowFile?.prompt).toBe(
|
||||
"You are a workflow assistant.",
|
||||
);
|
||||
|
||||
// Verify that if we manually apply the same logic as ConfigEnhancer,
|
||||
// the workflow prompt gets added as prefix
|
||||
const options = { prompt: ["Tell me about TypeScript"] };
|
||||
|
||||
if (workflowState.workflowFile?.prompt) {
|
||||
options.prompt = [workflowState.workflowFile.prompt, ...options.prompt];
|
||||
}
|
||||
|
||||
expect(options.prompt).toEqual([
|
||||
"You are a workflow assistant.",
|
||||
"Tell me about TypeScript",
|
||||
]);
|
||||
|
||||
// This mimics what processAndCombinePrompts would do
|
||||
const combinedPrompt = options.prompt.join("\n\n");
|
||||
expect(combinedPrompt).toBe(
|
||||
"You are a workflow assistant.\n\nTell me about TypeScript",
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue