opencode-aicodewith-auth 0.1.43 → 0.1.45

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 +33 -227
  2. package/package.json +6 -3
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  // @bun
2
2
  // index.ts
3
- import { mkdir as mkdir2, readFile as readFile3, writeFile as writeFile3, access as access2 } from "fs/promises";
3
+ import { mkdir, readFile as readFile2, writeFile as writeFile2, access as access2 } from "fs/promises";
4
4
  import path5 from "path";
5
5
  import os4 from "os";
6
6
 
@@ -308,15 +308,6 @@ function logDebug(message, data) {
308
308
  console.log(`[${PLUGIN_NAME}] ${message}`);
309
309
  }
310
310
  }
311
- function logWarn(message, data) {
312
- if (!DEBUG_ENABLED && !LOGGING_ENABLED)
313
- return;
314
- if (data !== undefined) {
315
- console.warn(`[${PLUGIN_NAME}] ${message}`, data);
316
- } else {
317
- console.warn(`[${PLUGIN_NAME}] ${message}`);
318
- }
319
- }
320
311
  var rawResponseCounter = 0;
321
312
  function saveRawResponse(provider, responseBody, metadata) {
322
313
  if (!SAVE_RAW_RESPONSE_ENABLED)
@@ -344,133 +335,18 @@ function saveRawResponse(provider, responseBody, metadata) {
344
335
  }
345
336
 
346
337
  // lib/prompts/codex.ts
347
- import { existsSync as existsSync2, mkdirSync as mkdirSync2, readFileSync, writeFileSync as writeFileSync2 } from "fs";
348
- import { homedir as homedir2 } from "os";
338
+ import { readFileSync } from "fs";
349
339
  import { dirname, join as join2 } from "path";
350
340
  import { fileURLToPath } from "url";
351
- var GITHUB_API_RELEASES = "https://api.github.com/repos/openai/codex/releases/latest";
352
- var GITHUB_HTML_RELEASES = "https://github.com/openai/codex/releases/latest";
353
- var CACHE_DIR = join2(homedir2(), ".opencode", "cache");
354
341
  var __filename2 = fileURLToPath(import.meta.url);
355
342
  var __dirname2 = dirname(__filename2);
356
- var PROMPT_FILES = {
357
- "gpt-5.2-codex": "gpt-5.2-codex_prompt.md",
358
- "codex-max": "gpt-5.1-codex-max_prompt.md",
359
- codex: "gpt_5_codex_prompt.md",
360
- "gpt-5.2": "gpt_5_2_prompt.md",
361
- "gpt-5.1": "gpt_5_1_prompt.md"
362
- };
363
- var CACHE_FILES = {
364
- "gpt-5.2-codex": "gpt-5.2-codex-instructions.md",
365
- "codex-max": "codex-max-instructions.md",
366
- codex: "codex-instructions.md",
367
- "gpt-5.2": "gpt-5.2-instructions.md",
368
- "gpt-5.1": "gpt-5.1-instructions.md"
369
- };
370
343
  var FALLBACK_FILE = join2(__dirname2, "fallback-instructions.txt");
371
- function getModelFamily(normalizedModel) {
372
- if (normalizedModel.includes("gpt-5.2-codex") || normalizedModel.includes("gpt 5.2 codex")) {
373
- return "gpt-5.2-codex";
374
- }
375
- if (normalizedModel.includes("codex-max")) {
376
- return "codex-max";
377
- }
378
- if (normalizedModel.includes("codex") || normalizedModel.startsWith("codex-")) {
379
- return "codex";
380
- }
381
- if (normalizedModel.includes("gpt-5.2")) {
382
- return "gpt-5.2";
383
- }
384
- return "gpt-5.1";
385
- }
386
- async function getLatestReleaseTag() {
387
- try {
388
- const response = await fetch(GITHUB_API_RELEASES);
389
- if (response.ok) {
390
- const data = await response.json();
391
- if (data.tag_name) {
392
- return data.tag_name;
393
- }
394
- }
395
- } catch {}
396
- const htmlResponse = await fetch(GITHUB_HTML_RELEASES);
397
- if (!htmlResponse.ok) {
398
- throw new Error(`Failed to fetch latest release: ${htmlResponse.status}`);
399
- }
400
- const finalUrl = htmlResponse.url;
401
- if (finalUrl) {
402
- const parts = finalUrl.split("/tag/");
403
- const last = parts[parts.length - 1];
404
- if (last && !last.includes("/")) {
405
- return last;
406
- }
407
- }
408
- const html = await htmlResponse.text();
409
- const match = html.match(/\/openai\/codex\/releases\/tag\/([^"]+)/);
410
- if (match && match[1]) {
411
- return match[1];
412
- }
413
- throw new Error("Failed to determine latest release tag from GitHub");
414
- }
415
- async function getCodexInstructions(normalizedModel = "gpt-5.1-codex") {
416
- const modelFamily = getModelFamily(normalizedModel);
417
- const promptFile = PROMPT_FILES[modelFamily];
418
- const cacheFile = join2(CACHE_DIR, CACHE_FILES[modelFamily]);
419
- const cacheMetaFile = join2(CACHE_DIR, `${CACHE_FILES[modelFamily].replace(".md", "-meta.json")}`);
420
- try {
421
- let cachedETag = null;
422
- let cachedTag = null;
423
- let cachedTimestamp = null;
424
- if (existsSync2(cacheMetaFile)) {
425
- const metadata = JSON.parse(readFileSync(cacheMetaFile, "utf8"));
426
- cachedETag = metadata.etag;
427
- cachedTag = metadata.tag;
428
- cachedTimestamp = metadata.lastChecked;
429
- }
430
- const CACHE_TTL_MS = 15 * 60 * 1000;
431
- if (cachedTimestamp && Date.now() - cachedTimestamp < CACHE_TTL_MS && existsSync2(cacheFile)) {
432
- return readFileSync(cacheFile, "utf8");
433
- }
434
- const latestTag = await getLatestReleaseTag();
435
- const url = `https://raw.githubusercontent.com/openai/codex/${latestTag}/codex-rs/core/${promptFile}`;
436
- if (cachedTag != latestTag) {
437
- cachedETag = null;
438
- }
439
- const headers = {};
440
- if (cachedETag) {
441
- headers["If-None-Match"] = cachedETag;
442
- }
443
- const response = await fetch(url, { headers });
444
- if (response.status === 304) {
445
- if (existsSync2(cacheFile)) {
446
- return readFileSync(cacheFile, "utf8");
447
- }
448
- }
449
- if (response.ok) {
450
- const instructions = await response.text();
451
- const newETag = response.headers.get("etag");
452
- if (!existsSync2(CACHE_DIR)) {
453
- mkdirSync2(CACHE_DIR, { recursive: true });
454
- }
455
- writeFileSync2(cacheFile, instructions, "utf8");
456
- writeFileSync2(cacheMetaFile, JSON.stringify({
457
- etag: newETag,
458
- tag: latestTag,
459
- lastChecked: Date.now(),
460
- url
461
- }), "utf8");
462
- return instructions;
463
- }
464
- throw new Error(`HTTP ${response.status}`);
465
- } catch (error) {
466
- const err = error;
467
- console.error(`[${PLUGIN_NAME}] Failed to fetch Codex instructions from GitHub:`, err.message);
468
- if (existsSync2(FALLBACK_FILE)) {
469
- console.error(`[${PLUGIN_NAME}] Using fallback instructions`);
470
- return readFileSync(FALLBACK_FILE, "utf8");
471
- }
472
- throw new Error("No fallback instructions available");
473
- }
344
+ var cachedInstructions = null;
345
+ function getCodexInstructions(_normalizedModel = "gpt-5.1-codex") {
346
+ if (cachedInstructions)
347
+ return cachedInstructions;
348
+ cachedInstructions = readFileSync(FALLBACK_FILE, "utf8");
349
+ return cachedInstructions;
474
350
  }
475
351
 
476
352
  // lib/prompts/codex-opencode-bridge.ts
@@ -597,59 +473,6 @@ Sandbox policies, approval mechanisms, final answer formatting, git commit proto
597
473
  - Treat destructive commands (e.g., \`rm\`, \`git reset --hard\`) as requiring explicit user request or approval.
598
474
  - When uncertain, prefer non-destructive verification first (e.g., confirm file existence with \`list\`, then delete with \`bash\`).`;
599
475
 
600
- // lib/prompts/opencode-codex.ts
601
- import { join as join3 } from "path";
602
- import { homedir as homedir3 } from "os";
603
- import { mkdir, readFile, writeFile } from "fs/promises";
604
- var OPENCODE_CODEX_URL = "https://raw.githubusercontent.com/anomalyco/opencode/dev/packages/opencode/src/session/prompt/codex.txt";
605
- var CACHE_DIR2 = join3(homedir3(), ".opencode", "cache");
606
- var CACHE_FILE = join3(CACHE_DIR2, "opencode-codex.txt");
607
- var CACHE_META_FILE = join3(CACHE_DIR2, "opencode-codex-meta.json");
608
- async function getOpenCodeCodexPrompt() {
609
- await mkdir(CACHE_DIR2, { recursive: true });
610
- let cachedContent = null;
611
- let cachedMeta = null;
612
- try {
613
- cachedContent = await readFile(CACHE_FILE, "utf-8");
614
- const metaContent = await readFile(CACHE_META_FILE, "utf-8");
615
- cachedMeta = JSON.parse(metaContent);
616
- } catch {}
617
- const CACHE_TTL_MS = 15 * 60 * 1000;
618
- if (cachedMeta?.lastChecked && Date.now() - cachedMeta.lastChecked < CACHE_TTL_MS && cachedContent) {
619
- return cachedContent;
620
- }
621
- const headers = {};
622
- if (cachedMeta?.etag) {
623
- headers["If-None-Match"] = cachedMeta.etag;
624
- }
625
- try {
626
- const response = await fetch(OPENCODE_CODEX_URL, { headers });
627
- if (response.status === 304 && cachedContent) {
628
- return cachedContent;
629
- }
630
- if (response.ok) {
631
- const content = await response.text();
632
- const etag = response.headers.get("etag") || "";
633
- await writeFile(CACHE_FILE, content, "utf-8");
634
- await writeFile(CACHE_META_FILE, JSON.stringify({
635
- etag,
636
- lastFetch: new Date().toISOString(),
637
- lastChecked: Date.now()
638
- }, null, 2), "utf-8");
639
- return content;
640
- }
641
- if (cachedContent) {
642
- return cachedContent;
643
- }
644
- throw new Error(`Failed to fetch OpenCode codex.txt: ${response.status}`);
645
- } catch (error) {
646
- if (cachedContent) {
647
- return cachedContent;
648
- }
649
- throw new Error(`Failed to fetch OpenCode codex.txt and no cache available: ${error}`);
650
- }
651
- }
652
-
653
476
  // lib/request/helpers/model-map.ts
654
477
  var MODEL_MAP = buildAliasMap();
655
478
  function getNormalizedModel(modelId) {
@@ -871,30 +694,19 @@ function resolveInclude(body) {
871
694
  }
872
695
  return include;
873
696
  }
874
- function filterInput(input) {
875
- if (!Array.isArray(input))
876
- return input;
877
- return input.filter((item) => {
878
- if (item.type === "item_reference") {
879
- return false;
880
- }
881
- return true;
882
- }).map((item) => {
883
- if (item.id) {
884
- const { id, ...itemWithoutId } = item;
885
- return itemWithoutId;
697
+ function stripItemIds(input) {
698
+ return input.filter((item) => item.type !== "item_reference").map((item) => {
699
+ if ("id" in item) {
700
+ const { id, ...rest } = item;
701
+ return rest;
886
702
  }
887
703
  return item;
888
704
  });
889
705
  }
890
- async function filterOpenCodeSystemPrompts(input) {
706
+ function filterOpenCodeSystemPrompts(input) {
891
707
  if (!Array.isArray(input))
892
708
  return input;
893
- let cachedPrompt = null;
894
- try {
895
- cachedPrompt = await getOpenCodeCodexPrompt();
896
- } catch {}
897
- return filterOpenCodeSystemPromptsWithCachedPrompt(input, cachedPrompt);
709
+ return filterOpenCodeSystemPromptsWithCachedPrompt(input, null);
898
710
  }
899
711
  function addCodexBridgeMessage(input, hasTools) {
900
712
  if (!hasTools || !Array.isArray(input))
@@ -938,19 +750,10 @@ async function transformRequestBody(body, codexInstructions) {
938
750
  });
939
751
  body.model = normalizedModel;
940
752
  body.stream = true;
941
- body.store = false;
942
753
  body.instructions = codexInstructions;
943
754
  if (body.input && Array.isArray(body.input)) {
944
- const originalIds = body.input.filter((item) => item.id).map((item) => item.id);
945
- if (originalIds.length > 0) {
946
- logDebug("Filtering message IDs", originalIds);
947
- }
948
- body.input = filterInput(body.input);
949
- const remainingIds = (body.input || []).filter((item) => item.id).map((item) => item.id);
950
- if (remainingIds.length > 0) {
951
- logWarn("IDs still present after filtering", remainingIds);
952
- }
953
- body.input = await filterOpenCodeSystemPrompts(body.input);
755
+ body.input = stripItemIds(body.input);
756
+ body.input = filterOpenCodeSystemPrompts(body.input);
954
757
  body.input = addCodexBridgeMessage(body.input, !!body.tools);
955
758
  if (body.input) {
956
759
  body.input = normalizeOrphanedToolOutputs(body.input);
@@ -1052,7 +855,7 @@ async function transformRequestForCodex(init) {
1052
855
  try {
1053
856
  const body = JSON.parse(init.body);
1054
857
  const normalizedModel = normalizeModel(body.model);
1055
- const codexInstructions = await getCodexInstructions(normalizedModel);
858
+ const codexInstructions = getCodexInstructions(normalizedModel);
1056
859
  const transformedBody = await transformRequestBody(body, codexInstructions);
1057
860
  logRequest("after-transform", {
1058
861
  model: transformedBody.model,
@@ -1217,8 +1020,8 @@ function getCacheDir() {
1217
1020
  }
1218
1021
  return path.join(os.homedir(), ".cache", "opencode");
1219
1022
  }
1220
- var CACHE_DIR3 = getCacheDir();
1221
- var INSTALLED_PACKAGE_JSON = path.join(CACHE_DIR3, "node_modules", PACKAGE_NAME, "package.json");
1023
+ var CACHE_DIR = getCacheDir();
1024
+ var INSTALLED_PACKAGE_JSON = path.join(CACHE_DIR, "node_modules", PACKAGE_NAME, "package.json");
1222
1025
  function getUserConfigDir() {
1223
1026
  if (process.platform === "win32") {
1224
1027
  const crossPlatformDir = path.join(os.homedir(), ".config");
@@ -1471,7 +1274,7 @@ function stripTrailingCommas(json) {
1471
1274
  return json.replace(/,(\s*[}\]])/g, "$1");
1472
1275
  }
1473
1276
  function removeFromBunLock(packageName) {
1474
- const lockPath = path3.join(CACHE_DIR3, "bun.lock");
1277
+ const lockPath = path3.join(CACHE_DIR, "bun.lock");
1475
1278
  if (!fs3.existsSync(lockPath))
1476
1279
  return false;
1477
1280
  try {
@@ -1497,8 +1300,8 @@ function removeFromBunLock(packageName) {
1497
1300
  }
1498
1301
  function invalidatePackage(packageName = PACKAGE_NAME) {
1499
1302
  try {
1500
- const pkgDir = path3.join(CACHE_DIR3, "node_modules", packageName);
1501
- const pkgJsonPath = path3.join(CACHE_DIR3, "package.json");
1303
+ const pkgDir = path3.join(CACHE_DIR, "node_modules", packageName);
1304
+ const pkgJsonPath = path3.join(CACHE_DIR, "package.json");
1502
1305
  let packageRemoved = false;
1503
1306
  let dependencyRemoved = false;
1504
1307
  let lockRemoved = false;
@@ -1640,7 +1443,7 @@ async function runBunInstallSafe() {
1640
1443
  return new Promise((resolve) => {
1641
1444
  try {
1642
1445
  const child = spawn("bun", ["install"], {
1643
- cwd: CACHE_DIR3,
1446
+ cwd: CACHE_DIR,
1644
1447
  stdio: "ignore",
1645
1448
  detached: true
1646
1449
  });
@@ -1684,7 +1487,7 @@ Restart OpenCode to apply.`,
1684
1487
  }
1685
1488
 
1686
1489
  // lib/hooks/omo-config-sync/index.ts
1687
- import { readFile as readFile2, writeFile as writeFile2, access } from "fs/promises";
1490
+ import { readFile, writeFile, access } from "fs/promises";
1688
1491
  import path4 from "path";
1689
1492
  import os3 from "os";
1690
1493
  var PACKAGE_NAME2 = "opencode-aicodewith-auth";
@@ -1781,7 +1584,7 @@ var syncOmoConfig = async () => {
1781
1584
  };
1782
1585
  } else {
1783
1586
  try {
1784
- const content = await readFile2(configPath, "utf-8");
1587
+ const content = await readFile(configPath, "utf-8");
1785
1588
  userConfig = JSON.parse(content);
1786
1589
  } catch {
1787
1590
  return;
@@ -1795,7 +1598,7 @@ var syncOmoConfig = async () => {
1795
1598
  return;
1796
1599
  }
1797
1600
  try {
1798
- await writeFile2(configPath, `${JSON.stringify(userConfig, null, 2)}
1601
+ await writeFile(configPath, `${JSON.stringify(userConfig, null, 2)}
1799
1602
  `, "utf-8");
1800
1603
  } catch (error) {
1801
1604
  console.warn(`[${PACKAGE_NAME2}] Failed to sync OMO config: ${error instanceof Error ? error.message : error}`);
@@ -1933,7 +1736,7 @@ var stripJsonComments2 = (content) => {
1933
1736
  };
1934
1737
  var readJsonOrJsonc = async (filePath) => {
1935
1738
  try {
1936
- const text = await readFile3(filePath, "utf-8");
1739
+ const text = await readFile2(filePath, "utf-8");
1937
1740
  const stripped = filePath.endsWith(".jsonc") ? stripJsonComments2(text) : text;
1938
1741
  return JSON.parse(stripped);
1939
1742
  } catch {
@@ -2018,8 +1821,8 @@ var ensureConfigFile = async () => {
2018
1821
  const changed = applyProviderConfig(config);
2019
1822
  if (!changed)
2020
1823
  return;
2021
- await mkdir2(configDir, { recursive: true });
2022
- await writeFile3(configPath, `${JSON.stringify(config, null, 2)}
1824
+ await mkdir(configDir, { recursive: true });
1825
+ await writeFile2(configPath, `${JSON.stringify(config, null, 2)}
2023
1826
  `, "utf-8");
2024
1827
  })();
2025
1828
  return ensureConfigPromise;
@@ -2222,6 +2025,9 @@ var AicodewithCodexAuthPlugin = async (ctx) => {
2222
2025
  "chat.params": async (input, output) => {
2223
2026
  if (input.model.providerID !== PROVIDER_ID2)
2224
2027
  return;
2028
+ if (isCodexModel(input.model.id)) {
2029
+ return;
2030
+ }
2225
2031
  if (!input.model.id?.startsWith("claude-"))
2226
2032
  return;
2227
2033
  const thinking = output.options?.thinking;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-aicodewith-auth",
3
- "version": "0.1.43",
3
+ "version": "0.1.45",
4
4
  "description": "OpenCode plugin for AICodewith authentication - Access GPT-5.3 Codex, GPT-5.2, Claude, and Gemini models through AICodewith API",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -19,9 +19,12 @@
19
19
  "dist"
20
20
  ],
21
21
  "scripts": {
22
- "build": "bun run generate:config && bun build index.ts provider.ts --outdir dist --target bun --format esm --external @ai-sdk/anthropic --external @ai-sdk/google --external @ai-sdk/openai --external @ai-sdk/provider --external @ai-sdk/provider-utils --external @opencode-ai/plugin --external @opencode-ai/sdk",
22
+ "build": "bun run generate:provider-config && bun build index.ts provider.ts --outdir dist --target bun --format esm --external @ai-sdk/anthropic --external @ai-sdk/google --external @ai-sdk/openai --external @ai-sdk/provider --external @ai-sdk/provider-utils --external @opencode-ai/plugin --external @opencode-ai/sdk",
23
23
  "clean": "rm -rf dist",
24
- "generate:config": "bun scripts/generate-provider-config.ts && bun scripts/generate-omo-config.ts",
24
+ "generate:provider-config": "bun scripts/generate-provider-config.ts",
25
+ "generate:omo-config": "bun scripts/generate-omo-config.ts",
26
+ "generate:config": "bun run generate:provider-config && bun run generate:omo-config",
27
+ "build:with-omo": "bun run generate:config && bun build index.ts provider.ts --outdir dist --target bun --format esm --external @ai-sdk/anthropic --external @ai-sdk/google --external @ai-sdk/openai --external @ai-sdk/provider --external @ai-sdk/provider-utils --external @opencode-ai/plugin --external @opencode-ai/sdk",
25
28
  "prepublishOnly": "bun run clean && bun run build",
26
29
  "typecheck": "bunx tsc --noEmit",
27
30
  "test": "vitest run",