@miadi/connector 0.1.33 → 0.1.36
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/README.md +15 -2
- package/dist/approve.d.ts +7 -0
- package/dist/approve.js +54 -0
- package/dist/bin.js +8 -0
- package/dist/captions.d.ts +66 -2
- package/dist/captions.js +111 -57
- package/dist/local-transcription.d.ts +21 -0
- package/dist/local-transcription.js +111 -0
- package/dist/reading/contract.d.ts +1 -1
- package/dist/reading/host-runtime.d.ts +7 -0
- package/dist/reading/host-runtime.js +61 -0
- package/dist/reading/runtime-report.d.ts +21 -0
- package/dist/reading/runtime-report.js +40 -0
- package/dist/records.d.ts +1 -1
- package/dist/records.js +2 -0
- package/dist/review-events.d.ts +9 -0
- package/dist/review-events.js +59 -0
- package/dist/server.d.ts +5 -1
- package/dist/server.js +5 -0
- package/dist/tools.js +135 -10
- package/dist/transcript-intake.d.ts +24 -0
- package/dist/transcript-intake.js +121 -0
- package/dist/transcript-job.d.ts +197 -0
- package/dist/transcript-job.js +574 -0
- package/dist/transcript-routes.d.ts +14 -0
- package/dist/transcript-routes.js +92 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +3 -1
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The connector takes the `review.created` facts handed to its inbox and makes sure each review holds its
|
|
3
|
+
* transcript (jgwill/Miadi#995).
|
|
4
|
+
*
|
|
5
|
+
* The review service announces `review.created` for every review, however it was made. Miadi's ingress
|
|
6
|
+
* (app/api/factory/review-events) checks the signature, normalizes the body with `@miadi/hooks-core`,
|
|
7
|
+
* records the envelope in the event ledger and writes it to `<events dir>/inbox/connector-transcripts/`,
|
|
8
|
+
* one file per fact named by its idempotency key. This module takes those files: it reads the envelope with
|
|
9
|
+
* `reviewCreatedOf`, asks the review service for the review again, starts the review's transcript job and
|
|
10
|
+
* moves the file to `done/`, or to `rejected/` with the reason.
|
|
11
|
+
*
|
|
12
|
+
* The job is the one `ensure_review_transcript` starts (reuse, withheld source, captions, store, readback),
|
|
13
|
+
* so a review that already holds a transcript stores nothing. A file waits while the connector is down or the
|
|
14
|
+
* review service cannot be asked. The job records the fact as its authority, and its ending is announced as
|
|
15
|
+
* a fact of its own (review-events.ts).
|
|
16
|
+
*/
|
|
17
|
+
import { existsSync, mkdirSync, readdirSync, readFileSync, renameSync, statSync, writeFileSync } from "node:fs";
|
|
18
|
+
import { join } from "node:path";
|
|
19
|
+
import { reviewCreatedOf } from "@miadi/hooks-core";
|
|
20
|
+
import { videoIdOf } from "./reviews.js";
|
|
21
|
+
import { ensureTranscript } from "./transcript-job.js";
|
|
22
|
+
/** How many facts one pass starts: each fetches captions from YouTube, so a burst is spread over passes. */
|
|
23
|
+
export const INTAKE_PER_PASS = 3;
|
|
24
|
+
function move(dir, file, to, extra) {
|
|
25
|
+
mkdirSync(join(dir, to), { recursive: true });
|
|
26
|
+
const stamp = Date.now();
|
|
27
|
+
const target = join(dir, to, `${stamp}-${file}`);
|
|
28
|
+
let content = {};
|
|
29
|
+
try {
|
|
30
|
+
content = JSON.parse(readFileSync(join(dir, file), "utf8"));
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
/* an unreadable file keeps only what is said about it */
|
|
34
|
+
}
|
|
35
|
+
renameSync(join(dir, file), target);
|
|
36
|
+
try {
|
|
37
|
+
writeFileSync(target, `${JSON.stringify({ ...content, [`taken_${to}`]: { at: new Date(stamp).toISOString(), ...extra } }, null, 2)}\n`, { mode: 0o640 });
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
/* the move is what matters */
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
export async function settleTranscriptRequests(io, o) {
|
|
44
|
+
const result = { taken: [], rejected: [], kept: 0 };
|
|
45
|
+
if (!existsSync(o.dir))
|
|
46
|
+
return result;
|
|
47
|
+
const files = readdirSync(o.dir)
|
|
48
|
+
.filter((f) => f.endsWith(".json"))
|
|
49
|
+
.sort((a, b) => statSync(join(o.dir, a)).mtimeMs - statSync(join(o.dir, b)).mtimeMs);
|
|
50
|
+
const f = o.fetchImpl ?? fetch;
|
|
51
|
+
let started = 0;
|
|
52
|
+
for (const file of files) {
|
|
53
|
+
if (started >= INTAKE_PER_PASS) {
|
|
54
|
+
result.kept += 1;
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
const reject = (reason) => {
|
|
58
|
+
move(o.dir, file, "rejected", { reason });
|
|
59
|
+
result.rejected.push({ file, reason });
|
|
60
|
+
};
|
|
61
|
+
let envelope;
|
|
62
|
+
try {
|
|
63
|
+
envelope = JSON.parse(readFileSync(join(o.dir, file), "utf8"));
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
reject("The file is not JSON.");
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
const fact = reviewCreatedOf(envelope);
|
|
70
|
+
if (!fact) {
|
|
71
|
+
reject("The file is not a review.created envelope from the review service.");
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
if (file !== `${envelope.idempotency_key}.json`) {
|
|
75
|
+
reject("The file is not named by its envelope's idempotency key.");
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
// The review service is asked again: the event's word is not the review's.
|
|
79
|
+
let review;
|
|
80
|
+
try {
|
|
81
|
+
const res = await f(`${o.reviewUrl}/review/${fact.review.id}/json`, { headers: { accept: "application/json" }, signal: AbortSignal.timeout(15_000) });
|
|
82
|
+
if (res.status === 404) {
|
|
83
|
+
reject("The review service has no review with this id.");
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
if (!res.ok)
|
|
87
|
+
throw new Error(`answered ${res.status}`);
|
|
88
|
+
review = (await res.json());
|
|
89
|
+
}
|
|
90
|
+
catch (error) {
|
|
91
|
+
io.log?.({ evt: "connector.transcript", intake: file, kept: true, error: error instanceof Error ? error.message : String(error) });
|
|
92
|
+
result.kept += 1;
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
// A review row may carry its video only as an address (video_id null), as the event's builder reads it too.
|
|
96
|
+
const reviewVideo = typeof review?.video_id === "string" && review.video_id ? review.video_id : typeof review?.video_url === "string" ? videoIdOf(review.video_url) : null;
|
|
97
|
+
if (reviewVideo !== fact.review.video_id) {
|
|
98
|
+
reject(`The review's video is ${String(reviewVideo)}, not ${fact.review.video_id}.`);
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
const ensured = ensureTranscript(io, {
|
|
102
|
+
review_id: fact.review.id,
|
|
103
|
+
video_id: fact.review.video_id,
|
|
104
|
+
actor: { person_id: "system:review-service", name: "Review service", client_id: "review.created" },
|
|
105
|
+
trigger: `review.created (${fact.created_by.kind} ${fact.created_by.name})`,
|
|
106
|
+
authority: {
|
|
107
|
+
basis: "review_service_event",
|
|
108
|
+
event: envelope.canonical_event,
|
|
109
|
+
event_id: envelope.id,
|
|
110
|
+
idempotency_key: envelope.idempotency_key,
|
|
111
|
+
trace_id: envelope.trace_id ?? null,
|
|
112
|
+
created_by: fact.created_by,
|
|
113
|
+
created_at: fact.created_at,
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
started += 1;
|
|
117
|
+
move(o.dir, file, "done", { job: ensured.job.id, joined: ensured.joined });
|
|
118
|
+
result.taken.push({ review_id: fact.review.id, job: ensured.job.id, done: ensured.done });
|
|
119
|
+
}
|
|
120
|
+
return result;
|
|
121
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { type FetchCaptions, type TranscriptMethod } from "./captions.js";
|
|
2
|
+
import { type Actor, type ConnectorRecord } from "./records.js";
|
|
3
|
+
import type { Ledger } from "./store.js";
|
|
4
|
+
export type TranscriptState = "unknown" | "absent" | "acquiring" | "stored" | "failed" | "held";
|
|
5
|
+
export type StageName = "requested" | "reuse_check" | "source_check" | "acquire" | "store" | "readback";
|
|
6
|
+
export interface JobStage {
|
|
7
|
+
stage: StageName;
|
|
8
|
+
status: "started" | "completed" | "waiting" | "blocked" | "failed";
|
|
9
|
+
at: string;
|
|
10
|
+
detail?: string;
|
|
11
|
+
attempt?: number;
|
|
12
|
+
}
|
|
13
|
+
export interface JobFailure {
|
|
14
|
+
code: string;
|
|
15
|
+
message: string;
|
|
16
|
+
remedy: string;
|
|
17
|
+
/** Who can do the remedy: the host's operator, the source's owner, or no one (the job retries by itself). */
|
|
18
|
+
owner: "operator" | "source_owner" | "none";
|
|
19
|
+
retry: string;
|
|
20
|
+
}
|
|
21
|
+
export interface Readback {
|
|
22
|
+
ok: boolean;
|
|
23
|
+
sha256: string;
|
|
24
|
+
chars: number;
|
|
25
|
+
source: string | null;
|
|
26
|
+
updated_at: string | null;
|
|
27
|
+
checked_at: string;
|
|
28
|
+
}
|
|
29
|
+
export interface TranscriptJobReceipt {
|
|
30
|
+
state: TranscriptState;
|
|
31
|
+
/** The finer outcome: acquiring, waiting_for_captions, held_source_withheld, stored, already_present, failed. */
|
|
32
|
+
phase: string;
|
|
33
|
+
review_id: string;
|
|
34
|
+
video_id: string;
|
|
35
|
+
trigger: string;
|
|
36
|
+
since: string;
|
|
37
|
+
attempts: number;
|
|
38
|
+
last_attempt_at?: string;
|
|
39
|
+
next_attempt_at?: string;
|
|
40
|
+
give_up_at?: string;
|
|
41
|
+
method?: TranscriptMethod | "existing";
|
|
42
|
+
source?: string;
|
|
43
|
+
language?: string;
|
|
44
|
+
track?: string;
|
|
45
|
+
basis?: string;
|
|
46
|
+
chars?: number;
|
|
47
|
+
words?: number;
|
|
48
|
+
sha256?: string;
|
|
49
|
+
covers?: string;
|
|
50
|
+
stored_at?: string;
|
|
51
|
+
readback?: Readback;
|
|
52
|
+
failure?: JobFailure;
|
|
53
|
+
stages: JobStage[];
|
|
54
|
+
}
|
|
55
|
+
export interface ServiceAnswer {
|
|
56
|
+
/** 0: the service could not be asked (no token, or unreachable). */
|
|
57
|
+
status: number;
|
|
58
|
+
error?: string;
|
|
59
|
+
}
|
|
60
|
+
export interface ReviewTranscriptService {
|
|
61
|
+
state(reviewId: string): Promise<ServiceAnswer & {
|
|
62
|
+
has_transcript?: boolean;
|
|
63
|
+
source?: string | null;
|
|
64
|
+
updated_at?: string | null;
|
|
65
|
+
}>;
|
|
66
|
+
read(reviewId: string): Promise<ServiceAnswer & {
|
|
67
|
+
transcript?: string;
|
|
68
|
+
source?: string | null;
|
|
69
|
+
updated_at?: string | null;
|
|
70
|
+
}>;
|
|
71
|
+
store(reviewId: string, transcript: string, provenance?: Provenance): Promise<ServiceAnswer & {
|
|
72
|
+
ok: boolean;
|
|
73
|
+
}>;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Where the words came from, sent with the transcript. The review service ignores it until it keeps it
|
|
77
|
+
* (packages/review-service/prompts/261111-transcript-provenance.md); after that it refuses a wrong digest.
|
|
78
|
+
*/
|
|
79
|
+
export interface Provenance {
|
|
80
|
+
method: string;
|
|
81
|
+
language?: string;
|
|
82
|
+
track?: string;
|
|
83
|
+
covers?: string;
|
|
84
|
+
fetched_at?: string;
|
|
85
|
+
by: string;
|
|
86
|
+
sha256: string;
|
|
87
|
+
}
|
|
88
|
+
/** The review service's own transcript door, as the connector's token reaches it. */
|
|
89
|
+
export declare function reviewTranscriptService(config: {
|
|
90
|
+
reviewUrl: string;
|
|
91
|
+
reviewToken?: string;
|
|
92
|
+
}, fetchImpl?: typeof fetch): ReviewTranscriptService;
|
|
93
|
+
export interface TranscriptJobIo {
|
|
94
|
+
ledger: Ledger<ConnectorRecord>;
|
|
95
|
+
service: ReviewTranscriptService;
|
|
96
|
+
/** The video's spoken-language captions. */
|
|
97
|
+
captions?: FetchCaptions;
|
|
98
|
+
/** A transcription made on this host, tried when captions are missing and the source is released. */
|
|
99
|
+
local?: FetchCaptions;
|
|
100
|
+
now?: () => number;
|
|
101
|
+
log?: (event: Record<string, unknown>) => void;
|
|
102
|
+
/**
|
|
103
|
+
* A folder every process that runs transcript jobs on this host shares (the connector service, the worker
|
|
104
|
+
* script). An attempt holds `<lockDir>/<review id>` with its process id for as long as it runs, so two
|
|
105
|
+
* processes never run one review's job at once, however long an attempt takes. A lock whose process is
|
|
106
|
+
* gone is taken over. Without it, only this process's own attempts are known.
|
|
107
|
+
*/
|
|
108
|
+
lockDir?: string;
|
|
109
|
+
/** Told once each time a job reaches an ending (stored, held, failed), to announce it as a fact. Never fails the job. */
|
|
110
|
+
outcome?: (job: ConnectorRecord, receipt: TranscriptJobReceipt) => void | Promise<unknown>;
|
|
111
|
+
}
|
|
112
|
+
/** How long an attempt this process does not run may go without writing a stage before it counts as abandoned. */
|
|
113
|
+
export declare const STALE_MS: number;
|
|
114
|
+
export declare const jobKey: (reviewId: string) => string;
|
|
115
|
+
/** What the ledger says of a video's source: released by its owner, withheld, or never registered. */
|
|
116
|
+
export declare function sourceStatus(records: ConnectorRecord[], videoId: string): "released" | "withheld" | "unregistered";
|
|
117
|
+
export declare function jobOf(records: ConnectorRecord[], reviewId: string): ConnectorRecord | undefined;
|
|
118
|
+
export declare const receiptOf: (job: ConnectorRecord | undefined) => TranscriptJobReceipt | undefined;
|
|
119
|
+
export interface EnsureInput {
|
|
120
|
+
review_id: string;
|
|
121
|
+
video_id: string;
|
|
122
|
+
actor: Actor;
|
|
123
|
+
trigger: string;
|
|
124
|
+
refs?: string[];
|
|
125
|
+
/** What entitles this run, recorded with the job: the approved mission a worker acts under. */
|
|
126
|
+
authority?: Record<string, unknown>;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Whether an approved mission entitles a worker to acquire this review's transcript: the person with the
|
|
130
|
+
* write tier approved a run or a work proposal that names the review. The worker has no Miadi API tier of
|
|
131
|
+
* its own, so the mission is its authority, and the job records it.
|
|
132
|
+
*/
|
|
133
|
+
export declare function missionAuthority(records: ConnectorRecord[], missionId: string, reviewId: string): {
|
|
134
|
+
ok: true;
|
|
135
|
+
authority: Record<string, unknown>;
|
|
136
|
+
} | {
|
|
137
|
+
ok: false;
|
|
138
|
+
reason: string;
|
|
139
|
+
};
|
|
140
|
+
export interface Ensured {
|
|
141
|
+
job: ConnectorRecord;
|
|
142
|
+
/** A trigger that met an attempt already running in this process joins it. */
|
|
143
|
+
joined: boolean;
|
|
144
|
+
/** Settles with the job once this attempt ends (stored, waiting for a retry, held or failed). Never rejects. */
|
|
145
|
+
done: Promise<ConnectorRecord>;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* The explicit, idempotent operation: one job per review, one attempt at a time. A second trigger
|
|
149
|
+
* while an attempt runs joins it. A stored job is only confirmed against the service. A failed or
|
|
150
|
+
* held job starts a new window, because a person asking again has usually repaired the cause.
|
|
151
|
+
*/
|
|
152
|
+
export declare function ensureTranscript(io: TranscriptJobIo, input: EnsureInput): Ensured;
|
|
153
|
+
/**
|
|
154
|
+
* Jobs that need a pass: one a restart interrupted (acquiring, with no attempt running here), one whose retry time has
|
|
155
|
+
* come, and one held for a withheld source that has been released since. Called when the connector starts and every 15 minutes.
|
|
156
|
+
*/
|
|
157
|
+
export declare function settleTranscriptJobs(io: TranscriptJobIo, o?: {
|
|
158
|
+
resumeAll?: boolean;
|
|
159
|
+
}): Promise<string[]>;
|
|
160
|
+
export interface TranscriptLinks {
|
|
161
|
+
status: string;
|
|
162
|
+
review: string;
|
|
163
|
+
transcript_api: string;
|
|
164
|
+
job?: string;
|
|
165
|
+
decision: null;
|
|
166
|
+
}
|
|
167
|
+
export interface TranscriptStatus {
|
|
168
|
+
state: TranscriptState;
|
|
169
|
+
/** One plain sentence a person reads without any card. */
|
|
170
|
+
summary: string;
|
|
171
|
+
review_id: string;
|
|
172
|
+
video_id: string | null;
|
|
173
|
+
job?: {
|
|
174
|
+
id: string;
|
|
175
|
+
receipt: TranscriptJobReceipt;
|
|
176
|
+
};
|
|
177
|
+
service: {
|
|
178
|
+
reachable: boolean;
|
|
179
|
+
has_transcript?: boolean;
|
|
180
|
+
source?: string | null;
|
|
181
|
+
updated_at?: string | null;
|
|
182
|
+
error?: string;
|
|
183
|
+
};
|
|
184
|
+
/** Present only when the caller asked to verify: read back from the service now, without writing. */
|
|
185
|
+
verified_now?: Readback;
|
|
186
|
+
links: TranscriptLinks;
|
|
187
|
+
}
|
|
188
|
+
export declare function summaryOf(s: Pick<TranscriptStatus, "state" | "job" | "service" | "verified_now">): string;
|
|
189
|
+
/**
|
|
190
|
+
* Where a review's transcript stands, read from the review service and the ledger. It writes nothing:
|
|
191
|
+
* `verify` reads the transcript back from the service and hashes it, and still records nothing.
|
|
192
|
+
*/
|
|
193
|
+
export declare function transcriptStatus(io: TranscriptJobIo, input: {
|
|
194
|
+
review_id: string;
|
|
195
|
+
video_id?: string | null;
|
|
196
|
+
verify?: boolean;
|
|
197
|
+
}, links: (jobId?: string) => TranscriptLinks): Promise<TranscriptStatus>;
|