Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
e80d5f0
Spring AI samples
donald-pinckney Apr 6, 2026
de64249
Fix sample configs and remove runtimeOnly workarounds
donald-pinckney Apr 7, 2026
ddc7720
Update samples for T15: remove @DeterministicTool and sandboxing sample
donald-pinckney Apr 13, 2026
df3ec0e
Clean up Spring AI samples PR for merge
donald-pinckney Apr 20, 2026
7011199
Use mavenLocal for temporal-spring-ai instead of composite build
donald-pinckney Apr 20, 2026
7f4acba
Add TASK_QUEUE.json tracking remaining PR work
donald-pinckney Apr 20, 2026
c941ccc
Updated after changes to Spring AI
donald-pinckney Apr 27, 2026
fbae80a
Add snipsync markers to Spring AI samples
donald-pinckney Apr 28, 2026
5354c42
spring-ai samples: pin to released temporal-spring-ai 1.35.0
donald-pinckney May 1, 2026
bb4f3cf
Merge remote-tracking branch 'origin/main' into d/20260406-164121
donald-pinckney May 1, 2026
a3c05a6
Group spring-ai samples under a single springai/ directory
donald-pinckney May 1, 2026
79a9966
springai/mcp: await initialization in chat signal handler
donald-pinckney May 1, 2026
3a4e2ab
springai/multimodel: drop redundant default: CLI prefix
donald-pinckney May 1, 2026
73bb79a
springai/rag: show usage when add/search are typed without args
donald-pinckney May 1, 2026
1b47bd6
Add ai-sdk to CODEOWNERS for springai
donald-pinckney May 1, 2026
2e02881
springai/multimodel: include "think" in chat() javadoc model names
donald-pinckney May 1, 2026
1e9c367
springai/basic: explain why run()'s systemPrompt parameter is unused
donald-pinckney May 1, 2026
6b8f7ac
gradle/springai: document the Spring Boot plugin/BOM version skew
donald-pinckney May 1, 2026
00ea6f3
.gitignore: collapse per-module build/out entries into globs
donald-pinckney May 1, 2026
bd76c1a
Merge remote-tracking branch 'origin/main' into d/20260406-164121
donald-pinckney May 1, 2026
7d3c067
README: document Spring AI samples and Java 17 requirement
donald-pinckney May 1, 2026
273a2e2
springai samples: address Copilot review nits
donald-pinckney May 1, 2026
903fd2c
Untrack .vscode/ IDE settings
donald-pinckney May 1, 2026
5292b27
undo gitignore change
donald-pinckney May 1, 2026
5f91882
springai/multimodel: use LinkedHashMap for chatClients
donald-pinckney May 1, 2026
4d32397
springai/rag: capture lastResponse before signaling, not after
donald-pinckney May 1, 2026
c3a3c88
springai/multimodel: capture previous response before signaling
donald-pinckney May 1, 2026
c606d2a
Remove TASK_QUEUE.json
donald-pinckney May 1, 2026
24ba8c0
Update README.md
donald-pinckney May 1, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# Primary owners

* @tsurdilo @temporalio/sdk @antmendoza
* @tsurdilo @temporalio/sdk @antmendoza

