@zeyos/client 0.3.0 → 0.5.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 (137) hide show
  1. package/CHANGELOG.md +54 -0
  2. package/README.md +41 -2
  3. package/agents/README.md +10 -0
  4. package/agents/shared/zeyos-agent-operating-guide.md +42 -0
  5. package/agents/shared/zeyos-entity-map.md +5 -1
  6. package/agents/shared/zeyos-entity-reference.md +89 -33
  7. package/agents/shared/zeyos-query-patterns.md +26 -0
  8. package/agents/zeyos-calendar-and-scheduling/SKILL.md +45 -0
  9. package/agents/zeyos-calendar-and-scheduling/references/workflows.md +49 -0
  10. package/agents/zeyos-data-quality-and-governance/SKILL.md +43 -0
  11. package/agents/zeyos-data-quality-and-governance/references/workflows.md +51 -0
  12. package/agents/zeyos-document-and-approval/SKILL.md +41 -0
  13. package/agents/zeyos-document-and-approval/references/workflows.md +43 -0
  14. package/agents/zeyos-procurement-and-supplier-performance/SKILL.md +36 -0
  15. package/agents/zeyos-procurement-and-supplier-performance/references/workflows.md +46 -0
  16. package/agents/zeyos-time-tracking/SKILL.md +2 -0
  17. package/agents/zeyos-time-tracking/references/workflows.md +68 -0
  18. package/agents/zeyos-work-management/SKILL.md +4 -3
  19. package/agents/zeyos-work-management/references/workflows.md +39 -1
  20. package/docs/03-cli/02-commands.md +36 -2
  21. package/docs/03-cli/03-configuration.md +1 -0
  22. package/docs/06-okf/01-overview.md +70 -0
  23. package/docs/06-okf/02-producing-and-consuming.md +46 -0
  24. package/docs/06-okf/03-keeping-fresh.md +53 -0
  25. package/docs/06-okf/04-loops.md +58 -0
  26. package/docs/06-okf/_category_.json +9 -0
  27. package/okf/concepts/calendar-timezones.md +10 -0
  28. package/okf/concepts/confirmation-and-side-effects.md +14 -0
  29. package/okf/concepts/counting-and-sums.md +10 -0
  30. package/okf/concepts/currency-and-rounding.md +10 -0
  31. package/okf/concepts/dates-unix-seconds.md +12 -0
  32. package/okf/concepts/enums.md +14 -0
  33. package/okf/concepts/filters-vs-filter.md +14 -0
  34. package/okf/concepts/idempotency-and-deduplication.md +10 -0
  35. package/okf/concepts/index.md +16 -0
  36. package/okf/concepts/null-empty-missing.md +10 -0
  37. package/okf/concepts/official-versus-latest.md +10 -0
  38. package/okf/concepts/operationid-vocabulary.md +17 -0
  39. package/okf/concepts/ownership-versus-attention.md +15 -0
  40. package/okf/concepts/untrusted-business-content.md +10 -0
  41. package/okf/concepts/visibility-column.md +13 -0
  42. package/okf/entities/accounts.md +82 -0
  43. package/okf/entities/actionsteps.md +84 -0
  44. package/okf/entities/addresses.md +50 -0
  45. package/okf/entities/applicationassets.md +43 -0
  46. package/okf/entities/applications.md +62 -0
  47. package/okf/entities/appointments.md +79 -0
  48. package/okf/entities/associations.md +41 -0
  49. package/okf/entities/binfiles.md +32 -0
  50. package/okf/entities/campaigns.md +66 -0
  51. package/okf/entities/categories.md +55 -0
  52. package/okf/entities/channels.md +54 -0
  53. package/okf/entities/comments.md +44 -0
  54. package/okf/entities/components.md +46 -0
  55. package/okf/entities/contacts.md +96 -0
  56. package/okf/entities/contacts2contacts.md +42 -0
  57. package/okf/entities/contracts.md +83 -0
  58. package/okf/entities/couponcodes.md +58 -0
  59. package/okf/entities/coupons.md +69 -0
  60. package/okf/entities/customfields.md +59 -0
  61. package/okf/entities/davservers.md +74 -0
  62. package/okf/entities/devices.md +65 -0
  63. package/okf/entities/documents.md +76 -0
  64. package/okf/entities/dunning.md +82 -0
  65. package/okf/entities/dunning2transactions.md +46 -0
  66. package/okf/entities/entities2channels.md +42 -0
  67. package/okf/entities/events.md +57 -0
  68. package/okf/entities/feedservers.md +67 -0
  69. package/okf/entities/files.md +50 -0
  70. package/okf/entities/follows.md +40 -0
  71. package/okf/entities/forks.md +54 -0
  72. package/okf/entities/groups.md +48 -0
  73. package/okf/entities/groups2users.md +44 -0
  74. package/okf/entities/index.md +93 -0
  75. package/okf/entities/invitations.md +53 -0
  76. package/okf/entities/items.md +95 -0
  77. package/okf/entities/ledgers.md +56 -0
  78. package/okf/entities/likes.md +40 -0
  79. package/okf/entities/links.md +70 -0
  80. package/okf/entities/mailinglists.md +67 -0
  81. package/okf/entities/mailingrecipients.md +45 -0
  82. package/okf/entities/mailservers.md +77 -0
  83. package/okf/entities/messagereads.md +40 -0
  84. package/okf/entities/messages.md +104 -0
  85. package/okf/entities/notes.md +73 -0
  86. package/okf/entities/objects.md +70 -0
  87. package/okf/entities/opportunities.md +87 -0
  88. package/okf/entities/participants.md +52 -0
  89. package/okf/entities/payments.md +76 -0
  90. package/okf/entities/permissions.md +46 -0
  91. package/okf/entities/pricelists.md +70 -0
  92. package/okf/entities/pricelists2accounts.md +46 -0
  93. package/okf/entities/prices.md +49 -0
  94. package/okf/entities/projects.md +72 -0
  95. package/okf/entities/records.md +75 -0
  96. package/okf/entities/relateditems.md +43 -0
  97. package/okf/entities/resources.md +55 -0
  98. package/okf/entities/services.md +64 -0
  99. package/okf/entities/stocktransactions.md +72 -0
  100. package/okf/entities/storages.md +56 -0
  101. package/okf/entities/suppliers.md +51 -0
  102. package/okf/entities/tasks.md +86 -0
  103. package/okf/entities/tickets.md +86 -0
  104. package/okf/entities/transactions.md +118 -0
  105. package/okf/entities/users.md +66 -0
  106. package/okf/entities/weblets.md +66 -0
  107. package/okf/index.md +11 -0
  108. package/okf/log.md +4 -0
  109. package/okf/metrics/account-address-completeness.md +10 -0
  110. package/okf/metrics/cash-received.md +10 -0
  111. package/okf/metrics/index.md +9 -0
  112. package/okf/metrics/invoiced-net-revenue.md +16 -0
  113. package/okf/metrics/open-customers.md +14 -0
  114. package/okf/metrics/overdue-receivables.md +12 -0
  115. package/okf/metrics/stock-movement-by-storage.md +10 -0
  116. package/okf/metrics/supplier-delivery-performance.md +10 -0
  117. package/okf/playbooks/activity-timeline.md +11 -0
  118. package/okf/playbooks/calendar-availability.md +11 -0
  119. package/okf/playbooks/campaign-recipient-coverage.md +12 -0
  120. package/okf/playbooks/customer-360.md +12 -0
  121. package/okf/playbooks/document-approval.md +10 -0
  122. package/okf/playbooks/duplicate-account-review.md +11 -0
  123. package/okf/playbooks/effective-customer-price.md +11 -0
  124. package/okf/playbooks/index.md +13 -0
  125. package/okf/playbooks/missing-billing-addresses.md +12 -0
  126. package/okf/playbooks/revenue-this-year.md +19 -0
  127. package/okf/playbooks/supplier-scorecard.md +10 -0
  128. package/okf/playbooks/ticket-work-packet.md +11 -0
  129. package/package.json +11 -3
  130. package/scripts/data/okf-curation.mjs +446 -0
  131. package/scripts/generate-client.mjs +4 -275
  132. package/scripts/generate-okf.mjs +241 -0
  133. package/scripts/lib/live-test-config.mjs +20 -0
  134. package/scripts/lib/okf.mjs +272 -0
  135. package/scripts/lib/spec-model.mjs +325 -0
  136. package/src/index.js +4 -0
  137. package/src/runtime/okf.js +237 -0
