Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/platform/endpoint/common/compactionDataContainer.tsx
13401 views
1
/*---------------------------------------------------------------------------------------------
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
import { BasePromptElementProps, PromptElement, Raw } from '@vscode/prompt-tsx';
6
import { OpenAIContextManagementResponse } from '../../networking/common/openai';
7
import { CustomDataPartMimeTypes } from './endpointTypes';
8
9
interface ICompactionDataOpaque {
10
type: typeof CustomDataPartMimeTypes.ContextManagement;
11
compaction: OpenAIContextManagementResponse;
12
}
13
14
export interface ICompactionDataContainerProps extends BasePromptElementProps {
15
compaction: OpenAIContextManagementResponse;
16
}
17
18
/**
19
* Helper element to embed compaction data into assistant messages
20
* as an opaque content part, for round-tripping in Responses API requests.
21
*/
22
export class CompactionDataContainer extends PromptElement<ICompactionDataContainerProps> {
23
render() {
24
const { compaction } = this.props;
25
const container: ICompactionDataOpaque = { type: CustomDataPartMimeTypes.ContextManagement, compaction };
26
return <opaque value={container} />;
27
}
28
}
29
30
/**
31
* Attempts to parse a Raw opaque content part into compaction data, if the type matches.
32
*/
33
export function rawPartAsCompactionData(part: Raw.ChatCompletionContentPartOpaque): OpenAIContextManagementResponse | undefined {
34
const value = part.value as unknown;
35
if (!value || typeof value !== 'object') {
36
return;
37
}
38
39
const data = value as ICompactionDataOpaque;
40
if (data.type === CustomDataPartMimeTypes.ContextManagement && data.compaction && typeof data.compaction === 'object') {
41
return data.compaction;
42
}
43
return;
44
}
45
46