@zhafron/opencode-kiro-auth 1.0.0
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.
- package/README.md +85 -0
- package/dist/constants.d.ts +26 -0
- package/dist/constants.js +60 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +1 -0
- package/dist/kiro/auth.d.ts +5 -0
- package/dist/kiro/auth.js +24 -0
- package/dist/kiro/oauth-idc.d.ts +24 -0
- package/dist/kiro/oauth-idc.js +132 -0
- package/dist/kiro/oauth-social.d.ts +17 -0
- package/dist/kiro/oauth-social.js +51 -0
- package/dist/plugin/accounts.d.ts +28 -0
- package/dist/plugin/accounts.js +156 -0
- package/dist/plugin/auth-page.d.ts +3 -0
- package/dist/plugin/auth-page.js +568 -0
- package/dist/plugin/cli.d.ts +6 -0
- package/dist/plugin/cli.js +98 -0
- package/dist/plugin/config/index.d.ts +3 -0
- package/dist/plugin/config/index.js +2 -0
- package/dist/plugin/config/loader.d.ts +6 -0
- package/dist/plugin/config/loader.js +125 -0
- package/dist/plugin/config/schema.d.ts +44 -0
- package/dist/plugin/config/schema.js +28 -0
- package/dist/plugin/debug.d.ts +2 -0
- package/dist/plugin/debug.js +9 -0
- package/dist/plugin/errors.d.ts +17 -0
- package/dist/plugin/errors.js +34 -0
- package/dist/plugin/logger.d.ts +4 -0
- package/dist/plugin/logger.js +37 -0
- package/dist/plugin/models.d.ts +3 -0
- package/dist/plugin/models.js +14 -0
- package/dist/plugin/oauth-parser.d.ts +5 -0
- package/dist/plugin/oauth-parser.js +23 -0
- package/dist/plugin/quota.d.ts +15 -0
- package/dist/plugin/quota.js +68 -0
- package/dist/plugin/recovery.d.ts +19 -0
- package/dist/plugin/recovery.js +302 -0
- package/dist/plugin/refresh-queue.d.ts +14 -0
- package/dist/plugin/refresh-queue.js +69 -0
- package/dist/plugin/request.d.ts +4 -0
- package/dist/plugin/request.js +240 -0
- package/dist/plugin/response.d.ts +6 -0
- package/dist/plugin/response.js +246 -0
- package/dist/plugin/server.d.ts +24 -0
- package/dist/plugin/server.js +96 -0
- package/dist/plugin/storage.d.ts +7 -0
- package/dist/plugin/storage.js +75 -0
- package/dist/plugin/streaming.d.ts +3 -0
- package/dist/plugin/streaming.js +503 -0
- package/dist/plugin/token.d.ts +2 -0
- package/dist/plugin/token.js +56 -0
- package/dist/plugin/types.d.ts +148 -0
- package/dist/plugin/types.js +0 -0
- package/dist/plugin/usage.d.ts +3 -0
- package/dist/plugin/usage.js +36 -0
- package/dist/plugin.d.ts +32 -0
- package/dist/plugin.js +222 -0
- package/dist/src/constants.d.ts +22 -0
- package/dist/src/constants.js +35 -0
- package/dist/src/kiro/auth.d.ts +5 -0
- package/dist/src/kiro/auth.js +69 -0
- package/dist/src/kiro/oauth-idc.d.ts +22 -0
- package/dist/src/kiro/oauth-idc.js +99 -0
- package/dist/src/kiro/oauth-social.d.ts +17 -0
- package/dist/src/kiro/oauth-social.js +69 -0
- package/dist/src/plugin/accounts.d.ts +23 -0
- package/dist/src/plugin/accounts.js +265 -0
- package/dist/src/plugin/cli.d.ts +6 -0
- package/dist/src/plugin/cli.js +98 -0
- package/dist/src/plugin/config/index.d.ts +3 -0
- package/dist/src/plugin/config/index.js +2 -0
- package/dist/src/plugin/config/loader.d.ts +7 -0
- package/dist/src/plugin/config/loader.js +143 -0
- package/dist/src/plugin/config/schema.d.ts +68 -0
- package/dist/src/plugin/config/schema.js +44 -0
- package/dist/src/plugin/debug.d.ts +2 -0
- package/dist/src/plugin/debug.js +9 -0
- package/dist/src/plugin/errors.d.ts +17 -0
- package/dist/src/plugin/errors.js +34 -0
- package/dist/src/plugin/logger.d.ts +4 -0
- package/dist/src/plugin/logger.js +17 -0
- package/dist/src/plugin/models.d.ts +3 -0
- package/dist/src/plugin/models.js +14 -0
- package/dist/src/plugin/oauth-parser.d.ts +5 -0
- package/dist/src/plugin/oauth-parser.js +23 -0
- package/dist/src/plugin/quota.d.ts +25 -0
- package/dist/src/plugin/quota.js +175 -0
- package/dist/src/plugin/recovery.d.ts +19 -0
- package/dist/src/plugin/recovery.js +302 -0
- package/dist/src/plugin/refresh-queue.d.ts +14 -0
- package/dist/src/plugin/refresh-queue.js +69 -0
- package/dist/src/plugin/request.d.ts +35 -0
- package/dist/src/plugin/request.js +411 -0
- package/dist/src/plugin/response.d.ts +6 -0
- package/dist/src/plugin/response.js +246 -0
- package/dist/src/plugin/server.d.ts +10 -0
- package/dist/src/plugin/server.js +203 -0
- package/dist/src/plugin/storage.d.ts +5 -0
- package/dist/src/plugin/storage.js +106 -0
- package/dist/src/plugin/streaming.d.ts +12 -0
- package/dist/src/plugin/streaming.js +444 -0
- package/dist/src/plugin/token.d.ts +8 -0
- package/dist/src/plugin/token.js +130 -0
- package/dist/src/plugin/types.d.ts +144 -0
- package/dist/src/plugin/types.js +0 -0
- package/dist/src/plugin/usage.d.ts +28 -0
- package/dist/src/plugin/usage.js +159 -0
- package/dist/src/plugin.d.ts +2 -0
- package/dist/src/plugin.js +341 -0
- package/package.json +57 -0
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { existsSync, readFileSync, writeFileSync, mkdirSync } from 'node:fs';
|
|
2
|
+
import { join, dirname } from 'node:path';
|
|
3
|
+
import { homedir } from 'node:os';
|
|
4
|
+
import { AccountSelectionStrategySchema, KiroConfigSchema, RegionSchema, DEFAULT_CONFIG } from './schema';
|
|
5
|
+
import * as logger from '../logger';
|
|
6
|
+
function getConfigDir() {
|
|
7
|
+
const platform = process.platform;
|
|
8
|
+
if (platform === 'win32') {
|
|
9
|
+
return join(process.env.APPDATA || join(homedir(), 'AppData', 'Roaming'), 'opencode');
|
|
10
|
+
}
|
|
11
|
+
const xdgConfig = process.env.XDG_CONFIG_HOME || join(homedir(), '.config');
|
|
12
|
+
return join(xdgConfig, 'opencode');
|
|
13
|
+
}
|
|
14
|
+
export function getUserConfigPath() {
|
|
15
|
+
return join(getConfigDir(), 'kiro.json');
|
|
16
|
+
}
|
|
17
|
+
function ensureUserConfigTemplate() {
|
|
18
|
+
const path = getUserConfigPath();
|
|
19
|
+
if (!existsSync(path)) {
|
|
20
|
+
try {
|
|
21
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
22
|
+
writeFileSync(path, JSON.stringify(DEFAULT_CONFIG, null, 2), 'utf-8');
|
|
23
|
+
logger.log(`Created default config template at ${path}`);
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
logger.warn(`Failed to create config template at ${path}: ${String(error)}`);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
export function getProjectConfigPath(directory) {
|
|
31
|
+
return join(directory, '.opencode', 'kiro.json');
|
|
32
|
+
}
|
|
33
|
+
function loadConfigFile(path) {
|
|
34
|
+
try {
|
|
35
|
+
if (!existsSync(path)) {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
const content = readFileSync(path, 'utf-8');
|
|
39
|
+
const rawConfig = JSON.parse(content);
|
|
40
|
+
const result = KiroConfigSchema.partial().safeParse(rawConfig);
|
|
41
|
+
if (!result.success) {
|
|
42
|
+
const issues = result.error.issues.map((i) => `${i.path.join('.')}: ${i.message}`).join(', ');
|
|
43
|
+
logger.warn(`Config validation error at ${path}: ${issues}`);
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
return result.data;
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
if (error instanceof SyntaxError) {
|
|
50
|
+
logger.warn(`Invalid JSON in config file ${path}: ${error.message}`);
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
logger.warn(`Failed to load config file ${path}: ${String(error)}`);
|
|
54
|
+
}
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
function mergeConfigs(base, override) {
|
|
59
|
+
return {
|
|
60
|
+
...base,
|
|
61
|
+
...override
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
function parseBooleanEnv(value, fallback) {
|
|
65
|
+
if (value === undefined) {
|
|
66
|
+
return fallback;
|
|
67
|
+
}
|
|
68
|
+
if (value === '1' || value === 'true') {
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
if (value === '0' || value === 'false') {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
return fallback;
|
|
75
|
+
}
|
|
76
|
+
function parseNumberEnv(value, fallback) {
|
|
77
|
+
if (value === undefined) {
|
|
78
|
+
return fallback;
|
|
79
|
+
}
|
|
80
|
+
const parsed = Number(value);
|
|
81
|
+
if (isNaN(parsed)) {
|
|
82
|
+
return fallback;
|
|
83
|
+
}
|
|
84
|
+
return parsed;
|
|
85
|
+
}
|
|
86
|
+
function applyEnvOverrides(config) {
|
|
87
|
+
const env = process.env;
|
|
88
|
+
return {
|
|
89
|
+
...config,
|
|
90
|
+
session_recovery: parseBooleanEnv(env.KIRO_SESSION_RECOVERY, config.session_recovery),
|
|
91
|
+
auto_resume: parseBooleanEnv(env.KIRO_AUTO_RESUME, config.auto_resume),
|
|
92
|
+
proactive_token_refresh: parseBooleanEnv(env.KIRO_PROACTIVE_TOKEN_REFRESH, config.proactive_token_refresh),
|
|
93
|
+
token_refresh_interval_seconds: parseNumberEnv(env.KIRO_TOKEN_REFRESH_INTERVAL_SECONDS, config.token_refresh_interval_seconds),
|
|
94
|
+
token_refresh_buffer_seconds: parseNumberEnv(env.KIRO_TOKEN_REFRESH_BUFFER_SECONDS, config.token_refresh_buffer_seconds),
|
|
95
|
+
account_selection_strategy: env.KIRO_ACCOUNT_SELECTION_STRATEGY
|
|
96
|
+
? AccountSelectionStrategySchema.catch('lowest-usage').parse(env.KIRO_ACCOUNT_SELECTION_STRATEGY)
|
|
97
|
+
: config.account_selection_strategy,
|
|
98
|
+
default_region: env.KIRO_DEFAULT_REGION ? RegionSchema.catch('us-east-1').parse(env.KIRO_DEFAULT_REGION) : config.default_region,
|
|
99
|
+
rate_limit_retry_delay_ms: parseNumberEnv(env.KIRO_RATE_LIMIT_RETRY_DELAY_MS, config.rate_limit_retry_delay_ms),
|
|
100
|
+
rate_limit_max_retries: parseNumberEnv(env.KIRO_RATE_LIMIT_MAX_RETRIES, config.rate_limit_max_retries),
|
|
101
|
+
usage_tracking_enabled: parseBooleanEnv(env.KIRO_USAGE_TRACKING_ENABLED, config.usage_tracking_enabled)
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
export function loadConfig(directory) {
|
|
105
|
+
ensureUserConfigTemplate();
|
|
106
|
+
let config = { ...DEFAULT_CONFIG };
|
|
107
|
+
const userConfigPath = getUserConfigPath();
|
|
108
|
+
const userConfig = loadConfigFile(userConfigPath);
|
|
109
|
+
if (userConfig) {
|
|
110
|
+
config = mergeConfigs(config, userConfig);
|
|
111
|
+
}
|
|
112
|
+
const projectConfigPath = getProjectConfigPath(directory);
|
|
113
|
+
const projectConfig = loadConfigFile(projectConfigPath);
|
|
114
|
+
if (projectConfig) {
|
|
115
|
+
config = mergeConfigs(config, projectConfig);
|
|
116
|
+
}
|
|
117
|
+
config = applyEnvOverrides(config);
|
|
118
|
+
return config;
|
|
119
|
+
}
|
|
120
|
+
export function configExists(path) {
|
|
121
|
+
return existsSync(path);
|
|
122
|
+
}
|
|
123
|
+
export function getDefaultLogsDir() {
|
|
124
|
+
return join(getConfigDir(), 'kiro-logs');
|
|
125
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
export declare const AccountSelectionStrategySchema: z.ZodEnum<["sticky", "round-robin", "lowest-usage"]>;
|
|
3
|
+
export type AccountSelectionStrategy = z.infer<typeof AccountSelectionStrategySchema>;
|
|
4
|
+
export declare const RegionSchema: z.ZodEnum<["us-east-1", "us-west-2"]>;
|
|
5
|
+
export type Region = z.infer<typeof RegionSchema>;
|
|
6
|
+
export declare const KiroConfigSchema: z.ZodObject<{
|
|
7
|
+
$schema: z.ZodOptional<z.ZodString>;
|
|
8
|
+
session_recovery: z.ZodDefault<z.ZodBoolean>;
|
|
9
|
+
auto_resume: z.ZodDefault<z.ZodBoolean>;
|
|
10
|
+
proactive_token_refresh: z.ZodDefault<z.ZodBoolean>;
|
|
11
|
+
token_refresh_interval_seconds: z.ZodDefault<z.ZodNumber>;
|
|
12
|
+
token_refresh_buffer_seconds: z.ZodDefault<z.ZodNumber>;
|
|
13
|
+
account_selection_strategy: z.ZodDefault<z.ZodEnum<["sticky", "round-robin", "lowest-usage"]>>;
|
|
14
|
+
default_region: z.ZodDefault<z.ZodEnum<["us-east-1", "us-west-2"]>>;
|
|
15
|
+
rate_limit_retry_delay_ms: z.ZodDefault<z.ZodNumber>;
|
|
16
|
+
rate_limit_max_retries: z.ZodDefault<z.ZodNumber>;
|
|
17
|
+
usage_tracking_enabled: z.ZodDefault<z.ZodBoolean>;
|
|
18
|
+
}, "strip", z.ZodTypeAny, {
|
|
19
|
+
session_recovery: boolean;
|
|
20
|
+
auto_resume: boolean;
|
|
21
|
+
proactive_token_refresh: boolean;
|
|
22
|
+
token_refresh_interval_seconds: number;
|
|
23
|
+
token_refresh_buffer_seconds: number;
|
|
24
|
+
account_selection_strategy: "sticky" | "round-robin" | "lowest-usage";
|
|
25
|
+
default_region: "us-east-1" | "us-west-2";
|
|
26
|
+
rate_limit_retry_delay_ms: number;
|
|
27
|
+
rate_limit_max_retries: number;
|
|
28
|
+
usage_tracking_enabled: boolean;
|
|
29
|
+
$schema?: string | undefined;
|
|
30
|
+
}, {
|
|
31
|
+
$schema?: string | undefined;
|
|
32
|
+
session_recovery?: boolean | undefined;
|
|
33
|
+
auto_resume?: boolean | undefined;
|
|
34
|
+
proactive_token_refresh?: boolean | undefined;
|
|
35
|
+
token_refresh_interval_seconds?: number | undefined;
|
|
36
|
+
token_refresh_buffer_seconds?: number | undefined;
|
|
37
|
+
account_selection_strategy?: "sticky" | "round-robin" | "lowest-usage" | undefined;
|
|
38
|
+
default_region?: "us-east-1" | "us-west-2" | undefined;
|
|
39
|
+
rate_limit_retry_delay_ms?: number | undefined;
|
|
40
|
+
rate_limit_max_retries?: number | undefined;
|
|
41
|
+
usage_tracking_enabled?: boolean | undefined;
|
|
42
|
+
}>;
|
|
43
|
+
export type KiroConfig = z.infer<typeof KiroConfigSchema>;
|
|
44
|
+
export declare const DEFAULT_CONFIG: KiroConfig;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
export const AccountSelectionStrategySchema = z.enum(['sticky', 'round-robin', 'lowest-usage']);
|
|
3
|
+
export const RegionSchema = z.enum(['us-east-1', 'us-west-2']);
|
|
4
|
+
export const KiroConfigSchema = z.object({
|
|
5
|
+
$schema: z.string().optional(),
|
|
6
|
+
session_recovery: z.boolean().default(true),
|
|
7
|
+
auto_resume: z.boolean().default(true),
|
|
8
|
+
proactive_token_refresh: z.boolean().default(true),
|
|
9
|
+
token_refresh_interval_seconds: z.number().min(60).max(3600).default(300),
|
|
10
|
+
token_refresh_buffer_seconds: z.number().min(60).max(1800).default(600),
|
|
11
|
+
account_selection_strategy: AccountSelectionStrategySchema.default('lowest-usage'),
|
|
12
|
+
default_region: RegionSchema.default('us-east-1'),
|
|
13
|
+
rate_limit_retry_delay_ms: z.number().min(1000).max(60000).default(5000),
|
|
14
|
+
rate_limit_max_retries: z.number().min(0).max(10).default(3),
|
|
15
|
+
usage_tracking_enabled: z.boolean().default(true)
|
|
16
|
+
});
|
|
17
|
+
export const DEFAULT_CONFIG = {
|
|
18
|
+
session_recovery: true,
|
|
19
|
+
auto_resume: true,
|
|
20
|
+
proactive_token_refresh: true,
|
|
21
|
+
token_refresh_interval_seconds: 300,
|
|
22
|
+
token_refresh_buffer_seconds: 600,
|
|
23
|
+
account_selection_strategy: 'lowest-usage',
|
|
24
|
+
default_region: 'us-east-1',
|
|
25
|
+
rate_limit_retry_delay_ms: 5000,
|
|
26
|
+
rate_limit_max_retries: 3,
|
|
27
|
+
usage_tracking_enabled: true
|
|
28
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export function isDebugEnabled() {
|
|
2
|
+
return !!process.env.DEBUG;
|
|
3
|
+
}
|
|
4
|
+
export function debugLog(context, message, data) {
|
|
5
|
+
if (isDebugEnabled()) {
|
|
6
|
+
const formattedData = data !== undefined ? ` ${JSON.stringify(data)}` : '';
|
|
7
|
+
console.debug(`[${context}] ${message}${formattedData}`);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export declare class KiroTokenRefreshError extends Error {
|
|
2
|
+
code?: string;
|
|
3
|
+
originalError?: Error;
|
|
4
|
+
constructor(message: string, code?: string, originalError?: Error);
|
|
5
|
+
}
|
|
6
|
+
export declare class KiroQuotaExhaustedError extends Error {
|
|
7
|
+
recoveryTime?: number;
|
|
8
|
+
constructor(message: string, recoveryTime?: number);
|
|
9
|
+
}
|
|
10
|
+
export declare class KiroRateLimitError extends Error {
|
|
11
|
+
retryAfter?: number;
|
|
12
|
+
constructor(message: string, retryAfter?: number);
|
|
13
|
+
}
|
|
14
|
+
export declare class KiroAuthError extends Error {
|
|
15
|
+
statusCode?: number;
|
|
16
|
+
constructor(message: string, statusCode?: number);
|
|
17
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export class KiroTokenRefreshError extends Error {
|
|
2
|
+
code;
|
|
3
|
+
originalError;
|
|
4
|
+
constructor(message, code, originalError) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.name = 'KiroTokenRefreshError';
|
|
7
|
+
this.code = code;
|
|
8
|
+
this.originalError = originalError;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export class KiroQuotaExhaustedError extends Error {
|
|
12
|
+
recoveryTime;
|
|
13
|
+
constructor(message, recoveryTime) {
|
|
14
|
+
super(message);
|
|
15
|
+
this.name = 'KiroQuotaExhaustedError';
|
|
16
|
+
this.recoveryTime = recoveryTime;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export class KiroRateLimitError extends Error {
|
|
20
|
+
retryAfter;
|
|
21
|
+
constructor(message, retryAfter) {
|
|
22
|
+
super(message);
|
|
23
|
+
this.name = 'KiroRateLimitError';
|
|
24
|
+
this.retryAfter = retryAfter;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export class KiroAuthError extends Error {
|
|
28
|
+
statusCode;
|
|
29
|
+
constructor(message, statusCode) {
|
|
30
|
+
super(message);
|
|
31
|
+
this.name = 'KiroAuthError';
|
|
32
|
+
this.statusCode = statusCode;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare function log(message: string, ...args: unknown[]): void;
|
|
2
|
+
export declare function error(message: string, ...args: unknown[]): void;
|
|
3
|
+
export declare function warn(message: string, ...args: unknown[]): void;
|
|
4
|
+
export declare function debug(message: string, ...args: unknown[]): void;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { appendFileSync, mkdirSync } from 'node:fs';
|
|
2
|
+
import { homedir } from 'node:os';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
const getLogDir = () => {
|
|
5
|
+
const platform = process.platform;
|
|
6
|
+
const base = platform === 'win32' ? join(process.env.APPDATA || join(homedir(), 'AppData', 'Roaming'), 'opencode') : join(process.env.XDG_CONFIG_HOME || join(homedir(), '.config'), 'opencode');
|
|
7
|
+
return join(base, 'kiro-logs');
|
|
8
|
+
};
|
|
9
|
+
const writeToFile = (level, message, ...args) => {
|
|
10
|
+
try {
|
|
11
|
+
const dir = getLogDir();
|
|
12
|
+
mkdirSync(dir, { recursive: true });
|
|
13
|
+
const path = join(dir, 'plugin.log');
|
|
14
|
+
const timestamp = new Date().toISOString();
|
|
15
|
+
const content = `[${timestamp}] ${level}: ${message} ${args.map((a) => (typeof a === 'object' ? JSON.stringify(a) : String(a))).join(' ')}\n`;
|
|
16
|
+
appendFileSync(path, content);
|
|
17
|
+
}
|
|
18
|
+
catch (e) { }
|
|
19
|
+
};
|
|
20
|
+
export function log(message, ...args) {
|
|
21
|
+
console.log(`[${new Date().toISOString()}] ${message}`, ...args);
|
|
22
|
+
writeToFile('INFO', message, ...args);
|
|
23
|
+
}
|
|
24
|
+
export function error(message, ...args) {
|
|
25
|
+
console.error(`[${new Date().toISOString()}] ERROR: ${message}`, ...args);
|
|
26
|
+
writeToFile('ERROR', message, ...args);
|
|
27
|
+
}
|
|
28
|
+
export function warn(message, ...args) {
|
|
29
|
+
console.warn(`[${new Date().toISOString()}] WARN: ${message}`, ...args);
|
|
30
|
+
writeToFile('WARN', message, ...args);
|
|
31
|
+
}
|
|
32
|
+
export function debug(message, ...args) {
|
|
33
|
+
if (process.env.DEBUG) {
|
|
34
|
+
console.debug(`[${new Date().toISOString()}] DEBUG: ${message}`, ...args);
|
|
35
|
+
writeToFile('DEBUG', message, ...args);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { MODEL_MAPPING, SUPPORTED_MODELS } from '../constants';
|
|
2
|
+
export function resolveKiroModel(model) {
|
|
3
|
+
const resolved = MODEL_MAPPING[model];
|
|
4
|
+
if (!resolved) {
|
|
5
|
+
throw new Error(`Unsupported model: ${model}. Supported models: ${SUPPORTED_MODELS.join(', ')}`);
|
|
6
|
+
}
|
|
7
|
+
return resolved;
|
|
8
|
+
}
|
|
9
|
+
export function getSupportedModels() {
|
|
10
|
+
return SUPPORTED_MODELS;
|
|
11
|
+
}
|
|
12
|
+
export function isModelSupported(model) {
|
|
13
|
+
return SUPPORTED_MODELS.includes(model);
|
|
14
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export function parseOAuthCallbackInput(input) {
|
|
2
|
+
const trimmed = input.trim();
|
|
3
|
+
try {
|
|
4
|
+
const url = new URL(trimmed);
|
|
5
|
+
const code = url.searchParams.get('code');
|
|
6
|
+
const state = url.searchParams.get('state');
|
|
7
|
+
if (!code || !state) {
|
|
8
|
+
throw new Error('Missing code or state in URL');
|
|
9
|
+
}
|
|
10
|
+
return { code, state };
|
|
11
|
+
}
|
|
12
|
+
catch {
|
|
13
|
+
const codeMatch = trimmed.match(/code=([^&\s]+)/);
|
|
14
|
+
const stateMatch = trimmed.match(/state=([^&\s]+)/);
|
|
15
|
+
if (codeMatch?.[1] && stateMatch?.[1]) {
|
|
16
|
+
return {
|
|
17
|
+
code: decodeURIComponent(codeMatch[1]),
|
|
18
|
+
state: decodeURIComponent(stateMatch[1]),
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
throw new Error('Invalid OAuth callback format. Expected full URL or query string with code and state parameters.');
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { ManagedAccount } from './types';
|
|
2
|
+
export type QuotaStatus = 'healthy' | 'warning' | 'exhausted';
|
|
3
|
+
export interface QuotaInfo {
|
|
4
|
+
status: QuotaStatus;
|
|
5
|
+
used: number;
|
|
6
|
+
limit: number;
|
|
7
|
+
remaining: number;
|
|
8
|
+
percentage: number;
|
|
9
|
+
recoveryTime?: number;
|
|
10
|
+
}
|
|
11
|
+
export declare function checkQuotaStatus(account: ManagedAccount): QuotaStatus;
|
|
12
|
+
export declare function updateAccountQuota(account: ManagedAccount, usage: any, accountManager?: any): void;
|
|
13
|
+
export declare function sortAccountsByQuota(accounts: ManagedAccount[]): ManagedAccount[];
|
|
14
|
+
export declare function filterHealthyAccounts(accounts: ManagedAccount[]): ManagedAccount[];
|
|
15
|
+
export declare function getAccountWithMostQuota(accounts: ManagedAccount[]): ManagedAccount | null;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { calculateUsagePercentage, isQuotaExhausted, getRemainingCount } from './usage';
|
|
2
|
+
const WARNING_THRESHOLD_PERCENT = 80;
|
|
3
|
+
export function checkQuotaStatus(account) {
|
|
4
|
+
if (!account.usedCount || !account.limitCount) {
|
|
5
|
+
return 'healthy';
|
|
6
|
+
}
|
|
7
|
+
const usage = {
|
|
8
|
+
usedCount: account.usedCount,
|
|
9
|
+
limitCount: account.limitCount,
|
|
10
|
+
};
|
|
11
|
+
if (isQuotaExhausted(usage)) {
|
|
12
|
+
return 'exhausted';
|
|
13
|
+
}
|
|
14
|
+
const percentage = calculateUsagePercentage(usage);
|
|
15
|
+
if (percentage >= WARNING_THRESHOLD_PERCENT) {
|
|
16
|
+
return 'warning';
|
|
17
|
+
}
|
|
18
|
+
return 'healthy';
|
|
19
|
+
}
|
|
20
|
+
export function updateAccountQuota(account, usage, accountManager) {
|
|
21
|
+
const metadata = {
|
|
22
|
+
usedCount: typeof usage.usedCount === 'number' ? usage.usedCount : 0,
|
|
23
|
+
limitCount: typeof usage.limitCount === 'number' ? usage.limitCount : 0,
|
|
24
|
+
realEmail: usage.email
|
|
25
|
+
};
|
|
26
|
+
account.usedCount = metadata.usedCount;
|
|
27
|
+
account.limitCount = metadata.limitCount;
|
|
28
|
+
if (metadata.realEmail) {
|
|
29
|
+
account.realEmail = metadata.realEmail;
|
|
30
|
+
}
|
|
31
|
+
if (accountManager) {
|
|
32
|
+
accountManager.updateUsage(account.id, metadata);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
export function sortAccountsByQuota(accounts) {
|
|
36
|
+
return [...accounts].sort((a, b) => {
|
|
37
|
+
const aRemaining = getRemainingQuota(a);
|
|
38
|
+
const bRemaining = getRemainingQuota(b);
|
|
39
|
+
return bRemaining - aRemaining;
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
function getRemainingQuota(account) {
|
|
43
|
+
if (!account.usedCount || !account.limitCount) {
|
|
44
|
+
return Infinity;
|
|
45
|
+
}
|
|
46
|
+
const usage = {
|
|
47
|
+
usedCount: account.usedCount,
|
|
48
|
+
limitCount: account.limitCount,
|
|
49
|
+
};
|
|
50
|
+
return getRemainingCount(usage);
|
|
51
|
+
}
|
|
52
|
+
export function filterHealthyAccounts(accounts) {
|
|
53
|
+
return accounts.filter(account => {
|
|
54
|
+
if (!account.isHealthy) {
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
const status = checkQuotaStatus(account);
|
|
58
|
+
return status !== 'exhausted';
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
export function getAccountWithMostQuota(accounts) {
|
|
62
|
+
const healthyAccounts = filterHealthyAccounts(accounts);
|
|
63
|
+
if (healthyAccounts.length === 0) {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
const sorted = sortAccountsByQuota(healthyAccounts);
|
|
67
|
+
return sorted[0] || null;
|
|
68
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export interface RecoveryState {
|
|
2
|
+
sessionId: string;
|
|
3
|
+
conversationId: string;
|
|
4
|
+
model: string;
|
|
5
|
+
lastMessageIndex: number;
|
|
6
|
+
errorCount: number;
|
|
7
|
+
lastError?: string;
|
|
8
|
+
timestamp: number;
|
|
9
|
+
}
|
|
10
|
+
export interface RecoveryStorage {
|
|
11
|
+
version: 1;
|
|
12
|
+
sessions: Record<string, RecoveryState>;
|
|
13
|
+
}
|
|
14
|
+
export interface SessionRecoveryHook {
|
|
15
|
+
handleSessionError(error: any, sessionId: string): Promise<boolean>;
|
|
16
|
+
isRecoverableError(error: any): boolean;
|
|
17
|
+
clearSession(sessionId: string): Promise<void>;
|
|
18
|
+
}
|
|
19
|
+
export declare function createSessionRecoveryHook(enabled: boolean, autoResume: boolean): SessionRecoveryHook;
|