@fabrika/canvas-kernel 0.1.44

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +74 -0
  3. package/annotate.ts +94 -0
  4. package/daemon.ts +46 -0
  5. package/index.ts +84 -0
  6. package/package.json +74 -0
  7. package/src/client/AnnotationDraftFooter.tsx +76 -0
  8. package/src/client/AnnotationEditor.tsx +320 -0
  9. package/src/client/AnnotationList.tsx +190 -0
  10. package/src/client/AnnotationProvider.tsx +294 -0
  11. package/src/client/AnnotationSidebar.tsx +374 -0
  12. package/src/client/FileIcon.tsx +76 -0
  13. package/src/client/PlanRenderer.tsx +587 -0
  14. package/src/client/Popover.tsx +361 -0
  15. package/src/client/RenderErrorContext.ts +11 -0
  16. package/src/client/ResponsePreview.tsx +186 -0
  17. package/src/client/SessionContext.tsx +3 -0
  18. package/src/client/annotationContext.ts +130 -0
  19. package/src/client/annotationDom.ts +115 -0
  20. package/src/client/annotationDraft.ts +130 -0
  21. package/src/client/annotationMarkdown.ts +102 -0
  22. package/src/client/annotationTarget.ts +73 -0
  23. package/src/client/appContext.tsx +61 -0
  24. package/src/client/blockTarget.ts +127 -0
  25. package/src/client/components/Callout.tsx +27 -0
  26. package/src/client/components/Checklist.tsx +37 -0
  27. package/src/client/components/Choice.tsx +178 -0
  28. package/src/client/components/CodeBlock.tsx +35 -0
  29. package/src/client/components/Diff.tsx +36 -0
  30. package/src/client/components/FilePreview.tsx +88 -0
  31. package/src/client/components/Image.tsx +48 -0
  32. package/src/client/components/Markdown.tsx +44 -0
  33. package/src/client/components/Mermaid.tsx +576 -0
  34. package/src/client/components/Note.tsx +14 -0
  35. package/src/client/components/Priority.tsx +19 -0
  36. package/src/client/components/ResponseNote.tsx +19 -0
  37. package/src/client/components/Section.tsx +33 -0
  38. package/src/client/components/Table.tsx +35 -0
  39. package/src/client/components/Task.tsx +76 -0
  40. package/src/client/components/UserInput.tsx +176 -0
  41. package/src/client/components/index.ts +18 -0
  42. package/src/client/components/renderMarkdownChildren.tsx +100 -0
  43. package/src/client/diffLines.ts +50 -0
  44. package/src/client/generateMarkdown.ts +250 -0
  45. package/src/client/highlightRange.ts +262 -0
  46. package/src/client/hostContext.tsx +55 -0
  47. package/src/client/index.ts +128 -0
  48. package/src/client/popoverPosition.ts +46 -0
  49. package/src/client/preact-all.ts +4 -0
  50. package/src/client/regionTarget.ts +180 -0
  51. package/src/client/runtime.ts +149 -0
  52. package/src/client/styles.css +492 -0
  53. package/src/client/testing/dom.ts +58 -0
  54. package/src/client/textTarget.ts +31 -0
  55. package/src/client/theme.css +66 -0
  56. package/src/client/useRegionAnnotation.tsx +188 -0
  57. package/src/client/useTextAnnotation.tsx +194 -0
  58. package/src/client/utils.ts +33 -0
  59. package/src/engine/channel.ts +278 -0
  60. package/src/engine/compiler.ts +415 -0
  61. package/src/engine/http-utils.ts +14 -0
  62. package/src/engine/lifecycle.ts +302 -0
  63. package/src/engine/paths.ts +63 -0
  64. package/src/engine/router.ts +16 -0
  65. package/src/engine/session.ts +678 -0
  66. package/src/engine/waiter.ts +188 -0
  67. package/src/engine/watcher.ts +84 -0
  68. package/src/engine/websocket.ts +159 -0
