Skip to content

Commit b09bfd7

Browse files
committed
refactor(clickclack): reuse session URL contract
1 parent e14b36c commit b09bfd7

6 files changed

Lines changed: 11 additions & 149 deletions

File tree

‎docs/.generated/plugin-sdk-api-baseline.sha256‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ aa8a411ad37c1d1143b67376bf2d20255b9eedff61d80815f42e4f8ed7bd8e58 module/secret-
124124
dc0ee07d392a85c218939000b28c0138f139215da00f5592b34a68ba8e29a25d module/secret-ref-runtime
125125
f97549081955e412d8eb64070c64bb5921bb1324744d824db05767d3adcce403 module/security-runtime
126126
b18d65a8abbed79bcb77e34679b7aeb03f2e9101271f48298b47eb839c33a379 module/session-catalog
127-
50f5e344f98c27570b7a30e32a906b612e2383d21f102e88cd93e1d5425a6de9 module/session-discussion
127+
046b8f75735b5abeea15dde510f409e429b90f5220ee5c89275861f71fe19f55 module/session-discussion
128128
f112bdabc51ba8659b37d0a6f6a32a2b1d471e5b49b56e108bf750ec55a7ea71 module/session-store-runtime
129129
36affbe151431a6141664b6838e20f2d121ff210d57a3c1b4b41a8818b5c81d8 module/setup
130130
21071e8c2ef020685aba09b5661e37e7415938ff6602053fa831ac9d58673248 module/setup-runtime

‎docs/plugins/sdk-subpaths.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ Use `isLoopbackHost(host)` when a plugin must accept only the local machine. It
251251
| `plugin-sdk/agent-scope-runtime` | Focused agent ID, directory, default-agent, and session-agent scope resolution helpers for dependency-light control-plane and migration paths |
252252
| `plugin-sdk/session-store-runtime` | Session workflow helpers (`getSessionEntry`, `listSessionEntries`, `patchSessionEntry`, `upsertSessionEntry`), repair/lifecycle helpers (`deleteSessionEntry`, `cleanupSessionLifecycleArtifacts`, `resolveSessionStoreBackupPaths`), marker helpers for transitional `sessionFile` values, bounded recent user/assistant transcript text reads by session identity, session store path/session-key helpers, and updated-at reads, without broad config writes/maintenance imports |
253253
| `plugin-sdk/session-catalog` | External session catalog contracts, projections, adoption helpers, and history import |
254+
| `plugin-sdk/session-discussion` | External session discussion provider contracts, registration, and canonical Control UI session path building |
254255
| `plugin-sdk/session-transcript-runtime` | Private-local after July 2026; Transcript identity, bounded raw and visible cursors, scoped target/read/write helpers, visible message-entry projection, update publishing, write locks, and transcript memory hit keys |
255256
| `plugin-sdk/sqlite-runtime` | Private-local after July 2026; Focused SQLite agent-schema, path, and transaction helpers for first-party runtime, without database lifecycle controls |
256257
| `plugin-sdk/cron-store-runtime` | Private-local after July 2026; Cron store path/load/save helpers |

‎extensions/clickclack/src/discussions/control-session-url.ts‎

