opencode-cluade-auth 1.0.1 → 1.0.3

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 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;
@@ -136,40 +136,30 @@ export function buildClaudePreparedRequest(params) {
136
136
  outputFormat: "text",
137
137
  };
138
138
  }
139
- export function parseClaudeResponseEnvelope(text) {
140
- const trimmed = text.trim();
141
- if (!trimmed) {
142
- return null;
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;
139
+ function coerceClaudeEnvelope(parsed) {
140
+ let candidate = parsed;
141
+ if (isRecord(candidate) && isRecord(candidate.structured_output)) {
142
+ candidate = candidate.structured_output;
153
143
  }
154
- if (isRecord(parsed) && typeof parsed.result === "string") {
144
+ if (isRecord(candidate) && typeof candidate.result === "string") {
155
145
  return {
156
146
  type: "final",
157
- content: parsed.result,
147
+ content: candidate.result,
158
148
  };
159
149
  }
160
- if (!isRecord(parsed)) {
150
+ if (!isRecord(candidate)) {
161
151
  return null;
162
152
  }
163
- if (parsed.type === "final") {
153
+ if (candidate.type === "final") {
164
154
  return {
165
155
  type: "final",
166
- content: asText(parsed.content),
156
+ content: asText(candidate.content),
167
157
  };
168
158
  }
169
- if (parsed.type !== "tool-calls" || !Array.isArray(parsed.tool_calls)) {
159
+ if (candidate.type !== "tool-calls" || !Array.isArray(candidate.tool_calls)) {
170
160
  return null;
171
161
  }
172
- const toolCalls = parsed.tool_calls.flatMap((entry) => {
162
+ const toolCalls = candidate.tool_calls.flatMap((entry) => {
173
163
  if (!isRecord(entry)) {
174
164
  return [];
175
165
  }
@@ -183,6 +173,259 @@ export function parseClaudeResponseEnvelope(text) {
183
173
  });
184
174
  return toolCalls.length > 0 ? { type: "tool-calls", tool_calls: toolCalls } : null;
185
175
  }
176
+ function parseEnvelopeCandidate(candidate) {
177
+ let parsed;
178
+ try {
179
+ parsed = JSON.parse(candidate);
180
+ }
181
+ catch {
182
+ return null;
183
+ }
184
+ return coerceClaudeEnvelope(parsed);
185
+ }
186
+ function extractJsonObjectCandidates(text) {
187
+ const candidates = [];
188
+ let depth = 0;
189
+ let start = -1;
190
+ let inString = false;
191
+ let escape = false;
192
+ for (let index = 0; index < text.length; index += 1) {
193
+ const char = text[index] ?? "";
194
+ if (inString) {
195
+ if (escape) {
196
+ escape = false;
197
+ continue;
198
+ }
199
+ if (char === "\\") {
200
+ escape = true;
201
+ continue;
202
+ }
203
+ if (char === '"') {
204
+ inString = false;
205
+ }
206
+ continue;
207
+ }
208
+ if (char === '"') {
209
+ inString = true;
210
+ continue;
211
+ }
212
+ if (char === "{") {
213
+ if (depth === 0) {
214
+ start = index;
215
+ }
216
+ depth += 1;
217
+ continue;
218
+ }
219
+ if (char === "}") {
220
+ if (depth > 0) {
221
+ depth -= 1;
222
+ if (depth === 0 && start >= 0) {
223
+ candidates.push(text.slice(start, index + 1));
224
+ start = -1;
225
+ }
226
+ }
227
+ }
228
+ }
229
+ return candidates;
230
+ }
231
+ export function parseClaudeResponseEnvelope(text) {
232
+ const trimmed = text.trim();
233
+ if (!trimmed) {
234
+ return null;
235
+ }
236
+ const direct = parseEnvelopeCandidate(trimmed);
237
+ if (direct) {
238
+ return direct;
239
+ }
240
+ const candidates = extractJsonObjectCandidates(trimmed);
241
+ for (let index = candidates.length - 1; index >= 0; index -= 1) {
242
+ const candidate = candidates[index];
243
+ if (!candidate || candidate === trimmed) {
244
+ continue;
245
+ }
246
+ const envelope = parseEnvelopeCandidate(candidate);
247
+ if (envelope) {
248
+ return envelope;
249
+ }
250
+ }
251
+ return null;
252
+ }
253
+ function requiredFieldsFromSchema(schema) {
254
+ if (!schema) {
255
+ return [];
256
+ }
257
+ const required = schema.required;
258
+ if (!Array.isArray(required)) {
259
+ return [];
260
+ }
261
+ return required.filter((entry) => typeof entry === "string" && entry.trim() !== "");
262
+ }
263
+ function propertiesFromSchema(schema) {
264
+ if (!schema) {
265
+ return {};
266
+ }
267
+ const properties = schema.properties;
268
+ return isRecord(properties) ? properties : {};
269
+ }
270
+ function typeUnionFromSchema(schema) {
271
+ if (!schema) {
272
+ return [];
273
+ }
274
+ const type = schema.type;
275
+ if (typeof type === "string" && type.trim() !== "") {
276
+ return [type];
277
+ }
278
+ if (Array.isArray(type)) {
279
+ return type.filter((entry) => typeof entry === "string" && entry.trim() !== "");
280
+ }
281
+ return [];
282
+ }
283
+ function enumValuesFromSchema(schema) {
284
+ if (!schema) {
285
+ return null;
286
+ }
287
+ const values = schema.enum;
288
+ return Array.isArray(values) ? values : null;
289
+ }
290
+ function constValueFromSchema(schema) {
291
+ if (!schema) {
292
+ return { hasConst: false };
293
+ }
294
+ if (!Object.prototype.hasOwnProperty.call(schema, "const")) {
295
+ return { hasConst: false };
296
+ }
297
+ return { hasConst: true, value: schema.const };
298
+ }
299
+ function itemsFromSchema(schema) {
300
+ if (!schema) {
301
+ return undefined;
302
+ }
303
+ const items = schema.items;
304
+ return isRecord(items) ? items : undefined;
305
+ }
306
+ function additionalPropertiesIsFalse(schema) {
307
+ if (!schema) {
308
+ return false;
309
+ }
310
+ return schema.additionalProperties === false;
311
+ }
312
+ function matchesSchemaType(value, type) {
313
+ switch (type) {
314
+ case "string":
315
+ return typeof value === "string";
316
+ case "number":
317
+ return typeof value === "number" && Number.isFinite(value);
318
+ case "integer":
319
+ return typeof value === "number" && Number.isInteger(value);
320
+ case "boolean":
321
+ return typeof value === "boolean";
322
+ case "object":
323
+ return isRecord(value);
324
+ case "array":
325
+ return Array.isArray(value);
326
+ case "null":
327
+ return value === null;
328
+ default:
329
+ return true;
330
+ }
331
+ }
332
+ function matchesSchemaTypes(value, types) {
333
+ if (types.length === 0) {
334
+ return true;
335
+ }
336
+ return types.some((type) => matchesSchemaType(value, type));
337
+ }
338
+ function isBlankString(value) {
339
+ return typeof value === "string" && value.trim() === "";
340
+ }
341
+ function hasRequiredFields(args, required) {
342
+ return required.every((field) => Object.prototype.hasOwnProperty.call(args, field) && args[field] !== undefined);
343
+ }
344
+ function validateValueAgainstSchema(value, schema, isRequired) {
345
+ if (!schema) {
346
+ return true;
347
+ }
348
+ const enumValues = enumValuesFromSchema(schema);
349
+ if (enumValues && !enumValues.some((entry) => Object.is(entry, value))) {
350
+ return false;
351
+ }
352
+ const constValue = constValueFromSchema(schema);
353
+ if (constValue.hasConst && !Object.is(constValue.value, value)) {
354
+ return false;
355
+ }
356
+ const types = typeUnionFromSchema(schema);
357
+ if (!matchesSchemaTypes(value, types)) {
358
+ return false;
359
+ }
360
+ if (isRequired && types.includes("string") && isBlankString(value)) {
361
+ return false;
362
+ }
363
+ const properties = propertiesFromSchema(schema);
364
+ const requiredFields = requiredFieldsFromSchema(schema);
365
+ const itemsSchema = itemsFromSchema(schema);
366
+ const hasAdditionalPropertiesConstraint = additionalPropertiesIsFalse(schema);
367
+ const expectsObject = hasAdditionalPropertiesConstraint || requiredFields.length > 0 || Object.keys(properties).length > 0;
368
+ const expectsArray = Boolean(itemsSchema);
369
+ if (Array.isArray(value)) {
370
+ if (!itemsSchema) {
371
+ return true;
372
+ }
373
+ return value.every((entry) => validateValueAgainstSchema(entry, itemsSchema, false));
374
+ }
375
+ if (isRecord(value)) {
376
+ if (!hasRequiredFields(value, requiredFields)) {
377
+ return false;
378
+ }
379
+ if (hasAdditionalPropertiesConstraint) {
380
+ for (const key of Object.keys(value)) {
381
+ if (!Object.prototype.hasOwnProperty.call(properties, key)) {
382
+ return false;
383
+ }
384
+ }
385
+ }
386
+ for (const field of requiredFields) {
387
+ const propertySchema = isRecord(properties[field]) ? properties[field] : undefined;
388
+ const fieldTypes = typeUnionFromSchema(propertySchema);
389
+ if (fieldTypes.includes("string") && isBlankString(value[field])) {
390
+ return false;
391
+ }
392
+ }
393
+ for (const [field, fieldValue] of Object.entries(value)) {
394
+ const propertySchema = isRecord(properties[field]) ? properties[field] : undefined;
395
+ if (!propertySchema) {
396
+ continue;
397
+ }
398
+ if (!validateValueAgainstSchema(fieldValue, propertySchema, requiredFields.includes(field))) {
399
+ return false;
400
+ }
401
+ }
402
+ return true;
403
+ }
404
+ if (value === null && types.includes("null")) {
405
+ return true;
406
+ }
407
+ if (expectsObject || expectsArray) {
408
+ return false;
409
+ }
410
+ return true;
411
+ }
412
+ export function validateToolCalls(params) {
413
+ const toolsByName = new Map(params.tools.map((tool) => [tool.name, tool]));
414
+ for (const toolCall of params.toolCalls) {
415
+ const tool = toolsByName.get(toolCall.name);
416
+ if (!tool) {
417
+ return { ok: false, reason: `unknown tool requested: ${toolCall.name}` };
418
+ }
419
+ const required = requiredFieldsFromSchema(tool.inputSchema);
420
+ if (!hasRequiredFields(toolCall.arguments, required)) {
421
+ return { ok: false, reason: `missing required tool arguments for ${toolCall.name}` };
422
+ }
423
+ if (!validateValueAgainstSchema(toolCall.arguments, tool.inputSchema, true)) {
424
+ return { ok: false, reason: `invalid tool arguments for ${toolCall.name}` };
425
+ }
426
+ }
427
+ return { ok: true, toolCalls: params.toolCalls };
428
+ }
186
429
  export function buildAnthropicMessageResponse(params) {
187
430
  return new Response(JSON.stringify({
188
431
  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: envelope.tool_calls,
56
+ toolCalls,
52
57
  });
53
58
  }
54
59
  return buildAnthropicToolUseResponse({
55
60
  model: request.model,
56
- toolCalls: envelope.tool_calls,
61
+ toolCalls,
57
62
  });
58
63
  }
59
64
  if (request.stream) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-cluade-auth",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "Claude Code CLI-backed Anthropic auth plugin for OpenCode.",
5
5
  "license": "MIT",
6
6
  "type": "module",