@salesforce/sfdx-agent-sdk 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/LICENSE.txt +21 -0
- package/README.md +508 -0
- package/dist/agent-connectivity-resolver.d.ts +62 -0
- package/dist/agent-connectivity-resolver.js +47 -0
- package/dist/agent-connectivity-resolver.js.map +1 -0
- package/dist/agent-manager.d.ts +134 -0
- package/dist/agent-manager.js +266 -0
- package/dist/agent-manager.js.map +1 -0
- package/dist/agent.d.ts +218 -0
- package/dist/agent.js +313 -0
- package/dist/agent.js.map +1 -0
- package/dist/chat-session.d.ts +298 -0
- package/dist/chat-session.js +407 -0
- package/dist/chat-session.js.map +1 -0
- package/dist/errors.d.ts +12 -0
- package/dist/errors.js +20 -0
- package/dist/errors.js.map +1 -0
- package/dist/harness/agent-harness.d.ts +200 -0
- package/dist/harness/agent-harness.js +6 -0
- package/dist/harness/agent-harness.js.map +1 -0
- package/dist/harness/harness-bus-owner.d.ts +34 -0
- package/dist/harness/harness-bus-owner.js +78 -0
- package/dist/harness/harness-bus-owner.js.map +1 -0
- package/dist/harness/harness-config.d.ts +89 -0
- package/dist/harness/harness-config.js +26 -0
- package/dist/harness/harness-config.js.map +1 -0
- package/dist/harness/harness-factory.d.ts +21 -0
- package/dist/harness/harness-factory.js +6 -0
- package/dist/harness/harness-factory.js.map +1 -0
- package/dist/harness/index.d.ts +4 -0
- package/dist/harness/index.js +6 -0
- package/dist/harness/index.js.map +1 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +20 -0
- package/dist/index.js.map +1 -0
- package/dist/internal/telemetry-router.d.ts +41 -0
- package/dist/internal/telemetry-router.js +128 -0
- package/dist/internal/telemetry-router.js.map +1 -0
- package/dist/mcp-auth.d.ts +20 -0
- package/dist/mcp-auth.js +40 -0
- package/dist/mcp-auth.js.map +1 -0
- package/dist/mcp-config.d.ts +52 -0
- package/dist/mcp-config.js +13 -0
- package/dist/mcp-config.js.map +1 -0
- package/dist/test/tsconfig.tsbuildinfo +1 -0
- package/dist/types/events.d.ts +151 -0
- package/dist/types/events.js +6 -0
- package/dist/types/events.js.map +1 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/index.js +6 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/messages.d.ts +60 -0
- package/dist/types/messages.js +6 -0
- package/dist/types/messages.js.map +1 -0
- package/dist/types/telemetry-events.d.ts +93 -0
- package/dist/types/telemetry-events.js +6 -0
- package/dist/types/telemetry-events.js.map +1 -0
- package/dist/types/tools.d.ts +57 -0
- package/dist/types/tools.js +6 -0
- package/dist/types/tools.js.map +1 -0
- package/dist/types/usage.d.ts +31 -0
- package/dist/types/usage.js +6 -0
- package/dist/types/usage.js.map +1 -0
- package/dist/workspace.d.ts +15 -0
- package/dist/workspace.js +48 -0
- package/dist/workspace.js.map +1 -0
- package/package.json +64 -0
|
@@ -0,0 +1,407 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2026, Salesforce, Inc. All rights reserved.
|
|
3
|
+
* See LICENSE.txt for license terms.
|
|
4
|
+
*/
|
|
5
|
+
import { EventBus, LogBus, RealClock, UUIDGenerator, } from '@salesforce/agentic-common';
|
|
6
|
+
import { AgentSDKError, AgentSDKErrorType } from './errors.js';
|
|
7
|
+
/**
|
|
8
|
+
* Default implementation of {@link ChatSession} that delegates all operations
|
|
9
|
+
* to an {@link AgentHarness}. The session holds its agent ID and thread ID
|
|
10
|
+
* and forwards calls with appropriate scoping.
|
|
11
|
+
*/
|
|
12
|
+
export class DefaultChatSession {
|
|
13
|
+
harness;
|
|
14
|
+
agentId;
|
|
15
|
+
threadId;
|
|
16
|
+
chatEventBus = new EventBus();
|
|
17
|
+
telemetryBus = new EventBus();
|
|
18
|
+
logBus = new LogBus();
|
|
19
|
+
inboundUnsubs;
|
|
20
|
+
parentUnsubs;
|
|
21
|
+
clock;
|
|
22
|
+
idGenerator;
|
|
23
|
+
disposed = false;
|
|
24
|
+
/**
|
|
25
|
+
* @param harness - The agent harness managing thread and message lifecycle.
|
|
26
|
+
* @param agentId - ID of the agent this session belongs to.
|
|
27
|
+
* @param threadId - ID of the conversation thread backing this session.
|
|
28
|
+
* @param inbound - Router slice delivering harness events routed to this session.
|
|
29
|
+
* @param parent - Parent agent's buses; this session forwards its events upward into them.
|
|
30
|
+
* @param clock - Source of monotonic timestamps for telemetry events. Defaults to `RealClock`.
|
|
31
|
+
* @param idGenerator - Source of message ids for `addContext()`. Defaults to `UUIDGenerator`.
|
|
32
|
+
*/
|
|
33
|
+
constructor(harness, agentId, threadId, inbound, parent, clock = new RealClock(), idGenerator = new UUIDGenerator()) {
|
|
34
|
+
this.harness = harness;
|
|
35
|
+
this.agentId = agentId;
|
|
36
|
+
this.threadId = threadId;
|
|
37
|
+
this.clock = clock;
|
|
38
|
+
this.idGenerator = idGenerator;
|
|
39
|
+
this.inboundUnsubs = [inbound.telemetry.forwardTo(this.telemetryBus), inbound.log.forwardTo(this.logBus)];
|
|
40
|
+
this.parentUnsubs = [this.telemetryBus.forwardTo(parent.telemetry), this.logBus.forwardTo(parent.log)];
|
|
41
|
+
}
|
|
42
|
+
getId() {
|
|
43
|
+
this.assertNotDisposed();
|
|
44
|
+
return this.threadId;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* @requirements
|
|
48
|
+
* - MUST delegate to `this.harness.stream()`, passing `this.agentId` and `this.threadId`.
|
|
49
|
+
* - MUST pass the `message` string and `options` arguments directly to the harness.
|
|
50
|
+
* - MUST wrap the returned `eventStream` using `this.wrapEventStream()` so that listeners are notified.
|
|
51
|
+
* - MUST return an object containing the original `textStream` and the wrapped `eventStream`.
|
|
52
|
+
* - MUST notify listeners with `ErrorEvent` + `FinishEvent` and re-throw if the harness throws
|
|
53
|
+
* before returning a stream result.
|
|
54
|
+
*/
|
|
55
|
+
async chat(message, options) {
|
|
56
|
+
this.assertNotDisposed();
|
|
57
|
+
const startedAt = this.emitChatStreamStarted('chat');
|
|
58
|
+
try {
|
|
59
|
+
const result = await this.harness.stream(this.agentId, this.threadId, message, options);
|
|
60
|
+
return {
|
|
61
|
+
textStream: result.textStream,
|
|
62
|
+
eventStream: this.wrapEventStream(result.eventStream, startedAt),
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
catch (err) {
|
|
66
|
+
this.notifyPreStreamError(err, startedAt);
|
|
67
|
+
throw err;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* @requirements
|
|
72
|
+
* - MUST delegate to `this.harness.submitToolResult()`, passing `this.agentId` and `this.threadId`.
|
|
73
|
+
* - MUST pass the `toolResult` argument directly to the harness.
|
|
74
|
+
* - MUST wrap the returned `eventStream` using `this.wrapEventStream()` so that listeners are notified.
|
|
75
|
+
* - MUST return an object containing the original `textStream` and the wrapped `eventStream`.
|
|
76
|
+
* - MUST notify listeners with `ErrorEvent` + `FinishEvent` and re-throw if the harness throws
|
|
77
|
+
* before returning a stream result.
|
|
78
|
+
*/
|
|
79
|
+
async submitToolResult(toolResult) {
|
|
80
|
+
this.assertNotDisposed();
|
|
81
|
+
const startedAt = this.emitChatStreamStarted('submit-tool-result');
|
|
82
|
+
try {
|
|
83
|
+
const result = await this.harness.submitToolResult(this.agentId, this.threadId, toolResult);
|
|
84
|
+
return {
|
|
85
|
+
textStream: result.textStream,
|
|
86
|
+
eventStream: this.wrapEventStream(result.eventStream, startedAt),
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
catch (err) {
|
|
90
|
+
this.notifyPreStreamError(err, startedAt);
|
|
91
|
+
throw err;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* @requirements
|
|
96
|
+
* - MUST yield each event from the provided `stream`.
|
|
97
|
+
* - MUST emit the event to the internal `chatEventBus`.
|
|
98
|
+
* - MUST always yield a `FinishEvent` with `finishReason: 'error'` after any error path
|
|
99
|
+
* (in-stream `ErrorEvent` or thrown exception) if no `FinishEvent` was already emitted.
|
|
100
|
+
* - MUST emit `chat-stream-completed` telemetry on natural completion (no error path).
|
|
101
|
+
* - MUST emit `chat-stream-error` telemetry exactly once when an error occurred (in-stream
|
|
102
|
+
* `ErrorEvent` or thrown exception). Mutually exclusive with `chat-stream-completed`.
|
|
103
|
+
* - MUST derive `tool-execution-started`, `tool-execution-completed`, and
|
|
104
|
+
* `tool-approval-requested` telemetry from the corresponding `ChatEvent` types so harness
|
|
105
|
+
* implementations don't reimplement this. `tool-execution-completed.durationMs` is measured
|
|
106
|
+
* between the matching `tool-call` and `tool-result`; an unmatched `tool-result` (e.g. from
|
|
107
|
+
* a resume flow that crosses a stream boundary) is skipped.
|
|
108
|
+
*
|
|
109
|
+
* `chat-stream-started` is emitted by the entry-point method (chat / submitToolResult /
|
|
110
|
+
* approveToolCall / declineToolCall) before the harness call so that pre-stream rejections
|
|
111
|
+
* still produce a started+error pair. `startedAt` is captured there and threaded down so
|
|
112
|
+
* `durationMs` measures real elapsed time on both terminal events.
|
|
113
|
+
*/
|
|
114
|
+
async *wrapEventStream(stream, startedAt) {
|
|
115
|
+
const toolStartMs = new Map();
|
|
116
|
+
let sawFinish = false;
|
|
117
|
+
let lastError;
|
|
118
|
+
let finishUsage;
|
|
119
|
+
try {
|
|
120
|
+
for await (const event of stream) {
|
|
121
|
+
this.chatEventBus.emit(event);
|
|
122
|
+
this.deriveToolTelemetry(event, toolStartMs);
|
|
123
|
+
yield event;
|
|
124
|
+
if (event.type === 'finish') {
|
|
125
|
+
sawFinish = true;
|
|
126
|
+
finishUsage = event.usage;
|
|
127
|
+
}
|
|
128
|
+
if (event.type === 'error')
|
|
129
|
+
lastError = event.error;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
catch (err) {
|
|
133
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
134
|
+
const errorEvent = { type: 'error', error };
|
|
135
|
+
this.chatEventBus.emit(errorEvent);
|
|
136
|
+
yield errorEvent;
|
|
137
|
+
lastError = error;
|
|
138
|
+
}
|
|
139
|
+
if (!sawFinish && lastError !== undefined) {
|
|
140
|
+
const finishEvent = { type: 'finish', finishReason: 'error' };
|
|
141
|
+
this.chatEventBus.emit(finishEvent);
|
|
142
|
+
yield finishEvent;
|
|
143
|
+
}
|
|
144
|
+
const finishedAt = this.clock.now();
|
|
145
|
+
const durationMs = finishedAt.getTime() - startedAt.getTime();
|
|
146
|
+
if (lastError !== undefined) {
|
|
147
|
+
this.telemetryBus.emit({
|
|
148
|
+
type: 'chat-stream-error',
|
|
149
|
+
timestamp: finishedAt,
|
|
150
|
+
agentId: this.agentId,
|
|
151
|
+
threadId: this.threadId,
|
|
152
|
+
durationMs,
|
|
153
|
+
error: lastError,
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
this.telemetryBus.emit({
|
|
158
|
+
type: 'chat-stream-completed',
|
|
159
|
+
timestamp: finishedAt,
|
|
160
|
+
agentId: this.agentId,
|
|
161
|
+
threadId: this.threadId,
|
|
162
|
+
durationMs,
|
|
163
|
+
usage: finishUsage,
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* @requirements
|
|
169
|
+
* - MUST delegate to `this.harness.approveToolCall()`, passing `this.agentId`, `this.threadId`, and `toolCallId`.
|
|
170
|
+
* - MUST wrap the returned `eventStream` using `this.wrapEventStream()` so that listeners are notified.
|
|
171
|
+
* - MUST return an object containing the original `textStream` and the wrapped `eventStream`.
|
|
172
|
+
* - MUST emit `tool-approval-resolved` telemetry with `approved: true` only after the harness
|
|
173
|
+
* call resolves. Pre-stream rejections produce `chat-stream-error` via `notifyPreStreamError`
|
|
174
|
+
* and intentionally skip approval-resolved emission — consumers correlate the prior
|
|
175
|
+
* `tool-approval-requested` by `toolCallId` and observe the failure on the chat-stream contract.
|
|
176
|
+
* - MUST notify listeners with `ErrorEvent` + `FinishEvent` and re-throw if the harness throws
|
|
177
|
+
* before returning a stream result.
|
|
178
|
+
* - The `options.remember` flag is consumer-only metadata — the harness does not use it.
|
|
179
|
+
*/
|
|
180
|
+
async approveToolCall(toolCallId, _options) {
|
|
181
|
+
this.assertNotDisposed();
|
|
182
|
+
const startedAt = this.emitChatStreamStarted('approve-tool-call');
|
|
183
|
+
try {
|
|
184
|
+
const result = await this.harness.approveToolCall(this.agentId, this.threadId, toolCallId);
|
|
185
|
+
this.emitToolApprovalResolved(toolCallId, true);
|
|
186
|
+
return {
|
|
187
|
+
textStream: result.textStream,
|
|
188
|
+
eventStream: this.wrapEventStream(result.eventStream, startedAt),
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
catch (err) {
|
|
192
|
+
this.notifyPreStreamError(err, startedAt);
|
|
193
|
+
throw err;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* @requirements
|
|
198
|
+
* - MUST delegate to `this.harness.declineToolCall()`, passing `this.agentId`, `this.threadId`, and `toolCallId`.
|
|
199
|
+
* - MUST wrap the returned `eventStream` using `this.wrapEventStream()` so that listeners are notified.
|
|
200
|
+
* - MUST return an object containing the original `textStream` and the wrapped `eventStream`.
|
|
201
|
+
* - MUST emit `tool-approval-resolved` telemetry with `approved: false` only after the harness
|
|
202
|
+
* call resolves. Pre-stream rejections produce `chat-stream-error` via `notifyPreStreamError`
|
|
203
|
+
* and intentionally skip approval-resolved emission.
|
|
204
|
+
* - MUST notify listeners with `ErrorEvent` + `FinishEvent` and re-throw if the harness throws
|
|
205
|
+
* before returning a stream result.
|
|
206
|
+
*/
|
|
207
|
+
async declineToolCall(toolCallId) {
|
|
208
|
+
this.assertNotDisposed();
|
|
209
|
+
const startedAt = this.emitChatStreamStarted('decline-tool-call');
|
|
210
|
+
try {
|
|
211
|
+
const result = await this.harness.declineToolCall(this.agentId, this.threadId, toolCallId);
|
|
212
|
+
this.emitToolApprovalResolved(toolCallId, false);
|
|
213
|
+
return {
|
|
214
|
+
textStream: result.textStream,
|
|
215
|
+
eventStream: this.wrapEventStream(result.eventStream, startedAt),
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
catch (err) {
|
|
219
|
+
this.notifyPreStreamError(err, startedAt);
|
|
220
|
+
throw err;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* @requirements
|
|
225
|
+
* - MUST delegate to `this.harness.getMessages()`, passing `this.agentId` and `this.threadId`.
|
|
226
|
+
* - MUST return the result directly.
|
|
227
|
+
*/
|
|
228
|
+
async getMessageHistory() {
|
|
229
|
+
this.assertNotDisposed();
|
|
230
|
+
return this.harness.getMessages(this.agentId, this.threadId);
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* @requirements
|
|
234
|
+
* - MUST delegate to `this.harness.clearMessages()`, passing `this.agentId` and `this.threadId`.
|
|
235
|
+
*/
|
|
236
|
+
async clearHistory() {
|
|
237
|
+
this.assertNotDisposed();
|
|
238
|
+
await this.harness.clearMessages(this.agentId, this.threadId);
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* @requirements
|
|
242
|
+
* - IF `message` is a `string`, it MUST be formatted into a standard `Message` object array containing exactly one message.
|
|
243
|
+
* - The formatted message MUST have `role: 'user'`.
|
|
244
|
+
* - The formatted message MUST have a newly generated `id` from the injected `idGenerator`.
|
|
245
|
+
* - The formatted message MUST have a `createdAt` timestamp from the injected `clock`.
|
|
246
|
+
* - IF `message` is already an array of `Message` objects, it MUST be used directly.
|
|
247
|
+
* - MUST delegate the final array of messages to `this.harness.addContext()`, passing `this.agentId` and `this.threadId`.
|
|
248
|
+
*/
|
|
249
|
+
async addContext(message) {
|
|
250
|
+
this.assertNotDisposed();
|
|
251
|
+
const messages = typeof message === 'string'
|
|
252
|
+
? [
|
|
253
|
+
{
|
|
254
|
+
id: this.idGenerator.getUniqueId(),
|
|
255
|
+
role: 'user',
|
|
256
|
+
content: message,
|
|
257
|
+
createdAt: this.clock.now(),
|
|
258
|
+
},
|
|
259
|
+
]
|
|
260
|
+
: message;
|
|
261
|
+
await this.harness.addContext(this.agentId, this.threadId, messages);
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* @requirements
|
|
265
|
+
* - MUST register the provided `callback` on the internal `chatEventBus`.
|
|
266
|
+
* - MUST return an `Unsubscribe` closure that removes the callback when called.
|
|
267
|
+
*/
|
|
268
|
+
subscribe(callback) {
|
|
269
|
+
this.assertNotDisposed();
|
|
270
|
+
return this.chatEventBus.on(callback);
|
|
271
|
+
}
|
|
272
|
+
onTelemetry(callback) {
|
|
273
|
+
this.assertNotDisposed();
|
|
274
|
+
return this.telemetryBus.on(callback);
|
|
275
|
+
}
|
|
276
|
+
onLog(callback) {
|
|
277
|
+
this.assertNotDisposed();
|
|
278
|
+
return this.logBus.on(callback);
|
|
279
|
+
}
|
|
280
|
+
dispose() {
|
|
281
|
+
if (this.disposed) {
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
this.telemetryBus.emit({
|
|
285
|
+
type: 'session-destroyed',
|
|
286
|
+
timestamp: this.clock.now(),
|
|
287
|
+
agentId: this.agentId,
|
|
288
|
+
threadId: this.threadId,
|
|
289
|
+
});
|
|
290
|
+
for (const unsub of this.inboundUnsubs)
|
|
291
|
+
unsub();
|
|
292
|
+
for (const unsub of this.parentUnsubs)
|
|
293
|
+
unsub();
|
|
294
|
+
this.chatEventBus.dispose();
|
|
295
|
+
this.telemetryBus.dispose();
|
|
296
|
+
this.logBus.dispose();
|
|
297
|
+
this.disposed = true;
|
|
298
|
+
}
|
|
299
|
+
emitToolApprovalResolved(toolCallId, approved) {
|
|
300
|
+
this.telemetryBus.emit({
|
|
301
|
+
type: 'tool-approval-resolved',
|
|
302
|
+
timestamp: this.clock.now(),
|
|
303
|
+
agentId: this.agentId,
|
|
304
|
+
threadId: this.threadId,
|
|
305
|
+
toolCallId,
|
|
306
|
+
approved,
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Derives `tool-execution-*` and `tool-approval-requested` telemetry from `ChatEvent`s as
|
|
311
|
+
* they pass through the stream wrapper. Centralizing the derivation here keeps every harness
|
|
312
|
+
* implementation free of telemetry plumbing — they just yield the right `ChatEvent` shapes.
|
|
313
|
+
*
|
|
314
|
+
* `tool-execution-completed.durationMs` is measured between the matching `tool-call` and
|
|
315
|
+
* `tool-result` using `this.clock`. An unmatched `tool-result` (e.g. a resume flow that
|
|
316
|
+
* crosses a stream boundary so the original `tool-call` was on a previous stream) is skipped
|
|
317
|
+
* — the consumer correlates by `toolCallId` against the earlier `tool-execution-started`.
|
|
318
|
+
*/
|
|
319
|
+
deriveToolTelemetry(event, toolStartMs) {
|
|
320
|
+
if (event.type === 'tool-call') {
|
|
321
|
+
toolStartMs.set(event.toolCallId, this.clock.now().getTime());
|
|
322
|
+
this.telemetryBus.emit({
|
|
323
|
+
type: 'tool-execution-started',
|
|
324
|
+
timestamp: this.clock.now(),
|
|
325
|
+
agentId: this.agentId,
|
|
326
|
+
threadId: this.threadId,
|
|
327
|
+
toolCallId: event.toolCallId,
|
|
328
|
+
toolName: event.toolName,
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
else if (event.type === 'tool-result') {
|
|
332
|
+
const start = toolStartMs.get(event.toolCallId);
|
|
333
|
+
if (start === undefined)
|
|
334
|
+
return;
|
|
335
|
+
toolStartMs.delete(event.toolCallId);
|
|
336
|
+
this.telemetryBus.emit({
|
|
337
|
+
type: 'tool-execution-completed',
|
|
338
|
+
timestamp: this.clock.now(),
|
|
339
|
+
agentId: this.agentId,
|
|
340
|
+
threadId: this.threadId,
|
|
341
|
+
toolCallId: event.toolCallId,
|
|
342
|
+
toolName: event.toolName,
|
|
343
|
+
durationMs: this.clock.now().getTime() - start,
|
|
344
|
+
isError: event.isError === true,
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
else if (event.type === 'tool-approval-request') {
|
|
348
|
+
this.telemetryBus.emit({
|
|
349
|
+
type: 'tool-approval-requested',
|
|
350
|
+
timestamp: this.clock.now(),
|
|
351
|
+
agentId: this.agentId,
|
|
352
|
+
threadId: this.threadId,
|
|
353
|
+
toolCallId: event.toolCall.toolCallId,
|
|
354
|
+
toolName: event.toolCall.toolName,
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* Emits a `chat-stream-started` telemetry event and returns the `startedAt` timestamp the
|
|
360
|
+
* caller threads through to the stream wrapper / pre-stream error notifier so terminal
|
|
361
|
+
* events report real elapsed time.
|
|
362
|
+
*
|
|
363
|
+
* Hoisting this above the harness call keeps the `chat-stream-*` contract honest under
|
|
364
|
+
* pre-stream failures: every `chat-stream-error` follows a `chat-stream-started` for the
|
|
365
|
+
* same `(agentId, threadId)` pair.
|
|
366
|
+
*/
|
|
367
|
+
emitChatStreamStarted(trigger) {
|
|
368
|
+
const startedAt = this.clock.now();
|
|
369
|
+
this.telemetryBus.emit({
|
|
370
|
+
type: 'chat-stream-started',
|
|
371
|
+
timestamp: startedAt,
|
|
372
|
+
agentId: this.agentId,
|
|
373
|
+
threadId: this.threadId,
|
|
374
|
+
trigger,
|
|
375
|
+
});
|
|
376
|
+
return startedAt;
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* Emits an `ErrorEvent` + `FinishEvent` to all `subscribe()` listeners and a `chat-stream-error`
|
|
380
|
+
* telemetry event for a failure that occurred before a stream was established. The caller is
|
|
381
|
+
* responsible for re-throwing the original error after calling this — the method itself does
|
|
382
|
+
* not throw.
|
|
383
|
+
*
|
|
384
|
+
* `startedAt` is the timestamp captured by {@link emitChatStreamStarted} so `durationMs`
|
|
385
|
+
* measures real elapsed time even for pre-stream rejections.
|
|
386
|
+
*/
|
|
387
|
+
notifyPreStreamError(err, startedAt) {
|
|
388
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
389
|
+
this.chatEventBus.emit({ type: 'error', error });
|
|
390
|
+
this.chatEventBus.emit({ type: 'finish', finishReason: 'error' });
|
|
391
|
+
const finishedAt = this.clock.now();
|
|
392
|
+
this.telemetryBus.emit({
|
|
393
|
+
type: 'chat-stream-error',
|
|
394
|
+
timestamp: finishedAt,
|
|
395
|
+
agentId: this.agentId,
|
|
396
|
+
threadId: this.threadId,
|
|
397
|
+
durationMs: finishedAt.getTime() - startedAt.getTime(),
|
|
398
|
+
error,
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
assertNotDisposed() {
|
|
402
|
+
if (this.disposed) {
|
|
403
|
+
throw new AgentSDKError('ChatSession has been disposed.', AgentSDKErrorType.DISPOSED);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
//# sourceMappingURL=chat-session.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chat-session.js","sourceRoot":"","sources":["../src/chat-session.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAEH,QAAQ,EACR,MAAM,EAEN,SAAS,EAGT,aAAa,GAChB,MAAM,4BAA4B,CAAC;AAGpC,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AA0J/D;;;;GAIG;AACH,MAAM,OAAO,kBAAkB;IACV,OAAO,CAAe;IACtB,OAAO,CAAS;IAChB,QAAQ,CAAS;IACjB,YAAY,GAAwB,IAAI,QAAQ,EAAE,CAAC;IACnD,YAAY,GAAiB,IAAI,QAAQ,EAAkB,CAAC;IAC5D,MAAM,GAAW,IAAI,MAAM,EAAE,CAAC;IAC9B,aAAa,CAAgB;IAC7B,YAAY,CAAgB;IAC5B,KAAK,CAAQ;IACb,WAAW,CAAoB;IACxC,QAAQ,GAAY,KAAK,CAAC;IAElC;;;;;;;;OAQG;IACH,YACI,OAAqB,EACrB,OAAe,EACf,QAAgB,EAChB,OAAuB,EACvB,MAA8B,EAC9B,QAAe,IAAI,SAAS,EAAE,EAC9B,cAAiC,IAAI,aAAa,EAAE;QAEpD,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI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EAAE,CAAC,CAAC;QAClE,MAAM,UAAU,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC;QACpC,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC;YACnB,IAAI,EAAE,mBAAmB;YACzB,SAAS,EAAE,UAAU;YACrB,OAAO,EAAE,IAAI,CAAC,OAAO;YACrB,QAAQ,EAAE,IAAI,CAAC,QAAQ;YACvB,UAAU,EAAE,UAAU,CAAC,OAAO,EAAE,GAAG,SAAS,CAAC,OAAO,EAAE;YACtD,KAAK;SACR,CAAC,CAAC;IACP,CAAC;IAEO,iBAAiB;QACrB,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;YAChB,MAAM,IAAI,aAAa,CAAC,gCAAgC,EAAE,iBAAiB,CAAC,QAAQ,CAAC,CAAC;QAC1F,CAAC;IACL,CAAC;CACJ"}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare const AgentSDKErrorType: {
|
|
2
|
+
readonly AGENT_NOT_FOUND: "AGENT_NOT_FOUND";
|
|
3
|
+
readonly CHAT_SESSION_NOT_FOUND: "CHAT_SESSION_NOT_FOUND";
|
|
4
|
+
readonly COMPACTION_FAILED: "COMPACTION_FAILED";
|
|
5
|
+
readonly DISPOSED: "DISPOSED";
|
|
6
|
+
readonly INCOMPATIBLE_HARNESS: "INCOMPATIBLE_HARNESS";
|
|
7
|
+
};
|
|
8
|
+
export type AgentSDKErrorType = (typeof AgentSDKErrorType)[keyof typeof AgentSDKErrorType];
|
|
9
|
+
export declare class AgentSDKError extends Error {
|
|
10
|
+
readonly type: AgentSDKErrorType;
|
|
11
|
+
constructor(message: string, type: AgentSDKErrorType, options?: ErrorOptions);
|
|
12
|
+
}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2026, Salesforce, Inc. All rights reserved.
|
|
3
|
+
* See LICENSE.txt for license terms.
|
|
4
|
+
*/
|
|
5
|
+
export const AgentSDKErrorType = {
|
|
6
|
+
AGENT_NOT_FOUND: 'AGENT_NOT_FOUND',
|
|
7
|
+
CHAT_SESSION_NOT_FOUND: 'CHAT_SESSION_NOT_FOUND',
|
|
8
|
+
COMPACTION_FAILED: 'COMPACTION_FAILED',
|
|
9
|
+
DISPOSED: 'DISPOSED',
|
|
10
|
+
INCOMPATIBLE_HARNESS: 'INCOMPATIBLE_HARNESS',
|
|
11
|
+
};
|
|
12
|
+
export class AgentSDKError extends Error {
|
|
13
|
+
type;
|
|
14
|
+
constructor(message, type, options) {
|
|
15
|
+
super(message, options);
|
|
16
|
+
this.name = 'AgentSDKError';
|
|
17
|
+
this.type = type;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,MAAM,CAAC,MAAM,iBAAiB,GAAG;IAC7B,eAAe,EAAE,iBAAiB;IAClC,sBAAsB,EAAE,wBAAwB;IAChD,iBAAiB,EAAE,mBAAmB;IACtC,QAAQ,EAAE,UAAU;IACpB,oBAAoB,EAAE,sBAAsB;CACtC,CAAC;AAIX,MAAM,OAAO,aAAc,SAAQ,KAAK;IACpB,IAAI,CAAoB;IAExC,YAAY,OAAe,EAAE,IAAuB,EAAE,OAAsB;QACxE,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACxB,IAAI,CAAC,IAAI,GAAG,eAAe,CAAC;QAC5B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACrB,CAAC;CACJ"}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import type { LogRecord, Unsubscribe } from '@salesforce/agentic-common';
|
|
2
|
+
import type { McpServerInfo } from '../mcp-config.js';
|
|
3
|
+
import type { ChatStreamResult } from '../types/events.js';
|
|
4
|
+
import type { Message } from '../types/messages.js';
|
|
5
|
+
import type { TelemetryEventCallback } from '../types/telemetry-events.js';
|
|
6
|
+
import type { ToolResultInfo } from '../types/tools.js';
|
|
7
|
+
import type { HarnessAgentConfig, StreamOptions } from './harness-config.js';
|
|
8
|
+
import type { LLMGatewayClient } from '@salesforce/llm-gateway-sdk';
|
|
9
|
+
export declare const SUPPORTED_PROTOCOL_VERSIONS: readonly [1];
|
|
10
|
+
/**
|
|
11
|
+
* Harness-agnostic interface abstracting the agentic runtime.
|
|
12
|
+
*
|
|
13
|
+
* This is the core abstraction that allows swapping agent harnesses (Mastra,
|
|
14
|
+
* Gemini, or future implementations) without changing consumer code. It manages
|
|
15
|
+
* the full lifecycle: agent creation, thread management, streaming, tool
|
|
16
|
+
* execution, approval flows, and message history.
|
|
17
|
+
*
|
|
18
|
+
* This interface is our own design — no AI SDK or ACP equivalent exists at this
|
|
19
|
+
* framework layer. AI SDK operates at the model-provider level; ACP operates at
|
|
20
|
+
* the communication-protocol level. `AgentHarness` operates at the agentic-
|
|
21
|
+
* framework level that sits between them.
|
|
22
|
+
*/
|
|
23
|
+
export interface AgentHarness {
|
|
24
|
+
/** Unique identifier for this harness type (e.g., `'mastra'`). */
|
|
25
|
+
readonly harnessId: string;
|
|
26
|
+
/** Version of the SDK-to-harness protocol implemented by this harness. */
|
|
27
|
+
readonly protocolVersion: number;
|
|
28
|
+
/**
|
|
29
|
+
* Shut down the harness gracefully, releasing all resources.
|
|
30
|
+
* Disconnects MCP servers, closes storage connections, and
|
|
31
|
+
* deregisters all agents.
|
|
32
|
+
*/
|
|
33
|
+
shutdown(): Promise<void>;
|
|
34
|
+
/**
|
|
35
|
+
* Subscribe to telemetry events emitted by the harness. Returns an unsubscribe function.
|
|
36
|
+
*
|
|
37
|
+
* Events carry `agentId` and (where applicable) `threadId` so the SDK can route them to the correct
|
|
38
|
+
* `Agent` / `ChatSession` scope. Implementors should not expose these buses directly to consumers —
|
|
39
|
+
* consumers subscribe at the SDK layer (`AgentManager` / `Agent` / `ChatSession`).
|
|
40
|
+
*/
|
|
41
|
+
onTelemetry(callback: TelemetryEventCallback): Unsubscribe;
|
|
42
|
+
/**
|
|
43
|
+
* Subscribe to structured log records emitted by the harness. Returns an unsubscribe function.
|
|
44
|
+
*
|
|
45
|
+
* Records SHOULD carry `agentId` / `threadId` in their `context` when emitted from an agent- or
|
|
46
|
+
* thread-scoped code path, so the SDK can route them to the correct subscriber scope.
|
|
47
|
+
*/
|
|
48
|
+
onLog(callback: (record: LogRecord) => void): Unsubscribe;
|
|
49
|
+
/**
|
|
50
|
+
* Create and register a new agent with the given configuration.
|
|
51
|
+
*
|
|
52
|
+
* ### MCP Auth Injection
|
|
53
|
+
*
|
|
54
|
+
* When `config.orgJwt` is present and the agent has remote MCP servers, implementers
|
|
55
|
+
* MUST call {@link resolveMcpServerHeaders} for each remote MCP server request. The utility
|
|
56
|
+
* detects Salesforce Hosted MCP Server URLs, checks for user-provided Authorization
|
|
57
|
+
* headers (opt-out), and injects a fresh Bearer token — returning the final resolved
|
|
58
|
+
* headers to use for the request.
|
|
59
|
+
*
|
|
60
|
+
* @param agentId - ID to register the agent under.
|
|
61
|
+
* @param projectRoot - Project folder the agent is allowed to manipulate files from.
|
|
62
|
+
* @param llmGatewayClient - Pre-configured LLM gateway client for this agent.
|
|
63
|
+
* @param config - Engine-facing agent configuration (org resolution omitted).
|
|
64
|
+
* @param options - Optional execution options, including abort signals.
|
|
65
|
+
*/
|
|
66
|
+
createAgent(agentId: string, projectRoot: string, llmGatewayClient: LLMGatewayClient, config?: HarnessAgentConfig, options?: {
|
|
67
|
+
abortSignal?: AbortSignal;
|
|
68
|
+
}): Promise<void>;
|
|
69
|
+
/**
|
|
70
|
+
* Destroy an agent and release its resources (MCP connections, workspace, memory).
|
|
71
|
+
* @param agentId - ID of the agent to destroy.
|
|
72
|
+
*/
|
|
73
|
+
destroyAgent(agentId: string): Promise<boolean>;
|
|
74
|
+
/**
|
|
75
|
+
* List the IDs of all currently registered agents.
|
|
76
|
+
*/
|
|
77
|
+
getAgentIds(): Promise<string[]>;
|
|
78
|
+
/**
|
|
79
|
+
* Returns runtime information about MCP servers attached to the given agent,
|
|
80
|
+
* including connection status and discovered tool names. This is a synchronous
|
|
81
|
+
* snapshot — status is updated asynchronously by background discovery promises.
|
|
82
|
+
*
|
|
83
|
+
* @param agentId - ID of the agent whose MCP servers to inspect.
|
|
84
|
+
* @returns Info for each configured MCP server (empty array if none configured).
|
|
85
|
+
*/
|
|
86
|
+
getMcpServerInfo(agentId: string): McpServerInfo[];
|
|
87
|
+
/**
|
|
88
|
+
* Create a new conversation thread for an agent.
|
|
89
|
+
* @param agentId - ID of the owning agent.
|
|
90
|
+
* @param threadId - Optional thread ID. If omitted, the harness generates one.
|
|
91
|
+
* @returns The ID of the created thread.
|
|
92
|
+
*/
|
|
93
|
+
createThread(agentId: string, threadId?: string): Promise<string>;
|
|
94
|
+
/**
|
|
95
|
+
* Destroy a thread and its message history.
|
|
96
|
+
* @param agentId - ID of the owning agent.
|
|
97
|
+
* @param threadId - ID of the thread to destroy.
|
|
98
|
+
*/
|
|
99
|
+
destroyThread(agentId: string, threadId: string): Promise<void>;
|
|
100
|
+
/**
|
|
101
|
+
* List all thread IDs for a given agent.
|
|
102
|
+
* @param agentId - ID of the agent whose threads to list.
|
|
103
|
+
*/
|
|
104
|
+
getThreadIds(agentId: string): Promise<string[]>;
|
|
105
|
+
/**
|
|
106
|
+
* Clone an existing thread, creating a new thread with copied message history.
|
|
107
|
+
* Used to implement conversation forking.
|
|
108
|
+
* @param agentId - ID of the owning agent.
|
|
109
|
+
* @param sourceThreadId - ID of the thread to clone.
|
|
110
|
+
* @param targetThreadId - Optional ID for the new thread.
|
|
111
|
+
* @returns The ID of the cloned thread.
|
|
112
|
+
*/
|
|
113
|
+
cloneThread(agentId: string, sourceThreadId: string, targetThreadId?: string): Promise<string>;
|
|
114
|
+
/**
|
|
115
|
+
* Compacts a thread's message history to reduce context window usage.
|
|
116
|
+
* Starts a new conversation thread seeded with an LLM-generated summary of the current session.
|
|
117
|
+
*
|
|
118
|
+
* Implementations MUST destroy the source thread on success and leave it
|
|
119
|
+
* intact on failure. Failures MUST surface as `AgentSDKError` with type
|
|
120
|
+
* `COMPACTION_FAILED`.
|
|
121
|
+
*
|
|
122
|
+
* @param agentId - ID of the agent.
|
|
123
|
+
* @param threadId - ID of the conversation thread to compact.
|
|
124
|
+
* @returns The ID of the new compacted thread.
|
|
125
|
+
*/
|
|
126
|
+
compactThread(agentId: string, threadId: string): Promise<string>;
|
|
127
|
+
/**
|
|
128
|
+
* Stream a response from the agent for the given message.
|
|
129
|
+
* Returns a {@link ChatStreamResult} providing multiple ways to consume
|
|
130
|
+
* the stream (text-only or full events).
|
|
131
|
+
*
|
|
132
|
+
* @param agentId - ID of the agent to invoke.
|
|
133
|
+
* @param threadId - ID of the conversation thread.
|
|
134
|
+
* @param message - User message as a plain string.
|
|
135
|
+
* @param options - Per-call streaming options.
|
|
136
|
+
*/
|
|
137
|
+
stream(agentId: string, threadId: string, message: string, options?: StreamOptions): Promise<ChatStreamResult>;
|
|
138
|
+
/**
|
|
139
|
+
* Feed the result of a client-side tool execution back into the conversation
|
|
140
|
+
* and resume stream generation.
|
|
141
|
+
*
|
|
142
|
+
* Resumes the suspended agentic loop from the most recent `stream()` call on
|
|
143
|
+
* the given thread. The harness feeds `toolResult` into the loop and returns
|
|
144
|
+
* a continuation stream.
|
|
145
|
+
*
|
|
146
|
+
* @param agentId - ID of the agent.
|
|
147
|
+
* @param threadId - ID of the conversation thread.
|
|
148
|
+
* @param toolResult - The completed tool execution result.
|
|
149
|
+
*/
|
|
150
|
+
submitToolResult(agentId: string, threadId: string, toolResult: ToolResultInfo): Promise<ChatStreamResult>;
|
|
151
|
+
/**
|
|
152
|
+
* Approve a pending tool call, allowing the harness to execute it.
|
|
153
|
+
* Called after receiving a `tool-approval-request` event.
|
|
154
|
+
*
|
|
155
|
+
* Returns a `ChatStreamResult` containing the continuation stream — the harness
|
|
156
|
+
* executes the approved tool, generates the model's follow-up response, and
|
|
157
|
+
* streams both the text and events back to the caller.
|
|
158
|
+
*
|
|
159
|
+
* @param agentId - ID of the agent.
|
|
160
|
+
* @param threadId - ID of the conversation thread (needed for harness-internal state lookup).
|
|
161
|
+
* @param toolCallId - ID of the tool call to approve.
|
|
162
|
+
*/
|
|
163
|
+
approveToolCall(agentId: string, threadId: string, toolCallId: string): Promise<ChatStreamResult>;
|
|
164
|
+
/**
|
|
165
|
+
* Decline a pending tool call. The harness resumes the stream with the
|
|
166
|
+
* model acknowledging the decline and potentially suggesting alternatives.
|
|
167
|
+
*
|
|
168
|
+
* Returns a `ChatStreamResult` containing the continuation stream — the harness
|
|
169
|
+
* cancels the pending tool call, generates the model's acknowledgement response,
|
|
170
|
+
* and streams both the text and events back to the caller.
|
|
171
|
+
*
|
|
172
|
+
* @param agentId - ID of the agent.
|
|
173
|
+
* @param threadId - ID of the conversation thread.
|
|
174
|
+
* @param toolCallId - ID of the tool call to decline.
|
|
175
|
+
*/
|
|
176
|
+
declineToolCall(agentId: string, threadId: string, toolCallId: string): Promise<ChatStreamResult>;
|
|
177
|
+
/**
|
|
178
|
+
* Retrieve message history for a thread.
|
|
179
|
+
*
|
|
180
|
+
* @param agentId - ID of the agent.
|
|
181
|
+
* @param threadId - ID of the conversation thread.
|
|
182
|
+
* @returns All messages in chronological order (ascending by creation time).
|
|
183
|
+
*/
|
|
184
|
+
getMessages(agentId: string, threadId: string): Promise<Message[]>;
|
|
185
|
+
/**
|
|
186
|
+
* Delete all messages in a thread (clear history).
|
|
187
|
+
* @param agentId - ID of the agent.
|
|
188
|
+
* @param threadId - ID of the thread to clear.
|
|
189
|
+
*/
|
|
190
|
+
clearMessages(agentId: string, threadId: string): Promise<void>;
|
|
191
|
+
/**
|
|
192
|
+
* Save context messages to a thread without triggering an LLM response.
|
|
193
|
+
* Used for injecting system context, file contents, or prior conversation state.
|
|
194
|
+
*
|
|
195
|
+
* @param agentId - ID of the agent.
|
|
196
|
+
* @param threadId - ID of the conversation thread.
|
|
197
|
+
* @param messages - Messages to add to the thread history.
|
|
198
|
+
*/
|
|
199
|
+
addContext(agentId: string, threadId: string, messages: Message[]): Promise<void>;
|
|
200
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent-harness.js","sourceRoot":"","sources":["../../src/harness/agent-harness.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAWH,MAAM,CAAC,MAAM,2BAA2B,GAAG,CAAC,CAAC,CAAU,CAAC"}
|