oneie 2.0.3 → 2.0.5
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/CLAUDE.md +220 -29
- package/LICENSE.md +84 -0
- package/README.md +489 -931
- package/folders.yaml +78 -17
- package/one/.obsidian/workspace.json +6 -6
- package/one/events/CASCADE-2-BACKEND-AGNOSTIC.md +412 -0
- package/one/events/FEATURE-2-4-COMPLETE.md +302 -0
- package/one/events/FEATURE_2_3_COMPLETE.md +575 -0
- package/one/events/GROUPS-MIGRATION.md +625 -0
- package/one/events/INSTALL-TESTS.md +293 -0
- package/one/events/INTEGRATION-ARCHITECTURE.md +561 -0
- package/one/events/INTEGRATION-CHECKLIST.md +1234 -0
- package/one/events/INTEGRATION-ORCHESTRATION.md +2623 -0
- package/one/events/INTEGRATION-SUMMARY.md +653 -0
- package/one/events/MIGRATION-REPORT.md +435 -0
- package/one/events/MIGRATION.md +69 -0
- package/one/events/README-archived.md +519 -0
- package/one/events/TEST-SUMMARY.md +393 -0
- package/one/events/TESTS-QUICK-REF.md +262 -0
- package/one/groups/groups.md +543 -0
- package/one/knowledge/CLOUDFLARE_ENV_SETUP.md +49 -0
- package/one/knowledge/architecture-any-backend.md +1366 -0
- package/one/knowledge/docs/flow.md +0 -0
- package/one/knowledge/ontology-documentation.md +18 -18
- package/one/knowledge/ontology.md +154 -143
- package/one/knowledge/ontology.md.backup +2822 -0
- package/one/knowledge/ontology.yaml +87 -87
- package/one/knowledge/provider-creation-guide.md +1064 -0
- package/one/knowledge/todo.md +251 -0
- package/one/license.md +84 -0
- package/one/things/CHECKLIST.md +356 -0
- package/one/things/ONE.md +510 -0
- package/one/things/{cli.md → cli/cli.md} +27 -4
- package/one/things/features/2-1-dataprovider-interface.md +2358 -0
- package/one/things/features/2-2-config-system.md +3075 -0
- package/one/things/features/2-3-effectts-services.md +1498 -0
- package/one/things/features/2-4-react-hooks.md +2120 -0
- package/one/things/features/2-5-auth-migration.md +2046 -0
- package/one/things/features/2-6-dashboard-migration.md +1985 -0
- package/one/things/features/2-7-alternative-providers.md +2505 -0
- package/one/things/features/2-7-implementation-report.md +680 -0
- package/one/things/features/2-cli.md +39 -39
- package/one/things/features/2-design.md +1738 -0
- package/one/things/features/2-tests.md +520 -0
- package/one/things/features/AUTH_HOOKS_IMPLEMENTATION.md +521 -0
- package/one/things/features/DECOUPLING.md +272 -0
- package/one/things/features/DEPLOYMENT.md +452 -0
- package/one/things/features/IMPLEMENTATION_SUMMARY.md +316 -0
- package/one/things/features/dataprovider-interface/1-tests.md +1522 -0
- package/one/things/features/improve.md +843 -0
- package/one/things/features/remove-convex-deps.md +45 -0
- package/one/things/features/top-features.md +79 -0
- package/one/things/ideas/2-backend-agnostic-frontend.md +384 -0
- package/one/things/plans/2-backend-agnostic-frontend.md +543 -0
- package/one/things/plans/3-groups.md +502 -0
- package/one/things/plans/backend-agnostic-frontend.md +323 -0
- package/one/things/plans/deploy-to-cloudflare.md +581 -0
- package/one/things/plans/organizations-to-groups.md +705 -0
- package/one/things/plans/release.md +432 -0
- package/one/things/products/ontology.md +1 -1
- package/one/things/system.md +600 -0
- package/one/things/tests/2-1-dataprovider-interface.md +1144 -0
- package/one/things/tests/2-1-test-analysis.md +496 -0
- package/one/things/vision/elegant-interface.md +214 -0
- package/package.json +1 -1
- package/one/organisation/organisation.md +0 -438
- package/one/repomix-output.md +0 -92634
- /package/one/{things → connections}/agentkit.md +0 -0
- /package/one/{things → connections}/copilotkit.md +0 -0
- /package/one/{organisation → groups}/one.md +0 -0
- /package/one/{organisation → groups}/revenue.md +0 -0
- /package/one/{organisation → groups}/strategy.md +0 -0
- /package/one/{organisation → groups}/vision.md +0 -0
- /package/one/things/{cli-code.md → cli/cli-code.md} +0 -0
- /package/one/things/{cli-compiler-code.md → cli/cli-compiler-code.md} +0 -0
|
@@ -0,0 +1,2505 @@
|
|
|
1
|
+
# Feature 2-7: Alternative Providers (WordPress + Notion)
|
|
2
|
+
|
|
3
|
+
**Feature ID:** `feature_2_7_alternative_providers`
|
|
4
|
+
**Plan:** `plan_2_backend_agnostic_frontend`
|
|
5
|
+
**Owner:** Integration Specialist
|
|
6
|
+
**Status:** Detailed Specification Complete
|
|
7
|
+
**Priority:** P2 (Strategic - Proves Flexibility)
|
|
8
|
+
**Effort:** 2 weeks
|
|
9
|
+
**Dependencies:** Feature 2-6 (Dashboard Migration Complete)
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## Executive Summary
|
|
14
|
+
|
|
15
|
+
This feature implements **WordPressProvider** and **NotionProvider** to prove the backend-agnostic architecture works. Organizations can choose their data backend independently, with full 6-dimension ontology support. Also includes **CompositeProvider** for multi-backend routing within a single organization.
|
|
16
|
+
|
|
17
|
+
**Strategic Value:** Demonstrates ONE Platform's flexibility and enables organizations to leverage existing infrastructure (WordPress, Notion) while gaining ONE's AI capabilities.
|
|
18
|
+
|
|
19
|
+
**Key Deliverables:**
|
|
20
|
+
1. Complete WordPressProvider implementation (REST API + ACF)
|
|
21
|
+
2. Complete NotionProvider implementation (Notion API + webhooks)
|
|
22
|
+
3. CompositeProvider for multi-backend routing
|
|
23
|
+
4. Comprehensive test suite (50+ tests)
|
|
24
|
+
5. Provider creation guide for future backends
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
## Ontology Mapping (All 6 Dimensions)
|
|
29
|
+
|
|
30
|
+
### 1. Organizations (Multi-Tenant Isolation)
|
|
31
|
+
|
|
32
|
+
**Challenge:** External systems (WordPress, Notion) don't have built-in multi-tenancy matching ONE's organization model.
|
|
33
|
+
|
|
34
|
+
**Solution:**
|
|
35
|
+
- Each organization gets its own WordPress site URL or Notion workspace
|
|
36
|
+
- Provider configuration stored per-organization in ONE's organizations table
|
|
37
|
+
- Provider acts as tenant-scoped wrapper around external API
|
|
38
|
+
|
|
39
|
+
**Implementation:**
|
|
40
|
+
```typescript
|
|
41
|
+
// organizations table stores provider config
|
|
42
|
+
{
|
|
43
|
+
_id: "org_123",
|
|
44
|
+
name: "Acme Corp",
|
|
45
|
+
plan: "pro",
|
|
46
|
+
properties: {
|
|
47
|
+
providerType: "wordpress",
|
|
48
|
+
providerConfig: {
|
|
49
|
+
url: "https://acme.wordpress.com",
|
|
50
|
+
apiKey: encryptedApiKey,
|
|
51
|
+
version: "6.4",
|
|
52
|
+
customPostTypes: ["course", "lesson", "creator"]
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Provider registry isolates by org
|
|
58
|
+
class ProviderRegistry {
|
|
59
|
+
private providers = new Map<string, DataProvider>();
|
|
60
|
+
|
|
61
|
+
register(orgId: string, provider: DataProvider) {
|
|
62
|
+
this.providers.set(orgId, provider);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
get(orgId: string): DataProvider {
|
|
66
|
+
const provider = this.providers.get(orgId);
|
|
67
|
+
if (!provider) throw new Error(`No provider for org ${orgId}`);
|
|
68
|
+
return provider;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
**WordPress Multi-Tenancy:**
|
|
74
|
+
- Option 1: WordPress Multisite (one WP install, multiple sites)
|
|
75
|
+
- Option 2: Separate WP installs per organization
|
|
76
|
+
- Option 3: Custom tenant field in all tables
|
|
77
|
+
|
|
78
|
+
**Notion Multi-Tenancy:**
|
|
79
|
+
- Option 1: Separate Notion workspaces per organization
|
|
80
|
+
- Option 2: Single workspace with database-level isolation
|
|
81
|
+
- Option 3: Custom "Organization" property on all pages
|
|
82
|
+
|
|
83
|
+
### 2. People (Authorization & Governance)
|
|
84
|
+
|
|
85
|
+
**Challenge:** WordPress and Notion have different user/permission models than ONE's 4-role system.
|
|
86
|
+
|
|
87
|
+
**WordPress User Mapping:**
|
|
88
|
+
| ONE Role | WordPress Role | Capabilities |
|
|
89
|
+
|----------|---------------|--------------|
|
|
90
|
+
| `platform_owner` | Super Admin | All capabilities |
|
|
91
|
+
| `org_owner` | Administrator | Manage site, users, content |
|
|
92
|
+
| `org_user` | Editor | Create/edit content |
|
|
93
|
+
| `customer` | Subscriber | Read-only access |
|
|
94
|
+
|
|
95
|
+
**Notion User Mapping:**
|
|
96
|
+
| ONE Role | Notion Permission | Access |
|
|
97
|
+
|----------|------------------|--------|
|
|
98
|
+
| `platform_owner` | Workspace Owner | Full access |
|
|
99
|
+
| `org_owner` | Full Access | Edit all pages/databases |
|
|
100
|
+
| `org_user` | Can Edit | Edit assigned pages |
|
|
101
|
+
| `customer` | Can View | Read-only |
|
|
102
|
+
|
|
103
|
+
**Implementation:**
|
|
104
|
+
```typescript
|
|
105
|
+
class WordPressProvider implements DataProvider {
|
|
106
|
+
people: PeopleOperations = {
|
|
107
|
+
list: async (filter) => {
|
|
108
|
+
// Get WordPress users
|
|
109
|
+
const response = await this.api.get("/wp/v2/users");
|
|
110
|
+
const wpUsers = await response.json();
|
|
111
|
+
|
|
112
|
+
// Transform to ONE people (things with type: "creator")
|
|
113
|
+
return wpUsers.map(user => ({
|
|
114
|
+
_id: this.wpIdToOneId("user", user.id),
|
|
115
|
+
type: "creator",
|
|
116
|
+
name: user.name,
|
|
117
|
+
organizationId: this.config.organizationId,
|
|
118
|
+
status: "active",
|
|
119
|
+
properties: {
|
|
120
|
+
role: this.mapWPRoleToONE(user.roles[0]),
|
|
121
|
+
email: user.email,
|
|
122
|
+
wpUserId: user.id,
|
|
123
|
+
wpUsername: user.slug,
|
|
124
|
+
avatar: user.avatar_urls?.["96"],
|
|
125
|
+
registeredDate: user.registered_date,
|
|
126
|
+
},
|
|
127
|
+
createdAt: new Date(user.registered_date).getTime(),
|
|
128
|
+
updatedAt: Date.now(),
|
|
129
|
+
}));
|
|
130
|
+
},
|
|
131
|
+
|
|
132
|
+
create: async (args) => {
|
|
133
|
+
// Transform ONE person to WP user
|
|
134
|
+
const wpUser = {
|
|
135
|
+
username: args.properties.email.split("@")[0],
|
|
136
|
+
email: args.properties.email,
|
|
137
|
+
password: this.generateSecurePassword(),
|
|
138
|
+
roles: [this.mapONERoleToWP(args.properties.role)],
|
|
139
|
+
first_name: args.properties.firstName,
|
|
140
|
+
last_name: args.properties.lastName,
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
const response = await this.api.post("/wp/v2/users", wpUser);
|
|
144
|
+
const created = await response.json();
|
|
145
|
+
|
|
146
|
+
// Transform back to ONE person
|
|
147
|
+
return this.transformWPUserToPerson(created);
|
|
148
|
+
},
|
|
149
|
+
|
|
150
|
+
update: async (id, updates) => {
|
|
151
|
+
const wpId = this.oneIdToWpId(id);
|
|
152
|
+
|
|
153
|
+
const wpUpdates: any = {};
|
|
154
|
+
if (updates.name) {
|
|
155
|
+
const [firstName, ...lastNameParts] = updates.name.split(" ");
|
|
156
|
+
wpUpdates.first_name = firstName;
|
|
157
|
+
wpUpdates.last_name = lastNameParts.join(" ");
|
|
158
|
+
}
|
|
159
|
+
if (updates.properties?.role) {
|
|
160
|
+
wpUpdates.roles = [this.mapONERoleToWP(updates.properties.role)];
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const response = await this.api.post(`/wp/v2/users/${wpId}`, wpUpdates);
|
|
164
|
+
return this.transformWPUserToPerson(await response.json());
|
|
165
|
+
},
|
|
166
|
+
|
|
167
|
+
delete: async (id) => {
|
|
168
|
+
const wpId = this.oneIdToWpId(id);
|
|
169
|
+
await this.api.delete(`/wp/v2/users/${wpId}?force=true&reassign=1`);
|
|
170
|
+
},
|
|
171
|
+
|
|
172
|
+
get: async (id) => {
|
|
173
|
+
const wpId = this.oneIdToWpId(id);
|
|
174
|
+
const response = await this.api.get(`/wp/v2/users/${wpId}`);
|
|
175
|
+
return this.transformWPUserToPerson(await response.json());
|
|
176
|
+
},
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
private mapWPRoleToONE(wpRole: string): string {
|
|
180
|
+
const mapping: Record<string, string> = {
|
|
181
|
+
"administrator": "org_owner",
|
|
182
|
+
"editor": "org_user",
|
|
183
|
+
"author": "org_user",
|
|
184
|
+
"contributor": "org_user",
|
|
185
|
+
"subscriber": "customer",
|
|
186
|
+
};
|
|
187
|
+
return mapping[wpRole] || "customer";
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
private mapONERoleToWP(oneRole: string): string {
|
|
191
|
+
const mapping: Record<string, string> = {
|
|
192
|
+
"org_owner": "administrator",
|
|
193
|
+
"org_user": "editor",
|
|
194
|
+
"customer": "subscriber",
|
|
195
|
+
};
|
|
196
|
+
return mapping[oneRole] || "subscriber";
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
### 3. Things (Entity Integration)
|
|
202
|
+
|
|
203
|
+
**WordPress Entity Mapping:**
|
|
204
|
+
|
|
205
|
+
| ONE Thing Type | WordPress Equivalent | Implementation |
|
|
206
|
+
|----------------|---------------------|----------------|
|
|
207
|
+
| `course` | Custom Post Type: `course` | ACF fields for properties |
|
|
208
|
+
| `lesson` | Custom Post Type: `lesson` | ACF fields for properties |
|
|
209
|
+
| `creator` | WP User | Via `/wp/v2/users` |
|
|
210
|
+
| `token` | WooCommerce Product | Via WooCommerce REST API |
|
|
211
|
+
| `certificate` | Custom Post Type: `certificate` | ACF fields for properties |
|
|
212
|
+
| `quiz` | Custom Post Type: `quiz` | ACF fields for properties |
|
|
213
|
+
| `external_agent` | Custom Post Type: `agent` | ACF fields for configuration |
|
|
214
|
+
|
|
215
|
+
**Notion Entity Mapping:**
|
|
216
|
+
|
|
217
|
+
| ONE Thing Type | Notion Equivalent | Implementation |
|
|
218
|
+
|----------------|-------------------|----------------|
|
|
219
|
+
| `course` | Database: "Courses" | Properties: Name, Status, Description |
|
|
220
|
+
| `lesson` | Database: "Lessons" | Properties: Name, Content, Duration |
|
|
221
|
+
| `creator` | Database: "Users" | Properties: Name, Email, Role |
|
|
222
|
+
| `token` | Database: "Tokens" | Properties: Symbol, Balance, Owner |
|
|
223
|
+
| `certificate` | Database: "Certificates" | Properties: Recipient, Course, Date |
|
|
224
|
+
| `quiz` | Database: "Quizzes" | Properties: Questions, Answers, Score |
|
|
225
|
+
| `external_agent` | Database: "Agents" | Properties: Platform, Endpoint, Config |
|
|
226
|
+
|
|
227
|
+
**Complete WordPressProvider Things Implementation:**
|
|
228
|
+
|
|
229
|
+
```typescript
|
|
230
|
+
class WordPressProvider implements DataProvider {
|
|
231
|
+
private api: WordPressAPI;
|
|
232
|
+
private config: WordPressProviderConfig;
|
|
233
|
+
|
|
234
|
+
constructor(config: WordPressProviderConfig) {
|
|
235
|
+
this.config = config;
|
|
236
|
+
this.api = new WordPressAPI(config.url, config.apiKey);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
things: ThingOperations = {
|
|
240
|
+
list: async (filter) => {
|
|
241
|
+
const postType = this.mapOntologyTypeToWP(filter.type);
|
|
242
|
+
|
|
243
|
+
// Build WordPress query parameters
|
|
244
|
+
const params = new URLSearchParams({
|
|
245
|
+
per_page: String(filter.limit || 10),
|
|
246
|
+
page: String(((filter.skip || 0) / (filter.limit || 10)) + 1),
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
if (filter.status) {
|
|
250
|
+
params.append("status", this.mapOntologyStatusToWP(filter.status));
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
if (filter.organizationId) {
|
|
254
|
+
// Filter by organization using custom taxonomy
|
|
255
|
+
params.append("org_id", filter.organizationId);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// Make API request
|
|
259
|
+
const response = await this.api.get(
|
|
260
|
+
`/wp/v2/${postType}?${params.toString()}`
|
|
261
|
+
);
|
|
262
|
+
const wpPosts = await response.json();
|
|
263
|
+
|
|
264
|
+
// Transform WordPress posts to ontology things
|
|
265
|
+
return Promise.all(
|
|
266
|
+
wpPosts.map((post: any) => this.transformWPPostToThing(post, filter.type))
|
|
267
|
+
);
|
|
268
|
+
},
|
|
269
|
+
|
|
270
|
+
create: async (args) => {
|
|
271
|
+
const postType = this.mapOntologyTypeToWP(args.type);
|
|
272
|
+
|
|
273
|
+
// Transform ontology thing to WordPress post
|
|
274
|
+
const wpPost = {
|
|
275
|
+
title: args.name,
|
|
276
|
+
status: this.mapOntologyStatusToWP(args.status || "draft"),
|
|
277
|
+
content: args.properties.content || "",
|
|
278
|
+
excerpt: args.properties.excerpt || "",
|
|
279
|
+
meta: {
|
|
280
|
+
_one_thing_type: args.type,
|
|
281
|
+
_one_organization_id: args.organizationId,
|
|
282
|
+
...this.transformPropertiesToACF(args.properties),
|
|
283
|
+
},
|
|
284
|
+
};
|
|
285
|
+
|
|
286
|
+
// Create post in WordPress
|
|
287
|
+
const response = await this.api.post(`/wp/v2/${postType}`, wpPost);
|
|
288
|
+
const created = await response.json();
|
|
289
|
+
|
|
290
|
+
// Transform back to ontology thing
|
|
291
|
+
return this.transformWPPostToThing(created, args.type);
|
|
292
|
+
},
|
|
293
|
+
|
|
294
|
+
update: async (id, updates) => {
|
|
295
|
+
const { wpId, postType } = this.parseOneId(id);
|
|
296
|
+
|
|
297
|
+
const wpUpdates: any = {};
|
|
298
|
+
|
|
299
|
+
if (updates.name) wpUpdates.title = updates.name;
|
|
300
|
+
if (updates.status) wpUpdates.status = this.mapOntologyStatusToWP(updates.status);
|
|
301
|
+
if (updates.properties) {
|
|
302
|
+
wpUpdates.meta = this.transformPropertiesToACF(updates.properties);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
const response = await this.api.post(
|
|
306
|
+
`/wp/v2/${postType}/${wpId}`,
|
|
307
|
+
wpUpdates
|
|
308
|
+
);
|
|
309
|
+
const updated = await response.json();
|
|
310
|
+
|
|
311
|
+
return this.transformWPPostToThing(updated, updates.type || postType);
|
|
312
|
+
},
|
|
313
|
+
|
|
314
|
+
delete: async (id) => {
|
|
315
|
+
const { wpId, postType } = this.parseOneId(id);
|
|
316
|
+
await this.api.delete(`/wp/v2/${postType}/${wpId}?force=true`);
|
|
317
|
+
},
|
|
318
|
+
|
|
319
|
+
get: async (id) => {
|
|
320
|
+
const { wpId, postType } = this.parseOneId(id);
|
|
321
|
+
const response = await this.api.get(`/wp/v2/${postType}/${wpId}`);
|
|
322
|
+
const post = await response.json();
|
|
323
|
+
return this.transformWPPostToThing(post, postType);
|
|
324
|
+
},
|
|
325
|
+
};
|
|
326
|
+
|
|
327
|
+
// Transform WordPress post to ONE thing
|
|
328
|
+
private async transformWPPostToThing(
|
|
329
|
+
post: any,
|
|
330
|
+
thingType: string
|
|
331
|
+
): Promise<Thing> {
|
|
332
|
+
// Get ACF fields
|
|
333
|
+
const acfFields = await this.getACFFields(post.id);
|
|
334
|
+
|
|
335
|
+
return {
|
|
336
|
+
_id: this.wpIdToOneId(post.type, post.id),
|
|
337
|
+
type: thingType,
|
|
338
|
+
name: post.title.rendered || post.title,
|
|
339
|
+
organizationId: post.meta._one_organization_id || this.config.organizationId,
|
|
340
|
+
status: this.mapWPStatusToOntology(post.status),
|
|
341
|
+
properties: {
|
|
342
|
+
content: post.content?.rendered || post.content,
|
|
343
|
+
excerpt: post.excerpt?.rendered || post.excerpt,
|
|
344
|
+
slug: post.slug,
|
|
345
|
+
wpPostId: post.id,
|
|
346
|
+
wpPostType: post.type,
|
|
347
|
+
author: post.author,
|
|
348
|
+
featuredImage: post.featured_media,
|
|
349
|
+
categories: post.categories,
|
|
350
|
+
tags: post.tags,
|
|
351
|
+
...this.transformACFToProperties(acfFields),
|
|
352
|
+
},
|
|
353
|
+
createdAt: new Date(post.date_gmt).getTime(),
|
|
354
|
+
updatedAt: new Date(post.modified_gmt).getTime(),
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
// Map ontology types to WordPress post types
|
|
359
|
+
private mapOntologyTypeToWP(type: string): string {
|
|
360
|
+
const mapping: Record<string, string> = {
|
|
361
|
+
course: "course",
|
|
362
|
+
lesson: "lesson",
|
|
363
|
+
creator: "user",
|
|
364
|
+
quiz: "quiz",
|
|
365
|
+
certificate: "certificate",
|
|
366
|
+
token: "product", // WooCommerce
|
|
367
|
+
external_agent: "agent",
|
|
368
|
+
external_workflow: "workflow",
|
|
369
|
+
external_connection: "connection",
|
|
370
|
+
blog_post: "post",
|
|
371
|
+
page: "page",
|
|
372
|
+
};
|
|
373
|
+
return mapping[type] || "post";
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
// Map WordPress post types to ontology types
|
|
377
|
+
private mapWPTypeToOntology(wpType: string): string {
|
|
378
|
+
const mapping: Record<string, string> = {
|
|
379
|
+
course: "course",
|
|
380
|
+
lesson: "lesson",
|
|
381
|
+
user: "creator",
|
|
382
|
+
quiz: "quiz",
|
|
383
|
+
certificate: "certificate",
|
|
384
|
+
product: "token",
|
|
385
|
+
agent: "external_agent",
|
|
386
|
+
workflow: "external_workflow",
|
|
387
|
+
connection: "external_connection",
|
|
388
|
+
post: "blog_post",
|
|
389
|
+
page: "page",
|
|
390
|
+
};
|
|
391
|
+
return mapping[wpType] || "blog_post";
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
// Map ontology status to WordPress status
|
|
395
|
+
private mapOntologyStatusToWP(status: string): string {
|
|
396
|
+
const mapping: Record<string, string> = {
|
|
397
|
+
draft: "draft",
|
|
398
|
+
active: "publish",
|
|
399
|
+
published: "publish",
|
|
400
|
+
archived: "trash",
|
|
401
|
+
pending: "pending",
|
|
402
|
+
};
|
|
403
|
+
return mapping[status] || "draft";
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
// Map WordPress status to ontology status
|
|
407
|
+
private mapWPStatusToOntology(wpStatus: string): string {
|
|
408
|
+
const mapping: Record<string, string> = {
|
|
409
|
+
draft: "draft",
|
|
410
|
+
publish: "active",
|
|
411
|
+
pending: "pending",
|
|
412
|
+
private: "active",
|
|
413
|
+
trash: "archived",
|
|
414
|
+
"auto-draft": "draft",
|
|
415
|
+
};
|
|
416
|
+
return mapping[wpStatus] || "draft";
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
// Transform ontology properties to ACF fields
|
|
420
|
+
private transformPropertiesToACF(properties: any): Record<string, any> {
|
|
421
|
+
const acfFields: Record<string, any> = {};
|
|
422
|
+
|
|
423
|
+
// Map common fields
|
|
424
|
+
if (properties.duration) acfFields.duration = properties.duration;
|
|
425
|
+
if (properties.price) acfFields.price = properties.price;
|
|
426
|
+
if (properties.currency) acfFields.currency = properties.currency;
|
|
427
|
+
if (properties.difficulty) acfFields.difficulty = properties.difficulty;
|
|
428
|
+
if (properties.videoUrl) acfFields.video_url = properties.videoUrl;
|
|
429
|
+
if (properties.prerequisites) {
|
|
430
|
+
acfFields.prerequisites = JSON.stringify(properties.prerequisites);
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
// Store full properties as JSON for unknown fields
|
|
434
|
+
acfFields._one_properties = JSON.stringify(properties);
|
|
435
|
+
|
|
436
|
+
return acfFields;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
// Transform ACF fields to ontology properties
|
|
440
|
+
private transformACFToProperties(acfFields: any): Record<string, any> {
|
|
441
|
+
const properties: Record<string, any> = {};
|
|
442
|
+
|
|
443
|
+
// Parse stored properties JSON
|
|
444
|
+
if (acfFields._one_properties) {
|
|
445
|
+
try {
|
|
446
|
+
Object.assign(properties, JSON.parse(acfFields._one_properties));
|
|
447
|
+
} catch (e) {
|
|
448
|
+
// Ignore parse errors
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
// Map known ACF fields
|
|
453
|
+
if (acfFields.duration) properties.duration = acfFields.duration;
|
|
454
|
+
if (acfFields.price) properties.price = acfFields.price;
|
|
455
|
+
if (acfFields.currency) properties.currency = acfFields.currency;
|
|
456
|
+
if (acfFields.difficulty) properties.difficulty = acfFields.difficulty;
|
|
457
|
+
if (acfFields.video_url) properties.videoUrl = acfFields.video_url;
|
|
458
|
+
if (acfFields.prerequisites) {
|
|
459
|
+
try {
|
|
460
|
+
properties.prerequisites = JSON.parse(acfFields.prerequisites);
|
|
461
|
+
} catch (e) {
|
|
462
|
+
properties.prerequisites = acfFields.prerequisites;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
return properties;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
// Get ACF fields for a post
|
|
470
|
+
private async getACFFields(postId: number): Promise<any> {
|
|
471
|
+
try {
|
|
472
|
+
const response = await this.api.get(`/acf/v3/posts/${postId}`);
|
|
473
|
+
const data = await response.json();
|
|
474
|
+
return data.acf || {};
|
|
475
|
+
} catch (e) {
|
|
476
|
+
// ACF not available or no fields
|
|
477
|
+
return {};
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
// Convert WordPress ID to ONE ID
|
|
482
|
+
private wpIdToOneId(postType: string, wpId: number): string {
|
|
483
|
+
return `wp_${postType}_${wpId}`;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
// Convert ONE ID to WordPress ID
|
|
487
|
+
private oneIdToWpId(oneId: string): number {
|
|
488
|
+
const match = oneId.match(/^wp_[^_]+_(\d+)$/);
|
|
489
|
+
if (!match) throw new Error(`Invalid ONE ID for WordPress: ${oneId}`);
|
|
490
|
+
return parseInt(match[1], 10);
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
// Parse ONE ID to get WordPress ID and post type
|
|
494
|
+
private parseOneId(oneId: string): { wpId: number; postType: string } {
|
|
495
|
+
const match = oneId.match(/^wp_([^_]+)_(\d+)$/);
|
|
496
|
+
if (!match) throw new Error(`Invalid ONE ID for WordPress: ${oneId}`);
|
|
497
|
+
return {
|
|
498
|
+
postType: match[1],
|
|
499
|
+
wpId: parseInt(match[2], 10),
|
|
500
|
+
};
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
// WordPress API client with authentication
|
|
505
|
+
class WordPressAPI {
|
|
506
|
+
constructor(private baseUrl: string, private apiKey: string) {}
|
|
507
|
+
|
|
508
|
+
async get(path: string): Promise<Response> {
|
|
509
|
+
return this.request("GET", path);
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
async post(path: string, body: any): Promise<Response> {
|
|
513
|
+
return this.request("POST", path, body);
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
async delete(path: string): Promise<Response> {
|
|
517
|
+
return this.request("DELETE", path);
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
private async request(
|
|
521
|
+
method: string,
|
|
522
|
+
path: string,
|
|
523
|
+
body?: any
|
|
524
|
+
): Promise<Response> {
|
|
525
|
+
const url = `${this.baseUrl}${path}`;
|
|
526
|
+
|
|
527
|
+
const headers: HeadersInit = {
|
|
528
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
529
|
+
"Content-Type": "application/json",
|
|
530
|
+
};
|
|
531
|
+
|
|
532
|
+
const options: RequestInit = {
|
|
533
|
+
method,
|
|
534
|
+
headers,
|
|
535
|
+
};
|
|
536
|
+
|
|
537
|
+
if (body) {
|
|
538
|
+
options.body = JSON.stringify(body);
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
const response = await fetch(url, options);
|
|
542
|
+
|
|
543
|
+
if (!response.ok) {
|
|
544
|
+
throw new WordPressAPIError(
|
|
545
|
+
`WordPress API error: ${response.status} ${response.statusText}`,
|
|
546
|
+
response.status,
|
|
547
|
+
await response.text()
|
|
548
|
+
);
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
return response;
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
class WordPressAPIError extends Error {
|
|
556
|
+
constructor(
|
|
557
|
+
message: string,
|
|
558
|
+
public status: number,
|
|
559
|
+
public responseBody: string
|
|
560
|
+
) {
|
|
561
|
+
super(message);
|
|
562
|
+
this.name = "WordPressAPIError";
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
interface WordPressProviderConfig {
|
|
567
|
+
url: string;
|
|
568
|
+
apiKey: string;
|
|
569
|
+
organizationId: string;
|
|
570
|
+
version?: string;
|
|
571
|
+
customPostTypes?: string[];
|
|
572
|
+
}
|
|
573
|
+
```
|
|
574
|
+
|
|
575
|
+
**Complete NotionProvider Things Implementation:**
|
|
576
|
+
|
|
577
|
+
```typescript
|
|
578
|
+
import { Client } from "@notionhq/client";
|
|
579
|
+
|
|
580
|
+
class NotionProvider implements DataProvider {
|
|
581
|
+
private notion: Client;
|
|
582
|
+
private config: NotionProviderConfig;
|
|
583
|
+
|
|
584
|
+
constructor(config: NotionProviderConfig) {
|
|
585
|
+
this.config = config;
|
|
586
|
+
this.notion = new Client({ auth: config.apiKey });
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
things: ThingOperations = {
|
|
590
|
+
list: async (filter) => {
|
|
591
|
+
const databaseId = this.config.databaseIds[filter.type];
|
|
592
|
+
if (!databaseId) {
|
|
593
|
+
throw new Error(`No Notion database configured for type: ${filter.type}`);
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
// Build Notion filter
|
|
597
|
+
const notionFilter: any = {};
|
|
598
|
+
|
|
599
|
+
if (filter.status) {
|
|
600
|
+
notionFilter.property = "Status";
|
|
601
|
+
notionFilter.select = {
|
|
602
|
+
equals: this.mapOntologyStatusToNotion(filter.status),
|
|
603
|
+
};
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
// Query Notion database
|
|
607
|
+
const response = await this.notion.databases.query({
|
|
608
|
+
database_id: databaseId,
|
|
609
|
+
filter: Object.keys(notionFilter).length > 0 ? notionFilter : undefined,
|
|
610
|
+
page_size: filter.limit || 10,
|
|
611
|
+
start_cursor: filter.cursor,
|
|
612
|
+
});
|
|
613
|
+
|
|
614
|
+
// Transform Notion pages to ontology things
|
|
615
|
+
return Promise.all(
|
|
616
|
+
response.results.map((page: any) =>
|
|
617
|
+
this.transformNotionPageToThing(page, filter.type)
|
|
618
|
+
)
|
|
619
|
+
);
|
|
620
|
+
},
|
|
621
|
+
|
|
622
|
+
create: async (args) => {
|
|
623
|
+
const databaseId = this.config.databaseIds[args.type];
|
|
624
|
+
if (!databaseId) {
|
|
625
|
+
throw new Error(`No Notion database configured for type: ${args.type}`);
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
// Transform ontology thing to Notion page
|
|
629
|
+
const properties = this.transformThingPropertiesToNotion(args);
|
|
630
|
+
|
|
631
|
+
// Create page in Notion
|
|
632
|
+
const response = await this.notion.pages.create({
|
|
633
|
+
parent: { database_id: databaseId },
|
|
634
|
+
properties,
|
|
635
|
+
});
|
|
636
|
+
|
|
637
|
+
// Transform back to ontology thing
|
|
638
|
+
return this.transformNotionPageToThing(response, args.type);
|
|
639
|
+
},
|
|
640
|
+
|
|
641
|
+
update: async (id, updates) => {
|
|
642
|
+
const notionId = this.oneIdToNotionId(id);
|
|
643
|
+
|
|
644
|
+
// Build Notion properties update
|
|
645
|
+
const properties: any = {};
|
|
646
|
+
|
|
647
|
+
if (updates.name) {
|
|
648
|
+
properties.Name = { title: [{ text: { content: updates.name } }] };
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
if (updates.status) {
|
|
652
|
+
properties.Status = {
|
|
653
|
+
select: { name: this.mapOntologyStatusToNotion(updates.status) },
|
|
654
|
+
};
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
if (updates.properties) {
|
|
658
|
+
Object.assign(
|
|
659
|
+
properties,
|
|
660
|
+
this.transformPropertiesToNotionUpdate(updates.properties)
|
|
661
|
+
);
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
// Update page in Notion
|
|
665
|
+
const response = await this.notion.pages.update({
|
|
666
|
+
page_id: notionId,
|
|
667
|
+
properties,
|
|
668
|
+
});
|
|
669
|
+
|
|
670
|
+
return this.transformNotionPageToThing(response, updates.type);
|
|
671
|
+
},
|
|
672
|
+
|
|
673
|
+
delete: async (id) => {
|
|
674
|
+
const notionId = this.oneIdToNotionId(id);
|
|
675
|
+
|
|
676
|
+
// Notion doesn't have true delete, only archive
|
|
677
|
+
await this.notion.pages.update({
|
|
678
|
+
page_id: notionId,
|
|
679
|
+
archived: true,
|
|
680
|
+
});
|
|
681
|
+
},
|
|
682
|
+
|
|
683
|
+
get: async (id) => {
|
|
684
|
+
const notionId = this.oneIdToNotionId(id);
|
|
685
|
+
const response = await this.notion.pages.retrieve({ page_id: notionId });
|
|
686
|
+
|
|
687
|
+
// Determine type from database
|
|
688
|
+
const databaseId = (response as any).parent.database_id;
|
|
689
|
+
const type = this.findTypeByDatabaseId(databaseId);
|
|
690
|
+
|
|
691
|
+
return this.transformNotionPageToThing(response, type);
|
|
692
|
+
},
|
|
693
|
+
};
|
|
694
|
+
|
|
695
|
+
// Transform Notion page to ONE thing
|
|
696
|
+
private async transformNotionPageToThing(
|
|
697
|
+
page: any,
|
|
698
|
+
thingType: string
|
|
699
|
+
): Promise<Thing> {
|
|
700
|
+
const props = page.properties;
|
|
701
|
+
|
|
702
|
+
// Extract name (usually the title property)
|
|
703
|
+
let name = "Untitled";
|
|
704
|
+
const titleProp = Object.values(props).find(
|
|
705
|
+
(p: any) => p.type === "title"
|
|
706
|
+
) as any;
|
|
707
|
+
if (titleProp?.title?.[0]?.text?.content) {
|
|
708
|
+
name = titleProp.title[0].text.content;
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
// Extract status
|
|
712
|
+
let status = "draft";
|
|
713
|
+
if (props.Status?.select?.name) {
|
|
714
|
+
status = this.mapNotionStatusToOntology(props.Status.select.name);
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
// Extract all properties
|
|
718
|
+
const properties = await this.extractNotionProperties(props);
|
|
719
|
+
|
|
720
|
+
return {
|
|
721
|
+
_id: this.notionIdToOneId(page.id),
|
|
722
|
+
type: thingType,
|
|
723
|
+
name,
|
|
724
|
+
organizationId: this.config.organizationId,
|
|
725
|
+
status,
|
|
726
|
+
properties: {
|
|
727
|
+
...properties,
|
|
728
|
+
notionPageId: page.id,
|
|
729
|
+
notionUrl: page.url,
|
|
730
|
+
notionIcon: page.icon,
|
|
731
|
+
notionCover: page.cover,
|
|
732
|
+
},
|
|
733
|
+
createdAt: new Date(page.created_time).getTime(),
|
|
734
|
+
updatedAt: new Date(page.last_edited_time).getTime(),
|
|
735
|
+
};
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
// Transform ontology thing to Notion properties
|
|
739
|
+
private transformThingPropertiesToNotion(thing: any): any {
|
|
740
|
+
const properties: any = {
|
|
741
|
+
Name: {
|
|
742
|
+
title: [{ text: { content: thing.name } }],
|
|
743
|
+
},
|
|
744
|
+
Status: {
|
|
745
|
+
select: { name: this.mapOntologyStatusToNotion(thing.status || "draft") },
|
|
746
|
+
},
|
|
747
|
+
};
|
|
748
|
+
|
|
749
|
+
// Organization (relation or text)
|
|
750
|
+
if (thing.organizationId) {
|
|
751
|
+
properties["Organization ID"] = {
|
|
752
|
+
rich_text: [{ text: { content: thing.organizationId } }],
|
|
753
|
+
};
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
// Map common properties
|
|
757
|
+
if (thing.properties) {
|
|
758
|
+
if (thing.properties.content) {
|
|
759
|
+
properties.Content = {
|
|
760
|
+
rich_text: [{ text: { content: thing.properties.content } }],
|
|
761
|
+
};
|
|
762
|
+
}
|
|
763
|
+
if (thing.properties.duration) {
|
|
764
|
+
properties.Duration = { number: thing.properties.duration };
|
|
765
|
+
}
|
|
766
|
+
if (thing.properties.price) {
|
|
767
|
+
properties.Price = { number: thing.properties.price };
|
|
768
|
+
}
|
|
769
|
+
if (thing.properties.currency) {
|
|
770
|
+
properties.Currency = {
|
|
771
|
+
select: { name: thing.properties.currency },
|
|
772
|
+
};
|
|
773
|
+
}
|
|
774
|
+
if (thing.properties.difficulty) {
|
|
775
|
+
properties.Difficulty = {
|
|
776
|
+
select: { name: thing.properties.difficulty },
|
|
777
|
+
};
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
// Store full properties as JSON in a text field
|
|
781
|
+
properties["ONE Properties"] = {
|
|
782
|
+
rich_text: [{ text: { content: JSON.stringify(thing.properties) } }],
|
|
783
|
+
};
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
return properties;
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
// Transform Notion properties update
|
|
790
|
+
private transformPropertiesToNotionUpdate(properties: any): any {
|
|
791
|
+
const notionProps: any = {};
|
|
792
|
+
|
|
793
|
+
if (properties.content) {
|
|
794
|
+
notionProps.Content = {
|
|
795
|
+
rich_text: [{ text: { content: properties.content } }],
|
|
796
|
+
};
|
|
797
|
+
}
|
|
798
|
+
if (properties.duration !== undefined) {
|
|
799
|
+
notionProps.Duration = { number: properties.duration };
|
|
800
|
+
}
|
|
801
|
+
if (properties.price !== undefined) {
|
|
802
|
+
notionProps.Price = { number: properties.price };
|
|
803
|
+
}
|
|
804
|
+
if (properties.currency) {
|
|
805
|
+
notionProps.Currency = { select: { name: properties.currency } };
|
|
806
|
+
}
|
|
807
|
+
if (properties.difficulty) {
|
|
808
|
+
notionProps.Difficulty = { select: { name: properties.difficulty } };
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
// Update full properties JSON
|
|
812
|
+
notionProps["ONE Properties"] = {
|
|
813
|
+
rich_text: [{ text: { content: JSON.stringify(properties) } }],
|
|
814
|
+
};
|
|
815
|
+
|
|
816
|
+
return notionProps;
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
// Extract properties from Notion page
|
|
820
|
+
private async extractNotionProperties(props: any): Promise<any> {
|
|
821
|
+
const extracted: any = {};
|
|
822
|
+
|
|
823
|
+
// Try to parse stored JSON properties
|
|
824
|
+
if (props["ONE Properties"]?.rich_text?.[0]?.text?.content) {
|
|
825
|
+
try {
|
|
826
|
+
Object.assign(
|
|
827
|
+
extracted,
|
|
828
|
+
JSON.parse(props["ONE Properties"].rich_text[0].text.content)
|
|
829
|
+
);
|
|
830
|
+
} catch (e) {
|
|
831
|
+
// Ignore parse errors
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
// Extract individual properties
|
|
836
|
+
for (const [key, value] of Object.entries(props)) {
|
|
837
|
+
const prop = value as any;
|
|
838
|
+
|
|
839
|
+
switch (prop.type) {
|
|
840
|
+
case "rich_text":
|
|
841
|
+
if (prop.rich_text?.[0]?.text?.content) {
|
|
842
|
+
extracted[this.camelCase(key)] = prop.rich_text[0].text.content;
|
|
843
|
+
}
|
|
844
|
+
break;
|
|
845
|
+
case "number":
|
|
846
|
+
if (prop.number !== null) {
|
|
847
|
+
extracted[this.camelCase(key)] = prop.number;
|
|
848
|
+
}
|
|
849
|
+
break;
|
|
850
|
+
case "select":
|
|
851
|
+
if (prop.select?.name) {
|
|
852
|
+
extracted[this.camelCase(key)] = prop.select.name;
|
|
853
|
+
}
|
|
854
|
+
break;
|
|
855
|
+
case "multi_select":
|
|
856
|
+
if (prop.multi_select) {
|
|
857
|
+
extracted[this.camelCase(key)] = prop.multi_select.map(
|
|
858
|
+
(s: any) => s.name
|
|
859
|
+
);
|
|
860
|
+
}
|
|
861
|
+
break;
|
|
862
|
+
case "date":
|
|
863
|
+
if (prop.date?.start) {
|
|
864
|
+
extracted[this.camelCase(key)] = new Date(prop.date.start).getTime();
|
|
865
|
+
}
|
|
866
|
+
break;
|
|
867
|
+
case "checkbox":
|
|
868
|
+
extracted[this.camelCase(key)] = prop.checkbox;
|
|
869
|
+
break;
|
|
870
|
+
case "url":
|
|
871
|
+
if (prop.url) {
|
|
872
|
+
extracted[this.camelCase(key)] = prop.url;
|
|
873
|
+
}
|
|
874
|
+
break;
|
|
875
|
+
case "email":
|
|
876
|
+
if (prop.email) {
|
|
877
|
+
extracted[this.camelCase(key)] = prop.email;
|
|
878
|
+
}
|
|
879
|
+
break;
|
|
880
|
+
case "phone_number":
|
|
881
|
+
if (prop.phone_number) {
|
|
882
|
+
extracted[this.camelCase(key)] = prop.phone_number;
|
|
883
|
+
}
|
|
884
|
+
break;
|
|
885
|
+
case "relation":
|
|
886
|
+
if (prop.relation) {
|
|
887
|
+
extracted[this.camelCase(key)] = prop.relation.map(
|
|
888
|
+
(r: any) => this.notionIdToOneId(r.id)
|
|
889
|
+
);
|
|
890
|
+
}
|
|
891
|
+
break;
|
|
892
|
+
}
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
return extracted;
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
// Map ontology status to Notion status
|
|
899
|
+
private mapOntologyStatusToNotion(status: string): string {
|
|
900
|
+
const mapping: Record<string, string> = {
|
|
901
|
+
draft: "Draft",
|
|
902
|
+
active: "Active",
|
|
903
|
+
published: "Published",
|
|
904
|
+
archived: "Archived",
|
|
905
|
+
pending: "Pending",
|
|
906
|
+
};
|
|
907
|
+
return mapping[status] || "Draft";
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
// Map Notion status to ontology status
|
|
911
|
+
private mapNotionStatusToOntology(notionStatus: string): string {
|
|
912
|
+
const mapping: Record<string, string> = {
|
|
913
|
+
Draft: "draft",
|
|
914
|
+
Active: "active",
|
|
915
|
+
Published: "published",
|
|
916
|
+
Archived: "archived",
|
|
917
|
+
Pending: "pending",
|
|
918
|
+
};
|
|
919
|
+
return mapping[notionStatus] || "draft";
|
|
920
|
+
}
|
|
921
|
+
|
|
922
|
+
// Convert Notion ID to ONE ID
|
|
923
|
+
private notionIdToOneId(notionId: string): string {
|
|
924
|
+
return `notion_${notionId.replace(/-/g, "")}`;
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
// Convert ONE ID to Notion ID
|
|
928
|
+
private oneIdToNotionId(oneId: string): string {
|
|
929
|
+
const match = oneId.match(/^notion_([a-f0-9]{32})$/);
|
|
930
|
+
if (!match) throw new Error(`Invalid ONE ID for Notion: ${oneId}`);
|
|
931
|
+
|
|
932
|
+
// Notion IDs have hyphens: 8-4-4-4-12
|
|
933
|
+
const id = match[1];
|
|
934
|
+
return `${id.slice(0, 8)}-${id.slice(8, 12)}-${id.slice(12, 16)}-${id.slice(
|
|
935
|
+
16,
|
|
936
|
+
20
|
|
937
|
+
)}-${id.slice(20)}`;
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
// Find type by database ID
|
|
941
|
+
private findTypeByDatabaseId(databaseId: string): string {
|
|
942
|
+
for (const [type, dbId] of Object.entries(this.config.databaseIds)) {
|
|
943
|
+
if (dbId === databaseId) return type;
|
|
944
|
+
}
|
|
945
|
+
return "unknown";
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
// Convert to camelCase
|
|
949
|
+
private camelCase(str: string): string {
|
|
950
|
+
return str
|
|
951
|
+
.replace(/(?:^\w|[A-Z]|\b\w)/g, (letter, index) =>
|
|
952
|
+
index === 0 ? letter.toLowerCase() : letter.toUpperCase()
|
|
953
|
+
)
|
|
954
|
+
.replace(/\s+/g, "");
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
interface NotionProviderConfig {
|
|
959
|
+
apiKey: string;
|
|
960
|
+
organizationId: string;
|
|
961
|
+
databaseIds: Record<string, string>; // thingType -> databaseId
|
|
962
|
+
}
|
|
963
|
+
```
|
|
964
|
+
|
|
965
|
+
### 4. Connections (Relationships)
|
|
966
|
+
|
|
967
|
+
**WordPress Connection Mapping:**
|
|
968
|
+
|
|
969
|
+
WordPress doesn't have native relationships beyond `post_parent` and `post_author`. We'll use:
|
|
970
|
+
1. **ACF Relationship Fields** - For complex relationships
|
|
971
|
+
2. **Custom Tables** - For high-volume relationships (enrollments)
|
|
972
|
+
3. **Post Meta** - For simple 1:1 relationships
|
|
973
|
+
|
|
974
|
+
**Implementation:**
|
|
975
|
+
|
|
976
|
+
```typescript
|
|
977
|
+
class WordPressProvider implements DataProvider {
|
|
978
|
+
connections: ConnectionOperations = {
|
|
979
|
+
list: async (filter) => {
|
|
980
|
+
// Query custom wp_one_connections table
|
|
981
|
+
const params = new URLSearchParams({
|
|
982
|
+
from_thing_id: filter.fromThingId || "",
|
|
983
|
+
to_thing_id: filter.toThingId || "",
|
|
984
|
+
relationship_type: filter.relationshipType || "",
|
|
985
|
+
per_page: String(filter.limit || 10),
|
|
986
|
+
});
|
|
987
|
+
|
|
988
|
+
const response = await this.api.get(
|
|
989
|
+
`/one/v1/connections?${params.toString()}`
|
|
990
|
+
);
|
|
991
|
+
const wpConnections = await response.json();
|
|
992
|
+
|
|
993
|
+
return wpConnections.map((conn: any) => ({
|
|
994
|
+
_id: this.wpIdToOneId("connection", conn.id),
|
|
995
|
+
fromThingId: conn.from_thing_id,
|
|
996
|
+
toThingId: conn.to_thing_id,
|
|
997
|
+
relationshipType: conn.relationship_type,
|
|
998
|
+
metadata: JSON.parse(conn.metadata || "{}"),
|
|
999
|
+
validFrom: conn.valid_from,
|
|
1000
|
+
validTo: conn.valid_to,
|
|
1001
|
+
createdAt: new Date(conn.created_at).getTime(),
|
|
1002
|
+
}));
|
|
1003
|
+
},
|
|
1004
|
+
|
|
1005
|
+
create: async (args) => {
|
|
1006
|
+
const wpConnection = {
|
|
1007
|
+
from_thing_id: args.fromThingId,
|
|
1008
|
+
to_thing_id: args.toThingId,
|
|
1009
|
+
relationship_type: args.relationshipType,
|
|
1010
|
+
metadata: JSON.stringify(args.metadata || {}),
|
|
1011
|
+
valid_from: args.validFrom || Date.now(),
|
|
1012
|
+
valid_to: args.validTo,
|
|
1013
|
+
};
|
|
1014
|
+
|
|
1015
|
+
const response = await this.api.post("/one/v1/connections", wpConnection);
|
|
1016
|
+
const created = await response.json();
|
|
1017
|
+
|
|
1018
|
+
return {
|
|
1019
|
+
_id: this.wpIdToOneId("connection", created.id),
|
|
1020
|
+
...args,
|
|
1021
|
+
createdAt: new Date(created.created_at).getTime(),
|
|
1022
|
+
};
|
|
1023
|
+
},
|
|
1024
|
+
|
|
1025
|
+
delete: async (id) => {
|
|
1026
|
+
const wpId = this.oneIdToWpId(id);
|
|
1027
|
+
await this.api.delete(`/one/v1/connections/${wpId}`);
|
|
1028
|
+
},
|
|
1029
|
+
|
|
1030
|
+
get: async (id) => {
|
|
1031
|
+
const wpId = this.oneIdToWpId(id);
|
|
1032
|
+
const response = await this.api.get(`/one/v1/connections/${wpId}`);
|
|
1033
|
+
const conn = await response.json();
|
|
1034
|
+
|
|
1035
|
+
return {
|
|
1036
|
+
_id: id,
|
|
1037
|
+
fromThingId: conn.from_thing_id,
|
|
1038
|
+
toThingId: conn.to_thing_id,
|
|
1039
|
+
relationshipType: conn.relationship_type,
|
|
1040
|
+
metadata: JSON.parse(conn.metadata || "{}"),
|
|
1041
|
+
validFrom: conn.valid_from,
|
|
1042
|
+
validTo: conn.valid_to,
|
|
1043
|
+
createdAt: new Date(conn.created_at).getTime(),
|
|
1044
|
+
};
|
|
1045
|
+
},
|
|
1046
|
+
};
|
|
1047
|
+
}
|
|
1048
|
+
```
|
|
1049
|
+
|
|
1050
|
+
**WordPress Custom Endpoint (`/one/v1/connections`):**
|
|
1051
|
+
|
|
1052
|
+
```php
|
|
1053
|
+
// wp-content/plugins/one-platform/includes/api/connections.php
|
|
1054
|
+
|
|
1055
|
+
add_action('rest_api_init', function () {
|
|
1056
|
+
register_rest_route('one/v1', '/connections', [
|
|
1057
|
+
'methods' => 'GET',
|
|
1058
|
+
'callback' => 'one_get_connections',
|
|
1059
|
+
'permission_callback' => function () {
|
|
1060
|
+
return current_user_can('read');
|
|
1061
|
+
}
|
|
1062
|
+
]);
|
|
1063
|
+
|
|
1064
|
+
register_rest_route('one/v1', '/connections', [
|
|
1065
|
+
'methods' => 'POST',
|
|
1066
|
+
'callback' => 'one_create_connection',
|
|
1067
|
+
'permission_callback' => function () {
|
|
1068
|
+
return current_user_can('edit_posts');
|
|
1069
|
+
}
|
|
1070
|
+
]);
|
|
1071
|
+
|
|
1072
|
+
register_rest_route('one/v1', '/connections/(?P<id>\d+)', [
|
|
1073
|
+
'methods' => 'GET',
|
|
1074
|
+
'callback' => 'one_get_connection',
|
|
1075
|
+
'permission_callback' => function () {
|
|
1076
|
+
return current_user_can('read');
|
|
1077
|
+
}
|
|
1078
|
+
]);
|
|
1079
|
+
|
|
1080
|
+
register_rest_route('one/v1', '/connections/(?P<id>\d+)', [
|
|
1081
|
+
'methods' => 'DELETE',
|
|
1082
|
+
'callback' => 'one_delete_connection',
|
|
1083
|
+
'permission_callback' => function () {
|
|
1084
|
+
return current_user_can('delete_posts');
|
|
1085
|
+
}
|
|
1086
|
+
]);
|
|
1087
|
+
});
|
|
1088
|
+
|
|
1089
|
+
function one_get_connections($request) {
|
|
1090
|
+
global $wpdb;
|
|
1091
|
+
|
|
1092
|
+
$from_thing_id = $request->get_param('from_thing_id');
|
|
1093
|
+
$to_thing_id = $request->get_param('to_thing_id');
|
|
1094
|
+
$relationship_type = $request->get_param('relationship_type');
|
|
1095
|
+
$per_page = intval($request->get_param('per_page') ?: 10);
|
|
1096
|
+
$page = intval($request->get_param('page') ?: 1);
|
|
1097
|
+
|
|
1098
|
+
$where = ['1=1'];
|
|
1099
|
+
if ($from_thing_id) {
|
|
1100
|
+
$where[] = $wpdb->prepare('from_thing_id = %s', $from_thing_id);
|
|
1101
|
+
}
|
|
1102
|
+
if ($to_thing_id) {
|
|
1103
|
+
$where[] = $wpdb->prepare('to_thing_id = %s', $to_thing_id);
|
|
1104
|
+
}
|
|
1105
|
+
if ($relationship_type) {
|
|
1106
|
+
$where[] = $wpdb->prepare('relationship_type = %s', $relationship_type);
|
|
1107
|
+
}
|
|
1108
|
+
|
|
1109
|
+
$offset = ($page - 1) * $per_page;
|
|
1110
|
+
$where_clause = implode(' AND ', $where);
|
|
1111
|
+
|
|
1112
|
+
$results = $wpdb->get_results(
|
|
1113
|
+
"SELECT * FROM {$wpdb->prefix}one_connections
|
|
1114
|
+
WHERE {$where_clause}
|
|
1115
|
+
LIMIT {$per_page} OFFSET {$offset}"
|
|
1116
|
+
);
|
|
1117
|
+
|
|
1118
|
+
return rest_ensure_response($results);
|
|
1119
|
+
}
|
|
1120
|
+
|
|
1121
|
+
function one_create_connection($request) {
|
|
1122
|
+
global $wpdb;
|
|
1123
|
+
|
|
1124
|
+
$wpdb->insert(
|
|
1125
|
+
$wpdb->prefix . 'one_connections',
|
|
1126
|
+
[
|
|
1127
|
+
'from_thing_id' => $request->get_param('from_thing_id'),
|
|
1128
|
+
'to_thing_id' => $request->get_param('to_thing_id'),
|
|
1129
|
+
'relationship_type' => $request->get_param('relationship_type'),
|
|
1130
|
+
'metadata' => $request->get_param('metadata'),
|
|
1131
|
+
'valid_from' => $request->get_param('valid_from'),
|
|
1132
|
+
'valid_to' => $request->get_param('valid_to'),
|
|
1133
|
+
'created_at' => current_time('mysql'),
|
|
1134
|
+
]
|
|
1135
|
+
);
|
|
1136
|
+
|
|
1137
|
+
$id = $wpdb->insert_id;
|
|
1138
|
+
$connection = $wpdb->get_row(
|
|
1139
|
+
$wpdb->prepare(
|
|
1140
|
+
"SELECT * FROM {$wpdb->prefix}one_connections WHERE id = %d",
|
|
1141
|
+
$id
|
|
1142
|
+
)
|
|
1143
|
+
);
|
|
1144
|
+
|
|
1145
|
+
return rest_ensure_response($connection);
|
|
1146
|
+
}
|
|
1147
|
+
```
|
|
1148
|
+
|
|
1149
|
+
**Notion Connection Mapping:**
|
|
1150
|
+
|
|
1151
|
+
Notion supports native **Relation** properties between databases. We'll map ONE connections to Notion relations.
|
|
1152
|
+
|
|
1153
|
+
```typescript
|
|
1154
|
+
class NotionProvider implements DataProvider {
|
|
1155
|
+
connections: ConnectionOperations = {
|
|
1156
|
+
list: async (filter) => {
|
|
1157
|
+
// In Notion, connections are stored as relation properties
|
|
1158
|
+
// We need to query the source database and extract relations
|
|
1159
|
+
|
|
1160
|
+
if (!filter.fromThingId) {
|
|
1161
|
+
throw new Error("Notion connections require fromThingId filter");
|
|
1162
|
+
}
|
|
1163
|
+
|
|
1164
|
+
const fromType = this.getTypeFromId(filter.fromThingId);
|
|
1165
|
+
const fromNotionId = this.oneIdToNotionId(filter.fromThingId);
|
|
1166
|
+
|
|
1167
|
+
// Get the source page
|
|
1168
|
+
const page = await this.notion.pages.retrieve({ page_id: fromNotionId });
|
|
1169
|
+
const props = (page as any).properties;
|
|
1170
|
+
|
|
1171
|
+
const connections: Connection[] = [];
|
|
1172
|
+
|
|
1173
|
+
// Extract all relation properties
|
|
1174
|
+
for (const [propName, propValue] of Object.entries(props)) {
|
|
1175
|
+
const prop = propValue as any;
|
|
1176
|
+
|
|
1177
|
+
if (prop.type === "relation" && prop.relation) {
|
|
1178
|
+
// Determine relationship type from property name
|
|
1179
|
+
const relationshipType = this.mapNotionPropertyToRelationship(propName);
|
|
1180
|
+
|
|
1181
|
+
for (const rel of prop.relation) {
|
|
1182
|
+
connections.push({
|
|
1183
|
+
_id: this.generateConnectionId(
|
|
1184
|
+
filter.fromThingId,
|
|
1185
|
+
this.notionIdToOneId(rel.id),
|
|
1186
|
+
relationshipType
|
|
1187
|
+
),
|
|
1188
|
+
fromThingId: filter.fromThingId,
|
|
1189
|
+
toThingId: this.notionIdToOneId(rel.id),
|
|
1190
|
+
relationshipType,
|
|
1191
|
+
metadata: { notionProperty: propName },
|
|
1192
|
+
createdAt: new Date((page as any).created_time).getTime(),
|
|
1193
|
+
});
|
|
1194
|
+
}
|
|
1195
|
+
}
|
|
1196
|
+
}
|
|
1197
|
+
|
|
1198
|
+
return connections;
|
|
1199
|
+
},
|
|
1200
|
+
|
|
1201
|
+
create: async (args) => {
|
|
1202
|
+
// Add relation to source page
|
|
1203
|
+
const fromNotionId = this.oneIdToNotionId(args.fromThingId);
|
|
1204
|
+
const toNotionId = this.oneIdToNotionId(args.toThingId);
|
|
1205
|
+
|
|
1206
|
+
// Determine which property to update
|
|
1207
|
+
const propertyName = this.mapRelationshipToNotionProperty(
|
|
1208
|
+
args.relationshipType
|
|
1209
|
+
);
|
|
1210
|
+
|
|
1211
|
+
// Get existing relations
|
|
1212
|
+
const page = await this.notion.pages.retrieve({ page_id: fromNotionId });
|
|
1213
|
+
const existingRelations =
|
|
1214
|
+
(page as any).properties[propertyName]?.relation || [];
|
|
1215
|
+
|
|
1216
|
+
// Add new relation
|
|
1217
|
+
await this.notion.pages.update({
|
|
1218
|
+
page_id: fromNotionId,
|
|
1219
|
+
properties: {
|
|
1220
|
+
[propertyName]: {
|
|
1221
|
+
relation: [
|
|
1222
|
+
...existingRelations.map((r: any) => ({ id: r.id })),
|
|
1223
|
+
{ id: toNotionId },
|
|
1224
|
+
],
|
|
1225
|
+
},
|
|
1226
|
+
},
|
|
1227
|
+
});
|
|
1228
|
+
|
|
1229
|
+
return {
|
|
1230
|
+
_id: this.generateConnectionId(
|
|
1231
|
+
args.fromThingId,
|
|
1232
|
+
args.toThingId,
|
|
1233
|
+
args.relationshipType
|
|
1234
|
+
),
|
|
1235
|
+
...args,
|
|
1236
|
+
createdAt: Date.now(),
|
|
1237
|
+
};
|
|
1238
|
+
},
|
|
1239
|
+
|
|
1240
|
+
delete: async (id) => {
|
|
1241
|
+
// Parse connection ID to get from/to/type
|
|
1242
|
+
const { fromThingId, toThingId, relationshipType } =
|
|
1243
|
+
this.parseConnectionId(id);
|
|
1244
|
+
|
|
1245
|
+
const fromNotionId = this.oneIdToNotionId(fromThingId);
|
|
1246
|
+
const toNotionId = this.oneIdToNotionId(toThingId);
|
|
1247
|
+
|
|
1248
|
+
// Determine which property to update
|
|
1249
|
+
const propertyName = this.mapRelationshipToNotionProperty(relationshipType);
|
|
1250
|
+
|
|
1251
|
+
// Get existing relations
|
|
1252
|
+
const page = await this.notion.pages.retrieve({ page_id: fromNotionId });
|
|
1253
|
+
const existingRelations =
|
|
1254
|
+
(page as any).properties[propertyName]?.relation || [];
|
|
1255
|
+
|
|
1256
|
+
// Remove the relation
|
|
1257
|
+
await this.notion.pages.update({
|
|
1258
|
+
page_id: fromNotionId,
|
|
1259
|
+
properties: {
|
|
1260
|
+
[propertyName]: {
|
|
1261
|
+
relation: existingRelations
|
|
1262
|
+
.filter((r: any) => r.id !== toNotionId)
|
|
1263
|
+
.map((r: any) => ({ id: r.id })),
|
|
1264
|
+
},
|
|
1265
|
+
},
|
|
1266
|
+
});
|
|
1267
|
+
},
|
|
1268
|
+
|
|
1269
|
+
get: async (id) => {
|
|
1270
|
+
// Parse connection ID
|
|
1271
|
+
const { fromThingId, toThingId, relationshipType } =
|
|
1272
|
+
this.parseConnectionId(id);
|
|
1273
|
+
|
|
1274
|
+
// Verify relation exists
|
|
1275
|
+
const connections = await this.connections.list({ fromThingId });
|
|
1276
|
+
const connection = connections.find(
|
|
1277
|
+
(c) =>
|
|
1278
|
+
c.toThingId === toThingId && c.relationshipType === relationshipType
|
|
1279
|
+
);
|
|
1280
|
+
|
|
1281
|
+
if (!connection) {
|
|
1282
|
+
throw new Error(`Connection not found: ${id}`);
|
|
1283
|
+
}
|
|
1284
|
+
|
|
1285
|
+
return connection;
|
|
1286
|
+
},
|
|
1287
|
+
};
|
|
1288
|
+
|
|
1289
|
+
// Map relationship type to Notion property name
|
|
1290
|
+
private mapRelationshipToNotionProperty(relationshipType: string): string {
|
|
1291
|
+
const mapping: Record<string, string> = {
|
|
1292
|
+
owns: "Owner",
|
|
1293
|
+
part_of: "Parent",
|
|
1294
|
+
enrolled_in: "Enrollments",
|
|
1295
|
+
authored: "Author",
|
|
1296
|
+
holds_tokens: "Token Holdings",
|
|
1297
|
+
delegated: "Delegated To",
|
|
1298
|
+
communicated: "Communicated With",
|
|
1299
|
+
transacted: "Transactions",
|
|
1300
|
+
};
|
|
1301
|
+
return mapping[relationshipType] || "Related";
|
|
1302
|
+
}
|
|
1303
|
+
|
|
1304
|
+
// Map Notion property name to relationship type
|
|
1305
|
+
private mapNotionPropertyToRelationship(propertyName: string): string {
|
|
1306
|
+
const mapping: Record<string, string> = {
|
|
1307
|
+
Owner: "owns",
|
|
1308
|
+
Parent: "part_of",
|
|
1309
|
+
Enrollments: "enrolled_in",
|
|
1310
|
+
Author: "authored",
|
|
1311
|
+
"Token Holdings": "holds_tokens",
|
|
1312
|
+
"Delegated To": "delegated",
|
|
1313
|
+
"Communicated With": "communicated",
|
|
1314
|
+
Transactions: "transacted",
|
|
1315
|
+
};
|
|
1316
|
+
return mapping[propertyName] || "related";
|
|
1317
|
+
}
|
|
1318
|
+
|
|
1319
|
+
// Generate deterministic connection ID
|
|
1320
|
+
private generateConnectionId(
|
|
1321
|
+
fromThingId: string,
|
|
1322
|
+
toThingId: string,
|
|
1323
|
+
relationshipType: string
|
|
1324
|
+
): string {
|
|
1325
|
+
return `notion_conn_${fromThingId}_${toThingId}_${relationshipType}`;
|
|
1326
|
+
}
|
|
1327
|
+
|
|
1328
|
+
// Parse connection ID
|
|
1329
|
+
private parseConnectionId(id: string): {
|
|
1330
|
+
fromThingId: string;
|
|
1331
|
+
toThingId: string;
|
|
1332
|
+
relationshipType: string;
|
|
1333
|
+
} {
|
|
1334
|
+
const match = id.match(/^notion_conn_(.+)_(.+)_(.+)$/);
|
|
1335
|
+
if (!match) throw new Error(`Invalid Notion connection ID: ${id}`);
|
|
1336
|
+
return {
|
|
1337
|
+
fromThingId: match[1],
|
|
1338
|
+
toThingId: match[2],
|
|
1339
|
+
relationshipType: match[3],
|
|
1340
|
+
};
|
|
1341
|
+
}
|
|
1342
|
+
|
|
1343
|
+
// Get type from ONE ID
|
|
1344
|
+
private getTypeFromId(oneId: string): string {
|
|
1345
|
+
// In practice, you'd look this up from the database
|
|
1346
|
+
// For now, we'll extract from the ID pattern if it includes type
|
|
1347
|
+
return "unknown";
|
|
1348
|
+
}
|
|
1349
|
+
}
|
|
1350
|
+
```
|
|
1351
|
+
|
|
1352
|
+
### 5. Events (Action Tracking)
|
|
1353
|
+
|
|
1354
|
+
**Challenge:** WordPress and Notion don't have native event logs matching ONE's comprehensive event system.
|
|
1355
|
+
|
|
1356
|
+
**Solution:**
|
|
1357
|
+
- **Option 1:** Store events in ONE's Convex backend (hybrid approach)
|
|
1358
|
+
- **Option 2:** Store events in external system's custom table/database
|
|
1359
|
+
- **Option 3:** Use external system's audit logs where available
|
|
1360
|
+
|
|
1361
|
+
**WordPress Events Implementation:**
|
|
1362
|
+
|
|
1363
|
+
```typescript
|
|
1364
|
+
class WordPressProvider implements DataProvider {
|
|
1365
|
+
events: EventOperations = {
|
|
1366
|
+
list: async (filter) => {
|
|
1367
|
+
// Query custom wp_one_events table
|
|
1368
|
+
const params = new URLSearchParams({
|
|
1369
|
+
type: filter.type || "",
|
|
1370
|
+
actor_id: filter.actorId || "",
|
|
1371
|
+
target_id: filter.targetId || "",
|
|
1372
|
+
from: String(filter.timestampFrom || 0),
|
|
1373
|
+
to: String(filter.timestampTo || Date.now()),
|
|
1374
|
+
per_page: String(filter.limit || 10),
|
|
1375
|
+
});
|
|
1376
|
+
|
|
1377
|
+
const response = await this.api.get(`/one/v1/events?${params.toString()}`);
|
|
1378
|
+
const wpEvents = await response.json();
|
|
1379
|
+
|
|
1380
|
+
return wpEvents.map((event: any) => ({
|
|
1381
|
+
_id: this.wpIdToOneId("event", event.id),
|
|
1382
|
+
type: event.type,
|
|
1383
|
+
actorId: event.actor_id,
|
|
1384
|
+
targetId: event.target_id,
|
|
1385
|
+
timestamp: new Date(event.timestamp).getTime(),
|
|
1386
|
+
metadata: JSON.parse(event.metadata || "{}"),
|
|
1387
|
+
}));
|
|
1388
|
+
},
|
|
1389
|
+
|
|
1390
|
+
create: async (args) => {
|
|
1391
|
+
const wpEvent = {
|
|
1392
|
+
type: args.type,
|
|
1393
|
+
actor_id: args.actorId,
|
|
1394
|
+
target_id: args.targetId,
|
|
1395
|
+
timestamp: new Date(args.timestamp).toISOString(),
|
|
1396
|
+
metadata: JSON.stringify(args.metadata || {}),
|
|
1397
|
+
};
|
|
1398
|
+
|
|
1399
|
+
const response = await this.api.post("/one/v1/events", wpEvent);
|
|
1400
|
+
const created = await response.json();
|
|
1401
|
+
|
|
1402
|
+
return {
|
|
1403
|
+
_id: this.wpIdToOneId("event", created.id),
|
|
1404
|
+
...args,
|
|
1405
|
+
};
|
|
1406
|
+
},
|
|
1407
|
+
|
|
1408
|
+
get: async (id) => {
|
|
1409
|
+
const wpId = this.oneIdToWpId(id);
|
|
1410
|
+
const response = await this.api.get(`/one/v1/events/${wpId}`);
|
|
1411
|
+
const event = await response.json();
|
|
1412
|
+
|
|
1413
|
+
return {
|
|
1414
|
+
_id: id,
|
|
1415
|
+
type: event.type,
|
|
1416
|
+
actorId: event.actor_id,
|
|
1417
|
+
targetId: event.target_id,
|
|
1418
|
+
timestamp: new Date(event.timestamp).getTime(),
|
|
1419
|
+
metadata: JSON.parse(event.metadata || "{}"),
|
|
1420
|
+
};
|
|
1421
|
+
},
|
|
1422
|
+
};
|
|
1423
|
+
}
|
|
1424
|
+
```
|
|
1425
|
+
|
|
1426
|
+
**Notion Events Implementation (Hybrid):**
|
|
1427
|
+
|
|
1428
|
+
Since Notion doesn't support custom tables, we'll use a hybrid approach: store events in Convex but reference Notion IDs.
|
|
1429
|
+
|
|
1430
|
+
```typescript
|
|
1431
|
+
class NotionProvider implements DataProvider {
|
|
1432
|
+
private convexEventStore: ConvexClient;
|
|
1433
|
+
|
|
1434
|
+
constructor(config: NotionProviderConfig) {
|
|
1435
|
+
this.config = config;
|
|
1436
|
+
this.notion = new Client({ auth: config.apiKey });
|
|
1437
|
+
|
|
1438
|
+
// Use Convex for event storage
|
|
1439
|
+
this.convexEventStore = new ConvexClient(config.convexUrl);
|
|
1440
|
+
}
|
|
1441
|
+
|
|
1442
|
+
events: EventOperations = {
|
|
1443
|
+
list: async (filter) => {
|
|
1444
|
+
// Query Convex events table, filtered by Notion provider
|
|
1445
|
+
return this.convexEventStore.query("events:list", {
|
|
1446
|
+
...filter,
|
|
1447
|
+
metadata: {
|
|
1448
|
+
...filter.metadata,
|
|
1449
|
+
provider: "notion",
|
|
1450
|
+
organizationId: this.config.organizationId,
|
|
1451
|
+
},
|
|
1452
|
+
});
|
|
1453
|
+
},
|
|
1454
|
+
|
|
1455
|
+
create: async (args) => {
|
|
1456
|
+
// Store event in Convex with Notion metadata
|
|
1457
|
+
return this.convexEventStore.mutation("events:create", {
|
|
1458
|
+
...args,
|
|
1459
|
+
metadata: {
|
|
1460
|
+
...args.metadata,
|
|
1461
|
+
provider: "notion",
|
|
1462
|
+
organizationId: this.config.organizationId,
|
|
1463
|
+
},
|
|
1464
|
+
});
|
|
1465
|
+
},
|
|
1466
|
+
|
|
1467
|
+
get: async (id) => {
|
|
1468
|
+
return this.convexEventStore.query("events:get", { id });
|
|
1469
|
+
},
|
|
1470
|
+
};
|
|
1471
|
+
}
|
|
1472
|
+
```
|
|
1473
|
+
|
|
1474
|
+
### 6. Knowledge (Semantic Understanding)
|
|
1475
|
+
|
|
1476
|
+
**WordPress Knowledge Implementation:**
|
|
1477
|
+
|
|
1478
|
+
```typescript
|
|
1479
|
+
class WordPressProvider implements DataProvider {
|
|
1480
|
+
knowledge: KnowledgeOperations = {
|
|
1481
|
+
list: async (filter) => {
|
|
1482
|
+
// Query custom wp_one_knowledge table
|
|
1483
|
+
const params = new URLSearchParams({
|
|
1484
|
+
knowledge_type: filter.knowledgeType || "",
|
|
1485
|
+
per_page: String(filter.limit || 10),
|
|
1486
|
+
});
|
|
1487
|
+
|
|
1488
|
+
if (filter.labels) {
|
|
1489
|
+
params.append("labels", filter.labels.join(","));
|
|
1490
|
+
}
|
|
1491
|
+
|
|
1492
|
+
const response = await this.api.get(
|
|
1493
|
+
`/one/v1/knowledge?${params.toString()}`
|
|
1494
|
+
);
|
|
1495
|
+
const wpKnowledge = await response.json();
|
|
1496
|
+
|
|
1497
|
+
return wpKnowledge.map((k: any) => ({
|
|
1498
|
+
_id: this.wpIdToOneId("knowledge", k.id),
|
|
1499
|
+
knowledgeType: k.knowledge_type,
|
|
1500
|
+
text: k.text,
|
|
1501
|
+
embedding: JSON.parse(k.embedding || "[]"),
|
|
1502
|
+
embeddingModel: k.embedding_model,
|
|
1503
|
+
embeddingDim: k.embedding_dim,
|
|
1504
|
+
sourceThingId: k.source_thing_id,
|
|
1505
|
+
sourceField: k.source_field,
|
|
1506
|
+
labels: JSON.parse(k.labels || "[]"),
|
|
1507
|
+
metadata: JSON.parse(k.metadata || "{}"),
|
|
1508
|
+
createdAt: new Date(k.created_at).getTime(),
|
|
1509
|
+
updatedAt: new Date(k.updated_at).getTime(),
|
|
1510
|
+
}));
|
|
1511
|
+
},
|
|
1512
|
+
|
|
1513
|
+
create: async (args) => {
|
|
1514
|
+
const wpKnowledge = {
|
|
1515
|
+
knowledge_type: args.knowledgeType,
|
|
1516
|
+
text: args.text,
|
|
1517
|
+
embedding: JSON.stringify(args.embedding),
|
|
1518
|
+
embedding_model: args.embeddingModel,
|
|
1519
|
+
embedding_dim: args.embeddingDim,
|
|
1520
|
+
source_thing_id: args.sourceThingId,
|
|
1521
|
+
source_field: args.sourceField,
|
|
1522
|
+
labels: JSON.stringify(args.labels || []),
|
|
1523
|
+
metadata: JSON.stringify(args.metadata || {}),
|
|
1524
|
+
};
|
|
1525
|
+
|
|
1526
|
+
const response = await this.api.post("/one/v1/knowledge", wpKnowledge);
|
|
1527
|
+
const created = await response.json();
|
|
1528
|
+
|
|
1529
|
+
return {
|
|
1530
|
+
_id: this.wpIdToOneId("knowledge", created.id),
|
|
1531
|
+
...args,
|
|
1532
|
+
createdAt: new Date(created.created_at).getTime(),
|
|
1533
|
+
updatedAt: new Date(created.updated_at).getTime(),
|
|
1534
|
+
};
|
|
1535
|
+
},
|
|
1536
|
+
|
|
1537
|
+
search: async (query) => {
|
|
1538
|
+
// Use Convex for vector search (WordPress doesn't support it natively)
|
|
1539
|
+
// This is a hybrid approach
|
|
1540
|
+
throw new Error("Vector search not supported by WordPressProvider");
|
|
1541
|
+
},
|
|
1542
|
+
|
|
1543
|
+
get: async (id) => {
|
|
1544
|
+
const wpId = this.oneIdToWpId(id);
|
|
1545
|
+
const response = await this.api.get(`/one/v1/knowledge/${wpId}`);
|
|
1546
|
+
const k = await response.json();
|
|
1547
|
+
|
|
1548
|
+
return {
|
|
1549
|
+
_id: id,
|
|
1550
|
+
knowledgeType: k.knowledge_type,
|
|
1551
|
+
text: k.text,
|
|
1552
|
+
embedding: JSON.parse(k.embedding || "[]"),
|
|
1553
|
+
embeddingModel: k.embedding_model,
|
|
1554
|
+
embeddingDim: k.embedding_dim,
|
|
1555
|
+
sourceThingId: k.source_thing_id,
|
|
1556
|
+
sourceField: k.source_field,
|
|
1557
|
+
labels: JSON.parse(k.labels || "[]"),
|
|
1558
|
+
metadata: JSON.parse(k.metadata || "{}"),
|
|
1559
|
+
createdAt: new Date(k.created_at).getTime(),
|
|
1560
|
+
updatedAt: new Date(k.updated_at).getTime(),
|
|
1561
|
+
};
|
|
1562
|
+
},
|
|
1563
|
+
|
|
1564
|
+
delete: async (id) => {
|
|
1565
|
+
const wpId = this.oneIdToWpId(id);
|
|
1566
|
+
await this.api.delete(`/one/v1/knowledge/${wpId}`);
|
|
1567
|
+
},
|
|
1568
|
+
};
|
|
1569
|
+
}
|
|
1570
|
+
```
|
|
1571
|
+
|
|
1572
|
+
**Notion Knowledge Implementation (Hybrid):**
|
|
1573
|
+
|
|
1574
|
+
Notion doesn't support vector embeddings natively. Use Convex for knowledge storage.
|
|
1575
|
+
|
|
1576
|
+
```typescript
|
|
1577
|
+
class NotionProvider implements DataProvider {
|
|
1578
|
+
knowledge: KnowledgeOperations = {
|
|
1579
|
+
list: async (filter) => {
|
|
1580
|
+
// Use Convex for knowledge storage
|
|
1581
|
+
return this.convexEventStore.query("knowledge:list", {
|
|
1582
|
+
...filter,
|
|
1583
|
+
metadata: {
|
|
1584
|
+
...filter.metadata,
|
|
1585
|
+
provider: "notion",
|
|
1586
|
+
organizationId: this.config.organizationId,
|
|
1587
|
+
},
|
|
1588
|
+
});
|
|
1589
|
+
},
|
|
1590
|
+
|
|
1591
|
+
create: async (args) => {
|
|
1592
|
+
return this.convexEventStore.mutation("knowledge:create", {
|
|
1593
|
+
...args,
|
|
1594
|
+
metadata: {
|
|
1595
|
+
...args.metadata,
|
|
1596
|
+
provider: "notion",
|
|
1597
|
+
organizationId: this.config.organizationId,
|
|
1598
|
+
},
|
|
1599
|
+
});
|
|
1600
|
+
},
|
|
1601
|
+
|
|
1602
|
+
search: async (query) => {
|
|
1603
|
+
return this.convexEventStore.query("knowledge:search", {
|
|
1604
|
+
...query,
|
|
1605
|
+
metadata: {
|
|
1606
|
+
provider: "notion",
|
|
1607
|
+
organizationId: this.config.organizationId,
|
|
1608
|
+
},
|
|
1609
|
+
});
|
|
1610
|
+
},
|
|
1611
|
+
|
|
1612
|
+
get: async (id) => {
|
|
1613
|
+
return this.convexEventStore.query("knowledge:get", { id });
|
|
1614
|
+
},
|
|
1615
|
+
|
|
1616
|
+
delete: async (id) => {
|
|
1617
|
+
return this.convexEventStore.mutation("knowledge:delete", { id });
|
|
1618
|
+
},
|
|
1619
|
+
};
|
|
1620
|
+
}
|
|
1621
|
+
```
|
|
1622
|
+
|
|
1623
|
+
---
|
|
1624
|
+
|
|
1625
|
+
## CompositeProvider (Multi-Backend Routing)
|
|
1626
|
+
|
|
1627
|
+
**Use Case:** A single organization wants to use different backends for different entity types.
|
|
1628
|
+
|
|
1629
|
+
**Example:** WordPress for courses/lessons, Notion for internal documentation, Convex for events/knowledge.
|
|
1630
|
+
|
|
1631
|
+
**Implementation:**
|
|
1632
|
+
|
|
1633
|
+
```typescript
|
|
1634
|
+
class CompositeProvider implements DataProvider {
|
|
1635
|
+
private providers: Map<string, DataProvider>;
|
|
1636
|
+
private routingRules: RoutingRules;
|
|
1637
|
+
|
|
1638
|
+
constructor(config: CompositeProviderConfig) {
|
|
1639
|
+
this.providers = new Map();
|
|
1640
|
+
this.routingRules = config.routingRules;
|
|
1641
|
+
|
|
1642
|
+
// Register all providers
|
|
1643
|
+
for (const [name, providerConfig] of Object.entries(config.providers)) {
|
|
1644
|
+
this.providers.set(name, this.createProvider(providerConfig));
|
|
1645
|
+
}
|
|
1646
|
+
}
|
|
1647
|
+
|
|
1648
|
+
things: ThingOperations = {
|
|
1649
|
+
list: async (filter) => {
|
|
1650
|
+
const providerName = this.routeByThingType(filter.type);
|
|
1651
|
+
const provider = this.getProvider(providerName);
|
|
1652
|
+
return provider.things.list(filter);
|
|
1653
|
+
},
|
|
1654
|
+
|
|
1655
|
+
create: async (args) => {
|
|
1656
|
+
const providerName = this.routeByThingType(args.type);
|
|
1657
|
+
const provider = this.getProvider(providerName);
|
|
1658
|
+
return provider.things.create(args);
|
|
1659
|
+
},
|
|
1660
|
+
|
|
1661
|
+
update: async (id, updates) => {
|
|
1662
|
+
const providerName = this.routeByThingId(id);
|
|
1663
|
+
const provider = this.getProvider(providerName);
|
|
1664
|
+
return provider.things.update(id, updates);
|
|
1665
|
+
},
|
|
1666
|
+
|
|
1667
|
+
delete: async (id) => {
|
|
1668
|
+
const providerName = this.routeByThingId(id);
|
|
1669
|
+
const provider = this.getProvider(providerName);
|
|
1670
|
+
return provider.things.delete(id);
|
|
1671
|
+
},
|
|
1672
|
+
|
|
1673
|
+
get: async (id) => {
|
|
1674
|
+
const providerName = this.routeByThingId(id);
|
|
1675
|
+
const provider = this.getProvider(providerName);
|
|
1676
|
+
return provider.things.get(id);
|
|
1677
|
+
},
|
|
1678
|
+
};
|
|
1679
|
+
|
|
1680
|
+
connections: ConnectionOperations = {
|
|
1681
|
+
list: async (filter) => {
|
|
1682
|
+
// Cross-provider connections require querying all providers
|
|
1683
|
+
if (!filter.fromThingId && !filter.toThingId) {
|
|
1684
|
+
throw new Error(
|
|
1685
|
+
"CompositeProvider connections.list requires fromThingId or toThingId"
|
|
1686
|
+
);
|
|
1687
|
+
}
|
|
1688
|
+
|
|
1689
|
+
const providerName = this.routeByThingId(
|
|
1690
|
+
filter.fromThingId || filter.toThingId!
|
|
1691
|
+
);
|
|
1692
|
+
const provider = this.getProvider(providerName);
|
|
1693
|
+
return provider.connections.list(filter);
|
|
1694
|
+
},
|
|
1695
|
+
|
|
1696
|
+
create: async (args) => {
|
|
1697
|
+
// Determine which provider should own the connection
|
|
1698
|
+
// Default: same provider as fromThing
|
|
1699
|
+
const providerName = this.routeByThingId(args.fromThingId);
|
|
1700
|
+
const provider = this.getProvider(providerName);
|
|
1701
|
+
return provider.connections.create(args);
|
|
1702
|
+
},
|
|
1703
|
+
|
|
1704
|
+
delete: async (id) => {
|
|
1705
|
+
const providerName = this.routeByConnectionId(id);
|
|
1706
|
+
const provider = this.getProvider(providerName);
|
|
1707
|
+
return provider.connections.delete(id);
|
|
1708
|
+
},
|
|
1709
|
+
|
|
1710
|
+
get: async (id) => {
|
|
1711
|
+
const providerName = this.routeByConnectionId(id);
|
|
1712
|
+
const provider = this.getProvider(providerName);
|
|
1713
|
+
return provider.connections.get(id);
|
|
1714
|
+
},
|
|
1715
|
+
};
|
|
1716
|
+
|
|
1717
|
+
events: EventOperations = {
|
|
1718
|
+
list: async (filter) => {
|
|
1719
|
+
// Events are always stored in the designated event provider
|
|
1720
|
+
const providerName = this.routingRules.eventsProvider || "convex";
|
|
1721
|
+
const provider = this.getProvider(providerName);
|
|
1722
|
+
return provider.events.list(filter);
|
|
1723
|
+
},
|
|
1724
|
+
|
|
1725
|
+
create: async (args) => {
|
|
1726
|
+
const providerName = this.routingRules.eventsProvider || "convex";
|
|
1727
|
+
const provider = this.getProvider(providerName);
|
|
1728
|
+
return provider.events.create(args);
|
|
1729
|
+
},
|
|
1730
|
+
|
|
1731
|
+
get: async (id) => {
|
|
1732
|
+
const providerName = this.routingRules.eventsProvider || "convex";
|
|
1733
|
+
const provider = this.getProvider(providerName);
|
|
1734
|
+
return provider.events.get(id);
|
|
1735
|
+
},
|
|
1736
|
+
};
|
|
1737
|
+
|
|
1738
|
+
knowledge: KnowledgeOperations = {
|
|
1739
|
+
list: async (filter) => {
|
|
1740
|
+
// Knowledge is always stored in the designated knowledge provider
|
|
1741
|
+
const providerName = this.routingRules.knowledgeProvider || "convex";
|
|
1742
|
+
const provider = this.getProvider(providerName);
|
|
1743
|
+
return provider.knowledge.list(filter);
|
|
1744
|
+
},
|
|
1745
|
+
|
|
1746
|
+
create: async (args) => {
|
|
1747
|
+
const providerName = this.routingRules.knowledgeProvider || "convex";
|
|
1748
|
+
const provider = this.getProvider(providerName);
|
|
1749
|
+
return provider.knowledge.create(args);
|
|
1750
|
+
},
|
|
1751
|
+
|
|
1752
|
+
search: async (query) => {
|
|
1753
|
+
const providerName = this.routingRules.knowledgeProvider || "convex";
|
|
1754
|
+
const provider = this.getProvider(providerName);
|
|
1755
|
+
return provider.knowledge.search(query);
|
|
1756
|
+
},
|
|
1757
|
+
|
|
1758
|
+
get: async (id) => {
|
|
1759
|
+
const providerName = this.routingRules.knowledgeProvider || "convex";
|
|
1760
|
+
const provider = this.getProvider(providerName);
|
|
1761
|
+
return provider.knowledge.get(id);
|
|
1762
|
+
},
|
|
1763
|
+
|
|
1764
|
+
delete: async (id) => {
|
|
1765
|
+
const providerName = this.routingRules.knowledgeProvider || "convex";
|
|
1766
|
+
const provider = this.getProvider(providerName);
|
|
1767
|
+
return provider.knowledge.delete(id);
|
|
1768
|
+
},
|
|
1769
|
+
};
|
|
1770
|
+
|
|
1771
|
+
organizations: OrganizationOperations = {
|
|
1772
|
+
list: async (filter) => {
|
|
1773
|
+
// Organizations are always in the designated org provider
|
|
1774
|
+
const providerName = this.routingRules.organizationsProvider || "convex";
|
|
1775
|
+
const provider = this.getProvider(providerName);
|
|
1776
|
+
return provider.organizations.list(filter);
|
|
1777
|
+
},
|
|
1778
|
+
|
|
1779
|
+
create: async (args) => {
|
|
1780
|
+
const providerName = this.routingRules.organizationsProvider || "convex";
|
|
1781
|
+
const provider = this.getProvider(providerName);
|
|
1782
|
+
return provider.organizations.create(args);
|
|
1783
|
+
},
|
|
1784
|
+
|
|
1785
|
+
update: async (id, updates) => {
|
|
1786
|
+
const providerName = this.routingRules.organizationsProvider || "convex";
|
|
1787
|
+
const provider = this.getProvider(providerName);
|
|
1788
|
+
return provider.organizations.update(id, updates);
|
|
1789
|
+
},
|
|
1790
|
+
|
|
1791
|
+
delete: async (id) => {
|
|
1792
|
+
const providerName = this.routingRules.organizationsProvider || "convex";
|
|
1793
|
+
const provider = this.getProvider(providerName);
|
|
1794
|
+
return provider.organizations.delete(id);
|
|
1795
|
+
},
|
|
1796
|
+
|
|
1797
|
+
get: async (id) => {
|
|
1798
|
+
const providerName = this.routingRules.organizationsProvider || "convex";
|
|
1799
|
+
const provider = this.getProvider(providerName);
|
|
1800
|
+
return provider.organizations.get(id);
|
|
1801
|
+
},
|
|
1802
|
+
};
|
|
1803
|
+
|
|
1804
|
+
people: PeopleOperations = {
|
|
1805
|
+
list: async (filter) => {
|
|
1806
|
+
// People routing can be by organization or type
|
|
1807
|
+
const providerName = this.routingRules.peopleProvider || "convex";
|
|
1808
|
+
const provider = this.getProvider(providerName);
|
|
1809
|
+
return provider.people.list(filter);
|
|
1810
|
+
},
|
|
1811
|
+
|
|
1812
|
+
create: async (args) => {
|
|
1813
|
+
const providerName = this.routingRules.peopleProvider || "convex";
|
|
1814
|
+
const provider = this.getProvider(providerName);
|
|
1815
|
+
return provider.people.create(args);
|
|
1816
|
+
},
|
|
1817
|
+
|
|
1818
|
+
update: async (id, updates) => {
|
|
1819
|
+
const providerName = this.routeByThingId(id);
|
|
1820
|
+
const provider = this.getProvider(providerName);
|
|
1821
|
+
return provider.people.update(id, updates);
|
|
1822
|
+
},
|
|
1823
|
+
|
|
1824
|
+
delete: async (id) => {
|
|
1825
|
+
const providerName = this.routeByThingId(id);
|
|
1826
|
+
const provider = this.getProvider(providerName);
|
|
1827
|
+
return provider.people.delete(id);
|
|
1828
|
+
},
|
|
1829
|
+
|
|
1830
|
+
get: async (id) => {
|
|
1831
|
+
const providerName = this.routeByThingId(id);
|
|
1832
|
+
const provider = this.getProvider(providerName);
|
|
1833
|
+
return provider.people.get(id);
|
|
1834
|
+
},
|
|
1835
|
+
};
|
|
1836
|
+
|
|
1837
|
+
// Health check all providers
|
|
1838
|
+
async healthCheck(): Promise<Record<string, boolean>> {
|
|
1839
|
+
const health: Record<string, boolean> = {};
|
|
1840
|
+
|
|
1841
|
+
for (const [name, provider] of this.providers.entries()) {
|
|
1842
|
+
try {
|
|
1843
|
+
// Try a simple query to test connectivity
|
|
1844
|
+
await provider.things.list({ type: "course", limit: 1 });
|
|
1845
|
+
health[name] = true;
|
|
1846
|
+
} catch (e) {
|
|
1847
|
+
health[name] = false;
|
|
1848
|
+
}
|
|
1849
|
+
}
|
|
1850
|
+
|
|
1851
|
+
return health;
|
|
1852
|
+
}
|
|
1853
|
+
|
|
1854
|
+
// Route by thing type
|
|
1855
|
+
private routeByThingType(type: string): string {
|
|
1856
|
+
const rule = this.routingRules.thingTypes[type];
|
|
1857
|
+
if (rule) return rule;
|
|
1858
|
+
|
|
1859
|
+
// Default routing
|
|
1860
|
+
return this.routingRules.defaultProvider || "convex";
|
|
1861
|
+
}
|
|
1862
|
+
|
|
1863
|
+
// Route by thing ID (ID prefix indicates provider)
|
|
1864
|
+
private routeByThingId(id: string): string {
|
|
1865
|
+
if (id.startsWith("wp_")) return "wordpress";
|
|
1866
|
+
if (id.startsWith("notion_")) return "notion";
|
|
1867
|
+
if (id.startsWith("convex_")) return "convex";
|
|
1868
|
+
|
|
1869
|
+
// Default routing
|
|
1870
|
+
return this.routingRules.defaultProvider || "convex";
|
|
1871
|
+
}
|
|
1872
|
+
|
|
1873
|
+
// Route by connection ID
|
|
1874
|
+
private routeByConnectionId(id: string): string {
|
|
1875
|
+
if (id.startsWith("wp_")) return "wordpress";
|
|
1876
|
+
if (id.startsWith("notion_")) return "notion";
|
|
1877
|
+
if (id.startsWith("convex_")) return "convex";
|
|
1878
|
+
|
|
1879
|
+
return this.routingRules.defaultProvider || "convex";
|
|
1880
|
+
}
|
|
1881
|
+
|
|
1882
|
+
// Get provider by name
|
|
1883
|
+
private getProvider(name: string): DataProvider {
|
|
1884
|
+
const provider = this.providers.get(name);
|
|
1885
|
+
if (!provider) {
|
|
1886
|
+
throw new Error(`Provider not found: ${name}`);
|
|
1887
|
+
}
|
|
1888
|
+
return provider;
|
|
1889
|
+
}
|
|
1890
|
+
|
|
1891
|
+
// Create provider from config
|
|
1892
|
+
private createProvider(config: any): DataProvider {
|
|
1893
|
+
switch (config.type) {
|
|
1894
|
+
case "convex":
|
|
1895
|
+
return new ConvexProvider(config);
|
|
1896
|
+
case "wordpress":
|
|
1897
|
+
return new WordPressProvider(config);
|
|
1898
|
+
case "notion":
|
|
1899
|
+
return new NotionProvider(config);
|
|
1900
|
+
default:
|
|
1901
|
+
throw new Error(`Unknown provider type: ${config.type}`);
|
|
1902
|
+
}
|
|
1903
|
+
}
|
|
1904
|
+
}
|
|
1905
|
+
|
|
1906
|
+
interface CompositeProviderConfig {
|
|
1907
|
+
providers: Record<string, any>; // providerName -> providerConfig
|
|
1908
|
+
routingRules: RoutingRules;
|
|
1909
|
+
}
|
|
1910
|
+
|
|
1911
|
+
interface RoutingRules {
|
|
1912
|
+
// Default provider for unmapped types
|
|
1913
|
+
defaultProvider: string;
|
|
1914
|
+
|
|
1915
|
+
// Thing type routing: thingType -> providerName
|
|
1916
|
+
thingTypes: Record<string, string>;
|
|
1917
|
+
|
|
1918
|
+
// Specific dimension providers
|
|
1919
|
+
eventsProvider?: string;
|
|
1920
|
+
knowledgeProvider?: string;
|
|
1921
|
+
organizationsProvider?: string;
|
|
1922
|
+
peopleProvider?: string;
|
|
1923
|
+
}
|
|
1924
|
+
|
|
1925
|
+
// Example usage:
|
|
1926
|
+
const compositeProvider = new CompositeProvider({
|
|
1927
|
+
providers: {
|
|
1928
|
+
convex: {
|
|
1929
|
+
type: "convex",
|
|
1930
|
+
url: "https://shocking-falcon-870.convex.cloud",
|
|
1931
|
+
},
|
|
1932
|
+
wordpress: {
|
|
1933
|
+
type: "wordpress",
|
|
1934
|
+
url: "https://acme.wordpress.com",
|
|
1935
|
+
apiKey: "wp_secret_key",
|
|
1936
|
+
organizationId: "org_123",
|
|
1937
|
+
},
|
|
1938
|
+
notion: {
|
|
1939
|
+
type: "notion",
|
|
1940
|
+
apiKey: "notion_secret_key",
|
|
1941
|
+
organizationId: "org_123",
|
|
1942
|
+
databaseIds: {
|
|
1943
|
+
documentation: "abc123",
|
|
1944
|
+
meeting_notes: "def456",
|
|
1945
|
+
},
|
|
1946
|
+
convexUrl: "https://shocking-falcon-870.convex.cloud",
|
|
1947
|
+
},
|
|
1948
|
+
},
|
|
1949
|
+
routingRules: {
|
|
1950
|
+
defaultProvider: "convex",
|
|
1951
|
+
thingTypes: {
|
|
1952
|
+
course: "wordpress",
|
|
1953
|
+
lesson: "wordpress",
|
|
1954
|
+
documentation: "notion",
|
|
1955
|
+
meeting_notes: "notion",
|
|
1956
|
+
},
|
|
1957
|
+
eventsProvider: "convex",
|
|
1958
|
+
knowledgeProvider: "convex",
|
|
1959
|
+
organizationsProvider: "convex",
|
|
1960
|
+
peopleProvider: "convex",
|
|
1961
|
+
},
|
|
1962
|
+
});
|
|
1963
|
+
|
|
1964
|
+
// Usage in components:
|
|
1965
|
+
const courses = await compositeProvider.things.list({ type: "course" });
|
|
1966
|
+
// Routes to WordPress
|
|
1967
|
+
|
|
1968
|
+
const docs = await compositeProvider.things.list({ type: "documentation" });
|
|
1969
|
+
// Routes to Notion
|
|
1970
|
+
```
|
|
1971
|
+
|
|
1972
|
+
---
|
|
1973
|
+
|
|
1974
|
+
## Implementation Steps (50+ Steps)
|
|
1975
|
+
|
|
1976
|
+
### Phase 1: WordPress Provider (Week 1)
|
|
1977
|
+
|
|
1978
|
+
**Step 1-10: Setup & Authentication**
|
|
1979
|
+
|
|
1980
|
+
1. Create `frontend/src/providers/wordpress/` directory
|
|
1981
|
+
2. Install dependencies: `bun add @types/wordpress__api-fetch`
|
|
1982
|
+
3. Create `WordPressAPI.ts` - HTTP client with auth
|
|
1983
|
+
4. Implement Application Password authentication
|
|
1984
|
+
5. Test authentication with real WordPress site
|
|
1985
|
+
6. Create `WordPressProvider.ts` - DataProvider implementation
|
|
1986
|
+
7. Implement ID conversion utilities (`wpIdToOneId`, `oneIdToWpId`)
|
|
1987
|
+
8. Create type mapping utilities (ontology ↔ WP)
|
|
1988
|
+
9. Create status mapping utilities (ontology ↔ WP)
|
|
1989
|
+
10. Write unit tests for utilities (30+ tests)
|
|
1990
|
+
|
|
1991
|
+
**Step 11-20: Things Implementation**
|
|
1992
|
+
|
|
1993
|
+
11. Implement `things.list()` with WordPress REST API
|
|
1994
|
+
12. Implement `things.create()` with post creation
|
|
1995
|
+
13. Implement `things.update()` with post updates
|
|
1996
|
+
14. Implement `things.delete()` with post deletion
|
|
1997
|
+
15. Implement `things.get()` with single post retrieval
|
|
1998
|
+
16. Implement ACF field transformation logic
|
|
1999
|
+
17. Add pagination support (WordPress pagination differs from Convex)
|
|
2000
|
+
18. Add filtering support (map to WordPress query params)
|
|
2001
|
+
19. Add error handling with exponential backoff retry
|
|
2002
|
+
20. Write unit tests for things operations (50+ tests)
|
|
2003
|
+
|
|
2004
|
+
**Step 21-30: Connections Implementation**
|
|
2005
|
+
|
|
2006
|
+
21. Design `wp_one_connections` table schema (SQL)
|
|
2007
|
+
22. Create WordPress plugin: `one-platform-connector`
|
|
2008
|
+
23. Implement `/one/v1/connections` REST endpoint (GET)
|
|
2009
|
+
24. Implement `/one/v1/connections` REST endpoint (POST)
|
|
2010
|
+
25. Implement `/one/v1/connections/:id` REST endpoint (GET)
|
|
2011
|
+
26. Implement `/one/v1/connections/:id` REST endpoint (DELETE)
|
|
2012
|
+
27. Implement `connections.list()` in TypeScript provider
|
|
2013
|
+
28. Implement `connections.create()` in TypeScript provider
|
|
2014
|
+
29. Implement `connections.delete()` in TypeScript provider
|
|
2015
|
+
30. Write integration tests with real WordPress instance (20+ tests)
|
|
2016
|
+
|
|
2017
|
+
**Step 31-40: Events & Knowledge Implementation**
|
|
2018
|
+
|
|
2019
|
+
31. Design `wp_one_events` table schema (SQL)
|
|
2020
|
+
32. Implement `/one/v1/events` REST endpoint (GET)
|
|
2021
|
+
33. Implement `/one/v1/events` REST endpoint (POST)
|
|
2022
|
+
34. Implement `events.list()` in TypeScript provider
|
|
2023
|
+
35. Implement `events.create()` in TypeScript provider
|
|
2024
|
+
36. Design `wp_one_knowledge` table schema (SQL)
|
|
2025
|
+
37. Implement `/one/v1/knowledge` REST endpoint (GET/POST)
|
|
2026
|
+
38. Implement `knowledge.list()` in TypeScript provider
|
|
2027
|
+
39. Implement `knowledge.create()` in TypeScript provider
|
|
2028
|
+
40. Document hybrid approach for vector search (Convex fallback)
|
|
2029
|
+
|
|
2030
|
+
### Phase 2: Notion Provider (Week 1-2)
|
|
2031
|
+
|
|
2032
|
+
**Step 41-50: Setup & Authentication**
|
|
2033
|
+
|
|
2034
|
+
41. Create `frontend/src/providers/notion/` directory
|
|
2035
|
+
42. Install dependencies: `bun add @notionhq/client`
|
|
2036
|
+
43. Create `NotionProvider.ts` - DataProvider implementation
|
|
2037
|
+
44. Set up Notion integration with test workspace
|
|
2038
|
+
45. Create test databases (Courses, Lessons, Users)
|
|
2039
|
+
46. Configure database properties (Name, Status, etc.)
|
|
2040
|
+
47. Implement ID conversion utilities (`notionIdToOneId`, `oneIdToNotionId`)
|
|
2041
|
+
48. Create property mapping utilities (ontology ↔ Notion)
|
|
2042
|
+
49. Create status mapping utilities (ontology ↔ Notion)
|
|
2043
|
+
50. Write unit tests for utilities (30+ tests)
|
|
2044
|
+
|
|
2045
|
+
**Step 51-60: Things Implementation**
|
|
2046
|
+
|
|
2047
|
+
51. Implement `things.list()` with Notion database query
|
|
2048
|
+
52. Implement `things.create()` with Notion page creation
|
|
2049
|
+
53. Implement `things.update()` with Notion page updates
|
|
2050
|
+
54. Implement `things.delete()` with Notion page archiving
|
|
2051
|
+
55. Implement `things.get()` with single page retrieval
|
|
2052
|
+
56. Implement property extraction logic (all property types)
|
|
2053
|
+
57. Add pagination support (Notion cursor-based pagination)
|
|
2054
|
+
58. Add filtering support (map to Notion filter syntax)
|
|
2055
|
+
59. Add error handling with rate limit retry
|
|
2056
|
+
60. Write unit tests for things operations (50+ tests)
|
|
2057
|
+
|
|
2058
|
+
**Step 61-70: Connections Implementation**
|
|
2059
|
+
|
|
2060
|
+
61. Design Notion relation property strategy
|
|
2061
|
+
62. Implement `connections.list()` with relation extraction
|
|
2062
|
+
63. Implement `connections.create()` with relation updates
|
|
2063
|
+
64. Implement `connections.delete()` with relation removal
|
|
2064
|
+
65. Implement `connections.get()` with relation lookup
|
|
2065
|
+
66. Map all 25 ONE relationship types to Notion properties
|
|
2066
|
+
67. Handle bidirectional relations
|
|
2067
|
+
68. Add validation for cross-database relations
|
|
2068
|
+
69. Add error handling for missing properties
|
|
2069
|
+
70. Write integration tests with real Notion workspace (20+ tests)
|
|
2070
|
+
|
|
2071
|
+
**Step 71-75: Events & Knowledge (Hybrid)**
|
|
2072
|
+
|
|
2073
|
+
71. Document hybrid approach (Convex for events/knowledge)
|
|
2074
|
+
72. Implement `events.list()` with Convex fallback
|
|
2075
|
+
73. Implement `events.create()` with Convex storage
|
|
2076
|
+
74. Implement `knowledge.list()` with Convex fallback
|
|
2077
|
+
75. Implement `knowledge.search()` with Convex vector search
|
|
2078
|
+
|
|
2079
|
+
### Phase 3: CompositeProvider (Week 2)
|
|
2080
|
+
|
|
2081
|
+
**Step 76-85: Routing Implementation**
|
|
2082
|
+
|
|
2083
|
+
76. Create `frontend/src/providers/composite/` directory
|
|
2084
|
+
77. Create `CompositeProvider.ts` - Multi-backend router
|
|
2085
|
+
78. Implement provider registry with Map
|
|
2086
|
+
79. Implement `routeByThingType()` routing logic
|
|
2087
|
+
80. Implement `routeByThingId()` routing logic (ID prefix detection)
|
|
2088
|
+
81. Implement `routeByConnectionId()` routing logic
|
|
2089
|
+
82. Create `RoutingRules` interface with configuration schema
|
|
2090
|
+
83. Implement provider factory (create providers from config)
|
|
2091
|
+
84. Add provider health check method
|
|
2092
|
+
85. Write unit tests for routing logic (40+ tests)
|
|
2093
|
+
|
|
2094
|
+
**Step 86-90: Operations Implementation**
|
|
2095
|
+
|
|
2096
|
+
86. Implement `things.*` operations with routing
|
|
2097
|
+
87. Implement `connections.*` operations with routing
|
|
2098
|
+
88. Implement `events.*` operations (always route to designated provider)
|
|
2099
|
+
89. Implement `knowledge.*` operations (always route to designated provider)
|
|
2100
|
+
90. Handle cross-provider connection scenarios
|
|
2101
|
+
|
|
2102
|
+
### Phase 4: Testing & Documentation (Week 2)
|
|
2103
|
+
|
|
2104
|
+
**Step 91-100: Integration Testing**
|
|
2105
|
+
|
|
2106
|
+
91. Set up test WordPress instance (local Docker)
|
|
2107
|
+
92. Set up test Notion workspace
|
|
2108
|
+
93. Write end-to-end test: Create course in WordPress
|
|
2109
|
+
94. Write end-to-end test: Create lesson in WordPress
|
|
2110
|
+
95. Write end-to-end test: Create documentation in Notion
|
|
2111
|
+
96. Write end-to-end test: Create cross-provider connection
|
|
2112
|
+
97. Write end-to-end test: CompositeProvider routing
|
|
2113
|
+
98. Write end-to-end test: Multi-backend organization
|
|
2114
|
+
99. Run full test suite (expect 200+ tests)
|
|
2115
|
+
100. Fix any failing tests
|
|
2116
|
+
|
|
2117
|
+
**Step 101-110: Documentation & Guides**
|
|
2118
|
+
|
|
2119
|
+
101. Write WordPress setup guide (installation, plugins, configuration)
|
|
2120
|
+
102. Write Notion setup guide (integration, databases, properties)
|
|
2121
|
+
103. Write provider configuration reference
|
|
2122
|
+
104. Write ontology mapping documentation (all 6 dimensions)
|
|
2123
|
+
105. Write CompositeProvider configuration guide
|
|
2124
|
+
106. Write troubleshooting guide (common issues)
|
|
2125
|
+
107. Create example configurations (3-5 scenarios)
|
|
2126
|
+
108. Document limitations (what's not supported)
|
|
2127
|
+
109. Create migration guide (Convex → WordPress/Notion)
|
|
2128
|
+
110. Review and publish documentation
|
|
2129
|
+
|
|
2130
|
+
---
|
|
2131
|
+
|
|
2132
|
+
## Testing Strategy
|
|
2133
|
+
|
|
2134
|
+
### Unit Tests (100+ tests)
|
|
2135
|
+
|
|
2136
|
+
**WordPress Provider:**
|
|
2137
|
+
- ID conversion (10 tests)
|
|
2138
|
+
- Type mapping (20 tests)
|
|
2139
|
+
- Status mapping (10 tests)
|
|
2140
|
+
- Property transformation (20 tests)
|
|
2141
|
+
- API client (20 tests)
|
|
2142
|
+
- Error handling (10 tests)
|
|
2143
|
+
|
|
2144
|
+
**Notion Provider:**
|
|
2145
|
+
- ID conversion (10 tests)
|
|
2146
|
+
- Property extraction (30 tests)
|
|
2147
|
+
- Status mapping (10 tests)
|
|
2148
|
+
- Property transformation (20 tests)
|
|
2149
|
+
- Error handling (10 tests)
|
|
2150
|
+
|
|
2151
|
+
**CompositeProvider:**
|
|
2152
|
+
- Routing logic (30 tests)
|
|
2153
|
+
- Provider factory (10 tests)
|
|
2154
|
+
- Health checks (10 tests)
|
|
2155
|
+
|
|
2156
|
+
### Integration Tests (50+ tests)
|
|
2157
|
+
|
|
2158
|
+
**WordPress Provider:**
|
|
2159
|
+
- CRUD operations for all thing types (20 tests)
|
|
2160
|
+
- Connection operations (10 tests)
|
|
2161
|
+
- Event operations (5 tests)
|
|
2162
|
+
- Authentication (5 tests)
|
|
2163
|
+
|
|
2164
|
+
**Notion Provider:**
|
|
2165
|
+
- CRUD operations for all thing types (20 tests)
|
|
2166
|
+
- Connection operations (10 tests)
|
|
2167
|
+
- Property type handling (10 tests)
|
|
2168
|
+
- Authentication (5 tests)
|
|
2169
|
+
|
|
2170
|
+
**CompositeProvider:**
|
|
2171
|
+
- Multi-backend routing (10 tests)
|
|
2172
|
+
- Cross-provider connections (5 tests)
|
|
2173
|
+
|
|
2174
|
+
### End-to-End Tests (20+ tests)
|
|
2175
|
+
|
|
2176
|
+
**Scenarios:**
|
|
2177
|
+
1. Create organization with WordPress backend
|
|
2178
|
+
2. Create organization with Notion backend
|
|
2179
|
+
3. Create organization with CompositeProvider
|
|
2180
|
+
4. Migrate organization from Convex to WordPress
|
|
2181
|
+
5. Create course in WordPress, view in ONE frontend
|
|
2182
|
+
6. Create documentation in Notion, search in ONE
|
|
2183
|
+
7. Create cross-provider connection (WordPress course → Notion doc)
|
|
2184
|
+
8. Test real-time updates (polling for WordPress, webhooks for Notion)
|
|
2185
|
+
9. Test provider failover (CompositeProvider health checks)
|
|
2186
|
+
10. Test data consistency across providers
|
|
2187
|
+
|
|
2188
|
+
### Performance Tests
|
|
2189
|
+
|
|
2190
|
+
**Benchmarks:**
|
|
2191
|
+
- Compare WordPress provider vs Convex (latency, throughput)
|
|
2192
|
+
- Compare Notion provider vs Convex (latency, throughput)
|
|
2193
|
+
- Test CompositeProvider overhead (routing delay)
|
|
2194
|
+
- Test concurrent operations (10, 100, 1000 requests)
|
|
2195
|
+
- Test pagination performance (large datasets)
|
|
2196
|
+
- Test connection queries (complex relationship graphs)
|
|
2197
|
+
|
|
2198
|
+
**Acceptance Criteria:**
|
|
2199
|
+
- WordPress provider: < 500ms p99 latency for simple queries
|
|
2200
|
+
- Notion provider: < 1s p99 latency for simple queries
|
|
2201
|
+
- CompositeProvider: < 50ms routing overhead
|
|
2202
|
+
- All providers: No data loss under normal operation
|
|
2203
|
+
- All providers: Graceful degradation under high load
|
|
2204
|
+
|
|
2205
|
+
---
|
|
2206
|
+
|
|
2207
|
+
## User Stories & Acceptance Criteria
|
|
2208
|
+
|
|
2209
|
+
### Story 1: WordPress as Backend
|
|
2210
|
+
|
|
2211
|
+
**As an** org_owner with existing WordPress infrastructure
|
|
2212
|
+
**I want to** use WordPress as my ONE Platform backend
|
|
2213
|
+
**So that** I can leverage my existing content and avoid data migration
|
|
2214
|
+
|
|
2215
|
+
**Acceptance Criteria:**
|
|
2216
|
+
- [ ] I can configure my organization to use WordPress provider
|
|
2217
|
+
- [ ] I provide my WordPress URL and API key
|
|
2218
|
+
- [ ] All course/lesson content is stored in WordPress
|
|
2219
|
+
- [ ] ONE frontend displays WordPress content seamlessly
|
|
2220
|
+
- [ ] I can create/edit content in either WordPress admin or ONE interface
|
|
2221
|
+
- [ ] Changes in WordPress appear in ONE within 30 seconds (polling)
|
|
2222
|
+
- [ ] Changes in ONE appear in WordPress immediately
|
|
2223
|
+
- [ ] All user roles map correctly (org_owner → Administrator, etc.)
|
|
2224
|
+
|
|
2225
|
+
### Story 2: Notion as Backend
|
|
2226
|
+
|
|
2227
|
+
**As an** org_owner using Notion for knowledge management
|
|
2228
|
+
**I want to** use Notion as my ONE Platform backend
|
|
2229
|
+
**So that** my team can continue using familiar Notion workflows
|
|
2230
|
+
|
|
2231
|
+
**Acceptance Criteria:**
|
|
2232
|
+
- [ ] I can configure my organization to use Notion provider
|
|
2233
|
+
- [ ] I provide my Notion integration token and database IDs
|
|
2234
|
+
- [ ] All documentation/notes are stored in Notion
|
|
2235
|
+
- [ ] ONE frontend displays Notion pages seamlessly
|
|
2236
|
+
- [ ] I can create/edit content in either Notion or ONE interface
|
|
2237
|
+
- [ ] Changes in Notion appear in ONE within 10 seconds (webhooks)
|
|
2238
|
+
- [ ] Changes in ONE appear in Notion immediately
|
|
2239
|
+
- [ ] Notion relation properties map to ONE connections
|
|
2240
|
+
|
|
2241
|
+
### Story 3: Multi-Backend Organization
|
|
2242
|
+
|
|
2243
|
+
**As an** org_owner with diverse needs
|
|
2244
|
+
**I want to** use different backends for different content types
|
|
2245
|
+
**So that** I can leverage the best tool for each use case
|
|
2246
|
+
|
|
2247
|
+
**Acceptance Criteria:**
|
|
2248
|
+
- [ ] I can configure CompositeProvider with routing rules
|
|
2249
|
+
- [ ] I specify: courses→WordPress, docs→Notion, events→Convex
|
|
2250
|
+
- [ ] Content is routed to correct backend automatically
|
|
2251
|
+
- [ ] I can query across backends seamlessly
|
|
2252
|
+
- [ ] ONE frontend shows unified view of all content
|
|
2253
|
+
- [ ] Cross-provider connections work correctly
|
|
2254
|
+
- [ ] Health checks alert me if any provider is down
|
|
2255
|
+
|
|
2256
|
+
### Story 4: Backend Migration
|
|
2257
|
+
|
|
2258
|
+
**As an** org_owner currently using Convex
|
|
2259
|
+
**I want to** migrate to WordPress
|
|
2260
|
+
**So that** I can use custom WordPress plugins
|
|
2261
|
+
|
|
2262
|
+
**Acceptance Criteria:**
|
|
2263
|
+
- [ ] I can export all data from Convex
|
|
2264
|
+
- [ ] I can import all data to WordPress
|
|
2265
|
+
- [ ] I can switch provider configuration
|
|
2266
|
+
- [ ] All existing ONE IDs are preserved (remapped)
|
|
2267
|
+
- [ ] All connections are preserved
|
|
2268
|
+
- [ ] All events are migrated
|
|
2269
|
+
- [ ] Migration completes without data loss
|
|
2270
|
+
- [ ] ONE frontend works immediately after migration
|
|
2271
|
+
|
|
2272
|
+
### Story 5: Provider Creation
|
|
2273
|
+
|
|
2274
|
+
**As a** developer
|
|
2275
|
+
**I want to** create a custom provider for my proprietary system
|
|
2276
|
+
**So that** my organization can use ONE with our existing infrastructure
|
|
2277
|
+
|
|
2278
|
+
**Acceptance Criteria:**
|
|
2279
|
+
- [ ] I have comprehensive provider creation guide
|
|
2280
|
+
- [ ] I have working examples (WordPress, Notion)
|
|
2281
|
+
- [ ] I understand all 6-dimension requirements
|
|
2282
|
+
- [ ] I can implement DataProvider interface
|
|
2283
|
+
- [ ] I can test my provider with integration test suite
|
|
2284
|
+
- [ ] I can deploy my provider to production
|
|
2285
|
+
- [ ] My provider passes all quality gates
|
|
2286
|
+
|
|
2287
|
+
---
|
|
2288
|
+
|
|
2289
|
+
## Quality Gates
|
|
2290
|
+
|
|
2291
|
+
- [ ] WordPressProvider implements all 6 dimensions
|
|
2292
|
+
- [ ] NotionProvider implements all 6 dimensions
|
|
2293
|
+
- [ ] CompositeProvider routes correctly
|
|
2294
|
+
- [ ] All unit tests pass (100+)
|
|
2295
|
+
- [ ] All integration tests pass (50+)
|
|
2296
|
+
- [ ] All end-to-end tests pass (20+)
|
|
2297
|
+
- [ ] Performance benchmarks acceptable
|
|
2298
|
+
- [ ] WordPress setup guide complete
|
|
2299
|
+
- [ ] Notion setup guide complete
|
|
2300
|
+
- [ ] Provider creation guide complete
|
|
2301
|
+
- [ ] API reference documentation complete
|
|
2302
|
+
- [ ] Code review approved by Engineering Director
|
|
2303
|
+
- [ ] Security review approved (API key handling)
|
|
2304
|
+
- [ ] No data loss in stress testing
|
|
2305
|
+
- [ ] Graceful error handling verified
|
|
2306
|
+
|
|
2307
|
+
---
|
|
2308
|
+
|
|
2309
|
+
## Rollback Plan
|
|
2310
|
+
|
|
2311
|
+
**Risk:** Alternative providers introduce bugs or performance issues.
|
|
2312
|
+
|
|
2313
|
+
**Mitigation:**
|
|
2314
|
+
- Providers are additive - ConvexProvider remains default
|
|
2315
|
+
- Organizations opt-in to alternative providers
|
|
2316
|
+
- Provider configuration stored per-organization
|
|
2317
|
+
- Easy to switch back to ConvexProvider
|
|
2318
|
+
|
|
2319
|
+
**Rollback Steps:**
|
|
2320
|
+
1. Update organization's `properties.providerType` to `"convex"`
|
|
2321
|
+
2. Provider registry automatically uses ConvexProvider
|
|
2322
|
+
3. No code changes required
|
|
2323
|
+
4. **Rollback time: Instant** (configuration change only)
|
|
2324
|
+
|
|
2325
|
+
**Data Preservation:**
|
|
2326
|
+
- External data (WordPress, Notion) remains intact
|
|
2327
|
+
- Events/knowledge in Convex remain intact
|
|
2328
|
+
- Can re-enable alternative provider at any time
|
|
2329
|
+
|
|
2330
|
+
---
|
|
2331
|
+
|
|
2332
|
+
## Dependencies
|
|
2333
|
+
|
|
2334
|
+
**Required:**
|
|
2335
|
+
- Feature 2-6 (Dashboard Migration) MUST be complete
|
|
2336
|
+
- All frontend components MUST use DataProvider abstraction
|
|
2337
|
+
- No direct Convex imports in components
|
|
2338
|
+
|
|
2339
|
+
**External:**
|
|
2340
|
+
- WordPress instance (6.4+)
|
|
2341
|
+
- WordPress plugins: ACF Pro, Custom Post Type UI
|
|
2342
|
+
- Notion workspace with API access
|
|
2343
|
+
- Notion integration token with full access permissions
|
|
2344
|
+
|
|
2345
|
+
**Development:**
|
|
2346
|
+
- Docker for local WordPress testing
|
|
2347
|
+
- Notion test workspace
|
|
2348
|
+
- Integration test credentials (stored in 1Password)
|
|
2349
|
+
|
|
2350
|
+
---
|
|
2351
|
+
|
|
2352
|
+
## Related Files
|
|
2353
|
+
|
|
2354
|
+
**Plan:**
|
|
2355
|
+
- `one/things/plans/2-backend-agnostic-frontend.md`
|
|
2356
|
+
|
|
2357
|
+
**Dependencies:**
|
|
2358
|
+
- `one/things/features/2-6-dashboard-migration.md`
|
|
2359
|
+
|
|
2360
|
+
**Implementation:**
|
|
2361
|
+
- `frontend/src/providers/DataProvider.ts` (interface)
|
|
2362
|
+
- `frontend/src/providers/wordpress/WordPressProvider.ts` (NEW)
|
|
2363
|
+
- `frontend/src/providers/wordpress/WordPressAPI.ts` (NEW)
|
|
2364
|
+
- `frontend/src/providers/notion/NotionProvider.ts` (NEW)
|
|
2365
|
+
- `frontend/src/providers/composite/CompositeProvider.ts` (NEW)
|
|
2366
|
+
|
|
2367
|
+
**WordPress Plugin:**
|
|
2368
|
+
- `wordpress/plugins/one-platform-connector/` (NEW - to be created)
|
|
2369
|
+
- `wordpress/plugins/one-platform-connector/includes/api/connections.php` (NEW)
|
|
2370
|
+
- `wordpress/plugins/one-platform-connector/includes/api/events.php` (NEW)
|
|
2371
|
+
- `wordpress/plugins/one-platform-connector/includes/api/knowledge.php` (NEW)
|
|
2372
|
+
- `wordpress/plugins/one-platform-connector/includes/schema.php` (NEW - SQL table creation)
|
|
2373
|
+
|
|
2374
|
+
**Tests:**
|
|
2375
|
+
- `frontend/tests/unit/providers/wordpress.test.ts` (NEW)
|
|
2376
|
+
- `frontend/tests/unit/providers/notion.test.ts` (NEW)
|
|
2377
|
+
- `frontend/tests/unit/providers/composite.test.ts` (NEW)
|
|
2378
|
+
- `frontend/tests/integration/providers/wordpress.test.ts` (NEW)
|
|
2379
|
+
- `frontend/tests/integration/providers/notion.test.ts` (NEW)
|
|
2380
|
+
- `frontend/tests/e2e/multi-backend.test.ts` (NEW)
|
|
2381
|
+
|
|
2382
|
+
**Documentation:**
|
|
2383
|
+
- `one/knowledge/providers/wordpress-setup.md` (NEW)
|
|
2384
|
+
- `one/knowledge/providers/notion-setup.md` (NEW)
|
|
2385
|
+
- `one/knowledge/providers/provider-creation-guide.md` (NEW)
|
|
2386
|
+
- `one/knowledge/providers/ontology-mapping.md` (NEW)
|
|
2387
|
+
|
|
2388
|
+
---
|
|
2389
|
+
|
|
2390
|
+
## Success Metrics
|
|
2391
|
+
|
|
2392
|
+
**Technical:**
|
|
2393
|
+
- [ ] 200+ tests passing
|
|
2394
|
+
- [ ] WordPress provider: < 500ms p99 latency
|
|
2395
|
+
- [ ] Notion provider: < 1s p99 latency
|
|
2396
|
+
- [ ] CompositeProvider: < 50ms routing overhead
|
|
2397
|
+
- [ ] Zero data loss in stress testing
|
|
2398
|
+
- [ ] All 6 dimensions fully implemented
|
|
2399
|
+
|
|
2400
|
+
**User Experience:**
|
|
2401
|
+
- [ ] Organizations can switch providers in < 5 minutes
|
|
2402
|
+
- [ ] No frontend code changes required
|
|
2403
|
+
- [ ] Seamless experience regardless of backend
|
|
2404
|
+
- [ ] Real-time updates working (polling/webhooks)
|
|
2405
|
+
|
|
2406
|
+
**Documentation:**
|
|
2407
|
+
- [ ] 3 complete setup guides published
|
|
2408
|
+
- [ ] Provider creation guide with examples
|
|
2409
|
+
- [ ] API reference complete
|
|
2410
|
+
- [ ] Troubleshooting guide covers 10+ common issues
|
|
2411
|
+
|
|
2412
|
+
**Strategic:**
|
|
2413
|
+
- [ ] Proves backend-agnostic architecture works
|
|
2414
|
+
- [ ] Demonstrates ONE's flexibility
|
|
2415
|
+
- [ ] Enables organizations with existing infrastructure to adopt ONE
|
|
2416
|
+
- [ ] Provides template for future provider implementations
|
|
2417
|
+
|
|
2418
|
+
---
|
|
2419
|
+
|
|
2420
|
+
## Next Steps
|
|
2421
|
+
|
|
2422
|
+
1. ✅ **Integration Specialist:** Create detailed specification (THIS FILE)
|
|
2423
|
+
2. **Integration Specialist:** Review with Engineering Director
|
|
2424
|
+
3. **Integration Specialist:** Set up WordPress test instance
|
|
2425
|
+
4. **Integration Specialist:** Set up Notion test workspace
|
|
2426
|
+
5. **Integration Specialist:** Implement WordPressProvider (Steps 1-40)
|
|
2427
|
+
6. **Integration Specialist:** Implement NotionProvider (Steps 41-75)
|
|
2428
|
+
7. **Integration Specialist:** Implement CompositeProvider (Steps 76-90)
|
|
2429
|
+
8. **Quality Agent:** Run full test suite
|
|
2430
|
+
9. **Integration Specialist:** Fix any failing tests
|
|
2431
|
+
10. **Documenter:** Write setup guides
|
|
2432
|
+
11. **Engineering Director:** Final review and approval
|
|
2433
|
+
12. **Integration Specialist:** Deploy to staging
|
|
2434
|
+
13. **Quality Agent:** Validate in staging
|
|
2435
|
+
14. **Engineering Director:** Deploy to production
|
|
2436
|
+
|
|
2437
|
+
---
|
|
2438
|
+
|
|
2439
|
+
**Status:** Detailed Specification Complete ✅
|
|
2440
|
+
**Created:** 2025-10-13
|
|
2441
|
+
**Specification Completed:** 2025-10-13
|
|
2442
|
+
**Validated By:** Integration Specialist (Self)
|
|
2443
|
+
**Awaiting Review By:** Engineering Director
|
|
2444
|
+
**Strategic Value:** Proves backend-agnostic architecture - enables organizations to use existing infrastructure (WordPress, Notion) while gaining ONE's AI capabilities
|
|
2445
|
+
**Lines:** 1,486
|
|
2446
|
+
|
|
2447
|
+
---
|
|
2448
|
+
|
|
2449
|
+
## Appendix: WordPress Database Schema
|
|
2450
|
+
|
|
2451
|
+
```sql
|
|
2452
|
+
-- wp_one_connections table
|
|
2453
|
+
CREATE TABLE `wp_one_connections` (
|
|
2454
|
+
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
|
|
2455
|
+
`from_thing_id` varchar(255) NOT NULL,
|
|
2456
|
+
`to_thing_id` varchar(255) NOT NULL,
|
|
2457
|
+
`relationship_type` varchar(100) NOT NULL,
|
|
2458
|
+
`metadata` text,
|
|
2459
|
+
`valid_from` bigint(20) unsigned,
|
|
2460
|
+
`valid_to` bigint(20) unsigned,
|
|
2461
|
+
`created_at` datetime NOT NULL,
|
|
2462
|
+
PRIMARY KEY (`id`),
|
|
2463
|
+
KEY `from_thing_id` (`from_thing_id`),
|
|
2464
|
+
KEY `to_thing_id` (`to_thing_id`),
|
|
2465
|
+
KEY `relationship_type` (`relationship_type`)
|
|
2466
|
+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
|
2467
|
+
|
|
2468
|
+
-- wp_one_events table
|
|
2469
|
+
CREATE TABLE `wp_one_events` (
|
|
2470
|
+
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
|
|
2471
|
+
`type` varchar(100) NOT NULL,
|
|
2472
|
+
`actor_id` varchar(255),
|
|
2473
|
+
`target_id` varchar(255),
|
|
2474
|
+
`timestamp` datetime NOT NULL,
|
|
2475
|
+
`metadata` text,
|
|
2476
|
+
PRIMARY KEY (`id`),
|
|
2477
|
+
KEY `type` (`type`),
|
|
2478
|
+
KEY `actor_id` (`actor_id`),
|
|
2479
|
+
KEY `target_id` (`target_id`),
|
|
2480
|
+
KEY `timestamp` (`timestamp`)
|
|
2481
|
+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
|
2482
|
+
|
|
2483
|
+
-- wp_one_knowledge table
|
|
2484
|
+
CREATE TABLE `wp_one_knowledge` (
|
|
2485
|
+
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
|
|
2486
|
+
`knowledge_type` varchar(50) NOT NULL,
|
|
2487
|
+
`text` longtext NOT NULL,
|
|
2488
|
+
`embedding` longtext,
|
|
2489
|
+
`embedding_model` varchar(100),
|
|
2490
|
+
`embedding_dim` int(11),
|
|
2491
|
+
`source_thing_id` varchar(255),
|
|
2492
|
+
`source_field` varchar(100),
|
|
2493
|
+
`labels` text,
|
|
2494
|
+
`metadata` text,
|
|
2495
|
+
`created_at` datetime NOT NULL,
|
|
2496
|
+
`updated_at` datetime NOT NULL,
|
|
2497
|
+
PRIMARY KEY (`id`),
|
|
2498
|
+
KEY `knowledge_type` (`knowledge_type`),
|
|
2499
|
+
KEY `source_thing_id` (`source_thing_id`)
|
|
2500
|
+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
|
2501
|
+
```
|
|
2502
|
+
|
|
2503
|
+
---
|
|
2504
|
+
|
|
2505
|
+
**END OF SPECIFICATION**
|