Skip to content

Commit

Permalink
Agent CRUD tests and sample (#31581)
Browse files Browse the repository at this point in the history
Created Test Infrastructure for testing
Created test for agent crud operations
Create sample for agent basic


### What are the possible designs available to address the problem? If
there are more than one possible design, why was the one in this PR
chosen?


### Are there test cases added in this PR? _(If not, why?)_
Yes

### Provide a list of related PRs _(if any)_


### Command used to generate this PR:**_(Applicable only to SDK release
request PRs)_

### Checklists
- [ ] Added impacted package name to the issue description
- [ ] Does this PR needs any fixes in the SDK Generator?** _(If so,
create an Issue in the
[Autorest/typescript](https://github.com/Azure/autorest.typescript)
repository and link it here)_
- [ ] Added a changelog (if necessary)
  • Loading branch information
ganeshyb authored Oct 31, 2024
1 parent 12d2659 commit 73aaa66
Show file tree
Hide file tree
Showing 37 changed files with 342 additions and 68 deletions.
23 changes: 22 additions & 1 deletion common/config/rush/pnpm-lock.yaml

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

2 changes: 1 addition & 1 deletion rush.json
Original file line number Diff line number Diff line change
Expand Up @@ -2294,7 +2294,7 @@
},
{
"packageName": "@azure/ai-projects",
"projectFolder": "sdk/ai/azure-ai-projects",
"projectFolder": "sdk/ai/ai-projects",
"versionPolicyName": "client"
}
]
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,24 @@
"@azure/core-paging": "^1.5.0"
},
"devDependencies": {
"dotenv": "^16.0.0",
"@azure/dev-tool": "^1.0.0",
"@azure/eslint-plugin-azure-sdk": "^3.0.0",
"@azure/identity": "^4.3.0",
"@azure-tools/test-credential": "^2.0.0",
"@azure-tools/test-recorder": "^4.1.0",
"@microsoft/api-extractor": "^7.40.3",
"@vitest/browser": "^2.0.5",
"@vitest/coverage-istanbul": "^2.0.5",
"@types/node": "^18.0.0",
"dotenv": "^16.0.0",
"eslint": "^8.55.0",
"prettier": "^3.2.5",
"rimraf": "^5.0.5",
"mkdirp": "^3.0.1",
"playwright": "^1.41.2",
"typescript": "~5.5.3",
"tshy": "^1.11.1",
"@azure/dev-tool": "^1.0.0",
"@azure/eslint-plugin-azure-sdk": "^3.0.0"
"vitest": "^2.0.5"
},
"scripts": {
"clean": "rimraf --glob dist dist-browser dist-esm test-dist temp types *.tgz *.log",
Expand All @@ -86,8 +93,9 @@
"check-format": "dev-tool run vendored prettier --list-different --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.{ts,cts,mts}\" \"test/**/*.{ts,cts,mts}\" \"*.{js,cjs,mjs,json}\" ",
"execute:samples": "echo skipped",
"format": "dev-tool run vendored prettier --write --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.{ts,cts,mts}\" \"test/**/*.{ts,cts,mts}\" \"*.{js,cjs,mjs,json}\" ",
"integration-test:browser": "echo skipped",
"integration-test:node": "echo skipped",
"integration-test": "npm run integration-test:node && npm run integration-test:browser",
"integration-test:browser": "dev-tool run build-package && dev-tool run build-test && dev-tool run test:vitest --no-test-proxy --browser",
"integration-test:node": "dev-tool run test:vitest --no-test-proxy",
"generate:client": "echo skipped",
"test:browser": "npm run clean && npm run build:test && npm run unit-test:browser && npm run integration-test:browser",
"minify": "uglifyjs -c -m --comments --source-map \"content='./dist/index.js.map'\" -o ./dist/index.min.js ./dist/index.js",
Expand All @@ -102,22 +110,18 @@
"./package.json": "./package.json",
".": {
"browser": {
"source": "./src/index.ts",
"types": "./dist/browser/index.d.ts",
"default": "./dist/browser/index.js"
},
"react-native": {
"source": "./src/index.ts",
"types": "./dist/react-native/index.d.ts",
"default": "./dist/react-native/index.js"
},
"import": {
"source": "./src/index.ts",
"types": "./dist/esm/index.d.ts",
"default": "./dist/esm/index.js"
},
"require": {
"source": "./src/index.ts",
"types": "./dist/commonjs/index.d.ts",
"default": "./dist/commonjs/index.js"
}
Expand Down
86 changes: 86 additions & 0 deletions sdk/ai/ai-projects/review/ai-projects.api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
## API Report File for "@azure/ai-projects"

> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).
```ts

import { ClientOptions } from '@azure-rest/core-client';
import { Pipeline } from '@azure/core-rest-pipeline';
import { RequestParameters } from '@azure-rest/core-client';
import { TokenCredential } from '@azure/core-auth';

// @public (undocumented)
export interface AgentsOperations {
// Warning: (ae-forgotten-export) The symbol "AgentsCancelRunParameters" needs to be exported by the entry point index.d.ts
cancelRun: (threadId: string, runId: string, options?: AgentsCancelRunParameters) => Promise<ThreadRunOutput>;
// Warning: (ae-forgotten-export) The symbol "CreateAgentOptions" needs to be exported by the entry point index.d.ts
// Warning: (ae-forgotten-export) The symbol "AgentOutput" needs to be exported by the entry point index.d.ts
createAgent: (options: CreateAgentOptions) => Promise<AgentOutput>;
// Warning: (ae-forgotten-export) The symbol "AgentsCreateMessageParameters" needs to be exported by the entry point index.d.ts
// Warning: (ae-forgotten-export) The symbol "ThreadMessageOutput" needs to be exported by the entry point index.d.ts
createMessage: (threadId: string, options: AgentsCreateMessageParameters) => Promise<ThreadMessageOutput>;
// Warning: (ae-forgotten-export) The symbol "AgentsCreateRunParameters" needs to be exported by the entry point index.d.ts
// Warning: (ae-forgotten-export) The symbol "ThreadRunOutput" needs to be exported by the entry point index.d.ts
createRun: (threadId: string, options: AgentsCreateRunParameters) => Promise<ThreadRunOutput>;
// Warning: (ae-forgotten-export) The symbol "AgentsCreateThreadParameters" needs to be exported by the entry point index.d.ts
// Warning: (ae-forgotten-export) The symbol "AgentThreadOutput" needs to be exported by the entry point index.d.ts
createThread: (options: AgentsCreateThreadParameters) => Promise<AgentThreadOutput>;
// Warning: (ae-forgotten-export) The symbol "AgentsCreateThreadAndRunParameters" needs to be exported by the entry point index.d.ts
createThreadAndRun: (options: AgentsCreateThreadAndRunParameters) => Promise<ThreadRunOutput>;
// Warning: (ae-forgotten-export) The symbol "AgentDeletionStatusOutput" needs to be exported by the entry point index.d.ts
deleteAgent: (assistantId: string) => Promise<AgentDeletionStatusOutput>;
// Warning: (ae-forgotten-export) The symbol "AgentsDeleteFileParameters" needs to be exported by the entry point index.d.ts
// Warning: (ae-forgotten-export) The symbol "FileDeletionStatusOutput" needs to be exported by the entry point index.d.ts
deleteFile: (fileId: string, options?: AgentsDeleteFileParameters) => Promise<FileDeletionStatusOutput>;
// Warning: (ae-forgotten-export) The symbol "AgentsDeleteThreadParameters" needs to be exported by the entry point index.d.ts
// Warning: (ae-forgotten-export) The symbol "ThreadDeletionStatusOutput" needs to be exported by the entry point index.d.ts
deleteThread: (threadId: string, options?: AgentsDeleteThreadParameters) => Promise<ThreadDeletionStatusOutput>;
getAgent: (assistantId: string) => Promise<AgentOutput>;
// Warning: (ae-forgotten-export) The symbol "AgentsGetFileParameters" needs to be exported by the entry point index.d.ts
getFile: (fileId: string, options?: AgentsGetFileParameters) => Promise<OpenAIFileOutput>;
// Warning: (ae-forgotten-export) The symbol "AgentsGetFileContentParameters" needs to be exported by the entry point index.d.ts
// Warning: (ae-forgotten-export) The symbol "FileContentResponseOutput" needs to be exported by the entry point index.d.ts
getFileContent: (fileId: string, options?: AgentsGetFileContentParameters) => Promise<FileContentResponseOutput>;
// Warning: (ae-forgotten-export) The symbol "AgentsGetRunParameters" needs to be exported by the entry point index.d.ts
getRun: (threadId: string, runId: string, options?: AgentsGetRunParameters) => Promise<ThreadRunOutput>;
// Warning: (ae-forgotten-export) The symbol "AgentsGetThreadParameters" needs to be exported by the entry point index.d.ts
getThread: (threadId: string, options?: AgentsGetThreadParameters) => Promise<AgentThreadOutput>;
// Warning: (ae-forgotten-export) The symbol "AgentsListAgentsQueryParamProperties" needs to be exported by the entry point index.d.ts
// Warning: (ae-forgotten-export) The symbol "OpenAIPageableListOfAgentOutput" needs to be exported by the entry point index.d.ts
listAgents: (options?: AgentsListAgentsQueryParamProperties) => Promise<OpenAIPageableListOfAgentOutput>;
// Warning: (ae-forgotten-export) The symbol "AgentsListFilesParameters" needs to be exported by the entry point index.d.ts
// Warning: (ae-forgotten-export) The symbol "FileListResponseOutput" needs to be exported by the entry point index.d.ts
listFiles: (options?: AgentsListFilesParameters) => Promise<FileListResponseOutput>;
// Warning: (ae-forgotten-export) The symbol "AgentsListMessagesParameters" needs to be exported by the entry point index.d.ts
listMessages: (threadId: string, options?: AgentsListMessagesParameters) => Promise<ThreadMessageOutput>;
// Warning: (ae-forgotten-export) The symbol "AgentsListRunsParameters" needs to be exported by the entry point index.d.ts
// Warning: (ae-forgotten-export) The symbol "OpenAIPageableListOfThreadRunOutput" needs to be exported by the entry point index.d.ts
listRuns: (threadId: string, options?: AgentsListRunsParameters) => Promise<OpenAIPageableListOfThreadRunOutput>;
// Warning: (ae-forgotten-export) The symbol "AgentsSubmitToolOutputsToRunParameters" needs to be exported by the entry point index.d.ts
submitToolOutputsToRun: (threadId: string, runId: string, options: AgentsSubmitToolOutputsToRunParameters) => Promise<ThreadRunOutput>;
// Warning: (ae-forgotten-export) The symbol "UpdateAgentOptions" needs to be exported by the entry point index.d.ts
updateAgent: (assistantId: string, options: UpdateAgentOptions) => Promise<AgentOutput>;
// Warning: (ae-forgotten-export) The symbol "AgentsUpdateMessageParameters" needs to be exported by the entry point index.d.ts
updateMessage: (threadId: string, messageId: string, options: AgentsUpdateMessageParameters) => Promise<ThreadMessageOutput>;
// Warning: (ae-forgotten-export) The symbol "AgentsUpdateRunParameters" needs to be exported by the entry point index.d.ts
updateRun: (threadId: string, runId: string, options: AgentsUpdateRunParameters) => Promise<ThreadRunOutput>;
// Warning: (ae-forgotten-export) The symbol "AgentsUpdateThreadParameters" needs to be exported by the entry point index.d.ts
updateThread: (threadId: string, options: AgentsUpdateThreadParameters) => Promise<AgentThreadOutput>;
// Warning: (ae-forgotten-export) The symbol "AgentsUploadFileParameters" needs to be exported by the entry point index.d.ts
// Warning: (ae-forgotten-export) The symbol "OpenAIFileOutput" needs to be exported by the entry point index.d.ts
uploadFile: (options: AgentsUploadFileParameters) => Promise<OpenAIFileOutput>;
}

// @public (undocumented)
export class AIProjectsClient {
// Warning: (ae-forgotten-export) The symbol "AIProjectsClientOptions" needs to be exported by the entry point index.d.ts
constructor(endpointParam: string, subscriptionId: string, resourceGroupName: string, projectName: string, credential: TokenCredential, options?: AIProjectsClientOptions);
readonly agents: AgentsOperations;
// Warning: (ae-forgotten-export) The symbol "CreateProjectsClient" needs to be exported by the entry point index.d.ts
static fromConnectionString(connectionString: string, credential: TokenCredential, options?: CreateProjectsClient): AIProjectsClient;
readonly pipeline: Pipeline;
}

// (No @packageDocumentation comment for this package)

```
26 changes: 26 additions & 0 deletions sdk/ai/ai-projects/samples-dev/agents/agents_basics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import {AIProjectsClient} from "@azure/ai-projects"
import { DefaultAzureCredential } from "@azure/identity";

import * as dotenv from "dotenv";
dotenv.config();

const connectionString = process.env["AZURE_AI_PROJECTS_CONNECTION_STRING"] || "<endpoint>>;<subscription>;<resource group>;<project>";

export async function main(): Promise<void> {
const client = AIProjectsClient.fromConnectionString(connectionString || "", new DefaultAzureCredential());

const agent = await client.agents.createAgent({model:"gpt-4o", name:"my-agent", instructions:"You are helpful agent"});

console.log(`Created agent, agent ID : ${agent.id}`);

client.agents.deleteAgent(agent.id);

console.log(`Deleted agent`);
}

main().catch((err) => {
console.error("The sample encountered an error:", err);
});
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,36 @@
// Licensed under the MIT License.

import { Client } from "@azure-rest/core-client";
import { AgentDeletionStatusOutput, AgentOutput, AgentThreadOutput, FileContentResponseOutput, FileDeletionStatusOutput, FileListResponseOutput, OpenAIFileOutput, OpenAIPageableListOfAgentOutput, OpenAIPageableListOfThreadRunOutput, ThreadDeletionStatusOutput, ThreadMessageOutput, ThreadRunOutput } from "../generated/src/outputModels.js";
import { AgentsCancelRunParameters, AgentsCreateAgentParameters, AgentsCreateMessageParameters, AgentsCreateRunParameters, AgentsCreateThreadAndRunParameters, AgentsCreateThreadParameters, AgentsDeleteAgentParameters, AgentsDeleteFileParameters, AgentsDeleteThreadParameters, AgentsGetAgentParameters, AgentsGetFileContentParameters, AgentsGetFileParameters, AgentsGetRunParameters, AgentsGetThreadParameters, AgentsListAgentsParameters, AgentsListFilesParameters, AgentsListMessagesParameters, AgentsListRunsParameters, AgentsSubmitToolOutputsToRunParameters, AgentsUpdateAgentParameters, AgentsUpdateMessageParameters, AgentsUpdateRunParameters, AgentsUpdateThreadParameters, AgentsUploadFileParameters } from "../generated/src/parameters.js";
import { AgentDeletionStatusOutput, AgentOutput, AgentThreadOutput, FileContentResponseOutput, FileDeletionStatusOutput, FileListResponseOutput, OpenAIFileOutput, OpenAIPageableListOfAgentOutput, OpenAIPageableListOfThreadRunOutput, ThreadDeletionStatusOutput, ThreadMessageOutput, ThreadRunOutput } from "../generated/src/outputModels.js";
import { AgentsCancelRunParameters, AgentsCreateMessageParameters, AgentsCreateRunParameters, AgentsCreateThreadAndRunParameters, AgentsCreateThreadParameters, AgentsDeleteFileParameters, AgentsDeleteThreadParameters, AgentsGetFileContentParameters, AgentsGetFileParameters, AgentsGetRunParameters, AgentsGetThreadParameters, AgentsListAgentsQueryParamProperties, AgentsListFilesParameters, AgentsListMessagesParameters, AgentsListRunsParameters, AgentsSubmitToolOutputsToRunParameters, AgentsUpdateMessageParameters, AgentsUpdateRunParameters, AgentsUpdateThreadParameters, AgentsUploadFileParameters } from "../generated/src/parameters.js";
import { createAgent, deleteAgent, getAgent, listAgents, updateAgent } from "./assistants.js";
import { deleteFile, getFile, getFileContent, listFiles, uploadFile } from "./files.js";
import { createThread, deleteThread, getThread, updateThread } from "./threads.js";
import { cancelRun, createRun, createThreadAndRun, getRun, listRuns, submitToolOutputsToRun, updateRun } from "./runs.js";
import { createMessage, listMessages, updateMessage } from "./messages.js";
import { CreateAgentOptions, UpdateAgentOptions } from "../generated/src/models.js";

export interface AgentsOperations {
/** Creates a new agent. */
createAgent: (
options: AgentsCreateAgentParameters,
options: CreateAgentOptions,
) => Promise<AgentOutput>;
/** Gets a list of agents that were previously created. */
listAgents: (
options?: AgentsListAgentsParameters,
options?: AgentsListAgentsQueryParamProperties,
) => Promise<OpenAIPageableListOfAgentOutput>;
/** Retrieves an existing agent. */
getAgent: (
assistantId: string,
options?: AgentsGetAgentParameters,
assistantId: string
) => Promise<AgentOutput>;
/** Modifies an existing agent. */
updateAgent: (
assistantId: string,
options?: AgentsUpdateAgentParameters,
options: UpdateAgentOptions,
) => Promise<AgentOutput>;
/** Deletes an agent. */
deleteAgent: (
assistantId: string,
options?: AgentsDeleteAgentParameters,
assistantId: string
) => Promise<AgentDeletionStatusOutput>;

/** Creates a new thread. Threads contain messages and can be run by agents. */
Expand Down Expand Up @@ -137,23 +136,21 @@ export interface AgentsOperations {
) => Promise<FileContentResponseOutput>;
}

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type, @typescript-eslint/explicit-module-boundary-types
export function getAgents(context: Client) {
function getAgents(context: Client) : AgentsOperations {
return {
createAgent: (options: AgentsCreateAgentParameters) =>
createAgent(context, options),
listAgents: (options?: AgentsListAgentsParameters) =>
listAgents(context, options),
getAgent: (assistantId: string, options?: AgentsGetAgentParameters) =>
getAgent(context, assistantId, options),
createAgent: (options: CreateAgentOptions) =>
createAgent(context, { body:options }),
listAgents: (options?: AgentsListAgentsQueryParamProperties) =>
listAgents(context, { queryParameters : options as Record<string, unknown> }),
getAgent: (assistantId: string) =>
getAgent(context, assistantId),
updateAgent: (
assistantId: string,
options?: AgentsUpdateAgentParameters,
) => updateAgent(context, assistantId, options),
options: UpdateAgentOptions,
) => updateAgent(context, assistantId, { body: options }),
deleteAgent: (
assistantId: string,
options?: AgentsDeleteAgentParameters,
) => deleteAgent(context, assistantId, options),
assistantId: string
) => deleteAgent(context, assistantId),

createThread: (options: AgentsCreateThreadParameters) =>
createThread(context, options),
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ export class AIProjectsClient {
* @param credential - The credential to use
* @param options - The parameter for all optional parameters
*/
static createProjectsClient(
static fromConnectionString(
connectionString: string,
credential: TokenCredential,
options: CreateProjectsClient = {},
): AIProjectsClient {
const { endpointParam, subscriptionId, resourceGroupName, projectName } =
AIProjectsClient.fromConnectionString(connectionString);
AIProjectsClient.praseConnectionString(connectionString);
return new AIProjectsClient(
endpointParam,
subscriptionId,
Expand All @@ -67,15 +67,15 @@ export class AIProjectsClient {
);
}

private static fromConnectionString(connectionString: string): {
private static praseConnectionString(connectionString: string): {
endpointParam: string;
subscriptionId: string;
resourceGroupName: string;
projectName: string;
} {
const parts = connectionString.split(";");
return {
endpointParam: parts[0],
endpointParam: `https://${parts[0]}`,
subscriptionId: parts[1],
resourceGroupName: parts[2],
projectName: parts[3],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@

import { AIProjectsClient } from "./aiProjectsClient.js";

export {AgentsOperations } from "./agents/index.js";

export { AIProjectsClient };
Loading

0 comments on commit 73aaa66

Please sign in to comment.