-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathruntime-api.ts
More file actions
150 lines (131 loc) · 3.71 KB
/
runtime-api.ts
File metadata and controls
150 lines (131 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Structural stand-ins for the openclaw plugin SDK types.
// The real module (openclaw/plugin-sdk/core) is only available at runtime inside
// the openclaw host process. These minimal types are sufficient for standalone
// compilation and testing.
export interface OpenClawPluginApi {
rootDir?: string;
pluginConfig?: Record<string, unknown>;
config?: Record<string, unknown>;
logger: {
debug?: (msg: string) => void;
info: (msg: string) => void;
warn: (msg: string) => void;
error: (msg: string) => void;
};
runtime: {
state: {
resolveStateDir: () => string;
};
};
on: (hookName: string, handler: (...args: any[]) => any) => void;
getPluginConfig: (pluginId: string) => Record<string, unknown> | undefined;
resolvePath: (p: string) => string;
}
export type OpenClawPluginConfigSchema = Record<string, unknown>;
export type PluginHookMessageContext = {
sessionKey?: string;
runId?: string;
[key: string]: unknown;
};
export type PluginHookGatewayStartEvent = Record<string, never>;
export type PluginHookMessageReceivedEvent = {
content?: string;
[key: string]: unknown;
};
export type PluginHookMessageSendingEvent = {
to: string;
content: string;
metadata?: Record<string, unknown>;
};
export type PluginHookMessageSendingResult = {
content?: string;
cancel?: boolean;
};
export type PluginHookBeforePromptBuildEvent = {
prompt?: string;
messages?: unknown[];
[key: string]: unknown;
};
export type PluginHookBeforePromptBuildResult = {
prependContext?: string;
appendContext?: string;
prependSystemContext?: string;
appendSystemContext?: string;
};
export type PluginHookBeforeToolCallEvent = {
toolName: string;
params?: Record<string, unknown>;
};
export type PluginHookBeforeToolCallResult = {
block?: boolean;
blockReason?: string;
};
export type PluginHookAfterToolCallEvent = {
toolName: string;
params?: Record<string, unknown>;
result?: unknown;
error?: string;
durationMs?: number;
[key: string]: unknown;
};
export type PluginHookBeforeMessageWriteEvent = {
message: Record<string, unknown>;
};
export type PluginHookBeforeMessageWriteResult = {
block?: boolean;
message?: Record<string, unknown>;
};
export type PluginHookAgentEndEvent = {
messages?: unknown[];
success?: boolean;
error?: string;
durationMs?: number;
[key: string]: unknown;
};
export type PluginHookSessionEndEvent = {
sessionId?: string;
sessionKey?: string;
messageCount?: number;
durationMs?: number;
[key: string]: unknown;
};
type CompatiblePluginKind = "memory" | "context-engine";
type CompatiblePluginEntry = {
id: string;
name: string;
description: string;
kind?: CompatiblePluginKind;
configSchema?: OpenClawPluginConfigSchema;
register: (api: OpenClawPluginApi) => void | Promise<void>;
};
type DefinePluginEntryOptions = CompatiblePluginEntry & {
configSchema?: OpenClawPluginConfigSchema | (() => OpenClawPluginConfigSchema);
};
function resolvePluginConfigSchema(
configSchema: DefinePluginEntryOptions["configSchema"],
): OpenClawPluginConfigSchema | undefined {
if (!configSchema) {
return undefined;
}
return typeof configSchema === "function" ? configSchema() : configSchema;
}
// Keep the plugin entry helper local so third-party installs do not depend on
// specific OpenClaw SDK subpaths being present at runtime.
export function definePluginEntry({
id,
name,
description,
kind,
configSchema,
register,
}: DefinePluginEntryOptions): CompatiblePluginEntry {
const resolvedConfigSchema = resolvePluginConfigSchema(configSchema);
return {
id,
name,
description,
...(kind ? { kind } : {}),
...(resolvedConfigSchema ? { configSchema: resolvedConfigSchema } : {}),
register,
};
}