wechat-clawbot 0.9.1
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/LICENSE +21 -0
- package/NOTICE +32 -0
- package/README.md +324 -0
- package/README.zh.md +192 -0
- package/cordis.patch.yml +23 -0
- package/lib/approvals.d.ts +29 -0
- package/lib/approvals.d.ts.map +1 -0
- package/lib/approvals.js +86 -0
- package/lib/approvals.js.map +1 -0
- package/lib/bridge.d.ts +281 -0
- package/lib/bridge.d.ts.map +1 -0
- package/lib/bridge.js +827 -0
- package/lib/bridge.js.map +1 -0
- package/lib/claude-peer.d.ts +44 -0
- package/lib/claude-peer.d.ts.map +1 -0
- package/lib/claude-peer.js +515 -0
- package/lib/claude-peer.js.map +1 -0
- package/lib/cli.d.ts +3 -0
- package/lib/cli.d.ts.map +1 -0
- package/lib/cli.js +128 -0
- package/lib/cli.js.map +1 -0
- package/lib/client.js +597 -0
- package/lib/config.d.ts +160 -0
- package/lib/config.d.ts.map +1 -0
- package/lib/config.js +187 -0
- package/lib/config.js.map +1 -0
- package/lib/emoji.d.ts +26 -0
- package/lib/emoji.d.ts.map +1 -0
- package/lib/emoji.js +54 -0
- package/lib/emoji.js.map +1 -0
- package/lib/ilink/api/api.d.ts +134 -0
- package/lib/ilink/api/api.d.ts.map +1 -0
- package/lib/ilink/api/api.js +507 -0
- package/lib/ilink/api/api.js.map +1 -0
- package/lib/ilink/api/session-guard.d.ts +16 -0
- package/lib/ilink/api/session-guard.d.ts.map +1 -0
- package/lib/ilink/api/session-guard.js +49 -0
- package/lib/ilink/api/session-guard.js.map +1 -0
- package/lib/ilink/api/types.d.ts +251 -0
- package/lib/ilink/api/types.d.ts.map +1 -0
- package/lib/ilink/api/types.js +37 -0
- package/lib/ilink/api/types.js.map +1 -0
- package/lib/ilink/auth/accounts.d.ts +67 -0
- package/lib/ilink/auth/accounts.d.ts.map +1 -0
- package/lib/ilink/auth/accounts.js +190 -0
- package/lib/ilink/auth/accounts.js.map +1 -0
- package/lib/ilink/auth/login-qr.d.ts +43 -0
- package/lib/ilink/auth/login-qr.d.ts.map +1 -0
- package/lib/ilink/auth/login-qr.js +333 -0
- package/lib/ilink/auth/login-qr.js.map +1 -0
- package/lib/ilink/cdn/aes-ecb.d.ts +7 -0
- package/lib/ilink/cdn/aes-ecb.d.ts.map +1 -0
- package/lib/ilink/cdn/aes-ecb.js +19 -0
- package/lib/ilink/cdn/aes-ecb.js.map +1 -0
- package/lib/ilink/cdn/cdn-upload.d.ts +18 -0
- package/lib/ilink/cdn/cdn-upload.d.ts.map +1 -0
- package/lib/ilink/cdn/cdn-upload.js +90 -0
- package/lib/ilink/cdn/cdn-upload.js.map +1 -0
- package/lib/ilink/cdn/cdn-url.d.ts +14 -0
- package/lib/ilink/cdn/cdn-url.d.ts.map +1 -0
- package/lib/ilink/cdn/cdn-url.js +14 -0
- package/lib/ilink/cdn/cdn-url.js.map +1 -0
- package/lib/ilink/cdn/pic-decrypt.d.ts +10 -0
- package/lib/ilink/cdn/pic-decrypt.d.ts.map +1 -0
- package/lib/ilink/cdn/pic-decrypt.js +89 -0
- package/lib/ilink/cdn/pic-decrypt.js.map +1 -0
- package/lib/ilink/cdn/upload.d.ts +43 -0
- package/lib/ilink/cdn/upload.d.ts.map +1 -0
- package/lib/ilink/cdn/upload.js +115 -0
- package/lib/ilink/cdn/upload.js.map +1 -0
- package/lib/ilink/media/mime.d.ts +7 -0
- package/lib/ilink/media/mime.d.ts.map +1 -0
- package/lib/ilink/media/mime.js +73 -0
- package/lib/ilink/media/mime.js.map +1 -0
- package/lib/ilink/messaging/send-media.d.ts +23 -0
- package/lib/ilink/messaging/send-media.d.ts.map +1 -0
- package/lib/ilink/messaging/send-media.js +71 -0
- package/lib/ilink/messaging/send-media.js.map +1 -0
- package/lib/ilink/messaging/send.d.ts +55 -0
- package/lib/ilink/messaging/send.d.ts.map +1 -0
- package/lib/ilink/messaging/send.js +182 -0
- package/lib/ilink/messaging/send.js.map +1 -0
- package/lib/ilink/storage/state-dir.d.ts +14 -0
- package/lib/ilink/storage/state-dir.d.ts.map +1 -0
- package/lib/ilink/storage/state-dir.js +22 -0
- package/lib/ilink/storage/state-dir.js.map +1 -0
- package/lib/ilink/storage/sync-buf.d.ts +21 -0
- package/lib/ilink/storage/sync-buf.d.ts.map +1 -0
- package/lib/ilink/storage/sync-buf.js +64 -0
- package/lib/ilink/storage/sync-buf.js.map +1 -0
- package/lib/ilink/util/logger.d.ts +20 -0
- package/lib/ilink/util/logger.d.ts.map +1 -0
- package/lib/ilink/util/logger.js +73 -0
- package/lib/ilink/util/logger.js.map +1 -0
- package/lib/ilink/util/random.d.ts +11 -0
- package/lib/ilink/util/random.d.ts.map +1 -0
- package/lib/ilink/util/random.js +16 -0
- package/lib/ilink/util/random.js.map +1 -0
- package/lib/ilink/util/redact.d.ts +21 -0
- package/lib/ilink/util/redact.d.ts.map +1 -0
- package/lib/ilink/util/redact.js +54 -0
- package/lib/ilink/util/redact.js.map +1 -0
- package/lib/image-compress.d.ts +38 -0
- package/lib/image-compress.d.ts.map +1 -0
- package/lib/image-compress.js +184 -0
- package/lib/image-compress.js.map +1 -0
- package/lib/inbound-media.d.ts +17 -0
- package/lib/inbound-media.d.ts.map +1 -0
- package/lib/inbound-media.js +96 -0
- package/lib/inbound-media.js.map +1 -0
- package/lib/inbound.d.ts +28 -0
- package/lib/inbound.d.ts.map +1 -0
- package/lib/inbound.js +265 -0
- package/lib/inbound.js.map +1 -0
- package/lib/index.d.ts +23 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +321 -0
- package/lib/index.js.map +1 -0
- package/lib/mcp-route.d.ts +33 -0
- package/lib/mcp-route.d.ts.map +1 -0
- package/lib/mcp-route.js +422 -0
- package/lib/mcp-route.js.map +1 -0
- package/lib/memory-auto.d.ts +22 -0
- package/lib/memory-auto.d.ts.map +1 -0
- package/lib/memory-auto.js +144 -0
- package/lib/memory-auto.js.map +1 -0
- package/lib/memory.d.ts +26 -0
- package/lib/memory.d.ts.map +1 -0
- package/lib/memory.js +168 -0
- package/lib/memory.js.map +1 -0
- package/lib/models-route.d.ts +24 -0
- package/lib/models-route.d.ts.map +1 -0
- package/lib/models-route.js +84 -0
- package/lib/models-route.js.map +1 -0
- package/lib/monitor.d.ts +13 -0
- package/lib/monitor.d.ts.map +1 -0
- package/lib/monitor.js +116 -0
- package/lib/monitor.js.map +1 -0
- package/lib/pending.d.ts +23 -0
- package/lib/pending.d.ts.map +1 -0
- package/lib/pending.js +38 -0
- package/lib/pending.js.map +1 -0
- package/lib/prompt.d.ts +36 -0
- package/lib/prompt.d.ts.map +1 -0
- package/lib/prompt.js +246 -0
- package/lib/prompt.js.map +1 -0
- package/lib/questions.d.ts +21 -0
- package/lib/questions.d.ts.map +1 -0
- package/lib/questions.js +116 -0
- package/lib/questions.js.map +1 -0
- package/lib/quote-history.d.ts +18 -0
- package/lib/quote-history.d.ts.map +1 -0
- package/lib/quote-history.js +96 -0
- package/lib/quote-history.js.map +1 -0
- package/lib/state.d.ts +27 -0
- package/lib/state.d.ts.map +1 -0
- package/lib/state.js +154 -0
- package/lib/state.js.map +1 -0
- package/lib/subagent-model.d.ts +26 -0
- package/lib/subagent-model.d.ts.map +1 -0
- package/lib/subagent-model.js +30 -0
- package/lib/subagent-model.js.map +1 -0
- package/lib/tool.d.ts +14 -0
- package/lib/tool.d.ts.map +1 -0
- package/lib/tool.js +518 -0
- package/lib/tool.js.map +1 -0
- package/package.json +94 -0
package/lib/approvals.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { logger } from "./ilink/util/logger.js";
|
|
2
|
+
// NOTE: no \b word boundaries here — JS \b does not match CJK text, so
|
|
3
|
+
// "同意" would never match a pattern like /^同意\b/.
|
|
4
|
+
const YES_RE = /^(同意|允许|批准|确认|可以|好的?|是|yes|y|ok|allow|approve)$/i;
|
|
5
|
+
const NO_RE = /^(拒绝|不同意|不允许|不行|不要|否|no|n|reject|deny)$/i;
|
|
6
|
+
function parseApprovalReply(text) {
|
|
7
|
+
// Trim and strip trailing punctuation before matching.
|
|
8
|
+
const trimmed = text.trim().replace(/[\s,。!!??,.、;;::]+$/g, "");
|
|
9
|
+
if (YES_RE.test(trimmed))
|
|
10
|
+
return "yes";
|
|
11
|
+
if (NO_RE.test(trimmed))
|
|
12
|
+
return "no";
|
|
13
|
+
// Anything else is treated as a rejection (fail closed).
|
|
14
|
+
return "no";
|
|
15
|
+
}
|
|
16
|
+
export class ApprovalRelay {
|
|
17
|
+
config;
|
|
18
|
+
getBridge;
|
|
19
|
+
pending;
|
|
20
|
+
sendText;
|
|
21
|
+
constructor(deps) {
|
|
22
|
+
this.config = deps.config;
|
|
23
|
+
this.getBridge = deps.getBridge;
|
|
24
|
+
this.pending = deps.pending;
|
|
25
|
+
this.sendText = deps.sendText;
|
|
26
|
+
}
|
|
27
|
+
/** Register the `approval/request` waterfall listener on the root context. */
|
|
28
|
+
register(ctx) {
|
|
29
|
+
ctx.on("approval/request", (req, next) => this.answer(req, next));
|
|
30
|
+
}
|
|
31
|
+
async answer(req, next) {
|
|
32
|
+
const bridge = this.getBridge();
|
|
33
|
+
if (!bridge || !bridge.isWechatSession(req.agent.id)) {
|
|
34
|
+
return next();
|
|
35
|
+
}
|
|
36
|
+
const sender = bridge.activeSender;
|
|
37
|
+
if (!sender) {
|
|
38
|
+
logger.warn(`approval: no active WeChat sender for agent ${req.agent.id}; failing closed`);
|
|
39
|
+
return "unavailable";
|
|
40
|
+
}
|
|
41
|
+
const question = `【DSH 权限请求】\n` +
|
|
42
|
+
`工具: ${req.toolName}\n` +
|
|
43
|
+
`原因: ${req.reason ?? "(未提供)"}\n` +
|
|
44
|
+
`请回复:同意 / 拒绝`;
|
|
45
|
+
try {
|
|
46
|
+
await this.sendText(sender, question);
|
|
47
|
+
}
|
|
48
|
+
catch (err) {
|
|
49
|
+
logger.error(`approval: failed to send question to ${sender}: ${String(err)}`);
|
|
50
|
+
return "unavailable";
|
|
51
|
+
}
|
|
52
|
+
return new Promise((resolveOutcome) => {
|
|
53
|
+
let settled = false;
|
|
54
|
+
const settle = (outcome) => {
|
|
55
|
+
if (settled)
|
|
56
|
+
return;
|
|
57
|
+
settled = true;
|
|
58
|
+
resolveOutcome(outcome);
|
|
59
|
+
};
|
|
60
|
+
const answer = {
|
|
61
|
+
tag: "approval",
|
|
62
|
+
sender,
|
|
63
|
+
resolve: (reply) => {
|
|
64
|
+
if (reply === null) {
|
|
65
|
+
settle("cancelled");
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
const decision = parseApprovalReply(reply);
|
|
69
|
+
void this.sendText(sender, decision === "yes" ? "已批准。" : "已拒绝。").catch(() => { });
|
|
70
|
+
settle(decision === "yes" ? "allowed-once" : "rejected");
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
this.pending.push(answer);
|
|
74
|
+
// Withdraw the question when the requesting turn is aborted.
|
|
75
|
+
req.signal?.addEventListener("abort", () => {
|
|
76
|
+
answer.resolve(null);
|
|
77
|
+
}, { once: true });
|
|
78
|
+
if (this.config.approvalTimeoutMs > 0) {
|
|
79
|
+
setTimeout(() => {
|
|
80
|
+
answer.resolve(null);
|
|
81
|
+
}, this.config.approvalTimeoutMs).unref?.();
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
//# sourceMappingURL=approvals.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"approvals.js","sourceRoot":"","sources":["../src/approvals.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAC;AAEhD,uEAAuE;AACvE,iDAAiD;AACjD,MAAM,MAAM,GAAG,kDAAkD,CAAC;AAClE,MAAM,KAAK,GAAG,0CAA0C,CAAC;AAEzD,SAAS,kBAAkB,CAAC,IAAY;IACtC,uDAAuD;IACvD,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,EAAE,CAAC,OAAO,CAAC,sBAAs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IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;YAE1B,6DAA6D;YAC7D,GAAG,CAAC,MAAM,EAAE,gBAAgB,CAC1B,OAAO,EACP,GAAG,EAAE;gBACH,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;YACvB,CAAC,EACD,EAAE,IAAI,EAAE,IAAI,EAAE,CACf,CAAC;YAEF,IAAI,IAAI,CAAC,MAAM,CAAC,iBAAiB,GAAG,CAAC,EAAE,CAAC;gBACtC,UAAU,CAAC,GAAG,EAAE;oBACd,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;gBACvB,CAAC,EAAE,IAAI,CAAC,MAAM,CAAC,iBAAiB,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC;YAC9C,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC;CACF"}
|
package/lib/bridge.d.ts
ADDED
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
import type { Context } from "@deepseek-ai/cordis";
|
|
2
|
+
import { type Agent } from "@deepseek-ai/dsh-agent";
|
|
3
|
+
import { SessionId, type Session, type SessionEvent } from "@deepseek-ai/dsh-session";
|
|
4
|
+
import type { ClawbotConfig } from "./config.js";
|
|
5
|
+
import { PendingRegistry } from "./pending.js";
|
|
6
|
+
import type { ResolvedWeixinAccount } from "./ilink/auth/accounts.js";
|
|
7
|
+
export type SendTextFn = (to: string, text: string) => Promise<void>;
|
|
8
|
+
export type BridgeDeps = {
|
|
9
|
+
/** Current bound account (API credentials), or null before login. */
|
|
10
|
+
getAccount: () => ResolvedWeixinAccount | null;
|
|
11
|
+
/** Outbound context token for a sender ("" when unknown). */
|
|
12
|
+
getContextToken: (sender: string) => string;
|
|
13
|
+
/** Pending WeChat interactions (approvals + questions). */
|
|
14
|
+
pending: PendingRegistry;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* The durable, serializable reference `attachments.saveImages()` hands back.
|
|
18
|
+
*
|
|
19
|
+
* Mirrored here rather than imported from `@deepseek-ai/dsh-attachment`: this
|
|
20
|
+
* plugin's dev tree carries an older copy of that package than the host runs,
|
|
21
|
+
* and a bare import resolves to the near one (that mismatch is exactly what
|
|
22
|
+
* silently broke attaching once already — see `attachImage`). Six plain fields,
|
|
23
|
+
* all JSON, so mirroring costs nothing and cannot drift into a runtime crash.
|
|
24
|
+
*/
|
|
25
|
+
export type ImageRef = {
|
|
26
|
+
attachmentId: string;
|
|
27
|
+
mediaType: "image/png" | "image/jpeg" | "image/webp" | "image/gif";
|
|
28
|
+
bytes: number;
|
|
29
|
+
width: number;
|
|
30
|
+
height: number;
|
|
31
|
+
name?: string;
|
|
32
|
+
/**
|
|
33
|
+
* Present from dsh-attachment 0.1.1-rc.2 onward, and only when the store
|
|
34
|
+
* actually resized: it now normalizes EXIF orientation and downscales
|
|
35
|
+
* oversized images, recording what the original was.
|
|
36
|
+
*/
|
|
37
|
+
originalDimensions?: {
|
|
38
|
+
width: number;
|
|
39
|
+
height: number;
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
export declare class WechatBridge {
|
|
43
|
+
private readonly ctx;
|
|
44
|
+
private readonly config;
|
|
45
|
+
private readonly sendText;
|
|
46
|
+
private readonly deps;
|
|
47
|
+
private readonly sessionId;
|
|
48
|
+
private handle;
|
|
49
|
+
private disposed;
|
|
50
|
+
/** Agent contexts we already installed the model selection on (avoid duplicates). */
|
|
51
|
+
private selectionInstalled;
|
|
52
|
+
/** Queued turns waiting for the serialized worker (oldest first). */
|
|
53
|
+
private readonly queue;
|
|
54
|
+
/** Whether the WeChat tools are registered on the current agent scope. */
|
|
55
|
+
private toolsRegistered;
|
|
56
|
+
/** The sender whose turn is currently being driven (approvals go to them). */
|
|
57
|
+
private currentSender;
|
|
58
|
+
/** Messages sent in the current turn (cap enforcement, RULE B). */
|
|
59
|
+
private turnMessageCount;
|
|
60
|
+
/** Per-turn outbound message cap. */
|
|
61
|
+
private static readonly MAX_TURN_MESSAGES;
|
|
62
|
+
/** The serialized turn worker (one followup → idle at a time). */
|
|
63
|
+
private worker;
|
|
64
|
+
/** Time index of session message events (for quote backfill). */
|
|
65
|
+
private sessionIndex;
|
|
66
|
+
/**
|
|
67
|
+
* Serialized outbound chain: assistant segments, tool acks, and approvals
|
|
68
|
+
* are delivered one at a time so WeChat messages never interleave.
|
|
69
|
+
*/
|
|
70
|
+
private sendChain;
|
|
71
|
+
constructor(ctx: Context, config: ClawbotConfig, sendText: SendTextFn, deps: BridgeDeps);
|
|
72
|
+
/** Outbound context token for a WeChat sender ("" when unknown). */
|
|
73
|
+
contextTokenFor(sender: string): string;
|
|
74
|
+
/**
|
|
75
|
+
* Send one text message to a WeChat user, serialized behind every other
|
|
76
|
+
* outbound message so delivery order is preserved.
|
|
77
|
+
*/
|
|
78
|
+
sendTextTo(sender: string, text: string): void;
|
|
79
|
+
get id(): SessionId;
|
|
80
|
+
/**
|
|
81
|
+
* Ask the user a question THROUGH WeChat and wait for their reply.
|
|
82
|
+
* Used by the WeChat-scoped ask_user_question override so bot questions
|
|
83
|
+
* never pop up in the webapp GUI. Returns the raw reply text, or null on
|
|
84
|
+
* timeout / abort.
|
|
85
|
+
*/
|
|
86
|
+
askWechat(question: string, timeoutMs?: number): Promise<string | null>;
|
|
87
|
+
/**
|
|
88
|
+
* Install the deployment default model selection (provider/model/
|
|
89
|
+
* reasoningEffort from settings.yaml) onto an agent context, once per
|
|
90
|
+
* context. Used both by the agent setup and for agents the harness
|
|
91
|
+
* resumed directly without running the bridge setup.
|
|
92
|
+
*/
|
|
93
|
+
/**
|
|
94
|
+
* One mutable selection shared by every agent context this bridge installs
|
|
95
|
+
* into. `installModelSelection` is documented to snapshot `current` per step,
|
|
96
|
+
* so writing a new value here switches the model from the next step onward —
|
|
97
|
+
* no reinstall, no restart, no dropped WeChat connection.
|
|
98
|
+
*/
|
|
99
|
+
private readonly selectionRef;
|
|
100
|
+
/**
|
|
101
|
+
* What this session should route to: the plugin's own pin when configured,
|
|
102
|
+
* otherwise the deployment default.
|
|
103
|
+
*
|
|
104
|
+
* The default-following branch is why choosing a model in the web UI used to
|
|
105
|
+
* move the bot as well — that picker writes through to the global
|
|
106
|
+
* `agent-default-model`, and with nothing pinned here the bot simply adopts
|
|
107
|
+
* it. Setting `model` in this plugin's settings breaks that coupling.
|
|
108
|
+
*/
|
|
109
|
+
private desiredSelection;
|
|
110
|
+
private installSelection;
|
|
111
|
+
/**
|
|
112
|
+
* Re-point the live selection after a settings change.
|
|
113
|
+
*
|
|
114
|
+
* A model id that cannot be resolved is REFUSED rather than applied: a typo
|
|
115
|
+
* in the settings box would otherwise brick the bot on its next message, and
|
|
116
|
+
* the failure would surface as a request error with no obvious cause. When
|
|
117
|
+
* the llm service is unavailable the value is taken on trust — refusing
|
|
118
|
+
* everything would be worse than a possible bad pin.
|
|
119
|
+
*/
|
|
120
|
+
refreshSelection(): Promise<void>;
|
|
121
|
+
/** Whether the given session id is the WeChat session. */
|
|
122
|
+
isWechatSession(sessionId: string): boolean;
|
|
123
|
+
/** The sender the current activity is attributed to (for approval routing). */
|
|
124
|
+
get activeSender(): string | undefined;
|
|
125
|
+
/**
|
|
126
|
+
* Get (or create/resume) the WeChat agent. The agent is composed from the
|
|
127
|
+
* deployment's default agent preset (standard coding tools) and its
|
|
128
|
+
* approval policy is forced to `ask`, so remote access requests arrive in
|
|
129
|
+
* WeChat regardless of the deployment-wide policy.
|
|
130
|
+
*
|
|
131
|
+
* Mirrors the headless runner's creation pattern: wait for the loader to
|
|
132
|
+
* finish composing sibling plugins (otherwise the agent's world is
|
|
133
|
+
* half-composed and the driver can stall) and install the deployment's
|
|
134
|
+
* default model selection into the agent scope.
|
|
135
|
+
*/
|
|
136
|
+
ensureAgent(): Promise<Agent>;
|
|
137
|
+
/**
|
|
138
|
+
* Deliver one WeChat text message immediately, no waiting window. Normal
|
|
139
|
+
* flow: when the agent is idle the message starts a new turn. Interrupt
|
|
140
|
+
* flow: when the agent is already mid-turn (the user interjects while we
|
|
141
|
+
* are working), the message is STEERED into the running turn (next-step) so
|
|
142
|
+
* the agent absorbs it and rethinks before replying — one reply total.
|
|
143
|
+
* Returns immediately: the monitor loop must NEVER block on an agent turn.
|
|
144
|
+
*/
|
|
145
|
+
/**
|
|
146
|
+
* Whether the model this session will actually route to accepts image input.
|
|
147
|
+
*
|
|
148
|
+
* Read from the session's live request header (the route in force), falling
|
|
149
|
+
* back to the agent's options. Any failure to resolve counts as "no": an
|
|
150
|
+
* unproven route must not be handed an image it may reject, which would fail
|
|
151
|
+
* the whole turn instead of just missing a picture.
|
|
152
|
+
*
|
|
153
|
+
* `llm` and `attachments` are deliberately NOT in this plugin's `inject`
|
|
154
|
+
* list — declaring them makes them hard requirements, and a composition
|
|
155
|
+
* without them (headless, say) would fail the entire loader tree. So they
|
|
156
|
+
* are probed defensively and absence simply degrades to the old
|
|
157
|
+
* path-plus-vision-tool behaviour.
|
|
158
|
+
*/
|
|
159
|
+
private visionCtx;
|
|
160
|
+
/**
|
|
161
|
+
* Receive a context scoped by `ctx.inject(["llm","attachments"], …)`.
|
|
162
|
+
*
|
|
163
|
+
* The only safe way to reach an optional service here: bare property access
|
|
164
|
+
* THROWS ("cannot get property \"llm\" without inject") and took down a whole
|
|
165
|
+
* inbound message, while listing them in `export const inject` would make
|
|
166
|
+
* them hard requirements and fail the loader tree wherever they are absent.
|
|
167
|
+
* This callback simply never fires in such a composition.
|
|
168
|
+
*/
|
|
169
|
+
useVisionServices(scoped: Context): void;
|
|
170
|
+
/** Read a service from the injected context; undefined when unavailable. */
|
|
171
|
+
private service;
|
|
172
|
+
/**
|
|
173
|
+
* Log at startup exactly what the image probe sees. Without it the only way
|
|
174
|
+
* to learn why a photo was not attached was to ask for another photo, and a
|
|
175
|
+
* genuine "not image capable" looks identical to a broken probe.
|
|
176
|
+
*/
|
|
177
|
+
reportVisionReadiness(): Promise<void>;
|
|
178
|
+
private llmService;
|
|
179
|
+
/**
|
|
180
|
+
* The provider/model this session will actually route to.
|
|
181
|
+
*
|
|
182
|
+
* Three sources, in descending authority. The live request header is the
|
|
183
|
+
* truth once a request has been made. A resumed agent has neither — no
|
|
184
|
+
* request yet in this process, and `options` is only populated on the create
|
|
185
|
+
* path — which is why the first probe reported "route unknown" and quietly
|
|
186
|
+
* disabled attachments. The deployment default is the reliable fallback
|
|
187
|
+
* precisely because installSelection() pins it onto this agent, so it IS the
|
|
188
|
+
* route the next turn takes.
|
|
189
|
+
*/
|
|
190
|
+
private currentRoute;
|
|
191
|
+
/**
|
|
192
|
+
* Whether the model this session routes to can take an image in its input.
|
|
193
|
+
* Public: the outbound pre-send check (`look_at_image` in tool.ts) gates on
|
|
194
|
+
* exactly the same answer as the inbound attach.
|
|
195
|
+
*/
|
|
196
|
+
routeTakesImages(): Promise<boolean>;
|
|
197
|
+
/**
|
|
198
|
+
* Commit one local image to the attachment store and hand back its durable
|
|
199
|
+
* reference — downscaled first, because a camera original is tens of
|
|
200
|
+
* megapixels and would blow the store's per-image limits long before it
|
|
201
|
+
* reached the model.
|
|
202
|
+
*
|
|
203
|
+
* Public, and used by BOTH directions: an inbound WeChat photo (via
|
|
204
|
+
* `imageBlock` below) and an outbound photo the agent is about to send (via
|
|
205
|
+
* `look_at_image` in tool.ts). Those used to differ — inbound attached the
|
|
206
|
+
* bytes natively while outbound handed a *path* to qwen and paid a second
|
|
207
|
+
* round trip for it — and there was never a reason for two mechanisms.
|
|
208
|
+
*
|
|
209
|
+
* @param imagePath - the local image to commit.
|
|
210
|
+
* @param tag - log prefix naming the caller, so the two directions stay
|
|
211
|
+
* distinguishable in the log.
|
|
212
|
+
* @returns the durable reference, or null when anything is missing.
|
|
213
|
+
*/
|
|
214
|
+
attachImage(imagePath: string, tag?: string): Promise<ImageRef | null>;
|
|
215
|
+
/** Wrap `attachImage` as the content block an inbound message carries. */
|
|
216
|
+
private imageBlock;
|
|
217
|
+
enqueueMessage(sender: string, text: string, imagePath?: string): Promise<void>;
|
|
218
|
+
/** Start the serialized worker if it is not already running. */
|
|
219
|
+
private runWorker;
|
|
220
|
+
/** Drive queued turns one at a time; never rejects. */
|
|
221
|
+
private drainQueue;
|
|
222
|
+
/**
|
|
223
|
+
* Session event feed: track the active WeChat recipient for turns NOT
|
|
224
|
+
* driven by the inbound queue. Assistant text is NEVER forwarded
|
|
225
|
+
* automatically — the agent must call the send_wechat_text /
|
|
226
|
+
* send_wechat_file tools explicitly to deliver anything to WeChat. This
|
|
227
|
+
* method only:
|
|
228
|
+
* - sets `currentSender` for scheduled-reminder injections (source plugin
|
|
229
|
+
* "schedule"), so the send tools know whom to deliver to; and
|
|
230
|
+
* - clears the stale sender when a GUI-only turn ends, so GUI messages
|
|
231
|
+
* never get a WeChat recipient.
|
|
232
|
+
*/
|
|
233
|
+
onSessionEvent(session: Session, event: SessionEvent): void;
|
|
234
|
+
/**
|
|
235
|
+
* Build (or refresh) a time index of all session message events, so quotes
|
|
236
|
+
* of messages that predate the quote-history registry (e.g. before a
|
|
237
|
+
* restart) can still be resolved by timestamp.
|
|
238
|
+
*/
|
|
239
|
+
private buildSessionIndex;
|
|
240
|
+
/**
|
|
241
|
+
* Resolve a quote by the quoted message's server creation time (used when
|
|
242
|
+
* the msg-id lookup misses, e.g. for messages sent before the quote
|
|
243
|
+
* history started). Returns the nearest session message within tolerance.
|
|
244
|
+
*/
|
|
245
|
+
lookupQuoteByTime(createTimeMs?: number): {
|
|
246
|
+
text: string;
|
|
247
|
+
imagePath?: string;
|
|
248
|
+
} | null;
|
|
249
|
+
/**
|
|
250
|
+
* Register the WeChat-scoped tools (send_wechat_file, preview_wechat_image,
|
|
251
|
+
* (send_wechat_file, preview_wechat_image) on an agent scope. Idempotent
|
|
252
|
+
* per bridge lifetime; safe
|
|
253
|
+
* to call from both the creation setup and the live-agent path.
|
|
254
|
+
*/
|
|
255
|
+
private ensureTools;
|
|
256
|
+
/**
|
|
257
|
+
* Hide the multi-agent orchestration tools from the WeChat session.
|
|
258
|
+
*
|
|
259
|
+
* Observed failure: the agent called `send_message` with
|
|
260
|
+
* `{subagent_id: "schedule-9", message: "ignore"}`, got
|
|
261
|
+
* `subagent "schedule-9" is unavailable`, then retried with schedule-10,
|
|
262
|
+
* -11, ... past -103 — 158 steps and 4.5M input tokens in about three
|
|
263
|
+
* minutes ($0.11 of a $10 monthly allowance) before it was stopped by hand.
|
|
264
|
+
* Running with reasoning_effort "none" (the only setting this gateway
|
|
265
|
+
* accepts alongside tools) leaves no self-correction to break such a loop.
|
|
266
|
+
*
|
|
267
|
+
* `send_message` is the likeliest trigger: it sits next to this plugin's own
|
|
268
|
+
* `send_wechat_text` in the same tool list, and the two names are easy to
|
|
269
|
+
* conflate. None of these tools have a role in a one-on-one WeChat chat, so
|
|
270
|
+
* denying them removes the whole failure mode rather than guarding it.
|
|
271
|
+
*
|
|
272
|
+
* A deny list, not an allow list: everything the bot actually uses (bash,
|
|
273
|
+
* read/glob/grep, web_search, the vision tools, schedule_*, memory) keeps
|
|
274
|
+
* working untouched. Per-scope restrictions also leave scoped registrations
|
|
275
|
+
* alone, so the WeChat tools registered just above survive.
|
|
276
|
+
*/
|
|
277
|
+
private restrictTools;
|
|
278
|
+
/** Tear down the owned agent handle (the persisted session log survives). */
|
|
279
|
+
dispose(): Promise<void>;
|
|
280
|
+
}
|
|
281
|
+
//# sourceMappingURL=bridge.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bridge.d.ts","sourceRoot":"","sources":["../src/bridge.ts"],"names":[],"mappings":"AAYA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,EAEL,KAAK,KAAK,EAIX,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,SAAS,EAAE,KAAK,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAQtF,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAEjD,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAG/C,OAAO,KAAK,EAAE,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