opencode-cluade-auth 1.0.2 → 1.0.4
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/anthropic-compat.js +90 -22
- package/package.json +1 -1
package/dist/anthropic-compat.js
CHANGED
|
@@ -83,7 +83,8 @@ export function extractAnthropicRequest(raw) {
|
|
|
83
83
|
tools.push({
|
|
84
84
|
name,
|
|
85
85
|
description: asText(tool.description).trim() || undefined,
|
|
86
|
-
inputSchema: isRecord(tool.
|
|
86
|
+
inputSchema: (isRecord(tool.inputSchema) ? tool.inputSchema : undefined) ??
|
|
87
|
+
(isRecord(tool.input_schema) ? tool.input_schema : undefined),
|
|
87
88
|
});
|
|
88
89
|
}
|
|
89
90
|
}
|
|
@@ -136,40 +137,30 @@ export function buildClaudePreparedRequest(params) {
|
|
|
136
137
|
outputFormat: "text",
|
|
137
138
|
};
|
|
138
139
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
if (
|
|
142
|
-
|
|
143
|
-
}
|
|
144
|
-
let parsed;
|
|
145
|
-
try {
|
|
146
|
-
parsed = JSON.parse(trimmed);
|
|
147
|
-
}
|
|
148
|
-
catch {
|
|
149
|
-
return null;
|
|
150
|
-
}
|
|
151
|
-
if (isRecord(parsed) && isRecord(parsed.structured_output)) {
|
|
152
|
-
parsed = parsed.structured_output;
|
|
140
|
+
function coerceClaudeEnvelope(parsed) {
|
|
141
|
+
let candidate = parsed;
|
|
142
|
+
if (isRecord(candidate) && isRecord(candidate.structured_output)) {
|
|
143
|
+
candidate = candidate.structured_output;
|
|
153
144
|
}
|
|
154
|
-
if (isRecord(
|
|
145
|
+
if (isRecord(candidate) && typeof candidate.result === "string") {
|
|
155
146
|
return {
|
|
156
147
|
type: "final",
|
|
157
|
-
content:
|
|
148
|
+
content: candidate.result,
|
|
158
149
|
};
|
|
159
150
|
}
|
|
160
|
-
if (!isRecord(
|
|
151
|
+
if (!isRecord(candidate)) {
|
|
161
152
|
return null;
|
|
162
153
|
}
|
|
163
|
-
if (
|
|
154
|
+
if (candidate.type === "final") {
|
|
164
155
|
return {
|
|
165
156
|
type: "final",
|
|
166
|
-
content: asText(
|
|
157
|
+
content: asText(candidate.content),
|
|
167
158
|
};
|
|
168
159
|
}
|
|
169
|
-
if (
|
|
160
|
+
if (candidate.type !== "tool-calls" || !Array.isArray(candidate.tool_calls)) {
|
|
170
161
|
return null;
|
|
171
162
|
}
|
|
172
|
-
const toolCalls =
|
|
163
|
+
const toolCalls = candidate.tool_calls.flatMap((entry) => {
|
|
173
164
|
if (!isRecord(entry)) {
|
|
174
165
|
return [];
|
|
175
166
|
}
|
|
@@ -183,6 +174,83 @@ export function parseClaudeResponseEnvelope(text) {
|
|
|
183
174
|
});
|
|
184
175
|
return toolCalls.length > 0 ? { type: "tool-calls", tool_calls: toolCalls } : null;
|
|
185
176
|
}
|
|
177
|
+
function parseEnvelopeCandidate(candidate) {
|
|
178
|
+
let parsed;
|
|
179
|
+
try {
|
|
180
|
+
parsed = JSON.parse(candidate);
|
|
181
|
+
}
|
|
182
|
+
catch {
|
|
183
|
+
return null;
|
|
184
|
+
}
|
|
185
|
+
return coerceClaudeEnvelope(parsed);
|
|
186
|
+
}
|
|
187
|
+
function extractJsonObjectCandidates(text) {
|
|
188
|
+
const candidates = [];
|
|
189
|
+
let depth = 0;
|
|
190
|
+
let start = -1;
|
|
191
|
+
let inString = false;
|
|
192
|
+
let escape = false;
|
|
193
|
+
for (let index = 0; index < text.length; index += 1) {
|
|
194
|
+
const char = text[index] ?? "";
|
|
195
|
+
if (inString) {
|
|
196
|
+
if (escape) {
|
|
197
|
+
escape = false;
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
if (char === "\\") {
|
|
201
|
+
escape = true;
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
if (char === '"') {
|
|
205
|
+
inString = false;
|
|
206
|
+
}
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
if (char === '"') {
|
|
210
|
+
inString = true;
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
if (char === "{") {
|
|
214
|
+
if (depth === 0) {
|
|
215
|
+
start = index;
|
|
216
|
+
}
|
|
217
|
+
depth += 1;
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
if (char === "}") {
|
|
221
|
+
if (depth > 0) {
|
|
222
|
+
depth -= 1;
|
|
223
|
+
if (depth === 0 && start >= 0) {
|
|
224
|
+
candidates.push(text.slice(start, index + 1));
|
|
225
|
+
start = -1;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
return candidates;
|
|
231
|
+
}
|
|
232
|
+
export function parseClaudeResponseEnvelope(text) {
|
|
233
|
+
const trimmed = text.trim();
|
|
234
|
+
if (!trimmed) {
|
|
235
|
+
return null;
|
|
236
|
+
}
|
|
237
|
+
const direct = parseEnvelopeCandidate(trimmed);
|
|
238
|
+
if (direct) {
|
|
239
|
+
return direct;
|
|
240
|
+
}
|
|
241
|
+
const candidates = extractJsonObjectCandidates(trimmed);
|
|
242
|
+
for (let index = candidates.length - 1; index >= 0; index -= 1) {
|
|
243
|
+
const candidate = candidates[index];
|
|
244
|
+
if (!candidate || candidate === trimmed) {
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
const envelope = parseEnvelopeCandidate(candidate);
|
|
248
|
+
if (envelope) {
|
|
249
|
+
return envelope;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return null;
|
|
253
|
+
}
|
|
186
254
|
function requiredFieldsFromSchema(schema) {
|
|
187
255
|
if (!schema) {
|
|
188
256
|
return [];
|