fix: record terminal turn effects once
This commit is contained in:
@@ -1222,9 +1222,7 @@ async function resolveCodeAgentTurnStatusSnapshot(traceId, options) {
|
||||
turnRefreshSatisfiedByResultSync = synced.eventsRefreshed === true || synced.resultSynced === true || synced.terminalRefreshSkipped === true;
|
||||
if (result && !canAccessOwnedResult(result, options.actor)) return forbiddenTurnSnapshot(traceId);
|
||||
if (result && isTraceCommandTerminalStatus(result.status)) {
|
||||
await finalizeCodeAgentBillingUsage({ payload: result, params: result, options });
|
||||
recordCodeAgentConversationFact(result, options);
|
||||
await recordCodeAgentSessionOwner({ payload: result, params: result, options, status: codeAgentOwnerStatusForResult(result) });
|
||||
await recordCodeAgentTerminalTurnStatusEffects({ payload: result, params: result, options });
|
||||
}
|
||||
} catch (error) {
|
||||
resultPollError = error;
|
||||
@@ -1265,9 +1263,7 @@ async function resolveCodeAgentTurnStatusSnapshot(traceId, options) {
|
||||
agentRunResult = synced.result ?? agentRunResult;
|
||||
}
|
||||
if (isTraceCommandTerminalStatus(agentRunResult?.status)) {
|
||||
await finalizeCodeAgentBillingUsage({ payload: agentRunResult, params: agentRunResult, options });
|
||||
recordCodeAgentConversationFact(agentRunResult, options);
|
||||
await recordCodeAgentSessionOwner({ payload: agentRunResult, params: agentRunResult, options, status: codeAgentOwnerStatusForResult(agentRunResult), preserveLastTraceId: true });
|
||||
await recordCodeAgentTerminalTurnStatusEffects({ payload: agentRunResult, params: agentRunResult, options, preserveLastTraceId: true });
|
||||
}
|
||||
} catch (error) {
|
||||
refreshError = error;
|
||||
@@ -1781,6 +1777,38 @@ function recordCodeAgentConversationFact(payload = {}, options = {}) {
|
||||
}
|
||||
}
|
||||
|
||||
async function recordCodeAgentTerminalTurnStatusEffects({ payload = {}, params = {}, options = {}, preserveLastTraceId = false } = {}) {
|
||||
if (!payload || typeof payload !== "object" || !isTraceCommandTerminalStatus(payload.status)) return null;
|
||||
if (payload.turnStatusTerminalEffects?.recorded === true) return payload.turnStatusTerminalEffects;
|
||||
const billing = await finalizeCodeAgentBillingUsage({ payload, params, options });
|
||||
recordCodeAgentConversationFact(payload, options);
|
||||
const owner = await recordCodeAgentSessionOwner({ payload, params, options, status: codeAgentOwnerStatusForResult(payload), preserveLastTraceId });
|
||||
const billingSettled = codeAgentTerminalBillingSettled({ payload, params, options, billing });
|
||||
const ownerRequired = Boolean(options.actor?.id && options.accessController?.recordAgentSessionOwner);
|
||||
const ownerSettled = !ownerRequired || Boolean(owner);
|
||||
const effects = {
|
||||
recorded: billingSettled && ownerSettled,
|
||||
billingSettled,
|
||||
ownerSettled,
|
||||
preserveLastTraceId: Boolean(preserveLastTraceId),
|
||||
recordedAt: new Date().toISOString(),
|
||||
valuesPrinted: false
|
||||
};
|
||||
if (effects.recorded) {
|
||||
payload.turnStatusTerminalEffects = effects;
|
||||
const traceId = safeTraceId(payload.traceId ?? params.traceId);
|
||||
if (traceId) options.codeAgentChatResults?.set?.(traceId, payload);
|
||||
}
|
||||
return effects;
|
||||
}
|
||||
|
||||
function codeAgentTerminalBillingSettled({ payload = {}, params = {}, options = {}, billing = null } = {}) {
|
||||
const reservation = params.userBillingReservation ?? payload.userBillingReservation;
|
||||
const reservationId = typeof reservation?.reservationId === "string" ? reservation.reservationId : "";
|
||||
if (!reservationId || !options.userBillingClient?.configured) return true;
|
||||
return billing?.recorded === true || billing?.released === true || payload.billing?.recorded === true || payload.billing?.released === true;
|
||||
}
|
||||
|
||||
export async function handleCodeAgentSteerHttp(request, response, options) {
|
||||
const body = await readBody(request, options.bodyLimitBytes);
|
||||
let params = {};
|
||||
|
||||
Reference in New Issue
Block a user