@dsh-ray/assistant-association 0.0.0-stage → 0.2.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 +133 -2
- package/cordis.patch.yml +26 -0
- package/lib/aho-corasick.d.ts +41 -0
- package/lib/aho-corasick.d.ts.map +1 -0
- package/lib/aho-corasick.js +352 -0
- package/lib/aho-corasick.js.map +1 -0
- package/lib/bm25.d.ts +17 -0
- package/lib/bm25.d.ts.map +1 -0
- package/lib/bm25.js +73 -0
- package/lib/bm25.js.map +1 -0
- package/lib/clustering.d.ts +161 -0
- package/lib/clustering.d.ts.map +1 -0
- package/lib/clustering.js +1043 -0
- package/lib/clustering.js.map +1 -0
- package/lib/config.d.ts +67 -0
- package/lib/config.d.ts.map +1 -0
- package/lib/config.js +834 -0
- package/lib/config.js.map +1 -0
- package/lib/cues.d.ts +134 -0
- package/lib/cues.d.ts.map +1 -0
- package/lib/cues.js +793 -0
- package/lib/cues.js.map +1 -0
- package/lib/dreaming.d.ts +211 -0
- package/lib/dreaming.d.ts.map +1 -0
- package/lib/dreaming.js +1418 -0
- package/lib/dreaming.js.map +1 -0
- package/lib/entities.d.ts +18 -0
- package/lib/entities.d.ts.map +1 -0
- package/lib/entities.js +111 -0
- package/lib/entities.js.map +1 -0
- package/lib/eval-pool.d.ts +111 -0
- package/lib/eval-pool.d.ts.map +1 -0
- package/lib/eval-pool.js +392 -0
- package/lib/eval-pool.js.map +1 -0
- package/lib/hebbian.d.ts +54 -0
- package/lib/hebbian.d.ts.map +1 -0
- package/lib/hebbian.js +226 -0
- package/lib/hebbian.js.map +1 -0
- package/lib/holdout.d.ts +18 -0
- package/lib/holdout.d.ts.map +1 -0
- package/lib/holdout.js +97 -0
- package/lib/holdout.js.map +1 -0
- package/lib/hot-path.d.ts +8 -0
- package/lib/hot-path.d.ts.map +1 -0
- package/lib/hot-path.js +503 -0
- package/lib/hot-path.js.map +1 -0
- package/lib/hysteresis.d.ts +39 -0
- package/lib/hysteresis.d.ts.map +1 -0
- package/lib/hysteresis.js +297 -0
- package/lib/hysteresis.js.map +1 -0
- package/lib/index-memory.d.ts +112 -0
- package/lib/index-memory.d.ts.map +1 -0
- package/lib/index-memory.js +391 -0
- package/lib/index-memory.js.map +1 -0
- package/lib/index.d.ts +37 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +37 -0
- package/lib/index.js.map +1 -0
- package/lib/intake-signal.d.ts +158 -0
- package/lib/intake-signal.d.ts.map +1 -0
- package/lib/intake-signal.js +366 -0
- package/lib/intake-signal.js.map +1 -0
- package/lib/param-tuning.d.ts +302 -0
- package/lib/param-tuning.d.ts.map +1 -0
- package/lib/param-tuning.js +1743 -0
- package/lib/param-tuning.js.map +1 -0
- package/lib/service.d.ts +164 -0
- package/lib/service.d.ts.map +1 -0
- package/lib/service.js +2071 -0
- package/lib/service.js.map +1 -0
- package/lib/sources.d.ts +42 -0
- package/lib/sources.d.ts.map +1 -0
- package/lib/sources.js +2024 -0
- package/lib/sources.js.map +1 -0
- package/lib/sqlite.d.ts +88 -0
- package/lib/sqlite.d.ts.map +1 -0
- package/lib/sqlite.js +1815 -0
- package/lib/sqlite.js.map +1 -0
- package/lib/tokenize.d.ts +4 -0
- package/lib/tokenize.d.ts.map +1 -0
- package/lib/tokenize.js +35 -0
- package/lib/tokenize.js.map +1 -0
- package/lib/tools.d.ts +27 -0
- package/lib/tools.d.ts.map +1 -0
- package/lib/tools.js +126 -0
- package/lib/tools.js.map +1 -0
- package/lib/types.d.ts +551 -0
- package/lib/types.d.ts.map +1 -0
- package/lib/types.js +163 -0
- package/lib/types.js.map +1 -0
- package/lib/version.d.ts +2 -0
- package/lib/version.d.ts.map +1 -0
- package/lib/version.js +2 -0
- package/lib/version.js.map +1 -0
- package/lib/web-fetch.d.ts +95 -0
- package/lib/web-fetch.d.ts.map +1 -0
- package/lib/web-fetch.js +833 -0
- package/lib/web-fetch.js.map +1 -0
- package/lib/web-learning-interests.d.ts +42 -0
- package/lib/web-learning-interests.d.ts.map +1 -0
- package/lib/web-learning-interests.js +4344 -0
- package/lib/web-learning-interests.js.map +1 -0
- package/lib/web-learning-runner.d.ts +43 -0
- package/lib/web-learning-runner.d.ts.map +1 -0
- package/lib/web-learning-runner.js +322 -0
- package/lib/web-learning-runner.js.map +1 -0
- package/lib/web-learning-worker.d.ts +75 -0
- package/lib/web-learning-worker.d.ts.map +1 -0
- package/lib/web-learning-worker.js +367 -0
- package/lib/web-learning-worker.js.map +1 -0
- package/lib/web-learning.d.ts +161 -0
- package/lib/web-learning.d.ts.map +1 -0
- package/lib/web-learning.js +912 -0
- package/lib/web-learning.js.map +1 -0
- package/lib/write-queue.d.ts +42 -0
- package/lib/write-queue.d.ts.map +1 -0
- package/lib/write-queue.js +439 -0
- package/lib/write-queue.js.map +1 -0
- package/package.json +156 -3
package/lib/cues.js
ADDED
|
@@ -0,0 +1,793 @@
|
|
|
1
|
+
import { isAbsolute } from 'node:path';
|
|
2
|
+
import { isBoundaryMatch, stepAcState } from './aho-corasick.js';
|
|
3
|
+
import { extractEntities } from './entities.js';
|
|
4
|
+
import { MAX_WEAK_USE_TEXT_CHARS } from './hebbian.js';
|
|
5
|
+
import { normalizeEntityKey, normalizeForAc } from './tokenize.js';
|
|
6
|
+
import { computeOwnerScopeKey, computePrincipalDigest, } from './types.js';
|
|
7
|
+
const MAX_CUE_TEXT_CHARS = 2_048;
|
|
8
|
+
const MAX_TOOL_RESULT_SCAN_CHARS = 16_384;
|
|
9
|
+
const MAX_TOOL_ENTITY_CUES = 32;
|
|
10
|
+
const MAX_SESSION_ACTIVATION_ENTRIES = 128;
|
|
11
|
+
const MAX_TOOL_NAMES_PER_TURN = 64;
|
|
12
|
+
const MAX_TOOL_TURN_KEYS = 512;
|
|
13
|
+
const MAX_CALL_LOOKBACK = 10_000;
|
|
14
|
+
const DEFAULT_SESSION_HALF_LIFE_MS = 300_000;
|
|
15
|
+
const DEFAULT_SESSION_STEP_DECAY = 0.85;
|
|
16
|
+
const HUMAN_TASK_SOURCE_KINDS = new Set([
|
|
17
|
+
'user',
|
|
18
|
+
'delivery',
|
|
19
|
+
'assistant-automations',
|
|
20
|
+
'plugin:@dsh-ray/assistant-automations',
|
|
21
|
+
]);
|
|
22
|
+
const MAX_TASK_EXPOSURES_CACHE = 32;
|
|
23
|
+
const MAX_TURN_TASK_IDS_CACHE = 32;
|
|
24
|
+
export function createAgentAssocState() {
|
|
25
|
+
return {
|
|
26
|
+
ownerKey: undefined,
|
|
27
|
+
inputNonce: 0,
|
|
28
|
+
invalidationNonce: 0,
|
|
29
|
+
claimedUserMessage: undefined,
|
|
30
|
+
toolEntities: new Map(),
|
|
31
|
+
hasNewToolEntity: false,
|
|
32
|
+
sessionActivation: new Map(),
|
|
33
|
+
retainedItems: new Map(),
|
|
34
|
+
retainedBlock: '',
|
|
35
|
+
retainedDirty: false,
|
|
36
|
+
overflowDroppedIds: new Set(),
|
|
37
|
+
lastEvaluatedNonce: -1,
|
|
38
|
+
lastMetricNonce: -1,
|
|
39
|
+
lastStepOutcome: undefined,
|
|
40
|
+
memo: undefined,
|
|
41
|
+
recordedTaskIds: new Set(),
|
|
42
|
+
turnPromptEntitiesTurn: -1,
|
|
43
|
+
turnPromptEntities: new Set(),
|
|
44
|
+
taskExposures: new Map(),
|
|
45
|
+
turnTaskIds: new Map(),
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
export function getOrCreateAgentAssocState(states, agent) {
|
|
49
|
+
let state = states.get(agent);
|
|
50
|
+
if (state === undefined) {
|
|
51
|
+
state = createAgentAssocState();
|
|
52
|
+
states.set(agent, state);
|
|
53
|
+
}
|
|
54
|
+
return state;
|
|
55
|
+
}
|
|
56
|
+
export function wipeStateOnOwnerRotation(state, nextOwnerKey) {
|
|
57
|
+
if (state.ownerKey !== undefined && state.ownerKey === nextOwnerKey) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
const rotated = state.ownerKey !== undefined && state.ownerKey !== nextOwnerKey;
|
|
61
|
+
state.ownerKey = nextOwnerKey;
|
|
62
|
+
if (rotated) {
|
|
63
|
+
state.toolEntities.clear();
|
|
64
|
+
state.hasNewToolEntity = false;
|
|
65
|
+
state.sessionActivation.clear();
|
|
66
|
+
state.retainedItems.clear();
|
|
67
|
+
state.retainedBlock = '';
|
|
68
|
+
state.retainedDirty = false;
|
|
69
|
+
state.overflowDroppedIds.clear();
|
|
70
|
+
state.memo = undefined;
|
|
71
|
+
state.lastEvaluatedNonce = -1;
|
|
72
|
+
state.lastMetricNonce = -1;
|
|
73
|
+
state.lastStepOutcome = undefined;
|
|
74
|
+
state.recordedTaskIds.clear();
|
|
75
|
+
state.turnPromptEntitiesTurn = -1;
|
|
76
|
+
state.turnPromptEntities.clear();
|
|
77
|
+
state.taskExposures.clear();
|
|
78
|
+
state.turnTaskIds.clear();
|
|
79
|
+
state.invalidationNonce += 1;
|
|
80
|
+
}
|
|
81
|
+
return rotated;
|
|
82
|
+
}
|
|
83
|
+
export function evictNodesFromAgentState(state, nodeIds) {
|
|
84
|
+
if (nodeIds.size === 0)
|
|
85
|
+
return;
|
|
86
|
+
let changed = false;
|
|
87
|
+
let retainedEvicted = false;
|
|
88
|
+
for (const id of nodeIds) {
|
|
89
|
+
if (state.retainedItems.delete(id)) {
|
|
90
|
+
changed = true;
|
|
91
|
+
retainedEvicted = true;
|
|
92
|
+
}
|
|
93
|
+
if (state.sessionActivation.delete(id))
|
|
94
|
+
changed = true;
|
|
95
|
+
state.overflowDroppedIds.delete(id);
|
|
96
|
+
for (const expMap of state.taskExposures.values()) {
|
|
97
|
+
expMap.delete(id);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
if (retainedEvicted) {
|
|
101
|
+
state.retainedDirty = true;
|
|
102
|
+
state.retainedBlock = '';
|
|
103
|
+
}
|
|
104
|
+
if (changed) {
|
|
105
|
+
state.invalidationNonce += 1;
|
|
106
|
+
state.memo = undefined;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
export function ensureTurnPromptEntities(state, turn) {
|
|
110
|
+
if (state.turnPromptEntitiesTurn !== turn) {
|
|
111
|
+
state.turnPromptEntities.clear();
|
|
112
|
+
state.turnPromptEntitiesTurn = turn;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
export function addPromptEntitiesFromText(state, text, activeIndex) {
|
|
116
|
+
if (text.trim().length === 0)
|
|
117
|
+
return;
|
|
118
|
+
for (const raw of extractEntities(text, 256)) {
|
|
119
|
+
const key = normalizeEntityKey(raw);
|
|
120
|
+
if (key !== null) {
|
|
121
|
+
state.turnPromptEntities.add(key);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
if (activeIndex !== undefined && activeIndex !== null) {
|
|
125
|
+
const acOpts = state.ownerKey !== undefined ? { ownerScopeKey: state.ownerKey } : undefined;
|
|
126
|
+
for (const hit of extractAcEntitiesFromText(activeIndex.ac, text, acOpts)) {
|
|
127
|
+
const key = normalizeEntityKey(hit.entityId);
|
|
128
|
+
if (key !== null) {
|
|
129
|
+
state.turnPromptEntities.add(key);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
export function recordTaskExposures(state, taskId, items) {
|
|
135
|
+
if (items.length === 0)
|
|
136
|
+
return;
|
|
137
|
+
let map = state.taskExposures.get(taskId);
|
|
138
|
+
if (map === undefined) {
|
|
139
|
+
map = new Map();
|
|
140
|
+
state.taskExposures.set(taskId, map);
|
|
141
|
+
while (state.taskExposures.size > MAX_TASK_EXPOSURES_CACHE) {
|
|
142
|
+
const oldest = state.taskExposures.keys().next().value;
|
|
143
|
+
if (oldest === undefined)
|
|
144
|
+
break;
|
|
145
|
+
state.taskExposures.delete(oldest);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
state.taskExposures.delete(taskId);
|
|
150
|
+
state.taskExposures.set(taskId, map);
|
|
151
|
+
}
|
|
152
|
+
for (const item of items) {
|
|
153
|
+
const existing = map.get(item.id);
|
|
154
|
+
if (existing !== undefined) {
|
|
155
|
+
map.set(item.id, {
|
|
156
|
+
nodeId: item.id,
|
|
157
|
+
sourcePlugin: item.sourcePlugin,
|
|
158
|
+
hop: item.hop,
|
|
159
|
+
score: item.score,
|
|
160
|
+
reason: item.reason,
|
|
161
|
+
provenanceEdges: item.hop === 0 ? [] : (item.provenanceEdges ?? existing.provenanceEdges),
|
|
162
|
+
strongUsed: existing.strongUsed,
|
|
163
|
+
weakUsed: existing.weakUsed,
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
map.set(item.id, {
|
|
168
|
+
nodeId: item.id,
|
|
169
|
+
sourcePlugin: item.sourcePlugin,
|
|
170
|
+
hop: item.hop,
|
|
171
|
+
score: item.score,
|
|
172
|
+
reason: item.reason,
|
|
173
|
+
provenanceEdges: item.provenanceEdges ?? [],
|
|
174
|
+
strongUsed: false,
|
|
175
|
+
weakUsed: false,
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
export function recordTurnTaskId(state, turn, taskId) {
|
|
181
|
+
state.turnTaskIds.delete(turn);
|
|
182
|
+
state.turnTaskIds.set(turn, taskId);
|
|
183
|
+
while (state.turnTaskIds.size > MAX_TURN_TASK_IDS_CACHE) {
|
|
184
|
+
const oldest = state.turnTaskIds.keys().next().value;
|
|
185
|
+
if (oldest === undefined)
|
|
186
|
+
break;
|
|
187
|
+
state.turnTaskIds.delete(oldest);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
function isHumanOrTaskMessage(message) {
|
|
191
|
+
if (message.role !== 'user')
|
|
192
|
+
return false;
|
|
193
|
+
const sourceKind = message.source?.kind;
|
|
194
|
+
if (typeof sourceKind !== 'string' || !HUMAN_TASK_SOURCE_KINDS.has(sourceKind)) {
|
|
195
|
+
return false;
|
|
196
|
+
}
|
|
197
|
+
return message.content.some((block) => block.type === 'text' && block.text.trim().length > 0);
|
|
198
|
+
}
|
|
199
|
+
function extractTextBounded(blocks, maxChars) {
|
|
200
|
+
let out = '';
|
|
201
|
+
for (const block of blocks) {
|
|
202
|
+
if (block.type !== 'text' || typeof block.text !== 'string')
|
|
203
|
+
continue;
|
|
204
|
+
const piece = block.text;
|
|
205
|
+
if (piece.length === 0)
|
|
206
|
+
continue;
|
|
207
|
+
if (out.length === 0) {
|
|
208
|
+
out = piece.slice(0, maxChars);
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
const remaining = maxChars - out.length;
|
|
212
|
+
if (remaining <= 1)
|
|
213
|
+
break;
|
|
214
|
+
out += `\n${piece.slice(0, remaining - 1)}`;
|
|
215
|
+
}
|
|
216
|
+
if (out.length >= maxChars)
|
|
217
|
+
break;
|
|
218
|
+
}
|
|
219
|
+
return out;
|
|
220
|
+
}
|
|
221
|
+
export function handleInboxClaimed(state, message, turn, activeIndex) {
|
|
222
|
+
state.inputNonce += 1;
|
|
223
|
+
ensureTurnPromptEntities(state, turn);
|
|
224
|
+
if (isHumanOrTaskMessage(message)) {
|
|
225
|
+
const text = extractTextBounded(message.content, MAX_CUE_TEXT_CHARS);
|
|
226
|
+
state.claimedUserMessage = {
|
|
227
|
+
turn,
|
|
228
|
+
messageId: String(message.id),
|
|
229
|
+
text,
|
|
230
|
+
sourceKind: message.source.kind,
|
|
231
|
+
};
|
|
232
|
+
addPromptEntitiesFromText(state, text, activeIndex);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
export function handleInboxDiscarded(state, message) {
|
|
236
|
+
state.inputNonce += 1;
|
|
237
|
+
if (state.claimedUserMessage?.messageId === String(message.id)) {
|
|
238
|
+
state.claimedUserMessage = undefined;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
export function handleAgentStatus(state, status) {
|
|
242
|
+
if (status === 'idle') {
|
|
243
|
+
state.claimedUserMessage = undefined;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
export function extractAcEntitiesFromText(ac, rawText, options) {
|
|
247
|
+
if (ac.entityCount === 0 || rawText.length === 0)
|
|
248
|
+
return [];
|
|
249
|
+
const filterByOwner = options?.ownerScopeKey !== undefined;
|
|
250
|
+
const acOwnerIdx = filterByOwner ? (ac.ownerKeyToIdx.get(options.ownerScopeKey) ?? -1) : -1;
|
|
251
|
+
const allowLowTrust = options?.allowLowTrust ?? true;
|
|
252
|
+
const normText = normalizeForAc(rawText);
|
|
253
|
+
const seen = new Set();
|
|
254
|
+
const matches = [];
|
|
255
|
+
let acState = 0;
|
|
256
|
+
for (let i = 0; i < normText.length; i += 1) {
|
|
257
|
+
const ch = normText.charCodeAt(i);
|
|
258
|
+
acState = stepAcState(ac, acState, ch);
|
|
259
|
+
const oStart = ac.outOffset[acState];
|
|
260
|
+
const oEnd = ac.outOffset[acState + 1];
|
|
261
|
+
if (oStart === oEnd)
|
|
262
|
+
continue;
|
|
263
|
+
for (let oi = oStart; oi < oEnd; oi += 1) {
|
|
264
|
+
const startPos = isBoundaryMatch(ac, oi, normText, i);
|
|
265
|
+
if (startPos < 0)
|
|
266
|
+
continue;
|
|
267
|
+
if (filterByOwner) {
|
|
268
|
+
const synOwnerIdx = ac.outSynonymOwnerIdx[oi];
|
|
269
|
+
if (synOwnerIdx >= 0 && synOwnerIdx !== acOwnerIdx)
|
|
270
|
+
continue;
|
|
271
|
+
}
|
|
272
|
+
if (!allowLowTrust && ac.outSynonymTrust[oi] === 0)
|
|
273
|
+
continue;
|
|
274
|
+
const eid = ac.outEntityId[oi];
|
|
275
|
+
if (seen.has(eid))
|
|
276
|
+
continue;
|
|
277
|
+
seen.add(eid);
|
|
278
|
+
const entityName = ac.entityNames[eid];
|
|
279
|
+
if (entityName !== undefined) {
|
|
280
|
+
matches.push({ entityId: entityName, surface: entityName });
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
return matches;
|
|
285
|
+
}
|
|
286
|
+
function recordSessionTurnToolName(toolNamesBySessionTurn, sessionId, turn, toolName) {
|
|
287
|
+
const key = `${sessionId}\0${turn}`;
|
|
288
|
+
let list = toolNamesBySessionTurn.get(key);
|
|
289
|
+
if (list === undefined) {
|
|
290
|
+
list = [];
|
|
291
|
+
toolNamesBySessionTurn.set(key, list);
|
|
292
|
+
while (toolNamesBySessionTurn.size > MAX_TOOL_TURN_KEYS) {
|
|
293
|
+
const oldest = toolNamesBySessionTurn.keys().next().value;
|
|
294
|
+
if (oldest === undefined)
|
|
295
|
+
break;
|
|
296
|
+
toolNamesBySessionTurn.delete(oldest);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
else {
|
|
300
|
+
// Refresh LRU order
|
|
301
|
+
toolNamesBySessionTurn.delete(key);
|
|
302
|
+
toolNamesBySessionTurn.set(key, list);
|
|
303
|
+
}
|
|
304
|
+
if (list.length < MAX_TOOL_NAMES_PER_TURN) {
|
|
305
|
+
list.push(toolName);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
export function consumeSessionTurnToolNames(toolNamesBySessionTurn, sessionId, turn) {
|
|
309
|
+
if (turn !== undefined) {
|
|
310
|
+
const key = `${sessionId}\0${turn}`;
|
|
311
|
+
const exact = toolNamesBySessionTurn.get(key);
|
|
312
|
+
if (exact !== undefined) {
|
|
313
|
+
toolNamesBySessionTurn.delete(key);
|
|
314
|
+
return [...exact];
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
const prefix = `${sessionId}\0`;
|
|
318
|
+
const merged = [];
|
|
319
|
+
for (const [key, list] of toolNamesBySessionTurn) {
|
|
320
|
+
if (!key.startsWith(prefix))
|
|
321
|
+
continue;
|
|
322
|
+
for (const name of list) {
|
|
323
|
+
if (merged.length >= MAX_TOOL_NAMES_PER_TURN)
|
|
324
|
+
break;
|
|
325
|
+
merged.push(name);
|
|
326
|
+
}
|
|
327
|
+
toolNamesBySessionTurn.delete(key);
|
|
328
|
+
}
|
|
329
|
+
return merged;
|
|
330
|
+
}
|
|
331
|
+
function resolveToolCallNameForResult(session, eventSeq, turn, step, callId) {
|
|
332
|
+
const earliest = Math.max(0, eventSeq - MAX_CALL_LOOKBACK);
|
|
333
|
+
for (let seq = eventSeq - 1; seq >= earliest; seq -= 1) {
|
|
334
|
+
const candidate = session.eventAt(seq);
|
|
335
|
+
if (candidate?.type === 'tool/result' && candidate.data.message.source.callId === callId) {
|
|
336
|
+
return undefined;
|
|
337
|
+
}
|
|
338
|
+
if (candidate?.type !== 'tool/call')
|
|
339
|
+
continue;
|
|
340
|
+
if (candidate.data.callId !== callId)
|
|
341
|
+
continue;
|
|
342
|
+
if (candidate.data.turn !== turn || candidate.data.step !== step)
|
|
343
|
+
return undefined;
|
|
344
|
+
return candidate.data.name;
|
|
345
|
+
}
|
|
346
|
+
return undefined;
|
|
347
|
+
}
|
|
348
|
+
export function handleSessionEvent(state, session, event, activeIndex, toolNamesBySessionTurn) {
|
|
349
|
+
if (event.type === 'user/message') {
|
|
350
|
+
if (event.data.source?.kind === 'runtime-context') {
|
|
351
|
+
return {};
|
|
352
|
+
}
|
|
353
|
+
state.inputNonce += 1;
|
|
354
|
+
const text = extractTextBounded(event.data.content, MAX_CUE_TEXT_CHARS);
|
|
355
|
+
if (text.length > 0) {
|
|
356
|
+
addPromptEntitiesFromText(state, text, activeIndex);
|
|
357
|
+
}
|
|
358
|
+
return {};
|
|
359
|
+
}
|
|
360
|
+
if (event.type === 'turn/start') {
|
|
361
|
+
state.inputNonce += 1;
|
|
362
|
+
ensureTurnPromptEntities(state, event.data.turn);
|
|
363
|
+
return {};
|
|
364
|
+
}
|
|
365
|
+
if (event.type === 'step/end') {
|
|
366
|
+
state.inputNonce += 1;
|
|
367
|
+
return {};
|
|
368
|
+
}
|
|
369
|
+
if (event.type === 'assistant/message') {
|
|
370
|
+
state.inputNonce += 1;
|
|
371
|
+
const data = event.data;
|
|
372
|
+
const assistantText = data.interrupted !== true
|
|
373
|
+
? extractTextBounded(data.message.content, MAX_WEAK_USE_TEXT_CHARS)
|
|
374
|
+
: '';
|
|
375
|
+
let inputTokensDelta = null;
|
|
376
|
+
let outputTokensDelta = null;
|
|
377
|
+
let costUsdMicrosDelta = null;
|
|
378
|
+
if (data.usage !== undefined) {
|
|
379
|
+
const u = data.usage;
|
|
380
|
+
const uncached = Number.isFinite(u.inputTokens) ? Math.max(0, Math.trunc(u.inputTokens)) : 0;
|
|
381
|
+
const cacheRead = Number.isFinite(u.cacheReadTokens)
|
|
382
|
+
? Math.max(0, Math.trunc(u.cacheReadTokens))
|
|
383
|
+
: 0;
|
|
384
|
+
const cacheWrite = Number.isFinite(u.cacheWriteTokens)
|
|
385
|
+
? Math.max(0, Math.trunc(u.cacheWriteTokens))
|
|
386
|
+
: 0;
|
|
387
|
+
inputTokensDelta = uncached + cacheRead + cacheWrite;
|
|
388
|
+
outputTokensDelta = Number.isFinite(u.outputTokens)
|
|
389
|
+
? Math.max(0, Math.trunc(u.outputTokens))
|
|
390
|
+
: 0;
|
|
391
|
+
const rawCost = u.costUsdMicros;
|
|
392
|
+
costUsdMicrosDelta =
|
|
393
|
+
typeof rawCost === 'number' && Number.isFinite(rawCost)
|
|
394
|
+
? Math.max(0, Math.trunc(rawCost))
|
|
395
|
+
: null;
|
|
396
|
+
}
|
|
397
|
+
return {
|
|
398
|
+
...(assistantText.length > 0
|
|
399
|
+
? { assistantText, assistantTurn: data.turn }
|
|
400
|
+
: {}),
|
|
401
|
+
telemetry: {
|
|
402
|
+
turn: data.turn,
|
|
403
|
+
stepsDelta: 1,
|
|
404
|
+
turnsDelta: 0,
|
|
405
|
+
inputTokensDelta,
|
|
406
|
+
outputTokensDelta,
|
|
407
|
+
costUsdMicrosDelta,
|
|
408
|
+
occurredAt: event.time,
|
|
409
|
+
},
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
if (event.type === 'turn/end') {
|
|
413
|
+
state.inputNonce += 1;
|
|
414
|
+
if (state.claimedUserMessage?.turn === event.data.turn) {
|
|
415
|
+
state.claimedUserMessage = undefined;
|
|
416
|
+
}
|
|
417
|
+
return {
|
|
418
|
+
telemetry: {
|
|
419
|
+
turn: event.data.turn,
|
|
420
|
+
stepsDelta: 0,
|
|
421
|
+
turnsDelta: 1,
|
|
422
|
+
inputTokensDelta: null,
|
|
423
|
+
outputTokensDelta: null,
|
|
424
|
+
costUsdMicrosDelta: null,
|
|
425
|
+
occurredAt: event.time,
|
|
426
|
+
},
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
if (event.type === 'tool/call') {
|
|
430
|
+
recordSessionTurnToolName(toolNamesBySessionTurn, String(session.id), event.data.turn, event.data.name);
|
|
431
|
+
return {};
|
|
432
|
+
}
|
|
433
|
+
if (event.type === 'tool/result') {
|
|
434
|
+
const surfaceOp = event.surfaceOp;
|
|
435
|
+
if (surfaceOp !== 'append')
|
|
436
|
+
return {};
|
|
437
|
+
state.inputNonce += 1;
|
|
438
|
+
const msg = event.data.message;
|
|
439
|
+
const callId = String(msg.source.callId);
|
|
440
|
+
const toolName = resolveToolCallNameForResult(session, event.seq, event.data.turn, event.data.step, callId);
|
|
441
|
+
if (toolName === 'assoc_read') {
|
|
442
|
+
// Never echo untrusted <association_item> wrapper prose back into step cues
|
|
443
|
+
return {};
|
|
444
|
+
}
|
|
445
|
+
// Read tool result text into a local bounded buffer, extract entities,
|
|
446
|
+
// and discard the local buffer immediately without retaining raw prose.
|
|
447
|
+
const localText = extractTextBounded(msg.content, MAX_TOOL_RESULT_SCAN_CHARS);
|
|
448
|
+
if (localText.length === 0)
|
|
449
|
+
return {};
|
|
450
|
+
ensureTurnPromptEntities(state, event.data.turn);
|
|
451
|
+
for (const raw of extractEntities(localText, 256)) {
|
|
452
|
+
const key = normalizeEntityKey(raw);
|
|
453
|
+
if (key !== null) {
|
|
454
|
+
state.turnPromptEntities.add(key);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
if (activeIndex === null)
|
|
458
|
+
return {};
|
|
459
|
+
const matched = extractAcEntitiesFromText(activeIndex.ac, localText, state.ownerKey !== undefined ? { ownerScopeKey: state.ownerKey } : undefined);
|
|
460
|
+
if (matched.length > 0) {
|
|
461
|
+
state.hasNewToolEntity = true;
|
|
462
|
+
}
|
|
463
|
+
for (const hit of matched) {
|
|
464
|
+
const key = normalizeEntityKey(hit.entityId);
|
|
465
|
+
if (key !== null) {
|
|
466
|
+
state.turnPromptEntities.add(key);
|
|
467
|
+
}
|
|
468
|
+
const existing = state.toolEntities.get(hit.entityId);
|
|
469
|
+
if (existing !== undefined) {
|
|
470
|
+
existing.hits += 1;
|
|
471
|
+
state.toolEntities.delete(hit.entityId);
|
|
472
|
+
state.toolEntities.set(hit.entityId, {
|
|
473
|
+
entityId: hit.entityId,
|
|
474
|
+
surface: hit.surface,
|
|
475
|
+
turn: event.data.turn,
|
|
476
|
+
step: event.data.step,
|
|
477
|
+
observedAt: event.time,
|
|
478
|
+
hits: existing.hits,
|
|
479
|
+
});
|
|
480
|
+
}
|
|
481
|
+
else {
|
|
482
|
+
state.toolEntities.set(hit.entityId, {
|
|
483
|
+
entityId: hit.entityId,
|
|
484
|
+
surface: hit.surface,
|
|
485
|
+
turn: event.data.turn,
|
|
486
|
+
step: event.data.step,
|
|
487
|
+
observedAt: event.time,
|
|
488
|
+
hits: 1,
|
|
489
|
+
});
|
|
490
|
+
while (state.toolEntities.size > MAX_TOOL_ENTITY_CUES) {
|
|
491
|
+
const oldest = state.toolEntities.keys().next().value;
|
|
492
|
+
if (oldest === undefined)
|
|
493
|
+
break;
|
|
494
|
+
state.toolEntities.delete(oldest);
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
return {};
|
|
500
|
+
}
|
|
501
|
+
export function resolveOwnerScope(ctx, agent, options) {
|
|
502
|
+
if (agent === undefined)
|
|
503
|
+
return undefined;
|
|
504
|
+
const agents = ctx.get('agents', false);
|
|
505
|
+
if (agents !== undefined && agents.get(agent.id) !== agent)
|
|
506
|
+
return undefined;
|
|
507
|
+
const sessions = ctx.get('sessions', false);
|
|
508
|
+
if (sessions !== undefined && sessions.get(agent.session.id) !== agent.session)
|
|
509
|
+
return undefined;
|
|
510
|
+
const workspace = agent.session.header.cwd;
|
|
511
|
+
const agentPreset = agent.session.header.agentPreset;
|
|
512
|
+
if (typeof workspace !== 'string' || !isAbsolute(workspace))
|
|
513
|
+
return undefined;
|
|
514
|
+
if (typeof agentPreset !== 'string' || agentPreset.trim() === '')
|
|
515
|
+
return undefined;
|
|
516
|
+
const delivery = ctx.get('assistantDelivery', false);
|
|
517
|
+
if (delivery !== undefined && typeof delivery.preferencePrincipalForAgent === 'function') {
|
|
518
|
+
let attestation;
|
|
519
|
+
try {
|
|
520
|
+
attestation = delivery.preferencePrincipalForAgent(agent);
|
|
521
|
+
}
|
|
522
|
+
catch {
|
|
523
|
+
return undefined;
|
|
524
|
+
}
|
|
525
|
+
if (attestation === undefined ||
|
|
526
|
+
attestation.scope.workspace !== workspace ||
|
|
527
|
+
attestation.scope.preset !== agentPreset ||
|
|
528
|
+
attestation.sessionId !== String(agent.session.id)) {
|
|
529
|
+
return undefined;
|
|
530
|
+
}
|
|
531
|
+
const principalDigest = computePrincipalDigest(attestation.principalId);
|
|
532
|
+
const principalRecordId = attestation.principalLineage.principalRecordId;
|
|
533
|
+
const principalVersion = attestation.principalLineage.principalVersion;
|
|
534
|
+
let authorityId;
|
|
535
|
+
if (typeof delivery.listActiveOwnerRouteScopes === 'function') {
|
|
536
|
+
try {
|
|
537
|
+
const routes = delivery.listActiveOwnerRouteScopes();
|
|
538
|
+
const match = routes.find((route) => route.principalRecordId === principalRecordId &&
|
|
539
|
+
route.principalVersion === principalVersion &&
|
|
540
|
+
route.workspace === workspace &&
|
|
541
|
+
route.agentPreset === agentPreset);
|
|
542
|
+
authorityId = match?.authorityId;
|
|
543
|
+
}
|
|
544
|
+
catch {
|
|
545
|
+
authorityId = undefined;
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
return {
|
|
549
|
+
mode: 'delivery',
|
|
550
|
+
ownerScopeKey: computeOwnerScopeKey({
|
|
551
|
+
mode: 'delivery',
|
|
552
|
+
principalDigest,
|
|
553
|
+
principalRecordId,
|
|
554
|
+
principalVersion,
|
|
555
|
+
workspace,
|
|
556
|
+
agentPreset,
|
|
557
|
+
}),
|
|
558
|
+
principalId: attestation.principalId,
|
|
559
|
+
principalDigest,
|
|
560
|
+
principalRecordId,
|
|
561
|
+
principalVersion,
|
|
562
|
+
workspace,
|
|
563
|
+
agentPreset,
|
|
564
|
+
...(authorityId === undefined ? {} : { authorityId }),
|
|
565
|
+
};
|
|
566
|
+
}
|
|
567
|
+
if (!options.allowHeadlessScope) {
|
|
568
|
+
return undefined;
|
|
569
|
+
}
|
|
570
|
+
const principalId = options.headlessPrincipals?.get(agent) ??
|
|
571
|
+
options.defaultHeadlessPrincipal ??
|
|
572
|
+
`headless:${workspace}:${agentPreset}`;
|
|
573
|
+
const principalDigest = computePrincipalDigest(principalId);
|
|
574
|
+
return {
|
|
575
|
+
mode: 'headless',
|
|
576
|
+
ownerScopeKey: computeOwnerScopeKey({
|
|
577
|
+
mode: 'headless',
|
|
578
|
+
principalDigest,
|
|
579
|
+
principalRecordId: null,
|
|
580
|
+
principalVersion: null,
|
|
581
|
+
workspace,
|
|
582
|
+
agentPreset,
|
|
583
|
+
}),
|
|
584
|
+
principalId,
|
|
585
|
+
principalDigest,
|
|
586
|
+
principalRecordId: null,
|
|
587
|
+
principalVersion: null,
|
|
588
|
+
workspace,
|
|
589
|
+
agentPreset,
|
|
590
|
+
};
|
|
591
|
+
}
|
|
592
|
+
function latestTurnNumber(agent, state) {
|
|
593
|
+
const events = agent.session.snapshotEvents();
|
|
594
|
+
for (let i = events.length - 1; i >= 0; i -= 1) {
|
|
595
|
+
const ev = events[i];
|
|
596
|
+
if (ev.type === 'turn/start' || ev.type === 'turn/end') {
|
|
597
|
+
return ev.data.turn;
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
return state.claimedUserMessage?.turn ?? 0;
|
|
601
|
+
}
|
|
602
|
+
function readValidGoalContext(ctx, agent, ownerScope) {
|
|
603
|
+
const goals = ctx.get('assistantGoals', false);
|
|
604
|
+
if (goals === undefined || typeof goals.taskContext !== 'function')
|
|
605
|
+
return undefined;
|
|
606
|
+
let task;
|
|
607
|
+
try {
|
|
608
|
+
task = goals.taskContext(agent);
|
|
609
|
+
}
|
|
610
|
+
catch {
|
|
611
|
+
return undefined;
|
|
612
|
+
}
|
|
613
|
+
if (task === undefined || task.protocol !== 'goal-task-context/v1' || task.active !== true) {
|
|
614
|
+
return undefined;
|
|
615
|
+
}
|
|
616
|
+
if (task.scope.workspace !== ownerScope.workspace || task.scope.preset !== ownerScope.agentPreset) {
|
|
617
|
+
return undefined;
|
|
618
|
+
}
|
|
619
|
+
if (ownerScope.mode === 'delivery') {
|
|
620
|
+
if (task.scope.principalRecordId !== ownerScope.principalRecordId ||
|
|
621
|
+
task.scope.principalVersion !== ownerScope.principalVersion ||
|
|
622
|
+
computePrincipalDigest(task.scope.principalId) !== ownerScope.principalDigest) {
|
|
623
|
+
return undefined;
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
return task;
|
|
627
|
+
}
|
|
628
|
+
export function resolveTaskId(ctx, agent, state, ownerScope) {
|
|
629
|
+
const delivery = ctx.get('assistantDelivery', false);
|
|
630
|
+
const turnNumber = latestTurnNumber(agent, state);
|
|
631
|
+
const goalContext = readValidGoalContext(ctx, agent, ownerScope);
|
|
632
|
+
if (delivery !== undefined && typeof delivery.currentPreferenceTurn === 'function') {
|
|
633
|
+
try {
|
|
634
|
+
const turn = delivery.currentPreferenceTurn(agent);
|
|
635
|
+
if (turn !== undefined && turn.sessionId === String(agent.session.id)) {
|
|
636
|
+
return {
|
|
637
|
+
taskId: `foreground-turn:${turn.sourceInboxId}`,
|
|
638
|
+
taskKind: 'foreground-turn',
|
|
639
|
+
turnNumber: turn.turn,
|
|
640
|
+
goalContext,
|
|
641
|
+
};
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
catch {
|
|
645
|
+
// Fallback below
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
const sessionId = String(agent.session.id);
|
|
649
|
+
const derivedMessages = agent.session.deriveMessages();
|
|
650
|
+
const latestHuman = derivedMessages.findLast(isHumanOrTaskMessage);
|
|
651
|
+
const sourceKind = state.claimedUserMessage?.sourceKind ?? latestHuman?.source?.kind;
|
|
652
|
+
if (sourceKind === 'assistant-automations' ||
|
|
653
|
+
sourceKind === 'plugin:@dsh-ray/assistant-automations') {
|
|
654
|
+
return {
|
|
655
|
+
taskId: `automation-run:${sessionId}:turn:${turnNumber}`,
|
|
656
|
+
taskKind: 'automation-run',
|
|
657
|
+
turnNumber,
|
|
658
|
+
goalContext,
|
|
659
|
+
};
|
|
660
|
+
}
|
|
661
|
+
if (goalContext !== undefined) {
|
|
662
|
+
return {
|
|
663
|
+
taskId: `goal-step:${goalContext.goal.id}:turn:${turnNumber}`,
|
|
664
|
+
taskKind: 'goal-step',
|
|
665
|
+
turnNumber,
|
|
666
|
+
goalContext,
|
|
667
|
+
};
|
|
668
|
+
}
|
|
669
|
+
return {
|
|
670
|
+
taskId: `headless-session:${sessionId}:turn:${turnNumber}`,
|
|
671
|
+
taskKind: ownerScope.mode === 'delivery' ? 'foreground-turn' : 'headless-session',
|
|
672
|
+
turnNumber,
|
|
673
|
+
goalContext,
|
|
674
|
+
};
|
|
675
|
+
}
|
|
676
|
+
export function assembleStepCues(input) {
|
|
677
|
+
const { agent, state, goalContext, turnNumber, nowMs, shouldDecaySession, activeIndex } = input;
|
|
678
|
+
const cues = [];
|
|
679
|
+
ensureTurnPromptEntities(state, turnNumber);
|
|
680
|
+
// Cue 1: Latest user message (weight 1.0)
|
|
681
|
+
const derivedMessages = agent.session.deriveMessages();
|
|
682
|
+
const derivedLatest = derivedMessages.findLast(isHumanOrTaskMessage);
|
|
683
|
+
let userText = '';
|
|
684
|
+
if (state.claimedUserMessage !== undefined) {
|
|
685
|
+
if (derivedLatest !== undefined && String(derivedLatest.id) === state.claimedUserMessage.messageId) {
|
|
686
|
+
userText = extractTextBounded(derivedLatest.content, MAX_CUE_TEXT_CHARS);
|
|
687
|
+
}
|
|
688
|
+
else {
|
|
689
|
+
userText = state.claimedUserMessage.text;
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
else if (derivedLatest !== undefined) {
|
|
693
|
+
userText = extractTextBounded(derivedLatest.content, MAX_CUE_TEXT_CHARS);
|
|
694
|
+
}
|
|
695
|
+
if (userText.trim().length > 0) {
|
|
696
|
+
addPromptEntitiesFromText(state, userText, activeIndex);
|
|
697
|
+
cues.push({
|
|
698
|
+
kind: 'user',
|
|
699
|
+
text: userText,
|
|
700
|
+
weight: 1.0,
|
|
701
|
+
});
|
|
702
|
+
}
|
|
703
|
+
// Cue 2: Current goal / step (weight 0.6)
|
|
704
|
+
let goalKey = 'none';
|
|
705
|
+
if (goalContext !== undefined) {
|
|
706
|
+
const objective = goalContext.goal.objective.slice(0, MAX_CUE_TEXT_CHARS);
|
|
707
|
+
const nextStep = goalContext.checkpoint.nextStep.slice(0, MAX_CUE_TEXT_CHARS);
|
|
708
|
+
const goalText = [objective, nextStep].filter((s) => s.trim().length > 0).join('\n');
|
|
709
|
+
goalKey = `${goalContext.goal.id}:${goalContext.goal.definition.version}:${goalContext.goal.native.revision}:${nextStep.slice(0, 64)}`;
|
|
710
|
+
if (goalText.length > 0) {
|
|
711
|
+
addPromptEntitiesFromText(state, goalText, activeIndex);
|
|
712
|
+
cues.push({
|
|
713
|
+
kind: 'goal',
|
|
714
|
+
text: goalText,
|
|
715
|
+
weight: 0.6,
|
|
716
|
+
});
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
// Cue 3: Tool-result entities ONLY (weight 0.4, never raw prose!)
|
|
720
|
+
const entityIds = [];
|
|
721
|
+
for (const cue of state.toolEntities.values()) {
|
|
722
|
+
if (cue.turn === turnNumber) {
|
|
723
|
+
entityIds.push(cue.entityId);
|
|
724
|
+
const key = normalizeEntityKey(cue.entityId);
|
|
725
|
+
if (key !== null) {
|
|
726
|
+
state.turnPromptEntities.add(key);
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
if (entityIds.length > 0) {
|
|
731
|
+
cues.push({
|
|
732
|
+
kind: 'tool-entity',
|
|
733
|
+
text: '',
|
|
734
|
+
entities: entityIds,
|
|
735
|
+
weight: 0.4,
|
|
736
|
+
});
|
|
737
|
+
}
|
|
738
|
+
// Cue 4: Time-decaying session activation set (decayed once per unique step evaluation)
|
|
739
|
+
const halfLifeMs = Math.max(1, input.sessionHalfLifeMs ?? DEFAULT_SESSION_HALF_LIFE_MS);
|
|
740
|
+
const stepDecay = input.sessionStepDecay ?? DEFAULT_SESSION_STEP_DECAY;
|
|
741
|
+
const sessionActivations = [];
|
|
742
|
+
if (shouldDecaySession) {
|
|
743
|
+
for (const [nodeId, entry] of state.sessionActivation) {
|
|
744
|
+
const dt = Math.max(0, nowMs - entry.updatedAt);
|
|
745
|
+
const nextScore = entry.score * Math.exp((-Math.LN2 * dt) / halfLifeMs) * stepDecay;
|
|
746
|
+
if (nextScore < 0.01) {
|
|
747
|
+
state.sessionActivation.delete(nodeId);
|
|
748
|
+
continue;
|
|
749
|
+
}
|
|
750
|
+
entry.score = nextScore;
|
|
751
|
+
entry.updatedAt = nowMs;
|
|
752
|
+
sessionActivations.push({ nodeId, score: nextScore, updatedAt: nowMs });
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
else {
|
|
756
|
+
for (const [nodeId, entry] of state.sessionActivation) {
|
|
757
|
+
if (entry.score >= 0.01) {
|
|
758
|
+
sessionActivations.push({ nodeId, score: entry.score, updatedAt: entry.updatedAt });
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
return { cues, goalKey, sessionActivations };
|
|
763
|
+
}
|
|
764
|
+
export function foldStepSessionActivation(state, items, nowMs) {
|
|
765
|
+
for (const item of items) {
|
|
766
|
+
const clampedScore = Math.min(2.0, Math.max(0.05, item.score));
|
|
767
|
+
const existing = state.sessionActivation.get(item.id);
|
|
768
|
+
if (existing !== undefined) {
|
|
769
|
+
existing.score = Math.min(2.0, Math.max(existing.score, clampedScore));
|
|
770
|
+
existing.updatedAt = nowMs;
|
|
771
|
+
}
|
|
772
|
+
else {
|
|
773
|
+
state.sessionActivation.set(item.id, {
|
|
774
|
+
score: clampedScore,
|
|
775
|
+
updatedAt: nowMs,
|
|
776
|
+
});
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
while (state.sessionActivation.size > MAX_SESSION_ACTIVATION_ENTRIES) {
|
|
780
|
+
let minKey;
|
|
781
|
+
let minScore = Infinity;
|
|
782
|
+
for (const [k, v] of state.sessionActivation) {
|
|
783
|
+
if (v.score < minScore) {
|
|
784
|
+
minScore = v.score;
|
|
785
|
+
minKey = k;
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
if (minKey === undefined)
|
|
789
|
+
break;
|
|
790
|
+
state.sessionActivation.delete(minKey);
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
//# sourceMappingURL=cues.js.map
|