@mastra/server 1.18.0-alpha.1 → 1.18.0-alpha.3
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 +16 -0
- package/dist/{chunk-ZBOL3JND.cjs → chunk-GDWCOWNR.cjs} +585 -44
- package/dist/chunk-GDWCOWNR.cjs.map +1 -0
- package/dist/{chunk-DJQT32SV.cjs → chunk-HUAXEKGI.cjs} +353 -105
- package/dist/chunk-HUAXEKGI.cjs.map +1 -0
- package/dist/{chunk-OCSZZ5UK.cjs → chunk-KXZR6QGF.cjs} +5 -3
- package/dist/chunk-KXZR6QGF.cjs.map +1 -0
- package/dist/{chunk-645P6ZAV.js → chunk-N3RNJ7AM.js} +5 -3
- package/dist/chunk-N3RNJ7AM.js.map +1 -0
- package/dist/{chunk-ALMN74MG.cjs → chunk-NRKHBUH6.cjs} +74 -74
- package/dist/{chunk-ALMN74MG.cjs.map → chunk-NRKHBUH6.cjs.map} +1 -1
- package/dist/{chunk-V4IWGD6S.js → chunk-QFX2PGOA.js} +3 -3
- package/dist/{chunk-V4IWGD6S.js.map → chunk-QFX2PGOA.js.map} +1 -1
- package/dist/{chunk-OSQ4FZT2.js → chunk-SDKSW2BQ.js} +572 -31
- package/dist/chunk-SDKSW2BQ.js.map +1 -0
- package/dist/{chunk-PVUR75AM.js → chunk-YUTITKH2.js} +353 -105
- package/dist/chunk-YUTITKH2.js.map +1 -0
- package/dist/docs/SKILL.md +1 -1
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/{observational-memory-UGDENJPE-NVMIXNI4.js → observational-memory-SN7GKMHZ-IWVBFBS6.js} +3 -3
- package/dist/{observational-memory-UGDENJPE-NVMIXNI4.js.map → observational-memory-SN7GKMHZ-IWVBFBS6.js.map} +1 -1
- package/dist/{observational-memory-UGDENJPE-DM3C7ZXI.cjs → observational-memory-SN7GKMHZ-WOK4TGDH.cjs} +26 -26
- package/dist/{observational-memory-UGDENJPE-DM3C7ZXI.cjs.map → observational-memory-SN7GKMHZ-WOK4TGDH.cjs.map} +1 -1
- package/dist/server/handlers/agent-builder.cjs +16 -16
- package/dist/server/handlers/agent-builder.js +1 -1
- package/dist/server/handlers/memory.cjs +27 -27
- package/dist/server/handlers/memory.d.ts +16 -0
- package/dist/server/handlers/memory.d.ts.map +1 -1
- package/dist/server/handlers/memory.js +1 -1
- package/dist/server/handlers.cjs +4 -4
- package/dist/server/handlers.js +2 -2
- package/dist/server/schemas/index.cjs +44 -44
- package/dist/server/schemas/index.js +1 -1
- package/dist/server/schemas/memory.d.ts +8 -0
- package/dist/server/schemas/memory.d.ts.map +1 -1
- package/dist/server/server-adapter/index.cjs +42 -42
- package/dist/server/server-adapter/index.js +2 -2
- package/dist/server/server-adapter/routes/memory.d.ts +16 -0
- package/dist/server/server-adapter/routes/memory.d.ts.map +1 -1
- package/package.json +5 -5
- package/dist/chunk-645P6ZAV.js.map +0 -1
- package/dist/chunk-DJQT32SV.cjs.map +0 -1
- package/dist/chunk-OCSZZ5UK.cjs.map +0 -1
- package/dist/chunk-OSQ4FZT2.js.map +0 -1
- package/dist/chunk-PVUR75AM.js.map +0 -1
- package/dist/chunk-ZBOL3JND.cjs.map +0 -1
|
@@ -9,6 +9,7 @@ var fs = require('fs');
|
|
|
9
9
|
var path = require('path');
|
|
10
10
|
var crypto$1 = require('crypto');
|
|
11
11
|
var agent = require('@mastra/core/agent');
|
|
12
|
+
var observability = require('@mastra/core/observability');
|
|
12
13
|
var async_hooks = require('async_hooks');
|
|
13
14
|
var util = require('util');
|
|
14
15
|
|
|
@@ -1970,7 +1971,8 @@ var ObservationStrategy = class _ObservationStrategy {
|
|
|
1970
1971
|
writer,
|
|
1971
1972
|
abortSignal,
|
|
1972
1973
|
reflectionHooks,
|
|
1973
|
-
requestContext
|
|
1974
|
+
requestContext,
|
|
1975
|
+
observabilityContext: this.opts.observabilityContext
|
|
1974
1976
|
});
|
|
1975
1977
|
}
|
|
1976
1978
|
} catch (error) {
|
|
@@ -2111,6 +2113,27 @@ ${threadClose}`;
|
|
|
2111
2113
|
const boundary = lastObservedAt ? _ObservationStrategy.createMessageBoundary(lastObservedAt) : "\n\n";
|
|
2112
2114
|
return `${existingObservations}${boundary}${newThreadSection}`;
|
|
2113
2115
|
}
|
|
2116
|
+
async indexObservationGroups(observations, threadId, resourceId, observedAt) {
|
|
2117
|
+
if (!resourceId || !this.deps.onIndexObservations) {
|
|
2118
|
+
return;
|
|
2119
|
+
}
|
|
2120
|
+
const groups = parseObservationGroups(observations);
|
|
2121
|
+
if (groups.length === 0) {
|
|
2122
|
+
return;
|
|
2123
|
+
}
|
|
2124
|
+
await Promise.all(
|
|
2125
|
+
groups.map(
|
|
2126
|
+
(group) => this.deps.onIndexObservations({
|
|
2127
|
+
text: group.content,
|
|
2128
|
+
groupId: group.id,
|
|
2129
|
+
range: group.range,
|
|
2130
|
+
threadId,
|
|
2131
|
+
resourceId,
|
|
2132
|
+
observedAt
|
|
2133
|
+
})
|
|
2134
|
+
)
|
|
2135
|
+
);
|
|
2136
|
+
}
|
|
2114
2137
|
// ── Marker persistence ──────────────────────────────────────
|
|
2115
2138
|
/**
|
|
2116
2139
|
* Persist a marker to the last assistant message in storage.
|
|
@@ -2244,6 +2267,7 @@ var SyncObservationStrategy = class extends ObservationStrategy {
|
|
|
2244
2267
|
const omMeta = thread ? memory.getThreadOMMetadata(thread.metadata) : void 0;
|
|
2245
2268
|
const result = await this.deps.observer.call(existingObservations, messages, this.opts.abortSignal, {
|
|
2246
2269
|
requestContext: this.opts.requestContext,
|
|
2270
|
+
observabilityContext: this.opts.observabilityContext,
|
|
2247
2271
|
priorCurrentTask: omMeta?.currentTask,
|
|
2248
2272
|
priorSuggestedResponse: omMeta?.suggestedResponse,
|
|
2249
2273
|
priorThreadTitle: omMeta?.threadTitle
|
|
@@ -2293,7 +2317,7 @@ var SyncObservationStrategy = class extends ObservationStrategy {
|
|
|
2293
2317
|
};
|
|
2294
2318
|
}
|
|
2295
2319
|
async persist(processed) {
|
|
2296
|
-
const { record, threadId, messages } = this.opts;
|
|
2320
|
+
const { record, threadId, resourceId, messages } = this.opts;
|
|
2297
2321
|
const thread = await this.storage.getThreadById({ threadId });
|
|
2298
2322
|
let threadUpdateMarker;
|
|
2299
2323
|
if (thread) {
|
|
@@ -2330,6 +2354,7 @@ var SyncObservationStrategy = class extends ObservationStrategy {
|
|
|
2330
2354
|
lastObservedAt: processed.lastObservedAt,
|
|
2331
2355
|
observedMessageIds: processed.observedMessageIds
|
|
2332
2356
|
});
|
|
2357
|
+
await this.indexObservationGroups(processed.observations, threadId, resourceId, processed.lastObservedAt);
|
|
2333
2358
|
}
|
|
2334
2359
|
async emitEndMarkers(cycleId, processed) {
|
|
2335
2360
|
const actualTokensObserved = await this.tokenCounter.countMessagesAsync(this.opts.messages);
|
|
@@ -2393,7 +2418,8 @@ var AsyncBufferObservationStrategy = class extends ObservationStrategy {
|
|
|
2393
2418
|
async observe(existingObservations, messages) {
|
|
2394
2419
|
return this.deps.observer.call(existingObservations, messages, void 0, {
|
|
2395
2420
|
skipContinuationHints: true,
|
|
2396
|
-
requestContext: this.opts.requestContext
|
|
2421
|
+
requestContext: this.opts.requestContext,
|
|
2422
|
+
observabilityContext: this.opts.observabilityContext
|
|
2397
2423
|
});
|
|
2398
2424
|
}
|
|
2399
2425
|
async process(output, _existingObservations) {
|
|
@@ -2432,7 +2458,7 @@ var AsyncBufferObservationStrategy = class extends ObservationStrategy {
|
|
|
2432
2458
|
}
|
|
2433
2459
|
async persist(processed) {
|
|
2434
2460
|
if (!processed.observations) return;
|
|
2435
|
-
const { record, messages } = this.opts;
|
|
2461
|
+
const { record, threadId, resourceId, messages } = this.opts;
|
|
2436
2462
|
const messageTokens = await this.tokenCounter.countMessagesAsync(messages);
|
|
2437
2463
|
await this.storage.updateBufferedObservations({
|
|
2438
2464
|
id: record.id,
|
|
@@ -2449,6 +2475,7 @@ var AsyncBufferObservationStrategy = class extends ObservationStrategy {
|
|
|
2449
2475
|
},
|
|
2450
2476
|
lastBufferedAtTime: processed.lastObservedAt
|
|
2451
2477
|
});
|
|
2478
|
+
await this.indexObservationGroups(processed.observations, threadId, resourceId, processed.lastObservedAt);
|
|
2452
2479
|
}
|
|
2453
2480
|
async emitEndMarkers(_cycleId, processed) {
|
|
2454
2481
|
if (!processed.observations || !this.opts.writer) return;
|
|
@@ -2663,7 +2690,8 @@ var ResourceScopedObservationStrategy = class extends ObservationStrategy {
|
|
|
2663
2690
|
batch.threadIds,
|
|
2664
2691
|
this.opts.abortSignal,
|
|
2665
2692
|
this.opts.requestContext,
|
|
2666
|
-
this.priorMetadataByThread
|
|
2693
|
+
this.priorMetadataByThread,
|
|
2694
|
+
this.opts.observabilityContext
|
|
2667
2695
|
);
|
|
2668
2696
|
})
|
|
2669
2697
|
);
|
|
@@ -2747,7 +2775,7 @@ var ResourceScopedObservationStrategy = class extends ObservationStrategy {
|
|
|
2747
2775
|
};
|
|
2748
2776
|
}
|
|
2749
2777
|
async persist(processed) {
|
|
2750
|
-
const { record } = this.opts;
|
|
2778
|
+
const { record, resourceId } = this.opts;
|
|
2751
2779
|
const threadUpdateMarkers = [];
|
|
2752
2780
|
if (processed.threadMetadataUpdates) {
|
|
2753
2781
|
for (const update of processed.threadMetadataUpdates) {
|
|
@@ -2791,6 +2819,18 @@ var ResourceScopedObservationStrategy = class extends ObservationStrategy {
|
|
|
2791
2819
|
lastObservedAt: processed.lastObservedAt,
|
|
2792
2820
|
observedMessageIds: processed.observedMessageIds
|
|
2793
2821
|
});
|
|
2822
|
+
if (resourceId) {
|
|
2823
|
+
await Promise.all(
|
|
2824
|
+
this.observationResults.map(
|
|
2825
|
+
({ threadId, threadMessages, result }) => this.indexObservationGroups(
|
|
2826
|
+
result.observations,
|
|
2827
|
+
threadId,
|
|
2828
|
+
resourceId,
|
|
2829
|
+
this.getMaxMessageTimestamp(threadMessages)
|
|
2830
|
+
)
|
|
2831
|
+
)
|
|
2832
|
+
);
|
|
2833
|
+
}
|
|
2794
2834
|
}
|
|
2795
2835
|
async emitEndMarkers(cycleId, processed) {
|
|
2796
2836
|
for (const obsResult of this.observationResults) {
|
|
@@ -2846,6 +2886,7 @@ ObservationStrategy.create = ((om, opts) => {
|
|
|
2846
2886
|
reflector: om.reflector,
|
|
2847
2887
|
observedMessageIds: om.observedMessageIds,
|
|
2848
2888
|
obscureThreadIds: om.getObscureThreadIds(),
|
|
2889
|
+
onIndexObservations: om.onIndexObservations,
|
|
2849
2890
|
emitDebugEvent: (e2) => om.emitDebugEvent(e2)
|
|
2850
2891
|
};
|
|
2851
2892
|
if (opts.cycleId) return new AsyncBufferObservationStrategy(deps, opts);
|
|
@@ -2914,7 +2955,8 @@ var ObservationStep = class {
|
|
|
2914
2955
|
observationTokens: obsTokens,
|
|
2915
2956
|
threadId,
|
|
2916
2957
|
writer: this.turn.writer,
|
|
2917
|
-
requestContext: this.turn.requestContext
|
|
2958
|
+
requestContext: this.turn.requestContext,
|
|
2959
|
+
observabilityContext: this.turn.observabilityContext
|
|
2918
2960
|
});
|
|
2919
2961
|
await this.turn.refreshRecord();
|
|
2920
2962
|
if (this.turn.record.generationCount > preReflectGeneration) {
|
|
@@ -2946,6 +2988,7 @@ var ObservationStep = class {
|
|
|
2946
2988
|
record: statusSnapshot.record,
|
|
2947
2989
|
writer: this.turn.writer,
|
|
2948
2990
|
requestContext: this.turn.requestContext,
|
|
2991
|
+
observabilityContext: this.turn.observabilityContext,
|
|
2949
2992
|
beforeBuffer: async (candidates) => {
|
|
2950
2993
|
if (candidates.length === 0) {
|
|
2951
2994
|
return;
|
|
@@ -3082,7 +3125,8 @@ var ObservationStep = class {
|
|
|
3082
3125
|
threadId,
|
|
3083
3126
|
writer: this.turn.writer,
|
|
3084
3127
|
messageList,
|
|
3085
|
-
requestContext: this.turn.requestContext
|
|
3128
|
+
requestContext: this.turn.requestContext,
|
|
3129
|
+
observabilityContext: this.turn.observabilityContext
|
|
3086
3130
|
});
|
|
3087
3131
|
return {
|
|
3088
3132
|
succeeded: true,
|
|
@@ -3096,7 +3140,8 @@ var ObservationStep = class {
|
|
|
3096
3140
|
resourceId,
|
|
3097
3141
|
messages: messageList.get.all.db(),
|
|
3098
3142
|
requestContext: this.turn.requestContext,
|
|
3099
|
-
writer: this.turn.writer
|
|
3143
|
+
writer: this.turn.writer,
|
|
3144
|
+
observabilityContext: this.turn.observabilityContext
|
|
3100
3145
|
});
|
|
3101
3146
|
return { succeeded: obsResult.observed, record: obsResult.record };
|
|
3102
3147
|
}
|
|
@@ -3115,6 +3160,8 @@ var ObservationTurn = class {
|
|
|
3115
3160
|
writer;
|
|
3116
3161
|
/** Optional request context for observation calls. */
|
|
3117
3162
|
requestContext;
|
|
3163
|
+
/** Optional observability context for nested OM spans. */
|
|
3164
|
+
observabilityContext;
|
|
3118
3165
|
/** Optional processor-provided hooks for turn/step lifecycle integration. */
|
|
3119
3166
|
hooks;
|
|
3120
3167
|
constructor(opts) {
|
|
@@ -3122,6 +3169,7 @@ var ObservationTurn = class {
|
|
|
3122
3169
|
this.threadId = opts.threadId;
|
|
3123
3170
|
this.resourceId = opts.resourceId;
|
|
3124
3171
|
this.messageList = opts.messageList;
|
|
3172
|
+
this.observabilityContext = opts.observabilityContext;
|
|
3125
3173
|
this.hooks = opts.hooks;
|
|
3126
3174
|
}
|
|
3127
3175
|
om;
|
|
@@ -4362,13 +4410,62 @@ function optimizeObservationsForContext(observations) {
|
|
|
4362
4410
|
optimized = optimized.replace(/\n{3,}/g, "\n\n");
|
|
4363
4411
|
return optimized.trim();
|
|
4364
4412
|
}
|
|
4413
|
+
var PHASE_CONFIG = {
|
|
4414
|
+
observer: {
|
|
4415
|
+
name: "om.observer",
|
|
4416
|
+
entityName: "Observer"
|
|
4417
|
+
},
|
|
4418
|
+
"observer-multi-thread": {
|
|
4419
|
+
name: "om.observer.multi-thread",
|
|
4420
|
+
entityName: "MultiThreadObserver"
|
|
4421
|
+
},
|
|
4422
|
+
reflector: {
|
|
4423
|
+
name: "om.reflector",
|
|
4424
|
+
entityName: "Reflector"
|
|
4425
|
+
}
|
|
4426
|
+
};
|
|
4427
|
+
async function withOmTracingSpan({
|
|
4428
|
+
phase,
|
|
4429
|
+
model,
|
|
4430
|
+
inputTokens,
|
|
4431
|
+
requestContext,
|
|
4432
|
+
observabilityContext,
|
|
4433
|
+
metadata,
|
|
4434
|
+
callback
|
|
4435
|
+
}) {
|
|
4436
|
+
const config = PHASE_CONFIG[phase];
|
|
4437
|
+
const span = observability.getOrCreateSpan({
|
|
4438
|
+
type: observability.SpanType.GENERIC,
|
|
4439
|
+
name: config.name,
|
|
4440
|
+
entityType: observability.EntityType.OUTPUT_STEP_PROCESSOR,
|
|
4441
|
+
entityName: config.entityName,
|
|
4442
|
+
tracingContext: observabilityContext?.tracingContext ?? observabilityContext?.tracing,
|
|
4443
|
+
attributes: {
|
|
4444
|
+
metadata: {
|
|
4445
|
+
omPhase: phase,
|
|
4446
|
+
omInputTokens: inputTokens,
|
|
4447
|
+
omSelectedModel: typeof model === "string" ? model : "(dynamic-model)",
|
|
4448
|
+
...metadata
|
|
4449
|
+
}
|
|
4450
|
+
},
|
|
4451
|
+
requestContext
|
|
4452
|
+
});
|
|
4453
|
+
const childObservabilityContext = observability.createObservabilityContext({ currentSpan: span });
|
|
4454
|
+
if (!span) {
|
|
4455
|
+
return callback(childObservabilityContext);
|
|
4456
|
+
}
|
|
4457
|
+
return span.executeInContext(() => callback(childObservabilityContext));
|
|
4458
|
+
}
|
|
4365
4459
|
var ObserverRunner = class {
|
|
4366
4460
|
observationConfig;
|
|
4367
4461
|
observedMessageIds;
|
|
4368
|
-
|
|
4462
|
+
resolveModel;
|
|
4463
|
+
tokenCounter;
|
|
4369
4464
|
constructor(opts) {
|
|
4370
4465
|
this.observationConfig = opts.observationConfig;
|
|
4371
4466
|
this.observedMessageIds = opts.observedMessageIds;
|
|
4467
|
+
this.resolveModel = opts.resolveModel;
|
|
4468
|
+
this.tokenCounter = opts.tokenCounter;
|
|
4372
4469
|
}
|
|
4373
4470
|
createAgent(model, isMultiThread = false) {
|
|
4374
4471
|
return new agent.Agent({
|
|
@@ -4382,10 +4479,6 @@ var ObserverRunner = class {
|
|
|
4382
4479
|
model
|
|
4383
4480
|
});
|
|
4384
4481
|
}
|
|
4385
|
-
getAgent(model) {
|
|
4386
|
-
this.observerAgent ??= this.createAgent(model);
|
|
4387
|
-
return this.observerAgent;
|
|
4388
|
-
}
|
|
4389
4482
|
async withAbortCheck(fn, abortSignal) {
|
|
4390
4483
|
if (abortSignal?.aborted) {
|
|
4391
4484
|
throw new Error("The operation was aborted.");
|
|
@@ -4400,7 +4493,9 @@ var ObserverRunner = class {
|
|
|
4400
4493
|
* Call the Observer agent for a single thread.
|
|
4401
4494
|
*/
|
|
4402
4495
|
async call(existingObservations, messagesToObserve, abortSignal, options) {
|
|
4403
|
-
const
|
|
4496
|
+
const inputTokens = this.tokenCounter.countMessages(messagesToObserve);
|
|
4497
|
+
const resolvedModel = options?.model ? { model: options.model } : this.resolveModel(inputTokens);
|
|
4498
|
+
const agent = this.createAgent(resolvedModel.model);
|
|
4404
4499
|
const observerMessages = [
|
|
4405
4500
|
{
|
|
4406
4501
|
role: "user",
|
|
@@ -4412,15 +4507,32 @@ var ObserverRunner = class {
|
|
|
4412
4507
|
buildObserverHistoryMessage(messagesToObserve)
|
|
4413
4508
|
];
|
|
4414
4509
|
const doGenerate = async () => {
|
|
4415
|
-
return
|
|
4416
|
-
|
|
4417
|
-
|
|
4418
|
-
|
|
4419
|
-
|
|
4420
|
-
|
|
4421
|
-
|
|
4422
|
-
|
|
4423
|
-
|
|
4510
|
+
return withOmTracingSpan({
|
|
4511
|
+
phase: "observer",
|
|
4512
|
+
model: resolvedModel.model,
|
|
4513
|
+
inputTokens,
|
|
4514
|
+
requestContext: options?.requestContext,
|
|
4515
|
+
observabilityContext: options?.observabilityContext,
|
|
4516
|
+
metadata: {
|
|
4517
|
+
omPreviousObserverTokens: this.observationConfig.previousObserverTokens,
|
|
4518
|
+
omThreadTitleEnabled: this.observationConfig.threadTitle,
|
|
4519
|
+
omSkipContinuationHints: options?.skipContinuationHints ?? false,
|
|
4520
|
+
omWasTruncated: options?.wasTruncated ?? false,
|
|
4521
|
+
...resolvedModel.selectedThreshold !== void 0 ? { omSelectedThreshold: resolvedModel.selectedThreshold } : {},
|
|
4522
|
+
...resolvedModel.routingStrategy ? { omRoutingStrategy: resolvedModel.routingStrategy } : {},
|
|
4523
|
+
...resolvedModel.routingThresholds ? { omRoutingThresholds: resolvedModel.routingThresholds } : {}
|
|
4524
|
+
},
|
|
4525
|
+
callback: (childObservabilityContext) => this.withAbortCheck(async () => {
|
|
4526
|
+
const streamResult = await agent.stream(observerMessages, {
|
|
4527
|
+
modelSettings: { ...this.observationConfig.modelSettings },
|
|
4528
|
+
providerOptions: this.observationConfig.providerOptions,
|
|
4529
|
+
...abortSignal ? { abortSignal } : {},
|
|
4530
|
+
...options?.requestContext ? { requestContext: options.requestContext } : {},
|
|
4531
|
+
...childObservabilityContext
|
|
4532
|
+
});
|
|
4533
|
+
return streamResult.getFullOutput();
|
|
4534
|
+
}, abortSignal)
|
|
4535
|
+
});
|
|
4424
4536
|
};
|
|
4425
4537
|
let result = await doGenerate();
|
|
4426
4538
|
let parsed = parseObserverOutput(result.text);
|
|
@@ -4445,8 +4557,13 @@ var ObserverRunner = class {
|
|
|
4445
4557
|
/**
|
|
4446
4558
|
* Call the Observer agent for multiple threads in a single batched request.
|
|
4447
4559
|
*/
|
|
4448
|
-
async callMultiThread(existingObservations, messagesByThread, threadOrder, abortSignal, requestContext, priorMetadataByThread, model) {
|
|
4449
|
-
const
|
|
4560
|
+
async callMultiThread(existingObservations, messagesByThread, threadOrder, abortSignal, requestContext, priorMetadataByThread, observabilityContext, model) {
|
|
4561
|
+
const inputTokens = Array.from(messagesByThread.values()).reduce(
|
|
4562
|
+
(total, messages) => total + this.tokenCounter.countMessages(messages),
|
|
4563
|
+
0
|
|
4564
|
+
);
|
|
4565
|
+
const resolvedModel = model ? { model } : this.resolveModel(inputTokens);
|
|
4566
|
+
const agent = this.createAgent(resolvedModel.model, true);
|
|
4450
4567
|
const observerMessages = [
|
|
4451
4568
|
{
|
|
4452
4569
|
role: "user",
|
|
@@ -4466,15 +4583,31 @@ var ObserverRunner = class {
|
|
|
4466
4583
|
}
|
|
4467
4584
|
}
|
|
4468
4585
|
const doGenerate = async () => {
|
|
4469
|
-
return
|
|
4470
|
-
|
|
4471
|
-
|
|
4472
|
-
|
|
4473
|
-
|
|
4474
|
-
|
|
4475
|
-
|
|
4476
|
-
|
|
4477
|
-
|
|
4586
|
+
return withOmTracingSpan({
|
|
4587
|
+
phase: "observer-multi-thread",
|
|
4588
|
+
model: resolvedModel.model,
|
|
4589
|
+
inputTokens,
|
|
4590
|
+
requestContext,
|
|
4591
|
+
observabilityContext,
|
|
4592
|
+
metadata: {
|
|
4593
|
+
omThreadCount: threadOrder.length,
|
|
4594
|
+
omPreviousObserverTokens: this.observationConfig.previousObserverTokens,
|
|
4595
|
+
omThreadTitleEnabled: this.observationConfig.threadTitle,
|
|
4596
|
+
...resolvedModel.selectedThreshold !== void 0 ? { omSelectedThreshold: resolvedModel.selectedThreshold } : {},
|
|
4597
|
+
...resolvedModel.routingStrategy ? { omRoutingStrategy: resolvedModel.routingStrategy } : {},
|
|
4598
|
+
...resolvedModel.routingThresholds ? { omRoutingThresholds: resolvedModel.routingThresholds } : {}
|
|
4599
|
+
},
|
|
4600
|
+
callback: (childObservabilityContext) => this.withAbortCheck(async () => {
|
|
4601
|
+
const streamResult = await agent.stream(observerMessages, {
|
|
4602
|
+
modelSettings: { ...this.observationConfig.modelSettings },
|
|
4603
|
+
providerOptions: this.observationConfig.providerOptions,
|
|
4604
|
+
...abortSignal ? { abortSignal } : {},
|
|
4605
|
+
...requestContext ? { requestContext } : {},
|
|
4606
|
+
...childObservabilityContext
|
|
4607
|
+
});
|
|
4608
|
+
return streamResult.getFullOutput();
|
|
4609
|
+
}, abortSignal)
|
|
4610
|
+
});
|
|
4478
4611
|
};
|
|
4479
4612
|
let result = await doGenerate();
|
|
4480
4613
|
let parsed = parseMultiThreadObserverOutput(result.text);
|
|
@@ -4777,6 +4910,7 @@ var ReflectorRunner = class {
|
|
|
4777
4910
|
reflectionConfig;
|
|
4778
4911
|
observationConfig;
|
|
4779
4912
|
tokenCounter;
|
|
4913
|
+
resolveModel;
|
|
4780
4914
|
storage;
|
|
4781
4915
|
scope;
|
|
4782
4916
|
buffering;
|
|
@@ -4788,6 +4922,7 @@ var ReflectorRunner = class {
|
|
|
4788
4922
|
this.reflectionConfig = opts.reflectionConfig;
|
|
4789
4923
|
this.observationConfig = opts.observationConfig;
|
|
4790
4924
|
this.tokenCounter = opts.tokenCounter;
|
|
4925
|
+
this.resolveModel = opts.resolveModel;
|
|
4791
4926
|
this.storage = opts.storage;
|
|
4792
4927
|
this.scope = opts.scope;
|
|
4793
4928
|
this.buffering = opts.buffering;
|
|
@@ -4814,9 +4949,10 @@ var ReflectorRunner = class {
|
|
|
4814
4949
|
/**
|
|
4815
4950
|
* Call the Reflector agent with escalating compression levels.
|
|
4816
4951
|
*/
|
|
4817
|
-
async call(observations, manualPrompt, streamContext, observationTokensThreshold, abortSignal, skipContinuationHints, compressionStartLevel, requestContext, model) {
|
|
4818
|
-
const agent = this.createAgent(model ?? this.reflectionConfig.model);
|
|
4952
|
+
async call(observations, manualPrompt, streamContext, observationTokensThreshold, abortSignal, skipContinuationHints, compressionStartLevel, requestContext, observabilityContext, model) {
|
|
4819
4953
|
const originalTokens = this.tokenCounter.countObservations(observations);
|
|
4954
|
+
const resolvedModel = model ? { model } : this.resolveModel(originalTokens);
|
|
4955
|
+
const agent = this.createAgent(resolvedModel.model);
|
|
4820
4956
|
const targetThreshold = observationTokensThreshold ?? getMaxThreshold(this.reflectionConfig.observationTokens);
|
|
4821
4957
|
let totalUsage = { inputTokens: 0, outputTokens: 0, totalTokens: 0 };
|
|
4822
4958
|
const startLevel = compressionStartLevel ?? 0;
|
|
@@ -4833,37 +4969,54 @@ var ReflectorRunner = class {
|
|
|
4833
4969
|
`[OM:callReflector] ${isRetry ? `retry #${attemptNumber - 1}` : "first attempt"}: level=${currentLevel}, originalTokens=${originalTokens}, targetThreshold=${targetThreshold}, promptLen=${prompt.length}, skipContinuationHints=${skipContinuationHints}`
|
|
4834
4970
|
);
|
|
4835
4971
|
let chunkCount = 0;
|
|
4836
|
-
const result = await
|
|
4837
|
-
|
|
4838
|
-
|
|
4839
|
-
|
|
4840
|
-
|
|
4841
|
-
|
|
4842
|
-
|
|
4843
|
-
|
|
4844
|
-
|
|
4845
|
-
|
|
4846
|
-
|
|
4847
|
-
|
|
4848
|
-
|
|
4849
|
-
|
|
4850
|
-
|
|
4851
|
-
|
|
4852
|
-
|
|
4853
|
-
|
|
4854
|
-
|
|
4855
|
-
);
|
|
4856
|
-
},
|
|
4857
|
-
onAbort(event) {
|
|
4858
|
-
omDebug(`[OM:callReflector] onAbort: chunks=${chunkCount}, reason=${event?.reason ?? "unknown"}`);
|
|
4972
|
+
const result = await withOmTracingSpan({
|
|
4973
|
+
phase: "reflector",
|
|
4974
|
+
model: resolvedModel.model,
|
|
4975
|
+
inputTokens: originalTokens,
|
|
4976
|
+
requestContext,
|
|
4977
|
+
observabilityContext,
|
|
4978
|
+
metadata: {
|
|
4979
|
+
omCompressionLevel: currentLevel,
|
|
4980
|
+
omCompressionAttempt: attemptNumber,
|
|
4981
|
+
omTargetThreshold: targetThreshold,
|
|
4982
|
+
omSkipContinuationHints: skipContinuationHints ?? false,
|
|
4983
|
+
...resolvedModel.selectedThreshold !== void 0 ? { omSelectedThreshold: resolvedModel.selectedThreshold } : {},
|
|
4984
|
+
...resolvedModel.routingStrategy ? { omRoutingStrategy: resolvedModel.routingStrategy } : {},
|
|
4985
|
+
...resolvedModel.routingThresholds ? { omRoutingThresholds: resolvedModel.routingThresholds } : {}
|
|
4986
|
+
},
|
|
4987
|
+
callback: (childObservabilityContext) => withAbortCheck(async () => {
|
|
4988
|
+
const streamResult = await agent.stream(prompt, {
|
|
4989
|
+
modelSettings: {
|
|
4990
|
+
...this.reflectionConfig.modelSettings
|
|
4859
4991
|
},
|
|
4860
|
-
|
|
4861
|
-
|
|
4862
|
-
}
|
|
4863
|
-
|
|
4864
|
-
|
|
4865
|
-
|
|
4866
|
-
|
|
4992
|
+
providerOptions: this.reflectionConfig.providerOptions,
|
|
4993
|
+
...abortSignal ? { abortSignal } : {},
|
|
4994
|
+
...requestContext ? { requestContext } : {},
|
|
4995
|
+
...childObservabilityContext,
|
|
4996
|
+
...attemptNumber === 1 ? {
|
|
4997
|
+
onChunk(chunk) {
|
|
4998
|
+
chunkCount++;
|
|
4999
|
+
if (chunkCount === 1 || chunkCount % 50 === 0) {
|
|
5000
|
+
const preview = chunk.type === "text-delta" ? ` text="${chunk.textDelta?.slice(0, 80)}..."` : chunk.type === "tool-call" ? ` tool=${chunk.toolName}` : "";
|
|
5001
|
+
omDebug(`[OM:callReflector] chunk#${chunkCount}: type=${chunk.type}${preview}`);
|
|
5002
|
+
}
|
|
5003
|
+
},
|
|
5004
|
+
onFinish(event) {
|
|
5005
|
+
omDebug(
|
|
5006
|
+
`[OM:callReflector] onFinish: chunks=${chunkCount}, finishReason=${event.finishReason}, inputTokens=${event.usage?.inputTokens}, outputTokens=${event.usage?.outputTokens}, textLen=${event.text?.length}`
|
|
5007
|
+
);
|
|
5008
|
+
},
|
|
5009
|
+
onAbort(event) {
|
|
5010
|
+
omDebug(`[OM:callReflector] onAbort: chunks=${chunkCount}, reason=${event?.reason ?? "unknown"}`);
|
|
5011
|
+
},
|
|
5012
|
+
onError({ error }) {
|
|
5013
|
+
omError(`[OM:callReflector] onError after ${chunkCount} chunks`, error);
|
|
5014
|
+
}
|
|
5015
|
+
} : {}
|
|
5016
|
+
});
|
|
5017
|
+
return streamResult.getFullOutput();
|
|
5018
|
+
}, abortSignal)
|
|
5019
|
+
});
|
|
4867
5020
|
omDebug(
|
|
4868
5021
|
`[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}`
|
|
4869
5022
|
);
|
|
@@ -4930,7 +5083,7 @@ var ReflectorRunner = class {
|
|
|
4930
5083
|
/**
|
|
4931
5084
|
* Start an async buffered reflection in the background.
|
|
4932
5085
|
*/
|
|
4933
|
-
startAsyncBufferedReflection(record, observationTokens, lockKey, writer, requestContext) {
|
|
5086
|
+
startAsyncBufferedReflection(record, observationTokens, lockKey, writer, requestContext, observabilityContext) {
|
|
4934
5087
|
const bufferKey = this.buffering.getReflectionBufferKey(lockKey);
|
|
4935
5088
|
if (this.buffering.isAsyncBufferingInProgress(bufferKey)) {
|
|
4936
5089
|
return;
|
|
@@ -4940,7 +5093,7 @@ var ReflectorRunner = class {
|
|
|
4940
5093
|
this.storage.setBufferingReflectionFlag(record.id, true).catch((err) => {
|
|
4941
5094
|
omError("[OM] Failed to set buffering reflection flag", err);
|
|
4942
5095
|
});
|
|
4943
|
-
const asyncOp = this.doAsyncBufferedReflection(record, bufferKey, writer, requestContext).catch(async (error) => {
|
|
5096
|
+
const asyncOp = this.doAsyncBufferedReflection(record, bufferKey, writer, requestContext, observabilityContext).catch(async (error) => {
|
|
4944
5097
|
if (writer) {
|
|
4945
5098
|
const failedMarker = createBufferingFailedMarker({
|
|
4946
5099
|
cycleId: `reflect-buf-${Date.now()}-${Math.random().toString(36).slice(2, 11)}`,
|
|
@@ -4970,7 +5123,7 @@ var ReflectorRunner = class {
|
|
|
4970
5123
|
* Perform async buffered reflection — reflects observations and stores to bufferedReflection.
|
|
4971
5124
|
* Does NOT create a new generation or update activeObservations.
|
|
4972
5125
|
*/
|
|
4973
|
-
async doAsyncBufferedReflection(record, _bufferKey, writer, requestContext) {
|
|
5126
|
+
async doAsyncBufferedReflection(record, _bufferKey, writer, requestContext, observabilityContext) {
|
|
4974
5127
|
const freshRecord = await this.storage.getObservationalMemory(record.threadId, record.resourceId);
|
|
4975
5128
|
const currentRecord = freshRecord ?? record;
|
|
4976
5129
|
const observationTokens = currentRecord.observationTokenCount ?? 0;
|
|
@@ -5017,7 +5170,8 @@ var ReflectorRunner = class {
|
|
|
5017
5170
|
void 0,
|
|
5018
5171
|
true,
|
|
5019
5172
|
compressionStartLevel,
|
|
5020
|
-
requestContext
|
|
5173
|
+
requestContext,
|
|
5174
|
+
observabilityContext
|
|
5021
5175
|
);
|
|
5022
5176
|
const reflectionTokenCount = this.tokenCounter.countObservations(reflectResult.observations);
|
|
5023
5177
|
omDebug(
|
|
@@ -5133,7 +5287,16 @@ ${unreflectedContent}` : freshRecord.bufferedReflection;
|
|
|
5133
5287
|
* @internal Used by observation strategies. Do not call directly.
|
|
5134
5288
|
*/
|
|
5135
5289
|
async maybeReflect(opts) {
|
|
5136
|
-
const {
|
|
5290
|
+
const {
|
|
5291
|
+
record,
|
|
5292
|
+
observationTokens,
|
|
5293
|
+
writer,
|
|
5294
|
+
abortSignal,
|
|
5295
|
+
messageList,
|
|
5296
|
+
reflectionHooks,
|
|
5297
|
+
requestContext,
|
|
5298
|
+
observabilityContext
|
|
5299
|
+
} = opts;
|
|
5137
5300
|
const lockKey = this.buffering.getLockKey(record.threadId, record.resourceId);
|
|
5138
5301
|
const reflectThreshold = getMaxThreshold(this.reflectionConfig.observationTokens);
|
|
5139
5302
|
if (this.buffering.isAsyncReflectionEnabled() && observationTokens < reflectThreshold) {
|
|
@@ -5153,7 +5316,14 @@ ${unreflectedContent}` : freshRecord.bufferedReflection;
|
|
|
5153
5316
|
return observationTokens >= activationPoint;
|
|
5154
5317
|
})();
|
|
5155
5318
|
if (shouldTrigger) {
|
|
5156
|
-
this.startAsyncBufferedReflection(
|
|
5319
|
+
this.startAsyncBufferedReflection(
|
|
5320
|
+
record,
|
|
5321
|
+
observationTokens,
|
|
5322
|
+
lockKey,
|
|
5323
|
+
writer,
|
|
5324
|
+
requestContext,
|
|
5325
|
+
observabilityContext
|
|
5326
|
+
);
|
|
5157
5327
|
}
|
|
5158
5328
|
}
|
|
5159
5329
|
if (observationTokens < reflectThreshold) {
|
|
@@ -5180,7 +5350,14 @@ ${unreflectedContent}` : freshRecord.bufferedReflection;
|
|
|
5180
5350
|
omDebug(
|
|
5181
5351
|
`[OM:reflect] async activation failed, no blockAfter or below it (obsTokens=${observationTokens}, blockAfter=${this.reflectionConfig.blockAfter}) \u2014 starting background reflection`
|
|
5182
5352
|
);
|
|
5183
|
-
this.startAsyncBufferedReflection(
|
|
5353
|
+
this.startAsyncBufferedReflection(
|
|
5354
|
+
record,
|
|
5355
|
+
observationTokens,
|
|
5356
|
+
lockKey,
|
|
5357
|
+
writer,
|
|
5358
|
+
requestContext,
|
|
5359
|
+
observabilityContext
|
|
5360
|
+
);
|
|
5184
5361
|
return;
|
|
5185
5362
|
}
|
|
5186
5363
|
}
|
|
@@ -5228,7 +5405,8 @@ ${unreflectedContent}` : freshRecord.bufferedReflection;
|
|
|
5228
5405
|
abortSignal,
|
|
5229
5406
|
void 0,
|
|
5230
5407
|
compressionStartLevel,
|
|
5231
|
-
requestContext
|
|
5408
|
+
requestContext,
|
|
5409
|
+
observabilityContext
|
|
5232
5410
|
);
|
|
5233
5411
|
const reflectionTokenCount = this.tokenCounter.countObservations(reflectResult.observations);
|
|
5234
5412
|
await this.storage.createReflectionGeneration({
|
|
@@ -6514,11 +6692,12 @@ var ObservationalMemory = class _ObservationalMemory {
|
|
|
6514
6692
|
storage;
|
|
6515
6693
|
tokenCounter;
|
|
6516
6694
|
scope;
|
|
6517
|
-
/** Whether retrieval-mode observation groups are enabled
|
|
6695
|
+
/** Whether retrieval-mode observation groups are enabled. */
|
|
6518
6696
|
retrieval;
|
|
6519
6697
|
observationConfig;
|
|
6520
6698
|
reflectionConfig;
|
|
6521
6699
|
onDebugEvent;
|
|
6700
|
+
onIndexObservations;
|
|
6522
6701
|
/** Observer agent runner — handles LLM calls for extracting observations. */
|
|
6523
6702
|
observer;
|
|
6524
6703
|
/** Reflector agent runner — handles LLM calls for compressing observations. */
|
|
@@ -6591,7 +6770,8 @@ var ObservationalMemory = class _ObservationalMemory {
|
|
|
6591
6770
|
this.shouldObscureThreadIds = config.obscureThreadIds || false;
|
|
6592
6771
|
this.storage = config.storage;
|
|
6593
6772
|
this.scope = config.scope ?? "thread";
|
|
6594
|
-
this.retrieval =
|
|
6773
|
+
this.retrieval = Boolean(config.retrieval);
|
|
6774
|
+
this.onIndexObservations = config.onIndexObservations;
|
|
6595
6775
|
const resolveModel = (m) => m === "default" ? chunk3D55EABW_cjs.OBSERVATIONAL_MEMORY_DEFAULTS.observation.model : m;
|
|
6596
6776
|
const observationModel = resolveModel(config.model) ?? resolveModel(config.observation?.model) ?? resolveModel(config.reflection?.model);
|
|
6597
6777
|
const reflectionModel = resolveModel(config.model) ?? resolveModel(config.reflection?.model) ?? resolveModel(config.observation?.model);
|
|
@@ -6685,7 +6865,9 @@ Async buffering is enabled by default \u2014 this opt-out is only needed when us
|
|
|
6685
6865
|
this.messageHistory = new processors.MessageHistory({ storage: this.storage });
|
|
6686
6866
|
this.observer = new ObserverRunner({
|
|
6687
6867
|
observationConfig: this.observationConfig,
|
|
6688
|
-
observedMessageIds: this.observedMessageIds
|
|
6868
|
+
observedMessageIds: this.observedMessageIds,
|
|
6869
|
+
resolveModel: (inputTokens) => this.resolveObservationModel(inputTokens),
|
|
6870
|
+
tokenCounter: this.tokenCounter
|
|
6689
6871
|
});
|
|
6690
6872
|
this.buffering = new BufferingCoordinator({
|
|
6691
6873
|
observationConfig: this.observationConfig,
|
|
@@ -6702,7 +6884,8 @@ Async buffering is enabled by default \u2014 this opt-out is only needed when us
|
|
|
6702
6884
|
emitDebugEvent: (e2) => this.emitDebugEvent(e2),
|
|
6703
6885
|
persistMarkerToStorage: (m, t2, r) => this.persistMarkerToStorage(m, t2, r),
|
|
6704
6886
|
persistMarkerToMessage: (m, ml, t2, r) => this.persistMarkerToMessage(m, ml, t2, r),
|
|
6705
|
-
getCompressionStartLevel: (rc) => this.getCompressionStartLevel(rc)
|
|
6887
|
+
getCompressionStartLevel: (rc) => this.getCompressionStartLevel(rc),
|
|
6888
|
+
resolveModel: (inputTokens) => this.resolveReflectionModel(inputTokens)
|
|
6706
6889
|
});
|
|
6707
6890
|
this.validateBufferConfig();
|
|
6708
6891
|
omDebug(
|
|
@@ -6764,6 +6947,54 @@ Async buffering is enabled by default \u2014 this opt-out is only needed when us
|
|
|
6764
6947
|
}
|
|
6765
6948
|
return model.provider ? `${model.provider}/${model.modelId}` : model.modelId;
|
|
6766
6949
|
}
|
|
6950
|
+
resolveObservationModel(inputTokens) {
|
|
6951
|
+
return this.resolveTieredModel(this.observationConfig.model, inputTokens);
|
|
6952
|
+
}
|
|
6953
|
+
resolveReflectionModel(inputTokens) {
|
|
6954
|
+
return this.resolveTieredModel(this.reflectionConfig.model, inputTokens);
|
|
6955
|
+
}
|
|
6956
|
+
resolveTieredModel(model, inputTokens) {
|
|
6957
|
+
if (!(model instanceof ModelByInputTokens)) {
|
|
6958
|
+
return {
|
|
6959
|
+
model
|
|
6960
|
+
};
|
|
6961
|
+
}
|
|
6962
|
+
const thresholds = model.getThresholds();
|
|
6963
|
+
const selectedThreshold = thresholds.find((upTo) => inputTokens <= upTo) ?? thresholds.at(-1);
|
|
6964
|
+
return {
|
|
6965
|
+
model: model.resolve(inputTokens),
|
|
6966
|
+
selectedThreshold,
|
|
6967
|
+
routingStrategy: "model-by-input-tokens",
|
|
6968
|
+
routingThresholds: thresholds.join(",")
|
|
6969
|
+
};
|
|
6970
|
+
}
|
|
6971
|
+
async resolveModelRouting(modelConfig, requestContext) {
|
|
6972
|
+
try {
|
|
6973
|
+
if (modelConfig instanceof ModelByInputTokens) {
|
|
6974
|
+
const routing = await Promise.all(
|
|
6975
|
+
modelConfig.getThresholds().map(async (upTo) => {
|
|
6976
|
+
const resolvedModel = modelConfig.resolve(upTo);
|
|
6977
|
+
const resolved2 = await this.resolveModelContext(resolvedModel, requestContext);
|
|
6978
|
+
return {
|
|
6979
|
+
upTo,
|
|
6980
|
+
model: resolved2?.modelId ? this.formatModelName(resolved2) : "(unknown)"
|
|
6981
|
+
};
|
|
6982
|
+
})
|
|
6983
|
+
);
|
|
6984
|
+
return {
|
|
6985
|
+
model: routing[0]?.model ?? "(unknown)",
|
|
6986
|
+
routing
|
|
6987
|
+
};
|
|
6988
|
+
}
|
|
6989
|
+
const resolved = await this.resolveModelContext(modelConfig, requestContext);
|
|
6990
|
+
return {
|
|
6991
|
+
model: resolved?.modelId ? this.formatModelName(resolved) : "(unknown)"
|
|
6992
|
+
};
|
|
6993
|
+
} catch (error) {
|
|
6994
|
+
omError("[OM] Failed to resolve model config", error);
|
|
6995
|
+
return { model: "(unknown)" };
|
|
6996
|
+
}
|
|
6997
|
+
}
|
|
6767
6998
|
async resolveModelContext(modelConfig, requestContext, inputTokens) {
|
|
6768
6999
|
const modelToResolve = this.getModelToResolve(modelConfig, inputTokens);
|
|
6769
7000
|
if (!modelToResolve) {
|
|
@@ -6796,29 +7027,22 @@ Async buffering is enabled by default \u2014 this opt-out is only needed when us
|
|
|
6796
7027
|
* This is async because it needs to resolve the model configs.
|
|
6797
7028
|
*/
|
|
6798
7029
|
async getResolvedConfig(requestContext) {
|
|
6799
|
-
const
|
|
6800
|
-
|
|
6801
|
-
|
|
6802
|
-
return resolved?.modelId ? this.formatModelName(resolved) : "(unknown)";
|
|
6803
|
-
} catch (error) {
|
|
6804
|
-
omError("[OM] Failed to resolve model config", error);
|
|
6805
|
-
return "(unknown)";
|
|
6806
|
-
}
|
|
6807
|
-
};
|
|
6808
|
-
const [observationModelName, reflectionModelName] = await Promise.all([
|
|
6809
|
-
safeResolveModel(this.observationConfig.model),
|
|
6810
|
-
safeResolveModel(this.reflectionConfig.model)
|
|
7030
|
+
const [observationResolved, reflectionResolved] = await Promise.all([
|
|
7031
|
+
this.resolveModelRouting(this.observationConfig.model, requestContext),
|
|
7032
|
+
this.resolveModelRouting(this.reflectionConfig.model, requestContext)
|
|
6811
7033
|
]);
|
|
6812
7034
|
return {
|
|
6813
7035
|
scope: this.scope,
|
|
6814
7036
|
observation: {
|
|
6815
7037
|
messageTokens: this.observationConfig.messageTokens,
|
|
6816
|
-
model:
|
|
6817
|
-
previousObserverTokens: this.observationConfig.previousObserverTokens
|
|
7038
|
+
model: observationResolved.model,
|
|
7039
|
+
previousObserverTokens: this.observationConfig.previousObserverTokens,
|
|
7040
|
+
routing: observationResolved.routing
|
|
6818
7041
|
},
|
|
6819
7042
|
reflection: {
|
|
6820
7043
|
observationTokens: this.reflectionConfig.observationTokens,
|
|
6821
|
-
model:
|
|
7044
|
+
model: reflectionResolved.model,
|
|
7045
|
+
routing: reflectionResolved.routing
|
|
6822
7046
|
}
|
|
6823
7047
|
};
|
|
6824
7048
|
}
|
|
@@ -7625,7 +7849,7 @@ ${grouped}` : grouped;
|
|
|
7625
7849
|
* @param lockKey - Lock key for this scope
|
|
7626
7850
|
* @param writer - Optional stream writer for emitting buffering markers
|
|
7627
7851
|
*/
|
|
7628
|
-
async startAsyncBufferedObservation(record, threadId, unobservedMessages, lockKey, writer, contextWindowTokens, requestContext) {
|
|
7852
|
+
async startAsyncBufferedObservation(record, threadId, unobservedMessages, lockKey, writer, contextWindowTokens, requestContext, observabilityContext) {
|
|
7629
7853
|
const bufferKey = this.buffering.getObservationBufferKey(lockKey);
|
|
7630
7854
|
const currentTokens = contextWindowTokens ?? await this.tokenCounter.countMessagesAsync(unobservedMessages) + (record.pendingMessageTokens ?? 0);
|
|
7631
7855
|
BufferingCoordinator.lastBufferedBoundary.set(bufferKey, currentTokens);
|
|
@@ -7639,7 +7863,8 @@ ${grouped}` : grouped;
|
|
|
7639
7863
|
unobservedMessages,
|
|
7640
7864
|
bufferKey,
|
|
7641
7865
|
writer,
|
|
7642
|
-
requestContext
|
|
7866
|
+
requestContext,
|
|
7867
|
+
observabilityContext
|
|
7643
7868
|
).finally(() => {
|
|
7644
7869
|
BufferingCoordinator.asyncBufferingOps.delete(bufferKey);
|
|
7645
7870
|
unregisterOp(record.id, "bufferingObservation");
|
|
@@ -7653,7 +7878,7 @@ ${grouped}` : grouped;
|
|
|
7653
7878
|
* Internal method that waits for existing buffering operation and then runs new buffering.
|
|
7654
7879
|
* This implements the mutex-wait behavior.
|
|
7655
7880
|
*/
|
|
7656
|
-
async runAsyncBufferedObservation(record, threadId, unobservedMessages, bufferKey, writer, requestContext) {
|
|
7881
|
+
async runAsyncBufferedObservation(record, threadId, unobservedMessages, bufferKey, writer, requestContext, observabilityContext) {
|
|
7657
7882
|
const existingOp = BufferingCoordinator.asyncBufferingOps.get(bufferKey);
|
|
7658
7883
|
if (existingOp) {
|
|
7659
7884
|
try {
|
|
@@ -7725,7 +7950,8 @@ ${grouped}` : grouped;
|
|
|
7725
7950
|
cycleId,
|
|
7726
7951
|
startedAt,
|
|
7727
7952
|
writer,
|
|
7728
|
-
requestContext
|
|
7953
|
+
requestContext,
|
|
7954
|
+
observabilityContext
|
|
7729
7955
|
}).run();
|
|
7730
7956
|
const maxTs = this.getMaxMessageTimestamp(messagesToBuffer);
|
|
7731
7957
|
const cursor = new Date(maxTs.getTime() + 1);
|
|
@@ -8272,7 +8498,7 @@ ${grouped}` : grouped;
|
|
|
8272
8498
|
*/
|
|
8273
8499
|
/** @internal Used by ObservationStep. */
|
|
8274
8500
|
async buffer(opts) {
|
|
8275
|
-
const { threadId, resourceId, requestContext } = opts;
|
|
8501
|
+
const { threadId, resourceId, requestContext, observabilityContext } = opts;
|
|
8276
8502
|
let record = opts.record ?? await this.getOrCreateRecord(threadId, resourceId);
|
|
8277
8503
|
if (!this.buffering.isAsyncObservationEnabled()) {
|
|
8278
8504
|
return { buffered: false, record };
|
|
@@ -8368,7 +8594,8 @@ ${grouped}` : grouped;
|
|
|
8368
8594
|
cycleId,
|
|
8369
8595
|
startedAt,
|
|
8370
8596
|
writer,
|
|
8371
|
-
requestContext
|
|
8597
|
+
requestContext,
|
|
8598
|
+
observabilityContext
|
|
8372
8599
|
}).run();
|
|
8373
8600
|
await this.storage.setBufferingObservationFlag(record.id, false, newTokens).catch(() => {
|
|
8374
8601
|
});
|
|
@@ -8572,7 +8799,8 @@ ${grouped}` : grouped;
|
|
|
8572
8799
|
messages: unobservedMessages,
|
|
8573
8800
|
reflectionHooks,
|
|
8574
8801
|
requestContext,
|
|
8575
|
-
writer: opts.writer
|
|
8802
|
+
writer: opts.writer,
|
|
8803
|
+
observabilityContext: opts.observabilityContext
|
|
8576
8804
|
}).run();
|
|
8577
8805
|
observed = true;
|
|
8578
8806
|
} finally {
|
|
@@ -8594,7 +8822,7 @@ ${grouped}` : grouped;
|
|
|
8594
8822
|
* );
|
|
8595
8823
|
* ```
|
|
8596
8824
|
*/
|
|
8597
|
-
async reflect(threadId, resourceId, prompt, requestContext) {
|
|
8825
|
+
async reflect(threadId, resourceId, prompt, requestContext, observabilityContext) {
|
|
8598
8826
|
const record = await this.getOrCreateRecord(threadId, resourceId);
|
|
8599
8827
|
if (!record.activeObservations) {
|
|
8600
8828
|
return { reflected: false, record };
|
|
@@ -8611,7 +8839,9 @@ ${grouped}` : grouped;
|
|
|
8611
8839
|
void 0,
|
|
8612
8840
|
void 0,
|
|
8613
8841
|
void 0,
|
|
8614
|
-
requestContext
|
|
8842
|
+
requestContext,
|
|
8843
|
+
observabilityContext,
|
|
8844
|
+
void 0
|
|
8615
8845
|
);
|
|
8616
8846
|
const reflectionTokenCount = this.tokenCounter.countObservations(reflectResult.observations);
|
|
8617
8847
|
await this.storage.createReflectionGeneration({
|
|
@@ -8718,6 +8948,7 @@ ${grouped}` : grouped;
|
|
|
8718
8948
|
threadId: opts.threadId,
|
|
8719
8949
|
resourceId: opts.resourceId,
|
|
8720
8950
|
messageList: opts.messageList,
|
|
8951
|
+
observabilityContext: opts.observabilityContext,
|
|
8721
8952
|
hooks: opts.hooks
|
|
8722
8953
|
});
|
|
8723
8954
|
}
|
|
@@ -8966,6 +9197,17 @@ function writeProcessInputStepReproCapture(params) {
|
|
|
8966
9197
|
params.debug?.(`[OM:repro-capture] failed to write processInputStep capture: ${String(error)}`);
|
|
8967
9198
|
}
|
|
8968
9199
|
}
|
|
9200
|
+
function getOmObservabilityContext(args) {
|
|
9201
|
+
if (!args.tracing || !args.tracingContext || !args.loggerVNext || !args.metrics) {
|
|
9202
|
+
return void 0;
|
|
9203
|
+
}
|
|
9204
|
+
return {
|
|
9205
|
+
tracing: args.tracing,
|
|
9206
|
+
tracingContext: args.tracingContext,
|
|
9207
|
+
loggerVNext: args.loggerVNext,
|
|
9208
|
+
metrics: args.metrics
|
|
9209
|
+
};
|
|
9210
|
+
}
|
|
8969
9211
|
var ObservationalMemoryProcessor = class {
|
|
8970
9212
|
id = "observational-memory";
|
|
8971
9213
|
name = "Observational Memory";
|
|
@@ -9023,6 +9265,7 @@ var ObservationalMemoryProcessor = class {
|
|
|
9023
9265
|
threadId,
|
|
9024
9266
|
resourceId,
|
|
9025
9267
|
messageList,
|
|
9268
|
+
observabilityContext: getOmObservabilityContext(args),
|
|
9026
9269
|
hooks: {
|
|
9027
9270
|
onBufferChunkSealed: rotateResponseMessageId
|
|
9028
9271
|
}
|
|
@@ -9032,6 +9275,9 @@ var ObservationalMemoryProcessor = class {
|
|
|
9032
9275
|
await this.turn.start(this.memory);
|
|
9033
9276
|
state.__omTurn = this.turn;
|
|
9034
9277
|
}
|
|
9278
|
+
const observabilityContext = getOmObservabilityContext(args);
|
|
9279
|
+
state.__omObservabilityContext = observabilityContext;
|
|
9280
|
+
this.turn.observabilityContext = observabilityContext;
|
|
9035
9281
|
{
|
|
9036
9282
|
const step = this.turn.step(stepNumber);
|
|
9037
9283
|
let ctx;
|
|
@@ -9112,6 +9358,8 @@ var ObservationalMemoryProcessor = class {
|
|
|
9112
9358
|
const state = _state ?? {};
|
|
9113
9359
|
const context = this.engine.getThreadContext(requestContext, messageList);
|
|
9114
9360
|
if (!context) return messageList;
|
|
9361
|
+
const observabilityContext = getOmObservabilityContext(args);
|
|
9362
|
+
state.__omObservabilityContext = observabilityContext;
|
|
9115
9363
|
return this.engine.getTokenCounter().runWithModelContext(state.__omActorModelContext, async () => {
|
|
9116
9364
|
const memoryContext = memory.parseMemoryRequestContext(requestContext);
|
|
9117
9365
|
if (memoryContext?.memoryConfig?.readOnly) return messageList;
|
|
@@ -9186,5 +9434,5 @@ exports.stripEphemeralAnchorIds = stripEphemeralAnchorIds;
|
|
|
9186
9434
|
exports.stripObservationGroups = stripObservationGroups;
|
|
9187
9435
|
exports.truncateStringByTokens = truncateStringByTokens;
|
|
9188
9436
|
exports.wrapInObservationGroup = wrapInObservationGroup;
|
|
9189
|
-
//# sourceMappingURL=chunk-
|
|
9190
|
-
//# sourceMappingURL=chunk-
|
|
9437
|
+
//# sourceMappingURL=chunk-HUAXEKGI.cjs.map
|
|
9438
|
+
//# sourceMappingURL=chunk-HUAXEKGI.cjs.map
|