palz-connector 1.4.3 → 1.4.5
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/openclaw.plugin.json +1 -1
- package/package.json +1 -1
- package/src/content.ts +26 -0
- package/src/outbound.ts +7 -9
- package/src/reply-dispatcher.ts +36 -17
- package/src/send.ts +14 -2
package/openclaw.plugin.json
CHANGED
package/package.json
CHANGED
package/src/content.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { ContentPart, OpenAIContent, TextContentPart } from "./types.js";
|
|
2
|
+
|
|
3
|
+
export function extractSingleHttpsUrl(text: string): string | null {
|
|
4
|
+
const trimmed = text.trim();
|
|
5
|
+
if (!trimmed || /\s/.test(trimmed)) return null;
|
|
6
|
+
|
|
7
|
+
try {
|
|
8
|
+
const url = new URL(trimmed);
|
|
9
|
+
return url.protocol === "https:" && url.hostname ? trimmed : null;
|
|
10
|
+
} catch {
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function textToOpenAIContent(text: string): OpenAIContent {
|
|
16
|
+
const url = extractSingleHttpsUrl(text);
|
|
17
|
+
return url ? [{ type: "file", file_url: { url } }] : text;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function contentPartsToOpenAIContent(contentParts: ContentPart[]): OpenAIContent {
|
|
21
|
+
if (contentParts.length === 1 && contentParts[0].type === "text") {
|
|
22
|
+
return textToOpenAIContent((contentParts[0] as TextContentPart).text);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return contentParts;
|
|
26
|
+
}
|
package/src/outbound.ts
CHANGED
|
@@ -9,7 +9,7 @@ import { resolvePalzAccount } from "./config.js";
|
|
|
9
9
|
import { sendToPalzIM } from "./send.js";
|
|
10
10
|
import { loadMediaAsOssUrl } from "./media.js";
|
|
11
11
|
import { parsePalzTarget } from "./targets.js";
|
|
12
|
-
import type { ContentPart
|
|
12
|
+
import type { ContentPart } from "./types.js";
|
|
13
13
|
|
|
14
14
|
export const palzOutbound = {
|
|
15
15
|
deliveryMode: "direct" as const,
|
|
@@ -35,10 +35,13 @@ export const palzOutbound = {
|
|
|
35
35
|
const { senderId, lobsterId, conversationId, conversationType } = parsePalzTarget(to);
|
|
36
36
|
log(`palz-outbound: [sendText] 解析: senderId="${senderId}" lobsterId="${lobsterId}" conversationId="${conversationId}" conversationType="${conversationType}" botId=${account.config.botId}`);
|
|
37
37
|
|
|
38
|
+
const content = text ?? "";
|
|
39
|
+
log(`palz-outbound: [sendText] 发送内容: textLen=${content.length}`);
|
|
40
|
+
|
|
38
41
|
const result = await sendToPalzIM({
|
|
39
42
|
config: account.config,
|
|
40
43
|
conversationId,
|
|
41
|
-
content
|
|
44
|
+
content,
|
|
42
45
|
senderId,
|
|
43
46
|
conversationType,
|
|
44
47
|
lobsterId,
|
|
@@ -75,17 +78,12 @@ export const palzOutbound = {
|
|
|
75
78
|
}
|
|
76
79
|
}
|
|
77
80
|
|
|
78
|
-
|
|
79
|
-
contentParts.length === 1 && contentParts[0].type === "text"
|
|
80
|
-
? (contentParts[0] as TextContentPart).text
|
|
81
|
-
: contentParts;
|
|
82
|
-
|
|
83
|
-
log(`palz-outbound: [sendMedia] 发送内容: type=${typeof content === "string" ? "string" : "array"} parts=${Array.isArray(content) ? content.length : 1}`);
|
|
81
|
+
log(`palz-outbound: [sendMedia] 发送内容: parts=${contentParts.length}`);
|
|
84
82
|
|
|
85
83
|
const result = await sendToPalzIM({
|
|
86
84
|
config: account.config,
|
|
87
85
|
conversationId,
|
|
88
|
-
content,
|
|
86
|
+
content: contentParts,
|
|
89
87
|
senderId,
|
|
90
88
|
conversationType,
|
|
91
89
|
lobsterId,
|
package/src/reply-dispatcher.ts
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
import { getPalzRuntime } from "./runtime.js";
|
|
12
12
|
import { loadMediaAsOssUrl } from "./media.js";
|
|
13
13
|
import { sendToPalzIM } from "./send.js";
|
|
14
|
+
|
|
14
15
|
import { resolveToolDisplay, formatToolStartText, formatToolResultText, formatResultSummary } from "./tool-display.js";
|
|
15
16
|
import type { PalzConfig, StreamChunkOpts, ContentPart, OpenAIContent } from "./types.js";
|
|
16
17
|
|
|
@@ -212,26 +213,44 @@ export function createPalzReplyDispatcher(params: CreatePalzReplyDispatcherParam
|
|
|
212
213
|
// Determine final content format
|
|
213
214
|
const content: OpenAIContent =
|
|
214
215
|
contentParts.length === 1 && contentParts[0].type === "text"
|
|
215
|
-
?
|
|
216
|
+
? contentParts[0].text
|
|
216
217
|
: contentParts;
|
|
217
218
|
|
|
218
|
-
if (streamState && kind === "final"
|
|
219
|
-
const delta = content.slice(streamState.lastSentLength);
|
|
219
|
+
if (streamState && kind === "final") {
|
|
220
220
|
const seq = streamState.seq++;
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
221
|
+
if (typeof content === "string") {
|
|
222
|
+
const delta = content.slice(streamState.lastSentLength);
|
|
223
|
+
log(
|
|
224
|
+
`${tag}: [DELIVER 流式最终] seq=${seq} totalLen=${content.length} deltaLen=${delta.length} delta="${delta.slice(0, 120)}"`,
|
|
225
|
+
);
|
|
226
|
+
await enqueueIMSend(
|
|
227
|
+
content,
|
|
228
|
+
{
|
|
229
|
+
streamId: streamState.streamId,
|
|
230
|
+
seq,
|
|
231
|
+
isFinal: true,
|
|
232
|
+
delta,
|
|
233
|
+
},
|
|
234
|
+
undefined,
|
|
235
|
+
`final seq=${seq}`,
|
|
236
|
+
);
|
|
237
|
+
} else if (streamState.lastSentLength > 0) {
|
|
238
|
+
log(`${tag}: [DELIVER 流式最终] seq=${seq} contentType=array parts=${content.length} replaceStream=true`);
|
|
239
|
+
await enqueueIMSend(
|
|
240
|
+
content,
|
|
241
|
+
{
|
|
242
|
+
streamId: streamState.streamId,
|
|
243
|
+
seq,
|
|
244
|
+
isFinal: true,
|
|
245
|
+
delta: "",
|
|
246
|
+
},
|
|
247
|
+
undefined,
|
|
248
|
+
`final seq=${seq}`,
|
|
249
|
+
);
|
|
250
|
+
} else {
|
|
251
|
+
log(`${tag}: [DELIVER 非流式] contentType=array parts=${content.length}`);
|
|
252
|
+
await enqueueIMSend(content, undefined, undefined, "deliver");
|
|
253
|
+
}
|
|
235
254
|
} else {
|
|
236
255
|
log(`${tag}: [DELIVER 非流式] contentType=${typeof content === "string" ? "string" : "array"}`);
|
|
237
256
|
await enqueueIMSend(content, undefined, undefined, "deliver");
|
package/src/send.ts
CHANGED
|
@@ -6,7 +6,8 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import { tracer, trace, SpanStatusCode, buildTraceparentHeader } from "./tracing.js";
|
|
9
|
-
import
|
|
9
|
+
import { contentPartsToOpenAIContent } from "./content.js";
|
|
10
|
+
import type { SendToIMParams, OpenAIContent, TextContentPart } from "./types.js";
|
|
10
11
|
|
|
11
12
|
let msgSeq = 0;
|
|
12
13
|
|
|
@@ -28,8 +29,19 @@ export async function sendToPalzIM(params: SendToIMParams): Promise<any> {
|
|
|
28
29
|
});
|
|
29
30
|
}
|
|
30
31
|
|
|
32
|
+
function normalizeContent(content: OpenAIContent): OpenAIContent {
|
|
33
|
+
if (typeof content === "string") {
|
|
34
|
+
return contentPartsToOpenAIContent([{ type: "text", text: content } as TextContentPart]);
|
|
35
|
+
}
|
|
36
|
+
if (Array.isArray(content) && content.length === 1 && content[0].type === "text") {
|
|
37
|
+
return contentPartsToOpenAIContent(content);
|
|
38
|
+
}
|
|
39
|
+
return content;
|
|
40
|
+
}
|
|
41
|
+
|
|
31
42
|
async function _sendToPalzIMInner(params: SendToIMParams): Promise<any> {
|
|
32
|
-
const { config, conversationId, content, conversationType, msgId, senderId, stream, msgType, groupId, lobsterId, palzMsgType, toolContent, passthrough } = params;
|
|
43
|
+
const { config, conversationId, content: rawContent, conversationType, msgId, senderId, stream, msgType, groupId, lobsterId, palzMsgType, toolContent, passthrough } = params;
|
|
44
|
+
const content = normalizeContent(rawContent);
|
|
33
45
|
const url = `${config.apiBaseUrl}/bot/send`;
|
|
34
46
|
const resolvedMsgId = msgId || nextMsgId();
|
|
35
47
|
const span = trace.getActiveSpan();
|