@memgrafter/flatagents 0.9.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/MACHINES.md +78 -1
- package/dist/index.d.mts +49 -23
- package/dist/index.d.ts +49 -23
- package/dist/index.js +187 -142
- package/dist/index.mjs +186 -142
- package/package.json +1 -1
- package/schemas/flatagent.d.ts +11 -1
- package/schemas/flatagent.schema.json +38 -0
- package/schemas/flatagent.slim.d.ts +10 -1
- package/schemas/flatagents-runtime.d.ts +338 -47
- package/schemas/flatagents-runtime.schema.json +73 -14
- package/schemas/flatagents-runtime.slim.d.ts +102 -7
- package/schemas/flatmachine.d.ts +95 -32
- package/schemas/flatmachine.schema.json +141 -20
- package/schemas/flatmachine.slim.d.ts +30 -8
- package/schemas/profiles.d.ts +3 -1
- package/schemas/profiles.schema.json +3 -0
- package/schemas/profiles.slim.d.ts +2 -1
package/dist/index.js
CHANGED
|
@@ -35,6 +35,7 @@ __export(index_exports, {
|
|
|
35
35
|
DefaultExecution: () => DefaultExecution,
|
|
36
36
|
FlatAgent: () => FlatAgent,
|
|
37
37
|
FlatMachine: () => FlatMachine,
|
|
38
|
+
HooksRegistry: () => HooksRegistry,
|
|
38
39
|
LocalFileBackend: () => LocalFileBackend,
|
|
39
40
|
LocalFileLock: () => LocalFileLock,
|
|
40
41
|
MCPToolProvider: () => MCPToolProvider,
|
|
@@ -1006,6 +1007,179 @@ var LocalFileLock = class {
|
|
|
1006
1007
|
}
|
|
1007
1008
|
};
|
|
1008
1009
|
|
|
1010
|
+
// src/hooks.ts
|
|
1011
|
+
var WebhookHooks = class {
|
|
1012
|
+
constructor(url) {
|
|
1013
|
+
this.url = url;
|
|
1014
|
+
}
|
|
1015
|
+
async send(event, data) {
|
|
1016
|
+
try {
|
|
1017
|
+
const body = JSON.stringify({ event, ...data, timestamp: (/* @__PURE__ */ new Date()).toISOString() }, (key, value) => {
|
|
1018
|
+
if (typeof value === "object" && value !== null) {
|
|
1019
|
+
const seen = /* @__PURE__ */ new WeakSet();
|
|
1020
|
+
return JSON.parse(JSON.stringify(value, (k, v) => {
|
|
1021
|
+
if (typeof v === "object" && v !== null) {
|
|
1022
|
+
if (seen.has(v)) return "[Circular]";
|
|
1023
|
+
seen.add(v);
|
|
1024
|
+
}
|
|
1025
|
+
return v;
|
|
1026
|
+
}));
|
|
1027
|
+
}
|
|
1028
|
+
return value;
|
|
1029
|
+
});
|
|
1030
|
+
await fetch(this.url, {
|
|
1031
|
+
method: "POST",
|
|
1032
|
+
headers: { "Content-Type": "application/json" },
|
|
1033
|
+
body
|
|
1034
|
+
});
|
|
1035
|
+
} catch {
|
|
1036
|
+
}
|
|
1037
|
+
}
|
|
1038
|
+
async onMachineStart(context) {
|
|
1039
|
+
await this.send("machine_start", { context });
|
|
1040
|
+
return context;
|
|
1041
|
+
}
|
|
1042
|
+
async onMachineEnd(context, output) {
|
|
1043
|
+
await this.send("machine_end", { context, output });
|
|
1044
|
+
return output;
|
|
1045
|
+
}
|
|
1046
|
+
async onStateEnter(state, context) {
|
|
1047
|
+
await this.send("state_enter", { state, context });
|
|
1048
|
+
return context;
|
|
1049
|
+
}
|
|
1050
|
+
async onStateExit(state, context, output) {
|
|
1051
|
+
await this.send("state_exit", { state, context, output });
|
|
1052
|
+
return output;
|
|
1053
|
+
}
|
|
1054
|
+
async onAction(action, context) {
|
|
1055
|
+
await this.send("action", { action, context });
|
|
1056
|
+
return context;
|
|
1057
|
+
}
|
|
1058
|
+
};
|
|
1059
|
+
var CompositeHooks = class {
|
|
1060
|
+
constructor(hooks) {
|
|
1061
|
+
this.hooks = hooks;
|
|
1062
|
+
}
|
|
1063
|
+
async onMachineStart(context) {
|
|
1064
|
+
let result = context;
|
|
1065
|
+
for (const hook of this.hooks) {
|
|
1066
|
+
if (hook.onMachineStart) {
|
|
1067
|
+
try {
|
|
1068
|
+
result = await hook.onMachineStart(result);
|
|
1069
|
+
} catch {
|
|
1070
|
+
}
|
|
1071
|
+
}
|
|
1072
|
+
}
|
|
1073
|
+
return result;
|
|
1074
|
+
}
|
|
1075
|
+
async onMachineEnd(context, output) {
|
|
1076
|
+
let result = output;
|
|
1077
|
+
for (const hook of this.hooks) {
|
|
1078
|
+
if (hook.onMachineEnd) {
|
|
1079
|
+
try {
|
|
1080
|
+
result = await hook.onMachineEnd(context, result);
|
|
1081
|
+
} catch {
|
|
1082
|
+
}
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1085
|
+
return result;
|
|
1086
|
+
}
|
|
1087
|
+
async onStateEnter(state, context) {
|
|
1088
|
+
let result = context;
|
|
1089
|
+
for (const hook of this.hooks) {
|
|
1090
|
+
if (hook.onStateEnter) {
|
|
1091
|
+
try {
|
|
1092
|
+
result = await hook.onStateEnter(state, result);
|
|
1093
|
+
} catch {
|
|
1094
|
+
}
|
|
1095
|
+
}
|
|
1096
|
+
}
|
|
1097
|
+
return result;
|
|
1098
|
+
}
|
|
1099
|
+
async onStateExit(state, context, output) {
|
|
1100
|
+
let result = output;
|
|
1101
|
+
for (const hook of this.hooks) {
|
|
1102
|
+
if (hook.onStateExit) {
|
|
1103
|
+
try {
|
|
1104
|
+
result = await hook.onStateExit(state, context, result);
|
|
1105
|
+
} catch {
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1108
|
+
}
|
|
1109
|
+
return result;
|
|
1110
|
+
}
|
|
1111
|
+
async onTransition(from, to, context) {
|
|
1112
|
+
let result = to;
|
|
1113
|
+
for (const hook of this.hooks) {
|
|
1114
|
+
if (hook.onTransition) {
|
|
1115
|
+
try {
|
|
1116
|
+
result = await hook.onTransition(from, result, context);
|
|
1117
|
+
} catch {
|
|
1118
|
+
}
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
return result;
|
|
1122
|
+
}
|
|
1123
|
+
async onError(state, error, context) {
|
|
1124
|
+
let result = null;
|
|
1125
|
+
for (const hook of this.hooks) {
|
|
1126
|
+
if (hook.onError) {
|
|
1127
|
+
try {
|
|
1128
|
+
const hookResult = await hook.onError(state, error, context);
|
|
1129
|
+
if (hookResult !== null) result = hookResult;
|
|
1130
|
+
} catch {
|
|
1131
|
+
}
|
|
1132
|
+
}
|
|
1133
|
+
}
|
|
1134
|
+
return result;
|
|
1135
|
+
}
|
|
1136
|
+
async onAction(action, context) {
|
|
1137
|
+
let result = context;
|
|
1138
|
+
for (const hook of this.hooks) {
|
|
1139
|
+
if (hook.onAction) {
|
|
1140
|
+
try {
|
|
1141
|
+
result = await hook.onAction(action, result);
|
|
1142
|
+
} catch {
|
|
1143
|
+
}
|
|
1144
|
+
}
|
|
1145
|
+
}
|
|
1146
|
+
return result;
|
|
1147
|
+
}
|
|
1148
|
+
};
|
|
1149
|
+
var HooksRegistry = class {
|
|
1150
|
+
constructor() {
|
|
1151
|
+
this.factories = /* @__PURE__ */ new Map();
|
|
1152
|
+
}
|
|
1153
|
+
register(name, factory) {
|
|
1154
|
+
this.factories.set(name, factory);
|
|
1155
|
+
}
|
|
1156
|
+
has(name) {
|
|
1157
|
+
return this.factories.has(name);
|
|
1158
|
+
}
|
|
1159
|
+
resolve(ref) {
|
|
1160
|
+
if (Array.isArray(ref)) {
|
|
1161
|
+
const hooks = ref.map((entry) => this.resolveSingle(entry));
|
|
1162
|
+
return new CompositeHooks(hooks);
|
|
1163
|
+
}
|
|
1164
|
+
return this.resolveSingle(ref);
|
|
1165
|
+
}
|
|
1166
|
+
resolveSingle(ref) {
|
|
1167
|
+
const name = typeof ref === "string" ? ref : ref.name;
|
|
1168
|
+
const args = typeof ref === "string" ? void 0 : ref.args;
|
|
1169
|
+
const factory = this.factories.get(name);
|
|
1170
|
+
if (!factory) {
|
|
1171
|
+
throw new Error(
|
|
1172
|
+
`No hooks registered for name '${name}'. Registered: [${[...this.factories.keys()].join(", ")}]`
|
|
1173
|
+
);
|
|
1174
|
+
}
|
|
1175
|
+
try {
|
|
1176
|
+
return new factory(args);
|
|
1177
|
+
} catch {
|
|
1178
|
+
return factory(args);
|
|
1179
|
+
}
|
|
1180
|
+
}
|
|
1181
|
+
};
|
|
1182
|
+
|
|
1009
1183
|
// src/flatmachine.ts
|
|
1010
1184
|
var FlatMachine = class _FlatMachine {
|
|
1011
1185
|
constructor(options) {
|
|
@@ -1017,7 +1191,8 @@ var FlatMachine = class _FlatMachine {
|
|
|
1017
1191
|
this.pendingLaunches = [];
|
|
1018
1192
|
this.currentStep = 0;
|
|
1019
1193
|
this.config = typeof options.config === "string" ? yaml3.parse((0, import_fs5.readFileSync)(options.config, "utf-8")) : options.config;
|
|
1020
|
-
this.
|
|
1194
|
+
this._hooksRegistry = options.hooksRegistry ?? new HooksRegistry();
|
|
1195
|
+
this.hooks = this.resolveHooks(options.hooks);
|
|
1021
1196
|
this.configDir = options.configDir ?? process.cwd();
|
|
1022
1197
|
this.profilesFile = this.resolveProfilesFile(options.profilesFile);
|
|
1023
1198
|
this.executionId = options.executionId ?? this.executionId;
|
|
@@ -1040,6 +1215,15 @@ var FlatMachine = class _FlatMachine {
|
|
|
1040
1215
|
this.checkpointEvents = new Set(events);
|
|
1041
1216
|
}
|
|
1042
1217
|
}
|
|
1218
|
+
get hooksRegistry() {
|
|
1219
|
+
return this._hooksRegistry;
|
|
1220
|
+
}
|
|
1221
|
+
resolveHooks(explicit) {
|
|
1222
|
+
if (explicit) return explicit;
|
|
1223
|
+
const hooksConfig = this.config.data.hooks;
|
|
1224
|
+
if (!hooksConfig) return void 0;
|
|
1225
|
+
return this._hooksRegistry.resolve(hooksConfig);
|
|
1226
|
+
}
|
|
1043
1227
|
async execute(input, resumeSnapshot) {
|
|
1044
1228
|
if (this.config.data.expression_engine === "cel") {
|
|
1045
1229
|
throw new Error("expression_engine 'cel' is not supported in the JS SDK yet");
|
|
@@ -1323,7 +1507,7 @@ var FlatMachine = class _FlatMachine {
|
|
|
1323
1507
|
config: resolved.config,
|
|
1324
1508
|
configDir: resolved.configDir,
|
|
1325
1509
|
resultBackend: this.resultBackend,
|
|
1326
|
-
|
|
1510
|
+
hooksRegistry: this._hooksRegistry,
|
|
1327
1511
|
executionId: overrides?.executionId,
|
|
1328
1512
|
parentExecutionId: overrides?.parentExecutionId,
|
|
1329
1513
|
profilesFile: this.profilesFile
|
|
@@ -1533,146 +1717,6 @@ var FlatMachine = class _FlatMachine {
|
|
|
1533
1717
|
return { [firstKey]: results[firstKey] };
|
|
1534
1718
|
}
|
|
1535
1719
|
};
|
|
1536
|
-
|
|
1537
|
-
// src/hooks.ts
|
|
1538
|
-
var WebhookHooks = class {
|
|
1539
|
-
constructor(url) {
|
|
1540
|
-
this.url = url;
|
|
1541
|
-
}
|
|
1542
|
-
async send(event, data) {
|
|
1543
|
-
try {
|
|
1544
|
-
const body = JSON.stringify({ event, ...data, timestamp: (/* @__PURE__ */ new Date()).toISOString() }, (key, value) => {
|
|
1545
|
-
if (typeof value === "object" && value !== null) {
|
|
1546
|
-
const seen = /* @__PURE__ */ new WeakSet();
|
|
1547
|
-
return JSON.parse(JSON.stringify(value, (k, v) => {
|
|
1548
|
-
if (typeof v === "object" && v !== null) {
|
|
1549
|
-
if (seen.has(v)) return "[Circular]";
|
|
1550
|
-
seen.add(v);
|
|
1551
|
-
}
|
|
1552
|
-
return v;
|
|
1553
|
-
}));
|
|
1554
|
-
}
|
|
1555
|
-
return value;
|
|
1556
|
-
});
|
|
1557
|
-
await fetch(this.url, {
|
|
1558
|
-
method: "POST",
|
|
1559
|
-
headers: { "Content-Type": "application/json" },
|
|
1560
|
-
body
|
|
1561
|
-
});
|
|
1562
|
-
} catch {
|
|
1563
|
-
}
|
|
1564
|
-
}
|
|
1565
|
-
async onMachineStart(context) {
|
|
1566
|
-
await this.send("machine_start", { context });
|
|
1567
|
-
return context;
|
|
1568
|
-
}
|
|
1569
|
-
async onMachineEnd(context, output) {
|
|
1570
|
-
await this.send("machine_end", { context, output });
|
|
1571
|
-
return output;
|
|
1572
|
-
}
|
|
1573
|
-
async onStateEnter(state, context) {
|
|
1574
|
-
await this.send("state_enter", { state, context });
|
|
1575
|
-
return context;
|
|
1576
|
-
}
|
|
1577
|
-
async onStateExit(state, context, output) {
|
|
1578
|
-
await this.send("state_exit", { state, context, output });
|
|
1579
|
-
return output;
|
|
1580
|
-
}
|
|
1581
|
-
async onAction(action, context) {
|
|
1582
|
-
await this.send("action", { action, context });
|
|
1583
|
-
return context;
|
|
1584
|
-
}
|
|
1585
|
-
};
|
|
1586
|
-
var CompositeHooks = class {
|
|
1587
|
-
constructor(hooks) {
|
|
1588
|
-
this.hooks = hooks;
|
|
1589
|
-
}
|
|
1590
|
-
async onMachineStart(context) {
|
|
1591
|
-
let result = context;
|
|
1592
|
-
for (const hook of this.hooks) {
|
|
1593
|
-
if (hook.onMachineStart) {
|
|
1594
|
-
try {
|
|
1595
|
-
result = await hook.onMachineStart(result);
|
|
1596
|
-
} catch {
|
|
1597
|
-
}
|
|
1598
|
-
}
|
|
1599
|
-
}
|
|
1600
|
-
return result;
|
|
1601
|
-
}
|
|
1602
|
-
async onMachineEnd(context, output) {
|
|
1603
|
-
let result = output;
|
|
1604
|
-
for (const hook of this.hooks) {
|
|
1605
|
-
if (hook.onMachineEnd) {
|
|
1606
|
-
try {
|
|
1607
|
-
result = await hook.onMachineEnd(context, result);
|
|
1608
|
-
} catch {
|
|
1609
|
-
}
|
|
1610
|
-
}
|
|
1611
|
-
}
|
|
1612
|
-
return result;
|
|
1613
|
-
}
|
|
1614
|
-
async onStateEnter(state, context) {
|
|
1615
|
-
let result = context;
|
|
1616
|
-
for (const hook of this.hooks) {
|
|
1617
|
-
if (hook.onStateEnter) {
|
|
1618
|
-
try {
|
|
1619
|
-
result = await hook.onStateEnter(state, result);
|
|
1620
|
-
} catch {
|
|
1621
|
-
}
|
|
1622
|
-
}
|
|
1623
|
-
}
|
|
1624
|
-
return result;
|
|
1625
|
-
}
|
|
1626
|
-
async onStateExit(state, context, output) {
|
|
1627
|
-
let result = output;
|
|
1628
|
-
for (const hook of this.hooks) {
|
|
1629
|
-
if (hook.onStateExit) {
|
|
1630
|
-
try {
|
|
1631
|
-
result = await hook.onStateExit(state, context, result);
|
|
1632
|
-
} catch {
|
|
1633
|
-
}
|
|
1634
|
-
}
|
|
1635
|
-
}
|
|
1636
|
-
return result;
|
|
1637
|
-
}
|
|
1638
|
-
async onTransition(from, to, context) {
|
|
1639
|
-
let result = to;
|
|
1640
|
-
for (const hook of this.hooks) {
|
|
1641
|
-
if (hook.onTransition) {
|
|
1642
|
-
try {
|
|
1643
|
-
result = await hook.onTransition(from, result, context);
|
|
1644
|
-
} catch {
|
|
1645
|
-
}
|
|
1646
|
-
}
|
|
1647
|
-
}
|
|
1648
|
-
return result;
|
|
1649
|
-
}
|
|
1650
|
-
async onError(state, error, context) {
|
|
1651
|
-
let result = null;
|
|
1652
|
-
for (const hook of this.hooks) {
|
|
1653
|
-
if (hook.onError) {
|
|
1654
|
-
try {
|
|
1655
|
-
const hookResult = await hook.onError(state, error, context);
|
|
1656
|
-
if (hookResult !== null) result = hookResult;
|
|
1657
|
-
} catch {
|
|
1658
|
-
}
|
|
1659
|
-
}
|
|
1660
|
-
}
|
|
1661
|
-
return result;
|
|
1662
|
-
}
|
|
1663
|
-
async onAction(action, context) {
|
|
1664
|
-
let result = context;
|
|
1665
|
-
for (const hook of this.hooks) {
|
|
1666
|
-
if (hook.onAction) {
|
|
1667
|
-
try {
|
|
1668
|
-
result = await hook.onAction(action, result);
|
|
1669
|
-
} catch {
|
|
1670
|
-
}
|
|
1671
|
-
}
|
|
1672
|
-
}
|
|
1673
|
-
return result;
|
|
1674
|
-
}
|
|
1675
|
-
};
|
|
1676
1720
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1677
1721
|
0 && (module.exports = {
|
|
1678
1722
|
CheckpointManager,
|
|
@@ -1680,6 +1724,7 @@ var CompositeHooks = class {
|
|
|
1680
1724
|
DefaultExecution,
|
|
1681
1725
|
FlatAgent,
|
|
1682
1726
|
FlatMachine,
|
|
1727
|
+
HooksRegistry,
|
|
1683
1728
|
LocalFileBackend,
|
|
1684
1729
|
LocalFileLock,
|
|
1685
1730
|
MCPToolProvider,
|