@cairnvibe/sdk 0.2.2 → 0.2.4

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.
package/dist/index.js CHANGED
@@ -22,6 +22,21 @@ function Copilot({ endpoint = "/api/copilot", registeredActions = [], onDo, repo
22
22
  // completion, so the exchange stays paired on screen the way a caption
23
23
  // track shows the current line, not a scrolling transcript.
24
24
  const [lastQuestion, setLastQuestion] = (0, react_1.useState)(null);
25
+ // Persistent scroll-back log: previous exchanges get archived here (see
26
+ // archiveCurrentExchange below) the instant a new one starts, so they
27
+ // stay visible — scrolled up, not gone — instead of the old behavior of
28
+ // silently overwriting `answer`/`caption` with nothing left to look back
29
+ // at once the next question began.
30
+ const [transcript, setTranscript] = (0, react_1.useState)([]);
31
+ const transcriptIdRef = (0, react_1.useRef)(0);
32
+ const panelRef = (0, react_1.useRef)(null);
33
+ // Mirror the values archiveCurrentExchange needs to read from inside
34
+ // stale closures (the realtime WebSocket's onmessage handler is created
35
+ // once per call and doesn't see later renders' state directly — same
36
+ // reason the rest of the realtime path already uses refs like
37
+ // rtStateRef instead of reading state).
38
+ const userCaptionRef = (0, react_1.useRef)("");
39
+ const answerRef = (0, react_1.useRef)(null);
25
40
  const [rtMicMuted, setRtMicMuted] = (0, react_1.useState)(false);
26
41
  const [rtSpeakerMuted, setRtSpeakerMuted] = (0, react_1.useState)(false);
27
42
  // Set while a "tour" verb's steps are being narrated/highlighted one at a
@@ -99,6 +114,37 @@ function Copilot({ endpoint = "/api/copilot", registeredActions = [], onDo, repo
99
114
  // slot falls back to the last typed question.
100
115
  const tourChip = touring ? caption : "";
101
116
  const userCaption = !touring && (recording || realtimeActive) ? caption : lastQuestion ?? "";
117
+ (0, react_1.useEffect)(() => {
118
+ userCaptionRef.current = userCaption;
119
+ }, [userCaption]);
120
+ (0, react_1.useEffect)(() => {
121
+ answerRef.current = answer;
122
+ }, [answer]);
123
+ // Auto-scroll to the newest content whenever the transcript grows or the
124
+ // live (not-yet-archived) bubble's text changes.
125
+ (0, react_1.useEffect)(() => {
126
+ const el = panelRef.current;
127
+ if (el)
128
+ el.scrollTop = el.scrollHeight;
129
+ }, [transcript, answer, userCaption, busy]);
130
+ /**
131
+ * Moves whatever's currently showing as the "live" exchange into the
132
+ * permanent transcript log, right before it's about to be overwritten by
133
+ * a new turn — called at the start of ask(), at the start of each
134
+ * realtime "final" transcript, and at the start of a tour/tour step. Its
135
+ * effect is exactly "previous goes up, recent shows": the outgoing
136
+ * text becomes a fixed history entry the instant the incoming one starts
137
+ * replacing it, instead of just vanishing.
138
+ */
139
+ function archiveText(role, text) {
140
+ if (!text)
141
+ return;
142
+ setTranscript((prev) => [...prev, { id: transcriptIdRef.current++, role, text }]);
143
+ }
144
+ function archiveCurrentExchange() {
145
+ archiveText("user", userCaptionRef.current);
146
+ archiveText("agent", answerRef.current ?? "");
147
+ }
102
148
  function setRtStatus(next) {
103
149
  rtStateRef.current = next;
104
150
  setStatus(next);
@@ -147,6 +193,7 @@ function Copilot({ endpoint = "/api/copilot", registeredActions = [], onDo, repo
147
193
  touringRef.current = true;
148
194
  if (wasRealtimeListening)
149
195
  setRtStatus("rt-speaking");
196
+ archiveCurrentExchange();
150
197
  setAnswer(null);
151
198
  // Tracked locally rather than reading the component's `pathname` —
152
199
  // that's only current as of this render, and a step below can navigate
@@ -159,6 +206,10 @@ function Copilot({ endpoint = "/api/copilot", registeredActions = [], onDo, repo
159
206
  if (tourGenerationRef.current !== myGeneration)
160
207
  return; // superseded — e.g. widget closed or a new question came in
161
208
  const step = steps[i];
209
+ // Move the previous step's narration into history before this one replaces it —
210
+ // agent-only, since the tour's triggering question was already archived once, above.
211
+ if (i > 0)
212
+ archiveText("agent", answerRef.current ?? "");
162
213
  setTourStep({ index: i, total: steps.length });
163
214
  setCaption(`Step ${i + 1} of ${steps.length}`);
164
215
  setAnswer(step.text);
@@ -214,6 +265,7 @@ function Copilot({ endpoint = "/api/copilot", registeredActions = [], onDo, repo
214
265
  // Typed / push-to-talk question flow
215
266
  // ---------------------------------------------------------------------
216
267
  async function ask(q) {
268
+ archiveCurrentExchange();
217
269
  setStatus("asking");
218
270
  setAnswer(null);
219
271
  setLastQuestion(q);
@@ -410,6 +462,7 @@ function Copilot({ endpoint = "/api/copilot", registeredActions = [], onDo, repo
410
462
  if (!realtimeUrl || !micSupported || realtimeActive || rtStartingRef.current)
411
463
  return;
412
464
  rtStartingRef.current = true;
465
+ archiveCurrentExchange(); // preserve whatever typed/mic exchange preceded switching into a live call
413
466
  setAnswer(null);
414
467
  setCaption("");
415
468
  setRtStatus("rt-connecting");
@@ -553,6 +606,7 @@ function Copilot({ endpoint = "/api/copilot", registeredActions = [], onDo, repo
553
606
  setCaption(msg.text);
554
607
  }
555
608
  else if (msg.type === "final") {
609
+ archiveCurrentExchange(); // the previous turn's pair is complete — move it into history before this one starts overwriting caption/answer
556
610
  setCaption(msg.text);
557
611
  setRtStatus("rt-thinking");
558
612
  armThinkingWatchdog();
@@ -683,7 +737,7 @@ function Copilot({ endpoint = "/api/copilot", registeredActions = [], onDo, repo
683
737
  "rt-thinking": "Thinking…",
684
738
  "rt-speaking": "Speaking…",
685
739
  };
686
- return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("style", { suppressHydrationWarning: true, dangerouslySetInnerHTML: { __html: COPILOT_STYLES } }), (0, jsx_runtime_1.jsx)("button", { className: status === "rt-speaking" ? "cairn-fab cairn-fab-speaking" : "cairn-fab", "aria-label": open ? `Close ${persona} help` : `Open ${persona} help`, onClick: () => setOpen((v) => !v), children: open ? (0, jsx_runtime_1.jsx)(lucide_react_1.X, { size: 22 }) : (0, jsx_runtime_1.jsx)(CairnMark, {}) }), open && ((0, jsx_runtime_1.jsxs)("div", { className: "cairn-panel", role: "dialog", "aria-label": `${persona} help panel`, children: [(userCaption || answer || busy) && ((0, jsx_runtime_1.jsxs)("div", { className: "cairn-stack", children: [userCaption && ((0, jsx_runtime_1.jsx)("div", { className: "cairn-bubble cairn-bubble-user", children: userCaption }, `u-${userCaption}`)), (answer || busy) && ((0, jsx_runtime_1.jsxs)("div", { className: "cairn-bubble cairn-bubble-agent", children: [tourChip && (0, jsx_runtime_1.jsx)("span", { className: "cairn-chip", children: tourChip }), answer ? ((0, jsx_runtime_1.jsx)("span", { className: "cairn-bubble-text", children: renderCaptionWords(answer) })) : ((0, jsx_runtime_1.jsxs)("span", { className: "cairn-thinking", "aria-label": "Thinking", children: [(0, jsx_runtime_1.jsx)("span", { className: "cairn-thinking-dot" }), (0, jsx_runtime_1.jsx)("span", { className: "cairn-thinking-dot" }), (0, jsx_runtime_1.jsx)("span", { className: "cairn-thinking-dot" })] }))] }, `a-${answer ?? status}`))] })), realtimeActive ? ((0, jsx_runtime_1.jsxs)("div", { className: "cairn-rt-bar", children: [(0, jsx_runtime_1.jsx)("span", { className: `cairn-rt-dot cairn-rt-dot-${status}` }), (0, jsx_runtime_1.jsx)("span", { className: "cairn-rt-label", children: statusLabel[status] }), (0, jsx_runtime_1.jsxs)("div", { className: "cairn-rt-controls", children: [(0, jsx_runtime_1.jsx)("button", { type: "button", className: status === "rt-speaking" ? "cairn-icon-btn cairn-icon-btn-speaking" : "cairn-icon-btn", "aria-label": rtMicMuted ? "Unmute microphone" : "Mute microphone", onClick: toggleRtMic, children: rtMicMuted ? (0, jsx_runtime_1.jsx)(lucide_react_1.MicOff, { size: 16 }) : (0, jsx_runtime_1.jsx)(lucide_react_1.Mic, { size: 16 }) }), (0, jsx_runtime_1.jsx)("button", { type: "button", className: status === "rt-speaking" ? "cairn-icon-btn cairn-icon-btn-speaking" : "cairn-icon-btn", "aria-label": rtSpeakerMuted ? "Unmute speaker" : "Mute speaker", onClick: toggleRtSpeaker, children: rtSpeakerMuted ? (0, jsx_runtime_1.jsx)(lucide_react_1.VolumeX, { size: 16 }) : (0, jsx_runtime_1.jsx)(lucide_react_1.Volume2, { size: 16 }) }), (0, jsx_runtime_1.jsx)("button", { type: "button", className: "cairn-icon-btn cairn-icon-btn-end", "aria-label": "End conversation", onClick: endRealtime, children: (0, jsx_runtime_1.jsx)(lucide_react_1.PhoneOff, { size: 16 }) })] })] })) : ((0, jsx_runtime_1.jsx)("form", { onSubmit: (e) => {
740
+ return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("style", { suppressHydrationWarning: true, dangerouslySetInnerHTML: { __html: COPILOT_STYLES } }), (0, jsx_runtime_1.jsx)("button", { className: status === "rt-speaking" ? "cairn-fab cairn-fab-speaking" : "cairn-fab", "aria-label": open ? `Close ${persona} help` : `Open ${persona} help`, onClick: () => setOpen((v) => !v), children: open ? (0, jsx_runtime_1.jsx)(lucide_react_1.X, { size: 22 }) : (0, jsx_runtime_1.jsx)(CairnMark, {}) }), open && ((0, jsx_runtime_1.jsxs)("div", { className: "cairn-panel", role: "dialog", "aria-label": `${persona} help panel`, ref: panelRef, children: [(transcript.length > 0 || userCaption || answer || busy) && ((0, jsx_runtime_1.jsxs)("div", { className: "cairn-stack", children: [transcript.map((entry) => ((0, jsx_runtime_1.jsx)("div", { className: entry.role === "user" ? "cairn-bubble cairn-bubble-user cairn-bubble-past" : "cairn-bubble cairn-bubble-agent cairn-bubble-past", children: entry.role === "agent" ? (0, jsx_runtime_1.jsx)("span", { className: "cairn-bubble-text", children: entry.text }) : entry.text }, entry.id))), userCaption && ((0, jsx_runtime_1.jsx)("div", { className: "cairn-bubble cairn-bubble-user", children: userCaption }, `u-${userCaption}`)), (answer || busy) && ((0, jsx_runtime_1.jsxs)("div", { className: "cairn-bubble cairn-bubble-agent", children: [tourChip && (0, jsx_runtime_1.jsx)("span", { className: "cairn-chip", children: tourChip }), answer ? ((0, jsx_runtime_1.jsx)("span", { className: "cairn-bubble-text", children: renderCaptionWords(answer) })) : ((0, jsx_runtime_1.jsxs)("span", { className: "cairn-thinking", "aria-label": "Thinking", children: [(0, jsx_runtime_1.jsx)("span", { className: "cairn-thinking-dot" }), (0, jsx_runtime_1.jsx)("span", { className: "cairn-thinking-dot" }), (0, jsx_runtime_1.jsx)("span", { className: "cairn-thinking-dot" })] }))] }, `a-${answer ?? status}`))] })), realtimeActive ? ((0, jsx_runtime_1.jsxs)("div", { className: "cairn-rt-bar", children: [(0, jsx_runtime_1.jsx)("span", { className: `cairn-rt-dot cairn-rt-dot-${status}` }), (0, jsx_runtime_1.jsx)("span", { className: "cairn-rt-label", children: statusLabel[status] }), (0, jsx_runtime_1.jsxs)("div", { className: "cairn-rt-controls", children: [(0, jsx_runtime_1.jsx)("button", { type: "button", className: status === "rt-speaking" ? "cairn-icon-btn cairn-icon-btn-speaking" : "cairn-icon-btn", "aria-label": rtMicMuted ? "Unmute microphone" : "Mute microphone", onClick: toggleRtMic, children: rtMicMuted ? (0, jsx_runtime_1.jsx)(lucide_react_1.MicOff, { size: 16 }) : (0, jsx_runtime_1.jsx)(lucide_react_1.Mic, { size: 16 }) }), (0, jsx_runtime_1.jsx)("button", { type: "button", className: status === "rt-speaking" ? "cairn-icon-btn cairn-icon-btn-speaking" : "cairn-icon-btn", "aria-label": rtSpeakerMuted ? "Unmute speaker" : "Mute speaker", onClick: toggleRtSpeaker, children: rtSpeakerMuted ? (0, jsx_runtime_1.jsx)(lucide_react_1.VolumeX, { size: 16 }) : (0, jsx_runtime_1.jsx)(lucide_react_1.Volume2, { size: 16 }) }), (0, jsx_runtime_1.jsx)("button", { type: "button", className: "cairn-icon-btn cairn-icon-btn-end", "aria-label": "End conversation", onClick: endRealtime, children: (0, jsx_runtime_1.jsx)(lucide_react_1.PhoneOff, { size: 16 }) })] })] })) : ((0, jsx_runtime_1.jsx)("form", { onSubmit: (e) => {
687
741
  e.preventDefault();
688
742
  const trimmed = question.trim();
689
743
  if (trimmed)
@@ -920,6 +974,9 @@ const COPILOT_STYLES = `
920
974
  .cairn-bubble-text {
921
975
  white-space: pre-wrap;
922
976
  }
977
+ .cairn-bubble-past {
978
+ opacity: 0.55;
979
+ }
923
980
  .cairn-word {
924
981
  display: inline-block;
925
982
  animation: cairn-word-sweep 0.4s ease forwards;
@@ -38,6 +38,42 @@ function parseWithFlag(argv) {
38
38
  const idx = argv.indexOf("--with");
39
39
  return idx === -1 ? undefined : argv[idx + 1];
40
40
  }
41
+ /**
42
+ * Real bug this closes: `--with "next dev"` makes this the *first* process
43
+ * `npm run dev` spawns, a sibling of Next.js, not code running inside it —
44
+ * so it never gets Next's own automatic .env/.env.local loading, and a real
45
+ * key sitting in .env was invisible to it (`process.env.DEEPGRAM_API_KEY`
46
+ * genuinely undefined) even though the exact same key worked fine for
47
+ * Next.js's own API routes a moment later. No new dependency for this —
48
+ * .env is a plain KEY=VALUE format. .env.local loads first (checked second
49
+ * in this list, but a key already set never gets overwritten below) to
50
+ * match Next.js's own precedence; nothing here ever overrides a real,
51
+ * already-set process.env value (a shell export, or a platform like Vercel
52
+ * injecting its own configured env vars) — file contents only ever fill a
53
+ * gap, never win over something actually set.
54
+ */
55
+ function loadDotEnv() {
56
+ for (const filename of [".env.local", ".env"]) {
57
+ const filePath = node_path_1.default.join(process.cwd(), filename);
58
+ if (!node_fs_1.default.existsSync(filePath))
59
+ continue;
60
+ for (const line of node_fs_1.default.readFileSync(filePath, "utf8").split("\n")) {
61
+ const trimmed = line.trim();
62
+ if (!trimmed || trimmed.startsWith("#"))
63
+ continue;
64
+ const eq = trimmed.indexOf("=");
65
+ if (eq === -1)
66
+ continue;
67
+ const key = trimmed.slice(0, eq).trim();
68
+ let value = trimmed.slice(eq + 1).trim();
69
+ if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) {
70
+ value = value.slice(1, -1);
71
+ }
72
+ if (process.env[key] === undefined)
73
+ process.env[key] = value;
74
+ }
75
+ }
76
+ }
41
77
  function spawnCompanion(command) {
42
78
  const child = (0, node_child_process_1.spawn)(command, { shell: true, stdio: "inherit" });
43
79
  const shutdown = (code) => {
@@ -50,6 +86,7 @@ function spawnCompanion(command) {
50
86
  process.on("SIGTERM", () => shutdown(0));
51
87
  }
52
88
  function main() {
89
+ loadDotEnv();
53
90
  const withCommand = parseWithFlag(process.argv.slice(2));
54
91
  // With --with, this process's real job is running the companion command
55
92
  // (`next dev`, typically) — a missing key or an unbuilt manifest should
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cairnvibe/sdk",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "description": "In-app AI copilot — <Copilot/> for React/Next.js, <cairn-widget> for any framework — plus the server handlers and realtime voice relay behind them.",
5
5
  "license": "MIT",
6
6
  "publishConfig": { "access": "public" },
package/src/index.tsx CHANGED
@@ -74,6 +74,21 @@ export function Copilot({
74
74
  // completion, so the exchange stays paired on screen the way a caption
75
75
  // track shows the current line, not a scrolling transcript.
76
76
  const [lastQuestion, setLastQuestion] = useState<string | null>(null);
77
+ // Persistent scroll-back log: previous exchanges get archived here (see
78
+ // archiveCurrentExchange below) the instant a new one starts, so they
79
+ // stay visible — scrolled up, not gone — instead of the old behavior of
80
+ // silently overwriting `answer`/`caption` with nothing left to look back
81
+ // at once the next question began.
82
+ const [transcript, setTranscript] = useState<{ id: number; role: "user" | "agent"; text: string }[]>([]);
83
+ const transcriptIdRef = useRef(0);
84
+ const panelRef = useRef<HTMLDivElement | null>(null);
85
+ // Mirror the values archiveCurrentExchange needs to read from inside
86
+ // stale closures (the realtime WebSocket's onmessage handler is created
87
+ // once per call and doesn't see later renders' state directly — same
88
+ // reason the rest of the realtime path already uses refs like
89
+ // rtStateRef instead of reading state).
90
+ const userCaptionRef = useRef<string>("");
91
+ const answerRef = useRef<string | null>(null);
77
92
  const [rtMicMuted, setRtMicMuted] = useState(false);
78
93
  const [rtSpeakerMuted, setRtSpeakerMuted] = useState(false);
79
94
  // Set while a "tour" verb's steps are being narrated/highlighted one at a
@@ -157,6 +172,39 @@ export function Copilot({
157
172
  const tourChip = touring ? caption : "";
158
173
  const userCaption = !touring && (recording || realtimeActive) ? caption : lastQuestion ?? "";
159
174
 
175
+ useEffect(() => {
176
+ userCaptionRef.current = userCaption;
177
+ }, [userCaption]);
178
+ useEffect(() => {
179
+ answerRef.current = answer;
180
+ }, [answer]);
181
+
182
+ // Auto-scroll to the newest content whenever the transcript grows or the
183
+ // live (not-yet-archived) bubble's text changes.
184
+ useEffect(() => {
185
+ const el = panelRef.current;
186
+ if (el) el.scrollTop = el.scrollHeight;
187
+ }, [transcript, answer, userCaption, busy]);
188
+
189
+ /**
190
+ * Moves whatever's currently showing as the "live" exchange into the
191
+ * permanent transcript log, right before it's about to be overwritten by
192
+ * a new turn — called at the start of ask(), at the start of each
193
+ * realtime "final" transcript, and at the start of a tour/tour step. Its
194
+ * effect is exactly "previous goes up, recent shows": the outgoing
195
+ * text becomes a fixed history entry the instant the incoming one starts
196
+ * replacing it, instead of just vanishing.
197
+ */
198
+ function archiveText(role: "user" | "agent", text: string) {
199
+ if (!text) return;
200
+ setTranscript((prev) => [...prev, { id: transcriptIdRef.current++, role, text }]);
201
+ }
202
+
203
+ function archiveCurrentExchange() {
204
+ archiveText("user", userCaptionRef.current);
205
+ archiveText("agent", answerRef.current ?? "");
206
+ }
207
+
160
208
  function setRtStatus(next: Status) {
161
209
  rtStateRef.current = next;
162
210
  setStatus(next);
@@ -206,6 +254,7 @@ export function Copilot({
206
254
  const wasRealtimeListening = realtimeActive;
207
255
  touringRef.current = true;
208
256
  if (wasRealtimeListening) setRtStatus("rt-speaking");
257
+ archiveCurrentExchange();
209
258
  setAnswer(null);
210
259
  // Tracked locally rather than reading the component's `pathname` —
211
260
  // that's only current as of this render, and a step below can navigate
@@ -218,6 +267,9 @@ export function Copilot({
218
267
  for (let i = 0; i < steps.length; i++) {
219
268
  if (tourGenerationRef.current !== myGeneration) return; // superseded — e.g. widget closed or a new question came in
220
269
  const step = steps[i];
270
+ // Move the previous step's narration into history before this one replaces it —
271
+ // agent-only, since the tour's triggering question was already archived once, above.
272
+ if (i > 0) archiveText("agent", answerRef.current ?? "");
221
273
  setTourStep({ index: i, total: steps.length });
222
274
  setCaption(`Step ${i + 1} of ${steps.length}`);
223
275
  setAnswer(step.text);
@@ -269,6 +321,7 @@ export function Copilot({
269
321
  // ---------------------------------------------------------------------
270
322
 
271
323
  async function ask(q: string) {
324
+ archiveCurrentExchange();
272
325
  setStatus("asking");
273
326
  setAnswer(null);
274
327
  setLastQuestion(q);
@@ -453,6 +506,7 @@ export function Copilot({
453
506
  // which is exactly what "hearing the agent twice, in parallel" was.
454
507
  if (!realtimeUrl || !micSupported || realtimeActive || rtStartingRef.current) return;
455
508
  rtStartingRef.current = true;
509
+ archiveCurrentExchange(); // preserve whatever typed/mic exchange preceded switching into a live call
456
510
  setAnswer(null);
457
511
  setCaption("");
458
512
  setRtStatus("rt-connecting");
@@ -600,6 +654,7 @@ export function Copilot({
600
654
  if (msg.type === "interim") {
601
655
  setCaption(msg.text);
602
656
  } else if (msg.type === "final") {
657
+ archiveCurrentExchange(); // the previous turn's pair is complete — move it into history before this one starts overwriting caption/answer
603
658
  setCaption(msg.text);
604
659
  setRtStatus("rt-thinking");
605
660
  armThinkingWatchdog();
@@ -743,10 +798,18 @@ export function Copilot({
743
798
  {open ? <X size={22} /> : <CairnMark />}
744
799
  </button>
745
800
  {open && (
746
- <div className="cairn-panel" role="dialog" aria-label={`${persona} help panel`}>
801
+ <div className="cairn-panel" role="dialog" aria-label={`${persona} help panel`} ref={panelRef}>
747
802
 
748
- {(userCaption || answer || busy) && (
803
+ {(transcript.length > 0 || userCaption || answer || busy) && (
749
804
  <div className="cairn-stack">
805
+ {transcript.map((entry) => (
806
+ <div
807
+ className={entry.role === "user" ? "cairn-bubble cairn-bubble-user cairn-bubble-past" : "cairn-bubble cairn-bubble-agent cairn-bubble-past"}
808
+ key={entry.id}
809
+ >
810
+ {entry.role === "agent" ? <span className="cairn-bubble-text">{entry.text}</span> : entry.text}
811
+ </div>
812
+ ))}
750
813
  {userCaption && (
751
814
  <div className="cairn-bubble cairn-bubble-user" key={`u-${userCaption}`}>
752
815
  {userCaption}
@@ -1101,6 +1164,9 @@ const COPILOT_STYLES = `
1101
1164
  .cairn-bubble-text {
1102
1165
  white-space: pre-wrap;
1103
1166
  }
1167
+ .cairn-bubble-past {
1168
+ opacity: 0.55;
1169
+ }
1104
1170
  .cairn-word {
1105
1171
  display: inline-block;
1106
1172
  animation: cairn-word-sweep 0.4s ease forwards;
@@ -36,6 +36,39 @@ function parseWithFlag(argv: string[]): string | undefined {
36
36
  return idx === -1 ? undefined : argv[idx + 1];
37
37
  }
38
38
 
39
+ /**
40
+ * Real bug this closes: `--with "next dev"` makes this the *first* process
41
+ * `npm run dev` spawns, a sibling of Next.js, not code running inside it —
42
+ * so it never gets Next's own automatic .env/.env.local loading, and a real
43
+ * key sitting in .env was invisible to it (`process.env.DEEPGRAM_API_KEY`
44
+ * genuinely undefined) even though the exact same key worked fine for
45
+ * Next.js's own API routes a moment later. No new dependency for this —
46
+ * .env is a plain KEY=VALUE format. .env.local loads first (checked second
47
+ * in this list, but a key already set never gets overwritten below) to
48
+ * match Next.js's own precedence; nothing here ever overrides a real,
49
+ * already-set process.env value (a shell export, or a platform like Vercel
50
+ * injecting its own configured env vars) — file contents only ever fill a
51
+ * gap, never win over something actually set.
52
+ */
53
+ function loadDotEnv(): void {
54
+ for (const filename of [".env.local", ".env"]) {
55
+ const filePath = path.join(process.cwd(), filename);
56
+ if (!fs.existsSync(filePath)) continue;
57
+ for (const line of fs.readFileSync(filePath, "utf8").split("\n")) {
58
+ const trimmed = line.trim();
59
+ if (!trimmed || trimmed.startsWith("#")) continue;
60
+ const eq = trimmed.indexOf("=");
61
+ if (eq === -1) continue;
62
+ const key = trimmed.slice(0, eq).trim();
63
+ let value = trimmed.slice(eq + 1).trim();
64
+ if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) {
65
+ value = value.slice(1, -1);
66
+ }
67
+ if (process.env[key] === undefined) process.env[key] = value;
68
+ }
69
+ }
70
+ }
71
+
39
72
  function spawnCompanion(command: string): void {
40
73
  const child = spawn(command, { shell: true, stdio: "inherit" });
41
74
  const shutdown = (code: number | null) => {
@@ -49,6 +82,7 @@ function spawnCompanion(command: string): void {
49
82
  }
50
83
 
51
84
  function main(): void {
85
+ loadDotEnv();
52
86
  const withCommand = parseWithFlag(process.argv.slice(2));
53
87
 
54
88
  // With --with, this process's real job is running the companion command