@aigne/core 1.19.0 → 1.20.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/CHANGELOG.md +21 -0
- package/lib/cjs/agents/agent.d.ts +11 -6
- package/lib/cjs/agents/agent.js +8 -10
- package/lib/cjs/agents/ai-agent.d.ts +18 -6
- package/lib/cjs/agents/ai-agent.js +14 -4
- package/lib/cjs/agents/guide-rail-agent.d.ts +1 -1
- package/lib/cjs/agents/mcp-agent.d.ts +1 -1
- package/lib/cjs/aigne/aigne.d.ts +10 -10
- package/lib/cjs/aigne/context.d.ts +8 -6
- package/lib/cjs/aigne/context.js +62 -42
- package/lib/cjs/aigne/message-queue.d.ts +1 -1
- package/lib/cjs/aigne/message-queue.js +2 -3
- package/lib/cjs/aigne/usage.d.ts +1 -0
- package/lib/cjs/aigne/usage.js +1 -0
- package/lib/cjs/loader/agent-yaml.d.ts +2 -1
- package/lib/cjs/loader/agent-yaml.js +4 -0
- package/lib/cjs/prompt/prompt-builder.d.ts +2 -16
- package/lib/cjs/prompt/prompt-builder.js +12 -25
- package/lib/cjs/utils/stream-utils.d.ts +1 -2
- package/lib/cjs/utils/type-utils.d.ts +1 -0
- package/lib/cjs/utils/type-utils.js +13 -0
- package/lib/dts/agents/agent.d.ts +11 -6
- package/lib/dts/agents/ai-agent.d.ts +18 -6
- package/lib/dts/agents/guide-rail-agent.d.ts +1 -1
- package/lib/dts/agents/mcp-agent.d.ts +1 -1
- package/lib/dts/aigne/aigne.d.ts +10 -10
- package/lib/dts/aigne/context.d.ts +8 -6
- package/lib/dts/aigne/message-queue.d.ts +1 -1
- package/lib/dts/aigne/usage.d.ts +1 -0
- package/lib/dts/loader/agent-yaml.d.ts +2 -1
- package/lib/dts/prompt/prompt-builder.d.ts +2 -16
- package/lib/dts/utils/stream-utils.d.ts +1 -2
- package/lib/dts/utils/type-utils.d.ts +1 -0
- package/lib/esm/agents/agent.d.ts +11 -6
- package/lib/esm/agents/agent.js +8 -10
- package/lib/esm/agents/ai-agent.d.ts +18 -6
- package/lib/esm/agents/ai-agent.js +15 -5
- package/lib/esm/agents/guide-rail-agent.d.ts +1 -1
- package/lib/esm/agents/mcp-agent.d.ts +1 -1
- package/lib/esm/aigne/aigne.d.ts +10 -10
- package/lib/esm/aigne/context.d.ts +8 -6
- package/lib/esm/aigne/context.js +63 -43
- package/lib/esm/aigne/message-queue.d.ts +1 -1
- package/lib/esm/aigne/message-queue.js +2 -3
- package/lib/esm/aigne/usage.d.ts +1 -0
- package/lib/esm/aigne/usage.js +1 -0
- package/lib/esm/loader/agent-yaml.d.ts +2 -1
- package/lib/esm/loader/agent-yaml.js +4 -0
- package/lib/esm/prompt/prompt-builder.d.ts +2 -16
- package/lib/esm/prompt/prompt-builder.js +12 -23
- package/lib/esm/utils/stream-utils.d.ts +1 -2
- package/lib/esm/utils/type-utils.d.ts +1 -0
- package/lib/esm/utils/type-utils.js +12 -0
- package/package.json +2 -2
|
@@ -5,11 +5,11 @@ export declare function loadAgentFromYamlFile(path: string): Promise<{
|
|
|
5
5
|
name: string;
|
|
6
6
|
description?: string | undefined;
|
|
7
7
|
skills?: string[] | undefined;
|
|
8
|
-
instructions?: string | undefined;
|
|
9
8
|
memory?: true | {
|
|
10
9
|
provider: string;
|
|
11
10
|
subscribeTopic?: string[] | undefined;
|
|
12
11
|
} | undefined;
|
|
12
|
+
instructions?: string | undefined;
|
|
13
13
|
inputSchema?: ZodObject<Record<string, ZodType<any, z.ZodTypeDef, any>>, z.UnknownKeysParam, z.ZodTypeAny, {
|
|
14
14
|
[x: string]: any;
|
|
15
15
|
}, {
|
|
@@ -20,6 +20,7 @@ export declare function loadAgentFromYamlFile(path: string): Promise<{
|
|
|
20
20
|
}, {
|
|
21
21
|
[x: string]: any;
|
|
22
22
|
}> | undefined;
|
|
23
|
+
inputKey?: string | undefined;
|
|
23
24
|
outputKey?: string | undefined;
|
|
24
25
|
toolChoice?: AIAgentToolChoice | undefined;
|
|
25
26
|
} | {
|
|
@@ -18,6 +18,10 @@ const agentFileSchema = z.discriminatedUnion("type", [
|
|
|
18
18
|
.string()
|
|
19
19
|
.nullish()
|
|
20
20
|
.transform((v) => v ?? undefined),
|
|
21
|
+
input_key: z
|
|
22
|
+
.string()
|
|
23
|
+
.nullish()
|
|
24
|
+
.transform((v) => v ?? undefined),
|
|
21
25
|
input_schema: inputOutputSchema
|
|
22
26
|
.nullish()
|
|
23
27
|
.transform((v) => (v ? jsonSchemaToZod(v) : undefined)),
|
|
@@ -3,33 +3,19 @@ import { Agent, type AgentInvokeOptions, type Message } from "../agents/agent.js
|
|
|
3
3
|
import type { AIAgent } from "../agents/ai-agent.js";
|
|
4
4
|
import type { ChatModel, ChatModelInput } from "../agents/chat-model.js";
|
|
5
5
|
import { ChatMessagesTemplate } from "./template.js";
|
|
6
|
-
export declare const MESSAGE_KEY = "$message";
|
|
7
|
-
export declare function createMessage<V extends Message>(message: string, variables?: V): {
|
|
8
|
-
[MESSAGE_KEY]: string;
|
|
9
|
-
} & typeof variables;
|
|
10
|
-
export declare function createMessage<I extends Message, V extends Message>(message: I, variables?: V): I & typeof variables;
|
|
11
|
-
export declare function createMessage<I extends Message, V extends Message>(message: string | I, variables?: V): ({
|
|
12
|
-
[MESSAGE_KEY]: string;
|
|
13
|
-
} | I) & typeof variables;
|
|
14
|
-
export declare function getMessage(input: Message): string | undefined;
|
|
15
6
|
export interface PromptBuilderOptions {
|
|
16
7
|
instructions?: string | ChatMessagesTemplate;
|
|
17
8
|
}
|
|
18
9
|
export interface PromptBuildOptions extends Pick<AgentInvokeOptions, "context"> {
|
|
19
|
-
agent?: AIAgent
|
|
10
|
+
agent?: AIAgent<any, any, any>;
|
|
20
11
|
input?: Message;
|
|
21
12
|
model?: ChatModel;
|
|
22
13
|
outputSchema?: Agent["outputSchema"];
|
|
23
14
|
}
|
|
24
15
|
export declare class PromptBuilder {
|
|
25
|
-
static from(instructions: string): PromptBuilder;
|
|
26
|
-
static from(instructions: GetPromptResult): PromptBuilder;
|
|
27
|
-
static from(instructions: {
|
|
28
|
-
path: string;
|
|
29
|
-
}): Promise<PromptBuilder>;
|
|
30
16
|
static from(instructions: string | {
|
|
31
17
|
path: string;
|
|
32
|
-
} | GetPromptResult): PromptBuilder
|
|
18
|
+
} | GetPromptResult): PromptBuilder;
|
|
33
19
|
private static fromFile;
|
|
34
20
|
private static fromMCPPromptResult;
|
|
35
21
|
constructor(options?: PromptBuilderOptions);
|
|
@@ -3,23 +3,9 @@ import { stringify } from "yaml";
|
|
|
3
3
|
import { ZodObject } from "zod";
|
|
4
4
|
import { Agent } from "../agents/agent.js";
|
|
5
5
|
import { outputSchemaToResponseFormatSchema } from "../utils/json-schema.js";
|
|
6
|
-
import {
|
|
6
|
+
import { unique } from "../utils/type-utils.js";
|
|
7
7
|
import { MEMORY_MESSAGE_TEMPLATE } from "./prompts/memory-message-template.js";
|
|
8
8
|
import { AgentMessageTemplate, ChatMessagesTemplate, PromptTemplate, SystemMessageTemplate, UserMessageTemplate, } from "./template.js";
|
|
9
|
-
export const MESSAGE_KEY = "$message";
|
|
10
|
-
export function createMessage(message, variables) {
|
|
11
|
-
return (typeof message === "string"
|
|
12
|
-
? { [MESSAGE_KEY]: message, ...variables }
|
|
13
|
-
: { ...message, ...variables });
|
|
14
|
-
}
|
|
15
|
-
export function getMessage(input) {
|
|
16
|
-
const userInputMessage = input[MESSAGE_KEY];
|
|
17
|
-
if (typeof userInputMessage === "string")
|
|
18
|
-
return userInputMessage;
|
|
19
|
-
if (!isNil(userInputMessage))
|
|
20
|
-
return JSON.stringify(userInputMessage);
|
|
21
|
-
return undefined;
|
|
22
|
-
}
|
|
23
9
|
export class PromptBuilder {
|
|
24
10
|
static from(instructions) {
|
|
25
11
|
if (typeof instructions === "string")
|
|
@@ -30,8 +16,8 @@ export class PromptBuilder {
|
|
|
30
16
|
return PromptBuilder.fromFile(instructions.path);
|
|
31
17
|
throw new Error(`Invalid instructions ${instructions}`);
|
|
32
18
|
}
|
|
33
|
-
static
|
|
34
|
-
const text =
|
|
19
|
+
static fromFile(path) {
|
|
20
|
+
const text = nodejs.fsSync.readFileSync(path, "utf-8");
|
|
35
21
|
return PromptBuilder.from(text);
|
|
36
22
|
}
|
|
37
23
|
static fromMCPPromptResult(result) {
|
|
@@ -75,22 +61,25 @@ export class PromptBuilder {
|
|
|
75
61
|
}
|
|
76
62
|
async buildMessages(options) {
|
|
77
63
|
const { input } = options;
|
|
64
|
+
const inputKey = options.agent?.inputKey;
|
|
65
|
+
const message = inputKey && typeof input?.[inputKey] === "string" ? input[inputKey] : undefined;
|
|
78
66
|
const messages = (typeof this.instructions === "string"
|
|
79
67
|
? ChatMessagesTemplate.from([SystemMessageTemplate.from(this.instructions)])
|
|
80
68
|
: this.instructions)?.format(options.input) ?? [];
|
|
81
69
|
const memories = [];
|
|
82
|
-
if (options.agent) {
|
|
83
|
-
memories.push(...(await options.agent.retrieveMemories({ search:
|
|
70
|
+
if (options.agent?.inputKey) {
|
|
71
|
+
memories.push(...(await options.agent.retrieveMemories({ search: message }, options)));
|
|
84
72
|
}
|
|
85
73
|
if (options.context.memories?.length) {
|
|
86
74
|
memories.push(...options.context.memories);
|
|
87
75
|
}
|
|
88
76
|
if (memories.length)
|
|
89
77
|
messages.push(...this.convertMemoriesToMessages(memories, options));
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
78
|
+
if (message) {
|
|
79
|
+
messages.push({
|
|
80
|
+
role: "user",
|
|
81
|
+
content: message,
|
|
82
|
+
});
|
|
94
83
|
}
|
|
95
84
|
return messages;
|
|
96
85
|
}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { type AgentProcessAsyncGenerator, type AgentResponseChunk, type AgentResponseStream, type Message } from "../agents/agent.js";
|
|
2
|
-
import type { MESSAGE_KEY } from "../prompt/prompt-builder.js";
|
|
3
2
|
import { type PromiseOrValue } from "./type-utils.js";
|
|
4
3
|
import "./stream-polyfill.js";
|
|
5
4
|
export declare function objectToAgentResponseStream<T extends Message>(json: T): AgentResponseStream<T>;
|
|
@@ -20,7 +19,7 @@ export declare function readableStreamToArray<T>(stream: ReadableStream<T>, opti
|
|
|
20
19
|
export declare function readableStreamToArray<T>(stream: ReadableStream<T>, options?: {
|
|
21
20
|
catchError?: boolean;
|
|
22
21
|
}): Promise<T[]>;
|
|
23
|
-
export declare function stringToAgentResponseStream(str: string, key?: "text" |
|
|
22
|
+
export declare function stringToAgentResponseStream(str: string, key?: "text" | string): AgentResponseStream<Message>;
|
|
24
23
|
export declare function toReadableStream(stream: NodeJS.ReadStream): ReadableStream<Uint8Array<ArrayBufferLike>>;
|
|
25
24
|
export declare function readAllString(stream: NodeJS.ReadStream | ReadableStream): Promise<string>;
|
|
26
25
|
export declare function mergeReadableStreams<T1, T2>(s1: ReadableStream<T1>, s2: ReadableStream<T2>): ReadableStream<T1 | T2>;
|
|
@@ -16,6 +16,7 @@ export declare function duplicates<T>(arr: T[], key?: (item: T) => unknown): T[]
|
|
|
16
16
|
export declare function remove<T>(arr: T[], remove: T[] | ((item: T) => boolean)): T[];
|
|
17
17
|
export declare function unique<T>(arr: T[], key?: (item: T) => unknown): T[];
|
|
18
18
|
export declare function omit<T extends Record<string, unknown>, K extends keyof T>(obj: T, ...keys: (K | K[])[]): Omit<T, K>;
|
|
19
|
+
export declare function omitDeep<T, K>(obj: T, ...keys: (K | K[])[]): unknown;
|
|
19
20
|
export declare function omitBy<T extends Record<string, unknown>, K extends keyof T>(obj: T, predicate: (value: T[K], key: K) => boolean): Partial<T>;
|
|
20
21
|
export declare function orArrayToArray<T>(value?: T | T[]): T[];
|
|
21
22
|
export declare function createAccessorArray<T>(array: T[], accessor: (array: T[], name: string) => T | undefined): T[] & {
|
|
@@ -62,6 +62,18 @@ export function omit(obj, ...keys) {
|
|
|
62
62
|
const flattenedKeys = new Set(keys.flat());
|
|
63
63
|
return Object.fromEntries(Object.entries(obj).filter(([key]) => !flattenedKeys.has(key)));
|
|
64
64
|
}
|
|
65
|
+
export function omitDeep(obj, ...keys) {
|
|
66
|
+
if (Array.isArray(obj)) {
|
|
67
|
+
return obj.map((item) => omitDeep(item, ...keys));
|
|
68
|
+
}
|
|
69
|
+
if (isRecord(obj)) {
|
|
70
|
+
const flattenedKeys = new Set(keys.flat());
|
|
71
|
+
return Object.fromEntries(Object.entries(obj)
|
|
72
|
+
.filter(([key]) => !flattenedKeys.has(key))
|
|
73
|
+
.map(([key, value]) => [key, omitDeep(value, ...keys)]));
|
|
74
|
+
}
|
|
75
|
+
return obj;
|
|
76
|
+
}
|
|
65
77
|
export function omitBy(obj, predicate) {
|
|
66
78
|
return Object.fromEntries(Object.entries(obj).filter(([key, value]) => {
|
|
67
79
|
const k = key;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aigne/core",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.20.1",
|
|
4
4
|
"description": "AIGNE core library for building AI-powered applications",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -81,7 +81,7 @@
|
|
|
81
81
|
"yaml": "^2.7.1",
|
|
82
82
|
"zod": "^3.24.4",
|
|
83
83
|
"zod-to-json-schema": "^3.24.5",
|
|
84
|
-
"@aigne/platform-helpers": "^0.1.
|
|
84
|
+
"@aigne/platform-helpers": "^0.1.2"
|
|
85
85
|
},
|
|
86
86
|
"devDependencies": {
|
|
87
87
|
"@types/bun": "^1.2.12",
|