1- import axios from 'axios' ;
2- import * as vscode from 'vscode' ;
3- import { BaseProvider } from './baseProvider' ;
4- import { GenerationOptions } from '../types' ;
5- import { ConfigurationManager } from '../utils/configurationManager' ;
1+ import * as vscode from "vscode" ;
2+
3+ import { BaseProvider } from "./baseProvider" ;
4+ import { ConfigurationManager } from "../utils/configurationManager" ;
5+ import { GenerationOptions } from "../types" ;
6+ import axios from "axios" ;
67
78export class AnthropicProvider extends BaseProvider {
8- private readonly API_KEY_SECRET = ' universal-commit-assistant.anthropic.apiKey' ;
9+ private readonly API_KEY_SECRET = " universal-commit-assistant.anthropic.apiKey" ;
910
10- constructor (
11- private readonly configManager : ConfigurationManager ,
12- private readonly secretStorage : vscode . SecretStorage
13- ) {
14- super ( ) ;
15- }
11+ constructor (
12+ private readonly configManager : ConfigurationManager ,
13+ private readonly secretStorage : vscode . SecretStorage
14+ ) {
15+ super ( ) ;
16+ }
1617
17- async generateCommitMessage ( changes : string , options ?: GenerationOptions ) : Promise < string > {
18- const apiKey = await this . getApiKey ( ) ;
19- const model = this . configManager . getAnthropicModel ( ) ;
20- const temperature = this . configManager . getTemperature ( ) ;
21- const systemPrompt = this . configManager . getSystemPrompt ( ) ;
22- const style = options ?. style || this . configManager . getMessageStyle ( ) ;
23- const language = this . configManager . getLanguage ( ) ;
24- const maxTokens = options ?. maxTokens || ( style === ' detailed' ? 300 : this . configManager . getMaxTokens ( ) ) ;
18+ async generateCommitMessage ( changes : string , options ?: GenerationOptions ) : Promise < string > {
19+ const apiKey = await this . getApiKey ( ) ;
20+ const model = this . configManager . getAnthropicModel ( ) ;
21+ const temperature = this . configManager . getTemperature ( ) ;
22+ const systemPrompt = this . configManager . getSystemPrompt ( ) ;
23+ const style = options ?. style || this . configManager . getMessageStyle ( ) ;
24+ const language = this . configManager . getLanguage ( ) ;
25+ const maxTokens = options ?. maxTokens || ( style === " detailed" ? 300 : this . configManager . getMaxTokens ( ) ) ;
2526
26- const prompt = this . buildPrompt ( changes , style , options ?. customPrompt , language ) ;
27+ const prompt = this . buildPrompt ( changes , style , options ?. customPrompt , language ) ;
2728
28- try {
29- const response = await axios . post (
30- ' https://api.anthropic.com/v1/messages' ,
31- {
32- model,
33- max_tokens : maxTokens ,
34- temperature : temperature ,
35- system : systemPrompt ,
36- messages : [
37- {
38- role : ' user' ,
39- content : prompt
40- }
41- ]
42- } ,
43- {
44- headers : {
45- ' x-api-key' : apiKey ,
46- ' Content-Type' : ' application/json' ,
47- ' anthropic-version' : ' 2023-06-01'
48- }
49- }
50- ) ;
29+ try {
30+ const response = await axios . post (
31+ " https://api.anthropic.com/v1/messages" ,
32+ {
33+ model,
34+ max_tokens : maxTokens ,
35+ temperature : temperature ,
36+ system : systemPrompt ,
37+ messages : [
38+ {
39+ role : " user" ,
40+ content : prompt ,
41+ } ,
42+ ] ,
43+ } ,
44+ {
45+ headers : {
46+ " x-api-key" : apiKey ,
47+ " Content-Type" : " application/json" ,
48+ " anthropic-version" : " 2023-06-01" ,
49+ } ,
50+ }
51+ ) ;
5152
52- const message = response . data . content [ 0 ] ?. text ;
53- if ( ! message ) {
54- throw new Error ( ' No response from Anthropic' ) ;
55- }
53+ const message = response . data . content [ 0 ] ?. text ;
54+ if ( ! message ) {
55+ throw new Error ( " No response from Anthropic" ) ;
56+ }
5657
57- return this . validateResponse ( message , style ) ;
58- } catch ( error ) {
59- if ( axios . isAxiosError ( error ) ) {
60- throw new Error ( `Anthropic API error: ${ error . response ?. data ?. error ?. message || error . message } ` ) ;
61- }
62- throw error ;
63- }
58+ return this . validateResponse ( message , style ) ;
59+ } catch ( error ) {
60+ if ( axios . isAxiosError ( error ) ) {
61+ throw new Error ( `Anthropic API error: ${ error . response ?. data ?. error ?. message || error . message } ` ) ;
62+ }
63+ throw error ;
6464 }
65+ }
6566
66- async validateConfiguration ( ) : Promise < boolean > {
67- const apiKey = await this . getApiKey ( ) ;
68- return ! ! apiKey ;
69- }
67+ async validateConfiguration ( ) : Promise < boolean > {
68+ const apiKey = await this . getApiKey ( ) ;
69+ return ! ! apiKey ;
70+ }
7071
71- private async getApiKey ( ) : Promise < string > {
72- let apiKey = await this . secretStorage . get ( this . API_KEY_SECRET ) ;
73-
74- if ( ! apiKey ) {
75- apiKey = await vscode . window . showInputBox ( {
76- prompt : 'Enter your Anthropic API key' ,
77- password : true ,
78- placeHolder : 'sk-ant-...'
79- } ) ;
80-
81- if ( ! apiKey ) {
82- throw new Error ( 'Anthropic API key is required' ) ;
83- }
84-
85- await this . secretStorage . store ( this . API_KEY_SECRET , apiKey ) ;
86- }
87-
88- return apiKey ;
72+ private async getApiKey ( ) : Promise < string > {
73+ let apiKey = await this . secretStorage . get ( this . API_KEY_SECRET ) ;
74+
75+ if ( ! apiKey ) {
76+ apiKey = await vscode . window . showInputBox ( {
77+ prompt : "Enter your Anthropic API key" ,
78+ password : true ,
79+ placeHolder : "sk-ant-..." ,
80+ } ) ;
81+
82+ if ( ! apiKey ) {
83+ throw new Error ( "Anthropic API key is required" ) ;
84+ }
85+
86+ await this . secretStorage . store ( this . API_KEY_SECRET , apiKey ) ;
8987 }
90- }
88+
89+ return apiKey ;
90+ }
91+ }
0 commit comments