clawsocial-plugin 1.4.0 → 1.6.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/src/ws-client.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import WebSocket from "ws";
2
2
  import { getState, upsertSession, getSession, addMessage, markRead, getSettings } from "./store.js";
3
3
  import { pushNotification } from "./notify.js";
4
+ import { t } from "./i18n.js";
4
5
 
5
6
  let ws: WebSocket | null = null;
6
7
  let reconnectTimer: ReturnType<typeof setTimeout> | null = null;
@@ -22,7 +23,7 @@ function maybePush(detailText: string): void {
22
23
  const mode = getSettings().notifyMode;
23
24
  if (mode === "silent") return;
24
25
  if (mode === "minimal") {
25
- pushNotification("[ClawSocial] 你有新消息,输入 /inbox 查看或打开收件箱。");
26
+ pushNotification(t("ws_new_msg_notify"));
26
27
  return;
27
28
  }
28
29
  pushNotification(detailText);
@@ -31,11 +32,11 @@ function maybePush(detailText: string): void {
31
32
  function handleServerMessage(msg: Record<string, unknown>): void {
32
33
  switch (msg.type) {
33
34
  case "auth_ok":
34
- log(`认证成功: ${msg.agent_id}`);
35
+ log(`${t("ws_auth_ok")}: ${msg.agent_id}`);
35
36
  break;
36
37
 
37
38
  case "auth_error":
38
- console.error(`[ClawSocial WS] 认证失败: ${msg.error}`);
39
+ console.error(`[ClawSocial WS] ${t("ws_auth_fail")}: ${msg.error}`);
39
40
  break;
40
41
 
41
42
  case "ping":
@@ -44,36 +45,32 @@ function handleServerMessage(msg: Record<string, unknown>): void {
44
45
 
45
46
  case "connect_request": {
46
47
  const sid = msg.session_id as string;
48
+ const name = msg.from_agent_name as string;
47
49
  upsertSession(sid, {
48
50
  status: "pending",
49
51
  is_receiver: true,
50
52
  partner_agent_id: msg.from_agent_id as string,
51
- partner_name: msg.from_agent_name as string,
53
+ partner_name: name,
52
54
  intro_message: (msg.intro_message as string) || "",
53
55
  messages: [],
54
56
  unread: 0,
55
57
  created_at: Math.floor(Date.now() / 1000),
56
58
  });
57
- log(
58
- `收到连接请求!来自:${msg.from_agent_name}${shortId(msg.from_agent_id as string)}。请调用 clawsocial_open_inbox 查看收件箱。`,
59
- );
60
- maybePush(
61
- `[ClawSocial] 收到来自 ${msg.from_agent_name} 的连接请求。可调用 clawsocial_open_inbox 查看。`,
62
- );
59
+ log(t("ws_connect_req", { name: `${name}${shortId(msg.from_agent_id as string)}` }));
60
+ maybePush(t("ws_connect_req_notify", { name }));
63
61
  break;
64
62
  }
65
63
 
66
64
  case "session_started": {
67
65
  const sid = msg.session_id as string;
66
+ const name = msg.with_agent_name as string;
68
67
  upsertSession(sid, {
69
68
  status: "active",
70
69
  partner_agent_id: msg.with_agent_id as string,
71
- partner_name: msg.with_agent_name as string,
70
+ partner_name: name,
72
71
  });
73
- log(`${msg.with_agent_name}${shortId(msg.with_agent_id as string)} 接受了连接请求,会话 ID:${sid}`);
74
- maybePush(
75
- `[ClawSocial] ${msg.with_agent_name} 开始了与你的会话。可调用 clawsocial_session_get 查看消息。`,
76
- );
72
+ log(t("ws_session_accepted", { name: `${name}${shortId(msg.with_agent_id as string)}`, id: sid }));
73
+ maybePush(t("ws_session_notify", { name }));
77
74
  break;
78
75
  }
79
76
 
@@ -102,12 +99,8 @@ function handleServerMessage(msg: Record<string, unknown>): void {
102
99
  intent: msg.intent as string | undefined,
103
100
  created_at: (msg.created_at as number) || Math.floor(Date.now() / 1000),
104
101
  });
105
- log(
106
- `来自 ${partnerName}${shortId(msg.from_agent as string)}:${(msg.content as string).slice(0, 60)}`,
107
- );
108
- maybePush(
109
- `[ClawSocial] 收到 ${partnerName} 的新消息:${(msg.content as string).slice(0, 80)}`,
110
- );
102
+ log(t("ws_msg_log", { name: `${partnerName}${shortId(msg.from_agent as string)}`, preview: (msg.content as string).slice(0, 60) }));
103
+ maybePush(t("ws_msg_notify", { name: partnerName, preview: (msg.content as string).slice(0, 80) }));
111
104
  break;
112
105
  }
113
106
 
@@ -130,7 +123,7 @@ export function startWsClient(serverUrl: string): void {
130
123
  function connect(): void {
131
124
  const state = getState();
132
125
  if (!state.agent_id || !state.api_key) {
133
- log("尚未注册,跳过 WS 连接");
126
+ log(t("ws_not_registered"));
134
127
  return;
135
128
  }
136
129
 
@@ -140,7 +133,7 @@ function connect(): void {
140
133
 
141
134
  ws.on("open", () => {
142
135
  const s = getState();
143
- log("已连接服务器");
136
+ log(t("ws_connected"));
144
137
  ws!.send(JSON.stringify({ type: "auth", agent_id: s.agent_id, api_key: s.api_key }));
145
138
  });
146
139
 
@@ -155,13 +148,13 @@ function connect(): void {
155
148
  });
156
149
 
157
150
  ws.on("close", (code: number) => {
158
- log(`连接断开 (${code}),5s 后重连`);
151
+ log(`${t("ws_disconnected")} (${code}), ${t("ws_reconnect")}`);
159
152
  ws = null;
160
153
  reconnectTimer = setTimeout(connect, 5000);
161
154
  });
162
155
 
163
156
  ws.on("error", (err: Error) => {
164
- console.error("[ClawSocial WS] 错误:", err.message);
157
+ console.error("[ClawSocial WS] Error:", err.message);
165
158
  });
166
159
  }
167
160