File size: 2,169 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
import type { AppContext } from "@api/ai/agents/config/shared";
import { db } from "@midday/db/client";
import { getTimerStatus } 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 getTimerStatusSchema = z.object({
  assignedId: z.string().nullable().optional().describe("User ID"),
});

export const getTimerStatusTool = tool({
  description:
    "Get current timer status - shows if timer is running and elapsed time.",
  inputSchema: getTimerStatusSchema,
  execute: async function* ({ 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 get timer status: Team ID not found in context.",
      };
      return;
    }

    try {
      const status = await getTimerStatus(db, {
        teamId,
        assignedId: userId,
      });

      if (!status.isRunning || !status.currentEntry) {
        yield {
          text: "No timer is currently running.",
        };
        return;
      }

      const start = new Date(0);
      const end = new Date(status.elapsedTime * 1000);
      const formattedElapsed = formatDistance(start, end, {
        includeSeconds: false,
      });
      const startTime = status.currentEntry.start
        ? formatDate(status.currentEntry.start, "HH:mm")
        : "N/A";

      const response = `Timer is running!\n\n**Project:** ${status.currentEntry.trackerProject?.name || "Unknown"}\n**Started at:** ${startTime}\n**Elapsed time:** ${formattedElapsed}\n**Description:** ${status.currentEntry.description || "None"}`;

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