@d-ai/pi 0.2.0 → 0.2.1

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 +40 -59
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -1514,74 +1514,55 @@ function remoteMediaFromJob(client, result, request, mimeType) {
1514
1514
  }
1515
1515
 
1516
1516
  // 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";
1517
+ var KEY_TTL_MS = 10 * 365 * 24 * 60 * 60 * 1e3;
1520
1518
  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
- }
1519
+ pi.registerProvider("dai", {
1520
+ name: "D.AI",
1521
+ baseUrl: `${DEFAULT_BASE_URL2}/v1`,
1522
+ api: "openai-completions",
1523
+ apiKey: "$DAI_API_KEY",
1524
+ authHeader: true,
1525
+ models: [],
1526
+ async refreshModels(context) {
1527
+ if (context.allowNetwork === false) {
1528
+ return [];
1529
+ }
1530
+ const file = await loadFileConfig();
1531
+ const key = file.apiKey ?? process.env.DAI_API_KEY;
1532
+ const origin = normalizeOrigin(file.baseUrl ?? DEFAULT_BASE_URL2);
1533
+ const catalog = await discoverDaiCatalog({
1534
+ origin,
1535
+ ...key === void 0 ? {} : { apiKey: key },
1536
+ ...context.signal === void 0 ? {} : { signal: context.signal }
1537
+ });
1538
+ return toProviderModels(catalog.chat, `${origin}/v1`);
1539
+ },
1540
+ oauth: {
1541
+ name: "D.AI API key",
1542
+ async login(callbacks) {
1543
+ const key = (await callbacks.onPrompt({ message: "D.AI API key (ddsk_...)" })).trim();
1544
+ await saveFileConfig({ apiKey: key });
1545
+ process.env.DAI_API_KEY = key;
1546
+ return {
1547
+ refresh: key,
1548
+ access: key,
1549
+ expires: Date.now() + KEY_TTL_MS
1550
+ };
1556
1551
  },
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`);
1552
+ async refreshToken(credentials) {
1553
+ return credentials;
1571
1554
  },
1572
- api: {
1573
- "openai-completions": openAICompletionsApi(),
1574
- "openai-responses": openAIResponsesApi()
1555
+ getApiKey(credentials) {
1556
+ return credentials.access;
1575
1557
  }
1576
- })
1577
- );
1558
+ }
1559
+ });
1578
1560
  }
1579
- function toPiModels(models, baseUrl) {
1561
+ function toProviderModels(models, baseUrl) {
1580
1562
  return models.map((model) => ({
1581
1563
  id: model.id,
1582
1564
  name: model.name,
1583
1565
  api: model.api,
1584
- provider: "dai",
1585
1566
  baseUrl,
1586
1567
  reasoning: model.reasoning,
1587
1568
  input: model.input,
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.1",
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",