All files / components/transcript tool-output.tsx

100% Statements 73/73
79.71% Branches 55/69
100% Functions 7/7
100% Lines 69/69

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 2583x             3x 3x           3x           3x           3x     18x 18x 5x     13x   13x   14x 14x 14x     14x 14x 14x   14x 3x                 11x 9x       9x           2x             14x     22x 22x 4x     18x 18x 18x   18x 1x     17x 1x     16x   2x           2x       1x           1x       2x               2x         1x               1x       2x                                   4x   2x       2x 2x   1x                                   2x   1x       1x               1x         4x 4x   4x 2x   2x 1x               1x     2x 1x               1x     2x     2x 2x   1x               1x       1x      
import { isToolUIPart, type UIMessage } from "ai";
import {
  asRecord,
  asRecordArray,
  asOptionalString,
  isBalanceLikeRecord,
  isRequestLikeRecord,
} from "./utils";
import { getToolName, getFriendlyToolLabelByName } from "./tool";
import {
  getSelfRequestRowActions,
  getTeamRequestRowActions,
  getBalanceRowActions,
  getRequestRowActionBuilder,
} from "./cells";
import {
  buildMembersTableModel,
  getRequestTableModel,
  getBalanceTableModel,
  type ToolOutputTableModel,
} from "./named-tables";
import {
  collectRecordCollections,
  getCollectionId,
  getCollectionTitle,
  getGenericTableModel,
} from "./generic-table";
 
function getDynamicToolOutputTables(output: unknown, toolName: string | null) {
  const collections = collectRecordCollections(output);
  if (collections.length === 0) {
    return [] as ToolOutputTableModel[];
  }
 
  const idCollisionCount = new Map<string, number>();
 
  return collections
    .map((collection, index) => {
      const baseId = getCollectionId(collection.path, toolName);
      const collisionCount = idCollisionCount.get(baseId) ?? 0;
      idCollisionCount.set(baseId, collisionCount + 1);
 
      const id =
        collisionCount === 0 ? baseId : `${baseId}-${collisionCount + 1}`;
      const title = getCollectionTitle(collection.path, toolName);
      const rows = collection.rows;
 
      if (rows.every(isBalanceLikeRecord)) {
        return getBalanceTableModel({
          id,
          title,
          payload: { balances: rows },
          getRowActions: getBalanceRowActions,
          emptyLabel: "No records found.",
        });
      }
 
      if (rows.every(isRequestLikeRecord)) {
        return getRequestTableModel({
          id,
          title,
          payload: { requests: rows },
          showEmployee: rows.some((row) => Boolean(asOptionalString(row.employeeName))),
          getRowActions: getRequestRowActionBuilder(toolName),
          emptyLabel: "No records found.",
        });
      }
 
      return getGenericTableModel({
        id,
        title: title || `${getFriendlyToolLabelByName(toolName)} ${index + 1}`,
        rows,
        emptyLabel: "No records found.",
      });
    })
    .filter((table): table is ToolOutputTableModel => table !== null);
}
 
