@aigne/aigne-hub 0.4.8 → 0.4.9
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/CHANGELOG.md +28 -0
- package/lib/cjs/blocklet-aigne-hub-model.js +4 -3
- package/lib/cjs/cli-aigne-hub-model.js +1 -1
- package/lib/cjs/index.d.ts +5 -1
- package/lib/cjs/index.js +8 -3
- package/lib/cjs/util/constants.d.ts +13 -0
- package/lib/cjs/util/constants.js +29 -0
- package/lib/cjs/util/credential.d.ts +24 -0
- package/lib/cjs/util/credential.js +223 -0
- package/lib/cjs/util/crypto.d.ts +4 -0
- package/lib/cjs/util/crypto.js +19 -0
- package/lib/cjs/util/model.d.ts +12 -0
- package/lib/cjs/util/model.js +201 -0
- package/lib/cjs/util/type.d.ts +58 -0
- package/lib/cjs/util/type.js +2 -0
- package/lib/dts/index.d.ts +5 -1
- package/lib/dts/util/constants.d.ts +13 -0
- package/lib/dts/util/credential.d.ts +24 -0
- package/lib/dts/util/crypto.d.ts +4 -0
- package/lib/dts/util/model.d.ts +12 -0
- package/lib/dts/util/type.d.ts +58 -0
- package/lib/esm/blocklet-aigne-hub-model.js +2 -1
- package/lib/esm/cli-aigne-hub-model.js +1 -1
- package/lib/esm/index.d.ts +5 -1
- package/lib/esm/index.js +7 -2
- package/lib/esm/util/constants.d.ts +13 -0
- package/lib/esm/util/constants.js +15 -0
- package/lib/esm/util/credential.d.ts +24 -0
- package/lib/esm/util/credential.js +211 -0
- package/lib/esm/util/crypto.d.ts +4 -0
- package/lib/esm/util/crypto.js +9 -0
- package/lib/esm/util/model.d.ts +12 -0
- package/lib/esm/{constants.js → util/model.js} +91 -18
- package/lib/esm/util/type.d.ts +58 -0
- package/lib/esm/util/type.js +1 -0
- package/package.json +22 -15
- package/lib/cjs/constants.d.ts +0 -28
- package/lib/cjs/constants.js +0 -123
- package/lib/dts/constants.d.ts +0 -28
- package/lib/esm/constants.d.ts +0 -28
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { ChatModel, ChatModelOptions } from "@aigne/core/agents/chat-model.js";
|
|
2
|
+
export type Model = {
|
|
3
|
+
provider?: string | null;
|
|
4
|
+
name?: string | null;
|
|
5
|
+
temperature?: number | null;
|
|
6
|
+
topP?: number | null;
|
|
7
|
+
frequencyPenalty?: number | null;
|
|
8
|
+
presencePenalty?: number | null;
|
|
9
|
+
} | undefined;
|
|
10
|
+
type InquirerPromptFn = (prompt: {
|
|
11
|
+
type: string;
|
|
12
|
+
name: string;
|
|
13
|
+
message: string;
|
|
14
|
+
choices: {
|
|
15
|
+
name: string;
|
|
16
|
+
value: any;
|
|
17
|
+
}[];
|
|
18
|
+
default: any;
|
|
19
|
+
}) => Promise<any>;
|
|
20
|
+
export type LoadCredentialOptions = {
|
|
21
|
+
model?: string;
|
|
22
|
+
aigneHubUrl?: string;
|
|
23
|
+
inquirerPromptFn?: InquirerPromptFn;
|
|
24
|
+
};
|
|
25
|
+
export interface LoadableModel {
|
|
26
|
+
name: string | string[];
|
|
27
|
+
apiKeyEnvName?: string | string[];
|
|
28
|
+
create: (options: {
|
|
29
|
+
model?: string;
|
|
30
|
+
modelOptions?: ChatModelOptions;
|
|
31
|
+
apiKey?: string;
|
|
32
|
+
url?: string;
|
|
33
|
+
}) => ChatModel;
|
|
34
|
+
}
|
|
35
|
+
export type FetchResult = {
|
|
36
|
+
accessKeyId: string;
|
|
37
|
+
accessKeySecret: string;
|
|
38
|
+
};
|
|
39
|
+
export type BaseWrapSpinner = (_: string, waiting: () => Promise<FetchResult>) => Promise<FetchResult>;
|
|
40
|
+
export interface CreateConnectOptions {
|
|
41
|
+
connectUrl: string;
|
|
42
|
+
openPage?: (url: string) => void;
|
|
43
|
+
fetchInterval?: number;
|
|
44
|
+
retry?: number;
|
|
45
|
+
source?: string;
|
|
46
|
+
connectAction?: string;
|
|
47
|
+
appName?: string;
|
|
48
|
+
appLogo?: string;
|
|
49
|
+
wrapSpinner?: BaseWrapSpinner;
|
|
50
|
+
prettyUrl?: (url: string) => string;
|
|
51
|
+
closeOnSuccess?: boolean;
|
|
52
|
+
intervalFetchConfig?: (options: {
|
|
53
|
+
sessionId: string;
|
|
54
|
+
fetchInterval: number;
|
|
55
|
+
fetchTimeout: number;
|
|
56
|
+
}) => Promise<FetchResult>;
|
|
57
|
+
}
|
|
58
|
+
export {};
|
package/lib/dts/index.d.ts
CHANGED
|
@@ -2,7 +2,11 @@ import { type AgentInvokeOptions, type AgentProcessResult, ChatModel, type ChatM
|
|
|
2
2
|
import type { PromiseOrValue } from "@aigne/core/utils/type-utils.js";
|
|
3
3
|
import { type HubChatModelOptions } from "./blocklet-aigne-hub-model.js";
|
|
4
4
|
import { type AIGNEHubChatModelOptions } from "./cli-aigne-hub-model.js";
|
|
5
|
-
export * from "./constants.js";
|
|
5
|
+
export * from "./util/constants.js";
|
|
6
|
+
export * from "./util/credential.js";
|
|
7
|
+
export * from "./util/crypto.js";
|
|
8
|
+
export * from "./util/model.js";
|
|
9
|
+
export * from "./util/type.js";
|
|
6
10
|
export declare class AIGNEHubChatModel extends ChatModel {
|
|
7
11
|
options: HubChatModelOptions;
|
|
8
12
|
private client;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
declare const WELLKNOWN_SERVICE_PATH_PREFIX = "/.well-known/service";
|
|
2
|
+
declare const ACCESS_KEY_PREFIX = "/api/access-key";
|
|
3
|
+
declare const ACCESS_KEY_SESSION_API = "/api/access-key/session";
|
|
4
|
+
declare const AIGNE_HUB_DID = "z8ia3xzq2tMq8CRHfaXj1BTYJyYnEcHbqP8cJ";
|
|
5
|
+
declare const AGENT_HUB_PROVIDER = "aignehub";
|
|
6
|
+
declare const AIGNE_HUB_URL = "https://hub.aigne.io/";
|
|
7
|
+
declare const DEFAULT_AIGNE_HUB_PROVIDER_MODEL = "aignehub:openai/gpt-5-mini";
|
|
8
|
+
declare const DEFAULT_MODEL_PROVIDER = "openai";
|
|
9
|
+
declare const isTest: string | boolean;
|
|
10
|
+
declare const TEST_AIGNE_ENV_FILE: string;
|
|
11
|
+
declare const PROD_AIGNE_ENV_FILE: string;
|
|
12
|
+
declare const AIGNE_ENV_FILE: string;
|
|
13
|
+
export { DEFAULT_AIGNE_HUB_PROVIDER_MODEL, DEFAULT_MODEL_PROVIDER, AIGNE_HUB_DID, AIGNE_HUB_URL, isTest, WELLKNOWN_SERVICE_PATH_PREFIX, ACCESS_KEY_PREFIX, ACCESS_KEY_SESSION_API, AGENT_HUB_PROVIDER, TEST_AIGNE_ENV_FILE, PROD_AIGNE_ENV_FILE, AIGNE_ENV_FILE, };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { CreateConnectOptions, FetchResult, LoadCredentialOptions } from "./type.js";
|
|
2
|
+
export declare const fetchConfigs: ({ connectUrl, sessionId, fetchInterval, fetchTimeout, }: {
|
|
3
|
+
connectUrl: string;
|
|
4
|
+
sessionId: string;
|
|
5
|
+
fetchInterval: number;
|
|
6
|
+
fetchTimeout: number;
|
|
7
|
+
}) => Promise<any>;
|
|
8
|
+
export declare function createConnect({ connectUrl, openPage, fetchInterval, retry, source, connectAction, wrapSpinner, closeOnSuccess, intervalFetchConfig, appName, appLogo, }: CreateConnectOptions): Promise<FetchResult>;
|
|
9
|
+
export declare function getAIGNEHubMountPoint(url: string): Promise<string>;
|
|
10
|
+
export declare function connectToAIGNEHub(url: string): Promise<{
|
|
11
|
+
apiKey: string;
|
|
12
|
+
url: string;
|
|
13
|
+
} | {
|
|
14
|
+
apiKey: undefined;
|
|
15
|
+
url: undefined;
|
|
16
|
+
}>;
|
|
17
|
+
export declare const checkConnectionStatus: (host: string) => Promise<{
|
|
18
|
+
apiKey: any;
|
|
19
|
+
url: string;
|
|
20
|
+
}>;
|
|
21
|
+
export declare function loadCredential(options?: LoadCredentialOptions): Promise<{
|
|
22
|
+
apiKey?: string;
|
|
23
|
+
url?: string;
|
|
24
|
+
} | undefined>;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare const decrypt: (m: string, s: string, i: string) => string;
|
|
2
|
+
export declare const encrypt: (m: string, s: string, i: string) => string;
|
|
3
|
+
export declare const encodeEncryptionKey: (key: string) => string;
|
|
4
|
+
export declare const decodeEncryptionKey: (str: string) => Uint8Array<ArrayBuffer>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ChatModel, ChatModelOptions } from "@aigne/core/agents/chat-model.js";
|
|
2
|
+
import type inquirer from "inquirer";
|
|
3
|
+
import type { LoadableModel, LoadCredentialOptions, Model } from "./type.js";
|
|
4
|
+
export declare function availableModels(): LoadableModel[];
|
|
5
|
+
export declare function findModel(models: LoadableModel[], provider: string): LoadableModel | undefined;
|
|
6
|
+
export declare const parseModelOption: (model?: string) => {
|
|
7
|
+
provider: string | undefined;
|
|
8
|
+
name: string | undefined;
|
|
9
|
+
};
|
|
10
|
+
export declare const formatModelName: (model: string, inquirerPrompt: typeof inquirer.prompt) => Promise<string>;
|
|
11
|
+
export declare function maskApiKey(apiKey?: string): string | undefined;
|
|
12
|
+
export declare function loadModel(model?: Model, modelOptions?: ChatModelOptions, options?: LoadCredentialOptions): Promise<ChatModel | undefined>;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { ChatModel, ChatModelOptions } from "@aigne/core/agents/chat-model.js";
|
|
2
|
+
export type Model = {
|
|
3
|
+
provider?: string | null;
|
|
4
|
+
name?: string | null;
|
|
5
|
+
temperature?: number | null;
|
|
6
|
+
topP?: number | null;
|
|
7
|
+
frequencyPenalty?: number | null;
|
|
8
|
+
presencePenalty?: number | null;
|
|
9
|
+
} | undefined;
|
|
10
|
+
type InquirerPromptFn = (prompt: {
|
|
11
|
+
type: string;
|
|
12
|
+
name: string;
|
|
13
|
+
message: string;
|
|
14
|
+
choices: {
|
|
15
|
+
name: string;
|
|
16
|
+
value: any;
|
|
17
|
+
}[];
|
|
18
|
+
default: any;
|
|
19
|
+
}) => Promise<any>;
|
|
20
|
+
export type LoadCredentialOptions = {
|
|
21
|
+
model?: string;
|
|
22
|
+
aigneHubUrl?: string;
|
|
23
|
+
inquirerPromptFn?: InquirerPromptFn;
|
|
24
|
+
};
|
|
25
|
+
export interface LoadableModel {
|
|
26
|
+
name: string | string[];
|
|
27
|
+
apiKeyEnvName?: string | string[];
|
|
28
|
+
create: (options: {
|
|
29
|
+
model?: string;
|
|
30
|
+
modelOptions?: ChatModelOptions;
|
|
31
|
+
apiKey?: string;
|
|
32
|
+
url?: string;
|
|
33
|
+
}) => ChatModel;
|
|
34
|
+
}
|
|
35
|
+
export type FetchResult = {
|
|
36
|
+
accessKeyId: string;
|
|
37
|
+
accessKeySecret: string;
|
|
38
|
+
};
|
|
39
|
+
export type BaseWrapSpinner = (_: string, waiting: () => Promise<FetchResult>) => Promise<FetchResult>;
|
|
40
|
+
export interface CreateConnectOptions {
|
|
41
|
+
connectUrl: string;
|
|
42
|
+
openPage?: (url: string) => void;
|
|
43
|
+
fetchInterval?: number;
|
|
44
|
+
retry?: number;
|
|
45
|
+
source?: string;
|
|
46
|
+
connectAction?: string;
|
|
47
|
+
appName?: string;
|
|
48
|
+
appLogo?: string;
|
|
49
|
+
wrapSpinner?: BaseWrapSpinner;
|
|
50
|
+
prettyUrl?: (url: string) => string;
|
|
51
|
+
closeOnSuccess?: boolean;
|
|
52
|
+
intervalFetchConfig?: (options: {
|
|
53
|
+
sessionId: string;
|
|
54
|
+
fetchInterval: number;
|
|
55
|
+
fetchTimeout: number;
|
|
56
|
+
}) => Promise<FetchResult>;
|
|
57
|
+
}
|
|
58
|
+
export {};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { ChatModel, } from "@aigne/core";
|
|
2
|
-
import { AIGNE_HUB_URL
|
|
2
|
+
import { AIGNE_HUB_URL } from "./util/constants.js";
|
|
3
|
+
import { availableModels, findModel } from "./util/model.js";
|
|
3
4
|
export class BlockletAIGNEHubChatModel extends ChatModel {
|
|
4
5
|
options;
|
|
5
6
|
client;
|
|
@@ -3,7 +3,7 @@ import { checkArguments } from "@aigne/core/utils/type-utils.js";
|
|
|
3
3
|
import { BaseClient } from "@aigne/transport/http-client/base-client.js";
|
|
4
4
|
import { joinURL } from "ufo";
|
|
5
5
|
import { z } from "zod";
|
|
6
|
-
import { AIGNE_HUB_URL } from "./constants.js";
|
|
6
|
+
import { AIGNE_HUB_URL } from "./util/constants.js";
|
|
7
7
|
const DEFAULT_CHAT_MODEL = "openai/gpt-4o";
|
|
8
8
|
const aigneHubChatModelOptionsSchema = z.object({
|
|
9
9
|
url: z.string().optional(),
|
package/lib/esm/index.d.ts
CHANGED
|
@@ -2,7 +2,11 @@ import { type AgentInvokeOptions, type AgentProcessResult, ChatModel, type ChatM
|
|
|
2
2
|
import type { PromiseOrValue } from "@aigne/core/utils/type-utils.js";
|
|
3
3
|
import { type HubChatModelOptions } from "./blocklet-aigne-hub-model.js";
|
|
4
4
|
import { type AIGNEHubChatModelOptions } from "./cli-aigne-hub-model.js";
|
|
5
|
-
export * from "./constants.js";
|
|
5
|
+
export * from "./util/constants.js";
|
|
6
|
+
export * from "./util/credential.js";
|
|
7
|
+
export * from "./util/crypto.js";
|
|
8
|
+
export * from "./util/model.js";
|
|
9
|
+
export * from "./util/type.js";
|
|
6
10
|
export declare class AIGNEHubChatModel extends ChatModel {
|
|
7
11
|
options: HubChatModelOptions;
|
|
8
12
|
private client;
|
package/lib/esm/index.js
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
import { ChatModel, } from "@aigne/core";
|
|
2
2
|
import { BlockletAIGNEHubChatModel } from "./blocklet-aigne-hub-model.js";
|
|
3
3
|
import { CliAIGNEHubChatModel } from "./cli-aigne-hub-model.js";
|
|
4
|
-
import { AIGNE_HUB_URL
|
|
5
|
-
|
|
4
|
+
import { AIGNE_HUB_URL } from "./util/constants.js";
|
|
5
|
+
import { getAIGNEHubMountPoint } from "./util/credential.js";
|
|
6
|
+
export * from "./util/constants.js";
|
|
7
|
+
export * from "./util/credential.js";
|
|
8
|
+
export * from "./util/crypto.js";
|
|
9
|
+
export * from "./util/model.js";
|
|
10
|
+
export * from "./util/type.js";
|
|
6
11
|
export class AIGNEHubChatModel extends ChatModel {
|
|
7
12
|
options;
|
|
8
13
|
client;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
declare const WELLKNOWN_SERVICE_PATH_PREFIX = "/.well-known/service";
|
|
2
|
+
declare const ACCESS_KEY_PREFIX = "/api/access-key";
|
|
3
|
+
declare const ACCESS_KEY_SESSION_API = "/api/access-key/session";
|
|
4
|
+
declare const AIGNE_HUB_DID = "z8ia3xzq2tMq8CRHfaXj1BTYJyYnEcHbqP8cJ";
|
|
5
|
+
declare const AGENT_HUB_PROVIDER = "aignehub";
|
|
6
|
+
declare const AIGNE_HUB_URL = "https://hub.aigne.io/";
|
|
7
|
+
declare const DEFAULT_AIGNE_HUB_PROVIDER_MODEL = "aignehub:openai/gpt-5-mini";
|
|
8
|
+
declare const DEFAULT_MODEL_PROVIDER = "openai";
|
|
9
|
+
declare const isTest: string | boolean;
|
|
10
|
+
declare const TEST_AIGNE_ENV_FILE: string;
|
|
11
|
+
declare const PROD_AIGNE_ENV_FILE: string;
|
|
12
|
+
declare const AIGNE_ENV_FILE: string;
|
|
13
|
+
export { DEFAULT_AIGNE_HUB_PROVIDER_MODEL, DEFAULT_MODEL_PROVIDER, AIGNE_HUB_DID, AIGNE_HUB_URL, isTest, WELLKNOWN_SERVICE_PATH_PREFIX, ACCESS_KEY_PREFIX, ACCESS_KEY_SESSION_API, AGENT_HUB_PROVIDER, TEST_AIGNE_ENV_FILE, PROD_AIGNE_ENV_FILE, AIGNE_ENV_FILE, };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { nodejs } from "@aigne/platform-helpers/nodejs/index.js";
|
|
2
|
+
const WELLKNOWN_SERVICE_PATH_PREFIX = "/.well-known/service";
|
|
3
|
+
const ACCESS_KEY_PREFIX = "/api/access-key";
|
|
4
|
+
const ACCESS_KEY_SESSION_API = `${ACCESS_KEY_PREFIX}/session`;
|
|
5
|
+
const AIGNE_HUB_DID = "z8ia3xzq2tMq8CRHfaXj1BTYJyYnEcHbqP8cJ";
|
|
6
|
+
const AGENT_HUB_PROVIDER = "aignehub";
|
|
7
|
+
const AIGNE_HUB_URL = "https://hub.aigne.io/";
|
|
8
|
+
const DEFAULT_AIGNE_HUB_MODEL = "openai/gpt-5-mini";
|
|
9
|
+
const DEFAULT_AIGNE_HUB_PROVIDER_MODEL = `${AGENT_HUB_PROVIDER}:${DEFAULT_AIGNE_HUB_MODEL}`;
|
|
10
|
+
const DEFAULT_MODEL_PROVIDER = "openai";
|
|
11
|
+
const isTest = process.env.CI || process.env.NODE_ENV === "test";
|
|
12
|
+
const TEST_AIGNE_ENV_FILE = nodejs.path.join(nodejs.os.homedir(), ".aigne", "test-aigne-hub-connected.yaml");
|
|
13
|
+
const PROD_AIGNE_ENV_FILE = nodejs.path.join(nodejs.os.homedir(), ".aigne", "aigne-hub-connected.yaml");
|
|
14
|
+
const AIGNE_ENV_FILE = isTest ? TEST_AIGNE_ENV_FILE : PROD_AIGNE_ENV_FILE;
|
|
15
|
+
export { DEFAULT_AIGNE_HUB_PROVIDER_MODEL, DEFAULT_MODEL_PROVIDER, AIGNE_HUB_DID, AIGNE_HUB_URL, isTest, WELLKNOWN_SERVICE_PATH_PREFIX, ACCESS_KEY_PREFIX, ACCESS_KEY_SESSION_API, AGENT_HUB_PROVIDER, TEST_AIGNE_ENV_FILE, PROD_AIGNE_ENV_FILE, AIGNE_ENV_FILE, };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { CreateConnectOptions, FetchResult, LoadCredentialOptions } from "./type.js";
|
|
2
|
+
export declare const fetchConfigs: ({ connectUrl, sessionId, fetchInterval, fetchTimeout, }: {
|
|
3
|
+
connectUrl: string;
|
|
4
|
+
sessionId: string;
|
|
5
|
+
fetchInterval: number;
|
|
6
|
+
fetchTimeout: number;
|
|
7
|
+
}) => Promise<any>;
|
|
8
|
+
export declare function createConnect({ connectUrl, openPage, fetchInterval, retry, source, connectAction, wrapSpinner, closeOnSuccess, intervalFetchConfig, appName, appLogo, }: CreateConnectOptions): Promise<FetchResult>;
|
|
9
|
+
export declare function getAIGNEHubMountPoint(url: string): Promise<string>;
|
|
10
|
+
export declare function connectToAIGNEHub(url: string): Promise<{
|
|
11
|
+
apiKey: string;
|
|
12
|
+
url: string;
|
|
13
|
+
} | {
|
|
14
|
+
apiKey: undefined;
|
|
15
|
+
url: undefined;
|
|
16
|
+
}>;
|
|
17
|
+
export declare const checkConnectionStatus: (host: string) => Promise<{
|
|
18
|
+
apiKey: any;
|
|
19
|
+
url: string;
|
|
20
|
+
}>;
|
|
21
|
+
export declare function loadCredential(options?: LoadCredentialOptions): Promise<{
|
|
22
|
+
apiKey?: string;
|
|
23
|
+
url?: string;
|
|
24
|
+
} | undefined>;
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import { logger } from "@aigne/core/utils/logger.js";
|
|
2
|
+
import { nodejs } from "@aigne/platform-helpers/nodejs/index.js";
|
|
3
|
+
import inquirer from "inquirer";
|
|
4
|
+
import open from "open";
|
|
5
|
+
import pWaitFor from "p-wait-for";
|
|
6
|
+
import { joinURL, withQuery } from "ufo";
|
|
7
|
+
import { parse, stringify } from "yaml";
|
|
8
|
+
import { ACCESS_KEY_SESSION_API, AGENT_HUB_PROVIDER, AIGNE_ENV_FILE, AIGNE_HUB_DID, AIGNE_HUB_URL as DEFAULT_AIGNE_HUB_URL, isTest, WELLKNOWN_SERVICE_PATH_PREFIX, } from "./constants.js";
|
|
9
|
+
import { decrypt, encodeEncryptionKey } from "./crypto.js";
|
|
10
|
+
const request = async (config) => {
|
|
11
|
+
const headers = {};
|
|
12
|
+
if (config.requestCount !== undefined) {
|
|
13
|
+
headers["X-Request-Count"] = config.requestCount.toString();
|
|
14
|
+
}
|
|
15
|
+
const response = await fetch(config.url, { method: config.method || "GET", headers });
|
|
16
|
+
if (!response.ok)
|
|
17
|
+
throw new Error(`HTTP error! status: ${response.status}`);
|
|
18
|
+
const data = await response.json();
|
|
19
|
+
return { data };
|
|
20
|
+
};
|
|
21
|
+
export const fetchConfigs = async ({ connectUrl, sessionId, fetchInterval, fetchTimeout, }) => {
|
|
22
|
+
const sessionURL = withQuery(joinURL(connectUrl, ACCESS_KEY_SESSION_API), { sid: sessionId });
|
|
23
|
+
let requestCount = 0;
|
|
24
|
+
const condition = async () => {
|
|
25
|
+
const { data: session } = await request({ url: sessionURL, requestCount });
|
|
26
|
+
requestCount++;
|
|
27
|
+
return Boolean(session.accessKeyId && session.accessKeySecret);
|
|
28
|
+
};
|
|
29
|
+
await pWaitFor(condition, { interval: fetchInterval, timeout: fetchTimeout });
|
|
30
|
+
const { data: session } = await request({ url: sessionURL, requestCount });
|
|
31
|
+
await request({ url: sessionURL, method: "DELETE" });
|
|
32
|
+
return {
|
|
33
|
+
...session,
|
|
34
|
+
accessKeyId: session.accessKeyId,
|
|
35
|
+
accessKeySecret: decrypt(session.accessKeySecret, session.accessKeyId, session.challenge),
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
function baseWrapSpinner(_, waiting) {
|
|
39
|
+
return Promise.resolve(waiting());
|
|
40
|
+
}
|
|
41
|
+
export async function createConnect({ connectUrl, openPage, fetchInterval = 3 * 1000, retry = 1500, source = "Blocklet CLI", connectAction = "connect-cli", wrapSpinner = baseWrapSpinner, closeOnSuccess, intervalFetchConfig, appName = "AIGNE CLI", appLogo = "https://www.aigne.io/favicon.ico?imageFilter=resize&w=32", }) {
|
|
42
|
+
const startSessionURL = joinURL(connectUrl, ACCESS_KEY_SESSION_API);
|
|
43
|
+
const { data: session } = await request({ url: startSessionURL, method: "POST" });
|
|
44
|
+
const token = session.id;
|
|
45
|
+
const pageUrl = withQuery(joinURL(connectUrl, connectAction), {
|
|
46
|
+
__token__: encodeEncryptionKey(token),
|
|
47
|
+
source,
|
|
48
|
+
closeOnSuccess,
|
|
49
|
+
cli: true,
|
|
50
|
+
appName: ` ${appName}`,
|
|
51
|
+
appLogo,
|
|
52
|
+
});
|
|
53
|
+
openPage?.(pageUrl);
|
|
54
|
+
return await wrapSpinner(`Waiting for connection: ${connectUrl}`, async () => {
|
|
55
|
+
const checkAuthorizeStatus = intervalFetchConfig ?? fetchConfigs;
|
|
56
|
+
const authorizeStatus = await checkAuthorizeStatus({
|
|
57
|
+
connectUrl,
|
|
58
|
+
sessionId: token,
|
|
59
|
+
fetchTimeout: retry * fetchInterval,
|
|
60
|
+
fetchInterval: retry,
|
|
61
|
+
});
|
|
62
|
+
return authorizeStatus;
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
export async function getAIGNEHubMountPoint(url) {
|
|
66
|
+
const { origin } = new URL(url);
|
|
67
|
+
const BLOCKLET_JSON_PATH = "__blocklet__.js?type=json";
|
|
68
|
+
const blockletInfo = await fetch(joinURL(origin, BLOCKLET_JSON_PATH));
|
|
69
|
+
const blocklet = await blockletInfo.json();
|
|
70
|
+
const aigneHubMount = (blocklet?.componentMountPoints || []).find((m) => m.did === AIGNE_HUB_DID);
|
|
71
|
+
return joinURL(origin, aigneHubMount?.mountPoint || "");
|
|
72
|
+
}
|
|
73
|
+
export async function connectToAIGNEHub(url) {
|
|
74
|
+
const { origin, host } = new URL(url);
|
|
75
|
+
const connectUrl = joinURL(origin, WELLKNOWN_SERVICE_PATH_PREFIX);
|
|
76
|
+
const urlWithAIGNEHubMountPoint = await getAIGNEHubMountPoint(url);
|
|
77
|
+
try {
|
|
78
|
+
const openFn = isTest ? () => { } : open;
|
|
79
|
+
const result = await createConnect({
|
|
80
|
+
connectUrl: connectUrl,
|
|
81
|
+
connectAction: "gen-simple-access-key",
|
|
82
|
+
source: `@aigne/cli connect to AIGNE hub`,
|
|
83
|
+
closeOnSuccess: true,
|
|
84
|
+
openPage: (pageUrl) => openFn(pageUrl),
|
|
85
|
+
});
|
|
86
|
+
const accessKeyOptions = {
|
|
87
|
+
apiKey: result.accessKeySecret,
|
|
88
|
+
url: urlWithAIGNEHubMountPoint,
|
|
89
|
+
};
|
|
90
|
+
// After redirection, write the AIGNE Hub access token
|
|
91
|
+
const aigneDir = nodejs.path.join(nodejs.os.homedir(), ".aigne");
|
|
92
|
+
if (!nodejs.fsSync.existsSync(aigneDir)) {
|
|
93
|
+
nodejs.fsSync.mkdirSync(aigneDir, { recursive: true });
|
|
94
|
+
}
|
|
95
|
+
const envs = parse(await nodejs.fs.readFile(AIGNE_ENV_FILE, "utf8").catch(() => stringify({})));
|
|
96
|
+
await nodejs.fs.writeFile(AIGNE_ENV_FILE, stringify({
|
|
97
|
+
...envs,
|
|
98
|
+
[host]: {
|
|
99
|
+
AIGNE_HUB_API_KEY: accessKeyOptions.apiKey,
|
|
100
|
+
AIGNE_HUB_API_URL: accessKeyOptions.url,
|
|
101
|
+
},
|
|
102
|
+
default: {
|
|
103
|
+
AIGNE_HUB_API_URL: accessKeyOptions.url,
|
|
104
|
+
},
|
|
105
|
+
}));
|
|
106
|
+
return accessKeyOptions;
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
logger.error("Failed to connect to AIGNE Hub", error.message);
|
|
110
|
+
return { apiKey: undefined, url: undefined };
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
export const checkConnectionStatus = async (host) => {
|
|
114
|
+
// aigne-hub access token
|
|
115
|
+
if (!nodejs.fsSync.existsSync(AIGNE_ENV_FILE)) {
|
|
116
|
+
throw new Error("AIGNE_HUB_API_KEY file not found, need to login first");
|
|
117
|
+
}
|
|
118
|
+
const data = await nodejs.fs.readFile(AIGNE_ENV_FILE, "utf8");
|
|
119
|
+
if (!data.includes("AIGNE_HUB_API_KEY")) {
|
|
120
|
+
throw new Error("AIGNE_HUB_API_KEY key not found, need to login first");
|
|
121
|
+
}
|
|
122
|
+
const envs = parse(data);
|
|
123
|
+
if (!envs[host]) {
|
|
124
|
+
throw new Error("AIGNE_HUB_API_KEY host not found, need to login first");
|
|
125
|
+
}
|
|
126
|
+
const env = envs[host];
|
|
127
|
+
if (!env.AIGNE_HUB_API_KEY) {
|
|
128
|
+
throw new Error("AIGNE_HUB_API_KEY key not found, need to login first");
|
|
129
|
+
}
|
|
130
|
+
return {
|
|
131
|
+
apiKey: env.AIGNE_HUB_API_KEY,
|
|
132
|
+
url: joinURL(env.AIGNE_HUB_API_URL),
|
|
133
|
+
};
|
|
134
|
+
};
|
|
135
|
+
export async function loadCredential(options) {
|
|
136
|
+
const isBlocklet = process.env.BLOCKLET_AIGNE_API_URL && process.env.BLOCKLET_AIGNE_API_PROVIDER;
|
|
137
|
+
if (isBlocklet)
|
|
138
|
+
return undefined;
|
|
139
|
+
const aigneDir = nodejs.path.join(nodejs.os.homedir(), ".aigne");
|
|
140
|
+
if (!nodejs.fsSync.existsSync(aigneDir)) {
|
|
141
|
+
nodejs.fsSync.mkdirSync(aigneDir, { recursive: true });
|
|
142
|
+
}
|
|
143
|
+
const envs = parse(await nodejs.fs.readFile(AIGNE_ENV_FILE, "utf8").catch(() => stringify({})));
|
|
144
|
+
const inquirerPrompt = (options?.inquirerPromptFn ?? inquirer.prompt);
|
|
145
|
+
const configUrl = options?.aigneHubUrl || process.env.AIGNE_HUB_API_URL;
|
|
146
|
+
const AIGNE_HUB_URL = configUrl || envs?.default?.AIGNE_HUB_API_URL || DEFAULT_AIGNE_HUB_URL;
|
|
147
|
+
const connectUrl = joinURL(new URL(AIGNE_HUB_URL).origin, WELLKNOWN_SERVICE_PATH_PREFIX);
|
|
148
|
+
const { host } = new URL(AIGNE_HUB_URL);
|
|
149
|
+
const modelName = options?.model || "";
|
|
150
|
+
const isAIGNEHubModel = (modelName.toLocaleLowerCase() || "").includes(AGENT_HUB_PROVIDER);
|
|
151
|
+
let credential = {};
|
|
152
|
+
if (isAIGNEHubModel) {
|
|
153
|
+
try {
|
|
154
|
+
credential = await checkConnectionStatus(host);
|
|
155
|
+
}
|
|
156
|
+
catch (error) {
|
|
157
|
+
if (error instanceof Error && error.message.includes("login first")) {
|
|
158
|
+
let aigneHubUrl = connectUrl;
|
|
159
|
+
if (!configUrl) {
|
|
160
|
+
const { subscribe } = await inquirerPrompt({
|
|
161
|
+
type: "list",
|
|
162
|
+
name: "subscribe",
|
|
163
|
+
message: "No LLM API Keys or AIGNE Hub connections found. How would you like to proceed?",
|
|
164
|
+
choices: [
|
|
165
|
+
{
|
|
166
|
+
name: "Connect to the Arcblock official AIGNE Hub (recommended, free credits for new users)",
|
|
167
|
+
value: "official",
|
|
168
|
+
},
|
|
169
|
+
connectUrl.includes(DEFAULT_AIGNE_HUB_URL)
|
|
170
|
+
? {
|
|
171
|
+
name: "Connect to your own AIGNE Hub instance (self-hosted)",
|
|
172
|
+
value: "custom",
|
|
173
|
+
}
|
|
174
|
+
: null,
|
|
175
|
+
{
|
|
176
|
+
name: "Exit and configure my own LLM API Keys",
|
|
177
|
+
value: "manual",
|
|
178
|
+
},
|
|
179
|
+
].filter(Boolean),
|
|
180
|
+
default: "official",
|
|
181
|
+
});
|
|
182
|
+
if (subscribe === "custom") {
|
|
183
|
+
const { customUrl } = await inquirerPrompt({
|
|
184
|
+
type: "input",
|
|
185
|
+
name: "customUrl",
|
|
186
|
+
message: "Enter the URL of your AIGNE Hub:",
|
|
187
|
+
validate(input) {
|
|
188
|
+
try {
|
|
189
|
+
const url = new URL(input);
|
|
190
|
+
return url.protocol.startsWith("http")
|
|
191
|
+
? true
|
|
192
|
+
: "Must be a valid URL with http or https";
|
|
193
|
+
}
|
|
194
|
+
catch {
|
|
195
|
+
return "Invalid URL";
|
|
196
|
+
}
|
|
197
|
+
},
|
|
198
|
+
});
|
|
199
|
+
aigneHubUrl = customUrl;
|
|
200
|
+
}
|
|
201
|
+
else if (subscribe === "manual") {
|
|
202
|
+
console.log("You chose to configure your own LLM API Keys. Exiting...");
|
|
203
|
+
process.exit(0);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
credential = await connectToAIGNEHub(aigneHubUrl);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return credential;
|
|
211
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare const decrypt: (m: string, s: string, i: string) => string;
|
|
2
|
+
export declare const encrypt: (m: string, s: string, i: string) => string;
|
|
3
|
+
export declare const encodeEncryptionKey: (key: string) => string;
|
|
4
|
+
export declare const decodeEncryptionKey: (str: string) => Uint8Array<ArrayBuffer>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import crypto from "node:crypto";
|
|
2
|
+
import { AesCrypter } from "@ocap/mcrypto/lib/crypter/aes-legacy.js";
|
|
3
|
+
const aes = new AesCrypter();
|
|
4
|
+
export const decrypt = (m, s, i) => aes.decrypt(m, crypto.pbkdf2Sync(i, s, 256, 32, "sha512").toString("hex"));
|
|
5
|
+
export const encrypt = (m, s, i) => aes.encrypt(m, crypto.pbkdf2Sync(i, s, 256, 32, "sha512").toString("hex"));
|
|
6
|
+
const escapeFn = (str) => str.replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
|
|
7
|
+
const unescapeFn = (str) => (str + "===".slice((str.length + 3) % 4)).replace(/-/g, "+").replace(/_/g, "/");
|
|
8
|
+
export const encodeEncryptionKey = (key) => escapeFn(Buffer.from(key).toString("base64"));
|
|
9
|
+
export const decodeEncryptionKey = (str) => new Uint8Array(Buffer.from(unescapeFn(str), "base64"));
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ChatModel, ChatModelOptions } from "@aigne/core/agents/chat-model.js";
|
|
2
|
+
import type inquirer from "inquirer";
|
|
3
|
+
import type { LoadableModel, LoadCredentialOptions, Model } from "./type.js";
|
|
4
|
+
export declare function availableModels(): LoadableModel[];
|
|
5
|
+
export declare function findModel(models: LoadableModel[], provider: string): LoadableModel | undefined;
|
|
6
|
+
export declare const parseModelOption: (model?: string) => {
|
|
7
|
+
provider: string | undefined;
|
|
8
|
+
name: string | undefined;
|
|
9
|
+
};
|
|
10
|
+
export declare const formatModelName: (model: string, inquirerPrompt: typeof inquirer.prompt) => Promise<string>;
|
|
11
|
+
export declare function maskApiKey(apiKey?: string): string | undefined;
|
|
12
|
+
export declare function loadModel(model?: Model, modelOptions?: ChatModelOptions, options?: LoadCredentialOptions): Promise<ChatModel | undefined>;
|