@d-ai/pi 0.2.1 → 0.2.2

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 +41 -6
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1042,6 +1042,34 @@ function sanitize(message) {
1042
1042
  function configPath() {
1043
1043
  return join(homedir(), ".pi", "agent", "dai.json");
1044
1044
  }
1045
+ function authJsonPath() {
1046
+ return join(homedir(), ".pi", "agent", "auth.json");
1047
+ }
1048
+ async function loadAuthJsonKey(path = authJsonPath()) {
1049
+ try {
1050
+ const raw = JSON.parse(await readFile(path, "utf8"));
1051
+ if (typeof raw !== "object" || raw === null || Array.isArray(raw)) {
1052
+ return void 0;
1053
+ }
1054
+ const dai = raw.dai;
1055
+ if (typeof dai !== "object" || dai === null || Array.isArray(dai)) {
1056
+ return void 0;
1057
+ }
1058
+ const record = dai;
1059
+ for (const field of ["key", "access"]) {
1060
+ const value = record[field];
1061
+ if (typeof value === "string" && value.trim()) {
1062
+ return value.trim();
1063
+ }
1064
+ }
1065
+ return void 0;
1066
+ } catch (error) {
1067
+ if (isMissingFile(error)) {
1068
+ return void 0;
1069
+ }
1070
+ throw error;
1071
+ }
1072
+ }
1045
1073
  async function loadFileConfig(path = configPath()) {
1046
1074
  try {
1047
1075
  const raw = JSON.parse(await readFile(path, "utf8"));
@@ -1071,17 +1099,24 @@ async function saveFileConfig(config, path = configPath()) {
1071
1099
  await writeFile(`${path}`, `${JSON.stringify(next, null, 2)}
1072
1100
  `, { mode: 384 });
1073
1101
  }
1074
- async function resolveConfig(env = process.env, path = configPath()) {
1102
+ async function resolveConfig(env = process.env, path = configPath(), authPath = authJsonPath()) {
1075
1103
  const file = await loadFileConfig(path);
1076
- const apiKey = firstNonEmpty(file.apiKey, env.DAI_API_KEY);
1104
+ const fromAuth = await loadAuthJsonKey(authPath);
1105
+ const apiKey = firstNonEmpty(file.apiKey, env.DAI_API_KEY, fromAuth);
1077
1106
  if (!apiKey) {
1078
1107
  throw missingApiKeyError();
1079
1108
  }
1109
+ if (!file.apiKey && fromAuth) {
1110
+ await saveFileConfig({ apiKey: fromAuth }, path);
1111
+ }
1080
1112
  const baseUrl = firstNonEmpty(file.baseUrl, env.DAI_BASE_URL);
1081
1113
  return baseUrl === void 0 ? { apiKey } : { apiKey, baseUrl };
1082
1114
  }
1083
- function hasApiKey(config, env = process.env) {
1084
- return Boolean(firstNonEmpty(config.apiKey, env.DAI_API_KEY));
1115
+ async function hasApiKey(config, env = process.env, authPath = authJsonPath()) {
1116
+ if (firstNonEmpty(config.apiKey, env.DAI_API_KEY)) {
1117
+ return true;
1118
+ }
1119
+ return Boolean(await loadAuthJsonKey(authPath));
1085
1120
  }
1086
1121
  function firstNonEmpty(...values) {
1087
1122
  for (const value of values) {
@@ -1528,7 +1563,7 @@ function registerDaiProvider(pi) {
1528
1563
  return [];
1529
1564
  }
1530
1565
  const file = await loadFileConfig();
1531
- const key = file.apiKey ?? process.env.DAI_API_KEY;
1566
+ const key = file.apiKey ?? process.env.DAI_API_KEY ?? await loadAuthJsonKey();
1532
1567
  const origin = normalizeOrigin(file.baseUrl ?? DEFAULT_BASE_URL2);
1533
1568
  const catalog = await discoverDaiCatalog({
1534
1569
  origin,
@@ -1822,7 +1857,7 @@ ${urls}` }],
1822
1857
  return;
1823
1858
  }
1824
1859
  const file = await loadFileConfig();
1825
- const ready = hasApiKey(file);
1860
+ const ready = await hasApiKey(file);
1826
1861
  const models = [
1827
1862
  `image ${DEFAULT_IMAGE_MODEL}`,
1828
1863
  `video ${DEFAULT_VIDEO_MODEL}`,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@d-ai/pi",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "Pi extension for D.AI image, video, and music generation",
5
5
  "license": "MIT",
6
6
  "type": "module",