Skip to content

Commit

Permalink
update to kotlin coding conventions (#6)
Browse files Browse the repository at this point in the history
* README version update script fixed

* gradle wrapper updated

* Updated to official kotlin code conventions

* README update
  • Loading branch information
morisil authored Jan 9, 2025
1 parent fa75f0d commit 522e0c4
Show file tree
Hide file tree
Showing 22 changed files with 643 additions and 653 deletions.
10 changes: 5 additions & 5 deletions .github/scripts/update-readme-version.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/bin/bash

#
# Copyright 2024 Kazimierz Pogoda / Xemantic
# Copyright 2024-2025 Kazimierz Pogoda / Xemantic
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
Expand Down Expand Up @@ -47,19 +47,19 @@ fi
ESCAPED_GROUP_ID=$(echo "$GROUP_ID" | sed 's/\./\\./g')

# Create the pattern to match
PATTERN="\"$ESCAPED_GROUP_ID:$ARTIFACT_ID:[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\""
PATTERN="\"$ESCAPED_GROUP_ID:$ARTIFACT_ID:[0-9]+(\.[0-9]+){0,2}\""

# Create the replacement string
REPLACEMENT="\"$GROUP_ID:$ARTIFACT_ID:$VERSION\""

# Check if the pattern exists in the file
if ! grep -q "$GROUP_ID:$ARTIFACT_ID:[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*" README.md; then
if ! grep -Eq "$GROUP_ID:$ARTIFACT_ID:[0-9]+(\.[0-9]+){0,2}" README.md; then
echo "Error: Dependency pattern not found in README.md"
exit 1
fi

# Perform the replacement and save to a temporary file
sed "s|$PATTERN|$REPLACEMENT|g" README.md > README.md.tmp
sed -E "s|$PATTERN|$REPLACEMENT|g" README.md > README.md.tmp

# Check if sed made any changes
if cmp -s README.md README.md.tmp; then
Expand Down
22 changes: 11 additions & 11 deletions .github/workflows/build-branch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ jobs:
build_branch:
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/[email protected]
- name: Checkout sources
uses: actions/[email protected]

- name: Setup Java
uses: actions/[email protected]
with:
distribution: 'temurin'
java-version: 23
- name: Setup Java
uses: actions/[email protected]
with:
distribution: 'temurin'
java-version: 23

- name: Setup Gradle
uses: gradle/actions/[email protected]
- name: Setup Gradle
uses: gradle/actions/[email protected]

- name: Build
run: ./gradlew build
- name: Build
run: ./gradlew build
28 changes: 14 additions & 14 deletions .github/workflows/build-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ jobs:
build_main:
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/[email protected]
- name: Checkout sources
uses: actions/[email protected]

- name: Setup Java
uses: actions/[email protected]
with:
distribution: 'temurin'
java-version: 23
- name: Setup Java
uses: actions/[email protected]
with:
distribution: 'temurin'
java-version: 23

- name: Setup Gradle
uses: gradle/actions/[email protected]
- name: Setup Gradle
uses: gradle/actions/[email protected]

- name: Build
run: ./gradlew build sourcesJar dokkaGeneratePublicationHtml publish
env:
ORG_GRADLE_PROJECT_githubActor: ${{ secrets.GITHUBACTOR }}
ORG_GRADLE_PROJECT_githubToken: ${{ secrets.GITHUBTOKEN }}
- name: Build
run: ./gradlew build sourcesJar dokkaGeneratePublicationHtml publish
env:
ORG_GRADLE_PROJECT_githubActor: ${{ secrets.GITHUBACTOR }}
ORG_GRADLE_PROJECT_githubToken: ${{ secrets.GITHUBTOKEN }}
78 changes: 39 additions & 39 deletions .github/workflows/build-release.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Build release
on:
release:
types: [published]
types: [ published ]
jobs:
build_release:
runs-on: ubuntu-latest
Expand All @@ -10,48 +10,48 @@ jobs:
# added or changed files to the repository.
contents: write
steps:
- name: Write release version
run: |
VERSION=${GITHUB_REF_NAME#v}
echo Version: $VERSION
echo "VERSION=$VERSION" >> $GITHUB_ENV
- name: Write release version
run: |
VERSION=${GITHUB_REF_NAME#v}
echo Version: $VERSION
echo "VERSION=$VERSION" >> $GITHUB_ENV
- name: Checkout sources
uses: actions/[email protected]
with:
ref: ${{ github.head_ref }}
fetch-depth: 0
- name: Checkout sources
uses: actions/[email protected]
with:
ref: ${{ github.head_ref }}
fetch-depth: 0

- name: Setup Java
uses: actions/[email protected]
with:
distribution: 'temurin'
java-version: 23
- name: Setup Java
uses: actions/[email protected]
with:
distribution: 'temurin'
java-version: 23

- name: Setup Gradle
uses: gradle/actions/[email protected]
- name: Setup Gradle
uses: gradle/actions/[email protected]

- name: Build
env:
ORG_GRADLE_PROJECT_githubActor: ${{ secrets.GITHUBACTOR }}
ORG_GRADLE_PROJECT_githubToken: ${{ secrets.GITHUBTOKEN }}
ORG_GRADLE_PROJECT_signingKey: ${{ secrets.SIGNING_KEY }}
ORG_GRADLE_PROJECT_signingPassword: ${{ secrets.SIGNING_PASSWORD }}
ORG_GRADLE_PROJECT_sonatypeUser: ${{ secrets.SONATYPE_USER }}
ORG_GRADLE_PROJECT_sonatypePassword: ${{ secrets.SONATYPE_PASSWORD }}
run: ./gradlew -Pversion=$VERSION build sourcesJar dokkaGeneratePublicationHtml publishToSonatype closeAndReleaseSonatypeStagingRepository
- name: Build
env:
ORG_GRADLE_PROJECT_githubActor: ${{ secrets.GITHUBACTOR }}
ORG_GRADLE_PROJECT_githubToken: ${{ secrets.GITHUBTOKEN }}
ORG_GRADLE_PROJECT_signingKey: ${{ secrets.SIGNING_KEY }}
ORG_GRADLE_PROJECT_signingPassword: ${{ secrets.SIGNING_PASSWORD }}
ORG_GRADLE_PROJECT_sonatypeUser: ${{ secrets.SONATYPE_USER }}
ORG_GRADLE_PROJECT_sonatypePassword: ${{ secrets.SONATYPE_PASSWORD }}
run: ./gradlew -Pversion=$VERSION build sourcesJar dokkaGeneratePublicationHtml publishToSonatype closeAndReleaseSonatypeStagingRepository

- name: Checkout main branch
uses: actions/[email protected]
with:
ref: main
fetch-depth: 0
- name: Checkout main branch
uses: actions/[email protected]
with:
ref: main
fetch-depth: 0

- name: Update README
run: sh .github/scripts/update-readme-version.sh
- name: Update README
run: sh .github/scripts/update-readme-version.sh

- name: Commit README
uses: stefanzweifel/[email protected]
with:
commit_message: Dependency version in README.md updated to ${{ env.VERSION }}
file_pattern: 'README.md'
- name: Commit README
uses: stefanzweifel/[email protected]
with:
commit_message: Dependency version in README.md updated to ${{ env.VERSION }}
file_pattern: 'README.md'
2 changes: 1 addition & 1 deletion .github/workflows/updater.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ name: GitHub Actions Version Updater
on:
schedule:
# Automatically run on every Sunday
- cron: '0 0 * * 0'
- cron: '0 0 * * 0'

jobs:
build:
Expand Down
2 changes: 1 addition & 1 deletion .idea/copyright/apache2_0.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 9 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# xemantic-ai-money
Kotlin multiplatform library for real-time calculation of LLM usage costs
Real-time calculation of LLM usage costs - a Kotlin multiplatform library

[<img alt="Maven Central Version" src="https://img.shields.io/maven-central/v/com.xemantic.ai/xemantic-ai-money">](https://central.sonatype.com/namespace/com.xemantic.ai)
[<img alt="GitHub Release Date" src="https://img.shields.io/github/release-date/xemantic/xemantic-ai-money">](https://github.com/xemantic/xemantic-ai-money/releases)
Expand All @@ -22,39 +22,32 @@ Kotlin multiplatform library for real-time calculation of LLM usage costs

## Why?

The APIs of AI companies, like [OpenAI API](https://platform.openai.com/docs/api-reference/introduction)
and [Anthropic API](https://docs.anthropic.com/en/api/getting-started), are providing the usage information regarding input and output tokens
associated with each API call. Depending on many factors, like the model being used, batch
processing, involved cache, etc., these tokens can be billed according to particular rules.
The APIs of AI companies, like [OpenAI API](https://platform.openai.com/docs/api-reference/introduction) and [Anthropic API](https://docs.anthropic.com/en/api/getting-started), are providing the usage information regarding input and output tokens associated with each API call.
Depending on many factors, like the model being used, batch processing, involved cache, etc., these tokens can be billed according to particular rules.
This library is fulfilling the need of proper accounting of API usage by:

* representing monetary amounts as [Money](src/commonMain/kotlin/Money.kt) interface,
supporting big decimal arithmetics and operator overloading,
* representing ratios of monetary amounts (e.g. input token cost for given model)
as [Money.Ratio](src/commonMain/kotlin/Money.kt) interface.

* representing monetary amounts as [Money](src/commonMain/kotlin/Money.kt) interface, supporting big decimal arithmetics and operator overloading,
* representing ratios of monetary amounts (e.g. input token cost for given model) as [Money.Ratio](src/commonMain/kotlin/Money.kt) interface.

> [!NOTE]
> The `xemantic-ai-money` was initially a part of the
> [anthropic-sdk-kotlin](https://github.com/xemantic/anthropic-sdk-kotlin), but was eventually externalized,
> as a common functionality applicable across various API-related use cases.
> The `xemantic-ai-money` was initially a part of the [anthropic-sdk-kotlin](https://github.com/xemantic/anthropic-sdk-kotlin), but was eventually externalized, as a
> common functionality applicable across various API-related use cases.
## Usage

In `build.gradle.kts` add:

```kotlin
dependencies {
implementation("com.xemantic.ai:xemantic-ai-money:0.2")
implementation("com.xemantic.ai:xemantic-ai-money:0.2")
}
```

See [test cases](src/commonTest/kotlin) for further information.

## Big decimal arithmetics

The implementation of big decimal arithmetic in use will depend on the multiplatform
targets:
The implementation of big decimal arithmetic in use will depend on the multiplatform targets:

* `java.math.BigDecimal` for JVM target, for maximal performance and stability.
* [kotlin-multiplatform-bignum](https://github.com/ionspin/kotlin-multiplatform-bignum) for non-JVM targets.
Expand Down
18 changes: 9 additions & 9 deletions api/xemantic-ai-money.api
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,6 @@ public final class com/xemantic/ai/money/JvmMoney : com/xemantic/ai/money/Money
public fun toString ()Ljava/lang/String;
}

public final class com/xemantic/ai/money/JvmMoneyKt {
public static final fun Money (Ljava/lang/String;)Lcom/xemantic/ai/money/Money;
public static final fun Ratio (Lcom/xemantic/ai/money/Money$Companion;Ljava/lang/String;)Lcom/xemantic/ai/money/Money$Ratio;
public static final fun getONE (Lcom/xemantic/ai/money/Money$Companion;)Lcom/xemantic/ai/money/Money;
public static final fun getONE (Lcom/xemantic/ai/money/Money$Ratio$Companion;)Lcom/xemantic/ai/money/Money$Ratio;
public static final fun getZERO (Lcom/xemantic/ai/money/Money$Companion;)Lcom/xemantic/ai/money/Money;
public static final fun toMoneyRatio (I)Lcom/xemantic/ai/money/Money$Ratio;
}

public abstract interface class com/xemantic/ai/money/Money {
public static final field Companion Lcom/xemantic/ai/money/Money$Companion;
public abstract fun compareTo (Lcom/xemantic/ai/money/Money;)I
Expand Down Expand Up @@ -52,6 +43,15 @@ public final class com/xemantic/ai/money/MoneyKt {
public static final fun times (ILcom/xemantic/ai/money/Money;)Lcom/xemantic/ai/money/Money;
}

public final class com/xemantic/ai/money/Money_jvmKt {
public static final fun Money (Ljava/lang/String;)Lcom/xemantic/ai/money/Money;
public static final fun Ratio (Lcom/xemantic/ai/money/Money$Companion;Ljava/lang/String;)Lcom/xemantic/ai/money/Money$Ratio;
public static final fun getONE (Lcom/xemantic/ai/money/Money$Companion;)Lcom/xemantic/ai/money/Money;
public static final fun getONE (Lcom/xemantic/ai/money/Money$Ratio$Companion;)Lcom/xemantic/ai/money/Money$Ratio;
public static final fun getZERO (Lcom/xemantic/ai/money/Money$Companion;)Lcom/xemantic/ai/money/Money;
public static final fun toMoneyRatio (I)Lcom/xemantic/ai/money/Money$Ratio;
}

public final class com/xemantic/ai/money/serialization/MoneyRatioSerializer : kotlinx/serialization/KSerializer {
public static final field INSTANCE Lcom/xemantic/ai/money/serialization/MoneyRatioSerializer;
public fun deserialize (Lkotlinx/serialization/encoding/Decoder;)Lcom/xemantic/ai/money/Money$Ratio;
Expand Down
Loading

0 comments on commit 522e0c4

Please sign in to comment.