@alfe.ai/openclaw-chat 0.0.28 → 0.0.30
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/dist/plugin.d.cts +11 -0
- package/dist/plugin.d.ts +11 -0
- package/dist/plugin2.cjs +100 -69
- package/dist/plugin2.js +100 -72
- package/openclaw.plugin.json +0 -1
- package/package.json +1 -1
package/dist/plugin.d.cts
CHANGED
|
@@ -220,6 +220,12 @@ interface AgentEventPayload {
|
|
|
220
220
|
data: Record<string, unknown>;
|
|
221
221
|
sessionKey?: string;
|
|
222
222
|
}
|
|
223
|
+
interface PluginServiceContext {
|
|
224
|
+
config: Record<string, unknown>;
|
|
225
|
+
workspaceDir?: string;
|
|
226
|
+
stateDir: string;
|
|
227
|
+
logger: PluginLogger;
|
|
228
|
+
}
|
|
223
229
|
interface PluginApi {
|
|
224
230
|
logger: PluginLogger;
|
|
225
231
|
registrationMode?: 'full' | 'setup-only' | 'setup-runtime' | 'cli-metadata';
|
|
@@ -234,6 +240,11 @@ interface PluginApi {
|
|
|
234
240
|
execute: (toolCallId: string, params: Record<string, unknown>) => Promise<unknown>;
|
|
235
241
|
}): void;
|
|
236
242
|
registerGatewayMethod?(name: string, handler: (...args: unknown[]) => Promise<unknown>): void;
|
|
243
|
+
registerService?(service: {
|
|
244
|
+
id: string;
|
|
245
|
+
start: (ctx: PluginServiceContext) => void | Promise<void>;
|
|
246
|
+
stop?: (ctx: PluginServiceContext) => void | Promise<void>;
|
|
247
|
+
}): void;
|
|
237
248
|
on(event: string, handler: (...args: unknown[]) => void | Promise<void>, options?: {
|
|
238
249
|
priority?: number;
|
|
239
250
|
}): void;
|
package/dist/plugin.d.ts
CHANGED
|
@@ -220,6 +220,12 @@ interface AgentEventPayload {
|
|
|
220
220
|
data: Record<string, unknown>;
|
|
221
221
|
sessionKey?: string;
|
|
222
222
|
}
|
|
223
|
+
interface PluginServiceContext {
|
|
224
|
+
config: Record<string, unknown>;
|
|
225
|
+
workspaceDir?: string;
|
|
226
|
+
stateDir: string;
|
|
227
|
+
logger: PluginLogger;
|
|
228
|
+
}
|
|
223
229
|
interface PluginApi {
|
|
224
230
|
logger: PluginLogger;
|
|
225
231
|
registrationMode?: 'full' | 'setup-only' | 'setup-runtime' | 'cli-metadata';
|
|
@@ -234,6 +240,11 @@ interface PluginApi {
|
|
|
234
240
|
execute: (toolCallId: string, params: Record<string, unknown>) => Promise<unknown>;
|
|
235
241
|
}): void;
|
|
236
242
|
registerGatewayMethod?(name: string, handler: (...args: unknown[]) => Promise<unknown>): void;
|
|
243
|
+
registerService?(service: {
|
|
244
|
+
id: string;
|
|
245
|
+
start: (ctx: PluginServiceContext) => void | Promise<void>;
|
|
246
|
+
stop?: (ctx: PluginServiceContext) => void | Promise<void>;
|
|
247
|
+
}): void;
|
|
237
248
|
on(event: string, handler: (...args: unknown[]) => void | Promise<void>, options?: {
|
|
238
249
|
priority?: number;
|
|
239
250
|
}): void;
|
package/dist/plugin2.cjs
CHANGED
|
@@ -517,6 +517,8 @@ function validateAttachmentUrl(input, opts = {}) {
|
|
|
517
517
|
* ← onAgentEvent streaming ← (real-time deltas)
|
|
518
518
|
* ← deliver() callback ← (final response)
|
|
519
519
|
*/
|
|
520
|
+
const require$1 = (0, node_module.createRequire)(require("url").pathToFileURL(__filename).href);
|
|
521
|
+
const pkg = require$1("../package.json");
|
|
520
522
|
let dispatchInbound = null;
|
|
521
523
|
/**
|
|
522
524
|
* Resolve OpenClaw SDK functions from the running process.
|
|
@@ -527,7 +529,7 @@ let dispatchInbound = null;
|
|
|
527
529
|
* the global modules path from process.execPath.
|
|
528
530
|
*/
|
|
529
531
|
function resolveOpenClawSdk(log) {
|
|
530
|
-
const anchors = [require.main?.filename, process.argv[1]].filter(Boolean);
|
|
532
|
+
const anchors = [require$1.main?.filename, process.argv[1]].filter(Boolean);
|
|
531
533
|
for (const anchor of anchors) try {
|
|
532
534
|
const channelInbound = (0, node_module.createRequire)(anchor)("openclaw/plugin-sdk/channel-inbound");
|
|
533
535
|
if (channelInbound.dispatchInboundDirectDmWithRuntime) {
|
|
@@ -807,16 +809,20 @@ const plugin = {
|
|
|
807
809
|
id: "@alfe.ai/openclaw-chat",
|
|
808
810
|
name: "Alfe Chat Plugin",
|
|
809
811
|
description: "Chat conversation channel — web widget and mobile app share unified chat sessions",
|
|
810
|
-
version:
|
|
812
|
+
version: pkg.version,
|
|
811
813
|
activate(api) {
|
|
812
814
|
const log = api.logger;
|
|
813
815
|
const alreadyActivated = globalThis.__alfeChatPluginActivated === true;
|
|
814
816
|
const alfeChannel = createAlfeChannelPlugin();
|
|
815
817
|
api.registerChannel(alfeChannel);
|
|
816
818
|
log.info(`Registered channel: ${alfeChannel.id}`);
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
819
|
+
const pluginConfig = (((api.config ?? {}).plugins?.entries)?.["@alfe.ai/openclaw-chat"] ?? {}).config ?? {};
|
|
820
|
+
const startChatService = () => {
|
|
821
|
+
if (globalThis.__alfeChatPluginActivated === true) {
|
|
822
|
+
log.debug("Chat plugin already activated — skipping duplicate");
|
|
823
|
+
return;
|
|
824
|
+
}
|
|
825
|
+
globalThis.__alfeChatPluginActivated = true;
|
|
820
826
|
log.info("Chat plugin registering...");
|
|
821
827
|
resolveOpenClawSdk(log);
|
|
822
828
|
pluginRuntime = api.runtime ?? null;
|
|
@@ -829,44 +835,88 @@ const plugin = {
|
|
|
829
835
|
} catch {
|
|
830
836
|
log.debug("Metrics client not initialized — activity tracking disabled");
|
|
831
837
|
}
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
apiKey: pluginConfig.apiKey,
|
|
838
|
-
chatWsUrl: pluginConfig.chatWsUrl
|
|
839
|
-
});
|
|
840
|
-
if (chatWsUrl && apiKey) {
|
|
841
|
-
log.info(`Connecting to chat service: ${chatWsUrl}`);
|
|
842
|
-
chatClient = new _alfe_ai_chat.ChatServiceClient({
|
|
843
|
-
wsUrl: chatWsUrl,
|
|
844
|
-
apiKey,
|
|
845
|
-
onRequest: (request) => {
|
|
846
|
-
const handle = async () => {
|
|
847
|
-
if (request.method === "agent") await handleAgentRequest(request, log);
|
|
848
|
-
else if (request.method === "sessions.list") await handleSessionsList(request, log);
|
|
849
|
-
else if (request.method === "sessions.get") await handleSessionsGet(request, log);
|
|
850
|
-
else chatClient?.sendResponse(request.id, false, { message: `Unknown method: ${request.method}` });
|
|
851
|
-
};
|
|
852
|
-
handle().catch((err) => {
|
|
853
|
-
const errMsg = err instanceof Error ? err.message : String(err);
|
|
854
|
-
log.error(`Request handler crashed: ${errMsg}`);
|
|
855
|
-
chatClient?.sendResponse(request.id, false, { message: "Internal error" });
|
|
856
|
-
});
|
|
857
|
-
},
|
|
858
|
-
onConnectionChange: (connected) => {
|
|
859
|
-
log.info(`Chat service connection: ${connected ? "connected" : "disconnected"}`);
|
|
860
|
-
},
|
|
861
|
-
logger: log
|
|
838
|
+
connectingPromise = Promise.resolve().then(() => {
|
|
839
|
+
try {
|
|
840
|
+
const { apiKey, chatWsUrl } = (0, _alfe_ai_chat.resolveAlfeChat)({
|
|
841
|
+
apiKey: pluginConfig.apiKey,
|
|
842
|
+
chatWsUrl: pluginConfig.chatWsUrl
|
|
862
843
|
});
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
844
|
+
if (chatWsUrl && apiKey) {
|
|
845
|
+
log.info(`Connecting to chat service: ${chatWsUrl}`);
|
|
846
|
+
chatClient = new _alfe_ai_chat.ChatServiceClient({
|
|
847
|
+
wsUrl: chatWsUrl,
|
|
848
|
+
apiKey,
|
|
849
|
+
onRequest: (request) => {
|
|
850
|
+
const handle = async () => {
|
|
851
|
+
if (request.method === "agent") await handleAgentRequest(request, log);
|
|
852
|
+
else if (request.method === "sessions.list") await handleSessionsList(request, log);
|
|
853
|
+
else if (request.method === "sessions.get") await handleSessionsGet(request, log);
|
|
854
|
+
else chatClient?.sendResponse(request.id, false, { message: `Unknown method: ${request.method}` });
|
|
855
|
+
};
|
|
856
|
+
handle().catch((err) => {
|
|
857
|
+
const errMsg = err instanceof Error ? err.message : String(err);
|
|
858
|
+
log.error(`Request handler crashed: ${errMsg}`);
|
|
859
|
+
chatClient?.sendResponse(request.id, false, { message: "Internal error" });
|
|
860
|
+
});
|
|
861
|
+
},
|
|
862
|
+
onConnectionChange: (connected) => {
|
|
863
|
+
log.info(`Chat service connection: ${connected ? "connected" : "disconnected"}`);
|
|
864
|
+
},
|
|
865
|
+
logger: log
|
|
866
|
+
});
|
|
867
|
+
chatClient.start();
|
|
868
|
+
log.info("Chat service relay started");
|
|
869
|
+
} else log.info("Chat service URL not configured — running without chat service relay");
|
|
870
|
+
} catch (err) {
|
|
871
|
+
log.error(`Failed to initialize chat service: ${err instanceof Error ? err.message : String(err)}`);
|
|
872
|
+
}
|
|
873
|
+
});
|
|
874
|
+
if (typeof api.registerTool === "function") connectingPromise.then(() => {
|
|
875
|
+
if (chatClient && typeof api.registerTool === "function") {
|
|
876
|
+
const a2aTools = buildA2ATools(chatClient, log);
|
|
877
|
+
for (const tool of a2aTools) api.registerTool(tool);
|
|
878
|
+
log.info(`Registered ${String(a2aTools.length)} agent-to-agent tools`);
|
|
879
|
+
}
|
|
880
|
+
}).catch(() => {});
|
|
881
|
+
api.on("session_start", async (...eventArgs) => {
|
|
882
|
+
if (!pluginRuntime) return;
|
|
883
|
+
const key = eventArgs[0].sessionKey;
|
|
884
|
+
if (!key || isAlfeSessionKey(key)) return;
|
|
885
|
+
log.info(`Chat session starting: ${key}`);
|
|
886
|
+
await createSession(key, "", "alfe");
|
|
887
|
+
}, { priority: 50 });
|
|
888
|
+
api.on("message_received", async (...eventArgs) => {
|
|
889
|
+
if (!pluginRuntime) return;
|
|
890
|
+
const event = eventArgs[0];
|
|
891
|
+
const ctx = eventArgs[1];
|
|
892
|
+
if (!ctx.conversationId || ctx.channelId === "alfe") return;
|
|
893
|
+
await addMessage(ctx.conversationId, "user", event.content, event.from);
|
|
894
|
+
});
|
|
895
|
+
api.on("session_end", (...eventArgs) => {
|
|
896
|
+
if (!pluginRuntime) return;
|
|
897
|
+
const key = eventArgs[0].sessionKey;
|
|
898
|
+
if (!key || !isAlfeSessionKey(key)) return;
|
|
899
|
+
log.info(`Chat session ending: ${key}`);
|
|
900
|
+
});
|
|
901
|
+
};
|
|
902
|
+
const stopChatService = async () => {
|
|
903
|
+
globalThis.__alfeChatPluginActivated = false;
|
|
904
|
+
if (connectingPromise) {
|
|
905
|
+
await connectingPromise.catch((err) => {
|
|
906
|
+
log.debug(`Connection attempt failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
907
|
+
});
|
|
908
|
+
connectingPromise = null;
|
|
868
909
|
}
|
|
869
|
-
|
|
910
|
+
if (chatClient) {
|
|
911
|
+
chatClient.stop();
|
|
912
|
+
chatClient = null;
|
|
913
|
+
log.info("Chat service client stopped");
|
|
914
|
+
}
|
|
915
|
+
pluginRuntime = null;
|
|
916
|
+
dispatchInbound = null;
|
|
917
|
+
metricsClient = null;
|
|
918
|
+
log.info("Chat plugin deactivated");
|
|
919
|
+
};
|
|
870
920
|
if (typeof api.registerGatewayMethod === "function") {
|
|
871
921
|
api.registerGatewayMethod("sessions.list", async (...args) => {
|
|
872
922
|
const params = args[0];
|
|
@@ -902,35 +952,16 @@ const plugin = {
|
|
|
902
952
|
});
|
|
903
953
|
log.info("Registered gateway RPC methods: sessions.list, sessions.get");
|
|
904
954
|
}
|
|
905
|
-
if (
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
955
|
+
if (api.registerService) api.registerService({
|
|
956
|
+
id: "alfe-chat-relay",
|
|
957
|
+
start: () => {
|
|
958
|
+
startChatService();
|
|
959
|
+
},
|
|
960
|
+
stop: async () => {
|
|
961
|
+
await stopChatService();
|
|
910
962
|
}
|
|
911
|
-
})
|
|
912
|
-
if (!alreadyActivated)
|
|
913
|
-
api.on("session_start", async (...eventArgs) => {
|
|
914
|
-
if (!pluginRuntime) return;
|
|
915
|
-
const key = eventArgs[0].sessionKey;
|
|
916
|
-
if (!key || isAlfeSessionKey(key)) return;
|
|
917
|
-
log.info(`Chat session starting: ${key}`);
|
|
918
|
-
await createSession(key, "", "alfe");
|
|
919
|
-
}, { priority: 50 });
|
|
920
|
-
api.on("message_received", async (...eventArgs) => {
|
|
921
|
-
if (!pluginRuntime) return;
|
|
922
|
-
const event = eventArgs[0];
|
|
923
|
-
const ctx = eventArgs[1];
|
|
924
|
-
if (!ctx.conversationId || ctx.channelId === "alfe") return;
|
|
925
|
-
await addMessage(ctx.conversationId, "user", event.content, event.from);
|
|
926
|
-
});
|
|
927
|
-
api.on("session_end", (...eventArgs) => {
|
|
928
|
-
if (!pluginRuntime) return;
|
|
929
|
-
const key = eventArgs[0].sessionKey;
|
|
930
|
-
if (!key || !isAlfeSessionKey(key)) return;
|
|
931
|
-
log.info(`Chat session ending: ${key}`);
|
|
932
|
-
});
|
|
933
|
-
}
|
|
963
|
+
});
|
|
964
|
+
else if (!alreadyActivated) startChatService();
|
|
934
965
|
log.info("Chat plugin registered");
|
|
935
966
|
},
|
|
936
967
|
async deactivate(api) {
|
package/dist/plugin2.js
CHANGED
|
@@ -6,9 +6,6 @@ import { ChatServiceClient, resolveAlfeChat } from "@alfe.ai/chat";
|
|
|
6
6
|
import { resolveConfig } from "@alfe.ai/config";
|
|
7
7
|
import { AgentApiClient } from "@alfe.ai/agent-api-client";
|
|
8
8
|
import { existsSync } from "node:fs";
|
|
9
|
-
//#region \0rolldown/runtime.js
|
|
10
|
-
var __require = /* @__PURE__ */ createRequire(import.meta.url);
|
|
11
|
-
//#endregion
|
|
12
9
|
//#region src/alfe-channel.ts
|
|
13
10
|
const CHANNEL_ID = "alfe";
|
|
14
11
|
const DEFAULT_ACCOUNT_ID = "default";
|
|
@@ -520,6 +517,8 @@ function validateAttachmentUrl(input, opts = {}) {
|
|
|
520
517
|
* ← onAgentEvent streaming ← (real-time deltas)
|
|
521
518
|
* ← deliver() callback ← (final response)
|
|
522
519
|
*/
|
|
520
|
+
const require = createRequire(import.meta.url);
|
|
521
|
+
const pkg = require("../package.json");
|
|
523
522
|
let dispatchInbound = null;
|
|
524
523
|
/**
|
|
525
524
|
* Resolve OpenClaw SDK functions from the running process.
|
|
@@ -530,7 +529,7 @@ let dispatchInbound = null;
|
|
|
530
529
|
* the global modules path from process.execPath.
|
|
531
530
|
*/
|
|
532
531
|
function resolveOpenClawSdk(log) {
|
|
533
|
-
const anchors = [
|
|
532
|
+
const anchors = [require.main?.filename, process.argv[1]].filter(Boolean);
|
|
534
533
|
for (const anchor of anchors) try {
|
|
535
534
|
const channelInbound = createRequire(anchor)("openclaw/plugin-sdk/channel-inbound");
|
|
536
535
|
if (channelInbound.dispatchInboundDirectDmWithRuntime) {
|
|
@@ -810,16 +809,20 @@ const plugin = {
|
|
|
810
809
|
id: "@alfe.ai/openclaw-chat",
|
|
811
810
|
name: "Alfe Chat Plugin",
|
|
812
811
|
description: "Chat conversation channel — web widget and mobile app share unified chat sessions",
|
|
813
|
-
version:
|
|
812
|
+
version: pkg.version,
|
|
814
813
|
activate(api) {
|
|
815
814
|
const log = api.logger;
|
|
816
815
|
const alreadyActivated = globalThis.__alfeChatPluginActivated === true;
|
|
817
816
|
const alfeChannel = createAlfeChannelPlugin();
|
|
818
817
|
api.registerChannel(alfeChannel);
|
|
819
818
|
log.info(`Registered channel: ${alfeChannel.id}`);
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
819
|
+
const pluginConfig = (((api.config ?? {}).plugins?.entries)?.["@alfe.ai/openclaw-chat"] ?? {}).config ?? {};
|
|
820
|
+
const startChatService = () => {
|
|
821
|
+
if (globalThis.__alfeChatPluginActivated === true) {
|
|
822
|
+
log.debug("Chat plugin already activated — skipping duplicate");
|
|
823
|
+
return;
|
|
824
|
+
}
|
|
825
|
+
globalThis.__alfeChatPluginActivated = true;
|
|
823
826
|
log.info("Chat plugin registering...");
|
|
824
827
|
resolveOpenClawSdk(log);
|
|
825
828
|
pluginRuntime = api.runtime ?? null;
|
|
@@ -832,44 +835,88 @@ const plugin = {
|
|
|
832
835
|
} catch {
|
|
833
836
|
log.debug("Metrics client not initialized — activity tracking disabled");
|
|
834
837
|
}
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
apiKey: pluginConfig.apiKey,
|
|
841
|
-
chatWsUrl: pluginConfig.chatWsUrl
|
|
842
|
-
});
|
|
843
|
-
if (chatWsUrl && apiKey) {
|
|
844
|
-
log.info(`Connecting to chat service: ${chatWsUrl}`);
|
|
845
|
-
chatClient = new ChatServiceClient({
|
|
846
|
-
wsUrl: chatWsUrl,
|
|
847
|
-
apiKey,
|
|
848
|
-
onRequest: (request) => {
|
|
849
|
-
const handle = async () => {
|
|
850
|
-
if (request.method === "agent") await handleAgentRequest(request, log);
|
|
851
|
-
else if (request.method === "sessions.list") await handleSessionsList(request, log);
|
|
852
|
-
else if (request.method === "sessions.get") await handleSessionsGet(request, log);
|
|
853
|
-
else chatClient?.sendResponse(request.id, false, { message: `Unknown method: ${request.method}` });
|
|
854
|
-
};
|
|
855
|
-
handle().catch((err) => {
|
|
856
|
-
const errMsg = err instanceof Error ? err.message : String(err);
|
|
857
|
-
log.error(`Request handler crashed: ${errMsg}`);
|
|
858
|
-
chatClient?.sendResponse(request.id, false, { message: "Internal error" });
|
|
859
|
-
});
|
|
860
|
-
},
|
|
861
|
-
onConnectionChange: (connected) => {
|
|
862
|
-
log.info(`Chat service connection: ${connected ? "connected" : "disconnected"}`);
|
|
863
|
-
},
|
|
864
|
-
logger: log
|
|
838
|
+
connectingPromise = Promise.resolve().then(() => {
|
|
839
|
+
try {
|
|
840
|
+
const { apiKey, chatWsUrl } = resolveAlfeChat({
|
|
841
|
+
apiKey: pluginConfig.apiKey,
|
|
842
|
+
chatWsUrl: pluginConfig.chatWsUrl
|
|
865
843
|
});
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
844
|
+
if (chatWsUrl && apiKey) {
|
|
845
|
+
log.info(`Connecting to chat service: ${chatWsUrl}`);
|
|
846
|
+
chatClient = new ChatServiceClient({
|
|
847
|
+
wsUrl: chatWsUrl,
|
|
848
|
+
apiKey,
|
|
849
|
+
onRequest: (request) => {
|
|
850
|
+
const handle = async () => {
|
|
851
|
+
if (request.method === "agent") await handleAgentRequest(request, log);
|
|
852
|
+
else if (request.method === "sessions.list") await handleSessionsList(request, log);
|
|
853
|
+
else if (request.method === "sessions.get") await handleSessionsGet(request, log);
|
|
854
|
+
else chatClient?.sendResponse(request.id, false, { message: `Unknown method: ${request.method}` });
|
|
855
|
+
};
|
|
856
|
+
handle().catch((err) => {
|
|
857
|
+
const errMsg = err instanceof Error ? err.message : String(err);
|
|
858
|
+
log.error(`Request handler crashed: ${errMsg}`);
|
|
859
|
+
chatClient?.sendResponse(request.id, false, { message: "Internal error" });
|
|
860
|
+
});
|
|
861
|
+
},
|
|
862
|
+
onConnectionChange: (connected) => {
|
|
863
|
+
log.info(`Chat service connection: ${connected ? "connected" : "disconnected"}`);
|
|
864
|
+
},
|
|
865
|
+
logger: log
|
|
866
|
+
});
|
|
867
|
+
chatClient.start();
|
|
868
|
+
log.info("Chat service relay started");
|
|
869
|
+
} else log.info("Chat service URL not configured — running without chat service relay");
|
|
870
|
+
} catch (err) {
|
|
871
|
+
log.error(`Failed to initialize chat service: ${err instanceof Error ? err.message : String(err)}`);
|
|
872
|
+
}
|
|
873
|
+
});
|
|
874
|
+
if (typeof api.registerTool === "function") connectingPromise.then(() => {
|
|
875
|
+
if (chatClient && typeof api.registerTool === "function") {
|
|
876
|
+
const a2aTools = buildA2ATools(chatClient, log);
|
|
877
|
+
for (const tool of a2aTools) api.registerTool(tool);
|
|
878
|
+
log.info(`Registered ${String(a2aTools.length)} agent-to-agent tools`);
|
|
879
|
+
}
|
|
880
|
+
}).catch(() => {});
|
|
881
|
+
api.on("session_start", async (...eventArgs) => {
|
|
882
|
+
if (!pluginRuntime) return;
|
|
883
|
+
const key = eventArgs[0].sessionKey;
|
|
884
|
+
if (!key || isAlfeSessionKey(key)) return;
|
|
885
|
+
log.info(`Chat session starting: ${key}`);
|
|
886
|
+
await createSession(key, "", "alfe");
|
|
887
|
+
}, { priority: 50 });
|
|
888
|
+
api.on("message_received", async (...eventArgs) => {
|
|
889
|
+
if (!pluginRuntime) return;
|
|
890
|
+
const event = eventArgs[0];
|
|
891
|
+
const ctx = eventArgs[1];
|
|
892
|
+
if (!ctx.conversationId || ctx.channelId === "alfe") return;
|
|
893
|
+
await addMessage(ctx.conversationId, "user", event.content, event.from);
|
|
894
|
+
});
|
|
895
|
+
api.on("session_end", (...eventArgs) => {
|
|
896
|
+
if (!pluginRuntime) return;
|
|
897
|
+
const key = eventArgs[0].sessionKey;
|
|
898
|
+
if (!key || !isAlfeSessionKey(key)) return;
|
|
899
|
+
log.info(`Chat session ending: ${key}`);
|
|
900
|
+
});
|
|
901
|
+
};
|
|
902
|
+
const stopChatService = async () => {
|
|
903
|
+
globalThis.__alfeChatPluginActivated = false;
|
|
904
|
+
if (connectingPromise) {
|
|
905
|
+
await connectingPromise.catch((err) => {
|
|
906
|
+
log.debug(`Connection attempt failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
907
|
+
});
|
|
908
|
+
connectingPromise = null;
|
|
871
909
|
}
|
|
872
|
-
|
|
910
|
+
if (chatClient) {
|
|
911
|
+
chatClient.stop();
|
|
912
|
+
chatClient = null;
|
|
913
|
+
log.info("Chat service client stopped");
|
|
914
|
+
}
|
|
915
|
+
pluginRuntime = null;
|
|
916
|
+
dispatchInbound = null;
|
|
917
|
+
metricsClient = null;
|
|
918
|
+
log.info("Chat plugin deactivated");
|
|
919
|
+
};
|
|
873
920
|
if (typeof api.registerGatewayMethod === "function") {
|
|
874
921
|
api.registerGatewayMethod("sessions.list", async (...args) => {
|
|
875
922
|
const params = args[0];
|
|
@@ -905,35 +952,16 @@ const plugin = {
|
|
|
905
952
|
});
|
|
906
953
|
log.info("Registered gateway RPC methods: sessions.list, sessions.get");
|
|
907
954
|
}
|
|
908
|
-
if (
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
955
|
+
if (api.registerService) api.registerService({
|
|
956
|
+
id: "alfe-chat-relay",
|
|
957
|
+
start: () => {
|
|
958
|
+
startChatService();
|
|
959
|
+
},
|
|
960
|
+
stop: async () => {
|
|
961
|
+
await stopChatService();
|
|
913
962
|
}
|
|
914
|
-
})
|
|
915
|
-
if (!alreadyActivated)
|
|
916
|
-
api.on("session_start", async (...eventArgs) => {
|
|
917
|
-
if (!pluginRuntime) return;
|
|
918
|
-
const key = eventArgs[0].sessionKey;
|
|
919
|
-
if (!key || isAlfeSessionKey(key)) return;
|
|
920
|
-
log.info(`Chat session starting: ${key}`);
|
|
921
|
-
await createSession(key, "", "alfe");
|
|
922
|
-
}, { priority: 50 });
|
|
923
|
-
api.on("message_received", async (...eventArgs) => {
|
|
924
|
-
if (!pluginRuntime) return;
|
|
925
|
-
const event = eventArgs[0];
|
|
926
|
-
const ctx = eventArgs[1];
|
|
927
|
-
if (!ctx.conversationId || ctx.channelId === "alfe") return;
|
|
928
|
-
await addMessage(ctx.conversationId, "user", event.content, event.from);
|
|
929
|
-
});
|
|
930
|
-
api.on("session_end", (...eventArgs) => {
|
|
931
|
-
if (!pluginRuntime) return;
|
|
932
|
-
const key = eventArgs[0].sessionKey;
|
|
933
|
-
if (!key || !isAlfeSessionKey(key)) return;
|
|
934
|
-
log.info(`Chat session ending: ${key}`);
|
|
935
|
-
});
|
|
936
|
-
}
|
|
963
|
+
});
|
|
964
|
+
else if (!alreadyActivated) startChatService();
|
|
937
965
|
log.info("Chat plugin registered");
|
|
938
966
|
},
|
|
939
967
|
async deactivate(api) {
|
package/openclaw.plugin.json
CHANGED