@cognitive-engine/reasoning 0.1.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/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/intention-generator.d.ts +10 -0
- package/dist/intention-generator.d.ts.map +1 -0
- package/dist/intention-generator.js +115 -0
- package/dist/intention-generator.js.map +1 -0
- package/dist/reasoner.d.ts +29 -0
- package/dist/reasoner.d.ts.map +1 -0
- package/dist/reasoner.js +102 -0
- package/dist/reasoner.js.map +1 -0
- package/dist/working-memory.d.ts +20 -0
- package/dist/working-memory.d.ts.map +1 -0
- package/dist/working-memory.js +69 -0
- package/dist/working-memory.js.map +1 -0
- package/dist/world-model.d.ts +31 -0
- package/dist/world-model.d.ts.map +1 -0
- package/dist/world-model.js +115 -0
- package/dist/world-model.js.map +1 -0
- package/package.json +38 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AACxC,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAC7C,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AACnD,OAAO,EAAE,kBAAkB,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AACxC,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAC7C,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AACnD,OAAO,EAAE,kBAAkB,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAA"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Percept, CognitiveState, Intention, Belief, InferenceRule, BeliefCandidate } from '@cognitive-engine/core';
|
|
2
|
+
/**
|
|
3
|
+
* Generate intentions from percept + cognitive state using BDI rules.
|
|
4
|
+
*/
|
|
5
|
+
export declare function generateIntentions(percept: Percept, state: CognitiveState): Intention[];
|
|
6
|
+
/**
|
|
7
|
+
* Apply inference rules to generate new belief candidates.
|
|
8
|
+
*/
|
|
9
|
+
export declare function applyInferenceRules(percept: Percept, beliefs: Belief[], customRules?: InferenceRule[]): BeliefCandidate[];
|
|
10
|
+
//# sourceMappingURL=intention-generator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"intention-generator.d.ts","sourceRoot":"","sources":["../src/intention-generator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,OAAO,EACP,cAAc,EACd,SAAS,EAET,MAAM,EACN,aAAa,EACb,eAAe,EAChB,MAAM,wBAAwB,CAAA;AAe/B;;GAEG;AACH,wBAAgB,kBAAkB,CAChC,OAAO,EAAE,OAAO,EAChB,KAAK,EAAE,cAAc,GACpB,SAAS,EAAE,CAwDb;AA2CD;;GAEG;AACH,wBAAgB,mBAAmB,CACjC,OAAO,EAAE,OAAO,EAChB,OAAO,EAAE,MAAM,EAAE,EACjB,WAAW,GAAE,aAAa,EAAO,GAChC,eAAe,EAAE,CAWnB"}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
const REQUEST_TO_INTENTION = {
|
|
2
|
+
question: 'inform',
|
|
3
|
+
task: 'inform',
|
|
4
|
+
creative: 'inform',
|
|
5
|
+
advice: 'suggest',
|
|
6
|
+
venting: 'empathize',
|
|
7
|
+
sharing: 'empathize',
|
|
8
|
+
greeting: 'inform',
|
|
9
|
+
feedback: 'empathize',
|
|
10
|
+
};
|
|
11
|
+
const NEGATIVE_EMOTIONS = new Set(['frustrated', 'anxious', 'negative']);
|
|
12
|
+
/**
|
|
13
|
+
* Generate intentions from percept + cognitive state using BDI rules.
|
|
14
|
+
*/
|
|
15
|
+
export function generateIntentions(percept, state) {
|
|
16
|
+
const intentions = [];
|
|
17
|
+
// CRITICAL: listening mode = ONLY empathize, no advice
|
|
18
|
+
if (percept.responseMode === 'listening') {
|
|
19
|
+
return [
|
|
20
|
+
{
|
|
21
|
+
type: 'empathize',
|
|
22
|
+
target: percept.emotionalTone || 'neutral',
|
|
23
|
+
priority: 10,
|
|
24
|
+
reason: 'User is sharing, not asking for advice',
|
|
25
|
+
},
|
|
26
|
+
];
|
|
27
|
+
}
|
|
28
|
+
// Primary intention by request type
|
|
29
|
+
const intentionType = REQUEST_TO_INTENTION[percept.requestType] ?? 'inform';
|
|
30
|
+
intentions.push({
|
|
31
|
+
type: intentionType,
|
|
32
|
+
target: percept.rawText.substring(0, 50),
|
|
33
|
+
priority: 10,
|
|
34
|
+
reason: `Primary response to ${percept.requestType}`,
|
|
35
|
+
});
|
|
36
|
+
// Negative emotion → empathize first
|
|
37
|
+
if (NEGATIVE_EMOTIONS.has(percept.emotionalTone)) {
|
|
38
|
+
intentions.push({
|
|
39
|
+
type: 'empathize',
|
|
40
|
+
target: percept.emotionalTone,
|
|
41
|
+
priority: 8,
|
|
42
|
+
reason: 'User is experiencing negative emotion',
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
// High urgency → prioritize speed
|
|
46
|
+
if (percept.urgency > 7) {
|
|
47
|
+
intentions.push({
|
|
48
|
+
type: 'inform',
|
|
49
|
+
target: 'urgent_response',
|
|
50
|
+
priority: 9,
|
|
51
|
+
reason: 'High urgency detected',
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
// Few beliefs → clarify
|
|
55
|
+
if (state.beliefs.length < 5) {
|
|
56
|
+
intentions.push({
|
|
57
|
+
type: 'clarify',
|
|
58
|
+
target: 'user_context',
|
|
59
|
+
priority: 4,
|
|
60
|
+
reason: 'Not enough context about user',
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
return intentions.sort((a, b) => b.priority - a.priority);
|
|
64
|
+
}
|
|
65
|
+
// ═══════════════════════════════════════════
|
|
66
|
+
// Built-in inference rules
|
|
67
|
+
// ═══════════════════════════════════════════
|
|
68
|
+
const BUILTIN_RULES = [
|
|
69
|
+
{
|
|
70
|
+
name: 'negative_emotion_problem',
|
|
71
|
+
condition: (percept, _beliefs) => NEGATIVE_EMOTIONS.has(percept.emotionalTone) &&
|
|
72
|
+
percept.entities.length > 0,
|
|
73
|
+
action: (percept) => ({
|
|
74
|
+
subject: 'user',
|
|
75
|
+
predicate: 'has_problem_with',
|
|
76
|
+
object: percept.entities[0]?.value ?? 'unknown',
|
|
77
|
+
confidence: 0.6,
|
|
78
|
+
}),
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
name: 'creative_personality',
|
|
82
|
+
condition: (percept, beliefs) => percept.requestType === 'creative' &&
|
|
83
|
+
!beliefs.some((b) => b.predicate === 'is_creative'),
|
|
84
|
+
action: () => ({
|
|
85
|
+
subject: 'user',
|
|
86
|
+
predicate: 'is_creative',
|
|
87
|
+
object: 'true',
|
|
88
|
+
confidence: 0.5,
|
|
89
|
+
}),
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
name: 'values_speed',
|
|
93
|
+
condition: (percept) => percept.urgency > 7,
|
|
94
|
+
action: () => ({
|
|
95
|
+
subject: 'user',
|
|
96
|
+
predicate: 'values',
|
|
97
|
+
object: 'speed',
|
|
98
|
+
confidence: 0.7,
|
|
99
|
+
}),
|
|
100
|
+
},
|
|
101
|
+
];
|
|
102
|
+
/**
|
|
103
|
+
* Apply inference rules to generate new belief candidates.
|
|
104
|
+
*/
|
|
105
|
+
export function applyInferenceRules(percept, beliefs, customRules = []) {
|
|
106
|
+
const allRules = [...BUILTIN_RULES, ...customRules];
|
|
107
|
+
const candidates = [];
|
|
108
|
+
for (const rule of allRules) {
|
|
109
|
+
if (rule.condition(percept, beliefs)) {
|
|
110
|
+
candidates.push(rule.action(percept));
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return candidates;
|
|
114
|
+
}
|
|
115
|
+
//# sourceMappingURL=intention-generator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"intention-generator.js","sourceRoot":"","sources":["../src/intention-generator.ts"],"names":[],"mappings":"AAUA,MAAM,oBAAoB,GAAkC;IAC1D,QAAQ,EAAE,QAAQ;IAClB,IAAI,EAAE,QAAQ;IACd,QAAQ,EAAE,QAAQ;IAClB,MAAM,EAAE,SAAS;IACjB,OAAO,EAAE,WAAW;IACpB,OAAO,EAAE,WAAW;IACpB,QAAQ,EAAE,QAAQ;IAClB,QAAQ,EAAE,WAAW;CACtB,CAAA;AAED,MAAM,iBAAiB,GAAG,IAAI,GAAG,CAAC,CAAC,YAAY,EAAE,SAAS,EAAE,UAAU,CAAC,CAAC,CAAA;AAExE;;GAEG;AACH,MAAM,UAAU,kBAAkB,CAChC,OAAgB,EAChB,KAAqB;IAErB,MAAM,UAAU,GAAgB,EAAE,CAAA;IAElC,uDAAuD;IACvD,IAAI,OAAO,CAAC,YAAY,KAAK,WAAW,EAAE,CAAC;QACzC,OAAO;YACL;gBACE,IAAI,EAAE,WAAW;gBACjB,MAAM,EAAE,OAAO,CAAC,aAAa,IAAI,SAAS;gBAC1C,QAAQ,EAAE,EAAE;gBACZ,MAAM,EAAE,wCAAwC;aACjD;SACF,CAAA;IACH,CAAC;IAED,oCAAoC;IACpC,MAAM,aAAa,GACjB,oBAAoB,CAAC,OAAO,CAAC,WAAW,CAAC,IAAI,QAAQ,CAAA;IACvD,UAAU,CAAC,IAAI,CAAC;QACd,IAAI,EAAE,aAAa;QACnB,MAAM,EAAE,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,EAAE,EAAE,CAAC;QACxC,QAAQ,EAAE,EAAE;QACZ,MAAM,EAAE,uBAAuB,OAAO,CAAC,WAAW,EAAE;KACrD,C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AACnB,CAAC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Percept, CognitiveState, ReasoningResult, ReasoningConfig } from '@cognitive-engine/core';
|
|
2
|
+
import { WorldModel } from './world-model.js';
|
|
3
|
+
import { WorkingMemory } from './working-memory.js';
|
|
4
|
+
/**
|
|
5
|
+
* BDI Reasoning Engine.
|
|
6
|
+
*
|
|
7
|
+
* Orchestrates the reasoning loop:
|
|
8
|
+
* 1. Update working memory from percept
|
|
9
|
+
* 2. Apply inference rules → new belief candidates
|
|
10
|
+
* 3. Update world model with new beliefs
|
|
11
|
+
* 4. Generate intentions (BDI core)
|
|
12
|
+
* 5. Calculate confidence
|
|
13
|
+
*/
|
|
14
|
+
export declare class Reasoner {
|
|
15
|
+
readonly worldModel: WorldModel;
|
|
16
|
+
readonly workingMemory: WorkingMemory;
|
|
17
|
+
private readonly customRules;
|
|
18
|
+
constructor(config?: ReasoningConfig);
|
|
19
|
+
/**
|
|
20
|
+
* Main reasoning loop: perceive → reason → decide.
|
|
21
|
+
*/
|
|
22
|
+
reason(percept: Percept): ReasoningResult;
|
|
23
|
+
/** Get current cognitive state snapshot. */
|
|
24
|
+
getState(): CognitiveState;
|
|
25
|
+
private generateHypotheses;
|
|
26
|
+
private generateQuestions;
|
|
27
|
+
private calculateConfidence;
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=reasoner.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reasoner.d.ts","sourceRoot":"","sources":["../src/reasoner.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,OAAO,EACP,cAAc,EACd,eAAe,EACf,eAAe,EAEhB,MAAM,wBAAwB,CAAA;AAC/B,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAC7C,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AAGnD;;;;;;;;;GASG;AACH,qBAAa,QAAQ;IACnB,QAAQ,CAAC,UAAU,EAAE,UAAU,CAAA;IAC/B,QAAQ,CAAC,aAAa,EAAE,aAAa,CAAA;IACrC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAiB;gBAEjC,MAAM,GAAE,eAAoB;IAMxC;;OAEG;IACH,MAAM,CAAC,OAAO,EAAE,OAAO,GAAG,eAAe;IAgDzC,4CAA4C;IAC5C,QAAQ,IAAI,cAAc;IAY1B,OAAO,CAAC,kBAAkB;IAQ1B,OAAO,CAAC,iBAAiB;IAczB,OAAO,CAAC,mBAAmB;CAqB5B"}
|
package/dist/reasoner.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { WorldModel } from './world-model.js';
|
|
2
|
+
import { WorkingMemory } from './working-memory.js';
|
|
3
|
+
import { generateIntentions, applyInferenceRules } from './intention-generator.js';
|
|
4
|
+
/**
|
|
5
|
+
* BDI Reasoning Engine.
|
|
6
|
+
*
|
|
7
|
+
* Orchestrates the reasoning loop:
|
|
8
|
+
* 1. Update working memory from percept
|
|
9
|
+
* 2. Apply inference rules → new belief candidates
|
|
10
|
+
* 3. Update world model with new beliefs
|
|
11
|
+
* 4. Generate intentions (BDI core)
|
|
12
|
+
* 5. Calculate confidence
|
|
13
|
+
*/
|
|
14
|
+
export class Reasoner {
|
|
15
|
+
worldModel;
|
|
16
|
+
workingMemory;
|
|
17
|
+
customRules;
|
|
18
|
+
constructor(config = {}) {
|
|
19
|
+
this.worldModel = new WorldModel();
|
|
20
|
+
this.workingMemory = new WorkingMemory(config.maxWorkingMemory ?? 50);
|
|
21
|
+
this.customRules = config.customRules ?? [];
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Main reasoning loop: perceive → reason → decide.
|
|
25
|
+
*/
|
|
26
|
+
reason(percept) {
|
|
27
|
+
// 1. Update working memory
|
|
28
|
+
this.workingMemory.update(percept);
|
|
29
|
+
// 2. Apply inference rules
|
|
30
|
+
const beliefs = this.worldModel.getBeliefs();
|
|
31
|
+
const candidates = applyInferenceRules(percept, beliefs, this.customRules);
|
|
32
|
+
// 3. Update world model with inferred beliefs
|
|
33
|
+
const newBeliefs = candidates.map((c) => this.worldModel.addBelief(c, 'inferred'));
|
|
34
|
+
// 4. Build cognitive state
|
|
35
|
+
const state = {
|
|
36
|
+
beliefs: this.worldModel.getBeliefs(),
|
|
37
|
+
goals: [],
|
|
38
|
+
workingMemory: this.workingMemory.getItems(),
|
|
39
|
+
currentIntentions: [],
|
|
40
|
+
emotionalContext: percept.emotionalTone,
|
|
41
|
+
attentionFocus: percept.entities.map((e) => e.value),
|
|
42
|
+
lastUpdated: new Date(),
|
|
43
|
+
};
|
|
44
|
+
// 5. Generate intentions
|
|
45
|
+
const intentions = generateIntentions(percept, state);
|
|
46
|
+
// 6. Generate hypotheses and questions
|
|
47
|
+
const hypotheses = this.generateHypotheses(percept);
|
|
48
|
+
const questionsToAsk = this.generateQuestions(percept, state);
|
|
49
|
+
// 7. Calculate confidence
|
|
50
|
+
const confidence = this.calculateConfidence(intentions, state);
|
|
51
|
+
return {
|
|
52
|
+
intentions,
|
|
53
|
+
newBeliefs,
|
|
54
|
+
hypotheses,
|
|
55
|
+
questionsToAsk,
|
|
56
|
+
suggestedActions: [],
|
|
57
|
+
confidence,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
/** Get current cognitive state snapshot. */
|
|
61
|
+
getState() {
|
|
62
|
+
return {
|
|
63
|
+
beliefs: this.worldModel.getBeliefs(),
|
|
64
|
+
goals: [],
|
|
65
|
+
workingMemory: this.workingMemory.getItems(),
|
|
66
|
+
currentIntentions: [],
|
|
67
|
+
emotionalContext: '',
|
|
68
|
+
attentionFocus: [],
|
|
69
|
+
lastUpdated: new Date(),
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
generateHypotheses(percept) {
|
|
73
|
+
const hypotheses = [];
|
|
74
|
+
for (const need of percept.implicitNeeds) {
|
|
75
|
+
hypotheses.push(`User might need ${need}`);
|
|
76
|
+
}
|
|
77
|
+
return hypotheses;
|
|
78
|
+
}
|
|
79
|
+
generateQuestions(percept, state) {
|
|
80
|
+
const questions = [];
|
|
81
|
+
// If few beliefs, suggest asking for more context
|
|
82
|
+
if (state.beliefs.length < 3 && percept.requestType !== 'greeting') {
|
|
83
|
+
questions.push('What specific outcome are you looking for?');
|
|
84
|
+
}
|
|
85
|
+
return questions;
|
|
86
|
+
}
|
|
87
|
+
calculateConfidence(intentions, state) {
|
|
88
|
+
let confidence = 0.5;
|
|
89
|
+
// More beliefs = more confident
|
|
90
|
+
confidence += Math.min(0.2, state.beliefs.length * 0.02);
|
|
91
|
+
// Clear primary intention = more confident
|
|
92
|
+
if (intentions.length > 0 && intentions[0].priority >= 8) {
|
|
93
|
+
confidence += 0.1;
|
|
94
|
+
}
|
|
95
|
+
// Few conflicting intentions = more confident
|
|
96
|
+
if (intentions.length <= 3) {
|
|
97
|
+
confidence += 0.1;
|
|
98
|
+
}
|
|
99
|
+
return Math.min(0.95, confidence);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
//# sourceMappingURL=reasoner.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reasoner.js","sourceRoot":"","sources":["../src/reasoner.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAC7C,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AACnD,OAAO,EAAE,kBAAkB,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAA;AAElF;;;;;;;;;GASG;AACH,MAAM,OAAO,QAAQ;IACV,UAAU,CAAY;IACtB,aAAa,CAAe;IACpB,WAAW,CAAiB;IAE7C,YAAY,SAA0B,EAAE;QACtC,IAAI,CAAC,UAAU,GAAG,IAAI,UAAU,EAAE,CAAA;QAClC,IAAI,CAAC,aAAa,GAAG,IAAI,aAAa,CAAC,MAAM,CAAC,gBAAgB,IAAI,EAAE,CAAC,CAAA;QACrE,IAAI,CAAC,WAAW,GAAG,MAAM,CAAC,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AE,CAAC,QAAQ,IAAI,CAAC,EAAE,CAAC;YAC1D,UAAU,IAAI,GAAG,CAAA;QACnB,CAAC;QAED,8CAA8C;QAC9C,IAAI,UAAU,CAAC,MAAM,IAAI,CAAC,EAAE,CAAC;YAC3B,UAAU,IAAI,GAAG,CAAA;QACnB,CAAC;QAED,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,UAAU,CAAC,CAAA;IACnC,CAAC;CACF"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { WorkingMemoryItem, Percept } from '@cognitive-engine/core';
|
|
2
|
+
/**
|
|
3
|
+
* Short-term working memory that holds the current focus.
|
|
4
|
+
* Items are sorted by relevance, oldest get evicted.
|
|
5
|
+
*/
|
|
6
|
+
export declare class WorkingMemory {
|
|
7
|
+
private items;
|
|
8
|
+
private readonly maxItems;
|
|
9
|
+
constructor(maxItems?: number);
|
|
10
|
+
/** Get current working memory contents. */
|
|
11
|
+
getItems(): WorkingMemoryItem[];
|
|
12
|
+
/** Update working memory from a new percept. */
|
|
13
|
+
update(percept: Percept): WorkingMemoryItem[];
|
|
14
|
+
/** Add a specific item. */
|
|
15
|
+
add(item: WorkingMemoryItem): void;
|
|
16
|
+
/** Clear all items. */
|
|
17
|
+
clear(): void;
|
|
18
|
+
get size(): number;
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=working-memory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"working-memory.d.ts","sourceRoot":"","sources":["../src/working-memory.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAA;AAMxE;;;GAGG;AACH,qBAAa,aAAa;IACxB,OAAO,CAAC,KAAK,CAA0B;IACvC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAQ;gBAErB,QAAQ,SAAoB;IAIxC,2CAA2C;IAC3C,QAAQ,IAAI,iBAAiB,EAAE;IAI/B,gDAAgD;IAChD,MAAM,CAAC,OAAO,EAAE,OAAO,GAAG,iBAAiB,EAAE;IAwC7C,2BAA2B;IAC3B,GAAG,CAAC,IAAI,EAAE,iBAAiB,GAAG,IAAI;IAQlC,uBAAuB;IACvB,KAAK,IAAI,IAAI;IAIb,IAAI,IAAI,IAAI,MAAM,CAEjB;CACF"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
const DEFAULT_MAX_ITEMS = 50;
|
|
2
|
+
const RELEVANCE_CURRENT = 10;
|
|
3
|
+
const RELEVANCE_RECENT = 6;
|
|
4
|
+
/**
|
|
5
|
+
* Short-term working memory that holds the current focus.
|
|
6
|
+
* Items are sorted by relevance, oldest get evicted.
|
|
7
|
+
*/
|
|
8
|
+
export class WorkingMemory {
|
|
9
|
+
items = [];
|
|
10
|
+
maxItems;
|
|
11
|
+
constructor(maxItems = DEFAULT_MAX_ITEMS) {
|
|
12
|
+
this.maxItems = maxItems;
|
|
13
|
+
}
|
|
14
|
+
/** Get current working memory contents. */
|
|
15
|
+
getItems() {
|
|
16
|
+
return this.items.slice();
|
|
17
|
+
}
|
|
18
|
+
/** Update working memory from a new percept. */
|
|
19
|
+
update(percept) {
|
|
20
|
+
const now = new Date();
|
|
21
|
+
const newItems = [];
|
|
22
|
+
// Add current message
|
|
23
|
+
newItems.push({
|
|
24
|
+
content: percept.rawText,
|
|
25
|
+
type: 'context',
|
|
26
|
+
relevance: RELEVANCE_CURRENT,
|
|
27
|
+
timestamp: now,
|
|
28
|
+
});
|
|
29
|
+
// Add entities as facts
|
|
30
|
+
for (const entity of percept.entities) {
|
|
31
|
+
newItems.push({
|
|
32
|
+
content: `${entity.type}: ${entity.value}`,
|
|
33
|
+
type: 'fact',
|
|
34
|
+
relevance: entity.confidence * RELEVANCE_CURRENT,
|
|
35
|
+
timestamp: now,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
// Add implicit needs as hypotheses
|
|
39
|
+
for (const need of percept.implicitNeeds) {
|
|
40
|
+
newItems.push({
|
|
41
|
+
content: need,
|
|
42
|
+
type: 'hypothesis',
|
|
43
|
+
relevance: RELEVANCE_RECENT,
|
|
44
|
+
timestamp: now,
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
// Merge, sort by relevance, keep top N
|
|
48
|
+
this.items = [...this.items, ...newItems]
|
|
49
|
+
.sort((a, b) => b.relevance - a.relevance)
|
|
50
|
+
.slice(0, this.maxItems);
|
|
51
|
+
return this.items;
|
|
52
|
+
}
|
|
53
|
+
/** Add a specific item. */
|
|
54
|
+
add(item) {
|
|
55
|
+
this.items.push(item);
|
|
56
|
+
this.items.sort((a, b) => b.relevance - a.relevance);
|
|
57
|
+
if (this.items.length > this.maxItems) {
|
|
58
|
+
this.items = this.items.slice(0, this.maxItems);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/** Clear all items. */
|
|
62
|
+
clear() {
|
|
63
|
+
this.items = [];
|
|
64
|
+
}
|
|
65
|
+
get size() {
|
|
66
|
+
return this.items.length;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=working-memory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"working-memory.js","sourceRoot":"","sources":["../src/working-memory.ts"],"names":[],"mappings":"AAEA,MAAM,iBAAiB,GAAG,EAAE,CAAA;AAC5B,MAAM,iBAAiB,GAAG,EAAE,CAAA;AAC5B,MAAM,gBAAgB,GAAG,CAAC,CAAA;AAE1B;;;GAGG;AACH,MAAM,OAAO,aAAa;IAChB,KAAK,GAAwB,EAAE,CAAA;IACtB,QAAQ,CAAQ;IAEjC,YAAY,QAAQ,GAAG,iBAAiB;QACtC,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAA;IAC1B,CAAC;IAED,2CAA2C;IAC3C,QAAQ;QACN,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAA;IAC3B,CAAC;IAED,gDAAgD;IAChD,MAAM,CAAC,OAAgB;QACrB,MA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AAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,SAAS,GAAG,CAAC,CAAC,SAAS,CAAC,CAAA;QACpD,IAAI,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,IAAI,CAAC,QAAQ,EAAE,CAAC;YACtC,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAA;QACjD,CAAC;IACH,CAAC;IAED,uBAAuB;IACvB,KAAK;QACH,IAAI,CAAC,KAAK,GAAG,EAAE,CAAA;IACjB,CAAC;IAED,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,CAAA;IAC1B,CAAC;CACF"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { Belief, BeliefSource, BeliefCandidate } from '@cognitive-engine/core';
|
|
2
|
+
/**
|
|
3
|
+
* Manages user beliefs (world model) with Bayesian confidence updates.
|
|
4
|
+
*/
|
|
5
|
+
export declare class WorldModel {
|
|
6
|
+
private beliefs;
|
|
7
|
+
/** Get all current beliefs. */
|
|
8
|
+
getBeliefs(): Belief[];
|
|
9
|
+
/** Get beliefs matching a predicate. */
|
|
10
|
+
findByPredicate(predicate: string): Belief[];
|
|
11
|
+
/** Get a specific belief by subject+predicate+object triple. */
|
|
12
|
+
findByTriple(subject: string, predicate: string, object: string): Belief | undefined;
|
|
13
|
+
/**
|
|
14
|
+
* Add or update a belief from a candidate.
|
|
15
|
+
* If the belief already exists, performs Bayesian confidence update.
|
|
16
|
+
*/
|
|
17
|
+
addBelief(candidate: BeliefCandidate, source: BeliefSource): Belief;
|
|
18
|
+
/** Strengthen belief confidence. */
|
|
19
|
+
confirmBelief(beliefId: string): void;
|
|
20
|
+
/** Weaken belief confidence. Auto-deletes if below threshold. */
|
|
21
|
+
weakenBelief(beliefId: string, amount?: number): void;
|
|
22
|
+
/** Apply periodic decay to inferred beliefs. */
|
|
23
|
+
applyDecay(): void;
|
|
24
|
+
/** Remove a specific belief. */
|
|
25
|
+
removeBelief(beliefId: string): void;
|
|
26
|
+
/** Clear all beliefs. */
|
|
27
|
+
clear(): void;
|
|
28
|
+
get size(): number;
|
|
29
|
+
private updateConfidence;
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=world-model.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"world-model.d.ts","sourceRoot":"","sources":["../src/world-model.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAA;AAQnF;;GAEG;AACH,qBAAa,UAAU;IACrB,OAAO,CAAC,OAAO,CAAiC;IAEhD,+BAA+B;IAC/B,UAAU,IAAI,MAAM,EAAE;IAItB,wCAAwC;IACxC,eAAe,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,EAAE;IAI5C,gEAAgE;IAChE,YAAY,CACV,OAAO,EAAE,MAAM,EACf,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,MAAM,GACb,MAAM,GAAG,SAAS;IASrB;;;OAGG;IACH,SAAS,CAAC,SAAS,EAAE,eAAe,EAAE,MAAM,EAAE,YAAY,GAAG,MAAM;IAsCnE,oCAAoC;IACpC,aAAa,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI;IAUrC,iEAAiE;IACjE,YAAY,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,SAAO,GAAG,IAAI;IAenD,gDAAgD;IAChD,UAAU,IAAI,IAAI;IAYlB,gCAAgC;IAChC,YAAY,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI;IAIpC,yBAAyB;IACzB,KAAK,IAAI,IAAI;IAIb,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED,OAAO,CAAC,gBAAgB;CASzB"}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { uid } from '@cognitive-engine/core';
|
|
2
|
+
const SOURCE_WEIGHTS = {
|
|
3
|
+
explicit: 0.9,
|
|
4
|
+
observed: 0.6,
|
|
5
|
+
inferred: 0.4,
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Manages user beliefs (world model) with Bayesian confidence updates.
|
|
9
|
+
*/
|
|
10
|
+
export class WorldModel {
|
|
11
|
+
beliefs = new Map();
|
|
12
|
+
/** Get all current beliefs. */
|
|
13
|
+
getBeliefs() {
|
|
14
|
+
return Array.from(this.beliefs.values());
|
|
15
|
+
}
|
|
16
|
+
/** Get beliefs matching a predicate. */
|
|
17
|
+
findByPredicate(predicate) {
|
|
18
|
+
return this.getBeliefs().filter((b) => b.predicate === predicate);
|
|
19
|
+
}
|
|
20
|
+
/** Get a specific belief by subject+predicate+object triple. */
|
|
21
|
+
findByTriple(subject, predicate, object) {
|
|
22
|
+
return this.getBeliefs().find((b) => b.subject === subject &&
|
|
23
|
+
b.predicate === predicate &&
|
|
24
|
+
b.object === object);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Add or update a belief from a candidate.
|
|
28
|
+
* If the belief already exists, performs Bayesian confidence update.
|
|
29
|
+
*/
|
|
30
|
+
addBelief(candidate, source) {
|
|
31
|
+
const existing = this.findByTriple(candidate.subject, candidate.predicate, candidate.object);
|
|
32
|
+
if (existing) {
|
|
33
|
+
const updated = {
|
|
34
|
+
...existing,
|
|
35
|
+
confidence: this.updateConfidence(existing.confidence, candidate.confidence, source),
|
|
36
|
+
evidence: [...existing.evidence, `Updated at ${new Date().toISOString()}`],
|
|
37
|
+
updatedAt: new Date(),
|
|
38
|
+
};
|
|
39
|
+
this.beliefs.set(updated.id, updated);
|
|
40
|
+
return updated;
|
|
41
|
+
}
|
|
42
|
+
const now = new Date();
|
|
43
|
+
const belief = {
|
|
44
|
+
id: uid('belief'),
|
|
45
|
+
subject: candidate.subject,
|
|
46
|
+
predicate: candidate.predicate,
|
|
47
|
+
object: candidate.object,
|
|
48
|
+
confidence: candidate.confidence,
|
|
49
|
+
source,
|
|
50
|
+
evidence: [],
|
|
51
|
+
createdAt: now,
|
|
52
|
+
updatedAt: now,
|
|
53
|
+
};
|
|
54
|
+
this.beliefs.set(belief.id, belief);
|
|
55
|
+
return belief;
|
|
56
|
+
}
|
|
57
|
+
/** Strengthen belief confidence. */
|
|
58
|
+
confirmBelief(beliefId) {
|
|
59
|
+
const belief = this.beliefs.get(beliefId);
|
|
60
|
+
if (!belief)
|
|
61
|
+
return;
|
|
62
|
+
this.beliefs.set(beliefId, {
|
|
63
|
+
...belief,
|
|
64
|
+
confidence: Math.min(0.99, belief.confidence + 0.1),
|
|
65
|
+
updatedAt: new Date(),
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
/** Weaken belief confidence. Auto-deletes if below threshold. */
|
|
69
|
+
weakenBelief(beliefId, amount = 0.15) {
|
|
70
|
+
const belief = this.beliefs.get(beliefId);
|
|
71
|
+
if (!belief)
|
|
72
|
+
return;
|
|
73
|
+
const newConfidence = belief.confidence - amount;
|
|
74
|
+
if (newConfidence < 0.1) {
|
|
75
|
+
this.beliefs.delete(beliefId);
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
this.beliefs.set(beliefId, {
|
|
79
|
+
...belief,
|
|
80
|
+
confidence: newConfidence,
|
|
81
|
+
updatedAt: new Date(),
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
/** Apply periodic decay to inferred beliefs. */
|
|
86
|
+
applyDecay() {
|
|
87
|
+
for (const [id, belief] of this.beliefs) {
|
|
88
|
+
const decayRate = belief.source === 'inferred' ? 0.1 : 0.05;
|
|
89
|
+
const newConfidence = belief.confidence * (1 - decayRate);
|
|
90
|
+
if (newConfidence < 0.1) {
|
|
91
|
+
this.beliefs.delete(id);
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
this.beliefs.set(id, { ...belief, confidence: newConfidence });
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
/** Remove a specific belief. */
|
|
99
|
+
removeBelief(beliefId) {
|
|
100
|
+
this.beliefs.delete(beliefId);
|
|
101
|
+
}
|
|
102
|
+
/** Clear all beliefs. */
|
|
103
|
+
clear() {
|
|
104
|
+
this.beliefs.clear();
|
|
105
|
+
}
|
|
106
|
+
get size() {
|
|
107
|
+
return this.beliefs.size;
|
|
108
|
+
}
|
|
109
|
+
updateConfidence(prior, newEvidence, source) {
|
|
110
|
+
const weight = SOURCE_WEIGHTS[source];
|
|
111
|
+
const updated = prior * (1 - weight) + newEvidence * weight;
|
|
112
|
+
return Math.min(0.99, Math.max(0.01, updated));
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
//# sourceMappingURL=world-model.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"world-model.js","sourceRoot":"","sources":["../src/world-model.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,wBAAwB,CAAA;AAG5C,MAAM,cAAc,GAAiC;IACnD,QAAQ,EAAE,GAAG;IACb,QAAQ,EAAE,GAAG;IACb,QAAQ,EAAE,GAAG;CACd,CAAA;AAED;;GAEG;AACH,MAAM,OAAO,UAAU;IACb,OAAO,GAAwB,IAAI,GAAG,EAAE,CAAA;IAEhD,+BAA+B;IAC/B,UAAU;QACR,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC,CAAA;IAC1C,CAAC;IAED,wCAAwC;IACxC,eAAe,CAAC,SAAiB;QAC/B,OAAO,IAAI,CAAC,UAAU,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,SAAS,KAAK,SAAS,CAAC,CAAA;IACnE,CAAC;IAED,gEAAgE;IAChE,YAAY,CACV,OAAe,EACf,SAAiB,EACjB,MAAc;QAEd,OAAO,IAAI,CAAC,UAAU,EAAE,CAAC,IAAI,CAC3B,CAAC,C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,GAAG,SAAS,CAAC,CAAA;YACzD,IAAI,aAAa,GAAG,GAAG,EAAE,CAAC;gBACxB,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC,CAAA;YACzB,CAAC;iBAAM,CAAC;gBACN,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,EAAE,EAAE,GAAG,MAAM,EAAE,UAAU,EAAE,aAAa,EAAE,CAAC,CAAA;YAChE,CAAC;QACH,CAAC;IACH,CAAC;IAED,gCAAgC;IAChC,YAAY,CAAC,QAAgB;QAC3B,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAA;IAC/B,CAAC;IAED,yBAAyB;IACzB,KAAK;QACH,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAA;IACtB,CAAC;IAED,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAA;IAC1B,CAAC;IAEO,gBAAgB,CACtB,KAAa,EACb,WAAmB,EACnB,MAAoB;QAEpB,MAAM,MAAM,GAAG,cAAc,CAAC,MAAM,CAAC,CAAA;QACrC,MAAM,OAAO,GAAG,KAAK,GAAG,CAAC,CAAC,GAAG,MAAM,CAAC,GAAG,WAAW,GAAG,MAAM,CAAA;QAC3D,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC,CAAA;IAChD,CAAC;CACF"}
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cognitive-engine/reasoning",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "BDI reasoning engine: beliefs, intentions, inference rules, working memory",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "tsc",
|
|
19
|
+
"test": "vitest run",
|
|
20
|
+
"test:watch": "vitest",
|
|
21
|
+
"lint": "eslint src/",
|
|
22
|
+
"clean": "rm -rf dist"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@cognitive-engine/core": "*"
|
|
26
|
+
},
|
|
27
|
+
"license": "Apache-2.0",
|
|
28
|
+
"author": "Dmitry Zorin",
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "https://github.com/medonomator/cognitive-engine.git",
|
|
32
|
+
"directory": "packages/reasoning"
|
|
33
|
+
},
|
|
34
|
+
"homepage": "https://github.com/medonomator/cognitive-engine/tree/main/packages/reasoning",
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public"
|
|
37
|
+
}
|
|
38
|
+
}
|