@aigne/core 1.0.16 → 1.1.0-beta.1
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/agents/agent.d.ts +49 -0
- package/lib/cjs/agents/agent.js +92 -0
- package/lib/cjs/agents/ai-agent.d.ts +21 -0
- package/lib/cjs/agents/ai-agent.js +84 -0
- package/lib/cjs/agents/mcp-agent.d.ts +26 -0
- package/lib/cjs/agents/mcp-agent.js +75 -0
- package/lib/cjs/agents/types.d.ts +9 -0
- package/lib/cjs/agents/types.js +16 -0
- package/lib/cjs/execution-engine/context.d.ts +10 -0
- package/lib/cjs/execution-engine/index.d.ts +45 -0
- package/lib/cjs/execution-engine/index.js +190 -0
- package/lib/cjs/execution-engine/message-queue.d.ts +5 -0
- package/lib/cjs/execution-engine/message-queue.js +9 -0
- package/lib/cjs/index.d.ts +10 -21
- package/lib/cjs/index.js +10 -21
- package/lib/cjs/models/chat-openai.d.ts +14 -0
- package/lib/cjs/{llm-models/openai-llm-model.js → models/chat-openai.js} +58 -26
- package/lib/cjs/models/chat.d.ts +78 -0
- package/lib/cjs/models/chat.js +91 -0
- package/lib/cjs/prompt/prompt-builder.d.ts +25 -0
- package/lib/cjs/prompt/prompt-builder.js +126 -0
- package/lib/cjs/prompt/template.d.ts +70 -0
- package/lib/cjs/prompt/template.js +172 -0
- package/lib/cjs/prompt/templates/instructions.d.ts +1 -0
- package/lib/cjs/prompt/templates/instructions.js +10 -0
- package/lib/cjs/utils/logger.js +1 -3
- package/lib/cjs/utils/type-utils.d.ts +5 -1
- package/lib/cjs/utils/type-utils.js +23 -0
- package/lib/dts/agents/agent.d.ts +49 -0
- package/lib/dts/agents/ai-agent.d.ts +21 -0
- package/lib/dts/agents/mcp-agent.d.ts +26 -0
- package/lib/dts/agents/types.d.ts +9 -0
- package/lib/dts/execution-engine/context.d.ts +10 -0
- package/lib/dts/execution-engine/index.d.ts +45 -0
- package/lib/dts/execution-engine/message-queue.d.ts +5 -0
- package/lib/dts/index.d.ts +10 -21
- package/lib/dts/models/chat-openai.d.ts +14 -0
- package/lib/dts/models/chat.d.ts +78 -0
- package/lib/dts/prompt/prompt-builder.d.ts +25 -0
- package/lib/dts/prompt/template.d.ts +70 -0
- package/lib/dts/prompt/templates/instructions.d.ts +1 -0
- package/lib/dts/utils/type-utils.d.ts +5 -1
- package/lib/esm/agents/agent.d.ts +49 -0
- package/lib/esm/agents/agent.js +84 -0
- package/lib/esm/agents/ai-agent.d.ts +21 -0
- package/lib/esm/agents/ai-agent.js +80 -0
- package/lib/esm/agents/mcp-agent.d.ts +26 -0
- package/lib/esm/agents/mcp-agent.js +70 -0
- package/lib/esm/agents/types.d.ts +9 -0
- package/lib/esm/agents/types.js +11 -0
- package/lib/esm/execution-engine/context.d.ts +10 -0
- package/lib/esm/execution-engine/index.d.ts +45 -0
- package/lib/esm/execution-engine/index.js +182 -0
- package/lib/esm/execution-engine/message-queue.d.ts +5 -0
- package/lib/esm/execution-engine/message-queue.js +5 -0
- package/lib/esm/index.d.ts +10 -21
- package/lib/esm/index.js +10 -21
- package/lib/esm/models/chat-openai.d.ts +14 -0
- package/lib/esm/{llm-models/openai-llm-model.js → models/chat-openai.js} +55 -23
- package/lib/esm/models/chat.d.ts +78 -0
- package/lib/esm/models/chat.js +87 -0
- package/lib/esm/prompt/prompt-builder.d.ts +25 -0
- package/lib/esm/prompt/prompt-builder.js +117 -0
- package/lib/esm/prompt/template.d.ts +70 -0
- package/lib/esm/prompt/template.js +158 -0
- package/lib/esm/prompt/templates/instructions.d.ts +1 -0
- package/lib/esm/prompt/templates/instructions.js +7 -0
- package/lib/esm/utils/logger.js +1 -3
- package/lib/esm/utils/type-utils.d.ts +5 -1
- package/lib/esm/utils/type-utils.js +17 -1
- package/package.json +14 -14
- package/lib/cjs/agent.d.ts +0 -96
- package/lib/cjs/agent.js +0 -165
- package/lib/cjs/constants.d.ts +0 -7
- package/lib/cjs/constants.js +0 -10
- package/lib/cjs/context.d.ts +0 -23
- package/lib/cjs/definitions/data-schema.d.ts +0 -61
- package/lib/cjs/definitions/data-schema.js +0 -47
- package/lib/cjs/definitions/data-type.d.ts +0 -32
- package/lib/cjs/definitions/data-type.js +0 -2
- package/lib/cjs/definitions/memory.d.ts +0 -53
- package/lib/cjs/definitions/memory.js +0 -23
- package/lib/cjs/definitions/open-api.d.ts +0 -36
- package/lib/cjs/definitions/open-api.js +0 -2
- package/lib/cjs/definitions/preload.d.ts +0 -43
- package/lib/cjs/definitions/preload.js +0 -34
- package/lib/cjs/function-agent.d.ts +0 -23
- package/lib/cjs/function-agent.js +0 -62
- package/lib/cjs/llm-agent.d.ts +0 -53
- package/lib/cjs/llm-agent.js +0 -137
- package/lib/cjs/llm-decision-agent.d.ts +0 -53
- package/lib/cjs/llm-decision-agent.js +0 -144
- package/lib/cjs/llm-model.d.ts +0 -80
- package/lib/cjs/llm-model.js +0 -27
- package/lib/cjs/llm-models/gemini-llm-model.d.ts +0 -24
- package/lib/cjs/llm-models/gemini-llm-model.js +0 -199
- package/lib/cjs/llm-models/openai-llm-model.d.ts +0 -26
- package/lib/cjs/memorable.d.ts +0 -198
- package/lib/cjs/memorable.js +0 -33
- package/lib/cjs/open-api-agent.d.ts +0 -26
- package/lib/cjs/open-api-agent.js +0 -68
- package/lib/cjs/pipeline-agent.d.ts +0 -56
- package/lib/cjs/pipeline-agent.js +0 -203
- package/lib/cjs/runnable.d.ts +0 -53
- package/lib/cjs/runnable.js +0 -32
- package/lib/cjs/runtime.d.ts +0 -64
- package/lib/cjs/runtime.js +0 -149
- package/lib/cjs/sandbox-function-agent.d.ts +0 -21
- package/lib/cjs/sandbox-function-agent.js +0 -78
- package/lib/cjs/sandbox-function-runner.d.ts +0 -16
- package/lib/cjs/sandbox-function-runner.js +0 -32
- package/lib/cjs/utils/constants.d.ts +0 -1
- package/lib/cjs/utils/constants.js +0 -4
- package/lib/cjs/utils/fetch-open-api.d.ts +0 -2
- package/lib/cjs/utils/fetch-open-api.js +0 -31
- package/lib/cjs/utils/fetch.d.ts +0 -1
- package/lib/cjs/utils/fetch.js +0 -21
- package/lib/cjs/utils/index.d.ts +0 -12
- package/lib/cjs/utils/index.js +0 -28
- package/lib/cjs/utils/is-non-nullable.d.ts +0 -2
- package/lib/cjs/utils/is-non-nullable.js +0 -20
- package/lib/cjs/utils/message-utils.d.ts +0 -20
- package/lib/cjs/utils/message-utils.js +0 -79
- package/lib/cjs/utils/mustache-utils.d.ts +0 -3
- package/lib/cjs/utils/mustache-utils.js +0 -14
- package/lib/cjs/utils/nullable.d.ts +0 -7
- package/lib/cjs/utils/nullable.js +0 -2
- package/lib/cjs/utils/omit.d.ts +0 -1
- package/lib/cjs/utils/omit.js +0 -2
- package/lib/cjs/utils/open-api-parameter.d.ts +0 -7
- package/lib/cjs/utils/open-api-parameter.js +0 -81
- package/lib/cjs/utils/ordered-map.d.ts +0 -37
- package/lib/cjs/utils/ordered-map.js +0 -103
- package/lib/cjs/utils/partial.d.ts +0 -3
- package/lib/cjs/utils/partial.js +0 -2
- package/lib/cjs/utils/runnable-type.d.ts +0 -5
- package/lib/cjs/utils/runnable-type.js +0 -2
- package/lib/cjs/utils/stream-utils.d.ts +0 -20
- package/lib/cjs/utils/stream-utils.js +0 -86
- package/lib/cjs/utils/structured-output-schema.d.ts +0 -3
- package/lib/cjs/utils/structured-output-schema.js +0 -43
- package/lib/cjs/utils/union.d.ts +0 -1
- package/lib/cjs/utils/union.js +0 -2
- package/lib/dts/agent.d.ts +0 -96
- package/lib/dts/constants.d.ts +0 -7
- package/lib/dts/context.d.ts +0 -23
- package/lib/dts/definitions/data-schema.d.ts +0 -61
- package/lib/dts/definitions/data-type.d.ts +0 -32
- package/lib/dts/definitions/memory.d.ts +0 -53
- package/lib/dts/definitions/open-api.d.ts +0 -36
- package/lib/dts/definitions/preload.d.ts +0 -43
- package/lib/dts/function-agent.d.ts +0 -23
- package/lib/dts/llm-agent.d.ts +0 -53
- package/lib/dts/llm-decision-agent.d.ts +0 -53
- package/lib/dts/llm-model.d.ts +0 -80
- package/lib/dts/llm-models/gemini-llm-model.d.ts +0 -24
- package/lib/dts/llm-models/openai-llm-model.d.ts +0 -26
- package/lib/dts/memorable.d.ts +0 -198
- package/lib/dts/open-api-agent.d.ts +0 -26
- package/lib/dts/pipeline-agent.d.ts +0 -56
- package/lib/dts/runnable.d.ts +0 -53
- package/lib/dts/runtime.d.ts +0 -64
- package/lib/dts/sandbox-function-agent.d.ts +0 -21
- package/lib/dts/sandbox-function-runner.d.ts +0 -16
- package/lib/dts/utils/constants.d.ts +0 -1
- package/lib/dts/utils/fetch-open-api.d.ts +0 -2
- package/lib/dts/utils/fetch.d.ts +0 -1
- package/lib/dts/utils/index.d.ts +0 -12
- package/lib/dts/utils/is-non-nullable.d.ts +0 -2
- package/lib/dts/utils/message-utils.d.ts +0 -20
- package/lib/dts/utils/mustache-utils.d.ts +0 -3
- package/lib/dts/utils/nullable.d.ts +0 -7
- package/lib/dts/utils/omit.d.ts +0 -1
- package/lib/dts/utils/open-api-parameter.d.ts +0 -7
- package/lib/dts/utils/ordered-map.d.ts +0 -37
- package/lib/dts/utils/partial.d.ts +0 -3
- package/lib/dts/utils/runnable-type.d.ts +0 -5
- package/lib/dts/utils/stream-utils.d.ts +0 -20
- package/lib/dts/utils/structured-output-schema.d.ts +0 -3
- package/lib/dts/utils/union.d.ts +0 -1
- package/lib/esm/agent.d.ts +0 -96
- package/lib/esm/agent.js +0 -161
- package/lib/esm/constants.d.ts +0 -7
- package/lib/esm/constants.js +0 -7
- package/lib/esm/context.d.ts +0 -23
- package/lib/esm/definitions/data-schema.d.ts +0 -61
- package/lib/esm/definitions/data-schema.js +0 -44
- package/lib/esm/definitions/data-type.d.ts +0 -32
- package/lib/esm/definitions/data-type.js +0 -1
- package/lib/esm/definitions/memory.d.ts +0 -53
- package/lib/esm/definitions/memory.js +0 -20
- package/lib/esm/definitions/open-api.d.ts +0 -36
- package/lib/esm/definitions/open-api.js +0 -1
- package/lib/esm/definitions/preload.d.ts +0 -43
- package/lib/esm/definitions/preload.js +0 -31
- package/lib/esm/function-agent.d.ts +0 -23
- package/lib/esm/function-agent.js +0 -59
- package/lib/esm/llm-agent.d.ts +0 -53
- package/lib/esm/llm-agent.js +0 -134
- package/lib/esm/llm-decision-agent.d.ts +0 -53
- package/lib/esm/llm-decision-agent.js +0 -141
- package/lib/esm/llm-model.d.ts +0 -80
- package/lib/esm/llm-model.js +0 -23
- package/lib/esm/llm-models/gemini-llm-model.d.ts +0 -24
- package/lib/esm/llm-models/gemini-llm-model.js +0 -195
- package/lib/esm/llm-models/openai-llm-model.d.ts +0 -26
- package/lib/esm/memorable.d.ts +0 -198
- package/lib/esm/memorable.js +0 -28
- package/lib/esm/open-api-agent.d.ts +0 -26
- package/lib/esm/open-api-agent.js +0 -65
- package/lib/esm/pipeline-agent.d.ts +0 -56
- package/lib/esm/pipeline-agent.js +0 -200
- package/lib/esm/runnable.d.ts +0 -53
- package/lib/esm/runnable.js +0 -26
- package/lib/esm/runtime.d.ts +0 -64
- package/lib/esm/runtime.js +0 -146
- package/lib/esm/sandbox-function-agent.d.ts +0 -21
- package/lib/esm/sandbox-function-agent.js +0 -74
- package/lib/esm/sandbox-function-runner.d.ts +0 -16
- package/lib/esm/sandbox-function-runner.js +0 -28
- package/lib/esm/utils/constants.d.ts +0 -1
- package/lib/esm/utils/constants.js +0 -1
- package/lib/esm/utils/fetch-open-api.d.ts +0 -2
- package/lib/esm/utils/fetch-open-api.js +0 -28
- package/lib/esm/utils/fetch.d.ts +0 -1
- package/lib/esm/utils/fetch.js +0 -18
- package/lib/esm/utils/index.d.ts +0 -12
- package/lib/esm/utils/index.js +0 -12
- package/lib/esm/utils/is-non-nullable.d.ts +0 -2
- package/lib/esm/utils/is-non-nullable.js +0 -13
- package/lib/esm/utils/message-utils.d.ts +0 -20
- package/lib/esm/utils/message-utils.js +0 -71
- package/lib/esm/utils/mustache-utils.d.ts +0 -3
- package/lib/esm/utils/mustache-utils.js +0 -8
- package/lib/esm/utils/nullable.d.ts +0 -7
- package/lib/esm/utils/nullable.js +0 -1
- package/lib/esm/utils/omit.d.ts +0 -1
- package/lib/esm/utils/omit.js +0 -1
- package/lib/esm/utils/open-api-parameter.d.ts +0 -7
- package/lib/esm/utils/open-api-parameter.js +0 -78
- package/lib/esm/utils/ordered-map.d.ts +0 -37
- package/lib/esm/utils/ordered-map.js +0 -100
- package/lib/esm/utils/partial.d.ts +0 -3
- package/lib/esm/utils/partial.js +0 -1
- package/lib/esm/utils/runnable-type.d.ts +0 -5
- package/lib/esm/utils/runnable-type.js +0 -1
- package/lib/esm/utils/stream-utils.d.ts +0 -20
- package/lib/esm/utils/stream-utils.js +0 -79
- package/lib/esm/utils/structured-output-schema.d.ts +0 -3
- package/lib/esm/utils/structured-output-schema.js +0 -37
- package/lib/esm/utils/union.d.ts +0 -1
- package/lib/esm/utils/union.js +0 -1
- /package/lib/cjs/{context.js → execution-engine/context.js} +0 -0
- /package/lib/esm/{context.js → execution-engine/context.js} +0 -0
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
-
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;
|
|
6
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
-
};
|
|
8
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
-
};
|
|
11
|
-
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
-
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
-
};
|
|
14
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.LLMDecisionAgent = void 0;
|
|
16
|
-
const nanoid_1 = require("nanoid");
|
|
17
|
-
const tsyringe_1 = require("tsyringe");
|
|
18
|
-
const agent_1 = require("./agent");
|
|
19
|
-
const constants_1 = require("./constants");
|
|
20
|
-
const memory_1 = require("./definitions/memory");
|
|
21
|
-
const utils_1 = require("./utils");
|
|
22
|
-
const message_utils_1 = require("./utils/message-utils");
|
|
23
|
-
const structured_output_schema_1 = require("./utils/structured-output-schema");
|
|
24
|
-
let LLMDecisionAgent = class LLMDecisionAgent extends agent_1.Agent {
|
|
25
|
-
definition;
|
|
26
|
-
model;
|
|
27
|
-
static create = create;
|
|
28
|
-
constructor(definition, context, model) {
|
|
29
|
-
super(definition, context);
|
|
30
|
-
this.definition = definition;
|
|
31
|
-
this.model = model;
|
|
32
|
-
this.model ??= context?.resolveDependency(constants_1.TYPES.llmModel);
|
|
33
|
-
}
|
|
34
|
-
async process(input, options) {
|
|
35
|
-
const { definition, context, model } = this;
|
|
36
|
-
if (!model)
|
|
37
|
-
throw new Error("LLM model is required");
|
|
38
|
-
if (!context)
|
|
39
|
-
throw new Error("Context is required");
|
|
40
|
-
const { originalMessages, messagesWithMemory } = (0, message_utils_1.prepareMessages)(definition, input, options.memories);
|
|
41
|
-
const cases = await Promise.all(utils_1.OrderedRecord.map(definition.cases, async (t) => {
|
|
42
|
-
if (!t.runnable?.id)
|
|
43
|
-
throw new Error("Runnable is required");
|
|
44
|
-
const runnable = await context.resolve(t.runnable.id);
|
|
45
|
-
// TODO: auto generate name by llm model if needed
|
|
46
|
-
const name = t.name || runnable.name;
|
|
47
|
-
if (!name)
|
|
48
|
-
throw new Error("Case name is required");
|
|
49
|
-
return { ...t, name, runnable };
|
|
50
|
-
}));
|
|
51
|
-
const llmInputs = {
|
|
52
|
-
messages: messagesWithMemory,
|
|
53
|
-
modelOptions: definition.modelOptions,
|
|
54
|
-
tools: cases.map((t) => {
|
|
55
|
-
// Filter inputs that are bound from AI
|
|
56
|
-
const inputsFromAI = utils_1.OrderedRecord.fromArray(utils_1.OrderedRecord.filter(t.runnable.definition.inputs, (i) => t.input?.[i.id]?.from === "ai"));
|
|
57
|
-
const parameters = inputsFromAI.$indexes.length > 0
|
|
58
|
-
? (0, structured_output_schema_1.outputsToJsonSchema)(inputsFromAI)
|
|
59
|
-
: {};
|
|
60
|
-
return {
|
|
61
|
-
type: "function",
|
|
62
|
-
function: {
|
|
63
|
-
name: t.name,
|
|
64
|
-
description: t.description,
|
|
65
|
-
parameters,
|
|
66
|
-
},
|
|
67
|
-
};
|
|
68
|
-
}),
|
|
69
|
-
toolChoice: "required",
|
|
70
|
-
};
|
|
71
|
-
const { toolCalls } = await model.run(llmInputs);
|
|
72
|
-
// TODO: support run multiple calls
|
|
73
|
-
const { name: functionNameToCall, arguments: args } = toolCalls?.[0]?.function ?? {};
|
|
74
|
-
if (!functionNameToCall)
|
|
75
|
-
throw new Error("No any runnable called");
|
|
76
|
-
const caseToCall = cases.find((i) => i.name === functionNameToCall);
|
|
77
|
-
if (!caseToCall)
|
|
78
|
-
throw new Error("Case not found");
|
|
79
|
-
// Prepare arguments generated by LLM model
|
|
80
|
-
const llmArgs = args ? JSON.parse(args) : {};
|
|
81
|
-
// TODO: check result structure and omit undefined values
|
|
82
|
-
const output = await caseToCall.runnable.run({ ...input, ...llmArgs }, { stream: true });
|
|
83
|
-
return (0, utils_1.extractOutputsFromRunnableOutput)(output, ({ $text, ...json }) => this.updateMemories([
|
|
84
|
-
...originalMessages,
|
|
85
|
-
{
|
|
86
|
-
role: "assistant",
|
|
87
|
-
content: (0, utils_1.renderMessage)("{{$text}}\n{{json}}", { $text, json }).trim(),
|
|
88
|
-
},
|
|
89
|
-
]));
|
|
90
|
-
}
|
|
91
|
-
};
|
|
92
|
-
exports.LLMDecisionAgent = LLMDecisionAgent;
|
|
93
|
-
exports.LLMDecisionAgent = LLMDecisionAgent = __decorate([
|
|
94
|
-
(0, tsyringe_1.injectable)(),
|
|
95
|
-
__param(0, (0, tsyringe_1.inject)(constants_1.TYPES.definition)),
|
|
96
|
-
__param(1, (0, tsyringe_1.inject)(constants_1.TYPES.context)),
|
|
97
|
-
__param(2, (0, tsyringe_1.inject)(constants_1.TYPES.llmModel)),
|
|
98
|
-
__metadata("design:paramtypes", [Object, Object, Function])
|
|
99
|
-
], LLMDecisionAgent);
|
|
100
|
-
function create(options) {
|
|
101
|
-
const agentId = options.name || (0, nanoid_1.nanoid)();
|
|
102
|
-
const cases = utils_1.OrderedRecord.fromArray(Object.entries(options.cases).map(([name, c]) => {
|
|
103
|
-
const bindInputs = Object.entries(c.input ?? {});
|
|
104
|
-
return {
|
|
105
|
-
id: (0, nanoid_1.nanoid)(),
|
|
106
|
-
name: name || c.runnable.name,
|
|
107
|
-
description: c.description,
|
|
108
|
-
runnable: { id: c.runnable.id },
|
|
109
|
-
input: Object.fromEntries(bindInputs.map(([inputName, v]) => {
|
|
110
|
-
const input = c.runnable.definition.inputs[inputName] ||
|
|
111
|
-
utils_1.OrderedRecord.find(c.runnable.definition.inputs, (i) => i.name === inputName);
|
|
112
|
-
if (!input)
|
|
113
|
-
throw new Error(`Input ${inputName} not found`);
|
|
114
|
-
return [input.id, v];
|
|
115
|
-
})),
|
|
116
|
-
};
|
|
117
|
-
}));
|
|
118
|
-
const inputs = utils_1.OrderedRecord.merge(...Object.values(options.cases).map((i) => i.runnable.definition.inputs));
|
|
119
|
-
const outputs = utils_1.OrderedRecord.fromArray(utils_1.OrderedRecord.map(utils_1.OrderedRecord.merge(...Object.values(options.cases).map((i) => i.runnable.definition.outputs)), (o) => ({ ...o, required: false })));
|
|
120
|
-
const memories = (0, memory_1.toRunnableMemories)(agentId, inputs, options.memories ?? {});
|
|
121
|
-
const primaryMemoryNames = Object.entries(options.memories ?? {})
|
|
122
|
-
.filter(([, i]) => i.primary)
|
|
123
|
-
.map(([name]) => name);
|
|
124
|
-
if (primaryMemoryNames && primaryMemoryNames.length > 1) {
|
|
125
|
-
throw new Error("Only one primary memory is allowed");
|
|
126
|
-
}
|
|
127
|
-
const messages = utils_1.OrderedRecord.fromArray(options.messages?.map((i) => ({
|
|
128
|
-
id: (0, nanoid_1.nanoid)(),
|
|
129
|
-
role: i.role,
|
|
130
|
-
content: i.content,
|
|
131
|
-
})));
|
|
132
|
-
return new LLMDecisionAgent({
|
|
133
|
-
id: agentId,
|
|
134
|
-
name: options.name,
|
|
135
|
-
type: "llm_decision_agent",
|
|
136
|
-
inputs,
|
|
137
|
-
outputs,
|
|
138
|
-
messages,
|
|
139
|
-
primaryMemoryId: primaryMemoryNames?.at(0),
|
|
140
|
-
memories,
|
|
141
|
-
modelOptions: options.modelOptions,
|
|
142
|
-
cases,
|
|
143
|
-
}, options.context);
|
|
144
|
-
}
|
package/lib/cjs/llm-model.d.ts
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
import { Agent } from "./agent";
|
|
2
|
-
import type { Context } from "./context";
|
|
3
|
-
export type Role = "system" | "user" | "assistant" | "tool";
|
|
4
|
-
export type LLMModelInputs = {
|
|
5
|
-
messages: LLMModelInputMessage[];
|
|
6
|
-
responseFormat?: {
|
|
7
|
-
type: "text";
|
|
8
|
-
} | {
|
|
9
|
-
type: "json_schema";
|
|
10
|
-
jsonSchema: {
|
|
11
|
-
name: string;
|
|
12
|
-
description?: string;
|
|
13
|
-
schema: object;
|
|
14
|
-
strict?: boolean;
|
|
15
|
-
};
|
|
16
|
-
};
|
|
17
|
-
tools?: LLMModelInputTool[];
|
|
18
|
-
toolChoice?: "auto" | "none" | "required" | {
|
|
19
|
-
type: "function";
|
|
20
|
-
function: {
|
|
21
|
-
name: string;
|
|
22
|
-
description?: string;
|
|
23
|
-
};
|
|
24
|
-
};
|
|
25
|
-
modelOptions?: LLMModelOptions;
|
|
26
|
-
};
|
|
27
|
-
export interface LLMModelInputMessage {
|
|
28
|
-
role: Role;
|
|
29
|
-
content?: string | ({
|
|
30
|
-
type: "text";
|
|
31
|
-
text: string;
|
|
32
|
-
} | {
|
|
33
|
-
type: "image_url";
|
|
34
|
-
imageUrl: {
|
|
35
|
-
url: string;
|
|
36
|
-
};
|
|
37
|
-
})[];
|
|
38
|
-
toolCalls?: {
|
|
39
|
-
id: string;
|
|
40
|
-
type: "function";
|
|
41
|
-
function: {
|
|
42
|
-
name: string;
|
|
43
|
-
arguments: string;
|
|
44
|
-
};
|
|
45
|
-
}[];
|
|
46
|
-
toolCallId?: string;
|
|
47
|
-
}
|
|
48
|
-
export interface LLMModelInputTool {
|
|
49
|
-
type: "function";
|
|
50
|
-
function: {
|
|
51
|
-
name: string;
|
|
52
|
-
description?: string;
|
|
53
|
-
parameters: object;
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
export interface LLMModelOptions {
|
|
57
|
-
model?: string;
|
|
58
|
-
temperature?: number;
|
|
59
|
-
topP?: number;
|
|
60
|
-
frequencyPenalty?: number;
|
|
61
|
-
presencePenalty?: number;
|
|
62
|
-
}
|
|
63
|
-
export type LLMModelOutputs = {
|
|
64
|
-
$text?: string | null;
|
|
65
|
-
toolCalls?: {
|
|
66
|
-
id?: string;
|
|
67
|
-
type?: "function";
|
|
68
|
-
function?: {
|
|
69
|
-
name?: string;
|
|
70
|
-
arguments?: string;
|
|
71
|
-
};
|
|
72
|
-
}[];
|
|
73
|
-
};
|
|
74
|
-
export declare abstract class LLMModel extends Agent<LLMModelInputs, LLMModelOutputs> {
|
|
75
|
-
constructor(context?: Context);
|
|
76
|
-
}
|
|
77
|
-
export interface LLMModelConfiguration {
|
|
78
|
-
default?: Partial<LLMModelOptions>;
|
|
79
|
-
override?: Partial<LLMModelOptions>;
|
|
80
|
-
}
|
package/lib/cjs/llm-model.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.LLMModel = void 0;
|
|
4
|
-
const agent_1 = require("./agent");
|
|
5
|
-
const utils_1 = require("./utils");
|
|
6
|
-
class LLMModel extends agent_1.Agent {
|
|
7
|
-
constructor(context) {
|
|
8
|
-
super({
|
|
9
|
-
id: "llm_model",
|
|
10
|
-
type: "llm_model",
|
|
11
|
-
name: "LLM Model",
|
|
12
|
-
description: "Run a LLM model",
|
|
13
|
-
inputs: utils_1.OrderedRecord.fromArray([
|
|
14
|
-
{ id: "messages", name: "messages", type: "array", required: true },
|
|
15
|
-
{ id: "responseFormat", name: "responseFormat", type: "object" },
|
|
16
|
-
{ id: "tools", name: "tools", type: "array" },
|
|
17
|
-
{ id: "toolChoice", name: "toolChoice", type: "object" },
|
|
18
|
-
{ id: "modelOptions", name: "modelOptions", type: "object" },
|
|
19
|
-
]),
|
|
20
|
-
outputs: utils_1.OrderedRecord.fromArray([
|
|
21
|
-
{ id: "$text", name: "$text", type: "string" },
|
|
22
|
-
{ id: "toolCalls", name: "toolCalls", type: "object" },
|
|
23
|
-
]),
|
|
24
|
-
}, context);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
exports.LLMModel = LLMModel;
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { LLMModel, type LLMModelInputs } from "../llm-model";
|
|
2
|
-
export declare class GeminiLLMModel extends LLMModel {
|
|
3
|
-
private config;
|
|
4
|
-
constructor(config: {
|
|
5
|
-
apiKey: string;
|
|
6
|
-
model: string;
|
|
7
|
-
});
|
|
8
|
-
private client;
|
|
9
|
-
private model;
|
|
10
|
-
setApiKey(apiKey: string): void;
|
|
11
|
-
process(input: LLMModelInputs): AsyncGenerator<{
|
|
12
|
-
$text: string;
|
|
13
|
-
delta: {
|
|
14
|
-
toolCalls: {
|
|
15
|
-
id?: string;
|
|
16
|
-
type?: "function";
|
|
17
|
-
function?: {
|
|
18
|
-
name?: string;
|
|
19
|
-
arguments?: string;
|
|
20
|
-
};
|
|
21
|
-
}[];
|
|
22
|
-
};
|
|
23
|
-
}, void, unknown>;
|
|
24
|
-
}
|
|
@@ -1,199 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.GeminiLLMModel = void 0;
|
|
4
|
-
const generative_ai_1 = require("@google/generative-ai");
|
|
5
|
-
const nanoid_1 = require("nanoid");
|
|
6
|
-
const llm_model_1 = require("../llm-model");
|
|
7
|
-
const is_non_nullable_1 = require("../utils/is-non-nullable");
|
|
8
|
-
class GeminiLLMModel extends llm_model_1.LLMModel {
|
|
9
|
-
config;
|
|
10
|
-
constructor(config) {
|
|
11
|
-
super();
|
|
12
|
-
this.config = config;
|
|
13
|
-
this.client = new generative_ai_1.GoogleGenerativeAI(this.config.apiKey);
|
|
14
|
-
this.model = this.client.getGenerativeModel({ model: this.config.model });
|
|
15
|
-
}
|
|
16
|
-
client;
|
|
17
|
-
model;
|
|
18
|
-
setApiKey(apiKey) {
|
|
19
|
-
this.client = new generative_ai_1.GoogleGenerativeAI(apiKey);
|
|
20
|
-
this.model = this.client.getGenerativeModel({ model: this.config.model });
|
|
21
|
-
}
|
|
22
|
-
async *process(input) {
|
|
23
|
-
const res = await this.model.generateContentStream({
|
|
24
|
-
contents: await contentsFromInputMessages(input.messages),
|
|
25
|
-
tools: toolsFromInputTools(input.tools),
|
|
26
|
-
toolConfig: toolConfigFromInputToolChoice(input.toolChoice),
|
|
27
|
-
generationConfig: generationConfigFromInput(input),
|
|
28
|
-
});
|
|
29
|
-
const toolCalls = [];
|
|
30
|
-
for await (const chunk of res.stream) {
|
|
31
|
-
const choice = chunk.candidates?.[0];
|
|
32
|
-
if (choice?.content.parts) {
|
|
33
|
-
const calls = choice.content.parts
|
|
34
|
-
.filter((i) => typeof i.functionCall === "object")
|
|
35
|
-
.map((i) => ({
|
|
36
|
-
id: (0, nanoid_1.nanoid)(),
|
|
37
|
-
type: "function",
|
|
38
|
-
function: {
|
|
39
|
-
name: i.functionCall.name,
|
|
40
|
-
arguments: JSON.stringify(i.functionCall.args),
|
|
41
|
-
},
|
|
42
|
-
}));
|
|
43
|
-
if (calls.length) {
|
|
44
|
-
toolCalls.push(...calls);
|
|
45
|
-
}
|
|
46
|
-
yield {
|
|
47
|
-
$text: choice.content.parts
|
|
48
|
-
.map((i) => i.text)
|
|
49
|
-
.filter(Boolean)
|
|
50
|
-
.join(""),
|
|
51
|
-
delta: { toolCalls },
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
else if (chunk.promptFeedback?.blockReason) {
|
|
55
|
-
const { blockReason, blockReasonMessage } = chunk.promptFeedback;
|
|
56
|
-
throw new Error(["PROMPT_BLOCKED", blockReason, blockReasonMessage]
|
|
57
|
-
.filter(Boolean)
|
|
58
|
-
.join(" "));
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
exports.GeminiLLMModel = GeminiLLMModel;
|
|
64
|
-
async function contentsFromInputMessages(messages) {
|
|
65
|
-
const contents = [];
|
|
66
|
-
let prevMsg;
|
|
67
|
-
while (messages.length) {
|
|
68
|
-
const message = messages.shift();
|
|
69
|
-
if (!prevMsg || message.role !== prevMsg.role) {
|
|
70
|
-
prevMsg = {
|
|
71
|
-
role: message.role === "assistant" ? "model" : "user",
|
|
72
|
-
parts: [],
|
|
73
|
-
};
|
|
74
|
-
contents.push(prevMsg);
|
|
75
|
-
}
|
|
76
|
-
if (typeof message.content === "string") {
|
|
77
|
-
prevMsg.parts.push({ text: message.content });
|
|
78
|
-
}
|
|
79
|
-
else if (Array.isArray(message.content)) {
|
|
80
|
-
const res = await Promise.all(message.content.map(resolveContent));
|
|
81
|
-
prevMsg.parts.push(...res.filter(is_non_nullable_1.isNonNullable));
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
return contents;
|
|
85
|
-
}
|
|
86
|
-
async function resolveContent(content) {
|
|
87
|
-
if (typeof content === "string")
|
|
88
|
-
return { text: content };
|
|
89
|
-
if (content.type === "text" && content.text) {
|
|
90
|
-
return { text: content.text };
|
|
91
|
-
}
|
|
92
|
-
if (content.type === "image_url") {
|
|
93
|
-
const url = content.imageUrl.url;
|
|
94
|
-
return { fileData: { mimeType: "image/jpeg", fileUri: url } };
|
|
95
|
-
}
|
|
96
|
-
return undefined;
|
|
97
|
-
}
|
|
98
|
-
function parameterSchemaToFunctionDeclarationSchema(schema) {
|
|
99
|
-
if (schema.type === "object") {
|
|
100
|
-
return {
|
|
101
|
-
type: generative_ai_1.SchemaType.OBJECT,
|
|
102
|
-
description: schema.description,
|
|
103
|
-
properties: Object.fromEntries(Object.entries(schema.properties).map(([key, s]) => [
|
|
104
|
-
key,
|
|
105
|
-
openAISchemaToGeminiSchema(s),
|
|
106
|
-
])),
|
|
107
|
-
required: schema.required,
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
throw new Error(`Unsupported schema type ${schema.type}`);
|
|
111
|
-
}
|
|
112
|
-
function generationConfigFromInput(input) {
|
|
113
|
-
const jsonSchema = input.responseFormat?.type === "json_schema"
|
|
114
|
-
? input.responseFormat.jsonSchema
|
|
115
|
-
: undefined;
|
|
116
|
-
return {
|
|
117
|
-
temperature: input.modelOptions?.temperature,
|
|
118
|
-
topP: input.modelOptions?.topP,
|
|
119
|
-
frequencyPenalty: input.modelOptions?.frequencyPenalty,
|
|
120
|
-
presencePenalty: input.modelOptions?.presencePenalty,
|
|
121
|
-
responseMimeType: jsonSchema ? "application/json" : undefined,
|
|
122
|
-
responseSchema: jsonSchema
|
|
123
|
-
? openAISchemaToGeminiSchema(jsonSchema)
|
|
124
|
-
: undefined,
|
|
125
|
-
};
|
|
126
|
-
}
|
|
127
|
-
function toolConfigFromInputToolChoice(toolChoice) {
|
|
128
|
-
if (!toolChoice)
|
|
129
|
-
return undefined;
|
|
130
|
-
const selectedToolFunctionName = typeof toolChoice === "object" ? toolChoice.function.name : undefined;
|
|
131
|
-
return !toolChoice
|
|
132
|
-
? undefined
|
|
133
|
-
: {
|
|
134
|
-
functionCallingConfig: {
|
|
135
|
-
mode: toolChoice === "required" || selectedToolFunctionName
|
|
136
|
-
? generative_ai_1.FunctionCallingMode.ANY
|
|
137
|
-
: toolChoice === "none"
|
|
138
|
-
? generative_ai_1.FunctionCallingMode.NONE
|
|
139
|
-
: generative_ai_1.FunctionCallingMode.AUTO,
|
|
140
|
-
allowedFunctionNames: selectedToolFunctionName
|
|
141
|
-
? [selectedToolFunctionName]
|
|
142
|
-
: undefined,
|
|
143
|
-
},
|
|
144
|
-
};
|
|
145
|
-
}
|
|
146
|
-
function toolsFromInputTools(tools) {
|
|
147
|
-
return tools?.length
|
|
148
|
-
? [
|
|
149
|
-
{
|
|
150
|
-
functionDeclarations: tools.map((i) => ({
|
|
151
|
-
name: i.function.name,
|
|
152
|
-
description: i.function.description,
|
|
153
|
-
parameters: !i.function.parameters ||
|
|
154
|
-
Object.keys(i.function.parameters).length === 0
|
|
155
|
-
? undefined
|
|
156
|
-
: parameterSchemaToFunctionDeclarationSchema(i.function.parameters),
|
|
157
|
-
})),
|
|
158
|
-
},
|
|
159
|
-
]
|
|
160
|
-
: undefined;
|
|
161
|
-
}
|
|
162
|
-
function openAISchemaToGeminiSchema(schema) {
|
|
163
|
-
if (schema.type === "string") {
|
|
164
|
-
return {
|
|
165
|
-
type: generative_ai_1.SchemaType.STRING,
|
|
166
|
-
description: schema.description,
|
|
167
|
-
};
|
|
168
|
-
}
|
|
169
|
-
if (schema.type === "number") {
|
|
170
|
-
return {
|
|
171
|
-
type: generative_ai_1.SchemaType.NUMBER,
|
|
172
|
-
description: schema.description,
|
|
173
|
-
};
|
|
174
|
-
}
|
|
175
|
-
if (schema.type === "boolean") {
|
|
176
|
-
return {
|
|
177
|
-
type: generative_ai_1.SchemaType.BOOLEAN,
|
|
178
|
-
description: schema.description,
|
|
179
|
-
};
|
|
180
|
-
}
|
|
181
|
-
if (schema.type === "object") {
|
|
182
|
-
return {
|
|
183
|
-
type: generative_ai_1.SchemaType.OBJECT,
|
|
184
|
-
description: schema.description,
|
|
185
|
-
properties: Object.fromEntries(Object.entries(schema.properties).map(([key, s]) => [
|
|
186
|
-
key,
|
|
187
|
-
openAISchemaToGeminiSchema(s),
|
|
188
|
-
])),
|
|
189
|
-
required: schema.required,
|
|
190
|
-
};
|
|
191
|
-
}
|
|
192
|
-
if (schema.type === "array") {
|
|
193
|
-
return {
|
|
194
|
-
type: generative_ai_1.SchemaType.ARRAY,
|
|
195
|
-
items: openAISchemaToGeminiSchema(schema.items),
|
|
196
|
-
};
|
|
197
|
-
}
|
|
198
|
-
throw new Error(`Unsupported schema type ${schema.type}`);
|
|
199
|
-
}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { LLMModel, type LLMModelInputs } from "../llm-model";
|
|
2
|
-
export declare class OpenaiLLMModel extends LLMModel {
|
|
3
|
-
private config;
|
|
4
|
-
constructor(config: {
|
|
5
|
-
apiKey: string;
|
|
6
|
-
model: string;
|
|
7
|
-
});
|
|
8
|
-
private client;
|
|
9
|
-
setApiKey(apiKey: string): void;
|
|
10
|
-
process(input: LLMModelInputs): AsyncGenerator<{
|
|
11
|
-
$text: string;
|
|
12
|
-
delta?: undefined;
|
|
13
|
-
} | {
|
|
14
|
-
delta: {
|
|
15
|
-
toolCalls: {
|
|
16
|
-
id?: string;
|
|
17
|
-
type?: "function";
|
|
18
|
-
function?: {
|
|
19
|
-
name?: string;
|
|
20
|
-
arguments?: string;
|
|
21
|
-
};
|
|
22
|
-
}[];
|
|
23
|
-
};
|
|
24
|
-
$text?: undefined;
|
|
25
|
-
}, void, unknown>;
|
|
26
|
-
}
|