@artemiskit/adapter-trueforge 0.1.1
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/CHANGELOG.md +8 -0
- package/dist/client.d.ts +19 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8892 -0
- package/dist/mapper.d.ts +7 -0
- package/dist/mapper.d.ts.map +1 -0
- package/dist/types.d.ts +104 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +35 -0
- package/src/client.test.ts +790 -0
- package/src/client.ts +333 -0
- package/src/index.ts +7 -0
- package/src/mapper.ts +221 -0
- package/src/test-preload.ts +4 -0
- package/src/types.ts +146 -0
- package/tsconfig.json +13 -0
package/src/client.ts
ADDED
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
import type { AgentHarness, AgentTask } from '@artemiskit/core';
|
|
2
|
+
import { TrueForge } from '@truefoundry/trueforge-sdk';
|
|
3
|
+
import {
|
|
4
|
+
normalizeTrueForgeActions,
|
|
5
|
+
sanitizeTrueForgeEvents,
|
|
6
|
+
sanitizeTrueForgeText,
|
|
7
|
+
sanitizeTrueForgeValue,
|
|
8
|
+
} from './mapper';
|
|
9
|
+
import type {
|
|
10
|
+
TrueForgeAdapterConfig,
|
|
11
|
+
TrueForgeAgentOutcome,
|
|
12
|
+
TrueForgeClient,
|
|
13
|
+
TrueForgeEvent,
|
|
14
|
+
TrueForgeOutcomeCollection,
|
|
15
|
+
TrueForgeOutcomeContext,
|
|
16
|
+
TrueForgeSetupResult,
|
|
17
|
+
} from './types';
|
|
18
|
+
|
|
19
|
+
const DEFAULT_BASE_URL = 'http://localhost:8790';
|
|
20
|
+
const CANCELLATION_GRACE_MS = 100;
|
|
21
|
+
const DEFAULT_FAILURE_COLLECTION_GRACE_MS = 250;
|
|
22
|
+
const MAX_FAILURE_COLLECTION_GRACE_MS = 1_000;
|
|
23
|
+
|
|
24
|
+
function providerName(
|
|
25
|
+
config: NonNullable<TrueForgeAdapterConfig['setup']>['provider']
|
|
26
|
+
): string | undefined {
|
|
27
|
+
if (!config) return undefined;
|
|
28
|
+
return config.type === 'custom' ? config.name : config.type;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export class TrueForgeAdapter implements AgentHarness {
|
|
32
|
+
private readonly client: TrueForgeClient;
|
|
33
|
+
private readonly config: TrueForgeAdapterConfig;
|
|
34
|
+
private readonly sensitiveValues: string[];
|
|
35
|
+
|
|
36
|
+
constructor(config: TrueForgeAdapterConfig, client?: TrueForgeClient) {
|
|
37
|
+
this.config = config;
|
|
38
|
+
this.client =
|
|
39
|
+
client ??
|
|
40
|
+
(new TrueForge({
|
|
41
|
+
baseUrl: config.baseUrl ?? DEFAULT_BASE_URL,
|
|
42
|
+
...(config.token ? { token: config.token } : { auth: false }),
|
|
43
|
+
...(config.fetch ? { fetch: config.fetch } : {}),
|
|
44
|
+
maxRetries: config.maxRetries ?? 0,
|
|
45
|
+
}) as TrueForgeClient);
|
|
46
|
+
this.sensitiveValues = this.collectSensitiveValues();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Explicitly upserts configured settings. run() never invokes this method. */
|
|
50
|
+
async setup(): Promise<TrueForgeSetupResult> {
|
|
51
|
+
const setup = this.config.setup;
|
|
52
|
+
if (!setup) return {};
|
|
53
|
+
|
|
54
|
+
if (setup.provider) {
|
|
55
|
+
try {
|
|
56
|
+
await this.client.settings.modelProviders.createOrUpdate({
|
|
57
|
+
manifest: setup.provider,
|
|
58
|
+
});
|
|
59
|
+
} catch {
|
|
60
|
+
throw new Error('Failed to configure the TrueForge model provider');
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (setup.mcpServer) {
|
|
65
|
+
try {
|
|
66
|
+
await this.client.settings.mcpServers.createOrUpdate({
|
|
67
|
+
manifest: setup.mcpServer,
|
|
68
|
+
});
|
|
69
|
+
} catch {
|
|
70
|
+
throw new Error('Failed to configure the TrueForge MCP server');
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
...(setup.provider ? { providerName: providerName(setup.provider) } : {}),
|
|
76
|
+
...(setup.mcpServer ? { mcpServerName: setup.mcpServer.name } : {}),
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async run(task: AgentTask): Promise<TrueForgeAgentOutcome> {
|
|
81
|
+
const startedAt = new Date().toISOString();
|
|
82
|
+
const events: TrueForgeEvent[] = [];
|
|
83
|
+
const controller = new AbortController();
|
|
84
|
+
const timeoutMs = Math.max(
|
|
85
|
+
1,
|
|
86
|
+
Math.min(task.timeoutMs, this.config.turnTimeoutMs ?? task.timeoutMs)
|
|
87
|
+
);
|
|
88
|
+
let sessionId: string | undefined;
|
|
89
|
+
let turnId: string | undefined;
|
|
90
|
+
let terminalEvent: Extract<TrueForgeEvent, { type: 'turn.done' }> | undefined;
|
|
91
|
+
let outcomeCollection: Promise<TrueForgeOutcomeCollection> | undefined;
|
|
92
|
+
let cancellationStarted = false;
|
|
93
|
+
let timedOut = false;
|
|
94
|
+
let timeoutHandle: ReturnType<typeof setTimeout> | undefined;
|
|
95
|
+
|
|
96
|
+
const cancelSession = async () => {
|
|
97
|
+
if (!sessionId || cancellationStarted) return;
|
|
98
|
+
cancellationStarted = true;
|
|
99
|
+
await this.cancelBestEffort(sessionId);
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
const execution = async (): Promise<TrueForgeOutcomeCollection> => {
|
|
103
|
+
const prompt =
|
|
104
|
+
this.config.buildPrompt !== undefined
|
|
105
|
+
? await this.config.buildPrompt(task)
|
|
106
|
+
: this.config.prompt;
|
|
107
|
+
if (!prompt.trim()) throw new Error('TrueForge task prompt must not be empty');
|
|
108
|
+
|
|
109
|
+
const session = await this.client.sessions.create(
|
|
110
|
+
{ agent: { spec: this.config.agent } },
|
|
111
|
+
this.requestOptions(timeoutMs, controller.signal)
|
|
112
|
+
);
|
|
113
|
+
sessionId = session.data.id;
|
|
114
|
+
if (controller.signal.aborted) {
|
|
115
|
+
await cancelSession();
|
|
116
|
+
throw new Error('turn-aborted');
|
|
117
|
+
}
|
|
118
|
+
const stream = await this.client.sessions.createTurnStream(
|
|
119
|
+
sessionId,
|
|
120
|
+
{ input: [{ type: 'user.message', content: prompt }] },
|
|
121
|
+
this.requestOptions(timeoutMs, controller.signal)
|
|
122
|
+
);
|
|
123
|
+
if (controller.signal.aborted) throw new Error('turn-aborted');
|
|
124
|
+
|
|
125
|
+
for await (const event of stream) {
|
|
126
|
+
if (controller.signal.aborted) throw new Error('turn-aborted');
|
|
127
|
+
events.push(event);
|
|
128
|
+
if (event.type === 'turn.created') turnId = event.turnId;
|
|
129
|
+
if (event.type === 'turn.done') {
|
|
130
|
+
terminalEvent = event;
|
|
131
|
+
break;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (!terminalEvent) throw new Error('TrueForge turn ended without a terminal event');
|
|
136
|
+
if (terminalEvent.state.status !== 'done') {
|
|
137
|
+
throw new Error(
|
|
138
|
+
terminalEvent.state.status === 'error'
|
|
139
|
+
? terminalEvent.state.message
|
|
140
|
+
: `TrueForge turn was ${terminalEvent.state.status}`
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
if (terminalEvent.state.requiredActions.length > 0) {
|
|
144
|
+
throw new Error('TrueForge turn requires additional action before completion');
|
|
145
|
+
}
|
|
146
|
+
if (!sessionId || !turnId) {
|
|
147
|
+
throw new Error('TrueForge did not return session and turn identifiers');
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
outcomeCollection = this.invokeOutcomeCollector({
|
|
151
|
+
task,
|
|
152
|
+
sessionId,
|
|
153
|
+
turnId,
|
|
154
|
+
events,
|
|
155
|
+
terminalState: terminalEvent.state,
|
|
156
|
+
});
|
|
157
|
+
return outcomeCollection;
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
const timeout = new Promise<never>((_resolve, reject) => {
|
|
161
|
+
timeoutHandle = setTimeout(() => {
|
|
162
|
+
timedOut = true;
|
|
163
|
+
controller.abort();
|
|
164
|
+
reject(new Error('turn-timeout'));
|
|
165
|
+
}, timeoutMs);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
try {
|
|
169
|
+
const collected = await Promise.race([execution(), timeout]);
|
|
170
|
+
if (!terminalEvent || terminalEvent.state.status !== 'done' || !sessionId || !turnId) {
|
|
171
|
+
throw new Error('TrueForge execution completed without a valid terminal state');
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return this.sanitizeOutcome({
|
|
175
|
+
taskId: task.id,
|
|
176
|
+
completed: true,
|
|
177
|
+
acceptancePassed: collected.acceptancePassed,
|
|
178
|
+
...(collected.finalDiff === undefined ? {} : { finalDiff: collected.finalDiff }),
|
|
179
|
+
sessionId,
|
|
180
|
+
turnId,
|
|
181
|
+
...(terminalEvent.state.metrics ? { metrics: terminalEvent.state.metrics } : {}),
|
|
182
|
+
trace: {
|
|
183
|
+
taskId: task.id,
|
|
184
|
+
actions: normalizeTrueForgeActions(events, this.sensitiveValues),
|
|
185
|
+
changedPaths: collected.changedPaths,
|
|
186
|
+
startedAt,
|
|
187
|
+
completedAt: terminalEvent.state.completedAt,
|
|
188
|
+
},
|
|
189
|
+
evidence: { events: sanitizeTrueForgeEvents(events, this.sensitiveValues) },
|
|
190
|
+
});
|
|
191
|
+
} catch (error) {
|
|
192
|
+
if (timeoutHandle) {
|
|
193
|
+
clearTimeout(timeoutHandle);
|
|
194
|
+
timeoutHandle = undefined;
|
|
195
|
+
}
|
|
196
|
+
if (sessionId && (timedOut || !terminalEvent)) {
|
|
197
|
+
controller.abort();
|
|
198
|
+
await cancelSession();
|
|
199
|
+
}
|
|
200
|
+
const collected = await this.collectFailureEvidence(
|
|
201
|
+
{
|
|
202
|
+
task,
|
|
203
|
+
sessionId,
|
|
204
|
+
turnId,
|
|
205
|
+
events,
|
|
206
|
+
terminalState: terminalEvent?.state,
|
|
207
|
+
},
|
|
208
|
+
outcomeCollection
|
|
209
|
+
);
|
|
210
|
+
return this.failureOutcome({
|
|
211
|
+
task,
|
|
212
|
+
events,
|
|
213
|
+
startedAt,
|
|
214
|
+
sessionId,
|
|
215
|
+
turnId,
|
|
216
|
+
terminalEvent,
|
|
217
|
+
error: timedOut
|
|
218
|
+
? `TrueForge turn timed out after ${String(timeoutMs)}ms`
|
|
219
|
+
: sanitizeTrueForgeText(
|
|
220
|
+
error instanceof Error ? error.message : 'TrueForge execution failed',
|
|
221
|
+
this.sensitiveValues
|
|
222
|
+
),
|
|
223
|
+
collected,
|
|
224
|
+
});
|
|
225
|
+
} finally {
|
|
226
|
+
if (timeoutHandle) clearTimeout(timeoutHandle);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
private requestOptions(timeoutMs: number, abortSignal: AbortSignal) {
|
|
231
|
+
return {
|
|
232
|
+
abortSignal,
|
|
233
|
+
maxRetries: this.config.maxRetries ?? 0,
|
|
234
|
+
timeoutInSeconds: Math.max(1, Math.ceil(timeoutMs / 1_000)),
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
private async cancelBestEffort(sessionId: string): Promise<void> {
|
|
239
|
+
let timeoutHandle: ReturnType<typeof setTimeout> | undefined;
|
|
240
|
+
const timeout = new Promise<void>((resolve) => {
|
|
241
|
+
timeoutHandle = setTimeout(resolve, CANCELLATION_GRACE_MS);
|
|
242
|
+
});
|
|
243
|
+
const cancellation = Promise.resolve()
|
|
244
|
+
.then(() =>
|
|
245
|
+
this.client.sessions.cancel(sessionId, {}, { maxRetries: 0, timeoutInSeconds: 1 })
|
|
246
|
+
)
|
|
247
|
+
.then(() => undefined)
|
|
248
|
+
.catch(() => {
|
|
249
|
+
// The run outcome remains authoritative when cancellation cannot be confirmed.
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
try {
|
|
253
|
+
await Promise.race([cancellation, timeout]);
|
|
254
|
+
} finally {
|
|
255
|
+
if (timeoutHandle) clearTimeout(timeoutHandle);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
private collectSensitiveValues(): string[] {
|
|
260
|
+
const values = [this.config.token, this.config.setup?.provider?.auth?.apiKey];
|
|
261
|
+
const auth = this.config.setup?.mcpServer?.auth;
|
|
262
|
+
if (auth?.type === 'header') values.push(...Object.values(auth.headers));
|
|
263
|
+
return values.filter((value): value is string => typeof value === 'string' && value.length > 0);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
private invokeOutcomeCollector(context: TrueForgeOutcomeContext) {
|
|
267
|
+
return Promise.resolve().then(() =>
|
|
268
|
+
this.config.collectOutcome({
|
|
269
|
+
...context,
|
|
270
|
+
events: context.events.map((event) => structuredClone(event)),
|
|
271
|
+
})
|
|
272
|
+
);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
private async collectFailureEvidence(
|
|
276
|
+
context: TrueForgeOutcomeContext,
|
|
277
|
+
existingCollection?: Promise<TrueForgeOutcomeCollection>
|
|
278
|
+
): Promise<TrueForgeOutcomeCollection | undefined> {
|
|
279
|
+
const collection = existingCollection ?? this.invokeOutcomeCollector(context);
|
|
280
|
+
const graceMs = Math.max(
|
|
281
|
+
1,
|
|
282
|
+
Math.min(
|
|
283
|
+
this.config.failureCollectionGraceMs ?? DEFAULT_FAILURE_COLLECTION_GRACE_MS,
|
|
284
|
+
MAX_FAILURE_COLLECTION_GRACE_MS
|
|
285
|
+
)
|
|
286
|
+
);
|
|
287
|
+
let timeoutHandle: ReturnType<typeof setTimeout> | undefined;
|
|
288
|
+
const timeout = new Promise<undefined>((resolve) => {
|
|
289
|
+
timeoutHandle = setTimeout(() => resolve(undefined), graceMs);
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
try {
|
|
293
|
+
return await Promise.race([collection.catch(() => undefined), timeout]);
|
|
294
|
+
} finally {
|
|
295
|
+
if (timeoutHandle) clearTimeout(timeoutHandle);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
private sanitizeOutcome(outcome: TrueForgeAgentOutcome): TrueForgeAgentOutcome {
|
|
300
|
+
return sanitizeTrueForgeValue(outcome, this.sensitiveValues);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
private failureOutcome(input: {
|
|
304
|
+
collected?: TrueForgeOutcomeCollection;
|
|
305
|
+
error: string;
|
|
306
|
+
events: TrueForgeEvent[];
|
|
307
|
+
sessionId?: string;
|
|
308
|
+
startedAt: string;
|
|
309
|
+
task: AgentTask;
|
|
310
|
+
terminalEvent?: Extract<TrueForgeEvent, { type: 'turn.done' }>;
|
|
311
|
+
turnId?: string;
|
|
312
|
+
}): TrueForgeAgentOutcome {
|
|
313
|
+
const completedAt = input.terminalEvent?.state.completedAt ?? new Date().toISOString();
|
|
314
|
+
return this.sanitizeOutcome({
|
|
315
|
+
taskId: input.task.id,
|
|
316
|
+
completed: false,
|
|
317
|
+
acceptancePassed: input.collected?.acceptancePassed ?? false,
|
|
318
|
+
error: input.error,
|
|
319
|
+
...(input.collected?.finalDiff === undefined ? {} : { finalDiff: input.collected.finalDiff }),
|
|
320
|
+
...(input.sessionId ? { sessionId: input.sessionId } : {}),
|
|
321
|
+
...(input.turnId ? { turnId: input.turnId } : {}),
|
|
322
|
+
...(input.terminalEvent?.state.metrics ? { metrics: input.terminalEvent.state.metrics } : {}),
|
|
323
|
+
trace: {
|
|
324
|
+
taskId: input.task.id,
|
|
325
|
+
actions: normalizeTrueForgeActions(input.events, this.sensitiveValues),
|
|
326
|
+
changedPaths: input.collected?.changedPaths ?? [],
|
|
327
|
+
startedAt: input.startedAt,
|
|
328
|
+
completedAt,
|
|
329
|
+
},
|
|
330
|
+
evidence: { events: sanitizeTrueForgeEvents(input.events, this.sensitiveValues) },
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
}
|
package/src/index.ts
ADDED
package/src/mapper.ts
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { type AgentAction, createDefaultRedactor } from '@artemiskit/core';
|
|
2
|
+
import { isEventDelta, mergeEventDelta } from '@truefoundry/trueforge-sdk';
|
|
3
|
+
import type { SanitizedTrueForgeEvent, TrueForgeEvent } from './types';
|
|
4
|
+
|
|
5
|
+
const REDACTED = '[REDACTED]';
|
|
6
|
+
const CORE_REDACTOR = createDefaultRedactor();
|
|
7
|
+
const SENSITIVE_KEYS = new Set([
|
|
8
|
+
'apikey',
|
|
9
|
+
'authorization',
|
|
10
|
+
'cookie',
|
|
11
|
+
'idtoken',
|
|
12
|
+
'password',
|
|
13
|
+
'refreshtoken',
|
|
14
|
+
'secret',
|
|
15
|
+
'setcookie',
|
|
16
|
+
'token',
|
|
17
|
+
'accesstoken',
|
|
18
|
+
]);
|
|
19
|
+
const SECRET_ASSIGNMENT_PATTERN =
|
|
20
|
+
/\b(?:password|passphrase|secret|client[-_]?secret|token|auth[-_]?token|api[-_]?key|authorization|credentials?|private[-_]?key)["']?\s*[=:]\s*(?:bearer\s+)?["']?[^\s,;'"}]+/gi;
|
|
21
|
+
const JWT_PATTERN = /\beyJ[A-Za-z0-9-_]+\.eyJ[A-Za-z0-9-_]+\.[A-Za-z0-9-_.+/=]*\b/g;
|
|
22
|
+
|
|
23
|
+
function escapeRegExp(value: string): string {
|
|
24
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function sanitizeTrueForgeText(
|
|
28
|
+
text: string,
|
|
29
|
+
sensitiveValues: readonly string[] = []
|
|
30
|
+
): string {
|
|
31
|
+
try {
|
|
32
|
+
const parsed = JSON.parse(text) as unknown;
|
|
33
|
+
if (parsed !== null && typeof parsed === 'object') {
|
|
34
|
+
return JSON.stringify(sanitizeValue(parsed, sensitiveValues));
|
|
35
|
+
}
|
|
36
|
+
} catch {
|
|
37
|
+
// Non-JSON text is handled by the string redactors below.
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
let result = text;
|
|
41
|
+
for (const sensitiveValue of sensitiveValues) {
|
|
42
|
+
if (sensitiveValue.length > 0) {
|
|
43
|
+
result = result.replace(new RegExp(escapeRegExp(sensitiveValue), 'g'), REDACTED);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
result = result.replace(SECRET_ASSIGNMENT_PATTERN, REDACTED);
|
|
47
|
+
result = CORE_REDACTOR.redact(result).text;
|
|
48
|
+
result = result.replace(JWT_PATTERN, REDACTED);
|
|
49
|
+
return result;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function isSensitiveKey(key: string): boolean {
|
|
53
|
+
const normalized = key.replace(/[^a-z0-9]/gi, '').toLowerCase();
|
|
54
|
+
return (
|
|
55
|
+
SENSITIVE_KEYS.has(normalized) ||
|
|
56
|
+
/(?:apikey|authtoken|clientsecret|privatekey)/.test(normalized) ||
|
|
57
|
+
/(?:password|passphrase|secret|token|authorization|credentials?|cookie)$/.test(normalized)
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function sanitizeValue(value: unknown, sensitiveValues: readonly string[], key?: string): unknown {
|
|
62
|
+
if (key && isSensitiveKey(key)) return REDACTED;
|
|
63
|
+
if (typeof value === 'string') return sanitizeTrueForgeText(value, sensitiveValues);
|
|
64
|
+
if (Array.isArray(value)) return value.map((item) => sanitizeValue(item, sensitiveValues));
|
|
65
|
+
if (value && typeof value === 'object') {
|
|
66
|
+
return Object.fromEntries(
|
|
67
|
+
Object.entries(value).map(([entryKey, entryValue]) => [
|
|
68
|
+
entryKey,
|
|
69
|
+
sanitizeValue(entryValue, sensitiveValues, entryKey),
|
|
70
|
+
])
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
return value;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function sanitizeTrueForgeValue<T>(value: T, sensitiveValues: readonly string[] = []): T {
|
|
77
|
+
return sanitizeValue(value, sensitiveValues) as T;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function sanitizeTrueForgeEvents(
|
|
81
|
+
events: readonly TrueForgeEvent[],
|
|
82
|
+
sensitiveValues: readonly string[] = []
|
|
83
|
+
): SanitizedTrueForgeEvent[] {
|
|
84
|
+
return events.map((event) => sanitizeValue(event, sensitiveValues) as SanitizedTrueForgeEvent);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function actionType(name: string): AgentAction['type'] {
|
|
88
|
+
if (/(?:^|_)(?:run|command|exec|shell)(?:$|_)/i.test(name)) return 'command';
|
|
89
|
+
if (/(?:^|_)(?:read|write|patch|file)(?:$|_)/i.test(name)) return 'file';
|
|
90
|
+
return 'tool';
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function normalizeToolName(name: string): string {
|
|
94
|
+
const segments = name.split(/__|[/:.]/).filter(Boolean);
|
|
95
|
+
return segments.at(-1) ?? name;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function responseStatus(content: string): AgentAction['status'] {
|
|
99
|
+
try {
|
|
100
|
+
const parsed = JSON.parse(content) as unknown;
|
|
101
|
+
if (containsRejection(parsed)) return 'rejected';
|
|
102
|
+
if (containsFailure(parsed)) return 'error';
|
|
103
|
+
} catch {
|
|
104
|
+
if (/\b(?:error|denied|rejected|failed)\b/i.test(content)) return 'error';
|
|
105
|
+
}
|
|
106
|
+
return 'success';
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function containsRejection(value: unknown): boolean {
|
|
110
|
+
if (
|
|
111
|
+
typeof value === 'string' &&
|
|
112
|
+
/\bSANDBOX_(?:COMMAND|PATH|SECRET|SYMLINK)_DENIED\b/.test(value)
|
|
113
|
+
) {
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
if (Array.isArray(value)) return value.some(containsRejection);
|
|
117
|
+
if (value === null || typeof value !== 'object') return false;
|
|
118
|
+
|
|
119
|
+
for (const [key, entry] of Object.entries(value)) {
|
|
120
|
+
const normalizedKey = key.replace(/[-_]/g, '').toLowerCase();
|
|
121
|
+
if (normalizedKey === 'status' && entry === 'rejected') return true;
|
|
122
|
+
if (containsRejection(entry)) return true;
|
|
123
|
+
}
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function containsFailure(value: unknown): boolean {
|
|
128
|
+
if (Array.isArray(value)) return value.some(containsFailure);
|
|
129
|
+
if (value === null || typeof value !== 'object') return false;
|
|
130
|
+
|
|
131
|
+
for (const [key, entry] of Object.entries(value)) {
|
|
132
|
+
const normalizedKey = key.replace(/[-_]/g, '').toLowerCase();
|
|
133
|
+
if (normalizedKey === 'exitcode' && typeof entry === 'number' && entry !== 0) return true;
|
|
134
|
+
if (normalizedKey === 'iserror' && entry === true) return true;
|
|
135
|
+
if (
|
|
136
|
+
normalizedKey === 'error' &&
|
|
137
|
+
entry !== undefined &&
|
|
138
|
+
entry !== null &&
|
|
139
|
+
entry !== false &&
|
|
140
|
+
entry !== ''
|
|
141
|
+
) {
|
|
142
|
+
return true;
|
|
143
|
+
}
|
|
144
|
+
if (
|
|
145
|
+
normalizedKey === 'status' &&
|
|
146
|
+
typeof entry === 'string' &&
|
|
147
|
+
/^(?:error|failed)$/i.test(entry)
|
|
148
|
+
) {
|
|
149
|
+
return true;
|
|
150
|
+
}
|
|
151
|
+
if (containsFailure(entry)) return true;
|
|
152
|
+
}
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function timestamp(value: string | undefined): number | undefined {
|
|
157
|
+
if (!value) return undefined;
|
|
158
|
+
const result = Date.parse(value);
|
|
159
|
+
return Number.isNaN(result) ? undefined : result;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export function normalizeTrueForgeActions(
|
|
163
|
+
events: readonly TrueForgeEvent[],
|
|
164
|
+
sensitiveValues: readonly string[] = []
|
|
165
|
+
): AgentAction[] {
|
|
166
|
+
const mergedEvents = events.map((event) => structuredClone(event));
|
|
167
|
+
const messages = new Map<string, Extract<TrueForgeEvent, { type: 'model.message' }>>();
|
|
168
|
+
for (const event of mergedEvents) {
|
|
169
|
+
if (event.type === 'model.message') {
|
|
170
|
+
messages.set(event.id, event);
|
|
171
|
+
} else if (isEventDelta(event)) {
|
|
172
|
+
const message = messages.get(event.id);
|
|
173
|
+
if (message) mergeEventDelta(message, event);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const pending = new Map<
|
|
178
|
+
string,
|
|
179
|
+
{ name: string; startedAt?: number; type: AgentAction['type'] }
|
|
180
|
+
>();
|
|
181
|
+
const actions: AgentAction[] = [];
|
|
182
|
+
|
|
183
|
+
for (const event of mergedEvents) {
|
|
184
|
+
if (event.type === 'model.message') {
|
|
185
|
+
const startedAt = timestamp(event.createdAt);
|
|
186
|
+
for (const toolCall of event.toolCalls ?? []) {
|
|
187
|
+
const name = normalizeToolName(toolCall.toolInfo.name || toolCall.function.name);
|
|
188
|
+
pending.set(toolCall.id, { name, startedAt, type: actionType(name) });
|
|
189
|
+
}
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (event.type !== 'tool.response') continue;
|
|
194
|
+
const call = pending.get(event.toolCallId);
|
|
195
|
+
const completedAt = timestamp(event.createdAt);
|
|
196
|
+
const durationMs =
|
|
197
|
+
call?.startedAt !== undefined && completedAt !== undefined
|
|
198
|
+
? Math.max(0, completedAt - call.startedAt)
|
|
199
|
+
: 0;
|
|
200
|
+
actions.push({
|
|
201
|
+
type: call?.type ?? 'tool',
|
|
202
|
+
name: call?.name ?? event.toolCallId,
|
|
203
|
+
status: responseStatus(event.content),
|
|
204
|
+
durationMs,
|
|
205
|
+
summary: sanitizeTrueForgeText(event.content, sensitiveValues).slice(0, 1_000),
|
|
206
|
+
});
|
|
207
|
+
pending.delete(event.toolCallId);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
for (const call of pending.values()) {
|
|
211
|
+
actions.push({
|
|
212
|
+
type: call.type,
|
|
213
|
+
name: call.name,
|
|
214
|
+
status: 'rejected',
|
|
215
|
+
durationMs: 0,
|
|
216
|
+
summary: 'No tool response before the turn completed',
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return actions;
|
|
221
|
+
}
|