@aigne/core 1.18.4 → 1.18.6
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 +15 -0
- package/lib/cjs/agents/agent.js +5 -0
- package/lib/cjs/aigne/context.d.ts +5 -0
- package/lib/cjs/aigne/context.js +22 -2
- package/lib/cjs/index.d.ts +1 -0
- package/lib/cjs/index.js +1 -0
- package/lib/cjs/prompt/prompt-builder.d.ts +1 -1
- package/lib/cjs/prompt/prompt-builder.js +3 -4
- package/lib/cjs/utils/stream-polyfill.d.ts +1 -0
- package/lib/cjs/utils/stream-polyfill.js +38 -0
- package/lib/cjs/utils/stream-utils.d.ts +1 -0
- package/lib/cjs/utils/stream-utils.js +1 -0
- package/lib/dts/aigne/context.d.ts +5 -0
- package/lib/dts/index.d.ts +1 -0
- package/lib/dts/prompt/prompt-builder.d.ts +1 -1
- package/lib/dts/utils/stream-polyfill.d.ts +1 -0
- package/lib/dts/utils/stream-utils.d.ts +1 -0
- package/lib/esm/agents/agent.js +5 -0
- package/lib/esm/aigne/context.d.ts +5 -0
- package/lib/esm/aigne/context.js +22 -2
- package/lib/esm/index.d.ts +1 -0
- package/lib/esm/index.js +1 -0
- package/lib/esm/prompt/prompt-builder.d.ts +1 -1
- package/lib/esm/prompt/prompt-builder.js +3 -4
- package/lib/esm/utils/stream-polyfill.d.ts +1 -0
- package/lib/esm/utils/stream-polyfill.js +37 -0
- package/lib/esm/utils/stream-utils.d.ts +1 -0
- package/lib/esm/utils/stream-utils.js +1 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,21 @@
|
|
|
5
5
|
|
|
6
6
|
* add user context support ([#131](https://github.com/AIGNE-io/aigne-framework/issues/131)) ([4dd9d20](https://github.com/AIGNE-io/aigne-framework/commit/4dd9d20953f6ac33933723db56efd9b44bafeb02))
|
|
7
7
|
|
|
8
|
+
## [1.18.6](https://github.com/AIGNE-io/aigne-framework/compare/core-v1.18.5...core-v1.18.6) (2025-06-11)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* **core:** add async generator polyfill for ReadableStream on safari ([#158](https://github.com/AIGNE-io/aigne-framework/issues/158)) ([70ef026](https://github.com/AIGNE-io/aigne-framework/commit/70ef026f413726c369f6a0781efc7f0333735406))
|
|
14
|
+
* **core:** exclude nested skills from final tool list in invokable skill ([#156](https://github.com/AIGNE-io/aigne-framework/issues/156)) ([91645f1](https://github.com/AIGNE-io/aigne-framework/commit/91645f12e79110a00f8f2db8ebc19401ddbd5a80))
|
|
15
|
+
|
|
16
|
+
## [1.18.5](https://github.com/AIGNE-io/aigne-framework/compare/core-v1.18.4...core-v1.18.5) (2025-06-06)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
### Bug Fixes
|
|
20
|
+
|
|
21
|
+
* **core:** should pass memories from invocation options to nested agents ([#153](https://github.com/AIGNE-io/aigne-framework/issues/153)) ([57629a5](https://github.com/AIGNE-io/aigne-framework/commit/57629a5da6cf2a295356dfe32ecbb15154e098fe))
|
|
22
|
+
|
|
8
23
|
## [1.18.4](https://github.com/AIGNE-io/aigne-framework/compare/core-v1.18.3...core-v1.18.4) (2025-06-05)
|
|
9
24
|
|
|
10
25
|
|
package/lib/cjs/agents/agent.js
CHANGED
|
@@ -336,6 +336,11 @@ class Agent {
|
|
|
336
336
|
};
|
|
337
337
|
if (options.userContext) {
|
|
338
338
|
Object.assign(opts.context.userContext, options.userContext);
|
|
339
|
+
options.userContext = undefined;
|
|
340
|
+
}
|
|
341
|
+
if (options.memories?.length) {
|
|
342
|
+
opts.context.memories.push(...options.memories);
|
|
343
|
+
options.memories = undefined;
|
|
339
344
|
}
|
|
340
345
|
const message = typeof input === "string" ? (0, prompt_builder_js_1.createMessage)(input) : input;
|
|
341
346
|
logger_js_1.logger.debug("Invoke agent %s started with input: %O", this.name, input);
|
|
@@ -2,6 +2,7 @@ import { Emitter } from "strict-event-emitter";
|
|
|
2
2
|
import { Agent, type AgentInvokeOptions, type AgentProcessAsyncGenerator, type AgentResponse, type AgentResponseStream, type Message } from "../agents/agent.js";
|
|
3
3
|
import type { ChatModel } from "../agents/chat-model.js";
|
|
4
4
|
import { UserAgent } from "../agents/user-agent.js";
|
|
5
|
+
import type { Memory } from "../memory/memory.js";
|
|
5
6
|
import { type OmitPropertiesFromArrayFirstElement } from "../utils/type-utils.js";
|
|
6
7
|
import type { Args, Listener, TypedEventEmitter } from "../utils/typed-event-emtter.js";
|
|
7
8
|
import { type MessagePayload, MessageQueue, type MessageQueueListener, type Unsubscribe } from "./message-queue.js";
|
|
@@ -58,6 +59,7 @@ export interface Context<U extends UserContext = UserContext> extends TypedEvent
|
|
|
58
59
|
limits?: ContextLimits;
|
|
59
60
|
status?: "normal" | "timeout";
|
|
60
61
|
userContext: U;
|
|
62
|
+
memories: Pick<Memory, "content">[];
|
|
61
63
|
/**
|
|
62
64
|
* Create a user agent to consistently invoke an agent
|
|
63
65
|
* @param agent Agent to invoke
|
|
@@ -131,6 +133,8 @@ export declare class AIGNEContext implements Context {
|
|
|
131
133
|
get usage(): ContextUsage;
|
|
132
134
|
get userContext(): Context["userContext"];
|
|
133
135
|
set userContext(userContext: Context["userContext"]);
|
|
136
|
+
get memories(): Context["memories"];
|
|
137
|
+
set memories(memories: Context["memories"]);
|
|
134
138
|
newContext({ reset }?: {
|
|
135
139
|
reset?: boolean;
|
|
136
140
|
}): AIGNEContext;
|
|
@@ -155,6 +159,7 @@ declare class AIGNEContextShared {
|
|
|
155
159
|
get limits(): ContextLimits | undefined;
|
|
156
160
|
usage: ContextUsage;
|
|
157
161
|
userContext: Context["userContext"];
|
|
162
|
+
memories: Context["memories"];
|
|
158
163
|
private abortController;
|
|
159
164
|
private timer?;
|
|
160
165
|
private initTimeout;
|
package/lib/cjs/aigne/context.js
CHANGED
|
@@ -54,6 +54,12 @@ class AIGNEContext {
|
|
|
54
54
|
set userContext(userContext) {
|
|
55
55
|
this.internal.userContext = userContext;
|
|
56
56
|
}
|
|
57
|
+
get memories() {
|
|
58
|
+
return this.internal.memories;
|
|
59
|
+
}
|
|
60
|
+
set memories(memories) {
|
|
61
|
+
this.internal.memories = memories;
|
|
62
|
+
}
|
|
57
63
|
newContext({ reset } = {}) {
|
|
58
64
|
if (reset)
|
|
59
65
|
return new AIGNEContext(this, { userContext: {} });
|
|
@@ -65,8 +71,14 @@ class AIGNEContext {
|
|
|
65
71
|
message,
|
|
66
72
|
options,
|
|
67
73
|
});
|
|
68
|
-
if (options?.userContext)
|
|
74
|
+
if (options?.userContext) {
|
|
69
75
|
Object.assign(this.userContext, options.userContext);
|
|
76
|
+
options.userContext = undefined;
|
|
77
|
+
}
|
|
78
|
+
if (options?.memories?.length) {
|
|
79
|
+
this.memories.push(...options.memories);
|
|
80
|
+
options.memories = undefined;
|
|
81
|
+
}
|
|
70
82
|
if ((0, type_utils_js_1.isNil)(message)) {
|
|
71
83
|
return user_agent_js_1.UserAgent.from({
|
|
72
84
|
context: this,
|
|
@@ -107,8 +119,14 @@ class AIGNEContext {
|
|
|
107
119
|
});
|
|
108
120
|
});
|
|
109
121
|
publish = ((topic, payload, options) => {
|
|
110
|
-
if (options?.userContext)
|
|
122
|
+
if (options?.userContext) {
|
|
111
123
|
Object.assign(this.userContext, options.userContext);
|
|
124
|
+
options.userContext = undefined;
|
|
125
|
+
}
|
|
126
|
+
if (options?.memories?.length) {
|
|
127
|
+
this.memories.push(...options.memories);
|
|
128
|
+
options.memories = undefined;
|
|
129
|
+
}
|
|
112
130
|
return this.internal.messageQueue.publish(topic, {
|
|
113
131
|
...(0, message_queue_js_1.toMessagePayload)(payload),
|
|
114
132
|
context: this,
|
|
@@ -150,6 +168,7 @@ class AIGNEContextShared {
|
|
|
150
168
|
this.parent = parent;
|
|
151
169
|
this.messageQueue = this.parent?.messageQueue ?? new message_queue_js_1.MessageQueue();
|
|
152
170
|
this.userContext = overrides?.userContext ?? {};
|
|
171
|
+
this.memories = overrides?.memories ?? [];
|
|
153
172
|
}
|
|
154
173
|
messageQueue;
|
|
155
174
|
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
@@ -165,6 +184,7 @@ class AIGNEContextShared {
|
|
|
165
184
|
}
|
|
166
185
|
usage = (0, usage_js_1.newEmptyContextUsage)();
|
|
167
186
|
userContext;
|
|
187
|
+
memories;
|
|
168
188
|
abortController = new AbortController();
|
|
169
189
|
timer;
|
|
170
190
|
initTimeout() {
|
package/lib/cjs/index.d.ts
CHANGED
package/lib/cjs/index.js
CHANGED
|
@@ -26,3 +26,4 @@ __exportStar(require("./aigne/index.js"), exports);
|
|
|
26
26
|
__exportStar(require("./memory/index.js"), exports);
|
|
27
27
|
__exportStar(require("./prompt/prompt-builder.js"), exports);
|
|
28
28
|
__exportStar(require("./prompt/template.js"), exports);
|
|
29
|
+
__exportStar(require("./utils/stream-utils.js"), exports);
|
|
@@ -15,7 +15,7 @@ export declare function getMessage(input: Message): string | undefined;
|
|
|
15
15
|
export interface PromptBuilderOptions {
|
|
16
16
|
instructions?: string | ChatMessagesTemplate;
|
|
17
17
|
}
|
|
18
|
-
export interface PromptBuildOptions extends AgentInvokeOptions {
|
|
18
|
+
export interface PromptBuildOptions extends Pick<AgentInvokeOptions, "context"> {
|
|
19
19
|
agent?: AIAgent;
|
|
20
20
|
input?: Message;
|
|
21
21
|
model?: ChatModel;
|
|
@@ -87,8 +87,8 @@ class PromptBuilder {
|
|
|
87
87
|
if (options.agent) {
|
|
88
88
|
memories.push(...(await options.agent.retrieveMemories({ search: options.input }, options)));
|
|
89
89
|
}
|
|
90
|
-
if (options.memories?.length) {
|
|
91
|
-
memories.push(...options.memories);
|
|
90
|
+
if (options.context.memories?.length) {
|
|
91
|
+
memories.push(...options.context.memories);
|
|
92
92
|
}
|
|
93
93
|
if (memories.length)
|
|
94
94
|
messages.push(...this.convertMemoriesToMessages(memories, options));
|
|
@@ -128,8 +128,7 @@ class PromptBuilder {
|
|
|
128
128
|
const toolAgents = (0, type_utils_js_1.unique)((options.context?.skills ?? [])
|
|
129
129
|
.concat(options.agent?.skills ?? [])
|
|
130
130
|
.concat(options.agent?.memoryAgentsAsTools ? options.agent.memories : [])
|
|
131
|
-
|
|
132
|
-
.flatMap((i) => (i.isInvokable ? i.skills.concat(i) : i.skills)), (i) => i.name);
|
|
131
|
+
.flatMap((i) => (i.isInvokable ? i : i.skills)), (i) => i.name);
|
|
133
132
|
const tools = toolAgents.map((i) => ({
|
|
134
133
|
type: "function",
|
|
135
134
|
function: {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
ReadableStream.prototype.values ??= function ({ preventCancel = false } = {}) {
|
|
4
|
+
const reader = this.getReader();
|
|
5
|
+
return {
|
|
6
|
+
async next() {
|
|
7
|
+
try {
|
|
8
|
+
const result = await reader.read();
|
|
9
|
+
if (result.done) {
|
|
10
|
+
reader.releaseLock();
|
|
11
|
+
}
|
|
12
|
+
return result;
|
|
13
|
+
}
|
|
14
|
+
catch (e) {
|
|
15
|
+
reader.releaseLock();
|
|
16
|
+
throw e;
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
async return(value) {
|
|
20
|
+
if (!preventCancel) {
|
|
21
|
+
const cancelPromise = reader.cancel(value);
|
|
22
|
+
reader.releaseLock();
|
|
23
|
+
await cancelPromise;
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
reader.releaseLock();
|
|
27
|
+
}
|
|
28
|
+
return { done: true, value };
|
|
29
|
+
},
|
|
30
|
+
[Symbol.asyncIterator]() {
|
|
31
|
+
return this;
|
|
32
|
+
},
|
|
33
|
+
async [Symbol.asyncDispose]() {
|
|
34
|
+
reader.releaseLock();
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
ReadableStream.prototype[Symbol.asyncIterator] ??= ReadableStream.prototype.values;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { type AgentProcessAsyncGenerator, type AgentResponseChunk, type AgentResponseStream, type Message } from "../agents/agent.js";
|
|
2
2
|
import type { MESSAGE_KEY } from "../prompt/prompt-builder.js";
|
|
3
3
|
import { type PromiseOrValue } from "./type-utils.js";
|
|
4
|
+
import "./stream-polyfill.js";
|
|
4
5
|
export declare function objectToAgentResponseStream<T extends Message>(json: T): AgentResponseStream<T>;
|
|
5
6
|
export declare function mergeAgentResponseChunk<T extends Message>(output: T, chunk: AgentResponseChunk<T>): T;
|
|
6
7
|
export declare function agentResponseStreamToObject<T extends Message>(stream: AgentResponseStream<T> | AgentProcessAsyncGenerator<T>): Promise<T>;
|
|
@@ -18,6 +18,7 @@ exports.readAllString = readAllString;
|
|
|
18
18
|
const fast_deep_equal_1 = __importDefault(require("fast-deep-equal"));
|
|
19
19
|
const agent_js_1 = require("../agents/agent.js");
|
|
20
20
|
const type_utils_js_1 = require("./type-utils.js");
|
|
21
|
+
require("./stream-polyfill.js");
|
|
21
22
|
function objectToAgentResponseStream(json) {
|
|
22
23
|
return new ReadableStream({
|
|
23
24
|
pull(controller) {
|
|
@@ -2,6 +2,7 @@ import { Emitter } from "strict-event-emitter";
|
|
|
2
2
|
import { Agent, type AgentInvokeOptions, type AgentProcessAsyncGenerator, type AgentResponse, type AgentResponseStream, type Message } from "../agents/agent.js";
|
|
3
3
|
import type { ChatModel } from "../agents/chat-model.js";
|
|
4
4
|
import { UserAgent } from "../agents/user-agent.js";
|
|
5
|
+
import type { Memory } from "../memory/memory.js";
|
|
5
6
|
import { type OmitPropertiesFromArrayFirstElement } from "../utils/type-utils.js";
|
|
6
7
|
import type { Args, Listener, TypedEventEmitter } from "../utils/typed-event-emtter.js";
|
|
7
8
|
import { type MessagePayload, MessageQueue, type MessageQueueListener, type Unsubscribe } from "./message-queue.js";
|
|
@@ -58,6 +59,7 @@ export interface Context<U extends UserContext = UserContext> extends TypedEvent
|
|
|
58
59
|
limits?: ContextLimits;
|
|
59
60
|
status?: "normal" | "timeout";
|
|
60
61
|
userContext: U;
|
|
62
|
+
memories: Pick<Memory, "content">[];
|
|
61
63
|
/**
|
|
62
64
|
* Create a user agent to consistently invoke an agent
|
|
63
65
|
* @param agent Agent to invoke
|
|
@@ -131,6 +133,8 @@ export declare class AIGNEContext implements Context {
|
|
|
131
133
|
get usage(): ContextUsage;
|
|
132
134
|
get userContext(): Context["userContext"];
|
|
133
135
|
set userContext(userContext: Context["userContext"]);
|
|
136
|
+
get memories(): Context["memories"];
|
|
137
|
+
set memories(memories: Context["memories"]);
|
|
134
138
|
newContext({ reset }?: {
|
|
135
139
|
reset?: boolean;
|
|
136
140
|
}): AIGNEContext;
|
|
@@ -155,6 +159,7 @@ declare class AIGNEContextShared {
|
|
|
155
159
|
get limits(): ContextLimits | undefined;
|
|
156
160
|
usage: ContextUsage;
|
|
157
161
|
userContext: Context["userContext"];
|
|
162
|
+
memories: Context["memories"];
|
|
158
163
|
private abortController;
|
|
159
164
|
private timer?;
|
|
160
165
|
private initTimeout;
|
package/lib/dts/index.d.ts
CHANGED
|
@@ -15,7 +15,7 @@ export declare function getMessage(input: Message): string | undefined;
|
|
|
15
15
|
export interface PromptBuilderOptions {
|
|
16
16
|
instructions?: string | ChatMessagesTemplate;
|
|
17
17
|
}
|
|
18
|
-
export interface PromptBuildOptions extends AgentInvokeOptions {
|
|
18
|
+
export interface PromptBuildOptions extends Pick<AgentInvokeOptions, "context"> {
|
|
19
19
|
agent?: AIAgent;
|
|
20
20
|
input?: Message;
|
|
21
21
|
model?: ChatModel;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { type AgentProcessAsyncGenerator, type AgentResponseChunk, type AgentResponseStream, type Message } from "../agents/agent.js";
|
|
2
2
|
import type { MESSAGE_KEY } from "../prompt/prompt-builder.js";
|
|
3
3
|
import { type PromiseOrValue } from "./type-utils.js";
|
|
4
|
+
import "./stream-polyfill.js";
|
|
4
5
|
export declare function objectToAgentResponseStream<T extends Message>(json: T): AgentResponseStream<T>;
|
|
5
6
|
export declare function mergeAgentResponseChunk<T extends Message>(output: T, chunk: AgentResponseChunk<T>): T;
|
|
6
7
|
export declare function agentResponseStreamToObject<T extends Message>(stream: AgentResponseStream<T> | AgentProcessAsyncGenerator<T>): Promise<T>;
|
package/lib/esm/agents/agent.js
CHANGED
|
@@ -294,6 +294,11 @@ export class Agent {
|
|
|
294
294
|
};
|
|
295
295
|
if (options.userContext) {
|
|
296
296
|
Object.assign(opts.context.userContext, options.userContext);
|
|
297
|
+
options.userContext = undefined;
|
|
298
|
+
}
|
|
299
|
+
if (options.memories?.length) {
|
|
300
|
+
opts.context.memories.push(...options.memories);
|
|
301
|
+
options.memories = undefined;
|
|
297
302
|
}
|
|
298
303
|
const message = typeof input === "string" ? createMessage(input) : input;
|
|
299
304
|
logger.debug("Invoke agent %s started with input: %O", this.name, input);
|
|
@@ -2,6 +2,7 @@ import { Emitter } from "strict-event-emitter";
|
|
|
2
2
|
import { Agent, type AgentInvokeOptions, type AgentProcessAsyncGenerator, type AgentResponse, type AgentResponseStream, type Message } from "../agents/agent.js";
|
|
3
3
|
import type { ChatModel } from "../agents/chat-model.js";
|
|
4
4
|
import { UserAgent } from "../agents/user-agent.js";
|
|
5
|
+
import type { Memory } from "../memory/memory.js";
|
|
5
6
|
import { type OmitPropertiesFromArrayFirstElement } from "../utils/type-utils.js";
|
|
6
7
|
import type { Args, Listener, TypedEventEmitter } from "../utils/typed-event-emtter.js";
|
|
7
8
|
import { type MessagePayload, MessageQueue, type MessageQueueListener, type Unsubscribe } from "./message-queue.js";
|
|
@@ -58,6 +59,7 @@ export interface Context<U extends UserContext = UserContext> extends TypedEvent
|
|
|
58
59
|
limits?: ContextLimits;
|
|
59
60
|
status?: "normal" | "timeout";
|
|
60
61
|
userContext: U;
|
|
62
|
+
memories: Pick<Memory, "content">[];
|
|
61
63
|
/**
|
|
62
64
|
* Create a user agent to consistently invoke an agent
|
|
63
65
|
* @param agent Agent to invoke
|
|
@@ -131,6 +133,8 @@ export declare class AIGNEContext implements Context {
|
|
|
131
133
|
get usage(): ContextUsage;
|
|
132
134
|
get userContext(): Context["userContext"];
|
|
133
135
|
set userContext(userContext: Context["userContext"]);
|
|
136
|
+
get memories(): Context["memories"];
|
|
137
|
+
set memories(memories: Context["memories"]);
|
|
134
138
|
newContext({ reset }?: {
|
|
135
139
|
reset?: boolean;
|
|
136
140
|
}): AIGNEContext;
|
|
@@ -155,6 +159,7 @@ declare class AIGNEContextShared {
|
|
|
155
159
|
get limits(): ContextLimits | undefined;
|
|
156
160
|
usage: ContextUsage;
|
|
157
161
|
userContext: Context["userContext"];
|
|
162
|
+
memories: Context["memories"];
|
|
158
163
|
private abortController;
|
|
159
164
|
private timer?;
|
|
160
165
|
private initTimeout;
|
package/lib/esm/aigne/context.js
CHANGED
|
@@ -48,6 +48,12 @@ export class AIGNEContext {
|
|
|
48
48
|
set userContext(userContext) {
|
|
49
49
|
this.internal.userContext = userContext;
|
|
50
50
|
}
|
|
51
|
+
get memories() {
|
|
52
|
+
return this.internal.memories;
|
|
53
|
+
}
|
|
54
|
+
set memories(memories) {
|
|
55
|
+
this.internal.memories = memories;
|
|
56
|
+
}
|
|
51
57
|
newContext({ reset } = {}) {
|
|
52
58
|
if (reset)
|
|
53
59
|
return new AIGNEContext(this, { userContext: {} });
|
|
@@ -59,8 +65,14 @@ export class AIGNEContext {
|
|
|
59
65
|
message,
|
|
60
66
|
options,
|
|
61
67
|
});
|
|
62
|
-
if (options?.userContext)
|
|
68
|
+
if (options?.userContext) {
|
|
63
69
|
Object.assign(this.userContext, options.userContext);
|
|
70
|
+
options.userContext = undefined;
|
|
71
|
+
}
|
|
72
|
+
if (options?.memories?.length) {
|
|
73
|
+
this.memories.push(...options.memories);
|
|
74
|
+
options.memories = undefined;
|
|
75
|
+
}
|
|
64
76
|
if (isNil(message)) {
|
|
65
77
|
return UserAgent.from({
|
|
66
78
|
context: this,
|
|
@@ -101,8 +113,14 @@ export class AIGNEContext {
|
|
|
101
113
|
});
|
|
102
114
|
});
|
|
103
115
|
publish = ((topic, payload, options) => {
|
|
104
|
-
if (options?.userContext)
|
|
116
|
+
if (options?.userContext) {
|
|
105
117
|
Object.assign(this.userContext, options.userContext);
|
|
118
|
+
options.userContext = undefined;
|
|
119
|
+
}
|
|
120
|
+
if (options?.memories?.length) {
|
|
121
|
+
this.memories.push(...options.memories);
|
|
122
|
+
options.memories = undefined;
|
|
123
|
+
}
|
|
106
124
|
return this.internal.messageQueue.publish(topic, {
|
|
107
125
|
...toMessagePayload(payload),
|
|
108
126
|
context: this,
|
|
@@ -143,6 +161,7 @@ class AIGNEContextShared {
|
|
|
143
161
|
this.parent = parent;
|
|
144
162
|
this.messageQueue = this.parent?.messageQueue ?? new MessageQueue();
|
|
145
163
|
this.userContext = overrides?.userContext ?? {};
|
|
164
|
+
this.memories = overrides?.memories ?? [];
|
|
146
165
|
}
|
|
147
166
|
messageQueue;
|
|
148
167
|
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
@@ -158,6 +177,7 @@ class AIGNEContextShared {
|
|
|
158
177
|
}
|
|
159
178
|
usage = newEmptyContextUsage();
|
|
160
179
|
userContext;
|
|
180
|
+
memories;
|
|
161
181
|
abortController = new AbortController();
|
|
162
182
|
timer;
|
|
163
183
|
initTimeout() {
|
package/lib/esm/index.d.ts
CHANGED
package/lib/esm/index.js
CHANGED
|
@@ -15,7 +15,7 @@ export declare function getMessage(input: Message): string | undefined;
|
|
|
15
15
|
export interface PromptBuilderOptions {
|
|
16
16
|
instructions?: string | ChatMessagesTemplate;
|
|
17
17
|
}
|
|
18
|
-
export interface PromptBuildOptions extends AgentInvokeOptions {
|
|
18
|
+
export interface PromptBuildOptions extends Pick<AgentInvokeOptions, "context"> {
|
|
19
19
|
agent?: AIAgent;
|
|
20
20
|
input?: Message;
|
|
21
21
|
model?: ChatModel;
|
|
@@ -82,8 +82,8 @@ export class PromptBuilder {
|
|
|
82
82
|
if (options.agent) {
|
|
83
83
|
memories.push(...(await options.agent.retrieveMemories({ search: options.input }, options)));
|
|
84
84
|
}
|
|
85
|
-
if (options.memories?.length) {
|
|
86
|
-
memories.push(...options.memories);
|
|
85
|
+
if (options.context.memories?.length) {
|
|
86
|
+
memories.push(...options.context.memories);
|
|
87
87
|
}
|
|
88
88
|
if (memories.length)
|
|
89
89
|
messages.push(...this.convertMemoriesToMessages(memories, options));
|
|
@@ -123,8 +123,7 @@ export class PromptBuilder {
|
|
|
123
123
|
const toolAgents = unique((options.context?.skills ?? [])
|
|
124
124
|
.concat(options.agent?.skills ?? [])
|
|
125
125
|
.concat(options.agent?.memoryAgentsAsTools ? options.agent.memories : [])
|
|
126
|
-
|
|
127
|
-
.flatMap((i) => (i.isInvokable ? i.skills.concat(i) : i.skills)), (i) => i.name);
|
|
126
|
+
.flatMap((i) => (i.isInvokable ? i : i.skills)), (i) => i.name);
|
|
128
127
|
const tools = toolAgents.map((i) => ({
|
|
129
128
|
type: "function",
|
|
130
129
|
function: {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
ReadableStream.prototype.values ??= function ({ preventCancel = false } = {}) {
|
|
2
|
+
const reader = this.getReader();
|
|
3
|
+
return {
|
|
4
|
+
async next() {
|
|
5
|
+
try {
|
|
6
|
+
const result = await reader.read();
|
|
7
|
+
if (result.done) {
|
|
8
|
+
reader.releaseLock();
|
|
9
|
+
}
|
|
10
|
+
return result;
|
|
11
|
+
}
|
|
12
|
+
catch (e) {
|
|
13
|
+
reader.releaseLock();
|
|
14
|
+
throw e;
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
async return(value) {
|
|
18
|
+
if (!preventCancel) {
|
|
19
|
+
const cancelPromise = reader.cancel(value);
|
|
20
|
+
reader.releaseLock();
|
|
21
|
+
await cancelPromise;
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
reader.releaseLock();
|
|
25
|
+
}
|
|
26
|
+
return { done: true, value };
|
|
27
|
+
},
|
|
28
|
+
[Symbol.asyncIterator]() {
|
|
29
|
+
return this;
|
|
30
|
+
},
|
|
31
|
+
async [Symbol.asyncDispose]() {
|
|
32
|
+
reader.releaseLock();
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
ReadableStream.prototype[Symbol.asyncIterator] ??= ReadableStream.prototype.values;
|
|
37
|
+
export {};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { type AgentProcessAsyncGenerator, type AgentResponseChunk, type AgentResponseStream, type Message } from "../agents/agent.js";
|
|
2
2
|
import type { MESSAGE_KEY } from "../prompt/prompt-builder.js";
|
|
3
3
|
import { type PromiseOrValue } from "./type-utils.js";
|
|
4
|
+
import "./stream-polyfill.js";
|
|
4
5
|
export declare function objectToAgentResponseStream<T extends Message>(json: T): AgentResponseStream<T>;
|
|
5
6
|
export declare function mergeAgentResponseChunk<T extends Message>(output: T, chunk: AgentResponseChunk<T>): T;
|
|
6
7
|
export declare function agentResponseStreamToObject<T extends Message>(stream: AgentResponseStream<T> | AgentProcessAsyncGenerator<T>): Promise<T>;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import equal from "fast-deep-equal";
|
|
2
2
|
import { isEmptyChunk, } from "../agents/agent.js";
|
|
3
3
|
import { omitBy } from "./type-utils.js";
|
|
4
|
+
import "./stream-polyfill.js";
|
|
4
5
|
export function objectToAgentResponseStream(json) {
|
|
5
6
|
return new ReadableStream({
|
|
6
7
|
pull(controller) {
|