Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,14 @@ const getCodefulWorkflowTemplateFileName = (workflowType: WorkflowType): string
switch (workflowType) {
case WorkflowType.statefulCodeful:
return 'StatefulCodefulWorkflow';
case WorkflowType.statelessCodeful:
return 'StatelessCodefulWorkflow';
case WorkflowType.agenticCodeful:
return 'AgenticCodefulWorkflow';
case WorkflowType.agentCodeful:
default:
return 'AgentCodefulWorkflow';
default:
throw new Error(`Unsupported codeful workflow type: ${workflowType}`);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
updateWorkspaceFile,
} from '../CreateLogicAppWorkspace';
import { createLogicAppVsCodeContents } from '../CreateLogicAppVSCodeContents';
import { ProjectType } from '@microsoft/vscode-extension-logic-apps';
import { ProjectType, WorkflowType } from '@microsoft/vscode-extension-logic-apps';
import type { IActionContext } from '@microsoft/vscode-azext-utils';
import type { IWebviewProjectContext, IProjectWizardContext } from '@microsoft/vscode-extension-logic-apps';
import { hostFileName } from '../../../../../constants';
Expand Down Expand Up @@ -937,7 +937,7 @@ local.settings.json`
logicAppName: 'TestLogicApp',
logicAppType,
workflowName: 'SettingsWorkflow',
workflowType: 'Stateful',
workflowType: logicAppType === ProjectType.codeful ? WorkflowType.statefulCodeful : 'Stateful',
functionFolderName: 'Functions',
functionName: 'TestFunction',
functionNamespace: 'TestNamespace',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ describe('CreateLogicAppWorkspace - Codeful Workflows', () => {

expect(templateContent).toContain('WorkflowActions.Managed.Msnweather("msnweather").CurrentWeather');
expect(templateContent).toContain('location: () => "98058"');
expect(templateContent).toContain('units: () => UnitsInput.Imperial');
expect(templateContent).toContain('public class <%= flowNameClass %> : IWorkflowProvider');
expect(templateContent).toContain('WorkflowActions.BuiltIn.Response(responseBody: () => $"{getCurrentWeatherAction.Body}")');
expect(templateContent).toContain('WorkflowFactory.CreateStatefulWorkflow(<%= flowName %>, workflow)');
Expand All @@ -101,6 +102,22 @@ describe('CreateLogicAppWorkspace - Codeful Workflows', () => {
});
});

describe('StatelessCodefulWorkflow template content', () => {
it('should use the provider-based stateless SDK template', () => {
const templateContent = actualFs.readFileSync(
new URL('../../../../../assets/CodefulProjectTemplate/StatelessCodefulWorkflow', import.meta.url),
'utf-8'
);

expect(templateContent).toContain('public class <%= flowNameClass %> : IWorkflowProvider');
expect(templateContent).toContain('WorkflowTriggers.BuiltIn.CreateHttpTrigger()');
expect(templateContent).toContain('WorkflowActions.BuiltIn.Compose(inputs: () => "hello")');
expect(templateContent).toContain('WorkflowFactory.CreateStatelessWorkflow(<%= flowName %>, workflow)');
expect(templateContent).not.toContain('WorkflowBuilderFactory');
expect(templateContent).not.toContain('AddWorkflow()');
});
});

describe('AgentCodefulWorkflow template content', () => {
it('should use the provider-based conversational agent SDK template', () => {
const templateContent = actualFs.readFileSync(
Expand All @@ -113,6 +130,10 @@ describe('CreateLogicAppWorkspace - Codeful Workflows', () => {
expect(templateContent).toContain('WorkflowActions.BuiltIn.Agent(');
expect(templateContent).toContain('WorkflowFactory.CreateAgentWorkflow(<%= flowName %>, workflow)');
expect(templateContent).toContain('WorkflowActions.Managed.Office365("outlook").SendEmail');
expect(templateContent).toContain('using Microsoft.Azure.Workflows.Sdk.Connectors.Office365;');
expect(templateContent).toContain('units: () => UnitsInput.Imperial');
expect(templateContent).toContain('emailMessage: () => new SendEmailEmailMessageInput(');
expect(templateContent).not.toContain('emailMessageto');
expect(templateContent).not.toContain('AgentBuilder');
expect(templateContent).not.toContain('.Builder');
expect(templateContent).not.toContain('CreateAgentTrigger');
Expand All @@ -134,6 +155,10 @@ describe('CreateLogicAppWorkspace - Codeful Workflows', () => {
expect(templateContent).toContain('WorkflowFactory.CreateStatefulWorkflow(<%= flowName %>, workflow)');
expect(templateContent).toContain('MessageRole.User');
expect(templateContent).toContain('WorkflowActions.Managed.Office365("outlook").SendEmail');
expect(templateContent).toContain('using Microsoft.Azure.Workflows.Sdk.Connectors.Office365;');
expect(templateContent).toContain('units: () => UnitsInput.Imperial');
expect(templateContent).toContain('emailMessage: () => new SendEmailEmailMessageInput(');
expect(templateContent).not.toContain('emailMessageto');
expect(templateContent).not.toContain('AgentBuilder');
expect(templateContent).not.toContain('.Builder');
expect(templateContent).not.toContain('CreateAgentTrigger');
Expand Down Expand Up @@ -283,6 +308,44 @@ describe('CreateLogicAppWorkspace - Codeful Workflows', () => {
expect(programWriteCall?.[1]).not.toContain(`${testWorkflowName}.AddWorkflow()`);
});

it('should create a stateless provider workflow from the StatelessCodeful template', async () => {
const statelessTemplate = actualFs.readFileSync(
new URL('../../../../../assets/CodefulProjectTemplate/StatelessCodefulWorkflow', import.meta.url),
'utf-8'
);
const programTemplate = actualFs.readFileSync(
new URL('../../../../../assets/CodefulProjectTemplate/ProgramFile', import.meta.url),
'utf-8'
);

vi.mocked(fse.readFile).mockImplementation((filePath: string) => {
if (filePath.includes('StatelessCodefulWorkflow')) {
return Promise.resolve(statelessTemplate);
}
if (filePath.includes('ProgramFile')) {
return Promise.resolve(programTemplate);
}
if (filePath.includes('CodefulProj')) {
return Promise.resolve('mock content');
}
return Promise.reject(new Error('Unexpected file read'));
});
vi.mocked(fse.pathExists).mockResolvedValue(false);
vi.mocked(fse.writeFile).mockResolvedValue(undefined);

await CreateLogicAppWorkspaceModule.createCodefulWorkflowFile(
testProjectPath,
testProjectName,
testWorkflowName,
WorkflowType.statelessCodeful
);

expect(vi.mocked(fse.readFile)).toHaveBeenCalledWith(expect.stringContaining('StatelessCodefulWorkflow'), 'utf-8');
const workflowWriteCall = vi.mocked(fse.writeFile).mock.calls.find((call: any) => call[0].includes(`${testWorkflowName}.cs`));
expect(workflowWriteCall?.[1]).toContain(`public class ${testWorkflowName} : IWorkflowProvider`);
expect(workflowWriteCall?.[1]).toContain(`WorkflowFactory.CreateStatelessWorkflow("${testWorkflowName}", workflow)`);
});

it('should not update Program.cs when adding provider-based agents to an existing project', async () => {
const agenticCodefulTemplate = 'public class <%= flowNameClass %> : IWorkflowProvider { }';
const existingProgramContent = `class Program {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,8 +396,8 @@ describe('createLogicAppWorkspace - Integration Tests', () => {
[ProjectType.logicApp, 'net10.0', 'Stateful', 'net10.0'],
[ProjectType.rulesEngine, 'net10.0', 'Stateful', undefined],
[ProjectType.rulesEngine, undefined, 'Stateful', undefined],
[ProjectType.codeful, 'net10.0', 'Stateful', undefined],
[ProjectType.codeful, undefined, 'Stateful', undefined],
[ProjectType.codeful, 'net10.0', WorkflowType.statefulCodeful, undefined],
[ProjectType.codeful, undefined, WorkflowType.statefulCodeful, undefined],
])('scaffolds type=%s framework=%s workflow=%s with version=%s', async (logicAppType, targetFramework, workflowType, version) => {
vi.mocked(vscode.workspace.getConfiguration).mockReturnValue({
get: vi.fn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,28 @@ describe('LocalCodefulOverviewPanel', () => {
);
});

it('preserves stateless kind during source-only workflow discovery', async () => {
mocks.isRuntimeUp.mockResolvedValue(false);
mocks.readFileSync.mockReturnValue(`
var trigger = WorkflowTriggers.BuiltIn.CreateHttpTrigger();
var workflow = trigger.Then(response);
return new[] { WorkflowFactory.CreateStatelessWorkflow("stateless-workflow", workflow) };
`);

const codefulPanel = new LocalCodefulOverviewPanel(context, vscode.Uri.file(codefulFilePath) as any);
await codefulPanel.create();

const messageHandler = panel.webview.onDidReceiveMessage.mock.calls[0][0];
await messageHandler({ command: ExtensionCommand.initialize });

const initCall = panel.webview.postMessage.mock.calls.find(([msg]: any) => msg.command === ExtensionCommand.initialize_frame);
const workflowProperties = initCall?.[0].data.workflowPropertiesList[0];

expect(workflowProperties.name).toBe('stateless-workflow');
expect(workflowProperties.kind).toBe('Stateless');
expect(workflowProperties.stateType).toBe('Stateless');
});

it('posts per-workflow callback URL updates when the runtime base URL appears', async () => {
vi.useFakeTimers();
(ext as any).workflowRuntimePort = undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,11 @@ export async function getCodefulWorkflowDataList(

const hasHttpTrigger = hasHttpRequestTrigger(workflowContent);
const fallbackTriggerName = getFallbackCodefulTriggerName(workflowContent, hasHttpTrigger);
const workflowNames = getCodefulWorkflowNames(workflowFilePath);
if (workflowNames.length > 0) {
const sourceWorkflows = getCodefulWorkflows(workflowFilePath);
if (sourceWorkflows.length > 0) {
return {
workflows: workflowNames.map((workflowName) => ({
workflowName,
workflowKind: 'Stateful',
workflows: sourceWorkflows.map((workflow) => ({
...workflow,
triggerName: fallbackTriggerName,
triggerType: hasHttpTrigger ? 'Request' : undefined,
triggerKind: hasHttpTrigger ? 'Http' : undefined,
Expand All @@ -169,7 +168,8 @@ export async function getCodefulWorkflowDataList(
? [
{
workflowName: workflowInfo.workflowName,
workflowKind: workflowInfo.workflowType === 'agent' ? 'Agent' : 'Stateful',
workflowKind:
workflowInfo.workflowType === 'agent' ? 'Agent' : workflowInfo.workflowType === 'stateless' ? 'Stateless' : 'Stateful',
triggerName: fallbackTriggerName,
triggerType: hasHttpTrigger ? 'Request' : undefined,
triggerKind: hasHttpTrigger ? 'Http' : undefined,
Expand Down Expand Up @@ -235,8 +235,8 @@ export async function getRuntimeCodefulWorkflows(
return [];
}

export function getCodefulWorkflowNames(filePath: string): string[] {
const workflowNames: string[] = [];
export function getCodefulWorkflows(filePath: string): CodefulWorkflowData[] {
const workflows: CodefulWorkflowData[] = [];
const visitedFiles = new Set<string>();
const projectDir = dirname(filePath);

Expand All @@ -248,12 +248,22 @@ export function getCodefulWorkflowNames(filePath: string): string[] {

try {
const fileContent = readFileSync(currentFilePath, 'utf8');
const workflowRegex = /(?:CreateConversationalAgent|CreateAgentWorkflow|CreateStatefulWorkflow)\s*\(\s*["']([^"']+)["']/g;
const workflowRegex =
/(CreateConversationalAgent|CreateAgentWorkflow|CreateStatefulWorkflow|CreateStatelessWorkflow)\s*\(\s*["']([^"']+)["']/g;
let match: RegExpExecArray | null;
while ((match = workflowRegex.exec(fileContent)) !== null) {
const workflowName = match[1];
if (workflowName && !workflowNames.includes(workflowName)) {
workflowNames.push(workflowName);
const factoryMethod = match[1];
const workflowName = match[2];
if (workflowName && !workflows.some((workflow) => workflow.workflowName === workflowName)) {
workflows.push({
workflowName,
workflowKind:
factoryMethod === 'CreateStatelessWorkflow'
? 'Stateless'
: factoryMethod === 'CreateConversationalAgent' || factoryMethod === 'CreateAgentWorkflow'
? 'Agent'
: 'Stateful',
});
}
}

Expand All @@ -276,7 +286,11 @@ export function getCodefulWorkflowNames(filePath: string): string[] {
};

extractWorkflowsFromFile(filePath);
return workflowNames;
return workflows;
}

export function getCodefulWorkflowNames(filePath: string): string[] {
return getCodefulWorkflows(filePath).map((workflow) => workflow.workflowName);
}

export function getFallbackCodefulTriggerName(workflowContent: string, hasHttpTrigger: boolean): string | undefined {
Expand Down
22 changes: 22 additions & 0 deletions apps/vs-code-designer/src/app/utils/__test__/codeful.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
codefulProjectsExist,
detectAgentCodefulWorkflow,
detectCodefulWorkflow,
detectStatelessCodefulWorkflow,
hasCodefulSdkReference,
parseCsprojCopyToCodefulInfo,
} from '../codeful';
Expand Down Expand Up @@ -178,6 +179,27 @@ namespace TestProject
});
});

describe('detectStatelessCodefulWorkflow', () => {
it('detects a provider-based stateless workflow', () => {
const source = `
var trigger = WorkflowTriggers.BuiltIn.CreateHttpTrigger();
var workflow = trigger.Then(response);
return new[]
{
WorkflowFactory.CreateStatelessWorkflow(
"StatelessWorkflow",
workflow)
};
`;

expect(detectStatelessCodefulWorkflow(source)).toBe('StatelessWorkflow');
expect(detectCodefulWorkflow(source)).toEqual({
workflowName: 'StatelessWorkflow',
workflowType: 'stateless',
});
});
});

describe('codefulProjectsExist', () => {
const codefulSettingsJson = JSON.stringify({
IsEncrypted: false,
Expand Down
32 changes: 31 additions & 1 deletion apps/vs-code-designer/src/app/utils/codeful.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,29 @@ export const detectStatefulCodefulWorkflow = (fileContent: string): string | und
return undefined;
};

/**
* Detects if a C# file contains a CreateStatelessWorkflow call and extracts the workflow name.
* @param fileContent - The content of the C# file
* @returns The workflow name if detected, undefined otherwise
*/
export const detectStatelessCodefulWorkflow = (fileContent: string): string | undefined => {
Comment thread
andrew-eldridge marked this conversation as resolved.
Resolved
const pattern = /WorkflowFactory[\s\S]*?\.CreateStatelessWorkflow\s*\(\s*([^,)]+)/;
const match = fileContent.match(pattern);

if (match && match[1]) {
let workflowName = match[1].trim();
workflowName = workflowName.replace(/^["']|["']$/g, '');

if (workflowName.includes('<%=') || workflowName.includes('%>')) {
return undefined;
}

return workflowName;
}

return undefined;
};

/**
* Detects if a C# file contains a CreateConversationalAgent call and extracts the workflow name.
* @param fileContent - The content of the C# file
Expand Down Expand Up @@ -247,12 +270,19 @@ export const detectAgentCodefulWorkflow = (fileContent: string): string | undefi
* @param fileContent - The content of the C# file
* @returns An object with the workflow name and type if detected, undefined otherwise
*/
export const detectCodefulWorkflow = (fileContent: string): { workflowName: string; workflowType: 'stateful' | 'agent' } | undefined => {
export const detectCodefulWorkflow = (
fileContent: string
): { workflowName: string; workflowType: 'stateful' | 'stateless' | 'agent' } | undefined => {
const statefulWorkflowName = detectStatefulCodefulWorkflow(fileContent);
if (statefulWorkflowName) {
return { workflowName: statefulWorkflowName, workflowType: 'stateful' };
}

const statelessWorkflowName = detectStatelessCodefulWorkflow(fileContent);
if (statelessWorkflowName) {
return { workflowName: statelessWorkflowName, workflowType: 'stateless' };
}

const agentWorkflowName = detectAgentCodefulWorkflow(fileContent);
if (agentWorkflowName) {
return { workflowName: agentWorkflowName, workflowType: 'agent' };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace <%= logicAppNamespace %>
{
using Microsoft.Azure.Workflows.Sdk;
using Microsoft.Azure.Workflows.Sdk.Connectors.Msnweather;
using Microsoft.Azure.Workflows.Sdk.Connectors.Office365;
using Newtonsoft.Json;

/// <summary>
Expand Down Expand Up @@ -55,7 +56,7 @@ namespace <%= logicAppNamespace %>
{
return WorkflowActions.Managed.Msnweather("msnweather").CurrentWeather(
location: () => toolContext.Parameters.Location,
units: () => unitsInput.Imperial
units: () => UnitsInput.Imperial
);
},
description: "This tool gets the weather",
Expand All @@ -64,9 +65,10 @@ namespace <%= logicAppNamespace %>
agent.AddTool(toolContext =>
{
return WorkflowActions.Managed.Office365("outlook").SendEmail(
emailMessageto: () => toolContext.Parameters.Recipient,
emailMessagesubject: () => toolContext.Parameters.Subject,
emailMessagebody: () => toolContext.Parameters.Body);
emailMessage: () => new SendEmailEmailMessageInput(
toolContext.Parameters.Recipient,
toolContext.Parameters.Subject,
toolContext.Parameters.Body));
},
description: "This tool will send an email",
parameters: new EmailParameters());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace <%= logicAppNamespace %>
{
using Microsoft.Azure.Workflows.Sdk;
using Microsoft.Azure.Workflows.Sdk.Connectors.Msnweather;
using Microsoft.Azure.Workflows.Sdk.Connectors.Office365;
using Newtonsoft.Json;

/// <summary>
Expand Down Expand Up @@ -60,7 +61,7 @@ namespace <%= logicAppNamespace %>
{
return WorkflowActions.Managed.Msnweather("msnweather").CurrentWeather(
location: () => toolContext.Parameters.Location,
units: () => unitsInput.Imperial
units: () => UnitsInput.Imperial
);
},
description: "This tool gets the weather",
Expand All @@ -69,9 +70,10 @@ namespace <%= logicAppNamespace %>
agent.AddTool(toolContext =>
{
return WorkflowActions.Managed.Office365("outlook").SendEmail(
emailMessageto: () => toolContext.Parameters.Recipient,
emailMessagesubject: () => toolContext.Parameters.Subject,
emailMessagebody: () => toolContext.Parameters.Body);
emailMessage: () => new SendEmailEmailMessageInput(
toolContext.Parameters.Recipient,
toolContext.Parameters.Subject,
toolContext.Parameters.Body));
},
description: "This tool will send an email",
parameters: new EmailParameters());
Expand Down
Loading
Loading