-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaxis-client.ts
More file actions
164 lines (145 loc) · 3.79 KB
/
axis-client.ts
File metadata and controls
164 lines (145 loc) · 3.79 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
* Axis HTTP Client
*
* Interface with the Axis CLI's headless HTTP API
*/
import { createPublicClient, http, Address } from 'viem';
import { base } from 'viem/chains';
const AXIS_API_URL = 'http://localhost:3000';
export interface AxisStatus {
status: 'running' | 'paused' | 'error';
world: string;
tick: number;
agent_address: Address;
last_action: string;
last_action_time: string;
pending_actions: number;
resources: Record<string, number>;
realms_owned: number;
armies: number;
}
export interface AxisCommand {
command: 'execute' | 'pause' | 'resume' | 'set_strategy';
params?: Record<string, any>;
}
export interface AxisMemory {
strategy: string;
current_goal: string;
priority_actions: string[];
threats: Array<{ type: string; location: number[]; size: number }>;
allies: string[];
tick_history: Array<{ tick: number; action: string; result: string }>;
}
export interface ActionResponse {
status: 'queued' | 'error';
command_id?: string;
estimated_execution?: string;
error?: {
code: string;
message: string;
action: string;
};
}
/**
* Axis HTTP Client
*/
export class AxisClient {
private baseUrl: string;
constructor(baseUrl: string = AXIS_API_URL) {
this.baseUrl = baseUrl;
}
/**
* Get current agent status
*/
async getStatus(): Promise<AxisStatus> {
const response = await fetch(`${this.baseUrl}/status`);
if (!response.ok) {
throw new Error(`Failed to get status: ${response.statusText}`);
}
return response.json();
}
/**
* Send a command to the agent
*/
async sendCommand(command: AxisCommand): Promise<ActionResponse> {
const response = await fetch(`${this.baseUrl}/command`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(command),
});
return response.json();
}
/**
* Get agent memory
*/
async getMemory(): Promise<AxisMemory> {
const response = await fetch(`${this.baseUrl}/memory`);
if (!response.ok) {
throw new Error(`Failed to get memory: ${response.statusText}`);
}
return response.json();
}
/**
* Update agent memory
*/
async updateMemory(updates: Partial<AxisMemory>): Promise<{ status: string; memory_size: number }> {
const response = await fetch(`${this.baseUrl}/memory`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ updates }),
});
return response.json();
}
/**
* Get recent actions
*/
async getActions(limit: number = 20): Promise<any[]> {
const response = await fetch(`${this.baseUrl}/actions?limit=${limit}`);
if (!response.ok) {
throw new Error(`Failed to get actions: ${response.statusText}`);
}
return response.json();
}
/**
* Get game state snapshot
*/
async getGameState(): Promise<any> {
const response = await fetch(`${this.baseUrl}/state`);
if (!response.ok) {
throw new Error(`Failed to get game state: ${response.statusText}`);
}
return response.json();
}
/**
* Execute a specific game action
*/
async executeAction(action: string, params: Record<string, any>): Promise<ActionResponse> {
return this.sendCommand({
command: 'execute',
params: { action, ...params }
});
}
/**
* Pause the agent
*/
async pause(): Promise<ActionResponse> {
return this.sendCommand({ command: 'pause' });
}
/**
* Resume the agent
*/
async resume(): Promise<ActionResponse> {
return this.sendCommand({ command: 'resume' });
}
/**
* Set agent strategy
*/
async setStrategy(strategy: string): Promise<ActionResponse> {
return this.sendCommand({
command: 'set_strategy',
params: { strategy }
});
}
}
// Export singleton
export const axisClient = new AxisClient();