Skip to content

Commit 7cd0d8a

Browse files
committed
formatting, use after second s
1 parent 47e7eba commit 7cd0d8a

File tree

2 files changed

+89
-19
lines changed

2 files changed

+89
-19
lines changed

apps/web-v2/src/features/agent-inbox/contexts/ThreadContext.tsx

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type ThreadContentType<
3535
_response: HumanResponse[],
3636
_options?: {
3737
stream?: TStream;
38+
afterSeconds?: number;
3839
},
3940
) => TStream extends true
4041
?
@@ -406,6 +407,7 @@ function ThreadsProviderInternal<
406407
response: HumanResponse[],
407408
options?: {
408409
stream?: TStream;
410+
afterSeconds?: number;
409411
},
410412
): TStream extends true
411413
?
@@ -432,19 +434,20 @@ function ThreadsProviderInternal<
432434
const client = createClient(deploymentId, session.accessToken);
433435

434436
try {
437+
const payload = {
438+
command: {
439+
resume: response,
440+
},
441+
...(options?.afterSeconds && { afterSeconds: options.afterSeconds }),
442+
};
443+
435444
if (options?.stream) {
436445
return client.runs.stream(threadId, assistantId, {
437-
command: {
438-
resume: response,
439-
},
446+
...payload,
440447
streamMode: "events",
441448
}) as any; // Type assertion needed due to conditional return type
442449
}
443-
return client.runs.create(threadId, assistantId, {
444-
command: {
445-
resume: response,
446-
},
447-
}) as any; // Type assertion needed due to conditional return type
450+
return client.runs.create(threadId, assistantId, payload) as any; // Type assertion needed due to conditional return type
448451
} catch (e: any) {
449452
logger.error("Error sending human response", e);
450453
throw e;

apps/web-v2/src/features/agent-inbox/hooks/use-interrupted-actions.tsx

Lines changed: 78 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -396,20 +396,87 @@ export default function useInterruptedActions<
396396
toast.error("Thread data is not available");
397397
return;
398398
}
399+
if (!humanResponse) {
400+
toast.error("Please enter a response.");
401+
return;
402+
}
403+
if (!selectedInbox) {
404+
toast.error("No inbox selected");
405+
return;
406+
}
399407

400-
// For now, this is a placeholder that shows a success message
401-
// In a real implementation, this would save the scheduled date to the backend
402-
// and handle the scheduling logic
403-
const formattedDate = scheduledDate.toLocaleDateString();
404-
const formattedTime = scheduledDate.toLocaleTimeString();
408+
// Calculate afterSeconds from the scheduled date
409+
const now = new Date();
410+
const afterSeconds = Math.max(
411+
0,
412+
Math.floor((scheduledDate.getTime() - now.getTime()) / 1000),
413+
);
405414

406-
toast.success(`Thread scheduled successfully!`, {
407-
description: `This interrupt will be processed on ${formattedDate} at ${formattedTime}`,
408-
duration: 5000,
409-
});
415+
if (afterSeconds <= 0) {
416+
toast.error("Scheduled time must be in the future");
417+
return;
418+
}
410419

411-
// Clear the selected thread ID to go back to inbox view
412-
await setSelectedThreadId(null);
420+
try {
421+
// Create the human response similar to handleSubmit
422+
const humanResponseInput: HumanResponse[] = humanResponse.flatMap((r) => {
423+
if (r.type === "edit") {
424+
if (r.acceptAllowed && !r.editsMade) {
425+
return {
426+
type: "accept",
427+
args: r.args,
428+
};
429+
} else {
430+
return {
431+
type: "edit",
432+
args: r.args,
433+
};
434+
}
435+
}
436+
437+
if (r.type === "response" && !r.args) {
438+
// If response was allowed but no response was given, do not include in the response
439+
return [];
440+
}
441+
return {
442+
type: r.type,
443+
args: r.args,
444+
};
445+
});
446+
447+
const input = humanResponseInput.find(
448+
(r) => r.type === selectedSubmitType,
449+
);
450+
if (!input) {
451+
toast.error("No response found.");
452+
return;
453+
}
454+
455+
setLoading(true);
456+
457+
// Send the human response with afterSeconds for scheduling
458+
await sendHumanResponse(threadData.thread.thread_id, [input], {
459+
afterSeconds,
460+
});
461+
462+
const formattedDate = scheduledDate.toLocaleDateString();
463+
const formattedTime = scheduledDate.toLocaleTimeString();
464+
465+
toast.success(`Thread scheduled successfully!`, {
466+
description: `This interrupt will be processed on ${formattedDate} at ${formattedTime}`,
467+
duration: 5000,
468+
});
469+
470+
// Clear the selected thread ID to go back to inbox view
471+
await setSelectedThreadId(null);
472+
} catch (e: any) {
473+
logger.error("Error scheduling response", e);
474+
toast.error("Failed to schedule response.", {
475+
duration: 5000,
476+
});
477+
} finally {
478+
setLoading(false);
479+
}
413480
};
414481

415482
return {

0 commit comments

Comments
 (0)