Skip to content

Commit f3d35b3

Browse files
committed
fix: review comment
chang default model for copilot refactor the message handler about copilot remove useless console ouput
1 parent 6e825b2 commit f3d35b3

File tree

11 files changed

+267
-113
lines changed

11 files changed

+267
-113
lines changed

packages/types/src/providers/copilot.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export const copilotDefaultModelId = "claude-sonnet-4"
1+
export const copilotDefaultModelId = "gpt-4.1"
22

33
export const GITHUB_CLIENT_ID = "Iv1.b507a08c87ecfe98"
44
export const GITHUB_DEVICE_CODE_URL = "https://github.com/login/device/code"

src/api/providers/fetchers/copilot.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,6 @@ export class CopilotAuthenticator {
173173
expires_in: deviceData.expires_in,
174174
interval: deviceData.interval || 5,
175175
})
176-
} else {
177-
// Fallback to console logging
178-
console.log(`\n🔐 Copilot Authentication Required`)
179-
console.log(`Please visit: ${deviceData.verification_uri}`)
180-
console.log(`Enter code: ${deviceData.user_code}`)
181-
console.log(`Waiting for authentication...\n`)
182176
}
183177

184178
// Step 3: Poll for access token
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { MessageHandlerStrategy, MessageHandlerContext } from "../types"
2+
import { CopilotAuthenticator } from "../../../../api/providers/fetchers/copilot"
3+
4+
/**
5+
* Strategy for handling authenticateCopilot message
6+
*/
7+
export class AuthenticateCopilotStrategy implements MessageHandlerStrategy {
8+
async handle(context: MessageHandlerContext): Promise<void> {
9+
const { provider } = context
10+
11+
// Start device code authentication for Copilot
12+
try {
13+
const authenticator = CopilotAuthenticator.getInstance()
14+
15+
// Set up callbacks
16+
authenticator.setDeviceCodeCallback((deviceInfo) => {
17+
provider.postMessageToWebview({
18+
type: "copilotDeviceCode",
19+
copilotDeviceCode: {
20+
user_code: deviceInfo.user_code,
21+
verification_uri: deviceInfo.verification_uri,
22+
expires_in: deviceInfo.expires_in,
23+
},
24+
})
25+
})
26+
27+
authenticator.setAuthTimeoutCallback((error) => {
28+
provider.postMessageToWebview({
29+
type: "copilotAuthError",
30+
error: error,
31+
})
32+
})
33+
34+
await authenticator.getApiKey() // This will trigger the device code flow
35+
provider.postMessageToWebview({
36+
type: "copilotAuthStatus",
37+
copilotAuthenticated: true,
38+
})
39+
} catch (error) {
40+
console.error("Failed to authenticate with Copilot:", error)
41+
provider.postMessageToWebview({
42+
type: "copilotAuthError",
43+
error: error instanceof Error ? error.message : "Authentication failed",
44+
})
45+
}
46+
}
47+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { MessageHandlerStrategy, MessageHandlerContext } from "../types"
2+
import { CopilotAuthenticator } from "../../../../api/providers/fetchers/copilot"
3+
4+
/**
5+
* Strategy for handling checkCopilotAuth message
6+
*/
7+
export class CheckCopilotAuthStrategy implements MessageHandlerStrategy {
8+
async handle(context: MessageHandlerContext): Promise<void> {
9+
const { provider } = context
10+
11+
try {
12+
const authenticator = CopilotAuthenticator.getInstance()
13+
const isAuthenticated = await authenticator.isAuthenticated()
14+
15+
provider.postMessageToWebview({
16+
type: "copilotAuthStatus",
17+
copilotAuthenticated: isAuthenticated,
18+
})
19+
} catch (error) {
20+
console.error("Failed to check Copilot authentication:", error)
21+
provider.postMessageToWebview({
22+
type: "copilotAuthStatus",
23+
copilotAuthenticated: false,
24+
})
25+
}
26+
}
27+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { MessageHandlerStrategy, MessageHandlerContext } from "../types"
2+
import { CopilotAuthenticator } from "../../../../api/providers/fetchers/copilot"
3+
4+
/**
5+
* Strategy for handling clearCopilotAuth message
6+
*/
7+
export class ClearCopilotAuthStrategy implements MessageHandlerStrategy {
8+
async handle(context: MessageHandlerContext): Promise<void> {
9+
const { provider } = context
10+
11+
try {
12+
const authenticator = CopilotAuthenticator.getInstance()
13+
await authenticator.clearAuth()
14+
provider.postMessageToWebview({
15+
type: "copilotAuthStatus",
16+
copilotAuthenticated: false,
17+
})
18+
} catch (error) {
19+
console.error("Failed to clear Copilot authentication:", error)
20+
provider.postMessageToWebview({
21+
type: "copilotAuthError",
22+
error: error instanceof Error ? error.message : "Failed to clear authentication",
23+
})
24+
}
25+
}
26+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copilot message handling module
3+
*
4+
* This module implements the Strategy pattern for handling Copilot-related messages.
5+
* Each message type has its own dedicated handler that implements the MessageHandler interface.
6+
*
7+
* Supported message types:
8+
* - requestCopilotModels: Fetches available Copilot models
9+
* - authenticateCopilot: Initiates Copilot authentication flow
10+
* - clearCopilotAuth: Clears stored Copilot authentication
11+
* - checkCopilotAuth: Checks current Copilot authentication status
12+
*/
13+
14+
import { RequestCopilotModelsStrategy } from "./requestCopilotModels"
15+
import { AuthenticateCopilotStrategy } from "./authenticateCopilot"
16+
import { ClearCopilotAuthStrategy } from "./clearCopilotAuth"
17+
import { CheckCopilotAuthStrategy } from "./checkCopilotAuth"
18+
import { MessageHandlerRegistry } from "../types"
19+
20+
/**
21+
* Register all Copilot-related message handler strategies
22+
*/
23+
export function registerCopilotStrategies(messageHandlerRegistry: MessageHandlerRegistry): void {
24+
// Register each strategy with its corresponding message type
25+
messageHandlerRegistry.registerStrategy("requestCopilotModels", new RequestCopilotModelsStrategy())
26+
messageHandlerRegistry.registerStrategy("authenticateCopilot", new AuthenticateCopilotStrategy())
27+
messageHandlerRegistry.registerStrategy("clearCopilotAuth", new ClearCopilotAuthStrategy())
28+
messageHandlerRegistry.registerStrategy("checkCopilotAuth", new CheckCopilotAuthStrategy())
29+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { MessageHandlerStrategy, MessageHandlerContext } from "../types"
2+
import { getCopilotModels } from "../../../../api/providers/fetchers/copilot"
3+
import { ModelRecord } from "../../../../shared/api"
4+
5+
/**
6+
* Strategy for handling requestCopilotModels message
7+
*/
8+
export class RequestCopilotModelsStrategy implements MessageHandlerStrategy {
9+
async handle(context: MessageHandlerContext): Promise<void> {
10+
const { provider } = context
11+
12+
try {
13+
const copilotModels = await getCopilotModels()
14+
provider.postMessageToWebview({
15+
type: "copilotModels",
16+
copilotModels,
17+
})
18+
} catch (error) {
19+
console.error("Failed to fetch Copilot models:", error)
20+
provider.postMessageToWebview({
21+
type: "copilotModels",
22+
copilotModels: {},
23+
})
24+
}
25+
}
26+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from "./types"
2+
export * from "./registry"
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { registerCopilotStrategies } from "./copilot"
2+
import { MessageHandlerStrategy, MessageHandlerRegistry, MessageHandlerContext } from "./types"
3+
4+
/**
5+
* Central registry for message handler strategies
6+
* Implements strategy pattern with key-based registration
7+
*/
8+
export class DefaultMessageHandlerRegistry implements MessageHandlerRegistry {
9+
private static instance: DefaultMessageHandlerRegistry | null = null
10+
private strategies: Map<string, MessageHandlerStrategy> = new Map()
11+
12+
private constructor() {}
13+
14+
public static getInstance() {
15+
if (DefaultMessageHandlerRegistry.instance === null) {
16+
DefaultMessageHandlerRegistry.instance = new DefaultMessageHandlerRegistry()
17+
registerCopilotStrategies(DefaultMessageHandlerRegistry.instance)
18+
}
19+
return DefaultMessageHandlerRegistry.instance
20+
}
21+
22+
/**
23+
* Registers a strategy for handling a specific message type
24+
*/
25+
registerStrategy(messageType: string, strategy: MessageHandlerStrategy): void {
26+
this.strategies.set(messageType, strategy)
27+
}
28+
29+
/**
30+
* Gets a strategy for the given message type
31+
*/
32+
getStrategy(messageType: string): MessageHandlerStrategy | null {
33+
return this.strategies.get(messageType) || null
34+
}
35+
36+
/**
37+
* Gets all registered message types
38+
*/
39+
getSupportedTypes(): string[] {
40+
return Array.from(this.strategies.keys())
41+
}
42+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { ClineProvider } from "../ClineProvider"
2+
import { WebviewMessage } from "../../../shared/WebviewMessage"
3+
import { MarketplaceManager } from "../../../services/marketplace"
4+
5+
/**
6+
* Context provided to message handlers
7+
*/
8+
export interface MessageHandlerContext {
9+
/** The ClineProvider instance */
10+
provider: ClineProvider
11+
/** The webview message to handle */
12+
message: WebviewMessage
13+
/** Optional marketplace manager */
14+
marketplaceManager?: MarketplaceManager
15+
}
16+
17+
/**
18+
* Strategy interface for handling specific message types
19+
*/
20+
export interface MessageHandlerStrategy {
21+
/**
22+
* Handles a specific webview message type
23+
* @param context The message handler context
24+
*/
25+
handle(context: MessageHandlerContext): Promise<void>
26+
}
27+
28+
/**
29+
* Registry for message handler strategies
30+
*/
31+
export interface MessageHandlerRegistry {
32+
/**
33+
* Registers a strategy for handling a specific message type
34+
* @param messageType The type of message to handle
35+
* @param strategy The strategy to register
36+
*/
37+
registerStrategy(messageType: string, strategy: MessageHandlerStrategy): void
38+
39+
/**
40+
* Gets a strategy for the given message type
41+
* @param messageType The type of message to handle
42+
* @returns MessageHandlerStrategy instance or null if not supported
43+
*/
44+
getStrategy(messageType: string): MessageHandlerStrategy | null
45+
46+
/**
47+
* Gets all registered message types
48+
* @returns Array of supported message type strings
49+
*/
50+
getSupportedTypes(): string[]
51+
}

0 commit comments

Comments
 (0)