-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagentService.ts
More file actions
299 lines (264 loc) · 7.83 KB
/
Copy pathagentService.ts
File metadata and controls
299 lines (264 loc) · 7.83 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/**
* Agent Service - Handles API calls for agents
*/
import { getClient } from "../utils/client.js";
import { formatTimeAgo } from "../utils/time.js";
import type { AgentView } from "@runloop/api-client/resources/agents";
// Re-export types
export type Agent = AgentView;
// ---------------------------------------------------------------------------
// Shared agent column definitions
// ---------------------------------------------------------------------------
export interface AgentColumn {
key: string;
label: string;
width: number;
getValue: (agent: Agent) => string;
}
/**
* Return the version string, prefixed with the package name when it differs
* from the agent name. Handles scoped packages (e.g. @scope/pkg) correctly.
*/
function agentVersionText(agent: Agent): string {
const src = (agent as any).source;
if (src?.type === "object") return "-";
const pkg: string | undefined =
src?.npm?.package_name || src?.pip?.package_name;
const version = agent.version || src?.git?.ref || "";
if (!version && !pkg) return "-";
// Strip leading @ and any scope prefix for comparison (e.g. "@scope/pkg" -> "pkg")
const barePkg = pkg?.replace(/^@[^/]+\//, "") ?? "";
const showPkg = pkg && barePkg !== agent.name;
if (showPkg && version) {
return `${pkg}@${version}`;
}
if (showPkg) {
return pkg!;
}
return version || "-";
}
// Fixed column widths (content + padding). These values never change.
const SOURCE_WIDTH = 10; // values: "npm", "pip", "git", "object", "-"
const ID_WIDTH = 27; // agent IDs are ~25 chars
const CREATED_WIDTH = 12; // e.g. "3d ago", "2mo ago"
const MIN_FLEX_WIDTH = 10; // minimum for each flexible column (name, version)
const FIXED_TOTAL = SOURCE_WIDTH + ID_WIDTH + CREATED_WIDTH;
/** Column spec before width calculation */
interface AgentColumnSpec {
key: string;
label: string;
getValue: (agent: Agent) => string;
/** Fixed width (used in trimToFit mode). Flex columns leave this undefined. */
fixedWidth?: number;
}
const columnSpecs: AgentColumnSpec[] = [
{ key: "name", label: "NAME", getValue: (a) => a.name },
{
key: "source",
label: "SOURCE",
fixedWidth: SOURCE_WIDTH,
getValue: (a) => (a as any).source?.type || "-",
},
{ key: "version", label: "VERSION", getValue: agentVersionText },
{ key: "id", label: "ID", fixedWidth: ID_WIDTH, getValue: (a) => a.id },
{
key: "created",
label: "CREATED",
fixedWidth: CREATED_WIDTH,
getValue: (a) => formatTimeAgo(a.create_time_ms),
},
];
/**
* Build agent column definitions with widths fitted to `availableWidth`.
*
* Column order: NAME, SOURCE, VERSION, ID, CREATED.
*
* When `trimToFit` is true (TUI), SOURCE/ID/CREATED use fixed widths and
* the remaining space is split evenly between NAME and VERSION. Content
* that overflows is truncated by the Table component.
*
* When `trimToFit` is false (CLI), each column is sized to its widest
* value (or header) plus padding. Columns may exceed `availableWidth`
* so that all content is visible and columns always align.
*/
export function getAgentColumns(
agents: Agent[],
availableWidth: number,
trimToFit = true,
): AgentColumn[] {
if (trimToFit) {
// TUI mode: fixed widths with flex split
const flexSpace = Math.max(
MIN_FLEX_WIDTH * 2,
availableWidth - FIXED_TOTAL,
);
const nameWidth = Math.ceil(flexSpace / 2);
const versionWidth = Math.floor(flexSpace / 2);
return columnSpecs.map((spec) => ({
key: spec.key,
label: spec.label,
width:
spec.fixedWidth ?? (spec.key === "name" ? nameWidth : versionWidth),
getValue: spec.getValue,
}));
}
// CLI mode: size each column to its content
const COL_PAD = 2;
return columnSpecs.map((spec) => {
let maxLen = spec.label.length;
for (const agent of agents) {
maxLen = Math.max(maxLen, spec.getValue(agent).length);
}
return {
key: spec.key,
label: spec.label,
width: maxLen + COL_PAD,
getValue: spec.getValue,
};
});
}
export interface ListAgentsOptions {
limit?: number;
startingAfter?: string;
publicOnly?: boolean;
privateOnly?: boolean;
name?: string;
search?: string;
version?: string;
includeTotalCount?: boolean;
}
export interface ListAgentsResult {
agents: Agent[];
totalCount: number;
hasMore: boolean;
}
/**
* List agents with pagination
* Can filter to only return public agents for benchmark jobs
*/
export async function listAgents(
options: ListAgentsOptions,
): Promise<ListAgentsResult> {
const client = getClient();
const queryParams: {
limit?: number;
starting_after?: string;
is_public?: boolean;
name?: string;
search?: string;
version?: string;
include_total_count?: boolean;
} = {
limit: options.limit,
};
if (options.includeTotalCount !== undefined) {
queryParams.include_total_count = options.includeTotalCount;
}
if (options.startingAfter) {
queryParams.starting_after = options.startingAfter;
}
if (options.publicOnly) {
queryParams.is_public = true;
} else if (options.privateOnly) {
queryParams.is_public = false;
}
if (options.name) {
queryParams.name = options.name;
}
if (options.search) {
queryParams.search = options.search;
}
if (options.version) {
queryParams.version = options.version;
}
// Use raw HTTP to get has_more and total_count from the API response directly
const response = await (client as any).get("/v1/agents", {
query: queryParams,
});
const agents: Agent[] = response.agents || [];
return {
agents,
totalCount: response.total_count ?? agents.length,
hasMore: response.has_more || false,
};
}
/**
* Get agent by ID
*/
export async function getAgent(id: string): Promise<Agent> {
const client = getClient();
return client.agents.retrieve(id);
}
/**
* List public agents with pagination
*/
export async function listPublicAgents(
options: ListAgentsOptions,
): Promise<ListAgentsResult> {
const client = getClient();
const queryParams: Record<string, unknown> = {
limit: options.limit,
};
if (options.startingAfter) {
queryParams.starting_after = options.startingAfter;
}
if (options.name) {
queryParams.name = options.name;
}
if (options.search) {
queryParams.search = options.search;
}
if (options.includeTotalCount !== undefined) {
queryParams.include_total_count = options.includeTotalCount;
}
// SDK doesn't have agents.listPublic yet, use raw HTTP call
const response = await (client as any).get("/v1/agents/list_public", {
query: queryParams,
});
const agents: Agent[] = response.agents || [];
return {
agents,
totalCount: response.total_count ?? agents.length,
hasMore: response.has_more || false,
};
}
export interface CreateAgentOptions {
name: string;
version?: string;
is_public?: boolean;
source?: {
type: string;
npm?: {
package_name: string;
registry_url?: string;
agent_setup?: string[];
};
pip?: {
package_name: string;
registry_url?: string;
agent_setup?: string[];
};
git?: { repository: string; ref?: string; agent_setup?: string[] };
object?: { object_id: string; agent_setup?: string[] };
};
}
/**
* Create a new agent
*/
export async function createAgent(options: CreateAgentOptions): Promise<Agent> {
const client = getClient();
const { version, is_public, ...rest } = options;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const params: any = { ...rest };
if (version) params.version = version;
if (is_public !== undefined) params.is_public = is_public;
return client.agents.create(params);
}
/**
* Delete an agent by ID
*/
export async function deleteAgent(id: string): Promise<void> {
const client = getClient();
// SDK doesn't have agents.delete yet, use raw HTTP call
await (client as any).post(`/v1/agents/${id}/delete`);
}