@lidianai/cli 0.1.2 → 0.1.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.
package/README.md CHANGED
@@ -43,6 +43,10 @@ When `--payment-rail x402` is used, CLI performs:
43
43
  Submit feedback later:
44
44
  4. `POST /v1/consume/feedback` with `executionId`, `rank`, optional `feedback`
45
45
 
46
+ Default payment rail behavior when `--payment-rail` is omitted:
47
+ - with API key/token: `prepaid_credits`
48
+ - without auth: `x402`
49
+
46
50
  ## Dev
47
51
 
48
52
  ```bash
package/dist/index.js CHANGED
@@ -254,11 +254,7 @@ var openUrl = (url) => {
254
254
  // src/lib/auth.ts
255
255
  var resolveApiKey = async (argsApiKey) => {
256
256
  const config = await readConfig();
257
- const key = argsApiKey ?? process.env.LIDIAN_API_KEY ?? config.apiKey;
258
- if (!key) {
259
- throw new CliError("Missing API key. Use `lidian login --key ld_...`, pass --api-key, or set LIDIAN_API_KEY.");
260
- }
261
- return key;
257
+ return argsApiKey ?? process.env.LIDIAN_API_KEY ?? config.apiKey;
262
258
  };
263
259
 
264
260
  // src/lib/http.ts
@@ -285,15 +281,26 @@ var createHttpClient = (baseUrl) => {
285
281
  }
286
282
  };
287
283
  };
288
- var authHeaders = (apiKey) => ({
289
- Authorization: `Bearer ${apiKey}`
290
- });
284
+ var authHeaders = (apiKey) => {
285
+ if (!apiKey || apiKey.trim().length === 0) {
286
+ return {};
287
+ }
288
+ return {
289
+ Authorization: `Bearer ${apiKey}`
290
+ };
291
+ };
291
292
  var handleResponse = async (response) => {
292
293
  const json = await response.json().catch(() => null);
293
294
  if (!response.ok) {
294
295
  if (json && "success" in json && json.success === false) {
295
296
  throw new CliError(`${json.error.code}: ${json.error.message}`, 1);
296
297
  }
298
+ if (json && typeof json === "object" && "error" in json && typeof json.error === "string") {
299
+ throw new CliError(json.error, 1);
300
+ }
301
+ if (json && typeof json === "object" && "message" in json && typeof json.message === "string") {
302
+ throw new CliError(json.message, 1);
303
+ }
297
304
  throw new CliError(`Request failed with status ${response.status}`, 1);
298
305
  }
299
306
  if (!json || !("success" in json) || json.success !== true) {
@@ -376,7 +383,7 @@ var main = async () => {
376
383
  }
377
384
  const paramsRaw = asString(parsed.options.params) ?? "{}";
378
385
  const params = parseJsonObject(paramsRaw, "--params");
379
- const paymentRail = asPaymentRail(asString(parsed.options["payment-rail"]) ?? "prepaid_credits");
386
+ const paymentRail = resolvePaymentRail(asString(parsed.options["payment-rail"]), apiKey);
380
387
  const network = asNetwork(asString(parsed.options.network));
381
388
  const result = await runConsumeCommand(http, apiKey, {
382
389
  endpointId: endpointIdValue,
@@ -514,6 +521,12 @@ var asPaymentRail = (value) => {
514
521
  return value;
515
522
  throw new CliError("Invalid --payment-rail. Use prepaid_credits or x402.");
516
523
  };
524
+ var resolvePaymentRail = (value, apiKey) => {
525
+ if (value) {
526
+ return asPaymentRail(value);
527
+ }
528
+ return apiKey && apiKey.trim().length > 0 ? "prepaid_credits" : "x402";
529
+ };
517
530
  var asAuthType = (value) => {
518
531
  if (!value)
519
532
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lidianai/cli",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -17,7 +17,7 @@ export interface AccountApiResponse {
17
17
 
18
18
  export const runAccountCommand = async (
19
19
  http: HttpClient,
20
- apiKey: string,
20
+ apiKey: string | undefined,
21
21
  ): Promise<AccountApiResponse> => {
22
22
  return http.get<AccountApiResponse>("/v1/account", apiKey);
23
23
  };
@@ -34,7 +34,7 @@ export interface ConsumeCommandResult {
34
34
 
35
35
  export const runConsumeCommand = async (
36
36
  http: HttpClient,
37
- apiKey: string,
37
+ apiKey: string | undefined,
38
38
  input: ConsumeCommandInput,
39
39
  ): Promise<ConsumeCommandResult> => {
40
40
  if (!isUuid(input.endpointId)) {
@@ -34,7 +34,7 @@ export interface DiscoverApiResponse {
34
34
 
35
35
  export const runDiscoverCommand = async (
36
36
  http: HttpClient,
37
- apiKey: string,
37
+ apiKey: string | undefined,
38
38
  input: DiscoverCommandInput,
39
39
  ): Promise<DiscoverApiResponse> => {
40
40
  if (input.pageSize < 1 || input.pageSize > 3) {
@@ -17,7 +17,7 @@ export interface FeedbackApiResponse {
17
17
 
18
18
  export const runFeedbackCommand = async (
19
19
  http: HttpClient,
20
- apiKey: string,
20
+ apiKey: string | undefined,
21
21
  input: FeedbackCommandInput,
22
22
  ): Promise<FeedbackApiResponse> => {
23
23
  if (!isUuid(input.executionId)) {
package/src/index.ts CHANGED
@@ -108,8 +108,9 @@ const main = async (): Promise<void> => {
108
108
  }
109
109
  const paramsRaw = asString(parsed.options.params) ?? "{}";
110
110
  const params = parseJsonObject(paramsRaw, "--params");
111
- const paymentRail = asPaymentRail(
112
- asString(parsed.options["payment-rail"]) ?? "prepaid_credits",
111
+ const paymentRail = resolvePaymentRail(
112
+ asString(parsed.options["payment-rail"]),
113
+ apiKey,
113
114
  );
114
115
  const network = asNetwork(asString(parsed.options.network));
115
116
  const result = await runConsumeCommand(http, apiKey, {
@@ -294,6 +295,16 @@ const asPaymentRail = (value: string): PaymentRail => {
294
295
  throw new CliError("Invalid --payment-rail. Use prepaid_credits or x402.");
295
296
  };
296
297
 
298
+ const resolvePaymentRail = (
299
+ value: string | undefined,
300
+ apiKey: string | undefined,
301
+ ): PaymentRail => {
302
+ if (value) {
303
+ return asPaymentRail(value);
304
+ }
305
+ return apiKey && apiKey.trim().length > 0 ? "prepaid_credits" : "x402";
306
+ };
307
+
297
308
  const asAuthType = (
298
309
  value: string | undefined,
299
310
  ):
package/src/lib/auth.ts CHANGED
@@ -1,15 +1,8 @@
1
1
  import { readConfig } from "@/lib/config";
2
- import { CliError } from "@/lib/errors";
3
2
 
4
3
  export const resolveApiKey = async (
5
4
  argsApiKey: string | undefined,
6
- ): Promise<string> => {
5
+ ): Promise<string | undefined> => {
7
6
  const config = await readConfig();
8
- const key = argsApiKey ?? process.env.LIDIAN_API_KEY ?? config.apiKey;
9
- if (!key) {
10
- throw new CliError(
11
- "Missing API key. Use `lidian login --key ld_...`, pass --api-key, or set LIDIAN_API_KEY.",
12
- );
13
- }
14
- return key;
7
+ return argsApiKey ?? process.env.LIDIAN_API_KEY ?? config.apiKey;
15
8
  };
package/src/lib/http.ts CHANGED
@@ -13,25 +13,30 @@ export interface ApiFailure {
13
13
  };
14
14
  }
15
15
 
16
- type ApiResponse<T> = ApiSuccess<T> | ApiFailure;
16
+ interface PlainApiError {
17
+ error?: string;
18
+ message?: string;
19
+ }
20
+
21
+ type ApiResponse<T> = ApiSuccess<T> | ApiFailure | PlainApiError;
17
22
 
18
23
  export interface HttpClient {
19
- get<T>(path: string, apiKey: string): Promise<T>;
20
- post<T, B>(path: string, body: B, apiKey: string): Promise<T>;
24
+ get<T>(path: string, apiKey?: string): Promise<T>;
25
+ post<T, B>(path: string, body: B, apiKey?: string): Promise<T>;
21
26
  }
22
27
 
23
28
  export const createHttpClient = (baseUrl: string): HttpClient => {
24
29
  const normalizedBaseUrl = baseUrl.replace(/\/$/, "");
25
30
 
26
31
  return {
27
- async get<T>(path: string, apiKey: string): Promise<T> {
32
+ async get<T>(path: string, apiKey?: string): Promise<T> {
28
33
  const response = await fetch(`${normalizedBaseUrl}${path}`, {
29
34
  method: "GET",
30
35
  headers: authHeaders(apiKey),
31
36
  });
32
37
  return handleResponse<T>(response);
33
38
  },
34
- async post<T, B>(path: string, body: B, apiKey: string): Promise<T> {
39
+ async post<T, B>(path: string, body: B, apiKey?: string): Promise<T> {
35
40
  const response = await fetch(`${normalizedBaseUrl}${path}`, {
36
41
  method: "POST",
37
42
  headers: {
@@ -45,9 +50,14 @@ export const createHttpClient = (baseUrl: string): HttpClient => {
45
50
  };
46
51
  };
47
52
 
48
- const authHeaders = (apiKey: string): HeadersInit => ({
49
- Authorization: `Bearer ${apiKey}`,
50
- });
53
+ const authHeaders = (apiKey?: string): HeadersInit => {
54
+ if (!apiKey || apiKey.trim().length === 0) {
55
+ return {};
56
+ }
57
+ return {
58
+ Authorization: `Bearer ${apiKey}`,
59
+ };
60
+ };
51
61
 
52
62
  const handleResponse = async <T>(response: Response): Promise<T> => {
53
63
  const json = (await response
@@ -58,6 +68,22 @@ const handleResponse = async <T>(response: Response): Promise<T> => {
58
68
  if (json && "success" in json && json.success === false) {
59
69
  throw new CliError(`${json.error.code}: ${json.error.message}`, 1);
60
70
  }
71
+ if (
72
+ json &&
73
+ typeof json === "object" &&
74
+ "error" in json &&
75
+ typeof json.error === "string"
76
+ ) {
77
+ throw new CliError(json.error, 1);
78
+ }
79
+ if (
80
+ json &&
81
+ typeof json === "object" &&
82
+ "message" in json &&
83
+ typeof json.message === "string"
84
+ ) {
85
+ throw new CliError(json.message, 1);
86
+ }
61
87
  throw new CliError(`Request failed with status ${response.status}`, 1);
62
88
  }
63
89
 
package/src/lib/x402.ts CHANGED
@@ -27,7 +27,7 @@ export interface PaymentVerifyResponse {
27
27
 
28
28
  export const requestPaymentRequirements = async (
29
29
  http: HttpClient,
30
- apiKey: string,
30
+ apiKey: string | undefined,
31
31
  endpointId: string,
32
32
  network?: string,
33
33
  ): Promise<PaymentRequirementsResponse> => {
@@ -43,7 +43,7 @@ export const requestPaymentRequirements = async (
43
43
 
44
44
  export const verifyPaymentAddress = async (
45
45
  http: HttpClient,
46
- apiKey: string,
46
+ apiKey: string | undefined,
47
47
  payTo: string,
48
48
  ): Promise<PaymentVerifyResponse> => {
49
49
  return http.post<PaymentVerifyResponse, { payTo: string }>(