1
>
/*---------------------------------------------------------------------------------------------
sessionState.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
>
// Immutable state types for the sessions process protocol.
7
>
// See protocol.md for the full design rationale.
8
>
//
9
>
// Most types are imported from the auto-generated protocol layer
10
>
// (synced from the agent-host-protocol repo). This file adds VS Code-specific
11
>
// helpers and re-exports.
12
>
13
>
import { decodeBase64, encodeBase64, VSBuffer } from '../../../../base/common/buffer.js';
14
>
import { hasKey, type Mutable } from '../../../../base/common/types.js';
15
>
import { URI as ResourceURI } from '../../../../base/common/uri.js';
16
>
import type { IProductService } from '../../../product/common/productService.js';
17
>
import { readToolCallMeta } from '../meta/agentToolCallMeta.js';
18
>
import {
19
>
ResponsePartKind,
20
>
SessionStatus,
21
>
ToolCallStatus,
22
>
SessionLifecycle,
23
>
TerminalState,
24
>
ToolResultContentType,
25
>
ToolResultFileEditContent,
26
>
ChatOriginKind,
27
>
ChatInteractivity,
28
>
type ActiveTurn,
29
>
type ChangesetState,
30
>
type ChatState,
31
>
type ChatSummary,
32
>
type PendingMessage,
33
>
type Turn,
34
>
type AnnotationsState,
35
>
type URI as ProtocolURI,
36
>
type RootState,
37
>
type SessionState,
38
>
type SessionSummary,
39
>
type TextRange,
40
>
type ToolCallCancelledState,
41
>
type ToolCallCompletedState,
42
>
type ToolCallResult,
43
>
type ToolCallState,
44
>
type ToolResultContent,
45
>
type ToolResultSubagentContent,
46
>
type ToolResultTextContent,
47
>
type UsageInfo,
48
>
type Message,
49
>
} from './protocol/state.js';
50
>
51
>
// Re-export everything from the protocol state module
52
>
export {
53
>
ChangesetOperationScope, ChangesetOperationStatus, ChangesetStatus, CustomizationLoadStatus,
54
>
CustomizationType, MessageAttachmentKind, MessageKind,
55
>
PendingMessageKind,
56
>
PolicyState,
57
>
ResponsePartKind,
58
>
ChatInputAnswerState as SessionInputAnswerState,
59
>
ChatInputAnswerValueKind as SessionInputAnswerValueKind,
60
>
ChatInputQuestionKind as SessionInputQuestionKind,
61
>
ChatInputResponseKind as SessionInputResponseKind,
62
>
ChatInteractivity,
63
>
ChatOriginKind,
64
>
SessionLifecycle,
65
>
SessionStatus, ToolCallCancellationReason, ToolCallConfirmationReason, ToolCallContributorKind, ToolCallRiskAssessmentKind, ToolCallRiskAssessmentStatus, ToolCallStatus,
66
>
ToolResultContentType,
67
>
TurnState, type ActiveTurn, type AgentCustomization, type AgentCapabilities, type AgentInfo, type AgentSelection, type Annotation, type AnnotationEntry, type AnnotationsState, type AnnotationsSummary, type Changeset, type ChangesetFile,
68
>
type ChangesetOperation, type ChangesetState, type ChatState, type ChatSummary, type ChatOrigin, type ChildCustomization, type ClientPluginCustomization, type ConfigPropertySchema,
69
>
type ConfigSchema,
70
>
type ContentRef, type Customization, type CustomizationDegradedState,
71
>
type CustomizationErrorState, type CustomizationLoadedState, type CustomizationLoadingState, type CustomizationLoadState, type DirectoryCustomization, type ErrorInfo, type HookCustomization, type FileEdit as ISessionFileDiff, type ToolResultEmbeddedResourceContent as IToolResultBinaryContent, type MarkdownResponsePart, type McpServerCustomization, type MessageAttachment,
72
>
type MessageResourceAttachment, type MessageEmbeddedResourceAttachment, type MessageAnnotationsAttachment, type ModelSelection, type PendingMessage, type PluginCustomization, type ProjectInfo, type PromptCustomization, type ReasoningResponsePart,
73
>
type ResponsePart,
74
>
type RootState, type RuleCustomization, type SessionActiveClient,
75
>
type SessionConfigState, type ChatInputAnswer as SessionInputAnswer,
76
>
type ChatInputOption as SessionInputOption, type ChatInputQuestion as SessionInputQuestion, type ChatInputRequest as SessionInputRequest, type SessionModelInfo,
77
>
type SessionState,
78
>
type SessionSummary, type SkillCustomization, type Snapshot, type StringOrMarkdown, type TerminalState, type TextRange,
79
>
type ToolAnnotations,
80
>
type ToolCallCancelledState,
81
>
type ToolCallCompletedState,
82
>
type ToolCallPendingConfirmationState,
83
>
type ToolCallPendingResultConfirmationState,
84
>
type ToolCallResponsePart,
85
>
type ToolCallResult,
86
>
type ToolCallRiskAssessment,
87
>
type ToolCallRiskAssessmentCompleteState,
88
>
type ToolCallRiskAssessmentLoadingState,
89
>
type ToolCallRunningState,
90
>
type ToolCallState,
91
>
type ToolCallStreamingState,
92
>
type ToolCallContributor,
93
>
type ToolDefinition, type ToolResultContent,
94
>
type ToolResultFileEditContent,
95
>
type TerminalCommandResult,
96
>
type ToolResultSubagentContent,
97
>
type ToolResultTerminalContent,
98
>
type ToolResultTextContent,
99
>
type Turn, type URI, type UsageInfo,
100
>
type Message
101
>
} from './protocol/state.js';
102
>
103
>
/**
104
>
* Well-known keys that may appear on {@link UsageInfo._meta}.
105
>
* Clients MAY read these to provide enhanced UI (e.g. credit cost display).
106
>
*/
107
>
export interface UsageInfoMeta {
108
>
/** Per-turn credit cost reported by the backend. */
109
>
cost?: number;
110
>
/** The concrete model selected by Copilot Auto and the routing explanation. */
111
>
autoModeResolved?: IAutoModeResolvedInfo;
112
>
/** Copilot-specific usage breakdown, including nano-AIU totals. */
113
>
copilotUsage?: {
114
>
totalNanoAiu?: number;
115
>
[key: string]: unknown;
116
>
};
117
>
/**
118
>
* Per-category account quota snapshots reported by the backend on the
119
>
* model-call usage event, keyed by quota type (e.g. `chat`,
120
>
* `premium_interactions`). Clients MAY use these to keep the account quota
121
>
* UI current without a separate quota fetch.
122
>
*/
123
>
quotaSnapshots?: {
124
>
[quotaType: string]: {
125
>
readonly isUnlimitedEntitlement?: boolean;
126
>
readonly entitlementRequests?: number;
127
>
readonly usedRequests?: number;
128
>
readonly remainingPercentage?: number;
129
>
readonly overage?: number;
130
>
readonly overageAllowedWithExhaustedQuota?: boolean;
131
>
/** ISO 8601 date when the quota resets, if applicable. */
132
>
readonly resetDate?: string;
133
>
} | undefined;
134
>
};
135
>
/**
136
>
* Per-source context-window attribution breakdown reported by the SDK's
137
>
* `session.rpc.metadata.getContextAttribution()`. Populated asynchronously
138
>
* after each usage event and piped to the context-usage widget as
139
>
* `promptTokenDetails`.
140
>
*/
141
>
contextAttribution?: IContextAttributionData;
142
>
[key: string]: unknown;
143
>
}
144
>
145
>
export interface IAutoModeResolvedInfo {
146
>
readonly chosenModel: string;
147
>
readonly reasoningBucket?: 'low' | 'medium' | 'high';
148
>
readonly categoryScores?: Readonly<Record<string, number | undefined>>;
149
>
readonly predictedLabel?: string;
150
>
readonly confidence?: number;
151
>
readonly candidateModels?: readonly string[];
152
>
}
153
>
154
>
/**
155
>
* Mirrors the SDK's `SessionContextAttribution` shape — a flat list of
156
>
* per-source entries describing what occupies the session's context window.
157
>
*/
158
>
export interface IContextAttributionData {
159
>
readonly totalTokens: number;
160
>
readonly entries: readonly IContextAttributionEntry[];
161
>
readonly compactions: { readonly count: number };
162
>
}
163
>
164
>
export interface IContextAttributionEntry {
165
>
readonly kind: string;
166
>
readonly id: string;
167
>
readonly label: string;
168
>
readonly tokens: number;
169
>
readonly parentId?: string;
170
>
readonly attributes?: Readonly<Record<string, string | undefined>>;
171
>
}
172
>
173
>
type AccountQuotaSnapshot = NonNullable<NonNullable<UsageInfoMeta['quotaSnapshots']>[string]>;
174
>
175
function readAccountQuotaSnapshot(value: unknown): AccountQuotaSnapshot | undefined {
176
if (!value || typeof value !== 'object' || Array.isArray(value)) {