Skip to content

Commit 03415bb

Browse files
committed
refactor: share MSTeams outbound send resolvers
1 parent 723b508 commit 03415bb

1 file changed

Lines changed: 51 additions & 56 deletions

File tree

extensions/msteams/src/outbound.ts

Lines changed: 51 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { resolveOutboundSendDep } from "openclaw/plugin-sdk/channel-outbound";
1+
import {
2+
resolveOutboundSendDep,
3+
type OutboundSendDeps,
4+
} from "openclaw/plugin-sdk/channel-outbound";
25
import {
36
attachChannelToResult,
47
createAttachedChannelResultAdapter,
@@ -25,6 +28,48 @@ function asObjectRecord(value: unknown): Record<string, unknown> | undefined {
2528

2629
const MSTEAMS_TEXT_CHUNK_LIMIT = 4000;
2730

31+
type MSTeamsSendConfig = Parameters<typeof sendMessageMSTeams>[0]["cfg"];
32+
type MSTeamsSendResult = { messageId: string; conversationId: string };
33+
type MSTeamsMediaSendOptions = {
34+
mediaUrl?: string;
35+
mediaLocalRoots?: readonly string[];
36+
mediaReadFile?: (filePath: string) => Promise<Buffer>;
37+
};
38+
type MSTeamsTextSendFn = (to: string, text: string) => Promise<MSTeamsSendResult>;
39+
type MSTeamsMediaSendFn = (
40+
to: string,
41+
text: string,
42+
opts?: MSTeamsMediaSendOptions,
43+
) => Promise<MSTeamsSendResult>;
44+
45+
function resolveMSTeamsTextSend(params: {
46+
cfg: MSTeamsSendConfig;
47+
deps?: OutboundSendDeps;
48+
}): MSTeamsTextSendFn {
49+
return (
50+
resolveOutboundSendDep<MSTeamsTextSendFn>(params.deps, "msteams") ??
51+
((to, text) => sendMessageMSTeams({ cfg: params.cfg, to, text }))
52+
);
53+
}
54+
55+
function resolveMSTeamsMediaSend(params: {
56+
cfg: MSTeamsSendConfig;
57+
deps?: OutboundSendDeps;
58+
}): MSTeamsMediaSendFn {
59+
return (
60+
resolveOutboundSendDep<MSTeamsMediaSendFn>(params.deps, "msteams") ??
61+
((to, text, opts) =>
62+
sendMessageMSTeams({
63+
cfg: params.cfg,
64+
to,
65+
text,
66+
mediaUrl: opts?.mediaUrl,
67+
mediaLocalRoots: opts?.mediaLocalRoots,
68+
mediaReadFile: opts?.mediaReadFile,
69+
}))
70+
);
71+
}
72+
2873
export const msteamsOutbound: ChannelOutboundAdapter = {
2974
deliveryMode: "direct",
3075
chunker: chunkTextForOutbound,
@@ -91,26 +136,7 @@ export const msteamsOutbound: ChannelOutboundAdapter = {
91136
}),
92137
);
93138
if (mediaUrls.length > 0) {
94-
type SendFn = (
95-
to: string,
96-
text: string,
97-
opts?: {
98-
mediaUrl?: string;
99-
mediaLocalRoots?: readonly string[];
100-
mediaReadFile?: (filePath: string) => Promise<Buffer>;
101-
},
102-
) => Promise<{ messageId: string; conversationId: string }>;
103-
const send =
104-
resolveOutboundSendDep<SendFn>(deps, "msteams") ??
105-
((to, text, opts) =>
106-
sendMessageMSTeams({
107-
cfg,
108-
to,
109-
text,
110-
mediaUrl: opts?.mediaUrl,
111-
mediaLocalRoots: opts?.mediaLocalRoots,
112-
mediaReadFile: opts?.mediaReadFile,
113-
}));
139+
const send = resolveMSTeamsMediaSend({ cfg, deps });
114140
const result = await sendPayloadMediaSequence({
115141
text,
116142
mediaUrls,
@@ -122,18 +148,12 @@ export const msteamsOutbound: ChannelOutboundAdapter = {
122148
}
123149
}
124150
if (text.trim()) {
125-
type SendFn = (
126-
to: string,
127-
text: string,
128-
) => Promise<{ messageId: string; conversationId: string }>;
129-
const send =
130-
resolveOutboundSendDep<SendFn>(deps, "msteams") ??
131-
((to, text) => sendMessageMSTeams({ cfg, to, text }));
151+
const send = resolveMSTeamsTextSend({ cfg, deps });
132152
const chunks = resolveTextChunksWithFallback(
133153
text,
134154
chunkTextForOutbound(text, MSTEAMS_TEXT_CHUNK_LIMIT),
135155
);
136-
let result: Awaited<ReturnType<SendFn>>;
156+
let result: Awaited<ReturnType<MSTeamsTextSendFn>>;
137157
for (const chunk of chunks) {
138158
result = await send(to, chunk);
139159
}
@@ -144,36 +164,11 @@ export const msteamsOutbound: ChannelOutboundAdapter = {
144164
...createAttachedChannelResultAdapter({
145165
channel: "msteams",
146166
sendText: async ({ cfg, to, text, deps }) => {
147-
type SendFn = (
148-
to: string,
149-
text: string,
150-
) => Promise<{ messageId: string; conversationId: string }>;
151-
const send =
152-
resolveOutboundSendDep<SendFn>(deps, "msteams") ??
153-
((to, text) => sendMessageMSTeams({ cfg, to, text }));
167+
const send = resolveMSTeamsTextSend({ cfg, deps });
154168
return await send(to, text);
155169
},
156170
sendMedia: async ({ cfg, to, text, mediaUrl, mediaLocalRoots, mediaReadFile, deps }) => {
157-
type SendFn = (
158-
to: string,
159-
text: string,
160-
opts?: {
161-
mediaUrl?: string;
162-
mediaLocalRoots?: readonly string[];
163-
mediaReadFile?: (filePath: string) => Promise<Buffer>;
164-
},
165-
) => Promise<{ messageId: string; conversationId: string }>;
166-
const send =
167-
resolveOutboundSendDep<SendFn>(deps, "msteams") ??
168-
((to, text, opts) =>
169-
sendMessageMSTeams({
170-
cfg,
171-
to,
172-
text,
173-
mediaUrl: opts?.mediaUrl,
174-
mediaLocalRoots: opts?.mediaLocalRoots,
175-
mediaReadFile: opts?.mediaReadFile,
176-
}));
171+
const send = resolveMSTeamsMediaSend({ cfg, deps });
177172
return await send(to, text, { mediaUrl, mediaLocalRoots, mediaReadFile });
178173
},
179174
sendPoll: async ({ cfg, to, poll }) => {

0 commit comments

Comments
 (0)