@enderfga/claw-orchestrator 3.0.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 (93) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +218 -0
  3. package/assets/banner.jpg +0 -0
  4. package/configs/council-reviewer-prompt.md +82 -0
  5. package/configs/council-system-prompt.md +141 -0
  6. package/dist/bin/cli.d.ts +13 -0
  7. package/dist/bin/cli.js +460 -0
  8. package/dist/bin/cli.js.map +1 -0
  9. package/dist/src/base-oneshot-session.d.ts +87 -0
  10. package/dist/src/base-oneshot-session.js +228 -0
  11. package/dist/src/base-oneshot-session.js.map +1 -0
  12. package/dist/src/circuit-breaker.d.ts +21 -0
  13. package/dist/src/circuit-breaker.js +49 -0
  14. package/dist/src/circuit-breaker.js.map +1 -0
  15. package/dist/src/consensus.d.ts +20 -0
  16. package/dist/src/consensus.js +52 -0
  17. package/dist/src/consensus.js.map +1 -0
  18. package/dist/src/constants.d.ts +129 -0
  19. package/dist/src/constants.js +138 -0
  20. package/dist/src/constants.js.map +1 -0
  21. package/dist/src/council.d.ts +67 -0
  22. package/dist/src/council.js +914 -0
  23. package/dist/src/council.js.map +1 -0
  24. package/dist/src/embedded-server.d.ts +25 -0
  25. package/dist/src/embedded-server.js +360 -0
  26. package/dist/src/embedded-server.js.map +1 -0
  27. package/dist/src/inbox-manager.d.ts +38 -0
  28. package/dist/src/inbox-manager.js +111 -0
  29. package/dist/src/inbox-manager.js.map +1 -0
  30. package/dist/src/index.d.ts +63 -0
  31. package/dist/src/index.js +973 -0
  32. package/dist/src/index.js.map +1 -0
  33. package/dist/src/logger.d.ts +16 -0
  34. package/dist/src/logger.js +44 -0
  35. package/dist/src/logger.js.map +1 -0
  36. package/dist/src/models.d.ts +69 -0
  37. package/dist/src/models.js +299 -0
  38. package/dist/src/models.js.map +1 -0
  39. package/dist/src/openai-compat.d.ts +224 -0
  40. package/dist/src/openai-compat.js +756 -0
  41. package/dist/src/openai-compat.js.map +1 -0
  42. package/dist/src/persistent-codex-app-session.d.ts +108 -0
  43. package/dist/src/persistent-codex-app-session.js +465 -0
  44. package/dist/src/persistent-codex-app-session.js.map +1 -0
  45. package/dist/src/persistent-codex-session.d.ts +37 -0
  46. package/dist/src/persistent-codex-session.js +208 -0
  47. package/dist/src/persistent-codex-session.js.map +1 -0
  48. package/dist/src/persistent-cursor-session.d.ts +21 -0
  49. package/dist/src/persistent-cursor-session.js +241 -0
  50. package/dist/src/persistent-cursor-session.js.map +1 -0
  51. package/dist/src/persistent-custom-session.d.ts +78 -0
  52. package/dist/src/persistent-custom-session.js +938 -0
  53. package/dist/src/persistent-custom-session.js.map +1 -0
  54. package/dist/src/persistent-gemini-session.d.ts +21 -0
  55. package/dist/src/persistent-gemini-session.js +216 -0
  56. package/dist/src/persistent-gemini-session.js.map +1 -0
  57. package/dist/src/persistent-session.d.ts +80 -0
  58. package/dist/src/persistent-session.js +745 -0
  59. package/dist/src/persistent-session.js.map +1 -0
  60. package/dist/src/proxy/anthropic-adapter.d.ts +136 -0
  61. package/dist/src/proxy/anthropic-adapter.js +392 -0
  62. package/dist/src/proxy/anthropic-adapter.js.map +1 -0
  63. package/dist/src/proxy/handler.d.ts +39 -0
  64. package/dist/src/proxy/handler.js +365 -0
  65. package/dist/src/proxy/handler.js.map +1 -0
  66. package/dist/src/proxy/schema-cleaner.d.ts +11 -0
  67. package/dist/src/proxy/schema-cleaner.js +34 -0
  68. package/dist/src/proxy/schema-cleaner.js.map +1 -0
  69. package/dist/src/proxy/thought-cache.d.ts +19 -0
  70. package/dist/src/proxy/thought-cache.js +53 -0
  71. package/dist/src/proxy/thought-cache.js.map +1 -0
  72. package/dist/src/session-manager.d.ts +317 -0
  73. package/dist/src/session-manager.js +1528 -0
  74. package/dist/src/session-manager.js.map +1 -0
  75. package/dist/src/types.d.ts +513 -0
  76. package/dist/src/types.js +8 -0
  77. package/dist/src/types.js.map +1 -0
  78. package/dist/src/validation.d.ts +31 -0
  79. package/dist/src/validation.js +104 -0
  80. package/dist/src/validation.js.map +1 -0
  81. package/openclaw.plugin.json +122 -0
  82. package/package.json +84 -0
  83. package/skills/SKILL.md +184 -0
  84. package/skills/references/claude-cli-tracking.md +25 -0
  85. package/skills/references/cli.md +187 -0
  86. package/skills/references/council.md +210 -0
  87. package/skills/references/getting-started.md +133 -0
  88. package/skills/references/inbox.md +81 -0
  89. package/skills/references/multi-engine.md +382 -0
  90. package/skills/references/openai-compat.md +203 -0
  91. package/skills/references/sessions.md +191 -0
  92. package/skills/references/tools.md +418 -0
  93. package/skills/references/ultra.md +126 -0
