1
>
/*---------------------------------------------------------------------------------------------
copilotAgent.ts
2
>
* Copyright (c) Microsoft Corporation. All rights reserved.
3
>
* Licensed under the MIT License. See License.txt in the project root for license information.
4
>
*--------------------------------------------------------------------------------------------*/
5
>
6
>
import { CopilotClient, RuntimeConnection, type CopilotClientOptions, type GitHubTelemetryNotification } from '@github/copilot-sdk';
7
>
import * as fs from 'fs/promises';
8
>
import * as os from 'os';
9
>
import { pathToFileURL } from 'url';
10
>
import { CancelablePromise, createCancelablePromise, DeferredPromise, Delayer, disposableTimeout, Limiter, SequencerByKey } from '../../../../base/common/async.js';
11
>
import { type CancellationToken } from '../../../../base/common/cancellation.js';
12
>
import { CancellationError } from '../../../../base/common/errors.js';
13
>
import { Emitter, Event } from '../../../../base/common/event.js';
14
>
import { combinedDisposable, Disposable, DisposableMap, MutableDisposable } from '../../../../base/common/lifecycle.js';
15
>
import { ResourceMap } from '../../../../base/common/map.js';
16
>
import { FileAccess } from '../../../../base/common/network.js';
17
>
import { formatTokenCount } from '../../../../base/common/numbers.js';
18
>
import { equals } from '../../../../base/common/objects.js';
19
>
import { autorun, observableValue, type ISettableObservable } from '../../../../base/common/observable.js';
20
>
import { delimiter, dirname, join } from '../../../../base/common/path.js';
21
>
import { basename as resourceBasename, isEqual, isEqualOrParent, joinPath as resourceJoinPath, relativePath } from '../../../../base/common/resources.js';
22
>
import { URI } from '../../../../base/common/uri.js';
23
>
import { generateUuid } from '../../../../base/common/uuid.js';
24
>
import { rgDiskPath } from '../../../../base/node/ripgrep.js';
25
>
import { localize } from '../../../../nls.js';
26
>
import { IParsedAgent, IParsedPlugin, IParsedRule, IParsedSkill, parseAgentFile, parsePlugin, parseRuleFile, parseSkillFile, PluginFormat } from '../../../agentPlugins/common/pluginParsers.js';
27
>
import { AiAgentEnvValue, AiAgentEnvVar } from '../../../chat/common/aiAgentEnv.js';
28
>
import { IFileService } from '../../../files/common/files.js';
29
>
import { IInstantiationService } from '../../../instantiation/common/instantiation.js';
30
>
import { ILogService, LogLevel } from '../../../log/common/log.js';
31
>
import { ITelemetryService } from '../../../telemetry/common/telemetry.js';
32
>
import { INativeEnvironmentService } from '../../../../platform/environment/common/environment.js';
33
>
import { workspacelessScratchDir } from '../workspacelessScratchDir.js';
34
>
import { IAgentHostCheckpointService } from '../../common/agentHostCheckpointService.js';
35
>
import { IAgentHostReviewService } from '../../common/agentHostReviewService.js';
36
>
import { createPricingMetaFromBilling, hasLongContextSurcharge, normalizeCAPIBilling, type ICAPIModelBilling } from '../../common/agentModelPricing.js';
37
>
import { createAgentModelByokMeta } from '../../common/agentModelByokMeta.js';
38
>
import { AgentHostConfigKey, agentHostCustomizationConfigSchema, DEFAULT_SESSION_CUSTOMIZATION_DISCOVERY_MODE, toContainerCustomization } from '../../common/agentHostCustomizationConfig.js';
39
>
import { CopilotCliConfigKey, copilotCliConfigSchema, type CopilotSdkLogLevelSetting } from '../../common/copilotCliConfig.js';
40
>
import { AgentHostMcpServersConfigKey, AgentHostPreferLongContextEnabledConfigKey, AgentHostSessionSyncEnabledConfigKey, AgentHostSystemProxyEnabledConfigKey, AutoApproveLevel, SessionMode, migrateLegacyAutopilotConfig, platformRootSchema, platformSessionSchema, type AgentHostMcpServers } from '../../common/agentHostSchema.js';
41
>
import { IAgentPluginManager, ISyncedCustomization } from '../../common/agentPluginManager.js';
42
>
import { AgentSessionEntry, decodeProviderData, encodeProviderData, prepareSideChatPrompt, stripSideChatContext, type IPersistedChat } from '../agentPeerChats.js';
43
>
import { AgentSession, AgentSignal, AuthenticateParams, IActiveClient, IAgent, IAgentChatDataChange, IAgentChats, IAgentLegacyChat, IAgentCreateChatForkSource, IAgentCreateChatOptions, IAgentCreateChatResult, IAgentCreateSessionConfig, IAgentCreateSessionResult, IAgentDescriptor, IAgentHostManagedSettingsSnapshot, IAgentHostNetworkEndpoint, IAgentMaterializeSessionEvent, IAgentModelInfo, IAgentResolveSessionConfigParams, IAgentSessionConfigCompletionsParams, IAgentSessionMetadata, IAgentSessionProjectInfo, IAgentSpawnChatEvent, IMcpNotification, IRestoredSubagentSession, SubagentChatSignal } from '../../common/agentService.js';
44
>
import { getReasoningEffortDescription, getReasoningEffortLabel } from '../../common/reasoningEffort.js';
45
>
import type { IAgentServerToolHost } from '../../common/agentServerTools.js';
46
>
import { IAgentHostOTelService } from '../../common/otel/agentHostOTelService.js';
47
>
import { SessionConfigKey } from '../../common/sessionConfigKeys.js';
48
>
import { ICopilotConfigSlashCommandState } from '../../common/copilotConfigSlashCommands.js';
49
>
import { getCopilotHomePath } from '../../common/copilotHome.js';
50
>
import { ISessionDataService, SESSION_DB_FILENAME } from '../../common/sessionDataService.js';
51
>
import { IAgentHostProxyResolver } from '../agentHostProxyResolver.js';
52
>
import type { ResolveSessionConfigResult, SessionConfigCompletionsResult } from '../../common/state/protocol/commands.js';
53
>
import { ProtectedResourceMetadata, type AgentSelection, type ChildCustomizationType, type ConfigPropertySchema, type ConfigSchema, type ModelSelection, type ToolDefinition } from '../../common/state/protocol/state.js';
54
>
import { ActionType, type SessionAction } from '../../common/state/sessionActions.js';
55
>
import { AgentCustomization, CustomizationLoadStatus, CustomizationType, RuleCustomization, ChatInputResponseKind, SkillCustomization, customizationId, buildChatUri, buildDefaultChatUri, isDefaultChatUri, parseChatUri, parseRequiredSessionUriFromChatUri, parseSubagentSessionUri, AH_META_WORKSPACELESS_DB_KEY, type ChildCustomization, type ClientPluginCustomization, type Customization, type DirectoryCustomization, type HookCustomization, type MessageAttachment, type PendingMessage, type PluginCustomization, type PolicyState, type ChatInputAnswer, type ToolCallResult, type Turn } from '../../common/state/sessionState.js';
56
>
import { ActiveClientToolSet } from '../activeClientState.js';
57
>
import { IAgentConfigurationService } from '../agentConfigurationService.js';
58
>
import { IAgentHostGitHubEndpointService } from '../agentHostGitHubEndpointService.js';
59
>
import { IAgentHostCompletions } from '../agentHostCompletions.js';
60
>
import { IAgentHostGitService } from '../../common/agentHostGitService.js';
61
>
import { applyMcpServerEnablement, findMcpChildId, type IMcpServerRuntimeState } from '../shared/mcpCustomizationController.js';
62
>
import { AgentHostStateManager, IAgentHostStateManager } from '../agentHostStateManager.js';
63
>
64
>
interface ICopilotRuntimeManagedSettingsSdk {
65
>
getManagedSettings(input?: { token?: string; host?: string }): Promise<{ account?: string; resolved: IAgentHostManagedSettingsSnapshot }>;
66
>
}
67
>
68
function isCopilotRuntimeManagedSettingsSdk(value: unknown): value is ICopilotRuntimeManagedSettingsSdk {
69
return typeof value === 'object' && value !== null && 'getManagedSettings' in value
70
&& typeof (value as { getManagedSettings?: unknown }).getManagedSettings === 'function';
71
}
72
>
import { IByokLmBridgeRegistry } from '../byokLmBridgeRegistry.js';
copilotAgent.ts
73
>
import { SessionWorkingDirectoryMissingError } from '../shared/worktreeIsolation.js';
74
>
import { buildSessionEventLogFromTurns } from './buildSessionEvents.js';
75
>
import { CopilotAgentSession, type CopilotSdkMode } from './copilotAgentSession.js';
76
>
import { ICopilotSessionContext, projectFromCopilotContext } from './copilotGitProject.js';
77
>
import { parsedPluginsEqual, toChildCustomizations } from './copilotPluginConverters.js';
78
>
import { CopilotGitHubTelemetryForwarder } from './copilotGitHubTelemetryForwarder.js';
79
>
import { CopilotSessionLauncher, ContextSizeConfigKey, ThinkingLevelConfigKey, getCopilotContextTier, resolveCopilotReasoningEffort, type CopilotSessionLaunchPlan, type IActiveClientSnapshot } from './copilotSessionLauncher.js';
80
>
import { ShellManager } from './copilotShellTools.js';
81
>
import { isAgentHostTelemetryService } from '../agentHostTelemetryService.js';
82
>
import { ICopilotApiService, type IRestrictedTelemetryContext } from '../shared/copilotApiService.js';
83
>
import { AgentHostGitHubTelemetryRouter } from '../agentHostGitHubTelemetryRouter.js';
84
>
import { CopilotSlashCommandCompletionProvider, ICopilotRuntimeSlashCommandQueryOptions } from './copilotSlashCommandCompletionProvider.js';
85
>
import { DiscoveredType, SessionCustomizationDiscovery, areDiscoveredDirectoriesEqual, type IDiscoveredDirectory } from './sessionCustomizationDiscovery.js';
86
>
import { COPILOT_INTEGRATION_ID } from '../../../endpoint/common/licenseAgreement.js';
87
>
import { getAppNodeModulesPath } from '../appNodeModules.js';
88
>
import { CopilotSlashCommandProvider } from './copilotSlashCommandProvider.js';
89
>
90
>
const RUNTIME_SLASH_COMMAND_COMPLETION_WAIT_MS = 300;
91
>
const COPILOT_CAPI_URL = 'https://api.githubcopilot.com';
92
>
/**
93
>
* Proxy env vars that indicate the environment already configures a proxy.
94
>
*/
95
>
const COPILOT_PROXY_ENV_KEYS = ['HTTPS_PROXY', 'https_proxy', 'HTTP_PROXY', 'http_proxy', 'ALL_PROXY', 'all_proxy'] as const;
96
>
/**
97
>
* Proxy env vars we set when injecting the resolved CAPI proxy.
98
>
*/
99
>
const COPILOT_PROXY_SET_ENV_KEYS = ['HTTP_PROXY', 'HTTPS_PROXY'] as const;
100
>
101
async function fileExists(filePath: string): Promise<boolean> {
102
try {