@clawchatsai/connector 0.1.19 → 0.1.20
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/dist/index.d.ts +7 -0
- package/dist/index.js +1 -0
- package/package.json +1 -1
- package/server/controllers/title.js +44 -0
- package/server/controllers/title.test.js +46 -0
- package/server/index.js +3 -0
package/dist/index.d.ts
CHANGED
|
@@ -60,6 +60,13 @@ interface PluginApi {
|
|
|
60
60
|
}) => void;
|
|
61
61
|
runtime: {
|
|
62
62
|
requestRestart?: (reason: string) => void;
|
|
63
|
+
llm?: {
|
|
64
|
+
complete: (params: Record<string, unknown>) => Promise<{
|
|
65
|
+
text?: string;
|
|
66
|
+
provider?: string;
|
|
67
|
+
model?: string;
|
|
68
|
+
}>;
|
|
69
|
+
};
|
|
63
70
|
};
|
|
64
71
|
config?: Record<string, unknown>;
|
|
65
72
|
}
|
package/dist/index.js
CHANGED
|
@@ -228,6 +228,7 @@ async function startClawChats(ctx, api) {
|
|
|
228
228
|
gatewayUrl: process.env.GATEWAY_WS_URL || 'ws://localhost:18789',
|
|
229
229
|
authToken: process.env.CLAWCHATS_AUTH_TOKEN || '', // P2P: DataChannel is the auth boundary
|
|
230
230
|
gatewayToken, // For WS auth to local OpenClaw gateway
|
|
231
|
+
llm: api.runtime?.llm, // host-run completions (chat-title fallback, /api/title)
|
|
231
232
|
openaiApiKey: (() => {
|
|
232
233
|
// Resolve OpenAI API key: openclaw config → env var
|
|
233
234
|
try {
|
package/package.json
CHANGED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { send, sendError, parseBody } from '../util/http.js';
|
|
2
|
+
|
|
3
|
+
// Chat-title fallback on the gateway's main model. ClawChats first asks the gateway's own
|
|
4
|
+
// titler (sessions.title.prepare), which only runs a utility model and returns null when the
|
|
5
|
+
// gateway has none (e.g. a claude-cli main model). The gateway's first-message titler falls
|
|
6
|
+
// back to the main model in that case; this endpoint does the same through the plugin's
|
|
7
|
+
// host-run completion (api.runtime.llm.complete), with the gateway's own title prompt.
|
|
8
|
+
export const TITLE_PROMPT = "Generate a concise session title (3-6 words, max 60 characters) from the user's first message. Use the same language as the message, in sentence case: capitalize only the first word and words that language always capitalizes. No emoji. Return only the title.";
|
|
9
|
+
const TITLE_MAX_CHARS = 60;
|
|
10
|
+
const SOURCE_MAX_CHARS = 1000;
|
|
11
|
+
const TIMEOUT_MS = 60_000;
|
|
12
|
+
|
|
13
|
+
// Port of the gateway's normalizeDashboardSessionTitle (src/gateway/dashboard-session-title.ts).
|
|
14
|
+
export function normalizeTitle(raw) {
|
|
15
|
+
const firstLine = String(raw || '').replace(/\r/g, '').split('\n').map(l => l.trim()).find(l => l && !l.startsWith('```'));
|
|
16
|
+
if (!firstLine) return null;
|
|
17
|
+
const normalized = firstLine.replace(/^\s*(?:title\s*:\s*)?/i, '').replace(/^["'`]+|["'`]+$/g, '').replace(/\s+/g, ' ').trim();
|
|
18
|
+
return normalized ? Array.from(normalized).slice(0, TITLE_MAX_CHARS).join('') : null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @param {{ complete?: (params: object) => Promise<{ text?: string }> }} llm api.runtime.llm
|
|
23
|
+
*/
|
|
24
|
+
export function createTitleHandler(llm) {
|
|
25
|
+
// Direct completion where the main model's provider has a direct transport; CLI and harness
|
|
26
|
+
// runtimes (claude-cli, codex, …) only run in isolated agent-runtime mode.
|
|
27
|
+
async function complete(message) {
|
|
28
|
+
const base = { messages: [{ role: 'user', content: message }], systemPrompt: TITLE_PROMPT, purpose: 'clawchats.title', maxTokens: 64 };
|
|
29
|
+
try {
|
|
30
|
+
return await llm.complete(base);
|
|
31
|
+
} catch (err) {
|
|
32
|
+
if (err?.code === 'LLM_COMPLETION_NOT_AUTHORIZED') throw err;
|
|
33
|
+
return await llm.complete({ ...base, maxTokens: undefined, execution: { mode: 'isolated-agent-runtime', timeoutMs: TIMEOUT_MS } });
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return async function handleTitle(req, res) {
|
|
38
|
+
if (typeof llm?.complete !== 'function') return sendError(res, 501, 'This OpenClaw version has no plugin model completion');
|
|
39
|
+
const { message } = await parseBody(req);
|
|
40
|
+
if (typeof message !== 'string' || !message.trim()) return sendError(res, 400, 'Missing message');
|
|
41
|
+
const result = await complete(Array.from(message.trim()).slice(0, SOURCE_MAX_CHARS).join(''));
|
|
42
|
+
return send(res, 200, { title: normalizeTitle(result?.text), model: result?.provider && result?.model ? `${result.provider}/${result.model}` : undefined });
|
|
43
|
+
};
|
|
44
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { test } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { Readable } from 'node:stream';
|
|
4
|
+
import { createTitleHandler, normalizeTitle, TITLE_PROMPT } from './title.js';
|
|
5
|
+
|
|
6
|
+
function call(handler, body) {
|
|
7
|
+
const req = Readable.from([Buffer.from(JSON.stringify(body))]);
|
|
8
|
+
return new Promise((resolve, reject) => {
|
|
9
|
+
const res = { writeHead(status) { this.status = status; }, end(data) { resolve({ status: this.status, body: JSON.parse(data) }); } };
|
|
10
|
+
handler(req, res).catch(reject);
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
test('normalizeTitle: first line, no label/quotes, max 60 chars', () => {
|
|
15
|
+
assert.equal(normalizeTitle('Title: "Fix sidebar flicker"\nmore'), 'Fix sidebar flicker');
|
|
16
|
+
assert.equal(normalizeTitle('```\n'), null);
|
|
17
|
+
assert.equal(normalizeTitle('x'.repeat(80)).length, 60);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
test('direct completion with the gateway title prompt', async () => {
|
|
21
|
+
const calls = [];
|
|
22
|
+
const handler = createTitleHandler({ complete: async p => { calls.push(p); return { text: 'Fix sidebar flicker', provider: 'anthropic', model: 'claude-haiku-4-5' }; } });
|
|
23
|
+
const r = await call(handler, { message: 'User: the sidebar flickers' });
|
|
24
|
+
assert.deepEqual(r, { status: 200, body: { title: 'Fix sidebar flicker', model: 'anthropic/claude-haiku-4-5' } });
|
|
25
|
+
assert.equal(calls.length, 1);
|
|
26
|
+
assert.equal(calls[0].systemPrompt, TITLE_PROMPT);
|
|
27
|
+
assert.deepEqual(calls[0].messages, [{ role: 'user', content: 'User: the sidebar flickers' }]);
|
|
28
|
+
assert.equal(calls[0].execution, undefined);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test('falls back to isolated agent-runtime mode when direct completion fails (CLI runtimes)', async () => {
|
|
32
|
+
const calls = [];
|
|
33
|
+
const handler = createTitleHandler({ complete: async p => { calls.push(p); if (!p.execution) throw Object.assign(new Error('no transport'), { code: 'LLM_COMPLETION_FAILED' }); return { text: 'Proxmox backups' }; } });
|
|
34
|
+
const r = await call(handler, { message: 'hi there' });
|
|
35
|
+
assert.equal(r.body.title, 'Proxmox backups');
|
|
36
|
+
assert.equal(calls[1].execution.mode, 'isolated-agent-runtime');
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test('authorization failures are not retried; bad input is rejected', async () => {
|
|
40
|
+
let n = 0;
|
|
41
|
+
const handler = createTitleHandler({ complete: async () => { n++; throw Object.assign(new Error('denied'), { code: 'LLM_COMPLETION_NOT_AUTHORIZED' }); } });
|
|
42
|
+
await assert.rejects(call(handler, { message: 'x' }), /denied/);
|
|
43
|
+
assert.equal(n, 1);
|
|
44
|
+
assert.equal((await call(handler, { message: ' ' })).status, 400);
|
|
45
|
+
assert.equal((await call(createTitleHandler(undefined), { message: 'x' })).status, 501);
|
|
46
|
+
});
|
package/server/index.js
CHANGED
|
@@ -13,6 +13,7 @@ import { handleTranscribe } from './controllers/transcribe.js';
|
|
|
13
13
|
import { createGatewayMediaHandler } from './controllers/gateway-media.js';
|
|
14
14
|
import { handleStatic } from './controllers/static.js';
|
|
15
15
|
import { handleAgents } from './controllers/agents.js';
|
|
16
|
+
import { createTitleHandler } from './controllers/title.js';
|
|
16
17
|
import { createSettingsHandlers } from './controllers/settings.js';
|
|
17
18
|
import { createWorkspaceStore } from './store/workspace-store.js';
|
|
18
19
|
import { createExtrasStore } from './store/extras-store.js';
|
|
@@ -36,6 +37,7 @@ export function createApp(config = {}) {
|
|
|
36
37
|
const gatewayToken = config.gatewayToken !== undefined ? config.gatewayToken : authToken;
|
|
37
38
|
const gatewayUrl = config.gatewayUrl || GATEWAY_WS_URL;
|
|
38
39
|
const openaiApiKey = config.openaiApiKey || null;
|
|
40
|
+
const handleTitle = createTitleHandler(config.llm); // api.runtime.llm (plugin mode only)
|
|
39
41
|
|
|
40
42
|
fs.mkdirSync(DATA_DIR, { recursive: true });
|
|
41
43
|
|
|
@@ -162,6 +164,7 @@ export function createApp(config = {}) {
|
|
|
162
164
|
if (method === 'POST' && urlPath === '/api/transcribe') return await handleTranscribe(req, res, { openaiApiKey });
|
|
163
165
|
if (method === 'GET' && urlPath === '/api/health') return send(res, 200, { ok: true, workspace: getWorkspaces().active, uptime: process.uptime() });
|
|
164
166
|
if (method === 'GET' && urlPath === '/api/agents') return handleAgents(req, res);
|
|
167
|
+
if (method === 'POST' && urlPath === '/api/title') return await handleTitle(req, res);
|
|
165
168
|
|
|
166
169
|
// ClawChats-only extras (see EXTRAS.md)
|
|
167
170
|
if (method === 'GET' && urlPath === '/api/extras/project-styles') {
|