opencode-cluade-auth 1.0.1 → 1.0.2
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/README.md +15 -0
- package/dist/anthropic-compat.d.ts +15 -0
- package/dist/anthropic-compat.js +176 -0
- package/dist/fetch.js +8 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -89,6 +89,21 @@ npm pack --dry-run
|
|
|
89
89
|
npm publish --dry-run
|
|
90
90
|
```
|
|
91
91
|
|
|
92
|
+
## GitHub Actions Publish
|
|
93
|
+
|
|
94
|
+
This repo includes a package-specific publish workflow at
|
|
95
|
+
`.github/workflows/publish-opencode-cluade-auth.yml`.
|
|
96
|
+
|
|
97
|
+
- trigger manually with `workflow_dispatch`, or push a tag matching
|
|
98
|
+
`opencode-cluade-auth-v*`
|
|
99
|
+
- the workflow hard-checks that `package.json` is publishing
|
|
100
|
+
`opencode-cluade-auth`
|
|
101
|
+
- npm auth is read only from the GitHub secret
|
|
102
|
+
`NPM_TOKEN_OPENCODE_CLAUDE_AUTH`
|
|
103
|
+
|
|
104
|
+
That secret is intended to be repo-scoped so it does not overlap with other
|
|
105
|
+
packages or repos such as `oh-my-aegis`.
|
|
106
|
+
|
|
92
107
|
## Limitations
|
|
93
108
|
|
|
94
109
|
- streaming responses are exposed as buffered Anthropic SSE, not live token passthrough
|
|
@@ -24,6 +24,11 @@ type ClaudeResponseEnvelope = {
|
|
|
24
24
|
arguments: Record<string, unknown>;
|
|
25
25
|
}>;
|
|
26
26
|
};
|
|
27
|
+
type ClaudeToolCall = {
|
|
28
|
+
id: string;
|
|
29
|
+
name: string;
|
|
30
|
+
arguments: Record<string, unknown>;
|
|
31
|
+
};
|
|
27
32
|
export declare function extractAnthropicRequest(raw: unknown): {
|
|
28
33
|
ok: true;
|
|
29
34
|
model: string;
|
|
@@ -43,6 +48,16 @@ export declare function buildClaudePreparedRequest(params: {
|
|
|
43
48
|
tools: AnthropicTool[];
|
|
44
49
|
}): ClaudePreparedRequest;
|
|
45
50
|
export declare function parseClaudeResponseEnvelope(text: string): ClaudeResponseEnvelope | null;
|
|
51
|
+
export declare function validateToolCalls(params: {
|
|
52
|
+
toolCalls: ClaudeToolCall[];
|
|
53
|
+
tools: AnthropicTool[];
|
|
54
|
+
}): {
|
|
55
|
+
ok: true;
|
|
56
|
+
toolCalls: ClaudeToolCall[];
|
|
57
|
+
} | {
|
|
58
|
+
ok: false;
|
|
59
|
+
reason: string;
|
|
60
|
+
};
|
|
46
61
|
export declare function buildAnthropicMessageResponse(params: {
|
|
47
62
|
model: string;
|
|
48
63
|
content: string;
|
package/dist/anthropic-compat.js
CHANGED
|
@@ -183,6 +183,182 @@ export function parseClaudeResponseEnvelope(text) {
|
|
|
183
183
|
});
|
|
184
184
|
return toolCalls.length > 0 ? { type: "tool-calls", tool_calls: toolCalls } : null;
|
|
185
185
|
}
|
|
186
|
+
function requiredFieldsFromSchema(schema) {
|
|
187
|
+
if (!schema) {
|
|
188
|
+
return [];
|
|
189
|
+
}
|
|
190
|
+
const required = schema.required;
|
|
191
|
+
if (!Array.isArray(required)) {
|
|
192
|
+
return [];
|
|
193
|
+
}
|
|
194
|
+
return required.filter((entry) => typeof entry === "string" && entry.trim() !== "");
|
|
195
|
+
}
|
|
196
|
+
function propertiesFromSchema(schema) {
|
|
197
|
+
if (!schema) {
|
|
198
|
+
return {};
|
|
199
|
+
}
|
|
200
|
+
const properties = schema.properties;
|
|
201
|
+
return isRecord(properties) ? properties : {};
|
|
202
|
+
}
|
|
203
|
+
function typeUnionFromSchema(schema) {
|
|
204
|
+
if (!schema) {
|
|
205
|
+
return [];
|
|
206
|
+
}
|
|
207
|
+
const type = schema.type;
|
|
208
|
+
if (typeof type === "string" && type.trim() !== "") {
|
|
209
|
+
return [type];
|
|
210
|
+
}
|
|
211
|
+
if (Array.isArray(type)) {
|
|
212
|
+
return type.filter((entry) => typeof entry === "string" && entry.trim() !== "");
|
|
213
|
+
}
|
|
214
|
+
return [];
|
|
215
|
+
}
|
|
216
|
+
function enumValuesFromSchema(schema) {
|
|
217
|
+
if (!schema) {
|
|
218
|
+
return null;
|
|
219
|
+
}
|
|
220
|
+
const values = schema.enum;
|
|
221
|
+
return Array.isArray(values) ? values : null;
|
|
222
|
+
}
|
|
223
|
+
function constValueFromSchema(schema) {
|
|
224
|
+
if (!schema) {
|
|
225
|
+
return { hasConst: false };
|
|
226
|
+
}
|
|
227
|
+
if (!Object.prototype.hasOwnProperty.call(schema, "const")) {
|
|
228
|
+
return { hasConst: false };
|
|
229
|
+
}
|
|
230
|
+
return { hasConst: true, value: schema.const };
|
|
231
|
+
}
|
|
232
|
+
function itemsFromSchema(schema) {
|
|
233
|
+
if (!schema) {
|
|
234
|
+
return undefined;
|
|
235
|
+
}
|
|
236
|
+
const items = schema.items;
|
|
237
|
+
return isRecord(items) ? items : undefined;
|
|
238
|
+
}
|
|
239
|
+
function additionalPropertiesIsFalse(schema) {
|
|
240
|
+
if (!schema) {
|
|
241
|
+
return false;
|
|
242
|
+
}
|
|
243
|
+
return schema.additionalProperties === false;
|
|
244
|
+
}
|
|
245
|
+
function matchesSchemaType(value, type) {
|
|
246
|
+
switch (type) {
|
|
247
|
+
case "string":
|
|
248
|
+
return typeof value === "string";
|
|
249
|
+
case "number":
|
|
250
|
+
return typeof value === "number" && Number.isFinite(value);
|
|
251
|
+
case "integer":
|
|
252
|
+
return typeof value === "number" && Number.isInteger(value);
|
|
253
|
+
case "boolean":
|
|
254
|
+
return typeof value === "boolean";
|
|
255
|
+
case "object":
|
|
256
|
+
return isRecord(value);
|
|
257
|
+
case "array":
|
|
258
|
+
return Array.isArray(value);
|
|
259
|
+
case "null":
|
|
260
|
+
return value === null;
|
|
261
|
+
default:
|
|
262
|
+
return true;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
function matchesSchemaTypes(value, types) {
|
|
266
|
+
if (types.length === 0) {
|
|
267
|
+
return true;
|
|
268
|
+
}
|
|
269
|
+
return types.some((type) => matchesSchemaType(value, type));
|
|
270
|
+
}
|
|
271
|
+
function isBlankString(value) {
|
|
272
|
+
return typeof value === "string" && value.trim() === "";
|
|
273
|
+
}
|
|
274
|
+
function hasRequiredFields(args, required) {
|
|
275
|
+
return required.every((field) => Object.prototype.hasOwnProperty.call(args, field) && args[field] !== undefined);
|
|
276
|
+
}
|
|
277
|
+
function validateValueAgainstSchema(value, schema, isRequired) {
|
|
278
|
+
if (!schema) {
|
|
279
|
+
return true;
|
|
280
|
+
}
|
|
281
|
+
const enumValues = enumValuesFromSchema(schema);
|
|
282
|
+
if (enumValues && !enumValues.some((entry) => Object.is(entry, value))) {
|
|
283
|
+
return false;
|
|
284
|
+
}
|
|
285
|
+
const constValue = constValueFromSchema(schema);
|
|
286
|
+
if (constValue.hasConst && !Object.is(constValue.value, value)) {
|
|
287
|
+
return false;
|
|
288
|
+
}
|
|
289
|
+
const types = typeUnionFromSchema(schema);
|
|
290
|
+
if (!matchesSchemaTypes(value, types)) {
|
|
291
|
+
return false;
|
|
292
|
+
}
|
|
293
|
+
if (isRequired && types.includes("string") && isBlankString(value)) {
|
|
294
|
+
return false;
|
|
295
|
+
}
|
|
296
|
+
const properties = propertiesFromSchema(schema);
|
|
297
|
+
const requiredFields = requiredFieldsFromSchema(schema);
|
|
298
|
+
const itemsSchema = itemsFromSchema(schema);
|
|
299
|
+
const hasAdditionalPropertiesConstraint = additionalPropertiesIsFalse(schema);
|
|
300
|
+
const expectsObject = hasAdditionalPropertiesConstraint || requiredFields.length > 0 || Object.keys(properties).length > 0;
|
|
301
|
+
const expectsArray = Boolean(itemsSchema);
|
|
302
|
+
if (Array.isArray(value)) {
|
|
303
|
+
if (!itemsSchema) {
|
|
304
|
+
return true;
|
|
305
|
+
}
|
|
306
|
+
return value.every((entry) => validateValueAgainstSchema(entry, itemsSchema, false));
|
|
307
|
+
}
|
|
308
|
+
if (isRecord(value)) {
|
|
309
|
+
if (!hasRequiredFields(value, requiredFields)) {
|
|
310
|
+
return false;
|
|
311
|
+
}
|
|
312
|
+
if (hasAdditionalPropertiesConstraint) {
|
|
313
|
+
for (const key of Object.keys(value)) {
|
|
314
|
+
if (!Object.prototype.hasOwnProperty.call(properties, key)) {
|
|
315
|
+
return false;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
for (const field of requiredFields) {
|
|
320
|
+
const propertySchema = isRecord(properties[field]) ? properties[field] : undefined;
|
|
321
|
+
const fieldTypes = typeUnionFromSchema(propertySchema);
|
|
322
|
+
if (fieldTypes.includes("string") && isBlankString(value[field])) {
|
|
323
|
+
return false;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
for (const [field, fieldValue] of Object.entries(value)) {
|
|
327
|
+
const propertySchema = isRecord(properties[field]) ? properties[field] : undefined;
|
|
328
|
+
if (!propertySchema) {
|
|
329
|
+
continue;
|
|
330
|
+
}
|
|
331
|
+
if (!validateValueAgainstSchema(fieldValue, propertySchema, requiredFields.includes(field))) {
|
|
332
|
+
return false;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
return true;
|
|
336
|
+
}
|
|
337
|
+
if (value === null && types.includes("null")) {
|
|
338
|
+
return true;
|
|
339
|
+
}
|
|
340
|
+
if (expectsObject || expectsArray) {
|
|
341
|
+
return false;
|
|
342
|
+
}
|
|
343
|
+
return true;
|
|
344
|
+
}
|
|
345
|
+
export function validateToolCalls(params) {
|
|
346
|
+
const toolsByName = new Map(params.tools.map((tool) => [tool.name, tool]));
|
|
347
|
+
for (const toolCall of params.toolCalls) {
|
|
348
|
+
const tool = toolsByName.get(toolCall.name);
|
|
349
|
+
if (!tool) {
|
|
350
|
+
return { ok: false, reason: `unknown tool requested: ${toolCall.name}` };
|
|
351
|
+
}
|
|
352
|
+
const required = requiredFieldsFromSchema(tool.inputSchema);
|
|
353
|
+
if (!hasRequiredFields(toolCall.arguments, required)) {
|
|
354
|
+
return { ok: false, reason: `missing required tool arguments for ${toolCall.name}` };
|
|
355
|
+
}
|
|
356
|
+
if (!validateValueAgainstSchema(toolCall.arguments, tool.inputSchema, true)) {
|
|
357
|
+
return { ok: false, reason: `invalid tool arguments for ${toolCall.name}` };
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
return { ok: true, toolCalls: params.toolCalls };
|
|
361
|
+
}
|
|
186
362
|
export function buildAnthropicMessageResponse(params) {
|
|
187
363
|
return new Response(JSON.stringify({
|
|
188
364
|
id: `msg_${Date.now()}`,
|
package/dist/fetch.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { buildAnthropicErrorResponse, buildAnthropicMessageResponse, buildAnthropicMessageStreamResponse, buildAnthropicToolUseResponse, buildAnthropicToolUseStreamResponse, buildClaudePreparedRequest, extractAnthropicRequest, parseClaudeResponseEnvelope, } from "./anthropic-compat.js";
|
|
1
|
+
import { buildAnthropicErrorResponse, buildAnthropicMessageResponse, buildAnthropicMessageStreamResponse, buildAnthropicToolUseResponse, buildAnthropicToolUseStreamResponse, buildClaudePreparedRequest, extractAnthropicRequest, parseClaudeResponseEnvelope, validateToolCalls, } from "./anthropic-compat.js";
|
|
2
2
|
import { runClaudeCode } from "./claude-cli.js";
|
|
3
3
|
function effortFromRequest(_raw) {
|
|
4
4
|
return undefined;
|
|
@@ -45,15 +45,20 @@ export function createClaudeCodeFetch(deps = {}) {
|
|
|
45
45
|
const responseText = result.responseText ?? "";
|
|
46
46
|
const envelope = parseClaudeResponseEnvelope(responseText);
|
|
47
47
|
if (envelope?.type === "tool-calls") {
|
|
48
|
+
const validation = validateToolCalls({ toolCalls: envelope.tool_calls, tools: request.tools });
|
|
49
|
+
if (!validation.ok) {
|
|
50
|
+
return buildAnthropicErrorResponse(502, validation.reason);
|
|
51
|
+
}
|
|
52
|
+
const toolCalls = validation.toolCalls;
|
|
48
53
|
if (request.stream) {
|
|
49
54
|
return buildAnthropicToolUseStreamResponse({
|
|
50
55
|
model: request.model,
|
|
51
|
-
toolCalls
|
|
56
|
+
toolCalls,
|
|
52
57
|
});
|
|
53
58
|
}
|
|
54
59
|
return buildAnthropicToolUseResponse({
|
|
55
60
|
model: request.model,
|
|
56
|
-
toolCalls
|
|
61
|
+
toolCalls,
|
|
57
62
|
});
|
|
58
63
|
}
|
|
59
64
|
if (request.stream) {
|