@wirecat/cli-core 0.662.0 → 0.664.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/CHANGELOG.md +213 -0
- package/dist/calendar/cli/bodies.js +2 -1
- package/dist/calendar/cli/index.js +13 -10
- package/dist/calendar/mcp/index.d.ts +3 -1
- package/dist/calendar/mcp/index.js +4 -4
- package/dist/cli/call-stats-command.d.ts +18 -0
- package/dist/cli/call-stats-command.js +52 -0
- package/dist/cli/chat-sending-command.d.ts +14 -0
- package/dist/cli/chat-sending-command.js +64 -0
- package/dist/cli/context.d.ts +7 -0
- package/dist/cli/context.js +12 -4
- package/dist/cli/contract-templates.js +2 -0
- package/dist/cli/envelope.d.ts +0 -5
- package/dist/cli/envelope.js +25 -0
- package/dist/cli/index.d.ts +4 -1
- package/dist/cli/index.js +4 -1
- package/dist/cli/meeting-batch.d.ts +2 -1
- package/dist/cli/meeting-batch.js +7 -5
- package/dist/cli/meeting-meaning-command.d.ts +47 -0
- package/dist/cli/meeting-meaning-command.js +136 -0
- package/dist/cli/meeting-preparation-answer.d.ts +6 -0
- package/dist/cli/meeting-preparation-command.js +5 -5
- package/dist/cli/meeting-preparation-projects.d.ts +6 -0
- package/dist/cli/meeting-preparation-read.d.ts +11 -5
- package/dist/cli/meeting-preparation-read.js +13 -21
- package/dist/cli/meeting-series-inventory-command.js +2 -2
- package/dist/cli/meeting-series-inventory-read.d.ts +2 -2
- package/dist/cli/meeting-series-inventory-read.js +7 -15
- package/dist/cli/meeting-tags-command.d.ts +18 -0
- package/dist/cli/meeting-tags-command.js +74 -0
- package/dist/cli/messenger/admin-statistics-command.js +2 -2
- package/dist/cli/messenger/conversation-answers.d.ts +1 -1
- package/dist/cli/messenger/conversation-answers.js +1 -0
- package/dist/cli/messenger/conversations-command.js +38 -12
- package/dist/cli/messenger/counters-command.js +2 -2
- package/dist/cli/messenger/media-archive-command.js +5 -4
- package/dist/cli/messenger/media-archive-move-command.js +1 -1
- package/dist/cli/messenger/media-archive-timer-command.js +5 -4
- package/dist/cli/messenger/metadata-command.js +3 -3
- package/dist/cli/messenger/models-command.js +7 -0
- package/dist/cli/messenger/rankings-command.js +3 -3
- package/dist/cli/messenger/records-command.d.ts +4 -1
- package/dist/cli/messenger/records-command.js +6 -1
- package/dist/cli/messenger/tags-command.js +7 -2
- package/dist/cli/messenger/tasks-command.js +2 -2
- package/dist/cli/model-catalogue-command.js +2 -0
- package/dist/cli/paging.d.ts +10 -0
- package/dist/cli/paging.js +29 -0
- package/dist/cli/profile-token.d.ts +15 -1
- package/dist/cli/profile-token.js +103 -7
- package/dist/cli/program.js +2 -2
- package/dist/cli/runs/command.js +6 -4
- package/dist/cli/runs/search-command.js +9 -5
- package/dist/cli/schedule/schedule-command.js +1 -5
- package/dist/domain/links.d.ts +11 -0
- package/dist/domain/links.js +4 -0
- package/dist/domain/moment.d.ts +29 -0
- package/dist/domain/moment.js +58 -0
- package/dist/domain/providers.d.ts +6 -4
- package/dist/domain/providers.js +10 -5
- package/dist/domain/send-time.d.ts +3 -4
- package/dist/domain/send-time.js +10 -24
- package/dist/entities/chat-sending.d.ts +10 -0
- package/dist/entities/chat-sending.js +11 -0
- package/dist/entities/fields.d.ts +2 -2
- package/dist/entities/fields.js +2 -2
- package/dist/entities/index.d.ts +135 -2
- package/dist/entities/index.js +8 -3
- package/dist/entities/meeting-preparation.d.ts +6 -0
- package/dist/entities/meeting.d.ts +1 -1
- package/dist/entities/meeting.js +1 -1
- package/dist/errors/reasons.js +25 -2
- package/dist/mail/cli/index.js +4 -2
- package/dist/mail/facts.d.ts +5 -2
- package/dist/mail/facts.js +6 -3
- package/dist/mail/index.d.ts +1 -1
- package/dist/mail/meeting-notes.js +1 -1
- package/dist/mail/probe.d.ts +5 -2
- package/dist/mail/probe.js +9 -5
- package/dist/mail/server-search.js +1 -1
- package/dist/mail/sync.js +1 -1
- package/dist/mcp/meeting-preparation.d.ts +6 -0
- package/dist/mcp/meeting-preparation.js +1 -1
- package/dist/mcp/meeting-series-inventory.js +1 -1
- package/dist/mcp/prompts.js +6 -9
- package/dist/meetings/answers.d.ts +7 -0
- package/dist/meetings/answers.js +4 -0
- package/dist/meetings/call-answers.d.ts +106 -0
- package/dist/meetings/call-answers.js +171 -0
- package/dist/meetings/call-stats.d.ts +121 -0
- package/dist/meetings/call-stats.js +211 -0
- package/dist/meetings/calls.d.ts +76 -0
- package/dist/meetings/calls.js +78 -0
- package/dist/meetings/cli/index.d.ts +8 -0
- package/dist/meetings/cli/index.js +10 -22
- package/dist/meetings/cli/search.d.ts +9 -2
- package/dist/meetings/cli/search.js +16 -26
- package/dist/meetings/index.d.ts +4 -0
- package/dist/meetings/index.js +4 -0
- package/dist/meetings/mcp/index.d.ts +6 -3
- package/dist/meetings/mcp/index.js +2 -4
- package/dist/meetings/mcp/search.d.ts +12 -1
- package/dist/meetings/mcp/search.js +3 -3
- package/dist/meetings/project-evidence-answer.d.ts +6 -0
- package/dist/meetings/read-through.d.ts +2 -1
- package/dist/meetings/read-through.js +10 -1
- package/dist/meetings/stats.d.ts +3 -0
- package/dist/meetings/stats.js +3 -3
- package/dist/meetings/tag-answers.d.ts +43 -0
- package/dist/meetings/tag-answers.js +41 -0
- package/dist/meetings/testing/index.d.ts +1 -0
- package/dist/meetings/testing/index.js +1 -0
- package/dist/meetings/testing/paging.d.ts +6 -0
- package/dist/meetings/testing/paging.js +5 -0
- package/dist/models/chat-sending.d.ts +24 -0
- package/dist/models/chat-sending.js +40 -0
- package/dist/moderation/rules.d.ts +2 -0
- package/dist/moderation/rules.js +16 -2
- package/dist/runs/events.d.ts +1 -1
- package/dist/search/fusion.d.ts +17 -0
- package/dist/search/fusion.js +17 -0
- package/dist/search/lucene/resolved.d.ts +2 -0
- package/dist/search/question-plan.d.ts +4 -0
- package/dist/search/question-plan.js +7 -0
- package/dist/search/service-query.d.ts +6 -1
- package/dist/search/service-query.js +9 -1
- package/dist/search/task-similarity.d.ts +1 -0
- package/dist/search/task-similarity.js +1 -1
- package/dist/services/agenda-mail.js +2 -1
- package/dist/services/agenda.js +5 -3
- package/dist/services/attachments.js +4 -2
- package/dist/services/call-stats.d.ts +17 -0
- package/dist/services/call-stats.js +41 -0
- package/dist/services/chats.js +4 -2
- package/dist/services/conversations.js +4 -2
- package/dist/services/deps.d.ts +1 -1
- package/dist/services/document-source.d.ts +1 -1
- package/dist/services/document-source.js +4 -3
- package/dist/services/documents/ingestion.js +11 -40
- package/dist/services/documents/plan.d.ts +1 -1
- package/dist/services/documents/plan.js +30 -17
- package/dist/services/documents/ports.d.ts +5 -0
- package/dist/services/documents/sync.js +1 -1
- package/dist/services/embeddings-search.js +39 -9
- package/dist/services/embeddings.d.ts +4 -0
- package/dist/services/events-search.js +2 -1
- package/dist/services/index.d.ts +3 -0
- package/dist/services/index.js +3 -0
- package/dist/services/mail-search.d.ts +1 -1
- package/dist/services/mail-search.js +10 -11
- package/dist/services/media-archive-move.js +2 -4
- package/dist/services/media-archive.d.ts +0 -1
- package/dist/services/media-archive.js +2 -12
- package/dist/services/meeting-combined.js +1 -1
- package/dist/services/meeting-meaning.d.ts +47 -0
- package/dist/services/meeting-meaning.js +158 -0
- package/dist/services/meeting-tags.d.ts +27 -0
- package/dist/services/meeting-tags.js +81 -0
- package/dist/services/messages-combined.js +15 -4
- package/dist/services/messages-discovery.js +2 -2
- package/dist/services/moderation.js +10 -2
- package/dist/services/moment.d.ts +4 -10
- package/dist/services/moment.js +16 -44
- package/dist/services/notes-search.d.ts +0 -2
- package/dist/services/notes-search.js +1 -2
- package/dist/services/search-all.js +2 -1
- package/dist/services/tags.js +4 -2
- package/dist/services/task-stats.js +2 -2
- package/dist/services/tasks.js +2 -2
- package/dist/sftp/index.d.ts +1 -1
- package/dist/sftp/index.js +1 -1
- package/dist/sftp/session.d.ts +30 -4
- package/dist/sftp/session.js +195 -11
- package/dist/sftp/ssh2.d.ts +19 -4
- package/dist/sftp/ssh2.js +144 -17
- package/dist/speech/cloud.d.ts +47 -0
- package/dist/speech/cloud.js +73 -0
- package/dist/speech/decode.d.ts +36 -9
- package/dist/speech/decode.js +208 -61
- package/dist/speech/index.d.ts +2 -1
- package/dist/speech/index.js +2 -1
- package/dist/speech/openai.d.ts +4 -0
- package/dist/speech/openai.js +13 -12
- package/dist/sql/index.d.ts +1 -0
- package/dist/sql/index.js +1 -0
- package/dist/sql/reader.d.ts +95 -0
- package/dist/sql/reader.js +214 -0
- package/dist/store/accounts.js +2 -1
- package/dist/store/sqlite/account-purge.d.ts +3 -0
- package/dist/store/sqlite/account-purge.js +143 -0
- package/dist/store/sqlite/accounts.d.ts +0 -2
- package/dist/store/sqlite/accounts.js +2 -134
- package/dist/store/sqlite/calendar.d.ts +1 -0
- package/dist/store/sqlite/calendar.js +5 -5
- package/dist/store/sqlite/catalog.js +2 -16
- package/dist/store/sqlite/chats.js +2 -1
- package/dist/store/sqlite/context-document-source.d.ts +3 -3
- package/dist/store/sqlite/context-document-source.js +9 -7
- package/dist/store/sqlite/context-evidence.js +9 -3
- package/dist/store/sqlite/document-listing.js +2 -1
- package/dist/store/sqlite/drizzle/bun.js +4 -4
- package/dist/store/sqlite/drizzle/bun.js.map +1 -1
- package/dist/store/sqlite/drizzle/core.js +5 -5
- package/dist/store/sqlite/drizzle/core.js.map +1 -1
- package/dist/store/sqlite/drizzle/node.js +4 -4
- package/dist/store/sqlite/drizzle/node.js.map +1 -1
- package/dist/store/sqlite/drizzle/{shared-55XJMTPA.js → shared-5XR7K42U.js} +58 -58
- package/dist/store/sqlite/drizzle/{shared-55XJMTPA.js.map → shared-5XR7K42U.js.map} +1 -1
- package/dist/store/sqlite/events.js +2 -1
- package/dist/store/sqlite/file-note.d.ts +6 -0
- package/dist/store/sqlite/file-note.js +15 -0
- package/dist/store/sqlite/files.d.ts +3 -2
- package/dist/store/sqlite/files.js +5 -3
- package/dist/store/sqlite/identities.js +2 -1
- package/dist/store/sqlite/imported-documents.js +2 -2
- package/dist/store/sqlite/knowledge.d.ts +1 -1
- package/dist/store/sqlite/knowledge.js +27 -23
- package/dist/store/sqlite/labelled.d.ts +5 -0
- package/dist/store/sqlite/labelled.js +6 -0
- package/dist/store/sqlite/link-row.d.ts +1 -0
- package/dist/store/sqlite/link-row.js +2 -0
- package/dist/store/sqlite/lucene-fields.js +8 -12
- package/dist/store/sqlite/mail-rules.js +2 -1
- package/dist/store/sqlite/meeting-calls.d.ts +4 -0
- package/dist/store/sqlite/meeting-calls.js +66 -0
- package/dist/store/sqlite/meetings.d.ts +2 -2
- package/dist/store/sqlite/meetings.js +4 -1
- package/dist/store/sqlite/mention-sources.js +2 -1
- package/dist/store/sqlite/mentions.js +2 -2
- package/dist/store/sqlite/messages.js +6 -1
- package/dist/store/sqlite/note-search.js +8 -11
- package/dist/store/sqlite/notes.d.ts +3 -1
- package/dist/store/sqlite/notes.js +25 -14
- package/dist/store/sqlite/organizations.js +2 -2
- package/dist/store/sqlite/people-list.js +3 -3
- package/dist/store/sqlite/query-filter.d.ts +3 -3
- package/dist/store/sqlite/query-filter.js +3 -3
- package/dist/store/sqlite/reads.d.ts +4 -4
- package/dist/store/sqlite/reads.js +4 -4
- package/dist/store/sqlite/search.js +11 -7
- package/dist/store/sqlite/test-accounts.js +1 -1
- package/dist/store/sqlite/trigram-match.d.ts +9 -0
- package/dist/store/sqlite/trigram-match.js +28 -0
- package/dist/store/sqlite/work-tasks.js +1 -1
- package/package.json +18 -3
- package/parity.json +20 -1
- package/skills/link-conversations/SKILL.md +1 -1
package/dist/sftp/session.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { backoffMs, CliError, isCliError, realSleep, } from "@wirecat/cli-base";
|
|
2
2
|
import { providerEvent, providerRequest } from "../cli/command-runs.js";
|
|
3
|
-
import { openWire, probeHostKey } from "./ssh2.js";
|
|
3
|
+
import { openForwarder, openWire, probeHostKey, } from "./ssh2.js";
|
|
4
4
|
export const SFTP_CONNECT_TIMEOUT_MS = 20_000;
|
|
5
5
|
export const SFTP_OPERATION_TIMEOUT_MS = 30_000;
|
|
6
6
|
/** Every operation only reads, so a transient failure is tried again: three attempts in all. */
|
|
@@ -62,7 +62,7 @@ const retrying = async (target, job) => {
|
|
|
62
62
|
if (stop)
|
|
63
63
|
throw stop;
|
|
64
64
|
const answered = providerRequest(job.operation, events, {
|
|
65
|
-
transport: "sftp",
|
|
65
|
+
transport: job.transport ?? "sftp",
|
|
66
66
|
attempt,
|
|
67
67
|
...(waitMs === undefined ? {} : { waitMs }),
|
|
68
68
|
});
|
|
@@ -130,12 +130,12 @@ export const sftpHostKey = async (target) => {
|
|
|
130
130
|
});
|
|
131
131
|
};
|
|
132
132
|
const closedError = () => new CliError("validation_error", "the SFTP connection is closed", { reason: "sftp.closed" });
|
|
133
|
-
const refuseOptions = (options) => {
|
|
133
|
+
const refuseOptions = (options, server = "SFTP") => {
|
|
134
134
|
if (!options.hostKey?.trim())
|
|
135
|
-
throw new CliError("configuration_error",
|
|
135
|
+
throw new CliError("configuration_error", `no host key is pinned for this ${server} server: show the owner its fingerprint (sftpHostKey) and pin the one confirmed`, { reason: "sftp.host-key-missing" });
|
|
136
136
|
const auth = options.auth;
|
|
137
137
|
if (!auth?.password && !auth?.privateKey)
|
|
138
|
-
throw new CliError("configuration_error",
|
|
138
|
+
throw new CliError("configuration_error", `an ${server} connection needs a password or a private key`, {
|
|
139
139
|
reason: "sftp.auth-missing",
|
|
140
140
|
});
|
|
141
141
|
};
|
|
@@ -144,17 +144,27 @@ const refuseOptions = (options) => {
|
|
|
144
144
|
* fingerprint before a credential is sent. Each operation is a request in the run's events (host and operation,
|
|
145
145
|
* never a path or file content) and is tried again on a transient failure over a fresh connection.
|
|
146
146
|
*/
|
|
147
|
+
const credentialsOf = (options) => ({
|
|
148
|
+
username: options.username,
|
|
149
|
+
...("password" in options.auth
|
|
150
|
+
? { password: options.auth.password }
|
|
151
|
+
: { privateKey: options.auth.privateKey, passphrase: options.auth.passphrase }),
|
|
152
|
+
});
|
|
153
|
+
const STREAM_CHUNK = 64 * 1024;
|
|
154
|
+
const STREAM_AHEAD = 16;
|
|
155
|
+
const fileChanged = () => new CliError("validation_error", "the file changed on the SFTP server while it was read; read it again", {
|
|
156
|
+
reason: "sftp.file-changed",
|
|
157
|
+
});
|
|
158
|
+
const tooLarge = (maxBytes) => new CliError("validation_error", `the file is larger than the read's limit of ${maxBytes} bytes`, {
|
|
159
|
+
reason: "sftp.too-large",
|
|
160
|
+
max_bytes: maxBytes,
|
|
161
|
+
});
|
|
147
162
|
export const openSftp = async (input) => {
|
|
148
163
|
refuseOptions(input);
|
|
149
164
|
const options = seams(input);
|
|
150
165
|
const pinned = normalized(options.hostKey);
|
|
151
166
|
const operationMs = options.operationTimeoutMs ?? SFTP_OPERATION_TIMEOUT_MS;
|
|
152
|
-
const credentials =
|
|
153
|
-
username: options.username,
|
|
154
|
-
...("password" in options.auth
|
|
155
|
-
? { password: options.auth.password }
|
|
156
|
-
: { privateKey: options.auth.privateKey, passphrase: options.auth.passphrase }),
|
|
157
|
-
};
|
|
167
|
+
const credentials = credentialsOf(options);
|
|
158
168
|
let wire;
|
|
159
169
|
let connecting;
|
|
160
170
|
let closed = false;
|
|
@@ -204,6 +214,94 @@ export const openSftp = async (input) => {
|
|
|
204
214
|
},
|
|
205
215
|
...(answer ? { answer } : {}),
|
|
206
216
|
});
|
|
217
|
+
const timed = (current, work) => deadline(operationMs, options.sleep, () => current.close(), () => work());
|
|
218
|
+
async function* readStream(path, maxBytes) {
|
|
219
|
+
const retry = options.retry ?? SFTP_RETRY;
|
|
220
|
+
const events = options.events ?? providerEvent;
|
|
221
|
+
let position = 0;
|
|
222
|
+
let tail = Buffer.alloc(0);
|
|
223
|
+
let waitMs;
|
|
224
|
+
for (let attempt = 1;; attempt++) {
|
|
225
|
+
const stop = stoppedBy(options.signal);
|
|
226
|
+
if (stop)
|
|
227
|
+
throw stop;
|
|
228
|
+
const answered = providerRequest("sftp.stream", events, {
|
|
229
|
+
transport: "sftp",
|
|
230
|
+
attempt,
|
|
231
|
+
...(waitMs === undefined ? {} : { waitMs }),
|
|
232
|
+
});
|
|
233
|
+
const from = position;
|
|
234
|
+
let current;
|
|
235
|
+
let file;
|
|
236
|
+
let finished = false;
|
|
237
|
+
try {
|
|
238
|
+
current = await connected();
|
|
239
|
+
const wire = current;
|
|
240
|
+
file = await timed(wire, () => wire.open(path));
|
|
241
|
+
const opened = file;
|
|
242
|
+
if (opened.size > maxBytes)
|
|
243
|
+
throw tooLarge(maxBytes);
|
|
244
|
+
if (position > 0) {
|
|
245
|
+
const again = await timed(wire, () => opened.read(position - tail.length, tail.length));
|
|
246
|
+
if (!again.equals(tail))
|
|
247
|
+
throw fileChanged();
|
|
248
|
+
}
|
|
249
|
+
const ahead = [];
|
|
250
|
+
let next = position;
|
|
251
|
+
const ask = () => {
|
|
252
|
+
const at = next;
|
|
253
|
+
const piece = timed(wire, () => opened.read(at, STREAM_CHUNK));
|
|
254
|
+
piece.catch(() => { });
|
|
255
|
+
ahead.push(piece);
|
|
256
|
+
next += STREAM_CHUNK;
|
|
257
|
+
};
|
|
258
|
+
while (ahead.length < STREAM_AHEAD)
|
|
259
|
+
ask();
|
|
260
|
+
for (;;) {
|
|
261
|
+
const piece = await ahead.shift();
|
|
262
|
+
if (piece.length > 0) {
|
|
263
|
+
position += piece.length;
|
|
264
|
+
if (position > maxBytes)
|
|
265
|
+
throw tooLarge(maxBytes);
|
|
266
|
+
tail = piece;
|
|
267
|
+
yield new Uint8Array(piece.buffer, piece.byteOffset, piece.length);
|
|
268
|
+
}
|
|
269
|
+
if (piece.length < STREAM_CHUNK) {
|
|
270
|
+
finished = true;
|
|
271
|
+
answered({ ids: { host: options.host }, bytes: position - from });
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
ask();
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
catch (caught) {
|
|
278
|
+
const error = stoppedBy(options.signal) ?? (caught instanceof Expired ? timedOut("request", operationMs) : caught);
|
|
279
|
+
answered({ ids: { host: options.host }, error, ...(isCliError(error) ? { errorCode: error.code } : {}) });
|
|
280
|
+
finished = true;
|
|
281
|
+
const retryable = isCliError(error) && error.details.retryable === true;
|
|
282
|
+
if (retryable || caught instanceof Expired)
|
|
283
|
+
drop(current);
|
|
284
|
+
if (!retryable || options.signal?.aborted || attempt > retry.retries) {
|
|
285
|
+
if (retryable)
|
|
286
|
+
throw new CliError(error.code, error.message, { ...error.details, attempts: attempt });
|
|
287
|
+
throw error;
|
|
288
|
+
}
|
|
289
|
+
waitMs = backoffMs(attempt, retry, options.random);
|
|
290
|
+
try {
|
|
291
|
+
await options.sleep(waitMs, options.signal, "retry");
|
|
292
|
+
}
|
|
293
|
+
catch {
|
|
294
|
+
throw stoppedBy(options.signal) ?? error;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
finally {
|
|
298
|
+
file?.close();
|
|
299
|
+
// The reader stopped early: what it took is this attempt's answer.
|
|
300
|
+
if (!finished)
|
|
301
|
+
answered({ ids: { host: options.host }, bytes: position - from });
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
207
305
|
try {
|
|
208
306
|
await retrying(options, { operation: "sftp.connect", run: () => connected() });
|
|
209
307
|
}
|
|
@@ -221,6 +319,92 @@ export const openSftp = async (input) => {
|
|
|
221
319
|
}));
|
|
222
320
|
return operation("sftp.read", (current, touch) => current.read(path, maxBytes, touch), (bytes) => ({ bytes: bytes.length }));
|
|
223
321
|
},
|
|
322
|
+
readStream: (path, { maxBytes = Number.POSITIVE_INFINITY } = {}) => {
|
|
323
|
+
if (maxBytes !== Number.POSITIVE_INFINITY && (!Number.isSafeInteger(maxBytes) || maxBytes < 0))
|
|
324
|
+
throw new CliError("validation_error", "maxBytes must be a whole number of bytes, 0 or more", {
|
|
325
|
+
reason: "input.invalid",
|
|
326
|
+
});
|
|
327
|
+
return readStream(path, maxBytes);
|
|
328
|
+
},
|
|
329
|
+
close: async () => {
|
|
330
|
+
closed = true;
|
|
331
|
+
options.signal?.removeEventListener("abort", abort);
|
|
332
|
+
drop();
|
|
333
|
+
},
|
|
334
|
+
};
|
|
335
|
+
};
|
|
336
|
+
/**
|
|
337
|
+
* An SSH connection that only forwards, for a database that listens only on its own server (a FreePBX CDR database):
|
|
338
|
+
* `ssh -L` without a port on this computer, so no other program here can reach the database through it. The host key
|
|
339
|
+
* must match the pinned fingerprint before a credential is sent. Each forward is a request in the run's events (host
|
|
340
|
+
* only, never the address forwarded to) and is tried again on a transient failure over a fresh connection.
|
|
341
|
+
*/
|
|
342
|
+
export const openSshTunnel = async (input) => {
|
|
343
|
+
refuseOptions(input, "SSH");
|
|
344
|
+
const options = seams(input);
|
|
345
|
+
const pinned = normalized(options.hostKey);
|
|
346
|
+
const credentials = credentialsOf(options);
|
|
347
|
+
const target = { ...options, events: options.events ?? providerEvent };
|
|
348
|
+
let ssh;
|
|
349
|
+
let connecting;
|
|
350
|
+
let closed = false;
|
|
351
|
+
const drop = (failed = ssh) => {
|
|
352
|
+
if (ssh === failed)
|
|
353
|
+
ssh = undefined;
|
|
354
|
+
failed?.close();
|
|
355
|
+
};
|
|
356
|
+
const abort = () => drop();
|
|
357
|
+
options.signal?.addEventListener("abort", abort, { once: true });
|
|
358
|
+
const open = async () => {
|
|
359
|
+
const opened = await connectOnce(options, (signal) => openForwarder(wireOptions(options, credentials), (key) => normalized(key.fingerprint) === pinned, signal));
|
|
360
|
+
if (options.signal?.aborted || closed) {
|
|
361
|
+
opened.close();
|
|
362
|
+
throw stoppedBy(options.signal) ?? closedError();
|
|
363
|
+
}
|
|
364
|
+
ssh = opened;
|
|
365
|
+
return opened;
|
|
366
|
+
};
|
|
367
|
+
const connected = async () => {
|
|
368
|
+
if (closed)
|
|
369
|
+
throw closedError();
|
|
370
|
+
if (ssh && !ssh.closed)
|
|
371
|
+
return ssh;
|
|
372
|
+
connecting ??= open().finally(() => {
|
|
373
|
+
connecting = undefined;
|
|
374
|
+
});
|
|
375
|
+
return connecting;
|
|
376
|
+
};
|
|
377
|
+
try {
|
|
378
|
+
await retrying(target, { operation: "ssh.connect", transport: "ssh", run: () => connected() });
|
|
379
|
+
}
|
|
380
|
+
catch (error) {
|
|
381
|
+
options.signal?.removeEventListener("abort", abort);
|
|
382
|
+
throw error;
|
|
383
|
+
}
|
|
384
|
+
const operationMs = SFTP_CONNECT_TIMEOUT_MS;
|
|
385
|
+
return {
|
|
386
|
+
connect: (host, port) => retrying(target, {
|
|
387
|
+
operation: "ssh.forward",
|
|
388
|
+
transport: "ssh",
|
|
389
|
+
run: async () => {
|
|
390
|
+
const current = await connected();
|
|
391
|
+
try {
|
|
392
|
+
return await deadline(options.connectTimeoutMs ?? operationMs, options.sleep, () => current.close(), () => current.forward(host, port));
|
|
393
|
+
}
|
|
394
|
+
catch (error) {
|
|
395
|
+
if (error instanceof Expired) {
|
|
396
|
+
drop(current);
|
|
397
|
+
throw new CliError("timeout", "the SSH server did not open the forwarded connection in time", {
|
|
398
|
+
reason: "sftp.timeout",
|
|
399
|
+
retryable: true,
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
if (isCliError(error) && error.details.retryable === true && error.details.reason !== "ssh.forward-failed")
|
|
403
|
+
drop(current);
|
|
404
|
+
throw error;
|
|
405
|
+
}
|
|
406
|
+
},
|
|
407
|
+
}),
|
|
224
408
|
close: async () => {
|
|
225
409
|
closed = true;
|
|
226
410
|
options.signal?.removeEventListener("abort", abort);
|
package/dist/sftp/ssh2.d.ts
CHANGED
|
@@ -22,17 +22,32 @@ export interface WireOptions {
|
|
|
22
22
|
/** Opens a stream already connected to the server, instead of a TCP connection to `host`. */
|
|
23
23
|
socket?: (() => Duplex | Promise<Duplex>) | undefined;
|
|
24
24
|
}
|
|
25
|
+
/** An open file, read by position: a stream reads it piece by piece and never holds it whole. */
|
|
26
|
+
export interface OpenFile {
|
|
27
|
+
size: number;
|
|
28
|
+
/** Whole seconds since 1970, to tell a file replaced between two connections. */
|
|
29
|
+
mtime: number;
|
|
30
|
+
/** Up to `length` bytes from `position`, fewer only at the end of the file. */
|
|
31
|
+
read(position: number, length: number): Promise<Buffer>;
|
|
32
|
+
close(): void;
|
|
33
|
+
}
|
|
25
34
|
export interface Wire {
|
|
26
35
|
list(path: string): Promise<SftpEntry[]>;
|
|
27
36
|
stat(path: string): Promise<SftpEntry>;
|
|
28
37
|
read(path: string, maxBytes: number, progress: () => void): Promise<Uint8Array>;
|
|
38
|
+
open(path: string): Promise<OpenFile>;
|
|
39
|
+
close(): void;
|
|
40
|
+
}
|
|
41
|
+
/** An SSH connection that only forwards: each `forward` is one TCP connection the server opens on the owner's behalf. */
|
|
42
|
+
export interface Forwarder {
|
|
43
|
+
forward(host: string, port: number): Promise<Duplex>;
|
|
29
44
|
close(): void;
|
|
45
|
+
readonly closed: boolean;
|
|
30
46
|
}
|
|
31
47
|
export declare const hostKeyOf: (blob: Buffer) => HostKey;
|
|
32
|
-
/**
|
|
33
|
-
* Connects, checks the host key with `accept` before any credential is sent, and opens the SFTP channel. `signal`
|
|
34
|
-
* ends the attempt and destroys the connection.
|
|
35
|
-
*/
|
|
48
|
+
/** Signs in as `connectClient` does and opens the SFTP channel. */
|
|
36
49
|
export declare const openWire: (options: WireOptions, accept: (key: HostKey) => boolean, signal: AbortSignal) => Promise<Wire>;
|
|
50
|
+
/** Signs in as `connectClient` does, for forwarding only: no shell, no command, no SFTP. */
|
|
51
|
+
export declare const openForwarder: (options: WireOptions, accept: (key: HostKey) => boolean, signal: AbortSignal) => Promise<Forwarder>;
|
|
37
52
|
/** Connects only as far as the server's host key, and returns it; no credential is sent. */
|
|
38
53
|
export declare const probeHostKey: (options: WireOptions, signal: AbortSignal) => Promise<HostKey>;
|
package/dist/sftp/ssh2.js
CHANGED
|
@@ -39,7 +39,7 @@ const CONNECTION_LOST = 7;
|
|
|
39
39
|
/** SSH disconnect reasons (RFC 4253, section 11.1) a busy or restarting server sends: service not available, connection
|
|
40
40
|
* lost, too many connections. */
|
|
41
41
|
const DISCONNECTS_TO_RETRY = new Set([7, 10, 12]);
|
|
42
|
-
const lost = () => new CliError("network_error",
|
|
42
|
+
const lost = (server = "SFTP server") => new CliError("network_error", `the connection to the ${server} broke`, {
|
|
43
43
|
reason: "network.failed",
|
|
44
44
|
retryable: true,
|
|
45
45
|
});
|
|
@@ -47,16 +47,16 @@ const lost = () => new CliError("network_error", "the connection to the SFTP ser
|
|
|
47
47
|
* The vendor's error as a CliError with fixed wording: an SFTP server's message and the path asked can name a caller's
|
|
48
48
|
* phone number, and a key error can quote the key (WC-SECR-6).
|
|
49
49
|
*/
|
|
50
|
-
const failureOf = (error, from = "sftp") => {
|
|
50
|
+
const failureOf = (error, from = "sftp", server = "SFTP server") => {
|
|
51
51
|
if (isCliError(error))
|
|
52
52
|
return error;
|
|
53
53
|
const { code, level, message } = (error ?? {});
|
|
54
54
|
if (level === "client-authentication")
|
|
55
|
-
return new CliError("authentication_error",
|
|
55
|
+
return new CliError("authentication_error", `the ${server} refused the user name and password or key`, {
|
|
56
56
|
reason: "auth.failed",
|
|
57
57
|
});
|
|
58
58
|
if (from === "ssh" && typeof code === "number")
|
|
59
|
-
return new CliError(DISCONNECTS_TO_RETRY.has(code) ? "provider_unavailable" : "provider_error", `the
|
|
59
|
+
return new CliError(DISCONNECTS_TO_RETRY.has(code) ? "provider_unavailable" : "provider_error", `the ${server} ended the connection (SSH disconnect reason ${code})`, {
|
|
60
60
|
reason: DISCONNECTS_TO_RETRY.has(code) ? "provider.unavailable" : "provider.refused",
|
|
61
61
|
...(DISCONNECTS_TO_RETRY.has(code) ? { retryable: true } : {}),
|
|
62
62
|
});
|
|
@@ -67,7 +67,7 @@ const failureOf = (error, from = "sftp") => {
|
|
|
67
67
|
if (code === NO_CONNECTION || code === CONNECTION_LOST || message === "No response from server")
|
|
68
68
|
return lost();
|
|
69
69
|
if (level === "client-dns" || code === "ENOTFOUND")
|
|
70
|
-
return new CliError("network_error",
|
|
70
|
+
return new CliError("network_error", `the ${server}'s host name was not found`, { reason: "network.failed" });
|
|
71
71
|
if ((typeof code === "string" && TRANSIENT_CODES.has(code)) ||
|
|
72
72
|
level === "client-socket" ||
|
|
73
73
|
level === "client-timeout" ||
|
|
@@ -159,6 +159,44 @@ const readAll = async (sftp, path, maxBytes, progress) => {
|
|
|
159
159
|
sftp.close(handle, () => { });
|
|
160
160
|
}
|
|
161
161
|
};
|
|
162
|
+
const openFile = async (sftp, path, guarded) => {
|
|
163
|
+
const call = (work) => new Promise((resolve, reject) => work((error, value) => (error ? reject(failureOf(error)) : resolve(value))));
|
|
164
|
+
const handle = await call((done) => sftp.open(path, "r", (error, value) => done(error, value)));
|
|
165
|
+
const close = () => sftp.close(handle, () => { });
|
|
166
|
+
try {
|
|
167
|
+
const stats = await call((done) => sftp.fstat(handle, (error, value) => done(error, value)));
|
|
168
|
+
if (kindOf(stats.mode) !== "file")
|
|
169
|
+
throw new CliError("validation_error", "the path names a folder or another thing that is not a file", {
|
|
170
|
+
reason: "file.not-a-file",
|
|
171
|
+
});
|
|
172
|
+
const readAt = (position, length) => call((done) => {
|
|
173
|
+
const into = Buffer.alloc(length);
|
|
174
|
+
sftp.read(handle, into, 0, length, position, (error, bytes) => done(error, into.subarray(0, bytes)));
|
|
175
|
+
});
|
|
176
|
+
return {
|
|
177
|
+
size: Number(stats.size),
|
|
178
|
+
mtime: stats.mtime,
|
|
179
|
+
// A server may answer a read with fewer bytes than asked; only an empty answer is the end of the file.
|
|
180
|
+
read: (position, length) => guarded(async () => {
|
|
181
|
+
const parts = [];
|
|
182
|
+
let got = 0;
|
|
183
|
+
while (got < length) {
|
|
184
|
+
const part = await readAt(position + got, length - got);
|
|
185
|
+
if (part.length === 0)
|
|
186
|
+
break;
|
|
187
|
+
parts.push(part);
|
|
188
|
+
got += part.length;
|
|
189
|
+
}
|
|
190
|
+
return Buffer.concat(parts, got);
|
|
191
|
+
}),
|
|
192
|
+
close,
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
catch (error) {
|
|
196
|
+
close();
|
|
197
|
+
throw error;
|
|
198
|
+
}
|
|
199
|
+
};
|
|
162
200
|
const validKey = (library, options) => {
|
|
163
201
|
if (options.privateKey === undefined)
|
|
164
202
|
return;
|
|
@@ -189,10 +227,10 @@ const configOf = async (options, verify) => ({
|
|
|
189
227
|
hostVerifier: verify,
|
|
190
228
|
});
|
|
191
229
|
/**
|
|
192
|
-
* Connects, checks the host key with `accept` before any credential is sent, and
|
|
193
|
-
*
|
|
230
|
+
* Connects, checks the host key with `accept` before any credential is sent, and signs in. `signal` ends the attempt
|
|
231
|
+
* and destroys the connection.
|
|
194
232
|
*/
|
|
195
|
-
|
|
233
|
+
const connectClient = async (options, accept, signal, server) => {
|
|
196
234
|
const library = await ssh2();
|
|
197
235
|
validKey(library, options);
|
|
198
236
|
signal.throwIfAborted();
|
|
@@ -207,7 +245,7 @@ export const openWire = async (options, accept, signal) => {
|
|
|
207
245
|
};
|
|
208
246
|
const config = await configOf(options, verify);
|
|
209
247
|
signal.throwIfAborted();
|
|
210
|
-
|
|
248
|
+
return new Promise((resolve, reject) => {
|
|
211
249
|
const stop = () => {
|
|
212
250
|
client.destroy();
|
|
213
251
|
reject(signal.reason);
|
|
@@ -217,24 +255,112 @@ export const openWire = async (options, accept, signal) => {
|
|
|
217
255
|
signal.removeEventListener("abort", stop);
|
|
218
256
|
client.destroy();
|
|
219
257
|
reject(refused
|
|
220
|
-
? new CliError("authentication_error", `the
|
|
258
|
+
? new CliError("authentication_error", `the ${server}'s host key (${refused.algorithm} ${refused.fingerprint}) is not the pinned one; ` +
|
|
221
259
|
"check it with the server's owner before pinning it again", { reason: "sftp.host-key-mismatch" })
|
|
222
|
-
: failureOf(error, "ssh"));
|
|
260
|
+
: failureOf(error, "ssh", server));
|
|
223
261
|
};
|
|
262
|
+
const closed = () => fail(lost(server));
|
|
224
263
|
client.once("error", fail);
|
|
225
|
-
client.once("close",
|
|
226
|
-
client.once("ready", () =>
|
|
227
|
-
if (error)
|
|
228
|
-
return fail(error);
|
|
264
|
+
client.once("close", closed);
|
|
265
|
+
client.once("ready", () => {
|
|
229
266
|
signal.removeEventListener("abort", stop);
|
|
230
267
|
client.removeListener("error", fail);
|
|
268
|
+
client.removeListener("close", closed);
|
|
231
269
|
client.on("error", () => { });
|
|
232
|
-
resolve(
|
|
233
|
-
})
|
|
270
|
+
resolve(client);
|
|
271
|
+
});
|
|
234
272
|
client.connect(config);
|
|
235
273
|
});
|
|
274
|
+
};
|
|
275
|
+
/** Signs in as `connectClient` does and opens the SFTP channel. */
|
|
276
|
+
export const openWire = async (options, accept, signal) => {
|
|
277
|
+
const client = await connectClient(options, accept, signal, "SFTP server");
|
|
278
|
+
const sftp = await new Promise((resolve, reject) => {
|
|
279
|
+
const stop = () => {
|
|
280
|
+
client.destroy();
|
|
281
|
+
reject(signal.reason);
|
|
282
|
+
};
|
|
283
|
+
signal.addEventListener("abort", stop, { once: true });
|
|
284
|
+
client.once("close", () => reject(lost()));
|
|
285
|
+
client.sftp((error, channel) => {
|
|
286
|
+
signal.removeEventListener("abort", stop);
|
|
287
|
+
if (!error)
|
|
288
|
+
return resolve(channel);
|
|
289
|
+
client.destroy();
|
|
290
|
+
reject(failureOf(error, "ssh"));
|
|
291
|
+
});
|
|
292
|
+
});
|
|
236
293
|
return wireOf(client, sftp);
|
|
237
294
|
};
|
|
295
|
+
/** SSH channel-open failure reasons (RFC 4254, section 5.1). */
|
|
296
|
+
const PROHIBITED = 1;
|
|
297
|
+
const CONNECT_FAILED = 2;
|
|
298
|
+
const forwardFailure = (error) => {
|
|
299
|
+
const reason = error?.reason;
|
|
300
|
+
if (reason === PROHIBITED)
|
|
301
|
+
return new CliError("permission_error", "the SSH server does not allow forwarding to that address", {
|
|
302
|
+
reason: "ssh.forward-refused",
|
|
303
|
+
});
|
|
304
|
+
if (reason === CONNECT_FAILED)
|
|
305
|
+
return new CliError("network_error", "the SSH server could not reach the address behind it", {
|
|
306
|
+
reason: "ssh.forward-failed",
|
|
307
|
+
retryable: true,
|
|
308
|
+
});
|
|
309
|
+
return failureOf(error, "ssh", "SSH server");
|
|
310
|
+
};
|
|
311
|
+
/**
|
|
312
|
+
* What a database driver expects of a socket, on a forwarded channel: `pg` calls `connect` and waits for its
|
|
313
|
+
* `connect` event, and both drivers set keep-alive and Nagle options that mean nothing inside an SSH channel.
|
|
314
|
+
*/
|
|
315
|
+
const socketLike = (channel) => {
|
|
316
|
+
const same = () => channel;
|
|
317
|
+
return Object.assign(channel, {
|
|
318
|
+
connect: () => {
|
|
319
|
+
setImmediate(() => channel.emit("connect"));
|
|
320
|
+
return channel;
|
|
321
|
+
},
|
|
322
|
+
setNoDelay: same,
|
|
323
|
+
setKeepAlive: same,
|
|
324
|
+
setTimeout: same,
|
|
325
|
+
ref: same,
|
|
326
|
+
unref: same,
|
|
327
|
+
});
|
|
328
|
+
};
|
|
329
|
+
/** Signs in as `connectClient` does, for forwarding only: no shell, no command, no SFTP. */
|
|
330
|
+
export const openForwarder = async (options, accept, signal) => {
|
|
331
|
+
const client = await connectClient(options, accept, signal, "SSH server");
|
|
332
|
+
let closed = false;
|
|
333
|
+
const channels = new Set();
|
|
334
|
+
const close = () => {
|
|
335
|
+
if (closed)
|
|
336
|
+
return;
|
|
337
|
+
closed = true;
|
|
338
|
+
for (const channel of channels)
|
|
339
|
+
channel.destroy();
|
|
340
|
+
client.destroy();
|
|
341
|
+
};
|
|
342
|
+
client.once("close", close);
|
|
343
|
+
return {
|
|
344
|
+
get closed() {
|
|
345
|
+
return closed;
|
|
346
|
+
},
|
|
347
|
+
forward: (host, port) => new Promise((resolve, reject) => {
|
|
348
|
+
if (closed)
|
|
349
|
+
return reject(lost("SSH server"));
|
|
350
|
+
const gone = () => reject(lost("SSH server"));
|
|
351
|
+
client.once("close", gone);
|
|
352
|
+
client.forwardOut("127.0.0.1", 0, host, port, (error, channel) => {
|
|
353
|
+
client.removeListener("close", gone);
|
|
354
|
+
if (error)
|
|
355
|
+
return reject(forwardFailure(error));
|
|
356
|
+
channels.add(channel);
|
|
357
|
+
channel.once("close", () => channels.delete(channel));
|
|
358
|
+
resolve(socketLike(channel));
|
|
359
|
+
});
|
|
360
|
+
}),
|
|
361
|
+
close,
|
|
362
|
+
};
|
|
363
|
+
};
|
|
238
364
|
const wireOf = (client, sftp) => {
|
|
239
365
|
const pending = new Set();
|
|
240
366
|
let closed = false;
|
|
@@ -271,6 +397,7 @@ const wireOf = (client, sftp) => {
|
|
|
271
397
|
.map((one) => entryOf(one.filename, one.attrs)))))),
|
|
272
398
|
stat: (path) => guarded(() => new Promise((resolve, reject) => sftp.stat(path, (error, attrs) => (error ? reject(error) : resolve(entryOf(basename(path), attrs)))))),
|
|
273
399
|
read: (path, maxBytes, progress) => guarded(() => readAll(sftp, path, maxBytes, progress)),
|
|
400
|
+
open: (path) => guarded(() => openFile(sftp, path, guarded)),
|
|
274
401
|
close,
|
|
275
402
|
};
|
|
276
403
|
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { type KeyringStore } from "@wirecat/cli-base";
|
|
2
|
+
import type { Settings } from "../cli/settings.js";
|
|
3
|
+
import { type Fetch } from "../embeddings/remote.js";
|
|
4
|
+
import { type AppIdentity } from "../ports/app.js";
|
|
5
|
+
import type { Pcm } from "./decode.js";
|
|
6
|
+
import type { TimedText } from "./recognize.js";
|
|
7
|
+
export interface CloudSpeechDeps {
|
|
8
|
+
app: AppIdentity;
|
|
9
|
+
env: NodeJS.ProcessEnv;
|
|
10
|
+
keyring?: KeyringStore | undefined;
|
|
11
|
+
/**
|
|
12
|
+
* The profile's settings. Only the profile's own `transcribeWith: "openai"` turns cloud speech on, never the shared
|
|
13
|
+
* defaults: sending a business's calls away is decided for one profile at a time.
|
|
14
|
+
*/
|
|
15
|
+
settings: Pick<Settings, "configured">;
|
|
16
|
+
/** Called once, before the first audio leaves the computer, with the line the run shows on stderr. */
|
|
17
|
+
notify: (line: string) => void;
|
|
18
|
+
fetch?: Fetch;
|
|
19
|
+
signal?: AbortSignal | undefined;
|
|
20
|
+
/** The voice detector model that cuts the speech into pieces; the shared models directory's when not given. */
|
|
21
|
+
vadModel?: string;
|
|
22
|
+
}
|
|
23
|
+
export interface CloudSpeech {
|
|
24
|
+
/**
|
|
25
|
+
* One channel's speech as timed pieces. Each stretch of speech is sent alone, at most ten minutes, under the
|
|
26
|
+
* provider's 25 MB per request; `bundle` sends fewer, longer pieces when the turns do not matter.
|
|
27
|
+
*/
|
|
28
|
+
recognize(audio: Pcm, options?: {
|
|
29
|
+
bundle?: boolean;
|
|
30
|
+
}): Promise<TimedText[]>;
|
|
31
|
+
/** What a host keeps with the transcript: `openai`, or the host name of a compatible service, and the model. */
|
|
32
|
+
recognition: {
|
|
33
|
+
engine: string;
|
|
34
|
+
model: string;
|
|
35
|
+
};
|
|
36
|
+
/** The address the audio goes to, `https://api.openai.com/v1` or the shared AI model's `--base-url`. */
|
|
37
|
+
baseUrl: string;
|
|
38
|
+
/** False only for a compatible service on this computer. */
|
|
39
|
+
leavesComputer: boolean;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Speech to text by an OpenAI-compatible `/audio/transcriptions`, for a host that transcribes recordings (calls,
|
|
43
|
+
* meetings). `null` unless the profile set `transcribeWith` to `openai`: the default is a local model and nothing
|
|
44
|
+
* leaves the computer. Refuses, before any audio is read, in a read-only process, with local only on for an address
|
|
45
|
+
* off this computer, and without a key for the address.
|
|
46
|
+
*/
|
|
47
|
+
export declare const cloudSpeech: (deps: CloudSpeechDeps) => CloudSpeech | null;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { CliError } from "@wirecat/cli-base";
|
|
2
|
+
import { OPENAI_URL } from "../embeddings/remote.js";
|
|
3
|
+
import { aiKeys, aiOrigin, aiSettingPath, readAiSetting } from "../models/ai.js";
|
|
4
|
+
import { onThisComputer } from "../models/endpoint.js";
|
|
5
|
+
import { localOnly, localOnlyRefusal } from "../models/local-only.js";
|
|
6
|
+
import { readOnlyLockOf, readOnlyRefusal } from "../ports/app.js";
|
|
7
|
+
import { typedCommand } from "../runs/scope.js";
|
|
8
|
+
import { OPENAI_TRANSCRIBE_MODEL } from "./models.js";
|
|
9
|
+
/**
|
|
10
|
+
* Where the audio goes: the shared AI model's address when it is OpenAI or compatible (`models ai set openai
|
|
11
|
+
* --base-url …`), else OpenAI's own. The key is the one kept for that address, never another's.
|
|
12
|
+
*/
|
|
13
|
+
const addressOf = (env) => {
|
|
14
|
+
const setting = readAiSetting(aiSettingPath(env));
|
|
15
|
+
return setting?.provider === "openai" && setting.base_url ? setting.base_url : OPENAI_URL;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Speech to text by an OpenAI-compatible `/audio/transcriptions`, for a host that transcribes recordings (calls,
|
|
19
|
+
* meetings). `null` unless the profile set `transcribeWith` to `openai`: the default is a local model and nothing
|
|
20
|
+
* leaves the computer. Refuses, before any audio is read, in a read-only process, with local only on for an address
|
|
21
|
+
* off this computer, and without a key for the address.
|
|
22
|
+
*/
|
|
23
|
+
export const cloudSpeech = (deps) => {
|
|
24
|
+
if (deps.settings.configured.transcribeWith !== "openai")
|
|
25
|
+
return null;
|
|
26
|
+
const baseUrl = addressOf(deps.env);
|
|
27
|
+
const origin = aiOrigin({ provider: "openai", base_url: baseUrl });
|
|
28
|
+
const host = new URL(origin).host;
|
|
29
|
+
const official = origin === "https://api.openai.com";
|
|
30
|
+
const leavesComputer = !onThisComputer(baseUrl);
|
|
31
|
+
const lock = readOnlyLockOf(deps.app, deps.env);
|
|
32
|
+
if (lock && leavesComputer)
|
|
33
|
+
throw readOnlyRefusal(lock, `send audio to ${host}`);
|
|
34
|
+
if (leavesComputer && localOnly(deps.env))
|
|
35
|
+
throw localOnlyRefusal("the recordings", host);
|
|
36
|
+
const apiKey = aiKeys(deps.app, deps.env, deps.keyring).read(origin)?.key;
|
|
37
|
+
const setKey = typedCommand(deps.app.command, official
|
|
38
|
+
? "models ai set openai --model <model> --secret-stdin"
|
|
39
|
+
: `models ai set openai --base-url ${baseUrl} --secret-stdin`);
|
|
40
|
+
if (!apiKey && leavesComputer)
|
|
41
|
+
throw new CliError("authentication_error", `no key for ${host} — \`${setKey}\`${official ? ", or OPENAI_API_KEY" : ""}`, {
|
|
42
|
+
reason: "transcription.no-key",
|
|
43
|
+
});
|
|
44
|
+
const configuredModel = deps.settings.configured.transcribeModel;
|
|
45
|
+
const model = typeof configuredModel === "string" && configuredModel.trim() ? configuredModel.trim() : OPENAI_TRANSCRIBE_MODEL;
|
|
46
|
+
let told = false;
|
|
47
|
+
return {
|
|
48
|
+
baseUrl,
|
|
49
|
+
leavesComputer,
|
|
50
|
+
recognition: { engine: official ? "openai" : host, model },
|
|
51
|
+
recognize: async (audio, options = {}) => {
|
|
52
|
+
if (!told) {
|
|
53
|
+
told = true;
|
|
54
|
+
deps.notify(leavesComputer
|
|
55
|
+
? `the recordings' audio leaves this computer: it goes to ${host} (${model}) to be transcribed`
|
|
56
|
+
: `the recordings' audio goes to ${host} on this computer (${model}) to be transcribed`);
|
|
57
|
+
}
|
|
58
|
+
const { transcribeOpenAI } = await import("./openai.js");
|
|
59
|
+
return transcribeOpenAI(audio, {
|
|
60
|
+
apiKey: apiKey ?? "",
|
|
61
|
+
model,
|
|
62
|
+
baseUrl,
|
|
63
|
+
label: official ? "OpenAI" : host,
|
|
64
|
+
keyHint: setKey,
|
|
65
|
+
env: deps.env,
|
|
66
|
+
...(options.bundle ? { bundle: true } : {}),
|
|
67
|
+
...(deps.vadModel ? { vadModel: deps.vadModel } : {}),
|
|
68
|
+
...(deps.fetch ? { fetch: deps.fetch } : {}),
|
|
69
|
+
...(deps.signal ? { signal: deps.signal } : {}),
|
|
70
|
+
});
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
};
|