Skip to content

Commit

Permalink
perf(ai-generator): generate api action for csharp (#12481)
Browse files Browse the repository at this point in the history
Co-authored-by: rentu <rentu>
  • Loading branch information
SLdragon authored Sep 30, 2024
1 parent 2beeded commit be7db5f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
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

0 comments on commit be7db5f

Please sign in to comment.