opencode-mastra-om 0.1.3 → 0.1.5
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/dist/index.js +131 -0
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -179825,6 +179825,79 @@ var MastraPlugin = async (ctx) => {
|
|
|
179825
179825
|
}
|
|
179826
179826
|
const om = new ObservationalMemory(omOptions);
|
|
179827
179827
|
omLog(`[init] ObservationalMemory created, model=${config2.model ?? "default"}`);
|
|
179828
|
+
const backupObservations = async (threadId, trigger) => {
|
|
179829
|
+
try {
|
|
179830
|
+
const record3 = await om.getRecord(threadId);
|
|
179831
|
+
const observations = record3?.activeObservations;
|
|
179832
|
+
if (!observations)
|
|
179833
|
+
return;
|
|
179834
|
+
const db = store.turso;
|
|
179835
|
+
if (!db)
|
|
179836
|
+
return;
|
|
179837
|
+
const snap = {
|
|
179838
|
+
lookupKey: threadId,
|
|
179839
|
+
generationCount: record3.generationCount ?? 0,
|
|
179840
|
+
observations,
|
|
179841
|
+
observationTokenCount: record3.observationTokenCount ?? 0,
|
|
179842
|
+
lastObservedAt: record3.lastObservedAt ?? null,
|
|
179843
|
+
lastReflectionAt: record3.lastReflectionAt ?? null,
|
|
179844
|
+
pendingMessageTokens: record3.pendingMessageTokens ?? 0,
|
|
179845
|
+
observedMessageIds: record3.observedMessageIds ?? "[]",
|
|
179846
|
+
trigger,
|
|
179847
|
+
savedAt: new Date().toISOString()
|
|
179848
|
+
};
|
|
179849
|
+
await db.execute({
|
|
179850
|
+
sql: `INSERT INTO mastra_om_backups
|
|
179851
|
+
(id, lookupKey, slot, generationCount, observations, observationTokenCount,
|
|
179852
|
+
lastObservedAt, lastReflectionAt, pendingMessageTokens, observedMessageIds, trigger, savedAt)
|
|
179853
|
+
SELECT hex(randomblob(16)), lookupKey, 2, generationCount, observations, observationTokenCount,
|
|
179854
|
+
lastObservedAt, lastReflectionAt, pendingMessageTokens, observedMessageIds, trigger, savedAt
|
|
179855
|
+
FROM mastra_om_backups WHERE lookupKey = ? AND slot = 1
|
|
179856
|
+
ON CONFLICT(lookupKey, slot) DO UPDATE SET
|
|
179857
|
+
generationCount = excluded.generationCount,
|
|
179858
|
+
observations = excluded.observations,
|
|
179859
|
+
observationTokenCount = excluded.observationTokenCount,
|
|
179860
|
+
lastObservedAt = excluded.lastObservedAt,
|
|
179861
|
+
lastReflectionAt = excluded.lastReflectionAt,
|
|
179862
|
+
pendingMessageTokens = excluded.pendingMessageTokens,
|
|
179863
|
+
observedMessageIds = excluded.observedMessageIds,
|
|
179864
|
+
trigger = excluded.trigger,
|
|
179865
|
+
savedAt = excluded.savedAt`,
|
|
179866
|
+
args: [threadId]
|
|
179867
|
+
});
|
|
179868
|
+
await db.execute({
|
|
179869
|
+
sql: `INSERT INTO mastra_om_backups
|
|
179870
|
+
(id, lookupKey, slot, generationCount, observations, observationTokenCount,
|
|
179871
|
+
lastObservedAt, lastReflectionAt, pendingMessageTokens, observedMessageIds, trigger, savedAt)
|
|
179872
|
+
VALUES (hex(randomblob(16)), ?, 1, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
179873
|
+
ON CONFLICT(lookupKey, slot) DO UPDATE SET
|
|
179874
|
+
generationCount = excluded.generationCount,
|
|
179875
|
+
observations = excluded.observations,
|
|
179876
|
+
observationTokenCount = excluded.observationTokenCount,
|
|
179877
|
+
lastObservedAt = excluded.lastObservedAt,
|
|
179878
|
+
lastReflectionAt = excluded.lastReflectionAt,
|
|
179879
|
+
pendingMessageTokens = excluded.pendingMessageTokens,
|
|
179880
|
+
observedMessageIds = excluded.observedMessageIds,
|
|
179881
|
+
trigger = excluded.trigger,
|
|
179882
|
+
savedAt = excluded.savedAt`,
|
|
179883
|
+
args: [
|
|
179884
|
+
threadId,
|
|
179885
|
+
snap.generationCount,
|
|
179886
|
+
snap.observations,
|
|
179887
|
+
snap.observationTokenCount,
|
|
179888
|
+
snap.lastObservedAt,
|
|
179889
|
+
snap.lastReflectionAt,
|
|
179890
|
+
snap.pendingMessageTokens,
|
|
179891
|
+
snap.observedMessageIds,
|
|
179892
|
+
snap.trigger,
|
|
179893
|
+
snap.savedAt
|
|
179894
|
+
]
|
|
179895
|
+
});
|
|
179896
|
+
omLog(`[backup] ${trigger} — saved gen ${snap.generationCount} to slot 1, rotated old slot 1 → slot 2`);
|
|
179897
|
+
} catch (err) {
|
|
179898
|
+
omLog(`[backup] failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
179899
|
+
}
|
|
179900
|
+
};
|
|
179828
179901
|
setTimeout(() => {
|
|
179829
179902
|
ctx.client.tui.showToast({
|
|
179830
179903
|
body: { title: "Mastra OM", message: "Observational Memory active", variant: "success", duration: 3000 }
|
|
@@ -179875,6 +179948,7 @@ var MastraPlugin = async (ctx) => {
|
|
|
179875
179948
|
try {
|
|
179876
179949
|
const mastraMessages = convertMessages2(output.messages, sessionId);
|
|
179877
179950
|
if (mastraMessages.length > 0) {
|
|
179951
|
+
await backupObservations(sessionId, "pre-auto-observe");
|
|
179878
179952
|
await runObserve(sessionId, mastraMessages);
|
|
179879
179953
|
}
|
|
179880
179954
|
const record3 = await om.getRecord(sessionId);
|
|
@@ -179981,6 +180055,7 @@ ${OBSERVATION_CONTINUATION_HINT}`);
|
|
|
179981
180055
|
if (!resp.data || resp.data.length === 0)
|
|
179982
180056
|
return "No messages to observe.";
|
|
179983
180057
|
const mastraMessages = convertMessages2(resp.data, threadId);
|
|
180058
|
+
await backupObservations(threadId, "pre-observe");
|
|
179984
180059
|
await runObserve(threadId, mastraMessages);
|
|
179985
180060
|
return "Observation cycle triggered. Check memory_status for results.";
|
|
179986
180061
|
} catch (err) {
|
|
@@ -179997,6 +180072,7 @@ ${OBSERVATION_CONTINUATION_HINT}`);
|
|
|
179997
180072
|
const threadId = context2.sessionID;
|
|
179998
180073
|
await resolveCredentials();
|
|
179999
180074
|
try {
|
|
180075
|
+
await backupObservations(threadId, "pre-reflect");
|
|
180000
180076
|
await om.reflect(threadId);
|
|
180001
180077
|
return "Reflection cycle triggered. Check memory_observations for results.";
|
|
180002
180078
|
} catch (err) {
|
|
@@ -180024,6 +180100,61 @@ ${OBSERVATION_CONTINUATION_HINT}`);
|
|
|
180024
180100
|
}
|
|
180025
180101
|
}
|
|
180026
180102
|
}),
|
|
180103
|
+
om_restore: tool5({
|
|
180104
|
+
description: "Restore observational memory from backup slot 1 (most recent) or slot 2 (one generation older).",
|
|
180105
|
+
args: { slot: { type: "number", description: "1 = most recent backup, 2 = one generation older" } },
|
|
180106
|
+
async execute(args, context2) {
|
|
180107
|
+
const threadId = context2.sessionID;
|
|
180108
|
+
const slot = args.slot === 2 ? 2 : 1;
|
|
180109
|
+
try {
|
|
180110
|
+
const db = store.turso;
|
|
180111
|
+
if (!db)
|
|
180112
|
+
return "Raw DB access unavailable.";
|
|
180113
|
+
const result = await db.execute({
|
|
180114
|
+
sql: `SELECT * FROM mastra_om_backups WHERE lookupKey = ? AND slot = ?`,
|
|
180115
|
+
args: [threadId, slot]
|
|
180116
|
+
});
|
|
180117
|
+
const row = result.rows?.[0];
|
|
180118
|
+
if (!row)
|
|
180119
|
+
return `No backup found in slot ${slot}.`;
|
|
180120
|
+
await db.execute({
|
|
180121
|
+
sql: `UPDATE mastra_observational_memory SET
|
|
180122
|
+
activeObservations = ?,
|
|
180123
|
+
generationCount = ?,
|
|
180124
|
+
observationTokenCount = ?,
|
|
180125
|
+
lastObservedAt = ?,
|
|
180126
|
+
lastReflectionAt = ?,
|
|
180127
|
+
pendingMessageTokens = ?,
|
|
180128
|
+
observedMessageIds = ?
|
|
180129
|
+
WHERE lookupKey = ?`,
|
|
180130
|
+
args: [
|
|
180131
|
+
row.observations,
|
|
180132
|
+
row.generationCount,
|
|
180133
|
+
row.observationTokenCount,
|
|
180134
|
+
row.lastObservedAt,
|
|
180135
|
+
row.lastReflectionAt,
|
|
180136
|
+
row.pendingMessageTokens,
|
|
180137
|
+
row.observedMessageIds,
|
|
180138
|
+
threadId
|
|
180139
|
+
]
|
|
180140
|
+
});
|
|
180141
|
+
omLog(`[restore] restored slot ${slot} — gen ${row.generationCount}, saved at ${row.savedAt}, trigger=${row.trigger}`);
|
|
180142
|
+
return [
|
|
180143
|
+
`✅ Restored from slot ${slot}`,
|
|
180144
|
+
` Generation: ${row.generationCount}`,
|
|
180145
|
+
` Saved at: ${row.savedAt}`,
|
|
180146
|
+
` Trigger: ${row.trigger}`,
|
|
180147
|
+
` Observation tokens: ${row.observationTokenCount}`,
|
|
180148
|
+
` Last observed: ${row.lastObservedAt ?? "never"}`,
|
|
180149
|
+
` Last reflection: ${row.lastReflectionAt ?? "never"}`
|
|
180150
|
+
].join(`
|
|
180151
|
+
`);
|
|
180152
|
+
} catch (err) {
|
|
180153
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
180154
|
+
return `Restore failed: ${msg}`;
|
|
180155
|
+
}
|
|
180156
|
+
}
|
|
180157
|
+
}),
|
|
180027
180158
|
om_config: tool5({
|
|
180028
180159
|
description: "Show the current Mastra Observational Memory configuration.",
|
|
180029
180160
|
args: {},
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package.json",
|
|
3
3
|
"name": "opencode-mastra-om",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.5",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"description": "Enhanced Mastra Observational Memory plugin for OpenCode — persistent cross-session memory with observation, reflection, and manual trigger tools",
|
|
@@ -38,8 +38,8 @@
|
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
40
|
"@mastra/core": "^1.21.0",
|
|
41
|
-
"@mastra/libsql": "^1.7.
|
|
42
|
-
"@mastra/memory": "^1.13.
|
|
41
|
+
"@mastra/libsql": "^1.7.4",
|
|
42
|
+
"@mastra/memory": "^1.13.1",
|
|
43
43
|
"@opencode-ai/plugin": "^1.3.11",
|
|
44
44
|
"@opencode-ai/sdk": "^1.3.11"
|
|
45
45
|
},
|