@visactor/vmind
Version:
<div align="center"> <a href="https://github.com/VisActor#gh-light-mode-only" target="_blank"> <img alt="VisActor Logo" width="200" src="https://github.com/VisActor/.github/blob/main/profile/logo_500_200_light.svg"/> </a> <a href="https://githu
112 lines (108 loc) • 4.38 kB
JavaScript
var __awaiter = this && this.__awaiter || function(thisArg, _arguments, P, generator) {
return new (P || (P = Promise))((function(resolve, reject) {
function fulfilled(value) {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
}
function rejected(value) {
try {
step(generator.throw(value));
} catch (e) {
reject(e);
}
}
function step(result) {
var value;
result.done ? resolve(result.value) : (value = result.value, value instanceof P ? value : new P((function(resolve) {
resolve(value);
}))).then(fulfilled, rejected);
}
step((generator = generator.apply(thisArg, _arguments || [])).next());
}));
};
import { merge } from "@visactor/vutils";
import { AtomName } from "../types/atom";
import { Factory } from "../core/factory";
export class Schedule {
constructor(atomList, options, context) {
this.atomList = atomList, this.options = options || {}, this.query = "", this.atomInstaces = atomList.map((atomName => this.atomFactory(atomName))),
this.setNewTask(context);
}
initContext() {
this.context = {}, this.atomInstaces.forEach((atom => {
this.context = atom.buildDefaultContext(this.context), atom.reset();
}));
}
getAtomOptions(atomName) {
return merge({}, this.options[AtomName.BASE], this.options[atomName]);
}
atomFactory(atomName) {
const options = this.getAtomOptions(atomName);
return Factory.createAtom(atomName, this.context, options);
}
parseSubTasks(query) {
let taskMapping = {};
return this.atomList.forEach((name => {
taskMapping = Object.assign(Object.assign({}, taskMapping), {
[name]: {
shouldRun: !0,
query: query
}
});
})), taskMapping;
}
addUsage(oldUsage, newUsage) {
const result = {};
if (!newUsage) return oldUsage;
for (const key in oldUsage) if (Object.prototype.hasOwnProperty.call(oldUsage, key)) {
const curKey = key;
result[curKey] = (oldUsage[curKey] || 0) + ((null == newUsage ? void 0 : newUsage[curKey]) || 0);
}
return result;
}
run(query, shouldRunList) {
var _a;
return __awaiter(this, void 0, void 0, (function*() {
this.query = query || "";
const subTasks = this.parseSubTasks(query);
let usage = {
prompt_tokens: 0,
completion_tokens: 0,
total_tokens: 0
};
for (const atom of this.atomInstaces) {
const {shouldRun: shouldRun, query: taskQuery} = (null == subTasks ? void 0 : subTasks[atom.name]) || {};
!1 !== (null == shouldRunList ? void 0 : shouldRunList[atom.name]) && (shouldRun || atom.shouldRunByContextUpdate(this.context)) && (this.context = yield atom.run({
context: this.context,
query: taskQuery
}), usage = this.addUsage(usage, null === (_a = atom.getContext()) || void 0 === _a ? void 0 : _a.usage));
}
return this.context = Object.assign(Object.assign({}, this.context), {
usage: usage
}), this.context;
}));
}
setNewTask(context) {
this.initContext(), this.updateContext(context), this.atomInstaces.forEach((atom => {
atom.reset(this.context), atom.clearHistory();
}));
}
updateOptions(options) {
this.options = merge({}, this.options, options), this.atomInstaces.forEach((atom => atom.updateOptions(this.getAtomOptions(atom.name))));
}
updateContext(context, isReplace = !1) {
this.context = isReplace ? context : merge({}, this.context, context);
}
getContext(atomName) {
if (atomName) {
const atomInstaces = this.atomInstaces.find((atom => atom.name === atomName));
return atomInstaces ? atomInstaces.getContext() : (console.error(`Doesn't exist ${atomName}`),
null);
}
return this.context;
}
}
//# sourceMappingURL=index.js.map