@aigne/core 0.4.209 → 0.4.210
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/lib/cjs/definitions/data-type-schema.js +3 -3
- package/lib/cjs/definitions/open-api.js +2 -0
- package/lib/cjs/index.js +3 -0
- package/lib/cjs/open-api-agent.js +62 -0
- package/lib/cjs/tsconfig.tsbuildinfo +1 -1
- package/lib/cjs/utils/constants.js +4 -0
- package/lib/cjs/utils/fetch-open-api.js +30 -0
- package/lib/cjs/utils/fetch.js +20 -0
- package/lib/cjs/utils/index.js +3 -0
- package/lib/cjs/utils/open-api-parameter.js +84 -0
- package/lib/esm/definitions/data-type-schema.js +1 -1
- package/lib/esm/definitions/open-api.js +1 -0
- package/lib/esm/index.js +3 -0
- package/lib/esm/open-api-agent.js +59 -0
- package/lib/esm/tsconfig.tsbuildinfo +1 -1
- package/lib/esm/utils/constants.js +1 -0
- package/lib/esm/utils/fetch-open-api.js +26 -0
- package/lib/esm/utils/fetch.js +17 -0
- package/lib/esm/utils/index.js +3 -0
- package/lib/esm/utils/open-api-parameter.js +78 -0
- package/lib/types/context.d.ts +1 -0
- package/lib/types/definitions/data-type-schema.d.ts +1 -1
- package/lib/types/definitions/open-api.d.ts +36 -0
- package/lib/types/index.d.ts +3 -0
- package/lib/types/open-api-agent.d.ts +55 -0
- package/lib/types/tsconfig.tsbuildinfo +1 -1
- package/lib/types/utils/constants.d.ts +1 -0
- package/lib/types/utils/fetch-open-api.d.ts +2 -0
- package/lib/types/utils/fetch.d.ts +1 -0
- package/lib/types/utils/index.d.ts +3 -0
- package/lib/types/utils/open-api-parameter.d.ts +7 -0
- package/package.json +2 -20
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const FETCH_TIMEOUT = Number(process.env.FETCH_TIMEOUT) || 30000;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { withQuery } from 'ufo';
|
|
2
|
+
import { FETCH_TIMEOUT } from './constants';
|
|
3
|
+
import { checkFetchResponse } from './fetch';
|
|
4
|
+
export const fetchOpenApi = async (request) => {
|
|
5
|
+
const cookie = request.cookies
|
|
6
|
+
? Object.entries(request.cookies)
|
|
7
|
+
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
|
|
8
|
+
.join('; ')
|
|
9
|
+
.trim()
|
|
10
|
+
: undefined;
|
|
11
|
+
const controller = new AbortController();
|
|
12
|
+
const abortTimer = setTimeout(() => controller.abort(), FETCH_TIMEOUT);
|
|
13
|
+
const response = await fetch(withQuery(request.url, request.query ?? {}), {
|
|
14
|
+
method: request.method,
|
|
15
|
+
headers: {
|
|
16
|
+
'Content-Type': 'application/json',
|
|
17
|
+
...request.headers,
|
|
18
|
+
...(cookie && { cookie }),
|
|
19
|
+
},
|
|
20
|
+
body: request.method.toLowerCase() !== 'get' && request.body ? JSON.stringify(request.body) : undefined,
|
|
21
|
+
credentials: request.cookies ? 'include' : 'same-origin',
|
|
22
|
+
signal: controller.signal,
|
|
23
|
+
}).finally(() => clearTimeout(abortTimer));
|
|
24
|
+
await checkFetchResponse(response);
|
|
25
|
+
return response.json();
|
|
26
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export async function checkFetchResponse(result) {
|
|
2
|
+
if (!result.ok) {
|
|
3
|
+
let message;
|
|
4
|
+
try {
|
|
5
|
+
const json = await result.json();
|
|
6
|
+
const msg = json.error?.message || json.message;
|
|
7
|
+
if (msg && typeof msg === 'string') {
|
|
8
|
+
message = msg;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
catch {
|
|
12
|
+
// ignore
|
|
13
|
+
}
|
|
14
|
+
throw new Error(message || `Failed to fetch url ${result.url} with status ${result.status}`);
|
|
15
|
+
}
|
|
16
|
+
return result;
|
|
17
|
+
}
|
package/lib/esm/utils/index.js
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
|
+
export * from './fetch';
|
|
2
|
+
export * from './fetch-open-api';
|
|
1
3
|
export * from './is-non-nullable';
|
|
2
4
|
export * from './mustache-utils';
|
|
3
5
|
export * from './nullable';
|
|
4
6
|
export * from './omit';
|
|
7
|
+
export * from './open-api-parameter';
|
|
5
8
|
export * from './ordered-map';
|
|
6
9
|
export * from './stream-utils';
|
|
7
10
|
export * from './union';
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { isEmpty, omitBy } from 'lodash';
|
|
2
|
+
import logger from '../logger';
|
|
3
|
+
import { OrderedRecord } from './ordered-map';
|
|
4
|
+
export async function formatOpenAPIRequest(api, inputs, input) {
|
|
5
|
+
const { url, method, ...inputParams } = processParameters(api, inputs, input);
|
|
6
|
+
logger.debug('inputParams', inputParams);
|
|
7
|
+
const authParams = await getAuthParams(api.auth);
|
|
8
|
+
logger.debug('authParams', authParams);
|
|
9
|
+
return {
|
|
10
|
+
url,
|
|
11
|
+
method,
|
|
12
|
+
...omitBy({
|
|
13
|
+
...inputParams,
|
|
14
|
+
query: { ...inputParams.query, ...authParams.query },
|
|
15
|
+
cookies: { ...inputParams.cookies, ...authParams.cookies },
|
|
16
|
+
headers: { ...inputParams.headers, ...authParams.headers },
|
|
17
|
+
}, (i) => isEmpty(i)),
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
async function getAuthParams(auth) {
|
|
21
|
+
if (!auth)
|
|
22
|
+
return {};
|
|
23
|
+
if (auth.type === 'custom') {
|
|
24
|
+
return await auth.getValue();
|
|
25
|
+
}
|
|
26
|
+
const { type, key, token } = auth;
|
|
27
|
+
switch (auth.in) {
|
|
28
|
+
case 'query':
|
|
29
|
+
return { query: { [key || 'token']: token } };
|
|
30
|
+
case 'cookie':
|
|
31
|
+
return { cookies: { [key || 'token']: token } };
|
|
32
|
+
default: {
|
|
33
|
+
const prefix = type === 'bearer' ? 'Bearer ' : type === 'basic' ? 'Basic ' : '';
|
|
34
|
+
return { headers: { [key || 'Authorization']: `${prefix}${token}` } };
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
function processParameters(api, inputs, input) {
|
|
39
|
+
const result = {
|
|
40
|
+
url: api.url,
|
|
41
|
+
method: api.method,
|
|
42
|
+
headers: {},
|
|
43
|
+
query: {},
|
|
44
|
+
cookies: {},
|
|
45
|
+
body: {},
|
|
46
|
+
};
|
|
47
|
+
Object.entries(input).forEach(([key, value]) => {
|
|
48
|
+
const schema = OrderedRecord.find(inputs, (x) => x.name === key);
|
|
49
|
+
if (!schema)
|
|
50
|
+
return;
|
|
51
|
+
switch (schema.in) {
|
|
52
|
+
case 'query':
|
|
53
|
+
result.query[key] = value;
|
|
54
|
+
break;
|
|
55
|
+
case 'header':
|
|
56
|
+
result.headers[key] = value;
|
|
57
|
+
break;
|
|
58
|
+
case 'cookie':
|
|
59
|
+
result.cookies[key] = value;
|
|
60
|
+
break;
|
|
61
|
+
case 'body':
|
|
62
|
+
result.body[key] = value;
|
|
63
|
+
break;
|
|
64
|
+
case 'path':
|
|
65
|
+
result.url = result.url.replace(`{${key}}`, String(value));
|
|
66
|
+
break;
|
|
67
|
+
default:
|
|
68
|
+
// 没有指定 in 的情况
|
|
69
|
+
if (result.method.toLowerCase() === 'get') {
|
|
70
|
+
result.query[key] = value;
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
result.body[key] = value;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
return result;
|
|
78
|
+
}
|
package/lib/types/context.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { OrderedRecord } from '../utils';
|
|
2
1
|
import { MakeNullablePropertyOptional } from '../utils/nullable';
|
|
2
|
+
import { OrderedRecord } from '../utils/ordered-map';
|
|
3
3
|
import { DataType } from './data-type';
|
|
4
4
|
export declare function schemaToDataType(dataType: {
|
|
5
5
|
[name: string]: DataTypeSchema;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { DataType } from './data-type';
|
|
2
|
+
import type { DataTypeSchema } from './data-type-schema';
|
|
3
|
+
export interface BaseAuthConfig {
|
|
4
|
+
type: 'bearer' | 'basic';
|
|
5
|
+
token: string;
|
|
6
|
+
in?: 'header' | 'query' | 'cookie';
|
|
7
|
+
/**
|
|
8
|
+
* The key to use for the token. Default `Authorization` in header and `token` in query and cookie.
|
|
9
|
+
*/
|
|
10
|
+
key?: string;
|
|
11
|
+
}
|
|
12
|
+
export interface CustomAuthConfig {
|
|
13
|
+
type: 'custom';
|
|
14
|
+
getValue: () => Promise<AuthResult> | AuthResult;
|
|
15
|
+
}
|
|
16
|
+
export type AuthConfig = BaseAuthConfig | CustomAuthConfig;
|
|
17
|
+
export type AuthType = AuthConfig['type'];
|
|
18
|
+
export type AuthResult = Pick<FetchRequest, 'headers' | 'query' | 'cookies'>;
|
|
19
|
+
type HTTPMethodLowercase = 'get' | 'post' | 'put' | 'delete' | 'patch' | 'head' | 'options';
|
|
20
|
+
export type HTTPMethod = Uppercase<HTTPMethodLowercase> | Lowercase<HTTPMethodLowercase>;
|
|
21
|
+
export type ParameterLocation = 'path' | 'query' | 'body' | 'header' | 'cookie';
|
|
22
|
+
export type OpenAPIDataType = DataType & {
|
|
23
|
+
in?: ParameterLocation;
|
|
24
|
+
};
|
|
25
|
+
export type OpenAPIDataTypeSchema = DataTypeSchema & {
|
|
26
|
+
in?: ParameterLocation;
|
|
27
|
+
};
|
|
28
|
+
export type FetchRequest = {
|
|
29
|
+
url: string;
|
|
30
|
+
method: HTTPMethod;
|
|
31
|
+
query?: Record<string, string | number | boolean>;
|
|
32
|
+
headers?: Record<string, string>;
|
|
33
|
+
cookies?: Record<string, string>;
|
|
34
|
+
body?: Record<string, any>;
|
|
35
|
+
};
|
|
36
|
+
export {};
|
package/lib/types/index.d.ts
CHANGED
|
@@ -2,6 +2,8 @@ export * from './utils';
|
|
|
2
2
|
export * from './constants';
|
|
3
3
|
export * from './definitions/data-type';
|
|
4
4
|
export * from './definitions/data-type-schema';
|
|
5
|
+
export * from './definitions/open-api';
|
|
6
|
+
export * from './definitions/memory';
|
|
5
7
|
export * from './context';
|
|
6
8
|
export * from './runnable';
|
|
7
9
|
export * from './agent';
|
|
@@ -12,4 +14,5 @@ export * from './function-agent';
|
|
|
12
14
|
export * from './function-runner';
|
|
13
15
|
export * from './llm-decision-agent';
|
|
14
16
|
export * from './local-function-agent';
|
|
17
|
+
export * from './open-api-agent';
|
|
15
18
|
export * from './memorable';
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { Agent } from './agent';
|
|
2
|
+
import type { Context, ContextState } from './context';
|
|
3
|
+
import { DataTypeSchema, SchemaMapType } from './definitions/data-type-schema';
|
|
4
|
+
import { CreateRunnableMemory } from './definitions/memory';
|
|
5
|
+
import { AuthConfig, FetchRequest, HTTPMethod, OpenAPIDataType, OpenAPIDataTypeSchema } from './definitions/open-api';
|
|
6
|
+
import { MemorableSearchOutput, MemoryItemWithScore } from './memorable';
|
|
7
|
+
import { RunnableDefinition } from './runnable';
|
|
8
|
+
import { OrderedRecord } from './utils';
|
|
9
|
+
export declare class OpenAPIAgent<I extends {
|
|
10
|
+
[name: string]: any;
|
|
11
|
+
} = {}, O extends {
|
|
12
|
+
[name: string]: any;
|
|
13
|
+
} = {}, Memories extends {
|
|
14
|
+
[name: string]: MemoryItemWithScore[];
|
|
15
|
+
} = {}, State extends ContextState = ContextState> extends Agent<I, O, Memories, State> {
|
|
16
|
+
definition: OpenAPIAgentDefinition;
|
|
17
|
+
static create: typeof create;
|
|
18
|
+
constructor(definition: OpenAPIAgentDefinition, context?: Context<State>);
|
|
19
|
+
process(input: I): Promise<any>;
|
|
20
|
+
fetch(request: FetchRequest): Promise<any>;
|
|
21
|
+
}
|
|
22
|
+
export interface OpenAPIAgentDefinition extends RunnableDefinition {
|
|
23
|
+
type: 'open_api_agent';
|
|
24
|
+
inputs: OrderedRecord<OpenAPIDataType>;
|
|
25
|
+
url: string;
|
|
26
|
+
method?: HTTPMethod;
|
|
27
|
+
auth?: AuthConfig;
|
|
28
|
+
}
|
|
29
|
+
export interface CreateOpenAPIAgentOptions<I extends {
|
|
30
|
+
[name: string]: OpenAPIDataTypeSchema;
|
|
31
|
+
}, O extends {
|
|
32
|
+
[name: string]: DataTypeSchema;
|
|
33
|
+
}, Memories extends {
|
|
34
|
+
[name: string]: CreateRunnableMemory<I>;
|
|
35
|
+
}, State extends ContextState> {
|
|
36
|
+
context?: Context<State>;
|
|
37
|
+
id?: string;
|
|
38
|
+
name?: string;
|
|
39
|
+
inputs: I;
|
|
40
|
+
outputs: O;
|
|
41
|
+
memories?: Memories;
|
|
42
|
+
url: string;
|
|
43
|
+
method?: HTTPMethod;
|
|
44
|
+
auth?: AuthConfig;
|
|
45
|
+
}
|
|
46
|
+
declare function create<I extends {
|
|
47
|
+
[name: string]: OpenAPIDataTypeSchema;
|
|
48
|
+
}, O extends {
|
|
49
|
+
[name: string]: DataTypeSchema;
|
|
50
|
+
}, Memories extends {
|
|
51
|
+
[name: string]: CreateRunnableMemory<I>;
|
|
52
|
+
}, State extends ContextState>({ context, ...options }: CreateOpenAPIAgentOptions<I, O, Memories, State>): OpenAPIAgent<SchemaMapType<I>, SchemaMapType<O>, {
|
|
53
|
+
[name in keyof Memories]: MemorableSearchOutput<Memories[name]['memory']>;
|
|
54
|
+
}, State>;
|
|
55
|
+
export {};
|