-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsovereign-memory.ts
More file actions
87 lines (83 loc) · 2.49 KB
/
Copy pathsovereign-memory.ts
File metadata and controls
87 lines (83 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import "server-only";
import { randomUUID } from "node:crypto";
import type {
StoredAgentMemoryRecord,
StoredProofRecord,
} from "@/lib/backend-data";
import type { WalletNetworkKey } from "@/lib/wallet";
import {
getLatestAgentMemory,
recordAgentMemory,
} from "@/lib/server/runtime-store";
import { uploadJsonToZeroGStorage } from "@/lib/server/zero-g-storage";
function nextMemoryVersion(previous: StoredAgentMemoryRecord | null) {
return (previous?.memoryVersion ?? 0) + 1;
}
export async function syncSovereignMemory(input: {
agentId?: string;
tokenId?: string;
walletAddress?: string;
networkKey: WalletNetworkKey;
proof?: StoredProofRecord;
contextSummary?: string;
recentTask?: string;
}) {
const timestamp = new Date().toISOString();
const agentId =
input.agentId ??
input.tokenId ??
input.walletAddress ??
"yieldboost-default-agent";
const previous = await getLatestAgentMemory(agentId, input.networkKey);
const memoryVersion = nextMemoryVersion(previous);
const snapshot = {
contextSummary:
input.contextSummary ??
input.proof?.decision.reasoning ??
"YieldBoost agent context snapshot synced after proof processing.",
recentTask:
input.recentTask ??
input.proof?.decision.recommended ??
"Portfolio optimization audit",
lastRecommendation: input.proof?.decision.recommended,
auditStatus: input.proof?.integrityAudit?.status,
proofCid: input.proof?.cid,
};
const storagePayload = {
appId: "yieldboost-ai",
artifactType: "sovereign-memory-snapshot",
timestamp,
networkKey: input.networkKey,
agentId,
tokenId: input.tokenId,
walletAddress: input.walletAddress ?? input.proof?.walletAddress,
proofCid: input.proof?.cid,
memoryVersion,
previousMemoryCid: previous?.cid,
snapshot,
};
const upload = await uploadJsonToZeroGStorage({
networkKey: input.networkKey,
payload: storagePayload,
filenamePrefix: "yieldboost-memory",
allowLocalFallback: true,
});
const record: StoredAgentMemoryRecord = {
id: randomUUID(),
agentId,
tokenId: input.tokenId,
walletAddress: input.walletAddress ?? input.proof?.walletAddress,
networkKey: input.networkKey,
cid: upload.cid,
txHash: upload.txHash,
blockNumber: upload.blockNumber,
explorerUrl: upload.explorerUrl,
proofCid: input.proof?.cid,
memoryVersion,
snapshot,
timestamp,
storageMode: upload.storageMode,
note: upload.note,
};
return recordAgentMemory(record);
}