pi-xai-oauth 1.0.2 → 1.0.3

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.
@@ -1,17 +1,48 @@
1
- import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
1
+ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
2
2
  import type { OAuthCredentials, OAuthLoginCallbacks } from "@earendil-works/pi-ai";
3
+ import { readFileSync, existsSync } from "fs";
4
+ import { homedir } from "os";
5
+ import { join } from "path";
6
+
7
+ function getGrokAuthToken(): string | null {
8
+ const authPath = join(homedir(), ".grok", "auth.json");
9
+ if (existsSync(authPath)) {
10
+ try {
11
+ const data = JSON.parse(readFileSync(authPath, "utf8"));
12
+ return data.access_token || data.token || null;
13
+ } catch {
14
+ return null;
15
+ }
16
+ }
17
+ return null;
18
+ }
3
19
 
4
20
  export default function (pi: ExtensionAPI) {
5
21
  pi.registerProvider("xai-oauth", {
6
22
  name: "xAI (OAuth)",
7
23
  baseUrl: "https://api.x.ai/v1",
8
- api: "openai-completions",
24
+ api: "openai-responses",
9
25
  authHeader: true,
10
26
 
11
27
  oauth: {
12
28
  name: "xAI (Grok)",
13
29
 
14
30
  async login(callbacks: OAuthLoginCallbacks): Promise<OAuthCredentials> {
31
+ // Check for existing Grok auth file first
32
+ const existingToken = getGrokAuthToken();
33
+ if (existingToken) {
34
+ const useExisting = await callbacks.onPrompt({
35
+ message: "Found existing Grok auth. Use it? (y/n)"
36
+ });
37
+ if (useExisting.toLowerCase().startsWith("y")) {
38
+ return {
39
+ refresh: "",
40
+ access: existingToken,
41
+ expires: Date.now() + 1000 * 60 * 60 * 24 * 30,
42
+ };
43
+ }
44
+ }
45
+
15
46
  const accessToken = await callbacks.onPrompt({
16
47
  message:
17
48
  "Paste your xAI API key (starts with xai-).\n" +
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-xai-oauth",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "xAI (Grok) provider with OAuth support for pi",
5
5
  "keywords": ["pi-package"],
6
6
  "license": "MIT",