Skip to content

Commit

Permalink
fix: improve types and linting
Browse files Browse the repository at this point in the history
  • Loading branch information
shetzel committed Nov 9, 2024
1 parent 0c5d8d6 commit d5a6cb3
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 36 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
33 changes: 20 additions & 13 deletions src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,18 +29,21 @@ export class Agent implements SfAgent {
this.mockDir = getMockDir();
}

public async create(config: AgentCreateConfig): Promise<void> {
public async create(config: AgentCreateConfig): Promise<AgentCreateResponse> {
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<AgentJobSpec> {
Expand All @@ -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}`,
Expand All @@ -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<AgentJobSpecCreateResponse>(this.buildAgentJobSpecUrl(config), {
retry: { maxRetries: 3 }
retry: { maxRetries: 3 },
});
if (response.isSuccess) {
agentSpec = response?.jobSpecs as AgentJobSpec;
Expand All @@ -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}`;
}
}
}
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
*/

export {
AgentCreateConfig as AgentConfig,
AgentCreateConfig,
AgentCreateResponse,
AgentJobSpec,
AgentJobSpecCreateConfig as AgentJobSpecConfig,
AgentJobSpecCreateResponse as AgentJobSpecResponse,
AgentJobSpecCreateConfig,
AgentJobSpecCreateResponse,
SfAgent,
} from './types';
38 changes: 24 additions & 14 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<void>;
create(config: AgentCreateConfig): Promise<AgentCreateResponse>;
createSpec(config: AgentJobSpecCreateConfig): Promise<AgentJobSpec>;
}
};

/**
* The response from the `agent-job-spec` API.
Expand All @@ -50,4 +52,12 @@ export type AgentJobSpecCreateResponse = {
isSuccess: boolean;
errorMessage?: string;
jobSpecs?: AgentJobSpec;
}
};

/**
* The response from the `attach-agent-topics` API.
*/
export type AgentCreateResponse = {
isSuccess: boolean;
errorMessage?: string;
};
3 changes: 1 addition & 2 deletions test/agentJobSpecCreate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit d5a6cb3

Please sign in to comment.