@asframe/opencode-iflow-auth 1.0.3 → 1.0.5

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,6 +1,9 @@
1
- import { spawn, execSync, exec } from 'child_process';
1
+ import { spawn, execSync } from 'child_process';
2
2
  import { createServer } from 'http';
3
3
  import { randomUUID } from 'crypto';
4
+ import { existsSync, readFileSync } from 'fs';
5
+ import { homedir } from 'os';
6
+ import { join } from 'path';
4
7
  const IFLOW_PROXY_PORT = 19998;
5
8
  const IFLOW_PROXY_HOST = '127.0.0.1';
6
9
  const IFLOW_API_BASE = 'https://apis.iflow.cn';
@@ -13,6 +16,12 @@ function log(...args) {
13
16
  console.error('[IFlowProxy]', ...args);
14
17
  }
15
18
  }
19
+ function getIFlowConfigPath() {
20
+ return join(homedir(), '.iflow');
21
+ }
22
+ function getIFlowOAuthCredsPath() {
23
+ return join(getIFlowConfigPath(), 'oauth_creds.json');
24
+ }
16
25
  function requiresCLI(model) {
17
26
  return CLI_REQUIRED_MODELS.some(m => model.includes(m));
18
27
  }
@@ -30,25 +39,27 @@ function checkIFlowCLI() {
30
39
  }
31
40
  }
