Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
75 changes: 51 additions & 24 deletions apps/web/src/app/api/telegram/webhook/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const {
summarizeConversationMemoryWithResponsesMock,
respondToConversationTurnWithResponsesMock,
recoverConfirmedMutationWithResponsesMock,
extractSlotsWithResponsesMock,
interpretWriteTurnWithResponsesMock,
} = vi.hoisted(() => ({
editTelegramMessageMock: vi.fn(),
sendTelegramMessageMock: vi.fn(),
Expand All @@ -54,7 +54,7 @@ const {
summarizeConversationMemoryWithResponsesMock: vi.fn(),
respondToConversationTurnWithResponsesMock: vi.fn(),
recoverConfirmedMutationWithResponsesMock: vi.fn(),
extractSlotsWithResponsesMock: vi.fn(),
interpretWriteTurnWithResponsesMock: vi.fn(),
}));

vi.mock("@atlas/integrations", async () => {
Expand Down Expand Up @@ -102,7 +102,7 @@ vi.mock("@atlas/integrations", async () => {
recoverConfirmedMutationWithResponsesMock,
classifyTurnWithResponses: classifyTurnWithResponsesMock,
routeTurnWithResponses: routeTurnWithResponsesMock,
extractSlotsWithResponses: extractSlotsWithResponsesMock,
interpretWriteTurnWithResponses: interpretWriteTurnWithResponsesMock,
sendTelegramChatAction: sendTelegramChatActionMock,
sendTelegramMessage: sendTelegramMessageMock,
summarizeConversationMemoryWithResponses:
Expand Down Expand Up @@ -367,14 +367,25 @@ beforeEach(async () => {
summarizeConversationMemoryWithResponsesMock.mockReset();
respondToConversationTurnWithResponsesMock.mockReset();
recoverConfirmedMutationWithResponsesMock.mockReset();
extractSlotsWithResponsesMock.mockReset();
extractSlotsWithResponsesMock.mockResolvedValue({
time: { kind: "absolute", hour: 9, minute: 0 },
day: { kind: "relative", value: "tomorrow" },
duration: null,
target: null,
confidence: { day: 0.95, time: 0.95 },
unresolvable: [],
interpretWriteTurnWithResponsesMock.mockReset();
interpretWriteTurnWithResponsesMock.mockResolvedValue({
operationKind: "plan",
actionDomain: "task",
targetRef: null,
taskName: null,
fields: {
scheduleFields: {
time: { kind: "absolute", hour: 9, minute: 0 },
day: { kind: "relative", value: "tomorrow" },
duration: null,
},
taskFields: null,
},
confidence: {
"scheduleFields.day": 0.95,
"scheduleFields.time": 0.95,
},
unresolvedFields: [],
});
routeTurnWithResponsesMock.mockResolvedValue({
route: "mutation",
Expand Down Expand Up @@ -842,13 +853,17 @@ describe("telegram webhook route", () => {

it("normalizes a Telegram text message and routes to clarification when slots are missing", async () => {
process.env.TELEGRAM_WEBHOOK_SECRET = "test-webhook-secret";
extractSlotsWithResponsesMock.mockResolvedValueOnce({
time: null,
day: null,
duration: null,
target: null,
interpretWriteTurnWithResponsesMock.mockResolvedValueOnce({
operationKind: "plan",
actionDomain: "task",
targetRef: null,
taskName: null,
fields: {
scheduleFields: null,
taskFields: null,
},
confidence: {},
unresolvable: [],
unresolvedFields: [],
});

const response = await handleTelegramWebhook(
Expand Down Expand Up @@ -1058,13 +1073,25 @@ describe("telegram webhook route", () => {

it("does not keep clear scheduling requests in discuss-first mode", async () => {
process.env.TELEGRAM_WEBHOOK_SECRET = "test-webhook-secret";
extractSlotsWithResponsesMock.mockResolvedValueOnce({
time: { kind: "absolute", hour: 18, minute: 0 },
day: { kind: "relative", value: "tomorrow" },
duration: { minutes: 60 },
target: null,
confidence: { day: 0.95, time: 0.95, duration: 0.9 },
unresolvable: [],
interpretWriteTurnWithResponsesMock.mockResolvedValueOnce({
operationKind: "plan",
actionDomain: "task",
targetRef: null,
taskName: null,
fields: {
scheduleFields: {
time: { kind: "absolute", hour: 18, minute: 0 },
day: { kind: "relative", value: "tomorrow" },
duration: { minutes: 60 },
},
taskFields: null,
},
confidence: {
"scheduleFields.day": 0.95,
"scheduleFields.time": 0.95,
"scheduleFields.duration": 0.9,
},
unresolvedFields: [],
});

const response = await handleTelegramWebhook(
Expand Down
1 change: 1 addition & 0 deletions apps/web/src/lib/server/decide-turn-policy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const emptyCommit: CommitPolicyOutput = {
needsClarification: [],
missingFields: [],
workflowChanged: false,
committedFieldPaths: [],
};

function input(
Expand Down
55 changes: 55 additions & 0 deletions apps/web/src/lib/server/interpret-write-turn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import {
normalizeRawWriteInterpretation,
rawWriteInterpretationSchema,
type WriteInterpretation,
type WriteInterpretationInput,
} from "@atlas/core";
import {
interpretWriteTurnWithResponses,
type OpenAIResponsesClient,
} from "@atlas/integrations";

export async function interpretWriteTurn(
input: WriteInterpretationInput,
client?: OpenAIResponsesClient,
): Promise<WriteInterpretation> {
try {
const raw = await interpretWriteTurnWithResponses(input, client);
const parsed = rawWriteInterpretationSchema.safeParse(raw);

if (!parsed.success) {
return fallbackInterpretation(input);
}

return normalizeRawWriteInterpretation(parsed.data, input.currentTurnText);
} catch {
return fallbackInterpretation(input);
}
}

function fallbackInterpretation(
input: WriteInterpretationInput,
): WriteInterpretation {
return {
operationKind:
input.priorPendingWriteOperation?.operationKind ?? inferFallbackOperation(input.turnType),
actionDomain: "task",
targetRef: input.priorPendingWriteOperation?.targetRef ?? null,
taskName: null,
fields: {},
sourceText: input.currentTurnText,
confidence: {},
unresolvedFields: [],
};
}

function inferFallbackOperation(turnType: WriteInterpretationInput["turnType"]) {
switch (turnType) {
case "edit_request":
return "edit" as const;
case "clarification_answer":
case "planning_request":
default:
return "plan" as const;
}
}
Loading