@a1hvdy/cc-openclaw 0.5.2 → 0.7.0

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.
Files changed (98) hide show
  1. package/dist/src/command-router/cc-handler.js +72 -0
  2. package/dist/src/command-router/cc-handler.js.map +1 -1
  3. package/dist/src/constants.d.ts +9 -0
  4. package/dist/src/constants.js +10 -0
  5. package/dist/src/constants.js.map +1 -1
  6. package/dist/src/engines/persistent-session.d.ts +2 -0
  7. package/dist/src/engines/persistent-session.js +41 -11
  8. package/dist/src/engines/persistent-session.js.map +1 -1
  9. package/dist/src/lib/config.d.ts +2 -0
  10. package/dist/src/lib/config.js +19 -0
  11. package/dist/src/lib/config.js.map +1 -1
  12. package/dist/src/lib/sysprompt-strip.js +12 -12
  13. package/dist/src/lib/sysprompt-strip.js.map +1 -1
  14. package/dist/src/lib/trajectory.d.ts +1 -1
  15. package/dist/src/lib/trajectory.js.map +1 -1
  16. package/dist/src/lib/vendor-paths.d.ts +6 -4
  17. package/dist/src/lib/vendor-paths.js +21 -14
  18. package/dist/src/lib/vendor-paths.js.map +1 -1
  19. package/dist/src/openai-compat/openai-compat.d.ts +7 -1
  20. package/dist/src/openai-compat/openai-compat.js +8 -1
  21. package/dist/src/openai-compat/openai-compat.js.map +1 -1
  22. package/dist/src/openai-compat/sse-translator.d.ts +23 -3
  23. package/dist/src/openai-compat/sse-translator.js +45 -6
  24. package/dist/src/openai-compat/sse-translator.js.map +1 -1
  25. package/dist/src/session-bootstrap/cwd-patch.js +59 -28
  26. package/dist/src/session-bootstrap/cwd-patch.js.map +1 -1
  27. package/dist/src/types.d.ts +1 -0
  28. package/package.json +2 -3
  29. package/vendor/base-oneshot-session.d.ts +0 -87
  30. package/vendor/base-oneshot-session.js +0 -227
  31. package/vendor/base-oneshot-session.js.map +0 -1
  32. package/vendor/circuit-breaker.d.ts +0 -21
  33. package/vendor/circuit-breaker.js +0 -47
  34. package/vendor/circuit-breaker.js.map +0 -1
  35. package/vendor/consensus.d.ts +0 -20
  36. package/vendor/consensus.js +0 -52
  37. package/vendor/consensus.js.map +0 -1
  38. package/vendor/constants.d.ts +0 -130
  39. package/vendor/constants.js +0 -139
  40. package/vendor/constants.js.map +0 -1
  41. package/vendor/council.d.ts +0 -67
  42. package/vendor/council.js +0 -913
  43. package/vendor/council.js.map +0 -1
  44. package/vendor/embedded-server.d.ts +0 -25
  45. package/vendor/embedded-server.js +0 -373
  46. package/vendor/embedded-server.js.map +0 -1
  47. package/vendor/inbox-manager.d.ts +0 -38
  48. package/vendor/inbox-manager.js +0 -111
  49. package/vendor/inbox-manager.js.map +0 -1
  50. package/vendor/index.d.ts +0 -63
  51. package/vendor/index.js +0 -705
  52. package/vendor/index.js.map +0 -1
  53. package/vendor/logger.d.ts +0 -16
  54. package/vendor/logger.js +0 -44
  55. package/vendor/logger.js.map +0 -1
  56. package/vendor/models.d.ts +0 -69
  57. package/vendor/models.js +0 -289
  58. package/vendor/models.js.map +0 -1
  59. package/vendor/openai-compat.d.ts +0 -197
  60. package/vendor/openai-compat.js +0 -765
  61. package/vendor/openai-compat.js.map +0 -1
  62. package/vendor/persistent-codex-session.d.ts +0 -16
  63. package/vendor/persistent-codex-session.js +0 -105
  64. package/vendor/persistent-codex-session.js.map +0 -1
  65. package/vendor/persistent-cursor-session.d.ts +0 -21
  66. package/vendor/persistent-cursor-session.js +0 -241
  67. package/vendor/persistent-cursor-session.js.map +0 -1
  68. package/vendor/persistent-custom-session.d.ts +0 -78
  69. package/vendor/persistent-custom-session.js +0 -937
  70. package/vendor/persistent-custom-session.js.map +0 -1
  71. package/vendor/persistent-gemini-session.d.ts +0 -21
  72. package/vendor/persistent-gemini-session.js +0 -216
  73. package/vendor/persistent-gemini-session.js.map +0 -1
  74. package/vendor/persistent-session.d.ts +0 -74
  75. package/vendor/persistent-session.js +0 -684
  76. package/vendor/persistent-session.js.map +0 -1
  77. package/vendor/proxy/anthropic-adapter.d.ts +0 -136
  78. package/vendor/proxy/anthropic-adapter.js +0 -392
  79. package/vendor/proxy/anthropic-adapter.js.map +0 -1
  80. package/vendor/proxy/handler.d.ts +0 -39
  81. package/vendor/proxy/handler.js +0 -323
  82. package/vendor/proxy/handler.js.map +0 -1
  83. package/vendor/proxy/schema-cleaner.d.ts +0 -11
  84. package/vendor/proxy/schema-cleaner.js +0 -34
  85. package/vendor/proxy/schema-cleaner.js.map +0 -1
  86. package/vendor/proxy/thought-cache.d.ts +0 -19
  87. package/vendor/proxy/thought-cache.js +0 -53
  88. package/vendor/proxy/thought-cache.js.map +0 -1
  89. package/vendor/session-manager.d.ts +0 -211
  90. package/vendor/session-manager.js +0 -1345
  91. package/vendor/session-manager.js.map +0 -1
  92. package/vendor/skill-resolver.js +0 -107
  93. package/vendor/types.d.ts +0 -466
  94. package/vendor/types.js +0 -8
  95. package/vendor/types.js.map +0 -1
  96. package/vendor/validation.d.ts +0 -31
  97. package/vendor/validation.js +0 -104
  98. package/vendor/validation.js.map +0 -1
