fix(chat): fold question answers into tool activity #11014
Mergedmaria-rcksmerged 13 commits into
main fromrefs/pull/11014/headmerged by maria-rcks View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/server/src/orchestration/ActivityPayloadProjection.ts lines 382 to 389OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -354,0 +369,21 @@369+ name !== "question"370+ )371+ return {};372+ const input = asRecord(373+ data.input ?? data.rawInput ?? asRecord(data.state)?.input ?? item?.arguments,374+ );375+ const questions = input?.questions ?? asRecord(input?.params)?.questions;376+ if (!Array.isArray(questions)) return {};377+ // Clients match native tools to the canonical question; choices and answers378+ // already live on the user-input activities and need not cross the wire twice.379+ return {380+ toolName,381+ input: {382+ questions: questions.map((value) => {383+ const question = asRecord(value);384+ return {385+ question: asTrimmedString(386+ question?.question ?? question?.question_text ?? question?.prompt ?? question?.title,387+ ),388+ };389+ }),@@ -3489,16 +3521,16 @@34893521 {[34903522 ...new Set([3523+ ...Object.keys(answer.questionTextById ?? {}),34913524 ...Object.keys(answer.answers),34923525 ...Object.keys(answer.attachmentsByQuestionId),34933526 ]),34943527 ].map((questionId) => (34953528 <div key={questionId} className="space-y-1">34963529 {answer.questionTextById?.[questionId] ? (3497- <p className="text-sm text-muted-foreground">{answer.questionTextById[questionId]}</p>3530+ <p className="whitespace-pre-wrap text-sm text-muted-foreground">3531+ {answer.questionTextById[questionId]}3532+ </p>34983533 ) : null}34993534 <p className="whitespace-pre-wrap text-sm">3500- {[answer.answers[questionId]]3501- .flat()3502- .filter((value): value is string => typeof value === "string")3503- .join(", ")}3535+ {getQuestionAnswerText(answer.answers[questionId])}35043536 </p>
- @@ -0,0 +134,21 @@134+ const answers =135+ submittedAnswers.get(requestId) ?? incomingAnswers ?? record(previousPayload?.answers) ?? {};136+ const attachmentsByQuestionId = {137+ ...record(previousPayload?.attachmentsByQuestionId),138+ ...record(payload.attachmentsByQuestionId),139+ };140+ const questionAnswer = { requestId, questionTextById, answers, attachmentsByQuestionId };141+ if (!isQuestionAnswer(questionAnswer)) {142+ result.push(activity);143+ continue;144+ }145+ const hasAnswer =146+ Object.keys(answers).length > 0 || Object.keys(attachmentsByQuestionId).length > 0;147+ const userInputStatus = hasAnswer148+ ? "submitted"149+ : activity.kind === "user-input.resolved"150+ ? "dismissed"151+ : (previousPayload?.userInputStatus ?? "pending");152+ const folded: OrchestrationThreadActivity = {153+ ...(previous ?? activity),154+ kind: "user-input.answer-submitted",
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- @@ -1624,0 +1629,21 @@1629+ const foldedAnswerMessageIds = new Set(1630+ workEntries.flatMap((entry) =>1631+ entry.questionAnswer ? [`async-answer:${entry.questionAnswer.requestId}`] : [],1632+ ),1633+ );1634+ const showMessage = (message: ChatMessage) =>1635+ message.role !== "user" || !foldedAnswerMessageIds.has(message.id);1636+ const asyncAnswers = new Map<string, ChatMessage>(1637+ messages1638+ .filter(1639+ (message) =>1640+ message.role === "user" && message.turnId && foldedAnswerMessageIds.has(message.id),1641+ )1642+ .map((message) => [message.id, message]),1643+ );1644+ const latestProviderActivity = new Map<TurnId, string>();1645+ if (asyncAnswers.size > 0) {1646+ for (const entry of workEntries) {1647+ if (1648+ entry.turnId &&1649+ (entry.sourceActivityKind?.startsWith("tool.") ||
- @@ -0,0 +203,21 @@203+ result[position] = { ...activity, payload: { ...payload, answers } };204+ }205+ const folded = withoutDuplicateQuestionTools(result);206+ const latestProviderActivity = new Map<string, string>();207+ for (const activity of folded) {208+ if (209+ activity.turnId &&210+ (activity.kind.startsWith("tool.") || activity.kind.startsWith("task."))211+ ) {212+ const previous = latestProviderActivity.get(activity.turnId);213+ if (!previous || activity.createdAt > previous)214+ latestProviderActivity.set(activity.turnId, activity.createdAt);215+ }216+ }217+ return folded.map((activity) => {218+ const payload = record(activity.payload);219+ const submittedAt = payload?.questionAnswerSubmittedAt;220+ if (221+ typeof submittedAt !== "string" ||222+ !activity.turnId ||223+ (latestProviderActivity.get(activity.turnId) ?? "") <= submittedAt
- apps/web/src/session-logic.ts lines 1646 to 1653OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -1624,0 +1633,21 @@1633+ );1634+ const showMessage = (message: ChatMessage) =>1635+ message.role !== "user" || !foldedAnswerMessageIds.has(message.id);1636+ const asyncAnswers = new Map<string, ChatMessage>(1637+ messages1638+ .filter(1639+ (message) =>1640+ message.role === "user" && message.turnId && foldedAnswerMessageIds.has(message.id),1641+ )1642+ .map((message) => [message.id, message]),1643+ );1644+ const latestProviderActivity = new Map<TurnId, string>();1645+ if (asyncAnswers.size > 0) {1646+ for (const entry of workEntries) {1647+ if (1648+ entry.turnId &&1649+ (entry.sourceActivityKind?.startsWith("tool.") ||1650+ entry.sourceActivityKind?.startsWith("task.")) &&1651+ entry.createdAt > (latestProviderActivity.get(entry.turnId) ?? "")1652+ )1653+ latestProviderActivity.set(entry.turnId, entry.createdAt);
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:XL
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- packages/client-runtime/src/work-log/userInput.ts lines 126 to 126OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +106,21 @@106+ result.push(activity);107+ continue;108+ }109+ const payload = record(activity.payload);110+ const requestId = payload?.requestId;111+ if (!payload || typeof requestId !== "string" || requestId.length === 0) {112+ result.push(activity);113+ continue;114+ }115+ const position = positions.get(requestId);116+ const previous = position === undefined ? undefined : result[position];117+ const previousPayload = record(previous?.payload);118+ const questionTextById = {119+ ...record(previousPayload?.questionTextById),120+ ...record(payload.questionTextById),121+ };122+ if (Array.isArray(payload.questions)) {123+ for (const value of payload.questions) {124+ const question = record(value);125+ if (typeof question?.id === "string" && typeof question.question === "string") {126+ questionTextById[question.id] = question.question;
- packages/client-runtime/src/work-log/userInput.ts line 58Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +38,21 @@38+ const questions = new Set<string>();39+ for (const activity of activities) {40+ if (activity.kind !== "user-input.answer-submitted" || !activity.turnId) continue;41+ const payload = record(activity.payload);42+ const texts = Object.values(record(payload?.questionTextById) ?? {});43+ const fingerprint = questionFingerprint(activity.turnId, texts);44+ if (fingerprint) questions.add(fingerprint);45+ }46+ if (questions.size === 0) return activities;47+ const duplicateToolIds = new Set<string>();48+ for (const activity of activities) {49+ if (!activity.kind.startsWith("tool.") || !activity.turnId) continue;50+ const payload = record(activity.payload);51+ if (typeof payload?.toolCallId !== "string") continue;52+ const input = projectQuestionToolInput(record(payload.data) ?? {}, payload.title).input;53+ if (!input) continue;54+ const fingerprint = questionFingerprint(55+ activity.turnId,56+ input.questions.map((question) => record(question)?.question),57+ );58+ if (fingerprint && questions.has(fingerprint)) {
maria-rcks merged this27eb79d
maria-rcks closed this
Sign in to join this conversation.


macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed