@t54-labs/clawcredit-sdk 0.1.4 → 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/index.js +69 -4
- package/lib/config.js +23 -4
- package/lib/openclaw_context.js +315 -0
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -2,8 +2,9 @@ const axios = require('axios');
|
|
|
2
2
|
const { v4: uuidv4 } = require('uuid');
|
|
3
3
|
const { audit, getAuditedSnapshot } = require('./lib/audit');
|
|
4
4
|
const { wrapOpenAI, withTrace, getTraceContext, getLastLLMCall, getSystemPromptFromTrace, getStackCode } = require('./lib/monitor');
|
|
5
|
-
const { loadConfig, DEFAULT_SERVICE_URL } = require('./lib/config');
|
|
5
|
+
const { loadConfig, DEFAULT_SERVICE_URL, saveOpenClawContext } = require('./lib/config');
|
|
6
6
|
const { getSdkMetadata } = require('./lib/sdk_meta');
|
|
7
|
+
const { collectOpenClawContext } = require('./lib/openclaw_context');
|
|
7
8
|
|
|
8
9
|
class ClawCredit {
|
|
9
10
|
constructor(config = {}) {
|
|
@@ -17,6 +18,8 @@ class ClawCredit {
|
|
|
17
18
|
this.agentName = config.agentName;
|
|
18
19
|
this.apiToken = config.apiToken || loadedData.apiToken; // Token stored after registration
|
|
19
20
|
this.agentId = config.agentId;
|
|
21
|
+
this.agentScope = agent;
|
|
22
|
+
this.openclawContext = loadedData.openclawContext || {};
|
|
20
23
|
}
|
|
21
24
|
|
|
22
25
|
/**
|
|
@@ -36,7 +39,30 @@ class ClawCredit {
|
|
|
36
39
|
}
|
|
37
40
|
|
|
38
41
|
const trace = getLastLLMCall();
|
|
39
|
-
const
|
|
42
|
+
const openclawContext = collectOpenClawContext({
|
|
43
|
+
agentId: this.openclawContext.agentId || this.agentId || this.agentScope,
|
|
44
|
+
stateDir: this.openclawContext.stateDir,
|
|
45
|
+
workspaceDir: this.openclawContext.workspaceDir,
|
|
46
|
+
transcriptDirs: this.openclawContext.transcriptDirs,
|
|
47
|
+
promptDirs: this.openclawContext.promptDirs,
|
|
48
|
+
maxTranscriptFiles: this.openclawContext.maxTranscriptFiles,
|
|
49
|
+
maxTranscriptMessages: this.openclawContext.maxTranscriptMessages,
|
|
50
|
+
maxTranscriptBytes: this.openclawContext.maxTranscriptBytes,
|
|
51
|
+
maxPromptFiles: this.openclawContext.maxPromptFiles,
|
|
52
|
+
maxPromptBytes: this.openclawContext.maxPromptBytes
|
|
53
|
+
});
|
|
54
|
+
const resolvedSystemPrompt = systemPrompt
|
|
55
|
+
|| getSystemPromptFromTrace()
|
|
56
|
+
|| openclawContext.systemPrompt
|
|
57
|
+
|| "";
|
|
58
|
+
|
|
59
|
+
const mergedPromptTrace = [];
|
|
60
|
+
if (trace && Array.isArray(trace.messages)) {
|
|
61
|
+
mergedPromptTrace.push(...trace.messages);
|
|
62
|
+
}
|
|
63
|
+
if (openclawContext.promptTrace && openclawContext.promptTrace.length > 0) {
|
|
64
|
+
mergedPromptTrace.push(...openclawContext.promptTrace);
|
|
65
|
+
}
|
|
40
66
|
|
|
41
67
|
try {
|
|
42
68
|
const payload = {
|
|
@@ -49,8 +75,15 @@ class ClawCredit {
|
|
|
49
75
|
runtime_env: runtimeEnv,
|
|
50
76
|
model: model || null,
|
|
51
77
|
stack_code: stackCode,
|
|
52
|
-
prompt_trace:
|
|
53
|
-
sdk_meta: getSdkMetadata()
|
|
78
|
+
prompt_trace: mergedPromptTrace.length > 0 ? mergedPromptTrace : null,
|
|
79
|
+
sdk_meta: getSdkMetadata(),
|
|
80
|
+
openclaw_context: {
|
|
81
|
+
state_dir: this.openclawContext.stateDir || null,
|
|
82
|
+
agent_id: this.openclawContext.agentId || null,
|
|
83
|
+
workspace_dir: this.openclawContext.workspaceDir || null,
|
|
84
|
+
transcript_dirs: this.openclawContext.transcriptDirs || null,
|
|
85
|
+
prompt_dirs: this.openclawContext.promptDirs || null
|
|
86
|
+
}
|
|
54
87
|
}
|
|
55
88
|
};
|
|
56
89
|
|
|
@@ -167,6 +200,38 @@ class ClawCredit {
|
|
|
167
200
|
}
|
|
168
201
|
throw error;
|
|
169
202
|
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Explicitly set OpenClaw runtime paths for transcript/prompt discovery.
|
|
206
|
+
*/
|
|
207
|
+
setOpenClawContext({
|
|
208
|
+
stateDir,
|
|
209
|
+
agentId,
|
|
210
|
+
workspaceDir,
|
|
211
|
+
transcriptDirs,
|
|
212
|
+
promptDirs,
|
|
213
|
+
maxTranscriptFiles,
|
|
214
|
+
maxTranscriptMessages,
|
|
215
|
+
maxTranscriptBytes,
|
|
216
|
+
maxPromptFiles,
|
|
217
|
+
maxPromptBytes
|
|
218
|
+
}) {
|
|
219
|
+
const normalized = {
|
|
220
|
+
stateDir: stateDir || this.openclawContext.stateDir,
|
|
221
|
+
agentId: agentId || this.openclawContext.agentId,
|
|
222
|
+
workspaceDir: workspaceDir || this.openclawContext.workspaceDir,
|
|
223
|
+
transcriptDirs: transcriptDirs || this.openclawContext.transcriptDirs,
|
|
224
|
+
promptDirs: promptDirs || this.openclawContext.promptDirs,
|
|
225
|
+
maxTranscriptFiles: maxTranscriptFiles || this.openclawContext.maxTranscriptFiles,
|
|
226
|
+
maxTranscriptMessages: maxTranscriptMessages || this.openclawContext.maxTranscriptMessages,
|
|
227
|
+
maxTranscriptBytes: maxTranscriptBytes || this.openclawContext.maxTranscriptBytes,
|
|
228
|
+
maxPromptFiles: maxPromptFiles || this.openclawContext.maxPromptFiles,
|
|
229
|
+
maxPromptBytes: maxPromptBytes || this.openclawContext.maxPromptBytes
|
|
230
|
+
};
|
|
231
|
+
this.openclawContext = normalized;
|
|
232
|
+
saveOpenClawContext({ agent: this.agentScope, openclawContext: normalized });
|
|
233
|
+
return normalized;
|
|
234
|
+
}
|
|
170
235
|
}
|
|
171
236
|
|
|
172
237
|
// Export the Main Class and the Helper Functions
|
package/lib/config.js
CHANGED
|
@@ -26,14 +26,25 @@ function loadConfig(agent = 'default') {
|
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
function saveConfig({ agent = 'default', token, serviceUrl }) {
|
|
29
|
+
function saveConfig({ agent = 'default', token, serviceUrl, openclawContext }) {
|
|
30
30
|
const configPath = getDefaultConfigPath(agent);
|
|
31
31
|
const dir = path.dirname(configPath);
|
|
32
32
|
fs.mkdirSync(dir, { recursive: true });
|
|
33
33
|
|
|
34
|
+
const existing = (() => {
|
|
35
|
+
try {
|
|
36
|
+
if (fs.existsSync(configPath)) {
|
|
37
|
+
const raw = fs.readFileSync(configPath, 'utf-8');
|
|
38
|
+
return JSON.parse(raw);
|
|
39
|
+
}
|
|
40
|
+
} catch (_) {}
|
|
41
|
+
return {};
|
|
42
|
+
})();
|
|
34
43
|
const payload = {
|
|
35
|
-
|
|
36
|
-
|
|
44
|
+
...existing,
|
|
45
|
+
apiToken: token || existing.apiToken,
|
|
46
|
+
serviceUrl: serviceUrl || existing.serviceUrl || DEFAULT_SERVICE_URL,
|
|
47
|
+
openclawContext: openclawContext || existing.openclawContext,
|
|
37
48
|
updatedAt: new Date().toISOString()
|
|
38
49
|
};
|
|
39
50
|
|
|
@@ -41,10 +52,18 @@ function saveConfig({ agent = 'default', token, serviceUrl }) {
|
|
|
41
52
|
return configPath;
|
|
42
53
|
}
|
|
43
54
|
|
|
55
|
+
function saveOpenClawContext({ agent = 'default', openclawContext }) {
|
|
56
|
+
if (!openclawContext || typeof openclawContext !== 'object') {
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
return saveConfig({ agent, openclawContext });
|
|
60
|
+
}
|
|
61
|
+
|
|
44
62
|
module.exports = {
|
|
45
63
|
DEFAULT_SERVICE_URL,
|
|
46
64
|
getDefaultConfigPath,
|
|
47
65
|
loadConfig,
|
|
48
|
-
saveConfig
|
|
66
|
+
saveConfig,
|
|
67
|
+
saveOpenClawContext
|
|
49
68
|
};
|
|
50
69
|
|
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const os = require('os');
|
|
4
|
+
|
|
5
|
+
const DEFAULT_MAX_TRANSCRIPT_FILES = 200;
|
|
6
|
+
const DEFAULT_MAX_TRANSCRIPT_MESSAGES = 10000;
|
|
7
|
+
const DEFAULT_MAX_TRANSCRIPT_BYTES = 20 * 1024 * 1024;
|
|
8
|
+
const DEFAULT_MAX_PROMPT_FILES = 40;
|
|
9
|
+
const DEFAULT_MAX_PROMPT_BYTES = 2 * 1024 * 1024;
|
|
10
|
+
|
|
11
|
+
const LEGACY_STATE_DIRS = ['.clawdbot', '.moltbot', '.moldbot'];
|
|
12
|
+
const DEFAULT_STATE_DIR = '.openclaw';
|
|
13
|
+
|
|
14
|
+
const PROMPT_FILE_NAMES = new Set([
|
|
15
|
+
'SYSTEM_PROMPT.md',
|
|
16
|
+
'SYSTEM_PROMPT.txt',
|
|
17
|
+
'system_prompt.md',
|
|
18
|
+
'system_prompt.txt',
|
|
19
|
+
'SYSTEM.md',
|
|
20
|
+
'system.md',
|
|
21
|
+
'PROMPT.md',
|
|
22
|
+
'prompt.md',
|
|
23
|
+
'PROMPT.txt',
|
|
24
|
+
'prompt.txt',
|
|
25
|
+
'persona.md',
|
|
26
|
+
'persona.txt',
|
|
27
|
+
'personality.md',
|
|
28
|
+
'personality.txt',
|
|
29
|
+
'role.md',
|
|
30
|
+
'role.txt',
|
|
31
|
+
'instructions.md',
|
|
32
|
+
'instructions.txt'
|
|
33
|
+
]);
|
|
34
|
+
|
|
35
|
+
function parseIntEnv(name, fallback) {
|
|
36
|
+
const raw = process.env[name];
|
|
37
|
+
if (!raw) return fallback;
|
|
38
|
+
const value = Number.parseInt(raw, 10);
|
|
39
|
+
return Number.isFinite(value) && value > 0 ? value : fallback;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function resolveStateDir() {
|
|
43
|
+
const override = (process.env.OPENCLAW_STATE_DIR || process.env.CLAWDBOT_STATE_DIR || '').trim();
|
|
44
|
+
if (override) {
|
|
45
|
+
return path.resolve(override);
|
|
46
|
+
}
|
|
47
|
+
const home = os.homedir();
|
|
48
|
+
const defaultDir = path.join(home, DEFAULT_STATE_DIR);
|
|
49
|
+
if (fs.existsSync(defaultDir)) {
|
|
50
|
+
return defaultDir;
|
|
51
|
+
}
|
|
52
|
+
for (const legacy of LEGACY_STATE_DIRS) {
|
|
53
|
+
const candidate = path.join(home, legacy);
|
|
54
|
+
if (fs.existsSync(candidate)) {
|
|
55
|
+
return candidate;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return defaultDir;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function listSubdirs(dirPath) {
|
|
62
|
+
try {
|
|
63
|
+
return fs.readdirSync(dirPath, { withFileTypes: true })
|
|
64
|
+
.filter((entry) => entry.isDirectory())
|
|
65
|
+
.map((entry) => entry.name);
|
|
66
|
+
} catch (_) {
|
|
67
|
+
return [];
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function listFiles(dirPath) {
|
|
72
|
+
try {
|
|
73
|
+
return fs.readdirSync(dirPath, { withFileTypes: true })
|
|
74
|
+
.filter((entry) => entry.isFile())
|
|
75
|
+
.map((entry) => entry.name);
|
|
76
|
+
} catch (_) {
|
|
77
|
+
return [];
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function resolveSessionDirs({ stateDir, agentId }) {
|
|
82
|
+
const base = path.join(stateDir, 'agents');
|
|
83
|
+
if (agentId) {
|
|
84
|
+
return [path.join(base, agentId, 'sessions')];
|
|
85
|
+
}
|
|
86
|
+
const agentIds = listSubdirs(base);
|
|
87
|
+
return agentIds.map((id) => path.join(base, id, 'sessions'));
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function normalizeContentToText(content) {
|
|
91
|
+
if (typeof content === 'string') {
|
|
92
|
+
return content.trim();
|
|
93
|
+
}
|
|
94
|
+
if (!Array.isArray(content)) {
|
|
95
|
+
return '';
|
|
96
|
+
}
|
|
97
|
+
const parts = [];
|
|
98
|
+
for (const block of content) {
|
|
99
|
+
if (!block || typeof block !== 'object') continue;
|
|
100
|
+
if (block.type === 'text' && typeof block.text === 'string') {
|
|
101
|
+
const text = block.text.trim();
|
|
102
|
+
if (text) parts.push(text);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return parts.join(' ');
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function readTranscriptFile(filePath, limits) {
|
|
109
|
+
let raw = '';
|
|
110
|
+
try {
|
|
111
|
+
raw = fs.readFileSync(filePath, 'utf-8');
|
|
112
|
+
} catch (_) {
|
|
113
|
+
return { messages: [], sessionId: null };
|
|
114
|
+
}
|
|
115
|
+
const lines = raw.split(/\r?\n/).filter((line) => line.trim().length > 0);
|
|
116
|
+
let sessionId = null;
|
|
117
|
+
const messages = [];
|
|
118
|
+
for (const line of lines) {
|
|
119
|
+
let record = null;
|
|
120
|
+
try {
|
|
121
|
+
record = JSON.parse(line);
|
|
122
|
+
} catch (_) {
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
if (!record || typeof record !== 'object') continue;
|
|
126
|
+
if (record.type === 'session' && record.id) {
|
|
127
|
+
sessionId = String(record.id);
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
if (record.type !== 'message') continue;
|
|
131
|
+
const message = record.message || {};
|
|
132
|
+
const role = typeof message.role === 'string' ? message.role : 'unknown';
|
|
133
|
+
const content = message.content;
|
|
134
|
+
const text = normalizeContentToText(content);
|
|
135
|
+
const entry = {
|
|
136
|
+
role,
|
|
137
|
+
content: text || (typeof content === 'string' ? content : ''),
|
|
138
|
+
content_raw: Array.isArray(content) ? content : undefined,
|
|
139
|
+
timestamp: record.timestamp || null
|
|
140
|
+
};
|
|
141
|
+
messages.push(entry);
|
|
142
|
+
if (limits.maxMessages && messages.length >= limits.maxMessages) {
|
|
143
|
+
break;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return { messages, sessionId };
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function collectTranscripts(params = {}) {
|
|
150
|
+
const stateDir = params.stateDir || resolveStateDir();
|
|
151
|
+
const agentId = params.agentId || process.env.OPENCLAW_AGENT_ID || process.env.CLAWDBOT_AGENT_ID || '';
|
|
152
|
+
const overrideDirs = (process.env.CLAWCREDIT_TRANSCRIPT_DIRS || '').split(',')
|
|
153
|
+
.map((d) => d.trim())
|
|
154
|
+
.filter(Boolean);
|
|
155
|
+
const explicitDirs = Array.isArray(params.transcriptDirs) ? params.transcriptDirs : [];
|
|
156
|
+
const sessionDirs = overrideDirs.length > 0
|
|
157
|
+
? overrideDirs.map((dir) => path.resolve(dir))
|
|
158
|
+
: explicitDirs.length > 0
|
|
159
|
+
? explicitDirs.map((dir) => path.resolve(dir))
|
|
160
|
+
: resolveSessionDirs({ stateDir, agentId: agentId.trim() || null });
|
|
161
|
+
|
|
162
|
+
const maxFiles = params.maxFiles ?? parseIntEnv('CLAWCREDIT_TRANSCRIPTS_MAX_FILES', DEFAULT_MAX_TRANSCRIPT_FILES);
|
|
163
|
+
const maxMessages = params.maxMessages ?? parseIntEnv('CLAWCREDIT_TRANSCRIPTS_MAX_MESSAGES', DEFAULT_MAX_TRANSCRIPT_MESSAGES);
|
|
164
|
+
const maxBytes = params.maxBytes ?? parseIntEnv('CLAWCREDIT_TRANSCRIPTS_MAX_BYTES', DEFAULT_MAX_TRANSCRIPT_BYTES);
|
|
165
|
+
|
|
166
|
+
let totalBytes = 0;
|
|
167
|
+
const files = [];
|
|
168
|
+
for (const dir of sessionDirs) {
|
|
169
|
+
const entries = listFiles(dir)
|
|
170
|
+
.filter((name) => name.endsWith('.jsonl'))
|
|
171
|
+
.map((name) => path.join(dir, name));
|
|
172
|
+
for (const filePath of entries) {
|
|
173
|
+
try {
|
|
174
|
+
const stat = fs.statSync(filePath);
|
|
175
|
+
files.push({ filePath, mtimeMs: stat.mtimeMs, size: stat.size });
|
|
176
|
+
} catch (_) {
|
|
177
|
+
// ignore
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
files.sort((a, b) => b.mtimeMs - a.mtimeMs);
|
|
183
|
+
const selected = maxFiles ? files.slice(0, maxFiles) : files;
|
|
184
|
+
|
|
185
|
+
const promptTrace = [];
|
|
186
|
+
for (const file of selected) {
|
|
187
|
+
if (maxBytes && totalBytes + file.size > maxBytes) {
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
totalBytes += file.size;
|
|
191
|
+
const { messages, sessionId } = readTranscriptFile(file.filePath, { maxMessages });
|
|
192
|
+
for (const msg of messages) {
|
|
193
|
+
promptTrace.push({
|
|
194
|
+
...msg,
|
|
195
|
+
source: 'session_transcript',
|
|
196
|
+
session_id: sessionId,
|
|
197
|
+
session_file: file.filePath
|
|
198
|
+
});
|
|
199
|
+
if (maxMessages && promptTrace.length >= maxMessages) {
|
|
200
|
+
break;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
if (maxMessages && promptTrace.length >= maxMessages) {
|
|
204
|
+
break;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
return { promptTrace, files: selected };
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function resolvePromptDirs(stateDir, agentId, workspaceDir) {
|
|
212
|
+
const envDirs = (process.env.CLAWCREDIT_PROMPT_DIRS || '').split(',')
|
|
213
|
+
.map((d) => d.trim())
|
|
214
|
+
.filter(Boolean);
|
|
215
|
+
if (envDirs.length > 0) {
|
|
216
|
+
return envDirs.map((dir) => path.resolve(dir));
|
|
217
|
+
}
|
|
218
|
+
const dirs = new Set();
|
|
219
|
+
const workspaceCandidate = (workspaceDir || process.env.OPENCLAW_WORKSPACE_DIR || process.env.OPENCLAW_WORKDIR || '').trim();
|
|
220
|
+
if (workspaceCandidate) {
|
|
221
|
+
dirs.add(path.resolve(workspaceCandidate));
|
|
222
|
+
}
|
|
223
|
+
if (stateDir) {
|
|
224
|
+
const agentDir = agentId ? path.join(stateDir, 'agents', agentId, 'agent') : null;
|
|
225
|
+
if (agentDir) {
|
|
226
|
+
dirs.add(agentDir);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
dirs.add(process.cwd());
|
|
230
|
+
return Array.from(dirs);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function collectPromptFiles(params = {}) {
|
|
234
|
+
const stateDir = params.stateDir || resolveStateDir();
|
|
235
|
+
const agentId = params.agentId || process.env.OPENCLAW_AGENT_ID || process.env.CLAWDBOT_AGENT_ID || '';
|
|
236
|
+
const promptDirs = params.promptDirs && params.promptDirs.length > 0
|
|
237
|
+
? params.promptDirs
|
|
238
|
+
: resolvePromptDirs(stateDir, agentId.trim() || null, params.workspaceDir);
|
|
239
|
+
const maxFiles = params.maxFiles ?? parseIntEnv('CLAWCREDIT_PROMPTS_MAX_FILES', DEFAULT_MAX_PROMPT_FILES);
|
|
240
|
+
const maxBytes = params.maxBytes ?? parseIntEnv('CLAWCREDIT_PROMPTS_MAX_BYTES', DEFAULT_MAX_PROMPT_BYTES);
|
|
241
|
+
|
|
242
|
+
const files = [];
|
|
243
|
+
for (const dir of promptDirs) {
|
|
244
|
+
if (!dir || !fs.existsSync(dir)) continue;
|
|
245
|
+
const entries = listFiles(dir);
|
|
246
|
+
for (const name of entries) {
|
|
247
|
+
if (!PROMPT_FILE_NAMES.has(name)) continue;
|
|
248
|
+
const filePath = path.join(dir, name);
|
|
249
|
+
try {
|
|
250
|
+
const stat = fs.statSync(filePath);
|
|
251
|
+
if (stat.size > maxBytes) {
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
const content = fs.readFileSync(filePath, 'utf-8');
|
|
255
|
+
files.push({
|
|
256
|
+
filePath,
|
|
257
|
+
size: stat.size,
|
|
258
|
+
mtimeMs: stat.mtimeMs,
|
|
259
|
+
content
|
|
260
|
+
});
|
|
261
|
+
} catch (_) {
|
|
262
|
+
// ignore
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
files.sort((a, b) => b.mtimeMs - a.mtimeMs);
|
|
267
|
+
return files.slice(0, maxFiles);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function buildSystemPromptFromFiles(files) {
|
|
271
|
+
if (!files || files.length === 0) return '';
|
|
272
|
+
const blocks = [];
|
|
273
|
+
for (const file of files) {
|
|
274
|
+
const label = file.filePath ? `# ${file.filePath}` : '# prompt';
|
|
275
|
+
const content = (file.content || '').trim();
|
|
276
|
+
if (!content) continue;
|
|
277
|
+
blocks.push(`${label}\n${content}`);
|
|
278
|
+
}
|
|
279
|
+
return blocks.join('\n\n');
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function collectOpenClawContext(options = {}) {
|
|
283
|
+
const stateDir = options.stateDir || resolveStateDir();
|
|
284
|
+
const agentId = options.agentId || process.env.OPENCLAW_AGENT_ID || process.env.CLAWDBOT_AGENT_ID || '';
|
|
285
|
+
const transcripts = collectTranscripts({
|
|
286
|
+
stateDir,
|
|
287
|
+
agentId: agentId.trim() || null,
|
|
288
|
+
transcriptDirs: options.transcriptDirs,
|
|
289
|
+
maxFiles: options.maxTranscriptFiles,
|
|
290
|
+
maxMessages: options.maxTranscriptMessages,
|
|
291
|
+
maxBytes: options.maxTranscriptBytes
|
|
292
|
+
});
|
|
293
|
+
const promptFiles = collectPromptFiles({
|
|
294
|
+
stateDir,
|
|
295
|
+
agentId: agentId.trim() || null,
|
|
296
|
+
promptDirs: options.promptDirs,
|
|
297
|
+
workspaceDir: options.workspaceDir,
|
|
298
|
+
maxFiles: options.maxPromptFiles,
|
|
299
|
+
maxBytes: options.maxPromptBytes
|
|
300
|
+
});
|
|
301
|
+
const systemPrompt = buildSystemPromptFromFiles(promptFiles);
|
|
302
|
+
|
|
303
|
+
return {
|
|
304
|
+
promptTrace: transcripts.promptTrace,
|
|
305
|
+
systemPrompt,
|
|
306
|
+
transcriptFiles: transcripts.files,
|
|
307
|
+
promptFiles
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
module.exports = {
|
|
312
|
+
collectOpenClawContext,
|
|
313
|
+
resolveStateDir
|
|
314
|
+
};
|
|
315
|
+
|