File size: 37,234 Bytes
68d7816 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 | import { createHash } from 'node:crypto';
import { LifecycleScope } from '#/app/scopes';
import { ScopeActivation, registerScopedService } from '#/_base/di/scope';
import { defineState } from '#/state/state';
import { IAgentContextMemoryService } from '#/agent/contextMemory/contextMemory';
import {
IAgentContextProjectorService,
type MediaStripSnapshot,
type ProjectionPolicy,
} from '#/agent/contextProjector/contextProjector';
import { ISessionTokenCountingService } from '#/session/tokenCounting/sessionTokenCounting';
import { IAgentProfileService, type ProfileModelContext } from '#/agent/profile/profile';
import { IAgentStateService } from '#/agent/state/agentState';
import { IAgentToolRegistryService } from '#/agent/toolRegistry/toolRegistry';
import { IAgentToolSelectService } from '#/agent/toolSelect/toolSelect';
import { IAgentMediaResolverService } from '#/agent/media/mediaResolver';
import { ISessionUsageService } from '#/session/usage/sessionUsage';
import { IConfigService } from '#/app/config/config';
import {
APIContextOverflowError,
APIRequestTooLargeError,
APIStatusError,
classifyApiError,
isImageFormatError,
isRecoverableRequestStructureError,
isRetryableGenerateError,
} from '#/llm-adapter/contract/errors';
import type { Message } from '#/llm-adapter/contract/message';
import { type ThinkingEffort } from '#human/llm/thinking';
import type { LlmCredentialProvider } from '#human/llm/requester/requester';
import { isToolCall, type StreamedMessagePart, type ToolDescription as Tool } from '#human/llm/message';
import { emptyUsage, inputTotal, type TokenUsage } from '#human/llm/usage';
import { ILogService, type LogContext } from '#/_base/log/log';
import { IModelCatalog, type Model } from '#/llm-adapter/model/catalog';
import {
effectiveMaxCompletionTokens,
type ModelRequestEvent,
type ModelRequestParams,
type ModelRequester,
type ModelRequestTiming,
} from '#/llm-adapter/model/model-requester';
import type { ModelOverrides } from '#/llm-adapter/model/model.types';
import { IModelService } from '#/llm-adapter/model/model';
import { completionBudgetParams, resolveCompletionBudget } from '#/llm-adapter/model/completion-budget';
import { resolveThinkingKeep, type ThinkingConfig } from '#/llm-adapter/model/thinking';
import { THINKING_SECTION } from '#/app/kosongConfig/configSection';
import type { Protocol } from '#/llm-adapter/protocol/protocol';
import type {
ApiErrorEvent,
LlmRequestProjectionFallbackEvent,
} from '#/app/telemetry/events';
import { ITelemetryService } from '#/app/telemetry/telemetry';
import { IAgentScopeContext } from '#/agent/scopeContext/scopeContext';
import { IEventDispatcher } from '#/state/eventDispatcher';
import { WarningIssued } from '#/agent/profile/profileOps';
import {
IAgentLLMRequesterService,
type AgentLLMRequestFinish,
type AgentLLMRequestLogFields,
type AgentLLMRequestOverrides,
type AgentLLMRequestPartHandler,
type AgentLLMRequestSource,
type AgentLLMRequestTask,
type PreparedTurnRequestConfig,
} from './llmRequester';
import type { LLMRequestTrace } from '#/llm-adapter/contract/request-trace';
import {
ToolCallIdNormalizer,
type ToolCallIdResponseNormalizer,
} from '#human/llm/toolCallIdNormalizer';
import {
LlmRequest,
llmRequestTraceKey,
LlmToolsSnapshot,
type LlmRequestPayload,
type LlmRequestToolSchema,
} from './llmRequestOps';
import { isAbortError } from '#/_base/utils/abort';
import { parseBooleanEnv } from '#/_base/utils/env';
import { ErrorCodes, Error2, unwrapErrorCause } from '#/errors';
import {
readRetryAfterMs,
retryBackoffDelay,
retryErrorFields,
sleepForRetry,
} from '#/_base/utils/retry';
import { IBootstrapService } from '#/app/bootstrap/bootstrap';
const EMPTY_TOOL_PARAMETERS: Record<string, unknown> = {
type: 'object',
properties: {},
};
const noopOnPart: AgentLLMRequestPartHandler = () => {};
export const KIMI_CODE_INFINITE_RETRY_ENV = 'KIMI_CODE_INFINITE_RETRY';
interface ResolvedLLMRequest {
readonly requester: ModelRequester;
readonly model: Model;
readonly params: ModelRequestParams;
readonly modelAlias: string;
readonly thinkingEffort: ThinkingEffort;
readonly systemPrompt: string;
readonly tools: readonly Tool[];
readonly messages: Message[];
readonly source: AgentLLMRequestSource | undefined;
readonly logFields: AgentLLMRequestLogFields;
}
interface LLMRequestLogInput {
readonly protocol: Protocol;
readonly providerType?: string;
readonly modelName: string;
readonly modelAlias?: string;
readonly thinkingEffort?: ThinkingEffort | null;
readonly maxTokens?: number;
readonly systemPrompt: string;
readonly tools: readonly Tool[];
readonly messages: readonly Message[];
readonly fields?: AgentLLMRequestLogFields;
}
interface TurnRequestConfig {
readonly resolved: ProfileModelContext;
readonly params: ModelRequestParams;
readonly systemPrompt: string;
}
export const llmRequesterLastConfigLogSignatureKey = defineState<string | undefined>(
'llmRequester.lastConfigLogSignature',
() => undefined as string | undefined,
);
export const llmRequesterTurnConfigsKey = defineState<Map<number, TurnRequestConfig>>(
'llmRequester.turnConfigs',
() => new Map(),
);
export const llmRequesterMediaDegradedTurnsKey = defineState<Set<number>>(
'llmRequester.mediaDegradedTurns',
() => new Set(),
);
export const llmRequesterMediaStrippedTurnsKey = defineState<Map<number, MediaStripSnapshot>>(
'llmRequester.mediaStrippedTurns',
() => new Map(),
);
export const llmRequesterEmittedThinkingEffortWarningsKey = defineState<Set<string>>(
'llmRequester.emittedThinkingEffortWarnings',
() => new Set(),
);
export class AgentLLMRequesterService implements IAgentLLMRequesterService {
declare readonly _serviceBrand: undefined;
private readonly toolCallIdNormalizer = new ToolCallIdNormalizer();
constructor(
@IAgentContextMemoryService private readonly context: IAgentContextMemoryService,
@IAgentContextProjectorService private readonly projector: IAgentContextProjectorService,
@ISessionTokenCountingService private readonly tokenCounting: ISessionTokenCountingService,
@IAgentToolRegistryService private readonly tools: IAgentToolRegistryService,
@IAgentToolSelectService private readonly toolSelect: IAgentToolSelectService,
@IAgentMediaResolverService private readonly mediaResolver: IAgentMediaResolverService,
@IAgentProfileService private readonly profile: IAgentProfileService,
@ISessionUsageService private readonly usage: ISessionUsageService,
@IConfigService private readonly config: IConfigService,
@IModelService private readonly modelService: IModelService,
@IModelCatalog private readonly modelCatalog: IModelCatalog,
@ILogService private readonly log: ILogService,
@ITelemetryService private readonly telemetry: ITelemetryService,
@IEventDispatcher private readonly dispatcher: IEventDispatcher,
@IAgentScopeContext private readonly scopeContext: IAgentScopeContext,
@IAgentStateService private readonly states: IAgentStateService,
@IBootstrapService private readonly bootstrap: IBootstrapService,
) {
this.states.contributeState(llmRequestTraceKey);
this.states.contributeState(llmRequesterLastConfigLogSignatureKey);
this.states.contributeState(llmRequesterTurnConfigsKey);
this.states.contributeState(llmRequesterMediaDegradedTurnsKey);
this.states.contributeState(llmRequesterMediaStrippedTurnsKey);
this.states.contributeState(llmRequesterEmittedThinkingEffortWarningsKey);
}
private get lastConfigLogSignature(): string | undefined {
return this.states.get(llmRequesterLastConfigLogSignatureKey);
}
private set lastConfigLogSignature(value: string | undefined) {
this.states.set(llmRequesterLastConfigLogSignatureKey, value);
}
private get turnConfigs(): Map<number, TurnRequestConfig> {
return this.states.get(llmRequesterTurnConfigsKey);
}
private get mediaDegradedTurns(): Set<number> {
return this.states.get(llmRequesterMediaDegradedTurnsKey);
}
private get mediaStrippedTurns(): Map<number, MediaStripSnapshot> {
return this.states.get(llmRequesterMediaStrippedTurnsKey);
}
private get emittedThinkingEffortWarnings(): Set<string> {
return this.states.get(llmRequesterEmittedThinkingEffortWarningsKey);
}
prepareTurnConfig(turnId: number): PreparedTurnRequestConfig | undefined {
if (!this.profile.hasProvider()) return undefined;
const config = this.getOrCreateTurnConfig(turnId);
return { thinkingEffort: config.resolved.thinkingLevel };
}
currentCredentialProvider(): LlmCredentialProvider | undefined {
if (!this.profile.hasProvider()) return undefined;
return this.modelCatalog.get(this.profile.resolveModelContext().modelAlias).credentialProvider;
}
credentialProviderForTurn(turnId: number): LlmCredentialProvider | undefined {
if (!this.profile.hasProvider()) return undefined;
const resolved = this.turnConfigs.get(turnId)?.resolved ?? this.profile.resolveModelContext();
return this.modelCatalog.get(resolved.modelAlias).credentialProvider;
}
async request(
overrides: AgentLLMRequestOverrides = {},
onPart: AgentLLMRequestPartHandler = noopOnPart,
signal?: AbortSignal,
): Promise<AgentLLMRequestFinish> {
return this.start(overrides, onPart, signal).result;
}
start(
overrides: AgentLLMRequestOverrides = {},
onPart: AgentLLMRequestPartHandler = noopOnPart,
signal?: AbortSignal,
): AgentLLMRequestTask {
const trace = new MutableLLMRequestTrace();
return {
trace,
result: this.requestWithTrace(trace, overrides, onPart, signal),
};
}
private async requestWithTrace(
trace: MutableLLMRequestTrace,
overrides: AgentLLMRequestOverrides,
onPart: AgentLLMRequestPartHandler,
signal: AbortSignal | undefined,
): Promise<AgentLLMRequestFinish> {
signal?.throwIfAborted();
const startedAt = Date.now();
const setTrace = (traceId: string | undefined): void => {
trace.set(traceId);
if (overrides.source?.type === 'turn') {
this.telemetry.setContext({ trace_id: traceId });
}
};
setTrace(undefined);
try {
return await this.runRequest(
this.resolveRequest(overrides),
onPart,
signal,
setTrace,
overrides.onAttemptRetry,
);
} catch (error) {
this.logRequestFailure(error, overrides, signal);
setTrace(this.trackApiError(error, startedAt, signal, overrides.source, trace.traceId));
throw error;
}
}
private logRequestFailure(
error: unknown,
overrides: AgentLLMRequestOverrides,
signal: AbortSignal | undefined,
): void {
if (isAbortError(error) || signal?.aborted === true) return;
const payload: LogContext = {
...logFieldsForSource(overrides.source),
model: this.profile.data().modelAlias ?? 'unknown',
...retryErrorFields(error),
};
this.log.warn('llm request failed', payload);
}
private trackApiError(
error: unknown,
startedAt: number,
signal: AbortSignal | undefined,
source?: AgentLLMRequestSource,
requestTraceId?: string,
): string | undefined {
if (isAbortError(error) || signal?.aborted === true) return requestTraceId;
const modelAlias = this.profile.data().modelAlias;
const model = this.tryGetModel();
const traceId = requestTraceId ?? apiTraceId(error);
const classification = classifyApiError(unwrapErrorCause(error));
const properties: ApiErrorEvent = {
error_type: classification.kind,
model: model?.id ?? modelAlias ?? 'unknown',
alias: modelAlias,
provider_type: model?.providerType ?? model?.protocol,
protocol: model?.protocol,
retryable: isRetryableGenerateError(error),
duration_ms: Math.max(0, Date.now() - startedAt),
turn_id: source?.turnId,
request_kind: requestKindForTelemetry(source),
trace_id: traceId,
};
if (source?.type === 'turn') {
if (source.step !== undefined) properties['step_no'] = source.step;
}
const statusCode = apiStatusCode(error);
if (statusCode !== undefined) properties['status_code'] = statusCode;
const currentTurn = this.usage.status(this.scopeContext.agentContext).currentTurn;
if (currentTurn !== undefined) properties['input_tokens'] = inputTotal(currentTurn);
this.telemetry.track2('api_error', properties);
return traceId;
}
private tryGetModel(): Model | undefined {
const modelAlias = this.profile.data().modelAlias;
if (modelAlias === undefined) return undefined;
try {
return this.modelCatalog.get(modelAlias);
} catch {
return undefined;
}
}
private async runRequest(
request: ResolvedLLMRequest,
onPart: AgentLLMRequestPartHandler,
signal: AbortSignal | undefined,
onRequestTrace: (traceId: string | undefined) => void,
onAttemptRetry: (() => void) | undefined,
): Promise<AgentLLMRequestFinish> {
this.toolCallIdNormalizer.seedFrom(this.context.get());
const shaped = this.toolSelect.shapeHistory(request.messages);
const recoveredStrip = this.mediaStripSnapshotForTurn(request.source);
let policy: ProjectionPolicy | undefined =
recoveredStrip !== undefined
? { media: { strip: recoveredStrip } }
: this.isRecoveryTurn(this.mediaDegradedTurns, request.source)
? { media: 'degraded' }
: undefined;
const captureMediaStripPolicy = (): { readonly strip: MediaStripSnapshot } => {
const snapshot = this.projector.captureMediaStripSnapshot(shaped);
this.markMediaStrippedRecoveryTurn(snapshot, request.source);
return { strip: snapshot };
};
let previousMediaCount: number | undefined;
let previousMediaPolicy: ProjectionPolicy['media'];
let mediaPaths: ReadonlyMap<string, string> | undefined;
const run = async (
policy: ProjectionPolicy | undefined,
): Promise<AgentLLMRequestFinish> => {
onRequestTrace(undefined);
const projection = projectionNameOf(policy);
const fields =
projection === undefined ? request.logFields : { ...request.logFields, projection };
if (policy?.media !== undefined) {
mediaPaths ??= await this.mediaResolver.displayPaths(shaped);
}
const projected = this.projector.project(shaped, policy, mediaPaths);
const currentMediaCount = mediaPartCount(projected);
const mediaPolicyChanged =
previousMediaCount !== undefined && previousMediaPolicy !== policy?.media;
const droppedMediaCount =
previousMediaCount === undefined ? 0 : previousMediaCount - currentMediaCount;
previousMediaCount = currentMediaCount;
previousMediaPolicy = policy?.media;
const input = {
systemPrompt: request.systemPrompt,
tools: request.tools,
messages: await this.mediaResolver.resolve(projected, request.requester, signal),
};
const mediaProjection =
projection === 'media-degraded' || projection === 'strict-media-degraded'
? 'media-degraded'
: projection === 'media-stripped' || projection === 'strict-media-stripped'
? 'media-stripped'
: undefined;
if (mediaPolicyChanged && droppedMediaCount > 0 && mediaProjection !== undefined) {
try {
void this.dispatcher.dispatch(
new WarningIssued({
agentId: this.scopeContext.agentId,
code: mediaProjection,
message:
mediaProjection === 'media-degraded'
? 'Provider rejected the request as too large; older media were dropped and the request was retried.'
: 'Provider rejected the media in the request; all media were omitted and the request was retried.',
}),
);
} catch {
}
}
this.warnAboutAnthropicThinkingEffort(request);
const logInput: LLMRequestLogInput = {
protocol: request.model.protocol,
providerType: request.model.providerType,
modelName: request.model.name,
modelAlias: request.modelAlias,
thinkingEffort: request.thinkingEffort,
maxTokens: effectiveMaxCompletionTokens(request.params),
systemPrompt: input.systemPrompt,
tools: input.tools,
messages: input.messages,
fields,
};
this.logRequest(logInput);
this.recordRequest(logInput);
let message: Message | undefined;
let usage: TokenUsage | undefined;
let timing: ModelRequestTiming | undefined;
let finish: Extract<ModelRequestEvent, { type: 'finish' }> | undefined;
const toolCallIds = this.toolCallIdNormalizer.beginResponse();
const setTraceId = (traceId: string | null | undefined): void => {
const normalized = traceId ?? undefined;
onRequestTrace(normalized);
};
try {
for await (const event of request.requester.request(input, signal, {
...request.params,
onTraceId: setTraceId,
})) {
switch (event.type) {
case 'part':
await onPart(this.normalizeStreamPart(toolCallIds, event.part));
break;
case 'usage':
usage = event.usage;
break;
case 'finish':
finish = event;
message = event.message;
setTraceId(event.traceId);
break;
case 'timing': {
const { type: _type, ...streamTiming } = event;
timing = streamTiming;
break;
}
}
}
if (message === undefined || finish === undefined) {
throw new Error2(
ErrorCodes.PROVIDER_API_ERROR,
'LLM request stream ended without a finish event.',
);
}
const finalizedCalls = toolCallIds.remapFinalizedCalls(message.toolCalls);
if (finalizedCalls !== message.toolCalls) {
message = { ...message, toolCalls: finalizedCalls };
}
for (const { raw, assigned } of toolCallIds.remapped) {
this.log.warn('Rewrote a duplicate provider tool call id into an agent-unique one.', {
raw,
assigned,
model: request.modelAlias,
});
}
} catch (error) {
toolCallIds.rollback();
throw error;
}
void this.usage.record(
this.scopeContext.agentContext,
request.modelAlias,
usage ?? emptyUsage(),
request.source,
);
if (usage !== undefined) {
this.tokenCounting.measured(this.scopeContext.agentContext, request.messages, [message], usage);
}
this.logResponse(request.logFields, usage ?? emptyUsage(), timing);
return {
message,
usage: usage ?? emptyUsage(),
model: request.modelAlias,
providerFinishReason: finish.providerFinishReason,
rawFinishReason: finish.rawFinishReason,
providerMessageId: finish.id,
timing,
traceId: finish.traceId,
};
};
let infiniteRetryAttempt = 0;
for (;;) {
try {
return await run(policy);
} catch (error) {
const nextPolicy = this.nextProjectionPolicyForError(
error,
policy,
request,
signal,
captureMediaStripPolicy,
);
if (nextPolicy !== undefined) {
onAttemptRetry?.();
policy = nextPolicy;
continue;
}
const raw = unwrapErrorCause(error);
if (
!this.infiniteRetryEnabled ||
isAbortError(error) ||
signal?.aborted === true ||
raw instanceof APIContextOverflowError
) {
throw error;
}
infiniteRetryAttempt += 1;
const delayMs =
readRetryAfterMs(raw) ??
retryBackoffDelay(infiniteRetryAttempt - 1);
this.log.warn('llm request failed; retrying indefinitely (KIMI_CODE_INFINITE_RETRY)', {
model: request.model.name,
...request.logFields,
attempt: infiniteRetryAttempt,
delayMs,
...retryErrorFields(error),
});
onAttemptRetry?.();
await sleepForRetry(delayMs, signal);
}
}
}
private get infiniteRetryEnabled(): boolean {
return parseBooleanEnv(this.bootstrap.getEnv(KIMI_CODE_INFINITE_RETRY_ENV)) === true;
}
private nextProjectionPolicyForError(
error: unknown,
policy: ProjectionPolicy | undefined,
request: ResolvedLLMRequest,
signal: AbortSignal | undefined,
captureMediaStripPolicy: () => { readonly strip: MediaStripSnapshot },
): ProjectionPolicy | undefined {
if (signal?.aborted === true) return undefined;
const raw = unwrapErrorCause(error);
const media = policy?.media;
let projection: LlmRequestProjectionFallbackEvent['projection'];
let nextPolicy: ProjectionPolicy;
if (
raw instanceof APIRequestTooLargeError &&
(media === undefined || media === 'degraded')
) {
signal?.throwIfAborted();
if (media === undefined) {
this.log.warn('provider rejected request as too large; resending with degraded media', {
model: request.model.name,
...request.logFields,
});
this.markRecoveryTurn(this.mediaDegradedTurns, request.source);
projection = 'media-degraded';
nextPolicy = { ...policy, media: 'degraded' };
} else {
this.log.warn(
'provider rejected degraded-media request as too large; resending with rejected media stripped',
{
model: request.model.name,
...request.logFields,
},
);
projection = 'media-stripped';
nextPolicy = { ...policy, media: captureMediaStripPolicy() };
}
} else if (typeof media !== 'object' && isImageFormatError(raw)) {
signal?.throwIfAborted();
this.log.warn(
'provider rejected an image in the request; resending with rejected media stripped',
{
model: request.model.name,
...request.logFields,
},
);
projection = 'media-stripped';
nextPolicy = { ...policy, media: captureMediaStripPolicy() };
} else if (policy?.structure === undefined && isRecoverableRequestStructureError(raw)) {
signal?.throwIfAborted();
this.log.warn('provider rejected request structure; resending with strict projection', {
model: request.model.name,
...request.logFields,
});
projection = 'strict';
nextPolicy = { ...policy, structure: 'strict' };
} else {
return undefined;
}
const properties: LlmRequestProjectionFallbackEvent = {
projection,
error_type: classifyApiError(raw).kind,
model: request.model.id,
turn_id: request.source?.turnId,
};
this.telemetry.track2('llm_request_projection_fallback', properties);
return nextPolicy;
}
private normalizeStreamPart(
toolCallIds: ToolCallIdResponseNormalizer,
part: StreamedMessagePart,
): StreamedMessagePart {
if (!isToolCall(part)) return part;
const assigned = toolCallIds.remapStreamedId(part.id, part._streamIndex);
return assigned === part.id ? part : { ...part, id: assigned, rawId: part.rawId ?? part.id };
}
private warnAboutAnthropicThinkingEffort(request: ResolvedLLMRequest): void {
if (request.model.protocol !== 'anthropic') return;
const effort = request.thinkingEffort;
if (effort === 'on' || effort === 'off') return;
let code: string;
let message: string;
let knownEfforts: string | undefined;
const supportEfforts = request.model.supportEfforts?.filter((value) => value.length > 0);
if (supportEfforts === undefined || supportEfforts.length === 0) return;
if (supportEfforts.includes(effort)) return;
code = 'anthropic-thinking-effort-not-listed';
knownEfforts = supportEfforts.join(',');
message = `Thinking effort "${effort}" is not listed for model "${request.model.name}" (known: ${supportEfforts.join(', ')}). The configured value will be sent unchanged to the Anthropic-compatible backend.`;
const key = [code, request.modelAlias, request.model.name, effort, knownEfforts].join('\u0000');
if (this.emittedThinkingEffortWarnings.has(key)) return;
this.emittedThinkingEffortWarnings.add(key);
try {
this.log.warn(message, {
modelAlias: request.modelAlias,
model: request.model.name,
effort,
knownEfforts,
});
} catch {
}
try {
void this.dispatcher.dispatch(
new WarningIssued({ agentId: this.scopeContext.agentId, code, message }),
);
} catch {
}
}
private isRecoveryTurn(set: ReadonlySet<number>, source: AgentLLMRequestSource | undefined): boolean {
if (source?.type !== 'turn') return false;
return set.has(source.turnId);
}
private mediaStripSnapshotForTurn(
source: AgentLLMRequestSource | undefined,
): MediaStripSnapshot | undefined {
if (source?.type !== 'turn') return undefined;
return this.mediaStrippedTurns.get(source.turnId);
}
private markMediaStrippedRecoveryTurn(
snapshot: MediaStripSnapshot,
source: AgentLLMRequestSource | undefined,
): void {
if (source?.type !== 'turn') return;
for (const id of this.mediaStrippedTurns.keys()) {
if (id < source.turnId) this.mediaStrippedTurns.delete(id);
}
this.mediaStrippedTurns.set(source.turnId, snapshot);
}
private markRecoveryTurn(set: Set<number>, source: AgentLLMRequestSource | undefined): void {
if (source?.type !== 'turn') return;
for (const id of set) {
if (id < source.turnId) set.delete(id);
}
set.add(source.turnId);
}
private resolveRequest(overrides: AgentLLMRequestOverrides): ResolvedLLMRequest {
const turnConfig = this.resolveTurnConfig(overrides.source);
const resolved = turnConfig?.resolved ?? this.profile.resolveModelContext();
const baseParams = turnConfig?.params ?? this.profile.resolveRequestParams();
const budgetParams = completionBudgetParams({
budget: resolveCompletionBudget({
maxOutputSize: overrides.maxOutputSize ?? resolved.maxOutputSize,
reservedContextSize: resolved.reservedContextSize,
maxCompletionTokensCap:
this.config.get<ModelOverrides>('modelOverrides')?.maxCompletionTokens,
}),
capability: resolved.modelCapabilities,
usedContextTokens:
overrides.messages === undefined
? this.tokenCounting.get(this.scopeContext.agentContext).measured
: undefined,
});
const requester = this.modelCatalog.getRequester(resolved.modelAlias);
const messages = overrides.messages ?? this.context.get();
return {
requester,
model: requester.model,
params: { ...baseParams, ...budgetParams },
modelAlias: resolved.modelAlias,
thinkingEffort: resolved.thinkingLevel,
systemPrompt: overrides.systemPrompt ?? turnConfig?.systemPrompt ?? this.profile.getSystemPrompt(),
tools: [...(overrides.tools ?? this.defaultTools())],
messages: [...messages],
source: overrides.source,
logFields: logFieldsForSource(overrides.source),
};
}
private resolveTurnConfig(source: AgentLLMRequestSource | undefined): TurnRequestConfig | undefined {
if (source?.type !== 'turn') return undefined;
return this.getOrCreateTurnConfig(source.turnId);
}
private getOrCreateTurnConfig(turnId: number): TurnRequestConfig {
for (const id of this.turnConfigs.keys()) {
if (id < turnId) this.turnConfigs.delete(id);
}
let snapshot = this.turnConfigs.get(turnId);
if (snapshot === undefined) {
snapshot = {
resolved: this.profile.resolveModelContext(),
params: this.profile.resolveRequestParams(),
systemPrompt: this.profile.getSystemPrompt(),
};
this.turnConfigs.set(turnId, snapshot);
}
return snapshot;
}
private logRequest(input: LLMRequestLogInput): void {
const logFields: AgentLLMRequestLogFields = input.fields ?? {};
const wireTools = providerVisibleTools(input.tools);
const config = {
provider: input.protocol,
model: input.modelName,
modelAlias: input.modelAlias,
thinkingEffort: input.thinkingEffort ?? undefined,
systemPromptChars: input.systemPrompt.length,
toolCount: wireTools.length,
};
const signature = JSON.stringify({
...config,
systemPromptHash: fingerprint(input.systemPrompt),
toolsHash: fingerprint(JSON.stringify(toolSignature(wireTools))),
});
if (signature !== this.lastConfigLogSignature) {
this.lastConfigLogSignature = signature;
this.log.info('llm config', { ...logFields, ...config });
}
const partialMessageCount = input.messages.filter((message) => message.partial === true).length;
const requestFields: LogContext = { ...logFields };
if (partialMessageCount > 0) requestFields['partialMessageCount'] = partialMessageCount;
this.log.info('llm request', requestFields);
}
private recordRequest(input: LLMRequestLogInput): void {
const fields = input.fields ?? {};
const wireTools = providerVisibleTools(input.tools);
const tools = toolSignature(wireTools);
const toolsHash = fingerprint(JSON.stringify(tools));
if (!this.states.get(llmRequestTraceKey).seenToolsHashes.includes(toolsHash)) {
void this.dispatcher.dispatch(
new LlmToolsSnapshot({ agentId: this.scopeContext.agentId, hash: toolsHash, tools }),
);
}
const systemPromptHash = fingerprint(input.systemPrompt);
const overrides = this.config.get<ModelOverrides>('modelOverrides');
const thinkingConfig = this.config.get<ThinkingConfig>(THINKING_SECTION);
const modelConfig =
input.modelAlias === undefined ? undefined : this.modelService.get(input.modelAlias);
const payload: LlmRequestPayload = {
agentId: this.scopeContext.agentId,
kind: requestKindForRecord(fields),
provider: input.protocol,
model: input.modelName,
modelAlias: input.modelAlias,
thinkingEffort: input.thinkingEffort ?? undefined,
thinkingKeep: resolveThinkingKeep(
overrides?.thinkingKeep,
thinkingConfig?.keep,
input.thinkingEffort ?? 'off',
),
temperature: overrides?.temperature,
topP: overrides?.topP,
maxTokens: input.maxTokens,
betaApi: modelConfig?.betaApi,
toolSelect: this.toolSelect.enabled(),
systemPromptHash,
systemPrompt:
input.systemPrompt === this.profile.data().systemPrompt
? undefined
: input.systemPrompt,
toolsHash,
messageCount: input.messages.length,
turnStep: stringField(fields, 'turnStep'),
attempt: stringField(fields, 'attempt'),
projection: projectionField(fields),
droppedCount: numberField(fields, 'droppedCount'),
};
void this.dispatcher.dispatch(new LlmRequest(payload));
}
private logResponse(
fields: AgentLLMRequestLogFields | undefined,
usage: TokenUsage,
timing: ModelRequestTiming | undefined,
): void {
if (timing === undefined) return;
const payload: LogContext = {
...fields,
ttftMs: timing.firstTokenLatencyMs,
streamDurationMs: timing.streamDurationMs,
outputTokens: usage.output,
};
if (timing.requestBuildMs !== undefined) payload['requestBuildMs'] = timing.requestBuildMs;
if (timing.serverFirstTokenMs !== undefined) {
payload['serverFirstTokenMs'] = timing.serverFirstTokenMs;
}
if (timing.serverDecodeMs !== undefined) payload['serverDecodeMs'] = timing.serverDecodeMs;
if (timing.clientConsumeMs !== undefined) payload['clientConsumeMs'] = timing.clientConsumeMs;
if (timing.clientBlockedMs !== undefined) payload['clientBlockedMs'] = timing.clientBlockedMs;
this.log.info('llm response', payload);
}
private defaultTools(): readonly Tool[] {
return this.toolSelect
.shapeTools(this.tools.list())
.map((tool) => ({
name: tool.name,
description: tool.description,
parameters: tool.parameters ?? EMPTY_TOOL_PARAMETERS,
deferred: tool.deferred,
}));
}
}
class MutableLLMRequestTrace implements LLMRequestTrace {
traceId: string | undefined;
set(traceId: string | undefined): void {
this.traceId = traceId;
}
}
function logFieldsForSource(source: AgentLLMRequestSource | undefined): AgentLLMRequestLogFields {
switch (source?.type) {
case 'turn':
return {
...source.logFields,
...(source.step === undefined
? {}
: { turnStep: `${String(source.turnId)}.${String(source.step)}` }),
};
case 'operation':
return {
...source.logFields,
...(source.requestKind === undefined ? {} : { requestKind: source.requestKind }),
};
default:
return {};
}
}
function requestKindForTelemetry(source: AgentLLMRequestSource | undefined): string | undefined {
if (source?.type === 'turn') return 'turn';
if (source?.type === 'operation') return source.requestKind ?? 'operation';
return undefined;
}
function providerVisibleTools(tools: readonly Tool[]): readonly Tool[] {
if (!tools.some((tool) => tool.deferred === true)) return tools;
return tools.filter((tool) => tool.deferred !== true);
}
function toolSignature(tools: readonly Tool[]): readonly LlmRequestToolSchema[] {
return tools.map(({ name, description, parameters }) => ({ name, description, parameters }));
}
function requestKindForRecord(fields: AgentLLMRequestLogFields): LlmRequestPayload['kind'] {
if (fields['kind'] === 'compaction') return 'compaction';
if (fields['requestKind'] === 'full_compaction') return 'compaction';
return 'loop';
}
function stringField(fields: AgentLLMRequestLogFields, key: string): string | undefined {
const value = fields[key];
return typeof value === 'string' ? value : undefined;
}
function numberField(fields: AgentLLMRequestLogFields, key: string): number | undefined {
const value = fields[key];
return typeof value === 'number' ? value : undefined;
}
function mediaPartCount(messages: readonly Message[]): number {
return messages.reduce(
(count, message) =>
count +
message.content.filter(
(part) =>
part.type === 'image_url' || part.type === 'audio_url' || part.type === 'video_url',
).length,
0,
);
}
type LlmRequestProjection = NonNullable<LlmRequestPayload['projection']>;
function projectionNameOf(policy: ProjectionPolicy | undefined): LlmRequestProjection | undefined {
if (policy?.structure === 'strict') {
if (policy.media === 'degraded') return 'strict-media-degraded';
if (typeof policy.media === 'object') return 'strict-media-stripped';
return 'strict';
}
if (policy === undefined) return undefined;
if (policy.media === 'degraded') return 'media-degraded';
if (typeof policy.media === 'object') return 'media-stripped';
return undefined;
}
function projectionField(fields: AgentLLMRequestLogFields): LlmRequestProjection | undefined {
const value = fields['projection'];
switch (value) {
case 'strict':
case 'media-degraded':
case 'media-stripped':
case 'strict-media-degraded':
case 'strict-media-stripped':
return value;
default:
return undefined;
}
}
function fingerprint(content: string): string {
return createHash('sha256').update(content).digest('hex');
}
function apiStatusCode(error: unknown): number | undefined {
const raw = unwrapErrorCause(error);
if (raw instanceof APIStatusError) return raw.statusCode;
if (typeof raw === 'object' && raw !== null) {
const statusCode = (raw as Record<string, unknown>)['statusCode'];
if (typeof statusCode === 'number') return statusCode;
const status = (raw as Record<string, unknown>)['status'];
if (typeof status === 'number') return status;
}
if (typeof error === 'object' && error !== null) {
const details = (error as Record<string, unknown>)['details'];
if (typeof details === 'object' && details !== null) {
const statusCode = (details as Record<string, unknown>)['statusCode'];
if (typeof statusCode === 'number') return statusCode;
}
}
return undefined;
}
function apiTraceId(error: unknown): string | undefined {
const raw = unwrapErrorCause(error);
if (raw instanceof APIStatusError && raw.traceId !== null) return raw.traceId;
if (typeof error === 'object' && error !== null) {
const details = (error as Record<string, unknown>)['details'];
if (typeof details === 'object' && details !== null) {
const traceId = (details as Record<string, unknown>)['traceId'];
if (typeof traceId === 'string') return traceId;
}
}
return undefined;
}
registerScopedService(
LifecycleScope.Agent,
IAgentLLMRequesterService,
AgentLLMRequesterService,
ScopeActivation.OnScopeCreated,
'llmRequester',
);
|