@openlearn/plugin-sdk 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # @openlearn/plugin-sdk
2
+
3
+ OpenLearn 平台插件开发的类型定义包。
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ npm install --save-dev @openlearn/plugin-sdk
9
+ ```
10
+
11
+ ## 使用
12
+
13
+ ```typescript
14
+ import type { PluginContext, Manifest } from '@openlearn/plugin-sdk';
15
+ import { ICommandBusServiceToken } from '@openlearn/plugin-sdk';
16
+
17
+ export default {
18
+ manifest: {
19
+ id: 'ext-my-plugin',
20
+ name: 'My Plugin',
21
+ version: '1.0.0',
22
+ main: 'index.js',
23
+ engines: { openlearn: '^2.5.0' },
24
+ capabilitiesProposed: ['lesson:read'],
25
+ } satisfies Manifest,
26
+
27
+ async activate(ctx: PluginContext) {
28
+ ctx.log.info('Plugin activated');
29
+ // ...
30
+ },
31
+ };
32
+ ```
33
+
34
+ ## 提供的内容
35
+
36
+ | 类别 | 导出 |
37
+ |------|------|
38
+ | 上下文 | `PluginContext`, `PluginDatabaseAPI`, `PluginInfo`, `IPluginLogger` |
39
+ | Manifest | `Manifest`, `ManifestV3` |
40
+ | 服务接口 | `ICommandBusService`, `IEventBusService`, `IActionRegistryService`, `ICapabilityService`, `IProcessService`, `IStorageService`, `IAIService` |
41
+ | Token | `ICommandBusServiceToken`, `IEventBusServiceToken`, ... (10 个 Token) |
42
+ | 命令/事件 | `PlatformCommand`, `PlatformEvent`, `CommandHandler`, `EventSubscriber` |
43
+ | 动作注册 | `ActionDescriptor` |
@@ -0,0 +1,299 @@
1
+ /**
2
+ * @openlearn/plugin-sdk — standalone type declarations (V3.1).
3
+ *
4
+ * Self-contained `.d.ts` for npm publishing. No re-exports from monorepo internals.
5
+ * This is the public API contract for OpenLearn plugin development.
6
+ *
7
+ * Generated from packages/core/ source types. Update when core types change.
8
+ */
9
+
10
+ // ── Token ────────────────────────────────────────────────────────────────
11
+
12
+ declare class Token<T> {
13
+ readonly name: string;
14
+ readonly version: string;
15
+ constructor(name: string, version?: string);
16
+ }
17
+
18
+ // ── Plugin State & Lifecycle ─────────────────────────────────────────────
19
+
20
+ declare enum PluginState {
21
+ INSTALLED = 'installed',
22
+ ACTIVATING = 'activating',
23
+ ACTIVE = 'active',
24
+ DEACTIVATING = 'deactivating',
25
+ INACTIVE = 'inactive',
26
+ ERROR = 'error',
27
+ UNINSTALLED = 'uninstalled',
28
+ }
29
+
30
+ interface Disposable {
31
+ dispose(): void;
32
+ }
33
+
34
+ // ── Manifest ─────────────────────────────────────────────────────────────
35
+
36
+ interface Manifest {
37
+ id: string;
38
+ name: string;
39
+ version: string;
40
+ main: string;
41
+ requires?: string[];
42
+ optional?: string[];
43
+ capabilitiesProposed?: string[];
44
+ engines?: { openlearn: string };
45
+ pluginDependencies?: string[];
46
+ provides?: string[];
47
+ configuration?: {
48
+ properties?: Record<string, {
49
+ type: 'string' | 'number' | 'boolean' | 'integer';
50
+ default?: unknown;
51
+ description?: string;
52
+ enum?: unknown[];
53
+ minimum?: number;
54
+ maximum?: number;
55
+ }>;
56
+ };
57
+ contributes?: Record<string, Array<{ id: string; [key: string]: unknown }>>;
58
+ [key: string]: unknown;
59
+ }
60
+
61
+ // ── Command & Event ──────────────────────────────────────────────────────
62
+
63
+ interface PlatformCommand<T = unknown> {
64
+ id: string;
65
+ type: string;
66
+ actorId: string;
67
+ payload: T;
68
+ timestamp: number;
69
+ metadata?: Record<string, unknown>;
70
+ }
71
+
72
+ interface CommandHandler {
73
+ execute(command: PlatformCommand): Promise<unknown>;
74
+ }
75
+
76
+ interface CommandMetadata {
77
+ approved?: boolean;
78
+ [key: string]: unknown;
79
+ }
80
+
81
+ interface PlatformEvent<T = unknown> {
82
+ id: string;
83
+ type: string;
84
+ source: string;
85
+ payload: T;
86
+ timestamp: number;
87
+ correlationId?: string;
88
+ }
89
+
90
+ type EventSubscriber = (event: PlatformEvent) => void | Promise<void>;
91
+
92
+ // ── Action Registry ──────────────────────────────────────────────────────
93
+
94
+ interface ActionDescriptor {
95
+ readonly id: string;
96
+ readonly commandType: string;
97
+ readonly description: string;
98
+ readonly inputSchema: unknown;
99
+ readonly capabilityRequired: string;
100
+ readonly isHighRisk?: boolean;
101
+ }
102
+
103
+ // ── Service Interfaces ───────────────────────────────────────────────────
104
+
105
+ interface ICommandBusService {
106
+ execute<T extends PlatformCommand>(command: T): Promise<unknown>;
107
+ registerHandler(commandType: string, handler: CommandHandler): Promise<void>;
108
+ unregisterHandler(commandType: string): Promise<void>;
109
+ createCommand<T>(type: string, payload: T, actorId: string, metadata?: CommandMetadata): Promise<PlatformCommand<T>>;
110
+ setInterceptor(interceptor: (command: PlatformCommand) => Promise<void>): Promise<void>;
111
+ }
112
+
113
+ interface IEventBusService {
114
+ publish(event: PlatformEvent): Promise<void>;
115
+ subscribe(eventType: string, subscriber: EventSubscriber): Promise<void>;
116
+ unsubscribe(eventType: string, subscriber: EventSubscriber): Promise<void>;
117
+ }
118
+
119
+ interface IActionRegistryService {
120
+ register(descriptor: ActionDescriptor): Promise<void>;
121
+ unregister(id: string): Promise<void>;
122
+ getAllActions(): Promise<ActionDescriptor[]>;
123
+ getAgentTools(): Promise<unknown[]>;
124
+ getActionByToolName(toolName: string): Promise<ActionDescriptor | undefined>;
125
+ getActionByCommandType(commandType: string): Promise<ActionDescriptor | undefined>;
126
+ }
127
+
128
+ interface ICapabilityService {
129
+ grant(actorId: string, cap: string): Promise<void>;
130
+ revokeAll(actorId: string): Promise<void>;
131
+ check(actorId: string, requiredCap: string): Promise<boolean>;
132
+ }
133
+
134
+ interface IProcessService {
135
+ spawn(name: string, taskType: string, payload: unknown): Promise<string>;
136
+ kill(processId: string): Promise<void>;
137
+ registerHandler(taskType: string, handler: ProcessHandler): Promise<void>;
138
+ unregisterHandler(taskType: string): Promise<void>;
139
+ registerInterval(name: string, intervalMs: number, tickFn: (log: (msg: string) => void) => void): Promise<string>;
140
+ restore(): Promise<void>;
141
+ }
142
+
143
+ type ProcessHandler = (
144
+ processId: string,
145
+ payload: unknown,
146
+ state: unknown,
147
+ log: (msg: string) => void,
148
+ updateState: (newState: unknown) => void,
149
+ ) => Promise<void>;
150
+
151
+ interface IStorageService {
152
+ get(key: string): Promise<unknown>;
153
+ set(key: string, value: unknown): Promise<void>;
154
+ delete(key: string): Promise<void>;
155
+ }
156
+
157
+ interface IAIService {
158
+ generateText(prompt: string, options?: { systemInstruction?: string; temperature?: number }): Promise<string>;
159
+ }
160
+
161
+ // ── Logger ───────────────────────────────────────────────────────────────
162
+
163
+ interface IPluginLogger {
164
+ debug(message: string, meta?: Record<string, unknown>): void;
165
+ info(message: string, meta?: Record<string, unknown>): void;
166
+ warn(message: string, meta?: Record<string, unknown>): void;
167
+ error(message: string, meta?: Record<string, unknown>): void;
168
+ }
169
+
170
+ // ── Config Service ───────────────────────────────────────────────────────
171
+
172
+ interface IConfigService {
173
+ get<T = unknown>(key: string): T;
174
+ getAll(): Record<string, unknown>;
175
+ set(key: string, value: unknown): Promise<void>;
176
+ onChange(callback: (key: string, newValue: unknown, oldValue: unknown) => void): () => void;
177
+ }
178
+
179
+ interface ConfigProperty {
180
+ type: 'string' | 'number' | 'boolean' | 'integer';
181
+ default?: unknown;
182
+ description?: string;
183
+ enum?: unknown[];
184
+ minimum?: number;
185
+ maximum?: number;
186
+ }
187
+
188
+ interface ConfigDeclaration {
189
+ properties?: Record<string, ConfigProperty>;
190
+ }
191
+
192
+ // ── Plugin Database API ──────────────────────────────────────────────────
193
+
194
+ interface PluginDatabaseAPI {
195
+ ensureTable(tableName: string, schema: string): Promise<void>;
196
+ table(tableName: string): string;
197
+ dropAllTables(): Promise<void>;
198
+ migrate(targetVersion: number, upgradeFn: (db: unknown) => Promise<void> | void): Promise<void>;
199
+ }
200
+
201
+ // ── Contribution Registry ────────────────────────────────────────────────
202
+
203
+ interface ContributionSummary {
204
+ slot: string;
205
+ count: number;
206
+ items: Array<{ id: string; label: string }>;
207
+ }
208
+
209
+ interface ContributionAccessor {
210
+ list(): ContributionSummary[];
211
+ }
212
+
213
+ interface ClassroomToolConfig {
214
+ id: string;
215
+ name: string;
216
+ icon?: string;
217
+ description?: string;
218
+ commandType: string;
219
+ payload?: Record<string, unknown>;
220
+ }
221
+
222
+ interface TeacherTabConfig {
223
+ id: string;
224
+ label: string;
225
+ icon?: string;
226
+ position?: number;
227
+ }
228
+
229
+ interface DashboardWidgetConfig {
230
+ id: string;
231
+ label: string;
232
+ icon?: string;
233
+ position?: number;
234
+ }
235
+
236
+ interface StudentViewConfig {
237
+ id: string;
238
+ label: string;
239
+ icon?: string;
240
+ route?: string;
241
+ }
242
+
243
+ interface StudentLessonToolConfig {
244
+ id: string;
245
+ label: string;
246
+ icon?: string;
247
+ }
248
+
249
+ type ContributionConfig =
250
+ | ClassroomToolConfig
251
+ | TeacherTabConfig
252
+ | DashboardWidgetConfig
253
+ | StudentViewConfig
254
+ | StudentLessonToolConfig;
255
+
256
+ // ── Plugin Context ───────────────────────────────────────────────────────
257
+
258
+ interface PluginContext {
259
+ services: {
260
+ commandBus: ICommandBusService;
261
+ eventBus: IEventBusService;
262
+ actionRegistry: IActionRegistryService;
263
+ capability: ICapabilityService;
264
+ processManager: IProcessService;
265
+ storage: IStorageService;
266
+ ai: IAIService;
267
+ };
268
+ pluginId: string;
269
+ manifest: Manifest;
270
+ resolve<T>(token: Token<T>): Promise<T>;
271
+ provide(tokenName: string, instance: unknown): Promise<void>;
272
+ db: PluginDatabaseAPI;
273
+ log: IPluginLogger;
274
+ config: IConfigService;
275
+ contributions: ContributionAccessor;
276
+ require(moduleName: string): unknown;
277
+ }
278
+
279
+ interface PluginInfo {
280
+ id: string;
281
+ name: string;
282
+ version: string;
283
+ state: PluginState;
284
+ status?: string;
285
+ execution_mode?: string;
286
+ }
287
+
288
+ // ── Token Constants ──────────────────────────────────────────────────────
289
+
290
+ declare const ICommandBusServiceToken: Token<ICommandBusService>;
291
+ declare const IEventBusServiceToken: Token<IEventBusService>;
292
+ declare const IActionRegistryServiceToken: Token<IActionRegistryService>;
293
+ declare const ICapabilityServiceToken: Token<ICapabilityService>;
294
+ declare const IProcessServiceToken: Token<IProcessService>;
295
+ declare const IStorageServiceToken: Token<IStorageService>;
296
+ declare const IAIServiceToken: Token<IAIService>;
297
+ declare const IDatabaseToken: Token<unknown>;
298
+ declare const IPluginHostToken: Token<unknown>;
299
+ declare const ISemesterGradeServiceToken: Token<unknown>;
package/dist/index.js ADDED
@@ -0,0 +1,70 @@
1
+ // ../core/di/errors.ts
2
+ var TokenError = class extends Error {
3
+ constructor(message) {
4
+ super(`[Token] ${message}`);
5
+ this.name = "TokenError";
6
+ }
7
+ };
8
+
9
+ // ../core/di/token.ts
10
+ var TOKEN_NAME_RE = /^@[a-zA-Z0-9_-]+\/[a-zA-Z0-9_-]+:[a-zA-Z0-9_]+$/;
11
+ var Token = class {
12
+ constructor(name, version = "1.0.0") {
13
+ if (!name || typeof name !== "string") {
14
+ throw new TokenError(
15
+ `Token name must be a non-empty string, got: ${String(name)}`
16
+ );
17
+ }
18
+ if (!TOKEN_NAME_RE.test(name)) {
19
+ throw new TokenError(
20
+ `Invalid Token name format: "${name}". Expected: @scope/domain:ServiceName`
21
+ );
22
+ }
23
+ this.name = name;
24
+ this.version = version;
25
+ }
26
+ };
27
+
28
+ // ../core/di/interfaces.ts
29
+ var ICommandBusServiceToken = new Token(
30
+ "@openlearn/core:ICommandBusService"
31
+ );
32
+ var IEventBusServiceToken = new Token(
33
+ "@openlearn/core:IEventBusService"
34
+ );
35
+ var IActionRegistryServiceToken = new Token(
36
+ "@openlearn/core:IActionRegistryService"
37
+ );
38
+ var ICapabilityServiceToken = new Token(
39
+ "@openlearn/core:ICapabilityService"
40
+ );
41
+ var IProcessServiceToken = new Token(
42
+ "@openlearn/core:IProcessService"
43
+ );
44
+ var IStorageServiceToken = new Token(
45
+ "@openlearn/core:IStorageService"
46
+ );
47
+ var IAIServiceToken = new Token(
48
+ "@openlearn/core:IAIService"
49
+ );
50
+ var IDatabaseToken = new Token(
51
+ "@openlearn/core:IDatabase"
52
+ );
53
+ var IPluginHostToken = new Token(
54
+ "@openlearn/core:IPluginHost"
55
+ );
56
+ var ISemesterGradeServiceToken = new Token(
57
+ "@openlearn/core:ISemesterGradeService"
58
+ );
59
+ export {
60
+ IAIServiceToken,
61
+ IActionRegistryServiceToken,
62
+ ICapabilityServiceToken,
63
+ ICommandBusServiceToken,
64
+ IDatabaseToken,
65
+ IEventBusServiceToken,
66
+ IPluginHostToken,
67
+ IProcessServiceToken,
68
+ ISemesterGradeServiceToken,
69
+ IStorageServiceToken
70
+ };
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@openlearn/plugin-sdk",
3
+ "version": "3.1.0",
4
+ "description": "TypeScript type definitions and DI tokens for OpenLearn plugin development",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js",
12
+ "default": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist/",
17
+ "README.md"
18
+ ],
19
+ "scripts": {
20
+ "build": "node build.mjs",
21
+ "prepublishOnly": "node build.mjs"
22
+ },
23
+ "peerDependencies": {
24
+ "zod": ">=4"
25
+ },
26
+ "keywords": [
27
+ "openlearn",
28
+ "plugin",
29
+ "sdk",
30
+ "types",
31
+ "educational",
32
+ "lms"
33
+ ],
34
+ "license": "MIT",
35
+ "repository": {
36
+ "type": "git",
37
+ "url": "https://github.com/openlearn/openlearnv2",
38
+ "directory": "packages/plugin-sdk"
39
+ },
40
+ "publishConfig": {
41
+ "access": "public"
42
+ }
43
+ }