Path: blob/main/extensions/copilot/src/extension/chatSessions/claude/node/claudeCodeSdkService.ts
13405 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 type { ForkSessionOptions, ForkSessionResult, GetSubagentMessagesOptions, ListSubagentsOptions, Options, Query, SDKSessionInfo, SDKUserMessage, SessionMessage } from '@anthropic-ai/claude-agent-sdk';6import { createServiceIdentifier } from '../../../../util/common/services';78export interface IClaudeCodeSdkService {9readonly _serviceBrand: undefined;1011/**12* Creates a new Claude Code query generator13* @param options Query options including prompt and configuration14* @returns Query instance for Claude Code responses15*/16query(options: {17prompt: AsyncIterable<SDKUserMessage>;18options: Options;19}): Promise<Query>;2021/**22* Lists all Claude Code sessions for the specified project directory23* @param dir Workspace/project directory path (the SDK resolves this to the session storage location internally)24* @returns Array of session info objects25*/26listSessions(dir?: string): Promise<SDKSessionInfo[]>;2728/**29* Gets detailed information for a specific session30* @param sessionId Session ID31* @param dir Workspace/project directory path (the SDK resolves this to the session storage location internally)32* @returns Session info object, or undefined if not found33*/34getSessionInfo(sessionId: string, dir?: string): Promise<SDKSessionInfo | undefined>;3536/**37* Gets all messages for a specific session38* @param sessionId Session ID39* @param dir Workspace/project directory path (the SDK resolves this to the session storage location internally)40* @returns Array of session messages41*/42getSessionMessages(sessionId: string, dir?: string): Promise<SessionMessage[]>;4344/**45* Renames a session by setting a custom title46* @param sessionId Session ID47* @param title New title for the session48*/49renameSession(sessionId: string, title: string): Promise<void>;5051/**52* Forks an existing session to create a new one, optionally with a subset of messages53* @param sessionId Session ID54* @param options Fork session options55*/56forkSession(sessionId: string, options?: ForkSessionOptions): Promise<ForkSessionResult>;5758/**59* Lists subagent IDs for a given session60* @param sessionId Session ID61* @param options Optional dir to narrow the project search62* @returns Array of subagent ID strings63*/64listSubagents(sessionId: string, options?: ListSubagentsOptions): Promise<string[]>;6566/**67* Gets messages for a specific subagent in a session68* @param sessionId Parent session ID69* @param agentId Subagent ID70* @param options Optional dir, limit, and offset71* @returns Array of session messages72*/73getSubagentMessages(sessionId: string, agentId: string, options?: GetSubagentMessagesOptions): Promise<SessionMessage[]>;74}757677export const IClaudeCodeSdkService = createServiceIdentifier<IClaudeCodeSdkService>('IClaudeCodeSdkService');7879/**80* Service that wraps the Claude Code SDK for DI in tests and lazy loading81*/82export class ClaudeCodeSdkService implements IClaudeCodeSdkService {83readonly _serviceBrand: undefined;8485private _sdk: Promise<typeof import('@anthropic-ai/claude-agent-sdk')> | undefined;8687private _loadSdk() {88this._sdk ??= import('@anthropic-ai/claude-agent-sdk');89return this._sdk;90}9192public async query(options: {93prompt: AsyncIterable<SDKUserMessage>;94options: Options;95}): Promise<Query> {96const { query } = await this._loadSdk();97return query(options);98}99100public async listSessions(dir?: string): Promise<SDKSessionInfo[]> {101const { listSessions } = await this._loadSdk();102return listSessions(dir !== undefined ? { dir } : undefined);103}104105public async getSessionInfo(sessionId: string, dir?: string): Promise<SDKSessionInfo | undefined> {106const { getSessionInfo } = await this._loadSdk();107return getSessionInfo(sessionId, dir !== undefined ? { dir } : undefined);108}109110public async getSessionMessages(sessionId: string, dir?: string): Promise<SessionMessage[]> {111const { getSessionMessages } = await this._loadSdk();112return getSessionMessages(sessionId, dir !== undefined ? { dir } : undefined);113}114115public async renameSession(sessionId: string, title: string): Promise<void> {116const { renameSession } = await this._loadSdk();117await renameSession(sessionId, title);118}119120public async forkSession(sessionId: string, options?: ForkSessionOptions): Promise<ForkSessionResult> {121const { forkSession } = await this._loadSdk();122return forkSession(sessionId, options);123}124125public async listSubagents(sessionId: string, options?: ListSubagentsOptions): Promise<string[]> {126const { listSubagents } = await this._loadSdk();127return listSubagents(sessionId, options);128}129130public async getSubagentMessages(sessionId: string, agentId: string, options?: GetSubagentMessagesOptions): Promise<SessionMessage[]> {131const { getSubagentMessages } = await this._loadSdk();132return getSubagentMessages(sessionId, agentId, options);133}134}135136137