Lines changed: 3 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -1,144 +1,4 @@
1-
const SESSION_UUID_SUFFIX_RE = /([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})$/iu;
2-
const SHORT_SESSION_REF_RE = /^(?:.*-)?([0-9a-f]{8,32})$/iu;
3-
const VALID_AGENT_ID_RE = /^[a-z0-9][a-z0-9_-]{0,63}$/iu;
4-
const INVALID_AGENT_ID_CHARS_RE = /[^a-z0-9_-]+/giu;
5-
const FIXED_RESERVED_SESSION_RESTS = new Set(["main", "global", "boot", "sessions"]);
6-
const SESSION_SLUG_MAX_LENGTH = 48;
7-
const DEFAULT_AGENT_ID = "main";
8-
const DEFAULT_MAIN_KEY = "main";
9-
10-
function optionalString(value: string | undefined | null): string | null {
11-
const trimmed = value?.trim() ?? "";
12-
return trimmed || null;
13-
}
14-
15-
function normalizeAgentId(value: string): string {
16-
const trimmed = value.trim();
17-
if (!trimmed) {
18-
return DEFAULT_AGENT_ID;
19-
}
20-
if (VALID_AGENT_ID_RE.test(trimmed)) {
21-
return trimmed.toLowerCase();
22-
}
23-
return (
24-
trimmed
25-
.toLowerCase()
26-
.replace(INVALID_AGENT_ID_CHARS_RE, "-")
27-
.replace(/^-+|-+$/gu, "")
28-
.slice(0, 64) || DEFAULT_AGENT_ID
29-
);
30-
}
31-
32-
function normalizeBasePath(basePath: string): string {
33-
const trimmed = basePath.trim().replace(/^\/+|\/+$/gu, "");
34-
return trimmed ? `/${trimmed}` : "";
35-
}
36-
37-
function agentSessionKeyParts(sessionKey: string): { agentId: string; rest: string } | null {
38-
const parts = sessionKey.split(":");
39-
if (parts.length < 3 || parts[0]?.toLowerCase() !== "agent") {
40-
return null;
41-
}
42-
const agentId = optionalString(parts[1]);
43-
const restSegments = parts.slice(2);
44-
if (!agentId || restSegments.some((segment) => !segment)) {
45-
return null;
46-
}
47-
return { agentId: normalizeAgentId(agentId), rest: restSegments.join(":") };
48-
}
49-
50-
function encodePathSegment(segment: string): string {
51-
if (segment === ".") {
52-
return "~dot";
53-
}
54-
if (segment === "..") {
55-
return "~dotdot";
56-
}
57-
// encodeURIComponent leaves "." alone, so a key segment like "release.js" would
58-
// reach the server looking like a static asset request and never hit the SPA.
59-
// Mirrors packages/session-url-contract; keep both encoders identical.
60-
const encoded = encodeURIComponent(segment).replaceAll(".", "%2E");
61-
return encoded.startsWith("~") ? `~${encoded}` : encoded;
62-
}
63-
64-
function isReservedSessionRest(rest: string, mainKey: string | undefined): boolean {
65-
const normalized = rest.toLowerCase();
66-
return (
67-
FIXED_RESERVED_SESSION_RESTS.has(normalized) ||
68-
normalized === (optionalString(mainKey)?.toLowerCase() ?? DEFAULT_MAIN_KEY)
69-
);
70-
}
71-
72-
function sessionSlug(displayName: string | undefined): string {
73-
const tokens = (displayName ?? "")
74-
.toLowerCase()
75-
.replace(/[^a-z0-9]+/gu, "-")
76-
.replace(/^-+|-+$/gu, "")
77-
.split("-")
78-
.filter(Boolean);
79-
while (tokens.length > 0 && /^[0-9a-f]+$/u.test(tokens.at(-1) ?? "")) {
80-
tokens.pop();
81-
}
82-
return tokens.join("-").slice(0, SESSION_SLUG_MAX_LENGTH).replace(/-+$/gu, "");
83-
}
84-
85-
function sessionKeyUuid(sessionKey: string): string | null {
86-
const rest = agentSessionKeyParts(sessionKey)?.rest;
87-
const uuid = rest?.match(SESSION_UUID_SUFFIX_RE)?.[1];
88-
return uuid ? uuid.toLowerCase().replaceAll("-", "") : null;
89-
}
90-
91-
function sessionPath(params: {
92-
sessionKey: string;
93-
fallbackAgentId: string;
94-
basePath: string;
95-
displayName?: string;
96-
mainKey?: string;
97-
}): string | null {
98-
const rawKey = optionalString(params.sessionKey);
99-
const parsed = rawKey ? agentSessionKeyParts(rawKey) : null;
100-
const fallbackAgentId = optionalString(params.fallbackAgentId);
101-
const agentId = parsed?.agentId ?? (fallbackAgentId ? normalizeAgentId(fallbackAgentId) : null);
102-
if (!rawKey || !agentId || (!parsed && rawKey.toLowerCase().startsWith("agent:"))) {
103-
return null;
104-
}
105-
const namespace = `${normalizeBasePath(params.basePath)}/chat`;
106-
const encodedAgentId = encodePathSegment(agentId);
107-
const rest = parsed?.rest ?? rawKey;
108-
const normalizedRest = rest.toLowerCase();
109-
const mainKey = optionalString(params.mainKey)?.toLowerCase() ?? DEFAULT_MAIN_KEY;
110-
if (
111-
(!parsed && normalizedRest === DEFAULT_MAIN_KEY) ||
112-
normalizedRest === mainKey ||
113-
normalizedRest === "global"
114-
) {
115-
return `${namespace}/${encodedAgentId}`;
116-
}
117-
const uuid = parsed ? sessionKeyUuid(rawKey) : null;
118-
if (uuid) {
119-
let length = 8;
120-
const slug = sessionSlug(params.displayName);
121-
let sessionRef = `${slug ? `${slug}-` : ""}${uuid.slice(0, length)}`;
122-
while (length < uuid.length && isReservedSessionRest(sessionRef, params.mainKey)) {
123-
length += 1;
124-
sessionRef = `${slug ? `${slug}-` : ""}${uuid.slice(0, length)}`;
125-
}
126-
return isReservedSessionRest(sessionRef, params.mainKey)
127-
? null
128-
: `${namespace}/${encodedAgentId}/${sessionRef}`;
129-
}
130-
const segments = rest.split(":");
131-
if (segments.some((segment) => !segment)) {
132-
return null;
133-
}
134-
if (segments.length === 1) {
135-
const segment = segments[0] ?? "";
136-
if (!isReservedSessionRest(segment, params.mainKey) && SHORT_SESSION_REF_RE.test(segment)) {
137-
return `${namespace}/${encodedAgentId}/~key/${encodePathSegment(segment)}`;
138-
}
139-
}
140-
return `${namespace}/${encodedAgentId}/${segments.map(encodePathSegment).join("/")}`;
141-
}
1+
import { buildControlUiSessionPath } from "openclaw/plugin-sdk/session-discussion";
1422

