diff --git a/examples/kotlin-koog-engram-cloud-demo/.env.example b/examples/kotlin-koog-engram-cloud-demo/.env.example new file mode 100644 index 0000000..4603898 --- /dev/null +++ b/examples/kotlin-koog-engram-cloud-demo/.env.example @@ -0,0 +1,8 @@ +# Sign up at https://platform.openai.com/api-keys to get an OpenAI key. +OPENAI_API_KEY=sk-... + +# Sign up at https://cloud.jamjet.dev, create a project, and copy the API key. +JAMJET_API_KEY=jk_... + +# Optional — defaults to the public hosted JamJet Cloud. +# JAMJET_API_URL=https://api.jamjet.dev diff --git a/examples/kotlin-koog-engram-cloud-demo/.gitignore b/examples/kotlin-koog-engram-cloud-demo/.gitignore new file mode 100644 index 0000000..2d70269 --- /dev/null +++ b/examples/kotlin-koog-engram-cloud-demo/.gitignore @@ -0,0 +1,5 @@ +target/ +.env +.idea/ +*.iml +.vscode/ diff --git a/examples/kotlin-koog-engram-cloud-demo/.mvn/wrapper/maven-wrapper.properties b/examples/kotlin-koog-engram-cloud-demo/.mvn/wrapper/maven-wrapper.properties new file mode 100644 index 0000000..ffcab66 --- /dev/null +++ b/examples/kotlin-koog-engram-cloud-demo/.mvn/wrapper/maven-wrapper.properties @@ -0,0 +1,3 @@ +wrapperVersion=3.3.4 +distributionType=only-script +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.9/apache-maven-3.9.9-bin.zip diff --git a/examples/kotlin-koog-engram-cloud-demo/README.md b/examples/kotlin-koog-engram-cloud-demo/README.md new file mode 100644 index 0000000..ddcf1ee --- /dev/null +++ b/examples/kotlin-koog-engram-cloud-demo/README.md @@ -0,0 +1,165 @@ +# Kotlin + Koog + Engram + JamJet Cloud Demo + +A multi-turn chat agent built on **[Koog](https://github.com/JetBrains/koog) 0.8** (JetBrains' Kotlin agent framework) that **remembers facts across calls** via [Engram](https://github.com/jamjet-labs/jamjet/tree/main/runtime/engram-server) and is **observed end-to-end** by [JamJet Cloud](https://cloud.jamjet.dev) — drop in a single 25-line extension function and your Koog agent is shipping OTLP traces. + +## What this demo shows + +- **Koog 0.8** Kotlin-native agent — `AIAgent` constructor with OpenAI executor, `singleRunStrategy`, and a `ToolRegistry` +- **`koog-spring-boot-starter`** autoconfigures the OpenAI `SingleLLMPromptExecutor` from `ai.koog.openai.api-key` +- **`engram-spring-boot-starter`** autoconfigures `EngramClient` so the agent's `@Tool` methods record + recall facts against a real Engram server +- **One extension function — `addJamjetCloudExporter()`** — wires Koog's built-in OpenTelemetry feature to JamJet Cloud's OTLP/JSON intake. No Micrometer, no Spring AI observation handlers, no per-vendor SDK. + +## The headline file: `JamjetCloudExporter.kt` + +The entire JamJet integration is one extension on Koog's `OpenTelemetryConfig`: + +```kotlin +@JvmOverloads +public fun OpenTelemetryConfig.addJamjetCloudExporter( + apiKey: String? = null, + apiUrl: String = "https://api.jamjet.dev", + timeout: Duration = 10.seconds, +) { + val key = apiKey + ?: getEnvironmentVariableOrNull("JAMJET_API_KEY") + ?: error("JAMJET_API_KEY is missing.") + + addSpanExporter( + OtlpJsonSpanExporter( + endpoint = "$apiUrl/v1/otlp/v1/traces", + headers = mapOf("Authorization" to "Bearer $key"), + timeout = timeout, + ) + ) +} +``` + +That's it. Mirrors the `addDatadogExporter` / `addLangfuseExporter` pattern that ships with Koog — plug it into any Koog agent's `install(OpenTelemetry) { ... }` block and JamJet receives every LLM span, tool span, and cost rollup. + +## How it's wired + +``` +User → POST /chat?session=alice ──→ Koog AIAgent + │ + ├─→ OpenAI chat completion (via SingleLLMPromptExecutor) + │ + ├─→ MemoryTools.rememberFact / recallFacts (Koog @Tool) + │ │ + │ └─→ EngramClient ──→ Engram REST API (Docker) + │ + └─→ OpenTelemetry feature → addJamjetCloudExporter() + │ + └─→ OTLP/JSON → JamJet Cloud +``` + +JamJet Cloud's OTLP/JSON intake (`POST /v1/otlp/v1/traces`) ingests Koog's spans directly — no per-framework adapter, no proprietary SDK on the agent side. + +## Prerequisites + +- **Java 21+** +- **Docker Desktop** (or any Docker engine) — for the Engram sidecar +- **An OpenAI API key** — sign up at [platform.openai.com](https://platform.openai.com/api-keys) +- **A JamJet Cloud project** — sign up at [cloud.jamjet.dev](https://cloud.jamjet.dev), create a project, copy the API key + +> **Cost note:** The OpenAI key is used twice per chat turn — once by Koog for the chat completion, once by Engram for fact extraction. Both calls use `gpt-4o-mini` by default. + +## Run it + +```bash +git clone https://github.com/jamjet-labs/jamjet-runtime-java +cd jamjet-runtime-java/examples/kotlin-koog-engram-cloud-demo + +cp .env.example .env # Windows: copy .env.example .env +# Edit .env — paste your OPENAI_API_KEY and JAMJET_API_KEY + +docker compose up -d # boots Engram on 127.0.0.1:9090 +./mvnw spring-boot:run # Windows: mvnw.cmd spring-boot:run +``` + +The app starts on `127.0.0.1:8181` (8181 instead of 8080 to leave room for a local JamJet Cloud dev stack on 8080). `PreflightCheck` validates both env vars and waits for Engram's `/health` endpoint before the server accepts requests — if either is missing or Engram is unreachable, the app exits with a clear error. + +In another terminal: + +```bash +# Tell the agent a fact — it stores it in Engram +curl -s -X POST "localhost:8181/chat?session=alice" \ + -H "Content-Type: text/plain" \ + -d "I work at Acme as a Kotlin engineer" +# → {"session":"alice","reply":"Got it, I've stored that you work at Acme as a Kotlin engineer."} + +# Ask about it — agent recalls from Engram +curl -s -X POST "localhost:8181/chat?session=alice" \ + -H "Content-Type: text/plain" \ + -d "Where do I work?" +# → {"session":"alice","reply":"You work at Acme."} + +curl -s -X POST "localhost:8181/chat?session=alice" \ + -H "Content-Type: text/plain" \ + -d "What languages do I use?" +# → {"session":"alice","reply":"Based on what you've shared, you work as a Kotlin engineer."} +``` + +Each response is a JSON object `{"session": "...", "reply": "..."}`. + +## See the trace in JamJet Cloud + +Open [cloud.jamjet.dev/dashboard/graph](https://cloud.jamjet.dev/dashboard/graph) — each `/chat` call appears as a trace tagged `service.name=kotlin-koog-engram-demo` with: + +- 1 `invoke_agent` span +- 1+ inference spans (OpenAI chat completion) +- 1+ tool execution spans (`rememberFact` or `recallFacts`) +- Cost rollup (per-token, per-call) + +## Anatomy + +The interesting code is ~150 LOC across 5 Kotlin files: + +| File | What it does | +|---|---| +| `cloud/JamjetCloudExporter.kt` | The 25-line extension function — adds JamJet to any Koog agent's `OpenTelemetry` config | +| `MemoryTools.kt` | `ToolSet` with `@Tool`+`@LLMDescription` methods backed by autoconfigured `EngramClient` | +| `MemoryAgent.kt` | Builds a Koog `AIAgent` per request: OpenAI executor + tools + `install(OpenTelemetry)` | +| `ChatController.kt` | `POST /chat?session=X` — accepts `text/plain`, returns `{"session","reply"}` | +| `startup/PreflightCheck.kt` | Validates env vars + polls Engram `/health` before the app accepts traffic | + +The pom has three Maven dependencies for the agent stack: `koog-spring-boot-starter`, `agents-features-opentelemetry-jvm`, `engram-spring-boot-starter`. + +## Configuration + +| Property | Default | Purpose | +|---|---|---| +| `engram.base-url` | `http://127.0.0.1:9090` | Where the autoconfigured `EngramClient` connects | +| `ai.koog.openai.api-key` | `${OPENAI_API_KEY}` | Koog OpenAI client API key | +| `jamjet.cloud.api-key` | `${JAMJET_API_KEY}` | JamJet Cloud project key (passed to `addJamjetCloudExporter`) | +| `jamjet.cloud.api-url` | `https://api.jamjet.dev` | JamJet Cloud OTLP intake URL | + +To swap the chat model (e.g. to `OpenAIModels.Chat.GPT4o`), edit `MemoryAgent.kt`. To use a different LLM provider for Engram's fact extraction, change `ENGRAM_LLM_PROVIDER` in `docker-compose.yml`. + +## How is this different from Track 1's Java/Spring AI demo? + +[Track 1 (`spring-ai-engram-cloud-demo`)](../spring-ai-engram-cloud-demo) targets Spring AI users — its observability is wired through Spring AI's Micrometer Observation hooks via `jamjet-cloud-spring-boot-starter`. This Kotlin track targets Koog users — observability is wired through Koog's built-in OpenTelemetry feature via vendor-neutral OTLP/JSON. **JamJet Cloud sees both flavours of trace identically** (same `service.name`, same span shape, same cost rollups) because both end up at the OTLP intake. Pick the demo that matches your runtime. + +## Windows notes + +- Use PowerShell or cmd; `mvnw.cmd` is the entry point instead of `./mvnw`. +- WSL2 users: run from the WSL side for cleanest networking with Docker Desktop. +- The `ghcr.io/jamjet-labs/engram-server:0.5.0` image is multi-arch. + +## Cleaning up + +```bash +docker compose down # stops Engram, removes container + volume +# Press Ctrl-C on the Spring app +``` + +When you're done, rotate or delete your JamJet API key and OpenAI key in their respective dashboards. + +## Security + +- `.env` is in `.gitignore` — never commit your keys. +- Both Engram and the Spring app bind to `127.0.0.1`. Do not expose this demo on a public network. +- For real apps with PII in prompts, enable [JamJet's redaction settings](https://docs.jamjet.dev/redaction) (Team tier and up). + +## License + +Apache 2.0. See [LICENSE](../../LICENSE). diff --git a/examples/kotlin-koog-engram-cloud-demo/docker-compose.yml b/examples/kotlin-koog-engram-cloud-demo/docker-compose.yml new file mode 100644 index 0000000..1941ae4 --- /dev/null +++ b/examples/kotlin-koog-engram-cloud-demo/docker-compose.yml @@ -0,0 +1,26 @@ +services: + engram: + image: ghcr.io/jamjet-labs/engram-server:0.5.0 + container_name: engram-kotlin-koog-demo + # No explicit command — all configuration is via environment variables. + # "serve" is the default entrypoint; mode=rest and port=9090 are set below. + ports: + - "127.0.0.1:9090:9090" # loopback-only — do not expose Engram on the network + environment: + - ENGRAM_MODE=rest # default is "mcp" (stdio); must be "rest" for HTTP + - ENGRAM_LLM_PROVIDER=openai-compatible # real fact extraction so the demo's memory actually works + - ENGRAM_OPENAI_BASE_URL=https://api.openai.com/v1 + - OPENAI_API_KEY=${OPENAI_API_KEY:?OPENAI_API_KEY required for Engram fact extraction; copy .env.example to .env and paste your key} + - ENGRAM_EMBEDDING_PROVIDER=mock # mock embeddings (768d, deterministic) — fine for a 3-fact demo without an Ollama prereq + volumes: + - engram-data:/data # ENGRAM_DB_PATH defaults to /data/engram.db inside the image + healthcheck: + test: ["CMD", "wget", "-q", "--spider", "http://localhost:9090/health"] + interval: 2s + timeout: 1s + retries: 15 + start_period: 5s + restart: unless-stopped + +volumes: + engram-data: diff --git a/examples/kotlin-koog-engram-cloud-demo/mvnw b/examples/kotlin-koog-engram-cloud-demo/mvnw new file mode 100755 index 0000000..bd8896b --- /dev/null +++ b/examples/kotlin-koog-engram-cloud-demo/mvnw @@ -0,0 +1,295 @@ +#!/bin/sh +# ---------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# ---------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- +# Apache Maven Wrapper startup batch script, version 3.3.4 +# +# Optional ENV vars +# ----------------- +# JAVA_HOME - location of a JDK home dir, required when download maven via java source +# MVNW_REPOURL - repo url base for downloading maven distribution +# MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven +# MVNW_VERBOSE - true: enable verbose log; debug: trace the mvnw script; others: silence the output +# ---------------------------------------------------------------------------- + +set -euf +[ "${MVNW_VERBOSE-}" != debug ] || set -x + +# OS specific support. +native_path() { printf %s\\n "$1"; } +case "$(uname)" in +CYGWIN* | MINGW*) + [ -z "${JAVA_HOME-}" ] || JAVA_HOME="$(cygpath --unix "$JAVA_HOME")" + native_path() { cygpath --path --windows "$1"; } + ;; +esac + +# set JAVACMD and JAVACCMD +set_java_home() { + # For Cygwin and MinGW, ensure paths are in Unix format before anything is touched + if [ -n "${JAVA_HOME-}" ]; then + if [ -x "$JAVA_HOME/jre/sh/java" ]; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + JAVACCMD="$JAVA_HOME/jre/sh/javac" + else + JAVACMD="$JAVA_HOME/bin/java" + JAVACCMD="$JAVA_HOME/bin/javac" + + if [ ! -x "$JAVACMD" ] || [ ! -x "$JAVACCMD" ]; then + echo "The JAVA_HOME environment variable is not defined correctly, so mvnw cannot run." >&2 + echo "JAVA_HOME is set to \"$JAVA_HOME\", but \"\$JAVA_HOME/bin/java\" or \"\$JAVA_HOME/bin/javac\" does not exist." >&2 + return 1 + fi + fi + else + JAVACMD="$( + 'set' +e + 'unset' -f command 2>/dev/null + 'command' -v java + )" || : + JAVACCMD="$( + 'set' +e + 'unset' -f command 2>/dev/null + 'command' -v javac + )" || : + + if [ ! -x "${JAVACMD-}" ] || [ ! -x "${JAVACCMD-}" ]; then + echo "The java/javac command does not exist in PATH nor is JAVA_HOME set, so mvnw cannot run." >&2 + return 1 + fi + fi +} + +# hash string like Java String::hashCode +hash_string() { + str="${1:-}" h=0 + while [ -n "$str" ]; do + char="${str%"${str#?}"}" + h=$(((h * 31 + $(LC_CTYPE=C printf %d "'$char")) % 4294967296)) + str="${str#?}" + done + printf %x\\n $h +} + +verbose() { :; } +[ "${MVNW_VERBOSE-}" != true ] || verbose() { printf %s\\n "${1-}"; } + +die() { + printf %s\\n "$1" >&2 + exit 1 +} + +trim() { + # MWRAPPER-139: + # Trims trailing and leading whitespace, carriage returns, tabs, and linefeeds. + # Needed for removing poorly interpreted newline sequences when running in more + # exotic environments such as mingw bash on Windows. + printf "%s" "${1}" | tr -d '[:space:]' +} + +scriptDir="$(dirname "$0")" +scriptName="$(basename "$0")" + +# parse distributionUrl and optional distributionSha256Sum, requires .mvn/wrapper/maven-wrapper.properties +while IFS="=" read -r key value; do + case "${key-}" in + distributionUrl) distributionUrl=$(trim "${value-}") ;; + distributionSha256Sum) distributionSha256Sum=$(trim "${value-}") ;; + esac +done <"$scriptDir/.mvn/wrapper/maven-wrapper.properties" +[ -n "${distributionUrl-}" ] || die "cannot read distributionUrl property in $scriptDir/.mvn/wrapper/maven-wrapper.properties" + +case "${distributionUrl##*/}" in +maven-mvnd-*bin.*) + MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/ + case "${PROCESSOR_ARCHITECTURE-}${PROCESSOR_ARCHITEW6432-}:$(uname -a)" in + *AMD64:CYGWIN* | *AMD64:MINGW*) distributionPlatform=windows-amd64 ;; + :Darwin*x86_64) distributionPlatform=darwin-amd64 ;; + :Darwin*arm64) distributionPlatform=darwin-aarch64 ;; + :Linux*x86_64*) distributionPlatform=linux-amd64 ;; + *) + echo "Cannot detect native platform for mvnd on $(uname)-$(uname -m), use pure java version" >&2 + distributionPlatform=linux-amd64 + ;; + esac + distributionUrl="${distributionUrl%-bin.*}-$distributionPlatform.zip" + ;; +maven-mvnd-*) MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/ ;; +*) MVN_CMD="mvn${scriptName#mvnw}" _MVNW_REPO_PATTERN=/org/apache/maven/ ;; +esac + +# apply MVNW_REPOURL and calculate MAVEN_HOME +# maven home pattern: ~/.m2/wrapper/dists/{apache-maven-,maven-mvnd--}/ +[ -z "${MVNW_REPOURL-}" ] || distributionUrl="$MVNW_REPOURL$_MVNW_REPO_PATTERN${distributionUrl#*"$_MVNW_REPO_PATTERN"}" +distributionUrlName="${distributionUrl##*/}" +distributionUrlNameMain="${distributionUrlName%.*}" +distributionUrlNameMain="${distributionUrlNameMain%-bin}" +MAVEN_USER_HOME="${MAVEN_USER_HOME:-${HOME}/.m2}" +MAVEN_HOME="${MAVEN_USER_HOME}/wrapper/dists/${distributionUrlNameMain-}/$(hash_string "$distributionUrl")" + +exec_maven() { + unset MVNW_VERBOSE MVNW_USERNAME MVNW_PASSWORD MVNW_REPOURL || : + exec "$MAVEN_HOME/bin/$MVN_CMD" "$@" || die "cannot exec $MAVEN_HOME/bin/$MVN_CMD" +} + +if [ -d "$MAVEN_HOME" ]; then + verbose "found existing MAVEN_HOME at $MAVEN_HOME" + exec_maven "$@" +fi + +case "${distributionUrl-}" in +*?-bin.zip | *?maven-mvnd-?*-?*.zip) ;; +*) die "distributionUrl is not valid, must match *-bin.zip or maven-mvnd-*.zip, but found '${distributionUrl-}'" ;; +esac + +# prepare tmp dir +if TMP_DOWNLOAD_DIR="$(mktemp -d)" && [ -d "$TMP_DOWNLOAD_DIR" ]; then + clean() { rm -rf -- "$TMP_DOWNLOAD_DIR"; } + trap clean HUP INT TERM EXIT +else + die "cannot create temp dir" +fi + +mkdir -p -- "${MAVEN_HOME%/*}" + +# Download and Install Apache Maven +verbose "Couldn't find MAVEN_HOME, downloading and installing it ..." +verbose "Downloading from: $distributionUrl" +verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName" + +# select .zip or .tar.gz +if ! command -v unzip >/dev/null; then + distributionUrl="${distributionUrl%.zip}.tar.gz" + distributionUrlName="${distributionUrl##*/}" +fi + +# verbose opt +__MVNW_QUIET_WGET=--quiet __MVNW_QUIET_CURL=--silent __MVNW_QUIET_UNZIP=-q __MVNW_QUIET_TAR='' +[ "${MVNW_VERBOSE-}" != true ] || __MVNW_QUIET_WGET='' __MVNW_QUIET_CURL='' __MVNW_QUIET_UNZIP='' __MVNW_QUIET_TAR=v + +# normalize http auth +case "${MVNW_PASSWORD:+has-password}" in +'') MVNW_USERNAME='' MVNW_PASSWORD='' ;; +has-password) [ -n "${MVNW_USERNAME-}" ] || MVNW_USERNAME='' MVNW_PASSWORD='' ;; +esac + +if [ -z "${MVNW_USERNAME-}" ] && command -v wget >/dev/null; then + verbose "Found wget ... using wget" + wget ${__MVNW_QUIET_WGET:+"$__MVNW_QUIET_WGET"} "$distributionUrl" -O "$TMP_DOWNLOAD_DIR/$distributionUrlName" || die "wget: Failed to fetch $distributionUrl" +elif [ -z "${MVNW_USERNAME-}" ] && command -v curl >/dev/null; then + verbose "Found curl ... using curl" + curl ${__MVNW_QUIET_CURL:+"$__MVNW_QUIET_CURL"} -f -L -o "$TMP_DOWNLOAD_DIR/$distributionUrlName" "$distributionUrl" || die "curl: Failed to fetch $distributionUrl" +elif set_java_home; then + verbose "Falling back to use Java to download" + javaSource="$TMP_DOWNLOAD_DIR/Downloader.java" + targetZip="$TMP_DOWNLOAD_DIR/$distributionUrlName" + cat >"$javaSource" <<-END + public class Downloader extends java.net.Authenticator + { + protected java.net.PasswordAuthentication getPasswordAuthentication() + { + return new java.net.PasswordAuthentication( System.getenv( "MVNW_USERNAME" ), System.getenv( "MVNW_PASSWORD" ).toCharArray() ); + } + public static void main( String[] args ) throws Exception + { + setDefault( new Downloader() ); + java.nio.file.Files.copy( java.net.URI.create( args[0] ).toURL().openStream(), java.nio.file.Paths.get( args[1] ).toAbsolutePath().normalize() ); + } + } + END + # For Cygwin/MinGW, switch paths to Windows format before running javac and java + verbose " - Compiling Downloader.java ..." + "$(native_path "$JAVACCMD")" "$(native_path "$javaSource")" || die "Failed to compile Downloader.java" + verbose " - Running Downloader.java ..." + "$(native_path "$JAVACMD")" -cp "$(native_path "$TMP_DOWNLOAD_DIR")" Downloader "$distributionUrl" "$(native_path "$targetZip")" +fi + +# If specified, validate the SHA-256 sum of the Maven distribution zip file +if [ -n "${distributionSha256Sum-}" ]; then + distributionSha256Result=false + if [ "$MVN_CMD" = mvnd.sh ]; then + echo "Checksum validation is not supported for maven-mvnd." >&2 + echo "Please disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2 + exit 1 + elif command -v sha256sum >/dev/null; then + if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | sha256sum -c - >/dev/null 2>&1; then + distributionSha256Result=true + fi + elif command -v shasum >/dev/null; then + if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | shasum -a 256 -c >/dev/null 2>&1; then + distributionSha256Result=true + fi + else + echo "Checksum validation was requested but neither 'sha256sum' or 'shasum' are available." >&2 + echo "Please install either command, or disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2 + exit 1 + fi + if [ $distributionSha256Result = false ]; then + echo "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised." >&2 + echo "If you updated your Maven version, you need to update the specified distributionSha256Sum property." >&2 + exit 1 + fi +fi + +# unzip and move +if command -v unzip >/dev/null; then + unzip ${__MVNW_QUIET_UNZIP:+"$__MVNW_QUIET_UNZIP"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -d "$TMP_DOWNLOAD_DIR" || die "failed to unzip" +else + tar xzf${__MVNW_QUIET_TAR:+"$__MVNW_QUIET_TAR"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -C "$TMP_DOWNLOAD_DIR" || die "failed to untar" +fi + +# Find the actual extracted directory name (handles snapshots where filename != directory name) +actualDistributionDir="" + +# First try the expected directory name (for regular distributions) +if [ -d "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" ]; then + if [ -f "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain/bin/$MVN_CMD" ]; then + actualDistributionDir="$distributionUrlNameMain" + fi +fi + +# If not found, search for any directory with the Maven executable (for snapshots) +if [ -z "$actualDistributionDir" ]; then + # enable globbing to iterate over items + set +f + for dir in "$TMP_DOWNLOAD_DIR"/*; do + if [ -d "$dir" ]; then + if [ -f "$dir/bin/$MVN_CMD" ]; then + actualDistributionDir="$(basename "$dir")" + break + fi + fi + done + set -f +fi + +if [ -z "$actualDistributionDir" ]; then + verbose "Contents of $TMP_DOWNLOAD_DIR:" + verbose "$(ls -la "$TMP_DOWNLOAD_DIR")" + die "Could not find Maven distribution directory in extracted archive" +fi + +verbose "Found extracted Maven distribution directory: $actualDistributionDir" +printf %s\\n "$distributionUrl" >"$TMP_DOWNLOAD_DIR/$actualDistributionDir/mvnw.url" +mv -- "$TMP_DOWNLOAD_DIR/$actualDistributionDir" "$MAVEN_HOME" || [ -d "$MAVEN_HOME" ] || die "fail to move MAVEN_HOME" + +clean || : +exec_maven "$@" diff --git a/examples/kotlin-koog-engram-cloud-demo/mvnw.cmd b/examples/kotlin-koog-engram-cloud-demo/mvnw.cmd new file mode 100644 index 0000000..5761d94 --- /dev/null +++ b/examples/kotlin-koog-engram-cloud-demo/mvnw.cmd @@ -0,0 +1,189 @@ +<# : batch portion +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM http://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Apache Maven Wrapper startup batch script, version 3.3.4 +@REM +@REM Optional ENV vars +@REM MVNW_REPOURL - repo url base for downloading maven distribution +@REM MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven +@REM MVNW_VERBOSE - true: enable verbose log; others: silence the output +@REM ---------------------------------------------------------------------------- + +@IF "%__MVNW_ARG0_NAME__%"=="" (SET __MVNW_ARG0_NAME__=%~nx0) +@SET __MVNW_CMD__= +@SET __MVNW_ERROR__= +@SET __MVNW_PSMODULEP_SAVE=%PSModulePath% +@SET PSModulePath= +@FOR /F "usebackq tokens=1* delims==" %%A IN (`powershell -noprofile "& {$scriptDir='%~dp0'; $script='%__MVNW_ARG0_NAME__%'; icm -ScriptBlock ([Scriptblock]::Create((Get-Content -Raw '%~f0'))) -NoNewScope}"`) DO @( + IF "%%A"=="MVN_CMD" (set __MVNW_CMD__=%%B) ELSE IF "%%B"=="" (echo %%A) ELSE (echo %%A=%%B) +) +@SET PSModulePath=%__MVNW_PSMODULEP_SAVE% +@SET __MVNW_PSMODULEP_SAVE= +@SET __MVNW_ARG0_NAME__= +@SET MVNW_USERNAME= +@SET MVNW_PASSWORD= +@IF NOT "%__MVNW_CMD__%"=="" ("%__MVNW_CMD__%" %*) +@echo Cannot start maven from wrapper >&2 && exit /b 1 +@GOTO :EOF +: end batch / begin powershell #> + +$ErrorActionPreference = "Stop" +if ($env:MVNW_VERBOSE -eq "true") { + $VerbosePreference = "Continue" +} + +# calculate distributionUrl, requires .mvn/wrapper/maven-wrapper.properties +$distributionUrl = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionUrl +if (!$distributionUrl) { + Write-Error "cannot read distributionUrl property in $scriptDir/.mvn/wrapper/maven-wrapper.properties" +} + +switch -wildcard -casesensitive ( $($distributionUrl -replace '^.*/','') ) { + "maven-mvnd-*" { + $USE_MVND = $true + $distributionUrl = $distributionUrl -replace '-bin\.[^.]*$',"-windows-amd64.zip" + $MVN_CMD = "mvnd.cmd" + break + } + default { + $USE_MVND = $false + $MVN_CMD = $script -replace '^mvnw','mvn' + break + } +} + +# apply MVNW_REPOURL and calculate MAVEN_HOME +# maven home pattern: ~/.m2/wrapper/dists/{apache-maven-,maven-mvnd--}/ +if ($env:MVNW_REPOURL) { + $MVNW_REPO_PATTERN = if ($USE_MVND -eq $False) { "/org/apache/maven/" } else { "/maven/mvnd/" } + $distributionUrl = "$env:MVNW_REPOURL$MVNW_REPO_PATTERN$($distributionUrl -replace "^.*$MVNW_REPO_PATTERN",'')" +} +$distributionUrlName = $distributionUrl -replace '^.*/','' +$distributionUrlNameMain = $distributionUrlName -replace '\.[^.]*$','' -replace '-bin$','' + +$MAVEN_M2_PATH = "$HOME/.m2" +if ($env:MAVEN_USER_HOME) { + $MAVEN_M2_PATH = "$env:MAVEN_USER_HOME" +} + +if (-not (Test-Path -Path $MAVEN_M2_PATH)) { + New-Item -Path $MAVEN_M2_PATH -ItemType Directory | Out-Null +} + +$MAVEN_WRAPPER_DISTS = $null +if ((Get-Item $MAVEN_M2_PATH).Target[0] -eq $null) { + $MAVEN_WRAPPER_DISTS = "$MAVEN_M2_PATH/wrapper/dists" +} else { + $MAVEN_WRAPPER_DISTS = (Get-Item $MAVEN_M2_PATH).Target[0] + "/wrapper/dists" +} + +$MAVEN_HOME_PARENT = "$MAVEN_WRAPPER_DISTS/$distributionUrlNameMain" +$MAVEN_HOME_NAME = ([System.Security.Cryptography.SHA256]::Create().ComputeHash([byte[]][char[]]$distributionUrl) | ForEach-Object {$_.ToString("x2")}) -join '' +$MAVEN_HOME = "$MAVEN_HOME_PARENT/$MAVEN_HOME_NAME" + +if (Test-Path -Path "$MAVEN_HOME" -PathType Container) { + Write-Verbose "found existing MAVEN_HOME at $MAVEN_HOME" + Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD" + exit $? +} + +if (! $distributionUrlNameMain -or ($distributionUrlName -eq $distributionUrlNameMain)) { + Write-Error "distributionUrl is not valid, must end with *-bin.zip, but found $distributionUrl" +} + +# prepare tmp dir +$TMP_DOWNLOAD_DIR_HOLDER = New-TemporaryFile +$TMP_DOWNLOAD_DIR = New-Item -Itemtype Directory -Path "$TMP_DOWNLOAD_DIR_HOLDER.dir" +$TMP_DOWNLOAD_DIR_HOLDER.Delete() | Out-Null +trap { + if ($TMP_DOWNLOAD_DIR.Exists) { + try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null } + catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" } + } +} + +New-Item -Itemtype Directory -Path "$MAVEN_HOME_PARENT" -Force | Out-Null + +# Download and Install Apache Maven +Write-Verbose "Couldn't find MAVEN_HOME, downloading and installing it ..." +Write-Verbose "Downloading from: $distributionUrl" +Write-Verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName" + +$webclient = New-Object System.Net.WebClient +if ($env:MVNW_USERNAME -and $env:MVNW_PASSWORD) { + $webclient.Credentials = New-Object System.Net.NetworkCredential($env:MVNW_USERNAME, $env:MVNW_PASSWORD) +} +[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 +$webclient.DownloadFile($distributionUrl, "$TMP_DOWNLOAD_DIR/$distributionUrlName") | Out-Null + +# If specified, validate the SHA-256 sum of the Maven distribution zip file +$distributionSha256Sum = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionSha256Sum +if ($distributionSha256Sum) { + if ($USE_MVND) { + Write-Error "Checksum validation is not supported for maven-mvnd. `nPlease disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." + } + Import-Module $PSHOME\Modules\Microsoft.PowerShell.Utility -Function Get-FileHash + if ((Get-FileHash "$TMP_DOWNLOAD_DIR/$distributionUrlName" -Algorithm SHA256).Hash.ToLower() -ne $distributionSha256Sum) { + Write-Error "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised. If you updated your Maven version, you need to update the specified distributionSha256Sum property." + } +} + +# unzip and move +Expand-Archive "$TMP_DOWNLOAD_DIR/$distributionUrlName" -DestinationPath "$TMP_DOWNLOAD_DIR" | Out-Null + +# Find the actual extracted directory name (handles snapshots where filename != directory name) +$actualDistributionDir = "" + +# First try the expected directory name (for regular distributions) +$expectedPath = Join-Path "$TMP_DOWNLOAD_DIR" "$distributionUrlNameMain" +$expectedMvnPath = Join-Path "$expectedPath" "bin/$MVN_CMD" +if ((Test-Path -Path $expectedPath -PathType Container) -and (Test-Path -Path $expectedMvnPath -PathType Leaf)) { + $actualDistributionDir = $distributionUrlNameMain +} + +# If not found, search for any directory with the Maven executable (for snapshots) +if (!$actualDistributionDir) { + Get-ChildItem -Path "$TMP_DOWNLOAD_DIR" -Directory | ForEach-Object { + $testPath = Join-Path $_.FullName "bin/$MVN_CMD" + if (Test-Path -Path $testPath -PathType Leaf) { + $actualDistributionDir = $_.Name + } + } +} + +if (!$actualDistributionDir) { + Write-Error "Could not find Maven distribution directory in extracted archive" +} + +Write-Verbose "Found extracted Maven distribution directory: $actualDistributionDir" +Rename-Item -Path "$TMP_DOWNLOAD_DIR/$actualDistributionDir" -NewName $MAVEN_HOME_NAME | Out-Null +try { + Move-Item -Path "$TMP_DOWNLOAD_DIR/$MAVEN_HOME_NAME" -Destination $MAVEN_HOME_PARENT | Out-Null +} catch { + if (! (Test-Path -Path "$MAVEN_HOME" -PathType Container)) { + Write-Error "fail to move MAVEN_HOME" + } +} finally { + try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null } + catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" } +} + +Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD" diff --git a/examples/kotlin-koog-engram-cloud-demo/pom.xml b/examples/kotlin-koog-engram-cloud-demo/pom.xml new file mode 100644 index 0000000..94edb50 --- /dev/null +++ b/examples/kotlin-koog-engram-cloud-demo/pom.xml @@ -0,0 +1,181 @@ + + + 4.0.0 + + dev.jamjet.examples + kotlin-koog-engram-cloud-demo + 1.0.0-SNAPSHOT + jar + + Kotlin + Koog + Engram + JamJet Cloud Demo + Reference demo: Koog (Kotlin agent framework) with Engram durable memory, observed by JamJet Cloud via OTLP/JSON + + + UTF-8 + 21 + 2.3.10 + 21 + + 3.5.13 + 0.8.0 + 0.2.0 + + + + + + + org.jetbrains.kotlin + kotlin-bom + ${kotlin.version} + pom + import + + + org.springframework.boot + spring-boot-dependencies + ${spring-boot.version} + pom + import + + + + + + + + org.springframework.boot + spring-boot-starter-web + + + + + ai.koog + koog-spring-boot-starter + ${koog.version} + + + + ai.koog + agents-features-opentelemetry-jvm + ${koog.version} + + + + + dev.jamjet + engram-spring-boot-starter + ${jamjet-engram.version} + + + + + org.jetbrains.kotlin + kotlin-stdlib + + + org.jetbrains.kotlin + kotlin-reflect + + + + org.jetbrains.kotlinx + kotlinx-coroutines-core + 1.10.0 + + + + org.jetbrains.kotlinx + kotlinx-serialization-core-jvm + 1.10.0 + + + org.jetbrains.kotlinx + kotlinx-serialization-json-jvm + 1.10.0 + + + com.fasterxml.jackson.module + jackson-module-kotlin + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + ${project.basedir}/src/main/kotlin + ${project.basedir}/src/test/kotlin + + + org.jetbrains.kotlin + kotlin-maven-plugin + ${kotlin.version} + + + -Xjsr305=strict + + + spring + + ${kotlin.compiler.jvmTarget} + + + + org.jetbrains.kotlin + kotlin-maven-allopen + ${kotlin.version} + + + + + compile + compile + + compile + + + + test-compile + test-compile + + test-compile + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + default-compile + none + + + default-testCompile + none + + + + + org.springframework.boot + spring-boot-maven-plugin + ${spring-boot.version} + + + + diff --git a/examples/kotlin-koog-engram-cloud-demo/src/main/kotlin/dev/jamjet/demo/koogengram/ChatController.kt b/examples/kotlin-koog-engram-cloud-demo/src/main/kotlin/dev/jamjet/demo/koogengram/ChatController.kt new file mode 100644 index 0000000..52bf6e3 --- /dev/null +++ b/examples/kotlin-koog-engram-cloud-demo/src/main/kotlin/dev/jamjet/demo/koogengram/ChatController.kt @@ -0,0 +1,20 @@ +package dev.jamjet.demo.koogengram + +import kotlinx.coroutines.runBlocking +import org.springframework.web.bind.annotation.PostMapping +import org.springframework.web.bind.annotation.RequestBody +import org.springframework.web.bind.annotation.RequestParam +import org.springframework.web.bind.annotation.RestController + +@RestController +class ChatController(private val agent: MemoryAgent) { + + @PostMapping("/chat") + fun chat(@RequestParam session: String, @RequestBody message: String): ChatResponse = + runBlocking { + val reply = agent.chat(session, message) + ChatResponse(session = session, reply = reply) + } + + data class ChatResponse(val session: String, val reply: String) +} diff --git a/examples/kotlin-koog-engram-cloud-demo/src/main/kotlin/dev/jamjet/demo/koogengram/DemoApplication.kt b/examples/kotlin-koog-engram-cloud-demo/src/main/kotlin/dev/jamjet/demo/koogengram/DemoApplication.kt new file mode 100644 index 0000000..d12faf1 --- /dev/null +++ b/examples/kotlin-koog-engram-cloud-demo/src/main/kotlin/dev/jamjet/demo/koogengram/DemoApplication.kt @@ -0,0 +1,11 @@ +package dev.jamjet.demo.koogengram + +import org.springframework.boot.autoconfigure.SpringBootApplication +import org.springframework.boot.runApplication + +@SpringBootApplication +class DemoApplication + +fun main(args: Array) { + runApplication(*args) +} diff --git a/examples/kotlin-koog-engram-cloud-demo/src/main/kotlin/dev/jamjet/demo/koogengram/MemoryAgent.kt b/examples/kotlin-koog-engram-cloud-demo/src/main/kotlin/dev/jamjet/demo/koogengram/MemoryAgent.kt new file mode 100644 index 0000000..7a35d2d --- /dev/null +++ b/examples/kotlin-koog-engram-cloud-demo/src/main/kotlin/dev/jamjet/demo/koogengram/MemoryAgent.kt @@ -0,0 +1,67 @@ +package dev.jamjet.demo.koogengram + +import ai.koog.agents.core.agent.AIAgent +import ai.koog.agents.core.agent.singleRunStrategy +import ai.koog.agents.core.tools.ToolRegistry +import ai.koog.agents.features.opentelemetry.feature.OpenTelemetry +import ai.koog.prompt.executor.clients.openai.OpenAIModels +import ai.koog.prompt.executor.llms.MultiLLMPromptExecutor +import ai.koog.utils.io.use +import dev.jamjet.demo.koogengram.cloud.addJamjetCloudExporter +import org.springframework.beans.factory.annotation.Value +import org.springframework.stereotype.Service + +/** + * A Koog [AIAgent] wired with Engram-backed memory tools and OpenTelemetry + * traces shipped to JamJet Cloud via OTLP/JSON. + * + * The autoconfigured `MultiLLMPromptExecutor` (provided by `koog-spring-boot-starter`) + * routes to whichever LLM client beans are present — here just OpenAI, configured + * via `ai.koog.openai.api-key`. The `addJamjetCloudExporter` extension function in + * [dev.jamjet.demo.koogengram.cloud] is the only piece of observability code in + * the entire demo — everything else (LLM spans, tool spans, cost) is captured + * automatically by the OpenTelemetry feature. + */ +@Service +class MemoryAgent( + private val promptExecutor: MultiLLMPromptExecutor, + private val memoryTools: MemoryTools, + @Value("\${jamjet.cloud.api-key}") private val jamjetApiKey: String, + @Value("\${jamjet.cloud.api-url:https://api.jamjet.dev}") private val jamjetApiUrl: String, +) { + + private val systemPrompt = """ + You are a helpful assistant with durable memory backed by Engram. + When the user shares a fact about themselves, call rememberFact to store it. + When the user asks a question that may have been answered before, call recallFacts first. + If recallFacts returns nothing relevant, say "I don't know yet" — do not guess. + Always pass the session id (provided in the user message header [session=...]) as the userId argument to both tools. + """.trimIndent() + + suspend fun chat(sessionId: String, userMessage: String): String { + val agent = AIAgent( + promptExecutor = promptExecutor, + llmModel = OpenAIModels.Chat.GPT4oMini, + toolRegistry = ToolRegistry { tools(memoryTools) }, + systemPrompt = systemPrompt, + strategy = singleRunStrategy(), + maxIterations = 10, + ) { + install(OpenTelemetry) { + setServiceInfo( + serviceName = SERVICE_NAME, + serviceVersion = SERVICE_VERSION, + ) + setVerbose(true) + addJamjetCloudExporter(apiKey = jamjetApiKey, apiUrl = jamjetApiUrl) + } + } + + return agent.use { it.run("[session=$sessionId] $userMessage") } + } + + private companion object { + const val SERVICE_NAME = "kotlin-koog-engram-demo" + const val SERVICE_VERSION = "1.0.0" + } +} diff --git a/examples/kotlin-koog-engram-cloud-demo/src/main/kotlin/dev/jamjet/demo/koogengram/MemoryTools.kt b/examples/kotlin-koog-engram-cloud-demo/src/main/kotlin/dev/jamjet/demo/koogengram/MemoryTools.kt new file mode 100644 index 0000000..f864208 --- /dev/null +++ b/examples/kotlin-koog-engram-cloud-demo/src/main/kotlin/dev/jamjet/demo/koogengram/MemoryTools.kt @@ -0,0 +1,63 @@ +package dev.jamjet.demo.koogengram + +import ai.koog.agents.core.tools.annotations.LLMDescription +import ai.koog.agents.core.tools.annotations.Tool +import ai.koog.agents.core.tools.reflect.ToolSet +import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper +import dev.jamjet.engram.EngramClient +import org.springframework.stereotype.Component + +/** + * Koog tools backed by Engram durable memory. + * + * Implements [ToolSet] so the agent can register every `@Tool`-annotated + * function in one go via `tools(memoryTools)` inside a `ToolRegistry { ... }`. + * + * The autoconfigured [EngramClient] (provided by `engram-spring-boot-starter`) + * is bound to `engram.base-url`. Both `rememberFact` and `recallFacts` scope + * memory by `userId` so multiple chat sessions stay isolated. + */ +@Component +class MemoryTools(private val engram: EngramClient) : ToolSet { + + // Engram returns List>; Koog's kotlinx-serialization-based tool layer + // can't serialize a Map back to a string for the LLM, so we hand it through + // Jackson — gives the LLM a clean JSON string to read. + private val mapper: ObjectMapper = jacksonObjectMapper() + + @Tool + @LLMDescription( + "Store a fact about the user in durable memory. " + + "Use when the user shares preferences or personal details." + ) + suspend fun rememberFact( + @LLMDescription("The fact to remember, in the user's own words") + text: String, + @LLMDescription("The user identifier — pass through the session id from the chat request") + userId: String, + ): String { + val messages = listOf(mapOf("role" to "user", "content" to text)) + val result = engram.add(messages, userId, DEMO_ORG, userId) + return "Stored fact for user $userId: $result" + } + + @Tool + @LLMDescription( + "Recall facts previously stored about the user. Returns the top matches; " + + "use when answering a question that may depend on prior context." + ) + suspend fun recallFacts( + @LLMDescription("Natural-language query describing what you're looking for") + query: String, + @LLMDescription("The user identifier — pass through the session id from the chat request") + userId: String, + ): String { + val facts = engram.recall(query, userId, DEMO_ORG, 5) + return if (facts.isEmpty()) "[]" else mapper.writeValueAsString(facts) + } + + private companion object { + const val DEMO_ORG = "demo" + } +} diff --git a/examples/kotlin-koog-engram-cloud-demo/src/main/kotlin/dev/jamjet/demo/koogengram/cloud/JamjetCloudExporter.kt b/examples/kotlin-koog-engram-cloud-demo/src/main/kotlin/dev/jamjet/demo/koogengram/cloud/JamjetCloudExporter.kt new file mode 100644 index 0000000..fe7c96f --- /dev/null +++ b/examples/kotlin-koog-engram-cloud-demo/src/main/kotlin/dev/jamjet/demo/koogengram/cloud/JamjetCloudExporter.kt @@ -0,0 +1,228 @@ +package dev.jamjet.demo.koogengram.cloud + +import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper +import io.opentelemetry.sdk.common.CompletableResultCode +import io.opentelemetry.sdk.trace.data.SpanData +import io.opentelemetry.sdk.trace.export.SpanExporter +import ai.koog.agents.features.opentelemetry.feature.OpenTelemetryConfig +import org.slf4j.LoggerFactory +import java.net.URI +import java.net.http.HttpClient +import java.net.http.HttpRequest +import java.net.http.HttpResponse +import java.time.Duration +import java.util.HexFormat + +private const val DEFAULT_JAMJET_URL = "https://api.jamjet.dev" +private val DEFAULT_TIMEOUT: Duration = Duration.ofSeconds(10) + +/** + * Configure an OpenTelemetry span exporter that ships agent traces to + * [JamJet Cloud](https://cloud.jamjet.dev) via direct OTLP/JSON intake. + * + * Mirrors the DataDog / Langfuse pattern that ships with Koog + * (`addDatadogExporter`, `addLangfuseExporter` in + * [ai.koog.agents.features.opentelemetry.integration]): a thin extension on + * [OpenTelemetryConfig] that registers an OTLP-shaped [SpanExporter] pointed at + * JamJet's `/v1/otlp/v1/traces` endpoint with `Authorization: Bearer `. + * + * Registered via [addSpanExporter][OpenTelemetryConfig.addSpanExporter], which + * wraps the exporter in a batch span processor — the cloud HTTP round-trip + * happens on a worker thread instead of blocking the agent on each span end. + * + * Typical usage: + * + * ```kotlin + * AIAgent( + * promptExecutor = openAIExecutor, + * llmModel = OpenAIModels.Chat.GPT4oMini, + * toolRegistry = ToolRegistry { tools(MyTools) }, + * ) { + * install(OpenTelemetry) { + * setServiceInfo(serviceName = "my-koog-agent", serviceVersion = "1.0") + * addJamjetCloudExporter() + * } + * } + * ``` + * + * @param apiKey JamJet Cloud project API key. If `null`, falls back to the + * `JAMJET_API_KEY` environment variable. + * @param apiUrl JamJet Cloud base URL. Defaults to `https://api.jamjet.dev`. + * Override for self-hosted deployments or local testing. + * @param timeout request timeout (default 10 s). + * + * @see JamJet Cloud + * @see JamJet OTLP intake + */ +@JvmOverloads +public fun OpenTelemetryConfig.addJamjetCloudExporter( + apiKey: String? = null, + apiUrl: String = DEFAULT_JAMJET_URL, + timeout: Duration = DEFAULT_TIMEOUT, +) { + val key = apiKey + ?: System.getenv("JAMJET_API_KEY") + ?: error( + "JAMJET_API_KEY is missing. Pass it explicitly to addJamjetCloudExporter() " + + "or set the JAMJET_API_KEY environment variable. " + + "Sign up at https://cloud.jamjet.dev to create a project key." + ) + + addSpanExporter(JamjetOtlpJsonSpanExporter("$apiUrl/v1/otlp/v1/traces", key, timeout)) +} + +/** + * Custom OTLP/JSON [SpanExporter] for JamJet Cloud's intake endpoint. + * + * The Java OTel SDK 1.37 ships only an OTLP/protobuf HTTP exporter, so we marshal + * spans to OTLP/JSON ourselves. The wire format follows the OTLP spec + * (https://opentelemetry.io/docs/specs/otlp/#json-protobuf-encoding): + * camelCase field names, hex-encoded `traceId` / `spanId`, int64 fields encoded + * as strings to preserve precision in JS clients. + */ +internal class JamjetOtlpJsonSpanExporter( + private val endpoint: String, + apiKey: String, + timeout: Duration, +) : SpanExporter { + + private val authHeader: String = "Bearer $apiKey" + private val httpClient: HttpClient = HttpClient.newBuilder() + .connectTimeout(timeout) + .build() + private val requestTimeout: Duration = timeout + // OTLP/JSON spec: omit absent fields; we already build maps with only non-null values. + private val mapper: ObjectMapper = jacksonObjectMapper() + + override fun export(spans: Collection): CompletableResultCode { + if (spans.isEmpty()) return CompletableResultCode.ofSuccess() + + val payload = OtlpJsonMarshaler.toExportRequest(spans) + val body = mapper.writeValueAsBytes(payload) + + val request = HttpRequest.newBuilder() + .uri(URI.create(endpoint)) + .timeout(requestTimeout) + .header("Content-Type", "application/json") + .header("Authorization", authHeader) + .header("User-Agent", "jamjet-koog-otlp-exporter/1.0") + .POST(HttpRequest.BodyPublishers.ofByteArray(body)) + .build() + + val result = CompletableResultCode() + httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString()) + .whenComplete { response, error -> + when { + error != null -> { + log.warn("OTLP/JSON export to {} failed: {}", endpoint, error.toString()) + result.fail() + } + response.statusCode() in 200..299 -> result.succeed() + else -> { + log.warn( + "OTLP/JSON export to {} failed: HTTP {} body={}", + endpoint, response.statusCode(), + response.body().take(MAX_LOG_BODY_CHARS), + ) + result.fail() + } + } + } + return result + } + + override fun flush(): CompletableResultCode = CompletableResultCode.ofSuccess() + + override fun shutdown(): CompletableResultCode = CompletableResultCode.ofSuccess() + + private companion object { + private val log = LoggerFactory.getLogger(JamjetOtlpJsonSpanExporter::class.java) + private const val MAX_LOG_BODY_CHARS = 500 + } +} + +/** + * Pure functions that marshal Java OTel `SpanData` into the OTLP/JSON wire shape. + * Kept separate from the [SpanExporter] so it's easy to unit-test without HTTP. + */ +internal object OtlpJsonMarshaler { + + private val HEX = HexFormat.of() + + fun toExportRequest(spans: Collection): Map { + // Group by Resource — each unique resource becomes one resourceSpans entry. + val byResource = spans.groupBy { it.resource } + val resourceSpans = byResource.map { (resource, resourceSpans) -> + // Then group by InstrumentationScope. + val byScope = resourceSpans.groupBy { it.instrumentationScopeInfo } + mapOf( + "resource" to mapOf( + "attributes" to attributesToJson(resource.attributes.asMap()), + ), + "scopeSpans" to byScope.map { (scope, scopeSpans) -> + mapOf( + "scope" to mutableMapOf("name" to scope.name).apply { + scope.version?.let { put("version", it) } + }, + "spans" to scopeSpans.map(::spanToJson), + ) + }, + ) + } + return mapOf("resourceSpans" to resourceSpans) + } + + private fun spanToJson(span: SpanData): Map = buildMap { + put("traceId", span.traceId) + put("spanId", span.spanId) + if (span.parentSpanContext.isValid) put("parentSpanId", span.parentSpanId) + put("name", span.name) + put("kind", span.kind.ordinal + 1) // OTLP SpanKind: 1=INTERNAL, 2=SERVER, 3=CLIENT, ... + put("startTimeUnixNano", span.startEpochNanos.toString()) + put("endTimeUnixNano", span.endEpochNanos.toString()) + put("attributes", attributesToJson(span.attributes.asMap())) + if (span.events.isNotEmpty()) { + put("events", span.events.map { ev -> + mapOf( + "timeUnixNano" to ev.epochNanos.toString(), + "name" to ev.name, + "attributes" to attributesToJson(ev.attributes.asMap()), + ) + }) + } + if (span.links.isNotEmpty()) { + put("links", span.links.map { link -> + mapOf( + "traceId" to link.spanContext.traceId, + "spanId" to link.spanContext.spanId, + "attributes" to attributesToJson(link.attributes.asMap()), + ) + }) + } + put( + "status", + mutableMapOf("code" to span.status.statusCode.ordinal).apply { + span.status.description.takeIf { it.isNotEmpty() }?.let { put("message", it) } + }, + ) + } + + private fun attributesToJson(attrs: Map, Any>): List> = + attrs.map { (key, value) -> + mapOf("key" to key.key, "value" to anyValue(value)) + } + + private fun anyValue(value: Any?): Map = when (value) { + null -> mapOf("stringValue" to "") + is String -> mapOf("stringValue" to value) + is Boolean -> mapOf("boolValue" to value) + is Long -> mapOf("intValue" to value.toString()) + is Int -> mapOf("intValue" to value.toString()) + is Double -> mapOf("doubleValue" to value) + is Float -> mapOf("doubleValue" to value.toDouble()) + is ByteArray -> mapOf("stringValue" to HEX.formatHex(value)) + is List<*> -> mapOf("arrayValue" to mapOf("values" to value.map { anyValue(it) })) + else -> mapOf("stringValue" to value.toString()) + } +} diff --git a/examples/kotlin-koog-engram-cloud-demo/src/main/kotlin/dev/jamjet/demo/koogengram/startup/PreflightCheck.kt b/examples/kotlin-koog-engram-cloud-demo/src/main/kotlin/dev/jamjet/demo/koogengram/startup/PreflightCheck.kt new file mode 100644 index 0000000..efb1506 --- /dev/null +++ b/examples/kotlin-koog-engram-cloud-demo/src/main/kotlin/dev/jamjet/demo/koogengram/startup/PreflightCheck.kt @@ -0,0 +1,72 @@ +package dev.jamjet.demo.koogengram.startup + +import org.springframework.beans.factory.annotation.Value +import org.springframework.boot.ApplicationArguments +import org.springframework.boot.ApplicationRunner +import org.springframework.context.annotation.Profile +import org.springframework.stereotype.Component +import java.net.URI +import java.net.http.HttpClient +import java.net.http.HttpRequest +import java.net.http.HttpResponse +import java.time.Duration + +/** + * Validates required env vars and waits for Engram before the Spring app accepts traffic. + * + * Mirrors Track 1's `PreflightCheck` so both demos fail fast with the same UX + * when keys are missing or Engram isn't reachable. + */ +@Component +@Profile("!test") +class PreflightCheck( + @Value("\${app.engram.health-url:http://127.0.0.1:9090/health}") + private val engramHealthUrl: String, + private val env: Map = System.getenv(), +) : ApplicationRunner { + + override fun run(args: ApplicationArguments) { + validateEnv() + waitForEngram(Duration.ofSeconds(30)) + } + + fun validateEnv() { + require(!env["OPENAI_API_KEY"].isNullOrBlank()) { + "OPENAI_API_KEY is not set. Get a key at https://platform.openai.com/api-keys " + + "and put it in your .env file." + } + require(!env["JAMJET_API_KEY"].isNullOrBlank()) { + "JAMJET_API_KEY is not set. Sign up at https://cloud.jamjet.dev, create a project, " + + "and put the API key in your .env file." + } + } + + fun waitForEngram(timeout: Duration) { + val client = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(2)).build() + val request = HttpRequest.newBuilder() + .uri(URI.create(engramHealthUrl)) + .timeout(Duration.ofSeconds(2)) + .GET() + .build() + + val deadline = System.currentTimeMillis() + timeout.toMillis() + while (System.currentTimeMillis() < deadline) { + try { + val resp = client.send(request, HttpResponse.BodyHandlers.discarding()) + if (resp.statusCode() == 200) return + } catch (_: Exception) { + // retry until deadline + } + try { + Thread.sleep(1000) + } catch (e: InterruptedException) { + Thread.currentThread().interrupt() + throw IllegalStateException("Interrupted while waiting for Engram", e) + } + } + + throw IllegalStateException( + "Engram is not reachable at $engramHealthUrl. Run `docker compose up -d` first.", + ) + } +} diff --git a/examples/kotlin-koog-engram-cloud-demo/src/main/resources/application.yml b/examples/kotlin-koog-engram-cloud-demo/src/main/resources/application.yml new file mode 100644 index 0000000..b102f0f --- /dev/null +++ b/examples/kotlin-koog-engram-cloud-demo/src/main/resources/application.yml @@ -0,0 +1,37 @@ +server: + address: 127.0.0.1 + # 8181 to avoid colliding with JamJet Cloud's local stack on 8080. If you're running + # against the hosted https://api.jamjet.dev, you can use 8080 here without conflict. + port: 8181 + +spring: + application: + name: kotlin-koog-engram-cloud-demo + +# Koog's Spring Boot starter — autoconfigures the OpenAI LLM client + executor beans. +# `enabled=true` and `base-url=https://api.openai.com` are pre-set in the starter's +# default property file (META-INF/config/koog/openai-llm.properties); we only need +# to supply the API key. +ai: + koog: + openai: + api-key: ${OPENAI_API_KEY} + +# Engram Spring Boot starter — autoconfigures EngramClient against engram.base-url. +engram: + base-url: ${ENGRAM_BASE_URL:http://127.0.0.1:9090} + +# JamJet Cloud — used by JamjetCloudExporter (in dev/jamjet/demo/koogengram/cloud). +jamjet: + cloud: + api-key: ${JAMJET_API_KEY} + api-url: ${JAMJET_API_URL:https://api.jamjet.dev} + +app: + engram: + health-url: ${ENGRAM_BASE_URL:http://127.0.0.1:9090}/health + +logging: + level: + ai.koog: INFO + dev.jamjet: INFO