@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
package/lib/esm/index.js
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
export * from
|
|
4
|
-
export * from
|
|
5
|
-
export * from
|
|
6
|
-
export * from
|
|
7
|
-
export * from
|
|
8
|
-
export * from
|
|
9
|
-
export * from
|
|
10
|
-
export * from
|
|
11
|
-
export * from
|
|
12
|
-
export * from
|
|
13
|
-
export * from
|
|
14
|
-
export * from
|
|
15
|
-
export * from
|
|
16
|
-
export * from
|
|
17
|
-
export * from
|
|
18
|
-
export * from
|
|
1
|
+
export * from "./utils";
|
|
2
|
+
export * from "./constants";
|
|
3
|
+
export * from "./definitions/data-type";
|
|
4
|
+
export * from "./definitions/data-type-schema";
|
|
5
|
+
export * from "./definitions/open-api";
|
|
6
|
+
export * from "./definitions/memory";
|
|
7
|
+
export * from "./context";
|
|
8
|
+
export * from "./runnable";
|
|
9
|
+
export * from "./agent";
|
|
10
|
+
export * from "./pipeline-agent";
|
|
11
|
+
export * from "./llm-agent";
|
|
12
|
+
export * from "./llm-model";
|
|
13
|
+
export * from "./function-agent";
|
|
14
|
+
export * from "./function-runner";
|
|
15
|
+
export * from "./llm-decision-agent";
|
|
16
|
+
export * from "./local-function-agent";
|
|
17
|
+
export * from "./open-api-agent";
|
|
18
|
+
export * from "./memorable";
|
package/lib/esm/llm-agent.js
CHANGED
|
@@ -10,17 +10,16 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
10
10
|
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
11
11
|
return function (target, key) { decorator(target, key, paramIndex); }
|
|
12
12
|
};
|
|
13
|
-
import { nanoid } from
|
|
14
|
-
import { inject, injectable } from
|
|
15
|
-
import { Agent } from
|
|
16
|
-
import { StreamTextOutputName, TYPES } from
|
|
17
|
-
import { schemaToDataType } from
|
|
18
|
-
import { toRunnableMemories } from
|
|
19
|
-
import {
|
|
20
|
-
import {
|
|
21
|
-
import {
|
|
22
|
-
import {
|
|
23
|
-
import { outputsToJsonSchema } from './utils/structured-output-schema';
|
|
13
|
+
import { nanoid } from "nanoid";
|
|
14
|
+
import { inject, injectable } from "tsyringe";
|
|
15
|
+
import { Agent } from "./agent";
|
|
16
|
+
import { StreamTextOutputName, TYPES } from "./constants";
|
|
17
|
+
import { schemaToDataType, } from "./definitions/data-type-schema";
|
|
18
|
+
import { toRunnableMemories, } from "./definitions/memory";
|
|
19
|
+
import { prepareMessages } from "./utils/message-utils";
|
|
20
|
+
import { renderMessage } from "./utils/mustache-utils";
|
|
21
|
+
import { OrderedRecord } from "./utils/ordered-map";
|
|
22
|
+
import { outputsToJsonSchema } from "./utils/structured-output-schema";
|
|
24
23
|
let LLMAgent = class LLMAgent extends Agent {
|
|
25
24
|
definition;
|
|
26
25
|
model;
|
|
@@ -34,17 +33,17 @@ let LLMAgent = class LLMAgent extends Agent {
|
|
|
34
33
|
async *process(input, options) {
|
|
35
34
|
const { definition, model } = this;
|
|
36
35
|
if (!model)
|
|
37
|
-
throw new Error(
|
|
36
|
+
throw new Error("LLM model is required");
|
|
38
37
|
const { originalMessages, messagesWithMemory } = prepareMessages(definition, input, options.memories);
|
|
39
38
|
const llmInputs = {
|
|
40
39
|
messages: messagesWithMemory,
|
|
41
40
|
modelOptions: definition.modelOptions,
|
|
42
41
|
};
|
|
43
|
-
let $text =
|
|
42
|
+
let $text = "";
|
|
44
43
|
const hasTextOutput = OrderedRecord.find(definition.outputs, (i) => i.name === StreamTextOutputName);
|
|
45
44
|
if (hasTextOutput) {
|
|
46
45
|
for await (const chunk of await this.runWithTextOutput(llmInputs)) {
|
|
47
|
-
$text += chunk.$text ||
|
|
46
|
+
$text += chunk.$text || "";
|
|
48
47
|
yield { $text: chunk.$text };
|
|
49
48
|
}
|
|
50
49
|
}
|
|
@@ -53,37 +52,39 @@ let LLMAgent = class LLMAgent extends Agent {
|
|
|
53
52
|
yield { delta: json };
|
|
54
53
|
await this.updateMemories([
|
|
55
54
|
...originalMessages,
|
|
56
|
-
{
|
|
55
|
+
{
|
|
56
|
+
role: "assistant",
|
|
57
|
+
content: renderMessage("{{$text}}\n{{json}}", { $text, json }).trim(),
|
|
58
|
+
},
|
|
57
59
|
]);
|
|
58
60
|
}
|
|
59
61
|
async runWithStructuredOutput(llmInputs) {
|
|
60
|
-
const jsonOutputs = OrderedRecord.filter(this.definition.outputs, (i) => i.name !== StreamTextOutputName
|
|
61
|
-
);
|
|
62
|
+
const jsonOutputs = OrderedRecord.filter(this.definition.outputs, (i) => i.name !== StreamTextOutputName);
|
|
62
63
|
if (!jsonOutputs.length)
|
|
63
64
|
return null;
|
|
64
65
|
const schema = outputsToJsonSchema(OrderedRecord.fromArray(jsonOutputs));
|
|
65
66
|
const { model } = this;
|
|
66
67
|
if (!model)
|
|
67
|
-
throw new Error(
|
|
68
|
+
throw new Error("LLM model is required");
|
|
68
69
|
const response = await model.run({
|
|
69
70
|
...llmInputs,
|
|
70
71
|
responseFormat: {
|
|
71
|
-
type:
|
|
72
|
+
type: "json_schema",
|
|
72
73
|
jsonSchema: {
|
|
73
|
-
name:
|
|
74
|
+
name: "output",
|
|
74
75
|
schema,
|
|
75
76
|
strict: true,
|
|
76
77
|
},
|
|
77
78
|
},
|
|
78
79
|
});
|
|
79
80
|
if (!response.$text)
|
|
80
|
-
throw new Error(
|
|
81
|
+
throw new Error("No text in JSON mode response");
|
|
81
82
|
return JSON.parse(response.$text);
|
|
82
83
|
}
|
|
83
84
|
async runWithTextOutput(llmInputs) {
|
|
84
85
|
const { model } = this;
|
|
85
86
|
if (!model)
|
|
86
|
-
throw new Error(
|
|
87
|
+
throw new Error("LLM model is required");
|
|
87
88
|
return model.run(llmInputs, { stream: true });
|
|
88
89
|
}
|
|
89
90
|
};
|
|
@@ -92,7 +93,7 @@ LLMAgent = __decorate([
|
|
|
92
93
|
__param(0, inject(TYPES.definition)),
|
|
93
94
|
__param(1, inject(TYPES.context)),
|
|
94
95
|
__param(2, inject(TYPES.llmModel)),
|
|
95
|
-
__metadata("design:paramtypes", [Object, Object,
|
|
96
|
+
__metadata("design:paramtypes", [Object, Object, Function])
|
|
96
97
|
], LLMAgent);
|
|
97
98
|
export { LLMAgent };
|
|
98
99
|
/**
|
|
@@ -109,7 +110,7 @@ function create({ context, ...options }) {
|
|
|
109
110
|
.filter(([, i]) => i.primary)
|
|
110
111
|
.map(([name]) => name);
|
|
111
112
|
if (primaryMemoryNames && primaryMemoryNames.length > 1) {
|
|
112
|
-
throw new Error(
|
|
113
|
+
throw new Error("Only one primary memory is allowed");
|
|
113
114
|
}
|
|
114
115
|
const messages = OrderedRecord.fromArray(options.messages?.map((i) => ({
|
|
115
116
|
id: nanoid(),
|
|
@@ -119,7 +120,7 @@ function create({ context, ...options }) {
|
|
|
119
120
|
return new LLMAgent({
|
|
120
121
|
id: agentId,
|
|
121
122
|
name: options.name,
|
|
122
|
-
type:
|
|
123
|
+
type: "llm_agent",
|
|
123
124
|
inputs,
|
|
124
125
|
outputs,
|
|
125
126
|
primaryMemoryId: primaryMemoryNames?.at(0),
|
|
@@ -10,14 +10,13 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
10
10
|
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
11
11
|
return function (target, key) { decorator(target, key, paramIndex); }
|
|
12
12
|
};
|
|
13
|
-
import { nanoid } from
|
|
14
|
-
import { inject, injectable } from
|
|
15
|
-
import { Agent } from
|
|
16
|
-
import { TYPES } from
|
|
17
|
-
import { toRunnableMemories } from
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
20
|
-
import { prepareMessages } from './utils/message-utils';
|
|
13
|
+
import { nanoid } from "nanoid";
|
|
14
|
+
import { inject, injectable } from "tsyringe";
|
|
15
|
+
import { Agent } from "./agent";
|
|
16
|
+
import { TYPES } from "./constants";
|
|
17
|
+
import { toRunnableMemories, } from "./definitions/memory";
|
|
18
|
+
import { OrderedRecord, extractOutputsFromRunnableOutput, renderMessage, } from "./utils";
|
|
19
|
+
import { prepareMessages } from "./utils/message-utils";
|
|
21
20
|
let LLMDecisionAgent = class LLMDecisionAgent extends Agent {
|
|
22
21
|
definition;
|
|
23
22
|
model;
|
|
@@ -31,18 +30,18 @@ let LLMDecisionAgent = class LLMDecisionAgent extends Agent {
|
|
|
31
30
|
async process(input, options) {
|
|
32
31
|
const { definition, context, model } = this;
|
|
33
32
|
if (!model)
|
|
34
|
-
throw new Error(
|
|
33
|
+
throw new Error("LLM model is required");
|
|
35
34
|
if (!context)
|
|
36
|
-
throw new Error(
|
|
35
|
+
throw new Error("Context is required");
|
|
37
36
|
const { originalMessages, messagesWithMemory } = prepareMessages(definition, input, options.memories);
|
|
38
37
|
const cases = await Promise.all(OrderedRecord.map(definition.cases, async (t) => {
|
|
39
38
|
if (!t.runnable?.id)
|
|
40
|
-
throw new Error(
|
|
39
|
+
throw new Error("Runnable is required");
|
|
41
40
|
const runnable = await context.resolve(t.runnable.id);
|
|
42
41
|
// TODO: auto generate name by llm model if needed
|
|
43
42
|
const name = t.name || runnable.name;
|
|
44
43
|
if (!name)
|
|
45
|
-
throw new Error(
|
|
44
|
+
throw new Error("Case name is required");
|
|
46
45
|
return { name, description: t.description, runnable };
|
|
47
46
|
}));
|
|
48
47
|
const llmInputs = {
|
|
@@ -51,7 +50,7 @@ let LLMDecisionAgent = class LLMDecisionAgent extends Agent {
|
|
|
51
50
|
tools: cases.map((t) => {
|
|
52
51
|
// TODO: auto generate parameters by llm model if needed
|
|
53
52
|
return {
|
|
54
|
-
type:
|
|
53
|
+
type: "function",
|
|
55
54
|
function: {
|
|
56
55
|
name: t.name,
|
|
57
56
|
description: t.description,
|
|
@@ -59,21 +58,24 @@ let LLMDecisionAgent = class LLMDecisionAgent extends Agent {
|
|
|
59
58
|
},
|
|
60
59
|
};
|
|
61
60
|
}),
|
|
62
|
-
toolChoice:
|
|
61
|
+
toolChoice: "required",
|
|
63
62
|
};
|
|
64
63
|
const { toolCalls } = await model.run(llmInputs);
|
|
65
64
|
// TODO: support run multiple calls
|
|
66
65
|
const functionNameToCall = toolCalls?.[0]?.function?.name;
|
|
67
66
|
if (!functionNameToCall)
|
|
68
|
-
throw new Error(
|
|
67
|
+
throw new Error("No any runnable called");
|
|
69
68
|
const caseToCall = cases.find((i) => i.name === functionNameToCall);
|
|
70
69
|
if (!caseToCall)
|
|
71
|
-
throw new Error(
|
|
70
|
+
throw new Error("Case not found");
|
|
72
71
|
// TODO: check result structure and omit undefined values
|
|
73
72
|
const output = await caseToCall.runnable.run(input, { stream: true });
|
|
74
73
|
return extractOutputsFromRunnableOutput(output, ({ $text, ...json }) => this.updateMemories([
|
|
75
74
|
...originalMessages,
|
|
76
|
-
{
|
|
75
|
+
{
|
|
76
|
+
role: "assistant",
|
|
77
|
+
content: renderMessage("{{$text}}\n{{json}}", { $text, json }).trim(),
|
|
78
|
+
},
|
|
77
79
|
]));
|
|
78
80
|
}
|
|
79
81
|
};
|
|
@@ -82,7 +84,7 @@ LLMDecisionAgent = __decorate([
|
|
|
82
84
|
__param(0, inject(TYPES.definition)),
|
|
83
85
|
__param(1, inject(TYPES.context)),
|
|
84
86
|
__param(2, inject(TYPES.llmModel)),
|
|
85
|
-
__metadata("design:paramtypes", [Object, Object,
|
|
87
|
+
__metadata("design:paramtypes", [Object, Object, Function])
|
|
86
88
|
], LLMDecisionAgent);
|
|
87
89
|
export { LLMDecisionAgent };
|
|
88
90
|
function create({ context, ...options }) {
|
|
@@ -100,7 +102,7 @@ function create({ context, ...options }) {
|
|
|
100
102
|
.filter(([, i]) => i.primary)
|
|
101
103
|
.map(([name]) => name);
|
|
102
104
|
if (primaryMemoryNames && primaryMemoryNames.length > 1) {
|
|
103
|
-
throw new Error(
|
|
105
|
+
throw new Error("Only one primary memory is allowed");
|
|
104
106
|
}
|
|
105
107
|
const messages = OrderedRecord.fromArray(options.messages?.map((i) => ({
|
|
106
108
|
id: nanoid(),
|
|
@@ -110,7 +112,7 @@ function create({ context, ...options }) {
|
|
|
110
112
|
return new LLMDecisionAgent({
|
|
111
113
|
id: agentId,
|
|
112
114
|
name: options.name,
|
|
113
|
-
type:
|
|
115
|
+
type: "llm_decision_agent",
|
|
114
116
|
inputs,
|
|
115
117
|
outputs,
|
|
116
118
|
messages,
|
package/lib/esm/llm-model.js
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
import { Agent } from
|
|
2
|
-
import { OrderedRecord } from
|
|
1
|
+
import { Agent } from "./agent";
|
|
2
|
+
import { OrderedRecord } from "./utils";
|
|
3
3
|
export class LLMModel extends Agent {
|
|
4
4
|
constructor(context) {
|
|
5
5
|
super({
|
|
6
|
-
id:
|
|
7
|
-
type:
|
|
8
|
-
name:
|
|
9
|
-
description:
|
|
6
|
+
id: "llm_model",
|
|
7
|
+
type: "llm_model",
|
|
8
|
+
name: "LLM Model",
|
|
9
|
+
description: "Run a LLM model",
|
|
10
10
|
inputs: OrderedRecord.fromArray([
|
|
11
|
-
{ id:
|
|
12
|
-
{ id:
|
|
13
|
-
{ id:
|
|
14
|
-
{ id:
|
|
15
|
-
{ id:
|
|
11
|
+
{ id: "messages", name: "messages", type: "array", required: true },
|
|
12
|
+
{ id: "responseFormat", name: "responseFormat", type: "object" },
|
|
13
|
+
{ id: "tools", name: "tools", type: "array" },
|
|
14
|
+
{ id: "toolChoice", name: "toolChoice", type: "object" },
|
|
15
|
+
{ id: "modelOptions", name: "modelOptions", type: "object" },
|
|
16
16
|
]),
|
|
17
17
|
outputs: OrderedRecord.fromArray([
|
|
18
|
-
{ id:
|
|
19
|
-
{ id:
|
|
18
|
+
{ id: "$text", name: "$text", type: "string" },
|
|
19
|
+
{ id: "toolCalls", name: "toolCalls", type: "object" },
|
|
20
20
|
]),
|
|
21
21
|
}, context);
|
|
22
22
|
}
|
|
@@ -10,12 +10,12 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
10
10
|
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
11
11
|
return function (target, key) { decorator(target, key, paramIndex); }
|
|
12
12
|
};
|
|
13
|
-
import { nanoid } from
|
|
14
|
-
import { inject, injectable } from
|
|
15
|
-
import { Agent } from
|
|
16
|
-
import { TYPES } from
|
|
17
|
-
import { schemaToDataType } from
|
|
18
|
-
import { toRunnableMemories } from
|
|
13
|
+
import { nanoid } from "nanoid";
|
|
14
|
+
import { inject, injectable } from "tsyringe";
|
|
15
|
+
import { Agent } from "./agent";
|
|
16
|
+
import { TYPES } from "./constants";
|
|
17
|
+
import { schemaToDataType, } from "./definitions/data-type-schema";
|
|
18
|
+
import { toRunnableMemories, } from "./definitions/memory";
|
|
19
19
|
let LocalFunctionAgent = class LocalFunctionAgent extends Agent {
|
|
20
20
|
definition;
|
|
21
21
|
static create = create;
|
|
@@ -26,9 +26,9 @@ let LocalFunctionAgent = class LocalFunctionAgent extends Agent {
|
|
|
26
26
|
async process(input, options) {
|
|
27
27
|
const { definition: { function: func }, context, } = this;
|
|
28
28
|
if (!func)
|
|
29
|
-
throw new Error(
|
|
29
|
+
throw new Error("Function is required");
|
|
30
30
|
if (!context)
|
|
31
|
-
throw new Error(
|
|
31
|
+
throw new Error("Context is required");
|
|
32
32
|
return await func(input, { context, memories: options.memories });
|
|
33
33
|
}
|
|
34
34
|
};
|
|
@@ -47,7 +47,7 @@ function create({ context, ...options }) {
|
|
|
47
47
|
return new LocalFunctionAgent({
|
|
48
48
|
id: agentId,
|
|
49
49
|
name: options.name,
|
|
50
|
-
type:
|
|
50
|
+
type: "local_function_agent",
|
|
51
51
|
inputs,
|
|
52
52
|
outputs,
|
|
53
53
|
memories,
|
package/lib/esm/memorable.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { camelCase, startCase } from
|
|
2
|
-
import { Runnable } from
|
|
3
|
-
import { OrderedRecord } from
|
|
1
|
+
import { camelCase, startCase } from "lodash";
|
|
2
|
+
import { Runnable } from "./runnable";
|
|
3
|
+
import { OrderedRecord } from "./utils";
|
|
4
4
|
export class Memorable extends Runnable {
|
|
5
5
|
constructor() {
|
|
6
6
|
super({
|
|
7
|
-
id:
|
|
8
|
-
type:
|
|
9
|
-
name:
|
|
7
|
+
id: "memory",
|
|
8
|
+
type: "memory",
|
|
9
|
+
name: "Memory",
|
|
10
10
|
inputs: OrderedRecord.fromArray([]),
|
|
11
11
|
outputs: OrderedRecord.fromArray([]),
|
|
12
12
|
});
|
|
@@ -10,13 +10,13 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
10
10
|
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
11
11
|
return function (target, key) { decorator(target, key, paramIndex); }
|
|
12
12
|
};
|
|
13
|
-
import { nanoid } from
|
|
14
|
-
import { inject, injectable } from
|
|
15
|
-
import { Agent } from
|
|
16
|
-
import { TYPES } from
|
|
17
|
-
import { schemaToDataType } from
|
|
18
|
-
import { fetchOpenApi } from
|
|
19
|
-
import { formatOpenAPIRequest } from
|
|
13
|
+
import { nanoid } from "nanoid";
|
|
14
|
+
import { inject, injectable } from "tsyringe";
|
|
15
|
+
import { Agent } from "./agent";
|
|
16
|
+
import { TYPES } from "./constants";
|
|
17
|
+
import { schemaToDataType, } from "./definitions/data-type-schema";
|
|
18
|
+
import { fetchOpenApi } from "./utils/fetch-open-api";
|
|
19
|
+
import { formatOpenAPIRequest } from "./utils/open-api-parameter";
|
|
20
20
|
let OpenAPIAgent = class OpenAPIAgent extends Agent {
|
|
21
21
|
definition;
|
|
22
22
|
static create = create;
|
|
@@ -25,13 +25,13 @@ let OpenAPIAgent = class OpenAPIAgent extends Agent {
|
|
|
25
25
|
this.definition = definition;
|
|
26
26
|
}
|
|
27
27
|
async process(input) {
|
|
28
|
-
const { url, method =
|
|
28
|
+
const { url, method = "get", auth, inputs } = this.definition;
|
|
29
29
|
if (!url)
|
|
30
|
-
throw new Error(
|
|
30
|
+
throw new Error("API url is required");
|
|
31
31
|
const request = await formatOpenAPIRequest({ url, method, auth }, inputs, input);
|
|
32
32
|
return this.fetch(request);
|
|
33
33
|
}
|
|
34
|
-
fetch(request) {
|
|
34
|
+
async fetch(request) {
|
|
35
35
|
return fetchOpenApi(request);
|
|
36
36
|
}
|
|
37
37
|
};
|
|
@@ -49,7 +49,7 @@ function create({ context, ...options }) {
|
|
|
49
49
|
return new OpenAPIAgent({
|
|
50
50
|
id: agentId,
|
|
51
51
|
name: options.name,
|
|
52
|
-
type:
|
|
52
|
+
type: "open_api_agent",
|
|
53
53
|
inputs,
|
|
54
54
|
outputs,
|
|
55
55
|
url: options.url,
|
|
@@ -10,16 +10,16 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
10
10
|
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
11
11
|
return function (target, key) { decorator(target, key, paramIndex); }
|
|
12
12
|
};
|
|
13
|
-
import { get, isNil } from
|
|
14
|
-
import { nanoid } from
|
|
15
|
-
import { inject, injectable } from
|
|
16
|
-
import { Agent } from
|
|
17
|
-
import { StreamTextOutputName, TYPES } from
|
|
18
|
-
import { schemaToDataType } from
|
|
19
|
-
import { toRunnableMemories } from
|
|
20
|
-
import logger from
|
|
21
|
-
import { isNonNullable } from
|
|
22
|
-
import { OrderedRecord } from
|
|
13
|
+
import { get, isNil } from "lodash";
|
|
14
|
+
import { nanoid } from "nanoid";
|
|
15
|
+
import { inject, injectable } from "tsyringe";
|
|
16
|
+
import { Agent } from "./agent";
|
|
17
|
+
import { StreamTextOutputName, TYPES } from "./constants";
|
|
18
|
+
import { schemaToDataType, } from "./definitions/data-type-schema";
|
|
19
|
+
import { toRunnableMemories, } from "./definitions/memory";
|
|
20
|
+
import logger from "./logger";
|
|
21
|
+
import { isNonNullable } from "./utils/is-non-nullable";
|
|
22
|
+
import { OrderedRecord } from "./utils/ordered-map";
|
|
23
23
|
let PipelineAgent = class PipelineAgent extends Agent {
|
|
24
24
|
definition;
|
|
25
25
|
static create = create;
|
|
@@ -30,10 +30,10 @@ let PipelineAgent = class PipelineAgent extends Agent {
|
|
|
30
30
|
async process(input, options) {
|
|
31
31
|
const { definition, context } = this;
|
|
32
32
|
if (!context)
|
|
33
|
-
throw new Error(
|
|
33
|
+
throw new Error("Context is required");
|
|
34
34
|
const { processes } = definition;
|
|
35
35
|
if (!processes?.$indexes.length) {
|
|
36
|
-
throw new Error(
|
|
36
|
+
throw new Error("No processes defined");
|
|
37
37
|
}
|
|
38
38
|
return new ReadableStream({
|
|
39
39
|
async start(controller) {
|
|
@@ -53,7 +53,7 @@ let PipelineAgent = class PipelineAgent extends Agent {
|
|
|
53
53
|
let result = {};
|
|
54
54
|
for (const process of OrderedRecord.iterator(processes)) {
|
|
55
55
|
if (!process.runnable?.id) {
|
|
56
|
-
logger.warn(
|
|
56
|
+
logger.warn("Runnable id is required for process", process);
|
|
57
57
|
continue;
|
|
58
58
|
}
|
|
59
59
|
const runnable = await context.resolve(process.runnable.id);
|
|
@@ -65,18 +65,21 @@ let PipelineAgent = class PipelineAgent extends Agent {
|
|
|
65
65
|
if (!targetInput?.name)
|
|
66
66
|
return null;
|
|
67
67
|
let value;
|
|
68
|
-
if (input.from ===
|
|
68
|
+
if (input.from === "variable") {
|
|
69
69
|
const v = variables[input.fromVariableId];
|
|
70
|
-
value = input.fromVariablePropPath?.length
|
|
70
|
+
value = input.fromVariablePropPath?.length
|
|
71
|
+
? get(v, input.fromVariablePropPath)
|
|
72
|
+
: v;
|
|
71
73
|
}
|
|
72
74
|
else {
|
|
73
|
-
throw new Error(
|
|
75
|
+
throw new Error("Unsupported input source");
|
|
74
76
|
}
|
|
75
77
|
return [targetInput.name, value];
|
|
76
78
|
})
|
|
77
79
|
.filter(isNonNullable));
|
|
78
80
|
const stream = await runnable.run(inputValues, { stream: true });
|
|
79
|
-
const needRespondTextStream = textStreamOutput?.from ===
|
|
81
|
+
const needRespondTextStream = textStreamOutput?.from === "variable" &&
|
|
82
|
+
textStreamOutput.fromVariableId === process.id;
|
|
80
83
|
// const needRespondJsonStream = outputs.some(
|
|
81
84
|
// (i) => i.name !== StreamTextOutputName && i.from === 'variable' && i.fromVariableId === process.id
|
|
82
85
|
// );
|
|
@@ -84,7 +87,9 @@ let PipelineAgent = class PipelineAgent extends Agent {
|
|
|
84
87
|
variables[process.id] = processResult;
|
|
85
88
|
for await (const chunk of stream) {
|
|
86
89
|
if (chunk.$text) {
|
|
87
|
-
Object.assign(processResult, {
|
|
90
|
+
Object.assign(processResult, {
|
|
91
|
+
$text: (processResult.$text || "") + chunk.$text,
|
|
92
|
+
});
|
|
88
93
|
if (needRespondTextStream) {
|
|
89
94
|
controller.enqueue({ $text: chunk.$text });
|
|
90
95
|
}
|
|
@@ -99,9 +104,11 @@ let PipelineAgent = class PipelineAgent extends Agent {
|
|
|
99
104
|
if (output.name === StreamTextOutputName)
|
|
100
105
|
return null;
|
|
101
106
|
let value;
|
|
102
|
-
if (output.from ===
|
|
107
|
+
if (output.from === "variable") {
|
|
103
108
|
const v = variables[output.fromVariableId];
|
|
104
|
-
value = output.fromVariablePropPath?.length
|
|
109
|
+
value = output.fromVariablePropPath?.length
|
|
110
|
+
? get(v, output.fromVariablePropPath)
|
|
111
|
+
: v;
|
|
105
112
|
}
|
|
106
113
|
else {
|
|
107
114
|
throw new Error(`Unsupported output source ${output.from}`);
|
|
@@ -157,7 +164,7 @@ function create({ context, ...options }) {
|
|
|
157
164
|
return [
|
|
158
165
|
inputOfProcess.id,
|
|
159
166
|
{
|
|
160
|
-
from:
|
|
167
|
+
from: "variable",
|
|
161
168
|
fromVariableId: inputFromPipeline.id,
|
|
162
169
|
fromVariablePropPath: i.fromVariablePropPath,
|
|
163
170
|
},
|
|
@@ -171,12 +178,17 @@ function create({ context, ...options }) {
|
|
|
171
178
|
OrderedRecord.find(processes, (p) => p.name === fromVariable);
|
|
172
179
|
if (!from)
|
|
173
180
|
throw new Error(`Output ${output.name} not found in inputs or processes`);
|
|
174
|
-
return {
|
|
181
|
+
return {
|
|
182
|
+
...output,
|
|
183
|
+
from: "variable",
|
|
184
|
+
fromVariableId: from.id,
|
|
185
|
+
fromVariablePropPath,
|
|
186
|
+
};
|
|
175
187
|
}));
|
|
176
188
|
return new PipelineAgent({
|
|
177
189
|
id: agentId,
|
|
178
190
|
name: options.name,
|
|
179
|
-
type:
|
|
191
|
+
type: "pipeline_agent",
|
|
180
192
|
inputs,
|
|
181
193
|
memories,
|
|
182
194
|
outputs,
|
package/lib/esm/runnable.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { OrderedRecord } from
|
|
1
|
+
import { OrderedRecord } from "./utils/ordered-map";
|
|
2
2
|
export class Runnable {
|
|
3
3
|
definition;
|
|
4
4
|
context;
|
|
@@ -19,8 +19,8 @@ export class Runnable {
|
|
|
19
19
|
outputs;
|
|
20
20
|
}
|
|
21
21
|
export function isRunnableResponseDelta(chunk) {
|
|
22
|
-
return
|
|
22
|
+
return "$text" in chunk || "delta" in chunk;
|
|
23
23
|
}
|
|
24
24
|
export function isRunnableResponseError(chunk) {
|
|
25
|
-
return
|
|
25
|
+
return "error" in chunk;
|
|
26
26
|
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { withQuery } from
|
|
2
|
-
import { FETCH_TIMEOUT } from
|
|
3
|
-
import { checkFetchResponse } from
|
|
4
|
-
export
|
|
1
|
+
import { withQuery } from "ufo";
|
|
2
|
+
import { FETCH_TIMEOUT } from "./constants";
|
|
3
|
+
import { checkFetchResponse } from "./fetch";
|
|
4
|
+
export async function fetchOpenApi(request) {
|
|
5
5
|
const cookie = request.cookies
|
|
6
6
|
? Object.entries(request.cookies)
|
|
7
7
|
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
|
|
8
|
-
.join(
|
|
8
|
+
.join("; ")
|
|
9
9
|
.trim()
|
|
10
10
|
: undefined;
|
|
11
11
|
const controller = new AbortController();
|
|
@@ -13,14 +13,16 @@ export const fetchOpenApi = async (request) => {
|
|
|
13
13
|
const response = await fetch(withQuery(request.url, request.query ?? {}), {
|
|
14
14
|
method: request.method,
|
|
15
15
|
headers: {
|
|
16
|
-
|
|
16
|
+
"Content-Type": "application/json",
|
|
17
17
|
...request.headers,
|
|
18
18
|
...(cookie && { cookie }),
|
|
19
19
|
},
|
|
20
|
-
body: request.method.toLowerCase() !==
|
|
21
|
-
|
|
20
|
+
body: request.method.toLowerCase() !== "get" && request.body
|
|
21
|
+
? JSON.stringify(request.body)
|
|
22
|
+
: undefined,
|
|
23
|
+
credentials: request.cookies ? "include" : "same-origin",
|
|
22
24
|
signal: controller.signal,
|
|
23
25
|
}).finally(() => clearTimeout(abortTimer));
|
|
24
26
|
await checkFetchResponse(response);
|
|
25
27
|
return response.json();
|
|
26
|
-
}
|
|
28
|
+
}
|
package/lib/esm/utils/fetch.js
CHANGED
|
@@ -4,14 +4,15 @@ export async function checkFetchResponse(result) {
|
|
|
4
4
|
try {
|
|
5
5
|
const json = await result.json();
|
|
6
6
|
const msg = json.error?.message || json.message;
|
|
7
|
-
if (msg && typeof msg ===
|
|
7
|
+
if (msg && typeof msg === "string") {
|
|
8
8
|
message = msg;
|
|
9
9
|
}
|
|
10
10
|
}
|
|
11
11
|
catch {
|
|
12
12
|
// ignore
|
|
13
13
|
}
|
|
14
|
-
throw new Error(message ||
|
|
14
|
+
throw new Error(message ||
|
|
15
|
+
`Failed to fetch url ${result.url} with status ${result.status}`);
|
|
15
16
|
}
|
|
16
17
|
return result;
|
|
17
18
|
}
|
package/lib/esm/utils/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
export * from
|
|
4
|
-
export * from
|
|
5
|
-
export * from
|
|
6
|
-
export * from
|
|
7
|
-
export * from
|
|
8
|
-
export * from
|
|
9
|
-
export * from
|
|
10
|
-
export * from
|
|
1
|
+
export * from "./fetch";
|
|
2
|
+
export * from "./fetch-open-api";
|
|
3
|
+
export * from "./is-non-nullable";
|
|
4
|
+
export * from "./mustache-utils";
|
|
5
|
+
export * from "./nullable";
|
|
6
|
+
export * from "./omit";
|
|
7
|
+
export * from "./open-api-parameter";
|
|
8
|
+
export * from "./ordered-map";
|
|
9
|
+
export * from "./stream-utils";
|
|
10
|
+
export * from "./union";
|