32
41
  function checkIFlowLogin() {
33
- return new Promise((resolve) => {
34
- try {
35
- exec('iflow whoami', { timeout: 5000 }, (error, stdout, stderr) => {
36
- if (error) {
37
- resolve({ loggedIn: false, error: error.message });
38
- return;
39
- }
40
- const output = stdout + stderr;
41
- if (output.includes('Invalid token') || output.includes('not logged in') || output.includes('Please login')) {
42
- resolve({ loggedIn: false, error: 'Not logged in' });
43
- return;
44
- }
45
- resolve({ loggedIn: true });
46
- });
42
+ try {
43
+ const oauthCredsPath = getIFlowOAuthCredsPath();
44
+ if (!existsSync(oauthCredsPath)) {
45
+ log('OAuth creds file not found:', oauthCredsPath);
46
+ return { loggedIn: false, error: 'Not logged in - no oauth_creds.json' };
47
47
  }
48
- catch (error) {
49
- resolve({ loggedIn: false, error: error.message });
48
+ const credsContent = readFileSync(oauthCredsPath, 'utf-8');
49
+ const creds = JSON.parse(credsContent);
50
+ if (!creds.access_token && !creds.apiKey) {
51
+ return { loggedIn: false, error: 'Not logged in - no token' };
50
52
  }
51
- });
53
+ if (creds.expiry_date && Date.now() > creds.expiry_date) {
54
+ return { loggedIn: false, error: 'Token expired' };
55
+ }
56
+ log('iflow CLI is logged in:', creds.userName || creds.userId);
57
+ return { loggedIn: true, apiKey: creds.apiKey };
58
+ }
59
+ catch (error) {
60
+ log('Error checking iflow login:', error.message);
61
+ return { loggedIn: false, error: error.message };
62
+ }
52
63
  }
53
64
  async function triggerIFlowLogin() {
54
65
  log('Triggering iflow login...');
@@ -130,7 +141,7 @@ export class IFlowCLIProxy {
130
141
  this.cliAvailable = cliCheck.installed;
131
142
  this.cliChecked = true;
132
143
  if (cliCheck.installed) {
133
- const loginCheck = await checkIFlowLogin();
144
+ const loginCheck = checkIFlowLogin();
134
145
  this.cliLoggedIn = loginCheck.loggedIn;
135
146
  if (!loginCheck.loggedIn) {
136
147
  console.error('');
@@ -3,10 +3,11 @@ import { AccountManager, generateAccountId } from "./plugin/accounts.js";
3
3
  import { authorizeIFlowOAuth } from "./iflow/oauth.js";
4
4
  import { validateApiKey } from "./iflow/apikey.js";
5
5
  import { startOAuthServer } from "./plugin/server.js";
6
- import { startProxy } from "./iflow/proxy.js";
6
+ import { startProxy, getProxyInstance } from "./iflow/proxy.js";
7
7
  import { promptApiKey, promptEmail, } from "./plugin/cli.js";
8
8
  import { IFLOW_CONSTANTS, registerIFlowModels } from "./constants.js";
9
9
  const DEBUG = process.env.IFLOW_PROXY_DEBUG === 'true';
10
+ const AUTO_START_PROXY = process.env.IFLOW_AUTO_START_PROXY !== 'false';
10
11
  function log(...args) {
11
12
  if (DEBUG) {
12
13
  console.error('[iflow-proxy]', ...args);
@@ -20,11 +21,25 @@ const openBrowser = (url) => {
20
21
  });
21
22
  }
22
23
  };
24
+ let proxyStarted = false;
25
+ async function ensureProxyStarted() {
26
+ if (!proxyStarted) {
27
+ if (AUTO_START_PROXY) {
28
+ log('Starting CLI proxy...');
29
+ const proxy = await startProxy();
30
+ proxyStarted = true;
31
+ log('CLI proxy started at:', proxy.getBaseUrl());
32
+ return proxy.getBaseUrl();
33
+ }
34
+ }
35
+ return getProxyInstance().getBaseUrl();
36
+ }
23
37
  export const IFlowProxyPlugin = async (input) => {
24
38
  const config = loadConfig();
25
39
  const showToast = (message, variant) => {
26
40
  input.client.tui.showToast({ body: { message, variant } }).catch(() => { });
27
41
  };
42
+ await ensureProxyStarted();
28
43
  return {
29
44
  auth: {
30
45
  provider: "iflow-proxy",
@@ -35,34 +50,39 @@ export const IFlowProxyPlugin = async (input) => {
35
50
  const am = await AccountManager.loadFromDisk(config.account_selection_strategy);
36
51
  log('account count:', am.getAccountCount());
37
52
  registerIFlowModels(provider);
38
- const accountCount = am.getAccountCount();
39
- if (accountCount === 0) {
40
- log('No accounts found in loader');
53
+ const proxyUrl = getProxyInstance().getBaseUrl();
54
+ const proxy = getProxyInstance();
55
+ if (!proxy.isCLIAvailable()) {
56
+ log('CLI not available, returning error');
57
+ showToast('iflow CLI is not available. Please install: npm install -g iflow-cli', 'error');
41
58
  return {};
42
59
  }
43
- const firstAccount = am.getCurrentOrNext();
44
- if (!firstAccount) {
45
- log('No available account');
60
+ if (!proxy.isCLILoggedIn()) {
61
+ log('CLI not logged in, returning error');
62
+ showToast('iflow CLI is not logged in. Please run: iflow login', 'error');
46
63
  return {};
47
64
  }
48
- log('Using account:', firstAccount.email, 'apiKey:', firstAccount.apiKey.substring(0, 10) + '...');
49
- try {
50
- log('Starting CLI proxy...');
51
- const proxy = await startProxy();
52
- const proxyUrl = proxy.getBaseUrl();
53
- log('CLI proxy started at:', proxyUrl);
65
+ const accountCount = am.getAccountCount();
66
+ if (accountCount === 0) {
67
+ log('No accounts found, using CLI auth directly');
54
68
  return {
55
- apiKey: firstAccount.apiKey,
69
+ apiKey: 'cli-auth',
56
70
  baseURL: proxyUrl,
57
71
  };
58
72
  }
59
- catch (err) {
60
- log('Failed to start proxy, using direct API:', err.message);
73
+ const firstAccount = am.getCurrentOrNext();
74
+ if (!firstAccount) {
75
+ log('No available account');
61
76
  return {
62
- apiKey: firstAccount.apiKey,
63
- baseURL: IFLOW_CONSTANTS.BASE_URL,
77
+ apiKey: 'cli-auth',
78
+ baseURL: proxyUrl,
64
79
  };
65
80
  }
81
+ log('Using account:', firstAccount.email, 'apiKey:', firstAccount.apiKey.substring(0, 10) + '...');
82
+ return {
83
+ apiKey: firstAccount.apiKey,
84
+ baseURL: proxyUrl,
85
+ };
66
86
  },
67
87
  methods: [
68
88
  {
@@ -109,7 +129,7 @@ export const IFlowProxyPlugin = async (input) => {
109
129
  url: "",
110
130
  instructions: "Authorization failed",
111
131
  method: "auto",
112
- callback: async () => ({ type: "failed" }),
132
+ callback: async () => ({ type: "failed" })
113
133
  });
114
134
  }
115
135
  }),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@asframe/opencode-iflow-auth",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "OpenCode plugin for iFlow.cn - Access Qwen, DeepSeek, Kimi, GLM-5 models with OAuth 2.0, API Key, and CLI Proxy support",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",