@@ -1,765 +0,0 @@
1
- /**
2
- * OpenAI-compatible /v1/chat/completions endpoint.
3
- *
4
- * Bridges OpenAI API format to persistent Claude Code sessions, enabling
5
- * webchat frontends (ChatGPT-Next-Web, Open WebUI, etc.) to use the plugin
6
- * as a drop-in backend. Stateful sessions maximize Anthropic prompt caching.
7
- */
8
- import * as http from 'node:http';
9
- import * as fs from 'node:fs';
10
- import * as path from 'node:path';
11
- import * as os from 'node:os';
12
- import { randomUUID, createHash } from 'node:crypto';
13
- import { resolveEngineAndModel } from './models.js';
14
- import { OPENAI_COMPAT_DEFAULT_MODEL, OPENAI_COMPAT_AUTO_COMPACT_THRESHOLD, OPENAI_COMPAT_SESSION_PREFIX, } from './constants.js';
15
- import { maybeInlineSkill } from './skill-resolver.js';
16
- // ─── Session Key Resolution ──────────────────────────────────────────────────
17
- /**
18
- * Derive a session key from the request.
19
- * Priority: X-Session-Id header > user field > sha1(model + systemPrompt) > "default"
20
- *
21
- * The system-prompt-hash fallback prevents the bug where every caller without
22
- * X-Session-Id or `user` collapses onto a single shared "openai-default"
23
- * plugin session. In multi-caller setups (OpenClaw routing the main agent,
24
- * cron jobs, and subagents through the same gateway) that previously meant
25
- * every request serialized against every other and frequently picked up the
26
- * wrong session's appendSystemPrompt — also a privacy leak across callers.
27
- *
28
- * The model is mixed into the hash so that two callers with the same system
29
- * prompt but different requested models don't collide and silently get
30
- * responses from the wrong model. Originally diagnosed in PR #40 by
31
- * @megayounus786.
32
- */
33
- export function resolveSessionKey(body, headers) {
34
- const headerKey = headers['x-session-id'];
35
- if (typeof headerKey === 'string' && headerKey.trim())
36
- return headerKey.trim();
37
- if (body.user && body.user.trim())
38
- return body.user.trim();
39
- const sys = (body.messages || [])
40
- .filter((m) => m && m.role === 'system')
41
- .map((m) => (typeof m.content === 'string' ? m.content : JSON.stringify(m.content)))
42
- .join('\n');
43
- const modelTag = (body.model || '').toString();
44
- if (sys || modelTag) {
45
- return ('sys-' +
46
- createHash('sha1')
47
- .update(modelTag + '\n' + sys)
48
- .digest('hex')
49
- .slice(0, 12));
50
- }
51
- return 'default';
52
- }
53
- /** Build the full session name from a key */
54
- export function sessionNameFromKey(key) {
55
- return `${OPENAI_COMPAT_SESSION_PREFIX}${key}`;
56
- }
57
- // ─── Function Calling Support ────────────────────────────────────────────────
58
- /**
59
- * Convert OpenAI tool definitions into a structured prompt block.
60
- * Injected into the user message so the CLI model sees tool definitions
61
- * and responds with <tool_calls> tags when it wants to invoke a function.
62
- */
63
- export function buildToolPromptBlock(tools) {
64
- if (!tools?.length)
65
- return '';
66
- const toolDefs = tools
67
- .map((t) => {
68
- const fn = t.function;
69
- const params = JSON.stringify(fn.parameters, null, 2);
70
- return `### ${fn.name}\n${fn.description}\n\nParameters:\n\`\`\`json\n${params}\n\`\`\``;
71
- })
72
- .join('\n\n');
73
- return ('<available_tools>\n' +
74
- 'You have access to the following tools. When you need to use a tool, respond with a JSON array wrapped in <tool_calls> tags.\n\n' +
75
- 'FORMAT:\n' +
76
- '<tool_calls>\n' +
77
- '[{"name": "tool_name", "arguments": {"param1": "value1"}}]\n' +
78
- '</tool_calls>\n\n' +
79
- 'If you do NOT need any tools, respond normally with text only (no <tool_calls> tags).\n\n' +
80
- '## Available Tools\n\n' +
81
- toolDefs +
82
- '\n</available_tools>');
83
- }
84
- /**
85
- * Parse tool_calls from CLI text output.
86
- *
87
- * Looks for <tool_calls>[...]</tool_calls> tags in the response text.
88
- * Returns both the extracted text content (before/after tags) and any tool calls found.
89
- */
90
- export function parseToolCallsFromText(text) {
91
- // Match ALL <tool_calls> blocks (model may output multiple)
92
- const tagRegex = /<tool_calls>\s*([\s\S]*?)\s*<\/tool_calls>/g;
93
- const allCalls = [];
94
- let lastIndex = 0;
95
- const textParts = [];
96
- let m;
97
- while ((m = tagRegex.exec(text)) !== null) {
98
- // Collect text before this block
99
- const before = text.slice(lastIndex, m.index).trim();
100
- if (before)
101
- textParts.push(before);
102
- lastIndex = m.index + m[0].length;
103
- try {
104
- const parsed = JSON.parse(m[1].trim());
105
- const arr = Array.isArray(parsed) ? parsed : [parsed];
106
- for (const raw of arr) {
107
- const call = raw;
108
- if (!call || typeof call !== 'object' || typeof call.name !== 'string')
109
- continue;
110
- let args;
111
- if (typeof call.arguments === 'string') {
112
- try {
113
- JSON.parse(call.arguments);
114
- args = call.arguments;
115
- }
116
- catch {
117
- args = JSON.stringify({ input: call.arguments });
118
- }
119
- }
120
- else {
121
- args = JSON.stringify(call.arguments ?? {});
122
- }
123
- allCalls.push({
124
- id: `call_${randomUUID().replace(/-/g, '').slice(0, 24)}`,
125
- type: 'function',
126
- function: { name: call.name, arguments: args },
127
- });
128
- }
129
- }
130
- catch {
131
- // One block failed — keep its text as content
132
- textParts.push(m[0]);
133
- }
134
- }
135
- // Collect text after last block
136
- const after = text.slice(lastIndex).trim();
137
- if (after)
138
- textParts.push(after);
139
- // Strip <tool_result> and <tool_results> tags that the model may echo back
140
- // from the serialized tool results we injected earlier.
141
- const stripToolResultTags = (s) => s
142
- .replace(/<tool_results?>[\s\S]*?<\/tool_results?>/g, '')
143
- .replace(/<tool_results?[^>]*>/g, '')
144
- .trim();
145
- if (allCalls.length > 0) {
146
- const raw = textParts.join('\n').trim();
147
- const cleaned = raw ? stripToolResultTags(raw) : null;
148
- return { textContent: cleaned || null, toolCalls: allCalls };
149
- }
150
- const cleaned = text ? stripToolResultTags(text) : null;
151
- return { textContent: cleaned || null, toolCalls: [] };
152
- }
153
- /**
154
- * Serialize tool result messages into a text block for the CLI model.
155
- * Converts OpenAI `tool` role messages into <tool_result> tags.
156
- */
157
- export function serializeToolResults(messages) {
158
- const toolMessages = messages.filter((m) => m.role === 'tool');
159
- if (!toolMessages.length)
160
- return '';
161
- const results = toolMessages
162
- .map((m) => {
163
- const content = typeof m.content === 'string' ? m.content : JSON.stringify(m.content);
164
- return `<tool_result tool_call_id="${m.tool_call_id || 'unknown'}">\n${content}\n</tool_result>`;
165
- })
166
- .join('\n\n');
167
- return `<tool_results>\n${results}\n</tool_results>\n\nAbove are the results of the tool calls you requested. Continue your response based on these results.`;
168
- }
169
- /**
170
- * Extract the relevant parts from an OpenAI messages array.
171
- *
172
- * Sessions are stateful — we only need the last user message. The tricky
173
- * question is whether to start a fresh session or append to the existing one.
174
- *
175
- * Default mode (no env var): only honor an explicit `X-Session-Reset: 1`
176
- * header. This is correct for clients that maintain their own conversation
177
- * transcript and forward only the latest user turn (OpenClaw main agent
178
- * loop, cron jobs, subagents). The previous heuristic
179
- * (`nonSystemMessages.length <= 1`) fired on every such request, killing the
180
- * persistent CLI every turn and preventing Anthropic prompt caching from
181
- * ever warming. Originally diagnosed in PR #40 by @megayounus786.
182
- *
183
- * Legacy mode (`OPENAI_COMPAT_NEW_CONVO_HEURISTIC=1`): restore the old
184
- * `system + single user ⇒ new conversation` rule, for clients that re-send
185
- * the full transcript on every turn (ChatGPT-Next-Web, Open WebUI, data
186
- * labeling tools, etc). They use the transcript shape itself as their only
187
- * "start a new conversation" signal.
188
- *
189
- * The env var is read on every call so ops can flip it via launchctl setenv
190
- * without restarting the server.
191
- */
192
- export function extractUserMessage(messages, headers) {
193
- if (!messages || messages.length === 0) {
194
- throw new Error('messages array is empty');
195
- }
196
- // Normalize content from any message: OpenAI API allows content as a string
197
- // OR an array of content parts (e.g. multimodal messages with text + images).
198
- // We need a string for the CLI, so arrays are joined.
199
- const textOf = (m) => {
200
- if (typeof m.content === 'string')
201
- return m.content;
202
- if (Array.isArray(m.content)) {
203
- return m.content
204
- .map((p) => p.text || '')
205
- .filter(Boolean)
206
- .join('');
207
- }
208
- return m.content != null ? String(m.content) : '';
209
- };
210
- // Extract system prompt if present
211
- const systemMessages = messages.filter((m) => m.role === 'system');
212
- const systemPrompt = systemMessages.length > 0 ? systemMessages.map(textOf).join('\n') : undefined;
213
- // Handle tool result messages — only when the LAST non-system message is
214
- // a tool role (meaning we're in an active tool-use cycle). If the last
215
- // message is a user role, it's a follow-up in an existing conversation
216
- // and the old tool results are already in the CLI's history.
217
- const lastNonSystem = [...messages].reverse().find((m) => m.role !== 'system');
218
- if (lastNonSystem?.role === 'tool') {
219
- const toolResultBlock = serializeToolResults(messages);
220
- const userMessages = messages.filter((m) => m.role === 'user');
221
- const lastUserText = userMessages.length > 0 ? textOf(userMessages[userMessages.length - 1]) : '';
222
- const userMessage = lastUserText ? `${toolResultBlock}\n\n${lastUserText}` : toolResultBlock;
223
- return { systemPrompt, userMessage, isNewConversation: false };
224
- }
225
- // Find user messages — concatenate all of them in order.
226
- //
227
- // 2026-05-07 fix for OpenClaw 2026.5.6 multi-user-message format:
228
- // 5.6 sends TWO user messages — [0]=actual user input as content blocks,
229
- // [1]=large system context + sender metadata as a string. The previous
230
- // `userMessages[length-1]` extraction picked up the metadata blob and
231
- // silently dropped the actual user input, causing bare prompts to appear
232
- // as empty questions to Claude (Claude responds with stale/recycled
233
- // context like "Good. What's up?" because no real question reached it).
234
- //
235
- // Fix: concatenate all user messages with double-newline separator. This
236
- // ensures the actual user input AND the conversation/runtime context
237
- // both reach Claude. The first message (typed text) leads, followed by
238
- // the metadata block as additional context.
239
- //
240
- // Backwards compat: when only one user message exists (older OpenClaw
241
- // versions or non-OpenClaw callers), this is identical to the old
242
- // single-message extraction.
243
- const userMessages = messages.filter((m) => m.role === 'user');
244
- if (userMessages.length === 0) {
245
- throw new Error('No user message found in messages array');
246
- }
247
- const userTexts = userMessages.map(textOf).filter((t) => t && t.length > 0);
248
- const rawUserMessage = userTexts.length > 1
249
- ? userTexts.join('\n\n')
250
- : (userTexts[0] || textOf(userMessages[userMessages.length - 1]));
251
- // Workspace skill auto-inline: if the user typed /<skillname> [args] and
252
- // ~/.openclaw/workspace/skills/*/SKILL.md has a matching `name:` frontmatter,
253
- // inline the SKILL.md body so the model has full skill context without
254
- // needing the Read tool (which cc-openclaw disables — see line 358).
255
- const userMessage = maybeInlineSkill(rawUserMessage) ?? rawUserMessage;
256
- // 1. Explicit reset header — honored in both modes. Normalize trim+lowercase
257
- // so callers using `TRUE`, ` 1 `, etc. don't silently fail.
258
- const rawReset = headers?.['x-session-reset'];
259
- const resetHeader = typeof rawReset === 'string' ? rawReset.trim().toLowerCase() : '';
260
- if (resetHeader === 'true' || resetHeader === '1') {
261
- return { systemPrompt, userMessage, isNewConversation: true };
262
- }
263
- // 2. Legacy heuristic — only when explicitly opted in via env var.
264
- if (process.env.OPENAI_COMPAT_NEW_CONVO_HEURISTIC === '1') {
265
- const nonSystemMessages = messages.filter((m) => m.role !== 'system');
266
- return { systemPrompt, userMessage, isNewConversation: nonSystemMessages.length <= 1 };
267
- }
268
- return { systemPrompt, userMessage, isNewConversation: false };
269
- }
270
- // ─── Response Formatting ─────────────────────────────────────────────────────
271
- export function formatCompletionResponse(id, model, text, tokensIn, tokensOut, toolCalls) {
272
- const hasToolCalls = toolCalls && toolCalls.length > 0;
273
- return {
274
- id,
275
- object: 'chat.completion',
276
- created: Math.floor(Date.now() / 1000),
277
- model,
278
- choices: [
279
- {
280
- index: 0,
281
- message: {
282
- role: 'assistant',
283
- content: text || null,
284
- ...(hasToolCalls ? { tool_calls: toolCalls } : {}),
285
- },
286
- finish_reason: hasToolCalls ? 'tool_calls' : 'stop',
287
- },
288
- ],
289
- usage: {
290
- prompt_tokens: tokensIn,
291
- completion_tokens: tokensOut,
292
- total_tokens: tokensIn + tokensOut,
293
- },
294
- };
295
- }
296
- export function formatCompletionChunk(id, model, delta, finishReason) {
297
- return {
298
- id,
299
- object: 'chat.completion.chunk',
300
- created: Math.floor(Date.now() / 1000),
301
- model,
302
- choices: [{ index: 0, delta, finish_reason: finishReason }],
303
- };
304
- }
305
- export async function handleChatCompletion(manager, body, headers, res) {
306
- // Validate before casting
307
- if (!body.messages || !Array.isArray(body.messages) || body.messages.length === 0) {
308
- res.writeHead(400, { 'Content-Type': 'application/json' });
309
- res.end(JSON.stringify({
310
- error: { message: 'messages is required and must be a non-empty array', type: 'invalid_request_error' },
311
- }));
312
- return;
313
- }
314
- // Safe cast: messages validated above, other fields are optional
315
- const request = {
316
- messages: body.messages,
317
- model: body.model,
318
- stream: body.stream,
319
- temperature: body.temperature,
320
- max_tokens: body.max_tokens,
321
- user: body.user,
322
- tools: body.tools,
323
- };
324
- // Validate max_tokens if provided
325
- if (request.max_tokens !== undefined && (typeof request.max_tokens !== 'number' || request.max_tokens <= 0)) {
326
- res.writeHead(400, { 'Content-Type': 'application/json' });
327
- res.end(JSON.stringify({
328
- error: { message: 'max_tokens must be a positive number', type: 'invalid_request_error' },
329
- }));
330
- return;
331
- }
332
- const modelStr = request.model || OPENAI_COMPAT_DEFAULT_MODEL;
333
- const { engine, model: resolvedModel } = resolveEngineAndModel(modelStr);
334
- const sessionKey = resolveSessionKey(request, headers);
335
- const sessionName = sessionNameFromKey(sessionKey);
336
- const isStreaming = request.stream === true;
337
- let extracted;
338
- try {
339
- extracted = extractUserMessage(request.messages, headers);
340
- }
341
- catch (err) {
342
- res.writeHead(400, { 'Content-Type': 'application/json' });
343
- res.end(JSON.stringify({ error: { message: err.message, type: 'invalid_request_error' } }));
344
- return;
345
- }
346
- // Check if session exists
347
- const existingSessions = manager.listSessions().map((s) => s.name);
348
- const sessionExists = existingSessions.includes(sessionName);
349
- // If new conversation detected and session exists, stop old one first
350
- if (extracted.isNewConversation && sessionExists) {
351
- try {
352
- await manager.stopSession(sessionName);
353
- }
354
- catch {
355
- /* session may have already been cleaned up */
356
- }
357
- }
358
- // Create session if needed
359
- const needsCreate = !sessionExists || extracted.isNewConversation;
360
- if (needsCreate) {
361
- // OpenAI-compat sessions are API proxies, not coding sessions.
362
- // Use a neutral empty temp dir so the CLI doesn't load CLAUDE.md,
363
- // git state, or project context from wherever `serve` was started.
364
- const sessionCwd = path.join(os.tmpdir(), `openclaw-compat-${sessionName}`);
365
- if (!fs.existsSync(sessionCwd))
366
- fs.mkdirSync(sessionCwd, { recursive: true });
367
- const sessionConfig = {
368
- name: sessionName,
369
- cwd: sessionCwd,
370
- engine,
371
- model: resolvedModel,
372
- permissionMode: 'bypassPermissions',
373
- // skipPersistence: tells SessionManager not to write this session to
374
- // the disk registry, preventing auto-resume of stale sessions.
375
- // Note: noSessionPersistence (--no-session-persistence) is NOT set
376
- // because some CLI forks don't support this flag.
377
- skipPersistence: true,
378
- };
379
- // Phase 2 R5: tool-stream mode flag (mirrors src/openai-compat/openai-compat.ts).
380
- // When CC_OPENCLAW_TOOL_STREAM=1 AND caller provides tools[], skip
381
- // both the sessionConfig.tools='' defensive clear AND the no-tools
382
- // system prompt — Claude CLI tool_use events flow through natively.
383
- const toolStreamMode = process.env.CC_OPENCLAW_TOOL_STREAM === '1';
384
- const useToolStreamPath = toolStreamMode && request.tools?.length;
385
- // Phase 2 R5+R3: tool-stream mode forwards the allowlist of tool names
386
- // from request.tools[] so Claude CLI knows which tools the model is
387
- // allowed to invoke. Legacy mode clears tools entirely.
388
- // v0.4.9 patch: CC_OPENCLAW_ALLOW_BUILTINS=1 short-circuits BOTH paths
389
- // so Claude CLI keeps its full built-in tool surface unrestricted.
390
- const __ccAllowBuiltinsForTools = process.env.CC_OPENCLAW_ALLOW_BUILTINS === '1';
391
- if (engine === 'claude' && !__ccAllowBuiltinsForTools) {
392
- if (useToolStreamPath) {
393
- const toolNames = request.tools
394
- .map((t) => t?.function?.name)
395
- .filter((n) => typeof n === 'string' && n.length > 0)
396
- .join(',');
397
- if (toolNames) sessionConfig.tools = toolNames;
398
- }
399
- else {
400
- sessionConfig.tools = '';
401
- }
402
- }
403
- // System prompt: legacy modes inject the defensive "no tools" or
404
- // "pure text" prompt. Tool-stream mode passes the caller's system
405
- // prompt through unchanged (Claude CLI gets native tools).
406
- // v0.4.9 patch: when CC_OPENCLAW_ALLOW_BUILTINS=1, ALL paths get an
407
- // affirmative tool prompt — the legacy "no tools" / "pure text" text
408
- // was being hallucinated/quoted by the model verbatim, suppressing
409
- // tool use even when the CLI was spawned with full built-ins.
410
- if (engine === 'claude') {
411
- const __ccAllowBuiltins = process.env.CC_OPENCLAW_ALLOW_BUILTINS === '1';
412
- const __ccAffirmation = 'You are an AI assistant with FULL Claude Code CLI tools available natively: Bash, Read, Write, Edit, Glob, Grep, WebFetch, WebSearch, NotebookEdit, plus gh, npm, pm2, openclaw, and any MCP-bridged tools (invoke via Bash). Use tools directly via your normal tool_use mechanism.\n' +
413
- 'CRITICAL — anti-hallucination: NEVER claim "I have no tools", "I cannot run commands", "I do not have access to this environment", "I am operating as a pure LLM behind an API proxy", or any similar phrasing. Those statements are FALSE in this runtime. If unsure whether a tool is available, ATTEMPT it; the runtime returns a concrete error if not.\n' +
414
- 'When a request needs action, ACT — do not narrate what you would do. Default to executing.\n' +
415
- '\n' +
416
- 'TURN-END INSIGHT (v0.5.2): For any turn involving reasoning, investigation, fixes, or non-trivial decisions, end your reply with a `★ Insight ─` fenced block summarizing 1–3 key takeaways the user should remember from this turn. Format EXACTLY:\n' +
417
- '\n' +
418
- '★ Insight ─────────────────────────────────────\n' +
419
- '[1–3 short concrete takeaways, one per line, dense and specific to THIS turn — not generic advice]\n' +
420
- '─────────────────────────────────────────────────\n' +
421
- '\n' +
422
- 'Skip the block ONLY for trivial turns (greetings, single-fact answers, "/new" greetings, command echoes). When in doubt, include it. The visual fence (★ + dashes) is parsed by the channel adapter to surface the takeaway as the live-card tip — keep the inner content under ~300 chars total so it fits the tip slot. Do NOT add markdown bold/headers inside the block; plain prose wins.';
423
- if (__ccAllowBuiltins) {
424
- sessionConfig.systemPrompt = extracted.systemPrompt
425
- ? `${__ccAffirmation}\n\n${extracted.systemPrompt}`
426
- : __ccAffirmation;
427
- }
428
- else if (useToolStreamPath) {
429
- if (extracted.systemPrompt) {
430
- sessionConfig.systemPrompt = extracted.systemPrompt;
431
- }
432
- }
433
- else if (request.tools?.length) {
434
- const noToolsPrompt = 'You are a helpful AI assistant acting as a pure LLM behind an API proxy.\n' +
435
- 'You do NOT have access to any tools such as Bash, Read, Write, Edit, Glob, Grep, or any other built-in tools.\n' +
436
- 'Do NOT attempt to call any tools or execute any commands.\n' +
437
- 'When you need to perform an action, use ONLY the tools defined in <available_tools> tags in the user message, ' +
438
- 'and respond with <tool_calls> tags as instructed there.\n' +
439
- 'If no <available_tools> are provided, respond with text only.';
440
- sessionConfig.systemPrompt = extracted.systemPrompt
441
- ? `${noToolsPrompt}\n\n${extracted.systemPrompt}`
442
- : noToolsPrompt;
443
- }
444
- else {
445
- const pureTextPrompt = 'You are a helpful AI assistant acting as a pure LLM behind an API proxy.\n' +
446
- 'You do NOT have access to any tools. Do NOT attempt to call any tools or execute any commands.\n' +
447
- 'Respond with text only.';
448
- sessionConfig.systemPrompt = extracted.systemPrompt
449
- ? `${pureTextPrompt}\n\n${extracted.systemPrompt}`
450
- : pureTextPrompt;
451
- }
452
- }
453
- try {
454
- await manager.startSession(sessionConfig);
455
- }
456
- catch (err) {
457
- res.writeHead(503, { 'Content-Type': 'application/json' });
458
- res.end(JSON.stringify({
459
- error: { message: `Failed to start session: ${err.message}`, type: 'server_error' },
460
- }));
461
- return;
462
- }
463
- }
464
- // Auto-compact if context is getting full
465
- if (sessionExists && !needsCreate) {
466
- try {
467
- const status = manager.getStatus(sessionName);
468
- if (status.stats.contextPercent > OPENAI_COMPAT_AUTO_COMPACT_THRESHOLD) {
469
- await manager.compactSession(sessionName);
470
- }
471
- }
472
- catch {
473
- /* best effort — session may not support compact */
474
- }
475
- }
476
- // For non-claude engines (Cursor, Codex, Gemini), their CLIs don't support
477
- // --append-system-prompt. Prepend the upstream system prompt to the user
478
- // message on EVERY turn so the model sees the caller's identity, tool
479
- // definitions, and workspace context. This is done here (not at session
480
- // creation) because these engines spawn a fresh CLI process per turn —
481
- // there's no persistent session to carry the system prompt forward.
482
- let userMessage = extracted.userMessage;
483
- if (extracted.systemPrompt && engine !== 'claude') {
484
- userMessage = `<system>\n${extracted.systemPrompt}\n</system>\n\n${userMessage}`;
485
- }
486
- // Inject tool definitions into the user message
487
- const hasTools = !!request.tools?.length;
488
- if (hasTools) {
489
- const toolBlock = buildToolPromptBlock(request.tools);
490
- userMessage = `${toolBlock}\n\n${userMessage}`;
491
- }
492
- const completionId = `chatcmpl-${randomUUID().replace(/-/g, '').slice(0, 29)}`;
493
- if (isStreaming) {
494
- await handleStreaming(manager, sessionName, resolvedModel, userMessage, completionId, res, hasTools);
495
- }
496
- else {
497
- await handleNonStreaming(manager, sessionName, resolvedModel, userMessage, completionId, res, hasTools);
498
- }
499
- // Clean up ephemeral sessions immediately after response.
500
- // When X-Session-Reset is set, each request creates a fresh session that
501
- // should not persist — leaving it alive leaks CLI subprocesses until TTL.
502
- if (extracted.isNewConversation) {
503
- manager.stopSession(sessionName).catch(() => { });
504
- }
505
- }
506
- // ─── Status Reporting ───────────────────────────────────────────────────────
507
- // Push tool/thinking status to an external webhook so a webchat status bar
508
- // can show what the CLI agent is doing. Best-effort fire-and-forget.
509
- /**
510
- * Optional status webhook — set `OPENAI_COMPAT_STATUS_URL` to an HTTP endpoint
511
- * that accepts `POST { state, activity, tool }`. The bridge will fire-and-forget
512
- * status updates when the CLI agent uses tools, so an external dashboard (e.g.
513
- * a webchat status bar) can show real-time progress.
514
- *
515
- * Example: `OPENAI_COMPAT_STATUS_URL=http://127.0.0.1:18795/my-app/agent-status`
516
- */
517
- function reportStatus(state, activity, tool) {
518
- const url = process.env.OPENAI_COMPAT_STATUS_URL;
519
- if (!url)
520
- return;
521
- const payload = JSON.stringify({ state, activity, tool: tool || null });
522
- const req = http.request(url, {
523
- method: 'POST',
524
- headers: { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(payload) },
525
- timeout: 2000,
526
- }, () => { });
527
- req.on('error', () => { });
528
- req.write(payload);
529
- req.end();
530
- }
531
- function getToolDescription(toolName, toolInput) {
532
- switch (toolName) {
533
- case 'Bash':
534
- case 'exec': {
535
- const cmd = String(toolInput?.command || '');
536
- return `Running: ${cmd.length > 50 ? cmd.slice(0, 50) + '...' : cmd}`;
537
- }
538
- case 'Read':
539
- case 'read':
540
- return `Reading: ${String(toolInput?.file_path || toolInput?.path || 'file')
541
- .split('/')
542
- .pop()}`;
543
- case 'Write':
544
- case 'write':
545
- return `Writing: ${String(toolInput?.file_path || toolInput?.path || 'file')
546
- .split('/')
547
- .pop()}`;
548
- case 'Edit':
549
- case 'edit':
550
- return `Editing: ${String(toolInput?.file_path || toolInput?.path || 'file')
551
- .split('/')
552
- .pop()}`;
553
- case 'Glob':
554
- case 'glob':
555
- return `Searching files: ${String(toolInput?.pattern || '')}`;
556
- case 'Grep':
557
- case 'grep':
558
- return `Searching content: ${String(toolInput?.pattern || '')}`;
559
- case 'WebSearch':
560
- return `Web search: ${String(toolInput?.query || '')}`;
561
- case 'Agent':
562
- return `Spawning sub-agent...`;
563
- default:
564
- return `Using tool: ${toolName}`;
565
- }
566
- }
567
- // ─── Non-Streaming ───────────────────────────────────────────────────────────
568
- async function handleNonStreaming(manager, sessionName, model, userMessage, completionId, res, hasTools) {
569
- try {
570
- reportStatus('thinking', 'Processing request...');
571
- const result = await manager.sendMessage(sessionName, userMessage, {
572
- onEvent: (event) => {
573
- if (event.type === 'tool_use' && event.tool?.name) {
574
- const desc = getToolDescription(event.tool.name, event.tool.input);
575
- reportStatus('working', desc, event.tool.name);
576
- }
577
- },
578
- });
579
- reportStatus('idle', 'Ready');
580
- let tokensIn = 0;
581
- let tokensOut = 0;
582
- try {
583
- const status = manager.getStatus(sessionName);
584
- tokensIn = status.stats.tokensIn;
585
- tokensOut = status.stats.tokensOut;
586
- }
587
- catch {
588
- /* stats unavailable */
589
- }
590
- // Parse tool_calls from response text when caller provided tools
591
- if (hasTools) {
592
- const parsed = parseToolCallsFromText(result.output);
593
- const response = formatCompletionResponse(completionId, model, parsed.textContent ?? '', tokensIn, tokensOut, parsed.toolCalls.length > 0 ? parsed.toolCalls : undefined);
594
- res.writeHead(200, { 'Content-Type': 'application/json' });
595
- res.end(JSON.stringify(response));
596
- }
597
- else {
598
- const response = formatCompletionResponse(completionId, model, result.output, tokensIn, tokensOut);
599
- res.writeHead(200, { 'Content-Type': 'application/json' });
600
- res.end(JSON.stringify(response));
601
- }
602
- }
603
- catch (err) {
604
- reportStatus('idle', 'Request failed');
605
- res.writeHead(500, { 'Content-Type': 'application/json' });
606
- res.end(JSON.stringify({ error: { message: err.message, type: 'server_error' } }));
607
- }
608
- }
609
- // ─── Streaming ───────────────────────────────────────────────────────────────
610
- async function handleStreaming(manager, sessionName, model, userMessage, completionId, res, hasTools) {
611
- res.writeHead(200, {
612
- 'Content-Type': 'text/event-stream',
613
- 'Cache-Control': 'no-cache',
614
- Connection: 'keep-alive',
615
- 'X-Accel-Buffering': 'no',
616
- });
617
- let clientDisconnected = false;
618
- res.on('close', () => {
619
- clientDisconnected = true;
620
- });
621
- const writeSSE = (data) => {
622
- if (!clientDisconnected) {
623
- try {
624
- res.write(`data: ${data}\n\n`);
625
- }
626
- catch {
627
- clientDisconnected = true;
628
- }
629
- }
630
- };
631
- // Initial chunk with role
632
- writeSSE(JSON.stringify(formatCompletionChunk(completionId, model, { role: 'assistant' }, null)));
633
- // SSE keepalive heartbeat
634
- const heartbeatTimer = setInterval(() => {
635
- if (!clientDisconnected) {
636
- try {
637
- res.write(': keepalive\n\n');
638
- }
639
- catch {
640
- clientDisconnected = true;
641
- }
642
- }
643
- }, 30_000);
644
- // When tools are present, buffer the full response to parse for tool_calls.
645
- // Without tools, stream text chunks directly for low latency.
646
- let bufferedText = '';
647
- let streamedAnything = false;
648
- try {
649
- reportStatus('thinking', 'Processing request...');
650
- const sendResult = await manager.sendMessage(sessionName, userMessage, {
651
- onChunk: (chunk) => {
652
- if (hasTools) {
653
- bufferedText += chunk;
654
- }
655
- else {
656
- streamedAnything = true;
657
- writeSSE(JSON.stringify(formatCompletionChunk(completionId, model, { content: chunk }, null)));
658
- }
659
- },
660
- onEvent: (event) => {
661
- if (event.type === 'tool_use' && event.tool?.name) {
662
- reportStatus('working', getToolDescription(event.tool.name, event.tool.input), event.tool.name);
663
- }
664
- },
665
- });
666
- // Fallback: if NOTHING reached the client (no streamed chunks AND no
667
- // tools-buffered text) but sendResult has output, emit it. Catches the
668
- // case where text comes only via the turn-complete event (e.g. after
669
- // internal tool calls) and was never delivered via onChunk.
670
- //
671
- // Bug fix 2026-04-29: previously this condition was just
672
- // `!bufferedText && !hasTools && sendResult.output`, which was
673
- // ALWAYS true for no-tools turns (bufferedText is only populated
674
- // when hasTools). Result: every no-tools turn re-emitted the full
675
- // sendResult.output as a SECOND SSE chunk after the streamed chunks
676
- // had already delivered the same text → doubled greeting on /new.
677
- // Adding `streamedAnything` to the condition only fires the fallback
678
- // when truly nothing reached the client.
679
- if (!streamedAnything && !bufferedText && !hasTools && sendResult?.output) {
680
- writeSSE(JSON.stringify(formatCompletionChunk(completionId, model, { content: sendResult.output }, null)));
681
- }
682
- reportStatus('idle', 'Ready');
683
- // Get token usage for final chunk
684
- let usage;
685
- try {
686
- const status = manager.getStatus(sessionName);
687
- usage = {
688
- prompt_tokens: status.stats.tokensIn,
689
- completion_tokens: status.stats.tokensOut,
690
- total_tokens: status.stats.tokensIn + status.stats.tokensOut,
691
- };
692
- }
693
- catch {
694
- /* best effort */
695
- }
696
- if (hasTools && bufferedText) {
697
- const parsed = parseToolCallsFromText(bufferedText);
698
- if (parsed.toolCalls.length > 0) {
699
- // Emit text content if any
700
- if (parsed.textContent) {
701
- writeSSE(JSON.stringify(formatCompletionChunk(completionId, model, { content: parsed.textContent }, null)));
702
- }
703
- // Emit tool_call chunks
704
- for (let i = 0; i < parsed.toolCalls.length; i++) {
705
- const tc = parsed.toolCalls[i];
706
- writeSSE(JSON.stringify({
707
- id: completionId,
708
- object: 'chat.completion.chunk',
709
- created: Math.floor(Date.now() / 1000),
710
- model,
711
- choices: [
712
- {
713
- index: 0,
714
- delta: {
715
- tool_calls: [
716
- {
717
- index: i,
718
- id: tc.id,
719
- type: 'function',
720
- function: { name: tc.function.name, arguments: tc.function.arguments },
721
- },
722
- ],
723
- },
724
- finish_reason: null,
725
- },
726
- ],
727
- }));
728
- }
729
- // Final chunk with tool_calls finish reason
730
- const finalChunk = formatCompletionChunk(completionId, model, {}, 'tool_calls');
731
- if (usage)
732
- finalChunk.usage = usage;
733
- writeSSE(JSON.stringify(finalChunk));
734
- }
735
- else {
736
- // No tool calls — emit buffered text as content
737
- writeSSE(JSON.stringify(formatCompletionChunk(completionId, model, { content: bufferedText }, null)));
738
- const finalChunk = formatCompletionChunk(completionId, model, {}, 'stop');
739
- if (usage)
740
- finalChunk.usage = usage;
741
- writeSSE(JSON.stringify(finalChunk));
742
- }
743
- }
744
- else {
745
- // No tools — standard finish
746
- const finalChunk = formatCompletionChunk(completionId, model, {}, 'stop');
747
- if (usage)
748
- finalChunk.usage = usage;
749
- writeSSE(JSON.stringify(finalChunk));
750
- }
751
- writeSSE('[DONE]');
752
- }
753
- catch (err) {
754
- reportStatus('idle', 'Request failed');
755
- writeSSE(JSON.stringify({ error: { message: err.message, type: 'server_error' } }));
756
- writeSSE('[DONE]');
757
- }
758
- finally {
759
- clearInterval(heartbeatTimer);
760
- }
761
- if (!clientDisconnected) {
762
- res.end();
763
- }
764
- }
765
- //# sourceMappingURL=openai-compat.js.map