-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathtypes.ts
More file actions
484 lines (422 loc) · 11.9 KB
/
types.ts
File metadata and controls
484 lines (422 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
/**
* Type definitions for the Copilot SDK
*/
// Import and re-export generated session event types
import type { SessionEvent as GeneratedSessionEvent } from "./generated/session-events.js";
export type SessionEvent = GeneratedSessionEvent;
/**
* Options for creating a CopilotClient
*/
export interface CopilotClientOptions {
/**
* Path to the Copilot CLI executable
* @default "copilot" (searches PATH)
*/
cliPath?: string;
/**
* Extra arguments to pass to the CLI executable (inserted before SDK-managed args)
*/
cliArgs?: string[];
/**
* Working directory for the CLI process
* If not set, inherits the current process's working directory
*/
cwd?: string;
/**
* Port for the CLI server (TCP mode only)
* @default 0 (random available port)
*/
port?: number;
/**
* Use stdio transport instead of TCP
* When true, communicates with CLI via stdin/stdout pipes
* @default true
*/
useStdio?: boolean;
/**
* URL of an existing Copilot CLI server to connect to over TCP
* When provided, the client will not spawn a CLI process
* Format: "host:port" or "http://host:port" or just "port" (defaults to localhost)
* Examples: "localhost:8080", "http://127.0.0.1:9000", "8080"
* Mutually exclusive with cliPath, useStdio
*/
cliUrl?: string;
/**
* Log level for the CLI server
*/
logLevel?: "none" | "error" | "warning" | "info" | "debug" | "all";
/**
* Auto-start the CLI server on first use
* @default true
*/
autoStart?: boolean;
/**
* Auto-restart the CLI server if it crashes
* @default true
*/
autoRestart?: boolean;
/**
* Environment variables to pass to the CLI process. If not set, inherits process.env.
*/
env?: Record<string, string | undefined>;
}
/**
* Configuration for creating a session
*/
export type ToolResultType = "success" | "failure" | "rejected" | "denied";
export type ToolBinaryResult = {
data: string;
mimeType: string;
type: string;
description?: string;
};
export type ToolResultObject = {
textResultForLlm: string;
binaryResultsForLlm?: ToolBinaryResult[];
resultType: ToolResultType;
error?: string;
sessionLog?: string;
toolTelemetry?: Record<string, unknown>;
};
export type ToolResult = string | ToolResultObject;
export interface ToolInvocation {
sessionId: string;
toolCallId: string;
toolName: string;
arguments: unknown;
}
export type ToolHandler<TArgs = unknown> = (
args: TArgs,
invocation: ToolInvocation
) => Promise<unknown> | unknown;
/**
* Zod-like schema interface for type inference.
* Any object with `toJSONSchema()` method is treated as a Zod schema.
*/
export interface ZodSchema<T = unknown> {
_output: T;
toJSONSchema(): Record<string, unknown>;
}
/**
* Tool definition. Parameters can be either:
* - A Zod schema (provides type inference for handler)
* - A raw JSON schema object
* - Omitted (no parameters)
*/
export interface Tool<TArgs = unknown> {
name: string;
description?: string;
parameters?: ZodSchema<TArgs> | Record<string, unknown>;
handler: ToolHandler<TArgs>;
}
/**
* Helper to define a tool with Zod schema and get type inference for the handler.
* Without this helper, TypeScript cannot infer handler argument types from Zod schemas.
*/
export function defineTool<T = unknown>(
name: string,
config: {
description?: string;
parameters?: ZodSchema<T> | Record<string, unknown>;
handler: ToolHandler<T>;
}
): Tool<T> {
return { name, ...config };
}
export interface ToolCallRequestPayload {
sessionId: string;
toolCallId: string;
toolName: string;
arguments: unknown;
}
export interface ToolCallResponsePayload {
result: ToolResult;
}
/**
* Append mode: Use CLI foundation with optional appended content (default).
*/
export interface SystemMessageAppendConfig {
mode?: "append";
/**
* Additional instructions appended after SDK-managed sections.
*/
content?: string;
}
/**
* Replace mode: Use caller-provided system message entirely.
* Removes all SDK guardrails including security restrictions.
*/
export interface SystemMessageReplaceConfig {
mode: "replace";
/**
* Complete system message content.
* Replaces the entire SDK-managed system message.
*/
content: string;
}
/**
* System message configuration for session creation.
* - Append mode (default): SDK foundation + optional custom content
* - Replace mode: Full control, caller provides entire system message
*/
export type SystemMessageConfig = SystemMessageAppendConfig | SystemMessageReplaceConfig;
/**
* Permission request types from the server
*/
export interface PermissionRequest {
kind: "shell" | "write" | "mcp" | "read" | "url";
toolCallId?: string;
[key: string]: unknown;
}
export interface PermissionRequestResult {
kind:
| "approved"
| "denied-by-rules"
| "denied-no-approval-rule-and-could-not-request-from-user"
| "denied-interactively-by-user";
rules?: unknown[];
}
export type PermissionHandler = (
request: PermissionRequest,
invocation: { sessionId: string }
) => Promise<PermissionRequestResult> | PermissionRequestResult;
// ============================================================================
// MCP Server Configuration Types
// ============================================================================
/**
* Base interface for MCP server configuration.
*/
interface MCPServerConfigBase {
/**
* List of tools to include from this server. [] means none. "*" means all.
*/
tools: string[];
/**
* Indicates "remote" or "local" server type.
* If not specified, defaults to "local".
*/
type?: string;
/**
* Optional timeout in milliseconds for tool calls to this server.
*/
timeout?: number;
}
/**
* Configuration for a local/stdio MCP server.
*/
export interface MCPLocalServerConfig extends MCPServerConfigBase {
type?: "local" | "stdio";
command: string;
args: string[];
/**
* Environment variables to pass to the server.
*/
env?: Record<string, string>;
cwd?: string;
}
/**
* Configuration for a remote MCP server (HTTP or SSE).
*/
export interface MCPRemoteServerConfig extends MCPServerConfigBase {
type: "http" | "sse";
/**
* URL of the remote server.
*/
url: string;
/**
* Optional HTTP headers to include in requests.
*/
headers?: Record<string, string>;
}
/**
* Union type for MCP server configurations.
*/
export type MCPServerConfig = MCPLocalServerConfig | MCPRemoteServerConfig;
// ============================================================================
// Custom Agent Configuration Types
// ============================================================================
/**
* Configuration for a custom agent.
*/
export interface CustomAgentConfig {
/**
* Unique name of the custom agent.
*/
name: string;
/**
* Display name for UI purposes.
*/
displayName?: string;
/**
* Description of what the agent does.
*/
description?: string;
/**
* List of tool names the agent can use.
* Use null or undefined for all tools.
*/
tools?: string[] | null;
/**
* The prompt content for the agent.
*/
prompt: string;
/**
* MCP servers specific to this agent.
*/
mcpServers?: Record<string, MCPServerConfig>;
/**
* Whether the agent should be available for model inference.
* @default true
*/
infer?: boolean;
}
export interface SessionConfig {
/**
* Optional custom session ID
* If not provided, server will generate one
*/
sessionId?: string;
/**
* Model to use for this session
*/
model?: string;
/**
* Override the default configuration directory location.
* When specified, the session will use this directory for storing config and state.
*/
configDir?: string;
/**
* Tools exposed to the CLI server
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
tools?: Tool<any>[];
/**
* System message configuration
* Controls how the system prompt is constructed
*/
systemMessage?: SystemMessageConfig;
/**
* List of tool names to allow. When specified, only these tools will be available.
* Takes precedence over excludedTools.
*/
availableTools?: string[];
/**
* List of tool names to disable. All other tools remain available.
* Ignored if availableTools is specified.
*/
excludedTools?: string[];
/**
* Custom provider configuration (BYOK - Bring Your Own Key).
* When specified, uses the provided API endpoint instead of the Copilot API.
*/
provider?: ProviderConfig;
/**
* Handler for permission requests from the server.
* When provided, the server will call this handler to request permission for operations.
*/
onPermissionRequest?: PermissionHandler;
/*
* Enable streaming of assistant message and reasoning chunks.
* When true, ephemeral assistant.message_delta and assistant.reasoning_delta
* events are sent as the response is generated. Clients should accumulate
* deltaContent values to build the full response.
* @default false
*/
streaming?: boolean;
/**
* MCP server configurations for the session.
* Keys are server names, values are server configurations.
*/
mcpServers?: Record<string, MCPServerConfig>;
/**
* Custom agent configurations for the session.
*/
customAgents?: CustomAgentConfig[];
}
/**
* Configuration for resuming a session
*/
export type ResumeSessionConfig = Pick<
SessionConfig,
"tools" | "provider" | "streaming" | "onPermissionRequest" | "mcpServers" | "customAgents"
>;
/**
* Configuration for a custom API provider.
*/
export interface ProviderConfig {
/**
* Provider type. Defaults to "openai" for generic OpenAI-compatible APIs.
*/
type?: "openai" | "azure" | "anthropic";
/**
* API format (openai/azure only). Defaults to "completions".
*/
wireApi?: "completions" | "responses";
/**
* API endpoint URL
*/
baseUrl: string;
/**
* API key. Optional for local providers like Ollama.
*/
apiKey?: string;
/**
* Bearer token for authentication. Sets the Authorization header directly.
* Use this for services requiring bearer token auth instead of API key.
* Takes precedence over apiKey when both are set.
*/
bearerToken?: string;
/**
* Azure-specific options
*/
azure?: {
/**
* API version. Defaults to "2024-10-21".
*/
apiVersion?: string;
};
}
/**
* Options for sending a message to a session
*/
export interface MessageOptions {
/**
* The prompt/message to send
*/
prompt: string;
/**
* File or directory attachments
*/
attachments?: Array<{
type: "file" | "directory";
path: string;
displayName?: string;
}>;
/**
* Message delivery mode
* - "enqueue": Add to queue (default)
* - "immediate": Send immediately
*/
mode?: "enqueue" | "immediate";
}
/**
* Event handler callback type
*/
export type SessionEventHandler = (event: SessionEvent) => void;
/**
* Connection state
*/
export type ConnectionState = "disconnected" | "connecting" | "connected" | "error";
/**
* Metadata about a session
*/
export interface SessionMetadata {
sessionId: string;
startTime: Date;
modifiedTime: Date;
summary?: string;
isRemote: boolean;
}