opencode-cluade-auth 1.0.2 → 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.
@@ -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,83 @@ 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
+ }
186
253
  function requiredFieldsFromSchema(schema) {
187
254
  if (!schema) {
188
255
  return [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-cluade-auth",
3
- "version": "1.0.2",
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",