diff --git a/anthropic-client/anthropic-client-core/src/commonMain/kotlin/com/tddworks/anthropic/api/Anthropic.kt b/anthropic-client/anthropic-client-core/src/commonMain/kotlin/com/tddworks/anthropic/api/Anthropic.kt index 35fd0bc..cc862c5 100644 --- a/anthropic-client/anthropic-client-core/src/commonMain/kotlin/com/tddworks/anthropic/api/Anthropic.kt +++ b/anthropic-client/anthropic-client-core/src/commonMain/kotlin/com/tddworks/anthropic/api/Anthropic.kt @@ -2,6 +2,7 @@ package com.tddworks.anthropic.api import com.tddworks.anthropic.api.internal.AnthropicApi import com.tddworks.anthropic.api.messages.api.Messages +import com.tddworks.di.getInstance /** * Interface for interacting with the Anthropic API. @@ -13,6 +14,26 @@ interface Anthropic : Messages { companion object { const val BASE_URL = "api.anthropic.com" const val ANTHROPIC_VERSION = "2023-06-01" + + /** + * Creates an instance of Anthropic API with the provided configurations. + * + * @param apiKey a function that returns the API key to be used for authentication. Defaults to "CONFIGURE_ME" if not provided. + * @param baseUrl a function that returns the base URL of the API. Defaults to the value specified in the Anthropic companion object if not provided. + * @param anthropicVersion a function that returns the version of the Anthropic API to be used. Defaults to "2023-06-01" if not provided. + * @return an instance of Anthropic API configured with the provided settings. + */ + fun create( + apiKey: () -> String = { "CONFIGURE_ME" }, + baseUrl: () -> String = { BASE_URL }, + anthropicVersion: () -> String = { ANTHROPIC_VERSION } + ): Anthropic { + return AnthropicApi( + apiKey = apiKey(), + apiURL = baseUrl(), + anthropicVersion = anthropicVersion(), + ) + } } /** @@ -51,10 +72,12 @@ fun Anthropic( apiKey: () -> String = { "CONFIGURE_ME" }, baseUrl: () -> String = { Anthropic.BASE_URL }, anthropicVersion: () -> String = { "2023-06-01" }, + messages: Messages = getInstance() ): Anthropic { return AnthropicApi( apiKey = apiKey(), apiURL = baseUrl(), - anthropicVersion = anthropicVersion() + anthropicVersion = anthropicVersion(), + messages = messages ) } \ No newline at end of file diff --git a/anthropic-client/anthropic-client-core/src/commonMain/kotlin/com/tddworks/anthropic/api/internal/AnthropicApi.kt b/anthropic-client/anthropic-client-core/src/commonMain/kotlin/com/tddworks/anthropic/api/internal/AnthropicApi.kt index 25ee01d..863026f 100644 --- a/anthropic-client/anthropic-client-core/src/commonMain/kotlin/com/tddworks/anthropic/api/internal/AnthropicApi.kt +++ b/anthropic-client/anthropic-client-core/src/commonMain/kotlin/com/tddworks/anthropic/api/internal/AnthropicApi.kt @@ -16,7 +16,8 @@ class AnthropicApi( private val apiKey: String, private val apiURL: String, private val anthropicVersion: String, -) : Anthropic, Messages by getInstance() { + private val messages: Messages = getInstance() +) : Anthropic, Messages by messages { /** * Gets the API key. * diff --git a/anthropic-client/anthropic-client-core/src/jvmTest/kotlin/com/tddworks/anthropic/api/AnthropicTest.kt b/anthropic-client/anthropic-client-core/src/jvmTest/kotlin/com/tddworks/anthropic/api/AnthropicTest.kt index 3dd976f..63e0ce4 100644 --- a/anthropic-client/anthropic-client-core/src/jvmTest/kotlin/com/tddworks/anthropic/api/AnthropicTest.kt +++ b/anthropic-client/anthropic-client-core/src/jvmTest/kotlin/com/tddworks/anthropic/api/AnthropicTest.kt @@ -22,7 +22,7 @@ class AnthropicTest : AutoCloseKoinTest() { val target = Anthropic( baseUrl = { "http://localhost:8080" }, apiKey = { "1234" }, - anthropicVersion = { "2024-03-01" } + anthropicVersion = { "2024-03-01" }, ) assertEquals("http://localhost:8080", target.baseUrl())