Skip to content
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,128 changes: 1,122 additions & 6 deletions api/server/controllers/agents/__tests__/responses.unit.spec.js

Large diffs are not rendered by default.

539 changes: 291 additions & 248 deletions api/server/controllers/agents/responses.js

Large diffs are not rendered by default.

57 changes: 56 additions & 1 deletion packages/api/src/agents/responses/__tests__/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import {
buildAggregatedResponse,
convertInputToMessages,
createAggregatorEventHandlers,
createResponseContext,
createResponseAggregator,
createResponsesEventHandlers,
buildResponsesUsage,
} from '../service';
import { createResponseTracker } from '../handlers';
import { buildResponse, createResponseTracker } from '../handlers';

describe('response usage aggregation', () => {
const context: ResponseContext = {
Expand Down Expand Up @@ -97,6 +98,60 @@ describe('response usage aggregation', () => {
const completed = writes.find((chunk) => chunk.startsWith('data: {'));
expect(JSON.parse(completed?.slice(6) ?? '{}').response.usage).toEqual(usage);
});

it('emits the persisted envelope unchanged after storage delays', () => {
const writes: string[] = [];
const res = { write: (chunk: string) => writes.push(chunk) } as unknown as ServerResponse;
const tracker = createResponseTracker();
const handlers = createResponsesEventHandlers({ res, context, tracker });
const now = jest.spyOn(Date, 'now');
try {
now.mockReturnValue(1700000000000);
handlers.completeOutput();
const response = buildResponse(context, tracker, 'completed');
now.mockReturnValue(1700000060000);
handlers.finalizeStream(undefined, response);
const completed = writes.find((chunk) => chunk.startsWith('data: {'));
expect(JSON.parse(completed?.slice(6) ?? '{}').response).toEqual(response);
expect(writes[writes.length - 1]).toBe('data: [DONE]\n\n');
} finally {
now.mockRestore();
}
});

it('reports the validated storage choice in JSON and streaming responses', () => {
const storedContext = createResponseContext(
{ model: 'agent_test', input: 'Hello', store: true },
'resp_stored',
);

expect(buildAggregatedResponse(storedContext, createResponseAggregator()).store).toBe(true);
expect(buildResponse(storedContext, createResponseTracker(), 'completed').store).toBe(true);
});

it('completes streamed output for persistence without emitting the terminal response', () => {
const writes: string[] = [];
const res = {
write: (chunk: string) => {
writes.push(chunk);
},
} as unknown as ServerResponse;
const tracker = createResponseTracker();
const { handlers, completeOutput } = createResponsesEventHandlers({ res, context, tracker });

handlers.on_message_delta.handle('on_message_delta', {
delta: { content: [{ type: 'text', text: 'Persisted first' }] },
});
completeOutput();

expect(buildResponse(context, tracker, 'completed').output).toEqual([
expect.objectContaining({
status: 'completed',
content: [expect.objectContaining({ text: 'Persisted first' })],
}),
]);
expect(writes.some((chunk) => chunk.includes('"type":"response.completed"'))).toBe(false);
});
});

describe('convertInputToMessages', () => {
Expand Down
10 changes: 7 additions & 3 deletions packages/api/src/agents/responses/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export function buildResponse(
: null,
max_output_tokens: null,
max_tool_calls: null,
store: false,
store: context.store === true,
background: false,
service_tier: 'default',
metadata: {},
Expand Down Expand Up @@ -310,10 +310,14 @@ export function emitResponseInProgress(config: StreamHandlerConfig): void {
/**
* Emit response.completed event
*/
export function emitResponseCompleted(config: StreamHandlerConfig, usage?: Usage): void {
export function emitResponseCompleted(
config: StreamHandlerConfig,
usage?: Usage,
completedResponse?: Response,
): void {
const { res, context, tracker } = config;
tracker.status = 'completed';
const response = buildResponse(context, tracker, 'completed', usage);
const response = completedResponse ?? buildResponse(context, tracker, 'completed', usage);
writeEvent(res, {
type: 'response.completed',
sequence_number: tracker.nextSequence(),
Expand Down
20 changes: 20 additions & 0 deletions packages/api/src/agents/responses/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,23 @@ export {
createAggregatorEventHandlers,
type ResponseAggregator,
} from './service';

export {
buildStoredResponseMetadata,
filterCommittedResponseMessages,
getStoredResponseSnapshot,
isCommittedStoredResponse,
isStoredResponseOutput,
persistStoredResponse,
resolveStoredResponse,
revalidateStoredResponseConversation,
selectStoredResponseHistory,
type PersistStoredResponseParams,
type StoredResponseConversationLookup,
type StoredResponseInputMessage,
type StoredResponseLookup,
type StoredResponseReference,
type StoredResponseResolution,
type StoredResponseSnapshot,
type StoredResponseWriteDependencies,
} from './persistence';
Loading
Loading