@aigne/core 0.4.211-beta.2 → 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/lib/cjs/agent.js +18 -11
- package/lib/cjs/constants.js +5 -5
- package/lib/cjs/definitions/data-type-schema.js +12 -11
- package/lib/cjs/definitions/memory.js +3 -1
- package/lib/cjs/function-agent.js +5 -6
- package/lib/cjs/function-runner.js +13 -13
- package/lib/cjs/llm-agent.js +16 -15
- package/lib/cjs/llm-decision-agent.js +15 -13
- package/lib/cjs/llm-model.js +11 -11
- package/lib/cjs/local-function-agent.js +3 -3
- package/lib/cjs/memorable.js +3 -3
- package/lib/cjs/open-api-agent.js +4 -4
- package/lib/cjs/pipeline-agent.js +26 -17
- package/lib/cjs/runnable.js +2 -2
- package/lib/cjs/utils/fetch-open-api.js +9 -8
- package/lib/cjs/utils/fetch.js +3 -2
- package/lib/cjs/utils/is-non-nullable.js +1 -4
- package/lib/cjs/utils/message-utils.js +23 -16
- package/lib/cjs/utils/mustache-utils.js +2 -5
- package/lib/cjs/utils/open-api-parameter.js +19 -22
- package/lib/cjs/utils/stream-utils.js +6 -5
- package/lib/cjs/utils/structured-output-schema.js +10 -9
- package/lib/esm/agent.js +20 -10
- package/lib/esm/constants.js +5 -5
- package/lib/esm/definitions/data-type-schema.js +14 -13
- package/lib/esm/definitions/memory.js +5 -3
- package/lib/esm/function-agent.js +11 -12
- package/lib/esm/function-runner.js +15 -15
- package/lib/esm/index.js +18 -18
- package/lib/esm/llm-agent.js +26 -25
- package/lib/esm/llm-decision-agent.js +22 -20
- package/lib/esm/llm-model.js +13 -13
- package/lib/esm/local-function-agent.js +9 -9
- package/lib/esm/memorable.js +6 -6
- package/lib/esm/open-api-agent.js +11 -11
- package/lib/esm/pipeline-agent.js +35 -23
- package/lib/esm/runnable.js +3 -3
- package/lib/esm/utils/fetch-open-api.js +11 -9
- package/lib/esm/utils/fetch.js +3 -2
- package/lib/esm/utils/index.js +10 -10
- package/lib/esm/utils/is-non-nullable.js +1 -1
- package/lib/esm/utils/message-utils.js +26 -16
- package/lib/esm/utils/mustache-utils.js +2 -2
- package/lib/esm/utils/open-api-parameter.js +21 -21
- package/lib/esm/utils/stream-utils.js +7 -6
- package/lib/esm/utils/structured-output-schema.js +12 -8
- package/lib/types/agent.d.ts +3 -3
- package/lib/types/context.d.ts +2 -2
- package/lib/types/definitions/data-type-schema.d.ts +14 -14
- package/lib/types/definitions/data-type.d.ts +8 -8
- package/lib/types/definitions/memory.d.ts +5 -5
- package/lib/types/definitions/open-api.d.ts +9 -9
- package/lib/types/function-agent.d.ts +9 -9
- package/lib/types/function-runner.d.ts +4 -4
- package/lib/types/index.d.ts +18 -18
- package/lib/types/llm-agent.d.ts +12 -12
- package/lib/types/llm-decision-agent.d.ts +20 -20
- package/lib/types/llm-model.d.ts +12 -12
- package/lib/types/local-function-agent.d.ts +14 -16
- package/lib/types/memorable.d.ts +51 -51
- package/lib/types/open-api-agent.d.ts +12 -12
- package/lib/types/pipeline-agent.d.ts +14 -14
- package/lib/types/runnable.d.ts +5 -5
- package/lib/types/utils/fetch-open-api.d.ts +2 -2
- package/lib/types/utils/index.d.ts +10 -10
- package/lib/types/utils/message-utils.d.ts +4 -4
- package/lib/types/utils/open-api-parameter.d.ts +3 -3
- package/lib/types/utils/runnable-type.d.ts +1 -1
- package/lib/types/utils/stream-utils.d.ts +1 -1
- package/lib/types/utils/structured-output-schema.d.ts +2 -2
- package/package.json +11 -10
- package/tsconfig.json +3 -1
- package/lib/cjs/api-agent.js +0 -70
- package/lib/cjs/data-type-schema.js +0 -46
- package/lib/cjs/data-type.js +0 -2
- package/lib/cjs/definitions/api-parameter.js +0 -10
- package/lib/cjs/memory.js +0 -32
- package/lib/cjs/tsconfig.tsbuildinfo +0 -1
- package/lib/cjs/utils/fetch-api.js +0 -35
- package/lib/cjs/utils/flatten-openapi.js +0 -14
- package/lib/cjs/utils/format-parameter.js +0 -126
- package/lib/esm/api-agent.js +0 -67
- package/lib/esm/data-type-schema.js +0 -43
- package/lib/esm/data-type.js +0 -1
- package/lib/esm/definitions/api-parameter.js +0 -7
- package/lib/esm/memory.js +0 -27
- package/lib/esm/tsconfig.tsbuildinfo +0 -1
- package/lib/esm/utils/fetch-api.js +0 -31
- package/lib/esm/utils/flatten-openapi.js +0 -11
- package/lib/esm/utils/format-parameter.js +0 -116
- package/lib/types/api-agent.d.ts +0 -53
- package/lib/types/data-type-schema.d.ts +0 -46
- package/lib/types/data-type.d.ts +0 -32
- package/lib/types/definitions/api-parameter.d.ts +0 -55
- package/lib/types/memory.d.ts +0 -184
- package/lib/types/tsconfig.tsbuildinfo +0 -1
- package/lib/types/utils/fetch-api.d.ts +0 -3
- package/lib/types/utils/flatten-openapi.d.ts +0 -25
- package/lib/types/utils/format-parameter.d.ts +0 -6
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.fetchApi = void 0;
|
|
4
|
-
const ufo_1 = require("ufo");
|
|
5
|
-
const constants_1 = require("./constants");
|
|
6
|
-
const fetchApi = async (request) => {
|
|
7
|
-
let cookieString = '';
|
|
8
|
-
if (request.cookies) {
|
|
9
|
-
cookieString = Object.entries(request.cookies)
|
|
10
|
-
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
|
|
11
|
-
.join('; ');
|
|
12
|
-
}
|
|
13
|
-
const controller = new AbortController();
|
|
14
|
-
const timeoutId = setTimeout(() => controller.abort(), Number(constants_1.TIMEOUT));
|
|
15
|
-
const response = await fetch((0, ufo_1.withQuery)(request.url, request.query || {}), {
|
|
16
|
-
method: request.method,
|
|
17
|
-
headers: {
|
|
18
|
-
'Content-Type': 'application/json',
|
|
19
|
-
...(cookieString && { Cookie: cookieString.trim() }),
|
|
20
|
-
...request.headers,
|
|
21
|
-
},
|
|
22
|
-
body: request.method !== 'GET' ? JSON.stringify(request.body) : undefined,
|
|
23
|
-
credentials: request.cookies ? 'include' : 'same-origin',
|
|
24
|
-
signal: controller.signal,
|
|
25
|
-
}).finally(() => clearTimeout(timeoutId));
|
|
26
|
-
if (!response.ok) {
|
|
27
|
-
const errorBody = await response.text();
|
|
28
|
-
throw new Error(`API request failed for ${request.url}\n` +
|
|
29
|
-
`Status: ${response.status} ${response.statusText}\n` +
|
|
30
|
-
`Response: ${errorBody.slice(0, 200)}`);
|
|
31
|
-
}
|
|
32
|
-
return response.json();
|
|
33
|
-
};
|
|
34
|
-
exports.fetchApi = fetchApi;
|
|
35
|
-
exports.default = exports.fetchApi;
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.flattenApiStructure = flattenApiStructure;
|
|
4
|
-
function flattenApiStructure(apiStructure) {
|
|
5
|
-
const paths = apiStructure?.paths || {};
|
|
6
|
-
return Object.entries(paths).flatMap(([, methods]) => Object.entries(methods).map(([, endpoint]) => ({
|
|
7
|
-
id: endpoint['x-id'],
|
|
8
|
-
name: endpoint['x-did'],
|
|
9
|
-
path: endpoint['x-path'],
|
|
10
|
-
method: endpoint['x-method'],
|
|
11
|
-
did: endpoint['x-did'],
|
|
12
|
-
...endpoint,
|
|
13
|
-
})));
|
|
14
|
-
}
|
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.formatRequest = void 0;
|
|
7
|
-
exports.getAuthParams = getAuthParams;
|
|
8
|
-
exports.processParameters = processParameters;
|
|
9
|
-
exports.mergeParameters = mergeParameters;
|
|
10
|
-
const logger_1 = __importDefault(require("../logger"));
|
|
11
|
-
function getAuthParams(auth) {
|
|
12
|
-
if (!auth)
|
|
13
|
-
return {};
|
|
14
|
-
if (auth.type === 'custom') {
|
|
15
|
-
return { headers: auth.getValue() };
|
|
16
|
-
}
|
|
17
|
-
const paramKey = auth.key || 'Authorization';
|
|
18
|
-
const paramValue = (() => {
|
|
19
|
-
let paramValue = auth.token;
|
|
20
|
-
switch (auth.type) {
|
|
21
|
-
case 'basic':
|
|
22
|
-
paramValue = `Basic ${auth.token}`;
|
|
23
|
-
break;
|
|
24
|
-
case 'bearer':
|
|
25
|
-
paramValue = `Bearer ${auth.token}`;
|
|
26
|
-
break;
|
|
27
|
-
default:
|
|
28
|
-
break;
|
|
29
|
-
}
|
|
30
|
-
return paramValue;
|
|
31
|
-
})();
|
|
32
|
-
switch (auth.in) {
|
|
33
|
-
case 'header':
|
|
34
|
-
return { headers: { [paramKey]: paramValue } };
|
|
35
|
-
case 'query':
|
|
36
|
-
return { query: { [paramKey]: paramValue } };
|
|
37
|
-
case 'cookie':
|
|
38
|
-
return { cookies: { [paramKey]: paramValue } };
|
|
39
|
-
default:
|
|
40
|
-
// 默认放在 header 中
|
|
41
|
-
return { headers: { [paramKey]: paramValue } };
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
function processParameters(api, inputs, input) {
|
|
45
|
-
const method = (api.method || 'GET').toUpperCase();
|
|
46
|
-
const result = {
|
|
47
|
-
url: api.url,
|
|
48
|
-
method,
|
|
49
|
-
headers: {},
|
|
50
|
-
query: {},
|
|
51
|
-
cookies: {},
|
|
52
|
-
body: {},
|
|
53
|
-
};
|
|
54
|
-
const filterInputs = Object.values(inputs).filter((i) => !Array.isArray(i));
|
|
55
|
-
// 处理路径参数
|
|
56
|
-
let processedUrl = api.url;
|
|
57
|
-
const pathParams = filterInputs.filter((x) => x.in === 'path');
|
|
58
|
-
pathParams.forEach(({ name }) => {
|
|
59
|
-
if (name && input[name] !== undefined) {
|
|
60
|
-
const placeholder = `{${name}}`;
|
|
61
|
-
processedUrl = processedUrl.replace(placeholder, String(input[name]));
|
|
62
|
-
}
|
|
63
|
-
});
|
|
64
|
-
result.url = processedUrl;
|
|
65
|
-
// 处理其他参数
|
|
66
|
-
Object.entries(input).forEach(([key, value]) => {
|
|
67
|
-
const schema = filterInputs.find((i) => i.name === key);
|
|
68
|
-
const paramIn = schema?.in;
|
|
69
|
-
switch (paramIn) {
|
|
70
|
-
case 'query':
|
|
71
|
-
result.query[key] = value;
|
|
72
|
-
break;
|
|
73
|
-
case 'header':
|
|
74
|
-
result.headers[key] = value;
|
|
75
|
-
break;
|
|
76
|
-
case 'cookie':
|
|
77
|
-
result.cookies[key] = value;
|
|
78
|
-
break;
|
|
79
|
-
case 'body':
|
|
80
|
-
result.body[key] = value;
|
|
81
|
-
break;
|
|
82
|
-
case 'path':
|
|
83
|
-
break;
|
|
84
|
-
default:
|
|
85
|
-
// 没有指定 in 的情况
|
|
86
|
-
if (method === 'GET') {
|
|
87
|
-
result.query[key] = value;
|
|
88
|
-
}
|
|
89
|
-
else {
|
|
90
|
-
result.body[key] = value;
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
});
|
|
94
|
-
return result;
|
|
95
|
-
}
|
|
96
|
-
function mergeParameters(parameters, authParams) {
|
|
97
|
-
const result = {};
|
|
98
|
-
if (parameters.query || authParams.query) {
|
|
99
|
-
result.query = { ...(parameters.query || {}), ...(authParams.query || {}) };
|
|
100
|
-
}
|
|
101
|
-
if (parameters.cookies || authParams.cookies) {
|
|
102
|
-
result.cookies = { ...(parameters.cookies || {}), ...(authParams.cookies || {}) };
|
|
103
|
-
}
|
|
104
|
-
if (parameters.headers || authParams.headers) {
|
|
105
|
-
result.headers = { ...(parameters.headers || {}), ...(authParams.headers || {}) };
|
|
106
|
-
}
|
|
107
|
-
return result;
|
|
108
|
-
}
|
|
109
|
-
const formatRequest = (api, inputs, input) => {
|
|
110
|
-
const inputParams = processParameters(api, inputs, input);
|
|
111
|
-
logger_1.default.debug('inputParams', inputParams);
|
|
112
|
-
const authParams = getAuthParams(api.auth);
|
|
113
|
-
logger_1.default.debug('authParams', authParams);
|
|
114
|
-
const mergedParams = mergeParameters(inputParams, authParams);
|
|
115
|
-
logger_1.default.debug('mergedParams', mergedParams);
|
|
116
|
-
const params = { ...inputParams, ...mergedParams };
|
|
117
|
-
return {
|
|
118
|
-
url: params.url,
|
|
119
|
-
method: params.method,
|
|
120
|
-
...(params.headers && Object.keys(params.headers).length > 0 && { headers: params.headers }),
|
|
121
|
-
...(params.query && Object.keys(params.query).length > 0 && { query: params.query }),
|
|
122
|
-
...(params.cookies && Object.keys(params.cookies).length > 0 && { cookies: params.cookies }),
|
|
123
|
-
...(params.body && Object.keys(params.body).length > 0 && { body: params.body }),
|
|
124
|
-
};
|
|
125
|
-
};
|
|
126
|
-
exports.formatRequest = formatRequest;
|
package/lib/esm/api-agent.js
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
-
};
|
|
7
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
-
};
|
|
10
|
-
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
11
|
-
return function (target, key) { decorator(target, key, paramIndex); }
|
|
12
|
-
};
|
|
13
|
-
import { pick } from 'lodash';
|
|
14
|
-
import { nanoid } from 'nanoid';
|
|
15
|
-
import { inject, injectable } from 'tsyringe';
|
|
16
|
-
import { Agent } from './agent';
|
|
17
|
-
import { TYPES } from './constants';
|
|
18
|
-
import { schemaToDataType } from './definitions/data-type-schema';
|
|
19
|
-
import { fetchApi } from './utils/fetch-api';
|
|
20
|
-
import { formatRequest } from './utils/format-parameter';
|
|
21
|
-
let OpenAPIAgent = class OpenAPIAgent extends Agent {
|
|
22
|
-
definition;
|
|
23
|
-
static create = create;
|
|
24
|
-
constructor(definition, context) {
|
|
25
|
-
super(definition, context);
|
|
26
|
-
this.definition = definition;
|
|
27
|
-
}
|
|
28
|
-
async process(input) {
|
|
29
|
-
const { definition: { url, method, auth, inputs }, context, } = this;
|
|
30
|
-
if (!url)
|
|
31
|
-
throw new Error('API url is required');
|
|
32
|
-
const api = {
|
|
33
|
-
url,
|
|
34
|
-
method,
|
|
35
|
-
auth,
|
|
36
|
-
};
|
|
37
|
-
const request = formatRequest(api, inputs, input);
|
|
38
|
-
const contextState = pick(context?.state, ['userId', 'sessionId']);
|
|
39
|
-
request.query = { ...contextState, ...(request.query || {}) };
|
|
40
|
-
return this.fetch(request);
|
|
41
|
-
}
|
|
42
|
-
fetch(request) {
|
|
43
|
-
return fetchApi(request);
|
|
44
|
-
}
|
|
45
|
-
};
|
|
46
|
-
OpenAPIAgent = __decorate([
|
|
47
|
-
injectable(),
|
|
48
|
-
__param(0, inject(TYPES.definition)),
|
|
49
|
-
__param(1, inject(TYPES.context)),
|
|
50
|
-
__metadata("design:paramtypes", [Object, Object])
|
|
51
|
-
], OpenAPIAgent);
|
|
52
|
-
export { OpenAPIAgent };
|
|
53
|
-
function create({ context, ...options }) {
|
|
54
|
-
const agentId = options.id || options.name || nanoid();
|
|
55
|
-
const inputs = schemaToDataType(options.inputs);
|
|
56
|
-
const outputs = schemaToDataType(options.outputs);
|
|
57
|
-
return new OpenAPIAgent({
|
|
58
|
-
id: agentId,
|
|
59
|
-
name: options.name,
|
|
60
|
-
type: 'api_agent',
|
|
61
|
-
inputs,
|
|
62
|
-
outputs,
|
|
63
|
-
url: options.url,
|
|
64
|
-
method: options.method,
|
|
65
|
-
auth: options.auth,
|
|
66
|
-
}, context);
|
|
67
|
-
}
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { nanoid } from 'nanoid';
|
|
2
|
-
import { OrderedRecord } from './utils';
|
|
3
|
-
export function schemaToDataType(dataType) {
|
|
4
|
-
return OrderedRecord.fromArray(Object.entries(dataType).map(([name, schema]) => {
|
|
5
|
-
const base = {
|
|
6
|
-
...schema,
|
|
7
|
-
id: nanoid(),
|
|
8
|
-
name,
|
|
9
|
-
};
|
|
10
|
-
switch (schema.type) {
|
|
11
|
-
case 'string':
|
|
12
|
-
return {
|
|
13
|
-
...base,
|
|
14
|
-
type: 'string',
|
|
15
|
-
};
|
|
16
|
-
case 'number':
|
|
17
|
-
return {
|
|
18
|
-
...base,
|
|
19
|
-
type: 'number',
|
|
20
|
-
};
|
|
21
|
-
case 'boolean':
|
|
22
|
-
return {
|
|
23
|
-
...base,
|
|
24
|
-
type: 'boolean',
|
|
25
|
-
};
|
|
26
|
-
case 'object':
|
|
27
|
-
return {
|
|
28
|
-
...base,
|
|
29
|
-
type: 'object',
|
|
30
|
-
properties: schemaToDataType(schema.properties),
|
|
31
|
-
};
|
|
32
|
-
case 'array':
|
|
33
|
-
return {
|
|
34
|
-
...base,
|
|
35
|
-
type: 'array',
|
|
36
|
-
items: OrderedRecord.find(schemaToDataType({ items: schema.items }), (i) => i.name === 'items'),
|
|
37
|
-
};
|
|
38
|
-
default: {
|
|
39
|
-
throw new Error(`Unknown data type: ${schema.type}`);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
}));
|
|
43
|
-
}
|
package/lib/esm/data-type.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/lib/esm/memory.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { camelCase, startCase } from 'lodash';
|
|
2
|
-
import { Runnable } from './runnable';
|
|
3
|
-
import { OrderedRecord } from './utils';
|
|
4
|
-
export class Memory extends Runnable {
|
|
5
|
-
constructor() {
|
|
6
|
-
super({
|
|
7
|
-
id: 'memory',
|
|
8
|
-
type: 'memory',
|
|
9
|
-
name: 'Memory',
|
|
10
|
-
inputs: OrderedRecord.fromArray([]),
|
|
11
|
-
outputs: OrderedRecord.fromArray([]),
|
|
12
|
-
});
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
export class MemoryRunner extends Runnable {
|
|
16
|
-
constructor(name) {
|
|
17
|
-
const id = `${camelCase(name)}_runner`;
|
|
18
|
-
super({
|
|
19
|
-
id,
|
|
20
|
-
type: id,
|
|
21
|
-
name: `${startCase(name)} Runner`,
|
|
22
|
-
description: `${startCase(name)} Runner`,
|
|
23
|
-
inputs: OrderedRecord.fromArray([]),
|
|
24
|
-
outputs: OrderedRecord.fromArray([]),
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
}
|