Skip to content

Commit 4d0591d

Browse files
committed
Added more tests
1 parent bbe1266 commit 4d0591d

40 files changed

+3516
-604
lines changed

.vscode/cspell.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,6 +1382,16 @@
13821382
"gcch",
13831383
"updation" // current typo in a swagger
13841384
]
1385+
},
1386+
{
1387+
"filename": "sdk/ai/azure-ai-projects/**",
1388+
"words": [
1389+
"AISEARCH",
1390+
"AISERVICES",
1391+
"SUBSCRIPTIONID",
1392+
"RESOURCEGROUPNAME",
1393+
"PROJECTNAME"
1394+
]
13851395
}
13861396
],
13871397
"allowCompoundWords": true

sdk/ai/azure-ai-projects/customization/src/main/java/AIProjectsCustomizations.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public void customize(LibraryCustomization customization, Logger logger) {
2121
connectionsImpl.getFileName(),
2222
"{endpoint}/agents/v1.0");
2323

24-
for (Range r : toReplace ) {
24+
for (Range r : toReplace) {
2525
logger.info("customizing");
2626
connectionsImpl.getEditor().replace(
2727
connectionsImpl.getFileName(),
@@ -30,5 +30,23 @@ public void customize(LibraryCustomization customization, Logger logger) {
3030
"{endpoint}"
3131
);
3232
}
33+
34+
ClassCustomization telemetriesImpl = customization.getClass(
35+
"com.azure.ai.projects.implementation",
36+
"TelemetriesImpl");
37+
38+
List<Range> toReplace = telemetriesImpl.getEditor().searchText(
39+
telemetriesImpl.getFileName(),
40+
"{endpoint}/agents/v1.0");
41+
42+
for (Range r : toReplace) {
43+
logger.info("customizing");
44+
telemetriesImpl.getEditor().replace(
45+
telemetriesImpl.getFileName(),
46+
r.getStart(),
47+
r.getEnd(),
48+
"{endpoint}"
49+
);
50+
}
3351
}
3452
}

sdk/ai/azure-ai-projects/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Code generated by Microsoft (R) TypeSpec Code Generator.
4646
<spotless.skip>false</spotless.skip>
4747

4848
<javaModulesSurefireArgLine>
49+
--add-exports com.azure.core/com.azure.core.implementation.util=ALL-UNNAMED
4950
--add-opens com.azure.core/com.azure.core.implementation.util=ALL-UNNAMED
5051
</javaModulesSurefireArgLine>
5152
</properties>

sdk/ai/azure-ai-projects/src/main/java/com/azure/ai/projects/AgentsClient.java

Lines changed: 196 additions & 127 deletions
Large diffs are not rendered by default.

