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
5281 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 getNewSessionUri(): URI {
27
const handle = Math.floor(Math.random() * 1e9);
28
return forSession(`chat-${handle}`);
29
}
30
31
export function parseLocalSessionId(resource: URI): string | undefined {
32
const parsed = parse(resource);
33
return parsed?.chatSessionType === localChatSessionType ? parsed.sessionId : undefined;
34
}
35
36
export function isLocalSession(resource: URI): boolean {
37
return !!parseLocalSessionId(resource);
38
}
39
40
function parse(resource: URI): ChatSessionIdentifier | undefined {
41
if (resource.scheme !== scheme) {
42
return undefined;
43
}
44
45
if (!resource.authority) {
46
return undefined;
47
}
48
49
const parts = resource.path.split('/');
50
if (parts.length !== 2) {
51
return undefined;
52
}
53
54
const chatSessionType = resource.authority;
55
const decodedSessionId = decodeBase64(parts[1]);
56
return { chatSessionType, sessionId: new TextDecoder().decode(decodedSessionId.buffer) };
57
}
58
}
59
60
/**
61
* Converts a chat session resource URI to a string ID.
62
*
63
* This exists mainly for backwards compatibility with existing code that uses string IDs in telemetry and storage.
64
*/
65
export function chatSessionResourceToId(resource: URI): string {
66
// If we have a local session, prefer using just the id part
67
const localId = LocalChatSessionUri.parseLocalSessionId(resource);
68
if (localId) {
69
return localId;
70
}
71
72
return resource.toString();
73
}
74
75
/**
76
* Extracts the chat session type from a resource URI.
77
*
78
* @param resource - The chat session resource URI
79
* @returns The session type string. Returns `localChatSessionType` for local sessions
80
* (vscodeChatEditor and vscodeLocalChatSession schemes), or the scheme/authority
81
* for contributed sessions.
82
*/
83
export function getChatSessionType(resource: URI): string {
84
if (resource.scheme === Schemas.vscodeChatEditor) {
85
return localChatSessionType;
86
}
87
88
if (resource.scheme === LocalChatSessionUri.scheme) {
89
return resource.authority || localChatSessionType;
90
}
91
92
return resource.scheme;
93
}
94
95