@@ -8,25 +8,18 @@ import {
88} from "../../infra/diagnostic-trace-context.js" ;
99import { formatErrorMessage } from "../../infra/errors.js" ;
1010import { createSubsystemLogger } from "../../logging/subsystem.js" ;
11- import { parseAgentSessionKey } from "../../routing/session-key.js" ;
12- import { resolveUserPath } from "../../utils.js" ;
1311import { isDefaultAgentRuntimeId , normalizeOptionalAgentRuntimeId } from "../agent-runtime-id.js" ;
14- import { resolveAgentDir , resolveSessionAgentIds } from "../agent-scope.js" ;
1512import {
1613 resolveEffectiveToolPolicy ,
1714 resolveGroupToolPolicy ,
1815 resolveInheritedToolPolicyForSession ,
1916 resolveSubagentToolPolicyForSession ,
2017} from "../agent-tools.policy.js" ;
21- import type { CompactEmbeddedAgentSessionParams } from "../embedded-agent-runner/compact.types.js" ;
22- import { resolveModelAsync } from "../embedded-agent-runner/model.js" ;
2318import type {
2419 EmbeddedRunAttemptParams ,
2520 EmbeddedRunAttemptResult ,
2621} from "../embedded-agent-runner/run/types.js" ;
27- import type { EmbeddedAgentCompactResult } from "../embedded-agent-runner/types.js" ;
28- import { getApiKeyForModel } from "../model-auth.js" ;
29- import { isCliRuntimeAliasForProvider , isCliRuntimeProvider } from "../model-runtime-aliases.js" ;
22+ import { isCliRuntimeAliasForProvider } from "../model-runtime-aliases.js" ;
3023import { resolveSandboxRuntimeStatus } from "../sandbox/runtime-status.js" ;
3124import { resolveSenderToolPolicy } from "../sender-tool-policy.js" ;
3225import {
@@ -500,134 +493,6 @@ function logAgentHarnessSelection(
500493 } ) ;
501494}
502495
503- function resolveHarnessCompactIdentity ( params : CompactEmbeddedAgentSessionParams ) : {
504- agentDir : string ;
505- agentId : string ;
506- } {
507- const agentIds = resolveSessionAgentIds ( {
508- sessionKey : params . sessionKey ,
509- config : params . config ,
510- agentId : params . agentId ,
511- } ) ;
512- return {
513- agentDir : params . agentDir ?? resolveAgentDir ( params . config ?? { } , agentIds . sessionAgentId ) ,
514- agentId : params . agentId ?? agentIds . sessionAgentId ,
515- } ;
516- }
517-
518- async function resolveHarnessCompactApiKey ( params : {
519- agentDir : string ;
520- compactParams : CompactEmbeddedAgentSessionParams ;
521- } ) : Promise < string | undefined > {
522- const { agentDir, compactParams } = params ;
523- const existing = compactParams . resolvedApiKey ?. trim ( ) ;
524- if ( existing ) {
525- return existing ;
526- }
527- if (
528- ! compactParams . authProfileId ?. trim ( ) ||
529- ! compactParams . provider ?. trim ( ) ||
530- ! compactParams . model ?. trim ( )
531- ) {
532- return undefined ;
533- }
534- const workspaceDir = resolveUserPath ( compactParams . workspaceDir ) ;
535- const { model } = await resolveModelAsync (
536- compactParams . provider ,
537- compactParams . model ,
538- agentDir ,
539- compactParams . config ,
540- {
541- authProfileId : compactParams . authProfileId ,
542- workspaceDir,
543- } ,
544- ) ;
545- if ( ! model ) {
546- return undefined ;
547- }
548- const apiKeyInfo = await getApiKeyForModel ( {
549- model,
550- cfg : compactParams . config ,
551- profileId : compactParams . authProfileId ,
552- agentDir,
553- workspaceDir,
554- } ) ;
555- return apiKeyInfo . apiKey ?. trim ( ) || undefined ;
556- }
557-
558- export async function maybeCompactAgentHarnessSession (
559- params : CompactEmbeddedAgentSessionParams ,
560- ) : Promise < EmbeddedAgentCompactResult | undefined > {
561- if ( params . provider && isCliRuntimeProvider ( params . provider , { config : params . config } ) ) {
562- return undefined ;
563- }
564- const runtimePolicySessionKey = params . sandboxSessionKey ?? params . sessionKey ;
565- const runtimePolicyAgentId =
566- params . sandboxSessionKey && parseAgentSessionKey ( params . sandboxSessionKey )
567- ? undefined
568- : params . agentId ;
569- const runtime = resolveConfiguredAgentHarnessPolicy ( {
570- provider : params . provider ,
571- modelId : params . model ,
572- config : params . config ,
573- agentId : runtimePolicyAgentId ,
574- sessionKey : runtimePolicySessionKey ,
575- } ) . runtime ;
576- if ( isCliRuntimeAliasForProvider ( { runtime, provider : params . provider , cfg : params . config } ) ) {
577- return undefined ;
578- }
579- const selectedRuntime = normalizeOptionalAgentRuntimeId ( params . agentHarnessId ) ;
580- const agentHarnessRuntimeOverride =
581- selectedRuntime && ! isDefaultAgentRuntimeId ( selectedRuntime ) ? selectedRuntime : undefined ;
582- let harness : AgentHarness ;
583- try {
584- harness = selectAgentHarness ( {
585- provider : params . provider ?? "" ,
586- modelId : params . model ,
587- config : params . config ,
588- agentId : runtimePolicyAgentId ,
589- sessionKey : runtimePolicySessionKey ,
590- agentHarnessRuntimeOverride,
591- } ) ;
592- } catch ( err ) {
593- if ( agentHarnessRuntimeOverride ) {
594- const message = formatErrorMessage ( err ) ;
595- if ( message . includes ( "does not support" ) ) {
596- return undefined ;
597- }
598- }
599- throw err ;
600- }
601- if ( ! harness . compact ) {
602- if ( harness . id !== "openclaw" ) {
603- return {
604- ok : false ,
605- compacted : false ,
606- reason : `Agent harness "${ harness . id } " does not support compaction.` ,
607- failure : { reason : "unsupported_harness_compaction" } ,
608- } ;
609- }
610- return undefined ;
611- }
612- const compactIdentity = resolveHarnessCompactIdentity ( params ) ;
613- const compactParams = {
614- ...params ,
615- agentDir : compactIdentity . agentDir ,
616- agentId : compactIdentity . agentId ,
617- } ;
618- let resolvedApiKey : string | undefined ;
619- try {
620- resolvedApiKey = await resolveHarnessCompactApiKey ( {
621- agentDir : compactIdentity . agentDir ,
622- compactParams,
623- } ) ;
624- } catch ( err ) {
625- log . debug ( "agent harness compaction credential lookup failed" , {
626- error : formatErrorMessage ( err ) ,
627- } ) ;
628- }
629- return harness . compact ( resolvedApiKey ? { ...compactParams , resolvedApiKey } : compactParams ) ;
630- }
631496function formatProviderModel ( params : { provider : string ; modelId ?: string } ) : string {
632497 return params . modelId ? `${ params . provider } /${ params . modelId } ` : params . provider ;
633498}
0 commit comments