@omp-provider-capabilities/core 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +28 -4
- package/src/a2a.ts +315 -0
- package/src/acp.ts +209 -0
- package/src/agent-sdk.ts +125 -0
- package/src/batch/eligibility.ts +109 -0
- package/src/batch/openai-compatible.ts +247 -0
- package/src/batch/persistence.ts +114 -0
- package/src/batch/state-machine.ts +77 -0
- package/src/credentials/resolver.ts +324 -0
- package/src/features/beta-features.ts +118 -0
- package/src/index.ts +91 -0
- package/src/mcp.ts +186 -0
- package/src/providers/capabilities.ts +397 -0
- package/src/types/oh-my-pi.d.ts +241 -0
- package/README.md +0 -3
package/package.json
CHANGED
|
@@ -1,6 +1,30 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@omp-provider-capabilities/core",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Shared infrastructure for OMP provider capabilities: batch execution, credential resolution, capability registry, A2A/ACP/MCP protocols.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "src/index.ts",
|
|
7
|
+
"types": "src/index.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsc --noEmit",
|
|
10
|
+
"test": "bun test"
|
|
11
|
+
},
|
|
12
|
+
"dependencies": {},
|
|
13
|
+
"peerDependencies": {
|
|
14
|
+
"@oh-my-pi/pi-coding-agent": "*"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"src/",
|
|
18
|
+
"!src/**/*.test.ts",
|
|
19
|
+
"package.json"
|
|
20
|
+
],
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public"
|
|
23
|
+
},
|
|
24
|
+
"keywords": [
|
|
25
|
+
"omp-package",
|
|
26
|
+
"omp-plugin",
|
|
27
|
+
"omp-extension",
|
|
28
|
+
"oh-my-pi"
|
|
29
|
+
]
|
|
30
|
+
}
|
package/src/a2a.ts
ADDED
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent-to-Agent (A2A) Protocol implementation.
|
|
3
|
+
*
|
|
4
|
+
* Implements the A2A Protocol (Linux Foundation, v1.0) for cross-provider
|
|
5
|
+
* agent delegation. A2A standardizes how agents discover each other,
|
|
6
|
+
* delegate tasks, exchange results, and manage task lifecycles.
|
|
7
|
+
*
|
|
8
|
+
* Key concepts:
|
|
9
|
+
* - AgentCard: Agent's capabilities and metadata (discovery)
|
|
10
|
+
* - Task: Delegated work unit with lifecycle states
|
|
11
|
+
* - Message: Communication between agents
|
|
12
|
+
* - Artifact: Results produced by agents
|
|
13
|
+
*
|
|
14
|
+
* Transports: JSON-RPC 2.0, HTTP+JSON/REST, gRPC
|
|
15
|
+
*
|
|
16
|
+
* @see https://a2a-protocol.org/
|
|
17
|
+
* @see https://github.com/a2aproject/a2a-js
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import type { AgentSDK, AgentConfig, AgentResult } from "./agent-sdk.js";
|
|
21
|
+
|
|
22
|
+
/** A2A task lifecycle states */
|
|
23
|
+
export enum A2ATaskStatus {
|
|
24
|
+
SUBMITTED = "submitted",
|
|
25
|
+
WORKING = "working",
|
|
26
|
+
INPUT_REQUIRED = "input-required",
|
|
27
|
+
COMPLETED = "completed",
|
|
28
|
+
FAILED = "failed",
|
|
29
|
+
CANCELLED = "cancelled",
|
|
30
|
+
TIMEOUT = "timeout",
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Agent capability description */
|
|
34
|
+
export interface A2ACapability {
|
|
35
|
+
/** Unique identifier */
|
|
36
|
+
id: string;
|
|
37
|
+
/** Human-readable name */
|
|
38
|
+
name: string;
|
|
39
|
+
/** Description of what this capability does */
|
|
40
|
+
description: string;
|
|
41
|
+
/** Input schema (JSON Schema) */
|
|
42
|
+
inputSchema?: Record<string, unknown>;
|
|
43
|
+
/** Output schema (JSON Schema) */
|
|
44
|
+
outputSchema?: Record<string, unknown>;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Agent Card — published by A2A servers for discovery */
|
|
48
|
+
export interface AgentCard {
|
|
49
|
+
/** Agent name */
|
|
50
|
+
name: string;
|
|
51
|
+
/** Agent description */
|
|
52
|
+
description: string;
|
|
53
|
+
/** Provider identifier */
|
|
54
|
+
provider: string;
|
|
55
|
+
/** A2A protocol version */
|
|
56
|
+
version: string;
|
|
57
|
+
/** Capabilities this agent offers */
|
|
58
|
+
capabilities: A2ACapability[];
|
|
59
|
+
/** URL for A2A JSON-RPC endpoint */
|
|
60
|
+
url: string;
|
|
61
|
+
/** Supported transports */
|
|
62
|
+
defaultInputModes?: string[];
|
|
63
|
+
defaultOutputModes?: string[];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** A2A message */
|
|
67
|
+
export interface A2AMessage {
|
|
68
|
+
/** Message ID */
|
|
69
|
+
messageId: string;
|
|
70
|
+
/** Task ID this message belongs to */
|
|
71
|
+
taskId: string;
|
|
72
|
+
/** Message content */
|
|
73
|
+
content: string;
|
|
74
|
+
/** Message type */
|
|
75
|
+
kind: "prompt" | "result" | "status-update" | "artifact";
|
|
76
|
+
/** Timestamp */
|
|
77
|
+
timestamp: number;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** A2A task */
|
|
81
|
+
export interface A2ATask {
|
|
82
|
+
/** Task ID */
|
|
83
|
+
id: string;
|
|
84
|
+
/** Current status */
|
|
85
|
+
status: A2ATaskStatus;
|
|
86
|
+
/** Messages exchanged for this task */
|
|
87
|
+
messages: A2AMessage[];
|
|
88
|
+
/** Artifacts produced */
|
|
89
|
+
artifacts?: Array<{ name: string; content: string; mimeType?: string }>;
|
|
90
|
+
/** Created at */
|
|
91
|
+
createdAt: number;
|
|
92
|
+
/** Updated at */
|
|
93
|
+
updatedAt: number;
|
|
94
|
+
/** Error message if failed */
|
|
95
|
+
error?: string;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** A2A server configuration */
|
|
99
|
+
export interface A2AConfig {
|
|
100
|
+
/** Agent name */
|
|
101
|
+
name: string;
|
|
102
|
+
/** Agent description */
|
|
103
|
+
description: string;
|
|
104
|
+
/** Provider identifier */
|
|
105
|
+
provider: string;
|
|
106
|
+
/** Base URL for the A2A endpoint */
|
|
107
|
+
baseUrl: string;
|
|
108
|
+
/** Agent SDK to use for execution */
|
|
109
|
+
agentSdk: AgentSDK;
|
|
110
|
+
/** Capabilities to advertise */
|
|
111
|
+
capabilities: A2ACapability[];
|
|
112
|
+
/** Task timeout in milliseconds */
|
|
113
|
+
taskTimeout?: number;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* A2A Server — exposes an agent as an A2A-compatible service.
|
|
118
|
+
*
|
|
119
|
+
* Other agents can discover this agent via its AgentCard and delegate
|
|
120
|
+
* tasks using the A2A protocol.
|
|
121
|
+
*/
|
|
122
|
+
export class A2AServer {
|
|
123
|
+
private config: A2AConfig;
|
|
124
|
+
private tasks = new Map<string, A2ATask>();
|
|
125
|
+
private agentSdk: AgentSDK;
|
|
126
|
+
|
|
127
|
+
constructor(config: A2AConfig) {
|
|
128
|
+
this.config = config;
|
|
129
|
+
this.agentSdk = config.agentSdk;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** Get the AgentCard for discovery */
|
|
133
|
+
getAgentCard(): AgentCard {
|
|
134
|
+
return {
|
|
135
|
+
name: this.config.name,
|
|
136
|
+
description: this.config.description,
|
|
137
|
+
provider: this.config.provider,
|
|
138
|
+
version: "1.0.0",
|
|
139
|
+
capabilities: this.config.capabilities,
|
|
140
|
+
url: `${this.config.baseUrl}/.well-known/agent.json`,
|
|
141
|
+
defaultInputModes: ["text"],
|
|
142
|
+
defaultOutputModes: ["text"],
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Send a task to this agent (A2A protocol method).
|
|
148
|
+
*/
|
|
149
|
+
async sendTask(message: A2AMessage): Promise<A2ATask> {
|
|
150
|
+
const taskId = message.taskId || `task-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`;
|
|
151
|
+
|
|
152
|
+
const task: A2ATask = {
|
|
153
|
+
id: taskId,
|
|
154
|
+
status: A2ATaskStatus.SUBMITTED,
|
|
155
|
+
messages: [message],
|
|
156
|
+
createdAt: Date.now(),
|
|
157
|
+
updatedAt: Date.now(),
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
this.tasks.set(taskId, task);
|
|
161
|
+
|
|
162
|
+
// Start processing asynchronously
|
|
163
|
+
this.processTask(taskId).catch((err) => {
|
|
164
|
+
const task = this.tasks.get(taskId);
|
|
165
|
+
if (task) {
|
|
166
|
+
task.status = A2ATaskStatus.FAILED;
|
|
167
|
+
task.error = err.message;
|
|
168
|
+
task.updatedAt = Date.now();
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
return task;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Get task status (A2A protocol method).
|
|
177
|
+
*/
|
|
178
|
+
getTask(taskId: string): A2ATask | undefined {
|
|
179
|
+
return this.tasks.get(taskId);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Cancel a task (A2A protocol method).
|
|
184
|
+
*/
|
|
185
|
+
cancelTask(taskId: string): A2ATask | undefined {
|
|
186
|
+
const task = this.tasks.get(taskId);
|
|
187
|
+
if (task && [A2ATaskStatus.WORKING, A2ATaskStatus.INPUT_REQUIRED].includes(task.status)) {
|
|
188
|
+
task.status = A2ATaskStatus.CANCELLED;
|
|
189
|
+
task.updatedAt = Date.now();
|
|
190
|
+
}
|
|
191
|
+
return task;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Process a task using the underlying Agent SDK.
|
|
196
|
+
*/
|
|
197
|
+
private async processTask(taskId: string): Promise<void> {
|
|
198
|
+
const task = this.tasks.get(taskId);
|
|
199
|
+
if (!task) return;
|
|
200
|
+
|
|
201
|
+
task.status = A2ATaskStatus.WORKING;
|
|
202
|
+
task.updatedAt = Date.now();
|
|
203
|
+
|
|
204
|
+
try {
|
|
205
|
+
const prompt = task.messages
|
|
206
|
+
.filter((m) => m.kind === "prompt")
|
|
207
|
+
.map((m) => m.content)
|
|
208
|
+
.join("\n");
|
|
209
|
+
|
|
210
|
+
const config: AgentConfig = {
|
|
211
|
+
model: this.config.provider,
|
|
212
|
+
instruction: this.config.description,
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
const result: AgentResult = await this.agentSdk.run(prompt, config);
|
|
216
|
+
|
|
217
|
+
if (result.error) {
|
|
218
|
+
task.status = A2ATaskStatus.FAILED;
|
|
219
|
+
task.error = result.error;
|
|
220
|
+
} else {
|
|
221
|
+
task.status = A2ATaskStatus.COMPLETED;
|
|
222
|
+
task.artifacts = [{ name: "result", content: result.output }];
|
|
223
|
+
|
|
224
|
+
// Add result message
|
|
225
|
+
task.messages.push({
|
|
226
|
+
messageId: `msg-${Date.now()}`,
|
|
227
|
+
taskId,
|
|
228
|
+
content: result.output,
|
|
229
|
+
kind: "result",
|
|
230
|
+
timestamp: Date.now(),
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
} catch (err) {
|
|
234
|
+
task.status = A2ATaskStatus.FAILED;
|
|
235
|
+
task.error = err instanceof Error ? err.message : String(err);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
task.updatedAt = Date.now();
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* A2A Client — discovers and delegates tasks to A2A-compatible agents.
|
|
244
|
+
*/
|
|
245
|
+
export class A2AClient {
|
|
246
|
+
private discoveredAgents = new Map<string, AgentCard>();
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Discover an agent by fetching its AgentCard.
|
|
250
|
+
*/
|
|
251
|
+
async discoverAgent(url: string): Promise<AgentCard> {
|
|
252
|
+
const agentCardUrl = `${url}/.well-known/agent.json`;
|
|
253
|
+
const response = await fetch(agentCardUrl);
|
|
254
|
+
|
|
255
|
+
if (!response.ok) {
|
|
256
|
+
throw new Error(`Failed to discover agent at ${agentCardUrl}: ${response.status}`);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
const card: AgentCard = await response.json();
|
|
260
|
+
this.discoveredAgents.set(card.name, card);
|
|
261
|
+
return card;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* List discovered agents.
|
|
266
|
+
*/
|
|
267
|
+
listAgents(): AgentCard[] {
|
|
268
|
+
return Array.from(this.discoveredAgents.values());
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Send a task to a remote agent.
|
|
273
|
+
*/
|
|
274
|
+
async sendTask(agentUrl: string, message: A2AMessage): Promise<A2ATask> {
|
|
275
|
+
const response = await fetch(`${agentUrl}/tasks`, {
|
|
276
|
+
method: "POST",
|
|
277
|
+
headers: { "Content-Type": "application/json" },
|
|
278
|
+
body: JSON.stringify(message),
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
if (!response.ok) {
|
|
282
|
+
throw new Error(`Failed to send task: ${response.status} ${await response.text()}`);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
return response.json();
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Get task status from a remote agent.
|
|
290
|
+
*/
|
|
291
|
+
async getTask(agentUrl: string, taskId: string): Promise<A2ATask> {
|
|
292
|
+
const response = await fetch(`${agentUrl}/tasks/${taskId}`);
|
|
293
|
+
|
|
294
|
+
if (!response.ok) {
|
|
295
|
+
throw new Error(`Failed to get task: ${response.status}`);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
return response.json();
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Cancel a task on a remote agent.
|
|
303
|
+
*/
|
|
304
|
+
async cancelTask(agentUrl: string, taskId: string): Promise<A2ATask> {
|
|
305
|
+
const response = await fetch(`${agentUrl}/tasks/${taskId}/cancel`, {
|
|
306
|
+
method: "POST",
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
if (!response.ok) {
|
|
310
|
+
throw new Error(`Failed to cancel task: ${response.status}`);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
return response.json();
|
|
314
|
+
}
|
|
315
|
+
}
|
package/src/acp.ts
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent Client Protocol (ACP) integration.
|
|
3
|
+
*
|
|
4
|
+
* ACP standardizes communication between code editors/IDEs and AI-powered
|
|
5
|
+
* coding agents. It uses JSON-RPC 2.0 over stdio for communication.
|
|
6
|
+
*
|
|
7
|
+
* Key concepts:
|
|
8
|
+
* - Sessions: Conversational context between client and agent
|
|
9
|
+
* - Prompts: User input to the agent
|
|
10
|
+
* - Responses: Agent output, including tool calls and edits
|
|
11
|
+
*
|
|
12
|
+
* @see https://github.com/agentclientprotocol/typescript-sdk
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/** ACP session state */
|
|
16
|
+
export enum ACPSessionState {
|
|
17
|
+
IDLE = "idle",
|
|
18
|
+
PROCESSING = "processing",
|
|
19
|
+
WAITING_FOR_INPUT = "waiting_for_input",
|
|
20
|
+
COMPLETED = "completed",
|
|
21
|
+
ERROR = "error",
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** ACP message */
|
|
25
|
+
export interface ACPMessage {
|
|
26
|
+
/** Message ID */
|
|
27
|
+
id: string;
|
|
28
|
+
/** Message type */
|
|
29
|
+
type: "prompt" | "response" | "tool_call" | "tool_result" | "notification";
|
|
30
|
+
/** Message content */
|
|
31
|
+
content: string;
|
|
32
|
+
/** Tool name (for tool calls/results) */
|
|
33
|
+
toolName?: string;
|
|
34
|
+
/** Tool arguments (for tool calls) */
|
|
35
|
+
toolArgs?: Record<string, unknown>;
|
|
36
|
+
/** Timestamp */
|
|
37
|
+
timestamp: number;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** ACP session */
|
|
41
|
+
export interface ACPSession {
|
|
42
|
+
/** Session ID */
|
|
43
|
+
id: string;
|
|
44
|
+
/** Current state */
|
|
45
|
+
state: ACPSessionState;
|
|
46
|
+
/** Messages in this session */
|
|
47
|
+
messages: ACPMessage[];
|
|
48
|
+
/** Created at */
|
|
49
|
+
createdAt: number;
|
|
50
|
+
/** Last activity */
|
|
51
|
+
lastActivity: number;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** ACP server info */
|
|
55
|
+
export interface ACPInfo {
|
|
56
|
+
/** Agent name */
|
|
57
|
+
name: string;
|
|
58
|
+
/** Agent version */
|
|
59
|
+
version: string;
|
|
60
|
+
/** Supported capabilities */
|
|
61
|
+
capabilities: string[];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* ACP Server — handles communication from IDE clients.
|
|
66
|
+
*
|
|
67
|
+
* Receives prompts from the IDE, processes them using the agent SDK,
|
|
68
|
+
* and returns responses including tool calls and file edits.
|
|
69
|
+
*/
|
|
70
|
+
export class ACPHandler {
|
|
71
|
+
private sessions = new Map<string, ACPSession>();
|
|
72
|
+
private info: ACPInfo;
|
|
73
|
+
|
|
74
|
+
constructor(info: ACPInfo) {
|
|
75
|
+
this.info = info;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Get server info */
|
|
79
|
+
getInfo(): ACPInfo {
|
|
80
|
+
return this.info;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Create a new session */
|
|
84
|
+
createSession(): ACPSession {
|
|
85
|
+
const sessionId = `session-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`;
|
|
86
|
+
const session: ACPSession = {
|
|
87
|
+
id: sessionId,
|
|
88
|
+
state: ACPSessionState.IDLE,
|
|
89
|
+
messages: [],
|
|
90
|
+
createdAt: Date.now(),
|
|
91
|
+
lastActivity: Date.now(),
|
|
92
|
+
};
|
|
93
|
+
this.sessions.set(sessionId, session);
|
|
94
|
+
return session;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Get a session */
|
|
98
|
+
getSession(sessionId: string): ACPSession | undefined {
|
|
99
|
+
return this.sessions.get(sessionId);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Handle a prompt from the client */
|
|
103
|
+
handlePrompt(sessionId: string, content: string): ACPMessage {
|
|
104
|
+
const session = this.sessions.get(sessionId);
|
|
105
|
+
if (!session) {
|
|
106
|
+
return {
|
|
107
|
+
id: `msg-${Date.now()}`,
|
|
108
|
+
type: "response",
|
|
109
|
+
content: `Session not found: ${sessionId}`,
|
|
110
|
+
timestamp: Date.now(),
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
session.state = ACPSessionState.PROCESSING;
|
|
115
|
+
session.lastActivity = Date.now();
|
|
116
|
+
|
|
117
|
+
const message: ACPMessage = {
|
|
118
|
+
id: `msg-${Date.now()}`,
|
|
119
|
+
type: "prompt",
|
|
120
|
+
content,
|
|
121
|
+
timestamp: Date.now(),
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
session.messages.push(message);
|
|
125
|
+
return message;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Add a response to a session */
|
|
129
|
+
addResponse(sessionId: string, content: string, type: "response" | "tool_call" | "tool_result" = "response"): ACPMessage {
|
|
130
|
+
const session = this.sessions.get(sessionId);
|
|
131
|
+
if (!session) return { id: "", type: "response", content: "Session not found", timestamp: Date.now() };
|
|
132
|
+
|
|
133
|
+
const message: ACPMessage = {
|
|
134
|
+
id: `msg-${Date.now()}`,
|
|
135
|
+
type,
|
|
136
|
+
content,
|
|
137
|
+
timestamp: Date.now(),
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
session.messages.push(message);
|
|
141
|
+
session.lastActivity = Date.now();
|
|
142
|
+
|
|
143
|
+
if (type === "response") {
|
|
144
|
+
session.state = ACPSessionState.IDLE;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return message;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** List active sessions */
|
|
151
|
+
listSessions(): ACPSession[] {
|
|
152
|
+
return Array.from(this.sessions.values());
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* ACP Client — connects to an ACP-compatible agent over stdio.
|
|
158
|
+
*
|
|
159
|
+
* Communicates using JSON-RPC 2.0 protocol.
|
|
160
|
+
*/
|
|
161
|
+
export class ACPClient {
|
|
162
|
+
private baseUrl: string;
|
|
163
|
+
|
|
164
|
+
constructor(baseUrl: string) {
|
|
165
|
+
this.baseUrl = baseUrl;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** Initialize connection */
|
|
169
|
+
async initialize(): Promise<ACPInfo> {
|
|
170
|
+
const response = await fetch(`${this.baseUrl}/initialize`, {
|
|
171
|
+
method: "POST",
|
|
172
|
+
headers: { "Content-Type": "application/json" },
|
|
173
|
+
body: JSON.stringify({
|
|
174
|
+
jsonrpc: "2.0",
|
|
175
|
+
method: "initialize",
|
|
176
|
+
params: { protocolVersion: "1.0" },
|
|
177
|
+
id: 1,
|
|
178
|
+
}),
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
if (!response.ok) {
|
|
182
|
+
throw new Error(`ACP initialization failed: ${response.status}`);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const data = await response.json();
|
|
186
|
+
return data.result;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** Send a prompt */
|
|
190
|
+
async sendPrompt(sessionId: string, content: string): Promise<ACPMessage> {
|
|
191
|
+
const response = await fetch(`${this.baseUrl}/prompt`, {
|
|
192
|
+
method: "POST",
|
|
193
|
+
headers: { "Content-Type": "application/json" },
|
|
194
|
+
body: JSON.stringify({
|
|
195
|
+
jsonrpc: "2.0",
|
|
196
|
+
method: "prompt",
|
|
197
|
+
params: { sessionId, content },
|
|
198
|
+
id: Date.now(),
|
|
199
|
+
}),
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
if (!response.ok) {
|
|
203
|
+
throw new Error(`Failed to send prompt: ${response.status}`);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const data = await response.json();
|
|
207
|
+
return data.result;
|
|
208
|
+
}
|
|
209
|
+
}
|
package/src/agent-sdk.ts
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent SDK abstraction layer.
|
|
3
|
+
*
|
|
4
|
+
* Provides a unified interface for provider-native Agent SDKs.
|
|
5
|
+
* Each provider's Agent SDK handles multi-turn agent loops, tool calling,
|
|
6
|
+
* memory, subagents, and orchestration — far beyond single API calls.
|
|
7
|
+
*
|
|
8
|
+
* Supported Agent SDKs:
|
|
9
|
+
* - Anthropic: @anthropic-ai/claude-agent-sdk (query(), subagents)
|
|
10
|
+
* - OpenAI: @openai/agents (Agent, run, handoffs)
|
|
11
|
+
* - Google: @google/adk (LlmAgent, workflows)
|
|
12
|
+
* - Mistral: @mistralai/mistralai (Beta.Agents, connectors)
|
|
13
|
+
* - xAI: @xai-official/sdk (responses API with server tools)
|
|
14
|
+
* - Groq: groq SDK + Compound agent system
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/** A single message in an agent conversation. */
|
|
18
|
+
export interface AgentMessage {
|
|
19
|
+
role: "user" | "assistant" | "system" | "tool";
|
|
20
|
+
content: string;
|
|
21
|
+
/** Tool call ID, if this message is a tool result */
|
|
22
|
+
toolCallId?: string;
|
|
23
|
+
/** Tool name, if this message is a tool result */
|
|
24
|
+
toolName?: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** A tool definition for an agent. */
|
|
28
|
+
export interface AgentTool {
|
|
29
|
+
name: string;
|
|
30
|
+
description: string;
|
|
31
|
+
/** JSON Schema for parameters */
|
|
32
|
+
parameters: Record<string, unknown>;
|
|
33
|
+
/** Execute the tool and return the result */
|
|
34
|
+
execute(input: Record<string, unknown>): Promise<string>;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Configuration for running an agent. */
|
|
38
|
+
export interface AgentConfig {
|
|
39
|
+
/** Model to use */
|
|
40
|
+
model: string;
|
|
41
|
+
/** System instruction */
|
|
42
|
+
instruction?: string;
|
|
43
|
+
/** Tools available to the agent */
|
|
44
|
+
tools?: AgentTool[];
|
|
45
|
+
/** Maximum turns (tool call + response cycles) */
|
|
46
|
+
maxTurns?: number;
|
|
47
|
+
/** Maximum total cost in USD */
|
|
48
|
+
maxCost?: number;
|
|
49
|
+
/** Conversation history to continue from */
|
|
50
|
+
history?: AgentMessage[];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Result from running an agent. */
|
|
54
|
+
export interface AgentResult {
|
|
55
|
+
/** Final output from the agent */
|
|
56
|
+
output: string;
|
|
57
|
+
/** Full conversation history */
|
|
58
|
+
history: AgentMessage[];
|
|
59
|
+
/** Number of tool calls made */
|
|
60
|
+
toolCallCount: number;
|
|
61
|
+
/** Token usage */
|
|
62
|
+
usage?: { inputTokens: number; outputTokens: number; totalTokens: number };
|
|
63
|
+
/** Estimated cost in USD */
|
|
64
|
+
cost?: number;
|
|
65
|
+
/** Error if the agent failed */
|
|
66
|
+
error?: string;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Unified Agent SDK interface.
|
|
71
|
+
* Implementations wrap provider-native Agent SDKs.
|
|
72
|
+
*/
|
|
73
|
+
export interface AgentSDK {
|
|
74
|
+
/** Provider identifier */
|
|
75
|
+
provider: string;
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Run an agent with the given prompt and config.
|
|
79
|
+
* Handles the full agent loop: tool calls, responses, memory.
|
|
80
|
+
*/
|
|
81
|
+
run(prompt: string, config: AgentConfig): Promise<AgentResult>;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Run an agent with streaming output.
|
|
85
|
+
* Yields messages as the agent works through the task.
|
|
86
|
+
*/
|
|
87
|
+
runStream?(
|
|
88
|
+
prompt: string,
|
|
89
|
+
config: AgentConfig,
|
|
90
|
+
): AsyncIterable<AgentMessage>;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Check if the Agent SDK is available (npm package installed).
|
|
94
|
+
*/
|
|
95
|
+
isAvailable(): boolean;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Registry of Agent SDK implementations.
|
|
100
|
+
*/
|
|
101
|
+
export class AgentSDKRegistry {
|
|
102
|
+
private sdks = new Map<string, AgentSDK>();
|
|
103
|
+
|
|
104
|
+
/** Register an Agent SDK implementation. */
|
|
105
|
+
register(sdk: AgentSDK): void {
|
|
106
|
+
this.sdks.set(sdk.provider.toLowerCase(), sdk);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Get an Agent SDK by provider. */
|
|
110
|
+
get(provider: string): AgentSDK | undefined {
|
|
111
|
+
return this.sdks.get(provider.toLowerCase());
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** List available providers with Agent SDK support. */
|
|
115
|
+
listAvailable(): string[] {
|
|
116
|
+
return Array.from(this.sdks.entries())
|
|
117
|
+
.filter(([, sdk]) => sdk.isAvailable())
|
|
118
|
+
.map(([provider]) => provider);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Check if a provider has an Agent SDK. */
|
|
122
|
+
has(provider: string): boolean {
|
|
123
|
+
return this.sdks.has(provider.toLowerCase());
|
|
124
|
+
}
|
|
125
|
+
}
|