sdk/ai/azure-ai-projects/src/main/java/com/azure/ai/projects/implementation/TelemetriesImpl.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public final class TelemetriesImpl {
4444

4545
/**
4646
* Initializes an instance of TelemetriesImpl.
47-
*
47+
*
4848
* @param client the instance of the service client containing this operation class.
4949
*/
5050
TelemetriesImpl(AIProjectClientImpl client) {
@@ -55,7 +55,7 @@ public final class TelemetriesImpl {
5555

5656
/**
5757
* Gets Service version.
58-
*
58+
*
5959
* @return the serviceVersion value.
6060
*/
6161
public ProjectsServiceVersion getServiceVersion() {
@@ -66,7 +66,7 @@ public ProjectsServiceVersion getServiceVersion() {
6666
* The interface defining all the services for AIProjectClientTelemetries to be used by the proxy service to perform
6767
* REST calls.
6868
*/
69-
@Host("{endpoint}/agents/v1.0/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.MachineLearningServices/workspaces/{projectName}")
69+
@Host("{endpoint}/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.MachineLearningServices/workspaces/{projectName}")
7070
@ServiceInterface(name = "AIProjectClientTelem")
7171
public interface TelemetriesService {
7272
@Get("/{appInsightsResourceUrl}")
@@ -99,7 +99,7 @@ Response<BinaryData> getAppInsightsSync(@HostParam("endpoint") String endpoint,
9999
/**
100100
* Gets the properties of the specified Application Insights resource.
101101
* <p><strong>Response Body Schema</strong></p>
102-
*
102+
*
103103
* <pre>
104104
* {@code
105105
* {
@@ -111,16 +111,16 @@ Response<BinaryData> getAppInsightsSync(@HostParam("endpoint") String endpoint,
111111
* }
112112
* }
113113
* </pre>
114-
*
114+
*
115115
* @param appInsightsResourceUrl The AppInsights Azure resource Url. It should have the format:
116116
* '/subscriptions/{subscription_id}/resourceGroups/{resource_group_name}/providers/microsoft.insights/components/{resourcename}'.
117117
* @param requestOptions The options to configure the HTTP request before HTTP client sends it.
118+
* @return the properties of the specified Application Insights resource along with {@link Response} on successful
119+
* completion of {@link Mono}.
118120
* @throws HttpResponseException thrown if the request is rejected by server.
119121
* @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401.
120122
* @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404.
121123
* @throws ResourceModifiedException thrown if the request is rejected by server on status code 409.
122-
* @return the properties of the specified Application Insights resource along with {@link Response} on successful
123-
* completion of {@link Mono}.
124124
*/
125125
@ServiceMethod(returns = ReturnType.SINGLE)
126126
public Mono<Response<BinaryData>> getAppInsightsWithResponseAsync(String appInsightsResourceUrl,
@@ -134,7 +134,7 @@ public Mono<Response<BinaryData>> getAppInsightsWithResponseAsync(String appInsi
134134
/**
135135
* Gets the properties of the specified Application Insights resource.
136136
* <p><strong>Response Body Schema</strong></p>
137-
*
137+
*
138138
* <pre>
139139
* {@code
140140
* {
@@ -146,15 +146,15 @@ public Mono<Response<BinaryData>> getAppInsightsWithResponseAsync(String appInsi
146146
* }
147147
* }
148148
* </pre>
149-
*
149+
*
150150
* @param appInsightsResourceUrl The AppInsights Azure resource Url. It should have the format:
151151
* '/subscriptions/{subscription_id}/resourceGroups/{resource_group_name}/providers/microsoft.insights/components/{resourcename}'.
152152
* @param requestOptions The options to configure the HTTP request before HTTP client sends it.
153+
* @return the properties of the specified Application Insights resource along with {@link Response}.
153154
* @throws HttpResponseException thrown if the request is rejected by server.
154155
* @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401.
155156
* @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404.
156157
* @throws ResourceModifiedException thrown if the request is rejected by server on status code 409.
157-
* @return the properties of the specified Application Insights resource along with {@link Response}.
158158
*/
159159
@ServiceMethod(returns = ReturnType.SINGLE)
160160
public Response<BinaryData> getAppInsightsWithResponse(String appInsightsResourceUrl,

sdk/ai/azure-ai-projects/src/custom/java/com/azure/ai/projects/models/AgentServerSentEvents.java renamed to sdk/ai/azure-ai-projects/src/main/java/com/azure/ai/projects/models/AgentServerSentEvents.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.azure.ai.projects.models.streaming.StreamTypeFactory;
66
import com.azure.ai.projects.models.streaming.StreamUpdate;
77
import com.azure.core.util.BinaryData;
8+
import com.azure.core.util.logging.ClientLogger;
89
import reactor.core.publisher.Flux;
910
import reactor.core.scheduler.Schedulers;
1011

@@ -27,6 +28,8 @@ public final class AgentServerSentEvents {
2728
// Server sent events are divided by 2 CRLF or single LF character
2829
private static final int SSE_CHUNK_LINE_BREAK_COUNT_MARKER = 2;
2930

31+
private final ClientLogger logger = new ClientLogger(AgentServerSentEvents.class);
32+
3033
/**
3134
* A factory that determines into which type to deserialize the server sent events.
3235
*/
@@ -147,6 +150,8 @@ private boolean isByteCarriageReturn(byte character) {
147150
* @param currentEvent The current line of the server sent event.
148151
* @param outputValues The list of values to add the current line to.
149152
* @throws IllegalStateException If the current event contains a server side error.
153+
* @throws IllegalArgumentException If there's an error processing the event data.
154+
* @throws UncheckedIOException If there's an error deserializing the event data.
150155
*/
151156
public void handleCurrentEvent(String currentEvent, List<StreamUpdate> outputValues)
152157
throws IllegalArgumentException {
@@ -172,7 +177,7 @@ public void handleCurrentEvent(String currentEvent, List<StreamUpdate> outputVal
172177
return;
173178
}
174179
if (ERROR.equals(AgentStreamEvent.fromString(eventName))) {
175-
throw new IllegalArgumentException(eventJson);
180+
throw logger.logExceptionAsError(new IllegalArgumentException(eventJson));
176181
}
177182

178183
outputValues.add(this.eventDeserializer.deserializeEvent(eventName, BinaryData.fromString(eventJson)));
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
13
package com.azure.ai.projects.models.streaming;
24

35
// Copyright (c) Microsoft Corporation. All rights reserved.
@@ -10,6 +12,7 @@
1012
import com.azure.ai.projects.models.ThreadMessage;
1113
import com.azure.ai.projects.models.ThreadRun;
1214
import com.azure.core.util.BinaryData;
15+
import com.azure.core.util.logging.ClientLogger;
1316

1417
import static com.azure.ai.projects.models.AgentStreamEvent.THREAD_CREATED;
1518
import static com.azure.ai.projects.models.AgentStreamEvent.THREAD_MESSAGE_COMPLETED;
@@ -40,6 +43,14 @@
4043
* data.
4144
*/
4245
public final class StreamTypeFactory {
46+
private final ClientLogger logger = new ClientLogger(StreamTypeFactory.class);
47+
48+
/**
49+
* Default constructor for creating a new instance of StreamTypeFactory.
50+
*/
51+
public StreamTypeFactory() {
52+
// Default constructor
53+
}
4354

4455
/**
4556
* Deserializes the server sent event into the appropriate type.
@@ -82,7 +93,7 @@ public StreamUpdate deserializeEvent(String eventName, BinaryData eventJson) thr
8293
} else if (THREAD_RUN_REQUIRES_ACTION.equals(event)) {
8394
return new StreamRequiredAction(eventJson.toObject(ThreadRun.class), event);
8495
} else {
85-
throw new IllegalArgumentException("Unknown event type: " + event);
96+
throw logger.logExceptionAsError(new IllegalArgumentException("Unknown event type: " + event));
8697
}
8798
}
8899
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
/**
5+
* Package containing classes for handling streaming updates from AI Project services.
6+
* This package provides models for different types of stream events and updates.
7+
*/
8+
package com.azure.ai.projects.models.streaming;

sdk/ai/azure-ai-projects/src/main/java/module-info.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
opens com.azure.ai.projects.models to com.azure.core;
1212
opens com.azure.ai.projects.implementation.models to com.azure.core;
13+
1314
exports com.azure.ai.projects.models.streaming;
15+
1416
opens com.azure.ai.projects.models.streaming to com.azure.core;
1517
}

sdk/ai/azure-ai-projects/src/samples/java/com/azure/ai/projects/usage/agent/SampleAgentAdditionalMessage.java

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
13
package com.azure.ai.projects.usage.agent;
24

35

46
import com.azure.ai.projects.AIProjectClientBuilder;
57
import com.azure.ai.projects.AgentsClient;
6-
import com.azure.ai.projects.models.*;
7-
import com.azure.core.http.policy.HttpLogDetailLevel;
8-
import com.azure.core.http.policy.HttpLogOptions;
8+
import com.azure.ai.projects.models.Agent;
9+
import com.azure.ai.projects.models.AgentThread;
10+
import com.azure.ai.projects.models.CodeInterpreterToolDefinition;
11+
import com.azure.ai.projects.models.CreateAgentOptions;
12+
import com.azure.ai.projects.models.CreateRunOptions;
13+
import com.azure.ai.projects.models.MessageContent;
14+
import com.azure.ai.projects.models.MessageImageFileContent;
15+
import com.azure.ai.projects.models.MessageRole;
16+
import com.azure.ai.projects.models.MessageTextContent;
17+
import com.azure.ai.projects.models.OpenAIPageableListOfThreadMessage;
18+
import com.azure.ai.projects.models.RunStatus;
19+
import com.azure.ai.projects.models.ThreadMessage;
20+
import com.azure.ai.projects.models.ThreadMessageOptions;
21+
import com.azure.ai.projects.models.ThreadRun;
922
import com.azure.core.util.Configuration;
1023
import com.azure.identity.DefaultAzureCredentialBuilder;
1124
import org.junit.jupiter.api.Test;
1225

13-
import java.util.List;
14-
15-
import static org.junit.jupiter.api.Assertions.assertNotNull;
26+
import java.util.Arrays;
1627

1728
public final class SampleAgentAdditionalMessage {
1829

@@ -26,27 +37,27 @@ void additionalMessageExample() {
2637
.credential(new DefaultAzureCredentialBuilder().build())
2738
.buildAgentsClient();
2839

29-
var agentName = "additional_message_example";
30-
var createAgentOptions = new CreateAgentOptions("gpt-4o-mini")
40+
String agentName = "additional_message_example";
41+
CreateAgentOptions createAgentOptions = new CreateAgentOptions("gpt-4o-mini")
3142
.setName(agentName)
3243
.setInstructions("You are a personal electronics tutor. Write and run code to answer questions.")
33-
.setTools(List.of(new CodeInterpreterToolDefinition()));
44+
.setTools(Arrays.asList(new CodeInterpreterToolDefinition()));
3445
Agent agent = agentsClient.createAgent(createAgentOptions);
3546

36-
var thread = agentsClient.createThread();
37-
var createdMessage = agentsClient.createMessage(
47+
AgentThread thread = agentsClient.createThread();
48+
ThreadMessage createdMessage = agentsClient.createMessage(
3849
thread.getId(),
3950
MessageRole.USER,
4051
"What is the impedance formula?");
4152

4253
//run agent
43-
var createRunOptions = new CreateRunOptions(thread.getId(), agent.getId())
44-
.setAdditionalMessages(List.of(new ThreadMessageOptions(
54+
CreateRunOptions createRunOptions = new CreateRunOptions(thread.getId(), agent.getId())
55+
.setAdditionalMessages(Arrays.asList(new ThreadMessageOptions(
4556
MessageRole.AGENT, "E=mc^2"
4657
), new ThreadMessageOptions(
4758
MessageRole.USER, "What is the impedance formula?"
4859
)));
49-
var threadRun = agentsClient.createRun(createRunOptions);
60+
ThreadRun threadRun = agentsClient.createRun(createRunOptions);
5061

5162
try {
5263
do {
@@ -62,18 +73,13 @@ void additionalMessageExample() {
6273
System.out.println(threadRun.getLastError().getMessage());
6374
}
6475

65-
var runMessages = agentsClient.listMessages(thread.getId());
66-
for (ThreadMessage message : runMessages.getData())
67-
{
76+
OpenAIPageableListOfThreadMessage runMessages = agentsClient.listMessages(thread.getId());
77+
for (ThreadMessage message : runMessages.getData()) {
6878
System.out.print(String.format("%1$s - %2$s : ", message.getCreatedAt(), message.getRole()));
69-
for (MessageContent contentItem : message.getContent())
70-
{
71-
if (contentItem instanceof MessageTextContent)
72-
{
79+
for (MessageContent contentItem : message.getContent()) {
80+
if (contentItem instanceof MessageTextContent) {
7381
System.out.print((((MessageTextContent) contentItem).getText().getValue()));
74-
}
75-
else if (contentItem instanceof MessageImageFileContent)
76-
{
82+
} else if (contentItem instanceof MessageImageFileContent) {
7783
String imageFileId = (((MessageImageFileContent) contentItem).getImageFile().getFileId());
7884
System.out.print("Image from ID: " + imageFileId);
7985
}
@@ -82,8 +88,7 @@ else if (contentItem instanceof MessageImageFileContent)
8288
}
8389
} catch (InterruptedException e) {
8490
throw new RuntimeException(e);
85-
}
86-
finally {
91+
} finally {
8792
//cleanup
8893
agentsClient.deleteThread(thread.getId());
8994
agentsClient.deleteAgent(agent.getId());

0 commit comments

Comments
 (0)