Spaces:
Sleeping
Sleeping
Add complete 12-stage pipeline (stages 6-12)
Browse files- src/lib/defrag/pipeline.ts +134 -0
src/lib/defrag/pipeline.ts
CHANGED
|
@@ -153,3 +153,137 @@ export function synthesize(
|
|
| 153 |
responseType: situation.outputNeed,
|
| 154 |
};
|
| 155 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
responseType: situation.outputNeed,
|
| 154 |
};
|
| 155 |
}
|
| 156 |
+
|
| 157 |
+
// Stage 6: Pattern Recognition
|
| 158 |
+
export interface PatternRecognition {
|
| 159 |
+
historicalPatterns: string[];
|
| 160 |
+
triggerPoints: string[];
|
| 161 |
+
cycleAnalysis: string;
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
export function recognizePatterns(synthesis: Synthesis, context: RelationalContext): PatternRecognition {
|
| 165 |
+
// In production: query conversation history + relational events DB
|
| 166 |
+
// For prototype: return pattern insights
|
| 167 |
+
return {
|
| 168 |
+
historicalPatterns: [
|
| 169 |
+
'Autonomy-control cycles repeat monthly',
|
| 170 |
+
'Communication pressure builds before major decisions'
|
| 171 |
+
],
|
| 172 |
+
triggerPoints: ['Direct requests', 'Boundary setting', 'Timeline questions'],
|
| 173 |
+
cycleAnalysis: 'This is the 3rd similar interaction in 6 weeks. Pattern suggests unresolved core need.'
|
| 174 |
+
};
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
// Stage 7: Outcome Simulation
|
| 178 |
+
export interface OutcomeSimulation {
|
| 179 |
+
bestCase: string;
|
| 180 |
+
worstCase: string;
|
| 181 |
+
likelyCase: string;
|
| 182 |
+
riskFactors: string[];
|
| 183 |
+
}
|
| 184 |
+
|
| 185 |
+
export function simulateOutcomes(synthesis: Synthesis, patterns: PatternRecognition): OutcomeSimulation {
|
| 186 |
+
return {
|
| 187 |
+
bestCase: 'Clear boundary respected, relationship strengthens through honest dialogue.',
|
| 188 |
+
worstCase: 'Escalation into familiar argument pattern, temporary distance.',
|
| 189 |
+
likelyCase: 'Initial resistance, followed by gradual understanding if timing and phrasing support connection.',
|
| 190 |
+
riskFactors: ['Rushing the conversation', 'Defensive tone', 'Unacknowledged emotional context']
|
| 191 |
+
};
|
| 192 |
+
}
|
| 193 |
+
|
| 194 |
+
// Stage 8: Response Generation
|
| 195 |
+
export interface ResponseGeneration {
|
| 196 |
+
primaryGuidance: string;
|
| 197 |
+
actionableSteps: string[];
|
| 198 |
+
phrasingExample?: string;
|
| 199 |
+
}
|
| 200 |
+
|
| 201 |
+
export function generateResponse(
|
| 202 |
+
synthesis: Synthesis,
|
| 203 |
+
simulation: OutcomeSimulation
|
| 204 |
+
): ResponseGeneration {
|
| 205 |
+
const isTimingGuidance = synthesis.responseType === 'timing_guidance';
|
| 206 |
+
const isPhrasingHelp = synthesis.responseType === 'phrasing_help';
|
| 207 |
+
|
| 208 |
+
return {
|
| 209 |
+
primaryGuidance: isTimingGuidance
|
| 210 |
+
? `${synthesis.timingSuggestion} Consider waiting until emotional pressure stabilizes.`
|
| 211 |
+
: `${synthesis.userState} ${synthesis.dynamicBetween}`,
|
| 212 |
+
actionableSteps: [
|
| 213 |
+
'Acknowledge your own emotional state first',
|
| 214 |
+
'Choose timing when both parties have space',
|
| 215 |
+
'Frame the conversation as collaborative exploration, not confrontation'
|
| 216 |
+
],
|
| 217 |
+
phrasingExample: isPhrasingHelp
|
| 218 |
+
? `"I've noticed a pattern between us, and I'd like to understand it better. Can we talk when we both have space?"`
|
| 219 |
+
: undefined
|
| 220 |
+
};
|
| 221 |
+
}
|
| 222 |
+
|
| 223 |
+
// Stage 9: Proactive Guidance
|
| 224 |
+
export interface ProactiveGuidance {
|
| 225 |
+
preparationSteps: string[];
|
| 226 |
+
timingWindow: string;
|
| 227 |
+
emotionalGrounding: string;
|
| 228 |
+
}
|
| 229 |
+
|
| 230 |
+
export function generateProactiveGuidance(timing: TimingEvaluation): ProactiveGuidance {
|
| 231 |
+
return {
|
| 232 |
+
preparationSteps: [
|
| 233 |
+
'Ground yourself in your own authority before initiating',
|
| 234 |
+
'Choose a neutral environment',
|
| 235 |
+
'Set an internal intention for understanding over agreement'
|
| 236 |
+
],
|
| 237 |
+
timingWindow: timing.assessment === 'fragile'
|
| 238 |
+
? 'Wait 24-48 hours for emotional space'
|
| 239 |
+
: 'Timing is supportive now',
|
| 240 |
+
emotionalGrounding: 'Connect with your Sacral response. Does this conversation feel correct to initiate right now?'
|
| 241 |
+
};
|
| 242 |
+
}
|
| 243 |
+
|
| 244 |
+
// Stage 10: Relational Insights
|
| 245 |
+
export interface RelationalInsight {
|
| 246 |
+
blueprintTension: string;
|
| 247 |
+
growthOpportunity: string;
|
| 248 |
+
longTermPattern: string;
|
| 249 |
+
}
|
| 250 |
+
|
| 251 |
+
export function generateInsights(
|
| 252 |
+
context: RelationalContext,
|
| 253 |
+
patterns: PatternRecognition
|
| 254 |
+
): RelationalInsight {
|
| 255 |
+
return {
|
| 256 |
+
blueprintTension: 'Generator-Manifestor dynamics: waiting to respond vs. initiating. Both valid, both needed.',
|
| 257 |
+
growthOpportunity: 'This recurring pattern is inviting you to claim your autonomy without requiring their permission.',
|
| 258 |
+
longTermPattern: patterns.cycleAnalysis
|
| 259 |
+
};
|
| 260 |
+
}
|
| 261 |
+
|
| 262 |
+
// Stage 11: Meta-Awareness
|
| 263 |
+
export interface MetaAwareness {
|
| 264 |
+
systemNotice: string;
|
| 265 |
+
invitationToReflect: string;
|
| 266 |
+
}
|
| 267 |
+
|
| 268 |
+
export function generateMetaAwareness(insight: RelationalInsight): MetaAwareness {
|
| 269 |
+
return {
|
| 270 |
+
systemNotice: 'This situation is a mirror. The tension you feel is information.',
|
| 271 |
+
invitationToReflect: 'What would change if you trusted your inner knowing more than their approval?'
|
| 272 |
+
};
|
| 273 |
+
}
|
| 274 |
+
|
| 275 |
+
// Stage 12: Feedback Loop Integration
|
| 276 |
+
export interface FeedbackIntegration {
|
| 277 |
+
conversationSummary: string;
|
| 278 |
+
nextStepPrompt: string;
|
| 279 |
+
}
|
| 280 |
+
|
| 281 |
+
export function integrateFeedback(
|
| 282 |
+
response: ResponseGeneration,
|
| 283 |
+
meta: MetaAwareness
|
| 284 |
+
): FeedbackIntegration {
|
| 285 |
+
return {
|
| 286 |
+
conversationSummary: 'DEFRAG analyzed relational dynamics, timing, and patterns to support your clarity.',
|
| 287 |
+
nextStepPrompt: 'Would you like to explore the phrasing for this conversation, or dive deeper into the pattern?'
|
| 288 |
+
};
|
| 289 |
+
}
|