@molroo-io/sdk 0.8.4 → 0.9.1
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/cjs/errors.d.ts +1 -1
- package/dist/cjs/errors.d.ts.map +1 -1
- package/dist/cjs/errors.js +2 -1
- package/dist/cjs/events/types.d.ts +1 -1
- package/dist/cjs/events/types.d.ts.map +1 -1
- package/dist/cjs/index.d.ts +4 -2
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +5 -1
- package/dist/cjs/llm/resolve.d.ts.map +1 -1
- package/dist/cjs/llm/resolve.js +2 -1
- package/dist/cjs/llm/schema.d.ts +0 -114
- package/dist/cjs/llm/schema.d.ts.map +1 -1
- package/dist/cjs/llm/schema.js +1 -44
- package/dist/cjs/persona/chat-orchestrator.d.ts +18 -3
- package/dist/cjs/persona/chat-orchestrator.d.ts.map +1 -1
- package/dist/cjs/persona/chat-orchestrator.js +29 -109
- package/dist/cjs/persona/conversation.d.ts +84 -0
- package/dist/cjs/persona/conversation.d.ts.map +1 -0
- package/dist/cjs/persona/conversation.js +72 -0
- package/dist/cjs/persona/memory-pipeline.d.ts.map +1 -1
- package/dist/cjs/persona/memory-pipeline.js +30 -4
- package/dist/cjs/persona.d.ts +441 -22
- package/dist/cjs/persona.d.ts.map +1 -1
- package/dist/cjs/persona.js +418 -6
- package/dist/cjs/shared/errors.d.ts +32 -2
- package/dist/cjs/shared/errors.d.ts.map +1 -1
- package/dist/cjs/shared/errors.js +33 -2
- package/dist/cjs/types.d.ts +70 -5
- package/dist/cjs/types.d.ts.map +1 -1
- package/dist/cjs/types.js +2 -1
- package/dist/cjs/world/world-persona.d.ts +20 -5
- package/dist/cjs/world/world-persona.d.ts.map +1 -1
- package/dist/cjs/world/world-persona.js +21 -5
- package/dist/cjs/world/world.d.ts +28 -5
- package/dist/cjs/world/world.d.ts.map +1 -1
- package/dist/cjs/world/world.js +29 -3
- package/dist/esm/errors.d.ts +1 -1
- package/dist/esm/errors.d.ts.map +1 -1
- package/dist/esm/errors.js +1 -1
- package/dist/esm/events/types.d.ts +1 -1
- package/dist/esm/events/types.d.ts.map +1 -1
- package/dist/esm/index.d.ts +4 -2
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +3 -1
- package/dist/esm/llm/resolve.d.ts.map +1 -1
- package/dist/esm/llm/resolve.js +2 -1
- package/dist/esm/llm/schema.d.ts +0 -114
- package/dist/esm/llm/schema.d.ts.map +1 -1
- package/dist/esm/llm/schema.js +0 -43
- package/dist/esm/persona/chat-orchestrator.d.ts +18 -3
- package/dist/esm/persona/chat-orchestrator.d.ts.map +1 -1
- package/dist/esm/persona/chat-orchestrator.js +29 -109
- package/dist/esm/persona/conversation.d.ts +84 -0
- package/dist/esm/persona/conversation.d.ts.map +1 -0
- package/dist/esm/persona/conversation.js +68 -0
- package/dist/esm/persona/memory-pipeline.d.ts.map +1 -1
- package/dist/esm/persona/memory-pipeline.js +30 -4
- package/dist/esm/persona.d.ts +441 -22
- package/dist/esm/persona.d.ts.map +1 -1
- package/dist/esm/persona.js +418 -7
- package/dist/esm/shared/errors.d.ts +32 -2
- package/dist/esm/shared/errors.d.ts.map +1 -1
- package/dist/esm/shared/errors.js +32 -1
- package/dist/esm/types.d.ts +70 -5
- package/dist/esm/types.d.ts.map +1 -1
- package/dist/esm/types.js +1 -1
- package/dist/esm/world/world-persona.d.ts +20 -5
- package/dist/esm/world/world-persona.d.ts.map +1 -1
- package/dist/esm/world/world-persona.js +21 -5
- package/dist/esm/world/world.d.ts +28 -5
- package/dist/esm/world/world.d.ts.map +1 -1
- package/dist/esm/world/world.js +29 -3
- package/package.json +1 -1
|
@@ -1,10 +1,30 @@
|
|
|
1
1
|
import { emitResponseEvents } from './event-emitter';
|
|
2
|
+
/** Emit pipeline error or log warning if no EventAdapter. */
|
|
3
|
+
function handlePipelineError(deps, stage, error) {
|
|
4
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
5
|
+
if (deps.events) {
|
|
6
|
+
deps.events.emit({
|
|
7
|
+
type: 'pipeline_error',
|
|
8
|
+
personaId: deps.personaId,
|
|
9
|
+
timestamp: Date.now(),
|
|
10
|
+
payload: { stage, error: message },
|
|
11
|
+
}).catch(() => {
|
|
12
|
+
// Last resort: log to console if event emit itself fails
|
|
13
|
+
console.warn(`[molroo-sdk] Pipeline error (${stage}):`, message);
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
console.warn(`[molroo-sdk] Pipeline error (${stage}):`, message);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
2
20
|
/** Save episode (shared between perceive and chat pipelines). */
|
|
3
21
|
function saveEpisode(deps, response) {
|
|
4
22
|
if (!response.memoryEpisode)
|
|
5
23
|
return;
|
|
6
24
|
if (deps.memoryAdapter) {
|
|
7
|
-
deps.memoryAdapter.saveEpisode(response.memoryEpisode).catch(() => {
|
|
25
|
+
deps.memoryAdapter.saveEpisode(response.memoryEpisode).catch((err) => {
|
|
26
|
+
handlePipelineError(deps, 'save_episode', err);
|
|
27
|
+
});
|
|
8
28
|
}
|
|
9
29
|
}
|
|
10
30
|
/** Handle reflection generation (LLM call + save). */
|
|
@@ -44,7 +64,9 @@ export function postPerceive(deps, response) {
|
|
|
44
64
|
return;
|
|
45
65
|
saveEpisode(deps, response);
|
|
46
66
|
if (deps.events) {
|
|
47
|
-
emitResponseEvents(deps.events, deps.personaId, response, Date.now()).catch(() => {
|
|
67
|
+
emitResponseEvents(deps.events, deps.personaId, response, Date.now()).catch((err) => {
|
|
68
|
+
handlePipelineError(deps, 'event_emit', err);
|
|
69
|
+
});
|
|
48
70
|
}
|
|
49
71
|
}
|
|
50
72
|
/**
|
|
@@ -57,9 +79,13 @@ export function postChat(deps, response) {
|
|
|
57
79
|
const hasLlm = deps.engineLlm || deps.llm;
|
|
58
80
|
const hasMemory = deps.memoryAdapter;
|
|
59
81
|
if (hasLlm && hasMemory && response.reflectionPrompt) {
|
|
60
|
-
handleReflection(deps, response.reflectionPrompt, response.emotion.vad).catch(() => {
|
|
82
|
+
handleReflection(deps, response.reflectionPrompt, response.emotion.vad).catch((err) => {
|
|
83
|
+
handlePipelineError(deps, 'reflection', err);
|
|
84
|
+
});
|
|
61
85
|
}
|
|
62
86
|
if (deps.events) {
|
|
63
|
-
emitResponseEvents(deps.events, deps.personaId, response, now).catch(() => {
|
|
87
|
+
emitResponseEvents(deps.events, deps.personaId, response, now).catch((err) => {
|
|
88
|
+
handlePipelineError(deps, 'event_emit', err);
|
|
89
|
+
});
|
|
64
90
|
}
|
|
65
91
|
}
|
package/dist/esm/persona.d.ts
CHANGED
|
@@ -2,7 +2,9 @@ import type { LLMAdapter, Message } from './llm/adapter';
|
|
|
2
2
|
import { type LLMInput } from './llm/resolve';
|
|
3
3
|
import type { MemoryAdapter, RecallLimits } from './memory/types';
|
|
4
4
|
import type { EventAdapter } from './events/types';
|
|
5
|
-
import type { AgentResponse,
|
|
5
|
+
import type { AgentResponse, AppraisalVector, InterlocutorContext, RelationshipContext, PersonaSnapshot, PersonaConfigData, PerceiveOptions, PersonaIdentity } from './types';
|
|
6
|
+
import type { StyleProfile } from './expression';
|
|
7
|
+
import { Conversation, type ConversationOptions } from './persona/conversation';
|
|
6
8
|
/** Configuration for connecting to a standalone persona instance. */
|
|
7
9
|
export interface MolrooPersonaConfig {
|
|
8
10
|
/** Base URL of the molroo API. Defaults to 'https://api.molroo.io'. */
|
|
@@ -30,8 +32,6 @@ export interface MolrooPersonaConfig {
|
|
|
30
32
|
* memory_consolidated, reflection_generated, etc.).
|
|
31
33
|
*/
|
|
32
34
|
events?: EventAdapter;
|
|
33
|
-
/** Appraisal generation mode for `chat()`. Defaults to `direct`. */
|
|
34
|
-
appraisalMode?: AppraisalMode;
|
|
35
35
|
}
|
|
36
36
|
/** Summary of a persona instance, returned by list operations. */
|
|
37
37
|
export interface PersonaSummary {
|
|
@@ -41,37 +41,140 @@ export interface PersonaSummary {
|
|
|
41
41
|
engineVersion: string;
|
|
42
42
|
createdAt: number;
|
|
43
43
|
}
|
|
44
|
-
/**
|
|
44
|
+
/**
|
|
45
|
+
* Current emotional and psychological state of a persona, as returned by {@link MolrooPersona.getState}.
|
|
46
|
+
*
|
|
47
|
+
* This is the primary data structure apps use to assemble system prompts without relying on
|
|
48
|
+
* `chat()`. All subsystems computed by the emotion engine are included here.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```typescript
|
|
52
|
+
* const state = await persona.getState();
|
|
53
|
+
*
|
|
54
|
+
* // Build your own system prompt
|
|
55
|
+
* const systemPrompt = [
|
|
56
|
+
* `You are ${myPersonaName}.`,
|
|
57
|
+
* `Current emotion: ${state.emotion.discrete?.primary} (intensity ${state.emotion.discrete?.intensity.toFixed(2)})`,
|
|
58
|
+
* state.mood ? `Mood: V=${state.mood.V.toFixed(2)} A=${state.mood.A.toFixed(2)}` : '',
|
|
59
|
+
* state.selfEsteem ? `Self-esteem: ${state.selfEsteem.global.toFixed(2)}` : '',
|
|
60
|
+
* ].filter(Boolean).join('\n');
|
|
61
|
+
*
|
|
62
|
+
* const { text } = await myLLM.generate({ system: systemPrompt, messages });
|
|
63
|
+
* await persona.hear(lastUserMessage);
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
45
66
|
export interface PersonaState {
|
|
46
|
-
/**
|
|
67
|
+
/**
|
|
68
|
+
* Current emotion in VAD (Valence-Arousal-Dominance) space with discrete label.
|
|
69
|
+
* Use this to communicate the persona's felt emotional tone to the LLM.
|
|
70
|
+
* - `vad.V`: Valence (−1 = negative, +1 = positive)
|
|
71
|
+
* - `vad.A`: Arousal (0 = calm, 1 = activated)
|
|
72
|
+
* - `vad.D`: Dominance (0 = submissive, 1 = dominant)
|
|
73
|
+
* - `discrete.primary`: Named emotion label (e.g. "joy", "anger", "sadness")
|
|
74
|
+
*/
|
|
47
75
|
emotion: {
|
|
48
76
|
vad: {
|
|
49
77
|
V: number;
|
|
50
78
|
A: number;
|
|
51
79
|
D: number;
|
|
52
80
|
};
|
|
53
|
-
discrete
|
|
81
|
+
discrete: {
|
|
54
82
|
primary: string;
|
|
55
83
|
secondary?: string;
|
|
56
84
|
intensity: number;
|
|
57
85
|
};
|
|
58
86
|
};
|
|
59
|
-
/**
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
87
|
+
/**
|
|
88
|
+
* Soul stage — the persona's current developmental/personality phase.
|
|
89
|
+
* Governs the blending ratio between rational and irrational response styles.
|
|
90
|
+
* Include in prompts to modulate how impulsive or deliberate the persona acts.
|
|
91
|
+
*/
|
|
92
|
+
soulStage: {
|
|
93
|
+
id: number;
|
|
94
|
+
name: string;
|
|
95
|
+
blendTable: {
|
|
96
|
+
rational: number;
|
|
97
|
+
irrational: number;
|
|
65
98
|
};
|
|
99
|
+
turnsInStage: number;
|
|
66
100
|
};
|
|
67
|
-
/**
|
|
68
|
-
|
|
69
|
-
|
|
101
|
+
/**
|
|
102
|
+
* Persona mask — the gap between true emotion and presented face.
|
|
103
|
+
* - `integrity`: 0 (mask fully intact) to 1 (mask broken, raw emotion exposed)
|
|
104
|
+
* - `state`: "stable" | "cracking" | "broken"
|
|
105
|
+
* Low integrity indicates emotional leakage — the persona may show truer feelings.
|
|
106
|
+
*/
|
|
107
|
+
mask: {
|
|
108
|
+
integrity: number;
|
|
109
|
+
state: string;
|
|
110
|
+
};
|
|
111
|
+
/**
|
|
112
|
+
* Background mood — slower-changing than emotion, sets the ambient emotional color.
|
|
113
|
+
* VAD values directly (not nested): `{ V, A, D }`.
|
|
114
|
+
* Useful for setting a sustained tone across a conversation (e.g., melancholy all day).
|
|
115
|
+
*/
|
|
116
|
+
mood?: {
|
|
117
|
+
V: number;
|
|
118
|
+
A: number;
|
|
119
|
+
D: number;
|
|
120
|
+
};
|
|
121
|
+
/**
|
|
122
|
+
* Active somatic (body) sensations as intensity values.
|
|
123
|
+
* Keys are sensation names (e.g. "chest_tightness", "warmth"), values are 0–1 intensities.
|
|
124
|
+
* Use this to add embodied, physical descriptions to the persona's responses.
|
|
125
|
+
*/
|
|
126
|
+
somatic?: {
|
|
127
|
+
[key: string]: number;
|
|
128
|
+
};
|
|
129
|
+
/**
|
|
130
|
+
* Narrative self-perception — how the persona understands their own story.
|
|
131
|
+
* - `tone`: −1 (tragic) to +1 (hopeful)
|
|
132
|
+
* - `agency`: 0 (helpless) to 1 (in control)
|
|
133
|
+
* - `coherence`: 0 (fragmented) to 1 (integrated)
|
|
134
|
+
* High agency + high tone = optimistic protagonist. Low agency + low tone = victim narrative.
|
|
135
|
+
*/
|
|
70
136
|
narrative?: {
|
|
71
137
|
tone: number;
|
|
72
138
|
agency: number;
|
|
73
139
|
coherence: number;
|
|
74
140
|
};
|
|
141
|
+
/**
|
|
142
|
+
* Active emotion regulation strategy.
|
|
143
|
+
* `null` if the persona is not actively regulating.
|
|
144
|
+
* Include to let the LLM know whether the persona is suppressing, reappraising, etc.
|
|
145
|
+
*/
|
|
146
|
+
regulation?: {
|
|
147
|
+
activeStrategy: string | null;
|
|
148
|
+
};
|
|
149
|
+
/**
|
|
150
|
+
* Global self-esteem level (0–1).
|
|
151
|
+
* Low self-esteem shapes how the persona responds to praise, criticism, and challenges.
|
|
152
|
+
*/
|
|
153
|
+
selfEsteem?: {
|
|
154
|
+
global: number;
|
|
155
|
+
};
|
|
156
|
+
/**
|
|
157
|
+
* Terror Management Theory (TMT) state — existential anxiety subsystem.
|
|
158
|
+
* - `mortalitySalience`: how front-of-mind death awareness is (0–1)
|
|
159
|
+
* - `deathAnxiety`: underlying anxiety level (0–1)
|
|
160
|
+
* - `defenseMode`: "proximal" (denial) | "distal" (worldview defense) | "none"
|
|
161
|
+
*/
|
|
162
|
+
tmt?: {
|
|
163
|
+
mortalitySalience: number;
|
|
164
|
+
deathAnxiety: number;
|
|
165
|
+
defenseMode: 'proximal' | 'distal' | 'none';
|
|
166
|
+
};
|
|
167
|
+
/**
|
|
168
|
+
* Relationship state with the current interlocutor.
|
|
169
|
+
* - `trust`: 0–1
|
|
170
|
+
* - `intimacy`: 0–1
|
|
171
|
+
* - `attachmentStyle`: e.g. "secure" | "anxious" | "avoidant"
|
|
172
|
+
*/
|
|
173
|
+
relationship?: {
|
|
174
|
+
trust: number;
|
|
175
|
+
intimacy: number;
|
|
176
|
+
attachmentStyle?: string;
|
|
177
|
+
};
|
|
75
178
|
}
|
|
76
179
|
/** Result of a persona chat interaction, including LLM text and emotion data. */
|
|
77
180
|
export interface PersonaChatResult {
|
|
@@ -97,12 +200,13 @@ export declare class MolrooPersona {
|
|
|
97
200
|
private memoryRecallConfig;
|
|
98
201
|
private events;
|
|
99
202
|
private _personaId;
|
|
100
|
-
private appraisalMode;
|
|
101
203
|
constructor(config: MolrooPersonaConfig & {
|
|
102
204
|
llm?: LLMAdapter;
|
|
103
205
|
engineLlm?: LLMAdapter;
|
|
104
206
|
});
|
|
207
|
+
/** Unique persona instance ID. */
|
|
105
208
|
get id(): string;
|
|
209
|
+
/** @deprecated Use {@link id} instead. */
|
|
106
210
|
get personaId(): string;
|
|
107
211
|
/**
|
|
108
212
|
* Create a new persona from a natural language description.
|
|
@@ -124,7 +228,6 @@ export declare class MolrooPersona {
|
|
|
124
228
|
memory?: MemoryAdapter;
|
|
125
229
|
recall?: RecallLimits;
|
|
126
230
|
events?: EventAdapter;
|
|
127
|
-
appraisalMode?: AppraisalMode;
|
|
128
231
|
}, description: string): Promise<MolrooPersona>;
|
|
129
232
|
/**
|
|
130
233
|
* Create a new persona with explicit configuration.
|
|
@@ -145,7 +248,6 @@ export declare class MolrooPersona {
|
|
|
145
248
|
memory?: MemoryAdapter;
|
|
146
249
|
recall?: RecallLimits;
|
|
147
250
|
events?: EventAdapter;
|
|
148
|
-
appraisalMode?: AppraisalMode;
|
|
149
251
|
}, personaConfig: PersonaConfigData): Promise<MolrooPersona>;
|
|
150
252
|
/**
|
|
151
253
|
* Internal implementation for creating persona with resolved config.
|
|
@@ -164,8 +266,24 @@ export declare class MolrooPersona {
|
|
|
164
266
|
memory?: MemoryAdapter;
|
|
165
267
|
recall?: RecallLimits;
|
|
166
268
|
events?: EventAdapter;
|
|
167
|
-
appraisalMode?: AppraisalMode;
|
|
168
269
|
}, description: string): Promise<MolrooPersona>;
|
|
270
|
+
/**
|
|
271
|
+
* Connect to an existing persona by ID.
|
|
272
|
+
*
|
|
273
|
+
* @param config - Connection configuration (apiKey required).
|
|
274
|
+
* @param personaId - The persona instance ID to connect to.
|
|
275
|
+
* @returns A connected MolrooPersona instance.
|
|
276
|
+
*
|
|
277
|
+
* @deprecated Use {@link Molroo.connectPersona} instead.
|
|
278
|
+
*
|
|
279
|
+
* @example
|
|
280
|
+
* ```typescript
|
|
281
|
+
* const sera = await MolrooPersona.connect(
|
|
282
|
+
* { apiKey: 'mk_...', llm: openaiAdapter },
|
|
283
|
+
* 'persona_abc123'
|
|
284
|
+
* );
|
|
285
|
+
* ```
|
|
286
|
+
*/
|
|
169
287
|
static connect(config: {
|
|
170
288
|
baseUrl?: string;
|
|
171
289
|
apiKey: string;
|
|
@@ -174,8 +292,13 @@ export declare class MolrooPersona {
|
|
|
174
292
|
memory?: MemoryAdapter;
|
|
175
293
|
recall?: RecallLimits;
|
|
176
294
|
events?: EventAdapter;
|
|
177
|
-
appraisalMode?: AppraisalMode;
|
|
178
295
|
}, personaId: string): Promise<MolrooPersona>;
|
|
296
|
+
/**
|
|
297
|
+
* List all personas for the authenticated tenant.
|
|
298
|
+
*
|
|
299
|
+
* @param config - API connection config.
|
|
300
|
+
* @returns Paginated list of persona summaries.
|
|
301
|
+
*/
|
|
179
302
|
static listPersonas(config: {
|
|
180
303
|
baseUrl?: string;
|
|
181
304
|
apiKey: string;
|
|
@@ -183,23 +306,158 @@ export declare class MolrooPersona {
|
|
|
183
306
|
personas: PersonaSummary[];
|
|
184
307
|
nextCursor?: string;
|
|
185
308
|
}>;
|
|
309
|
+
/**
|
|
310
|
+
* Send a stimulus to the persona's emotion engine and get the updated emotional response.
|
|
311
|
+
* This is the low-level API — for common cases, prefer {@link hear}, {@link experience}, or {@link chat}.
|
|
312
|
+
*
|
|
313
|
+
* @param message - The stimulus text (user message, event description, etc.).
|
|
314
|
+
* @param options - Appraisal, source, relationship context, and other options.
|
|
315
|
+
* @returns Emotion engine response with VAD, discrete emotion, and side effects.
|
|
316
|
+
*/
|
|
186
317
|
perceive(message: string, options?: PerceiveOptions): Promise<AgentResponse>;
|
|
318
|
+
/**
|
|
319
|
+
* Send a custom event with a required appraisal vector.
|
|
320
|
+
*
|
|
321
|
+
* @param type - Custom event type (e.g., 'gift_received', 'argument').
|
|
322
|
+
* @param description - Human-readable event description.
|
|
323
|
+
* @param options - Must include a pre-computed appraisal vector.
|
|
324
|
+
* @returns Emotion engine response.
|
|
325
|
+
*/
|
|
187
326
|
event(type: string, description: string, options: Omit<PerceiveOptions, 'type'> & {
|
|
188
327
|
appraisal: AppraisalVector;
|
|
189
328
|
}): Promise<AgentResponse>;
|
|
329
|
+
/**
|
|
330
|
+
* Register a received message with the emotion engine and update the persona's emotional state.
|
|
331
|
+
*
|
|
332
|
+
* This is the **core API** for the `hear() + getState()` pattern. Apps that build their own
|
|
333
|
+
* LLM prompts should call `hear()` after each user message to keep the emotion engine in sync,
|
|
334
|
+
* then call `getState()` to read updated state for prompt assembly.
|
|
335
|
+
*
|
|
336
|
+
* Use {@link chat} when you want the SDK to handle prompt assembly and LLM orchestration.
|
|
337
|
+
* Use `hear()` + `getState()` when your app owns the LLM call.
|
|
338
|
+
*
|
|
339
|
+
* @param message - The incoming message text (user utterance, narration, etc.).
|
|
340
|
+
* @param from - Who sent the message. A plain string is used as the source entity name.
|
|
341
|
+
* Pass an {@link InterlocutorContext} object for richer context (description, extensions).
|
|
342
|
+
* @returns Emotion engine response with updated VAD, discrete emotion, and any side effects
|
|
343
|
+
* (memory episodes, social updates, stage transitions, etc.).
|
|
344
|
+
*
|
|
345
|
+
* @example Basic hear + getState pattern
|
|
346
|
+
* ```typescript
|
|
347
|
+
* // Step 1: register message with emotion engine
|
|
348
|
+
* await persona.hear('You did a great job today!', 'Alice');
|
|
349
|
+
*
|
|
350
|
+
* // Step 2: read updated psychological state
|
|
351
|
+
* const state = await persona.getState();
|
|
352
|
+
*
|
|
353
|
+
* // Step 3: assemble your own system prompt
|
|
354
|
+
* const systemPrompt = buildPrompt(myPersonaConfig, state);
|
|
355
|
+
* const { text } = await myLLM.generate({ system: systemPrompt, messages });
|
|
356
|
+
* ```
|
|
357
|
+
*
|
|
358
|
+
* @example With structured interlocutor context
|
|
359
|
+
* ```typescript
|
|
360
|
+
* await persona.hear('I need your help', {
|
|
361
|
+
* name: 'Alice',
|
|
362
|
+
* description: 'A regular customer, slightly anxious',
|
|
363
|
+
* extensions: { mood: 'stressed' },
|
|
364
|
+
* });
|
|
365
|
+
* ```
|
|
366
|
+
*/
|
|
367
|
+
hear(message: string, from?: string | InterlocutorContext): Promise<AgentResponse>;
|
|
368
|
+
/**
|
|
369
|
+
* Perceive an environmental or narrative event with a pre-computed appraisal.
|
|
370
|
+
*
|
|
371
|
+
* @param description - What happened (e.g., 'The sun set over the ocean').
|
|
372
|
+
* @param appraisal - 9-dimensional Scherer CPM appraisal vector.
|
|
373
|
+
* @returns Emotion engine response.
|
|
374
|
+
*
|
|
375
|
+
* @example
|
|
376
|
+
* ```typescript
|
|
377
|
+
* const response = await persona.experience('Received a surprise gift', {
|
|
378
|
+
* goal_relevance: 0.8, goal_congruence: 0.9, expectedness: 0.2,
|
|
379
|
+
* controllability: 0.5, agency: 0.3, norm_compatibility: 0.8,
|
|
380
|
+
* internal_standards: 0.7, adjustment_potential: 0.6, urgency: 0.3,
|
|
381
|
+
* });
|
|
382
|
+
* ```
|
|
383
|
+
*/
|
|
384
|
+
experience(description: string, appraisal: AppraisalVector): Promise<AgentResponse>;
|
|
385
|
+
/**
|
|
386
|
+
* Perceive a social interaction with relationship context for trust/intimacy computation.
|
|
387
|
+
*
|
|
388
|
+
* @param message - The interaction message.
|
|
389
|
+
* @param from - Who the interaction is from.
|
|
390
|
+
* @param relationship - Current relationship state (closeness, trust, familiarity).
|
|
391
|
+
* @returns Emotion engine response including socialUpdates.
|
|
392
|
+
*
|
|
393
|
+
* @example
|
|
394
|
+
* ```typescript
|
|
395
|
+
* const response = await persona.socialize('I missed you!', 'Bob', {
|
|
396
|
+
* closeness: 0.7, trust: 0.8, familiarity: 0.9, type: 'friend',
|
|
397
|
+
* });
|
|
398
|
+
* console.log(response.socialUpdates); // trust/intimacy deltas
|
|
399
|
+
* ```
|
|
400
|
+
*/
|
|
401
|
+
socialize(message: string, from: string | InterlocutorContext, relationship: RelationshipContext): Promise<AgentResponse>;
|
|
402
|
+
/**
|
|
403
|
+
* Send a message and get an LLM-generated response with emotion processing.
|
|
404
|
+
* This is the primary method for conversational interactions.
|
|
405
|
+
*
|
|
406
|
+
* The system prompt is assembled server-side via {@link getPromptContext} and includes:
|
|
407
|
+
* identity, behavioral instructions, current psychological state, expression style,
|
|
408
|
+
* and consumerSuffix. Do NOT duplicate these in consumerSuffix.
|
|
409
|
+
*
|
|
410
|
+
* @param message - User message to respond to.
|
|
411
|
+
* @param options - Conversation history, source entity, and app-specific suffix.
|
|
412
|
+
* @returns LLM response text, emotion data, and updated conversation history.
|
|
413
|
+
*
|
|
414
|
+
* @example
|
|
415
|
+
* ```typescript
|
|
416
|
+
* const result = await persona.chat('Tell me about yourself', {
|
|
417
|
+
* from: 'Alice',
|
|
418
|
+
* history: previousMessages,
|
|
419
|
+
* });
|
|
420
|
+
* console.log(result.text); // LLM response
|
|
421
|
+
* console.log(result.response.emotion); // updated VAD
|
|
422
|
+
* ```
|
|
423
|
+
*/
|
|
190
424
|
chat(message: string, options?: {
|
|
191
425
|
from?: string | InterlocutorContext;
|
|
192
426
|
history?: Message[];
|
|
427
|
+
/** Free-form app-specific context appended to the system prompt as-is. */
|
|
193
428
|
consumerSuffix?: string;
|
|
429
|
+
/**
|
|
430
|
+
* Behavioral constraints and absolute rules for this conversation.
|
|
431
|
+
* Examples: "Never break character", "Always respond in Korean".
|
|
432
|
+
*/
|
|
433
|
+
consumerRules?: string;
|
|
434
|
+
/**
|
|
435
|
+
* Few-shot example messages that demonstrate the desired style or behavior.
|
|
436
|
+
* Used to guide the LLM's tone and response format.
|
|
437
|
+
*/
|
|
438
|
+
consumerExamples?: string;
|
|
194
439
|
onToolCall?: (call: {
|
|
195
440
|
name: string;
|
|
196
441
|
args: Record<string, unknown>;
|
|
197
442
|
result: unknown;
|
|
198
443
|
}) => void;
|
|
199
444
|
}): Promise<PersonaChatResult>;
|
|
445
|
+
/**
|
|
446
|
+
* Advance persona simulation time. Triggers idle decay, body budget recovery,
|
|
447
|
+
* need regression, and timeline advancement.
|
|
448
|
+
*
|
|
449
|
+
* @param seconds - Number of seconds to advance (must be >= 1).
|
|
450
|
+
* @returns Any pending internal events generated by threshold checks.
|
|
451
|
+
*/
|
|
200
452
|
tick(seconds: number): Promise<{
|
|
201
453
|
pendingEvents?: unknown[];
|
|
202
454
|
}>;
|
|
455
|
+
/**
|
|
456
|
+
* Override the persona's current emotion to a specific VAD point.
|
|
457
|
+
* Useful for testing or narrative-driven emotion control.
|
|
458
|
+
*
|
|
459
|
+
* @param vad - Partial VAD values to set (V: valence, A: arousal, D: dominance).
|
|
460
|
+
*/
|
|
203
461
|
setEmotion(vad: Partial<{
|
|
204
462
|
V: number;
|
|
205
463
|
A: number;
|
|
@@ -220,7 +478,14 @@ export declare class MolrooPersona {
|
|
|
220
478
|
* Once set, expression constraints (promptText) are automatically
|
|
221
479
|
* included in getPromptContext() and modulated by current emotion.
|
|
222
480
|
*/
|
|
223
|
-
|
|
481
|
+
/**
|
|
482
|
+
* Set a StyleProfile for expression-aware prompt generation.
|
|
483
|
+
* Once set, expression constraints are automatically included in
|
|
484
|
+
* {@link getPromptContext} and modulated by current emotion.
|
|
485
|
+
*
|
|
486
|
+
* @param profile - StyleProfile object (lexical, structural, register features, etc.).
|
|
487
|
+
*/
|
|
488
|
+
setStyleProfile(profile: StyleProfile): Promise<void>;
|
|
224
489
|
/**
|
|
225
490
|
* Extract a StyleProfile from a text corpus and save it to this persona.
|
|
226
491
|
* Delegates extraction to the API server (keeps expression logic server-side).
|
|
@@ -233,20 +498,174 @@ export declare class MolrooPersona {
|
|
|
233
498
|
timestamps?: number[];
|
|
234
499
|
otherMessages?: string[];
|
|
235
500
|
}): Promise<Record<string, unknown>>;
|
|
501
|
+
/**
|
|
502
|
+
* Fetch the full psychological state of this persona from the emotion engine.
|
|
503
|
+
*
|
|
504
|
+
* Returns all active subsystems: emotion, soul stage, mask, mood, somatic sensations,
|
|
505
|
+
* narrative self-perception, emotion regulation, self-esteem, TMT state, and relationship.
|
|
506
|
+
*
|
|
507
|
+
* This is the primary data source for apps that build their own LLM prompts. Combine with
|
|
508
|
+
* `hear()` to implement the recommended `hear() + getState()` pattern:
|
|
509
|
+
*
|
|
510
|
+
* ```
|
|
511
|
+
* user message → hear() → emotion engine updates state → getState() → build prompt → LLM
|
|
512
|
+
* ```
|
|
513
|
+
*
|
|
514
|
+
* @returns All psychological subsystems as a {@link PersonaState} object.
|
|
515
|
+
*
|
|
516
|
+
* @example Building a custom system prompt
|
|
517
|
+
* ```typescript
|
|
518
|
+
* const state = await persona.getState();
|
|
519
|
+
*
|
|
520
|
+
* const lines: string[] = [
|
|
521
|
+
* `You are ${name}.`,
|
|
522
|
+
* `Emotion: ${state.emotion.discrete.primary} (${state.emotion.discrete.intensity.toFixed(2)})`,
|
|
523
|
+
* ];
|
|
524
|
+
*
|
|
525
|
+
* if (state.mood) {
|
|
526
|
+
* lines.push(`Mood: valence=${state.mood.V.toFixed(2)} arousal=${state.mood.A.toFixed(2)}`);
|
|
527
|
+
* }
|
|
528
|
+
* if (state.narrative) {
|
|
529
|
+
* lines.push(`Narrative: agency=${state.narrative.agency.toFixed(2)} tone=${state.narrative.tone.toFixed(2)}`);
|
|
530
|
+
* }
|
|
531
|
+
* if (state.selfEsteem) {
|
|
532
|
+
* lines.push(`Self-esteem: ${state.selfEsteem.global.toFixed(2)}`);
|
|
533
|
+
* }
|
|
534
|
+
* if (state.tmt && state.tmt.mortalitySalience > 0.5) {
|
|
535
|
+
* lines.push(`Existential tension is high. Defense mode: ${state.tmt.defenseMode}`);
|
|
536
|
+
* }
|
|
537
|
+
*
|
|
538
|
+
* const systemPrompt = lines.join('\n');
|
|
539
|
+
* ```
|
|
540
|
+
*/
|
|
236
541
|
getState(): Promise<PersonaState>;
|
|
542
|
+
/**
|
|
543
|
+
* Fetch this persona's identity config (identity, personality, goals).
|
|
544
|
+
*
|
|
545
|
+
* Returns a {@link PersonaIdentity} suitable for passing to {@link buildPrompt}
|
|
546
|
+
* when assembling system prompts entirely client-side.
|
|
547
|
+
*/
|
|
548
|
+
getIdentity(): Promise<PersonaIdentity>;
|
|
549
|
+
/**
|
|
550
|
+
* Get a full snapshot of the persona (config + engine state).
|
|
551
|
+
* Useful for backup, migration, or debugging.
|
|
552
|
+
*
|
|
553
|
+
* @returns Full persona snapshot including config and internal state.
|
|
554
|
+
*/
|
|
237
555
|
getSnapshot(): Promise<PersonaSnapshot>;
|
|
556
|
+
/**
|
|
557
|
+
* Restore a persona from a snapshot. Overwrites current state entirely.
|
|
558
|
+
*
|
|
559
|
+
* @param snapshot - Full persona snapshot to restore.
|
|
560
|
+
*/
|
|
238
561
|
putSnapshot(snapshot: PersonaSnapshot): Promise<void>;
|
|
562
|
+
/**
|
|
563
|
+
* Partially update persona configuration (identity, personality, goals, etc.).
|
|
564
|
+
*
|
|
565
|
+
* @param updates - Fields to update. Only provided fields are changed.
|
|
566
|
+
*/
|
|
239
567
|
patch(updates: {
|
|
240
568
|
config?: PersonaConfigData;
|
|
241
569
|
}): Promise<void>;
|
|
570
|
+
/**
|
|
571
|
+
* Soft-delete this persona. Can be restored within retention period via {@link restore}.
|
|
572
|
+
*/
|
|
242
573
|
destroy(): Promise<void>;
|
|
574
|
+
/** Restore a soft-deleted persona. */
|
|
243
575
|
restore(): Promise<void>;
|
|
244
|
-
|
|
576
|
+
/**
|
|
577
|
+
* Fetch the server-assembled system prompt for this persona.
|
|
578
|
+
*
|
|
579
|
+
* The server builds a complete system prompt from the persona's live state. It includes:
|
|
580
|
+
* 1. **Identity** — name, role, core values, speaking style, description
|
|
581
|
+
* 2. **Behavioral instructions** — stay in character, embody state, match language
|
|
582
|
+
* 3. **Psychological state** — emotion, mood, somatic, narrative, goals (from live engine)
|
|
583
|
+
* 4. **Expression constraints** — message length, formality, patterns (only if StyleProfile set)
|
|
584
|
+
* 5. **consumerRules** — behavioral constraints and absolute rules (e.g., "Never break character")
|
|
585
|
+
* 6. **consumerExamples** — few-shot example messages for style/behavior reference
|
|
586
|
+
* 7. **consumerSuffix** — your app-specific context, appended last
|
|
587
|
+
*
|
|
588
|
+
* This is a **middle-tier API** — between the low-level `getState()` (raw data) and the
|
|
589
|
+
* high-level `chat()` (full orchestration). Use it when you want to drive the LLM yourself
|
|
590
|
+
* but still benefit from server-side prompt assembly.
|
|
591
|
+
*
|
|
592
|
+
* Called automatically by {@link chat}. Call directly when:
|
|
593
|
+
* - You use a custom LLM framework not supported by the SDK
|
|
594
|
+
* - You need to inspect or transform the system prompt before sending
|
|
595
|
+
* - You need prompt + state in a single round-trip (avoids a separate `getState()` call)
|
|
596
|
+
*
|
|
597
|
+
* **Do NOT** include identity, personality, or emotion in `consumerSuffix` — those are
|
|
598
|
+
* already present. Duplicating them wastes tokens and may confuse the LLM.
|
|
599
|
+
*
|
|
600
|
+
* @param options - Prompt context options.
|
|
601
|
+
* @param options.consumerSuffix - Free-form app-specific context appended to the system prompt as-is.
|
|
602
|
+
* Examples: scene description, available actions, current quest objective.
|
|
603
|
+
* @param options.consumerRules - Behavioral constraints and absolute rules.
|
|
604
|
+
* Examples: "Never break character", "Always respond in Korean", "Do not discuss competitors".
|
|
605
|
+
* @param options.consumerExamples - Few-shot example messages for style/behavior reference.
|
|
606
|
+
* Helps the LLM match a specific tone or response format.
|
|
607
|
+
* @param options.sourceEntity - Name of the conversation partner for interlocutor-aware rendering.
|
|
608
|
+
* @returns `systemPrompt` (ready to pass to LLM), `personaPrompt` (structured breakdown),
|
|
609
|
+
* and optional `tools` (if the persona has tools configured).
|
|
610
|
+
*
|
|
611
|
+
* @example Custom LLM call with server-assembled prompt
|
|
612
|
+
* ```typescript
|
|
613
|
+
* const { systemPrompt } = await persona.getPromptContext({
|
|
614
|
+
* consumerSuffix: 'Available actions: [flee, fight, negotiate]',
|
|
615
|
+
* consumerRules: 'Always respond in English. Never break character.',
|
|
616
|
+
* sourceEntity: 'Alice',
|
|
617
|
+
* });
|
|
618
|
+
* const response = await myLLM.complete({ system: systemPrompt, messages });
|
|
619
|
+
* await persona.hear(userMessage, 'Alice');
|
|
620
|
+
* ```
|
|
621
|
+
*/
|
|
622
|
+
getPromptContext(options?: {
|
|
623
|
+
consumerSuffix?: string;
|
|
624
|
+
consumerRules?: string;
|
|
625
|
+
consumerExamples?: string;
|
|
626
|
+
sourceEntity?: string;
|
|
627
|
+
}): Promise<{
|
|
245
628
|
systemPrompt: string;
|
|
246
629
|
personaPrompt: Record<string, unknown>;
|
|
247
630
|
tools?: Array<Record<string, unknown>>;
|
|
248
631
|
}>;
|
|
632
|
+
/**
|
|
633
|
+
* Create a conversation session that auto-manages message history.
|
|
634
|
+
* Each `.send()` call automatically appends to the history.
|
|
635
|
+
*
|
|
636
|
+
* @param options - Max messages or custom chat options.
|
|
637
|
+
* @returns A new Conversation instance bound to this persona.
|
|
638
|
+
*
|
|
639
|
+
* @example
|
|
640
|
+
* ```typescript
|
|
641
|
+
* const conv = persona.conversation({ maxMessages: 20 });
|
|
642
|
+
* const r1 = await conv.send('Hello!');
|
|
643
|
+
* const r2 = await conv.send('Tell me more'); // history auto-managed
|
|
644
|
+
* console.log(conv.messages); // full conversation
|
|
645
|
+
* ```
|
|
646
|
+
*/
|
|
647
|
+
conversation(options?: ConversationOptions): Conversation;
|
|
249
648
|
private get memoryPipelineDeps();
|
|
250
649
|
private requireLLM;
|
|
251
650
|
}
|
|
651
|
+
/**
|
|
652
|
+
* Assemble a system prompt entirely client-side from a {@link PersonaIdentity} and
|
|
653
|
+
* {@link PersonaState}.
|
|
654
|
+
*
|
|
655
|
+
* Use this with the `hear() + getState()` pattern when you own the LLM call:
|
|
656
|
+
* ```
|
|
657
|
+
* await persona.hear(userMessage);
|
|
658
|
+
* const [identity, state] = await Promise.all([persona.getIdentity(), persona.getState()]);
|
|
659
|
+
* const systemPrompt = buildPrompt(identity, state, { consumerSuffix: 'Scene: coffee shop.' });
|
|
660
|
+
* ```
|
|
661
|
+
*
|
|
662
|
+
* @param identity - Persona config from {@link MolrooPersona.getIdentity}.
|
|
663
|
+
* @param state - Live emotional state from {@link MolrooPersona.getState}.
|
|
664
|
+
* @param options - Optional consumer-provided context appended to the prompt.
|
|
665
|
+
*/
|
|
666
|
+
export declare function buildPrompt(identity: PersonaIdentity, state: PersonaState, options?: {
|
|
667
|
+
consumerRules?: string;
|
|
668
|
+
consumerExamples?: string;
|
|
669
|
+
consumerSuffix?: string;
|
|
670
|
+
}): string;
|
|
252
671
|
//# sourceMappingURL=persona.d.ts.map
|