@wirecat/cli-core 0.640.0 → 0.642.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 +80 -0
- package/dist/analysis/provider.d.ts +1 -1
- package/dist/analysis/provider.js +2 -1
- package/dist/attachments/gateway-ocr.js +13 -8
- package/dist/background/schedules.d.ts +5 -1
- package/dist/background/schedules.js +28 -3
- package/dist/calendar/mcp/index.d.ts +12 -12
- package/dist/cli/ai-model-command.js +6 -3
- package/dist/cli/analysis-choice.js +20 -4
- package/dist/cli/enrichment-consents-command.js +2 -0
- package/dist/cli/local-only-command.d.ts +10 -0
- package/dist/cli/local-only-command.js +103 -0
- package/dist/cli/messenger/backfill-command.js +16 -1
- package/dist/cli/messenger/conversations-command.js +1 -1
- package/dist/cli/messenger/replies-consents-command.js +12 -6
- package/dist/cli/messenger/search-answers.js +2 -0
- package/dist/cli/model-catalogue-command.js +2 -0
- package/dist/cli/search-harder-consents-command.js +16 -1
- package/dist/cli/store-settings.d.ts +4 -1
- package/dist/cli/store-settings.js +26 -9
- package/dist/crm/cli/csv.js +2 -0
- package/dist/crm/cli/index.js +8 -2
- package/dist/crm/cli/search-all.js +4 -4
- package/dist/crm/stage-tasks.d.ts +8 -0
- package/dist/crm/stage-tasks.js +42 -0
- package/dist/documents.d.ts +1 -1
- package/dist/documents.js +1 -1
- package/dist/domain/crm.d.ts +6 -0
- package/dist/embeddings/remote.d.ts +2 -1
- package/dist/embeddings/remote.js +5 -3
- package/dist/entities/deal.d.ts +6 -0
- package/dist/entities/deal.js +3 -0
- package/dist/entities/index.d.ts +4 -0
- package/dist/errors/reasons.js +8 -0
- package/dist/meetings/mcp/index.d.ts +15 -15
- package/dist/models/ai.d.ts +16 -3
- package/dist/models/ai.js +19 -13
- package/dist/models/endpoint.d.ts +5 -0
- package/dist/models/endpoint.js +13 -0
- package/dist/models/index.d.ts +4 -0
- package/dist/models/index.js +9 -1
- package/dist/models/local-only.d.ts +17 -0
- package/dist/models/local-only.js +42 -0
- package/dist/models/purpose.d.ts +24 -0
- package/dist/models/purpose.js +26 -0
- package/dist/replies/rendering.js +10 -10
- package/dist/runs/events.d.ts +9 -1
- package/dist/runs/events.js +5 -1
- package/dist/runs/log-events.js +2 -0
- package/dist/services/deal-context.js +17 -0
- package/dist/services/embeddings-maintenance.js +4 -1
- package/dist/services/enrichment.d.ts +1 -1
- package/dist/services/enrichment.js +27 -0
- package/dist/services/meeting-model.js +1 -1
- package/dist/services/message-resolution.d.ts +1 -1
- package/dist/services/message-resolution.js +2 -2
- package/dist/services/messages-search.js +12 -5
- package/dist/services/search-all.d.ts +10 -3
- package/dist/services/search-all.js +10 -4
- package/dist/services/search-harder.d.ts +11 -1
- package/dist/services/search-harder.js +38 -5
- package/dist/speech/openai.js +2 -0
- package/dist/speech/transcribe.d.ts +5 -2
- package/dist/speech/transcribe.js +14 -5
- package/dist/store/index.d.ts +1 -1
- package/dist/store/sqlite/crm-rows.js +2 -0
- package/dist/store/sqlite/crm-settings.d.ts +26 -1
- package/dist/store/sqlite/crm-settings.js +104 -1
- package/dist/store/sqlite/crm.d.ts +12 -0
- package/dist/store/sqlite/crm.js +4 -1
- package/dist/store/sqlite/deals.d.ts +28 -5
- package/dist/store/sqlite/deals.js +158 -14
- package/dist/store/sqlite/lucene-fields.js +56 -3
- package/dist/store/sqlite/tasks.js +9 -8
- package/dist/tasks/model.d.ts +6 -2
- package/dist/tasks/service.d.ts +2 -0
- package/package.json +1 -1
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { CliError } from "@wirecat/cli-base";
|
|
2
2
|
import * as v from "valibot";
|
|
3
3
|
import { embeddingKeys, endpointKeyName } from "../embeddings/keys.js";
|
|
4
|
+
import { modelAddressAllowed, onThisComputer } from "../models/endpoint.js";
|
|
4
5
|
import { modelGateway } from "../models/index.js";
|
|
6
|
+
import { localOnly } from "../models/local-only.js";
|
|
5
7
|
import { costOf, DEFAULT_OFFER, enrichmentEndpoint, enrichmentPrice, estimateEnrichment, offeredModels, readEnrichmentSetting, } from "./enrichment-setting.js";
|
|
6
8
|
/** One sync's share, as embedding is bounded per run; the next sync goes on where it stopped. */
|
|
7
9
|
export const ENRICH_PER_RUN = 600;
|
|
@@ -62,6 +64,8 @@ export const ENRICHMENT_REASON_IDS = [
|
|
|
62
64
|
"enrichment.rate-limited",
|
|
63
65
|
"enrichment.bad-replies",
|
|
64
66
|
"enrichment.cancelled",
|
|
67
|
+
"enrichment.local-only",
|
|
68
|
+
"enrichment.address-refused",
|
|
65
69
|
"enrichment.error",
|
|
66
70
|
];
|
|
67
71
|
const REASON_TEXT = {
|
|
@@ -76,6 +80,8 @@ const reasonIdOf = (error) => {
|
|
|
76
80
|
return "enrichment.key-rejected";
|
|
77
81
|
if (error.details.reason === "model.unreachable")
|
|
78
82
|
return "enrichment.unreachable";
|
|
83
|
+
if (error.details.reason === "local-only.refused")
|
|
84
|
+
return "enrichment.local-only";
|
|
79
85
|
if (error.code === "rate_limited")
|
|
80
86
|
return "enrichment.rate-limited";
|
|
81
87
|
if (error.code === "cancelled")
|
|
@@ -126,6 +132,26 @@ export const enrichRecords = async (store, options) => {
|
|
|
126
132
|
const target = { provider: setting.provider, model: setting.model, baseUrl };
|
|
127
133
|
const apiKey = keyLookup(options, setting)?.key;
|
|
128
134
|
const maxRecords = options.maxRecords ?? ENRICH_PER_RUN;
|
|
135
|
+
const refused = !modelAddressAllowed(baseUrl)
|
|
136
|
+
? "enrichment.address-refused"
|
|
137
|
+
: !onThisComputer(baseUrl) && localOnly(options.env)
|
|
138
|
+
? "enrichment.local-only"
|
|
139
|
+
: undefined;
|
|
140
|
+
if (refused)
|
|
141
|
+
return {
|
|
142
|
+
status: "stopped",
|
|
143
|
+
provider: setting.provider,
|
|
144
|
+
model: setting.model,
|
|
145
|
+
enriched: 0,
|
|
146
|
+
failed: 0,
|
|
147
|
+
left: (await enrichment.pending(Number.MAX_SAFE_INTEGER, options.narrow)).length,
|
|
148
|
+
usage: { input: 0, output: 0 },
|
|
149
|
+
cost_usd: 0,
|
|
150
|
+
reason: refused === "enrichment.local-only"
|
|
151
|
+
? "local only is on and the model is not on this computer; nothing was sent"
|
|
152
|
+
: "the address is plain http to another computer: use https, or http only on this computer; nothing was sent",
|
|
153
|
+
reason_id: refused,
|
|
154
|
+
};
|
|
129
155
|
// Without a key the public API answers 401 only after the records' text has left; a custom server may need none.
|
|
130
156
|
if (apiKey === undefined && setting.base_url === undefined)
|
|
131
157
|
return {
|
|
@@ -145,6 +171,7 @@ export const enrichRecords = async (store, options) => {
|
|
|
145
171
|
// The setting exists only with the owner's yes for this provider and endpoint; the endpoint is checked again.
|
|
146
172
|
consent: (_request, chosen) => chosen.provider === setting.provider && chosen.baseUrl === baseUrl,
|
|
147
173
|
key: () => apiKey,
|
|
174
|
+
env: options.env,
|
|
148
175
|
...(options.adapters ? { adapters: options.adapters } : {}),
|
|
149
176
|
...(options.fetch ? { fetch: options.fetch } : {}),
|
|
150
177
|
...(options.signal ? { signal: options.signal } : {}),
|
|
@@ -179,7 +179,7 @@ export const openMeetingEmbedder = async (choice, options = {}) => {
|
|
|
179
179
|
throw error;
|
|
180
180
|
}
|
|
181
181
|
};
|
|
182
|
-
engine = openRemote(remote, key, { concurrency: 1, fetch: guarded, tries: 1 });
|
|
182
|
+
engine = openRemote(remote, key, { concurrency: 1, fetch: guarded, tries: 1, env: options.env ?? process.env });
|
|
183
183
|
}
|
|
184
184
|
if (signal?.aborted) {
|
|
185
185
|
await engine.close();
|
|
@@ -30,7 +30,7 @@ export declare const chatAmong: (messenger: Saved, store: MessageStore, accounts
|
|
|
30
30
|
chatId: Id;
|
|
31
31
|
}>;
|
|
32
32
|
/** A sender, through the names each messenger's accounts have seen; `from:` names one person. */
|
|
33
|
-
export declare const senderAmong: (store: MessageStore, accounts: AccountKey[], reference: string) => Promise<{
|
|
33
|
+
export declare const senderAmong: (store: MessageStore, accounts: AccountKey[], reference: string, field?: string) => Promise<{
|
|
34
34
|
provider: string;
|
|
35
35
|
id: string;
|
|
36
36
|
}>;
|
|
@@ -149,7 +149,7 @@ export const chatAmong = async (messenger, store, accounts, reference) => {
|
|
|
149
149
|
})));
|
|
150
150
|
};
|
|
151
151
|
/** A sender, through the names each messenger's accounts have seen; `from:` names one person. */
|
|
152
|
-
export const senderAmong = async (store, accounts, reference) => {
|
|
152
|
+
export const senderAmong = async (store, accounts, reference, field = "from") => {
|
|
153
153
|
const providers = [...new Set(accounts.map(({ provider }) => provider))];
|
|
154
154
|
const [only] = accounts;
|
|
155
155
|
try {
|
|
@@ -175,7 +175,7 @@ export const senderAmong = async (store, accounts, reference) => {
|
|
|
175
175
|
}
|
|
176
176
|
catch (error) {
|
|
177
177
|
if (error instanceof CliError && error.code === "not_found") {
|
|
178
|
-
throw new CliError("not_found",
|
|
178
|
+
throw new CliError("not_found", `${field}:${reference} — ${error.message}`);
|
|
179
179
|
}
|
|
180
180
|
throw error;
|
|
181
181
|
}
|
|
@@ -14,7 +14,7 @@ import { mailAround, mergeMail, withMailThreads } from "./mail-search.js";
|
|
|
14
14
|
import { chatAmong, chatsMatchingIn, senderAmong, sendersMatching } from "./message-resolution.js";
|
|
15
15
|
import { accountsOfKind, listedAccounts, MAIL } from "./search-kind.js";
|
|
16
16
|
const PERSON_FIELDS = new Set(["from", "to", "cc", "bcc"]);
|
|
17
|
-
const MAIL_FIELDS = new Set(["to", "cc", "bcc", "subject", "mailbox", "label", "is"]);
|
|
17
|
+
const MAIL_FIELDS = new Set(["to", "cc", "bcc", "subject", "mailbox", "label", "is", "list"]);
|
|
18
18
|
import { dateRange, parseLucene, timezoneOf, walkQuery, } from "@wirecat/cli-base/query";
|
|
19
19
|
import { MESSAGE_PARSING, searchError } from "../search/lucene/types.js";
|
|
20
20
|
const positiveSources = (node) => node.kind === "predicate"
|
|
@@ -110,7 +110,7 @@ export const prepareLucene = async (store, account, request, messenger) => {
|
|
|
110
110
|
if (person && node.operator === "wildcard")
|
|
111
111
|
return anyOf(node, await sendersMatching(store, scopeAccounts, node.field, node.value), (sender) => ({ sender }));
|
|
112
112
|
if (person)
|
|
113
|
-
return { ...node, resolution: { sender: await senderAmong(store, scopeAccounts, node.value) } };
|
|
113
|
+
return { ...node, resolution: { sender: await senderAmong(store, scopeAccounts, node.value, node.field) } };
|
|
114
114
|
if (node.field === "from")
|
|
115
115
|
return { ...node, resolution: { outgoing: true } };
|
|
116
116
|
if (node.field === "date")
|
|
@@ -324,14 +324,19 @@ export const searchLucene = async (store, account, request, messenger = {}) => {
|
|
|
324
324
|
const prepared = await prepareLucene(store, account, request, messenger);
|
|
325
325
|
const execute = store.queries.matchQuery.bind(store.queries);
|
|
326
326
|
const mailAccounts = prepared.scopeAccounts.filter(({ provider }) => provider === MAIL);
|
|
327
|
+
let mailLeftOut;
|
|
327
328
|
const inMailTables = async () => {
|
|
328
329
|
try {
|
|
329
330
|
return await execute({ ...prepared.execution, accounts: mailAccounts, corpus: "mail" });
|
|
330
331
|
}
|
|
331
332
|
catch (error) {
|
|
332
|
-
// A messenger field (`kind:`, `topic:`) leaves mail out of a search that names messenger accounts too
|
|
333
|
-
|
|
333
|
+
// A messenger field (`kind:`, `topic:`) leaves mail out of a search that names messenger accounts too; any
|
|
334
|
+
// other refusal (`*word*` over a large mailbox) is said, or the answer would look complete.
|
|
335
|
+
if (request.kind !== "mail" && error instanceof CliError && error.code === "validation_error") {
|
|
336
|
+
if (error.details.reason !== "query.unsupported-field")
|
|
337
|
+
mailLeftOut = { source: MAIL, code: "validation_error", reason: error.message };
|
|
334
338
|
return { items: [], hasMore: false };
|
|
339
|
+
}
|
|
335
340
|
throw error;
|
|
336
341
|
}
|
|
337
342
|
};
|
|
@@ -367,7 +372,9 @@ export const searchLucene = async (store, account, request, messenger = {}) => {
|
|
|
367
372
|
completeness,
|
|
368
373
|
query: queryOf(prepared.timezone, request.newest, prepared.stemming),
|
|
369
374
|
...(coverage ? { coverage } : {}),
|
|
370
|
-
...(prepared.skipped
|
|
375
|
+
...(prepared.skipped || mailLeftOut
|
|
376
|
+
? { skipped: [...(prepared.skipped ?? []), ...(mailLeftOut ? [mailLeftOut] : [])] }
|
|
377
|
+
: {}),
|
|
371
378
|
...(request.kind === "mail"
|
|
372
379
|
? { accounts: mailAccounts.map(({ provider, account }) => ({ resource: "mail", provider, account })) }
|
|
373
380
|
: {}),
|
|
@@ -38,8 +38,15 @@ export interface SearchAllRecordItem {
|
|
|
38
38
|
email?: MailFields;
|
|
39
39
|
/** A message's or a mail's files, as `search messages` names them. */
|
|
40
40
|
attachments?: HitFile[];
|
|
41
|
-
/** Which search found it: words, meaning or both. */
|
|
41
|
+
/** Which search found it: words, meaning or both; a deal or a lead by `title`, or `words` of its company or contacts. */
|
|
42
42
|
foundBy: ("title" | "words" | "meaning")[];
|
|
43
|
+
/** A deal's or a lead's: `corrected` when it matched only once a misspelt query word was corrected. */
|
|
44
|
+
match?: "corrected";
|
|
45
|
+
/** For a `corrected` hit, the stored words each corrected query word became. */
|
|
46
|
+
corrections?: {
|
|
47
|
+
from: string;
|
|
48
|
+
to: string[];
|
|
49
|
+
}[];
|
|
43
50
|
/** A business hit's: whether the owner confirmed it, or it waits for him. */
|
|
44
51
|
review?: "proposed" | "confirmed";
|
|
45
52
|
/** A business hit's source record, when an import made it. */
|
|
@@ -155,8 +162,8 @@ export type SearchAllMessenger = Partial<Pick<MessengerTraits, "savedChatId" | "
|
|
|
155
162
|
hintCommand?: string | null;
|
|
156
163
|
};
|
|
157
164
|
/**
|
|
158
|
-
* What a deal or a lead is found by: words or phrases
|
|
159
|
-
*
|
|
165
|
+
* What a deal or a lead is found by: plain words or phrases, all of them, or any with OR. Anything else is refused,
|
|
166
|
+
* so `search all` skips deals with the reason instead of matching a field they do not have.
|
|
160
167
|
*/
|
|
161
168
|
export declare const titleWords: (text: string, refused?: string) => {
|
|
162
169
|
words: string[];
|
|
@@ -46,10 +46,10 @@ const filtersBeside = (text) => {
|
|
|
46
46
|
: undefined;
|
|
47
47
|
};
|
|
48
48
|
/**
|
|
49
|
-
* What a deal or a lead is found by: words or phrases
|
|
50
|
-
*
|
|
49
|
+
* What a deal or a lead is found by: plain words or phrases, all of them, or any with OR. Anything else is refused,
|
|
50
|
+
* so `search all` skips deals with the reason instead of matching a field they do not have.
|
|
51
51
|
*/
|
|
52
|
-
export const titleWords = (text, refused = "deals and leads are found by the words of their title only") => {
|
|
52
|
+
export const titleWords = (text, refused = "deals and leads are found by the words of their title, company and contacts only") => {
|
|
53
53
|
const { root } = parseLucene(text, SEARCH_QUERY);
|
|
54
54
|
const clauses = root.kind === "boolean" ? root.clauses : [{ node: root, occur: "must" }];
|
|
55
55
|
const words = clauses.map(({ node, occur }) => occur !== "must_not" &&
|
|
@@ -313,11 +313,16 @@ export const searchAll = async (store, account, asked, messenger = {}, searchMes
|
|
|
313
313
|
accounts.push(...read.map(({ provider, account }) => ({ resource, provider, account })));
|
|
314
314
|
}
|
|
315
315
|
else if (resource === "crm") {
|
|
316
|
+
// `plainQuery` made `stage:won` a phrase, which would find nothing and still call the answer complete.
|
|
317
|
+
const field = /(?:^|\s)[+-]?(stage|status|pipeline):/iu.exec(asked.text)?.[1]?.toLowerCase();
|
|
318
|
+
if (field)
|
|
319
|
+
throw new CliError("validation_error", `deals and leads are not searched by ${field}; list them by it with \`deals list ${field}:<value>\``);
|
|
316
320
|
const { words, anyWord } = titleWords(request.text);
|
|
317
321
|
const chosen = scope?.crm;
|
|
318
322
|
const found = await store.deals.find({
|
|
319
323
|
words,
|
|
320
324
|
anyWord: anyWord || discover,
|
|
325
|
+
...(request.exact ? { exact: true } : {}),
|
|
321
326
|
...(request.crmKinds === undefined ? {} : { kinds: request.crmKinds }),
|
|
322
327
|
limit: Math.min(depth + 1, 500),
|
|
323
328
|
...(chosen === undefined ? {} : { accounts: chosen }),
|
|
@@ -331,7 +336,8 @@ export const searchAll = async (store, account, asked, messenger = {}, searchMes
|
|
|
331
336
|
title: deal.title,
|
|
332
337
|
timestamp: deal.updatedAt,
|
|
333
338
|
text: deal.summary,
|
|
334
|
-
foundBy: ["title"],
|
|
339
|
+
foundBy: deal.matched.includes("title") ? ["title"] : ["words"],
|
|
340
|
+
...(deal.corrections ? { match: "corrected", corrections: deal.corrections } : {}),
|
|
335
341
|
})));
|
|
336
342
|
const read = chosen ?? [...(await crmAccounts(store)), { provider: "crm", account: INTERNAL_NOTES }];
|
|
337
343
|
for (const { provider, account } of read)
|
|
@@ -12,6 +12,11 @@ export declare const HARDER_MODELS: Readonly<Record<EnrichmentProvider, string>>
|
|
|
12
12
|
/** The search lab's setup (cli-testing search-lab, "Haiku rerank"): the top 30, each cut to 400 characters. */
|
|
13
13
|
export declare const HARDER_CANDIDATES = 30;
|
|
14
14
|
export declare const HARDER_CHARS = 400;
|
|
15
|
+
/** A hosted model answers in 1 to 2 s; a model on this computer's CPU can take tens of seconds for 30 hits. */
|
|
16
|
+
export declare const HARDER_TIMEOUT_S: {
|
|
17
|
+
readonly hosted: 5;
|
|
18
|
+
readonly local: 60;
|
|
19
|
+
};
|
|
15
20
|
declare const Setting: v.StrictObjectSchema<{
|
|
16
21
|
readonly provider: v.PicklistSchema<readonly ["openai", "anthropic"], undefined>;
|
|
17
22
|
readonly model: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.TrimAction, v.MinLengthAction<string, 1, undefined>, v.MaxLengthAction<string, 200, undefined>]>;
|
|
@@ -23,7 +28,11 @@ export type HarderSetting = v.InferOutput<typeof Setting>;
|
|
|
23
28
|
/** The owner's consent, or `undefined`: no section means `--harder` sends nothing. */
|
|
24
29
|
export declare const readHarderSetting: (env: NodeJS.ProcessEnv) => HarderSetting | undefined;
|
|
25
30
|
export declare const writeHarderSetting: (env: NodeJS.ProcessEnv, setting: HarderSetting | undefined) => void;
|
|
26
|
-
|
|
31
|
+
/** The owner's time limit in seconds, or `undefined` for the default by where the model runs. */
|
|
32
|
+
export declare const readHarderTimeout: (env: NodeJS.ProcessEnv) => number | undefined;
|
|
33
|
+
export declare const writeHarderTimeout: (env: NodeJS.ProcessEnv, seconds: number | undefined) => void;
|
|
34
|
+
export declare const harderTimeoutSeconds: (env: NodeJS.ProcessEnv, endpoint: string) => number;
|
|
35
|
+
export declare const HARDER_REASON_IDS: readonly ["harder.no-consent", "harder.no-key", "harder.bad-setting", "harder.local-only", "harder.address-refused", "harder.later-page", "harder.too-few", "harder.timeout", "harder.key-rejected", "harder.unreachable", "harder.rate-limited", "harder.bad-reply", "harder.error"];
|
|
27
36
|
export type HarderReasonId = (typeof HARDER_REASON_IDS)[number];
|
|
28
37
|
/** What `--harder` did: `applied` only when the model's order replaced the plain one. */
|
|
29
38
|
export interface HarderResult {
|
|
@@ -70,6 +79,7 @@ export type HarderAccess = {
|
|
|
70
79
|
model: string;
|
|
71
80
|
endpoint: string;
|
|
72
81
|
custom: boolean;
|
|
82
|
+
timeoutMs: number;
|
|
73
83
|
key?: string;
|
|
74
84
|
} | {
|
|
75
85
|
ready: false;
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { CliError } from "@wirecat/cli-base";
|
|
2
2
|
import * as v from "valibot";
|
|
3
|
+
import { modelAddressAllowed, onThisComputer } from "../models/endpoint.js";
|
|
3
4
|
import { modelGateway } from "../models/index.js";
|
|
5
|
+
import { localOnly } from "../models/local-only.js";
|
|
4
6
|
import { readSharedSection, writeSharedSection } from "../store/shared-config.js";
|
|
5
7
|
import { keyLookup, keyVariable } from "./enrichment.js";
|
|
6
8
|
import { costOf, enrichmentEndpoint, enrichmentSettingPath, MODEL_CHOICE, } from "./enrichment-setting.js";
|
|
@@ -16,7 +18,9 @@ export const HARDER_MODELS = {
|
|
|
16
18
|
/** The search lab's setup (cli-testing search-lab, "Haiku rerank"): the top 30, each cut to 400 characters. */
|
|
17
19
|
export const HARDER_CANDIDATES = 30;
|
|
18
20
|
export const HARDER_CHARS = 400;
|
|
19
|
-
|
|
21
|
+
/** A hosted model answers in 1 to 2 s; a model on this computer's CPU can take tens of seconds for 30 hits. */
|
|
22
|
+
export const HARDER_TIMEOUT_S = { hosted: 5, local: 60 };
|
|
23
|
+
const TIMEOUT_SECTION = "search_harder_timeout_s";
|
|
20
24
|
const Setting = v.strictObject({
|
|
21
25
|
...MODEL_CHOICE,
|
|
22
26
|
/** When the owner agreed that a `--harder` search sends its question and hits; the setting exists only with it. */
|
|
@@ -34,10 +38,22 @@ export const readHarderSetting = (env) => {
|
|
|
34
38
|
return checked.output;
|
|
35
39
|
};
|
|
36
40
|
export const writeHarderSetting = (env, setting) => writeSharedSection(enrichmentSettingPath(env), SECTION, setting === undefined ? undefined : v.parse(Setting, setting));
|
|
41
|
+
// Its own key beside search_harder: an older tool reads that section strictly and would refuse a new field in it.
|
|
42
|
+
const Timeout = v.pipe(v.number(), v.integer(), v.minValue(1), v.maxValue(600));
|
|
43
|
+
/** The owner's time limit in seconds, or `undefined` for the default by where the model runs. */
|
|
44
|
+
export const readHarderTimeout = (env) => {
|
|
45
|
+
const value = readSharedSection(enrichmentSettingPath(env), TIMEOUT_SECTION);
|
|
46
|
+
const checked = v.safeParse(Timeout, value);
|
|
47
|
+
return checked.success ? checked.output : undefined;
|
|
48
|
+
};
|
|
49
|
+
export const writeHarderTimeout = (env, seconds) => writeSharedSection(enrichmentSettingPath(env), TIMEOUT_SECTION, seconds === undefined ? undefined : v.parse(Timeout, seconds));
|
|
50
|
+
export const harderTimeoutSeconds = (env, endpoint) => readHarderTimeout(env) ?? (onThisComputer(endpoint) ? HARDER_TIMEOUT_S.local : HARDER_TIMEOUT_S.hosted);
|
|
37
51
|
export const HARDER_REASON_IDS = [
|
|
38
52
|
"harder.no-consent",
|
|
39
53
|
"harder.no-key",
|
|
40
54
|
"harder.bad-setting",
|
|
55
|
+
"harder.local-only",
|
|
56
|
+
"harder.address-refused",
|
|
41
57
|
"harder.later-page",
|
|
42
58
|
"harder.too-few",
|
|
43
59
|
"harder.timeout",
|
|
@@ -87,7 +103,9 @@ const REASON_TEXT = {
|
|
|
87
103
|
"harder.bad-setting": "the search_harder setting cannot be read; nothing was sent",
|
|
88
104
|
"harder.later-page": "--harder reorders the first page only; nothing was sent",
|
|
89
105
|
"harder.too-few": "fewer than two hits to reorder; nothing was sent",
|
|
90
|
-
"harder.
|
|
106
|
+
"harder.address-refused": "the address is plain http to another computer: use https, or http only on this computer; nothing was sent",
|
|
107
|
+
"harder.local-only": "local only is on and the model is not on this computer; nothing was sent",
|
|
108
|
+
"harder.timeout": "the model did not answer within the time limit",
|
|
91
109
|
"harder.key-rejected": "the provider refused the key",
|
|
92
110
|
"harder.unreachable": "the provider could not be reached",
|
|
93
111
|
"harder.rate-limited": "the provider's rate limit was reached",
|
|
@@ -135,6 +153,15 @@ export const harderAccess = ({ env, command, ...options }) => {
|
|
|
135
153
|
endpoint: enrichmentEndpoint(setting),
|
|
136
154
|
custom: setting.base_url !== undefined,
|
|
137
155
|
};
|
|
156
|
+
if (!modelAddressAllowed(chosen.endpoint))
|
|
157
|
+
return {
|
|
158
|
+
ready: false,
|
|
159
|
+
...chosen,
|
|
160
|
+
reason_id: "harder.address-refused",
|
|
161
|
+
reason: REASON_TEXT["harder.address-refused"],
|
|
162
|
+
};
|
|
163
|
+
if (!onThisComputer(chosen.endpoint) && localOnly(env))
|
|
164
|
+
return { ready: false, ...chosen, reason_id: "harder.local-only", reason: REASON_TEXT["harder.local-only"] };
|
|
138
165
|
let key;
|
|
139
166
|
try {
|
|
140
167
|
key = keyLookup({ env, ...options }, setting)?.key;
|
|
@@ -155,17 +182,23 @@ export const harderAccess = ({ env, command, ...options }) => {
|
|
|
155
182
|
reason_id: "harder.no-key",
|
|
156
183
|
reason: `no ${setting.provider} key: \`${tool} models keys set ${setting.provider}\` or set ${keyVariable(setting.provider)}; nothing was sent`,
|
|
157
184
|
};
|
|
158
|
-
return {
|
|
185
|
+
return {
|
|
186
|
+
ready: true,
|
|
187
|
+
...chosen,
|
|
188
|
+
timeoutMs: options.timeoutMs ?? harderTimeoutSeconds(env, chosen.endpoint) * 1000,
|
|
189
|
+
...(key === undefined ? {} : { key }),
|
|
190
|
+
};
|
|
159
191
|
};
|
|
160
|
-
/** One call to the model under the
|
|
192
|
+
/** One call to the model under the time limit; a failure comes back as its reason, never thrown. */
|
|
161
193
|
const ask = async (access, options, request) => {
|
|
162
194
|
const limit = new AbortController();
|
|
163
|
-
const timer = setTimeout(() => limit.abort(),
|
|
195
|
+
const timer = setTimeout(() => limit.abort(), access.timeoutMs);
|
|
164
196
|
const gateway = modelGateway({
|
|
165
197
|
resolve: () => ({ provider: access.provider, model: access.model, baseUrl: access.endpoint }),
|
|
166
198
|
consent: (_request, chosen) => chosen.provider === access.provider && chosen.baseUrl === access.endpoint,
|
|
167
199
|
key: () => access.key,
|
|
168
200
|
signal: limit.signal,
|
|
201
|
+
env: options.env,
|
|
169
202
|
...(options.adapters ? { adapters: options.adapters } : {}),
|
|
170
203
|
...(options.fetch ? { fetch: options.fetch } : {}),
|
|
171
204
|
});
|
package/dist/speech/openai.js
CHANGED
|
@@ -2,6 +2,7 @@ import { setTimeout as wait } from "node:timers/promises";
|
|
|
2
2
|
import { backoffMs, CliError, DEFAULT_RETRY } from "@wirecat/cli-base";
|
|
3
3
|
import { providerWaitMs } from "@wirecat/cli-base/http";
|
|
4
4
|
import { OPENAI_URL } from "../embeddings/remote.js";
|
|
5
|
+
import { refuseModelAddress } from "../models/local-only.js";
|
|
5
6
|
import { filesPresent, modelsDirectory, vadPath } from "./install.js";
|
|
6
7
|
import { OPENAI_TRANSCRIBE_MODEL, VAD } from "./models.js";
|
|
7
8
|
import { SAMPLE_RATE, toModelRate } from "./recognize.js";
|
|
@@ -161,6 +162,7 @@ const checkKeywords = (keywords = []) => {
|
|
|
161
162
|
* dialogue by `start`. `gpt-transcribe` gives no timestamps of its own, so the times are the detector's.
|
|
162
163
|
*/
|
|
163
164
|
export const transcribeOpenAI = async (audio, options) => {
|
|
165
|
+
refuseModelAddress(options.env ?? process.env, options.baseUrl ?? OPENAI_URL, "audio");
|
|
164
166
|
if (!options.apiKey)
|
|
165
167
|
throw new CliError("authentication_error", "no OpenAI key — `models keys set openai`, or OPENAI_API_KEY", {
|
|
166
168
|
reason: "transcription.no-key",
|
|
@@ -6,8 +6,9 @@ import type { Ffmpeg } from "./decode.js";
|
|
|
6
6
|
import { type SpeechModel } from "./models.js";
|
|
7
7
|
import type { Recognizer } from "./recognize.js";
|
|
8
8
|
/**
|
|
9
|
-
* `messenger`: its own recognition (Telegram Premium). `local`: a model on this machine. `auto`: the
|
|
10
|
-
*
|
|
9
|
+
* `messenger`: its own recognition (Telegram Premium). `local`: a model on this machine. `auto`: the local model when
|
|
10
|
+
* it is downloaded, else the messenger's. `openai`: OpenAI's API, only when asked by name — `auto` never sends audio
|
|
11
|
+
* to OpenAI, and with local only on it never asks the messenger either.
|
|
11
12
|
*/
|
|
12
13
|
export declare const TRANSCRIBE_WITH: readonly ["auto", "messenger", "local", "openai"];
|
|
13
14
|
export type TranscribeWith = (typeof TRANSCRIBE_WITH)[number];
|
|
@@ -35,6 +36,8 @@ export interface Choice {
|
|
|
35
36
|
fetch?: (url: string, init: RequestInit) => Promise<Response>;
|
|
36
37
|
/** The command's own cancel and deadline, for a request that leaves the machine. */
|
|
37
38
|
signal?: AbortSignal;
|
|
39
|
+
/** Local only is on: `auto` never asks the messenger. */
|
|
40
|
+
localOnly?: boolean;
|
|
38
41
|
}
|
|
39
42
|
/** A voice note, or an audio file someone sent — an iPhone recording reaches WhatsApp as `audio/mp4`. */
|
|
40
43
|
export declare const isAudioFile: ({ kind, mime }: {
|
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
import { CliError, isCliError } from "@wirecat/cli-base";
|
|
2
2
|
import { fromFile } from "../cli/settings.js";
|
|
3
3
|
import { embeddingKeys } from "../embeddings/keys.js";
|
|
4
|
+
import { localOnly, localOnlyRefusal } from "../models/local-only.js";
|
|
4
5
|
import { readOnlyLockOf, readOnlyRefusal } from "../ports/app.js";
|
|
5
6
|
import { capability } from "../ports/messenger.js";
|
|
6
7
|
import { typedCommand } from "../runs/scope.js";
|
|
7
8
|
import { installedBytes, isInstalled, megabytes, modelPath, modelsDirectory, vadPath } from "./install.js";
|
|
8
9
|
import { OPENAI_TRANSCRIBE_MODEL, orderedModels, speechModel } from "./models.js";
|
|
9
10
|
/**
|
|
10
|
-
* `messenger`: its own recognition (Telegram Premium). `local`: a model on this machine. `auto`: the
|
|
11
|
-
*
|
|
11
|
+
* `messenger`: its own recognition (Telegram Premium). `local`: a model on this machine. `auto`: the local model when
|
|
12
|
+
* it is downloaded, else the messenger's. `openai`: OpenAI's API, only when asked by name — `auto` never sends audio
|
|
13
|
+
* to OpenAI, and with local only on it never asks the messenger either.
|
|
12
14
|
*/
|
|
13
15
|
export const TRANSCRIBE_WITH = ["auto", "messenger", "local", "openai"];
|
|
14
16
|
/** A recording is read into memory whole; minutes of Opus are a few megabytes. */
|
|
@@ -23,7 +25,10 @@ export const choose = (messenger, settings, flags, env = process.env, keyring) =
|
|
|
23
25
|
: (flags.with ?? fromFile(settings, "transcribeWith", "auto").value);
|
|
24
26
|
// Refused only where something is heard: `models list` reads the same profile for its model alone.
|
|
25
27
|
const known = TRANSCRIBE_WITH.includes(chosen);
|
|
28
|
+
const onlyHere = localOnly(env);
|
|
29
|
+
const offComputer = known && onlyHere && (chosen === "messenger" || chosen === "openai");
|
|
26
30
|
return {
|
|
31
|
+
...(onlyHere ? { localOnly: true } : {}),
|
|
27
32
|
directory: modelsDirectory(env),
|
|
28
33
|
with: known ? chosen : "auto",
|
|
29
34
|
...(known
|
|
@@ -33,7 +38,10 @@ export const choose = (messenger, settings, flags, env = process.env, keyring) =
|
|
|
33
38
|
}),
|
|
34
39
|
model: model === undefined ? orderedModels(messenger.speechModels)[0] : speechModel(model),
|
|
35
40
|
transcribeModel: fromFile(settings, "transcribeModel", OPENAI_TRANSCRIBE_MODEL).value,
|
|
36
|
-
...(known && chosen === "openai" ? openaiAccess(messenger, env, keyring) : {}),
|
|
41
|
+
...(known && chosen === "openai" && !offComputer ? openaiAccess(messenger, env, keyring) : {}),
|
|
42
|
+
...(offComputer
|
|
43
|
+
? { refusal: localOnlyRefusal("the voice message", chosen === "openai" ? "OpenAI" : messenger.provider) }
|
|
44
|
+
: {}),
|
|
37
45
|
};
|
|
38
46
|
};
|
|
39
47
|
const openaiAccess = (messenger, env, keyring) => {
|
|
@@ -58,7 +66,8 @@ export const notDownloaded = (messenger, model) => new CliError("not_found", `th
|
|
|
58
66
|
export const hearOnline = async (messenger, connection, chat, messageId, choice, read) => {
|
|
59
67
|
if (choice.refusal)
|
|
60
68
|
throw choice.refusal;
|
|
61
|
-
|
|
69
|
+
const localReady = choice.with !== "openai" && isInstalled(choice.model, choice.directory);
|
|
70
|
+
if (choice.with === "messenger" || (choice.with === "auto" && !localReady && !choice.localOnly)) {
|
|
62
71
|
const canAsk = typeof connection.transcribe === "function";
|
|
63
72
|
if (choice.with === "messenger" || canAsk) {
|
|
64
73
|
try {
|
|
@@ -72,7 +81,7 @@ export const hearOnline = async (messenger, connection, chat, messageId, choice,
|
|
|
72
81
|
}
|
|
73
82
|
}
|
|
74
83
|
}
|
|
75
|
-
if (choice.with !== "openai" && !
|
|
84
|
+
if (choice.with !== "openai" && !localReady)
|
|
76
85
|
throw notDownloaded(messenger, choice.model);
|
|
77
86
|
const { files } = await capability(connection, "download", "download attachments")(chat, messageId);
|
|
78
87
|
const voice = files.find(isAudioFile);
|
package/dist/store/index.d.ts
CHANGED
|
@@ -50,7 +50,7 @@ export type { CatalogActor, CatalogEntry, CatalogInput, CatalogQuery, CatalogSto
|
|
|
50
50
|
export { CATALOG_QUERY } from "./sqlite/catalog.js";
|
|
51
51
|
export type { ChatMetadata } from "./sqlite/chats.js";
|
|
52
52
|
export { type CrmContactsRemoved, type CrmCopies, type CrmRelationIssue, type CrmSaved, type CrmSkipped, crmIdentityId, } from "./sqlite/crm.js";
|
|
53
|
-
export { COMPANY_TYPE_MEANINGS, type CompanyTypeMeaning, CRM_LINK_MODES, type CrmLinkMode, crmSettingKey, } from "./sqlite/crm-settings.js";
|
|
53
|
+
export { COMPANY_TYPE_MEANINGS, type CompanyTypeMeaning, CRM_LINK_MODES, type CrmLinkMode, type CrmStageMove, type CrmStageTasks, crmSettingKey, type StageTaskRule, } from "./sqlite/crm-settings.js";
|
|
54
54
|
export type { Deal, DealFilter, DealInput, DealPerson, DealScope, DealsStore, FunnelStage, Pipeline, PipelineInput, PipelineReport, PipelineStage, StageChange, } from "./sqlite/deals.js";
|
|
55
55
|
export type { DecisionMessage, DecisionPress, DecisionRefusal, DecisionRequest, DecisionRequestInput, DecisionRequestStore, TakeResult, } from "./sqlite/decision-requests.js";
|
|
56
56
|
export type { Email, EmailAddress, EmailAttachment, EmailFacts, EmailFilter, EmailInput, EmailInThread, EmailLocationInput, EmailRecipient, EmailRef, EmailThread, EmailVectorHit, FolderEmail, Mailbox, MailboxInput, MailFilter, MailStore, PartialEmail, SenderScore, ThreadDetails, ThreadSave, } from "./sqlite/emails.js";
|
|
@@ -145,6 +145,8 @@ export const crmRowsOver = (context) => {
|
|
|
145
145
|
...(item.metadata ?? {}),
|
|
146
146
|
...(item.url ? { url: item.url } : {}),
|
|
147
147
|
...(item.assignee ? { assignee: crmIdentityId(key, item.assignee, "user") } : {}),
|
|
148
|
+
...(textOf(item.contactName, 200) ? { contact_name: textOf(item.contactName, 200) } : {}),
|
|
149
|
+
...(textOf(item.companyName, 200) ? { company_name: textOf(item.companyName, 200) } : {}),
|
|
148
150
|
...(item.closedReason && status !== "open" ? { closed_reason: textOf(item.closedReason, 2000) } : {}),
|
|
149
151
|
};
|
|
150
152
|
const values = {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type CrmScopeSetting } from "../../domain/crm.js";
|
|
2
|
+
import type { DealKind } from "../../domain/deals.js";
|
|
2
3
|
import type { CacheDatabase } from "../driver.js";
|
|
3
4
|
import type { AccountKey } from "../types.js";
|
|
4
5
|
/** `reported`: what a CRM account says is a relationship nobody confirmed; `review`: it waits as a proposal. */
|
|
@@ -9,7 +10,7 @@ export declare const COMPANY_TYPE_MEANINGS: readonly ["customer-of", "supplier",
|
|
|
9
10
|
export type CompanyTypeMeaning = (typeof COMPANY_TYPE_MEANINGS)[number];
|
|
10
11
|
/** `mine`: only the deals and leads of the sign-in's own user and of the `unassigned` users; `all`: every one. */
|
|
11
12
|
export declare const CRM_SCOPES: readonly ["mine", "all"];
|
|
12
|
-
declare const NAMES: readonly ["links", "company-types", "scope", "unassigned", "self"];
|
|
13
|
+
declare const NAMES: readonly ["links", "company-types", "scope", "unassigned", "self", "stage-tasks"];
|
|
13
14
|
type CrmSettingName = (typeof NAMES)[number];
|
|
14
15
|
export declare const crmSettingKey: (key: AccountKey, name: CrmSettingName) => string;
|
|
15
16
|
/** The account and the setting a key names; a portal's account may hold dots, so it is read from both ends. */
|
|
@@ -31,6 +32,30 @@ export declare const crmScopeUsers: (database: CacheDatabase, key: AccountKey) =
|
|
|
31
32
|
*/
|
|
32
33
|
/** The CRM user whose deals the account's copy holds: the first sync's sign-in, until the owner unsets it. */
|
|
33
34
|
export declare const crmOwnUser: (database: CacheDatabase, key: AccountKey) => string | undefined;
|
|
35
|
+
/** What a deal or lead entering the stage opens: a task of this title, due this many days later. */
|
|
36
|
+
export interface StageTaskRule {
|
|
37
|
+
title: string;
|
|
38
|
+
dueDays: number;
|
|
39
|
+
}
|
|
40
|
+
export type CrmStageTasks = Record<DealKind, Record<string, StageTaskRule>>;
|
|
41
|
+
/** Only the owner's rules open a task on a stage change; an entry this build cannot read opens none. */
|
|
42
|
+
export declare const crmStageTasks: (database: CacheDatabase, key: AccountKey) => CrmStageTasks;
|
|
43
|
+
/** After the mark, oldest first: each stage a deal or lead of the account entered from another one, and the newest id read. */
|
|
44
|
+
export declare const crmStageMoves: (database: CacheDatabase, key: AccountKey, accountId: number | undefined) => {
|
|
45
|
+
moves: CrmStageMove[];
|
|
46
|
+
through: number;
|
|
47
|
+
};
|
|
48
|
+
export interface CrmStageMove {
|
|
49
|
+
/** The stage change's own id: one task per entry into a stage, never two. */
|
|
50
|
+
id: number;
|
|
51
|
+
kind: DealKind;
|
|
52
|
+
ref: string;
|
|
53
|
+
/** The CRM's id of the stage entered. */
|
|
54
|
+
stage: string;
|
|
55
|
+
stageName: string;
|
|
56
|
+
pipelineName: string;
|
|
57
|
+
}
|
|
58
|
+
export declare const saveStageMovesMark: (database: CacheDatabase, key: AccountKey, through: number, now: number) => void;
|
|
34
59
|
export declare const parseCrmSettingValue: (name: CrmSettingName, value: string) => unknown;
|
|
35
60
|
export declare const saveCrmSetting: (database: CacheDatabase, setting: string, value: unknown, now: number) => void;
|
|
36
61
|
export declare const crmSettingsOf: (database: CacheDatabase) => {
|