-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose a general function for agent execution (#268)
* feat: add assistant client to public and server Signed-off-by: Yulong Ruan <[email protected]> * tweaks Signed-off-by: Yulong Ruan <[email protected]> * fix path Signed-off-by: Yulong Ruan <[email protected]> * update changelog Signed-off-by: Yulong Ruan <[email protected]> * feat: support both execute agent by name and by id Signed-off-by: Yulong Ruan <[email protected]> * fix(public): support execute agent by name or id Signed-off-by: Yulong Ruan <[email protected]> --------- Signed-off-by: Yulong Ruan <[email protected]> Signed-off-by: gaobinlong <[email protected]> Co-authored-by: gaobinlong <[email protected]> (cherry picked from commit 88aa96b) Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> # Conflicts: # CHANGELOG.md
- Loading branch information
1 parent
4df9b30
commit a0a5dc5
Showing
11 changed files
with
262 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { API_BASE } from '../../common/constants/llm'; | ||
import { HttpSetup } from '../../../../src/core/public'; | ||
|
||
interface Options { | ||
dataSourceId?: string; | ||
} | ||
|
||
export class AssistantClient { | ||
constructor(private http: HttpSetup) {} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
executeAgent = (agentId: string, parameters: Record<string, any>, options?: Options) => { | ||
return this.http.fetch({ | ||
method: 'POST', | ||
path: `${API_BASE}/agent/_execute`, | ||
body: JSON.stringify(parameters), | ||
query: { dataSourceId: options?.dataSourceId, agentId }, | ||
}); | ||
}; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
executeAgentByName = (agentName: string, parameters: Record<string, any>, options?: Options) => { | ||
return this.http.fetch({ | ||
method: 'POST', | ||
path: `${API_BASE}/agent/_execute`, | ||
body: JSON.stringify(parameters), | ||
query: { dataSourceId: options?.dataSourceId, agentName }, | ||
}); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { HttpSetup } from '../../../../src/core/public'; | ||
import { AssistantClient } from './assistant_client'; | ||
|
||
export interface AssistantServiceStart { | ||
client: AssistantClient; | ||
} | ||
|
||
export class AssistantService { | ||
constructor() {} | ||
|
||
setup() {} | ||
|
||
start(http: HttpSetup): AssistantServiceStart { | ||
const assistantClient = new AssistantClient(http); | ||
return { | ||
client: assistantClient, | ||
}; | ||
} | ||
|
||
stop() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { schema } from '@osd/config-schema'; | ||
import { IRouter } from '../../../../src/core/server'; | ||
import { AGENT_API } from '../../common/constants/llm'; | ||
import { AssistantServiceSetup } from '../services/assistant_service'; | ||
|
||
export function registerAgentRoutes(router: IRouter, assistantService: AssistantServiceSetup) { | ||
router.post( | ||
{ | ||
path: AGENT_API.EXECUTE, | ||
validate: { | ||
body: schema.any(), | ||
query: schema.oneOf([ | ||
schema.object({ | ||
dataSourceId: schema.maybe(schema.string()), | ||
agentId: schema.string(), | ||
}), | ||
schema.object({ | ||
dataSourceId: schema.maybe(schema.string()), | ||
agentName: schema.string(), | ||
}), | ||
]), | ||
}, | ||
}, | ||
router.handleLegacyErrors(async (context, req, res) => { | ||
try { | ||
const assistantClient = assistantService.getScopedClient(req, context); | ||
if ('agentId' in req.query) { | ||
const response = await assistantClient.executeAgent(req.query.agentId, req.body); | ||
return res.ok({ body: response }); | ||
} | ||
const response = await assistantClient.executeAgentByName(req.query.agentName, req.body); | ||
return res.ok({ body: response }); | ||
} catch (e) { | ||
return res.internalError(); | ||
} | ||
}) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.