@wirecat/cli-core 0.604.0 → 0.606.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.
Files changed (141) hide show
  1. package/CHANGELOG.md +159 -0
  2. package/dist/calendar/prepare.d.ts +4 -2
  3. package/dist/calendar/prepare.js +8 -8
  4. package/dist/cli/index.d.ts +2 -2
  5. package/dist/cli/index.js +2 -1
  6. package/dist/cli/messenger/contacts-command.js +3 -2
  7. package/dist/cli/messenger/doctor-command.js +14 -7
  8. package/dist/cli/messenger/store-maintenance-command.d.ts +34 -2
  9. package/dist/cli/messenger/store-maintenance-command.js +31 -4
  10. package/dist/cli/program.js +11 -2
  11. package/dist/cli/steps.js +9 -1
  12. package/dist/cli/store-recover.d.ts +10 -2
  13. package/dist/cli/store-recover.js +18 -4
  14. package/dist/cli/store-settings.js +38 -4
  15. package/dist/crm/cli/index.js +3 -1
  16. package/dist/crm/index.d.ts +2 -2
  17. package/dist/crm/index.js +1 -1
  18. package/dist/crm/source.d.ts +35 -5
  19. package/dist/crm/sync.d.ts +15 -7
  20. package/dist/crm/sync.js +169 -11
  21. package/dist/domain/crm.d.ts +41 -2
  22. package/dist/domain/deals.d.ts +5 -2
  23. package/dist/domain/deals.js +5 -2
  24. package/dist/domain/record-types.d.ts +61 -0
  25. package/dist/domain/record-types.js +26 -0
  26. package/dist/domain/records.d.ts +3 -5
  27. package/dist/domain/records.js +10 -13
  28. package/dist/domain/relations.d.ts +125 -0
  29. package/dist/domain/relations.js +107 -0
  30. package/dist/entities/contact-activity.d.ts +4 -0
  31. package/dist/entities/contact-activity.js +1 -0
  32. package/dist/entities/contact-context.d.ts +20 -0
  33. package/dist/entities/contact-context.js +2 -0
  34. package/dist/entities/deal.d.ts +2 -2
  35. package/dist/entities/deal.js +2 -1
  36. package/dist/entities/index.d.ts +39 -13
  37. package/dist/entities/relationship.d.ts +8 -2
  38. package/dist/entities/relationship.js +8 -2
  39. package/dist/entities/task-results.d.ts +10 -10
  40. package/dist/errors/failures.d.ts +17 -1
  41. package/dist/errors/failures.js +57 -1
  42. package/dist/errors/reasons.js +28 -0
  43. package/dist/index.d.ts +2 -1
  44. package/dist/index.js +1 -0
  45. package/dist/mail/cli/review.js +20 -6
  46. package/dist/mail/index.d.ts +1 -1
  47. package/dist/mail/marks.d.ts +10 -5
  48. package/dist/mail/marks.js +16 -8
  49. package/dist/mail/review.d.ts +5 -0
  50. package/dist/mcp/tool.js +2 -1
  51. package/dist/runs/recording.js +13 -4
  52. package/dist/services/deps.js +4 -6
  53. package/dist/services/index.d.ts +1 -1
  54. package/dist/services/link-scorers/chat-senders.d.ts +3 -2
  55. package/dist/services/link-scorers/chat-senders.js +7 -4
  56. package/dist/services/link-scorers/mail-thread.js +1 -0
  57. package/dist/services/link-scorers/member-chats.js +1 -0
  58. package/dist/services/link-scorers/member-dialog.d.ts +1 -1
  59. package/dist/services/link-scorers/member-dialog.js +4 -3
  60. package/dist/services/link-scorers/scan.d.ts +5 -2
  61. package/dist/services/link-scorers/scan.js +100 -54
  62. package/dist/services/link-scorers/shared.d.ts +2 -2
  63. package/dist/services/link-scorers/shared.js +8 -4
  64. package/dist/services/link-scorers/types.d.ts +3 -0
  65. package/dist/services/metadata.d.ts +1 -1
  66. package/dist/services/person-context.d.ts +24 -7
  67. package/dist/services/person-context.js +45 -11
  68. package/dist/services/person-preparation.js +4 -5
  69. package/dist/services/project-status/index.js +2 -1
  70. package/dist/services/stored-project-gathering.js +4 -3
  71. package/dist/store/damage.d.ts +3 -1
  72. package/dist/store/damage.js +19 -12
  73. package/dist/store/document-transfer/index.js +7 -0
  74. package/dist/store/exports.d.ts +1 -0
  75. package/dist/store/exports.js +1 -0
  76. package/dist/store/index.d.ts +7 -1
  77. package/dist/store/index.js +2 -0
  78. package/dist/store/marks.d.ts +57 -0
  79. package/dist/store/marks.js +121 -0
  80. package/dist/store/project-transfer/preview.js +12 -7
  81. package/dist/store/project-transfer/request.d.ts +4 -1
  82. package/dist/store/project-transfer/schema.d.ts +9 -2
  83. package/dist/store/project-transfer/schema.js +4 -0
  84. package/dist/store/project-transfer/snapshot-model.d.ts +8 -2
  85. package/dist/store/recover.js +16 -1
  86. package/dist/store/sqlite/crm-settings.d.ts +28 -0
  87. package/dist/store/sqlite/crm-settings.js +80 -0
  88. package/dist/store/sqlite/crm.d.ts +42 -2
  89. package/dist/store/sqlite/crm.js +397 -36
  90. package/dist/store/sqlite/deals.js +3 -2
  91. package/dist/store/sqlite/finance.d.ts +8 -1
  92. package/dist/store/sqlite/finance.js +10 -3
  93. package/dist/store/sqlite/involvement-queue.js +4 -3
  94. package/dist/store/sqlite/knowledge.d.ts +7 -3
  95. package/dist/store/sqlite/knowledge.js +43 -31
  96. package/dist/store/sqlite/link-kinds.d.ts +2 -3
  97. package/dist/store/sqlite/link-kinds.js +4 -2
  98. package/dist/store/sqlite/manifest.js +5 -0
  99. package/dist/store/sqlite/memories.d.ts +1 -1
  100. package/dist/store/sqlite/memories.js +33 -7
  101. package/dist/store/sqlite/migrations.generated.js +31 -0
  102. package/dist/store/sqlite/notes.d.ts +3 -2
  103. package/dist/store/sqlite/notes.js +23 -6
  104. package/dist/store/sqlite/open-store.js +4 -1
  105. package/dist/store/sqlite/organizations.d.ts +8 -0
  106. package/dist/store/sqlite/organizations.js +14 -0
  107. package/dist/store/sqlite/own-identities.d.ts +1 -1
  108. package/dist/store/sqlite/own-identities.js +7 -5
  109. package/dist/store/sqlite/person-links.d.ts +5 -2
  110. package/dist/store/sqlite/person-links.js +24 -18
  111. package/dist/store/sqlite/person-preparation.d.ts +1 -1
  112. package/dist/store/sqlite/person-preparation.js +3 -2
  113. package/dist/store/sqlite/person-separation.d.ts +5 -0
  114. package/dist/store/sqlite/person-separation.js +26 -0
  115. package/dist/store/sqlite/project-transfer/records.js +14 -3
  116. package/dist/store/sqlite/project-transfer/snapshot.d.ts +4 -1
  117. package/dist/store/sqlite/project-transfer/snapshot.js +6 -0
  118. package/dist/store/sqlite/record-projectors.d.ts +44 -0
  119. package/dist/store/sqlite/record-projectors.js +72 -0
  120. package/dist/store/sqlite/records.js +12 -70
  121. package/dist/store/sqlite/relation-list.d.ts +43 -0
  122. package/dist/store/sqlite/relation-list.js +133 -0
  123. package/dist/store/sqlite/relation-proposals.d.ts +28 -6
  124. package/dist/store/sqlite/relation-proposals.js +134 -10
  125. package/dist/store/sqlite/relation-reads.d.ts +8 -0
  126. package/dist/store/sqlite/relation-reads.js +7 -0
  127. package/dist/store/sqlite/relation-roles.d.ts +26 -0
  128. package/dist/store/sqlite/relation-roles.js +91 -0
  129. package/dist/store/sqlite/reported-relations.d.ts +63 -0
  130. package/dist/store/sqlite/reported-relations.js +132 -0
  131. package/dist/store/sqlite/schema/link-revisions.d.ts +136 -0
  132. package/dist/store/sqlite/schema/link-revisions.js +11 -0
  133. package/dist/store/sqlite/schema/links.d.ts +360 -0
  134. package/dist/store/sqlite/schema/links.js +32 -0
  135. package/dist/store/sqlite/schema.d.ts +2 -296
  136. package/dist/store/sqlite/schema.js +2 -24
  137. package/dist/store/sqlite/transaction.js +2 -5
  138. package/dist/store/store.d.ts +3 -0
  139. package/dist/store/store.js +5 -0
  140. package/dist/store/types.d.ts +5 -0
  141. package/package.json +5 -5
