Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/chat/common/model/chatUri.ts
4780 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
6
import { encodeBase64, VSBuffer, decodeBase64 } from '../../../../../base/common/buffer.js';
7
import { Schemas } from '../../../../../base/common/network.js';
8
import { URI } from '../../../../../base/common/uri.js';
9
import { localChatSessionType } from '../chatSessionsService.js';
10
11
type ChatSessionIdentifier = {
12
readonly chatSessionType: string;
13
readonly sessionId: string;
14
};
15
16
17
export namespace LocalChatSessionUri {
18
19
export const scheme = Schemas.vscodeLocalChatSession;
20
21
export function forSession(sessionId: string): URI {
22
const encodedId = encodeBase64(VSBuffer.wrap(new TextEncoder().encode(sessionId)), false, true);
23
return URI.from({ scheme, authority: localChatSessionType, path: '/' + encodedId });
24
}
25
26
export function parseLocalSessionId(resource: URI): string | undefined {
27
const parsed = parse(resource);
28
return parsed?.chatSessionType === localChatSessionType ? parsed.sessionId : undefined;
29
}
30
31
export function isLocalSession(resource: URI): boolean {
32
return !!parseLocalSessionId(resource);
33
}
34
35
function parse(resource: URI): ChatSessionIdentifier | undefined {
36
if (resource.scheme !== scheme) {
37
return undefined;
38
}
39
40
if (!resource.authority) {
41
return undefined;
42
}
43
44
const parts = resource.path.split('/');
45
if (parts.length !== 2) {
46
return undefined;
47
}
48
49
const chatSessionType = resource.authority;
50
const decodedSessionId = decodeBase64(parts[1]);
51
return { chatSessionType, sessionId: new TextDecoder().decode(decodedSessionId.buffer) };
52
}
53
}
54
55
/**
56
* Converts a chat session resource URI to a string ID.
57
*
58
* This exists mainly for backwards compatibility with existing code that uses string IDs in telemetry and storage.
59
*/
60
export function chatSessionResourceToId(resource: URI): string {
61
// If we have a local session, prefer using just the id part
62
const localId = LocalChatSessionUri.parseLocalSessionId(resource);
63
if (localId) {
64
return localId;
65
}
66
67
return resource.toString();
68
}
69
70
/**
71
* Extracts the chat session type from a resource URI.
72
*
73
* @param resource - The chat session resource URI
74
* @returns The session type string. Returns `localChatSessionType` for local sessions
75
* (vscodeChatEditor and vscodeLocalChatSession schemes), or the scheme/authority
76
* for contributed sessions.
77
*/
78
export function getChatSessionType(resource: URI): string {
79
if (resource.scheme === Schemas.vscodeChatEditor) {
80
return localChatSessionType;
81
}
82
83
if (resource.scheme === LocalChatSessionUri.scheme) {
84
return resource.authority || localChatSessionType;
85
}
86
87
return resource.scheme;
88
}
89
90