Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(ai-generator): generate api action for csharp #12481

Merged
merged 1 commit into from
Sep 30, 2024
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
40 changes: 40 additions & 0 deletions packages/fx-core/src/component/generator/apiSpec/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1243,6 +1243,28 @@ async def {{operationId}}(
await context.send_activity(message)
return "success"
`,
cs: `
[Action("{{actionName}}")]
public async Task<string> {{actionName}}Async([ActionTurnContext] ITurnContext turnContext, [ActionTurnState] TurnState turnState, [ActionParameters] Dictionary<string, object> args)
{
try
{
RequestParams requestParam = ParseRequestParams(args);
var response = await Client.CallAsync("{{apiPath}}", Method.{{apiMethod}}, requestParam);
var data = response.Content;
var cardTemplatePath = "./adaptiveCards/{{operationId}}.json";
var message = RenderCardToMessage(cardTemplatePath, data);
await turnContext.SendActivityAsync(message);
}
catch (Exception ex) {
await turnContext.SendActivityAsync("Failed to call API with error: " + ex.Message);
}
return "complete";
}`,
};

const AuthCode = {
Expand Down Expand Up @@ -1309,6 +1331,24 @@ async function updateCodeForCustomApi(
.replace("{{OPENAPI_SPEC_PATH}}", openapiSpecFileName)
.replace("# Replace with action code", actionsCode.join("\n"));
await fs.writeFile(botFilePath, updateBotFileContent);
} else if (language === ProgrammingLanguage.CSharp) {
const actionsCode = [];
const codeTemplate = ActionCode["cs"];
for (const item of specItems) {
const code = codeTemplate
.replace(/{{operationId}}/g, item.item.operationId!)
.replace(/{{apiPath}}/g, item.pathUrl)
.replace(/{{apiMethod}}/g, Utils.updateFirstLetter(item.method))
.replace(/{{actionName}}/g, Utils.updateFirstLetter(item.item.operationId!));
actionsCode.push(code);
}

const apiActionCsFilePath = path.join(destinationPath, "APIActions.cs");
const apiActionCsFileContent = (await fs.readFile(apiActionCsFilePath)).toString();
const updateApiActionCsFileContent = apiActionCsFileContent
.replace("{{OPENAPI_SPEC_PATH}}", openapiSpecFileName)
.replace("// Replace with action code", actionsCode.join("\n"));
await fs.writeFile(apiActionCsFilePath, updateApiActionCsFileContent);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -818,9 +818,20 @@ describe("updateForCustomApi", async () => {

it("happy path: csharp", async () => {
sandbox.stub(fs, "ensureDir").resolves();
const mockWriteFile = sandbox.stub(fs, "writeFile").resolves();
sandbox.stub(fs, "writeFile").callsFake((file, data) => {
if (file == path.join("path", "APIActions.cs")) {
expect(data).to.contains(`[Action("GetHello")]`);
expect(data).to.contains(`public async Task<string> GetHelloAsync`);
expect(data).to.contains("openapi.yaml");
expect(data).not.to.contains("{{");
expect(data).not.to.contains("# Replace with action code");
}
});

sandbox
.stub(fs, "readFile")
.resolves(Buffer.from("test code // Replace with action code {{OPENAPI_SPEC_PATH}}"));
await CopilotPluginHelper.updateForCustomApi(spec, "csharp", "path", "openapi.yaml");
expect(mockWriteFile.notCalled).to.be.true;
});

it("happy path with spec without path", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Text.Json;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers;
using rentu_vs_ai_bot_test;
using RestSharp.Authenticators;

namespace OpenAPIClient
Expand Down
Loading