@@ -0,0 +1,61 @@
1
+ /** Types with a table of their own; anything else a source sends is `custom:<name>` and stays a record only. */
2
+ export declare const RECORD_ENTITY_TYPES: readonly ["invoice", "payment", "person", "organization", "project"];
3
+ export type RecordEntityType = (typeof RECORD_ENTITY_TYPES)[number];
4
+ export interface RecordRoleSpec {
5
+ /** Several relations may carry it; a single role may also be named in `fields.<role>`. */
6
+ many?: boolean;
7
+ amount?: boolean;
8
+ }
9
+ export interface RecordTypeSpec {
10
+ table: string;
11
+ /** `proposal`: the import proposes the entity and the owner decides; `identity`: only the owner's `map` gives one. */
12
+ review: "proposal" | "identity";
13
+ /** The roles the entity is made from; any other role word is kept as the source's own. */
14
+ roles: Record<string, RecordRoleSpec>;
15
+ }
16
+ export declare const RECORD_TYPES: {
17
+ readonly invoice: {
18
+ readonly table: "invoices";
19
+ readonly review: "proposal";
20
+ readonly roles: {
21
+ readonly issuer: {};
22
+ readonly counterparty: {};
23
+ readonly project: {};
24
+ };
25
+ };
26
+ readonly payment: {
27
+ readonly table: "payments";
28
+ readonly review: "proposal";
29
+ readonly roles: {
30
+ readonly payer: {};
31
+ readonly payee: {};
32
+ readonly project: {};
33
+ readonly settles: {
34
+ readonly many: true;
35
+ readonly amount: true;
36
+ };
37
+ };
38
+ };
39
+ readonly person: {
40
+ readonly table: "persons";
41
+ readonly review: "identity";
42
+ readonly roles: {};
43
+ };
44
+ readonly organization: {
45
+ readonly table: "organizations";
46
+ readonly review: "identity";
47
+ readonly roles: {};
48
+ };
49
+ readonly project: {
50
+ readonly table: "projects";
51
+ readonly review: "identity";
52
+ readonly roles: {};
53
+ };
54
+ };
55
+ /** A role word, a source provider, and the name after `custom:`. */
56
+ export declare const WORD_BODY = "[a-z0-9][a-z0-9_-]{0,62}";
57
+ export declare const TYPE_PATTERN: string;
58
+ export declare const TYPE_RULE: string;
59
+ export declare const rolesOf: (type: RecordEntityType) => [string, RecordRoleSpec][];
60
+ export declare const roleNames: (types: readonly RecordEntityType[], keep: (role: RecordRoleSpec) => boolean | undefined) => string[];
61
+ export declare const AMOUNT_ROLES: string[];
@@ -0,0 +1,26 @@
1
+ /** Types with a table of their own; anything else a source sends is `custom:<name>` and stays a record only. */
2
+ export const RECORD_ENTITY_TYPES = ["invoice", "payment", "person", "organization", "project"];
3
+ export const RECORD_TYPES = {
4
+ invoice: {
5
+ table: "invoices",
6
+ review: "proposal",
7
+ roles: { issuer: {}, counterparty: {}, project: {} },
8
+ },
9
+ payment: {
10
+ table: "payments",
11
+ review: "proposal",
12
+ roles: { payer: {}, payee: {}, project: {}, settles: { many: true, amount: true } },
13
+ },
14
+ person: { table: "persons", review: "identity", roles: {} },
15
+ organization: { table: "organizations", review: "identity", roles: {} },
16
+ project: { table: "projects", review: "identity", roles: {} },
17
+ };
18
+ /** A role word, a source provider, and the name after `custom:`. */
19
+ export const WORD_BODY = "[a-z0-9][a-z0-9_-]{0,62}";
20
+ export const TYPE_PATTERN = `^(${RECORD_ENTITY_TYPES.join("|")}|custom:${WORD_BODY})$`;
21
+ export const TYPE_RULE = `a type is one of ${RECORD_ENTITY_TYPES.join(", ")} or custom:<name>`;
22
+ export const rolesOf = (type) => Object.entries(RECORD_TYPES[type].roles);
23
+ export const roleNames = (types, keep) => [
24
+ ...new Set(types.flatMap((type) => rolesOf(type).flatMap(([name, role]) => (keep(role) ? [name] : [])))),
25
+ ];
26
+ export const AMOUNT_ROLES = roleNames(RECORD_ENTITY_TYPES, (role) => role.amount);
@@ -1,9 +1,7 @@
1
+ export { RECORD_ENTITY_TYPES, type RecordEntityType } from "./record-types.js";
1
2
  export declare const RECORDS_SCHEMA_VERSION = 1;