@@ -0,0 +1,237 @@
1
+ // Open Knowledge Format (OKF v0.1) support for @zeyos/client.
2
+ //
3
+ // Two halves:
4
+ // • Pure (browser + Node): the OKF conformance constants, a tolerant concept
5
+ // parser, a bundle validator, and buildOkf() — which synthesizes a conformant
6
+ // OKF bundle from the client's own introspection surface (the generated
7
+ // SCHEMA + SERVICES). No filesystem access.
8
+ // • Node-only: loadOkfBundle() reads the shipped okf/ bundle (or any directory)
9
+ // from disk via a lazy import, so this module stays bundler/browser-safe.
10
+ //
11
+ // The richer build-time producer (scripts/generate-okf.mjs) emits the curated,
12
+ // managed-block bundle under okf/. buildOkf() here is the lightweight runtime
13
+ // projection of what the shipped client knows — handy for emitting OKF in
14
+ // environments without the bundled files, or to diff against a live instance.
15
+
16
+ import { SCHEMA } from '../generated/schema.js';
17
+ import { SERVICES } from '../generated/operations.js';
18
+
19
+ export const OKF_VERSION = '0.1';
20
+
21
+ // Markers fencing producer-generated content inside a concept's body. Exported so
22
+ // the build-time renderer (scripts/lib/okf.mjs) shares one definition.
23
+ export const GENERATED_START = '<!-- okf:generated:start — rewritten by scripts/generate-okf.mjs; do not edit by hand -->';
24
+ export const GENERATED_END = '<!-- okf:generated:end -->';
25
+ export const GENERATED_FRONTMATTER_KEYS = Object.freeze([
26
+ 'type', 'title', 'description', 'resource', 'tags', 'timestamp',
27
+ 'api_backed', 'list_operation', 'visibility_column'
28
+ ]);
29
+
30
+ // Files that are not concept documents (spec §5/§6).
31
+ const RESERVED_BASENAMES = new Set(['index.md', 'log.md']);
32
+
33
+ const VERB_RE = /^(list|get|create|update|delete|exists)/;
34
+
35
+ // ── Parsing / validation (pure) ────────────────────────────────────────────────
36
+
37
+ /** Tolerant frontmatter + body split. Returns `{ frontmatter, body }` where
38
+ * frontmatter is a flat string→string map (lists kept as their raw text). */
39
+ export function parseConcept(content) {
40
+ const text = String(content || '');
41
+ const match = /^---\n([\s\S]*?)\n---\n?/.exec(text);
42
+ if (!match) return { frontmatter: {}, body: text };
43
+ const frontmatter = {};
44
+ for (const line of match[1].split('\n')) {
45
+ const kv = /^([A-Za-z0-9_]+):\s*(.*)$/.exec(line);
46
+ if (kv) frontmatter[kv[1]] = kv[2].trim();
47
+ }
48
+ return { frontmatter, body: text.slice(match[0].length) };
49
+ }
50
+
51
+ function isReserved(relPath) {
52
+ const base = relPath.split('/').pop();
53
+ return RESERVED_BASENAMES.has(base);
54
+ }
55
+
56
+ /**
57
+ * Validate an OKF bundle for v0.1 conformance (spec §9): every non-reserved `.md`
58
+ * file must have parseable frontmatter with a non-empty `type`. Tolerant by
59
+ * design — unknown types/keys and broken links are NOT errors.
60
+ *
61
+ * @param {Record<string,string>} files - relativePath → file content.
62
+ * @returns {{ valid: boolean, errors: { path: string, message: string }[], conceptCount: number }}
63
+ */
64
+ export function validateOkfFiles(files) {
65
+ const errors = [];
66
+ let conceptCount = 0;
67
+ for (const [relPath, content] of Object.entries(files || {})) {
68
+ if (!relPath.endsWith('.md') || isReserved(relPath)) continue;
69
+ conceptCount += 1;
70
+ const { frontmatter } = parseConcept(content);
71
+ if (!Object.keys(frontmatter).length) {
72
+ errors.push({ path: relPath, message: 'Missing YAML frontmatter.' });
73
+ continue;
74
+ }
75
+ if (!frontmatter.type) {
76
+ errors.push({ path: relPath, message: 'Frontmatter is missing the required `type` field.' });
77
+ }
78
+ }
79
+ return { valid: errors.length === 0, errors, conceptCount };
80
+ }
81
+
82
+ /** The OKF concept ID for a ZeyOS resource, e.g. `tickets` → `entities/tickets`. */
83
+ export function conceptIdForResource(resource) {
84
+ return `entities/${resource}`;
85
+ }
86
+
87
+ // ── buildOkf: synthesize a bundle from the client's introspection surface ───────
88
+
89
+ function groupOperations(services) {
90
+ const byResource = new Map();
91
+ for (const service of Object.values(services || {})) {
92
+ for (const op of service.operations || []) {
93
+ const resource = resourceFromPath(op.path);
94
+ if (!resource) continue;
95
+ if (!byResource.has(resource)) byResource.set(resource, {});
96
+ const bucket = byResource.get(resource);
97
+ const m = VERB_RE.exec(op.operationId);
98
+ const key = m ? m[1] : op.operationId;
99
+ if (!bucket[key]) bucket[key] = op.operationId;
100
+ }
101
+ }
102
+ return byResource;
103
+ }
104
+
105
+ function resourceFromPath(p) {
106
+ if (typeof p !== 'string') return null;
107
+ for (const segment of p.split('/')) {
108
+ if (segment && !segment.startsWith('{')) return segment;
109
+ }
110
+ return null;
111
+ }
112
+
113
+ function titleFromOps(ops, fallback) {
114
+ const op = ops.list || ops.get;
115
+ if (!op) return fallback.charAt(0).toUpperCase() + fallback.slice(1);
116
+ return op.replace(VERB_RE, '').replace(/([a-z0-9])([A-Z])/g, '$1 $2').trim() || fallback;
117
+ }
118
+
119
+ // Deterministic, dependency-free 32-bit hash for the bundle source snapshot.
120
+ function fnv1a(str) {
121
+ let h = 0x811c9dc5;
122
+ for (let i = 0; i < str.length; i += 1) {
123
+ h ^= str.charCodeAt(i);
124
+ h = Math.imul(h, 0x01000193);
125
+ }
126
+ return (h >>> 0).toString(16).padStart(8, '0');
127
+ }
128
+
129
+ function renderEntityDoc(name, entry, ops, hasDoc) {
130
+ const link = (target) => (hasDoc.has(target) ? `/entities/${target}.md` : null);
131
+ const fields = Object.entries(entry.fields || {});
132
+
133
+ const schemaRows = fields.map(([fname, def]) => {
134
+ const fkCell = def.fk ? (link(def.fk) ? `[${def.fk}](${link(def.fk)})` : def.fk) : '—';
135
+ const enumCell = def.enum ? Object.entries(def.enum).map(([k, v]) => `${k}=${v}`).join(', ') : '—';
136
+ return `| \`${fname}\` | ${def.type || 'unknown'} | ${def.indexed ? 'yes' : '—'} | ${fkCell} | ${enumCell} |`;
137
+ });
138
+ const schema = `# Schema\n\n| Column | Type | Indexed | FK | Enum |\n|---|---|---|---|---|\n${schemaRows.join('\n')}`;
139
+
140
+ const fks = fields.filter(([, d]) => d.fk);
141
+ const fkSection = fks.length
142
+ ? `\n\n# Foreign Keys\n\n${fks.map(([f, d]) => `- \`${f}\` → ${link(d.fk) ? `[${d.fk}](${link(d.fk)})` : d.fk}`).join('\n')}`
143
+ : '';
144
+
145
+ const order = ['list', 'get', 'create', 'update', 'delete', 'exists'];
146
+ const opLines = order.filter((k) => ops[k]).map((k) => `- ${k}: \`${ops[k]}\``);
147
+ const opSection = opLines.length ? `\n\n# Operations\n\n${opLines.join('\n')}` : '';
148
+
149
+ const title = titleFromOps(ops, name);
150
+ const fm = [
151
+ 'type: ZeyOS Entity',
152
+ `title: ${title}`,
153
+ `resource: zeyos://api/${name}`,
154
+ 'tags: [generated]',
155
+ 'api_backed: true'
156
+ ];
157
+ if (ops.list) fm.push(`list_operation: ${ops.list}`);
158
+ fm.push(`visibility_column: ${Object.prototype.hasOwnProperty.call(entry.fields || {}, 'visibility')}`);
159
+
160
+ return `---\n${fm.join('\n')}\n---\n\n${schema}${fkSection}${opSection}\n`;
161
+ }
162
+
163
+ /**
164
+ * Synthesize a conformant OKF v0.1 bundle from a client schema + services. Pure:
165
+ * returns a `{ relativePath: content }` map; the caller decides whether to write
166
+ * it to disk. Defaults to the generated SCHEMA/SERVICES baked into the client.
167
+ *
168
+ * @param {{ schema?: object, services?: object }} [input]
169
+ * @returns {Record<string,string>}
170
+ */
171
+ export function buildOkf({ schema = SCHEMA, services = SERVICES } = {}) {
172
+ const ops = groupOperations(services);
173
+ const resources = Object.keys(schema).filter((r) => ops.has(r)).sort();
174
+ const hasDoc = new Set(resources);
175
+ const files = {};
176
+
177
+ for (const name of resources) {
178
+ files[`entities/${name}.md`] = renderEntityDoc(name, schema[name], ops.get(name) || {}, hasDoc);
179
+ }
180
+
181
+ const indexItems = resources
182
+ .map((name) => `* [${titleFromOps(ops.get(name) || {}, name)}](${name}.md)`)
183
+ .join('\n');
184
+ files['entities/index.md'] = `# Entities\n\n${indexItems}\n`;
185
+
186
+ const signature = resources.map((r) => `${r}:${Object.keys(schema[r].fields || {}).join(',')}`).join('|');
187
+ files['index.md'] = `---\nokf_version: ${OKF_VERSION}\nsource_snapshot: ${fnv1a(signature)}\n---\n\n# ZeyOS Knowledge Bundle\n\n* [Entities](entities/) - ${resources.length} API-backed entity concepts.\n`;
188
+
189
+ return files;
190
+ }
191
+
192
+ // ── Node-only loaders (lazy fs so the module stays browser-safe) ────────────────
193
+
194
+ /**
195
+ * Read an OKF bundle directory from disk. Node only.
196
+ * @param {string} dir - Path to a bundle root (e.g. the shipped okf/).
197
+ * @returns {Promise<{ version: string|null, files: Record<string,string>, concepts: Record<string,{frontmatter:object,body:string}> }>}
198
+ */
199
+ export async function loadOkfBundle(dir) {
200
+ const { readFile, readdir } = await import('node:fs/promises');
201
+ const path = await import('node:path');
202
+
203
+ const files = {};
204
+ async function walk(current, prefix) {
205
+ const entries = await readdir(current, { withFileTypes: true });
206
+ for (const entry of entries) {
207
+ const abs = path.join(current, entry.name);
208
+ const rel = prefix ? `${prefix}/${entry.name}` : entry.name;
209
+ if (entry.isDirectory()) await walk(abs, rel);
210
+ else if (entry.name.endsWith('.md')) files[rel] = await readFile(abs, 'utf8');
211
+ }
212
+ }
213
+ await walk(dir, '');
214
+
215
+ const concepts = {};
216
+ for (const [rel, content] of Object.entries(files)) {
217
+ if (isReserved(rel)) continue;
218
+ concepts[rel.replace(/\.md$/, '')] = parseConcept(content);
219
+ }
220
+
221
+ let version = null;
222
+ if (files['index.md']) version = parseConcept(files['index.md']).frontmatter.okf_version || null;
223
+
224
+ return { version, files, concepts };
225
+ }
226
+
227
+ /**
228
+ * Validate an OKF bundle. Accepts a directory path (Node) or an in-memory
229
+ * `{ path: content }` map (universal). Returns the validateOkfFiles() result.
230
+ */
231
+ export async function validateOkfBundle(dirOrFiles) {
232
+ if (typeof dirOrFiles === 'string') {
233
+ const { files } = await loadOkfBundle(dirOrFiles);
234
+ return validateOkfFiles(files);
235
+ }
236
+ return validateOkfFiles(dirOrFiles);
237
+ }