@gajae-code/agent-core 0.18.10 → 0.18.12
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 +20 -0
- package/dist/types/telemetry.d.ts +5 -3
- package/package.json +4 -4
- package/src/agent-loop.ts +117 -22
- package/src/compaction/compaction.ts +21 -2
- package/src/telemetry.ts +54 -11
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,26 @@
|
|
|
2
2
|
|
|
3
3
|
## [Unreleased]
|
|
4
4
|
|
|
5
|
+
## [0.18.12] - 2026-10-16
|
|
6
|
+
|
|
7
|
+
### Fixed
|
|
8
|
+
|
|
9
|
+
- Preserve a completed terminal outcome when a subscriber aborts after an accepted assistant response with no pending tool calls.
|
|
10
|
+
|
|
11
|
+
- Standalone agent loops now report `agent_end.stopReason: "cancelled"` when cancellation aborts a provider request or pending tool execution, including when a pause is requested at the same boundary.
|
|
12
|
+
|
|
13
|
+
- Preserve completed agent runs when a subscriber aborts after a final assistant message is accepted, while still reporting cancellation for interrupted tool execution and continuations.
|
|
14
|
+
|
|
15
|
+
- A project `.env` value of `OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT` no longer turns GenAI message-content capture on. An operator value the project does not declare is unchanged, including a downgrade before the first read. Removing the variable before that read keeps capture off, even when a trusted agent env file still says `full`.
|
|
16
|
+
|
|
17
|
+
- The typed `incompleteArgumentsReason` an adapter recorded on a tool-call block survives the accepted attempt snapshot. The snapshot rewrote every incomplete call to `malformed`, so a call the adapter had typed as `truncated`, `conflicting`, or `ambiguous` reached dispatch with only the boolean left and was reported with the generic wording.
|
|
18
|
+
- An incomplete tool call with no typed reason is no longer reported as an output-token-limit truncation unless the turn actually stopped for length, or as a decode failure the provider never declared. The rejection keeps the same actionable `Re-issue the call` guidance.
|
|
19
|
+
|
|
20
|
+
- Billing callbacks now receive the provider-effective service tier after a downgrade, avoiding cost-tier mismatches.
|
|
21
|
+
- Lossless message snapshots preserve the provider-effective service tier when other metadata is not cloneable.
|
|
22
|
+
|
|
23
|
+
## [0.18.11] - 2026-10-12
|
|
24
|
+
|
|
5
25
|
## [0.18.10] - 2026-10-12
|
|
6
26
|
|
|
7
27
|
## [0.18.9] - 2026-10-11
|
|
@@ -264,9 +264,11 @@ export interface AgentTelemetryConfig {
|
|
|
264
264
|
* truncated to bounded snippets, not removed. `"full"` emits both summaries
|
|
265
265
|
* and full OTEL message payloads.
|
|
266
266
|
*
|
|
267
|
-
* Defaults to the value of
|
|
268
|
-
*
|
|
269
|
-
* `
|
|
267
|
+
* Defaults to the trusted value of
|
|
268
|
+
* `OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT`
|
|
269
|
+
* (`true`/`1`/`yes` => `"summary"`, `"summary"` => `"summary"`,
|
|
270
|
+
* `"full"` => `"full"`). A project `.env` cannot select this default.
|
|
271
|
+
* Env-driven `"full"` capture emits a
|
|
270
272
|
* `full_content_capture_env_active` telemetry warning once per process.
|
|
271
273
|
*/
|
|
272
274
|
readonly captureMessageContent?: TelemetryContentCapture;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "@gajae-code/agent-core",
|
|
4
|
-
"version": "0.18.
|
|
4
|
+
"version": "0.18.12",
|
|
5
5
|
"description": "General-purpose agent with transport abstraction, state management, and attachment support",
|
|
6
6
|
"homepage": "https://gajae-code.com",
|
|
7
7
|
"author": "Yeachan-Heo and Gajae Code Contributors",
|
|
@@ -32,9 +32,9 @@
|
|
|
32
32
|
"fmt": "biome format --write ."
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@gajae-code/ai": "0.18.
|
|
36
|
-
"@gajae-code/natives": "0.18.
|
|
37
|
-
"@gajae-code/utils": "0.18.
|
|
35
|
+
"@gajae-code/ai": "0.18.12",
|
|
36
|
+
"@gajae-code/natives": "0.18.12",
|
|
37
|
+
"@gajae-code/utils": "0.18.12",
|
|
38
38
|
"@opentelemetry/api": "^1.9.0"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
package/src/agent-loop.ts
CHANGED
|
@@ -405,6 +405,14 @@ export const ESCAPED_NONASCII_RECOVERY_PROMPT = escapedNonAsciiRecoveryPrompt;
|
|
|
405
405
|
|
|
406
406
|
const MAX_ESCAPED_NONASCII_RESAMPLES = 2;
|
|
407
407
|
|
|
408
|
+
/**
|
|
409
|
+
* Recovery guidance kept byte-for-byte for every incomplete-argument rejection
|
|
410
|
+
* that carries a typed reason, and identical in meaning for the fallback where
|
|
411
|
+
* only the boolean was set.
|
|
412
|
+
*/
|
|
413
|
+
const INCOMPLETE_ARGUMENTS_REISSUE_GUIDANCE =
|
|
414
|
+
"Re-issue the call with complete arguments, splitting the work into smaller steps if needed.";
|
|
415
|
+
|
|
408
416
|
/** Whether any tool call in the turn carried `\uXXXX`-escaped arguments. */
|
|
409
417
|
interface EscapedToolCallMetadata {
|
|
410
418
|
guarded: boolean;
|
|
@@ -416,6 +424,13 @@ interface EscapedToolCallMetadata {
|
|
|
416
424
|
|
|
417
425
|
const acceptedToolCallMetadata = new WeakMap<object, EscapedToolCallMetadata>();
|
|
418
426
|
|
|
427
|
+
/** The closed reason vocabulary the tool-call contract accepts. */
|
|
428
|
+
const INCOMPLETE_ARGUMENTS_REASONS = new Set(["truncated", "malformed", "conflicting", "ambiguous"]);
|
|
429
|
+
|
|
430
|
+
function isIncompleteArgumentsReason(value: unknown): value is "truncated" | "malformed" | "conflicting" | "ambiguous" {
|
|
431
|
+
return typeof value === "string" && INCOMPLETE_ARGUMENTS_REASONS.has(value);
|
|
432
|
+
}
|
|
433
|
+
|
|
419
434
|
function escapedToolCallMetadata(
|
|
420
435
|
block: Extract<AssistantMessage["content"][number], { type: "toolCall" }>,
|
|
421
436
|
): EscapedToolCallMetadata {
|
|
@@ -442,6 +457,15 @@ function escapedToolCallMetadata(
|
|
|
442
457
|
(typeof incompleteReasonRead.value === "string" && SANITIZER_SENTINELS.has(incompleteReasonRead.value)) ||
|
|
443
458
|
(evidenceRead.present && (evidenceRead.value === undefined || evidence === undefined || evidence.malformed));
|
|
444
459
|
const incomplete = incompleteRead.ok && incompleteRead.value === true;
|
|
460
|
+
// A producer that typed the reason has already answered the question the
|
|
461
|
+
// metadata check is guessing at, so the declaration survives a failed or
|
|
462
|
+
// unverifiable evidence read instead of being overwritten with `malformed`.
|
|
463
|
+
// Re-labelling it would report a decode failure for a call the wire said was
|
|
464
|
+
// truncated (or conflicting, or ambiguously identified).
|
|
465
|
+
const declaredReason =
|
|
466
|
+
incompleteReasonRead.ok && typeof incompleteReasonRead.value === "string"
|
|
467
|
+
? incompleteReasonRead.value
|
|
468
|
+
: undefined;
|
|
445
469
|
return {
|
|
446
470
|
guarded:
|
|
447
471
|
malformedMetadata ||
|
|
@@ -451,7 +475,7 @@ function escapedToolCallMetadata(
|
|
|
451
475
|
evidence,
|
|
452
476
|
incompleteArguments: incomplete,
|
|
453
477
|
incompleteArgumentsReason:
|
|
454
|
-
!incompleteReasonRead.ok || malformedMetadata ? "malformed" : incompleteReasonRead.value,
|
|
478
|
+
declaredReason ?? (!incompleteReasonRead.ok || malformedMetadata ? "malformed" : incompleteReasonRead.value),
|
|
455
479
|
};
|
|
456
480
|
}
|
|
457
481
|
|
|
@@ -2019,6 +2043,7 @@ const LOSSLESS_SNAPSHOT_KEYS = [
|
|
|
2019
2043
|
"provider",
|
|
2020
2044
|
"model",
|
|
2021
2045
|
"responseId",
|
|
2046
|
+
"serviceTier",
|
|
2022
2047
|
"usage",
|
|
2023
2048
|
"stopReason",
|
|
2024
2049
|
"errorMessage",
|
|
@@ -3031,11 +3056,25 @@ class ManagedAttemptTransaction {
|
|
|
3031
3056
|
metadata.incompleteArgumentsReason ?? detachedMetadata.incompleteArgumentsReason,
|
|
3032
3057
|
};
|
|
3033
3058
|
if (combinedMetadata.malformed) {
|
|
3059
|
+
// The snapshot could not corroborate this call's account of its
|
|
3060
|
+
// arguments, so it must not execute. A reason the producer already
|
|
3061
|
+
// declared is still the accurate cause and is preserved: rewriting
|
|
3062
|
+
// every incomplete call to `malformed` here is what left a typed
|
|
3063
|
+
// reason unreachable at dispatch, where the block's own field is
|
|
3064
|
+
// the last surviving declaration of why the arguments stopped. If
|
|
3065
|
+
// no reason was declared, leave it unknown rather than claiming that
|
|
3066
|
+
// the terminal arguments failed to decode.
|
|
3067
|
+
const declaredReason = managedProperty(snapshotBlock, "incompleteArgumentsReason");
|
|
3068
|
+
const markedReason = combinedMetadata.incompleteArgumentsReason ?? declaredReason;
|
|
3034
3069
|
const marked = {
|
|
3035
3070
|
...snapshotBlock,
|
|
3036
3071
|
incompleteArguments: true,
|
|
3037
|
-
incompleteArgumentsReason: "malformed" as const,
|
|
3038
3072
|
};
|
|
3073
|
+
if (isIncompleteArgumentsReason(markedReason)) {
|
|
3074
|
+
marked.incompleteArgumentsReason = markedReason;
|
|
3075
|
+
} else {
|
|
3076
|
+
delete marked.incompleteArgumentsReason;
|
|
3077
|
+
}
|
|
3039
3078
|
snapshot.content[index] = marked;
|
|
3040
3079
|
acceptedToolCallMetadata.set(marked, combinedMetadata);
|
|
3041
3080
|
} else {
|
|
@@ -3413,7 +3452,7 @@ function buildAgentEndEvent(
|
|
|
3413
3452
|
messages: AgentMessage[],
|
|
3414
3453
|
telemetry: AgentTelemetry | undefined,
|
|
3415
3454
|
stepCount: number,
|
|
3416
|
-
stopReason: "completed" | "paused" = "completed",
|
|
3455
|
+
stopReason: "completed" | "paused" | "cancelled" = "completed",
|
|
3417
3456
|
scope?: AttemptScope,
|
|
3418
3457
|
): Extract<AgentEvent, { type: "agent_end" }> {
|
|
3419
3458
|
const base = { type: "agent_end" as const, messages, stopReason, ...(scope ? { scope } : {}) };
|
|
@@ -3812,6 +3851,9 @@ async function runLoopBody(
|
|
|
3812
3851
|
): Promise<void> {
|
|
3813
3852
|
let firstTurn = true;
|
|
3814
3853
|
let lastAttemptScope: AttemptScope | undefined;
|
|
3854
|
+
// A subscriber may abort reentrantly from the final assistant message_end;
|
|
3855
|
+
// do not relabel that already-accepted answer as cancelled.
|
|
3856
|
+
let lastAttemptAcceptedFinalAssistantMessage = false;
|
|
3815
3857
|
// Check for steering messages at start (user may have typed while waiting)
|
|
3816
3858
|
let pendingMessages: AgentMessage[] = (await config.getSteeringMessages?.()) || [];
|
|
3817
3859
|
let harmonyRetryAttempt = 0;
|
|
@@ -3857,6 +3899,23 @@ async function runLoopBody(
|
|
|
3857
3899
|
// the loop could run forever. Any turn that produces a non-malformed batch
|
|
3858
3900
|
// resets the counter, so healthy runs are unaffected.
|
|
3859
3901
|
let consecutiveMalformedTurns = 0;
|
|
3902
|
+
const finishCancelled = (scope?: AttemptScope): void => {
|
|
3903
|
+
publishAgentEnd(
|
|
3904
|
+
stream,
|
|
3905
|
+
config,
|
|
3906
|
+
buildAgentEndEvent(newMessages, telemetry, stepCounter.count, "cancelled", scope),
|
|
3907
|
+
scope,
|
|
3908
|
+
);
|
|
3909
|
+
stream.end(newMessages);
|
|
3910
|
+
};
|
|
3911
|
+
const commitMessages = (messages: AgentMessage[], scope?: AttemptScope): void => {
|
|
3912
|
+
for (const message of messages) {
|
|
3913
|
+
stream.push({ type: "message_start", message, scope });
|
|
3914
|
+
stream.push({ type: "message_end", message, scope });
|
|
3915
|
+
currentContext.messages.push(message);
|
|
3916
|
+
newMessages.push(message);
|
|
3917
|
+
}
|
|
3918
|
+
};
|
|
3860
3919
|
|
|
3861
3920
|
// Outer loop: continues when queued follow-up messages arrive after agent would stop
|
|
3862
3921
|
while (true) {
|
|
@@ -3864,6 +3923,7 @@ async function runLoopBody(
|
|
|
3864
3923
|
|
|
3865
3924
|
// Inner loop: process tool calls and steering messages
|
|
3866
3925
|
while (hasMoreToolCalls || pendingMessages.length > 0) {
|
|
3926
|
+
lastAttemptAcceptedFinalAssistantMessage = false;
|
|
3867
3927
|
const scope =
|
|
3868
3928
|
initialScope ?? (firstTurn ? config.initialScope : undefined) ?? config.attemptMinter?.mint("main");
|
|
3869
3929
|
initialScope = undefined;
|
|
@@ -3889,12 +3949,7 @@ async function runLoopBody(
|
|
|
3889
3949
|
// Commit queued user input outside the provisional assistant transaction so a
|
|
3890
3950
|
// discarded managed attempt cannot lose it before its retry continuation.
|
|
3891
3951
|
if (pendingMessages.length > 0) {
|
|
3892
|
-
|
|
3893
|
-
stream.push({ type: "message_start", message, scope: attemptScope });
|
|
3894
|
-
stream.push({ type: "message_end", message, scope: attemptScope });
|
|
3895
|
-
currentContext.messages.push(message);
|
|
3896
|
-
newMessages.push(message);
|
|
3897
|
-
}
|
|
3952
|
+
commitMessages(pendingMessages, attemptScope);
|
|
3898
3953
|
pendingMessages = [];
|
|
3899
3954
|
}
|
|
3900
3955
|
|
|
@@ -4411,13 +4466,17 @@ async function runLoopBody(
|
|
|
4411
4466
|
});
|
|
4412
4467
|
}
|
|
4413
4468
|
stream.push({ type: "turn_end", message, toolResults, scope: attemptScope });
|
|
4414
|
-
|
|
4415
|
-
|
|
4416
|
-
|
|
4417
|
-
|
|
4418
|
-
|
|
4419
|
-
|
|
4420
|
-
|
|
4469
|
+
if (message.stopReason === "aborted") {
|
|
4470
|
+
finishCancelled(attemptScope);
|
|
4471
|
+
} else {
|
|
4472
|
+
publishAgentEnd(
|
|
4473
|
+
stream,
|
|
4474
|
+
config,
|
|
4475
|
+
buildAgentEndEvent(newMessages, telemetry, stepCounter.count, "completed", attemptScope),
|
|
4476
|
+
attemptScope,
|
|
4477
|
+
);
|
|
4478
|
+
stream.end(newMessages);
|
|
4479
|
+
}
|
|
4421
4480
|
return;
|
|
4422
4481
|
}
|
|
4423
4482
|
|
|
@@ -4427,6 +4486,7 @@ async function runLoopBody(
|
|
|
4427
4486
|
(c): c is ToolCallContent => c.type === "toolCall" && !isProviderResolvedToolCall(c),
|
|
4428
4487
|
);
|
|
4429
4488
|
hasMoreToolCalls = toolCalls.length > 0;
|
|
4489
|
+
lastAttemptAcceptedFinalAssistantMessage = !hasMoreToolCalls;
|
|
4430
4490
|
|
|
4431
4491
|
const toolResults: ToolResultMessage[] = [];
|
|
4432
4492
|
let repeatedMalformedToolCall = false;
|
|
@@ -4578,8 +4638,18 @@ async function runLoopBody(
|
|
|
4578
4638
|
}
|
|
4579
4639
|
|
|
4580
4640
|
// Agent would stop here. Check for follow-up messages.
|
|
4581
|
-
|
|
4582
|
-
|
|
4641
|
+
if (loopSignal.aborted && !lastAttemptAcceptedFinalAssistantMessage) {
|
|
4642
|
+
finishCancelled(lastAttemptScope);
|
|
4643
|
+
return;
|
|
4644
|
+
}
|
|
4645
|
+
if (!loopSignal.aborted) {
|
|
4646
|
+
await config.onBeforeYield?.();
|
|
4647
|
+
}
|
|
4648
|
+
if (loopSignal.aborted && !lastAttemptAcceptedFinalAssistantMessage) {
|
|
4649
|
+
finishCancelled(lastAttemptScope);
|
|
4650
|
+
return;
|
|
4651
|
+
}
|
|
4652
|
+
if (!loopSignal.aborted && config.shouldPause?.()) {
|
|
4583
4653
|
publishAgentEnd(
|
|
4584
4654
|
stream,
|
|
4585
4655
|
config,
|
|
@@ -4591,9 +4661,22 @@ async function runLoopBody(
|
|
|
4591
4661
|
}
|
|
4592
4662
|
// Poll the consume-on-read recovery candidate before follow-ups so a real
|
|
4593
4663
|
// user message can supersede it without letting the stale recovery resurface
|
|
4594
|
-
// after that follow-up turn.
|
|
4664
|
+
// after that follow-up turn. An abort from an accepted final message_end must
|
|
4665
|
+
// also see a pending recovery before the run can report successful completion.
|
|
4595
4666
|
const syntheticRecoveryMessage = await config.getSyntheticRecoveryMessage?.();
|
|
4667
|
+
if (loopSignal.aborted) {
|
|
4668
|
+
if (lastAttemptAcceptedFinalAssistantMessage && !syntheticRecoveryMessage) break;
|
|
4669
|
+
finishCancelled(lastAttemptScope);
|
|
4670
|
+
return;
|
|
4671
|
+
}
|
|
4596
4672
|
const followUpMessages = (await config.getFollowUpMessages?.()) || [];
|
|
4673
|
+
if (loopSignal.aborted) {
|
|
4674
|
+
commitMessages(followUpMessages, lastAttemptScope);
|
|
4675
|
+
if (lastAttemptAcceptedFinalAssistantMessage && followUpMessages.length === 0 && !syntheticRecoveryMessage)
|
|
4676
|
+
break;
|
|
4677
|
+
finishCancelled(lastAttemptScope);
|
|
4678
|
+
return;
|
|
4679
|
+
}
|
|
4597
4680
|
if (followUpMessages.length > 0) {
|
|
4598
4681
|
// Set as pending so inner loop processes them
|
|
4599
4682
|
pendingMessages = followUpMessages;
|
|
@@ -5504,8 +5587,18 @@ async function executeToolCalls(
|
|
|
5504
5587
|
record.argumentValidationFailed = true;
|
|
5505
5588
|
// The provider flagged this call's arguments as unsafe to execute.
|
|
5506
5589
|
// The typed reason selects accurate recovery guidance; callers that
|
|
5507
|
-
// only read the boolean still get a safe, actionable rejection.
|
|
5508
|
-
|
|
5590
|
+
// only read the boolean still get a safe, actionable rejection. An
|
|
5591
|
+
// attempt-scoped snapshot can carry metadata captured before the
|
|
5592
|
+
// terminal block declared its reason, so the block's own field is
|
|
5593
|
+
// consulted whenever the snapshot has none.
|
|
5594
|
+
const reason =
|
|
5595
|
+
metadata.incompleteArgumentsReason ?? managedProperty(toolCall, "incompleteArgumentsReason");
|
|
5596
|
+
// The output-token-limit sentence is an assertion about WHY the
|
|
5597
|
+
// arguments stopped, and only a length stop proves it. A call that
|
|
5598
|
+
// carries no typed reason (an older producer that set only the
|
|
5599
|
+
// boolean) is rejected honestly with the same actionable guidance
|
|
5600
|
+
// instead of blaming a limit the turn may never have hit.
|
|
5601
|
+
const lengthTruncation = reason === "truncated" || assistantMessage.stopReason === "length";
|
|
5509
5602
|
const detail =
|
|
5510
5603
|
reason === "malformed"
|
|
5511
5604
|
? `The terminal arguments for tool call "${toolCall.name}" did not decode to a valid JSON object. The arguments cannot be executed. Re-issue the call with valid, complete arguments.`
|
|
@@ -5513,7 +5606,9 @@ async function executeToolCalls(
|
|
|
5513
5606
|
? `The streamed and terminal arguments for tool call "${toolCall.name}" disagree. The arguments cannot be executed safely. Re-issue the call with consistent arguments.`
|
|
5514
5607
|
: reason === "ambiguous"
|
|
5515
5608
|
? `The identity of tool call "${toolCall.name}" was ambiguous on the wire (duplicate call id or id/call_id collision), so its arguments cannot be safely attributed. Re-issue the call.`
|
|
5516
|
-
:
|
|
5609
|
+
: lengthTruncation
|
|
5610
|
+
? `Tool call "${toolCall.name}" was cut off before its arguments finished streaming (the response hit its output token limit). The partial arguments cannot be executed. ${INCOMPLETE_ARGUMENTS_REISSUE_GUIDANCE}`
|
|
5611
|
+
: `Tool call "${toolCall.name}" was incomplete before its arguments finished streaming. The partial arguments cannot be executed. ${INCOMPLETE_ARGUMENTS_REISSUE_GUIDANCE}`;
|
|
5517
5612
|
throw markDesignedError(new Error(detail));
|
|
5518
5613
|
}
|
|
5519
5614
|
const displaySafeEscapedArguments =
|
|
@@ -983,6 +983,23 @@ export function boundConversationTextForSummary(
|
|
|
983
983
|
return fitsBudget(bareMarker) ? bareMarker : "";
|
|
984
984
|
}
|
|
985
985
|
|
|
986
|
+
/** Convert a stored summary the same way as conversation text, for the outbound prompt only. */
|
|
987
|
+
function outboundSummaryText(text: string, convert: ConvertToLlm): string {
|
|
988
|
+
const [message] = convert([
|
|
989
|
+
{
|
|
990
|
+
role: "user",
|
|
991
|
+
content: [{ type: "text", text }],
|
|
992
|
+
timestamp: 0,
|
|
993
|
+
},
|
|
994
|
+
]);
|
|
995
|
+
if (!message) return text;
|
|
996
|
+
if (typeof message.content === "string") return message.content;
|
|
997
|
+
for (const block of message.content) {
|
|
998
|
+
if (block.type === "text") return block.text;
|
|
999
|
+
}
|
|
1000
|
+
return text;
|
|
1001
|
+
}
|
|
1002
|
+
|
|
986
1003
|
export async function generateSummary(
|
|
987
1004
|
currentMessages: AgentMessage[],
|
|
988
1005
|
model: Model,
|
|
@@ -1009,10 +1026,12 @@ export async function generateSummary(
|
|
|
1009
1026
|
const llmMessages = (options?.convertToLlm ?? convertToLlm)(currentMessages);
|
|
1010
1027
|
const conversationText = boundConversationTextForSummary(serializeConversation(llmMessages), model, maxTokens);
|
|
1011
1028
|
|
|
1012
|
-
// Build the prompt with conversation wrapped in tags
|
|
1029
|
+
// Build the prompt with conversation wrapped in tags.
|
|
1030
|
+
// The stored summary stays unchanged; only this outbound copy is converted.
|
|
1013
1031
|
let promptText = `<conversation>\n${conversationText}\n</conversation>\n\n`;
|
|
1014
1032
|
if (previousSummary) {
|
|
1015
|
-
|
|
1033
|
+
const outboundPreviousSummary = outboundSummaryText(previousSummary, options?.convertToLlm ?? convertToLlm);
|
|
1034
|
+
promptText += `<previous-summary>\n${outboundPreviousSummary}\n</previous-summary>\n\n`;
|
|
1016
1035
|
}
|
|
1017
1036
|
promptText += formatAdditionalContext(options?.extraContext);
|
|
1018
1037
|
promptText += basePrompt;
|
package/src/telemetry.ts
CHANGED
|
@@ -30,6 +30,8 @@ import {
|
|
|
30
30
|
completeSimple,
|
|
31
31
|
type Message,
|
|
32
32
|
type Model,
|
|
33
|
+
modelSupportsServiceTier,
|
|
34
|
+
modelSupportsUltrafastTier,
|
|
33
35
|
type ServiceTier,
|
|
34
36
|
type SimpleStreamOptions,
|
|
35
37
|
type StopReason,
|
|
@@ -37,6 +39,7 @@ import {
|
|
|
37
39
|
type ToolChoice,
|
|
38
40
|
type Usage,
|
|
39
41
|
} from "@gajae-code/ai";
|
|
42
|
+
import { $credentialEnv } from "@gajae-code/utils";
|
|
40
43
|
import { isDesignedError } from "@gajae-code/utils/error-classification";
|
|
41
44
|
import { recordHandledError } from "@gajae-code/utils/postmortem";
|
|
42
45
|
import {
|
|
@@ -345,9 +348,11 @@ export interface AgentTelemetryConfig {
|
|
|
345
348
|
* truncated to bounded snippets, not removed. `"full"` emits both summaries
|
|
346
349
|
* and full OTEL message payloads.
|
|
347
350
|
*
|
|
348
|
-
* Defaults to the value of
|
|
349
|
-
*
|
|
350
|
-
* `
|
|
351
|
+
* Defaults to the trusted value of
|
|
352
|
+
* `OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT`
|
|
353
|
+
* (`true`/`1`/`yes` => `"summary"`, `"summary"` => `"summary"`,
|
|
354
|
+
* `"full"` => `"full"`). A project `.env` cannot select this default.
|
|
355
|
+
* Env-driven `"full"` capture emits a
|
|
351
356
|
* `full_content_capture_env_active` telemetry warning once per process.
|
|
352
357
|
*/
|
|
353
358
|
readonly captureMessageContent?: TelemetryContentCapture;
|
|
@@ -457,9 +462,28 @@ export function resolveTelemetry(
|
|
|
457
462
|
|
|
458
463
|
let contentCaptureEnvCache: ResolvedTelemetryContentCapture | undefined;
|
|
459
464
|
let hasWarnedFullContentCaptureEnv = false;
|
|
465
|
+
|
|
466
|
+
/** `full` > `summary` > anything else. Matches the parser below. */
|
|
467
|
+
function contentCaptureRank(raw: string | undefined): 0 | 1 | 2 {
|
|
468
|
+
if (!raw) return 0;
|
|
469
|
+
const normalized = raw.trim().toLowerCase();
|
|
470
|
+
if (normalized === "full") return 2;
|
|
471
|
+
if (normalized === "summary" || normalized === "true" || normalized === "1" || normalized === "yes") return 1;
|
|
472
|
+
return 0;
|
|
473
|
+
}
|
|
474
|
+
|
|
460
475
|
function readContentCaptureEnv(): ResolvedTelemetryContentCapture {
|
|
461
476
|
if (contentCaptureEnvCache !== undefined) return contentCaptureEnvCache;
|
|
462
|
-
|
|
477
|
+
// `$credentialEnv` ignores a project `.env`, pins an inherited value, and
|
|
478
|
+
// falls back to trusted files. A missing current key is off, so that
|
|
479
|
+
// fallback cannot restore a deleted value. A lower live rank is a downgrade.
|
|
480
|
+
// A present higher rank cannot raise the pin.
|
|
481
|
+
const trusted = $credentialEnv(CONTENT_CAPTURE_ENV);
|
|
482
|
+
const live = Bun.env[CONTENT_CAPTURE_ENV];
|
|
483
|
+
let raw: string | undefined;
|
|
484
|
+
if (live === undefined) raw = undefined;
|
|
485
|
+
else if (contentCaptureRank(live) < contentCaptureRank(trusted)) raw = live;
|
|
486
|
+
else raw = trusted;
|
|
463
487
|
if (!raw) {
|
|
464
488
|
contentCaptureEnvCache = "none";
|
|
465
489
|
return "none";
|
|
@@ -753,7 +777,7 @@ export function startChatSpan(
|
|
|
753
777
|
model,
|
|
754
778
|
parent: options.parent,
|
|
755
779
|
stepNumber: options.stepNumber,
|
|
756
|
-
attributes: buildChatRequestAttributes(options.stepNumber, options.request, model
|
|
780
|
+
attributes: buildChatRequestAttributes(options.stepNumber, options.request, model),
|
|
757
781
|
});
|
|
758
782
|
if (span) {
|
|
759
783
|
telemetry.collector.beginChat(span, {
|
|
@@ -787,7 +811,7 @@ export interface ChatRequestSnapshot {
|
|
|
787
811
|
readonly messages?: readonly Message[];
|
|
788
812
|
}
|
|
789
813
|
|
|
790
|
-
function buildChatRequestAttributes(stepNumber: number, request: ChatRequestSnapshot,
|
|
814
|
+
function buildChatRequestAttributes(stepNumber: number, request: ChatRequestSnapshot, model: Model): Attributes {
|
|
791
815
|
const attrs: Attributes = {
|
|
792
816
|
[PiGenAIAttr.AgentStepNumber]: stepNumber,
|
|
793
817
|
[GenAIAttr.OutputType]: "text",
|
|
@@ -803,7 +827,16 @@ function buildChatRequestAttributes(stepNumber: number, request: ChatRequestSnap
|
|
|
803
827
|
if (request.stopSequences && request.stopSequences.length > 0) {
|
|
804
828
|
attrs[GenAIAttr.RequestStopSequences] = [...request.stopSequences];
|
|
805
829
|
}
|
|
806
|
-
if (
|
|
830
|
+
if (
|
|
831
|
+
request.serviceTier &&
|
|
832
|
+
shouldSendServiceTier(
|
|
833
|
+
request.serviceTier,
|
|
834
|
+
model.provider,
|
|
835
|
+
modelSupportsServiceTier(model),
|
|
836
|
+
modelSupportsUltrafastTier(model),
|
|
837
|
+
) &&
|
|
838
|
+
(request.serviceTier !== "ultrafast" || modelSupportsUltrafastTier(model))
|
|
839
|
+
) {
|
|
807
840
|
attrs[OpenAIAttr.RequestServiceTier] = request.serviceTier;
|
|
808
841
|
}
|
|
809
842
|
if (request.reasoningEffort) attrs[PiGenAIAttr.RequestReasoningEffort] = request.reasoningEffort;
|
|
@@ -1167,17 +1200,18 @@ export async function finishChatSpan(
|
|
|
1167
1200
|
readonly baseUrl?: string;
|
|
1168
1201
|
},
|
|
1169
1202
|
): Promise<void> {
|
|
1203
|
+
const serviceTier = resolveCompletedServiceTier(message, options.serviceTier);
|
|
1170
1204
|
// Usage-only mode (spans disabled): the chat span is absent but usage/cost
|
|
1171
1205
|
// hooks must still fire per contract C3. Cost estimation and the usage
|
|
1172
1206
|
// event run against a non-recording placeholder span.
|
|
1173
1207
|
if (!span) {
|
|
1174
1208
|
if (!telemetry || telemetry.spansEnabled) return;
|
|
1175
1209
|
const placeholder = trace.wrapSpanContext(INVALID_SPAN_CONTEXT);
|
|
1176
|
-
const usageCost = applyCostEstimate(telemetry, placeholder, message,
|
|
1210
|
+
const usageCost = applyCostEstimate(telemetry, placeholder, message, serviceTier, options.stepNumber);
|
|
1177
1211
|
await emitChatUsage(telemetry, placeholder, {
|
|
1178
1212
|
model: message.model,
|
|
1179
1213
|
provider: message.provider,
|
|
1180
|
-
serviceTier
|
|
1214
|
+
serviceTier,
|
|
1181
1215
|
stepNumber: options.stepNumber,
|
|
1182
1216
|
usage: message.usage,
|
|
1183
1217
|
applied: usageCost,
|
|
@@ -1195,12 +1229,12 @@ export async function finishChatSpan(
|
|
|
1195
1229
|
applyChatResponseAttributes(span, message);
|
|
1196
1230
|
applyUsageAttributes(span, message.usage);
|
|
1197
1231
|
applyGatewayAttributes(span, options.responseHeaders, options.baseUrl);
|
|
1198
|
-
const cost = applyCostEstimate(telemetry, span, message,
|
|
1232
|
+
const cost = applyCostEstimate(telemetry, span, message, serviceTier, options.stepNumber);
|
|
1199
1233
|
if (telemetry) {
|
|
1200
1234
|
await emitChatUsage(telemetry, span, {
|
|
1201
1235
|
model: message.model,
|
|
1202
1236
|
provider: message.provider,
|
|
1203
|
-
serviceTier
|
|
1237
|
+
serviceTier,
|
|
1204
1238
|
stepNumber: options.stepNumber,
|
|
1205
1239
|
usage: message.usage,
|
|
1206
1240
|
applied: cost,
|
|
@@ -1267,6 +1301,15 @@ export function failChatSpan(
|
|
|
1267
1301
|
span.end();
|
|
1268
1302
|
}
|
|
1269
1303
|
|
|
1304
|
+
function resolveCompletedServiceTier(
|
|
1305
|
+
message: AssistantMessage,
|
|
1306
|
+
requestedServiceTier: ServiceTier | undefined,
|
|
1307
|
+
): ServiceTier | undefined {
|
|
1308
|
+
if (message.serviceTier !== undefined) return message.serviceTier;
|
|
1309
|
+
// Ultrafast is sent only for opted-in models, so an absent completion tier means it was not applied.
|
|
1310
|
+
return requestedServiceTier === "ultrafast" ? undefined : requestedServiceTier;
|
|
1311
|
+
}
|
|
1312
|
+
|
|
1270
1313
|
function applyChatResponseAttributes(span: Span, message: AssistantMessage): void {
|
|
1271
1314
|
span.setAttribute(GenAIAttr.ResponseModel, message.model);
|
|
1272
1315
|
if (message.responseId) span.setAttribute(GenAIAttr.ResponseId, message.responseId);
|