wechat-clawbot 1.0.1 → 1.2.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 +19 -0
- package/README.zh.md +19 -0
- package/lib/backlog.d.ts +7 -3
- package/lib/backlog.d.ts.map +1 -1
- package/lib/backlog.js +4 -2
- package/lib/backlog.js.map +1 -1
- package/lib/bridge.d.ts +23 -0
- package/lib/bridge.d.ts.map +1 -1
- package/lib/bridge.js +135 -2
- package/lib/bridge.js.map +1 -1
- package/lib/claude-peer.d.ts +30 -0
- package/lib/claude-peer.d.ts.map +1 -1
- package/lib/claude-peer.js +292 -28
- package/lib/claude-peer.js.map +1 -1
- package/lib/client.js +165 -1
- package/lib/codex-rpc.js +1 -1
- package/lib/config.d.ts +31 -0
- package/lib/config.d.ts.map +1 -1
- package/lib/config.js +41 -0
- package/lib/config.js.map +1 -1
- package/lib/dsh-sessions.d.ts +124 -0
- package/lib/dsh-sessions.d.ts.map +1 -0
- package/lib/dsh-sessions.js +344 -0
- package/lib/dsh-sessions.js.map +1 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +78 -0
- package/lib/index.js.map +1 -1
- package/lib/instance-guard.d.ts.map +1 -1
- package/lib/instance-guard.js +32 -7
- package/lib/instance-guard.js.map +1 -1
- package/lib/mac-calendar.d.ts +124 -0
- package/lib/mac-calendar.d.ts.map +1 -0
- package/lib/mac-calendar.js +883 -0
- package/lib/mac-calendar.js.map +1 -0
- package/lib/mcp-route.d.ts +1 -1
- package/lib/mcp-route.d.ts.map +1 -1
- package/lib/mcp-route.js +48 -189
- package/lib/mcp-route.js.map +1 -1
- package/lib/mcp-server.js +1 -1
- package/lib/memory.d.ts.map +1 -1
- package/lib/memory.js +12 -1
- package/lib/memory.js.map +1 -1
- package/lib/owner-notify.d.ts +90 -0
- package/lib/owner-notify.d.ts.map +1 -0
- package/lib/owner-notify.js +200 -0
- package/lib/owner-notify.js.map +1 -0
- package/lib/secretary.d.ts +27 -0
- package/lib/secretary.d.ts.map +1 -0
- package/lib/secretary.js +175 -0
- package/lib/secretary.js.map +1 -0
- package/native/macos-calendar/main.swift +543 -0
- package/package.json +2 -1
- package/wechat-clawbot-INSTALL.md +83 -6
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One WeChat message to the owner, from something other than the WeChat
|
|
3
|
+
* conversation: Claude Code or Codex through the MCP routes (mcp-route.ts), or
|
|
4
|
+
* any DSH conversation through the `notify_wechat` tool registered here.
|
|
5
|
+
*
|
|
6
|
+
* ## The recipient is not a parameter
|
|
7
|
+
*
|
|
8
|
+
* It goes to `account.userId`, the id linked by QR login, and nothing takes a
|
|
9
|
+
* `to`. That makes "message someone who isn't the owner" unrepresentable
|
|
10
|
+
* rather than merely discouraged.
|
|
11
|
+
*
|
|
12
|
+
* ## `notify_wechat` is global
|
|
13
|
+
*
|
|
14
|
+
* The tool is registered on the plugin's root context, so every DSH
|
|
15
|
+
* conversation gets it, in every working directory, with no per-folder setup.
|
|
16
|
+
* Only the WeChat conversation itself is left out: it already talks to the
|
|
17
|
+
* owner with send_wechat_text, and a second way would double-send.
|
|
18
|
+
*/
|
|
19
|
+
import type { Context } from "@deepseek-ai/cordis";
|
|
20
|
+
import type { ResolvedWeixinAccount } from "./ilink/auth/accounts.js";
|
|
21
|
+
import { type Backlog, type NoticeSource } from "./backlog.js";
|
|
22
|
+
export type OwnerNotifyDeps = {
|
|
23
|
+
/** Which session gets the mirrored copy of an outbound notify. */
|
|
24
|
+
config: {
|
|
25
|
+
sessionId: string;
|
|
26
|
+
};
|
|
27
|
+
/** Send one plain-text WeChat message (applies the emoji policy). */
|
|
28
|
+
sendText: (to: string, text: string) => Promise<void>;
|
|
29
|
+
/** The currently bound account, or null before `clawbot login`. */
|
|
30
|
+
getAccount: () => ResolvedWeixinAccount | null;
|
|
31
|
+
/** Where a notification waits when WeChat will not let the bot write first (see backlog.ts). */
|
|
32
|
+
backlog?: Backlog;
|
|
33
|
+
};
|
|
34
|
+
/** The name every DSH conversation sees. */
|
|
35
|
+
export declare const NOTIFY_TOOL = "notify_wechat";
|
|
36
|
+
/**
|
|
37
|
+
* Put a copy of an outbound notify into the WeChat session's history.
|
|
38
|
+
*
|
|
39
|
+
* Without this the bot is blind to whatever another agent told the user: reply "那怎么办"
|
|
40
|
+
* to one of these messages and the bot has no idea what "那" refers to.
|
|
41
|
+
*
|
|
42
|
+
* `session.append`, not `followup`: an inbox item starts a turn, and the bot
|
|
43
|
+
* would answer a message that was already sent. `user/message`, not
|
|
44
|
+
* `assistant/message`: the latter carries turn/step bookkeeping that cannot be
|
|
45
|
+
* invented safely. The prefix is what makes it legible, and prompt rule C.6
|
|
46
|
+
* tells the bot what to do with one: already delivered, do not repeat.
|
|
47
|
+
*
|
|
48
|
+
* Best effort: the WeChat message is already out, so a mirroring failure must
|
|
49
|
+
* never turn a delivered notify into a reported failure.
|
|
50
|
+
*/
|
|
51
|
+
export declare function mirrorIntoSession(ctx: Context, sessionId: string, text: string, sender: string, note?: string): boolean;
|
|
52
|
+
export type NotifyOutcome = {
|
|
53
|
+
ok: true;
|
|
54
|
+
to: string;
|
|
55
|
+
chars: number;
|
|
56
|
+
mirrored: boolean;
|
|
57
|
+
deferred?: false;
|
|
58
|
+
} | {
|
|
59
|
+
ok: true;
|
|
60
|
+
to: string;
|
|
61
|
+
chars: number;
|
|
62
|
+
mirrored: boolean;
|
|
63
|
+
deferred: true;
|
|
64
|
+
summary: string;
|
|
65
|
+
message: string;
|
|
66
|
+
} | {
|
|
67
|
+
ok: false;
|
|
68
|
+
status: number;
|
|
69
|
+
message: string;
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* Send `text` to the owner. When WeChat will not let the bot write first, keep
|
|
73
|
+
* one sentence for the digest after the owner's next message instead.
|
|
74
|
+
*
|
|
75
|
+
* @param source - who is talking, for the digest ("Claude 的通知").
|
|
76
|
+
* @param origin - which conversation, when there is one worth naming.
|
|
77
|
+
*/
|
|
78
|
+
export declare function notifyOwner(ctx: Context, deps: OwnerNotifyDeps, input: {
|
|
79
|
+
text: string;
|
|
80
|
+
summary?: string;
|
|
81
|
+
source: NoticeSource;
|
|
82
|
+
origin?: string;
|
|
83
|
+
}): Promise<NotifyOutcome>;
|
|
84
|
+
/**
|
|
85
|
+
* Register `notify_wechat` for every DSH conversation.
|
|
86
|
+
*
|
|
87
|
+
* @param ctx - the plugin's ROOT context: a tool registered there is global.
|
|
88
|
+
*/
|
|
89
|
+
export declare function registerOwnerNotifyTool(ctx: Context, deps: OwnerNotifyDeps): void;
|
|
90
|
+
//# sourceMappingURL=owner-notify.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"owner-notify.d.ts","sourceRoot":"","sources":["../src/owner-notify.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AAKnD,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,0BAA0B,CAAC;AAEtE,OAAO,EAAmC,KAAK,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,cAAc,CAAC;AAGhG,MAAM,MAAM,eAAe,GAAG;IAC5B,kEAAkE;IAClE,MAAM,EAAE;QAAE,SAAS,EAAE,MAAM,CAAA;KAAE,CAAC;IAC9B,qEAAqE;IACrE,QAAQ,EAAE,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IACtD,mEAAmE;IACnE,UAAU,EAAE,MAAM,qBAAqB,GAAG,IAAI,CAAC;IAC/C,gGAAgG;IAChG,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB,CAAC;AAEF,4CAA4C;AAC5C,eAAO,MAAM,WAAW,kBAAkB,CAAC;AAE3C;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,iBAAiB,CAC/B,GAAG,EAAE,OAAO,EACZ,SAAS,EAAE,MAAM,EACjB,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,MAAM,EACd,IAAI,CAAC,EAAE,MAAM,GACZ,OAAO,CAkBT;AAED,MAAM,MAAM,aAAa,GACrB;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,EAAE,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,OAAO,CAAC;IAAC,QAAQ,CAAC,EAAE,KAAK,CAAA;CAAE,GAC5E;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,EAAE,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,OAAO,CAAC;IAAC,QAAQ,EAAE,IAAI,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,GAC5G;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC;AAEnD;;;;;;GAMG;AACH,wBAAsB,WAAW,CAC/B,GAAG,EAAE,OAAO,EACZ,IAAI,EAAE,eAAe,EACrB,KAAK,EAAE;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,YAAY,CAAC;IAAC,MAAM,CAAC,EAAE,MAAM,CAAA;CAAE,GAC/E,OAAO,CAAC,aAAa,CAAC,CAqCxB;AA0BD;;;;GAIG;AACH,wBAAgB,uBAAuB,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,eAAe,GAAG,IAAI,CAgFjF"}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { createUserMessage } from "@deepseek-ai/dsh-llm";
|
|
2
|
+
import { SessionId } from "@deepseek-ai/dsh-session";
|
|
3
|
+
import { defineTool } from "@deepseek-ai/dsh-tools";
|
|
4
|
+
import { logger } from "./ilink/util/logger.js";
|
|
5
|
+
import { isSendWindowClosed, oneSentence } from "./backlog.js";
|
|
6
|
+
import { listDshSessions } from "./dsh-sessions.js";
|
|
7
|
+
/** The name every DSH conversation sees. */
|
|
8
|
+
export const NOTIFY_TOOL = "notify_wechat";
|
|
9
|
+
/**
|
|
10
|
+
* Put a copy of an outbound notify into the WeChat session's history.
|
|
11
|
+
*
|
|
12
|
+
* Without this the bot is blind to whatever another agent told the user: reply "那怎么办"
|
|
13
|
+
* to one of these messages and the bot has no idea what "那" refers to.
|
|
14
|
+
*
|
|
15
|
+
* `session.append`, not `followup`: an inbox item starts a turn, and the bot
|
|
16
|
+
* would answer a message that was already sent. `user/message`, not
|
|
17
|
+
* `assistant/message`: the latter carries turn/step bookkeeping that cannot be
|
|
18
|
+
* invented safely. The prefix is what makes it legible, and prompt rule C.6
|
|
19
|
+
* tells the bot what to do with one: already delivered, do not repeat.
|
|
20
|
+
*
|
|
21
|
+
* Best effort: the WeChat message is already out, so a mirroring failure must
|
|
22
|
+
* never turn a delivered notify into a reported failure.
|
|
23
|
+
*/
|
|
24
|
+
export function mirrorIntoSession(ctx, sessionId, text, sender, note) {
|
|
25
|
+
try {
|
|
26
|
+
const agent = ctx.agents.get(SessionId(sessionId));
|
|
27
|
+
if (agent === undefined)
|
|
28
|
+
return false;
|
|
29
|
+
const message = createUserMessage({
|
|
30
|
+
// "Claude 发给用户的", but "DSH 会话「X」发给用户的": no space after a closing bracket.
|
|
31
|
+
content: [{ type: "text", text: `[${sender}${sender.endsWith("」") ? "" : " "}发给用户的${note === undefined ? "" : `,${note}`}] ${text}` }],
|
|
32
|
+
// v4 refuses the retired `plugin` wrapper; this is the kind the v3→v4
|
|
33
|
+
// converter assigns this producer, so migrated history matches.
|
|
34
|
+
source: { kind: "plugin:clawbot-mcp" },
|
|
35
|
+
});
|
|
36
|
+
agent.session.append("user/message", message, { surfaceOp: "append" });
|
|
37
|
+
logger.info(`notify: mirrored ${text.length} chars into ${sessionId}`);
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
catch (err) {
|
|
41
|
+
logger.warn(`notify: mirror failed (message was still delivered): ${String(err).slice(0, 160)}`);
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Send `text` to the owner. When WeChat will not let the bot write first, keep
|
|
47
|
+
* one sentence for the digest after the owner's next message instead.
|
|
48
|
+
*
|
|
49
|
+
* @param source - who is talking, for the digest ("Claude 的通知").
|
|
50
|
+
* @param origin - which conversation, when there is one worth naming.
|
|
51
|
+
*/
|
|
52
|
+
export async function notifyOwner(ctx, deps, input) {
|
|
53
|
+
const account = deps.getAccount();
|
|
54
|
+
if (account === null || !account.configured) {
|
|
55
|
+
return { ok: false, status: 503, message: "no bound WeChat account — run `clawbot login` first" };
|
|
56
|
+
}
|
|
57
|
+
const owner = account.userId;
|
|
58
|
+
if (owner === undefined || owner === "") {
|
|
59
|
+
return { ok: false, status: 503, message: "the bound account has no linked userId; re-run the QR login so it is recorded" };
|
|
60
|
+
}
|
|
61
|
+
const { text, source, origin } = input;
|
|
62
|
+
const sender = origin === undefined ? source : `${source} 会话「${origin}」`;
|
|
63
|
+
try {
|
|
64
|
+
await deps.sendText(owner, text);
|
|
65
|
+
}
|
|
66
|
+
catch (err) {
|
|
67
|
+
if (!isSendWindowClosed(err) || deps.backlog === undefined)
|
|
68
|
+
throw err;
|
|
69
|
+
const given = (input.summary ?? "").trim();
|
|
70
|
+
const summary = given !== "" ? given.slice(0, 120) : oneSentence(text);
|
|
71
|
+
deps.backlog.add(owner, {
|
|
72
|
+
kind: "notice", at: new Date().toISOString(), source, summary,
|
|
73
|
+
...(origin === undefined ? {} : { origin }),
|
|
74
|
+
});
|
|
75
|
+
logger.info(`notify: WeChat will not let the bot write first now; kept "${summary}" for the owner's next message`);
|
|
76
|
+
const mirrored = mirrorIntoSession(ctx, deps.config.sessionId, text, sender, "暂未送达,对方下次发消息后合并补发");
|
|
77
|
+
return {
|
|
78
|
+
ok: true,
|
|
79
|
+
deferred: true,
|
|
80
|
+
to: owner,
|
|
81
|
+
chars: text.length,
|
|
82
|
+
summary,
|
|
83
|
+
mirrored,
|
|
84
|
+
message: "微信现在不让 bot 主动发消息(机主有一段时间没发消息了)。这条已存下,机主下次发消息、bot 回复之后,"
|
|
85
|
+
+ "会和其他没送达的消息按时间合并成一条补发,这条只保留一句:" + summary,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
logger.info(`notify: sent ${text.length} chars to the owner (${sender})`);
|
|
89
|
+
const mirrored = mirrorIntoSession(ctx, deps.config.sessionId, text, sender);
|
|
90
|
+
return { ok: true, to: owner, chars: text.length, mirrored };
|
|
91
|
+
}
|
|
92
|
+
/** A conversation's title, else its folder, else its short id: how the owner will recognise it. */
|
|
93
|
+
async function originOf(ctx, sessionId) {
|
|
94
|
+
try {
|
|
95
|
+
const { rows } = await listDshSessions(ctx);
|
|
96
|
+
const row = rows.find((r) => r.id === sessionId);
|
|
97
|
+
if (row?.title !== undefined)
|
|
98
|
+
return row.title;
|
|
99
|
+
const folder = row?.cwd?.split(/[\\/]/).filter((s) => s !== "").pop();
|
|
100
|
+
if (folder !== undefined)
|
|
101
|
+
return folder;
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
// fall through to the id
|
|
105
|
+
}
|
|
106
|
+
return sessionId.slice(0, 8);
|
|
107
|
+
}
|
|
108
|
+
const SLOT = Symbol.for("clawbot.notify-wechat");
|
|
109
|
+
const slots = globalThis;
|
|
110
|
+
/**
|
|
111
|
+
* Register `notify_wechat` for every DSH conversation.
|
|
112
|
+
*
|
|
113
|
+
* @param ctx - the plugin's ROOT context: a tool registered there is global.
|
|
114
|
+
*/
|
|
115
|
+
export function registerOwnerNotifyTool(ctx, deps) {
|
|
116
|
+
const existing = slots[SLOT];
|
|
117
|
+
if (existing?.registered === true) {
|
|
118
|
+
existing.ctx = ctx;
|
|
119
|
+
existing.deps = deps;
|
|
120
|
+
logger.info(`${NOTIFY_TOOL}: already registered in this process; now served by the newest instance`);
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
const slot = { ctx, deps, registered: false };
|
|
124
|
+
slots[SLOT] = slot;
|
|
125
|
+
const serving = () => slots[SLOT] ?? slot;
|
|
126
|
+
const tool = (defineTool({
|
|
127
|
+
name: NOTIFY_TOOL,
|
|
128
|
+
description: "Send a WeChat message to the owner of this computer (the person who linked the WeChat bot). "
|
|
129
|
+
+ "Use it when the owner asked to be told on WeChat, when a long task finishes or fails, or when it "
|
|
130
|
+
+ "needs a decision while they are away. Keep it short and self-contained: they read it on their phone "
|
|
131
|
+
+ "without this conversation in front of them, so say which task it is about. Do not use it for "
|
|
132
|
+
+ "ordinary replies — they are shown here. If WeChat will not let the bot write first right now "
|
|
133
|
+
+ "(the owner has been silent for about a day), the message is kept and delivered after their next "
|
|
134
|
+
+ "WeChat message; the result says so.",
|
|
135
|
+
parameters: {
|
|
136
|
+
text: { type: "string", required: true, description: "The message, as the owner should read it." },
|
|
137
|
+
summary: {
|
|
138
|
+
type: "string",
|
|
139
|
+
description: "Optional one-sentence version, used if the message has to wait and is folded into a digest.",
|
|
140
|
+
},
|
|
141
|
+
},
|
|
142
|
+
output: {
|
|
143
|
+
schema: {
|
|
144
|
+
type: "object",
|
|
145
|
+
additionalProperties: false,
|
|
146
|
+
properties: {
|
|
147
|
+
ok: { type: "boolean", required: true },
|
|
148
|
+
summary: { type: "string", required: true },
|
|
149
|
+
},
|
|
150
|
+
},
|
|
151
|
+
render: (_args, value) => [{ type: "text", text: value.summary }],
|
|
152
|
+
},
|
|
153
|
+
async execute(args, exec) {
|
|
154
|
+
const { ctx, deps } = serving();
|
|
155
|
+
const { text, summary } = args;
|
|
156
|
+
const body = (text ?? "").trim();
|
|
157
|
+
if (body === "")
|
|
158
|
+
return { ok: false, summary: "消息是空的,没有发送" };
|
|
159
|
+
const caller = String(exec?.agent?.session?.id ?? "");
|
|
160
|
+
if (caller !== "" && caller === deps.config.sessionId) {
|
|
161
|
+
return { ok: false, summary: "这是微信会话本身:直接用 send_wechat_text 回复。" };
|
|
162
|
+
}
|
|
163
|
+
const origin = caller === "" ? undefined : await originOf(ctx, caller);
|
|
164
|
+
try {
|
|
165
|
+
const outcome = await notifyOwner(ctx, deps, {
|
|
166
|
+
text: body,
|
|
167
|
+
...(typeof summary === "string" ? { summary } : {}),
|
|
168
|
+
source: "DSH",
|
|
169
|
+
...(origin === undefined ? {} : { origin }),
|
|
170
|
+
});
|
|
171
|
+
if (!outcome.ok)
|
|
172
|
+
return { ok: false, summary: `没发出去:${outcome.message}` };
|
|
173
|
+
if (outcome.deferred === true)
|
|
174
|
+
return { ok: true, summary: outcome.message };
|
|
175
|
+
return { ok: true, summary: `已发到机主微信(${outcome.chars} 字)。` };
|
|
176
|
+
}
|
|
177
|
+
catch (err) {
|
|
178
|
+
logger.warn(`${NOTIFY_TOOL}: ${String(err).slice(0, 200)}`);
|
|
179
|
+
return { ok: false, summary: `没发出去:${String(err).slice(0, 200)}` };
|
|
180
|
+
}
|
|
181
|
+
},
|
|
182
|
+
}));
|
|
183
|
+
const register = () => {
|
|
184
|
+
const off = ctx.tools.register(tool);
|
|
185
|
+
slot.registered = true;
|
|
186
|
+
return () => {
|
|
187
|
+
off();
|
|
188
|
+
// If the host ever does dispose this instance, the next one registers again.
|
|
189
|
+
if (slots[SLOT] === slot)
|
|
190
|
+
slot.registered = false;
|
|
191
|
+
};
|
|
192
|
+
};
|
|
193
|
+
const scoped = ctx;
|
|
194
|
+
if (typeof scoped.effect === "function")
|
|
195
|
+
scoped.effect(register, `clawbot: ${NOTIFY_TOOL}`);
|
|
196
|
+
else
|
|
197
|
+
register();
|
|
198
|
+
logger.info(`${NOTIFY_TOOL}: registered for every DSH conversation except ${deps.config.sessionId}`);
|
|
199
|
+
}
|
|
200
|
+
//# sourceMappingURL=owner-notify.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"owner-notify.js","sourceRoot":"","sources":["../src/owner-notify.ts"],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export declare const SECRETARY_HEADING = "## \u65E5\u7A0B\u79D8\u4E66";
|
|
4
|
+
/** The default rules (written with the first owner who used them). */
|
|
5
|
+
export declare const SECRETARY_RULES: string;
|
|
6
|
+
export declare function hasSecretaryRules(): boolean;
|
|
7
|
+
/** Put the rules at the end of memory.md: the owner's own last version if there is one. */
|
|
8
|
+
export declare function addSecretaryRules(): "added" | "present";
|
|
9
|
+
/** Take the section out of memory.md, keeping it for the next time the switch goes on. */
|
|
10
|
+
export declare function removeSecretaryRules(): "removed" | "absent";
|
|
11
|
+
export declare const MEMORY_OPEN_PATH = "/plugins/clawbot/memory/open";
|
|
12
|
+
/** Opens a file with the desktop's own app. */
|
|
13
|
+
export type Opener = (file: string) => Promise<void>;
|
|
14
|
+
/** macOS: the app set for .md files, else the text editor; Linux: xdg-open; Windows: start. */
|
|
15
|
+
export declare const openOnDesktop: Opener;
|
|
16
|
+
/**
|
|
17
|
+
* POST /plugins/clawbot/memory/open — the settings card's "打开 memory.md".
|
|
18
|
+
*
|
|
19
|
+
* Plugin routes are not behind DSH's login cookie, so this takes only what the
|
|
20
|
+
* card itself sends: a JSON POST from the DSH page. A page on another site
|
|
21
|
+
* cannot send that content type without a preflight this route never answers,
|
|
22
|
+
* and a browser names the page a request came from in Origin. It opens a file
|
|
23
|
+
* the bot already reads every turn; nothing is sent back but its path.
|
|
24
|
+
*/
|
|
25
|
+
export declare function handleMemoryOpen(req: IncomingMessage, res: ServerResponse, opener?: Opener): Promise<void>;
|
|
26
|
+
export declare function registerMemoryOpenRoute(ctx: Context): void;
|
|
27
|
+
//# sourceMappingURL=secretary.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"secretary.d.ts","sourceRoot":"","sources":["../src/secretary.ts"],"names":[],"mappings":"AAkBA,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAGjE,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AAMnD,eAAO,MAAM,iBAAiB,gCAAY,CAAC;AAI3C,sEAAsE;AACtE,eAAO,MAAM,eAAe,QAShB,CAAC;AAsCb,wBAAgB,iBAAiB,IAAI,OAAO,CAM3C;AAED,2FAA2F;AAC3F,wBAAgB,iBAAiB,IAAI,OAAO,GAAG,SAAS,CAcvD;AAED,0FAA0F;AAC1F,wBAAgB,oBAAoB,IAAI,SAAS,GAAG,QAAQ,CAW3D;AAID,eAAO,MAAM,gBAAgB,iCAAiC,CAAC;AAE/D,+CAA+C;AAC/C,MAAM,MAAM,MAAM,GAAG,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;AAErD,+FAA+F;AAC/F,eAAO,MAAM,aAAa,EAAE,MAYxB,CAAC;AAEL;;;;;;;;GAQG;AACH,wBAAsB,gBAAgB,CAAC,GAAG,EAAE,eAAe,EAAE,GAAG,EAAE,cAAc,EAAE,MAAM,GAAE,MAAsB,GAAG,OAAO,CAAC,IAAI,CAAC,CAsB/H;AAUD,wBAAgB,uBAAuB,CAAC,GAAG,EAAE,OAAO,GAAG,IAAI,CAQ1D"}
|
package/lib/secretary.js
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The scheduling secretary ("日程秘书"): a short set of planning rules the
|
|
3
|
+
* owner switches on in the settings card.
|
|
4
|
+
*
|
|
5
|
+
* The rules go into the long-term memory file (memory.md), not into this
|
|
6
|
+
* plugin's prompt. Like a persona they belong to one owner, and in the memory
|
|
7
|
+
* file the owner can read and rewrite them; the plugin's own prompt stays the
|
|
8
|
+
* same for everyone and byte-stable. memory.md is read into every WeChat turn
|
|
9
|
+
* whether or not auto-memory is on, so nothing else has to be enabled for the
|
|
10
|
+
* rules to apply — except the Mac calendar, which the rules lean on.
|
|
11
|
+
*
|
|
12
|
+
* Switching off moves the section out of memory.md into a copy beside it, and
|
|
13
|
+
* switching on again puts that copy back, so the owner's own edits survive a
|
|
14
|
+
* round trip. Only an explicit switch changes the file: a start-up with the
|
|
15
|
+
* switch off never deletes anything.
|
|
16
|
+
*/
|
|
17
|
+
import { execFile } from "node:child_process";
|
|
18
|
+
import fs from "node:fs";
|
|
19
|
+
import path from "node:path";
|
|
20
|
+
import { resolveStateDir } from "./ilink/storage/state-dir.js";
|
|
21
|
+
import { logger } from "./ilink/util/logger.js";
|
|
22
|
+
import { ensureMemoryFile, memoryFilePath } from "./memory.js";
|
|
23
|
+
export const SECRETARY_HEADING = "## 日程秘书";
|
|
24
|
+
/** The owner may retitle it ("## 日程秘书(我的版本)"); it is still the same section. */
|
|
25
|
+
const HEADING = /^##\s*日程秘书/;
|
|
26
|
+
/** The default rules (written with the first owner who used them). */
|
|
27
|
+
export const SECRETARY_RULES = [
|
|
28
|
+
SECRETARY_HEADING,
|
|
29
|
+
"- 用户问接下来做什么,或补充待办、截止时间,就直接排进日历再汇报,不反问;只说了哪天、没说几点的,看那天已有的日程找空档先排上;闲聊不硬排。",
|
|
30
|
+
"- 排之前先查日历,先保住课程、会议、硬截止,不凭记忆说有空。",
|
|
31
|
+
"- 一天一条主线、最多三个关键交付,其余写明延后;用户没明确选的叫“建议主线”。",
|
|
32
|
+
"- 深度工作 60–90 分钟一块,零碎事合并,只排满 70–80%,不默认熬夜;没有外部截止就标“暂排”。",
|
|
33
|
+
"- 只说「提醒我…」、到点叫一下就行的事(吃药、出门、打电话),只设微信提醒,不进日历;要占一段时间的事(写东西、开会、约饭、出差)才排进日历,并照样设微信提醒,和日历自带的提醒重复也没关系。",
|
|
34
|
+
"- 插单或进度落后时重算,直接说保留什么、挪走什么、今天能交付什么。",
|
|
35
|
+
"- 回复要短:用几行编号说清今天主要做什么、几点做什么、哪些挪到以后、哪里有冲突;用户只报了一件事的进度,就只说这件事带来的调整,不把整天安排再发一遍。",
|
|
36
|
+
].join("\n");
|
|
37
|
+
/** Where the section waits while the switch is off. */
|
|
38
|
+
function savedCopyPath() {
|
|
39
|
+
return path.join(resolveStateDir(), "secretary-rules.md");
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* The whole memory file. A missing file is empty; any other failure throws,
|
|
43
|
+
* because the callers write the file back and must never write over a memory
|
|
44
|
+
* they could not read.
|
|
45
|
+
*/
|
|
46
|
+
function readMemory() {
|
|
47
|
+
try {
|
|
48
|
+
return fs.readFileSync(memoryFilePath(), "utf-8");
|
|
49
|
+
}
|
|
50
|
+
catch (err) {
|
|
51
|
+
if (err.code === "ENOENT")
|
|
52
|
+
return "";
|
|
53
|
+
throw err;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
function writeMemory(text) {
|
|
57
|
+
const p = memoryFilePath();
|
|
58
|
+
fs.mkdirSync(path.dirname(p), { recursive: true });
|
|
59
|
+
const tmp = `${p}.tmp`;
|
|
60
|
+
fs.writeFileSync(tmp, text, "utf-8");
|
|
61
|
+
fs.renameSync(tmp, p);
|
|
62
|
+
}
|
|
63
|
+
/** The section's [start, end) line range: its heading up to the next heading. */
|
|
64
|
+
function sectionRange(lines) {
|
|
65
|
+
const start = lines.findIndex((line) => HEADING.test(line.trim()));
|
|
66
|
+
if (start < 0)
|
|
67
|
+
return null;
|
|
68
|
+
let end = start + 1;
|
|
69
|
+
while (end < lines.length && !/^#{1,2} /.test(lines[end]))
|
|
70
|
+
end += 1;
|
|
71
|
+
return [start, end];
|
|
72
|
+
}
|
|
73
|
+
export function hasSecretaryRules() {
|
|
74
|
+
try {
|
|
75
|
+
return sectionRange(readMemory().split("\n")) !== null;
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/** Put the rules at the end of memory.md: the owner's own last version if there is one. */
|
|
82
|
+
export function addSecretaryRules() {
|
|
83
|
+
ensureMemoryFile();
|
|
84
|
+
const text = readMemory();
|
|
85
|
+
if (sectionRange(text.split("\n")) !== null)
|
|
86
|
+
return "present";
|
|
87
|
+
let block = SECRETARY_RULES;
|
|
88
|
+
try {
|
|
89
|
+
const saved = fs.readFileSync(savedCopyPath(), "utf-8").trim();
|
|
90
|
+
if (HEADING.test(saved))
|
|
91
|
+
block = saved;
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
// never switched off before: the default rules
|
|
95
|
+
}
|
|
96
|
+
writeMemory(`${text.replace(/\s*$/, "")}\n\n${block}\n`);
|
|
97
|
+
logger.info("secretary: scheduling rules added to memory.md");
|
|
98
|
+
return "added";
|
|
99
|
+
}
|
|
100
|
+
/** Take the section out of memory.md, keeping it for the next time the switch goes on. */
|
|
101
|
+
export function removeSecretaryRules() {
|
|
102
|
+
const lines = readMemory().split("\n");
|
|
103
|
+
const range = sectionRange(lines);
|
|
104
|
+
if (range === null)
|
|
105
|
+
return "absent";
|
|
106
|
+
const [start, end] = range;
|
|
107
|
+
fs.mkdirSync(path.dirname(savedCopyPath()), { recursive: true });
|
|
108
|
+
fs.writeFileSync(savedCopyPath(), `${lines.slice(start, end).join("\n").trim()}\n`, { mode: 0o600 });
|
|
109
|
+
const rest = [...lines.slice(0, start), ...lines.slice(end)].join("\n").replace(/\n{3,}/g, "\n\n");
|
|
110
|
+
writeMemory(`${rest.replace(/\s*$/, "")}\n`);
|
|
111
|
+
logger.info("secretary: scheduling rules moved out of memory.md (kept for the next time)");
|
|
112
|
+
return "removed";
|
|
113
|
+
}
|
|
114
|
+
// ------------------------------------------------- "open memory.md" (settings card)
|
|
115
|
+
export const MEMORY_OPEN_PATH = "/plugins/clawbot/memory/open";
|
|
116
|
+
/** macOS: the app set for .md files, else the text editor; Linux: xdg-open; Windows: start. */
|
|
117
|
+
export const openOnDesktop = (file) => new Promise((resolve, reject) => {
|
|
118
|
+
const run = (cmd, args, fallback) => {
|
|
119
|
+
execFile(cmd, args, { timeout: 10_000 }, (err) => {
|
|
120
|
+
if (err === null)
|
|
121
|
+
resolve();
|
|
122
|
+
else if (fallback !== undefined)
|
|
123
|
+
fallback();
|
|
124
|
+
else
|
|
125
|
+
reject(err);
|
|
126
|
+
});
|
|
127
|
+
};
|
|
128
|
+
if (process.platform === "darwin")
|
|
129
|
+
run("open", [file], () => run("open", ["-t", file]));
|
|
130
|
+
else if (process.platform === "win32")
|
|
131
|
+
run("cmd", ["/c", "start", "", file]);
|
|
132
|
+
else
|
|
133
|
+
run("xdg-open", [file]);
|
|
134
|
+
});
|
|
135
|
+
/**
|
|
136
|
+
* POST /plugins/clawbot/memory/open — the settings card's "打开 memory.md".
|
|
137
|
+
*
|
|
138
|
+
* Plugin routes are not behind DSH's login cookie, so this takes only what the
|
|
139
|
+
* card itself sends: a JSON POST from the DSH page. A page on another site
|
|
140
|
+
* cannot send that content type without a preflight this route never answers,
|
|
141
|
+
* and a browser names the page a request came from in Origin. It opens a file
|
|
142
|
+
* the bot already reads every turn; nothing is sent back but its path.
|
|
143
|
+
*/
|
|
144
|
+
export async function handleMemoryOpen(req, res, opener = openOnDesktop) {
|
|
145
|
+
const reply = (status, body) => {
|
|
146
|
+
res.writeHead(status, { "content-type": "application/json; charset=utf-8", "cache-control": "no-store" });
|
|
147
|
+
res.end(JSON.stringify(body));
|
|
148
|
+
};
|
|
149
|
+
if ((req.method ?? "GET") !== "POST")
|
|
150
|
+
return reply(405, { ok: false, message: "use POST" });
|
|
151
|
+
const type = String(req.headers["content-type"] ?? "");
|
|
152
|
+
const origin = req.headers.origin;
|
|
153
|
+
const host = req.headers.host;
|
|
154
|
+
const sameSite = origin === undefined || (host !== undefined && (origin === `http://${host}` || origin === `https://${host}`));
|
|
155
|
+
if (!type.startsWith("application/json") || !sameSite) {
|
|
156
|
+
return reply(403, { ok: false, message: "只接受设置卡片发来的请求" });
|
|
157
|
+
}
|
|
158
|
+
ensureMemoryFile();
|
|
159
|
+
const file = memoryFilePath();
|
|
160
|
+
try {
|
|
161
|
+
await opener(file);
|
|
162
|
+
logger.info(`memory: opened ${file} on the desktop (settings card)`);
|
|
163
|
+
reply(200, { ok: true, path: file });
|
|
164
|
+
}
|
|
165
|
+
catch (err) {
|
|
166
|
+
reply(500, { ok: false, path: file, message: `打不开:${String(err.message ?? err).slice(0, 160)}` });
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
export function registerMemoryOpenRoute(ctx) {
|
|
170
|
+
ctx.inject(["webServer"], (scoped) => {
|
|
171
|
+
const server = scoped.webServer;
|
|
172
|
+
scoped.effect(() => server.register({ kind: "exact", path: MEMORY_OPEN_PATH, handler: (req, res) => handleMemoryOpen(req, res) }), "clawbot: memory open route");
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
//# sourceMappingURL=secretary.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"secretary.js","sourceRoot":"","sources":["../src/secretary.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAC9C,OAAO,EAAE,MAAM,SAAS,CAAC;AAEzB,OAAO,IAAI,MAAM,WAAW,CAAC;AAI7B,OAAO,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAC/D,OAAO,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAC;AAChD,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAE/D,MAAM,CAAC,MAAM,iBAAiB,GAAG,SAAS,CAAC;AAC3C,gFAAgF;AAChF,MAAM,OAAO,GAAG,YAAY,CAAC;AAE7B,sEAAsE;AACtE,MAAM,CAAC,MAAM,eAAe,GAAG;IAC7B,iBAAiB;IACjB,yEAAyE;IACzE,iCAAiC;IACjC,0CAA0C;IAC1C,wDAAwD;IACxD,kGAAkG;IAClG,oCAAoC;IACpC,8EAA8E;CAC/E,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAEb,uDAAuD;AACvD,SAAS,aAAa;IACpB,OAAO,IAAI,CAAC,IAAI,CAAC,eAAe,EAAE,EAAE,oBAAoB,CAAC,CAAC;AAC5D,CAAC;AAED;;;;GAIG;AACH,SAAS,UAAU;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