tsdoc/ts/classes.aidoc.ts
2024-04-03 13:34:26 +02:00

81 lines
2.7 KiB
TypeScript

import * as plugins from './plugins.js';
import * as aiDocsClasses from './aidocs_classes/index.js';
export class AiDoc {
private openaiToken: string;
public npmextraKV: plugins.npmextra.KeyValueStore;
public qenvInstance: plugins.qenv.Qenv;
public smartinteractInstance: plugins.smartinteract.SmartInteract;
public openaiInstance: plugins.smartai.OpenAiProvider;
argvArg: any;
constructor(argvArg?: any) {
this.argvArg = argvArg;
}
private printSanitizedToken() {
// Check if the token length is greater than the sum of startLength and endLength
let printToken: string;
if (this.openaiToken.length > 6) {
// Extract the beginning and end parts of the token
const start = this.openaiToken.substring(0, 3);
const end = this.openaiToken.substring(this.openaiToken.length - 3);
printToken = `${start}...${end}`;
} else {
// If the token is not long enough, return it as is
printToken = this.openaiToken;
}
console.log(`OpenAI Token on record: ${printToken}`);
}
public async start() {
// lets care about prerequisites
this.smartinteractInstance = new plugins.smartinteract.SmartInteract();
this.qenvInstance = new plugins.qenv.Qenv();
if (!(await this.qenvInstance.getEnvVarOnDemand('OPENAI_TOKEN'))) {
this.npmextraKV = new plugins.npmextra.KeyValueStore({
typeArg: 'userHomeDir',
identityArg: 'tsdoc',
mandatoryKeys: ['OPENAI_TOKEN'],
});
const missingKeys = this.npmextraKV.getMissingMandatoryKeys();
if (missingKeys.length > 0) {
// lets try argv
if (this.argvArg?.OPENAI_TOKEN) {
this.openaiToken = this.argvArg.OPENAI_TOKEN;
} else {
// lets try smartinteract
const answerObject = await this.smartinteractInstance.askQuestion({
type: 'input',
message: `Please provide your OpenAI token`,
name: 'OPENAI_TOKEN',
default: '',
});
this.openaiToken = answerObject.value;
}
this.printSanitizedToken();
// await this.npmextraKV.writeKey('OPENAI_TOKEN', this.openaiToken);
}
}
// lets assume we have an OPENAI_Token now
this.openaiInstance = new plugins.smartai.OpenAiProvider(this.openaiToken);
await this.openaiInstance.start();
}
public async buildReadme(projectDirArg: string) {
const readmeInstance = new aiDocsClasses.Readme(this, projectDirArg);
return await readmeInstance.build();
}
public async buildDescription(projectDirArg: string) {
const descriptionInstance = new aiDocsClasses.Description(this, projectDirArg);
return await descriptionInstance.build();
}
}