@kweaver-ai/kweaver-sdk 0.4.1 → 0.4.4

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,38 +1,18 @@
1
- import { clearPlatformSession, deletePlatform, getConfigDir, getCurrentPlatform, getPlatformAlias, hasPlatform, listPlatforms, resolvePlatformIdentifier, setCurrentPlatform, setPlatformAlias, } from "../config/store.js";
2
- import { callLogoutEndpoint, formatHttpError, getStoredAuthSummary, login, normalizeBaseUrl, } from "../auth/oauth.js";
3
- export function getClientProvisioningMessage(created) {
4
- return created ? "Registered a new OAuth client." : "Reusing existing OAuth client.";
5
- }
6
- export function formatAuthStatusSummary(input) {
7
- const lines = [
8
- `Config directory: ${getConfigDir()}`,
9
- `Platform: ${input.client.baseUrl}`,
10
- `Current platform: ${input.isCurrent ? "yes" : "no"}`,
11
- `Client ID: ${input.client.clientId}`,
12
- `Redirect URI: ${input.client.redirectUri}`,
13
- `Token present: ${input.token ? "yes" : "no"}`,
14
- `Callback recorded: ${input.callback ? "yes" : "no"}`,
15
- ];
16
- if (input.client.product) {
17
- lines.push(`Product: ${input.client.product}`);
18
- }
19
- if (input.client.lang) {
20
- lines.push(`Lang: ${input.client.lang}`);
21
- }
22
- if (input.token?.expiresAt) {
23
- lines.push(`Token expires at: ${input.token.expiresAt}`);
24
- }
25
- if (input.callback?.receivedAt) {
26
- lines.push(`Last callback at: ${input.callback.receivedAt}`);
27
- }
28
- if (input.callback?.scope) {
29
- lines.push(`Last callback scope: ${input.callback.scope}`);
30
- }
31
- return lines;
32
- }
1
+ import { clearPlatformSession, deletePlatform, getConfigDir, getCurrentPlatform, getPlatformAlias, hasPlatform, listPlatforms, loadTokenConfig, resolvePlatformIdentifier, setCurrentPlatform, setPlatformAlias, } from "../config/store.js";
2
+ import { formatHttpError, normalizeBaseUrl, playwrightLogin, } from "../auth/oauth.js";
33
3
  export async function runAuthCommand(args) {
34
4
  const target = args[0];
35
5
  const rest = args.slice(1);
6
+ if (!target || target === "--help" || target === "-h") {
7
+ console.log(`kweaver auth login <url> Login to a platform (browser login)
8
+ kweaver auth <url> Login (shorthand)
9
+ kweaver auth status [url] Show current auth status
10
+ kweaver auth list List saved platforms
11
+ kweaver auth use <url> Switch active platform
12
+ kweaver auth logout [url] Logout (clear local token)
13
+ kweaver auth delete <url> Delete saved credentials`);
14
+ return 0;
15
+ }
36
16
  if (target === "login") {
37
17
  const url = rest[0];
38
18
  if (!url) {
@@ -44,47 +24,34 @@ export async function runAuthCommand(args) {
44
24
  if (target && target !== "status" && target !== "list" && target !== "use" && target !== "delete" && target !== "logout") {
45
25
  try {
46
26
  const normalizedTarget = normalizeBaseUrl(target);
47
- const port = Number(readOption(args, "--port") ?? "9010");
48
- const clientName = readOption(args, "--client-name") ?? "kweaver";
49
27
  const alias = readOption(args, "--alias");
50
- const host = readOption(args, "--host");
51
- const redirectUriOverride = readOption(args, "--redirect-uri");
52
- const forceRegister = args.includes("--force-register");
53
- const open = !args.includes("--no-open");
54
- const lang = readOption(args, "--lang") ?? "zh-cn";
55
- const product = readOption(args, "--product") ?? "adp";
56
- const xForwardedPrefix = readOption(args, "--x-forwarded-prefix") ?? "";
57
- const result = await login({
58
- baseUrl: normalizedTarget,
59
- port,
60
- clientName,
61
- open,
62
- forceRegister,
63
- host,
64
- redirectUriOverride,
65
- lang,
66
- product,
67
- xForwardedPrefix,
68
- });
28
+ const username = readOption(args, "--username") ?? readOption(args, "-u");
29
+ const password = readOption(args, "--password") ?? readOption(args, "-p");
30
+ if (username && password) {
31
+ console.log("Logging in (headless)...");
32
+ }
33
+ else {
34
+ console.log("Opening browser for login...");
35
+ }
36
+ const token = await playwrightLogin(normalizedTarget, username && password ? { username, password } : undefined);
69
37
  if (alias) {
70
38
  setPlatformAlias(normalizedTarget, alias);
71
39
  }
72
40
  console.log(`Config directory: ${getConfigDir()}`);
73
- console.log(getClientProvisioningMessage(result.created));
74
- console.log(`Client ID: ${result.client.clientId}`);
75
41
  if (alias) {
76
42
  console.log(`Alias: ${alias.toLowerCase()}`);
77
43
  }
78
44
  else {
79
- const savedAlias = getPlatformAlias(result.client.baseUrl);
45
+ const savedAlias = getPlatformAlias(normalizedTarget);
80
46
  if (savedAlias) {
81
47
  console.log(`Alias: ${savedAlias}`);
82
48
  }
83
49
  }
84
- console.log(`Authorization URL: ${result.authorizationUrl}`);
85
- console.log(`Callback received at: ${result.callback.receivedAt}`);
86
- console.log(`Current platform: ${result.client.baseUrl}`);
50
+ console.log(`Current platform: ${normalizedTarget}`);
87
51
  console.log(`Access token saved: yes`);
52
+ if (token.expiresAt) {
53
+ console.log(`Token expires at: ${token.expiresAt}`);
54
+ }
88
55
  return 0;
89
56
  }
90
57
  catch (error) {
@@ -95,18 +62,35 @@ export async function runAuthCommand(args) {
95
62
  if (target === "status") {
96
63
  const resolvedTarget = args[1] ? resolvePlatformIdentifier(args[1]) : undefined;
97
64
  const statusTarget = resolvedTarget && /^https?:\/\//.test(resolvedTarget) ? normalizeBaseUrl(resolvedTarget) : resolvedTarget ?? undefined;
98
- const { client, token, callback } = getStoredAuthSummary(statusTarget);
99
- if (!client) {
100
- console.error(statusTarget ? `No saved client config found for ${statusTarget}.` : "No saved client config found.");
65
+ const platform = statusTarget ?? getCurrentPlatform();
66
+ if (!platform) {
67
+ console.error("No active platform. Run `kweaver auth login <platform-url>` first.");
68
+ return 1;
69
+ }
70
+ const token = loadTokenConfig(platform);
71
+ if (!token) {
72
+ console.error(statusTarget ? `No saved token for ${statusTarget}.` : "No saved token found.");
101
73
  return 1;
102
74
  }
103
75
  const currentPlatform = getCurrentPlatform();
104
- for (const line of formatAuthStatusSummary({
105
- client,
106
- token,
107
- callback,
108
- isCurrent: currentPlatform === client.baseUrl,
109
- })) {
76
+ const lines = [
77
+ `Config directory: ${getConfigDir()}`,
78
+ `Platform: ${token.baseUrl}`,
79
+ `Current platform: ${token.baseUrl === currentPlatform ? "yes" : "no"}`,
80
+ `Token present: yes`,
81
+ ];
82
+ if (token.expiresAt) {
83
+ const expiry = new Date(token.expiresAt);
84
+ const remainingMs = expiry.getTime() - Date.now();
85
+ if (remainingMs > 0) {
86
+ const remainingMin = Math.ceil(remainingMs / 60_000);
87
+ lines.push(`Token status: active (expires in ${remainingMin} min)`);
88
+ }
89
+ else {
90
+ lines.push(`Token status: expired (run \`kweaver auth login ${token.baseUrl}\` again)`);
91
+ }
92
+ }
93
+ for (const line of lines) {
110
94
  console.log(line);
111
95
  }
112
96
  return 0;
@@ -134,7 +118,7 @@ export async function runAuthCommand(args) {
134
118
  return 1;
135
119
  }
136
120
  if (!hasPlatform(useTarget)) {
137
- console.error(`No saved client config found for ${useTarget}. Run \`kweaver auth ${useTarget}\` first.`);
121
+ console.error(`No saved token for ${useTarget}. Run \`kweaver auth login ${useTarget}\` first.`);
138
122
  return 1;
139
123
  }
140
124
  setCurrentPlatform(useTarget);
@@ -149,7 +133,7 @@ export async function runAuthCommand(args) {
149
133
  return 1;
150
134
  }
151
135
  if (!hasPlatform(deleteTarget)) {
152
- console.error(`No saved client config found for ${deleteTarget}.`);
136
+ console.error(`No saved token for ${deleteTarget}.`);
153
137
  return 1;
154
138
  }
155
139
  const wasCurrent = getCurrentPlatform() === deleteTarget;
@@ -166,25 +150,20 @@ export async function runAuthCommand(args) {
166
150
  const logoutTarget = resolvedTarget && /^https?:\/\//.test(resolvedTarget) ? normalizeBaseUrl(resolvedTarget) : resolvedTarget;
167
151
  if (!logoutTarget) {
168
152
  console.error("Usage: kweaver auth logout [platform-url|alias]");
169
- console.error("No current platform. Specify a platform to logout, e.g. kweaver auth logout <platform-url|alias>");
153
+ console.error("No current platform. Specify a platform to logout.");
170
154
  return 1;
171
155
  }
172
156
  if (!hasPlatform(logoutTarget)) {
173
- console.error(`No saved client config found for ${logoutTarget}.`);
157
+ console.error(`No saved token for ${logoutTarget}.`);
174
158
  return 1;
175
159
  }
176
- const { client, token } = getStoredAuthSummary(logoutTarget);
177
- if (client) {
178
- await callLogoutEndpoint(client, token);
179
- }
180
160
  clearPlatformSession(logoutTarget);
181
161
  console.log(`Logged out: ${logoutTarget}`);
182
- console.log(`Run \`kweaver auth ${logoutTarget}\` to sign in again (e.g. as a different user).`);
162
+ console.log(`Run \`kweaver auth login ${logoutTarget}\` to sign in again.`);
183
163
  return 0;
184
164
  }
185
- console.error("Usage: kweaver auth <platform-url>");
186
- console.error(" kweaver auth login <platform-url>");
187
- console.error(" kweaver auth <platform-url> [--alias <name>] [--no-open] [--host <host>] [--redirect-uri <uri>]");
165
+ console.error("Usage: kweaver auth login <platform-url>");
166
+ console.error(" kweaver auth <platform-url> [--alias <name>]");
188
167
  console.error(" kweaver auth status [platform-url|alias]");
189
168
  console.error(" kweaver auth list");
190
169
  console.error(" kweaver auth use <platform-url|alias>");
@@ -41,6 +41,20 @@ export interface KnDeleteOptions {
41
41
  yes: boolean;
42
42
  }
43
43
  export declare function parseKnDeleteArgs(args: string[]): KnDeleteOptions;
44
+ export interface KnPushOptions {
45
+ directory: string;
46
+ branch: string;
47
+ businessDomain: string;
48
+ pretty: boolean;
49
+ }
50
+ export declare function parseKnPushArgs(args: string[]): KnPushOptions;
51
+ export interface KnPullOptions {
52
+ knId: string;
53
+ directory: string;
54
+ branch: string;
55
+ businessDomain: string;
56
+ }
57
+ export declare function parseKnPullArgs(args: string[]): KnPullOptions;
44
58
  export interface KnObjectTypeQueryOptions {
45
59
  knId: string;
46
60
  otId: string;
@@ -66,6 +80,8 @@ export declare function parseKnBuildArgs(args: string[]): {
66
80
  timeout: number;
67
81
  businessDomain: string;
68
82
  };
83
+ export declare function packDirectoryToTar(dirPath: string): Buffer;
84
+ export declare function extractTarToDirectory(tarBuffer: Buffer, dirPath: string): void;
69
85
  export declare function parseKnSearchArgs(args: string[]): {
70
86
  knId: string;
71
87
  query: string;