Skip to content

Commit 74ada0d

Browse files
committed
feat(types): 添加 IKernel 和 RuntimePlugin 接口定义以支持插件架构
1 parent f03e5d4 commit 74ada0d

File tree

6 files changed

+134
-28
lines changed

6 files changed

+134
-28
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
title: IKernel (Interface)
3+
description: Kernel 接口契约
4+
---
5+
6+
`IKernel` 是插件与宿主系统交互的核心接口。为了保持插件的独立性和可测试性,插件不应直接依赖 Kernel 的具体实现类,而应依赖此接口。
7+
8+
## 接口定义
9+
10+
```typescript
11+
export interface IKernel {
12+
/**
13+
* 启动内核
14+
* 初始化所有已注册的插件和驱动
15+
*/
16+
start(): Promise<void>;
17+
18+
/**
19+
* 获取 ObjectQL 引擎实例
20+
*/
21+
getEngine(): ObjectQL;
22+
23+
/**
24+
* 获取上下文对象
25+
*/
26+
getContext(): RuntimeContext;
27+
28+
// ... 更多生命周期方法
29+
}
30+
```
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
title: RuntimePlugin (Interface)
3+
description: 运行时插件接口
4+
---
5+
6+
所有扩展 ObjectStack 功能的模块必须实现此接口。
7+
8+
## 接口定义
9+
10+
```typescript
11+
export interface RuntimePlugin {
12+
/**
13+
* 插件唯一标识符
14+
* 推荐格式: com.organization.plugin-name
15+
*/
16+
name: string;
17+
18+
/**
19+
* 安装阶段
20+
* 在 Kernel 初始化时调用,用于注册 Hook 或扩展 Schema
21+
*/
22+
install?: (ctx: RuntimeContext) => void | Promise<void>;
23+
24+
/**
25+
* 启动阶段
26+
* 在 Kernel 启动时调用,用于初始化服务(如启动 HTTP Server)
27+
*/
28+
onStart?: (ctx: RuntimeContext) => void | Promise<void>;
29+
}
30+
```
31+
32+
## 依赖关系
33+
34+
插件应声明它依赖于 `@objectstack/types` 而不是具体运行时。
35+
36+
```json
37+
{
38+
"dependencies": {
39+
"@objectstack/types": "*"
40+
},
41+
"peerDependencies": {
42+
"@objectstack/runtime": "*"
43+
}
44+
}
45+
```

content/docs/specifications/server/kernel-architecture.cn.mdx

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,21 @@ import { Cpu, ShieldCheck, RefreshCw, Zap } from 'lucide-react';
99

1010
它充当位于 API 和数据库驱动程序之间的中间件内核,确保每次数据访问都经过身份验证、授权并符合业务规则。
1111

12-
## 内核层
13-
14-
### 1. 身份层(谁?)
15-
在任何逻辑运行之前,内核解析行为者。
16-
* **身份验证:** OIDC、SAML 或 LDAP。
17-
* **会话管理:** 无状态 JWT 或会话存储。
18-
* **上下文注入:**`userId``role``organization` 加载到请求上下文中。
19-
20-
### 2. 策略层(他们可以吗?)
21-
内核强制执行**"纵深防御"**安全模型。
22-
* **配置文件检查:** 此用户是否有使用此应用的许可证?
23-
* **对象权限:** 他们可以 `READ` `Contract` 对象吗?
24-
* **字段级安全(FLS):** 他们可以看到 `contract_value` 吗?
25-
* **共享规则:** 他们可以访问*此特定记录*吗?
26-
27-
### 3. 逻辑层(会发生什么?)
28-
这是"业务逻辑"容器。
29-
* **触发器:** 同步逻辑(`beforeInsert`)。
30-
* **流程:** 可视化编排(`审批流程`)。
31-
* **作业:** 异步任务(`发送电子邮件`)。
32-
33-
### 4. 同步层(它在哪里?)
34-
ObjectOS 设计为**本地优先**操作。
35-
* **变更数据捕获(CDC):** 跟踪每个事务的"增量"。
36-
* **复制:** 将这些增量同步到边缘设备(SQLite)或数据仓库(Snowflake)。
12+
## 架构组件
13+
14+
```mermaid
15+
graph TD
16+
A[外部 API 请求] --> B(Host / Adapter Layer);
17+
B --> C{ObjectStack Kernel};
18+
C --> D[Plugin: Identity Provider];
19+
C --> E[Plugin: Authorization Engine];
20+
C --> F[ObjectQL Engine];
21+
F --> G[Schema Registry];
22+
F --> H[Driver Layer];
23+
```
24+
25+
* **Host (Adapter)**: 适配不同的运行环境(Hono/Node, Next.js/Edge, Electron)。
26+
* **Kernel**: 核心控制器,负责加载插件、管理上下文。
27+
* **Plugins**: 可插拔的能力模块(如 Auth, Audit, Workflow)。
28+
* **ObjectQL**: 纯逻辑引擎,负责数据查询解析与验证。
29+
* **Driver**: 实际的 I/O 执行者。

examples/host/debug-registry.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
import { ObjectStackKernel } from '@objectstack/runtime';
3+
import { SchemaRegistry, ObjectQL } from '@objectstack/objectql';
4+
import { InMemoryDriver } from '@objectstack/driver-memory';
5+
6+
import TodoApp from '@objectstack/example-todo/objectstack.config';
7+
8+
(async () => {
9+
console.log('--- Debug Registry ---');
10+
console.log('Apps:', [TodoApp.name]);
11+
console.log('Objects inside App:', TodoApp.objects?.map((o: any) => o.name));
12+
13+
const kernel = new ObjectStackKernel([
14+
TodoApp,
15+
new InMemoryDriver()
16+
]);
17+
18+
await kernel.start();
19+
20+
console.log('--- Post Start ---');
21+
22+
// Check Registry directly
23+
const obj = SchemaRegistry.getObject('todo_task');
24+
console.log('Registry "todo_task":', obj ? 'FOUND' : 'MISSING');
25+
26+
// Check Registry via Engine
27+
try {
28+
// Access private engine map if possible or simulate query
29+
// The engine doesn't expose a 'hasObject' method easily, but we can inspect internal logic
30+
// Actually SchemaRegistry is static, so if it's there, it's there.
31+
} catch (e) {
32+
console.error(e);
33+
}
34+
})();

examples/host/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
"clean": "rm -rf dist node_modules"
88
},
99
"dependencies": {
10-
"@objectstack/runtime": "workspace:*",
1110
"@objectstack/driver-memory": "workspace:*",
12-
"@objectstack/plugin-hono-server": "workspace:*",
1311
"@objectstack/example-crm": "workspace:*",
1412
"@objectstack/example-todo": "workspace:*",
15-
"@objectstack/plugin-bi": "workspace:*"
13+
"@objectstack/objectql": "workspace:*",
14+
"@objectstack/plugin-bi": "workspace:*",
15+
"@objectstack/plugin-hono-server": "workspace:*",
16+
"@objectstack/runtime": "workspace:*"
1617
},
1718
"devDependencies": {
1819
"ts-node": "^10.9.1",

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)