@agentproto/corpus-cli 0.9.4 → 0.10.1

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.
@@ -87,10 +87,35 @@ declare class ScrapeMcpFetcher implements FetcherPort {
87
87
  * concrete impl (OpenAI Whisper here; swap for a self-hosted whisper.cpp
88
88
  * or Deepgram impl without touching the fetcher).
89
89
  */
90
+ /** One speaker turn from a diarizing engine. `start`/`end` are seconds. */
91
+ interface Utterance {
92
+ readonly speaker: string;
93
+ readonly text: string;
94
+ readonly start?: number;
95
+ readonly end?: number;
96
+ }
90
97
  interface Transcript {
91
98
  readonly text: string;
92
99
  /** BCP-47 language, when the engine detects it. */
93
100
  readonly language?: string;
101
+ /**
102
+ * Per-speaker turns, when the engine diarizes (e.g. AssemblyAI). `text`
103
+ * above stays the flattened `Speaker A: …` rendering for backward
104
+ * compatibility — this is the structured form consumers can key off of
105
+ * (speaker counting, per-turn attribution) without re-parsing `text`.
106
+ */
107
+ readonly utterances?: ReadonlyArray<Utterance>;
108
+ /** Which STT engine produced this transcript (e.g. "assemblyai"). */
109
+ readonly engine?: string;
110
+ /**
111
+ * True when `utterances[].speaker` labels are only stable WITHIN this
112
+ * transcript and can't be compared across a different call (set by
113
+ * `ChunkedStt`, whose parts are each diarized independently). Consumers
114
+ * that count distinct speakers should treat the labels as already
115
+ * disambiguated (e.g. prefixed) rather than re-merging same-named
116
+ * speakers from different segments.
117
+ */
118
+ readonly speakerLabelsLocalToSegment?: boolean;
94
119
  }
95
120
  interface SttPort {
96
121
  transcribe(audioPath: string): Promise<Transcript>;
@@ -359,12 +359,26 @@ var YtDlpWhisperFetcher = class {
359
359
  try {
360
360
  const t = await this.stt.transcribe(dl.audioPath);
361
361
  if (!t.text.trim()) return null;
362
+ const utterances = t.utterances ?? [];
363
+ const diarized = utterances.length > 0;
364
+ const speakers = diarized ? new Set(utterances.map((u) => u.speaker)).size : 0;
362
365
  return {
363
366
  title: dl.title || url,
364
367
  text: t.text,
365
368
  kind: "video",
366
369
  ...t.language ?? dl.language ? { language: t.language ?? dl.language } : {},
367
- via: "transcription"
370
+ via: diarized ? "diarized-transcription" : "transcription",
371
+ ...diarized ? {
372
+ metadata: {
373
+ speech: {
374
+ regime: "spoken",
375
+ diarized: true,
376
+ engine: t.engine ?? "unknown",
377
+ speakers,
378
+ ...t.speakerLabelsLocalToSegment ? { segmented: true } : {}
379
+ }
380
+ }
381
+ } : {}
368
382
  };
369
383
  } catch (e) {
370
384
  if (isAuthError(e)) throw e;
@@ -524,10 +538,24 @@ var UPLOAD_RESPONSE = z.object({ upload_url: z.string() });
524
538
  var AAI_TRANSCRIPT = z.object({
525
539
  id: z.string(),
526
540
  status: z.enum(["queued", "processing", "completed", "error"]),
527
- text: z.string().optional(),
528
- language_code: z.string().optional(),
529
- error: z.string().optional(),
530
- utterances: z.array(z.object({ speaker: z.string(), text: z.string() })).nullish()
541
+ // Nullish, not optional: while a job is `queued`/`processing`
542
+ // AssemblyAI sends an explicit `null` for these, and the poll loop
543
+ // parses every intermediate response — an `.optional()` here rejected
544
+ // the very first poll of every transcript (0/64 on the Werber corpus).
545
+ text: z.string().nullish(),
546
+ language_code: z.string().nullish(),
547
+ error: z.string().nullish(),
548
+ utterances: z.array(
549
+ z.object({
550
+ speaker: z.string(),
551
+ text: z.string(),
552
+ // AssemblyAI reports these in milliseconds. Nullish, not just
553
+ // optional: an explicit `null` from the API must parse cleanly
554
+ // rather than failing the whole video's transcript.
555
+ start: z.number().nullish(),
556
+ end: z.number().nullish()
557
+ })
558
+ ).nullish()
531
559
  }).loose();
532
560
  var AssemblyAiStt = class {
533
561
  apiKey;
@@ -567,9 +595,12 @@ var AssemblyAiStt = class {
567
595
  job = await this.get(`/transcript/${id}`);
568
596
  }
569
597
  if (job.status === "error") throw new Error(`AssemblyAI transcript failed: ${job.error ?? "unknown"}`);
598
+ const utterances = toUtterances(job);
570
599
  return {
571
600
  text: formatDiarized(job),
572
- ...job.language_code ? { language: job.language_code } : {}
601
+ engine: "assemblyai",
602
+ ...job.language_code ? { language: job.language_code } : {},
603
+ ...utterances ? { utterances } : {}
573
604
  };
574
605
  }
575
606
  async post(path3, body) {
@@ -587,6 +618,15 @@ var AssemblyAiStt = class {
587
618
  return AAI_TRANSCRIPT.parse(await r.json());
588
619
  }
589
620
  };
621
+ function toUtterances(job) {
622
+ if (!job.utterances || job.utterances.length === 0) return void 0;
623
+ return job.utterances.map((u) => ({
624
+ speaker: u.speaker,
625
+ text: u.text.trim(),
626
+ ...u.start != null ? { start: u.start / 1e3 } : {},
627
+ ...u.end != null ? { end: u.end / 1e3 } : {}
628
+ }));
629
+ }
590
630
  function formatDiarized(job) {
591
631
  if (job.utterances && job.utterances.length > 0) {
592
632
  return job.utterances.map((u) => `Speaker ${u.speaker}: ${u.text.trim()}`).join("\n\n").trim();
@@ -617,17 +657,39 @@ var ChunkedStt = class {
617
657
  return this.base.transcribe(audioPath);
618
658
  }
619
659
  const texts = [];
660
+ const utterances = [];
661
+ let hasUtterances = false;
620
662
  let language;
663
+ let engine;
664
+ let offset = 0;
621
665
  let failed = 0;
622
666
  let firstErr;
623
- for (const part of parts) {
667
+ for (const [i, part] of parts.entries()) {
624
668
  try {
625
669
  const t = await this.base.transcribe(part);
626
670
  if (t.text.trim()) texts.push(t.text.trim());
627
671
  language ??= t.language;
672
+ engine ??= t.engine;
673
+ let span;
674
+ if (t.utterances && t.utterances.length > 0) {
675
+ hasUtterances = true;
676
+ const partOffset = offset;
677
+ for (const u of t.utterances) {
678
+ utterances.push({
679
+ speaker: `${u.speaker}#${i}`,
680
+ text: u.text,
681
+ ...u.start !== void 0 ? { start: u.start + partOffset } : {},
682
+ ...u.end !== void 0 ? { end: u.end + partOffset } : {}
683
+ });
684
+ }
685
+ const ends = t.utterances.map((u) => u.end).filter((e) => e !== void 0);
686
+ if (ends.length > 0) span = Math.max(...ends);
687
+ }
688
+ offset += span ?? this.segmentSeconds;
628
689
  } catch (e) {
629
690
  failed++;
630
691
  firstErr ??= e;
692
+ offset += this.segmentSeconds;
631
693
  }
632
694
  }
633
695
  if (texts.length === 0) {
@@ -641,7 +703,9 @@ var ChunkedStt = class {
641
703
  }
642
704
  return {
643
705
  text: texts.join("\n\n"),
644
- ...language ? { language } : {}
706
+ ...language ? { language } : {},
707
+ ...engine ? { engine } : {},
708
+ ...hasUtterances ? { utterances, speakerLabelsLocalToSegment: true } : {}
645
709
  };
646
710
  } finally {
647
711
  await rm(dir, { recursive: true, force: true }).catch(() => {