Skip to content

Commit 171bf4d

Browse files
authored
refactor: move env management (#11)
1 parent e6b114d commit 171bf4d

File tree

9 files changed

+126
-114
lines changed

9 files changed

+126
-114
lines changed

apps/web/.env.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Server configuration
2+
PORT=2512
3+
4+
# Atoma SDK configuration
5+
ATOMASDK_BEARER_AUTH=your_bearer_auth_token_here

apps/web/src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ dotenv.config();
66
// Application configuration
77
export const config = {
88
port: process.env.PORT || 2512, // Default port if not specified in environment
9+
atomaSdkBearerAuth: process.env.ATOMASDK_BEARER_AUTH, // Bearer auth token for Atoma SDK
910
};

packages/sui-agent/.env.example

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/sui-agent/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@
2020
"aftermath-ts-sdk": "^1.2.49",
2121
"atoma-sdk": "github:atoma-network/atoma-sdk-typescript",
2222
"axios": "^1.7.9",
23-
"dotenv": "^16.4.7",
2423
"typescript": "^5.7.3"
2524
},
2625
"devDependencies": {
2726
"@types/node": "^20.11.19",
2827
"@typescript-eslint/eslint-plugin": "^5.0.0",
2928
"@typescript-eslint/parser": "^5.0.0",
30-
"prettier": "^3.4.2",
31-
"eslint": "^8.57.0"
29+
"eslint": "^8.57.0",
30+
"prettier": "^3.4.2"
3231
}
3332
}

packages/sui-agent/src/agents/SuiAgent.ts

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,37 @@ import { IntentAgentResponse } from '../@types/interface';
22
import Tools from '../tools/aftermath';
33
import { registerAllTools } from './ToolRegistry';
44
import Utils from '../utils';
5-
6-
/**
7-
* Tools instance for handling various tool-related operations
8-
* Used across the agent system for processing and executing tasks
9-
*/
10-
const tools = new Tools();
11-
12-
/**
13-
* Utils instance that provides utility functions
14-
* Initialized with tools instance for coordinated operations
15-
*/
16-
const utils = new Utils(tools);
5+
import intent_agent_prompt from '../prompts/intent_agent_prompt';
6+
import final_answer_agent_prompt from '../prompts/final_answer_agent';
177

