@autobe/agent
Version:
AI backend server code generator
207 lines • 9.07 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
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) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AutoBeAgent = void 0;
const core_1 = require("@agentica/core");
const tstl_1 = require("tstl");
const uuid_1 = require("uuid");
const createAgenticaHistory_1 = require("./factory/createAgenticaHistory");
const createAutoBeApplication_1 = require("./factory/createAutoBeApplication");
const createAutoBeState_1 = require("./factory/createAutoBeState");
const transformFacadeStateMessage_1 = require("./orchestrate/facade/transformFacadeStateMessage");
const emplaceMap_1 = require("./utils/emplaceMap");
class AutoBeAgent {
/* -----------------------------------------------------------
CONSTRUCTOR
----------------------------------------------------------- */
/**
* Initializer constructor.
*
* @param props Properties to construct the agent
*/
constructor(props) {
var _a, _b, _c, _d;
this.props = props;
this.histories_ = (_b = (_a = props.histories) === null || _a === void 0 ? void 0 : _a.slice()) !== null && _b !== void 0 ? _b : [];
this.state_ = (0, createAutoBeState_1.createAutoBeState)(this.histories_);
this.context_ = {
model: props.model,
vendor: props.vendor,
config: props.config,
compiler: props.compiler,
histories: () => this.histories_,
state: () => this.state_,
usage: () => this.agentica_.getTokenUsage(),
files: () => this.getFiles(),
dispatch: (event) => {
this.dispatch(event).catch(() => { });
},
};
this.listeners_ = new Map();
this.agentica_ = new core_1.MicroAgentica({
model: props.model,
vendor: Object.assign(Object.assign({}, props.vendor), { semaphore: new tstl_1.Semaphore((_c = props.vendor.semaphore) !== null && _c !== void 0 ? _c : 16) }),
config: Object.assign(Object.assign({}, ((_d = props.config) !== null && _d !== void 0 ? _d : {})), { executor: {
describe: null,
}, systemPrompt: {
execute: () => (0, transformFacadeStateMessage_1.transformFacadeStateMessage)(this.state_),
} }),
controllers: [
(0, createAutoBeApplication_1.createAutoBeController)({
model: props.model,
context: this.context_,
}),
],
});
this.agentica_.getHistories().push(...this.histories_
.map((history) => (0, createAgenticaHistory_1.createAgenticaHistory)({
operations: this.agentica_.getOperations(),
history,
}))
.filter((h) => h !== null));
this.agentica_.on("assistantMessage", (message) => __awaiter(this, void 0, void 0, function* () {
const start = new Date();
const history = {
id: (0, uuid_1.v4)(),
type: "assistantMessage",
text: yield message.join(),
created_at: start.toISOString(),
completed_at: new Date().toISOString(),
};
this.histories_.push(history);
this.dispatch({
type: "assistantMessage",
text: history.text,
created_at: history.created_at,
}).catch(() => { });
}));
this.agentica_.on("request", (e) => {
if (e.body.parallel_tool_calls !== undefined)
delete e.body.parallel_tool_calls;
});
}
/** @internal */
clone() {
return new AutoBeAgent(Object.assign(Object.assign({}, this.props), { histories: this.histories_.slice() }));
}
on(type, listener) {
(0, emplaceMap_1.emplaceMap)(this.listeners_, type, () => new Set()).add(listener);
return this;
}
off(type, listener) {
const set = this.listeners_.get(type);
if (set === undefined)
return this;
set.delete(listener);
if (set.size === 0)
this.listeners_.delete(type);
return this;
}
/* -----------------------------------------------------------
ACCESSORS
----------------------------------------------------------- */
conversate(content) {
return __awaiter(this, void 0, void 0, function* () {
const index = this.histories_.length;
const userMessageHistory = {
id: (0, uuid_1.v4)(),
type: "userMessage",
contents: typeof content === "string"
? [
{
type: "text",
text: content,
},
]
: Array.isArray(content)
? content
: [content],
created_at: new Date().toISOString(),
};
this.histories_.push(userMessageHistory);
this.dispatch(userMessageHistory).catch(() => { });
yield this.agentica_.conversate(content);
return this.histories_.slice(index);
});
}
getFiles() {
var _a, _b, _c;
const files = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, Object.fromEntries(this.state_.analyze
? Object.entries(this.state_.analyze.files).map(([key, value]) => [
`docs/analysis/${key.split("/").at(-1)}`,
value,
])
: [])), Object.fromEntries(((_a = this.state_.prisma) === null || _a === void 0 ? void 0 : _a.result.success) === true
? [
...Object.entries(this.state_.prisma.schemas).map(([key, value]) => [
`prisma/schema/${key.split("/").at(-1)}`,
value,
]),
...(this.state_.prisma.compiled.type === "success"
? [["docs/ERD.md", this.state_.prisma.compiled.document]]
: []),
...(this.state_.prisma.compiled.type === "failure"
? [
[
"prisma/compile-error-reason.log",
this.state_.prisma.compiled.reason,
],
]
: []),
[
"autobe/prisma.json",
JSON.stringify(this.state_.prisma.result.data, null, 2),
],
]
: [])), (this.state_.interface ? this.state_.interface.files : {})), (((_b = this.state_.test) === null || _b === void 0 ? void 0 : _b.compiled.type) === "success"
? this.state_.test.files
: {})), (((_c = this.state_.realize) === null || _c === void 0 ? void 0 : _c.compiled.type) === "success"
? this.state_.realize.files
: {})), { "autobe/histories.json": JSON.stringify(this.histories_, null, 2), "autobe/tokenUsage.json": JSON.stringify(this.getTokenUsage(), null, 2) }), (this.state_.interface
? {
"autobe/document.json": JSON.stringify(this.state_.interface.document, null, 2),
}
: {}));
return Object.fromEntries(Object.entries(files).map(([k, v]) => [
k.startsWith("/") ? k.substring(1) : k,
v,
]));
}
getHistories() {
return this.histories_;
}
getTokenUsage() {
return this.agentica_.getTokenUsage();
}
/* -----------------------------------------------------------
CONTEXTS
----------------------------------------------------------- */
/** @internal */
getContext() {
return this.context_;
}
/** @internal */
dispatch(event) {
return __awaiter(this, void 0, void 0, function* () {
const set = this.listeners_.get(event.type);
if (set === undefined)
return;
yield Promise.all(Array.from(set).map((listener) => __awaiter(this, void 0, void 0, function* () {
try {
yield listener(event);
}
catch (_a) { }
})));
});
}
}
exports.AutoBeAgent = AutoBeAgent;
//# sourceMappingURL=AutoBeAgent.js.map