@wirecat/cli-core 0.612.0 → 0.613.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.
@@ -0,0 +1,139 @@
1
+ import { parseReference } from "./references.js";
2
+ export const LINK_VERSION = "v1";
3
+ export const DEFAULT_LINK_CONFIG = { base: "https://wirecat.dev/o", enabled: true };
4
+ const APP_PREFIX = "wirecat://open";
5
+ const MAX_PATH = 512;
6
+ const MAX_SEGMENTS = 6;
7
+ const TASK_FILTERS = ["overdue", "today", "tomorrow"];
8
+ const DATE = /^\d{4}-\d{2}-\d{2}$/;
9
+ const DATE_TIME = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(:\d{2}(\.\d+)?)?(Z|[+-]\d{2}:\d{2})$/;
10
+ const isMoment = (text, pattern) => pattern.test(text) && !Number.isNaN(Date.parse(text));
11
+ const partsOf = (target) => {
12
+ switch (target.type) {
13
+ case "message":
14
+ return ["msg", target.provider, target.account, target.chat, target.message];
15
+ case "chat":
16
+ return ["chat", target.provider, target.account, target.chat];
17
+ case "task":
18
+ return ["task", target.id];
19
+ case "tasks":
20
+ return target.filter === "new" ? ["tasks", "new", target.since] : ["tasks", target.filter];
21
+ case "proposal":
22
+ return ["proposal", target.id];
23
+ case "digest":
24
+ return ["digest", target.date];
25
+ case "sync":
26
+ return target.account === undefined ? ["sync", target.tool] : ["sync", target.tool, target.account];
27
+ }
28
+ };
29
+ /** The path after the version, `msg/max/1001/-2002/3003`: each part percent-encoded, so no id can add a part. */
30
+ const pathOf = (target) => partsOf(target).map(encodeURIComponent).join("/");
31
+ const targetOfParts = (parts) => {
32
+ const [kind, ...rest] = parts;
33
+ const [a = "", b = "", c = "", d = ""] = rest;
34
+ switch (kind) {
35
+ case "msg":
36
+ return rest.length === 4 ? { type: "message", provider: a, account: b, chat: c, message: d } : null;
37
+ case "chat":
38
+ return rest.length === 3 ? { type: "chat", provider: a, account: b, chat: c } : null;
39
+ case "task":
40
+ return rest.length === 1 ? { type: "task", id: a } : null;
41
+ case "tasks":
42
+ if (rest.length === 1 && TASK_FILTERS.includes(a))
43
+ return { type: "tasks", filter: a };
44
+ return rest.length === 2 && a === "new" && (isMoment(b, DATE) || isMoment(b, DATE_TIME))
45
+ ? { type: "tasks", filter: "new", since: b }
46
+ : null;
47
+ case "proposal":
48
+ return rest.length === 1 ? { type: "proposal", id: a } : null;
49
+ case "digest":
50
+ return rest.length === 1 && isMoment(a, DATE) ? { type: "digest", date: a } : null;
51
+ case "sync":
52
+ return rest.length === 1 || rest.length === 2
53
+ ? { type: "sync", tool: a, ...(rest.length === 2 ? { account: b } : {}) }
54
+ : null;
55
+ default:
56
+ return null;
57
+ }
58
+ };
59
+ /** A path after the version, or `null`: an empty part or one that does not decode is not a WireCat link. */
60
+ const targetOfPath = (path) => {
61
+ const raw = path.split("/");
62
+ if (raw.some((part) => part === ""))
63
+ return null;
64
+ try {
65
+ return targetOfParts(raw.map(decodeURIComponent));
66
+ }
67
+ catch {
68
+ return null;
69
+ }
70
+ };
71
+ /** `/v1/msg/max/1001/-2002/3003`, the part both forms share. */
72
+ export const formatLink = (target) => `/${LINK_VERSION}/${pathOf(target)}`;
73
+ /**
74
+ * The target of a link in either form (`https://…#/v1/…`, `wirecat://open/v1/…`) or of its bare path (`/v1/…`), or
75
+ * `null` for anything else: another version, an unknown kind, a part too many or empty, a path over 512 characters.
76
+ */
77
+ export const parseLink = (text) => {
78
+ const link = text.trim();
79
+ const path = link.startsWith(`${APP_PREFIX}/`)
80
+ ? link.slice(APP_PREFIX.length)
81
+ : link.startsWith("https://") && link.includes("#")
82
+ ? link.slice(link.indexOf("#") + 1)
83
+ : link;
84
+ if (path.length > MAX_PATH || !path.startsWith(`/${LINK_VERSION}/`))
85
+ return null;
86
+ // A later version may add extras as a query; a v1 reader ignores them rather than refusing the link.
87
+ const rest = path.split("?")[0].slice(LINK_VERSION.length + 2);
88
+ if (rest.split("/").length + 1 > MAX_SEGMENTS)
89
+ return null;
90
+ return targetOfPath(rest);
91
+ };
92
+ const LINK_ONLY = new Set(["tasks", "proposal", "digest", "sync"]);
93
+ const FROM_REFERENCE = new Set(["message", "chat", "task"]);
94
+ /**
95
+ * The target a ref names, or `null` when it names nothing a link opens. A store reference (`msg:`, `chat:`, `task:`)
96
+ * is read by its own grammar; the kinds that are no reference are written as their path with a colon after the
97
+ * kind: `proposal:fact%3A512`, `tasks:overdue`, `tasks:new/2026-11-07T08%3A00%3A00Z`, `digest:2026-11-08`,
98
+ * `sync:telegram/1001`.
99
+ */
100
+ export const linkTargetOf = (ref) => {
101
+ const text = ref.trim();
102
+ const colon = text.indexOf(":");
103
+ if (colon < 0)
104
+ return null;
105
+ const kind = text.slice(0, colon);
106
+ if (LINK_ONLY.has(kind))
107
+ return targetOfPath(`${kind}/${text.slice(colon + 1)}`);
108
+ try {
109
+ const reference = parseReference(text);
110
+ return FROM_REFERENCE.has(reference.type) ? reference : null;
111
+ }
112
+ catch {
113
+ return null;
114
+ }
115
+ };
116
+ /** The `links` section of the shared config, with the default for what is missing or not a usable value. */
117
+ export const linkConfigOf = (section) => {
118
+ const value = section && typeof section === "object" ? section : {};
119
+ const base = typeof value.base === "string" && /^https:\/\/[^\s#?]+$/.test(value.base)
120
+ ? value.base.replace(/\/+$/, "")
121
+ : DEFAULT_LINK_CONFIG.base;
122
+ return { base, enabled: typeof value.enabled === "boolean" ? value.enabled : DEFAULT_LINK_CONFIG.enabled };
123
+ };
124
+ /**
125
+ * The one way to build a WireCat link: the web form for messages and mail (an https page that hands it to the app),
126
+ * the app form for a desktop terminal. `null` when links are off (`links.enabled: false`, or `WIRECAT_LINKS=off` for
127
+ * one run) or the ref names nothing a link opens; the caller then leaves the link out.
128
+ */
129
+ export const linkFor = (ref, { form, config = DEFAULT_LINK_CONFIG, env = process.env, }) => {
130
+ if (!config.enabled || env.WIRECAT_LINKS === "off")
131
+ return null;
132
+ const target = typeof ref === "string" ? linkTargetOf(ref) : ref;
133
+ if (target === null)
134
+ return null;
135
+ const path = formatLink(target);
136
+ if (path.length > MAX_PATH || parseLink(path) === null)
137
+ return null;
138
+ return form === "app" ? `${APP_PREFIX}${path}` : `${config.base}#${path}`;
139
+ };
package/dist/index.d.ts CHANGED
@@ -6,6 +6,7 @@ export { CRM_KINDS, type CrmDepartment, type CrmField, type CrmItem, type CrmKin
6
6
  export { DEAL_KINDS, DEAL_ROLES, DEAL_STATUSES, type DealKind, type DealRole, type DealStatus, STAGE_OUTCOMES, type StageOutcome, } from "./domain/deals.js";
7
7
  export { FINANCE_REVIEWS, type FinanceReview, INVOICE_DIRECTIONS, INVOICE_STATUSES, type InvoiceDirection, type InvoiceStatus, MONEY_DIRECTIONS, type MoneyDirection, } from "./domain/finance.js";
8
8
  export { type FormattedText, type HtmlFormatting, type MarkdownFormatting, type TextSpan, validateFormattedText, } from "./domain/formatting.js";
9
+ export { DEFAULT_LINK_CONFIG, type DeepLinkTarget, formatLink, LINK_VERSION, type LinkConfig, type LinkForm, linkConfigOf, linkFor, linkTargetOf, parseLink, } from "./domain/links.js";
9
10
  export { formatLocator, isLocator, type MessageLocator, parseLocator } from "./domain/locator.js";
10
11
  export { type Markup, parseMarkdown } from "./domain/markdown.js";
11
12
  export { parseMarkdownSpans } from "./domain/markdown-spans.js";
package/dist/index.js CHANGED
@@ -5,6 +5,7 @@ export { CRM_KINDS, isItemKind, } from "./domain/crm.js";
5
5
  export { DEAL_KINDS, DEAL_ROLES, DEAL_STATUSES, STAGE_OUTCOMES, } from "./domain/deals.js";
6
6
  export { FINANCE_REVIEWS, INVOICE_DIRECTIONS, INVOICE_STATUSES, MONEY_DIRECTIONS, } from "./domain/finance.js";
7
7
  export { validateFormattedText, } from "./domain/formatting.js";
8
+ export { DEFAULT_LINK_CONFIG, formatLink, LINK_VERSION, linkConfigOf, linkFor, linkTargetOf, parseLink, } from "./domain/links.js";
8
9
  export { formatLocator, isLocator, parseLocator } from "./domain/locator.js";
9
10
  export { parseMarkdown } from "./domain/markdown.js";
10
11
  export { parseMarkdownSpans } from "./domain/markdown-spans.js";
@@ -14,12 +14,12 @@ export declare const metadataService: (deps: ServiceDeps) => {
14
14
  dryRun?: boolean;
15
15
  }) => Promise<{
16
16
  items: ({
17
+ error?: undefined;
17
18
  chatId: string;
18
19
  tags: import("../domain/channel-tags.js").ChannelTagMatch[];
19
20
  algorithm: string;
20
21
  fetchedAt: string | null;
21
22
  source: string;
22
- error?: undefined;
23
23
  } | {
24
24
  tags?: undefined;
25
25
  algorithm?: undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wirecat/cli-core",
3
- "version": "0.612.0",
3
+ "version": "0.613.0",
4
4
  "description": "The messenger-neutral half of a messaging CLI: domain model, message locators, rendering, name resolution, the SQLite seam, the command skeleton and shared read commands, the send guard, run records and the message store",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Viacheslav Ptsarev",