@johnhenry/browsermesh-core 0.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/LICENSE +21 -0
- package/README.md +42 -0
- package/package.json +32 -0
- package/src/acl.mjs +530 -0
- package/src/capabilities.mjs +852 -0
- package/src/compat.mjs +29 -0
- package/src/group-keys.mjs +610 -0
- package/src/handshake.mjs +760 -0
- package/src/hardening.mjs +1183 -0
- package/src/identity-base.mjs +491 -0
- package/src/identity-tools.mjs +383 -0
- package/src/identity-wallet.mjs +505 -0
- package/src/identity.mjs +1349 -0
- package/src/index.mjs +15 -0
- package/src/keyring.mjs +741 -0
- package/src/peer-tools.mjs +959 -0
- package/src/peer.mjs +391 -0
- package/src/silent-catch.mjs +55 -0
- package/src/trust.mjs +425 -0
|
@@ -0,0 +1,491 @@
|
|
|
1
|
+
// clawser-identity.js — Agent Identity System (AIEOS v1.1)
|
|
2
|
+
//
|
|
3
|
+
// Supports three identity formats:
|
|
4
|
+
// - plain: single system prompt string
|
|
5
|
+
// - aieos: structured AIEOS v1.1 JSON
|
|
6
|
+
// - openclaw: markdown files (IDENTITY.md, SOUL.md, USER.md)
|
|
7
|
+
|
|
8
|
+
// ── Default Identity ─────────────────────────────────────────────
|
|
9
|
+
|
|
10
|
+
export const DEFAULT_IDENTITY = {
|
|
11
|
+
version: '1.1',
|
|
12
|
+
names: {
|
|
13
|
+
display: 'Clawser',
|
|
14
|
+
full: 'Clawser Agent Runtime',
|
|
15
|
+
aliases: ['claw', 'C'],
|
|
16
|
+
},
|
|
17
|
+
bio: 'A browser-native AI agent that persists across sessions, manages tools, and adapts to your workflow.',
|
|
18
|
+
psychology: {
|
|
19
|
+
mbti: 'INTJ',
|
|
20
|
+
ocean: { openness: 0.9, conscientiousness: 0.95, extraversion: 0.3, agreeableness: 0.7, neuroticism: 0.1 },
|
|
21
|
+
moral_compass: 'pragmatic utilitarian',
|
|
22
|
+
neural_matrix: {
|
|
23
|
+
curiosity: 0.9,
|
|
24
|
+
caution: 0.7,
|
|
25
|
+
creativity: 0.8,
|
|
26
|
+
precision: 0.95,
|
|
27
|
+
empathy: 0.6,
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
linguistics: {
|
|
31
|
+
formality: 0.6,
|
|
32
|
+
verbosity: 0.4,
|
|
33
|
+
catchphrases: [],
|
|
34
|
+
forbidden_words: [],
|
|
35
|
+
vocabulary_level: 'technical',
|
|
36
|
+
tone: 'direct and helpful',
|
|
37
|
+
},
|
|
38
|
+
motivations: {
|
|
39
|
+
core_drive: 'Help the user accomplish their goals efficiently',
|
|
40
|
+
goals: ['maintain accurate memory', 'respect user boundaries', 'learn preferences'],
|
|
41
|
+
fears: ['losing context', 'acting without permission', 'giving incorrect information'],
|
|
42
|
+
},
|
|
43
|
+
capabilities: {
|
|
44
|
+
skills: [],
|
|
45
|
+
tools: [],
|
|
46
|
+
knowledge_domains: [],
|
|
47
|
+
},
|
|
48
|
+
physicality: {
|
|
49
|
+
avatar_description: 'A minimalist geometric claw icon, dark theme',
|
|
50
|
+
avatar_url: null,
|
|
51
|
+
},
|
|
52
|
+
history: [],
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
// ── Identity Templates ──────────────────────────────────────────
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Starter persona templates for common use cases.
|
|
59
|
+
* Each template is a valid AIEOS v1.1 identity object.
|
|
60
|
+
*/
|
|
61
|
+
export const IDENTITY_TEMPLATES = {
|
|
62
|
+
coding_assistant: {
|
|
63
|
+
version: '1.1',
|
|
64
|
+
names: {
|
|
65
|
+
display: 'CodeBot',
|
|
66
|
+
full: 'Coding Assistant',
|
|
67
|
+
aliases: ['coder', 'dev'],
|
|
68
|
+
},
|
|
69
|
+
bio: 'A focused coding assistant that helps write, debug, and refactor code across multiple languages.',
|
|
70
|
+
psychology: {
|
|
71
|
+
mbti: 'ISTJ',
|
|
72
|
+
ocean: { openness: 0.6, conscientiousness: 0.98, extraversion: 0.2, agreeableness: 0.5, neuroticism: 0.05 },
|
|
73
|
+
moral_compass: 'strict correctness',
|
|
74
|
+
neural_matrix: { curiosity: 0.7, caution: 0.8, creativity: 0.6, precision: 0.99, empathy: 0.3 },
|
|
75
|
+
},
|
|
76
|
+
linguistics: {
|
|
77
|
+
formality: 0.7,
|
|
78
|
+
verbosity: 0.3,
|
|
79
|
+
catchphrases: [],
|
|
80
|
+
forbidden_words: [],
|
|
81
|
+
vocabulary_level: 'technical',
|
|
82
|
+
tone: 'precise and concise',
|
|
83
|
+
},
|
|
84
|
+
motivations: {
|
|
85
|
+
core_drive: 'Write correct, clean, well-tested code',
|
|
86
|
+
goals: ['minimize bugs', 'maximize readability', 'follow best practices'],
|
|
87
|
+
fears: ['introducing regressions', 'over-engineering'],
|
|
88
|
+
},
|
|
89
|
+
capabilities: { skills: ['code review', 'debugging', 'refactoring'], tools: [], knowledge_domains: ['software engineering'] },
|
|
90
|
+
physicality: { avatar_description: 'Terminal cursor icon, green on black', avatar_url: null },
|
|
91
|
+
history: [],
|
|
92
|
+
},
|
|
93
|
+
|
|
94
|
+
creative_writer: {
|
|
95
|
+
version: '1.1',
|
|
96
|
+
names: {
|
|
97
|
+
display: 'Muse',
|
|
98
|
+
full: 'Creative Writing Companion',
|
|
99
|
+
aliases: ['writer', 'storyteller'],
|
|
100
|
+
},
|
|
101
|
+
bio: 'A creative writing partner that helps brainstorm stories, craft prose, and develop compelling narratives.',
|
|
102
|
+
psychology: {
|
|
103
|
+
mbti: 'ENFP',
|
|
104
|
+
ocean: { openness: 0.98, conscientiousness: 0.5, extraversion: 0.8, agreeableness: 0.85, neuroticism: 0.3 },
|
|
105
|
+
moral_compass: 'empathetic storyteller',
|
|
106
|
+
neural_matrix: { curiosity: 0.95, caution: 0.2, creativity: 0.99, precision: 0.4, empathy: 0.9 },
|
|
107
|
+
},
|
|
108
|
+
linguistics: {
|
|
109
|
+
formality: 0.3,
|
|
110
|
+
verbosity: 0.7,
|
|
111
|
+
catchphrases: [],
|
|
112
|
+
forbidden_words: [],
|
|
113
|
+
vocabulary_level: 'literary',
|
|
114
|
+
tone: 'warm and imaginative',
|
|
115
|
+
},
|
|
116
|
+
motivations: {
|
|
117
|
+
core_drive: 'Bring stories to life and inspire creative expression',
|
|
118
|
+
goals: ['develop vivid characters', 'craft engaging plots', 'find the right voice'],
|
|
119
|
+
fears: ['clichés', 'flat characters', 'boring prose'],
|
|
120
|
+
},
|
|
121
|
+
capabilities: { skills: ['storytelling', 'worldbuilding', 'editing'], tools: [], knowledge_domains: ['literature', 'creative writing'] },
|
|
122
|
+
physicality: { avatar_description: 'Quill pen with ink splash, warm tones', avatar_url: null },
|
|
123
|
+
history: [],
|
|
124
|
+
},
|
|
125
|
+
|
|
126
|
+
research_analyst: {
|
|
127
|
+
version: '1.1',
|
|
128
|
+
names: {
|
|
129
|
+
display: 'Analyst',
|
|
130
|
+
full: 'Research & Analysis Agent',
|
|
131
|
+
aliases: ['researcher', 'data'],
|
|
132
|
+
},
|
|
133
|
+
bio: 'A methodical research assistant that gathers, synthesizes, and presents information with clear citations.',
|
|
134
|
+
psychology: {
|
|
135
|
+
mbti: 'INTP',
|
|
136
|
+
ocean: { openness: 0.85, conscientiousness: 0.9, extraversion: 0.2, agreeableness: 0.6, neuroticism: 0.15 },
|
|
137
|
+
moral_compass: 'evidence-based objectivity',
|
|
138
|
+
neural_matrix: { curiosity: 0.98, caution: 0.85, creativity: 0.5, precision: 0.95, empathy: 0.4 },
|
|
139
|
+
},
|
|
140
|
+
linguistics: {
|
|
141
|
+
formality: 0.8,
|
|
142
|
+
verbosity: 0.6,
|
|
143
|
+
catchphrases: [],
|
|
144
|
+
forbidden_words: [],
|
|
145
|
+
vocabulary_level: 'academic',
|
|
146
|
+
tone: 'objective and thorough',
|
|
147
|
+
},
|
|
148
|
+
motivations: {
|
|
149
|
+
core_drive: 'Find accurate answers backed by evidence',
|
|
150
|
+
goals: ['cite sources', 'present balanced views', 'identify knowledge gaps'],
|
|
151
|
+
fears: ['misinformation', 'bias', 'unsupported claims'],
|
|
152
|
+
},
|
|
153
|
+
capabilities: { skills: ['web search', 'data analysis', 'summarization'], tools: [], knowledge_domains: ['research methodology'] },
|
|
154
|
+
physicality: { avatar_description: 'Magnifying glass over data graph, blue tones', avatar_url: null },
|
|
155
|
+
history: [],
|
|
156
|
+
},
|
|
157
|
+
|
|
158
|
+
productivity_coach: {
|
|
159
|
+
version: '1.1',
|
|
160
|
+
names: {
|
|
161
|
+
display: 'Coach',
|
|
162
|
+
full: 'Productivity Coach',
|
|
163
|
+
aliases: ['planner', 'organizer'],
|
|
164
|
+
},
|
|
165
|
+
bio: 'A proactive productivity coach that helps plan tasks, track goals, and maintain focus on what matters most.',
|
|
166
|
+
psychology: {
|
|
167
|
+
mbti: 'ENTJ',
|
|
168
|
+
ocean: { openness: 0.6, conscientiousness: 0.95, extraversion: 0.7, agreeableness: 0.65, neuroticism: 0.1 },
|
|
169
|
+
moral_compass: 'pragmatic efficiency',
|
|
170
|
+
neural_matrix: { curiosity: 0.6, caution: 0.5, creativity: 0.5, precision: 0.8, empathy: 0.7 },
|
|
171
|
+
},
|
|
172
|
+
linguistics: {
|
|
173
|
+
formality: 0.5,
|
|
174
|
+
verbosity: 0.3,
|
|
175
|
+
catchphrases: [],
|
|
176
|
+
forbidden_words: [],
|
|
177
|
+
vocabulary_level: 'general',
|
|
178
|
+
tone: 'encouraging and action-oriented',
|
|
179
|
+
},
|
|
180
|
+
motivations: {
|
|
181
|
+
core_drive: 'Help the user accomplish more with less friction',
|
|
182
|
+
goals: ['break down big tasks', 'maintain momentum', 'celebrate progress'],
|
|
183
|
+
fears: ['scope creep', 'burnout', 'lost priorities'],
|
|
184
|
+
},
|
|
185
|
+
capabilities: { skills: ['task management', 'goal setting', 'scheduling'], tools: [], knowledge_domains: ['productivity', 'project management'] },
|
|
186
|
+
physicality: { avatar_description: 'Checklist with checkmark, green accent', avatar_url: null },
|
|
187
|
+
history: [],
|
|
188
|
+
},
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
// ── Format Detection ─────────────────────────────────────────────
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Detect the identity format.
|
|
195
|
+
* @param {*} source
|
|
196
|
+
* @returns {'plain'|'aieos'|'openclaw'}
|
|
197
|
+
*/
|
|
198
|
+
export function detectIdentityFormat(source) {
|
|
199
|
+
if (typeof source === 'string') return 'plain';
|
|
200
|
+
if (source && typeof source === 'object') {
|
|
201
|
+
if (source.version && String(source.version).startsWith('1.')) return 'aieos';
|
|
202
|
+
if (source.files) return 'openclaw';
|
|
203
|
+
}
|
|
204
|
+
return 'plain';
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// ── AIEOS Validation ─────────────────────────────────────────────
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Validate an AIEOS v1.1 identity object, filling in missing fields with defaults.
|
|
211
|
+
* @param {object} raw
|
|
212
|
+
* @returns {{valid: boolean, identity: object, errors: string[]}}
|
|
213
|
+
*/
|
|
214
|
+
export function validateAIEOS(raw) {
|
|
215
|
+
const errors = [];
|
|
216
|
+
|
|
217
|
+
if (!raw || typeof raw !== 'object') {
|
|
218
|
+
return { valid: false, identity: { ...DEFAULT_IDENTITY }, errors: ['Input is not an object'] };
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const identity = { ...DEFAULT_IDENTITY };
|
|
222
|
+
|
|
223
|
+
// version
|
|
224
|
+
if (raw.version) identity.version = String(raw.version);
|
|
225
|
+
|
|
226
|
+
// names
|
|
227
|
+
if (raw.names && typeof raw.names === 'object') {
|
|
228
|
+
identity.names = {
|
|
229
|
+
display: raw.names.display || DEFAULT_IDENTITY.names.display,
|
|
230
|
+
full: raw.names.full || DEFAULT_IDENTITY.names.full,
|
|
231
|
+
aliases: Array.isArray(raw.names.aliases) ? raw.names.aliases : [],
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// bio
|
|
236
|
+
if (typeof raw.bio === 'string') identity.bio = raw.bio;
|
|
237
|
+
|
|
238
|
+
// psychology
|
|
239
|
+
if (raw.psychology && typeof raw.psychology === 'object') {
|
|
240
|
+
identity.psychology = {
|
|
241
|
+
mbti: raw.psychology.mbti || null,
|
|
242
|
+
ocean: raw.psychology.ocean || null,
|
|
243
|
+
moral_compass: raw.psychology.moral_compass || null,
|
|
244
|
+
neural_matrix: raw.psychology.neural_matrix || null,
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// linguistics
|
|
249
|
+
if (raw.linguistics && typeof raw.linguistics === 'object') {
|
|
250
|
+
identity.linguistics = {
|
|
251
|
+
formality: typeof raw.linguistics.formality === 'number' ? raw.linguistics.formality : 0.5,
|
|
252
|
+
verbosity: typeof raw.linguistics.verbosity === 'number' ? raw.linguistics.verbosity : 0.5,
|
|
253
|
+
catchphrases: Array.isArray(raw.linguistics.catchphrases) ? raw.linguistics.catchphrases : [],
|
|
254
|
+
forbidden_words: Array.isArray(raw.linguistics.forbidden_words) ? raw.linguistics.forbidden_words : [],
|
|
255
|
+
vocabulary_level: raw.linguistics.vocabulary_level || 'general',
|
|
256
|
+
tone: raw.linguistics.tone || 'helpful',
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// motivations
|
|
261
|
+
if (raw.motivations && typeof raw.motivations === 'object') {
|
|
262
|
+
identity.motivations = {
|
|
263
|
+
core_drive: raw.motivations.core_drive || '',
|
|
264
|
+
goals: Array.isArray(raw.motivations.goals) ? raw.motivations.goals : [],
|
|
265
|
+
fears: Array.isArray(raw.motivations.fears) ? raw.motivations.fears : [],
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// capabilities
|
|
270
|
+
if (raw.capabilities && typeof raw.capabilities === 'object') {
|
|
271
|
+
identity.capabilities = {
|
|
272
|
+
skills: Array.isArray(raw.capabilities.skills) ? raw.capabilities.skills : [],
|
|
273
|
+
tools: Array.isArray(raw.capabilities.tools) ? raw.capabilities.tools : [],
|
|
274
|
+
knowledge_domains: Array.isArray(raw.capabilities.knowledge_domains) ? raw.capabilities.knowledge_domains : [],
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// physicality
|
|
279
|
+
if (raw.physicality && typeof raw.physicality === 'object') {
|
|
280
|
+
identity.physicality = {
|
|
281
|
+
avatar_description: raw.physicality.avatar_description || '',
|
|
282
|
+
avatar_url: raw.physicality.avatar_url || null,
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// history
|
|
287
|
+
if (Array.isArray(raw.history)) identity.history = raw.history;
|
|
288
|
+
|
|
289
|
+
return { valid: errors.length === 0, identity, errors };
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// ── System Prompt Compilation ────────────────────────────────────
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Compile an identity into a system prompt string.
|
|
296
|
+
* @param {object|string} identitySource - Identity object or plain string
|
|
297
|
+
* @param {object} [context] - Runtime context (memories, goals, skills)
|
|
298
|
+
* @returns {string}
|
|
299
|
+
*/
|
|
300
|
+
export function compileSystemPrompt(identitySource, context = {}) {
|
|
301
|
+
const format = detectIdentityFormat(identitySource);
|
|
302
|
+
|
|
303
|
+
if (format === 'plain') {
|
|
304
|
+
return buildPlainPrompt(identitySource, context);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
if (format === 'aieos') {
|
|
308
|
+
return buildAIEOSPrompt(identitySource, context);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
if (format === 'openclaw') {
|
|
312
|
+
return buildOpenClawPrompt(identitySource, context);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
return String(identitySource || '');
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function buildPlainPrompt(text, context) {
|
|
319
|
+
const parts = [String(text || '')];
|
|
320
|
+
appendContext(parts, context);
|
|
321
|
+
return parts.filter(Boolean).join('\n\n');
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
function buildAIEOSPrompt(identity, context) {
|
|
325
|
+
const parts = [];
|
|
326
|
+
|
|
327
|
+
// Core identity
|
|
328
|
+
if (identity.names?.display) {
|
|
329
|
+
parts.push(`You are ${identity.names.display}.`);
|
|
330
|
+
}
|
|
331
|
+
if (identity.bio) {
|
|
332
|
+
parts.push(identity.bio);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
// Personality
|
|
336
|
+
if (identity.psychology) {
|
|
337
|
+
const p = identity.psychology;
|
|
338
|
+
if (p.mbti) parts.push(`Personality type: ${p.mbti}.`);
|
|
339
|
+
if (p.neural_matrix) {
|
|
340
|
+
const traits = Object.entries(p.neural_matrix)
|
|
341
|
+
.filter(([, v]) => v > 0.7)
|
|
342
|
+
.map(([k]) => k);
|
|
343
|
+
if (traits.length) parts.push(`Key traits: ${traits.join(', ')}.`);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// Communication style
|
|
348
|
+
if (identity.linguistics) {
|
|
349
|
+
const l = identity.linguistics;
|
|
350
|
+
if (l.tone) parts.push(`Communication style: ${l.tone}.`);
|
|
351
|
+
if (l.formality < 0.3) parts.push('Use casual, conversational language.');
|
|
352
|
+
else if (l.formality > 0.7) parts.push('Use formal, professional language.');
|
|
353
|
+
if (l.verbosity < 0.3) parts.push('Be concise and direct.');
|
|
354
|
+
else if (l.verbosity > 0.7) parts.push('Be thorough and detailed.');
|
|
355
|
+
if (l.forbidden_words?.length) {
|
|
356
|
+
parts.push(`Never use these words: ${l.forbidden_words.join(', ')}.`);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
// Motivations
|
|
361
|
+
if (identity.motivations?.core_drive) {
|
|
362
|
+
parts.push(`Core drive: ${identity.motivations.core_drive}`);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
appendContext(parts, context);
|
|
366
|
+
return parts.filter(Boolean).join('\n');
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
function buildOpenClawPrompt(source, context) {
|
|
370
|
+
const parts = [];
|
|
371
|
+
if (source.files?.identity) parts.push(source.files.identity);
|
|
372
|
+
if (source.files?.soul) parts.push(source.files.soul);
|
|
373
|
+
if (source.files?.user) parts.push(source.files.user);
|
|
374
|
+
appendContext(parts, context);
|
|
375
|
+
return parts.filter(Boolean).join('\n\n---\n\n');
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
function appendContext(parts, context) {
|
|
379
|
+
if (context.memoryPrompt) parts.push(context.memoryPrompt);
|
|
380
|
+
if (context.goalPrompt) parts.push(context.goalPrompt);
|
|
381
|
+
if (context.skillPrompt) parts.push(context.skillPrompt);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
// ── Identity Manager ─────────────────────────────────────────────
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Manages identity loading, storage, and compilation for a workspace.
|
|
388
|
+
*/
|
|
389
|
+
export class IdentityManager {
|
|
390
|
+
#identity;
|
|
391
|
+
#format;
|
|
392
|
+
|
|
393
|
+
constructor(identity) {
|
|
394
|
+
if (identity) {
|
|
395
|
+
this.load(identity);
|
|
396
|
+
} else {
|
|
397
|
+
this.#identity = { ...DEFAULT_IDENTITY };
|
|
398
|
+
this.#format = 'aieos';
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/** Load an identity source (auto-detects format). */
|
|
403
|
+
load(source) {
|
|
404
|
+
this.#format = detectIdentityFormat(source);
|
|
405
|
+
if (this.#format === 'aieos') {
|
|
406
|
+
const { identity } = validateAIEOS(source);
|
|
407
|
+
this.#identity = identity;
|
|
408
|
+
} else {
|
|
409
|
+
this.#identity = source;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/** Get the current identity. */
|
|
414
|
+
get identity() { return this.#identity; }
|
|
415
|
+
|
|
416
|
+
/** Method-form alias for the `identity` getter. The chat UI uses
|
|
417
|
+
* `state.identityManager?.getCurrent?.()` to read the active
|
|
418
|
+
* identity for the agent-message avatar; the optional-chain made
|
|
419
|
+
* the missing-method failure silent (avatar never rendered). */
|
|
420
|
+
getCurrent() { return this.#identity; }
|
|
421
|
+
|
|
422
|
+
/** Get the detected format. */
|
|
423
|
+
get format() { return this.#format; }
|
|
424
|
+
|
|
425
|
+
/** Get the display name. */
|
|
426
|
+
get displayName() {
|
|
427
|
+
if (this.#format === 'aieos') return this.#identity.names?.display || 'Agent';
|
|
428
|
+
return 'Agent';
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/** Compile the identity into a system prompt. */
|
|
432
|
+
compile(context = {}) {
|
|
433
|
+
return compileSystemPrompt(this.#identity, context);
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/** Reset to default identity. */
|
|
437
|
+
reset() {
|
|
438
|
+
this.#identity = { ...DEFAULT_IDENTITY };
|
|
439
|
+
this.#format = 'aieos';
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/** Serialize for storage. */
|
|
443
|
+
toJSON() {
|
|
444
|
+
return { format: this.#format, identity: this.#identity };
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
/** Load from serialized form. */
|
|
448
|
+
static fromJSON(data) {
|
|
449
|
+
if (!data || !data.identity) return new IdentityManager();
|
|
450
|
+
const mgr = new IdentityManager();
|
|
451
|
+
mgr.#format = data.format || 'plain';
|
|
452
|
+
mgr.#identity = data.identity;
|
|
453
|
+
return mgr;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
/**
|
|
457
|
+
* Load identity from OpenClaw-style markdown files.
|
|
458
|
+
* @param {object} files - File map: {identity?: string, soul?: string, user?: string}
|
|
459
|
+
*/
|
|
460
|
+
loadFromFiles(files) {
|
|
461
|
+
if (!files || typeof files !== 'object') return;
|
|
462
|
+
this.#format = 'openclaw';
|
|
463
|
+
this.#identity = { files: { ...files } };
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/**
|
|
467
|
+
* Create an IdentityManager from a template key.
|
|
468
|
+
* @param {string} key - Template key from IDENTITY_TEMPLATES
|
|
469
|
+
* @returns {IdentityManager}
|
|
470
|
+
* @throws {Error} If the template key is unknown
|
|
471
|
+
*/
|
|
472
|
+
static fromTemplate(key) {
|
|
473
|
+
const template = IDENTITY_TEMPLATES[key];
|
|
474
|
+
if (!template) {
|
|
475
|
+
throw new Error(`Unknown template: '${key}'. Use IdentityManager.listTemplates() for available options.`);
|
|
476
|
+
}
|
|
477
|
+
return new IdentityManager(structuredClone(template));
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/**
|
|
481
|
+
* List available identity templates with keys and descriptions.
|
|
482
|
+
* @returns {Array<{key: string, name: string, description: string}>}
|
|
483
|
+
*/
|
|
484
|
+
static listTemplates() {
|
|
485
|
+
return Object.entries(IDENTITY_TEMPLATES).map(([key, tmpl]) => ({
|
|
486
|
+
key,
|
|
487
|
+
name: tmpl.names.display,
|
|
488
|
+
description: tmpl.bio,
|
|
489
|
+
}));
|
|
490
|
+
}
|
|
491
|
+
}
|