@agenr/openclaw-plugin 3.3.0 → 2026.6.3

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.
@@ -0,0 +1,106 @@
1
+ // src/adapters/openclaw/debug/sink.ts
2
+ import { appendFile, mkdir } from "fs/promises";
3
+ import path from "path";
4
+ var NOOP_SINK = {
5
+ enabled: false,
6
+ eventLevel: "basic",
7
+ maxTopCandidates: 0,
8
+ async emit() {
9
+ return;
10
+ },
11
+ async close() {
12
+ return;
13
+ }
14
+ };
15
+ function createNoopAgenrDebugSink() {
16
+ return NOOP_SINK;
17
+ }
18
+ function createAgenrDebugSink(config) {
19
+ if (!config.enabled || !config.logPath) {
20
+ return NOOP_SINK;
21
+ }
22
+ const basePath = config.logPath;
23
+ const perSessionFiles = config.perSessionFiles;
24
+ const eventLevel = config.eventLevel;
25
+ const maxTopCandidates = config.maxTopCandidates;
26
+ const directoriesEnsured = /* @__PURE__ */ new Set();
27
+ let writeChain = Promise.resolve();
28
+ let closed = false;
29
+ const ensureDirectoryOnce = async (filePath) => {
30
+ const directory = path.dirname(filePath);
31
+ if (directoriesEnsured.has(directory)) {
32
+ return;
33
+ }
34
+ await mkdir(directory, { recursive: true });
35
+ directoriesEnsured.add(directory);
36
+ };
37
+ const resolveFilePath = (event) => {
38
+ if (!perSessionFiles) {
39
+ return basePath;
40
+ }
41
+ const sessionSuffix = sanitizeSessionSuffix(event.sessionId) ?? sanitizeSessionSuffix(event.sessionKey);
42
+ if (!sessionSuffix) {
43
+ return basePath;
44
+ }
45
+ const directory = path.dirname(basePath);
46
+ const extension = path.extname(basePath);
47
+ const baseName = path.basename(basePath, extension);
48
+ const suffixed = `${baseName}.${sessionSuffix}${extension || ".jsonl"}`;
49
+ return path.join(directory, suffixed);
50
+ };
51
+ const writeLine = async (filePath, line) => {
52
+ await ensureDirectoryOnce(filePath);
53
+ await appendFile(filePath, `${line}
54
+ `, "utf8");
55
+ };
56
+ return {
57
+ enabled: true,
58
+ eventLevel,
59
+ maxTopCandidates,
60
+ logPath: basePath,
61
+ async emit(event) {
62
+ if (closed) {
63
+ return;
64
+ }
65
+ const filePath = resolveFilePath(event);
66
+ const line = formatEventLine(event);
67
+ writeChain = writeChain.then(async () => {
68
+ try {
69
+ await writeLine(filePath, line);
70
+ } catch {
71
+ }
72
+ });
73
+ await writeChain;
74
+ },
75
+ async close() {
76
+ closed = true;
77
+ await writeChain;
78
+ }
79
+ };
80
+ }
81
+ function formatEventLine(event) {
82
+ const line = {
83
+ ts: (/* @__PURE__ */ new Date()).toISOString(),
84
+ ...event
85
+ };
86
+ return JSON.stringify(line);
87
+ }
88
+ function sanitizeSessionSuffix(value) {
89
+ if (typeof value !== "string") {
90
+ return void 0;
91
+ }
92
+ const trimmed = value.trim();
93
+ if (trimmed.length === 0) {
94
+ return void 0;
95
+ }
96
+ const sanitized = trimmed.replace(/[^A-Za-z0-9._-]+/gu, "_");
97
+ if (sanitized.length === 0) {
98
+ return void 0;
99
+ }
100
+ return sanitized.slice(0, 120);
101
+ }
102
+
103
+ export {
104
+ createNoopAgenrDebugSink,
105
+ createAgenrDebugSink
106
+ };