ddchat 0.4.3 → 0.4.4

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 CHANGED
@@ -13,10 +13,19 @@ openclaw plugins uninstall ddchat # 卸载插件
13
13
 
14
14
  # 默认default账号
15
15
  ```shell
16
- openclaw channels add --channel ddchat --token "appId:appSecret"
17
- ```
18
-
19
- # 多个账号时指定账户名 避免覆盖
20
- ```shell
21
- openclaw channels add --channel ddchat --account xxx --token "appId:appSecret"
16
+ openclaw channels add --channel ddchat --token "appId:appSecret"
17
+ ```
18
+
19
+ # 登录命令
20
+ ```shell
21
+ openclaw channels login --channel ddchat
22
+
23
+ # 非交互环境可以通过环境变量传入 token。
24
+ # PowerShell:
25
+ $env:OPENCLAW_DDCHAT_TOKEN="appId:appSecret"; openclaw channels login --channel ddchat
26
+ ```
27
+
28
+ # 多个账号时指定账户名 避免覆盖
29
+ ```shell
30
+ openclaw channels add --channel ddchat --account xxx --token "appId:appSecret"
22
31
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ddchat",
3
- "version": "0.4.3",
3
+ "version": "0.4.4",
4
4
  "description": "DDChat channel plugin for OpenClaw",
5
5
  "type": "module",
6
6
  "main": "./index.js",
package/src/channel.js CHANGED
@@ -1,4 +1,6 @@
1
+ import { createInterface } from "node:readline/promises";
1
2
  import { createChatChannelPlugin } from "openclaw/plugin-sdk/core";
3
+ import { mutateConfigFile } from "openclaw/plugin-sdk/config-mutation";
2
4
  import { patchScopedAccountConfig, prepareScopedSetupConfig } from "openclaw/plugin-sdk/setup";
3
5
  import { DDCHAT_CHANNEL_ID } from "./constants.js";
4
6
  import { ddchatGateway } from "./gateway.js";
@@ -18,6 +20,31 @@ function inspectDdchatAccount(cfg, accountId) {
18
20
  streamingMode: account.streamingMode,
19
21
  };
20
22
  }
23
+ function replaceConfigContents(target, next) {
24
+ for (const key of Object.keys(target)) {
25
+ delete target[key];
26
+ }
27
+ Object.assign(target, next);
28
+ }
29
+ async function promptDdchatToken() {
30
+ const envToken = process.env.OPENCLAW_DDCHAT_TOKEN?.trim() || process.env.DDCHAT_TOKEN?.trim();
31
+ if (envToken) {
32
+ return envToken;
33
+ }
34
+ if (!process.stdin.isTTY) {
35
+ throw new Error("ddchat login requires OPENCLAW_DDCHAT_TOKEN, DDCHAT_TOKEN, or an interactive TTY");
36
+ }
37
+ const rl = createInterface({
38
+ input: process.stdin,
39
+ output: process.stderr,
40
+ });
41
+ try {
42
+ return (await rl.question("DDChat token (appId:appSecret): ")).trim();
43
+ }
44
+ finally {
45
+ rl.close();
46
+ }
47
+ }
21
48
  export const ddchatPlugin = createChatChannelPlugin({
22
49
  base: {
23
50
  id: DDCHAT_CHANNEL_ID,
@@ -81,6 +108,31 @@ export const ddchatPlugin = createChatChannelPlugin({
81
108
  },
82
109
  },
83
110
  gateway: ddchatGateway,
111
+ auth: {
112
+ login: async ({ cfg, accountId, runtime }) => {
113
+ const resolvedAccountId = accountId?.trim() || "default";
114
+ const token = await promptDdchatToken();
115
+ if (!token) {
116
+ throw new Error("ddchat login requires a non-empty token");
117
+ }
118
+ await mutateConfigFile({
119
+ base: "source",
120
+ afterWrite: { mode: "restart", reason: "ddchat login updated channel credentials" },
121
+ mutate: (draft) => {
122
+ const next = ddchatPlugin.setup?.applyAccountConfig({
123
+ cfg: draft,
124
+ accountId: resolvedAccountId,
125
+ input: { token },
126
+ });
127
+ if (!next) {
128
+ throw new Error("ddchat setup adapter is unavailable");
129
+ }
130
+ replaceConfigContents(draft, next);
131
+ },
132
+ });
133
+ runtime.log(`DDChat credentials saved for account ${resolvedAccountId}.`);
134
+ },
135
+ },
84
136
  },
85
137
  pairing: ddchatPairing,
86
138
  security: {