# Below are owners for samples for modules
# that are owned by teams other than the SDK team.
# For each one, we add the owning team, as well as
# @temporalio/sdk, so the SDK team can continue to
# manage repo-wide concerns
/springai/ @temporalio/ai-sdk @temporalio/sdk
10 changes: 2 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,8 @@ target
.DS_Store
.idea
.gradle
/build
/core/build
/springboot/build
/springboot-basic/build
/out
/core/out
/springboot/out
/springboot-basic/out
**/build/
**/out/
.classpath
.project
.settings/
Expand Down
26 changes: 22 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
This repository contains samples that demonstrate various capabilities of
Temporal using the [Java SDK](https://github.com/temporalio/sdk-java).

It contains two modules:
It contains the following modules:
* [Core](/core): showcases many different SDK features.
* [SpringBoot](/springboot): showcases SpringBoot autoconfig integration.
* [SpringBoot Basic](/springboot-basic): Minimal sample showing SpringBoot autoconfig integration without any extra external dependencies.
* [Spring AI](/springai): demonstrates the Temporal Spring AI integration — durable AI agents with chat models, tools, MCP servers, vector stores, and embeddings.

## Learn more about Temporal and Java SDK

Expand All @@ -16,9 +17,7 @@ It contains two modules:

## Requirements

- Java 1.8+ for build and runtime of core samples
- Java 1.8+ for build and runtime of SpringBoot samples when using SpringBoot 2
- Java 1.17+ for build and runtime of Spring Boot samples when using SpringBoot 3
- Java 17+
- Local Temporal Server, easiest to get started would be using [Temporal CLI](https://github.com/temporalio/cli).
For more options see docs [here](https://docs.temporal.io/kb/all-the-ways-to-run-a-cluster).

Expand Down Expand Up @@ -213,3 +212,22 @@ To run any of the SpringBoot samples in your Temporal Cloud namespace:
./gradlew bootRun --args='--spring.profiles.active=tc'

3. Follow the previous section from step 2

### Running Spring AI Samples

The Spring AI samples demonstrate the [Temporal Spring AI integration](https://github.com/temporalio/sdk-java/tree/master/temporal-spring-ai), which makes Spring AI agents durable on Temporal — model calls run as Temporal Activities recorded in Workflow history, and tools are dispatched per their type so they fit Workflow execution.

Each sample is its own Spring Boot application with an interactive CLI. Run from the main repo dir:

./gradlew :springai:basic:bootRun
./gradlew :springai:mcp:bootRun
./gradlew :springai:multimodel:bootRun
./gradlew :springai:rag:bootRun

All samples need an `OPENAI_API_KEY` environment variable; some need additional setup (see each sample's source for details).

More info on each sample:
- [**Basic**](/springai/basic): Chat workflow with three tool flavors — activity-backed (`WeatherActivity`), plain workflow tools (`StringTools`), and `@SideEffectTool` (`TimestampTools`) — plus a `PromptChatMemoryAdvisor` for conversation history.
- [**MCP**](/springai/mcp): Connects to a Model Context Protocol server and exposes its tools to the AI through Temporal activities. Defaults to the filesystem MCP server.
- [**Multi-Model**](/springai/multimodel): Two providers in one workflow (OpenAI and Anthropic), per-model `ActivityOptions` overrides via a Spring bean, plus a route that exercises Anthropic's extended-thinking mode through provider-specific `ChatOptions` pass-through. Requires `ANTHROPIC_API_KEY` in addition to `OPENAI_API_KEY`.
- [**RAG**](/springai/rag): Vector store + embeddings for retrieval-augmented generation. Add documents, then ask questions; the workflow searches the vector store and grounds the answer in the retrieved context.
61 changes: 61 additions & 0 deletions gradle/springai.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Shared configuration for all Spring AI sample modules.
// Applied via: apply from: "$rootDir/gradle/springai.gradle"
//
// Note on Spring Boot version skew: the root build.gradle pins the
// org.springframework.boot Gradle plugin at $springBootPluginVersion (currently
// 2.7.13) for the legacy springboot/ samples. Spring AI 1.1.0 requires Spring
// Boot 3.5.x, which we get by importing the spring-boot-dependencies BOM at
// $springBootVersionForSpringAi below. The plugin and the BOM are independent —
// the plugin contributes bootJar/bootRun task wiring, the BOM dictates
// dependency versions — so this works in practice even though the two version
// numbers don't match. Long-term fix is to either move the plugin declaration
// out of the root plugins block (so each module applies its own version) or
// migrate the legacy springboot/ samples to Spring Boot 3.x; until one of those
// happens, this skew is intentional.

apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

ext {
springBootVersionForSpringAi = '3.5.3'
springAiVersion = '1.1.0'
}

java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

dependencyManagement {
imports {
mavenBom "org.springframework.boot:spring-boot-dependencies:$springBootVersionForSpringAi"
mavenBom "org.springframework.ai:spring-ai-bom:$springAiVersion"
}
Comment thread
donald-pinckney marked this conversation as resolved.
}

dependencies {
implementation "io.temporal:temporal-spring-boot-starter:$javaSDKVersion"
implementation "io.temporal:temporal-spring-ai:$javaSDKVersion"
// temporal-spring-ai declares temporal-sdk as compileOnly, so bring it in explicitly.
implementation "io.temporal:temporal-sdk:$javaSDKVersion"

// Spring Boot
implementation 'org.springframework.boot:spring-boot-starter'

dependencies {
errorproneJavac('com.google.errorprone:javac:9+181-r4173-1')
errorprone('com.google.errorprone:error_prone_core:2.28.0')
}
}

bootJar {
enabled = false
}

jar {
enabled = true
}

bootRun {
standardInput = System.in
}
5 changes: 5 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
rootProject.name = 'temporal-java-samples'
include 'core'
include 'springai:basic'
include 'springai:mcp'
include 'springai:multimodel'
include 'springai:rag'
include 'springboot'
include 'springboot-basic'

5 changes: 5 additions & 0 deletions springai/basic/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apply from: "$rootDir/gradle/springai.gradle"

dependencies {
implementation 'org.springframework.ai:spring-ai-starter-model-openai'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package io.temporal.samples.springai.chat;

import io.temporal.client.WorkflowClient;
import io.temporal.client.WorkflowOptions;
import java.util.Scanner;
import java.util.UUID;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
* Example application demonstrating the Spring AI Temporal plugin.
*
* <p>Starts an interactive chat workflow where each AI call is a durable Temporal activity with
* automatic retries and timeout handling.
*/
@SpringBootApplication
public class ChatExampleApplication {

public static void main(String[] args) {
SpringApplication.run(ChatExampleApplication.class, args);
}
}

@Component
class ChatRunner {

private final WorkflowClient workflowClient;

ChatRunner(WorkflowClient workflowClient) {
this.workflowClient = workflowClient;
}

@EventListener(ApplicationReadyEvent.class)
public void run() {
String workflowId = "chat-" + UUID.randomUUID().toString().substring(0, 8);

System.out.println("\n===========================================");
System.out.println(" Spring AI + Temporal Chat Demo");
System.out.println("===========================================");
System.out.println("Workflow ID: " + workflowId);
System.out.println("Type messages, or 'quit' to exit.\n");

// Start the chat workflow
ChatWorkflow workflow =
workflowClient.newWorkflowStub(
ChatWorkflow.class,
WorkflowOptions.newBuilder()
.setWorkflowId(workflowId)
.setTaskQueue("spring-ai-example")
.build());

WorkflowClient.start(workflow::run, "You are a helpful assistant. Be concise.");

// Get stub for the running workflow
ChatWorkflow chat = workflowClient.newWorkflowStub(ChatWorkflow.class, workflowId);

// Interactive loop
try (Scanner scanner = new Scanner(System.in, java.nio.charset.StandardCharsets.UTF_8)) {
while (true) {
System.out.print("You: ");
String input = scanner.nextLine().trim();

if (input.equalsIgnoreCase("quit") || input.equalsIgnoreCase("exit")) {
chat.end();
break;
}

if (input.isEmpty()) {
continue;
}

try {
String response = chat.chat(input);
System.out.println("Assistant: " + response + "\n");
} catch (Exception e) {
System.err.println("Error: " + e.getMessage() + "\n");
}
}
}

System.out.println("Goodbye!");
System.exit(0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.temporal.samples.springai.chat;

import io.temporal.workflow.SignalMethod;
import io.temporal.workflow.UpdateMethod;
import io.temporal.workflow.WorkflowInterface;
import io.temporal.workflow.WorkflowMethod;

/**
* A chat workflow that maintains a conversation with an AI model.
*
* <p>The workflow runs until explicitly ended via the {@link #end()} signal. Messages can be sent
* via the {@link #chat(String)} update method, which returns the AI's response synchronously.
*/
@WorkflowInterface
public interface ChatWorkflow {

/**
* Starts the chat workflow and waits until ended.
*
* @param systemPrompt the system prompt that defines the AI's behavior
* @return a summary when the chat ends
*/
@WorkflowMethod
String run(String systemPrompt);

/**
* Sends a message to the AI and returns its response.
*
* @param message the user's message
* @return the AI's response
*/
@UpdateMethod
String chat(String message);

/** Ends the chat session. */
@SignalMethod
void end();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package io.temporal.samples.springai.chat;

import io.temporal.activity.ActivityOptions;
import io.temporal.common.RetryOptions;
import io.temporal.springai.chat.TemporalChatClient;
import io.temporal.springai.model.ActivityChatModel;
import io.temporal.workflow.Workflow;
import io.temporal.workflow.WorkflowInit;
import java.time.Duration;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.PromptChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemoryRepository;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;

/**
* Implementation of the chat workflow using Spring AI's ChatClient with Temporal tools.
*
* <p>This demonstrates how to use the Spring AI plugin within a Temporal workflow:
*
* <ol>
* <li>Build an {@link ActivityChatModel} via its factory to get a standard Spring AI ChatModel
* backed by a durable Temporal activity
* <li>Create activity stubs for tools (e.g., {@link WeatherActivity})
* <li>Create deterministic tools (e.g., {@link StringTools})
* <li>Create side-effect tools (e.g., {@link TimestampTools})
* <li>Use {@link TemporalChatClient} to build a tool-aware chat client
* </ol>
*
* <p>The AI model can call:
*
* <ul>
* <li>{@code getWeather(city)} - Executes as a durable Temporal activity
* <li>{@code getForecast(city, days)} - Executes as a durable Temporal activity
* <li>{@code reverse(text)}, {@code countWords(text)}, etc. - Execute directly in workflow (plain
* workflow tool)
* <li>{@code getCurrentDateTime()}, {@code generateUuid()}, etc. - Wrapped in sideEffect
* (@SideEffectTool)
* </ul>
*/
public class ChatWorkflowImpl implements ChatWorkflow {

private final ChatClient chatClient;
private boolean ended = false;
private int messageCount = 0;

// @@@SNIPSTART samples-java-spring-ai-chat-workflow-init
@WorkflowInit
public ChatWorkflowImpl(String systemPrompt) {
// Build an activity-backed chat model. The factory creates the activity stub
// internally and registers per-call Summaries on the Temporal UI.
ActivityChatModel activityChatModel = ActivityChatModel.forDefault();

// Create an activity stub for weather tools - these execute as durable activities
WeatherActivity weatherTool =
Workflow.newActivityStub(
WeatherActivity.class,
ActivityOptions.newBuilder()
.setStartToCloseTimeout(Duration.ofSeconds(30))
.setRetryOptions(RetryOptions.newBuilder().setMaximumAttempts(3).build())
.build());

// Create deterministic tools - these execute directly in the workflow
StringTools stringTools = new StringTools();

// Create side-effect tools - these are wrapped in Workflow.sideEffect()
// The result is recorded in history, making replay deterministic
TimestampTools timestampTools = new TimestampTools();

// Create chat memory - uses in-memory storage that gets rebuilt on replay
ChatMemory chatMemory =
MessageWindowChatMemory.builder()
.chatMemoryRepository(new InMemoryChatMemoryRepository())
.maxMessages(20)
.build();

// Build a TemporalChatClient with tools and memory
// - Activity stubs (weatherTool) become durable AI tools
// - plain workflow tool classes (stringTools) execute directly in workflow
// - @SideEffectTool classes (timestampTools) are wrapped in sideEffect()
// - PromptChatMemoryAdvisor maintains conversation history
this.chatClient =
TemporalChatClient.builder(activityChatModel)
.defaultSystem(systemPrompt)
.defaultTools(weatherTool, stringTools, timestampTools)
.defaultAdvisors(PromptChatMemoryAdvisor.builder(chatMemory).build())
.build();
}

// @@@SNIPEND

@Override
public String run(String systemPrompt) {
// systemPrompt is unused here on purpose — @WorkflowInit requires the constructor
// and the @WorkflowMethod to share a parameter list, and the constructor above
// already consumed it to build the chat client.
Workflow.await(() -> ended);
return "Chat ended after " + messageCount + " messages.";
}
Comment thread
donald-pinckney marked this conversation as resolved.

@Override
public String chat(String message) {
messageCount++;
return chatClient.prompt().user(message).call().content();
}

@Override
public void end() {
ended = true;
}
}
Loading
Loading