@pyai/sdk 0.12.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/CLI.schema.json +2 -1
- package/README.md +58 -6
- 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/cli.js +2 -2
- package/dist/dialer/appServer.d.ts +4 -0
- package/dist/dialer/appServer.js +84 -4
- package/dist/dialer/dialerElement.js +28 -6
- package/dist/dialer/dialingQueue.d.ts +33 -10
- package/dist/dialer/dialingQueue.js +124 -41
- 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 +62 -1
- package/dist/dialer/managedDialer.d.ts +40 -2
- package/dist/dialer/managedDialer.js +137 -10
- 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/serverBroker.js +19 -2
- package/dist/dialer/serverMeetingIntelligence.d.ts +60 -0
- package/dist/dialer/serverMeetingIntelligence.js +207 -0
- package/dist/index.d.ts +40 -0
- package/dist/index.js +24 -0
- package/package.json +9 -1
- package/src/cli-omni-template.ts +2 -2
- package/src/cli-web-auth.ts +3 -0
- package/src/cli.ts +2 -2
- package/src/dialer/MEETING_INTELLIGENCE.md +76 -0
- package/src/dialer/app/app.html +3 -3
- package/src/dialer/app/app.js +45 -16
- package/src/dialer/appServer.ts +63 -5
- package/src/dialer/dialerElement.ts +24 -5
- package/src/dialer/dialingQueue.ts +108 -45
- package/src/dialer/fileMeetingIntelligenceJournal.ts +77 -0
- package/src/dialer/index.ts +2 -0
- package/src/dialer/indexedDbJournal.ts +58 -1
- package/src/dialer/managedDialer.ts +115 -9
- package/src/dialer/meetingIntelligence.ts +21 -0
- package/src/dialer/meetingIntelligenceElement.ts +65 -0
- package/src/dialer/serverBroker.ts +17 -2
- package/src/dialer/serverMeetingIntelligence.ts +148 -0
- package/src/index.ts +37 -1
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/** Private local metadata journal used by the CLI and desktop companion.
|
|
2
|
+
* Audio remains in the browser store until an explicit send. */
|
|
3
|
+
import { mkdir, open, readFile, rename, rm, lstat, readdir } from "node:fs/promises";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
6
|
+
import type { MeetingIntelligenceEntry, MeetingIntelligenceJournal } from "./serverMeetingIntelligence.ts";
|
|
7
|
+
|
|
8
|
+
export class FileMeetingIntelligenceJournal implements MeetingIntelligenceJournal {
|
|
9
|
+
private readonly directory: string;
|
|
10
|
+
private initialized: Promise<void> | null = null;
|
|
11
|
+
constructor(root: string, scope: string) {
|
|
12
|
+
if (!scope.trim()) throw new Error("A private meeting scope is required.");
|
|
13
|
+
this.directory = join(root, createHash("sha256").update(scope).digest("hex"));
|
|
14
|
+
}
|
|
15
|
+
private initialize() { return this.initialized ??= (async () => {
|
|
16
|
+
await mkdir(this.directory, { recursive: true, mode: 0o700 });
|
|
17
|
+
const info = await lstat(this.directory);
|
|
18
|
+
if (!info.isDirectory() || info.isSymbolicLink() || (process.platform !== "win32" && (info.mode & 0o077)) || process.getuid && info.uid !== process.getuid()) throw new Error("Meeting journal must be a private directory owned by this user.");
|
|
19
|
+
})(); }
|
|
20
|
+
async sweep(): Promise<void> {
|
|
21
|
+
await this.initialize();
|
|
22
|
+
const files = await readdir(this.directory);
|
|
23
|
+
if (files.length > 20000) throw new Error("Meeting metadata limit reached. Clean up old local meeting entries.");
|
|
24
|
+
for (const file of files.filter(file => /^meeting_[a-f0-9]{64}\.json$/.test(file))) {
|
|
25
|
+
const entry = await this.get(file.slice(0, -5));
|
|
26
|
+
if (entry && entry.expiresAt <= Date.now() && (entry.transcript || entry.notes || entry.actionItems.length)) await this.run(entry.key, async () => {
|
|
27
|
+
const current = await this.get(entry.key); if (current && current.expiresAt <= Date.now()) await this.save({ ...current, transcript: null, notes: null, actionItems: [] });
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
private path(key: string) { if (!/^meeting_[a-f0-9]{64}$/.test(key)) throw new Error("Invalid private meeting key."); return join(this.directory, key + ".json"); }
|
|
32
|
+
private alive(pid: number) { try { process.kill(pid, 0); return true; } catch (error) { return (error as NodeJS.ErrnoException).code !== "ESRCH"; } }
|
|
33
|
+
async run<T>(key: string, action: () => Promise<T>): Promise<T> {
|
|
34
|
+
await this.initialize();
|
|
35
|
+
this.path(key); const lock = join(this.directory, key + ".lock"), token = randomUUID();
|
|
36
|
+
const deadline = Date.now() + 3000, staged = lock + "." + token + ".staged"; let acquired = false;
|
|
37
|
+
// Publish a fully written owner atomically. A crash between mkdir and an
|
|
38
|
+
// owner write must never leave an ownerless visible lease.
|
|
39
|
+
await mkdir(staged, { mode: 0o700 }); await this.write(join(staged, "owner.json"), { pid: process.pid, token });
|
|
40
|
+
try { while (!acquired) {
|
|
41
|
+
try { await rename(staged, lock); acquired = true; }
|
|
42
|
+
catch (error) {
|
|
43
|
+
if (!["EEXIST", "ENOTEMPTY", ...(process.platform === "win32" ? ["EPERM"] : [])].includes((error as NodeJS.ErrnoException).code ?? "")) throw error;
|
|
44
|
+
// Recover a crashed writer under a separate exclusive recovery gate.
|
|
45
|
+
// A competing live writer is never removed. An interrupted recovery
|
|
46
|
+
// fails closed rather than guessing about ownership.
|
|
47
|
+
try {
|
|
48
|
+
const prior = JSON.parse(await readFile(join(lock, "owner.json"), "utf8")) as { pid: number; token: string };
|
|
49
|
+
if (Number.isInteger(prior.pid) && prior.pid > 0 && !this.alive(prior.pid)) {
|
|
50
|
+
const recovery = await open(lock + ".recover", "wx", 0o600);
|
|
51
|
+
try { const current = JSON.parse(await readFile(join(lock, "owner.json"), "utf8")) as { pid: number; token: string }; if (current.token === prior.token && !this.alive(current.pid)) await rm(lock, { recursive: true, force: true }); }
|
|
52
|
+
finally { await recovery.close(); await rm(lock + ".recover", { force: true }); }
|
|
53
|
+
}
|
|
54
|
+
} catch (error) { if (!(error instanceof SyntaxError) && !["ENOENT", "EEXIST"].includes((error as NodeJS.ErrnoException).code ?? "")) throw error; }
|
|
55
|
+
if (Date.now() >= deadline) throw new Error("This meeting is being updated in another app. Wait for it to finish and recover the same recording.");
|
|
56
|
+
await new Promise(resolve => setTimeout(resolve, 25));
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return await action();
|
|
60
|
+
} finally {
|
|
61
|
+
await rm(staged, { recursive: true, force: true });
|
|
62
|
+
if (acquired) { const owner = JSON.parse(await readFile(join(lock, "owner.json"), "utf8")) as { token: string }; if (owner.token === token) await rm(lock, { recursive: true, force: true }); }
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
async get(key: string): Promise<MeetingIntelligenceEntry | null> {
|
|
66
|
+
try { const file = this.path(key), info = await lstat(file); if (!info.isFile() || info.isSymbolicLink() || info.size > 4 * 1024 * 1024 || (process.platform !== "win32" && (info.mode & 0o077))) throw new Error("Private meeting metadata is invalid."); return JSON.parse(await readFile(file, "utf8")) as MeetingIntelligenceEntry; }
|
|
67
|
+
catch (error) { if ((error as NodeJS.ErrnoException).code === "ENOENT") return null; throw error; }
|
|
68
|
+
}
|
|
69
|
+
private async write(file: string, value: unknown) { const handle = await open(file, "wx", 0o600); try { await handle.writeFile(JSON.stringify(value)); await handle.sync(); } finally { await handle.close(); } }
|
|
70
|
+
async save(entry: MeetingIntelligenceEntry): Promise<void> {
|
|
71
|
+
const file = this.path(entry.key), current = await this.get(entry.key);
|
|
72
|
+
if (current?.state === "removed" && entry.state !== "removed") throw new Error("This meeting was removed. Recover the current state.");
|
|
73
|
+
const temporary = file + "." + randomUUID() + ".tmp";
|
|
74
|
+
try { if (Buffer.byteLength(JSON.stringify(entry)) > 4 * 1024 * 1024) throw new Error("Meeting metadata exceeds the private cache limit."); await this.write(temporary, entry); await rename(temporary, file); if (process.platform !== "win32") { const directory = await open(this.directory, "r"); try { await directory.sync(); } finally { await directory.close(); } } }
|
|
75
|
+
finally { await rm(temporary, { force: true }); }
|
|
76
|
+
}
|
|
77
|
+
}
|
package/src/dialer/index.ts
CHANGED
|
@@ -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,10 +36,27 @@ 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;
|
|
43
|
+
next.retiredTransferKeys = [...new Set([...(prior?.retiredTransferKeys ?? []), ...(next.retiredTransferKeys ?? [])])];
|
|
44
|
+
if (next.transfer && next.retiredTransferKeys.includes(next.transfer.key)) {
|
|
45
|
+
fail("This transfer has already finished. Refresh the original call."); return;
|
|
46
|
+
}
|
|
47
|
+
if (prior?.transfer && !next.retiredTransferKeys.includes(prior.transfer.key)) {
|
|
48
|
+
if (!next.transfer) next.transfer = structuredClone(prior.transfer);
|
|
49
|
+
else if (prior.transfer.key !== next.transfer.key || prior.transfer.to !== next.transfer.to || prior.transfer.mode !== next.transfer.mode ||
|
|
50
|
+
prior.transfer.id && next.transfer.id && prior.transfer.id !== next.transfer.id ||
|
|
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)) {
|
|
53
|
+
fail("Another transfer is saved for this call. Recover the original transfer."); return;
|
|
54
|
+
} else {
|
|
55
|
+
next.transfer.priorIds ??= prior.transfer.priorIds;
|
|
56
|
+
next.transfer.id ??= prior.transfer.id;
|
|
57
|
+
next.transfer.command ??= prior.transfer.command;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
42
60
|
store.put(next); result(undefined);
|
|
43
61
|
};
|
|
44
62
|
});
|
|
@@ -54,3 +72,42 @@ export class IndexedDbDialerJournal implements DialerJournal {
|
|
|
54
72
|
});
|
|
55
73
|
}
|
|
56
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
|
+
}
|
|
@@ -6,6 +6,11 @@ export interface DialerBroker {
|
|
|
6
6
|
create(input: TelephonyBrowserSessionCreate, idempotencyKey: string): Promise<TelephonyBrowserSession>;
|
|
7
7
|
get(callId: string): Promise<TelephonyCall>;
|
|
8
8
|
hangup(callId: string): Promise<TelephonyCall>;
|
|
9
|
+
transfers?: {
|
|
10
|
+
create(callId: string, input: { to: string; mode: "warm" | "cold" }, key: string): Promise<TelephonyCall>;
|
|
11
|
+
complete(callId: string, transferId: string): Promise<TelephonyCall>;
|
|
12
|
+
cancel(callId: string, transferId: string): Promise<TelephonyCall>;
|
|
13
|
+
};
|
|
9
14
|
}
|
|
10
15
|
export interface DialerIntent {
|
|
11
16
|
scope: string;
|
|
@@ -13,6 +18,9 @@ export interface DialerIntent {
|
|
|
13
18
|
input: TelephonyBrowserSessionCreate;
|
|
14
19
|
callId?: string;
|
|
15
20
|
stopping: boolean;
|
|
21
|
+
queue?: { key: string; contactIds: string[] };
|
|
22
|
+
transfer?: { key: string; to: string; mode: "warm" | "cold"; priorIds?: string[]; id?: string; command?: "complete" | "cancel" };
|
|
23
|
+
retiredTransferKeys?: string[];
|
|
16
24
|
}
|
|
17
25
|
export interface DialerJournal {
|
|
18
26
|
read(scope: string): Promise<DialerIntent | null>;
|
|
@@ -26,13 +34,17 @@ export interface DialerAudio {
|
|
|
26
34
|
sendDigits(digits: string): void;
|
|
27
35
|
}
|
|
28
36
|
export interface DialerSnapshot {
|
|
29
|
-
state: "idle" | "preparing" | "ringing" | "active" | "stopping" | "unconfirmed" | "wrapup";
|
|
37
|
+
state: "idle" | "preparing" | "ringing" | "active" | "stopping" | "unconfirmed" | "handing_off" | "transferred" | "wrapup";
|
|
30
38
|
callId: string | null;
|
|
31
39
|
call: TelephonyCall | null;
|
|
32
40
|
muted: boolean;
|
|
33
41
|
error: string | null;
|
|
42
|
+
transferPending?: boolean;
|
|
34
43
|
}
|
|
35
44
|
const terminal = (call: TelephonyCall) => ["completed", "failed", "canceled"].includes(call.status);
|
|
45
|
+
/** A durable handoff keeps its original room after the browser goes away. */
|
|
46
|
+
export const dialerCallHandedOff = (call: TelephonyCall) => call.transfer_enabled === true && Boolean(call.transfers?.some(transfer =>
|
|
47
|
+
transfer.status === "completed" || transfer.mode === "cold" && ["dialing", "ready", "completing"].includes(transfer.status)));
|
|
36
48
|
|
|
37
49
|
/** One immutable workspace/user scope per instance. Setup and stop retain the
|
|
38
50
|
* original intent through lost acknowledgments, reloads and audio failures. */
|
|
@@ -42,6 +54,7 @@ export class ManagedDialer {
|
|
|
42
54
|
private readonly listeners = new Set<(snapshot: DialerSnapshot) => void>();
|
|
43
55
|
private operation: Promise<void> | null = null;
|
|
44
56
|
private initialized = false;
|
|
57
|
+
private stopBeforeIntent = false;
|
|
45
58
|
private readonly scope: string;
|
|
46
59
|
private readonly broker: DialerBroker;
|
|
47
60
|
private readonly journal: DialerJournal;
|
|
@@ -57,6 +70,10 @@ export class ManagedDialer {
|
|
|
57
70
|
}
|
|
58
71
|
|
|
59
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; }
|
|
60
77
|
subscribe(listener: (snapshot: DialerSnapshot) => void): () => void {
|
|
61
78
|
this.listeners.add(listener); listener(this.current);
|
|
62
79
|
return () => { this.listeners.delete(listener); };
|
|
@@ -78,13 +95,13 @@ export class ManagedDialer {
|
|
|
78
95
|
this.intent = saved ? structuredClone(saved) : null;
|
|
79
96
|
this.initialized = true;
|
|
80
97
|
if (saved) {
|
|
81
|
-
this.update({ state: "unconfirmed", callId: saved.callId ?? null, error: "Recover the previous call before starting another." });
|
|
98
|
+
this.update({ state: "unconfirmed", callId: saved.callId ?? null, transferPending: Boolean(saved.transfer), error: "Recover the previous call before starting another." });
|
|
82
99
|
// Reload never consumes a new browser capability automatically.
|
|
83
100
|
if (saved.callId) await this.readStatus();
|
|
84
101
|
}
|
|
85
102
|
});
|
|
86
103
|
}
|
|
87
|
-
async start(input: TelephonyBrowserSessionCreate): Promise<void> {
|
|
104
|
+
async start(input: TelephonyBrowserSessionCreate, queue?: DialerIntent["queue"]): Promise<void> {
|
|
88
105
|
if (!this.initialized) throw new Error("Restore the calling journal before dialing.");
|
|
89
106
|
if (this.intent) throw new Error("The previous call must finish and be wrapped up first.");
|
|
90
107
|
const destinations = input.destinations ?? [input.to];
|
|
@@ -92,9 +109,11 @@ export class ManagedDialer {
|
|
|
92
109
|
new Set(destinations).size !== destinations.length || destinations.some(to => typeof to !== "string" || !/^\+[1-9]\d{6,14}$/.test(to)))
|
|
93
110
|
throw new Error("Choose one destination or two to four distinct parallel destinations with country codes.");
|
|
94
111
|
return this.exclusive(async () => {
|
|
95
|
-
|
|
112
|
+
this.stopBeforeIntent = false;
|
|
113
|
+
const intent: DialerIntent = { scope: this.scope, key: this.newKey(), input: structuredClone(input), stopping: false, ...(queue ? { queue: structuredClone(queue) } : {}) };
|
|
96
114
|
// Persist before the first network request. A storage failure cannot dial.
|
|
97
115
|
await this.journal.write(structuredClone(intent)); this.intent = intent;
|
|
116
|
+
if (this.stopBeforeIntent) { intent.stopping = true; await this.journal.write(structuredClone(intent)); }
|
|
98
117
|
await this.setup();
|
|
99
118
|
});
|
|
100
119
|
}
|
|
@@ -118,13 +137,13 @@ export class ManagedDialer {
|
|
|
118
137
|
if (!saved || saved.key !== intent.key || saved.callId !== intent.callId) throw new Error("Calling recovery intent changed.");
|
|
119
138
|
if (saved.stopping) intent.stopping = true;
|
|
120
139
|
this.update({ callId: session.call_id, call: session.call });
|
|
121
|
-
if (terminal(session.call)) { await this.acceptStatus(session.call); return; }
|
|
140
|
+
if (terminal(session.call) || dialerCallHandedOff(session.call)) { await this.acceptStatus(session.call); return; }
|
|
122
141
|
if (intent.stopping) { await this.stopKnown(); return; }
|
|
123
142
|
if (!session.access_token || session.expires_at <= this.now() || session.call.stop_requested) {
|
|
124
143
|
this.update({ state: "unconfirmed", error: "This session was consumed, stopped or expired. End or recover the original call." }); return;
|
|
125
144
|
}
|
|
126
145
|
await this.audio.connect(session, state => {
|
|
127
|
-
if (this.intent !== intent || intent.stopping || this.snapshot.state
|
|
146
|
+
if (this.intent?.key !== intent.key || this.intent.stopping || ["wrapup", "handing_off", "transferred"].includes(this.snapshot.state)) return;
|
|
128
147
|
if (state === "disconnected") {
|
|
129
148
|
this.audio.disconnect();
|
|
130
149
|
this.update({ state: "unconfirmed", error: "Audio disconnected. Check the original call before dialing again." });
|
|
@@ -147,19 +166,46 @@ export class ManagedDialer {
|
|
|
147
166
|
private async acceptStatus(call: TelephonyCall) {
|
|
148
167
|
if (!this.intent || call.id !== this.intent.callId) throw new Error("Calling status identity changed.");
|
|
149
168
|
if (this.snapshot.call && call.version < this.snapshot.call.version) return;
|
|
169
|
+
const transfer = this.intent.transfer;
|
|
170
|
+
if (transfer) {
|
|
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);
|
|
172
|
+
if (observed && observed.to === transfer.to && observed.mode === transfer.mode) {
|
|
173
|
+
const next = structuredClone(this.intent);
|
|
174
|
+
if (["canceled", "failed"].includes(observed.status)) {
|
|
175
|
+
next.retiredTransferKeys = [...new Set([...(next.retiredTransferKeys ?? []), transfer.key])];
|
|
176
|
+
delete next.transfer;
|
|
177
|
+
}
|
|
178
|
+
else next.transfer!.id = observed.id;
|
|
179
|
+
await this.journal.write(next); this.intent = next;
|
|
180
|
+
this.update({ transferPending: Boolean(transfer.command && !["completed", "canceled", "failed"].includes(observed.status)) });
|
|
181
|
+
}
|
|
182
|
+
}
|
|
150
183
|
if (terminal(call)) {
|
|
151
184
|
this.audio.disconnect();
|
|
152
185
|
this.update({ call, state: "wrapup", muted: false, error: null });
|
|
186
|
+
} else if (dialerCallHandedOff(call)) {
|
|
187
|
+
this.audio.disconnect();
|
|
188
|
+
this.update({ call, state: call.transfers?.some(item => item.status === "completed") ? "transferred" : "handing_off", muted: false, error: null });
|
|
153
189
|
} else this.update({ call });
|
|
154
190
|
}
|
|
155
191
|
async stop(): Promise<void> {
|
|
156
|
-
|
|
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
|
+
}
|
|
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.");
|
|
157
200
|
// Mark immediately so a late create acknowledgment cannot connect audio.
|
|
158
201
|
this.intent.stopping = true; this.audio.disconnect();
|
|
159
202
|
this.update({ state: "stopping", muted: false });
|
|
160
203
|
// Await in-flight setup; never issue an independent replacement create.
|
|
161
204
|
if (this.operation) await this.operation;
|
|
162
205
|
return this.exclusive(async () => {
|
|
206
|
+
if (this.snapshot.call && dialerCallHandedOff(this.snapshot.call)) {
|
|
207
|
+
this.intent!.stopping = false; await this.journal.write(structuredClone(this.intent!)); return;
|
|
208
|
+
}
|
|
163
209
|
await this.journal.write(structuredClone(this.intent!));
|
|
164
210
|
if (!this.intent!.callId) await this.setup();
|
|
165
211
|
else await this.stopKnown();
|
|
@@ -167,6 +213,11 @@ export class ManagedDialer {
|
|
|
167
213
|
}
|
|
168
214
|
private async stopKnown() {
|
|
169
215
|
try {
|
|
216
|
+
const current = await this.broker.get(this.intent!.callId!);
|
|
217
|
+
if (dialerCallHandedOff(current)) {
|
|
218
|
+
this.intent!.stopping = false; await this.journal.write(structuredClone(this.intent!));
|
|
219
|
+
await this.acceptStatus(current); return;
|
|
220
|
+
}
|
|
170
221
|
const call = await this.broker.hangup(this.intent!.callId!);
|
|
171
222
|
await this.acceptStatus(call);
|
|
172
223
|
if (!terminal(call)) this.update({ state: "stopping", error: null });
|
|
@@ -174,11 +225,66 @@ export class ManagedDialer {
|
|
|
174
225
|
}
|
|
175
226
|
async finishWrapup(): Promise<void> {
|
|
176
227
|
return this.exclusive(async () => {
|
|
177
|
-
if (!this.intent || this.snapshot.
|
|
228
|
+
if (!this.intent || !this.snapshot.call || !(this.snapshot.state === "wrapup" && terminal(this.snapshot.call) ||
|
|
229
|
+
this.snapshot.state === "transferred" && this.snapshot.call.transfers?.some(item => item.status === "completed"))) throw new Error("Confirmed call completion or successful transfer is required.");
|
|
178
230
|
await this.journal.clear(this.scope, this.intent.key); this.intent = null;
|
|
179
|
-
this.update({ state: "idle", callId: null, call: null, muted: false, error: null });
|
|
231
|
+
this.update({ state: "idle", callId: null, call: null, muted: false, error: null, transferPending: false });
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
async transfer(to: string, mode: "warm" | "cold") {
|
|
235
|
+
if (!/^\+[1-9]\d{6,14}$/.test(to) || !["warm", "cold"].includes(mode)) throw new Error("Choose a transfer destination with its country code and a transfer mode.");
|
|
236
|
+
return this.exclusive(async () => {
|
|
237
|
+
if (!this.intent?.callId || this.snapshot.state !== "active" || !this.snapshot.call?.transfer_enabled || !this.broker.transfers)
|
|
238
|
+
throw new Error("Transfers require a connected call started with transfers enabled.");
|
|
239
|
+
if (this.intent.transfer || this.snapshot.call.transfers?.some(item => !["completed", "canceled", "failed"].includes(item.status)))
|
|
240
|
+
throw new Error("Recover or finish the current transfer before starting another.");
|
|
241
|
+
const next = structuredClone(this.intent);
|
|
242
|
+
next.transfer = { key: this.newKey(), to, mode, priorIds: this.snapshot.call.transfers?.map(item => item.id) ?? [] };
|
|
243
|
+
await this.journal.write(next); this.intent = next;
|
|
244
|
+
await this.transferKnown();
|
|
180
245
|
});
|
|
181
246
|
}
|
|
247
|
+
async retryTransfer() {
|
|
248
|
+
return this.exclusive(async () => {
|
|
249
|
+
if (!this.intent?.transfer) throw new Error("No transfer to recover.");
|
|
250
|
+
await this.transferKnown();
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
async completeTransfer() { return this.transferCommand("complete"); }
|
|
254
|
+
async cancelTransfer() { return this.transferCommand("cancel"); }
|
|
255
|
+
private async transferCommand(command: "complete" | "cancel") {
|
|
256
|
+
return this.exclusive(async () => {
|
|
257
|
+
if (!this.intent?.transfer?.id || !this.broker.transfers) throw new Error("Recover the transfer identity first.");
|
|
258
|
+
const transfer = this.snapshot.call?.transfers?.find(item => item.id === this.intent!.transfer!.id);
|
|
259
|
+
if (command === "complete" && transfer?.status !== "ready") throw new Error("Wait until the transfer target has joined.");
|
|
260
|
+
if (this.intent.transfer.mode !== "warm") throw new Error("Cold transfers complete automatically.");
|
|
261
|
+
if (this.intent.transfer.command && this.intent.transfer.command !== command) throw new Error("Recover the saved transfer command first.");
|
|
262
|
+
const next = structuredClone(this.intent); next.transfer!.command = command;
|
|
263
|
+
await this.journal.write(next); this.intent = next;
|
|
264
|
+
await this.transferKnown();
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
private async transferKnown() {
|
|
268
|
+
this.update({ transferPending: true, error: null });
|
|
269
|
+
try {
|
|
270
|
+
const original = this.intent!, saved = await this.journal.read(this.scope);
|
|
271
|
+
if (!saved || saved.scope !== this.scope || saved.key !== original.key || saved.callId !== original.callId ||
|
|
272
|
+
saved.transfer?.key !== original.transfer?.key || saved.transfer?.to !== original.transfer?.to || saved.transfer?.mode !== original.transfer?.mode)
|
|
273
|
+
throw new Error("Saved transfer intent changed.");
|
|
274
|
+
this.intent = saved;
|
|
275
|
+
if (saved.stopping) { await this.stopKnown(); return; }
|
|
276
|
+
const intent = saved, transfer = saved.transfer!, broker = this.broker.transfers!;
|
|
277
|
+
const call = transfer.command && transfer.id ? await broker[transfer.command](intent.callId!, transfer.id)
|
|
278
|
+
: await broker.create(intent.callId!, { to: transfer.to, mode: transfer.mode }, transfer.key);
|
|
279
|
+
if (call.id !== intent.callId) throw new Error("Transfer call identity changed.");
|
|
280
|
+
const observed = transfer.id ? call.transfers?.find(item => item.id === transfer.id)
|
|
281
|
+
: call.transfers?.slice().reverse().find(item => !transfer.priorIds?.includes(item.id) && item.to === transfer.to && item.mode === transfer.mode);
|
|
282
|
+
if (!observed || observed.to !== transfer.to || observed.mode !== transfer.mode) throw new Error("Transfer identity is unavailable.");
|
|
283
|
+
transfer.id = observed.id;
|
|
284
|
+
await this.journal.write(structuredClone(intent));
|
|
285
|
+
await this.acceptStatus(call); this.update({ transferPending: false });
|
|
286
|
+
} catch { this.update({ transferPending: true, error: "Transfer is unconfirmed. Recover this same transfer before trying another." }); }
|
|
287
|
+
}
|
|
182
288
|
mute(value: boolean) {
|
|
183
289
|
if (this.snapshot.state !== "active") throw new Error("No active audio call.");
|
|
184
290
|
this.audio.mute(value); this.update({ muted: value });
|
|
@@ -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);
|
|
@@ -2,12 +2,13 @@
|
|
|
2
2
|
import { createHash } from "node:crypto";
|
|
3
3
|
import type { PyAI, TelephonyBrowserSessionCreate } from "../index.ts";
|
|
4
4
|
import type { DialerBroker } from "./managedDialer.ts";
|
|
5
|
+
import { dialerCallHandedOff } from "./managedDialer.ts";
|
|
5
6
|
|
|
6
7
|
export function createServerDialerBroker(sdk: PyAI, authenticatedUserId: string): DialerBroker {
|
|
7
8
|
if (!authenticatedUserId.trim()) throw new Error("An authenticated user is required.");
|
|
8
9
|
const ownedCall = async (id: string) => {
|
|
9
10
|
const call = await sdk.telephony.calls.get(id);
|
|
10
|
-
if (call.call_type !== "human" || call.user_id !== authenticatedUserId) throw new Error("No such call.");
|
|
11
|
+
if (call.id !== id || call.call_type !== "human" || call.user_id !== authenticatedUserId) throw new Error("No such call.");
|
|
11
12
|
return call;
|
|
12
13
|
};
|
|
13
14
|
return {
|
|
@@ -18,6 +19,20 @@ export function createServerDialerBroker(sdk: PyAI, authenticatedUserId: string)
|
|
|
18
19
|
return sdk.telephony.browserSessions.create({ ...input, user_id: authenticatedUserId }, { idempotencyKey: scopedKey });
|
|
19
20
|
},
|
|
20
21
|
get: ownedCall,
|
|
21
|
-
async hangup(id: string) {
|
|
22
|
+
async hangup(id: string) {
|
|
23
|
+
const call = await ownedCall(id);
|
|
24
|
+
if (dialerCallHandedOff(call)) return call;
|
|
25
|
+
return sdk.telephony.calls.hangup(id);
|
|
26
|
+
},
|
|
27
|
+
transfers: {
|
|
28
|
+
async create(id, input, key) {
|
|
29
|
+
await ownedCall(id);
|
|
30
|
+
if (!key || key.length > 255 || /[\r\n]/.test(key)) throw new Error("A valid transfer retry key is required.");
|
|
31
|
+
const scopedKey = "dialer-transfer:" + createHash("sha256").update(authenticatedUserId).update("\0").update(id).update("\0").update(key).digest("hex");
|
|
32
|
+
return sdk.telephony.calls.transfers.create(id, { ...input, user_id: authenticatedUserId }, { idempotencyKey: scopedKey });
|
|
33
|
+
},
|
|
34
|
+
async complete(id, transferId) { await ownedCall(id); return sdk.telephony.calls.transfers.complete(id, transferId, authenticatedUserId); },
|
|
35
|
+
async cancel(id, transferId) { await ownedCall(id); return sdk.telephony.calls.transfers.cancel(id, transferId, authenticatedUserId); },
|
|
36
|
+
},
|
|
22
37
|
};
|
|
23
38
|
}
|