@mastra/memory 1.10.1-alpha.3 → 1.11.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/CHANGELOG.md +66 -0
- package/dist/{chunk-SKRONGCV.js → chunk-D4D6ZFBQ.js} +355 -105
- package/dist/chunk-D4D6ZFBQ.js.map +1 -0
- package/dist/{chunk-DTAZSLVU.cjs → chunk-VINRPDYQ.cjs} +355 -105
- package/dist/chunk-VINRPDYQ.cjs.map +1 -0
- package/dist/docs/SKILL.md +1 -1
- package/dist/docs/assets/SOURCE_MAP.json +47 -47
- package/dist/docs/references/docs-memory-observational-memory.md +49 -4
- package/dist/docs/references/reference-memory-observational-memory.md +32 -6
- package/dist/index.cjs +577 -36
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +46 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +570 -29
- package/dist/index.js.map +1 -1
- package/dist/{observational-memory-XIZTJN3S.cjs → observational-memory-FATH657E.cjs} +26 -26
- package/dist/{observational-memory-XIZTJN3S.cjs.map → observational-memory-FATH657E.cjs.map} +1 -1
- package/dist/{observational-memory-UGDENJPE.js → observational-memory-SN7GKMHZ.js} +3 -3
- package/dist/{observational-memory-UGDENJPE.js.map → observational-memory-SN7GKMHZ.js.map} +1 -1
- package/dist/processors/index.cjs +24 -24
- package/dist/processors/index.js +1 -1
- package/dist/processors/observational-memory/observation-strategies/async-buffer.d.ts.map +1 -1
- package/dist/processors/observational-memory/observation-strategies/base.d.ts +9 -0
- package/dist/processors/observational-memory/observation-strategies/base.d.ts.map +1 -1
- package/dist/processors/observational-memory/observation-strategies/resource-scoped.d.ts.map +1 -1
- package/dist/processors/observational-memory/observation-strategies/sync.d.ts.map +1 -1
- package/dist/processors/observational-memory/observation-strategies/types.d.ts +2 -0
- package/dist/processors/observational-memory/observation-strategies/types.d.ts.map +1 -1
- package/dist/processors/observational-memory/observation-turn/step.d.ts.map +1 -1
- package/dist/processors/observational-memory/observation-turn/turn.d.ts +4 -0
- package/dist/processors/observational-memory/observation-turn/turn.d.ts.map +1 -1
- package/dist/processors/observational-memory/observational-memory.d.ts +27 -2
- package/dist/processors/observational-memory/observational-memory.d.ts.map +1 -1
- package/dist/processors/observational-memory/observer-runner.d.ts +14 -3
- package/dist/processors/observational-memory/observer-runner.d.ts.map +1 -1
- package/dist/processors/observational-memory/processor.d.ts +8 -12
- package/dist/processors/observational-memory/processor.d.ts.map +1 -1
- package/dist/processors/observational-memory/reflector-runner.d.ts +11 -1
- package/dist/processors/observational-memory/reflector-runner.d.ts.map +1 -1
- package/dist/processors/observational-memory/tracing.d.ts +17 -0
- package/dist/processors/observational-memory/tracing.d.ts.map +1 -0
- package/dist/processors/observational-memory/types.d.ts +19 -1
- package/dist/processors/observational-memory/types.d.ts.map +1 -1
- package/dist/tools/om-tools.d.ts +96 -4
- package/dist/tools/om-tools.d.ts.map +1 -1
- package/package.json +7 -7
- package/dist/chunk-DTAZSLVU.cjs.map +0 -1
- package/dist/chunk-SKRONGCV.js.map +0 -1
|
@@ -9,6 +9,7 @@ import { join } from 'path';
|
|
|
9
9
|
import { randomBytes, createHash, randomUUID } from 'crypto';
|
|
10
10
|
import { estimateTokenCount } from 'tokenx';
|
|
11
11
|
import { Agent } from '@mastra/core/agent';
|
|
12
|
+
import { getOrCreateSpan, EntityType, SpanType, createObservabilityContext } from '@mastra/core/observability';
|
|
12
13
|
import { AsyncLocalStorage } from 'async_hooks';
|
|
13
14
|
import imageSize from 'image-size';
|
|
14
15
|
import { inspect } from 'util';
|
|
@@ -959,7 +960,8 @@ var ObservationStrategy = class _ObservationStrategy {
|
|
|
959
960
|
writer,
|
|
960
961
|
abortSignal,
|
|
961
962
|
reflectionHooks,
|
|
962
|
-
requestContext
|
|
963
|
+
requestContext,
|
|
964
|
+
observabilityContext: this.opts.observabilityContext
|
|
963
965
|
});
|
|
964
966
|
}
|
|
965
967
|
} catch (error) {
|
|
@@ -1100,6 +1102,27 @@ ${threadClose}`;
|
|
|
1100
1102
|
const boundary = lastObservedAt ? _ObservationStrategy.createMessageBoundary(lastObservedAt) : "\n\n";
|
|
1101
1103
|
return `${existingObservations}${boundary}${newThreadSection}`;
|
|
1102
1104
|
}
|
|
1105
|
+
async indexObservationGroups(observations, threadId, resourceId, observedAt) {
|
|
1106
|
+
if (!resourceId || !this.deps.onIndexObservations) {
|
|
1107
|
+
return;
|
|
1108
|
+
}
|
|
1109
|
+
const groups = parseObservationGroups(observations);
|
|
1110
|
+
if (groups.length === 0) {
|
|
1111
|
+
return;
|
|
1112
|
+
}
|
|
1113
|
+
await Promise.all(
|
|
1114
|
+
groups.map(
|
|
1115
|
+
(group) => this.deps.onIndexObservations({
|
|
1116
|
+
text: group.content,
|
|
1117
|
+
groupId: group.id,
|
|
1118
|
+
range: group.range,
|
|
1119
|
+
threadId,
|
|
1120
|
+
resourceId,
|
|
1121
|
+
observedAt
|
|
1122
|
+
})
|
|
1123
|
+
)
|
|
1124
|
+
);
|
|
1125
|
+
}
|
|
1103
1126
|
// ── Marker persistence ──────────────────────────────────────
|
|
1104
1127
|
/**
|
|
1105
1128
|
* Persist a marker to the last assistant message in storage.
|
|
@@ -1233,6 +1256,7 @@ var SyncObservationStrategy = class extends ObservationStrategy {
|
|
|
1233
1256
|
const omMeta = thread ? getThreadOMMetadata(thread.metadata) : void 0;
|
|
1234
1257
|
const result = await this.deps.observer.call(existingObservations, messages, this.opts.abortSignal, {
|
|
1235
1258
|
requestContext: this.opts.requestContext,
|
|
1259
|
+
observabilityContext: this.opts.observabilityContext,
|
|
1236
1260
|
priorCurrentTask: omMeta?.currentTask,
|
|
1237
1261
|
priorSuggestedResponse: omMeta?.suggestedResponse,
|
|
1238
1262
|
priorThreadTitle: omMeta?.threadTitle
|
|
@@ -1282,7 +1306,7 @@ var SyncObservationStrategy = class extends ObservationStrategy {
|
|
|
1282
1306
|
};
|
|
1283
1307
|
}
|
|
1284
1308
|
async persist(processed) {
|
|
1285
|
-
const { record, threadId, messages } = this.opts;
|
|
1309
|
+
const { record, threadId, resourceId, messages } = this.opts;
|
|
1286
1310
|
const thread = await this.storage.getThreadById({ threadId });
|
|
1287
1311
|
let threadUpdateMarker;
|
|
1288
1312
|
if (thread) {
|
|
@@ -1319,6 +1343,7 @@ var SyncObservationStrategy = class extends ObservationStrategy {
|
|
|
1319
1343
|
lastObservedAt: processed.lastObservedAt,
|
|
1320
1344
|
observedMessageIds: processed.observedMessageIds
|
|
1321
1345
|
});
|
|
1346
|
+
await this.indexObservationGroups(processed.observations, threadId, resourceId, processed.lastObservedAt);
|
|
1322
1347
|
}
|
|
1323
1348
|
async emitEndMarkers(cycleId, processed) {
|
|
1324
1349
|
const actualTokensObserved = await this.tokenCounter.countMessagesAsync(this.opts.messages);
|
|
@@ -1384,7 +1409,8 @@ var AsyncBufferObservationStrategy = class extends ObservationStrategy {
|
|
|
1384
1409
|
async observe(existingObservations, messages) {
|
|
1385
1410
|
return this.deps.observer.call(existingObservations, messages, void 0, {
|
|
1386
1411
|
skipContinuationHints: true,
|
|
1387
|
-
requestContext: this.opts.requestContext
|
|
1412
|
+
requestContext: this.opts.requestContext,
|
|
1413
|
+
observabilityContext: this.opts.observabilityContext
|
|
1388
1414
|
});
|
|
1389
1415
|
}
|
|
1390
1416
|
async process(output, _existingObservations) {
|
|
@@ -1423,7 +1449,7 @@ var AsyncBufferObservationStrategy = class extends ObservationStrategy {
|
|
|
1423
1449
|
}
|
|
1424
1450
|
async persist(processed) {
|
|
1425
1451
|
if (!processed.observations) return;
|
|
1426
|
-
const { record, messages } = this.opts;
|
|
1452
|
+
const { record, threadId, resourceId, messages } = this.opts;
|
|
1427
1453
|
const messageTokens = await this.tokenCounter.countMessagesAsync(messages);
|
|
1428
1454
|
await this.storage.updateBufferedObservations({
|
|
1429
1455
|
id: record.id,
|
|
@@ -1440,6 +1466,7 @@ var AsyncBufferObservationStrategy = class extends ObservationStrategy {
|
|
|
1440
1466
|
},
|
|
1441
1467
|
lastBufferedAtTime: processed.lastObservedAt
|
|
1442
1468
|
});
|
|
1469
|
+
await this.indexObservationGroups(processed.observations, threadId, resourceId, processed.lastObservedAt);
|
|
1443
1470
|
}
|
|
1444
1471
|
async emitEndMarkers(_cycleId, processed) {
|
|
1445
1472
|
if (!processed.observations || !this.opts.writer) return;
|
|
@@ -1654,7 +1681,8 @@ var ResourceScopedObservationStrategy = class extends ObservationStrategy {
|
|
|
1654
1681
|
batch.threadIds,
|
|
1655
1682
|
this.opts.abortSignal,
|
|
1656
1683
|
this.opts.requestContext,
|
|
1657
|
-
this.priorMetadataByThread
|
|
1684
|
+
this.priorMetadataByThread,
|
|
1685
|
+
this.opts.observabilityContext
|
|
1658
1686
|
);
|
|
1659
1687
|
})
|
|
1660
1688
|
);
|
|
@@ -1738,7 +1766,7 @@ var ResourceScopedObservationStrategy = class extends ObservationStrategy {
|
|
|
1738
1766
|
};
|
|
1739
1767
|
}
|
|
1740
1768
|
async persist(processed) {
|
|
1741
|
-
const { record } = this.opts;
|
|
1769
|
+
const { record, resourceId } = this.opts;
|
|
1742
1770
|
const threadUpdateMarkers = [];
|
|
1743
1771
|
if (processed.threadMetadataUpdates) {
|
|
1744
1772
|
for (const update of processed.threadMetadataUpdates) {
|
|
@@ -1782,6 +1810,18 @@ var ResourceScopedObservationStrategy = class extends ObservationStrategy {
|
|
|
1782
1810
|
lastObservedAt: processed.lastObservedAt,
|
|
1783
1811
|
observedMessageIds: processed.observedMessageIds
|
|
1784
1812
|
});
|
|
1813
|
+
if (resourceId) {
|
|
1814
|
+
await Promise.all(
|
|
1815
|
+
this.observationResults.map(
|
|
1816
|
+
({ threadId, threadMessages, result }) => this.indexObservationGroups(
|
|
1817
|
+
result.observations,
|
|
1818
|
+
threadId,
|
|
1819
|
+
resourceId,
|
|
1820
|
+
this.getMaxMessageTimestamp(threadMessages)
|
|
1821
|
+
)
|
|
1822
|
+
)
|
|
1823
|
+
);
|
|
1824
|
+
}
|
|
1785
1825
|
}
|
|
1786
1826
|
async emitEndMarkers(cycleId, processed) {
|
|
1787
1827
|
for (const obsResult of this.observationResults) {
|
|
@@ -1839,6 +1879,7 @@ ObservationStrategy.create = ((om, opts) => {
|
|
|
1839
1879
|
reflector: om.reflector,
|
|
1840
1880
|
observedMessageIds: om.observedMessageIds,
|
|
1841
1881
|
obscureThreadIds: om.getObscureThreadIds(),
|
|
1882
|
+
onIndexObservations: om.onIndexObservations,
|
|
1842
1883
|
emitDebugEvent: (e) => om.emitDebugEvent(e)
|
|
1843
1884
|
};
|
|
1844
1885
|
if (opts.cycleId) return new AsyncBufferObservationStrategy(deps, opts);
|
|
@@ -1907,7 +1948,8 @@ var ObservationStep = class {
|
|
|
1907
1948
|
observationTokens: obsTokens,
|
|
1908
1949
|
threadId,
|
|
1909
1950
|
writer: this.turn.writer,
|
|
1910
|
-
requestContext: this.turn.requestContext
|
|
1951
|
+
requestContext: this.turn.requestContext,
|
|
1952
|
+
observabilityContext: this.turn.observabilityContext
|
|
1911
1953
|
});
|
|
1912
1954
|
await this.turn.refreshRecord();
|
|
1913
1955
|
if (this.turn.record.generationCount > preReflectGeneration) {
|
|
@@ -1939,6 +1981,7 @@ var ObservationStep = class {
|
|
|
1939
1981
|
record: statusSnapshot.record,
|
|
1940
1982
|
writer: this.turn.writer,
|
|
1941
1983
|
requestContext: this.turn.requestContext,
|
|
1984
|
+
observabilityContext: this.turn.observabilityContext,
|
|
1942
1985
|
beforeBuffer: async (candidates) => {
|
|
1943
1986
|
if (candidates.length === 0) {
|
|
1944
1987
|
return;
|
|
@@ -2075,7 +2118,8 @@ var ObservationStep = class {
|
|
|
2075
2118
|
threadId,
|
|
2076
2119
|
writer: this.turn.writer,
|
|
2077
2120
|
messageList,
|
|
2078
|
-
requestContext: this.turn.requestContext
|
|
2121
|
+
requestContext: this.turn.requestContext,
|
|
2122
|
+
observabilityContext: this.turn.observabilityContext
|
|
2079
2123
|
});
|
|
2080
2124
|
return {
|
|
2081
2125
|
succeeded: true,
|
|
@@ -2089,7 +2133,8 @@ var ObservationStep = class {
|
|
|
2089
2133
|
resourceId,
|
|
2090
2134
|
messages: messageList.get.all.db(),
|
|
2091
2135
|
requestContext: this.turn.requestContext,
|
|
2092
|
-
writer: this.turn.writer
|
|
2136
|
+
writer: this.turn.writer,
|
|
2137
|
+
observabilityContext: this.turn.observabilityContext
|
|
2093
2138
|
});
|
|
2094
2139
|
return { succeeded: obsResult.observed, record: obsResult.record };
|
|
2095
2140
|
}
|
|
@@ -2110,6 +2155,8 @@ var ObservationTurn = class {
|
|
|
2110
2155
|
writer;
|
|
2111
2156
|
/** Optional request context for observation calls. */
|
|
2112
2157
|
requestContext;
|
|
2158
|
+
/** Optional observability context for nested OM spans. */
|
|
2159
|
+
observabilityContext;
|
|
2113
2160
|
/** Optional processor-provided hooks for turn/step lifecycle integration. */
|
|
2114
2161
|
hooks;
|
|
2115
2162
|
constructor(opts) {
|
|
@@ -2117,6 +2164,7 @@ var ObservationTurn = class {
|
|
|
2117
2164
|
this.threadId = opts.threadId;
|
|
2118
2165
|
this.resourceId = opts.resourceId;
|
|
2119
2166
|
this.messageList = opts.messageList;
|
|
2167
|
+
this.observabilityContext = opts.observabilityContext;
|
|
2120
2168
|
this.hooks = opts.hooks;
|
|
2121
2169
|
}
|
|
2122
2170
|
om;
|
|
@@ -3361,13 +3409,64 @@ function optimizeObservationsForContext(observations) {
|
|
|
3361
3409
|
optimized = optimized.replace(/\n{3,}/g, "\n\n");
|
|
3362
3410
|
return optimized.trim();
|
|
3363
3411
|
}
|
|
3412
|
+
var PHASE_CONFIG = {
|
|
3413
|
+
observer: {
|
|
3414
|
+
name: "om.observer",
|
|
3415
|
+
entityName: "Observer"
|
|
3416
|
+
},
|
|
3417
|
+
"observer-multi-thread": {
|
|
3418
|
+
name: "om.observer.multi-thread",
|
|
3419
|
+
entityName: "MultiThreadObserver"
|
|
3420
|
+
},
|
|
3421
|
+
reflector: {
|
|
3422
|
+
name: "om.reflector",
|
|
3423
|
+
entityName: "Reflector"
|
|
3424
|
+
}
|
|
3425
|
+
};
|
|
3426
|
+
async function withOmTracingSpan({
|
|
3427
|
+
phase,
|
|
3428
|
+
model,
|
|
3429
|
+
inputTokens,
|
|
3430
|
+
requestContext,
|
|
3431
|
+
observabilityContext,
|
|
3432
|
+
metadata,
|
|
3433
|
+
callback
|
|
3434
|
+
}) {
|
|
3435
|
+
const config = PHASE_CONFIG[phase];
|
|
3436
|
+
const span = getOrCreateSpan({
|
|
3437
|
+
type: SpanType.GENERIC,
|
|
3438
|
+
name: config.name,
|
|
3439
|
+
entityType: EntityType.OUTPUT_STEP_PROCESSOR,
|
|
3440
|
+
entityName: config.entityName,
|
|
3441
|
+
tracingContext: observabilityContext?.tracingContext ?? observabilityContext?.tracing,
|
|
3442
|
+
attributes: {
|
|
3443
|
+
metadata: {
|
|
3444
|
+
omPhase: phase,
|
|
3445
|
+
omInputTokens: inputTokens,
|
|
3446
|
+
omSelectedModel: typeof model === "string" ? model : "(dynamic-model)",
|
|
3447
|
+
...metadata
|
|
3448
|
+
}
|
|
3449
|
+
},
|
|
3450
|
+
requestContext
|
|
3451
|
+
});
|
|
3452
|
+
const childObservabilityContext = createObservabilityContext({ currentSpan: span });
|
|
3453
|
+
if (!span) {
|
|
3454
|
+
return callback(childObservabilityContext);
|
|
3455
|
+
}
|
|
3456
|
+
return span.executeInContext(() => callback(childObservabilityContext));
|
|
3457
|
+
}
|
|
3458
|
+
|
|
3459
|
+
// src/processors/observational-memory/observer-runner.ts
|
|
3364
3460
|
var ObserverRunner = class {
|
|
3365
3461
|
observationConfig;
|
|
3366
3462
|
observedMessageIds;
|
|
3367
|
-
|
|
3463
|
+
resolveModel;
|
|
3464
|
+
tokenCounter;
|
|
3368
3465
|
constructor(opts) {
|
|
3369
3466
|
this.observationConfig = opts.observationConfig;
|
|
3370
3467
|
this.observedMessageIds = opts.observedMessageIds;
|
|
3468
|
+
this.resolveModel = opts.resolveModel;
|
|
3469
|
+
this.tokenCounter = opts.tokenCounter;
|
|
3371
3470
|
}
|
|
3372
3471
|
createAgent(model, isMultiThread = false) {
|
|
3373
3472
|
return new Agent({
|
|
@@ -3381,10 +3480,6 @@ var ObserverRunner = class {
|
|
|
3381
3480
|
model
|
|
3382
3481
|
});
|
|
3383
3482
|
}
|
|
3384
|
-
getAgent(model) {
|
|
3385
|
-
this.observerAgent ??= this.createAgent(model);
|
|
3386
|
-
return this.observerAgent;
|
|
3387
|
-
}
|
|
3388
3483
|
async withAbortCheck(fn, abortSignal) {
|
|
3389
3484
|
if (abortSignal?.aborted) {
|
|
3390
3485
|
throw new Error("The operation was aborted.");
|
|
@@ -3399,7 +3494,9 @@ var ObserverRunner = class {
|
|
|
3399
3494
|
* Call the Observer agent for a single thread.
|
|
3400
3495
|
*/
|
|
3401
3496
|
async call(existingObservations, messagesToObserve, abortSignal, options) {
|
|
3402
|
-
const
|
|
3497
|
+
const inputTokens = this.tokenCounter.countMessages(messagesToObserve);
|
|
3498
|
+
const resolvedModel = options?.model ? { model: options.model } : this.resolveModel(inputTokens);
|
|
3499
|
+
const agent = this.createAgent(resolvedModel.model);
|
|
3403
3500
|
const observerMessages = [
|
|
3404
3501
|
{
|
|
3405
3502
|
role: "user",
|
|
@@ -3411,15 +3508,32 @@ var ObserverRunner = class {
|
|
|
3411
3508
|
buildObserverHistoryMessage(messagesToObserve)
|
|
3412
3509
|
];
|
|
3413
3510
|
const doGenerate = async () => {
|
|
3414
|
-
return
|
|
3415
|
-
|
|
3416
|
-
|
|
3417
|
-
|
|
3418
|
-
|
|
3419
|
-
|
|
3420
|
-
|
|
3421
|
-
|
|
3422
|
-
|
|
3511
|
+
return withOmTracingSpan({
|
|
3512
|
+
phase: "observer",
|
|
3513
|
+
model: resolvedModel.model,
|
|
3514
|
+
inputTokens,
|
|
3515
|
+
requestContext: options?.requestContext,
|
|
3516
|
+
observabilityContext: options?.observabilityContext,
|
|
3517
|
+
metadata: {
|
|
3518
|
+
omPreviousObserverTokens: this.observationConfig.previousObserverTokens,
|
|
3519
|
+
omThreadTitleEnabled: this.observationConfig.threadTitle,
|
|
3520
|
+
omSkipContinuationHints: options?.skipContinuationHints ?? false,
|
|
3521
|
+
omWasTruncated: options?.wasTruncated ?? false,
|
|
3522
|
+
...resolvedModel.selectedThreshold !== void 0 ? { omSelectedThreshold: resolvedModel.selectedThreshold } : {},
|
|
3523
|
+
...resolvedModel.routingStrategy ? { omRoutingStrategy: resolvedModel.routingStrategy } : {},
|
|
3524
|
+
...resolvedModel.routingThresholds ? { omRoutingThresholds: resolvedModel.routingThresholds } : {}
|
|
3525
|
+
},
|
|
3526
|
+
callback: (childObservabilityContext) => this.withAbortCheck(async () => {
|
|
3527
|
+
const streamResult = await agent.stream(observerMessages, {
|
|
3528
|
+
modelSettings: { ...this.observationConfig.modelSettings },
|
|
3529
|
+
providerOptions: this.observationConfig.providerOptions,
|
|
3530
|
+
...abortSignal ? { abortSignal } : {},
|
|
3531
|
+
...options?.requestContext ? { requestContext: options.requestContext } : {},
|
|
3532
|
+
...childObservabilityContext
|
|
3533
|
+
});
|
|
3534
|
+
return streamResult.getFullOutput();
|
|
3535
|
+
}, abortSignal)
|
|
3536
|
+
});
|
|
3423
3537
|
};
|
|
3424
3538
|
let result = await doGenerate();
|
|
3425
3539
|
let parsed = parseObserverOutput(result.text);
|
|
@@ -3444,8 +3558,13 @@ var ObserverRunner = class {
|
|
|
3444
3558
|
/**
|
|
3445
3559
|
* Call the Observer agent for multiple threads in a single batched request.
|
|
3446
3560
|
*/
|
|
3447
|
-
async callMultiThread(existingObservations, messagesByThread, threadOrder, abortSignal, requestContext, priorMetadataByThread, model) {
|
|
3448
|
-
const
|
|
3561
|
+
async callMultiThread(existingObservations, messagesByThread, threadOrder, abortSignal, requestContext, priorMetadataByThread, observabilityContext, model) {
|
|
3562
|
+
const inputTokens = Array.from(messagesByThread.values()).reduce(
|
|
3563
|
+
(total, messages) => total + this.tokenCounter.countMessages(messages),
|
|
3564
|
+
0
|
|
3565
|
+
);
|
|
3566
|
+
const resolvedModel = model ? { model } : this.resolveModel(inputTokens);
|
|
3567
|
+
const agent = this.createAgent(resolvedModel.model, true);
|
|
3449
3568
|
const observerMessages = [
|
|
3450
3569
|
{
|
|
3451
3570
|
role: "user",
|
|
@@ -3465,15 +3584,31 @@ var ObserverRunner = class {
|
|
|
3465
3584
|
}
|
|
3466
3585
|
}
|
|
3467
3586
|
const doGenerate = async () => {
|
|
3468
|
-
return
|
|
3469
|
-
|
|
3470
|
-
|
|
3471
|
-
|
|
3472
|
-
|
|
3473
|
-
|
|
3474
|
-
|
|
3475
|
-
|
|
3476
|
-
|
|
3587
|
+
return withOmTracingSpan({
|
|
3588
|
+
phase: "observer-multi-thread",
|
|
3589
|
+
model: resolvedModel.model,
|
|
3590
|
+
inputTokens,
|
|
3591
|
+
requestContext,
|
|
3592
|
+
observabilityContext,
|
|
3593
|
+
metadata: {
|
|
3594
|
+
omThreadCount: threadOrder.length,
|
|
3595
|
+
omPreviousObserverTokens: this.observationConfig.previousObserverTokens,
|
|
3596
|
+
omThreadTitleEnabled: this.observationConfig.threadTitle,
|
|
3597
|
+
...resolvedModel.selectedThreshold !== void 0 ? { omSelectedThreshold: resolvedModel.selectedThreshold } : {},
|
|
3598
|
+
...resolvedModel.routingStrategy ? { omRoutingStrategy: resolvedModel.routingStrategy } : {},
|
|
3599
|
+
...resolvedModel.routingThresholds ? { omRoutingThresholds: resolvedModel.routingThresholds } : {}
|
|
3600
|
+
},
|
|
3601
|
+
callback: (childObservabilityContext) => this.withAbortCheck(async () => {
|
|
3602
|
+
const streamResult = await agent.stream(observerMessages, {
|
|
3603
|
+
modelSettings: { ...this.observationConfig.modelSettings },
|
|
3604
|
+
providerOptions: this.observationConfig.providerOptions,
|
|
3605
|
+
...abortSignal ? { abortSignal } : {},
|
|
3606
|
+
...requestContext ? { requestContext } : {},
|
|
3607
|
+
...childObservabilityContext
|
|
3608
|
+
});
|
|
3609
|
+
return streamResult.getFullOutput();
|
|
3610
|
+
}, abortSignal)
|
|
3611
|
+
});
|
|
3477
3612
|
};
|
|
3478
3613
|
let result = await doGenerate();
|
|
3479
3614
|
let parsed = parseMultiThreadObserverOutput(result.text);
|
|
@@ -3780,6 +3915,7 @@ var ReflectorRunner = class {
|
|
|
3780
3915
|
reflectionConfig;
|
|
3781
3916
|
observationConfig;
|
|
3782
3917
|
tokenCounter;
|
|
3918
|
+
resolveModel;
|
|
3783
3919
|
storage;
|
|
3784
3920
|
scope;
|
|
3785
3921
|
buffering;
|
|
@@ -3791,6 +3927,7 @@ var ReflectorRunner = class {
|
|
|
3791
3927
|
this.reflectionConfig = opts.reflectionConfig;
|
|
3792
3928
|
this.observationConfig = opts.observationConfig;
|
|
3793
3929
|
this.tokenCounter = opts.tokenCounter;
|
|
3930
|
+
this.resolveModel = opts.resolveModel;
|
|
3794
3931
|
this.storage = opts.storage;
|
|
3795
3932
|
this.scope = opts.scope;
|
|
3796
3933
|
this.buffering = opts.buffering;
|
|
@@ -3817,9 +3954,10 @@ var ReflectorRunner = class {
|
|
|
3817
3954
|
/**
|
|
3818
3955
|
* Call the Reflector agent with escalating compression levels.
|
|
3819
3956
|
*/
|
|
3820
|
-
async call(observations, manualPrompt, streamContext, observationTokensThreshold, abortSignal, skipContinuationHints, compressionStartLevel, requestContext, model) {
|
|
3821
|
-
const agent = this.createAgent(model ?? this.reflectionConfig.model);
|
|
3957
|
+
async call(observations, manualPrompt, streamContext, observationTokensThreshold, abortSignal, skipContinuationHints, compressionStartLevel, requestContext, observabilityContext, model) {
|
|
3822
3958
|
const originalTokens = this.tokenCounter.countObservations(observations);
|
|
3959
|
+
const resolvedModel = model ? { model } : this.resolveModel(originalTokens);
|
|
3960
|
+
const agent = this.createAgent(resolvedModel.model);
|
|
3823
3961
|
const targetThreshold = observationTokensThreshold ?? getMaxThreshold(this.reflectionConfig.observationTokens);
|
|
3824
3962
|
let totalUsage = { inputTokens: 0, outputTokens: 0, totalTokens: 0 };
|
|
3825
3963
|
const startLevel = compressionStartLevel ?? 0;
|
|
@@ -3836,37 +3974,54 @@ var ReflectorRunner = class {
|
|
|
3836
3974
|
`[OM:callReflector] ${isRetry ? `retry #${attemptNumber - 1}` : "first attempt"}: level=${currentLevel}, originalTokens=${originalTokens}, targetThreshold=${targetThreshold}, promptLen=${prompt.length}, skipContinuationHints=${skipContinuationHints}`
|
|
3837
3975
|
);
|
|
3838
3976
|
let chunkCount = 0;
|
|
3839
|
-
const result = await
|
|
3840
|
-
|
|
3841
|
-
|
|
3842
|
-
|
|
3843
|
-
|
|
3844
|
-
|
|
3845
|
-
|
|
3846
|
-
|
|
3847
|
-
|
|
3848
|
-
|
|
3849
|
-
|
|
3850
|
-
|
|
3851
|
-
|
|
3852
|
-
|
|
3853
|
-
|
|
3854
|
-
|
|
3855
|
-
|
|
3856
|
-
|
|
3857
|
-
|
|
3858
|
-
);
|
|
3859
|
-
},
|
|
3860
|
-
onAbort(event) {
|
|
3861
|
-
omDebug(`[OM:callReflector] onAbort: chunks=${chunkCount}, reason=${event?.reason ?? "unknown"}`);
|
|
3977
|
+
const result = await withOmTracingSpan({
|
|
3978
|
+
phase: "reflector",
|
|
3979
|
+
model: resolvedModel.model,
|
|
3980
|
+
inputTokens: originalTokens,
|
|
3981
|
+
requestContext,
|
|
3982
|
+
observabilityContext,
|
|
3983
|
+
metadata: {
|
|
3984
|
+
omCompressionLevel: currentLevel,
|
|
3985
|
+
omCompressionAttempt: attemptNumber,
|
|
3986
|
+
omTargetThreshold: targetThreshold,
|
|
3987
|
+
omSkipContinuationHints: skipContinuationHints ?? false,
|
|
3988
|
+
...resolvedModel.selectedThreshold !== void 0 ? { omSelectedThreshold: resolvedModel.selectedThreshold } : {},
|
|
3989
|
+
...resolvedModel.routingStrategy ? { omRoutingStrategy: resolvedModel.routingStrategy } : {},
|
|
3990
|
+
...resolvedModel.routingThresholds ? { omRoutingThresholds: resolvedModel.routingThresholds } : {}
|
|
3991
|
+
},
|
|
3992
|
+
callback: (childObservabilityContext) => withAbortCheck(async () => {
|
|
3993
|
+
const streamResult = await agent.stream(prompt, {
|
|
3994
|
+
modelSettings: {
|
|
3995
|
+
...this.reflectionConfig.modelSettings
|
|
3862
3996
|
},
|
|
3863
|
-
|
|
3864
|
-
|
|
3865
|
-
}
|
|
3866
|
-
|
|
3867
|
-
|
|
3868
|
-
|
|
3869
|
-
|
|
3997
|
+
providerOptions: this.reflectionConfig.providerOptions,
|
|
3998
|
+
...abortSignal ? { abortSignal } : {},
|
|
3999
|
+
...requestContext ? { requestContext } : {},
|
|
4000
|
+
...childObservabilityContext,
|
|
4001
|
+
...attemptNumber === 1 ? {
|
|
4002
|
+
onChunk(chunk) {
|
|
4003
|
+
chunkCount++;
|
|
4004
|
+
if (chunkCount === 1 || chunkCount % 50 === 0) {
|
|
4005
|
+
const preview = chunk.type === "text-delta" ? ` text="${chunk.textDelta?.slice(0, 80)}..."` : chunk.type === "tool-call" ? ` tool=${chunk.toolName}` : "";
|
|
4006
|
+
omDebug(`[OM:callReflector] chunk#${chunkCount}: type=${chunk.type}${preview}`);
|
|
4007
|
+
}
|
|
4008
|
+
},
|
|
4009
|
+
onFinish(event) {
|
|
4010
|
+
omDebug(
|
|
4011
|
+
`[OM:callReflector] onFinish: chunks=${chunkCount}, finishReason=${event.finishReason}, inputTokens=${event.usage?.inputTokens}, outputTokens=${event.usage?.outputTokens}, textLen=${event.text?.length}`
|
|
4012
|
+
);
|
|
4013
|
+
},
|
|
4014
|
+
onAbort(event) {
|
|
4015
|
+
omDebug(`[OM:callReflector] onAbort: chunks=${chunkCount}, reason=${event?.reason ?? "unknown"}`);
|
|
4016
|
+
},
|
|
4017
|
+
onError({ error }) {
|
|
4018
|
+
omError(`[OM:callReflector] onError after ${chunkCount} chunks`, error);
|
|
4019
|
+
}
|
|
4020
|
+
} : {}
|
|
4021
|
+
});
|
|
4022
|
+
return streamResult.getFullOutput();
|
|
4023
|
+
}, abortSignal)
|
|
4024
|
+
});
|
|
3870
4025
|
omDebug(
|
|
3871
4026
|
`[OM:callReflector] attempt #${attemptNumber} returned: textLen=${result.text?.length}, textPreview="${result.text?.slice(0, 120)}...", inputTokens=${result.usage?.inputTokens ?? result.totalUsage?.inputTokens}, outputTokens=${result.usage?.outputTokens ?? result.totalUsage?.outputTokens}`
|
|
3872
4027
|
);
|
|
@@ -3933,7 +4088,7 @@ var ReflectorRunner = class {
|
|
|
3933
4088
|
/**
|
|
3934
4089
|
* Start an async buffered reflection in the background.
|
|
3935
4090
|
*/
|
|
3936
|
-
startAsyncBufferedReflection(record, observationTokens, lockKey, writer, requestContext) {
|
|
4091
|
+
startAsyncBufferedReflection(record, observationTokens, lockKey, writer, requestContext, observabilityContext) {
|
|
3937
4092
|
const bufferKey = this.buffering.getReflectionBufferKey(lockKey);
|
|
3938
4093
|
if (this.buffering.isAsyncBufferingInProgress(bufferKey)) {
|
|
3939
4094
|
return;
|
|
@@ -3943,7 +4098,7 @@ var ReflectorRunner = class {
|
|
|
3943
4098
|
this.storage.setBufferingReflectionFlag(record.id, true).catch((err) => {
|
|
3944
4099
|
omError("[OM] Failed to set buffering reflection flag", err);
|
|
3945
4100
|
});
|
|
3946
|
-
const asyncOp = this.doAsyncBufferedReflection(record, bufferKey, writer, requestContext).catch(async (error) => {
|
|
4101
|
+
const asyncOp = this.doAsyncBufferedReflection(record, bufferKey, writer, requestContext, observabilityContext).catch(async (error) => {
|
|
3947
4102
|
if (writer) {
|
|
3948
4103
|
const failedMarker = createBufferingFailedMarker({
|
|
3949
4104
|
cycleId: `reflect-buf-${Date.now()}-${Math.random().toString(36).slice(2, 11)}`,
|
|
@@ -3973,7 +4128,7 @@ var ReflectorRunner = class {
|
|
|
3973
4128
|
* Perform async buffered reflection — reflects observations and stores to bufferedReflection.
|
|
3974
4129
|
* Does NOT create a new generation or update activeObservations.
|
|
3975
4130
|
*/
|
|
3976
|
-
async doAsyncBufferedReflection(record, _bufferKey, writer, requestContext) {
|
|
4131
|
+
async doAsyncBufferedReflection(record, _bufferKey, writer, requestContext, observabilityContext) {
|
|
3977
4132
|
const freshRecord = await this.storage.getObservationalMemory(record.threadId, record.resourceId);
|
|
3978
4133
|
const currentRecord = freshRecord ?? record;
|
|
3979
4134
|
const observationTokens = currentRecord.observationTokenCount ?? 0;
|
|
@@ -4020,7 +4175,8 @@ var ReflectorRunner = class {
|
|
|
4020
4175
|
void 0,
|
|
4021
4176
|
true,
|
|
4022
4177
|
compressionStartLevel,
|
|
4023
|
-
requestContext
|
|
4178
|
+
requestContext,
|
|
4179
|
+
observabilityContext
|
|
4024
4180
|
);
|
|
4025
4181
|
const reflectionTokenCount = this.tokenCounter.countObservations(reflectResult.observations);
|
|
4026
4182
|
omDebug(
|
|
@@ -4136,7 +4292,16 @@ ${unreflectedContent}` : freshRecord.bufferedReflection;
|
|
|
4136
4292
|
* @internal Used by observation strategies. Do not call directly.
|
|
4137
4293
|
*/
|
|
4138
4294
|
async maybeReflect(opts) {
|
|
4139
|
-
const {
|
|
4295
|
+
const {
|
|
4296
|
+
record,
|
|
4297
|
+
observationTokens,
|
|
4298
|
+
writer,
|
|
4299
|
+
abortSignal,
|
|
4300
|
+
messageList,
|
|
4301
|
+
reflectionHooks,
|
|
4302
|
+
requestContext,
|
|
4303
|
+
observabilityContext
|
|
4304
|
+
} = opts;
|
|
4140
4305
|
const lockKey = this.buffering.getLockKey(record.threadId, record.resourceId);
|
|
4141
4306
|
const reflectThreshold = getMaxThreshold(this.reflectionConfig.observationTokens);
|
|
4142
4307
|
if (this.buffering.isAsyncReflectionEnabled() && observationTokens < reflectThreshold) {
|
|
@@ -4156,7 +4321,14 @@ ${unreflectedContent}` : freshRecord.bufferedReflection;
|
|
|
4156
4321
|
return observationTokens >= activationPoint;
|
|
4157
4322
|
})();
|
|
4158
4323
|
if (shouldTrigger) {
|
|
4159
|
-
this.startAsyncBufferedReflection(
|
|
4324
|
+
this.startAsyncBufferedReflection(
|
|
4325
|
+
record,
|
|
4326
|
+
observationTokens,
|
|
4327
|
+
lockKey,
|
|
4328
|
+
writer,
|
|
4329
|
+
requestContext,
|
|
4330
|
+
observabilityContext
|
|
4331
|
+
);
|
|
4160
4332
|
}
|
|
4161
4333
|
}
|
|
4162
4334
|
if (observationTokens < reflectThreshold) {
|
|
@@ -4183,7 +4355,14 @@ ${unreflectedContent}` : freshRecord.bufferedReflection;
|
|
|
4183
4355
|
omDebug(
|
|
4184
4356
|
`[OM:reflect] async activation failed, no blockAfter or below it (obsTokens=${observationTokens}, blockAfter=${this.reflectionConfig.blockAfter}) \u2014 starting background reflection`
|
|
4185
4357
|
);
|
|
4186
|
-
this.startAsyncBufferedReflection(
|
|
4358
|
+
this.startAsyncBufferedReflection(
|
|
4359
|
+
record,
|
|
4360
|
+
observationTokens,
|
|
4361
|
+
lockKey,
|
|
4362
|
+
writer,
|
|
4363
|
+
requestContext,
|
|
4364
|
+
observabilityContext
|
|
4365
|
+
);
|
|
4187
4366
|
return;
|
|
4188
4367
|
}
|
|
4189
4368
|
}
|
|
@@ -4231,7 +4410,8 @@ ${unreflectedContent}` : freshRecord.bufferedReflection;
|
|
|
4231
4410
|
abortSignal,
|
|
4232
4411
|
void 0,
|
|
4233
4412
|
compressionStartLevel,
|
|
4234
|
-
requestContext
|
|
4413
|
+
requestContext,
|
|
4414
|
+
observabilityContext
|
|
4235
4415
|
);
|
|
4236
4416
|
const reflectionTokenCount = this.tokenCounter.countObservations(reflectResult.observations);
|
|
4237
4417
|
await this.storage.createReflectionGeneration({
|
|
@@ -5519,11 +5699,12 @@ var ObservationalMemory = class _ObservationalMemory {
|
|
|
5519
5699
|
storage;
|
|
5520
5700
|
tokenCounter;
|
|
5521
5701
|
scope;
|
|
5522
|
-
/** Whether retrieval-mode observation groups are enabled
|
|
5702
|
+
/** Whether retrieval-mode observation groups are enabled. */
|
|
5523
5703
|
retrieval;
|
|
5524
5704
|
observationConfig;
|
|
5525
5705
|
reflectionConfig;
|
|
5526
5706
|
onDebugEvent;
|
|
5707
|
+
onIndexObservations;
|
|
5527
5708
|
/** Observer agent runner — handles LLM calls for extracting observations. */
|
|
5528
5709
|
observer;
|
|
5529
5710
|
/** Reflector agent runner — handles LLM calls for compressing observations. */
|
|
@@ -5596,7 +5777,8 @@ var ObservationalMemory = class _ObservationalMemory {
|
|
|
5596
5777
|
this.shouldObscureThreadIds = config.obscureThreadIds || false;
|
|
5597
5778
|
this.storage = config.storage;
|
|
5598
5779
|
this.scope = config.scope ?? "thread";
|
|
5599
|
-
this.retrieval =
|
|
5780
|
+
this.retrieval = Boolean(config.retrieval);
|
|
5781
|
+
this.onIndexObservations = config.onIndexObservations;
|
|
5600
5782
|
const resolveModel = (m) => m === "default" ? OBSERVATIONAL_MEMORY_DEFAULTS.observation.model : m;
|
|
5601
5783
|
const observationModel = resolveModel(config.model) ?? resolveModel(config.observation?.model) ?? resolveModel(config.reflection?.model);
|
|
5602
5784
|
const reflectionModel = resolveModel(config.model) ?? resolveModel(config.reflection?.model) ?? resolveModel(config.observation?.model);
|
|
@@ -5690,7 +5872,9 @@ Async buffering is enabled by default \u2014 this opt-out is only needed when us
|
|
|
5690
5872
|
this.messageHistory = new MessageHistory({ storage: this.storage });
|
|
5691
5873
|
this.observer = new ObserverRunner({
|
|
5692
5874
|
observationConfig: this.observationConfig,
|
|
5693
|
-
observedMessageIds: this.observedMessageIds
|
|
5875
|
+
observedMessageIds: this.observedMessageIds,
|
|
5876
|
+
resolveModel: (inputTokens) => this.resolveObservationModel(inputTokens),
|
|
5877
|
+
tokenCounter: this.tokenCounter
|
|
5694
5878
|
});
|
|
5695
5879
|
this.buffering = new BufferingCoordinator({
|
|
5696
5880
|
observationConfig: this.observationConfig,
|
|
@@ -5707,7 +5891,8 @@ Async buffering is enabled by default \u2014 this opt-out is only needed when us
|
|
|
5707
5891
|
emitDebugEvent: (e) => this.emitDebugEvent(e),
|
|
5708
5892
|
persistMarkerToStorage: (m, t, r) => this.persistMarkerToStorage(m, t, r),
|
|
5709
5893
|
persistMarkerToMessage: (m, ml, t, r) => this.persistMarkerToMessage(m, ml, t, r),
|
|
5710
|
-
getCompressionStartLevel: (rc) => this.getCompressionStartLevel(rc)
|
|
5894
|
+
getCompressionStartLevel: (rc) => this.getCompressionStartLevel(rc),
|
|
5895
|
+
resolveModel: (inputTokens) => this.resolveReflectionModel(inputTokens)
|
|
5711
5896
|
});
|
|
5712
5897
|
this.validateBufferConfig();
|
|
5713
5898
|
omDebug(
|
|
@@ -5769,6 +5954,54 @@ Async buffering is enabled by default \u2014 this opt-out is only needed when us
|
|
|
5769
5954
|
}
|
|
5770
5955
|
return model.provider ? `${model.provider}/${model.modelId}` : model.modelId;
|
|
5771
5956
|
}
|
|
5957
|
+
resolveObservationModel(inputTokens) {
|
|
5958
|
+
return this.resolveTieredModel(this.observationConfig.model, inputTokens);
|
|
5959
|
+
}
|
|
5960
|
+
resolveReflectionModel(inputTokens) {
|
|
5961
|
+
return this.resolveTieredModel(this.reflectionConfig.model, inputTokens);
|
|
5962
|
+
}
|
|
5963
|
+
resolveTieredModel(model, inputTokens) {
|
|
5964
|
+
if (!(model instanceof ModelByInputTokens)) {
|
|
5965
|
+
return {
|
|
5966
|
+
model
|
|
5967
|
+
};
|
|
5968
|
+
}
|
|
5969
|
+
const thresholds = model.getThresholds();
|
|
5970
|
+
const selectedThreshold = thresholds.find((upTo) => inputTokens <= upTo) ?? thresholds.at(-1);
|
|
5971
|
+
return {
|
|
5972
|
+
model: model.resolve(inputTokens),
|
|
5973
|
+
selectedThreshold,
|
|
5974
|
+
routingStrategy: "model-by-input-tokens",
|
|
5975
|
+
routingThresholds: thresholds.join(",")
|
|
5976
|
+
};
|
|
5977
|
+
}
|
|
5978
|
+
async resolveModelRouting(modelConfig, requestContext) {
|
|
5979
|
+
try {
|
|
5980
|
+
if (modelConfig instanceof ModelByInputTokens) {
|
|
5981
|
+
const routing = await Promise.all(
|
|
5982
|
+
modelConfig.getThresholds().map(async (upTo) => {
|
|
5983
|
+
const resolvedModel = modelConfig.resolve(upTo);
|
|
5984
|
+
const resolved2 = await this.resolveModelContext(resolvedModel, requestContext);
|
|
5985
|
+
return {
|
|
5986
|
+
upTo,
|
|
5987
|
+
model: resolved2?.modelId ? this.formatModelName(resolved2) : "(unknown)"
|
|
5988
|
+
};
|
|
5989
|
+
})
|
|
5990
|
+
);
|
|
5991
|
+
return {
|
|
5992
|
+
model: routing[0]?.model ?? "(unknown)",
|
|
5993
|
+
routing
|
|
5994
|
+
};
|
|
5995
|
+
}
|
|
5996
|
+
const resolved = await this.resolveModelContext(modelConfig, requestContext);
|
|
5997
|
+
return {
|
|
5998
|
+
model: resolved?.modelId ? this.formatModelName(resolved) : "(unknown)"
|
|
5999
|
+
};
|
|
6000
|
+
} catch (error) {
|
|
6001
|
+
omError("[OM] Failed to resolve model config", error);
|
|
6002
|
+
return { model: "(unknown)" };
|
|
6003
|
+
}
|
|
6004
|
+
}
|
|
5772
6005
|
async resolveModelContext(modelConfig, requestContext, inputTokens) {
|
|
5773
6006
|
const modelToResolve = this.getModelToResolve(modelConfig, inputTokens);
|
|
5774
6007
|
if (!modelToResolve) {
|
|
@@ -5801,29 +6034,22 @@ Async buffering is enabled by default \u2014 this opt-out is only needed when us
|
|
|
5801
6034
|
* This is async because it needs to resolve the model configs.
|
|
5802
6035
|
*/
|
|
5803
6036
|
async getResolvedConfig(requestContext) {
|
|
5804
|
-
const
|
|
5805
|
-
|
|
5806
|
-
|
|
5807
|
-
return resolved?.modelId ? this.formatModelName(resolved) : "(unknown)";
|
|
5808
|
-
} catch (error) {
|
|
5809
|
-
omError("[OM] Failed to resolve model config", error);
|
|
5810
|
-
return "(unknown)";
|
|
5811
|
-
}
|
|
5812
|
-
};
|
|
5813
|
-
const [observationModelName, reflectionModelName] = await Promise.all([
|
|
5814
|
-
safeResolveModel(this.observationConfig.model),
|
|
5815
|
-
safeResolveModel(this.reflectionConfig.model)
|
|
6037
|
+
const [observationResolved, reflectionResolved] = await Promise.all([
|
|
6038
|
+
this.resolveModelRouting(this.observationConfig.model, requestContext),
|
|
6039
|
+
this.resolveModelRouting(this.reflectionConfig.model, requestContext)
|
|
5816
6040
|
]);
|
|
5817
6041
|
return {
|
|
5818
6042
|
scope: this.scope,
|
|
5819
6043
|
observation: {
|
|
5820
6044
|
messageTokens: this.observationConfig.messageTokens,
|
|
5821
|
-
model:
|
|
5822
|
-
previousObserverTokens: this.observationConfig.previousObserverTokens
|
|
6045
|
+
model: observationResolved.model,
|
|
6046
|
+
previousObserverTokens: this.observationConfig.previousObserverTokens,
|
|
6047
|
+
routing: observationResolved.routing
|
|
5823
6048
|
},
|
|
5824
6049
|
reflection: {
|
|
5825
6050
|
observationTokens: this.reflectionConfig.observationTokens,
|
|
5826
|
-
model:
|
|
6051
|
+
model: reflectionResolved.model,
|
|
6052
|
+
routing: reflectionResolved.routing
|
|
5827
6053
|
}
|
|
5828
6054
|
};
|
|
5829
6055
|
}
|
|
@@ -6630,7 +6856,7 @@ ${grouped}` : grouped;
|
|
|
6630
6856
|
* @param lockKey - Lock key for this scope
|
|
6631
6857
|
* @param writer - Optional stream writer for emitting buffering markers
|
|
6632
6858
|
*/
|
|
6633
|
-
async startAsyncBufferedObservation(record, threadId, unobservedMessages, lockKey, writer, contextWindowTokens, requestContext) {
|
|
6859
|
+
async startAsyncBufferedObservation(record, threadId, unobservedMessages, lockKey, writer, contextWindowTokens, requestContext, observabilityContext) {
|
|
6634
6860
|
const bufferKey = this.buffering.getObservationBufferKey(lockKey);
|
|
6635
6861
|
const currentTokens = contextWindowTokens ?? await this.tokenCounter.countMessagesAsync(unobservedMessages) + (record.pendingMessageTokens ?? 0);
|
|
6636
6862
|
BufferingCoordinator.lastBufferedBoundary.set(bufferKey, currentTokens);
|
|
@@ -6644,7 +6870,8 @@ ${grouped}` : grouped;
|
|
|
6644
6870
|
unobservedMessages,
|
|
6645
6871
|
bufferKey,
|
|
6646
6872
|
writer,
|
|
6647
|
-
requestContext
|
|
6873
|
+
requestContext,
|
|
6874
|
+
observabilityContext
|
|
6648
6875
|
).finally(() => {
|
|
6649
6876
|
BufferingCoordinator.asyncBufferingOps.delete(bufferKey);
|
|
6650
6877
|
unregisterOp(record.id, "bufferingObservation");
|
|
@@ -6658,7 +6885,7 @@ ${grouped}` : grouped;
|
|
|
6658
6885
|
* Internal method that waits for existing buffering operation and then runs new buffering.
|
|
6659
6886
|
* This implements the mutex-wait behavior.
|
|
6660
6887
|
*/
|
|
6661
|
-
async runAsyncBufferedObservation(record, threadId, unobservedMessages, bufferKey, writer, requestContext) {
|
|
6888
|
+
async runAsyncBufferedObservation(record, threadId, unobservedMessages, bufferKey, writer, requestContext, observabilityContext) {
|
|
6662
6889
|
const existingOp = BufferingCoordinator.asyncBufferingOps.get(bufferKey);
|
|
6663
6890
|
if (existingOp) {
|
|
6664
6891
|
try {
|
|
@@ -6730,7 +6957,8 @@ ${grouped}` : grouped;
|
|
|
6730
6957
|
cycleId,
|
|
6731
6958
|
startedAt,
|
|
6732
6959
|
writer,
|
|
6733
|
-
requestContext
|
|
6960
|
+
requestContext,
|
|
6961
|
+
observabilityContext
|
|
6734
6962
|
}).run();
|
|
6735
6963
|
const maxTs = this.getMaxMessageTimestamp(messagesToBuffer);
|
|
6736
6964
|
const cursor = new Date(maxTs.getTime() + 1);
|
|
@@ -7277,7 +7505,7 @@ ${grouped}` : grouped;
|
|
|
7277
7505
|
*/
|
|
7278
7506
|
/** @internal Used by ObservationStep. */
|
|
7279
7507
|
async buffer(opts) {
|
|
7280
|
-
const { threadId, resourceId, requestContext } = opts;
|
|
7508
|
+
const { threadId, resourceId, requestContext, observabilityContext } = opts;
|
|
7281
7509
|
let record = opts.record ?? await this.getOrCreateRecord(threadId, resourceId);
|
|
7282
7510
|
if (!this.buffering.isAsyncObservationEnabled()) {
|
|
7283
7511
|
return { buffered: false, record };
|
|
@@ -7373,7 +7601,8 @@ ${grouped}` : grouped;
|
|
|
7373
7601
|
cycleId,
|
|
7374
7602
|
startedAt,
|
|
7375
7603
|
writer,
|
|
7376
|
-
requestContext
|
|
7604
|
+
requestContext,
|
|
7605
|
+
observabilityContext
|
|
7377
7606
|
}).run();
|
|
7378
7607
|
await this.storage.setBufferingObservationFlag(record.id, false, newTokens).catch(() => {
|
|
7379
7608
|
});
|
|
@@ -7577,7 +7806,8 @@ ${grouped}` : grouped;
|
|
|
7577
7806
|
messages: unobservedMessages,
|
|
7578
7807
|
reflectionHooks,
|
|
7579
7808
|
requestContext,
|
|
7580
|
-
writer: opts.writer
|
|
7809
|
+
writer: opts.writer,
|
|
7810
|
+
observabilityContext: opts.observabilityContext
|
|
7581
7811
|
}).run();
|
|
7582
7812
|
observed = true;
|
|
7583
7813
|
} finally {
|
|
@@ -7599,7 +7829,7 @@ ${grouped}` : grouped;
|
|
|
7599
7829
|
* );
|
|
7600
7830
|
* ```
|
|
7601
7831
|
*/
|
|
7602
|
-
async reflect(threadId, resourceId, prompt, requestContext) {
|
|
7832
|
+
async reflect(threadId, resourceId, prompt, requestContext, observabilityContext) {
|
|
7603
7833
|
const record = await this.getOrCreateRecord(threadId, resourceId);
|
|
7604
7834
|
if (!record.activeObservations) {
|
|
7605
7835
|
return { reflected: false, record };
|
|
@@ -7616,7 +7846,9 @@ ${grouped}` : grouped;
|
|
|
7616
7846
|
void 0,
|
|
7617
7847
|
void 0,
|
|
7618
7848
|
void 0,
|
|
7619
|
-
requestContext
|
|
7849
|
+
requestContext,
|
|
7850
|
+
observabilityContext,
|
|
7851
|
+
void 0
|
|
7620
7852
|
);
|
|
7621
7853
|
const reflectionTokenCount = this.tokenCounter.countObservations(reflectResult.observations);
|
|
7622
7854
|
await this.storage.createReflectionGeneration({
|
|
@@ -7723,6 +7955,7 @@ ${grouped}` : grouped;
|
|
|
7723
7955
|
threadId: opts.threadId,
|
|
7724
7956
|
resourceId: opts.resourceId,
|
|
7725
7957
|
messageList: opts.messageList,
|
|
7958
|
+
observabilityContext: opts.observabilityContext,
|
|
7726
7959
|
hooks: opts.hooks
|
|
7727
7960
|
});
|
|
7728
7961
|
}
|
|
@@ -7973,6 +8206,17 @@ function writeProcessInputStepReproCapture(params) {
|
|
|
7973
8206
|
}
|
|
7974
8207
|
|
|
7975
8208
|
// src/processors/observational-memory/processor.ts
|
|
8209
|
+
function getOmObservabilityContext(args) {
|
|
8210
|
+
if (!args.tracing || !args.tracingContext || !args.loggerVNext || !args.metrics) {
|
|
8211
|
+
return void 0;
|
|
8212
|
+
}
|
|
8213
|
+
return {
|
|
8214
|
+
tracing: args.tracing,
|
|
8215
|
+
tracingContext: args.tracingContext,
|
|
8216
|
+
loggerVNext: args.loggerVNext,
|
|
8217
|
+
metrics: args.metrics
|
|
8218
|
+
};
|
|
8219
|
+
}
|
|
7976
8220
|
var ObservationalMemoryProcessor = class {
|
|
7977
8221
|
id = "observational-memory";
|
|
7978
8222
|
name = "Observational Memory";
|
|
@@ -8030,6 +8274,7 @@ var ObservationalMemoryProcessor = class {
|
|
|
8030
8274
|
threadId,
|
|
8031
8275
|
resourceId,
|
|
8032
8276
|
messageList,
|
|
8277
|
+
observabilityContext: getOmObservabilityContext(args),
|
|
8033
8278
|
hooks: {
|
|
8034
8279
|
onBufferChunkSealed: rotateResponseMessageId
|
|
8035
8280
|
}
|
|
@@ -8039,6 +8284,9 @@ var ObservationalMemoryProcessor = class {
|
|
|
8039
8284
|
await this.turn.start(this.memory);
|
|
8040
8285
|
state.__omTurn = this.turn;
|
|
8041
8286
|
}
|
|
8287
|
+
const observabilityContext = getOmObservabilityContext(args);
|
|
8288
|
+
state.__omObservabilityContext = observabilityContext;
|
|
8289
|
+
this.turn.observabilityContext = observabilityContext;
|
|
8042
8290
|
{
|
|
8043
8291
|
const step = this.turn.step(stepNumber);
|
|
8044
8292
|
let ctx;
|
|
@@ -8119,6 +8367,8 @@ var ObservationalMemoryProcessor = class {
|
|
|
8119
8367
|
const state = _state ?? {};
|
|
8120
8368
|
const context = this.engine.getThreadContext(requestContext, messageList);
|
|
8121
8369
|
if (!context) return messageList;
|
|
8370
|
+
const observabilityContext = getOmObservabilityContext(args);
|
|
8371
|
+
state.__omObservabilityContext = observabilityContext;
|
|
8122
8372
|
return this.engine.getTokenCounter().runWithModelContext(state.__omActorModelContext, async () => {
|
|
8123
8373
|
const memoryContext = parseMemoryRequestContext(requestContext);
|
|
8124
8374
|
if (memoryContext?.memoryConfig?.readOnly) return messageList;
|
|
@@ -8168,5 +8418,5 @@ function getObservationsAsOf(activeObservations, asOf) {
|
|
|
8168
8418
|
}
|
|
8169
8419
|
|
|
8170
8420
|
export { ModelByInputTokens, OBSERVER_SYSTEM_PROMPT, ObservationalMemory, ObservationalMemoryProcessor, TokenCounter, buildObserverPrompt, buildObserverSystemPrompt, combineObservationGroupRanges, deriveObservationGroupProvenance, extractCurrentTask, formatMessagesForObserver, formatToolResultForObserver, getObservationsAsOf, hasCurrentTaskSection, injectAnchorIds, optimizeObservationsForContext, parseAnchorId, parseObservationGroups, parseObserverOutput, reconcileObservationGroupsFromReflection, renderObservationGroupsForReflection, resolveToolResultValue, stripEphemeralAnchorIds, stripObservationGroups, truncateStringByTokens, wrapInObservationGroup };
|
|
8171
|
-
//# sourceMappingURL=chunk-
|
|
8172
|
-
//# sourceMappingURL=chunk-
|
|
8421
|
+
//# sourceMappingURL=chunk-D4D6ZFBQ.js.map
|
|
8422
|
+
//# sourceMappingURL=chunk-D4D6ZFBQ.js.map
|