@4yi/cli 0.1.6 → 0.1.7

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@4yi/cli",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "description": "4YI command-line launcher for OAuth login and OpenCode runtime",
5
5
  "type": "module",
6
6
  "bin": {
package/src/http.mjs CHANGED
@@ -1,12 +1,17 @@
1
1
  export async function requestJson(baseUrl, path, options = {}) {
2
2
  const url = `${baseUrl}${path}`;
3
+ const optionHeaders = normalizeHeaders(options.headers);
4
+ const headers = {
5
+ "content-type": "application/json",
6
+ ...(options.token ? { authorization: `Bearer ${options.token}` } : {}),
7
+ ...optionHeaders,
8
+ };
9
+ if (shouldGenerateIdempotencyKey(path, options, headers)) {
10
+ headers["Idempotency-Key"] = `cli-${randomId()}`;
11
+ }
3
12
  const res = await fetch(url, {
4
13
  ...options,
5
- headers: {
6
- "content-type": "application/json",
7
- ...(options.token ? { authorization: `Bearer ${options.token}` } : {}),
8
- ...(options.headers || {}),
9
- },
14
+ headers,
10
15
  });
11
16
  const text = await res.text();
12
17
  let body = null;
@@ -27,3 +32,47 @@ export async function requestJson(baseUrl, path, options = {}) {
27
32
  }
28
33
  return body;
29
34
  }
35
+
36
+ function normalizeHeaders(headers) {
37
+ if (!headers) return {};
38
+ if (headers instanceof Headers) return Object.fromEntries(headers.entries());
39
+ if (Array.isArray(headers)) return Object.fromEntries(headers);
40
+ return { ...headers };
41
+ }
42
+
43
+ function hasHeader(headers, name) {
44
+ const expected = name.toLowerCase();
45
+ return Object.keys(headers).some((key) => key.toLowerCase() === expected);
46
+ }
47
+
48
+ function shouldGenerateIdempotencyKey(path, options, headers) {
49
+ const method = String(options.method || "GET").toUpperCase();
50
+ if (method !== "POST") return false;
51
+ if (!path.endsWith("/chat/completions")) return false;
52
+ if (hasHeader(headers, "Idempotency-Key") || hasHeader(headers, "X-Idempotency-Key")) return false;
53
+
54
+ const body = parseJsonBody(options.body);
55
+ return body && body.stream !== true;
56
+ }
57
+
58
+ function parseJsonBody(body) {
59
+ if (!body) return null;
60
+ if (typeof body === "string") {
61
+ try {
62
+ return JSON.parse(body);
63
+ } catch {
64
+ return null;
65
+ }
66
+ }
67
+ if (body && typeof body === "object" && !(body instanceof ArrayBuffer)) return body;
68
+ return null;
69
+ }
70
+
71
+ function randomId() {
72
+ if (globalThis.crypto?.randomUUID) return globalThis.crypto.randomUUID();
73
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (ch) => {
74
+ const value = Math.floor(Math.random() * 16);
75
+ const nibble = ch === "x" ? value : (value & 0x3) | 0x8;
76
+ return nibble.toString(16);
77
+ });
78
+ }
package/src/opencode.mjs CHANGED
@@ -29,13 +29,16 @@ export function buildOpenCodeConfig({ modelConfig, preferredModel = null, tokenE
29
29
  const list = modelConfig.models || [];
30
30
  const models = {};
31
31
  for (const model of list) {
32
- models[model.id] = {
32
+ const entry = {
33
33
  name: model.display_name || model.id,
34
34
  limit: {
35
35
  context: modelContextLimit(model),
36
36
  output: modelOutputLimit(model),
37
37
  },
38
38
  };
39
+ if (typeof model.attachment === "boolean") entry.attachment = model.attachment;
40
+ if (model.modalities && typeof model.modalities === "object") entry.modalities = model.modalities;
41
+ models[model.id] = entry;
39
42
  }
40
43
  const has = (id) => !!id && list.some((model) => model.id === id);
41
44
  const firstClaude = list.find(isClaudeModel)?.id;