Skip to content

Commit

Permalink
feat: use new llm service
Browse files Browse the repository at this point in the history
  • Loading branch information
CristiCanizales committed Jan 10, 2025
1 parent 3b9c37a commit 8fcd05a
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 33 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/salesforcedx-utils-vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"@salesforce/core-bundle": "8.6.1",
"@salesforce/source-deploy-retrieve-bundle": "12.7.4",
"@salesforce/source-tracking-bundle": "7.1.17",
"@salesforce/vscode-service-provider": "1.3.0-rc.3",
"@salesforce/vscode-service-provider": "1.3.0-rc.6.3",
"applicationinsights": "1.0.7",
"cross-spawn": "7.0.6",
"rxjs": "^5.4.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/salesforcedx-vscode-apex/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"@salesforce/apex-tmlanguage": "1.8.0",
"@salesforce/core-bundle": "8.6.1",
"@salesforce/salesforcedx-utils-vscode": "62.13.0",
"@salesforce/vscode-service-provider": "1.3.0-rc.3",
"@salesforce/vscode-service-provider": "1.3.0-rc.6.3",
"expand-home-dir": "0.0.3",
"find-java-home": "0.2.0",
"shelljs": "0.8.5",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import * as fs from 'fs';
import * as path from 'path';
import { URL } from 'url';
import * as vscode from 'vscode';
import { parse, stringify } from 'yaml';
import { workspaceContext } from '../context';
import { nls } from '../messages';
import { ApexClassOASEligibleResponse, ApexClassOASGatherContextResponse } from '../openApiUtilities/schemas';
Expand Down Expand Up @@ -115,12 +114,16 @@ export class ApexActionController {
if (openApiIndex === -1) {
throw new Error('Could not find openapi line in document:\n' + doc);
}
const theDoc = doc
// const theDoc = doc
// .substring(openApiIndex)
// .split('\n')
// .filter(line => !/^```$/.test(line))
// .join('\n');
return doc
.substring(openApiIndex)
.split('\n')
.filter((line: string) => !line.includes('{AUTHOR_PLACEHOLDER}'))
.filter(line => !/^```$/.test(line))
.join('\n');
return stringify(parse(theDoc));
}

private saveOasAsErsMetadata = async (oasSpec: string, fullPath: string): Promise<void> => {
Expand Down Expand Up @@ -239,7 +242,7 @@ export class ApexActionController {
oasSpec: string
) => {
const baseName = path.basename(fullPath).split('.')[0];
const safeOasSpec = oasSpec.replaceAll('"', '&apos;');
const safeOasSpec = oasSpec.replaceAll('"', '&apos;').replaceAll('type: Id', 'type: string');

const parser = new XMLParser({ ignoreAttributes: false });
let jsonObj;
Expand All @@ -255,6 +258,7 @@ export class ApexActionController {
} else {
// Create a new XML structure
jsonObj = {
'?xml': { '@_version': '1.0', '@_encoding': 'UTF-8' },
ExternalServiceRegistration: {
'@_xmlns': 'http://soap.sforce.com/2006/04/metadata',
description: `${baseName} External Service`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { AiApiClient, CommandSource, ServiceProvider, ServiceType } from '@salesforce/vscode-service-provider';
import { LLMServiceInterface, ServiceProvider, ServiceType } from '@salesforce/vscode-service-provider';
import * as path from 'path';
import * as vscode from 'vscode';
import { languageClientUtils } from '../languageUtils';
Expand Down Expand Up @@ -198,28 +198,16 @@ export class MetadataOrchestrator {
const input =
`${systemTag}\n${systemPrompt}\n\n${endOfPromptTag}\n${userTag}\n` +
userPrompt +
'\n\n***Code Context***\n```\n' +
'\nThis is the Apex class the OpenAPI v3 specification should be generated for:\n```\n' +
editorText +
`\nClass name: ${context.classDetail.name}, methods: ${context.methods.map(method => method.name).join(', ')}\n` +
`\n\`\`\`\n${endOfPromptTag}\n${assistantTag}`;

console.log('input = ' + input);
let result;
let documentContents = '';
let tries = 0;
try {
const apiClient = await this.getAiApiClient();
while (!documentContents.startsWith('yaml') && tries < 10) {
result = await apiClient.naturalLanguageQuery({
prefix: '',
suffix: '',
input,
commandSource: CommandSource.NLtoCodeGen,
promptId: 'generateOpenAPIv3Specifications'
});
documentContents = result[0].completion;
if (documentContents.includes('try again')) tries++;
}
if (tries === 10) throw new Error(documentContents);
const llmService = await this.getLLMServiceInterface();
documentContents = await llmService.callLLM(input);
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
throw new Error(errorMessage);
Expand All @@ -228,7 +216,7 @@ export class MetadataOrchestrator {
return documentContents;
};

getAiApiClient = async (): Promise<AiApiClient> => {
return ServiceProvider.getService(ServiceType.AiApiClient);
getLLMServiceInterface = async (): Promise<LLMServiceInterface> => {
return ServiceProvider.getService(ServiceType.LLMService, 'salesforcedx-vscode');
};
}
2 changes: 1 addition & 1 deletion packages/salesforcedx-vscode-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"@salesforce/source-deploy-retrieve-bundle": "12.7.4",
"@salesforce/templates": "62.0.1",
"@salesforce/ts-types": "2.0.12",
"@salesforce/vscode-service-provider": "1.3.0-rc.3",
"@salesforce/vscode-service-provider": "1.3.0-rc.6.3",
"adm-zip": "0.5.10",
"applicationinsights": "1.0.7",
"glob": "^7.1.2",
Expand Down

0 comments on commit 8fcd05a

Please sign in to comment.