Skip to content

Commit b1f769e

Browse files
committed
Replace unsafe type casts with proper narrowing, enable noUncheckedIndexedAccess
- Use type guard instead of `as` casts in parseCursorEvent - Narrow run output via `in` checks instead of casting to Record - Enable noUncheckedIndexedAccess in tsconfig for safer indexing
1 parent c2f7b99 commit b1f769e

3 files changed

Lines changed: 17 additions & 10 deletions

File tree

cursor-cli-demo/components/terminal.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ import type { cursorAgentTask, STREAMS } from "@/trigger/cursor-agent";
66
import { parseCursorEvent } from "@/lib/cursor-events";
77
import { CursorEventRow } from "./cursor-event";
88

9+
function getRunErrorMessage(output: unknown): string {
10+
if (typeof output !== "object" || output === null) return "Task failed";
11+
if ("message" in output && typeof output.message === "string") return output.message;
12+
if ("error" in output && typeof output.error === "string") return output.error;
13+
return "Task failed";
14+
}
15+
916
export function Terminal({
1017
runId,
1118
publicAccessToken,
@@ -98,11 +105,7 @@ export function Terminal({
98105

99106
{isFailed && (
100107
<div className="text-red-400 text-sm">
101-
{(() => {
102-
const output = run?.output as Record<string, unknown> | undefined;
103-
const msg = output?.message ?? output?.error;
104-
return typeof msg === "string" ? msg : "Task failed";
105-
})()}
108+
{getRunErrorMessage(run?.output)}
106109
</div>
107110
)}
108111

cursor-cli-demo/lib/cursor-events.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,17 @@ export type ResultEvent = {
6464
session_id: string;
6565
};
6666

67-
const knownTypes = new Set(["system", "user", "assistant", "tool_call", "result"]);
67+
const knownTypes = new Set<string>(["system", "user", "assistant", "tool_call", "result"]);
68+
69+
function isCursorEvent(data: unknown): data is CursorEvent {
70+
if (typeof data !== "object" || data === null) return false;
71+
if (!("type" in data)) return false;
72+
return typeof data.type === "string" && knownTypes.has(data.type);
73+
}
6874

6975
/** Parse raw JSON into a CursorEvent, returns null for unknown types */
7076
export function parseCursorEvent(data: unknown): CursorEvent | null {
71-
if (typeof data !== "object" || data === null || !("type" in data)) return null;
72-
const typed = data as { type: string };
73-
if (!knownTypes.has(typed.type)) return null;
74-
return data as CursorEvent;
77+
return isCursorEvent(data) ? data : null;
7578
}
7679

7780
/** Extract the tool name from a tool_call event */

cursor-cli-demo/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"allowJs": true,
1010
"skipLibCheck": true,
1111
"strict": true,
12+
"noUncheckedIndexedAccess": true,
1213
"noEmit": true,
1314
"esModuleInterop": true,
1415
"module": "esnext",

0 commit comments

Comments
 (0)