@@ -0,0 +1,35 @@
1
+ import React from "react";
2
+
3
+ interface TableProps {
4
+ headers: string[];
5
+ rows: string[][];
6
+ }
7
+
8
+ export function Table({ headers, rows }: TableProps) {
9
+ return (
10
+ <div className="overflow-x-auto mt-3" data-md="table">
11
+ <table className="w-full text-body">
12
+ <thead>
13
+ <tr className="border-b border-border-subtle">
14
+ {headers.map((h, i) => (
15
+ <th key={i} className="text-left px-4 py-2.5 text-meta font-medium uppercase tracking-wider text-text-tertiary font-body">
16
+ {h}
17
+ </th>
18
+ ))}
19
+ </tr>
20
+ </thead>
21
+ <tbody>
22
+ {rows.map((row, ri) => (
23
+ <tr key={ri} className={`relative border-b border-border-subtle last:border-b-0 transition-colors hover:bg-bg-surface/50 ${ri % 2 === 1 ? "bg-bg-surface/30" : ""}`}>
24
+ {row.map((cell, ci) => (
25
+ <td key={ci} className="px-4 py-2.5 text-text-secondary font-body">
26
+ {cell}
27
+ </td>
28
+ ))}
29
+ </tr>
30
+ ))}
31
+ </tbody>
32
+ </table>
33
+ </div>
34
+ );
35
+ }
@@ -0,0 +1,76 @@
1
+ import React from "react";
2
+ import { renderMarkdownChildren } from "./renderMarkdownChildren";
3
+
4
+ type Status = "todo" | "done" | "blocked" | "in-progress";
5
+ type BadgeVariant = "info" | "success" | "warning" | "danger" | "neutral";
6
+
7
+ interface ItemProps {
8
+ id: string;
9
+ label: string;
10
+ status?: Status;
11
+ badge?: string;
12
+ badgeVariant?: BadgeVariant;
13
+ children?: React.ReactNode;
14
+ }
15
+
16
+ const dotColors: Record<Status, string> = {
17
+ todo: "bg-border-hover",
18
+ done: "bg-accent-green",
19
+ blocked: "bg-accent-red",
20
+ "in-progress": "bg-accent-blue",
21
+ };
22
+
23
+ const statusLabels: Record<Status, string> = {
24
+ todo: "Todo",
25
+ done: "Done",
26
+ blocked: "Blocked",
27
+ "in-progress": "In progress",
28
+ };
29
+
30
+ const badgeStyles: Record<BadgeVariant, string> = {
31
+ info: "bg-accent-blue-muted text-accent-blue",
32
+ success: "bg-accent-green-muted text-accent-green",
33
+ warning: "bg-accent-amber-muted text-accent-amber",
34
+ danger: "bg-accent-red-muted text-accent-red",
35
+ neutral: "bg-border-subtle text-text-tertiary",
36
+ };
37
+
38
+ export function Item({ id, label, status, badge, badgeVariant = "neutral", children }: ItemProps) {
39
+ return (
40
+ <div
41
+ className="group/task relative -mx-4 px-4 py-3 rounded-md transition-colors duration-150 hover:bg-bg-input"
42
+ data-task-id={id}
43
+ data-md="item"
44
+ data-md-label={label}
45
+ {...(status ? { "data-md-status": status } : {})}
46
+ {...(badge ? { "data-md-badge": badge } : {})}
47
+ >
48
+ <div className="flex items-start gap-3">
49
+ {status && <span className={`w-2 h-2 rounded-full mt-[7px] flex-shrink-0 ${dotColors[status]}`} />}
50
+ <div className="flex-1 min-w-0">
51
+ <div className="flex items-baseline gap-2">
52
+ <span className="font-body text-task-label font-semibold text-text-primary">{label}</span>
53
+ {status && (
54
+ <span className="font-body text-tiny uppercase tracking-wider text-text-tertiary opacity-0 group-hover/task:opacity-100 transition-opacity duration-150">
55
+ {statusLabels[status]}
56
+ </span>
57
+ )}
58
+ {badge && (
59
+ <span className={`font-body text-tiny font-medium px-1.5 py-px rounded-full ${badgeStyles[badgeVariant]}`}>
60
+ {badge}
61
+ </span>
62
+ )}
63
+ </div>
64
+ {children && (
65
+ <div className="text-body text-text-secondary mt-1 leading-relaxed">{renderMarkdownChildren(children)}</div>
66
+ )}
67
+ </div>
68
+ </div>
69
+ </div>
70
+ );
71
+ }
72
+
73
+ /** @deprecated Use <Item> instead */
74
+ export function Task(props: ItemProps) {
75
+ return <Item {...props} />;
76
+ }
@@ -0,0 +1,176 @@
1
+ import React, { useRef, useState } from "react";
2
+ import { useAnnotations, useResponseRegistration } from "#canvas/runtime";
3
+ import { ResponseNote } from "./ResponseNote";
4
+ import { autoResizeTextarea } from "../utils";
5
+
6
+ /** Free text input from user */
7
+ interface UserInputProps {
8
+ id: string;
9
+ label: string;
10
+ placeholder?: string;
11
+ multiline?: boolean;
12
+ required?: boolean;
13
+ }
14
+
15
+ export function UserInput({ id, label, placeholder, multiline, required }: UserInputProps) {
16
+ const { responses, setResponse } = useAnnotations();
17
+ useResponseRegistration(id);
18
+ const current = responses.get(id);
19
+ const value = (current?.value as string) ?? "";
20
+ const note = current?.note || "";
21
+ const [showNote, setShowNote] = useState(false);
22
+ const ref = useRef<HTMLTextAreaElement>(null);
23
+
24
+ if (!responses.has(id)) {
25
+ setResponse(id, { id, type: "text", label, value: "", required });
26
+ }
27
+
28
+ const handleChange = (v: string) => {
29
+ setResponse(id, { ...current!, value: v });
30
+ };
31
+
32
+ const autoResize = (el: HTMLTextAreaElement) => autoResizeTextarea(el, multiline ? 60 : 32);
33
+
34
+ const showError = current?.required && !value.trim();
35
+
36
+ return (
37
+ <div data-md="userinput" data-md-label={label} className="-mx-4 px-4 py-3 my-1 rounded-lg transition-colors duration-150 hover:bg-bg-input">
38
+ <div className="flex items-baseline gap-2 mb-2">
39
+ <span className="text-[13px] font-body font-medium text-text-primary">{label}</span>
40
+ {required && <span className="text-[10px] text-accent-red font-body">*</span>}
41
+ </div>
42
+ <textarea
43
+ ref={ref}
44
+ value={value}
45
+ onChange={(e) => { handleChange(e.currentTarget.value); autoResize(e.currentTarget); }}
46
+ placeholder={placeholder || "Type your response..."}
47
+ rows={multiline ? 3 : 1}
48
+ className="w-full bg-bg-input text-[13px] font-body text-text-primary rounded-lg px-3 py-2 resize-none focus:outline-none border border-border-subtle focus:border-border-hover placeholder:text-text-tertiary transition-colors"
49
+ onInput={(e) => autoResize(e.currentTarget)}
50
+ onFocus={(e) => autoResize(e.currentTarget)}
51
+ />
52
+ <ResponseNote show={showNote} note={note} onToggle={() => setShowNote(!showNote)} onChange={(n) => setResponse(id, { ...current!, note: n })} />
53
+ {showError && <p className="text-[11px] text-accent-red font-body mt-1">This field is required.</p>}
54
+ </div>
55
+ );
56
+ }
57
+
58
+ /** Numeric range / slider */
59
+ interface RangeInputProps {
60
+ id: string;
61
+ label: string;
62
+ min?: number;
63
+ max?: number;
64
+ step?: number;
65
+ required?: boolean;
66
+ minLabel?: string;
67
+ maxLabel?: string;
68
+ }
69
+
70
+ export function RangeInput({ id, label, min = 1, max = 10, step = 1, required, minLabel, maxLabel }: RangeInputProps) {
71
+ const { responses, setResponse } = useAnnotations();
72
+ useResponseRegistration(id);
73
+ const current = responses.get(id);
74
+ const value = current?.value as number | null;
75
+ const note = current?.note || "";
76
+ const [showNote, setShowNote] = useState(false);
77
+
78
+ if (!responses.has(id)) {
79
+ setResponse(id, { id, type: "range", label, value: null, required });
80
+ }
81
+
82
+ const handleChange = (v: number) => {
83
+ setResponse(id, { ...current!, value: v });
84
+ };
85
+
86
+ const hasValue = value !== null && value !== undefined;
87
+ const displayValue = hasValue ? value : Math.round((min + max) / 2);
88
+ const pct = ((displayValue - min) / (max - min)) * 100;
89
+ const stepCount = Math.round((max - min) / step);
90
+ const showDots = stepCount <= 20;
91
+
92
+ const showError = current?.required && !hasValue;
93
+
94
+ return (
95
+ <div data-md="rangeinput" data-md-label={label} className="-mx-4 px-4 py-3 my-1 rounded-lg transition-colors duration-150 hover:bg-bg-input">
96
+ <div className="flex items-baseline justify-between mb-1">
97
+ <div className="flex items-baseline gap-2">
98
+ <span className="text-[13px] font-body font-medium text-text-primary">{label}</span>
99
+ {required && <span className="text-[10px] text-accent-red font-body">*</span>}
100
+ </div>
101
+ <span className="flex items-center gap-1.5">
102
+ <span className={`text-[13px] font-mono ${hasValue ? "text-accent-amber" : "text-text-disabled"}`}>
103
+ {hasValue ? value : "—"}
104
+ </span>
105
+ {hasValue && (
106
+ <button
107
+ onClick={() => setResponse(id, { ...current!, value: null })}
108
+ className="text-text-disabled hover:text-text-tertiary transition-colors text-[10px] leading-none"
109
+ title="Clear"
110
+ >
111
+ &#x2715;
112
+ </button>
113
+ )}
114
+ </span>
115
+ </div>
116
+ <div className="relative h-6 flex items-center">
117
+ {/* Track background */}
118
+ <div className="absolute left-0 right-0 h-1.5 rounded-full bg-border-medium">
119
+ {hasValue && <div className="h-full rounded-full bg-accent-amber transition-all duration-150" style={{ width: `${pct}%` }} />}
120
+ </div>
121
+ {/* Discrete step dots */}
122
+ {showDots && Array.from({ length: stepCount + 1 }, (_, i) => {
123
+ const dotVal = min + i * step;
124
+ const dotPct = ((dotVal - min) / (max - min)) * 100;
125
+ const isSelected = hasValue && dotVal <= value!;
126
+ const isActive = hasValue && dotVal === value;
127
+ return (
128
+ <div
129
+ key={i}
130
+ className={`absolute rounded-full pointer-events-none transition-all duration-150 ${
131
+ isActive ? "w-2.5 h-2.5 bg-accent-amber" : isSelected ? "w-1.5 h-1.5 bg-accent-amber" : "w-1.5 h-1.5 bg-border-hover"
132
+ }`}
133
+ style={{ left: `${dotPct}%`, transform: "translate(-50%, -50%)", top: "50%" }}
134
+ />
135
+ );
136
+ })}
137
+ {/* Thumb indicator */}
138
+ {hasValue ? (
139
+ <div className="absolute h-5 w-5 rounded-full bg-accent-amber shadow-md pointer-events-none transition-all duration-150" style={{ left: `calc(${pct}% - 10px)`, borderWidth: "2px", borderStyle: "solid", borderColor: "var(--color-bg-base)" }} />
140
+ ) : (
141
+ <div className="absolute h-4 w-4 rounded-full bg-border-hover pointer-events-none opacity-50" style={{ left: "calc(50% - 8px)", borderWidth: "2px", borderStyle: "solid", borderColor: "var(--color-bg-base)" }} />
142
+ )}
143
+ <input
144
+ type="range"
145
+ min={min}
146
+ max={max}
147
+ step={step}
148
+ value={displayValue}
149
+ onChange={(e) => handleChange(Number(e.currentTarget.value))}
150
+ className="absolute inset-0 w-full opacity-0 cursor-pointer"
151
+ />
152
+ </div>
153
+ {/* Labels: show step values if few enough, otherwise just min/max */}
154
+ {showDots && stepCount <= 10 ? (
155
+ <div className="flex justify-between -mt-0.5 px-0">
156
+ {Array.from({ length: stepCount + 1 }, (_, i) => {
157
+ const dotVal = min + i * step;
158
+ const isActive = hasValue && dotVal === value;
159
+ return (
160
+ <span key={i} className={`text-[10px] font-mono transition-colors ${isActive ? "text-accent-amber font-medium" : "text-text-disabled"}`} style={{ minWidth: "1ch", textAlign: "center" }}>
161
+ {dotVal}
162
+ </span>
163
+ );
164
+ })}
165
+ </div>
166
+ ) : (
167
+ <div className="flex justify-between -mt-0.5">
168
+ <span className="text-[11px] text-text-tertiary font-body">{minLabel || min}</span>
169
+ <span className="text-[11px] text-text-tertiary font-body">{maxLabel || max}</span>
170
+ </div>
171
+ )}
172
+ <ResponseNote show={showNote} note={note} onToggle={() => setShowNote(!showNote)} onChange={(n) => setResponse(id, { ...current!, note: n })} />
173
+ {showError && <p className="text-[11px] text-accent-red font-body mt-1">Please select a value.</p>}
174
+ </div>
175
+ );
176
+ }
@@ -0,0 +1,18 @@
1
+ // Components every canvas host provides. Hosts that ship extra components
2
+ // compose their own `#canvas/components` barrel over this one.
3
+ export { Section } from "./Section";
4
+ export { Item, Task } from "./Task";
5
+ export { CodeBlock } from "./CodeBlock";
6
+ export { Callout } from "./Callout";
7
+ export { Note } from "./Note";
8
+ export { Table } from "./Table";
9
+ export { Checklist } from "./Checklist";
10
+ export { Priority } from "./Priority";
11
+ export { FilePreview } from "./FilePreview";
12
+ export { Mermaid } from "./Mermaid";
13
+ export { Diff } from "./Diff";
14
+ export { Choice, MultiChoice } from "./Choice";
15
+ export { UserInput, RangeInput } from "./UserInput";
16
+ export { ImageView } from "./Image";
17
+ export { Markdown } from "./Markdown";
18
+ export { useFeedback } from "#canvas/runtime";
@@ -0,0 +1,100 @@
1
+ import React from "react";
2
+ import { marked } from "marked";
3
+
4
+ marked.setOptions({ gfm: true, breaks: false });
5
+
6
+ /** Host tags that flow inline and must not break a prose run into separate blocks. */
7
+ const INLINE_TAGS = new Set([
8
+ "a", "abbr", "b", "br", "code", "del", "em", "i", "kbd", "mark",
9
+ "q", "s", "small", "span", "strong", "sub", "sup", "time", "u",
10
+ ]);
11
+
12
+ /** Custom components that render inline (e.g. <Priority>). */
13
+ const INLINE_COMPONENTS = new Set(["Priority"]);
14
+
15
+ function isInlineNode(node: React.ReactNode): boolean {
16
+ if (typeof node === "string" || typeof node === "number") return true;
17
+ if (!React.isValidElement(node)) return false;
18
+ // Read `type` structurally: preact/compat's isValidElement is not a type
19
+ // guard, so the narrowing React's typings give here does not survive there.
20
+ if (typeof node !== "object" || node === null || !("type" in node)) return false;
21
+ const type: unknown = node.type;
22
+ if (typeof type === "string") return INLINE_TAGS.has(type);
23
+ if (typeof type === "function") {
24
+ const name = "displayName" in type && typeof type.displayName === "string" ? type.displayName : type.name;
25
+ return name ? INLINE_COMPONENTS.has(name) : false;
26
+ }
27
+ return false;
28
+ }
29
+
30
+ function renderTextAsMarkdown(text: string, key: string): React.ReactNode {
31
+ const trimmed = text.trim();
32
+ if (!trimmed) return null;
33
+ const html = marked.parse(trimmed, { async: false }) as string;
34
+ return (
35
+ <div
36
+ key={key}
37
+ className="prose-canvas"
38
+ data-md="markdown"
39
+ data-md-source={trimmed}
40
+ dangerouslySetInnerHTML={{ __html: html }}
41
+ />
42
+ );
43
+ }
44
+
45
+ /**
46
+ * Render a run of inline content (text + inline JSX like <code>/<strong>) as a single
47
+ * flowing paragraph. Text segments are parsed as inline markdown; JSX passes through.
48
+ * No `data-md` attribute so the markdown exporter treats it as an inline container.
49
+ */
50
+ function renderInlineRun(run: React.ReactNode[], key: string): React.ReactNode {
51
+ return (
52
+ <div key={key} className="prose-canvas">
53
+ {run.map((node, i) => {
54
+ if (typeof node === "string" || typeof node === "number") {
55
+ const html = marked.parseInline(String(node), { async: false }) as string;
56
+ return <span key={i} dangerouslySetInnerHTML={{ __html: html }} />;
57
+ }
58
+ return node;
59
+ })}
60
+ </div>
61
+ );
62
+ }
63
+
64
+ /**
65
+ * Auto-render markdown for string children, passing JSX children through.
66
+ * Block-level JSX (Callout, Diff, etc.) breaks the prose into separate markdown blocks,
67
+ * while inline JSX (code, strong, links) stays within the surrounding paragraph so text
68
+ * keeps flowing instead of stacking one fragment per line.
69
+ */
70
+ export function renderMarkdownChildren(children: React.ReactNode): React.ReactNode {
71
+ const kids = React.Children.toArray(children);
72
+ if (kids.length === 0) return null;
73
+
74
+ const result: React.ReactNode[] = [];
75
+ let segment: React.ReactNode[] = [];
76
+ let segIdx = 0;
77
+
78
+ const flush = () => {
79
+ if (segment.length === 0) return;
80
+ const hasInlineJsx = segment.some((n) => typeof n !== "string" && typeof n !== "number");
81
+ if (hasInlineJsx) {
82
+ result.push(renderInlineRun(segment, `md-${segIdx++}`));
83
+ } else {
84
+ const node = renderTextAsMarkdown(segment.join(""), `md-${segIdx++}`);
85
+ if (node) result.push(node);
86
+ }
87
+ segment = [];
88
+ };
89
+
90
+ for (const child of kids) {
91
+ if (isInlineNode(child)) {
92
+ segment.push(child);
93
+ } else {
94
+ flush();
95
+ result.push(child);
96
+ }
97
+ }
98
+ flush();
99
+ return result;
100
+ }
@@ -0,0 +1,50 @@
1
+ export interface DiffLine {
2
+ type: "same" | "removed" | "added";
3
+ line: string;
4
+ }
5
+
6
+ /** Line-level diff of two texts, longest-common-subsequence based. */
7
+ export function buildDiffLines(before: string, after: string): DiffLine[] {
8
+ const beforeLines = before.split("\n");
9
+ const afterLines = after.split("\n");
10
+ const lcs = computeLCS(beforeLines, afterLines);
11
+ const diffLines: DiffLine[] = [];
12
+
13
+ let bi = 0, ai = 0, li = 0;
14
+ while (bi < beforeLines.length || ai < afterLines.length) {
15
+ // split() never leaves holes, so "in range" and "defined" are the same test.
16
+ const beforeLine = beforeLines[bi];
17
+ const afterLine = afterLines[ai];
18
+ if (li < lcs.length && beforeLine !== undefined && afterLine !== undefined
19
+ && beforeLine === lcs[li] && afterLine === lcs[li]) {
20
+ diffLines.push({ type: "same", line: beforeLine }); bi++; ai++; li++;
21
+ } else if (beforeLine !== undefined && (li >= lcs.length || beforeLine !== lcs[li])) {
22
+ diffLines.push({ type: "removed", line: beforeLine }); bi++;
23
+ } else if (afterLine !== undefined) {
24
+ diffLines.push({ type: "added", line: afterLine }); ai++;
25
+ }
26
+ }
27
+
28
+ return diffLines;
29
+ }
30
+
31
+ export function computeLCS(a: string[], b: string[]): string[] {
32
+ const m = a.length, n = b.length;
33
+ // Rows are fully filled before use; the `?? 0` fallbacks only satisfy the
34
+ // checker, which cannot see that every index here is in range.
35
+ const dp: number[][] = Array.from({ length: m + 1 }, () => new Array<number>(n + 1).fill(0));
36
+ for (let i = 1; i <= m; i++) {
37
+ const row = dp[i] ?? [];
38
+ const prev = dp[i - 1] ?? [];
39
+ for (let j = 1; j <= n; j++)
40
+ row[j] = a[i-1] === b[j-1] ? (prev[j-1] ?? 0) + 1 : Math.max(prev[j] ?? 0, row[j-1] ?? 0);
41
+ }
42
+ const result: string[] = [];
43
+ let i = m, j = n;
44
+ while (i > 0 && j > 0) {
45
+ const line = a[i-1];
46
+ if (line !== undefined && line === b[j-1]) { result.unshift(line); i--; j--; }
47
+ else if ((dp[i-1]?.[j] ?? 0) > (dp[i]?.[j-1] ?? 0)) i--; else j--;
48
+ }
49
+ return result;
50
+ }