Path: blob/main/src/vs/workbench/contrib/chat/common/model/chatUri.ts
5281 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 getNewSessionUri(): URI {26const handle = Math.floor(Math.random() * 1e9);27return forSession(`chat-${handle}`);28}2930export function parseLocalSessionId(resource: URI): string | undefined {31const parsed = parse(resource);32return parsed?.chatSessionType === localChatSessionType ? parsed.sessionId : undefined;33}3435export function isLocalSession(resource: URI): boolean {36return !!parseLocalSessionId(resource);37}3839function parse(resource: URI): ChatSessionIdentifier | undefined {40if (resource.scheme !== scheme) {41return undefined;42}4344if (!resource.authority) {45return undefined;46}4748const parts = resource.path.split('/');49if (parts.length !== 2) {50return undefined;51}5253const chatSessionType = resource.authority;54const decodedSessionId = decodeBase64(parts[1]);55return { chatSessionType, sessionId: new TextDecoder().decode(decodedSessionId.buffer) };56}57}5859/**60* Converts a chat session resource URI to a string ID.61*62* This exists mainly for backwards compatibility with existing code that uses string IDs in telemetry and storage.63*/64export function chatSessionResourceToId(resource: URI): string {65// If we have a local session, prefer using just the id part66const localId = LocalChatSessionUri.parseLocalSessionId(resource);67if (localId) {68return localId;69}7071return resource.toString();72}7374/**75* Extracts the chat session type from a resource URI.76*77* @param resource - The chat session resource URI78* @returns The session type string. Returns `localChatSessionType` for local sessions79* (vscodeChatEditor and vscodeLocalChatSession schemes), or the scheme/authority80* for contributed sessions.81*/82export function getChatSessionType(resource: URI): string {83if (resource.scheme === Schemas.vscodeChatEditor) {84return localChatSessionType;85}8687if (resource.scheme === LocalChatSessionUri.scheme) {88return resource.authority || localChatSessionType;89}9091return resource.scheme;92}939495