fix(agent): resolve question call id replies
This commit is contained in:
@@ -36,10 +36,33 @@ type RegisterInteractionRoutesOptions = {
|
||||
sessionUiStateStore: SessionUiStateStore;
|
||||
};
|
||||
|
||||
type ResolvedPendingQuestion = {
|
||||
requestId: string;
|
||||
question: NonNullable<ReturnType<ActiveRun["pendingQuestions"]["get"]>>;
|
||||
};
|
||||
|
||||
const toSessionUiStateContext = (sessionId: string) => ({
|
||||
sessionId,
|
||||
});
|
||||
|
||||
const resolvePendingQuestion = (
|
||||
run: ActiveRun,
|
||||
requestId: string,
|
||||
): ResolvedPendingQuestion | null => {
|
||||
const exactQuestion = run.pendingQuestions.get(requestId);
|
||||
if (exactQuestion) {
|
||||
return { requestId, question: exactQuestion };
|
||||
}
|
||||
|
||||
for (const [pendingRequestId, question] of run.pendingQuestions.entries()) {
|
||||
if (question.tool?.callID === requestId) {
|
||||
return { requestId: pendingRequestId, question };
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
export const registerChatInteractionRoutes = (
|
||||
chatRouter: Router,
|
||||
{
|
||||
@@ -210,7 +233,7 @@ export const registerChatInteractionRoutes = (
|
||||
return;
|
||||
}
|
||||
|
||||
const pendingQuestion = run.pendingQuestions.get(requestId);
|
||||
const pendingQuestion = resolvePendingQuestion(run, requestId);
|
||||
if (!pendingQuestion) {
|
||||
res.status(404).json({ message: "question request not found" });
|
||||
return;
|
||||
@@ -228,14 +251,14 @@ export const registerChatInteractionRoutes = (
|
||||
|
||||
try {
|
||||
await runtime.replyQuestion({
|
||||
requestId,
|
||||
requestId: pendingQuestion.requestId,
|
||||
sessionId: sessionRecord.sessionId,
|
||||
answers: parsed.data.answers,
|
||||
});
|
||||
} catch (error) {
|
||||
run.messages = updateLastAssistantQuestion(
|
||||
run.messages,
|
||||
requestId,
|
||||
pendingQuestion.requestId,
|
||||
(question) => ({
|
||||
...question,
|
||||
status: "error",
|
||||
@@ -258,10 +281,10 @@ export const registerChatInteractionRoutes = (
|
||||
return;
|
||||
}
|
||||
|
||||
run.pendingQuestions.delete(requestId);
|
||||
run.pendingQuestions.delete(pendingQuestion.requestId);
|
||||
run.messages = updateLastAssistantQuestion(
|
||||
run.messages,
|
||||
requestId,
|
||||
pendingQuestion.requestId,
|
||||
(question) => ({
|
||||
...question,
|
||||
status: "answered",
|
||||
@@ -278,8 +301,8 @@ export const registerChatInteractionRoutes = (
|
||||
});
|
||||
for (const subscriber of run.subscribers) {
|
||||
subscriber.write("question_response", {
|
||||
session_id: pendingQuestion.session_id,
|
||||
request_id: requestId,
|
||||
session_id: pendingQuestion.question.session_id,
|
||||
request_id: pendingQuestion.requestId,
|
||||
answers: parsed.data.answers,
|
||||
});
|
||||
}
|
||||
@@ -292,8 +315,8 @@ export const registerChatInteractionRoutes = (
|
||||
}
|
||||
|
||||
res.status(202).json({
|
||||
session_id: pendingQuestion.session_id,
|
||||
request_id: requestId,
|
||||
session_id: pendingQuestion.question.session_id,
|
||||
request_id: pendingQuestion.requestId,
|
||||
answers: parsed.data.answers,
|
||||
});
|
||||
} catch (error) {
|
||||
@@ -342,7 +365,7 @@ export const registerChatInteractionRoutes = (
|
||||
return;
|
||||
}
|
||||
|
||||
const pendingQuestion = run.pendingQuestions.get(requestId);
|
||||
const pendingQuestion = resolvePendingQuestion(run, requestId);
|
||||
if (!pendingQuestion) {
|
||||
res.status(404).json({ message: "question request not found" });
|
||||
return;
|
||||
@@ -360,13 +383,13 @@ export const registerChatInteractionRoutes = (
|
||||
|
||||
try {
|
||||
await runtime.rejectQuestion({
|
||||
requestId,
|
||||
requestId: pendingQuestion.requestId,
|
||||
sessionId: sessionRecord.sessionId,
|
||||
});
|
||||
} catch (error) {
|
||||
run.messages = updateLastAssistantQuestion(
|
||||
run.messages,
|
||||
requestId,
|
||||
pendingQuestion.requestId,
|
||||
(question) => ({
|
||||
...question,
|
||||
status: "error",
|
||||
@@ -389,10 +412,10 @@ export const registerChatInteractionRoutes = (
|
||||
return;
|
||||
}
|
||||
|
||||
run.pendingQuestions.delete(requestId);
|
||||
run.pendingQuestions.delete(pendingQuestion.requestId);
|
||||
run.messages = updateLastAssistantQuestion(
|
||||
run.messages,
|
||||
requestId,
|
||||
pendingQuestion.requestId,
|
||||
(question) => ({
|
||||
...question,
|
||||
status: "rejected",
|
||||
@@ -408,8 +431,8 @@ export const registerChatInteractionRoutes = (
|
||||
});
|
||||
for (const subscriber of run.subscribers) {
|
||||
subscriber.write("question_response", {
|
||||
session_id: pendingQuestion.session_id,
|
||||
request_id: requestId,
|
||||
session_id: pendingQuestion.question.session_id,
|
||||
request_id: pendingQuestion.requestId,
|
||||
rejected: true,
|
||||
});
|
||||
}
|
||||
@@ -422,8 +445,8 @@ export const registerChatInteractionRoutes = (
|
||||
}
|
||||
|
||||
res.status(202).json({
|
||||
session_id: pendingQuestion.session_id,
|
||||
request_id: requestId,
|
||||
session_id: pendingQuestion.question.session_id,
|
||||
request_id: pendingQuestion.requestId,
|
||||
rejected: true,
|
||||
});
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user