@pyai/sdk 0.13.0 → 0.14.0
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 +34 -4
- package/dist/cli-omni-template.d.ts +2 -2
- package/dist/cli-omni-template.js +2 -2
- package/dist/cli-web-auth.d.ts +2 -0
- package/dist/cli-web-auth.js +2 -0
- package/dist/dialer/appServer.d.ts +2 -0
- package/dist/dialer/appServer.js +65 -2
- package/dist/dialer/dialingQueue.d.ts +33 -10
- package/dist/dialer/dialingQueue.js +121 -40
- package/dist/dialer/fileMeetingIntelligenceJournal.d.ts +14 -0
- package/dist/dialer/fileMeetingIntelligenceJournal.js +147 -0
- package/dist/dialer/index.d.ts +1 -0
- package/dist/dialer/index.js +1 -0
- package/dist/dialer/indexedDbJournal.d.ts +9 -0
- package/dist/dialer/indexedDbJournal.js +44 -2
- package/dist/dialer/managedDialer.d.ts +14 -1
- package/dist/dialer/managedDialer.js +25 -7
- package/dist/dialer/meetingIntelligence.d.ts +28 -0
- package/dist/dialer/meetingIntelligence.js +3 -0
- package/dist/dialer/meetingIntelligenceElement.d.ts +21 -0
- package/dist/dialer/meetingIntelligenceElement.js +112 -0
- package/dist/dialer/serverMeetingIntelligence.d.ts +60 -0
- package/dist/dialer/serverMeetingIntelligence.js +207 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +18 -0
- package/package.json +9 -1
- package/src/cli-omni-template.ts +2 -2
- package/src/cli-web-auth.ts +3 -0
- package/src/dialer/MEETING_INTELLIGENCE.md +76 -0
- package/src/dialer/app/app.html +2 -2
- package/src/dialer/app/app.js +39 -12
- package/src/dialer/appServer.ts +45 -3
- package/src/dialer/dialingQueue.ts +107 -46
- package/src/dialer/fileMeetingIntelligenceJournal.ts +77 -0
- package/src/dialer/index.ts +2 -0
- package/src/dialer/indexedDbJournal.ts +44 -2
- package/src/dialer/managedDialer.ts +21 -7
- package/src/dialer/meetingIntelligence.ts +21 -0
- package/src/dialer/meetingIntelligenceElement.ts +65 -0
- package/src/dialer/serverMeetingIntelligence.ts +148 -0
- package/src/index.ts +15 -0
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { DialerIntent, DialerJournal } from "./managedDialer.ts";
|
|
2
|
+
import type { DialingQueueJournal, DialingQueueState } from "./dialingQueue.ts";
|
|
2
3
|
|
|
3
4
|
/** Origin-local recovery journal. Browser capabilities never enter this store.
|
|
4
5
|
* Conflicting tabs cannot overwrite a different intent for the same scope. */
|
|
@@ -35,7 +36,7 @@ export class IndexedDbDialerJournal implements DialerJournal {
|
|
|
35
36
|
request.onsuccess = () => {
|
|
36
37
|
const prior = request.result as DialerIntent | undefined;
|
|
37
38
|
if (prior && (prior.key !== next.key || JSON.stringify(prior.input) !== JSON.stringify(next.input) ||
|
|
38
|
-
(prior.callId && prior.callId !== next.callId))) {
|
|
39
|
+
(prior.callId && prior.callId !== next.callId) || JSON.stringify(prior.queue) !== JSON.stringify(next.queue))) {
|
|
39
40
|
fail("Another call is already saved in this workspace. Recover it before dialing."); return;
|
|
40
41
|
}
|
|
41
42
|
if (prior?.stopping) next.stopping = true;
|
|
@@ -47,9 +48,11 @@ export class IndexedDbDialerJournal implements DialerJournal {
|
|
|
47
48
|
if (!next.transfer) next.transfer = structuredClone(prior.transfer);
|
|
48
49
|
else if (prior.transfer.key !== next.transfer.key || prior.transfer.to !== next.transfer.to || prior.transfer.mode !== next.transfer.mode ||
|
|
49
50
|
prior.transfer.id && next.transfer.id && prior.transfer.id !== next.transfer.id ||
|
|
50
|
-
prior.transfer.command && next.transfer.command && prior.transfer.command !== next.transfer.command
|
|
51
|
+
prior.transfer.command && next.transfer.command && prior.transfer.command !== next.transfer.command ||
|
|
52
|
+
prior.transfer.priorIds && next.transfer.priorIds && JSON.stringify(prior.transfer.priorIds) !== JSON.stringify(next.transfer.priorIds)) {
|
|
51
53
|
fail("Another transfer is saved for this call. Recover the original transfer."); return;
|
|
52
54
|
} else {
|
|
55
|
+
next.transfer.priorIds ??= prior.transfer.priorIds;
|
|
53
56
|
next.transfer.id ??= prior.transfer.id;
|
|
54
57
|
next.transfer.command ??= prior.transfer.command;
|
|
55
58
|
}
|
|
@@ -69,3 +72,42 @@ export class IndexedDbDialerJournal implements DialerJournal {
|
|
|
69
72
|
});
|
|
70
73
|
}
|
|
71
74
|
}
|
|
75
|
+
|
|
76
|
+
/** Queue progress lives separately from call capabilities. Revision checks stop
|
|
77
|
+
* stale tabs from replacing or advancing another tab's saved contact list. */
|
|
78
|
+
export class IndexedDbDialingQueueJournal implements DialingQueueJournal {
|
|
79
|
+
private readonly database: Promise<IDBDatabase>;
|
|
80
|
+
constructor(name = "pyai-dialing-queue") {
|
|
81
|
+
this.database = new Promise((resolve, reject) => {
|
|
82
|
+
const request = indexedDB.open(name, 1);
|
|
83
|
+
request.onupgradeneeded = () => { request.result.createObjectStore("queues", { keyPath: "scope" }); };
|
|
84
|
+
request.onsuccess = () => { request.result.onversionchange = () => request.result.close(); resolve(request.result); };
|
|
85
|
+
request.onerror = () => reject(new Error("Calling queue storage is unavailable."));
|
|
86
|
+
request.onblocked = () => reject(new Error("Close the older dialer tab to open queue storage."));
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
async read(scope: string): Promise<DialingQueueState | null> {
|
|
90
|
+
const db = await this.database;
|
|
91
|
+
return new Promise((resolve, reject) => {
|
|
92
|
+
const tx = db.transaction("queues", "readonly"), request = tx.objectStore("queues").get(scope);
|
|
93
|
+
tx.oncomplete = () => resolve(request.result ? structuredClone(request.result) : null);
|
|
94
|
+
tx.onerror = tx.onabort = () => reject(new Error("Calling queue storage is unavailable."));
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
async write(state: DialingQueueState, expectedRevision: number | null): Promise<void> {
|
|
98
|
+
const db = await this.database, next = structuredClone(state);
|
|
99
|
+
return new Promise((resolve, reject) => {
|
|
100
|
+
const tx = db.transaction("queues", "readwrite"), store = tx.objectStore("queues"), request = store.get(next.scope);
|
|
101
|
+
let conflict = false;
|
|
102
|
+
request.onsuccess = () => {
|
|
103
|
+
const prior = request.result as DialingQueueState | undefined;
|
|
104
|
+
if ((prior?.revision ?? null) !== expectedRevision || next.revision !== (expectedRevision ?? 0) + 1) {
|
|
105
|
+
conflict = true; tx.abort(); return;
|
|
106
|
+
}
|
|
107
|
+
store.put(next);
|
|
108
|
+
};
|
|
109
|
+
tx.oncomplete = () => resolve();
|
|
110
|
+
tx.onerror = tx.onabort = () => reject(new Error(conflict ? "The saved queue changed in another tab. Reload before continuing." : "Calling queue progress could not be saved."));
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
}
|
|
@@ -18,7 +18,8 @@ export interface DialerIntent {
|
|
|
18
18
|
input: TelephonyBrowserSessionCreate;
|
|
19
19
|
callId?: string;
|
|
20
20
|
stopping: boolean;
|
|
21
|
-
|
|
21
|
+
queue?: { key: string; contactIds: string[] };
|
|
22
|
+
transfer?: { key: string; to: string; mode: "warm" | "cold"; priorIds?: string[]; id?: string; command?: "complete" | "cancel" };
|
|
22
23
|
retiredTransferKeys?: string[];
|
|
23
24
|
}
|
|
24
25
|
export interface DialerJournal {
|
|
@@ -53,6 +54,7 @@ export class ManagedDialer {
|
|
|
53
54
|
private readonly listeners = new Set<(snapshot: DialerSnapshot) => void>();
|
|
54
55
|
private operation: Promise<void> | null = null;
|
|
55
56
|
private initialized = false;
|
|
57
|
+
private stopBeforeIntent = false;
|
|
56
58
|
private readonly scope: string;
|
|
57
59
|
private readonly broker: DialerBroker;
|
|
58
60
|
private readonly journal: DialerJournal;
|
|
@@ -68,6 +70,10 @@ export class ManagedDialer {
|
|
|
68
70
|
}
|
|
69
71
|
|
|
70
72
|
get current(): DialerSnapshot { return structuredClone(this.snapshot); }
|
|
73
|
+
get restored() { return this.initialized; }
|
|
74
|
+
get recoveryScope() { return this.scope; }
|
|
75
|
+
/** Durable queue ownership only; never contains a browser capability. */
|
|
76
|
+
get queueContext() { return this.intent?.queue ? structuredClone(this.intent.queue) : null; }
|
|
71
77
|
subscribe(listener: (snapshot: DialerSnapshot) => void): () => void {
|
|
72
78
|
this.listeners.add(listener); listener(this.current);
|
|
73
79
|
return () => { this.listeners.delete(listener); };
|
|
@@ -95,7 +101,7 @@ export class ManagedDialer {
|
|
|
95
101
|
}
|
|
96
102
|
});
|
|
97
103
|
}
|
|
98
|
-
async start(input: TelephonyBrowserSessionCreate): Promise<void> {
|
|
104
|
+
async start(input: TelephonyBrowserSessionCreate, queue?: DialerIntent["queue"]): Promise<void> {
|
|
99
105
|
if (!this.initialized) throw new Error("Restore the calling journal before dialing.");
|
|
100
106
|
if (this.intent) throw new Error("The previous call must finish and be wrapped up first.");
|
|
101
107
|
const destinations = input.destinations ?? [input.to];
|
|
@@ -103,9 +109,11 @@ export class ManagedDialer {
|
|
|
103
109
|
new Set(destinations).size !== destinations.length || destinations.some(to => typeof to !== "string" || !/^\+[1-9]\d{6,14}$/.test(to)))
|
|
104
110
|
throw new Error("Choose one destination or two to four distinct parallel destinations with country codes.");
|
|
105
111
|
return this.exclusive(async () => {
|
|
106
|
-
|
|
112
|
+
this.stopBeforeIntent = false;
|
|
113
|
+
const intent: DialerIntent = { scope: this.scope, key: this.newKey(), input: structuredClone(input), stopping: false, ...(queue ? { queue: structuredClone(queue) } : {}) };
|
|
107
114
|
// Persist before the first network request. A storage failure cannot dial.
|
|
108
115
|
await this.journal.write(structuredClone(intent)); this.intent = intent;
|
|
116
|
+
if (this.stopBeforeIntent) { intent.stopping = true; await this.journal.write(structuredClone(intent)); }
|
|
109
117
|
await this.setup();
|
|
110
118
|
});
|
|
111
119
|
}
|
|
@@ -160,7 +168,7 @@ export class ManagedDialer {
|
|
|
160
168
|
if (this.snapshot.call && call.version < this.snapshot.call.version) return;
|
|
161
169
|
const transfer = this.intent.transfer;
|
|
162
170
|
if (transfer) {
|
|
163
|
-
const observed = call.transfers?.find(item => transfer.id ? item.id === transfer.id : item.to === transfer.to && item.mode === transfer.mode);
|
|
171
|
+
const observed = call.transfers?.slice().reverse().find(item => transfer.id ? item.id === transfer.id : !transfer.priorIds?.includes(item.id) && item.to === transfer.to && item.mode === transfer.mode);
|
|
164
172
|
if (observed && observed.to === transfer.to && observed.mode === transfer.mode) {
|
|
165
173
|
const next = structuredClone(this.intent);
|
|
166
174
|
if (["canceled", "failed"].includes(observed.status)) {
|
|
@@ -181,7 +189,13 @@ export class ManagedDialer {
|
|
|
181
189
|
} else this.update({ call });
|
|
182
190
|
}
|
|
183
191
|
async stop(): Promise<void> {
|
|
184
|
-
|
|
192
|
+
// The first durable write may still be pending, before intent is installed.
|
|
193
|
+
// Preserve a close/stop request so setup cannot open late browser audio.
|
|
194
|
+
if (!this.intent) {
|
|
195
|
+
if (!this.operation) return;
|
|
196
|
+
this.stopBeforeIntent = true; await this.operation;
|
|
197
|
+
if (!this.intent) return;
|
|
198
|
+
}
|
|
185
199
|
if (this.snapshot.call && dialerCallHandedOff(this.snapshot.call)) throw new Error("The transferred conversation continues independently. Check its status instead of ending the caller's conversation.");
|
|
186
200
|
// Mark immediately so a late create acknowledgment cannot connect audio.
|
|
187
201
|
this.intent.stopping = true; this.audio.disconnect();
|
|
@@ -225,7 +239,7 @@ export class ManagedDialer {
|
|
|
225
239
|
if (this.intent.transfer || this.snapshot.call.transfers?.some(item => !["completed", "canceled", "failed"].includes(item.status)))
|
|
226
240
|
throw new Error("Recover or finish the current transfer before starting another.");
|
|
227
241
|
const next = structuredClone(this.intent);
|
|
228
|
-
next.transfer = { key: this.newKey(), to, mode };
|
|
242
|
+
next.transfer = { key: this.newKey(), to, mode, priorIds: this.snapshot.call.transfers?.map(item => item.id) ?? [] };
|
|
229
243
|
await this.journal.write(next); this.intent = next;
|
|
230
244
|
await this.transferKnown();
|
|
231
245
|
});
|
|
@@ -264,7 +278,7 @@ export class ManagedDialer {
|
|
|
264
278
|
: await broker.create(intent.callId!, { to: transfer.to, mode: transfer.mode }, transfer.key);
|
|
265
279
|
if (call.id !== intent.callId) throw new Error("Transfer call identity changed.");
|
|
266
280
|
const observed = transfer.id ? call.transfers?.find(item => item.id === transfer.id)
|
|
267
|
-
: call.transfers?.slice().reverse().find(item => item.to === transfer.to && item.mode === transfer.mode);
|
|
281
|
+
: call.transfers?.slice().reverse().find(item => !transfer.priorIds?.includes(item.id) && item.to === transfer.to && item.mode === transfer.mode);
|
|
268
282
|
if (!observed || observed.to !== transfer.to || observed.mode !== transfer.mode) throw new Error("Transfer identity is unavailable.");
|
|
269
283
|
transfer.id = observed.id;
|
|
270
284
|
await this.journal.write(structuredClone(intent));
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** Browser-safe meeting intelligence contract. All methods belong to the
|
|
2
|
+
* authenticated host; project credentials and private audio URLs stay there. */
|
|
3
|
+
export const MEETING_UPLOAD_NOTICE = "Send this recording to PyAI Hear and Recap for a transcript, notes and action items. Uploaded audio may remain for up to 7 days and job results for up to 30 days; Recap follows your workspace retention policy. Removing the meeting from this app clears cached notes and requests cancellation, not cloud erasure. The browser recording remains saved. Continue only after informing everyone and accepting this policy.";
|
|
4
|
+
export interface MeetingIntelligenceView {
|
|
5
|
+
recordingId: string;
|
|
6
|
+
state: "submitting" | "transcribing" | "summarizing" | "complete" | "failed" | "removed";
|
|
7
|
+
transcript: string | null;
|
|
8
|
+
notes: string | null;
|
|
9
|
+
actionItems: Array<{ task: string; owner?: string | null; due?: string | null }>;
|
|
10
|
+
error: string | null;
|
|
11
|
+
}
|
|
12
|
+
export interface MeetingIntelligenceAvailability { available: boolean; reason: string | null }
|
|
13
|
+
export interface MeetingIntelligenceBroker {
|
|
14
|
+
availability(): Promise<MeetingIntelligenceAvailability>;
|
|
15
|
+
/** Must be a foreground action with the upload/retention notice accepted. */
|
|
16
|
+
submit(recordingId: string, audio: Blob, noticeAccepted: boolean): Promise<MeetingIntelligenceView>;
|
|
17
|
+
get(recordingId: string): Promise<MeetingIntelligenceView>;
|
|
18
|
+
/** Clears cached notes, cancels known unfinished Hear work, and keeps an
|
|
19
|
+
* opaque tombstone. Public job cancellation does not erase cloud artifacts. */
|
|
20
|
+
remove(recordingId: string): Promise<MeetingIntelligenceView>;
|
|
21
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { MEETING_UPLOAD_NOTICE, type MeetingIntelligenceBroker, type MeetingIntelligenceView } from "./meetingIntelligence.ts";
|
|
2
|
+
import type { MeetingRecordingStore } from "./meetingRecorder.ts";
|
|
3
|
+
/** Optional companion to pyai-meeting-recorder. Bind the trusted current
|
|
4
|
+
* org/user scope and authenticated host broker; never pass a project API key. */
|
|
5
|
+
export class PyaiMeetingIntelligenceElement extends HTMLElement {
|
|
6
|
+
private root: ShadowRoot;
|
|
7
|
+
private scope = ""; private store: MeetingRecordingStore | null = null; private broker: MeetingIntelligenceBroker | null = null;
|
|
8
|
+
private generation = 0; private busy = false;
|
|
9
|
+
constructor() {
|
|
10
|
+
super(); this.root = this.attachShadow({ mode: "open" });
|
|
11
|
+
this.root.innerHTML = `<style>:host{display:block;font:14px/1.5 system-ui;color:#202332;max-width:600px}section{border:1px solid #e7e8ef;border-radius:20px;padding:24px;background:white}label{display:block;margin:12px 0}select,button{font:inherit;padding:10px;border:1px solid #ddd;border-radius:10px;margin:4px}button:disabled{opacity:.5}pre{white-space:pre-wrap;font:inherit;overflow-wrap:anywhere}.notice{font-size:12px;color:#667085}h3{margin-top:0}[role=alert]{color:#b42338}</style><section><h3>Meeting transcript & notes</h3><p data-availability role="status"></p><select aria-label="Saved meeting" data-recording></select><button data-list>Refresh recordings</button><label class="notice"><input type="checkbox" data-notice> <span data-notice-text></span></label><button data-send disabled>Send for transcription</button><button data-refresh disabled>Refresh transcript & notes</button><button data-remove disabled>Remove meeting from app</button><p data-status role="status" aria-live="polite"></p><p role="alert" data-error></p><h4>Transcript</h4><pre data-transcript></pre><h4>Notes</h4><pre data-notes></pre><h4>Action items</h4><ul data-actions></ul></section>`;
|
|
12
|
+
this.root.querySelector("[data-notice-text]")!.textContent = MEETING_UPLOAD_NOTICE;
|
|
13
|
+
this.root.querySelector("[data-list]")!.addEventListener("click", () => void this.run(() => this.refreshRecordings()));
|
|
14
|
+
this.root.querySelector("[data-notice]")!.addEventListener("change", () => this.buttons());
|
|
15
|
+
this.root.querySelector("[data-recording]")!.addEventListener("change", () => { this.clear(); this.buttons(); });
|
|
16
|
+
this.root.querySelector("[data-send]")!.addEventListener("click", () => void this.run(async () => {
|
|
17
|
+
const id = this.selected(), scope = this.scope, store = this.store!, broker = this.broker!, generation = this.generation;
|
|
18
|
+
if (!this.root.querySelector<HTMLInputElement>("[data-notice]")!.checked) throw new Error("Accept the upload and retention notice first.");
|
|
19
|
+
const recording = await store.get(scope, id);
|
|
20
|
+
if (!recording || recording.scope !== scope || recording.state !== "ready") throw new Error("Stop the recording and review any interrupted capture before submitting it through your app.");
|
|
21
|
+
const audio = await store.audio(scope, id);
|
|
22
|
+
// A workspace change cannot submit the former workspace's audio.
|
|
23
|
+
if (generation !== this.generation) return;
|
|
24
|
+
const result = await broker.submit(id, audio, true); if (generation === this.generation) this.render(result);
|
|
25
|
+
}));
|
|
26
|
+
for (const [selector, operation] of [["[data-refresh]", "get"], ["[data-remove]", "remove"]] as const) this.root.querySelector(selector)!.addEventListener("click", () => void this.run(async () => {
|
|
27
|
+
const generation = this.generation, result = await this.broker); if (generation === this.generation) this.render(result);
|
|
28
|
+
}));
|
|
29
|
+
}
|
|
30
|
+
bind(scope: string, store: MeetingRecordingStore, broker: MeetingIntelligenceBroker) {
|
|
31
|
+
if (this.busy) throw new Error("Wait for the current meeting action before switching workspaces.");
|
|
32
|
+
if (!scope.trim()) throw new Error("An authenticated workspace/user scope is required.");
|
|
33
|
+
++this.generation; this.scope = scope; this.store = store; this.broker = broker; this.clear();
|
|
34
|
+
this.root.querySelector<HTMLInputElement>("[data-notice]")!.checked = false;
|
|
35
|
+
void this.run(() => this.refreshRecordings());
|
|
36
|
+
}
|
|
37
|
+
disconnectedCallback() { ++this.generation; }
|
|
38
|
+
private selected() { const id = this.root.querySelector<HTMLSelectElement>("[data-recording]")!.value; if (!id) throw new Error("Choose a saved meeting."); return id; }
|
|
39
|
+
private clear() { for (const selector of ["[data-status]", "[data-error]", "[data-transcript]", "[data-notes]", "[data-actions]"]) this.root.querySelector(selector)!.textContent = ""; }
|
|
40
|
+
private buttons() {
|
|
41
|
+
const selected = !!this.root.querySelector<HTMLSelectElement>("[data-recording]")!.value;
|
|
42
|
+
for (const selector of ["[data-send]", "[data-refresh]", "[data-remove]"]) this.root.querySelector<HTMLButtonElement>(selector)!.disabled = this.busy || !this.broker || !selected || selector === "[data-send]" && !this.root.querySelector<HTMLInputElement>("[data-notice]")!.checked || selector === "[data-send]" && this.root.querySelector("[data-availability]")!.getAttribute("data-available") !== "true";
|
|
43
|
+
this.root.querySelector<HTMLSelectElement>("[data-recording]")!.disabled = this.busy;
|
|
44
|
+
}
|
|
45
|
+
private async refreshRecordings() {
|
|
46
|
+
if (!this.store || !this.broker) return;
|
|
47
|
+
const generation = this.generation, [records, availability] = await Promise.all([this.store.list(this.scope), this.broker.availability()]);
|
|
48
|
+
if (generation !== this.generation) return;
|
|
49
|
+
const status = this.root.querySelector("[data-availability]")!;
|
|
50
|
+
status.textContent = availability.reason ?? "Choose a saved recording, then explicitly send it for transcription."; status.setAttribute("data-available", String(availability.available));
|
|
51
|
+
const select = this.root.querySelector<HTMLSelectElement>("[data-recording]")!, previous = select.value; select.replaceChildren();
|
|
52
|
+
for (const recording of records.filter(recording => recording.state === "ready").slice(0, 40)) { const option = document.createElement("option"); option.value = recording.id; option.textContent = new Date(recording.createdAt).toLocaleString(); select.append(option); }
|
|
53
|
+
if (Array.from(select.options).some(option => option.value === previous)) select.value = previous;
|
|
54
|
+
this.buttons();
|
|
55
|
+
}
|
|
56
|
+
private render(view: MeetingIntelligenceView) {
|
|
57
|
+
this.root.querySelector("[data-status]")!.textContent = ({ submitting: "Sending recording…", transcribing: "Transcribing… Refresh to check progress.", summarizing: "Transcript ready. Preparing notes… Refresh to check progress.", complete: "Transcript and notes ready.", failed: "Meeting needs attention.", removed: "Cached notes removed. The browser recording remains saved; cloud retention still applies." })[view.state];
|
|
58
|
+
this.root.querySelector("[data-error]")!.textContent = view.error ?? "";
|
|
59
|
+
this.root.querySelector("[data-transcript]")!.textContent = view.transcript ?? ""; this.root.querySelector("[data-notes]")!.textContent = view.notes ?? "";
|
|
60
|
+
const list = this.root.querySelector("[data-actions]")!; list.replaceChildren();
|
|
61
|
+
for (const action of view.actionItems) { const item = document.createElement("li"); item.textContent = [action.task, action.owner, action.due].filter(Boolean).join(" · "); list.append(item); }
|
|
62
|
+
}
|
|
63
|
+
private async run(action: () => Promise<void>) { if (this.busy) return; this.busy = true; this.buttons(); try { await action(); } catch (error) { this.root.querySelector("[data-error]")!.textContent = error instanceof Error ? error.message : "Meeting action failed. Recover the same recording before sending another."; } finally { this.busy = false; this.buttons(); } }
|
|
64
|
+
}
|
|
65
|
+
if (!customElements.get("pyai-meeting-intelligence")) customElements.define("pyai-meeting-intelligence", PyaiMeetingIntelligenceElement);
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/** Server-only. Bind one broker to a trusted org/user and an existing project
|
|
2
|
+
* key. Never fall back to an operator/parent key or change that key's grants. */
|
|
3
|
+
import { createHash } from "node:crypto";
|
|
4
|
+
import type { MeResponse, PyAI, RecapCall, TranscriptionJob } from "../index.ts";
|
|
5
|
+
import type { MeetingIntelligenceBroker, MeetingIntelligenceView } from "./meetingIntelligence.ts";
|
|
6
|
+
|
|
7
|
+
export interface MeetingIntelligenceApi {
|
|
8
|
+
me(): Promise<MeResponse>;
|
|
9
|
+
recapEnabled(): Promise<boolean>;
|
|
10
|
+
upload(audio: Blob, filename: string, callId: string, key: string): Promise<TranscriptionJob>;
|
|
11
|
+
job(id: string): Promise<TranscriptionJob>;
|
|
12
|
+
recap(id: string): Promise<RecapCall | null>;
|
|
13
|
+
cancel(id: string): Promise<TranscriptionJob>;
|
|
14
|
+
}
|
|
15
|
+
export class MeetingApiError extends Error {
|
|
16
|
+
readonly status: number;
|
|
17
|
+
constructor(status: number) { super(`PyAI meeting request failed (${status}).`); this.status = status; }
|
|
18
|
+
}
|
|
19
|
+
/** Uses only public routes. Keyed multipart requires the durable job retry contract; no public bucket,
|
|
20
|
+
* arbitrary result_url fetch, or browser-held key is involved. */
|
|
21
|
+
export function createPyaiMeetingApi(options: { apiKey: string; baseURL?: string; fetch?: typeof fetch; timeoutMs?: number }): MeetingIntelligenceApi {
|
|
22
|
+
const base = new URL(options.baseURL ?? "https://api.pyai.com");
|
|
23
|
+
if ((base.protocol !== "https:" && !(base.protocol === "http:" && ["127.0.0.1", "[::1]"].includes(base.hostname))) || base.username || base.password || base.search || base.hash || base.pathname !== "/") throw new Error("Use a trusted HTTPS API origin.");
|
|
24
|
+
if (!options.apiKey || /[\r\n]/.test(options.apiKey)) throw new Error("An existing project key is required.");
|
|
25
|
+
const timeout = options.timeoutMs ?? 20000;
|
|
26
|
+
if (!Number.isInteger(timeout) || timeout < 1 || timeout > 60000) throw new Error("Meeting requests require a deadline of 1–60000 ms.");
|
|
27
|
+
const request = async <T>(path: string, method = "GET", input?: unknown, key?: string): Promise<T> => {
|
|
28
|
+
const response = await (options.fetch ?? fetch)(new URL(path, base), { method, redirect: "error", signal: AbortSignal.timeout(timeout),
|
|
29
|
+
headers: { Authorization: `Bearer ${options.apiKey}`, ...(input !== undefined && !(input instanceof FormData) ? { "content-type": "application/json" } : {}), ...(key ? { "Idempotency-Key": key } : {}) },
|
|
30
|
+
...(input !== undefined ? { body: input instanceof FormData ? input : JSON.stringify(input) } : {}) });
|
|
31
|
+
if (!response.ok) { await response.body?.cancel(); throw new MeetingApiError(response.status); }
|
|
32
|
+
// Avoid unbounded or attacker-selected artifact downloads. Recap supplies
|
|
33
|
+
// retained transcript text even when Hear returns only a signed result URL.
|
|
34
|
+
const reader = response.body?.getReader(); if (!reader) throw new Error("PyAI returned an empty response.");
|
|
35
|
+
const chunks: Uint8Array[] = []; let bytes = 0;
|
|
36
|
+
try { while (true) { const chunk = await reader.read(); if (chunk.done) break; bytes += chunk.value.length; if (bytes > 4 * 1024 * 1024) throw new Error("Meeting response exceeds 4 MiB."); chunks.push(chunk.value); } }
|
|
37
|
+
finally { await reader.cancel(); }
|
|
38
|
+
return JSON.parse(Buffer.concat(chunks).toString("utf8")) as T;
|
|
39
|
+
};
|
|
40
|
+
return { me: () => request("/v1/me"), recapEnabled: async () => (await request<{ enabled?: boolean }>("/v1/recap/config")).enabled === true,
|
|
41
|
+
upload: (audio, filename, callId, key) => { const form = new FormData(); form.set("call_id", callId); form.set("diarize", "true"); form.set("output_formats", "json"); form.set("audio", audio, filename); return request("/v1/transcription/jobs", "POST", form, key); },
|
|
42
|
+
job: id => request(`/v1/transcription/jobs/${encodeURIComponent(id)}`), cancel: id => request(`/v1/transcription/jobs/${encodeURIComponent(id)}`, "DELETE"),
|
|
43
|
+
recap: async id => { try { return await request(`/v1/recap/calls/${encodeURIComponent(id)}`); } catch (error) { if (error instanceof MeetingApiError && error.status === 404) return null; throw error; } } };
|
|
44
|
+
}
|
|
45
|
+
export interface MeetingIntelligenceEntry {
|
|
46
|
+
key: string; recordingId: string; owner: string; digest: string; mimeType: string; bytes: number;
|
|
47
|
+
createdAt: number; expiresAt: number; jobId: string | null;
|
|
48
|
+
state: MeetingIntelligenceView["state"]; transcript: string | null; notes: string | null;
|
|
49
|
+
actionItems: MeetingIntelligenceView["actionItems"]; error: string | null;
|
|
50
|
+
}
|
|
51
|
+
/** Host-owned persistent store. run must serialize across ALL host instances,
|
|
52
|
+
* including erase vs submit/get. Save must durably commit before resolving.
|
|
53
|
+
* Reuse the host's existing ownership journal/transaction facility. */
|
|
54
|
+
export interface MeetingIntelligenceJournal {
|
|
55
|
+
run<T>(key: string, action: () => Promise<T>): Promise<T>;
|
|
56
|
+
get(key: string): Promise<MeetingIntelligenceEntry | null>;
|
|
57
|
+
save(entry: MeetingIntelligenceEntry): Promise<void>;
|
|
58
|
+
}
|
|
59
|
+
const hash = (...values: string[]) => createHash("sha256").update(JSON.stringify(values)).digest("hex");
|
|
60
|
+
const view = (entry: MeetingIntelligenceEntry): MeetingIntelligenceView => ({ recordingId: entry.recordingId, state: entry.state, transcript: entry.transcript, notes: entry.notes, actionItems: structuredClone(entry.actionItems), error: entry.error });
|
|
61
|
+
export function createServerMeetingIntelligence(options: {
|
|
62
|
+
api: MeetingIntelligenceApi; orgId: string; projectId: string; userId: string; keyId: string;
|
|
63
|
+
journal: MeetingIntelligenceJournal;
|
|
64
|
+
now?: () => number; maxBytes?: number; retentionMs?: number;
|
|
65
|
+
}): MeetingIntelligenceBroker {
|
|
66
|
+
const { api, journal } = options, now = options.now ?? Date.now;
|
|
67
|
+
const maxBytes = options.maxBytes ?? 128 * 1024 * 1024, retention = options.retentionMs ?? 24 * 3600000;
|
|
68
|
+
if (![options.orgId, options.projectId, options.userId, options.keyId].every(value => value.trim()) || !Number.isSafeInteger(maxBytes) || maxBytes < 1 || maxBytes > 256 * 1024 * 1024 || !Number.isSafeInteger(retention) || retention < 60000 || retention > 7 * 24 * 3600000) throw new Error("Trusted identity and bounded private recording retention are required.");
|
|
69
|
+
const owner = hash(options.orgId, options.projectId, options.userId);
|
|
70
|
+
const identity = (id: string) => { if (!/^[A-Za-z0-9_-]{1,200}$/.test(id)) throw new Error("Invalid recording ID."); return "meeting_" + hash(owner, id); };
|
|
71
|
+
const owned = async (key: string) => { const entry = await journal.get(key); if (!entry || entry.owner !== owner || entry.key !== key) throw new Error("No meeting in this workspace."); return entry; };
|
|
72
|
+
const availability = async () => {
|
|
73
|
+
const me = await api.me();
|
|
74
|
+
if (me.org_id !== options.orgId || me.project_id !== options.projectId || me.key_id !== options.keyId) throw new Error("The selected PyAI project identity changed. Select the original project.");
|
|
75
|
+
const required = ["hear:transcribe", "transcribe:jobs", "recap:read", "recap:configure"];
|
|
76
|
+
const missing = required.filter(scope => !me.scopes?.includes(scope));
|
|
77
|
+
if (missing.length) return { available: false, reason: `Select a project permitted to transcribe and summarize meetings. Missing scopes: ${missing.join(", ")}. Managed calling access alone does not include this feature.` };
|
|
78
|
+
if (!await api.recapEnabled()) return { available: false, reason: "Enable Recap through your workspace's normal settings before sending meeting audio." };
|
|
79
|
+
return { available: true, reason: null };
|
|
80
|
+
};
|
|
81
|
+
const requireAvailable = async () => { const result = await availability(); if (!result.available) throw new Error(result.reason!); };
|
|
82
|
+
const refresh = async (entry: MeetingIntelligenceEntry) => {
|
|
83
|
+
if (entry.state === "removed") return view(entry);
|
|
84
|
+
if (now() >= entry.expiresAt) {
|
|
85
|
+
entry.state = "removed"; entry.transcript = null; entry.notes = null; entry.actionItems = []; entry.error = "Private meeting retention expired. PyAI results and Recap follow workspace retention.";
|
|
86
|
+
await journal.save(entry); if (entry.jobId) await api.cancel(entry.jobId); return view(entry);
|
|
87
|
+
}
|
|
88
|
+
if (!entry.jobId) return view(entry);
|
|
89
|
+
const job = await api.job(entry.jobId);
|
|
90
|
+
if (job.job_id !== entry.jobId) throw new Error("PyAI returned a different meeting job.");
|
|
91
|
+
if (["failed", "cancelled"].includes(job.status)) { entry.state = "failed"; entry.error = "Transcription did not complete. Saved audio remains available locally; review the job before starting another submission."; await journal.save(entry); return view(entry); }
|
|
92
|
+
if (job.status !== "completed") { entry.state = "transcribing"; await journal.save(entry); return view(entry); }
|
|
93
|
+
const result = job.result as { text?: unknown } | undefined;
|
|
94
|
+
if (typeof result?.text === "string") entry.transcript = result.text;
|
|
95
|
+
entry.state = "summarizing"; await journal.save(entry);
|
|
96
|
+
const recap = await api.recap(entry.key);
|
|
97
|
+
if (recap && recap.call_id !== entry.key) throw new Error("PyAI returned a different meeting summary.");
|
|
98
|
+
if (recap?.status === "failed") { entry.state = "failed"; entry.error = "The transcript is ready but Recap failed. Review the existing summary job; audio is not submitted again."; }
|
|
99
|
+
if (recap?.status === "complete") {
|
|
100
|
+
entry.transcript ??= recap.transcript?.utterances.map(item => item.text).join("\n") ?? null;
|
|
101
|
+
entry.notes = recap.record?.summary ?? recap.record?.tldr ?? null;
|
|
102
|
+
entry.actionItems = structuredClone(recap.record?.action_items ?? []); entry.state = "complete"; entry.error = null;
|
|
103
|
+
}
|
|
104
|
+
await journal.save(entry); return view(entry);
|
|
105
|
+
};
|
|
106
|
+
return { availability,
|
|
107
|
+
async submit(id, audio, noticeAccepted) {
|
|
108
|
+
if (noticeAccepted !== true) throw new Error("Accept the upload and retention notice before sending audio.");
|
|
109
|
+
if (!audio.size || audio.size > maxBytes || !/^audio\/(webm|mp4|wav|x-wav|mpeg|ogg)(?:;|$)/i.test(audio.type)) throw new Error("Choose a supported saved audio recording within the workspace size limit.");
|
|
110
|
+
const key = identity(id); await requireAvailable();
|
|
111
|
+
const digest = createHash("sha256").update(new Uint8Array(await audio.arrayBuffer())).digest("hex");
|
|
112
|
+
return journal.run(key, async () => {
|
|
113
|
+
let entry = await journal.get(key);
|
|
114
|
+
if (entry && (entry.owner !== owner || entry.digest !== digest || entry.mimeType !== audio.type || entry.bytes !== audio.size)) throw new Error("This recording ID already belongs to different audio. Recover the original submission.");
|
|
115
|
+
if (entry?.state === "removed") throw new Error("This meeting was removed. It cannot be resubmitted with the same ID.");
|
|
116
|
+
if (!entry) { entry = { key, recordingId: id, owner, digest, mimeType: audio.type, bytes: audio.size, createdAt: now(), expiresAt: now() + retention, jobId: null, state: "submitting", transcript: null, notes: null, actionItems: [], error: null }; await journal.save(entry); }
|
|
117
|
+
if (now() >= entry.expiresAt || entry.jobId) return refresh(entry);
|
|
118
|
+
// Persist the same content identity before POST. A lost response is
|
|
119
|
+
// recovered with the SAME durable JSON/multipart job retry key.
|
|
120
|
+
const extension = ({ "audio/webm": "webm", "audio/mp4": "m4a", "audio/wav": "wav", "audio/x-wav": "wav", "audio/mpeg": "mp3", "audio/ogg": "ogg" } as Record<string, string>)[audio.type.split(";")[0]!.toLowerCase()]!;
|
|
121
|
+
const job = await api.upload(audio, `meeting.${extension}`, key, key);
|
|
122
|
+
entry.jobId = job.job_id; entry.state = "transcribing"; await journal.save(entry); return refresh(entry);
|
|
123
|
+
});
|
|
124
|
+
},
|
|
125
|
+
async get(id) { const key = identity(id); await requireAvailable(); return journal.run(key, async () => refresh(await owned(key))); },
|
|
126
|
+
async remove(id) {
|
|
127
|
+
const key = identity(id); return journal.run(key, async () => {
|
|
128
|
+
const entry = await owned(key); entry.state = "removed"; entry.transcript = null; entry.notes = null; entry.actionItems = []; entry.error = "Cached notes removed. PyAI uploaded audio may remain for up to 7 days and results up to 30 days; Recap follows workspace retention. Cancellation is not erasure.";
|
|
129
|
+
await journal.save(entry);
|
|
130
|
+
// Erase never submits or replays audio. If the initial POST outcome was
|
|
131
|
+
// unknown we cannot invent its job ID or claim it was cancelled.
|
|
132
|
+
if (!entry.jobId) entry.error = "Cached notes removed. The earlier submission outcome is unknown; a PyAI job may remain under workspace retention. Uploaded audio may remain for up to 7 days and results up to 30 days.";
|
|
133
|
+
await journal.save(entry);
|
|
134
|
+
if (entry.jobId) await api.cancel(entry.jobId);
|
|
135
|
+
return view(entry);
|
|
136
|
+
});
|
|
137
|
+
},
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Reuses the host's already selected SDK instance and its normal transport.
|
|
142
|
+
* The SDK never exposes the key to the renderer. */
|
|
143
|
+
export function createSdkMeetingApi(sdk: PyAI): MeetingIntelligenceApi {
|
|
144
|
+
return { me: () => sdk.me(), recapEnabled: async () => (await sdk.recap.config.get()).enabled === true,
|
|
145
|
+
upload: (audio, filename, callId, key) => sdk.transcriptionJobs.upload({ file: audio, filename, call_id: callId, diarize: true, output_formats: ["json"] }, { idempotencyKey: key }),
|
|
146
|
+
job: id => sdk.transcriptionJobs.get(id), cancel: id => sdk.transcriptionJobs.cancel(id),
|
|
147
|
+
recap: async id => { try { return await sdk.recap.calls.get(id); } catch (error) { if ((error as { status?: number }).status === 404) return null; throw error; } } };
|
|
148
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1462,6 +1462,7 @@ export class OmniConnection {
|
|
|
1462
1462
|
|
|
1463
1463
|
/** Shape of `GET /v1/me`. Fields are best-effort / forward-compatible. */
|
|
1464
1464
|
export interface MeResponse {
|
|
1465
|
+
project_id?: string;
|
|
1465
1466
|
object?: string;
|
|
1466
1467
|
key_id?: string;
|
|
1467
1468
|
org_id?: string;
|
|
@@ -2324,6 +2325,20 @@ export class PyAI {
|
|
|
2324
2325
|
});
|
|
2325
2326
|
return (await res.json()) as TranscriptionJob;
|
|
2326
2327
|
},
|
|
2328
|
+
/** Upload a recording with a durable retry key. Requires the current
|
|
2329
|
+
* keyed multipart job contract; never change the key after an unknown ACK. */
|
|
2330
|
+
upload: async (params: { file: Blob; filename: string; call_id?: string; diarize?: boolean; output_formats?: Array<"json" | "srt" | "vtt"> }, opts: { idempotencyKey: string }): Promise<TranscriptionJob> => {
|
|
2331
|
+
if (!opts.idempotencyKey || opts.idempotencyKey.length > 255 || /[\r\n]/.test(opts.idempotencyKey)) throw new Error("A valid recording retry key is required.");
|
|
2332
|
+
const form = new FormData();
|
|
2333
|
+
if (params.call_id) form.set("call_id", params.call_id);
|
|
2334
|
+
if (params.diarize !== undefined) form.set("diarize", String(params.diarize));
|
|
2335
|
+
if (params.output_formats) form.set("output_formats", params.output_formats.join(","));
|
|
2336
|
+
form.set("audio", params.file, params.filename);
|
|
2337
|
+
const response = await this.request("/v1/transcription/jobs", { method: "POST", headers: this.authHeaders({ "Idempotency-Key": opts.idempotencyKey }), body: form });
|
|
2338
|
+
return await response.json() as TranscriptionJob;
|
|
2339
|
+
},
|
|
2340
|
+
/** Cancels queued/running work; existing cloud retention still applies. */
|
|
2341
|
+
cancel: async (jobId: string): Promise<TranscriptionJob> => { const response = await this.request(`/v1/transcription/jobs/${encodeURIComponent(jobId)}`, { method: "DELETE", headers: this.authHeaders() }); return await response.json() as TranscriptionJob; },
|
|
2327
2342
|
get: (jobId: string): Promise<TranscriptionJob> =>
|
|
2328
2343
|
this.getJson(`/v1/transcription/jobs/${encodeURIComponent(jobId)}`),
|
|
2329
2344
|
list: (params: { limit?: number; cursor?: string } = {}): Promise<ListResponse<TranscriptionJob>> => {
|