agentlang
Version:
The easiest way to build the most reliable AI agents - enterprise-grade teams of AI agents that collaborate with each other and humans
330 lines • 9.07 kB
JavaScript
import { Instance, isAgentEventInstance } from './module.js';
import { disableInternalMonitoring, enableInternalMonitoring } from './state.js';
export class MonitorEntry {
constructor(statement) {
this.result = undefined;
this.error = undefined;
this.llm = false;
this.planner = false;
this.flowStep = false;
this.flow = false;
this.decision = false;
this.input = statement;
this.timestamp = Date.now();
this.latencyMs = -1;
}
getStatement() {
return this.input;
}
setResult(result) {
if (this.result === undefined) {
this.result = result;
this.error = undefined;
}
return this;
}
getResult() {
return this.result;
}
setError(error) {
this.error = error;
this.result = undefined;
return this;
}
getError() {
return this.error;
}
setLatencyMs(ms) {
this.latencyMs = ms;
return this;
}
flagAsLlm() {
this.llm = true;
return this;
}
setLlmPrompt(s) {
this.llm = true;
if (this.llmPrompt === undefined)
this.llmPrompt = s;
return this;
}
setLlmResponse(s) {
if (this.llmResponse === undefined)
this.llmResponse = s;
return this;
}
flagAsPlanner() {
this.llm = true;
if (this.flowStep || this.flow || this.decision) {
this.planner = false;
}
else {
this.planner = true;
}
return this;
}
flagAsFlowStep() {
this.llm = true;
if (this.flow) {
return this;
}
this.planner = false;
this.flowStep = true;
return this;
}
flagAsFlow() {
this.llm = true;
this.planner = false;
this.flowStep = false;
this.flow = true;
return this;
}
flagAsDecision() {
this.llm = true;
this.planner = false;
this.decision = true;
return this;
}
static resultAsObject(result) {
if (Instance.IsInstance(result))
return result.asSerializableObject();
else if (result instanceof Array)
return result.map((v) => {
return MonitorEntry.resultAsObject(v);
});
else
return result;
}
asObject() {
const obj = {
input: this.input,
timestamp: this.timestamp,
};
if (this.latencyMs >= 0) {
obj.latencyMs = this.latencyMs;
}
if (this.error !== undefined) {
obj.error = this.error;
}
else if (this.result !== undefined) {
obj.finalResult = MonitorEntry.resultAsObject(this.result);
}
if (this.llm === true) {
const llmObj = {};
if (this.llmPrompt !== undefined) {
llmObj.prompt = this.llmPrompt;
}
if (this.llmResponse !== undefined) {
llmObj.response = this.llmResponse;
}
llmObj.isPlanner = this.planner;
llmObj.isFlowStep = this.flowStep;
llmObj.isDecision = this.decision;
llmObj.isFlow = this.flow;
obj.llm = llmObj;
}
obj.label = this.input;
return obj;
}
}
let MonitoringCallback = undefined;
export function setMonitoringCallback(f) {
MonitoringCallback = f;
enableInternalMonitoring();
}
export function resetMonitoringCallback() {
MonitoringCallback = undefined;
disableInternalMonitoring();
}
export class Monitor {
constructor(eventInstance, user) {
this.entries = new Array();
this.parent = undefined;
this.lastEntry = undefined;
this.lastEntrySetAtMs = 0;
this.totalLatency = 0;
this.flowResult = undefined;
this.eventInstance = eventInstance;
this.id = eventInstance ? eventInstance.getId() : crypto.randomUUID();
this.user = user;
this.timestamp = Date.now();
while (monitorRegistry.length >= Monitor.MAX_REGISTRY_SIZE) {
monitorRegistry.shift();
}
monitorRegistry.push(this);
}
getId() {
return this.id;
}
getEventInstance() {
return this.eventInstance;
}
getUser() {
return this.user;
}
getTotalLatencyMs() {
return this.totalLatency;
}
addEntry(entry) {
this.entries.push(entry);
this.lastEntry = entry;
this.lastEntrySetAtMs = Date.now();
return this;
}
setEntryResult(result) {
if (this.lastEntry !== undefined) {
this.lastEntry.setResult(result);
this.finalizeLastEntry();
}
return this;
}
setEntryError(reason) {
if (this.lastEntry !== undefined) {
this.lastEntry.setError(reason);
this.finalizeLastEntry();
}
return this;
}
flagEntryAsLlm() {
if (this.lastEntry !== undefined) {
this.lastEntry.flagAsLlm();
}
return this;
}
flagEntryAsPlanner() {
if (this.lastEntry !== undefined) {
this.lastEntry.flagAsPlanner();
}
return this;
}
flagEntryAsFlowStep() {
if (this.lastEntry !== undefined) {
this.lastEntry.flagAsFlowStep();
}
return this;
}
flagEntryAsFlow() {
if (this.lastEntry !== undefined) {
this.lastEntry.flagAsFlow();
}
return this;
}
flagEntryAsDecision() {
if (this.lastEntry !== undefined) {
this.lastEntry.flagAsDecision();
}
return this;
}
setEntryLlmPrompt(s) {
if (this.lastEntry !== undefined) {
this.lastEntry.setLlmPrompt(s);
}
return this;
}
setEntryLlmResponse(s) {
if (this.lastEntry !== undefined) {
this.lastEntry.setLlmResponse(s);
}
return this;
}
finalizeLastEntry() {
if (this.lastEntry) {
const ms = Date.now() - this.lastEntrySetAtMs;
this.lastEntry.setLatencyMs(ms);
if (MonitoringCallback !== undefined) {
MonitoringCallback(this.lastEntry);
}
this.totalLatency += ms;
}
}
increment() {
const m = new Monitor();
m.parent = this;
this.entries.push(m);
return m;
}
decrement() {
if (this.parent !== undefined) {
return this.parent;
}
return this;
}
setFlowResult(result) {
if (this.parent !== undefined) {
this.parent.setFlowResult(result);
}
else {
this.flowResult = result;
}
return this;
}
asObject() {
const objs = new Array();
this.entries.forEach((entry) => {
objs.push(entry.asObject());
});
const r = { id: this.id, totalLatencyMs: this.totalLatency, flow: objs };
if (this.eventInstance) {
const n = this.eventInstance.getFqName();
const inst = this.eventInstance.asSerializableObject();
if (isAgentEventInstance(this.eventInstance)) {
r.agent = n;
r.agentInstance = inst;
}
else {
r.event = n;
r.eventInstance = inst;
}
r.label = n;
}
if (this.user) {
r.user = this.user;
}
r.timestamp = this.timestamp;
if (this.flowResult !== undefined) {
let fr = this.flowResult;
if (fr instanceof Array && Instance.IsInstance(fr[0])) {
fr = fr.map((v) => {
return v.asSerializableObject();
});
}
else if (Instance.IsInstance(fr)) {
fr = fr.asSerializableObject();
}
r.flowResult = fr;
}
return r;
}
}
Monitor.MAX_REGISTRY_SIZE = 25;
const monitorRegistry = new Array();
export function getMonitor(id) {
return monitorRegistry.filter((m) => {
return m.getId() === id;
})[0];
}
export function getMonitorsForEvent(eventName) {
return monitorRegistry.filter((m) => {
var _a;
return ((_a = m.getEventInstance()) === null || _a === void 0 ? void 0 : _a.getFqName()) === eventName;
});
}
export function identifyMonitorNode(monitorNode) {
var _a, _b;
if (monitorNode.agent) {
return 'agent';
}
else if ((_a = monitorNode.llm) === null || _a === void 0 ? void 0 : _a.isFlowStep) {
return 'flow';
}
else if ((_b = monitorNode.llm) === null || _b === void 0 ? void 0 : _b.isDecision) {
return 'decision';
}
else if (monitorNode.llm) {
return 'agent';
}
else {
return 'event';
}
}
//# sourceMappingURL=monitor.js.map