export function getToolOutputTables(part: UIMessage["parts"][number]) {
  if (!isToolUIPart(part) || part.state !== "output-available" || part.preliminary) {
    return [] as ToolOutputTableModel[];
  }
 
  const toolName = getToolName(part);
  const output = asRecord(part.output);
  const dynamicTables = getDynamicToolOutputTables(part.output, toolName);
 
  if (!toolName) {
    return dynamicTables;
  }
 
  if (!output) {
    return dynamicTables;
  }
 
  switch (toolName) {
    case "list_employees": {
      const table = buildMembersTableModel({
        id: "all-employees",
        title: "Project members",
        memberRows: asRecordArray(output.employees),
        emptyLabel: "No employees found.",
      });
      return table ? [table] : dynamicTables;
    }
 
    case "list_team_members": {
      const table = buildMembersTableModel({
        id: "team-members",
        title: "Team members",
        memberRows: asRecordArray(output.members),
        emptyLabel: "No team members found.",
      });
      return table ? [table] : dynamicTables;
    }
 
    case "list_my_time_off_requests": {
      const requests = getRequestTableModel({
        id: "my-time-off-requests",
        title: "My time-off requests",
        payload: output,
        showEmployee: false,
        getRowActions: getSelfRequestRowActions,
        emptyLabel: "No time-off requests found.",
      });
      return requests ? [requests] : dynamicTables;
    }
 
    case "list_team_time_off_requests": {
 
      const requests = getRequestTableModel({
        id: "team-time-off-requests",
        title: "Team time-off requests",
        payload: output,
        showEmployee: true,
        getRowActions: getTeamRequestRowActions,
        emptyLabel: "No team requests found.",
      });
      return requests ? [requests] : dynamicTables;
    }
 
    case "get_my_time_off_balance": {
      const tables = [
        getBalanceTableModel({
          id: "my-time-off-balance",
          title: "My leave balance",
          payload: output,
          getRowActions: getBalanceRowActions,
          emptyLabel: "No balance data found.",
        }),
        getRequestTableModel({
          id: "my-upcoming-requests",
          title: "Upcoming requests",
          payload: {
            requests: output.upcomingRequests,
          },
          showEmployee: false,
          getRowActions: getSelfRequestRowActions,
          emptyLabel: "No upcoming requests.",
        }),
      ].filter((table): table is ToolOutputTableModel => table !== null);
 
      return tables.length > 0 ? tables : dynamicTables;
    }
 
    case "submit_my_time_off_request": {
      const balance = asRecord(output.balance);
      if (!balance) return dynamicTables;
 
      const tables = [
        getBalanceTableModel({
          id: "updated-time-off-balance",
          title: "Updated leave balance",
          payload: balance,
          getRowActions: getBalanceRowActions,
          emptyLabel: "No balance data found.",
        }),
        getRequestTableModel({
          id: "updated-upcoming-requests",
          title: "Upcoming requests",
          payload: {
            requests: balance.upcomingRequests,
          },
          showEmployee: false,
          getRowActions: getSelfRequestRowActions,
          emptyLabel: "No upcoming requests.",
        }),
      ].filter((table): table is ToolOutputTableModel => table !== null);
 
      return tables.length > 0 ? tables : dynamicTables;
    }
 
    case "cancel_my_time_off_request": {
      const table = getRequestTableModel({
        id: "my-cancelled-requests",
        title: "Cancelled requests",
        payload: asRecord(output.cancelledRequests) ?? {},
        showEmployee: false,
        getRowActions: getSelfRequestRowActions,
        emptyLabel: "No cancelled requests.",
      });
      return table ? [table] : dynamicTables;
    }
 
    case "approve_team_time_off_request":
    case "reject_team_time_off_request": {
      const reviewedEmployeeRequests = asRecord(output.reviewedEmployeeRequests);
      const pendingTeamRequests = asRecord(output.pendingTeamRequests);
 
      if (reviewedEmployeeRequests || pendingTeamRequests) {
        const tables: ToolOutputTableModel[] = [];
 
        if (reviewedEmployeeRequests) {
          const table = getRequestTableModel({
            id: "reviewed-employee-requests",
            title: `${reviewedEmployeeRequests.query ?? "Employee"}'s requests`,
            payload: reviewedEmployeeRequests,
            showEmployee: true,
            getRowActions: getTeamRequestRowActions,
            emptyLabel: "No requests found.",
          });
          Eif (table) tables.push(table);
        }
 
        if (pendingTeamRequests) {
          const table = getRequestTableModel({
            id: "pending-team-requests",
            title: "Pending team requests",
            payload: pendingTeamRequests,
            showEmployee: true,
            getRowActions: getTeamRequestRowActions,
            emptyLabel: "No pending team requests.",
          });
          Eif (table) tables.push(table);
        }
 
        return tables.length > 0 ? tables : dynamicTables;
      }
 
      const teamRequests = asRecord(output.teamRequests);
      if (!teamRequests) return dynamicTables;
 
      const requests = getRequestTableModel({
        id: "team-time-off-requests",
        title: "Team time-off requests",
        payload: teamRequests,
        showEmployee: true,
        getRowActions: getTeamRequestRowActions,
        emptyLabel: "No team requests found.",
      });
      return requests ? [requests] : dynamicTables;
    }
 
    default:
      return dynamicTables;
  }
}