1433
export function controlSessionUrl(
1444
baseUrl: string | undefined,
@@ -151,7 +11,8 @@ export function controlSessionUrl(
15111
return undefined;
15212
}
15313
const url = new URL(baseUrl);
154-
const path = sessionPath({
14+
const path = buildControlUiSessionPath({
15+
namespace: "chat",
15516
sessionKey,
15617
fallbackAgentId,
15718
basePath: url.pathname,

‎extensions/clickclack/src/discussions/service.test.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ type SessionUrlContractCase = {
1212
expectedPath: string | null;
1313
};
1414

15-
// Keep in sync with ui/src/app-navigation.test.ts. Core cannot import plugin internals,
16-
// and this independently published plugin cannot source-import the workspace contract.
15+
// Keep the independently published plugin wired to the host's canonical session URL contract.
1716
const SESSION_URL_CONTRACT_CASES = [
1817
{
1918
sessionKey: "agent:main:main",

‎scripts/plugin-sdk-surface-report.mjs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ export function readPluginSdkSurfaceBudgets(env = process.env) {
172172
),
173173
publicExports: readPluginSdkSurfaceBudgetEnv(
174174
"OPENCLAW_PLUGIN_SDK_MAX_PUBLIC_EXPORTS",
175-
// +4: session discussion state, info, provider, and registration contracts.
175+
// +5: session discussion state, info, provider, registration, and Control UI path contracts.
176176
// +2: structured media placeholder formatter and its text-fact contract.
177177
// +2: narrow settled-turn finalization result and safe full-attempt projector.
178178
// +1: channel-owned setup contract factory.
@@ -228,12 +228,12 @@ export function readPluginSdkSurfaceBudgets(env = process.env) {
228228
// +1: simple channel secret contract factory replacing repeated collectors.
229229
// +4: focused agent scope functions for doctor migration enumeration.
230230
// +1: shared transcript credential-safety prompt for plugin-owned agent harnesses.
231-
4840,
231+
4841,
232232
env,
233233
),
234234
publicFunctionExports: readPluginSdkSurfaceBudgetEnv(
235235
"OPENCLAW_PLUGIN_SDK_MAX_PUBLIC_FUNCTION_EXPORTS",
236-
// +1: session discussion provider registration.
236+
// +2: session discussion provider registration and canonical Control UI path building.
237237
// +1: structured media placeholder formatter for text-only channel carriers.
238238
// +1: settled-turn full-attempt projector.
239239
// +1: channel-owned setup contract factory.
@@ -280,7 +280,7 @@ export function readPluginSdkSurfaceBudgets(env = process.env) {
280280
// +1: shared channel interactive dispatcher with canonical binding authorization.
281281
// +1: simple channel secret contract factory replacing repeated collectors.
282282
// +4: focused agent scope functions for doctor migration enumeration.
283-
2916,
283+
2917,
284284
env,
285285
),
286286
publicDeprecatedExports: readPluginSdkSurfaceBudgetEnv(

‎src/plugin-sdk/session-discussion.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ export type {
33
SessionDiscussionProvider,
44
SessionDiscussionState,
55
} from "../plugins/session-discussion-registry.js";
6+
export { buildControlUiSessionPath } from "../../packages/session-url-contract/src/index.js";
67
export { registerSessionDiscussionProvider } from "../plugins/session-discussion-registry.js";

0 commit comments

Comments
 (0)