opencode-aicodewith-auth 0.1.4 → 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.
Files changed (2) hide show
  1. package/dist/index.js +90 -51
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -59,7 +59,7 @@ function createAicodewith(options = {}) {
59
59
  var aicodewith = createAicodewith();
60
60
 
61
61
  // index.ts
62
- import { mkdir as mkdir2, readFile as readFile2, writeFile as writeFile2 } from "fs/promises";
62
+ import { mkdir as mkdir2, readFile as readFile2, writeFile as writeFile2, access } from "fs/promises";
63
63
  import path4 from "path";
64
64
  import os3 from "os";
65
65
 
@@ -1423,46 +1423,91 @@ Restart OpenCode to apply.`,
1423
1423
  }).catch(() => {});
1424
1424
  log(`Auto-updated toast shown: v${oldVersion} \u2192 v${newVersion}`);
1425
1425
  }
1426
+ // lib/provider-config.json
1427
+ var provider_config_default = {
1428
+ name: "AICodewith",
1429
+ env: ["AICODEWITH_API_KEY"],
1430
+ api: "https://api.openai.com/v1",
1431
+ models: {
1432
+ "gpt-5.2-codex": {
1433
+ name: "GPT-5.2 Codex",
1434
+ modalities: { input: ["text", "image"], output: ["text"] }
1435
+ },
1436
+ "gpt-5.2": {
1437
+ name: "GPT-5.2",
1438
+ modalities: { input: ["text", "image"], output: ["text"] }
1439
+ },
1440
+ "claude-sonnet-4-5-20250929": {
1441
+ name: "Claude Sonnet 4.5",
1442
+ modalities: { input: ["text", "image"], output: ["text"] }
1443
+ },
1444
+ "claude-opus-4-5-20251101": {
1445
+ name: "Claude Opus 4.5",
1446
+ modalities: { input: ["text", "image"], output: ["text"] }
1447
+ },
1448
+ "gemini-3-pro-high": {
1449
+ name: "Gemini 3 Pro",
1450
+ modalities: { input: ["text", "image"], output: ["text"] }
1451
+ }
1452
+ }
1453
+ };
1426
1454
 
1427
1455
  // index.ts
1428
1456
  var CODEX_MODEL_PREFIXES = ["gpt-", "codex"];
1429
1457
  var PACKAGE_NAME2 = "opencode-aicodewith-auth";
1430
- var PROVIDER_NAME = "AICodewith";
1431
1458
  var PLUGIN_ENTRY = import.meta.url;
1432
1459
  var PROVIDER_EXT = import.meta.url.endsWith(".ts") ? ".ts" : ".js";
1433
1460
  var PROVIDER_NPM = new URL(`./provider${PROVIDER_EXT}`, import.meta.url).href;
1434
- var DEFAULT_API = "https://api.openai.com/v1";
1435
- var DEFAULT_ENV = ["AICODEWITH_API_KEY"];
1436
1461
  var DEFAULT_OUTPUT_TOKEN_MAX = 32000;
1437
- var IMAGE_MODALITIES = { input: ["text", "image"], output: ["text"] };
1438
- var MODEL_CONFIGS = {
1439
- "gpt-5.2-codex": { name: "GPT-5.2 Codex", modalities: IMAGE_MODALITIES },
1440
- "gpt-5.2": { name: "GPT-5.2", modalities: IMAGE_MODALITIES },
1441
- "claude-sonnet-4-5-20250929": { name: "Claude Sonnet 4.5", modalities: IMAGE_MODALITIES },
1442
- "claude-opus-4-5-20251101": { name: "Claude Opus 4.5", modalities: IMAGE_MODALITIES },
1443
- "gemini-3-pro-high": { name: "Gemini 3 Pro", modalities: IMAGE_MODALITIES }
1444
- };
1445
- var ALLOWED_MODEL_IDS = Object.keys(MODEL_CONFIGS);
1446
- var ALLOWED_MODEL_SET = new Set(ALLOWED_MODEL_IDS);
1447
1462
  var homeDir = process.env.OPENCODE_TEST_HOME || os3.homedir();
1448
1463
  var configRoot = process.env.XDG_CONFIG_HOME || path4.join(homeDir, ".config");
1449
1464
  var configDir = path4.join(configRoot, "opencode");
1450
- var configPath = path4.join(configDir, "opencode.json");
1465
+ var configPathJson = path4.join(configDir, "opencode.json");
1466
+ var configPathJsonc = path4.join(configDir, "opencode.jsonc");
1451
1467
  var ensureConfigPromise;
1452
- var toModelMap = (ids, existing = {}) => ids.reduce((acc, id) => {
1453
- const existingConfig = Object.prototype.hasOwnProperty.call(existing, id) ? existing[id] : {};
1454
- const defaultConfig = MODEL_CONFIGS[id] ?? {};
1455
- acc[id] = { ...defaultConfig, ...typeof existingConfig === "object" ? existingConfig : {} };
1456
- return acc;
1457
- }, {});
1458
- var readJson = async (filePath) => {
1468
+ var fileExists = async (filePath) => {
1469
+ try {
1470
+ await access(filePath);
1471
+ return true;
1472
+ } catch {
1473
+ return false;
1474
+ }
1475
+ };
1476
+ var stripJsonComments2 = (content) => {
1477
+ return content.replace(/\\"|"(?:\\"|[^"])*"|(\/\/.*|\/\*[\s\S]*?\*\/)/g, (m, g) => g ? "" : m).replace(/,(\s*[}\]])/g, "$1");
1478
+ };
1479
+ var readJsonOrJsonc = async (filePath) => {
1459
1480
  try {
1460
1481
  const text = await readFile2(filePath, "utf-8");
1461
- return JSON.parse(text);
1482
+ const stripped = filePath.endsWith(".jsonc") ? stripJsonComments2(text) : text;
1483
+ return JSON.parse(stripped);
1462
1484
  } catch {
1463
1485
  return;
1464
1486
  }
1465
1487
  };
1488
+ var deepEqual = (a, b) => {
1489
+ if (a === b)
1490
+ return true;
1491
+ if (typeof a !== typeof b)
1492
+ return false;
1493
+ if (a === null || b === null)
1494
+ return a === b;
1495
+ if (typeof a !== "object")
1496
+ return false;
1497
+ const aObj = a;
1498
+ const bObj = b;
1499
+ const aKeys = Object.keys(aObj);
1500
+ const bKeys = Object.keys(bObj);
1501
+ if (aKeys.length !== bKeys.length)
1502
+ return false;
1503
+ for (const key of aKeys) {
1504
+ if (!Object.prototype.hasOwnProperty.call(bObj, key))
1505
+ return false;
1506
+ if (!deepEqual(aObj[key], bObj[key]))
1507
+ return false;
1508
+ }
1509
+ return true;
1510
+ };
1466
1511
  var isPackageEntry = (value) => value === PACKAGE_NAME2 || value.startsWith(`${PACKAGE_NAME2}@`);
1467
1512
  var ensurePluginEntry = (list) => {
1468
1513
  if (!Array.isArray(list))
@@ -1470,36 +1515,17 @@ var ensurePluginEntry = (list) => {
1470
1515
  const hasPlugin = list.some((entry) => typeof entry === "string" && (entry === PLUGIN_ENTRY || isPackageEntry(entry)));
1471
1516
  return hasPlugin ? list : [...list, PLUGIN_ENTRY];
1472
1517
  };
1518
+ var buildStandardProviderConfig = () => ({
1519
+ ...provider_config_default,
1520
+ npm: PROVIDER_NPM
1521
+ });
1473
1522
  var applyProviderConfig = (config) => {
1474
1523
  let changed = false;
1475
1524
  const providerMap = config.provider && typeof config.provider === "object" ? config.provider : {};
1476
- const existing = providerMap[PROVIDER_ID] && typeof providerMap[PROVIDER_ID] === "object" ? providerMap[PROVIDER_ID] : {};
1477
- const existingModels = existing.models && typeof existing.models === "object" ? existing.models : {};
1478
- const next = { ...existing };
1479
- if (!next.name) {
1480
- next.name = PROVIDER_NAME;
1481
- changed = true;
1482
- }
1483
- if (!Array.isArray(next.env)) {
1484
- next.env = DEFAULT_ENV;
1485
- changed = true;
1486
- }
1487
- if (!next.npm || typeof next.npm === "string" && isPackageEntry(next.npm)) {
1488
- next.npm = PROVIDER_NPM;
1489
- changed = true;
1490
- }
1491
- if (!next.api) {
1492
- next.api = DEFAULT_API;
1493
- changed = true;
1494
- }
1495
- const hasExtraModels = Object.keys(existingModels).some((id) => !ALLOWED_MODEL_SET.has(id));
1496
- const hasMissingModels = ALLOWED_MODEL_IDS.some((id) => !Object.prototype.hasOwnProperty.call(existingModels, id));
1497
- if (!next.models || hasExtraModels || hasMissingModels) {
1498
- next.models = toModelMap(ALLOWED_MODEL_IDS, existingModels);
1499
- changed = true;
1500
- }
1501
- providerMap[PROVIDER_ID] = next;
1502
- if (config.provider !== providerMap) {
1525
+ const existingProvider = providerMap[PROVIDER_ID];
1526
+ const standardProvider = buildStandardProviderConfig();
1527
+ if (!deepEqual(existingProvider, standardProvider)) {
1528
+ providerMap[PROVIDER_ID] = standardProvider;
1503
1529
  config.provider = providerMap;
1504
1530
  changed = true;
1505
1531
  }
@@ -1514,7 +1540,20 @@ var ensureConfigFile = async () => {
1514
1540
  if (ensureConfigPromise)
1515
1541
  return ensureConfigPromise;
1516
1542
  ensureConfigPromise = (async () => {
1517
- const config = await readJson(configPath) ?? {};
1543
+ const jsoncExists = await fileExists(configPathJsonc);
1544
+ const jsonExists = await fileExists(configPathJson);
1545
+ let configPath;
1546
+ let config;
1547
+ if (jsoncExists) {
1548
+ configPath = configPathJsonc;
1549
+ config = await readJsonOrJsonc(configPath) ?? {};
1550
+ } else if (jsonExists) {
1551
+ configPath = configPathJson;
1552
+ config = await readJsonOrJsonc(configPath) ?? {};
1553
+ } else {
1554
+ configPath = configPathJson;
1555
+ config = { $schema: "https://opencode.ai/config.json" };
1556
+ }
1518
1557
  if (!config || typeof config !== "object")
1519
1558
  return;
1520
1559
  const changed = applyProviderConfig(config);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-aicodewith-auth",
3
- "version": "0.1.4",
3
+ "version": "0.1.7",
4
4
  "description": "OpenCode plugin for AICodewith authentication - Access GPT-5.2, Claude, and Gemini models through AICodewith API",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",