Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/chatSessions/claude/node/claudeCodeSdkService.ts
13405 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 type { ForkSessionOptions, ForkSessionResult, GetSubagentMessagesOptions, ListSubagentsOptions, Options, Query, SDKSessionInfo, SDKUserMessage, SessionMessage } from '@anthropic-ai/claude-agent-sdk';
7
import { createServiceIdentifier } from '../../../../util/common/services';
8
9
export interface IClaudeCodeSdkService {
10
readonly _serviceBrand: undefined;
11
12
/**
13
* Creates a new Claude Code query generator
14
* @param options Query options including prompt and configuration
15
* @returns Query instance for Claude Code responses
16
*/
17
query(options: {
18
prompt: AsyncIterable<SDKUserMessage>;
19
options: Options;
20
}): Promise<Query>;
21
22
/**
23
* Lists all Claude Code sessions for the specified project directory
24
* @param dir Workspace/project directory path (the SDK resolves this to the session storage location internally)
25
* @returns Array of session info objects
26
*/
27
listSessions(dir?: string): Promise<SDKSessionInfo[]>;
28
29
/**
30
* Gets detailed information for a specific session
31
* @param sessionId Session ID
32
* @param dir Workspace/project directory path (the SDK resolves this to the session storage location internally)
33
* @returns Session info object, or undefined if not found
34
*/
35
getSessionInfo(sessionId: string, dir?: string): Promise<SDKSessionInfo | undefined>;
36
37
/**
38
* Gets all messages for a specific session
39
* @param sessionId Session ID
40
* @param dir Workspace/project directory path (the SDK resolves this to the session storage location internally)
41
* @returns Array of session messages
42
*/
43
getSessionMessages(sessionId: string, dir?: string): Promise<SessionMessage[]>;
44
45
/**
46
* Renames a session by setting a custom title
47
* @param sessionId Session ID
48
* @param title New title for the session
49
*/
50
renameSession(sessionId: string, title: string): Promise<void>;
51
52
/**
53
* Forks an existing session to create a new one, optionally with a subset of messages
54
* @param sessionId Session ID
55
* @param options Fork session options
56
*/
57
forkSession(sessionId: string, options?: ForkSessionOptions): Promise<ForkSessionResult>;
58
59
/**
60
* Lists subagent IDs for a given session
61
* @param sessionId Session ID
62
* @param options Optional dir to narrow the project search
63
* @returns Array of subagent ID strings
64
*/
65
listSubagents(sessionId: string, options?: ListSubagentsOptions): Promise<string[]>;
66
67
/**
68
* Gets messages for a specific subagent in a session
69
* @param sessionId Parent session ID
70
* @param agentId Subagent ID
71
* @param options Optional dir, limit, and offset
72
* @returns Array of session messages
73
*/
74
getSubagentMessages(sessionId: string, agentId: string, options?: GetSubagentMessagesOptions): Promise<SessionMessage[]>;
75
}
76
77
78
export const IClaudeCodeSdkService = createServiceIdentifier<IClaudeCodeSdkService>('IClaudeCodeSdkService');
79
80
/**
81
* Service that wraps the Claude Code SDK for DI in tests and lazy loading
82
*/
83
export class ClaudeCodeSdkService implements IClaudeCodeSdkService {
84
readonly _serviceBrand: undefined;
85
86
private _sdk: Promise<typeof import('@anthropic-ai/claude-agent-sdk')> | undefined;
87
88
private _loadSdk() {
89
this._sdk ??= import('@anthropic-ai/claude-agent-sdk');
90
return this._sdk;
91
}
92
93
public async query(options: {
94
prompt: AsyncIterable<SDKUserMessage>;
95
options: Options;
96
}): Promise<Query> {
97
const { query } = await this._loadSdk();
98
return query(options);
99
}
100
101
public async listSessions(dir?: string): Promise<SDKSessionInfo[]> {
102
const { listSessions } = await this._loadSdk();
103
return listSessions(dir !== undefined ? { dir } : undefined);
104
}
105
106
public async getSessionInfo(sessionId: string, dir?: string): Promise<SDKSessionInfo | undefined> {
107
const { getSessionInfo } = await this._loadSdk();
108
return getSessionInfo(sessionId, dir !== undefined ? { dir } : undefined);
109
}
110
111
public async getSessionMessages(sessionId: string, dir?: string): Promise<SessionMessage[]> {
112
const { getSessionMessages } = await this._loadSdk();
113
return getSessionMessages(sessionId, dir !== undefined ? { dir } : undefined);
114
}
115
116
public async renameSession(sessionId: string, title: string): Promise<void> {
117
const { renameSession } = await this._loadSdk();
118
await renameSession(sessionId, title);
119
}
120
121
public async forkSession(sessionId: string, options?: ForkSessionOptions): Promise<ForkSessionResult> {
122
const { forkSession } = await this._loadSdk();
123
return forkSession(sessionId, options);
124
}
125
126
public async listSubagents(sessionId: string, options?: ListSubagentsOptions): Promise<string[]> {
127
const { listSubagents } = await this._loadSdk();
128
return listSubagents(sessionId, options);
129
}
130
131
public async getSubagentMessages(sessionId: string, agentId: string, options?: GetSubagentMessagesOptions): Promise<SessionMessage[]> {
132
const { getSubagentMessages } = await this._loadSdk();
133
return getSubagentMessages(sessionId, agentId, options);
134
}
135
}
136
137