|
| 1 | +#!/bin/bash |
| 2 | +set -e |
| 3 | + |
| 4 | +echo "==============================================" |
| 5 | +echo "01-create.sh - Create Spring Boot AI Agent" |
| 6 | +echo "==============================================" |
| 7 | + |
| 8 | +# Idempotency check |
| 9 | +if [ -d ~/environment/aiagent ]; then |
| 10 | + echo "~/environment/aiagent already exists, skipping creation" |
| 11 | +else |
| 12 | + cd ~/environment/ |
| 13 | + |
| 14 | + curl -s https://start.spring.io/starter.zip \ |
| 15 | + -d type=maven-project \ |
| 16 | + -d language=java \ |
| 17 | + -d packaging=jar \ |
| 18 | + -d javaVersion=25 \ |
| 19 | + -d bootVersion=4.0.2 \ |
| 20 | + -d baseDir=aiagent \ |
| 21 | + -d groupId=com.example \ |
| 22 | + -d artifactId=agent \ |
| 23 | + -d name=agent \ |
| 24 | + -d description='AI agent with Spring AI and Amazon Bedrock' \ |
| 25 | + -d dependencies=spring-ai-bedrock-converse,web,webflux,actuator \ |
| 26 | + -o aiagent.zip |
| 27 | + |
| 28 | + unzip -q aiagent.zip |
| 29 | + rm aiagent.zip |
| 30 | + |
| 31 | + cd ~/environment/aiagent |
| 32 | + git init -q |
| 33 | + git add -A |
| 34 | + git commit -q -m "Initial Spring Boot project from start.spring.io" |
| 35 | +fi |
| 36 | + |
| 37 | +echo "" |
| 38 | +echo "Project created at ~/environment/aiagent" |
| 39 | +read -p "Press ENTER to continue..." |
| 40 | + |
| 41 | +# --- Patch pom.xml: add agentcore BOM + runtime-starter --- |
| 42 | + |
| 43 | +cd ~/environment/aiagent |
| 44 | + |
| 45 | +# Add agentcore BOM to dependencyManagement (inside <dependencies>, after spring-ai-bom closing tag) |
| 46 | +if ! grep -q "spring-ai-agentcore-bom" pom.xml; then |
| 47 | + sed -i '/<artifactId>spring-ai-bom<\/artifactId>/,/<\/dependency>/{ |
| 48 | + /<\/dependency>/a \ |
| 49 | +\t\t\t<dependency>\n\t\t\t\t<groupId>org.springaicommunity</groupId>\n\t\t\t\t<artifactId>spring-ai-agentcore-bom</artifactId>\n\t\t\t\t<version>1.0.0</version>\n\t\t\t\t<type>pom</type>\n\t\t\t\t<scope>import</scope>\n\t\t\t</dependency> |
| 50 | + }' pom.xml |
| 51 | +fi |
| 52 | + |
| 53 | +# Add runtime-starter dependency (after bedrock-converse) |
| 54 | +if ! grep -q "spring-ai-agentcore-runtime-starter" pom.xml; then |
| 55 | + sed -i '/<artifactId>spring-ai-starter-model-bedrock-converse<\/artifactId>/,/<\/dependency>/{ |
| 56 | + /<\/dependency>/a \ |
| 57 | +\t\t<!-- AgentCore dependencies -->\n\t\t<dependency>\n\t\t\t<groupId>org.springaicommunity</groupId>\n\t\t\t<artifactId>spring-ai-agentcore-runtime-starter</artifactId>\n\t\t</dependency> |
| 58 | + }' pom.xml |
| 59 | +fi |
| 60 | + |
| 61 | +# --- Write application.properties --- |
| 62 | + |
| 63 | +cat > ~/environment/aiagent/src/main/resources/application.properties << 'EOF' |
| 64 | +spring.application.name=agent |
| 65 | +# Logging |
| 66 | +logging.level.org.springframework.ai=DEBUG |
| 67 | +logging.level.org.springaicommunity.agentcore=DEBUG |
| 68 | +logging.level.com.example.agent=DEBUG |
| 69 | +logging.pattern.console=%msg%n |
| 70 | +# Amazon Bedrock Configuration |
| 71 | +spring.ai.bedrock.aws.timeout=120s |
| 72 | +spring.ai.bedrock.converse.chat.options.max-tokens=4096 |
| 73 | +spring.ai.bedrock.converse.chat.options.model=global.anthropic.claude-sonnet-4-5-20250929-v1:0 |
| 74 | +spring.ai.bedrock.converse.chat.options.temperature=0.7 |
| 75 | +EOF |
| 76 | + |
| 77 | +# --- Write ChatService.java --- |
| 78 | + |
| 79 | +cat <<'EOF' > ~/environment/aiagent/src/main/java/com/example/agent/ChatService.java |
| 80 | +package com.example.agent; |
| 81 | +
|
| 82 | +import org.springaicommunity.agentcore.annotation.AgentCoreInvocation; |
| 83 | +import org.springframework.ai.chat.client.ChatClient; |
| 84 | +import org.springframework.stereotype.Service; |
| 85 | +import reactor.core.publisher.Flux; |
| 86 | +
|
| 87 | +record ChatRequest(String prompt) {} |
| 88 | +
|
| 89 | +@Service |
| 90 | +public class ChatService { |
| 91 | +
|
| 92 | + private final ChatClient chatClient; |
| 93 | +
|
| 94 | + private static final String SYSTEM_PROMPT = """ |
| 95 | + You are a helpful AI agent for travel and expense management. |
| 96 | + Be friendly, helpful, and concise in your responses. |
| 97 | + """; |
| 98 | +
|
| 99 | + public ChatService(ChatClient.Builder chatClientBuilder) { |
| 100 | + this.chatClient = chatClientBuilder |
| 101 | + .defaultSystem(SYSTEM_PROMPT) |
| 102 | + .build(); |
| 103 | + } |
| 104 | +
|
| 105 | + @AgentCoreInvocation |
| 106 | + public Flux<String> chat(ChatRequest request) { |
| 107 | + return chatClient.prompt().user(request.prompt()).stream().content(); |
| 108 | + } |
| 109 | +} |
| 110 | +EOF |
| 111 | + |
| 112 | +# --- Copy static UI files --- |
| 113 | + |
| 114 | +mkdir -p ~/environment/aiagent/src/main/resources/static |
| 115 | +cp ~/java-on-aws/apps/java-spring-ai-agents/aiagent/src/main/resources/static/* \ |
| 116 | + ~/environment/aiagent/src/main/resources/static/ |
| 117 | + |
| 118 | +echo "" |
| 119 | +echo "Code added: ChatService + properties + UI" |
| 120 | +read -p "Press ENTER to continue..." |
| 121 | + |
| 122 | +# --- Commit and run --- |
| 123 | + |
| 124 | +cd ~/environment/aiagent |
| 125 | +git add -A |
| 126 | +git commit -q -m "Add chat client with system prompt and web UI" |
| 127 | + |
| 128 | +cd ~/environment/aiagent && ./mvnw spring-boot:run |
0 commit comments