@dexto/core 1.16.3 → 1.16.5
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/context/manager.cjs +84 -60
- package/dist/context/manager.d.ts +38 -3
- package/dist/context/manager.d.ts.map +1 -1
- package/dist/context/manager.js +84 -60
- package/dist/llm/executor/stream-processor.cjs +7 -0
- package/dist/llm/executor/stream-processor.d.ts +1 -0
- package/dist/llm/executor/stream-processor.d.ts.map +1 -1
- package/dist/llm/executor/stream-processor.js +7 -0
- package/dist/llm/executor/turn-executor.cjs +34 -23
- package/dist/llm/executor/turn-executor.d.ts +2 -1
- package/dist/llm/executor/turn-executor.d.ts.map +1 -1
- package/dist/llm/executor/turn-executor.js +34 -23
- package/dist/storage/conversation/database.cjs +4 -0
- package/dist/storage/conversation/database.d.ts +3 -0
- package/dist/storage/conversation/database.d.ts.map +1 -1
- package/dist/storage/conversation/database.js +4 -0
- package/dist/storage/conversation/types.d.ts +8 -0
- package/dist/storage/conversation/types.d.ts.map +1 -1
- package/dist/storage/stores/in-memory.cjs +3 -0
- package/dist/storage/stores/in-memory.d.ts.map +1 -1
- package/dist/storage/stores/in-memory.js +3 -0
- package/dist/test-utils/model-history-count-cases.cjs +88 -0
- package/dist/test-utils/model-history-count-cases.js +64 -0
- package/package.json +2 -2
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stream-processor.d.ts","sourceRoot":"","sources":["../../../src/llm/executor/stream-processor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,OAAO,IAAI,aAAa,EAAE,MAAM,IAAI,CAAC;AAEhE,OAAO,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAC1D,OAAO,EAAE,eAAe,EAAmB,MAAM,uBAAuB,CAAC;AACzE,OAAO,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AAEnD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAC;AAKv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,CAAC,sBAAsB;YAwChB,eAAe;YA4Bf,sBAAsB;IAMpC;;;;OAIG;YACW,2BAA2B;IAQzC;;OAEG;YACW,wBAAwB;YAQxB,yBAAyB;CAuC1C"}
|
|
@@ -95,6 +95,13 @@ class StreamProcessor {
|
|
|
95
95
|
partialToolCalls = /* @__PURE__ */ new Map();
|
|
96
96
|
/** Runs one model attempt. `streamFn` must send its request with the given abort signal. */
|
|
97
97
|
async process(streamFn) {
|
|
98
|
+
try {
|
|
99
|
+
return await this.processStream(streamFn);
|
|
100
|
+
} finally {
|
|
101
|
+
this.contextManager.releaseAssistantMessage();
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
async processStream(streamFn) {
|
|
98
105
|
const startedAtMs = Date.now();
|
|
99
106
|
const setStreamAttribute = (name, value) => {
|
|
100
107
|
trace.getActiveSpan()?.setAttribute(`llm.stream.${name}`, value);
|
|
@@ -1035,10 +1035,13 @@ class TurnExecutor {
|
|
|
1035
1035
|
await this.injectQueuedMessages(coalesced);
|
|
1036
1036
|
}
|
|
1037
1037
|
}
|
|
1038
|
-
|
|
1039
|
-
this.reportRepeatedPrunedToolCalls(modelHistory);
|
|
1040
|
-
const { preparedHistory: visibleHistory } =
|
|
1041
|
-
await this.pruneOldToolOutputs(visibleHistory);
|
|
1038
|
+
let modelHistory = await this.contextManager.loadModelHistory();
|
|
1039
|
+
this.reportRepeatedPrunedToolCalls(modelHistory.messages);
|
|
1040
|
+
const { preparedHistory: visibleHistory } = this.contextManager.prepareLoadedModelHistory(modelHistory);
|
|
1041
|
+
const { prunedCount } = await this.pruneOldToolOutputs(visibleHistory);
|
|
1042
|
+
if (prunedCount > 0) {
|
|
1043
|
+
modelHistory = await this.contextManager.loadModelHistory();
|
|
1044
|
+
}
|
|
1042
1045
|
let systemPrompt = await (0, import_operation_span.recordOperationSpan)(
|
|
1043
1046
|
{
|
|
1044
1047
|
name: "system_prompt.build",
|
|
@@ -1061,7 +1064,7 @@ class TurnExecutor {
|
|
|
1061
1064
|
}
|
|
1062
1065
|
},
|
|
1063
1066
|
async () => {
|
|
1064
|
-
const preparedHistoryResult =
|
|
1067
|
+
const preparedHistoryResult = this.contextManager.prepareLoadedModelHistory(modelHistory);
|
|
1065
1068
|
const preparedHistory2 = preparedHistoryResult.preparedHistory;
|
|
1066
1069
|
const formattedMessages2 = await this.contextManager.getFormattedMessages(
|
|
1067
1070
|
input.contributorContext,
|
|
@@ -1100,7 +1103,8 @@ class TurnExecutor {
|
|
|
1100
1103
|
() => this.contextManager.getEstimatedNextInputTokens(
|
|
1101
1104
|
systemPrompt,
|
|
1102
1105
|
preparedHistory,
|
|
1103
|
-
toolDefinitions
|
|
1106
|
+
toolDefinitions,
|
|
1107
|
+
modelHistory
|
|
1104
1108
|
),
|
|
1105
1109
|
this.logger
|
|
1106
1110
|
);
|
|
@@ -1109,7 +1113,7 @@ class TurnExecutor {
|
|
|
1109
1113
|
this.logger.debug(
|
|
1110
1114
|
`Pre-check: estimated ${estimatedInputTokens} tokens exceeds threshold, compacting`
|
|
1111
1115
|
);
|
|
1112
|
-
const
|
|
1116
|
+
const compactedHistory = await (0, import_operation_span.recordOperationSpan)(
|
|
1113
1117
|
{
|
|
1114
1118
|
name: "context.compact",
|
|
1115
1119
|
componentName: "TurnExecutor",
|
|
@@ -1118,12 +1122,14 @@ class TurnExecutor {
|
|
|
1118
1122
|
() => this.compactContext(
|
|
1119
1123
|
estimatedInputTokens,
|
|
1120
1124
|
input.contributorContext,
|
|
1121
|
-
toolDefinitions
|
|
1125
|
+
toolDefinitions,
|
|
1126
|
+
modelHistory
|
|
1122
1127
|
),
|
|
1123
1128
|
this.logger
|
|
1124
1129
|
);
|
|
1125
|
-
if (
|
|
1130
|
+
if (compactedHistory) {
|
|
1126
1131
|
compacted = true;
|
|
1132
|
+
modelHistory = compactedHistory;
|
|
1127
1133
|
systemPrompt = await (0, import_operation_span.recordOperationSpan)(
|
|
1128
1134
|
{
|
|
1129
1135
|
name: "system_prompt.build",
|
|
@@ -1148,7 +1154,7 @@ class TurnExecutor {
|
|
|
1148
1154
|
}
|
|
1149
1155
|
},
|
|
1150
1156
|
async () => {
|
|
1151
|
-
const preparedHistoryResult =
|
|
1157
|
+
const preparedHistoryResult = this.contextManager.prepareLoadedModelHistory(modelHistory);
|
|
1152
1158
|
const preparedHistory2 = preparedHistoryResult.preparedHistory;
|
|
1153
1159
|
const formattedMessages2 = await this.contextManager.getFormattedMessages(
|
|
1154
1160
|
input.contributorContext,
|
|
@@ -1179,7 +1185,8 @@ class TurnExecutor {
|
|
|
1179
1185
|
() => this.contextManager.getEstimatedNextInputTokens(
|
|
1180
1186
|
systemPrompt,
|
|
1181
1187
|
preparedHistory,
|
|
1182
|
-
toolDefinitions
|
|
1188
|
+
toolDefinitions,
|
|
1189
|
+
modelHistory
|
|
1183
1190
|
),
|
|
1184
1191
|
this.logger
|
|
1185
1192
|
);
|
|
@@ -1344,12 +1351,12 @@ class TurnExecutor {
|
|
|
1344
1351
|
let failedAttempts = 0;
|
|
1345
1352
|
let emptyResponseRetries = 0;
|
|
1346
1353
|
for (; ; ) {
|
|
1347
|
-
const historyLengthBefore =
|
|
1354
|
+
const historyLengthBefore = await this.contextManager.countModelMessages();
|
|
1348
1355
|
let result;
|
|
1349
1356
|
try {
|
|
1350
1357
|
result = await this.runModelStep(request);
|
|
1351
1358
|
} catch (error) {
|
|
1352
|
-
const historyLengthAfter2 =
|
|
1359
|
+
const historyLengthAfter2 = await this.contextManager.countModelMessages();
|
|
1353
1360
|
const historyLengthChanged = historyLengthAfter2 !== historyLengthBefore;
|
|
1354
1361
|
if (!this.canRetryModelRequest(error, historyLengthChanged) || failedAttempts >= MODEL_REQUEST_MAX_RETRIES) {
|
|
1355
1362
|
throw error;
|
|
@@ -1375,7 +1382,7 @@ class TurnExecutor {
|
|
|
1375
1382
|
if (!(0, import_empty_response.isEmptyModelResponse)(result)) {
|
|
1376
1383
|
return result;
|
|
1377
1384
|
}
|
|
1378
|
-
const historyLengthAfter =
|
|
1385
|
+
const historyLengthAfter = await this.contextManager.countModelMessages();
|
|
1379
1386
|
if (this.stepAbortController.signal.aborted) {
|
|
1380
1387
|
return this.cancelHeldEmptyResponse(result);
|
|
1381
1388
|
}
|
|
@@ -1454,7 +1461,8 @@ class TurnExecutor {
|
|
|
1454
1461
|
await this.compactContext(
|
|
1455
1462
|
contextInputTokens,
|
|
1456
1463
|
contributorContext,
|
|
1457
|
-
request.toolDefinitions
|
|
1464
|
+
request.toolDefinitions,
|
|
1465
|
+
await this.contextManager.loadModelHistory()
|
|
1458
1466
|
);
|
|
1459
1467
|
}
|
|
1460
1468
|
}
|
|
@@ -2045,22 +2053,23 @@ class TurnExecutor {
|
|
|
2045
2053
|
* @param originalTokens The estimated input token count that triggered overflow
|
|
2046
2054
|
* @param contributorContext Context for system prompt contributors (needed for accurate token estimation)
|
|
2047
2055
|
* @param tools Tool definitions (needed for accurate token estimation)
|
|
2048
|
-
* @
|
|
2056
|
+
* @param modelHistory The model history as just loaded; compaction summarizes this load
|
|
2057
|
+
* @returns the model history as loaded after compaction, or null if compaction was skipped
|
|
2049
2058
|
*/
|
|
2050
|
-
async compactContext(originalTokens, contributorContext, tools) {
|
|
2059
|
+
async compactContext(originalTokens, contributorContext, tools, modelHistory) {
|
|
2051
2060
|
if (!this.compactionStrategy) {
|
|
2052
|
-
return
|
|
2061
|
+
return null;
|
|
2053
2062
|
}
|
|
2054
2063
|
this.logger.info(
|
|
2055
2064
|
`Context overflow detected (${originalTokens} tokens), checking if compression is possible`
|
|
2056
2065
|
);
|
|
2057
|
-
const history =
|
|
2066
|
+
const history = modelHistory.messages;
|
|
2058
2067
|
const { filterCompacted } = await import("../../context/utils.js");
|
|
2059
2068
|
const originalFiltered = filterCompacted(history);
|
|
2060
2069
|
const originalMessages = originalFiltered.length;
|
|
2061
2070
|
if (history.length < 4) {
|
|
2062
2071
|
this.logger.debug("Compaction skipped: history too short to summarize");
|
|
2063
|
-
return
|
|
2072
|
+
return null;
|
|
2064
2073
|
}
|
|
2065
2074
|
this.eventBus.emit("context:compacting", {
|
|
2066
2075
|
estimatedTokens: originalTokens
|
|
@@ -2084,15 +2093,17 @@ class TurnExecutor {
|
|
|
2084
2093
|
strategy: this.compactionStrategy.name,
|
|
2085
2094
|
reason: "overflow"
|
|
2086
2095
|
});
|
|
2087
|
-
return
|
|
2096
|
+
return null;
|
|
2088
2097
|
}
|
|
2089
2098
|
for (const summary of summaryMessages) {
|
|
2090
2099
|
await this.contextManager.addMessage(summary);
|
|
2091
2100
|
}
|
|
2092
2101
|
this.contextManager.resetActualTokenTracking();
|
|
2102
|
+
const compactedHistory = await this.contextManager.loadModelHistory();
|
|
2093
2103
|
const afterEstimate = await this.contextManager.getContextTokenEstimate(
|
|
2094
2104
|
contributorContext,
|
|
2095
|
-
tools
|
|
2105
|
+
tools,
|
|
2106
|
+
compactedHistory
|
|
2096
2107
|
);
|
|
2097
2108
|
const compactedTokens = afterEstimate.estimated;
|
|
2098
2109
|
const compactedMessages = afterEstimate.stats.filteredMessageCount;
|
|
@@ -2107,7 +2118,7 @@ class TurnExecutor {
|
|
|
2107
2118
|
this.logger.info(
|
|
2108
2119
|
`Compaction complete: ${originalTokens} \u2192 ~${compactedTokens} tokens (${originalMessages} \u2192 ${compactedMessages} messages after filtering)`
|
|
2109
2120
|
);
|
|
2110
|
-
return
|
|
2121
|
+
return compactedHistory;
|
|
2111
2122
|
}
|
|
2112
2123
|
/**
|
|
2113
2124
|
* Set telemetry span attributes for token usage.
|
|
@@ -448,7 +448,8 @@ export declare class TurnExecutor {
|
|
|
448
448
|
* @param originalTokens The estimated input token count that triggered overflow
|
|
449
449
|
* @param contributorContext Context for system prompt contributors (needed for accurate token estimation)
|
|
450
450
|
* @param tools Tool definitions (needed for accurate token estimation)
|
|
451
|
-
* @
|
|
451
|
+
* @param modelHistory The model history as just loaded; compaction summarizes this load
|
|
452
|
+
* @returns the model history as loaded after compaction, or null if compaction was skipped
|
|
452
453
|
*/
|
|
453
454
|
private compactContext;
|
|
454
455
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"turn-executor.d.ts","sourceRoot":"","sources":["../../../src/llm/executor/turn-executor.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,aAAa,EAMb,KAAK,YAAY,EAEpB,MAAM,IAAI,CAAC;AACZ,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EAAa,uBAAuB,EAAE,MAAM,kBAAkB,CAAC;AAE3E,OAAO,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAY1D,OAAO,EACH,WAAW,EAId,MAAM,6BAA6B,CAAC;AACrC,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;
|
|
1
|
+
{"version":3,"file":"turn-executor.d.ts","sourceRoot":"","sources":["../../../src/llm/executor/turn-executor.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,aAAa,EAMb,KAAK,YAAY,EAEpB,MAAM,IAAI,CAAC;AACZ,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EAAa,uBAAuB,EAAE,MAAM,kBAAkB,CAAC;AAE3E,OAAO,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAY1D,OAAO,EACH,WAAW,EAId,MAAM,6BAA6B,CAAC;AACrC,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAcpD,OAAO,KAAK,EAAE,cAAc,EAAiB,qBAAqB,EAAE,MAAM,YAAY,CAAC;AAEvF,OAAO,KAAK,EACR,UAAU,EACV,kBAAkB,EAClB,UAAU,EAEV,aAAa,EAEhB,MAAM,YAAY,CAAC;AACpB,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAC;AAEvD,OAAO,KAAK,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAC9E,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;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AAmB;IA2DjC;;;;;;OAMG;IACH,OAAO,CAAC,kBAAkB;IAgB1B;;;OAGG;IACH,OAAO,CAAC,OAAO;IASf;;;;;;OAMG;IACH,OAAO,CAAC,aAAa;IAOrB;;;;;;OAMG;IACH,OAAO,CAAC,uBAAuB;IAO/B;;;;;;;;;;;;OAYG;YACW,cAAc;IA2F5B;;OAEG;IACH,OAAO,CAAC,sBAAsB;IAoB9B,OAAO,CAAC,qBAAqB;IAK7B;;OAEG;IACH,OAAO,CAAC,gBAAgB;CAQ3B"}
|
|
@@ -1021,10 +1021,13 @@ class TurnExecutor {
|
|
|
1021
1021
|
await this.injectQueuedMessages(coalesced);
|
|
1022
1022
|
}
|
|
1023
1023
|
}
|
|
1024
|
-
|
|
1025
|
-
this.reportRepeatedPrunedToolCalls(modelHistory);
|
|
1026
|
-
const { preparedHistory: visibleHistory } =
|
|
1027
|
-
await this.pruneOldToolOutputs(visibleHistory);
|
|
1024
|
+
let modelHistory = await this.contextManager.loadModelHistory();
|
|
1025
|
+
this.reportRepeatedPrunedToolCalls(modelHistory.messages);
|
|
1026
|
+
const { preparedHistory: visibleHistory } = this.contextManager.prepareLoadedModelHistory(modelHistory);
|
|
1027
|
+
const { prunedCount } = await this.pruneOldToolOutputs(visibleHistory);
|
|
1028
|
+
if (prunedCount > 0) {
|
|
1029
|
+
modelHistory = await this.contextManager.loadModelHistory();
|
|
1030
|
+
}
|
|
1028
1031
|
let systemPrompt = await recordOperationSpan(
|
|
1029
1032
|
{
|
|
1030
1033
|
name: "system_prompt.build",
|
|
@@ -1047,7 +1050,7 @@ class TurnExecutor {
|
|
|
1047
1050
|
}
|
|
1048
1051
|
},
|
|
1049
1052
|
async () => {
|
|
1050
|
-
const preparedHistoryResult =
|
|
1053
|
+
const preparedHistoryResult = this.contextManager.prepareLoadedModelHistory(modelHistory);
|
|
1051
1054
|
const preparedHistory2 = preparedHistoryResult.preparedHistory;
|
|
1052
1055
|
const formattedMessages2 = await this.contextManager.getFormattedMessages(
|
|
1053
1056
|
input.contributorContext,
|
|
@@ -1086,7 +1089,8 @@ class TurnExecutor {
|
|
|
1086
1089
|
() => this.contextManager.getEstimatedNextInputTokens(
|
|
1087
1090
|
systemPrompt,
|
|
1088
1091
|
preparedHistory,
|
|
1089
|
-
toolDefinitions
|
|
1092
|
+
toolDefinitions,
|
|
1093
|
+
modelHistory
|
|
1090
1094
|
),
|
|
1091
1095
|
this.logger
|
|
1092
1096
|
);
|
|
@@ -1095,7 +1099,7 @@ class TurnExecutor {
|
|
|
1095
1099
|
this.logger.debug(
|
|
1096
1100
|
`Pre-check: estimated ${estimatedInputTokens} tokens exceeds threshold, compacting`
|
|
1097
1101
|
);
|
|
1098
|
-
const
|
|
1102
|
+
const compactedHistory = await recordOperationSpan(
|
|
1099
1103
|
{
|
|
1100
1104
|
name: "context.compact",
|
|
1101
1105
|
componentName: "TurnExecutor",
|
|
@@ -1104,12 +1108,14 @@ class TurnExecutor {
|
|
|
1104
1108
|
() => this.compactContext(
|
|
1105
1109
|
estimatedInputTokens,
|
|
1106
1110
|
input.contributorContext,
|
|
1107
|
-
toolDefinitions
|
|
1111
|
+
toolDefinitions,
|
|
1112
|
+
modelHistory
|
|
1108
1113
|
),
|
|
1109
1114
|
this.logger
|
|
1110
1115
|
);
|
|
1111
|
-
if (
|
|
1116
|
+
if (compactedHistory) {
|
|
1112
1117
|
compacted = true;
|
|
1118
|
+
modelHistory = compactedHistory;
|
|
1113
1119
|
systemPrompt = await recordOperationSpan(
|
|
1114
1120
|
{
|
|
1115
1121
|
name: "system_prompt.build",
|
|
@@ -1134,7 +1140,7 @@ class TurnExecutor {
|
|
|
1134
1140
|
}
|
|
1135
1141
|
},
|
|
1136
1142
|
async () => {
|
|
1137
|
-
const preparedHistoryResult =
|
|
1143
|
+
const preparedHistoryResult = this.contextManager.prepareLoadedModelHistory(modelHistory);
|
|
1138
1144
|
const preparedHistory2 = preparedHistoryResult.preparedHistory;
|
|
1139
1145
|
const formattedMessages2 = await this.contextManager.getFormattedMessages(
|
|
1140
1146
|
input.contributorContext,
|
|
@@ -1165,7 +1171,8 @@ class TurnExecutor {
|
|
|
1165
1171
|
() => this.contextManager.getEstimatedNextInputTokens(
|
|
1166
1172
|
systemPrompt,
|
|
1167
1173
|
preparedHistory,
|
|
1168
|
-
toolDefinitions
|
|
1174
|
+
toolDefinitions,
|
|
1175
|
+
modelHistory
|
|
1169
1176
|
),
|
|
1170
1177
|
this.logger
|
|
1171
1178
|
);
|
|
@@ -1330,12 +1337,12 @@ class TurnExecutor {
|
|
|
1330
1337
|
let failedAttempts = 0;
|
|
1331
1338
|
let emptyResponseRetries = 0;
|
|
1332
1339
|
for (; ; ) {
|
|
1333
|
-
const historyLengthBefore =
|
|
1340
|
+
const historyLengthBefore = await this.contextManager.countModelMessages();
|
|
1334
1341
|
let result;
|
|
1335
1342
|
try {
|
|
1336
1343
|
result = await this.runModelStep(request);
|
|
1337
1344
|
} catch (error) {
|
|
1338
|
-
const historyLengthAfter2 =
|
|
1345
|
+
const historyLengthAfter2 = await this.contextManager.countModelMessages();
|
|
1339
1346
|
const historyLengthChanged = historyLengthAfter2 !== historyLengthBefore;
|
|
1340
1347
|
if (!this.canRetryModelRequest(error, historyLengthChanged) || failedAttempts >= MODEL_REQUEST_MAX_RETRIES) {
|
|
1341
1348
|
throw error;
|
|
@@ -1361,7 +1368,7 @@ class TurnExecutor {
|
|
|
1361
1368
|
if (!isEmptyModelResponse(result)) {
|
|
1362
1369
|
return result;
|
|
1363
1370
|
}
|
|
1364
|
-
const historyLengthAfter =
|
|
1371
|
+
const historyLengthAfter = await this.contextManager.countModelMessages();
|
|
1365
1372
|
if (this.stepAbortController.signal.aborted) {
|
|
1366
1373
|
return this.cancelHeldEmptyResponse(result);
|
|
1367
1374
|
}
|
|
@@ -1440,7 +1447,8 @@ class TurnExecutor {
|
|
|
1440
1447
|
await this.compactContext(
|
|
1441
1448
|
contextInputTokens,
|
|
1442
1449
|
contributorContext,
|
|
1443
|
-
request.toolDefinitions
|
|
1450
|
+
request.toolDefinitions,
|
|
1451
|
+
await this.contextManager.loadModelHistory()
|
|
1444
1452
|
);
|
|
1445
1453
|
}
|
|
1446
1454
|
}
|
|
@@ -2031,22 +2039,23 @@ class TurnExecutor {
|
|
|
2031
2039
|
* @param originalTokens The estimated input token count that triggered overflow
|
|
2032
2040
|
* @param contributorContext Context for system prompt contributors (needed for accurate token estimation)
|
|
2033
2041
|
* @param tools Tool definitions (needed for accurate token estimation)
|
|
2034
|
-
* @
|
|
2042
|
+
* @param modelHistory The model history as just loaded; compaction summarizes this load
|
|
2043
|
+
* @returns the model history as loaded after compaction, or null if compaction was skipped
|
|
2035
2044
|
*/
|
|
2036
|
-
async compactContext(originalTokens, contributorContext, tools) {
|
|
2045
|
+
async compactContext(originalTokens, contributorContext, tools, modelHistory) {
|
|
2037
2046
|
if (!this.compactionStrategy) {
|
|
2038
|
-
return
|
|
2047
|
+
return null;
|
|
2039
2048
|
}
|
|
2040
2049
|
this.logger.info(
|
|
2041
2050
|
`Context overflow detected (${originalTokens} tokens), checking if compression is possible`
|
|
2042
2051
|
);
|
|
2043
|
-
const history =
|
|
2052
|
+
const history = modelHistory.messages;
|
|
2044
2053
|
const { filterCompacted } = await import("../../context/utils.js");
|
|
2045
2054
|
const originalFiltered = filterCompacted(history);
|
|
2046
2055
|
const originalMessages = originalFiltered.length;
|
|
2047
2056
|
if (history.length < 4) {
|
|
2048
2057
|
this.logger.debug("Compaction skipped: history too short to summarize");
|
|
2049
|
-
return
|
|
2058
|
+
return null;
|
|
2050
2059
|
}
|
|
2051
2060
|
this.eventBus.emit("context:compacting", {
|
|
2052
2061
|
estimatedTokens: originalTokens
|
|
@@ -2070,15 +2079,17 @@ class TurnExecutor {
|
|
|
2070
2079
|
strategy: this.compactionStrategy.name,
|
|
2071
2080
|
reason: "overflow"
|
|
2072
2081
|
});
|
|
2073
|
-
return
|
|
2082
|
+
return null;
|
|
2074
2083
|
}
|
|
2075
2084
|
for (const summary of summaryMessages) {
|
|
2076
2085
|
await this.contextManager.addMessage(summary);
|
|
2077
2086
|
}
|
|
2078
2087
|
this.contextManager.resetActualTokenTracking();
|
|
2088
|
+
const compactedHistory = await this.contextManager.loadModelHistory();
|
|
2079
2089
|
const afterEstimate = await this.contextManager.getContextTokenEstimate(
|
|
2080
2090
|
contributorContext,
|
|
2081
|
-
tools
|
|
2091
|
+
tools,
|
|
2092
|
+
compactedHistory
|
|
2082
2093
|
);
|
|
2083
2094
|
const compactedTokens = afterEstimate.estimated;
|
|
2084
2095
|
const compactedMessages = afterEstimate.stats.filteredMessageCount;
|
|
@@ -2093,7 +2104,7 @@ class TurnExecutor {
|
|
|
2093
2104
|
this.logger.info(
|
|
2094
2105
|
`Compaction complete: ${originalTokens} \u2192 ~${compactedTokens} tokens (${originalMessages} \u2192 ${compactedMessages} messages after filtering)`
|
|
2095
2106
|
);
|
|
2096
|
-
return
|
|
2107
|
+
return compactedHistory;
|
|
2097
2108
|
}
|
|
2098
2109
|
/**
|
|
2099
2110
|
* Set telemetry span attributes for token usage.
|
|
@@ -52,6 +52,10 @@ class DatabaseConversationStore {
|
|
|
52
52
|
}
|
|
53
53
|
};
|
|
54
54
|
}
|
|
55
|
+
async countModelMessages(input) {
|
|
56
|
+
const state = await this.loadState(input.sessionId);
|
|
57
|
+
return (0, import_utils.filterCompacted)(state.cache ?? []).length;
|
|
58
|
+
}
|
|
55
59
|
async saveMessage(input) {
|
|
56
60
|
const state = await this.loadState(input.sessionId);
|
|
57
61
|
const cache = state.cache ?? [];
|
|
@@ -15,6 +15,9 @@ export declare class DatabaseConversationStore implements ConversationStore {
|
|
|
15
15
|
loadModelHistory(input: {
|
|
16
16
|
sessionId: string;
|
|
17
17
|
}): Promise<ModelHistoryLoad>;
|
|
18
|
+
countModelMessages(input: {
|
|
19
|
+
sessionId: string;
|
|
20
|
+
}): Promise<number>;
|
|
18
21
|
saveMessage(input: {
|
|
19
22
|
sessionId: string;
|
|
20
23
|
message: InternalMessage;
|
|
@@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AAG,OAAO,CAAC,MAAM,CAAC;IAKjE,WAAW,CAAC,KAAK,EAAE;QAAE,SAAS,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,eAAe,CAAA;KAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IA0BlF,aAAa,CAAC,KAAK,EAAE;QAAE,SAAS,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,eAAe,CAAA;KAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAyBpF,aAAa,CAAC,KAAK,EAAE;QAAE,SAAS,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAmB1D,KAAK,CAAC,KAAK,EAAE;QAAE,SAAS,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,IAAI,CAAC;YAoB1C,SAAS;IA4BvB,OAAO,CAAC,QAAQ;IAgBhB,OAAO,CAAC,cAAc;YA6BR,UAAU;IAuBxB,OAAO,CAAC,aAAa;IAgBrB,OAAO,CAAC,kBAAkB;IAS1B,OAAO,CAAC,cAAc;IAItB,OAAO,CAAC,YAAY;CAGvB"}
|
|
@@ -30,6 +30,10 @@ class DatabaseConversationStore {
|
|
|
30
30
|
}
|
|
31
31
|
};
|
|
32
32
|
}
|
|
33
|
+
async countModelMessages(input) {
|
|
34
|
+
const state = await this.loadState(input.sessionId);
|
|
35
|
+
return filterCompacted(state.cache ?? []).length;
|
|
36
|
+
}
|
|
33
37
|
async saveMessage(input) {
|
|
34
38
|
const state = await this.loadState(input.sessionId);
|
|
35
39
|
const cache = state.cache ?? [];
|
|
@@ -15,6 +15,14 @@ export interface ConversationStore {
|
|
|
15
15
|
loadModelHistory(input: {
|
|
16
16
|
sessionId: string;
|
|
17
17
|
}): Promise<ModelHistoryLoad>;
|
|
18
|
+
/**
|
|
19
|
+
* Number of messages `loadModelHistory` would return, without loading them. Optional: when a
|
|
20
|
+
* store omits it, core derives the count from `loadModelHistory`. Implement it when the store
|
|
21
|
+
* can count without reading or parsing the messages.
|
|
22
|
+
*/
|
|
23
|
+
countModelMessages?(input: {
|
|
24
|
+
sessionId: string;
|
|
25
|
+
}): Promise<number>;
|
|
18
26
|
saveMessage(input: {
|
|
19
27
|
sessionId: string;
|
|
20
28
|
message: InternalMessage;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/storage/conversation/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAE9D,MAAM,MAAM,qBAAqB,GAAG;IAChC,gBAAgB,EAAE,MAAM,CAAC;IACzB,yBAAyB,EAAE,MAAM,CAAC;IAClC,gBAAgB,EAAE,MAAM,GAAG,IAAI,CAAC;CACnC,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG;IAC3B,QAAQ,EAAE,eAAe,EAAE,CAAC;IAC5B,KAAK,EAAE,qBAAqB,CAAC;CAChC,CAAC;AAEF,MAAM,WAAW,iBAAiB;IAC9B,YAAY,CAAC,KAAK,EAAE;QAAE,SAAS,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,eAAe,EAAE,CAAC,CAAC;IACvE,gBAAgB,CAAC,KAAK,EAAE;QAAE,SAAS,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAAC;IAC1E,WAAW,CAAC,KAAK,EAAE;QAAE,SAAS,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,eAAe,CAAA;KAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACnF,aAAa,CAAC,KAAK,EAAE;QAAE,SAAS,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,eAAe,CAAA;KAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACrF,aAAa,CAAC,KAAK,EAAE;QAAE,SAAS,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC3D,KAAK,CAAC,KAAK,EAAE;QAA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KAAK,CAAC,KAAK,EAAE;QAAE,SAAS,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACtD"}
|
|
@@ -55,6 +55,9 @@ class InMemoryConversationStore {
|
|
|
55
55
|
}
|
|
56
56
|
};
|
|
57
57
|
}
|
|
58
|
+
async countModelMessages(input) {
|
|
59
|
+
return (0, import_utils.filterCompacted)(this.messages.get(input.sessionId) ?? []).length;
|
|
60
|
+
}
|
|
58
61
|
async saveMessage(input) {
|
|
59
62
|
const messages = this.messages.get(input.sessionId) ?? [];
|
|
60
63
|
if (input.message.id && messages.some((message) => message.id === input.message.id)) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"in-memory.d.ts","sourceRoot":"","sources":["../../../src/storage/stores/in-memory.ts"],"names":[],"mappings":"AAuCA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAKhE,OAAO,KAAK,EAAE,aAAa,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"in-memory.d.ts","sourceRoot":"","sources":["../../../src/storage/stores/in-memory.ts"],"names":[],"mappings":"AAuCA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAKhE,OAAO,KAAK,EAAE,aAAa,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAsf7E,qBAAa,uBAAwB,YAAW,eAAe;IAC3D,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA0C;IAE5D,IAAI,CAAC,KAAK,EAAE;QAAE,SAAS,EAAE,MAAM,CAAC;QAAC,UAAU,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAMnF,IAAI,CAAC,KAAK,EAAE;QAAE,SAAS,EAAE,MAAM,CAAC;QAAC,UAAU,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC;IAInF,aAAa,CAAC,KAAK,EAAE;QAAE,SAAS,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,IAAI,CAAC;CAGnE;AA8HD,qBAAa,mBAAoB,YAAW,WAAW;IACnD,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAerB;IAEF,QAAQ,CAAC,CAAC,SAAS,cAAc,EAAE,IAAI,EAAE,CAAC,GAAG,aAAa,CAAC,CAAC,CAAC;IAIvD,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAIxB,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAIjC,WAAW,IAAI,OAAO;IAItB,YAAY,IAAI,MAAM;CAGzB"}
|
|
@@ -32,6 +32,9 @@ class InMemoryConversationStore {
|
|
|
32
32
|
}
|
|
33
33
|
};
|
|
34
34
|
}
|
|
35
|
+
async countModelMessages(input) {
|
|
36
|
+
return filterCompacted(this.messages.get(input.sessionId) ?? []).length;
|
|
37
|
+
}
|
|
35
38
|
async saveMessage(input) {
|
|
36
39
|
const messages = this.messages.get(input.sessionId) ?? [];
|
|
37
40
|
if (input.message.id && messages.some((message) => message.id === input.message.id)) {
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var model_history_count_cases_exports = {};
|
|
20
|
+
__export(model_history_count_cases_exports, {
|
|
21
|
+
MODEL_HISTORY_COUNT_CASES: () => MODEL_HISTORY_COUNT_CASES,
|
|
22
|
+
buildModelHistoryCountMessages: () => buildModelHistoryCountMessages
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(model_history_count_cases_exports);
|
|
25
|
+
const MODEL_HISTORY_COUNT_CASES = [
|
|
26
|
+
{ name: "empty", history: "" },
|
|
27
|
+
{ name: "one message", history: "u" },
|
|
28
|
+
{ name: "plain", history: "u a u a u a" },
|
|
29
|
+
{ name: "plain with pruned rows", history: "u a t t u a t u a" },
|
|
30
|
+
{ name: "legacy summary", history: "u a u a S u a" },
|
|
31
|
+
{ name: "summary with preserved messages", history: "u a u a u a S:2 u a" },
|
|
32
|
+
{ name: "summary preserving everything", history: "u a u a S:0 u" },
|
|
33
|
+
{ name: "summary preserving nothing", history: "u a u a S:4 u" },
|
|
34
|
+
{ name: "original count past the summary", history: "u a u a S:99 u" },
|
|
35
|
+
{ name: "negative original count", history: "u a u a S:-3 u" },
|
|
36
|
+
{ name: "summary with messages after it", history: "u a u a S:1 u a u a" },
|
|
37
|
+
{ name: "summary as the last message", history: "u a u a u S:3" },
|
|
38
|
+
{ name: "two summaries", history: "u a S:1 u a u a S:3 u" },
|
|
39
|
+
{ name: "two summaries with pruned rows", history: "u a t t S:1 u t a S:4 t u" },
|
|
40
|
+
{ name: "session summary with preserved messages", history: "u a u a u SS:2 u a" },
|
|
41
|
+
{ name: "session summary with a clamped count", history: "u a u a SS:50 u" }
|
|
42
|
+
];
|
|
43
|
+
function buildModelHistoryCountMessages(history) {
|
|
44
|
+
return history.split(" ").filter((token) => token !== "").map((token, index) => {
|
|
45
|
+
const id = `message-${index}`;
|
|
46
|
+
const timestamp = index + 1;
|
|
47
|
+
if (token === "u") {
|
|
48
|
+
return { id, role: "user", content: [{ type: "text", text: token }], timestamp };
|
|
49
|
+
}
|
|
50
|
+
if (token === "a") {
|
|
51
|
+
return {
|
|
52
|
+
id,
|
|
53
|
+
role: "assistant",
|
|
54
|
+
assistantOutput: { status: "complete" },
|
|
55
|
+
content: [{ type: "text", text: token }],
|
|
56
|
+
timestamp
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
if (token === "t") {
|
|
60
|
+
return {
|
|
61
|
+
id,
|
|
62
|
+
role: "tool",
|
|
63
|
+
name: "read",
|
|
64
|
+
toolCallId: `call-${index}`,
|
|
65
|
+
content: [{ type: "text", text: "old result" }],
|
|
66
|
+
compactedAt: 1,
|
|
67
|
+
timestamp
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
const [kind, count] = token.split(":");
|
|
71
|
+
return {
|
|
72
|
+
id,
|
|
73
|
+
role: "assistant",
|
|
74
|
+
assistantOutput: { status: "complete" },
|
|
75
|
+
content: [{ type: "text", text: "summary" }],
|
|
76
|
+
metadata: {
|
|
77
|
+
...kind === "SS" ? { isSessionSummary: true } : { isSummary: true },
|
|
78
|
+
...count === void 0 ? {} : { originalMessageCount: Number(count) }
|
|
79
|
+
},
|
|
80
|
+
timestamp
|
|
81
|
+
};
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
85
|
+
0 && (module.exports = {
|
|
86
|
+
MODEL_HISTORY_COUNT_CASES,
|
|
87
|
+
buildModelHistoryCountMessages
|
|
88
|
+
});
|