188
/**
199
* Main agent class that handles intent processing and decision making
2010
* Coordinates between different agent types to process user queries
2111
*
2212
* @example
23-
* const agent = new Agents();
13+
* const agent = new Agents("your-bearer-auth-token");
2414
* const response = await agent.SuperVisorAgent("What is the current price of the Sui token?");
2515
* console.log(response);
2616
*/
2717
class Agents {
18+
private tools: Tools;
19+
private utils: Utils;
20+
21+
constructor(bearerAuth: string) {
22+
this.tools = new Tools(bearerAuth, intent_agent_prompt);
23+
this.utils = new Utils(bearerAuth, final_answer_agent_prompt);
24+
}
25+
2826
/**
2927
* Processes initial user intent and selects appropriate tools
3028
* @param prompt - User's input query
3129
* @returns IntentAgentResponse containing tool selection and processing details
3230
*/
3331
async IntentAgent(prompt: string) {
3432
// Register all available tools before processing
35-
registerAllTools(tools);
33+
registerAllTools(this.tools);
3634
const IntentResponse: IntentAgentResponse =
37-
(await tools.selectAppropriateTool(prompt)) as IntentAgentResponse;
35+
(await this.tools.selectAppropriateTool(prompt)) as IntentAgentResponse;
3836
return IntentResponse;
3937
}
4038

@@ -45,10 +43,7 @@ class Agents {
4543
* @returns Processed response after decision making
4644
*/
4745
async DecisionMakingAgent(intentResponse: any, query: string) {
48-
return await utils.makeDecision(
49-
intentResponse as IntentAgentResponse,
50-
query,
51-
);
46+
return await this.utils.processQuery(query);
5247
}
5348

5449
/**

packages/sui-agent/src/config/atoma.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
1-
import dotenv from 'dotenv';
21
import { AtomaSDK } from 'atoma-sdk';
32

4-
dotenv.config();
5-
63
const ATOMA_CHAT_COMPLETIONS_MODEL = 'meta-llama/Llama-3.3-70B-Instruct';
74

8-
// Initialize Atoma SDK with authentication
9-
const atomaSDK = new AtomaSDK({
10-
bearerAuth: process.env.ATOMASDK_BEARER_AUTH,
11-
});
5+
/**
6+
* Initialize Atoma SDK with authentication
7+
* @param bearerAuth - Bearer auth token for Atoma SDK
8+
* @returns Initialized Atoma SDK instance
9+
*/
10+
export function initializeAtomaSDK(bearerAuth: string): AtomaSDK {
11+
return new AtomaSDK({ bearerAuth });
12+
}
1213

1314
/**
1415
* Helper function to create chat completions using Atoma SDK
16+
* @param sdk - Initialized Atoma SDK instance
1517
* @param messages - Array of message objects with content and role
1618
* @param model - Optional model identifier (defaults to Llama-3.3-70B-Instruct)
1719
* @returns Chat completion response
1820
*/
1921
async function atomaChat(
22+
sdk: AtomaSDK,
2023
messages: { content: string; role: string }[],
2124
model?: string,
2225
) {
2326
try {
24-
return await atomaSDK.chat.create({
27+
return await sdk.chat.create({
2528
messages,
2629
model: model || ATOMA_CHAT_COMPLETIONS_MODEL,
2730
maxTokens: 128,
@@ -53,10 +56,14 @@ async function atomaChat(
5356
}
5457
}
5558

56-
// Health check function that returns service status
57-
async function isAtomaHealthy(): Promise<boolean> {
59+
/**
60+
* Health check function that returns service status
61+
* @param sdk - Initialized Atoma SDK instance
62+
* @returns Boolean indicating if service is healthy
63+
*/
64+
async function isAtomaHealthy(sdk: AtomaSDK): Promise<boolean> {
5865
try {
59-
await atomaSDK.health.health();
66+
await sdk.health.health();
6067
return true;
6168
} catch (error) {
6269
console.error('Atoma health check failed:', error);
@@ -65,4 +72,3 @@ async function isAtomaHealthy(): Promise<boolean> {
6572
}
6673

6774
export { atomaChat, isAtomaHealthy };
68-
export default atomaSDK;

packages/sui-agent/src/tools/aftermath/index.ts

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import intent_agent_prompt from '../../prompts/intent_agent_prompt';
2-
3-
import { Tool, ToolParameter, toolResponse } from '../../@types/interface';
1+
import { Tool, toolResponse } from '../../@types/interface';
42
import { atomaChat } from '../../config/atoma';
3+
import { AtomaSDK } from 'atoma-sdk';
54

65
/**
76
* Main tools management class
@@ -10,54 +9,53 @@ import { atomaChat } from '../../config/atoma';
109
class Tools {
1110
private tools: Tool[] = [];
1211
private prompt: string;
12+
private sdk: AtomaSDK;
1313

14-
constructor() {
15-
this.prompt = intent_agent_prompt;
14+
constructor(bearerAuth: string, prompt: string) {
15+
this.prompt = prompt;
16+
this.sdk = new AtomaSDK({ bearerAuth });
1617
}
1718

1819
/**
19-
* Register a new tool with the system
20-
* @param name - Name of the tool
21-
* @param description - Description of what the tool does
22-
* @param parameters - Parameters the tool accepts
23-
* @param process - Function to execute the tool
20+
* Register a new tool
21+
* @param name - Tool name
22+
* @param description - Tool description
23+
* @param parameters - Tool parameters
24+
* @param process - Tool process function
2425
*/
2526
registerTool(
2627
name: string,
2728
description: string,
28-
parameters: ToolParameter[],
29-
process: (...args: any[]) => Promise<string> | string,
30-
): void {
31-
const tool: Tool = {
32-
name,
33-
description,
34-
parameters,
35-
process,
36-
};
37-
this.tools.push(tool);
29+
parameters: { name: string; type: string; description: string; required: boolean }[],
30+
process: (...args: any[]) => any
31+
) {
32+
this.tools.push({ name, description, parameters, process });
3833
}
3934

4035
/**
4136
* Select appropriate tool based on user query
42-
* @param query - User's input query
43-
* @returns Selected tool response or null if no tool matches
37+
* @param query - User query
38+
* @returns Selected tool response or null if no tool found
4439
*/
4540
async selectAppropriateTool(query: string): Promise<toolResponse | null> {
4641
const finalPrompt = this.prompt.replace(
4742
'${toolsList}',
4843
JSON.stringify(this.getAllTools()),
4944
);
5045

51-
const ai: any = await atomaChat([
52-
{
53-
content: finalPrompt,
54-
role: 'system',
55-
},
56-
{
57-
content: query || '',
58-
role: 'user',
59-
},
60-
]);
46+
const ai: any = await atomaChat(
47+
this.sdk,
48+
[
49+
{
50+
content: finalPrompt,
51+
role: 'system',
52+
},
53+
{
54+
content: query || '',
55+
role: 'user',
56+
},
57+
]
58+
);
6159
const res = ai.choices[0].message.content;
6260

6361
const applicableTools: toolResponse[] = JSON.parse(res);
@@ -74,6 +72,7 @@ class Tools {
7472
return this.tools;
7573
}
7674
}
75+
7776
export default Tools;
7877

7978
// tools.registerTool("Tool 1", "Description of Tool 1", () => {

packages/sui-agent/src/utils/index.ts

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,73 @@
1-
import final_answer_agent_prompt from '../prompts/final_answer_agent';
1+
import { randomUUID } from 'crypto';
2+
import { AtomaSDK } from 'atoma-sdk';
23
import { atomaChat } from '../config/atoma';
34
import Tools from '../tools/aftermath';
45
import { IntentAgentResponse } from '../@types/interface';
5-
import { randomUUID } from 'crypto';
66

77
/**
88
* Utility class for processing agent responses and making decisions
99
* Handles the execution of tools and formatting of final responses
1010
*/
1111
class Utils {
1212
private tools: Tools;
13+
private sdk: AtomaSDK;
14+
private prompt: string;
1315

14-
constructor(tools: Tools) {
15-
this.tools = tools;
16+
constructor(bearerAuth: string, prompt: string) {
17+
this.tools = new Tools(bearerAuth, prompt);
18+
this.sdk = new AtomaSDK({ bearerAuth });
19+
this.prompt = prompt;
1620
}
1721

1822
/**
19-
* Makes decisions based on intent agent response and executes appropriate actions
20-
* @param intent_agent_response - Response from the intent agent
21-
* @param query - Original user query
22-
* @param tools - Optional tools configuration
23-
* @returns Processed and formatted response
23+
* Process user query and execute appropriate tool
24+
* @param query - User query
25+
* @returns Processed response
2426
*/
25-
async makeDecision(
26-
intent_agent_response: IntentAgentResponse,
27-
query: string,
28-
tools?: any,
29-
) {
30-
if (intent_agent_response.success && intent_agent_response.selected_tool) {
31-
return await this.executeTools(
32-
intent_agent_response.selected_tool,
33-
intent_agent_response.tool_arguments,
34-
);
35-
} else {
36-
return await this.finalAnswer(
37-
intent_agent_response.response,
27+
async processQuery(query: string): Promise<any> {
28+
try {
29+
const selectedTool = await this.tools.selectAppropriateTool(query);
30+
if (!selectedTool) {
31+
return this.finalAnswer('No tool found for the query', query);
32+
}
33+
34+
return this.executeTools(selectedTool.selected_tool || '', selectedTool.tool_arguments || []);
35+
} catch (error: unknown) {
36+
console.error('Error processing query:', error);
37+
return handleError(error, {
38+
reasoning: 'The system encountered an issue while processing your query',
3839
query,
39-
tools,
40-
);
40+
});
4141
}
4242
}
4343

4444
/**
45-
* Formats and processes the final answer using the AI model
46-
* @param response - Raw response to be formatted
47-
* @param query - Original user query
48-
* @param tools - Optional tools used in processing
49-
* @returns Formatted final response
45+
* Format final answer
46+
* @param response - Raw response
47+
* @param query - Original query
48+
* @param tools - Tools used
49+
* @returns Formatted response
5050
* @private
5151
*/
52-
private async finalAnswer(response: any, query: string, tools?: any) {
53-
const finalPrompt = final_answer_agent_prompt
52+
private async finalAnswer(
53+
response: string,
54+
query: string,
55+
tools?: string,
56+
): Promise<any> {
57+
const finalPrompt = this.prompt
5458
.replace('${query}', query)
5559
.replace('${response}', response)
5660
.replace('tools', `${tools || null}`);
5761

58-
const finalAns: any = await atomaChat([
59-
{
60-
content: finalPrompt,
61-
role: 'assistant',
62-
},
63-
]);
62+
const finalAns: any = await atomaChat(
63+
this.sdk,
64+
[
65+
{
66+
content: finalPrompt,
67+
role: 'assistant',
68+
},
69+
]
70+
);
6471
const res = finalAns.choices[0].message.content;
6572
console.log(finalPrompt);
6673
return JSON.parse(res);

0 commit comments

Comments
 (0)