@d-ai/pi 0.2.0 → 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 +80 -64
  2. package/package.json +2 -2
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) {
@@ -1514,74 +1549,55 @@ function remoteMediaFromJob(client, result, request, mimeType) {
1514
1549
  }
1515
1550
 
1516
1551
  // src/provider.ts
1517
- import { createProvider } from "@earendil-works/pi-ai";
1518
- import { openAICompletionsApi } from "@earendil-works/pi-ai/api/openai-completions.lazy";
1519
- import { openAIResponsesApi } from "@earendil-works/pi-ai/api/openai-responses.lazy";
1552
+ var KEY_TTL_MS = 10 * 365 * 24 * 60 * 60 * 1e3;
1520
1553
  function registerDaiProvider(pi) {
1521
- pi.registerProvider(
1522
- createProvider({
1523
- id: "dai",
1524
- name: "D.AI",
1525
- baseUrl: `${DEFAULT_BASE_URL2}/v1`,
1526
- auth: {
1527
- apiKey: {
1528
- name: "D.AI API key",
1529
- async login(interaction) {
1530
- const key = await interaction.prompt({
1531
- type: "secret",
1532
- message: "D.AI API key (ddsk_...)",
1533
- placeholder: "ddsk_..."
1534
- });
1535
- const trimmed = key.trim();
1536
- await saveFileConfig({ apiKey: trimmed });
1537
- process.env.DAI_API_KEY = trimmed;
1538
- return { type: "api_key", key: trimmed };
1539
- },
1540
- async resolve({
1541
- credential,
1542
- ctx
1543
- }) {
1544
- const file = await loadFileConfig();
1545
- const key = credential?.key?.trim() || await ctx.env("DAI_API_KEY") || file.apiKey?.trim();
1546
- if (!key) {
1547
- return void 0;
1548
- }
1549
- const origin = normalizeOrigin(file.baseUrl ?? DEFAULT_BASE_URL2);
1550
- return {
1551
- auth: { apiKey: key, baseUrl: `${origin}/v1` },
1552
- source: credential?.key ? "stored D.AI API key" : "DAI_API_KEY"
1553
- };
1554
- }
1555
- }
1554
+ pi.registerProvider("dai", {
1555
+ name: "D.AI",
1556
+ baseUrl: `${DEFAULT_BASE_URL2}/v1`,
1557
+ api: "openai-completions",
1558
+ apiKey: "$DAI_API_KEY",
1559
+ authHeader: true,
1560
+ models: [],
1561
+ async refreshModels(context) {
1562
+ if (context.allowNetwork === false) {
1563
+ return [];
1564
+ }
1565
+ const file = await loadFileConfig();
1566
+ const key = file.apiKey ?? process.env.DAI_API_KEY ?? await loadAuthJsonKey();
1567
+ const origin = normalizeOrigin(file.baseUrl ?? DEFAULT_BASE_URL2);
1568
+ const catalog = await discoverDaiCatalog({
1569
+ origin,
1570
+ ...key === void 0 ? {} : { apiKey: key },
1571
+ ...context.signal === void 0 ? {} : { signal: context.signal }
1572
+ });
1573
+ return toProviderModels(catalog.chat, `${origin}/v1`);
1574
+ },
1575
+ oauth: {
1576
+ name: "D.AI API key",
1577
+ async login(callbacks) {
1578
+ const key = (await callbacks.onPrompt({ message: "D.AI API key (ddsk_...)" })).trim();
1579
+ await saveFileConfig({ apiKey: key });
1580
+ process.env.DAI_API_KEY = key;
1581
+ return {
1582
+ refresh: key,
1583
+ access: key,
1584
+ expires: Date.now() + KEY_TTL_MS
1585
+ };
1556
1586
  },
1557
- models: [],
1558
- async fetchModels(context) {
1559
- if (!context.allowNetwork) {
1560
- return context.stored?.models ?? [];
1561
- }
1562
- const file = await loadFileConfig();
1563
- const key = (context.credential && "key" in context.credential ? context.credential.key : void 0) || file.apiKey;
1564
- const origin = normalizeOrigin(file.baseUrl ?? DEFAULT_BASE_URL2);
1565
- const catalog = await discoverDaiCatalog({
1566
- origin,
1567
- ...key === void 0 ? {} : { apiKey: key },
1568
- signal: context.signal
1569
- });
1570
- return toPiModels(catalog.chat, `${origin}/v1`);
1587
+ async refreshToken(credentials) {
1588
+ return credentials;
1571
1589
  },
1572
- api: {
1573
- "openai-completions": openAICompletionsApi(),
1574
- "openai-responses": openAIResponsesApi()
1590
+ getApiKey(credentials) {
1591
+ return credentials.access;
1575
1592
  }
1576
- })
1577
- );
1593
+ }
1594
+ });
1578
1595
  }
1579
- function toPiModels(models, baseUrl) {
1596
+ function toProviderModels(models, baseUrl) {
1580
1597
  return models.map((model) => ({
1581
1598
  id: model.id,
1582
1599
  name: model.name,
1583
1600
  api: model.api,
1584
- provider: "dai",
1585
1601
  baseUrl,
1586
1602
  reasoning: model.reasoning,
1587
1603
  input: model.input,
@@ -1841,7 +1857,7 @@ ${urls}` }],
1841
1857
  return;
1842
1858
  }
1843
1859
  const file = await loadFileConfig();
1844
- const ready = hasApiKey(file);
1860
+ const ready = await hasApiKey(file);
1845
1861
  const models = [
1846
1862
  `image ${DEFAULT_IMAGE_MODEL}`,
1847
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.0",
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",
@@ -17,7 +17,7 @@
17
17
  "README.md"
18
18
  ],
19
19
  "scripts": {
20
- "build": "tsup src/index.ts --format esm --dts --clean --external @earendil-works/pi-coding-agent --external @earendil-works/pi-ai --external typebox",
20
+ "build": "tsup src/index.ts --format esm --dts --clean --external @earendil-works/pi-coding-agent --external typebox",
21
21
  "typecheck": "tsc --noEmit",
22
22
  "test": "vitest run",
23
23
  "check": "npm run typecheck && npm test && npm run build",