File size: 2,606 Bytes
c09f67c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import type { AppContext } from "@api/ai/agents/config/shared";
import { db } from "@midday/db/client";
import { stopTimer } from "@midday/db/queries";
import { getAppUrl } from "@midday/utils/envs";
import { formatDate } from "@midday/utils/format";
import { tool } from "ai";
import { formatDistance } from "date-fns";
import { z } from "zod";

const stopTimerSchema = z.object({
  entryId: z.string().nullable().optional().describe("Timer entry ID"),
  assignedId: z.string().nullable().optional().describe("User ID"),
});

export const stopTimerTool = tool({
  description:
    "Stop the current running timer - calculates and saves the duration.",
  inputSchema: stopTimerSchema,
  execute: async function* ({ entryId, assignedId }, executionOptions) {
    const appContext = executionOptions.experimental_context as AppContext;
    const teamId = appContext.teamId as string;
    const userId = assignedId || appContext.userId || null;

    if (!teamId) {
      yield {
        text: "Unable to stop timer: Team ID not found in context.",
      };

      return;
    }

    try {
      // Stop the timer
      const result = await stopTimer(db, {
        teamId,
        entryId: entryId || undefined,
        assignedId: userId,
      });

      // Handle discarded entries (under 60 seconds)
      if (result.discarded) {
        yield {
          text: `Timer discarded - entry was under 1 minute and was not saved.\n\n**Project:** ${result.project?.name || "Unknown"}`,
          link: {
            text: "View tracker",
            url: `${getAppUrl()}/tracker`,
          },
        };
        return;
      }

      const duration = result.duration ? Number(result.duration) : 0;
      const start = new Date(0);
      const end = new Date(duration * 1000);
      const formattedDuration = formatDistance(start, end, {
        includeSeconds: false,
      });

      const startTime = result.start
        ? formatDate(result.start, "HH:mm")
        : "N/A";
      const stopTime = result.stop ? formatDate(result.stop, "HH:mm") : "N/A";

      const response = `Timer stopped successfully!\n\n**Project:** ${result.project?.name || "Unknown"}\n**Duration:** ${formattedDuration}\n**Started:** ${startTime}\n**Stopped:** ${stopTime}\n**Description:** ${result.description || "None"}`;

      yield {
        text: response,
        link: {
          text: "View tracker",
          url: `${getAppUrl()}/tracker`,
        },
      };
    } catch (error) {
      yield {
        text: `Failed to stop timer: ${error instanceof Error ? error.message : "Unknown error"}`,
      };
    }
  },
});