Path: blob/main/src/vs/workbench/contrib/chat/common/model/chatUri.ts
4780 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { encodeBase64, VSBuffer, decodeBase64 } from '../../../../../base/common/buffer.js';6import { Schemas } from '../../../../../base/common/network.js';7import { URI } from '../../../../../base/common/uri.js';8import { localChatSessionType } from '../chatSessionsService.js';910type ChatSessionIdentifier = {11readonly chatSessionType: string;12readonly sessionId: string;13};141516export namespace LocalChatSessionUri {1718export const scheme = Schemas.vscodeLocalChatSession;1920export function forSession(sessionId: string): URI {21const encodedId = encodeBase64(VSBuffer.wrap(new TextEncoder().encode(sessionId)), false, true);22return URI.from({ scheme, authority: localChatSessionType, path: '/' + encodedId });23}2425export function parseLocalSessionId(resource: URI): string | undefined {26const parsed = parse(resource);27return parsed?.chatSessionType === localChatSessionType ? parsed.sessionId : undefined;28}2930export function isLocalSession(resource: URI): boolean {31return !!parseLocalSessionId(resource);32}3334function parse(resource: URI): ChatSessionIdentifier | undefined {35if (resource.scheme !== scheme) {36return undefined;37}3839if (!resource.authority) {40return undefined;41}4243const parts = resource.path.split('/');44if (parts.length !== 2) {45return undefined;46}4748const chatSessionType = resource.authority;49const decodedSessionId = decodeBase64(parts[1]);50return { chatSessionType, sessionId: new TextDecoder().decode(decodedSessionId.buffer) };51}52}5354/**55* Converts a chat session resource URI to a string ID.56*57* This exists mainly for backwards compatibility with existing code that uses string IDs in telemetry and storage.58*/59export function chatSessionResourceToId(resource: URI): string {60// If we have a local session, prefer using just the id part61const localId = LocalChatSessionUri.parseLocalSessionId(resource);62if (localId) {63return localId;64}6566return resource.toString();67}6869/**70* Extracts the chat session type from a resource URI.71*72* @param resource - The chat session resource URI73* @returns The session type string. Returns `localChatSessionType` for local sessions74* (vscodeChatEditor and vscodeLocalChatSession schemes), or the scheme/authority75* for contributed sessions.76*/77export function getChatSessionType(resource: URI): string {78if (resource.scheme === Schemas.vscodeChatEditor) {79return localChatSessionType;80}8182if (resource.scheme === LocalChatSessionUri.scheme) {83return resource.authority || localChatSessionType;84}8586return resource.scheme;87}888990