-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathnegotiationAgent.ts
More file actions
96 lines (82 loc) · 2.52 KB
/
negotiationAgent.ts
File metadata and controls
96 lines (82 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { ChatAgent } from "@virtuals-protocol/game";
import { GameFunction } from "@virtuals-protocol/game";
import { AcpNegoStatus } from "../../../src/acpContractClient";
import { NegotiationState } from "../newNegotiationAgent";
// Use ACP negotiation states instead of custom ones
export { AcpNegoStatus as NegotiationState } from "../../../src/acpContractClient";
export interface NegotiationTerms {
quantity: number;
pricePerUnit: number;
requirements: string;
}
export interface AgentConfig {
role: 'client' | 'provider';
budget?: number; // For buyers
minPrice?: number; // For sellers
maxPrice?: number; // For sellers
}
// Add helper type for external API
export type BuyerConfig = {
budget?: number;
};
export type SellerConfig = {
minPrice?: number;
maxPrice?: number;
};
export class NegotiationAgent {
private chatAgent: ChatAgent;
private chat: any;
private agentName: string;
private partnerId: string;
constructor(
apiKey: string,
systemPrompt: string,
agentName: string,
actionSpace: GameFunction<any>[],
partnerId: string = "negotiation-partner"
) {
this.chatAgent = new ChatAgent(apiKey, systemPrompt);
this.agentName = agentName;
this.partnerId = partnerId;
}
async initialize(actionSpace: GameFunction<any>[]) {
// Create chat with the action space
this.chat = await this.chatAgent.createChat({
partnerId: this.partnerId,
partnerName: this.partnerId,
actionSpace: actionSpace,
});
console.log(`🤖 ${this.agentName} initialized with ChatAgent`);
}
async sendMessage(incomingMessage: string): Promise<{
message?: string;
functionCall?: {
fn_name: string;
arguments: any;
};
isFinished?: boolean;
}> {
try {
// Use the ChatAgent's next method to process the message
const response = await this.chat.next(incomingMessage);
return {
message: response.message,
functionCall: response.functionCall ? {
fn_name: response.functionCall.fn_name,
arguments: response.functionCall.arguments
} : undefined,
isFinished: response.isFinished
};
} catch (error: any) {
console.error(`${this.agentName} error:`, error.message);
// Fallback response
return {
message: "I'm having trouble processing that. Could you rephrase?"
};
}
}
// Get conversation history for debugging
getHistory(): any {
return this.chat?.getHistory() || [];
}
}