tsdoc/ts/classes.aidoc.ts

98 lines
3.3 KiB
TypeScript
Raw Permalink Normal View History

2024-04-03 11:34:26 +00:00
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;
2024-06-23 10:11:06 +00:00
public aidocInteract: plugins.smartinteract.SmartInteract;
2024-04-03 11:34:26 +00:00
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
2024-06-23 10:11:06 +00:00
this.aidocInteract = new plugins.smartinteract.SmartInteract();
2024-04-03 11:34:26 +00:00
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'],
});
2024-04-14 00:11:38 +00:00
const missingKeys = await this.npmextraKV.getMissingMandatoryKeys();
2024-04-03 11:34:26 +00:00
if (missingKeys.length > 0) {
// lets try argv
if (this.argvArg?.OPENAI_TOKEN) {
this.openaiToken = this.argvArg.OPENAI_TOKEN;
} else {
// lets try smartinteract
2024-04-13 22:47:49 +00:00
// wait for a second until OpenAI fixes punycode problem...
await plugins.smartdelay.delayFor(1000);
2024-06-23 10:11:06 +00:00
const answerObject = await this.aidocInteract.askQuestion({
2024-04-03 11:34:26 +00:00
type: 'input',
2024-06-23 10:11:06 +00:00
message: `Please provide your OpenAI token. This will be persisted in your home directory.`,
2024-04-03 11:34:26 +00:00
name: 'OPENAI_TOKEN',
default: '',
});
this.openaiToken = answerObject.value;
}
this.printSanitizedToken();
2024-04-13 22:52:04 +00:00
await this.npmextraKV.writeKey('OPENAI_TOKEN', this.openaiToken);
2024-04-03 11:34:26 +00:00
}
}
2024-04-14 00:23:56 +00:00
if (!this.openaiToken) {
this.openaiToken = await this.npmextraKV.readKey('OPENAI_TOKEN');
}
2024-04-03 11:34:26 +00:00
// lets assume we have an OPENAI_Token now
2024-05-17 15:38:35 +00:00
this.openaiInstance = new plugins.smartai.OpenAiProvider({
openaiToken: this.openaiToken,
});
2024-04-03 11:34:26 +00:00
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();
}
2024-06-22 11:11:22 +00:00
2024-06-22 19:21:52 +00:00
public async buildNextCommitObject(projectDirArg: string) {
const commitInstance = new aiDocsClasses.Commit(this, projectDirArg);
return await commitInstance.buildNextCommitObject();
}
2024-06-22 11:11:22 +00:00
public async getProjectContext(projectDirArg: string) {
const projectContextInstance = new aiDocsClasses.ProjectContext(projectDirArg);
return await projectContextInstance.gatherFiles();
}
2024-04-03 11:34:26 +00:00
}