mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 09:07:39 +00:00
refactor: share thread binding id parser
This commit is contained in:
43
src/channels/thread-binding-id.test.ts
Normal file
43
src/channels/thread-binding-id.test.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { resolveThreadBindingConversationIdFromBindingId } from "./thread-binding-id.js";
|
||||
|
||||
describe("resolveThreadBindingConversationIdFromBindingId", () => {
|
||||
it("returns the conversation id for matching account-prefixed binding ids", () => {
|
||||
expect(
|
||||
resolveThreadBindingConversationIdFromBindingId({
|
||||
accountId: "default",
|
||||
bindingId: "default:thread-123",
|
||||
}),
|
||||
).toBe("thread-123");
|
||||
});
|
||||
|
||||
it("returns undefined when binding id is missing or account prefix does not match", () => {
|
||||
expect(
|
||||
resolveThreadBindingConversationIdFromBindingId({
|
||||
accountId: "default",
|
||||
bindingId: undefined,
|
||||
}),
|
||||
).toBeUndefined();
|
||||
expect(
|
||||
resolveThreadBindingConversationIdFromBindingId({
|
||||
accountId: "default",
|
||||
bindingId: "work:thread-123",
|
||||
}),
|
||||
).toBeUndefined();
|
||||
});
|
||||
|
||||
it("trims whitespace and rejects empty ids after the account prefix", () => {
|
||||
expect(
|
||||
resolveThreadBindingConversationIdFromBindingId({
|
||||
accountId: "default",
|
||||
bindingId: " default:group-1:topic:99 ",
|
||||
}),
|
||||
).toBe("group-1:topic:99");
|
||||
expect(
|
||||
resolveThreadBindingConversationIdFromBindingId({
|
||||
accountId: "default",
|
||||
bindingId: "default: ",
|
||||
}),
|
||||
).toBeUndefined();
|
||||
});
|
||||
});
|
||||
15
src/channels/thread-binding-id.ts
Normal file
15
src/channels/thread-binding-id.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
export function resolveThreadBindingConversationIdFromBindingId(params: {
|
||||
accountId: string;
|
||||
bindingId?: string;
|
||||
}): string | undefined {
|
||||
const bindingId = params.bindingId?.trim();
|
||||
if (!bindingId) {
|
||||
return undefined;
|
||||
}
|
||||
const prefix = `${params.accountId}:`;
|
||||
if (!bindingId.startsWith(prefix)) {
|
||||
return undefined;
|
||||
}
|
||||
const conversationId = bindingId.slice(prefix.length).trim();
|
||||
return conversationId || undefined;
|
||||
}
|
||||
Reference in New Issue
Block a user