From d5a6cb3348e63d52e10540e99cf509be64a26649 Mon Sep 17 00:00:00 2001 From: Steve Hetzel Date: Sat, 9 Nov 2024 15:21:01 -0700 Subject: [PATCH] fix: improve types and linting --- README.md | 4 ---- src/agent.ts | 33 +++++++++++++++++----------- src/index.ts | 7 +++--- src/types.ts | 38 +++++++++++++++++++++------------ test/agentJobSpecCreate.test.ts | 3 +-- 5 files changed, 49 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 12a1b72..66f180d 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,3 @@ A TypeScript library for working with Salesforce Agents. ## Usage TBD - -## Contributing - -If you are interested in contributing, please take a look at the [CONTRIBUTING](CONTRIBUTING.md) guide. \ No newline at end of file diff --git a/src/agent.ts b/src/agent.ts index b9e2cef..33c012b 100644 --- a/src/agent.ts +++ b/src/agent.ts @@ -9,14 +9,16 @@ import { join } from 'node:path'; import { readFileSync, statSync } from 'node:fs'; import { inspect } from 'node:util'; import { Connection, Logger, SfError, SfProject } from '@salesforce/core'; -import { getMockDir } from './mockDir.js'; +import { Duration, sleep } from '@salesforce/kit'; +import { getMockDir } from './mockDir'; import { type SfAgent, type AgentCreateConfig, + type AgentCreateResponse, type AgentJobSpec, type AgentJobSpecCreateConfig, - type AgentJobSpecCreateResponse -} from './types.js' + type AgentJobSpecCreateResponse, +} from './types.js'; export class Agent implements SfAgent { private logger: Logger; @@ -27,18 +29,21 @@ export class Agent implements SfAgent { this.mockDir = getMockDir(); } - public async create(config: AgentCreateConfig): Promise { + public async create(config: AgentCreateConfig): Promise { this.logger.debug(`Creating Agent using config: ${inspect(config)} in project: ${this.project.getPath()}`); // Generate a GenAiPlanner in the local project and deploy // make API request to /services/data/{api-version}/connect/attach-agent-topics + await sleep(Duration.seconds(3)); // on success, retrieve all Agent metadata + + return { isSuccess: true }; } /** * Create an agent spec from provided data. - * + * * @param config The configuration used to generate an agent spec. */ public async createSpec(config: AgentJobSpecCreateConfig): Promise { @@ -52,7 +57,7 @@ export class Agent implements SfAgent { try { this.logger.debug(`Using mock directory: ${this.mockDir} for agent job spec creation`); statSync(specFilePath); - } catch(err) { + } catch (err) { throw SfError.create({ name: 'MissingMockFile', message: `SF_MOCK_DIR [${this.mockDir}] must contain a spec file with name ${specFileName}`, @@ -62,22 +67,22 @@ export class Agent implements SfAgent { try { this.logger.debug(`Returning mock agent spec file: ${specFilePath}`); agentSpec = JSON.parse(readFileSync(specFilePath, 'utf8')) as AgentJobSpec; - } catch(err) { + } catch (err) { throw SfError.create({ name: 'InvalidMockFile', message: `SF_MOCK_DIR [${this.mockDir}] must contain a valid spec file with name ${specFileName}`, cause: err, actions: [ 'Check that the file is readable', - 'Check that the file is a valid JSON array of jobTitle and jobDescription objects' - ] + 'Check that the file is a valid JSON array of jobTitle and jobDescription objects', + ], }); } } else { // TODO: We'll probably want to wrap this for better error handling but let's see // what it looks like first. const response = await this.connection.requestGet(this.buildAgentJobSpecUrl(config), { - retry: { maxRetries: 3 } + retry: { maxRetries: 3 }, }); if (response.isSuccess) { agentSpec = response?.jobSpecs as AgentJobSpec; @@ -92,14 +97,16 @@ export class Agent implements SfAgent { return agentSpec; } + // eslint-disable-next-line class-methods-use-this private verifyAgentSpecConfig(config: AgentJobSpecCreateConfig): void { // TBD: for now just return. At some point verify all required config values. if (config) return; } + // eslint-disable-next-line class-methods-use-this private buildAgentJobSpecUrl(config: AgentJobSpecCreateConfig): string { const { type, role, companyName, companyDescription, companyWebsite } = config; - const website = companyWebsite ? `&${companyWebsite}` : ''; - return `/connect/agent-job-spec?${type}&${role}&${companyName}&${companyDescription}${website}`; + const website = companyWebsite ? `&companyWebsite=${companyWebsite}` : ''; + return `/connect/agent-job-spec?agentType=${type}&role=${role}&companyName=${companyName}&companyDescription=${companyDescription}${website}`; } -} \ No newline at end of file +} diff --git a/src/index.ts b/src/index.ts index e8a561e..512822b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,9 +6,10 @@ */ export { - AgentCreateConfig as AgentConfig, + AgentCreateConfig, + AgentCreateResponse, AgentJobSpec, - AgentJobSpecCreateConfig as AgentJobSpecConfig, - AgentJobSpecCreateResponse as AgentJobSpecResponse, + AgentJobSpecCreateConfig, + AgentJobSpecCreateResponse, SfAgent, } from './types'; diff --git a/src/types.ts b/src/types.ts index ce189cc..32214e4 100644 --- a/src/types.ts +++ b/src/types.ts @@ -6,16 +6,18 @@ */ /** - * An agent spec is a list of job titles and descriptions + * An agent job spec is a list of job titles and descriptions * to be performed by the agent. */ -export type AgentJobSpec = [{ - jobTitle: string; - jobDescription: string; -}]; +export type AgentJobSpec = [ + { + jobTitle: string; + jobDescription: string; + } +]; /** - * The parameters needed to generate an agent spec. + * The parameters used to generate an agent spec. */ export type AgentJobSpecCreateConfig = { name: string; @@ -24,24 +26,24 @@ export type AgentJobSpecCreateConfig = { companyName: string; companyDescription: string; companyWebsite?: string; -} +}; /** - * The parameters needed to generate an agent in an org. - * + * The parameters used to generate an agent in an org. + * * NOTE: This is likely to change with planned serverside APIs. */ export type AgentCreateConfig = AgentJobSpecCreateConfig & { - spec: AgentJobSpec; -} + jobSpecs: AgentJobSpec; +}; /** * An interface for working with Agents. */ export type SfAgent = { - create(config: AgentCreateConfig): Promise; + create(config: AgentCreateConfig): Promise; createSpec(config: AgentJobSpecCreateConfig): Promise; -} +}; /** * The response from the `agent-job-spec` API. @@ -50,4 +52,12 @@ export type AgentJobSpecCreateResponse = { isSuccess: boolean; errorMessage?: string; jobSpecs?: AgentJobSpec; -} \ No newline at end of file +}; + +/** + * The response from the `attach-agent-topics` API. + */ +export type AgentCreateResponse = { + isSuccess: boolean; + errorMessage?: string; +}; diff --git a/test/agentJobSpecCreate.test.ts b/test/agentJobSpecCreate.test.ts index f2e0907..8c242a3 100644 --- a/test/agentJobSpecCreate.test.ts +++ b/test/agentJobSpecCreate.test.ts @@ -7,10 +7,9 @@ import { expect } from 'chai'; import { MockTestOrgData, TestContext } from '@salesforce/core/testSetup'; import { SfProject } from '@salesforce/core'; -import { Agent } from '../src/agent.js'; +import { Agent } from '../src/agent'; describe('agent job spec create test', () => { - const $$ = new TestContext(); const testOrg = new MockTestOrgData(); $$.inProject(true);