@@ -0,0 +1,224 @@
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
+ export interface OpenAIChatMessage {
10
+ role: 'system' | 'user' | 'assistant' | 'tool';
11
+ content: string | Array<{
12
+ type?: string;
13
+ text?: string;
14
+ }> | null;
15
+ tool_calls?: Array<{
16
+ id: string;
17
+ type: 'function';
18
+ function: {
19
+ name: string;
20
+ arguments: string;
21
+ };
22
+ }>;
23
+ tool_call_id?: string;
24
+ }
25
+ export interface OpenAIChatCompletionRequest {
26
+ model?: string;
27
+ messages: OpenAIChatMessage[];
28
+ stream?: boolean;
29
+ temperature?: number;
30
+ max_tokens?: number;
31
+ max_completion_tokens?: number;
32
+ user?: string;
33
+ tools?: Array<{
34
+ type: 'function';
35
+ function: {
36
+ name: string;
37
+ description: string;
38
+ parameters: unknown;
39
+ };
40
+ }>;
41
+ }
42
+ export interface OpenAIToolCall {
43
+ id: string;
44
+ type: 'function';
45
+ function: {
46
+ name: string;
47
+ arguments: string;
48
+ };
49
+ }
50
+ export interface OpenAIChatCompletionResponse {
51
+ id: string;
52
+ object: 'chat.completion';
53
+ created: number;
54
+ model: string;
55
+ choices: Array<{
56
+ index: number;
57
+ message: {
58
+ role: 'assistant';
59
+ content: string | null;
60
+ tool_calls?: OpenAIToolCall[];
61
+ };
62
+ finish_reason: 'stop' | 'length' | 'tool_calls';
63
+ }>;
64
+ usage: {
65
+ prompt_tokens: number;
66
+ completion_tokens: number;
67
+ total_tokens: number;
68
+ };
69
+ }
70
+ export interface OpenAIChatCompletionChunk {
71
+ id: string;
72
+ object: 'chat.completion.chunk';
73
+ created: number;
74
+ model: string;
75
+ choices: Array<{
76
+ index: number;
77
+ delta: {
78
+ role?: string;
79
+ content?: string | null;
80
+ tool_calls?: Array<{
81
+ index: number;
82
+ id?: string;
83
+ type?: 'function';
84
+ function?: {
85
+ name?: string;
86
+ arguments?: string;
87
+ };
88
+ }>;
89
+ };
90
+ finish_reason: string | null;
91
+ }>;
92
+ usage?: {
93
+ prompt_tokens: number;
94
+ completion_tokens: number;
95
+ total_tokens: number;
96
+ };
97
+ }
98
+ /**
99
+ * Derive a session key from the request.
100
+ * Priority: X-Session-Id header > user field > sha1(model + systemPrompt) > "default"
101
+ *
102
+ * The system-prompt-hash fallback prevents the bug where every caller without
103
+ * X-Session-Id or `user` collapses onto a single shared "openai-default"
104
+ * plugin session. In multi-caller setups (OpenClaw routing the main agent,
105
+ * cron jobs, and subagents through the same gateway) that previously meant
106
+ * every request serialized against every other and frequently picked up the
107
+ * wrong session's appendSystemPrompt — also a privacy leak across callers.
108
+ *
109
+ * The model is mixed into the hash so that two callers with the same system
110
+ * prompt but different requested models don't collide and silently get
111
+ * responses from the wrong model. Originally diagnosed in PR #40 by
112
+ * @megayounus786.
113
+ */
114
+ /**
115
+ * When set (to '1', 'true', 'yes'), the proxy preserves the pre-fix behavior:
116
+ * - tools injected into every user message
117
+ * - session key NOT fingerprinted by tools (same session across tool changes)
118
+ * Default (unset) is the new behavior: tools embedded in session system prompt
119
+ * at create time + session key fingerprinted by tools. The new behavior
120
+ * eliminates periodic latency spikes but does not support mutating the tool
121
+ * list within a single session (a new session is created when tools change).
122
+ */
123
+ export declare function isToolsPerMessageModeEnabled(): boolean;
124
+ /**
125
+ * Generate the "no built-in tools" system prompt preamble.
126
+ * The `toolLocation` parameter controls how the model is told where to find
127
+ * tool definitions — 'system' means "in the <available_tools> block below"
128
+ * (tools baked into system prompt), 'user' means "in <available_tools> tags
129
+ * in the user message" (legacy per-turn injection).
130
+ */
131
+ export declare function noToolsSystemPrompt(toolLocation: 'system' | 'user'): string;
132
+ /**
133
+ * Build the full session system prompt for a Claude Code session with tools.
134
+ * Exported for testability — called from `handleChatCompletion`.
135
+ *
136
+ * - Default mode: tools are embedded in the system prompt (cacheable by Anthropic).
137
+ * - Legacy mode (OPENAI_COMPAT_TOOLS_PER_MESSAGE=1): tools are NOT embedded;
138
+ * they'll be injected per-turn in the user message instead.
139
+ */
140
+ export declare function buildSessionSystemPrompt(tools: OpenAIChatCompletionRequest['tools'], callerSystemPrompt: string | undefined): string;
141
+ export declare function resolveSessionKey(body: OpenAIChatCompletionRequest, headers: http.IncomingHttpHeaders): string;
142
+ /** Build the full session name from a key */
143
+ export declare function sessionNameFromKey(key: string): string;
144
+ /**
145
+ * Convert OpenAI tool definitions into a structured prompt block.
146
+ * Injected into the user message so the CLI model sees tool definitions
147
+ * and responds with <tool_calls> tags when it wants to invoke a function.
148
+ */
149
+ export declare function buildToolPromptBlock(tools: OpenAIChatCompletionRequest['tools']): string;
150
+ export interface ParsedToolCalls {
151
+ textContent: string | null;
152
+ toolCalls: OpenAIToolCall[];
153
+ }
154
+ /**
155
+ * Parse tool_calls from CLI text output.
156
+ *
157
+ * Looks for <tool_calls>[...]</tool_calls> tags in the response text.
158
+ * Returns both the extracted text content (before/after tags) and any tool calls found.
159
+ */
160
+ export declare function parseToolCallsFromText(text: string): ParsedToolCalls;
161
+ /**
162
+ * Serialize tool result messages into a text block for the CLI model.
163
+ * Converts OpenAI `tool` role messages into <tool_result> tags.
164
+ */
165
+ export declare function serializeToolResults(messages: OpenAIChatMessage[]): string;
166
+ export interface ExtractedMessage {
167
+ systemPrompt: string | undefined;
168
+ userMessage: string;
169
+ isNewConversation: boolean;
170
+ }
171
+ /**
172
+ * Extract the relevant parts from an OpenAI messages array.
173
+ *
174
+ * Sessions are stateful — we only need the last user message. The tricky
175
+ * question is whether to start a fresh session or append to the existing one.
176
+ *
177
+ * Default mode (no env var): only honor an explicit `X-Session-Reset: 1`
178
+ * header. This is correct for clients that maintain their own conversation
179
+ * transcript and forward only the latest user turn (OpenClaw main agent
180
+ * loop, cron jobs, subagents). The previous heuristic
181
+ * (`nonSystemMessages.length <= 1`) fired on every such request, killing the
182
+ * persistent CLI every turn and preventing Anthropic prompt caching from
183
+ * ever warming. Originally diagnosed in PR #40 by @megayounus786.
184
+ *
185
+ * Legacy mode (`OPENAI_COMPAT_NEW_CONVO_HEURISTIC=1`): restore the old
186
+ * `system + single user ⇒ new conversation` rule, for clients that re-send
187
+ * the full transcript on every turn (ChatGPT-Next-Web, Open WebUI, data
188
+ * labeling tools, etc). They use the transcript shape itself as their only
189
+ * "start a new conversation" signal.
190
+ *
191
+ * The env var is read on every call so ops can flip it via launchctl setenv
192
+ * without restarting the server.
193
+ */
194
+ export declare function extractUserMessage(messages: OpenAIChatMessage[], headers?: Record<string, string | string[] | undefined>): ExtractedMessage;
195
+ export declare function formatCompletionResponse(id: string, model: string, text: string, tokensIn: number, tokensOut: number, toolCalls?: OpenAIToolCall[]): OpenAIChatCompletionResponse;
196
+ export declare function formatCompletionChunk(id: string, model: string, delta: {
197
+ role?: string;
198
+ content?: string;
199
+ }, finishReason: string | null): OpenAIChatCompletionChunk;
200
+ /** SessionManager-like interface to avoid circular imports */
201
+ interface SessionManagerLike {
202
+ startSession(config: Record<string, unknown>): Promise<{
203
+ name: string;
204
+ }>;
205
+ sendMessage(name: string, message: string, options?: Record<string, unknown>): Promise<{
206
+ output: string;
207
+ sessionId?: string;
208
+ events: unknown[];
209
+ }>;
210
+ stopSession(name: string): Promise<void>;
211
+ listSessions(): Array<{
212
+ name: string;
213
+ }>;
214
+ getStatus(name: string): {
215
+ stats: {
216
+ tokensIn: number;
217
+ tokensOut: number;
218
+ contextPercent: number;
219
+ };
220
+ };
221
+ compactSession(name: string): Promise<unknown>;
222
+ }
223
+ export declare function handleChatCompletion(manager: SessionManagerLike, body: Record<string, unknown>, headers: http.IncomingHttpHeaders, res: http.ServerResponse): Promise<void>;
224
+ export {};