2
3
  export declare const RECORD_OPERATIONS: readonly ["upsert", "delete"];
3
- /** Types with a table of their own; anything else a source sends is `custom:<name>` and stays a record only. */
4
- export declare const RECORD_ENTITY_TYPES: readonly ["invoice", "payment", "person", "organization", "project"];
5
4
  export type RecordOperation = (typeof RECORD_OPERATIONS)[number];
6
- export type RecordEntityType = (typeof RECORD_ENTITY_TYPES)[number];
7
5
  export interface RecordRelation {
8
6
  /** What the target is to this record: `counterparty`, `payer`, `settles`, or a source's own word. */
9
7
  role: string;
@@ -99,7 +97,7 @@ export declare const RECORDS_ENVELOPE_SCHEMA: {
99
97
  };
100
98
  readonly type: {
101
99
  readonly type: "string";
102
- readonly pattern: "^(invoice|payment|person|organization|project|custom:[a-z0-9][a-z0-9_-]{0,62})$";
100
+ readonly pattern: string;
103
101
  };
104
102
  readonly external_id: {
105
103
  readonly type: "string";
@@ -138,7 +136,7 @@ export declare const RECORDS_ENVELOPE_SCHEMA: {
138
136
  };
139
137
  readonly type: {
140
138
  readonly type: "string";
141
- readonly pattern: "^(invoice|payment|person|organization|project|custom:[a-z0-9][a-z0-9_-]{0,62})$";
139
+ readonly pattern: string;
142
140
  };
143
141
  readonly external_id: {
144
142
  readonly type: "string";
@@ -1,11 +1,10 @@
1
1
  import { createHash } from "node:crypto";
2
2
  import { holdsCredential, secretNamed } from "../runs/secrets.js";
3
+ import { AMOUNT_ROLES, TYPE_PATTERN, TYPE_RULE, WORD_BODY } from "./record-types.js";
4
+ export { RECORD_ENTITY_TYPES } from "./record-types.js";
3
5
  export const RECORDS_SCHEMA_VERSION = 1;
4
6
  export const RECORD_OPERATIONS = ["upsert", "delete"];
5
- /** Types with a table of their own; anything else a source sends is `custom:<name>` and stays a record only. */
6
- export const RECORD_ENTITY_TYPES = ["invoice", "payment", "person", "organization", "project"];
7
- const WORD = "^[a-z0-9][a-z0-9_-]{0,62}$";
8
- const TYPE = "^(invoice|payment|person|organization|project|custom:[a-z0-9][a-z0-9_-]{0,62})$";
7
+ const WORD = `^${WORD_BODY}$`;
9
8
  const MAX_ID = 512;
10
9
  const MAX_FIELDS_BYTES = 65_536;
11
10
  const MAX_RELATIONS = 100;
@@ -41,7 +40,7 @@ export const RECORDS_ENVELOPE_SCHEMA = {
41
40
  additionalProperties: false,
42
41
  properties: {
43
42
  key: { type: "string", minLength: 1, maxLength: MAX_ID },
44
- type: { type: "string", pattern: TYPE },
43
+ type: { type: "string", pattern: TYPE_PATTERN },
45
44
  external_id: { type: "string", minLength: 1, maxLength: MAX_ID },
46
45
  operation: { enum: [...RECORD_OPERATIONS] },
47
46
  fields: {
@@ -58,7 +57,7 @@ export const RECORDS_ENVELOPE_SCHEMA = {
58
57
  additionalProperties: false,
59
58
  properties: {
60
59
  role: { type: "string", pattern: WORD },
61
- type: { type: "string", pattern: TYPE },
60
+ type: { type: "string", pattern: TYPE_PATTERN },
62
61
  external_id: { type: "string", minLength: 1, maxLength: MAX_ID },
63
62
  amount: {
64
63
  type: "string",
@@ -70,7 +69,7 @@ export const RECORDS_ENVELOPE_SCHEMA = {
70
69
  },
71
70
  };
72
71
  const wordPattern = new RegExp(WORD);
73
- const typePattern = new RegExp(TYPE);
72
+ const typePattern = new RegExp(TYPE_PATTERN);
74
73
  const decimalPattern = new RegExp(DECIMAL);
75
74
  const isObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
76
75
  const escapePointer = (segment) => String(segment).replaceAll("~", "~0").replaceAll("/", "~1");
@@ -115,13 +114,13 @@ const relationErrors = (relations, at) => {
115
114
  if (typeof relation.role !== "string" || !wordPattern.test(relation.role))
116
115
  return [invalid([...here, "role"], "a role is lowercase letters, digits, - and _")];
117
116
  if (typeof relation.type !== "string" || !typePattern.test(relation.type))
118
- return [invalid([...here, "type"], `a type is one of ${RECORD_ENTITY_TYPES.join(", ")} or custom:<name>`)];
117
+ return [invalid([...here, "type"], TYPE_RULE)];
119
118
  if (!idText(relation.external_id))
120
119
  return [invalid([...here, "external_id"], `an external id takes 1–${MAX_ID} characters`)];
121
120
  if (relation.amount !== undefined && (typeof relation.amount !== "string" || !decimalPattern.test(relation.amount)))
122
121
  return [invalid([...here, "amount"], 'an amount is a decimal string, such as "13000.00"')];
123
- if (relation.amount !== undefined && relation.role !== "settles")
124
- return [invalid([...here, "amount"], "only a settles relation carries an amount")];
122
+ if (relation.amount !== undefined && !AMOUNT_ROLES.includes(relation.role))
123
+ return [invalid([...here, "amount"], `only a ${AMOUNT_ROLES.join(" or ")} relation carries an amount`)];
125
124
  return [];
126
125
  });
127
126
  };
@@ -143,9 +142,7 @@ export const recordErrors = (record, index) => {
143
142
  if (!idText(record.key))
144
143
  return [invalid([...at, "key"], `a key takes 1–${MAX_ID} characters`)];
145
144
  if (typeof record.type !== "string" || !typePattern.test(record.type))
146
- return withKey([
147
- invalid([...at, "type"], `a type is one of ${RECORD_ENTITY_TYPES.join(", ")} or custom:<name>`, "record.type"),
148
- ]);
145
+ return withKey([invalid([...at, "type"], TYPE_RULE, "record.type")]);
149
146
  if (!idText(record.external_id))
150
147
  return withKey([invalid([...at, "external_id"], `an external id takes 1–${MAX_ID} characters`)]);
151
148
  if (!RECORD_OPERATIONS.includes(record.operation))
@@ -0,0 +1,125 @@
1
+ /** What a person is to an organization; who reports to whom is `reports-to`, and a job title is never a role. */
2
+ export declare const ORGANIZATION_ROLES: readonly ["employee", "contractor", "founder", "owner", "manager", "adviser", "contact"];
3
+ export declare const PROJECT_ROLES: readonly ["owner", "manager", "contributor", "client-contact", "approver"];
4
+ type End = "person" | "organization" | "project" | "task" | "chat" | "email_thread" | "deal" | "lead";
5
+ interface RelationSpec {
6
+ from: readonly End[];
7
+ to: readonly End[];
8
+ /** Role lists by the end that names them; where none applies, a role is free text. */
9
+ roles?: {
10
+ from?: Partial<Record<End, readonly string[]>>;
11
+ to?: Partial<Record<End, readonly string[]>>;
12
+ };
13
+ /** Shown when a relationship is read from its `to` end; never stored. */
14
+ inverse?: string;
15
+ /** Stored once, the lower id as `from`. */
16
+ symmetric?: true;
17
+ }
18
+ /**
19
+ * Every relationship `knowledge.relate` makes, its ends, its roles and how it reads backwards. An organization may
20
+ * still be a member: owner data from before this list holds such links.
21
+ */
22
+ export declare const RELATIONS: {
23
+ readonly "member-of": {
24
+ readonly from: readonly ["person", "organization", "project", "chat", "email_thread"];
25
+ readonly to: readonly ["organization", "project"];
26
+ readonly roles: {
27
+ readonly to: {
28
+ readonly organization: readonly ["employee", "contractor", "founder", "owner", "manager", "adviser", "contact"];
29
+ readonly project: readonly ["owner", "manager", "contributor", "client-contact", "approver"];
30
+ };
31
+ };
32
+ };
33
+ readonly "reports-to": {
34
+ readonly from: readonly ["person"];
35
+ readonly to: readonly ["person"];
36
+ readonly inverse: "manages";
37
+ };
38
+ readonly "customer-of": {
39
+ readonly from: readonly ["person", "organization"];
40
+ readonly to: readonly ["organization"];
41
+ readonly inverse: "supplier of";
42
+ };
43
+ readonly "partner-of": {
44
+ readonly from: readonly ["organization"];
45
+ readonly to: readonly ["organization"];
46
+ readonly symmetric: true;
47
+ };
48
+ readonly "related-to": {
49
+ readonly from: readonly ["person", "organization", "project", "task", "deal", "lead"];
50
+ readonly to: readonly ["person", "organization", "project", "task"];
51
+ readonly roles: {
52
+ readonly from: {
53
+ readonly deal: readonly ["owner", "contact", "decision-maker", "influencer"];
54
+ readonly lead: readonly ["owner", "contact", "decision-maker", "influencer"];
55
+ };
56
+ };
57
+ };
58
+ };
59
+ export type RelationKind = keyof typeof RELATIONS;
60
+ export declare const RELATION_KINDS: RelationKind[];
61
+ export declare const isRelationKind: (kind: string) => kind is RelationKind;
62
+ export declare const checkEnds: (kind: string, from: string, to: string) => RelationSpec;
63
+ /**
64
+ * The roles a relationship takes, or `undefined` for free text. A member's list is the organization's or the
65
+ * project's only when the member is a person: a chat's or a sub-project's role was never one of them.
66
+ */
67
+ export declare const rolesOf: (kind: string, from: string, to: string) => readonly string[] | undefined;
68
+ /** The role lists the owner may add to; each is a list the registry gives some relationship's ends. */
69
+ export declare const ROLE_LISTS: {
70
+ readonly organization: readonly ["employee", "contractor", "founder", "owner", "manager", "adviser", "contact"];
71
+ readonly project: readonly ["owner", "manager", "contributor", "client-contact", "approver"];
72
+ readonly deal: readonly ["owner", "contact", "decision-maker", "influencer"];
73
+ };
74
+ export type RoleList = keyof typeof ROLE_LISTS;
75
+ /** Which list a relationship's role comes from, or `undefined` where a role is free text. */
76
+ export declare const roleListOf: (kind: string, from: string, to: string) => RoleList | undefined;
77
+ /**
78
+ * The role as given when the relationship has no list, or when it is on the list or among `extra` (the owner's own
79
+ * roles for that list); refused otherwise.
80
+ */
81
+ export declare const checkRole: (kind: string, from: string, to: string, role: string, extra?: readonly string[]) => string;
82
+ export declare const isSymmetric: (kind: string) => boolean;
83
+ /** The label of a relationship read from its `to` end: `manages` for `reports-to`; the kind itself when symmetric. */
84
+ export declare const inverseOf: (kind: string) => string | null;
85
+ /**
86
+ * What an answer says about a relationship, derived, never stored: the owner's, a source's that nobody confirmed, an
87
+ * agent's suggestion waiting for review, or one that no longer holds.
88
+ */
89
+ export declare const STANDINGS: readonly ["confirmed", "reported", "proposed", "ended"];
90
+ export type Standing = (typeof STANDINGS)[number];
91
+ /** A link's columns `standing` reads; a column an older store lacks is `null`. */
92
+ export interface StandingFields {
93
+ confirmed: boolean;
94
+ accountId?: number | null;
95
+ /** Epoch ms, exclusive. */
96
+ endsAt?: number | null;
97
+ deletedAt?: number | null;
98
+ }
99
+ /** What a `links` row of a relationship kind holds beyond any link, when set. */
100
+ export interface RelationshipColumns {
101
+ /** The `accounts` row of the source that reported it. */
102
+ accountId?: number;
103
+ /** When it held, epoch ms; `endsAt` exclusive. */
104
+ startsAt?: number;
105
+ endsAt?: number;
106
+ /** The source stopped reporting it. */
107
+ deletedAt?: number;
108
+ /** A job title beside it (`metadata.position`), never its role. */
109
+ position?: string;
110
+ /** The source's link this owner link overrides (`metadata.overrides`). */
111
+ overrides?: number;
112
+ }
113
+ /** The owner's confirmation outlives a source that stopped reporting it; only its own end date ends it. */
114
+ export declare const standingOf: (link: StandingFields, now: number) => Standing;
115
+ /** A relationship a context or a traversal counts: the owner's, or a source's that still holds. */
116
+ export declare const isUsable: (standing: Standing) => boolean;
117
+ export declare const usableLink: (link: StandingFields, now: number) => boolean;
118
+ /**
119
+ * The `links` row aliased `alias` has no owner link overriding it (`metadata.overrides`, an owner's edit of a link a
120
+ * source reported): where one does, the override is the answer.
121
+ */
122
+ export declare const notOverriddenSql: (alias: string) => string;
123
+ /** `usableLink` as SQL over the `links` row aliased `alias`; without `now` an end date is not checked. */
124
+ export declare const usableLinkSql: (alias: string, now?: number) => string;
125
+ export {};
@@ -0,0 +1,107 @@
1
+ import { CliError } from "@wirecat/cli-base";
2
+ import { DEAL_ROLES } from "./deals.js";
3
+ /** What a person is to an organization; who reports to whom is `reports-to`, and a job title is never a role. */
4
+ export const ORGANIZATION_ROLES = [
5
+ "employee",
6
+ "contractor",
7
+ "founder",
8
+ "owner",
9
+ "manager",
10
+ "adviser",
11
+ "contact",
12
+ ];
13
+ export const PROJECT_ROLES = ["owner", "manager", "contributor", "client-contact", "approver"];
14
+ /**
15
+ * Every relationship `knowledge.relate` makes, its ends, its roles and how it reads backwards. An organization may
16
+ * still be a member: owner data from before this list holds such links.
17
+ */
18
+ export const RELATIONS = {
19
+ "member-of": {
20
+ from: ["person", "organization", "project", "chat", "email_thread"],
21
+ to: ["organization", "project"],
22
+ roles: { to: { organization: ORGANIZATION_ROLES, project: PROJECT_ROLES } },
23
+ },
24
+ "reports-to": { from: ["person"], to: ["person"], inverse: "manages" },
25
+ "customer-of": { from: ["person", "organization"], to: ["organization"], inverse: "supplier of" },
26
+ "partner-of": { from: ["organization"], to: ["organization"], symmetric: true },
27
+ "related-to": {
28
+ from: ["person", "organization", "project", "task", "deal", "lead"],
29
+ to: ["person", "organization", "project", "task"],
30
+ roles: { from: { deal: DEAL_ROLES, lead: DEAL_ROLES } },
31
+ },
32
+ };
33
+ export const RELATION_KINDS = Object.keys(RELATIONS);
34
+ const specOf = (kind) => Object.hasOwn(RELATIONS, kind) ? RELATIONS[kind] : undefined;
35
+ export const isRelationKind = (kind) => specOf(kind) !== undefined;
36
+ /** A chat or a mail thread is only ever a project's member: its reference names one account. */
37
+ const memberOnly = (type) => type === "chat" || type === "email_thread";
38
+ export const checkEnds = (kind, from, to) => {
39
+ const spec = specOf(kind);
40
+ if (!spec)
41
+ throw new CliError("validation_error", `a relationship's kind is one of ${RELATION_KINDS.join(", ")}`);
42
+ if (memberOnly(from) && (kind !== "member-of" || to !== "project"))
43
+ throw new CliError("validation_error", `a ${from === "chat" ? "chat" : "mail thread"} is related only as member-of a project`);
44
+ if (!spec.from.includes(from) || !spec.to.includes(to))
45
+ throw new CliError("validation_error", `${kind} joins ${spec.from.join(", ")} to ${spec.to.join(", ")}, not ${from} to ${to}`);
46
+ return spec;
47
+ };
48
+ /**
49
+ * The roles a relationship takes, or `undefined` for free text. A member's list is the organization's or the
50
+ * project's only when the member is a person: a chat's or a sub-project's role was never one of them.
51
+ */
52
+ export const rolesOf = (kind, from, to) => {
53
+ const spec = specOf(kind);
54
+ if (kind === "member-of" && from !== "person")
55
+ return undefined;
56
+ return spec?.roles?.from?.[from] ?? spec?.roles?.to?.[to];
57
+ };
58
+ /** The role lists the owner may add to; each is a list the registry gives some relationship's ends. */
59
+ export const ROLE_LISTS = { organization: ORGANIZATION_ROLES, project: PROJECT_ROLES, deal: DEAL_ROLES };
60
+ /** Which list a relationship's role comes from, or `undefined` where a role is free text. */
61
+ export const roleListOf = (kind, from, to) => {
62
+ const roles = rolesOf(kind, from, to);
63
+ return Object.keys(ROLE_LISTS).find((list) => ROLE_LISTS[list] === roles);
64
+ };
65
+ /**
66
+ * The role as given when the relationship has no list, or when it is on the list or among `extra` (the owner's own
67
+ * roles for that list); refused otherwise.
68
+ */
69
+ export const checkRole = (kind, from, to, role, extra = []) => {
70
+ const roles = rolesOf(kind, from, to)?.concat(extra);
71
+ if (roles && !roles.includes(role))
72
+ throw new CliError("validation_error", `a ${from}'s role on ${kind} ${to} is one of ${roles.join(", ")}`, {
73
+ reason: "relationship.role-unknown",
74
+ });
75
+ return role;
76
+ };
77
+ export const isSymmetric = (kind) => specOf(kind)?.symmetric === true;
78
+ /** The label of a relationship read from its `to` end: `manages` for `reports-to`; the kind itself when symmetric. */
79
+ export const inverseOf = (kind) => specOf(kind)?.symmetric ? kind : (specOf(kind)?.inverse ?? null);
80
+ /**
81
+ * What an answer says about a relationship, derived, never stored: the owner's, a source's that nobody confirmed, an
82
+ * agent's suggestion waiting for review, or one that no longer holds.
83
+ */
84
+ export const STANDINGS = ["confirmed", "reported", "proposed", "ended"];
85
+ /** The owner's confirmation outlives a source that stopped reporting it; only its own end date ends it. */
86
+ export const standingOf = (link, now) => {
87
+ if (link.endsAt != null && link.endsAt <= now)
88
+ return "ended";
89
+ if (link.confirmed)
90
+ return "confirmed";
91
+ if (link.deletedAt != null)
92
+ return "ended";
93
+ return link.accountId != null ? "reported" : "proposed";
94
+ };
95
+ /** A relationship a context or a traversal counts: the owner's, or a source's that still holds. */
96
+ export const isUsable = (standing) => standing === "confirmed" || standing === "reported";
97
+ export const usableLink = (link, now) => isUsable(standingOf(link, now));
98
+ /**
99
+ * The `links` row aliased `alias` has no owner link overriding it (`metadata.overrides`, an owner's edit of a link a
100
+ * source reported): where one does, the override is the answer.
101
+ */
102
+ export const notOverriddenSql = (alias) => `NOT EXISTS (SELECT 1 FROM links override WHERE override.kind = ${alias}.kind AND override.from_type = ${alias}.from_type ` +
103
+ `AND override.from_id = ${alias}.from_id AND override.to_type = ${alias}.to_type AND override.to_id = ${alias}.to_id ` +
104
+ `AND json_extract(override.metadata, '$.overrides') = ${alias}.id)`;
105
+ /** `usableLink` as SQL over the `links` row aliased `alias`; without `now` an end date is not checked. */
106
+ export const usableLinkSql = (alias, now) => `(${alias}.confirmed = 1 OR (${alias}.account_id IS NOT NULL AND ${alias}.deleted_at IS NULL))` +
107
+ (now === undefined ? "" : ` AND (${alias}.ends_at IS NULL OR ${alias}.ends_at > ${Math.trunc(now)})`);
@@ -9,6 +9,10 @@ export declare const linkedPerson: v.StrictObjectSchema<{
9
9
  readonly username: v.NullableSchema<v.StringSchema<undefined>, undefined>;
10
10
  readonly is_bot: v.NullableSchema<v.BooleanSchema<undefined>, undefined>;
11
11
  readonly accounts: v.ArraySchema<v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>, undefined>;
12
+ readonly seen_in: v.ArraySchema<v.StrictObjectSchema<{
13
+ readonly account: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
14
+ readonly scope: v.PicklistSchema<["personal", "work"], undefined>;
15
+ }, undefined>, undefined>;
12
16
  readonly method: v.StringSchema<undefined>;
13
17
  readonly linked_by: v.StringSchema<undefined>;
14
18
  readonly linked_at: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.IsoTimestampAction<string, "an ISO 8601 time in UTC">, v.EndsWithAction<string, "Z", "an ISO 8601 time in UTC, ending in `Z`">]>;
@@ -8,6 +8,7 @@ const linkedIdentity = v.strictObject({
8
8
  username: v.nullable(text),
9
9
  is_bot: v.nullable(v.boolean()),
10
10
  accounts: v.array(id),
11
+ seen_in: v.array(v.strictObject({ account: id, scope: v.picklist(["personal", "work"]) })),
11
12
  method: text,
12
13
  linked_by: text,
13
14
  linked_at: time,
@@ -669,6 +669,10 @@ export declare const contactContext: v.UnionSchema<[v.StrictObjectSchema<{
669
669
  readonly username: v.NullableSchema<v.StringSchema<undefined>, undefined>;
670
670
  readonly is_bot: v.NullableSchema<v.BooleanSchema<undefined>, undefined>;
671
671
  readonly accounts: v.ArraySchema<v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>, undefined>;
672
+ readonly seen_in: v.ArraySchema<v.StrictObjectSchema<{
673
+ readonly account: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
674
+ readonly scope: v.PicklistSchema<["personal", "work"], undefined>;
675
+ }, undefined>, undefined>;
672
676
  readonly method: v.StringSchema<undefined>;
673
677
  readonly linked_by: v.StringSchema<undefined>;
674
678
  readonly linked_at: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.IsoTimestampAction<string, "an ISO 8601 time in UTC">, v.EndsWithAction<string, "Z", "an ISO 8601 time in UTC, ending in `Z`">]>;
@@ -676,6 +680,12 @@ export declare const contactContext: v.UnionSchema<[v.StrictObjectSchema<{
676
680
  readonly provider: v.StringSchema<undefined>;
677
681
  readonly id: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
678
682
  }, undefined>;
683
+ /** The accounts read: only those of the scopes the caller may read. */
684
+ readonly accounts: v.ArraySchema<v.StrictObjectSchema<{
685
+ readonly provider: v.StringSchema<undefined>;
686
+ readonly account: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
687
+ readonly scope: v.PicklistSchema<["personal", "work"], undefined>;
688
+ }, undefined>, undefined>;
679
689
  readonly shared: v.ArraySchema<v.StrictObjectSchema<{
680
690
  readonly provider: v.StringSchema<undefined>;
681
691
  readonly account: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
@@ -1459,6 +1469,10 @@ export declare const contactPreparation: v.StrictObjectSchema<{
1459
1469
  readonly username: v.NullableSchema<v.StringSchema<undefined>, undefined>;
1460
1470
  readonly is_bot: v.NullableSchema<v.BooleanSchema<undefined>, undefined>;
1461
1471
  readonly accounts: v.ArraySchema<v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>, undefined>;
1472
+ readonly seen_in: v.ArraySchema<v.StrictObjectSchema<{
1473
+ readonly account: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
1474
+ readonly scope: v.PicklistSchema<["personal", "work"], undefined>;
1475
+ }, undefined>, undefined>;
1462
1476
  readonly method: v.StringSchema<undefined>;
1463
1477
  readonly linked_by: v.StringSchema<undefined>;
1464
1478
  readonly linked_at: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.IsoTimestampAction<string, "an ISO 8601 time in UTC">, v.EndsWithAction<string, "Z", "an ISO 8601 time in UTC, ending in `Z`">]>;
@@ -1466,6 +1480,12 @@ export declare const contactPreparation: v.StrictObjectSchema<{
1466
1480
  readonly provider: v.StringSchema<undefined>;
1467
1481
  readonly id: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
1468
1482
  }, undefined>;
1483
+ /** The accounts read: only those of the scopes the caller may read. */
1484
+ readonly accounts: v.ArraySchema<v.StrictObjectSchema<{
1485
+ readonly provider: v.StringSchema<undefined>;
1486
+ readonly account: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
1487
+ readonly scope: v.PicklistSchema<["personal", "work"], undefined>;
1488
+ }, undefined>, undefined>;
1469
1489
  readonly shared: v.ArraySchema<v.StrictObjectSchema<{
1470
1490
  readonly provider: v.StringSchema<undefined>;
1471
1491
  readonly account: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
@@ -36,6 +36,8 @@ export const contextEvent = v.strictObject({
36
36
  });
37
37
  const context = v.strictObject({
38
38
  person: contextPerson,
39
+ /** The accounts read: only those of the scopes the caller may read. */
40
+ accounts: v.array(v.strictObject({ provider: text, account: id, scope: v.picklist(["personal", "work"]) })),
39
41
  shared: v.array(sharedChat),
40
42
  last: v.strictObject({
41
43
  from_them: v.nullable(contextMessage),
@@ -45,7 +45,7 @@ export declare const deal: v.StrictObjectSchema<{
45
45
  readonly ref: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.RegexAction<string, "a reference such as `note:42`">]>;
46
46
  readonly name: v.NullableSchema<v.StringSchema<undefined>, undefined>;
47
47
  readonly owner: v.BooleanSchema<undefined>;
48
- readonly role: v.PicklistSchema<["owner", "contact", "decision-maker"], undefined>;
48
+ readonly role: v.PicklistSchema<readonly ["owner", "contact", "decision-maker", "influencer"], undefined>;
49
49
  }, undefined>, undefined>;
50
50
  readonly author: v.StrictObjectSchema<{
51
51
  readonly type: v.PicklistSchema<["person", "bot"], undefined>;
@@ -106,7 +106,7 @@ export declare const lead: v.StrictObjectSchema<{
106
106
  readonly ref: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.RegexAction<string, "a reference such as `note:42`">]>;
107
107
  readonly name: v.NullableSchema<v.StringSchema<undefined>, undefined>;
108
108
  readonly owner: v.BooleanSchema<undefined>;
109
- readonly role: v.PicklistSchema<["owner", "contact", "decision-maker"], undefined>;
109
+ readonly role: v.PicklistSchema<readonly ["owner", "contact", "decision-maker", "influencer"], undefined>;
110
110
  }, undefined>, undefined>;
111
111
  readonly author: v.StrictObjectSchema<{
112
112
  readonly type: v.PicklistSchema<["person", "bot"], undefined>;
@@ -1,4 +1,5 @@
1
1
  import * as v from "valibot";
2
+ import { DEAL_ROLES } from "../domain/deals.js";
2
3
  import { count, id, reference, text, time, untrusted } from "./fields.js";
3
4
  const nullableText = v.nullable(text);
4
5
  const party = v.strictObject({ ref: reference, name: nullableText, owner: v.boolean() });
@@ -40,7 +41,7 @@ export const deal = v.strictObject({
40
41
  value: v.unknown(),
41
42
  option_id: v.optional(v.union([v.string(), v.array(v.string())])),
42
43
  })),
43
- people: v.array(v.strictObject({ ...party.entries, role: v.picklist(["owner", "contact", "decision-maker"]) })),
44
+ people: v.array(v.strictObject({ ...party.entries, role: v.picklist(DEAL_ROLES) })),
44
45
  author: v.strictObject({
45
46
  type: v.picklist(["person", "bot"]),
46
47
  id,