From 9997664aedd066759a901f79bfc58c87f885a0f8 Mon Sep 17 00:00:00 2001 From: amitjoshi Date: Tue, 12 Nov 2024 15:46:11 +0530 Subject: [PATCH] Hook for create site command with nl2page&site --- .../PowerPagesChatParticipantConstants.ts | 3 ++ .../commands/create-site/CreateSiteCommand.ts | 41 ++++++++++++++++ .../commands/create-site/CreateSiteHelper.ts | 49 +++++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 src/common/chat-participants/powerpages/commands/create-site/CreateSiteCommand.ts create mode 100644 src/common/chat-participants/powerpages/commands/create-site/CreateSiteHelper.ts diff --git a/src/common/chat-participants/powerpages/PowerPagesChatParticipantConstants.ts b/src/common/chat-participants/powerpages/PowerPagesChatParticipantConstants.ts index 2145a6e3..1faa46f6 100644 --- a/src/common/chat-participants/powerpages/PowerPagesChatParticipantConstants.ts +++ b/src/common/chat-participants/powerpages/PowerPagesChatParticipantConstants.ts @@ -53,3 +53,6 @@ export const NL2SITE_SCENARIO = 'NL2Site'; export const NL2PAGE_GENERATE_NEW_PAGE = 'GenerateNewPage'; export const NL2SITE_GENERATE_NEW_SITE = 'GenerateNewSite'; export const NL2PAGE_SCOPE = 'Page'; +export const NL2SITE_REQUEST_FAILED = vscode.l10n.t('Failed to get site content from NL2Site service'); +export const NL2PAGE_GENERATING_WEBPAGES = vscode.l10n.t("Generating webpages..."); +export const NL2PAGE_RESPONSE_FAILED = 'Failed to get page content from NL2Page service'; diff --git a/src/common/chat-participants/powerpages/commands/create-site/CreateSiteCommand.ts b/src/common/chat-participants/powerpages/commands/create-site/CreateSiteCommand.ts new file mode 100644 index 00000000..aa871c9a --- /dev/null +++ b/src/common/chat-participants/powerpages/commands/create-site/CreateSiteCommand.ts @@ -0,0 +1,41 @@ +/* + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + */ + +import { Command } from "../../../CommandRegistry"; +import * as vscode from 'vscode'; +import { createSite } from "./CreateSiteHelper"; + +export class CreateSiteCommand implements Command { + async execute(request: any, stream: vscode.ChatResponseStream): Promise { + const { prompt, intelligenceAPIEndpointInfo, intelligenceApiToken, powerPagesAgentSessionId, telemetry } = request; + + stream.progress('Generating a new Power Pages site...'); + try { + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const result = await createSite( + intelligenceAPIEndpointInfo.intelligenceEndpoint, + intelligenceApiToken, + prompt, + powerPagesAgentSessionId, + stream, + telemetry + ); + // Process the result + + return { + metadata: { + command: 'create-site', + } + }; + } catch (error) { + //TODO: Handle error + return { + metadata: { + command: '' + } + }; + } + } +} diff --git a/src/common/chat-participants/powerpages/commands/create-site/CreateSiteHelper.ts b/src/common/chat-participants/powerpages/commands/create-site/CreateSiteHelper.ts new file mode 100644 index 00000000..cb6f362b --- /dev/null +++ b/src/common/chat-participants/powerpages/commands/create-site/CreateSiteHelper.ts @@ -0,0 +1,49 @@ +/* + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + */ + +import * as vscode from 'vscode'; +import { ITelemetry } from '../../../../OneDSLoggerTelemetry/telemetry/ITelemetry'; +import { getNL2PageData } from './Nl2PageService'; +import { getNL2SiteData } from './Nl2SiteService'; +import { NL2SITE_REQUEST_FAILED, NL2PAGE_GENERATING_WEBPAGES, NL2PAGE_RESPONSE_FAILED } from '../../PowerPagesChatParticipantConstants'; + +export const createSite = async (intelligenceEndpoint: string, intelligenceApiToken: string, userPrompt: string, sessionId: string, stream: vscode.ChatResponseStream, telemetry: ITelemetry) => { + try { + const { siteName, siteDescription } = await fetchSiteAndPageData(intelligenceEndpoint, intelligenceApiToken, userPrompt, sessionId, telemetry, stream); + + return { + siteName, + //websiteId, + siteDescription, + }; + + } catch (error) { + stream.markdown(`Error: ${(error as Error).message}`); + throw error; + } +}; + +async function fetchSiteAndPageData(intelligenceEndpoint: string, intelligenceApiToken: string, userPrompt: string, sessionId: string, telemetry: ITelemetry, stream: vscode.ChatResponseStream) { + // Call NL2Site service to get initial site content + const { siteName, pages, siteDescription } = await getNL2SiteData(intelligenceEndpoint, intelligenceApiToken, userPrompt, sessionId, telemetry); + + if (!siteName) { + stream.markdown(NL2SITE_REQUEST_FAILED); + throw new Error(NL2SITE_REQUEST_FAILED); + } + + const sitePagesList = pages.map((page: { pageName: string; }) => page.pageName); + + stream.progress(NL2PAGE_GENERATING_WEBPAGES); + + // Call NL2Page service to get page content + const sitePages = await getNL2PageData(intelligenceEndpoint, intelligenceApiToken, userPrompt, siteName, sitePagesList, sessionId, telemetry); + + if (!sitePages) { + throw new Error(NL2PAGE_RESPONSE_FAILED); + } + + return { siteName, sitePagesList, sitePages, siteDescription }; +}